tree-log.c 155 KB

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