extent_io.c 152 KB

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