extent_io.c 128 KB

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