send.c 153 KB

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