send.c 146 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255
  1. /*
  2. * Copyright (C) 2012 Alexander Block. 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/bsearch.h>
  19. #include <linux/fs.h>
  20. #include <linux/file.h>
  21. #include <linux/sort.h>
  22. #include <linux/mount.h>
  23. #include <linux/xattr.h>
  24. #include <linux/posix_acl_xattr.h>
  25. #include <linux/radix-tree.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/string.h>
  28. #include "send.h"
  29. #include "backref.h"
  30. #include "hash.h"
  31. #include "locking.h"
  32. #include "disk-io.h"
  33. #include "btrfs_inode.h"
  34. #include "transaction.h"
  35. static int g_verbose = 0;
  36. #define verbose_printk(...) if (g_verbose) printk(__VA_ARGS__)
  37. /*
  38. * A fs_path is a helper to dynamically build path names with unknown size.
  39. * It reallocates the internal buffer on demand.
  40. * It allows fast adding of path elements on the right side (normal path) and
  41. * fast adding to the left side (reversed path). A reversed path can also be
  42. * unreversed if needed.
  43. */
  44. struct fs_path {
  45. union {
  46. struct {
  47. char *start;
  48. char *end;
  49. char *buf;
  50. unsigned short buf_len:15;
  51. unsigned short reversed:1;
  52. char inline_buf[];
  53. };
  54. /*
  55. * Average path length does not exceed 200 bytes, we'll have
  56. * better packing in the slab and higher chance to satisfy
  57. * a allocation later during send.
  58. */
  59. char pad[256];
  60. };
  61. };
  62. #define FS_PATH_INLINE_SIZE \
  63. (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
  64. /* reused for each extent */
  65. struct clone_root {
  66. struct btrfs_root *root;
  67. u64 ino;
  68. u64 offset;
  69. u64 found_refs;
  70. };
  71. #define SEND_CTX_MAX_NAME_CACHE_SIZE 128
  72. #define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
  73. struct send_ctx {
  74. struct file *send_filp;
  75. loff_t send_off;
  76. char *send_buf;
  77. u32 send_size;
  78. u32 send_max_size;
  79. u64 total_send_size;
  80. u64 cmd_send_size[BTRFS_SEND_C_MAX + 1];
  81. u64 flags; /* 'flags' member of btrfs_ioctl_send_args is u64 */
  82. struct btrfs_root *send_root;
  83. struct btrfs_root *parent_root;
  84. struct clone_root *clone_roots;
  85. int clone_roots_cnt;
  86. /* current state of the compare_tree call */
  87. struct btrfs_path *left_path;
  88. struct btrfs_path *right_path;
  89. struct btrfs_key *cmp_key;
  90. /*
  91. * infos of the currently processed inode. In case of deleted inodes,
  92. * these are the values from the deleted inode.
  93. */
  94. u64 cur_ino;
  95. u64 cur_inode_gen;
  96. int cur_inode_new;
  97. int cur_inode_new_gen;
  98. int cur_inode_deleted;
  99. u64 cur_inode_size;
  100. u64 cur_inode_mode;
  101. u64 cur_inode_rdev;
  102. u64 cur_inode_last_extent;
  103. u64 send_progress;
  104. struct list_head new_refs;
  105. struct list_head deleted_refs;
  106. struct radix_tree_root name_cache;
  107. struct list_head name_cache_list;
  108. int name_cache_size;
  109. struct file_ra_state ra;
  110. char *read_buf;
  111. /*
  112. * We process inodes by their increasing order, so if before an
  113. * incremental send we reverse the parent/child relationship of
  114. * directories such that a directory with a lower inode number was
  115. * the parent of a directory with a higher inode number, and the one
  116. * becoming the new parent got renamed too, we can't rename/move the
  117. * directory with lower inode number when we finish processing it - we
  118. * must process the directory with higher inode number first, then
  119. * rename/move it and then rename/move the directory with lower inode
  120. * number. Example follows.
  121. *
  122. * Tree state when the first send was performed:
  123. *
  124. * .
  125. * |-- a (ino 257)
  126. * |-- b (ino 258)
  127. * |
  128. * |
  129. * |-- c (ino 259)
  130. * | |-- d (ino 260)
  131. * |
  132. * |-- c2 (ino 261)
  133. *
  134. * Tree state when the second (incremental) send is performed:
  135. *
  136. * .
  137. * |-- a (ino 257)
  138. * |-- b (ino 258)
  139. * |-- c2 (ino 261)
  140. * |-- d2 (ino 260)
  141. * |-- cc (ino 259)
  142. *
  143. * The sequence of steps that lead to the second state was:
  144. *
  145. * mv /a/b/c/d /a/b/c2/d2
  146. * mv /a/b/c /a/b/c2/d2/cc
  147. *
  148. * "c" has lower inode number, but we can't move it (2nd mv operation)
  149. * before we move "d", which has higher inode number.
  150. *
  151. * So we just memorize which move/rename operations must be performed
  152. * later when their respective parent is processed and moved/renamed.
  153. */
  154. /* Indexed by parent directory inode number. */
  155. struct rb_root pending_dir_moves;
  156. /*
  157. * Reverse index, indexed by the inode number of a directory that
  158. * is waiting for the move/rename of its immediate parent before its
  159. * own move/rename can be performed.
  160. */
  161. struct rb_root waiting_dir_moves;
  162. /*
  163. * A directory that is going to be rm'ed might have a child directory
  164. * which is in the pending directory moves index above. In this case,
  165. * the directory can only be removed after the move/rename of its child
  166. * is performed. Example:
  167. *
  168. * Parent snapshot:
  169. *
  170. * . (ino 256)
  171. * |-- a/ (ino 257)
  172. * |-- b/ (ino 258)
  173. * |-- c/ (ino 259)
  174. * | |-- x/ (ino 260)
  175. * |
  176. * |-- y/ (ino 261)
  177. *
  178. * Send snapshot:
  179. *
  180. * . (ino 256)
  181. * |-- a/ (ino 257)
  182. * |-- b/ (ino 258)
  183. * |-- YY/ (ino 261)
  184. * |-- x/ (ino 260)
  185. *
  186. * Sequence of steps that lead to the send snapshot:
  187. * rm -f /a/b/c/foo.txt
  188. * mv /a/b/y /a/b/YY
  189. * mv /a/b/c/x /a/b/YY
  190. * rmdir /a/b/c
  191. *
  192. * When the child is processed, its move/rename is delayed until its
  193. * parent is processed (as explained above), but all other operations
  194. * like update utimes, chown, chgrp, etc, are performed and the paths
  195. * that it uses for those operations must use the orphanized name of
  196. * its parent (the directory we're going to rm later), so we need to
  197. * memorize that name.
  198. *
  199. * Indexed by the inode number of the directory to be deleted.
  200. */
  201. struct rb_root orphan_dirs;
  202. };
  203. struct pending_dir_move {
  204. struct rb_node node;
  205. struct list_head list;
  206. u64 parent_ino;
  207. u64 ino;
  208. u64 gen;
  209. bool is_orphan;
  210. struct list_head update_refs;
  211. };
  212. struct waiting_dir_move {
  213. struct rb_node node;
  214. u64 ino;
  215. /*
  216. * There might be some directory that could not be removed because it
  217. * was waiting for this directory inode to be moved first. Therefore
  218. * after this directory is moved, we can try to rmdir the ino rmdir_ino.
  219. */
  220. u64 rmdir_ino;
  221. bool orphanized;
  222. };
  223. struct orphan_dir_info {
  224. struct rb_node node;
  225. u64 ino;
  226. u64 gen;
  227. };
  228. struct name_cache_entry {
  229. struct list_head list;
  230. /*
  231. * radix_tree has only 32bit entries but we need to handle 64bit inums.
  232. * We use the lower 32bit of the 64bit inum to store it in the tree. If
  233. * more then one inum would fall into the same entry, we use radix_list
  234. * to store the additional entries. radix_list is also used to store
  235. * entries where two entries have the same inum but different
  236. * generations.
  237. */
  238. struct list_head radix_list;
  239. u64 ino;
  240. u64 gen;
  241. u64 parent_ino;
  242. u64 parent_gen;
  243. int ret;
  244. int need_later_update;
  245. int name_len;
  246. char name[];
  247. };
  248. static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
  249. static struct waiting_dir_move *
  250. get_waiting_dir_move(struct send_ctx *sctx, u64 ino);
  251. static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino);
  252. static int need_send_hole(struct send_ctx *sctx)
  253. {
  254. return (sctx->parent_root && !sctx->cur_inode_new &&
  255. !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted &&
  256. S_ISREG(sctx->cur_inode_mode));
  257. }
  258. static void fs_path_reset(struct fs_path *p)
  259. {
  260. if (p->reversed) {
  261. p->start = p->buf + p->buf_len - 1;
  262. p->end = p->start;
  263. *p->start = 0;
  264. } else {
  265. p->start = p->buf;
  266. p->end = p->start;
  267. *p->start = 0;
  268. }
  269. }
  270. static struct fs_path *fs_path_alloc(void)
  271. {
  272. struct fs_path *p;
  273. p = kmalloc(sizeof(*p), GFP_NOFS);
  274. if (!p)
  275. return NULL;
  276. p->reversed = 0;
  277. p->buf = p->inline_buf;
  278. p->buf_len = FS_PATH_INLINE_SIZE;
  279. fs_path_reset(p);
  280. return p;
  281. }
  282. static struct fs_path *fs_path_alloc_reversed(void)
  283. {
  284. struct fs_path *p;
  285. p = fs_path_alloc();
  286. if (!p)
  287. return NULL;
  288. p->reversed = 1;
  289. fs_path_reset(p);
  290. return p;
  291. }
  292. static void fs_path_free(struct fs_path *p)
  293. {
  294. if (!p)
  295. return;
  296. if (p->buf != p->inline_buf)
  297. kfree(p->buf);
  298. kfree(p);
  299. }
  300. static int fs_path_len(struct fs_path *p)
  301. {
  302. return p->end - p->start;
  303. }
  304. static int fs_path_ensure_buf(struct fs_path *p, int len)
  305. {
  306. char *tmp_buf;
  307. int path_len;
  308. int old_buf_len;
  309. len++;
  310. if (p->buf_len >= len)
  311. return 0;
  312. if (len > PATH_MAX) {
  313. WARN_ON(1);
  314. return -ENOMEM;
  315. }
  316. path_len = p->end - p->start;
  317. old_buf_len = p->buf_len;
  318. /*
  319. * First time the inline_buf does not suffice
  320. */
  321. if (p->buf == p->inline_buf) {
  322. tmp_buf = kmalloc(len, GFP_NOFS);
  323. if (tmp_buf)
  324. memcpy(tmp_buf, p->buf, old_buf_len);
  325. } else {
  326. tmp_buf = krealloc(p->buf, len, GFP_NOFS);
  327. }
  328. if (!tmp_buf)
  329. return -ENOMEM;
  330. p->buf = tmp_buf;
  331. /*
  332. * The real size of the buffer is bigger, this will let the fast path
  333. * happen most of the time
  334. */
  335. p->buf_len = ksize(p->buf);
  336. if (p->reversed) {
  337. tmp_buf = p->buf + old_buf_len - path_len - 1;
  338. p->end = p->buf + p->buf_len - 1;
  339. p->start = p->end - path_len;
  340. memmove(p->start, tmp_buf, path_len + 1);
  341. } else {
  342. p->start = p->buf;
  343. p->end = p->start + path_len;
  344. }
  345. return 0;
  346. }
  347. static int fs_path_prepare_for_add(struct fs_path *p, int name_len,
  348. char **prepared)
  349. {
  350. int ret;
  351. int new_len;
  352. new_len = p->end - p->start + name_len;
  353. if (p->start != p->end)
  354. new_len++;
  355. ret = fs_path_ensure_buf(p, new_len);
  356. if (ret < 0)
  357. goto out;
  358. if (p->reversed) {
  359. if (p->start != p->end)
  360. *--p->start = '/';
  361. p->start -= name_len;
  362. *prepared = p->start;
  363. } else {
  364. if (p->start != p->end)
  365. *p->end++ = '/';
  366. *prepared = p->end;
  367. p->end += name_len;
  368. *p->end = 0;
  369. }
  370. out:
  371. return ret;
  372. }
  373. static int fs_path_add(struct fs_path *p, const char *name, int name_len)
  374. {
  375. int ret;
  376. char *prepared;
  377. ret = fs_path_prepare_for_add(p, name_len, &prepared);
  378. if (ret < 0)
  379. goto out;
  380. memcpy(prepared, name, name_len);
  381. out:
  382. return ret;
  383. }
  384. static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
  385. {
  386. int ret;
  387. char *prepared;
  388. ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared);
  389. if (ret < 0)
  390. goto out;
  391. memcpy(prepared, p2->start, p2->end - p2->start);
  392. out:
  393. return ret;
  394. }
  395. static int fs_path_add_from_extent_buffer(struct fs_path *p,
  396. struct extent_buffer *eb,
  397. unsigned long off, int len)
  398. {
  399. int ret;
  400. char *prepared;
  401. ret = fs_path_prepare_for_add(p, len, &prepared);
  402. if (ret < 0)
  403. goto out;
  404. read_extent_buffer(eb, prepared, off, len);
  405. out:
  406. return ret;
  407. }
  408. static int fs_path_copy(struct fs_path *p, struct fs_path *from)
  409. {
  410. int ret;
  411. p->reversed = from->reversed;
  412. fs_path_reset(p);
  413. ret = fs_path_add_path(p, from);
  414. return ret;
  415. }
  416. static void fs_path_unreverse(struct fs_path *p)
  417. {
  418. char *tmp;
  419. int len;
  420. if (!p->reversed)
  421. return;
  422. tmp = p->start;
  423. len = p->end - p->start;
  424. p->start = p->buf;
  425. p->end = p->start + len;
  426. memmove(p->start, tmp, len + 1);
  427. p->reversed = 0;
  428. }
  429. static struct btrfs_path *alloc_path_for_send(void)
  430. {
  431. struct btrfs_path *path;
  432. path = btrfs_alloc_path();
  433. if (!path)
  434. return NULL;
  435. path->search_commit_root = 1;
  436. path->skip_locking = 1;
  437. path->need_commit_sem = 1;
  438. return path;
  439. }
  440. static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
  441. {
  442. int ret;
  443. mm_segment_t old_fs;
  444. u32 pos = 0;
  445. old_fs = get_fs();
  446. set_fs(KERNEL_DS);
  447. while (pos < len) {
  448. ret = vfs_write(filp, (__force const char __user *)buf + pos,
  449. len - pos, off);
  450. /* TODO handle that correctly */
  451. /*if (ret == -ERESTARTSYS) {
  452. continue;
  453. }*/
  454. if (ret < 0)
  455. goto out;
  456. if (ret == 0) {
  457. ret = -EIO;
  458. goto out;
  459. }
  460. pos += ret;
  461. }
  462. ret = 0;
  463. out:
  464. set_fs(old_fs);
  465. return ret;
  466. }
  467. static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
  468. {
  469. struct btrfs_tlv_header *hdr;
  470. int total_len = sizeof(*hdr) + len;
  471. int left = sctx->send_max_size - sctx->send_size;
  472. if (unlikely(left < total_len))
  473. return -EOVERFLOW;
  474. hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
  475. hdr->tlv_type = cpu_to_le16(attr);
  476. hdr->tlv_len = cpu_to_le16(len);
  477. memcpy(hdr + 1, data, len);
  478. sctx->send_size += total_len;
  479. return 0;
  480. }
  481. #define TLV_PUT_DEFINE_INT(bits) \
  482. static int tlv_put_u##bits(struct send_ctx *sctx, \
  483. u##bits attr, u##bits value) \
  484. { \
  485. __le##bits __tmp = cpu_to_le##bits(value); \
  486. return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
  487. }
  488. TLV_PUT_DEFINE_INT(64)
  489. static int tlv_put_string(struct send_ctx *sctx, u16 attr,
  490. const char *str, int len)
  491. {
  492. if (len == -1)
  493. len = strlen(str);
  494. return tlv_put(sctx, attr, str, len);
  495. }
  496. static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
  497. const u8 *uuid)
  498. {
  499. return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
  500. }
  501. static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
  502. struct extent_buffer *eb,
  503. struct btrfs_timespec *ts)
  504. {
  505. struct btrfs_timespec bts;
  506. read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
  507. return tlv_put(sctx, attr, &bts, sizeof(bts));
  508. }
  509. #define TLV_PUT(sctx, attrtype, attrlen, data) \
  510. do { \
  511. ret = tlv_put(sctx, attrtype, attrlen, data); \
  512. if (ret < 0) \
  513. goto tlv_put_failure; \
  514. } while (0)
  515. #define TLV_PUT_INT(sctx, attrtype, bits, value) \
  516. do { \
  517. ret = tlv_put_u##bits(sctx, attrtype, value); \
  518. if (ret < 0) \
  519. goto tlv_put_failure; \
  520. } while (0)
  521. #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
  522. #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
  523. #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
  524. #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
  525. #define TLV_PUT_STRING(sctx, attrtype, str, len) \
  526. do { \
  527. ret = tlv_put_string(sctx, attrtype, str, len); \
  528. if (ret < 0) \
  529. goto tlv_put_failure; \
  530. } while (0)
  531. #define TLV_PUT_PATH(sctx, attrtype, p) \
  532. do { \
  533. ret = tlv_put_string(sctx, attrtype, p->start, \
  534. p->end - p->start); \
  535. if (ret < 0) \
  536. goto tlv_put_failure; \
  537. } while(0)
  538. #define TLV_PUT_UUID(sctx, attrtype, uuid) \
  539. do { \
  540. ret = tlv_put_uuid(sctx, attrtype, uuid); \
  541. if (ret < 0) \
  542. goto tlv_put_failure; \
  543. } while (0)
  544. #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
  545. do { \
  546. ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
  547. if (ret < 0) \
  548. goto tlv_put_failure; \
  549. } while (0)
  550. static int send_header(struct send_ctx *sctx)
  551. {
  552. struct btrfs_stream_header hdr;
  553. strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
  554. hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
  555. return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
  556. &sctx->send_off);
  557. }
  558. /*
  559. * For each command/item we want to send to userspace, we call this function.
  560. */
  561. static int begin_cmd(struct send_ctx *sctx, int cmd)
  562. {
  563. struct btrfs_cmd_header *hdr;
  564. if (WARN_ON(!sctx->send_buf))
  565. return -EINVAL;
  566. BUG_ON(sctx->send_size);
  567. sctx->send_size += sizeof(*hdr);
  568. hdr = (struct btrfs_cmd_header *)sctx->send_buf;
  569. hdr->cmd = cpu_to_le16(cmd);
  570. return 0;
  571. }
  572. static int send_cmd(struct send_ctx *sctx)
  573. {
  574. int ret;
  575. struct btrfs_cmd_header *hdr;
  576. u32 crc;
  577. hdr = (struct btrfs_cmd_header *)sctx->send_buf;
  578. hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
  579. hdr->crc = 0;
  580. crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
  581. hdr->crc = cpu_to_le32(crc);
  582. ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
  583. &sctx->send_off);
  584. sctx->total_send_size += sctx->send_size;
  585. sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
  586. sctx->send_size = 0;
  587. return ret;
  588. }
  589. /*
  590. * Sends a move instruction to user space
  591. */
  592. static int send_rename(struct send_ctx *sctx,
  593. struct fs_path *from, struct fs_path *to)
  594. {
  595. int ret;
  596. verbose_printk("btrfs: send_rename %s -> %s\n", from->start, to->start);
  597. ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
  598. if (ret < 0)
  599. goto out;
  600. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
  601. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
  602. ret = send_cmd(sctx);
  603. tlv_put_failure:
  604. out:
  605. return ret;
  606. }
  607. /*
  608. * Sends a link instruction to user space
  609. */
  610. static int send_link(struct send_ctx *sctx,
  611. struct fs_path *path, struct fs_path *lnk)
  612. {
  613. int ret;
  614. verbose_printk("btrfs: send_link %s -> %s\n", path->start, lnk->start);
  615. ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
  616. if (ret < 0)
  617. goto out;
  618. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
  619. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
  620. ret = send_cmd(sctx);
  621. tlv_put_failure:
  622. out:
  623. return ret;
  624. }
  625. /*
  626. * Sends an unlink instruction to user space
  627. */
  628. static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
  629. {
  630. int ret;
  631. verbose_printk("btrfs: send_unlink %s\n", path->start);
  632. ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
  633. if (ret < 0)
  634. goto out;
  635. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
  636. ret = send_cmd(sctx);
  637. tlv_put_failure:
  638. out:
  639. return ret;
  640. }
  641. /*
  642. * Sends a rmdir instruction to user space
  643. */
  644. static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
  645. {
  646. int ret;
  647. verbose_printk("btrfs: send_rmdir %s\n", path->start);
  648. ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
  649. if (ret < 0)
  650. goto out;
  651. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
  652. ret = send_cmd(sctx);
  653. tlv_put_failure:
  654. out:
  655. return ret;
  656. }
  657. /*
  658. * Helper function to retrieve some fields from an inode item.
  659. */
  660. static int __get_inode_info(struct btrfs_root *root, struct btrfs_path *path,
  661. u64 ino, u64 *size, u64 *gen, u64 *mode, u64 *uid,
  662. u64 *gid, u64 *rdev)
  663. {
  664. int ret;
  665. struct btrfs_inode_item *ii;
  666. struct btrfs_key key;
  667. key.objectid = ino;
  668. key.type = BTRFS_INODE_ITEM_KEY;
  669. key.offset = 0;
  670. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  671. if (ret) {
  672. if (ret > 0)
  673. ret = -ENOENT;
  674. return ret;
  675. }
  676. ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
  677. struct btrfs_inode_item);
  678. if (size)
  679. *size = btrfs_inode_size(path->nodes[0], ii);
  680. if (gen)
  681. *gen = btrfs_inode_generation(path->nodes[0], ii);
  682. if (mode)
  683. *mode = btrfs_inode_mode(path->nodes[0], ii);
  684. if (uid)
  685. *uid = btrfs_inode_uid(path->nodes[0], ii);
  686. if (gid)
  687. *gid = btrfs_inode_gid(path->nodes[0], ii);
  688. if (rdev)
  689. *rdev = btrfs_inode_rdev(path->nodes[0], ii);
  690. return ret;
  691. }
  692. static int get_inode_info(struct btrfs_root *root,
  693. u64 ino, u64 *size, u64 *gen,
  694. u64 *mode, u64 *uid, u64 *gid,
  695. u64 *rdev)
  696. {
  697. struct btrfs_path *path;
  698. int ret;
  699. path = alloc_path_for_send();
  700. if (!path)
  701. return -ENOMEM;
  702. ret = __get_inode_info(root, path, ino, size, gen, mode, uid, gid,
  703. rdev);
  704. btrfs_free_path(path);
  705. return ret;
  706. }
  707. typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
  708. struct fs_path *p,
  709. void *ctx);
  710. /*
  711. * Helper function to iterate the entries in ONE btrfs_inode_ref or
  712. * btrfs_inode_extref.
  713. * The iterate callback may return a non zero value to stop iteration. This can
  714. * be a negative value for error codes or 1 to simply stop it.
  715. *
  716. * path must point to the INODE_REF or INODE_EXTREF when called.
  717. */
  718. static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
  719. struct btrfs_key *found_key, int resolve,
  720. iterate_inode_ref_t iterate, void *ctx)
  721. {
  722. struct extent_buffer *eb = path->nodes[0];
  723. struct btrfs_item *item;
  724. struct btrfs_inode_ref *iref;
  725. struct btrfs_inode_extref *extref;
  726. struct btrfs_path *tmp_path;
  727. struct fs_path *p;
  728. u32 cur = 0;
  729. u32 total;
  730. int slot = path->slots[0];
  731. u32 name_len;
  732. char *start;
  733. int ret = 0;
  734. int num = 0;
  735. int index;
  736. u64 dir;
  737. unsigned long name_off;
  738. unsigned long elem_size;
  739. unsigned long ptr;
  740. p = fs_path_alloc_reversed();
  741. if (!p)
  742. return -ENOMEM;
  743. tmp_path = alloc_path_for_send();
  744. if (!tmp_path) {
  745. fs_path_free(p);
  746. return -ENOMEM;
  747. }
  748. if (found_key->type == BTRFS_INODE_REF_KEY) {
  749. ptr = (unsigned long)btrfs_item_ptr(eb, slot,
  750. struct btrfs_inode_ref);
  751. item = btrfs_item_nr(slot);
  752. total = btrfs_item_size(eb, item);
  753. elem_size = sizeof(*iref);
  754. } else {
  755. ptr = btrfs_item_ptr_offset(eb, slot);
  756. total = btrfs_item_size_nr(eb, slot);
  757. elem_size = sizeof(*extref);
  758. }
  759. while (cur < total) {
  760. fs_path_reset(p);
  761. if (found_key->type == BTRFS_INODE_REF_KEY) {
  762. iref = (struct btrfs_inode_ref *)(ptr + cur);
  763. name_len = btrfs_inode_ref_name_len(eb, iref);
  764. name_off = (unsigned long)(iref + 1);
  765. index = btrfs_inode_ref_index(eb, iref);
  766. dir = found_key->offset;
  767. } else {
  768. extref = (struct btrfs_inode_extref *)(ptr + cur);
  769. name_len = btrfs_inode_extref_name_len(eb, extref);
  770. name_off = (unsigned long)&extref->name;
  771. index = btrfs_inode_extref_index(eb, extref);
  772. dir = btrfs_inode_extref_parent(eb, extref);
  773. }
  774. if (resolve) {
  775. start = btrfs_ref_to_path(root, tmp_path, name_len,
  776. name_off, eb, dir,
  777. p->buf, p->buf_len);
  778. if (IS_ERR(start)) {
  779. ret = PTR_ERR(start);
  780. goto out;
  781. }
  782. if (start < p->buf) {
  783. /* overflow , try again with larger buffer */
  784. ret = fs_path_ensure_buf(p,
  785. p->buf_len + p->buf - start);
  786. if (ret < 0)
  787. goto out;
  788. start = btrfs_ref_to_path(root, tmp_path,
  789. name_len, name_off,
  790. eb, dir,
  791. p->buf, p->buf_len);
  792. if (IS_ERR(start)) {
  793. ret = PTR_ERR(start);
  794. goto out;
  795. }
  796. BUG_ON(start < p->buf);
  797. }
  798. p->start = start;
  799. } else {
  800. ret = fs_path_add_from_extent_buffer(p, eb, name_off,
  801. name_len);
  802. if (ret < 0)
  803. goto out;
  804. }
  805. cur += elem_size + name_len;
  806. ret = iterate(num, dir, index, p, ctx);
  807. if (ret)
  808. goto out;
  809. num++;
  810. }
  811. out:
  812. btrfs_free_path(tmp_path);
  813. fs_path_free(p);
  814. return ret;
  815. }
  816. typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
  817. const char *name, int name_len,
  818. const char *data, int data_len,
  819. u8 type, void *ctx);
  820. /*
  821. * Helper function to iterate the entries in ONE btrfs_dir_item.
  822. * The iterate callback may return a non zero value to stop iteration. This can
  823. * be a negative value for error codes or 1 to simply stop it.
  824. *
  825. * path must point to the dir item when called.
  826. */
  827. static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
  828. struct btrfs_key *found_key,
  829. iterate_dir_item_t iterate, void *ctx)
  830. {
  831. int ret = 0;
  832. struct extent_buffer *eb;
  833. struct btrfs_item *item;
  834. struct btrfs_dir_item *di;
  835. struct btrfs_key di_key;
  836. char *buf = NULL;
  837. int buf_len;
  838. u32 name_len;
  839. u32 data_len;
  840. u32 cur;
  841. u32 len;
  842. u32 total;
  843. int slot;
  844. int num;
  845. u8 type;
  846. /*
  847. * Start with a small buffer (1 page). If later we end up needing more
  848. * space, which can happen for xattrs on a fs with a leaf size greater
  849. * then the page size, attempt to increase the buffer. Typically xattr
  850. * values are small.
  851. */
  852. buf_len = PATH_MAX;
  853. buf = kmalloc(buf_len, GFP_NOFS);
  854. if (!buf) {
  855. ret = -ENOMEM;
  856. goto out;
  857. }
  858. eb = path->nodes[0];
  859. slot = path->slots[0];
  860. item = btrfs_item_nr(slot);
  861. di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
  862. cur = 0;
  863. len = 0;
  864. total = btrfs_item_size(eb, item);
  865. num = 0;
  866. while (cur < total) {
  867. name_len = btrfs_dir_name_len(eb, di);
  868. data_len = btrfs_dir_data_len(eb, di);
  869. type = btrfs_dir_type(eb, di);
  870. btrfs_dir_item_key_to_cpu(eb, di, &di_key);
  871. if (type == BTRFS_FT_XATTR) {
  872. if (name_len > XATTR_NAME_MAX) {
  873. ret = -ENAMETOOLONG;
  874. goto out;
  875. }
  876. if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(root)) {
  877. ret = -E2BIG;
  878. goto out;
  879. }
  880. } else {
  881. /*
  882. * Path too long
  883. */
  884. if (name_len + data_len > PATH_MAX) {
  885. ret = -ENAMETOOLONG;
  886. goto out;
  887. }
  888. }
  889. if (name_len + data_len > buf_len) {
  890. buf_len = name_len + data_len;
  891. if (is_vmalloc_addr(buf)) {
  892. vfree(buf);
  893. buf = NULL;
  894. } else {
  895. char *tmp = krealloc(buf, buf_len,
  896. GFP_NOFS | __GFP_NOWARN);
  897. if (!tmp)
  898. kfree(buf);
  899. buf = tmp;
  900. }
  901. if (!buf) {
  902. buf = vmalloc(buf_len);
  903. if (!buf) {
  904. ret = -ENOMEM;
  905. goto out;
  906. }
  907. }
  908. }
  909. read_extent_buffer(eb, buf, (unsigned long)(di + 1),
  910. name_len + data_len);
  911. len = sizeof(*di) + name_len + data_len;
  912. di = (struct btrfs_dir_item *)((char *)di + len);
  913. cur += len;
  914. ret = iterate(num, &di_key, buf, name_len, buf + name_len,
  915. data_len, type, ctx);
  916. if (ret < 0)
  917. goto out;
  918. if (ret) {
  919. ret = 0;
  920. goto out;
  921. }
  922. num++;
  923. }
  924. out:
  925. kvfree(buf);
  926. return ret;
  927. }
  928. static int __copy_first_ref(int num, u64 dir, int index,
  929. struct fs_path *p, void *ctx)
  930. {
  931. int ret;
  932. struct fs_path *pt = ctx;
  933. ret = fs_path_copy(pt, p);
  934. if (ret < 0)
  935. return ret;
  936. /* we want the first only */
  937. return 1;
  938. }
  939. /*
  940. * Retrieve the first path of an inode. If an inode has more then one
  941. * ref/hardlink, this is ignored.
  942. */
  943. static int get_inode_path(struct btrfs_root *root,
  944. u64 ino, struct fs_path *path)
  945. {
  946. int ret;
  947. struct btrfs_key key, found_key;
  948. struct btrfs_path *p;
  949. p = alloc_path_for_send();
  950. if (!p)
  951. return -ENOMEM;
  952. fs_path_reset(path);
  953. key.objectid = ino;
  954. key.type = BTRFS_INODE_REF_KEY;
  955. key.offset = 0;
  956. ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
  957. if (ret < 0)
  958. goto out;
  959. if (ret) {
  960. ret = 1;
  961. goto out;
  962. }
  963. btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
  964. if (found_key.objectid != ino ||
  965. (found_key.type != BTRFS_INODE_REF_KEY &&
  966. found_key.type != BTRFS_INODE_EXTREF_KEY)) {
  967. ret = -ENOENT;
  968. goto out;
  969. }
  970. ret = iterate_inode_ref(root, p, &found_key, 1,
  971. __copy_first_ref, path);
  972. if (ret < 0)
  973. goto out;
  974. ret = 0;
  975. out:
  976. btrfs_free_path(p);
  977. return ret;
  978. }
  979. struct backref_ctx {
  980. struct send_ctx *sctx;
  981. struct btrfs_path *path;
  982. /* number of total found references */
  983. u64 found;
  984. /*
  985. * used for clones found in send_root. clones found behind cur_objectid
  986. * and cur_offset are not considered as allowed clones.
  987. */
  988. u64 cur_objectid;
  989. u64 cur_offset;
  990. /* may be truncated in case it's the last extent in a file */
  991. u64 extent_len;
  992. /* data offset in the file extent item */
  993. u64 data_offset;
  994. /* Just to check for bugs in backref resolving */
  995. int found_itself;
  996. };
  997. static int __clone_root_cmp_bsearch(const void *key, const void *elt)
  998. {
  999. u64 root = (u64)(uintptr_t)key;
  1000. struct clone_root *cr = (struct clone_root *)elt;
  1001. if (root < cr->root->objectid)
  1002. return -1;
  1003. if (root > cr->root->objectid)
  1004. return 1;
  1005. return 0;
  1006. }
  1007. static int __clone_root_cmp_sort(const void *e1, const void *e2)
  1008. {
  1009. struct clone_root *cr1 = (struct clone_root *)e1;
  1010. struct clone_root *cr2 = (struct clone_root *)e2;
  1011. if (cr1->root->objectid < cr2->root->objectid)
  1012. return -1;
  1013. if (cr1->root->objectid > cr2->root->objectid)
  1014. return 1;
  1015. return 0;
  1016. }
  1017. /*
  1018. * Called for every backref that is found for the current extent.
  1019. * Results are collected in sctx->clone_roots->ino/offset/found_refs
  1020. */
  1021. static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
  1022. {
  1023. struct backref_ctx *bctx = ctx_;
  1024. struct clone_root *found;
  1025. int ret;
  1026. u64 i_size;
  1027. /* First check if the root is in the list of accepted clone sources */
  1028. found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
  1029. bctx->sctx->clone_roots_cnt,
  1030. sizeof(struct clone_root),
  1031. __clone_root_cmp_bsearch);
  1032. if (!found)
  1033. return 0;
  1034. if (found->root == bctx->sctx->send_root &&
  1035. ino == bctx->cur_objectid &&
  1036. offset == bctx->cur_offset) {
  1037. bctx->found_itself = 1;
  1038. }
  1039. /*
  1040. * There are inodes that have extents that lie behind its i_size. Don't
  1041. * accept clones from these extents.
  1042. */
  1043. ret = __get_inode_info(found->root, bctx->path, ino, &i_size, NULL, NULL,
  1044. NULL, NULL, NULL);
  1045. btrfs_release_path(bctx->path);
  1046. if (ret < 0)
  1047. return ret;
  1048. if (offset + bctx->data_offset + bctx->extent_len > i_size)
  1049. return 0;
  1050. /*
  1051. * Make sure we don't consider clones from send_root that are
  1052. * behind the current inode/offset.
  1053. */
  1054. if (found->root == bctx->sctx->send_root) {
  1055. /*
  1056. * TODO for the moment we don't accept clones from the inode
  1057. * that is currently send. We may change this when
  1058. * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
  1059. * file.
  1060. */
  1061. if (ino >= bctx->cur_objectid)
  1062. return 0;
  1063. #if 0
  1064. if (ino > bctx->cur_objectid)
  1065. return 0;
  1066. if (offset + bctx->extent_len > bctx->cur_offset)
  1067. return 0;
  1068. #endif
  1069. }
  1070. bctx->found++;
  1071. found->found_refs++;
  1072. if (ino < found->ino) {
  1073. found->ino = ino;
  1074. found->offset = offset;
  1075. } else if (found->ino == ino) {
  1076. /*
  1077. * same extent found more then once in the same file.
  1078. */
  1079. if (found->offset > offset + bctx->extent_len)
  1080. found->offset = offset;
  1081. }
  1082. return 0;
  1083. }
  1084. /*
  1085. * Given an inode, offset and extent item, it finds a good clone for a clone
  1086. * instruction. Returns -ENOENT when none could be found. The function makes
  1087. * sure that the returned clone is usable at the point where sending is at the
  1088. * moment. This means, that no clones are accepted which lie behind the current
  1089. * inode+offset.
  1090. *
  1091. * path must point to the extent item when called.
  1092. */
  1093. static int find_extent_clone(struct send_ctx *sctx,
  1094. struct btrfs_path *path,
  1095. u64 ino, u64 data_offset,
  1096. u64 ino_size,
  1097. struct clone_root **found)
  1098. {
  1099. int ret;
  1100. int extent_type;
  1101. u64 logical;
  1102. u64 disk_byte;
  1103. u64 num_bytes;
  1104. u64 extent_item_pos;
  1105. u64 flags = 0;
  1106. struct btrfs_file_extent_item *fi;
  1107. struct extent_buffer *eb = path->nodes[0];
  1108. struct backref_ctx *backref_ctx = NULL;
  1109. struct clone_root *cur_clone_root;
  1110. struct btrfs_key found_key;
  1111. struct btrfs_path *tmp_path;
  1112. int compressed;
  1113. u32 i;
  1114. tmp_path = alloc_path_for_send();
  1115. if (!tmp_path)
  1116. return -ENOMEM;
  1117. /* We only use this path under the commit sem */
  1118. tmp_path->need_commit_sem = 0;
  1119. backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_NOFS);
  1120. if (!backref_ctx) {
  1121. ret = -ENOMEM;
  1122. goto out;
  1123. }
  1124. backref_ctx->path = tmp_path;
  1125. if (data_offset >= ino_size) {
  1126. /*
  1127. * There may be extents that lie behind the file's size.
  1128. * I at least had this in combination with snapshotting while
  1129. * writing large files.
  1130. */
  1131. ret = 0;
  1132. goto out;
  1133. }
  1134. fi = btrfs_item_ptr(eb, path->slots[0],
  1135. struct btrfs_file_extent_item);
  1136. extent_type = btrfs_file_extent_type(eb, fi);
  1137. if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  1138. ret = -ENOENT;
  1139. goto out;
  1140. }
  1141. compressed = btrfs_file_extent_compression(eb, fi);
  1142. num_bytes = btrfs_file_extent_num_bytes(eb, fi);
  1143. disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
  1144. if (disk_byte == 0) {
  1145. ret = -ENOENT;
  1146. goto out;
  1147. }
  1148. logical = disk_byte + btrfs_file_extent_offset(eb, fi);
  1149. down_read(&sctx->send_root->fs_info->commit_root_sem);
  1150. ret = extent_from_logical(sctx->send_root->fs_info, disk_byte, tmp_path,
  1151. &found_key, &flags);
  1152. up_read(&sctx->send_root->fs_info->commit_root_sem);
  1153. btrfs_release_path(tmp_path);
  1154. if (ret < 0)
  1155. goto out;
  1156. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  1157. ret = -EIO;
  1158. goto out;
  1159. }
  1160. /*
  1161. * Setup the clone roots.
  1162. */
  1163. for (i = 0; i < sctx->clone_roots_cnt; i++) {
  1164. cur_clone_root = sctx->clone_roots + i;
  1165. cur_clone_root->ino = (u64)-1;
  1166. cur_clone_root->offset = 0;
  1167. cur_clone_root->found_refs = 0;
  1168. }
  1169. backref_ctx->sctx = sctx;
  1170. backref_ctx->found = 0;
  1171. backref_ctx->cur_objectid = ino;
  1172. backref_ctx->cur_offset = data_offset;
  1173. backref_ctx->found_itself = 0;
  1174. backref_ctx->extent_len = num_bytes;
  1175. /*
  1176. * For non-compressed extents iterate_extent_inodes() gives us extent
  1177. * offsets that already take into account the data offset, but not for
  1178. * compressed extents, since the offset is logical and not relative to
  1179. * the physical extent locations. We must take this into account to
  1180. * avoid sending clone offsets that go beyond the source file's size,
  1181. * which would result in the clone ioctl failing with -EINVAL on the
  1182. * receiving end.
  1183. */
  1184. if (compressed == BTRFS_COMPRESS_NONE)
  1185. backref_ctx->data_offset = 0;
  1186. else
  1187. backref_ctx->data_offset = btrfs_file_extent_offset(eb, fi);
  1188. /*
  1189. * The last extent of a file may be too large due to page alignment.
  1190. * We need to adjust extent_len in this case so that the checks in
  1191. * __iterate_backrefs work.
  1192. */
  1193. if (data_offset + num_bytes >= ino_size)
  1194. backref_ctx->extent_len = ino_size - data_offset;
  1195. /*
  1196. * Now collect all backrefs.
  1197. */
  1198. if (compressed == BTRFS_COMPRESS_NONE)
  1199. extent_item_pos = logical - found_key.objectid;
  1200. else
  1201. extent_item_pos = 0;
  1202. ret = iterate_extent_inodes(sctx->send_root->fs_info,
  1203. found_key.objectid, extent_item_pos, 1,
  1204. __iterate_backrefs, backref_ctx);
  1205. if (ret < 0)
  1206. goto out;
  1207. if (!backref_ctx->found_itself) {
  1208. /* found a bug in backref code? */
  1209. ret = -EIO;
  1210. btrfs_err(sctx->send_root->fs_info, "did not find backref in "
  1211. "send_root. inode=%llu, offset=%llu, "
  1212. "disk_byte=%llu found extent=%llu",
  1213. ino, data_offset, disk_byte, found_key.objectid);
  1214. goto out;
  1215. }
  1216. verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, "
  1217. "ino=%llu, "
  1218. "num_bytes=%llu, logical=%llu\n",
  1219. data_offset, ino, num_bytes, logical);
  1220. if (!backref_ctx->found)
  1221. verbose_printk("btrfs: no clones found\n");
  1222. cur_clone_root = NULL;
  1223. for (i = 0; i < sctx->clone_roots_cnt; i++) {
  1224. if (sctx->clone_roots[i].found_refs) {
  1225. if (!cur_clone_root)
  1226. cur_clone_root = sctx->clone_roots + i;
  1227. else if (sctx->clone_roots[i].root == sctx->send_root)
  1228. /* prefer clones from send_root over others */
  1229. cur_clone_root = sctx->clone_roots + i;
  1230. }
  1231. }
  1232. if (cur_clone_root) {
  1233. *found = cur_clone_root;
  1234. ret = 0;
  1235. } else {
  1236. ret = -ENOENT;
  1237. }
  1238. out:
  1239. btrfs_free_path(tmp_path);
  1240. kfree(backref_ctx);
  1241. return ret;
  1242. }
  1243. static int read_symlink(struct btrfs_root *root,
  1244. u64 ino,
  1245. struct fs_path *dest)
  1246. {
  1247. int ret;
  1248. struct btrfs_path *path;
  1249. struct btrfs_key key;
  1250. struct btrfs_file_extent_item *ei;
  1251. u8 type;
  1252. u8 compression;
  1253. unsigned long off;
  1254. int len;
  1255. path = alloc_path_for_send();
  1256. if (!path)
  1257. return -ENOMEM;
  1258. key.objectid = ino;
  1259. key.type = BTRFS_EXTENT_DATA_KEY;
  1260. key.offset = 0;
  1261. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1262. if (ret < 0)
  1263. goto out;
  1264. if (ret) {
  1265. /*
  1266. * An empty symlink inode. Can happen in rare error paths when
  1267. * creating a symlink (transaction committed before the inode
  1268. * eviction handler removed the symlink inode items and a crash
  1269. * happened in between or the subvol was snapshoted in between).
  1270. * Print an informative message to dmesg/syslog so that the user
  1271. * can delete the symlink.
  1272. */
  1273. btrfs_err(root->fs_info,
  1274. "Found empty symlink inode %llu at root %llu",
  1275. ino, root->root_key.objectid);
  1276. ret = -EIO;
  1277. goto out;
  1278. }
  1279. ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1280. struct btrfs_file_extent_item);
  1281. type = btrfs_file_extent_type(path->nodes[0], ei);
  1282. compression = btrfs_file_extent_compression(path->nodes[0], ei);
  1283. BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
  1284. BUG_ON(compression);
  1285. off = btrfs_file_extent_inline_start(ei);
  1286. len = btrfs_file_extent_inline_len(path->nodes[0], path->slots[0], ei);
  1287. ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
  1288. out:
  1289. btrfs_free_path(path);
  1290. return ret;
  1291. }
  1292. /*
  1293. * Helper function to generate a file name that is unique in the root of
  1294. * send_root and parent_root. This is used to generate names for orphan inodes.
  1295. */
  1296. static int gen_unique_name(struct send_ctx *sctx,
  1297. u64 ino, u64 gen,
  1298. struct fs_path *dest)
  1299. {
  1300. int ret = 0;
  1301. struct btrfs_path *path;
  1302. struct btrfs_dir_item *di;
  1303. char tmp[64];
  1304. int len;
  1305. u64 idx = 0;
  1306. path = alloc_path_for_send();
  1307. if (!path)
  1308. return -ENOMEM;
  1309. while (1) {
  1310. len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
  1311. ino, gen, idx);
  1312. ASSERT(len < sizeof(tmp));
  1313. di = btrfs_lookup_dir_item(NULL, sctx->send_root,
  1314. path, BTRFS_FIRST_FREE_OBJECTID,
  1315. tmp, strlen(tmp), 0);
  1316. btrfs_release_path(path);
  1317. if (IS_ERR(di)) {
  1318. ret = PTR_ERR(di);
  1319. goto out;
  1320. }
  1321. if (di) {
  1322. /* not unique, try again */
  1323. idx++;
  1324. continue;
  1325. }
  1326. if (!sctx->parent_root) {
  1327. /* unique */
  1328. ret = 0;
  1329. break;
  1330. }
  1331. di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
  1332. path, BTRFS_FIRST_FREE_OBJECTID,
  1333. tmp, strlen(tmp), 0);
  1334. btrfs_release_path(path);
  1335. if (IS_ERR(di)) {
  1336. ret = PTR_ERR(di);
  1337. goto out;
  1338. }
  1339. if (di) {
  1340. /* not unique, try again */
  1341. idx++;
  1342. continue;
  1343. }
  1344. /* unique */
  1345. break;
  1346. }
  1347. ret = fs_path_add(dest, tmp, strlen(tmp));
  1348. out:
  1349. btrfs_free_path(path);
  1350. return ret;
  1351. }
  1352. enum inode_state {
  1353. inode_state_no_change,
  1354. inode_state_will_create,
  1355. inode_state_did_create,
  1356. inode_state_will_delete,
  1357. inode_state_did_delete,
  1358. };
  1359. static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
  1360. {
  1361. int ret;
  1362. int left_ret;
  1363. int right_ret;
  1364. u64 left_gen;
  1365. u64 right_gen;
  1366. ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
  1367. NULL, NULL);
  1368. if (ret < 0 && ret != -ENOENT)
  1369. goto out;
  1370. left_ret = ret;
  1371. if (!sctx->parent_root) {
  1372. right_ret = -ENOENT;
  1373. } else {
  1374. ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
  1375. NULL, NULL, NULL, NULL);
  1376. if (ret < 0 && ret != -ENOENT)
  1377. goto out;
  1378. right_ret = ret;
  1379. }
  1380. if (!left_ret && !right_ret) {
  1381. if (left_gen == gen && right_gen == gen) {
  1382. ret = inode_state_no_change;
  1383. } else if (left_gen == gen) {
  1384. if (ino < sctx->send_progress)
  1385. ret = inode_state_did_create;
  1386. else
  1387. ret = inode_state_will_create;
  1388. } else if (right_gen == gen) {
  1389. if (ino < sctx->send_progress)
  1390. ret = inode_state_did_delete;
  1391. else
  1392. ret = inode_state_will_delete;
  1393. } else {
  1394. ret = -ENOENT;
  1395. }
  1396. } else if (!left_ret) {
  1397. if (left_gen == gen) {
  1398. if (ino < sctx->send_progress)
  1399. ret = inode_state_did_create;
  1400. else
  1401. ret = inode_state_will_create;
  1402. } else {
  1403. ret = -ENOENT;
  1404. }
  1405. } else if (!right_ret) {
  1406. if (right_gen == gen) {
  1407. if (ino < sctx->send_progress)
  1408. ret = inode_state_did_delete;
  1409. else
  1410. ret = inode_state_will_delete;
  1411. } else {
  1412. ret = -ENOENT;
  1413. }
  1414. } else {
  1415. ret = -ENOENT;
  1416. }
  1417. out:
  1418. return ret;
  1419. }
  1420. static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
  1421. {
  1422. int ret;
  1423. if (ino == BTRFS_FIRST_FREE_OBJECTID)
  1424. return 1;
  1425. ret = get_cur_inode_state(sctx, ino, gen);
  1426. if (ret < 0)
  1427. goto out;
  1428. if (ret == inode_state_no_change ||
  1429. ret == inode_state_did_create ||
  1430. ret == inode_state_will_delete)
  1431. ret = 1;
  1432. else
  1433. ret = 0;
  1434. out:
  1435. return ret;
  1436. }
  1437. /*
  1438. * Helper function to lookup a dir item in a dir.
  1439. */
  1440. static int lookup_dir_item_inode(struct btrfs_root *root,
  1441. u64 dir, const char *name, int name_len,
  1442. u64 *found_inode,
  1443. u8 *found_type)
  1444. {
  1445. int ret = 0;
  1446. struct btrfs_dir_item *di;
  1447. struct btrfs_key key;
  1448. struct btrfs_path *path;
  1449. path = alloc_path_for_send();
  1450. if (!path)
  1451. return -ENOMEM;
  1452. di = btrfs_lookup_dir_item(NULL, root, path,
  1453. dir, name, name_len, 0);
  1454. if (!di) {
  1455. ret = -ENOENT;
  1456. goto out;
  1457. }
  1458. if (IS_ERR(di)) {
  1459. ret = PTR_ERR(di);
  1460. goto out;
  1461. }
  1462. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
  1463. if (key.type == BTRFS_ROOT_ITEM_KEY) {
  1464. ret = -ENOENT;
  1465. goto out;
  1466. }
  1467. *found_inode = key.objectid;
  1468. *found_type = btrfs_dir_type(path->nodes[0], di);
  1469. out:
  1470. btrfs_free_path(path);
  1471. return ret;
  1472. }
  1473. /*
  1474. * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
  1475. * generation of the parent dir and the name of the dir entry.
  1476. */
  1477. static int get_first_ref(struct btrfs_root *root, u64 ino,
  1478. u64 *dir, u64 *dir_gen, struct fs_path *name)
  1479. {
  1480. int ret;
  1481. struct btrfs_key key;
  1482. struct btrfs_key found_key;
  1483. struct btrfs_path *path;
  1484. int len;
  1485. u64 parent_dir;
  1486. path = alloc_path_for_send();
  1487. if (!path)
  1488. return -ENOMEM;
  1489. key.objectid = ino;
  1490. key.type = BTRFS_INODE_REF_KEY;
  1491. key.offset = 0;
  1492. ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
  1493. if (ret < 0)
  1494. goto out;
  1495. if (!ret)
  1496. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1497. path->slots[0]);
  1498. if (ret || found_key.objectid != ino ||
  1499. (found_key.type != BTRFS_INODE_REF_KEY &&
  1500. found_key.type != BTRFS_INODE_EXTREF_KEY)) {
  1501. ret = -ENOENT;
  1502. goto out;
  1503. }
  1504. if (found_key.type == BTRFS_INODE_REF_KEY) {
  1505. struct btrfs_inode_ref *iref;
  1506. iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1507. struct btrfs_inode_ref);
  1508. len = btrfs_inode_ref_name_len(path->nodes[0], iref);
  1509. ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
  1510. (unsigned long)(iref + 1),
  1511. len);
  1512. parent_dir = found_key.offset;
  1513. } else {
  1514. struct btrfs_inode_extref *extref;
  1515. extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1516. struct btrfs_inode_extref);
  1517. len = btrfs_inode_extref_name_len(path->nodes[0], extref);
  1518. ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
  1519. (unsigned long)&extref->name, len);
  1520. parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
  1521. }
  1522. if (ret < 0)
  1523. goto out;
  1524. btrfs_release_path(path);
  1525. if (dir_gen) {
  1526. ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL,
  1527. NULL, NULL, NULL);
  1528. if (ret < 0)
  1529. goto out;
  1530. }
  1531. *dir = parent_dir;
  1532. out:
  1533. btrfs_free_path(path);
  1534. return ret;
  1535. }
  1536. static int is_first_ref(struct btrfs_root *root,
  1537. u64 ino, u64 dir,
  1538. const char *name, int name_len)
  1539. {
  1540. int ret;
  1541. struct fs_path *tmp_name;
  1542. u64 tmp_dir;
  1543. tmp_name = fs_path_alloc();
  1544. if (!tmp_name)
  1545. return -ENOMEM;
  1546. ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name);
  1547. if (ret < 0)
  1548. goto out;
  1549. if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
  1550. ret = 0;
  1551. goto out;
  1552. }
  1553. ret = !memcmp(tmp_name->start, name, name_len);
  1554. out:
  1555. fs_path_free(tmp_name);
  1556. return ret;
  1557. }
  1558. /*
  1559. * Used by process_recorded_refs to determine if a new ref would overwrite an
  1560. * already existing ref. In case it detects an overwrite, it returns the
  1561. * inode/gen in who_ino/who_gen.
  1562. * When an overwrite is detected, process_recorded_refs does proper orphanizing
  1563. * to make sure later references to the overwritten inode are possible.
  1564. * Orphanizing is however only required for the first ref of an inode.
  1565. * process_recorded_refs does an additional is_first_ref check to see if
  1566. * orphanizing is really required.
  1567. */
  1568. static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
  1569. const char *name, int name_len,
  1570. u64 *who_ino, u64 *who_gen)
  1571. {
  1572. int ret = 0;
  1573. u64 gen;
  1574. u64 other_inode = 0;
  1575. u8 other_type = 0;
  1576. if (!sctx->parent_root)
  1577. goto out;
  1578. ret = is_inode_existent(sctx, dir, dir_gen);
  1579. if (ret <= 0)
  1580. goto out;
  1581. /*
  1582. * If we have a parent root we need to verify that the parent dir was
  1583. * not delted and then re-created, if it was then we have no overwrite
  1584. * and we can just unlink this entry.
  1585. */
  1586. if (sctx->parent_root && dir != BTRFS_FIRST_FREE_OBJECTID) {
  1587. ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
  1588. NULL, NULL, NULL);
  1589. if (ret < 0 && ret != -ENOENT)
  1590. goto out;
  1591. if (ret) {
  1592. ret = 0;
  1593. goto out;
  1594. }
  1595. if (gen != dir_gen)
  1596. goto out;
  1597. }
  1598. ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
  1599. &other_inode, &other_type);
  1600. if (ret < 0 && ret != -ENOENT)
  1601. goto out;
  1602. if (ret) {
  1603. ret = 0;
  1604. goto out;
  1605. }
  1606. /*
  1607. * Check if the overwritten ref was already processed. If yes, the ref
  1608. * was already unlinked/moved, so we can safely assume that we will not
  1609. * overwrite anything at this point in time.
  1610. */
  1611. if (other_inode > sctx->send_progress) {
  1612. ret = get_inode_info(sctx->parent_root, other_inode, NULL,
  1613. who_gen, NULL, NULL, NULL, NULL);
  1614. if (ret < 0)
  1615. goto out;
  1616. ret = 1;
  1617. *who_ino = other_inode;
  1618. } else {
  1619. ret = 0;
  1620. }
  1621. out:
  1622. return ret;
  1623. }
  1624. /*
  1625. * Checks if the ref was overwritten by an already processed inode. This is
  1626. * used by __get_cur_name_and_parent to find out if the ref was orphanized and
  1627. * thus the orphan name needs be used.
  1628. * process_recorded_refs also uses it to avoid unlinking of refs that were
  1629. * overwritten.
  1630. */
  1631. static int did_overwrite_ref(struct send_ctx *sctx,
  1632. u64 dir, u64 dir_gen,
  1633. u64 ino, u64 ino_gen,
  1634. const char *name, int name_len)
  1635. {
  1636. int ret = 0;
  1637. u64 gen;
  1638. u64 ow_inode;
  1639. u8 other_type;
  1640. if (!sctx->parent_root)
  1641. goto out;
  1642. ret = is_inode_existent(sctx, dir, dir_gen);
  1643. if (ret <= 0)
  1644. goto out;
  1645. /* check if the ref was overwritten by another ref */
  1646. ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
  1647. &ow_inode, &other_type);
  1648. if (ret < 0 && ret != -ENOENT)
  1649. goto out;
  1650. if (ret) {
  1651. /* was never and will never be overwritten */
  1652. ret = 0;
  1653. goto out;
  1654. }
  1655. ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
  1656. NULL, NULL);
  1657. if (ret < 0)
  1658. goto out;
  1659. if (ow_inode == ino && gen == ino_gen) {
  1660. ret = 0;
  1661. goto out;
  1662. }
  1663. /*
  1664. * We know that it is or will be overwritten. Check this now.
  1665. * The current inode being processed might have been the one that caused
  1666. * inode 'ino' to be orphanized, therefore check if ow_inode matches
  1667. * the current inode being processed.
  1668. */
  1669. if ((ow_inode < sctx->send_progress) ||
  1670. (ino != sctx->cur_ino && ow_inode == sctx->cur_ino &&
  1671. gen == sctx->cur_inode_gen))
  1672. ret = 1;
  1673. else
  1674. ret = 0;
  1675. out:
  1676. return ret;
  1677. }
  1678. /*
  1679. * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
  1680. * that got overwritten. This is used by process_recorded_refs to determine
  1681. * if it has to use the path as returned by get_cur_path or the orphan name.
  1682. */
  1683. static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
  1684. {
  1685. int ret = 0;
  1686. struct fs_path *name = NULL;
  1687. u64 dir;
  1688. u64 dir_gen;
  1689. if (!sctx->parent_root)
  1690. goto out;
  1691. name = fs_path_alloc();
  1692. if (!name)
  1693. return -ENOMEM;
  1694. ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
  1695. if (ret < 0)
  1696. goto out;
  1697. ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
  1698. name->start, fs_path_len(name));
  1699. out:
  1700. fs_path_free(name);
  1701. return ret;
  1702. }
  1703. /*
  1704. * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
  1705. * so we need to do some special handling in case we have clashes. This function
  1706. * takes care of this with the help of name_cache_entry::radix_list.
  1707. * In case of error, nce is kfreed.
  1708. */
  1709. static int name_cache_insert(struct send_ctx *sctx,
  1710. struct name_cache_entry *nce)
  1711. {
  1712. int ret = 0;
  1713. struct list_head *nce_head;
  1714. nce_head = radix_tree_lookup(&sctx->name_cache,
  1715. (unsigned long)nce->ino);
  1716. if (!nce_head) {
  1717. nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS);
  1718. if (!nce_head) {
  1719. kfree(nce);
  1720. return -ENOMEM;
  1721. }
  1722. INIT_LIST_HEAD(nce_head);
  1723. ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
  1724. if (ret < 0) {
  1725. kfree(nce_head);
  1726. kfree(nce);
  1727. return ret;
  1728. }
  1729. }
  1730. list_add_tail(&nce->radix_list, nce_head);
  1731. list_add_tail(&nce->list, &sctx->name_cache_list);
  1732. sctx->name_cache_size++;
  1733. return ret;
  1734. }
  1735. static void name_cache_delete(struct send_ctx *sctx,
  1736. struct name_cache_entry *nce)
  1737. {
  1738. struct list_head *nce_head;
  1739. nce_head = radix_tree_lookup(&sctx->name_cache,
  1740. (unsigned long)nce->ino);
  1741. if (!nce_head) {
  1742. btrfs_err(sctx->send_root->fs_info,
  1743. "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
  1744. nce->ino, sctx->name_cache_size);
  1745. }
  1746. list_del(&nce->radix_list);
  1747. list_del(&nce->list);
  1748. sctx->name_cache_size--;
  1749. /*
  1750. * We may not get to the final release of nce_head if the lookup fails
  1751. */
  1752. if (nce_head && list_empty(nce_head)) {
  1753. radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
  1754. kfree(nce_head);
  1755. }
  1756. }
  1757. static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
  1758. u64 ino, u64 gen)
  1759. {
  1760. struct list_head *nce_head;
  1761. struct name_cache_entry *cur;
  1762. nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
  1763. if (!nce_head)
  1764. return NULL;
  1765. list_for_each_entry(cur, nce_head, radix_list) {
  1766. if (cur->ino == ino && cur->gen == gen)
  1767. return cur;
  1768. }
  1769. return NULL;
  1770. }
  1771. /*
  1772. * Removes the entry from the list and adds it back to the end. This marks the
  1773. * entry as recently used so that name_cache_clean_unused does not remove it.
  1774. */
  1775. static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
  1776. {
  1777. list_del(&nce->list);
  1778. list_add_tail(&nce->list, &sctx->name_cache_list);
  1779. }
  1780. /*
  1781. * Remove some entries from the beginning of name_cache_list.
  1782. */
  1783. static void name_cache_clean_unused(struct send_ctx *sctx)
  1784. {
  1785. struct name_cache_entry *nce;
  1786. if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
  1787. return;
  1788. while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
  1789. nce = list_entry(sctx->name_cache_list.next,
  1790. struct name_cache_entry, list);
  1791. name_cache_delete(sctx, nce);
  1792. kfree(nce);
  1793. }
  1794. }
  1795. static void name_cache_free(struct send_ctx *sctx)
  1796. {
  1797. struct name_cache_entry *nce;
  1798. while (!list_empty(&sctx->name_cache_list)) {
  1799. nce = list_entry(sctx->name_cache_list.next,
  1800. struct name_cache_entry, list);
  1801. name_cache_delete(sctx, nce);
  1802. kfree(nce);
  1803. }
  1804. }
  1805. /*
  1806. * Used by get_cur_path for each ref up to the root.
  1807. * Returns 0 if it succeeded.
  1808. * Returns 1 if the inode is not existent or got overwritten. In that case, the
  1809. * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
  1810. * is returned, parent_ino/parent_gen are not guaranteed to be valid.
  1811. * Returns <0 in case of error.
  1812. */
  1813. static int __get_cur_name_and_parent(struct send_ctx *sctx,
  1814. u64 ino, u64 gen,
  1815. u64 *parent_ino,
  1816. u64 *parent_gen,
  1817. struct fs_path *dest)
  1818. {
  1819. int ret;
  1820. int nce_ret;
  1821. struct name_cache_entry *nce = NULL;
  1822. /*
  1823. * First check if we already did a call to this function with the same
  1824. * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
  1825. * return the cached result.
  1826. */
  1827. nce = name_cache_search(sctx, ino, gen);
  1828. if (nce) {
  1829. if (ino < sctx->send_progress && nce->need_later_update) {
  1830. name_cache_delete(sctx, nce);
  1831. kfree(nce);
  1832. nce = NULL;
  1833. } else {
  1834. name_cache_used(sctx, nce);
  1835. *parent_ino = nce->parent_ino;
  1836. *parent_gen = nce->parent_gen;
  1837. ret = fs_path_add(dest, nce->name, nce->name_len);
  1838. if (ret < 0)
  1839. goto out;
  1840. ret = nce->ret;
  1841. goto out;
  1842. }
  1843. }
  1844. /*
  1845. * If the inode is not existent yet, add the orphan name and return 1.
  1846. * This should only happen for the parent dir that we determine in
  1847. * __record_new_ref
  1848. */
  1849. ret = is_inode_existent(sctx, ino, gen);
  1850. if (ret < 0)
  1851. goto out;
  1852. if (!ret) {
  1853. ret = gen_unique_name(sctx, ino, gen, dest);
  1854. if (ret < 0)
  1855. goto out;
  1856. ret = 1;
  1857. goto out_cache;
  1858. }
  1859. /*
  1860. * Depending on whether the inode was already processed or not, use
  1861. * send_root or parent_root for ref lookup.
  1862. */
  1863. if (ino < sctx->send_progress)
  1864. ret = get_first_ref(sctx->send_root, ino,
  1865. parent_ino, parent_gen, dest);
  1866. else
  1867. ret = get_first_ref(sctx->parent_root, ino,
  1868. parent_ino, parent_gen, dest);
  1869. if (ret < 0)
  1870. goto out;
  1871. /*
  1872. * Check if the ref was overwritten by an inode's ref that was processed
  1873. * earlier. If yes, treat as orphan and return 1.
  1874. */
  1875. ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
  1876. dest->start, dest->end - dest->start);
  1877. if (ret < 0)
  1878. goto out;
  1879. if (ret) {
  1880. fs_path_reset(dest);
  1881. ret = gen_unique_name(sctx, ino, gen, dest);
  1882. if (ret < 0)
  1883. goto out;
  1884. ret = 1;
  1885. }
  1886. out_cache:
  1887. /*
  1888. * Store the result of the lookup in the name cache.
  1889. */
  1890. nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS);
  1891. if (!nce) {
  1892. ret = -ENOMEM;
  1893. goto out;
  1894. }
  1895. nce->ino = ino;
  1896. nce->gen = gen;
  1897. nce->parent_ino = *parent_ino;
  1898. nce->parent_gen = *parent_gen;
  1899. nce->name_len = fs_path_len(dest);
  1900. nce->ret = ret;
  1901. strcpy(nce->name, dest->start);
  1902. if (ino < sctx->send_progress)
  1903. nce->need_later_update = 0;
  1904. else
  1905. nce->need_later_update = 1;
  1906. nce_ret = name_cache_insert(sctx, nce);
  1907. if (nce_ret < 0)
  1908. ret = nce_ret;
  1909. name_cache_clean_unused(sctx);
  1910. out:
  1911. return ret;
  1912. }
  1913. /*
  1914. * Magic happens here. This function returns the first ref to an inode as it
  1915. * would look like while receiving the stream at this point in time.
  1916. * We walk the path up to the root. For every inode in between, we check if it
  1917. * was already processed/sent. If yes, we continue with the parent as found
  1918. * in send_root. If not, we continue with the parent as found in parent_root.
  1919. * If we encounter an inode that was deleted at this point in time, we use the
  1920. * inodes "orphan" name instead of the real name and stop. Same with new inodes
  1921. * that were not created yet and overwritten inodes/refs.
  1922. *
  1923. * When do we have have orphan inodes:
  1924. * 1. When an inode is freshly created and thus no valid refs are available yet
  1925. * 2. When a directory lost all it's refs (deleted) but still has dir items
  1926. * inside which were not processed yet (pending for move/delete). If anyone
  1927. * tried to get the path to the dir items, it would get a path inside that
  1928. * orphan directory.
  1929. * 3. When an inode is moved around or gets new links, it may overwrite the ref
  1930. * of an unprocessed inode. If in that case the first ref would be
  1931. * overwritten, the overwritten inode gets "orphanized". Later when we
  1932. * process this overwritten inode, it is restored at a new place by moving
  1933. * the orphan inode.
  1934. *
  1935. * sctx->send_progress tells this function at which point in time receiving
  1936. * would be.
  1937. */
  1938. static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
  1939. struct fs_path *dest)
  1940. {
  1941. int ret = 0;
  1942. struct fs_path *name = NULL;
  1943. u64 parent_inode = 0;
  1944. u64 parent_gen = 0;
  1945. int stop = 0;
  1946. name = fs_path_alloc();
  1947. if (!name) {
  1948. ret = -ENOMEM;
  1949. goto out;
  1950. }
  1951. dest->reversed = 1;
  1952. fs_path_reset(dest);
  1953. while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
  1954. struct waiting_dir_move *wdm;
  1955. fs_path_reset(name);
  1956. if (is_waiting_for_rm(sctx, ino)) {
  1957. ret = gen_unique_name(sctx, ino, gen, name);
  1958. if (ret < 0)
  1959. goto out;
  1960. ret = fs_path_add_path(dest, name);
  1961. break;
  1962. }
  1963. wdm = get_waiting_dir_move(sctx, ino);
  1964. if (wdm && wdm->orphanized) {
  1965. ret = gen_unique_name(sctx, ino, gen, name);
  1966. stop = 1;
  1967. } else if (wdm) {
  1968. ret = get_first_ref(sctx->parent_root, ino,
  1969. &parent_inode, &parent_gen, name);
  1970. } else {
  1971. ret = __get_cur_name_and_parent(sctx, ino, gen,
  1972. &parent_inode,
  1973. &parent_gen, name);
  1974. if (ret)
  1975. stop = 1;
  1976. }
  1977. if (ret < 0)
  1978. goto out;
  1979. ret = fs_path_add_path(dest, name);
  1980. if (ret < 0)
  1981. goto out;
  1982. ino = parent_inode;
  1983. gen = parent_gen;
  1984. }
  1985. out:
  1986. fs_path_free(name);
  1987. if (!ret)
  1988. fs_path_unreverse(dest);
  1989. return ret;
  1990. }
  1991. /*
  1992. * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
  1993. */
  1994. static int send_subvol_begin(struct send_ctx *sctx)
  1995. {
  1996. int ret;
  1997. struct btrfs_root *send_root = sctx->send_root;
  1998. struct btrfs_root *parent_root = sctx->parent_root;
  1999. struct btrfs_path *path;
  2000. struct btrfs_key key;
  2001. struct btrfs_root_ref *ref;
  2002. struct extent_buffer *leaf;
  2003. char *name = NULL;
  2004. int namelen;
  2005. path = btrfs_alloc_path();
  2006. if (!path)
  2007. return -ENOMEM;
  2008. name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS);
  2009. if (!name) {
  2010. btrfs_free_path(path);
  2011. return -ENOMEM;
  2012. }
  2013. key.objectid = send_root->objectid;
  2014. key.type = BTRFS_ROOT_BACKREF_KEY;
  2015. key.offset = 0;
  2016. ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
  2017. &key, path, 1, 0);
  2018. if (ret < 0)
  2019. goto out;
  2020. if (ret) {
  2021. ret = -ENOENT;
  2022. goto out;
  2023. }
  2024. leaf = path->nodes[0];
  2025. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  2026. if (key.type != BTRFS_ROOT_BACKREF_KEY ||
  2027. key.objectid != send_root->objectid) {
  2028. ret = -ENOENT;
  2029. goto out;
  2030. }
  2031. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
  2032. namelen = btrfs_root_ref_name_len(leaf, ref);
  2033. read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
  2034. btrfs_release_path(path);
  2035. if (parent_root) {
  2036. ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
  2037. if (ret < 0)
  2038. goto out;
  2039. } else {
  2040. ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
  2041. if (ret < 0)
  2042. goto out;
  2043. }
  2044. TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
  2045. if (!btrfs_is_empty_uuid(sctx->send_root->root_item.received_uuid))
  2046. TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
  2047. sctx->send_root->root_item.received_uuid);
  2048. else
  2049. TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
  2050. sctx->send_root->root_item.uuid);
  2051. TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
  2052. le64_to_cpu(sctx->send_root->root_item.ctransid));
  2053. if (parent_root) {
  2054. if (!btrfs_is_empty_uuid(parent_root->root_item.received_uuid))
  2055. TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
  2056. parent_root->root_item.received_uuid);
  2057. else
  2058. TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
  2059. parent_root->root_item.uuid);
  2060. TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
  2061. le64_to_cpu(sctx->parent_root->root_item.ctransid));
  2062. }
  2063. ret = send_cmd(sctx);
  2064. tlv_put_failure:
  2065. out:
  2066. btrfs_free_path(path);
  2067. kfree(name);
  2068. return ret;
  2069. }
  2070. static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
  2071. {
  2072. int ret = 0;
  2073. struct fs_path *p;
  2074. verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size);
  2075. p = fs_path_alloc();
  2076. if (!p)
  2077. return -ENOMEM;
  2078. ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
  2079. if (ret < 0)
  2080. goto out;
  2081. ret = get_cur_path(sctx, ino, gen, p);
  2082. if (ret < 0)
  2083. goto out;
  2084. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  2085. TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
  2086. ret = send_cmd(sctx);
  2087. tlv_put_failure:
  2088. out:
  2089. fs_path_free(p);
  2090. return ret;
  2091. }
  2092. static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
  2093. {
  2094. int ret = 0;
  2095. struct fs_path *p;
  2096. verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode);
  2097. p = fs_path_alloc();
  2098. if (!p)
  2099. return -ENOMEM;
  2100. ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
  2101. if (ret < 0)
  2102. goto out;
  2103. ret = get_cur_path(sctx, ino, gen, p);
  2104. if (ret < 0)
  2105. goto out;
  2106. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  2107. TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
  2108. ret = send_cmd(sctx);
  2109. tlv_put_failure:
  2110. out:
  2111. fs_path_free(p);
  2112. return ret;
  2113. }
  2114. static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
  2115. {
  2116. int ret = 0;
  2117. struct fs_path *p;
  2118. verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
  2119. p = fs_path_alloc();
  2120. if (!p)
  2121. return -ENOMEM;
  2122. ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
  2123. if (ret < 0)
  2124. goto out;
  2125. ret = get_cur_path(sctx, ino, gen, p);
  2126. if (ret < 0)
  2127. goto out;
  2128. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  2129. TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
  2130. TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
  2131. ret = send_cmd(sctx);
  2132. tlv_put_failure:
  2133. out:
  2134. fs_path_free(p);
  2135. return ret;
  2136. }
  2137. static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
  2138. {
  2139. int ret = 0;
  2140. struct fs_path *p = NULL;
  2141. struct btrfs_inode_item *ii;
  2142. struct btrfs_path *path = NULL;
  2143. struct extent_buffer *eb;
  2144. struct btrfs_key key;
  2145. int slot;
  2146. verbose_printk("btrfs: send_utimes %llu\n", ino);
  2147. p = fs_path_alloc();
  2148. if (!p)
  2149. return -ENOMEM;
  2150. path = alloc_path_for_send();
  2151. if (!path) {
  2152. ret = -ENOMEM;
  2153. goto out;
  2154. }
  2155. key.objectid = ino;
  2156. key.type = BTRFS_INODE_ITEM_KEY;
  2157. key.offset = 0;
  2158. ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
  2159. if (ret < 0)
  2160. goto out;
  2161. eb = path->nodes[0];
  2162. slot = path->slots[0];
  2163. ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
  2164. ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
  2165. if (ret < 0)
  2166. goto out;
  2167. ret = get_cur_path(sctx, ino, gen, p);
  2168. if (ret < 0)
  2169. goto out;
  2170. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  2171. TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime);
  2172. TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime);
  2173. TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime);
  2174. /* TODO Add otime support when the otime patches get into upstream */
  2175. ret = send_cmd(sctx);
  2176. tlv_put_failure:
  2177. out:
  2178. fs_path_free(p);
  2179. btrfs_free_path(path);
  2180. return ret;
  2181. }
  2182. /*
  2183. * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
  2184. * a valid path yet because we did not process the refs yet. So, the inode
  2185. * is created as orphan.
  2186. */
  2187. static int send_create_inode(struct send_ctx *sctx, u64 ino)
  2188. {
  2189. int ret = 0;
  2190. struct fs_path *p;
  2191. int cmd;
  2192. u64 gen;
  2193. u64 mode;
  2194. u64 rdev;
  2195. verbose_printk("btrfs: send_create_inode %llu\n", ino);
  2196. p = fs_path_alloc();
  2197. if (!p)
  2198. return -ENOMEM;
  2199. if (ino != sctx->cur_ino) {
  2200. ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode,
  2201. NULL, NULL, &rdev);
  2202. if (ret < 0)
  2203. goto out;
  2204. } else {
  2205. gen = sctx->cur_inode_gen;
  2206. mode = sctx->cur_inode_mode;
  2207. rdev = sctx->cur_inode_rdev;
  2208. }
  2209. if (S_ISREG(mode)) {
  2210. cmd = BTRFS_SEND_C_MKFILE;
  2211. } else if (S_ISDIR(mode)) {
  2212. cmd = BTRFS_SEND_C_MKDIR;
  2213. } else if (S_ISLNK(mode)) {
  2214. cmd = BTRFS_SEND_C_SYMLINK;
  2215. } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
  2216. cmd = BTRFS_SEND_C_MKNOD;
  2217. } else if (S_ISFIFO(mode)) {
  2218. cmd = BTRFS_SEND_C_MKFIFO;
  2219. } else if (S_ISSOCK(mode)) {
  2220. cmd = BTRFS_SEND_C_MKSOCK;
  2221. } else {
  2222. btrfs_warn(sctx->send_root->fs_info, "unexpected inode type %o",
  2223. (int)(mode & S_IFMT));
  2224. ret = -ENOTSUPP;
  2225. goto out;
  2226. }
  2227. ret = begin_cmd(sctx, cmd);
  2228. if (ret < 0)
  2229. goto out;
  2230. ret = gen_unique_name(sctx, ino, gen, p);
  2231. if (ret < 0)
  2232. goto out;
  2233. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  2234. TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
  2235. if (S_ISLNK(mode)) {
  2236. fs_path_reset(p);
  2237. ret = read_symlink(sctx->send_root, ino, p);
  2238. if (ret < 0)
  2239. goto out;
  2240. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
  2241. } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
  2242. S_ISFIFO(mode) || S_ISSOCK(mode)) {
  2243. TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
  2244. TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
  2245. }
  2246. ret = send_cmd(sctx);
  2247. if (ret < 0)
  2248. goto out;
  2249. tlv_put_failure:
  2250. out:
  2251. fs_path_free(p);
  2252. return ret;
  2253. }
  2254. /*
  2255. * We need some special handling for inodes that get processed before the parent
  2256. * directory got created. See process_recorded_refs for details.
  2257. * This function does the check if we already created the dir out of order.
  2258. */
  2259. static int did_create_dir(struct send_ctx *sctx, u64 dir)
  2260. {
  2261. int ret = 0;
  2262. struct btrfs_path *path = NULL;
  2263. struct btrfs_key key;
  2264. struct btrfs_key found_key;
  2265. struct btrfs_key di_key;
  2266. struct extent_buffer *eb;
  2267. struct btrfs_dir_item *di;
  2268. int slot;
  2269. path = alloc_path_for_send();
  2270. if (!path) {
  2271. ret = -ENOMEM;
  2272. goto out;
  2273. }
  2274. key.objectid = dir;
  2275. key.type = BTRFS_DIR_INDEX_KEY;
  2276. key.offset = 0;
  2277. ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
  2278. if (ret < 0)
  2279. goto out;
  2280. while (1) {
  2281. eb = path->nodes[0];
  2282. slot = path->slots[0];
  2283. if (slot >= btrfs_header_nritems(eb)) {
  2284. ret = btrfs_next_leaf(sctx->send_root, path);
  2285. if (ret < 0) {
  2286. goto out;
  2287. } else if (ret > 0) {
  2288. ret = 0;
  2289. break;
  2290. }
  2291. continue;
  2292. }
  2293. btrfs_item_key_to_cpu(eb, &found_key, slot);
  2294. if (found_key.objectid != key.objectid ||
  2295. found_key.type != key.type) {
  2296. ret = 0;
  2297. goto out;
  2298. }
  2299. di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
  2300. btrfs_dir_item_key_to_cpu(eb, di, &di_key);
  2301. if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
  2302. di_key.objectid < sctx->send_progress) {
  2303. ret = 1;
  2304. goto out;
  2305. }
  2306. path->slots[0]++;
  2307. }
  2308. out:
  2309. btrfs_free_path(path);
  2310. return ret;
  2311. }
  2312. /*
  2313. * Only creates the inode if it is:
  2314. * 1. Not a directory
  2315. * 2. Or a directory which was not created already due to out of order
  2316. * directories. See did_create_dir and process_recorded_refs for details.
  2317. */
  2318. static int send_create_inode_if_needed(struct send_ctx *sctx)
  2319. {
  2320. int ret;
  2321. if (S_ISDIR(sctx->cur_inode_mode)) {
  2322. ret = did_create_dir(sctx, sctx->cur_ino);
  2323. if (ret < 0)
  2324. goto out;
  2325. if (ret) {
  2326. ret = 0;
  2327. goto out;
  2328. }
  2329. }
  2330. ret = send_create_inode(sctx, sctx->cur_ino);
  2331. if (ret < 0)
  2332. goto out;
  2333. out:
  2334. return ret;
  2335. }
  2336. struct recorded_ref {
  2337. struct list_head list;
  2338. char *dir_path;
  2339. char *name;
  2340. struct fs_path *full_path;
  2341. u64 dir;
  2342. u64 dir_gen;
  2343. int dir_path_len;
  2344. int name_len;
  2345. };
  2346. /*
  2347. * We need to process new refs before deleted refs, but compare_tree gives us
  2348. * everything mixed. So we first record all refs and later process them.
  2349. * This function is a helper to record one ref.
  2350. */
  2351. static int __record_ref(struct list_head *head, u64 dir,
  2352. u64 dir_gen, struct fs_path *path)
  2353. {
  2354. struct recorded_ref *ref;
  2355. ref = kmalloc(sizeof(*ref), GFP_NOFS);
  2356. if (!ref)
  2357. return -ENOMEM;
  2358. ref->dir = dir;
  2359. ref->dir_gen = dir_gen;
  2360. ref->full_path = path;
  2361. ref->name = (char *)kbasename(ref->full_path->start);
  2362. ref->name_len = ref->full_path->end - ref->name;
  2363. ref->dir_path = ref->full_path->start;
  2364. if (ref->name == ref->full_path->start)
  2365. ref->dir_path_len = 0;
  2366. else
  2367. ref->dir_path_len = ref->full_path->end -
  2368. ref->full_path->start - 1 - ref->name_len;
  2369. list_add_tail(&ref->list, head);
  2370. return 0;
  2371. }
  2372. static int dup_ref(struct recorded_ref *ref, struct list_head *list)
  2373. {
  2374. struct recorded_ref *new;
  2375. new = kmalloc(sizeof(*ref), GFP_NOFS);
  2376. if (!new)
  2377. return -ENOMEM;
  2378. new->dir = ref->dir;
  2379. new->dir_gen = ref->dir_gen;
  2380. new->full_path = NULL;
  2381. INIT_LIST_HEAD(&new->list);
  2382. list_add_tail(&new->list, list);
  2383. return 0;
  2384. }
  2385. static void __free_recorded_refs(struct list_head *head)
  2386. {
  2387. struct recorded_ref *cur;
  2388. while (!list_empty(head)) {
  2389. cur = list_entry(head->next, struct recorded_ref, list);
  2390. fs_path_free(cur->full_path);
  2391. list_del(&cur->list);
  2392. kfree(cur);
  2393. }
  2394. }
  2395. static void free_recorded_refs(struct send_ctx *sctx)
  2396. {
  2397. __free_recorded_refs(&sctx->new_refs);
  2398. __free_recorded_refs(&sctx->deleted_refs);
  2399. }
  2400. /*
  2401. * Renames/moves a file/dir to its orphan name. Used when the first
  2402. * ref of an unprocessed inode gets overwritten and for all non empty
  2403. * directories.
  2404. */
  2405. static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
  2406. struct fs_path *path)
  2407. {
  2408. int ret;
  2409. struct fs_path *orphan;
  2410. orphan = fs_path_alloc();
  2411. if (!orphan)
  2412. return -ENOMEM;
  2413. ret = gen_unique_name(sctx, ino, gen, orphan);
  2414. if (ret < 0)
  2415. goto out;
  2416. ret = send_rename(sctx, path, orphan);
  2417. out:
  2418. fs_path_free(orphan);
  2419. return ret;
  2420. }
  2421. static struct orphan_dir_info *
  2422. add_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
  2423. {
  2424. struct rb_node **p = &sctx->orphan_dirs.rb_node;
  2425. struct rb_node *parent = NULL;
  2426. struct orphan_dir_info *entry, *odi;
  2427. odi = kmalloc(sizeof(*odi), GFP_NOFS);
  2428. if (!odi)
  2429. return ERR_PTR(-ENOMEM);
  2430. odi->ino = dir_ino;
  2431. odi->gen = 0;
  2432. while (*p) {
  2433. parent = *p;
  2434. entry = rb_entry(parent, struct orphan_dir_info, node);
  2435. if (dir_ino < entry->ino) {
  2436. p = &(*p)->rb_left;
  2437. } else if (dir_ino > entry->ino) {
  2438. p = &(*p)->rb_right;
  2439. } else {
  2440. kfree(odi);
  2441. return entry;
  2442. }
  2443. }
  2444. rb_link_node(&odi->node, parent, p);
  2445. rb_insert_color(&odi->node, &sctx->orphan_dirs);
  2446. return odi;
  2447. }
  2448. static struct orphan_dir_info *
  2449. get_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
  2450. {
  2451. struct rb_node *n = sctx->orphan_dirs.rb_node;
  2452. struct orphan_dir_info *entry;
  2453. while (n) {
  2454. entry = rb_entry(n, struct orphan_dir_info, node);
  2455. if (dir_ino < entry->ino)
  2456. n = n->rb_left;
  2457. else if (dir_ino > entry->ino)
  2458. n = n->rb_right;
  2459. else
  2460. return entry;
  2461. }
  2462. return NULL;
  2463. }
  2464. static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino)
  2465. {
  2466. struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino);
  2467. return odi != NULL;
  2468. }
  2469. static void free_orphan_dir_info(struct send_ctx *sctx,
  2470. struct orphan_dir_info *odi)
  2471. {
  2472. if (!odi)
  2473. return;
  2474. rb_erase(&odi->node, &sctx->orphan_dirs);
  2475. kfree(odi);
  2476. }
  2477. /*
  2478. * Returns 1 if a directory can be removed at this point in time.
  2479. * We check this by iterating all dir items and checking if the inode behind
  2480. * the dir item was already processed.
  2481. */
  2482. static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen,
  2483. u64 send_progress)
  2484. {
  2485. int ret = 0;
  2486. struct btrfs_root *root = sctx->parent_root;
  2487. struct btrfs_path *path;
  2488. struct btrfs_key key;
  2489. struct btrfs_key found_key;
  2490. struct btrfs_key loc;
  2491. struct btrfs_dir_item *di;
  2492. /*
  2493. * Don't try to rmdir the top/root subvolume dir.
  2494. */
  2495. if (dir == BTRFS_FIRST_FREE_OBJECTID)
  2496. return 0;
  2497. path = alloc_path_for_send();
  2498. if (!path)
  2499. return -ENOMEM;
  2500. key.objectid = dir;
  2501. key.type = BTRFS_DIR_INDEX_KEY;
  2502. key.offset = 0;
  2503. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2504. if (ret < 0)
  2505. goto out;
  2506. while (1) {
  2507. struct waiting_dir_move *dm;
  2508. if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
  2509. ret = btrfs_next_leaf(root, path);
  2510. if (ret < 0)
  2511. goto out;
  2512. else if (ret > 0)
  2513. break;
  2514. continue;
  2515. }
  2516. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  2517. path->slots[0]);
  2518. if (found_key.objectid != key.objectid ||
  2519. found_key.type != key.type)
  2520. break;
  2521. di = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2522. struct btrfs_dir_item);
  2523. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
  2524. dm = get_waiting_dir_move(sctx, loc.objectid);
  2525. if (dm) {
  2526. struct orphan_dir_info *odi;
  2527. odi = add_orphan_dir_info(sctx, dir);
  2528. if (IS_ERR(odi)) {
  2529. ret = PTR_ERR(odi);
  2530. goto out;
  2531. }
  2532. odi->gen = dir_gen;
  2533. dm->rmdir_ino = dir;
  2534. ret = 0;
  2535. goto out;
  2536. }
  2537. if (loc.objectid > send_progress) {
  2538. ret = 0;
  2539. goto out;
  2540. }
  2541. path->slots[0]++;
  2542. }
  2543. ret = 1;
  2544. out:
  2545. btrfs_free_path(path);
  2546. return ret;
  2547. }
  2548. static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
  2549. {
  2550. struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
  2551. return entry != NULL;
  2552. }
  2553. static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino, bool orphanized)
  2554. {
  2555. struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
  2556. struct rb_node *parent = NULL;
  2557. struct waiting_dir_move *entry, *dm;
  2558. dm = kmalloc(sizeof(*dm), GFP_NOFS);
  2559. if (!dm)
  2560. return -ENOMEM;
  2561. dm->ino = ino;
  2562. dm->rmdir_ino = 0;
  2563. dm->orphanized = orphanized;
  2564. while (*p) {
  2565. parent = *p;
  2566. entry = rb_entry(parent, struct waiting_dir_move, node);
  2567. if (ino < entry->ino) {
  2568. p = &(*p)->rb_left;
  2569. } else if (ino > entry->ino) {
  2570. p = &(*p)->rb_right;
  2571. } else {
  2572. kfree(dm);
  2573. return -EEXIST;
  2574. }
  2575. }
  2576. rb_link_node(&dm->node, parent, p);
  2577. rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
  2578. return 0;
  2579. }
  2580. static struct waiting_dir_move *
  2581. get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
  2582. {
  2583. struct rb_node *n = sctx->waiting_dir_moves.rb_node;
  2584. struct waiting_dir_move *entry;
  2585. while (n) {
  2586. entry = rb_entry(n, struct waiting_dir_move, node);
  2587. if (ino < entry->ino)
  2588. n = n->rb_left;
  2589. else if (ino > entry->ino)
  2590. n = n->rb_right;
  2591. else
  2592. return entry;
  2593. }
  2594. return NULL;
  2595. }
  2596. static void free_waiting_dir_move(struct send_ctx *sctx,
  2597. struct waiting_dir_move *dm)
  2598. {
  2599. if (!dm)
  2600. return;
  2601. rb_erase(&dm->node, &sctx->waiting_dir_moves);
  2602. kfree(dm);
  2603. }
  2604. static int add_pending_dir_move(struct send_ctx *sctx,
  2605. u64 ino,
  2606. u64 ino_gen,
  2607. u64 parent_ino,
  2608. struct list_head *new_refs,
  2609. struct list_head *deleted_refs,
  2610. const bool is_orphan)
  2611. {
  2612. struct rb_node **p = &sctx->pending_dir_moves.rb_node;
  2613. struct rb_node *parent = NULL;
  2614. struct pending_dir_move *entry = NULL, *pm;
  2615. struct recorded_ref *cur;
  2616. int exists = 0;
  2617. int ret;
  2618. pm = kmalloc(sizeof(*pm), GFP_NOFS);
  2619. if (!pm)
  2620. return -ENOMEM;
  2621. pm->parent_ino = parent_ino;
  2622. pm->ino = ino;
  2623. pm->gen = ino_gen;
  2624. pm->is_orphan = is_orphan;
  2625. INIT_LIST_HEAD(&pm->list);
  2626. INIT_LIST_HEAD(&pm->update_refs);
  2627. RB_CLEAR_NODE(&pm->node);
  2628. while (*p) {
  2629. parent = *p;
  2630. entry = rb_entry(parent, struct pending_dir_move, node);
  2631. if (parent_ino < entry->parent_ino) {
  2632. p = &(*p)->rb_left;
  2633. } else if (parent_ino > entry->parent_ino) {
  2634. p = &(*p)->rb_right;
  2635. } else {
  2636. exists = 1;
  2637. break;
  2638. }
  2639. }
  2640. list_for_each_entry(cur, deleted_refs, list) {
  2641. ret = dup_ref(cur, &pm->update_refs);
  2642. if (ret < 0)
  2643. goto out;
  2644. }
  2645. list_for_each_entry(cur, new_refs, list) {
  2646. ret = dup_ref(cur, &pm->update_refs);
  2647. if (ret < 0)
  2648. goto out;
  2649. }
  2650. ret = add_waiting_dir_move(sctx, pm->ino, is_orphan);
  2651. if (ret)
  2652. goto out;
  2653. if (exists) {
  2654. list_add_tail(&pm->list, &entry->list);
  2655. } else {
  2656. rb_link_node(&pm->node, parent, p);
  2657. rb_insert_color(&pm->node, &sctx->pending_dir_moves);
  2658. }
  2659. ret = 0;
  2660. out:
  2661. if (ret) {
  2662. __free_recorded_refs(&pm->update_refs);
  2663. kfree(pm);
  2664. }
  2665. return ret;
  2666. }
  2667. static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
  2668. u64 parent_ino)
  2669. {
  2670. struct rb_node *n = sctx->pending_dir_moves.rb_node;
  2671. struct pending_dir_move *entry;
  2672. while (n) {
  2673. entry = rb_entry(n, struct pending_dir_move, node);
  2674. if (parent_ino < entry->parent_ino)
  2675. n = n->rb_left;
  2676. else if (parent_ino > entry->parent_ino)
  2677. n = n->rb_right;
  2678. else
  2679. return entry;
  2680. }
  2681. return NULL;
  2682. }
  2683. static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
  2684. {
  2685. struct fs_path *from_path = NULL;
  2686. struct fs_path *to_path = NULL;
  2687. struct fs_path *name = NULL;
  2688. u64 orig_progress = sctx->send_progress;
  2689. struct recorded_ref *cur;
  2690. u64 parent_ino, parent_gen;
  2691. struct waiting_dir_move *dm = NULL;
  2692. u64 rmdir_ino = 0;
  2693. int ret;
  2694. name = fs_path_alloc();
  2695. from_path = fs_path_alloc();
  2696. if (!name || !from_path) {
  2697. ret = -ENOMEM;
  2698. goto out;
  2699. }
  2700. dm = get_waiting_dir_move(sctx, pm->ino);
  2701. ASSERT(dm);
  2702. rmdir_ino = dm->rmdir_ino;
  2703. free_waiting_dir_move(sctx, dm);
  2704. if (pm->is_orphan) {
  2705. ret = gen_unique_name(sctx, pm->ino,
  2706. pm->gen, from_path);
  2707. } else {
  2708. ret = get_first_ref(sctx->parent_root, pm->ino,
  2709. &parent_ino, &parent_gen, name);
  2710. if (ret < 0)
  2711. goto out;
  2712. ret = get_cur_path(sctx, parent_ino, parent_gen,
  2713. from_path);
  2714. if (ret < 0)
  2715. goto out;
  2716. ret = fs_path_add_path(from_path, name);
  2717. }
  2718. if (ret < 0)
  2719. goto out;
  2720. sctx->send_progress = sctx->cur_ino + 1;
  2721. fs_path_reset(name);
  2722. to_path = name;
  2723. name = NULL;
  2724. ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
  2725. if (ret < 0)
  2726. goto out;
  2727. ret = send_rename(sctx, from_path, to_path);
  2728. if (ret < 0)
  2729. goto out;
  2730. if (rmdir_ino) {
  2731. struct orphan_dir_info *odi;
  2732. odi = get_orphan_dir_info(sctx, rmdir_ino);
  2733. if (!odi) {
  2734. /* already deleted */
  2735. goto finish;
  2736. }
  2737. ret = can_rmdir(sctx, rmdir_ino, odi->gen, sctx->cur_ino + 1);
  2738. if (ret < 0)
  2739. goto out;
  2740. if (!ret)
  2741. goto finish;
  2742. name = fs_path_alloc();
  2743. if (!name) {
  2744. ret = -ENOMEM;
  2745. goto out;
  2746. }
  2747. ret = get_cur_path(sctx, rmdir_ino, odi->gen, name);
  2748. if (ret < 0)
  2749. goto out;
  2750. ret = send_rmdir(sctx, name);
  2751. if (ret < 0)
  2752. goto out;
  2753. free_orphan_dir_info(sctx, odi);
  2754. }
  2755. finish:
  2756. ret = send_utimes(sctx, pm->ino, pm->gen);
  2757. if (ret < 0)
  2758. goto out;
  2759. /*
  2760. * After rename/move, need to update the utimes of both new parent(s)
  2761. * and old parent(s).
  2762. */
  2763. list_for_each_entry(cur, &pm->update_refs, list) {
  2764. if (cur->dir == rmdir_ino)
  2765. continue;
  2766. ret = send_utimes(sctx, cur->dir, cur->dir_gen);
  2767. if (ret < 0)
  2768. goto out;
  2769. }
  2770. out:
  2771. fs_path_free(name);
  2772. fs_path_free(from_path);
  2773. fs_path_free(to_path);
  2774. sctx->send_progress = orig_progress;
  2775. return ret;
  2776. }
  2777. static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
  2778. {
  2779. if (!list_empty(&m->list))
  2780. list_del(&m->list);
  2781. if (!RB_EMPTY_NODE(&m->node))
  2782. rb_erase(&m->node, &sctx->pending_dir_moves);
  2783. __free_recorded_refs(&m->update_refs);
  2784. kfree(m);
  2785. }
  2786. static void tail_append_pending_moves(struct send_ctx *sctx,
  2787. struct pending_dir_move *moves,
  2788. struct list_head *stack)
  2789. {
  2790. if (list_empty(&moves->list)) {
  2791. list_add_tail(&moves->list, stack);
  2792. } else {
  2793. LIST_HEAD(list);
  2794. list_splice_init(&moves->list, &list);
  2795. list_add_tail(&moves->list, stack);
  2796. list_splice_tail(&list, stack);
  2797. }
  2798. if (!RB_EMPTY_NODE(&moves->node)) {
  2799. rb_erase(&moves->node, &sctx->pending_dir_moves);
  2800. RB_CLEAR_NODE(&moves->node);
  2801. }
  2802. }
  2803. static int apply_children_dir_moves(struct send_ctx *sctx)
  2804. {
  2805. struct pending_dir_move *pm;
  2806. struct list_head stack;
  2807. u64 parent_ino = sctx->cur_ino;
  2808. int ret = 0;
  2809. pm = get_pending_dir_moves(sctx, parent_ino);
  2810. if (!pm)
  2811. return 0;
  2812. INIT_LIST_HEAD(&stack);
  2813. tail_append_pending_moves(sctx, pm, &stack);
  2814. while (!list_empty(&stack)) {
  2815. pm = list_first_entry(&stack, struct pending_dir_move, list);
  2816. parent_ino = pm->ino;
  2817. ret = apply_dir_move(sctx, pm);
  2818. free_pending_move(sctx, pm);
  2819. if (ret)
  2820. goto out;
  2821. pm = get_pending_dir_moves(sctx, parent_ino);
  2822. if (pm)
  2823. tail_append_pending_moves(sctx, pm, &stack);
  2824. }
  2825. return 0;
  2826. out:
  2827. while (!list_empty(&stack)) {
  2828. pm = list_first_entry(&stack, struct pending_dir_move, list);
  2829. free_pending_move(sctx, pm);
  2830. }
  2831. return ret;
  2832. }
  2833. /*
  2834. * We might need to delay a directory rename even when no ancestor directory
  2835. * (in the send root) with a higher inode number than ours (sctx->cur_ino) was
  2836. * renamed. This happens when we rename a directory to the old name (the name
  2837. * in the parent root) of some other unrelated directory that got its rename
  2838. * delayed due to some ancestor with higher number that got renamed.
  2839. *
  2840. * Example:
  2841. *
  2842. * Parent snapshot:
  2843. * . (ino 256)
  2844. * |---- a/ (ino 257)
  2845. * | |---- file (ino 260)
  2846. * |
  2847. * |---- b/ (ino 258)
  2848. * |---- c/ (ino 259)
  2849. *
  2850. * Send snapshot:
  2851. * . (ino 256)
  2852. * |---- a/ (ino 258)
  2853. * |---- x/ (ino 259)
  2854. * |---- y/ (ino 257)
  2855. * |----- file (ino 260)
  2856. *
  2857. * Here we can not rename 258 from 'b' to 'a' without the rename of inode 257
  2858. * from 'a' to 'x/y' happening first, which in turn depends on the rename of
  2859. * inode 259 from 'c' to 'x'. So the order of rename commands the send stream
  2860. * must issue is:
  2861. *
  2862. * 1 - rename 259 from 'c' to 'x'
  2863. * 2 - rename 257 from 'a' to 'x/y'
  2864. * 3 - rename 258 from 'b' to 'a'
  2865. *
  2866. * Returns 1 if the rename of sctx->cur_ino needs to be delayed, 0 if it can
  2867. * be done right away and < 0 on error.
  2868. */
  2869. static int wait_for_dest_dir_move(struct send_ctx *sctx,
  2870. struct recorded_ref *parent_ref,
  2871. const bool is_orphan)
  2872. {
  2873. struct btrfs_path *path;
  2874. struct btrfs_key key;
  2875. struct btrfs_key di_key;
  2876. struct btrfs_dir_item *di;
  2877. u64 left_gen;
  2878. u64 right_gen;
  2879. int ret = 0;
  2880. if (RB_EMPTY_ROOT(&sctx->waiting_dir_moves))
  2881. return 0;
  2882. path = alloc_path_for_send();
  2883. if (!path)
  2884. return -ENOMEM;
  2885. key.objectid = parent_ref->dir;
  2886. key.type = BTRFS_DIR_ITEM_KEY;
  2887. key.offset = btrfs_name_hash(parent_ref->name, parent_ref->name_len);
  2888. ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
  2889. if (ret < 0) {
  2890. goto out;
  2891. } else if (ret > 0) {
  2892. ret = 0;
  2893. goto out;
  2894. }
  2895. di = btrfs_match_dir_item_name(sctx->parent_root, path,
  2896. parent_ref->name, parent_ref->name_len);
  2897. if (!di) {
  2898. ret = 0;
  2899. goto out;
  2900. }
  2901. /*
  2902. * di_key.objectid has the number of the inode that has a dentry in the
  2903. * parent directory with the same name that sctx->cur_ino is being
  2904. * renamed to. We need to check if that inode is in the send root as
  2905. * well and if it is currently marked as an inode with a pending rename,
  2906. * if it is, we need to delay the rename of sctx->cur_ino as well, so
  2907. * that it happens after that other inode is renamed.
  2908. */
  2909. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &di_key);
  2910. if (di_key.type != BTRFS_INODE_ITEM_KEY) {
  2911. ret = 0;
  2912. goto out;
  2913. }
  2914. ret = get_inode_info(sctx->parent_root, di_key.objectid, NULL,
  2915. &left_gen, NULL, NULL, NULL, NULL);
  2916. if (ret < 0)
  2917. goto out;
  2918. ret = get_inode_info(sctx->send_root, di_key.objectid, NULL,
  2919. &right_gen, NULL, NULL, NULL, NULL);
  2920. if (ret < 0) {
  2921. if (ret == -ENOENT)
  2922. ret = 0;
  2923. goto out;
  2924. }
  2925. /* Different inode, no need to delay the rename of sctx->cur_ino */
  2926. if (right_gen != left_gen) {
  2927. ret = 0;
  2928. goto out;
  2929. }
  2930. if (is_waiting_for_move(sctx, di_key.objectid)) {
  2931. ret = add_pending_dir_move(sctx,
  2932. sctx->cur_ino,
  2933. sctx->cur_inode_gen,
  2934. di_key.objectid,
  2935. &sctx->new_refs,
  2936. &sctx->deleted_refs,
  2937. is_orphan);
  2938. if (!ret)
  2939. ret = 1;
  2940. }
  2941. out:
  2942. btrfs_free_path(path);
  2943. return ret;
  2944. }
  2945. /*
  2946. * Check if ino ino1 is an ancestor of inode ino2 in the given root.
  2947. * Return 1 if true, 0 if false and < 0 on error.
  2948. */
  2949. static int is_ancestor(struct btrfs_root *root,
  2950. const u64 ino1,
  2951. const u64 ino1_gen,
  2952. const u64 ino2,
  2953. struct fs_path *fs_path)
  2954. {
  2955. u64 ino = ino2;
  2956. while (ino > BTRFS_FIRST_FREE_OBJECTID) {
  2957. int ret;
  2958. u64 parent;
  2959. u64 parent_gen;
  2960. fs_path_reset(fs_path);
  2961. ret = get_first_ref(root, ino, &parent, &parent_gen, fs_path);
  2962. if (ret < 0) {
  2963. if (ret == -ENOENT && ino == ino2)
  2964. ret = 0;
  2965. return ret;
  2966. }
  2967. if (parent == ino1)
  2968. return parent_gen == ino1_gen ? 1 : 0;
  2969. ino = parent;
  2970. }
  2971. return 0;
  2972. }
  2973. static int wait_for_parent_move(struct send_ctx *sctx,
  2974. struct recorded_ref *parent_ref,
  2975. const bool is_orphan)
  2976. {
  2977. int ret = 0;
  2978. u64 ino = parent_ref->dir;
  2979. u64 parent_ino_before, parent_ino_after;
  2980. struct fs_path *path_before = NULL;
  2981. struct fs_path *path_after = NULL;
  2982. int len1, len2;
  2983. path_after = fs_path_alloc();
  2984. path_before = fs_path_alloc();
  2985. if (!path_after || !path_before) {
  2986. ret = -ENOMEM;
  2987. goto out;
  2988. }
  2989. /*
  2990. * Our current directory inode may not yet be renamed/moved because some
  2991. * ancestor (immediate or not) has to be renamed/moved first. So find if
  2992. * such ancestor exists and make sure our own rename/move happens after
  2993. * that ancestor is processed to avoid path build infinite loops (done
  2994. * at get_cur_path()).
  2995. */
  2996. while (ino > BTRFS_FIRST_FREE_OBJECTID) {
  2997. if (is_waiting_for_move(sctx, ino)) {
  2998. /*
  2999. * If the current inode is an ancestor of ino in the
  3000. * parent root, we need to delay the rename of the
  3001. * current inode, otherwise don't delayed the rename
  3002. * because we can end up with a circular dependency
  3003. * of renames, resulting in some directories never
  3004. * getting the respective rename operations issued in
  3005. * the send stream or getting into infinite path build
  3006. * loops.
  3007. */
  3008. ret = is_ancestor(sctx->parent_root,
  3009. sctx->cur_ino, sctx->cur_inode_gen,
  3010. ino, path_before);
  3011. break;
  3012. }
  3013. fs_path_reset(path_before);
  3014. fs_path_reset(path_after);
  3015. ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
  3016. NULL, path_after);
  3017. if (ret < 0)
  3018. goto out;
  3019. ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
  3020. NULL, path_before);
  3021. if (ret < 0 && ret != -ENOENT) {
  3022. goto out;
  3023. } else if (ret == -ENOENT) {
  3024. ret = 0;
  3025. break;
  3026. }
  3027. len1 = fs_path_len(path_before);
  3028. len2 = fs_path_len(path_after);
  3029. if (ino > sctx->cur_ino &&
  3030. (parent_ino_before != parent_ino_after || len1 != len2 ||
  3031. memcmp(path_before->start, path_after->start, len1))) {
  3032. ret = 1;
  3033. break;
  3034. }
  3035. ino = parent_ino_after;
  3036. }
  3037. out:
  3038. fs_path_free(path_before);
  3039. fs_path_free(path_after);
  3040. if (ret == 1) {
  3041. ret = add_pending_dir_move(sctx,
  3042. sctx->cur_ino,
  3043. sctx->cur_inode_gen,
  3044. ino,
  3045. &sctx->new_refs,
  3046. &sctx->deleted_refs,
  3047. is_orphan);
  3048. if (!ret)
  3049. ret = 1;
  3050. }
  3051. return ret;
  3052. }
  3053. /*
  3054. * This does all the move/link/unlink/rmdir magic.
  3055. */
  3056. static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
  3057. {
  3058. int ret = 0;
  3059. struct recorded_ref *cur;
  3060. struct recorded_ref *cur2;
  3061. struct list_head check_dirs;
  3062. struct fs_path *valid_path = NULL;
  3063. u64 ow_inode = 0;
  3064. u64 ow_gen;
  3065. int did_overwrite = 0;
  3066. int is_orphan = 0;
  3067. u64 last_dir_ino_rm = 0;
  3068. bool can_rename = true;
  3069. verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
  3070. /*
  3071. * This should never happen as the root dir always has the same ref
  3072. * which is always '..'
  3073. */
  3074. BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
  3075. INIT_LIST_HEAD(&check_dirs);
  3076. valid_path = fs_path_alloc();
  3077. if (!valid_path) {
  3078. ret = -ENOMEM;
  3079. goto out;
  3080. }
  3081. /*
  3082. * First, check if the first ref of the current inode was overwritten
  3083. * before. If yes, we know that the current inode was already orphanized
  3084. * and thus use the orphan name. If not, we can use get_cur_path to
  3085. * get the path of the first ref as it would like while receiving at
  3086. * this point in time.
  3087. * New inodes are always orphan at the beginning, so force to use the
  3088. * orphan name in this case.
  3089. * The first ref is stored in valid_path and will be updated if it
  3090. * gets moved around.
  3091. */
  3092. if (!sctx->cur_inode_new) {
  3093. ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
  3094. sctx->cur_inode_gen);
  3095. if (ret < 0)
  3096. goto out;
  3097. if (ret)
  3098. did_overwrite = 1;
  3099. }
  3100. if (sctx->cur_inode_new || did_overwrite) {
  3101. ret = gen_unique_name(sctx, sctx->cur_ino,
  3102. sctx->cur_inode_gen, valid_path);
  3103. if (ret < 0)
  3104. goto out;
  3105. is_orphan = 1;
  3106. } else {
  3107. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  3108. valid_path);
  3109. if (ret < 0)
  3110. goto out;
  3111. }
  3112. list_for_each_entry(cur, &sctx->new_refs, list) {
  3113. /*
  3114. * We may have refs where the parent directory does not exist
  3115. * yet. This happens if the parent directories inum is higher
  3116. * the the current inum. To handle this case, we create the
  3117. * parent directory out of order. But we need to check if this
  3118. * did already happen before due to other refs in the same dir.
  3119. */
  3120. ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
  3121. if (ret < 0)
  3122. goto out;
  3123. if (ret == inode_state_will_create) {
  3124. ret = 0;
  3125. /*
  3126. * First check if any of the current inodes refs did
  3127. * already create the dir.
  3128. */
  3129. list_for_each_entry(cur2, &sctx->new_refs, list) {
  3130. if (cur == cur2)
  3131. break;
  3132. if (cur2->dir == cur->dir) {
  3133. ret = 1;
  3134. break;
  3135. }
  3136. }
  3137. /*
  3138. * If that did not happen, check if a previous inode
  3139. * did already create the dir.
  3140. */
  3141. if (!ret)
  3142. ret = did_create_dir(sctx, cur->dir);
  3143. if (ret < 0)
  3144. goto out;
  3145. if (!ret) {
  3146. ret = send_create_inode(sctx, cur->dir);
  3147. if (ret < 0)
  3148. goto out;
  3149. }
  3150. }
  3151. /*
  3152. * Check if this new ref would overwrite the first ref of
  3153. * another unprocessed inode. If yes, orphanize the
  3154. * overwritten inode. If we find an overwritten ref that is
  3155. * not the first ref, simply unlink it.
  3156. */
  3157. ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
  3158. cur->name, cur->name_len,
  3159. &ow_inode, &ow_gen);
  3160. if (ret < 0)
  3161. goto out;
  3162. if (ret) {
  3163. ret = is_first_ref(sctx->parent_root,
  3164. ow_inode, cur->dir, cur->name,
  3165. cur->name_len);
  3166. if (ret < 0)
  3167. goto out;
  3168. if (ret) {
  3169. struct name_cache_entry *nce;
  3170. ret = orphanize_inode(sctx, ow_inode, ow_gen,
  3171. cur->full_path);
  3172. if (ret < 0)
  3173. goto out;
  3174. /*
  3175. * Make sure we clear our orphanized inode's
  3176. * name from the name cache. This is because the
  3177. * inode ow_inode might be an ancestor of some
  3178. * other inode that will be orphanized as well
  3179. * later and has an inode number greater than
  3180. * sctx->send_progress. We need to prevent
  3181. * future name lookups from using the old name
  3182. * and get instead the orphan name.
  3183. */
  3184. nce = name_cache_search(sctx, ow_inode, ow_gen);
  3185. if (nce) {
  3186. name_cache_delete(sctx, nce);
  3187. kfree(nce);
  3188. }
  3189. } else {
  3190. ret = send_unlink(sctx, cur->full_path);
  3191. if (ret < 0)
  3192. goto out;
  3193. }
  3194. }
  3195. if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root) {
  3196. ret = wait_for_dest_dir_move(sctx, cur, is_orphan);
  3197. if (ret < 0)
  3198. goto out;
  3199. if (ret == 1) {
  3200. can_rename = false;
  3201. *pending_move = 1;
  3202. }
  3203. }
  3204. if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root &&
  3205. can_rename) {
  3206. ret = wait_for_parent_move(sctx, cur, is_orphan);
  3207. if (ret < 0)
  3208. goto out;
  3209. if (ret == 1) {
  3210. can_rename = false;
  3211. *pending_move = 1;
  3212. }
  3213. }
  3214. /*
  3215. * link/move the ref to the new place. If we have an orphan
  3216. * inode, move it and update valid_path. If not, link or move
  3217. * it depending on the inode mode.
  3218. */
  3219. if (is_orphan && can_rename) {
  3220. ret = send_rename(sctx, valid_path, cur->full_path);
  3221. if (ret < 0)
  3222. goto out;
  3223. is_orphan = 0;
  3224. ret = fs_path_copy(valid_path, cur->full_path);
  3225. if (ret < 0)
  3226. goto out;
  3227. } else if (can_rename) {
  3228. if (S_ISDIR(sctx->cur_inode_mode)) {
  3229. /*
  3230. * Dirs can't be linked, so move it. For moved
  3231. * dirs, we always have one new and one deleted
  3232. * ref. The deleted ref is ignored later.
  3233. */
  3234. ret = send_rename(sctx, valid_path,
  3235. cur->full_path);
  3236. if (!ret)
  3237. ret = fs_path_copy(valid_path,
  3238. cur->full_path);
  3239. if (ret < 0)
  3240. goto out;
  3241. } else {
  3242. ret = send_link(sctx, cur->full_path,
  3243. valid_path);
  3244. if (ret < 0)
  3245. goto out;
  3246. }
  3247. }
  3248. ret = dup_ref(cur, &check_dirs);
  3249. if (ret < 0)
  3250. goto out;
  3251. }
  3252. if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
  3253. /*
  3254. * Check if we can already rmdir the directory. If not,
  3255. * orphanize it. For every dir item inside that gets deleted
  3256. * later, we do this check again and rmdir it then if possible.
  3257. * See the use of check_dirs for more details.
  3258. */
  3259. ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  3260. sctx->cur_ino);
  3261. if (ret < 0)
  3262. goto out;
  3263. if (ret) {
  3264. ret = send_rmdir(sctx, valid_path);
  3265. if (ret < 0)
  3266. goto out;
  3267. } else if (!is_orphan) {
  3268. ret = orphanize_inode(sctx, sctx->cur_ino,
  3269. sctx->cur_inode_gen, valid_path);
  3270. if (ret < 0)
  3271. goto out;
  3272. is_orphan = 1;
  3273. }
  3274. list_for_each_entry(cur, &sctx->deleted_refs, list) {
  3275. ret = dup_ref(cur, &check_dirs);
  3276. if (ret < 0)
  3277. goto out;
  3278. }
  3279. } else if (S_ISDIR(sctx->cur_inode_mode) &&
  3280. !list_empty(&sctx->deleted_refs)) {
  3281. /*
  3282. * We have a moved dir. Add the old parent to check_dirs
  3283. */
  3284. cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
  3285. list);
  3286. ret = dup_ref(cur, &check_dirs);
  3287. if (ret < 0)
  3288. goto out;
  3289. } else if (!S_ISDIR(sctx->cur_inode_mode)) {
  3290. /*
  3291. * We have a non dir inode. Go through all deleted refs and
  3292. * unlink them if they were not already overwritten by other
  3293. * inodes.
  3294. */
  3295. list_for_each_entry(cur, &sctx->deleted_refs, list) {
  3296. ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
  3297. sctx->cur_ino, sctx->cur_inode_gen,
  3298. cur->name, cur->name_len);
  3299. if (ret < 0)
  3300. goto out;
  3301. if (!ret) {
  3302. ret = send_unlink(sctx, cur->full_path);
  3303. if (ret < 0)
  3304. goto out;
  3305. }
  3306. ret = dup_ref(cur, &check_dirs);
  3307. if (ret < 0)
  3308. goto out;
  3309. }
  3310. /*
  3311. * If the inode is still orphan, unlink the orphan. This may
  3312. * happen when a previous inode did overwrite the first ref
  3313. * of this inode and no new refs were added for the current
  3314. * inode. Unlinking does not mean that the inode is deleted in
  3315. * all cases. There may still be links to this inode in other
  3316. * places.
  3317. */
  3318. if (is_orphan) {
  3319. ret = send_unlink(sctx, valid_path);
  3320. if (ret < 0)
  3321. goto out;
  3322. }
  3323. }
  3324. /*
  3325. * We did collect all parent dirs where cur_inode was once located. We
  3326. * now go through all these dirs and check if they are pending for
  3327. * deletion and if it's finally possible to perform the rmdir now.
  3328. * We also update the inode stats of the parent dirs here.
  3329. */
  3330. list_for_each_entry(cur, &check_dirs, list) {
  3331. /*
  3332. * In case we had refs into dirs that were not processed yet,
  3333. * we don't need to do the utime and rmdir logic for these dirs.
  3334. * The dir will be processed later.
  3335. */
  3336. if (cur->dir > sctx->cur_ino)
  3337. continue;
  3338. ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
  3339. if (ret < 0)
  3340. goto out;
  3341. if (ret == inode_state_did_create ||
  3342. ret == inode_state_no_change) {
  3343. /* TODO delayed utimes */
  3344. ret = send_utimes(sctx, cur->dir, cur->dir_gen);
  3345. if (ret < 0)
  3346. goto out;
  3347. } else if (ret == inode_state_did_delete &&
  3348. cur->dir != last_dir_ino_rm) {
  3349. ret = can_rmdir(sctx, cur->dir, cur->dir_gen,
  3350. sctx->cur_ino);
  3351. if (ret < 0)
  3352. goto out;
  3353. if (ret) {
  3354. ret = get_cur_path(sctx, cur->dir,
  3355. cur->dir_gen, valid_path);
  3356. if (ret < 0)
  3357. goto out;
  3358. ret = send_rmdir(sctx, valid_path);
  3359. if (ret < 0)
  3360. goto out;
  3361. last_dir_ino_rm = cur->dir;
  3362. }
  3363. }
  3364. }
  3365. ret = 0;
  3366. out:
  3367. __free_recorded_refs(&check_dirs);
  3368. free_recorded_refs(sctx);
  3369. fs_path_free(valid_path);
  3370. return ret;
  3371. }
  3372. static int record_ref(struct btrfs_root *root, int num, u64 dir, int index,
  3373. struct fs_path *name, void *ctx, struct list_head *refs)
  3374. {
  3375. int ret = 0;
  3376. struct send_ctx *sctx = ctx;
  3377. struct fs_path *p;
  3378. u64 gen;
  3379. p = fs_path_alloc();
  3380. if (!p)
  3381. return -ENOMEM;
  3382. ret = get_inode_info(root, dir, NULL, &gen, NULL, NULL,
  3383. NULL, NULL);
  3384. if (ret < 0)
  3385. goto out;
  3386. ret = get_cur_path(sctx, dir, gen, p);
  3387. if (ret < 0)
  3388. goto out;
  3389. ret = fs_path_add_path(p, name);
  3390. if (ret < 0)
  3391. goto out;
  3392. ret = __record_ref(refs, dir, gen, p);
  3393. out:
  3394. if (ret)
  3395. fs_path_free(p);
  3396. return ret;
  3397. }
  3398. static int __record_new_ref(int num, u64 dir, int index,
  3399. struct fs_path *name,
  3400. void *ctx)
  3401. {
  3402. struct send_ctx *sctx = ctx;
  3403. return record_ref(sctx->send_root, num, dir, index, name,
  3404. ctx, &sctx->new_refs);
  3405. }
  3406. static int __record_deleted_ref(int num, u64 dir, int index,
  3407. struct fs_path *name,
  3408. void *ctx)
  3409. {
  3410. struct send_ctx *sctx = ctx;
  3411. return record_ref(sctx->parent_root, num, dir, index, name,
  3412. ctx, &sctx->deleted_refs);
  3413. }
  3414. static int record_new_ref(struct send_ctx *sctx)
  3415. {
  3416. int ret;
  3417. ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
  3418. sctx->cmp_key, 0, __record_new_ref, sctx);
  3419. if (ret < 0)
  3420. goto out;
  3421. ret = 0;
  3422. out:
  3423. return ret;
  3424. }
  3425. static int record_deleted_ref(struct send_ctx *sctx)
  3426. {
  3427. int ret;
  3428. ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
  3429. sctx->cmp_key, 0, __record_deleted_ref, sctx);
  3430. if (ret < 0)
  3431. goto out;
  3432. ret = 0;
  3433. out:
  3434. return ret;
  3435. }
  3436. struct find_ref_ctx {
  3437. u64 dir;
  3438. u64 dir_gen;
  3439. struct btrfs_root *root;
  3440. struct fs_path *name;
  3441. int found_idx;
  3442. };
  3443. static int __find_iref(int num, u64 dir, int index,
  3444. struct fs_path *name,
  3445. void *ctx_)
  3446. {
  3447. struct find_ref_ctx *ctx = ctx_;
  3448. u64 dir_gen;
  3449. int ret;
  3450. if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
  3451. strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
  3452. /*
  3453. * To avoid doing extra lookups we'll only do this if everything
  3454. * else matches.
  3455. */
  3456. ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
  3457. NULL, NULL, NULL);
  3458. if (ret)
  3459. return ret;
  3460. if (dir_gen != ctx->dir_gen)
  3461. return 0;
  3462. ctx->found_idx = num;
  3463. return 1;
  3464. }
  3465. return 0;
  3466. }
  3467. static int find_iref(struct btrfs_root *root,
  3468. struct btrfs_path *path,
  3469. struct btrfs_key *key,
  3470. u64 dir, u64 dir_gen, struct fs_path *name)
  3471. {
  3472. int ret;
  3473. struct find_ref_ctx ctx;
  3474. ctx.dir = dir;
  3475. ctx.name = name;
  3476. ctx.dir_gen = dir_gen;
  3477. ctx.found_idx = -1;
  3478. ctx.root = root;
  3479. ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
  3480. if (ret < 0)
  3481. return ret;
  3482. if (ctx.found_idx == -1)
  3483. return -ENOENT;
  3484. return ctx.found_idx;
  3485. }
  3486. static int __record_changed_new_ref(int num, u64 dir, int index,
  3487. struct fs_path *name,
  3488. void *ctx)
  3489. {
  3490. u64 dir_gen;
  3491. int ret;
  3492. struct send_ctx *sctx = ctx;
  3493. ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
  3494. NULL, NULL, NULL);
  3495. if (ret)
  3496. return ret;
  3497. ret = find_iref(sctx->parent_root, sctx->right_path,
  3498. sctx->cmp_key, dir, dir_gen, name);
  3499. if (ret == -ENOENT)
  3500. ret = __record_new_ref(num, dir, index, name, sctx);
  3501. else if (ret > 0)
  3502. ret = 0;
  3503. return ret;
  3504. }
  3505. static int __record_changed_deleted_ref(int num, u64 dir, int index,
  3506. struct fs_path *name,
  3507. void *ctx)
  3508. {
  3509. u64 dir_gen;
  3510. int ret;
  3511. struct send_ctx *sctx = ctx;
  3512. ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
  3513. NULL, NULL, NULL);
  3514. if (ret)
  3515. return ret;
  3516. ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
  3517. dir, dir_gen, name);
  3518. if (ret == -ENOENT)
  3519. ret = __record_deleted_ref(num, dir, index, name, sctx);
  3520. else if (ret > 0)
  3521. ret = 0;
  3522. return ret;
  3523. }
  3524. static int record_changed_ref(struct send_ctx *sctx)
  3525. {
  3526. int ret = 0;
  3527. ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
  3528. sctx->cmp_key, 0, __record_changed_new_ref, sctx);
  3529. if (ret < 0)
  3530. goto out;
  3531. ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
  3532. sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
  3533. if (ret < 0)
  3534. goto out;
  3535. ret = 0;
  3536. out:
  3537. return ret;
  3538. }
  3539. /*
  3540. * Record and process all refs at once. Needed when an inode changes the
  3541. * generation number, which means that it was deleted and recreated.
  3542. */
  3543. static int process_all_refs(struct send_ctx *sctx,
  3544. enum btrfs_compare_tree_result cmd)
  3545. {
  3546. int ret;
  3547. struct btrfs_root *root;
  3548. struct btrfs_path *path;
  3549. struct btrfs_key key;
  3550. struct btrfs_key found_key;
  3551. struct extent_buffer *eb;
  3552. int slot;
  3553. iterate_inode_ref_t cb;
  3554. int pending_move = 0;
  3555. path = alloc_path_for_send();
  3556. if (!path)
  3557. return -ENOMEM;
  3558. if (cmd == BTRFS_COMPARE_TREE_NEW) {
  3559. root = sctx->send_root;
  3560. cb = __record_new_ref;
  3561. } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
  3562. root = sctx->parent_root;
  3563. cb = __record_deleted_ref;
  3564. } else {
  3565. btrfs_err(sctx->send_root->fs_info,
  3566. "Wrong command %d in process_all_refs", cmd);
  3567. ret = -EINVAL;
  3568. goto out;
  3569. }
  3570. key.objectid = sctx->cmp_key->objectid;
  3571. key.type = BTRFS_INODE_REF_KEY;
  3572. key.offset = 0;
  3573. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3574. if (ret < 0)
  3575. goto out;
  3576. while (1) {
  3577. eb = path->nodes[0];
  3578. slot = path->slots[0];
  3579. if (slot >= btrfs_header_nritems(eb)) {
  3580. ret = btrfs_next_leaf(root, path);
  3581. if (ret < 0)
  3582. goto out;
  3583. else if (ret > 0)
  3584. break;
  3585. continue;
  3586. }
  3587. btrfs_item_key_to_cpu(eb, &found_key, slot);
  3588. if (found_key.objectid != key.objectid ||
  3589. (found_key.type != BTRFS_INODE_REF_KEY &&
  3590. found_key.type != BTRFS_INODE_EXTREF_KEY))
  3591. break;
  3592. ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
  3593. if (ret < 0)
  3594. goto out;
  3595. path->slots[0]++;
  3596. }
  3597. btrfs_release_path(path);
  3598. ret = process_recorded_refs(sctx, &pending_move);
  3599. /* Only applicable to an incremental send. */
  3600. ASSERT(pending_move == 0);
  3601. out:
  3602. btrfs_free_path(path);
  3603. return ret;
  3604. }
  3605. static int send_set_xattr(struct send_ctx *sctx,
  3606. struct fs_path *path,
  3607. const char *name, int name_len,
  3608. const char *data, int data_len)
  3609. {
  3610. int ret = 0;
  3611. ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
  3612. if (ret < 0)
  3613. goto out;
  3614. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
  3615. TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
  3616. TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
  3617. ret = send_cmd(sctx);
  3618. tlv_put_failure:
  3619. out:
  3620. return ret;
  3621. }
  3622. static int send_remove_xattr(struct send_ctx *sctx,
  3623. struct fs_path *path,
  3624. const char *name, int name_len)
  3625. {
  3626. int ret = 0;
  3627. ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
  3628. if (ret < 0)
  3629. goto out;
  3630. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
  3631. TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
  3632. ret = send_cmd(sctx);
  3633. tlv_put_failure:
  3634. out:
  3635. return ret;
  3636. }
  3637. static int __process_new_xattr(int num, struct btrfs_key *di_key,
  3638. const char *name, int name_len,
  3639. const char *data, int data_len,
  3640. u8 type, void *ctx)
  3641. {
  3642. int ret;
  3643. struct send_ctx *sctx = ctx;
  3644. struct fs_path *p;
  3645. posix_acl_xattr_header dummy_acl;
  3646. p = fs_path_alloc();
  3647. if (!p)
  3648. return -ENOMEM;
  3649. /*
  3650. * This hack is needed because empty acl's are stored as zero byte
  3651. * data in xattrs. Problem with that is, that receiving these zero byte
  3652. * acl's will fail later. To fix this, we send a dummy acl list that
  3653. * only contains the version number and no entries.
  3654. */
  3655. if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
  3656. !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
  3657. if (data_len == 0) {
  3658. dummy_acl.a_version =
  3659. cpu_to_le32(POSIX_ACL_XATTR_VERSION);
  3660. data = (char *)&dummy_acl;
  3661. data_len = sizeof(dummy_acl);
  3662. }
  3663. }
  3664. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  3665. if (ret < 0)
  3666. goto out;
  3667. ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
  3668. out:
  3669. fs_path_free(p);
  3670. return ret;
  3671. }
  3672. static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
  3673. const char *name, int name_len,
  3674. const char *data, int data_len,
  3675. u8 type, void *ctx)
  3676. {
  3677. int ret;
  3678. struct send_ctx *sctx = ctx;
  3679. struct fs_path *p;
  3680. p = fs_path_alloc();
  3681. if (!p)
  3682. return -ENOMEM;
  3683. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  3684. if (ret < 0)
  3685. goto out;
  3686. ret = send_remove_xattr(sctx, p, name, name_len);
  3687. out:
  3688. fs_path_free(p);
  3689. return ret;
  3690. }
  3691. static int process_new_xattr(struct send_ctx *sctx)
  3692. {
  3693. int ret = 0;
  3694. ret = iterate_dir_item(sctx->send_root, sctx->left_path,
  3695. sctx->cmp_key, __process_new_xattr, sctx);
  3696. return ret;
  3697. }
  3698. static int process_deleted_xattr(struct send_ctx *sctx)
  3699. {
  3700. int ret;
  3701. ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
  3702. sctx->cmp_key, __process_deleted_xattr, sctx);
  3703. return ret;
  3704. }
  3705. struct find_xattr_ctx {
  3706. const char *name;
  3707. int name_len;
  3708. int found_idx;
  3709. char *found_data;
  3710. int found_data_len;
  3711. };
  3712. static int __find_xattr(int num, struct btrfs_key *di_key,
  3713. const char *name, int name_len,
  3714. const char *data, int data_len,
  3715. u8 type, void *vctx)
  3716. {
  3717. struct find_xattr_ctx *ctx = vctx;
  3718. if (name_len == ctx->name_len &&
  3719. strncmp(name, ctx->name, name_len) == 0) {
  3720. ctx->found_idx = num;
  3721. ctx->found_data_len = data_len;
  3722. ctx->found_data = kmemdup(data, data_len, GFP_NOFS);
  3723. if (!ctx->found_data)
  3724. return -ENOMEM;
  3725. return 1;
  3726. }
  3727. return 0;
  3728. }
  3729. static int find_xattr(struct btrfs_root *root,
  3730. struct btrfs_path *path,
  3731. struct btrfs_key *key,
  3732. const char *name, int name_len,
  3733. char **data, int *data_len)
  3734. {
  3735. int ret;
  3736. struct find_xattr_ctx ctx;
  3737. ctx.name = name;
  3738. ctx.name_len = name_len;
  3739. ctx.found_idx = -1;
  3740. ctx.found_data = NULL;
  3741. ctx.found_data_len = 0;
  3742. ret = iterate_dir_item(root, path, key, __find_xattr, &ctx);
  3743. if (ret < 0)
  3744. return ret;
  3745. if (ctx.found_idx == -1)
  3746. return -ENOENT;
  3747. if (data) {
  3748. *data = ctx.found_data;
  3749. *data_len = ctx.found_data_len;
  3750. } else {
  3751. kfree(ctx.found_data);
  3752. }
  3753. return ctx.found_idx;
  3754. }
  3755. static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
  3756. const char *name, int name_len,
  3757. const char *data, int data_len,
  3758. u8 type, void *ctx)
  3759. {
  3760. int ret;
  3761. struct send_ctx *sctx = ctx;
  3762. char *found_data = NULL;
  3763. int found_data_len = 0;
  3764. ret = find_xattr(sctx->parent_root, sctx->right_path,
  3765. sctx->cmp_key, name, name_len, &found_data,
  3766. &found_data_len);
  3767. if (ret == -ENOENT) {
  3768. ret = __process_new_xattr(num, di_key, name, name_len, data,
  3769. data_len, type, ctx);
  3770. } else if (ret >= 0) {
  3771. if (data_len != found_data_len ||
  3772. memcmp(data, found_data, data_len)) {
  3773. ret = __process_new_xattr(num, di_key, name, name_len,
  3774. data, data_len, type, ctx);
  3775. } else {
  3776. ret = 0;
  3777. }
  3778. }
  3779. kfree(found_data);
  3780. return ret;
  3781. }
  3782. static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
  3783. const char *name, int name_len,
  3784. const char *data, int data_len,
  3785. u8 type, void *ctx)
  3786. {
  3787. int ret;
  3788. struct send_ctx *sctx = ctx;
  3789. ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
  3790. name, name_len, NULL, NULL);
  3791. if (ret == -ENOENT)
  3792. ret = __process_deleted_xattr(num, di_key, name, name_len, data,
  3793. data_len, type, ctx);
  3794. else if (ret >= 0)
  3795. ret = 0;
  3796. return ret;
  3797. }
  3798. static int process_changed_xattr(struct send_ctx *sctx)
  3799. {
  3800. int ret = 0;
  3801. ret = iterate_dir_item(sctx->send_root, sctx->left_path,
  3802. sctx->cmp_key, __process_changed_new_xattr, sctx);
  3803. if (ret < 0)
  3804. goto out;
  3805. ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
  3806. sctx->cmp_key, __process_changed_deleted_xattr, sctx);
  3807. out:
  3808. return ret;
  3809. }
  3810. static int process_all_new_xattrs(struct send_ctx *sctx)
  3811. {
  3812. int ret;
  3813. struct btrfs_root *root;
  3814. struct btrfs_path *path;
  3815. struct btrfs_key key;
  3816. struct btrfs_key found_key;
  3817. struct extent_buffer *eb;
  3818. int slot;
  3819. path = alloc_path_for_send();
  3820. if (!path)
  3821. return -ENOMEM;
  3822. root = sctx->send_root;
  3823. key.objectid = sctx->cmp_key->objectid;
  3824. key.type = BTRFS_XATTR_ITEM_KEY;
  3825. key.offset = 0;
  3826. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3827. if (ret < 0)
  3828. goto out;
  3829. while (1) {
  3830. eb = path->nodes[0];
  3831. slot = path->slots[0];
  3832. if (slot >= btrfs_header_nritems(eb)) {
  3833. ret = btrfs_next_leaf(root, path);
  3834. if (ret < 0) {
  3835. goto out;
  3836. } else if (ret > 0) {
  3837. ret = 0;
  3838. break;
  3839. }
  3840. continue;
  3841. }
  3842. btrfs_item_key_to_cpu(eb, &found_key, slot);
  3843. if (found_key.objectid != key.objectid ||
  3844. found_key.type != key.type) {
  3845. ret = 0;
  3846. goto out;
  3847. }
  3848. ret = iterate_dir_item(root, path, &found_key,
  3849. __process_new_xattr, sctx);
  3850. if (ret < 0)
  3851. goto out;
  3852. path->slots[0]++;
  3853. }
  3854. out:
  3855. btrfs_free_path(path);
  3856. return ret;
  3857. }
  3858. static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
  3859. {
  3860. struct btrfs_root *root = sctx->send_root;
  3861. struct btrfs_fs_info *fs_info = root->fs_info;
  3862. struct inode *inode;
  3863. struct page *page;
  3864. char *addr;
  3865. struct btrfs_key key;
  3866. pgoff_t index = offset >> PAGE_CACHE_SHIFT;
  3867. pgoff_t last_index;
  3868. unsigned pg_offset = offset & ~PAGE_CACHE_MASK;
  3869. ssize_t ret = 0;
  3870. key.objectid = sctx->cur_ino;
  3871. key.type = BTRFS_INODE_ITEM_KEY;
  3872. key.offset = 0;
  3873. inode = btrfs_iget(fs_info->sb, &key, root, NULL);
  3874. if (IS_ERR(inode))
  3875. return PTR_ERR(inode);
  3876. if (offset + len > i_size_read(inode)) {
  3877. if (offset > i_size_read(inode))
  3878. len = 0;
  3879. else
  3880. len = offset - i_size_read(inode);
  3881. }
  3882. if (len == 0)
  3883. goto out;
  3884. last_index = (offset + len - 1) >> PAGE_CACHE_SHIFT;
  3885. /* initial readahead */
  3886. memset(&sctx->ra, 0, sizeof(struct file_ra_state));
  3887. file_ra_state_init(&sctx->ra, inode->i_mapping);
  3888. btrfs_force_ra(inode->i_mapping, &sctx->ra, NULL, index,
  3889. last_index - index + 1);
  3890. while (index <= last_index) {
  3891. unsigned cur_len = min_t(unsigned, len,
  3892. PAGE_CACHE_SIZE - pg_offset);
  3893. page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
  3894. if (!page) {
  3895. ret = -ENOMEM;
  3896. break;
  3897. }
  3898. if (!PageUptodate(page)) {
  3899. btrfs_readpage(NULL, page);
  3900. lock_page(page);
  3901. if (!PageUptodate(page)) {
  3902. unlock_page(page);
  3903. page_cache_release(page);
  3904. ret = -EIO;
  3905. break;
  3906. }
  3907. }
  3908. addr = kmap(page);
  3909. memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len);
  3910. kunmap(page);
  3911. unlock_page(page);
  3912. page_cache_release(page);
  3913. index++;
  3914. pg_offset = 0;
  3915. len -= cur_len;
  3916. ret += cur_len;
  3917. }
  3918. out:
  3919. iput(inode);
  3920. return ret;
  3921. }
  3922. /*
  3923. * Read some bytes from the current inode/file and send a write command to
  3924. * user space.
  3925. */
  3926. static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
  3927. {
  3928. int ret = 0;
  3929. struct fs_path *p;
  3930. ssize_t num_read = 0;
  3931. p = fs_path_alloc();
  3932. if (!p)
  3933. return -ENOMEM;
  3934. verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
  3935. num_read = fill_read_buf(sctx, offset, len);
  3936. if (num_read <= 0) {
  3937. if (num_read < 0)
  3938. ret = num_read;
  3939. goto out;
  3940. }
  3941. ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
  3942. if (ret < 0)
  3943. goto out;
  3944. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  3945. if (ret < 0)
  3946. goto out;
  3947. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  3948. TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
  3949. TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
  3950. ret = send_cmd(sctx);
  3951. tlv_put_failure:
  3952. out:
  3953. fs_path_free(p);
  3954. if (ret < 0)
  3955. return ret;
  3956. return num_read;
  3957. }
  3958. /*
  3959. * Send a clone command to user space.
  3960. */
  3961. static int send_clone(struct send_ctx *sctx,
  3962. u64 offset, u32 len,
  3963. struct clone_root *clone_root)
  3964. {
  3965. int ret = 0;
  3966. struct fs_path *p;
  3967. u64 gen;
  3968. verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
  3969. "clone_inode=%llu, clone_offset=%llu\n", offset, len,
  3970. clone_root->root->objectid, clone_root->ino,
  3971. clone_root->offset);
  3972. p = fs_path_alloc();
  3973. if (!p)
  3974. return -ENOMEM;
  3975. ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
  3976. if (ret < 0)
  3977. goto out;
  3978. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  3979. if (ret < 0)
  3980. goto out;
  3981. TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
  3982. TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
  3983. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  3984. if (clone_root->root == sctx->send_root) {
  3985. ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
  3986. &gen, NULL, NULL, NULL, NULL);
  3987. if (ret < 0)
  3988. goto out;
  3989. ret = get_cur_path(sctx, clone_root->ino, gen, p);
  3990. } else {
  3991. ret = get_inode_path(clone_root->root, clone_root->ino, p);
  3992. }
  3993. if (ret < 0)
  3994. goto out;
  3995. /*
  3996. * If the parent we're using has a received_uuid set then use that as
  3997. * our clone source as that is what we will look for when doing a
  3998. * receive.
  3999. *
  4000. * This covers the case that we create a snapshot off of a received
  4001. * subvolume and then use that as the parent and try to receive on a
  4002. * different host.
  4003. */
  4004. if (!btrfs_is_empty_uuid(clone_root->root->root_item.received_uuid))
  4005. TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
  4006. clone_root->root->root_item.received_uuid);
  4007. else
  4008. TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
  4009. clone_root->root->root_item.uuid);
  4010. TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
  4011. le64_to_cpu(clone_root->root->root_item.ctransid));
  4012. TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
  4013. TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
  4014. clone_root->offset);
  4015. ret = send_cmd(sctx);
  4016. tlv_put_failure:
  4017. out:
  4018. fs_path_free(p);
  4019. return ret;
  4020. }
  4021. /*
  4022. * Send an update extent command to user space.
  4023. */
  4024. static int send_update_extent(struct send_ctx *sctx,
  4025. u64 offset, u32 len)
  4026. {
  4027. int ret = 0;
  4028. struct fs_path *p;
  4029. p = fs_path_alloc();
  4030. if (!p)
  4031. return -ENOMEM;
  4032. ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
  4033. if (ret < 0)
  4034. goto out;
  4035. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  4036. if (ret < 0)
  4037. goto out;
  4038. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  4039. TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
  4040. TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
  4041. ret = send_cmd(sctx);
  4042. tlv_put_failure:
  4043. out:
  4044. fs_path_free(p);
  4045. return ret;
  4046. }
  4047. static int send_hole(struct send_ctx *sctx, u64 end)
  4048. {
  4049. struct fs_path *p = NULL;
  4050. u64 offset = sctx->cur_inode_last_extent;
  4051. u64 len;
  4052. int ret = 0;
  4053. if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
  4054. return send_update_extent(sctx, offset, end - offset);
  4055. p = fs_path_alloc();
  4056. if (!p)
  4057. return -ENOMEM;
  4058. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  4059. if (ret < 0)
  4060. goto tlv_put_failure;
  4061. memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE);
  4062. while (offset < end) {
  4063. len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE);
  4064. ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
  4065. if (ret < 0)
  4066. break;
  4067. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  4068. TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
  4069. TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len);
  4070. ret = send_cmd(sctx);
  4071. if (ret < 0)
  4072. break;
  4073. offset += len;
  4074. }
  4075. tlv_put_failure:
  4076. fs_path_free(p);
  4077. return ret;
  4078. }
  4079. static int send_extent_data(struct send_ctx *sctx,
  4080. const u64 offset,
  4081. const u64 len)
  4082. {
  4083. u64 sent = 0;
  4084. if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
  4085. return send_update_extent(sctx, offset, len);
  4086. while (sent < len) {
  4087. u64 size = len - sent;
  4088. int ret;
  4089. if (size > BTRFS_SEND_READ_SIZE)
  4090. size = BTRFS_SEND_READ_SIZE;
  4091. ret = send_write(sctx, offset + sent, size);
  4092. if (ret < 0)
  4093. return ret;
  4094. if (!ret)
  4095. break;
  4096. sent += ret;
  4097. }
  4098. return 0;
  4099. }
  4100. static int clone_range(struct send_ctx *sctx,
  4101. struct clone_root *clone_root,
  4102. const u64 disk_byte,
  4103. u64 data_offset,
  4104. u64 offset,
  4105. u64 len)
  4106. {
  4107. struct btrfs_path *path;
  4108. struct btrfs_key key;
  4109. int ret;
  4110. path = alloc_path_for_send();
  4111. if (!path)
  4112. return -ENOMEM;
  4113. /*
  4114. * We can't send a clone operation for the entire range if we find
  4115. * extent items in the respective range in the source file that
  4116. * refer to different extents or if we find holes.
  4117. * So check for that and do a mix of clone and regular write/copy
  4118. * operations if needed.
  4119. *
  4120. * Example:
  4121. *
  4122. * mkfs.btrfs -f /dev/sda
  4123. * mount /dev/sda /mnt
  4124. * xfs_io -f -c "pwrite -S 0xaa 0K 100K" /mnt/foo
  4125. * cp --reflink=always /mnt/foo /mnt/bar
  4126. * xfs_io -c "pwrite -S 0xbb 50K 50K" /mnt/foo
  4127. * btrfs subvolume snapshot -r /mnt /mnt/snap
  4128. *
  4129. * If when we send the snapshot and we are processing file bar (which
  4130. * has a higher inode number than foo) we blindly send a clone operation
  4131. * for the [0, 100K[ range from foo to bar, the receiver ends up getting
  4132. * a file bar that matches the content of file foo - iow, doesn't match
  4133. * the content from bar in the original filesystem.
  4134. */
  4135. key.objectid = clone_root->ino;
  4136. key.type = BTRFS_EXTENT_DATA_KEY;
  4137. key.offset = clone_root->offset;
  4138. ret = btrfs_search_slot(NULL, clone_root->root, &key, path, 0, 0);
  4139. if (ret < 0)
  4140. goto out;
  4141. if (ret > 0 && path->slots[0] > 0) {
  4142. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1);
  4143. if (key.objectid == clone_root->ino &&
  4144. key.type == BTRFS_EXTENT_DATA_KEY)
  4145. path->slots[0]--;
  4146. }
  4147. while (true) {
  4148. struct extent_buffer *leaf = path->nodes[0];
  4149. int slot = path->slots[0];
  4150. struct btrfs_file_extent_item *ei;
  4151. u8 type;
  4152. u64 ext_len;
  4153. u64 clone_len;
  4154. if (slot >= btrfs_header_nritems(leaf)) {
  4155. ret = btrfs_next_leaf(clone_root->root, path);
  4156. if (ret < 0)
  4157. goto out;
  4158. else if (ret > 0)
  4159. break;
  4160. continue;
  4161. }
  4162. btrfs_item_key_to_cpu(leaf, &key, slot);
  4163. /*
  4164. * We might have an implicit trailing hole (NO_HOLES feature
  4165. * enabled). We deal with it after leaving this loop.
  4166. */
  4167. if (key.objectid != clone_root->ino ||
  4168. key.type != BTRFS_EXTENT_DATA_KEY)
  4169. break;
  4170. ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  4171. type = btrfs_file_extent_type(leaf, ei);
  4172. if (type == BTRFS_FILE_EXTENT_INLINE) {
  4173. ext_len = btrfs_file_extent_inline_len(leaf, slot, ei);
  4174. ext_len = PAGE_CACHE_ALIGN(ext_len);
  4175. } else {
  4176. ext_len = btrfs_file_extent_num_bytes(leaf, ei);
  4177. }
  4178. if (key.offset + ext_len <= clone_root->offset)
  4179. goto next;
  4180. if (key.offset > clone_root->offset) {
  4181. /* Implicit hole, NO_HOLES feature enabled. */
  4182. u64 hole_len = key.offset - clone_root->offset;
  4183. if (hole_len > len)
  4184. hole_len = len;
  4185. ret = send_extent_data(sctx, offset, hole_len);
  4186. if (ret < 0)
  4187. goto out;
  4188. len -= hole_len;
  4189. if (len == 0)
  4190. break;
  4191. offset += hole_len;
  4192. clone_root->offset += hole_len;
  4193. data_offset += hole_len;
  4194. }
  4195. if (key.offset >= clone_root->offset + len)
  4196. break;
  4197. clone_len = min_t(u64, ext_len, len);
  4198. if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte &&
  4199. btrfs_file_extent_offset(leaf, ei) == data_offset)
  4200. ret = send_clone(sctx, offset, clone_len, clone_root);
  4201. else
  4202. ret = send_extent_data(sctx, offset, clone_len);
  4203. if (ret < 0)
  4204. goto out;
  4205. len -= clone_len;
  4206. if (len == 0)
  4207. break;
  4208. offset += clone_len;
  4209. clone_root->offset += clone_len;
  4210. data_offset += clone_len;
  4211. next:
  4212. path->slots[0]++;
  4213. }
  4214. if (len > 0)
  4215. ret = send_extent_data(sctx, offset, len);
  4216. else
  4217. ret = 0;
  4218. out:
  4219. btrfs_free_path(path);
  4220. return ret;
  4221. }
  4222. static int send_write_or_clone(struct send_ctx *sctx,
  4223. struct btrfs_path *path,
  4224. struct btrfs_key *key,
  4225. struct clone_root *clone_root)
  4226. {
  4227. int ret = 0;
  4228. struct btrfs_file_extent_item *ei;
  4229. u64 offset = key->offset;
  4230. u64 len;
  4231. u8 type;
  4232. u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
  4233. ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
  4234. struct btrfs_file_extent_item);
  4235. type = btrfs_file_extent_type(path->nodes[0], ei);
  4236. if (type == BTRFS_FILE_EXTENT_INLINE) {
  4237. len = btrfs_file_extent_inline_len(path->nodes[0],
  4238. path->slots[0], ei);
  4239. /*
  4240. * it is possible the inline item won't cover the whole page,
  4241. * but there may be items after this page. Make
  4242. * sure to send the whole thing
  4243. */
  4244. len = PAGE_CACHE_ALIGN(len);
  4245. } else {
  4246. len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
  4247. }
  4248. if (offset + len > sctx->cur_inode_size)
  4249. len = sctx->cur_inode_size - offset;
  4250. if (len == 0) {
  4251. ret = 0;
  4252. goto out;
  4253. }
  4254. if (clone_root && IS_ALIGNED(offset + len, bs)) {
  4255. u64 disk_byte;
  4256. u64 data_offset;
  4257. disk_byte = btrfs_file_extent_disk_bytenr(path->nodes[0], ei);
  4258. data_offset = btrfs_file_extent_offset(path->nodes[0], ei);
  4259. ret = clone_range(sctx, clone_root, disk_byte, data_offset,
  4260. offset, len);
  4261. } else {
  4262. ret = send_extent_data(sctx, offset, len);
  4263. }
  4264. out:
  4265. return ret;
  4266. }
  4267. static int is_extent_unchanged(struct send_ctx *sctx,
  4268. struct btrfs_path *left_path,
  4269. struct btrfs_key *ekey)
  4270. {
  4271. int ret = 0;
  4272. struct btrfs_key key;
  4273. struct btrfs_path *path = NULL;
  4274. struct extent_buffer *eb;
  4275. int slot;
  4276. struct btrfs_key found_key;
  4277. struct btrfs_file_extent_item *ei;
  4278. u64 left_disknr;
  4279. u64 right_disknr;
  4280. u64 left_offset;
  4281. u64 right_offset;
  4282. u64 left_offset_fixed;
  4283. u64 left_len;
  4284. u64 right_len;
  4285. u64 left_gen;
  4286. u64 right_gen;
  4287. u8 left_type;
  4288. u8 right_type;
  4289. path = alloc_path_for_send();
  4290. if (!path)
  4291. return -ENOMEM;
  4292. eb = left_path->nodes[0];
  4293. slot = left_path->slots[0];
  4294. ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  4295. left_type = btrfs_file_extent_type(eb, ei);
  4296. if (left_type != BTRFS_FILE_EXTENT_REG) {
  4297. ret = 0;
  4298. goto out;
  4299. }
  4300. left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
  4301. left_len = btrfs_file_extent_num_bytes(eb, ei);
  4302. left_offset = btrfs_file_extent_offset(eb, ei);
  4303. left_gen = btrfs_file_extent_generation(eb, ei);
  4304. /*
  4305. * Following comments will refer to these graphics. L is the left
  4306. * extents which we are checking at the moment. 1-8 are the right
  4307. * extents that we iterate.
  4308. *
  4309. * |-----L-----|
  4310. * |-1-|-2a-|-3-|-4-|-5-|-6-|
  4311. *
  4312. * |-----L-----|
  4313. * |--1--|-2b-|...(same as above)
  4314. *
  4315. * Alternative situation. Happens on files where extents got split.
  4316. * |-----L-----|
  4317. * |-----------7-----------|-6-|
  4318. *
  4319. * Alternative situation. Happens on files which got larger.
  4320. * |-----L-----|
  4321. * |-8-|
  4322. * Nothing follows after 8.
  4323. */
  4324. key.objectid = ekey->objectid;
  4325. key.type = BTRFS_EXTENT_DATA_KEY;
  4326. key.offset = ekey->offset;
  4327. ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
  4328. if (ret < 0)
  4329. goto out;
  4330. if (ret) {
  4331. ret = 0;
  4332. goto out;
  4333. }
  4334. /*
  4335. * Handle special case where the right side has no extents at all.
  4336. */
  4337. eb = path->nodes[0];
  4338. slot = path->slots[0];
  4339. btrfs_item_key_to_cpu(eb, &found_key, slot);
  4340. if (found_key.objectid != key.objectid ||
  4341. found_key.type != key.type) {
  4342. /* If we're a hole then just pretend nothing changed */
  4343. ret = (left_disknr) ? 0 : 1;
  4344. goto out;
  4345. }
  4346. /*
  4347. * We're now on 2a, 2b or 7.
  4348. */
  4349. key = found_key;
  4350. while (key.offset < ekey->offset + left_len) {
  4351. ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  4352. right_type = btrfs_file_extent_type(eb, ei);
  4353. if (right_type != BTRFS_FILE_EXTENT_REG &&
  4354. right_type != BTRFS_FILE_EXTENT_INLINE) {
  4355. ret = 0;
  4356. goto out;
  4357. }
  4358. right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
  4359. if (right_type == BTRFS_FILE_EXTENT_INLINE) {
  4360. right_len = btrfs_file_extent_inline_len(eb, slot, ei);
  4361. right_len = PAGE_ALIGN(right_len);
  4362. } else {
  4363. right_len = btrfs_file_extent_num_bytes(eb, ei);
  4364. }
  4365. right_offset = btrfs_file_extent_offset(eb, ei);
  4366. right_gen = btrfs_file_extent_generation(eb, ei);
  4367. /*
  4368. * Are we at extent 8? If yes, we know the extent is changed.
  4369. * This may only happen on the first iteration.
  4370. */
  4371. if (found_key.offset + right_len <= ekey->offset) {
  4372. /* If we're a hole just pretend nothing changed */
  4373. ret = (left_disknr) ? 0 : 1;
  4374. goto out;
  4375. }
  4376. /*
  4377. * We just wanted to see if when we have an inline extent, what
  4378. * follows it is a regular extent (wanted to check the above
  4379. * condition for inline extents too). This should normally not
  4380. * happen but it's possible for example when we have an inline
  4381. * compressed extent representing data with a size matching
  4382. * the page size (currently the same as sector size).
  4383. */
  4384. if (right_type == BTRFS_FILE_EXTENT_INLINE) {
  4385. ret = 0;
  4386. goto out;
  4387. }
  4388. left_offset_fixed = left_offset;
  4389. if (key.offset < ekey->offset) {
  4390. /* Fix the right offset for 2a and 7. */
  4391. right_offset += ekey->offset - key.offset;
  4392. } else {
  4393. /* Fix the left offset for all behind 2a and 2b */
  4394. left_offset_fixed += key.offset - ekey->offset;
  4395. }
  4396. /*
  4397. * Check if we have the same extent.
  4398. */
  4399. if (left_disknr != right_disknr ||
  4400. left_offset_fixed != right_offset ||
  4401. left_gen != right_gen) {
  4402. ret = 0;
  4403. goto out;
  4404. }
  4405. /*
  4406. * Go to the next extent.
  4407. */
  4408. ret = btrfs_next_item(sctx->parent_root, path);
  4409. if (ret < 0)
  4410. goto out;
  4411. if (!ret) {
  4412. eb = path->nodes[0];
  4413. slot = path->slots[0];
  4414. btrfs_item_key_to_cpu(eb, &found_key, slot);
  4415. }
  4416. if (ret || found_key.objectid != key.objectid ||
  4417. found_key.type != key.type) {
  4418. key.offset += right_len;
  4419. break;
  4420. }
  4421. if (found_key.offset != key.offset + right_len) {
  4422. ret = 0;
  4423. goto out;
  4424. }
  4425. key = found_key;
  4426. }
  4427. /*
  4428. * We're now behind the left extent (treat as unchanged) or at the end
  4429. * of the right side (treat as changed).
  4430. */
  4431. if (key.offset >= ekey->offset + left_len)
  4432. ret = 1;
  4433. else
  4434. ret = 0;
  4435. out:
  4436. btrfs_free_path(path);
  4437. return ret;
  4438. }
  4439. static int get_last_extent(struct send_ctx *sctx, u64 offset)
  4440. {
  4441. struct btrfs_path *path;
  4442. struct btrfs_root *root = sctx->send_root;
  4443. struct btrfs_file_extent_item *fi;
  4444. struct btrfs_key key;
  4445. u64 extent_end;
  4446. u8 type;
  4447. int ret;
  4448. path = alloc_path_for_send();
  4449. if (!path)
  4450. return -ENOMEM;
  4451. sctx->cur_inode_last_extent = 0;
  4452. key.objectid = sctx->cur_ino;
  4453. key.type = BTRFS_EXTENT_DATA_KEY;
  4454. key.offset = offset;
  4455. ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
  4456. if (ret < 0)
  4457. goto out;
  4458. ret = 0;
  4459. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  4460. if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
  4461. goto out;
  4462. fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
  4463. struct btrfs_file_extent_item);
  4464. type = btrfs_file_extent_type(path->nodes[0], fi);
  4465. if (type == BTRFS_FILE_EXTENT_INLINE) {
  4466. u64 size = btrfs_file_extent_inline_len(path->nodes[0],
  4467. path->slots[0], fi);
  4468. extent_end = ALIGN(key.offset + size,
  4469. sctx->send_root->sectorsize);
  4470. } else {
  4471. extent_end = key.offset +
  4472. btrfs_file_extent_num_bytes(path->nodes[0], fi);
  4473. }
  4474. sctx->cur_inode_last_extent = extent_end;
  4475. out:
  4476. btrfs_free_path(path);
  4477. return ret;
  4478. }
  4479. static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
  4480. struct btrfs_key *key)
  4481. {
  4482. struct btrfs_file_extent_item *fi;
  4483. u64 extent_end;
  4484. u8 type;
  4485. int ret = 0;
  4486. if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
  4487. return 0;
  4488. if (sctx->cur_inode_last_extent == (u64)-1) {
  4489. ret = get_last_extent(sctx, key->offset - 1);
  4490. if (ret)
  4491. return ret;
  4492. }
  4493. fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
  4494. struct btrfs_file_extent_item);
  4495. type = btrfs_file_extent_type(path->nodes[0], fi);
  4496. if (type == BTRFS_FILE_EXTENT_INLINE) {
  4497. u64 size = btrfs_file_extent_inline_len(path->nodes[0],
  4498. path->slots[0], fi);
  4499. extent_end = ALIGN(key->offset + size,
  4500. sctx->send_root->sectorsize);
  4501. } else {
  4502. extent_end = key->offset +
  4503. btrfs_file_extent_num_bytes(path->nodes[0], fi);
  4504. }
  4505. if (path->slots[0] == 0 &&
  4506. sctx->cur_inode_last_extent < key->offset) {
  4507. /*
  4508. * We might have skipped entire leafs that contained only
  4509. * file extent items for our current inode. These leafs have
  4510. * a generation number smaller (older) than the one in the
  4511. * current leaf and the leaf our last extent came from, and
  4512. * are located between these 2 leafs.
  4513. */
  4514. ret = get_last_extent(sctx, key->offset - 1);
  4515. if (ret)
  4516. return ret;
  4517. }
  4518. if (sctx->cur_inode_last_extent < key->offset)
  4519. ret = send_hole(sctx, key->offset);
  4520. sctx->cur_inode_last_extent = extent_end;
  4521. return ret;
  4522. }
  4523. static int process_extent(struct send_ctx *sctx,
  4524. struct btrfs_path *path,
  4525. struct btrfs_key *key)
  4526. {
  4527. struct clone_root *found_clone = NULL;
  4528. int ret = 0;
  4529. if (S_ISLNK(sctx->cur_inode_mode))
  4530. return 0;
  4531. if (sctx->parent_root && !sctx->cur_inode_new) {
  4532. ret = is_extent_unchanged(sctx, path, key);
  4533. if (ret < 0)
  4534. goto out;
  4535. if (ret) {
  4536. ret = 0;
  4537. goto out_hole;
  4538. }
  4539. } else {
  4540. struct btrfs_file_extent_item *ei;
  4541. u8 type;
  4542. ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
  4543. struct btrfs_file_extent_item);
  4544. type = btrfs_file_extent_type(path->nodes[0], ei);
  4545. if (type == BTRFS_FILE_EXTENT_PREALLOC ||
  4546. type == BTRFS_FILE_EXTENT_REG) {
  4547. /*
  4548. * The send spec does not have a prealloc command yet,
  4549. * so just leave a hole for prealloc'ed extents until
  4550. * we have enough commands queued up to justify rev'ing
  4551. * the send spec.
  4552. */
  4553. if (type == BTRFS_FILE_EXTENT_PREALLOC) {
  4554. ret = 0;
  4555. goto out;
  4556. }
  4557. /* Have a hole, just skip it. */
  4558. if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
  4559. ret = 0;
  4560. goto out;
  4561. }
  4562. }
  4563. }
  4564. ret = find_extent_clone(sctx, path, key->objectid, key->offset,
  4565. sctx->cur_inode_size, &found_clone);
  4566. if (ret != -ENOENT && ret < 0)
  4567. goto out;
  4568. ret = send_write_or_clone(sctx, path, key, found_clone);
  4569. if (ret)
  4570. goto out;
  4571. out_hole:
  4572. ret = maybe_send_hole(sctx, path, key);
  4573. out:
  4574. return ret;
  4575. }
  4576. static int process_all_extents(struct send_ctx *sctx)
  4577. {
  4578. int ret;
  4579. struct btrfs_root *root;
  4580. struct btrfs_path *path;
  4581. struct btrfs_key key;
  4582. struct btrfs_key found_key;
  4583. struct extent_buffer *eb;
  4584. int slot;
  4585. root = sctx->send_root;
  4586. path = alloc_path_for_send();
  4587. if (!path)
  4588. return -ENOMEM;
  4589. key.objectid = sctx->cmp_key->objectid;
  4590. key.type = BTRFS_EXTENT_DATA_KEY;
  4591. key.offset = 0;
  4592. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4593. if (ret < 0)
  4594. goto out;
  4595. while (1) {
  4596. eb = path->nodes[0];
  4597. slot = path->slots[0];
  4598. if (slot >= btrfs_header_nritems(eb)) {
  4599. ret = btrfs_next_leaf(root, path);
  4600. if (ret < 0) {
  4601. goto out;
  4602. } else if (ret > 0) {
  4603. ret = 0;
  4604. break;
  4605. }
  4606. continue;
  4607. }
  4608. btrfs_item_key_to_cpu(eb, &found_key, slot);
  4609. if (found_key.objectid != key.objectid ||
  4610. found_key.type != key.type) {
  4611. ret = 0;
  4612. goto out;
  4613. }
  4614. ret = process_extent(sctx, path, &found_key);
  4615. if (ret < 0)
  4616. goto out;
  4617. path->slots[0]++;
  4618. }
  4619. out:
  4620. btrfs_free_path(path);
  4621. return ret;
  4622. }
  4623. static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
  4624. int *pending_move,
  4625. int *refs_processed)
  4626. {
  4627. int ret = 0;
  4628. if (sctx->cur_ino == 0)
  4629. goto out;
  4630. if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
  4631. sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
  4632. goto out;
  4633. if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
  4634. goto out;
  4635. ret = process_recorded_refs(sctx, pending_move);
  4636. if (ret < 0)
  4637. goto out;
  4638. *refs_processed = 1;
  4639. out:
  4640. return ret;
  4641. }
  4642. static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
  4643. {
  4644. int ret = 0;
  4645. u64 left_mode;
  4646. u64 left_uid;
  4647. u64 left_gid;
  4648. u64 right_mode;
  4649. u64 right_uid;
  4650. u64 right_gid;
  4651. int need_chmod = 0;
  4652. int need_chown = 0;
  4653. int pending_move = 0;
  4654. int refs_processed = 0;
  4655. ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
  4656. &refs_processed);
  4657. if (ret < 0)
  4658. goto out;
  4659. /*
  4660. * We have processed the refs and thus need to advance send_progress.
  4661. * Now, calls to get_cur_xxx will take the updated refs of the current
  4662. * inode into account.
  4663. *
  4664. * On the other hand, if our current inode is a directory and couldn't
  4665. * be moved/renamed because its parent was renamed/moved too and it has
  4666. * a higher inode number, we can only move/rename our current inode
  4667. * after we moved/renamed its parent. Therefore in this case operate on
  4668. * the old path (pre move/rename) of our current inode, and the
  4669. * move/rename will be performed later.
  4670. */
  4671. if (refs_processed && !pending_move)
  4672. sctx->send_progress = sctx->cur_ino + 1;
  4673. if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
  4674. goto out;
  4675. if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
  4676. goto out;
  4677. ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
  4678. &left_mode, &left_uid, &left_gid, NULL);
  4679. if (ret < 0)
  4680. goto out;
  4681. if (!sctx->parent_root || sctx->cur_inode_new) {
  4682. need_chown = 1;
  4683. if (!S_ISLNK(sctx->cur_inode_mode))
  4684. need_chmod = 1;
  4685. } else {
  4686. ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
  4687. NULL, NULL, &right_mode, &right_uid,
  4688. &right_gid, NULL);
  4689. if (ret < 0)
  4690. goto out;
  4691. if (left_uid != right_uid || left_gid != right_gid)
  4692. need_chown = 1;
  4693. if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
  4694. need_chmod = 1;
  4695. }
  4696. if (S_ISREG(sctx->cur_inode_mode)) {
  4697. if (need_send_hole(sctx)) {
  4698. if (sctx->cur_inode_last_extent == (u64)-1 ||
  4699. sctx->cur_inode_last_extent <
  4700. sctx->cur_inode_size) {
  4701. ret = get_last_extent(sctx, (u64)-1);
  4702. if (ret)
  4703. goto out;
  4704. }
  4705. if (sctx->cur_inode_last_extent <
  4706. sctx->cur_inode_size) {
  4707. ret = send_hole(sctx, sctx->cur_inode_size);
  4708. if (ret)
  4709. goto out;
  4710. }
  4711. }
  4712. ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  4713. sctx->cur_inode_size);
  4714. if (ret < 0)
  4715. goto out;
  4716. }
  4717. if (need_chown) {
  4718. ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  4719. left_uid, left_gid);
  4720. if (ret < 0)
  4721. goto out;
  4722. }
  4723. if (need_chmod) {
  4724. ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  4725. left_mode);
  4726. if (ret < 0)
  4727. goto out;
  4728. }
  4729. /*
  4730. * If other directory inodes depended on our current directory
  4731. * inode's move/rename, now do their move/rename operations.
  4732. */
  4733. if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
  4734. ret = apply_children_dir_moves(sctx);
  4735. if (ret)
  4736. goto out;
  4737. /*
  4738. * Need to send that every time, no matter if it actually
  4739. * changed between the two trees as we have done changes to
  4740. * the inode before. If our inode is a directory and it's
  4741. * waiting to be moved/renamed, we will send its utimes when
  4742. * it's moved/renamed, therefore we don't need to do it here.
  4743. */
  4744. sctx->send_progress = sctx->cur_ino + 1;
  4745. ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
  4746. if (ret < 0)
  4747. goto out;
  4748. }
  4749. out:
  4750. return ret;
  4751. }
  4752. static int changed_inode(struct send_ctx *sctx,
  4753. enum btrfs_compare_tree_result result)
  4754. {
  4755. int ret = 0;
  4756. struct btrfs_key *key = sctx->cmp_key;
  4757. struct btrfs_inode_item *left_ii = NULL;
  4758. struct btrfs_inode_item *right_ii = NULL;
  4759. u64 left_gen = 0;
  4760. u64 right_gen = 0;
  4761. sctx->cur_ino = key->objectid;
  4762. sctx->cur_inode_new_gen = 0;
  4763. sctx->cur_inode_last_extent = (u64)-1;
  4764. /*
  4765. * Set send_progress to current inode. This will tell all get_cur_xxx
  4766. * functions that the current inode's refs are not updated yet. Later,
  4767. * when process_recorded_refs is finished, it is set to cur_ino + 1.
  4768. */
  4769. sctx->send_progress = sctx->cur_ino;
  4770. if (result == BTRFS_COMPARE_TREE_NEW ||
  4771. result == BTRFS_COMPARE_TREE_CHANGED) {
  4772. left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
  4773. sctx->left_path->slots[0],
  4774. struct btrfs_inode_item);
  4775. left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
  4776. left_ii);
  4777. } else {
  4778. right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
  4779. sctx->right_path->slots[0],
  4780. struct btrfs_inode_item);
  4781. right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
  4782. right_ii);
  4783. }
  4784. if (result == BTRFS_COMPARE_TREE_CHANGED) {
  4785. right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
  4786. sctx->right_path->slots[0],
  4787. struct btrfs_inode_item);
  4788. right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
  4789. right_ii);
  4790. /*
  4791. * The cur_ino = root dir case is special here. We can't treat
  4792. * the inode as deleted+reused because it would generate a
  4793. * stream that tries to delete/mkdir the root dir.
  4794. */
  4795. if (left_gen != right_gen &&
  4796. sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
  4797. sctx->cur_inode_new_gen = 1;
  4798. }
  4799. if (result == BTRFS_COMPARE_TREE_NEW) {
  4800. sctx->cur_inode_gen = left_gen;
  4801. sctx->cur_inode_new = 1;
  4802. sctx->cur_inode_deleted = 0;
  4803. sctx->cur_inode_size = btrfs_inode_size(
  4804. sctx->left_path->nodes[0], left_ii);
  4805. sctx->cur_inode_mode = btrfs_inode_mode(
  4806. sctx->left_path->nodes[0], left_ii);
  4807. sctx->cur_inode_rdev = btrfs_inode_rdev(
  4808. sctx->left_path->nodes[0], left_ii);
  4809. if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
  4810. ret = send_create_inode_if_needed(sctx);
  4811. } else if (result == BTRFS_COMPARE_TREE_DELETED) {
  4812. sctx->cur_inode_gen = right_gen;
  4813. sctx->cur_inode_new = 0;
  4814. sctx->cur_inode_deleted = 1;
  4815. sctx->cur_inode_size = btrfs_inode_size(
  4816. sctx->right_path->nodes[0], right_ii);
  4817. sctx->cur_inode_mode = btrfs_inode_mode(
  4818. sctx->right_path->nodes[0], right_ii);
  4819. } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
  4820. /*
  4821. * We need to do some special handling in case the inode was
  4822. * reported as changed with a changed generation number. This
  4823. * means that the original inode was deleted and new inode
  4824. * reused the same inum. So we have to treat the old inode as
  4825. * deleted and the new one as new.
  4826. */
  4827. if (sctx->cur_inode_new_gen) {
  4828. /*
  4829. * First, process the inode as if it was deleted.
  4830. */
  4831. sctx->cur_inode_gen = right_gen;
  4832. sctx->cur_inode_new = 0;
  4833. sctx->cur_inode_deleted = 1;
  4834. sctx->cur_inode_size = btrfs_inode_size(
  4835. sctx->right_path->nodes[0], right_ii);
  4836. sctx->cur_inode_mode = btrfs_inode_mode(
  4837. sctx->right_path->nodes[0], right_ii);
  4838. ret = process_all_refs(sctx,
  4839. BTRFS_COMPARE_TREE_DELETED);
  4840. if (ret < 0)
  4841. goto out;
  4842. /*
  4843. * Now process the inode as if it was new.
  4844. */
  4845. sctx->cur_inode_gen = left_gen;
  4846. sctx->cur_inode_new = 1;
  4847. sctx->cur_inode_deleted = 0;
  4848. sctx->cur_inode_size = btrfs_inode_size(
  4849. sctx->left_path->nodes[0], left_ii);
  4850. sctx->cur_inode_mode = btrfs_inode_mode(
  4851. sctx->left_path->nodes[0], left_ii);
  4852. sctx->cur_inode_rdev = btrfs_inode_rdev(
  4853. sctx->left_path->nodes[0], left_ii);
  4854. ret = send_create_inode_if_needed(sctx);
  4855. if (ret < 0)
  4856. goto out;
  4857. ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
  4858. if (ret < 0)
  4859. goto out;
  4860. /*
  4861. * Advance send_progress now as we did not get into
  4862. * process_recorded_refs_if_needed in the new_gen case.
  4863. */
  4864. sctx->send_progress = sctx->cur_ino + 1;
  4865. /*
  4866. * Now process all extents and xattrs of the inode as if
  4867. * they were all new.
  4868. */
  4869. ret = process_all_extents(sctx);
  4870. if (ret < 0)
  4871. goto out;
  4872. ret = process_all_new_xattrs(sctx);
  4873. if (ret < 0)
  4874. goto out;
  4875. } else {
  4876. sctx->cur_inode_gen = left_gen;
  4877. sctx->cur_inode_new = 0;
  4878. sctx->cur_inode_new_gen = 0;
  4879. sctx->cur_inode_deleted = 0;
  4880. sctx->cur_inode_size = btrfs_inode_size(
  4881. sctx->left_path->nodes[0], left_ii);
  4882. sctx->cur_inode_mode = btrfs_inode_mode(
  4883. sctx->left_path->nodes[0], left_ii);
  4884. }
  4885. }
  4886. out:
  4887. return ret;
  4888. }
  4889. /*
  4890. * We have to process new refs before deleted refs, but compare_trees gives us
  4891. * the new and deleted refs mixed. To fix this, we record the new/deleted refs
  4892. * first and later process them in process_recorded_refs.
  4893. * For the cur_inode_new_gen case, we skip recording completely because
  4894. * changed_inode did already initiate processing of refs. The reason for this is
  4895. * that in this case, compare_tree actually compares the refs of 2 different
  4896. * inodes. To fix this, process_all_refs is used in changed_inode to handle all
  4897. * refs of the right tree as deleted and all refs of the left tree as new.
  4898. */
  4899. static int changed_ref(struct send_ctx *sctx,
  4900. enum btrfs_compare_tree_result result)
  4901. {
  4902. int ret = 0;
  4903. BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
  4904. if (!sctx->cur_inode_new_gen &&
  4905. sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
  4906. if (result == BTRFS_COMPARE_TREE_NEW)
  4907. ret = record_new_ref(sctx);
  4908. else if (result == BTRFS_COMPARE_TREE_DELETED)
  4909. ret = record_deleted_ref(sctx);
  4910. else if (result == BTRFS_COMPARE_TREE_CHANGED)
  4911. ret = record_changed_ref(sctx);
  4912. }
  4913. return ret;
  4914. }
  4915. /*
  4916. * Process new/deleted/changed xattrs. We skip processing in the
  4917. * cur_inode_new_gen case because changed_inode did already initiate processing
  4918. * of xattrs. The reason is the same as in changed_ref
  4919. */
  4920. static int changed_xattr(struct send_ctx *sctx,
  4921. enum btrfs_compare_tree_result result)
  4922. {
  4923. int ret = 0;
  4924. BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
  4925. if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
  4926. if (result == BTRFS_COMPARE_TREE_NEW)
  4927. ret = process_new_xattr(sctx);
  4928. else if (result == BTRFS_COMPARE_TREE_DELETED)
  4929. ret = process_deleted_xattr(sctx);
  4930. else if (result == BTRFS_COMPARE_TREE_CHANGED)
  4931. ret = process_changed_xattr(sctx);
  4932. }
  4933. return ret;
  4934. }
  4935. /*
  4936. * Process new/deleted/changed extents. We skip processing in the
  4937. * cur_inode_new_gen case because changed_inode did already initiate processing
  4938. * of extents. The reason is the same as in changed_ref
  4939. */
  4940. static int changed_extent(struct send_ctx *sctx,
  4941. enum btrfs_compare_tree_result result)
  4942. {
  4943. int ret = 0;
  4944. BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
  4945. if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
  4946. if (result != BTRFS_COMPARE_TREE_DELETED)
  4947. ret = process_extent(sctx, sctx->left_path,
  4948. sctx->cmp_key);
  4949. }
  4950. return ret;
  4951. }
  4952. static int dir_changed(struct send_ctx *sctx, u64 dir)
  4953. {
  4954. u64 orig_gen, new_gen;
  4955. int ret;
  4956. ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
  4957. NULL, NULL);
  4958. if (ret)
  4959. return ret;
  4960. ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
  4961. NULL, NULL, NULL);
  4962. if (ret)
  4963. return ret;
  4964. return (orig_gen != new_gen) ? 1 : 0;
  4965. }
  4966. static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
  4967. struct btrfs_key *key)
  4968. {
  4969. struct btrfs_inode_extref *extref;
  4970. struct extent_buffer *leaf;
  4971. u64 dirid = 0, last_dirid = 0;
  4972. unsigned long ptr;
  4973. u32 item_size;
  4974. u32 cur_offset = 0;
  4975. int ref_name_len;
  4976. int ret = 0;
  4977. /* Easy case, just check this one dirid */
  4978. if (key->type == BTRFS_INODE_REF_KEY) {
  4979. dirid = key->offset;
  4980. ret = dir_changed(sctx, dirid);
  4981. goto out;
  4982. }
  4983. leaf = path->nodes[0];
  4984. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  4985. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  4986. while (cur_offset < item_size) {
  4987. extref = (struct btrfs_inode_extref *)(ptr +
  4988. cur_offset);
  4989. dirid = btrfs_inode_extref_parent(leaf, extref);
  4990. ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
  4991. cur_offset += ref_name_len + sizeof(*extref);
  4992. if (dirid == last_dirid)
  4993. continue;
  4994. ret = dir_changed(sctx, dirid);
  4995. if (ret)
  4996. break;
  4997. last_dirid = dirid;
  4998. }
  4999. out:
  5000. return ret;
  5001. }
  5002. /*
  5003. * Updates compare related fields in sctx and simply forwards to the actual
  5004. * changed_xxx functions.
  5005. */
  5006. static int changed_cb(struct btrfs_root *left_root,
  5007. struct btrfs_root *right_root,
  5008. struct btrfs_path *left_path,
  5009. struct btrfs_path *right_path,
  5010. struct btrfs_key *key,
  5011. enum btrfs_compare_tree_result result,
  5012. void *ctx)
  5013. {
  5014. int ret = 0;
  5015. struct send_ctx *sctx = ctx;
  5016. if (result == BTRFS_COMPARE_TREE_SAME) {
  5017. if (key->type == BTRFS_INODE_REF_KEY ||
  5018. key->type == BTRFS_INODE_EXTREF_KEY) {
  5019. ret = compare_refs(sctx, left_path, key);
  5020. if (!ret)
  5021. return 0;
  5022. if (ret < 0)
  5023. return ret;
  5024. } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
  5025. return maybe_send_hole(sctx, left_path, key);
  5026. } else {
  5027. return 0;
  5028. }
  5029. result = BTRFS_COMPARE_TREE_CHANGED;
  5030. ret = 0;
  5031. }
  5032. sctx->left_path = left_path;
  5033. sctx->right_path = right_path;
  5034. sctx->cmp_key = key;
  5035. ret = finish_inode_if_needed(sctx, 0);
  5036. if (ret < 0)
  5037. goto out;
  5038. /* Ignore non-FS objects */
  5039. if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
  5040. key->objectid == BTRFS_FREE_SPACE_OBJECTID)
  5041. goto out;
  5042. if (key->type == BTRFS_INODE_ITEM_KEY)
  5043. ret = changed_inode(sctx, result);
  5044. else if (key->type == BTRFS_INODE_REF_KEY ||
  5045. key->type == BTRFS_INODE_EXTREF_KEY)
  5046. ret = changed_ref(sctx, result);
  5047. else if (key->type == BTRFS_XATTR_ITEM_KEY)
  5048. ret = changed_xattr(sctx, result);
  5049. else if (key->type == BTRFS_EXTENT_DATA_KEY)
  5050. ret = changed_extent(sctx, result);
  5051. out:
  5052. return ret;
  5053. }
  5054. static int full_send_tree(struct send_ctx *sctx)
  5055. {
  5056. int ret;
  5057. struct btrfs_root *send_root = sctx->send_root;
  5058. struct btrfs_key key;
  5059. struct btrfs_key found_key;
  5060. struct btrfs_path *path;
  5061. struct extent_buffer *eb;
  5062. int slot;
  5063. path = alloc_path_for_send();
  5064. if (!path)
  5065. return -ENOMEM;
  5066. key.objectid = BTRFS_FIRST_FREE_OBJECTID;
  5067. key.type = BTRFS_INODE_ITEM_KEY;
  5068. key.offset = 0;
  5069. ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
  5070. if (ret < 0)
  5071. goto out;
  5072. if (ret)
  5073. goto out_finish;
  5074. while (1) {
  5075. eb = path->nodes[0];
  5076. slot = path->slots[0];
  5077. btrfs_item_key_to_cpu(eb, &found_key, slot);
  5078. ret = changed_cb(send_root, NULL, path, NULL,
  5079. &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
  5080. if (ret < 0)
  5081. goto out;
  5082. key.objectid = found_key.objectid;
  5083. key.type = found_key.type;
  5084. key.offset = found_key.offset + 1;
  5085. ret = btrfs_next_item(send_root, path);
  5086. if (ret < 0)
  5087. goto out;
  5088. if (ret) {
  5089. ret = 0;
  5090. break;
  5091. }
  5092. }
  5093. out_finish:
  5094. ret = finish_inode_if_needed(sctx, 1);
  5095. out:
  5096. btrfs_free_path(path);
  5097. return ret;
  5098. }
  5099. static int send_subvol(struct send_ctx *sctx)
  5100. {
  5101. int ret;
  5102. if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
  5103. ret = send_header(sctx);
  5104. if (ret < 0)
  5105. goto out;
  5106. }
  5107. ret = send_subvol_begin(sctx);
  5108. if (ret < 0)
  5109. goto out;
  5110. if (sctx->parent_root) {
  5111. ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
  5112. changed_cb, sctx);
  5113. if (ret < 0)
  5114. goto out;
  5115. ret = finish_inode_if_needed(sctx, 1);
  5116. if (ret < 0)
  5117. goto out;
  5118. } else {
  5119. ret = full_send_tree(sctx);
  5120. if (ret < 0)
  5121. goto out;
  5122. }
  5123. out:
  5124. free_recorded_refs(sctx);
  5125. return ret;
  5126. }
  5127. /*
  5128. * If orphan cleanup did remove any orphans from a root, it means the tree
  5129. * was modified and therefore the commit root is not the same as the current
  5130. * root anymore. This is a problem, because send uses the commit root and
  5131. * therefore can see inode items that don't exist in the current root anymore,
  5132. * and for example make calls to btrfs_iget, which will do tree lookups based
  5133. * on the current root and not on the commit root. Those lookups will fail,
  5134. * returning a -ESTALE error, and making send fail with that error. So make
  5135. * sure a send does not see any orphans we have just removed, and that it will
  5136. * see the same inodes regardless of whether a transaction commit happened
  5137. * before it started (meaning that the commit root will be the same as the
  5138. * current root) or not.
  5139. */
  5140. static int ensure_commit_roots_uptodate(struct send_ctx *sctx)
  5141. {
  5142. int i;
  5143. struct btrfs_trans_handle *trans = NULL;
  5144. again:
  5145. if (sctx->parent_root &&
  5146. sctx->parent_root->node != sctx->parent_root->commit_root)
  5147. goto commit_trans;
  5148. for (i = 0; i < sctx->clone_roots_cnt; i++)
  5149. if (sctx->clone_roots[i].root->node !=
  5150. sctx->clone_roots[i].root->commit_root)
  5151. goto commit_trans;
  5152. if (trans)
  5153. return btrfs_end_transaction(trans, sctx->send_root);
  5154. return 0;
  5155. commit_trans:
  5156. /* Use any root, all fs roots will get their commit roots updated. */
  5157. if (!trans) {
  5158. trans = btrfs_join_transaction(sctx->send_root);
  5159. if (IS_ERR(trans))
  5160. return PTR_ERR(trans);
  5161. goto again;
  5162. }
  5163. return btrfs_commit_transaction(trans, sctx->send_root);
  5164. }
  5165. static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
  5166. {
  5167. spin_lock(&root->root_item_lock);
  5168. root->send_in_progress--;
  5169. /*
  5170. * Not much left to do, we don't know why it's unbalanced and
  5171. * can't blindly reset it to 0.
  5172. */
  5173. if (root->send_in_progress < 0)
  5174. btrfs_err(root->fs_info,
  5175. "send_in_progres unbalanced %d root %llu",
  5176. root->send_in_progress, root->root_key.objectid);
  5177. spin_unlock(&root->root_item_lock);
  5178. }
  5179. long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
  5180. {
  5181. int ret = 0;
  5182. struct btrfs_root *send_root;
  5183. struct btrfs_root *clone_root;
  5184. struct btrfs_fs_info *fs_info;
  5185. struct btrfs_ioctl_send_args *arg = NULL;
  5186. struct btrfs_key key;
  5187. struct send_ctx *sctx = NULL;
  5188. u32 i;
  5189. u64 *clone_sources_tmp = NULL;
  5190. int clone_sources_to_rollback = 0;
  5191. int sort_clone_roots = 0;
  5192. int index;
  5193. if (!capable(CAP_SYS_ADMIN))
  5194. return -EPERM;
  5195. send_root = BTRFS_I(file_inode(mnt_file))->root;
  5196. fs_info = send_root->fs_info;
  5197. /*
  5198. * The subvolume must remain read-only during send, protect against
  5199. * making it RW. This also protects against deletion.
  5200. */
  5201. spin_lock(&send_root->root_item_lock);
  5202. send_root->send_in_progress++;
  5203. spin_unlock(&send_root->root_item_lock);
  5204. /*
  5205. * This is done when we lookup the root, it should already be complete
  5206. * by the time we get here.
  5207. */
  5208. WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
  5209. /*
  5210. * Userspace tools do the checks and warn the user if it's
  5211. * not RO.
  5212. */
  5213. if (!btrfs_root_readonly(send_root)) {
  5214. ret = -EPERM;
  5215. goto out;
  5216. }
  5217. arg = memdup_user(arg_, sizeof(*arg));
  5218. if (IS_ERR(arg)) {
  5219. ret = PTR_ERR(arg);
  5220. arg = NULL;
  5221. goto out;
  5222. }
  5223. if (!access_ok(VERIFY_READ, arg->clone_sources,
  5224. sizeof(*arg->clone_sources) *
  5225. arg->clone_sources_count)) {
  5226. ret = -EFAULT;
  5227. goto out;
  5228. }
  5229. if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
  5230. ret = -EINVAL;
  5231. goto out;
  5232. }
  5233. sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
  5234. if (!sctx) {
  5235. ret = -ENOMEM;
  5236. goto out;
  5237. }
  5238. INIT_LIST_HEAD(&sctx->new_refs);
  5239. INIT_LIST_HEAD(&sctx->deleted_refs);
  5240. INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
  5241. INIT_LIST_HEAD(&sctx->name_cache_list);
  5242. sctx->flags = arg->flags;
  5243. sctx->send_filp = fget(arg->send_fd);
  5244. if (!sctx->send_filp) {
  5245. ret = -EBADF;
  5246. goto out;
  5247. }
  5248. sctx->send_root = send_root;
  5249. /*
  5250. * Unlikely but possible, if the subvolume is marked for deletion but
  5251. * is slow to remove the directory entry, send can still be started
  5252. */
  5253. if (btrfs_root_dead(sctx->send_root)) {
  5254. ret = -EPERM;
  5255. goto out;
  5256. }
  5257. sctx->clone_roots_cnt = arg->clone_sources_count;
  5258. sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
  5259. sctx->send_buf = vmalloc(sctx->send_max_size);
  5260. if (!sctx->send_buf) {
  5261. ret = -ENOMEM;
  5262. goto out;
  5263. }
  5264. sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
  5265. if (!sctx->read_buf) {
  5266. ret = -ENOMEM;
  5267. goto out;
  5268. }
  5269. sctx->pending_dir_moves = RB_ROOT;
  5270. sctx->waiting_dir_moves = RB_ROOT;
  5271. sctx->orphan_dirs = RB_ROOT;
  5272. sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
  5273. (arg->clone_sources_count + 1));
  5274. if (!sctx->clone_roots) {
  5275. ret = -ENOMEM;
  5276. goto out;
  5277. }
  5278. if (arg->clone_sources_count) {
  5279. clone_sources_tmp = vmalloc(arg->clone_sources_count *
  5280. sizeof(*arg->clone_sources));
  5281. if (!clone_sources_tmp) {
  5282. ret = -ENOMEM;
  5283. goto out;
  5284. }
  5285. ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
  5286. arg->clone_sources_count *
  5287. sizeof(*arg->clone_sources));
  5288. if (ret) {
  5289. ret = -EFAULT;
  5290. goto out;
  5291. }
  5292. for (i = 0; i < arg->clone_sources_count; i++) {
  5293. key.objectid = clone_sources_tmp[i];
  5294. key.type = BTRFS_ROOT_ITEM_KEY;
  5295. key.offset = (u64)-1;
  5296. index = srcu_read_lock(&fs_info->subvol_srcu);
  5297. clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
  5298. if (IS_ERR(clone_root)) {
  5299. srcu_read_unlock(&fs_info->subvol_srcu, index);
  5300. ret = PTR_ERR(clone_root);
  5301. goto out;
  5302. }
  5303. spin_lock(&clone_root->root_item_lock);
  5304. if (!btrfs_root_readonly(clone_root) ||
  5305. btrfs_root_dead(clone_root)) {
  5306. spin_unlock(&clone_root->root_item_lock);
  5307. srcu_read_unlock(&fs_info->subvol_srcu, index);
  5308. ret = -EPERM;
  5309. goto out;
  5310. }
  5311. clone_root->send_in_progress++;
  5312. spin_unlock(&clone_root->root_item_lock);
  5313. srcu_read_unlock(&fs_info->subvol_srcu, index);
  5314. sctx->clone_roots[i].root = clone_root;
  5315. clone_sources_to_rollback = i + 1;
  5316. }
  5317. vfree(clone_sources_tmp);
  5318. clone_sources_tmp = NULL;
  5319. }
  5320. if (arg->parent_root) {
  5321. key.objectid = arg->parent_root;
  5322. key.type = BTRFS_ROOT_ITEM_KEY;
  5323. key.offset = (u64)-1;
  5324. index = srcu_read_lock(&fs_info->subvol_srcu);
  5325. sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
  5326. if (IS_ERR(sctx->parent_root)) {
  5327. srcu_read_unlock(&fs_info->subvol_srcu, index);
  5328. ret = PTR_ERR(sctx->parent_root);
  5329. goto out;
  5330. }
  5331. spin_lock(&sctx->parent_root->root_item_lock);
  5332. sctx->parent_root->send_in_progress++;
  5333. if (!btrfs_root_readonly(sctx->parent_root) ||
  5334. btrfs_root_dead(sctx->parent_root)) {
  5335. spin_unlock(&sctx->parent_root->root_item_lock);
  5336. srcu_read_unlock(&fs_info->subvol_srcu, index);
  5337. ret = -EPERM;
  5338. goto out;
  5339. }
  5340. spin_unlock(&sctx->parent_root->root_item_lock);
  5341. srcu_read_unlock(&fs_info->subvol_srcu, index);
  5342. }
  5343. /*
  5344. * Clones from send_root are allowed, but only if the clone source
  5345. * is behind the current send position. This is checked while searching
  5346. * for possible clone sources.
  5347. */
  5348. sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
  5349. /* We do a bsearch later */
  5350. sort(sctx->clone_roots, sctx->clone_roots_cnt,
  5351. sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
  5352. NULL);
  5353. sort_clone_roots = 1;
  5354. ret = ensure_commit_roots_uptodate(sctx);
  5355. if (ret)
  5356. goto out;
  5357. current->journal_info = BTRFS_SEND_TRANS_STUB;
  5358. ret = send_subvol(sctx);
  5359. current->journal_info = NULL;
  5360. if (ret < 0)
  5361. goto out;
  5362. if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
  5363. ret = begin_cmd(sctx, BTRFS_SEND_C_END);
  5364. if (ret < 0)
  5365. goto out;
  5366. ret = send_cmd(sctx);
  5367. if (ret < 0)
  5368. goto out;
  5369. }
  5370. out:
  5371. WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
  5372. while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
  5373. struct rb_node *n;
  5374. struct pending_dir_move *pm;
  5375. n = rb_first(&sctx->pending_dir_moves);
  5376. pm = rb_entry(n, struct pending_dir_move, node);
  5377. while (!list_empty(&pm->list)) {
  5378. struct pending_dir_move *pm2;
  5379. pm2 = list_first_entry(&pm->list,
  5380. struct pending_dir_move, list);
  5381. free_pending_move(sctx, pm2);
  5382. }
  5383. free_pending_move(sctx, pm);
  5384. }
  5385. WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
  5386. while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
  5387. struct rb_node *n;
  5388. struct waiting_dir_move *dm;
  5389. n = rb_first(&sctx->waiting_dir_moves);
  5390. dm = rb_entry(n, struct waiting_dir_move, node);
  5391. rb_erase(&dm->node, &sctx->waiting_dir_moves);
  5392. kfree(dm);
  5393. }
  5394. WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
  5395. while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
  5396. struct rb_node *n;
  5397. struct orphan_dir_info *odi;
  5398. n = rb_first(&sctx->orphan_dirs);
  5399. odi = rb_entry(n, struct orphan_dir_info, node);
  5400. free_orphan_dir_info(sctx, odi);
  5401. }
  5402. if (sort_clone_roots) {
  5403. for (i = 0; i < sctx->clone_roots_cnt; i++)
  5404. btrfs_root_dec_send_in_progress(
  5405. sctx->clone_roots[i].root);
  5406. } else {
  5407. for (i = 0; sctx && i < clone_sources_to_rollback; i++)
  5408. btrfs_root_dec_send_in_progress(
  5409. sctx->clone_roots[i].root);
  5410. btrfs_root_dec_send_in_progress(send_root);
  5411. }
  5412. if (sctx && !IS_ERR_OR_NULL(sctx->parent_root))
  5413. btrfs_root_dec_send_in_progress(sctx->parent_root);
  5414. kfree(arg);
  5415. vfree(clone_sources_tmp);
  5416. if (sctx) {
  5417. if (sctx->send_filp)
  5418. fput(sctx->send_filp);
  5419. vfree(sctx->clone_roots);
  5420. vfree(sctx->send_buf);
  5421. vfree(sctx->read_buf);
  5422. name_cache_free(sctx);
  5423. kfree(sctx);
  5424. }
  5425. return ret;
  5426. }