extent_io.c 131 KB

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