extent_io.c 149 KB

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