extent_io.c 151 KB

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