send.c 163 KB

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