extent_io.c 126 KB

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