extent_io.c 120 KB

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