extent_io.c 146 KB

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