send.c 160 KB

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