tree-log.c 154 KB

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