extent_io.c 121 KB

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