extent_io.c 125 KB

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