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