extent_io.c 126 KB

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