extent_io.c 141 KB

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