extent_io.c 118 KB

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