send.c 164 KB

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