send.c 156 KB

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