tree-log.c 156 KB

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