send.c 145 KB

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