tree-log.c 159 KB

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