extent_io.c 122 KB

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