tree-log.c 164 KB

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