tree-log.c 156 KB

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