extent_io.c 126 KB

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