send.c 168 KB

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