tree-log.c 156 KB

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