tree-log.c 141 KB

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