extent_io.c 123 KB

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