extent_io.c 149 KB

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