extent_io.c 150 KB

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