extent_io.c 120 KB

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