send.c 150 KB

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