extent_io.c 122 KB

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