tree-log.c 155 KB

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