extent_io.c 140 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608
  1. #include <linux/bitops.h>
  2. #include <linux/slab.h>
  3. #include <linux/bio.h>
  4. #include <linux/mm.h>
  5. #include <linux/pagemap.h>
  6. #include <linux/page-flags.h>
  7. #include <linux/spinlock.h>
  8. #include <linux/blkdev.h>
  9. #include <linux/swap.h>
  10. #include <linux/writeback.h>
  11. #include <linux/pagevec.h>
  12. #include <linux/prefetch.h>
  13. #include <linux/cleancache.h>
  14. #include "extent_io.h"
  15. #include "extent_map.h"
  16. #include "ctree.h"
  17. #include "btrfs_inode.h"
  18. #include "volumes.h"
  19. #include "check-integrity.h"
  20. #include "locking.h"
  21. #include "rcu-string.h"
  22. #include "backref.h"
  23. static struct kmem_cache *extent_state_cache;
  24. static struct kmem_cache *extent_buffer_cache;
  25. static struct bio_set *btrfs_bioset;
  26. static inline bool extent_state_in_tree(const struct extent_state *state)
  27. {
  28. return !RB_EMPTY_NODE(&state->rb_node);
  29. }
  30. #ifdef CONFIG_BTRFS_DEBUG
  31. static LIST_HEAD(buffers);
  32. static LIST_HEAD(states);
  33. static DEFINE_SPINLOCK(leak_lock);
  34. static inline
  35. void btrfs_leak_debug_add(struct list_head *new, struct list_head *head)
  36. {
  37. unsigned long flags;
  38. spin_lock_irqsave(&leak_lock, flags);
  39. list_add(new, head);
  40. spin_unlock_irqrestore(&leak_lock, flags);
  41. }
  42. static inline
  43. void btrfs_leak_debug_del(struct list_head *entry)
  44. {
  45. unsigned long flags;
  46. spin_lock_irqsave(&leak_lock, flags);
  47. list_del(entry);
  48. spin_unlock_irqrestore(&leak_lock, flags);
  49. }
  50. static inline
  51. void btrfs_leak_debug_check(void)
  52. {
  53. struct extent_state *state;
  54. struct extent_buffer *eb;
  55. while (!list_empty(&states)) {
  56. state = list_entry(states.next, struct extent_state, leak_list);
  57. pr_err("BTRFS: state leak: start %llu end %llu state %u in tree %d refs %d\n",
  58. state->start, state->end, state->state,
  59. extent_state_in_tree(state),
  60. atomic_read(&state->refs));
  61. list_del(&state->leak_list);
  62. kmem_cache_free(extent_state_cache, state);
  63. }
  64. while (!list_empty(&buffers)) {
  65. eb = list_entry(buffers.next, struct extent_buffer, leak_list);
  66. printk(KERN_ERR "BTRFS: buffer leak start %llu len %lu "
  67. "refs %d\n",
  68. eb->start, eb->len, atomic_read(&eb->refs));
  69. list_del(&eb->leak_list);
  70. kmem_cache_free(extent_buffer_cache, eb);
  71. }
  72. }
  73. #define btrfs_debug_check_extent_io_range(tree, start, end) \
  74. __btrfs_debug_check_extent_io_range(__func__, (tree), (start), (end))
  75. static inline void __btrfs_debug_check_extent_io_range(const char *caller,
  76. struct extent_io_tree *tree, u64 start, u64 end)
  77. {
  78. struct inode *inode;
  79. u64 isize;
  80. if (!tree->mapping)
  81. return;
  82. inode = tree->mapping->host;
  83. isize = i_size_read(inode);
  84. if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
  85. printk_ratelimited(KERN_DEBUG
  86. "BTRFS: %s: ino %llu isize %llu odd range [%llu,%llu]\n",
  87. caller, btrfs_ino(inode), isize, start, end);
  88. }
  89. }
  90. #else
  91. #define btrfs_leak_debug_add(new, head) do {} while (0)
  92. #define btrfs_leak_debug_del(entry) do {} while (0)
  93. #define btrfs_leak_debug_check() do {} while (0)
  94. #define btrfs_debug_check_extent_io_range(c, s, e) do {} while (0)
  95. #endif
  96. #define BUFFER_LRU_MAX 64
  97. struct tree_entry {
  98. u64 start;
  99. u64 end;
  100. struct rb_node rb_node;
  101. };
  102. struct extent_page_data {
  103. struct bio *bio;
  104. struct extent_io_tree *tree;
  105. get_extent_t *get_extent;
  106. unsigned long bio_flags;
  107. /* tells writepage not to lock the state bits for this range
  108. * it still does the unlocking
  109. */
  110. unsigned int extent_locked:1;
  111. /* tells the submit_bio code to use a WRITE_SYNC */
  112. unsigned int sync_io:1;
  113. };
  114. static noinline void flush_write_bio(void *data);
  115. static inline struct btrfs_fs_info *
  116. tree_fs_info(struct extent_io_tree *tree)
  117. {
  118. if (!tree->mapping)
  119. return NULL;
  120. return btrfs_sb(tree->mapping->host->i_sb);
  121. }
  122. int __init extent_io_init(void)
  123. {
  124. extent_state_cache = kmem_cache_create("btrfs_extent_state",
  125. sizeof(struct extent_state), 0,
  126. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  127. if (!extent_state_cache)
  128. return -ENOMEM;
  129. extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
  130. sizeof(struct extent_buffer), 0,
  131. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  132. if (!extent_buffer_cache)
  133. goto free_state_cache;
  134. btrfs_bioset = bioset_create(BIO_POOL_SIZE,
  135. offsetof(struct btrfs_io_bio, bio));
  136. if (!btrfs_bioset)
  137. goto free_buffer_cache;
  138. if (bioset_integrity_create(btrfs_bioset, BIO_POOL_SIZE))
  139. goto free_bioset;
  140. return 0;
  141. free_bioset:
  142. bioset_free(btrfs_bioset);
  143. btrfs_bioset = NULL;
  144. free_buffer_cache:
  145. kmem_cache_destroy(extent_buffer_cache);
  146. extent_buffer_cache = NULL;
  147. free_state_cache:
  148. kmem_cache_destroy(extent_state_cache);
  149. extent_state_cache = NULL;
  150. return -ENOMEM;
  151. }
  152. void extent_io_exit(void)
  153. {
  154. btrfs_leak_debug_check();
  155. /*
  156. * Make sure all delayed rcu free are flushed before we
  157. * destroy caches.
  158. */
  159. rcu_barrier();
  160. if (extent_state_cache)
  161. kmem_cache_destroy(extent_state_cache);
  162. if (extent_buffer_cache)
  163. kmem_cache_destroy(extent_buffer_cache);
  164. if (btrfs_bioset)
  165. bioset_free(btrfs_bioset);
  166. }
  167. void extent_io_tree_init(struct extent_io_tree *tree,
  168. struct address_space *mapping)
  169. {
  170. tree->state = RB_ROOT;
  171. tree->ops = NULL;
  172. tree->dirty_bytes = 0;
  173. spin_lock_init(&tree->lock);
  174. tree->mapping = mapping;
  175. }
  176. static struct extent_state *alloc_extent_state(gfp_t mask)
  177. {
  178. struct extent_state *state;
  179. state = kmem_cache_alloc(extent_state_cache, mask);
  180. if (!state)
  181. return state;
  182. state->state = 0;
  183. state->private = 0;
  184. RB_CLEAR_NODE(&state->rb_node);
  185. btrfs_leak_debug_add(&state->leak_list, &states);
  186. atomic_set(&state->refs, 1);
  187. init_waitqueue_head(&state->wq);
  188. trace_alloc_extent_state(state, mask, _RET_IP_);
  189. return state;
  190. }
  191. void free_extent_state(struct extent_state *state)
  192. {
  193. if (!state)
  194. return;
  195. if (atomic_dec_and_test(&state->refs)) {
  196. WARN_ON(extent_state_in_tree(state));
  197. btrfs_leak_debug_del(&state->leak_list);
  198. trace_free_extent_state(state, _RET_IP_);
  199. kmem_cache_free(extent_state_cache, state);
  200. }
  201. }
  202. static struct rb_node *tree_insert(struct rb_root *root,
  203. struct rb_node *search_start,
  204. u64 offset,
  205. struct rb_node *node,
  206. struct rb_node ***p_in,
  207. struct rb_node **parent_in)
  208. {
  209. struct rb_node **p;
  210. struct rb_node *parent = NULL;
  211. struct tree_entry *entry;
  212. if (p_in && parent_in) {
  213. p = *p_in;
  214. parent = *parent_in;
  215. goto do_insert;
  216. }
  217. p = search_start ? &search_start : &root->rb_node;
  218. while (*p) {
  219. parent = *p;
  220. entry = rb_entry(parent, struct tree_entry, rb_node);
  221. if (offset < entry->start)
  222. p = &(*p)->rb_left;
  223. else if (offset > entry->end)
  224. p = &(*p)->rb_right;
  225. else
  226. return parent;
  227. }
  228. do_insert:
  229. rb_link_node(node, parent, p);
  230. rb_insert_color(node, root);
  231. return NULL;
  232. }
  233. static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
  234. struct rb_node **prev_ret,
  235. struct rb_node **next_ret,
  236. struct rb_node ***p_ret,
  237. struct rb_node **parent_ret)
  238. {
  239. struct rb_root *root = &tree->state;
  240. struct rb_node **n = &root->rb_node;
  241. struct rb_node *prev = NULL;
  242. struct rb_node *orig_prev = NULL;
  243. struct tree_entry *entry;
  244. struct tree_entry *prev_entry = NULL;
  245. while (*n) {
  246. prev = *n;
  247. entry = rb_entry(prev, struct tree_entry, rb_node);
  248. prev_entry = entry;
  249. if (offset < entry->start)
  250. n = &(*n)->rb_left;
  251. else if (offset > entry->end)
  252. n = &(*n)->rb_right;
  253. else
  254. return *n;
  255. }
  256. if (p_ret)
  257. *p_ret = n;
  258. if (parent_ret)
  259. *parent_ret = prev;
  260. if (prev_ret) {
  261. orig_prev = prev;
  262. while (prev && offset > prev_entry->end) {
  263. prev = rb_next(prev);
  264. prev_entry = rb_entry(prev, struct tree_entry, rb_node);
  265. }
  266. *prev_ret = prev;
  267. prev = orig_prev;
  268. }
  269. if (next_ret) {
  270. prev_entry = rb_entry(prev, struct tree_entry, rb_node);
  271. while (prev && offset < prev_entry->start) {
  272. prev = rb_prev(prev);
  273. prev_entry = rb_entry(prev, struct tree_entry, rb_node);
  274. }
  275. *next_ret = prev;
  276. }
  277. return NULL;
  278. }
  279. static inline struct rb_node *
  280. tree_search_for_insert(struct extent_io_tree *tree,
  281. u64 offset,
  282. struct rb_node ***p_ret,
  283. struct rb_node **parent_ret)
  284. {
  285. struct rb_node *prev = NULL;
  286. struct rb_node *ret;
  287. ret = __etree_search(tree, offset, &prev, NULL, p_ret, parent_ret);
  288. if (!ret)
  289. return prev;
  290. return ret;
  291. }
  292. static inline struct rb_node *tree_search(struct extent_io_tree *tree,
  293. u64 offset)
  294. {
  295. return tree_search_for_insert(tree, offset, NULL, NULL);
  296. }
  297. static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
  298. struct extent_state *other)
  299. {
  300. if (tree->ops && tree->ops->merge_extent_hook)
  301. tree->ops->merge_extent_hook(tree->mapping->host, new,
  302. other);
  303. }
  304. /*
  305. * utility function to look for merge candidates inside a given range.
  306. * Any extents with matching state are merged together into a single
  307. * extent in the tree. Extents with EXTENT_IO in their state field
  308. * are not merged because the end_io handlers need to be able to do
  309. * operations on them without sleeping (or doing allocations/splits).
  310. *
  311. * This should be called with the tree lock held.
  312. */
  313. static void merge_state(struct extent_io_tree *tree,
  314. struct extent_state *state)
  315. {
  316. struct extent_state *other;
  317. struct rb_node *other_node;
  318. if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
  319. return;
  320. other_node = rb_prev(&state->rb_node);
  321. if (other_node) {
  322. other = rb_entry(other_node, struct extent_state, rb_node);
  323. if (other->end == state->start - 1 &&
  324. other->state == state->state) {
  325. merge_cb(tree, state, other);
  326. state->start = other->start;
  327. rb_erase(&other->rb_node, &tree->state);
  328. RB_CLEAR_NODE(&other->rb_node);
  329. free_extent_state(other);
  330. }
  331. }
  332. other_node = rb_next(&state->rb_node);
  333. if (other_node) {
  334. other = rb_entry(other_node, struct extent_state, rb_node);
  335. if (other->start == state->end + 1 &&
  336. other->state == state->state) {
  337. merge_cb(tree, state, other);
  338. state->end = other->end;
  339. rb_erase(&other->rb_node, &tree->state);
  340. RB_CLEAR_NODE(&other->rb_node);
  341. free_extent_state(other);
  342. }
  343. }
  344. }
  345. static void set_state_cb(struct extent_io_tree *tree,
  346. struct extent_state *state, unsigned *bits)
  347. {
  348. if (tree->ops && tree->ops->set_bit_hook)
  349. tree->ops->set_bit_hook(tree->mapping->host, state, bits);
  350. }
  351. static void clear_state_cb(struct extent_io_tree *tree,
  352. struct extent_state *state, unsigned *bits)
  353. {
  354. if (tree->ops && tree->ops->clear_bit_hook)
  355. tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
  356. }
  357. static void set_state_bits(struct extent_io_tree *tree,
  358. struct extent_state *state, unsigned *bits);
  359. /*
  360. * insert an extent_state struct into the tree. 'bits' are set on the
  361. * struct before it is inserted.
  362. *
  363. * This may return -EEXIST if the extent is already there, in which case the
  364. * state struct is freed.
  365. *
  366. * The tree lock is not taken internally. This is a utility function and
  367. * probably isn't what you want to call (see set/clear_extent_bit).
  368. */
  369. static int insert_state(struct extent_io_tree *tree,
  370. struct extent_state *state, u64 start, u64 end,
  371. struct rb_node ***p,
  372. struct rb_node **parent,
  373. unsigned *bits)
  374. {
  375. struct rb_node *node;
  376. if (end < start)
  377. WARN(1, KERN_ERR "BTRFS: end < start %llu %llu\n",
  378. end, start);
  379. state->start = start;
  380. state->end = end;
  381. set_state_bits(tree, state, bits);
  382. node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent);
  383. if (node) {
  384. struct extent_state *found;
  385. found = rb_entry(node, struct extent_state, rb_node);
  386. printk(KERN_ERR "BTRFS: found node %llu %llu on insert of "
  387. "%llu %llu\n",
  388. found->start, found->end, start, end);
  389. return -EEXIST;
  390. }
  391. merge_state(tree, state);
  392. return 0;
  393. }
  394. static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
  395. u64 split)
  396. {
  397. if (tree->ops && tree->ops->split_extent_hook)
  398. tree->ops->split_extent_hook(tree->mapping->host, orig, split);
  399. }
  400. /*
  401. * split a given extent state struct in two, inserting the preallocated
  402. * struct 'prealloc' as the newly created second half. 'split' indicates an
  403. * offset inside 'orig' where it should be split.
  404. *
  405. * Before calling,
  406. * the tree has 'orig' at [orig->start, orig->end]. After calling, there
  407. * are two extent state structs in the tree:
  408. * prealloc: [orig->start, split - 1]
  409. * orig: [ split, orig->end ]
  410. *
  411. * The tree locks are not taken by this function. They need to be held
  412. * by the caller.
  413. */
  414. static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
  415. struct extent_state *prealloc, u64 split)
  416. {
  417. struct rb_node *node;
  418. split_cb(tree, orig, split);
  419. prealloc->start = orig->start;
  420. prealloc->end = split - 1;
  421. prealloc->state = orig->state;
  422. orig->start = split;
  423. node = tree_insert(&tree->state, &orig->rb_node, prealloc->end,
  424. &prealloc->rb_node, NULL, NULL);
  425. if (node) {
  426. free_extent_state(prealloc);
  427. return -EEXIST;
  428. }
  429. return 0;
  430. }
  431. static struct extent_state *next_state(struct extent_state *state)
  432. {
  433. struct rb_node *next = rb_next(&state->rb_node);
  434. if (next)
  435. return rb_entry(next, struct extent_state, rb_node);
  436. else
  437. return NULL;
  438. }
  439. /*
  440. * utility function to clear some bits in an extent state struct.
  441. * it will optionally wake up any one waiting on this state (wake == 1).
  442. *
  443. * If no bits are set on the state struct after clearing things, the
  444. * struct is freed and removed from the tree
  445. */
  446. static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
  447. struct extent_state *state,
  448. unsigned *bits, int wake)
  449. {
  450. struct extent_state *next;
  451. unsigned bits_to_clear = *bits & ~EXTENT_CTLBITS;
  452. if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
  453. u64 range = state->end - state->start + 1;
  454. WARN_ON(range > tree->dirty_bytes);
  455. tree->dirty_bytes -= range;
  456. }
  457. clear_state_cb(tree, state, bits);
  458. state->state &= ~bits_to_clear;
  459. if (wake)
  460. wake_up(&state->wq);
  461. if (state->state == 0) {
  462. next = next_state(state);
  463. if (extent_state_in_tree(state)) {
  464. rb_erase(&state->rb_node, &tree->state);
  465. RB_CLEAR_NODE(&state->rb_node);
  466. free_extent_state(state);
  467. } else {
  468. WARN_ON(1);
  469. }
  470. } else {
  471. merge_state(tree, state);
  472. next = next_state(state);
  473. }
  474. return next;
  475. }
  476. static struct extent_state *
  477. alloc_extent_state_atomic(struct extent_state *prealloc)
  478. {
  479. if (!prealloc)
  480. prealloc = alloc_extent_state(GFP_ATOMIC);
  481. return prealloc;
  482. }
  483. static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
  484. {
  485. btrfs_panic(tree_fs_info(tree), err, "Locking error: "
  486. "Extent tree was modified by another "
  487. "thread while locked.");
  488. }
  489. /*
  490. * clear some bits on a range in the tree. This may require splitting
  491. * or inserting elements in the tree, so the gfp mask is used to
  492. * indicate which allocations or sleeping are allowed.
  493. *
  494. * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
  495. * the given range from the tree regardless of state (ie for truncate).
  496. *
  497. * the range [start, end] is inclusive.
  498. *
  499. * This takes the tree lock, and returns 0 on success and < 0 on error.
  500. */
  501. int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  502. unsigned bits, int wake, int delete,
  503. struct extent_state **cached_state,
  504. gfp_t mask)
  505. {
  506. struct extent_state *state;
  507. struct extent_state *cached;
  508. struct extent_state *prealloc = NULL;
  509. struct rb_node *node;
  510. u64 last_end;
  511. int err;
  512. int clear = 0;
  513. btrfs_debug_check_extent_io_range(tree, start, end);
  514. if (bits & EXTENT_DELALLOC)
  515. bits |= EXTENT_NORESERVE;
  516. if (delete)
  517. bits |= ~EXTENT_CTLBITS;
  518. bits |= EXTENT_FIRST_DELALLOC;
  519. if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
  520. clear = 1;
  521. again:
  522. if (!prealloc && (mask & __GFP_WAIT)) {
  523. /*
  524. * Don't care for allocation failure here because we might end
  525. * up not needing the pre-allocated extent state at all, which
  526. * is the case if we only have in the tree extent states that
  527. * cover our input range and don't cover too any other range.
  528. * If we end up needing a new extent state we allocate it later.
  529. */
  530. prealloc = alloc_extent_state(mask);
  531. }
  532. spin_lock(&tree->lock);
  533. if (cached_state) {
  534. cached = *cached_state;
  535. if (clear) {
  536. *cached_state = NULL;
  537. cached_state = NULL;
  538. }
  539. if (cached && extent_state_in_tree(cached) &&
  540. cached->start <= start && cached->end > start) {
  541. if (clear)
  542. atomic_dec(&cached->refs);
  543. state = cached;
  544. goto hit_next;
  545. }
  546. if (clear)
  547. free_extent_state(cached);
  548. }
  549. /*
  550. * this search will find the extents that end after
  551. * our range starts
  552. */
  553. node = tree_search(tree, start);
  554. if (!node)
  555. goto out;
  556. state = rb_entry(node, struct extent_state, rb_node);
  557. hit_next:
  558. if (state->start > end)
  559. goto out;
  560. WARN_ON(state->end < start);
  561. last_end = state->end;
  562. /* the state doesn't have the wanted bits, go ahead */
  563. if (!(state->state & bits)) {
  564. state = next_state(state);
  565. goto next;
  566. }
  567. /*
  568. * | ---- desired range ---- |
  569. * | state | or
  570. * | ------------- state -------------- |
  571. *
  572. * We need to split the extent we found, and may flip
  573. * bits on second half.
  574. *
  575. * If the extent we found extends past our range, we
  576. * just split and search again. It'll get split again
  577. * the next time though.
  578. *
  579. * If the extent we found is inside our range, we clear
  580. * the desired bit on it.
  581. */
  582. if (state->start < start) {
  583. prealloc = alloc_extent_state_atomic(prealloc);
  584. BUG_ON(!prealloc);
  585. err = split_state(tree, state, prealloc, start);
  586. if (err)
  587. extent_io_tree_panic(tree, err);
  588. prealloc = NULL;
  589. if (err)
  590. goto out;
  591. if (state->end <= end) {
  592. state = clear_state_bit(tree, state, &bits, wake);
  593. goto next;
  594. }
  595. goto search_again;
  596. }
  597. /*
  598. * | ---- desired range ---- |
  599. * | state |
  600. * We need to split the extent, and clear the bit
  601. * on the first half
  602. */
  603. if (state->start <= end && state->end > end) {
  604. prealloc = alloc_extent_state_atomic(prealloc);
  605. BUG_ON(!prealloc);
  606. err = split_state(tree, state, prealloc, end + 1);
  607. if (err)
  608. extent_io_tree_panic(tree, err);
  609. if (wake)
  610. wake_up(&state->wq);
  611. clear_state_bit(tree, prealloc, &bits, wake);
  612. prealloc = NULL;
  613. goto out;
  614. }
  615. state = clear_state_bit(tree, state, &bits, wake);
  616. next:
  617. if (last_end == (u64)-1)
  618. goto out;
  619. start = last_end + 1;
  620. if (start <= end && state && !need_resched())
  621. goto hit_next;
  622. goto search_again;
  623. out:
  624. spin_unlock(&tree->lock);
  625. if (prealloc)
  626. free_extent_state(prealloc);
  627. return 0;
  628. search_again:
  629. if (start > end)
  630. goto out;
  631. spin_unlock(&tree->lock);
  632. if (mask & __GFP_WAIT)
  633. cond_resched();
  634. goto again;
  635. }
  636. static void wait_on_state(struct extent_io_tree *tree,
  637. struct extent_state *state)
  638. __releases(tree->lock)
  639. __acquires(tree->lock)
  640. {
  641. DEFINE_WAIT(wait);
  642. prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
  643. spin_unlock(&tree->lock);
  644. schedule();
  645. spin_lock(&tree->lock);
  646. finish_wait(&state->wq, &wait);
  647. }
  648. /*
  649. * waits for one or more bits to clear on a range in the state tree.
  650. * The range [start, end] is inclusive.
  651. * The tree lock is taken by this function
  652. */
  653. static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  654. unsigned long bits)
  655. {
  656. struct extent_state *state;
  657. struct rb_node *node;
  658. btrfs_debug_check_extent_io_range(tree, start, end);
  659. spin_lock(&tree->lock);
  660. again:
  661. while (1) {
  662. /*
  663. * this search will find all the extents that end after
  664. * our range starts
  665. */
  666. node = tree_search(tree, start);
  667. process_node:
  668. if (!node)
  669. break;
  670. state = rb_entry(node, struct extent_state, rb_node);
  671. if (state->start > end)
  672. goto out;
  673. if (state->state & bits) {
  674. start = state->start;
  675. atomic_inc(&state->refs);
  676. wait_on_state(tree, state);
  677. free_extent_state(state);
  678. goto again;
  679. }
  680. start = state->end + 1;
  681. if (start > end)
  682. break;
  683. if (!cond_resched_lock(&tree->lock)) {
  684. node = rb_next(node);
  685. goto process_node;
  686. }
  687. }
  688. out:
  689. spin_unlock(&tree->lock);
  690. }
  691. static void set_state_bits(struct extent_io_tree *tree,
  692. struct extent_state *state,
  693. unsigned *bits)
  694. {
  695. unsigned bits_to_set = *bits & ~EXTENT_CTLBITS;
  696. set_state_cb(tree, state, bits);
  697. if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
  698. u64 range = state->end - state->start + 1;
  699. tree->dirty_bytes += range;
  700. }
  701. state->state |= bits_to_set;
  702. }
  703. static void cache_state_if_flags(struct extent_state *state,
  704. struct extent_state **cached_ptr,
  705. unsigned flags)
  706. {
  707. if (cached_ptr && !(*cached_ptr)) {
  708. if (!flags || (state->state & flags)) {
  709. *cached_ptr = state;
  710. atomic_inc(&state->refs);
  711. }
  712. }
  713. }
  714. static void cache_state(struct extent_state *state,
  715. struct extent_state **cached_ptr)
  716. {
  717. return cache_state_if_flags(state, cached_ptr,
  718. EXTENT_IOBITS | EXTENT_BOUNDARY);
  719. }
  720. /*
  721. * set some bits on a range in the tree. This may require allocations or
  722. * sleeping, so the gfp mask is used to indicate what is allowed.
  723. *
  724. * If any of the exclusive bits are set, this will fail with -EEXIST if some
  725. * part of the range already has the desired bits set. The start of the
  726. * existing range is returned in failed_start in this case.
  727. *
  728. * [start, end] is inclusive This takes the tree lock.
  729. */
  730. static int __must_check
  731. __set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  732. unsigned bits, unsigned exclusive_bits,
  733. u64 *failed_start, struct extent_state **cached_state,
  734. gfp_t mask)
  735. {
  736. struct extent_state *state;
  737. struct extent_state *prealloc = NULL;
  738. struct rb_node *node;
  739. struct rb_node **p;
  740. struct rb_node *parent;
  741. int err = 0;
  742. u64 last_start;
  743. u64 last_end;
  744. btrfs_debug_check_extent_io_range(tree, start, end);
  745. bits |= EXTENT_FIRST_DELALLOC;
  746. again:
  747. if (!prealloc && (mask & __GFP_WAIT)) {
  748. prealloc = alloc_extent_state(mask);
  749. BUG_ON(!prealloc);
  750. }
  751. spin_lock(&tree->lock);
  752. if (cached_state && *cached_state) {
  753. state = *cached_state;
  754. if (state->start <= start && state->end > start &&
  755. extent_state_in_tree(state)) {
  756. node = &state->rb_node;
  757. goto hit_next;
  758. }
  759. }
  760. /*
  761. * this search will find all the extents that end after
  762. * our range starts.
  763. */
  764. node = tree_search_for_insert(tree, start, &p, &parent);
  765. if (!node) {
  766. prealloc = alloc_extent_state_atomic(prealloc);
  767. BUG_ON(!prealloc);
  768. err = insert_state(tree, prealloc, start, end,
  769. &p, &parent, &bits);
  770. if (err)
  771. extent_io_tree_panic(tree, err);
  772. cache_state(prealloc, cached_state);
  773. prealloc = NULL;
  774. goto out;
  775. }
  776. state = rb_entry(node, struct extent_state, rb_node);
  777. hit_next:
  778. last_start = state->start;
  779. last_end = state->end;
  780. /*
  781. * | ---- desired range ---- |
  782. * | state |
  783. *
  784. * Just lock what we found and keep going
  785. */
  786. if (state->start == start && state->end <= end) {
  787. if (state->state & exclusive_bits) {
  788. *failed_start = state->start;
  789. err = -EEXIST;
  790. goto out;
  791. }
  792. set_state_bits(tree, state, &bits);
  793. cache_state(state, cached_state);
  794. merge_state(tree, state);
  795. if (last_end == (u64)-1)
  796. goto out;
  797. start = last_end + 1;
  798. state = next_state(state);
  799. if (start < end && state && state->start == start &&
  800. !need_resched())
  801. goto hit_next;
  802. goto search_again;
  803. }
  804. /*
  805. * | ---- desired range ---- |
  806. * | state |
  807. * or
  808. * | ------------- state -------------- |
  809. *
  810. * We need to split the extent we found, and may flip bits on
  811. * second half.
  812. *
  813. * If the extent we found extends past our
  814. * range, we just split and search again. It'll get split
  815. * again the next time though.
  816. *
  817. * If the extent we found is inside our range, we set the
  818. * desired bit on it.
  819. */
  820. if (state->start < start) {
  821. if (state->state & exclusive_bits) {
  822. *failed_start = start;
  823. err = -EEXIST;
  824. goto out;
  825. }
  826. prealloc = alloc_extent_state_atomic(prealloc);
  827. BUG_ON(!prealloc);
  828. err = split_state(tree, state, prealloc, start);
  829. if (err)
  830. extent_io_tree_panic(tree, err);
  831. prealloc = NULL;
  832. if (err)
  833. goto out;
  834. if (state->end <= end) {
  835. set_state_bits(tree, state, &bits);
  836. cache_state(state, cached_state);
  837. merge_state(tree, state);
  838. if (last_end == (u64)-1)
  839. goto out;
  840. start = last_end + 1;
  841. state = next_state(state);
  842. if (start < end && state && state->start == start &&
  843. !need_resched())
  844. goto hit_next;
  845. }
  846. goto search_again;
  847. }
  848. /*
  849. * | ---- desired range ---- |
  850. * | state | or | state |
  851. *
  852. * There's a hole, we need to insert something in it and
  853. * ignore the extent we found.
  854. */
  855. if (state->start > start) {
  856. u64 this_end;
  857. if (end < last_start)
  858. this_end = end;
  859. else
  860. this_end = last_start - 1;
  861. prealloc = alloc_extent_state_atomic(prealloc);
  862. BUG_ON(!prealloc);
  863. /*
  864. * Avoid to free 'prealloc' if it can be merged with
  865. * the later extent.
  866. */
  867. err = insert_state(tree, prealloc, start, this_end,
  868. NULL, NULL, &bits);
  869. if (err)
  870. extent_io_tree_panic(tree, err);
  871. cache_state(prealloc, cached_state);
  872. prealloc = NULL;
  873. start = this_end + 1;
  874. goto search_again;
  875. }
  876. /*
  877. * | ---- desired range ---- |
  878. * | state |
  879. * We need to split the extent, and set the bit
  880. * on the first half
  881. */
  882. if (state->start <= end && state->end > end) {
  883. if (state->state & exclusive_bits) {
  884. *failed_start = start;
  885. err = -EEXIST;
  886. goto out;
  887. }
  888. prealloc = alloc_extent_state_atomic(prealloc);
  889. BUG_ON(!prealloc);
  890. err = split_state(tree, state, prealloc, end + 1);
  891. if (err)
  892. extent_io_tree_panic(tree, err);
  893. set_state_bits(tree, prealloc, &bits);
  894. cache_state(prealloc, cached_state);
  895. merge_state(tree, prealloc);
  896. prealloc = NULL;
  897. goto out;
  898. }
  899. goto search_again;
  900. out:
  901. spin_unlock(&tree->lock);
  902. if (prealloc)
  903. free_extent_state(prealloc);
  904. return err;
  905. search_again:
  906. if (start > end)
  907. goto out;
  908. spin_unlock(&tree->lock);
  909. if (mask & __GFP_WAIT)
  910. cond_resched();
  911. goto again;
  912. }
  913. int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  914. unsigned bits, u64 * failed_start,
  915. struct extent_state **cached_state, gfp_t mask)
  916. {
  917. return __set_extent_bit(tree, start, end, bits, 0, failed_start,
  918. cached_state, mask);
  919. }
  920. /**
  921. * convert_extent_bit - convert all bits in a given range from one bit to
  922. * another
  923. * @tree: the io tree to search
  924. * @start: the start offset in bytes
  925. * @end: the end offset in bytes (inclusive)
  926. * @bits: the bits to set in this range
  927. * @clear_bits: the bits to clear in this range
  928. * @cached_state: state that we're going to cache
  929. * @mask: the allocation mask
  930. *
  931. * This will go through and set bits for the given range. If any states exist
  932. * already in this range they are set with the given bit and cleared of the
  933. * clear_bits. This is only meant to be used by things that are mergeable, ie
  934. * converting from say DELALLOC to DIRTY. This is not meant to be used with
  935. * boundary bits like LOCK.
  936. */
  937. int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  938. unsigned bits, unsigned clear_bits,
  939. struct extent_state **cached_state, gfp_t mask)
  940. {
  941. struct extent_state *state;
  942. struct extent_state *prealloc = NULL;
  943. struct rb_node *node;
  944. struct rb_node **p;
  945. struct rb_node *parent;
  946. int err = 0;
  947. u64 last_start;
  948. u64 last_end;
  949. bool first_iteration = true;
  950. btrfs_debug_check_extent_io_range(tree, start, end);
  951. again:
  952. if (!prealloc && (mask & __GFP_WAIT)) {
  953. /*
  954. * Best effort, don't worry if extent state allocation fails
  955. * here for the first iteration. We might have a cached state
  956. * that matches exactly the target range, in which case no
  957. * extent state allocations are needed. We'll only know this
  958. * after locking the tree.
  959. */
  960. prealloc = alloc_extent_state(mask);
  961. if (!prealloc && !first_iteration)
  962. return -ENOMEM;
  963. }
  964. spin_lock(&tree->lock);
  965. if (cached_state && *cached_state) {
  966. state = *cached_state;
  967. if (state->start <= start && state->end > start &&
  968. extent_state_in_tree(state)) {
  969. node = &state->rb_node;
  970. goto hit_next;
  971. }
  972. }
  973. /*
  974. * this search will find all the extents that end after
  975. * our range starts.
  976. */
  977. node = tree_search_for_insert(tree, start, &p, &parent);
  978. if (!node) {
  979. prealloc = alloc_extent_state_atomic(prealloc);
  980. if (!prealloc) {
  981. err = -ENOMEM;
  982. goto out;
  983. }
  984. err = insert_state(tree, prealloc, start, end,
  985. &p, &parent, &bits);
  986. if (err)
  987. extent_io_tree_panic(tree, err);
  988. cache_state(prealloc, cached_state);
  989. prealloc = NULL;
  990. goto out;
  991. }
  992. state = rb_entry(node, struct extent_state, rb_node);
  993. hit_next:
  994. last_start = state->start;
  995. last_end = state->end;
  996. /*
  997. * | ---- desired range ---- |
  998. * | state |
  999. *
  1000. * Just lock what we found and keep going
  1001. */
  1002. if (state->start == start && state->end <= end) {
  1003. set_state_bits(tree, state, &bits);
  1004. cache_state(state, cached_state);
  1005. state = clear_state_bit(tree, state, &clear_bits, 0);
  1006. if (last_end == (u64)-1)
  1007. goto out;
  1008. start = last_end + 1;
  1009. if (start < end && state && state->start == start &&
  1010. !need_resched())
  1011. goto hit_next;
  1012. goto search_again;
  1013. }
  1014. /*
  1015. * | ---- desired range ---- |
  1016. * | state |
  1017. * or
  1018. * | ------------- state -------------- |
  1019. *
  1020. * We need to split the extent we found, and may flip bits on
  1021. * second half.
  1022. *
  1023. * If the extent we found extends past our
  1024. * range, we just split and search again. It'll get split
  1025. * again the next time though.
  1026. *
  1027. * If the extent we found is inside our range, we set the
  1028. * desired bit on it.
  1029. */
  1030. if (state->start < start) {
  1031. prealloc = alloc_extent_state_atomic(prealloc);
  1032. if (!prealloc) {
  1033. err = -ENOMEM;
  1034. goto out;
  1035. }
  1036. err = split_state(tree, state, prealloc, start);
  1037. if (err)
  1038. extent_io_tree_panic(tree, err);
  1039. prealloc = NULL;
  1040. if (err)
  1041. goto out;
  1042. if (state->end <= end) {
  1043. set_state_bits(tree, state, &bits);
  1044. cache_state(state, cached_state);
  1045. state = clear_state_bit(tree, state, &clear_bits, 0);
  1046. if (last_end == (u64)-1)
  1047. goto out;
  1048. start = last_end + 1;
  1049. if (start < end && state && state->start == start &&
  1050. !need_resched())
  1051. goto hit_next;
  1052. }
  1053. goto search_again;
  1054. }
  1055. /*
  1056. * | ---- desired range ---- |
  1057. * | state | or | state |
  1058. *
  1059. * There's a hole, we need to insert something in it and
  1060. * ignore the extent we found.
  1061. */
  1062. if (state->start > start) {
  1063. u64 this_end;
  1064. if (end < last_start)
  1065. this_end = end;
  1066. else
  1067. this_end = last_start - 1;
  1068. prealloc = alloc_extent_state_atomic(prealloc);
  1069. if (!prealloc) {
  1070. err = -ENOMEM;
  1071. goto out;
  1072. }
  1073. /*
  1074. * Avoid to free 'prealloc' if it can be merged with
  1075. * the later extent.
  1076. */
  1077. err = insert_state(tree, prealloc, start, this_end,
  1078. NULL, NULL, &bits);
  1079. if (err)
  1080. extent_io_tree_panic(tree, err);
  1081. cache_state(prealloc, cached_state);
  1082. prealloc = NULL;
  1083. start = this_end + 1;
  1084. goto search_again;
  1085. }
  1086. /*
  1087. * | ---- desired range ---- |
  1088. * | state |
  1089. * We need to split the extent, and set the bit
  1090. * on the first half
  1091. */
  1092. if (state->start <= end && state->end > end) {
  1093. prealloc = alloc_extent_state_atomic(prealloc);
  1094. if (!prealloc) {
  1095. err = -ENOMEM;
  1096. goto out;
  1097. }
  1098. err = split_state(tree, state, prealloc, end + 1);
  1099. if (err)
  1100. extent_io_tree_panic(tree, err);
  1101. set_state_bits(tree, prealloc, &bits);
  1102. cache_state(prealloc, cached_state);
  1103. clear_state_bit(tree, prealloc, &clear_bits, 0);
  1104. prealloc = NULL;
  1105. goto out;
  1106. }
  1107. goto search_again;
  1108. out:
  1109. spin_unlock(&tree->lock);
  1110. if (prealloc)
  1111. free_extent_state(prealloc);
  1112. return err;
  1113. search_again:
  1114. if (start > end)
  1115. goto out;
  1116. spin_unlock(&tree->lock);
  1117. if (mask & __GFP_WAIT)
  1118. cond_resched();
  1119. first_iteration = false;
  1120. goto again;
  1121. }
  1122. /* wrappers around set/clear extent bit */
  1123. int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
  1124. gfp_t mask)
  1125. {
  1126. return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL,
  1127. NULL, mask);
  1128. }
  1129. int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
  1130. unsigned bits, gfp_t mask)
  1131. {
  1132. return set_extent_bit(tree, start, end, bits, NULL,
  1133. NULL, mask);
  1134. }
  1135. int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
  1136. unsigned bits, gfp_t mask)
  1137. {
  1138. return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
  1139. }
  1140. int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
  1141. struct extent_state **cached_state, gfp_t mask)
  1142. {
  1143. return set_extent_bit(tree, start, end,
  1144. EXTENT_DELALLOC | EXTENT_UPTODATE,
  1145. NULL, cached_state, mask);
  1146. }
  1147. int set_extent_defrag(struct extent_io_tree *tree, u64 start, u64 end,
  1148. struct extent_state **cached_state, gfp_t mask)
  1149. {
  1150. return set_extent_bit(tree, start, end,
  1151. EXTENT_DELALLOC | EXTENT_UPTODATE | EXTENT_DEFRAG,
  1152. NULL, cached_state, mask);
  1153. }
  1154. int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
  1155. gfp_t mask)
  1156. {
  1157. return clear_extent_bit(tree, start, end,
  1158. EXTENT_DIRTY | EXTENT_DELALLOC |
  1159. EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
  1160. }
  1161. int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
  1162. gfp_t mask)
  1163. {
  1164. return set_extent_bit(tree, start, end, EXTENT_NEW, NULL,
  1165. NULL, mask);
  1166. }
  1167. int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
  1168. struct extent_state **cached_state, gfp_t mask)
  1169. {
  1170. return set_extent_bit(tree, start, end, EXTENT_UPTODATE, NULL,
  1171. cached_state, mask);
  1172. }
  1173. int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
  1174. struct extent_state **cached_state, gfp_t mask)
  1175. {
  1176. return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
  1177. cached_state, mask);
  1178. }
  1179. /*
  1180. * either insert or lock state struct between start and end use mask to tell
  1181. * us if waiting is desired.
  1182. */
  1183. int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
  1184. unsigned bits, struct extent_state **cached_state)
  1185. {
  1186. int err;
  1187. u64 failed_start;
  1188. while (1) {
  1189. err = __set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
  1190. EXTENT_LOCKED, &failed_start,
  1191. cached_state, GFP_NOFS);
  1192. if (err == -EEXIST) {
  1193. wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
  1194. start = failed_start;
  1195. } else
  1196. break;
  1197. WARN_ON(start > end);
  1198. }
  1199. return err;
  1200. }
  1201. int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
  1202. {
  1203. return lock_extent_bits(tree, start, end, 0, NULL);
  1204. }
  1205. int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
  1206. {
  1207. int err;
  1208. u64 failed_start;
  1209. err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
  1210. &failed_start, NULL, GFP_NOFS);
  1211. if (err == -EEXIST) {
  1212. if (failed_start > start)
  1213. clear_extent_bit(tree, start, failed_start - 1,
  1214. EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
  1215. return 0;
  1216. }
  1217. return 1;
  1218. }
  1219. int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
  1220. struct extent_state **cached, gfp_t mask)
  1221. {
  1222. return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
  1223. mask);
  1224. }
  1225. int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
  1226. {
  1227. return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
  1228. GFP_NOFS);
  1229. }
  1230. int extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
  1231. {
  1232. unsigned long index = start >> PAGE_CACHE_SHIFT;
  1233. unsigned long end_index = end >> PAGE_CACHE_SHIFT;
  1234. struct page *page;
  1235. while (index <= end_index) {
  1236. page = find_get_page(inode->i_mapping, index);
  1237. BUG_ON(!page); /* Pages should be in the extent_io_tree */
  1238. clear_page_dirty_for_io(page);
  1239. page_cache_release(page);
  1240. index++;
  1241. }
  1242. return 0;
  1243. }
  1244. int extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
  1245. {
  1246. unsigned long index = start >> PAGE_CACHE_SHIFT;
  1247. unsigned long end_index = end >> PAGE_CACHE_SHIFT;
  1248. struct page *page;
  1249. while (index <= end_index) {
  1250. page = find_get_page(inode->i_mapping, index);
  1251. BUG_ON(!page); /* Pages should be in the extent_io_tree */
  1252. __set_page_dirty_nobuffers(page);
  1253. account_page_redirty(page);
  1254. page_cache_release(page);
  1255. index++;
  1256. }
  1257. return 0;
  1258. }
  1259. /*
  1260. * helper function to set both pages and extents in the tree writeback
  1261. */
  1262. static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
  1263. {
  1264. unsigned long index = start >> PAGE_CACHE_SHIFT;
  1265. unsigned long end_index = end >> PAGE_CACHE_SHIFT;
  1266. struct page *page;
  1267. while (index <= end_index) {
  1268. page = find_get_page(tree->mapping, index);
  1269. BUG_ON(!page); /* Pages should be in the extent_io_tree */
  1270. set_page_writeback(page);
  1271. page_cache_release(page);
  1272. index++;
  1273. }
  1274. return 0;
  1275. }
  1276. /* find the first state struct with 'bits' set after 'start', and
  1277. * return it. tree->lock must be held. NULL will returned if
  1278. * nothing was found after 'start'
  1279. */
  1280. static struct extent_state *
  1281. find_first_extent_bit_state(struct extent_io_tree *tree,
  1282. u64 start, unsigned bits)
  1283. {
  1284. struct rb_node *node;
  1285. struct extent_state *state;
  1286. /*
  1287. * this search will find all the extents that end after
  1288. * our range starts.
  1289. */
  1290. node = tree_search(tree, start);
  1291. if (!node)
  1292. goto out;
  1293. while (1) {
  1294. state = rb_entry(node, struct extent_state, rb_node);
  1295. if (state->end >= start && (state->state & bits))
  1296. return state;
  1297. node = rb_next(node);
  1298. if (!node)
  1299. break;
  1300. }
  1301. out:
  1302. return NULL;
  1303. }
  1304. /*
  1305. * find the first offset in the io tree with 'bits' set. zero is
  1306. * returned if we find something, and *start_ret and *end_ret are
  1307. * set to reflect the state struct that was found.
  1308. *
  1309. * If nothing was found, 1 is returned. If found something, return 0.
  1310. */
  1311. int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
  1312. u64 *start_ret, u64 *end_ret, unsigned bits,
  1313. struct extent_state **cached_state)
  1314. {
  1315. struct extent_state *state;
  1316. struct rb_node *n;
  1317. int ret = 1;
  1318. spin_lock(&tree->lock);
  1319. if (cached_state && *cached_state) {
  1320. state = *cached_state;
  1321. if (state->end == start - 1 && extent_state_in_tree(state)) {
  1322. n = rb_next(&state->rb_node);
  1323. while (n) {
  1324. state = rb_entry(n, struct extent_state,
  1325. rb_node);
  1326. if (state->state & bits)
  1327. goto got_it;
  1328. n = rb_next(n);
  1329. }
  1330. free_extent_state(*cached_state);
  1331. *cached_state = NULL;
  1332. goto out;
  1333. }
  1334. free_extent_state(*cached_state);
  1335. *cached_state = NULL;
  1336. }
  1337. state = find_first_extent_bit_state(tree, start, bits);
  1338. got_it:
  1339. if (state) {
  1340. cache_state_if_flags(state, cached_state, 0);
  1341. *start_ret = state->start;
  1342. *end_ret = state->end;
  1343. ret = 0;
  1344. }
  1345. out:
  1346. spin_unlock(&tree->lock);
  1347. return ret;
  1348. }
  1349. /*
  1350. * find a contiguous range of bytes in the file marked as delalloc, not
  1351. * more than 'max_bytes'. start and end are used to return the range,
  1352. *
  1353. * 1 is returned if we find something, 0 if nothing was in the tree
  1354. */
  1355. static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
  1356. u64 *start, u64 *end, u64 max_bytes,
  1357. struct extent_state **cached_state)
  1358. {
  1359. struct rb_node *node;
  1360. struct extent_state *state;
  1361. u64 cur_start = *start;
  1362. u64 found = 0;
  1363. u64 total_bytes = 0;
  1364. spin_lock(&tree->lock);
  1365. /*
  1366. * this search will find all the extents that end after
  1367. * our range starts.
  1368. */
  1369. node = tree_search(tree, cur_start);
  1370. if (!node) {
  1371. if (!found)
  1372. *end = (u64)-1;
  1373. goto out;
  1374. }
  1375. while (1) {
  1376. state = rb_entry(node, struct extent_state, rb_node);
  1377. if (found && (state->start != cur_start ||
  1378. (state->state & EXTENT_BOUNDARY))) {
  1379. goto out;
  1380. }
  1381. if (!(state->state & EXTENT_DELALLOC)) {
  1382. if (!found)
  1383. *end = state->end;
  1384. goto out;
  1385. }
  1386. if (!found) {
  1387. *start = state->start;
  1388. *cached_state = state;
  1389. atomic_inc(&state->refs);
  1390. }
  1391. found++;
  1392. *end = state->end;
  1393. cur_start = state->end + 1;
  1394. node = rb_next(node);
  1395. total_bytes += state->end - state->start + 1;
  1396. if (total_bytes >= max_bytes)
  1397. break;
  1398. if (!node)
  1399. break;
  1400. }
  1401. out:
  1402. spin_unlock(&tree->lock);
  1403. return found;
  1404. }
  1405. static noinline void __unlock_for_delalloc(struct inode *inode,
  1406. struct page *locked_page,
  1407. u64 start, u64 end)
  1408. {
  1409. int ret;
  1410. struct page *pages[16];
  1411. unsigned long index = start >> PAGE_CACHE_SHIFT;
  1412. unsigned long end_index = end >> PAGE_CACHE_SHIFT;
  1413. unsigned long nr_pages = end_index - index + 1;
  1414. int i;
  1415. if (index == locked_page->index && end_index == index)
  1416. return;
  1417. while (nr_pages > 0) {
  1418. ret = find_get_pages_contig(inode->i_mapping, index,
  1419. min_t(unsigned long, nr_pages,
  1420. ARRAY_SIZE(pages)), pages);
  1421. for (i = 0; i < ret; i++) {
  1422. if (pages[i] != locked_page)
  1423. unlock_page(pages[i]);
  1424. page_cache_release(pages[i]);
  1425. }
  1426. nr_pages -= ret;
  1427. index += ret;
  1428. cond_resched();
  1429. }
  1430. }
  1431. static noinline int lock_delalloc_pages(struct inode *inode,
  1432. struct page *locked_page,
  1433. u64 delalloc_start,
  1434. u64 delalloc_end)
  1435. {
  1436. unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
  1437. unsigned long start_index = index;
  1438. unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
  1439. unsigned long pages_locked = 0;
  1440. struct page *pages[16];
  1441. unsigned long nrpages;
  1442. int ret;
  1443. int i;
  1444. /* the caller is responsible for locking the start index */
  1445. if (index == locked_page->index && index == end_index)
  1446. return 0;
  1447. /* skip the page at the start index */
  1448. nrpages = end_index - index + 1;
  1449. while (nrpages > 0) {
  1450. ret = find_get_pages_contig(inode->i_mapping, index,
  1451. min_t(unsigned long,
  1452. nrpages, ARRAY_SIZE(pages)), pages);
  1453. if (ret == 0) {
  1454. ret = -EAGAIN;
  1455. goto done;
  1456. }
  1457. /* now we have an array of pages, lock them all */
  1458. for (i = 0; i < ret; i++) {
  1459. /*
  1460. * the caller is taking responsibility for
  1461. * locked_page
  1462. */
  1463. if (pages[i] != locked_page) {
  1464. lock_page(pages[i]);
  1465. if (!PageDirty(pages[i]) ||
  1466. pages[i]->mapping != inode->i_mapping) {
  1467. ret = -EAGAIN;
  1468. unlock_page(pages[i]);
  1469. page_cache_release(pages[i]);
  1470. goto done;
  1471. }
  1472. }
  1473. page_cache_release(pages[i]);
  1474. pages_locked++;
  1475. }
  1476. nrpages -= ret;
  1477. index += ret;
  1478. cond_resched();
  1479. }
  1480. ret = 0;
  1481. done:
  1482. if (ret && pages_locked) {
  1483. __unlock_for_delalloc(inode, locked_page,
  1484. delalloc_start,
  1485. ((u64)(start_index + pages_locked - 1)) <<
  1486. PAGE_CACHE_SHIFT);
  1487. }
  1488. return ret;
  1489. }
  1490. /*
  1491. * find a contiguous range of bytes in the file marked as delalloc, not
  1492. * more than 'max_bytes'. start and end are used to return the range,
  1493. *
  1494. * 1 is returned if we find something, 0 if nothing was in the tree
  1495. */
  1496. STATIC u64 find_lock_delalloc_range(struct inode *inode,
  1497. struct extent_io_tree *tree,
  1498. struct page *locked_page, u64 *start,
  1499. u64 *end, u64 max_bytes)
  1500. {
  1501. u64 delalloc_start;
  1502. u64 delalloc_end;
  1503. u64 found;
  1504. struct extent_state *cached_state = NULL;
  1505. int ret;
  1506. int loops = 0;
  1507. again:
  1508. /* step one, find a bunch of delalloc bytes starting at start */
  1509. delalloc_start = *start;
  1510. delalloc_end = 0;
  1511. found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
  1512. max_bytes, &cached_state);
  1513. if (!found || delalloc_end <= *start) {
  1514. *start = delalloc_start;
  1515. *end = delalloc_end;
  1516. free_extent_state(cached_state);
  1517. return 0;
  1518. }
  1519. /*
  1520. * start comes from the offset of locked_page. We have to lock
  1521. * pages in order, so we can't process delalloc bytes before
  1522. * locked_page
  1523. */
  1524. if (delalloc_start < *start)
  1525. delalloc_start = *start;
  1526. /*
  1527. * make sure to limit the number of pages we try to lock down
  1528. */
  1529. if (delalloc_end + 1 - delalloc_start > max_bytes)
  1530. delalloc_end = delalloc_start + max_bytes - 1;
  1531. /* step two, lock all the pages after the page that has start */
  1532. ret = lock_delalloc_pages(inode, locked_page,
  1533. delalloc_start, delalloc_end);
  1534. if (ret == -EAGAIN) {
  1535. /* some of the pages are gone, lets avoid looping by
  1536. * shortening the size of the delalloc range we're searching
  1537. */
  1538. free_extent_state(cached_state);
  1539. cached_state = NULL;
  1540. if (!loops) {
  1541. max_bytes = PAGE_CACHE_SIZE;
  1542. loops = 1;
  1543. goto again;
  1544. } else {
  1545. found = 0;
  1546. goto out_failed;
  1547. }
  1548. }
  1549. BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
  1550. /* step three, lock the state bits for the whole range */
  1551. lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state);
  1552. /* then test to make sure it is all still delalloc */
  1553. ret = test_range_bit(tree, delalloc_start, delalloc_end,
  1554. EXTENT_DELALLOC, 1, cached_state);
  1555. if (!ret) {
  1556. unlock_extent_cached(tree, delalloc_start, delalloc_end,
  1557. &cached_state, GFP_NOFS);
  1558. __unlock_for_delalloc(inode, locked_page,
  1559. delalloc_start, delalloc_end);
  1560. cond_resched();
  1561. goto again;
  1562. }
  1563. free_extent_state(cached_state);
  1564. *start = delalloc_start;
  1565. *end = delalloc_end;
  1566. out_failed:
  1567. return found;
  1568. }
  1569. int extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,
  1570. struct page *locked_page,
  1571. unsigned clear_bits,
  1572. unsigned long page_ops)
  1573. {
  1574. struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
  1575. int ret;
  1576. struct page *pages[16];
  1577. unsigned long index = start >> PAGE_CACHE_SHIFT;
  1578. unsigned long end_index = end >> PAGE_CACHE_SHIFT;
  1579. unsigned long nr_pages = end_index - index + 1;
  1580. int i;
  1581. clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
  1582. if (page_ops == 0)
  1583. return 0;
  1584. if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0)
  1585. mapping_set_error(inode->i_mapping, -EIO);
  1586. while (nr_pages > 0) {
  1587. ret = find_get_pages_contig(inode->i_mapping, index,
  1588. min_t(unsigned long,
  1589. nr_pages, ARRAY_SIZE(pages)), pages);
  1590. for (i = 0; i < ret; i++) {
  1591. if (page_ops & PAGE_SET_PRIVATE2)
  1592. SetPagePrivate2(pages[i]);
  1593. if (pages[i] == locked_page) {
  1594. page_cache_release(pages[i]);
  1595. continue;
  1596. }
  1597. if (page_ops & PAGE_CLEAR_DIRTY)
  1598. clear_page_dirty_for_io(pages[i]);
  1599. if (page_ops & PAGE_SET_WRITEBACK)
  1600. set_page_writeback(pages[i]);
  1601. if (page_ops & PAGE_SET_ERROR)
  1602. SetPageError(pages[i]);
  1603. if (page_ops & PAGE_END_WRITEBACK)
  1604. end_page_writeback(pages[i]);
  1605. if (page_ops & PAGE_UNLOCK)
  1606. unlock_page(pages[i]);
  1607. page_cache_release(pages[i]);
  1608. }
  1609. nr_pages -= ret;
  1610. index += ret;
  1611. cond_resched();
  1612. }
  1613. return 0;
  1614. }
  1615. /*
  1616. * count the number of bytes in the tree that have a given bit(s)
  1617. * set. This can be fairly slow, except for EXTENT_DIRTY which is
  1618. * cached. The total number found is returned.
  1619. */
  1620. u64 count_range_bits(struct extent_io_tree *tree,
  1621. u64 *start, u64 search_end, u64 max_bytes,
  1622. unsigned bits, int contig)
  1623. {
  1624. struct rb_node *node;
  1625. struct extent_state *state;
  1626. u64 cur_start = *start;
  1627. u64 total_bytes = 0;
  1628. u64 last = 0;
  1629. int found = 0;
  1630. if (WARN_ON(search_end <= cur_start))
  1631. return 0;
  1632. spin_lock(&tree->lock);
  1633. if (cur_start == 0 && bits == EXTENT_DIRTY) {
  1634. total_bytes = tree->dirty_bytes;
  1635. goto out;
  1636. }
  1637. /*
  1638. * this search will find all the extents that end after
  1639. * our range starts.
  1640. */
  1641. node = tree_search(tree, cur_start);
  1642. if (!node)
  1643. goto out;
  1644. while (1) {
  1645. state = rb_entry(node, struct extent_state, rb_node);
  1646. if (state->start > search_end)
  1647. break;
  1648. if (contig && found && state->start > last + 1)
  1649. break;
  1650. if (state->end >= cur_start && (state->state & bits) == bits) {
  1651. total_bytes += min(search_end, state->end) + 1 -
  1652. max(cur_start, state->start);
  1653. if (total_bytes >= max_bytes)
  1654. break;
  1655. if (!found) {
  1656. *start = max(cur_start, state->start);
  1657. found = 1;
  1658. }
  1659. last = state->end;
  1660. } else if (contig && found) {
  1661. break;
  1662. }
  1663. node = rb_next(node);
  1664. if (!node)
  1665. break;
  1666. }
  1667. out:
  1668. spin_unlock(&tree->lock);
  1669. return total_bytes;
  1670. }
  1671. /*
  1672. * set the private field for a given byte offset in the tree. If there isn't
  1673. * an extent_state there already, this does nothing.
  1674. */
  1675. static int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
  1676. {
  1677. struct rb_node *node;
  1678. struct extent_state *state;
  1679. int ret = 0;
  1680. spin_lock(&tree->lock);
  1681. /*
  1682. * this search will find all the extents that end after
  1683. * our range starts.
  1684. */
  1685. node = tree_search(tree, start);
  1686. if (!node) {
  1687. ret = -ENOENT;
  1688. goto out;
  1689. }
  1690. state = rb_entry(node, struct extent_state, rb_node);
  1691. if (state->start != start) {
  1692. ret = -ENOENT;
  1693. goto out;
  1694. }
  1695. state->private = private;
  1696. out:
  1697. spin_unlock(&tree->lock);
  1698. return ret;
  1699. }
  1700. int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
  1701. {
  1702. struct rb_node *node;
  1703. struct extent_state *state;
  1704. int ret = 0;
  1705. spin_lock(&tree->lock);
  1706. /*
  1707. * this search will find all the extents that end after
  1708. * our range starts.
  1709. */
  1710. node = tree_search(tree, start);
  1711. if (!node) {
  1712. ret = -ENOENT;
  1713. goto out;
  1714. }
  1715. state = rb_entry(node, struct extent_state, rb_node);
  1716. if (state->start != start) {
  1717. ret = -ENOENT;
  1718. goto out;
  1719. }
  1720. *private = state->private;
  1721. out:
  1722. spin_unlock(&tree->lock);
  1723. return ret;
  1724. }
  1725. /*
  1726. * searches a range in the state tree for a given mask.
  1727. * If 'filled' == 1, this returns 1 only if every extent in the tree
  1728. * has the bits set. Otherwise, 1 is returned if any bit in the
  1729. * range is found set.
  1730. */
  1731. int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
  1732. unsigned bits, int filled, struct extent_state *cached)
  1733. {
  1734. struct extent_state *state = NULL;
  1735. struct rb_node *node;
  1736. int bitset = 0;
  1737. spin_lock(&tree->lock);
  1738. if (cached && extent_state_in_tree(cached) && cached->start <= start &&
  1739. cached->end > start)
  1740. node = &cached->rb_node;
  1741. else
  1742. node = tree_search(tree, start);
  1743. while (node && start <= end) {
  1744. state = rb_entry(node, struct extent_state, rb_node);
  1745. if (filled && state->start > start) {
  1746. bitset = 0;
  1747. break;
  1748. }
  1749. if (state->start > end)
  1750. break;
  1751. if (state->state & bits) {
  1752. bitset = 1;
  1753. if (!filled)
  1754. break;
  1755. } else if (filled) {
  1756. bitset = 0;
  1757. break;
  1758. }
  1759. if (state->end == (u64)-1)
  1760. break;
  1761. start = state->end + 1;
  1762. if (start > end)
  1763. break;
  1764. node = rb_next(node);
  1765. if (!node) {
  1766. if (filled)
  1767. bitset = 0;
  1768. break;
  1769. }
  1770. }
  1771. spin_unlock(&tree->lock);
  1772. return bitset;
  1773. }
  1774. /*
  1775. * helper function to set a given page up to date if all the
  1776. * extents in the tree for that page are up to date
  1777. */
  1778. static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
  1779. {
  1780. u64 start = page_offset(page);
  1781. u64 end = start + PAGE_CACHE_SIZE - 1;
  1782. if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
  1783. SetPageUptodate(page);
  1784. }
  1785. int free_io_failure(struct inode *inode, struct io_failure_record *rec)
  1786. {
  1787. int ret;
  1788. int err = 0;
  1789. struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
  1790. set_state_private(failure_tree, rec->start, 0);
  1791. ret = clear_extent_bits(failure_tree, rec->start,
  1792. rec->start + rec->len - 1,
  1793. EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
  1794. if (ret)
  1795. err = ret;
  1796. ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
  1797. rec->start + rec->len - 1,
  1798. EXTENT_DAMAGED, GFP_NOFS);
  1799. if (ret && !err)
  1800. err = ret;
  1801. kfree(rec);
  1802. return err;
  1803. }
  1804. /*
  1805. * this bypasses the standard btrfs submit functions deliberately, as
  1806. * the standard behavior is to write all copies in a raid setup. here we only
  1807. * want to write the one bad copy. so we do the mapping for ourselves and issue
  1808. * submit_bio directly.
  1809. * to avoid any synchronization issues, wait for the data after writing, which
  1810. * actually prevents the read that triggered the error from finishing.
  1811. * currently, there can be no more than two copies of every data bit. thus,
  1812. * exactly one rewrite is required.
  1813. */
  1814. int repair_io_failure(struct inode *inode, u64 start, u64 length, u64 logical,
  1815. struct page *page, unsigned int pg_offset, int mirror_num)
  1816. {
  1817. struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
  1818. struct bio *bio;
  1819. struct btrfs_device *dev;
  1820. u64 map_length = 0;
  1821. u64 sector;
  1822. struct btrfs_bio *bbio = NULL;
  1823. struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
  1824. int ret;
  1825. ASSERT(!(fs_info->sb->s_flags & MS_RDONLY));
  1826. BUG_ON(!mirror_num);
  1827. /* we can't repair anything in raid56 yet */
  1828. if (btrfs_is_parity_mirror(map_tree, logical, length, mirror_num))
  1829. return 0;
  1830. bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
  1831. if (!bio)
  1832. return -EIO;
  1833. bio->bi_iter.bi_size = 0;
  1834. map_length = length;
  1835. ret = btrfs_map_block(fs_info, WRITE, logical,
  1836. &map_length, &bbio, mirror_num);
  1837. if (ret) {
  1838. bio_put(bio);
  1839. return -EIO;
  1840. }
  1841. BUG_ON(mirror_num != bbio->mirror_num);
  1842. sector = bbio->stripes[mirror_num-1].physical >> 9;
  1843. bio->bi_iter.bi_sector = sector;
  1844. dev = bbio->stripes[mirror_num-1].dev;
  1845. btrfs_put_bbio(bbio);
  1846. if (!dev || !dev->bdev || !dev->writeable) {
  1847. bio_put(bio);
  1848. return -EIO;
  1849. }
  1850. bio->bi_bdev = dev->bdev;
  1851. bio_add_page(bio, page, length, pg_offset);
  1852. if (btrfsic_submit_bio_wait(WRITE_SYNC, bio)) {
  1853. /* try to remap that extent elsewhere? */
  1854. bio_put(bio);
  1855. btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
  1856. return -EIO;
  1857. }
  1858. printk_ratelimited_in_rcu(KERN_INFO
  1859. "BTRFS: read error corrected: ino %llu off %llu (dev %s sector %llu)\n",
  1860. btrfs_ino(inode), start,
  1861. rcu_str_deref(dev->name), sector);
  1862. bio_put(bio);
  1863. return 0;
  1864. }
  1865. int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
  1866. int mirror_num)
  1867. {
  1868. u64 start = eb->start;
  1869. unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
  1870. int ret = 0;
  1871. if (root->fs_info->sb->s_flags & MS_RDONLY)
  1872. return -EROFS;
  1873. for (i = 0; i < num_pages; i++) {
  1874. struct page *p = eb->pages[i];
  1875. ret = repair_io_failure(root->fs_info->btree_inode, start,
  1876. PAGE_CACHE_SIZE, start, p,
  1877. start - page_offset(p), mirror_num);
  1878. if (ret)
  1879. break;
  1880. start += PAGE_CACHE_SIZE;
  1881. }
  1882. return ret;
  1883. }
  1884. /*
  1885. * each time an IO finishes, we do a fast check in the IO failure tree
  1886. * to see if we need to process or clean up an io_failure_record
  1887. */
  1888. int clean_io_failure(struct inode *inode, u64 start, struct page *page,
  1889. unsigned int pg_offset)
  1890. {
  1891. u64 private;
  1892. u64 private_failure;
  1893. struct io_failure_record *failrec;
  1894. struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
  1895. struct extent_state *state;
  1896. int num_copies;
  1897. int ret;
  1898. private = 0;
  1899. ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
  1900. (u64)-1, 1, EXTENT_DIRTY, 0);
  1901. if (!ret)
  1902. return 0;
  1903. ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
  1904. &private_failure);
  1905. if (ret)
  1906. return 0;
  1907. failrec = (struct io_failure_record *)(unsigned long) private_failure;
  1908. BUG_ON(!failrec->this_mirror);
  1909. if (failrec->in_validation) {
  1910. /* there was no real error, just free the record */
  1911. pr_debug("clean_io_failure: freeing dummy error at %llu\n",
  1912. failrec->start);
  1913. goto out;
  1914. }
  1915. if (fs_info->sb->s_flags & MS_RDONLY)
  1916. goto out;
  1917. spin_lock(&BTRFS_I(inode)->io_tree.lock);
  1918. state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
  1919. failrec->start,
  1920. EXTENT_LOCKED);
  1921. spin_unlock(&BTRFS_I(inode)->io_tree.lock);
  1922. if (state && state->start <= failrec->start &&
  1923. state->end >= failrec->start + failrec->len - 1) {
  1924. num_copies = btrfs_num_copies(fs_info, failrec->logical,
  1925. failrec->len);
  1926. if (num_copies > 1) {
  1927. repair_io_failure(inode, start, failrec->len,
  1928. failrec->logical, page,
  1929. pg_offset, failrec->failed_mirror);
  1930. }
  1931. }
  1932. out:
  1933. free_io_failure(inode, failrec);
  1934. return 0;
  1935. }
  1936. /*
  1937. * Can be called when
  1938. * - hold extent lock
  1939. * - under ordered extent
  1940. * - the inode is freeing
  1941. */
  1942. void btrfs_free_io_failure_record(struct inode *inode, u64 start, u64 end)
  1943. {
  1944. struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
  1945. struct io_failure_record *failrec;
  1946. struct extent_state *state, *next;
  1947. if (RB_EMPTY_ROOT(&failure_tree->state))
  1948. return;
  1949. spin_lock(&failure_tree->lock);
  1950. state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
  1951. while (state) {
  1952. if (state->start > end)
  1953. break;
  1954. ASSERT(state->end <= end);
  1955. next = next_state(state);
  1956. failrec = (struct io_failure_record *)(unsigned long)state->private;
  1957. free_extent_state(state);
  1958. kfree(failrec);
  1959. state = next;
  1960. }
  1961. spin_unlock(&failure_tree->lock);
  1962. }
  1963. int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
  1964. struct io_failure_record **failrec_ret)
  1965. {
  1966. struct io_failure_record *failrec;
  1967. u64 private;
  1968. struct extent_map *em;
  1969. struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
  1970. struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
  1971. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  1972. int ret;
  1973. u64 logical;
  1974. ret = get_state_private(failure_tree, start, &private);
  1975. if (ret) {
  1976. failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
  1977. if (!failrec)
  1978. return -ENOMEM;
  1979. failrec->start = start;
  1980. failrec->len = end - start + 1;
  1981. failrec->this_mirror = 0;
  1982. failrec->bio_flags = 0;
  1983. failrec->in_validation = 0;
  1984. read_lock(&em_tree->lock);
  1985. em = lookup_extent_mapping(em_tree, start, failrec->len);
  1986. if (!em) {
  1987. read_unlock(&em_tree->lock);
  1988. kfree(failrec);
  1989. return -EIO;
  1990. }
  1991. if (em->start > start || em->start + em->len <= start) {
  1992. free_extent_map(em);
  1993. em = NULL;
  1994. }
  1995. read_unlock(&em_tree->lock);
  1996. if (!em) {
  1997. kfree(failrec);
  1998. return -EIO;
  1999. }
  2000. logical = start - em->start;
  2001. logical = em->block_start + logical;
  2002. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  2003. logical = em->block_start;
  2004. failrec->bio_flags = EXTENT_BIO_COMPRESSED;
  2005. extent_set_compress_type(&failrec->bio_flags,
  2006. em->compress_type);
  2007. }
  2008. pr_debug("Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu\n",
  2009. logical, start, failrec->len);
  2010. failrec->logical = logical;
  2011. free_extent_map(em);
  2012. /* set the bits in the private failure tree */
  2013. ret = set_extent_bits(failure_tree, start, end,
  2014. EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
  2015. if (ret >= 0)
  2016. ret = set_state_private(failure_tree, start,
  2017. (u64)(unsigned long)failrec);
  2018. /* set the bits in the inode's tree */
  2019. if (ret >= 0)
  2020. ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
  2021. GFP_NOFS);
  2022. if (ret < 0) {
  2023. kfree(failrec);
  2024. return ret;
  2025. }
  2026. } else {
  2027. failrec = (struct io_failure_record *)(unsigned long)private;
  2028. pr_debug("Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d\n",
  2029. failrec->logical, failrec->start, failrec->len,
  2030. failrec->in_validation);
  2031. /*
  2032. * when data can be on disk more than twice, add to failrec here
  2033. * (e.g. with a list for failed_mirror) to make
  2034. * clean_io_failure() clean all those errors at once.
  2035. */
  2036. }
  2037. *failrec_ret = failrec;
  2038. return 0;
  2039. }
  2040. int btrfs_check_repairable(struct inode *inode, struct bio *failed_bio,
  2041. struct io_failure_record *failrec, int failed_mirror)
  2042. {
  2043. int num_copies;
  2044. num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
  2045. failrec->logical, failrec->len);
  2046. if (num_copies == 1) {
  2047. /*
  2048. * we only have a single copy of the data, so don't bother with
  2049. * all the retry and error correction code that follows. no
  2050. * matter what the error is, it is very likely to persist.
  2051. */
  2052. pr_debug("Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
  2053. num_copies, failrec->this_mirror, failed_mirror);
  2054. return 0;
  2055. }
  2056. /*
  2057. * there are two premises:
  2058. * a) deliver good data to the caller
  2059. * b) correct the bad sectors on disk
  2060. */
  2061. if (failed_bio->bi_vcnt > 1) {
  2062. /*
  2063. * to fulfill b), we need to know the exact failing sectors, as
  2064. * we don't want to rewrite any more than the failed ones. thus,
  2065. * we need separate read requests for the failed bio
  2066. *
  2067. * if the following BUG_ON triggers, our validation request got
  2068. * merged. we need separate requests for our algorithm to work.
  2069. */
  2070. BUG_ON(failrec->in_validation);
  2071. failrec->in_validation = 1;
  2072. failrec->this_mirror = failed_mirror;
  2073. } else {
  2074. /*
  2075. * we're ready to fulfill a) and b) alongside. get a good copy
  2076. * of the failed sector and if we succeed, we have setup
  2077. * everything for repair_io_failure to do the rest for us.
  2078. */
  2079. if (failrec->in_validation) {
  2080. BUG_ON(failrec->this_mirror != failed_mirror);
  2081. failrec->in_validation = 0;
  2082. failrec->this_mirror = 0;
  2083. }
  2084. failrec->failed_mirror = failed_mirror;
  2085. failrec->this_mirror++;
  2086. if (failrec->this_mirror == failed_mirror)
  2087. failrec->this_mirror++;
  2088. }
  2089. if (failrec->this_mirror > num_copies) {
  2090. pr_debug("Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
  2091. num_copies, failrec->this_mirror, failed_mirror);
  2092. return 0;
  2093. }
  2094. return 1;
  2095. }
  2096. struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
  2097. struct io_failure_record *failrec,
  2098. struct page *page, int pg_offset, int icsum,
  2099. bio_end_io_t *endio_func, void *data)
  2100. {
  2101. struct bio *bio;
  2102. struct btrfs_io_bio *btrfs_failed_bio;
  2103. struct btrfs_io_bio *btrfs_bio;
  2104. bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
  2105. if (!bio)
  2106. return NULL;
  2107. bio->bi_end_io = endio_func;
  2108. bio->bi_iter.bi_sector = failrec->logical >> 9;
  2109. bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
  2110. bio->bi_iter.bi_size = 0;
  2111. bio->bi_private = data;
  2112. btrfs_failed_bio = btrfs_io_bio(failed_bio);
  2113. if (btrfs_failed_bio->csum) {
  2114. struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
  2115. u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
  2116. btrfs_bio = btrfs_io_bio(bio);
  2117. btrfs_bio->csum = btrfs_bio->csum_inline;
  2118. icsum *= csum_size;
  2119. memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum,
  2120. csum_size);
  2121. }
  2122. bio_add_page(bio, page, failrec->len, pg_offset);
  2123. return bio;
  2124. }
  2125. /*
  2126. * this is a generic handler for readpage errors (default
  2127. * readpage_io_failed_hook). if other copies exist, read those and write back
  2128. * good data to the failed position. does not investigate in remapping the
  2129. * failed extent elsewhere, hoping the device will be smart enough to do this as
  2130. * needed
  2131. */
  2132. static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
  2133. struct page *page, u64 start, u64 end,
  2134. int failed_mirror)
  2135. {
  2136. struct io_failure_record *failrec;
  2137. struct inode *inode = page->mapping->host;
  2138. struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
  2139. struct bio *bio;
  2140. int read_mode;
  2141. int ret;
  2142. BUG_ON(failed_bio->bi_rw & REQ_WRITE);
  2143. ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
  2144. if (ret)
  2145. return ret;
  2146. ret = btrfs_check_repairable(inode, failed_bio, failrec, failed_mirror);
  2147. if (!ret) {
  2148. free_io_failure(inode, failrec);
  2149. return -EIO;
  2150. }
  2151. if (failed_bio->bi_vcnt > 1)
  2152. read_mode = READ_SYNC | REQ_FAILFAST_DEV;
  2153. else
  2154. read_mode = READ_SYNC;
  2155. phy_offset >>= inode->i_sb->s_blocksize_bits;
  2156. bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
  2157. start - page_offset(page),
  2158. (int)phy_offset, failed_bio->bi_end_io,
  2159. NULL);
  2160. if (!bio) {
  2161. free_io_failure(inode, failrec);
  2162. return -EIO;
  2163. }
  2164. pr_debug("Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d\n",
  2165. read_mode, failrec->this_mirror, failrec->in_validation);
  2166. ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
  2167. failrec->this_mirror,
  2168. failrec->bio_flags, 0);
  2169. if (ret) {
  2170. free_io_failure(inode, failrec);
  2171. bio_put(bio);
  2172. }
  2173. return ret;
  2174. }
  2175. /* lots and lots of room for performance fixes in the end_bio funcs */
  2176. int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
  2177. {
  2178. int uptodate = (err == 0);
  2179. struct extent_io_tree *tree;
  2180. int ret = 0;
  2181. tree = &BTRFS_I(page->mapping->host)->io_tree;
  2182. if (tree->ops && tree->ops->writepage_end_io_hook) {
  2183. ret = tree->ops->writepage_end_io_hook(page, start,
  2184. end, NULL, uptodate);
  2185. if (ret)
  2186. uptodate = 0;
  2187. }
  2188. if (!uptodate) {
  2189. ClearPageUptodate(page);
  2190. SetPageError(page);
  2191. ret = ret < 0 ? ret : -EIO;
  2192. mapping_set_error(page->mapping, ret);
  2193. }
  2194. return 0;
  2195. }
  2196. /*
  2197. * after a writepage IO is done, we need to:
  2198. * clear the uptodate bits on error
  2199. * clear the writeback bits in the extent tree for this IO
  2200. * end_page_writeback if the page has no more pending IO
  2201. *
  2202. * Scheduling is not allowed, so the extent state tree is expected
  2203. * to have one and only one object corresponding to this IO.
  2204. */
  2205. static void end_bio_extent_writepage(struct bio *bio, int err)
  2206. {
  2207. struct bio_vec *bvec;
  2208. u64 start;
  2209. u64 end;
  2210. int i;
  2211. bio_for_each_segment_all(bvec, bio, i) {
  2212. struct page *page = bvec->bv_page;
  2213. /* We always issue full-page reads, but if some block
  2214. * in a page fails to read, blk_update_request() will
  2215. * advance bv_offset and adjust bv_len to compensate.
  2216. * Print a warning for nonzero offsets, and an error
  2217. * if they don't add up to a full page. */
  2218. if (bvec->bv_offset || bvec->bv_len != PAGE_CACHE_SIZE) {
  2219. if (bvec->bv_offset + bvec->bv_len != PAGE_CACHE_SIZE)
  2220. btrfs_err(BTRFS_I(page->mapping->host)->root->fs_info,
  2221. "partial page write in btrfs with offset %u and length %u",
  2222. bvec->bv_offset, bvec->bv_len);
  2223. else
  2224. btrfs_info(BTRFS_I(page->mapping->host)->root->fs_info,
  2225. "incomplete page write in btrfs with offset %u and "
  2226. "length %u",
  2227. bvec->bv_offset, bvec->bv_len);
  2228. }
  2229. start = page_offset(page);
  2230. end = start + bvec->bv_offset + bvec->bv_len - 1;
  2231. if (end_extent_writepage(page, err, start, end))
  2232. continue;
  2233. end_page_writeback(page);
  2234. }
  2235. bio_put(bio);
  2236. }
  2237. static void
  2238. endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len,
  2239. int uptodate)
  2240. {
  2241. struct extent_state *cached = NULL;
  2242. u64 end = start + len - 1;
  2243. if (uptodate && tree->track_uptodate)
  2244. set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC);
  2245. unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
  2246. }
  2247. /*
  2248. * after a readpage IO is done, we need to:
  2249. * clear the uptodate bits on error
  2250. * set the uptodate bits if things worked
  2251. * set the page up to date if all extents in the tree are uptodate
  2252. * clear the lock bit in the extent tree
  2253. * unlock the page if there are no other extents locked for it
  2254. *
  2255. * Scheduling is not allowed, so the extent state tree is expected
  2256. * to have one and only one object corresponding to this IO.
  2257. */
  2258. static void end_bio_extent_readpage(struct bio *bio, int err)
  2259. {
  2260. struct bio_vec *bvec;
  2261. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  2262. struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
  2263. struct extent_io_tree *tree;
  2264. u64 offset = 0;
  2265. u64 start;
  2266. u64 end;
  2267. u64 len;
  2268. u64 extent_start = 0;
  2269. u64 extent_len = 0;
  2270. int mirror;
  2271. int ret;
  2272. int i;
  2273. if (err)
  2274. uptodate = 0;
  2275. bio_for_each_segment_all(bvec, bio, i) {
  2276. struct page *page = bvec->bv_page;
  2277. struct inode *inode = page->mapping->host;
  2278. pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, "
  2279. "mirror=%u\n", (u64)bio->bi_iter.bi_sector, err,
  2280. io_bio->mirror_num);
  2281. tree = &BTRFS_I(inode)->io_tree;
  2282. /* We always issue full-page reads, but if some block
  2283. * in a page fails to read, blk_update_request() will
  2284. * advance bv_offset and adjust bv_len to compensate.
  2285. * Print a warning for nonzero offsets, and an error
  2286. * if they don't add up to a full page. */
  2287. if (bvec->bv_offset || bvec->bv_len != PAGE_CACHE_SIZE) {
  2288. if (bvec->bv_offset + bvec->bv_len != PAGE_CACHE_SIZE)
  2289. btrfs_err(BTRFS_I(page->mapping->host)->root->fs_info,
  2290. "partial page read in btrfs with offset %u and length %u",
  2291. bvec->bv_offset, bvec->bv_len);
  2292. else
  2293. btrfs_info(BTRFS_I(page->mapping->host)->root->fs_info,
  2294. "incomplete page read in btrfs with offset %u and "
  2295. "length %u",
  2296. bvec->bv_offset, bvec->bv_len);
  2297. }
  2298. start = page_offset(page);
  2299. end = start + bvec->bv_offset + bvec->bv_len - 1;
  2300. len = bvec->bv_len;
  2301. mirror = io_bio->mirror_num;
  2302. if (likely(uptodate && tree->ops &&
  2303. tree->ops->readpage_end_io_hook)) {
  2304. ret = tree->ops->readpage_end_io_hook(io_bio, offset,
  2305. page, start, end,
  2306. mirror);
  2307. if (ret)
  2308. uptodate = 0;
  2309. else
  2310. clean_io_failure(inode, start, page, 0);
  2311. }
  2312. if (likely(uptodate))
  2313. goto readpage_ok;
  2314. if (tree->ops && tree->ops->readpage_io_failed_hook) {
  2315. ret = tree->ops->readpage_io_failed_hook(page, mirror);
  2316. if (!ret && !err &&
  2317. test_bit(BIO_UPTODATE, &bio->bi_flags))
  2318. uptodate = 1;
  2319. } else {
  2320. /*
  2321. * The generic bio_readpage_error handles errors the
  2322. * following way: If possible, new read requests are
  2323. * created and submitted and will end up in
  2324. * end_bio_extent_readpage as well (if we're lucky, not
  2325. * in the !uptodate case). In that case it returns 0 and
  2326. * we just go on with the next page in our bio. If it
  2327. * can't handle the error it will return -EIO and we
  2328. * remain responsible for that page.
  2329. */
  2330. ret = bio_readpage_error(bio, offset, page, start, end,
  2331. mirror);
  2332. if (ret == 0) {
  2333. uptodate =
  2334. test_bit(BIO_UPTODATE, &bio->bi_flags);
  2335. if (err)
  2336. uptodate = 0;
  2337. offset += len;
  2338. continue;
  2339. }
  2340. }
  2341. readpage_ok:
  2342. if (likely(uptodate)) {
  2343. loff_t i_size = i_size_read(inode);
  2344. pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
  2345. unsigned off;
  2346. /* Zero out the end if this page straddles i_size */
  2347. off = i_size & (PAGE_CACHE_SIZE-1);
  2348. if (page->index == end_index && off)
  2349. zero_user_segment(page, off, PAGE_CACHE_SIZE);
  2350. SetPageUptodate(page);
  2351. } else {
  2352. ClearPageUptodate(page);
  2353. SetPageError(page);
  2354. }
  2355. unlock_page(page);
  2356. offset += len;
  2357. if (unlikely(!uptodate)) {
  2358. if (extent_len) {
  2359. endio_readpage_release_extent(tree,
  2360. extent_start,
  2361. extent_len, 1);
  2362. extent_start = 0;
  2363. extent_len = 0;
  2364. }
  2365. endio_readpage_release_extent(tree, start,
  2366. end - start + 1, 0);
  2367. } else if (!extent_len) {
  2368. extent_start = start;
  2369. extent_len = end + 1 - start;
  2370. } else if (extent_start + extent_len == start) {
  2371. extent_len += end + 1 - start;
  2372. } else {
  2373. endio_readpage_release_extent(tree, extent_start,
  2374. extent_len, uptodate);
  2375. extent_start = start;
  2376. extent_len = end + 1 - start;
  2377. }
  2378. }
  2379. if (extent_len)
  2380. endio_readpage_release_extent(tree, extent_start, extent_len,
  2381. uptodate);
  2382. if (io_bio->end_io)
  2383. io_bio->end_io(io_bio, err);
  2384. bio_put(bio);
  2385. }
  2386. /*
  2387. * this allocates from the btrfs_bioset. We're returning a bio right now
  2388. * but you can call btrfs_io_bio for the appropriate container_of magic
  2389. */
  2390. struct bio *
  2391. btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
  2392. gfp_t gfp_flags)
  2393. {
  2394. struct btrfs_io_bio *btrfs_bio;
  2395. struct bio *bio;
  2396. bio = bio_alloc_bioset(gfp_flags, nr_vecs, btrfs_bioset);
  2397. if (bio == NULL && (current->flags & PF_MEMALLOC)) {
  2398. while (!bio && (nr_vecs /= 2)) {
  2399. bio = bio_alloc_bioset(gfp_flags,
  2400. nr_vecs, btrfs_bioset);
  2401. }
  2402. }
  2403. if (bio) {
  2404. bio->bi_bdev = bdev;
  2405. bio->bi_iter.bi_sector = first_sector;
  2406. btrfs_bio = btrfs_io_bio(bio);
  2407. btrfs_bio->csum = NULL;
  2408. btrfs_bio->csum_allocated = NULL;
  2409. btrfs_bio->end_io = NULL;
  2410. }
  2411. return bio;
  2412. }
  2413. struct bio *btrfs_bio_clone(struct bio *bio, gfp_t gfp_mask)
  2414. {
  2415. struct btrfs_io_bio *btrfs_bio;
  2416. struct bio *new;
  2417. new = bio_clone_bioset(bio, gfp_mask, btrfs_bioset);
  2418. if (new) {
  2419. btrfs_bio = btrfs_io_bio(new);
  2420. btrfs_bio->csum = NULL;
  2421. btrfs_bio->csum_allocated = NULL;
  2422. btrfs_bio->end_io = NULL;
  2423. }
  2424. return new;
  2425. }
  2426. /* this also allocates from the btrfs_bioset */
  2427. struct bio *btrfs_io_bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs)
  2428. {
  2429. struct btrfs_io_bio *btrfs_bio;
  2430. struct bio *bio;
  2431. bio = bio_alloc_bioset(gfp_mask, nr_iovecs, btrfs_bioset);
  2432. if (bio) {
  2433. btrfs_bio = btrfs_io_bio(bio);
  2434. btrfs_bio->csum = NULL;
  2435. btrfs_bio->csum_allocated = NULL;
  2436. btrfs_bio->end_io = NULL;
  2437. }
  2438. return bio;
  2439. }
  2440. static int __must_check submit_one_bio(int rw, struct bio *bio,
  2441. int mirror_num, unsigned long bio_flags)
  2442. {
  2443. int ret = 0;
  2444. struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
  2445. struct page *page = bvec->bv_page;
  2446. struct extent_io_tree *tree = bio->bi_private;
  2447. u64 start;
  2448. start = page_offset(page) + bvec->bv_offset;
  2449. bio->bi_private = NULL;
  2450. bio_get(bio);
  2451. if (tree->ops && tree->ops->submit_bio_hook)
  2452. ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
  2453. mirror_num, bio_flags, start);
  2454. else
  2455. btrfsic_submit_bio(rw, bio);
  2456. if (bio_flagged(bio, BIO_EOPNOTSUPP))
  2457. ret = -EOPNOTSUPP;
  2458. bio_put(bio);
  2459. return ret;
  2460. }
  2461. static int merge_bio(int rw, struct extent_io_tree *tree, struct page *page,
  2462. unsigned long offset, size_t size, struct bio *bio,
  2463. unsigned long bio_flags)
  2464. {
  2465. int ret = 0;
  2466. if (tree->ops && tree->ops->merge_bio_hook)
  2467. ret = tree->ops->merge_bio_hook(rw, page, offset, size, bio,
  2468. bio_flags);
  2469. BUG_ON(ret < 0);
  2470. return ret;
  2471. }
  2472. static int submit_extent_page(int rw, struct extent_io_tree *tree,
  2473. struct page *page, sector_t sector,
  2474. size_t size, unsigned long offset,
  2475. struct block_device *bdev,
  2476. struct bio **bio_ret,
  2477. unsigned long max_pages,
  2478. bio_end_io_t end_io_func,
  2479. int mirror_num,
  2480. unsigned long prev_bio_flags,
  2481. unsigned long bio_flags)
  2482. {
  2483. int ret = 0;
  2484. struct bio *bio;
  2485. int nr;
  2486. int contig = 0;
  2487. int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
  2488. int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
  2489. size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
  2490. if (bio_ret && *bio_ret) {
  2491. bio = *bio_ret;
  2492. if (old_compressed)
  2493. contig = bio->bi_iter.bi_sector == sector;
  2494. else
  2495. contig = bio_end_sector(bio) == sector;
  2496. if (prev_bio_flags != bio_flags || !contig ||
  2497. merge_bio(rw, tree, page, offset, page_size, bio, bio_flags) ||
  2498. bio_add_page(bio, page, page_size, offset) < page_size) {
  2499. ret = submit_one_bio(rw, bio, mirror_num,
  2500. prev_bio_flags);
  2501. if (ret < 0) {
  2502. *bio_ret = NULL;
  2503. return ret;
  2504. }
  2505. bio = NULL;
  2506. } else {
  2507. return 0;
  2508. }
  2509. }
  2510. if (this_compressed)
  2511. nr = BIO_MAX_PAGES;
  2512. else
  2513. nr = bio_get_nr_vecs(bdev);
  2514. bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
  2515. if (!bio)
  2516. return -ENOMEM;
  2517. bio_add_page(bio, page, page_size, offset);
  2518. bio->bi_end_io = end_io_func;
  2519. bio->bi_private = tree;
  2520. if (bio_ret)
  2521. *bio_ret = bio;
  2522. else
  2523. ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
  2524. return ret;
  2525. }
  2526. static void attach_extent_buffer_page(struct extent_buffer *eb,
  2527. struct page *page)
  2528. {
  2529. if (!PagePrivate(page)) {
  2530. SetPagePrivate(page);
  2531. page_cache_get(page);
  2532. set_page_private(page, (unsigned long)eb);
  2533. } else {
  2534. WARN_ON(page->private != (unsigned long)eb);
  2535. }
  2536. }
  2537. void set_page_extent_mapped(struct page *page)
  2538. {
  2539. if (!PagePrivate(page)) {
  2540. SetPagePrivate(page);
  2541. page_cache_get(page);
  2542. set_page_private(page, EXTENT_PAGE_PRIVATE);
  2543. }
  2544. }
  2545. static struct extent_map *
  2546. __get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
  2547. u64 start, u64 len, get_extent_t *get_extent,
  2548. struct extent_map **em_cached)
  2549. {
  2550. struct extent_map *em;
  2551. if (em_cached && *em_cached) {
  2552. em = *em_cached;
  2553. if (extent_map_in_tree(em) && start >= em->start &&
  2554. start < extent_map_end(em)) {
  2555. atomic_inc(&em->refs);
  2556. return em;
  2557. }
  2558. free_extent_map(em);
  2559. *em_cached = NULL;
  2560. }
  2561. em = get_extent(inode, page, pg_offset, start, len, 0);
  2562. if (em_cached && !IS_ERR_OR_NULL(em)) {
  2563. BUG_ON(*em_cached);
  2564. atomic_inc(&em->refs);
  2565. *em_cached = em;
  2566. }
  2567. return em;
  2568. }
  2569. /*
  2570. * basic readpage implementation. Locked extent state structs are inserted
  2571. * into the tree that are removed when the IO is done (by the end_io
  2572. * handlers)
  2573. * XXX JDM: This needs looking at to ensure proper page locking
  2574. */
  2575. static int __do_readpage(struct extent_io_tree *tree,
  2576. struct page *page,
  2577. get_extent_t *get_extent,
  2578. struct extent_map **em_cached,
  2579. struct bio **bio, int mirror_num,
  2580. unsigned long *bio_flags, int rw)
  2581. {
  2582. struct inode *inode = page->mapping->host;
  2583. u64 start = page_offset(page);
  2584. u64 page_end = start + PAGE_CACHE_SIZE - 1;
  2585. u64 end;
  2586. u64 cur = start;
  2587. u64 extent_offset;
  2588. u64 last_byte = i_size_read(inode);
  2589. u64 block_start;
  2590. u64 cur_end;
  2591. sector_t sector;
  2592. struct extent_map *em;
  2593. struct block_device *bdev;
  2594. int ret;
  2595. int nr = 0;
  2596. int parent_locked = *bio_flags & EXTENT_BIO_PARENT_LOCKED;
  2597. size_t pg_offset = 0;
  2598. size_t iosize;
  2599. size_t disk_io_size;
  2600. size_t blocksize = inode->i_sb->s_blocksize;
  2601. unsigned long this_bio_flag = *bio_flags & EXTENT_BIO_PARENT_LOCKED;
  2602. set_page_extent_mapped(page);
  2603. end = page_end;
  2604. if (!PageUptodate(page)) {
  2605. if (cleancache_get_page(page) == 0) {
  2606. BUG_ON(blocksize != PAGE_SIZE);
  2607. unlock_extent(tree, start, end);
  2608. goto out;
  2609. }
  2610. }
  2611. if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
  2612. char *userpage;
  2613. size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
  2614. if (zero_offset) {
  2615. iosize = PAGE_CACHE_SIZE - zero_offset;
  2616. userpage = kmap_atomic(page);
  2617. memset(userpage + zero_offset, 0, iosize);
  2618. flush_dcache_page(page);
  2619. kunmap_atomic(userpage);
  2620. }
  2621. }
  2622. while (cur <= end) {
  2623. unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
  2624. if (cur >= last_byte) {
  2625. char *userpage;
  2626. struct extent_state *cached = NULL;
  2627. iosize = PAGE_CACHE_SIZE - pg_offset;
  2628. userpage = kmap_atomic(page);
  2629. memset(userpage + pg_offset, 0, iosize);
  2630. flush_dcache_page(page);
  2631. kunmap_atomic(userpage);
  2632. set_extent_uptodate(tree, cur, cur + iosize - 1,
  2633. &cached, GFP_NOFS);
  2634. if (!parent_locked)
  2635. unlock_extent_cached(tree, cur,
  2636. cur + iosize - 1,
  2637. &cached, GFP_NOFS);
  2638. break;
  2639. }
  2640. em = __get_extent_map(inode, page, pg_offset, cur,
  2641. end - cur + 1, get_extent, em_cached);
  2642. if (IS_ERR_OR_NULL(em)) {
  2643. SetPageError(page);
  2644. if (!parent_locked)
  2645. unlock_extent(tree, cur, end);
  2646. break;
  2647. }
  2648. extent_offset = cur - em->start;
  2649. BUG_ON(extent_map_end(em) <= cur);
  2650. BUG_ON(end < cur);
  2651. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  2652. this_bio_flag |= EXTENT_BIO_COMPRESSED;
  2653. extent_set_compress_type(&this_bio_flag,
  2654. em->compress_type);
  2655. }
  2656. iosize = min(extent_map_end(em) - cur, end - cur + 1);
  2657. cur_end = min(extent_map_end(em) - 1, end);
  2658. iosize = ALIGN(iosize, blocksize);
  2659. if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
  2660. disk_io_size = em->block_len;
  2661. sector = em->block_start >> 9;
  2662. } else {
  2663. sector = (em->block_start + extent_offset) >> 9;
  2664. disk_io_size = iosize;
  2665. }
  2666. bdev = em->bdev;
  2667. block_start = em->block_start;
  2668. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  2669. block_start = EXTENT_MAP_HOLE;
  2670. free_extent_map(em);
  2671. em = NULL;
  2672. /* we've found a hole, just zero and go on */
  2673. if (block_start == EXTENT_MAP_HOLE) {
  2674. char *userpage;
  2675. struct extent_state *cached = NULL;
  2676. userpage = kmap_atomic(page);
  2677. memset(userpage + pg_offset, 0, iosize);
  2678. flush_dcache_page(page);
  2679. kunmap_atomic(userpage);
  2680. set_extent_uptodate(tree, cur, cur + iosize - 1,
  2681. &cached, GFP_NOFS);
  2682. unlock_extent_cached(tree, cur, cur + iosize - 1,
  2683. &cached, GFP_NOFS);
  2684. cur = cur + iosize;
  2685. pg_offset += iosize;
  2686. continue;
  2687. }
  2688. /* the get_extent function already copied into the page */
  2689. if (test_range_bit(tree, cur, cur_end,
  2690. EXTENT_UPTODATE, 1, NULL)) {
  2691. check_page_uptodate(tree, page);
  2692. if (!parent_locked)
  2693. unlock_extent(tree, cur, cur + iosize - 1);
  2694. cur = cur + iosize;
  2695. pg_offset += iosize;
  2696. continue;
  2697. }
  2698. /* we have an inline extent but it didn't get marked up
  2699. * to date. Error out
  2700. */
  2701. if (block_start == EXTENT_MAP_INLINE) {
  2702. SetPageError(page);
  2703. if (!parent_locked)
  2704. unlock_extent(tree, cur, cur + iosize - 1);
  2705. cur = cur + iosize;
  2706. pg_offset += iosize;
  2707. continue;
  2708. }
  2709. pnr -= page->index;
  2710. ret = submit_extent_page(rw, tree, page,
  2711. sector, disk_io_size, pg_offset,
  2712. bdev, bio, pnr,
  2713. end_bio_extent_readpage, mirror_num,
  2714. *bio_flags,
  2715. this_bio_flag);
  2716. if (!ret) {
  2717. nr++;
  2718. *bio_flags = this_bio_flag;
  2719. } else {
  2720. SetPageError(page);
  2721. if (!parent_locked)
  2722. unlock_extent(tree, cur, cur + iosize - 1);
  2723. }
  2724. cur = cur + iosize;
  2725. pg_offset += iosize;
  2726. }
  2727. out:
  2728. if (!nr) {
  2729. if (!PageError(page))
  2730. SetPageUptodate(page);
  2731. unlock_page(page);
  2732. }
  2733. return 0;
  2734. }
  2735. static inline void __do_contiguous_readpages(struct extent_io_tree *tree,
  2736. struct page *pages[], int nr_pages,
  2737. u64 start, u64 end,
  2738. get_extent_t *get_extent,
  2739. struct extent_map **em_cached,
  2740. struct bio **bio, int mirror_num,
  2741. unsigned long *bio_flags, int rw)
  2742. {
  2743. struct inode *inode;
  2744. struct btrfs_ordered_extent *ordered;
  2745. int index;
  2746. inode = pages[0]->mapping->host;
  2747. while (1) {
  2748. lock_extent(tree, start, end);
  2749. ordered = btrfs_lookup_ordered_range(inode, start,
  2750. end - start + 1);
  2751. if (!ordered)
  2752. break;
  2753. unlock_extent(tree, start, end);
  2754. btrfs_start_ordered_extent(inode, ordered, 1);
  2755. btrfs_put_ordered_extent(ordered);
  2756. }
  2757. for (index = 0; index < nr_pages; index++) {
  2758. __do_readpage(tree, pages[index], get_extent, em_cached, bio,
  2759. mirror_num, bio_flags, rw);
  2760. page_cache_release(pages[index]);
  2761. }
  2762. }
  2763. static void __extent_readpages(struct extent_io_tree *tree,
  2764. struct page *pages[],
  2765. int nr_pages, get_extent_t *get_extent,
  2766. struct extent_map **em_cached,
  2767. struct bio **bio, int mirror_num,
  2768. unsigned long *bio_flags, int rw)
  2769. {
  2770. u64 start = 0;
  2771. u64 end = 0;
  2772. u64 page_start;
  2773. int index;
  2774. int first_index = 0;
  2775. for (index = 0; index < nr_pages; index++) {
  2776. page_start = page_offset(pages[index]);
  2777. if (!end) {
  2778. start = page_start;
  2779. end = start + PAGE_CACHE_SIZE - 1;
  2780. first_index = index;
  2781. } else if (end + 1 == page_start) {
  2782. end += PAGE_CACHE_SIZE;
  2783. } else {
  2784. __do_contiguous_readpages(tree, &pages[first_index],
  2785. index - first_index, start,
  2786. end, get_extent, em_cached,
  2787. bio, mirror_num, bio_flags,
  2788. rw);
  2789. start = page_start;
  2790. end = start + PAGE_CACHE_SIZE - 1;
  2791. first_index = index;
  2792. }
  2793. }
  2794. if (end)
  2795. __do_contiguous_readpages(tree, &pages[first_index],
  2796. index - first_index, start,
  2797. end, get_extent, em_cached, bio,
  2798. mirror_num, bio_flags, rw);
  2799. }
  2800. static int __extent_read_full_page(struct extent_io_tree *tree,
  2801. struct page *page,
  2802. get_extent_t *get_extent,
  2803. struct bio **bio, int mirror_num,
  2804. unsigned long *bio_flags, int rw)
  2805. {
  2806. struct inode *inode = page->mapping->host;
  2807. struct btrfs_ordered_extent *ordered;
  2808. u64 start = page_offset(page);
  2809. u64 end = start + PAGE_CACHE_SIZE - 1;
  2810. int ret;
  2811. while (1) {
  2812. lock_extent(tree, start, end);
  2813. ordered = btrfs_lookup_ordered_extent(inode, start);
  2814. if (!ordered)
  2815. break;
  2816. unlock_extent(tree, start, end);
  2817. btrfs_start_ordered_extent(inode, ordered, 1);
  2818. btrfs_put_ordered_extent(ordered);
  2819. }
  2820. ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num,
  2821. bio_flags, rw);
  2822. return ret;
  2823. }
  2824. int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
  2825. get_extent_t *get_extent, int mirror_num)
  2826. {
  2827. struct bio *bio = NULL;
  2828. unsigned long bio_flags = 0;
  2829. int ret;
  2830. ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
  2831. &bio_flags, READ);
  2832. if (bio)
  2833. ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
  2834. return ret;
  2835. }
  2836. int extent_read_full_page_nolock(struct extent_io_tree *tree, struct page *page,
  2837. get_extent_t *get_extent, int mirror_num)
  2838. {
  2839. struct bio *bio = NULL;
  2840. unsigned long bio_flags = EXTENT_BIO_PARENT_LOCKED;
  2841. int ret;
  2842. ret = __do_readpage(tree, page, get_extent, NULL, &bio, mirror_num,
  2843. &bio_flags, READ);
  2844. if (bio)
  2845. ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
  2846. return ret;
  2847. }
  2848. static noinline void update_nr_written(struct page *page,
  2849. struct writeback_control *wbc,
  2850. unsigned long nr_written)
  2851. {
  2852. wbc->nr_to_write -= nr_written;
  2853. if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
  2854. wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
  2855. page->mapping->writeback_index = page->index + nr_written;
  2856. }
  2857. /*
  2858. * helper for __extent_writepage, doing all of the delayed allocation setup.
  2859. *
  2860. * This returns 1 if our fill_delalloc function did all the work required
  2861. * to write the page (copy into inline extent). In this case the IO has
  2862. * been started and the page is already unlocked.
  2863. *
  2864. * This returns 0 if all went well (page still locked)
  2865. * This returns < 0 if there were errors (page still locked)
  2866. */
  2867. static noinline_for_stack int writepage_delalloc(struct inode *inode,
  2868. struct page *page, struct writeback_control *wbc,
  2869. struct extent_page_data *epd,
  2870. u64 delalloc_start,
  2871. unsigned long *nr_written)
  2872. {
  2873. struct extent_io_tree *tree = epd->tree;
  2874. u64 page_end = delalloc_start + PAGE_CACHE_SIZE - 1;
  2875. u64 nr_delalloc;
  2876. u64 delalloc_to_write = 0;
  2877. u64 delalloc_end = 0;
  2878. int ret;
  2879. int page_started = 0;
  2880. if (epd->extent_locked || !tree->ops || !tree->ops->fill_delalloc)
  2881. return 0;
  2882. while (delalloc_end < page_end) {
  2883. nr_delalloc = find_lock_delalloc_range(inode, tree,
  2884. page,
  2885. &delalloc_start,
  2886. &delalloc_end,
  2887. BTRFS_MAX_EXTENT_SIZE);
  2888. if (nr_delalloc == 0) {
  2889. delalloc_start = delalloc_end + 1;
  2890. continue;
  2891. }
  2892. ret = tree->ops->fill_delalloc(inode, page,
  2893. delalloc_start,
  2894. delalloc_end,
  2895. &page_started,
  2896. nr_written);
  2897. /* File system has been set read-only */
  2898. if (ret) {
  2899. SetPageError(page);
  2900. /* fill_delalloc should be return < 0 for error
  2901. * but just in case, we use > 0 here meaning the
  2902. * IO is started, so we don't want to return > 0
  2903. * unless things are going well.
  2904. */
  2905. ret = ret < 0 ? ret : -EIO;
  2906. goto done;
  2907. }
  2908. /*
  2909. * delalloc_end is already one less than the total
  2910. * length, so we don't subtract one from
  2911. * PAGE_CACHE_SIZE
  2912. */
  2913. delalloc_to_write += (delalloc_end - delalloc_start +
  2914. PAGE_CACHE_SIZE) >>
  2915. PAGE_CACHE_SHIFT;
  2916. delalloc_start = delalloc_end + 1;
  2917. }
  2918. if (wbc->nr_to_write < delalloc_to_write) {
  2919. int thresh = 8192;
  2920. if (delalloc_to_write < thresh * 2)
  2921. thresh = delalloc_to_write;
  2922. wbc->nr_to_write = min_t(u64, delalloc_to_write,
  2923. thresh);
  2924. }
  2925. /* did the fill delalloc function already unlock and start
  2926. * the IO?
  2927. */
  2928. if (page_started) {
  2929. /*
  2930. * we've unlocked the page, so we can't update
  2931. * the mapping's writeback index, just update
  2932. * nr_to_write.
  2933. */
  2934. wbc->nr_to_write -= *nr_written;
  2935. return 1;
  2936. }
  2937. ret = 0;
  2938. done:
  2939. return ret;
  2940. }
  2941. /*
  2942. * helper for __extent_writepage. This calls the writepage start hooks,
  2943. * and does the loop to map the page into extents and bios.
  2944. *
  2945. * We return 1 if the IO is started and the page is unlocked,
  2946. * 0 if all went well (page still locked)
  2947. * < 0 if there were errors (page still locked)
  2948. */
  2949. static noinline_for_stack int __extent_writepage_io(struct inode *inode,
  2950. struct page *page,
  2951. struct writeback_control *wbc,
  2952. struct extent_page_data *epd,
  2953. loff_t i_size,
  2954. unsigned long nr_written,
  2955. int write_flags, int *nr_ret)
  2956. {
  2957. struct extent_io_tree *tree = epd->tree;
  2958. u64 start = page_offset(page);
  2959. u64 page_end = start + PAGE_CACHE_SIZE - 1;
  2960. u64 end;
  2961. u64 cur = start;
  2962. u64 extent_offset;
  2963. u64 block_start;
  2964. u64 iosize;
  2965. sector_t sector;
  2966. struct extent_state *cached_state = NULL;
  2967. struct extent_map *em;
  2968. struct block_device *bdev;
  2969. size_t pg_offset = 0;
  2970. size_t blocksize;
  2971. int ret = 0;
  2972. int nr = 0;
  2973. bool compressed;
  2974. if (tree->ops && tree->ops->writepage_start_hook) {
  2975. ret = tree->ops->writepage_start_hook(page, start,
  2976. page_end);
  2977. if (ret) {
  2978. /* Fixup worker will requeue */
  2979. if (ret == -EBUSY)
  2980. wbc->pages_skipped++;
  2981. else
  2982. redirty_page_for_writepage(wbc, page);
  2983. update_nr_written(page, wbc, nr_written);
  2984. unlock_page(page);
  2985. ret = 1;
  2986. goto done_unlocked;
  2987. }
  2988. }
  2989. /*
  2990. * we don't want to touch the inode after unlocking the page,
  2991. * so we update the mapping writeback index now
  2992. */
  2993. update_nr_written(page, wbc, nr_written + 1);
  2994. end = page_end;
  2995. if (i_size <= start) {
  2996. if (tree->ops && tree->ops->writepage_end_io_hook)
  2997. tree->ops->writepage_end_io_hook(page, start,
  2998. page_end, NULL, 1);
  2999. goto done;
  3000. }
  3001. blocksize = inode->i_sb->s_blocksize;
  3002. while (cur <= end) {
  3003. u64 em_end;
  3004. if (cur >= i_size) {
  3005. if (tree->ops && tree->ops->writepage_end_io_hook)
  3006. tree->ops->writepage_end_io_hook(page, cur,
  3007. page_end, NULL, 1);
  3008. break;
  3009. }
  3010. em = epd->get_extent(inode, page, pg_offset, cur,
  3011. end - cur + 1, 1);
  3012. if (IS_ERR_OR_NULL(em)) {
  3013. SetPageError(page);
  3014. ret = PTR_ERR_OR_ZERO(em);
  3015. break;
  3016. }
  3017. extent_offset = cur - em->start;
  3018. em_end = extent_map_end(em);
  3019. BUG_ON(em_end <= cur);
  3020. BUG_ON(end < cur);
  3021. iosize = min(em_end - cur, end - cur + 1);
  3022. iosize = ALIGN(iosize, blocksize);
  3023. sector = (em->block_start + extent_offset) >> 9;
  3024. bdev = em->bdev;
  3025. block_start = em->block_start;
  3026. compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  3027. free_extent_map(em);
  3028. em = NULL;
  3029. /*
  3030. * compressed and inline extents are written through other
  3031. * paths in the FS
  3032. */
  3033. if (compressed || block_start == EXTENT_MAP_HOLE ||
  3034. block_start == EXTENT_MAP_INLINE) {
  3035. /*
  3036. * end_io notification does not happen here for
  3037. * compressed extents
  3038. */
  3039. if (!compressed && tree->ops &&
  3040. tree->ops->writepage_end_io_hook)
  3041. tree->ops->writepage_end_io_hook(page, cur,
  3042. cur + iosize - 1,
  3043. NULL, 1);
  3044. else if (compressed) {
  3045. /* we don't want to end_page_writeback on
  3046. * a compressed extent. this happens
  3047. * elsewhere
  3048. */
  3049. nr++;
  3050. }
  3051. cur += iosize;
  3052. pg_offset += iosize;
  3053. continue;
  3054. }
  3055. if (tree->ops && tree->ops->writepage_io_hook) {
  3056. ret = tree->ops->writepage_io_hook(page, cur,
  3057. cur + iosize - 1);
  3058. } else {
  3059. ret = 0;
  3060. }
  3061. if (ret) {
  3062. SetPageError(page);
  3063. } else {
  3064. unsigned long max_nr = (i_size >> PAGE_CACHE_SHIFT) + 1;
  3065. set_range_writeback(tree, cur, cur + iosize - 1);
  3066. if (!PageWriteback(page)) {
  3067. btrfs_err(BTRFS_I(inode)->root->fs_info,
  3068. "page %lu not writeback, cur %llu end %llu",
  3069. page->index, cur, end);
  3070. }
  3071. ret = submit_extent_page(write_flags, tree, page,
  3072. sector, iosize, pg_offset,
  3073. bdev, &epd->bio, max_nr,
  3074. end_bio_extent_writepage,
  3075. 0, 0, 0);
  3076. if (ret)
  3077. SetPageError(page);
  3078. }
  3079. cur = cur + iosize;
  3080. pg_offset += iosize;
  3081. nr++;
  3082. }
  3083. done:
  3084. *nr_ret = nr;
  3085. done_unlocked:
  3086. /* drop our reference on any cached states */
  3087. free_extent_state(cached_state);
  3088. return ret;
  3089. }
  3090. /*
  3091. * the writepage semantics are similar to regular writepage. extent
  3092. * records are inserted to lock ranges in the tree, and as dirty areas
  3093. * are found, they are marked writeback. Then the lock bits are removed
  3094. * and the end_io handler clears the writeback ranges
  3095. */
  3096. static int __extent_writepage(struct page *page, struct writeback_control *wbc,
  3097. void *data)
  3098. {
  3099. struct inode *inode = page->mapping->host;
  3100. struct extent_page_data *epd = data;
  3101. u64 start = page_offset(page);
  3102. u64 page_end = start + PAGE_CACHE_SIZE - 1;
  3103. int ret;
  3104. int nr = 0;
  3105. size_t pg_offset = 0;
  3106. loff_t i_size = i_size_read(inode);
  3107. unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
  3108. int write_flags;
  3109. unsigned long nr_written = 0;
  3110. if (wbc->sync_mode == WB_SYNC_ALL)
  3111. write_flags = WRITE_SYNC;
  3112. else
  3113. write_flags = WRITE;
  3114. trace___extent_writepage(page, inode, wbc);
  3115. WARN_ON(!PageLocked(page));
  3116. ClearPageError(page);
  3117. pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
  3118. if (page->index > end_index ||
  3119. (page->index == end_index && !pg_offset)) {
  3120. page->mapping->a_ops->invalidatepage(page, 0, PAGE_CACHE_SIZE);
  3121. unlock_page(page);
  3122. return 0;
  3123. }
  3124. if (page->index == end_index) {
  3125. char *userpage;
  3126. userpage = kmap_atomic(page);
  3127. memset(userpage + pg_offset, 0,
  3128. PAGE_CACHE_SIZE - pg_offset);
  3129. kunmap_atomic(userpage);
  3130. flush_dcache_page(page);
  3131. }
  3132. pg_offset = 0;
  3133. set_page_extent_mapped(page);
  3134. ret = writepage_delalloc(inode, page, wbc, epd, start, &nr_written);
  3135. if (ret == 1)
  3136. goto done_unlocked;
  3137. if (ret)
  3138. goto done;
  3139. ret = __extent_writepage_io(inode, page, wbc, epd,
  3140. i_size, nr_written, write_flags, &nr);
  3141. if (ret == 1)
  3142. goto done_unlocked;
  3143. done:
  3144. if (nr == 0) {
  3145. /* make sure the mapping tag for page dirty gets cleared */
  3146. set_page_writeback(page);
  3147. end_page_writeback(page);
  3148. }
  3149. if (PageError(page)) {
  3150. ret = ret < 0 ? ret : -EIO;
  3151. end_extent_writepage(page, ret, start, page_end);
  3152. }
  3153. unlock_page(page);
  3154. return ret;
  3155. done_unlocked:
  3156. return 0;
  3157. }
  3158. void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
  3159. {
  3160. wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
  3161. TASK_UNINTERRUPTIBLE);
  3162. }
  3163. static noinline_for_stack int
  3164. lock_extent_buffer_for_io(struct extent_buffer *eb,
  3165. struct btrfs_fs_info *fs_info,
  3166. struct extent_page_data *epd)
  3167. {
  3168. unsigned long i, num_pages;
  3169. int flush = 0;
  3170. int ret = 0;
  3171. if (!btrfs_try_tree_write_lock(eb)) {
  3172. flush = 1;
  3173. flush_write_bio(epd);
  3174. btrfs_tree_lock(eb);
  3175. }
  3176. if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
  3177. btrfs_tree_unlock(eb);
  3178. if (!epd->sync_io)
  3179. return 0;
  3180. if (!flush) {
  3181. flush_write_bio(epd);
  3182. flush = 1;
  3183. }
  3184. while (1) {
  3185. wait_on_extent_buffer_writeback(eb);
  3186. btrfs_tree_lock(eb);
  3187. if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
  3188. break;
  3189. btrfs_tree_unlock(eb);
  3190. }
  3191. }
  3192. /*
  3193. * We need to do this to prevent races in people who check if the eb is
  3194. * under IO since we can end up having no IO bits set for a short period
  3195. * of time.
  3196. */
  3197. spin_lock(&eb->refs_lock);
  3198. if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
  3199. set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
  3200. spin_unlock(&eb->refs_lock);
  3201. btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
  3202. __percpu_counter_add(&fs_info->dirty_metadata_bytes,
  3203. -eb->len,
  3204. fs_info->dirty_metadata_batch);
  3205. ret = 1;
  3206. } else {
  3207. spin_unlock(&eb->refs_lock);
  3208. }
  3209. btrfs_tree_unlock(eb);
  3210. if (!ret)
  3211. return ret;
  3212. num_pages = num_extent_pages(eb->start, eb->len);
  3213. for (i = 0; i < num_pages; i++) {
  3214. struct page *p = eb->pages[i];
  3215. if (!trylock_page(p)) {
  3216. if (!flush) {
  3217. flush_write_bio(epd);
  3218. flush = 1;
  3219. }
  3220. lock_page(p);
  3221. }
  3222. }
  3223. return ret;
  3224. }
  3225. static void end_extent_buffer_writeback(struct extent_buffer *eb)
  3226. {
  3227. clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
  3228. smp_mb__after_atomic();
  3229. wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
  3230. }
  3231. static void set_btree_ioerr(struct page *page)
  3232. {
  3233. struct extent_buffer *eb = (struct extent_buffer *)page->private;
  3234. struct btrfs_inode *btree_ino = BTRFS_I(eb->fs_info->btree_inode);
  3235. SetPageError(page);
  3236. if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
  3237. return;
  3238. /*
  3239. * If writeback for a btree extent that doesn't belong to a log tree
  3240. * failed, increment the counter transaction->eb_write_errors.
  3241. * We do this because while the transaction is running and before it's
  3242. * committing (when we call filemap_fdata[write|wait]_range against
  3243. * the btree inode), we might have
  3244. * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
  3245. * returns an error or an error happens during writeback, when we're
  3246. * committing the transaction we wouldn't know about it, since the pages
  3247. * can be no longer dirty nor marked anymore for writeback (if a
  3248. * subsequent modification to the extent buffer didn't happen before the
  3249. * transaction commit), which makes filemap_fdata[write|wait]_range not
  3250. * able to find the pages tagged with SetPageError at transaction
  3251. * commit time. So if this happens we must abort the transaction,
  3252. * otherwise we commit a super block with btree roots that point to
  3253. * btree nodes/leafs whose content on disk is invalid - either garbage
  3254. * or the content of some node/leaf from a past generation that got
  3255. * cowed or deleted and is no longer valid.
  3256. *
  3257. * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
  3258. * not be enough - we need to distinguish between log tree extents vs
  3259. * non-log tree extents, and the next filemap_fdatawait_range() call
  3260. * will catch and clear such errors in the mapping - and that call might
  3261. * be from a log sync and not from a transaction commit. Also, checking
  3262. * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
  3263. * not done and would not be reliable - the eb might have been released
  3264. * from memory and reading it back again means that flag would not be
  3265. * set (since it's a runtime flag, not persisted on disk).
  3266. *
  3267. * Using the flags below in the btree inode also makes us achieve the
  3268. * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
  3269. * writeback for all dirty pages and before filemap_fdatawait_range()
  3270. * is called, the writeback for all dirty pages had already finished
  3271. * with errors - because we were not using AS_EIO/AS_ENOSPC,
  3272. * filemap_fdatawait_range() would return success, as it could not know
  3273. * that writeback errors happened (the pages were no longer tagged for
  3274. * writeback).
  3275. */
  3276. switch (eb->log_index) {
  3277. case -1:
  3278. set_bit(BTRFS_INODE_BTREE_ERR, &btree_ino->runtime_flags);
  3279. break;
  3280. case 0:
  3281. set_bit(BTRFS_INODE_BTREE_LOG1_ERR, &btree_ino->runtime_flags);
  3282. break;
  3283. case 1:
  3284. set_bit(BTRFS_INODE_BTREE_LOG2_ERR, &btree_ino->runtime_flags);
  3285. break;
  3286. default:
  3287. BUG(); /* unexpected, logic error */
  3288. }
  3289. }
  3290. static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
  3291. {
  3292. struct bio_vec *bvec;
  3293. struct extent_buffer *eb;
  3294. int i, done;
  3295. bio_for_each_segment_all(bvec, bio, i) {
  3296. struct page *page = bvec->bv_page;
  3297. eb = (struct extent_buffer *)page->private;
  3298. BUG_ON(!eb);
  3299. done = atomic_dec_and_test(&eb->io_pages);
  3300. if (err || test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
  3301. ClearPageUptodate(page);
  3302. set_btree_ioerr(page);
  3303. }
  3304. end_page_writeback(page);
  3305. if (!done)
  3306. continue;
  3307. end_extent_buffer_writeback(eb);
  3308. }
  3309. bio_put(bio);
  3310. }
  3311. static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
  3312. struct btrfs_fs_info *fs_info,
  3313. struct writeback_control *wbc,
  3314. struct extent_page_data *epd)
  3315. {
  3316. struct block_device *bdev = fs_info->fs_devices->latest_bdev;
  3317. struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
  3318. u64 offset = eb->start;
  3319. unsigned long i, num_pages;
  3320. unsigned long bio_flags = 0;
  3321. int rw = (epd->sync_io ? WRITE_SYNC : WRITE) | REQ_META;
  3322. int ret = 0;
  3323. clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
  3324. num_pages = num_extent_pages(eb->start, eb->len);
  3325. atomic_set(&eb->io_pages, num_pages);
  3326. if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID)
  3327. bio_flags = EXTENT_BIO_TREE_LOG;
  3328. for (i = 0; i < num_pages; i++) {
  3329. struct page *p = eb->pages[i];
  3330. clear_page_dirty_for_io(p);
  3331. set_page_writeback(p);
  3332. ret = submit_extent_page(rw, tree, p, offset >> 9,
  3333. PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
  3334. -1, end_bio_extent_buffer_writepage,
  3335. 0, epd->bio_flags, bio_flags);
  3336. epd->bio_flags = bio_flags;
  3337. if (ret) {
  3338. set_btree_ioerr(p);
  3339. end_page_writeback(p);
  3340. if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
  3341. end_extent_buffer_writeback(eb);
  3342. ret = -EIO;
  3343. break;
  3344. }
  3345. offset += PAGE_CACHE_SIZE;
  3346. update_nr_written(p, wbc, 1);
  3347. unlock_page(p);
  3348. }
  3349. if (unlikely(ret)) {
  3350. for (; i < num_pages; i++) {
  3351. struct page *p = eb->pages[i];
  3352. clear_page_dirty_for_io(p);
  3353. unlock_page(p);
  3354. }
  3355. }
  3356. return ret;
  3357. }
  3358. int btree_write_cache_pages(struct address_space *mapping,
  3359. struct writeback_control *wbc)
  3360. {
  3361. struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
  3362. struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
  3363. struct extent_buffer *eb, *prev_eb = NULL;
  3364. struct extent_page_data epd = {
  3365. .bio = NULL,
  3366. .tree = tree,
  3367. .extent_locked = 0,
  3368. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  3369. .bio_flags = 0,
  3370. };
  3371. int ret = 0;
  3372. int done = 0;
  3373. int nr_to_write_done = 0;
  3374. struct pagevec pvec;
  3375. int nr_pages;
  3376. pgoff_t index;
  3377. pgoff_t end; /* Inclusive */
  3378. int scanned = 0;
  3379. int tag;
  3380. pagevec_init(&pvec, 0);
  3381. if (wbc->range_cyclic) {
  3382. index = mapping->writeback_index; /* Start from prev offset */
  3383. end = -1;
  3384. } else {
  3385. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  3386. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  3387. scanned = 1;
  3388. }
  3389. if (wbc->sync_mode == WB_SYNC_ALL)
  3390. tag = PAGECACHE_TAG_TOWRITE;
  3391. else
  3392. tag = PAGECACHE_TAG_DIRTY;
  3393. retry:
  3394. if (wbc->sync_mode == WB_SYNC_ALL)
  3395. tag_pages_for_writeback(mapping, index, end);
  3396. while (!done && !nr_to_write_done && (index <= end) &&
  3397. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
  3398. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
  3399. unsigned i;
  3400. scanned = 1;
  3401. for (i = 0; i < nr_pages; i++) {
  3402. struct page *page = pvec.pages[i];
  3403. if (!PagePrivate(page))
  3404. continue;
  3405. if (!wbc->range_cyclic && page->index > end) {
  3406. done = 1;
  3407. break;
  3408. }
  3409. spin_lock(&mapping->private_lock);
  3410. if (!PagePrivate(page)) {
  3411. spin_unlock(&mapping->private_lock);
  3412. continue;
  3413. }
  3414. eb = (struct extent_buffer *)page->private;
  3415. /*
  3416. * Shouldn't happen and normally this would be a BUG_ON
  3417. * but no sense in crashing the users box for something
  3418. * we can survive anyway.
  3419. */
  3420. if (WARN_ON(!eb)) {
  3421. spin_unlock(&mapping->private_lock);
  3422. continue;
  3423. }
  3424. if (eb == prev_eb) {
  3425. spin_unlock(&mapping->private_lock);
  3426. continue;
  3427. }
  3428. ret = atomic_inc_not_zero(&eb->refs);
  3429. spin_unlock(&mapping->private_lock);
  3430. if (!ret)
  3431. continue;
  3432. prev_eb = eb;
  3433. ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
  3434. if (!ret) {
  3435. free_extent_buffer(eb);
  3436. continue;
  3437. }
  3438. ret = write_one_eb(eb, fs_info, wbc, &epd);
  3439. if (ret) {
  3440. done = 1;
  3441. free_extent_buffer(eb);
  3442. break;
  3443. }
  3444. free_extent_buffer(eb);
  3445. /*
  3446. * the filesystem may choose to bump up nr_to_write.
  3447. * We have to make sure to honor the new nr_to_write
  3448. * at any time
  3449. */
  3450. nr_to_write_done = wbc->nr_to_write <= 0;
  3451. }
  3452. pagevec_release(&pvec);
  3453. cond_resched();
  3454. }
  3455. if (!scanned && !done) {
  3456. /*
  3457. * We hit the last page and there is more work to be done: wrap
  3458. * back to the start of the file
  3459. */
  3460. scanned = 1;
  3461. index = 0;
  3462. goto retry;
  3463. }
  3464. flush_write_bio(&epd);
  3465. return ret;
  3466. }
  3467. /**
  3468. * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
  3469. * @mapping: address space structure to write
  3470. * @wbc: subtract the number of written pages from *@wbc->nr_to_write
  3471. * @writepage: function called for each page
  3472. * @data: data passed to writepage function
  3473. *
  3474. * If a page is already under I/O, write_cache_pages() skips it, even
  3475. * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
  3476. * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
  3477. * and msync() need to guarantee that all the data which was dirty at the time
  3478. * the call was made get new I/O started against them. If wbc->sync_mode is
  3479. * WB_SYNC_ALL then we were called for data integrity and we must wait for
  3480. * existing IO to complete.
  3481. */
  3482. static int extent_write_cache_pages(struct extent_io_tree *tree,
  3483. struct address_space *mapping,
  3484. struct writeback_control *wbc,
  3485. writepage_t writepage, void *data,
  3486. void (*flush_fn)(void *))
  3487. {
  3488. struct inode *inode = mapping->host;
  3489. int ret = 0;
  3490. int done = 0;
  3491. int err = 0;
  3492. int nr_to_write_done = 0;
  3493. struct pagevec pvec;
  3494. int nr_pages;
  3495. pgoff_t index;
  3496. pgoff_t end; /* Inclusive */
  3497. int scanned = 0;
  3498. int tag;
  3499. /*
  3500. * We have to hold onto the inode so that ordered extents can do their
  3501. * work when the IO finishes. The alternative to this is failing to add
  3502. * an ordered extent if the igrab() fails there and that is a huge pain
  3503. * to deal with, so instead just hold onto the inode throughout the
  3504. * writepages operation. If it fails here we are freeing up the inode
  3505. * anyway and we'd rather not waste our time writing out stuff that is
  3506. * going to be truncated anyway.
  3507. */
  3508. if (!igrab(inode))
  3509. return 0;
  3510. pagevec_init(&pvec, 0);
  3511. if (wbc->range_cyclic) {
  3512. index = mapping->writeback_index; /* Start from prev offset */
  3513. end = -1;
  3514. } else {
  3515. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  3516. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  3517. scanned = 1;
  3518. }
  3519. if (wbc->sync_mode == WB_SYNC_ALL)
  3520. tag = PAGECACHE_TAG_TOWRITE;
  3521. else
  3522. tag = PAGECACHE_TAG_DIRTY;
  3523. retry:
  3524. if (wbc->sync_mode == WB_SYNC_ALL)
  3525. tag_pages_for_writeback(mapping, index, end);
  3526. while (!done && !nr_to_write_done && (index <= end) &&
  3527. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
  3528. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
  3529. unsigned i;
  3530. scanned = 1;
  3531. for (i = 0; i < nr_pages; i++) {
  3532. struct page *page = pvec.pages[i];
  3533. /*
  3534. * At this point we hold neither mapping->tree_lock nor
  3535. * lock on the page itself: the page may be truncated or
  3536. * invalidated (changing page->mapping to NULL), or even
  3537. * swizzled back from swapper_space to tmpfs file
  3538. * mapping
  3539. */
  3540. if (!trylock_page(page)) {
  3541. flush_fn(data);
  3542. lock_page(page);
  3543. }
  3544. if (unlikely(page->mapping != mapping)) {
  3545. unlock_page(page);
  3546. continue;
  3547. }
  3548. if (!wbc->range_cyclic && page->index > end) {
  3549. done = 1;
  3550. unlock_page(page);
  3551. continue;
  3552. }
  3553. if (wbc->sync_mode != WB_SYNC_NONE) {
  3554. if (PageWriteback(page))
  3555. flush_fn(data);
  3556. wait_on_page_writeback(page);
  3557. }
  3558. if (PageWriteback(page) ||
  3559. !clear_page_dirty_for_io(page)) {
  3560. unlock_page(page);
  3561. continue;
  3562. }
  3563. ret = (*writepage)(page, wbc, data);
  3564. if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
  3565. unlock_page(page);
  3566. ret = 0;
  3567. }
  3568. if (!err && ret < 0)
  3569. err = ret;
  3570. /*
  3571. * the filesystem may choose to bump up nr_to_write.
  3572. * We have to make sure to honor the new nr_to_write
  3573. * at any time
  3574. */
  3575. nr_to_write_done = wbc->nr_to_write <= 0;
  3576. }
  3577. pagevec_release(&pvec);
  3578. cond_resched();
  3579. }
  3580. if (!scanned && !done && !err) {
  3581. /*
  3582. * We hit the last page and there is more work to be done: wrap
  3583. * back to the start of the file
  3584. */
  3585. scanned = 1;
  3586. index = 0;
  3587. goto retry;
  3588. }
  3589. btrfs_add_delayed_iput(inode);
  3590. return err;
  3591. }
  3592. static void flush_epd_write_bio(struct extent_page_data *epd)
  3593. {
  3594. if (epd->bio) {
  3595. int rw = WRITE;
  3596. int ret;
  3597. if (epd->sync_io)
  3598. rw = WRITE_SYNC;
  3599. ret = submit_one_bio(rw, epd->bio, 0, epd->bio_flags);
  3600. BUG_ON(ret < 0); /* -ENOMEM */
  3601. epd->bio = NULL;
  3602. }
  3603. }
  3604. static noinline void flush_write_bio(void *data)
  3605. {
  3606. struct extent_page_data *epd = data;
  3607. flush_epd_write_bio(epd);
  3608. }
  3609. int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
  3610. get_extent_t *get_extent,
  3611. struct writeback_control *wbc)
  3612. {
  3613. int ret;
  3614. struct extent_page_data epd = {
  3615. .bio = NULL,
  3616. .tree = tree,
  3617. .get_extent = get_extent,
  3618. .extent_locked = 0,
  3619. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  3620. .bio_flags = 0,
  3621. };
  3622. ret = __extent_writepage(page, wbc, &epd);
  3623. flush_epd_write_bio(&epd);
  3624. return ret;
  3625. }
  3626. int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
  3627. u64 start, u64 end, get_extent_t *get_extent,
  3628. int mode)
  3629. {
  3630. int ret = 0;
  3631. struct address_space *mapping = inode->i_mapping;
  3632. struct page *page;
  3633. unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
  3634. PAGE_CACHE_SHIFT;
  3635. struct extent_page_data epd = {
  3636. .bio = NULL,
  3637. .tree = tree,
  3638. .get_extent = get_extent,
  3639. .extent_locked = 1,
  3640. .sync_io = mode == WB_SYNC_ALL,
  3641. .bio_flags = 0,
  3642. };
  3643. struct writeback_control wbc_writepages = {
  3644. .sync_mode = mode,
  3645. .nr_to_write = nr_pages * 2,
  3646. .range_start = start,
  3647. .range_end = end + 1,
  3648. };
  3649. while (start <= end) {
  3650. page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
  3651. if (clear_page_dirty_for_io(page))
  3652. ret = __extent_writepage(page, &wbc_writepages, &epd);
  3653. else {
  3654. if (tree->ops && tree->ops->writepage_end_io_hook)
  3655. tree->ops->writepage_end_io_hook(page, start,
  3656. start + PAGE_CACHE_SIZE - 1,
  3657. NULL, 1);
  3658. unlock_page(page);
  3659. }
  3660. page_cache_release(page);
  3661. start += PAGE_CACHE_SIZE;
  3662. }
  3663. flush_epd_write_bio(&epd);
  3664. return ret;
  3665. }
  3666. int extent_writepages(struct extent_io_tree *tree,
  3667. struct address_space *mapping,
  3668. get_extent_t *get_extent,
  3669. struct writeback_control *wbc)
  3670. {
  3671. int ret = 0;
  3672. struct extent_page_data epd = {
  3673. .bio = NULL,
  3674. .tree = tree,
  3675. .get_extent = get_extent,
  3676. .extent_locked = 0,
  3677. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  3678. .bio_flags = 0,
  3679. };
  3680. ret = extent_write_cache_pages(tree, mapping, wbc,
  3681. __extent_writepage, &epd,
  3682. flush_write_bio);
  3683. flush_epd_write_bio(&epd);
  3684. return ret;
  3685. }
  3686. int extent_readpages(struct extent_io_tree *tree,
  3687. struct address_space *mapping,
  3688. struct list_head *pages, unsigned nr_pages,
  3689. get_extent_t get_extent)
  3690. {
  3691. struct bio *bio = NULL;
  3692. unsigned page_idx;
  3693. unsigned long bio_flags = 0;
  3694. struct page *pagepool[16];
  3695. struct page *page;
  3696. struct extent_map *em_cached = NULL;
  3697. int nr = 0;
  3698. for (page_idx = 0; page_idx < nr_pages; page_idx++) {
  3699. page = list_entry(pages->prev, struct page, lru);
  3700. prefetchw(&page->flags);
  3701. list_del(&page->lru);
  3702. if (add_to_page_cache_lru(page, mapping,
  3703. page->index, GFP_NOFS)) {
  3704. page_cache_release(page);
  3705. continue;
  3706. }
  3707. pagepool[nr++] = page;
  3708. if (nr < ARRAY_SIZE(pagepool))
  3709. continue;
  3710. __extent_readpages(tree, pagepool, nr, get_extent, &em_cached,
  3711. &bio, 0, &bio_flags, READ);
  3712. nr = 0;
  3713. }
  3714. if (nr)
  3715. __extent_readpages(tree, pagepool, nr, get_extent, &em_cached,
  3716. &bio, 0, &bio_flags, READ);
  3717. if (em_cached)
  3718. free_extent_map(em_cached);
  3719. BUG_ON(!list_empty(pages));
  3720. if (bio)
  3721. return submit_one_bio(READ, bio, 0, bio_flags);
  3722. return 0;
  3723. }
  3724. /*
  3725. * basic invalidatepage code, this waits on any locked or writeback
  3726. * ranges corresponding to the page, and then deletes any extent state
  3727. * records from the tree
  3728. */
  3729. int extent_invalidatepage(struct extent_io_tree *tree,
  3730. struct page *page, unsigned long offset)
  3731. {
  3732. struct extent_state *cached_state = NULL;
  3733. u64 start = page_offset(page);
  3734. u64 end = start + PAGE_CACHE_SIZE - 1;
  3735. size_t blocksize = page->mapping->host->i_sb->s_blocksize;
  3736. start += ALIGN(offset, blocksize);
  3737. if (start > end)
  3738. return 0;
  3739. lock_extent_bits(tree, start, end, 0, &cached_state);
  3740. wait_on_page_writeback(page);
  3741. clear_extent_bit(tree, start, end,
  3742. EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
  3743. EXTENT_DO_ACCOUNTING,
  3744. 1, 1, &cached_state, GFP_NOFS);
  3745. return 0;
  3746. }
  3747. /*
  3748. * a helper for releasepage, this tests for areas of the page that
  3749. * are locked or under IO and drops the related state bits if it is safe
  3750. * to drop the page.
  3751. */
  3752. static int try_release_extent_state(struct extent_map_tree *map,
  3753. struct extent_io_tree *tree,
  3754. struct page *page, gfp_t mask)
  3755. {
  3756. u64 start = page_offset(page);
  3757. u64 end = start + PAGE_CACHE_SIZE - 1;
  3758. int ret = 1;
  3759. if (test_range_bit(tree, start, end,
  3760. EXTENT_IOBITS, 0, NULL))
  3761. ret = 0;
  3762. else {
  3763. if ((mask & GFP_NOFS) == GFP_NOFS)
  3764. mask = GFP_NOFS;
  3765. /*
  3766. * at this point we can safely clear everything except the
  3767. * locked bit and the nodatasum bit
  3768. */
  3769. ret = clear_extent_bit(tree, start, end,
  3770. ~(EXTENT_LOCKED | EXTENT_NODATASUM),
  3771. 0, 0, NULL, mask);
  3772. /* if clear_extent_bit failed for enomem reasons,
  3773. * we can't allow the release to continue.
  3774. */
  3775. if (ret < 0)
  3776. ret = 0;
  3777. else
  3778. ret = 1;
  3779. }
  3780. return ret;
  3781. }
  3782. /*
  3783. * a helper for releasepage. As long as there are no locked extents
  3784. * in the range corresponding to the page, both state records and extent
  3785. * map records are removed
  3786. */
  3787. int try_release_extent_mapping(struct extent_map_tree *map,
  3788. struct extent_io_tree *tree, struct page *page,
  3789. gfp_t mask)
  3790. {
  3791. struct extent_map *em;
  3792. u64 start = page_offset(page);
  3793. u64 end = start + PAGE_CACHE_SIZE - 1;
  3794. if ((mask & __GFP_WAIT) &&
  3795. page->mapping->host->i_size > 16 * 1024 * 1024) {
  3796. u64 len;
  3797. while (start <= end) {
  3798. len = end - start + 1;
  3799. write_lock(&map->lock);
  3800. em = lookup_extent_mapping(map, start, len);
  3801. if (!em) {
  3802. write_unlock(&map->lock);
  3803. break;
  3804. }
  3805. if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
  3806. em->start != start) {
  3807. write_unlock(&map->lock);
  3808. free_extent_map(em);
  3809. break;
  3810. }
  3811. if (!test_range_bit(tree, em->start,
  3812. extent_map_end(em) - 1,
  3813. EXTENT_LOCKED | EXTENT_WRITEBACK,
  3814. 0, NULL)) {
  3815. remove_extent_mapping(map, em);
  3816. /* once for the rb tree */
  3817. free_extent_map(em);
  3818. }
  3819. start = extent_map_end(em);
  3820. write_unlock(&map->lock);
  3821. /* once for us */
  3822. free_extent_map(em);
  3823. }
  3824. }
  3825. return try_release_extent_state(map, tree, page, mask);
  3826. }
  3827. /*
  3828. * helper function for fiemap, which doesn't want to see any holes.
  3829. * This maps until we find something past 'last'
  3830. */
  3831. static struct extent_map *get_extent_skip_holes(struct inode *inode,
  3832. u64 offset,
  3833. u64 last,
  3834. get_extent_t *get_extent)
  3835. {
  3836. u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
  3837. struct extent_map *em;
  3838. u64 len;
  3839. if (offset >= last)
  3840. return NULL;
  3841. while (1) {
  3842. len = last - offset;
  3843. if (len == 0)
  3844. break;
  3845. len = ALIGN(len, sectorsize);
  3846. em = get_extent(inode, NULL, 0, offset, len, 0);
  3847. if (IS_ERR_OR_NULL(em))
  3848. return em;
  3849. /* if this isn't a hole return it */
  3850. if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
  3851. em->block_start != EXTENT_MAP_HOLE) {
  3852. return em;
  3853. }
  3854. /* this is a hole, advance to the next extent */
  3855. offset = extent_map_end(em);
  3856. free_extent_map(em);
  3857. if (offset >= last)
  3858. break;
  3859. }
  3860. return NULL;
  3861. }
  3862. int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  3863. __u64 start, __u64 len, get_extent_t *get_extent)
  3864. {
  3865. int ret = 0;
  3866. u64 off = start;
  3867. u64 max = start + len;
  3868. u32 flags = 0;
  3869. u32 found_type;
  3870. u64 last;
  3871. u64 last_for_get_extent = 0;
  3872. u64 disko = 0;
  3873. u64 isize = i_size_read(inode);
  3874. struct btrfs_key found_key;
  3875. struct extent_map *em = NULL;
  3876. struct extent_state *cached_state = NULL;
  3877. struct btrfs_path *path;
  3878. struct btrfs_root *root = BTRFS_I(inode)->root;
  3879. int end = 0;
  3880. u64 em_start = 0;
  3881. u64 em_len = 0;
  3882. u64 em_end = 0;
  3883. if (len == 0)
  3884. return -EINVAL;
  3885. path = btrfs_alloc_path();
  3886. if (!path)
  3887. return -ENOMEM;
  3888. path->leave_spinning = 1;
  3889. start = round_down(start, BTRFS_I(inode)->root->sectorsize);
  3890. len = round_up(max, BTRFS_I(inode)->root->sectorsize) - start;
  3891. /*
  3892. * lookup the last file extent. We're not using i_size here
  3893. * because there might be preallocation past i_size
  3894. */
  3895. ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), -1,
  3896. 0);
  3897. if (ret < 0) {
  3898. btrfs_free_path(path);
  3899. return ret;
  3900. }
  3901. WARN_ON(!ret);
  3902. path->slots[0]--;
  3903. btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
  3904. found_type = found_key.type;
  3905. /* No extents, but there might be delalloc bits */
  3906. if (found_key.objectid != btrfs_ino(inode) ||
  3907. found_type != BTRFS_EXTENT_DATA_KEY) {
  3908. /* have to trust i_size as the end */
  3909. last = (u64)-1;
  3910. last_for_get_extent = isize;
  3911. } else {
  3912. /*
  3913. * remember the start of the last extent. There are a
  3914. * bunch of different factors that go into the length of the
  3915. * extent, so its much less complex to remember where it started
  3916. */
  3917. last = found_key.offset;
  3918. last_for_get_extent = last + 1;
  3919. }
  3920. btrfs_release_path(path);
  3921. /*
  3922. * we might have some extents allocated but more delalloc past those
  3923. * extents. so, we trust isize unless the start of the last extent is
  3924. * beyond isize
  3925. */
  3926. if (last < isize) {
  3927. last = (u64)-1;
  3928. last_for_get_extent = isize;
  3929. }
  3930. lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1, 0,
  3931. &cached_state);
  3932. em = get_extent_skip_holes(inode, start, last_for_get_extent,
  3933. get_extent);
  3934. if (!em)
  3935. goto out;
  3936. if (IS_ERR(em)) {
  3937. ret = PTR_ERR(em);
  3938. goto out;
  3939. }
  3940. while (!end) {
  3941. u64 offset_in_extent = 0;
  3942. /* break if the extent we found is outside the range */
  3943. if (em->start >= max || extent_map_end(em) < off)
  3944. break;
  3945. /*
  3946. * get_extent may return an extent that starts before our
  3947. * requested range. We have to make sure the ranges
  3948. * we return to fiemap always move forward and don't
  3949. * overlap, so adjust the offsets here
  3950. */
  3951. em_start = max(em->start, off);
  3952. /*
  3953. * record the offset from the start of the extent
  3954. * for adjusting the disk offset below. Only do this if the
  3955. * extent isn't compressed since our in ram offset may be past
  3956. * what we have actually allocated on disk.
  3957. */
  3958. if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
  3959. offset_in_extent = em_start - em->start;
  3960. em_end = extent_map_end(em);
  3961. em_len = em_end - em_start;
  3962. disko = 0;
  3963. flags = 0;
  3964. /*
  3965. * bump off for our next call to get_extent
  3966. */
  3967. off = extent_map_end(em);
  3968. if (off >= max)
  3969. end = 1;
  3970. if (em->block_start == EXTENT_MAP_LAST_BYTE) {
  3971. end = 1;
  3972. flags |= FIEMAP_EXTENT_LAST;
  3973. } else if (em->block_start == EXTENT_MAP_INLINE) {
  3974. flags |= (FIEMAP_EXTENT_DATA_INLINE |
  3975. FIEMAP_EXTENT_NOT_ALIGNED);
  3976. } else if (em->block_start == EXTENT_MAP_DELALLOC) {
  3977. flags |= (FIEMAP_EXTENT_DELALLOC |
  3978. FIEMAP_EXTENT_UNKNOWN);
  3979. } else if (fieinfo->fi_extents_max) {
  3980. u64 bytenr = em->block_start -
  3981. (em->start - em->orig_start);
  3982. disko = em->block_start + offset_in_extent;
  3983. /*
  3984. * As btrfs supports shared space, this information
  3985. * can be exported to userspace tools via
  3986. * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
  3987. * then we're just getting a count and we can skip the
  3988. * lookup stuff.
  3989. */
  3990. ret = btrfs_check_shared(NULL, root->fs_info,
  3991. root->objectid,
  3992. btrfs_ino(inode), bytenr);
  3993. if (ret < 0)
  3994. goto out_free;
  3995. if (ret)
  3996. flags |= FIEMAP_EXTENT_SHARED;
  3997. ret = 0;
  3998. }
  3999. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
  4000. flags |= FIEMAP_EXTENT_ENCODED;
  4001. free_extent_map(em);
  4002. em = NULL;
  4003. if ((em_start >= last) || em_len == (u64)-1 ||
  4004. (last == (u64)-1 && isize <= em_end)) {
  4005. flags |= FIEMAP_EXTENT_LAST;
  4006. end = 1;
  4007. }
  4008. /* now scan forward to see if this is really the last extent. */
  4009. em = get_extent_skip_holes(inode, off, last_for_get_extent,
  4010. get_extent);
  4011. if (IS_ERR(em)) {
  4012. ret = PTR_ERR(em);
  4013. goto out;
  4014. }
  4015. if (!em) {
  4016. flags |= FIEMAP_EXTENT_LAST;
  4017. end = 1;
  4018. }
  4019. ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
  4020. em_len, flags);
  4021. if (ret)
  4022. goto out_free;
  4023. }
  4024. out_free:
  4025. free_extent_map(em);
  4026. out:
  4027. btrfs_free_path(path);
  4028. unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
  4029. &cached_state, GFP_NOFS);
  4030. return ret;
  4031. }
  4032. static void __free_extent_buffer(struct extent_buffer *eb)
  4033. {
  4034. btrfs_leak_debug_del(&eb->leak_list);
  4035. kmem_cache_free(extent_buffer_cache, eb);
  4036. }
  4037. int extent_buffer_under_io(struct extent_buffer *eb)
  4038. {
  4039. return (atomic_read(&eb->io_pages) ||
  4040. test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
  4041. test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
  4042. }
  4043. /*
  4044. * Helper for releasing extent buffer page.
  4045. */
  4046. static void btrfs_release_extent_buffer_page(struct extent_buffer *eb)
  4047. {
  4048. unsigned long index;
  4049. struct page *page;
  4050. int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
  4051. BUG_ON(extent_buffer_under_io(eb));
  4052. index = num_extent_pages(eb->start, eb->len);
  4053. if (index == 0)
  4054. return;
  4055. do {
  4056. index--;
  4057. page = eb->pages[index];
  4058. if (page && mapped) {
  4059. spin_lock(&page->mapping->private_lock);
  4060. /*
  4061. * We do this since we'll remove the pages after we've
  4062. * removed the eb from the radix tree, so we could race
  4063. * and have this page now attached to the new eb. So
  4064. * only clear page_private if it's still connected to
  4065. * this eb.
  4066. */
  4067. if (PagePrivate(page) &&
  4068. page->private == (unsigned long)eb) {
  4069. BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
  4070. BUG_ON(PageDirty(page));
  4071. BUG_ON(PageWriteback(page));
  4072. /*
  4073. * We need to make sure we haven't be attached
  4074. * to a new eb.
  4075. */
  4076. ClearPagePrivate(page);
  4077. set_page_private(page, 0);
  4078. /* One for the page private */
  4079. page_cache_release(page);
  4080. }
  4081. spin_unlock(&page->mapping->private_lock);
  4082. }
  4083. if (page) {
  4084. /* One for when we alloced the page */
  4085. page_cache_release(page);
  4086. }
  4087. } while (index != 0);
  4088. }
  4089. /*
  4090. * Helper for releasing the extent buffer.
  4091. */
  4092. static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
  4093. {
  4094. btrfs_release_extent_buffer_page(eb);
  4095. __free_extent_buffer(eb);
  4096. }
  4097. static struct extent_buffer *
  4098. __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
  4099. unsigned long len)
  4100. {
  4101. struct extent_buffer *eb = NULL;
  4102. eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS);
  4103. if (eb == NULL)
  4104. return NULL;
  4105. eb->start = start;
  4106. eb->len = len;
  4107. eb->fs_info = fs_info;
  4108. eb->bflags = 0;
  4109. rwlock_init(&eb->lock);
  4110. atomic_set(&eb->write_locks, 0);
  4111. atomic_set(&eb->read_locks, 0);
  4112. atomic_set(&eb->blocking_readers, 0);
  4113. atomic_set(&eb->blocking_writers, 0);
  4114. atomic_set(&eb->spinning_readers, 0);
  4115. atomic_set(&eb->spinning_writers, 0);
  4116. eb->lock_nested = 0;
  4117. init_waitqueue_head(&eb->write_lock_wq);
  4118. init_waitqueue_head(&eb->read_lock_wq);
  4119. btrfs_leak_debug_add(&eb->leak_list, &buffers);
  4120. spin_lock_init(&eb->refs_lock);
  4121. atomic_set(&eb->refs, 1);
  4122. atomic_set(&eb->io_pages, 0);
  4123. /*
  4124. * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
  4125. */
  4126. BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
  4127. > MAX_INLINE_EXTENT_BUFFER_SIZE);
  4128. BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
  4129. return eb;
  4130. }
  4131. struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
  4132. {
  4133. unsigned long i;
  4134. struct page *p;
  4135. struct extent_buffer *new;
  4136. unsigned long num_pages = num_extent_pages(src->start, src->len);
  4137. new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
  4138. if (new == NULL)
  4139. return NULL;
  4140. for (i = 0; i < num_pages; i++) {
  4141. p = alloc_page(GFP_NOFS);
  4142. if (!p) {
  4143. btrfs_release_extent_buffer(new);
  4144. return NULL;
  4145. }
  4146. attach_extent_buffer_page(new, p);
  4147. WARN_ON(PageDirty(p));
  4148. SetPageUptodate(p);
  4149. new->pages[i] = p;
  4150. }
  4151. copy_extent_buffer(new, src, 0, 0, src->len);
  4152. set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
  4153. set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
  4154. return new;
  4155. }
  4156. struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
  4157. u64 start)
  4158. {
  4159. struct extent_buffer *eb;
  4160. unsigned long len;
  4161. unsigned long num_pages;
  4162. unsigned long i;
  4163. if (!fs_info) {
  4164. /*
  4165. * Called only from tests that don't always have a fs_info
  4166. * available, but we know that nodesize is 4096
  4167. */
  4168. len = 4096;
  4169. } else {
  4170. len = fs_info->tree_root->nodesize;
  4171. }
  4172. num_pages = num_extent_pages(0, len);
  4173. eb = __alloc_extent_buffer(fs_info, start, len);
  4174. if (!eb)
  4175. return NULL;
  4176. for (i = 0; i < num_pages; i++) {
  4177. eb->pages[i] = alloc_page(GFP_NOFS);
  4178. if (!eb->pages[i])
  4179. goto err;
  4180. }
  4181. set_extent_buffer_uptodate(eb);
  4182. btrfs_set_header_nritems(eb, 0);
  4183. set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
  4184. return eb;
  4185. err:
  4186. for (; i > 0; i--)
  4187. __free_page(eb->pages[i - 1]);
  4188. __free_extent_buffer(eb);
  4189. return NULL;
  4190. }
  4191. static void check_buffer_tree_ref(struct extent_buffer *eb)
  4192. {
  4193. int refs;
  4194. /* the ref bit is tricky. We have to make sure it is set
  4195. * if we have the buffer dirty. Otherwise the
  4196. * code to free a buffer can end up dropping a dirty
  4197. * page
  4198. *
  4199. * Once the ref bit is set, it won't go away while the
  4200. * buffer is dirty or in writeback, and it also won't
  4201. * go away while we have the reference count on the
  4202. * eb bumped.
  4203. *
  4204. * We can't just set the ref bit without bumping the
  4205. * ref on the eb because free_extent_buffer might
  4206. * see the ref bit and try to clear it. If this happens
  4207. * free_extent_buffer might end up dropping our original
  4208. * ref by mistake and freeing the page before we are able
  4209. * to add one more ref.
  4210. *
  4211. * So bump the ref count first, then set the bit. If someone
  4212. * beat us to it, drop the ref we added.
  4213. */
  4214. refs = atomic_read(&eb->refs);
  4215. if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  4216. return;
  4217. spin_lock(&eb->refs_lock);
  4218. if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  4219. atomic_inc(&eb->refs);
  4220. spin_unlock(&eb->refs_lock);
  4221. }
  4222. static void mark_extent_buffer_accessed(struct extent_buffer *eb,
  4223. struct page *accessed)
  4224. {
  4225. unsigned long num_pages, i;
  4226. check_buffer_tree_ref(eb);
  4227. num_pages = num_extent_pages(eb->start, eb->len);
  4228. for (i = 0; i < num_pages; i++) {
  4229. struct page *p = eb->pages[i];
  4230. if (p != accessed)
  4231. mark_page_accessed(p);
  4232. }
  4233. }
  4234. struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
  4235. u64 start)
  4236. {
  4237. struct extent_buffer *eb;
  4238. rcu_read_lock();
  4239. eb = radix_tree_lookup(&fs_info->buffer_radix,
  4240. start >> PAGE_CACHE_SHIFT);
  4241. if (eb && atomic_inc_not_zero(&eb->refs)) {
  4242. rcu_read_unlock();
  4243. mark_extent_buffer_accessed(eb, NULL);
  4244. return eb;
  4245. }
  4246. rcu_read_unlock();
  4247. return NULL;
  4248. }
  4249. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  4250. struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
  4251. u64 start)
  4252. {
  4253. struct extent_buffer *eb, *exists = NULL;
  4254. int ret;
  4255. eb = find_extent_buffer(fs_info, start);
  4256. if (eb)
  4257. return eb;
  4258. eb = alloc_dummy_extent_buffer(fs_info, start);
  4259. if (!eb)
  4260. return NULL;
  4261. eb->fs_info = fs_info;
  4262. again:
  4263. ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
  4264. if (ret)
  4265. goto free_eb;
  4266. spin_lock(&fs_info->buffer_lock);
  4267. ret = radix_tree_insert(&fs_info->buffer_radix,
  4268. start >> PAGE_CACHE_SHIFT, eb);
  4269. spin_unlock(&fs_info->buffer_lock);
  4270. radix_tree_preload_end();
  4271. if (ret == -EEXIST) {
  4272. exists = find_extent_buffer(fs_info, start);
  4273. if (exists)
  4274. goto free_eb;
  4275. else
  4276. goto again;
  4277. }
  4278. check_buffer_tree_ref(eb);
  4279. set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
  4280. /*
  4281. * We will free dummy extent buffer's if they come into
  4282. * free_extent_buffer with a ref count of 2, but if we are using this we
  4283. * want the buffers to stay in memory until we're done with them, so
  4284. * bump the ref count again.
  4285. */
  4286. atomic_inc(&eb->refs);
  4287. return eb;
  4288. free_eb:
  4289. btrfs_release_extent_buffer(eb);
  4290. return exists;
  4291. }
  4292. #endif
  4293. struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
  4294. u64 start)
  4295. {
  4296. unsigned long len = fs_info->tree_root->nodesize;
  4297. unsigned long num_pages = num_extent_pages(start, len);
  4298. unsigned long i;
  4299. unsigned long index = start >> PAGE_CACHE_SHIFT;
  4300. struct extent_buffer *eb;
  4301. struct extent_buffer *exists = NULL;
  4302. struct page *p;
  4303. struct address_space *mapping = fs_info->btree_inode->i_mapping;
  4304. int uptodate = 1;
  4305. int ret;
  4306. eb = find_extent_buffer(fs_info, start);
  4307. if (eb)
  4308. return eb;
  4309. eb = __alloc_extent_buffer(fs_info, start, len);
  4310. if (!eb)
  4311. return NULL;
  4312. for (i = 0; i < num_pages; i++, index++) {
  4313. p = find_or_create_page(mapping, index, GFP_NOFS);
  4314. if (!p)
  4315. goto free_eb;
  4316. spin_lock(&mapping->private_lock);
  4317. if (PagePrivate(p)) {
  4318. /*
  4319. * We could have already allocated an eb for this page
  4320. * and attached one so lets see if we can get a ref on
  4321. * the existing eb, and if we can we know it's good and
  4322. * we can just return that one, else we know we can just
  4323. * overwrite page->private.
  4324. */
  4325. exists = (struct extent_buffer *)p->private;
  4326. if (atomic_inc_not_zero(&exists->refs)) {
  4327. spin_unlock(&mapping->private_lock);
  4328. unlock_page(p);
  4329. page_cache_release(p);
  4330. mark_extent_buffer_accessed(exists, p);
  4331. goto free_eb;
  4332. }
  4333. /*
  4334. * Do this so attach doesn't complain and we need to
  4335. * drop the ref the old guy had.
  4336. */
  4337. ClearPagePrivate(p);
  4338. WARN_ON(PageDirty(p));
  4339. page_cache_release(p);
  4340. }
  4341. attach_extent_buffer_page(eb, p);
  4342. spin_unlock(&mapping->private_lock);
  4343. WARN_ON(PageDirty(p));
  4344. eb->pages[i] = p;
  4345. if (!PageUptodate(p))
  4346. uptodate = 0;
  4347. /*
  4348. * see below about how we avoid a nasty race with release page
  4349. * and why we unlock later
  4350. */
  4351. }
  4352. if (uptodate)
  4353. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4354. again:
  4355. ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
  4356. if (ret)
  4357. goto free_eb;
  4358. spin_lock(&fs_info->buffer_lock);
  4359. ret = radix_tree_insert(&fs_info->buffer_radix,
  4360. start >> PAGE_CACHE_SHIFT, eb);
  4361. spin_unlock(&fs_info->buffer_lock);
  4362. radix_tree_preload_end();
  4363. if (ret == -EEXIST) {
  4364. exists = find_extent_buffer(fs_info, start);
  4365. if (exists)
  4366. goto free_eb;
  4367. else
  4368. goto again;
  4369. }
  4370. /* add one reference for the tree */
  4371. check_buffer_tree_ref(eb);
  4372. set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
  4373. /*
  4374. * there is a race where release page may have
  4375. * tried to find this extent buffer in the radix
  4376. * but failed. It will tell the VM it is safe to
  4377. * reclaim the, and it will clear the page private bit.
  4378. * We must make sure to set the page private bit properly
  4379. * after the extent buffer is in the radix tree so
  4380. * it doesn't get lost
  4381. */
  4382. SetPageChecked(eb->pages[0]);
  4383. for (i = 1; i < num_pages; i++) {
  4384. p = eb->pages[i];
  4385. ClearPageChecked(p);
  4386. unlock_page(p);
  4387. }
  4388. unlock_page(eb->pages[0]);
  4389. return eb;
  4390. free_eb:
  4391. for (i = 0; i < num_pages; i++) {
  4392. if (eb->pages[i])
  4393. unlock_page(eb->pages[i]);
  4394. }
  4395. WARN_ON(!atomic_dec_and_test(&eb->refs));
  4396. btrfs_release_extent_buffer(eb);
  4397. return exists;
  4398. }
  4399. static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
  4400. {
  4401. struct extent_buffer *eb =
  4402. container_of(head, struct extent_buffer, rcu_head);
  4403. __free_extent_buffer(eb);
  4404. }
  4405. /* Expects to have eb->eb_lock already held */
  4406. static int release_extent_buffer(struct extent_buffer *eb)
  4407. {
  4408. WARN_ON(atomic_read(&eb->refs) == 0);
  4409. if (atomic_dec_and_test(&eb->refs)) {
  4410. if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
  4411. struct btrfs_fs_info *fs_info = eb->fs_info;
  4412. spin_unlock(&eb->refs_lock);
  4413. spin_lock(&fs_info->buffer_lock);
  4414. radix_tree_delete(&fs_info->buffer_radix,
  4415. eb->start >> PAGE_CACHE_SHIFT);
  4416. spin_unlock(&fs_info->buffer_lock);
  4417. } else {
  4418. spin_unlock(&eb->refs_lock);
  4419. }
  4420. /* Should be safe to release our pages at this point */
  4421. btrfs_release_extent_buffer_page(eb);
  4422. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  4423. if (unlikely(test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))) {
  4424. __free_extent_buffer(eb);
  4425. return 1;
  4426. }
  4427. #endif
  4428. call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
  4429. return 1;
  4430. }
  4431. spin_unlock(&eb->refs_lock);
  4432. return 0;
  4433. }
  4434. void free_extent_buffer(struct extent_buffer *eb)
  4435. {
  4436. int refs;
  4437. int old;
  4438. if (!eb)
  4439. return;
  4440. while (1) {
  4441. refs = atomic_read(&eb->refs);
  4442. if (refs <= 3)
  4443. break;
  4444. old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
  4445. if (old == refs)
  4446. return;
  4447. }
  4448. spin_lock(&eb->refs_lock);
  4449. if (atomic_read(&eb->refs) == 2 &&
  4450. test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
  4451. atomic_dec(&eb->refs);
  4452. if (atomic_read(&eb->refs) == 2 &&
  4453. test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
  4454. !extent_buffer_under_io(eb) &&
  4455. test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  4456. atomic_dec(&eb->refs);
  4457. /*
  4458. * I know this is terrible, but it's temporary until we stop tracking
  4459. * the uptodate bits and such for the extent buffers.
  4460. */
  4461. release_extent_buffer(eb);
  4462. }
  4463. void free_extent_buffer_stale(struct extent_buffer *eb)
  4464. {
  4465. if (!eb)
  4466. return;
  4467. spin_lock(&eb->refs_lock);
  4468. set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
  4469. if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
  4470. test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  4471. atomic_dec(&eb->refs);
  4472. release_extent_buffer(eb);
  4473. }
  4474. void clear_extent_buffer_dirty(struct extent_buffer *eb)
  4475. {
  4476. unsigned long i;
  4477. unsigned long num_pages;
  4478. struct page *page;
  4479. num_pages = num_extent_pages(eb->start, eb->len);
  4480. for (i = 0; i < num_pages; i++) {
  4481. page = eb->pages[i];
  4482. if (!PageDirty(page))
  4483. continue;
  4484. lock_page(page);
  4485. WARN_ON(!PagePrivate(page));
  4486. clear_page_dirty_for_io(page);
  4487. spin_lock_irq(&page->mapping->tree_lock);
  4488. if (!PageDirty(page)) {
  4489. radix_tree_tag_clear(&page->mapping->page_tree,
  4490. page_index(page),
  4491. PAGECACHE_TAG_DIRTY);
  4492. }
  4493. spin_unlock_irq(&page->mapping->tree_lock);
  4494. ClearPageError(page);
  4495. unlock_page(page);
  4496. }
  4497. WARN_ON(atomic_read(&eb->refs) == 0);
  4498. }
  4499. int set_extent_buffer_dirty(struct extent_buffer *eb)
  4500. {
  4501. unsigned long i;
  4502. unsigned long num_pages;
  4503. int was_dirty = 0;
  4504. check_buffer_tree_ref(eb);
  4505. was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
  4506. num_pages = num_extent_pages(eb->start, eb->len);
  4507. WARN_ON(atomic_read(&eb->refs) == 0);
  4508. WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
  4509. for (i = 0; i < num_pages; i++)
  4510. set_page_dirty(eb->pages[i]);
  4511. return was_dirty;
  4512. }
  4513. int clear_extent_buffer_uptodate(struct extent_buffer *eb)
  4514. {
  4515. unsigned long i;
  4516. struct page *page;
  4517. unsigned long num_pages;
  4518. clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4519. num_pages = num_extent_pages(eb->start, eb->len);
  4520. for (i = 0; i < num_pages; i++) {
  4521. page = eb->pages[i];
  4522. if (page)
  4523. ClearPageUptodate(page);
  4524. }
  4525. return 0;
  4526. }
  4527. int set_extent_buffer_uptodate(struct extent_buffer *eb)
  4528. {
  4529. unsigned long i;
  4530. struct page *page;
  4531. unsigned long num_pages;
  4532. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4533. num_pages = num_extent_pages(eb->start, eb->len);
  4534. for (i = 0; i < num_pages; i++) {
  4535. page = eb->pages[i];
  4536. SetPageUptodate(page);
  4537. }
  4538. return 0;
  4539. }
  4540. int extent_buffer_uptodate(struct extent_buffer *eb)
  4541. {
  4542. return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4543. }
  4544. int read_extent_buffer_pages(struct extent_io_tree *tree,
  4545. struct extent_buffer *eb, u64 start, int wait,
  4546. get_extent_t *get_extent, int mirror_num)
  4547. {
  4548. unsigned long i;
  4549. unsigned long start_i;
  4550. struct page *page;
  4551. int err;
  4552. int ret = 0;
  4553. int locked_pages = 0;
  4554. int all_uptodate = 1;
  4555. unsigned long num_pages;
  4556. unsigned long num_reads = 0;
  4557. struct bio *bio = NULL;
  4558. unsigned long bio_flags = 0;
  4559. if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
  4560. return 0;
  4561. if (start) {
  4562. WARN_ON(start < eb->start);
  4563. start_i = (start >> PAGE_CACHE_SHIFT) -
  4564. (eb->start >> PAGE_CACHE_SHIFT);
  4565. } else {
  4566. start_i = 0;
  4567. }
  4568. num_pages = num_extent_pages(eb->start, eb->len);
  4569. for (i = start_i; i < num_pages; i++) {
  4570. page = eb->pages[i];
  4571. if (wait == WAIT_NONE) {
  4572. if (!trylock_page(page))
  4573. goto unlock_exit;
  4574. } else {
  4575. lock_page(page);
  4576. }
  4577. locked_pages++;
  4578. if (!PageUptodate(page)) {
  4579. num_reads++;
  4580. all_uptodate = 0;
  4581. }
  4582. }
  4583. if (all_uptodate) {
  4584. if (start_i == 0)
  4585. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4586. goto unlock_exit;
  4587. }
  4588. clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
  4589. eb->read_mirror = 0;
  4590. atomic_set(&eb->io_pages, num_reads);
  4591. for (i = start_i; i < num_pages; i++) {
  4592. page = eb->pages[i];
  4593. if (!PageUptodate(page)) {
  4594. ClearPageError(page);
  4595. err = __extent_read_full_page(tree, page,
  4596. get_extent, &bio,
  4597. mirror_num, &bio_flags,
  4598. READ | REQ_META);
  4599. if (err)
  4600. ret = err;
  4601. } else {
  4602. unlock_page(page);
  4603. }
  4604. }
  4605. if (bio) {
  4606. err = submit_one_bio(READ | REQ_META, bio, mirror_num,
  4607. bio_flags);
  4608. if (err)
  4609. return err;
  4610. }
  4611. if (ret || wait != WAIT_COMPLETE)
  4612. return ret;
  4613. for (i = start_i; i < num_pages; i++) {
  4614. page = eb->pages[i];
  4615. wait_on_page_locked(page);
  4616. if (!PageUptodate(page))
  4617. ret = -EIO;
  4618. }
  4619. return ret;
  4620. unlock_exit:
  4621. i = start_i;
  4622. while (locked_pages > 0) {
  4623. page = eb->pages[i];
  4624. i++;
  4625. unlock_page(page);
  4626. locked_pages--;
  4627. }
  4628. return ret;
  4629. }
  4630. void read_extent_buffer(struct extent_buffer *eb, void *dstv,
  4631. unsigned long start,
  4632. unsigned long len)
  4633. {
  4634. size_t cur;
  4635. size_t offset;
  4636. struct page *page;
  4637. char *kaddr;
  4638. char *dst = (char *)dstv;
  4639. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  4640. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  4641. WARN_ON(start > eb->len);
  4642. WARN_ON(start + len > eb->start + eb->len);
  4643. offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1);
  4644. while (len > 0) {
  4645. page = eb->pages[i];
  4646. cur = min(len, (PAGE_CACHE_SIZE - offset));
  4647. kaddr = page_address(page);
  4648. memcpy(dst, kaddr + offset, cur);
  4649. dst += cur;
  4650. len -= cur;
  4651. offset = 0;
  4652. i++;
  4653. }
  4654. }
  4655. int read_extent_buffer_to_user(struct extent_buffer *eb, void __user *dstv,
  4656. unsigned long start,
  4657. unsigned long len)
  4658. {
  4659. size_t cur;
  4660. size_t offset;
  4661. struct page *page;
  4662. char *kaddr;
  4663. char __user *dst = (char __user *)dstv;
  4664. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  4665. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  4666. int ret = 0;
  4667. WARN_ON(start > eb->len);
  4668. WARN_ON(start + len > eb->start + eb->len);
  4669. offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1);
  4670. while (len > 0) {
  4671. page = eb->pages[i];
  4672. cur = min(len, (PAGE_CACHE_SIZE - offset));
  4673. kaddr = page_address(page);
  4674. if (copy_to_user(dst, kaddr + offset, cur)) {
  4675. ret = -EFAULT;
  4676. break;
  4677. }
  4678. dst += cur;
  4679. len -= cur;
  4680. offset = 0;
  4681. i++;
  4682. }
  4683. return ret;
  4684. }
  4685. int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
  4686. unsigned long min_len, char **map,
  4687. unsigned long *map_start,
  4688. unsigned long *map_len)
  4689. {
  4690. size_t offset = start & (PAGE_CACHE_SIZE - 1);
  4691. char *kaddr;
  4692. struct page *p;
  4693. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  4694. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  4695. unsigned long end_i = (start_offset + start + min_len - 1) >>
  4696. PAGE_CACHE_SHIFT;
  4697. if (i != end_i)
  4698. return -EINVAL;
  4699. if (i == 0) {
  4700. offset = start_offset;
  4701. *map_start = 0;
  4702. } else {
  4703. offset = 0;
  4704. *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
  4705. }
  4706. if (start + min_len > eb->len) {
  4707. WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
  4708. "wanted %lu %lu\n",
  4709. eb->start, eb->len, start, min_len);
  4710. return -EINVAL;
  4711. }
  4712. p = eb->pages[i];
  4713. kaddr = page_address(p);
  4714. *map = kaddr + offset;
  4715. *map_len = PAGE_CACHE_SIZE - offset;
  4716. return 0;
  4717. }
  4718. int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
  4719. unsigned long start,
  4720. unsigned long len)
  4721. {
  4722. size_t cur;
  4723. size_t offset;
  4724. struct page *page;
  4725. char *kaddr;
  4726. char *ptr = (char *)ptrv;
  4727. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  4728. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  4729. int ret = 0;
  4730. WARN_ON(start > eb->len);
  4731. WARN_ON(start + len > eb->start + eb->len);
  4732. offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1);
  4733. while (len > 0) {
  4734. page = eb->pages[i];
  4735. cur = min(len, (PAGE_CACHE_SIZE - offset));
  4736. kaddr = page_address(page);
  4737. ret = memcmp(ptr, kaddr + offset, cur);
  4738. if (ret)
  4739. break;
  4740. ptr += cur;
  4741. len -= cur;
  4742. offset = 0;
  4743. i++;
  4744. }
  4745. return ret;
  4746. }
  4747. void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
  4748. unsigned long start, unsigned long len)
  4749. {
  4750. size_t cur;
  4751. size_t offset;
  4752. struct page *page;
  4753. char *kaddr;
  4754. char *src = (char *)srcv;
  4755. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  4756. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  4757. WARN_ON(start > eb->len);
  4758. WARN_ON(start + len > eb->start + eb->len);
  4759. offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1);
  4760. while (len > 0) {
  4761. page = eb->pages[i];
  4762. WARN_ON(!PageUptodate(page));
  4763. cur = min(len, PAGE_CACHE_SIZE - offset);
  4764. kaddr = page_address(page);
  4765. memcpy(kaddr + offset, src, cur);
  4766. src += cur;
  4767. len -= cur;
  4768. offset = 0;
  4769. i++;
  4770. }
  4771. }
  4772. void memset_extent_buffer(struct extent_buffer *eb, char c,
  4773. unsigned long start, unsigned long len)
  4774. {
  4775. size_t cur;
  4776. size_t offset;
  4777. struct page *page;
  4778. char *kaddr;
  4779. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  4780. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  4781. WARN_ON(start > eb->len);
  4782. WARN_ON(start + len > eb->start + eb->len);
  4783. offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1);
  4784. while (len > 0) {
  4785. page = eb->pages[i];
  4786. WARN_ON(!PageUptodate(page));
  4787. cur = min(len, PAGE_CACHE_SIZE - offset);
  4788. kaddr = page_address(page);
  4789. memset(kaddr + offset, c, cur);
  4790. len -= cur;
  4791. offset = 0;
  4792. i++;
  4793. }
  4794. }
  4795. void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
  4796. unsigned long dst_offset, unsigned long src_offset,
  4797. unsigned long len)
  4798. {
  4799. u64 dst_len = dst->len;
  4800. size_t cur;
  4801. size_t offset;
  4802. struct page *page;
  4803. char *kaddr;
  4804. size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
  4805. unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
  4806. WARN_ON(src->len != dst_len);
  4807. offset = (start_offset + dst_offset) &
  4808. (PAGE_CACHE_SIZE - 1);
  4809. while (len > 0) {
  4810. page = dst->pages[i];
  4811. WARN_ON(!PageUptodate(page));
  4812. cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
  4813. kaddr = page_address(page);
  4814. read_extent_buffer(src, kaddr + offset, src_offset, cur);
  4815. src_offset += cur;
  4816. len -= cur;
  4817. offset = 0;
  4818. i++;
  4819. }
  4820. }
  4821. static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
  4822. {
  4823. unsigned long distance = (src > dst) ? src - dst : dst - src;
  4824. return distance < len;
  4825. }
  4826. static void copy_pages(struct page *dst_page, struct page *src_page,
  4827. unsigned long dst_off, unsigned long src_off,
  4828. unsigned long len)
  4829. {
  4830. char *dst_kaddr = page_address(dst_page);
  4831. char *src_kaddr;
  4832. int must_memmove = 0;
  4833. if (dst_page != src_page) {
  4834. src_kaddr = page_address(src_page);
  4835. } else {
  4836. src_kaddr = dst_kaddr;
  4837. if (areas_overlap(src_off, dst_off, len))
  4838. must_memmove = 1;
  4839. }
  4840. if (must_memmove)
  4841. memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
  4842. else
  4843. memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
  4844. }
  4845. void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
  4846. unsigned long src_offset, unsigned long len)
  4847. {
  4848. size_t cur;
  4849. size_t dst_off_in_page;
  4850. size_t src_off_in_page;
  4851. size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
  4852. unsigned long dst_i;
  4853. unsigned long src_i;
  4854. if (src_offset + len > dst->len) {
  4855. printk(KERN_ERR "BTRFS: memmove bogus src_offset %lu move "
  4856. "len %lu dst len %lu\n", src_offset, len, dst->len);
  4857. BUG_ON(1);
  4858. }
  4859. if (dst_offset + len > dst->len) {
  4860. printk(KERN_ERR "BTRFS: memmove bogus dst_offset %lu move "
  4861. "len %lu dst len %lu\n", dst_offset, len, dst->len);
  4862. BUG_ON(1);
  4863. }
  4864. while (len > 0) {
  4865. dst_off_in_page = (start_offset + dst_offset) &
  4866. (PAGE_CACHE_SIZE - 1);
  4867. src_off_in_page = (start_offset + src_offset) &
  4868. (PAGE_CACHE_SIZE - 1);
  4869. dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
  4870. src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
  4871. cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
  4872. src_off_in_page));
  4873. cur = min_t(unsigned long, cur,
  4874. (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
  4875. copy_pages(dst->pages[dst_i], dst->pages[src_i],
  4876. dst_off_in_page, src_off_in_page, cur);
  4877. src_offset += cur;
  4878. dst_offset += cur;
  4879. len -= cur;
  4880. }
  4881. }
  4882. void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
  4883. unsigned long src_offset, unsigned long len)
  4884. {
  4885. size_t cur;
  4886. size_t dst_off_in_page;
  4887. size_t src_off_in_page;
  4888. unsigned long dst_end = dst_offset + len - 1;
  4889. unsigned long src_end = src_offset + len - 1;
  4890. size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
  4891. unsigned long dst_i;
  4892. unsigned long src_i;
  4893. if (src_offset + len > dst->len) {
  4894. printk(KERN_ERR "BTRFS: memmove bogus src_offset %lu move "
  4895. "len %lu len %lu\n", src_offset, len, dst->len);
  4896. BUG_ON(1);
  4897. }
  4898. if (dst_offset + len > dst->len) {
  4899. printk(KERN_ERR "BTRFS: memmove bogus dst_offset %lu move "
  4900. "len %lu len %lu\n", dst_offset, len, dst->len);
  4901. BUG_ON(1);
  4902. }
  4903. if (dst_offset < src_offset) {
  4904. memcpy_extent_buffer(dst, dst_offset, src_offset, len);
  4905. return;
  4906. }
  4907. while (len > 0) {
  4908. dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
  4909. src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
  4910. dst_off_in_page = (start_offset + dst_end) &
  4911. (PAGE_CACHE_SIZE - 1);
  4912. src_off_in_page = (start_offset + src_end) &
  4913. (PAGE_CACHE_SIZE - 1);
  4914. cur = min_t(unsigned long, len, src_off_in_page + 1);
  4915. cur = min(cur, dst_off_in_page + 1);
  4916. copy_pages(dst->pages[dst_i], dst->pages[src_i],
  4917. dst_off_in_page - cur + 1,
  4918. src_off_in_page - cur + 1, cur);
  4919. dst_end -= cur;
  4920. src_end -= cur;
  4921. len -= cur;
  4922. }
  4923. }
  4924. int try_release_extent_buffer(struct page *page)
  4925. {
  4926. struct extent_buffer *eb;
  4927. /*
  4928. * We need to make sure noboody is attaching this page to an eb right
  4929. * now.
  4930. */
  4931. spin_lock(&page->mapping->private_lock);
  4932. if (!PagePrivate(page)) {
  4933. spin_unlock(&page->mapping->private_lock);
  4934. return 1;
  4935. }
  4936. eb = (struct extent_buffer *)page->private;
  4937. BUG_ON(!eb);
  4938. /*
  4939. * This is a little awful but should be ok, we need to make sure that
  4940. * the eb doesn't disappear out from under us while we're looking at
  4941. * this page.
  4942. */
  4943. spin_lock(&eb->refs_lock);
  4944. if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
  4945. spin_unlock(&eb->refs_lock);
  4946. spin_unlock(&page->mapping->private_lock);
  4947. return 0;
  4948. }
  4949. spin_unlock(&page->mapping->private_lock);
  4950. /*
  4951. * If tree ref isn't set then we know the ref on this eb is a real ref,
  4952. * so just return, this page will likely be freed soon anyway.
  4953. */
  4954. if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
  4955. spin_unlock(&eb->refs_lock);
  4956. return 0;
  4957. }
  4958. return release_extent_buffer(eb);
  4959. }