tree-log.c 163 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2008 Oracle. All rights reserved.
  4. */
  5. #include <linux/sched.h>
  6. #include <linux/slab.h>
  7. #include <linux/blkdev.h>
  8. #include <linux/list_sort.h>
  9. #include <linux/iversion.h>
  10. #include "ctree.h"
  11. #include "tree-log.h"
  12. #include "disk-io.h"
  13. #include "locking.h"
  14. #include "print-tree.h"
  15. #include "backref.h"
  16. #include "compression.h"
  17. #include "qgroup.h"
  18. #include "inode-map.h"
  19. /* magic values for the inode_only field in btrfs_log_inode:
  20. *
  21. * LOG_INODE_ALL means to log everything
  22. * LOG_INODE_EXISTS means to log just enough to recreate the inode
  23. * during log replay
  24. */
  25. #define LOG_INODE_ALL 0
  26. #define LOG_INODE_EXISTS 1
  27. #define LOG_OTHER_INODE 2
  28. /*
  29. * directory trouble cases
  30. *
  31. * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
  32. * log, we must force a full commit before doing an fsync of the directory
  33. * where the unlink was done.
  34. * ---> record transid of last unlink/rename per directory
  35. *
  36. * mkdir foo/some_dir
  37. * normal commit
  38. * rename foo/some_dir foo2/some_dir
  39. * mkdir foo/some_dir
  40. * fsync foo/some_dir/some_file
  41. *
  42. * The fsync above will unlink the original some_dir without recording
  43. * it in its new location (foo2). After a crash, some_dir will be gone
  44. * unless the fsync of some_file forces a full commit
  45. *
  46. * 2) we must log any new names for any file or dir that is in the fsync
  47. * log. ---> check inode while renaming/linking.
  48. *
  49. * 2a) we must log any new names for any file or dir during rename
  50. * when the directory they are being removed from was logged.
  51. * ---> check inode and old parent dir during rename
  52. *
  53. * 2a is actually the more important variant. With the extra logging
  54. * a crash might unlink the old name without recreating the new one
  55. *
  56. * 3) after a crash, we must go through any directories with a link count
  57. * of zero and redo the rm -rf
  58. *
  59. * mkdir f1/foo
  60. * normal commit
  61. * rm -rf f1/foo
  62. * fsync(f1)
  63. *
  64. * The directory f1 was fully removed from the FS, but fsync was never
  65. * called on f1, only its parent dir. After a crash the rm -rf must
  66. * be replayed. This must be able to recurse down the entire
  67. * directory tree. The inode link count fixup code takes care of the
  68. * ugly details.
  69. */
  70. /*
  71. * stages for the tree walking. The first
  72. * stage (0) is to only pin down the blocks we find
  73. * the second stage (1) is to make sure that all the inodes
  74. * we find in the log are created in the subvolume.
  75. *
  76. * The last stage is to deal with directories and links and extents
  77. * and all the other fun semantics
  78. */
  79. #define LOG_WALK_PIN_ONLY 0
  80. #define LOG_WALK_REPLAY_INODES 1
  81. #define LOG_WALK_REPLAY_DIR_INDEX 2
  82. #define LOG_WALK_REPLAY_ALL 3
  83. static int btrfs_log_inode(struct btrfs_trans_handle *trans,
  84. struct btrfs_root *root, struct btrfs_inode *inode,
  85. int inode_only,
  86. const loff_t start,
  87. const loff_t end,
  88. struct btrfs_log_ctx *ctx);
  89. static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
  90. struct btrfs_root *root,
  91. struct btrfs_path *path, u64 objectid);
  92. static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
  93. struct btrfs_root *root,
  94. struct btrfs_root *log,
  95. struct btrfs_path *path,
  96. u64 dirid, int del_all);
  97. /*
  98. * tree logging is a special write ahead log used to make sure that
  99. * fsyncs and O_SYNCs can happen without doing full tree commits.
  100. *
  101. * Full tree commits are expensive because they require commonly
  102. * modified blocks to be recowed, creating many dirty pages in the
  103. * extent tree an 4x-6x higher write load than ext3.
  104. *
  105. * Instead of doing a tree commit on every fsync, we use the
  106. * key ranges and transaction ids to find items for a given file or directory
  107. * that have changed in this transaction. Those items are copied into
  108. * a special tree (one per subvolume root), that tree is written to disk
  109. * and then the fsync is considered complete.
  110. *
  111. * After a crash, items are copied out of the log-tree back into the
  112. * subvolume tree. Any file data extents found are recorded in the extent
  113. * allocation tree, and the log-tree freed.
  114. *
  115. * The log tree is read three times, once to pin down all the extents it is
  116. * using in ram and once, once to create all the inodes logged in the tree
  117. * and once to do all the other items.
  118. */
  119. /*
  120. * start a sub transaction and setup the log tree
  121. * this increments the log tree writer count to make the people
  122. * syncing the tree wait for us to finish
  123. */
  124. static int start_log_trans(struct btrfs_trans_handle *trans,
  125. struct btrfs_root *root,
  126. struct btrfs_log_ctx *ctx)
  127. {
  128. struct btrfs_fs_info *fs_info = root->fs_info;
  129. int ret = 0;
  130. mutex_lock(&root->log_mutex);
  131. if (root->log_root) {
  132. if (btrfs_need_log_full_commit(fs_info, trans)) {
  133. ret = -EAGAIN;
  134. goto out;
  135. }
  136. if (!root->log_start_pid) {
  137. clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
  138. root->log_start_pid = current->pid;
  139. } else if (root->log_start_pid != current->pid) {
  140. set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
  141. }
  142. } else {
  143. mutex_lock(&fs_info->tree_log_mutex);
  144. if (!fs_info->log_root_tree)
  145. ret = btrfs_init_log_root_tree(trans, fs_info);
  146. mutex_unlock(&fs_info->tree_log_mutex);
  147. if (ret)
  148. goto out;
  149. ret = btrfs_add_log_tree(trans, root);
  150. if (ret)
  151. goto out;
  152. clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
  153. root->log_start_pid = current->pid;
  154. }
  155. atomic_inc(&root->log_batch);
  156. atomic_inc(&root->log_writers);
  157. if (ctx) {
  158. int index = root->log_transid % 2;
  159. list_add_tail(&ctx->list, &root->log_ctxs[index]);
  160. ctx->log_transid = root->log_transid;
  161. }
  162. out:
  163. mutex_unlock(&root->log_mutex);
  164. return ret;
  165. }
  166. /*
  167. * returns 0 if there was a log transaction running and we were able
  168. * to join, or returns -ENOENT if there were not transactions
  169. * in progress
  170. */
  171. static int join_running_log_trans(struct btrfs_root *root)
  172. {
  173. int ret = -ENOENT;
  174. smp_mb();
  175. if (!root->log_root)
  176. return -ENOENT;
  177. mutex_lock(&root->log_mutex);
  178. if (root->log_root) {
  179. ret = 0;
  180. atomic_inc(&root->log_writers);
  181. }
  182. mutex_unlock(&root->log_mutex);
  183. return ret;
  184. }
  185. /*
  186. * This either makes the current running log transaction wait
  187. * until you call btrfs_end_log_trans() or it makes any future
  188. * log transactions wait until you call btrfs_end_log_trans()
  189. */
  190. int btrfs_pin_log_trans(struct btrfs_root *root)
  191. {
  192. int ret = -ENOENT;
  193. mutex_lock(&root->log_mutex);
  194. atomic_inc(&root->log_writers);
  195. mutex_unlock(&root->log_mutex);
  196. return ret;
  197. }
  198. /*
  199. * indicate we're done making changes to the log tree
  200. * and wake up anyone waiting to do a sync
  201. */
  202. void btrfs_end_log_trans(struct btrfs_root *root)
  203. {
  204. if (atomic_dec_and_test(&root->log_writers)) {
  205. /*
  206. * Implicit memory barrier after atomic_dec_and_test
  207. */
  208. if (waitqueue_active(&root->log_writer_wait))
  209. wake_up(&root->log_writer_wait);
  210. }
  211. }
  212. /*
  213. * the walk control struct is used to pass state down the chain when
  214. * processing the log tree. The stage field tells us which part
  215. * of the log tree processing we are currently doing. The others
  216. * are state fields used for that specific part
  217. */
  218. struct walk_control {
  219. /* should we free the extent on disk when done? This is used
  220. * at transaction commit time while freeing a log tree
  221. */
  222. int free;
  223. /* should we write out the extent buffer? This is used
  224. * while flushing the log tree to disk during a sync
  225. */
  226. int write;
  227. /* should we wait for the extent buffer io to finish? Also used
  228. * while flushing the log tree to disk for a sync
  229. */
  230. int wait;
  231. /* pin only walk, we record which extents on disk belong to the
  232. * log trees
  233. */
  234. int pin;
  235. /* what stage of the replay code we're currently in */
  236. int stage;
  237. /* the root we are currently replaying */
  238. struct btrfs_root *replay_dest;
  239. /* the trans handle for the current replay */
  240. struct btrfs_trans_handle *trans;
  241. /* the function that gets used to process blocks we find in the
  242. * tree. Note the extent_buffer might not be up to date when it is
  243. * passed in, and it must be checked or read if you need the data
  244. * inside it
  245. */
  246. int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
  247. struct walk_control *wc, u64 gen, int level);
  248. };
  249. /*
  250. * process_func used to pin down extents, write them or wait on them
  251. */
  252. static int process_one_buffer(struct btrfs_root *log,
  253. struct extent_buffer *eb,
  254. struct walk_control *wc, u64 gen, int level)
  255. {
  256. struct btrfs_fs_info *fs_info = log->fs_info;
  257. int ret = 0;
  258. /*
  259. * If this fs is mixed then we need to be able to process the leaves to
  260. * pin down any logged extents, so we have to read the block.
  261. */
  262. if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
  263. ret = btrfs_read_buffer(eb, gen, level, NULL);
  264. if (ret)
  265. return ret;
  266. }
  267. if (wc->pin)
  268. ret = btrfs_pin_extent_for_log_replay(fs_info, eb->start,
  269. eb->len);
  270. if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
  271. if (wc->pin && btrfs_header_level(eb) == 0)
  272. ret = btrfs_exclude_logged_extents(fs_info, eb);
  273. if (wc->write)
  274. btrfs_write_tree_block(eb);
  275. if (wc->wait)
  276. btrfs_wait_tree_block_writeback(eb);
  277. }
  278. return ret;
  279. }
  280. /*
  281. * Item overwrite used by replay and tree logging. eb, slot and key all refer
  282. * to the src data we are copying out.
  283. *
  284. * root is the tree we are copying into, and path is a scratch
  285. * path for use in this function (it should be released on entry and
  286. * will be released on exit).
  287. *
  288. * If the key is already in the destination tree the existing item is
  289. * overwritten. If the existing item isn't big enough, it is extended.
  290. * If it is too large, it is truncated.
  291. *
  292. * If the key isn't in the destination yet, a new item is inserted.
  293. */
  294. static noinline int overwrite_item(struct btrfs_trans_handle *trans,
  295. struct btrfs_root *root,
  296. struct btrfs_path *path,
  297. struct extent_buffer *eb, int slot,
  298. struct btrfs_key *key)
  299. {
  300. struct btrfs_fs_info *fs_info = root->fs_info;
  301. int ret;
  302. u32 item_size;
  303. u64 saved_i_size = 0;
  304. int save_old_i_size = 0;
  305. unsigned long src_ptr;
  306. unsigned long dst_ptr;
  307. int overwrite_root = 0;
  308. bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
  309. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  310. overwrite_root = 1;
  311. item_size = btrfs_item_size_nr(eb, slot);
  312. src_ptr = btrfs_item_ptr_offset(eb, slot);
  313. /* look for the key in the destination tree */
  314. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  315. if (ret < 0)
  316. return ret;
  317. if (ret == 0) {
  318. char *src_copy;
  319. char *dst_copy;
  320. u32 dst_size = btrfs_item_size_nr(path->nodes[0],
  321. path->slots[0]);
  322. if (dst_size != item_size)
  323. goto insert;
  324. if (item_size == 0) {
  325. btrfs_release_path(path);
  326. return 0;
  327. }
  328. dst_copy = kmalloc(item_size, GFP_NOFS);
  329. src_copy = kmalloc(item_size, GFP_NOFS);
  330. if (!dst_copy || !src_copy) {
  331. btrfs_release_path(path);
  332. kfree(dst_copy);
  333. kfree(src_copy);
  334. return -ENOMEM;
  335. }
  336. read_extent_buffer(eb, src_copy, src_ptr, item_size);
  337. dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
  338. read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
  339. item_size);
  340. ret = memcmp(dst_copy, src_copy, item_size);
  341. kfree(dst_copy);
  342. kfree(src_copy);
  343. /*
  344. * they have the same contents, just return, this saves
  345. * us from cowing blocks in the destination tree and doing
  346. * extra writes that may not have been done by a previous
  347. * sync
  348. */
  349. if (ret == 0) {
  350. btrfs_release_path(path);
  351. return 0;
  352. }
  353. /*
  354. * We need to load the old nbytes into the inode so when we
  355. * replay the extents we've logged we get the right nbytes.
  356. */
  357. if (inode_item) {
  358. struct btrfs_inode_item *item;
  359. u64 nbytes;
  360. u32 mode;
  361. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  362. struct btrfs_inode_item);
  363. nbytes = btrfs_inode_nbytes(path->nodes[0], item);
  364. item = btrfs_item_ptr(eb, slot,
  365. struct btrfs_inode_item);
  366. btrfs_set_inode_nbytes(eb, item, nbytes);
  367. /*
  368. * If this is a directory we need to reset the i_size to
  369. * 0 so that we can set it up properly when replaying
  370. * the rest of the items in this log.
  371. */
  372. mode = btrfs_inode_mode(eb, item);
  373. if (S_ISDIR(mode))
  374. btrfs_set_inode_size(eb, item, 0);
  375. }
  376. } else if (inode_item) {
  377. struct btrfs_inode_item *item;
  378. u32 mode;
  379. /*
  380. * New inode, set nbytes to 0 so that the nbytes comes out
  381. * properly when we replay the extents.
  382. */
  383. item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
  384. btrfs_set_inode_nbytes(eb, item, 0);
  385. /*
  386. * If this is a directory we need to reset the i_size to 0 so
  387. * that we can set it up properly when replaying the rest of
  388. * the items in this log.
  389. */
  390. mode = btrfs_inode_mode(eb, item);
  391. if (S_ISDIR(mode))
  392. btrfs_set_inode_size(eb, item, 0);
  393. }
  394. insert:
  395. btrfs_release_path(path);
  396. /* try to insert the key into the destination tree */
  397. path->skip_release_on_error = 1;
  398. ret = btrfs_insert_empty_item(trans, root, path,
  399. key, item_size);
  400. path->skip_release_on_error = 0;
  401. /* make sure any existing item is the correct size */
  402. if (ret == -EEXIST || ret == -EOVERFLOW) {
  403. u32 found_size;
  404. found_size = btrfs_item_size_nr(path->nodes[0],
  405. path->slots[0]);
  406. if (found_size > item_size)
  407. btrfs_truncate_item(fs_info, path, item_size, 1);
  408. else if (found_size < item_size)
  409. btrfs_extend_item(fs_info, path,
  410. item_size - found_size);
  411. } else if (ret) {
  412. return ret;
  413. }
  414. dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
  415. path->slots[0]);
  416. /* don't overwrite an existing inode if the generation number
  417. * was logged as zero. This is done when the tree logging code
  418. * is just logging an inode to make sure it exists after recovery.
  419. *
  420. * Also, don't overwrite i_size on directories during replay.
  421. * log replay inserts and removes directory items based on the
  422. * state of the tree found in the subvolume, and i_size is modified
  423. * as it goes
  424. */
  425. if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
  426. struct btrfs_inode_item *src_item;
  427. struct btrfs_inode_item *dst_item;
  428. src_item = (struct btrfs_inode_item *)src_ptr;
  429. dst_item = (struct btrfs_inode_item *)dst_ptr;
  430. if (btrfs_inode_generation(eb, src_item) == 0) {
  431. struct extent_buffer *dst_eb = path->nodes[0];
  432. const u64 ino_size = btrfs_inode_size(eb, src_item);
  433. /*
  434. * For regular files an ino_size == 0 is used only when
  435. * logging that an inode exists, as part of a directory
  436. * fsync, and the inode wasn't fsynced before. In this
  437. * case don't set the size of the inode in the fs/subvol
  438. * tree, otherwise we would be throwing valid data away.
  439. */
  440. if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
  441. S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
  442. ino_size != 0) {
  443. struct btrfs_map_token token;
  444. btrfs_init_map_token(&token);
  445. btrfs_set_token_inode_size(dst_eb, dst_item,
  446. ino_size, &token);
  447. }
  448. goto no_copy;
  449. }
  450. if (overwrite_root &&
  451. S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
  452. S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
  453. save_old_i_size = 1;
  454. saved_i_size = btrfs_inode_size(path->nodes[0],
  455. dst_item);
  456. }
  457. }
  458. copy_extent_buffer(path->nodes[0], eb, dst_ptr,
  459. src_ptr, item_size);
  460. if (save_old_i_size) {
  461. struct btrfs_inode_item *dst_item;
  462. dst_item = (struct btrfs_inode_item *)dst_ptr;
  463. btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
  464. }
  465. /* make sure the generation is filled in */
  466. if (key->type == BTRFS_INODE_ITEM_KEY) {
  467. struct btrfs_inode_item *dst_item;
  468. dst_item = (struct btrfs_inode_item *)dst_ptr;
  469. if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
  470. btrfs_set_inode_generation(path->nodes[0], dst_item,
  471. trans->transid);
  472. }
  473. }
  474. no_copy:
  475. btrfs_mark_buffer_dirty(path->nodes[0]);
  476. btrfs_release_path(path);
  477. return 0;
  478. }
  479. /*
  480. * simple helper to read an inode off the disk from a given root
  481. * This can only be called for subvolume roots and not for the log
  482. */
  483. static noinline struct inode *read_one_inode(struct btrfs_root *root,
  484. u64 objectid)
  485. {
  486. struct btrfs_key key;
  487. struct inode *inode;
  488. key.objectid = objectid;
  489. key.type = BTRFS_INODE_ITEM_KEY;
  490. key.offset = 0;
  491. inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
  492. if (IS_ERR(inode)) {
  493. inode = NULL;
  494. } else if (is_bad_inode(inode)) {
  495. iput(inode);
  496. inode = NULL;
  497. }
  498. return inode;
  499. }
  500. /* replays a single extent in 'eb' at 'slot' with 'key' into the
  501. * subvolume 'root'. path is released on entry and should be released
  502. * on exit.
  503. *
  504. * extents in the log tree have not been allocated out of the extent
  505. * tree yet. So, this completes the allocation, taking a reference
  506. * as required if the extent already exists or creating a new extent
  507. * if it isn't in the extent allocation tree yet.
  508. *
  509. * The extent is inserted into the file, dropping any existing extents
  510. * from the file that overlap the new one.
  511. */
  512. static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
  513. struct btrfs_root *root,
  514. struct btrfs_path *path,
  515. struct extent_buffer *eb, int slot,
  516. struct btrfs_key *key)
  517. {
  518. struct btrfs_fs_info *fs_info = root->fs_info;
  519. int found_type;
  520. u64 extent_end;
  521. u64 start = key->offset;
  522. u64 nbytes = 0;
  523. struct btrfs_file_extent_item *item;
  524. struct inode *inode = NULL;
  525. unsigned long size;
  526. int ret = 0;
  527. item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  528. found_type = btrfs_file_extent_type(eb, item);
  529. if (found_type == BTRFS_FILE_EXTENT_REG ||
  530. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  531. nbytes = btrfs_file_extent_num_bytes(eb, item);
  532. extent_end = start + nbytes;
  533. /*
  534. * We don't add to the inodes nbytes if we are prealloc or a
  535. * hole.
  536. */
  537. if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
  538. nbytes = 0;
  539. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  540. size = btrfs_file_extent_inline_len(eb, slot, item);
  541. nbytes = btrfs_file_extent_ram_bytes(eb, item);
  542. extent_end = ALIGN(start + size,
  543. fs_info->sectorsize);
  544. } else {
  545. ret = 0;
  546. goto out;
  547. }
  548. inode = read_one_inode(root, key->objectid);
  549. if (!inode) {
  550. ret = -EIO;
  551. goto out;
  552. }
  553. /*
  554. * first check to see if we already have this extent in the
  555. * file. This must be done before the btrfs_drop_extents run
  556. * so we don't try to drop this extent.
  557. */
  558. ret = btrfs_lookup_file_extent(trans, root, path,
  559. btrfs_ino(BTRFS_I(inode)), start, 0);
  560. if (ret == 0 &&
  561. (found_type == BTRFS_FILE_EXTENT_REG ||
  562. found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
  563. struct btrfs_file_extent_item cmp1;
  564. struct btrfs_file_extent_item cmp2;
  565. struct btrfs_file_extent_item *existing;
  566. struct extent_buffer *leaf;
  567. leaf = path->nodes[0];
  568. existing = btrfs_item_ptr(leaf, path->slots[0],
  569. struct btrfs_file_extent_item);
  570. read_extent_buffer(eb, &cmp1, (unsigned long)item,
  571. sizeof(cmp1));
  572. read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
  573. sizeof(cmp2));
  574. /*
  575. * we already have a pointer to this exact extent,
  576. * we don't have to do anything
  577. */
  578. if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
  579. btrfs_release_path(path);
  580. goto out;
  581. }
  582. }
  583. btrfs_release_path(path);
  584. /* drop any overlapping extents */
  585. ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
  586. if (ret)
  587. goto out;
  588. if (found_type == BTRFS_FILE_EXTENT_REG ||
  589. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  590. u64 offset;
  591. unsigned long dest_offset;
  592. struct btrfs_key ins;
  593. if (btrfs_file_extent_disk_bytenr(eb, item) == 0 &&
  594. btrfs_fs_incompat(fs_info, NO_HOLES))
  595. goto update_inode;
  596. ret = btrfs_insert_empty_item(trans, root, path, key,
  597. sizeof(*item));
  598. if (ret)
  599. goto out;
  600. dest_offset = btrfs_item_ptr_offset(path->nodes[0],
  601. path->slots[0]);
  602. copy_extent_buffer(path->nodes[0], eb, dest_offset,
  603. (unsigned long)item, sizeof(*item));
  604. ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
  605. ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
  606. ins.type = BTRFS_EXTENT_ITEM_KEY;
  607. offset = key->offset - btrfs_file_extent_offset(eb, item);
  608. /*
  609. * Manually record dirty extent, as here we did a shallow
  610. * file extent item copy and skip normal backref update,
  611. * but modifying extent tree all by ourselves.
  612. * So need to manually record dirty extent for qgroup,
  613. * as the owner of the file extent changed from log tree
  614. * (doesn't affect qgroup) to fs/file tree(affects qgroup)
  615. */
  616. ret = btrfs_qgroup_trace_extent(trans, fs_info,
  617. btrfs_file_extent_disk_bytenr(eb, item),
  618. btrfs_file_extent_disk_num_bytes(eb, item),
  619. GFP_NOFS);
  620. if (ret < 0)
  621. goto out;
  622. if (ins.objectid > 0) {
  623. u64 csum_start;
  624. u64 csum_end;
  625. LIST_HEAD(ordered_sums);
  626. /*
  627. * is this extent already allocated in the extent
  628. * allocation tree? If so, just add a reference
  629. */
  630. ret = btrfs_lookup_data_extent(fs_info, ins.objectid,
  631. ins.offset);
  632. if (ret == 0) {
  633. ret = btrfs_inc_extent_ref(trans, root,
  634. ins.objectid, ins.offset,
  635. 0, root->root_key.objectid,
  636. key->objectid, offset);
  637. if (ret)
  638. goto out;
  639. } else {
  640. /*
  641. * insert the extent pointer in the extent
  642. * allocation tree
  643. */
  644. ret = btrfs_alloc_logged_file_extent(trans,
  645. fs_info,
  646. root->root_key.objectid,
  647. key->objectid, offset, &ins);
  648. if (ret)
  649. goto out;
  650. }
  651. btrfs_release_path(path);
  652. if (btrfs_file_extent_compression(eb, item)) {
  653. csum_start = ins.objectid;
  654. csum_end = csum_start + ins.offset;
  655. } else {
  656. csum_start = ins.objectid +
  657. btrfs_file_extent_offset(eb, item);
  658. csum_end = csum_start +
  659. btrfs_file_extent_num_bytes(eb, item);
  660. }
  661. ret = btrfs_lookup_csums_range(root->log_root,
  662. csum_start, csum_end - 1,
  663. &ordered_sums, 0);
  664. if (ret)
  665. goto out;
  666. /*
  667. * Now delete all existing cums in the csum root that
  668. * cover our range. We do this because we can have an
  669. * extent that is completely referenced by one file
  670. * extent item and partially referenced by another
  671. * file extent item (like after using the clone or
  672. * extent_same ioctls). In this case if we end up doing
  673. * the replay of the one that partially references the
  674. * extent first, and we do not do the csum deletion
  675. * below, we can get 2 csum items in the csum tree that
  676. * overlap each other. For example, imagine our log has
  677. * the two following file extent items:
  678. *
  679. * key (257 EXTENT_DATA 409600)
  680. * extent data disk byte 12845056 nr 102400
  681. * extent data offset 20480 nr 20480 ram 102400
  682. *
  683. * key (257 EXTENT_DATA 819200)
  684. * extent data disk byte 12845056 nr 102400
  685. * extent data offset 0 nr 102400 ram 102400
  686. *
  687. * Where the second one fully references the 100K extent
  688. * that starts at disk byte 12845056, and the log tree
  689. * has a single csum item that covers the entire range
  690. * of the extent:
  691. *
  692. * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
  693. *
  694. * After the first file extent item is replayed, the
  695. * csum tree gets the following csum item:
  696. *
  697. * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
  698. *
  699. * Which covers the 20K sub-range starting at offset 20K
  700. * of our extent. Now when we replay the second file
  701. * extent item, if we do not delete existing csum items
  702. * that cover any of its blocks, we end up getting two
  703. * csum items in our csum tree that overlap each other:
  704. *
  705. * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
  706. * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
  707. *
  708. * Which is a problem, because after this anyone trying
  709. * to lookup up for the checksum of any block of our
  710. * extent starting at an offset of 40K or higher, will
  711. * end up looking at the second csum item only, which
  712. * does not contain the checksum for any block starting
  713. * at offset 40K or higher of our extent.
  714. */
  715. while (!list_empty(&ordered_sums)) {
  716. struct btrfs_ordered_sum *sums;
  717. sums = list_entry(ordered_sums.next,
  718. struct btrfs_ordered_sum,
  719. list);
  720. if (!ret)
  721. ret = btrfs_del_csums(trans, fs_info,
  722. sums->bytenr,
  723. sums->len);
  724. if (!ret)
  725. ret = btrfs_csum_file_blocks(trans,
  726. fs_info->csum_root, sums);
  727. list_del(&sums->list);
  728. kfree(sums);
  729. }
  730. if (ret)
  731. goto out;
  732. } else {
  733. btrfs_release_path(path);
  734. }
  735. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  736. /* inline extents are easy, we just overwrite them */
  737. ret = overwrite_item(trans, root, path, eb, slot, key);
  738. if (ret)
  739. goto out;
  740. }
  741. inode_add_bytes(inode, nbytes);
  742. update_inode:
  743. ret = btrfs_update_inode(trans, root, inode);
  744. out:
  745. if (inode)
  746. iput(inode);
  747. return ret;
  748. }
  749. /*
  750. * when cleaning up conflicts between the directory names in the
  751. * subvolume, directory names in the log and directory names in the
  752. * inode back references, we may have to unlink inodes from directories.
  753. *
  754. * This is a helper function to do the unlink of a specific directory
  755. * item
  756. */
  757. static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
  758. struct btrfs_root *root,
  759. struct btrfs_path *path,
  760. struct btrfs_inode *dir,
  761. struct btrfs_dir_item *di)
  762. {
  763. struct inode *inode;
  764. char *name;
  765. int name_len;
  766. struct extent_buffer *leaf;
  767. struct btrfs_key location;
  768. int ret;
  769. leaf = path->nodes[0];
  770. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  771. name_len = btrfs_dir_name_len(leaf, di);
  772. name = kmalloc(name_len, GFP_NOFS);
  773. if (!name)
  774. return -ENOMEM;
  775. read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
  776. btrfs_release_path(path);
  777. inode = read_one_inode(root, location.objectid);
  778. if (!inode) {
  779. ret = -EIO;
  780. goto out;
  781. }
  782. ret = link_to_fixup_dir(trans, root, path, location.objectid);
  783. if (ret)
  784. goto out;
  785. ret = btrfs_unlink_inode(trans, root, dir, BTRFS_I(inode), name,
  786. name_len);
  787. if (ret)
  788. goto out;
  789. else
  790. ret = btrfs_run_delayed_items(trans);
  791. out:
  792. kfree(name);
  793. iput(inode);
  794. return ret;
  795. }
  796. /*
  797. * helper function to see if a given name and sequence number found
  798. * in an inode back reference are already in a directory and correctly
  799. * point to this inode
  800. */
  801. static noinline int inode_in_dir(struct btrfs_root *root,
  802. struct btrfs_path *path,
  803. u64 dirid, u64 objectid, u64 index,
  804. const char *name, int name_len)
  805. {
  806. struct btrfs_dir_item *di;
  807. struct btrfs_key location;
  808. int match = 0;
  809. di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
  810. index, name, name_len, 0);
  811. if (di && !IS_ERR(di)) {
  812. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
  813. if (location.objectid != objectid)
  814. goto out;
  815. } else
  816. goto out;
  817. btrfs_release_path(path);
  818. di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
  819. if (di && !IS_ERR(di)) {
  820. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
  821. if (location.objectid != objectid)
  822. goto out;
  823. } else
  824. goto out;
  825. match = 1;
  826. out:
  827. btrfs_release_path(path);
  828. return match;
  829. }
  830. /*
  831. * helper function to check a log tree for a named back reference in
  832. * an inode. This is used to decide if a back reference that is
  833. * found in the subvolume conflicts with what we find in the log.
  834. *
  835. * inode backreferences may have multiple refs in a single item,
  836. * during replay we process one reference at a time, and we don't
  837. * want to delete valid links to a file from the subvolume if that
  838. * link is also in the log.
  839. */
  840. static noinline int backref_in_log(struct btrfs_root *log,
  841. struct btrfs_key *key,
  842. u64 ref_objectid,
  843. const char *name, int namelen)
  844. {
  845. struct btrfs_path *path;
  846. struct btrfs_inode_ref *ref;
  847. unsigned long ptr;
  848. unsigned long ptr_end;
  849. unsigned long name_ptr;
  850. int found_name_len;
  851. int item_size;
  852. int ret;
  853. int match = 0;
  854. path = btrfs_alloc_path();
  855. if (!path)
  856. return -ENOMEM;
  857. ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
  858. if (ret != 0)
  859. goto out;
  860. ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
  861. if (key->type == BTRFS_INODE_EXTREF_KEY) {
  862. if (btrfs_find_name_in_ext_backref(path->nodes[0],
  863. path->slots[0],
  864. ref_objectid,
  865. name, namelen, NULL))
  866. match = 1;
  867. goto out;
  868. }
  869. item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
  870. ptr_end = ptr + item_size;
  871. while (ptr < ptr_end) {
  872. ref = (struct btrfs_inode_ref *)ptr;
  873. found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
  874. if (found_name_len == namelen) {
  875. name_ptr = (unsigned long)(ref + 1);
  876. ret = memcmp_extent_buffer(path->nodes[0], name,
  877. name_ptr, namelen);
  878. if (ret == 0) {
  879. match = 1;
  880. goto out;
  881. }
  882. }
  883. ptr = (unsigned long)(ref + 1) + found_name_len;
  884. }
  885. out:
  886. btrfs_free_path(path);
  887. return match;
  888. }
  889. static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
  890. struct btrfs_root *root,
  891. struct btrfs_path *path,
  892. struct btrfs_root *log_root,
  893. struct btrfs_inode *dir,
  894. struct btrfs_inode *inode,
  895. u64 inode_objectid, u64 parent_objectid,
  896. u64 ref_index, char *name, int namelen,
  897. int *search_done)
  898. {
  899. int ret;
  900. char *victim_name;
  901. int victim_name_len;
  902. struct extent_buffer *leaf;
  903. struct btrfs_dir_item *di;
  904. struct btrfs_key search_key;
  905. struct btrfs_inode_extref *extref;
  906. again:
  907. /* Search old style refs */
  908. search_key.objectid = inode_objectid;
  909. search_key.type = BTRFS_INODE_REF_KEY;
  910. search_key.offset = parent_objectid;
  911. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  912. if (ret == 0) {
  913. struct btrfs_inode_ref *victim_ref;
  914. unsigned long ptr;
  915. unsigned long ptr_end;
  916. leaf = path->nodes[0];
  917. /* are we trying to overwrite a back ref for the root directory
  918. * if so, just jump out, we're done
  919. */
  920. if (search_key.objectid == search_key.offset)
  921. return 1;
  922. /* check all the names in this back reference to see
  923. * if they are in the log. if so, we allow them to stay
  924. * otherwise they must be unlinked as a conflict
  925. */
  926. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  927. ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
  928. while (ptr < ptr_end) {
  929. victim_ref = (struct btrfs_inode_ref *)ptr;
  930. victim_name_len = btrfs_inode_ref_name_len(leaf,
  931. victim_ref);
  932. victim_name = kmalloc(victim_name_len, GFP_NOFS);
  933. if (!victim_name)
  934. return -ENOMEM;
  935. read_extent_buffer(leaf, victim_name,
  936. (unsigned long)(victim_ref + 1),
  937. victim_name_len);
  938. if (!backref_in_log(log_root, &search_key,
  939. parent_objectid,
  940. victim_name,
  941. victim_name_len)) {
  942. inc_nlink(&inode->vfs_inode);
  943. btrfs_release_path(path);
  944. ret = btrfs_unlink_inode(trans, root, dir, inode,
  945. victim_name, victim_name_len);
  946. kfree(victim_name);
  947. if (ret)
  948. return ret;
  949. ret = btrfs_run_delayed_items(trans);
  950. if (ret)
  951. return ret;
  952. *search_done = 1;
  953. goto again;
  954. }
  955. kfree(victim_name);
  956. ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
  957. }
  958. /*
  959. * NOTE: we have searched root tree and checked the
  960. * corresponding ref, it does not need to check again.
  961. */
  962. *search_done = 1;
  963. }
  964. btrfs_release_path(path);
  965. /* Same search but for extended refs */
  966. extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
  967. inode_objectid, parent_objectid, 0,
  968. 0);
  969. if (!IS_ERR_OR_NULL(extref)) {
  970. u32 item_size;
  971. u32 cur_offset = 0;
  972. unsigned long base;
  973. struct inode *victim_parent;
  974. leaf = path->nodes[0];
  975. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  976. base = btrfs_item_ptr_offset(leaf, path->slots[0]);
  977. while (cur_offset < item_size) {
  978. extref = (struct btrfs_inode_extref *)(base + cur_offset);
  979. victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
  980. if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
  981. goto next;
  982. victim_name = kmalloc(victim_name_len, GFP_NOFS);
  983. if (!victim_name)
  984. return -ENOMEM;
  985. read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
  986. victim_name_len);
  987. search_key.objectid = inode_objectid;
  988. search_key.type = BTRFS_INODE_EXTREF_KEY;
  989. search_key.offset = btrfs_extref_hash(parent_objectid,
  990. victim_name,
  991. victim_name_len);
  992. ret = 0;
  993. if (!backref_in_log(log_root, &search_key,
  994. parent_objectid, victim_name,
  995. victim_name_len)) {
  996. ret = -ENOENT;
  997. victim_parent = read_one_inode(root,
  998. parent_objectid);
  999. if (victim_parent) {
  1000. inc_nlink(&inode->vfs_inode);
  1001. btrfs_release_path(path);
  1002. ret = btrfs_unlink_inode(trans, root,
  1003. BTRFS_I(victim_parent),
  1004. inode,
  1005. victim_name,
  1006. victim_name_len);
  1007. if (!ret)
  1008. ret = btrfs_run_delayed_items(
  1009. trans);
  1010. }
  1011. iput(victim_parent);
  1012. kfree(victim_name);
  1013. if (ret)
  1014. return ret;
  1015. *search_done = 1;
  1016. goto again;
  1017. }
  1018. kfree(victim_name);
  1019. next:
  1020. cur_offset += victim_name_len + sizeof(*extref);
  1021. }
  1022. *search_done = 1;
  1023. }
  1024. btrfs_release_path(path);
  1025. /* look for a conflicting sequence number */
  1026. di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
  1027. ref_index, name, namelen, 0);
  1028. if (di && !IS_ERR(di)) {
  1029. ret = drop_one_dir_item(trans, root, path, dir, di);
  1030. if (ret)
  1031. return ret;
  1032. }
  1033. btrfs_release_path(path);
  1034. /* look for a conflicing name */
  1035. di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
  1036. name, namelen, 0);
  1037. if (di && !IS_ERR(di)) {
  1038. ret = drop_one_dir_item(trans, root, path, dir, di);
  1039. if (ret)
  1040. return ret;
  1041. }
  1042. btrfs_release_path(path);
  1043. return 0;
  1044. }
  1045. static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
  1046. u32 *namelen, char **name, u64 *index,
  1047. u64 *parent_objectid)
  1048. {
  1049. struct btrfs_inode_extref *extref;
  1050. extref = (struct btrfs_inode_extref *)ref_ptr;
  1051. *namelen = btrfs_inode_extref_name_len(eb, extref);
  1052. *name = kmalloc(*namelen, GFP_NOFS);
  1053. if (*name == NULL)
  1054. return -ENOMEM;
  1055. read_extent_buffer(eb, *name, (unsigned long)&extref->name,
  1056. *namelen);
  1057. if (index)
  1058. *index = btrfs_inode_extref_index(eb, extref);
  1059. if (parent_objectid)
  1060. *parent_objectid = btrfs_inode_extref_parent(eb, extref);
  1061. return 0;
  1062. }
  1063. static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
  1064. u32 *namelen, char **name, u64 *index)
  1065. {
  1066. struct btrfs_inode_ref *ref;
  1067. ref = (struct btrfs_inode_ref *)ref_ptr;
  1068. *namelen = btrfs_inode_ref_name_len(eb, ref);
  1069. *name = kmalloc(*namelen, GFP_NOFS);
  1070. if (*name == NULL)
  1071. return -ENOMEM;
  1072. read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
  1073. if (index)
  1074. *index = btrfs_inode_ref_index(eb, ref);
  1075. return 0;
  1076. }
  1077. /*
  1078. * Take an inode reference item from the log tree and iterate all names from the
  1079. * inode reference item in the subvolume tree with the same key (if it exists).
  1080. * For any name that is not in the inode reference item from the log tree, do a
  1081. * proper unlink of that name (that is, remove its entry from the inode
  1082. * reference item and both dir index keys).
  1083. */
  1084. static int unlink_old_inode_refs(struct btrfs_trans_handle *trans,
  1085. struct btrfs_root *root,
  1086. struct btrfs_path *path,
  1087. struct btrfs_inode *inode,
  1088. struct extent_buffer *log_eb,
  1089. int log_slot,
  1090. struct btrfs_key *key)
  1091. {
  1092. int ret;
  1093. unsigned long ref_ptr;
  1094. unsigned long ref_end;
  1095. struct extent_buffer *eb;
  1096. again:
  1097. btrfs_release_path(path);
  1098. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  1099. if (ret > 0) {
  1100. ret = 0;
  1101. goto out;
  1102. }
  1103. if (ret < 0)
  1104. goto out;
  1105. eb = path->nodes[0];
  1106. ref_ptr = btrfs_item_ptr_offset(eb, path->slots[0]);
  1107. ref_end = ref_ptr + btrfs_item_size_nr(eb, path->slots[0]);
  1108. while (ref_ptr < ref_end) {
  1109. char *name = NULL;
  1110. int namelen;
  1111. u64 parent_id;
  1112. if (key->type == BTRFS_INODE_EXTREF_KEY) {
  1113. ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
  1114. NULL, &parent_id);
  1115. } else {
  1116. parent_id = key->offset;
  1117. ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
  1118. NULL);
  1119. }
  1120. if (ret)
  1121. goto out;
  1122. if (key->type == BTRFS_INODE_EXTREF_KEY)
  1123. ret = btrfs_find_name_in_ext_backref(log_eb, log_slot,
  1124. parent_id, name,
  1125. namelen, NULL);
  1126. else
  1127. ret = btrfs_find_name_in_backref(log_eb, log_slot, name,
  1128. namelen, NULL);
  1129. if (!ret) {
  1130. struct inode *dir;
  1131. btrfs_release_path(path);
  1132. dir = read_one_inode(root, parent_id);
  1133. if (!dir) {
  1134. ret = -ENOENT;
  1135. kfree(name);
  1136. goto out;
  1137. }
  1138. ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
  1139. inode, name, namelen);
  1140. kfree(name);
  1141. iput(dir);
  1142. if (ret)
  1143. goto out;
  1144. goto again;
  1145. }
  1146. kfree(name);
  1147. ref_ptr += namelen;
  1148. if (key->type == BTRFS_INODE_EXTREF_KEY)
  1149. ref_ptr += sizeof(struct btrfs_inode_extref);
  1150. else
  1151. ref_ptr += sizeof(struct btrfs_inode_ref);
  1152. }
  1153. ret = 0;
  1154. out:
  1155. btrfs_release_path(path);
  1156. return ret;
  1157. }
  1158. /*
  1159. * replay one inode back reference item found in the log tree.
  1160. * eb, slot and key refer to the buffer and key found in the log tree.
  1161. * root is the destination we are replaying into, and path is for temp
  1162. * use by this function. (it should be released on return).
  1163. */
  1164. static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
  1165. struct btrfs_root *root,
  1166. struct btrfs_root *log,
  1167. struct btrfs_path *path,
  1168. struct extent_buffer *eb, int slot,
  1169. struct btrfs_key *key)
  1170. {
  1171. struct inode *dir = NULL;
  1172. struct inode *inode = NULL;
  1173. unsigned long ref_ptr;
  1174. unsigned long ref_end;
  1175. char *name = NULL;
  1176. int namelen;
  1177. int ret;
  1178. int search_done = 0;
  1179. int log_ref_ver = 0;
  1180. u64 parent_objectid;
  1181. u64 inode_objectid;
  1182. u64 ref_index = 0;
  1183. int ref_struct_size;
  1184. ref_ptr = btrfs_item_ptr_offset(eb, slot);
  1185. ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
  1186. if (key->type == BTRFS_INODE_EXTREF_KEY) {
  1187. struct btrfs_inode_extref *r;
  1188. ref_struct_size = sizeof(struct btrfs_inode_extref);
  1189. log_ref_ver = 1;
  1190. r = (struct btrfs_inode_extref *)ref_ptr;
  1191. parent_objectid = btrfs_inode_extref_parent(eb, r);
  1192. } else {
  1193. ref_struct_size = sizeof(struct btrfs_inode_ref);
  1194. parent_objectid = key->offset;
  1195. }
  1196. inode_objectid = key->objectid;
  1197. /*
  1198. * it is possible that we didn't log all the parent directories
  1199. * for a given inode. If we don't find the dir, just don't
  1200. * copy the back ref in. The link count fixup code will take
  1201. * care of the rest
  1202. */
  1203. dir = read_one_inode(root, parent_objectid);
  1204. if (!dir) {
  1205. ret = -ENOENT;
  1206. goto out;
  1207. }
  1208. inode = read_one_inode(root, inode_objectid);
  1209. if (!inode) {
  1210. ret = -EIO;
  1211. goto out;
  1212. }
  1213. while (ref_ptr < ref_end) {
  1214. if (log_ref_ver) {
  1215. ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
  1216. &ref_index, &parent_objectid);
  1217. /*
  1218. * parent object can change from one array
  1219. * item to another.
  1220. */
  1221. if (!dir)
  1222. dir = read_one_inode(root, parent_objectid);
  1223. if (!dir) {
  1224. ret = -ENOENT;
  1225. goto out;
  1226. }
  1227. } else {
  1228. ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
  1229. &ref_index);
  1230. }
  1231. if (ret)
  1232. goto out;
  1233. /* if we already have a perfect match, we're done */
  1234. if (!inode_in_dir(root, path, btrfs_ino(BTRFS_I(dir)),
  1235. btrfs_ino(BTRFS_I(inode)), ref_index,
  1236. name, namelen)) {
  1237. /*
  1238. * look for a conflicting back reference in the
  1239. * metadata. if we find one we have to unlink that name
  1240. * of the file before we add our new link. Later on, we
  1241. * overwrite any existing back reference, and we don't
  1242. * want to create dangling pointers in the directory.
  1243. */
  1244. if (!search_done) {
  1245. ret = __add_inode_ref(trans, root, path, log,
  1246. BTRFS_I(dir),
  1247. BTRFS_I(inode),
  1248. inode_objectid,
  1249. parent_objectid,
  1250. ref_index, name, namelen,
  1251. &search_done);
  1252. if (ret) {
  1253. if (ret == 1)
  1254. ret = 0;
  1255. goto out;
  1256. }
  1257. }
  1258. /* insert our name */
  1259. ret = btrfs_add_link(trans, BTRFS_I(dir),
  1260. BTRFS_I(inode),
  1261. name, namelen, 0, ref_index);
  1262. if (ret)
  1263. goto out;
  1264. btrfs_update_inode(trans, root, inode);
  1265. }
  1266. ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
  1267. kfree(name);
  1268. name = NULL;
  1269. if (log_ref_ver) {
  1270. iput(dir);
  1271. dir = NULL;
  1272. }
  1273. }
  1274. /*
  1275. * Before we overwrite the inode reference item in the subvolume tree
  1276. * with the item from the log tree, we must unlink all names from the
  1277. * parent directory that are in the subvolume's tree inode reference
  1278. * item, otherwise we end up with an inconsistent subvolume tree where
  1279. * dir index entries exist for a name but there is no inode reference
  1280. * item with the same name.
  1281. */
  1282. ret = unlink_old_inode_refs(trans, root, path, BTRFS_I(inode), eb, slot,
  1283. key);
  1284. if (ret)
  1285. goto out;
  1286. /* finally write the back reference in the inode */
  1287. ret = overwrite_item(trans, root, path, eb, slot, key);
  1288. out:
  1289. btrfs_release_path(path);
  1290. kfree(name);
  1291. iput(dir);
  1292. iput(inode);
  1293. return ret;
  1294. }
  1295. static int insert_orphan_item(struct btrfs_trans_handle *trans,
  1296. struct btrfs_root *root, u64 ino)
  1297. {
  1298. int ret;
  1299. ret = btrfs_insert_orphan_item(trans, root, ino);
  1300. if (ret == -EEXIST)
  1301. ret = 0;
  1302. return ret;
  1303. }
  1304. static int count_inode_extrefs(struct btrfs_root *root,
  1305. struct btrfs_inode *inode, struct btrfs_path *path)
  1306. {
  1307. int ret = 0;
  1308. int name_len;
  1309. unsigned int nlink = 0;
  1310. u32 item_size;
  1311. u32 cur_offset = 0;
  1312. u64 inode_objectid = btrfs_ino(inode);
  1313. u64 offset = 0;
  1314. unsigned long ptr;
  1315. struct btrfs_inode_extref *extref;
  1316. struct extent_buffer *leaf;
  1317. while (1) {
  1318. ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
  1319. &extref, &offset);
  1320. if (ret)
  1321. break;
  1322. leaf = path->nodes[0];
  1323. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  1324. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  1325. cur_offset = 0;
  1326. while (cur_offset < item_size) {
  1327. extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
  1328. name_len = btrfs_inode_extref_name_len(leaf, extref);
  1329. nlink++;
  1330. cur_offset += name_len + sizeof(*extref);
  1331. }
  1332. offset++;
  1333. btrfs_release_path(path);
  1334. }
  1335. btrfs_release_path(path);
  1336. if (ret < 0 && ret != -ENOENT)
  1337. return ret;
  1338. return nlink;
  1339. }
  1340. static int count_inode_refs(struct btrfs_root *root,
  1341. struct btrfs_inode *inode, struct btrfs_path *path)
  1342. {
  1343. int ret;
  1344. struct btrfs_key key;
  1345. unsigned int nlink = 0;
  1346. unsigned long ptr;
  1347. unsigned long ptr_end;
  1348. int name_len;
  1349. u64 ino = btrfs_ino(inode);
  1350. key.objectid = ino;
  1351. key.type = BTRFS_INODE_REF_KEY;
  1352. key.offset = (u64)-1;
  1353. while (1) {
  1354. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1355. if (ret < 0)
  1356. break;
  1357. if (ret > 0) {
  1358. if (path->slots[0] == 0)
  1359. break;
  1360. path->slots[0]--;
  1361. }
  1362. process_slot:
  1363. btrfs_item_key_to_cpu(path->nodes[0], &key,
  1364. path->slots[0]);
  1365. if (key.objectid != ino ||
  1366. key.type != BTRFS_INODE_REF_KEY)
  1367. break;
  1368. ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
  1369. ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
  1370. path->slots[0]);
  1371. while (ptr < ptr_end) {
  1372. struct btrfs_inode_ref *ref;
  1373. ref = (struct btrfs_inode_ref *)ptr;
  1374. name_len = btrfs_inode_ref_name_len(path->nodes[0],
  1375. ref);
  1376. ptr = (unsigned long)(ref + 1) + name_len;
  1377. nlink++;
  1378. }
  1379. if (key.offset == 0)
  1380. break;
  1381. if (path->slots[0] > 0) {
  1382. path->slots[0]--;
  1383. goto process_slot;
  1384. }
  1385. key.offset--;
  1386. btrfs_release_path(path);
  1387. }
  1388. btrfs_release_path(path);
  1389. return nlink;
  1390. }
  1391. /*
  1392. * There are a few corners where the link count of the file can't
  1393. * be properly maintained during replay. So, instead of adding
  1394. * lots of complexity to the log code, we just scan the backrefs
  1395. * for any file that has been through replay.
  1396. *
  1397. * The scan will update the link count on the inode to reflect the
  1398. * number of back refs found. If it goes down to zero, the iput
  1399. * will free the inode.
  1400. */
  1401. static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
  1402. struct btrfs_root *root,
  1403. struct inode *inode)
  1404. {
  1405. struct btrfs_path *path;
  1406. int ret;
  1407. u64 nlink = 0;
  1408. u64 ino = btrfs_ino(BTRFS_I(inode));
  1409. path = btrfs_alloc_path();
  1410. if (!path)
  1411. return -ENOMEM;
  1412. ret = count_inode_refs(root, BTRFS_I(inode), path);
  1413. if (ret < 0)
  1414. goto out;
  1415. nlink = ret;
  1416. ret = count_inode_extrefs(root, BTRFS_I(inode), path);
  1417. if (ret < 0)
  1418. goto out;
  1419. nlink += ret;
  1420. ret = 0;
  1421. if (nlink != inode->i_nlink) {
  1422. set_nlink(inode, nlink);
  1423. btrfs_update_inode(trans, root, inode);
  1424. }
  1425. BTRFS_I(inode)->index_cnt = (u64)-1;
  1426. if (inode->i_nlink == 0) {
  1427. if (S_ISDIR(inode->i_mode)) {
  1428. ret = replay_dir_deletes(trans, root, NULL, path,
  1429. ino, 1);
  1430. if (ret)
  1431. goto out;
  1432. }
  1433. ret = insert_orphan_item(trans, root, ino);
  1434. }
  1435. out:
  1436. btrfs_free_path(path);
  1437. return ret;
  1438. }
  1439. static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
  1440. struct btrfs_root *root,
  1441. struct btrfs_path *path)
  1442. {
  1443. int ret;
  1444. struct btrfs_key key;
  1445. struct inode *inode;
  1446. key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
  1447. key.type = BTRFS_ORPHAN_ITEM_KEY;
  1448. key.offset = (u64)-1;
  1449. while (1) {
  1450. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1451. if (ret < 0)
  1452. break;
  1453. if (ret == 1) {
  1454. if (path->slots[0] == 0)
  1455. break;
  1456. path->slots[0]--;
  1457. }
  1458. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1459. if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
  1460. key.type != BTRFS_ORPHAN_ITEM_KEY)
  1461. break;
  1462. ret = btrfs_del_item(trans, root, path);
  1463. if (ret)
  1464. goto out;
  1465. btrfs_release_path(path);
  1466. inode = read_one_inode(root, key.offset);
  1467. if (!inode)
  1468. return -EIO;
  1469. ret = fixup_inode_link_count(trans, root, inode);
  1470. iput(inode);
  1471. if (ret)
  1472. goto out;
  1473. /*
  1474. * fixup on a directory may create new entries,
  1475. * make sure we always look for the highset possible
  1476. * offset
  1477. */
  1478. key.offset = (u64)-1;
  1479. }
  1480. ret = 0;
  1481. out:
  1482. btrfs_release_path(path);
  1483. return ret;
  1484. }
  1485. /*
  1486. * record a given inode in the fixup dir so we can check its link
  1487. * count when replay is done. The link count is incremented here
  1488. * so the inode won't go away until we check it
  1489. */
  1490. static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
  1491. struct btrfs_root *root,
  1492. struct btrfs_path *path,
  1493. u64 objectid)
  1494. {
  1495. struct btrfs_key key;
  1496. int ret = 0;
  1497. struct inode *inode;
  1498. inode = read_one_inode(root, objectid);
  1499. if (!inode)
  1500. return -EIO;
  1501. key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
  1502. key.type = BTRFS_ORPHAN_ITEM_KEY;
  1503. key.offset = objectid;
  1504. ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
  1505. btrfs_release_path(path);
  1506. if (ret == 0) {
  1507. if (!inode->i_nlink)
  1508. set_nlink(inode, 1);
  1509. else
  1510. inc_nlink(inode);
  1511. ret = btrfs_update_inode(trans, root, inode);
  1512. } else if (ret == -EEXIST) {
  1513. ret = 0;
  1514. } else {
  1515. BUG(); /* Logic Error */
  1516. }
  1517. iput(inode);
  1518. return ret;
  1519. }
  1520. /*
  1521. * when replaying the log for a directory, we only insert names
  1522. * for inodes that actually exist. This means an fsync on a directory
  1523. * does not implicitly fsync all the new files in it
  1524. */
  1525. static noinline int insert_one_name(struct btrfs_trans_handle *trans,
  1526. struct btrfs_root *root,
  1527. u64 dirid, u64 index,
  1528. char *name, int name_len,
  1529. struct btrfs_key *location)
  1530. {
  1531. struct inode *inode;
  1532. struct inode *dir;
  1533. int ret;
  1534. inode = read_one_inode(root, location->objectid);
  1535. if (!inode)
  1536. return -ENOENT;
  1537. dir = read_one_inode(root, dirid);
  1538. if (!dir) {
  1539. iput(inode);
  1540. return -EIO;
  1541. }
  1542. ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name,
  1543. name_len, 1, index);
  1544. /* FIXME, put inode into FIXUP list */
  1545. iput(inode);
  1546. iput(dir);
  1547. return ret;
  1548. }
  1549. /*
  1550. * Return true if an inode reference exists in the log for the given name,
  1551. * inode and parent inode.
  1552. */
  1553. static bool name_in_log_ref(struct btrfs_root *log_root,
  1554. const char *name, const int name_len,
  1555. const u64 dirid, const u64 ino)
  1556. {
  1557. struct btrfs_key search_key;
  1558. search_key.objectid = ino;
  1559. search_key.type = BTRFS_INODE_REF_KEY;
  1560. search_key.offset = dirid;
  1561. if (backref_in_log(log_root, &search_key, dirid, name, name_len))
  1562. return true;
  1563. search_key.type = BTRFS_INODE_EXTREF_KEY;
  1564. search_key.offset = btrfs_extref_hash(dirid, name, name_len);
  1565. if (backref_in_log(log_root, &search_key, dirid, name, name_len))
  1566. return true;
  1567. return false;
  1568. }
  1569. /*
  1570. * take a single entry in a log directory item and replay it into
  1571. * the subvolume.
  1572. *
  1573. * if a conflicting item exists in the subdirectory already,
  1574. * the inode it points to is unlinked and put into the link count
  1575. * fix up tree.
  1576. *
  1577. * If a name from the log points to a file or directory that does
  1578. * not exist in the FS, it is skipped. fsyncs on directories
  1579. * do not force down inodes inside that directory, just changes to the
  1580. * names or unlinks in a directory.
  1581. *
  1582. * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
  1583. * non-existing inode) and 1 if the name was replayed.
  1584. */
  1585. static noinline int replay_one_name(struct btrfs_trans_handle *trans,
  1586. struct btrfs_root *root,
  1587. struct btrfs_path *path,
  1588. struct extent_buffer *eb,
  1589. struct btrfs_dir_item *di,
  1590. struct btrfs_key *key)
  1591. {
  1592. char *name;
  1593. int name_len;
  1594. struct btrfs_dir_item *dst_di;
  1595. struct btrfs_key found_key;
  1596. struct btrfs_key log_key;
  1597. struct inode *dir;
  1598. u8 log_type;
  1599. int exists;
  1600. int ret = 0;
  1601. bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
  1602. bool name_added = false;
  1603. dir = read_one_inode(root, key->objectid);
  1604. if (!dir)
  1605. return -EIO;
  1606. name_len = btrfs_dir_name_len(eb, di);
  1607. name = kmalloc(name_len, GFP_NOFS);
  1608. if (!name) {
  1609. ret = -ENOMEM;
  1610. goto out;
  1611. }
  1612. log_type = btrfs_dir_type(eb, di);
  1613. read_extent_buffer(eb, name, (unsigned long)(di + 1),
  1614. name_len);
  1615. btrfs_dir_item_key_to_cpu(eb, di, &log_key);
  1616. exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
  1617. if (exists == 0)
  1618. exists = 1;
  1619. else
  1620. exists = 0;
  1621. btrfs_release_path(path);
  1622. if (key->type == BTRFS_DIR_ITEM_KEY) {
  1623. dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
  1624. name, name_len, 1);
  1625. } else if (key->type == BTRFS_DIR_INDEX_KEY) {
  1626. dst_di = btrfs_lookup_dir_index_item(trans, root, path,
  1627. key->objectid,
  1628. key->offset, name,
  1629. name_len, 1);
  1630. } else {
  1631. /* Corruption */
  1632. ret = -EINVAL;
  1633. goto out;
  1634. }
  1635. if (IS_ERR_OR_NULL(dst_di)) {
  1636. /* we need a sequence number to insert, so we only
  1637. * do inserts for the BTRFS_DIR_INDEX_KEY types
  1638. */
  1639. if (key->type != BTRFS_DIR_INDEX_KEY)
  1640. goto out;
  1641. goto insert;
  1642. }
  1643. btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
  1644. /* the existing item matches the logged item */
  1645. if (found_key.objectid == log_key.objectid &&
  1646. found_key.type == log_key.type &&
  1647. found_key.offset == log_key.offset &&
  1648. btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
  1649. update_size = false;
  1650. goto out;
  1651. }
  1652. /*
  1653. * don't drop the conflicting directory entry if the inode
  1654. * for the new entry doesn't exist
  1655. */
  1656. if (!exists)
  1657. goto out;
  1658. ret = drop_one_dir_item(trans, root, path, BTRFS_I(dir), dst_di);
  1659. if (ret)
  1660. goto out;
  1661. if (key->type == BTRFS_DIR_INDEX_KEY)
  1662. goto insert;
  1663. out:
  1664. btrfs_release_path(path);
  1665. if (!ret && update_size) {
  1666. btrfs_i_size_write(BTRFS_I(dir), dir->i_size + name_len * 2);
  1667. ret = btrfs_update_inode(trans, root, dir);
  1668. }
  1669. kfree(name);
  1670. iput(dir);
  1671. if (!ret && name_added)
  1672. ret = 1;
  1673. return ret;
  1674. insert:
  1675. if (name_in_log_ref(root->log_root, name, name_len,
  1676. key->objectid, log_key.objectid)) {
  1677. /* The dentry will be added later. */
  1678. ret = 0;
  1679. update_size = false;
  1680. goto out;
  1681. }
  1682. btrfs_release_path(path);
  1683. ret = insert_one_name(trans, root, key->objectid, key->offset,
  1684. name, name_len, &log_key);
  1685. if (ret && ret != -ENOENT && ret != -EEXIST)
  1686. goto out;
  1687. if (!ret)
  1688. name_added = true;
  1689. update_size = false;
  1690. ret = 0;
  1691. goto out;
  1692. }
  1693. /*
  1694. * find all the names in a directory item and reconcile them into
  1695. * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
  1696. * one name in a directory item, but the same code gets used for
  1697. * both directory index types
  1698. */
  1699. static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
  1700. struct btrfs_root *root,
  1701. struct btrfs_path *path,
  1702. struct extent_buffer *eb, int slot,
  1703. struct btrfs_key *key)
  1704. {
  1705. int ret = 0;
  1706. u32 item_size = btrfs_item_size_nr(eb, slot);
  1707. struct btrfs_dir_item *di;
  1708. int name_len;
  1709. unsigned long ptr;
  1710. unsigned long ptr_end;
  1711. struct btrfs_path *fixup_path = NULL;
  1712. ptr = btrfs_item_ptr_offset(eb, slot);
  1713. ptr_end = ptr + item_size;
  1714. while (ptr < ptr_end) {
  1715. di = (struct btrfs_dir_item *)ptr;
  1716. name_len = btrfs_dir_name_len(eb, di);
  1717. ret = replay_one_name(trans, root, path, eb, di, key);
  1718. if (ret < 0)
  1719. break;
  1720. ptr = (unsigned long)(di + 1);
  1721. ptr += name_len;
  1722. /*
  1723. * If this entry refers to a non-directory (directories can not
  1724. * have a link count > 1) and it was added in the transaction
  1725. * that was not committed, make sure we fixup the link count of
  1726. * the inode it the entry points to. Otherwise something like
  1727. * the following would result in a directory pointing to an
  1728. * inode with a wrong link that does not account for this dir
  1729. * entry:
  1730. *
  1731. * mkdir testdir
  1732. * touch testdir/foo
  1733. * touch testdir/bar
  1734. * sync
  1735. *
  1736. * ln testdir/bar testdir/bar_link
  1737. * ln testdir/foo testdir/foo_link
  1738. * xfs_io -c "fsync" testdir/bar
  1739. *
  1740. * <power failure>
  1741. *
  1742. * mount fs, log replay happens
  1743. *
  1744. * File foo would remain with a link count of 1 when it has two
  1745. * entries pointing to it in the directory testdir. This would
  1746. * make it impossible to ever delete the parent directory has
  1747. * it would result in stale dentries that can never be deleted.
  1748. */
  1749. if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) {
  1750. struct btrfs_key di_key;
  1751. if (!fixup_path) {
  1752. fixup_path = btrfs_alloc_path();
  1753. if (!fixup_path) {
  1754. ret = -ENOMEM;
  1755. break;
  1756. }
  1757. }
  1758. btrfs_dir_item_key_to_cpu(eb, di, &di_key);
  1759. ret = link_to_fixup_dir(trans, root, fixup_path,
  1760. di_key.objectid);
  1761. if (ret)
  1762. break;
  1763. }
  1764. ret = 0;
  1765. }
  1766. btrfs_free_path(fixup_path);
  1767. return ret;
  1768. }
  1769. /*
  1770. * directory replay has two parts. There are the standard directory
  1771. * items in the log copied from the subvolume, and range items
  1772. * created in the log while the subvolume was logged.
  1773. *
  1774. * The range items tell us which parts of the key space the log
  1775. * is authoritative for. During replay, if a key in the subvolume
  1776. * directory is in a logged range item, but not actually in the log
  1777. * that means it was deleted from the directory before the fsync
  1778. * and should be removed.
  1779. */
  1780. static noinline int find_dir_range(struct btrfs_root *root,
  1781. struct btrfs_path *path,
  1782. u64 dirid, int key_type,
  1783. u64 *start_ret, u64 *end_ret)
  1784. {
  1785. struct btrfs_key key;
  1786. u64 found_end;
  1787. struct btrfs_dir_log_item *item;
  1788. int ret;
  1789. int nritems;
  1790. if (*start_ret == (u64)-1)
  1791. return 1;
  1792. key.objectid = dirid;
  1793. key.type = key_type;
  1794. key.offset = *start_ret;
  1795. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1796. if (ret < 0)
  1797. goto out;
  1798. if (ret > 0) {
  1799. if (path->slots[0] == 0)
  1800. goto out;
  1801. path->slots[0]--;
  1802. }
  1803. if (ret != 0)
  1804. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1805. if (key.type != key_type || key.objectid != dirid) {
  1806. ret = 1;
  1807. goto next;
  1808. }
  1809. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1810. struct btrfs_dir_log_item);
  1811. found_end = btrfs_dir_log_end(path->nodes[0], item);
  1812. if (*start_ret >= key.offset && *start_ret <= found_end) {
  1813. ret = 0;
  1814. *start_ret = key.offset;
  1815. *end_ret = found_end;
  1816. goto out;
  1817. }
  1818. ret = 1;
  1819. next:
  1820. /* check the next slot in the tree to see if it is a valid item */
  1821. nritems = btrfs_header_nritems(path->nodes[0]);
  1822. path->slots[0]++;
  1823. if (path->slots[0] >= nritems) {
  1824. ret = btrfs_next_leaf(root, path);
  1825. if (ret)
  1826. goto out;
  1827. }
  1828. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1829. if (key.type != key_type || key.objectid != dirid) {
  1830. ret = 1;
  1831. goto out;
  1832. }
  1833. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1834. struct btrfs_dir_log_item);
  1835. found_end = btrfs_dir_log_end(path->nodes[0], item);
  1836. *start_ret = key.offset;
  1837. *end_ret = found_end;
  1838. ret = 0;
  1839. out:
  1840. btrfs_release_path(path);
  1841. return ret;
  1842. }
  1843. /*
  1844. * this looks for a given directory item in the log. If the directory
  1845. * item is not in the log, the item is removed and the inode it points
  1846. * to is unlinked
  1847. */
  1848. static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
  1849. struct btrfs_root *root,
  1850. struct btrfs_root *log,
  1851. struct btrfs_path *path,
  1852. struct btrfs_path *log_path,
  1853. struct inode *dir,
  1854. struct btrfs_key *dir_key)
  1855. {
  1856. int ret;
  1857. struct extent_buffer *eb;
  1858. int slot;
  1859. u32 item_size;
  1860. struct btrfs_dir_item *di;
  1861. struct btrfs_dir_item *log_di;
  1862. int name_len;
  1863. unsigned long ptr;
  1864. unsigned long ptr_end;
  1865. char *name;
  1866. struct inode *inode;
  1867. struct btrfs_key location;
  1868. again:
  1869. eb = path->nodes[0];
  1870. slot = path->slots[0];
  1871. item_size = btrfs_item_size_nr(eb, slot);
  1872. ptr = btrfs_item_ptr_offset(eb, slot);
  1873. ptr_end = ptr + item_size;
  1874. while (ptr < ptr_end) {
  1875. di = (struct btrfs_dir_item *)ptr;
  1876. name_len = btrfs_dir_name_len(eb, di);
  1877. name = kmalloc(name_len, GFP_NOFS);
  1878. if (!name) {
  1879. ret = -ENOMEM;
  1880. goto out;
  1881. }
  1882. read_extent_buffer(eb, name, (unsigned long)(di + 1),
  1883. name_len);
  1884. log_di = NULL;
  1885. if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
  1886. log_di = btrfs_lookup_dir_item(trans, log, log_path,
  1887. dir_key->objectid,
  1888. name, name_len, 0);
  1889. } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
  1890. log_di = btrfs_lookup_dir_index_item(trans, log,
  1891. log_path,
  1892. dir_key->objectid,
  1893. dir_key->offset,
  1894. name, name_len, 0);
  1895. }
  1896. if (!log_di || (IS_ERR(log_di) && PTR_ERR(log_di) == -ENOENT)) {
  1897. btrfs_dir_item_key_to_cpu(eb, di, &location);
  1898. btrfs_release_path(path);
  1899. btrfs_release_path(log_path);
  1900. inode = read_one_inode(root, location.objectid);
  1901. if (!inode) {
  1902. kfree(name);
  1903. return -EIO;
  1904. }
  1905. ret = link_to_fixup_dir(trans, root,
  1906. path, location.objectid);
  1907. if (ret) {
  1908. kfree(name);
  1909. iput(inode);
  1910. goto out;
  1911. }
  1912. inc_nlink(inode);
  1913. ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
  1914. BTRFS_I(inode), name, name_len);
  1915. if (!ret)
  1916. ret = btrfs_run_delayed_items(trans);
  1917. kfree(name);
  1918. iput(inode);
  1919. if (ret)
  1920. goto out;
  1921. /* there might still be more names under this key
  1922. * check and repeat if required
  1923. */
  1924. ret = btrfs_search_slot(NULL, root, dir_key, path,
  1925. 0, 0);
  1926. if (ret == 0)
  1927. goto again;
  1928. ret = 0;
  1929. goto out;
  1930. } else if (IS_ERR(log_di)) {
  1931. kfree(name);
  1932. return PTR_ERR(log_di);
  1933. }
  1934. btrfs_release_path(log_path);
  1935. kfree(name);
  1936. ptr = (unsigned long)(di + 1);
  1937. ptr += name_len;
  1938. }
  1939. ret = 0;
  1940. out:
  1941. btrfs_release_path(path);
  1942. btrfs_release_path(log_path);
  1943. return ret;
  1944. }
  1945. static int replay_xattr_deletes(struct btrfs_trans_handle *trans,
  1946. struct btrfs_root *root,
  1947. struct btrfs_root *log,
  1948. struct btrfs_path *path,
  1949. const u64 ino)
  1950. {
  1951. struct btrfs_key search_key;
  1952. struct btrfs_path *log_path;
  1953. int i;
  1954. int nritems;
  1955. int ret;
  1956. log_path = btrfs_alloc_path();
  1957. if (!log_path)
  1958. return -ENOMEM;
  1959. search_key.objectid = ino;
  1960. search_key.type = BTRFS_XATTR_ITEM_KEY;
  1961. search_key.offset = 0;
  1962. again:
  1963. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  1964. if (ret < 0)
  1965. goto out;
  1966. process_leaf:
  1967. nritems = btrfs_header_nritems(path->nodes[0]);
  1968. for (i = path->slots[0]; i < nritems; i++) {
  1969. struct btrfs_key key;
  1970. struct btrfs_dir_item *di;
  1971. struct btrfs_dir_item *log_di;
  1972. u32 total_size;
  1973. u32 cur;
  1974. btrfs_item_key_to_cpu(path->nodes[0], &key, i);
  1975. if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
  1976. ret = 0;
  1977. goto out;
  1978. }
  1979. di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item);
  1980. total_size = btrfs_item_size_nr(path->nodes[0], i);
  1981. cur = 0;
  1982. while (cur < total_size) {
  1983. u16 name_len = btrfs_dir_name_len(path->nodes[0], di);
  1984. u16 data_len = btrfs_dir_data_len(path->nodes[0], di);
  1985. u32 this_len = sizeof(*di) + name_len + data_len;
  1986. char *name;
  1987. name = kmalloc(name_len, GFP_NOFS);
  1988. if (!name) {
  1989. ret = -ENOMEM;
  1990. goto out;
  1991. }
  1992. read_extent_buffer(path->nodes[0], name,
  1993. (unsigned long)(di + 1), name_len);
  1994. log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
  1995. name, name_len, 0);
  1996. btrfs_release_path(log_path);
  1997. if (!log_di) {
  1998. /* Doesn't exist in log tree, so delete it. */
  1999. btrfs_release_path(path);
  2000. di = btrfs_lookup_xattr(trans, root, path, ino,
  2001. name, name_len, -1);
  2002. kfree(name);
  2003. if (IS_ERR(di)) {
  2004. ret = PTR_ERR(di);
  2005. goto out;
  2006. }
  2007. ASSERT(di);
  2008. ret = btrfs_delete_one_dir_name(trans, root,
  2009. path, di);
  2010. if (ret)
  2011. goto out;
  2012. btrfs_release_path(path);
  2013. search_key = key;
  2014. goto again;
  2015. }
  2016. kfree(name);
  2017. if (IS_ERR(log_di)) {
  2018. ret = PTR_ERR(log_di);
  2019. goto out;
  2020. }
  2021. cur += this_len;
  2022. di = (struct btrfs_dir_item *)((char *)di + this_len);
  2023. }
  2024. }
  2025. ret = btrfs_next_leaf(root, path);
  2026. if (ret > 0)
  2027. ret = 0;
  2028. else if (ret == 0)
  2029. goto process_leaf;
  2030. out:
  2031. btrfs_free_path(log_path);
  2032. btrfs_release_path(path);
  2033. return ret;
  2034. }
  2035. /*
  2036. * deletion replay happens before we copy any new directory items
  2037. * out of the log or out of backreferences from inodes. It
  2038. * scans the log to find ranges of keys that log is authoritative for,
  2039. * and then scans the directory to find items in those ranges that are
  2040. * not present in the log.
  2041. *
  2042. * Anything we don't find in the log is unlinked and removed from the
  2043. * directory.
  2044. */
  2045. static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
  2046. struct btrfs_root *root,
  2047. struct btrfs_root *log,
  2048. struct btrfs_path *path,
  2049. u64 dirid, int del_all)
  2050. {
  2051. u64 range_start;
  2052. u64 range_end;
  2053. int key_type = BTRFS_DIR_LOG_ITEM_KEY;
  2054. int ret = 0;
  2055. struct btrfs_key dir_key;
  2056. struct btrfs_key found_key;
  2057. struct btrfs_path *log_path;
  2058. struct inode *dir;
  2059. dir_key.objectid = dirid;
  2060. dir_key.type = BTRFS_DIR_ITEM_KEY;
  2061. log_path = btrfs_alloc_path();
  2062. if (!log_path)
  2063. return -ENOMEM;
  2064. dir = read_one_inode(root, dirid);
  2065. /* it isn't an error if the inode isn't there, that can happen
  2066. * because we replay the deletes before we copy in the inode item
  2067. * from the log
  2068. */
  2069. if (!dir) {
  2070. btrfs_free_path(log_path);
  2071. return 0;
  2072. }
  2073. again:
  2074. range_start = 0;
  2075. range_end = 0;
  2076. while (1) {
  2077. if (del_all)
  2078. range_end = (u64)-1;
  2079. else {
  2080. ret = find_dir_range(log, path, dirid, key_type,
  2081. &range_start, &range_end);
  2082. if (ret != 0)
  2083. break;
  2084. }
  2085. dir_key.offset = range_start;
  2086. while (1) {
  2087. int nritems;
  2088. ret = btrfs_search_slot(NULL, root, &dir_key, path,
  2089. 0, 0);
  2090. if (ret < 0)
  2091. goto out;
  2092. nritems = btrfs_header_nritems(path->nodes[0]);
  2093. if (path->slots[0] >= nritems) {
  2094. ret = btrfs_next_leaf(root, path);
  2095. if (ret == 1)
  2096. break;
  2097. else if (ret < 0)
  2098. goto out;
  2099. }
  2100. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  2101. path->slots[0]);
  2102. if (found_key.objectid != dirid ||
  2103. found_key.type != dir_key.type)
  2104. goto next_type;
  2105. if (found_key.offset > range_end)
  2106. break;
  2107. ret = check_item_in_log(trans, root, log, path,
  2108. log_path, dir,
  2109. &found_key);
  2110. if (ret)
  2111. goto out;
  2112. if (found_key.offset == (u64)-1)
  2113. break;
  2114. dir_key.offset = found_key.offset + 1;
  2115. }
  2116. btrfs_release_path(path);
  2117. if (range_end == (u64)-1)
  2118. break;
  2119. range_start = range_end + 1;
  2120. }
  2121. next_type:
  2122. ret = 0;
  2123. if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
  2124. key_type = BTRFS_DIR_LOG_INDEX_KEY;
  2125. dir_key.type = BTRFS_DIR_INDEX_KEY;
  2126. btrfs_release_path(path);
  2127. goto again;
  2128. }
  2129. out:
  2130. btrfs_release_path(path);
  2131. btrfs_free_path(log_path);
  2132. iput(dir);
  2133. return ret;
  2134. }
  2135. /*
  2136. * the process_func used to replay items from the log tree. This
  2137. * gets called in two different stages. The first stage just looks
  2138. * for inodes and makes sure they are all copied into the subvolume.
  2139. *
  2140. * The second stage copies all the other item types from the log into
  2141. * the subvolume. The two stage approach is slower, but gets rid of
  2142. * lots of complexity around inodes referencing other inodes that exist
  2143. * only in the log (references come from either directory items or inode
  2144. * back refs).
  2145. */
  2146. static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
  2147. struct walk_control *wc, u64 gen, int level)
  2148. {
  2149. int nritems;
  2150. struct btrfs_path *path;
  2151. struct btrfs_root *root = wc->replay_dest;
  2152. struct btrfs_key key;
  2153. int i;
  2154. int ret;
  2155. ret = btrfs_read_buffer(eb, gen, level, NULL);
  2156. if (ret)
  2157. return ret;
  2158. level = btrfs_header_level(eb);
  2159. if (level != 0)
  2160. return 0;
  2161. path = btrfs_alloc_path();
  2162. if (!path)
  2163. return -ENOMEM;
  2164. nritems = btrfs_header_nritems(eb);
  2165. for (i = 0; i < nritems; i++) {
  2166. btrfs_item_key_to_cpu(eb, &key, i);
  2167. /* inode keys are done during the first stage */
  2168. if (key.type == BTRFS_INODE_ITEM_KEY &&
  2169. wc->stage == LOG_WALK_REPLAY_INODES) {
  2170. struct btrfs_inode_item *inode_item;
  2171. u32 mode;
  2172. inode_item = btrfs_item_ptr(eb, i,
  2173. struct btrfs_inode_item);
  2174. ret = replay_xattr_deletes(wc->trans, root, log,
  2175. path, key.objectid);
  2176. if (ret)
  2177. break;
  2178. mode = btrfs_inode_mode(eb, inode_item);
  2179. if (S_ISDIR(mode)) {
  2180. ret = replay_dir_deletes(wc->trans,
  2181. root, log, path, key.objectid, 0);
  2182. if (ret)
  2183. break;
  2184. }
  2185. ret = overwrite_item(wc->trans, root, path,
  2186. eb, i, &key);
  2187. if (ret)
  2188. break;
  2189. /*
  2190. * Before replaying extents, truncate the inode to its
  2191. * size. We need to do it now and not after log replay
  2192. * because before an fsync we can have prealloc extents
  2193. * added beyond the inode's i_size. If we did it after,
  2194. * through orphan cleanup for example, we would drop
  2195. * those prealloc extents just after replaying them.
  2196. */
  2197. if (S_ISREG(mode)) {
  2198. struct inode *inode;
  2199. u64 from;
  2200. inode = read_one_inode(root, key.objectid);
  2201. if (!inode) {
  2202. ret = -EIO;
  2203. break;
  2204. }
  2205. from = ALIGN(i_size_read(inode),
  2206. root->fs_info->sectorsize);
  2207. ret = btrfs_drop_extents(wc->trans, root, inode,
  2208. from, (u64)-1, 1);
  2209. /*
  2210. * If the nlink count is zero here, the iput
  2211. * will free the inode. We bump it to make
  2212. * sure it doesn't get freed until the link
  2213. * count fixup is done.
  2214. */
  2215. if (!ret) {
  2216. if (inode->i_nlink == 0)
  2217. inc_nlink(inode);
  2218. /* Update link count and nbytes. */
  2219. ret = btrfs_update_inode(wc->trans,
  2220. root, inode);
  2221. }
  2222. iput(inode);
  2223. if (ret)
  2224. break;
  2225. }
  2226. ret = link_to_fixup_dir(wc->trans, root,
  2227. path, key.objectid);
  2228. if (ret)
  2229. break;
  2230. }
  2231. if (key.type == BTRFS_DIR_INDEX_KEY &&
  2232. wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
  2233. ret = replay_one_dir_item(wc->trans, root, path,
  2234. eb, i, &key);
  2235. if (ret)
  2236. break;
  2237. }
  2238. if (wc->stage < LOG_WALK_REPLAY_ALL)
  2239. continue;
  2240. /* these keys are simply copied */
  2241. if (key.type == BTRFS_XATTR_ITEM_KEY) {
  2242. ret = overwrite_item(wc->trans, root, path,
  2243. eb, i, &key);
  2244. if (ret)
  2245. break;
  2246. } else if (key.type == BTRFS_INODE_REF_KEY ||
  2247. key.type == BTRFS_INODE_EXTREF_KEY) {
  2248. ret = add_inode_ref(wc->trans, root, log, path,
  2249. eb, i, &key);
  2250. if (ret && ret != -ENOENT)
  2251. break;
  2252. ret = 0;
  2253. } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
  2254. ret = replay_one_extent(wc->trans, root, path,
  2255. eb, i, &key);
  2256. if (ret)
  2257. break;
  2258. } else if (key.type == BTRFS_DIR_ITEM_KEY) {
  2259. ret = replay_one_dir_item(wc->trans, root, path,
  2260. eb, i, &key);
  2261. if (ret)
  2262. break;
  2263. }
  2264. }
  2265. btrfs_free_path(path);
  2266. return ret;
  2267. }
  2268. static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
  2269. struct btrfs_root *root,
  2270. struct btrfs_path *path, int *level,
  2271. struct walk_control *wc)
  2272. {
  2273. struct btrfs_fs_info *fs_info = root->fs_info;
  2274. u64 root_owner;
  2275. u64 bytenr;
  2276. u64 ptr_gen;
  2277. struct extent_buffer *next;
  2278. struct extent_buffer *cur;
  2279. struct extent_buffer *parent;
  2280. u32 blocksize;
  2281. int ret = 0;
  2282. WARN_ON(*level < 0);
  2283. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  2284. while (*level > 0) {
  2285. struct btrfs_key first_key;
  2286. WARN_ON(*level < 0);
  2287. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  2288. cur = path->nodes[*level];
  2289. WARN_ON(btrfs_header_level(cur) != *level);
  2290. if (path->slots[*level] >=
  2291. btrfs_header_nritems(cur))
  2292. break;
  2293. bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
  2294. ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
  2295. btrfs_node_key_to_cpu(cur, &first_key, path->slots[*level]);
  2296. blocksize = fs_info->nodesize;
  2297. parent = path->nodes[*level];
  2298. root_owner = btrfs_header_owner(parent);
  2299. next = btrfs_find_create_tree_block(fs_info, bytenr);
  2300. if (IS_ERR(next))
  2301. return PTR_ERR(next);
  2302. if (*level == 1) {
  2303. ret = wc->process_func(root, next, wc, ptr_gen,
  2304. *level - 1);
  2305. if (ret) {
  2306. free_extent_buffer(next);
  2307. return ret;
  2308. }
  2309. path->slots[*level]++;
  2310. if (wc->free) {
  2311. ret = btrfs_read_buffer(next, ptr_gen,
  2312. *level - 1, &first_key);
  2313. if (ret) {
  2314. free_extent_buffer(next);
  2315. return ret;
  2316. }
  2317. if (trans) {
  2318. btrfs_tree_lock(next);
  2319. btrfs_set_lock_blocking(next);
  2320. clean_tree_block(fs_info, next);
  2321. btrfs_wait_tree_block_writeback(next);
  2322. btrfs_tree_unlock(next);
  2323. } else {
  2324. if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
  2325. clear_extent_buffer_dirty(next);
  2326. }
  2327. WARN_ON(root_owner !=
  2328. BTRFS_TREE_LOG_OBJECTID);
  2329. ret = btrfs_free_and_pin_reserved_extent(
  2330. fs_info, bytenr,
  2331. blocksize);
  2332. if (ret) {
  2333. free_extent_buffer(next);
  2334. return ret;
  2335. }
  2336. }
  2337. free_extent_buffer(next);
  2338. continue;
  2339. }
  2340. ret = btrfs_read_buffer(next, ptr_gen, *level - 1, &first_key);
  2341. if (ret) {
  2342. free_extent_buffer(next);
  2343. return ret;
  2344. }
  2345. WARN_ON(*level <= 0);
  2346. if (path->nodes[*level-1])
  2347. free_extent_buffer(path->nodes[*level-1]);
  2348. path->nodes[*level-1] = next;
  2349. *level = btrfs_header_level(next);
  2350. path->slots[*level] = 0;
  2351. cond_resched();
  2352. }
  2353. WARN_ON(*level < 0);
  2354. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  2355. path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
  2356. cond_resched();
  2357. return 0;
  2358. }
  2359. static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
  2360. struct btrfs_root *root,
  2361. struct btrfs_path *path, int *level,
  2362. struct walk_control *wc)
  2363. {
  2364. struct btrfs_fs_info *fs_info = root->fs_info;
  2365. u64 root_owner;
  2366. int i;
  2367. int slot;
  2368. int ret;
  2369. for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
  2370. slot = path->slots[i];
  2371. if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
  2372. path->slots[i]++;
  2373. *level = i;
  2374. WARN_ON(*level == 0);
  2375. return 0;
  2376. } else {
  2377. struct extent_buffer *parent;
  2378. if (path->nodes[*level] == root->node)
  2379. parent = path->nodes[*level];
  2380. else
  2381. parent = path->nodes[*level + 1];
  2382. root_owner = btrfs_header_owner(parent);
  2383. ret = wc->process_func(root, path->nodes[*level], wc,
  2384. btrfs_header_generation(path->nodes[*level]),
  2385. *level);
  2386. if (ret)
  2387. return ret;
  2388. if (wc->free) {
  2389. struct extent_buffer *next;
  2390. next = path->nodes[*level];
  2391. if (trans) {
  2392. btrfs_tree_lock(next);
  2393. btrfs_set_lock_blocking(next);
  2394. clean_tree_block(fs_info, next);
  2395. btrfs_wait_tree_block_writeback(next);
  2396. btrfs_tree_unlock(next);
  2397. } else {
  2398. if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
  2399. clear_extent_buffer_dirty(next);
  2400. }
  2401. WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
  2402. ret = btrfs_free_and_pin_reserved_extent(
  2403. fs_info,
  2404. path->nodes[*level]->start,
  2405. path->nodes[*level]->len);
  2406. if (ret)
  2407. return ret;
  2408. }
  2409. free_extent_buffer(path->nodes[*level]);
  2410. path->nodes[*level] = NULL;
  2411. *level = i + 1;
  2412. }
  2413. }
  2414. return 1;
  2415. }
  2416. /*
  2417. * drop the reference count on the tree rooted at 'snap'. This traverses
  2418. * the tree freeing any blocks that have a ref count of zero after being
  2419. * decremented.
  2420. */
  2421. static int walk_log_tree(struct btrfs_trans_handle *trans,
  2422. struct btrfs_root *log, struct walk_control *wc)
  2423. {
  2424. struct btrfs_fs_info *fs_info = log->fs_info;
  2425. int ret = 0;
  2426. int wret;
  2427. int level;
  2428. struct btrfs_path *path;
  2429. int orig_level;
  2430. path = btrfs_alloc_path();
  2431. if (!path)
  2432. return -ENOMEM;
  2433. level = btrfs_header_level(log->node);
  2434. orig_level = level;
  2435. path->nodes[level] = log->node;
  2436. extent_buffer_get(log->node);
  2437. path->slots[level] = 0;
  2438. while (1) {
  2439. wret = walk_down_log_tree(trans, log, path, &level, wc);
  2440. if (wret > 0)
  2441. break;
  2442. if (wret < 0) {
  2443. ret = wret;
  2444. goto out;
  2445. }
  2446. wret = walk_up_log_tree(trans, log, path, &level, wc);
  2447. if (wret > 0)
  2448. break;
  2449. if (wret < 0) {
  2450. ret = wret;
  2451. goto out;
  2452. }
  2453. }
  2454. /* was the root node processed? if not, catch it here */
  2455. if (path->nodes[orig_level]) {
  2456. ret = wc->process_func(log, path->nodes[orig_level], wc,
  2457. btrfs_header_generation(path->nodes[orig_level]),
  2458. orig_level);
  2459. if (ret)
  2460. goto out;
  2461. if (wc->free) {
  2462. struct extent_buffer *next;
  2463. next = path->nodes[orig_level];
  2464. if (trans) {
  2465. btrfs_tree_lock(next);
  2466. btrfs_set_lock_blocking(next);
  2467. clean_tree_block(fs_info, next);
  2468. btrfs_wait_tree_block_writeback(next);
  2469. btrfs_tree_unlock(next);
  2470. } else {
  2471. if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
  2472. clear_extent_buffer_dirty(next);
  2473. }
  2474. WARN_ON(log->root_key.objectid !=
  2475. BTRFS_TREE_LOG_OBJECTID);
  2476. ret = btrfs_free_and_pin_reserved_extent(fs_info,
  2477. next->start, next->len);
  2478. if (ret)
  2479. goto out;
  2480. }
  2481. }
  2482. out:
  2483. btrfs_free_path(path);
  2484. return ret;
  2485. }
  2486. /*
  2487. * helper function to update the item for a given subvolumes log root
  2488. * in the tree of log roots
  2489. */
  2490. static int update_log_root(struct btrfs_trans_handle *trans,
  2491. struct btrfs_root *log)
  2492. {
  2493. struct btrfs_fs_info *fs_info = log->fs_info;
  2494. int ret;
  2495. if (log->log_transid == 1) {
  2496. /* insert root item on the first sync */
  2497. ret = btrfs_insert_root(trans, fs_info->log_root_tree,
  2498. &log->root_key, &log->root_item);
  2499. } else {
  2500. ret = btrfs_update_root(trans, fs_info->log_root_tree,
  2501. &log->root_key, &log->root_item);
  2502. }
  2503. return ret;
  2504. }
  2505. static void wait_log_commit(struct btrfs_root *root, int transid)
  2506. {
  2507. DEFINE_WAIT(wait);
  2508. int index = transid % 2;
  2509. /*
  2510. * we only allow two pending log transactions at a time,
  2511. * so we know that if ours is more than 2 older than the
  2512. * current transaction, we're done
  2513. */
  2514. for (;;) {
  2515. prepare_to_wait(&root->log_commit_wait[index],
  2516. &wait, TASK_UNINTERRUPTIBLE);
  2517. if (!(root->log_transid_committed < transid &&
  2518. atomic_read(&root->log_commit[index])))
  2519. break;
  2520. mutex_unlock(&root->log_mutex);
  2521. schedule();
  2522. mutex_lock(&root->log_mutex);
  2523. }
  2524. finish_wait(&root->log_commit_wait[index], &wait);
  2525. }
  2526. static void wait_for_writer(struct btrfs_root *root)
  2527. {
  2528. DEFINE_WAIT(wait);
  2529. for (;;) {
  2530. prepare_to_wait(&root->log_writer_wait, &wait,
  2531. TASK_UNINTERRUPTIBLE);
  2532. if (!atomic_read(&root->log_writers))
  2533. break;
  2534. mutex_unlock(&root->log_mutex);
  2535. schedule();
  2536. mutex_lock(&root->log_mutex);
  2537. }
  2538. finish_wait(&root->log_writer_wait, &wait);
  2539. }
  2540. static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
  2541. struct btrfs_log_ctx *ctx)
  2542. {
  2543. if (!ctx)
  2544. return;
  2545. mutex_lock(&root->log_mutex);
  2546. list_del_init(&ctx->list);
  2547. mutex_unlock(&root->log_mutex);
  2548. }
  2549. /*
  2550. * Invoked in log mutex context, or be sure there is no other task which
  2551. * can access the list.
  2552. */
  2553. static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
  2554. int index, int error)
  2555. {
  2556. struct btrfs_log_ctx *ctx;
  2557. struct btrfs_log_ctx *safe;
  2558. list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
  2559. list_del_init(&ctx->list);
  2560. ctx->log_ret = error;
  2561. }
  2562. INIT_LIST_HEAD(&root->log_ctxs[index]);
  2563. }
  2564. /*
  2565. * btrfs_sync_log does sends a given tree log down to the disk and
  2566. * updates the super blocks to record it. When this call is done,
  2567. * you know that any inodes previously logged are safely on disk only
  2568. * if it returns 0.
  2569. *
  2570. * Any other return value means you need to call btrfs_commit_transaction.
  2571. * Some of the edge cases for fsyncing directories that have had unlinks
  2572. * or renames done in the past mean that sometimes the only safe
  2573. * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
  2574. * that has happened.
  2575. */
  2576. int btrfs_sync_log(struct btrfs_trans_handle *trans,
  2577. struct btrfs_root *root, struct btrfs_log_ctx *ctx)
  2578. {
  2579. int index1;
  2580. int index2;
  2581. int mark;
  2582. int ret;
  2583. struct btrfs_fs_info *fs_info = root->fs_info;
  2584. struct btrfs_root *log = root->log_root;
  2585. struct btrfs_root *log_root_tree = fs_info->log_root_tree;
  2586. int log_transid = 0;
  2587. struct btrfs_log_ctx root_log_ctx;
  2588. struct blk_plug plug;
  2589. mutex_lock(&root->log_mutex);
  2590. log_transid = ctx->log_transid;
  2591. if (root->log_transid_committed >= log_transid) {
  2592. mutex_unlock(&root->log_mutex);
  2593. return ctx->log_ret;
  2594. }
  2595. index1 = log_transid % 2;
  2596. if (atomic_read(&root->log_commit[index1])) {
  2597. wait_log_commit(root, log_transid);
  2598. mutex_unlock(&root->log_mutex);
  2599. return ctx->log_ret;
  2600. }
  2601. ASSERT(log_transid == root->log_transid);
  2602. atomic_set(&root->log_commit[index1], 1);
  2603. /* wait for previous tree log sync to complete */
  2604. if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
  2605. wait_log_commit(root, log_transid - 1);
  2606. while (1) {
  2607. int batch = atomic_read(&root->log_batch);
  2608. /* when we're on an ssd, just kick the log commit out */
  2609. if (!btrfs_test_opt(fs_info, SSD) &&
  2610. test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
  2611. mutex_unlock(&root->log_mutex);
  2612. schedule_timeout_uninterruptible(1);
  2613. mutex_lock(&root->log_mutex);
  2614. }
  2615. wait_for_writer(root);
  2616. if (batch == atomic_read(&root->log_batch))
  2617. break;
  2618. }
  2619. /* bail out if we need to do a full commit */
  2620. if (btrfs_need_log_full_commit(fs_info, trans)) {
  2621. ret = -EAGAIN;
  2622. btrfs_free_logged_extents(log, log_transid);
  2623. mutex_unlock(&root->log_mutex);
  2624. goto out;
  2625. }
  2626. if (log_transid % 2 == 0)
  2627. mark = EXTENT_DIRTY;
  2628. else
  2629. mark = EXTENT_NEW;
  2630. /* we start IO on all the marked extents here, but we don't actually
  2631. * wait for them until later.
  2632. */
  2633. blk_start_plug(&plug);
  2634. ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark);
  2635. if (ret) {
  2636. blk_finish_plug(&plug);
  2637. btrfs_abort_transaction(trans, ret);
  2638. btrfs_free_logged_extents(log, log_transid);
  2639. btrfs_set_log_full_commit(fs_info, trans);
  2640. mutex_unlock(&root->log_mutex);
  2641. goto out;
  2642. }
  2643. btrfs_set_root_node(&log->root_item, log->node);
  2644. root->log_transid++;
  2645. log->log_transid = root->log_transid;
  2646. root->log_start_pid = 0;
  2647. /*
  2648. * IO has been started, blocks of the log tree have WRITTEN flag set
  2649. * in their headers. new modifications of the log will be written to
  2650. * new positions. so it's safe to allow log writers to go in.
  2651. */
  2652. mutex_unlock(&root->log_mutex);
  2653. btrfs_init_log_ctx(&root_log_ctx, NULL);
  2654. mutex_lock(&log_root_tree->log_mutex);
  2655. atomic_inc(&log_root_tree->log_batch);
  2656. atomic_inc(&log_root_tree->log_writers);
  2657. index2 = log_root_tree->log_transid % 2;
  2658. list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
  2659. root_log_ctx.log_transid = log_root_tree->log_transid;
  2660. mutex_unlock(&log_root_tree->log_mutex);
  2661. ret = update_log_root(trans, log);
  2662. mutex_lock(&log_root_tree->log_mutex);
  2663. if (atomic_dec_and_test(&log_root_tree->log_writers)) {
  2664. /*
  2665. * Implicit memory barrier after atomic_dec_and_test
  2666. */
  2667. if (waitqueue_active(&log_root_tree->log_writer_wait))
  2668. wake_up(&log_root_tree->log_writer_wait);
  2669. }
  2670. if (ret) {
  2671. if (!list_empty(&root_log_ctx.list))
  2672. list_del_init(&root_log_ctx.list);
  2673. blk_finish_plug(&plug);
  2674. btrfs_set_log_full_commit(fs_info, trans);
  2675. if (ret != -ENOSPC) {
  2676. btrfs_abort_transaction(trans, ret);
  2677. mutex_unlock(&log_root_tree->log_mutex);
  2678. goto out;
  2679. }
  2680. btrfs_wait_tree_log_extents(log, mark);
  2681. btrfs_free_logged_extents(log, log_transid);
  2682. mutex_unlock(&log_root_tree->log_mutex);
  2683. ret = -EAGAIN;
  2684. goto out;
  2685. }
  2686. if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
  2687. blk_finish_plug(&plug);
  2688. list_del_init(&root_log_ctx.list);
  2689. mutex_unlock(&log_root_tree->log_mutex);
  2690. ret = root_log_ctx.log_ret;
  2691. goto out;
  2692. }
  2693. index2 = root_log_ctx.log_transid % 2;
  2694. if (atomic_read(&log_root_tree->log_commit[index2])) {
  2695. blk_finish_plug(&plug);
  2696. ret = btrfs_wait_tree_log_extents(log, mark);
  2697. btrfs_wait_logged_extents(trans, log, log_transid);
  2698. wait_log_commit(log_root_tree,
  2699. root_log_ctx.log_transid);
  2700. mutex_unlock(&log_root_tree->log_mutex);
  2701. if (!ret)
  2702. ret = root_log_ctx.log_ret;
  2703. goto out;
  2704. }
  2705. ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
  2706. atomic_set(&log_root_tree->log_commit[index2], 1);
  2707. if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
  2708. wait_log_commit(log_root_tree,
  2709. root_log_ctx.log_transid - 1);
  2710. }
  2711. wait_for_writer(log_root_tree);
  2712. /*
  2713. * now that we've moved on to the tree of log tree roots,
  2714. * check the full commit flag again
  2715. */
  2716. if (btrfs_need_log_full_commit(fs_info, trans)) {
  2717. blk_finish_plug(&plug);
  2718. btrfs_wait_tree_log_extents(log, mark);
  2719. btrfs_free_logged_extents(log, log_transid);
  2720. mutex_unlock(&log_root_tree->log_mutex);
  2721. ret = -EAGAIN;
  2722. goto out_wake_log_root;
  2723. }
  2724. ret = btrfs_write_marked_extents(fs_info,
  2725. &log_root_tree->dirty_log_pages,
  2726. EXTENT_DIRTY | EXTENT_NEW);
  2727. blk_finish_plug(&plug);
  2728. if (ret) {
  2729. btrfs_set_log_full_commit(fs_info, trans);
  2730. btrfs_abort_transaction(trans, ret);
  2731. btrfs_free_logged_extents(log, log_transid);
  2732. mutex_unlock(&log_root_tree->log_mutex);
  2733. goto out_wake_log_root;
  2734. }
  2735. ret = btrfs_wait_tree_log_extents(log, mark);
  2736. if (!ret)
  2737. ret = btrfs_wait_tree_log_extents(log_root_tree,
  2738. EXTENT_NEW | EXTENT_DIRTY);
  2739. if (ret) {
  2740. btrfs_set_log_full_commit(fs_info, trans);
  2741. btrfs_free_logged_extents(log, log_transid);
  2742. mutex_unlock(&log_root_tree->log_mutex);
  2743. goto out_wake_log_root;
  2744. }
  2745. btrfs_wait_logged_extents(trans, log, log_transid);
  2746. btrfs_set_super_log_root(fs_info->super_for_commit,
  2747. log_root_tree->node->start);
  2748. btrfs_set_super_log_root_level(fs_info->super_for_commit,
  2749. btrfs_header_level(log_root_tree->node));
  2750. log_root_tree->log_transid++;
  2751. mutex_unlock(&log_root_tree->log_mutex);
  2752. /*
  2753. * nobody else is going to jump in and write the the ctree
  2754. * super here because the log_commit atomic below is protecting
  2755. * us. We must be called with a transaction handle pinning
  2756. * the running transaction open, so a full commit can't hop
  2757. * in and cause problems either.
  2758. */
  2759. ret = write_all_supers(fs_info, 1);
  2760. if (ret) {
  2761. btrfs_set_log_full_commit(fs_info, trans);
  2762. btrfs_abort_transaction(trans, ret);
  2763. goto out_wake_log_root;
  2764. }
  2765. mutex_lock(&root->log_mutex);
  2766. if (root->last_log_commit < log_transid)
  2767. root->last_log_commit = log_transid;
  2768. mutex_unlock(&root->log_mutex);
  2769. out_wake_log_root:
  2770. mutex_lock(&log_root_tree->log_mutex);
  2771. btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
  2772. log_root_tree->log_transid_committed++;
  2773. atomic_set(&log_root_tree->log_commit[index2], 0);
  2774. mutex_unlock(&log_root_tree->log_mutex);
  2775. /*
  2776. * The barrier before waitqueue_active is implied by mutex_unlock
  2777. */
  2778. if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
  2779. wake_up(&log_root_tree->log_commit_wait[index2]);
  2780. out:
  2781. mutex_lock(&root->log_mutex);
  2782. btrfs_remove_all_log_ctxs(root, index1, ret);
  2783. root->log_transid_committed++;
  2784. atomic_set(&root->log_commit[index1], 0);
  2785. mutex_unlock(&root->log_mutex);
  2786. /*
  2787. * The barrier before waitqueue_active is implied by mutex_unlock
  2788. */
  2789. if (waitqueue_active(&root->log_commit_wait[index1]))
  2790. wake_up(&root->log_commit_wait[index1]);
  2791. return ret;
  2792. }
  2793. static void free_log_tree(struct btrfs_trans_handle *trans,
  2794. struct btrfs_root *log)
  2795. {
  2796. int ret;
  2797. u64 start;
  2798. u64 end;
  2799. struct walk_control wc = {
  2800. .free = 1,
  2801. .process_func = process_one_buffer
  2802. };
  2803. ret = walk_log_tree(trans, log, &wc);
  2804. /* I don't think this can happen but just in case */
  2805. if (ret)
  2806. btrfs_abort_transaction(trans, ret);
  2807. while (1) {
  2808. ret = find_first_extent_bit(&log->dirty_log_pages,
  2809. 0, &start, &end,
  2810. EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT,
  2811. NULL);
  2812. if (ret)
  2813. break;
  2814. clear_extent_bits(&log->dirty_log_pages, start, end,
  2815. EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT);
  2816. }
  2817. /*
  2818. * We may have short-circuited the log tree with the full commit logic
  2819. * and left ordered extents on our list, so clear these out to keep us
  2820. * from leaking inodes and memory.
  2821. */
  2822. btrfs_free_logged_extents(log, 0);
  2823. btrfs_free_logged_extents(log, 1);
  2824. free_extent_buffer(log->node);
  2825. kfree(log);
  2826. }
  2827. /*
  2828. * free all the extents used by the tree log. This should be called
  2829. * at commit time of the full transaction
  2830. */
  2831. int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
  2832. {
  2833. if (root->log_root) {
  2834. free_log_tree(trans, root->log_root);
  2835. root->log_root = NULL;
  2836. }
  2837. return 0;
  2838. }
  2839. int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
  2840. struct btrfs_fs_info *fs_info)
  2841. {
  2842. if (fs_info->log_root_tree) {
  2843. free_log_tree(trans, fs_info->log_root_tree);
  2844. fs_info->log_root_tree = NULL;
  2845. }
  2846. return 0;
  2847. }
  2848. /*
  2849. * If both a file and directory are logged, and unlinks or renames are
  2850. * mixed in, we have a few interesting corners:
  2851. *
  2852. * create file X in dir Y
  2853. * link file X to X.link in dir Y
  2854. * fsync file X
  2855. * unlink file X but leave X.link
  2856. * fsync dir Y
  2857. *
  2858. * After a crash we would expect only X.link to exist. But file X
  2859. * didn't get fsync'd again so the log has back refs for X and X.link.
  2860. *
  2861. * We solve this by removing directory entries and inode backrefs from the
  2862. * log when a file that was logged in the current transaction is
  2863. * unlinked. Any later fsync will include the updated log entries, and
  2864. * we'll be able to reconstruct the proper directory items from backrefs.
  2865. *
  2866. * This optimizations allows us to avoid relogging the entire inode
  2867. * or the entire directory.
  2868. */
  2869. int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
  2870. struct btrfs_root *root,
  2871. const char *name, int name_len,
  2872. struct btrfs_inode *dir, u64 index)
  2873. {
  2874. struct btrfs_root *log;
  2875. struct btrfs_dir_item *di;
  2876. struct btrfs_path *path;
  2877. int ret;
  2878. int err = 0;
  2879. int bytes_del = 0;
  2880. u64 dir_ino = btrfs_ino(dir);
  2881. if (dir->logged_trans < trans->transid)
  2882. return 0;
  2883. ret = join_running_log_trans(root);
  2884. if (ret)
  2885. return 0;
  2886. mutex_lock(&dir->log_mutex);
  2887. log = root->log_root;
  2888. path = btrfs_alloc_path();
  2889. if (!path) {
  2890. err = -ENOMEM;
  2891. goto out_unlock;
  2892. }
  2893. di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
  2894. name, name_len, -1);
  2895. if (IS_ERR(di)) {
  2896. err = PTR_ERR(di);
  2897. goto fail;
  2898. }
  2899. if (di) {
  2900. ret = btrfs_delete_one_dir_name(trans, log, path, di);
  2901. bytes_del += name_len;
  2902. if (ret) {
  2903. err = ret;
  2904. goto fail;
  2905. }
  2906. }
  2907. btrfs_release_path(path);
  2908. di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
  2909. index, name, name_len, -1);
  2910. if (IS_ERR(di)) {
  2911. err = PTR_ERR(di);
  2912. goto fail;
  2913. }
  2914. if (di) {
  2915. ret = btrfs_delete_one_dir_name(trans, log, path, di);
  2916. bytes_del += name_len;
  2917. if (ret) {
  2918. err = ret;
  2919. goto fail;
  2920. }
  2921. }
  2922. /* update the directory size in the log to reflect the names
  2923. * we have removed
  2924. */
  2925. if (bytes_del) {
  2926. struct btrfs_key key;
  2927. key.objectid = dir_ino;
  2928. key.offset = 0;
  2929. key.type = BTRFS_INODE_ITEM_KEY;
  2930. btrfs_release_path(path);
  2931. ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
  2932. if (ret < 0) {
  2933. err = ret;
  2934. goto fail;
  2935. }
  2936. if (ret == 0) {
  2937. struct btrfs_inode_item *item;
  2938. u64 i_size;
  2939. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2940. struct btrfs_inode_item);
  2941. i_size = btrfs_inode_size(path->nodes[0], item);
  2942. if (i_size > bytes_del)
  2943. i_size -= bytes_del;
  2944. else
  2945. i_size = 0;
  2946. btrfs_set_inode_size(path->nodes[0], item, i_size);
  2947. btrfs_mark_buffer_dirty(path->nodes[0]);
  2948. } else
  2949. ret = 0;
  2950. btrfs_release_path(path);
  2951. }
  2952. fail:
  2953. btrfs_free_path(path);
  2954. out_unlock:
  2955. mutex_unlock(&dir->log_mutex);
  2956. if (ret == -ENOSPC) {
  2957. btrfs_set_log_full_commit(root->fs_info, trans);
  2958. ret = 0;
  2959. } else if (ret < 0)
  2960. btrfs_abort_transaction(trans, ret);
  2961. btrfs_end_log_trans(root);
  2962. return err;
  2963. }
  2964. /* see comments for btrfs_del_dir_entries_in_log */
  2965. int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
  2966. struct btrfs_root *root,
  2967. const char *name, int name_len,
  2968. struct btrfs_inode *inode, u64 dirid)
  2969. {
  2970. struct btrfs_fs_info *fs_info = root->fs_info;
  2971. struct btrfs_root *log;
  2972. u64 index;
  2973. int ret;
  2974. if (inode->logged_trans < trans->transid)
  2975. return 0;
  2976. ret = join_running_log_trans(root);
  2977. if (ret)
  2978. return 0;
  2979. log = root->log_root;
  2980. mutex_lock(&inode->log_mutex);
  2981. ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
  2982. dirid, &index);
  2983. mutex_unlock(&inode->log_mutex);
  2984. if (ret == -ENOSPC) {
  2985. btrfs_set_log_full_commit(fs_info, trans);
  2986. ret = 0;
  2987. } else if (ret < 0 && ret != -ENOENT)
  2988. btrfs_abort_transaction(trans, ret);
  2989. btrfs_end_log_trans(root);
  2990. return ret;
  2991. }
  2992. /*
  2993. * creates a range item in the log for 'dirid'. first_offset and
  2994. * last_offset tell us which parts of the key space the log should
  2995. * be considered authoritative for.
  2996. */
  2997. static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
  2998. struct btrfs_root *log,
  2999. struct btrfs_path *path,
  3000. int key_type, u64 dirid,
  3001. u64 first_offset, u64 last_offset)
  3002. {
  3003. int ret;
  3004. struct btrfs_key key;
  3005. struct btrfs_dir_log_item *item;
  3006. key.objectid = dirid;
  3007. key.offset = first_offset;
  3008. if (key_type == BTRFS_DIR_ITEM_KEY)
  3009. key.type = BTRFS_DIR_LOG_ITEM_KEY;
  3010. else
  3011. key.type = BTRFS_DIR_LOG_INDEX_KEY;
  3012. ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
  3013. if (ret)
  3014. return ret;
  3015. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3016. struct btrfs_dir_log_item);
  3017. btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
  3018. btrfs_mark_buffer_dirty(path->nodes[0]);
  3019. btrfs_release_path(path);
  3020. return 0;
  3021. }
  3022. /*
  3023. * log all the items included in the current transaction for a given
  3024. * directory. This also creates the range items in the log tree required
  3025. * to replay anything deleted before the fsync
  3026. */
  3027. static noinline int log_dir_items(struct btrfs_trans_handle *trans,
  3028. struct btrfs_root *root, struct btrfs_inode *inode,
  3029. struct btrfs_path *path,
  3030. struct btrfs_path *dst_path, int key_type,
  3031. struct btrfs_log_ctx *ctx,
  3032. u64 min_offset, u64 *last_offset_ret)
  3033. {
  3034. struct btrfs_key min_key;
  3035. struct btrfs_root *log = root->log_root;
  3036. struct extent_buffer *src;
  3037. int err = 0;
  3038. int ret;
  3039. int i;
  3040. int nritems;
  3041. u64 first_offset = min_offset;
  3042. u64 last_offset = (u64)-1;
  3043. u64 ino = btrfs_ino(inode);
  3044. log = root->log_root;
  3045. min_key.objectid = ino;
  3046. min_key.type = key_type;
  3047. min_key.offset = min_offset;
  3048. ret = btrfs_search_forward(root, &min_key, path, trans->transid);
  3049. /*
  3050. * we didn't find anything from this transaction, see if there
  3051. * is anything at all
  3052. */
  3053. if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
  3054. min_key.objectid = ino;
  3055. min_key.type = key_type;
  3056. min_key.offset = (u64)-1;
  3057. btrfs_release_path(path);
  3058. ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
  3059. if (ret < 0) {
  3060. btrfs_release_path(path);
  3061. return ret;
  3062. }
  3063. ret = btrfs_previous_item(root, path, ino, key_type);
  3064. /* if ret == 0 there are items for this type,
  3065. * create a range to tell us the last key of this type.
  3066. * otherwise, there are no items in this directory after
  3067. * *min_offset, and we create a range to indicate that.
  3068. */
  3069. if (ret == 0) {
  3070. struct btrfs_key tmp;
  3071. btrfs_item_key_to_cpu(path->nodes[0], &tmp,
  3072. path->slots[0]);
  3073. if (key_type == tmp.type)
  3074. first_offset = max(min_offset, tmp.offset) + 1;
  3075. }
  3076. goto done;
  3077. }
  3078. /* go backward to find any previous key */
  3079. ret = btrfs_previous_item(root, path, ino, key_type);
  3080. if (ret == 0) {
  3081. struct btrfs_key tmp;
  3082. btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
  3083. if (key_type == tmp.type) {
  3084. first_offset = tmp.offset;
  3085. ret = overwrite_item(trans, log, dst_path,
  3086. path->nodes[0], path->slots[0],
  3087. &tmp);
  3088. if (ret) {
  3089. err = ret;
  3090. goto done;
  3091. }
  3092. }
  3093. }
  3094. btrfs_release_path(path);
  3095. /* find the first key from this transaction again */
  3096. ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
  3097. if (WARN_ON(ret != 0))
  3098. goto done;
  3099. /*
  3100. * we have a block from this transaction, log every item in it
  3101. * from our directory
  3102. */
  3103. while (1) {
  3104. struct btrfs_key tmp;
  3105. src = path->nodes[0];
  3106. nritems = btrfs_header_nritems(src);
  3107. for (i = path->slots[0]; i < nritems; i++) {
  3108. struct btrfs_dir_item *di;
  3109. btrfs_item_key_to_cpu(src, &min_key, i);
  3110. if (min_key.objectid != ino || min_key.type != key_type)
  3111. goto done;
  3112. ret = overwrite_item(trans, log, dst_path, src, i,
  3113. &min_key);
  3114. if (ret) {
  3115. err = ret;
  3116. goto done;
  3117. }
  3118. /*
  3119. * We must make sure that when we log a directory entry,
  3120. * the corresponding inode, after log replay, has a
  3121. * matching link count. For example:
  3122. *
  3123. * touch foo
  3124. * mkdir mydir
  3125. * sync
  3126. * ln foo mydir/bar
  3127. * xfs_io -c "fsync" mydir
  3128. * <crash>
  3129. * <mount fs and log replay>
  3130. *
  3131. * Would result in a fsync log that when replayed, our
  3132. * file inode would have a link count of 1, but we get
  3133. * two directory entries pointing to the same inode.
  3134. * After removing one of the names, it would not be
  3135. * possible to remove the other name, which resulted
  3136. * always in stale file handle errors, and would not
  3137. * be possible to rmdir the parent directory, since
  3138. * its i_size could never decrement to the value
  3139. * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
  3140. */
  3141. di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
  3142. btrfs_dir_item_key_to_cpu(src, di, &tmp);
  3143. if (ctx &&
  3144. (btrfs_dir_transid(src, di) == trans->transid ||
  3145. btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
  3146. tmp.type != BTRFS_ROOT_ITEM_KEY)
  3147. ctx->log_new_dentries = true;
  3148. }
  3149. path->slots[0] = nritems;
  3150. /*
  3151. * look ahead to the next item and see if it is also
  3152. * from this directory and from this transaction
  3153. */
  3154. ret = btrfs_next_leaf(root, path);
  3155. if (ret) {
  3156. if (ret == 1)
  3157. last_offset = (u64)-1;
  3158. else
  3159. err = ret;
  3160. goto done;
  3161. }
  3162. btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
  3163. if (tmp.objectid != ino || tmp.type != key_type) {
  3164. last_offset = (u64)-1;
  3165. goto done;
  3166. }
  3167. if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
  3168. ret = overwrite_item(trans, log, dst_path,
  3169. path->nodes[0], path->slots[0],
  3170. &tmp);
  3171. if (ret)
  3172. err = ret;
  3173. else
  3174. last_offset = tmp.offset;
  3175. goto done;
  3176. }
  3177. }
  3178. done:
  3179. btrfs_release_path(path);
  3180. btrfs_release_path(dst_path);
  3181. if (err == 0) {
  3182. *last_offset_ret = last_offset;
  3183. /*
  3184. * insert the log range keys to indicate where the log
  3185. * is valid
  3186. */
  3187. ret = insert_dir_log_key(trans, log, path, key_type,
  3188. ino, first_offset, last_offset);
  3189. if (ret)
  3190. err = ret;
  3191. }
  3192. return err;
  3193. }
  3194. /*
  3195. * logging directories is very similar to logging inodes, We find all the items
  3196. * from the current transaction and write them to the log.
  3197. *
  3198. * The recovery code scans the directory in the subvolume, and if it finds a
  3199. * key in the range logged that is not present in the log tree, then it means
  3200. * that dir entry was unlinked during the transaction.
  3201. *
  3202. * In order for that scan to work, we must include one key smaller than
  3203. * the smallest logged by this transaction and one key larger than the largest
  3204. * key logged by this transaction.
  3205. */
  3206. static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
  3207. struct btrfs_root *root, struct btrfs_inode *inode,
  3208. struct btrfs_path *path,
  3209. struct btrfs_path *dst_path,
  3210. struct btrfs_log_ctx *ctx)
  3211. {
  3212. u64 min_key;
  3213. u64 max_key;
  3214. int ret;
  3215. int key_type = BTRFS_DIR_ITEM_KEY;
  3216. again:
  3217. min_key = 0;
  3218. max_key = 0;
  3219. while (1) {
  3220. ret = log_dir_items(trans, root, inode, path, dst_path, key_type,
  3221. ctx, min_key, &max_key);
  3222. if (ret)
  3223. return ret;
  3224. if (max_key == (u64)-1)
  3225. break;
  3226. min_key = max_key + 1;
  3227. }
  3228. if (key_type == BTRFS_DIR_ITEM_KEY) {
  3229. key_type = BTRFS_DIR_INDEX_KEY;
  3230. goto again;
  3231. }
  3232. return 0;
  3233. }
  3234. /*
  3235. * a helper function to drop items from the log before we relog an
  3236. * inode. max_key_type indicates the highest item type to remove.
  3237. * This cannot be run for file data extents because it does not
  3238. * free the extents they point to.
  3239. */
  3240. static int drop_objectid_items(struct btrfs_trans_handle *trans,
  3241. struct btrfs_root *log,
  3242. struct btrfs_path *path,
  3243. u64 objectid, int max_key_type)
  3244. {
  3245. int ret;
  3246. struct btrfs_key key;
  3247. struct btrfs_key found_key;
  3248. int start_slot;
  3249. key.objectid = objectid;
  3250. key.type = max_key_type;
  3251. key.offset = (u64)-1;
  3252. while (1) {
  3253. ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
  3254. BUG_ON(ret == 0); /* Logic error */
  3255. if (ret < 0)
  3256. break;
  3257. if (path->slots[0] == 0)
  3258. break;
  3259. path->slots[0]--;
  3260. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  3261. path->slots[0]);
  3262. if (found_key.objectid != objectid)
  3263. break;
  3264. found_key.offset = 0;
  3265. found_key.type = 0;
  3266. ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
  3267. &start_slot);
  3268. ret = btrfs_del_items(trans, log, path, start_slot,
  3269. path->slots[0] - start_slot + 1);
  3270. /*
  3271. * If start slot isn't 0 then we don't need to re-search, we've
  3272. * found the last guy with the objectid in this tree.
  3273. */
  3274. if (ret || start_slot != 0)
  3275. break;
  3276. btrfs_release_path(path);
  3277. }
  3278. btrfs_release_path(path);
  3279. if (ret > 0)
  3280. ret = 0;
  3281. return ret;
  3282. }
  3283. static void fill_inode_item(struct btrfs_trans_handle *trans,
  3284. struct extent_buffer *leaf,
  3285. struct btrfs_inode_item *item,
  3286. struct inode *inode, int log_inode_only,
  3287. u64 logged_isize)
  3288. {
  3289. struct btrfs_map_token token;
  3290. btrfs_init_map_token(&token);
  3291. if (log_inode_only) {
  3292. /* set the generation to zero so the recover code
  3293. * can tell the difference between an logging
  3294. * just to say 'this inode exists' and a logging
  3295. * to say 'update this inode with these values'
  3296. */
  3297. btrfs_set_token_inode_generation(leaf, item, 0, &token);
  3298. btrfs_set_token_inode_size(leaf, item, logged_isize, &token);
  3299. } else {
  3300. btrfs_set_token_inode_generation(leaf, item,
  3301. BTRFS_I(inode)->generation,
  3302. &token);
  3303. btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
  3304. }
  3305. btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
  3306. btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
  3307. btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
  3308. btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
  3309. btrfs_set_token_timespec_sec(leaf, &item->atime,
  3310. inode->i_atime.tv_sec, &token);
  3311. btrfs_set_token_timespec_nsec(leaf, &item->atime,
  3312. inode->i_atime.tv_nsec, &token);
  3313. btrfs_set_token_timespec_sec(leaf, &item->mtime,
  3314. inode->i_mtime.tv_sec, &token);
  3315. btrfs_set_token_timespec_nsec(leaf, &item->mtime,
  3316. inode->i_mtime.tv_nsec, &token);
  3317. btrfs_set_token_timespec_sec(leaf, &item->ctime,
  3318. inode->i_ctime.tv_sec, &token);
  3319. btrfs_set_token_timespec_nsec(leaf, &item->ctime,
  3320. inode->i_ctime.tv_nsec, &token);
  3321. btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
  3322. &token);
  3323. btrfs_set_token_inode_sequence(leaf, item,
  3324. inode_peek_iversion(inode), &token);
  3325. btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
  3326. btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
  3327. btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
  3328. btrfs_set_token_inode_block_group(leaf, item, 0, &token);
  3329. }
  3330. static int log_inode_item(struct btrfs_trans_handle *trans,
  3331. struct btrfs_root *log, struct btrfs_path *path,
  3332. struct btrfs_inode *inode)
  3333. {
  3334. struct btrfs_inode_item *inode_item;
  3335. int ret;
  3336. ret = btrfs_insert_empty_item(trans, log, path,
  3337. &inode->location, sizeof(*inode_item));
  3338. if (ret && ret != -EEXIST)
  3339. return ret;
  3340. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3341. struct btrfs_inode_item);
  3342. fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode,
  3343. 0, 0);
  3344. btrfs_release_path(path);
  3345. return 0;
  3346. }
  3347. static noinline int copy_items(struct btrfs_trans_handle *trans,
  3348. struct btrfs_inode *inode,
  3349. struct btrfs_path *dst_path,
  3350. struct btrfs_path *src_path, u64 *last_extent,
  3351. int start_slot, int nr, int inode_only,
  3352. u64 logged_isize)
  3353. {
  3354. struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
  3355. unsigned long src_offset;
  3356. unsigned long dst_offset;
  3357. struct btrfs_root *log = inode->root->log_root;
  3358. struct btrfs_file_extent_item *extent;
  3359. struct btrfs_inode_item *inode_item;
  3360. struct extent_buffer *src = src_path->nodes[0];
  3361. struct btrfs_key first_key, last_key, key;
  3362. int ret;
  3363. struct btrfs_key *ins_keys;
  3364. u32 *ins_sizes;
  3365. char *ins_data;
  3366. int i;
  3367. struct list_head ordered_sums;
  3368. int skip_csum = inode->flags & BTRFS_INODE_NODATASUM;
  3369. bool has_extents = false;
  3370. bool need_find_last_extent = true;
  3371. bool done = false;
  3372. INIT_LIST_HEAD(&ordered_sums);
  3373. ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
  3374. nr * sizeof(u32), GFP_NOFS);
  3375. if (!ins_data)
  3376. return -ENOMEM;
  3377. first_key.objectid = (u64)-1;
  3378. ins_sizes = (u32 *)ins_data;
  3379. ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
  3380. for (i = 0; i < nr; i++) {
  3381. ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
  3382. btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
  3383. }
  3384. ret = btrfs_insert_empty_items(trans, log, dst_path,
  3385. ins_keys, ins_sizes, nr);
  3386. if (ret) {
  3387. kfree(ins_data);
  3388. return ret;
  3389. }
  3390. for (i = 0; i < nr; i++, dst_path->slots[0]++) {
  3391. dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
  3392. dst_path->slots[0]);
  3393. src_offset = btrfs_item_ptr_offset(src, start_slot + i);
  3394. if (i == nr - 1)
  3395. last_key = ins_keys[i];
  3396. if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
  3397. inode_item = btrfs_item_ptr(dst_path->nodes[0],
  3398. dst_path->slots[0],
  3399. struct btrfs_inode_item);
  3400. fill_inode_item(trans, dst_path->nodes[0], inode_item,
  3401. &inode->vfs_inode,
  3402. inode_only == LOG_INODE_EXISTS,
  3403. logged_isize);
  3404. } else {
  3405. copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
  3406. src_offset, ins_sizes[i]);
  3407. }
  3408. /*
  3409. * We set need_find_last_extent here in case we know we were
  3410. * processing other items and then walk into the first extent in
  3411. * the inode. If we don't hit an extent then nothing changes,
  3412. * we'll do the last search the next time around.
  3413. */
  3414. if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
  3415. has_extents = true;
  3416. if (first_key.objectid == (u64)-1)
  3417. first_key = ins_keys[i];
  3418. } else {
  3419. need_find_last_extent = false;
  3420. }
  3421. /* take a reference on file data extents so that truncates
  3422. * or deletes of this inode don't have to relog the inode
  3423. * again
  3424. */
  3425. if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
  3426. !skip_csum) {
  3427. int found_type;
  3428. extent = btrfs_item_ptr(src, start_slot + i,
  3429. struct btrfs_file_extent_item);
  3430. if (btrfs_file_extent_generation(src, extent) < trans->transid)
  3431. continue;
  3432. found_type = btrfs_file_extent_type(src, extent);
  3433. if (found_type == BTRFS_FILE_EXTENT_REG) {
  3434. u64 ds, dl, cs, cl;
  3435. ds = btrfs_file_extent_disk_bytenr(src,
  3436. extent);
  3437. /* ds == 0 is a hole */
  3438. if (ds == 0)
  3439. continue;
  3440. dl = btrfs_file_extent_disk_num_bytes(src,
  3441. extent);
  3442. cs = btrfs_file_extent_offset(src, extent);
  3443. cl = btrfs_file_extent_num_bytes(src,
  3444. extent);
  3445. if (btrfs_file_extent_compression(src,
  3446. extent)) {
  3447. cs = 0;
  3448. cl = dl;
  3449. }
  3450. ret = btrfs_lookup_csums_range(
  3451. fs_info->csum_root,
  3452. ds + cs, ds + cs + cl - 1,
  3453. &ordered_sums, 0);
  3454. if (ret) {
  3455. btrfs_release_path(dst_path);
  3456. kfree(ins_data);
  3457. return ret;
  3458. }
  3459. }
  3460. }
  3461. }
  3462. btrfs_mark_buffer_dirty(dst_path->nodes[0]);
  3463. btrfs_release_path(dst_path);
  3464. kfree(ins_data);
  3465. /*
  3466. * we have to do this after the loop above to avoid changing the
  3467. * log tree while trying to change the log tree.
  3468. */
  3469. ret = 0;
  3470. while (!list_empty(&ordered_sums)) {
  3471. struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
  3472. struct btrfs_ordered_sum,
  3473. list);
  3474. if (!ret)
  3475. ret = btrfs_csum_file_blocks(trans, log, sums);
  3476. list_del(&sums->list);
  3477. kfree(sums);
  3478. }
  3479. if (!has_extents)
  3480. return ret;
  3481. if (need_find_last_extent && *last_extent == first_key.offset) {
  3482. /*
  3483. * We don't have any leafs between our current one and the one
  3484. * we processed before that can have file extent items for our
  3485. * inode (and have a generation number smaller than our current
  3486. * transaction id).
  3487. */
  3488. need_find_last_extent = false;
  3489. }
  3490. /*
  3491. * Because we use btrfs_search_forward we could skip leaves that were
  3492. * not modified and then assume *last_extent is valid when it really
  3493. * isn't. So back up to the previous leaf and read the end of the last
  3494. * extent before we go and fill in holes.
  3495. */
  3496. if (need_find_last_extent) {
  3497. u64 len;
  3498. ret = btrfs_prev_leaf(inode->root, src_path);
  3499. if (ret < 0)
  3500. return ret;
  3501. if (ret)
  3502. goto fill_holes;
  3503. if (src_path->slots[0])
  3504. src_path->slots[0]--;
  3505. src = src_path->nodes[0];
  3506. btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
  3507. if (key.objectid != btrfs_ino(inode) ||
  3508. key.type != BTRFS_EXTENT_DATA_KEY)
  3509. goto fill_holes;
  3510. extent = btrfs_item_ptr(src, src_path->slots[0],
  3511. struct btrfs_file_extent_item);
  3512. if (btrfs_file_extent_type(src, extent) ==
  3513. BTRFS_FILE_EXTENT_INLINE) {
  3514. len = btrfs_file_extent_inline_len(src,
  3515. src_path->slots[0],
  3516. extent);
  3517. *last_extent = ALIGN(key.offset + len,
  3518. fs_info->sectorsize);
  3519. } else {
  3520. len = btrfs_file_extent_num_bytes(src, extent);
  3521. *last_extent = key.offset + len;
  3522. }
  3523. }
  3524. fill_holes:
  3525. /* So we did prev_leaf, now we need to move to the next leaf, but a few
  3526. * things could have happened
  3527. *
  3528. * 1) A merge could have happened, so we could currently be on a leaf
  3529. * that holds what we were copying in the first place.
  3530. * 2) A split could have happened, and now not all of the items we want
  3531. * are on the same leaf.
  3532. *
  3533. * So we need to adjust how we search for holes, we need to drop the
  3534. * path and re-search for the first extent key we found, and then walk
  3535. * forward until we hit the last one we copied.
  3536. */
  3537. if (need_find_last_extent) {
  3538. /* btrfs_prev_leaf could return 1 without releasing the path */
  3539. btrfs_release_path(src_path);
  3540. ret = btrfs_search_slot(NULL, inode->root, &first_key,
  3541. src_path, 0, 0);
  3542. if (ret < 0)
  3543. return ret;
  3544. ASSERT(ret == 0);
  3545. src = src_path->nodes[0];
  3546. i = src_path->slots[0];
  3547. } else {
  3548. i = start_slot;
  3549. }
  3550. /*
  3551. * Ok so here we need to go through and fill in any holes we may have
  3552. * to make sure that holes are punched for those areas in case they had
  3553. * extents previously.
  3554. */
  3555. while (!done) {
  3556. u64 offset, len;
  3557. u64 extent_end;
  3558. if (i >= btrfs_header_nritems(src_path->nodes[0])) {
  3559. ret = btrfs_next_leaf(inode->root, src_path);
  3560. if (ret < 0)
  3561. return ret;
  3562. ASSERT(ret == 0);
  3563. src = src_path->nodes[0];
  3564. i = 0;
  3565. need_find_last_extent = true;
  3566. }
  3567. btrfs_item_key_to_cpu(src, &key, i);
  3568. if (!btrfs_comp_cpu_keys(&key, &last_key))
  3569. done = true;
  3570. if (key.objectid != btrfs_ino(inode) ||
  3571. key.type != BTRFS_EXTENT_DATA_KEY) {
  3572. i++;
  3573. continue;
  3574. }
  3575. extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
  3576. if (btrfs_file_extent_type(src, extent) ==
  3577. BTRFS_FILE_EXTENT_INLINE) {
  3578. len = btrfs_file_extent_inline_len(src, i, extent);
  3579. extent_end = ALIGN(key.offset + len,
  3580. fs_info->sectorsize);
  3581. } else {
  3582. len = btrfs_file_extent_num_bytes(src, extent);
  3583. extent_end = key.offset + len;
  3584. }
  3585. i++;
  3586. if (*last_extent == key.offset) {
  3587. *last_extent = extent_end;
  3588. continue;
  3589. }
  3590. offset = *last_extent;
  3591. len = key.offset - *last_extent;
  3592. ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
  3593. offset, 0, 0, len, 0, len, 0, 0, 0);
  3594. if (ret)
  3595. break;
  3596. *last_extent = extent_end;
  3597. }
  3598. /*
  3599. * Check if there is a hole between the last extent found in our leaf
  3600. * and the first extent in the next leaf. If there is one, we need to
  3601. * log an explicit hole so that at replay time we can punch the hole.
  3602. */
  3603. if (ret == 0 &&
  3604. key.objectid == btrfs_ino(inode) &&
  3605. key.type == BTRFS_EXTENT_DATA_KEY &&
  3606. i == btrfs_header_nritems(src_path->nodes[0])) {
  3607. ret = btrfs_next_leaf(inode->root, src_path);
  3608. need_find_last_extent = true;
  3609. if (ret > 0) {
  3610. ret = 0;
  3611. } else if (ret == 0) {
  3612. btrfs_item_key_to_cpu(src_path->nodes[0], &key,
  3613. src_path->slots[0]);
  3614. if (key.objectid == btrfs_ino(inode) &&
  3615. key.type == BTRFS_EXTENT_DATA_KEY &&
  3616. *last_extent < key.offset) {
  3617. const u64 len = key.offset - *last_extent;
  3618. ret = btrfs_insert_file_extent(trans, log,
  3619. btrfs_ino(inode),
  3620. *last_extent, 0,
  3621. 0, len, 0, len,
  3622. 0, 0, 0);
  3623. }
  3624. }
  3625. }
  3626. /*
  3627. * Need to let the callers know we dropped the path so they should
  3628. * re-search.
  3629. */
  3630. if (!ret && need_find_last_extent)
  3631. ret = 1;
  3632. return ret;
  3633. }
  3634. static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
  3635. {
  3636. struct extent_map *em1, *em2;
  3637. em1 = list_entry(a, struct extent_map, list);
  3638. em2 = list_entry(b, struct extent_map, list);
  3639. if (em1->start < em2->start)
  3640. return -1;
  3641. else if (em1->start > em2->start)
  3642. return 1;
  3643. return 0;
  3644. }
  3645. static int wait_ordered_extents(struct btrfs_trans_handle *trans,
  3646. struct inode *inode,
  3647. struct btrfs_root *root,
  3648. const struct extent_map *em,
  3649. const struct list_head *logged_list,
  3650. bool *ordered_io_error)
  3651. {
  3652. struct btrfs_fs_info *fs_info = root->fs_info;
  3653. struct btrfs_ordered_extent *ordered;
  3654. struct btrfs_root *log = root->log_root;
  3655. u64 mod_start = em->mod_start;
  3656. u64 mod_len = em->mod_len;
  3657. const bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  3658. u64 csum_offset;
  3659. u64 csum_len;
  3660. LIST_HEAD(ordered_sums);
  3661. int ret = 0;
  3662. *ordered_io_error = false;
  3663. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
  3664. em->block_start == EXTENT_MAP_HOLE)
  3665. return 0;
  3666. /*
  3667. * Wait far any ordered extent that covers our extent map. If it
  3668. * finishes without an error, first check and see if our csums are on
  3669. * our outstanding ordered extents.
  3670. */
  3671. list_for_each_entry(ordered, logged_list, log_list) {
  3672. struct btrfs_ordered_sum *sum;
  3673. if (!mod_len)
  3674. break;
  3675. if (ordered->file_offset + ordered->len <= mod_start ||
  3676. mod_start + mod_len <= ordered->file_offset)
  3677. continue;
  3678. if (!test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) &&
  3679. !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) &&
  3680. !test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
  3681. const u64 start = ordered->file_offset;
  3682. const u64 end = ordered->file_offset + ordered->len - 1;
  3683. WARN_ON(ordered->inode != inode);
  3684. filemap_fdatawrite_range(inode->i_mapping, start, end);
  3685. }
  3686. wait_event(ordered->wait,
  3687. (test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) ||
  3688. test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)));
  3689. if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)) {
  3690. /*
  3691. * Clear the AS_EIO/AS_ENOSPC flags from the inode's
  3692. * i_mapping flags, so that the next fsync won't get
  3693. * an outdated io error too.
  3694. */
  3695. filemap_check_errors(inode->i_mapping);
  3696. *ordered_io_error = true;
  3697. break;
  3698. }
  3699. /*
  3700. * We are going to copy all the csums on this ordered extent, so
  3701. * go ahead and adjust mod_start and mod_len in case this
  3702. * ordered extent has already been logged.
  3703. */
  3704. if (ordered->file_offset > mod_start) {
  3705. if (ordered->file_offset + ordered->len >=
  3706. mod_start + mod_len)
  3707. mod_len = ordered->file_offset - mod_start;
  3708. /*
  3709. * If we have this case
  3710. *
  3711. * |--------- logged extent ---------|
  3712. * |----- ordered extent ----|
  3713. *
  3714. * Just don't mess with mod_start and mod_len, we'll
  3715. * just end up logging more csums than we need and it
  3716. * will be ok.
  3717. */
  3718. } else {
  3719. if (ordered->file_offset + ordered->len <
  3720. mod_start + mod_len) {
  3721. mod_len = (mod_start + mod_len) -
  3722. (ordered->file_offset + ordered->len);
  3723. mod_start = ordered->file_offset +
  3724. ordered->len;
  3725. } else {
  3726. mod_len = 0;
  3727. }
  3728. }
  3729. if (skip_csum)
  3730. continue;
  3731. /*
  3732. * To keep us from looping for the above case of an ordered
  3733. * extent that falls inside of the logged extent.
  3734. */
  3735. if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
  3736. &ordered->flags))
  3737. continue;
  3738. list_for_each_entry(sum, &ordered->list, list) {
  3739. ret = btrfs_csum_file_blocks(trans, log, sum);
  3740. if (ret)
  3741. break;
  3742. }
  3743. }
  3744. if (*ordered_io_error || !mod_len || ret || skip_csum)
  3745. return ret;
  3746. if (em->compress_type) {
  3747. csum_offset = 0;
  3748. csum_len = max(em->block_len, em->orig_block_len);
  3749. } else {
  3750. csum_offset = mod_start - em->start;
  3751. csum_len = mod_len;
  3752. }
  3753. /* block start is already adjusted for the file extent offset. */
  3754. ret = btrfs_lookup_csums_range(fs_info->csum_root,
  3755. em->block_start + csum_offset,
  3756. em->block_start + csum_offset +
  3757. csum_len - 1, &ordered_sums, 0);
  3758. if (ret)
  3759. return ret;
  3760. while (!list_empty(&ordered_sums)) {
  3761. struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
  3762. struct btrfs_ordered_sum,
  3763. list);
  3764. if (!ret)
  3765. ret = btrfs_csum_file_blocks(trans, log, sums);
  3766. list_del(&sums->list);
  3767. kfree(sums);
  3768. }
  3769. return ret;
  3770. }
  3771. static int log_one_extent(struct btrfs_trans_handle *trans,
  3772. struct btrfs_inode *inode, struct btrfs_root *root,
  3773. const struct extent_map *em,
  3774. struct btrfs_path *path,
  3775. const struct list_head *logged_list,
  3776. struct btrfs_log_ctx *ctx)
  3777. {
  3778. struct btrfs_root *log = root->log_root;
  3779. struct btrfs_file_extent_item *fi;
  3780. struct extent_buffer *leaf;
  3781. struct btrfs_map_token token;
  3782. struct btrfs_key key;
  3783. u64 extent_offset = em->start - em->orig_start;
  3784. u64 block_len;
  3785. int ret;
  3786. int extent_inserted = 0;
  3787. bool ordered_io_err = false;
  3788. ret = wait_ordered_extents(trans, &inode->vfs_inode, root, em,
  3789. logged_list, &ordered_io_err);
  3790. if (ret)
  3791. return ret;
  3792. if (ordered_io_err) {
  3793. ctx->io_err = -EIO;
  3794. return ctx->io_err;
  3795. }
  3796. btrfs_init_map_token(&token);
  3797. ret = __btrfs_drop_extents(trans, log, &inode->vfs_inode, path, em->start,
  3798. em->start + em->len, NULL, 0, 1,
  3799. sizeof(*fi), &extent_inserted);
  3800. if (ret)
  3801. return ret;
  3802. if (!extent_inserted) {
  3803. key.objectid = btrfs_ino(inode);
  3804. key.type = BTRFS_EXTENT_DATA_KEY;
  3805. key.offset = em->start;
  3806. ret = btrfs_insert_empty_item(trans, log, path, &key,
  3807. sizeof(*fi));
  3808. if (ret)
  3809. return ret;
  3810. }
  3811. leaf = path->nodes[0];
  3812. fi = btrfs_item_ptr(leaf, path->slots[0],
  3813. struct btrfs_file_extent_item);
  3814. btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
  3815. &token);
  3816. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  3817. btrfs_set_token_file_extent_type(leaf, fi,
  3818. BTRFS_FILE_EXTENT_PREALLOC,
  3819. &token);
  3820. else
  3821. btrfs_set_token_file_extent_type(leaf, fi,
  3822. BTRFS_FILE_EXTENT_REG,
  3823. &token);
  3824. block_len = max(em->block_len, em->orig_block_len);
  3825. if (em->compress_type != BTRFS_COMPRESS_NONE) {
  3826. btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
  3827. em->block_start,
  3828. &token);
  3829. btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
  3830. &token);
  3831. } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
  3832. btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
  3833. em->block_start -
  3834. extent_offset, &token);
  3835. btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
  3836. &token);
  3837. } else {
  3838. btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
  3839. btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
  3840. &token);
  3841. }
  3842. btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
  3843. btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
  3844. btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
  3845. btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
  3846. &token);
  3847. btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
  3848. btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
  3849. btrfs_mark_buffer_dirty(leaf);
  3850. btrfs_release_path(path);
  3851. return ret;
  3852. }
  3853. /*
  3854. * Log all prealloc extents beyond the inode's i_size to make sure we do not
  3855. * lose them after doing a fast fsync and replaying the log. We scan the
  3856. * subvolume's root instead of iterating the inode's extent map tree because
  3857. * otherwise we can log incorrect extent items based on extent map conversion.
  3858. * That can happen due to the fact that extent maps are merged when they
  3859. * are not in the extent map tree's list of modified extents.
  3860. */
  3861. static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans,
  3862. struct btrfs_inode *inode,
  3863. struct btrfs_path *path)
  3864. {
  3865. struct btrfs_root *root = inode->root;
  3866. struct btrfs_key key;
  3867. const u64 i_size = i_size_read(&inode->vfs_inode);
  3868. const u64 ino = btrfs_ino(inode);
  3869. struct btrfs_path *dst_path = NULL;
  3870. u64 last_extent = (u64)-1;
  3871. int ins_nr = 0;
  3872. int start_slot;
  3873. int ret;
  3874. if (!(inode->flags & BTRFS_INODE_PREALLOC))
  3875. return 0;
  3876. key.objectid = ino;
  3877. key.type = BTRFS_EXTENT_DATA_KEY;
  3878. key.offset = i_size;
  3879. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3880. if (ret < 0)
  3881. goto out;
  3882. while (true) {
  3883. struct extent_buffer *leaf = path->nodes[0];
  3884. int slot = path->slots[0];
  3885. if (slot >= btrfs_header_nritems(leaf)) {
  3886. if (ins_nr > 0) {
  3887. ret = copy_items(trans, inode, dst_path, path,
  3888. &last_extent, start_slot,
  3889. ins_nr, 1, 0);
  3890. if (ret < 0)
  3891. goto out;
  3892. ins_nr = 0;
  3893. }
  3894. ret = btrfs_next_leaf(root, path);
  3895. if (ret < 0)
  3896. goto out;
  3897. if (ret > 0) {
  3898. ret = 0;
  3899. break;
  3900. }
  3901. continue;
  3902. }
  3903. btrfs_item_key_to_cpu(leaf, &key, slot);
  3904. if (key.objectid > ino)
  3905. break;
  3906. if (WARN_ON_ONCE(key.objectid < ino) ||
  3907. key.type < BTRFS_EXTENT_DATA_KEY ||
  3908. key.offset < i_size) {
  3909. path->slots[0]++;
  3910. continue;
  3911. }
  3912. if (last_extent == (u64)-1) {
  3913. last_extent = key.offset;
  3914. /*
  3915. * Avoid logging extent items logged in past fsync calls
  3916. * and leading to duplicate keys in the log tree.
  3917. */
  3918. do {
  3919. ret = btrfs_truncate_inode_items(trans,
  3920. root->log_root,
  3921. &inode->vfs_inode,
  3922. i_size,
  3923. BTRFS_EXTENT_DATA_KEY);
  3924. } while (ret == -EAGAIN);
  3925. if (ret)
  3926. goto out;
  3927. }
  3928. if (ins_nr == 0)
  3929. start_slot = slot;
  3930. ins_nr++;
  3931. path->slots[0]++;
  3932. if (!dst_path) {
  3933. dst_path = btrfs_alloc_path();
  3934. if (!dst_path) {
  3935. ret = -ENOMEM;
  3936. goto out;
  3937. }
  3938. }
  3939. }
  3940. if (ins_nr > 0) {
  3941. ret = copy_items(trans, inode, dst_path, path, &last_extent,
  3942. start_slot, ins_nr, 1, 0);
  3943. if (ret > 0)
  3944. ret = 0;
  3945. }
  3946. out:
  3947. btrfs_release_path(path);
  3948. btrfs_free_path(dst_path);
  3949. return ret;
  3950. }
  3951. static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
  3952. struct btrfs_root *root,
  3953. struct btrfs_inode *inode,
  3954. struct btrfs_path *path,
  3955. struct list_head *logged_list,
  3956. struct btrfs_log_ctx *ctx,
  3957. const u64 start,
  3958. const u64 end)
  3959. {
  3960. struct extent_map *em, *n;
  3961. struct list_head extents;
  3962. struct extent_map_tree *tree = &inode->extent_tree;
  3963. u64 logged_start, logged_end;
  3964. u64 test_gen;
  3965. int ret = 0;
  3966. int num = 0;
  3967. INIT_LIST_HEAD(&extents);
  3968. down_write(&inode->dio_sem);
  3969. write_lock(&tree->lock);
  3970. test_gen = root->fs_info->last_trans_committed;
  3971. logged_start = start;
  3972. logged_end = end;
  3973. list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
  3974. list_del_init(&em->list);
  3975. /*
  3976. * Just an arbitrary number, this can be really CPU intensive
  3977. * once we start getting a lot of extents, and really once we
  3978. * have a bunch of extents we just want to commit since it will
  3979. * be faster.
  3980. */
  3981. if (++num > 32768) {
  3982. list_del_init(&tree->modified_extents);
  3983. ret = -EFBIG;
  3984. goto process;
  3985. }
  3986. if (em->generation <= test_gen)
  3987. continue;
  3988. /* We log prealloc extents beyond eof later. */
  3989. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) &&
  3990. em->start >= i_size_read(&inode->vfs_inode))
  3991. continue;
  3992. if (em->start < logged_start)
  3993. logged_start = em->start;
  3994. if ((em->start + em->len - 1) > logged_end)
  3995. logged_end = em->start + em->len - 1;
  3996. /* Need a ref to keep it from getting evicted from cache */
  3997. refcount_inc(&em->refs);
  3998. set_bit(EXTENT_FLAG_LOGGING, &em->flags);
  3999. list_add_tail(&em->list, &extents);
  4000. num++;
  4001. }
  4002. list_sort(NULL, &extents, extent_cmp);
  4003. btrfs_get_logged_extents(inode, logged_list, logged_start, logged_end);
  4004. /*
  4005. * Some ordered extents started by fsync might have completed
  4006. * before we could collect them into the list logged_list, which
  4007. * means they're gone, not in our logged_list nor in the inode's
  4008. * ordered tree. We want the application/user space to know an
  4009. * error happened while attempting to persist file data so that
  4010. * it can take proper action. If such error happened, we leave
  4011. * without writing to the log tree and the fsync must report the
  4012. * file data write error and not commit the current transaction.
  4013. */
  4014. ret = filemap_check_errors(inode->vfs_inode.i_mapping);
  4015. if (ret)
  4016. ctx->io_err = ret;
  4017. process:
  4018. while (!list_empty(&extents)) {
  4019. em = list_entry(extents.next, struct extent_map, list);
  4020. list_del_init(&em->list);
  4021. /*
  4022. * If we had an error we just need to delete everybody from our
  4023. * private list.
  4024. */
  4025. if (ret) {
  4026. clear_em_logging(tree, em);
  4027. free_extent_map(em);
  4028. continue;
  4029. }
  4030. write_unlock(&tree->lock);
  4031. ret = log_one_extent(trans, inode, root, em, path, logged_list,
  4032. ctx);
  4033. write_lock(&tree->lock);
  4034. clear_em_logging(tree, em);
  4035. free_extent_map(em);
  4036. }
  4037. WARN_ON(!list_empty(&extents));
  4038. write_unlock(&tree->lock);
  4039. up_write(&inode->dio_sem);
  4040. btrfs_release_path(path);
  4041. if (!ret)
  4042. ret = btrfs_log_prealloc_extents(trans, inode, path);
  4043. return ret;
  4044. }
  4045. static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode,
  4046. struct btrfs_path *path, u64 *size_ret)
  4047. {
  4048. struct btrfs_key key;
  4049. int ret;
  4050. key.objectid = btrfs_ino(inode);
  4051. key.type = BTRFS_INODE_ITEM_KEY;
  4052. key.offset = 0;
  4053. ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
  4054. if (ret < 0) {
  4055. return ret;
  4056. } else if (ret > 0) {
  4057. *size_ret = 0;
  4058. } else {
  4059. struct btrfs_inode_item *item;
  4060. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  4061. struct btrfs_inode_item);
  4062. *size_ret = btrfs_inode_size(path->nodes[0], item);
  4063. }
  4064. btrfs_release_path(path);
  4065. return 0;
  4066. }
  4067. /*
  4068. * At the moment we always log all xattrs. This is to figure out at log replay
  4069. * time which xattrs must have their deletion replayed. If a xattr is missing
  4070. * in the log tree and exists in the fs/subvol tree, we delete it. This is
  4071. * because if a xattr is deleted, the inode is fsynced and a power failure
  4072. * happens, causing the log to be replayed the next time the fs is mounted,
  4073. * we want the xattr to not exist anymore (same behaviour as other filesystems
  4074. * with a journal, ext3/4, xfs, f2fs, etc).
  4075. */
  4076. static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
  4077. struct btrfs_root *root,
  4078. struct btrfs_inode *inode,
  4079. struct btrfs_path *path,
  4080. struct btrfs_path *dst_path)
  4081. {
  4082. int ret;
  4083. struct btrfs_key key;
  4084. const u64 ino = btrfs_ino(inode);
  4085. int ins_nr = 0;
  4086. int start_slot = 0;
  4087. key.objectid = ino;
  4088. key.type = BTRFS_XATTR_ITEM_KEY;
  4089. key.offset = 0;
  4090. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4091. if (ret < 0)
  4092. return ret;
  4093. while (true) {
  4094. int slot = path->slots[0];
  4095. struct extent_buffer *leaf = path->nodes[0];
  4096. int nritems = btrfs_header_nritems(leaf);
  4097. if (slot >= nritems) {
  4098. if (ins_nr > 0) {
  4099. u64 last_extent = 0;
  4100. ret = copy_items(trans, inode, dst_path, path,
  4101. &last_extent, start_slot,
  4102. ins_nr, 1, 0);
  4103. /* can't be 1, extent items aren't processed */
  4104. ASSERT(ret <= 0);
  4105. if (ret < 0)
  4106. return ret;
  4107. ins_nr = 0;
  4108. }
  4109. ret = btrfs_next_leaf(root, path);
  4110. if (ret < 0)
  4111. return ret;
  4112. else if (ret > 0)
  4113. break;
  4114. continue;
  4115. }
  4116. btrfs_item_key_to_cpu(leaf, &key, slot);
  4117. if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
  4118. break;
  4119. if (ins_nr == 0)
  4120. start_slot = slot;
  4121. ins_nr++;
  4122. path->slots[0]++;
  4123. cond_resched();
  4124. }
  4125. if (ins_nr > 0) {
  4126. u64 last_extent = 0;
  4127. ret = copy_items(trans, inode, dst_path, path,
  4128. &last_extent, start_slot,
  4129. ins_nr, 1, 0);
  4130. /* can't be 1, extent items aren't processed */
  4131. ASSERT(ret <= 0);
  4132. if (ret < 0)
  4133. return ret;
  4134. }
  4135. return 0;
  4136. }
  4137. /*
  4138. * If the no holes feature is enabled we need to make sure any hole between the
  4139. * last extent and the i_size of our inode is explicitly marked in the log. This
  4140. * is to make sure that doing something like:
  4141. *
  4142. * 1) create file with 128Kb of data
  4143. * 2) truncate file to 64Kb
  4144. * 3) truncate file to 256Kb
  4145. * 4) fsync file
  4146. * 5) <crash/power failure>
  4147. * 6) mount fs and trigger log replay
  4148. *
  4149. * Will give us a file with a size of 256Kb, the first 64Kb of data match what
  4150. * the file had in its first 64Kb of data at step 1 and the last 192Kb of the
  4151. * file correspond to a hole. The presence of explicit holes in a log tree is
  4152. * what guarantees that log replay will remove/adjust file extent items in the
  4153. * fs/subvol tree.
  4154. *
  4155. * Here we do not need to care about holes between extents, that is already done
  4156. * by copy_items(). We also only need to do this in the full sync path, where we
  4157. * lookup for extents from the fs/subvol tree only. In the fast path case, we
  4158. * lookup the list of modified extent maps and if any represents a hole, we
  4159. * insert a corresponding extent representing a hole in the log tree.
  4160. */
  4161. static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
  4162. struct btrfs_root *root,
  4163. struct btrfs_inode *inode,
  4164. struct btrfs_path *path)
  4165. {
  4166. struct btrfs_fs_info *fs_info = root->fs_info;
  4167. int ret;
  4168. struct btrfs_key key;
  4169. u64 hole_start;
  4170. u64 hole_size;
  4171. struct extent_buffer *leaf;
  4172. struct btrfs_root *log = root->log_root;
  4173. const u64 ino = btrfs_ino(inode);
  4174. const u64 i_size = i_size_read(&inode->vfs_inode);
  4175. if (!btrfs_fs_incompat(fs_info, NO_HOLES))
  4176. return 0;
  4177. key.objectid = ino;
  4178. key.type = BTRFS_EXTENT_DATA_KEY;
  4179. key.offset = (u64)-1;
  4180. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4181. ASSERT(ret != 0);
  4182. if (ret < 0)
  4183. return ret;
  4184. ASSERT(path->slots[0] > 0);
  4185. path->slots[0]--;
  4186. leaf = path->nodes[0];
  4187. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  4188. if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
  4189. /* inode does not have any extents */
  4190. hole_start = 0;
  4191. hole_size = i_size;
  4192. } else {
  4193. struct btrfs_file_extent_item *extent;
  4194. u64 len;
  4195. /*
  4196. * If there's an extent beyond i_size, an explicit hole was
  4197. * already inserted by copy_items().
  4198. */
  4199. if (key.offset >= i_size)
  4200. return 0;
  4201. extent = btrfs_item_ptr(leaf, path->slots[0],
  4202. struct btrfs_file_extent_item);
  4203. if (btrfs_file_extent_type(leaf, extent) ==
  4204. BTRFS_FILE_EXTENT_INLINE) {
  4205. len = btrfs_file_extent_inline_len(leaf,
  4206. path->slots[0],
  4207. extent);
  4208. ASSERT(len == i_size ||
  4209. (len == fs_info->sectorsize &&
  4210. btrfs_file_extent_compression(leaf, extent) !=
  4211. BTRFS_COMPRESS_NONE));
  4212. return 0;
  4213. }
  4214. len = btrfs_file_extent_num_bytes(leaf, extent);
  4215. /* Last extent goes beyond i_size, no need to log a hole. */
  4216. if (key.offset + len > i_size)
  4217. return 0;
  4218. hole_start = key.offset + len;
  4219. hole_size = i_size - hole_start;
  4220. }
  4221. btrfs_release_path(path);
  4222. /* Last extent ends at i_size. */
  4223. if (hole_size == 0)
  4224. return 0;
  4225. hole_size = ALIGN(hole_size, fs_info->sectorsize);
  4226. ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0,
  4227. hole_size, 0, hole_size, 0, 0, 0);
  4228. return ret;
  4229. }
  4230. /*
  4231. * When we are logging a new inode X, check if it doesn't have a reference that
  4232. * matches the reference from some other inode Y created in a past transaction
  4233. * and that was renamed in the current transaction. If we don't do this, then at
  4234. * log replay time we can lose inode Y (and all its files if it's a directory):
  4235. *
  4236. * mkdir /mnt/x
  4237. * echo "hello world" > /mnt/x/foobar
  4238. * sync
  4239. * mv /mnt/x /mnt/y
  4240. * mkdir /mnt/x # or touch /mnt/x
  4241. * xfs_io -c fsync /mnt/x
  4242. * <power fail>
  4243. * mount fs, trigger log replay
  4244. *
  4245. * After the log replay procedure, we would lose the first directory and all its
  4246. * files (file foobar).
  4247. * For the case where inode Y is not a directory we simply end up losing it:
  4248. *
  4249. * echo "123" > /mnt/foo
  4250. * sync
  4251. * mv /mnt/foo /mnt/bar
  4252. * echo "abc" > /mnt/foo
  4253. * xfs_io -c fsync /mnt/foo
  4254. * <power fail>
  4255. *
  4256. * We also need this for cases where a snapshot entry is replaced by some other
  4257. * entry (file or directory) otherwise we end up with an unreplayable log due to
  4258. * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
  4259. * if it were a regular entry:
  4260. *
  4261. * mkdir /mnt/x
  4262. * btrfs subvolume snapshot /mnt /mnt/x/snap
  4263. * btrfs subvolume delete /mnt/x/snap
  4264. * rmdir /mnt/x
  4265. * mkdir /mnt/x
  4266. * fsync /mnt/x or fsync some new file inside it
  4267. * <power fail>
  4268. *
  4269. * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
  4270. * the same transaction.
  4271. */
  4272. static int btrfs_check_ref_name_override(struct extent_buffer *eb,
  4273. const int slot,
  4274. const struct btrfs_key *key,
  4275. struct btrfs_inode *inode,
  4276. u64 *other_ino)
  4277. {
  4278. int ret;
  4279. struct btrfs_path *search_path;
  4280. char *name = NULL;
  4281. u32 name_len = 0;
  4282. u32 item_size = btrfs_item_size_nr(eb, slot);
  4283. u32 cur_offset = 0;
  4284. unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
  4285. search_path = btrfs_alloc_path();
  4286. if (!search_path)
  4287. return -ENOMEM;
  4288. search_path->search_commit_root = 1;
  4289. search_path->skip_locking = 1;
  4290. while (cur_offset < item_size) {
  4291. u64 parent;
  4292. u32 this_name_len;
  4293. u32 this_len;
  4294. unsigned long name_ptr;
  4295. struct btrfs_dir_item *di;
  4296. if (key->type == BTRFS_INODE_REF_KEY) {
  4297. struct btrfs_inode_ref *iref;
  4298. iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
  4299. parent = key->offset;
  4300. this_name_len = btrfs_inode_ref_name_len(eb, iref);
  4301. name_ptr = (unsigned long)(iref + 1);
  4302. this_len = sizeof(*iref) + this_name_len;
  4303. } else {
  4304. struct btrfs_inode_extref *extref;
  4305. extref = (struct btrfs_inode_extref *)(ptr +
  4306. cur_offset);
  4307. parent = btrfs_inode_extref_parent(eb, extref);
  4308. this_name_len = btrfs_inode_extref_name_len(eb, extref);
  4309. name_ptr = (unsigned long)&extref->name;
  4310. this_len = sizeof(*extref) + this_name_len;
  4311. }
  4312. if (this_name_len > name_len) {
  4313. char *new_name;
  4314. new_name = krealloc(name, this_name_len, GFP_NOFS);
  4315. if (!new_name) {
  4316. ret = -ENOMEM;
  4317. goto out;
  4318. }
  4319. name_len = this_name_len;
  4320. name = new_name;
  4321. }
  4322. read_extent_buffer(eb, name, name_ptr, this_name_len);
  4323. di = btrfs_lookup_dir_item(NULL, inode->root, search_path,
  4324. parent, name, this_name_len, 0);
  4325. if (di && !IS_ERR(di)) {
  4326. struct btrfs_key di_key;
  4327. btrfs_dir_item_key_to_cpu(search_path->nodes[0],
  4328. di, &di_key);
  4329. if (di_key.type == BTRFS_INODE_ITEM_KEY) {
  4330. ret = 1;
  4331. *other_ino = di_key.objectid;
  4332. } else {
  4333. ret = -EAGAIN;
  4334. }
  4335. goto out;
  4336. } else if (IS_ERR(di)) {
  4337. ret = PTR_ERR(di);
  4338. goto out;
  4339. }
  4340. btrfs_release_path(search_path);
  4341. cur_offset += this_len;
  4342. }
  4343. ret = 0;
  4344. out:
  4345. btrfs_free_path(search_path);
  4346. kfree(name);
  4347. return ret;
  4348. }
  4349. /* log a single inode in the tree log.
  4350. * At least one parent directory for this inode must exist in the tree
  4351. * or be logged already.
  4352. *
  4353. * Any items from this inode changed by the current transaction are copied
  4354. * to the log tree. An extra reference is taken on any extents in this
  4355. * file, allowing us to avoid a whole pile of corner cases around logging
  4356. * blocks that have been removed from the tree.
  4357. *
  4358. * See LOG_INODE_ALL and related defines for a description of what inode_only
  4359. * does.
  4360. *
  4361. * This handles both files and directories.
  4362. */
  4363. static int btrfs_log_inode(struct btrfs_trans_handle *trans,
  4364. struct btrfs_root *root, struct btrfs_inode *inode,
  4365. int inode_only,
  4366. const loff_t start,
  4367. const loff_t end,
  4368. struct btrfs_log_ctx *ctx)
  4369. {
  4370. struct btrfs_fs_info *fs_info = root->fs_info;
  4371. struct btrfs_path *path;
  4372. struct btrfs_path *dst_path;
  4373. struct btrfs_key min_key;
  4374. struct btrfs_key max_key;
  4375. struct btrfs_root *log = root->log_root;
  4376. LIST_HEAD(logged_list);
  4377. u64 last_extent = 0;
  4378. int err = 0;
  4379. int ret;
  4380. int nritems;
  4381. int ins_start_slot = 0;
  4382. int ins_nr;
  4383. bool fast_search = false;
  4384. u64 ino = btrfs_ino(inode);
  4385. struct extent_map_tree *em_tree = &inode->extent_tree;
  4386. u64 logged_isize = 0;
  4387. bool need_log_inode_item = true;
  4388. bool xattrs_logged = false;
  4389. path = btrfs_alloc_path();
  4390. if (!path)
  4391. return -ENOMEM;
  4392. dst_path = btrfs_alloc_path();
  4393. if (!dst_path) {
  4394. btrfs_free_path(path);
  4395. return -ENOMEM;
  4396. }
  4397. min_key.objectid = ino;
  4398. min_key.type = BTRFS_INODE_ITEM_KEY;
  4399. min_key.offset = 0;
  4400. max_key.objectid = ino;
  4401. /* today the code can only do partial logging of directories */
  4402. if (S_ISDIR(inode->vfs_inode.i_mode) ||
  4403. (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  4404. &inode->runtime_flags) &&
  4405. inode_only >= LOG_INODE_EXISTS))
  4406. max_key.type = BTRFS_XATTR_ITEM_KEY;
  4407. else
  4408. max_key.type = (u8)-1;
  4409. max_key.offset = (u64)-1;
  4410. /*
  4411. * Only run delayed items if we are a dir or a new file.
  4412. * Otherwise commit the delayed inode only, which is needed in
  4413. * order for the log replay code to mark inodes for link count
  4414. * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
  4415. */
  4416. if (S_ISDIR(inode->vfs_inode.i_mode) ||
  4417. inode->generation > fs_info->last_trans_committed)
  4418. ret = btrfs_commit_inode_delayed_items(trans, inode);
  4419. else
  4420. ret = btrfs_commit_inode_delayed_inode(inode);
  4421. if (ret) {
  4422. btrfs_free_path(path);
  4423. btrfs_free_path(dst_path);
  4424. return ret;
  4425. }
  4426. if (inode_only == LOG_OTHER_INODE) {
  4427. inode_only = LOG_INODE_EXISTS;
  4428. mutex_lock_nested(&inode->log_mutex, SINGLE_DEPTH_NESTING);
  4429. } else {
  4430. mutex_lock(&inode->log_mutex);
  4431. }
  4432. /*
  4433. * a brute force approach to making sure we get the most uptodate
  4434. * copies of everything.
  4435. */
  4436. if (S_ISDIR(inode->vfs_inode.i_mode)) {
  4437. int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
  4438. if (inode_only == LOG_INODE_EXISTS)
  4439. max_key_type = BTRFS_XATTR_ITEM_KEY;
  4440. ret = drop_objectid_items(trans, log, path, ino, max_key_type);
  4441. } else {
  4442. if (inode_only == LOG_INODE_EXISTS) {
  4443. /*
  4444. * Make sure the new inode item we write to the log has
  4445. * the same isize as the current one (if it exists).
  4446. * This is necessary to prevent data loss after log
  4447. * replay, and also to prevent doing a wrong expanding
  4448. * truncate - for e.g. create file, write 4K into offset
  4449. * 0, fsync, write 4K into offset 4096, add hard link,
  4450. * fsync some other file (to sync log), power fail - if
  4451. * we use the inode's current i_size, after log replay
  4452. * we get a 8Kb file, with the last 4Kb extent as a hole
  4453. * (zeroes), as if an expanding truncate happened,
  4454. * instead of getting a file of 4Kb only.
  4455. */
  4456. err = logged_inode_size(log, inode, path, &logged_isize);
  4457. if (err)
  4458. goto out_unlock;
  4459. }
  4460. if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  4461. &inode->runtime_flags)) {
  4462. if (inode_only == LOG_INODE_EXISTS) {
  4463. max_key.type = BTRFS_XATTR_ITEM_KEY;
  4464. ret = drop_objectid_items(trans, log, path, ino,
  4465. max_key.type);
  4466. } else {
  4467. clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  4468. &inode->runtime_flags);
  4469. clear_bit(BTRFS_INODE_COPY_EVERYTHING,
  4470. &inode->runtime_flags);
  4471. while(1) {
  4472. ret = btrfs_truncate_inode_items(trans,
  4473. log, &inode->vfs_inode, 0, 0);
  4474. if (ret != -EAGAIN)
  4475. break;
  4476. }
  4477. }
  4478. } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
  4479. &inode->runtime_flags) ||
  4480. inode_only == LOG_INODE_EXISTS) {
  4481. if (inode_only == LOG_INODE_ALL)
  4482. fast_search = true;
  4483. max_key.type = BTRFS_XATTR_ITEM_KEY;
  4484. ret = drop_objectid_items(trans, log, path, ino,
  4485. max_key.type);
  4486. } else {
  4487. if (inode_only == LOG_INODE_ALL)
  4488. fast_search = true;
  4489. goto log_extents;
  4490. }
  4491. }
  4492. if (ret) {
  4493. err = ret;
  4494. goto out_unlock;
  4495. }
  4496. while (1) {
  4497. ins_nr = 0;
  4498. ret = btrfs_search_forward(root, &min_key,
  4499. path, trans->transid);
  4500. if (ret < 0) {
  4501. err = ret;
  4502. goto out_unlock;
  4503. }
  4504. if (ret != 0)
  4505. break;
  4506. again:
  4507. /* note, ins_nr might be > 0 here, cleanup outside the loop */
  4508. if (min_key.objectid != ino)
  4509. break;
  4510. if (min_key.type > max_key.type)
  4511. break;
  4512. if (min_key.type == BTRFS_INODE_ITEM_KEY)
  4513. need_log_inode_item = false;
  4514. if ((min_key.type == BTRFS_INODE_REF_KEY ||
  4515. min_key.type == BTRFS_INODE_EXTREF_KEY) &&
  4516. inode->generation == trans->transid) {
  4517. u64 other_ino = 0;
  4518. ret = btrfs_check_ref_name_override(path->nodes[0],
  4519. path->slots[0], &min_key, inode,
  4520. &other_ino);
  4521. if (ret < 0) {
  4522. err = ret;
  4523. goto out_unlock;
  4524. } else if (ret > 0 && ctx &&
  4525. other_ino != btrfs_ino(BTRFS_I(ctx->inode))) {
  4526. struct btrfs_key inode_key;
  4527. struct inode *other_inode;
  4528. if (ins_nr > 0) {
  4529. ins_nr++;
  4530. } else {
  4531. ins_nr = 1;
  4532. ins_start_slot = path->slots[0];
  4533. }
  4534. ret = copy_items(trans, inode, dst_path, path,
  4535. &last_extent, ins_start_slot,
  4536. ins_nr, inode_only,
  4537. logged_isize);
  4538. if (ret < 0) {
  4539. err = ret;
  4540. goto out_unlock;
  4541. }
  4542. ins_nr = 0;
  4543. btrfs_release_path(path);
  4544. inode_key.objectid = other_ino;
  4545. inode_key.type = BTRFS_INODE_ITEM_KEY;
  4546. inode_key.offset = 0;
  4547. other_inode = btrfs_iget(fs_info->sb,
  4548. &inode_key, root,
  4549. NULL);
  4550. /*
  4551. * If the other inode that had a conflicting dir
  4552. * entry was deleted in the current transaction,
  4553. * we don't need to do more work nor fallback to
  4554. * a transaction commit.
  4555. */
  4556. if (IS_ERR(other_inode) &&
  4557. PTR_ERR(other_inode) == -ENOENT) {
  4558. goto next_key;
  4559. } else if (IS_ERR(other_inode)) {
  4560. err = PTR_ERR(other_inode);
  4561. goto out_unlock;
  4562. }
  4563. /*
  4564. * We are safe logging the other inode without
  4565. * acquiring its i_mutex as long as we log with
  4566. * the LOG_INODE_EXISTS mode. We're safe against
  4567. * concurrent renames of the other inode as well
  4568. * because during a rename we pin the log and
  4569. * update the log with the new name before we
  4570. * unpin it.
  4571. */
  4572. err = btrfs_log_inode(trans, root,
  4573. BTRFS_I(other_inode),
  4574. LOG_OTHER_INODE, 0, LLONG_MAX,
  4575. ctx);
  4576. iput(other_inode);
  4577. if (err)
  4578. goto out_unlock;
  4579. else
  4580. goto next_key;
  4581. }
  4582. }
  4583. /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
  4584. if (min_key.type == BTRFS_XATTR_ITEM_KEY) {
  4585. if (ins_nr == 0)
  4586. goto next_slot;
  4587. ret = copy_items(trans, inode, dst_path, path,
  4588. &last_extent, ins_start_slot,
  4589. ins_nr, inode_only, logged_isize);
  4590. if (ret < 0) {
  4591. err = ret;
  4592. goto out_unlock;
  4593. }
  4594. ins_nr = 0;
  4595. if (ret) {
  4596. btrfs_release_path(path);
  4597. continue;
  4598. }
  4599. goto next_slot;
  4600. }
  4601. if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
  4602. ins_nr++;
  4603. goto next_slot;
  4604. } else if (!ins_nr) {
  4605. ins_start_slot = path->slots[0];
  4606. ins_nr = 1;
  4607. goto next_slot;
  4608. }
  4609. ret = copy_items(trans, inode, dst_path, path, &last_extent,
  4610. ins_start_slot, ins_nr, inode_only,
  4611. logged_isize);
  4612. if (ret < 0) {
  4613. err = ret;
  4614. goto out_unlock;
  4615. }
  4616. if (ret) {
  4617. ins_nr = 0;
  4618. btrfs_release_path(path);
  4619. continue;
  4620. }
  4621. ins_nr = 1;
  4622. ins_start_slot = path->slots[0];
  4623. next_slot:
  4624. nritems = btrfs_header_nritems(path->nodes[0]);
  4625. path->slots[0]++;
  4626. if (path->slots[0] < nritems) {
  4627. btrfs_item_key_to_cpu(path->nodes[0], &min_key,
  4628. path->slots[0]);
  4629. goto again;
  4630. }
  4631. if (ins_nr) {
  4632. ret = copy_items(trans, inode, dst_path, path,
  4633. &last_extent, ins_start_slot,
  4634. ins_nr, inode_only, logged_isize);
  4635. if (ret < 0) {
  4636. err = ret;
  4637. goto out_unlock;
  4638. }
  4639. ret = 0;
  4640. ins_nr = 0;
  4641. }
  4642. btrfs_release_path(path);
  4643. next_key:
  4644. if (min_key.offset < (u64)-1) {
  4645. min_key.offset++;
  4646. } else if (min_key.type < max_key.type) {
  4647. min_key.type++;
  4648. min_key.offset = 0;
  4649. } else {
  4650. break;
  4651. }
  4652. }
  4653. if (ins_nr) {
  4654. ret = copy_items(trans, inode, dst_path, path, &last_extent,
  4655. ins_start_slot, ins_nr, inode_only,
  4656. logged_isize);
  4657. if (ret < 0) {
  4658. err = ret;
  4659. goto out_unlock;
  4660. }
  4661. ret = 0;
  4662. ins_nr = 0;
  4663. }
  4664. btrfs_release_path(path);
  4665. btrfs_release_path(dst_path);
  4666. err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
  4667. if (err)
  4668. goto out_unlock;
  4669. xattrs_logged = true;
  4670. if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
  4671. btrfs_release_path(path);
  4672. btrfs_release_path(dst_path);
  4673. err = btrfs_log_trailing_hole(trans, root, inode, path);
  4674. if (err)
  4675. goto out_unlock;
  4676. }
  4677. log_extents:
  4678. btrfs_release_path(path);
  4679. btrfs_release_path(dst_path);
  4680. if (need_log_inode_item) {
  4681. err = log_inode_item(trans, log, dst_path, inode);
  4682. if (!err && !xattrs_logged) {
  4683. err = btrfs_log_all_xattrs(trans, root, inode, path,
  4684. dst_path);
  4685. btrfs_release_path(path);
  4686. }
  4687. if (err)
  4688. goto out_unlock;
  4689. }
  4690. if (fast_search) {
  4691. ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
  4692. &logged_list, ctx, start, end);
  4693. if (ret) {
  4694. err = ret;
  4695. goto out_unlock;
  4696. }
  4697. } else if (inode_only == LOG_INODE_ALL) {
  4698. struct extent_map *em, *n;
  4699. write_lock(&em_tree->lock);
  4700. /*
  4701. * We can't just remove every em if we're called for a ranged
  4702. * fsync - that is, one that doesn't cover the whole possible
  4703. * file range (0 to LLONG_MAX). This is because we can have
  4704. * em's that fall outside the range we're logging and therefore
  4705. * their ordered operations haven't completed yet
  4706. * (btrfs_finish_ordered_io() not invoked yet). This means we
  4707. * didn't get their respective file extent item in the fs/subvol
  4708. * tree yet, and need to let the next fast fsync (one which
  4709. * consults the list of modified extent maps) find the em so
  4710. * that it logs a matching file extent item and waits for the
  4711. * respective ordered operation to complete (if it's still
  4712. * running).
  4713. *
  4714. * Removing every em outside the range we're logging would make
  4715. * the next fast fsync not log their matching file extent items,
  4716. * therefore making us lose data after a log replay.
  4717. */
  4718. list_for_each_entry_safe(em, n, &em_tree->modified_extents,
  4719. list) {
  4720. const u64 mod_end = em->mod_start + em->mod_len - 1;
  4721. if (em->mod_start >= start && mod_end <= end)
  4722. list_del_init(&em->list);
  4723. }
  4724. write_unlock(&em_tree->lock);
  4725. }
  4726. if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->vfs_inode.i_mode)) {
  4727. ret = log_directory_changes(trans, root, inode, path, dst_path,
  4728. ctx);
  4729. if (ret) {
  4730. err = ret;
  4731. goto out_unlock;
  4732. }
  4733. }
  4734. spin_lock(&inode->lock);
  4735. inode->logged_trans = trans->transid;
  4736. inode->last_log_commit = inode->last_sub_trans;
  4737. spin_unlock(&inode->lock);
  4738. out_unlock:
  4739. if (unlikely(err))
  4740. btrfs_put_logged_extents(&logged_list);
  4741. else
  4742. btrfs_submit_logged_extents(&logged_list, log);
  4743. mutex_unlock(&inode->log_mutex);
  4744. btrfs_free_path(path);
  4745. btrfs_free_path(dst_path);
  4746. return err;
  4747. }
  4748. /*
  4749. * Check if we must fallback to a transaction commit when logging an inode.
  4750. * This must be called after logging the inode and is used only in the context
  4751. * when fsyncing an inode requires the need to log some other inode - in which
  4752. * case we can't lock the i_mutex of each other inode we need to log as that
  4753. * can lead to deadlocks with concurrent fsync against other inodes (as we can
  4754. * log inodes up or down in the hierarchy) or rename operations for example. So
  4755. * we take the log_mutex of the inode after we have logged it and then check for
  4756. * its last_unlink_trans value - this is safe because any task setting
  4757. * last_unlink_trans must take the log_mutex and it must do this before it does
  4758. * the actual unlink operation, so if we do this check before a concurrent task
  4759. * sets last_unlink_trans it means we've logged a consistent version/state of
  4760. * all the inode items, otherwise we are not sure and must do a transaction
  4761. * commit (the concurrent task might have only updated last_unlink_trans before
  4762. * we logged the inode or it might have also done the unlink).
  4763. */
  4764. static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
  4765. struct btrfs_inode *inode)
  4766. {
  4767. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  4768. bool ret = false;
  4769. mutex_lock(&inode->log_mutex);
  4770. if (inode->last_unlink_trans > fs_info->last_trans_committed) {
  4771. /*
  4772. * Make sure any commits to the log are forced to be full
  4773. * commits.
  4774. */
  4775. btrfs_set_log_full_commit(fs_info, trans);
  4776. ret = true;
  4777. }
  4778. mutex_unlock(&inode->log_mutex);
  4779. return ret;
  4780. }
  4781. /*
  4782. * follow the dentry parent pointers up the chain and see if any
  4783. * of the directories in it require a full commit before they can
  4784. * be logged. Returns zero if nothing special needs to be done or 1 if
  4785. * a full commit is required.
  4786. */
  4787. static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
  4788. struct btrfs_inode *inode,
  4789. struct dentry *parent,
  4790. struct super_block *sb,
  4791. u64 last_committed)
  4792. {
  4793. int ret = 0;
  4794. struct dentry *old_parent = NULL;
  4795. struct btrfs_inode *orig_inode = inode;
  4796. /*
  4797. * for regular files, if its inode is already on disk, we don't
  4798. * have to worry about the parents at all. This is because
  4799. * we can use the last_unlink_trans field to record renames
  4800. * and other fun in this file.
  4801. */
  4802. if (S_ISREG(inode->vfs_inode.i_mode) &&
  4803. inode->generation <= last_committed &&
  4804. inode->last_unlink_trans <= last_committed)
  4805. goto out;
  4806. if (!S_ISDIR(inode->vfs_inode.i_mode)) {
  4807. if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
  4808. goto out;
  4809. inode = BTRFS_I(d_inode(parent));
  4810. }
  4811. while (1) {
  4812. /*
  4813. * If we are logging a directory then we start with our inode,
  4814. * not our parent's inode, so we need to skip setting the
  4815. * logged_trans so that further down in the log code we don't
  4816. * think this inode has already been logged.
  4817. */
  4818. if (inode != orig_inode)
  4819. inode->logged_trans = trans->transid;
  4820. smp_mb();
  4821. if (btrfs_must_commit_transaction(trans, inode)) {
  4822. ret = 1;
  4823. break;
  4824. }
  4825. if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
  4826. break;
  4827. if (IS_ROOT(parent)) {
  4828. inode = BTRFS_I(d_inode(parent));
  4829. if (btrfs_must_commit_transaction(trans, inode))
  4830. ret = 1;
  4831. break;
  4832. }
  4833. parent = dget_parent(parent);
  4834. dput(old_parent);
  4835. old_parent = parent;
  4836. inode = BTRFS_I(d_inode(parent));
  4837. }
  4838. dput(old_parent);
  4839. out:
  4840. return ret;
  4841. }
  4842. struct btrfs_dir_list {
  4843. u64 ino;
  4844. struct list_head list;
  4845. };
  4846. /*
  4847. * Log the inodes of the new dentries of a directory. See log_dir_items() for
  4848. * details about the why it is needed.
  4849. * This is a recursive operation - if an existing dentry corresponds to a
  4850. * directory, that directory's new entries are logged too (same behaviour as
  4851. * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
  4852. * the dentries point to we do not lock their i_mutex, otherwise lockdep
  4853. * complains about the following circular lock dependency / possible deadlock:
  4854. *
  4855. * CPU0 CPU1
  4856. * ---- ----
  4857. * lock(&type->i_mutex_dir_key#3/2);
  4858. * lock(sb_internal#2);
  4859. * lock(&type->i_mutex_dir_key#3/2);
  4860. * lock(&sb->s_type->i_mutex_key#14);
  4861. *
  4862. * Where sb_internal is the lock (a counter that works as a lock) acquired by
  4863. * sb_start_intwrite() in btrfs_start_transaction().
  4864. * Not locking i_mutex of the inodes is still safe because:
  4865. *
  4866. * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
  4867. * that while logging the inode new references (names) are added or removed
  4868. * from the inode, leaving the logged inode item with a link count that does
  4869. * not match the number of logged inode reference items. This is fine because
  4870. * at log replay time we compute the real number of links and correct the
  4871. * link count in the inode item (see replay_one_buffer() and
  4872. * link_to_fixup_dir());
  4873. *
  4874. * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
  4875. * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
  4876. * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
  4877. * has a size that doesn't match the sum of the lengths of all the logged
  4878. * names. This does not result in a problem because if a dir_item key is
  4879. * logged but its matching dir_index key is not logged, at log replay time we
  4880. * don't use it to replay the respective name (see replay_one_name()). On the
  4881. * other hand if only the dir_index key ends up being logged, the respective
  4882. * name is added to the fs/subvol tree with both the dir_item and dir_index
  4883. * keys created (see replay_one_name()).
  4884. * The directory's inode item with a wrong i_size is not a problem as well,
  4885. * since we don't use it at log replay time to set the i_size in the inode
  4886. * item of the fs/subvol tree (see overwrite_item()).
  4887. */
  4888. static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
  4889. struct btrfs_root *root,
  4890. struct btrfs_inode *start_inode,
  4891. struct btrfs_log_ctx *ctx)
  4892. {
  4893. struct btrfs_fs_info *fs_info = root->fs_info;
  4894. struct btrfs_root *log = root->log_root;
  4895. struct btrfs_path *path;
  4896. LIST_HEAD(dir_list);
  4897. struct btrfs_dir_list *dir_elem;
  4898. int ret = 0;
  4899. path = btrfs_alloc_path();
  4900. if (!path)
  4901. return -ENOMEM;
  4902. dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
  4903. if (!dir_elem) {
  4904. btrfs_free_path(path);
  4905. return -ENOMEM;
  4906. }
  4907. dir_elem->ino = btrfs_ino(start_inode);
  4908. list_add_tail(&dir_elem->list, &dir_list);
  4909. while (!list_empty(&dir_list)) {
  4910. struct extent_buffer *leaf;
  4911. struct btrfs_key min_key;
  4912. int nritems;
  4913. int i;
  4914. dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
  4915. list);
  4916. if (ret)
  4917. goto next_dir_inode;
  4918. min_key.objectid = dir_elem->ino;
  4919. min_key.type = BTRFS_DIR_ITEM_KEY;
  4920. min_key.offset = 0;
  4921. again:
  4922. btrfs_release_path(path);
  4923. ret = btrfs_search_forward(log, &min_key, path, trans->transid);
  4924. if (ret < 0) {
  4925. goto next_dir_inode;
  4926. } else if (ret > 0) {
  4927. ret = 0;
  4928. goto next_dir_inode;
  4929. }
  4930. process_leaf:
  4931. leaf = path->nodes[0];
  4932. nritems = btrfs_header_nritems(leaf);
  4933. for (i = path->slots[0]; i < nritems; i++) {
  4934. struct btrfs_dir_item *di;
  4935. struct btrfs_key di_key;
  4936. struct inode *di_inode;
  4937. struct btrfs_dir_list *new_dir_elem;
  4938. int log_mode = LOG_INODE_EXISTS;
  4939. int type;
  4940. btrfs_item_key_to_cpu(leaf, &min_key, i);
  4941. if (min_key.objectid != dir_elem->ino ||
  4942. min_key.type != BTRFS_DIR_ITEM_KEY)
  4943. goto next_dir_inode;
  4944. di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
  4945. type = btrfs_dir_type(leaf, di);
  4946. if (btrfs_dir_transid(leaf, di) < trans->transid &&
  4947. type != BTRFS_FT_DIR)
  4948. continue;
  4949. btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
  4950. if (di_key.type == BTRFS_ROOT_ITEM_KEY)
  4951. continue;
  4952. btrfs_release_path(path);
  4953. di_inode = btrfs_iget(fs_info->sb, &di_key, root, NULL);
  4954. if (IS_ERR(di_inode)) {
  4955. ret = PTR_ERR(di_inode);
  4956. goto next_dir_inode;
  4957. }
  4958. if (btrfs_inode_in_log(BTRFS_I(di_inode), trans->transid)) {
  4959. iput(di_inode);
  4960. break;
  4961. }
  4962. ctx->log_new_dentries = false;
  4963. if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
  4964. log_mode = LOG_INODE_ALL;
  4965. ret = btrfs_log_inode(trans, root, BTRFS_I(di_inode),
  4966. log_mode, 0, LLONG_MAX, ctx);
  4967. if (!ret &&
  4968. btrfs_must_commit_transaction(trans, BTRFS_I(di_inode)))
  4969. ret = 1;
  4970. iput(di_inode);
  4971. if (ret)
  4972. goto next_dir_inode;
  4973. if (ctx->log_new_dentries) {
  4974. new_dir_elem = kmalloc(sizeof(*new_dir_elem),
  4975. GFP_NOFS);
  4976. if (!new_dir_elem) {
  4977. ret = -ENOMEM;
  4978. goto next_dir_inode;
  4979. }
  4980. new_dir_elem->ino = di_key.objectid;
  4981. list_add_tail(&new_dir_elem->list, &dir_list);
  4982. }
  4983. break;
  4984. }
  4985. if (i == nritems) {
  4986. ret = btrfs_next_leaf(log, path);
  4987. if (ret < 0) {
  4988. goto next_dir_inode;
  4989. } else if (ret > 0) {
  4990. ret = 0;
  4991. goto next_dir_inode;
  4992. }
  4993. goto process_leaf;
  4994. }
  4995. if (min_key.offset < (u64)-1) {
  4996. min_key.offset++;
  4997. goto again;
  4998. }
  4999. next_dir_inode:
  5000. list_del(&dir_elem->list);
  5001. kfree(dir_elem);
  5002. }
  5003. btrfs_free_path(path);
  5004. return ret;
  5005. }
  5006. static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
  5007. struct btrfs_inode *inode,
  5008. struct btrfs_log_ctx *ctx)
  5009. {
  5010. struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
  5011. int ret;
  5012. struct btrfs_path *path;
  5013. struct btrfs_key key;
  5014. struct btrfs_root *root = inode->root;
  5015. const u64 ino = btrfs_ino(inode);
  5016. path = btrfs_alloc_path();
  5017. if (!path)
  5018. return -ENOMEM;
  5019. path->skip_locking = 1;
  5020. path->search_commit_root = 1;
  5021. key.objectid = ino;
  5022. key.type = BTRFS_INODE_REF_KEY;
  5023. key.offset = 0;
  5024. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  5025. if (ret < 0)
  5026. goto out;
  5027. while (true) {
  5028. struct extent_buffer *leaf = path->nodes[0];
  5029. int slot = path->slots[0];
  5030. u32 cur_offset = 0;
  5031. u32 item_size;
  5032. unsigned long ptr;
  5033. if (slot >= btrfs_header_nritems(leaf)) {
  5034. ret = btrfs_next_leaf(root, path);
  5035. if (ret < 0)
  5036. goto out;
  5037. else if (ret > 0)
  5038. break;
  5039. continue;
  5040. }
  5041. btrfs_item_key_to_cpu(leaf, &key, slot);
  5042. /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
  5043. if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
  5044. break;
  5045. item_size = btrfs_item_size_nr(leaf, slot);
  5046. ptr = btrfs_item_ptr_offset(leaf, slot);
  5047. while (cur_offset < item_size) {
  5048. struct btrfs_key inode_key;
  5049. struct inode *dir_inode;
  5050. inode_key.type = BTRFS_INODE_ITEM_KEY;
  5051. inode_key.offset = 0;
  5052. if (key.type == BTRFS_INODE_EXTREF_KEY) {
  5053. struct btrfs_inode_extref *extref;
  5054. extref = (struct btrfs_inode_extref *)
  5055. (ptr + cur_offset);
  5056. inode_key.objectid = btrfs_inode_extref_parent(
  5057. leaf, extref);
  5058. cur_offset += sizeof(*extref);
  5059. cur_offset += btrfs_inode_extref_name_len(leaf,
  5060. extref);
  5061. } else {
  5062. inode_key.objectid = key.offset;
  5063. cur_offset = item_size;
  5064. }
  5065. dir_inode = btrfs_iget(fs_info->sb, &inode_key,
  5066. root, NULL);
  5067. /* If parent inode was deleted, skip it. */
  5068. if (IS_ERR(dir_inode))
  5069. continue;
  5070. if (ctx)
  5071. ctx->log_new_dentries = false;
  5072. ret = btrfs_log_inode(trans, root, BTRFS_I(dir_inode),
  5073. LOG_INODE_ALL, 0, LLONG_MAX, ctx);
  5074. if (!ret &&
  5075. btrfs_must_commit_transaction(trans, BTRFS_I(dir_inode)))
  5076. ret = 1;
  5077. if (!ret && ctx && ctx->log_new_dentries)
  5078. ret = log_new_dir_dentries(trans, root,
  5079. BTRFS_I(dir_inode), ctx);
  5080. iput(dir_inode);
  5081. if (ret)
  5082. goto out;
  5083. }
  5084. path->slots[0]++;
  5085. }
  5086. ret = 0;
  5087. out:
  5088. btrfs_free_path(path);
  5089. return ret;
  5090. }
  5091. /*
  5092. * helper function around btrfs_log_inode to make sure newly created
  5093. * parent directories also end up in the log. A minimal inode and backref
  5094. * only logging is done of any parent directories that are older than
  5095. * the last committed transaction
  5096. */
  5097. static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
  5098. struct btrfs_inode *inode,
  5099. struct dentry *parent,
  5100. const loff_t start,
  5101. const loff_t end,
  5102. int inode_only,
  5103. struct btrfs_log_ctx *ctx)
  5104. {
  5105. struct btrfs_root *root = inode->root;
  5106. struct btrfs_fs_info *fs_info = root->fs_info;
  5107. struct super_block *sb;
  5108. struct dentry *old_parent = NULL;
  5109. int ret = 0;
  5110. u64 last_committed = fs_info->last_trans_committed;
  5111. bool log_dentries = false;
  5112. struct btrfs_inode *orig_inode = inode;
  5113. sb = inode->vfs_inode.i_sb;
  5114. if (btrfs_test_opt(fs_info, NOTREELOG)) {
  5115. ret = 1;
  5116. goto end_no_trans;
  5117. }
  5118. /*
  5119. * The prev transaction commit doesn't complete, we need do
  5120. * full commit by ourselves.
  5121. */
  5122. if (fs_info->last_trans_log_full_commit >
  5123. fs_info->last_trans_committed) {
  5124. ret = 1;
  5125. goto end_no_trans;
  5126. }
  5127. if (btrfs_root_refs(&root->root_item) == 0) {
  5128. ret = 1;
  5129. goto end_no_trans;
  5130. }
  5131. ret = check_parent_dirs_for_sync(trans, inode, parent, sb,
  5132. last_committed);
  5133. if (ret)
  5134. goto end_no_trans;
  5135. if (btrfs_inode_in_log(inode, trans->transid)) {
  5136. ret = BTRFS_NO_LOG_SYNC;
  5137. goto end_no_trans;
  5138. }
  5139. ret = start_log_trans(trans, root, ctx);
  5140. if (ret)
  5141. goto end_no_trans;
  5142. ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx);
  5143. if (ret)
  5144. goto end_trans;
  5145. /*
  5146. * for regular files, if its inode is already on disk, we don't
  5147. * have to worry about the parents at all. This is because
  5148. * we can use the last_unlink_trans field to record renames
  5149. * and other fun in this file.
  5150. */
  5151. if (S_ISREG(inode->vfs_inode.i_mode) &&
  5152. inode->generation <= last_committed &&
  5153. inode->last_unlink_trans <= last_committed) {
  5154. ret = 0;
  5155. goto end_trans;
  5156. }
  5157. if (S_ISDIR(inode->vfs_inode.i_mode) && ctx && ctx->log_new_dentries)
  5158. log_dentries = true;
  5159. /*
  5160. * On unlink we must make sure all our current and old parent directory
  5161. * inodes are fully logged. This is to prevent leaving dangling
  5162. * directory index entries in directories that were our parents but are
  5163. * not anymore. Not doing this results in old parent directory being
  5164. * impossible to delete after log replay (rmdir will always fail with
  5165. * error -ENOTEMPTY).
  5166. *
  5167. * Example 1:
  5168. *
  5169. * mkdir testdir
  5170. * touch testdir/foo
  5171. * ln testdir/foo testdir/bar
  5172. * sync
  5173. * unlink testdir/bar
  5174. * xfs_io -c fsync testdir/foo
  5175. * <power failure>
  5176. * mount fs, triggers log replay
  5177. *
  5178. * If we don't log the parent directory (testdir), after log replay the
  5179. * directory still has an entry pointing to the file inode using the bar
  5180. * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
  5181. * the file inode has a link count of 1.
  5182. *
  5183. * Example 2:
  5184. *
  5185. * mkdir testdir
  5186. * touch foo
  5187. * ln foo testdir/foo2
  5188. * ln foo testdir/foo3
  5189. * sync
  5190. * unlink testdir/foo3
  5191. * xfs_io -c fsync foo
  5192. * <power failure>
  5193. * mount fs, triggers log replay
  5194. *
  5195. * Similar as the first example, after log replay the parent directory
  5196. * testdir still has an entry pointing to the inode file with name foo3
  5197. * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
  5198. * and has a link count of 2.
  5199. */
  5200. if (inode->last_unlink_trans > last_committed) {
  5201. ret = btrfs_log_all_parents(trans, orig_inode, ctx);
  5202. if (ret)
  5203. goto end_trans;
  5204. }
  5205. while (1) {
  5206. if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
  5207. break;
  5208. inode = BTRFS_I(d_inode(parent));
  5209. if (root != inode->root)
  5210. break;
  5211. if (inode->generation > last_committed) {
  5212. ret = btrfs_log_inode(trans, root, inode,
  5213. LOG_INODE_EXISTS, 0, LLONG_MAX, ctx);
  5214. if (ret)
  5215. goto end_trans;
  5216. }
  5217. if (IS_ROOT(parent))
  5218. break;
  5219. parent = dget_parent(parent);
  5220. dput(old_parent);
  5221. old_parent = parent;
  5222. }
  5223. if (log_dentries)
  5224. ret = log_new_dir_dentries(trans, root, orig_inode, ctx);
  5225. else
  5226. ret = 0;
  5227. end_trans:
  5228. dput(old_parent);
  5229. if (ret < 0) {
  5230. btrfs_set_log_full_commit(fs_info, trans);
  5231. ret = 1;
  5232. }
  5233. if (ret)
  5234. btrfs_remove_log_ctx(root, ctx);
  5235. btrfs_end_log_trans(root);
  5236. end_no_trans:
  5237. return ret;
  5238. }
  5239. /*
  5240. * it is not safe to log dentry if the chunk root has added new
  5241. * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
  5242. * If this returns 1, you must commit the transaction to safely get your
  5243. * data on disk.
  5244. */
  5245. int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
  5246. struct dentry *dentry,
  5247. const loff_t start,
  5248. const loff_t end,
  5249. struct btrfs_log_ctx *ctx)
  5250. {
  5251. struct dentry *parent = dget_parent(dentry);
  5252. int ret;
  5253. ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent,
  5254. start, end, LOG_INODE_ALL, ctx);
  5255. dput(parent);
  5256. return ret;
  5257. }
  5258. /*
  5259. * should be called during mount to recover any replay any log trees
  5260. * from the FS
  5261. */
  5262. int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
  5263. {
  5264. int ret;
  5265. struct btrfs_path *path;
  5266. struct btrfs_trans_handle *trans;
  5267. struct btrfs_key key;
  5268. struct btrfs_key found_key;
  5269. struct btrfs_key tmp_key;
  5270. struct btrfs_root *log;
  5271. struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
  5272. struct walk_control wc = {
  5273. .process_func = process_one_buffer,
  5274. .stage = 0,
  5275. };
  5276. path = btrfs_alloc_path();
  5277. if (!path)
  5278. return -ENOMEM;
  5279. set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
  5280. trans = btrfs_start_transaction(fs_info->tree_root, 0);
  5281. if (IS_ERR(trans)) {
  5282. ret = PTR_ERR(trans);
  5283. goto error;
  5284. }
  5285. wc.trans = trans;
  5286. wc.pin = 1;
  5287. ret = walk_log_tree(trans, log_root_tree, &wc);
  5288. if (ret) {
  5289. btrfs_handle_fs_error(fs_info, ret,
  5290. "Failed to pin buffers while recovering log root tree.");
  5291. goto error;
  5292. }
  5293. again:
  5294. key.objectid = BTRFS_TREE_LOG_OBJECTID;
  5295. key.offset = (u64)-1;
  5296. key.type = BTRFS_ROOT_ITEM_KEY;
  5297. while (1) {
  5298. ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
  5299. if (ret < 0) {
  5300. btrfs_handle_fs_error(fs_info, ret,
  5301. "Couldn't find tree log root.");
  5302. goto error;
  5303. }
  5304. if (ret > 0) {
  5305. if (path->slots[0] == 0)
  5306. break;
  5307. path->slots[0]--;
  5308. }
  5309. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  5310. path->slots[0]);
  5311. btrfs_release_path(path);
  5312. if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  5313. break;
  5314. log = btrfs_read_fs_root(log_root_tree, &found_key);
  5315. if (IS_ERR(log)) {
  5316. ret = PTR_ERR(log);
  5317. btrfs_handle_fs_error(fs_info, ret,
  5318. "Couldn't read tree log root.");
  5319. goto error;
  5320. }
  5321. tmp_key.objectid = found_key.offset;
  5322. tmp_key.type = BTRFS_ROOT_ITEM_KEY;
  5323. tmp_key.offset = (u64)-1;
  5324. wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
  5325. if (IS_ERR(wc.replay_dest)) {
  5326. ret = PTR_ERR(wc.replay_dest);
  5327. free_extent_buffer(log->node);
  5328. free_extent_buffer(log->commit_root);
  5329. kfree(log);
  5330. btrfs_handle_fs_error(fs_info, ret,
  5331. "Couldn't read target root for tree log recovery.");
  5332. goto error;
  5333. }
  5334. wc.replay_dest->log_root = log;
  5335. btrfs_record_root_in_trans(trans, wc.replay_dest);
  5336. ret = walk_log_tree(trans, log, &wc);
  5337. if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
  5338. ret = fixup_inode_link_counts(trans, wc.replay_dest,
  5339. path);
  5340. }
  5341. if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
  5342. struct btrfs_root *root = wc.replay_dest;
  5343. btrfs_release_path(path);
  5344. /*
  5345. * We have just replayed everything, and the highest
  5346. * objectid of fs roots probably has changed in case
  5347. * some inode_item's got replayed.
  5348. *
  5349. * root->objectid_mutex is not acquired as log replay
  5350. * could only happen during mount.
  5351. */
  5352. ret = btrfs_find_highest_objectid(root,
  5353. &root->highest_objectid);
  5354. }
  5355. key.offset = found_key.offset - 1;
  5356. wc.replay_dest->log_root = NULL;
  5357. free_extent_buffer(log->node);
  5358. free_extent_buffer(log->commit_root);
  5359. kfree(log);
  5360. if (ret)
  5361. goto error;
  5362. if (found_key.offset == 0)
  5363. break;
  5364. }
  5365. btrfs_release_path(path);
  5366. /* step one is to pin it all, step two is to replay just inodes */
  5367. if (wc.pin) {
  5368. wc.pin = 0;
  5369. wc.process_func = replay_one_buffer;
  5370. wc.stage = LOG_WALK_REPLAY_INODES;
  5371. goto again;
  5372. }
  5373. /* step three is to replay everything */
  5374. if (wc.stage < LOG_WALK_REPLAY_ALL) {
  5375. wc.stage++;
  5376. goto again;
  5377. }
  5378. btrfs_free_path(path);
  5379. /* step 4: commit the transaction, which also unpins the blocks */
  5380. ret = btrfs_commit_transaction(trans);
  5381. if (ret)
  5382. return ret;
  5383. free_extent_buffer(log_root_tree->node);
  5384. log_root_tree->log_root = NULL;
  5385. clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
  5386. kfree(log_root_tree);
  5387. return 0;
  5388. error:
  5389. if (wc.trans)
  5390. btrfs_end_transaction(wc.trans);
  5391. btrfs_free_path(path);
  5392. return ret;
  5393. }
  5394. /*
  5395. * there are some corner cases where we want to force a full
  5396. * commit instead of allowing a directory to be logged.
  5397. *
  5398. * They revolve around files there were unlinked from the directory, and
  5399. * this function updates the parent directory so that a full commit is
  5400. * properly done if it is fsync'd later after the unlinks are done.
  5401. *
  5402. * Must be called before the unlink operations (updates to the subvolume tree,
  5403. * inodes, etc) are done.
  5404. */
  5405. void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
  5406. struct btrfs_inode *dir, struct btrfs_inode *inode,
  5407. int for_rename)
  5408. {
  5409. /*
  5410. * when we're logging a file, if it hasn't been renamed
  5411. * or unlinked, and its inode is fully committed on disk,
  5412. * we don't have to worry about walking up the directory chain
  5413. * to log its parents.
  5414. *
  5415. * So, we use the last_unlink_trans field to put this transid
  5416. * into the file. When the file is logged we check it and
  5417. * don't log the parents if the file is fully on disk.
  5418. */
  5419. mutex_lock(&inode->log_mutex);
  5420. inode->last_unlink_trans = trans->transid;
  5421. mutex_unlock(&inode->log_mutex);
  5422. /*
  5423. * if this directory was already logged any new
  5424. * names for this file/dir will get recorded
  5425. */
  5426. smp_mb();
  5427. if (dir->logged_trans == trans->transid)
  5428. return;
  5429. /*
  5430. * if the inode we're about to unlink was logged,
  5431. * the log will be properly updated for any new names
  5432. */
  5433. if (inode->logged_trans == trans->transid)
  5434. return;
  5435. /*
  5436. * when renaming files across directories, if the directory
  5437. * there we're unlinking from gets fsync'd later on, there's
  5438. * no way to find the destination directory later and fsync it
  5439. * properly. So, we have to be conservative and force commits
  5440. * so the new name gets discovered.
  5441. */
  5442. if (for_rename)
  5443. goto record;
  5444. /* we can safely do the unlink without any special recording */
  5445. return;
  5446. record:
  5447. mutex_lock(&dir->log_mutex);
  5448. dir->last_unlink_trans = trans->transid;
  5449. mutex_unlock(&dir->log_mutex);
  5450. }
  5451. /*
  5452. * Make sure that if someone attempts to fsync the parent directory of a deleted
  5453. * snapshot, it ends up triggering a transaction commit. This is to guarantee
  5454. * that after replaying the log tree of the parent directory's root we will not
  5455. * see the snapshot anymore and at log replay time we will not see any log tree
  5456. * corresponding to the deleted snapshot's root, which could lead to replaying
  5457. * it after replaying the log tree of the parent directory (which would replay
  5458. * the snapshot delete operation).
  5459. *
  5460. * Must be called before the actual snapshot destroy operation (updates to the
  5461. * parent root and tree of tree roots trees, etc) are done.
  5462. */
  5463. void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
  5464. struct btrfs_inode *dir)
  5465. {
  5466. mutex_lock(&dir->log_mutex);
  5467. dir->last_unlink_trans = trans->transid;
  5468. mutex_unlock(&dir->log_mutex);
  5469. }
  5470. /*
  5471. * Call this after adding a new name for a file and it will properly
  5472. * update the log to reflect the new name.
  5473. *
  5474. * It will return zero if all goes well, and it will return 1 if a
  5475. * full transaction commit is required.
  5476. */
  5477. int btrfs_log_new_name(struct btrfs_trans_handle *trans,
  5478. struct btrfs_inode *inode, struct btrfs_inode *old_dir,
  5479. struct dentry *parent)
  5480. {
  5481. struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
  5482. /*
  5483. * this will force the logging code to walk the dentry chain
  5484. * up for the file
  5485. */
  5486. if (!S_ISDIR(inode->vfs_inode.i_mode))
  5487. inode->last_unlink_trans = trans->transid;
  5488. /*
  5489. * if this inode hasn't been logged and directory we're renaming it
  5490. * from hasn't been logged, we don't need to log it
  5491. */
  5492. if (inode->logged_trans <= fs_info->last_trans_committed &&
  5493. (!old_dir || old_dir->logged_trans <= fs_info->last_trans_committed))
  5494. return 0;
  5495. return btrfs_log_inode_parent(trans, inode, parent, 0, LLONG_MAX,
  5496. LOG_INODE_EXISTS, NULL);
  5497. }