send.c 146 KB

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