extent_io.c 124 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034
  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 = page_offset(page);
  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 = page_offset(page);
  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. if (did_repair) {
  1711. ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
  1712. rec->start + rec->len - 1,
  1713. EXTENT_DAMAGED, GFP_NOFS);
  1714. if (ret && !err)
  1715. err = ret;
  1716. }
  1717. kfree(rec);
  1718. return err;
  1719. }
  1720. static void repair_io_failure_callback(struct bio *bio, int err)
  1721. {
  1722. complete(bio->bi_private);
  1723. }
  1724. /*
  1725. * this bypasses the standard btrfs submit functions deliberately, as
  1726. * the standard behavior is to write all copies in a raid setup. here we only
  1727. * want to write the one bad copy. so we do the mapping for ourselves and issue
  1728. * submit_bio directly.
  1729. * to avoid any synchronization issues, wait for the data after writing, which
  1730. * actually prevents the read that triggered the error from finishing.
  1731. * currently, there can be no more than two copies of every data bit. thus,
  1732. * exactly one rewrite is required.
  1733. */
  1734. int repair_io_failure(struct btrfs_fs_info *fs_info, u64 start,
  1735. u64 length, u64 logical, struct page *page,
  1736. int mirror_num)
  1737. {
  1738. struct bio *bio;
  1739. struct btrfs_device *dev;
  1740. DECLARE_COMPLETION_ONSTACK(compl);
  1741. u64 map_length = 0;
  1742. u64 sector;
  1743. struct btrfs_bio *bbio = NULL;
  1744. int ret;
  1745. BUG_ON(!mirror_num);
  1746. bio = bio_alloc(GFP_NOFS, 1);
  1747. if (!bio)
  1748. return -EIO;
  1749. bio->bi_private = &compl;
  1750. bio->bi_end_io = repair_io_failure_callback;
  1751. bio->bi_size = 0;
  1752. map_length = length;
  1753. ret = btrfs_map_block(fs_info, WRITE, logical,
  1754. &map_length, &bbio, mirror_num);
  1755. if (ret) {
  1756. bio_put(bio);
  1757. return -EIO;
  1758. }
  1759. BUG_ON(mirror_num != bbio->mirror_num);
  1760. sector = bbio->stripes[mirror_num-1].physical >> 9;
  1761. bio->bi_sector = sector;
  1762. dev = bbio->stripes[mirror_num-1].dev;
  1763. kfree(bbio);
  1764. if (!dev || !dev->bdev || !dev->writeable) {
  1765. bio_put(bio);
  1766. return -EIO;
  1767. }
  1768. bio->bi_bdev = dev->bdev;
  1769. bio_add_page(bio, page, length, start - page_offset(page));
  1770. btrfsic_submit_bio(WRITE_SYNC, bio);
  1771. wait_for_completion(&compl);
  1772. if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
  1773. /* try to remap that extent elsewhere? */
  1774. bio_put(bio);
  1775. btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
  1776. return -EIO;
  1777. }
  1778. printk_ratelimited_in_rcu(KERN_INFO "btrfs read error corrected: ino %lu off %llu "
  1779. "(dev %s sector %llu)\n", page->mapping->host->i_ino,
  1780. start, rcu_str_deref(dev->name), sector);
  1781. bio_put(bio);
  1782. return 0;
  1783. }
  1784. int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
  1785. int mirror_num)
  1786. {
  1787. u64 start = eb->start;
  1788. unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
  1789. int ret = 0;
  1790. for (i = 0; i < num_pages; i++) {
  1791. struct page *p = extent_buffer_page(eb, i);
  1792. ret = repair_io_failure(root->fs_info, start, PAGE_CACHE_SIZE,
  1793. start, p, mirror_num);
  1794. if (ret)
  1795. break;
  1796. start += PAGE_CACHE_SIZE;
  1797. }
  1798. return ret;
  1799. }
  1800. /*
  1801. * each time an IO finishes, we do a fast check in the IO failure tree
  1802. * to see if we need to process or clean up an io_failure_record
  1803. */
  1804. static int clean_io_failure(u64 start, struct page *page)
  1805. {
  1806. u64 private;
  1807. u64 private_failure;
  1808. struct io_failure_record *failrec;
  1809. struct btrfs_fs_info *fs_info;
  1810. struct extent_state *state;
  1811. int num_copies;
  1812. int did_repair = 0;
  1813. int ret;
  1814. struct inode *inode = page->mapping->host;
  1815. private = 0;
  1816. ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
  1817. (u64)-1, 1, EXTENT_DIRTY, 0);
  1818. if (!ret)
  1819. return 0;
  1820. ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
  1821. &private_failure);
  1822. if (ret)
  1823. return 0;
  1824. failrec = (struct io_failure_record *)(unsigned long) private_failure;
  1825. BUG_ON(!failrec->this_mirror);
  1826. if (failrec->in_validation) {
  1827. /* there was no real error, just free the record */
  1828. pr_debug("clean_io_failure: freeing dummy error at %llu\n",
  1829. failrec->start);
  1830. did_repair = 1;
  1831. goto out;
  1832. }
  1833. spin_lock(&BTRFS_I(inode)->io_tree.lock);
  1834. state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
  1835. failrec->start,
  1836. EXTENT_LOCKED);
  1837. spin_unlock(&BTRFS_I(inode)->io_tree.lock);
  1838. if (state && state->start == failrec->start) {
  1839. fs_info = BTRFS_I(inode)->root->fs_info;
  1840. num_copies = btrfs_num_copies(fs_info, failrec->logical,
  1841. failrec->len);
  1842. if (num_copies > 1) {
  1843. ret = repair_io_failure(fs_info, start, failrec->len,
  1844. failrec->logical, page,
  1845. failrec->failed_mirror);
  1846. did_repair = !ret;
  1847. }
  1848. }
  1849. out:
  1850. if (!ret)
  1851. ret = free_io_failure(inode, failrec, did_repair);
  1852. return ret;
  1853. }
  1854. /*
  1855. * this is a generic handler for readpage errors (default
  1856. * readpage_io_failed_hook). if other copies exist, read those and write back
  1857. * good data to the failed position. does not investigate in remapping the
  1858. * failed extent elsewhere, hoping the device will be smart enough to do this as
  1859. * needed
  1860. */
  1861. static int bio_readpage_error(struct bio *failed_bio, struct page *page,
  1862. u64 start, u64 end, int failed_mirror,
  1863. struct extent_state *state)
  1864. {
  1865. struct io_failure_record *failrec = NULL;
  1866. u64 private;
  1867. struct extent_map *em;
  1868. struct inode *inode = page->mapping->host;
  1869. struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
  1870. struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
  1871. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  1872. struct bio *bio;
  1873. int num_copies;
  1874. int ret;
  1875. int read_mode;
  1876. u64 logical;
  1877. BUG_ON(failed_bio->bi_rw & REQ_WRITE);
  1878. ret = get_state_private(failure_tree, start, &private);
  1879. if (ret) {
  1880. failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
  1881. if (!failrec)
  1882. return -ENOMEM;
  1883. failrec->start = start;
  1884. failrec->len = end - start + 1;
  1885. failrec->this_mirror = 0;
  1886. failrec->bio_flags = 0;
  1887. failrec->in_validation = 0;
  1888. read_lock(&em_tree->lock);
  1889. em = lookup_extent_mapping(em_tree, start, failrec->len);
  1890. if (!em) {
  1891. read_unlock(&em_tree->lock);
  1892. kfree(failrec);
  1893. return -EIO;
  1894. }
  1895. if (em->start > start || em->start + em->len < start) {
  1896. free_extent_map(em);
  1897. em = NULL;
  1898. }
  1899. read_unlock(&em_tree->lock);
  1900. if (!em) {
  1901. kfree(failrec);
  1902. return -EIO;
  1903. }
  1904. logical = start - em->start;
  1905. logical = em->block_start + logical;
  1906. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  1907. logical = em->block_start;
  1908. failrec->bio_flags = EXTENT_BIO_COMPRESSED;
  1909. extent_set_compress_type(&failrec->bio_flags,
  1910. em->compress_type);
  1911. }
  1912. pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
  1913. "len=%llu\n", logical, start, failrec->len);
  1914. failrec->logical = logical;
  1915. free_extent_map(em);
  1916. /* set the bits in the private failure tree */
  1917. ret = set_extent_bits(failure_tree, start, end,
  1918. EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
  1919. if (ret >= 0)
  1920. ret = set_state_private(failure_tree, start,
  1921. (u64)(unsigned long)failrec);
  1922. /* set the bits in the inode's tree */
  1923. if (ret >= 0)
  1924. ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
  1925. GFP_NOFS);
  1926. if (ret < 0) {
  1927. kfree(failrec);
  1928. return ret;
  1929. }
  1930. } else {
  1931. failrec = (struct io_failure_record *)(unsigned long)private;
  1932. pr_debug("bio_readpage_error: (found) logical=%llu, "
  1933. "start=%llu, len=%llu, validation=%d\n",
  1934. failrec->logical, failrec->start, failrec->len,
  1935. failrec->in_validation);
  1936. /*
  1937. * when data can be on disk more than twice, add to failrec here
  1938. * (e.g. with a list for failed_mirror) to make
  1939. * clean_io_failure() clean all those errors at once.
  1940. */
  1941. }
  1942. num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
  1943. failrec->logical, failrec->len);
  1944. if (num_copies == 1) {
  1945. /*
  1946. * we only have a single copy of the data, so don't bother with
  1947. * all the retry and error correction code that follows. no
  1948. * matter what the error is, it is very likely to persist.
  1949. */
  1950. pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
  1951. "state=%p, num_copies=%d, next_mirror %d, "
  1952. "failed_mirror %d\n", state, num_copies,
  1953. failrec->this_mirror, failed_mirror);
  1954. free_io_failure(inode, failrec, 0);
  1955. return -EIO;
  1956. }
  1957. if (!state) {
  1958. spin_lock(&tree->lock);
  1959. state = find_first_extent_bit_state(tree, failrec->start,
  1960. EXTENT_LOCKED);
  1961. if (state && state->start != failrec->start)
  1962. state = NULL;
  1963. spin_unlock(&tree->lock);
  1964. }
  1965. /*
  1966. * there are two premises:
  1967. * a) deliver good data to the caller
  1968. * b) correct the bad sectors on disk
  1969. */
  1970. if (failed_bio->bi_vcnt > 1) {
  1971. /*
  1972. * to fulfill b), we need to know the exact failing sectors, as
  1973. * we don't want to rewrite any more than the failed ones. thus,
  1974. * we need separate read requests for the failed bio
  1975. *
  1976. * if the following BUG_ON triggers, our validation request got
  1977. * merged. we need separate requests for our algorithm to work.
  1978. */
  1979. BUG_ON(failrec->in_validation);
  1980. failrec->in_validation = 1;
  1981. failrec->this_mirror = failed_mirror;
  1982. read_mode = READ_SYNC | REQ_FAILFAST_DEV;
  1983. } else {
  1984. /*
  1985. * we're ready to fulfill a) and b) alongside. get a good copy
  1986. * of the failed sector and if we succeed, we have setup
  1987. * everything for repair_io_failure to do the rest for us.
  1988. */
  1989. if (failrec->in_validation) {
  1990. BUG_ON(failrec->this_mirror != failed_mirror);
  1991. failrec->in_validation = 0;
  1992. failrec->this_mirror = 0;
  1993. }
  1994. failrec->failed_mirror = failed_mirror;
  1995. failrec->this_mirror++;
  1996. if (failrec->this_mirror == failed_mirror)
  1997. failrec->this_mirror++;
  1998. read_mode = READ_SYNC;
  1999. }
  2000. if (!state || failrec->this_mirror > num_copies) {
  2001. pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
  2002. "next_mirror %d, failed_mirror %d\n", state,
  2003. num_copies, failrec->this_mirror, failed_mirror);
  2004. free_io_failure(inode, failrec, 0);
  2005. return -EIO;
  2006. }
  2007. bio = bio_alloc(GFP_NOFS, 1);
  2008. if (!bio) {
  2009. free_io_failure(inode, failrec, 0);
  2010. return -EIO;
  2011. }
  2012. bio->bi_private = state;
  2013. bio->bi_end_io = failed_bio->bi_end_io;
  2014. bio->bi_sector = failrec->logical >> 9;
  2015. bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
  2016. bio->bi_size = 0;
  2017. bio_add_page(bio, page, failrec->len, start - page_offset(page));
  2018. pr_debug("bio_readpage_error: submitting new read[%#x] to "
  2019. "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
  2020. failrec->this_mirror, num_copies, failrec->in_validation);
  2021. ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
  2022. failrec->this_mirror,
  2023. failrec->bio_flags, 0);
  2024. return ret;
  2025. }
  2026. /* lots and lots of room for performance fixes in the end_bio funcs */
  2027. int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
  2028. {
  2029. int uptodate = (err == 0);
  2030. struct extent_io_tree *tree;
  2031. int ret;
  2032. tree = &BTRFS_I(page->mapping->host)->io_tree;
  2033. if (tree->ops && tree->ops->writepage_end_io_hook) {
  2034. ret = tree->ops->writepage_end_io_hook(page, start,
  2035. end, NULL, uptodate);
  2036. if (ret)
  2037. uptodate = 0;
  2038. }
  2039. if (!uptodate) {
  2040. ClearPageUptodate(page);
  2041. SetPageError(page);
  2042. }
  2043. return 0;
  2044. }
  2045. /*
  2046. * after a writepage IO is done, we need to:
  2047. * clear the uptodate bits on error
  2048. * clear the writeback bits in the extent tree for this IO
  2049. * end_page_writeback if the page has no more pending IO
  2050. *
  2051. * Scheduling is not allowed, so the extent state tree is expected
  2052. * to have one and only one object corresponding to this IO.
  2053. */
  2054. static void end_bio_extent_writepage(struct bio *bio, int err)
  2055. {
  2056. struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
  2057. struct extent_io_tree *tree;
  2058. u64 start;
  2059. u64 end;
  2060. int whole_page;
  2061. do {
  2062. struct page *page = bvec->bv_page;
  2063. tree = &BTRFS_I(page->mapping->host)->io_tree;
  2064. start = page_offset(page) + bvec->bv_offset;
  2065. end = start + bvec->bv_len - 1;
  2066. if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
  2067. whole_page = 1;
  2068. else
  2069. whole_page = 0;
  2070. if (--bvec >= bio->bi_io_vec)
  2071. prefetchw(&bvec->bv_page->flags);
  2072. if (end_extent_writepage(page, err, start, end))
  2073. continue;
  2074. if (whole_page)
  2075. end_page_writeback(page);
  2076. else
  2077. check_page_writeback(tree, page);
  2078. } while (bvec >= bio->bi_io_vec);
  2079. bio_put(bio);
  2080. }
  2081. /*
  2082. * after a readpage IO is done, we need to:
  2083. * clear the uptodate bits on error
  2084. * set the uptodate bits if things worked
  2085. * set the page up to date if all extents in the tree are uptodate
  2086. * clear the lock bit in the extent tree
  2087. * unlock the page if there are no other extents locked for it
  2088. *
  2089. * Scheduling is not allowed, so the extent state tree is expected
  2090. * to have one and only one object corresponding to this IO.
  2091. */
  2092. static void end_bio_extent_readpage(struct bio *bio, int err)
  2093. {
  2094. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  2095. struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
  2096. struct bio_vec *bvec = bio->bi_io_vec;
  2097. struct extent_io_tree *tree;
  2098. u64 start;
  2099. u64 end;
  2100. int whole_page;
  2101. int mirror;
  2102. int ret;
  2103. if (err)
  2104. uptodate = 0;
  2105. do {
  2106. struct page *page = bvec->bv_page;
  2107. struct extent_state *cached = NULL;
  2108. struct extent_state *state;
  2109. pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, "
  2110. "mirror=%ld\n", (u64)bio->bi_sector, err,
  2111. (long int)bio->bi_bdev);
  2112. tree = &BTRFS_I(page->mapping->host)->io_tree;
  2113. start = page_offset(page) + bvec->bv_offset;
  2114. end = start + bvec->bv_len - 1;
  2115. if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
  2116. whole_page = 1;
  2117. else
  2118. whole_page = 0;
  2119. if (++bvec <= bvec_end)
  2120. prefetchw(&bvec->bv_page->flags);
  2121. spin_lock(&tree->lock);
  2122. state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
  2123. if (state && state->start == start) {
  2124. /*
  2125. * take a reference on the state, unlock will drop
  2126. * the ref
  2127. */
  2128. cache_state(state, &cached);
  2129. }
  2130. spin_unlock(&tree->lock);
  2131. mirror = (int)(unsigned long)bio->bi_bdev;
  2132. if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
  2133. ret = tree->ops->readpage_end_io_hook(page, start, end,
  2134. state, mirror);
  2135. if (ret)
  2136. uptodate = 0;
  2137. else
  2138. clean_io_failure(start, page);
  2139. }
  2140. if (!uptodate && tree->ops && tree->ops->readpage_io_failed_hook) {
  2141. ret = tree->ops->readpage_io_failed_hook(page, mirror);
  2142. if (!ret && !err &&
  2143. test_bit(BIO_UPTODATE, &bio->bi_flags))
  2144. uptodate = 1;
  2145. } else if (!uptodate) {
  2146. /*
  2147. * The generic bio_readpage_error handles errors the
  2148. * following way: If possible, new read requests are
  2149. * created and submitted and will end up in
  2150. * end_bio_extent_readpage as well (if we're lucky, not
  2151. * in the !uptodate case). In that case it returns 0 and
  2152. * we just go on with the next page in our bio. If it
  2153. * can't handle the error it will return -EIO and we
  2154. * remain responsible for that page.
  2155. */
  2156. ret = bio_readpage_error(bio, page, start, end, mirror, NULL);
  2157. if (ret == 0) {
  2158. uptodate =
  2159. test_bit(BIO_UPTODATE, &bio->bi_flags);
  2160. if (err)
  2161. uptodate = 0;
  2162. uncache_state(&cached);
  2163. continue;
  2164. }
  2165. }
  2166. if (uptodate && tree->track_uptodate) {
  2167. set_extent_uptodate(tree, start, end, &cached,
  2168. GFP_ATOMIC);
  2169. }
  2170. unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
  2171. if (whole_page) {
  2172. if (uptodate) {
  2173. SetPageUptodate(page);
  2174. } else {
  2175. ClearPageUptodate(page);
  2176. SetPageError(page);
  2177. }
  2178. unlock_page(page);
  2179. } else {
  2180. if (uptodate) {
  2181. check_page_uptodate(tree, page);
  2182. } else {
  2183. ClearPageUptodate(page);
  2184. SetPageError(page);
  2185. }
  2186. check_page_locked(tree, page);
  2187. }
  2188. } while (bvec <= bvec_end);
  2189. bio_put(bio);
  2190. }
  2191. struct bio *
  2192. btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
  2193. gfp_t gfp_flags)
  2194. {
  2195. struct bio *bio;
  2196. bio = bio_alloc(gfp_flags, nr_vecs);
  2197. if (bio == NULL && (current->flags & PF_MEMALLOC)) {
  2198. while (!bio && (nr_vecs /= 2))
  2199. bio = bio_alloc(gfp_flags, nr_vecs);
  2200. }
  2201. if (bio) {
  2202. bio->bi_size = 0;
  2203. bio->bi_bdev = bdev;
  2204. bio->bi_sector = first_sector;
  2205. }
  2206. return bio;
  2207. }
  2208. static int __must_check submit_one_bio(int rw, struct bio *bio,
  2209. int mirror_num, unsigned long bio_flags)
  2210. {
  2211. int ret = 0;
  2212. struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
  2213. struct page *page = bvec->bv_page;
  2214. struct extent_io_tree *tree = bio->bi_private;
  2215. u64 start;
  2216. start = page_offset(page) + bvec->bv_offset;
  2217. bio->bi_private = NULL;
  2218. bio_get(bio);
  2219. if (tree->ops && tree->ops->submit_bio_hook)
  2220. ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
  2221. mirror_num, bio_flags, start);
  2222. else
  2223. btrfsic_submit_bio(rw, bio);
  2224. if (bio_flagged(bio, BIO_EOPNOTSUPP))
  2225. ret = -EOPNOTSUPP;
  2226. bio_put(bio);
  2227. return ret;
  2228. }
  2229. static int merge_bio(struct extent_io_tree *tree, struct page *page,
  2230. unsigned long offset, size_t size, struct bio *bio,
  2231. unsigned long bio_flags)
  2232. {
  2233. int ret = 0;
  2234. if (tree->ops && tree->ops->merge_bio_hook)
  2235. ret = tree->ops->merge_bio_hook(page, offset, size, bio,
  2236. bio_flags);
  2237. BUG_ON(ret < 0);
  2238. return ret;
  2239. }
  2240. static int submit_extent_page(int rw, struct extent_io_tree *tree,
  2241. struct page *page, sector_t sector,
  2242. size_t size, unsigned long offset,
  2243. struct block_device *bdev,
  2244. struct bio **bio_ret,
  2245. unsigned long max_pages,
  2246. bio_end_io_t end_io_func,
  2247. int mirror_num,
  2248. unsigned long prev_bio_flags,
  2249. unsigned long bio_flags)
  2250. {
  2251. int ret = 0;
  2252. struct bio *bio;
  2253. int nr;
  2254. int contig = 0;
  2255. int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
  2256. int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
  2257. size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
  2258. if (bio_ret && *bio_ret) {
  2259. bio = *bio_ret;
  2260. if (old_compressed)
  2261. contig = bio->bi_sector == sector;
  2262. else
  2263. contig = bio->bi_sector + (bio->bi_size >> 9) ==
  2264. sector;
  2265. if (prev_bio_flags != bio_flags || !contig ||
  2266. merge_bio(tree, page, offset, page_size, bio, bio_flags) ||
  2267. bio_add_page(bio, page, page_size, offset) < page_size) {
  2268. ret = submit_one_bio(rw, bio, mirror_num,
  2269. prev_bio_flags);
  2270. if (ret < 0)
  2271. return ret;
  2272. bio = NULL;
  2273. } else {
  2274. return 0;
  2275. }
  2276. }
  2277. if (this_compressed)
  2278. nr = BIO_MAX_PAGES;
  2279. else
  2280. nr = bio_get_nr_vecs(bdev);
  2281. bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
  2282. if (!bio)
  2283. return -ENOMEM;
  2284. bio_add_page(bio, page, page_size, offset);
  2285. bio->bi_end_io = end_io_func;
  2286. bio->bi_private = tree;
  2287. if (bio_ret)
  2288. *bio_ret = bio;
  2289. else
  2290. ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
  2291. return ret;
  2292. }
  2293. void attach_extent_buffer_page(struct extent_buffer *eb, struct page *page)
  2294. {
  2295. if (!PagePrivate(page)) {
  2296. SetPagePrivate(page);
  2297. page_cache_get(page);
  2298. set_page_private(page, (unsigned long)eb);
  2299. } else {
  2300. WARN_ON(page->private != (unsigned long)eb);
  2301. }
  2302. }
  2303. void set_page_extent_mapped(struct page *page)
  2304. {
  2305. if (!PagePrivate(page)) {
  2306. SetPagePrivate(page);
  2307. page_cache_get(page);
  2308. set_page_private(page, EXTENT_PAGE_PRIVATE);
  2309. }
  2310. }
  2311. /*
  2312. * basic readpage implementation. Locked extent state structs are inserted
  2313. * into the tree that are removed when the IO is done (by the end_io
  2314. * handlers)
  2315. * XXX JDM: This needs looking at to ensure proper page locking
  2316. */
  2317. static int __extent_read_full_page(struct extent_io_tree *tree,
  2318. struct page *page,
  2319. get_extent_t *get_extent,
  2320. struct bio **bio, int mirror_num,
  2321. unsigned long *bio_flags)
  2322. {
  2323. struct inode *inode = page->mapping->host;
  2324. u64 start = page_offset(page);
  2325. u64 page_end = start + PAGE_CACHE_SIZE - 1;
  2326. u64 end;
  2327. u64 cur = start;
  2328. u64 extent_offset;
  2329. u64 last_byte = i_size_read(inode);
  2330. u64 block_start;
  2331. u64 cur_end;
  2332. sector_t sector;
  2333. struct extent_map *em;
  2334. struct block_device *bdev;
  2335. struct btrfs_ordered_extent *ordered;
  2336. int ret;
  2337. int nr = 0;
  2338. size_t pg_offset = 0;
  2339. size_t iosize;
  2340. size_t disk_io_size;
  2341. size_t blocksize = inode->i_sb->s_blocksize;
  2342. unsigned long this_bio_flag = 0;
  2343. set_page_extent_mapped(page);
  2344. if (!PageUptodate(page)) {
  2345. if (cleancache_get_page(page) == 0) {
  2346. BUG_ON(blocksize != PAGE_SIZE);
  2347. goto out;
  2348. }
  2349. }
  2350. end = page_end;
  2351. while (1) {
  2352. lock_extent(tree, start, end);
  2353. ordered = btrfs_lookup_ordered_extent(inode, start);
  2354. if (!ordered)
  2355. break;
  2356. unlock_extent(tree, start, end);
  2357. btrfs_start_ordered_extent(inode, ordered, 1);
  2358. btrfs_put_ordered_extent(ordered);
  2359. }
  2360. if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
  2361. char *userpage;
  2362. size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
  2363. if (zero_offset) {
  2364. iosize = PAGE_CACHE_SIZE - zero_offset;
  2365. userpage = kmap_atomic(page);
  2366. memset(userpage + zero_offset, 0, iosize);
  2367. flush_dcache_page(page);
  2368. kunmap_atomic(userpage);
  2369. }
  2370. }
  2371. while (cur <= end) {
  2372. if (cur >= last_byte) {
  2373. char *userpage;
  2374. struct extent_state *cached = NULL;
  2375. iosize = PAGE_CACHE_SIZE - pg_offset;
  2376. userpage = kmap_atomic(page);
  2377. memset(userpage + pg_offset, 0, iosize);
  2378. flush_dcache_page(page);
  2379. kunmap_atomic(userpage);
  2380. set_extent_uptodate(tree, cur, cur + iosize - 1,
  2381. &cached, GFP_NOFS);
  2382. unlock_extent_cached(tree, cur, cur + iosize - 1,
  2383. &cached, GFP_NOFS);
  2384. break;
  2385. }
  2386. em = get_extent(inode, page, pg_offset, cur,
  2387. end - cur + 1, 0);
  2388. if (IS_ERR_OR_NULL(em)) {
  2389. SetPageError(page);
  2390. unlock_extent(tree, cur, end);
  2391. break;
  2392. }
  2393. extent_offset = cur - em->start;
  2394. BUG_ON(extent_map_end(em) <= cur);
  2395. BUG_ON(end < cur);
  2396. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  2397. this_bio_flag = EXTENT_BIO_COMPRESSED;
  2398. extent_set_compress_type(&this_bio_flag,
  2399. em->compress_type);
  2400. }
  2401. iosize = min(extent_map_end(em) - cur, end - cur + 1);
  2402. cur_end = min(extent_map_end(em) - 1, end);
  2403. iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
  2404. if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
  2405. disk_io_size = em->block_len;
  2406. sector = em->block_start >> 9;
  2407. } else {
  2408. sector = (em->block_start + extent_offset) >> 9;
  2409. disk_io_size = iosize;
  2410. }
  2411. bdev = em->bdev;
  2412. block_start = em->block_start;
  2413. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  2414. block_start = EXTENT_MAP_HOLE;
  2415. free_extent_map(em);
  2416. em = NULL;
  2417. /* we've found a hole, just zero and go on */
  2418. if (block_start == EXTENT_MAP_HOLE) {
  2419. char *userpage;
  2420. struct extent_state *cached = NULL;
  2421. userpage = kmap_atomic(page);
  2422. memset(userpage + pg_offset, 0, iosize);
  2423. flush_dcache_page(page);
  2424. kunmap_atomic(userpage);
  2425. set_extent_uptodate(tree, cur, cur + iosize - 1,
  2426. &cached, GFP_NOFS);
  2427. unlock_extent_cached(tree, cur, cur + iosize - 1,
  2428. &cached, GFP_NOFS);
  2429. cur = cur + iosize;
  2430. pg_offset += iosize;
  2431. continue;
  2432. }
  2433. /* the get_extent function already copied into the page */
  2434. if (test_range_bit(tree, cur, cur_end,
  2435. EXTENT_UPTODATE, 1, NULL)) {
  2436. check_page_uptodate(tree, page);
  2437. unlock_extent(tree, cur, cur + iosize - 1);
  2438. cur = cur + iosize;
  2439. pg_offset += iosize;
  2440. continue;
  2441. }
  2442. /* we have an inline extent but it didn't get marked up
  2443. * to date. Error out
  2444. */
  2445. if (block_start == EXTENT_MAP_INLINE) {
  2446. SetPageError(page);
  2447. unlock_extent(tree, cur, cur + iosize - 1);
  2448. cur = cur + iosize;
  2449. pg_offset += iosize;
  2450. continue;
  2451. }
  2452. ret = 0;
  2453. if (tree->ops && tree->ops->readpage_io_hook) {
  2454. ret = tree->ops->readpage_io_hook(page, cur,
  2455. cur + iosize - 1);
  2456. }
  2457. if (!ret) {
  2458. unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
  2459. pnr -= page->index;
  2460. ret = submit_extent_page(READ, tree, page,
  2461. sector, disk_io_size, pg_offset,
  2462. bdev, bio, pnr,
  2463. end_bio_extent_readpage, mirror_num,
  2464. *bio_flags,
  2465. this_bio_flag);
  2466. if (!ret) {
  2467. nr++;
  2468. *bio_flags = this_bio_flag;
  2469. }
  2470. }
  2471. if (ret) {
  2472. SetPageError(page);
  2473. unlock_extent(tree, cur, cur + iosize - 1);
  2474. }
  2475. cur = cur + iosize;
  2476. pg_offset += iosize;
  2477. }
  2478. out:
  2479. if (!nr) {
  2480. if (!PageError(page))
  2481. SetPageUptodate(page);
  2482. unlock_page(page);
  2483. }
  2484. return 0;
  2485. }
  2486. int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
  2487. get_extent_t *get_extent, int mirror_num)
  2488. {
  2489. struct bio *bio = NULL;
  2490. unsigned long bio_flags = 0;
  2491. int ret;
  2492. ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
  2493. &bio_flags);
  2494. if (bio)
  2495. ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
  2496. return ret;
  2497. }
  2498. static noinline void update_nr_written(struct page *page,
  2499. struct writeback_control *wbc,
  2500. unsigned long nr_written)
  2501. {
  2502. wbc->nr_to_write -= nr_written;
  2503. if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
  2504. wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
  2505. page->mapping->writeback_index = page->index + nr_written;
  2506. }
  2507. /*
  2508. * the writepage semantics are similar to regular writepage. extent
  2509. * records are inserted to lock ranges in the tree, and as dirty areas
  2510. * are found, they are marked writeback. Then the lock bits are removed
  2511. * and the end_io handler clears the writeback ranges
  2512. */
  2513. static int __extent_writepage(struct page *page, struct writeback_control *wbc,
  2514. void *data)
  2515. {
  2516. struct inode *inode = page->mapping->host;
  2517. struct extent_page_data *epd = data;
  2518. struct extent_io_tree *tree = epd->tree;
  2519. u64 start = page_offset(page);
  2520. u64 delalloc_start;
  2521. u64 page_end = start + PAGE_CACHE_SIZE - 1;
  2522. u64 end;
  2523. u64 cur = start;
  2524. u64 extent_offset;
  2525. u64 last_byte = i_size_read(inode);
  2526. u64 block_start;
  2527. u64 iosize;
  2528. sector_t sector;
  2529. struct extent_state *cached_state = NULL;
  2530. struct extent_map *em;
  2531. struct block_device *bdev;
  2532. int ret;
  2533. int nr = 0;
  2534. size_t pg_offset = 0;
  2535. size_t blocksize;
  2536. loff_t i_size = i_size_read(inode);
  2537. unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
  2538. u64 nr_delalloc;
  2539. u64 delalloc_end;
  2540. int page_started;
  2541. int compressed;
  2542. int write_flags;
  2543. unsigned long nr_written = 0;
  2544. bool fill_delalloc = true;
  2545. if (wbc->sync_mode == WB_SYNC_ALL)
  2546. write_flags = WRITE_SYNC;
  2547. else
  2548. write_flags = WRITE;
  2549. trace___extent_writepage(page, inode, wbc);
  2550. WARN_ON(!PageLocked(page));
  2551. ClearPageError(page);
  2552. pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
  2553. if (page->index > end_index ||
  2554. (page->index == end_index && !pg_offset)) {
  2555. page->mapping->a_ops->invalidatepage(page, 0);
  2556. unlock_page(page);
  2557. return 0;
  2558. }
  2559. if (page->index == end_index) {
  2560. char *userpage;
  2561. userpage = kmap_atomic(page);
  2562. memset(userpage + pg_offset, 0,
  2563. PAGE_CACHE_SIZE - pg_offset);
  2564. kunmap_atomic(userpage);
  2565. flush_dcache_page(page);
  2566. }
  2567. pg_offset = 0;
  2568. set_page_extent_mapped(page);
  2569. if (!tree->ops || !tree->ops->fill_delalloc)
  2570. fill_delalloc = false;
  2571. delalloc_start = start;
  2572. delalloc_end = 0;
  2573. page_started = 0;
  2574. if (!epd->extent_locked && fill_delalloc) {
  2575. u64 delalloc_to_write = 0;
  2576. /*
  2577. * make sure the wbc mapping index is at least updated
  2578. * to this page.
  2579. */
  2580. update_nr_written(page, wbc, 0);
  2581. while (delalloc_end < page_end) {
  2582. nr_delalloc = find_lock_delalloc_range(inode, tree,
  2583. page,
  2584. &delalloc_start,
  2585. &delalloc_end,
  2586. 128 * 1024 * 1024);
  2587. if (nr_delalloc == 0) {
  2588. delalloc_start = delalloc_end + 1;
  2589. continue;
  2590. }
  2591. ret = tree->ops->fill_delalloc(inode, page,
  2592. delalloc_start,
  2593. delalloc_end,
  2594. &page_started,
  2595. &nr_written);
  2596. /* File system has been set read-only */
  2597. if (ret) {
  2598. SetPageError(page);
  2599. goto done;
  2600. }
  2601. /*
  2602. * delalloc_end is already one less than the total
  2603. * length, so we don't subtract one from
  2604. * PAGE_CACHE_SIZE
  2605. */
  2606. delalloc_to_write += (delalloc_end - delalloc_start +
  2607. PAGE_CACHE_SIZE) >>
  2608. PAGE_CACHE_SHIFT;
  2609. delalloc_start = delalloc_end + 1;
  2610. }
  2611. if (wbc->nr_to_write < delalloc_to_write) {
  2612. int thresh = 8192;
  2613. if (delalloc_to_write < thresh * 2)
  2614. thresh = delalloc_to_write;
  2615. wbc->nr_to_write = min_t(u64, delalloc_to_write,
  2616. thresh);
  2617. }
  2618. /* did the fill delalloc function already unlock and start
  2619. * the IO?
  2620. */
  2621. if (page_started) {
  2622. ret = 0;
  2623. /*
  2624. * we've unlocked the page, so we can't update
  2625. * the mapping's writeback index, just update
  2626. * nr_to_write.
  2627. */
  2628. wbc->nr_to_write -= nr_written;
  2629. goto done_unlocked;
  2630. }
  2631. }
  2632. if (tree->ops && tree->ops->writepage_start_hook) {
  2633. ret = tree->ops->writepage_start_hook(page, start,
  2634. page_end);
  2635. if (ret) {
  2636. /* Fixup worker will requeue */
  2637. if (ret == -EBUSY)
  2638. wbc->pages_skipped++;
  2639. else
  2640. redirty_page_for_writepage(wbc, page);
  2641. update_nr_written(page, wbc, nr_written);
  2642. unlock_page(page);
  2643. ret = 0;
  2644. goto done_unlocked;
  2645. }
  2646. }
  2647. /*
  2648. * we don't want to touch the inode after unlocking the page,
  2649. * so we update the mapping writeback index now
  2650. */
  2651. update_nr_written(page, wbc, nr_written + 1);
  2652. end = page_end;
  2653. if (last_byte <= start) {
  2654. if (tree->ops && tree->ops->writepage_end_io_hook)
  2655. tree->ops->writepage_end_io_hook(page, start,
  2656. page_end, NULL, 1);
  2657. goto done;
  2658. }
  2659. blocksize = inode->i_sb->s_blocksize;
  2660. while (cur <= end) {
  2661. if (cur >= last_byte) {
  2662. if (tree->ops && tree->ops->writepage_end_io_hook)
  2663. tree->ops->writepage_end_io_hook(page, cur,
  2664. page_end, NULL, 1);
  2665. break;
  2666. }
  2667. em = epd->get_extent(inode, page, pg_offset, cur,
  2668. end - cur + 1, 1);
  2669. if (IS_ERR_OR_NULL(em)) {
  2670. SetPageError(page);
  2671. break;
  2672. }
  2673. extent_offset = cur - em->start;
  2674. BUG_ON(extent_map_end(em) <= cur);
  2675. BUG_ON(end < cur);
  2676. iosize = min(extent_map_end(em) - cur, end - cur + 1);
  2677. iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
  2678. sector = (em->block_start + extent_offset) >> 9;
  2679. bdev = em->bdev;
  2680. block_start = em->block_start;
  2681. compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  2682. free_extent_map(em);
  2683. em = NULL;
  2684. /*
  2685. * compressed and inline extents are written through other
  2686. * paths in the FS
  2687. */
  2688. if (compressed || block_start == EXTENT_MAP_HOLE ||
  2689. block_start == EXTENT_MAP_INLINE) {
  2690. /*
  2691. * end_io notification does not happen here for
  2692. * compressed extents
  2693. */
  2694. if (!compressed && tree->ops &&
  2695. tree->ops->writepage_end_io_hook)
  2696. tree->ops->writepage_end_io_hook(page, cur,
  2697. cur + iosize - 1,
  2698. NULL, 1);
  2699. else if (compressed) {
  2700. /* we don't want to end_page_writeback on
  2701. * a compressed extent. this happens
  2702. * elsewhere
  2703. */
  2704. nr++;
  2705. }
  2706. cur += iosize;
  2707. pg_offset += iosize;
  2708. continue;
  2709. }
  2710. /* leave this out until we have a page_mkwrite call */
  2711. if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
  2712. EXTENT_DIRTY, 0, NULL)) {
  2713. cur = cur + iosize;
  2714. pg_offset += iosize;
  2715. continue;
  2716. }
  2717. if (tree->ops && tree->ops->writepage_io_hook) {
  2718. ret = tree->ops->writepage_io_hook(page, cur,
  2719. cur + iosize - 1);
  2720. } else {
  2721. ret = 0;
  2722. }
  2723. if (ret) {
  2724. SetPageError(page);
  2725. } else {
  2726. unsigned long max_nr = end_index + 1;
  2727. set_range_writeback(tree, cur, cur + iosize - 1);
  2728. if (!PageWriteback(page)) {
  2729. printk(KERN_ERR "btrfs warning page %lu not "
  2730. "writeback, cur %llu end %llu\n",
  2731. page->index, (unsigned long long)cur,
  2732. (unsigned long long)end);
  2733. }
  2734. ret = submit_extent_page(write_flags, tree, page,
  2735. sector, iosize, pg_offset,
  2736. bdev, &epd->bio, max_nr,
  2737. end_bio_extent_writepage,
  2738. 0, 0, 0);
  2739. if (ret)
  2740. SetPageError(page);
  2741. }
  2742. cur = cur + iosize;
  2743. pg_offset += iosize;
  2744. nr++;
  2745. }
  2746. done:
  2747. if (nr == 0) {
  2748. /* make sure the mapping tag for page dirty gets cleared */
  2749. set_page_writeback(page);
  2750. end_page_writeback(page);
  2751. }
  2752. unlock_page(page);
  2753. done_unlocked:
  2754. /* drop our reference on any cached states */
  2755. free_extent_state(cached_state);
  2756. return 0;
  2757. }
  2758. static int eb_wait(void *word)
  2759. {
  2760. io_schedule();
  2761. return 0;
  2762. }
  2763. static void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
  2764. {
  2765. wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait,
  2766. TASK_UNINTERRUPTIBLE);
  2767. }
  2768. static int lock_extent_buffer_for_io(struct extent_buffer *eb,
  2769. struct btrfs_fs_info *fs_info,
  2770. struct extent_page_data *epd)
  2771. {
  2772. unsigned long i, num_pages;
  2773. int flush = 0;
  2774. int ret = 0;
  2775. if (!btrfs_try_tree_write_lock(eb)) {
  2776. flush = 1;
  2777. flush_write_bio(epd);
  2778. btrfs_tree_lock(eb);
  2779. }
  2780. if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
  2781. btrfs_tree_unlock(eb);
  2782. if (!epd->sync_io)
  2783. return 0;
  2784. if (!flush) {
  2785. flush_write_bio(epd);
  2786. flush = 1;
  2787. }
  2788. while (1) {
  2789. wait_on_extent_buffer_writeback(eb);
  2790. btrfs_tree_lock(eb);
  2791. if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
  2792. break;
  2793. btrfs_tree_unlock(eb);
  2794. }
  2795. }
  2796. /*
  2797. * We need to do this to prevent races in people who check if the eb is
  2798. * under IO since we can end up having no IO bits set for a short period
  2799. * of time.
  2800. */
  2801. spin_lock(&eb->refs_lock);
  2802. if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
  2803. set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
  2804. spin_unlock(&eb->refs_lock);
  2805. btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
  2806. __percpu_counter_add(&fs_info->dirty_metadata_bytes,
  2807. -eb->len,
  2808. fs_info->dirty_metadata_batch);
  2809. ret = 1;
  2810. } else {
  2811. spin_unlock(&eb->refs_lock);
  2812. }
  2813. btrfs_tree_unlock(eb);
  2814. if (!ret)
  2815. return ret;
  2816. num_pages = num_extent_pages(eb->start, eb->len);
  2817. for (i = 0; i < num_pages; i++) {
  2818. struct page *p = extent_buffer_page(eb, i);
  2819. if (!trylock_page(p)) {
  2820. if (!flush) {
  2821. flush_write_bio(epd);
  2822. flush = 1;
  2823. }
  2824. lock_page(p);
  2825. }
  2826. }
  2827. return ret;
  2828. }
  2829. static void end_extent_buffer_writeback(struct extent_buffer *eb)
  2830. {
  2831. clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
  2832. smp_mb__after_clear_bit();
  2833. wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
  2834. }
  2835. static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
  2836. {
  2837. int uptodate = err == 0;
  2838. struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
  2839. struct extent_buffer *eb;
  2840. int done;
  2841. do {
  2842. struct page *page = bvec->bv_page;
  2843. bvec--;
  2844. eb = (struct extent_buffer *)page->private;
  2845. BUG_ON(!eb);
  2846. done = atomic_dec_and_test(&eb->io_pages);
  2847. if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
  2848. set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
  2849. ClearPageUptodate(page);
  2850. SetPageError(page);
  2851. }
  2852. end_page_writeback(page);
  2853. if (!done)
  2854. continue;
  2855. end_extent_buffer_writeback(eb);
  2856. } while (bvec >= bio->bi_io_vec);
  2857. bio_put(bio);
  2858. }
  2859. static int write_one_eb(struct extent_buffer *eb,
  2860. struct btrfs_fs_info *fs_info,
  2861. struct writeback_control *wbc,
  2862. struct extent_page_data *epd)
  2863. {
  2864. struct block_device *bdev = fs_info->fs_devices->latest_bdev;
  2865. u64 offset = eb->start;
  2866. unsigned long i, num_pages;
  2867. unsigned long bio_flags = 0;
  2868. int rw = (epd->sync_io ? WRITE_SYNC : WRITE);
  2869. int ret = 0;
  2870. clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
  2871. num_pages = num_extent_pages(eb->start, eb->len);
  2872. atomic_set(&eb->io_pages, num_pages);
  2873. if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID)
  2874. bio_flags = EXTENT_BIO_TREE_LOG;
  2875. for (i = 0; i < num_pages; i++) {
  2876. struct page *p = extent_buffer_page(eb, i);
  2877. clear_page_dirty_for_io(p);
  2878. set_page_writeback(p);
  2879. ret = submit_extent_page(rw, eb->tree, p, offset >> 9,
  2880. PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
  2881. -1, end_bio_extent_buffer_writepage,
  2882. 0, epd->bio_flags, bio_flags);
  2883. epd->bio_flags = bio_flags;
  2884. if (ret) {
  2885. set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
  2886. SetPageError(p);
  2887. if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
  2888. end_extent_buffer_writeback(eb);
  2889. ret = -EIO;
  2890. break;
  2891. }
  2892. offset += PAGE_CACHE_SIZE;
  2893. update_nr_written(p, wbc, 1);
  2894. unlock_page(p);
  2895. }
  2896. if (unlikely(ret)) {
  2897. for (; i < num_pages; i++) {
  2898. struct page *p = extent_buffer_page(eb, i);
  2899. unlock_page(p);
  2900. }
  2901. }
  2902. return ret;
  2903. }
  2904. int btree_write_cache_pages(struct address_space *mapping,
  2905. struct writeback_control *wbc)
  2906. {
  2907. struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
  2908. struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
  2909. struct extent_buffer *eb, *prev_eb = NULL;
  2910. struct extent_page_data epd = {
  2911. .bio = NULL,
  2912. .tree = tree,
  2913. .extent_locked = 0,
  2914. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  2915. .bio_flags = 0,
  2916. };
  2917. int ret = 0;
  2918. int done = 0;
  2919. int nr_to_write_done = 0;
  2920. struct pagevec pvec;
  2921. int nr_pages;
  2922. pgoff_t index;
  2923. pgoff_t end; /* Inclusive */
  2924. int scanned = 0;
  2925. int tag;
  2926. pagevec_init(&pvec, 0);
  2927. if (wbc->range_cyclic) {
  2928. index = mapping->writeback_index; /* Start from prev offset */
  2929. end = -1;
  2930. } else {
  2931. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  2932. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  2933. scanned = 1;
  2934. }
  2935. if (wbc->sync_mode == WB_SYNC_ALL)
  2936. tag = PAGECACHE_TAG_TOWRITE;
  2937. else
  2938. tag = PAGECACHE_TAG_DIRTY;
  2939. retry:
  2940. if (wbc->sync_mode == WB_SYNC_ALL)
  2941. tag_pages_for_writeback(mapping, index, end);
  2942. while (!done && !nr_to_write_done && (index <= end) &&
  2943. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
  2944. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
  2945. unsigned i;
  2946. scanned = 1;
  2947. for (i = 0; i < nr_pages; i++) {
  2948. struct page *page = pvec.pages[i];
  2949. if (!PagePrivate(page))
  2950. continue;
  2951. if (!wbc->range_cyclic && page->index > end) {
  2952. done = 1;
  2953. break;
  2954. }
  2955. spin_lock(&mapping->private_lock);
  2956. if (!PagePrivate(page)) {
  2957. spin_unlock(&mapping->private_lock);
  2958. continue;
  2959. }
  2960. eb = (struct extent_buffer *)page->private;
  2961. /*
  2962. * Shouldn't happen and normally this would be a BUG_ON
  2963. * but no sense in crashing the users box for something
  2964. * we can survive anyway.
  2965. */
  2966. if (!eb) {
  2967. spin_unlock(&mapping->private_lock);
  2968. WARN_ON(1);
  2969. continue;
  2970. }
  2971. if (eb == prev_eb) {
  2972. spin_unlock(&mapping->private_lock);
  2973. continue;
  2974. }
  2975. ret = atomic_inc_not_zero(&eb->refs);
  2976. spin_unlock(&mapping->private_lock);
  2977. if (!ret)
  2978. continue;
  2979. prev_eb = eb;
  2980. ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
  2981. if (!ret) {
  2982. free_extent_buffer(eb);
  2983. continue;
  2984. }
  2985. ret = write_one_eb(eb, fs_info, wbc, &epd);
  2986. if (ret) {
  2987. done = 1;
  2988. free_extent_buffer(eb);
  2989. break;
  2990. }
  2991. free_extent_buffer(eb);
  2992. /*
  2993. * the filesystem may choose to bump up nr_to_write.
  2994. * We have to make sure to honor the new nr_to_write
  2995. * at any time
  2996. */
  2997. nr_to_write_done = wbc->nr_to_write <= 0;
  2998. }
  2999. pagevec_release(&pvec);
  3000. cond_resched();
  3001. }
  3002. if (!scanned && !done) {
  3003. /*
  3004. * We hit the last page and there is more work to be done: wrap
  3005. * back to the start of the file
  3006. */
  3007. scanned = 1;
  3008. index = 0;
  3009. goto retry;
  3010. }
  3011. flush_write_bio(&epd);
  3012. return ret;
  3013. }
  3014. /**
  3015. * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
  3016. * @mapping: address space structure to write
  3017. * @wbc: subtract the number of written pages from *@wbc->nr_to_write
  3018. * @writepage: function called for each page
  3019. * @data: data passed to writepage function
  3020. *
  3021. * If a page is already under I/O, write_cache_pages() skips it, even
  3022. * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
  3023. * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
  3024. * and msync() need to guarantee that all the data which was dirty at the time
  3025. * the call was made get new I/O started against them. If wbc->sync_mode is
  3026. * WB_SYNC_ALL then we were called for data integrity and we must wait for
  3027. * existing IO to complete.
  3028. */
  3029. static int extent_write_cache_pages(struct extent_io_tree *tree,
  3030. struct address_space *mapping,
  3031. struct writeback_control *wbc,
  3032. writepage_t writepage, void *data,
  3033. void (*flush_fn)(void *))
  3034. {
  3035. struct inode *inode = mapping->host;
  3036. int ret = 0;
  3037. int done = 0;
  3038. int nr_to_write_done = 0;
  3039. struct pagevec pvec;
  3040. int nr_pages;
  3041. pgoff_t index;
  3042. pgoff_t end; /* Inclusive */
  3043. int scanned = 0;
  3044. int tag;
  3045. /*
  3046. * We have to hold onto the inode so that ordered extents can do their
  3047. * work when the IO finishes. The alternative to this is failing to add
  3048. * an ordered extent if the igrab() fails there and that is a huge pain
  3049. * to deal with, so instead just hold onto the inode throughout the
  3050. * writepages operation. If it fails here we are freeing up the inode
  3051. * anyway and we'd rather not waste our time writing out stuff that is
  3052. * going to be truncated anyway.
  3053. */
  3054. if (!igrab(inode))
  3055. return 0;
  3056. pagevec_init(&pvec, 0);
  3057. if (wbc->range_cyclic) {
  3058. index = mapping->writeback_index; /* Start from prev offset */
  3059. end = -1;
  3060. } else {
  3061. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  3062. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  3063. scanned = 1;
  3064. }
  3065. if (wbc->sync_mode == WB_SYNC_ALL)
  3066. tag = PAGECACHE_TAG_TOWRITE;
  3067. else
  3068. tag = PAGECACHE_TAG_DIRTY;
  3069. retry:
  3070. if (wbc->sync_mode == WB_SYNC_ALL)
  3071. tag_pages_for_writeback(mapping, index, end);
  3072. while (!done && !nr_to_write_done && (index <= end) &&
  3073. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
  3074. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
  3075. unsigned i;
  3076. scanned = 1;
  3077. for (i = 0; i < nr_pages; i++) {
  3078. struct page *page = pvec.pages[i];
  3079. /*
  3080. * At this point we hold neither mapping->tree_lock nor
  3081. * lock on the page itself: the page may be truncated or
  3082. * invalidated (changing page->mapping to NULL), or even
  3083. * swizzled back from swapper_space to tmpfs file
  3084. * mapping
  3085. */
  3086. if (tree->ops &&
  3087. tree->ops->write_cache_pages_lock_hook) {
  3088. tree->ops->write_cache_pages_lock_hook(page,
  3089. data, flush_fn);
  3090. } else {
  3091. if (!trylock_page(page)) {
  3092. flush_fn(data);
  3093. lock_page(page);
  3094. }
  3095. }
  3096. if (unlikely(page->mapping != mapping)) {
  3097. unlock_page(page);
  3098. continue;
  3099. }
  3100. if (!wbc->range_cyclic && page->index > end) {
  3101. done = 1;
  3102. unlock_page(page);
  3103. continue;
  3104. }
  3105. if (wbc->sync_mode != WB_SYNC_NONE) {
  3106. if (PageWriteback(page))
  3107. flush_fn(data);
  3108. wait_on_page_writeback(page);
  3109. }
  3110. if (PageWriteback(page) ||
  3111. !clear_page_dirty_for_io(page)) {
  3112. unlock_page(page);
  3113. continue;
  3114. }
  3115. ret = (*writepage)(page, wbc, data);
  3116. if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
  3117. unlock_page(page);
  3118. ret = 0;
  3119. }
  3120. if (ret)
  3121. done = 1;
  3122. /*
  3123. * the filesystem may choose to bump up nr_to_write.
  3124. * We have to make sure to honor the new nr_to_write
  3125. * at any time
  3126. */
  3127. nr_to_write_done = wbc->nr_to_write <= 0;
  3128. }
  3129. pagevec_release(&pvec);
  3130. cond_resched();
  3131. }
  3132. if (!scanned && !done) {
  3133. /*
  3134. * We hit the last page and there is more work to be done: wrap
  3135. * back to the start of the file
  3136. */
  3137. scanned = 1;
  3138. index = 0;
  3139. goto retry;
  3140. }
  3141. btrfs_add_delayed_iput(inode);
  3142. return ret;
  3143. }
  3144. static void flush_epd_write_bio(struct extent_page_data *epd)
  3145. {
  3146. if (epd->bio) {
  3147. int rw = WRITE;
  3148. int ret;
  3149. if (epd->sync_io)
  3150. rw = WRITE_SYNC;
  3151. ret = submit_one_bio(rw, epd->bio, 0, epd->bio_flags);
  3152. BUG_ON(ret < 0); /* -ENOMEM */
  3153. epd->bio = NULL;
  3154. }
  3155. }
  3156. static noinline void flush_write_bio(void *data)
  3157. {
  3158. struct extent_page_data *epd = data;
  3159. flush_epd_write_bio(epd);
  3160. }
  3161. int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
  3162. get_extent_t *get_extent,
  3163. struct writeback_control *wbc)
  3164. {
  3165. int ret;
  3166. struct extent_page_data epd = {
  3167. .bio = NULL,
  3168. .tree = tree,
  3169. .get_extent = get_extent,
  3170. .extent_locked = 0,
  3171. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  3172. .bio_flags = 0,
  3173. };
  3174. ret = __extent_writepage(page, wbc, &epd);
  3175. flush_epd_write_bio(&epd);
  3176. return ret;
  3177. }
  3178. int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
  3179. u64 start, u64 end, get_extent_t *get_extent,
  3180. int mode)
  3181. {
  3182. int ret = 0;
  3183. struct address_space *mapping = inode->i_mapping;
  3184. struct page *page;
  3185. unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
  3186. PAGE_CACHE_SHIFT;
  3187. struct extent_page_data epd = {
  3188. .bio = NULL,
  3189. .tree = tree,
  3190. .get_extent = get_extent,
  3191. .extent_locked = 1,
  3192. .sync_io = mode == WB_SYNC_ALL,
  3193. .bio_flags = 0,
  3194. };
  3195. struct writeback_control wbc_writepages = {
  3196. .sync_mode = mode,
  3197. .nr_to_write = nr_pages * 2,
  3198. .range_start = start,
  3199. .range_end = end + 1,
  3200. };
  3201. while (start <= end) {
  3202. page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
  3203. if (clear_page_dirty_for_io(page))
  3204. ret = __extent_writepage(page, &wbc_writepages, &epd);
  3205. else {
  3206. if (tree->ops && tree->ops->writepage_end_io_hook)
  3207. tree->ops->writepage_end_io_hook(page, start,
  3208. start + PAGE_CACHE_SIZE - 1,
  3209. NULL, 1);
  3210. unlock_page(page);
  3211. }
  3212. page_cache_release(page);
  3213. start += PAGE_CACHE_SIZE;
  3214. }
  3215. flush_epd_write_bio(&epd);
  3216. return ret;
  3217. }
  3218. int extent_writepages(struct extent_io_tree *tree,
  3219. struct address_space *mapping,
  3220. get_extent_t *get_extent,
  3221. struct writeback_control *wbc)
  3222. {
  3223. int ret = 0;
  3224. struct extent_page_data epd = {
  3225. .bio = NULL,
  3226. .tree = tree,
  3227. .get_extent = get_extent,
  3228. .extent_locked = 0,
  3229. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  3230. .bio_flags = 0,
  3231. };
  3232. ret = extent_write_cache_pages(tree, mapping, wbc,
  3233. __extent_writepage, &epd,
  3234. flush_write_bio);
  3235. flush_epd_write_bio(&epd);
  3236. return ret;
  3237. }
  3238. int extent_readpages(struct extent_io_tree *tree,
  3239. struct address_space *mapping,
  3240. struct list_head *pages, unsigned nr_pages,
  3241. get_extent_t get_extent)
  3242. {
  3243. struct bio *bio = NULL;
  3244. unsigned page_idx;
  3245. unsigned long bio_flags = 0;
  3246. struct page *pagepool[16];
  3247. struct page *page;
  3248. int i = 0;
  3249. int nr = 0;
  3250. for (page_idx = 0; page_idx < nr_pages; page_idx++) {
  3251. page = list_entry(pages->prev, struct page, lru);
  3252. prefetchw(&page->flags);
  3253. list_del(&page->lru);
  3254. if (add_to_page_cache_lru(page, mapping,
  3255. page->index, GFP_NOFS)) {
  3256. page_cache_release(page);
  3257. continue;
  3258. }
  3259. pagepool[nr++] = page;
  3260. if (nr < ARRAY_SIZE(pagepool))
  3261. continue;
  3262. for (i = 0; i < nr; i++) {
  3263. __extent_read_full_page(tree, pagepool[i], get_extent,
  3264. &bio, 0, &bio_flags);
  3265. page_cache_release(pagepool[i]);
  3266. }
  3267. nr = 0;
  3268. }
  3269. for (i = 0; i < nr; i++) {
  3270. __extent_read_full_page(tree, pagepool[i], get_extent,
  3271. &bio, 0, &bio_flags);
  3272. page_cache_release(pagepool[i]);
  3273. }
  3274. BUG_ON(!list_empty(pages));
  3275. if (bio)
  3276. return submit_one_bio(READ, bio, 0, bio_flags);
  3277. return 0;
  3278. }
  3279. /*
  3280. * basic invalidatepage code, this waits on any locked or writeback
  3281. * ranges corresponding to the page, and then deletes any extent state
  3282. * records from the tree
  3283. */
  3284. int extent_invalidatepage(struct extent_io_tree *tree,
  3285. struct page *page, unsigned long offset)
  3286. {
  3287. struct extent_state *cached_state = NULL;
  3288. u64 start = page_offset(page);
  3289. u64 end = start + PAGE_CACHE_SIZE - 1;
  3290. size_t blocksize = page->mapping->host->i_sb->s_blocksize;
  3291. start += (offset + blocksize - 1) & ~(blocksize - 1);
  3292. if (start > end)
  3293. return 0;
  3294. lock_extent_bits(tree, start, end, 0, &cached_state);
  3295. wait_on_page_writeback(page);
  3296. clear_extent_bit(tree, start, end,
  3297. EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
  3298. EXTENT_DO_ACCOUNTING,
  3299. 1, 1, &cached_state, GFP_NOFS);
  3300. return 0;
  3301. }
  3302. /*
  3303. * a helper for releasepage, this tests for areas of the page that
  3304. * are locked or under IO and drops the related state bits if it is safe
  3305. * to drop the page.
  3306. */
  3307. int try_release_extent_state(struct extent_map_tree *map,
  3308. struct extent_io_tree *tree, struct page *page,
  3309. gfp_t mask)
  3310. {
  3311. u64 start = page_offset(page);
  3312. u64 end = start + PAGE_CACHE_SIZE - 1;
  3313. int ret = 1;
  3314. if (test_range_bit(tree, start, end,
  3315. EXTENT_IOBITS, 0, NULL))
  3316. ret = 0;
  3317. else {
  3318. if ((mask & GFP_NOFS) == GFP_NOFS)
  3319. mask = GFP_NOFS;
  3320. /*
  3321. * at this point we can safely clear everything except the
  3322. * locked bit and the nodatasum bit
  3323. */
  3324. ret = clear_extent_bit(tree, start, end,
  3325. ~(EXTENT_LOCKED | EXTENT_NODATASUM),
  3326. 0, 0, NULL, mask);
  3327. /* if clear_extent_bit failed for enomem reasons,
  3328. * we can't allow the release to continue.
  3329. */
  3330. if (ret < 0)
  3331. ret = 0;
  3332. else
  3333. ret = 1;
  3334. }
  3335. return ret;
  3336. }
  3337. /*
  3338. * a helper for releasepage. As long as there are no locked extents
  3339. * in the range corresponding to the page, both state records and extent
  3340. * map records are removed
  3341. */
  3342. int try_release_extent_mapping(struct extent_map_tree *map,
  3343. struct extent_io_tree *tree, struct page *page,
  3344. gfp_t mask)
  3345. {
  3346. struct extent_map *em;
  3347. u64 start = page_offset(page);
  3348. u64 end = start + PAGE_CACHE_SIZE - 1;
  3349. if ((mask & __GFP_WAIT) &&
  3350. page->mapping->host->i_size > 16 * 1024 * 1024) {
  3351. u64 len;
  3352. while (start <= end) {
  3353. len = end - start + 1;
  3354. write_lock(&map->lock);
  3355. em = lookup_extent_mapping(map, start, len);
  3356. if (!em) {
  3357. write_unlock(&map->lock);
  3358. break;
  3359. }
  3360. if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
  3361. em->start != start) {
  3362. write_unlock(&map->lock);
  3363. free_extent_map(em);
  3364. break;
  3365. }
  3366. if (!test_range_bit(tree, em->start,
  3367. extent_map_end(em) - 1,
  3368. EXTENT_LOCKED | EXTENT_WRITEBACK,
  3369. 0, NULL)) {
  3370. remove_extent_mapping(map, em);
  3371. /* once for the rb tree */
  3372. free_extent_map(em);
  3373. }
  3374. start = extent_map_end(em);
  3375. write_unlock(&map->lock);
  3376. /* once for us */
  3377. free_extent_map(em);
  3378. }
  3379. }
  3380. return try_release_extent_state(map, tree, page, mask);
  3381. }
  3382. /*
  3383. * helper function for fiemap, which doesn't want to see any holes.
  3384. * This maps until we find something past 'last'
  3385. */
  3386. static struct extent_map *get_extent_skip_holes(struct inode *inode,
  3387. u64 offset,
  3388. u64 last,
  3389. get_extent_t *get_extent)
  3390. {
  3391. u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
  3392. struct extent_map *em;
  3393. u64 len;
  3394. if (offset >= last)
  3395. return NULL;
  3396. while(1) {
  3397. len = last - offset;
  3398. if (len == 0)
  3399. break;
  3400. len = (len + sectorsize - 1) & ~(sectorsize - 1);
  3401. em = get_extent(inode, NULL, 0, offset, len, 0);
  3402. if (IS_ERR_OR_NULL(em))
  3403. return em;
  3404. /* if this isn't a hole return it */
  3405. if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
  3406. em->block_start != EXTENT_MAP_HOLE) {
  3407. return em;
  3408. }
  3409. /* this is a hole, advance to the next extent */
  3410. offset = extent_map_end(em);
  3411. free_extent_map(em);
  3412. if (offset >= last)
  3413. break;
  3414. }
  3415. return NULL;
  3416. }
  3417. int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  3418. __u64 start, __u64 len, get_extent_t *get_extent)
  3419. {
  3420. int ret = 0;
  3421. u64 off = start;
  3422. u64 max = start + len;
  3423. u32 flags = 0;
  3424. u32 found_type;
  3425. u64 last;
  3426. u64 last_for_get_extent = 0;
  3427. u64 disko = 0;
  3428. u64 isize = i_size_read(inode);
  3429. struct btrfs_key found_key;
  3430. struct extent_map *em = NULL;
  3431. struct extent_state *cached_state = NULL;
  3432. struct btrfs_path *path;
  3433. struct btrfs_file_extent_item *item;
  3434. int end = 0;
  3435. u64 em_start = 0;
  3436. u64 em_len = 0;
  3437. u64 em_end = 0;
  3438. unsigned long emflags;
  3439. if (len == 0)
  3440. return -EINVAL;
  3441. path = btrfs_alloc_path();
  3442. if (!path)
  3443. return -ENOMEM;
  3444. path->leave_spinning = 1;
  3445. start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
  3446. len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
  3447. /*
  3448. * lookup the last file extent. We're not using i_size here
  3449. * because there might be preallocation past i_size
  3450. */
  3451. ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
  3452. path, btrfs_ino(inode), -1, 0);
  3453. if (ret < 0) {
  3454. btrfs_free_path(path);
  3455. return ret;
  3456. }
  3457. WARN_ON(!ret);
  3458. path->slots[0]--;
  3459. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3460. struct btrfs_file_extent_item);
  3461. btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
  3462. found_type = btrfs_key_type(&found_key);
  3463. /* No extents, but there might be delalloc bits */
  3464. if (found_key.objectid != btrfs_ino(inode) ||
  3465. found_type != BTRFS_EXTENT_DATA_KEY) {
  3466. /* have to trust i_size as the end */
  3467. last = (u64)-1;
  3468. last_for_get_extent = isize;
  3469. } else {
  3470. /*
  3471. * remember the start of the last extent. There are a
  3472. * bunch of different factors that go into the length of the
  3473. * extent, so its much less complex to remember where it started
  3474. */
  3475. last = found_key.offset;
  3476. last_for_get_extent = last + 1;
  3477. }
  3478. btrfs_free_path(path);
  3479. /*
  3480. * we might have some extents allocated but more delalloc past those
  3481. * extents. so, we trust isize unless the start of the last extent is
  3482. * beyond isize
  3483. */
  3484. if (last < isize) {
  3485. last = (u64)-1;
  3486. last_for_get_extent = isize;
  3487. }
  3488. lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
  3489. &cached_state);
  3490. em = get_extent_skip_holes(inode, start, last_for_get_extent,
  3491. get_extent);
  3492. if (!em)
  3493. goto out;
  3494. if (IS_ERR(em)) {
  3495. ret = PTR_ERR(em);
  3496. goto out;
  3497. }
  3498. while (!end) {
  3499. u64 offset_in_extent;
  3500. /* break if the extent we found is outside the range */
  3501. if (em->start >= max || extent_map_end(em) < off)
  3502. break;
  3503. /*
  3504. * get_extent may return an extent that starts before our
  3505. * requested range. We have to make sure the ranges
  3506. * we return to fiemap always move forward and don't
  3507. * overlap, so adjust the offsets here
  3508. */
  3509. em_start = max(em->start, off);
  3510. /*
  3511. * record the offset from the start of the extent
  3512. * for adjusting the disk offset below
  3513. */
  3514. offset_in_extent = em_start - em->start;
  3515. em_end = extent_map_end(em);
  3516. em_len = em_end - em_start;
  3517. emflags = em->flags;
  3518. disko = 0;
  3519. flags = 0;
  3520. /*
  3521. * bump off for our next call to get_extent
  3522. */
  3523. off = extent_map_end(em);
  3524. if (off >= max)
  3525. end = 1;
  3526. if (em->block_start == EXTENT_MAP_LAST_BYTE) {
  3527. end = 1;
  3528. flags |= FIEMAP_EXTENT_LAST;
  3529. } else if (em->block_start == EXTENT_MAP_INLINE) {
  3530. flags |= (FIEMAP_EXTENT_DATA_INLINE |
  3531. FIEMAP_EXTENT_NOT_ALIGNED);
  3532. } else if (em->block_start == EXTENT_MAP_DELALLOC) {
  3533. flags |= (FIEMAP_EXTENT_DELALLOC |
  3534. FIEMAP_EXTENT_UNKNOWN);
  3535. } else {
  3536. disko = em->block_start + offset_in_extent;
  3537. }
  3538. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
  3539. flags |= FIEMAP_EXTENT_ENCODED;
  3540. free_extent_map(em);
  3541. em = NULL;
  3542. if ((em_start >= last) || em_len == (u64)-1 ||
  3543. (last == (u64)-1 && isize <= em_end)) {
  3544. flags |= FIEMAP_EXTENT_LAST;
  3545. end = 1;
  3546. }
  3547. /* now scan forward to see if this is really the last extent. */
  3548. em = get_extent_skip_holes(inode, off, last_for_get_extent,
  3549. get_extent);
  3550. if (IS_ERR(em)) {
  3551. ret = PTR_ERR(em);
  3552. goto out;
  3553. }
  3554. if (!em) {
  3555. flags |= FIEMAP_EXTENT_LAST;
  3556. end = 1;
  3557. }
  3558. ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
  3559. em_len, flags);
  3560. if (ret)
  3561. goto out_free;
  3562. }
  3563. out_free:
  3564. free_extent_map(em);
  3565. out:
  3566. unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
  3567. &cached_state, GFP_NOFS);
  3568. return ret;
  3569. }
  3570. static void __free_extent_buffer(struct extent_buffer *eb)
  3571. {
  3572. #if LEAK_DEBUG
  3573. unsigned long flags;
  3574. spin_lock_irqsave(&leak_lock, flags);
  3575. list_del(&eb->leak_list);
  3576. spin_unlock_irqrestore(&leak_lock, flags);
  3577. #endif
  3578. if (eb->pages && eb->pages != eb->inline_pages)
  3579. kfree(eb->pages);
  3580. kmem_cache_free(extent_buffer_cache, eb);
  3581. }
  3582. static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
  3583. u64 start,
  3584. unsigned long len,
  3585. gfp_t mask)
  3586. {
  3587. struct extent_buffer *eb = NULL;
  3588. #if LEAK_DEBUG
  3589. unsigned long flags;
  3590. #endif
  3591. eb = kmem_cache_zalloc(extent_buffer_cache, mask);
  3592. if (eb == NULL)
  3593. return NULL;
  3594. eb->start = start;
  3595. eb->len = len;
  3596. eb->tree = tree;
  3597. eb->bflags = 0;
  3598. rwlock_init(&eb->lock);
  3599. atomic_set(&eb->write_locks, 0);
  3600. atomic_set(&eb->read_locks, 0);
  3601. atomic_set(&eb->blocking_readers, 0);
  3602. atomic_set(&eb->blocking_writers, 0);
  3603. atomic_set(&eb->spinning_readers, 0);
  3604. atomic_set(&eb->spinning_writers, 0);
  3605. eb->lock_nested = 0;
  3606. init_waitqueue_head(&eb->write_lock_wq);
  3607. init_waitqueue_head(&eb->read_lock_wq);
  3608. #if LEAK_DEBUG
  3609. spin_lock_irqsave(&leak_lock, flags);
  3610. list_add(&eb->leak_list, &buffers);
  3611. spin_unlock_irqrestore(&leak_lock, flags);
  3612. #endif
  3613. spin_lock_init(&eb->refs_lock);
  3614. atomic_set(&eb->refs, 1);
  3615. atomic_set(&eb->io_pages, 0);
  3616. if (len > MAX_INLINE_EXTENT_BUFFER_SIZE) {
  3617. struct page **pages;
  3618. int num_pages = (len + PAGE_CACHE_SIZE - 1) >>
  3619. PAGE_CACHE_SHIFT;
  3620. pages = kzalloc(num_pages, mask);
  3621. if (!pages) {
  3622. __free_extent_buffer(eb);
  3623. return NULL;
  3624. }
  3625. eb->pages = pages;
  3626. } else {
  3627. eb->pages = eb->inline_pages;
  3628. }
  3629. return eb;
  3630. }
  3631. struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
  3632. {
  3633. unsigned long i;
  3634. struct page *p;
  3635. struct extent_buffer *new;
  3636. unsigned long num_pages = num_extent_pages(src->start, src->len);
  3637. new = __alloc_extent_buffer(NULL, src->start, src->len, GFP_ATOMIC);
  3638. if (new == NULL)
  3639. return NULL;
  3640. for (i = 0; i < num_pages; i++) {
  3641. p = alloc_page(GFP_ATOMIC);
  3642. BUG_ON(!p);
  3643. attach_extent_buffer_page(new, p);
  3644. WARN_ON(PageDirty(p));
  3645. SetPageUptodate(p);
  3646. new->pages[i] = p;
  3647. }
  3648. copy_extent_buffer(new, src, 0, 0, src->len);
  3649. set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
  3650. set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
  3651. return new;
  3652. }
  3653. struct extent_buffer *alloc_dummy_extent_buffer(u64 start, unsigned long len)
  3654. {
  3655. struct extent_buffer *eb;
  3656. unsigned long num_pages = num_extent_pages(0, len);
  3657. unsigned long i;
  3658. eb = __alloc_extent_buffer(NULL, start, len, GFP_ATOMIC);
  3659. if (!eb)
  3660. return NULL;
  3661. for (i = 0; i < num_pages; i++) {
  3662. eb->pages[i] = alloc_page(GFP_ATOMIC);
  3663. if (!eb->pages[i])
  3664. goto err;
  3665. }
  3666. set_extent_buffer_uptodate(eb);
  3667. btrfs_set_header_nritems(eb, 0);
  3668. set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
  3669. return eb;
  3670. err:
  3671. for (; i > 0; i--)
  3672. __free_page(eb->pages[i - 1]);
  3673. __free_extent_buffer(eb);
  3674. return NULL;
  3675. }
  3676. static int extent_buffer_under_io(struct extent_buffer *eb)
  3677. {
  3678. return (atomic_read(&eb->io_pages) ||
  3679. test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
  3680. test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
  3681. }
  3682. /*
  3683. * Helper for releasing extent buffer page.
  3684. */
  3685. static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
  3686. unsigned long start_idx)
  3687. {
  3688. unsigned long index;
  3689. unsigned long num_pages;
  3690. struct page *page;
  3691. int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
  3692. BUG_ON(extent_buffer_under_io(eb));
  3693. num_pages = num_extent_pages(eb->start, eb->len);
  3694. index = start_idx + num_pages;
  3695. if (start_idx >= index)
  3696. return;
  3697. do {
  3698. index--;
  3699. page = extent_buffer_page(eb, index);
  3700. if (page && mapped) {
  3701. spin_lock(&page->mapping->private_lock);
  3702. /*
  3703. * We do this since we'll remove the pages after we've
  3704. * removed the eb from the radix tree, so we could race
  3705. * and have this page now attached to the new eb. So
  3706. * only clear page_private if it's still connected to
  3707. * this eb.
  3708. */
  3709. if (PagePrivate(page) &&
  3710. page->private == (unsigned long)eb) {
  3711. BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
  3712. BUG_ON(PageDirty(page));
  3713. BUG_ON(PageWriteback(page));
  3714. /*
  3715. * We need to make sure we haven't be attached
  3716. * to a new eb.
  3717. */
  3718. ClearPagePrivate(page);
  3719. set_page_private(page, 0);
  3720. /* One for the page private */
  3721. page_cache_release(page);
  3722. }
  3723. spin_unlock(&page->mapping->private_lock);
  3724. }
  3725. if (page) {
  3726. /* One for when we alloced the page */
  3727. page_cache_release(page);
  3728. }
  3729. } while (index != start_idx);
  3730. }
  3731. /*
  3732. * Helper for releasing the extent buffer.
  3733. */
  3734. static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
  3735. {
  3736. btrfs_release_extent_buffer_page(eb, 0);
  3737. __free_extent_buffer(eb);
  3738. }
  3739. static void check_buffer_tree_ref(struct extent_buffer *eb)
  3740. {
  3741. /* the ref bit is tricky. We have to make sure it is set
  3742. * if we have the buffer dirty. Otherwise the
  3743. * code to free a buffer can end up dropping a dirty
  3744. * page
  3745. *
  3746. * Once the ref bit is set, it won't go away while the
  3747. * buffer is dirty or in writeback, and it also won't
  3748. * go away while we have the reference count on the
  3749. * eb bumped.
  3750. *
  3751. * We can't just set the ref bit without bumping the
  3752. * ref on the eb because free_extent_buffer might
  3753. * see the ref bit and try to clear it. If this happens
  3754. * free_extent_buffer might end up dropping our original
  3755. * ref by mistake and freeing the page before we are able
  3756. * to add one more ref.
  3757. *
  3758. * So bump the ref count first, then set the bit. If someone
  3759. * beat us to it, drop the ref we added.
  3760. */
  3761. spin_lock(&eb->refs_lock);
  3762. if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  3763. atomic_inc(&eb->refs);
  3764. spin_unlock(&eb->refs_lock);
  3765. }
  3766. static void mark_extent_buffer_accessed(struct extent_buffer *eb)
  3767. {
  3768. unsigned long num_pages, i;
  3769. check_buffer_tree_ref(eb);
  3770. num_pages = num_extent_pages(eb->start, eb->len);
  3771. for (i = 0; i < num_pages; i++) {
  3772. struct page *p = extent_buffer_page(eb, i);
  3773. mark_page_accessed(p);
  3774. }
  3775. }
  3776. struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
  3777. u64 start, unsigned long len)
  3778. {
  3779. unsigned long num_pages = num_extent_pages(start, len);
  3780. unsigned long i;
  3781. unsigned long index = start >> PAGE_CACHE_SHIFT;
  3782. struct extent_buffer *eb;
  3783. struct extent_buffer *exists = NULL;
  3784. struct page *p;
  3785. struct address_space *mapping = tree->mapping;
  3786. int uptodate = 1;
  3787. int ret;
  3788. rcu_read_lock();
  3789. eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
  3790. if (eb && atomic_inc_not_zero(&eb->refs)) {
  3791. rcu_read_unlock();
  3792. mark_extent_buffer_accessed(eb);
  3793. return eb;
  3794. }
  3795. rcu_read_unlock();
  3796. eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
  3797. if (!eb)
  3798. return NULL;
  3799. for (i = 0; i < num_pages; i++, index++) {
  3800. p = find_or_create_page(mapping, index, GFP_NOFS);
  3801. if (!p)
  3802. goto free_eb;
  3803. spin_lock(&mapping->private_lock);
  3804. if (PagePrivate(p)) {
  3805. /*
  3806. * We could have already allocated an eb for this page
  3807. * and attached one so lets see if we can get a ref on
  3808. * the existing eb, and if we can we know it's good and
  3809. * we can just return that one, else we know we can just
  3810. * overwrite page->private.
  3811. */
  3812. exists = (struct extent_buffer *)p->private;
  3813. if (atomic_inc_not_zero(&exists->refs)) {
  3814. spin_unlock(&mapping->private_lock);
  3815. unlock_page(p);
  3816. page_cache_release(p);
  3817. mark_extent_buffer_accessed(exists);
  3818. goto free_eb;
  3819. }
  3820. /*
  3821. * Do this so attach doesn't complain and we need to
  3822. * drop the ref the old guy had.
  3823. */
  3824. ClearPagePrivate(p);
  3825. WARN_ON(PageDirty(p));
  3826. page_cache_release(p);
  3827. }
  3828. attach_extent_buffer_page(eb, p);
  3829. spin_unlock(&mapping->private_lock);
  3830. WARN_ON(PageDirty(p));
  3831. mark_page_accessed(p);
  3832. eb->pages[i] = p;
  3833. if (!PageUptodate(p))
  3834. uptodate = 0;
  3835. /*
  3836. * see below about how we avoid a nasty race with release page
  3837. * and why we unlock later
  3838. */
  3839. }
  3840. if (uptodate)
  3841. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  3842. again:
  3843. ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
  3844. if (ret)
  3845. goto free_eb;
  3846. spin_lock(&tree->buffer_lock);
  3847. ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
  3848. if (ret == -EEXIST) {
  3849. exists = radix_tree_lookup(&tree->buffer,
  3850. start >> PAGE_CACHE_SHIFT);
  3851. if (!atomic_inc_not_zero(&exists->refs)) {
  3852. spin_unlock(&tree->buffer_lock);
  3853. radix_tree_preload_end();
  3854. exists = NULL;
  3855. goto again;
  3856. }
  3857. spin_unlock(&tree->buffer_lock);
  3858. radix_tree_preload_end();
  3859. mark_extent_buffer_accessed(exists);
  3860. goto free_eb;
  3861. }
  3862. /* add one reference for the tree */
  3863. check_buffer_tree_ref(eb);
  3864. spin_unlock(&tree->buffer_lock);
  3865. radix_tree_preload_end();
  3866. /*
  3867. * there is a race where release page may have
  3868. * tried to find this extent buffer in the radix
  3869. * but failed. It will tell the VM it is safe to
  3870. * reclaim the, and it will clear the page private bit.
  3871. * We must make sure to set the page private bit properly
  3872. * after the extent buffer is in the radix tree so
  3873. * it doesn't get lost
  3874. */
  3875. SetPageChecked(eb->pages[0]);
  3876. for (i = 1; i < num_pages; i++) {
  3877. p = extent_buffer_page(eb, i);
  3878. ClearPageChecked(p);
  3879. unlock_page(p);
  3880. }
  3881. unlock_page(eb->pages[0]);
  3882. return eb;
  3883. free_eb:
  3884. for (i = 0; i < num_pages; i++) {
  3885. if (eb->pages[i])
  3886. unlock_page(eb->pages[i]);
  3887. }
  3888. WARN_ON(!atomic_dec_and_test(&eb->refs));
  3889. btrfs_release_extent_buffer(eb);
  3890. return exists;
  3891. }
  3892. struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
  3893. u64 start, unsigned long len)
  3894. {
  3895. struct extent_buffer *eb;
  3896. rcu_read_lock();
  3897. eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
  3898. if (eb && atomic_inc_not_zero(&eb->refs)) {
  3899. rcu_read_unlock();
  3900. mark_extent_buffer_accessed(eb);
  3901. return eb;
  3902. }
  3903. rcu_read_unlock();
  3904. return NULL;
  3905. }
  3906. static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
  3907. {
  3908. struct extent_buffer *eb =
  3909. container_of(head, struct extent_buffer, rcu_head);
  3910. __free_extent_buffer(eb);
  3911. }
  3912. /* Expects to have eb->eb_lock already held */
  3913. static int release_extent_buffer(struct extent_buffer *eb, gfp_t mask)
  3914. {
  3915. WARN_ON(atomic_read(&eb->refs) == 0);
  3916. if (atomic_dec_and_test(&eb->refs)) {
  3917. if (test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) {
  3918. spin_unlock(&eb->refs_lock);
  3919. } else {
  3920. struct extent_io_tree *tree = eb->tree;
  3921. spin_unlock(&eb->refs_lock);
  3922. spin_lock(&tree->buffer_lock);
  3923. radix_tree_delete(&tree->buffer,
  3924. eb->start >> PAGE_CACHE_SHIFT);
  3925. spin_unlock(&tree->buffer_lock);
  3926. }
  3927. /* Should be safe to release our pages at this point */
  3928. btrfs_release_extent_buffer_page(eb, 0);
  3929. call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
  3930. return 1;
  3931. }
  3932. spin_unlock(&eb->refs_lock);
  3933. return 0;
  3934. }
  3935. void free_extent_buffer(struct extent_buffer *eb)
  3936. {
  3937. if (!eb)
  3938. return;
  3939. spin_lock(&eb->refs_lock);
  3940. if (atomic_read(&eb->refs) == 2 &&
  3941. test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
  3942. atomic_dec(&eb->refs);
  3943. if (atomic_read(&eb->refs) == 2 &&
  3944. test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
  3945. !extent_buffer_under_io(eb) &&
  3946. test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  3947. atomic_dec(&eb->refs);
  3948. /*
  3949. * I know this is terrible, but it's temporary until we stop tracking
  3950. * the uptodate bits and such for the extent buffers.
  3951. */
  3952. release_extent_buffer(eb, GFP_ATOMIC);
  3953. }
  3954. void free_extent_buffer_stale(struct extent_buffer *eb)
  3955. {
  3956. if (!eb)
  3957. return;
  3958. spin_lock(&eb->refs_lock);
  3959. set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
  3960. if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
  3961. test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  3962. atomic_dec(&eb->refs);
  3963. release_extent_buffer(eb, GFP_NOFS);
  3964. }
  3965. void clear_extent_buffer_dirty(struct extent_buffer *eb)
  3966. {
  3967. unsigned long i;
  3968. unsigned long num_pages;
  3969. struct page *page;
  3970. num_pages = num_extent_pages(eb->start, eb->len);
  3971. for (i = 0; i < num_pages; i++) {
  3972. page = extent_buffer_page(eb, i);
  3973. if (!PageDirty(page))
  3974. continue;
  3975. lock_page(page);
  3976. WARN_ON(!PagePrivate(page));
  3977. clear_page_dirty_for_io(page);
  3978. spin_lock_irq(&page->mapping->tree_lock);
  3979. if (!PageDirty(page)) {
  3980. radix_tree_tag_clear(&page->mapping->page_tree,
  3981. page_index(page),
  3982. PAGECACHE_TAG_DIRTY);
  3983. }
  3984. spin_unlock_irq(&page->mapping->tree_lock);
  3985. ClearPageError(page);
  3986. unlock_page(page);
  3987. }
  3988. WARN_ON(atomic_read(&eb->refs) == 0);
  3989. }
  3990. int set_extent_buffer_dirty(struct extent_buffer *eb)
  3991. {
  3992. unsigned long i;
  3993. unsigned long num_pages;
  3994. int was_dirty = 0;
  3995. check_buffer_tree_ref(eb);
  3996. was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
  3997. num_pages = num_extent_pages(eb->start, eb->len);
  3998. WARN_ON(atomic_read(&eb->refs) == 0);
  3999. WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
  4000. for (i = 0; i < num_pages; i++)
  4001. set_page_dirty(extent_buffer_page(eb, i));
  4002. return was_dirty;
  4003. }
  4004. static int range_straddles_pages(u64 start, u64 len)
  4005. {
  4006. if (len < PAGE_CACHE_SIZE)
  4007. return 1;
  4008. if (start & (PAGE_CACHE_SIZE - 1))
  4009. return 1;
  4010. if ((start + len) & (PAGE_CACHE_SIZE - 1))
  4011. return 1;
  4012. return 0;
  4013. }
  4014. int clear_extent_buffer_uptodate(struct extent_buffer *eb)
  4015. {
  4016. unsigned long i;
  4017. struct page *page;
  4018. unsigned long num_pages;
  4019. clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4020. num_pages = num_extent_pages(eb->start, eb->len);
  4021. for (i = 0; i < num_pages; i++) {
  4022. page = extent_buffer_page(eb, i);
  4023. if (page)
  4024. ClearPageUptodate(page);
  4025. }
  4026. return 0;
  4027. }
  4028. int set_extent_buffer_uptodate(struct extent_buffer *eb)
  4029. {
  4030. unsigned long i;
  4031. struct page *page;
  4032. unsigned long num_pages;
  4033. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4034. num_pages = num_extent_pages(eb->start, eb->len);
  4035. for (i = 0; i < num_pages; i++) {
  4036. page = extent_buffer_page(eb, i);
  4037. SetPageUptodate(page);
  4038. }
  4039. return 0;
  4040. }
  4041. int extent_range_uptodate(struct extent_io_tree *tree,
  4042. u64 start, u64 end)
  4043. {
  4044. struct page *page;
  4045. int ret;
  4046. int pg_uptodate = 1;
  4047. int uptodate;
  4048. unsigned long index;
  4049. if (range_straddles_pages(start, end - start + 1)) {
  4050. ret = test_range_bit(tree, start, end,
  4051. EXTENT_UPTODATE, 1, NULL);
  4052. if (ret)
  4053. return 1;
  4054. }
  4055. while (start <= end) {
  4056. index = start >> PAGE_CACHE_SHIFT;
  4057. page = find_get_page(tree->mapping, index);
  4058. if (!page)
  4059. return 1;
  4060. uptodate = PageUptodate(page);
  4061. page_cache_release(page);
  4062. if (!uptodate) {
  4063. pg_uptodate = 0;
  4064. break;
  4065. }
  4066. start += PAGE_CACHE_SIZE;
  4067. }
  4068. return pg_uptodate;
  4069. }
  4070. int extent_buffer_uptodate(struct extent_buffer *eb)
  4071. {
  4072. return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4073. }
  4074. int read_extent_buffer_pages(struct extent_io_tree *tree,
  4075. struct extent_buffer *eb, u64 start, int wait,
  4076. get_extent_t *get_extent, int mirror_num)
  4077. {
  4078. unsigned long i;
  4079. unsigned long start_i;
  4080. struct page *page;
  4081. int err;
  4082. int ret = 0;
  4083. int locked_pages = 0;
  4084. int all_uptodate = 1;
  4085. unsigned long num_pages;
  4086. unsigned long num_reads = 0;
  4087. struct bio *bio = NULL;
  4088. unsigned long bio_flags = 0;
  4089. if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
  4090. return 0;
  4091. if (start) {
  4092. WARN_ON(start < eb->start);
  4093. start_i = (start >> PAGE_CACHE_SHIFT) -
  4094. (eb->start >> PAGE_CACHE_SHIFT);
  4095. } else {
  4096. start_i = 0;
  4097. }
  4098. num_pages = num_extent_pages(eb->start, eb->len);
  4099. for (i = start_i; i < num_pages; i++) {
  4100. page = extent_buffer_page(eb, i);
  4101. if (wait == WAIT_NONE) {
  4102. if (!trylock_page(page))
  4103. goto unlock_exit;
  4104. } else {
  4105. lock_page(page);
  4106. }
  4107. locked_pages++;
  4108. if (!PageUptodate(page)) {
  4109. num_reads++;
  4110. all_uptodate = 0;
  4111. }
  4112. }
  4113. if (all_uptodate) {
  4114. if (start_i == 0)
  4115. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4116. goto unlock_exit;
  4117. }
  4118. clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
  4119. eb->read_mirror = 0;
  4120. atomic_set(&eb->io_pages, num_reads);
  4121. for (i = start_i; i < num_pages; i++) {
  4122. page = extent_buffer_page(eb, i);
  4123. if (!PageUptodate(page)) {
  4124. ClearPageError(page);
  4125. err = __extent_read_full_page(tree, page,
  4126. get_extent, &bio,
  4127. mirror_num, &bio_flags);
  4128. if (err)
  4129. ret = err;
  4130. } else {
  4131. unlock_page(page);
  4132. }
  4133. }
  4134. if (bio) {
  4135. err = submit_one_bio(READ, bio, mirror_num, bio_flags);
  4136. if (err)
  4137. return err;
  4138. }
  4139. if (ret || wait != WAIT_COMPLETE)
  4140. return ret;
  4141. for (i = start_i; i < num_pages; i++) {
  4142. page = extent_buffer_page(eb, i);
  4143. wait_on_page_locked(page);
  4144. if (!PageUptodate(page))
  4145. ret = -EIO;
  4146. }
  4147. return ret;
  4148. unlock_exit:
  4149. i = start_i;
  4150. while (locked_pages > 0) {
  4151. page = extent_buffer_page(eb, i);
  4152. i++;
  4153. unlock_page(page);
  4154. locked_pages--;
  4155. }
  4156. return ret;
  4157. }
  4158. void read_extent_buffer(struct extent_buffer *eb, void *dstv,
  4159. unsigned long start,
  4160. unsigned long len)
  4161. {
  4162. size_t cur;
  4163. size_t offset;
  4164. struct page *page;
  4165. char *kaddr;
  4166. char *dst = (char *)dstv;
  4167. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  4168. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  4169. WARN_ON(start > eb->len);
  4170. WARN_ON(start + len > eb->start + eb->len);
  4171. offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
  4172. while (len > 0) {
  4173. page = extent_buffer_page(eb, i);
  4174. cur = min(len, (PAGE_CACHE_SIZE - offset));
  4175. kaddr = page_address(page);
  4176. memcpy(dst, kaddr + offset, cur);
  4177. dst += cur;
  4178. len -= cur;
  4179. offset = 0;
  4180. i++;
  4181. }
  4182. }
  4183. int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
  4184. unsigned long min_len, char **map,
  4185. unsigned long *map_start,
  4186. unsigned long *map_len)
  4187. {
  4188. size_t offset = start & (PAGE_CACHE_SIZE - 1);
  4189. char *kaddr;
  4190. struct page *p;
  4191. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  4192. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  4193. unsigned long end_i = (start_offset + start + min_len - 1) >>
  4194. PAGE_CACHE_SHIFT;
  4195. if (i != end_i)
  4196. return -EINVAL;
  4197. if (i == 0) {
  4198. offset = start_offset;
  4199. *map_start = 0;
  4200. } else {
  4201. offset = 0;
  4202. *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
  4203. }
  4204. if (start + min_len > eb->len) {
  4205. WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
  4206. "wanted %lu %lu\n", (unsigned long long)eb->start,
  4207. eb->len, start, min_len);
  4208. return -EINVAL;
  4209. }
  4210. p = extent_buffer_page(eb, i);
  4211. kaddr = page_address(p);
  4212. *map = kaddr + offset;
  4213. *map_len = PAGE_CACHE_SIZE - offset;
  4214. return 0;
  4215. }
  4216. int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
  4217. unsigned long start,
  4218. unsigned long len)
  4219. {
  4220. size_t cur;
  4221. size_t offset;
  4222. struct page *page;
  4223. char *kaddr;
  4224. char *ptr = (char *)ptrv;
  4225. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  4226. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  4227. int ret = 0;
  4228. WARN_ON(start > eb->len);
  4229. WARN_ON(start + len > eb->start + eb->len);
  4230. offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
  4231. while (len > 0) {
  4232. page = extent_buffer_page(eb, i);
  4233. cur = min(len, (PAGE_CACHE_SIZE - offset));
  4234. kaddr = page_address(page);
  4235. ret = memcmp(ptr, kaddr + offset, cur);
  4236. if (ret)
  4237. break;
  4238. ptr += cur;
  4239. len -= cur;
  4240. offset = 0;
  4241. i++;
  4242. }
  4243. return ret;
  4244. }
  4245. void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
  4246. unsigned long start, unsigned long len)
  4247. {
  4248. size_t cur;
  4249. size_t offset;
  4250. struct page *page;
  4251. char *kaddr;
  4252. char *src = (char *)srcv;
  4253. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  4254. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  4255. WARN_ON(start > eb->len);
  4256. WARN_ON(start + len > eb->start + eb->len);
  4257. offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
  4258. while (len > 0) {
  4259. page = extent_buffer_page(eb, i);
  4260. WARN_ON(!PageUptodate(page));
  4261. cur = min(len, PAGE_CACHE_SIZE - offset);
  4262. kaddr = page_address(page);
  4263. memcpy(kaddr + offset, src, cur);
  4264. src += cur;
  4265. len -= cur;
  4266. offset = 0;
  4267. i++;
  4268. }
  4269. }
  4270. void memset_extent_buffer(struct extent_buffer *eb, char c,
  4271. unsigned long start, unsigned long len)
  4272. {
  4273. size_t cur;
  4274. size_t offset;
  4275. struct page *page;
  4276. char *kaddr;
  4277. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  4278. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  4279. WARN_ON(start > eb->len);
  4280. WARN_ON(start + len > eb->start + eb->len);
  4281. offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
  4282. while (len > 0) {
  4283. page = extent_buffer_page(eb, i);
  4284. WARN_ON(!PageUptodate(page));
  4285. cur = min(len, PAGE_CACHE_SIZE - offset);
  4286. kaddr = page_address(page);
  4287. memset(kaddr + offset, c, cur);
  4288. len -= cur;
  4289. offset = 0;
  4290. i++;
  4291. }
  4292. }
  4293. void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
  4294. unsigned long dst_offset, unsigned long src_offset,
  4295. unsigned long len)
  4296. {
  4297. u64 dst_len = dst->len;
  4298. size_t cur;
  4299. size_t offset;
  4300. struct page *page;
  4301. char *kaddr;
  4302. size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
  4303. unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
  4304. WARN_ON(src->len != dst_len);
  4305. offset = (start_offset + dst_offset) &
  4306. ((unsigned long)PAGE_CACHE_SIZE - 1);
  4307. while (len > 0) {
  4308. page = extent_buffer_page(dst, i);
  4309. WARN_ON(!PageUptodate(page));
  4310. cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
  4311. kaddr = page_address(page);
  4312. read_extent_buffer(src, kaddr + offset, src_offset, cur);
  4313. src_offset += cur;
  4314. len -= cur;
  4315. offset = 0;
  4316. i++;
  4317. }
  4318. }
  4319. static void move_pages(struct page *dst_page, struct page *src_page,
  4320. unsigned long dst_off, unsigned long src_off,
  4321. unsigned long len)
  4322. {
  4323. char *dst_kaddr = page_address(dst_page);
  4324. if (dst_page == src_page) {
  4325. memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
  4326. } else {
  4327. char *src_kaddr = page_address(src_page);
  4328. char *p = dst_kaddr + dst_off + len;
  4329. char *s = src_kaddr + src_off + len;
  4330. while (len--)
  4331. *--p = *--s;
  4332. }
  4333. }
  4334. static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
  4335. {
  4336. unsigned long distance = (src > dst) ? src - dst : dst - src;
  4337. return distance < len;
  4338. }
  4339. static void copy_pages(struct page *dst_page, struct page *src_page,
  4340. unsigned long dst_off, unsigned long src_off,
  4341. unsigned long len)
  4342. {
  4343. char *dst_kaddr = page_address(dst_page);
  4344. char *src_kaddr;
  4345. int must_memmove = 0;
  4346. if (dst_page != src_page) {
  4347. src_kaddr = page_address(src_page);
  4348. } else {
  4349. src_kaddr = dst_kaddr;
  4350. if (areas_overlap(src_off, dst_off, len))
  4351. must_memmove = 1;
  4352. }
  4353. if (must_memmove)
  4354. memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
  4355. else
  4356. memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
  4357. }
  4358. void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
  4359. unsigned long src_offset, unsigned long len)
  4360. {
  4361. size_t cur;
  4362. size_t dst_off_in_page;
  4363. size_t src_off_in_page;
  4364. size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
  4365. unsigned long dst_i;
  4366. unsigned long src_i;
  4367. if (src_offset + len > dst->len) {
  4368. printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
  4369. "len %lu dst len %lu\n", src_offset, len, dst->len);
  4370. BUG_ON(1);
  4371. }
  4372. if (dst_offset + len > dst->len) {
  4373. printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
  4374. "len %lu dst len %lu\n", dst_offset, len, dst->len);
  4375. BUG_ON(1);
  4376. }
  4377. while (len > 0) {
  4378. dst_off_in_page = (start_offset + dst_offset) &
  4379. ((unsigned long)PAGE_CACHE_SIZE - 1);
  4380. src_off_in_page = (start_offset + src_offset) &
  4381. ((unsigned long)PAGE_CACHE_SIZE - 1);
  4382. dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
  4383. src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
  4384. cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
  4385. src_off_in_page));
  4386. cur = min_t(unsigned long, cur,
  4387. (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
  4388. copy_pages(extent_buffer_page(dst, dst_i),
  4389. extent_buffer_page(dst, src_i),
  4390. dst_off_in_page, src_off_in_page, cur);
  4391. src_offset += cur;
  4392. dst_offset += cur;
  4393. len -= cur;
  4394. }
  4395. }
  4396. void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
  4397. unsigned long src_offset, unsigned long len)
  4398. {
  4399. size_t cur;
  4400. size_t dst_off_in_page;
  4401. size_t src_off_in_page;
  4402. unsigned long dst_end = dst_offset + len - 1;
  4403. unsigned long src_end = src_offset + len - 1;
  4404. size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
  4405. unsigned long dst_i;
  4406. unsigned long src_i;
  4407. if (src_offset + len > dst->len) {
  4408. printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
  4409. "len %lu len %lu\n", src_offset, len, dst->len);
  4410. BUG_ON(1);
  4411. }
  4412. if (dst_offset + len > dst->len) {
  4413. printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
  4414. "len %lu len %lu\n", dst_offset, len, dst->len);
  4415. BUG_ON(1);
  4416. }
  4417. if (dst_offset < src_offset) {
  4418. memcpy_extent_buffer(dst, dst_offset, src_offset, len);
  4419. return;
  4420. }
  4421. while (len > 0) {
  4422. dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
  4423. src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
  4424. dst_off_in_page = (start_offset + dst_end) &
  4425. ((unsigned long)PAGE_CACHE_SIZE - 1);
  4426. src_off_in_page = (start_offset + src_end) &
  4427. ((unsigned long)PAGE_CACHE_SIZE - 1);
  4428. cur = min_t(unsigned long, len, src_off_in_page + 1);
  4429. cur = min(cur, dst_off_in_page + 1);
  4430. move_pages(extent_buffer_page(dst, dst_i),
  4431. extent_buffer_page(dst, src_i),
  4432. dst_off_in_page - cur + 1,
  4433. src_off_in_page - cur + 1, cur);
  4434. dst_end -= cur;
  4435. src_end -= cur;
  4436. len -= cur;
  4437. }
  4438. }
  4439. int try_release_extent_buffer(struct page *page, gfp_t mask)
  4440. {
  4441. struct extent_buffer *eb;
  4442. /*
  4443. * We need to make sure noboody is attaching this page to an eb right
  4444. * now.
  4445. */
  4446. spin_lock(&page->mapping->private_lock);
  4447. if (!PagePrivate(page)) {
  4448. spin_unlock(&page->mapping->private_lock);
  4449. return 1;
  4450. }
  4451. eb = (struct extent_buffer *)page->private;
  4452. BUG_ON(!eb);
  4453. /*
  4454. * This is a little awful but should be ok, we need to make sure that
  4455. * the eb doesn't disappear out from under us while we're looking at
  4456. * this page.
  4457. */
  4458. spin_lock(&eb->refs_lock);
  4459. if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
  4460. spin_unlock(&eb->refs_lock);
  4461. spin_unlock(&page->mapping->private_lock);
  4462. return 0;
  4463. }
  4464. spin_unlock(&page->mapping->private_lock);
  4465. if ((mask & GFP_NOFS) == GFP_NOFS)
  4466. mask = GFP_NOFS;
  4467. /*
  4468. * If tree ref isn't set then we know the ref on this eb is a real ref,
  4469. * so just return, this page will likely be freed soon anyway.
  4470. */
  4471. if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
  4472. spin_unlock(&eb->refs_lock);
  4473. return 0;
  4474. }
  4475. return release_extent_buffer(eb, mask);
  4476. }