tree-log.c 159 KB

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