send.c 139 KB

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