tree-log.c 153 KB

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