tree-log.c 156 KB

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