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