send.c 155 KB

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