extent_io.c 135 KB

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