extent_io.c 125 KB

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