extent_io.c 125 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061
  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. * When IO fails, either with EIO or csum verification fails, we
  1751. * try other mirrors that might have a good copy of the data. This
  1752. * io_failure_record is used to record state as we go through all the
  1753. * mirrors. If another mirror has good data, the page is set up to date
  1754. * and things continue. If a good mirror can't be found, the original
  1755. * bio end_io callback is called to indicate things have failed.
  1756. */
  1757. struct io_failure_record {
  1758. struct page *page;
  1759. u64 start;
  1760. u64 len;
  1761. u64 logical;
  1762. unsigned long bio_flags;
  1763. int this_mirror;
  1764. int failed_mirror;
  1765. int in_validation;
  1766. };
  1767. static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
  1768. int did_repair)
  1769. {
  1770. int ret;
  1771. int err = 0;
  1772. struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
  1773. set_state_private(failure_tree, rec->start, 0);
  1774. ret = clear_extent_bits(failure_tree, rec->start,
  1775. rec->start + rec->len - 1,
  1776. EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
  1777. if (ret)
  1778. err = ret;
  1779. ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
  1780. rec->start + rec->len - 1,
  1781. EXTENT_DAMAGED, GFP_NOFS);
  1782. if (ret && !err)
  1783. err = ret;
  1784. kfree(rec);
  1785. return err;
  1786. }
  1787. static void repair_io_failure_callback(struct bio *bio, int err)
  1788. {
  1789. complete(bio->bi_private);
  1790. }
  1791. /*
  1792. * this bypasses the standard btrfs submit functions deliberately, as
  1793. * the standard behavior is to write all copies in a raid setup. here we only
  1794. * want to write the one bad copy. so we do the mapping for ourselves and issue
  1795. * submit_bio directly.
  1796. * to avoid any synchronization issues, wait for the data after writing, which
  1797. * actually prevents the read that triggered the error from finishing.
  1798. * currently, there can be no more than two copies of every data bit. thus,
  1799. * exactly one rewrite is required.
  1800. */
  1801. int repair_io_failure(struct btrfs_fs_info *fs_info, u64 start,
  1802. u64 length, u64 logical, struct page *page,
  1803. int mirror_num)
  1804. {
  1805. struct bio *bio;
  1806. struct btrfs_device *dev;
  1807. DECLARE_COMPLETION_ONSTACK(compl);
  1808. u64 map_length = 0;
  1809. u64 sector;
  1810. struct btrfs_bio *bbio = NULL;
  1811. struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
  1812. int ret;
  1813. BUG_ON(!mirror_num);
  1814. /* we can't repair anything in raid56 yet */
  1815. if (btrfs_is_parity_mirror(map_tree, logical, length, mirror_num))
  1816. return 0;
  1817. bio = bio_alloc(GFP_NOFS, 1);
  1818. if (!bio)
  1819. return -EIO;
  1820. bio->bi_private = &compl;
  1821. bio->bi_end_io = repair_io_failure_callback;
  1822. bio->bi_size = 0;
  1823. map_length = length;
  1824. ret = btrfs_map_block(fs_info, WRITE, logical,
  1825. &map_length, &bbio, mirror_num);
  1826. if (ret) {
  1827. bio_put(bio);
  1828. return -EIO;
  1829. }
  1830. BUG_ON(mirror_num != bbio->mirror_num);
  1831. sector = bbio->stripes[mirror_num-1].physical >> 9;
  1832. bio->bi_sector = sector;
  1833. dev = bbio->stripes[mirror_num-1].dev;
  1834. kfree(bbio);
  1835. if (!dev || !dev->bdev || !dev->writeable) {
  1836. bio_put(bio);
  1837. return -EIO;
  1838. }
  1839. bio->bi_bdev = dev->bdev;
  1840. bio_add_page(bio, page, length, start - page_offset(page));
  1841. btrfsic_submit_bio(WRITE_SYNC, bio);
  1842. wait_for_completion(&compl);
  1843. if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
  1844. /* try to remap that extent elsewhere? */
  1845. bio_put(bio);
  1846. btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
  1847. return -EIO;
  1848. }
  1849. printk_ratelimited_in_rcu(KERN_INFO "btrfs read error corrected: ino %lu off %llu "
  1850. "(dev %s sector %llu)\n", page->mapping->host->i_ino,
  1851. start, rcu_str_deref(dev->name), sector);
  1852. bio_put(bio);
  1853. return 0;
  1854. }
  1855. int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
  1856. int mirror_num)
  1857. {
  1858. u64 start = eb->start;
  1859. unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
  1860. int ret = 0;
  1861. for (i = 0; i < num_pages; i++) {
  1862. struct page *p = extent_buffer_page(eb, i);
  1863. ret = repair_io_failure(root->fs_info, start, PAGE_CACHE_SIZE,
  1864. start, p, mirror_num);
  1865. if (ret)
  1866. break;
  1867. start += PAGE_CACHE_SIZE;
  1868. }
  1869. return ret;
  1870. }
  1871. /*
  1872. * each time an IO finishes, we do a fast check in the IO failure tree
  1873. * to see if we need to process or clean up an io_failure_record
  1874. */
  1875. static int clean_io_failure(u64 start, struct page *page)
  1876. {
  1877. u64 private;
  1878. u64 private_failure;
  1879. struct io_failure_record *failrec;
  1880. struct btrfs_fs_info *fs_info;
  1881. struct extent_state *state;
  1882. int num_copies;
  1883. int did_repair = 0;
  1884. int ret;
  1885. struct inode *inode = page->mapping->host;
  1886. private = 0;
  1887. ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
  1888. (u64)-1, 1, EXTENT_DIRTY, 0);
  1889. if (!ret)
  1890. return 0;
  1891. ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
  1892. &private_failure);
  1893. if (ret)
  1894. return 0;
  1895. failrec = (struct io_failure_record *)(unsigned long) private_failure;
  1896. BUG_ON(!failrec->this_mirror);
  1897. if (failrec->in_validation) {
  1898. /* there was no real error, just free the record */
  1899. pr_debug("clean_io_failure: freeing dummy error at %llu\n",
  1900. failrec->start);
  1901. did_repair = 1;
  1902. goto out;
  1903. }
  1904. spin_lock(&BTRFS_I(inode)->io_tree.lock);
  1905. state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
  1906. failrec->start,
  1907. EXTENT_LOCKED);
  1908. spin_unlock(&BTRFS_I(inode)->io_tree.lock);
  1909. if (state && state->start == failrec->start) {
  1910. fs_info = BTRFS_I(inode)->root->fs_info;
  1911. num_copies = btrfs_num_copies(fs_info, failrec->logical,
  1912. failrec->len);
  1913. if (num_copies > 1) {
  1914. ret = repair_io_failure(fs_info, start, failrec->len,
  1915. failrec->logical, page,
  1916. failrec->failed_mirror);
  1917. did_repair = !ret;
  1918. }
  1919. ret = 0;
  1920. }
  1921. out:
  1922. if (!ret)
  1923. ret = free_io_failure(inode, failrec, did_repair);
  1924. return ret;
  1925. }
  1926. /*
  1927. * this is a generic handler for readpage errors (default
  1928. * readpage_io_failed_hook). if other copies exist, read those and write back
  1929. * good data to the failed position. does not investigate in remapping the
  1930. * failed extent elsewhere, hoping the device will be smart enough to do this as
  1931. * needed
  1932. */
  1933. static int bio_readpage_error(struct bio *failed_bio, struct page *page,
  1934. u64 start, u64 end, int failed_mirror,
  1935. struct extent_state *state)
  1936. {
  1937. struct io_failure_record *failrec = NULL;
  1938. u64 private;
  1939. struct extent_map *em;
  1940. struct inode *inode = page->mapping->host;
  1941. struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
  1942. struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
  1943. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  1944. struct bio *bio;
  1945. int num_copies;
  1946. int ret;
  1947. int read_mode;
  1948. u64 logical;
  1949. BUG_ON(failed_bio->bi_rw & REQ_WRITE);
  1950. ret = get_state_private(failure_tree, start, &private);
  1951. if (ret) {
  1952. failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
  1953. if (!failrec)
  1954. return -ENOMEM;
  1955. failrec->start = start;
  1956. failrec->len = end - start + 1;
  1957. failrec->this_mirror = 0;
  1958. failrec->bio_flags = 0;
  1959. failrec->in_validation = 0;
  1960. read_lock(&em_tree->lock);
  1961. em = lookup_extent_mapping(em_tree, start, failrec->len);
  1962. if (!em) {
  1963. read_unlock(&em_tree->lock);
  1964. kfree(failrec);
  1965. return -EIO;
  1966. }
  1967. if (em->start > start || em->start + em->len < start) {
  1968. free_extent_map(em);
  1969. em = NULL;
  1970. }
  1971. read_unlock(&em_tree->lock);
  1972. if (!em) {
  1973. kfree(failrec);
  1974. return -EIO;
  1975. }
  1976. logical = start - em->start;
  1977. logical = em->block_start + logical;
  1978. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  1979. logical = em->block_start;
  1980. failrec->bio_flags = EXTENT_BIO_COMPRESSED;
  1981. extent_set_compress_type(&failrec->bio_flags,
  1982. em->compress_type);
  1983. }
  1984. pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
  1985. "len=%llu\n", logical, start, failrec->len);
  1986. failrec->logical = logical;
  1987. free_extent_map(em);
  1988. /* set the bits in the private failure tree */
  1989. ret = set_extent_bits(failure_tree, start, end,
  1990. EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
  1991. if (ret >= 0)
  1992. ret = set_state_private(failure_tree, start,
  1993. (u64)(unsigned long)failrec);
  1994. /* set the bits in the inode's tree */
  1995. if (ret >= 0)
  1996. ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
  1997. GFP_NOFS);
  1998. if (ret < 0) {
  1999. kfree(failrec);
  2000. return ret;
  2001. }
  2002. } else {
  2003. failrec = (struct io_failure_record *)(unsigned long)private;
  2004. pr_debug("bio_readpage_error: (found) logical=%llu, "
  2005. "start=%llu, len=%llu, validation=%d\n",
  2006. failrec->logical, failrec->start, failrec->len,
  2007. failrec->in_validation);
  2008. /*
  2009. * when data can be on disk more than twice, add to failrec here
  2010. * (e.g. with a list for failed_mirror) to make
  2011. * clean_io_failure() clean all those errors at once.
  2012. */
  2013. }
  2014. num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
  2015. failrec->logical, failrec->len);
  2016. if (num_copies == 1) {
  2017. /*
  2018. * we only have a single copy of the data, so don't bother with
  2019. * all the retry and error correction code that follows. no
  2020. * matter what the error is, it is very likely to persist.
  2021. */
  2022. pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
  2023. "state=%p, num_copies=%d, next_mirror %d, "
  2024. "failed_mirror %d\n", state, num_copies,
  2025. failrec->this_mirror, failed_mirror);
  2026. free_io_failure(inode, failrec, 0);
  2027. return -EIO;
  2028. }
  2029. if (!state) {
  2030. spin_lock(&tree->lock);
  2031. state = find_first_extent_bit_state(tree, failrec->start,
  2032. EXTENT_LOCKED);
  2033. if (state && state->start != failrec->start)
  2034. state = NULL;
  2035. spin_unlock(&tree->lock);
  2036. }
  2037. /*
  2038. * there are two premises:
  2039. * a) deliver good data to the caller
  2040. * b) correct the bad sectors on disk
  2041. */
  2042. if (failed_bio->bi_vcnt > 1) {
  2043. /*
  2044. * to fulfill b), we need to know the exact failing sectors, as
  2045. * we don't want to rewrite any more than the failed ones. thus,
  2046. * we need separate read requests for the failed bio
  2047. *
  2048. * if the following BUG_ON triggers, our validation request got
  2049. * merged. we need separate requests for our algorithm to work.
  2050. */
  2051. BUG_ON(failrec->in_validation);
  2052. failrec->in_validation = 1;
  2053. failrec->this_mirror = failed_mirror;
  2054. read_mode = READ_SYNC | REQ_FAILFAST_DEV;
  2055. } else {
  2056. /*
  2057. * we're ready to fulfill a) and b) alongside. get a good copy
  2058. * of the failed sector and if we succeed, we have setup
  2059. * everything for repair_io_failure to do the rest for us.
  2060. */
  2061. if (failrec->in_validation) {
  2062. BUG_ON(failrec->this_mirror != failed_mirror);
  2063. failrec->in_validation = 0;
  2064. failrec->this_mirror = 0;
  2065. }
  2066. failrec->failed_mirror = failed_mirror;
  2067. failrec->this_mirror++;
  2068. if (failrec->this_mirror == failed_mirror)
  2069. failrec->this_mirror++;
  2070. read_mode = READ_SYNC;
  2071. }
  2072. if (!state || failrec->this_mirror > num_copies) {
  2073. pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
  2074. "next_mirror %d, failed_mirror %d\n", state,
  2075. num_copies, failrec->this_mirror, failed_mirror);
  2076. free_io_failure(inode, failrec, 0);
  2077. return -EIO;
  2078. }
  2079. bio = bio_alloc(GFP_NOFS, 1);
  2080. if (!bio) {
  2081. free_io_failure(inode, failrec, 0);
  2082. return -EIO;
  2083. }
  2084. bio->bi_private = state;
  2085. bio->bi_end_io = failed_bio->bi_end_io;
  2086. bio->bi_sector = failrec->logical >> 9;
  2087. bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
  2088. bio->bi_size = 0;
  2089. bio_add_page(bio, page, failrec->len, start - page_offset(page));
  2090. pr_debug("bio_readpage_error: submitting new read[%#x] to "
  2091. "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
  2092. failrec->this_mirror, num_copies, failrec->in_validation);
  2093. ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
  2094. failrec->this_mirror,
  2095. failrec->bio_flags, 0);
  2096. return ret;
  2097. }
  2098. /* lots and lots of room for performance fixes in the end_bio funcs */
  2099. int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
  2100. {
  2101. int uptodate = (err == 0);
  2102. struct extent_io_tree *tree;
  2103. int ret;
  2104. tree = &BTRFS_I(page->mapping->host)->io_tree;
  2105. if (tree->ops && tree->ops->writepage_end_io_hook) {
  2106. ret = tree->ops->writepage_end_io_hook(page, start,
  2107. end, NULL, uptodate);
  2108. if (ret)
  2109. uptodate = 0;
  2110. }
  2111. if (!uptodate) {
  2112. ClearPageUptodate(page);
  2113. SetPageError(page);
  2114. }
  2115. return 0;
  2116. }
  2117. /*
  2118. * after a writepage IO is done, we need to:
  2119. * clear the uptodate bits on error
  2120. * clear the writeback bits in the extent tree for this IO
  2121. * end_page_writeback if the page has no more pending IO
  2122. *
  2123. * Scheduling is not allowed, so the extent state tree is expected
  2124. * to have one and only one object corresponding to this IO.
  2125. */
  2126. static void end_bio_extent_writepage(struct bio *bio, int err)
  2127. {
  2128. struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
  2129. struct extent_io_tree *tree;
  2130. u64 start;
  2131. u64 end;
  2132. do {
  2133. struct page *page = bvec->bv_page;
  2134. tree = &BTRFS_I(page->mapping->host)->io_tree;
  2135. /* We always issue full-page reads, but if some block
  2136. * in a page fails to read, blk_update_request() will
  2137. * advance bv_offset and adjust bv_len to compensate.
  2138. * Print a warning for nonzero offsets, and an error
  2139. * if they don't add up to a full page. */
  2140. if (bvec->bv_offset || bvec->bv_len != PAGE_CACHE_SIZE)
  2141. printk("%s page write in btrfs with offset %u and length %u\n",
  2142. bvec->bv_offset + bvec->bv_len != PAGE_CACHE_SIZE
  2143. ? KERN_ERR "partial" : KERN_INFO "incomplete",
  2144. bvec->bv_offset, bvec->bv_len);
  2145. start = page_offset(page);
  2146. end = start + bvec->bv_offset + bvec->bv_len - 1;
  2147. if (--bvec >= bio->bi_io_vec)
  2148. prefetchw(&bvec->bv_page->flags);
  2149. if (end_extent_writepage(page, err, start, end))
  2150. continue;
  2151. end_page_writeback(page);
  2152. } while (bvec >= bio->bi_io_vec);
  2153. bio_put(bio);
  2154. }
  2155. /*
  2156. * after a readpage IO is done, we need to:
  2157. * clear the uptodate bits on error
  2158. * set the uptodate bits if things worked
  2159. * set the page up to date if all extents in the tree are uptodate
  2160. * clear the lock bit in the extent tree
  2161. * unlock the page if there are no other extents locked for it
  2162. *
  2163. * Scheduling is not allowed, so the extent state tree is expected
  2164. * to have one and only one object corresponding to this IO.
  2165. */
  2166. static void end_bio_extent_readpage(struct bio *bio, int err)
  2167. {
  2168. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  2169. struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
  2170. struct bio_vec *bvec = bio->bi_io_vec;
  2171. struct extent_io_tree *tree;
  2172. u64 start;
  2173. u64 end;
  2174. int mirror;
  2175. int ret;
  2176. if (err)
  2177. uptodate = 0;
  2178. do {
  2179. struct page *page = bvec->bv_page;
  2180. struct extent_state *cached = NULL;
  2181. struct extent_state *state;
  2182. pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, "
  2183. "mirror=%ld\n", (u64)bio->bi_sector, err,
  2184. (long int)bio->bi_bdev);
  2185. tree = &BTRFS_I(page->mapping->host)->io_tree;
  2186. /* We always issue full-page reads, but if some block
  2187. * in a page fails to read, blk_update_request() will
  2188. * advance bv_offset and adjust bv_len to compensate.
  2189. * Print a warning for nonzero offsets, and an error
  2190. * if they don't add up to a full page. */
  2191. if (bvec->bv_offset || bvec->bv_len != PAGE_CACHE_SIZE)
  2192. printk("%s page read in btrfs with offset %u and length %u\n",
  2193. bvec->bv_offset + bvec->bv_len != PAGE_CACHE_SIZE
  2194. ? KERN_ERR "partial" : KERN_INFO "incomplete",
  2195. bvec->bv_offset, bvec->bv_len);
  2196. start = page_offset(page);
  2197. end = start + bvec->bv_offset + bvec->bv_len - 1;
  2198. if (++bvec <= bvec_end)
  2199. prefetchw(&bvec->bv_page->flags);
  2200. spin_lock(&tree->lock);
  2201. state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
  2202. if (state && state->start == start) {
  2203. /*
  2204. * take a reference on the state, unlock will drop
  2205. * the ref
  2206. */
  2207. cache_state(state, &cached);
  2208. }
  2209. spin_unlock(&tree->lock);
  2210. mirror = (int)(unsigned long)bio->bi_bdev;
  2211. if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
  2212. ret = tree->ops->readpage_end_io_hook(page, start, end,
  2213. state, mirror);
  2214. if (ret)
  2215. uptodate = 0;
  2216. else
  2217. clean_io_failure(start, page);
  2218. }
  2219. if (!uptodate && tree->ops && tree->ops->readpage_io_failed_hook) {
  2220. ret = tree->ops->readpage_io_failed_hook(page, mirror);
  2221. if (!ret && !err &&
  2222. test_bit(BIO_UPTODATE, &bio->bi_flags))
  2223. uptodate = 1;
  2224. } else if (!uptodate) {
  2225. /*
  2226. * The generic bio_readpage_error handles errors the
  2227. * following way: If possible, new read requests are
  2228. * created and submitted and will end up in
  2229. * end_bio_extent_readpage as well (if we're lucky, not
  2230. * in the !uptodate case). In that case it returns 0 and
  2231. * we just go on with the next page in our bio. If it
  2232. * can't handle the error it will return -EIO and we
  2233. * remain responsible for that page.
  2234. */
  2235. ret = bio_readpage_error(bio, page, start, end, mirror, NULL);
  2236. if (ret == 0) {
  2237. uptodate =
  2238. test_bit(BIO_UPTODATE, &bio->bi_flags);
  2239. if (err)
  2240. uptodate = 0;
  2241. uncache_state(&cached);
  2242. continue;
  2243. }
  2244. }
  2245. if (uptodate && tree->track_uptodate) {
  2246. set_extent_uptodate(tree, start, end, &cached,
  2247. GFP_ATOMIC);
  2248. }
  2249. unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
  2250. if (uptodate) {
  2251. SetPageUptodate(page);
  2252. } else {
  2253. ClearPageUptodate(page);
  2254. SetPageError(page);
  2255. }
  2256. unlock_page(page);
  2257. } while (bvec <= bvec_end);
  2258. bio_put(bio);
  2259. }
  2260. struct bio *
  2261. btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
  2262. gfp_t gfp_flags)
  2263. {
  2264. struct bio *bio;
  2265. bio = bio_alloc(gfp_flags, nr_vecs);
  2266. if (bio == NULL && (current->flags & PF_MEMALLOC)) {
  2267. while (!bio && (nr_vecs /= 2))
  2268. bio = bio_alloc(gfp_flags, nr_vecs);
  2269. }
  2270. if (bio) {
  2271. bio->bi_size = 0;
  2272. bio->bi_bdev = bdev;
  2273. bio->bi_sector = first_sector;
  2274. }
  2275. return bio;
  2276. }
  2277. static int __must_check submit_one_bio(int rw, struct bio *bio,
  2278. int mirror_num, unsigned long bio_flags)
  2279. {
  2280. int ret = 0;
  2281. struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
  2282. struct page *page = bvec->bv_page;
  2283. struct extent_io_tree *tree = bio->bi_private;
  2284. u64 start;
  2285. start = page_offset(page) + bvec->bv_offset;
  2286. bio->bi_private = NULL;
  2287. bio_get(bio);
  2288. if (tree->ops && tree->ops->submit_bio_hook)
  2289. ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
  2290. mirror_num, bio_flags, start);
  2291. else
  2292. btrfsic_submit_bio(rw, bio);
  2293. if (bio_flagged(bio, BIO_EOPNOTSUPP))
  2294. ret = -EOPNOTSUPP;
  2295. bio_put(bio);
  2296. return ret;
  2297. }
  2298. static int merge_bio(int rw, struct extent_io_tree *tree, struct page *page,
  2299. unsigned long offset, size_t size, struct bio *bio,
  2300. unsigned long bio_flags)
  2301. {
  2302. int ret = 0;
  2303. if (tree->ops && tree->ops->merge_bio_hook)
  2304. ret = tree->ops->merge_bio_hook(rw, page, offset, size, bio,
  2305. bio_flags);
  2306. BUG_ON(ret < 0);
  2307. return ret;
  2308. }
  2309. static int submit_extent_page(int rw, struct extent_io_tree *tree,
  2310. struct page *page, sector_t sector,
  2311. size_t size, unsigned long offset,
  2312. struct block_device *bdev,
  2313. struct bio **bio_ret,
  2314. unsigned long max_pages,
  2315. bio_end_io_t end_io_func,
  2316. int mirror_num,
  2317. unsigned long prev_bio_flags,
  2318. unsigned long bio_flags)
  2319. {
  2320. int ret = 0;
  2321. struct bio *bio;
  2322. int nr;
  2323. int contig = 0;
  2324. int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
  2325. int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
  2326. size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
  2327. if (bio_ret && *bio_ret) {
  2328. bio = *bio_ret;
  2329. if (old_compressed)
  2330. contig = bio->bi_sector == sector;
  2331. else
  2332. contig = bio->bi_sector + (bio->bi_size >> 9) ==
  2333. sector;
  2334. if (prev_bio_flags != bio_flags || !contig ||
  2335. merge_bio(rw, tree, page, offset, page_size, bio, bio_flags) ||
  2336. bio_add_page(bio, page, page_size, offset) < page_size) {
  2337. ret = submit_one_bio(rw, bio, mirror_num,
  2338. prev_bio_flags);
  2339. if (ret < 0)
  2340. return ret;
  2341. bio = NULL;
  2342. } else {
  2343. return 0;
  2344. }
  2345. }
  2346. if (this_compressed)
  2347. nr = BIO_MAX_PAGES;
  2348. else
  2349. nr = bio_get_nr_vecs(bdev);
  2350. bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
  2351. if (!bio)
  2352. return -ENOMEM;
  2353. bio_add_page(bio, page, page_size, offset);
  2354. bio->bi_end_io = end_io_func;
  2355. bio->bi_private = tree;
  2356. if (bio_ret)
  2357. *bio_ret = bio;
  2358. else
  2359. ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
  2360. return ret;
  2361. }
  2362. static void attach_extent_buffer_page(struct extent_buffer *eb,
  2363. struct page *page)
  2364. {
  2365. if (!PagePrivate(page)) {
  2366. SetPagePrivate(page);
  2367. page_cache_get(page);
  2368. set_page_private(page, (unsigned long)eb);
  2369. } else {
  2370. WARN_ON(page->private != (unsigned long)eb);
  2371. }
  2372. }
  2373. void set_page_extent_mapped(struct page *page)
  2374. {
  2375. if (!PagePrivate(page)) {
  2376. SetPagePrivate(page);
  2377. page_cache_get(page);
  2378. set_page_private(page, EXTENT_PAGE_PRIVATE);
  2379. }
  2380. }
  2381. /*
  2382. * basic readpage implementation. Locked extent state structs are inserted
  2383. * into the tree that are removed when the IO is done (by the end_io
  2384. * handlers)
  2385. * XXX JDM: This needs looking at to ensure proper page locking
  2386. */
  2387. static int __extent_read_full_page(struct extent_io_tree *tree,
  2388. struct page *page,
  2389. get_extent_t *get_extent,
  2390. struct bio **bio, int mirror_num,
  2391. unsigned long *bio_flags, int rw)
  2392. {
  2393. struct inode *inode = page->mapping->host;
  2394. u64 start = page_offset(page);
  2395. u64 page_end = start + PAGE_CACHE_SIZE - 1;
  2396. u64 end;
  2397. u64 cur = start;
  2398. u64 extent_offset;
  2399. u64 last_byte = i_size_read(inode);
  2400. u64 block_start;
  2401. u64 cur_end;
  2402. sector_t sector;
  2403. struct extent_map *em;
  2404. struct block_device *bdev;
  2405. struct btrfs_ordered_extent *ordered;
  2406. int ret;
  2407. int nr = 0;
  2408. size_t pg_offset = 0;
  2409. size_t iosize;
  2410. size_t disk_io_size;
  2411. size_t blocksize = inode->i_sb->s_blocksize;
  2412. unsigned long this_bio_flag = 0;
  2413. set_page_extent_mapped(page);
  2414. if (!PageUptodate(page)) {
  2415. if (cleancache_get_page(page) == 0) {
  2416. BUG_ON(blocksize != PAGE_SIZE);
  2417. goto out;
  2418. }
  2419. }
  2420. end = page_end;
  2421. while (1) {
  2422. lock_extent(tree, start, end);
  2423. ordered = btrfs_lookup_ordered_extent(inode, start);
  2424. if (!ordered)
  2425. break;
  2426. unlock_extent(tree, start, end);
  2427. btrfs_start_ordered_extent(inode, ordered, 1);
  2428. btrfs_put_ordered_extent(ordered);
  2429. }
  2430. if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
  2431. char *userpage;
  2432. size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
  2433. if (zero_offset) {
  2434. iosize = PAGE_CACHE_SIZE - zero_offset;
  2435. userpage = kmap_atomic(page);
  2436. memset(userpage + zero_offset, 0, iosize);
  2437. flush_dcache_page(page);
  2438. kunmap_atomic(userpage);
  2439. }
  2440. }
  2441. while (cur <= end) {
  2442. unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
  2443. if (cur >= last_byte) {
  2444. char *userpage;
  2445. struct extent_state *cached = NULL;
  2446. iosize = PAGE_CACHE_SIZE - pg_offset;
  2447. userpage = kmap_atomic(page);
  2448. memset(userpage + pg_offset, 0, iosize);
  2449. flush_dcache_page(page);
  2450. kunmap_atomic(userpage);
  2451. set_extent_uptodate(tree, cur, cur + iosize - 1,
  2452. &cached, GFP_NOFS);
  2453. unlock_extent_cached(tree, cur, cur + iosize - 1,
  2454. &cached, GFP_NOFS);
  2455. break;
  2456. }
  2457. em = get_extent(inode, page, pg_offset, cur,
  2458. end - cur + 1, 0);
  2459. if (IS_ERR_OR_NULL(em)) {
  2460. SetPageError(page);
  2461. unlock_extent(tree, cur, end);
  2462. break;
  2463. }
  2464. extent_offset = cur - em->start;
  2465. BUG_ON(extent_map_end(em) <= cur);
  2466. BUG_ON(end < cur);
  2467. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  2468. this_bio_flag = EXTENT_BIO_COMPRESSED;
  2469. extent_set_compress_type(&this_bio_flag,
  2470. em->compress_type);
  2471. }
  2472. iosize = min(extent_map_end(em) - cur, end - cur + 1);
  2473. cur_end = min(extent_map_end(em) - 1, end);
  2474. iosize = ALIGN(iosize, blocksize);
  2475. if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
  2476. disk_io_size = em->block_len;
  2477. sector = em->block_start >> 9;
  2478. } else {
  2479. sector = (em->block_start + extent_offset) >> 9;
  2480. disk_io_size = iosize;
  2481. }
  2482. bdev = em->bdev;
  2483. block_start = em->block_start;
  2484. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  2485. block_start = EXTENT_MAP_HOLE;
  2486. free_extent_map(em);
  2487. em = NULL;
  2488. /* we've found a hole, just zero and go on */
  2489. if (block_start == EXTENT_MAP_HOLE) {
  2490. char *userpage;
  2491. struct extent_state *cached = NULL;
  2492. userpage = kmap_atomic(page);
  2493. memset(userpage + pg_offset, 0, iosize);
  2494. flush_dcache_page(page);
  2495. kunmap_atomic(userpage);
  2496. set_extent_uptodate(tree, cur, cur + iosize - 1,
  2497. &cached, GFP_NOFS);
  2498. unlock_extent_cached(tree, cur, cur + iosize - 1,
  2499. &cached, GFP_NOFS);
  2500. cur = cur + iosize;
  2501. pg_offset += iosize;
  2502. continue;
  2503. }
  2504. /* the get_extent function already copied into the page */
  2505. if (test_range_bit(tree, cur, cur_end,
  2506. EXTENT_UPTODATE, 1, NULL)) {
  2507. check_page_uptodate(tree, page);
  2508. unlock_extent(tree, cur, cur + iosize - 1);
  2509. cur = cur + iosize;
  2510. pg_offset += iosize;
  2511. continue;
  2512. }
  2513. /* we have an inline extent but it didn't get marked up
  2514. * to date. Error out
  2515. */
  2516. if (block_start == EXTENT_MAP_INLINE) {
  2517. SetPageError(page);
  2518. unlock_extent(tree, cur, cur + iosize - 1);
  2519. cur = cur + iosize;
  2520. pg_offset += iosize;
  2521. continue;
  2522. }
  2523. pnr -= page->index;
  2524. ret = submit_extent_page(rw, tree, page,
  2525. sector, disk_io_size, pg_offset,
  2526. bdev, bio, pnr,
  2527. end_bio_extent_readpage, mirror_num,
  2528. *bio_flags,
  2529. this_bio_flag);
  2530. if (!ret) {
  2531. nr++;
  2532. *bio_flags = this_bio_flag;
  2533. } else {
  2534. SetPageError(page);
  2535. unlock_extent(tree, cur, cur + iosize - 1);
  2536. }
  2537. cur = cur + iosize;
  2538. pg_offset += iosize;
  2539. }
  2540. out:
  2541. if (!nr) {
  2542. if (!PageError(page))
  2543. SetPageUptodate(page);
  2544. unlock_page(page);
  2545. }
  2546. return 0;
  2547. }
  2548. int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
  2549. get_extent_t *get_extent, int mirror_num)
  2550. {
  2551. struct bio *bio = NULL;
  2552. unsigned long bio_flags = 0;
  2553. int ret;
  2554. ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
  2555. &bio_flags, READ);
  2556. if (bio)
  2557. ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
  2558. return ret;
  2559. }
  2560. static noinline void update_nr_written(struct page *page,
  2561. struct writeback_control *wbc,
  2562. unsigned long nr_written)
  2563. {
  2564. wbc->nr_to_write -= nr_written;
  2565. if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
  2566. wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
  2567. page->mapping->writeback_index = page->index + nr_written;
  2568. }
  2569. /*
  2570. * the writepage semantics are similar to regular writepage. extent
  2571. * records are inserted to lock ranges in the tree, and as dirty areas
  2572. * are found, they are marked writeback. Then the lock bits are removed
  2573. * and the end_io handler clears the writeback ranges
  2574. */
  2575. static int __extent_writepage(struct page *page, struct writeback_control *wbc,
  2576. void *data)
  2577. {
  2578. struct inode *inode = page->mapping->host;
  2579. struct extent_page_data *epd = data;
  2580. struct extent_io_tree *tree = epd->tree;
  2581. u64 start = page_offset(page);
  2582. u64 delalloc_start;
  2583. u64 page_end = start + PAGE_CACHE_SIZE - 1;
  2584. u64 end;
  2585. u64 cur = start;
  2586. u64 extent_offset;
  2587. u64 last_byte = i_size_read(inode);
  2588. u64 block_start;
  2589. u64 iosize;
  2590. sector_t sector;
  2591. struct extent_state *cached_state = NULL;
  2592. struct extent_map *em;
  2593. struct block_device *bdev;
  2594. int ret;
  2595. int nr = 0;
  2596. size_t pg_offset = 0;
  2597. size_t blocksize;
  2598. loff_t i_size = i_size_read(inode);
  2599. unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
  2600. u64 nr_delalloc;
  2601. u64 delalloc_end;
  2602. int page_started;
  2603. int compressed;
  2604. int write_flags;
  2605. unsigned long nr_written = 0;
  2606. bool fill_delalloc = true;
  2607. if (wbc->sync_mode == WB_SYNC_ALL)
  2608. write_flags = WRITE_SYNC;
  2609. else
  2610. write_flags = WRITE;
  2611. trace___extent_writepage(page, inode, wbc);
  2612. WARN_ON(!PageLocked(page));
  2613. ClearPageError(page);
  2614. pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
  2615. if (page->index > end_index ||
  2616. (page->index == end_index && !pg_offset)) {
  2617. page->mapping->a_ops->invalidatepage(page, 0);
  2618. unlock_page(page);
  2619. return 0;
  2620. }
  2621. if (page->index == end_index) {
  2622. char *userpage;
  2623. userpage = kmap_atomic(page);
  2624. memset(userpage + pg_offset, 0,
  2625. PAGE_CACHE_SIZE - pg_offset);
  2626. kunmap_atomic(userpage);
  2627. flush_dcache_page(page);
  2628. }
  2629. pg_offset = 0;
  2630. set_page_extent_mapped(page);
  2631. if (!tree->ops || !tree->ops->fill_delalloc)
  2632. fill_delalloc = false;
  2633. delalloc_start = start;
  2634. delalloc_end = 0;
  2635. page_started = 0;
  2636. if (!epd->extent_locked && fill_delalloc) {
  2637. u64 delalloc_to_write = 0;
  2638. /*
  2639. * make sure the wbc mapping index is at least updated
  2640. * to this page.
  2641. */
  2642. update_nr_written(page, wbc, 0);
  2643. while (delalloc_end < page_end) {
  2644. nr_delalloc = find_lock_delalloc_range(inode, tree,
  2645. page,
  2646. &delalloc_start,
  2647. &delalloc_end,
  2648. 128 * 1024 * 1024);
  2649. if (nr_delalloc == 0) {
  2650. delalloc_start = delalloc_end + 1;
  2651. continue;
  2652. }
  2653. ret = tree->ops->fill_delalloc(inode, page,
  2654. delalloc_start,
  2655. delalloc_end,
  2656. &page_started,
  2657. &nr_written);
  2658. /* File system has been set read-only */
  2659. if (ret) {
  2660. SetPageError(page);
  2661. goto done;
  2662. }
  2663. /*
  2664. * delalloc_end is already one less than the total
  2665. * length, so we don't subtract one from
  2666. * PAGE_CACHE_SIZE
  2667. */
  2668. delalloc_to_write += (delalloc_end - delalloc_start +
  2669. PAGE_CACHE_SIZE) >>
  2670. PAGE_CACHE_SHIFT;
  2671. delalloc_start = delalloc_end + 1;
  2672. }
  2673. if (wbc->nr_to_write < delalloc_to_write) {
  2674. int thresh = 8192;
  2675. if (delalloc_to_write < thresh * 2)
  2676. thresh = delalloc_to_write;
  2677. wbc->nr_to_write = min_t(u64, delalloc_to_write,
  2678. thresh);
  2679. }
  2680. /* did the fill delalloc function already unlock and start
  2681. * the IO?
  2682. */
  2683. if (page_started) {
  2684. ret = 0;
  2685. /*
  2686. * we've unlocked the page, so we can't update
  2687. * the mapping's writeback index, just update
  2688. * nr_to_write.
  2689. */
  2690. wbc->nr_to_write -= nr_written;
  2691. goto done_unlocked;
  2692. }
  2693. }
  2694. if (tree->ops && tree->ops->writepage_start_hook) {
  2695. ret = tree->ops->writepage_start_hook(page, start,
  2696. page_end);
  2697. if (ret) {
  2698. /* Fixup worker will requeue */
  2699. if (ret == -EBUSY)
  2700. wbc->pages_skipped++;
  2701. else
  2702. redirty_page_for_writepage(wbc, page);
  2703. update_nr_written(page, wbc, nr_written);
  2704. unlock_page(page);
  2705. ret = 0;
  2706. goto done_unlocked;
  2707. }
  2708. }
  2709. /*
  2710. * we don't want to touch the inode after unlocking the page,
  2711. * so we update the mapping writeback index now
  2712. */
  2713. update_nr_written(page, wbc, nr_written + 1);
  2714. end = page_end;
  2715. if (last_byte <= start) {
  2716. if (tree->ops && tree->ops->writepage_end_io_hook)
  2717. tree->ops->writepage_end_io_hook(page, start,
  2718. page_end, NULL, 1);
  2719. goto done;
  2720. }
  2721. blocksize = inode->i_sb->s_blocksize;
  2722. while (cur <= end) {
  2723. if (cur >= last_byte) {
  2724. if (tree->ops && tree->ops->writepage_end_io_hook)
  2725. tree->ops->writepage_end_io_hook(page, cur,
  2726. page_end, NULL, 1);
  2727. break;
  2728. }
  2729. em = epd->get_extent(inode, page, pg_offset, cur,
  2730. end - cur + 1, 1);
  2731. if (IS_ERR_OR_NULL(em)) {
  2732. SetPageError(page);
  2733. break;
  2734. }
  2735. extent_offset = cur - em->start;
  2736. BUG_ON(extent_map_end(em) <= cur);
  2737. BUG_ON(end < cur);
  2738. iosize = min(extent_map_end(em) - cur, end - cur + 1);
  2739. iosize = ALIGN(iosize, blocksize);
  2740. sector = (em->block_start + extent_offset) >> 9;
  2741. bdev = em->bdev;
  2742. block_start = em->block_start;
  2743. compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  2744. free_extent_map(em);
  2745. em = NULL;
  2746. /*
  2747. * compressed and inline extents are written through other
  2748. * paths in the FS
  2749. */
  2750. if (compressed || block_start == EXTENT_MAP_HOLE ||
  2751. block_start == EXTENT_MAP_INLINE) {
  2752. /*
  2753. * end_io notification does not happen here for
  2754. * compressed extents
  2755. */
  2756. if (!compressed && tree->ops &&
  2757. tree->ops->writepage_end_io_hook)
  2758. tree->ops->writepage_end_io_hook(page, cur,
  2759. cur + iosize - 1,
  2760. NULL, 1);
  2761. else if (compressed) {
  2762. /* we don't want to end_page_writeback on
  2763. * a compressed extent. this happens
  2764. * elsewhere
  2765. */
  2766. nr++;
  2767. }
  2768. cur += iosize;
  2769. pg_offset += iosize;
  2770. continue;
  2771. }
  2772. /* leave this out until we have a page_mkwrite call */
  2773. if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
  2774. EXTENT_DIRTY, 0, NULL)) {
  2775. cur = cur + iosize;
  2776. pg_offset += iosize;
  2777. continue;
  2778. }
  2779. if (tree->ops && tree->ops->writepage_io_hook) {
  2780. ret = tree->ops->writepage_io_hook(page, cur,
  2781. cur + iosize - 1);
  2782. } else {
  2783. ret = 0;
  2784. }
  2785. if (ret) {
  2786. SetPageError(page);
  2787. } else {
  2788. unsigned long max_nr = end_index + 1;
  2789. set_range_writeback(tree, cur, cur + iosize - 1);
  2790. if (!PageWriteback(page)) {
  2791. printk(KERN_ERR "btrfs warning page %lu not "
  2792. "writeback, cur %llu end %llu\n",
  2793. page->index, (unsigned long long)cur,
  2794. (unsigned long long)end);
  2795. }
  2796. ret = submit_extent_page(write_flags, tree, page,
  2797. sector, iosize, pg_offset,
  2798. bdev, &epd->bio, max_nr,
  2799. end_bio_extent_writepage,
  2800. 0, 0, 0);
  2801. if (ret)
  2802. SetPageError(page);
  2803. }
  2804. cur = cur + iosize;
  2805. pg_offset += iosize;
  2806. nr++;
  2807. }
  2808. done:
  2809. if (nr == 0) {
  2810. /* make sure the mapping tag for page dirty gets cleared */
  2811. set_page_writeback(page);
  2812. end_page_writeback(page);
  2813. }
  2814. unlock_page(page);
  2815. done_unlocked:
  2816. /* drop our reference on any cached states */
  2817. free_extent_state(cached_state);
  2818. return 0;
  2819. }
  2820. static int eb_wait(void *word)
  2821. {
  2822. io_schedule();
  2823. return 0;
  2824. }
  2825. void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
  2826. {
  2827. wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait,
  2828. TASK_UNINTERRUPTIBLE);
  2829. }
  2830. static int lock_extent_buffer_for_io(struct extent_buffer *eb,
  2831. struct btrfs_fs_info *fs_info,
  2832. struct extent_page_data *epd)
  2833. {
  2834. unsigned long i, num_pages;
  2835. int flush = 0;
  2836. int ret = 0;
  2837. if (!btrfs_try_tree_write_lock(eb)) {
  2838. flush = 1;
  2839. flush_write_bio(epd);
  2840. btrfs_tree_lock(eb);
  2841. }
  2842. if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
  2843. btrfs_tree_unlock(eb);
  2844. if (!epd->sync_io)
  2845. return 0;
  2846. if (!flush) {
  2847. flush_write_bio(epd);
  2848. flush = 1;
  2849. }
  2850. while (1) {
  2851. wait_on_extent_buffer_writeback(eb);
  2852. btrfs_tree_lock(eb);
  2853. if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
  2854. break;
  2855. btrfs_tree_unlock(eb);
  2856. }
  2857. }
  2858. /*
  2859. * We need to do this to prevent races in people who check if the eb is
  2860. * under IO since we can end up having no IO bits set for a short period
  2861. * of time.
  2862. */
  2863. spin_lock(&eb->refs_lock);
  2864. if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
  2865. set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
  2866. spin_unlock(&eb->refs_lock);
  2867. btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
  2868. __percpu_counter_add(&fs_info->dirty_metadata_bytes,
  2869. -eb->len,
  2870. fs_info->dirty_metadata_batch);
  2871. ret = 1;
  2872. } else {
  2873. spin_unlock(&eb->refs_lock);
  2874. }
  2875. btrfs_tree_unlock(eb);
  2876. if (!ret)
  2877. return ret;
  2878. num_pages = num_extent_pages(eb->start, eb->len);
  2879. for (i = 0; i < num_pages; i++) {
  2880. struct page *p = extent_buffer_page(eb, i);
  2881. if (!trylock_page(p)) {
  2882. if (!flush) {
  2883. flush_write_bio(epd);
  2884. flush = 1;
  2885. }
  2886. lock_page(p);
  2887. }
  2888. }
  2889. return ret;
  2890. }
  2891. static void end_extent_buffer_writeback(struct extent_buffer *eb)
  2892. {
  2893. clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
  2894. smp_mb__after_clear_bit();
  2895. wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
  2896. }
  2897. static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
  2898. {
  2899. int uptodate = err == 0;
  2900. struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
  2901. struct extent_buffer *eb;
  2902. int done;
  2903. do {
  2904. struct page *page = bvec->bv_page;
  2905. bvec--;
  2906. eb = (struct extent_buffer *)page->private;
  2907. BUG_ON(!eb);
  2908. done = atomic_dec_and_test(&eb->io_pages);
  2909. if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
  2910. set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
  2911. ClearPageUptodate(page);
  2912. SetPageError(page);
  2913. }
  2914. end_page_writeback(page);
  2915. if (!done)
  2916. continue;
  2917. end_extent_buffer_writeback(eb);
  2918. } while (bvec >= bio->bi_io_vec);
  2919. bio_put(bio);
  2920. }
  2921. static int write_one_eb(struct extent_buffer *eb,
  2922. struct btrfs_fs_info *fs_info,
  2923. struct writeback_control *wbc,
  2924. struct extent_page_data *epd)
  2925. {
  2926. struct block_device *bdev = fs_info->fs_devices->latest_bdev;
  2927. u64 offset = eb->start;
  2928. unsigned long i, num_pages;
  2929. unsigned long bio_flags = 0;
  2930. int rw = (epd->sync_io ? WRITE_SYNC : WRITE) | REQ_META;
  2931. int ret = 0;
  2932. clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
  2933. num_pages = num_extent_pages(eb->start, eb->len);
  2934. atomic_set(&eb->io_pages, num_pages);
  2935. if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID)
  2936. bio_flags = EXTENT_BIO_TREE_LOG;
  2937. for (i = 0; i < num_pages; i++) {
  2938. struct page *p = extent_buffer_page(eb, i);
  2939. clear_page_dirty_for_io(p);
  2940. set_page_writeback(p);
  2941. ret = submit_extent_page(rw, eb->tree, p, offset >> 9,
  2942. PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
  2943. -1, end_bio_extent_buffer_writepage,
  2944. 0, epd->bio_flags, bio_flags);
  2945. epd->bio_flags = bio_flags;
  2946. if (ret) {
  2947. set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
  2948. SetPageError(p);
  2949. if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
  2950. end_extent_buffer_writeback(eb);
  2951. ret = -EIO;
  2952. break;
  2953. }
  2954. offset += PAGE_CACHE_SIZE;
  2955. update_nr_written(p, wbc, 1);
  2956. unlock_page(p);
  2957. }
  2958. if (unlikely(ret)) {
  2959. for (; i < num_pages; i++) {
  2960. struct page *p = extent_buffer_page(eb, i);
  2961. unlock_page(p);
  2962. }
  2963. }
  2964. return ret;
  2965. }
  2966. int btree_write_cache_pages(struct address_space *mapping,
  2967. struct writeback_control *wbc)
  2968. {
  2969. struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
  2970. struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
  2971. struct extent_buffer *eb, *prev_eb = NULL;
  2972. struct extent_page_data epd = {
  2973. .bio = NULL,
  2974. .tree = tree,
  2975. .extent_locked = 0,
  2976. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  2977. .bio_flags = 0,
  2978. };
  2979. int ret = 0;
  2980. int done = 0;
  2981. int nr_to_write_done = 0;
  2982. struct pagevec pvec;
  2983. int nr_pages;
  2984. pgoff_t index;
  2985. pgoff_t end; /* Inclusive */
  2986. int scanned = 0;
  2987. int tag;
  2988. pagevec_init(&pvec, 0);
  2989. if (wbc->range_cyclic) {
  2990. index = mapping->writeback_index; /* Start from prev offset */
  2991. end = -1;
  2992. } else {
  2993. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  2994. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  2995. scanned = 1;
  2996. }
  2997. if (wbc->sync_mode == WB_SYNC_ALL)
  2998. tag = PAGECACHE_TAG_TOWRITE;
  2999. else
  3000. tag = PAGECACHE_TAG_DIRTY;
  3001. retry:
  3002. if (wbc->sync_mode == WB_SYNC_ALL)
  3003. tag_pages_for_writeback(mapping, index, end);
  3004. while (!done && !nr_to_write_done && (index <= end) &&
  3005. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
  3006. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
  3007. unsigned i;
  3008. scanned = 1;
  3009. for (i = 0; i < nr_pages; i++) {
  3010. struct page *page = pvec.pages[i];
  3011. if (!PagePrivate(page))
  3012. continue;
  3013. if (!wbc->range_cyclic && page->index > end) {
  3014. done = 1;
  3015. break;
  3016. }
  3017. spin_lock(&mapping->private_lock);
  3018. if (!PagePrivate(page)) {
  3019. spin_unlock(&mapping->private_lock);
  3020. continue;
  3021. }
  3022. eb = (struct extent_buffer *)page->private;
  3023. /*
  3024. * Shouldn't happen and normally this would be a BUG_ON
  3025. * but no sense in crashing the users box for something
  3026. * we can survive anyway.
  3027. */
  3028. if (!eb) {
  3029. spin_unlock(&mapping->private_lock);
  3030. WARN_ON(1);
  3031. continue;
  3032. }
  3033. if (eb == prev_eb) {
  3034. spin_unlock(&mapping->private_lock);
  3035. continue;
  3036. }
  3037. ret = atomic_inc_not_zero(&eb->refs);
  3038. spin_unlock(&mapping->private_lock);
  3039. if (!ret)
  3040. continue;
  3041. prev_eb = eb;
  3042. ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
  3043. if (!ret) {
  3044. free_extent_buffer(eb);
  3045. continue;
  3046. }
  3047. ret = write_one_eb(eb, fs_info, wbc, &epd);
  3048. if (ret) {
  3049. done = 1;
  3050. free_extent_buffer(eb);
  3051. break;
  3052. }
  3053. free_extent_buffer(eb);
  3054. /*
  3055. * the filesystem may choose to bump up nr_to_write.
  3056. * We have to make sure to honor the new nr_to_write
  3057. * at any time
  3058. */
  3059. nr_to_write_done = wbc->nr_to_write <= 0;
  3060. }
  3061. pagevec_release(&pvec);
  3062. cond_resched();
  3063. }
  3064. if (!scanned && !done) {
  3065. /*
  3066. * We hit the last page and there is more work to be done: wrap
  3067. * back to the start of the file
  3068. */
  3069. scanned = 1;
  3070. index = 0;
  3071. goto retry;
  3072. }
  3073. flush_write_bio(&epd);
  3074. return ret;
  3075. }
  3076. /**
  3077. * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
  3078. * @mapping: address space structure to write
  3079. * @wbc: subtract the number of written pages from *@wbc->nr_to_write
  3080. * @writepage: function called for each page
  3081. * @data: data passed to writepage function
  3082. *
  3083. * If a page is already under I/O, write_cache_pages() skips it, even
  3084. * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
  3085. * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
  3086. * and msync() need to guarantee that all the data which was dirty at the time
  3087. * the call was made get new I/O started against them. If wbc->sync_mode is
  3088. * WB_SYNC_ALL then we were called for data integrity and we must wait for
  3089. * existing IO to complete.
  3090. */
  3091. static int extent_write_cache_pages(struct extent_io_tree *tree,
  3092. struct address_space *mapping,
  3093. struct writeback_control *wbc,
  3094. writepage_t writepage, void *data,
  3095. void (*flush_fn)(void *))
  3096. {
  3097. struct inode *inode = mapping->host;
  3098. int ret = 0;
  3099. int done = 0;
  3100. int nr_to_write_done = 0;
  3101. struct pagevec pvec;
  3102. int nr_pages;
  3103. pgoff_t index;
  3104. pgoff_t end; /* Inclusive */
  3105. int scanned = 0;
  3106. int tag;
  3107. /*
  3108. * We have to hold onto the inode so that ordered extents can do their
  3109. * work when the IO finishes. The alternative to this is failing to add
  3110. * an ordered extent if the igrab() fails there and that is a huge pain
  3111. * to deal with, so instead just hold onto the inode throughout the
  3112. * writepages operation. If it fails here we are freeing up the inode
  3113. * anyway and we'd rather not waste our time writing out stuff that is
  3114. * going to be truncated anyway.
  3115. */
  3116. if (!igrab(inode))
  3117. return 0;
  3118. pagevec_init(&pvec, 0);
  3119. if (wbc->range_cyclic) {
  3120. index = mapping->writeback_index; /* Start from prev offset */
  3121. end = -1;
  3122. } else {
  3123. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  3124. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  3125. scanned = 1;
  3126. }
  3127. if (wbc->sync_mode == WB_SYNC_ALL)
  3128. tag = PAGECACHE_TAG_TOWRITE;
  3129. else
  3130. tag = PAGECACHE_TAG_DIRTY;
  3131. retry:
  3132. if (wbc->sync_mode == WB_SYNC_ALL)
  3133. tag_pages_for_writeback(mapping, index, end);
  3134. while (!done && !nr_to_write_done && (index <= end) &&
  3135. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
  3136. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
  3137. unsigned i;
  3138. scanned = 1;
  3139. for (i = 0; i < nr_pages; i++) {
  3140. struct page *page = pvec.pages[i];
  3141. /*
  3142. * At this point we hold neither mapping->tree_lock nor
  3143. * lock on the page itself: the page may be truncated or
  3144. * invalidated (changing page->mapping to NULL), or even
  3145. * swizzled back from swapper_space to tmpfs file
  3146. * mapping
  3147. */
  3148. if (!trylock_page(page)) {
  3149. flush_fn(data);
  3150. lock_page(page);
  3151. }
  3152. if (unlikely(page->mapping != mapping)) {
  3153. unlock_page(page);
  3154. continue;
  3155. }
  3156. if (!wbc->range_cyclic && page->index > end) {
  3157. done = 1;
  3158. unlock_page(page);
  3159. continue;
  3160. }
  3161. if (wbc->sync_mode != WB_SYNC_NONE) {
  3162. if (PageWriteback(page))
  3163. flush_fn(data);
  3164. wait_on_page_writeback(page);
  3165. }
  3166. if (PageWriteback(page) ||
  3167. !clear_page_dirty_for_io(page)) {
  3168. unlock_page(page);
  3169. continue;
  3170. }
  3171. ret = (*writepage)(page, wbc, data);
  3172. if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
  3173. unlock_page(page);
  3174. ret = 0;
  3175. }
  3176. if (ret)
  3177. done = 1;
  3178. /*
  3179. * the filesystem may choose to bump up nr_to_write.
  3180. * We have to make sure to honor the new nr_to_write
  3181. * at any time
  3182. */
  3183. nr_to_write_done = wbc->nr_to_write <= 0;
  3184. }
  3185. pagevec_release(&pvec);
  3186. cond_resched();
  3187. }
  3188. if (!scanned && !done) {
  3189. /*
  3190. * We hit the last page and there is more work to be done: wrap
  3191. * back to the start of the file
  3192. */
  3193. scanned = 1;
  3194. index = 0;
  3195. goto retry;
  3196. }
  3197. btrfs_add_delayed_iput(inode);
  3198. return ret;
  3199. }
  3200. static void flush_epd_write_bio(struct extent_page_data *epd)
  3201. {
  3202. if (epd->bio) {
  3203. int rw = WRITE;
  3204. int ret;
  3205. if (epd->sync_io)
  3206. rw = WRITE_SYNC;
  3207. ret = submit_one_bio(rw, epd->bio, 0, epd->bio_flags);
  3208. BUG_ON(ret < 0); /* -ENOMEM */
  3209. epd->bio = NULL;
  3210. }
  3211. }
  3212. static noinline void flush_write_bio(void *data)
  3213. {
  3214. struct extent_page_data *epd = data;
  3215. flush_epd_write_bio(epd);
  3216. }
  3217. int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
  3218. get_extent_t *get_extent,
  3219. struct writeback_control *wbc)
  3220. {
  3221. int ret;
  3222. struct extent_page_data epd = {
  3223. .bio = NULL,
  3224. .tree = tree,
  3225. .get_extent = get_extent,
  3226. .extent_locked = 0,
  3227. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  3228. .bio_flags = 0,
  3229. };
  3230. ret = __extent_writepage(page, wbc, &epd);
  3231. flush_epd_write_bio(&epd);
  3232. return ret;
  3233. }
  3234. int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
  3235. u64 start, u64 end, get_extent_t *get_extent,
  3236. int mode)
  3237. {
  3238. int ret = 0;
  3239. struct address_space *mapping = inode->i_mapping;
  3240. struct page *page;
  3241. unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
  3242. PAGE_CACHE_SHIFT;
  3243. struct extent_page_data epd = {
  3244. .bio = NULL,
  3245. .tree = tree,
  3246. .get_extent = get_extent,
  3247. .extent_locked = 1,
  3248. .sync_io = mode == WB_SYNC_ALL,
  3249. .bio_flags = 0,
  3250. };
  3251. struct writeback_control wbc_writepages = {
  3252. .sync_mode = mode,
  3253. .nr_to_write = nr_pages * 2,
  3254. .range_start = start,
  3255. .range_end = end + 1,
  3256. };
  3257. while (start <= end) {
  3258. page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
  3259. if (clear_page_dirty_for_io(page))
  3260. ret = __extent_writepage(page, &wbc_writepages, &epd);
  3261. else {
  3262. if (tree->ops && tree->ops->writepage_end_io_hook)
  3263. tree->ops->writepage_end_io_hook(page, start,
  3264. start + PAGE_CACHE_SIZE - 1,
  3265. NULL, 1);
  3266. unlock_page(page);
  3267. }
  3268. page_cache_release(page);
  3269. start += PAGE_CACHE_SIZE;
  3270. }
  3271. flush_epd_write_bio(&epd);
  3272. return ret;
  3273. }
  3274. int extent_writepages(struct extent_io_tree *tree,
  3275. struct address_space *mapping,
  3276. get_extent_t *get_extent,
  3277. struct writeback_control *wbc)
  3278. {
  3279. int ret = 0;
  3280. struct extent_page_data epd = {
  3281. .bio = NULL,
  3282. .tree = tree,
  3283. .get_extent = get_extent,
  3284. .extent_locked = 0,
  3285. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  3286. .bio_flags = 0,
  3287. };
  3288. ret = extent_write_cache_pages(tree, mapping, wbc,
  3289. __extent_writepage, &epd,
  3290. flush_write_bio);
  3291. flush_epd_write_bio(&epd);
  3292. return ret;
  3293. }
  3294. int extent_readpages(struct extent_io_tree *tree,
  3295. struct address_space *mapping,
  3296. struct list_head *pages, unsigned nr_pages,
  3297. get_extent_t get_extent)
  3298. {
  3299. struct bio *bio = NULL;
  3300. unsigned page_idx;
  3301. unsigned long bio_flags = 0;
  3302. struct page *pagepool[16];
  3303. struct page *page;
  3304. int i = 0;
  3305. int nr = 0;
  3306. for (page_idx = 0; page_idx < nr_pages; page_idx++) {
  3307. page = list_entry(pages->prev, struct page, lru);
  3308. prefetchw(&page->flags);
  3309. list_del(&page->lru);
  3310. if (add_to_page_cache_lru(page, mapping,
  3311. page->index, GFP_NOFS)) {
  3312. page_cache_release(page);
  3313. continue;
  3314. }
  3315. pagepool[nr++] = page;
  3316. if (nr < ARRAY_SIZE(pagepool))
  3317. continue;
  3318. for (i = 0; i < nr; i++) {
  3319. __extent_read_full_page(tree, pagepool[i], get_extent,
  3320. &bio, 0, &bio_flags, READ);
  3321. page_cache_release(pagepool[i]);
  3322. }
  3323. nr = 0;
  3324. }
  3325. for (i = 0; i < nr; i++) {
  3326. __extent_read_full_page(tree, pagepool[i], get_extent,
  3327. &bio, 0, &bio_flags, READ);
  3328. page_cache_release(pagepool[i]);
  3329. }
  3330. BUG_ON(!list_empty(pages));
  3331. if (bio)
  3332. return submit_one_bio(READ, bio, 0, bio_flags);
  3333. return 0;
  3334. }
  3335. /*
  3336. * basic invalidatepage code, this waits on any locked or writeback
  3337. * ranges corresponding to the page, and then deletes any extent state
  3338. * records from the tree
  3339. */
  3340. int extent_invalidatepage(struct extent_io_tree *tree,
  3341. struct page *page, unsigned long offset)
  3342. {
  3343. struct extent_state *cached_state = NULL;
  3344. u64 start = page_offset(page);
  3345. u64 end = start + PAGE_CACHE_SIZE - 1;
  3346. size_t blocksize = page->mapping->host->i_sb->s_blocksize;
  3347. start += ALIGN(offset, blocksize);
  3348. if (start > end)
  3349. return 0;
  3350. lock_extent_bits(tree, start, end, 0, &cached_state);
  3351. wait_on_page_writeback(page);
  3352. clear_extent_bit(tree, start, end,
  3353. EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
  3354. EXTENT_DO_ACCOUNTING,
  3355. 1, 1, &cached_state, GFP_NOFS);
  3356. return 0;
  3357. }
  3358. /*
  3359. * a helper for releasepage, this tests for areas of the page that
  3360. * are locked or under IO and drops the related state bits if it is safe
  3361. * to drop the page.
  3362. */
  3363. static int try_release_extent_state(struct extent_map_tree *map,
  3364. struct extent_io_tree *tree,
  3365. struct page *page, gfp_t mask)
  3366. {
  3367. u64 start = page_offset(page);
  3368. u64 end = start + PAGE_CACHE_SIZE - 1;
  3369. int ret = 1;
  3370. if (test_range_bit(tree, start, end,
  3371. EXTENT_IOBITS, 0, NULL))
  3372. ret = 0;
  3373. else {
  3374. if ((mask & GFP_NOFS) == GFP_NOFS)
  3375. mask = GFP_NOFS;
  3376. /*
  3377. * at this point we can safely clear everything except the
  3378. * locked bit and the nodatasum bit
  3379. */
  3380. ret = clear_extent_bit(tree, start, end,
  3381. ~(EXTENT_LOCKED | EXTENT_NODATASUM),
  3382. 0, 0, NULL, mask);
  3383. /* if clear_extent_bit failed for enomem reasons,
  3384. * we can't allow the release to continue.
  3385. */
  3386. if (ret < 0)
  3387. ret = 0;
  3388. else
  3389. ret = 1;
  3390. }
  3391. return ret;
  3392. }
  3393. /*
  3394. * a helper for releasepage. As long as there are no locked extents
  3395. * in the range corresponding to the page, both state records and extent
  3396. * map records are removed
  3397. */
  3398. int try_release_extent_mapping(struct extent_map_tree *map,
  3399. struct extent_io_tree *tree, struct page *page,
  3400. gfp_t mask)
  3401. {
  3402. struct extent_map *em;
  3403. u64 start = page_offset(page);
  3404. u64 end = start + PAGE_CACHE_SIZE - 1;
  3405. if ((mask & __GFP_WAIT) &&
  3406. page->mapping->host->i_size > 16 * 1024 * 1024) {
  3407. u64 len;
  3408. while (start <= end) {
  3409. len = end - start + 1;
  3410. write_lock(&map->lock);
  3411. em = lookup_extent_mapping(map, start, len);
  3412. if (!em) {
  3413. write_unlock(&map->lock);
  3414. break;
  3415. }
  3416. if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
  3417. em->start != start) {
  3418. write_unlock(&map->lock);
  3419. free_extent_map(em);
  3420. break;
  3421. }
  3422. if (!test_range_bit(tree, em->start,
  3423. extent_map_end(em) - 1,
  3424. EXTENT_LOCKED | EXTENT_WRITEBACK,
  3425. 0, NULL)) {
  3426. remove_extent_mapping(map, em);
  3427. /* once for the rb tree */
  3428. free_extent_map(em);
  3429. }
  3430. start = extent_map_end(em);
  3431. write_unlock(&map->lock);
  3432. /* once for us */
  3433. free_extent_map(em);
  3434. }
  3435. }
  3436. return try_release_extent_state(map, tree, page, mask);
  3437. }
  3438. /*
  3439. * helper function for fiemap, which doesn't want to see any holes.
  3440. * This maps until we find something past 'last'
  3441. */
  3442. static struct extent_map *get_extent_skip_holes(struct inode *inode,
  3443. u64 offset,
  3444. u64 last,
  3445. get_extent_t *get_extent)
  3446. {
  3447. u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
  3448. struct extent_map *em;
  3449. u64 len;
  3450. if (offset >= last)
  3451. return NULL;
  3452. while(1) {
  3453. len = last - offset;
  3454. if (len == 0)
  3455. break;
  3456. len = ALIGN(len, sectorsize);
  3457. em = get_extent(inode, NULL, 0, offset, len, 0);
  3458. if (IS_ERR_OR_NULL(em))
  3459. return em;
  3460. /* if this isn't a hole return it */
  3461. if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
  3462. em->block_start != EXTENT_MAP_HOLE) {
  3463. return em;
  3464. }
  3465. /* this is a hole, advance to the next extent */
  3466. offset = extent_map_end(em);
  3467. free_extent_map(em);
  3468. if (offset >= last)
  3469. break;
  3470. }
  3471. return NULL;
  3472. }
  3473. int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  3474. __u64 start, __u64 len, get_extent_t *get_extent)
  3475. {
  3476. int ret = 0;
  3477. u64 off = start;
  3478. u64 max = start + len;
  3479. u32 flags = 0;
  3480. u32 found_type;
  3481. u64 last;
  3482. u64 last_for_get_extent = 0;
  3483. u64 disko = 0;
  3484. u64 isize = i_size_read(inode);
  3485. struct btrfs_key found_key;
  3486. struct extent_map *em = NULL;
  3487. struct extent_state *cached_state = NULL;
  3488. struct btrfs_path *path;
  3489. struct btrfs_file_extent_item *item;
  3490. int end = 0;
  3491. u64 em_start = 0;
  3492. u64 em_len = 0;
  3493. u64 em_end = 0;
  3494. unsigned long emflags;
  3495. if (len == 0)
  3496. return -EINVAL;
  3497. path = btrfs_alloc_path();
  3498. if (!path)
  3499. return -ENOMEM;
  3500. path->leave_spinning = 1;
  3501. start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
  3502. len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
  3503. /*
  3504. * lookup the last file extent. We're not using i_size here
  3505. * because there might be preallocation past i_size
  3506. */
  3507. ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
  3508. path, btrfs_ino(inode), -1, 0);
  3509. if (ret < 0) {
  3510. btrfs_free_path(path);
  3511. return ret;
  3512. }
  3513. WARN_ON(!ret);
  3514. path->slots[0]--;
  3515. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3516. struct btrfs_file_extent_item);
  3517. btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
  3518. found_type = btrfs_key_type(&found_key);
  3519. /* No extents, but there might be delalloc bits */
  3520. if (found_key.objectid != btrfs_ino(inode) ||
  3521. found_type != BTRFS_EXTENT_DATA_KEY) {
  3522. /* have to trust i_size as the end */
  3523. last = (u64)-1;
  3524. last_for_get_extent = isize;
  3525. } else {
  3526. /*
  3527. * remember the start of the last extent. There are a
  3528. * bunch of different factors that go into the length of the
  3529. * extent, so its much less complex to remember where it started
  3530. */
  3531. last = found_key.offset;
  3532. last_for_get_extent = last + 1;
  3533. }
  3534. btrfs_free_path(path);
  3535. /*
  3536. * we might have some extents allocated but more delalloc past those
  3537. * extents. so, we trust isize unless the start of the last extent is
  3538. * beyond isize
  3539. */
  3540. if (last < isize) {
  3541. last = (u64)-1;
  3542. last_for_get_extent = isize;
  3543. }
  3544. lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1, 0,
  3545. &cached_state);
  3546. em = get_extent_skip_holes(inode, start, last_for_get_extent,
  3547. get_extent);
  3548. if (!em)
  3549. goto out;
  3550. if (IS_ERR(em)) {
  3551. ret = PTR_ERR(em);
  3552. goto out;
  3553. }
  3554. while (!end) {
  3555. u64 offset_in_extent;
  3556. /* break if the extent we found is outside the range */
  3557. if (em->start >= max || extent_map_end(em) < off)
  3558. break;
  3559. /*
  3560. * get_extent may return an extent that starts before our
  3561. * requested range. We have to make sure the ranges
  3562. * we return to fiemap always move forward and don't
  3563. * overlap, so adjust the offsets here
  3564. */
  3565. em_start = max(em->start, off);
  3566. /*
  3567. * record the offset from the start of the extent
  3568. * for adjusting the disk offset below
  3569. */
  3570. offset_in_extent = em_start - em->start;
  3571. em_end = extent_map_end(em);
  3572. em_len = em_end - em_start;
  3573. emflags = em->flags;
  3574. disko = 0;
  3575. flags = 0;
  3576. /*
  3577. * bump off for our next call to get_extent
  3578. */
  3579. off = extent_map_end(em);
  3580. if (off >= max)
  3581. end = 1;
  3582. if (em->block_start == EXTENT_MAP_LAST_BYTE) {
  3583. end = 1;
  3584. flags |= FIEMAP_EXTENT_LAST;
  3585. } else if (em->block_start == EXTENT_MAP_INLINE) {
  3586. flags |= (FIEMAP_EXTENT_DATA_INLINE |
  3587. FIEMAP_EXTENT_NOT_ALIGNED);
  3588. } else if (em->block_start == EXTENT_MAP_DELALLOC) {
  3589. flags |= (FIEMAP_EXTENT_DELALLOC |
  3590. FIEMAP_EXTENT_UNKNOWN);
  3591. } else {
  3592. disko = em->block_start + offset_in_extent;
  3593. }
  3594. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
  3595. flags |= FIEMAP_EXTENT_ENCODED;
  3596. free_extent_map(em);
  3597. em = NULL;
  3598. if ((em_start >= last) || em_len == (u64)-1 ||
  3599. (last == (u64)-1 && isize <= em_end)) {
  3600. flags |= FIEMAP_EXTENT_LAST;
  3601. end = 1;
  3602. }
  3603. /* now scan forward to see if this is really the last extent. */
  3604. em = get_extent_skip_holes(inode, off, last_for_get_extent,
  3605. get_extent);
  3606. if (IS_ERR(em)) {
  3607. ret = PTR_ERR(em);
  3608. goto out;
  3609. }
  3610. if (!em) {
  3611. flags |= FIEMAP_EXTENT_LAST;
  3612. end = 1;
  3613. }
  3614. ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
  3615. em_len, flags);
  3616. if (ret)
  3617. goto out_free;
  3618. }
  3619. out_free:
  3620. free_extent_map(em);
  3621. out:
  3622. unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
  3623. &cached_state, GFP_NOFS);
  3624. return ret;
  3625. }
  3626. static void __free_extent_buffer(struct extent_buffer *eb)
  3627. {
  3628. btrfs_leak_debug_del(&eb->leak_list);
  3629. kmem_cache_free(extent_buffer_cache, eb);
  3630. }
  3631. static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
  3632. u64 start,
  3633. unsigned long len,
  3634. gfp_t mask)
  3635. {
  3636. struct extent_buffer *eb = NULL;
  3637. eb = kmem_cache_zalloc(extent_buffer_cache, mask);
  3638. if (eb == NULL)
  3639. return NULL;
  3640. eb->start = start;
  3641. eb->len = len;
  3642. eb->tree = tree;
  3643. eb->bflags = 0;
  3644. rwlock_init(&eb->lock);
  3645. atomic_set(&eb->write_locks, 0);
  3646. atomic_set(&eb->read_locks, 0);
  3647. atomic_set(&eb->blocking_readers, 0);
  3648. atomic_set(&eb->blocking_writers, 0);
  3649. atomic_set(&eb->spinning_readers, 0);
  3650. atomic_set(&eb->spinning_writers, 0);
  3651. eb->lock_nested = 0;
  3652. init_waitqueue_head(&eb->write_lock_wq);
  3653. init_waitqueue_head(&eb->read_lock_wq);
  3654. btrfs_leak_debug_add(&eb->leak_list, &buffers);
  3655. spin_lock_init(&eb->refs_lock);
  3656. atomic_set(&eb->refs, 1);
  3657. atomic_set(&eb->io_pages, 0);
  3658. /*
  3659. * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
  3660. */
  3661. BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
  3662. > MAX_INLINE_EXTENT_BUFFER_SIZE);
  3663. BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
  3664. return eb;
  3665. }
  3666. struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
  3667. {
  3668. unsigned long i;
  3669. struct page *p;
  3670. struct extent_buffer *new;
  3671. unsigned long num_pages = num_extent_pages(src->start, src->len);
  3672. new = __alloc_extent_buffer(NULL, src->start, src->len, GFP_ATOMIC);
  3673. if (new == NULL)
  3674. return NULL;
  3675. for (i = 0; i < num_pages; i++) {
  3676. p = alloc_page(GFP_ATOMIC);
  3677. BUG_ON(!p);
  3678. attach_extent_buffer_page(new, p);
  3679. WARN_ON(PageDirty(p));
  3680. SetPageUptodate(p);
  3681. new->pages[i] = p;
  3682. }
  3683. copy_extent_buffer(new, src, 0, 0, src->len);
  3684. set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
  3685. set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
  3686. return new;
  3687. }
  3688. struct extent_buffer *alloc_dummy_extent_buffer(u64 start, unsigned long len)
  3689. {
  3690. struct extent_buffer *eb;
  3691. unsigned long num_pages = num_extent_pages(0, len);
  3692. unsigned long i;
  3693. eb = __alloc_extent_buffer(NULL, start, len, GFP_ATOMIC);
  3694. if (!eb)
  3695. return NULL;
  3696. for (i = 0; i < num_pages; i++) {
  3697. eb->pages[i] = alloc_page(GFP_ATOMIC);
  3698. if (!eb->pages[i])
  3699. goto err;
  3700. }
  3701. set_extent_buffer_uptodate(eb);
  3702. btrfs_set_header_nritems(eb, 0);
  3703. set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
  3704. return eb;
  3705. err:
  3706. for (; i > 0; i--)
  3707. __free_page(eb->pages[i - 1]);
  3708. __free_extent_buffer(eb);
  3709. return NULL;
  3710. }
  3711. static int extent_buffer_under_io(struct extent_buffer *eb)
  3712. {
  3713. return (atomic_read(&eb->io_pages) ||
  3714. test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
  3715. test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
  3716. }
  3717. /*
  3718. * Helper for releasing extent buffer page.
  3719. */
  3720. static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
  3721. unsigned long start_idx)
  3722. {
  3723. unsigned long index;
  3724. unsigned long num_pages;
  3725. struct page *page;
  3726. int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
  3727. BUG_ON(extent_buffer_under_io(eb));
  3728. num_pages = num_extent_pages(eb->start, eb->len);
  3729. index = start_idx + num_pages;
  3730. if (start_idx >= index)
  3731. return;
  3732. do {
  3733. index--;
  3734. page = extent_buffer_page(eb, index);
  3735. if (page && mapped) {
  3736. spin_lock(&page->mapping->private_lock);
  3737. /*
  3738. * We do this since we'll remove the pages after we've
  3739. * removed the eb from the radix tree, so we could race
  3740. * and have this page now attached to the new eb. So
  3741. * only clear page_private if it's still connected to
  3742. * this eb.
  3743. */
  3744. if (PagePrivate(page) &&
  3745. page->private == (unsigned long)eb) {
  3746. BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
  3747. BUG_ON(PageDirty(page));
  3748. BUG_ON(PageWriteback(page));
  3749. /*
  3750. * We need to make sure we haven't be attached
  3751. * to a new eb.
  3752. */
  3753. ClearPagePrivate(page);
  3754. set_page_private(page, 0);
  3755. /* One for the page private */
  3756. page_cache_release(page);
  3757. }
  3758. spin_unlock(&page->mapping->private_lock);
  3759. }
  3760. if (page) {
  3761. /* One for when we alloced the page */
  3762. page_cache_release(page);
  3763. }
  3764. } while (index != start_idx);
  3765. }
  3766. /*
  3767. * Helper for releasing the extent buffer.
  3768. */
  3769. static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
  3770. {
  3771. btrfs_release_extent_buffer_page(eb, 0);
  3772. __free_extent_buffer(eb);
  3773. }
  3774. static void check_buffer_tree_ref(struct extent_buffer *eb)
  3775. {
  3776. int refs;
  3777. /* the ref bit is tricky. We have to make sure it is set
  3778. * if we have the buffer dirty. Otherwise the
  3779. * code to free a buffer can end up dropping a dirty
  3780. * page
  3781. *
  3782. * Once the ref bit is set, it won't go away while the
  3783. * buffer is dirty or in writeback, and it also won't
  3784. * go away while we have the reference count on the
  3785. * eb bumped.
  3786. *
  3787. * We can't just set the ref bit without bumping the
  3788. * ref on the eb because free_extent_buffer might
  3789. * see the ref bit and try to clear it. If this happens
  3790. * free_extent_buffer might end up dropping our original
  3791. * ref by mistake and freeing the page before we are able
  3792. * to add one more ref.
  3793. *
  3794. * So bump the ref count first, then set the bit. If someone
  3795. * beat us to it, drop the ref we added.
  3796. */
  3797. refs = atomic_read(&eb->refs);
  3798. if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  3799. return;
  3800. spin_lock(&eb->refs_lock);
  3801. if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  3802. atomic_inc(&eb->refs);
  3803. spin_unlock(&eb->refs_lock);
  3804. }
  3805. static void mark_extent_buffer_accessed(struct extent_buffer *eb)
  3806. {
  3807. unsigned long num_pages, i;
  3808. check_buffer_tree_ref(eb);
  3809. num_pages = num_extent_pages(eb->start, eb->len);
  3810. for (i = 0; i < num_pages; i++) {
  3811. struct page *p = extent_buffer_page(eb, i);
  3812. mark_page_accessed(p);
  3813. }
  3814. }
  3815. struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
  3816. u64 start, unsigned long len)
  3817. {
  3818. unsigned long num_pages = num_extent_pages(start, len);
  3819. unsigned long i;
  3820. unsigned long index = start >> PAGE_CACHE_SHIFT;
  3821. struct extent_buffer *eb;
  3822. struct extent_buffer *exists = NULL;
  3823. struct page *p;
  3824. struct address_space *mapping = tree->mapping;
  3825. int uptodate = 1;
  3826. int ret;
  3827. rcu_read_lock();
  3828. eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
  3829. if (eb && atomic_inc_not_zero(&eb->refs)) {
  3830. rcu_read_unlock();
  3831. mark_extent_buffer_accessed(eb);
  3832. return eb;
  3833. }
  3834. rcu_read_unlock();
  3835. eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
  3836. if (!eb)
  3837. return NULL;
  3838. for (i = 0; i < num_pages; i++, index++) {
  3839. p = find_or_create_page(mapping, index, GFP_NOFS);
  3840. if (!p)
  3841. goto free_eb;
  3842. spin_lock(&mapping->private_lock);
  3843. if (PagePrivate(p)) {
  3844. /*
  3845. * We could have already allocated an eb for this page
  3846. * and attached one so lets see if we can get a ref on
  3847. * the existing eb, and if we can we know it's good and
  3848. * we can just return that one, else we know we can just
  3849. * overwrite page->private.
  3850. */
  3851. exists = (struct extent_buffer *)p->private;
  3852. if (atomic_inc_not_zero(&exists->refs)) {
  3853. spin_unlock(&mapping->private_lock);
  3854. unlock_page(p);
  3855. page_cache_release(p);
  3856. mark_extent_buffer_accessed(exists);
  3857. goto free_eb;
  3858. }
  3859. /*
  3860. * Do this so attach doesn't complain and we need to
  3861. * drop the ref the old guy had.
  3862. */
  3863. ClearPagePrivate(p);
  3864. WARN_ON(PageDirty(p));
  3865. page_cache_release(p);
  3866. }
  3867. attach_extent_buffer_page(eb, p);
  3868. spin_unlock(&mapping->private_lock);
  3869. WARN_ON(PageDirty(p));
  3870. mark_page_accessed(p);
  3871. eb->pages[i] = p;
  3872. if (!PageUptodate(p))
  3873. uptodate = 0;
  3874. /*
  3875. * see below about how we avoid a nasty race with release page
  3876. * and why we unlock later
  3877. */
  3878. }
  3879. if (uptodate)
  3880. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  3881. again:
  3882. ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
  3883. if (ret)
  3884. goto free_eb;
  3885. spin_lock(&tree->buffer_lock);
  3886. ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
  3887. if (ret == -EEXIST) {
  3888. exists = radix_tree_lookup(&tree->buffer,
  3889. start >> PAGE_CACHE_SHIFT);
  3890. if (!atomic_inc_not_zero(&exists->refs)) {
  3891. spin_unlock(&tree->buffer_lock);
  3892. radix_tree_preload_end();
  3893. exists = NULL;
  3894. goto again;
  3895. }
  3896. spin_unlock(&tree->buffer_lock);
  3897. radix_tree_preload_end();
  3898. mark_extent_buffer_accessed(exists);
  3899. goto free_eb;
  3900. }
  3901. /* add one reference for the tree */
  3902. check_buffer_tree_ref(eb);
  3903. spin_unlock(&tree->buffer_lock);
  3904. radix_tree_preload_end();
  3905. /*
  3906. * there is a race where release page may have
  3907. * tried to find this extent buffer in the radix
  3908. * but failed. It will tell the VM it is safe to
  3909. * reclaim the, and it will clear the page private bit.
  3910. * We must make sure to set the page private bit properly
  3911. * after the extent buffer is in the radix tree so
  3912. * it doesn't get lost
  3913. */
  3914. SetPageChecked(eb->pages[0]);
  3915. for (i = 1; i < num_pages; i++) {
  3916. p = extent_buffer_page(eb, i);
  3917. ClearPageChecked(p);
  3918. unlock_page(p);
  3919. }
  3920. unlock_page(eb->pages[0]);
  3921. return eb;
  3922. free_eb:
  3923. for (i = 0; i < num_pages; i++) {
  3924. if (eb->pages[i])
  3925. unlock_page(eb->pages[i]);
  3926. }
  3927. WARN_ON(!atomic_dec_and_test(&eb->refs));
  3928. btrfs_release_extent_buffer(eb);
  3929. return exists;
  3930. }
  3931. struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
  3932. u64 start, unsigned long len)
  3933. {
  3934. struct extent_buffer *eb;
  3935. rcu_read_lock();
  3936. eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
  3937. if (eb && atomic_inc_not_zero(&eb->refs)) {
  3938. rcu_read_unlock();
  3939. mark_extent_buffer_accessed(eb);
  3940. return eb;
  3941. }
  3942. rcu_read_unlock();
  3943. return NULL;
  3944. }
  3945. static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
  3946. {
  3947. struct extent_buffer *eb =
  3948. container_of(head, struct extent_buffer, rcu_head);
  3949. __free_extent_buffer(eb);
  3950. }
  3951. /* Expects to have eb->eb_lock already held */
  3952. static int release_extent_buffer(struct extent_buffer *eb)
  3953. {
  3954. WARN_ON(atomic_read(&eb->refs) == 0);
  3955. if (atomic_dec_and_test(&eb->refs)) {
  3956. if (test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) {
  3957. spin_unlock(&eb->refs_lock);
  3958. } else {
  3959. struct extent_io_tree *tree = eb->tree;
  3960. spin_unlock(&eb->refs_lock);
  3961. spin_lock(&tree->buffer_lock);
  3962. radix_tree_delete(&tree->buffer,
  3963. eb->start >> PAGE_CACHE_SHIFT);
  3964. spin_unlock(&tree->buffer_lock);
  3965. }
  3966. /* Should be safe to release our pages at this point */
  3967. btrfs_release_extent_buffer_page(eb, 0);
  3968. call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
  3969. return 1;
  3970. }
  3971. spin_unlock(&eb->refs_lock);
  3972. return 0;
  3973. }
  3974. void free_extent_buffer(struct extent_buffer *eb)
  3975. {
  3976. int refs;
  3977. int old;
  3978. if (!eb)
  3979. return;
  3980. while (1) {
  3981. refs = atomic_read(&eb->refs);
  3982. if (refs <= 3)
  3983. break;
  3984. old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
  3985. if (old == refs)
  3986. return;
  3987. }
  3988. spin_lock(&eb->refs_lock);
  3989. if (atomic_read(&eb->refs) == 2 &&
  3990. test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
  3991. atomic_dec(&eb->refs);
  3992. if (atomic_read(&eb->refs) == 2 &&
  3993. test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
  3994. !extent_buffer_under_io(eb) &&
  3995. test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  3996. atomic_dec(&eb->refs);
  3997. /*
  3998. * I know this is terrible, but it's temporary until we stop tracking
  3999. * the uptodate bits and such for the extent buffers.
  4000. */
  4001. release_extent_buffer(eb);
  4002. }
  4003. void free_extent_buffer_stale(struct extent_buffer *eb)
  4004. {
  4005. if (!eb)
  4006. return;
  4007. spin_lock(&eb->refs_lock);
  4008. set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
  4009. if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
  4010. test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  4011. atomic_dec(&eb->refs);
  4012. release_extent_buffer(eb);
  4013. }
  4014. void clear_extent_buffer_dirty(struct extent_buffer *eb)
  4015. {
  4016. unsigned long i;
  4017. unsigned long num_pages;
  4018. struct page *page;
  4019. num_pages = num_extent_pages(eb->start, eb->len);
  4020. for (i = 0; i < num_pages; i++) {
  4021. page = extent_buffer_page(eb, i);
  4022. if (!PageDirty(page))
  4023. continue;
  4024. lock_page(page);
  4025. WARN_ON(!PagePrivate(page));
  4026. clear_page_dirty_for_io(page);
  4027. spin_lock_irq(&page->mapping->tree_lock);
  4028. if (!PageDirty(page)) {
  4029. radix_tree_tag_clear(&page->mapping->page_tree,
  4030. page_index(page),
  4031. PAGECACHE_TAG_DIRTY);
  4032. }
  4033. spin_unlock_irq(&page->mapping->tree_lock);
  4034. ClearPageError(page);
  4035. unlock_page(page);
  4036. }
  4037. WARN_ON(atomic_read(&eb->refs) == 0);
  4038. }
  4039. int set_extent_buffer_dirty(struct extent_buffer *eb)
  4040. {
  4041. unsigned long i;
  4042. unsigned long num_pages;
  4043. int was_dirty = 0;
  4044. check_buffer_tree_ref(eb);
  4045. was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
  4046. num_pages = num_extent_pages(eb->start, eb->len);
  4047. WARN_ON(atomic_read(&eb->refs) == 0);
  4048. WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
  4049. for (i = 0; i < num_pages; i++)
  4050. set_page_dirty(extent_buffer_page(eb, i));
  4051. return was_dirty;
  4052. }
  4053. int clear_extent_buffer_uptodate(struct extent_buffer *eb)
  4054. {
  4055. unsigned long i;
  4056. struct page *page;
  4057. unsigned long num_pages;
  4058. clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4059. num_pages = num_extent_pages(eb->start, eb->len);
  4060. for (i = 0; i < num_pages; i++) {
  4061. page = extent_buffer_page(eb, i);
  4062. if (page)
  4063. ClearPageUptodate(page);
  4064. }
  4065. return 0;
  4066. }
  4067. int set_extent_buffer_uptodate(struct extent_buffer *eb)
  4068. {
  4069. unsigned long i;
  4070. struct page *page;
  4071. unsigned long num_pages;
  4072. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4073. num_pages = num_extent_pages(eb->start, eb->len);
  4074. for (i = 0; i < num_pages; i++) {
  4075. page = extent_buffer_page(eb, i);
  4076. SetPageUptodate(page);
  4077. }
  4078. return 0;
  4079. }
  4080. int extent_buffer_uptodate(struct extent_buffer *eb)
  4081. {
  4082. return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4083. }
  4084. int read_extent_buffer_pages(struct extent_io_tree *tree,
  4085. struct extent_buffer *eb, u64 start, int wait,
  4086. get_extent_t *get_extent, int mirror_num)
  4087. {
  4088. unsigned long i;
  4089. unsigned long start_i;
  4090. struct page *page;
  4091. int err;
  4092. int ret = 0;
  4093. int locked_pages = 0;
  4094. int all_uptodate = 1;
  4095. unsigned long num_pages;
  4096. unsigned long num_reads = 0;
  4097. struct bio *bio = NULL;
  4098. unsigned long bio_flags = 0;
  4099. if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
  4100. return 0;
  4101. if (start) {
  4102. WARN_ON(start < eb->start);
  4103. start_i = (start >> PAGE_CACHE_SHIFT) -
  4104. (eb->start >> PAGE_CACHE_SHIFT);
  4105. } else {
  4106. start_i = 0;
  4107. }
  4108. num_pages = num_extent_pages(eb->start, eb->len);
  4109. for (i = start_i; i < num_pages; i++) {
  4110. page = extent_buffer_page(eb, i);
  4111. if (wait == WAIT_NONE) {
  4112. if (!trylock_page(page))
  4113. goto unlock_exit;
  4114. } else {
  4115. lock_page(page);
  4116. }
  4117. locked_pages++;
  4118. if (!PageUptodate(page)) {
  4119. num_reads++;
  4120. all_uptodate = 0;
  4121. }
  4122. }
  4123. if (all_uptodate) {
  4124. if (start_i == 0)
  4125. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4126. goto unlock_exit;
  4127. }
  4128. clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
  4129. eb->read_mirror = 0;
  4130. atomic_set(&eb->io_pages, num_reads);
  4131. for (i = start_i; i < num_pages; i++) {
  4132. page = extent_buffer_page(eb, i);
  4133. if (!PageUptodate(page)) {
  4134. ClearPageError(page);
  4135. err = __extent_read_full_page(tree, page,
  4136. get_extent, &bio,
  4137. mirror_num, &bio_flags,
  4138. READ | REQ_META);
  4139. if (err)
  4140. ret = err;
  4141. } else {
  4142. unlock_page(page);
  4143. }
  4144. }
  4145. if (bio) {
  4146. err = submit_one_bio(READ | REQ_META, bio, mirror_num,
  4147. bio_flags);
  4148. if (err)
  4149. return err;
  4150. }
  4151. if (ret || wait != WAIT_COMPLETE)
  4152. return ret;
  4153. for (i = start_i; i < num_pages; i++) {
  4154. page = extent_buffer_page(eb, i);
  4155. wait_on_page_locked(page);
  4156. if (!PageUptodate(page))
  4157. ret = -EIO;
  4158. }
  4159. return ret;
  4160. unlock_exit:
  4161. i = start_i;
  4162. while (locked_pages > 0) {
  4163. page = extent_buffer_page(eb, i);
  4164. i++;
  4165. unlock_page(page);
  4166. locked_pages--;
  4167. }
  4168. return ret;
  4169. }
  4170. void read_extent_buffer(struct extent_buffer *eb, void *dstv,
  4171. unsigned long start,
  4172. unsigned long len)
  4173. {
  4174. size_t cur;
  4175. size_t offset;
  4176. struct page *page;
  4177. char *kaddr;
  4178. char *dst = (char *)dstv;
  4179. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  4180. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  4181. WARN_ON(start > eb->len);
  4182. WARN_ON(start + len > eb->start + eb->len);
  4183. offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
  4184. while (len > 0) {
  4185. page = extent_buffer_page(eb, i);
  4186. cur = min(len, (PAGE_CACHE_SIZE - offset));
  4187. kaddr = page_address(page);
  4188. memcpy(dst, kaddr + offset, cur);
  4189. dst += cur;
  4190. len -= cur;
  4191. offset = 0;
  4192. i++;
  4193. }
  4194. }
  4195. int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
  4196. unsigned long min_len, char **map,
  4197. unsigned long *map_start,
  4198. unsigned long *map_len)
  4199. {
  4200. size_t offset = start & (PAGE_CACHE_SIZE - 1);
  4201. char *kaddr;
  4202. struct page *p;
  4203. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  4204. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  4205. unsigned long end_i = (start_offset + start + min_len - 1) >>
  4206. PAGE_CACHE_SHIFT;
  4207. if (i != end_i)
  4208. return -EINVAL;
  4209. if (i == 0) {
  4210. offset = start_offset;
  4211. *map_start = 0;
  4212. } else {
  4213. offset = 0;
  4214. *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
  4215. }
  4216. if (start + min_len > eb->len) {
  4217. WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
  4218. "wanted %lu %lu\n", (unsigned long long)eb->start,
  4219. eb->len, start, min_len);
  4220. return -EINVAL;
  4221. }
  4222. p = extent_buffer_page(eb, i);
  4223. kaddr = page_address(p);
  4224. *map = kaddr + offset;
  4225. *map_len = PAGE_CACHE_SIZE - offset;
  4226. return 0;
  4227. }
  4228. int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
  4229. unsigned long start,
  4230. unsigned long len)
  4231. {
  4232. size_t cur;
  4233. size_t offset;
  4234. struct page *page;
  4235. char *kaddr;
  4236. char *ptr = (char *)ptrv;
  4237. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  4238. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  4239. int ret = 0;
  4240. WARN_ON(start > eb->len);
  4241. WARN_ON(start + len > eb->start + eb->len);
  4242. offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
  4243. while (len > 0) {
  4244. page = extent_buffer_page(eb, i);
  4245. cur = min(len, (PAGE_CACHE_SIZE - offset));
  4246. kaddr = page_address(page);
  4247. ret = memcmp(ptr, kaddr + offset, cur);
  4248. if (ret)
  4249. break;
  4250. ptr += cur;
  4251. len -= cur;
  4252. offset = 0;
  4253. i++;
  4254. }
  4255. return ret;
  4256. }
  4257. void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
  4258. unsigned long start, unsigned long len)
  4259. {
  4260. size_t cur;
  4261. size_t offset;
  4262. struct page *page;
  4263. char *kaddr;
  4264. char *src = (char *)srcv;
  4265. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  4266. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  4267. WARN_ON(start > eb->len);
  4268. WARN_ON(start + len > eb->start + eb->len);
  4269. offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
  4270. while (len > 0) {
  4271. page = extent_buffer_page(eb, i);
  4272. WARN_ON(!PageUptodate(page));
  4273. cur = min(len, PAGE_CACHE_SIZE - offset);
  4274. kaddr = page_address(page);
  4275. memcpy(kaddr + offset, src, cur);
  4276. src += cur;
  4277. len -= cur;
  4278. offset = 0;
  4279. i++;
  4280. }
  4281. }
  4282. void memset_extent_buffer(struct extent_buffer *eb, char c,
  4283. unsigned long start, unsigned long len)
  4284. {
  4285. size_t cur;
  4286. size_t offset;
  4287. struct page *page;
  4288. char *kaddr;
  4289. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  4290. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  4291. WARN_ON(start > eb->len);
  4292. WARN_ON(start + len > eb->start + eb->len);
  4293. offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
  4294. while (len > 0) {
  4295. page = extent_buffer_page(eb, i);
  4296. WARN_ON(!PageUptodate(page));
  4297. cur = min(len, PAGE_CACHE_SIZE - offset);
  4298. kaddr = page_address(page);
  4299. memset(kaddr + offset, c, cur);
  4300. len -= cur;
  4301. offset = 0;
  4302. i++;
  4303. }
  4304. }
  4305. void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
  4306. unsigned long dst_offset, unsigned long src_offset,
  4307. unsigned long len)
  4308. {
  4309. u64 dst_len = dst->len;
  4310. size_t cur;
  4311. size_t offset;
  4312. struct page *page;
  4313. char *kaddr;
  4314. size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
  4315. unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
  4316. WARN_ON(src->len != dst_len);
  4317. offset = (start_offset + dst_offset) &
  4318. ((unsigned long)PAGE_CACHE_SIZE - 1);
  4319. while (len > 0) {
  4320. page = extent_buffer_page(dst, i);
  4321. WARN_ON(!PageUptodate(page));
  4322. cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
  4323. kaddr = page_address(page);
  4324. read_extent_buffer(src, kaddr + offset, src_offset, cur);
  4325. src_offset += cur;
  4326. len -= cur;
  4327. offset = 0;
  4328. i++;
  4329. }
  4330. }
  4331. static void move_pages(struct page *dst_page, struct page *src_page,
  4332. unsigned long dst_off, unsigned long src_off,
  4333. unsigned long len)
  4334. {
  4335. char *dst_kaddr = page_address(dst_page);
  4336. if (dst_page == src_page) {
  4337. memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
  4338. } else {
  4339. char *src_kaddr = page_address(src_page);
  4340. char *p = dst_kaddr + dst_off + len;
  4341. char *s = src_kaddr + src_off + len;
  4342. while (len--)
  4343. *--p = *--s;
  4344. }
  4345. }
  4346. static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
  4347. {
  4348. unsigned long distance = (src > dst) ? src - dst : dst - src;
  4349. return distance < len;
  4350. }
  4351. static void copy_pages(struct page *dst_page, struct page *src_page,
  4352. unsigned long dst_off, unsigned long src_off,
  4353. unsigned long len)
  4354. {
  4355. char *dst_kaddr = page_address(dst_page);
  4356. char *src_kaddr;
  4357. int must_memmove = 0;
  4358. if (dst_page != src_page) {
  4359. src_kaddr = page_address(src_page);
  4360. } else {
  4361. src_kaddr = dst_kaddr;
  4362. if (areas_overlap(src_off, dst_off, len))
  4363. must_memmove = 1;
  4364. }
  4365. if (must_memmove)
  4366. memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
  4367. else
  4368. memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
  4369. }
  4370. void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
  4371. unsigned long src_offset, unsigned long len)
  4372. {
  4373. size_t cur;
  4374. size_t dst_off_in_page;
  4375. size_t src_off_in_page;
  4376. size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
  4377. unsigned long dst_i;
  4378. unsigned long src_i;
  4379. if (src_offset + len > dst->len) {
  4380. printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
  4381. "len %lu dst len %lu\n", src_offset, len, dst->len);
  4382. BUG_ON(1);
  4383. }
  4384. if (dst_offset + len > dst->len) {
  4385. printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
  4386. "len %lu dst len %lu\n", dst_offset, len, dst->len);
  4387. BUG_ON(1);
  4388. }
  4389. while (len > 0) {
  4390. dst_off_in_page = (start_offset + dst_offset) &
  4391. ((unsigned long)PAGE_CACHE_SIZE - 1);
  4392. src_off_in_page = (start_offset + src_offset) &
  4393. ((unsigned long)PAGE_CACHE_SIZE - 1);
  4394. dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
  4395. src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
  4396. cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
  4397. src_off_in_page));
  4398. cur = min_t(unsigned long, cur,
  4399. (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
  4400. copy_pages(extent_buffer_page(dst, dst_i),
  4401. extent_buffer_page(dst, src_i),
  4402. dst_off_in_page, src_off_in_page, cur);
  4403. src_offset += cur;
  4404. dst_offset += cur;
  4405. len -= cur;
  4406. }
  4407. }
  4408. void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
  4409. unsigned long src_offset, unsigned long len)
  4410. {
  4411. size_t cur;
  4412. size_t dst_off_in_page;
  4413. size_t src_off_in_page;
  4414. unsigned long dst_end = dst_offset + len - 1;
  4415. unsigned long src_end = src_offset + len - 1;
  4416. size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
  4417. unsigned long dst_i;
  4418. unsigned long src_i;
  4419. if (src_offset + len > dst->len) {
  4420. printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
  4421. "len %lu len %lu\n", src_offset, len, dst->len);
  4422. BUG_ON(1);
  4423. }
  4424. if (dst_offset + len > dst->len) {
  4425. printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
  4426. "len %lu len %lu\n", dst_offset, len, dst->len);
  4427. BUG_ON(1);
  4428. }
  4429. if (dst_offset < src_offset) {
  4430. memcpy_extent_buffer(dst, dst_offset, src_offset, len);
  4431. return;
  4432. }
  4433. while (len > 0) {
  4434. dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
  4435. src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
  4436. dst_off_in_page = (start_offset + dst_end) &
  4437. ((unsigned long)PAGE_CACHE_SIZE - 1);
  4438. src_off_in_page = (start_offset + src_end) &
  4439. ((unsigned long)PAGE_CACHE_SIZE - 1);
  4440. cur = min_t(unsigned long, len, src_off_in_page + 1);
  4441. cur = min(cur, dst_off_in_page + 1);
  4442. move_pages(extent_buffer_page(dst, dst_i),
  4443. extent_buffer_page(dst, src_i),
  4444. dst_off_in_page - cur + 1,
  4445. src_off_in_page - cur + 1, cur);
  4446. dst_end -= cur;
  4447. src_end -= cur;
  4448. len -= cur;
  4449. }
  4450. }
  4451. int try_release_extent_buffer(struct page *page)
  4452. {
  4453. struct extent_buffer *eb;
  4454. /*
  4455. * We need to make sure noboody is attaching this page to an eb right
  4456. * now.
  4457. */
  4458. spin_lock(&page->mapping->private_lock);
  4459. if (!PagePrivate(page)) {
  4460. spin_unlock(&page->mapping->private_lock);
  4461. return 1;
  4462. }
  4463. eb = (struct extent_buffer *)page->private;
  4464. BUG_ON(!eb);
  4465. /*
  4466. * This is a little awful but should be ok, we need to make sure that
  4467. * the eb doesn't disappear out from under us while we're looking at
  4468. * this page.
  4469. */
  4470. spin_lock(&eb->refs_lock);
  4471. if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
  4472. spin_unlock(&eb->refs_lock);
  4473. spin_unlock(&page->mapping->private_lock);
  4474. return 0;
  4475. }
  4476. spin_unlock(&page->mapping->private_lock);
  4477. /*
  4478. * If tree ref isn't set then we know the ref on this eb is a real ref,
  4479. * so just return, this page will likely be freed soon anyway.
  4480. */
  4481. if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
  4482. spin_unlock(&eb->refs_lock);
  4483. return 0;
  4484. }
  4485. return release_extent_buffer(eb);
  4486. }