extent_io.c 146 KB

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