extent_io.c 130 KB

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