tree-log.c 216 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695
  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 "misc.h"
  11. #include "ctree.h"
  12. #include "tree-log.h"
  13. #include "disk-io.h"
  14. #include "locking.h"
  15. #include "backref.h"
  16. #include "compression.h"
  17. #include "qgroup.h"
  18. #include "block-group.h"
  19. #include "space-info.h"
  20. #include "inode-item.h"
  21. #include "fs.h"
  22. #include "accessors.h"
  23. #include "extent-tree.h"
  24. #include "root-tree.h"
  25. #include "dir-item.h"
  26. #include "file-item.h"
  27. #include "file.h"
  28. #include "orphan.h"
  29. #include "tree-checker.h"
  30. #define MAX_CONFLICT_INODES 10
  31. /* magic values for the inode_only field in btrfs_log_inode:
  32. *
  33. * LOG_INODE_ALL means to log everything
  34. * LOG_INODE_EXISTS means to log just enough to recreate the inode
  35. * during log replay
  36. */
  37. enum {
  38. LOG_INODE_ALL,
  39. LOG_INODE_EXISTS,
  40. };
  41. /*
  42. * directory trouble cases
  43. *
  44. * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
  45. * log, we must force a full commit before doing an fsync of the directory
  46. * where the unlink was done.
  47. * ---> record transid of last unlink/rename per directory
  48. *
  49. * mkdir foo/some_dir
  50. * normal commit
  51. * rename foo/some_dir foo2/some_dir
  52. * mkdir foo/some_dir
  53. * fsync foo/some_dir/some_file
  54. *
  55. * The fsync above will unlink the original some_dir without recording
  56. * it in its new location (foo2). After a crash, some_dir will be gone
  57. * unless the fsync of some_file forces a full commit
  58. *
  59. * 2) we must log any new names for any file or dir that is in the fsync
  60. * log. ---> check inode while renaming/linking.
  61. *
  62. * 2a) we must log any new names for any file or dir during rename
  63. * when the directory they are being removed from was logged.
  64. * ---> check inode and old parent dir during rename
  65. *
  66. * 2a is actually the more important variant. With the extra logging
  67. * a crash might unlink the old name without recreating the new one
  68. *
  69. * 3) after a crash, we must go through any directories with a link count
  70. * of zero and redo the rm -rf
  71. *
  72. * mkdir f1/foo
  73. * normal commit
  74. * rm -rf f1/foo
  75. * fsync(f1)
  76. *
  77. * The directory f1 was fully removed from the FS, but fsync was never
  78. * called on f1, only its parent dir. After a crash the rm -rf must
  79. * be replayed. This must be able to recurse down the entire
  80. * directory tree. The inode link count fixup code takes care of the
  81. * ugly details.
  82. */
  83. /*
  84. * stages for the tree walking. The first
  85. * stage (0) is to only pin down the blocks we find
  86. * the second stage (1) is to make sure that all the inodes
  87. * we find in the log are created in the subvolume.
  88. *
  89. * The last stage is to deal with directories and links and extents
  90. * and all the other fun semantics
  91. */
  92. enum {
  93. LOG_WALK_PIN_ONLY,
  94. LOG_WALK_REPLAY_INODES,
  95. LOG_WALK_REPLAY_DIR_INDEX,
  96. LOG_WALK_REPLAY_ALL,
  97. };
  98. static int btrfs_log_inode(struct btrfs_trans_handle *trans,
  99. struct btrfs_inode *inode,
  100. int inode_only,
  101. struct btrfs_log_ctx *ctx);
  102. static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
  103. struct btrfs_root *root,
  104. struct btrfs_path *path, u64 objectid);
  105. static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
  106. struct btrfs_root *root,
  107. struct btrfs_root *log,
  108. struct btrfs_path *path,
  109. u64 dirid, int del_all);
  110. static void wait_log_commit(struct btrfs_root *root, int transid);
  111. /*
  112. * tree logging is a special write ahead log used to make sure that
  113. * fsyncs and O_SYNCs can happen without doing full tree commits.
  114. *
  115. * Full tree commits are expensive because they require commonly
  116. * modified blocks to be recowed, creating many dirty pages in the
  117. * extent tree an 4x-6x higher write load than ext3.
  118. *
  119. * Instead of doing a tree commit on every fsync, we use the
  120. * key ranges and transaction ids to find items for a given file or directory
  121. * that have changed in this transaction. Those items are copied into
  122. * a special tree (one per subvolume root), that tree is written to disk
  123. * and then the fsync is considered complete.
  124. *
  125. * After a crash, items are copied out of the log-tree back into the
  126. * subvolume tree. Any file data extents found are recorded in the extent
  127. * allocation tree, and the log-tree freed.
  128. *
  129. * The log tree is read three times, once to pin down all the extents it is
  130. * using in ram and once, once to create all the inodes logged in the tree
  131. * and once to do all the other items.
  132. */
  133. static struct btrfs_inode *btrfs_iget_logging(u64 objectid, struct btrfs_root *root)
  134. {
  135. unsigned int nofs_flag;
  136. struct inode *inode;
  137. /* Only meant to be called for subvolume roots and not for log roots. */
  138. ASSERT(is_fstree(btrfs_root_id(root)));
  139. /*
  140. * We're holding a transaction handle whether we are logging or
  141. * replaying a log tree, so we must make sure NOFS semantics apply
  142. * because btrfs_alloc_inode() may be triggered and it uses GFP_KERNEL
  143. * to allocate an inode, which can recurse back into the filesystem and
  144. * attempt a transaction commit, resulting in a deadlock.
  145. */
  146. nofs_flag = memalloc_nofs_save();
  147. inode = btrfs_iget(objectid, root);
  148. memalloc_nofs_restore(nofs_flag);
  149. if (IS_ERR(inode))
  150. return ERR_CAST(inode);
  151. return BTRFS_I(inode);
  152. }
  153. /*
  154. * start a sub transaction and setup the log tree
  155. * this increments the log tree writer count to make the people
  156. * syncing the tree wait for us to finish
  157. */
  158. static int start_log_trans(struct btrfs_trans_handle *trans,
  159. struct btrfs_root *root,
  160. struct btrfs_log_ctx *ctx)
  161. {
  162. struct btrfs_fs_info *fs_info = root->fs_info;
  163. struct btrfs_root *tree_root = fs_info->tree_root;
  164. const bool zoned = btrfs_is_zoned(fs_info);
  165. int ret = 0;
  166. bool created = false;
  167. /*
  168. * First check if the log root tree was already created. If not, create
  169. * it before locking the root's log_mutex, just to keep lockdep happy.
  170. */
  171. if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &tree_root->state)) {
  172. mutex_lock(&tree_root->log_mutex);
  173. if (!fs_info->log_root_tree) {
  174. ret = btrfs_init_log_root_tree(trans, fs_info);
  175. if (!ret) {
  176. set_bit(BTRFS_ROOT_HAS_LOG_TREE, &tree_root->state);
  177. created = true;
  178. }
  179. }
  180. mutex_unlock(&tree_root->log_mutex);
  181. if (ret)
  182. return ret;
  183. }
  184. mutex_lock(&root->log_mutex);
  185. again:
  186. if (root->log_root) {
  187. int index = (root->log_transid + 1) % 2;
  188. if (btrfs_need_log_full_commit(trans)) {
  189. ret = BTRFS_LOG_FORCE_COMMIT;
  190. goto out;
  191. }
  192. if (zoned && atomic_read(&root->log_commit[index])) {
  193. wait_log_commit(root, root->log_transid - 1);
  194. goto again;
  195. }
  196. if (!root->log_start_pid) {
  197. clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
  198. root->log_start_pid = current->pid;
  199. } else if (root->log_start_pid != current->pid) {
  200. set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
  201. }
  202. } else {
  203. /*
  204. * This means fs_info->log_root_tree was already created
  205. * for some other FS trees. Do the full commit not to mix
  206. * nodes from multiple log transactions to do sequential
  207. * writing.
  208. */
  209. if (zoned && !created) {
  210. ret = BTRFS_LOG_FORCE_COMMIT;
  211. goto out;
  212. }
  213. ret = btrfs_add_log_tree(trans, root);
  214. if (ret)
  215. goto out;
  216. set_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state);
  217. clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
  218. root->log_start_pid = current->pid;
  219. }
  220. atomic_inc(&root->log_writers);
  221. if (!ctx->logging_new_name) {
  222. int index = root->log_transid % 2;
  223. list_add_tail(&ctx->list, &root->log_ctxs[index]);
  224. ctx->log_transid = root->log_transid;
  225. }
  226. out:
  227. mutex_unlock(&root->log_mutex);
  228. return ret;
  229. }
  230. /*
  231. * returns 0 if there was a log transaction running and we were able
  232. * to join, or returns -ENOENT if there were not transactions
  233. * in progress
  234. */
  235. static int join_running_log_trans(struct btrfs_root *root)
  236. {
  237. const bool zoned = btrfs_is_zoned(root->fs_info);
  238. int ret = -ENOENT;
  239. if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state))
  240. return ret;
  241. mutex_lock(&root->log_mutex);
  242. again:
  243. if (root->log_root) {
  244. int index = (root->log_transid + 1) % 2;
  245. ret = 0;
  246. if (zoned && atomic_read(&root->log_commit[index])) {
  247. wait_log_commit(root, root->log_transid - 1);
  248. goto again;
  249. }
  250. atomic_inc(&root->log_writers);
  251. }
  252. mutex_unlock(&root->log_mutex);
  253. return ret;
  254. }
  255. /*
  256. * This either makes the current running log transaction wait
  257. * until you call btrfs_end_log_trans() or it makes any future
  258. * log transactions wait until you call btrfs_end_log_trans()
  259. */
  260. void btrfs_pin_log_trans(struct btrfs_root *root)
  261. {
  262. atomic_inc(&root->log_writers);
  263. }
  264. /*
  265. * indicate we're done making changes to the log tree
  266. * and wake up anyone waiting to do a sync
  267. */
  268. void btrfs_end_log_trans(struct btrfs_root *root)
  269. {
  270. if (atomic_dec_and_test(&root->log_writers)) {
  271. /* atomic_dec_and_test implies a barrier */
  272. cond_wake_up_nomb(&root->log_writer_wait);
  273. }
  274. }
  275. /*
  276. * the walk control struct is used to pass state down the chain when
  277. * processing the log tree. The stage field tells us which part
  278. * of the log tree processing we are currently doing. The others
  279. * are state fields used for that specific part
  280. */
  281. struct walk_control {
  282. /* should we free the extent on disk when done? This is used
  283. * at transaction commit time while freeing a log tree
  284. */
  285. int free;
  286. /* pin only walk, we record which extents on disk belong to the
  287. * log trees
  288. */
  289. int pin;
  290. /* what stage of the replay code we're currently in */
  291. int stage;
  292. /*
  293. * Ignore any items from the inode currently being processed. Needs
  294. * to be set every time we find a BTRFS_INODE_ITEM_KEY.
  295. */
  296. bool ignore_cur_inode;
  297. /* the root we are currently replaying */
  298. struct btrfs_root *replay_dest;
  299. /* the trans handle for the current replay */
  300. struct btrfs_trans_handle *trans;
  301. /* the function that gets used to process blocks we find in the
  302. * tree. Note the extent_buffer might not be up to date when it is
  303. * passed in, and it must be checked or read if you need the data
  304. * inside it
  305. */
  306. int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
  307. struct walk_control *wc, u64 gen, int level);
  308. };
  309. /*
  310. * process_func used to pin down extents, write them or wait on them
  311. */
  312. static int process_one_buffer(struct btrfs_root *log,
  313. struct extent_buffer *eb,
  314. struct walk_control *wc, u64 gen, int level)
  315. {
  316. struct btrfs_fs_info *fs_info = log->fs_info;
  317. int ret = 0;
  318. /*
  319. * If this fs is mixed then we need to be able to process the leaves to
  320. * pin down any logged extents, so we have to read the block.
  321. */
  322. if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
  323. struct btrfs_tree_parent_check check = {
  324. .level = level,
  325. .transid = gen
  326. };
  327. ret = btrfs_read_extent_buffer(eb, &check);
  328. if (ret)
  329. return ret;
  330. }
  331. if (wc->pin) {
  332. ret = btrfs_pin_extent_for_log_replay(wc->trans, eb);
  333. if (ret)
  334. return ret;
  335. if (btrfs_buffer_uptodate(eb, gen, 0) &&
  336. btrfs_header_level(eb) == 0)
  337. ret = btrfs_exclude_logged_extents(eb);
  338. }
  339. return ret;
  340. }
  341. /*
  342. * Item overwrite used by replay and tree logging. eb, slot and key all refer
  343. * to the src data we are copying out.
  344. *
  345. * root is the tree we are copying into, and path is a scratch
  346. * path for use in this function (it should be released on entry and
  347. * will be released on exit).
  348. *
  349. * If the key is already in the destination tree the existing item is
  350. * overwritten. If the existing item isn't big enough, it is extended.
  351. * If it is too large, it is truncated.
  352. *
  353. * If the key isn't in the destination yet, a new item is inserted.
  354. */
  355. static int overwrite_item(struct btrfs_trans_handle *trans,
  356. struct btrfs_root *root,
  357. struct btrfs_path *path,
  358. struct extent_buffer *eb, int slot,
  359. struct btrfs_key *key)
  360. {
  361. int ret;
  362. u32 item_size;
  363. u64 saved_i_size = 0;
  364. int save_old_i_size = 0;
  365. unsigned long src_ptr;
  366. unsigned long dst_ptr;
  367. bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
  368. /*
  369. * This is only used during log replay, so the root is always from a
  370. * fs/subvolume tree. In case we ever need to support a log root, then
  371. * we'll have to clone the leaf in the path, release the path and use
  372. * the leaf before writing into the log tree. See the comments at
  373. * copy_items() for more details.
  374. */
  375. ASSERT(btrfs_root_id(root) != BTRFS_TREE_LOG_OBJECTID);
  376. item_size = btrfs_item_size(eb, slot);
  377. src_ptr = btrfs_item_ptr_offset(eb, slot);
  378. /* Look for the key in the destination tree. */
  379. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  380. if (ret < 0)
  381. return ret;
  382. if (ret == 0) {
  383. char *src_copy;
  384. char *dst_copy;
  385. u32 dst_size = btrfs_item_size(path->nodes[0],
  386. path->slots[0]);
  387. if (dst_size != item_size)
  388. goto insert;
  389. if (item_size == 0) {
  390. btrfs_release_path(path);
  391. return 0;
  392. }
  393. dst_copy = kmalloc(item_size, GFP_NOFS);
  394. src_copy = kmalloc(item_size, GFP_NOFS);
  395. if (!dst_copy || !src_copy) {
  396. btrfs_release_path(path);
  397. kfree(dst_copy);
  398. kfree(src_copy);
  399. return -ENOMEM;
  400. }
  401. read_extent_buffer(eb, src_copy, src_ptr, item_size);
  402. dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
  403. read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
  404. item_size);
  405. ret = memcmp(dst_copy, src_copy, item_size);
  406. kfree(dst_copy);
  407. kfree(src_copy);
  408. /*
  409. * they have the same contents, just return, this saves
  410. * us from cowing blocks in the destination tree and doing
  411. * extra writes that may not have been done by a previous
  412. * sync
  413. */
  414. if (ret == 0) {
  415. btrfs_release_path(path);
  416. return 0;
  417. }
  418. /*
  419. * We need to load the old nbytes into the inode so when we
  420. * replay the extents we've logged we get the right nbytes.
  421. */
  422. if (inode_item) {
  423. struct btrfs_inode_item *item;
  424. u64 nbytes;
  425. u32 mode;
  426. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  427. struct btrfs_inode_item);
  428. nbytes = btrfs_inode_nbytes(path->nodes[0], item);
  429. item = btrfs_item_ptr(eb, slot,
  430. struct btrfs_inode_item);
  431. btrfs_set_inode_nbytes(eb, item, nbytes);
  432. /*
  433. * If this is a directory we need to reset the i_size to
  434. * 0 so that we can set it up properly when replaying
  435. * the rest of the items in this log.
  436. */
  437. mode = btrfs_inode_mode(eb, item);
  438. if (S_ISDIR(mode))
  439. btrfs_set_inode_size(eb, item, 0);
  440. }
  441. } else if (inode_item) {
  442. struct btrfs_inode_item *item;
  443. u32 mode;
  444. /*
  445. * New inode, set nbytes to 0 so that the nbytes comes out
  446. * properly when we replay the extents.
  447. */
  448. item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
  449. btrfs_set_inode_nbytes(eb, item, 0);
  450. /*
  451. * If this is a directory we need to reset the i_size to 0 so
  452. * that we can set it up properly when replaying the rest of
  453. * the items in this log.
  454. */
  455. mode = btrfs_inode_mode(eb, item);
  456. if (S_ISDIR(mode))
  457. btrfs_set_inode_size(eb, item, 0);
  458. }
  459. insert:
  460. btrfs_release_path(path);
  461. /* try to insert the key into the destination tree */
  462. path->skip_release_on_error = 1;
  463. ret = btrfs_insert_empty_item(trans, root, path,
  464. key, item_size);
  465. path->skip_release_on_error = 0;
  466. /* make sure any existing item is the correct size */
  467. if (ret == -EEXIST || ret == -EOVERFLOW) {
  468. u32 found_size;
  469. found_size = btrfs_item_size(path->nodes[0],
  470. path->slots[0]);
  471. if (found_size > item_size)
  472. btrfs_truncate_item(trans, path, item_size, 1);
  473. else if (found_size < item_size)
  474. btrfs_extend_item(trans, path, item_size - found_size);
  475. } else if (ret) {
  476. return ret;
  477. }
  478. dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
  479. path->slots[0]);
  480. /* don't overwrite an existing inode if the generation number
  481. * was logged as zero. This is done when the tree logging code
  482. * is just logging an inode to make sure it exists after recovery.
  483. *
  484. * Also, don't overwrite i_size on directories during replay.
  485. * log replay inserts and removes directory items based on the
  486. * state of the tree found in the subvolume, and i_size is modified
  487. * as it goes
  488. */
  489. if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
  490. struct btrfs_inode_item *src_item;
  491. struct btrfs_inode_item *dst_item;
  492. src_item = (struct btrfs_inode_item *)src_ptr;
  493. dst_item = (struct btrfs_inode_item *)dst_ptr;
  494. if (btrfs_inode_generation(eb, src_item) == 0) {
  495. struct extent_buffer *dst_eb = path->nodes[0];
  496. const u64 ino_size = btrfs_inode_size(eb, src_item);
  497. /*
  498. * For regular files an ino_size == 0 is used only when
  499. * logging that an inode exists, as part of a directory
  500. * fsync, and the inode wasn't fsynced before. In this
  501. * case don't set the size of the inode in the fs/subvol
  502. * tree, otherwise we would be throwing valid data away.
  503. */
  504. if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
  505. S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
  506. ino_size != 0)
  507. btrfs_set_inode_size(dst_eb, dst_item, ino_size);
  508. goto no_copy;
  509. }
  510. if (S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
  511. S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
  512. save_old_i_size = 1;
  513. saved_i_size = btrfs_inode_size(path->nodes[0],
  514. dst_item);
  515. }
  516. }
  517. copy_extent_buffer(path->nodes[0], eb, dst_ptr,
  518. src_ptr, item_size);
  519. if (save_old_i_size) {
  520. struct btrfs_inode_item *dst_item;
  521. dst_item = (struct btrfs_inode_item *)dst_ptr;
  522. btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
  523. }
  524. /* make sure the generation is filled in */
  525. if (key->type == BTRFS_INODE_ITEM_KEY) {
  526. struct btrfs_inode_item *dst_item;
  527. dst_item = (struct btrfs_inode_item *)dst_ptr;
  528. if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
  529. btrfs_set_inode_generation(path->nodes[0], dst_item,
  530. trans->transid);
  531. }
  532. }
  533. no_copy:
  534. btrfs_mark_buffer_dirty(trans, path->nodes[0]);
  535. btrfs_release_path(path);
  536. return 0;
  537. }
  538. static int read_alloc_one_name(struct extent_buffer *eb, void *start, int len,
  539. struct fscrypt_str *name)
  540. {
  541. char *buf;
  542. buf = kmalloc(len, GFP_NOFS);
  543. if (!buf)
  544. return -ENOMEM;
  545. read_extent_buffer(eb, buf, (unsigned long)start, len);
  546. name->name = buf;
  547. name->len = len;
  548. return 0;
  549. }
  550. /* replays a single extent in 'eb' at 'slot' with 'key' into the
  551. * subvolume 'root'. path is released on entry and should be released
  552. * on exit.
  553. *
  554. * extents in the log tree have not been allocated out of the extent
  555. * tree yet. So, this completes the allocation, taking a reference
  556. * as required if the extent already exists or creating a new extent
  557. * if it isn't in the extent allocation tree yet.
  558. *
  559. * The extent is inserted into the file, dropping any existing extents
  560. * from the file that overlap the new one.
  561. */
  562. static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
  563. struct btrfs_root *root,
  564. struct btrfs_path *path,
  565. struct extent_buffer *eb, int slot,
  566. struct btrfs_key *key)
  567. {
  568. struct btrfs_drop_extents_args drop_args = { 0 };
  569. struct btrfs_fs_info *fs_info = root->fs_info;
  570. int found_type;
  571. u64 extent_end;
  572. u64 start = key->offset;
  573. u64 nbytes = 0;
  574. struct btrfs_file_extent_item *item;
  575. struct btrfs_inode *inode = NULL;
  576. unsigned long size;
  577. int ret = 0;
  578. item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  579. found_type = btrfs_file_extent_type(eb, item);
  580. if (found_type == BTRFS_FILE_EXTENT_REG ||
  581. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  582. nbytes = btrfs_file_extent_num_bytes(eb, item);
  583. extent_end = start + nbytes;
  584. /*
  585. * We don't add to the inodes nbytes if we are prealloc or a
  586. * hole.
  587. */
  588. if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
  589. nbytes = 0;
  590. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  591. size = btrfs_file_extent_ram_bytes(eb, item);
  592. nbytes = btrfs_file_extent_ram_bytes(eb, item);
  593. extent_end = ALIGN(start + size,
  594. fs_info->sectorsize);
  595. } else {
  596. return 0;
  597. }
  598. inode = btrfs_iget_logging(key->objectid, root);
  599. if (IS_ERR(inode))
  600. return PTR_ERR(inode);
  601. /*
  602. * first check to see if we already have this extent in the
  603. * file. This must be done before the btrfs_drop_extents run
  604. * so we don't try to drop this extent.
  605. */
  606. ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode), start, 0);
  607. if (ret == 0 &&
  608. (found_type == BTRFS_FILE_EXTENT_REG ||
  609. found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
  610. struct btrfs_file_extent_item cmp1;
  611. struct btrfs_file_extent_item cmp2;
  612. struct btrfs_file_extent_item *existing;
  613. struct extent_buffer *leaf;
  614. leaf = path->nodes[0];
  615. existing = btrfs_item_ptr(leaf, path->slots[0],
  616. struct btrfs_file_extent_item);
  617. read_extent_buffer(eb, &cmp1, (unsigned long)item,
  618. sizeof(cmp1));
  619. read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
  620. sizeof(cmp2));
  621. /*
  622. * we already have a pointer to this exact extent,
  623. * we don't have to do anything
  624. */
  625. if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
  626. btrfs_release_path(path);
  627. goto out;
  628. }
  629. }
  630. btrfs_release_path(path);
  631. /* drop any overlapping extents */
  632. drop_args.start = start;
  633. drop_args.end = extent_end;
  634. drop_args.drop_cache = true;
  635. ret = btrfs_drop_extents(trans, root, inode, &drop_args);
  636. if (ret)
  637. goto out;
  638. if (found_type == BTRFS_FILE_EXTENT_REG ||
  639. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  640. u64 offset;
  641. unsigned long dest_offset;
  642. struct btrfs_key ins;
  643. if (btrfs_file_extent_disk_bytenr(eb, item) == 0 &&
  644. btrfs_fs_incompat(fs_info, NO_HOLES))
  645. goto update_inode;
  646. ret = btrfs_insert_empty_item(trans, root, path, key,
  647. sizeof(*item));
  648. if (ret)
  649. goto out;
  650. dest_offset = btrfs_item_ptr_offset(path->nodes[0],
  651. path->slots[0]);
  652. copy_extent_buffer(path->nodes[0], eb, dest_offset,
  653. (unsigned long)item, sizeof(*item));
  654. ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
  655. ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
  656. ins.type = BTRFS_EXTENT_ITEM_KEY;
  657. offset = key->offset - btrfs_file_extent_offset(eb, item);
  658. /*
  659. * Manually record dirty extent, as here we did a shallow
  660. * file extent item copy and skip normal backref update,
  661. * but modifying extent tree all by ourselves.
  662. * So need to manually record dirty extent for qgroup,
  663. * as the owner of the file extent changed from log tree
  664. * (doesn't affect qgroup) to fs/file tree(affects qgroup)
  665. */
  666. ret = btrfs_qgroup_trace_extent(trans,
  667. btrfs_file_extent_disk_bytenr(eb, item),
  668. btrfs_file_extent_disk_num_bytes(eb, item));
  669. if (ret < 0)
  670. goto out;
  671. if (ins.objectid > 0) {
  672. u64 csum_start;
  673. u64 csum_end;
  674. LIST_HEAD(ordered_sums);
  675. /*
  676. * is this extent already allocated in the extent
  677. * allocation tree? If so, just add a reference
  678. */
  679. ret = btrfs_lookup_data_extent(fs_info, ins.objectid,
  680. ins.offset);
  681. if (ret < 0) {
  682. goto out;
  683. } else if (ret == 0) {
  684. struct btrfs_ref ref = {
  685. .action = BTRFS_ADD_DELAYED_REF,
  686. .bytenr = ins.objectid,
  687. .num_bytes = ins.offset,
  688. .owning_root = btrfs_root_id(root),
  689. .ref_root = btrfs_root_id(root),
  690. };
  691. btrfs_init_data_ref(&ref, key->objectid, offset,
  692. 0, false);
  693. ret = btrfs_inc_extent_ref(trans, &ref);
  694. if (ret)
  695. goto out;
  696. } else {
  697. /*
  698. * insert the extent pointer in the extent
  699. * allocation tree
  700. */
  701. ret = btrfs_alloc_logged_file_extent(trans,
  702. btrfs_root_id(root),
  703. key->objectid, offset, &ins);
  704. if (ret)
  705. goto out;
  706. }
  707. btrfs_release_path(path);
  708. if (btrfs_file_extent_compression(eb, item)) {
  709. csum_start = ins.objectid;
  710. csum_end = csum_start + ins.offset;
  711. } else {
  712. csum_start = ins.objectid +
  713. btrfs_file_extent_offset(eb, item);
  714. csum_end = csum_start +
  715. btrfs_file_extent_num_bytes(eb, item);
  716. }
  717. ret = btrfs_lookup_csums_list(root->log_root,
  718. csum_start, csum_end - 1,
  719. &ordered_sums, false);
  720. if (ret < 0)
  721. goto out;
  722. ret = 0;
  723. /*
  724. * Now delete all existing cums in the csum root that
  725. * cover our range. We do this because we can have an
  726. * extent that is completely referenced by one file
  727. * extent item and partially referenced by another
  728. * file extent item (like after using the clone or
  729. * extent_same ioctls). In this case if we end up doing
  730. * the replay of the one that partially references the
  731. * extent first, and we do not do the csum deletion
  732. * below, we can get 2 csum items in the csum tree that
  733. * overlap each other. For example, imagine our log has
  734. * the two following file extent items:
  735. *
  736. * key (257 EXTENT_DATA 409600)
  737. * extent data disk byte 12845056 nr 102400
  738. * extent data offset 20480 nr 20480 ram 102400
  739. *
  740. * key (257 EXTENT_DATA 819200)
  741. * extent data disk byte 12845056 nr 102400
  742. * extent data offset 0 nr 102400 ram 102400
  743. *
  744. * Where the second one fully references the 100K extent
  745. * that starts at disk byte 12845056, and the log tree
  746. * has a single csum item that covers the entire range
  747. * of the extent:
  748. *
  749. * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
  750. *
  751. * After the first file extent item is replayed, the
  752. * csum tree gets the following csum item:
  753. *
  754. * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
  755. *
  756. * Which covers the 20K sub-range starting at offset 20K
  757. * of our extent. Now when we replay the second file
  758. * extent item, if we do not delete existing csum items
  759. * that cover any of its blocks, we end up getting two
  760. * csum items in our csum tree that overlap each other:
  761. *
  762. * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
  763. * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
  764. *
  765. * Which is a problem, because after this anyone trying
  766. * to lookup up for the checksum of any block of our
  767. * extent starting at an offset of 40K or higher, will
  768. * end up looking at the second csum item only, which
  769. * does not contain the checksum for any block starting
  770. * at offset 40K or higher of our extent.
  771. */
  772. while (!list_empty(&ordered_sums)) {
  773. struct btrfs_ordered_sum *sums;
  774. struct btrfs_root *csum_root;
  775. sums = list_entry(ordered_sums.next,
  776. struct btrfs_ordered_sum,
  777. list);
  778. csum_root = btrfs_csum_root(fs_info,
  779. sums->logical);
  780. if (!ret)
  781. ret = btrfs_del_csums(trans, csum_root,
  782. sums->logical,
  783. sums->len);
  784. if (!ret)
  785. ret = btrfs_csum_file_blocks(trans,
  786. csum_root,
  787. sums);
  788. list_del(&sums->list);
  789. kfree(sums);
  790. }
  791. if (ret)
  792. goto out;
  793. } else {
  794. btrfs_release_path(path);
  795. }
  796. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  797. /* inline extents are easy, we just overwrite them */
  798. ret = overwrite_item(trans, root, path, eb, slot, key);
  799. if (ret)
  800. goto out;
  801. }
  802. ret = btrfs_inode_set_file_extent_range(inode, start, extent_end - start);
  803. if (ret)
  804. goto out;
  805. update_inode:
  806. btrfs_update_inode_bytes(inode, nbytes, drop_args.bytes_found);
  807. ret = btrfs_update_inode(trans, inode);
  808. out:
  809. iput(&inode->vfs_inode);
  810. return ret;
  811. }
  812. static int unlink_inode_for_log_replay(struct btrfs_trans_handle *trans,
  813. struct btrfs_inode *dir,
  814. struct btrfs_inode *inode,
  815. const struct fscrypt_str *name)
  816. {
  817. int ret;
  818. ret = btrfs_unlink_inode(trans, dir, inode, name);
  819. if (ret)
  820. return ret;
  821. /*
  822. * Whenever we need to check if a name exists or not, we check the
  823. * fs/subvolume tree. So after an unlink we must run delayed items, so
  824. * that future checks for a name during log replay see that the name
  825. * does not exists anymore.
  826. */
  827. return btrfs_run_delayed_items(trans);
  828. }
  829. /*
  830. * when cleaning up conflicts between the directory names in the
  831. * subvolume, directory names in the log and directory names in the
  832. * inode back references, we may have to unlink inodes from directories.
  833. *
  834. * This is a helper function to do the unlink of a specific directory
  835. * item
  836. */
  837. static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
  838. struct btrfs_path *path,
  839. struct btrfs_inode *dir,
  840. struct btrfs_dir_item *di)
  841. {
  842. struct btrfs_root *root = dir->root;
  843. struct btrfs_inode *inode;
  844. struct fscrypt_str name;
  845. struct extent_buffer *leaf;
  846. struct btrfs_key location;
  847. int ret;
  848. leaf = path->nodes[0];
  849. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  850. ret = read_alloc_one_name(leaf, di + 1, btrfs_dir_name_len(leaf, di), &name);
  851. if (ret)
  852. return -ENOMEM;
  853. btrfs_release_path(path);
  854. inode = btrfs_iget_logging(location.objectid, root);
  855. if (IS_ERR(inode)) {
  856. ret = PTR_ERR(inode);
  857. inode = NULL;
  858. goto out;
  859. }
  860. ret = link_to_fixup_dir(trans, root, path, location.objectid);
  861. if (ret)
  862. goto out;
  863. ret = unlink_inode_for_log_replay(trans, dir, inode, &name);
  864. out:
  865. kfree(name.name);
  866. if (inode)
  867. iput(&inode->vfs_inode);
  868. return ret;
  869. }
  870. /*
  871. * See if a given name and sequence number found in an inode back reference are
  872. * already in a directory and correctly point to this inode.
  873. *
  874. * Returns: < 0 on error, 0 if the directory entry does not exists and 1 if it
  875. * exists.
  876. */
  877. static noinline int inode_in_dir(struct btrfs_root *root,
  878. struct btrfs_path *path,
  879. u64 dirid, u64 objectid, u64 index,
  880. struct fscrypt_str *name)
  881. {
  882. struct btrfs_dir_item *di;
  883. struct btrfs_key location;
  884. int ret = 0;
  885. di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
  886. index, name, 0);
  887. if (IS_ERR(di)) {
  888. ret = PTR_ERR(di);
  889. goto out;
  890. } else if (di) {
  891. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
  892. if (location.objectid != objectid)
  893. goto out;
  894. } else {
  895. goto out;
  896. }
  897. btrfs_release_path(path);
  898. di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, 0);
  899. if (IS_ERR(di)) {
  900. ret = PTR_ERR(di);
  901. goto out;
  902. } else if (di) {
  903. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
  904. if (location.objectid == objectid)
  905. ret = 1;
  906. }
  907. out:
  908. btrfs_release_path(path);
  909. return ret;
  910. }
  911. /*
  912. * helper function to check a log tree for a named back reference in
  913. * an inode. This is used to decide if a back reference that is
  914. * found in the subvolume conflicts with what we find in the log.
  915. *
  916. * inode backreferences may have multiple refs in a single item,
  917. * during replay we process one reference at a time, and we don't
  918. * want to delete valid links to a file from the subvolume if that
  919. * link is also in the log.
  920. */
  921. static noinline int backref_in_log(struct btrfs_root *log,
  922. struct btrfs_key *key,
  923. u64 ref_objectid,
  924. const struct fscrypt_str *name)
  925. {
  926. struct btrfs_path *path;
  927. int ret;
  928. path = btrfs_alloc_path();
  929. if (!path)
  930. return -ENOMEM;
  931. ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
  932. if (ret < 0) {
  933. goto out;
  934. } else if (ret == 1) {
  935. ret = 0;
  936. goto out;
  937. }
  938. if (key->type == BTRFS_INODE_EXTREF_KEY)
  939. ret = !!btrfs_find_name_in_ext_backref(path->nodes[0],
  940. path->slots[0],
  941. ref_objectid, name);
  942. else
  943. ret = !!btrfs_find_name_in_backref(path->nodes[0],
  944. path->slots[0], name);
  945. out:
  946. btrfs_free_path(path);
  947. return ret;
  948. }
  949. static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
  950. struct btrfs_root *root,
  951. struct btrfs_path *path,
  952. struct btrfs_root *log_root,
  953. struct btrfs_inode *dir,
  954. struct btrfs_inode *inode,
  955. u64 inode_objectid, u64 parent_objectid,
  956. u64 ref_index, struct fscrypt_str *name)
  957. {
  958. int ret;
  959. struct extent_buffer *leaf;
  960. struct btrfs_dir_item *di;
  961. struct btrfs_key search_key;
  962. struct btrfs_inode_extref *extref;
  963. again:
  964. /* Search old style refs */
  965. search_key.objectid = inode_objectid;
  966. search_key.type = BTRFS_INODE_REF_KEY;
  967. search_key.offset = parent_objectid;
  968. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  969. if (ret < 0) {
  970. return ret;
  971. } else if (ret == 0) {
  972. struct btrfs_inode_ref *victim_ref;
  973. unsigned long ptr;
  974. unsigned long ptr_end;
  975. leaf = path->nodes[0];
  976. /* are we trying to overwrite a back ref for the root directory
  977. * if so, just jump out, we're done
  978. */
  979. if (search_key.objectid == search_key.offset)
  980. return 1;
  981. /* check all the names in this back reference to see
  982. * if they are in the log. if so, we allow them to stay
  983. * otherwise they must be unlinked as a conflict
  984. */
  985. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  986. ptr_end = ptr + btrfs_item_size(leaf, path->slots[0]);
  987. while (ptr < ptr_end) {
  988. struct fscrypt_str victim_name;
  989. victim_ref = (struct btrfs_inode_ref *)ptr;
  990. ret = read_alloc_one_name(leaf, (victim_ref + 1),
  991. btrfs_inode_ref_name_len(leaf, victim_ref),
  992. &victim_name);
  993. if (ret)
  994. return ret;
  995. ret = backref_in_log(log_root, &search_key,
  996. parent_objectid, &victim_name);
  997. if (ret < 0) {
  998. kfree(victim_name.name);
  999. return ret;
  1000. } else if (!ret) {
  1001. inc_nlink(&inode->vfs_inode);
  1002. btrfs_release_path(path);
  1003. ret = unlink_inode_for_log_replay(trans, dir, inode,
  1004. &victim_name);
  1005. kfree(victim_name.name);
  1006. if (ret)
  1007. return ret;
  1008. goto again;
  1009. }
  1010. kfree(victim_name.name);
  1011. ptr = (unsigned long)(victim_ref + 1) + victim_name.len;
  1012. }
  1013. }
  1014. btrfs_release_path(path);
  1015. /* Same search but for extended refs */
  1016. extref = btrfs_lookup_inode_extref(NULL, root, path, name,
  1017. inode_objectid, parent_objectid, 0,
  1018. 0);
  1019. if (IS_ERR(extref)) {
  1020. return PTR_ERR(extref);
  1021. } else if (extref) {
  1022. u32 item_size;
  1023. u32 cur_offset = 0;
  1024. unsigned long base;
  1025. struct btrfs_inode *victim_parent;
  1026. leaf = path->nodes[0];
  1027. item_size = btrfs_item_size(leaf, path->slots[0]);
  1028. base = btrfs_item_ptr_offset(leaf, path->slots[0]);
  1029. while (cur_offset < item_size) {
  1030. struct fscrypt_str victim_name;
  1031. extref = (struct btrfs_inode_extref *)(base + cur_offset);
  1032. victim_name.len = btrfs_inode_extref_name_len(leaf, extref);
  1033. if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
  1034. goto next;
  1035. ret = read_alloc_one_name(leaf, &extref->name,
  1036. victim_name.len, &victim_name);
  1037. if (ret)
  1038. return ret;
  1039. search_key.objectid = inode_objectid;
  1040. search_key.type = BTRFS_INODE_EXTREF_KEY;
  1041. search_key.offset = btrfs_extref_hash(parent_objectid,
  1042. victim_name.name,
  1043. victim_name.len);
  1044. ret = backref_in_log(log_root, &search_key,
  1045. parent_objectid, &victim_name);
  1046. if (ret < 0) {
  1047. kfree(victim_name.name);
  1048. return ret;
  1049. } else if (!ret) {
  1050. victim_parent = btrfs_iget_logging(parent_objectid, root);
  1051. if (IS_ERR(victim_parent)) {
  1052. ret = PTR_ERR(victim_parent);
  1053. } else {
  1054. inc_nlink(&inode->vfs_inode);
  1055. btrfs_release_path(path);
  1056. ret = unlink_inode_for_log_replay(trans,
  1057. victim_parent,
  1058. inode, &victim_name);
  1059. iput(&victim_parent->vfs_inode);
  1060. }
  1061. kfree(victim_name.name);
  1062. if (ret)
  1063. return ret;
  1064. goto again;
  1065. }
  1066. kfree(victim_name.name);
  1067. next:
  1068. cur_offset += victim_name.len + sizeof(*extref);
  1069. }
  1070. }
  1071. btrfs_release_path(path);
  1072. /* look for a conflicting sequence number */
  1073. di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
  1074. ref_index, name, 0);
  1075. if (IS_ERR(di)) {
  1076. return PTR_ERR(di);
  1077. } else if (di) {
  1078. ret = drop_one_dir_item(trans, path, dir, di);
  1079. if (ret)
  1080. return ret;
  1081. }
  1082. btrfs_release_path(path);
  1083. /* look for a conflicting name */
  1084. di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir), name, 0);
  1085. if (IS_ERR(di)) {
  1086. return PTR_ERR(di);
  1087. } else if (di) {
  1088. ret = drop_one_dir_item(trans, path, dir, di);
  1089. if (ret)
  1090. return ret;
  1091. }
  1092. btrfs_release_path(path);
  1093. return 0;
  1094. }
  1095. static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
  1096. struct fscrypt_str *name, u64 *index,
  1097. u64 *parent_objectid)
  1098. {
  1099. struct btrfs_inode_extref *extref;
  1100. int ret;
  1101. extref = (struct btrfs_inode_extref *)ref_ptr;
  1102. ret = read_alloc_one_name(eb, &extref->name,
  1103. btrfs_inode_extref_name_len(eb, extref), name);
  1104. if (ret)
  1105. return ret;
  1106. if (index)
  1107. *index = btrfs_inode_extref_index(eb, extref);
  1108. if (parent_objectid)
  1109. *parent_objectid = btrfs_inode_extref_parent(eb, extref);
  1110. return 0;
  1111. }
  1112. static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
  1113. struct fscrypt_str *name, u64 *index)
  1114. {
  1115. struct btrfs_inode_ref *ref;
  1116. int ret;
  1117. ref = (struct btrfs_inode_ref *)ref_ptr;
  1118. ret = read_alloc_one_name(eb, ref + 1, btrfs_inode_ref_name_len(eb, ref),
  1119. name);
  1120. if (ret)
  1121. return ret;
  1122. if (index)
  1123. *index = btrfs_inode_ref_index(eb, ref);
  1124. return 0;
  1125. }
  1126. /*
  1127. * Take an inode reference item from the log tree and iterate all names from the
  1128. * inode reference item in the subvolume tree with the same key (if it exists).
  1129. * For any name that is not in the inode reference item from the log tree, do a
  1130. * proper unlink of that name (that is, remove its entry from the inode
  1131. * reference item and both dir index keys).
  1132. */
  1133. static int unlink_old_inode_refs(struct btrfs_trans_handle *trans,
  1134. struct btrfs_root *root,
  1135. struct btrfs_path *path,
  1136. struct btrfs_inode *inode,
  1137. struct extent_buffer *log_eb,
  1138. int log_slot,
  1139. struct btrfs_key *key)
  1140. {
  1141. int ret;
  1142. unsigned long ref_ptr;
  1143. unsigned long ref_end;
  1144. struct extent_buffer *eb;
  1145. again:
  1146. btrfs_release_path(path);
  1147. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  1148. if (ret > 0) {
  1149. ret = 0;
  1150. goto out;
  1151. }
  1152. if (ret < 0)
  1153. goto out;
  1154. eb = path->nodes[0];
  1155. ref_ptr = btrfs_item_ptr_offset(eb, path->slots[0]);
  1156. ref_end = ref_ptr + btrfs_item_size(eb, path->slots[0]);
  1157. while (ref_ptr < ref_end) {
  1158. struct fscrypt_str name;
  1159. u64 parent_id;
  1160. if (key->type == BTRFS_INODE_EXTREF_KEY) {
  1161. ret = extref_get_fields(eb, ref_ptr, &name,
  1162. NULL, &parent_id);
  1163. } else {
  1164. parent_id = key->offset;
  1165. ret = ref_get_fields(eb, ref_ptr, &name, NULL);
  1166. }
  1167. if (ret)
  1168. goto out;
  1169. if (key->type == BTRFS_INODE_EXTREF_KEY)
  1170. ret = !!btrfs_find_name_in_ext_backref(log_eb, log_slot,
  1171. parent_id, &name);
  1172. else
  1173. ret = !!btrfs_find_name_in_backref(log_eb, log_slot, &name);
  1174. if (!ret) {
  1175. struct btrfs_inode *dir;
  1176. btrfs_release_path(path);
  1177. dir = btrfs_iget_logging(parent_id, root);
  1178. if (IS_ERR(dir)) {
  1179. ret = PTR_ERR(dir);
  1180. kfree(name.name);
  1181. goto out;
  1182. }
  1183. ret = unlink_inode_for_log_replay(trans, dir, inode, &name);
  1184. kfree(name.name);
  1185. iput(&dir->vfs_inode);
  1186. if (ret)
  1187. goto out;
  1188. goto again;
  1189. }
  1190. kfree(name.name);
  1191. ref_ptr += name.len;
  1192. if (key->type == BTRFS_INODE_EXTREF_KEY)
  1193. ref_ptr += sizeof(struct btrfs_inode_extref);
  1194. else
  1195. ref_ptr += sizeof(struct btrfs_inode_ref);
  1196. }
  1197. ret = 0;
  1198. out:
  1199. btrfs_release_path(path);
  1200. return ret;
  1201. }
  1202. /*
  1203. * replay one inode back reference item found in the log tree.
  1204. * eb, slot and key refer to the buffer and key found in the log tree.
  1205. * root is the destination we are replaying into, and path is for temp
  1206. * use by this function. (it should be released on return).
  1207. */
  1208. static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
  1209. struct btrfs_root *root,
  1210. struct btrfs_root *log,
  1211. struct btrfs_path *path,
  1212. struct extent_buffer *eb, int slot,
  1213. struct btrfs_key *key)
  1214. {
  1215. struct btrfs_inode *dir = NULL;
  1216. struct btrfs_inode *inode = NULL;
  1217. unsigned long ref_ptr;
  1218. unsigned long ref_end;
  1219. struct fscrypt_str name = { 0 };
  1220. int ret;
  1221. int log_ref_ver = 0;
  1222. u64 parent_objectid;
  1223. u64 inode_objectid;
  1224. u64 ref_index = 0;
  1225. int ref_struct_size;
  1226. ref_ptr = btrfs_item_ptr_offset(eb, slot);
  1227. ref_end = ref_ptr + btrfs_item_size(eb, slot);
  1228. if (key->type == BTRFS_INODE_EXTREF_KEY) {
  1229. struct btrfs_inode_extref *r;
  1230. ref_struct_size = sizeof(struct btrfs_inode_extref);
  1231. log_ref_ver = 1;
  1232. r = (struct btrfs_inode_extref *)ref_ptr;
  1233. parent_objectid = btrfs_inode_extref_parent(eb, r);
  1234. } else {
  1235. ref_struct_size = sizeof(struct btrfs_inode_ref);
  1236. parent_objectid = key->offset;
  1237. }
  1238. inode_objectid = key->objectid;
  1239. /*
  1240. * it is possible that we didn't log all the parent directories
  1241. * for a given inode. If we don't find the dir, just don't
  1242. * copy the back ref in. The link count fixup code will take
  1243. * care of the rest
  1244. */
  1245. dir = btrfs_iget_logging(parent_objectid, root);
  1246. if (IS_ERR(dir)) {
  1247. ret = PTR_ERR(dir);
  1248. if (ret == -ENOENT)
  1249. ret = 0;
  1250. dir = NULL;
  1251. goto out;
  1252. }
  1253. inode = btrfs_iget_logging(inode_objectid, root);
  1254. if (IS_ERR(inode)) {
  1255. ret = PTR_ERR(inode);
  1256. inode = NULL;
  1257. goto out;
  1258. }
  1259. while (ref_ptr < ref_end) {
  1260. if (log_ref_ver) {
  1261. ret = extref_get_fields(eb, ref_ptr, &name,
  1262. &ref_index, &parent_objectid);
  1263. if (ret)
  1264. goto out;
  1265. /*
  1266. * parent object can change from one array
  1267. * item to another.
  1268. */
  1269. if (!dir) {
  1270. dir = btrfs_iget_logging(parent_objectid, root);
  1271. if (IS_ERR(dir)) {
  1272. ret = PTR_ERR(dir);
  1273. dir = NULL;
  1274. /*
  1275. * A new parent dir may have not been
  1276. * logged and not exist in the subvolume
  1277. * tree, see the comment above before
  1278. * the loop when getting the first
  1279. * parent dir.
  1280. */
  1281. if (ret == -ENOENT) {
  1282. /*
  1283. * The next extref may refer to
  1284. * another parent dir that
  1285. * exists, so continue.
  1286. */
  1287. ret = 0;
  1288. goto next;
  1289. }
  1290. goto out;
  1291. }
  1292. }
  1293. } else {
  1294. ret = ref_get_fields(eb, ref_ptr, &name, &ref_index);
  1295. if (ret)
  1296. goto out;
  1297. }
  1298. ret = inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode),
  1299. ref_index, &name);
  1300. if (ret < 0) {
  1301. goto out;
  1302. } else if (ret == 0) {
  1303. /*
  1304. * look for a conflicting back reference in the
  1305. * metadata. if we find one we have to unlink that name
  1306. * of the file before we add our new link. Later on, we
  1307. * overwrite any existing back reference, and we don't
  1308. * want to create dangling pointers in the directory.
  1309. */
  1310. ret = __add_inode_ref(trans, root, path, log, dir, inode,
  1311. inode_objectid, parent_objectid,
  1312. ref_index, &name);
  1313. if (ret) {
  1314. if (ret == 1)
  1315. ret = 0;
  1316. goto out;
  1317. }
  1318. /* insert our name */
  1319. ret = btrfs_add_link(trans, dir, inode, &name, 0, ref_index);
  1320. if (ret)
  1321. goto out;
  1322. ret = btrfs_update_inode(trans, inode);
  1323. if (ret)
  1324. goto out;
  1325. }
  1326. /* Else, ret == 1, we already have a perfect match, we're done. */
  1327. next:
  1328. ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + name.len;
  1329. kfree(name.name);
  1330. name.name = NULL;
  1331. if (log_ref_ver && dir) {
  1332. iput(&dir->vfs_inode);
  1333. dir = NULL;
  1334. }
  1335. }
  1336. /*
  1337. * Before we overwrite the inode reference item in the subvolume tree
  1338. * with the item from the log tree, we must unlink all names from the
  1339. * parent directory that are in the subvolume's tree inode reference
  1340. * item, otherwise we end up with an inconsistent subvolume tree where
  1341. * dir index entries exist for a name but there is no inode reference
  1342. * item with the same name.
  1343. */
  1344. ret = unlink_old_inode_refs(trans, root, path, inode, eb, slot, key);
  1345. if (ret)
  1346. goto out;
  1347. /* finally write the back reference in the inode */
  1348. ret = overwrite_item(trans, root, path, eb, slot, key);
  1349. out:
  1350. btrfs_release_path(path);
  1351. kfree(name.name);
  1352. if (dir)
  1353. iput(&dir->vfs_inode);
  1354. if (inode)
  1355. iput(&inode->vfs_inode);
  1356. return ret;
  1357. }
  1358. static int count_inode_extrefs(struct btrfs_inode *inode, struct btrfs_path *path)
  1359. {
  1360. int ret = 0;
  1361. int name_len;
  1362. unsigned int nlink = 0;
  1363. u32 item_size;
  1364. u32 cur_offset = 0;
  1365. u64 inode_objectid = btrfs_ino(inode);
  1366. u64 offset = 0;
  1367. unsigned long ptr;
  1368. struct btrfs_inode_extref *extref;
  1369. struct extent_buffer *leaf;
  1370. while (1) {
  1371. ret = btrfs_find_one_extref(inode->root, inode_objectid, offset,
  1372. path, &extref, &offset);
  1373. if (ret)
  1374. break;
  1375. leaf = path->nodes[0];
  1376. item_size = btrfs_item_size(leaf, path->slots[0]);
  1377. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  1378. cur_offset = 0;
  1379. while (cur_offset < item_size) {
  1380. extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
  1381. name_len = btrfs_inode_extref_name_len(leaf, extref);
  1382. nlink++;
  1383. cur_offset += name_len + sizeof(*extref);
  1384. }
  1385. offset++;
  1386. btrfs_release_path(path);
  1387. }
  1388. btrfs_release_path(path);
  1389. if (ret < 0 && ret != -ENOENT)
  1390. return ret;
  1391. return nlink;
  1392. }
  1393. static int count_inode_refs(struct btrfs_inode *inode, struct btrfs_path *path)
  1394. {
  1395. int ret;
  1396. struct btrfs_key key;
  1397. unsigned int nlink = 0;
  1398. unsigned long ptr;
  1399. unsigned long ptr_end;
  1400. int name_len;
  1401. u64 ino = btrfs_ino(inode);
  1402. key.objectid = ino;
  1403. key.type = BTRFS_INODE_REF_KEY;
  1404. key.offset = (u64)-1;
  1405. while (1) {
  1406. ret = btrfs_search_slot(NULL, inode->root, &key, path, 0, 0);
  1407. if (ret < 0)
  1408. break;
  1409. if (ret > 0) {
  1410. if (path->slots[0] == 0)
  1411. break;
  1412. path->slots[0]--;
  1413. }
  1414. process_slot:
  1415. btrfs_item_key_to_cpu(path->nodes[0], &key,
  1416. path->slots[0]);
  1417. if (key.objectid != ino ||
  1418. key.type != BTRFS_INODE_REF_KEY)
  1419. break;
  1420. ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
  1421. ptr_end = ptr + btrfs_item_size(path->nodes[0],
  1422. path->slots[0]);
  1423. while (ptr < ptr_end) {
  1424. struct btrfs_inode_ref *ref;
  1425. ref = (struct btrfs_inode_ref *)ptr;
  1426. name_len = btrfs_inode_ref_name_len(path->nodes[0],
  1427. ref);
  1428. ptr = (unsigned long)(ref + 1) + name_len;
  1429. nlink++;
  1430. }
  1431. if (key.offset == 0)
  1432. break;
  1433. if (path->slots[0] > 0) {
  1434. path->slots[0]--;
  1435. goto process_slot;
  1436. }
  1437. key.offset--;
  1438. btrfs_release_path(path);
  1439. }
  1440. btrfs_release_path(path);
  1441. return nlink;
  1442. }
  1443. /*
  1444. * There are a few corners where the link count of the file can't
  1445. * be properly maintained during replay. So, instead of adding
  1446. * lots of complexity to the log code, we just scan the backrefs
  1447. * for any file that has been through replay.
  1448. *
  1449. * The scan will update the link count on the inode to reflect the
  1450. * number of back refs found. If it goes down to zero, the iput
  1451. * will free the inode.
  1452. */
  1453. static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
  1454. struct inode *inode)
  1455. {
  1456. struct btrfs_root *root = BTRFS_I(inode)->root;
  1457. struct btrfs_path *path;
  1458. int ret;
  1459. u64 nlink = 0;
  1460. u64 ino = btrfs_ino(BTRFS_I(inode));
  1461. path = btrfs_alloc_path();
  1462. if (!path)
  1463. return -ENOMEM;
  1464. ret = count_inode_refs(BTRFS_I(inode), path);
  1465. if (ret < 0)
  1466. goto out;
  1467. nlink = ret;
  1468. ret = count_inode_extrefs(BTRFS_I(inode), path);
  1469. if (ret < 0)
  1470. goto out;
  1471. nlink += ret;
  1472. ret = 0;
  1473. if (nlink != inode->i_nlink) {
  1474. set_nlink(inode, nlink);
  1475. ret = btrfs_update_inode(trans, BTRFS_I(inode));
  1476. if (ret)
  1477. goto out;
  1478. }
  1479. if (S_ISDIR(inode->i_mode))
  1480. BTRFS_I(inode)->index_cnt = (u64)-1;
  1481. if (inode->i_nlink == 0) {
  1482. if (S_ISDIR(inode->i_mode)) {
  1483. ret = replay_dir_deletes(trans, root, NULL, path,
  1484. ino, 1);
  1485. if (ret)
  1486. goto out;
  1487. }
  1488. ret = btrfs_insert_orphan_item(trans, root, ino);
  1489. if (ret == -EEXIST)
  1490. ret = 0;
  1491. }
  1492. out:
  1493. btrfs_free_path(path);
  1494. return ret;
  1495. }
  1496. static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
  1497. struct btrfs_root *root,
  1498. struct btrfs_path *path)
  1499. {
  1500. int ret;
  1501. struct btrfs_key key;
  1502. key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
  1503. key.type = BTRFS_ORPHAN_ITEM_KEY;
  1504. key.offset = (u64)-1;
  1505. while (1) {
  1506. struct btrfs_inode *inode;
  1507. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1508. if (ret < 0)
  1509. break;
  1510. if (ret == 1) {
  1511. ret = 0;
  1512. if (path->slots[0] == 0)
  1513. break;
  1514. path->slots[0]--;
  1515. }
  1516. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1517. if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
  1518. key.type != BTRFS_ORPHAN_ITEM_KEY)
  1519. break;
  1520. ret = btrfs_del_item(trans, root, path);
  1521. if (ret)
  1522. break;
  1523. btrfs_release_path(path);
  1524. inode = btrfs_iget_logging(key.offset, root);
  1525. if (IS_ERR(inode)) {
  1526. ret = PTR_ERR(inode);
  1527. break;
  1528. }
  1529. ret = fixup_inode_link_count(trans, &inode->vfs_inode);
  1530. iput(&inode->vfs_inode);
  1531. if (ret)
  1532. break;
  1533. /*
  1534. * fixup on a directory may create new entries,
  1535. * make sure we always look for the highset possible
  1536. * offset
  1537. */
  1538. key.offset = (u64)-1;
  1539. }
  1540. btrfs_release_path(path);
  1541. return ret;
  1542. }
  1543. /*
  1544. * record a given inode in the fixup dir so we can check its link
  1545. * count when replay is done. The link count is incremented here
  1546. * so the inode won't go away until we check it
  1547. */
  1548. static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
  1549. struct btrfs_root *root,
  1550. struct btrfs_path *path,
  1551. u64 objectid)
  1552. {
  1553. struct btrfs_key key;
  1554. int ret = 0;
  1555. struct btrfs_inode *inode;
  1556. struct inode *vfs_inode;
  1557. inode = btrfs_iget_logging(objectid, root);
  1558. if (IS_ERR(inode))
  1559. return PTR_ERR(inode);
  1560. vfs_inode = &inode->vfs_inode;
  1561. key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
  1562. key.type = BTRFS_ORPHAN_ITEM_KEY;
  1563. key.offset = objectid;
  1564. ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
  1565. btrfs_release_path(path);
  1566. if (ret == 0) {
  1567. if (!vfs_inode->i_nlink)
  1568. set_nlink(vfs_inode, 1);
  1569. else
  1570. inc_nlink(vfs_inode);
  1571. ret = btrfs_update_inode(trans, inode);
  1572. } else if (ret == -EEXIST) {
  1573. ret = 0;
  1574. }
  1575. iput(vfs_inode);
  1576. return ret;
  1577. }
  1578. /*
  1579. * when replaying the log for a directory, we only insert names
  1580. * for inodes that actually exist. This means an fsync on a directory
  1581. * does not implicitly fsync all the new files in it
  1582. */
  1583. static noinline int insert_one_name(struct btrfs_trans_handle *trans,
  1584. struct btrfs_root *root,
  1585. u64 dirid, u64 index,
  1586. const struct fscrypt_str *name,
  1587. struct btrfs_key *location)
  1588. {
  1589. struct btrfs_inode *inode;
  1590. struct btrfs_inode *dir;
  1591. int ret;
  1592. inode = btrfs_iget_logging(location->objectid, root);
  1593. if (IS_ERR(inode))
  1594. return PTR_ERR(inode);
  1595. dir = btrfs_iget_logging(dirid, root);
  1596. if (IS_ERR(dir)) {
  1597. iput(&inode->vfs_inode);
  1598. return PTR_ERR(dir);
  1599. }
  1600. ret = btrfs_add_link(trans, dir, inode, name, 1, index);
  1601. /* FIXME, put inode into FIXUP list */
  1602. iput(&inode->vfs_inode);
  1603. iput(&dir->vfs_inode);
  1604. return ret;
  1605. }
  1606. static int delete_conflicting_dir_entry(struct btrfs_trans_handle *trans,
  1607. struct btrfs_inode *dir,
  1608. struct btrfs_path *path,
  1609. struct btrfs_dir_item *dst_di,
  1610. const struct btrfs_key *log_key,
  1611. u8 log_flags,
  1612. bool exists)
  1613. {
  1614. struct btrfs_key found_key;
  1615. btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
  1616. /* The existing dentry points to the same inode, don't delete it. */
  1617. if (found_key.objectid == log_key->objectid &&
  1618. found_key.type == log_key->type &&
  1619. found_key.offset == log_key->offset &&
  1620. btrfs_dir_flags(path->nodes[0], dst_di) == log_flags)
  1621. return 1;
  1622. /*
  1623. * Don't drop the conflicting directory entry if the inode for the new
  1624. * entry doesn't exist.
  1625. */
  1626. if (!exists)
  1627. return 0;
  1628. return drop_one_dir_item(trans, path, dir, dst_di);
  1629. }
  1630. /*
  1631. * take a single entry in a log directory item and replay it into
  1632. * the subvolume.
  1633. *
  1634. * if a conflicting item exists in the subdirectory already,
  1635. * the inode it points to is unlinked and put into the link count
  1636. * fix up tree.
  1637. *
  1638. * If a name from the log points to a file or directory that does
  1639. * not exist in the FS, it is skipped. fsyncs on directories
  1640. * do not force down inodes inside that directory, just changes to the
  1641. * names or unlinks in a directory.
  1642. *
  1643. * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
  1644. * non-existing inode) and 1 if the name was replayed.
  1645. */
  1646. static noinline int replay_one_name(struct btrfs_trans_handle *trans,
  1647. struct btrfs_root *root,
  1648. struct btrfs_path *path,
  1649. struct extent_buffer *eb,
  1650. struct btrfs_dir_item *di,
  1651. struct btrfs_key *key)
  1652. {
  1653. struct fscrypt_str name = { 0 };
  1654. struct btrfs_dir_item *dir_dst_di;
  1655. struct btrfs_dir_item *index_dst_di;
  1656. bool dir_dst_matches = false;
  1657. bool index_dst_matches = false;
  1658. struct btrfs_key log_key;
  1659. struct btrfs_key search_key;
  1660. struct btrfs_inode *dir;
  1661. u8 log_flags;
  1662. bool exists;
  1663. int ret;
  1664. bool update_size = true;
  1665. bool name_added = false;
  1666. dir = btrfs_iget_logging(key->objectid, root);
  1667. if (IS_ERR(dir))
  1668. return PTR_ERR(dir);
  1669. ret = read_alloc_one_name(eb, di + 1, btrfs_dir_name_len(eb, di), &name);
  1670. if (ret)
  1671. goto out;
  1672. log_flags = btrfs_dir_flags(eb, di);
  1673. btrfs_dir_item_key_to_cpu(eb, di, &log_key);
  1674. ret = btrfs_lookup_inode(trans, root, path, &log_key, 0);
  1675. btrfs_release_path(path);
  1676. if (ret < 0)
  1677. goto out;
  1678. exists = (ret == 0);
  1679. ret = 0;
  1680. dir_dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
  1681. &name, 1);
  1682. if (IS_ERR(dir_dst_di)) {
  1683. ret = PTR_ERR(dir_dst_di);
  1684. goto out;
  1685. } else if (dir_dst_di) {
  1686. ret = delete_conflicting_dir_entry(trans, dir, path, dir_dst_di,
  1687. &log_key, log_flags, exists);
  1688. if (ret < 0)
  1689. goto out;
  1690. dir_dst_matches = (ret == 1);
  1691. }
  1692. btrfs_release_path(path);
  1693. index_dst_di = btrfs_lookup_dir_index_item(trans, root, path,
  1694. key->objectid, key->offset,
  1695. &name, 1);
  1696. if (IS_ERR(index_dst_di)) {
  1697. ret = PTR_ERR(index_dst_di);
  1698. goto out;
  1699. } else if (index_dst_di) {
  1700. ret = delete_conflicting_dir_entry(trans, dir, path, index_dst_di,
  1701. &log_key, log_flags, exists);
  1702. if (ret < 0)
  1703. goto out;
  1704. index_dst_matches = (ret == 1);
  1705. }
  1706. btrfs_release_path(path);
  1707. if (dir_dst_matches && index_dst_matches) {
  1708. ret = 0;
  1709. update_size = false;
  1710. goto out;
  1711. }
  1712. /*
  1713. * Check if the inode reference exists in the log for the given name,
  1714. * inode and parent inode
  1715. */
  1716. search_key.objectid = log_key.objectid;
  1717. search_key.type = BTRFS_INODE_REF_KEY;
  1718. search_key.offset = key->objectid;
  1719. ret = backref_in_log(root->log_root, &search_key, 0, &name);
  1720. if (ret < 0) {
  1721. goto out;
  1722. } else if (ret) {
  1723. /* The dentry will be added later. */
  1724. ret = 0;
  1725. update_size = false;
  1726. goto out;
  1727. }
  1728. search_key.objectid = log_key.objectid;
  1729. search_key.type = BTRFS_INODE_EXTREF_KEY;
  1730. search_key.offset = btrfs_extref_hash(key->objectid, name.name, name.len);
  1731. ret = backref_in_log(root->log_root, &search_key, key->objectid, &name);
  1732. if (ret < 0) {
  1733. goto out;
  1734. } else if (ret) {
  1735. /* The dentry will be added later. */
  1736. ret = 0;
  1737. update_size = false;
  1738. goto out;
  1739. }
  1740. btrfs_release_path(path);
  1741. ret = insert_one_name(trans, root, key->objectid, key->offset,
  1742. &name, &log_key);
  1743. if (ret && ret != -ENOENT && ret != -EEXIST)
  1744. goto out;
  1745. if (!ret)
  1746. name_added = true;
  1747. update_size = false;
  1748. ret = 0;
  1749. out:
  1750. if (!ret && update_size) {
  1751. btrfs_i_size_write(dir, dir->vfs_inode.i_size + name.len * 2);
  1752. ret = btrfs_update_inode(trans, dir);
  1753. }
  1754. kfree(name.name);
  1755. iput(&dir->vfs_inode);
  1756. if (!ret && name_added)
  1757. ret = 1;
  1758. return ret;
  1759. }
  1760. /* Replay one dir item from a BTRFS_DIR_INDEX_KEY key. */
  1761. static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
  1762. struct btrfs_root *root,
  1763. struct btrfs_path *path,
  1764. struct extent_buffer *eb, int slot,
  1765. struct btrfs_key *key)
  1766. {
  1767. int ret;
  1768. struct btrfs_dir_item *di;
  1769. /* We only log dir index keys, which only contain a single dir item. */
  1770. ASSERT(key->type == BTRFS_DIR_INDEX_KEY);
  1771. di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
  1772. ret = replay_one_name(trans, root, path, eb, di, key);
  1773. if (ret < 0)
  1774. return ret;
  1775. /*
  1776. * If this entry refers to a non-directory (directories can not have a
  1777. * link count > 1) and it was added in the transaction that was not
  1778. * committed, make sure we fixup the link count of the inode the entry
  1779. * points to. Otherwise something like the following would result in a
  1780. * directory pointing to an inode with a wrong link that does not account
  1781. * for this dir entry:
  1782. *
  1783. * mkdir testdir
  1784. * touch testdir/foo
  1785. * touch testdir/bar
  1786. * sync
  1787. *
  1788. * ln testdir/bar testdir/bar_link
  1789. * ln testdir/foo testdir/foo_link
  1790. * xfs_io -c "fsync" testdir/bar
  1791. *
  1792. * <power failure>
  1793. *
  1794. * mount fs, log replay happens
  1795. *
  1796. * File foo would remain with a link count of 1 when it has two entries
  1797. * pointing to it in the directory testdir. This would make it impossible
  1798. * to ever delete the parent directory has it would result in stale
  1799. * dentries that can never be deleted.
  1800. */
  1801. if (ret == 1 && btrfs_dir_ftype(eb, di) != BTRFS_FT_DIR) {
  1802. struct btrfs_path *fixup_path;
  1803. struct btrfs_key di_key;
  1804. fixup_path = btrfs_alloc_path();
  1805. if (!fixup_path)
  1806. return -ENOMEM;
  1807. btrfs_dir_item_key_to_cpu(eb, di, &di_key);
  1808. ret = link_to_fixup_dir(trans, root, fixup_path, di_key.objectid);
  1809. btrfs_free_path(fixup_path);
  1810. }
  1811. return ret;
  1812. }
  1813. /*
  1814. * directory replay has two parts. There are the standard directory
  1815. * items in the log copied from the subvolume, and range items
  1816. * created in the log while the subvolume was logged.
  1817. *
  1818. * The range items tell us which parts of the key space the log
  1819. * is authoritative for. During replay, if a key in the subvolume
  1820. * directory is in a logged range item, but not actually in the log
  1821. * that means it was deleted from the directory before the fsync
  1822. * and should be removed.
  1823. */
  1824. static noinline int find_dir_range(struct btrfs_root *root,
  1825. struct btrfs_path *path,
  1826. u64 dirid,
  1827. u64 *start_ret, u64 *end_ret)
  1828. {
  1829. struct btrfs_key key;
  1830. u64 found_end;
  1831. struct btrfs_dir_log_item *item;
  1832. int ret;
  1833. int nritems;
  1834. if (*start_ret == (u64)-1)
  1835. return 1;
  1836. key.objectid = dirid;
  1837. key.type = BTRFS_DIR_LOG_INDEX_KEY;
  1838. key.offset = *start_ret;
  1839. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1840. if (ret < 0)
  1841. goto out;
  1842. if (ret > 0) {
  1843. if (path->slots[0] == 0)
  1844. goto out;
  1845. path->slots[0]--;
  1846. }
  1847. if (ret != 0)
  1848. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1849. if (key.type != BTRFS_DIR_LOG_INDEX_KEY || key.objectid != dirid) {
  1850. ret = 1;
  1851. goto next;
  1852. }
  1853. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1854. struct btrfs_dir_log_item);
  1855. found_end = btrfs_dir_log_end(path->nodes[0], item);
  1856. if (*start_ret >= key.offset && *start_ret <= found_end) {
  1857. ret = 0;
  1858. *start_ret = key.offset;
  1859. *end_ret = found_end;
  1860. goto out;
  1861. }
  1862. ret = 1;
  1863. next:
  1864. /* check the next slot in the tree to see if it is a valid item */
  1865. nritems = btrfs_header_nritems(path->nodes[0]);
  1866. path->slots[0]++;
  1867. if (path->slots[0] >= nritems) {
  1868. ret = btrfs_next_leaf(root, path);
  1869. if (ret)
  1870. goto out;
  1871. }
  1872. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1873. if (key.type != BTRFS_DIR_LOG_INDEX_KEY || key.objectid != dirid) {
  1874. ret = 1;
  1875. goto out;
  1876. }
  1877. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1878. struct btrfs_dir_log_item);
  1879. found_end = btrfs_dir_log_end(path->nodes[0], item);
  1880. *start_ret = key.offset;
  1881. *end_ret = found_end;
  1882. ret = 0;
  1883. out:
  1884. btrfs_release_path(path);
  1885. return ret;
  1886. }
  1887. /*
  1888. * this looks for a given directory item in the log. If the directory
  1889. * item is not in the log, the item is removed and the inode it points
  1890. * to is unlinked
  1891. */
  1892. static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
  1893. struct btrfs_root *log,
  1894. struct btrfs_path *path,
  1895. struct btrfs_path *log_path,
  1896. struct btrfs_inode *dir,
  1897. struct btrfs_key *dir_key)
  1898. {
  1899. struct btrfs_root *root = dir->root;
  1900. int ret;
  1901. struct extent_buffer *eb;
  1902. int slot;
  1903. struct btrfs_dir_item *di;
  1904. struct fscrypt_str name = { 0 };
  1905. struct btrfs_inode *inode = NULL;
  1906. struct btrfs_key location;
  1907. /*
  1908. * Currently we only log dir index keys. Even if we replay a log created
  1909. * by an older kernel that logged both dir index and dir item keys, all
  1910. * we need to do is process the dir index keys, we (and our caller) can
  1911. * safely ignore dir item keys (key type BTRFS_DIR_ITEM_KEY).
  1912. */
  1913. ASSERT(dir_key->type == BTRFS_DIR_INDEX_KEY);
  1914. eb = path->nodes[0];
  1915. slot = path->slots[0];
  1916. di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
  1917. ret = read_alloc_one_name(eb, di + 1, btrfs_dir_name_len(eb, di), &name);
  1918. if (ret)
  1919. goto out;
  1920. if (log) {
  1921. struct btrfs_dir_item *log_di;
  1922. log_di = btrfs_lookup_dir_index_item(trans, log, log_path,
  1923. dir_key->objectid,
  1924. dir_key->offset, &name, 0);
  1925. if (IS_ERR(log_di)) {
  1926. ret = PTR_ERR(log_di);
  1927. goto out;
  1928. } else if (log_di) {
  1929. /* The dentry exists in the log, we have nothing to do. */
  1930. ret = 0;
  1931. goto out;
  1932. }
  1933. }
  1934. btrfs_dir_item_key_to_cpu(eb, di, &location);
  1935. btrfs_release_path(path);
  1936. btrfs_release_path(log_path);
  1937. inode = btrfs_iget_logging(location.objectid, root);
  1938. if (IS_ERR(inode)) {
  1939. ret = PTR_ERR(inode);
  1940. inode = NULL;
  1941. goto out;
  1942. }
  1943. ret = link_to_fixup_dir(trans, root, path, location.objectid);
  1944. if (ret)
  1945. goto out;
  1946. inc_nlink(&inode->vfs_inode);
  1947. ret = unlink_inode_for_log_replay(trans, dir, inode, &name);
  1948. /*
  1949. * Unlike dir item keys, dir index keys can only have one name (entry) in
  1950. * them, as there are no key collisions since each key has a unique offset
  1951. * (an index number), so we're done.
  1952. */
  1953. out:
  1954. btrfs_release_path(path);
  1955. btrfs_release_path(log_path);
  1956. kfree(name.name);
  1957. if (inode)
  1958. iput(&inode->vfs_inode);
  1959. return ret;
  1960. }
  1961. static int replay_xattr_deletes(struct btrfs_trans_handle *trans,
  1962. struct btrfs_root *root,
  1963. struct btrfs_root *log,
  1964. struct btrfs_path *path,
  1965. const u64 ino)
  1966. {
  1967. struct btrfs_key search_key;
  1968. struct btrfs_path *log_path;
  1969. int i;
  1970. int nritems;
  1971. int ret;
  1972. log_path = btrfs_alloc_path();
  1973. if (!log_path)
  1974. return -ENOMEM;
  1975. search_key.objectid = ino;
  1976. search_key.type = BTRFS_XATTR_ITEM_KEY;
  1977. search_key.offset = 0;
  1978. again:
  1979. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  1980. if (ret < 0)
  1981. goto out;
  1982. process_leaf:
  1983. nritems = btrfs_header_nritems(path->nodes[0]);
  1984. for (i = path->slots[0]; i < nritems; i++) {
  1985. struct btrfs_key key;
  1986. struct btrfs_dir_item *di;
  1987. struct btrfs_dir_item *log_di;
  1988. u32 total_size;
  1989. u32 cur;
  1990. btrfs_item_key_to_cpu(path->nodes[0], &key, i);
  1991. if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
  1992. ret = 0;
  1993. goto out;
  1994. }
  1995. di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item);
  1996. total_size = btrfs_item_size(path->nodes[0], i);
  1997. cur = 0;
  1998. while (cur < total_size) {
  1999. u16 name_len = btrfs_dir_name_len(path->nodes[0], di);
  2000. u16 data_len = btrfs_dir_data_len(path->nodes[0], di);
  2001. u32 this_len = sizeof(*di) + name_len + data_len;
  2002. char *name;
  2003. name = kmalloc(name_len, GFP_NOFS);
  2004. if (!name) {
  2005. ret = -ENOMEM;
  2006. goto out;
  2007. }
  2008. read_extent_buffer(path->nodes[0], name,
  2009. (unsigned long)(di + 1), name_len);
  2010. log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
  2011. name, name_len, 0);
  2012. btrfs_release_path(log_path);
  2013. if (!log_di) {
  2014. /* Doesn't exist in log tree, so delete it. */
  2015. btrfs_release_path(path);
  2016. di = btrfs_lookup_xattr(trans, root, path, ino,
  2017. name, name_len, -1);
  2018. kfree(name);
  2019. if (IS_ERR(di)) {
  2020. ret = PTR_ERR(di);
  2021. goto out;
  2022. }
  2023. ASSERT(di);
  2024. ret = btrfs_delete_one_dir_name(trans, root,
  2025. path, di);
  2026. if (ret)
  2027. goto out;
  2028. btrfs_release_path(path);
  2029. search_key = key;
  2030. goto again;
  2031. }
  2032. kfree(name);
  2033. if (IS_ERR(log_di)) {
  2034. ret = PTR_ERR(log_di);
  2035. goto out;
  2036. }
  2037. cur += this_len;
  2038. di = (struct btrfs_dir_item *)((char *)di + this_len);
  2039. }
  2040. }
  2041. ret = btrfs_next_leaf(root, path);
  2042. if (ret > 0)
  2043. ret = 0;
  2044. else if (ret == 0)
  2045. goto process_leaf;
  2046. out:
  2047. btrfs_free_path(log_path);
  2048. btrfs_release_path(path);
  2049. return ret;
  2050. }
  2051. /*
  2052. * deletion replay happens before we copy any new directory items
  2053. * out of the log or out of backreferences from inodes. It
  2054. * scans the log to find ranges of keys that log is authoritative for,
  2055. * and then scans the directory to find items in those ranges that are
  2056. * not present in the log.
  2057. *
  2058. * Anything we don't find in the log is unlinked and removed from the
  2059. * directory.
  2060. */
  2061. static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
  2062. struct btrfs_root *root,
  2063. struct btrfs_root *log,
  2064. struct btrfs_path *path,
  2065. u64 dirid, int del_all)
  2066. {
  2067. u64 range_start;
  2068. u64 range_end;
  2069. int ret = 0;
  2070. struct btrfs_key dir_key;
  2071. struct btrfs_key found_key;
  2072. struct btrfs_path *log_path;
  2073. struct btrfs_inode *dir;
  2074. dir_key.objectid = dirid;
  2075. dir_key.type = BTRFS_DIR_INDEX_KEY;
  2076. log_path = btrfs_alloc_path();
  2077. if (!log_path)
  2078. return -ENOMEM;
  2079. dir = btrfs_iget_logging(dirid, root);
  2080. /*
  2081. * It isn't an error if the inode isn't there, that can happen because
  2082. * we replay the deletes before we copy in the inode item from the log.
  2083. */
  2084. if (IS_ERR(dir)) {
  2085. btrfs_free_path(log_path);
  2086. ret = PTR_ERR(dir);
  2087. if (ret == -ENOENT)
  2088. ret = 0;
  2089. return ret;
  2090. }
  2091. range_start = 0;
  2092. range_end = 0;
  2093. while (1) {
  2094. if (del_all)
  2095. range_end = (u64)-1;
  2096. else {
  2097. ret = find_dir_range(log, path, dirid,
  2098. &range_start, &range_end);
  2099. if (ret < 0)
  2100. goto out;
  2101. else if (ret > 0)
  2102. break;
  2103. }
  2104. dir_key.offset = range_start;
  2105. while (1) {
  2106. int nritems;
  2107. ret = btrfs_search_slot(NULL, root, &dir_key, path,
  2108. 0, 0);
  2109. if (ret < 0)
  2110. goto out;
  2111. nritems = btrfs_header_nritems(path->nodes[0]);
  2112. if (path->slots[0] >= nritems) {
  2113. ret = btrfs_next_leaf(root, path);
  2114. if (ret == 1)
  2115. break;
  2116. else if (ret < 0)
  2117. goto out;
  2118. }
  2119. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  2120. path->slots[0]);
  2121. if (found_key.objectid != dirid ||
  2122. found_key.type != dir_key.type) {
  2123. ret = 0;
  2124. goto out;
  2125. }
  2126. if (found_key.offset > range_end)
  2127. break;
  2128. ret = check_item_in_log(trans, log, path,
  2129. log_path, dir,
  2130. &found_key);
  2131. if (ret)
  2132. goto out;
  2133. if (found_key.offset == (u64)-1)
  2134. break;
  2135. dir_key.offset = found_key.offset + 1;
  2136. }
  2137. btrfs_release_path(path);
  2138. if (range_end == (u64)-1)
  2139. break;
  2140. range_start = range_end + 1;
  2141. }
  2142. ret = 0;
  2143. out:
  2144. btrfs_release_path(path);
  2145. btrfs_free_path(log_path);
  2146. iput(&dir->vfs_inode);
  2147. return ret;
  2148. }
  2149. /*
  2150. * the process_func used to replay items from the log tree. This
  2151. * gets called in two different stages. The first stage just looks
  2152. * for inodes and makes sure they are all copied into the subvolume.
  2153. *
  2154. * The second stage copies all the other item types from the log into
  2155. * the subvolume. The two stage approach is slower, but gets rid of
  2156. * lots of complexity around inodes referencing other inodes that exist
  2157. * only in the log (references come from either directory items or inode
  2158. * back refs).
  2159. */
  2160. static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
  2161. struct walk_control *wc, u64 gen, int level)
  2162. {
  2163. int nritems;
  2164. struct btrfs_tree_parent_check check = {
  2165. .transid = gen,
  2166. .level = level
  2167. };
  2168. struct btrfs_path *path;
  2169. struct btrfs_root *root = wc->replay_dest;
  2170. struct btrfs_key key;
  2171. int i;
  2172. int ret;
  2173. ret = btrfs_read_extent_buffer(eb, &check);
  2174. if (ret)
  2175. return ret;
  2176. level = btrfs_header_level(eb);
  2177. if (level != 0)
  2178. return 0;
  2179. path = btrfs_alloc_path();
  2180. if (!path)
  2181. return -ENOMEM;
  2182. nritems = btrfs_header_nritems(eb);
  2183. for (i = 0; i < nritems; i++) {
  2184. struct btrfs_inode_item *inode_item;
  2185. btrfs_item_key_to_cpu(eb, &key, i);
  2186. if (key.type == BTRFS_INODE_ITEM_KEY) {
  2187. inode_item = btrfs_item_ptr(eb, i, struct btrfs_inode_item);
  2188. /*
  2189. * An inode with no links is either:
  2190. *
  2191. * 1) A tmpfile (O_TMPFILE) that got fsync'ed and never
  2192. * got linked before the fsync, skip it, as replaying
  2193. * it is pointless since it would be deleted later.
  2194. * We skip logging tmpfiles, but it's always possible
  2195. * we are replaying a log created with a kernel that
  2196. * used to log tmpfiles;
  2197. *
  2198. * 2) A non-tmpfile which got its last link deleted
  2199. * while holding an open fd on it and later got
  2200. * fsynced through that fd. We always log the
  2201. * parent inodes when inode->last_unlink_trans is
  2202. * set to the current transaction, so ignore all the
  2203. * inode items for this inode. We will delete the
  2204. * inode when processing the parent directory with
  2205. * replay_dir_deletes().
  2206. */
  2207. if (btrfs_inode_nlink(eb, inode_item) == 0) {
  2208. wc->ignore_cur_inode = true;
  2209. continue;
  2210. } else {
  2211. wc->ignore_cur_inode = false;
  2212. }
  2213. }
  2214. /* Inode keys are done during the first stage. */
  2215. if (key.type == BTRFS_INODE_ITEM_KEY &&
  2216. wc->stage == LOG_WALK_REPLAY_INODES) {
  2217. u32 mode;
  2218. ret = replay_xattr_deletes(wc->trans, root, log, path, key.objectid);
  2219. if (ret)
  2220. break;
  2221. mode = btrfs_inode_mode(eb, inode_item);
  2222. if (S_ISDIR(mode)) {
  2223. ret = replay_dir_deletes(wc->trans,
  2224. root, log, path, key.objectid, 0);
  2225. if (ret)
  2226. break;
  2227. }
  2228. ret = overwrite_item(wc->trans, root, path,
  2229. eb, i, &key);
  2230. if (ret)
  2231. break;
  2232. /*
  2233. * Before replaying extents, truncate the inode to its
  2234. * size. We need to do it now and not after log replay
  2235. * because before an fsync we can have prealloc extents
  2236. * added beyond the inode's i_size. If we did it after,
  2237. * through orphan cleanup for example, we would drop
  2238. * those prealloc extents just after replaying them.
  2239. */
  2240. if (S_ISREG(mode)) {
  2241. struct btrfs_drop_extents_args drop_args = { 0 };
  2242. struct btrfs_inode *inode;
  2243. u64 from;
  2244. inode = btrfs_iget_logging(key.objectid, root);
  2245. if (IS_ERR(inode)) {
  2246. ret = PTR_ERR(inode);
  2247. break;
  2248. }
  2249. from = ALIGN(i_size_read(&inode->vfs_inode),
  2250. root->fs_info->sectorsize);
  2251. drop_args.start = from;
  2252. drop_args.end = (u64)-1;
  2253. drop_args.drop_cache = true;
  2254. ret = btrfs_drop_extents(wc->trans, root, inode,
  2255. &drop_args);
  2256. if (!ret) {
  2257. inode_sub_bytes(&inode->vfs_inode,
  2258. drop_args.bytes_found);
  2259. /* Update the inode's nbytes. */
  2260. ret = btrfs_update_inode(wc->trans, inode);
  2261. }
  2262. iput(&inode->vfs_inode);
  2263. if (ret)
  2264. break;
  2265. }
  2266. ret = link_to_fixup_dir(wc->trans, root,
  2267. path, key.objectid);
  2268. if (ret)
  2269. break;
  2270. }
  2271. if (wc->ignore_cur_inode)
  2272. continue;
  2273. if (key.type == BTRFS_DIR_INDEX_KEY &&
  2274. wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
  2275. ret = replay_one_dir_item(wc->trans, root, path,
  2276. eb, i, &key);
  2277. if (ret)
  2278. break;
  2279. }
  2280. if (wc->stage < LOG_WALK_REPLAY_ALL)
  2281. continue;
  2282. /* these keys are simply copied */
  2283. if (key.type == BTRFS_XATTR_ITEM_KEY) {
  2284. ret = overwrite_item(wc->trans, root, path,
  2285. eb, i, &key);
  2286. if (ret)
  2287. break;
  2288. } else if (key.type == BTRFS_INODE_REF_KEY ||
  2289. key.type == BTRFS_INODE_EXTREF_KEY) {
  2290. ret = add_inode_ref(wc->trans, root, log, path,
  2291. eb, i, &key);
  2292. if (ret)
  2293. break;
  2294. } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
  2295. ret = replay_one_extent(wc->trans, root, path,
  2296. eb, i, &key);
  2297. if (ret)
  2298. break;
  2299. }
  2300. /*
  2301. * We don't log BTRFS_DIR_ITEM_KEY keys anymore, only the
  2302. * BTRFS_DIR_INDEX_KEY items which we use to derive the
  2303. * BTRFS_DIR_ITEM_KEY items. If we are replaying a log from an
  2304. * older kernel with such keys, ignore them.
  2305. */
  2306. }
  2307. btrfs_free_path(path);
  2308. return ret;
  2309. }
  2310. /*
  2311. * Correctly adjust the reserved bytes occupied by a log tree extent buffer
  2312. */
  2313. static int unaccount_log_buffer(struct btrfs_fs_info *fs_info, u64 start)
  2314. {
  2315. struct btrfs_block_group *cache;
  2316. cache = btrfs_lookup_block_group(fs_info, start);
  2317. if (!cache) {
  2318. btrfs_err(fs_info, "unable to find block group for %llu", start);
  2319. return -ENOENT;
  2320. }
  2321. spin_lock(&cache->space_info->lock);
  2322. spin_lock(&cache->lock);
  2323. cache->reserved -= fs_info->nodesize;
  2324. cache->space_info->bytes_reserved -= fs_info->nodesize;
  2325. spin_unlock(&cache->lock);
  2326. spin_unlock(&cache->space_info->lock);
  2327. btrfs_put_block_group(cache);
  2328. return 0;
  2329. }
  2330. static int clean_log_buffer(struct btrfs_trans_handle *trans,
  2331. struct extent_buffer *eb)
  2332. {
  2333. btrfs_tree_lock(eb);
  2334. btrfs_clear_buffer_dirty(trans, eb);
  2335. wait_on_extent_buffer_writeback(eb);
  2336. btrfs_tree_unlock(eb);
  2337. if (trans)
  2338. return btrfs_pin_reserved_extent(trans, eb);
  2339. return unaccount_log_buffer(eb->fs_info, eb->start);
  2340. }
  2341. static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
  2342. struct btrfs_root *root,
  2343. struct btrfs_path *path, int *level,
  2344. struct walk_control *wc)
  2345. {
  2346. struct btrfs_fs_info *fs_info = root->fs_info;
  2347. u64 bytenr;
  2348. u64 ptr_gen;
  2349. struct extent_buffer *next;
  2350. struct extent_buffer *cur;
  2351. int ret = 0;
  2352. while (*level > 0) {
  2353. struct btrfs_tree_parent_check check = { 0 };
  2354. cur = path->nodes[*level];
  2355. WARN_ON(btrfs_header_level(cur) != *level);
  2356. if (path->slots[*level] >=
  2357. btrfs_header_nritems(cur))
  2358. break;
  2359. bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
  2360. ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
  2361. check.transid = ptr_gen;
  2362. check.level = *level - 1;
  2363. check.has_first_key = true;
  2364. btrfs_node_key_to_cpu(cur, &check.first_key, path->slots[*level]);
  2365. next = btrfs_find_create_tree_block(fs_info, bytenr,
  2366. btrfs_header_owner(cur),
  2367. *level - 1);
  2368. if (IS_ERR(next))
  2369. return PTR_ERR(next);
  2370. if (*level == 1) {
  2371. ret = wc->process_func(root, next, wc, ptr_gen,
  2372. *level - 1);
  2373. if (ret) {
  2374. free_extent_buffer(next);
  2375. return ret;
  2376. }
  2377. path->slots[*level]++;
  2378. if (wc->free) {
  2379. ret = btrfs_read_extent_buffer(next, &check);
  2380. if (ret) {
  2381. free_extent_buffer(next);
  2382. return ret;
  2383. }
  2384. ret = clean_log_buffer(trans, next);
  2385. if (ret) {
  2386. free_extent_buffer(next);
  2387. return ret;
  2388. }
  2389. }
  2390. free_extent_buffer(next);
  2391. continue;
  2392. }
  2393. ret = btrfs_read_extent_buffer(next, &check);
  2394. if (ret) {
  2395. free_extent_buffer(next);
  2396. return ret;
  2397. }
  2398. if (path->nodes[*level-1])
  2399. free_extent_buffer(path->nodes[*level-1]);
  2400. path->nodes[*level-1] = next;
  2401. *level = btrfs_header_level(next);
  2402. path->slots[*level] = 0;
  2403. cond_resched();
  2404. }
  2405. path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
  2406. cond_resched();
  2407. return 0;
  2408. }
  2409. static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
  2410. struct btrfs_root *root,
  2411. struct btrfs_path *path, int *level,
  2412. struct walk_control *wc)
  2413. {
  2414. int i;
  2415. int slot;
  2416. int ret;
  2417. for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
  2418. slot = path->slots[i];
  2419. if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
  2420. path->slots[i]++;
  2421. *level = i;
  2422. WARN_ON(*level == 0);
  2423. return 0;
  2424. } else {
  2425. ret = wc->process_func(root, path->nodes[*level], wc,
  2426. btrfs_header_generation(path->nodes[*level]),
  2427. *level);
  2428. if (ret)
  2429. return ret;
  2430. if (wc->free) {
  2431. ret = clean_log_buffer(trans, path->nodes[*level]);
  2432. if (ret)
  2433. return ret;
  2434. }
  2435. free_extent_buffer(path->nodes[*level]);
  2436. path->nodes[*level] = NULL;
  2437. *level = i + 1;
  2438. }
  2439. }
  2440. return 1;
  2441. }
  2442. /*
  2443. * drop the reference count on the tree rooted at 'snap'. This traverses
  2444. * the tree freeing any blocks that have a ref count of zero after being
  2445. * decremented.
  2446. */
  2447. static int walk_log_tree(struct btrfs_trans_handle *trans,
  2448. struct btrfs_root *log, struct walk_control *wc)
  2449. {
  2450. int ret = 0;
  2451. int wret;
  2452. int level;
  2453. struct btrfs_path *path;
  2454. int orig_level;
  2455. path = btrfs_alloc_path();
  2456. if (!path)
  2457. return -ENOMEM;
  2458. level = btrfs_header_level(log->node);
  2459. orig_level = level;
  2460. path->nodes[level] = log->node;
  2461. atomic_inc(&log->node->refs);
  2462. path->slots[level] = 0;
  2463. while (1) {
  2464. wret = walk_down_log_tree(trans, log, path, &level, wc);
  2465. if (wret > 0)
  2466. break;
  2467. if (wret < 0) {
  2468. ret = wret;
  2469. goto out;
  2470. }
  2471. wret = walk_up_log_tree(trans, log, path, &level, wc);
  2472. if (wret > 0)
  2473. break;
  2474. if (wret < 0) {
  2475. ret = wret;
  2476. goto out;
  2477. }
  2478. }
  2479. /* was the root node processed? if not, catch it here */
  2480. if (path->nodes[orig_level]) {
  2481. ret = wc->process_func(log, path->nodes[orig_level], wc,
  2482. btrfs_header_generation(path->nodes[orig_level]),
  2483. orig_level);
  2484. if (ret)
  2485. goto out;
  2486. if (wc->free)
  2487. ret = clean_log_buffer(trans, path->nodes[orig_level]);
  2488. }
  2489. out:
  2490. btrfs_free_path(path);
  2491. return ret;
  2492. }
  2493. /*
  2494. * helper function to update the item for a given subvolumes log root
  2495. * in the tree of log roots
  2496. */
  2497. static int update_log_root(struct btrfs_trans_handle *trans,
  2498. struct btrfs_root *log,
  2499. struct btrfs_root_item *root_item)
  2500. {
  2501. struct btrfs_fs_info *fs_info = log->fs_info;
  2502. int ret;
  2503. if (log->log_transid == 1) {
  2504. /* insert root item on the first sync */
  2505. ret = btrfs_insert_root(trans, fs_info->log_root_tree,
  2506. &log->root_key, root_item);
  2507. } else {
  2508. ret = btrfs_update_root(trans, fs_info->log_root_tree,
  2509. &log->root_key, root_item);
  2510. }
  2511. return ret;
  2512. }
  2513. static void wait_log_commit(struct btrfs_root *root, int transid)
  2514. {
  2515. DEFINE_WAIT(wait);
  2516. int index = transid % 2;
  2517. /*
  2518. * we only allow two pending log transactions at a time,
  2519. * so we know that if ours is more than 2 older than the
  2520. * current transaction, we're done
  2521. */
  2522. for (;;) {
  2523. prepare_to_wait(&root->log_commit_wait[index],
  2524. &wait, TASK_UNINTERRUPTIBLE);
  2525. if (!(root->log_transid_committed < transid &&
  2526. atomic_read(&root->log_commit[index])))
  2527. break;
  2528. mutex_unlock(&root->log_mutex);
  2529. schedule();
  2530. mutex_lock(&root->log_mutex);
  2531. }
  2532. finish_wait(&root->log_commit_wait[index], &wait);
  2533. }
  2534. static void wait_for_writer(struct btrfs_root *root)
  2535. {
  2536. DEFINE_WAIT(wait);
  2537. for (;;) {
  2538. prepare_to_wait(&root->log_writer_wait, &wait,
  2539. TASK_UNINTERRUPTIBLE);
  2540. if (!atomic_read(&root->log_writers))
  2541. break;
  2542. mutex_unlock(&root->log_mutex);
  2543. schedule();
  2544. mutex_lock(&root->log_mutex);
  2545. }
  2546. finish_wait(&root->log_writer_wait, &wait);
  2547. }
  2548. void btrfs_init_log_ctx(struct btrfs_log_ctx *ctx, struct btrfs_inode *inode)
  2549. {
  2550. ctx->log_ret = 0;
  2551. ctx->log_transid = 0;
  2552. ctx->log_new_dentries = false;
  2553. ctx->logging_new_name = false;
  2554. ctx->logging_new_delayed_dentries = false;
  2555. ctx->logged_before = false;
  2556. ctx->inode = inode;
  2557. INIT_LIST_HEAD(&ctx->list);
  2558. INIT_LIST_HEAD(&ctx->ordered_extents);
  2559. INIT_LIST_HEAD(&ctx->conflict_inodes);
  2560. ctx->num_conflict_inodes = 0;
  2561. ctx->logging_conflict_inodes = false;
  2562. ctx->scratch_eb = NULL;
  2563. }
  2564. void btrfs_init_log_ctx_scratch_eb(struct btrfs_log_ctx *ctx)
  2565. {
  2566. struct btrfs_inode *inode = ctx->inode;
  2567. if (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags) &&
  2568. !test_bit(BTRFS_INODE_COPY_EVERYTHING, &inode->runtime_flags))
  2569. return;
  2570. /*
  2571. * Don't care about allocation failure. This is just for optimization,
  2572. * if we fail to allocate here, we will try again later if needed.
  2573. */
  2574. ctx->scratch_eb = alloc_dummy_extent_buffer(inode->root->fs_info, 0);
  2575. }
  2576. void btrfs_release_log_ctx_extents(struct btrfs_log_ctx *ctx)
  2577. {
  2578. struct btrfs_ordered_extent *ordered;
  2579. struct btrfs_ordered_extent *tmp;
  2580. btrfs_assert_inode_locked(ctx->inode);
  2581. list_for_each_entry_safe(ordered, tmp, &ctx->ordered_extents, log_list) {
  2582. list_del_init(&ordered->log_list);
  2583. btrfs_put_ordered_extent(ordered);
  2584. }
  2585. }
  2586. static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
  2587. struct btrfs_log_ctx *ctx)
  2588. {
  2589. mutex_lock(&root->log_mutex);
  2590. list_del_init(&ctx->list);
  2591. mutex_unlock(&root->log_mutex);
  2592. }
  2593. /*
  2594. * Invoked in log mutex context, or be sure there is no other task which
  2595. * can access the list.
  2596. */
  2597. static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
  2598. int index, int error)
  2599. {
  2600. struct btrfs_log_ctx *ctx;
  2601. struct btrfs_log_ctx *safe;
  2602. list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
  2603. list_del_init(&ctx->list);
  2604. ctx->log_ret = error;
  2605. }
  2606. }
  2607. /*
  2608. * Sends a given tree log down to the disk and updates the super blocks to
  2609. * record it. When this call is done, you know that any inodes previously
  2610. * logged are safely on disk only if it returns 0.
  2611. *
  2612. * Any other return value means you need to call btrfs_commit_transaction.
  2613. * Some of the edge cases for fsyncing directories that have had unlinks
  2614. * or renames done in the past mean that sometimes the only safe
  2615. * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
  2616. * that has happened.
  2617. */
  2618. int btrfs_sync_log(struct btrfs_trans_handle *trans,
  2619. struct btrfs_root *root, struct btrfs_log_ctx *ctx)
  2620. {
  2621. int index1;
  2622. int index2;
  2623. int mark;
  2624. int ret;
  2625. struct btrfs_fs_info *fs_info = root->fs_info;
  2626. struct btrfs_root *log = root->log_root;
  2627. struct btrfs_root *log_root_tree = fs_info->log_root_tree;
  2628. struct btrfs_root_item new_root_item;
  2629. int log_transid = 0;
  2630. struct btrfs_log_ctx root_log_ctx;
  2631. struct blk_plug plug;
  2632. u64 log_root_start;
  2633. u64 log_root_level;
  2634. mutex_lock(&root->log_mutex);
  2635. log_transid = ctx->log_transid;
  2636. if (root->log_transid_committed >= log_transid) {
  2637. mutex_unlock(&root->log_mutex);
  2638. return ctx->log_ret;
  2639. }
  2640. index1 = log_transid % 2;
  2641. if (atomic_read(&root->log_commit[index1])) {
  2642. wait_log_commit(root, log_transid);
  2643. mutex_unlock(&root->log_mutex);
  2644. return ctx->log_ret;
  2645. }
  2646. ASSERT(log_transid == root->log_transid);
  2647. atomic_set(&root->log_commit[index1], 1);
  2648. /* wait for previous tree log sync to complete */
  2649. if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
  2650. wait_log_commit(root, log_transid - 1);
  2651. while (1) {
  2652. int batch = atomic_read(&root->log_batch);
  2653. /* when we're on an ssd, just kick the log commit out */
  2654. if (!btrfs_test_opt(fs_info, SSD) &&
  2655. test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
  2656. mutex_unlock(&root->log_mutex);
  2657. schedule_timeout_uninterruptible(1);
  2658. mutex_lock(&root->log_mutex);
  2659. }
  2660. wait_for_writer(root);
  2661. if (batch == atomic_read(&root->log_batch))
  2662. break;
  2663. }
  2664. /* bail out if we need to do a full commit */
  2665. if (btrfs_need_log_full_commit(trans)) {
  2666. ret = BTRFS_LOG_FORCE_COMMIT;
  2667. mutex_unlock(&root->log_mutex);
  2668. goto out;
  2669. }
  2670. if (log_transid % 2 == 0)
  2671. mark = EXTENT_DIRTY;
  2672. else
  2673. mark = EXTENT_NEW;
  2674. /* we start IO on all the marked extents here, but we don't actually
  2675. * wait for them until later.
  2676. */
  2677. blk_start_plug(&plug);
  2678. ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark);
  2679. /*
  2680. * -EAGAIN happens when someone, e.g., a concurrent transaction
  2681. * commit, writes a dirty extent in this tree-log commit. This
  2682. * concurrent write will create a hole writing out the extents,
  2683. * and we cannot proceed on a zoned filesystem, requiring
  2684. * sequential writing. While we can bail out to a full commit
  2685. * here, but we can continue hoping the concurrent writing fills
  2686. * the hole.
  2687. */
  2688. if (ret == -EAGAIN && btrfs_is_zoned(fs_info))
  2689. ret = 0;
  2690. if (ret) {
  2691. blk_finish_plug(&plug);
  2692. btrfs_set_log_full_commit(trans);
  2693. mutex_unlock(&root->log_mutex);
  2694. goto out;
  2695. }
  2696. /*
  2697. * We _must_ update under the root->log_mutex in order to make sure we
  2698. * have a consistent view of the log root we are trying to commit at
  2699. * this moment.
  2700. *
  2701. * We _must_ copy this into a local copy, because we are not holding the
  2702. * log_root_tree->log_mutex yet. This is important because when we
  2703. * commit the log_root_tree we must have a consistent view of the
  2704. * log_root_tree when we update the super block to point at the
  2705. * log_root_tree bytenr. If we update the log_root_tree here we'll race
  2706. * with the commit and possibly point at the new block which we may not
  2707. * have written out.
  2708. */
  2709. btrfs_set_root_node(&log->root_item, log->node);
  2710. memcpy(&new_root_item, &log->root_item, sizeof(new_root_item));
  2711. btrfs_set_root_log_transid(root, root->log_transid + 1);
  2712. log->log_transid = root->log_transid;
  2713. root->log_start_pid = 0;
  2714. /*
  2715. * IO has been started, blocks of the log tree have WRITTEN flag set
  2716. * in their headers. new modifications of the log will be written to
  2717. * new positions. so it's safe to allow log writers to go in.
  2718. */
  2719. mutex_unlock(&root->log_mutex);
  2720. if (btrfs_is_zoned(fs_info)) {
  2721. mutex_lock(&fs_info->tree_root->log_mutex);
  2722. if (!log_root_tree->node) {
  2723. ret = btrfs_alloc_log_tree_node(trans, log_root_tree);
  2724. if (ret) {
  2725. mutex_unlock(&fs_info->tree_root->log_mutex);
  2726. blk_finish_plug(&plug);
  2727. goto out;
  2728. }
  2729. }
  2730. mutex_unlock(&fs_info->tree_root->log_mutex);
  2731. }
  2732. btrfs_init_log_ctx(&root_log_ctx, NULL);
  2733. mutex_lock(&log_root_tree->log_mutex);
  2734. index2 = log_root_tree->log_transid % 2;
  2735. list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
  2736. root_log_ctx.log_transid = log_root_tree->log_transid;
  2737. /*
  2738. * Now we are safe to update the log_root_tree because we're under the
  2739. * log_mutex, and we're a current writer so we're holding the commit
  2740. * open until we drop the log_mutex.
  2741. */
  2742. ret = update_log_root(trans, log, &new_root_item);
  2743. if (ret) {
  2744. list_del_init(&root_log_ctx.list);
  2745. blk_finish_plug(&plug);
  2746. btrfs_set_log_full_commit(trans);
  2747. if (ret != -ENOSPC)
  2748. btrfs_err(fs_info,
  2749. "failed to update log for root %llu ret %d",
  2750. btrfs_root_id(root), ret);
  2751. btrfs_wait_tree_log_extents(log, mark);
  2752. mutex_unlock(&log_root_tree->log_mutex);
  2753. goto out;
  2754. }
  2755. if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
  2756. blk_finish_plug(&plug);
  2757. list_del_init(&root_log_ctx.list);
  2758. mutex_unlock(&log_root_tree->log_mutex);
  2759. ret = root_log_ctx.log_ret;
  2760. goto out;
  2761. }
  2762. if (atomic_read(&log_root_tree->log_commit[index2])) {
  2763. blk_finish_plug(&plug);
  2764. ret = btrfs_wait_tree_log_extents(log, mark);
  2765. wait_log_commit(log_root_tree,
  2766. root_log_ctx.log_transid);
  2767. mutex_unlock(&log_root_tree->log_mutex);
  2768. if (!ret)
  2769. ret = root_log_ctx.log_ret;
  2770. goto out;
  2771. }
  2772. ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
  2773. atomic_set(&log_root_tree->log_commit[index2], 1);
  2774. if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
  2775. wait_log_commit(log_root_tree,
  2776. root_log_ctx.log_transid - 1);
  2777. }
  2778. /*
  2779. * now that we've moved on to the tree of log tree roots,
  2780. * check the full commit flag again
  2781. */
  2782. if (btrfs_need_log_full_commit(trans)) {
  2783. blk_finish_plug(&plug);
  2784. btrfs_wait_tree_log_extents(log, mark);
  2785. mutex_unlock(&log_root_tree->log_mutex);
  2786. ret = BTRFS_LOG_FORCE_COMMIT;
  2787. goto out_wake_log_root;
  2788. }
  2789. ret = btrfs_write_marked_extents(fs_info,
  2790. &log_root_tree->dirty_log_pages,
  2791. EXTENT_DIRTY | EXTENT_NEW);
  2792. blk_finish_plug(&plug);
  2793. /*
  2794. * As described above, -EAGAIN indicates a hole in the extents. We
  2795. * cannot wait for these write outs since the waiting cause a
  2796. * deadlock. Bail out to the full commit instead.
  2797. */
  2798. if (ret == -EAGAIN && btrfs_is_zoned(fs_info)) {
  2799. btrfs_set_log_full_commit(trans);
  2800. btrfs_wait_tree_log_extents(log, mark);
  2801. mutex_unlock(&log_root_tree->log_mutex);
  2802. goto out_wake_log_root;
  2803. } else if (ret) {
  2804. btrfs_set_log_full_commit(trans);
  2805. mutex_unlock(&log_root_tree->log_mutex);
  2806. goto out_wake_log_root;
  2807. }
  2808. ret = btrfs_wait_tree_log_extents(log, mark);
  2809. if (!ret)
  2810. ret = btrfs_wait_tree_log_extents(log_root_tree,
  2811. EXTENT_NEW | EXTENT_DIRTY);
  2812. if (ret) {
  2813. btrfs_set_log_full_commit(trans);
  2814. mutex_unlock(&log_root_tree->log_mutex);
  2815. goto out_wake_log_root;
  2816. }
  2817. log_root_start = log_root_tree->node->start;
  2818. log_root_level = btrfs_header_level(log_root_tree->node);
  2819. log_root_tree->log_transid++;
  2820. mutex_unlock(&log_root_tree->log_mutex);
  2821. /*
  2822. * Here we are guaranteed that nobody is going to write the superblock
  2823. * for the current transaction before us and that neither we do write
  2824. * our superblock before the previous transaction finishes its commit
  2825. * and writes its superblock, because:
  2826. *
  2827. * 1) We are holding a handle on the current transaction, so no body
  2828. * can commit it until we release the handle;
  2829. *
  2830. * 2) Before writing our superblock we acquire the tree_log_mutex, so
  2831. * if the previous transaction is still committing, and hasn't yet
  2832. * written its superblock, we wait for it to do it, because a
  2833. * transaction commit acquires the tree_log_mutex when the commit
  2834. * begins and releases it only after writing its superblock.
  2835. */
  2836. mutex_lock(&fs_info->tree_log_mutex);
  2837. /*
  2838. * The previous transaction writeout phase could have failed, and thus
  2839. * marked the fs in an error state. We must not commit here, as we
  2840. * could have updated our generation in the super_for_commit and
  2841. * writing the super here would result in transid mismatches. If there
  2842. * is an error here just bail.
  2843. */
  2844. if (BTRFS_FS_ERROR(fs_info)) {
  2845. ret = -EIO;
  2846. btrfs_set_log_full_commit(trans);
  2847. btrfs_abort_transaction(trans, ret);
  2848. mutex_unlock(&fs_info->tree_log_mutex);
  2849. goto out_wake_log_root;
  2850. }
  2851. btrfs_set_super_log_root(fs_info->super_for_commit, log_root_start);
  2852. btrfs_set_super_log_root_level(fs_info->super_for_commit, log_root_level);
  2853. ret = write_all_supers(fs_info, 1);
  2854. mutex_unlock(&fs_info->tree_log_mutex);
  2855. if (ret) {
  2856. btrfs_set_log_full_commit(trans);
  2857. btrfs_abort_transaction(trans, ret);
  2858. goto out_wake_log_root;
  2859. }
  2860. /*
  2861. * We know there can only be one task here, since we have not yet set
  2862. * root->log_commit[index1] to 0 and any task attempting to sync the
  2863. * log must wait for the previous log transaction to commit if it's
  2864. * still in progress or wait for the current log transaction commit if
  2865. * someone else already started it. We use <= and not < because the
  2866. * first log transaction has an ID of 0.
  2867. */
  2868. ASSERT(btrfs_get_root_last_log_commit(root) <= log_transid);
  2869. btrfs_set_root_last_log_commit(root, log_transid);
  2870. out_wake_log_root:
  2871. mutex_lock(&log_root_tree->log_mutex);
  2872. btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
  2873. log_root_tree->log_transid_committed++;
  2874. atomic_set(&log_root_tree->log_commit[index2], 0);
  2875. mutex_unlock(&log_root_tree->log_mutex);
  2876. /*
  2877. * The barrier before waitqueue_active (in cond_wake_up) is needed so
  2878. * all the updates above are seen by the woken threads. It might not be
  2879. * necessary, but proving that seems to be hard.
  2880. */
  2881. cond_wake_up(&log_root_tree->log_commit_wait[index2]);
  2882. out:
  2883. mutex_lock(&root->log_mutex);
  2884. btrfs_remove_all_log_ctxs(root, index1, ret);
  2885. root->log_transid_committed++;
  2886. atomic_set(&root->log_commit[index1], 0);
  2887. mutex_unlock(&root->log_mutex);
  2888. /*
  2889. * The barrier before waitqueue_active (in cond_wake_up) is needed so
  2890. * all the updates above are seen by the woken threads. It might not be
  2891. * necessary, but proving that seems to be hard.
  2892. */
  2893. cond_wake_up(&root->log_commit_wait[index1]);
  2894. return ret;
  2895. }
  2896. static void free_log_tree(struct btrfs_trans_handle *trans,
  2897. struct btrfs_root *log)
  2898. {
  2899. int ret;
  2900. struct walk_control wc = {
  2901. .free = 1,
  2902. .process_func = process_one_buffer
  2903. };
  2904. if (log->node) {
  2905. ret = walk_log_tree(trans, log, &wc);
  2906. if (ret) {
  2907. /*
  2908. * We weren't able to traverse the entire log tree, the
  2909. * typical scenario is getting an -EIO when reading an
  2910. * extent buffer of the tree, due to a previous writeback
  2911. * failure of it.
  2912. */
  2913. set_bit(BTRFS_FS_STATE_LOG_CLEANUP_ERROR,
  2914. &log->fs_info->fs_state);
  2915. /*
  2916. * Some extent buffers of the log tree may still be dirty
  2917. * and not yet written back to storage, because we may
  2918. * have updates to a log tree without syncing a log tree,
  2919. * such as during rename and link operations. So flush
  2920. * them out and wait for their writeback to complete, so
  2921. * that we properly cleanup their state and pages.
  2922. */
  2923. btrfs_write_marked_extents(log->fs_info,
  2924. &log->dirty_log_pages,
  2925. EXTENT_DIRTY | EXTENT_NEW);
  2926. btrfs_wait_tree_log_extents(log,
  2927. EXTENT_DIRTY | EXTENT_NEW);
  2928. if (trans)
  2929. btrfs_abort_transaction(trans, ret);
  2930. else
  2931. btrfs_handle_fs_error(log->fs_info, ret, NULL);
  2932. }
  2933. }
  2934. extent_io_tree_release(&log->dirty_log_pages);
  2935. extent_io_tree_release(&log->log_csum_range);
  2936. btrfs_put_root(log);
  2937. }
  2938. /*
  2939. * free all the extents used by the tree log. This should be called
  2940. * at commit time of the full transaction
  2941. */
  2942. int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
  2943. {
  2944. if (root->log_root) {
  2945. free_log_tree(trans, root->log_root);
  2946. root->log_root = NULL;
  2947. clear_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state);
  2948. }
  2949. return 0;
  2950. }
  2951. int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
  2952. struct btrfs_fs_info *fs_info)
  2953. {
  2954. if (fs_info->log_root_tree) {
  2955. free_log_tree(trans, fs_info->log_root_tree);
  2956. fs_info->log_root_tree = NULL;
  2957. clear_bit(BTRFS_ROOT_HAS_LOG_TREE, &fs_info->tree_root->state);
  2958. }
  2959. return 0;
  2960. }
  2961. static bool mark_inode_as_not_logged(const struct btrfs_trans_handle *trans,
  2962. struct btrfs_inode *inode)
  2963. {
  2964. bool ret = false;
  2965. /*
  2966. * Do this only if ->logged_trans is still 0 to prevent races with
  2967. * concurrent logging as we may see the inode not logged when
  2968. * inode_logged() is called but it gets logged after inode_logged() did
  2969. * not find it in the log tree and we end up setting ->logged_trans to a
  2970. * value less than trans->transid after the concurrent logging task has
  2971. * set it to trans->transid. As a consequence, subsequent rename, unlink
  2972. * and link operations may end up not logging new names and removing old
  2973. * names from the log.
  2974. */
  2975. spin_lock(&inode->lock);
  2976. if (inode->logged_trans == 0)
  2977. inode->logged_trans = trans->transid - 1;
  2978. else if (inode->logged_trans == trans->transid)
  2979. ret = true;
  2980. spin_unlock(&inode->lock);
  2981. return ret;
  2982. }
  2983. /*
  2984. * Check if an inode was logged in the current transaction. This correctly deals
  2985. * with the case where the inode was logged but has a logged_trans of 0, which
  2986. * happens if the inode is evicted and loaded again, as logged_trans is an in
  2987. * memory only field (not persisted).
  2988. *
  2989. * Returns 1 if the inode was logged before in the transaction, 0 if it was not,
  2990. * and < 0 on error.
  2991. */
  2992. static int inode_logged(const struct btrfs_trans_handle *trans,
  2993. struct btrfs_inode *inode,
  2994. struct btrfs_path *path_in)
  2995. {
  2996. struct btrfs_path *path = path_in;
  2997. struct btrfs_key key;
  2998. int ret;
  2999. /*
  3000. * Quick lockless call, since once ->logged_trans is set to the current
  3001. * transaction, we never set it to a lower value anywhere else.
  3002. */
  3003. if (data_race(inode->logged_trans) == trans->transid)
  3004. return 1;
  3005. /*
  3006. * If logged_trans is not 0 and not trans->transid, then we know the
  3007. * inode was not logged in this transaction, so we can return false
  3008. * right away. We take the lock to avoid a race caused by load/store
  3009. * tearing with a concurrent btrfs_log_inode() call or a concurrent task
  3010. * in this function further below - an update to trans->transid can be
  3011. * teared into two 32 bits updates for example, in which case we could
  3012. * see a positive value that is not trans->transid and assume the inode
  3013. * was not logged when it was.
  3014. */
  3015. spin_lock(&inode->lock);
  3016. if (inode->logged_trans == trans->transid) {
  3017. spin_unlock(&inode->lock);
  3018. return 1;
  3019. } else if (inode->logged_trans > 0) {
  3020. spin_unlock(&inode->lock);
  3021. return 0;
  3022. }
  3023. spin_unlock(&inode->lock);
  3024. /*
  3025. * If no log tree was created for this root in this transaction, then
  3026. * the inode can not have been logged in this transaction. In that case
  3027. * set logged_trans to anything greater than 0 and less than the current
  3028. * transaction's ID, to avoid the search below in a future call in case
  3029. * a log tree gets created after this.
  3030. */
  3031. if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &inode->root->state))
  3032. return mark_inode_as_not_logged(trans, inode);
  3033. /*
  3034. * We have a log tree and the inode's logged_trans is 0. We can't tell
  3035. * for sure if the inode was logged before in this transaction by looking
  3036. * only at logged_trans. We could be pessimistic and assume it was, but
  3037. * that can lead to unnecessarily logging an inode during rename and link
  3038. * operations, and then further updating the log in followup rename and
  3039. * link operations, specially if it's a directory, which adds latency
  3040. * visible to applications doing a series of rename or link operations.
  3041. *
  3042. * A logged_trans of 0 here can mean several things:
  3043. *
  3044. * 1) The inode was never logged since the filesystem was mounted, and may
  3045. * or may have not been evicted and loaded again;
  3046. *
  3047. * 2) The inode was logged in a previous transaction, then evicted and
  3048. * then loaded again;
  3049. *
  3050. * 3) The inode was logged in the current transaction, then evicted and
  3051. * then loaded again.
  3052. *
  3053. * For cases 1) and 2) we don't want to return true, but we need to detect
  3054. * case 3) and return true. So we do a search in the log root for the inode
  3055. * item.
  3056. */
  3057. key.objectid = btrfs_ino(inode);
  3058. key.type = BTRFS_INODE_ITEM_KEY;
  3059. key.offset = 0;
  3060. if (!path) {
  3061. path = btrfs_alloc_path();
  3062. if (!path)
  3063. return -ENOMEM;
  3064. }
  3065. ret = btrfs_search_slot(NULL, inode->root->log_root, &key, path, 0, 0);
  3066. if (path_in)
  3067. btrfs_release_path(path);
  3068. else
  3069. btrfs_free_path(path);
  3070. /*
  3071. * Logging an inode always results in logging its inode item. So if we
  3072. * did not find the item we know the inode was not logged for sure.
  3073. */
  3074. if (ret < 0) {
  3075. return ret;
  3076. } else if (ret > 0) {
  3077. /*
  3078. * Set logged_trans to a value greater than 0 and less then the
  3079. * current transaction to avoid doing the search in future calls.
  3080. */
  3081. return mark_inode_as_not_logged(trans, inode);
  3082. }
  3083. /*
  3084. * The inode was previously logged and then evicted, set logged_trans to
  3085. * the current transacion's ID, to avoid future tree searches as long as
  3086. * the inode is not evicted again.
  3087. */
  3088. spin_lock(&inode->lock);
  3089. inode->logged_trans = trans->transid;
  3090. spin_unlock(&inode->lock);
  3091. return 1;
  3092. }
  3093. /*
  3094. * Delete a directory entry from the log if it exists.
  3095. *
  3096. * Returns < 0 on error
  3097. * 1 if the entry does not exists
  3098. * 0 if the entry existed and was successfully deleted
  3099. */
  3100. static int del_logged_dentry(struct btrfs_trans_handle *trans,
  3101. struct btrfs_root *log,
  3102. struct btrfs_path *path,
  3103. u64 dir_ino,
  3104. const struct fscrypt_str *name,
  3105. u64 index)
  3106. {
  3107. struct btrfs_dir_item *di;
  3108. /*
  3109. * We only log dir index items of a directory, so we don't need to look
  3110. * for dir item keys.
  3111. */
  3112. di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
  3113. index, name, -1);
  3114. if (IS_ERR(di))
  3115. return PTR_ERR(di);
  3116. else if (!di)
  3117. return 1;
  3118. /*
  3119. * We do not need to update the size field of the directory's
  3120. * inode item because on log replay we update the field to reflect
  3121. * all existing entries in the directory (see overwrite_item()).
  3122. */
  3123. return btrfs_delete_one_dir_name(trans, log, path, di);
  3124. }
  3125. /*
  3126. * If both a file and directory are logged, and unlinks or renames are
  3127. * mixed in, we have a few interesting corners:
  3128. *
  3129. * create file X in dir Y
  3130. * link file X to X.link in dir Y
  3131. * fsync file X
  3132. * unlink file X but leave X.link
  3133. * fsync dir Y
  3134. *
  3135. * After a crash we would expect only X.link to exist. But file X
  3136. * didn't get fsync'd again so the log has back refs for X and X.link.
  3137. *
  3138. * We solve this by removing directory entries and inode backrefs from the
  3139. * log when a file that was logged in the current transaction is
  3140. * unlinked. Any later fsync will include the updated log entries, and
  3141. * we'll be able to reconstruct the proper directory items from backrefs.
  3142. *
  3143. * This optimizations allows us to avoid relogging the entire inode
  3144. * or the entire directory.
  3145. */
  3146. void btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
  3147. struct btrfs_root *root,
  3148. const struct fscrypt_str *name,
  3149. struct btrfs_inode *dir, u64 index)
  3150. {
  3151. struct btrfs_path *path;
  3152. int ret;
  3153. ret = inode_logged(trans, dir, NULL);
  3154. if (ret == 0)
  3155. return;
  3156. else if (ret < 0) {
  3157. btrfs_set_log_full_commit(trans);
  3158. return;
  3159. }
  3160. ret = join_running_log_trans(root);
  3161. if (ret)
  3162. return;
  3163. mutex_lock(&dir->log_mutex);
  3164. path = btrfs_alloc_path();
  3165. if (!path) {
  3166. ret = -ENOMEM;
  3167. goto out_unlock;
  3168. }
  3169. ret = del_logged_dentry(trans, root->log_root, path, btrfs_ino(dir),
  3170. name, index);
  3171. btrfs_free_path(path);
  3172. out_unlock:
  3173. mutex_unlock(&dir->log_mutex);
  3174. if (ret < 0)
  3175. btrfs_set_log_full_commit(trans);
  3176. btrfs_end_log_trans(root);
  3177. }
  3178. /* see comments for btrfs_del_dir_entries_in_log */
  3179. void btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
  3180. struct btrfs_root *root,
  3181. const struct fscrypt_str *name,
  3182. struct btrfs_inode *inode, u64 dirid)
  3183. {
  3184. struct btrfs_root *log;
  3185. u64 index;
  3186. int ret;
  3187. ret = inode_logged(trans, inode, NULL);
  3188. if (ret == 0)
  3189. return;
  3190. else if (ret < 0) {
  3191. btrfs_set_log_full_commit(trans);
  3192. return;
  3193. }
  3194. ret = join_running_log_trans(root);
  3195. if (ret)
  3196. return;
  3197. log = root->log_root;
  3198. mutex_lock(&inode->log_mutex);
  3199. ret = btrfs_del_inode_ref(trans, log, name, btrfs_ino(inode),
  3200. dirid, &index);
  3201. mutex_unlock(&inode->log_mutex);
  3202. if (ret < 0 && ret != -ENOENT)
  3203. btrfs_set_log_full_commit(trans);
  3204. btrfs_end_log_trans(root);
  3205. }
  3206. /*
  3207. * creates a range item in the log for 'dirid'. first_offset and
  3208. * last_offset tell us which parts of the key space the log should
  3209. * be considered authoritative for.
  3210. */
  3211. static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
  3212. struct btrfs_root *log,
  3213. struct btrfs_path *path,
  3214. u64 dirid,
  3215. u64 first_offset, u64 last_offset)
  3216. {
  3217. int ret;
  3218. struct btrfs_key key;
  3219. struct btrfs_dir_log_item *item;
  3220. key.objectid = dirid;
  3221. key.offset = first_offset;
  3222. key.type = BTRFS_DIR_LOG_INDEX_KEY;
  3223. ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
  3224. /*
  3225. * -EEXIST is fine and can happen sporadically when we are logging a
  3226. * directory and have concurrent insertions in the subvolume's tree for
  3227. * items from other inodes and that result in pushing off some dir items
  3228. * from one leaf to another in order to accommodate for the new items.
  3229. * This results in logging the same dir index range key.
  3230. */
  3231. if (ret && ret != -EEXIST)
  3232. return ret;
  3233. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3234. struct btrfs_dir_log_item);
  3235. if (ret == -EEXIST) {
  3236. const u64 curr_end = btrfs_dir_log_end(path->nodes[0], item);
  3237. /*
  3238. * btrfs_del_dir_entries_in_log() might have been called during
  3239. * an unlink between the initial insertion of this key and the
  3240. * current update, or we might be logging a single entry deletion
  3241. * during a rename, so set the new last_offset to the max value.
  3242. */
  3243. last_offset = max(last_offset, curr_end);
  3244. }
  3245. btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
  3246. btrfs_mark_buffer_dirty(trans, path->nodes[0]);
  3247. btrfs_release_path(path);
  3248. return 0;
  3249. }
  3250. static int flush_dir_items_batch(struct btrfs_trans_handle *trans,
  3251. struct btrfs_inode *inode,
  3252. struct extent_buffer *src,
  3253. struct btrfs_path *dst_path,
  3254. int start_slot,
  3255. int count)
  3256. {
  3257. struct btrfs_root *log = inode->root->log_root;
  3258. char *ins_data = NULL;
  3259. struct btrfs_item_batch batch;
  3260. struct extent_buffer *dst;
  3261. unsigned long src_offset;
  3262. unsigned long dst_offset;
  3263. u64 last_index;
  3264. struct btrfs_key key;
  3265. u32 item_size;
  3266. int ret;
  3267. int i;
  3268. ASSERT(count > 0);
  3269. batch.nr = count;
  3270. if (count == 1) {
  3271. btrfs_item_key_to_cpu(src, &key, start_slot);
  3272. item_size = btrfs_item_size(src, start_slot);
  3273. batch.keys = &key;
  3274. batch.data_sizes = &item_size;
  3275. batch.total_data_size = item_size;
  3276. } else {
  3277. struct btrfs_key *ins_keys;
  3278. u32 *ins_sizes;
  3279. ins_data = kmalloc(count * sizeof(u32) +
  3280. count * sizeof(struct btrfs_key), GFP_NOFS);
  3281. if (!ins_data)
  3282. return -ENOMEM;
  3283. ins_sizes = (u32 *)ins_data;
  3284. ins_keys = (struct btrfs_key *)(ins_data + count * sizeof(u32));
  3285. batch.keys = ins_keys;
  3286. batch.data_sizes = ins_sizes;
  3287. batch.total_data_size = 0;
  3288. for (i = 0; i < count; i++) {
  3289. const int slot = start_slot + i;
  3290. btrfs_item_key_to_cpu(src, &ins_keys[i], slot);
  3291. ins_sizes[i] = btrfs_item_size(src, slot);
  3292. batch.total_data_size += ins_sizes[i];
  3293. }
  3294. }
  3295. ret = btrfs_insert_empty_items(trans, log, dst_path, &batch);
  3296. if (ret)
  3297. goto out;
  3298. dst = dst_path->nodes[0];
  3299. /*
  3300. * Copy all the items in bulk, in a single copy operation. Item data is
  3301. * organized such that it's placed at the end of a leaf and from right
  3302. * to left. For example, the data for the second item ends at an offset
  3303. * that matches the offset where the data for the first item starts, the
  3304. * data for the third item ends at an offset that matches the offset
  3305. * where the data of the second items starts, and so on.
  3306. * Therefore our source and destination start offsets for copy match the
  3307. * offsets of the last items (highest slots).
  3308. */
  3309. dst_offset = btrfs_item_ptr_offset(dst, dst_path->slots[0] + count - 1);
  3310. src_offset = btrfs_item_ptr_offset(src, start_slot + count - 1);
  3311. copy_extent_buffer(dst, src, dst_offset, src_offset, batch.total_data_size);
  3312. btrfs_release_path(dst_path);
  3313. last_index = batch.keys[count - 1].offset;
  3314. ASSERT(last_index > inode->last_dir_index_offset);
  3315. /*
  3316. * If for some unexpected reason the last item's index is not greater
  3317. * than the last index we logged, warn and force a transaction commit.
  3318. */
  3319. if (WARN_ON(last_index <= inode->last_dir_index_offset))
  3320. ret = BTRFS_LOG_FORCE_COMMIT;
  3321. else
  3322. inode->last_dir_index_offset = last_index;
  3323. if (btrfs_get_first_dir_index_to_log(inode) == 0)
  3324. btrfs_set_first_dir_index_to_log(inode, batch.keys[0].offset);
  3325. out:
  3326. kfree(ins_data);
  3327. return ret;
  3328. }
  3329. static int clone_leaf(struct btrfs_path *path, struct btrfs_log_ctx *ctx)
  3330. {
  3331. const int slot = path->slots[0];
  3332. if (ctx->scratch_eb) {
  3333. copy_extent_buffer_full(ctx->scratch_eb, path->nodes[0]);
  3334. } else {
  3335. ctx->scratch_eb = btrfs_clone_extent_buffer(path->nodes[0]);
  3336. if (!ctx->scratch_eb)
  3337. return -ENOMEM;
  3338. }
  3339. btrfs_release_path(path);
  3340. path->nodes[0] = ctx->scratch_eb;
  3341. path->slots[0] = slot;
  3342. /*
  3343. * Add extra ref to scratch eb so that it is not freed when callers
  3344. * release the path, so we can reuse it later if needed.
  3345. */
  3346. atomic_inc(&ctx->scratch_eb->refs);
  3347. return 0;
  3348. }
  3349. static int process_dir_items_leaf(struct btrfs_trans_handle *trans,
  3350. struct btrfs_inode *inode,
  3351. struct btrfs_path *path,
  3352. struct btrfs_path *dst_path,
  3353. struct btrfs_log_ctx *ctx,
  3354. u64 *last_old_dentry_offset)
  3355. {
  3356. struct btrfs_root *log = inode->root->log_root;
  3357. struct extent_buffer *src;
  3358. const int nritems = btrfs_header_nritems(path->nodes[0]);
  3359. const u64 ino = btrfs_ino(inode);
  3360. bool last_found = false;
  3361. int batch_start = 0;
  3362. int batch_size = 0;
  3363. int ret;
  3364. /*
  3365. * We need to clone the leaf, release the read lock on it, and use the
  3366. * clone before modifying the log tree. See the comment at copy_items()
  3367. * about why we need to do this.
  3368. */
  3369. ret = clone_leaf(path, ctx);
  3370. if (ret < 0)
  3371. return ret;
  3372. src = path->nodes[0];
  3373. for (int i = path->slots[0]; i < nritems; i++) {
  3374. struct btrfs_dir_item *di;
  3375. struct btrfs_key key;
  3376. int ret;
  3377. btrfs_item_key_to_cpu(src, &key, i);
  3378. if (key.objectid != ino || key.type != BTRFS_DIR_INDEX_KEY) {
  3379. last_found = true;
  3380. break;
  3381. }
  3382. di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
  3383. /*
  3384. * Skip ranges of items that consist only of dir item keys created
  3385. * in past transactions. However if we find a gap, we must log a
  3386. * dir index range item for that gap, so that index keys in that
  3387. * gap are deleted during log replay.
  3388. */
  3389. if (btrfs_dir_transid(src, di) < trans->transid) {
  3390. if (key.offset > *last_old_dentry_offset + 1) {
  3391. ret = insert_dir_log_key(trans, log, dst_path,
  3392. ino, *last_old_dentry_offset + 1,
  3393. key.offset - 1);
  3394. if (ret < 0)
  3395. return ret;
  3396. }
  3397. *last_old_dentry_offset = key.offset;
  3398. continue;
  3399. }
  3400. /* If we logged this dir index item before, we can skip it. */
  3401. if (key.offset <= inode->last_dir_index_offset)
  3402. continue;
  3403. /*
  3404. * We must make sure that when we log a directory entry, the
  3405. * corresponding inode, after log replay, has a matching link
  3406. * count. For example:
  3407. *
  3408. * touch foo
  3409. * mkdir mydir
  3410. * sync
  3411. * ln foo mydir/bar
  3412. * xfs_io -c "fsync" mydir
  3413. * <crash>
  3414. * <mount fs and log replay>
  3415. *
  3416. * Would result in a fsync log that when replayed, our file inode
  3417. * would have a link count of 1, but we get two directory entries
  3418. * pointing to the same inode. After removing one of the names,
  3419. * it would not be possible to remove the other name, which
  3420. * resulted always in stale file handle errors, and would not be
  3421. * possible to rmdir the parent directory, since its i_size could
  3422. * never be decremented to the value BTRFS_EMPTY_DIR_SIZE,
  3423. * resulting in -ENOTEMPTY errors.
  3424. */
  3425. if (!ctx->log_new_dentries) {
  3426. struct btrfs_key di_key;
  3427. btrfs_dir_item_key_to_cpu(src, di, &di_key);
  3428. if (di_key.type != BTRFS_ROOT_ITEM_KEY)
  3429. ctx->log_new_dentries = true;
  3430. }
  3431. if (batch_size == 0)
  3432. batch_start = i;
  3433. batch_size++;
  3434. }
  3435. if (batch_size > 0) {
  3436. int ret;
  3437. ret = flush_dir_items_batch(trans, inode, src, dst_path,
  3438. batch_start, batch_size);
  3439. if (ret < 0)
  3440. return ret;
  3441. }
  3442. return last_found ? 1 : 0;
  3443. }
  3444. /*
  3445. * log all the items included in the current transaction for a given
  3446. * directory. This also creates the range items in the log tree required
  3447. * to replay anything deleted before the fsync
  3448. */
  3449. static noinline int log_dir_items(struct btrfs_trans_handle *trans,
  3450. struct btrfs_inode *inode,
  3451. struct btrfs_path *path,
  3452. struct btrfs_path *dst_path,
  3453. struct btrfs_log_ctx *ctx,
  3454. u64 min_offset, u64 *last_offset_ret)
  3455. {
  3456. struct btrfs_key min_key;
  3457. struct btrfs_root *root = inode->root;
  3458. struct btrfs_root *log = root->log_root;
  3459. int ret;
  3460. u64 last_old_dentry_offset = min_offset - 1;
  3461. u64 last_offset = (u64)-1;
  3462. u64 ino = btrfs_ino(inode);
  3463. min_key.objectid = ino;
  3464. min_key.type = BTRFS_DIR_INDEX_KEY;
  3465. min_key.offset = min_offset;
  3466. ret = btrfs_search_forward(root, &min_key, path, trans->transid);
  3467. /*
  3468. * we didn't find anything from this transaction, see if there
  3469. * is anything at all
  3470. */
  3471. if (ret != 0 || min_key.objectid != ino ||
  3472. min_key.type != BTRFS_DIR_INDEX_KEY) {
  3473. min_key.objectid = ino;
  3474. min_key.type = BTRFS_DIR_INDEX_KEY;
  3475. min_key.offset = (u64)-1;
  3476. btrfs_release_path(path);
  3477. ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
  3478. if (ret < 0) {
  3479. btrfs_release_path(path);
  3480. return ret;
  3481. }
  3482. ret = btrfs_previous_item(root, path, ino, BTRFS_DIR_INDEX_KEY);
  3483. /* if ret == 0 there are items for this type,
  3484. * create a range to tell us the last key of this type.
  3485. * otherwise, there are no items in this directory after
  3486. * *min_offset, and we create a range to indicate that.
  3487. */
  3488. if (ret == 0) {
  3489. struct btrfs_key tmp;
  3490. btrfs_item_key_to_cpu(path->nodes[0], &tmp,
  3491. path->slots[0]);
  3492. if (tmp.type == BTRFS_DIR_INDEX_KEY)
  3493. last_old_dentry_offset = tmp.offset;
  3494. } else if (ret > 0) {
  3495. ret = 0;
  3496. }
  3497. goto done;
  3498. }
  3499. /* go backward to find any previous key */
  3500. ret = btrfs_previous_item(root, path, ino, BTRFS_DIR_INDEX_KEY);
  3501. if (ret == 0) {
  3502. struct btrfs_key tmp;
  3503. btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
  3504. /*
  3505. * The dir index key before the first one we found that needs to
  3506. * be logged might be in a previous leaf, and there might be a
  3507. * gap between these keys, meaning that we had deletions that
  3508. * happened. So the key range item we log (key type
  3509. * BTRFS_DIR_LOG_INDEX_KEY) must cover a range that starts at the
  3510. * previous key's offset plus 1, so that those deletes are replayed.
  3511. */
  3512. if (tmp.type == BTRFS_DIR_INDEX_KEY)
  3513. last_old_dentry_offset = tmp.offset;
  3514. } else if (ret < 0) {
  3515. goto done;
  3516. }
  3517. btrfs_release_path(path);
  3518. /*
  3519. * Find the first key from this transaction again or the one we were at
  3520. * in the loop below in case we had to reschedule. We may be logging the
  3521. * directory without holding its VFS lock, which happen when logging new
  3522. * dentries (through log_new_dir_dentries()) or in some cases when we
  3523. * need to log the parent directory of an inode. This means a dir index
  3524. * key might be deleted from the inode's root, and therefore we may not
  3525. * find it anymore. If we can't find it, just move to the next key. We
  3526. * can not bail out and ignore, because if we do that we will simply
  3527. * not log dir index keys that come after the one that was just deleted
  3528. * and we can end up logging a dir index range that ends at (u64)-1
  3529. * (@last_offset is initialized to that), resulting in removing dir
  3530. * entries we should not remove at log replay time.
  3531. */
  3532. search:
  3533. ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
  3534. if (ret > 0) {
  3535. ret = btrfs_next_item(root, path);
  3536. if (ret > 0) {
  3537. /* There are no more keys in the inode's root. */
  3538. ret = 0;
  3539. goto done;
  3540. }
  3541. }
  3542. if (ret < 0)
  3543. goto done;
  3544. /*
  3545. * we have a block from this transaction, log every item in it
  3546. * from our directory
  3547. */
  3548. while (1) {
  3549. ret = process_dir_items_leaf(trans, inode, path, dst_path, ctx,
  3550. &last_old_dentry_offset);
  3551. if (ret != 0) {
  3552. if (ret > 0)
  3553. ret = 0;
  3554. goto done;
  3555. }
  3556. path->slots[0] = btrfs_header_nritems(path->nodes[0]);
  3557. /*
  3558. * look ahead to the next item and see if it is also
  3559. * from this directory and from this transaction
  3560. */
  3561. ret = btrfs_next_leaf(root, path);
  3562. if (ret) {
  3563. if (ret == 1) {
  3564. last_offset = (u64)-1;
  3565. ret = 0;
  3566. }
  3567. goto done;
  3568. }
  3569. btrfs_item_key_to_cpu(path->nodes[0], &min_key, path->slots[0]);
  3570. if (min_key.objectid != ino || min_key.type != BTRFS_DIR_INDEX_KEY) {
  3571. last_offset = (u64)-1;
  3572. goto done;
  3573. }
  3574. if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
  3575. /*
  3576. * The next leaf was not changed in the current transaction
  3577. * and has at least one dir index key.
  3578. * We check for the next key because there might have been
  3579. * one or more deletions between the last key we logged and
  3580. * that next key. So the key range item we log (key type
  3581. * BTRFS_DIR_LOG_INDEX_KEY) must end at the next key's
  3582. * offset minus 1, so that those deletes are replayed.
  3583. */
  3584. last_offset = min_key.offset - 1;
  3585. goto done;
  3586. }
  3587. if (need_resched()) {
  3588. btrfs_release_path(path);
  3589. cond_resched();
  3590. goto search;
  3591. }
  3592. }
  3593. done:
  3594. btrfs_release_path(path);
  3595. btrfs_release_path(dst_path);
  3596. if (ret == 0) {
  3597. *last_offset_ret = last_offset;
  3598. /*
  3599. * In case the leaf was changed in the current transaction but
  3600. * all its dir items are from a past transaction, the last item
  3601. * in the leaf is a dir item and there's no gap between that last
  3602. * dir item and the first one on the next leaf (which did not
  3603. * change in the current transaction), then we don't need to log
  3604. * a range, last_old_dentry_offset is == to last_offset.
  3605. */
  3606. ASSERT(last_old_dentry_offset <= last_offset);
  3607. if (last_old_dentry_offset < last_offset)
  3608. ret = insert_dir_log_key(trans, log, path, ino,
  3609. last_old_dentry_offset + 1,
  3610. last_offset);
  3611. }
  3612. return ret;
  3613. }
  3614. /*
  3615. * If the inode was logged before and it was evicted, then its
  3616. * last_dir_index_offset is 0, so we don't know the value of the last index
  3617. * key offset. If that's the case, search for it and update the inode. This
  3618. * is to avoid lookups in the log tree every time we try to insert a dir index
  3619. * key from a leaf changed in the current transaction, and to allow us to always
  3620. * do batch insertions of dir index keys.
  3621. */
  3622. static int update_last_dir_index_offset(struct btrfs_inode *inode,
  3623. struct btrfs_path *path,
  3624. const struct btrfs_log_ctx *ctx)
  3625. {
  3626. const u64 ino = btrfs_ino(inode);
  3627. struct btrfs_key key;
  3628. int ret;
  3629. lockdep_assert_held(&inode->log_mutex);
  3630. if (inode->last_dir_index_offset != 0)
  3631. return 0;
  3632. if (!ctx->logged_before) {
  3633. inode->last_dir_index_offset = BTRFS_DIR_START_INDEX - 1;
  3634. return 0;
  3635. }
  3636. key.objectid = ino;
  3637. key.type = BTRFS_DIR_INDEX_KEY;
  3638. key.offset = (u64)-1;
  3639. ret = btrfs_search_slot(NULL, inode->root->log_root, &key, path, 0, 0);
  3640. /*
  3641. * An error happened or we actually have an index key with an offset
  3642. * value of (u64)-1. Bail out, we're done.
  3643. */
  3644. if (ret <= 0)
  3645. goto out;
  3646. ret = 0;
  3647. inode->last_dir_index_offset = BTRFS_DIR_START_INDEX - 1;
  3648. /*
  3649. * No dir index items, bail out and leave last_dir_index_offset with
  3650. * the value right before the first valid index value.
  3651. */
  3652. if (path->slots[0] == 0)
  3653. goto out;
  3654. /*
  3655. * btrfs_search_slot() left us at one slot beyond the slot with the last
  3656. * index key, or beyond the last key of the directory that is not an
  3657. * index key. If we have an index key before, set last_dir_index_offset
  3658. * to its offset value, otherwise leave it with a value right before the
  3659. * first valid index value, as it means we have an empty directory.
  3660. */
  3661. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1);
  3662. if (key.objectid == ino && key.type == BTRFS_DIR_INDEX_KEY)
  3663. inode->last_dir_index_offset = key.offset;
  3664. out:
  3665. btrfs_release_path(path);
  3666. return ret;
  3667. }
  3668. /*
  3669. * logging directories is very similar to logging inodes, We find all the items
  3670. * from the current transaction and write them to the log.
  3671. *
  3672. * The recovery code scans the directory in the subvolume, and if it finds a
  3673. * key in the range logged that is not present in the log tree, then it means
  3674. * that dir entry was unlinked during the transaction.
  3675. *
  3676. * In order for that scan to work, we must include one key smaller than
  3677. * the smallest logged by this transaction and one key larger than the largest
  3678. * key logged by this transaction.
  3679. */
  3680. static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
  3681. struct btrfs_inode *inode,
  3682. struct btrfs_path *path,
  3683. struct btrfs_path *dst_path,
  3684. struct btrfs_log_ctx *ctx)
  3685. {
  3686. u64 min_key;
  3687. u64 max_key;
  3688. int ret;
  3689. ret = update_last_dir_index_offset(inode, path, ctx);
  3690. if (ret)
  3691. return ret;
  3692. min_key = BTRFS_DIR_START_INDEX;
  3693. max_key = 0;
  3694. while (1) {
  3695. ret = log_dir_items(trans, inode, path, dst_path,
  3696. ctx, min_key, &max_key);
  3697. if (ret)
  3698. return ret;
  3699. if (max_key == (u64)-1)
  3700. break;
  3701. min_key = max_key + 1;
  3702. }
  3703. return 0;
  3704. }
  3705. /*
  3706. * a helper function to drop items from the log before we relog an
  3707. * inode. max_key_type indicates the highest item type to remove.
  3708. * This cannot be run for file data extents because it does not
  3709. * free the extents they point to.
  3710. */
  3711. static int drop_inode_items(struct btrfs_trans_handle *trans,
  3712. struct btrfs_root *log,
  3713. struct btrfs_path *path,
  3714. struct btrfs_inode *inode,
  3715. int max_key_type)
  3716. {
  3717. int ret;
  3718. struct btrfs_key key;
  3719. struct btrfs_key found_key;
  3720. int start_slot;
  3721. key.objectid = btrfs_ino(inode);
  3722. key.type = max_key_type;
  3723. key.offset = (u64)-1;
  3724. while (1) {
  3725. ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
  3726. if (ret < 0) {
  3727. break;
  3728. } else if (ret > 0) {
  3729. if (path->slots[0] == 0)
  3730. break;
  3731. path->slots[0]--;
  3732. }
  3733. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  3734. path->slots[0]);
  3735. if (found_key.objectid != key.objectid)
  3736. break;
  3737. found_key.offset = 0;
  3738. found_key.type = 0;
  3739. ret = btrfs_bin_search(path->nodes[0], 0, &found_key, &start_slot);
  3740. if (ret < 0)
  3741. break;
  3742. ret = btrfs_del_items(trans, log, path, start_slot,
  3743. path->slots[0] - start_slot + 1);
  3744. /*
  3745. * If start slot isn't 0 then we don't need to re-search, we've
  3746. * found the last guy with the objectid in this tree.
  3747. */
  3748. if (ret || start_slot != 0)
  3749. break;
  3750. btrfs_release_path(path);
  3751. }
  3752. btrfs_release_path(path);
  3753. if (ret > 0)
  3754. ret = 0;
  3755. return ret;
  3756. }
  3757. static int truncate_inode_items(struct btrfs_trans_handle *trans,
  3758. struct btrfs_root *log_root,
  3759. struct btrfs_inode *inode,
  3760. u64 new_size, u32 min_type)
  3761. {
  3762. struct btrfs_truncate_control control = {
  3763. .new_size = new_size,
  3764. .ino = btrfs_ino(inode),
  3765. .min_type = min_type,
  3766. .skip_ref_updates = true,
  3767. };
  3768. return btrfs_truncate_inode_items(trans, log_root, &control);
  3769. }
  3770. static void fill_inode_item(struct btrfs_trans_handle *trans,
  3771. struct extent_buffer *leaf,
  3772. struct btrfs_inode_item *item,
  3773. struct inode *inode, int log_inode_only,
  3774. u64 logged_isize)
  3775. {
  3776. struct btrfs_map_token token;
  3777. u64 flags;
  3778. btrfs_init_map_token(&token, leaf);
  3779. if (log_inode_only) {
  3780. /* set the generation to zero so the recover code
  3781. * can tell the difference between an logging
  3782. * just to say 'this inode exists' and a logging
  3783. * to say 'update this inode with these values'
  3784. */
  3785. btrfs_set_token_inode_generation(&token, item, 0);
  3786. btrfs_set_token_inode_size(&token, item, logged_isize);
  3787. } else {
  3788. btrfs_set_token_inode_generation(&token, item,
  3789. BTRFS_I(inode)->generation);
  3790. btrfs_set_token_inode_size(&token, item, inode->i_size);
  3791. }
  3792. btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
  3793. btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
  3794. btrfs_set_token_inode_mode(&token, item, inode->i_mode);
  3795. btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
  3796. btrfs_set_token_timespec_sec(&token, &item->atime,
  3797. inode_get_atime_sec(inode));
  3798. btrfs_set_token_timespec_nsec(&token, &item->atime,
  3799. inode_get_atime_nsec(inode));
  3800. btrfs_set_token_timespec_sec(&token, &item->mtime,
  3801. inode_get_mtime_sec(inode));
  3802. btrfs_set_token_timespec_nsec(&token, &item->mtime,
  3803. inode_get_mtime_nsec(inode));
  3804. btrfs_set_token_timespec_sec(&token, &item->ctime,
  3805. inode_get_ctime_sec(inode));
  3806. btrfs_set_token_timespec_nsec(&token, &item->ctime,
  3807. inode_get_ctime_nsec(inode));
  3808. btrfs_set_timespec_sec(leaf, &item->otime, BTRFS_I(inode)->i_otime_sec);
  3809. btrfs_set_timespec_nsec(leaf, &item->otime, BTRFS_I(inode)->i_otime_nsec);
  3810. /*
  3811. * We do not need to set the nbytes field, in fact during a fast fsync
  3812. * its value may not even be correct, since a fast fsync does not wait
  3813. * for ordered extent completion, which is where we update nbytes, it
  3814. * only waits for writeback to complete. During log replay as we find
  3815. * file extent items and replay them, we adjust the nbytes field of the
  3816. * inode item in subvolume tree as needed (see overwrite_item()).
  3817. */
  3818. btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
  3819. btrfs_set_token_inode_transid(&token, item, trans->transid);
  3820. btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
  3821. flags = btrfs_inode_combine_flags(BTRFS_I(inode)->flags,
  3822. BTRFS_I(inode)->ro_flags);
  3823. btrfs_set_token_inode_flags(&token, item, flags);
  3824. btrfs_set_token_inode_block_group(&token, item, 0);
  3825. }
  3826. static int log_inode_item(struct btrfs_trans_handle *trans,
  3827. struct btrfs_root *log, struct btrfs_path *path,
  3828. struct btrfs_inode *inode, bool inode_item_dropped)
  3829. {
  3830. struct btrfs_inode_item *inode_item;
  3831. struct btrfs_key key;
  3832. int ret;
  3833. btrfs_get_inode_key(inode, &key);
  3834. /*
  3835. * If we are doing a fast fsync and the inode was logged before in the
  3836. * current transaction, then we know the inode was previously logged and
  3837. * it exists in the log tree. For performance reasons, in this case use
  3838. * btrfs_search_slot() directly with ins_len set to 0 so that we never
  3839. * attempt a write lock on the leaf's parent, which adds unnecessary lock
  3840. * contention in case there are concurrent fsyncs for other inodes of the
  3841. * same subvolume. Using btrfs_insert_empty_item() when the inode item
  3842. * already exists can also result in unnecessarily splitting a leaf.
  3843. */
  3844. if (!inode_item_dropped && inode->logged_trans == trans->transid) {
  3845. ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
  3846. ASSERT(ret <= 0);
  3847. if (ret > 0)
  3848. ret = -ENOENT;
  3849. } else {
  3850. /*
  3851. * This means it is the first fsync in the current transaction,
  3852. * so the inode item is not in the log and we need to insert it.
  3853. * We can never get -EEXIST because we are only called for a fast
  3854. * fsync and in case an inode eviction happens after the inode was
  3855. * logged before in the current transaction, when we load again
  3856. * the inode, we set BTRFS_INODE_NEEDS_FULL_SYNC on its runtime
  3857. * flags and set ->logged_trans to 0.
  3858. */
  3859. ret = btrfs_insert_empty_item(trans, log, path, &key,
  3860. sizeof(*inode_item));
  3861. ASSERT(ret != -EEXIST);
  3862. }
  3863. if (ret)
  3864. return ret;
  3865. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3866. struct btrfs_inode_item);
  3867. fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode,
  3868. 0, 0);
  3869. btrfs_release_path(path);
  3870. return 0;
  3871. }
  3872. static int log_csums(struct btrfs_trans_handle *trans,
  3873. struct btrfs_inode *inode,
  3874. struct btrfs_root *log_root,
  3875. struct btrfs_ordered_sum *sums)
  3876. {
  3877. const u64 lock_end = sums->logical + sums->len - 1;
  3878. struct extent_state *cached_state = NULL;
  3879. int ret;
  3880. /*
  3881. * If this inode was not used for reflink operations in the current
  3882. * transaction with new extents, then do the fast path, no need to
  3883. * worry about logging checksum items with overlapping ranges.
  3884. */
  3885. if (inode->last_reflink_trans < trans->transid)
  3886. return btrfs_csum_file_blocks(trans, log_root, sums);
  3887. /*
  3888. * Serialize logging for checksums. This is to avoid racing with the
  3889. * same checksum being logged by another task that is logging another
  3890. * file which happens to refer to the same extent as well. Such races
  3891. * can leave checksum items in the log with overlapping ranges.
  3892. */
  3893. ret = lock_extent(&log_root->log_csum_range, sums->logical, lock_end,
  3894. &cached_state);
  3895. if (ret)
  3896. return ret;
  3897. /*
  3898. * Due to extent cloning, we might have logged a csum item that covers a
  3899. * subrange of a cloned extent, and later we can end up logging a csum
  3900. * item for a larger subrange of the same extent or the entire range.
  3901. * This would leave csum items in the log tree that cover the same range
  3902. * and break the searches for checksums in the log tree, resulting in
  3903. * some checksums missing in the fs/subvolume tree. So just delete (or
  3904. * trim and adjust) any existing csum items in the log for this range.
  3905. */
  3906. ret = btrfs_del_csums(trans, log_root, sums->logical, sums->len);
  3907. if (!ret)
  3908. ret = btrfs_csum_file_blocks(trans, log_root, sums);
  3909. unlock_extent(&log_root->log_csum_range, sums->logical, lock_end,
  3910. &cached_state);
  3911. return ret;
  3912. }
  3913. static noinline int copy_items(struct btrfs_trans_handle *trans,
  3914. struct btrfs_inode *inode,
  3915. struct btrfs_path *dst_path,
  3916. struct btrfs_path *src_path,
  3917. int start_slot, int nr, int inode_only,
  3918. u64 logged_isize, struct btrfs_log_ctx *ctx)
  3919. {
  3920. struct btrfs_root *log = inode->root->log_root;
  3921. struct btrfs_file_extent_item *extent;
  3922. struct extent_buffer *src;
  3923. int ret;
  3924. struct btrfs_key *ins_keys;
  3925. u32 *ins_sizes;
  3926. struct btrfs_item_batch batch;
  3927. char *ins_data;
  3928. int dst_index;
  3929. const bool skip_csum = (inode->flags & BTRFS_INODE_NODATASUM);
  3930. const u64 i_size = i_size_read(&inode->vfs_inode);
  3931. /*
  3932. * To keep lockdep happy and avoid deadlocks, clone the source leaf and
  3933. * use the clone. This is because otherwise we would be changing the log
  3934. * tree, to insert items from the subvolume tree or insert csum items,
  3935. * while holding a read lock on a leaf from the subvolume tree, which
  3936. * creates a nasty lock dependency when COWing log tree nodes/leaves:
  3937. *
  3938. * 1) Modifying the log tree triggers an extent buffer allocation while
  3939. * holding a write lock on a parent extent buffer from the log tree.
  3940. * Allocating the pages for an extent buffer, or the extent buffer
  3941. * struct, can trigger inode eviction and finally the inode eviction
  3942. * will trigger a release/remove of a delayed node, which requires
  3943. * taking the delayed node's mutex;
  3944. *
  3945. * 2) Allocating a metadata extent for a log tree can trigger the async
  3946. * reclaim thread and make us wait for it to release enough space and
  3947. * unblock our reservation ticket. The reclaim thread can start
  3948. * flushing delayed items, and that in turn results in the need to
  3949. * lock delayed node mutexes and in the need to write lock extent
  3950. * buffers of a subvolume tree - all this while holding a write lock
  3951. * on the parent extent buffer in the log tree.
  3952. *
  3953. * So one task in scenario 1) running in parallel with another task in
  3954. * scenario 2) could lead to a deadlock, one wanting to lock a delayed
  3955. * node mutex while having a read lock on a leaf from the subvolume,
  3956. * while the other is holding the delayed node's mutex and wants to
  3957. * write lock the same subvolume leaf for flushing delayed items.
  3958. */
  3959. ret = clone_leaf(src_path, ctx);
  3960. if (ret < 0)
  3961. return ret;
  3962. src = src_path->nodes[0];
  3963. ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
  3964. nr * sizeof(u32), GFP_NOFS);
  3965. if (!ins_data)
  3966. return -ENOMEM;
  3967. ins_sizes = (u32 *)ins_data;
  3968. ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
  3969. batch.keys = ins_keys;
  3970. batch.data_sizes = ins_sizes;
  3971. batch.total_data_size = 0;
  3972. batch.nr = 0;
  3973. dst_index = 0;
  3974. for (int i = 0; i < nr; i++) {
  3975. const int src_slot = start_slot + i;
  3976. struct btrfs_root *csum_root;
  3977. struct btrfs_ordered_sum *sums;
  3978. struct btrfs_ordered_sum *sums_next;
  3979. LIST_HEAD(ordered_sums);
  3980. u64 disk_bytenr;
  3981. u64 disk_num_bytes;
  3982. u64 extent_offset;
  3983. u64 extent_num_bytes;
  3984. bool is_old_extent;
  3985. btrfs_item_key_to_cpu(src, &ins_keys[dst_index], src_slot);
  3986. if (ins_keys[dst_index].type != BTRFS_EXTENT_DATA_KEY)
  3987. goto add_to_batch;
  3988. extent = btrfs_item_ptr(src, src_slot,
  3989. struct btrfs_file_extent_item);
  3990. is_old_extent = (btrfs_file_extent_generation(src, extent) <
  3991. trans->transid);
  3992. /*
  3993. * Don't copy extents from past generations. That would make us
  3994. * log a lot more metadata for common cases like doing only a
  3995. * few random writes into a file and then fsync it for the first
  3996. * time or after the full sync flag is set on the inode. We can
  3997. * get leaves full of extent items, most of which are from past
  3998. * generations, so we can skip them - as long as the inode has
  3999. * not been the target of a reflink operation in this transaction,
  4000. * as in that case it might have had file extent items with old
  4001. * generations copied into it. We also must always log prealloc
  4002. * extents that start at or beyond eof, otherwise we would lose
  4003. * them on log replay.
  4004. */
  4005. if (is_old_extent &&
  4006. ins_keys[dst_index].offset < i_size &&
  4007. inode->last_reflink_trans < trans->transid)
  4008. continue;
  4009. if (skip_csum)
  4010. goto add_to_batch;
  4011. /* Only regular extents have checksums. */
  4012. if (btrfs_file_extent_type(src, extent) != BTRFS_FILE_EXTENT_REG)
  4013. goto add_to_batch;
  4014. /*
  4015. * If it's an extent created in a past transaction, then its
  4016. * checksums are already accessible from the committed csum tree,
  4017. * no need to log them.
  4018. */
  4019. if (is_old_extent)
  4020. goto add_to_batch;
  4021. disk_bytenr = btrfs_file_extent_disk_bytenr(src, extent);
  4022. /* If it's an explicit hole, there are no checksums. */
  4023. if (disk_bytenr == 0)
  4024. goto add_to_batch;
  4025. disk_num_bytes = btrfs_file_extent_disk_num_bytes(src, extent);
  4026. if (btrfs_file_extent_compression(src, extent)) {
  4027. extent_offset = 0;
  4028. extent_num_bytes = disk_num_bytes;
  4029. } else {
  4030. extent_offset = btrfs_file_extent_offset(src, extent);
  4031. extent_num_bytes = btrfs_file_extent_num_bytes(src, extent);
  4032. }
  4033. csum_root = btrfs_csum_root(trans->fs_info, disk_bytenr);
  4034. disk_bytenr += extent_offset;
  4035. ret = btrfs_lookup_csums_list(csum_root, disk_bytenr,
  4036. disk_bytenr + extent_num_bytes - 1,
  4037. &ordered_sums, false);
  4038. if (ret < 0)
  4039. goto out;
  4040. ret = 0;
  4041. list_for_each_entry_safe(sums, sums_next, &ordered_sums, list) {
  4042. if (!ret)
  4043. ret = log_csums(trans, inode, log, sums);
  4044. list_del(&sums->list);
  4045. kfree(sums);
  4046. }
  4047. if (ret)
  4048. goto out;
  4049. add_to_batch:
  4050. ins_sizes[dst_index] = btrfs_item_size(src, src_slot);
  4051. batch.total_data_size += ins_sizes[dst_index];
  4052. batch.nr++;
  4053. dst_index++;
  4054. }
  4055. /*
  4056. * We have a leaf full of old extent items that don't need to be logged,
  4057. * so we don't need to do anything.
  4058. */
  4059. if (batch.nr == 0)
  4060. goto out;
  4061. ret = btrfs_insert_empty_items(trans, log, dst_path, &batch);
  4062. if (ret)
  4063. goto out;
  4064. dst_index = 0;
  4065. for (int i = 0; i < nr; i++) {
  4066. const int src_slot = start_slot + i;
  4067. const int dst_slot = dst_path->slots[0] + dst_index;
  4068. struct btrfs_key key;
  4069. unsigned long src_offset;
  4070. unsigned long dst_offset;
  4071. /*
  4072. * We're done, all the remaining items in the source leaf
  4073. * correspond to old file extent items.
  4074. */
  4075. if (dst_index >= batch.nr)
  4076. break;
  4077. btrfs_item_key_to_cpu(src, &key, src_slot);
  4078. if (key.type != BTRFS_EXTENT_DATA_KEY)
  4079. goto copy_item;
  4080. extent = btrfs_item_ptr(src, src_slot,
  4081. struct btrfs_file_extent_item);
  4082. /* See the comment in the previous loop, same logic. */
  4083. if (btrfs_file_extent_generation(src, extent) < trans->transid &&
  4084. key.offset < i_size &&
  4085. inode->last_reflink_trans < trans->transid)
  4086. continue;
  4087. copy_item:
  4088. dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0], dst_slot);
  4089. src_offset = btrfs_item_ptr_offset(src, src_slot);
  4090. if (key.type == BTRFS_INODE_ITEM_KEY) {
  4091. struct btrfs_inode_item *inode_item;
  4092. inode_item = btrfs_item_ptr(dst_path->nodes[0], dst_slot,
  4093. struct btrfs_inode_item);
  4094. fill_inode_item(trans, dst_path->nodes[0], inode_item,
  4095. &inode->vfs_inode,
  4096. inode_only == LOG_INODE_EXISTS,
  4097. logged_isize);
  4098. } else {
  4099. copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
  4100. src_offset, ins_sizes[dst_index]);
  4101. }
  4102. dst_index++;
  4103. }
  4104. btrfs_mark_buffer_dirty(trans, dst_path->nodes[0]);
  4105. btrfs_release_path(dst_path);
  4106. out:
  4107. kfree(ins_data);
  4108. return ret;
  4109. }
  4110. static int extent_cmp(void *priv, const struct list_head *a,
  4111. const struct list_head *b)
  4112. {
  4113. const struct extent_map *em1, *em2;
  4114. em1 = list_entry(a, struct extent_map, list);
  4115. em2 = list_entry(b, struct extent_map, list);
  4116. if (em1->start < em2->start)
  4117. return -1;
  4118. else if (em1->start > em2->start)
  4119. return 1;
  4120. return 0;
  4121. }
  4122. static int log_extent_csums(struct btrfs_trans_handle *trans,
  4123. struct btrfs_inode *inode,
  4124. struct btrfs_root *log_root,
  4125. const struct extent_map *em,
  4126. struct btrfs_log_ctx *ctx)
  4127. {
  4128. struct btrfs_ordered_extent *ordered;
  4129. struct btrfs_root *csum_root;
  4130. u64 block_start;
  4131. u64 csum_offset;
  4132. u64 csum_len;
  4133. u64 mod_start = em->start;
  4134. u64 mod_len = em->len;
  4135. LIST_HEAD(ordered_sums);
  4136. int ret = 0;
  4137. if (inode->flags & BTRFS_INODE_NODATASUM ||
  4138. (em->flags & EXTENT_FLAG_PREALLOC) ||
  4139. em->disk_bytenr == EXTENT_MAP_HOLE)
  4140. return 0;
  4141. list_for_each_entry(ordered, &ctx->ordered_extents, log_list) {
  4142. const u64 ordered_end = ordered->file_offset + ordered->num_bytes;
  4143. const u64 mod_end = mod_start + mod_len;
  4144. struct btrfs_ordered_sum *sums;
  4145. if (mod_len == 0)
  4146. break;
  4147. if (ordered_end <= mod_start)
  4148. continue;
  4149. if (mod_end <= ordered->file_offset)
  4150. break;
  4151. /*
  4152. * We are going to copy all the csums on this ordered extent, so
  4153. * go ahead and adjust mod_start and mod_len in case this ordered
  4154. * extent has already been logged.
  4155. */
  4156. if (ordered->file_offset > mod_start) {
  4157. if (ordered_end >= mod_end)
  4158. mod_len = ordered->file_offset - mod_start;
  4159. /*
  4160. * If we have this case
  4161. *
  4162. * |--------- logged extent ---------|
  4163. * |----- ordered extent ----|
  4164. *
  4165. * Just don't mess with mod_start and mod_len, we'll
  4166. * just end up logging more csums than we need and it
  4167. * will be ok.
  4168. */
  4169. } else {
  4170. if (ordered_end < mod_end) {
  4171. mod_len = mod_end - ordered_end;
  4172. mod_start = ordered_end;
  4173. } else {
  4174. mod_len = 0;
  4175. }
  4176. }
  4177. /*
  4178. * To keep us from looping for the above case of an ordered
  4179. * extent that falls inside of the logged extent.
  4180. */
  4181. if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM, &ordered->flags))
  4182. continue;
  4183. list_for_each_entry(sums, &ordered->list, list) {
  4184. ret = log_csums(trans, inode, log_root, sums);
  4185. if (ret)
  4186. return ret;
  4187. }
  4188. }
  4189. /* We're done, found all csums in the ordered extents. */
  4190. if (mod_len == 0)
  4191. return 0;
  4192. /* If we're compressed we have to save the entire range of csums. */
  4193. if (extent_map_is_compressed(em)) {
  4194. csum_offset = 0;
  4195. csum_len = em->disk_num_bytes;
  4196. } else {
  4197. csum_offset = mod_start - em->start;
  4198. csum_len = mod_len;
  4199. }
  4200. /* block start is already adjusted for the file extent offset. */
  4201. block_start = extent_map_block_start(em);
  4202. csum_root = btrfs_csum_root(trans->fs_info, block_start);
  4203. ret = btrfs_lookup_csums_list(csum_root, block_start + csum_offset,
  4204. block_start + csum_offset + csum_len - 1,
  4205. &ordered_sums, false);
  4206. if (ret < 0)
  4207. return ret;
  4208. ret = 0;
  4209. while (!list_empty(&ordered_sums)) {
  4210. struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
  4211. struct btrfs_ordered_sum,
  4212. list);
  4213. if (!ret)
  4214. ret = log_csums(trans, inode, log_root, sums);
  4215. list_del(&sums->list);
  4216. kfree(sums);
  4217. }
  4218. return ret;
  4219. }
  4220. static int log_one_extent(struct btrfs_trans_handle *trans,
  4221. struct btrfs_inode *inode,
  4222. const struct extent_map *em,
  4223. struct btrfs_path *path,
  4224. struct btrfs_log_ctx *ctx)
  4225. {
  4226. struct btrfs_drop_extents_args drop_args = { 0 };
  4227. struct btrfs_root *log = inode->root->log_root;
  4228. struct btrfs_file_extent_item fi = { 0 };
  4229. struct extent_buffer *leaf;
  4230. struct btrfs_key key;
  4231. enum btrfs_compression_type compress_type;
  4232. u64 extent_offset = em->offset;
  4233. u64 block_start = extent_map_block_start(em);
  4234. u64 block_len;
  4235. int ret;
  4236. btrfs_set_stack_file_extent_generation(&fi, trans->transid);
  4237. if (em->flags & EXTENT_FLAG_PREALLOC)
  4238. btrfs_set_stack_file_extent_type(&fi, BTRFS_FILE_EXTENT_PREALLOC);
  4239. else
  4240. btrfs_set_stack_file_extent_type(&fi, BTRFS_FILE_EXTENT_REG);
  4241. block_len = em->disk_num_bytes;
  4242. compress_type = extent_map_compression(em);
  4243. if (compress_type != BTRFS_COMPRESS_NONE) {
  4244. btrfs_set_stack_file_extent_disk_bytenr(&fi, block_start);
  4245. btrfs_set_stack_file_extent_disk_num_bytes(&fi, block_len);
  4246. } else if (em->disk_bytenr < EXTENT_MAP_LAST_BYTE) {
  4247. btrfs_set_stack_file_extent_disk_bytenr(&fi, block_start - extent_offset);
  4248. btrfs_set_stack_file_extent_disk_num_bytes(&fi, block_len);
  4249. }
  4250. btrfs_set_stack_file_extent_offset(&fi, extent_offset);
  4251. btrfs_set_stack_file_extent_num_bytes(&fi, em->len);
  4252. btrfs_set_stack_file_extent_ram_bytes(&fi, em->ram_bytes);
  4253. btrfs_set_stack_file_extent_compression(&fi, compress_type);
  4254. ret = log_extent_csums(trans, inode, log, em, ctx);
  4255. if (ret)
  4256. return ret;
  4257. /*
  4258. * If this is the first time we are logging the inode in the current
  4259. * transaction, we can avoid btrfs_drop_extents(), which is expensive
  4260. * because it does a deletion search, which always acquires write locks
  4261. * for extent buffers at levels 2, 1 and 0. This not only wastes time
  4262. * but also adds significant contention in a log tree, since log trees
  4263. * are small, with a root at level 2 or 3 at most, due to their short
  4264. * life span.
  4265. */
  4266. if (ctx->logged_before) {
  4267. drop_args.path = path;
  4268. drop_args.start = em->start;
  4269. drop_args.end = em->start + em->len;
  4270. drop_args.replace_extent = true;
  4271. drop_args.extent_item_size = sizeof(fi);
  4272. ret = btrfs_drop_extents(trans, log, inode, &drop_args);
  4273. if (ret)
  4274. return ret;
  4275. }
  4276. if (!drop_args.extent_inserted) {
  4277. key.objectid = btrfs_ino(inode);
  4278. key.type = BTRFS_EXTENT_DATA_KEY;
  4279. key.offset = em->start;
  4280. ret = btrfs_insert_empty_item(trans, log, path, &key,
  4281. sizeof(fi));
  4282. if (ret)
  4283. return ret;
  4284. }
  4285. leaf = path->nodes[0];
  4286. write_extent_buffer(leaf, &fi,
  4287. btrfs_item_ptr_offset(leaf, path->slots[0]),
  4288. sizeof(fi));
  4289. btrfs_mark_buffer_dirty(trans, leaf);
  4290. btrfs_release_path(path);
  4291. return ret;
  4292. }
  4293. /*
  4294. * Log all prealloc extents beyond the inode's i_size to make sure we do not
  4295. * lose them after doing a full/fast fsync and replaying the log. We scan the
  4296. * subvolume's root instead of iterating the inode's extent map tree because
  4297. * otherwise we can log incorrect extent items based on extent map conversion.
  4298. * That can happen due to the fact that extent maps are merged when they
  4299. * are not in the extent map tree's list of modified extents.
  4300. */
  4301. static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans,
  4302. struct btrfs_inode *inode,
  4303. struct btrfs_path *path,
  4304. struct btrfs_log_ctx *ctx)
  4305. {
  4306. struct btrfs_root *root = inode->root;
  4307. struct btrfs_key key;
  4308. const u64 i_size = i_size_read(&inode->vfs_inode);
  4309. const u64 ino = btrfs_ino(inode);
  4310. struct btrfs_path *dst_path = NULL;
  4311. bool dropped_extents = false;
  4312. u64 truncate_offset = i_size;
  4313. struct extent_buffer *leaf;
  4314. int slot;
  4315. int ins_nr = 0;
  4316. int start_slot = 0;
  4317. int ret;
  4318. if (!(inode->flags & BTRFS_INODE_PREALLOC))
  4319. return 0;
  4320. key.objectid = ino;
  4321. key.type = BTRFS_EXTENT_DATA_KEY;
  4322. key.offset = i_size;
  4323. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4324. if (ret < 0)
  4325. goto out;
  4326. /*
  4327. * We must check if there is a prealloc extent that starts before the
  4328. * i_size and crosses the i_size boundary. This is to ensure later we
  4329. * truncate down to the end of that extent and not to the i_size, as
  4330. * otherwise we end up losing part of the prealloc extent after a log
  4331. * replay and with an implicit hole if there is another prealloc extent
  4332. * that starts at an offset beyond i_size.
  4333. */
  4334. ret = btrfs_previous_item(root, path, ino, BTRFS_EXTENT_DATA_KEY);
  4335. if (ret < 0)
  4336. goto out;
  4337. if (ret == 0) {
  4338. struct btrfs_file_extent_item *ei;
  4339. leaf = path->nodes[0];
  4340. slot = path->slots[0];
  4341. ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  4342. if (btrfs_file_extent_type(leaf, ei) ==
  4343. BTRFS_FILE_EXTENT_PREALLOC) {
  4344. u64 extent_end;
  4345. btrfs_item_key_to_cpu(leaf, &key, slot);
  4346. extent_end = key.offset +
  4347. btrfs_file_extent_num_bytes(leaf, ei);
  4348. if (extent_end > i_size)
  4349. truncate_offset = extent_end;
  4350. }
  4351. } else {
  4352. ret = 0;
  4353. }
  4354. while (true) {
  4355. leaf = path->nodes[0];
  4356. slot = path->slots[0];
  4357. if (slot >= btrfs_header_nritems(leaf)) {
  4358. if (ins_nr > 0) {
  4359. ret = copy_items(trans, inode, dst_path, path,
  4360. start_slot, ins_nr, 1, 0, ctx);
  4361. if (ret < 0)
  4362. goto out;
  4363. ins_nr = 0;
  4364. }
  4365. ret = btrfs_next_leaf(root, path);
  4366. if (ret < 0)
  4367. goto out;
  4368. if (ret > 0) {
  4369. ret = 0;
  4370. break;
  4371. }
  4372. continue;
  4373. }
  4374. btrfs_item_key_to_cpu(leaf, &key, slot);
  4375. if (key.objectid > ino)
  4376. break;
  4377. if (WARN_ON_ONCE(key.objectid < ino) ||
  4378. key.type < BTRFS_EXTENT_DATA_KEY ||
  4379. key.offset < i_size) {
  4380. path->slots[0]++;
  4381. continue;
  4382. }
  4383. /*
  4384. * Avoid overlapping items in the log tree. The first time we
  4385. * get here, get rid of everything from a past fsync. After
  4386. * that, if the current extent starts before the end of the last
  4387. * extent we copied, truncate the last one. This can happen if
  4388. * an ordered extent completion modifies the subvolume tree
  4389. * while btrfs_next_leaf() has the tree unlocked.
  4390. */
  4391. if (!dropped_extents || key.offset < truncate_offset) {
  4392. ret = truncate_inode_items(trans, root->log_root, inode,
  4393. min(key.offset, truncate_offset),
  4394. BTRFS_EXTENT_DATA_KEY);
  4395. if (ret)
  4396. goto out;
  4397. dropped_extents = true;
  4398. }
  4399. truncate_offset = btrfs_file_extent_end(path);
  4400. if (ins_nr == 0)
  4401. start_slot = slot;
  4402. ins_nr++;
  4403. path->slots[0]++;
  4404. if (!dst_path) {
  4405. dst_path = btrfs_alloc_path();
  4406. if (!dst_path) {
  4407. ret = -ENOMEM;
  4408. goto out;
  4409. }
  4410. }
  4411. }
  4412. if (ins_nr > 0)
  4413. ret = copy_items(trans, inode, dst_path, path,
  4414. start_slot, ins_nr, 1, 0, ctx);
  4415. out:
  4416. btrfs_release_path(path);
  4417. btrfs_free_path(dst_path);
  4418. return ret;
  4419. }
  4420. static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
  4421. struct btrfs_inode *inode,
  4422. struct btrfs_path *path,
  4423. struct btrfs_log_ctx *ctx)
  4424. {
  4425. struct btrfs_ordered_extent *ordered;
  4426. struct btrfs_ordered_extent *tmp;
  4427. struct extent_map *em, *n;
  4428. LIST_HEAD(extents);
  4429. struct extent_map_tree *tree = &inode->extent_tree;
  4430. int ret = 0;
  4431. int num = 0;
  4432. write_lock(&tree->lock);
  4433. list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
  4434. list_del_init(&em->list);
  4435. /*
  4436. * Just an arbitrary number, this can be really CPU intensive
  4437. * once we start getting a lot of extents, and really once we
  4438. * have a bunch of extents we just want to commit since it will
  4439. * be faster.
  4440. */
  4441. if (++num > 32768) {
  4442. list_del_init(&tree->modified_extents);
  4443. ret = -EFBIG;
  4444. goto process;
  4445. }
  4446. if (em->generation < trans->transid)
  4447. continue;
  4448. /* We log prealloc extents beyond eof later. */
  4449. if ((em->flags & EXTENT_FLAG_PREALLOC) &&
  4450. em->start >= i_size_read(&inode->vfs_inode))
  4451. continue;
  4452. /* Need a ref to keep it from getting evicted from cache */
  4453. refcount_inc(&em->refs);
  4454. em->flags |= EXTENT_FLAG_LOGGING;
  4455. list_add_tail(&em->list, &extents);
  4456. num++;
  4457. }
  4458. list_sort(NULL, &extents, extent_cmp);
  4459. process:
  4460. while (!list_empty(&extents)) {
  4461. em = list_entry(extents.next, struct extent_map, list);
  4462. list_del_init(&em->list);
  4463. /*
  4464. * If we had an error we just need to delete everybody from our
  4465. * private list.
  4466. */
  4467. if (ret) {
  4468. clear_em_logging(inode, em);
  4469. free_extent_map(em);
  4470. continue;
  4471. }
  4472. write_unlock(&tree->lock);
  4473. ret = log_one_extent(trans, inode, em, path, ctx);
  4474. write_lock(&tree->lock);
  4475. clear_em_logging(inode, em);
  4476. free_extent_map(em);
  4477. }
  4478. WARN_ON(!list_empty(&extents));
  4479. write_unlock(&tree->lock);
  4480. if (!ret)
  4481. ret = btrfs_log_prealloc_extents(trans, inode, path, ctx);
  4482. if (ret)
  4483. return ret;
  4484. /*
  4485. * We have logged all extents successfully, now make sure the commit of
  4486. * the current transaction waits for the ordered extents to complete
  4487. * before it commits and wipes out the log trees, otherwise we would
  4488. * lose data if an ordered extents completes after the transaction
  4489. * commits and a power failure happens after the transaction commit.
  4490. */
  4491. list_for_each_entry_safe(ordered, tmp, &ctx->ordered_extents, log_list) {
  4492. list_del_init(&ordered->log_list);
  4493. set_bit(BTRFS_ORDERED_LOGGED, &ordered->flags);
  4494. if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {
  4495. spin_lock_irq(&inode->ordered_tree_lock);
  4496. if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {
  4497. set_bit(BTRFS_ORDERED_PENDING, &ordered->flags);
  4498. atomic_inc(&trans->transaction->pending_ordered);
  4499. }
  4500. spin_unlock_irq(&inode->ordered_tree_lock);
  4501. }
  4502. btrfs_put_ordered_extent(ordered);
  4503. }
  4504. return 0;
  4505. }
  4506. static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode,
  4507. struct btrfs_path *path, u64 *size_ret)
  4508. {
  4509. struct btrfs_key key;
  4510. int ret;
  4511. key.objectid = btrfs_ino(inode);
  4512. key.type = BTRFS_INODE_ITEM_KEY;
  4513. key.offset = 0;
  4514. ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
  4515. if (ret < 0) {
  4516. return ret;
  4517. } else if (ret > 0) {
  4518. *size_ret = 0;
  4519. } else {
  4520. struct btrfs_inode_item *item;
  4521. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  4522. struct btrfs_inode_item);
  4523. *size_ret = btrfs_inode_size(path->nodes[0], item);
  4524. /*
  4525. * If the in-memory inode's i_size is smaller then the inode
  4526. * size stored in the btree, return the inode's i_size, so
  4527. * that we get a correct inode size after replaying the log
  4528. * when before a power failure we had a shrinking truncate
  4529. * followed by addition of a new name (rename / new hard link).
  4530. * Otherwise return the inode size from the btree, to avoid
  4531. * data loss when replaying a log due to previously doing a
  4532. * write that expands the inode's size and logging a new name
  4533. * immediately after.
  4534. */
  4535. if (*size_ret > inode->vfs_inode.i_size)
  4536. *size_ret = inode->vfs_inode.i_size;
  4537. }
  4538. btrfs_release_path(path);
  4539. return 0;
  4540. }
  4541. /*
  4542. * At the moment we always log all xattrs. This is to figure out at log replay
  4543. * time which xattrs must have their deletion replayed. If a xattr is missing
  4544. * in the log tree and exists in the fs/subvol tree, we delete it. This is
  4545. * because if a xattr is deleted, the inode is fsynced and a power failure
  4546. * happens, causing the log to be replayed the next time the fs is mounted,
  4547. * we want the xattr to not exist anymore (same behaviour as other filesystems
  4548. * with a journal, ext3/4, xfs, f2fs, etc).
  4549. */
  4550. static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
  4551. struct btrfs_inode *inode,
  4552. struct btrfs_path *path,
  4553. struct btrfs_path *dst_path,
  4554. struct btrfs_log_ctx *ctx)
  4555. {
  4556. struct btrfs_root *root = inode->root;
  4557. int ret;
  4558. struct btrfs_key key;
  4559. const u64 ino = btrfs_ino(inode);
  4560. int ins_nr = 0;
  4561. int start_slot = 0;
  4562. bool found_xattrs = false;
  4563. if (test_bit(BTRFS_INODE_NO_XATTRS, &inode->runtime_flags))
  4564. return 0;
  4565. key.objectid = ino;
  4566. key.type = BTRFS_XATTR_ITEM_KEY;
  4567. key.offset = 0;
  4568. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4569. if (ret < 0)
  4570. return ret;
  4571. while (true) {
  4572. int slot = path->slots[0];
  4573. struct extent_buffer *leaf = path->nodes[0];
  4574. int nritems = btrfs_header_nritems(leaf);
  4575. if (slot >= nritems) {
  4576. if (ins_nr > 0) {
  4577. ret = copy_items(trans, inode, dst_path, path,
  4578. start_slot, ins_nr, 1, 0, ctx);
  4579. if (ret < 0)
  4580. return ret;
  4581. ins_nr = 0;
  4582. }
  4583. ret = btrfs_next_leaf(root, path);
  4584. if (ret < 0)
  4585. return ret;
  4586. else if (ret > 0)
  4587. break;
  4588. continue;
  4589. }
  4590. btrfs_item_key_to_cpu(leaf, &key, slot);
  4591. if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
  4592. break;
  4593. if (ins_nr == 0)
  4594. start_slot = slot;
  4595. ins_nr++;
  4596. path->slots[0]++;
  4597. found_xattrs = true;
  4598. cond_resched();
  4599. }
  4600. if (ins_nr > 0) {
  4601. ret = copy_items(trans, inode, dst_path, path,
  4602. start_slot, ins_nr, 1, 0, ctx);
  4603. if (ret < 0)
  4604. return ret;
  4605. }
  4606. if (!found_xattrs)
  4607. set_bit(BTRFS_INODE_NO_XATTRS, &inode->runtime_flags);
  4608. return 0;
  4609. }
  4610. /*
  4611. * When using the NO_HOLES feature if we punched a hole that causes the
  4612. * deletion of entire leafs or all the extent items of the first leaf (the one
  4613. * that contains the inode item and references) we may end up not processing
  4614. * any extents, because there are no leafs with a generation matching the
  4615. * current transaction that have extent items for our inode. So we need to find
  4616. * if any holes exist and then log them. We also need to log holes after any
  4617. * truncate operation that changes the inode's size.
  4618. */
  4619. static int btrfs_log_holes(struct btrfs_trans_handle *trans,
  4620. struct btrfs_inode *inode,
  4621. struct btrfs_path *path)
  4622. {
  4623. struct btrfs_root *root = inode->root;
  4624. struct btrfs_fs_info *fs_info = root->fs_info;
  4625. struct btrfs_key key;
  4626. const u64 ino = btrfs_ino(inode);
  4627. const u64 i_size = i_size_read(&inode->vfs_inode);
  4628. u64 prev_extent_end = 0;
  4629. int ret;
  4630. if (!btrfs_fs_incompat(fs_info, NO_HOLES) || i_size == 0)
  4631. return 0;
  4632. key.objectid = ino;
  4633. key.type = BTRFS_EXTENT_DATA_KEY;
  4634. key.offset = 0;
  4635. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4636. if (ret < 0)
  4637. return ret;
  4638. while (true) {
  4639. struct extent_buffer *leaf = path->nodes[0];
  4640. if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
  4641. ret = btrfs_next_leaf(root, path);
  4642. if (ret < 0)
  4643. return ret;
  4644. if (ret > 0) {
  4645. ret = 0;
  4646. break;
  4647. }
  4648. leaf = path->nodes[0];
  4649. }
  4650. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  4651. if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY)
  4652. break;
  4653. /* We have a hole, log it. */
  4654. if (prev_extent_end < key.offset) {
  4655. const u64 hole_len = key.offset - prev_extent_end;
  4656. /*
  4657. * Release the path to avoid deadlocks with other code
  4658. * paths that search the root while holding locks on
  4659. * leafs from the log root.
  4660. */
  4661. btrfs_release_path(path);
  4662. ret = btrfs_insert_hole_extent(trans, root->log_root,
  4663. ino, prev_extent_end,
  4664. hole_len);
  4665. if (ret < 0)
  4666. return ret;
  4667. /*
  4668. * Search for the same key again in the root. Since it's
  4669. * an extent item and we are holding the inode lock, the
  4670. * key must still exist. If it doesn't just emit warning
  4671. * and return an error to fall back to a transaction
  4672. * commit.
  4673. */
  4674. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4675. if (ret < 0)
  4676. return ret;
  4677. if (WARN_ON(ret > 0))
  4678. return -ENOENT;
  4679. leaf = path->nodes[0];
  4680. }
  4681. prev_extent_end = btrfs_file_extent_end(path);
  4682. path->slots[0]++;
  4683. cond_resched();
  4684. }
  4685. if (prev_extent_end < i_size) {
  4686. u64 hole_len;
  4687. btrfs_release_path(path);
  4688. hole_len = ALIGN(i_size - prev_extent_end, fs_info->sectorsize);
  4689. ret = btrfs_insert_hole_extent(trans, root->log_root, ino,
  4690. prev_extent_end, hole_len);
  4691. if (ret < 0)
  4692. return ret;
  4693. }
  4694. return 0;
  4695. }
  4696. /*
  4697. * When we are logging a new inode X, check if it doesn't have a reference that
  4698. * matches the reference from some other inode Y created in a past transaction
  4699. * and that was renamed in the current transaction. If we don't do this, then at
  4700. * log replay time we can lose inode Y (and all its files if it's a directory):
  4701. *
  4702. * mkdir /mnt/x
  4703. * echo "hello world" > /mnt/x/foobar
  4704. * sync
  4705. * mv /mnt/x /mnt/y
  4706. * mkdir /mnt/x # or touch /mnt/x
  4707. * xfs_io -c fsync /mnt/x
  4708. * <power fail>
  4709. * mount fs, trigger log replay
  4710. *
  4711. * After the log replay procedure, we would lose the first directory and all its
  4712. * files (file foobar).
  4713. * For the case where inode Y is not a directory we simply end up losing it:
  4714. *
  4715. * echo "123" > /mnt/foo
  4716. * sync
  4717. * mv /mnt/foo /mnt/bar
  4718. * echo "abc" > /mnt/foo
  4719. * xfs_io -c fsync /mnt/foo
  4720. * <power fail>
  4721. *
  4722. * We also need this for cases where a snapshot entry is replaced by some other
  4723. * entry (file or directory) otherwise we end up with an unreplayable log due to
  4724. * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
  4725. * if it were a regular entry:
  4726. *
  4727. * mkdir /mnt/x
  4728. * btrfs subvolume snapshot /mnt /mnt/x/snap
  4729. * btrfs subvolume delete /mnt/x/snap
  4730. * rmdir /mnt/x
  4731. * mkdir /mnt/x
  4732. * fsync /mnt/x or fsync some new file inside it
  4733. * <power fail>
  4734. *
  4735. * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
  4736. * the same transaction.
  4737. */
  4738. static int btrfs_check_ref_name_override(struct extent_buffer *eb,
  4739. const int slot,
  4740. const struct btrfs_key *key,
  4741. struct btrfs_inode *inode,
  4742. u64 *other_ino, u64 *other_parent)
  4743. {
  4744. int ret;
  4745. struct btrfs_path *search_path;
  4746. char *name = NULL;
  4747. u32 name_len = 0;
  4748. u32 item_size = btrfs_item_size(eb, slot);
  4749. u32 cur_offset = 0;
  4750. unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
  4751. search_path = btrfs_alloc_path();
  4752. if (!search_path)
  4753. return -ENOMEM;
  4754. search_path->search_commit_root = 1;
  4755. search_path->skip_locking = 1;
  4756. while (cur_offset < item_size) {
  4757. u64 parent;
  4758. u32 this_name_len;
  4759. u32 this_len;
  4760. unsigned long name_ptr;
  4761. struct btrfs_dir_item *di;
  4762. struct fscrypt_str name_str;
  4763. if (key->type == BTRFS_INODE_REF_KEY) {
  4764. struct btrfs_inode_ref *iref;
  4765. iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
  4766. parent = key->offset;
  4767. this_name_len = btrfs_inode_ref_name_len(eb, iref);
  4768. name_ptr = (unsigned long)(iref + 1);
  4769. this_len = sizeof(*iref) + this_name_len;
  4770. } else {
  4771. struct btrfs_inode_extref *extref;
  4772. extref = (struct btrfs_inode_extref *)(ptr +
  4773. cur_offset);
  4774. parent = btrfs_inode_extref_parent(eb, extref);
  4775. this_name_len = btrfs_inode_extref_name_len(eb, extref);
  4776. name_ptr = (unsigned long)&extref->name;
  4777. this_len = sizeof(*extref) + this_name_len;
  4778. }
  4779. if (this_name_len > name_len) {
  4780. char *new_name;
  4781. new_name = krealloc(name, this_name_len, GFP_NOFS);
  4782. if (!new_name) {
  4783. ret = -ENOMEM;
  4784. goto out;
  4785. }
  4786. name_len = this_name_len;
  4787. name = new_name;
  4788. }
  4789. read_extent_buffer(eb, name, name_ptr, this_name_len);
  4790. name_str.name = name;
  4791. name_str.len = this_name_len;
  4792. di = btrfs_lookup_dir_item(NULL, inode->root, search_path,
  4793. parent, &name_str, 0);
  4794. if (di && !IS_ERR(di)) {
  4795. struct btrfs_key di_key;
  4796. btrfs_dir_item_key_to_cpu(search_path->nodes[0],
  4797. di, &di_key);
  4798. if (di_key.type == BTRFS_INODE_ITEM_KEY) {
  4799. if (di_key.objectid != key->objectid) {
  4800. ret = 1;
  4801. *other_ino = di_key.objectid;
  4802. *other_parent = parent;
  4803. } else {
  4804. ret = 0;
  4805. }
  4806. } else {
  4807. ret = -EAGAIN;
  4808. }
  4809. goto out;
  4810. } else if (IS_ERR(di)) {
  4811. ret = PTR_ERR(di);
  4812. goto out;
  4813. }
  4814. btrfs_release_path(search_path);
  4815. cur_offset += this_len;
  4816. }
  4817. ret = 0;
  4818. out:
  4819. btrfs_free_path(search_path);
  4820. kfree(name);
  4821. return ret;
  4822. }
  4823. /*
  4824. * Check if we need to log an inode. This is used in contexts where while
  4825. * logging an inode we need to log another inode (either that it exists or in
  4826. * full mode). This is used instead of btrfs_inode_in_log() because the later
  4827. * requires the inode to be in the log and have the log transaction committed,
  4828. * while here we do not care if the log transaction was already committed - our
  4829. * caller will commit the log later - and we want to avoid logging an inode
  4830. * multiple times when multiple tasks have joined the same log transaction.
  4831. */
  4832. static bool need_log_inode(const struct btrfs_trans_handle *trans,
  4833. struct btrfs_inode *inode)
  4834. {
  4835. /*
  4836. * If a directory was not modified, no dentries added or removed, we can
  4837. * and should avoid logging it.
  4838. */
  4839. if (S_ISDIR(inode->vfs_inode.i_mode) && inode->last_trans < trans->transid)
  4840. return false;
  4841. /*
  4842. * If this inode does not have new/updated/deleted xattrs since the last
  4843. * time it was logged and is flagged as logged in the current transaction,
  4844. * we can skip logging it. As for new/deleted names, those are updated in
  4845. * the log by link/unlink/rename operations.
  4846. * In case the inode was logged and then evicted and reloaded, its
  4847. * logged_trans will be 0, in which case we have to fully log it since
  4848. * logged_trans is a transient field, not persisted.
  4849. */
  4850. if (inode_logged(trans, inode, NULL) == 1 &&
  4851. !test_bit(BTRFS_INODE_COPY_EVERYTHING, &inode->runtime_flags))
  4852. return false;
  4853. return true;
  4854. }
  4855. struct btrfs_dir_list {
  4856. u64 ino;
  4857. struct list_head list;
  4858. };
  4859. /*
  4860. * Log the inodes of the new dentries of a directory.
  4861. * See process_dir_items_leaf() for details about why it is needed.
  4862. * This is a recursive operation - if an existing dentry corresponds to a
  4863. * directory, that directory's new entries are logged too (same behaviour as
  4864. * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
  4865. * the dentries point to we do not acquire their VFS lock, otherwise lockdep
  4866. * complains about the following circular lock dependency / possible deadlock:
  4867. *
  4868. * CPU0 CPU1
  4869. * ---- ----
  4870. * lock(&type->i_mutex_dir_key#3/2);
  4871. * lock(sb_internal#2);
  4872. * lock(&type->i_mutex_dir_key#3/2);
  4873. * lock(&sb->s_type->i_mutex_key#14);
  4874. *
  4875. * Where sb_internal is the lock (a counter that works as a lock) acquired by
  4876. * sb_start_intwrite() in btrfs_start_transaction().
  4877. * Not acquiring the VFS lock of the inodes is still safe because:
  4878. *
  4879. * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
  4880. * that while logging the inode new references (names) are added or removed
  4881. * from the inode, leaving the logged inode item with a link count that does
  4882. * not match the number of logged inode reference items. This is fine because
  4883. * at log replay time we compute the real number of links and correct the
  4884. * link count in the inode item (see replay_one_buffer() and
  4885. * link_to_fixup_dir());
  4886. *
  4887. * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
  4888. * while logging the inode's items new index items (key type
  4889. * BTRFS_DIR_INDEX_KEY) are added to fs/subvol tree and the logged inode item
  4890. * has a size that doesn't match the sum of the lengths of all the logged
  4891. * names - this is ok, not a problem, because at log replay time we set the
  4892. * directory's i_size to the correct value (see replay_one_name() and
  4893. * overwrite_item()).
  4894. */
  4895. static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
  4896. struct btrfs_inode *start_inode,
  4897. struct btrfs_log_ctx *ctx)
  4898. {
  4899. struct btrfs_root *root = start_inode->root;
  4900. struct btrfs_path *path;
  4901. LIST_HEAD(dir_list);
  4902. struct btrfs_dir_list *dir_elem;
  4903. u64 ino = btrfs_ino(start_inode);
  4904. struct btrfs_inode *curr_inode = start_inode;
  4905. int ret = 0;
  4906. /*
  4907. * If we are logging a new name, as part of a link or rename operation,
  4908. * don't bother logging new dentries, as we just want to log the names
  4909. * of an inode and that any new parents exist.
  4910. */
  4911. if (ctx->logging_new_name)
  4912. return 0;
  4913. path = btrfs_alloc_path();
  4914. if (!path)
  4915. return -ENOMEM;
  4916. /* Pairs with btrfs_add_delayed_iput below. */
  4917. ihold(&curr_inode->vfs_inode);
  4918. while (true) {
  4919. struct btrfs_key key;
  4920. struct btrfs_key found_key;
  4921. u64 next_index;
  4922. bool continue_curr_inode = true;
  4923. int iter_ret;
  4924. key.objectid = ino;
  4925. key.type = BTRFS_DIR_INDEX_KEY;
  4926. key.offset = btrfs_get_first_dir_index_to_log(curr_inode);
  4927. next_index = key.offset;
  4928. again:
  4929. btrfs_for_each_slot(root->log_root, &key, &found_key, path, iter_ret) {
  4930. struct extent_buffer *leaf = path->nodes[0];
  4931. struct btrfs_dir_item *di;
  4932. struct btrfs_key di_key;
  4933. struct btrfs_inode *di_inode;
  4934. int log_mode = LOG_INODE_EXISTS;
  4935. int type;
  4936. if (found_key.objectid != ino ||
  4937. found_key.type != BTRFS_DIR_INDEX_KEY) {
  4938. continue_curr_inode = false;
  4939. break;
  4940. }
  4941. next_index = found_key.offset + 1;
  4942. di = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
  4943. type = btrfs_dir_ftype(leaf, di);
  4944. if (btrfs_dir_transid(leaf, di) < trans->transid)
  4945. continue;
  4946. btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
  4947. if (di_key.type == BTRFS_ROOT_ITEM_KEY)
  4948. continue;
  4949. btrfs_release_path(path);
  4950. di_inode = btrfs_iget_logging(di_key.objectid, root);
  4951. if (IS_ERR(di_inode)) {
  4952. ret = PTR_ERR(di_inode);
  4953. goto out;
  4954. }
  4955. if (!need_log_inode(trans, di_inode)) {
  4956. btrfs_add_delayed_iput(di_inode);
  4957. break;
  4958. }
  4959. ctx->log_new_dentries = false;
  4960. if (type == BTRFS_FT_DIR)
  4961. log_mode = LOG_INODE_ALL;
  4962. ret = btrfs_log_inode(trans, di_inode, log_mode, ctx);
  4963. btrfs_add_delayed_iput(di_inode);
  4964. if (ret)
  4965. goto out;
  4966. if (ctx->log_new_dentries) {
  4967. dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
  4968. if (!dir_elem) {
  4969. ret = -ENOMEM;
  4970. goto out;
  4971. }
  4972. dir_elem->ino = di_key.objectid;
  4973. list_add_tail(&dir_elem->list, &dir_list);
  4974. }
  4975. break;
  4976. }
  4977. btrfs_release_path(path);
  4978. if (iter_ret < 0) {
  4979. ret = iter_ret;
  4980. goto out;
  4981. } else if (iter_ret > 0) {
  4982. continue_curr_inode = false;
  4983. } else {
  4984. key = found_key;
  4985. }
  4986. if (continue_curr_inode && key.offset < (u64)-1) {
  4987. key.offset++;
  4988. goto again;
  4989. }
  4990. btrfs_set_first_dir_index_to_log(curr_inode, next_index);
  4991. if (list_empty(&dir_list))
  4992. break;
  4993. dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list, list);
  4994. ino = dir_elem->ino;
  4995. list_del(&dir_elem->list);
  4996. kfree(dir_elem);
  4997. btrfs_add_delayed_iput(curr_inode);
  4998. curr_inode = btrfs_iget_logging(ino, root);
  4999. if (IS_ERR(curr_inode)) {
  5000. ret = PTR_ERR(curr_inode);
  5001. curr_inode = NULL;
  5002. break;
  5003. }
  5004. }
  5005. out:
  5006. btrfs_free_path(path);
  5007. if (curr_inode)
  5008. btrfs_add_delayed_iput(curr_inode);
  5009. if (ret) {
  5010. struct btrfs_dir_list *next;
  5011. list_for_each_entry_safe(dir_elem, next, &dir_list, list)
  5012. kfree(dir_elem);
  5013. }
  5014. return ret;
  5015. }
  5016. struct btrfs_ino_list {
  5017. u64 ino;
  5018. u64 parent;
  5019. struct list_head list;
  5020. };
  5021. static void free_conflicting_inodes(struct btrfs_log_ctx *ctx)
  5022. {
  5023. struct btrfs_ino_list *curr;
  5024. struct btrfs_ino_list *next;
  5025. list_for_each_entry_safe(curr, next, &ctx->conflict_inodes, list) {
  5026. list_del(&curr->list);
  5027. kfree(curr);
  5028. }
  5029. }
  5030. static int conflicting_inode_is_dir(struct btrfs_root *root, u64 ino,
  5031. struct btrfs_path *path)
  5032. {
  5033. struct btrfs_key key;
  5034. int ret;
  5035. key.objectid = ino;
  5036. key.type = BTRFS_INODE_ITEM_KEY;
  5037. key.offset = 0;
  5038. path->search_commit_root = 1;
  5039. path->skip_locking = 1;
  5040. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  5041. if (WARN_ON_ONCE(ret > 0)) {
  5042. /*
  5043. * We have previously found the inode through the commit root
  5044. * so this should not happen. If it does, just error out and
  5045. * fallback to a transaction commit.
  5046. */
  5047. ret = -ENOENT;
  5048. } else if (ret == 0) {
  5049. struct btrfs_inode_item *item;
  5050. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  5051. struct btrfs_inode_item);
  5052. if (S_ISDIR(btrfs_inode_mode(path->nodes[0], item)))
  5053. ret = 1;
  5054. }
  5055. btrfs_release_path(path);
  5056. path->search_commit_root = 0;
  5057. path->skip_locking = 0;
  5058. return ret;
  5059. }
  5060. static int add_conflicting_inode(struct btrfs_trans_handle *trans,
  5061. struct btrfs_root *root,
  5062. struct btrfs_path *path,
  5063. u64 ino, u64 parent,
  5064. struct btrfs_log_ctx *ctx)
  5065. {
  5066. struct btrfs_ino_list *ino_elem;
  5067. struct btrfs_inode *inode;
  5068. /*
  5069. * It's rare to have a lot of conflicting inodes, in practice it is not
  5070. * common to have more than 1 or 2. We don't want to collect too many,
  5071. * as we could end up logging too many inodes (even if only in
  5072. * LOG_INODE_EXISTS mode) and slow down other fsyncs or transaction
  5073. * commits.
  5074. */
  5075. if (ctx->num_conflict_inodes >= MAX_CONFLICT_INODES)
  5076. return BTRFS_LOG_FORCE_COMMIT;
  5077. inode = btrfs_iget_logging(ino, root);
  5078. /*
  5079. * If the other inode that had a conflicting dir entry was deleted in
  5080. * the current transaction then we either:
  5081. *
  5082. * 1) Log the parent directory (later after adding it to the list) if
  5083. * the inode is a directory. This is because it may be a deleted
  5084. * subvolume/snapshot or it may be a regular directory that had
  5085. * deleted subvolumes/snapshots (or subdirectories that had them),
  5086. * and at the moment we can't deal with dropping subvolumes/snapshots
  5087. * during log replay. So we just log the parent, which will result in
  5088. * a fallback to a transaction commit if we are dealing with those
  5089. * cases (last_unlink_trans will match the current transaction);
  5090. *
  5091. * 2) Do nothing if it's not a directory. During log replay we simply
  5092. * unlink the conflicting dentry from the parent directory and then
  5093. * add the dentry for our inode. Like this we can avoid logging the
  5094. * parent directory (and maybe fallback to a transaction commit in
  5095. * case it has a last_unlink_trans == trans->transid, due to moving
  5096. * some inode from it to some other directory).
  5097. */
  5098. if (IS_ERR(inode)) {
  5099. int ret = PTR_ERR(inode);
  5100. if (ret != -ENOENT)
  5101. return ret;
  5102. ret = conflicting_inode_is_dir(root, ino, path);
  5103. /* Not a directory or we got an error. */
  5104. if (ret <= 0)
  5105. return ret;
  5106. /* Conflicting inode is a directory, so we'll log its parent. */
  5107. ino_elem = kmalloc(sizeof(*ino_elem), GFP_NOFS);
  5108. if (!ino_elem)
  5109. return -ENOMEM;
  5110. ino_elem->ino = ino;
  5111. ino_elem->parent = parent;
  5112. list_add_tail(&ino_elem->list, &ctx->conflict_inodes);
  5113. ctx->num_conflict_inodes++;
  5114. return 0;
  5115. }
  5116. /*
  5117. * If the inode was already logged skip it - otherwise we can hit an
  5118. * infinite loop. Example:
  5119. *
  5120. * From the commit root (previous transaction) we have the following
  5121. * inodes:
  5122. *
  5123. * inode 257 a directory
  5124. * inode 258 with references "zz" and "zz_link" on inode 257
  5125. * inode 259 with reference "a" on inode 257
  5126. *
  5127. * And in the current (uncommitted) transaction we have:
  5128. *
  5129. * inode 257 a directory, unchanged
  5130. * inode 258 with references "a" and "a2" on inode 257
  5131. * inode 259 with reference "zz_link" on inode 257
  5132. * inode 261 with reference "zz" on inode 257
  5133. *
  5134. * When logging inode 261 the following infinite loop could
  5135. * happen if we don't skip already logged inodes:
  5136. *
  5137. * - we detect inode 258 as a conflicting inode, with inode 261
  5138. * on reference "zz", and log it;
  5139. *
  5140. * - we detect inode 259 as a conflicting inode, with inode 258
  5141. * on reference "a", and log it;
  5142. *
  5143. * - we detect inode 258 as a conflicting inode, with inode 259
  5144. * on reference "zz_link", and log it - again! After this we
  5145. * repeat the above steps forever.
  5146. *
  5147. * Here we can use need_log_inode() because we only need to log the
  5148. * inode in LOG_INODE_EXISTS mode and rename operations update the log,
  5149. * so that the log ends up with the new name and without the old name.
  5150. */
  5151. if (!need_log_inode(trans, inode)) {
  5152. btrfs_add_delayed_iput(inode);
  5153. return 0;
  5154. }
  5155. btrfs_add_delayed_iput(inode);
  5156. ino_elem = kmalloc(sizeof(*ino_elem), GFP_NOFS);
  5157. if (!ino_elem)
  5158. return -ENOMEM;
  5159. ino_elem->ino = ino;
  5160. ino_elem->parent = parent;
  5161. list_add_tail(&ino_elem->list, &ctx->conflict_inodes);
  5162. ctx->num_conflict_inodes++;
  5163. return 0;
  5164. }
  5165. static int log_conflicting_inodes(struct btrfs_trans_handle *trans,
  5166. struct btrfs_root *root,
  5167. struct btrfs_log_ctx *ctx)
  5168. {
  5169. int ret = 0;
  5170. /*
  5171. * Conflicting inodes are logged by the first call to btrfs_log_inode(),
  5172. * otherwise we could have unbounded recursion of btrfs_log_inode()
  5173. * calls. This check guarantees we can have only 1 level of recursion.
  5174. */
  5175. if (ctx->logging_conflict_inodes)
  5176. return 0;
  5177. ctx->logging_conflict_inodes = true;
  5178. /*
  5179. * New conflicting inodes may be found and added to the list while we
  5180. * are logging a conflicting inode, so keep iterating while the list is
  5181. * not empty.
  5182. */
  5183. while (!list_empty(&ctx->conflict_inodes)) {
  5184. struct btrfs_ino_list *curr;
  5185. struct btrfs_inode *inode;
  5186. u64 ino;
  5187. u64 parent;
  5188. curr = list_first_entry(&ctx->conflict_inodes,
  5189. struct btrfs_ino_list, list);
  5190. ino = curr->ino;
  5191. parent = curr->parent;
  5192. list_del(&curr->list);
  5193. kfree(curr);
  5194. inode = btrfs_iget_logging(ino, root);
  5195. /*
  5196. * If the other inode that had a conflicting dir entry was
  5197. * deleted in the current transaction, we need to log its parent
  5198. * directory. See the comment at add_conflicting_inode().
  5199. */
  5200. if (IS_ERR(inode)) {
  5201. ret = PTR_ERR(inode);
  5202. if (ret != -ENOENT)
  5203. break;
  5204. inode = btrfs_iget_logging(parent, root);
  5205. if (IS_ERR(inode)) {
  5206. ret = PTR_ERR(inode);
  5207. break;
  5208. }
  5209. /*
  5210. * Always log the directory, we cannot make this
  5211. * conditional on need_log_inode() because the directory
  5212. * might have been logged in LOG_INODE_EXISTS mode or
  5213. * the dir index of the conflicting inode is not in a
  5214. * dir index key range logged for the directory. So we
  5215. * must make sure the deletion is recorded.
  5216. */
  5217. ret = btrfs_log_inode(trans, inode, LOG_INODE_ALL, ctx);
  5218. btrfs_add_delayed_iput(inode);
  5219. if (ret)
  5220. break;
  5221. continue;
  5222. }
  5223. /*
  5224. * Here we can use need_log_inode() because we only need to log
  5225. * the inode in LOG_INODE_EXISTS mode and rename operations
  5226. * update the log, so that the log ends up with the new name and
  5227. * without the old name.
  5228. *
  5229. * We did this check at add_conflicting_inode(), but here we do
  5230. * it again because if some other task logged the inode after
  5231. * that, we can avoid doing it again.
  5232. */
  5233. if (!need_log_inode(trans, inode)) {
  5234. btrfs_add_delayed_iput(inode);
  5235. continue;
  5236. }
  5237. /*
  5238. * We are safe logging the other inode without acquiring its
  5239. * lock as long as we log with the LOG_INODE_EXISTS mode. We
  5240. * are safe against concurrent renames of the other inode as
  5241. * well because during a rename we pin the log and update the
  5242. * log with the new name before we unpin it.
  5243. */
  5244. ret = btrfs_log_inode(trans, inode, LOG_INODE_EXISTS, ctx);
  5245. btrfs_add_delayed_iput(inode);
  5246. if (ret)
  5247. break;
  5248. }
  5249. ctx->logging_conflict_inodes = false;
  5250. if (ret)
  5251. free_conflicting_inodes(ctx);
  5252. return ret;
  5253. }
  5254. static int copy_inode_items_to_log(struct btrfs_trans_handle *trans,
  5255. struct btrfs_inode *inode,
  5256. struct btrfs_key *min_key,
  5257. const struct btrfs_key *max_key,
  5258. struct btrfs_path *path,
  5259. struct btrfs_path *dst_path,
  5260. const u64 logged_isize,
  5261. const int inode_only,
  5262. struct btrfs_log_ctx *ctx,
  5263. bool *need_log_inode_item)
  5264. {
  5265. const u64 i_size = i_size_read(&inode->vfs_inode);
  5266. struct btrfs_root *root = inode->root;
  5267. int ins_start_slot = 0;
  5268. int ins_nr = 0;
  5269. int ret;
  5270. while (1) {
  5271. ret = btrfs_search_forward(root, min_key, path, trans->transid);
  5272. if (ret < 0)
  5273. return ret;
  5274. if (ret > 0) {
  5275. ret = 0;
  5276. break;
  5277. }
  5278. again:
  5279. /* Note, ins_nr might be > 0 here, cleanup outside the loop */
  5280. if (min_key->objectid != max_key->objectid)
  5281. break;
  5282. if (min_key->type > max_key->type)
  5283. break;
  5284. if (min_key->type == BTRFS_INODE_ITEM_KEY) {
  5285. *need_log_inode_item = false;
  5286. } else if (min_key->type == BTRFS_EXTENT_DATA_KEY &&
  5287. min_key->offset >= i_size) {
  5288. /*
  5289. * Extents at and beyond eof are logged with
  5290. * btrfs_log_prealloc_extents().
  5291. * Only regular files have BTRFS_EXTENT_DATA_KEY keys,
  5292. * and no keys greater than that, so bail out.
  5293. */
  5294. break;
  5295. } else if ((min_key->type == BTRFS_INODE_REF_KEY ||
  5296. min_key->type == BTRFS_INODE_EXTREF_KEY) &&
  5297. (inode->generation == trans->transid ||
  5298. ctx->logging_conflict_inodes)) {
  5299. u64 other_ino = 0;
  5300. u64 other_parent = 0;
  5301. ret = btrfs_check_ref_name_override(path->nodes[0],
  5302. path->slots[0], min_key, inode,
  5303. &other_ino, &other_parent);
  5304. if (ret < 0) {
  5305. return ret;
  5306. } else if (ret > 0 &&
  5307. other_ino != btrfs_ino(ctx->inode)) {
  5308. if (ins_nr > 0) {
  5309. ins_nr++;
  5310. } else {
  5311. ins_nr = 1;
  5312. ins_start_slot = path->slots[0];
  5313. }
  5314. ret = copy_items(trans, inode, dst_path, path,
  5315. ins_start_slot, ins_nr,
  5316. inode_only, logged_isize, ctx);
  5317. if (ret < 0)
  5318. return ret;
  5319. ins_nr = 0;
  5320. btrfs_release_path(path);
  5321. ret = add_conflicting_inode(trans, root, path,
  5322. other_ino,
  5323. other_parent, ctx);
  5324. if (ret)
  5325. return ret;
  5326. goto next_key;
  5327. }
  5328. } else if (min_key->type == BTRFS_XATTR_ITEM_KEY) {
  5329. /* Skip xattrs, logged later with btrfs_log_all_xattrs() */
  5330. if (ins_nr == 0)
  5331. goto next_slot;
  5332. ret = copy_items(trans, inode, dst_path, path,
  5333. ins_start_slot,
  5334. ins_nr, inode_only, logged_isize, ctx);
  5335. if (ret < 0)
  5336. return ret;
  5337. ins_nr = 0;
  5338. goto next_slot;
  5339. }
  5340. if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
  5341. ins_nr++;
  5342. goto next_slot;
  5343. } else if (!ins_nr) {
  5344. ins_start_slot = path->slots[0];
  5345. ins_nr = 1;
  5346. goto next_slot;
  5347. }
  5348. ret = copy_items(trans, inode, dst_path, path, ins_start_slot,
  5349. ins_nr, inode_only, logged_isize, ctx);
  5350. if (ret < 0)
  5351. return ret;
  5352. ins_nr = 1;
  5353. ins_start_slot = path->slots[0];
  5354. next_slot:
  5355. path->slots[0]++;
  5356. if (path->slots[0] < btrfs_header_nritems(path->nodes[0])) {
  5357. btrfs_item_key_to_cpu(path->nodes[0], min_key,
  5358. path->slots[0]);
  5359. goto again;
  5360. }
  5361. if (ins_nr) {
  5362. ret = copy_items(trans, inode, dst_path, path,
  5363. ins_start_slot, ins_nr, inode_only,
  5364. logged_isize, ctx);
  5365. if (ret < 0)
  5366. return ret;
  5367. ins_nr = 0;
  5368. }
  5369. btrfs_release_path(path);
  5370. next_key:
  5371. if (min_key->offset < (u64)-1) {
  5372. min_key->offset++;
  5373. } else if (min_key->type < max_key->type) {
  5374. min_key->type++;
  5375. min_key->offset = 0;
  5376. } else {
  5377. break;
  5378. }
  5379. /*
  5380. * We may process many leaves full of items for our inode, so
  5381. * avoid monopolizing a cpu for too long by rescheduling while
  5382. * not holding locks on any tree.
  5383. */
  5384. cond_resched();
  5385. }
  5386. if (ins_nr) {
  5387. ret = copy_items(trans, inode, dst_path, path, ins_start_slot,
  5388. ins_nr, inode_only, logged_isize, ctx);
  5389. if (ret)
  5390. return ret;
  5391. }
  5392. if (inode_only == LOG_INODE_ALL && S_ISREG(inode->vfs_inode.i_mode)) {
  5393. /*
  5394. * Release the path because otherwise we might attempt to double
  5395. * lock the same leaf with btrfs_log_prealloc_extents() below.
  5396. */
  5397. btrfs_release_path(path);
  5398. ret = btrfs_log_prealloc_extents(trans, inode, dst_path, ctx);
  5399. }
  5400. return ret;
  5401. }
  5402. static int insert_delayed_items_batch(struct btrfs_trans_handle *trans,
  5403. struct btrfs_root *log,
  5404. struct btrfs_path *path,
  5405. const struct btrfs_item_batch *batch,
  5406. const struct btrfs_delayed_item *first_item)
  5407. {
  5408. const struct btrfs_delayed_item *curr = first_item;
  5409. int ret;
  5410. ret = btrfs_insert_empty_items(trans, log, path, batch);
  5411. if (ret)
  5412. return ret;
  5413. for (int i = 0; i < batch->nr; i++) {
  5414. char *data_ptr;
  5415. data_ptr = btrfs_item_ptr(path->nodes[0], path->slots[0], char);
  5416. write_extent_buffer(path->nodes[0], &curr->data,
  5417. (unsigned long)data_ptr, curr->data_len);
  5418. curr = list_next_entry(curr, log_list);
  5419. path->slots[0]++;
  5420. }
  5421. btrfs_release_path(path);
  5422. return 0;
  5423. }
  5424. static int log_delayed_insertion_items(struct btrfs_trans_handle *trans,
  5425. struct btrfs_inode *inode,
  5426. struct btrfs_path *path,
  5427. const struct list_head *delayed_ins_list,
  5428. struct btrfs_log_ctx *ctx)
  5429. {
  5430. /* 195 (4095 bytes of keys and sizes) fits in a single 4K page. */
  5431. const int max_batch_size = 195;
  5432. const int leaf_data_size = BTRFS_LEAF_DATA_SIZE(trans->fs_info);
  5433. const u64 ino = btrfs_ino(inode);
  5434. struct btrfs_root *log = inode->root->log_root;
  5435. struct btrfs_item_batch batch = {
  5436. .nr = 0,
  5437. .total_data_size = 0,
  5438. };
  5439. const struct btrfs_delayed_item *first = NULL;
  5440. const struct btrfs_delayed_item *curr;
  5441. char *ins_data;
  5442. struct btrfs_key *ins_keys;
  5443. u32 *ins_sizes;
  5444. u64 curr_batch_size = 0;
  5445. int batch_idx = 0;
  5446. int ret;
  5447. /* We are adding dir index items to the log tree. */
  5448. lockdep_assert_held(&inode->log_mutex);
  5449. /*
  5450. * We collect delayed items before copying index keys from the subvolume
  5451. * to the log tree. However just after we collected them, they may have
  5452. * been flushed (all of them or just some of them), and therefore we
  5453. * could have copied them from the subvolume tree to the log tree.
  5454. * So find the first delayed item that was not yet logged (they are
  5455. * sorted by index number).
  5456. */
  5457. list_for_each_entry(curr, delayed_ins_list, log_list) {
  5458. if (curr->index > inode->last_dir_index_offset) {
  5459. first = curr;
  5460. break;
  5461. }
  5462. }
  5463. /* Empty list or all delayed items were already logged. */
  5464. if (!first)
  5465. return 0;
  5466. ins_data = kmalloc(max_batch_size * sizeof(u32) +
  5467. max_batch_size * sizeof(struct btrfs_key), GFP_NOFS);
  5468. if (!ins_data)
  5469. return -ENOMEM;
  5470. ins_sizes = (u32 *)ins_data;
  5471. batch.data_sizes = ins_sizes;
  5472. ins_keys = (struct btrfs_key *)(ins_data + max_batch_size * sizeof(u32));
  5473. batch.keys = ins_keys;
  5474. curr = first;
  5475. while (!list_entry_is_head(curr, delayed_ins_list, log_list)) {
  5476. const u32 curr_size = curr->data_len + sizeof(struct btrfs_item);
  5477. if (curr_batch_size + curr_size > leaf_data_size ||
  5478. batch.nr == max_batch_size) {
  5479. ret = insert_delayed_items_batch(trans, log, path,
  5480. &batch, first);
  5481. if (ret)
  5482. goto out;
  5483. batch_idx = 0;
  5484. batch.nr = 0;
  5485. batch.total_data_size = 0;
  5486. curr_batch_size = 0;
  5487. first = curr;
  5488. }
  5489. ins_sizes[batch_idx] = curr->data_len;
  5490. ins_keys[batch_idx].objectid = ino;
  5491. ins_keys[batch_idx].type = BTRFS_DIR_INDEX_KEY;
  5492. ins_keys[batch_idx].offset = curr->index;
  5493. curr_batch_size += curr_size;
  5494. batch.total_data_size += curr->data_len;
  5495. batch.nr++;
  5496. batch_idx++;
  5497. curr = list_next_entry(curr, log_list);
  5498. }
  5499. ASSERT(batch.nr >= 1);
  5500. ret = insert_delayed_items_batch(trans, log, path, &batch, first);
  5501. curr = list_last_entry(delayed_ins_list, struct btrfs_delayed_item,
  5502. log_list);
  5503. inode->last_dir_index_offset = curr->index;
  5504. out:
  5505. kfree(ins_data);
  5506. return ret;
  5507. }
  5508. static int log_delayed_deletions_full(struct btrfs_trans_handle *trans,
  5509. struct btrfs_inode *inode,
  5510. struct btrfs_path *path,
  5511. const struct list_head *delayed_del_list,
  5512. struct btrfs_log_ctx *ctx)
  5513. {
  5514. const u64 ino = btrfs_ino(inode);
  5515. const struct btrfs_delayed_item *curr;
  5516. curr = list_first_entry(delayed_del_list, struct btrfs_delayed_item,
  5517. log_list);
  5518. while (!list_entry_is_head(curr, delayed_del_list, log_list)) {
  5519. u64 first_dir_index = curr->index;
  5520. u64 last_dir_index;
  5521. const struct btrfs_delayed_item *next;
  5522. int ret;
  5523. /*
  5524. * Find a range of consecutive dir index items to delete. Like
  5525. * this we log a single dir range item spanning several contiguous
  5526. * dir items instead of logging one range item per dir index item.
  5527. */
  5528. next = list_next_entry(curr, log_list);
  5529. while (!list_entry_is_head(next, delayed_del_list, log_list)) {
  5530. if (next->index != curr->index + 1)
  5531. break;
  5532. curr = next;
  5533. next = list_next_entry(next, log_list);
  5534. }
  5535. last_dir_index = curr->index;
  5536. ASSERT(last_dir_index >= first_dir_index);
  5537. ret = insert_dir_log_key(trans, inode->root->log_root, path,
  5538. ino, first_dir_index, last_dir_index);
  5539. if (ret)
  5540. return ret;
  5541. curr = list_next_entry(curr, log_list);
  5542. }
  5543. return 0;
  5544. }
  5545. static int batch_delete_dir_index_items(struct btrfs_trans_handle *trans,
  5546. struct btrfs_inode *inode,
  5547. struct btrfs_path *path,
  5548. struct btrfs_log_ctx *ctx,
  5549. const struct list_head *delayed_del_list,
  5550. const struct btrfs_delayed_item *first,
  5551. const struct btrfs_delayed_item **last_ret)
  5552. {
  5553. const struct btrfs_delayed_item *next;
  5554. struct extent_buffer *leaf = path->nodes[0];
  5555. const int last_slot = btrfs_header_nritems(leaf) - 1;
  5556. int slot = path->slots[0] + 1;
  5557. const u64 ino = btrfs_ino(inode);
  5558. next = list_next_entry(first, log_list);
  5559. while (slot < last_slot &&
  5560. !list_entry_is_head(next, delayed_del_list, log_list)) {
  5561. struct btrfs_key key;
  5562. btrfs_item_key_to_cpu(leaf, &key, slot);
  5563. if (key.objectid != ino ||
  5564. key.type != BTRFS_DIR_INDEX_KEY ||
  5565. key.offset != next->index)
  5566. break;
  5567. slot++;
  5568. *last_ret = next;
  5569. next = list_next_entry(next, log_list);
  5570. }
  5571. return btrfs_del_items(trans, inode->root->log_root, path,
  5572. path->slots[0], slot - path->slots[0]);
  5573. }
  5574. static int log_delayed_deletions_incremental(struct btrfs_trans_handle *trans,
  5575. struct btrfs_inode *inode,
  5576. struct btrfs_path *path,
  5577. const struct list_head *delayed_del_list,
  5578. struct btrfs_log_ctx *ctx)
  5579. {
  5580. struct btrfs_root *log = inode->root->log_root;
  5581. const struct btrfs_delayed_item *curr;
  5582. u64 last_range_start = 0;
  5583. u64 last_range_end = 0;
  5584. struct btrfs_key key;
  5585. key.objectid = btrfs_ino(inode);
  5586. key.type = BTRFS_DIR_INDEX_KEY;
  5587. curr = list_first_entry(delayed_del_list, struct btrfs_delayed_item,
  5588. log_list);
  5589. while (!list_entry_is_head(curr, delayed_del_list, log_list)) {
  5590. const struct btrfs_delayed_item *last = curr;
  5591. u64 first_dir_index = curr->index;
  5592. u64 last_dir_index;
  5593. bool deleted_items = false;
  5594. int ret;
  5595. key.offset = curr->index;
  5596. ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
  5597. if (ret < 0) {
  5598. return ret;
  5599. } else if (ret == 0) {
  5600. ret = batch_delete_dir_index_items(trans, inode, path, ctx,
  5601. delayed_del_list, curr,
  5602. &last);
  5603. if (ret)
  5604. return ret;
  5605. deleted_items = true;
  5606. }
  5607. btrfs_release_path(path);
  5608. /*
  5609. * If we deleted items from the leaf, it means we have a range
  5610. * item logging their range, so no need to add one or update an
  5611. * existing one. Otherwise we have to log a dir range item.
  5612. */
  5613. if (deleted_items)
  5614. goto next_batch;
  5615. last_dir_index = last->index;
  5616. ASSERT(last_dir_index >= first_dir_index);
  5617. /*
  5618. * If this range starts right after where the previous one ends,
  5619. * then we want to reuse the previous range item and change its
  5620. * end offset to the end of this range. This is just to minimize
  5621. * leaf space usage, by avoiding adding a new range item.
  5622. */
  5623. if (last_range_end != 0 && first_dir_index == last_range_end + 1)
  5624. first_dir_index = last_range_start;
  5625. ret = insert_dir_log_key(trans, log, path, key.objectid,
  5626. first_dir_index, last_dir_index);
  5627. if (ret)
  5628. return ret;
  5629. last_range_start = first_dir_index;
  5630. last_range_end = last_dir_index;
  5631. next_batch:
  5632. curr = list_next_entry(last, log_list);
  5633. }
  5634. return 0;
  5635. }
  5636. static int log_delayed_deletion_items(struct btrfs_trans_handle *trans,
  5637. struct btrfs_inode *inode,
  5638. struct btrfs_path *path,
  5639. const struct list_head *delayed_del_list,
  5640. struct btrfs_log_ctx *ctx)
  5641. {
  5642. /*
  5643. * We are deleting dir index items from the log tree or adding range
  5644. * items to it.
  5645. */
  5646. lockdep_assert_held(&inode->log_mutex);
  5647. if (list_empty(delayed_del_list))
  5648. return 0;
  5649. if (ctx->logged_before)
  5650. return log_delayed_deletions_incremental(trans, inode, path,
  5651. delayed_del_list, ctx);
  5652. return log_delayed_deletions_full(trans, inode, path, delayed_del_list,
  5653. ctx);
  5654. }
  5655. /*
  5656. * Similar logic as for log_new_dir_dentries(), but it iterates over the delayed
  5657. * items instead of the subvolume tree.
  5658. */
  5659. static int log_new_delayed_dentries(struct btrfs_trans_handle *trans,
  5660. struct btrfs_inode *inode,
  5661. const struct list_head *delayed_ins_list,
  5662. struct btrfs_log_ctx *ctx)
  5663. {
  5664. const bool orig_log_new_dentries = ctx->log_new_dentries;
  5665. struct btrfs_delayed_item *item;
  5666. int ret = 0;
  5667. /*
  5668. * No need for the log mutex, plus to avoid potential deadlocks or
  5669. * lockdep annotations due to nesting of delayed inode mutexes and log
  5670. * mutexes.
  5671. */
  5672. lockdep_assert_not_held(&inode->log_mutex);
  5673. ASSERT(!ctx->logging_new_delayed_dentries);
  5674. ctx->logging_new_delayed_dentries = true;
  5675. list_for_each_entry(item, delayed_ins_list, log_list) {
  5676. struct btrfs_dir_item *dir_item;
  5677. struct btrfs_inode *di_inode;
  5678. struct btrfs_key key;
  5679. int log_mode = LOG_INODE_EXISTS;
  5680. dir_item = (struct btrfs_dir_item *)item->data;
  5681. btrfs_disk_key_to_cpu(&key, &dir_item->location);
  5682. if (key.type == BTRFS_ROOT_ITEM_KEY)
  5683. continue;
  5684. di_inode = btrfs_iget_logging(key.objectid, inode->root);
  5685. if (IS_ERR(di_inode)) {
  5686. ret = PTR_ERR(di_inode);
  5687. break;
  5688. }
  5689. if (!need_log_inode(trans, di_inode)) {
  5690. btrfs_add_delayed_iput(di_inode);
  5691. continue;
  5692. }
  5693. if (btrfs_stack_dir_ftype(dir_item) == BTRFS_FT_DIR)
  5694. log_mode = LOG_INODE_ALL;
  5695. ctx->log_new_dentries = false;
  5696. ret = btrfs_log_inode(trans, di_inode, log_mode, ctx);
  5697. if (!ret && ctx->log_new_dentries)
  5698. ret = log_new_dir_dentries(trans, di_inode, ctx);
  5699. btrfs_add_delayed_iput(di_inode);
  5700. if (ret)
  5701. break;
  5702. }
  5703. ctx->log_new_dentries = orig_log_new_dentries;
  5704. ctx->logging_new_delayed_dentries = false;
  5705. return ret;
  5706. }
  5707. /* log a single inode in the tree log.
  5708. * At least one parent directory for this inode must exist in the tree
  5709. * or be logged already.
  5710. *
  5711. * Any items from this inode changed by the current transaction are copied
  5712. * to the log tree. An extra reference is taken on any extents in this
  5713. * file, allowing us to avoid a whole pile of corner cases around logging
  5714. * blocks that have been removed from the tree.
  5715. *
  5716. * See LOG_INODE_ALL and related defines for a description of what inode_only
  5717. * does.
  5718. *
  5719. * This handles both files and directories.
  5720. */
  5721. static int btrfs_log_inode(struct btrfs_trans_handle *trans,
  5722. struct btrfs_inode *inode,
  5723. int inode_only,
  5724. struct btrfs_log_ctx *ctx)
  5725. {
  5726. struct btrfs_path *path;
  5727. struct btrfs_path *dst_path;
  5728. struct btrfs_key min_key;
  5729. struct btrfs_key max_key;
  5730. struct btrfs_root *log = inode->root->log_root;
  5731. int ret;
  5732. bool fast_search = false;
  5733. u64 ino = btrfs_ino(inode);
  5734. struct extent_map_tree *em_tree = &inode->extent_tree;
  5735. u64 logged_isize = 0;
  5736. bool need_log_inode_item = true;
  5737. bool xattrs_logged = false;
  5738. bool inode_item_dropped = true;
  5739. bool full_dir_logging = false;
  5740. LIST_HEAD(delayed_ins_list);
  5741. LIST_HEAD(delayed_del_list);
  5742. path = btrfs_alloc_path();
  5743. if (!path)
  5744. return -ENOMEM;
  5745. dst_path = btrfs_alloc_path();
  5746. if (!dst_path) {
  5747. btrfs_free_path(path);
  5748. return -ENOMEM;
  5749. }
  5750. min_key.objectid = ino;
  5751. min_key.type = BTRFS_INODE_ITEM_KEY;
  5752. min_key.offset = 0;
  5753. max_key.objectid = ino;
  5754. /* today the code can only do partial logging of directories */
  5755. if (S_ISDIR(inode->vfs_inode.i_mode) ||
  5756. (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  5757. &inode->runtime_flags) &&
  5758. inode_only >= LOG_INODE_EXISTS))
  5759. max_key.type = BTRFS_XATTR_ITEM_KEY;
  5760. else
  5761. max_key.type = (u8)-1;
  5762. max_key.offset = (u64)-1;
  5763. if (S_ISDIR(inode->vfs_inode.i_mode) && inode_only == LOG_INODE_ALL)
  5764. full_dir_logging = true;
  5765. /*
  5766. * If we are logging a directory while we are logging dentries of the
  5767. * delayed items of some other inode, then we need to flush the delayed
  5768. * items of this directory and not log the delayed items directly. This
  5769. * is to prevent more than one level of recursion into btrfs_log_inode()
  5770. * by having something like this:
  5771. *
  5772. * $ mkdir -p a/b/c/d/e/f/g/h/...
  5773. * $ xfs_io -c "fsync" a
  5774. *
  5775. * Where all directories in the path did not exist before and are
  5776. * created in the current transaction.
  5777. * So in such a case we directly log the delayed items of the main
  5778. * directory ("a") without flushing them first, while for each of its
  5779. * subdirectories we flush their delayed items before logging them.
  5780. * This prevents a potential unbounded recursion like this:
  5781. *
  5782. * btrfs_log_inode()
  5783. * log_new_delayed_dentries()
  5784. * btrfs_log_inode()
  5785. * log_new_delayed_dentries()
  5786. * btrfs_log_inode()
  5787. * log_new_delayed_dentries()
  5788. * (...)
  5789. *
  5790. * We have thresholds for the maximum number of delayed items to have in
  5791. * memory, and once they are hit, the items are flushed asynchronously.
  5792. * However the limit is quite high, so lets prevent deep levels of
  5793. * recursion to happen by limiting the maximum depth to be 1.
  5794. */
  5795. if (full_dir_logging && ctx->logging_new_delayed_dentries) {
  5796. ret = btrfs_commit_inode_delayed_items(trans, inode);
  5797. if (ret)
  5798. goto out;
  5799. }
  5800. mutex_lock(&inode->log_mutex);
  5801. /*
  5802. * For symlinks, we must always log their content, which is stored in an
  5803. * inline extent, otherwise we could end up with an empty symlink after
  5804. * log replay, which is invalid on linux (symlink(2) returns -ENOENT if
  5805. * one attempts to create an empty symlink).
  5806. * We don't need to worry about flushing delalloc, because when we create
  5807. * the inline extent when the symlink is created (we never have delalloc
  5808. * for symlinks).
  5809. */
  5810. if (S_ISLNK(inode->vfs_inode.i_mode))
  5811. inode_only = LOG_INODE_ALL;
  5812. /*
  5813. * Before logging the inode item, cache the value returned by
  5814. * inode_logged(), because after that we have the need to figure out if
  5815. * the inode was previously logged in this transaction.
  5816. */
  5817. ret = inode_logged(trans, inode, path);
  5818. if (ret < 0)
  5819. goto out_unlock;
  5820. ctx->logged_before = (ret == 1);
  5821. ret = 0;
  5822. /*
  5823. * This is for cases where logging a directory could result in losing a
  5824. * a file after replaying the log. For example, if we move a file from a
  5825. * directory A to a directory B, then fsync directory A, we have no way
  5826. * to known the file was moved from A to B, so logging just A would
  5827. * result in losing the file after a log replay.
  5828. */
  5829. if (full_dir_logging && inode->last_unlink_trans >= trans->transid) {
  5830. ret = BTRFS_LOG_FORCE_COMMIT;
  5831. goto out_unlock;
  5832. }
  5833. /*
  5834. * a brute force approach to making sure we get the most uptodate
  5835. * copies of everything.
  5836. */
  5837. if (S_ISDIR(inode->vfs_inode.i_mode)) {
  5838. clear_bit(BTRFS_INODE_COPY_EVERYTHING, &inode->runtime_flags);
  5839. if (ctx->logged_before)
  5840. ret = drop_inode_items(trans, log, path, inode,
  5841. BTRFS_XATTR_ITEM_KEY);
  5842. } else {
  5843. if (inode_only == LOG_INODE_EXISTS && ctx->logged_before) {
  5844. /*
  5845. * Make sure the new inode item we write to the log has
  5846. * the same isize as the current one (if it exists).
  5847. * This is necessary to prevent data loss after log
  5848. * replay, and also to prevent doing a wrong expanding
  5849. * truncate - for e.g. create file, write 4K into offset
  5850. * 0, fsync, write 4K into offset 4096, add hard link,
  5851. * fsync some other file (to sync log), power fail - if
  5852. * we use the inode's current i_size, after log replay
  5853. * we get a 8Kb file, with the last 4Kb extent as a hole
  5854. * (zeroes), as if an expanding truncate happened,
  5855. * instead of getting a file of 4Kb only.
  5856. */
  5857. ret = logged_inode_size(log, inode, path, &logged_isize);
  5858. if (ret)
  5859. goto out_unlock;
  5860. }
  5861. if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  5862. &inode->runtime_flags)) {
  5863. if (inode_only == LOG_INODE_EXISTS) {
  5864. max_key.type = BTRFS_XATTR_ITEM_KEY;
  5865. if (ctx->logged_before)
  5866. ret = drop_inode_items(trans, log, path,
  5867. inode, max_key.type);
  5868. } else {
  5869. clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  5870. &inode->runtime_flags);
  5871. clear_bit(BTRFS_INODE_COPY_EVERYTHING,
  5872. &inode->runtime_flags);
  5873. if (ctx->logged_before)
  5874. ret = truncate_inode_items(trans, log,
  5875. inode, 0, 0);
  5876. }
  5877. } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
  5878. &inode->runtime_flags) ||
  5879. inode_only == LOG_INODE_EXISTS) {
  5880. if (inode_only == LOG_INODE_ALL)
  5881. fast_search = true;
  5882. max_key.type = BTRFS_XATTR_ITEM_KEY;
  5883. if (ctx->logged_before)
  5884. ret = drop_inode_items(trans, log, path, inode,
  5885. max_key.type);
  5886. } else {
  5887. if (inode_only == LOG_INODE_ALL)
  5888. fast_search = true;
  5889. inode_item_dropped = false;
  5890. goto log_extents;
  5891. }
  5892. }
  5893. if (ret)
  5894. goto out_unlock;
  5895. /*
  5896. * If we are logging a directory in full mode, collect the delayed items
  5897. * before iterating the subvolume tree, so that we don't miss any new
  5898. * dir index items in case they get flushed while or right after we are
  5899. * iterating the subvolume tree.
  5900. */
  5901. if (full_dir_logging && !ctx->logging_new_delayed_dentries)
  5902. btrfs_log_get_delayed_items(inode, &delayed_ins_list,
  5903. &delayed_del_list);
  5904. ret = copy_inode_items_to_log(trans, inode, &min_key, &max_key,
  5905. path, dst_path, logged_isize,
  5906. inode_only, ctx,
  5907. &need_log_inode_item);
  5908. if (ret)
  5909. goto out_unlock;
  5910. btrfs_release_path(path);
  5911. btrfs_release_path(dst_path);
  5912. ret = btrfs_log_all_xattrs(trans, inode, path, dst_path, ctx);
  5913. if (ret)
  5914. goto out_unlock;
  5915. xattrs_logged = true;
  5916. if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
  5917. btrfs_release_path(path);
  5918. btrfs_release_path(dst_path);
  5919. ret = btrfs_log_holes(trans, inode, path);
  5920. if (ret)
  5921. goto out_unlock;
  5922. }
  5923. log_extents:
  5924. btrfs_release_path(path);
  5925. btrfs_release_path(dst_path);
  5926. if (need_log_inode_item) {
  5927. ret = log_inode_item(trans, log, dst_path, inode, inode_item_dropped);
  5928. if (ret)
  5929. goto out_unlock;
  5930. /*
  5931. * If we are doing a fast fsync and the inode was logged before
  5932. * in this transaction, we don't need to log the xattrs because
  5933. * they were logged before. If xattrs were added, changed or
  5934. * deleted since the last time we logged the inode, then we have
  5935. * already logged them because the inode had the runtime flag
  5936. * BTRFS_INODE_COPY_EVERYTHING set.
  5937. */
  5938. if (!xattrs_logged && inode->logged_trans < trans->transid) {
  5939. ret = btrfs_log_all_xattrs(trans, inode, path, dst_path, ctx);
  5940. if (ret)
  5941. goto out_unlock;
  5942. btrfs_release_path(path);
  5943. }
  5944. }
  5945. if (fast_search) {
  5946. ret = btrfs_log_changed_extents(trans, inode, dst_path, ctx);
  5947. if (ret)
  5948. goto out_unlock;
  5949. } else if (inode_only == LOG_INODE_ALL) {
  5950. struct extent_map *em, *n;
  5951. write_lock(&em_tree->lock);
  5952. list_for_each_entry_safe(em, n, &em_tree->modified_extents, list)
  5953. list_del_init(&em->list);
  5954. write_unlock(&em_tree->lock);
  5955. }
  5956. if (full_dir_logging) {
  5957. ret = log_directory_changes(trans, inode, path, dst_path, ctx);
  5958. if (ret)
  5959. goto out_unlock;
  5960. ret = log_delayed_insertion_items(trans, inode, path,
  5961. &delayed_ins_list, ctx);
  5962. if (ret)
  5963. goto out_unlock;
  5964. ret = log_delayed_deletion_items(trans, inode, path,
  5965. &delayed_del_list, ctx);
  5966. if (ret)
  5967. goto out_unlock;
  5968. }
  5969. spin_lock(&inode->lock);
  5970. inode->logged_trans = trans->transid;
  5971. /*
  5972. * Don't update last_log_commit if we logged that an inode exists.
  5973. * We do this for three reasons:
  5974. *
  5975. * 1) We might have had buffered writes to this inode that were
  5976. * flushed and had their ordered extents completed in this
  5977. * transaction, but we did not previously log the inode with
  5978. * LOG_INODE_ALL. Later the inode was evicted and after that
  5979. * it was loaded again and this LOG_INODE_EXISTS log operation
  5980. * happened. We must make sure that if an explicit fsync against
  5981. * the inode is performed later, it logs the new extents, an
  5982. * updated inode item, etc, and syncs the log. The same logic
  5983. * applies to direct IO writes instead of buffered writes.
  5984. *
  5985. * 2) When we log the inode with LOG_INODE_EXISTS, its inode item
  5986. * is logged with an i_size of 0 or whatever value was logged
  5987. * before. If later the i_size of the inode is increased by a
  5988. * truncate operation, the log is synced through an fsync of
  5989. * some other inode and then finally an explicit fsync against
  5990. * this inode is made, we must make sure this fsync logs the
  5991. * inode with the new i_size, the hole between old i_size and
  5992. * the new i_size, and syncs the log.
  5993. *
  5994. * 3) If we are logging that an ancestor inode exists as part of
  5995. * logging a new name from a link or rename operation, don't update
  5996. * its last_log_commit - otherwise if an explicit fsync is made
  5997. * against an ancestor, the fsync considers the inode in the log
  5998. * and doesn't sync the log, resulting in the ancestor missing after
  5999. * a power failure unless the log was synced as part of an fsync
  6000. * against any other unrelated inode.
  6001. */
  6002. if (inode_only != LOG_INODE_EXISTS)
  6003. inode->last_log_commit = inode->last_sub_trans;
  6004. spin_unlock(&inode->lock);
  6005. /*
  6006. * Reset the last_reflink_trans so that the next fsync does not need to
  6007. * go through the slower path when logging extents and their checksums.
  6008. */
  6009. if (inode_only == LOG_INODE_ALL)
  6010. inode->last_reflink_trans = 0;
  6011. out_unlock:
  6012. mutex_unlock(&inode->log_mutex);
  6013. out:
  6014. btrfs_free_path(path);
  6015. btrfs_free_path(dst_path);
  6016. if (ret)
  6017. free_conflicting_inodes(ctx);
  6018. else
  6019. ret = log_conflicting_inodes(trans, inode->root, ctx);
  6020. if (full_dir_logging && !ctx->logging_new_delayed_dentries) {
  6021. if (!ret)
  6022. ret = log_new_delayed_dentries(trans, inode,
  6023. &delayed_ins_list, ctx);
  6024. btrfs_log_put_delayed_items(inode, &delayed_ins_list,
  6025. &delayed_del_list);
  6026. }
  6027. return ret;
  6028. }
  6029. static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
  6030. struct btrfs_inode *inode,
  6031. struct btrfs_log_ctx *ctx)
  6032. {
  6033. int ret;
  6034. struct btrfs_path *path;
  6035. struct btrfs_key key;
  6036. struct btrfs_root *root = inode->root;
  6037. const u64 ino = btrfs_ino(inode);
  6038. path = btrfs_alloc_path();
  6039. if (!path)
  6040. return -ENOMEM;
  6041. path->skip_locking = 1;
  6042. path->search_commit_root = 1;
  6043. key.objectid = ino;
  6044. key.type = BTRFS_INODE_REF_KEY;
  6045. key.offset = 0;
  6046. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  6047. if (ret < 0)
  6048. goto out;
  6049. while (true) {
  6050. struct extent_buffer *leaf = path->nodes[0];
  6051. int slot = path->slots[0];
  6052. u32 cur_offset = 0;
  6053. u32 item_size;
  6054. unsigned long ptr;
  6055. if (slot >= btrfs_header_nritems(leaf)) {
  6056. ret = btrfs_next_leaf(root, path);
  6057. if (ret < 0)
  6058. goto out;
  6059. else if (ret > 0)
  6060. break;
  6061. continue;
  6062. }
  6063. btrfs_item_key_to_cpu(leaf, &key, slot);
  6064. /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
  6065. if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
  6066. break;
  6067. item_size = btrfs_item_size(leaf, slot);
  6068. ptr = btrfs_item_ptr_offset(leaf, slot);
  6069. while (cur_offset < item_size) {
  6070. struct btrfs_key inode_key;
  6071. struct btrfs_inode *dir_inode;
  6072. inode_key.type = BTRFS_INODE_ITEM_KEY;
  6073. inode_key.offset = 0;
  6074. if (key.type == BTRFS_INODE_EXTREF_KEY) {
  6075. struct btrfs_inode_extref *extref;
  6076. extref = (struct btrfs_inode_extref *)
  6077. (ptr + cur_offset);
  6078. inode_key.objectid = btrfs_inode_extref_parent(
  6079. leaf, extref);
  6080. cur_offset += sizeof(*extref);
  6081. cur_offset += btrfs_inode_extref_name_len(leaf,
  6082. extref);
  6083. } else {
  6084. inode_key.objectid = key.offset;
  6085. cur_offset = item_size;
  6086. }
  6087. dir_inode = btrfs_iget_logging(inode_key.objectid, root);
  6088. /*
  6089. * If the parent inode was deleted, return an error to
  6090. * fallback to a transaction commit. This is to prevent
  6091. * getting an inode that was moved from one parent A to
  6092. * a parent B, got its former parent A deleted and then
  6093. * it got fsync'ed, from existing at both parents after
  6094. * a log replay (and the old parent still existing).
  6095. * Example:
  6096. *
  6097. * mkdir /mnt/A
  6098. * mkdir /mnt/B
  6099. * touch /mnt/B/bar
  6100. * sync
  6101. * mv /mnt/B/bar /mnt/A/bar
  6102. * mv -T /mnt/A /mnt/B
  6103. * fsync /mnt/B/bar
  6104. * <power fail>
  6105. *
  6106. * If we ignore the old parent B which got deleted,
  6107. * after a log replay we would have file bar linked
  6108. * at both parents and the old parent B would still
  6109. * exist.
  6110. */
  6111. if (IS_ERR(dir_inode)) {
  6112. ret = PTR_ERR(dir_inode);
  6113. goto out;
  6114. }
  6115. if (!need_log_inode(trans, dir_inode)) {
  6116. btrfs_add_delayed_iput(dir_inode);
  6117. continue;
  6118. }
  6119. ctx->log_new_dentries = false;
  6120. ret = btrfs_log_inode(trans, dir_inode, LOG_INODE_ALL, ctx);
  6121. if (!ret && ctx->log_new_dentries)
  6122. ret = log_new_dir_dentries(trans, dir_inode, ctx);
  6123. btrfs_add_delayed_iput(dir_inode);
  6124. if (ret)
  6125. goto out;
  6126. }
  6127. path->slots[0]++;
  6128. }
  6129. ret = 0;
  6130. out:
  6131. btrfs_free_path(path);
  6132. return ret;
  6133. }
  6134. static int log_new_ancestors(struct btrfs_trans_handle *trans,
  6135. struct btrfs_root *root,
  6136. struct btrfs_path *path,
  6137. struct btrfs_log_ctx *ctx)
  6138. {
  6139. struct btrfs_key found_key;
  6140. btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
  6141. while (true) {
  6142. struct extent_buffer *leaf;
  6143. int slot;
  6144. struct btrfs_key search_key;
  6145. struct btrfs_inode *inode;
  6146. u64 ino;
  6147. int ret = 0;
  6148. btrfs_release_path(path);
  6149. ino = found_key.offset;
  6150. search_key.objectid = found_key.offset;
  6151. search_key.type = BTRFS_INODE_ITEM_KEY;
  6152. search_key.offset = 0;
  6153. inode = btrfs_iget_logging(ino, root);
  6154. if (IS_ERR(inode))
  6155. return PTR_ERR(inode);
  6156. if (inode->generation >= trans->transid &&
  6157. need_log_inode(trans, inode))
  6158. ret = btrfs_log_inode(trans, inode, LOG_INODE_EXISTS, ctx);
  6159. btrfs_add_delayed_iput(inode);
  6160. if (ret)
  6161. return ret;
  6162. if (search_key.objectid == BTRFS_FIRST_FREE_OBJECTID)
  6163. break;
  6164. search_key.type = BTRFS_INODE_REF_KEY;
  6165. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  6166. if (ret < 0)
  6167. return ret;
  6168. leaf = path->nodes[0];
  6169. slot = path->slots[0];
  6170. if (slot >= btrfs_header_nritems(leaf)) {
  6171. ret = btrfs_next_leaf(root, path);
  6172. if (ret < 0)
  6173. return ret;
  6174. else if (ret > 0)
  6175. return -ENOENT;
  6176. leaf = path->nodes[0];
  6177. slot = path->slots[0];
  6178. }
  6179. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  6180. if (found_key.objectid != search_key.objectid ||
  6181. found_key.type != BTRFS_INODE_REF_KEY)
  6182. return -ENOENT;
  6183. }
  6184. return 0;
  6185. }
  6186. static int log_new_ancestors_fast(struct btrfs_trans_handle *trans,
  6187. struct btrfs_inode *inode,
  6188. struct dentry *parent,
  6189. struct btrfs_log_ctx *ctx)
  6190. {
  6191. struct btrfs_root *root = inode->root;
  6192. struct dentry *old_parent = NULL;
  6193. struct super_block *sb = inode->vfs_inode.i_sb;
  6194. int ret = 0;
  6195. while (true) {
  6196. if (!parent || d_really_is_negative(parent) ||
  6197. sb != parent->d_sb)
  6198. break;
  6199. inode = BTRFS_I(d_inode(parent));
  6200. if (root != inode->root)
  6201. break;
  6202. if (inode->generation >= trans->transid &&
  6203. need_log_inode(trans, inode)) {
  6204. ret = btrfs_log_inode(trans, inode,
  6205. LOG_INODE_EXISTS, ctx);
  6206. if (ret)
  6207. break;
  6208. }
  6209. if (IS_ROOT(parent))
  6210. break;
  6211. parent = dget_parent(parent);
  6212. dput(old_parent);
  6213. old_parent = parent;
  6214. }
  6215. dput(old_parent);
  6216. return ret;
  6217. }
  6218. static int log_all_new_ancestors(struct btrfs_trans_handle *trans,
  6219. struct btrfs_inode *inode,
  6220. struct dentry *parent,
  6221. struct btrfs_log_ctx *ctx)
  6222. {
  6223. struct btrfs_root *root = inode->root;
  6224. const u64 ino = btrfs_ino(inode);
  6225. struct btrfs_path *path;
  6226. struct btrfs_key search_key;
  6227. int ret;
  6228. /*
  6229. * For a single hard link case, go through a fast path that does not
  6230. * need to iterate the fs/subvolume tree.
  6231. */
  6232. if (inode->vfs_inode.i_nlink < 2)
  6233. return log_new_ancestors_fast(trans, inode, parent, ctx);
  6234. path = btrfs_alloc_path();
  6235. if (!path)
  6236. return -ENOMEM;
  6237. search_key.objectid = ino;
  6238. search_key.type = BTRFS_INODE_REF_KEY;
  6239. search_key.offset = 0;
  6240. again:
  6241. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  6242. if (ret < 0)
  6243. goto out;
  6244. if (ret == 0)
  6245. path->slots[0]++;
  6246. while (true) {
  6247. struct extent_buffer *leaf = path->nodes[0];
  6248. int slot = path->slots[0];
  6249. struct btrfs_key found_key;
  6250. if (slot >= btrfs_header_nritems(leaf)) {
  6251. ret = btrfs_next_leaf(root, path);
  6252. if (ret < 0)
  6253. goto out;
  6254. else if (ret > 0)
  6255. break;
  6256. continue;
  6257. }
  6258. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  6259. if (found_key.objectid != ino ||
  6260. found_key.type > BTRFS_INODE_EXTREF_KEY)
  6261. break;
  6262. /*
  6263. * Don't deal with extended references because they are rare
  6264. * cases and too complex to deal with (we would need to keep
  6265. * track of which subitem we are processing for each item in
  6266. * this loop, etc). So just return some error to fallback to
  6267. * a transaction commit.
  6268. */
  6269. if (found_key.type == BTRFS_INODE_EXTREF_KEY) {
  6270. ret = -EMLINK;
  6271. goto out;
  6272. }
  6273. /*
  6274. * Logging ancestors needs to do more searches on the fs/subvol
  6275. * tree, so it releases the path as needed to avoid deadlocks.
  6276. * Keep track of the last inode ref key and resume from that key
  6277. * after logging all new ancestors for the current hard link.
  6278. */
  6279. memcpy(&search_key, &found_key, sizeof(search_key));
  6280. ret = log_new_ancestors(trans, root, path, ctx);
  6281. if (ret)
  6282. goto out;
  6283. btrfs_release_path(path);
  6284. goto again;
  6285. }
  6286. ret = 0;
  6287. out:
  6288. btrfs_free_path(path);
  6289. return ret;
  6290. }
  6291. /*
  6292. * helper function around btrfs_log_inode to make sure newly created
  6293. * parent directories also end up in the log. A minimal inode and backref
  6294. * only logging is done of any parent directories that are older than
  6295. * the last committed transaction
  6296. */
  6297. static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
  6298. struct btrfs_inode *inode,
  6299. struct dentry *parent,
  6300. int inode_only,
  6301. struct btrfs_log_ctx *ctx)
  6302. {
  6303. struct btrfs_root *root = inode->root;
  6304. struct btrfs_fs_info *fs_info = root->fs_info;
  6305. int ret = 0;
  6306. bool log_dentries = false;
  6307. if (btrfs_test_opt(fs_info, NOTREELOG)) {
  6308. ret = BTRFS_LOG_FORCE_COMMIT;
  6309. goto end_no_trans;
  6310. }
  6311. if (btrfs_root_refs(&root->root_item) == 0) {
  6312. ret = BTRFS_LOG_FORCE_COMMIT;
  6313. goto end_no_trans;
  6314. }
  6315. /*
  6316. * If we're logging an inode from a subvolume created in the current
  6317. * transaction we must force a commit since the root is not persisted.
  6318. */
  6319. if (btrfs_root_generation(&root->root_item) == trans->transid) {
  6320. ret = BTRFS_LOG_FORCE_COMMIT;
  6321. goto end_no_trans;
  6322. }
  6323. /*
  6324. * Skip already logged inodes or inodes corresponding to tmpfiles
  6325. * (since logging them is pointless, a link count of 0 means they
  6326. * will never be accessible).
  6327. */
  6328. if ((btrfs_inode_in_log(inode, trans->transid) &&
  6329. list_empty(&ctx->ordered_extents)) ||
  6330. inode->vfs_inode.i_nlink == 0) {
  6331. ret = BTRFS_NO_LOG_SYNC;
  6332. goto end_no_trans;
  6333. }
  6334. ret = start_log_trans(trans, root, ctx);
  6335. if (ret)
  6336. goto end_no_trans;
  6337. ret = btrfs_log_inode(trans, inode, inode_only, ctx);
  6338. if (ret)
  6339. goto end_trans;
  6340. /*
  6341. * for regular files, if its inode is already on disk, we don't
  6342. * have to worry about the parents at all. This is because
  6343. * we can use the last_unlink_trans field to record renames
  6344. * and other fun in this file.
  6345. */
  6346. if (S_ISREG(inode->vfs_inode.i_mode) &&
  6347. inode->generation < trans->transid &&
  6348. inode->last_unlink_trans < trans->transid) {
  6349. ret = 0;
  6350. goto end_trans;
  6351. }
  6352. if (S_ISDIR(inode->vfs_inode.i_mode) && ctx->log_new_dentries)
  6353. log_dentries = true;
  6354. /*
  6355. * On unlink we must make sure all our current and old parent directory
  6356. * inodes are fully logged. This is to prevent leaving dangling
  6357. * directory index entries in directories that were our parents but are
  6358. * not anymore. Not doing this results in old parent directory being
  6359. * impossible to delete after log replay (rmdir will always fail with
  6360. * error -ENOTEMPTY).
  6361. *
  6362. * Example 1:
  6363. *
  6364. * mkdir testdir
  6365. * touch testdir/foo
  6366. * ln testdir/foo testdir/bar
  6367. * sync
  6368. * unlink testdir/bar
  6369. * xfs_io -c fsync testdir/foo
  6370. * <power failure>
  6371. * mount fs, triggers log replay
  6372. *
  6373. * If we don't log the parent directory (testdir), after log replay the
  6374. * directory still has an entry pointing to the file inode using the bar
  6375. * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
  6376. * the file inode has a link count of 1.
  6377. *
  6378. * Example 2:
  6379. *
  6380. * mkdir testdir
  6381. * touch foo
  6382. * ln foo testdir/foo2
  6383. * ln foo testdir/foo3
  6384. * sync
  6385. * unlink testdir/foo3
  6386. * xfs_io -c fsync foo
  6387. * <power failure>
  6388. * mount fs, triggers log replay
  6389. *
  6390. * Similar as the first example, after log replay the parent directory
  6391. * testdir still has an entry pointing to the inode file with name foo3
  6392. * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
  6393. * and has a link count of 2.
  6394. */
  6395. if (inode->last_unlink_trans >= trans->transid) {
  6396. ret = btrfs_log_all_parents(trans, inode, ctx);
  6397. if (ret)
  6398. goto end_trans;
  6399. }
  6400. ret = log_all_new_ancestors(trans, inode, parent, ctx);
  6401. if (ret)
  6402. goto end_trans;
  6403. if (log_dentries)
  6404. ret = log_new_dir_dentries(trans, inode, ctx);
  6405. else
  6406. ret = 0;
  6407. end_trans:
  6408. if (ret < 0) {
  6409. btrfs_set_log_full_commit(trans);
  6410. ret = BTRFS_LOG_FORCE_COMMIT;
  6411. }
  6412. if (ret)
  6413. btrfs_remove_log_ctx(root, ctx);
  6414. btrfs_end_log_trans(root);
  6415. end_no_trans:
  6416. return ret;
  6417. }
  6418. /*
  6419. * it is not safe to log dentry if the chunk root has added new
  6420. * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
  6421. * If this returns 1, you must commit the transaction to safely get your
  6422. * data on disk.
  6423. */
  6424. int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
  6425. struct dentry *dentry,
  6426. struct btrfs_log_ctx *ctx)
  6427. {
  6428. struct dentry *parent = dget_parent(dentry);
  6429. int ret;
  6430. ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent,
  6431. LOG_INODE_ALL, ctx);
  6432. dput(parent);
  6433. return ret;
  6434. }
  6435. /*
  6436. * should be called during mount to recover any replay any log trees
  6437. * from the FS
  6438. */
  6439. int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
  6440. {
  6441. int ret;
  6442. struct btrfs_path *path;
  6443. struct btrfs_trans_handle *trans;
  6444. struct btrfs_key key;
  6445. struct btrfs_key found_key;
  6446. struct btrfs_root *log;
  6447. struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
  6448. struct walk_control wc = {
  6449. .process_func = process_one_buffer,
  6450. .stage = LOG_WALK_PIN_ONLY,
  6451. };
  6452. path = btrfs_alloc_path();
  6453. if (!path)
  6454. return -ENOMEM;
  6455. set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
  6456. trans = btrfs_start_transaction(fs_info->tree_root, 0);
  6457. if (IS_ERR(trans)) {
  6458. ret = PTR_ERR(trans);
  6459. goto error;
  6460. }
  6461. wc.trans = trans;
  6462. wc.pin = 1;
  6463. ret = walk_log_tree(trans, log_root_tree, &wc);
  6464. if (ret) {
  6465. btrfs_abort_transaction(trans, ret);
  6466. goto error;
  6467. }
  6468. again:
  6469. key.objectid = BTRFS_TREE_LOG_OBJECTID;
  6470. key.offset = (u64)-1;
  6471. key.type = BTRFS_ROOT_ITEM_KEY;
  6472. while (1) {
  6473. ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
  6474. if (ret < 0) {
  6475. btrfs_abort_transaction(trans, ret);
  6476. goto error;
  6477. }
  6478. if (ret > 0) {
  6479. if (path->slots[0] == 0)
  6480. break;
  6481. path->slots[0]--;
  6482. }
  6483. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  6484. path->slots[0]);
  6485. btrfs_release_path(path);
  6486. if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  6487. break;
  6488. log = btrfs_read_tree_root(log_root_tree, &found_key);
  6489. if (IS_ERR(log)) {
  6490. ret = PTR_ERR(log);
  6491. btrfs_abort_transaction(trans, ret);
  6492. goto error;
  6493. }
  6494. wc.replay_dest = btrfs_get_fs_root(fs_info, found_key.offset,
  6495. true);
  6496. if (IS_ERR(wc.replay_dest)) {
  6497. ret = PTR_ERR(wc.replay_dest);
  6498. /*
  6499. * We didn't find the subvol, likely because it was
  6500. * deleted. This is ok, simply skip this log and go to
  6501. * the next one.
  6502. *
  6503. * We need to exclude the root because we can't have
  6504. * other log replays overwriting this log as we'll read
  6505. * it back in a few more times. This will keep our
  6506. * block from being modified, and we'll just bail for
  6507. * each subsequent pass.
  6508. */
  6509. if (ret == -ENOENT)
  6510. ret = btrfs_pin_extent_for_log_replay(trans, log->node);
  6511. btrfs_put_root(log);
  6512. if (!ret)
  6513. goto next;
  6514. btrfs_abort_transaction(trans, ret);
  6515. goto error;
  6516. }
  6517. wc.replay_dest->log_root = log;
  6518. ret = btrfs_record_root_in_trans(trans, wc.replay_dest);
  6519. if (ret) {
  6520. /* The loop needs to continue due to the root refs */
  6521. btrfs_abort_transaction(trans, ret);
  6522. } else {
  6523. ret = walk_log_tree(trans, log, &wc);
  6524. if (ret)
  6525. btrfs_abort_transaction(trans, ret);
  6526. }
  6527. if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
  6528. ret = fixup_inode_link_counts(trans, wc.replay_dest,
  6529. path);
  6530. if (ret)
  6531. btrfs_abort_transaction(trans, ret);
  6532. }
  6533. if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
  6534. struct btrfs_root *root = wc.replay_dest;
  6535. btrfs_release_path(path);
  6536. /*
  6537. * We have just replayed everything, and the highest
  6538. * objectid of fs roots probably has changed in case
  6539. * some inode_item's got replayed.
  6540. *
  6541. * root->objectid_mutex is not acquired as log replay
  6542. * could only happen during mount.
  6543. */
  6544. ret = btrfs_init_root_free_objectid(root);
  6545. if (ret)
  6546. btrfs_abort_transaction(trans, ret);
  6547. }
  6548. wc.replay_dest->log_root = NULL;
  6549. btrfs_put_root(wc.replay_dest);
  6550. btrfs_put_root(log);
  6551. if (ret)
  6552. goto error;
  6553. next:
  6554. if (found_key.offset == 0)
  6555. break;
  6556. key.offset = found_key.offset - 1;
  6557. }
  6558. btrfs_release_path(path);
  6559. /* step one is to pin it all, step two is to replay just inodes */
  6560. if (wc.pin) {
  6561. wc.pin = 0;
  6562. wc.process_func = replay_one_buffer;
  6563. wc.stage = LOG_WALK_REPLAY_INODES;
  6564. goto again;
  6565. }
  6566. /* step three is to replay everything */
  6567. if (wc.stage < LOG_WALK_REPLAY_ALL) {
  6568. wc.stage++;
  6569. goto again;
  6570. }
  6571. btrfs_free_path(path);
  6572. /* step 4: commit the transaction, which also unpins the blocks */
  6573. ret = btrfs_commit_transaction(trans);
  6574. if (ret)
  6575. return ret;
  6576. log_root_tree->log_root = NULL;
  6577. clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
  6578. btrfs_put_root(log_root_tree);
  6579. return 0;
  6580. error:
  6581. if (wc.trans)
  6582. btrfs_end_transaction(wc.trans);
  6583. clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
  6584. btrfs_free_path(path);
  6585. return ret;
  6586. }
  6587. /*
  6588. * there are some corner cases where we want to force a full
  6589. * commit instead of allowing a directory to be logged.
  6590. *
  6591. * They revolve around files there were unlinked from the directory, and
  6592. * this function updates the parent directory so that a full commit is
  6593. * properly done if it is fsync'd later after the unlinks are done.
  6594. *
  6595. * Must be called before the unlink operations (updates to the subvolume tree,
  6596. * inodes, etc) are done.
  6597. */
  6598. void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
  6599. struct btrfs_inode *dir, struct btrfs_inode *inode,
  6600. bool for_rename)
  6601. {
  6602. /*
  6603. * when we're logging a file, if it hasn't been renamed
  6604. * or unlinked, and its inode is fully committed on disk,
  6605. * we don't have to worry about walking up the directory chain
  6606. * to log its parents.
  6607. *
  6608. * So, we use the last_unlink_trans field to put this transid
  6609. * into the file. When the file is logged we check it and
  6610. * don't log the parents if the file is fully on disk.
  6611. */
  6612. mutex_lock(&inode->log_mutex);
  6613. inode->last_unlink_trans = trans->transid;
  6614. mutex_unlock(&inode->log_mutex);
  6615. if (!for_rename)
  6616. return;
  6617. /*
  6618. * If this directory was already logged, any new names will be logged
  6619. * with btrfs_log_new_name() and old names will be deleted from the log
  6620. * tree with btrfs_del_dir_entries_in_log() or with
  6621. * btrfs_del_inode_ref_in_log().
  6622. */
  6623. if (inode_logged(trans, dir, NULL) == 1)
  6624. return;
  6625. /*
  6626. * If the inode we're about to unlink was logged before, the log will be
  6627. * properly updated with the new name with btrfs_log_new_name() and the
  6628. * old name removed with btrfs_del_dir_entries_in_log() or with
  6629. * btrfs_del_inode_ref_in_log().
  6630. */
  6631. if (inode_logged(trans, inode, NULL) == 1)
  6632. return;
  6633. /*
  6634. * when renaming files across directories, if the directory
  6635. * there we're unlinking from gets fsync'd later on, there's
  6636. * no way to find the destination directory later and fsync it
  6637. * properly. So, we have to be conservative and force commits
  6638. * so the new name gets discovered.
  6639. */
  6640. mutex_lock(&dir->log_mutex);
  6641. dir->last_unlink_trans = trans->transid;
  6642. mutex_unlock(&dir->log_mutex);
  6643. }
  6644. /*
  6645. * Make sure that if someone attempts to fsync the parent directory of a deleted
  6646. * snapshot, it ends up triggering a transaction commit. This is to guarantee
  6647. * that after replaying the log tree of the parent directory's root we will not
  6648. * see the snapshot anymore and at log replay time we will not see any log tree
  6649. * corresponding to the deleted snapshot's root, which could lead to replaying
  6650. * it after replaying the log tree of the parent directory (which would replay
  6651. * the snapshot delete operation).
  6652. *
  6653. * Must be called before the actual snapshot destroy operation (updates to the
  6654. * parent root and tree of tree roots trees, etc) are done.
  6655. */
  6656. void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
  6657. struct btrfs_inode *dir)
  6658. {
  6659. mutex_lock(&dir->log_mutex);
  6660. dir->last_unlink_trans = trans->transid;
  6661. mutex_unlock(&dir->log_mutex);
  6662. }
  6663. /*
  6664. * Call this when creating a subvolume in a directory.
  6665. * Because we don't commit a transaction when creating a subvolume, we can't
  6666. * allow the directory pointing to the subvolume to be logged with an entry that
  6667. * points to an unpersisted root if we are still in the transaction used to
  6668. * create the subvolume, so make any attempt to log the directory to result in a
  6669. * full log sync.
  6670. * Also we don't need to worry with renames, since btrfs_rename() marks the log
  6671. * for full commit when renaming a subvolume.
  6672. *
  6673. * Must be called before creating the subvolume entry in its parent directory.
  6674. */
  6675. void btrfs_record_new_subvolume(const struct btrfs_trans_handle *trans,
  6676. struct btrfs_inode *dir)
  6677. {
  6678. mutex_lock(&dir->log_mutex);
  6679. dir->last_unlink_trans = trans->transid;
  6680. mutex_unlock(&dir->log_mutex);
  6681. }
  6682. /*
  6683. * Update the log after adding a new name for an inode.
  6684. *
  6685. * @trans: Transaction handle.
  6686. * @old_dentry: The dentry associated with the old name and the old
  6687. * parent directory.
  6688. * @old_dir: The inode of the previous parent directory for the case
  6689. * of a rename. For a link operation, it must be NULL.
  6690. * @old_dir_index: The index number associated with the old name, meaningful
  6691. * only for rename operations (when @old_dir is not NULL).
  6692. * Ignored for link operations.
  6693. * @parent: The dentry associated with the directory under which the
  6694. * new name is located.
  6695. *
  6696. * Call this after adding a new name for an inode, as a result of a link or
  6697. * rename operation, and it will properly update the log to reflect the new name.
  6698. */
  6699. void btrfs_log_new_name(struct btrfs_trans_handle *trans,
  6700. struct dentry *old_dentry, struct btrfs_inode *old_dir,
  6701. u64 old_dir_index, struct dentry *parent)
  6702. {
  6703. struct btrfs_inode *inode = BTRFS_I(d_inode(old_dentry));
  6704. struct btrfs_root *root = inode->root;
  6705. struct btrfs_log_ctx ctx;
  6706. bool log_pinned = false;
  6707. int ret;
  6708. /*
  6709. * this will force the logging code to walk the dentry chain
  6710. * up for the file
  6711. */
  6712. if (!S_ISDIR(inode->vfs_inode.i_mode))
  6713. inode->last_unlink_trans = trans->transid;
  6714. /*
  6715. * if this inode hasn't been logged and directory we're renaming it
  6716. * from hasn't been logged, we don't need to log it
  6717. */
  6718. ret = inode_logged(trans, inode, NULL);
  6719. if (ret < 0) {
  6720. goto out;
  6721. } else if (ret == 0) {
  6722. if (!old_dir)
  6723. return;
  6724. /*
  6725. * If the inode was not logged and we are doing a rename (old_dir is not
  6726. * NULL), check if old_dir was logged - if it was not we can return and
  6727. * do nothing.
  6728. */
  6729. ret = inode_logged(trans, old_dir, NULL);
  6730. if (ret < 0)
  6731. goto out;
  6732. else if (ret == 0)
  6733. return;
  6734. }
  6735. ret = 0;
  6736. /*
  6737. * If we are doing a rename (old_dir is not NULL) from a directory that
  6738. * was previously logged, make sure that on log replay we get the old
  6739. * dir entry deleted. This is needed because we will also log the new
  6740. * name of the renamed inode, so we need to make sure that after log
  6741. * replay we don't end up with both the new and old dir entries existing.
  6742. */
  6743. if (old_dir && old_dir->logged_trans == trans->transid) {
  6744. struct btrfs_root *log = old_dir->root->log_root;
  6745. struct btrfs_path *path;
  6746. struct fscrypt_name fname;
  6747. ASSERT(old_dir_index >= BTRFS_DIR_START_INDEX);
  6748. ret = fscrypt_setup_filename(&old_dir->vfs_inode,
  6749. &old_dentry->d_name, 0, &fname);
  6750. if (ret)
  6751. goto out;
  6752. /*
  6753. * We have two inodes to update in the log, the old directory and
  6754. * the inode that got renamed, so we must pin the log to prevent
  6755. * anyone from syncing the log until we have updated both inodes
  6756. * in the log.
  6757. */
  6758. ret = join_running_log_trans(root);
  6759. /*
  6760. * At least one of the inodes was logged before, so this should
  6761. * not fail, but if it does, it's not serious, just bail out and
  6762. * mark the log for a full commit.
  6763. */
  6764. if (WARN_ON_ONCE(ret < 0)) {
  6765. fscrypt_free_filename(&fname);
  6766. goto out;
  6767. }
  6768. log_pinned = true;
  6769. path = btrfs_alloc_path();
  6770. if (!path) {
  6771. ret = -ENOMEM;
  6772. fscrypt_free_filename(&fname);
  6773. goto out;
  6774. }
  6775. /*
  6776. * Other concurrent task might be logging the old directory,
  6777. * as it can be triggered when logging other inode that had or
  6778. * still has a dentry in the old directory. We lock the old
  6779. * directory's log_mutex to ensure the deletion of the old
  6780. * name is persisted, because during directory logging we
  6781. * delete all BTRFS_DIR_LOG_INDEX_KEY keys and the deletion of
  6782. * the old name's dir index item is in the delayed items, so
  6783. * it could be missed by an in progress directory logging.
  6784. */
  6785. mutex_lock(&old_dir->log_mutex);
  6786. ret = del_logged_dentry(trans, log, path, btrfs_ino(old_dir),
  6787. &fname.disk_name, old_dir_index);
  6788. if (ret > 0) {
  6789. /*
  6790. * The dentry does not exist in the log, so record its
  6791. * deletion.
  6792. */
  6793. btrfs_release_path(path);
  6794. ret = insert_dir_log_key(trans, log, path,
  6795. btrfs_ino(old_dir),
  6796. old_dir_index, old_dir_index);
  6797. }
  6798. mutex_unlock(&old_dir->log_mutex);
  6799. btrfs_free_path(path);
  6800. fscrypt_free_filename(&fname);
  6801. if (ret < 0)
  6802. goto out;
  6803. }
  6804. btrfs_init_log_ctx(&ctx, inode);
  6805. ctx.logging_new_name = true;
  6806. btrfs_init_log_ctx_scratch_eb(&ctx);
  6807. /*
  6808. * We don't care about the return value. If we fail to log the new name
  6809. * then we know the next attempt to sync the log will fallback to a full
  6810. * transaction commit (due to a call to btrfs_set_log_full_commit()), so
  6811. * we don't need to worry about getting a log committed that has an
  6812. * inconsistent state after a rename operation.
  6813. */
  6814. btrfs_log_inode_parent(trans, inode, parent, LOG_INODE_EXISTS, &ctx);
  6815. free_extent_buffer(ctx.scratch_eb);
  6816. ASSERT(list_empty(&ctx.conflict_inodes));
  6817. out:
  6818. /*
  6819. * If an error happened mark the log for a full commit because it's not
  6820. * consistent and up to date or we couldn't find out if one of the
  6821. * inodes was logged before in this transaction. Do it before unpinning
  6822. * the log, to avoid any races with someone else trying to commit it.
  6823. */
  6824. if (ret < 0)
  6825. btrfs_set_log_full_commit(trans);
  6826. if (log_pinned)
  6827. btrfs_end_log_trans(root);
  6828. }