tree-log.c 155 KB

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