tree-log.c 154 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811
  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. struct btrfs_log_ctx *safe;
  2420. list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
  2421. list_del_init(&ctx->list);
  2422. ctx->log_ret = error;
  2423. }
  2424. INIT_LIST_HEAD(&root->log_ctxs[index]);
  2425. }
  2426. /*
  2427. * btrfs_sync_log does sends a given tree log down to the disk and
  2428. * updates the super blocks to record it. When this call is done,
  2429. * you know that any inodes previously logged are safely on disk only
  2430. * if it returns 0.
  2431. *
  2432. * Any other return value means you need to call btrfs_commit_transaction.
  2433. * Some of the edge cases for fsyncing directories that have had unlinks
  2434. * or renames done in the past mean that sometimes the only safe
  2435. * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
  2436. * that has happened.
  2437. */
  2438. int btrfs_sync_log(struct btrfs_trans_handle *trans,
  2439. struct btrfs_root *root, struct btrfs_log_ctx *ctx)
  2440. {
  2441. int index1;
  2442. int index2;
  2443. int mark;
  2444. int ret;
  2445. struct btrfs_root *log = root->log_root;
  2446. struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
  2447. int log_transid = 0;
  2448. struct btrfs_log_ctx root_log_ctx;
  2449. struct blk_plug plug;
  2450. mutex_lock(&root->log_mutex);
  2451. log_transid = ctx->log_transid;
  2452. if (root->log_transid_committed >= log_transid) {
  2453. mutex_unlock(&root->log_mutex);
  2454. return ctx->log_ret;
  2455. }
  2456. index1 = log_transid % 2;
  2457. if (atomic_read(&root->log_commit[index1])) {
  2458. wait_log_commit(root, log_transid);
  2459. mutex_unlock(&root->log_mutex);
  2460. return ctx->log_ret;
  2461. }
  2462. ASSERT(log_transid == root->log_transid);
  2463. atomic_set(&root->log_commit[index1], 1);
  2464. /* wait for previous tree log sync to complete */
  2465. if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
  2466. wait_log_commit(root, log_transid - 1);
  2467. while (1) {
  2468. int batch = atomic_read(&root->log_batch);
  2469. /* when we're on an ssd, just kick the log commit out */
  2470. if (!btrfs_test_opt(root->fs_info, SSD) &&
  2471. test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
  2472. mutex_unlock(&root->log_mutex);
  2473. schedule_timeout_uninterruptible(1);
  2474. mutex_lock(&root->log_mutex);
  2475. }
  2476. wait_for_writer(root);
  2477. if (batch == atomic_read(&root->log_batch))
  2478. break;
  2479. }
  2480. /* bail out if we need to do a full commit */
  2481. if (btrfs_need_log_full_commit(root->fs_info, trans)) {
  2482. ret = -EAGAIN;
  2483. btrfs_free_logged_extents(log, log_transid);
  2484. mutex_unlock(&root->log_mutex);
  2485. goto out;
  2486. }
  2487. if (log_transid % 2 == 0)
  2488. mark = EXTENT_DIRTY;
  2489. else
  2490. mark = EXTENT_NEW;
  2491. /* we start IO on all the marked extents here, but we don't actually
  2492. * wait for them until later.
  2493. */
  2494. blk_start_plug(&plug);
  2495. ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
  2496. if (ret) {
  2497. blk_finish_plug(&plug);
  2498. btrfs_abort_transaction(trans, ret);
  2499. btrfs_free_logged_extents(log, log_transid);
  2500. btrfs_set_log_full_commit(root->fs_info, trans);
  2501. mutex_unlock(&root->log_mutex);
  2502. goto out;
  2503. }
  2504. btrfs_set_root_node(&log->root_item, log->node);
  2505. root->log_transid++;
  2506. log->log_transid = root->log_transid;
  2507. root->log_start_pid = 0;
  2508. /*
  2509. * IO has been started, blocks of the log tree have WRITTEN flag set
  2510. * in their headers. new modifications of the log will be written to
  2511. * new positions. so it's safe to allow log writers to go in.
  2512. */
  2513. mutex_unlock(&root->log_mutex);
  2514. btrfs_init_log_ctx(&root_log_ctx, NULL);
  2515. mutex_lock(&log_root_tree->log_mutex);
  2516. atomic_inc(&log_root_tree->log_batch);
  2517. atomic_inc(&log_root_tree->log_writers);
  2518. index2 = log_root_tree->log_transid % 2;
  2519. list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
  2520. root_log_ctx.log_transid = log_root_tree->log_transid;
  2521. mutex_unlock(&log_root_tree->log_mutex);
  2522. ret = update_log_root(trans, log);
  2523. mutex_lock(&log_root_tree->log_mutex);
  2524. if (atomic_dec_and_test(&log_root_tree->log_writers)) {
  2525. /*
  2526. * Implicit memory barrier after atomic_dec_and_test
  2527. */
  2528. if (waitqueue_active(&log_root_tree->log_writer_wait))
  2529. wake_up(&log_root_tree->log_writer_wait);
  2530. }
  2531. if (ret) {
  2532. if (!list_empty(&root_log_ctx.list))
  2533. list_del_init(&root_log_ctx.list);
  2534. blk_finish_plug(&plug);
  2535. btrfs_set_log_full_commit(root->fs_info, trans);
  2536. if (ret != -ENOSPC) {
  2537. btrfs_abort_transaction(trans, ret);
  2538. mutex_unlock(&log_root_tree->log_mutex);
  2539. goto out;
  2540. }
  2541. btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
  2542. btrfs_free_logged_extents(log, log_transid);
  2543. mutex_unlock(&log_root_tree->log_mutex);
  2544. ret = -EAGAIN;
  2545. goto out;
  2546. }
  2547. if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
  2548. blk_finish_plug(&plug);
  2549. list_del_init(&root_log_ctx.list);
  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. mutex_lock(&log_root_tree->log_mutex);
  2634. btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
  2635. log_root_tree->log_transid_committed++;
  2636. atomic_set(&log_root_tree->log_commit[index2], 0);
  2637. mutex_unlock(&log_root_tree->log_mutex);
  2638. /*
  2639. * The barrier before waitqueue_active is implied by mutex_unlock
  2640. */
  2641. if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
  2642. wake_up(&log_root_tree->log_commit_wait[index2]);
  2643. out:
  2644. mutex_lock(&root->log_mutex);
  2645. btrfs_remove_all_log_ctxs(root, index1, ret);
  2646. root->log_transid_committed++;
  2647. atomic_set(&root->log_commit[index1], 0);
  2648. mutex_unlock(&root->log_mutex);
  2649. /*
  2650. * The barrier before waitqueue_active is implied by mutex_unlock
  2651. */
  2652. if (waitqueue_active(&root->log_commit_wait[index1]))
  2653. wake_up(&root->log_commit_wait[index1]);
  2654. return ret;
  2655. }
  2656. static void free_log_tree(struct btrfs_trans_handle *trans,
  2657. struct btrfs_root *log)
  2658. {
  2659. int ret;
  2660. u64 start;
  2661. u64 end;
  2662. struct walk_control wc = {
  2663. .free = 1,
  2664. .process_func = process_one_buffer
  2665. };
  2666. ret = walk_log_tree(trans, log, &wc);
  2667. /* I don't think this can happen but just in case */
  2668. if (ret)
  2669. btrfs_abort_transaction(trans, ret);
  2670. while (1) {
  2671. ret = find_first_extent_bit(&log->dirty_log_pages,
  2672. 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW,
  2673. NULL);
  2674. if (ret)
  2675. break;
  2676. clear_extent_bits(&log->dirty_log_pages, start, end,
  2677. EXTENT_DIRTY | EXTENT_NEW);
  2678. }
  2679. /*
  2680. * We may have short-circuited the log tree with the full commit logic
  2681. * and left ordered extents on our list, so clear these out to keep us
  2682. * from leaking inodes and memory.
  2683. */
  2684. btrfs_free_logged_extents(log, 0);
  2685. btrfs_free_logged_extents(log, 1);
  2686. free_extent_buffer(log->node);
  2687. kfree(log);
  2688. }
  2689. /*
  2690. * free all the extents used by the tree log. This should be called
  2691. * at commit time of the full transaction
  2692. */
  2693. int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
  2694. {
  2695. if (root->log_root) {
  2696. free_log_tree(trans, root->log_root);
  2697. root->log_root = NULL;
  2698. }
  2699. return 0;
  2700. }
  2701. int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
  2702. struct btrfs_fs_info *fs_info)
  2703. {
  2704. if (fs_info->log_root_tree) {
  2705. free_log_tree(trans, fs_info->log_root_tree);
  2706. fs_info->log_root_tree = NULL;
  2707. }
  2708. return 0;
  2709. }
  2710. /*
  2711. * If both a file and directory are logged, and unlinks or renames are
  2712. * mixed in, we have a few interesting corners:
  2713. *
  2714. * create file X in dir Y
  2715. * link file X to X.link in dir Y
  2716. * fsync file X
  2717. * unlink file X but leave X.link
  2718. * fsync dir Y
  2719. *
  2720. * After a crash we would expect only X.link to exist. But file X
  2721. * didn't get fsync'd again so the log has back refs for X and X.link.
  2722. *
  2723. * We solve this by removing directory entries and inode backrefs from the
  2724. * log when a file that was logged in the current transaction is
  2725. * unlinked. Any later fsync will include the updated log entries, and
  2726. * we'll be able to reconstruct the proper directory items from backrefs.
  2727. *
  2728. * This optimizations allows us to avoid relogging the entire inode
  2729. * or the entire directory.
  2730. */
  2731. int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
  2732. struct btrfs_root *root,
  2733. const char *name, int name_len,
  2734. struct inode *dir, u64 index)
  2735. {
  2736. struct btrfs_root *log;
  2737. struct btrfs_dir_item *di;
  2738. struct btrfs_path *path;
  2739. int ret;
  2740. int err = 0;
  2741. int bytes_del = 0;
  2742. u64 dir_ino = btrfs_ino(dir);
  2743. if (BTRFS_I(dir)->logged_trans < trans->transid)
  2744. return 0;
  2745. ret = join_running_log_trans(root);
  2746. if (ret)
  2747. return 0;
  2748. mutex_lock(&BTRFS_I(dir)->log_mutex);
  2749. log = root->log_root;
  2750. path = btrfs_alloc_path();
  2751. if (!path) {
  2752. err = -ENOMEM;
  2753. goto out_unlock;
  2754. }
  2755. di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
  2756. name, name_len, -1);
  2757. if (IS_ERR(di)) {
  2758. err = PTR_ERR(di);
  2759. goto fail;
  2760. }
  2761. if (di) {
  2762. ret = btrfs_delete_one_dir_name(trans, log, path, di);
  2763. bytes_del += name_len;
  2764. if (ret) {
  2765. err = ret;
  2766. goto fail;
  2767. }
  2768. }
  2769. btrfs_release_path(path);
  2770. di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
  2771. index, name, name_len, -1);
  2772. if (IS_ERR(di)) {
  2773. err = PTR_ERR(di);
  2774. goto fail;
  2775. }
  2776. if (di) {
  2777. ret = btrfs_delete_one_dir_name(trans, log, path, di);
  2778. bytes_del += name_len;
  2779. if (ret) {
  2780. err = ret;
  2781. goto fail;
  2782. }
  2783. }
  2784. /* update the directory size in the log to reflect the names
  2785. * we have removed
  2786. */
  2787. if (bytes_del) {
  2788. struct btrfs_key key;
  2789. key.objectid = dir_ino;
  2790. key.offset = 0;
  2791. key.type = BTRFS_INODE_ITEM_KEY;
  2792. btrfs_release_path(path);
  2793. ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
  2794. if (ret < 0) {
  2795. err = ret;
  2796. goto fail;
  2797. }
  2798. if (ret == 0) {
  2799. struct btrfs_inode_item *item;
  2800. u64 i_size;
  2801. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2802. struct btrfs_inode_item);
  2803. i_size = btrfs_inode_size(path->nodes[0], item);
  2804. if (i_size > bytes_del)
  2805. i_size -= bytes_del;
  2806. else
  2807. i_size = 0;
  2808. btrfs_set_inode_size(path->nodes[0], item, i_size);
  2809. btrfs_mark_buffer_dirty(path->nodes[0]);
  2810. } else
  2811. ret = 0;
  2812. btrfs_release_path(path);
  2813. }
  2814. fail:
  2815. btrfs_free_path(path);
  2816. out_unlock:
  2817. mutex_unlock(&BTRFS_I(dir)->log_mutex);
  2818. if (ret == -ENOSPC) {
  2819. btrfs_set_log_full_commit(root->fs_info, trans);
  2820. ret = 0;
  2821. } else if (ret < 0)
  2822. btrfs_abort_transaction(trans, ret);
  2823. btrfs_end_log_trans(root);
  2824. return err;
  2825. }
  2826. /* see comments for btrfs_del_dir_entries_in_log */
  2827. int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
  2828. struct btrfs_root *root,
  2829. const char *name, int name_len,
  2830. struct inode *inode, u64 dirid)
  2831. {
  2832. struct btrfs_root *log;
  2833. u64 index;
  2834. int ret;
  2835. if (BTRFS_I(inode)->logged_trans < trans->transid)
  2836. return 0;
  2837. ret = join_running_log_trans(root);
  2838. if (ret)
  2839. return 0;
  2840. log = root->log_root;
  2841. mutex_lock(&BTRFS_I(inode)->log_mutex);
  2842. ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
  2843. dirid, &index);
  2844. mutex_unlock(&BTRFS_I(inode)->log_mutex);
  2845. if (ret == -ENOSPC) {
  2846. btrfs_set_log_full_commit(root->fs_info, trans);
  2847. ret = 0;
  2848. } else if (ret < 0 && ret != -ENOENT)
  2849. btrfs_abort_transaction(trans, ret);
  2850. btrfs_end_log_trans(root);
  2851. return ret;
  2852. }
  2853. /*
  2854. * creates a range item in the log for 'dirid'. first_offset and
  2855. * last_offset tell us which parts of the key space the log should
  2856. * be considered authoritative for.
  2857. */
  2858. static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
  2859. struct btrfs_root *log,
  2860. struct btrfs_path *path,
  2861. int key_type, u64 dirid,
  2862. u64 first_offset, u64 last_offset)
  2863. {
  2864. int ret;
  2865. struct btrfs_key key;
  2866. struct btrfs_dir_log_item *item;
  2867. key.objectid = dirid;
  2868. key.offset = first_offset;
  2869. if (key_type == BTRFS_DIR_ITEM_KEY)
  2870. key.type = BTRFS_DIR_LOG_ITEM_KEY;
  2871. else
  2872. key.type = BTRFS_DIR_LOG_INDEX_KEY;
  2873. ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
  2874. if (ret)
  2875. return ret;
  2876. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2877. struct btrfs_dir_log_item);
  2878. btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
  2879. btrfs_mark_buffer_dirty(path->nodes[0]);
  2880. btrfs_release_path(path);
  2881. return 0;
  2882. }
  2883. /*
  2884. * log all the items included in the current transaction for a given
  2885. * directory. This also creates the range items in the log tree required
  2886. * to replay anything deleted before the fsync
  2887. */
  2888. static noinline int log_dir_items(struct btrfs_trans_handle *trans,
  2889. struct btrfs_root *root, struct inode *inode,
  2890. struct btrfs_path *path,
  2891. struct btrfs_path *dst_path, int key_type,
  2892. struct btrfs_log_ctx *ctx,
  2893. u64 min_offset, u64 *last_offset_ret)
  2894. {
  2895. struct btrfs_key min_key;
  2896. struct btrfs_root *log = root->log_root;
  2897. struct extent_buffer *src;
  2898. int err = 0;
  2899. int ret;
  2900. int i;
  2901. int nritems;
  2902. u64 first_offset = min_offset;
  2903. u64 last_offset = (u64)-1;
  2904. u64 ino = btrfs_ino(inode);
  2905. log = root->log_root;
  2906. min_key.objectid = ino;
  2907. min_key.type = key_type;
  2908. min_key.offset = min_offset;
  2909. ret = btrfs_search_forward(root, &min_key, path, trans->transid);
  2910. /*
  2911. * we didn't find anything from this transaction, see if there
  2912. * is anything at all
  2913. */
  2914. if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
  2915. min_key.objectid = ino;
  2916. min_key.type = key_type;
  2917. min_key.offset = (u64)-1;
  2918. btrfs_release_path(path);
  2919. ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
  2920. if (ret < 0) {
  2921. btrfs_release_path(path);
  2922. return ret;
  2923. }
  2924. ret = btrfs_previous_item(root, path, ino, key_type);
  2925. /* if ret == 0 there are items for this type,
  2926. * create a range to tell us the last key of this type.
  2927. * otherwise, there are no items in this directory after
  2928. * *min_offset, and we create a range to indicate that.
  2929. */
  2930. if (ret == 0) {
  2931. struct btrfs_key tmp;
  2932. btrfs_item_key_to_cpu(path->nodes[0], &tmp,
  2933. path->slots[0]);
  2934. if (key_type == tmp.type)
  2935. first_offset = max(min_offset, tmp.offset) + 1;
  2936. }
  2937. goto done;
  2938. }
  2939. /* go backward to find any previous key */
  2940. ret = btrfs_previous_item(root, path, ino, key_type);
  2941. if (ret == 0) {
  2942. struct btrfs_key tmp;
  2943. btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
  2944. if (key_type == tmp.type) {
  2945. first_offset = tmp.offset;
  2946. ret = overwrite_item(trans, log, dst_path,
  2947. path->nodes[0], path->slots[0],
  2948. &tmp);
  2949. if (ret) {
  2950. err = ret;
  2951. goto done;
  2952. }
  2953. }
  2954. }
  2955. btrfs_release_path(path);
  2956. /* find the first key from this transaction again */
  2957. ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
  2958. if (WARN_ON(ret != 0))
  2959. goto done;
  2960. /*
  2961. * we have a block from this transaction, log every item in it
  2962. * from our directory
  2963. */
  2964. while (1) {
  2965. struct btrfs_key tmp;
  2966. src = path->nodes[0];
  2967. nritems = btrfs_header_nritems(src);
  2968. for (i = path->slots[0]; i < nritems; i++) {
  2969. struct btrfs_dir_item *di;
  2970. btrfs_item_key_to_cpu(src, &min_key, i);
  2971. if (min_key.objectid != ino || min_key.type != key_type)
  2972. goto done;
  2973. ret = overwrite_item(trans, log, dst_path, src, i,
  2974. &min_key);
  2975. if (ret) {
  2976. err = ret;
  2977. goto done;
  2978. }
  2979. /*
  2980. * We must make sure that when we log a directory entry,
  2981. * the corresponding inode, after log replay, has a
  2982. * matching link count. For example:
  2983. *
  2984. * touch foo
  2985. * mkdir mydir
  2986. * sync
  2987. * ln foo mydir/bar
  2988. * xfs_io -c "fsync" mydir
  2989. * <crash>
  2990. * <mount fs and log replay>
  2991. *
  2992. * Would result in a fsync log that when replayed, our
  2993. * file inode would have a link count of 1, but we get
  2994. * two directory entries pointing to the same inode.
  2995. * After removing one of the names, it would not be
  2996. * possible to remove the other name, which resulted
  2997. * always in stale file handle errors, and would not
  2998. * be possible to rmdir the parent directory, since
  2999. * its i_size could never decrement to the value
  3000. * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
  3001. */
  3002. di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
  3003. btrfs_dir_item_key_to_cpu(src, di, &tmp);
  3004. if (ctx &&
  3005. (btrfs_dir_transid(src, di) == trans->transid ||
  3006. btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
  3007. tmp.type != BTRFS_ROOT_ITEM_KEY)
  3008. ctx->log_new_dentries = true;
  3009. }
  3010. path->slots[0] = nritems;
  3011. /*
  3012. * look ahead to the next item and see if it is also
  3013. * from this directory and from this transaction
  3014. */
  3015. ret = btrfs_next_leaf(root, path);
  3016. if (ret == 1) {
  3017. last_offset = (u64)-1;
  3018. goto done;
  3019. }
  3020. btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
  3021. if (tmp.objectid != ino || tmp.type != key_type) {
  3022. last_offset = (u64)-1;
  3023. goto done;
  3024. }
  3025. if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
  3026. ret = overwrite_item(trans, log, dst_path,
  3027. path->nodes[0], path->slots[0],
  3028. &tmp);
  3029. if (ret)
  3030. err = ret;
  3031. else
  3032. last_offset = tmp.offset;
  3033. goto done;
  3034. }
  3035. }
  3036. done:
  3037. btrfs_release_path(path);
  3038. btrfs_release_path(dst_path);
  3039. if (err == 0) {
  3040. *last_offset_ret = last_offset;
  3041. /*
  3042. * insert the log range keys to indicate where the log
  3043. * is valid
  3044. */
  3045. ret = insert_dir_log_key(trans, log, path, key_type,
  3046. ino, first_offset, last_offset);
  3047. if (ret)
  3048. err = ret;
  3049. }
  3050. return err;
  3051. }
  3052. /*
  3053. * logging directories is very similar to logging inodes, We find all the items
  3054. * from the current transaction and write them to the log.
  3055. *
  3056. * The recovery code scans the directory in the subvolume, and if it finds a
  3057. * key in the range logged that is not present in the log tree, then it means
  3058. * that dir entry was unlinked during the transaction.
  3059. *
  3060. * In order for that scan to work, we must include one key smaller than
  3061. * the smallest logged by this transaction and one key larger than the largest
  3062. * key logged by this transaction.
  3063. */
  3064. static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
  3065. struct btrfs_root *root, struct inode *inode,
  3066. struct btrfs_path *path,
  3067. struct btrfs_path *dst_path,
  3068. struct btrfs_log_ctx *ctx)
  3069. {
  3070. u64 min_key;
  3071. u64 max_key;
  3072. int ret;
  3073. int key_type = BTRFS_DIR_ITEM_KEY;
  3074. again:
  3075. min_key = 0;
  3076. max_key = 0;
  3077. while (1) {
  3078. ret = log_dir_items(trans, root, inode, path,
  3079. dst_path, key_type, ctx, min_key,
  3080. &max_key);
  3081. if (ret)
  3082. return ret;
  3083. if (max_key == (u64)-1)
  3084. break;
  3085. min_key = max_key + 1;
  3086. }
  3087. if (key_type == BTRFS_DIR_ITEM_KEY) {
  3088. key_type = BTRFS_DIR_INDEX_KEY;
  3089. goto again;
  3090. }
  3091. return 0;
  3092. }
  3093. /*
  3094. * a helper function to drop items from the log before we relog an
  3095. * inode. max_key_type indicates the highest item type to remove.
  3096. * This cannot be run for file data extents because it does not
  3097. * free the extents they point to.
  3098. */
  3099. static int drop_objectid_items(struct btrfs_trans_handle *trans,
  3100. struct btrfs_root *log,
  3101. struct btrfs_path *path,
  3102. u64 objectid, int max_key_type)
  3103. {
  3104. int ret;
  3105. struct btrfs_key key;
  3106. struct btrfs_key found_key;
  3107. int start_slot;
  3108. key.objectid = objectid;
  3109. key.type = max_key_type;
  3110. key.offset = (u64)-1;
  3111. while (1) {
  3112. ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
  3113. BUG_ON(ret == 0); /* Logic error */
  3114. if (ret < 0)
  3115. break;
  3116. if (path->slots[0] == 0)
  3117. break;
  3118. path->slots[0]--;
  3119. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  3120. path->slots[0]);
  3121. if (found_key.objectid != objectid)
  3122. break;
  3123. found_key.offset = 0;
  3124. found_key.type = 0;
  3125. ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
  3126. &start_slot);
  3127. ret = btrfs_del_items(trans, log, path, start_slot,
  3128. path->slots[0] - start_slot + 1);
  3129. /*
  3130. * If start slot isn't 0 then we don't need to re-search, we've
  3131. * found the last guy with the objectid in this tree.
  3132. */
  3133. if (ret || start_slot != 0)
  3134. break;
  3135. btrfs_release_path(path);
  3136. }
  3137. btrfs_release_path(path);
  3138. if (ret > 0)
  3139. ret = 0;
  3140. return ret;
  3141. }
  3142. static void fill_inode_item(struct btrfs_trans_handle *trans,
  3143. struct extent_buffer *leaf,
  3144. struct btrfs_inode_item *item,
  3145. struct inode *inode, int log_inode_only,
  3146. u64 logged_isize)
  3147. {
  3148. struct btrfs_map_token token;
  3149. btrfs_init_map_token(&token);
  3150. if (log_inode_only) {
  3151. /* set the generation to zero so the recover code
  3152. * can tell the difference between an logging
  3153. * just to say 'this inode exists' and a logging
  3154. * to say 'update this inode with these values'
  3155. */
  3156. btrfs_set_token_inode_generation(leaf, item, 0, &token);
  3157. btrfs_set_token_inode_size(leaf, item, logged_isize, &token);
  3158. } else {
  3159. btrfs_set_token_inode_generation(leaf, item,
  3160. BTRFS_I(inode)->generation,
  3161. &token);
  3162. btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
  3163. }
  3164. btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
  3165. btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
  3166. btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
  3167. btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
  3168. btrfs_set_token_timespec_sec(leaf, &item->atime,
  3169. inode->i_atime.tv_sec, &token);
  3170. btrfs_set_token_timespec_nsec(leaf, &item->atime,
  3171. inode->i_atime.tv_nsec, &token);
  3172. btrfs_set_token_timespec_sec(leaf, &item->mtime,
  3173. inode->i_mtime.tv_sec, &token);
  3174. btrfs_set_token_timespec_nsec(leaf, &item->mtime,
  3175. inode->i_mtime.tv_nsec, &token);
  3176. btrfs_set_token_timespec_sec(leaf, &item->ctime,
  3177. inode->i_ctime.tv_sec, &token);
  3178. btrfs_set_token_timespec_nsec(leaf, &item->ctime,
  3179. inode->i_ctime.tv_nsec, &token);
  3180. btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
  3181. &token);
  3182. btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
  3183. btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
  3184. btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
  3185. btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
  3186. btrfs_set_token_inode_block_group(leaf, item, 0, &token);
  3187. }
  3188. static int log_inode_item(struct btrfs_trans_handle *trans,
  3189. struct btrfs_root *log, struct btrfs_path *path,
  3190. struct inode *inode)
  3191. {
  3192. struct btrfs_inode_item *inode_item;
  3193. int ret;
  3194. ret = btrfs_insert_empty_item(trans, log, path,
  3195. &BTRFS_I(inode)->location,
  3196. sizeof(*inode_item));
  3197. if (ret && ret != -EEXIST)
  3198. return ret;
  3199. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3200. struct btrfs_inode_item);
  3201. fill_inode_item(trans, path->nodes[0], inode_item, inode, 0, 0);
  3202. btrfs_release_path(path);
  3203. return 0;
  3204. }
  3205. static noinline int copy_items(struct btrfs_trans_handle *trans,
  3206. struct inode *inode,
  3207. struct btrfs_path *dst_path,
  3208. struct btrfs_path *src_path, u64 *last_extent,
  3209. int start_slot, int nr, int inode_only,
  3210. u64 logged_isize)
  3211. {
  3212. unsigned long src_offset;
  3213. unsigned long dst_offset;
  3214. struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
  3215. struct btrfs_file_extent_item *extent;
  3216. struct btrfs_inode_item *inode_item;
  3217. struct extent_buffer *src = src_path->nodes[0];
  3218. struct btrfs_key first_key, last_key, key;
  3219. int ret;
  3220. struct btrfs_key *ins_keys;
  3221. u32 *ins_sizes;
  3222. char *ins_data;
  3223. int i;
  3224. struct list_head ordered_sums;
  3225. int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  3226. bool has_extents = false;
  3227. bool need_find_last_extent = true;
  3228. bool done = false;
  3229. INIT_LIST_HEAD(&ordered_sums);
  3230. ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
  3231. nr * sizeof(u32), GFP_NOFS);
  3232. if (!ins_data)
  3233. return -ENOMEM;
  3234. first_key.objectid = (u64)-1;
  3235. ins_sizes = (u32 *)ins_data;
  3236. ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
  3237. for (i = 0; i < nr; i++) {
  3238. ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
  3239. btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
  3240. }
  3241. ret = btrfs_insert_empty_items(trans, log, dst_path,
  3242. ins_keys, ins_sizes, nr);
  3243. if (ret) {
  3244. kfree(ins_data);
  3245. return ret;
  3246. }
  3247. for (i = 0; i < nr; i++, dst_path->slots[0]++) {
  3248. dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
  3249. dst_path->slots[0]);
  3250. src_offset = btrfs_item_ptr_offset(src, start_slot + i);
  3251. if ((i == (nr - 1)))
  3252. last_key = ins_keys[i];
  3253. if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
  3254. inode_item = btrfs_item_ptr(dst_path->nodes[0],
  3255. dst_path->slots[0],
  3256. struct btrfs_inode_item);
  3257. fill_inode_item(trans, dst_path->nodes[0], inode_item,
  3258. inode, inode_only == LOG_INODE_EXISTS,
  3259. logged_isize);
  3260. } else {
  3261. copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
  3262. src_offset, ins_sizes[i]);
  3263. }
  3264. /*
  3265. * We set need_find_last_extent here in case we know we were
  3266. * processing other items and then walk into the first extent in
  3267. * the inode. If we don't hit an extent then nothing changes,
  3268. * we'll do the last search the next time around.
  3269. */
  3270. if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
  3271. has_extents = true;
  3272. if (first_key.objectid == (u64)-1)
  3273. first_key = ins_keys[i];
  3274. } else {
  3275. need_find_last_extent = false;
  3276. }
  3277. /* take a reference on file data extents so that truncates
  3278. * or deletes of this inode don't have to relog the inode
  3279. * again
  3280. */
  3281. if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
  3282. !skip_csum) {
  3283. int found_type;
  3284. extent = btrfs_item_ptr(src, start_slot + i,
  3285. struct btrfs_file_extent_item);
  3286. if (btrfs_file_extent_generation(src, extent) < trans->transid)
  3287. continue;
  3288. found_type = btrfs_file_extent_type(src, extent);
  3289. if (found_type == BTRFS_FILE_EXTENT_REG) {
  3290. u64 ds, dl, cs, cl;
  3291. ds = btrfs_file_extent_disk_bytenr(src,
  3292. extent);
  3293. /* ds == 0 is a hole */
  3294. if (ds == 0)
  3295. continue;
  3296. dl = btrfs_file_extent_disk_num_bytes(src,
  3297. extent);
  3298. cs = btrfs_file_extent_offset(src, extent);
  3299. cl = btrfs_file_extent_num_bytes(src,
  3300. extent);
  3301. if (btrfs_file_extent_compression(src,
  3302. extent)) {
  3303. cs = 0;
  3304. cl = dl;
  3305. }
  3306. ret = btrfs_lookup_csums_range(
  3307. log->fs_info->csum_root,
  3308. ds + cs, ds + cs + cl - 1,
  3309. &ordered_sums, 0);
  3310. if (ret) {
  3311. btrfs_release_path(dst_path);
  3312. kfree(ins_data);
  3313. return ret;
  3314. }
  3315. }
  3316. }
  3317. }
  3318. btrfs_mark_buffer_dirty(dst_path->nodes[0]);
  3319. btrfs_release_path(dst_path);
  3320. kfree(ins_data);
  3321. /*
  3322. * we have to do this after the loop above to avoid changing the
  3323. * log tree while trying to change the log tree.
  3324. */
  3325. ret = 0;
  3326. while (!list_empty(&ordered_sums)) {
  3327. struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
  3328. struct btrfs_ordered_sum,
  3329. list);
  3330. if (!ret)
  3331. ret = btrfs_csum_file_blocks(trans, log, sums);
  3332. list_del(&sums->list);
  3333. kfree(sums);
  3334. }
  3335. if (!has_extents)
  3336. return ret;
  3337. if (need_find_last_extent && *last_extent == first_key.offset) {
  3338. /*
  3339. * We don't have any leafs between our current one and the one
  3340. * we processed before that can have file extent items for our
  3341. * inode (and have a generation number smaller than our current
  3342. * transaction id).
  3343. */
  3344. need_find_last_extent = false;
  3345. }
  3346. /*
  3347. * Because we use btrfs_search_forward we could skip leaves that were
  3348. * not modified and then assume *last_extent is valid when it really
  3349. * isn't. So back up to the previous leaf and read the end of the last
  3350. * extent before we go and fill in holes.
  3351. */
  3352. if (need_find_last_extent) {
  3353. u64 len;
  3354. ret = btrfs_prev_leaf(BTRFS_I(inode)->root, src_path);
  3355. if (ret < 0)
  3356. return ret;
  3357. if (ret)
  3358. goto fill_holes;
  3359. if (src_path->slots[0])
  3360. src_path->slots[0]--;
  3361. src = src_path->nodes[0];
  3362. btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
  3363. if (key.objectid != btrfs_ino(inode) ||
  3364. key.type != BTRFS_EXTENT_DATA_KEY)
  3365. goto fill_holes;
  3366. extent = btrfs_item_ptr(src, src_path->slots[0],
  3367. struct btrfs_file_extent_item);
  3368. if (btrfs_file_extent_type(src, extent) ==
  3369. BTRFS_FILE_EXTENT_INLINE) {
  3370. len = btrfs_file_extent_inline_len(src,
  3371. src_path->slots[0],
  3372. extent);
  3373. *last_extent = ALIGN(key.offset + len,
  3374. log->sectorsize);
  3375. } else {
  3376. len = btrfs_file_extent_num_bytes(src, extent);
  3377. *last_extent = key.offset + len;
  3378. }
  3379. }
  3380. fill_holes:
  3381. /* So we did prev_leaf, now we need to move to the next leaf, but a few
  3382. * things could have happened
  3383. *
  3384. * 1) A merge could have happened, so we could currently be on a leaf
  3385. * that holds what we were copying in the first place.
  3386. * 2) A split could have happened, and now not all of the items we want
  3387. * are on the same leaf.
  3388. *
  3389. * So we need to adjust how we search for holes, we need to drop the
  3390. * path and re-search for the first extent key we found, and then walk
  3391. * forward until we hit the last one we copied.
  3392. */
  3393. if (need_find_last_extent) {
  3394. /* btrfs_prev_leaf could return 1 without releasing the path */
  3395. btrfs_release_path(src_path);
  3396. ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &first_key,
  3397. src_path, 0, 0);
  3398. if (ret < 0)
  3399. return ret;
  3400. ASSERT(ret == 0);
  3401. src = src_path->nodes[0];
  3402. i = src_path->slots[0];
  3403. } else {
  3404. i = start_slot;
  3405. }
  3406. /*
  3407. * Ok so here we need to go through and fill in any holes we may have
  3408. * to make sure that holes are punched for those areas in case they had
  3409. * extents previously.
  3410. */
  3411. while (!done) {
  3412. u64 offset, len;
  3413. u64 extent_end;
  3414. if (i >= btrfs_header_nritems(src_path->nodes[0])) {
  3415. ret = btrfs_next_leaf(BTRFS_I(inode)->root, src_path);
  3416. if (ret < 0)
  3417. return ret;
  3418. ASSERT(ret == 0);
  3419. src = src_path->nodes[0];
  3420. i = 0;
  3421. }
  3422. btrfs_item_key_to_cpu(src, &key, i);
  3423. if (!btrfs_comp_cpu_keys(&key, &last_key))
  3424. done = true;
  3425. if (key.objectid != btrfs_ino(inode) ||
  3426. key.type != BTRFS_EXTENT_DATA_KEY) {
  3427. i++;
  3428. continue;
  3429. }
  3430. extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
  3431. if (btrfs_file_extent_type(src, extent) ==
  3432. BTRFS_FILE_EXTENT_INLINE) {
  3433. len = btrfs_file_extent_inline_len(src, i, extent);
  3434. extent_end = ALIGN(key.offset + len, log->sectorsize);
  3435. } else {
  3436. len = btrfs_file_extent_num_bytes(src, extent);
  3437. extent_end = key.offset + len;
  3438. }
  3439. i++;
  3440. if (*last_extent == key.offset) {
  3441. *last_extent = extent_end;
  3442. continue;
  3443. }
  3444. offset = *last_extent;
  3445. len = key.offset - *last_extent;
  3446. ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
  3447. offset, 0, 0, len, 0, len, 0,
  3448. 0, 0);
  3449. if (ret)
  3450. break;
  3451. *last_extent = extent_end;
  3452. }
  3453. /*
  3454. * Need to let the callers know we dropped the path so they should
  3455. * re-search.
  3456. */
  3457. if (!ret && need_find_last_extent)
  3458. ret = 1;
  3459. return ret;
  3460. }
  3461. static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
  3462. {
  3463. struct extent_map *em1, *em2;
  3464. em1 = list_entry(a, struct extent_map, list);
  3465. em2 = list_entry(b, struct extent_map, list);
  3466. if (em1->start < em2->start)
  3467. return -1;
  3468. else if (em1->start > em2->start)
  3469. return 1;
  3470. return 0;
  3471. }
  3472. static int wait_ordered_extents(struct btrfs_trans_handle *trans,
  3473. struct inode *inode,
  3474. struct btrfs_root *root,
  3475. const struct extent_map *em,
  3476. const struct list_head *logged_list,
  3477. bool *ordered_io_error)
  3478. {
  3479. struct btrfs_ordered_extent *ordered;
  3480. struct btrfs_root *log = root->log_root;
  3481. u64 mod_start = em->mod_start;
  3482. u64 mod_len = em->mod_len;
  3483. const bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  3484. u64 csum_offset;
  3485. u64 csum_len;
  3486. LIST_HEAD(ordered_sums);
  3487. int ret = 0;
  3488. *ordered_io_error = false;
  3489. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
  3490. em->block_start == EXTENT_MAP_HOLE)
  3491. return 0;
  3492. /*
  3493. * Wait far any ordered extent that covers our extent map. If it
  3494. * finishes without an error, first check and see if our csums are on
  3495. * our outstanding ordered extents.
  3496. */
  3497. list_for_each_entry(ordered, logged_list, log_list) {
  3498. struct btrfs_ordered_sum *sum;
  3499. if (!mod_len)
  3500. break;
  3501. if (ordered->file_offset + ordered->len <= mod_start ||
  3502. mod_start + mod_len <= ordered->file_offset)
  3503. continue;
  3504. if (!test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) &&
  3505. !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) &&
  3506. !test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
  3507. const u64 start = ordered->file_offset;
  3508. const u64 end = ordered->file_offset + ordered->len - 1;
  3509. WARN_ON(ordered->inode != inode);
  3510. filemap_fdatawrite_range(inode->i_mapping, start, end);
  3511. }
  3512. wait_event(ordered->wait,
  3513. (test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) ||
  3514. test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)));
  3515. if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)) {
  3516. /*
  3517. * Clear the AS_EIO/AS_ENOSPC flags from the inode's
  3518. * i_mapping flags, so that the next fsync won't get
  3519. * an outdated io error too.
  3520. */
  3521. filemap_check_errors(inode->i_mapping);
  3522. *ordered_io_error = true;
  3523. break;
  3524. }
  3525. /*
  3526. * We are going to copy all the csums on this ordered extent, so
  3527. * go ahead and adjust mod_start and mod_len in case this
  3528. * ordered extent has already been logged.
  3529. */
  3530. if (ordered->file_offset > mod_start) {
  3531. if (ordered->file_offset + ordered->len >=
  3532. mod_start + mod_len)
  3533. mod_len = ordered->file_offset - mod_start;
  3534. /*
  3535. * If we have this case
  3536. *
  3537. * |--------- logged extent ---------|
  3538. * |----- ordered extent ----|
  3539. *
  3540. * Just don't mess with mod_start and mod_len, we'll
  3541. * just end up logging more csums than we need and it
  3542. * will be ok.
  3543. */
  3544. } else {
  3545. if (ordered->file_offset + ordered->len <
  3546. mod_start + mod_len) {
  3547. mod_len = (mod_start + mod_len) -
  3548. (ordered->file_offset + ordered->len);
  3549. mod_start = ordered->file_offset +
  3550. ordered->len;
  3551. } else {
  3552. mod_len = 0;
  3553. }
  3554. }
  3555. if (skip_csum)
  3556. continue;
  3557. /*
  3558. * To keep us from looping for the above case of an ordered
  3559. * extent that falls inside of the logged extent.
  3560. */
  3561. if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
  3562. &ordered->flags))
  3563. continue;
  3564. list_for_each_entry(sum, &ordered->list, list) {
  3565. ret = btrfs_csum_file_blocks(trans, log, sum);
  3566. if (ret)
  3567. break;
  3568. }
  3569. }
  3570. if (*ordered_io_error || !mod_len || ret || skip_csum)
  3571. return ret;
  3572. if (em->compress_type) {
  3573. csum_offset = 0;
  3574. csum_len = max(em->block_len, em->orig_block_len);
  3575. } else {
  3576. csum_offset = mod_start - em->start;
  3577. csum_len = mod_len;
  3578. }
  3579. /* block start is already adjusted for the file extent offset. */
  3580. ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
  3581. em->block_start + csum_offset,
  3582. em->block_start + csum_offset +
  3583. csum_len - 1, &ordered_sums, 0);
  3584. if (ret)
  3585. return ret;
  3586. while (!list_empty(&ordered_sums)) {
  3587. struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
  3588. struct btrfs_ordered_sum,
  3589. list);
  3590. if (!ret)
  3591. ret = btrfs_csum_file_blocks(trans, log, sums);
  3592. list_del(&sums->list);
  3593. kfree(sums);
  3594. }
  3595. return ret;
  3596. }
  3597. static int log_one_extent(struct btrfs_trans_handle *trans,
  3598. struct inode *inode, struct btrfs_root *root,
  3599. const struct extent_map *em,
  3600. struct btrfs_path *path,
  3601. const struct list_head *logged_list,
  3602. struct btrfs_log_ctx *ctx)
  3603. {
  3604. struct btrfs_root *log = root->log_root;
  3605. struct btrfs_file_extent_item *fi;
  3606. struct extent_buffer *leaf;
  3607. struct btrfs_map_token token;
  3608. struct btrfs_key key;
  3609. u64 extent_offset = em->start - em->orig_start;
  3610. u64 block_len;
  3611. int ret;
  3612. int extent_inserted = 0;
  3613. bool ordered_io_err = false;
  3614. ret = wait_ordered_extents(trans, inode, root, em, logged_list,
  3615. &ordered_io_err);
  3616. if (ret)
  3617. return ret;
  3618. if (ordered_io_err) {
  3619. ctx->io_err = -EIO;
  3620. return 0;
  3621. }
  3622. btrfs_init_map_token(&token);
  3623. ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
  3624. em->start + em->len, NULL, 0, 1,
  3625. sizeof(*fi), &extent_inserted);
  3626. if (ret)
  3627. return ret;
  3628. if (!extent_inserted) {
  3629. key.objectid = btrfs_ino(inode);
  3630. key.type = BTRFS_EXTENT_DATA_KEY;
  3631. key.offset = em->start;
  3632. ret = btrfs_insert_empty_item(trans, log, path, &key,
  3633. sizeof(*fi));
  3634. if (ret)
  3635. return ret;
  3636. }
  3637. leaf = path->nodes[0];
  3638. fi = btrfs_item_ptr(leaf, path->slots[0],
  3639. struct btrfs_file_extent_item);
  3640. btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
  3641. &token);
  3642. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  3643. btrfs_set_token_file_extent_type(leaf, fi,
  3644. BTRFS_FILE_EXTENT_PREALLOC,
  3645. &token);
  3646. else
  3647. btrfs_set_token_file_extent_type(leaf, fi,
  3648. BTRFS_FILE_EXTENT_REG,
  3649. &token);
  3650. block_len = max(em->block_len, em->orig_block_len);
  3651. if (em->compress_type != BTRFS_COMPRESS_NONE) {
  3652. btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
  3653. em->block_start,
  3654. &token);
  3655. btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
  3656. &token);
  3657. } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
  3658. btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
  3659. em->block_start -
  3660. extent_offset, &token);
  3661. btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
  3662. &token);
  3663. } else {
  3664. btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
  3665. btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
  3666. &token);
  3667. }
  3668. btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
  3669. btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
  3670. btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
  3671. btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
  3672. &token);
  3673. btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
  3674. btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
  3675. btrfs_mark_buffer_dirty(leaf);
  3676. btrfs_release_path(path);
  3677. return ret;
  3678. }
  3679. static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
  3680. struct btrfs_root *root,
  3681. struct inode *inode,
  3682. struct btrfs_path *path,
  3683. struct list_head *logged_list,
  3684. struct btrfs_log_ctx *ctx,
  3685. const u64 start,
  3686. const u64 end)
  3687. {
  3688. struct extent_map *em, *n;
  3689. struct list_head extents;
  3690. struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
  3691. u64 test_gen;
  3692. int ret = 0;
  3693. int num = 0;
  3694. INIT_LIST_HEAD(&extents);
  3695. down_write(&BTRFS_I(inode)->dio_sem);
  3696. write_lock(&tree->lock);
  3697. test_gen = root->fs_info->last_trans_committed;
  3698. list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
  3699. list_del_init(&em->list);
  3700. /*
  3701. * Just an arbitrary number, this can be really CPU intensive
  3702. * once we start getting a lot of extents, and really once we
  3703. * have a bunch of extents we just want to commit since it will
  3704. * be faster.
  3705. */
  3706. if (++num > 32768) {
  3707. list_del_init(&tree->modified_extents);
  3708. ret = -EFBIG;
  3709. goto process;
  3710. }
  3711. if (em->generation <= test_gen)
  3712. continue;
  3713. /* Need a ref to keep it from getting evicted from cache */
  3714. atomic_inc(&em->refs);
  3715. set_bit(EXTENT_FLAG_LOGGING, &em->flags);
  3716. list_add_tail(&em->list, &extents);
  3717. num++;
  3718. }
  3719. list_sort(NULL, &extents, extent_cmp);
  3720. btrfs_get_logged_extents(inode, logged_list, start, end);
  3721. /*
  3722. * Some ordered extents started by fsync might have completed
  3723. * before we could collect them into the list logged_list, which
  3724. * means they're gone, not in our logged_list nor in the inode's
  3725. * ordered tree. We want the application/user space to know an
  3726. * error happened while attempting to persist file data so that
  3727. * it can take proper action. If such error happened, we leave
  3728. * without writing to the log tree and the fsync must report the
  3729. * file data write error and not commit the current transaction.
  3730. */
  3731. ret = filemap_check_errors(inode->i_mapping);
  3732. if (ret)
  3733. ctx->io_err = ret;
  3734. process:
  3735. while (!list_empty(&extents)) {
  3736. em = list_entry(extents.next, struct extent_map, list);
  3737. list_del_init(&em->list);
  3738. /*
  3739. * If we had an error we just need to delete everybody from our
  3740. * private list.
  3741. */
  3742. if (ret) {
  3743. clear_em_logging(tree, em);
  3744. free_extent_map(em);
  3745. continue;
  3746. }
  3747. write_unlock(&tree->lock);
  3748. ret = log_one_extent(trans, inode, root, em, path, logged_list,
  3749. ctx);
  3750. write_lock(&tree->lock);
  3751. clear_em_logging(tree, em);
  3752. free_extent_map(em);
  3753. }
  3754. WARN_ON(!list_empty(&extents));
  3755. write_unlock(&tree->lock);
  3756. up_write(&BTRFS_I(inode)->dio_sem);
  3757. btrfs_release_path(path);
  3758. return ret;
  3759. }
  3760. static int logged_inode_size(struct btrfs_root *log, struct inode *inode,
  3761. struct btrfs_path *path, u64 *size_ret)
  3762. {
  3763. struct btrfs_key key;
  3764. int ret;
  3765. key.objectid = btrfs_ino(inode);
  3766. key.type = BTRFS_INODE_ITEM_KEY;
  3767. key.offset = 0;
  3768. ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
  3769. if (ret < 0) {
  3770. return ret;
  3771. } else if (ret > 0) {
  3772. *size_ret = 0;
  3773. } else {
  3774. struct btrfs_inode_item *item;
  3775. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3776. struct btrfs_inode_item);
  3777. *size_ret = btrfs_inode_size(path->nodes[0], item);
  3778. }
  3779. btrfs_release_path(path);
  3780. return 0;
  3781. }
  3782. /*
  3783. * At the moment we always log all xattrs. This is to figure out at log replay
  3784. * time which xattrs must have their deletion replayed. If a xattr is missing
  3785. * in the log tree and exists in the fs/subvol tree, we delete it. This is
  3786. * because if a xattr is deleted, the inode is fsynced and a power failure
  3787. * happens, causing the log to be replayed the next time the fs is mounted,
  3788. * we want the xattr to not exist anymore (same behaviour as other filesystems
  3789. * with a journal, ext3/4, xfs, f2fs, etc).
  3790. */
  3791. static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
  3792. struct btrfs_root *root,
  3793. struct inode *inode,
  3794. struct btrfs_path *path,
  3795. struct btrfs_path *dst_path)
  3796. {
  3797. int ret;
  3798. struct btrfs_key key;
  3799. const u64 ino = btrfs_ino(inode);
  3800. int ins_nr = 0;
  3801. int start_slot = 0;
  3802. key.objectid = ino;
  3803. key.type = BTRFS_XATTR_ITEM_KEY;
  3804. key.offset = 0;
  3805. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3806. if (ret < 0)
  3807. return ret;
  3808. while (true) {
  3809. int slot = path->slots[0];
  3810. struct extent_buffer *leaf = path->nodes[0];
  3811. int nritems = btrfs_header_nritems(leaf);
  3812. if (slot >= nritems) {
  3813. if (ins_nr > 0) {
  3814. u64 last_extent = 0;
  3815. ret = copy_items(trans, inode, dst_path, path,
  3816. &last_extent, start_slot,
  3817. ins_nr, 1, 0);
  3818. /* can't be 1, extent items aren't processed */
  3819. ASSERT(ret <= 0);
  3820. if (ret < 0)
  3821. return ret;
  3822. ins_nr = 0;
  3823. }
  3824. ret = btrfs_next_leaf(root, path);
  3825. if (ret < 0)
  3826. return ret;
  3827. else if (ret > 0)
  3828. break;
  3829. continue;
  3830. }
  3831. btrfs_item_key_to_cpu(leaf, &key, slot);
  3832. if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
  3833. break;
  3834. if (ins_nr == 0)
  3835. start_slot = slot;
  3836. ins_nr++;
  3837. path->slots[0]++;
  3838. cond_resched();
  3839. }
  3840. if (ins_nr > 0) {
  3841. u64 last_extent = 0;
  3842. ret = copy_items(trans, inode, dst_path, path,
  3843. &last_extent, start_slot,
  3844. ins_nr, 1, 0);
  3845. /* can't be 1, extent items aren't processed */
  3846. ASSERT(ret <= 0);
  3847. if (ret < 0)
  3848. return ret;
  3849. }
  3850. return 0;
  3851. }
  3852. /*
  3853. * If the no holes feature is enabled we need to make sure any hole between the
  3854. * last extent and the i_size of our inode is explicitly marked in the log. This
  3855. * is to make sure that doing something like:
  3856. *
  3857. * 1) create file with 128Kb of data
  3858. * 2) truncate file to 64Kb
  3859. * 3) truncate file to 256Kb
  3860. * 4) fsync file
  3861. * 5) <crash/power failure>
  3862. * 6) mount fs and trigger log replay
  3863. *
  3864. * Will give us a file with a size of 256Kb, the first 64Kb of data match what
  3865. * the file had in its first 64Kb of data at step 1 and the last 192Kb of the
  3866. * file correspond to a hole. The presence of explicit holes in a log tree is
  3867. * what guarantees that log replay will remove/adjust file extent items in the
  3868. * fs/subvol tree.
  3869. *
  3870. * Here we do not need to care about holes between extents, that is already done
  3871. * by copy_items(). We also only need to do this in the full sync path, where we
  3872. * lookup for extents from the fs/subvol tree only. In the fast path case, we
  3873. * lookup the list of modified extent maps and if any represents a hole, we
  3874. * insert a corresponding extent representing a hole in the log tree.
  3875. */
  3876. static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
  3877. struct btrfs_root *root,
  3878. struct inode *inode,
  3879. struct btrfs_path *path)
  3880. {
  3881. int ret;
  3882. struct btrfs_key key;
  3883. u64 hole_start;
  3884. u64 hole_size;
  3885. struct extent_buffer *leaf;
  3886. struct btrfs_root *log = root->log_root;
  3887. const u64 ino = btrfs_ino(inode);
  3888. const u64 i_size = i_size_read(inode);
  3889. if (!btrfs_fs_incompat(root->fs_info, NO_HOLES))
  3890. return 0;
  3891. key.objectid = ino;
  3892. key.type = BTRFS_EXTENT_DATA_KEY;
  3893. key.offset = (u64)-1;
  3894. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3895. ASSERT(ret != 0);
  3896. if (ret < 0)
  3897. return ret;
  3898. ASSERT(path->slots[0] > 0);
  3899. path->slots[0]--;
  3900. leaf = path->nodes[0];
  3901. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  3902. if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
  3903. /* inode does not have any extents */
  3904. hole_start = 0;
  3905. hole_size = i_size;
  3906. } else {
  3907. struct btrfs_file_extent_item *extent;
  3908. u64 len;
  3909. /*
  3910. * If there's an extent beyond i_size, an explicit hole was
  3911. * already inserted by copy_items().
  3912. */
  3913. if (key.offset >= i_size)
  3914. return 0;
  3915. extent = btrfs_item_ptr(leaf, path->slots[0],
  3916. struct btrfs_file_extent_item);
  3917. if (btrfs_file_extent_type(leaf, extent) ==
  3918. BTRFS_FILE_EXTENT_INLINE) {
  3919. len = btrfs_file_extent_inline_len(leaf,
  3920. path->slots[0],
  3921. extent);
  3922. ASSERT(len == i_size);
  3923. return 0;
  3924. }
  3925. len = btrfs_file_extent_num_bytes(leaf, extent);
  3926. /* Last extent goes beyond i_size, no need to log a hole. */
  3927. if (key.offset + len > i_size)
  3928. return 0;
  3929. hole_start = key.offset + len;
  3930. hole_size = i_size - hole_start;
  3931. }
  3932. btrfs_release_path(path);
  3933. /* Last extent ends at i_size. */
  3934. if (hole_size == 0)
  3935. return 0;
  3936. hole_size = ALIGN(hole_size, root->sectorsize);
  3937. ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0,
  3938. hole_size, 0, hole_size, 0, 0, 0);
  3939. return ret;
  3940. }
  3941. /*
  3942. * When we are logging a new inode X, check if it doesn't have a reference that
  3943. * matches the reference from some other inode Y created in a past transaction
  3944. * and that was renamed in the current transaction. If we don't do this, then at
  3945. * log replay time we can lose inode Y (and all its files if it's a directory):
  3946. *
  3947. * mkdir /mnt/x
  3948. * echo "hello world" > /mnt/x/foobar
  3949. * sync
  3950. * mv /mnt/x /mnt/y
  3951. * mkdir /mnt/x # or touch /mnt/x
  3952. * xfs_io -c fsync /mnt/x
  3953. * <power fail>
  3954. * mount fs, trigger log replay
  3955. *
  3956. * After the log replay procedure, we would lose the first directory and all its
  3957. * files (file foobar).
  3958. * For the case where inode Y is not a directory we simply end up losing it:
  3959. *
  3960. * echo "123" > /mnt/foo
  3961. * sync
  3962. * mv /mnt/foo /mnt/bar
  3963. * echo "abc" > /mnt/foo
  3964. * xfs_io -c fsync /mnt/foo
  3965. * <power fail>
  3966. *
  3967. * We also need this for cases where a snapshot entry is replaced by some other
  3968. * entry (file or directory) otherwise we end up with an unreplayable log due to
  3969. * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
  3970. * if it were a regular entry:
  3971. *
  3972. * mkdir /mnt/x
  3973. * btrfs subvolume snapshot /mnt /mnt/x/snap
  3974. * btrfs subvolume delete /mnt/x/snap
  3975. * rmdir /mnt/x
  3976. * mkdir /mnt/x
  3977. * fsync /mnt/x or fsync some new file inside it
  3978. * <power fail>
  3979. *
  3980. * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
  3981. * the same transaction.
  3982. */
  3983. static int btrfs_check_ref_name_override(struct extent_buffer *eb,
  3984. const int slot,
  3985. const struct btrfs_key *key,
  3986. struct inode *inode,
  3987. u64 *other_ino)
  3988. {
  3989. int ret;
  3990. struct btrfs_path *search_path;
  3991. char *name = NULL;
  3992. u32 name_len = 0;
  3993. u32 item_size = btrfs_item_size_nr(eb, slot);
  3994. u32 cur_offset = 0;
  3995. unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
  3996. search_path = btrfs_alloc_path();
  3997. if (!search_path)
  3998. return -ENOMEM;
  3999. search_path->search_commit_root = 1;
  4000. search_path->skip_locking = 1;
  4001. while (cur_offset < item_size) {
  4002. u64 parent;
  4003. u32 this_name_len;
  4004. u32 this_len;
  4005. unsigned long name_ptr;
  4006. struct btrfs_dir_item *di;
  4007. if (key->type == BTRFS_INODE_REF_KEY) {
  4008. struct btrfs_inode_ref *iref;
  4009. iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
  4010. parent = key->offset;
  4011. this_name_len = btrfs_inode_ref_name_len(eb, iref);
  4012. name_ptr = (unsigned long)(iref + 1);
  4013. this_len = sizeof(*iref) + this_name_len;
  4014. } else {
  4015. struct btrfs_inode_extref *extref;
  4016. extref = (struct btrfs_inode_extref *)(ptr +
  4017. cur_offset);
  4018. parent = btrfs_inode_extref_parent(eb, extref);
  4019. this_name_len = btrfs_inode_extref_name_len(eb, extref);
  4020. name_ptr = (unsigned long)&extref->name;
  4021. this_len = sizeof(*extref) + this_name_len;
  4022. }
  4023. if (this_name_len > name_len) {
  4024. char *new_name;
  4025. new_name = krealloc(name, this_name_len, GFP_NOFS);
  4026. if (!new_name) {
  4027. ret = -ENOMEM;
  4028. goto out;
  4029. }
  4030. name_len = this_name_len;
  4031. name = new_name;
  4032. }
  4033. read_extent_buffer(eb, name, name_ptr, this_name_len);
  4034. di = btrfs_lookup_dir_item(NULL, BTRFS_I(inode)->root,
  4035. search_path, parent,
  4036. name, this_name_len, 0);
  4037. if (di && !IS_ERR(di)) {
  4038. struct btrfs_key di_key;
  4039. btrfs_dir_item_key_to_cpu(search_path->nodes[0],
  4040. di, &di_key);
  4041. if (di_key.type == BTRFS_INODE_ITEM_KEY) {
  4042. ret = 1;
  4043. *other_ino = di_key.objectid;
  4044. } else {
  4045. ret = -EAGAIN;
  4046. }
  4047. goto out;
  4048. } else if (IS_ERR(di)) {
  4049. ret = PTR_ERR(di);
  4050. goto out;
  4051. }
  4052. btrfs_release_path(search_path);
  4053. cur_offset += this_len;
  4054. }
  4055. ret = 0;
  4056. out:
  4057. btrfs_free_path(search_path);
  4058. kfree(name);
  4059. return ret;
  4060. }
  4061. /* log a single inode in the tree log.
  4062. * At least one parent directory for this inode must exist in the tree
  4063. * or be logged already.
  4064. *
  4065. * Any items from this inode changed by the current transaction are copied
  4066. * to the log tree. An extra reference is taken on any extents in this
  4067. * file, allowing us to avoid a whole pile of corner cases around logging
  4068. * blocks that have been removed from the tree.
  4069. *
  4070. * See LOG_INODE_ALL and related defines for a description of what inode_only
  4071. * does.
  4072. *
  4073. * This handles both files and directories.
  4074. */
  4075. static int btrfs_log_inode(struct btrfs_trans_handle *trans,
  4076. struct btrfs_root *root, struct inode *inode,
  4077. int inode_only,
  4078. const loff_t start,
  4079. const loff_t end,
  4080. struct btrfs_log_ctx *ctx)
  4081. {
  4082. struct btrfs_path *path;
  4083. struct btrfs_path *dst_path;
  4084. struct btrfs_key min_key;
  4085. struct btrfs_key max_key;
  4086. struct btrfs_root *log = root->log_root;
  4087. struct extent_buffer *src = NULL;
  4088. LIST_HEAD(logged_list);
  4089. u64 last_extent = 0;
  4090. int err = 0;
  4091. int ret;
  4092. int nritems;
  4093. int ins_start_slot = 0;
  4094. int ins_nr;
  4095. bool fast_search = false;
  4096. u64 ino = btrfs_ino(inode);
  4097. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  4098. u64 logged_isize = 0;
  4099. bool need_log_inode_item = true;
  4100. path = btrfs_alloc_path();
  4101. if (!path)
  4102. return -ENOMEM;
  4103. dst_path = btrfs_alloc_path();
  4104. if (!dst_path) {
  4105. btrfs_free_path(path);
  4106. return -ENOMEM;
  4107. }
  4108. min_key.objectid = ino;
  4109. min_key.type = BTRFS_INODE_ITEM_KEY;
  4110. min_key.offset = 0;
  4111. max_key.objectid = ino;
  4112. /* today the code can only do partial logging of directories */
  4113. if (S_ISDIR(inode->i_mode) ||
  4114. (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  4115. &BTRFS_I(inode)->runtime_flags) &&
  4116. inode_only == LOG_INODE_EXISTS))
  4117. max_key.type = BTRFS_XATTR_ITEM_KEY;
  4118. else
  4119. max_key.type = (u8)-1;
  4120. max_key.offset = (u64)-1;
  4121. /*
  4122. * Only run delayed items if we are a dir or a new file.
  4123. * Otherwise commit the delayed inode only, which is needed in
  4124. * order for the log replay code to mark inodes for link count
  4125. * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
  4126. */
  4127. if (S_ISDIR(inode->i_mode) ||
  4128. BTRFS_I(inode)->generation > root->fs_info->last_trans_committed)
  4129. ret = btrfs_commit_inode_delayed_items(trans, inode);
  4130. else
  4131. ret = btrfs_commit_inode_delayed_inode(inode);
  4132. if (ret) {
  4133. btrfs_free_path(path);
  4134. btrfs_free_path(dst_path);
  4135. return ret;
  4136. }
  4137. mutex_lock(&BTRFS_I(inode)->log_mutex);
  4138. /*
  4139. * a brute force approach to making sure we get the most uptodate
  4140. * copies of everything.
  4141. */
  4142. if (S_ISDIR(inode->i_mode)) {
  4143. int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
  4144. if (inode_only == LOG_INODE_EXISTS)
  4145. max_key_type = BTRFS_XATTR_ITEM_KEY;
  4146. ret = drop_objectid_items(trans, log, path, ino, max_key_type);
  4147. } else {
  4148. if (inode_only == LOG_INODE_EXISTS) {
  4149. /*
  4150. * Make sure the new inode item we write to the log has
  4151. * the same isize as the current one (if it exists).
  4152. * This is necessary to prevent data loss after log
  4153. * replay, and also to prevent doing a wrong expanding
  4154. * truncate - for e.g. create file, write 4K into offset
  4155. * 0, fsync, write 4K into offset 4096, add hard link,
  4156. * fsync some other file (to sync log), power fail - if
  4157. * we use the inode's current i_size, after log replay
  4158. * we get a 8Kb file, with the last 4Kb extent as a hole
  4159. * (zeroes), as if an expanding truncate happened,
  4160. * instead of getting a file of 4Kb only.
  4161. */
  4162. err = logged_inode_size(log, inode, path,
  4163. &logged_isize);
  4164. if (err)
  4165. goto out_unlock;
  4166. }
  4167. if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  4168. &BTRFS_I(inode)->runtime_flags)) {
  4169. if (inode_only == LOG_INODE_EXISTS) {
  4170. max_key.type = BTRFS_XATTR_ITEM_KEY;
  4171. ret = drop_objectid_items(trans, log, path, ino,
  4172. max_key.type);
  4173. } else {
  4174. clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  4175. &BTRFS_I(inode)->runtime_flags);
  4176. clear_bit(BTRFS_INODE_COPY_EVERYTHING,
  4177. &BTRFS_I(inode)->runtime_flags);
  4178. while(1) {
  4179. ret = btrfs_truncate_inode_items(trans,
  4180. log, inode, 0, 0);
  4181. if (ret != -EAGAIN)
  4182. break;
  4183. }
  4184. }
  4185. } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
  4186. &BTRFS_I(inode)->runtime_flags) ||
  4187. inode_only == LOG_INODE_EXISTS) {
  4188. if (inode_only == LOG_INODE_ALL)
  4189. fast_search = true;
  4190. max_key.type = BTRFS_XATTR_ITEM_KEY;
  4191. ret = drop_objectid_items(trans, log, path, ino,
  4192. max_key.type);
  4193. } else {
  4194. if (inode_only == LOG_INODE_ALL)
  4195. fast_search = true;
  4196. goto log_extents;
  4197. }
  4198. }
  4199. if (ret) {
  4200. err = ret;
  4201. goto out_unlock;
  4202. }
  4203. while (1) {
  4204. ins_nr = 0;
  4205. ret = btrfs_search_forward(root, &min_key,
  4206. path, trans->transid);
  4207. if (ret < 0) {
  4208. err = ret;
  4209. goto out_unlock;
  4210. }
  4211. if (ret != 0)
  4212. break;
  4213. again:
  4214. /* note, ins_nr might be > 0 here, cleanup outside the loop */
  4215. if (min_key.objectid != ino)
  4216. break;
  4217. if (min_key.type > max_key.type)
  4218. break;
  4219. if (min_key.type == BTRFS_INODE_ITEM_KEY)
  4220. need_log_inode_item = false;
  4221. if ((min_key.type == BTRFS_INODE_REF_KEY ||
  4222. min_key.type == BTRFS_INODE_EXTREF_KEY) &&
  4223. BTRFS_I(inode)->generation == trans->transid) {
  4224. u64 other_ino = 0;
  4225. ret = btrfs_check_ref_name_override(path->nodes[0],
  4226. path->slots[0],
  4227. &min_key, inode,
  4228. &other_ino);
  4229. if (ret < 0) {
  4230. err = ret;
  4231. goto out_unlock;
  4232. } else if (ret > 0 && ctx &&
  4233. other_ino != btrfs_ino(ctx->inode)) {
  4234. struct btrfs_key inode_key;
  4235. struct inode *other_inode;
  4236. if (ins_nr > 0) {
  4237. ins_nr++;
  4238. } else {
  4239. ins_nr = 1;
  4240. ins_start_slot = path->slots[0];
  4241. }
  4242. ret = copy_items(trans, inode, dst_path, path,
  4243. &last_extent, ins_start_slot,
  4244. ins_nr, inode_only,
  4245. logged_isize);
  4246. if (ret < 0) {
  4247. err = ret;
  4248. goto out_unlock;
  4249. }
  4250. ins_nr = 0;
  4251. btrfs_release_path(path);
  4252. inode_key.objectid = other_ino;
  4253. inode_key.type = BTRFS_INODE_ITEM_KEY;
  4254. inode_key.offset = 0;
  4255. other_inode = btrfs_iget(root->fs_info->sb,
  4256. &inode_key, root,
  4257. NULL);
  4258. /*
  4259. * If the other inode that had a conflicting dir
  4260. * entry was deleted in the current transaction,
  4261. * we don't need to do more work nor fallback to
  4262. * a transaction commit.
  4263. */
  4264. if (IS_ERR(other_inode) &&
  4265. PTR_ERR(other_inode) == -ENOENT) {
  4266. goto next_key;
  4267. } else if (IS_ERR(other_inode)) {
  4268. err = PTR_ERR(other_inode);
  4269. goto out_unlock;
  4270. }
  4271. /*
  4272. * We are safe logging the other inode without
  4273. * acquiring its i_mutex as long as we log with
  4274. * the LOG_INODE_EXISTS mode. We're safe against
  4275. * concurrent renames of the other inode as well
  4276. * because during a rename we pin the log and
  4277. * update the log with the new name before we
  4278. * unpin it.
  4279. */
  4280. err = btrfs_log_inode(trans, root, other_inode,
  4281. LOG_INODE_EXISTS,
  4282. 0, LLONG_MAX, ctx);
  4283. iput(other_inode);
  4284. if (err)
  4285. goto out_unlock;
  4286. else
  4287. goto next_key;
  4288. }
  4289. }
  4290. /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
  4291. if (min_key.type == BTRFS_XATTR_ITEM_KEY) {
  4292. if (ins_nr == 0)
  4293. goto next_slot;
  4294. ret = copy_items(trans, inode, dst_path, path,
  4295. &last_extent, ins_start_slot,
  4296. ins_nr, inode_only, logged_isize);
  4297. if (ret < 0) {
  4298. err = ret;
  4299. goto out_unlock;
  4300. }
  4301. ins_nr = 0;
  4302. if (ret) {
  4303. btrfs_release_path(path);
  4304. continue;
  4305. }
  4306. goto next_slot;
  4307. }
  4308. src = path->nodes[0];
  4309. if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
  4310. ins_nr++;
  4311. goto next_slot;
  4312. } else if (!ins_nr) {
  4313. ins_start_slot = path->slots[0];
  4314. ins_nr = 1;
  4315. goto next_slot;
  4316. }
  4317. ret = copy_items(trans, inode, dst_path, path, &last_extent,
  4318. ins_start_slot, ins_nr, inode_only,
  4319. logged_isize);
  4320. if (ret < 0) {
  4321. err = ret;
  4322. goto out_unlock;
  4323. }
  4324. if (ret) {
  4325. ins_nr = 0;
  4326. btrfs_release_path(path);
  4327. continue;
  4328. }
  4329. ins_nr = 1;
  4330. ins_start_slot = path->slots[0];
  4331. next_slot:
  4332. nritems = btrfs_header_nritems(path->nodes[0]);
  4333. path->slots[0]++;
  4334. if (path->slots[0] < nritems) {
  4335. btrfs_item_key_to_cpu(path->nodes[0], &min_key,
  4336. path->slots[0]);
  4337. goto again;
  4338. }
  4339. if (ins_nr) {
  4340. ret = copy_items(trans, inode, dst_path, path,
  4341. &last_extent, ins_start_slot,
  4342. ins_nr, inode_only, logged_isize);
  4343. if (ret < 0) {
  4344. err = ret;
  4345. goto out_unlock;
  4346. }
  4347. ret = 0;
  4348. ins_nr = 0;
  4349. }
  4350. btrfs_release_path(path);
  4351. next_key:
  4352. if (min_key.offset < (u64)-1) {
  4353. min_key.offset++;
  4354. } else if (min_key.type < max_key.type) {
  4355. min_key.type++;
  4356. min_key.offset = 0;
  4357. } else {
  4358. break;
  4359. }
  4360. }
  4361. if (ins_nr) {
  4362. ret = copy_items(trans, inode, dst_path, path, &last_extent,
  4363. ins_start_slot, ins_nr, inode_only,
  4364. logged_isize);
  4365. if (ret < 0) {
  4366. err = ret;
  4367. goto out_unlock;
  4368. }
  4369. ret = 0;
  4370. ins_nr = 0;
  4371. }
  4372. btrfs_release_path(path);
  4373. btrfs_release_path(dst_path);
  4374. err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
  4375. if (err)
  4376. goto out_unlock;
  4377. if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
  4378. btrfs_release_path(path);
  4379. btrfs_release_path(dst_path);
  4380. err = btrfs_log_trailing_hole(trans, root, inode, path);
  4381. if (err)
  4382. goto out_unlock;
  4383. }
  4384. log_extents:
  4385. btrfs_release_path(path);
  4386. btrfs_release_path(dst_path);
  4387. if (need_log_inode_item) {
  4388. err = log_inode_item(trans, log, dst_path, inode);
  4389. if (err)
  4390. goto out_unlock;
  4391. }
  4392. if (fast_search) {
  4393. ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
  4394. &logged_list, ctx, start, end);
  4395. if (ret) {
  4396. err = ret;
  4397. goto out_unlock;
  4398. }
  4399. } else if (inode_only == LOG_INODE_ALL) {
  4400. struct extent_map *em, *n;
  4401. write_lock(&em_tree->lock);
  4402. /*
  4403. * We can't just remove every em if we're called for a ranged
  4404. * fsync - that is, one that doesn't cover the whole possible
  4405. * file range (0 to LLONG_MAX). This is because we can have
  4406. * em's that fall outside the range we're logging and therefore
  4407. * their ordered operations haven't completed yet
  4408. * (btrfs_finish_ordered_io() not invoked yet). This means we
  4409. * didn't get their respective file extent item in the fs/subvol
  4410. * tree yet, and need to let the next fast fsync (one which
  4411. * consults the list of modified extent maps) find the em so
  4412. * that it logs a matching file extent item and waits for the
  4413. * respective ordered operation to complete (if it's still
  4414. * running).
  4415. *
  4416. * Removing every em outside the range we're logging would make
  4417. * the next fast fsync not log their matching file extent items,
  4418. * therefore making us lose data after a log replay.
  4419. */
  4420. list_for_each_entry_safe(em, n, &em_tree->modified_extents,
  4421. list) {
  4422. const u64 mod_end = em->mod_start + em->mod_len - 1;
  4423. if (em->mod_start >= start && mod_end <= end)
  4424. list_del_init(&em->list);
  4425. }
  4426. write_unlock(&em_tree->lock);
  4427. }
  4428. if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
  4429. ret = log_directory_changes(trans, root, inode, path, dst_path,
  4430. ctx);
  4431. if (ret) {
  4432. err = ret;
  4433. goto out_unlock;
  4434. }
  4435. }
  4436. spin_lock(&BTRFS_I(inode)->lock);
  4437. BTRFS_I(inode)->logged_trans = trans->transid;
  4438. BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans;
  4439. spin_unlock(&BTRFS_I(inode)->lock);
  4440. out_unlock:
  4441. if (unlikely(err))
  4442. btrfs_put_logged_extents(&logged_list);
  4443. else
  4444. btrfs_submit_logged_extents(&logged_list, log);
  4445. mutex_unlock(&BTRFS_I(inode)->log_mutex);
  4446. btrfs_free_path(path);
  4447. btrfs_free_path(dst_path);
  4448. return err;
  4449. }
  4450. /*
  4451. * Check if we must fallback to a transaction commit when logging an inode.
  4452. * This must be called after logging the inode and is used only in the context
  4453. * when fsyncing an inode requires the need to log some other inode - in which
  4454. * case we can't lock the i_mutex of each other inode we need to log as that
  4455. * can lead to deadlocks with concurrent fsync against other inodes (as we can
  4456. * log inodes up or down in the hierarchy) or rename operations for example. So
  4457. * we take the log_mutex of the inode after we have logged it and then check for
  4458. * its last_unlink_trans value - this is safe because any task setting
  4459. * last_unlink_trans must take the log_mutex and it must do this before it does
  4460. * the actual unlink operation, so if we do this check before a concurrent task
  4461. * sets last_unlink_trans it means we've logged a consistent version/state of
  4462. * all the inode items, otherwise we are not sure and must do a transaction
  4463. * commit (the concurrent task might have only updated last_unlink_trans before
  4464. * we logged the inode or it might have also done the unlink).
  4465. */
  4466. static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
  4467. struct inode *inode)
  4468. {
  4469. struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
  4470. bool ret = false;
  4471. mutex_lock(&BTRFS_I(inode)->log_mutex);
  4472. if (BTRFS_I(inode)->last_unlink_trans > fs_info->last_trans_committed) {
  4473. /*
  4474. * Make sure any commits to the log are forced to be full
  4475. * commits.
  4476. */
  4477. btrfs_set_log_full_commit(fs_info, trans);
  4478. ret = true;
  4479. }
  4480. mutex_unlock(&BTRFS_I(inode)->log_mutex);
  4481. return ret;
  4482. }
  4483. /*
  4484. * follow the dentry parent pointers up the chain and see if any
  4485. * of the directories in it require a full commit before they can
  4486. * be logged. Returns zero if nothing special needs to be done or 1 if
  4487. * a full commit is required.
  4488. */
  4489. static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
  4490. struct inode *inode,
  4491. struct dentry *parent,
  4492. struct super_block *sb,
  4493. u64 last_committed)
  4494. {
  4495. int ret = 0;
  4496. struct dentry *old_parent = NULL;
  4497. struct inode *orig_inode = inode;
  4498. /*
  4499. * for regular files, if its inode is already on disk, we don't
  4500. * have to worry about the parents at all. This is because
  4501. * we can use the last_unlink_trans field to record renames
  4502. * and other fun in this file.
  4503. */
  4504. if (S_ISREG(inode->i_mode) &&
  4505. BTRFS_I(inode)->generation <= last_committed &&
  4506. BTRFS_I(inode)->last_unlink_trans <= last_committed)
  4507. goto out;
  4508. if (!S_ISDIR(inode->i_mode)) {
  4509. if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
  4510. goto out;
  4511. inode = d_inode(parent);
  4512. }
  4513. while (1) {
  4514. /*
  4515. * If we are logging a directory then we start with our inode,
  4516. * not our parent's inode, so we need to skip setting the
  4517. * logged_trans so that further down in the log code we don't
  4518. * think this inode has already been logged.
  4519. */
  4520. if (inode != orig_inode)
  4521. BTRFS_I(inode)->logged_trans = trans->transid;
  4522. smp_mb();
  4523. if (btrfs_must_commit_transaction(trans, inode)) {
  4524. ret = 1;
  4525. break;
  4526. }
  4527. if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
  4528. break;
  4529. if (IS_ROOT(parent)) {
  4530. inode = d_inode(parent);
  4531. if (btrfs_must_commit_transaction(trans, inode))
  4532. ret = 1;
  4533. break;
  4534. }
  4535. parent = dget_parent(parent);
  4536. dput(old_parent);
  4537. old_parent = parent;
  4538. inode = d_inode(parent);
  4539. }
  4540. dput(old_parent);
  4541. out:
  4542. return ret;
  4543. }
  4544. struct btrfs_dir_list {
  4545. u64 ino;
  4546. struct list_head list;
  4547. };
  4548. /*
  4549. * Log the inodes of the new dentries of a directory. See log_dir_items() for
  4550. * details about the why it is needed.
  4551. * This is a recursive operation - if an existing dentry corresponds to a
  4552. * directory, that directory's new entries are logged too (same behaviour as
  4553. * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
  4554. * the dentries point to we do not lock their i_mutex, otherwise lockdep
  4555. * complains about the following circular lock dependency / possible deadlock:
  4556. *
  4557. * CPU0 CPU1
  4558. * ---- ----
  4559. * lock(&type->i_mutex_dir_key#3/2);
  4560. * lock(sb_internal#2);
  4561. * lock(&type->i_mutex_dir_key#3/2);
  4562. * lock(&sb->s_type->i_mutex_key#14);
  4563. *
  4564. * Where sb_internal is the lock (a counter that works as a lock) acquired by
  4565. * sb_start_intwrite() in btrfs_start_transaction().
  4566. * Not locking i_mutex of the inodes is still safe because:
  4567. *
  4568. * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
  4569. * that while logging the inode new references (names) are added or removed
  4570. * from the inode, leaving the logged inode item with a link count that does
  4571. * not match the number of logged inode reference items. This is fine because
  4572. * at log replay time we compute the real number of links and correct the
  4573. * link count in the inode item (see replay_one_buffer() and
  4574. * link_to_fixup_dir());
  4575. *
  4576. * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
  4577. * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
  4578. * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
  4579. * has a size that doesn't match the sum of the lengths of all the logged
  4580. * names. This does not result in a problem because if a dir_item key is
  4581. * logged but its matching dir_index key is not logged, at log replay time we
  4582. * don't use it to replay the respective name (see replay_one_name()). On the
  4583. * other hand if only the dir_index key ends up being logged, the respective
  4584. * name is added to the fs/subvol tree with both the dir_item and dir_index
  4585. * keys created (see replay_one_name()).
  4586. * The directory's inode item with a wrong i_size is not a problem as well,
  4587. * since we don't use it at log replay time to set the i_size in the inode
  4588. * item of the fs/subvol tree (see overwrite_item()).
  4589. */
  4590. static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
  4591. struct btrfs_root *root,
  4592. struct inode *start_inode,
  4593. struct btrfs_log_ctx *ctx)
  4594. {
  4595. struct btrfs_root *log = root->log_root;
  4596. struct btrfs_path *path;
  4597. LIST_HEAD(dir_list);
  4598. struct btrfs_dir_list *dir_elem;
  4599. int ret = 0;
  4600. path = btrfs_alloc_path();
  4601. if (!path)
  4602. return -ENOMEM;
  4603. dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
  4604. if (!dir_elem) {
  4605. btrfs_free_path(path);
  4606. return -ENOMEM;
  4607. }
  4608. dir_elem->ino = btrfs_ino(start_inode);
  4609. list_add_tail(&dir_elem->list, &dir_list);
  4610. while (!list_empty(&dir_list)) {
  4611. struct extent_buffer *leaf;
  4612. struct btrfs_key min_key;
  4613. int nritems;
  4614. int i;
  4615. dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
  4616. list);
  4617. if (ret)
  4618. goto next_dir_inode;
  4619. min_key.objectid = dir_elem->ino;
  4620. min_key.type = BTRFS_DIR_ITEM_KEY;
  4621. min_key.offset = 0;
  4622. again:
  4623. btrfs_release_path(path);
  4624. ret = btrfs_search_forward(log, &min_key, path, trans->transid);
  4625. if (ret < 0) {
  4626. goto next_dir_inode;
  4627. } else if (ret > 0) {
  4628. ret = 0;
  4629. goto next_dir_inode;
  4630. }
  4631. process_leaf:
  4632. leaf = path->nodes[0];
  4633. nritems = btrfs_header_nritems(leaf);
  4634. for (i = path->slots[0]; i < nritems; i++) {
  4635. struct btrfs_dir_item *di;
  4636. struct btrfs_key di_key;
  4637. struct inode *di_inode;
  4638. struct btrfs_dir_list *new_dir_elem;
  4639. int log_mode = LOG_INODE_EXISTS;
  4640. int type;
  4641. btrfs_item_key_to_cpu(leaf, &min_key, i);
  4642. if (min_key.objectid != dir_elem->ino ||
  4643. min_key.type != BTRFS_DIR_ITEM_KEY)
  4644. goto next_dir_inode;
  4645. di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
  4646. type = btrfs_dir_type(leaf, di);
  4647. if (btrfs_dir_transid(leaf, di) < trans->transid &&
  4648. type != BTRFS_FT_DIR)
  4649. continue;
  4650. btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
  4651. if (di_key.type == BTRFS_ROOT_ITEM_KEY)
  4652. continue;
  4653. di_inode = btrfs_iget(root->fs_info->sb, &di_key,
  4654. root, NULL);
  4655. if (IS_ERR(di_inode)) {
  4656. ret = PTR_ERR(di_inode);
  4657. goto next_dir_inode;
  4658. }
  4659. if (btrfs_inode_in_log(di_inode, trans->transid)) {
  4660. iput(di_inode);
  4661. continue;
  4662. }
  4663. ctx->log_new_dentries = false;
  4664. if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
  4665. log_mode = LOG_INODE_ALL;
  4666. btrfs_release_path(path);
  4667. ret = btrfs_log_inode(trans, root, di_inode,
  4668. log_mode, 0, LLONG_MAX, ctx);
  4669. if (!ret &&
  4670. btrfs_must_commit_transaction(trans, di_inode))
  4671. ret = 1;
  4672. iput(di_inode);
  4673. if (ret)
  4674. goto next_dir_inode;
  4675. if (ctx->log_new_dentries) {
  4676. new_dir_elem = kmalloc(sizeof(*new_dir_elem),
  4677. GFP_NOFS);
  4678. if (!new_dir_elem) {
  4679. ret = -ENOMEM;
  4680. goto next_dir_inode;
  4681. }
  4682. new_dir_elem->ino = di_key.objectid;
  4683. list_add_tail(&new_dir_elem->list, &dir_list);
  4684. }
  4685. break;
  4686. }
  4687. if (i == nritems) {
  4688. ret = btrfs_next_leaf(log, path);
  4689. if (ret < 0) {
  4690. goto next_dir_inode;
  4691. } else if (ret > 0) {
  4692. ret = 0;
  4693. goto next_dir_inode;
  4694. }
  4695. goto process_leaf;
  4696. }
  4697. if (min_key.offset < (u64)-1) {
  4698. min_key.offset++;
  4699. goto again;
  4700. }
  4701. next_dir_inode:
  4702. list_del(&dir_elem->list);
  4703. kfree(dir_elem);
  4704. }
  4705. btrfs_free_path(path);
  4706. return ret;
  4707. }
  4708. static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
  4709. struct inode *inode,
  4710. struct btrfs_log_ctx *ctx)
  4711. {
  4712. int ret;
  4713. struct btrfs_path *path;
  4714. struct btrfs_key key;
  4715. struct btrfs_root *root = BTRFS_I(inode)->root;
  4716. const u64 ino = btrfs_ino(inode);
  4717. path = btrfs_alloc_path();
  4718. if (!path)
  4719. return -ENOMEM;
  4720. path->skip_locking = 1;
  4721. path->search_commit_root = 1;
  4722. key.objectid = ino;
  4723. key.type = BTRFS_INODE_REF_KEY;
  4724. key.offset = 0;
  4725. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4726. if (ret < 0)
  4727. goto out;
  4728. while (true) {
  4729. struct extent_buffer *leaf = path->nodes[0];
  4730. int slot = path->slots[0];
  4731. u32 cur_offset = 0;
  4732. u32 item_size;
  4733. unsigned long ptr;
  4734. if (slot >= btrfs_header_nritems(leaf)) {
  4735. ret = btrfs_next_leaf(root, path);
  4736. if (ret < 0)
  4737. goto out;
  4738. else if (ret > 0)
  4739. break;
  4740. continue;
  4741. }
  4742. btrfs_item_key_to_cpu(leaf, &key, slot);
  4743. /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
  4744. if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
  4745. break;
  4746. item_size = btrfs_item_size_nr(leaf, slot);
  4747. ptr = btrfs_item_ptr_offset(leaf, slot);
  4748. while (cur_offset < item_size) {
  4749. struct btrfs_key inode_key;
  4750. struct inode *dir_inode;
  4751. inode_key.type = BTRFS_INODE_ITEM_KEY;
  4752. inode_key.offset = 0;
  4753. if (key.type == BTRFS_INODE_EXTREF_KEY) {
  4754. struct btrfs_inode_extref *extref;
  4755. extref = (struct btrfs_inode_extref *)
  4756. (ptr + cur_offset);
  4757. inode_key.objectid = btrfs_inode_extref_parent(
  4758. leaf, extref);
  4759. cur_offset += sizeof(*extref);
  4760. cur_offset += btrfs_inode_extref_name_len(leaf,
  4761. extref);
  4762. } else {
  4763. inode_key.objectid = key.offset;
  4764. cur_offset = item_size;
  4765. }
  4766. dir_inode = btrfs_iget(root->fs_info->sb, &inode_key,
  4767. root, NULL);
  4768. /* If parent inode was deleted, skip it. */
  4769. if (IS_ERR(dir_inode))
  4770. continue;
  4771. if (ctx)
  4772. ctx->log_new_dentries = false;
  4773. ret = btrfs_log_inode(trans, root, dir_inode,
  4774. LOG_INODE_ALL, 0, LLONG_MAX, ctx);
  4775. if (!ret &&
  4776. btrfs_must_commit_transaction(trans, dir_inode))
  4777. ret = 1;
  4778. if (!ret && ctx && ctx->log_new_dentries)
  4779. ret = log_new_dir_dentries(trans, root,
  4780. dir_inode, ctx);
  4781. iput(dir_inode);
  4782. if (ret)
  4783. goto out;
  4784. }
  4785. path->slots[0]++;
  4786. }
  4787. ret = 0;
  4788. out:
  4789. btrfs_free_path(path);
  4790. return ret;
  4791. }
  4792. /*
  4793. * helper function around btrfs_log_inode to make sure newly created
  4794. * parent directories also end up in the log. A minimal inode and backref
  4795. * only logging is done of any parent directories that are older than
  4796. * the last committed transaction
  4797. */
  4798. static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
  4799. struct btrfs_root *root, struct inode *inode,
  4800. struct dentry *parent,
  4801. const loff_t start,
  4802. const loff_t end,
  4803. int exists_only,
  4804. struct btrfs_log_ctx *ctx)
  4805. {
  4806. int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
  4807. struct super_block *sb;
  4808. struct dentry *old_parent = NULL;
  4809. int ret = 0;
  4810. u64 last_committed = root->fs_info->last_trans_committed;
  4811. bool log_dentries = false;
  4812. struct inode *orig_inode = inode;
  4813. sb = inode->i_sb;
  4814. if (btrfs_test_opt(root->fs_info, NOTREELOG)) {
  4815. ret = 1;
  4816. goto end_no_trans;
  4817. }
  4818. /*
  4819. * The prev transaction commit doesn't complete, we need do
  4820. * full commit by ourselves.
  4821. */
  4822. if (root->fs_info->last_trans_log_full_commit >
  4823. root->fs_info->last_trans_committed) {
  4824. ret = 1;
  4825. goto end_no_trans;
  4826. }
  4827. if (root != BTRFS_I(inode)->root ||
  4828. btrfs_root_refs(&root->root_item) == 0) {
  4829. ret = 1;
  4830. goto end_no_trans;
  4831. }
  4832. ret = check_parent_dirs_for_sync(trans, inode, parent,
  4833. sb, last_committed);
  4834. if (ret)
  4835. goto end_no_trans;
  4836. if (btrfs_inode_in_log(inode, trans->transid)) {
  4837. ret = BTRFS_NO_LOG_SYNC;
  4838. goto end_no_trans;
  4839. }
  4840. ret = start_log_trans(trans, root, ctx);
  4841. if (ret)
  4842. goto end_no_trans;
  4843. ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx);
  4844. if (ret)
  4845. goto end_trans;
  4846. /*
  4847. * for regular files, if its inode is already on disk, we don't
  4848. * have to worry about the parents at all. This is because
  4849. * we can use the last_unlink_trans field to record renames
  4850. * and other fun in this file.
  4851. */
  4852. if (S_ISREG(inode->i_mode) &&
  4853. BTRFS_I(inode)->generation <= last_committed &&
  4854. BTRFS_I(inode)->last_unlink_trans <= last_committed) {
  4855. ret = 0;
  4856. goto end_trans;
  4857. }
  4858. if (S_ISDIR(inode->i_mode) && ctx && ctx->log_new_dentries)
  4859. log_dentries = true;
  4860. /*
  4861. * On unlink we must make sure all our current and old parent directory
  4862. * inodes are fully logged. This is to prevent leaving dangling
  4863. * directory index entries in directories that were our parents but are
  4864. * not anymore. Not doing this results in old parent directory being
  4865. * impossible to delete after log replay (rmdir will always fail with
  4866. * error -ENOTEMPTY).
  4867. *
  4868. * Example 1:
  4869. *
  4870. * mkdir testdir
  4871. * touch testdir/foo
  4872. * ln testdir/foo testdir/bar
  4873. * sync
  4874. * unlink testdir/bar
  4875. * xfs_io -c fsync testdir/foo
  4876. * <power failure>
  4877. * mount fs, triggers log replay
  4878. *
  4879. * If we don't log the parent directory (testdir), after log replay the
  4880. * directory still has an entry pointing to the file inode using the bar
  4881. * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
  4882. * the file inode has a link count of 1.
  4883. *
  4884. * Example 2:
  4885. *
  4886. * mkdir testdir
  4887. * touch foo
  4888. * ln foo testdir/foo2
  4889. * ln foo testdir/foo3
  4890. * sync
  4891. * unlink testdir/foo3
  4892. * xfs_io -c fsync foo
  4893. * <power failure>
  4894. * mount fs, triggers log replay
  4895. *
  4896. * Similar as the first example, after log replay the parent directory
  4897. * testdir still has an entry pointing to the inode file with name foo3
  4898. * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
  4899. * and has a link count of 2.
  4900. */
  4901. if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
  4902. ret = btrfs_log_all_parents(trans, orig_inode, ctx);
  4903. if (ret)
  4904. goto end_trans;
  4905. }
  4906. while (1) {
  4907. if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
  4908. break;
  4909. inode = d_inode(parent);
  4910. if (root != BTRFS_I(inode)->root)
  4911. break;
  4912. if (BTRFS_I(inode)->generation > last_committed) {
  4913. ret = btrfs_log_inode(trans, root, inode,
  4914. LOG_INODE_EXISTS,
  4915. 0, LLONG_MAX, ctx);
  4916. if (ret)
  4917. goto end_trans;
  4918. }
  4919. if (IS_ROOT(parent))
  4920. break;
  4921. parent = dget_parent(parent);
  4922. dput(old_parent);
  4923. old_parent = parent;
  4924. }
  4925. if (log_dentries)
  4926. ret = log_new_dir_dentries(trans, root, orig_inode, ctx);
  4927. else
  4928. ret = 0;
  4929. end_trans:
  4930. dput(old_parent);
  4931. if (ret < 0) {
  4932. btrfs_set_log_full_commit(root->fs_info, trans);
  4933. ret = 1;
  4934. }
  4935. if (ret)
  4936. btrfs_remove_log_ctx(root, ctx);
  4937. btrfs_end_log_trans(root);
  4938. end_no_trans:
  4939. return ret;
  4940. }
  4941. /*
  4942. * it is not safe to log dentry if the chunk root has added new
  4943. * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
  4944. * If this returns 1, you must commit the transaction to safely get your
  4945. * data on disk.
  4946. */
  4947. int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
  4948. struct btrfs_root *root, struct dentry *dentry,
  4949. const loff_t start,
  4950. const loff_t end,
  4951. struct btrfs_log_ctx *ctx)
  4952. {
  4953. struct dentry *parent = dget_parent(dentry);
  4954. int ret;
  4955. ret = btrfs_log_inode_parent(trans, root, d_inode(dentry), parent,
  4956. start, end, 0, ctx);
  4957. dput(parent);
  4958. return ret;
  4959. }
  4960. /*
  4961. * should be called during mount to recover any replay any log trees
  4962. * from the FS
  4963. */
  4964. int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
  4965. {
  4966. int ret;
  4967. struct btrfs_path *path;
  4968. struct btrfs_trans_handle *trans;
  4969. struct btrfs_key key;
  4970. struct btrfs_key found_key;
  4971. struct btrfs_key tmp_key;
  4972. struct btrfs_root *log;
  4973. struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
  4974. struct walk_control wc = {
  4975. .process_func = process_one_buffer,
  4976. .stage = 0,
  4977. };
  4978. path = btrfs_alloc_path();
  4979. if (!path)
  4980. return -ENOMEM;
  4981. set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
  4982. trans = btrfs_start_transaction(fs_info->tree_root, 0);
  4983. if (IS_ERR(trans)) {
  4984. ret = PTR_ERR(trans);
  4985. goto error;
  4986. }
  4987. wc.trans = trans;
  4988. wc.pin = 1;
  4989. ret = walk_log_tree(trans, log_root_tree, &wc);
  4990. if (ret) {
  4991. btrfs_handle_fs_error(fs_info, ret,
  4992. "Failed to pin buffers while recovering log root tree.");
  4993. goto error;
  4994. }
  4995. again:
  4996. key.objectid = BTRFS_TREE_LOG_OBJECTID;
  4997. key.offset = (u64)-1;
  4998. key.type = BTRFS_ROOT_ITEM_KEY;
  4999. while (1) {
  5000. ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
  5001. if (ret < 0) {
  5002. btrfs_handle_fs_error(fs_info, ret,
  5003. "Couldn't find tree log root.");
  5004. goto error;
  5005. }
  5006. if (ret > 0) {
  5007. if (path->slots[0] == 0)
  5008. break;
  5009. path->slots[0]--;
  5010. }
  5011. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  5012. path->slots[0]);
  5013. btrfs_release_path(path);
  5014. if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  5015. break;
  5016. log = btrfs_read_fs_root(log_root_tree, &found_key);
  5017. if (IS_ERR(log)) {
  5018. ret = PTR_ERR(log);
  5019. btrfs_handle_fs_error(fs_info, ret,
  5020. "Couldn't read tree log root.");
  5021. goto error;
  5022. }
  5023. tmp_key.objectid = found_key.offset;
  5024. tmp_key.type = BTRFS_ROOT_ITEM_KEY;
  5025. tmp_key.offset = (u64)-1;
  5026. wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
  5027. if (IS_ERR(wc.replay_dest)) {
  5028. ret = PTR_ERR(wc.replay_dest);
  5029. free_extent_buffer(log->node);
  5030. free_extent_buffer(log->commit_root);
  5031. kfree(log);
  5032. btrfs_handle_fs_error(fs_info, ret,
  5033. "Couldn't read target root for tree log recovery.");
  5034. goto error;
  5035. }
  5036. wc.replay_dest->log_root = log;
  5037. btrfs_record_root_in_trans(trans, wc.replay_dest);
  5038. ret = walk_log_tree(trans, log, &wc);
  5039. if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
  5040. ret = fixup_inode_link_counts(trans, wc.replay_dest,
  5041. path);
  5042. }
  5043. key.offset = found_key.offset - 1;
  5044. wc.replay_dest->log_root = NULL;
  5045. free_extent_buffer(log->node);
  5046. free_extent_buffer(log->commit_root);
  5047. kfree(log);
  5048. if (ret)
  5049. goto error;
  5050. if (found_key.offset == 0)
  5051. break;
  5052. }
  5053. btrfs_release_path(path);
  5054. /* step one is to pin it all, step two is to replay just inodes */
  5055. if (wc.pin) {
  5056. wc.pin = 0;
  5057. wc.process_func = replay_one_buffer;
  5058. wc.stage = LOG_WALK_REPLAY_INODES;
  5059. goto again;
  5060. }
  5061. /* step three is to replay everything */
  5062. if (wc.stage < LOG_WALK_REPLAY_ALL) {
  5063. wc.stage++;
  5064. goto again;
  5065. }
  5066. btrfs_free_path(path);
  5067. /* step 4: commit the transaction, which also unpins the blocks */
  5068. ret = btrfs_commit_transaction(trans, fs_info->tree_root);
  5069. if (ret)
  5070. return ret;
  5071. free_extent_buffer(log_root_tree->node);
  5072. log_root_tree->log_root = NULL;
  5073. clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
  5074. kfree(log_root_tree);
  5075. return 0;
  5076. error:
  5077. if (wc.trans)
  5078. btrfs_end_transaction(wc.trans, fs_info->tree_root);
  5079. btrfs_free_path(path);
  5080. return ret;
  5081. }
  5082. /*
  5083. * there are some corner cases where we want to force a full
  5084. * commit instead of allowing a directory to be logged.
  5085. *
  5086. * They revolve around files there were unlinked from the directory, and
  5087. * this function updates the parent directory so that a full commit is
  5088. * properly done if it is fsync'd later after the unlinks are done.
  5089. *
  5090. * Must be called before the unlink operations (updates to the subvolume tree,
  5091. * inodes, etc) are done.
  5092. */
  5093. void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
  5094. struct inode *dir, struct inode *inode,
  5095. int for_rename)
  5096. {
  5097. /*
  5098. * when we're logging a file, if it hasn't been renamed
  5099. * or unlinked, and its inode is fully committed on disk,
  5100. * we don't have to worry about walking up the directory chain
  5101. * to log its parents.
  5102. *
  5103. * So, we use the last_unlink_trans field to put this transid
  5104. * into the file. When the file is logged we check it and
  5105. * don't log the parents if the file is fully on disk.
  5106. */
  5107. mutex_lock(&BTRFS_I(inode)->log_mutex);
  5108. BTRFS_I(inode)->last_unlink_trans = trans->transid;
  5109. mutex_unlock(&BTRFS_I(inode)->log_mutex);
  5110. /*
  5111. * if this directory was already logged any new
  5112. * names for this file/dir will get recorded
  5113. */
  5114. smp_mb();
  5115. if (BTRFS_I(dir)->logged_trans == trans->transid)
  5116. return;
  5117. /*
  5118. * if the inode we're about to unlink was logged,
  5119. * the log will be properly updated for any new names
  5120. */
  5121. if (BTRFS_I(inode)->logged_trans == trans->transid)
  5122. return;
  5123. /*
  5124. * when renaming files across directories, if the directory
  5125. * there we're unlinking from gets fsync'd later on, there's
  5126. * no way to find the destination directory later and fsync it
  5127. * properly. So, we have to be conservative and force commits
  5128. * so the new name gets discovered.
  5129. */
  5130. if (for_rename)
  5131. goto record;
  5132. /* we can safely do the unlink without any special recording */
  5133. return;
  5134. record:
  5135. mutex_lock(&BTRFS_I(dir)->log_mutex);
  5136. BTRFS_I(dir)->last_unlink_trans = trans->transid;
  5137. mutex_unlock(&BTRFS_I(dir)->log_mutex);
  5138. }
  5139. /*
  5140. * Make sure that if someone attempts to fsync the parent directory of a deleted
  5141. * snapshot, it ends up triggering a transaction commit. This is to guarantee
  5142. * that after replaying the log tree of the parent directory's root we will not
  5143. * see the snapshot anymore and at log replay time we will not see any log tree
  5144. * corresponding to the deleted snapshot's root, which could lead to replaying
  5145. * it after replaying the log tree of the parent directory (which would replay
  5146. * the snapshot delete operation).
  5147. *
  5148. * Must be called before the actual snapshot destroy operation (updates to the
  5149. * parent root and tree of tree roots trees, etc) are done.
  5150. */
  5151. void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
  5152. struct inode *dir)
  5153. {
  5154. mutex_lock(&BTRFS_I(dir)->log_mutex);
  5155. BTRFS_I(dir)->last_unlink_trans = trans->transid;
  5156. mutex_unlock(&BTRFS_I(dir)->log_mutex);
  5157. }
  5158. /*
  5159. * Call this after adding a new name for a file and it will properly
  5160. * update the log to reflect the new name.
  5161. *
  5162. * It will return zero if all goes well, and it will return 1 if a
  5163. * full transaction commit is required.
  5164. */
  5165. int btrfs_log_new_name(struct btrfs_trans_handle *trans,
  5166. struct inode *inode, struct inode *old_dir,
  5167. struct dentry *parent)
  5168. {
  5169. struct btrfs_root * root = BTRFS_I(inode)->root;
  5170. /*
  5171. * this will force the logging code to walk the dentry chain
  5172. * up for the file
  5173. */
  5174. if (S_ISREG(inode->i_mode))
  5175. BTRFS_I(inode)->last_unlink_trans = trans->transid;
  5176. /*
  5177. * if this inode hasn't been logged and directory we're renaming it
  5178. * from hasn't been logged, we don't need to log it
  5179. */
  5180. if (BTRFS_I(inode)->logged_trans <=
  5181. root->fs_info->last_trans_committed &&
  5182. (!old_dir || BTRFS_I(old_dir)->logged_trans <=
  5183. root->fs_info->last_trans_committed))
  5184. return 0;
  5185. return btrfs_log_inode_parent(trans, root, inode, parent, 0,
  5186. LLONG_MAX, 1, NULL);
  5187. }