extent_io.c 148 KB

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