tree-log.c 155 KB

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