extent_io.c 124 KB

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