extent_io.c 130 KB

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