extent_io.c 127 KB

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