extent_io.c 130 KB

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