extent_io.c 124 KB

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