extent_io.c 148 KB

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