extent_io.c 120 KB

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