extent_io.c 140 KB

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