send.c 219 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521
  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/falloc.h>
  7. #include <linux/fs.h>
  8. #include <linux/file.h>
  9. #include <linux/sort.h>
  10. #include <linux/mount.h>
  11. #include <linux/xattr.h>
  12. #include <linux/posix_acl_xattr.h>
  13. #include <linux/radix-tree.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/string.h>
  16. #include <linux/compat.h>
  17. #include <linux/crc32c.h>
  18. #include <linux/fsverity.h>
  19. #include "send.h"
  20. #include "ctree.h"
  21. #include "backref.h"
  22. #include "locking.h"
  23. #include "disk-io.h"
  24. #include "btrfs_inode.h"
  25. #include "transaction.h"
  26. #include "compression.h"
  27. #include "print-tree.h"
  28. #include "accessors.h"
  29. #include "dir-item.h"
  30. #include "file-item.h"
  31. #include "ioctl.h"
  32. #include "verity.h"
  33. #include "lru_cache.h"
  34. /*
  35. * Maximum number of references an extent can have in order for us to attempt to
  36. * issue clone operations instead of write operations. This currently exists to
  37. * avoid hitting limitations of the backreference walking code (taking a lot of
  38. * time and using too much memory for extents with large number of references).
  39. */
  40. #define SEND_MAX_EXTENT_REFS 1024
  41. /*
  42. * A fs_path is a helper to dynamically build path names with unknown size.
  43. * It reallocates the internal buffer on demand.
  44. * It allows fast adding of path elements on the right side (normal path) and
  45. * fast adding to the left side (reversed path). A reversed path can also be
  46. * unreversed if needed.
  47. */
  48. struct fs_path {
  49. union {
  50. struct {
  51. char *start;
  52. char *end;
  53. char *buf;
  54. unsigned short buf_len:15;
  55. unsigned short reversed:1;
  56. char inline_buf[];
  57. };
  58. /*
  59. * Average path length does not exceed 200 bytes, we'll have
  60. * better packing in the slab and higher chance to satisfy
  61. * an allocation later during send.
  62. */
  63. char pad[256];
  64. };
  65. };
  66. #define FS_PATH_INLINE_SIZE \
  67. (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
  68. /* reused for each extent */
  69. struct clone_root {
  70. struct btrfs_root *root;
  71. u64 ino;
  72. u64 offset;
  73. u64 num_bytes;
  74. bool found_ref;
  75. };
  76. #define SEND_MAX_NAME_CACHE_SIZE 256
  77. /*
  78. * Limit the root_ids array of struct backref_cache_entry to 17 elements.
  79. * This makes the size of a cache entry to be exactly 192 bytes on x86_64, which
  80. * can be satisfied from the kmalloc-192 slab, without wasting any space.
  81. * The most common case is to have a single root for cloning, which corresponds
  82. * to the send root. Having the user specify more than 16 clone roots is not
  83. * common, and in such rare cases we simply don't use caching if the number of
  84. * cloning roots that lead down to a leaf is more than 17.
  85. */
  86. #define SEND_MAX_BACKREF_CACHE_ROOTS 17
  87. /*
  88. * Max number of entries in the cache.
  89. * With SEND_MAX_BACKREF_CACHE_ROOTS as 17, the size in bytes, excluding
  90. * maple tree's internal nodes, is 24K.
  91. */
  92. #define SEND_MAX_BACKREF_CACHE_SIZE 128
  93. /*
  94. * A backref cache entry maps a leaf to a list of IDs of roots from which the
  95. * leaf is accessible and we can use for clone operations.
  96. * With SEND_MAX_BACKREF_CACHE_ROOTS as 12, each cache entry is 128 bytes (on
  97. * x86_64).
  98. */
  99. struct backref_cache_entry {
  100. struct btrfs_lru_cache_entry entry;
  101. u64 root_ids[SEND_MAX_BACKREF_CACHE_ROOTS];
  102. /* Number of valid elements in the root_ids array. */
  103. int num_roots;
  104. };
  105. /* See the comment at lru_cache.h about struct btrfs_lru_cache_entry. */
  106. static_assert(offsetof(struct backref_cache_entry, entry) == 0);
  107. /*
  108. * Max number of entries in the cache that stores directories that were already
  109. * created. The cache uses raw struct btrfs_lru_cache_entry entries, so it uses
  110. * at most 4096 bytes - sizeof(struct btrfs_lru_cache_entry) is 48 bytes, but
  111. * the kmalloc-64 slab is used, so we get 4096 bytes (64 bytes * 64).
  112. */
  113. #define SEND_MAX_DIR_CREATED_CACHE_SIZE 64
  114. /*
  115. * Max number of entries in the cache that stores directories that were already
  116. * created. The cache uses raw struct btrfs_lru_cache_entry entries, so it uses
  117. * at most 4096 bytes - sizeof(struct btrfs_lru_cache_entry) is 48 bytes, but
  118. * the kmalloc-64 slab is used, so we get 4096 bytes (64 bytes * 64).
  119. */
  120. #define SEND_MAX_DIR_UTIMES_CACHE_SIZE 64
  121. struct send_ctx {
  122. struct file *send_filp;
  123. loff_t send_off;
  124. char *send_buf;
  125. u32 send_size;
  126. u32 send_max_size;
  127. /*
  128. * Whether BTRFS_SEND_A_DATA attribute was already added to current
  129. * command (since protocol v2, data must be the last attribute).
  130. */
  131. bool put_data;
  132. struct page **send_buf_pages;
  133. u64 flags; /* 'flags' member of btrfs_ioctl_send_args is u64 */
  134. /* Protocol version compatibility requested */
  135. u32 proto;
  136. struct btrfs_root *send_root;
  137. struct btrfs_root *parent_root;
  138. struct clone_root *clone_roots;
  139. int clone_roots_cnt;
  140. /* current state of the compare_tree call */
  141. struct btrfs_path *left_path;
  142. struct btrfs_path *right_path;
  143. struct btrfs_key *cmp_key;
  144. /*
  145. * Keep track of the generation of the last transaction that was used
  146. * for relocating a block group. This is periodically checked in order
  147. * to detect if a relocation happened since the last check, so that we
  148. * don't operate on stale extent buffers for nodes (level >= 1) or on
  149. * stale disk_bytenr values of file extent items.
  150. */
  151. u64 last_reloc_trans;
  152. /*
  153. * infos of the currently processed inode. In case of deleted inodes,
  154. * these are the values from the deleted inode.
  155. */
  156. u64 cur_ino;
  157. u64 cur_inode_gen;
  158. u64 cur_inode_size;
  159. u64 cur_inode_mode;
  160. u64 cur_inode_rdev;
  161. u64 cur_inode_last_extent;
  162. u64 cur_inode_next_write_offset;
  163. struct fs_path cur_inode_path;
  164. bool cur_inode_new;
  165. bool cur_inode_new_gen;
  166. bool cur_inode_deleted;
  167. bool ignore_cur_inode;
  168. bool cur_inode_needs_verity;
  169. void *verity_descriptor;
  170. u64 send_progress;
  171. struct list_head new_refs;
  172. struct list_head deleted_refs;
  173. struct btrfs_lru_cache name_cache;
  174. /*
  175. * The inode we are currently processing. It's not NULL only when we
  176. * need to issue write commands for data extents from this inode.
  177. */
  178. struct inode *cur_inode;
  179. struct file_ra_state ra;
  180. u64 page_cache_clear_start;
  181. bool clean_page_cache;
  182. /*
  183. * We process inodes by their increasing order, so if before an
  184. * incremental send we reverse the parent/child relationship of
  185. * directories such that a directory with a lower inode number was
  186. * the parent of a directory with a higher inode number, and the one
  187. * becoming the new parent got renamed too, we can't rename/move the
  188. * directory with lower inode number when we finish processing it - we
  189. * must process the directory with higher inode number first, then
  190. * rename/move it and then rename/move the directory with lower inode
  191. * number. Example follows.
  192. *
  193. * Tree state when the first send was performed:
  194. *
  195. * .
  196. * |-- a (ino 257)
  197. * |-- b (ino 258)
  198. * |
  199. * |
  200. * |-- c (ino 259)
  201. * | |-- d (ino 260)
  202. * |
  203. * |-- c2 (ino 261)
  204. *
  205. * Tree state when the second (incremental) send is performed:
  206. *
  207. * .
  208. * |-- a (ino 257)
  209. * |-- b (ino 258)
  210. * |-- c2 (ino 261)
  211. * |-- d2 (ino 260)
  212. * |-- cc (ino 259)
  213. *
  214. * The sequence of steps that lead to the second state was:
  215. *
  216. * mv /a/b/c/d /a/b/c2/d2
  217. * mv /a/b/c /a/b/c2/d2/cc
  218. *
  219. * "c" has lower inode number, but we can't move it (2nd mv operation)
  220. * before we move "d", which has higher inode number.
  221. *
  222. * So we just memorize which move/rename operations must be performed
  223. * later when their respective parent is processed and moved/renamed.
  224. */
  225. /* Indexed by parent directory inode number. */
  226. struct rb_root pending_dir_moves;
  227. /*
  228. * Reverse index, indexed by the inode number of a directory that
  229. * is waiting for the move/rename of its immediate parent before its
  230. * own move/rename can be performed.
  231. */
  232. struct rb_root waiting_dir_moves;
  233. /*
  234. * A directory that is going to be rm'ed might have a child directory
  235. * which is in the pending directory moves index above. In this case,
  236. * the directory can only be removed after the move/rename of its child
  237. * is performed. Example:
  238. *
  239. * Parent snapshot:
  240. *
  241. * . (ino 256)
  242. * |-- a/ (ino 257)
  243. * |-- b/ (ino 258)
  244. * |-- c/ (ino 259)
  245. * | |-- x/ (ino 260)
  246. * |
  247. * |-- y/ (ino 261)
  248. *
  249. * Send snapshot:
  250. *
  251. * . (ino 256)
  252. * |-- a/ (ino 257)
  253. * |-- b/ (ino 258)
  254. * |-- YY/ (ino 261)
  255. * |-- x/ (ino 260)
  256. *
  257. * Sequence of steps that lead to the send snapshot:
  258. * rm -f /a/b/c/foo.txt
  259. * mv /a/b/y /a/b/YY
  260. * mv /a/b/c/x /a/b/YY
  261. * rmdir /a/b/c
  262. *
  263. * When the child is processed, its move/rename is delayed until its
  264. * parent is processed (as explained above), but all other operations
  265. * like update utimes, chown, chgrp, etc, are performed and the paths
  266. * that it uses for those operations must use the orphanized name of
  267. * its parent (the directory we're going to rm later), so we need to
  268. * memorize that name.
  269. *
  270. * Indexed by the inode number of the directory to be deleted.
  271. */
  272. struct rb_root orphan_dirs;
  273. struct rb_root rbtree_new_refs;
  274. struct rb_root rbtree_deleted_refs;
  275. struct btrfs_lru_cache backref_cache;
  276. u64 backref_cache_last_reloc_trans;
  277. struct btrfs_lru_cache dir_created_cache;
  278. struct btrfs_lru_cache dir_utimes_cache;
  279. };
  280. struct pending_dir_move {
  281. struct rb_node node;
  282. struct list_head list;
  283. u64 parent_ino;
  284. u64 ino;
  285. u64 gen;
  286. struct list_head update_refs;
  287. };
  288. struct waiting_dir_move {
  289. struct rb_node node;
  290. u64 ino;
  291. /*
  292. * There might be some directory that could not be removed because it
  293. * was waiting for this directory inode to be moved first. Therefore
  294. * after this directory is moved, we can try to rmdir the ino rmdir_ino.
  295. */
  296. u64 rmdir_ino;
  297. u64 rmdir_gen;
  298. bool orphanized;
  299. };
  300. struct orphan_dir_info {
  301. struct rb_node node;
  302. u64 ino;
  303. u64 gen;
  304. u64 last_dir_index_offset;
  305. u64 dir_high_seq_ino;
  306. };
  307. struct name_cache_entry {
  308. /*
  309. * The key in the entry is an inode number, and the generation matches
  310. * the inode's generation.
  311. */
  312. struct btrfs_lru_cache_entry entry;
  313. u64 parent_ino;
  314. u64 parent_gen;
  315. int ret;
  316. int need_later_update;
  317. /* Name length without NUL terminator. */
  318. int name_len;
  319. /* Not NUL terminated. */
  320. char name[] __counted_by(name_len) __nonstring;
  321. };
  322. /* See the comment at lru_cache.h about struct btrfs_lru_cache_entry. */
  323. static_assert(offsetof(struct name_cache_entry, entry) == 0);
  324. #define ADVANCE 1
  325. #define ADVANCE_ONLY_NEXT -1
  326. enum btrfs_compare_tree_result {
  327. BTRFS_COMPARE_TREE_NEW,
  328. BTRFS_COMPARE_TREE_DELETED,
  329. BTRFS_COMPARE_TREE_CHANGED,
  330. BTRFS_COMPARE_TREE_SAME,
  331. };
  332. __cold
  333. static void inconsistent_snapshot_error(struct send_ctx *sctx,
  334. enum btrfs_compare_tree_result result,
  335. const char *what)
  336. {
  337. const char *result_string;
  338. switch (result) {
  339. case BTRFS_COMPARE_TREE_NEW:
  340. result_string = "new";
  341. break;
  342. case BTRFS_COMPARE_TREE_DELETED:
  343. result_string = "deleted";
  344. break;
  345. case BTRFS_COMPARE_TREE_CHANGED:
  346. result_string = "updated";
  347. break;
  348. case BTRFS_COMPARE_TREE_SAME:
  349. ASSERT(0);
  350. result_string = "unchanged";
  351. break;
  352. default:
  353. ASSERT(0);
  354. result_string = "unexpected";
  355. }
  356. btrfs_err(sctx->send_root->fs_info,
  357. "Send: inconsistent snapshot, found %s %s for inode %llu without updated inode item, send root is %llu, parent root is %llu",
  358. result_string, what, sctx->cmp_key->objectid,
  359. btrfs_root_id(sctx->send_root),
  360. (sctx->parent_root ? btrfs_root_id(sctx->parent_root) : 0));
  361. }
  362. __maybe_unused
  363. static bool proto_cmd_ok(const struct send_ctx *sctx, int cmd)
  364. {
  365. switch (sctx->proto) {
  366. case 1: return cmd <= BTRFS_SEND_C_MAX_V1;
  367. case 2: return cmd <= BTRFS_SEND_C_MAX_V2;
  368. case 3: return cmd <= BTRFS_SEND_C_MAX_V3;
  369. default: return false;
  370. }
  371. }
  372. static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
  373. static struct waiting_dir_move *
  374. get_waiting_dir_move(struct send_ctx *sctx, u64 ino);
  375. static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino, u64 gen);
  376. static int need_send_hole(struct send_ctx *sctx)
  377. {
  378. return (sctx->parent_root && !sctx->cur_inode_new &&
  379. !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted &&
  380. S_ISREG(sctx->cur_inode_mode));
  381. }
  382. static void fs_path_reset(struct fs_path *p)
  383. {
  384. if (p->reversed) {
  385. p->start = p->buf + p->buf_len - 1;
  386. p->end = p->start;
  387. *p->start = 0;
  388. } else {
  389. p->start = p->buf;
  390. p->end = p->start;
  391. *p->start = 0;
  392. }
  393. }
  394. static void init_path(struct fs_path *p)
  395. {
  396. p->reversed = 0;
  397. p->buf = p->inline_buf;
  398. p->buf_len = FS_PATH_INLINE_SIZE;
  399. fs_path_reset(p);
  400. }
  401. static struct fs_path *fs_path_alloc(void)
  402. {
  403. struct fs_path *p;
  404. p = kmalloc(sizeof(*p), GFP_KERNEL);
  405. if (!p)
  406. return NULL;
  407. init_path(p);
  408. return p;
  409. }
  410. static struct fs_path *fs_path_alloc_reversed(void)
  411. {
  412. struct fs_path *p;
  413. p = fs_path_alloc();
  414. if (!p)
  415. return NULL;
  416. p->reversed = 1;
  417. fs_path_reset(p);
  418. return p;
  419. }
  420. static void fs_path_free(struct fs_path *p)
  421. {
  422. if (!p)
  423. return;
  424. if (p->buf != p->inline_buf)
  425. kfree(p->buf);
  426. kfree(p);
  427. }
  428. static inline int fs_path_len(const struct fs_path *p)
  429. {
  430. return p->end - p->start;
  431. }
  432. static int fs_path_ensure_buf(struct fs_path *p, int len)
  433. {
  434. char *tmp_buf;
  435. int path_len;
  436. int old_buf_len;
  437. len++;
  438. if (p->buf_len >= len)
  439. return 0;
  440. if (WARN_ON(len > PATH_MAX))
  441. return -ENAMETOOLONG;
  442. path_len = p->end - p->start;
  443. old_buf_len = p->buf_len;
  444. /*
  445. * Allocate to the next largest kmalloc bucket size, to let
  446. * the fast path happen most of the time.
  447. */
  448. len = kmalloc_size_roundup(len);
  449. /*
  450. * First time the inline_buf does not suffice
  451. */
  452. if (p->buf == p->inline_buf) {
  453. tmp_buf = kmalloc(len, GFP_KERNEL);
  454. if (tmp_buf)
  455. memcpy(tmp_buf, p->buf, old_buf_len);
  456. } else {
  457. tmp_buf = krealloc(p->buf, len, GFP_KERNEL);
  458. }
  459. if (!tmp_buf)
  460. return -ENOMEM;
  461. p->buf = tmp_buf;
  462. p->buf_len = len;
  463. if (p->reversed) {
  464. tmp_buf = p->buf + old_buf_len - path_len - 1;
  465. p->end = p->buf + p->buf_len - 1;
  466. p->start = p->end - path_len;
  467. memmove(p->start, tmp_buf, path_len + 1);
  468. } else {
  469. p->start = p->buf;
  470. p->end = p->start + path_len;
  471. }
  472. return 0;
  473. }
  474. static int fs_path_prepare_for_add(struct fs_path *p, int name_len,
  475. char **prepared)
  476. {
  477. int ret;
  478. int new_len;
  479. new_len = p->end - p->start + name_len;
  480. if (p->start != p->end)
  481. new_len++;
  482. ret = fs_path_ensure_buf(p, new_len);
  483. if (ret < 0)
  484. goto out;
  485. if (p->reversed) {
  486. if (p->start != p->end)
  487. *--p->start = '/';
  488. p->start -= name_len;
  489. *prepared = p->start;
  490. } else {
  491. if (p->start != p->end)
  492. *p->end++ = '/';
  493. *prepared = p->end;
  494. p->end += name_len;
  495. *p->end = 0;
  496. }
  497. out:
  498. return ret;
  499. }
  500. static int fs_path_add(struct fs_path *p, const char *name, int name_len)
  501. {
  502. int ret;
  503. char *prepared;
  504. ret = fs_path_prepare_for_add(p, name_len, &prepared);
  505. if (ret < 0)
  506. goto out;
  507. memcpy(prepared, name, name_len);
  508. out:
  509. return ret;
  510. }
  511. static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
  512. {
  513. int ret;
  514. char *prepared;
  515. ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared);
  516. if (ret < 0)
  517. goto out;
  518. memcpy(prepared, p2->start, p2->end - p2->start);
  519. out:
  520. return ret;
  521. }
  522. static int fs_path_add_from_extent_buffer(struct fs_path *p,
  523. struct extent_buffer *eb,
  524. unsigned long off, int len)
  525. {
  526. int ret;
  527. char *prepared;
  528. ret = fs_path_prepare_for_add(p, len, &prepared);
  529. if (ret < 0)
  530. goto out;
  531. read_extent_buffer(eb, prepared, off, len);
  532. out:
  533. return ret;
  534. }
  535. static int fs_path_copy(struct fs_path *p, struct fs_path *from)
  536. {
  537. p->reversed = from->reversed;
  538. fs_path_reset(p);
  539. return fs_path_add_path(p, from);
  540. }
  541. static void fs_path_unreverse(struct fs_path *p)
  542. {
  543. char *tmp;
  544. int len;
  545. if (!p->reversed)
  546. return;
  547. tmp = p->start;
  548. len = p->end - p->start;
  549. p->start = p->buf;
  550. p->end = p->start + len;
  551. memmove(p->start, tmp, len + 1);
  552. p->reversed = 0;
  553. }
  554. static inline bool is_current_inode_path(const struct send_ctx *sctx,
  555. const struct fs_path *path)
  556. {
  557. const struct fs_path *cur = &sctx->cur_inode_path;
  558. return (strncmp(path->start, cur->start, fs_path_len(cur)) == 0);
  559. }
  560. static struct btrfs_path *alloc_path_for_send(void)
  561. {
  562. struct btrfs_path *path;
  563. path = btrfs_alloc_path();
  564. if (!path)
  565. return NULL;
  566. path->search_commit_root = 1;
  567. path->skip_locking = 1;
  568. path->need_commit_sem = 1;
  569. return path;
  570. }
  571. static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
  572. {
  573. int ret;
  574. u32 pos = 0;
  575. while (pos < len) {
  576. ret = kernel_write(filp, buf + pos, len - pos, off);
  577. if (ret < 0)
  578. return ret;
  579. if (ret == 0)
  580. return -EIO;
  581. pos += ret;
  582. }
  583. return 0;
  584. }
  585. static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
  586. {
  587. struct btrfs_tlv_header *hdr;
  588. int total_len = sizeof(*hdr) + len;
  589. int left = sctx->send_max_size - sctx->send_size;
  590. if (WARN_ON_ONCE(sctx->put_data))
  591. return -EINVAL;
  592. if (unlikely(left < total_len))
  593. return -EOVERFLOW;
  594. hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
  595. put_unaligned_le16(attr, &hdr->tlv_type);
  596. put_unaligned_le16(len, &hdr->tlv_len);
  597. memcpy(hdr + 1, data, len);
  598. sctx->send_size += total_len;
  599. return 0;
  600. }
  601. #define TLV_PUT_DEFINE_INT(bits) \
  602. static int tlv_put_u##bits(struct send_ctx *sctx, \
  603. u##bits attr, u##bits value) \
  604. { \
  605. __le##bits __tmp = cpu_to_le##bits(value); \
  606. return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
  607. }
  608. TLV_PUT_DEFINE_INT(8)
  609. TLV_PUT_DEFINE_INT(32)
  610. TLV_PUT_DEFINE_INT(64)
  611. static int tlv_put_string(struct send_ctx *sctx, u16 attr,
  612. const char *str, int len)
  613. {
  614. if (len == -1)
  615. len = strlen(str);
  616. return tlv_put(sctx, attr, str, len);
  617. }
  618. static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
  619. const u8 *uuid)
  620. {
  621. return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
  622. }
  623. static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
  624. struct extent_buffer *eb,
  625. struct btrfs_timespec *ts)
  626. {
  627. struct btrfs_timespec bts;
  628. read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
  629. return tlv_put(sctx, attr, &bts, sizeof(bts));
  630. }
  631. #define TLV_PUT(sctx, attrtype, data, attrlen) \
  632. do { \
  633. ret = tlv_put(sctx, attrtype, data, attrlen); \
  634. if (ret < 0) \
  635. goto tlv_put_failure; \
  636. } while (0)
  637. #define TLV_PUT_INT(sctx, attrtype, bits, value) \
  638. do { \
  639. ret = tlv_put_u##bits(sctx, attrtype, value); \
  640. if (ret < 0) \
  641. goto tlv_put_failure; \
  642. } while (0)
  643. #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
  644. #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
  645. #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
  646. #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
  647. #define TLV_PUT_STRING(sctx, attrtype, str, len) \
  648. do { \
  649. ret = tlv_put_string(sctx, attrtype, str, len); \
  650. if (ret < 0) \
  651. goto tlv_put_failure; \
  652. } while (0)
  653. #define TLV_PUT_PATH(sctx, attrtype, p) \
  654. do { \
  655. ret = tlv_put_string(sctx, attrtype, p->start, \
  656. p->end - p->start); \
  657. if (ret < 0) \
  658. goto tlv_put_failure; \
  659. } while(0)
  660. #define TLV_PUT_UUID(sctx, attrtype, uuid) \
  661. do { \
  662. ret = tlv_put_uuid(sctx, attrtype, uuid); \
  663. if (ret < 0) \
  664. goto tlv_put_failure; \
  665. } while (0)
  666. #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
  667. do { \
  668. ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
  669. if (ret < 0) \
  670. goto tlv_put_failure; \
  671. } while (0)
  672. static int send_header(struct send_ctx *sctx)
  673. {
  674. struct btrfs_stream_header hdr;
  675. strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
  676. hdr.version = cpu_to_le32(sctx->proto);
  677. return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
  678. &sctx->send_off);
  679. }
  680. /*
  681. * For each command/item we want to send to userspace, we call this function.
  682. */
  683. static int begin_cmd(struct send_ctx *sctx, int cmd)
  684. {
  685. struct btrfs_cmd_header *hdr;
  686. if (WARN_ON(!sctx->send_buf))
  687. return -EINVAL;
  688. if (unlikely(sctx->send_size != 0)) {
  689. btrfs_err(sctx->send_root->fs_info,
  690. "send: command header buffer not empty cmd %d offset %llu",
  691. cmd, sctx->send_off);
  692. return -EINVAL;
  693. }
  694. sctx->send_size += sizeof(*hdr);
  695. hdr = (struct btrfs_cmd_header *)sctx->send_buf;
  696. put_unaligned_le16(cmd, &hdr->cmd);
  697. return 0;
  698. }
  699. static int send_cmd(struct send_ctx *sctx)
  700. {
  701. int ret;
  702. struct btrfs_cmd_header *hdr;
  703. u32 crc;
  704. hdr = (struct btrfs_cmd_header *)sctx->send_buf;
  705. put_unaligned_le32(sctx->send_size - sizeof(*hdr), &hdr->len);
  706. put_unaligned_le32(0, &hdr->crc);
  707. crc = crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
  708. put_unaligned_le32(crc, &hdr->crc);
  709. ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
  710. &sctx->send_off);
  711. sctx->send_size = 0;
  712. sctx->put_data = false;
  713. return ret;
  714. }
  715. /*
  716. * Sends a move instruction to user space
  717. */
  718. static int send_rename(struct send_ctx *sctx,
  719. struct fs_path *from, struct fs_path *to)
  720. {
  721. struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
  722. int ret;
  723. btrfs_debug(fs_info, "send_rename %s -> %s", from->start, to->start);
  724. ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
  725. if (ret < 0)
  726. goto out;
  727. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
  728. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
  729. ret = send_cmd(sctx);
  730. tlv_put_failure:
  731. out:
  732. return ret;
  733. }
  734. /*
  735. * Sends a link instruction to user space
  736. */
  737. static int send_link(struct send_ctx *sctx,
  738. struct fs_path *path, struct fs_path *lnk)
  739. {
  740. struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
  741. int ret;
  742. btrfs_debug(fs_info, "send_link %s -> %s", path->start, lnk->start);
  743. ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
  744. if (ret < 0)
  745. goto out;
  746. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
  747. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
  748. ret = send_cmd(sctx);
  749. tlv_put_failure:
  750. out:
  751. return ret;
  752. }
  753. /*
  754. * Sends an unlink instruction to user space
  755. */
  756. static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
  757. {
  758. struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
  759. int ret;
  760. btrfs_debug(fs_info, "send_unlink %s", path->start);
  761. ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
  762. if (ret < 0)
  763. goto out;
  764. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
  765. ret = send_cmd(sctx);
  766. tlv_put_failure:
  767. out:
  768. return ret;
  769. }
  770. /*
  771. * Sends a rmdir instruction to user space
  772. */
  773. static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
  774. {
  775. struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
  776. int ret;
  777. btrfs_debug(fs_info, "send_rmdir %s", path->start);
  778. ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
  779. if (ret < 0)
  780. goto out;
  781. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
  782. ret = send_cmd(sctx);
  783. tlv_put_failure:
  784. out:
  785. return ret;
  786. }
  787. struct btrfs_inode_info {
  788. u64 size;
  789. u64 gen;
  790. u64 mode;
  791. u64 uid;
  792. u64 gid;
  793. u64 rdev;
  794. u64 fileattr;
  795. u64 nlink;
  796. };
  797. /*
  798. * Helper function to retrieve some fields from an inode item.
  799. */
  800. static int get_inode_info(struct btrfs_root *root, u64 ino,
  801. struct btrfs_inode_info *info)
  802. {
  803. int ret;
  804. struct btrfs_path *path;
  805. struct btrfs_inode_item *ii;
  806. struct btrfs_key key;
  807. path = alloc_path_for_send();
  808. if (!path)
  809. return -ENOMEM;
  810. key.objectid = ino;
  811. key.type = BTRFS_INODE_ITEM_KEY;
  812. key.offset = 0;
  813. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  814. if (ret) {
  815. if (ret > 0)
  816. ret = -ENOENT;
  817. goto out;
  818. }
  819. if (!info)
  820. goto out;
  821. ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
  822. struct btrfs_inode_item);
  823. info->size = btrfs_inode_size(path->nodes[0], ii);
  824. info->gen = btrfs_inode_generation(path->nodes[0], ii);
  825. info->mode = btrfs_inode_mode(path->nodes[0], ii);
  826. info->uid = btrfs_inode_uid(path->nodes[0], ii);
  827. info->gid = btrfs_inode_gid(path->nodes[0], ii);
  828. info->rdev = btrfs_inode_rdev(path->nodes[0], ii);
  829. info->nlink = btrfs_inode_nlink(path->nodes[0], ii);
  830. /*
  831. * Transfer the unchanged u64 value of btrfs_inode_item::flags, that's
  832. * otherwise logically split to 32/32 parts.
  833. */
  834. info->fileattr = btrfs_inode_flags(path->nodes[0], ii);
  835. out:
  836. btrfs_free_path(path);
  837. return ret;
  838. }
  839. static int get_inode_gen(struct btrfs_root *root, u64 ino, u64 *gen)
  840. {
  841. int ret;
  842. struct btrfs_inode_info info = { 0 };
  843. ASSERT(gen);
  844. ret = get_inode_info(root, ino, &info);
  845. *gen = info.gen;
  846. return ret;
  847. }
  848. typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
  849. struct fs_path *p,
  850. void *ctx);
  851. /*
  852. * Helper function to iterate the entries in ONE btrfs_inode_ref or
  853. * btrfs_inode_extref.
  854. * The iterate callback may return a non zero value to stop iteration. This can
  855. * be a negative value for error codes or 1 to simply stop it.
  856. *
  857. * path must point to the INODE_REF or INODE_EXTREF when called.
  858. */
  859. static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
  860. struct btrfs_key *found_key, int resolve,
  861. iterate_inode_ref_t iterate, void *ctx)
  862. {
  863. struct extent_buffer *eb = path->nodes[0];
  864. struct btrfs_inode_ref *iref;
  865. struct btrfs_inode_extref *extref;
  866. struct btrfs_path *tmp_path;
  867. struct fs_path *p;
  868. u32 cur = 0;
  869. u32 total;
  870. int slot = path->slots[0];
  871. u32 name_len;
  872. char *start;
  873. int ret = 0;
  874. int num = 0;
  875. int index;
  876. u64 dir;
  877. unsigned long name_off;
  878. unsigned long elem_size;
  879. unsigned long ptr;
  880. p = fs_path_alloc_reversed();
  881. if (!p)
  882. return -ENOMEM;
  883. tmp_path = alloc_path_for_send();
  884. if (!tmp_path) {
  885. fs_path_free(p);
  886. return -ENOMEM;
  887. }
  888. if (found_key->type == BTRFS_INODE_REF_KEY) {
  889. ptr = (unsigned long)btrfs_item_ptr(eb, slot,
  890. struct btrfs_inode_ref);
  891. total = btrfs_item_size(eb, slot);
  892. elem_size = sizeof(*iref);
  893. } else {
  894. ptr = btrfs_item_ptr_offset(eb, slot);
  895. total = btrfs_item_size(eb, slot);
  896. elem_size = sizeof(*extref);
  897. }
  898. while (cur < total) {
  899. fs_path_reset(p);
  900. if (found_key->type == BTRFS_INODE_REF_KEY) {
  901. iref = (struct btrfs_inode_ref *)(ptr + cur);
  902. name_len = btrfs_inode_ref_name_len(eb, iref);
  903. name_off = (unsigned long)(iref + 1);
  904. index = btrfs_inode_ref_index(eb, iref);
  905. dir = found_key->offset;
  906. } else {
  907. extref = (struct btrfs_inode_extref *)(ptr + cur);
  908. name_len = btrfs_inode_extref_name_len(eb, extref);
  909. name_off = (unsigned long)&extref->name;
  910. index = btrfs_inode_extref_index(eb, extref);
  911. dir = btrfs_inode_extref_parent(eb, extref);
  912. }
  913. if (resolve) {
  914. start = btrfs_ref_to_path(root, tmp_path, name_len,
  915. name_off, eb, dir,
  916. p->buf, p->buf_len);
  917. if (IS_ERR(start)) {
  918. ret = PTR_ERR(start);
  919. goto out;
  920. }
  921. if (start < p->buf) {
  922. /* overflow , try again with larger buffer */
  923. ret = fs_path_ensure_buf(p,
  924. p->buf_len + p->buf - start);
  925. if (ret < 0)
  926. goto out;
  927. start = btrfs_ref_to_path(root, tmp_path,
  928. name_len, name_off,
  929. eb, dir,
  930. p->buf, p->buf_len);
  931. if (IS_ERR(start)) {
  932. ret = PTR_ERR(start);
  933. goto out;
  934. }
  935. if (unlikely(start < p->buf)) {
  936. btrfs_err(root->fs_info,
  937. "send: path ref buffer underflow for key (%llu %u %llu)",
  938. found_key->objectid,
  939. found_key->type,
  940. found_key->offset);
  941. ret = -EINVAL;
  942. goto out;
  943. }
  944. }
  945. p->start = start;
  946. } else {
  947. ret = fs_path_add_from_extent_buffer(p, eb, name_off,
  948. name_len);
  949. if (ret < 0)
  950. goto out;
  951. }
  952. cur += elem_size + name_len;
  953. ret = iterate(num, dir, index, p, ctx);
  954. if (ret)
  955. goto out;
  956. num++;
  957. }
  958. out:
  959. btrfs_free_path(tmp_path);
  960. fs_path_free(p);
  961. return ret;
  962. }
  963. typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
  964. const char *name, int name_len,
  965. const char *data, int data_len,
  966. void *ctx);
  967. /*
  968. * Helper function to iterate the entries in ONE btrfs_dir_item.
  969. * The iterate callback may return a non zero value to stop iteration. This can
  970. * be a negative value for error codes or 1 to simply stop it.
  971. *
  972. * path must point to the dir item when called.
  973. */
  974. static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
  975. iterate_dir_item_t iterate, void *ctx)
  976. {
  977. int ret = 0;
  978. struct extent_buffer *eb;
  979. struct btrfs_dir_item *di;
  980. struct btrfs_key di_key;
  981. char *buf = NULL;
  982. int buf_len;
  983. u32 name_len;
  984. u32 data_len;
  985. u32 cur;
  986. u32 len;
  987. u32 total;
  988. int slot;
  989. int num;
  990. /*
  991. * Start with a small buffer (1 page). If later we end up needing more
  992. * space, which can happen for xattrs on a fs with a leaf size greater
  993. * than the page size, attempt to increase the buffer. Typically xattr
  994. * values are small.
  995. */
  996. buf_len = PATH_MAX;
  997. buf = kmalloc(buf_len, GFP_KERNEL);
  998. if (!buf) {
  999. ret = -ENOMEM;
  1000. goto out;
  1001. }
  1002. eb = path->nodes[0];
  1003. slot = path->slots[0];
  1004. di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
  1005. cur = 0;
  1006. len = 0;
  1007. total = btrfs_item_size(eb, slot);
  1008. num = 0;
  1009. while (cur < total) {
  1010. name_len = btrfs_dir_name_len(eb, di);
  1011. data_len = btrfs_dir_data_len(eb, di);
  1012. btrfs_dir_item_key_to_cpu(eb, di, &di_key);
  1013. if (btrfs_dir_ftype(eb, di) == BTRFS_FT_XATTR) {
  1014. if (name_len > XATTR_NAME_MAX) {
  1015. ret = -ENAMETOOLONG;
  1016. goto out;
  1017. }
  1018. if (name_len + data_len >
  1019. BTRFS_MAX_XATTR_SIZE(root->fs_info)) {
  1020. ret = -E2BIG;
  1021. goto out;
  1022. }
  1023. } else {
  1024. /*
  1025. * Path too long
  1026. */
  1027. if (name_len + data_len > PATH_MAX) {
  1028. ret = -ENAMETOOLONG;
  1029. goto out;
  1030. }
  1031. }
  1032. if (name_len + data_len > buf_len) {
  1033. buf_len = name_len + data_len;
  1034. if (is_vmalloc_addr(buf)) {
  1035. vfree(buf);
  1036. buf = NULL;
  1037. } else {
  1038. char *tmp = krealloc(buf, buf_len,
  1039. GFP_KERNEL | __GFP_NOWARN);
  1040. if (!tmp)
  1041. kfree(buf);
  1042. buf = tmp;
  1043. }
  1044. if (!buf) {
  1045. buf = kvmalloc(buf_len, GFP_KERNEL);
  1046. if (!buf) {
  1047. ret = -ENOMEM;
  1048. goto out;
  1049. }
  1050. }
  1051. }
  1052. read_extent_buffer(eb, buf, (unsigned long)(di + 1),
  1053. name_len + data_len);
  1054. len = sizeof(*di) + name_len + data_len;
  1055. di = (struct btrfs_dir_item *)((char *)di + len);
  1056. cur += len;
  1057. ret = iterate(num, &di_key, buf, name_len, buf + name_len,
  1058. data_len, ctx);
  1059. if (ret < 0)
  1060. goto out;
  1061. if (ret) {
  1062. ret = 0;
  1063. goto out;
  1064. }
  1065. num++;
  1066. }
  1067. out:
  1068. kvfree(buf);
  1069. return ret;
  1070. }
  1071. static int __copy_first_ref(int num, u64 dir, int index,
  1072. struct fs_path *p, void *ctx)
  1073. {
  1074. int ret;
  1075. struct fs_path *pt = ctx;
  1076. ret = fs_path_copy(pt, p);
  1077. if (ret < 0)
  1078. return ret;
  1079. /* we want the first only */
  1080. return 1;
  1081. }
  1082. /*
  1083. * Retrieve the first path of an inode. If an inode has more then one
  1084. * ref/hardlink, this is ignored.
  1085. */
  1086. static int get_inode_path(struct btrfs_root *root,
  1087. u64 ino, struct fs_path *path)
  1088. {
  1089. int ret;
  1090. struct btrfs_key key, found_key;
  1091. struct btrfs_path *p;
  1092. p = alloc_path_for_send();
  1093. if (!p)
  1094. return -ENOMEM;
  1095. fs_path_reset(path);
  1096. key.objectid = ino;
  1097. key.type = BTRFS_INODE_REF_KEY;
  1098. key.offset = 0;
  1099. ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
  1100. if (ret < 0)
  1101. goto out;
  1102. if (ret) {
  1103. ret = 1;
  1104. goto out;
  1105. }
  1106. btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
  1107. if (found_key.objectid != ino ||
  1108. (found_key.type != BTRFS_INODE_REF_KEY &&
  1109. found_key.type != BTRFS_INODE_EXTREF_KEY)) {
  1110. ret = -ENOENT;
  1111. goto out;
  1112. }
  1113. ret = iterate_inode_ref(root, p, &found_key, 1,
  1114. __copy_first_ref, path);
  1115. if (ret < 0)
  1116. goto out;
  1117. ret = 0;
  1118. out:
  1119. btrfs_free_path(p);
  1120. return ret;
  1121. }
  1122. struct backref_ctx {
  1123. struct send_ctx *sctx;
  1124. /* number of total found references */
  1125. u64 found;
  1126. /*
  1127. * used for clones found in send_root. clones found behind cur_objectid
  1128. * and cur_offset are not considered as allowed clones.
  1129. */
  1130. u64 cur_objectid;
  1131. u64 cur_offset;
  1132. /* may be truncated in case it's the last extent in a file */
  1133. u64 extent_len;
  1134. /* The bytenr the file extent item we are processing refers to. */
  1135. u64 bytenr;
  1136. /* The owner (root id) of the data backref for the current extent. */
  1137. u64 backref_owner;
  1138. /* The offset of the data backref for the current extent. */
  1139. u64 backref_offset;
  1140. };
  1141. static int __clone_root_cmp_bsearch(const void *key, const void *elt)
  1142. {
  1143. u64 root = (u64)(uintptr_t)key;
  1144. const struct clone_root *cr = elt;
  1145. if (root < btrfs_root_id(cr->root))
  1146. return -1;
  1147. if (root > btrfs_root_id(cr->root))
  1148. return 1;
  1149. return 0;
  1150. }
  1151. static int __clone_root_cmp_sort(const void *e1, const void *e2)
  1152. {
  1153. const struct clone_root *cr1 = e1;
  1154. const struct clone_root *cr2 = e2;
  1155. if (btrfs_root_id(cr1->root) < btrfs_root_id(cr2->root))
  1156. return -1;
  1157. if (btrfs_root_id(cr1->root) > btrfs_root_id(cr2->root))
  1158. return 1;
  1159. return 0;
  1160. }
  1161. /*
  1162. * Called for every backref that is found for the current extent.
  1163. * Results are collected in sctx->clone_roots->ino/offset.
  1164. */
  1165. static int iterate_backrefs(u64 ino, u64 offset, u64 num_bytes, u64 root_id,
  1166. void *ctx_)
  1167. {
  1168. struct backref_ctx *bctx = ctx_;
  1169. struct clone_root *clone_root;
  1170. /* First check if the root is in the list of accepted clone sources */
  1171. clone_root = bsearch((void *)(uintptr_t)root_id, bctx->sctx->clone_roots,
  1172. bctx->sctx->clone_roots_cnt,
  1173. sizeof(struct clone_root),
  1174. __clone_root_cmp_bsearch);
  1175. if (!clone_root)
  1176. return 0;
  1177. /* This is our own reference, bail out as we can't clone from it. */
  1178. if (clone_root->root == bctx->sctx->send_root &&
  1179. ino == bctx->cur_objectid &&
  1180. offset == bctx->cur_offset)
  1181. return 0;
  1182. /*
  1183. * Make sure we don't consider clones from send_root that are
  1184. * behind the current inode/offset.
  1185. */
  1186. if (clone_root->root == bctx->sctx->send_root) {
  1187. /*
  1188. * If the source inode was not yet processed we can't issue a
  1189. * clone operation, as the source extent does not exist yet at
  1190. * the destination of the stream.
  1191. */
  1192. if (ino > bctx->cur_objectid)
  1193. return 0;
  1194. /*
  1195. * We clone from the inode currently being sent as long as the
  1196. * source extent is already processed, otherwise we could try
  1197. * to clone from an extent that does not exist yet at the
  1198. * destination of the stream.
  1199. */
  1200. if (ino == bctx->cur_objectid &&
  1201. offset + bctx->extent_len >
  1202. bctx->sctx->cur_inode_next_write_offset)
  1203. return 0;
  1204. }
  1205. bctx->found++;
  1206. clone_root->found_ref = true;
  1207. /*
  1208. * If the given backref refers to a file extent item with a larger
  1209. * number of bytes than what we found before, use the new one so that
  1210. * we clone more optimally and end up doing less writes and getting
  1211. * less exclusive, non-shared extents at the destination.
  1212. */
  1213. if (num_bytes > clone_root->num_bytes) {
  1214. clone_root->ino = ino;
  1215. clone_root->offset = offset;
  1216. clone_root->num_bytes = num_bytes;
  1217. /*
  1218. * Found a perfect candidate, so there's no need to continue
  1219. * backref walking.
  1220. */
  1221. if (num_bytes >= bctx->extent_len)
  1222. return BTRFS_ITERATE_EXTENT_INODES_STOP;
  1223. }
  1224. return 0;
  1225. }
  1226. static bool lookup_backref_cache(u64 leaf_bytenr, void *ctx,
  1227. const u64 **root_ids_ret, int *root_count_ret)
  1228. {
  1229. struct backref_ctx *bctx = ctx;
  1230. struct send_ctx *sctx = bctx->sctx;
  1231. struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
  1232. const u64 key = leaf_bytenr >> fs_info->sectorsize_bits;
  1233. struct btrfs_lru_cache_entry *raw_entry;
  1234. struct backref_cache_entry *entry;
  1235. if (sctx->backref_cache.size == 0)
  1236. return false;
  1237. /*
  1238. * If relocation happened since we first filled the cache, then we must
  1239. * empty the cache and can not use it, because even though we operate on
  1240. * read-only roots, their leaves and nodes may have been reallocated and
  1241. * now be used for different nodes/leaves of the same tree or some other
  1242. * tree.
  1243. *
  1244. * We are called from iterate_extent_inodes() while either holding a
  1245. * transaction handle or holding fs_info->commit_root_sem, so no need
  1246. * to take any lock here.
  1247. */
  1248. if (fs_info->last_reloc_trans > sctx->backref_cache_last_reloc_trans) {
  1249. btrfs_lru_cache_clear(&sctx->backref_cache);
  1250. return false;
  1251. }
  1252. raw_entry = btrfs_lru_cache_lookup(&sctx->backref_cache, key, 0);
  1253. if (!raw_entry)
  1254. return false;
  1255. entry = container_of(raw_entry, struct backref_cache_entry, entry);
  1256. *root_ids_ret = entry->root_ids;
  1257. *root_count_ret = entry->num_roots;
  1258. return true;
  1259. }
  1260. static void store_backref_cache(u64 leaf_bytenr, const struct ulist *root_ids,
  1261. void *ctx)
  1262. {
  1263. struct backref_ctx *bctx = ctx;
  1264. struct send_ctx *sctx = bctx->sctx;
  1265. struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
  1266. struct backref_cache_entry *new_entry;
  1267. struct ulist_iterator uiter;
  1268. struct ulist_node *node;
  1269. int ret;
  1270. /*
  1271. * We're called while holding a transaction handle or while holding
  1272. * fs_info->commit_root_sem (at iterate_extent_inodes()), so must do a
  1273. * NOFS allocation.
  1274. */
  1275. new_entry = kmalloc(sizeof(struct backref_cache_entry), GFP_NOFS);
  1276. /* No worries, cache is optional. */
  1277. if (!new_entry)
  1278. return;
  1279. new_entry->entry.key = leaf_bytenr >> fs_info->sectorsize_bits;
  1280. new_entry->entry.gen = 0;
  1281. new_entry->num_roots = 0;
  1282. ULIST_ITER_INIT(&uiter);
  1283. while ((node = ulist_next(root_ids, &uiter)) != NULL) {
  1284. const u64 root_id = node->val;
  1285. struct clone_root *root;
  1286. root = bsearch((void *)(uintptr_t)root_id, sctx->clone_roots,
  1287. sctx->clone_roots_cnt, sizeof(struct clone_root),
  1288. __clone_root_cmp_bsearch);
  1289. if (!root)
  1290. continue;
  1291. /* Too many roots, just exit, no worries as caching is optional. */
  1292. if (new_entry->num_roots >= SEND_MAX_BACKREF_CACHE_ROOTS) {
  1293. kfree(new_entry);
  1294. return;
  1295. }
  1296. new_entry->root_ids[new_entry->num_roots] = root_id;
  1297. new_entry->num_roots++;
  1298. }
  1299. /*
  1300. * We may have not added any roots to the new cache entry, which means
  1301. * none of the roots is part of the list of roots from which we are
  1302. * allowed to clone. Cache the new entry as it's still useful to avoid
  1303. * backref walking to determine which roots have a path to the leaf.
  1304. *
  1305. * Also use GFP_NOFS because we're called while holding a transaction
  1306. * handle or while holding fs_info->commit_root_sem.
  1307. */
  1308. ret = btrfs_lru_cache_store(&sctx->backref_cache, &new_entry->entry,
  1309. GFP_NOFS);
  1310. ASSERT(ret == 0 || ret == -ENOMEM);
  1311. if (ret) {
  1312. /* Caching is optional, no worries. */
  1313. kfree(new_entry);
  1314. return;
  1315. }
  1316. /*
  1317. * We are called from iterate_extent_inodes() while either holding a
  1318. * transaction handle or holding fs_info->commit_root_sem, so no need
  1319. * to take any lock here.
  1320. */
  1321. if (sctx->backref_cache.size == 1)
  1322. sctx->backref_cache_last_reloc_trans = fs_info->last_reloc_trans;
  1323. }
  1324. static int check_extent_item(u64 bytenr, const struct btrfs_extent_item *ei,
  1325. const struct extent_buffer *leaf, void *ctx)
  1326. {
  1327. const u64 refs = btrfs_extent_refs(leaf, ei);
  1328. const struct backref_ctx *bctx = ctx;
  1329. const struct send_ctx *sctx = bctx->sctx;
  1330. if (bytenr == bctx->bytenr) {
  1331. const u64 flags = btrfs_extent_flags(leaf, ei);
  1332. if (WARN_ON(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
  1333. return -EUCLEAN;
  1334. /*
  1335. * If we have only one reference and only the send root as a
  1336. * clone source - meaning no clone roots were given in the
  1337. * struct btrfs_ioctl_send_args passed to the send ioctl - then
  1338. * it's our reference and there's no point in doing backref
  1339. * walking which is expensive, so exit early.
  1340. */
  1341. if (refs == 1 && sctx->clone_roots_cnt == 1)
  1342. return -ENOENT;
  1343. }
  1344. /*
  1345. * Backreference walking (iterate_extent_inodes() below) is currently
  1346. * too expensive when an extent has a large number of references, both
  1347. * in time spent and used memory. So for now just fallback to write
  1348. * operations instead of clone operations when an extent has more than
  1349. * a certain amount of references.
  1350. */
  1351. if (refs > SEND_MAX_EXTENT_REFS)
  1352. return -ENOENT;
  1353. return 0;
  1354. }
  1355. static bool skip_self_data_ref(u64 root, u64 ino, u64 offset, void *ctx)
  1356. {
  1357. const struct backref_ctx *bctx = ctx;
  1358. if (ino == bctx->cur_objectid &&
  1359. root == bctx->backref_owner &&
  1360. offset == bctx->backref_offset)
  1361. return true;
  1362. return false;
  1363. }
  1364. /*
  1365. * Given an inode, offset and extent item, it finds a good clone for a clone
  1366. * instruction. Returns -ENOENT when none could be found. The function makes
  1367. * sure that the returned clone is usable at the point where sending is at the
  1368. * moment. This means, that no clones are accepted which lie behind the current
  1369. * inode+offset.
  1370. *
  1371. * path must point to the extent item when called.
  1372. */
  1373. static int find_extent_clone(struct send_ctx *sctx,
  1374. struct btrfs_path *path,
  1375. u64 ino, u64 data_offset,
  1376. u64 ino_size,
  1377. struct clone_root **found)
  1378. {
  1379. struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
  1380. int ret;
  1381. int extent_type;
  1382. u64 logical;
  1383. u64 disk_byte;
  1384. u64 num_bytes;
  1385. struct btrfs_file_extent_item *fi;
  1386. struct extent_buffer *eb = path->nodes[0];
  1387. struct backref_ctx backref_ctx = { 0 };
  1388. struct btrfs_backref_walk_ctx backref_walk_ctx = { 0 };
  1389. struct clone_root *cur_clone_root;
  1390. int compressed;
  1391. u32 i;
  1392. /*
  1393. * With fallocate we can get prealloc extents beyond the inode's i_size,
  1394. * so we don't do anything here because clone operations can not clone
  1395. * to a range beyond i_size without increasing the i_size of the
  1396. * destination inode.
  1397. */
  1398. if (data_offset >= ino_size)
  1399. return 0;
  1400. fi = btrfs_item_ptr(eb, path->slots[0], struct btrfs_file_extent_item);
  1401. extent_type = btrfs_file_extent_type(eb, fi);
  1402. if (extent_type == BTRFS_FILE_EXTENT_INLINE)
  1403. return -ENOENT;
  1404. disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
  1405. if (disk_byte == 0)
  1406. return -ENOENT;
  1407. compressed = btrfs_file_extent_compression(eb, fi);
  1408. num_bytes = btrfs_file_extent_num_bytes(eb, fi);
  1409. logical = disk_byte + btrfs_file_extent_offset(eb, fi);
  1410. /*
  1411. * Setup the clone roots.
  1412. */
  1413. for (i = 0; i < sctx->clone_roots_cnt; i++) {
  1414. cur_clone_root = sctx->clone_roots + i;
  1415. cur_clone_root->ino = (u64)-1;
  1416. cur_clone_root->offset = 0;
  1417. cur_clone_root->num_bytes = 0;
  1418. cur_clone_root->found_ref = false;
  1419. }
  1420. backref_ctx.sctx = sctx;
  1421. backref_ctx.cur_objectid = ino;
  1422. backref_ctx.cur_offset = data_offset;
  1423. backref_ctx.bytenr = disk_byte;
  1424. /*
  1425. * Use the header owner and not the send root's id, because in case of a
  1426. * snapshot we can have shared subtrees.
  1427. */
  1428. backref_ctx.backref_owner = btrfs_header_owner(eb);
  1429. backref_ctx.backref_offset = data_offset - btrfs_file_extent_offset(eb, fi);
  1430. /*
  1431. * The last extent of a file may be too large due to page alignment.
  1432. * We need to adjust extent_len in this case so that the checks in
  1433. * iterate_backrefs() work.
  1434. */
  1435. if (data_offset + num_bytes >= ino_size)
  1436. backref_ctx.extent_len = ino_size - data_offset;
  1437. else
  1438. backref_ctx.extent_len = num_bytes;
  1439. /*
  1440. * Now collect all backrefs.
  1441. */
  1442. backref_walk_ctx.bytenr = disk_byte;
  1443. if (compressed == BTRFS_COMPRESS_NONE)
  1444. backref_walk_ctx.extent_item_pos = btrfs_file_extent_offset(eb, fi);
  1445. backref_walk_ctx.fs_info = fs_info;
  1446. backref_walk_ctx.cache_lookup = lookup_backref_cache;
  1447. backref_walk_ctx.cache_store = store_backref_cache;
  1448. backref_walk_ctx.indirect_ref_iterator = iterate_backrefs;
  1449. backref_walk_ctx.check_extent_item = check_extent_item;
  1450. backref_walk_ctx.user_ctx = &backref_ctx;
  1451. /*
  1452. * If have a single clone root, then it's the send root and we can tell
  1453. * the backref walking code to skip our own backref and not resolve it,
  1454. * since we can not use it for cloning - the source and destination
  1455. * ranges can't overlap and in case the leaf is shared through a subtree
  1456. * due to snapshots, we can't use those other roots since they are not
  1457. * in the list of clone roots.
  1458. */
  1459. if (sctx->clone_roots_cnt == 1)
  1460. backref_walk_ctx.skip_data_ref = skip_self_data_ref;
  1461. ret = iterate_extent_inodes(&backref_walk_ctx, true, iterate_backrefs,
  1462. &backref_ctx);
  1463. if (ret < 0)
  1464. return ret;
  1465. down_read(&fs_info->commit_root_sem);
  1466. if (fs_info->last_reloc_trans > sctx->last_reloc_trans) {
  1467. /*
  1468. * A transaction commit for a transaction in which block group
  1469. * relocation was done just happened.
  1470. * The disk_bytenr of the file extent item we processed is
  1471. * possibly stale, referring to the extent's location before
  1472. * relocation. So act as if we haven't found any clone sources
  1473. * and fallback to write commands, which will read the correct
  1474. * data from the new extent location. Otherwise we will fail
  1475. * below because we haven't found our own back reference or we
  1476. * could be getting incorrect sources in case the old extent
  1477. * was already reallocated after the relocation.
  1478. */
  1479. up_read(&fs_info->commit_root_sem);
  1480. return -ENOENT;
  1481. }
  1482. up_read(&fs_info->commit_root_sem);
  1483. btrfs_debug(fs_info,
  1484. "find_extent_clone: data_offset=%llu, ino=%llu, num_bytes=%llu, logical=%llu",
  1485. data_offset, ino, num_bytes, logical);
  1486. if (!backref_ctx.found) {
  1487. btrfs_debug(fs_info, "no clones found");
  1488. return -ENOENT;
  1489. }
  1490. cur_clone_root = NULL;
  1491. for (i = 0; i < sctx->clone_roots_cnt; i++) {
  1492. struct clone_root *clone_root = &sctx->clone_roots[i];
  1493. if (!clone_root->found_ref)
  1494. continue;
  1495. /*
  1496. * Choose the root from which we can clone more bytes, to
  1497. * minimize write operations and therefore have more extent
  1498. * sharing at the destination (the same as in the source).
  1499. */
  1500. if (!cur_clone_root ||
  1501. clone_root->num_bytes > cur_clone_root->num_bytes) {
  1502. cur_clone_root = clone_root;
  1503. /*
  1504. * We found an optimal clone candidate (any inode from
  1505. * any root is fine), so we're done.
  1506. */
  1507. if (clone_root->num_bytes >= backref_ctx.extent_len)
  1508. break;
  1509. }
  1510. }
  1511. if (cur_clone_root) {
  1512. *found = cur_clone_root;
  1513. ret = 0;
  1514. } else {
  1515. ret = -ENOENT;
  1516. }
  1517. return ret;
  1518. }
  1519. static int read_symlink(struct btrfs_root *root,
  1520. u64 ino,
  1521. struct fs_path *dest)
  1522. {
  1523. int ret;
  1524. struct btrfs_path *path;
  1525. struct btrfs_key key;
  1526. struct btrfs_file_extent_item *ei;
  1527. u8 type;
  1528. u8 compression;
  1529. unsigned long off;
  1530. int len;
  1531. path = alloc_path_for_send();
  1532. if (!path)
  1533. return -ENOMEM;
  1534. key.objectid = ino;
  1535. key.type = BTRFS_EXTENT_DATA_KEY;
  1536. key.offset = 0;
  1537. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1538. if (ret < 0)
  1539. goto out;
  1540. if (ret) {
  1541. /*
  1542. * An empty symlink inode. Can happen in rare error paths when
  1543. * creating a symlink (transaction committed before the inode
  1544. * eviction handler removed the symlink inode items and a crash
  1545. * happened in between or the subvol was snapshoted in between).
  1546. * Print an informative message to dmesg/syslog so that the user
  1547. * can delete the symlink.
  1548. */
  1549. btrfs_err(root->fs_info,
  1550. "Found empty symlink inode %llu at root %llu",
  1551. ino, btrfs_root_id(root));
  1552. ret = -EIO;
  1553. goto out;
  1554. }
  1555. ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1556. struct btrfs_file_extent_item);
  1557. type = btrfs_file_extent_type(path->nodes[0], ei);
  1558. if (unlikely(type != BTRFS_FILE_EXTENT_INLINE)) {
  1559. ret = -EUCLEAN;
  1560. btrfs_crit(root->fs_info,
  1561. "send: found symlink extent that is not inline, ino %llu root %llu extent type %d",
  1562. ino, btrfs_root_id(root), type);
  1563. goto out;
  1564. }
  1565. compression = btrfs_file_extent_compression(path->nodes[0], ei);
  1566. if (unlikely(compression != BTRFS_COMPRESS_NONE)) {
  1567. ret = -EUCLEAN;
  1568. btrfs_crit(root->fs_info,
  1569. "send: found symlink extent with compression, ino %llu root %llu compression type %d",
  1570. ino, btrfs_root_id(root), compression);
  1571. goto out;
  1572. }
  1573. off = btrfs_file_extent_inline_start(ei);
  1574. len = btrfs_file_extent_ram_bytes(path->nodes[0], ei);
  1575. ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
  1576. out:
  1577. btrfs_free_path(path);
  1578. return ret;
  1579. }
  1580. /*
  1581. * Helper function to generate a file name that is unique in the root of
  1582. * send_root and parent_root. This is used to generate names for orphan inodes.
  1583. */
  1584. static int gen_unique_name(struct send_ctx *sctx,
  1585. u64 ino, u64 gen,
  1586. struct fs_path *dest)
  1587. {
  1588. int ret = 0;
  1589. struct btrfs_path *path;
  1590. struct btrfs_dir_item *di;
  1591. char tmp[64];
  1592. int len;
  1593. u64 idx = 0;
  1594. path = alloc_path_for_send();
  1595. if (!path)
  1596. return -ENOMEM;
  1597. while (1) {
  1598. struct fscrypt_str tmp_name;
  1599. len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
  1600. ino, gen, idx);
  1601. ASSERT(len < sizeof(tmp));
  1602. tmp_name.name = tmp;
  1603. tmp_name.len = strlen(tmp);
  1604. di = btrfs_lookup_dir_item(NULL, sctx->send_root,
  1605. path, BTRFS_FIRST_FREE_OBJECTID,
  1606. &tmp_name, 0);
  1607. btrfs_release_path(path);
  1608. if (IS_ERR(di)) {
  1609. ret = PTR_ERR(di);
  1610. goto out;
  1611. }
  1612. if (di) {
  1613. /* not unique, try again */
  1614. idx++;
  1615. continue;
  1616. }
  1617. if (!sctx->parent_root) {
  1618. /* unique */
  1619. ret = 0;
  1620. break;
  1621. }
  1622. di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
  1623. path, BTRFS_FIRST_FREE_OBJECTID,
  1624. &tmp_name, 0);
  1625. btrfs_release_path(path);
  1626. if (IS_ERR(di)) {
  1627. ret = PTR_ERR(di);
  1628. goto out;
  1629. }
  1630. if (di) {
  1631. /* not unique, try again */
  1632. idx++;
  1633. continue;
  1634. }
  1635. /* unique */
  1636. break;
  1637. }
  1638. ret = fs_path_add(dest, tmp, strlen(tmp));
  1639. out:
  1640. btrfs_free_path(path);
  1641. return ret;
  1642. }
  1643. enum inode_state {
  1644. inode_state_no_change,
  1645. inode_state_will_create,
  1646. inode_state_did_create,
  1647. inode_state_will_delete,
  1648. inode_state_did_delete,
  1649. };
  1650. static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen,
  1651. u64 *send_gen, u64 *parent_gen)
  1652. {
  1653. int ret;
  1654. int left_ret;
  1655. int right_ret;
  1656. u64 left_gen;
  1657. u64 right_gen = 0;
  1658. struct btrfs_inode_info info;
  1659. ret = get_inode_info(sctx->send_root, ino, &info);
  1660. if (ret < 0 && ret != -ENOENT)
  1661. goto out;
  1662. left_ret = (info.nlink == 0) ? -ENOENT : ret;
  1663. left_gen = info.gen;
  1664. if (send_gen)
  1665. *send_gen = ((left_ret == -ENOENT) ? 0 : info.gen);
  1666. if (!sctx->parent_root) {
  1667. right_ret = -ENOENT;
  1668. } else {
  1669. ret = get_inode_info(sctx->parent_root, ino, &info);
  1670. if (ret < 0 && ret != -ENOENT)
  1671. goto out;
  1672. right_ret = (info.nlink == 0) ? -ENOENT : ret;
  1673. right_gen = info.gen;
  1674. if (parent_gen)
  1675. *parent_gen = ((right_ret == -ENOENT) ? 0 : info.gen);
  1676. }
  1677. if (!left_ret && !right_ret) {
  1678. if (left_gen == gen && right_gen == gen) {
  1679. ret = inode_state_no_change;
  1680. } else if (left_gen == gen) {
  1681. if (ino < sctx->send_progress)
  1682. ret = inode_state_did_create;
  1683. else
  1684. ret = inode_state_will_create;
  1685. } else if (right_gen == gen) {
  1686. if (ino < sctx->send_progress)
  1687. ret = inode_state_did_delete;
  1688. else
  1689. ret = inode_state_will_delete;
  1690. } else {
  1691. ret = -ENOENT;
  1692. }
  1693. } else if (!left_ret) {
  1694. if (left_gen == gen) {
  1695. if (ino < sctx->send_progress)
  1696. ret = inode_state_did_create;
  1697. else
  1698. ret = inode_state_will_create;
  1699. } else {
  1700. ret = -ENOENT;
  1701. }
  1702. } else if (!right_ret) {
  1703. if (right_gen == gen) {
  1704. if (ino < sctx->send_progress)
  1705. ret = inode_state_did_delete;
  1706. else
  1707. ret = inode_state_will_delete;
  1708. } else {
  1709. ret = -ENOENT;
  1710. }
  1711. } else {
  1712. ret = -ENOENT;
  1713. }
  1714. out:
  1715. return ret;
  1716. }
  1717. static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen,
  1718. u64 *send_gen, u64 *parent_gen)
  1719. {
  1720. int ret;
  1721. if (ino == BTRFS_FIRST_FREE_OBJECTID)
  1722. return 1;
  1723. ret = get_cur_inode_state(sctx, ino, gen, send_gen, parent_gen);
  1724. if (ret < 0)
  1725. goto out;
  1726. if (ret == inode_state_no_change ||
  1727. ret == inode_state_did_create ||
  1728. ret == inode_state_will_delete)
  1729. ret = 1;
  1730. else
  1731. ret = 0;
  1732. out:
  1733. return ret;
  1734. }
  1735. /*
  1736. * Helper function to lookup a dir item in a dir.
  1737. */
  1738. static int lookup_dir_item_inode(struct btrfs_root *root,
  1739. u64 dir, const char *name, int name_len,
  1740. u64 *found_inode)
  1741. {
  1742. int ret = 0;
  1743. struct btrfs_dir_item *di;
  1744. struct btrfs_key key;
  1745. struct btrfs_path *path;
  1746. struct fscrypt_str name_str = FSTR_INIT((char *)name, name_len);
  1747. path = alloc_path_for_send();
  1748. if (!path)
  1749. return -ENOMEM;
  1750. di = btrfs_lookup_dir_item(NULL, root, path, dir, &name_str, 0);
  1751. if (IS_ERR_OR_NULL(di)) {
  1752. ret = di ? PTR_ERR(di) : -ENOENT;
  1753. goto out;
  1754. }
  1755. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
  1756. if (key.type == BTRFS_ROOT_ITEM_KEY) {
  1757. ret = -ENOENT;
  1758. goto out;
  1759. }
  1760. *found_inode = key.objectid;
  1761. out:
  1762. btrfs_free_path(path);
  1763. return ret;
  1764. }
  1765. /*
  1766. * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
  1767. * generation of the parent dir and the name of the dir entry.
  1768. */
  1769. static int get_first_ref(struct btrfs_root *root, u64 ino,
  1770. u64 *dir, u64 *dir_gen, struct fs_path *name)
  1771. {
  1772. int ret;
  1773. struct btrfs_key key;
  1774. struct btrfs_key found_key;
  1775. struct btrfs_path *path;
  1776. int len;
  1777. u64 parent_dir;
  1778. path = alloc_path_for_send();
  1779. if (!path)
  1780. return -ENOMEM;
  1781. key.objectid = ino;
  1782. key.type = BTRFS_INODE_REF_KEY;
  1783. key.offset = 0;
  1784. ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
  1785. if (ret < 0)
  1786. goto out;
  1787. if (!ret)
  1788. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1789. path->slots[0]);
  1790. if (ret || found_key.objectid != ino ||
  1791. (found_key.type != BTRFS_INODE_REF_KEY &&
  1792. found_key.type != BTRFS_INODE_EXTREF_KEY)) {
  1793. ret = -ENOENT;
  1794. goto out;
  1795. }
  1796. if (found_key.type == BTRFS_INODE_REF_KEY) {
  1797. struct btrfs_inode_ref *iref;
  1798. iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1799. struct btrfs_inode_ref);
  1800. len = btrfs_inode_ref_name_len(path->nodes[0], iref);
  1801. ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
  1802. (unsigned long)(iref + 1),
  1803. len);
  1804. parent_dir = found_key.offset;
  1805. } else {
  1806. struct btrfs_inode_extref *extref;
  1807. extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1808. struct btrfs_inode_extref);
  1809. len = btrfs_inode_extref_name_len(path->nodes[0], extref);
  1810. ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
  1811. (unsigned long)&extref->name, len);
  1812. parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
  1813. }
  1814. if (ret < 0)
  1815. goto out;
  1816. btrfs_release_path(path);
  1817. if (dir_gen) {
  1818. ret = get_inode_gen(root, parent_dir, dir_gen);
  1819. if (ret < 0)
  1820. goto out;
  1821. }
  1822. *dir = parent_dir;
  1823. out:
  1824. btrfs_free_path(path);
  1825. return ret;
  1826. }
  1827. static int is_first_ref(struct btrfs_root *root,
  1828. u64 ino, u64 dir,
  1829. const char *name, int name_len)
  1830. {
  1831. int ret;
  1832. struct fs_path *tmp_name;
  1833. u64 tmp_dir;
  1834. tmp_name = fs_path_alloc();
  1835. if (!tmp_name)
  1836. return -ENOMEM;
  1837. ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name);
  1838. if (ret < 0)
  1839. goto out;
  1840. if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
  1841. ret = 0;
  1842. goto out;
  1843. }
  1844. ret = !memcmp(tmp_name->start, name, name_len);
  1845. out:
  1846. fs_path_free(tmp_name);
  1847. return ret;
  1848. }
  1849. /*
  1850. * Used by process_recorded_refs to determine if a new ref would overwrite an
  1851. * already existing ref. In case it detects an overwrite, it returns the
  1852. * inode/gen in who_ino/who_gen.
  1853. * When an overwrite is detected, process_recorded_refs does proper orphanizing
  1854. * to make sure later references to the overwritten inode are possible.
  1855. * Orphanizing is however only required for the first ref of an inode.
  1856. * process_recorded_refs does an additional is_first_ref check to see if
  1857. * orphanizing is really required.
  1858. */
  1859. static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
  1860. const char *name, int name_len,
  1861. u64 *who_ino, u64 *who_gen, u64 *who_mode)
  1862. {
  1863. int ret;
  1864. u64 parent_root_dir_gen;
  1865. u64 other_inode = 0;
  1866. struct btrfs_inode_info info;
  1867. if (!sctx->parent_root)
  1868. return 0;
  1869. ret = is_inode_existent(sctx, dir, dir_gen, NULL, &parent_root_dir_gen);
  1870. if (ret <= 0)
  1871. return 0;
  1872. /*
  1873. * If we have a parent root we need to verify that the parent dir was
  1874. * not deleted and then re-created, if it was then we have no overwrite
  1875. * and we can just unlink this entry.
  1876. *
  1877. * @parent_root_dir_gen was set to 0 if the inode does not exist in the
  1878. * parent root.
  1879. */
  1880. if (sctx->parent_root && dir != BTRFS_FIRST_FREE_OBJECTID &&
  1881. parent_root_dir_gen != dir_gen)
  1882. return 0;
  1883. ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
  1884. &other_inode);
  1885. if (ret == -ENOENT)
  1886. return 0;
  1887. else if (ret < 0)
  1888. return ret;
  1889. /*
  1890. * Check if the overwritten ref was already processed. If yes, the ref
  1891. * was already unlinked/moved, so we can safely assume that we will not
  1892. * overwrite anything at this point in time.
  1893. */
  1894. if (other_inode > sctx->send_progress ||
  1895. is_waiting_for_move(sctx, other_inode)) {
  1896. ret = get_inode_info(sctx->parent_root, other_inode, &info);
  1897. if (ret < 0)
  1898. return ret;
  1899. *who_ino = other_inode;
  1900. *who_gen = info.gen;
  1901. *who_mode = info.mode;
  1902. return 1;
  1903. }
  1904. return 0;
  1905. }
  1906. /*
  1907. * Checks if the ref was overwritten by an already processed inode. This is
  1908. * used by __get_cur_name_and_parent to find out if the ref was orphanized and
  1909. * thus the orphan name needs be used.
  1910. * process_recorded_refs also uses it to avoid unlinking of refs that were
  1911. * overwritten.
  1912. */
  1913. static int did_overwrite_ref(struct send_ctx *sctx,
  1914. u64 dir, u64 dir_gen,
  1915. u64 ino, u64 ino_gen,
  1916. const char *name, int name_len)
  1917. {
  1918. int ret;
  1919. u64 ow_inode;
  1920. u64 ow_gen = 0;
  1921. u64 send_root_dir_gen;
  1922. if (!sctx->parent_root)
  1923. return 0;
  1924. ret = is_inode_existent(sctx, dir, dir_gen, &send_root_dir_gen, NULL);
  1925. if (ret <= 0)
  1926. return ret;
  1927. /*
  1928. * @send_root_dir_gen was set to 0 if the inode does not exist in the
  1929. * send root.
  1930. */
  1931. if (dir != BTRFS_FIRST_FREE_OBJECTID && send_root_dir_gen != dir_gen)
  1932. return 0;
  1933. /* check if the ref was overwritten by another ref */
  1934. ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
  1935. &ow_inode);
  1936. if (ret == -ENOENT) {
  1937. /* was never and will never be overwritten */
  1938. return 0;
  1939. } else if (ret < 0) {
  1940. return ret;
  1941. }
  1942. if (ow_inode == ino) {
  1943. ret = get_inode_gen(sctx->send_root, ow_inode, &ow_gen);
  1944. if (ret < 0)
  1945. return ret;
  1946. /* It's the same inode, so no overwrite happened. */
  1947. if (ow_gen == ino_gen)
  1948. return 0;
  1949. }
  1950. /*
  1951. * We know that it is or will be overwritten. Check this now.
  1952. * The current inode being processed might have been the one that caused
  1953. * inode 'ino' to be orphanized, therefore check if ow_inode matches
  1954. * the current inode being processed.
  1955. */
  1956. if (ow_inode < sctx->send_progress)
  1957. return 1;
  1958. if (ino != sctx->cur_ino && ow_inode == sctx->cur_ino) {
  1959. if (ow_gen == 0) {
  1960. ret = get_inode_gen(sctx->send_root, ow_inode, &ow_gen);
  1961. if (ret < 0)
  1962. return ret;
  1963. }
  1964. if (ow_gen == sctx->cur_inode_gen)
  1965. return 1;
  1966. }
  1967. return 0;
  1968. }
  1969. /*
  1970. * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
  1971. * that got overwritten. This is used by process_recorded_refs to determine
  1972. * if it has to use the path as returned by get_cur_path or the orphan name.
  1973. */
  1974. static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
  1975. {
  1976. int ret = 0;
  1977. struct fs_path *name = NULL;
  1978. u64 dir;
  1979. u64 dir_gen;
  1980. if (!sctx->parent_root)
  1981. goto out;
  1982. name = fs_path_alloc();
  1983. if (!name)
  1984. return -ENOMEM;
  1985. ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
  1986. if (ret < 0)
  1987. goto out;
  1988. ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
  1989. name->start, fs_path_len(name));
  1990. out:
  1991. fs_path_free(name);
  1992. return ret;
  1993. }
  1994. static inline struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
  1995. u64 ino, u64 gen)
  1996. {
  1997. struct btrfs_lru_cache_entry *entry;
  1998. entry = btrfs_lru_cache_lookup(&sctx->name_cache, ino, gen);
  1999. if (!entry)
  2000. return NULL;
  2001. return container_of(entry, struct name_cache_entry, entry);
  2002. }
  2003. /*
  2004. * Used by get_cur_path for each ref up to the root.
  2005. * Returns 0 if it succeeded.
  2006. * Returns 1 if the inode is not existent or got overwritten. In that case, the
  2007. * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
  2008. * is returned, parent_ino/parent_gen are not guaranteed to be valid.
  2009. * Returns <0 in case of error.
  2010. */
  2011. static int __get_cur_name_and_parent(struct send_ctx *sctx,
  2012. u64 ino, u64 gen,
  2013. u64 *parent_ino,
  2014. u64 *parent_gen,
  2015. struct fs_path *dest)
  2016. {
  2017. int ret;
  2018. int nce_ret;
  2019. struct name_cache_entry *nce;
  2020. /*
  2021. * First check if we already did a call to this function with the same
  2022. * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
  2023. * return the cached result.
  2024. */
  2025. nce = name_cache_search(sctx, ino, gen);
  2026. if (nce) {
  2027. if (ino < sctx->send_progress && nce->need_later_update) {
  2028. btrfs_lru_cache_remove(&sctx->name_cache, &nce->entry);
  2029. nce = NULL;
  2030. } else {
  2031. *parent_ino = nce->parent_ino;
  2032. *parent_gen = nce->parent_gen;
  2033. ret = fs_path_add(dest, nce->name, nce->name_len);
  2034. if (ret < 0)
  2035. goto out;
  2036. ret = nce->ret;
  2037. goto out;
  2038. }
  2039. }
  2040. /*
  2041. * If the inode is not existent yet, add the orphan name and return 1.
  2042. * This should only happen for the parent dir that we determine in
  2043. * record_new_ref_if_needed().
  2044. */
  2045. ret = is_inode_existent(sctx, ino, gen, NULL, NULL);
  2046. if (ret < 0)
  2047. goto out;
  2048. if (!ret) {
  2049. ret = gen_unique_name(sctx, ino, gen, dest);
  2050. if (ret < 0)
  2051. goto out;
  2052. ret = 1;
  2053. goto out_cache;
  2054. }
  2055. /*
  2056. * Depending on whether the inode was already processed or not, use
  2057. * send_root or parent_root for ref lookup.
  2058. */
  2059. if (ino < sctx->send_progress)
  2060. ret = get_first_ref(sctx->send_root, ino,
  2061. parent_ino, parent_gen, dest);
  2062. else
  2063. ret = get_first_ref(sctx->parent_root, ino,
  2064. parent_ino, parent_gen, dest);
  2065. if (ret < 0)
  2066. goto out;
  2067. /*
  2068. * Check if the ref was overwritten by an inode's ref that was processed
  2069. * earlier. If yes, treat as orphan and return 1.
  2070. */
  2071. ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
  2072. dest->start, dest->end - dest->start);
  2073. if (ret < 0)
  2074. goto out;
  2075. if (ret) {
  2076. fs_path_reset(dest);
  2077. ret = gen_unique_name(sctx, ino, gen, dest);
  2078. if (ret < 0)
  2079. goto out;
  2080. ret = 1;
  2081. }
  2082. out_cache:
  2083. /*
  2084. * Store the result of the lookup in the name cache.
  2085. */
  2086. nce = kmalloc(sizeof(*nce) + fs_path_len(dest), GFP_KERNEL);
  2087. if (!nce) {
  2088. ret = -ENOMEM;
  2089. goto out;
  2090. }
  2091. nce->entry.key = ino;
  2092. nce->entry.gen = gen;
  2093. nce->parent_ino = *parent_ino;
  2094. nce->parent_gen = *parent_gen;
  2095. nce->name_len = fs_path_len(dest);
  2096. nce->ret = ret;
  2097. memcpy(nce->name, dest->start, nce->name_len);
  2098. if (ino < sctx->send_progress)
  2099. nce->need_later_update = 0;
  2100. else
  2101. nce->need_later_update = 1;
  2102. nce_ret = btrfs_lru_cache_store(&sctx->name_cache, &nce->entry, GFP_KERNEL);
  2103. if (nce_ret < 0) {
  2104. kfree(nce);
  2105. ret = nce_ret;
  2106. }
  2107. out:
  2108. return ret;
  2109. }
  2110. /*
  2111. * Magic happens here. This function returns the first ref to an inode as it
  2112. * would look like while receiving the stream at this point in time.
  2113. * We walk the path up to the root. For every inode in between, we check if it
  2114. * was already processed/sent. If yes, we continue with the parent as found
  2115. * in send_root. If not, we continue with the parent as found in parent_root.
  2116. * If we encounter an inode that was deleted at this point in time, we use the
  2117. * inodes "orphan" name instead of the real name and stop. Same with new inodes
  2118. * that were not created yet and overwritten inodes/refs.
  2119. *
  2120. * When do we have orphan inodes:
  2121. * 1. When an inode is freshly created and thus no valid refs are available yet
  2122. * 2. When a directory lost all it's refs (deleted) but still has dir items
  2123. * inside which were not processed yet (pending for move/delete). If anyone
  2124. * tried to get the path to the dir items, it would get a path inside that
  2125. * orphan directory.
  2126. * 3. When an inode is moved around or gets new links, it may overwrite the ref
  2127. * of an unprocessed inode. If in that case the first ref would be
  2128. * overwritten, the overwritten inode gets "orphanized". Later when we
  2129. * process this overwritten inode, it is restored at a new place by moving
  2130. * the orphan inode.
  2131. *
  2132. * sctx->send_progress tells this function at which point in time receiving
  2133. * would be.
  2134. */
  2135. static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
  2136. struct fs_path *dest)
  2137. {
  2138. int ret = 0;
  2139. struct fs_path *name = NULL;
  2140. u64 parent_inode = 0;
  2141. u64 parent_gen = 0;
  2142. int stop = 0;
  2143. const bool is_cur_inode = (ino == sctx->cur_ino && gen == sctx->cur_inode_gen);
  2144. if (is_cur_inode && fs_path_len(&sctx->cur_inode_path) > 0) {
  2145. if (dest != &sctx->cur_inode_path)
  2146. return fs_path_copy(dest, &sctx->cur_inode_path);
  2147. return 0;
  2148. }
  2149. name = fs_path_alloc();
  2150. if (!name) {
  2151. ret = -ENOMEM;
  2152. goto out;
  2153. }
  2154. dest->reversed = 1;
  2155. fs_path_reset(dest);
  2156. while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
  2157. struct waiting_dir_move *wdm;
  2158. fs_path_reset(name);
  2159. if (is_waiting_for_rm(sctx, ino, gen)) {
  2160. ret = gen_unique_name(sctx, ino, gen, name);
  2161. if (ret < 0)
  2162. goto out;
  2163. ret = fs_path_add_path(dest, name);
  2164. break;
  2165. }
  2166. wdm = get_waiting_dir_move(sctx, ino);
  2167. if (wdm && wdm->orphanized) {
  2168. ret = gen_unique_name(sctx, ino, gen, name);
  2169. stop = 1;
  2170. } else if (wdm) {
  2171. ret = get_first_ref(sctx->parent_root, ino,
  2172. &parent_inode, &parent_gen, name);
  2173. } else {
  2174. ret = __get_cur_name_and_parent(sctx, ino, gen,
  2175. &parent_inode,
  2176. &parent_gen, name);
  2177. if (ret)
  2178. stop = 1;
  2179. }
  2180. if (ret < 0)
  2181. goto out;
  2182. ret = fs_path_add_path(dest, name);
  2183. if (ret < 0)
  2184. goto out;
  2185. ino = parent_inode;
  2186. gen = parent_gen;
  2187. }
  2188. out:
  2189. fs_path_free(name);
  2190. if (!ret) {
  2191. fs_path_unreverse(dest);
  2192. if (is_cur_inode && dest != &sctx->cur_inode_path)
  2193. ret = fs_path_copy(&sctx->cur_inode_path, dest);
  2194. }
  2195. return ret;
  2196. }
  2197. /*
  2198. * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
  2199. */
  2200. static int send_subvol_begin(struct send_ctx *sctx)
  2201. {
  2202. int ret;
  2203. struct btrfs_root *send_root = sctx->send_root;
  2204. struct btrfs_root *parent_root = sctx->parent_root;
  2205. struct btrfs_path *path;
  2206. struct btrfs_key key;
  2207. struct btrfs_root_ref *ref;
  2208. struct extent_buffer *leaf;
  2209. char *name = NULL;
  2210. int namelen;
  2211. path = btrfs_alloc_path();
  2212. if (!path)
  2213. return -ENOMEM;
  2214. name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_KERNEL);
  2215. if (!name) {
  2216. btrfs_free_path(path);
  2217. return -ENOMEM;
  2218. }
  2219. key.objectid = btrfs_root_id(send_root);
  2220. key.type = BTRFS_ROOT_BACKREF_KEY;
  2221. key.offset = 0;
  2222. ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
  2223. &key, path, 1, 0);
  2224. if (ret < 0)
  2225. goto out;
  2226. if (ret) {
  2227. ret = -ENOENT;
  2228. goto out;
  2229. }
  2230. leaf = path->nodes[0];
  2231. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  2232. if (key.type != BTRFS_ROOT_BACKREF_KEY ||
  2233. key.objectid != btrfs_root_id(send_root)) {
  2234. ret = -ENOENT;
  2235. goto out;
  2236. }
  2237. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
  2238. namelen = btrfs_root_ref_name_len(leaf, ref);
  2239. read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
  2240. btrfs_release_path(path);
  2241. if (parent_root) {
  2242. ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
  2243. if (ret < 0)
  2244. goto out;
  2245. } else {
  2246. ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
  2247. if (ret < 0)
  2248. goto out;
  2249. }
  2250. TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
  2251. if (!btrfs_is_empty_uuid(sctx->send_root->root_item.received_uuid))
  2252. TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
  2253. sctx->send_root->root_item.received_uuid);
  2254. else
  2255. TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
  2256. sctx->send_root->root_item.uuid);
  2257. TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
  2258. btrfs_root_ctransid(&sctx->send_root->root_item));
  2259. if (parent_root) {
  2260. if (!btrfs_is_empty_uuid(parent_root->root_item.received_uuid))
  2261. TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
  2262. parent_root->root_item.received_uuid);
  2263. else
  2264. TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
  2265. parent_root->root_item.uuid);
  2266. TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
  2267. btrfs_root_ctransid(&sctx->parent_root->root_item));
  2268. }
  2269. ret = send_cmd(sctx);
  2270. tlv_put_failure:
  2271. out:
  2272. btrfs_free_path(path);
  2273. kfree(name);
  2274. return ret;
  2275. }
  2276. static struct fs_path *get_cur_inode_path(struct send_ctx *sctx)
  2277. {
  2278. if (fs_path_len(&sctx->cur_inode_path) == 0) {
  2279. int ret;
  2280. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  2281. &sctx->cur_inode_path);
  2282. if (ret < 0)
  2283. return ERR_PTR(ret);
  2284. }
  2285. return &sctx->cur_inode_path;
  2286. }
  2287. static struct fs_path *get_path_for_command(struct send_ctx *sctx, u64 ino, u64 gen)
  2288. {
  2289. struct fs_path *path;
  2290. int ret;
  2291. if (ino == sctx->cur_ino && gen == sctx->cur_inode_gen)
  2292. return get_cur_inode_path(sctx);
  2293. path = fs_path_alloc();
  2294. if (!path)
  2295. return ERR_PTR(-ENOMEM);
  2296. ret = get_cur_path(sctx, ino, gen, path);
  2297. if (ret < 0) {
  2298. fs_path_free(path);
  2299. return ERR_PTR(ret);
  2300. }
  2301. return path;
  2302. }
  2303. static void free_path_for_command(const struct send_ctx *sctx, struct fs_path *path)
  2304. {
  2305. if (path != &sctx->cur_inode_path)
  2306. fs_path_free(path);
  2307. }
  2308. static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
  2309. {
  2310. struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
  2311. int ret = 0;
  2312. struct fs_path *p;
  2313. btrfs_debug(fs_info, "send_truncate %llu size=%llu", ino, size);
  2314. p = get_path_for_command(sctx, ino, gen);
  2315. if (IS_ERR(p))
  2316. return PTR_ERR(p);
  2317. ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
  2318. if (ret < 0)
  2319. goto out;
  2320. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  2321. TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
  2322. ret = send_cmd(sctx);
  2323. tlv_put_failure:
  2324. out:
  2325. free_path_for_command(sctx, p);
  2326. return ret;
  2327. }
  2328. static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
  2329. {
  2330. struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
  2331. int ret = 0;
  2332. struct fs_path *p;
  2333. btrfs_debug(fs_info, "send_chmod %llu mode=%llu", ino, mode);
  2334. p = get_path_for_command(sctx, ino, gen);
  2335. if (IS_ERR(p))
  2336. return PTR_ERR(p);
  2337. ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
  2338. if (ret < 0)
  2339. goto out;
  2340. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  2341. TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
  2342. ret = send_cmd(sctx);
  2343. tlv_put_failure:
  2344. out:
  2345. free_path_for_command(sctx, p);
  2346. return ret;
  2347. }
  2348. static int send_fileattr(struct send_ctx *sctx, u64 ino, u64 gen, u64 fileattr)
  2349. {
  2350. struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
  2351. int ret = 0;
  2352. struct fs_path *p;
  2353. if (sctx->proto < 2)
  2354. return 0;
  2355. btrfs_debug(fs_info, "send_fileattr %llu fileattr=%llu", ino, fileattr);
  2356. p = get_path_for_command(sctx, ino, gen);
  2357. if (IS_ERR(p))
  2358. return PTR_ERR(p);
  2359. ret = begin_cmd(sctx, BTRFS_SEND_C_FILEATTR);
  2360. if (ret < 0)
  2361. goto out;
  2362. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  2363. TLV_PUT_U64(sctx, BTRFS_SEND_A_FILEATTR, fileattr);
  2364. ret = send_cmd(sctx);
  2365. tlv_put_failure:
  2366. out:
  2367. free_path_for_command(sctx, p);
  2368. return ret;
  2369. }
  2370. static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
  2371. {
  2372. struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
  2373. int ret = 0;
  2374. struct fs_path *p;
  2375. btrfs_debug(fs_info, "send_chown %llu uid=%llu, gid=%llu",
  2376. ino, uid, gid);
  2377. p = get_path_for_command(sctx, ino, gen);
  2378. if (IS_ERR(p))
  2379. return PTR_ERR(p);
  2380. ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
  2381. if (ret < 0)
  2382. goto out;
  2383. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  2384. TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
  2385. TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
  2386. ret = send_cmd(sctx);
  2387. tlv_put_failure:
  2388. out:
  2389. free_path_for_command(sctx, p);
  2390. return ret;
  2391. }
  2392. static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
  2393. {
  2394. struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
  2395. int ret = 0;
  2396. struct fs_path *p = NULL;
  2397. struct btrfs_inode_item *ii;
  2398. struct btrfs_path *path = NULL;
  2399. struct extent_buffer *eb;
  2400. struct btrfs_key key;
  2401. int slot;
  2402. btrfs_debug(fs_info, "send_utimes %llu", ino);
  2403. p = get_path_for_command(sctx, ino, gen);
  2404. if (IS_ERR(p))
  2405. return PTR_ERR(p);
  2406. path = alloc_path_for_send();
  2407. if (!path) {
  2408. ret = -ENOMEM;
  2409. goto out;
  2410. }
  2411. key.objectid = ino;
  2412. key.type = BTRFS_INODE_ITEM_KEY;
  2413. key.offset = 0;
  2414. ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
  2415. if (ret > 0)
  2416. ret = -ENOENT;
  2417. if (ret < 0)
  2418. goto out;
  2419. eb = path->nodes[0];
  2420. slot = path->slots[0];
  2421. ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
  2422. ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
  2423. if (ret < 0)
  2424. goto out;
  2425. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  2426. TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime);
  2427. TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime);
  2428. TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime);
  2429. if (sctx->proto >= 2)
  2430. TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_OTIME, eb, &ii->otime);
  2431. ret = send_cmd(sctx);
  2432. tlv_put_failure:
  2433. out:
  2434. free_path_for_command(sctx, p);
  2435. btrfs_free_path(path);
  2436. return ret;
  2437. }
  2438. /*
  2439. * If the cache is full, we can't remove entries from it and do a call to
  2440. * send_utimes() for each respective inode, because we might be finishing
  2441. * processing an inode that is a directory and it just got renamed, and existing
  2442. * entries in the cache may refer to inodes that have the directory in their
  2443. * full path - in which case we would generate outdated paths (pre-rename)
  2444. * for the inodes that the cache entries point to. Instead of prunning the
  2445. * cache when inserting, do it after we finish processing each inode at
  2446. * finish_inode_if_needed().
  2447. */
  2448. static int cache_dir_utimes(struct send_ctx *sctx, u64 dir, u64 gen)
  2449. {
  2450. struct btrfs_lru_cache_entry *entry;
  2451. int ret;
  2452. entry = btrfs_lru_cache_lookup(&sctx->dir_utimes_cache, dir, gen);
  2453. if (entry != NULL)
  2454. return 0;
  2455. /* Caching is optional, don't fail if we can't allocate memory. */
  2456. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  2457. if (!entry)
  2458. return send_utimes(sctx, dir, gen);
  2459. entry->key = dir;
  2460. entry->gen = gen;
  2461. ret = btrfs_lru_cache_store(&sctx->dir_utimes_cache, entry, GFP_KERNEL);
  2462. ASSERT(ret != -EEXIST);
  2463. if (ret) {
  2464. kfree(entry);
  2465. return send_utimes(sctx, dir, gen);
  2466. }
  2467. return 0;
  2468. }
  2469. static int trim_dir_utimes_cache(struct send_ctx *sctx)
  2470. {
  2471. while (sctx->dir_utimes_cache.size > SEND_MAX_DIR_UTIMES_CACHE_SIZE) {
  2472. struct btrfs_lru_cache_entry *lru;
  2473. int ret;
  2474. lru = btrfs_lru_cache_lru_entry(&sctx->dir_utimes_cache);
  2475. ASSERT(lru != NULL);
  2476. ret = send_utimes(sctx, lru->key, lru->gen);
  2477. if (ret)
  2478. return ret;
  2479. btrfs_lru_cache_remove(&sctx->dir_utimes_cache, lru);
  2480. }
  2481. return 0;
  2482. }
  2483. /*
  2484. * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
  2485. * a valid path yet because we did not process the refs yet. So, the inode
  2486. * is created as orphan.
  2487. */
  2488. static int send_create_inode(struct send_ctx *sctx, u64 ino)
  2489. {
  2490. struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
  2491. int ret = 0;
  2492. struct fs_path *p;
  2493. int cmd;
  2494. struct btrfs_inode_info info;
  2495. u64 gen;
  2496. u64 mode;
  2497. u64 rdev;
  2498. btrfs_debug(fs_info, "send_create_inode %llu", ino);
  2499. p = fs_path_alloc();
  2500. if (!p)
  2501. return -ENOMEM;
  2502. if (ino != sctx->cur_ino) {
  2503. ret = get_inode_info(sctx->send_root, ino, &info);
  2504. if (ret < 0)
  2505. goto out;
  2506. gen = info.gen;
  2507. mode = info.mode;
  2508. rdev = info.rdev;
  2509. } else {
  2510. gen = sctx->cur_inode_gen;
  2511. mode = sctx->cur_inode_mode;
  2512. rdev = sctx->cur_inode_rdev;
  2513. }
  2514. if (S_ISREG(mode)) {
  2515. cmd = BTRFS_SEND_C_MKFILE;
  2516. } else if (S_ISDIR(mode)) {
  2517. cmd = BTRFS_SEND_C_MKDIR;
  2518. } else if (S_ISLNK(mode)) {
  2519. cmd = BTRFS_SEND_C_SYMLINK;
  2520. } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
  2521. cmd = BTRFS_SEND_C_MKNOD;
  2522. } else if (S_ISFIFO(mode)) {
  2523. cmd = BTRFS_SEND_C_MKFIFO;
  2524. } else if (S_ISSOCK(mode)) {
  2525. cmd = BTRFS_SEND_C_MKSOCK;
  2526. } else {
  2527. btrfs_warn(sctx->send_root->fs_info, "unexpected inode type %o",
  2528. (int)(mode & S_IFMT));
  2529. ret = -EOPNOTSUPP;
  2530. goto out;
  2531. }
  2532. ret = begin_cmd(sctx, cmd);
  2533. if (ret < 0)
  2534. goto out;
  2535. ret = gen_unique_name(sctx, ino, gen, p);
  2536. if (ret < 0)
  2537. goto out;
  2538. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  2539. TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
  2540. if (S_ISLNK(mode)) {
  2541. fs_path_reset(p);
  2542. ret = read_symlink(sctx->send_root, ino, p);
  2543. if (ret < 0)
  2544. goto out;
  2545. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
  2546. } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
  2547. S_ISFIFO(mode) || S_ISSOCK(mode)) {
  2548. TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
  2549. TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
  2550. }
  2551. ret = send_cmd(sctx);
  2552. if (ret < 0)
  2553. goto out;
  2554. tlv_put_failure:
  2555. out:
  2556. fs_path_free(p);
  2557. return ret;
  2558. }
  2559. static void cache_dir_created(struct send_ctx *sctx, u64 dir)
  2560. {
  2561. struct btrfs_lru_cache_entry *entry;
  2562. int ret;
  2563. /* Caching is optional, ignore any failures. */
  2564. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  2565. if (!entry)
  2566. return;
  2567. entry->key = dir;
  2568. entry->gen = 0;
  2569. ret = btrfs_lru_cache_store(&sctx->dir_created_cache, entry, GFP_KERNEL);
  2570. if (ret < 0)
  2571. kfree(entry);
  2572. }
  2573. /*
  2574. * We need some special handling for inodes that get processed before the parent
  2575. * directory got created. See process_recorded_refs for details.
  2576. * This function does the check if we already created the dir out of order.
  2577. */
  2578. static int did_create_dir(struct send_ctx *sctx, u64 dir)
  2579. {
  2580. int ret = 0;
  2581. int iter_ret = 0;
  2582. struct btrfs_path *path = NULL;
  2583. struct btrfs_key key;
  2584. struct btrfs_key found_key;
  2585. struct btrfs_key di_key;
  2586. struct btrfs_dir_item *di;
  2587. if (btrfs_lru_cache_lookup(&sctx->dir_created_cache, dir, 0))
  2588. return 1;
  2589. path = alloc_path_for_send();
  2590. if (!path)
  2591. return -ENOMEM;
  2592. key.objectid = dir;
  2593. key.type = BTRFS_DIR_INDEX_KEY;
  2594. key.offset = 0;
  2595. btrfs_for_each_slot(sctx->send_root, &key, &found_key, path, iter_ret) {
  2596. struct extent_buffer *eb = path->nodes[0];
  2597. if (found_key.objectid != key.objectid ||
  2598. found_key.type != key.type) {
  2599. ret = 0;
  2600. break;
  2601. }
  2602. di = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dir_item);
  2603. btrfs_dir_item_key_to_cpu(eb, di, &di_key);
  2604. if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
  2605. di_key.objectid < sctx->send_progress) {
  2606. ret = 1;
  2607. cache_dir_created(sctx, dir);
  2608. break;
  2609. }
  2610. }
  2611. /* Catch error found during iteration */
  2612. if (iter_ret < 0)
  2613. ret = iter_ret;
  2614. btrfs_free_path(path);
  2615. return ret;
  2616. }
  2617. /*
  2618. * Only creates the inode if it is:
  2619. * 1. Not a directory
  2620. * 2. Or a directory which was not created already due to out of order
  2621. * directories. See did_create_dir and process_recorded_refs for details.
  2622. */
  2623. static int send_create_inode_if_needed(struct send_ctx *sctx)
  2624. {
  2625. int ret;
  2626. if (S_ISDIR(sctx->cur_inode_mode)) {
  2627. ret = did_create_dir(sctx, sctx->cur_ino);
  2628. if (ret < 0)
  2629. return ret;
  2630. else if (ret > 0)
  2631. return 0;
  2632. }
  2633. ret = send_create_inode(sctx, sctx->cur_ino);
  2634. if (ret == 0 && S_ISDIR(sctx->cur_inode_mode))
  2635. cache_dir_created(sctx, sctx->cur_ino);
  2636. return ret;
  2637. }
  2638. struct recorded_ref {
  2639. struct list_head list;
  2640. char *name;
  2641. struct fs_path *full_path;
  2642. u64 dir;
  2643. u64 dir_gen;
  2644. int name_len;
  2645. struct rb_node node;
  2646. struct rb_root *root;
  2647. };
  2648. static struct recorded_ref *recorded_ref_alloc(void)
  2649. {
  2650. struct recorded_ref *ref;
  2651. ref = kzalloc(sizeof(*ref), GFP_KERNEL);
  2652. if (!ref)
  2653. return NULL;
  2654. RB_CLEAR_NODE(&ref->node);
  2655. INIT_LIST_HEAD(&ref->list);
  2656. return ref;
  2657. }
  2658. static void recorded_ref_free(struct recorded_ref *ref)
  2659. {
  2660. if (!ref)
  2661. return;
  2662. if (!RB_EMPTY_NODE(&ref->node))
  2663. rb_erase(&ref->node, ref->root);
  2664. list_del(&ref->list);
  2665. fs_path_free(ref->full_path);
  2666. kfree(ref);
  2667. }
  2668. static void set_ref_path(struct recorded_ref *ref, struct fs_path *path)
  2669. {
  2670. ref->full_path = path;
  2671. ref->name = (char *)kbasename(ref->full_path->start);
  2672. ref->name_len = ref->full_path->end - ref->name;
  2673. }
  2674. static int dup_ref(struct recorded_ref *ref, struct list_head *list)
  2675. {
  2676. struct recorded_ref *new;
  2677. new = recorded_ref_alloc();
  2678. if (!new)
  2679. return -ENOMEM;
  2680. new->dir = ref->dir;
  2681. new->dir_gen = ref->dir_gen;
  2682. list_add_tail(&new->list, list);
  2683. return 0;
  2684. }
  2685. static void __free_recorded_refs(struct list_head *head)
  2686. {
  2687. struct recorded_ref *cur;
  2688. while (!list_empty(head)) {
  2689. cur = list_entry(head->next, struct recorded_ref, list);
  2690. recorded_ref_free(cur);
  2691. }
  2692. }
  2693. static void free_recorded_refs(struct send_ctx *sctx)
  2694. {
  2695. __free_recorded_refs(&sctx->new_refs);
  2696. __free_recorded_refs(&sctx->deleted_refs);
  2697. }
  2698. /*
  2699. * Renames/moves a file/dir to its orphan name. Used when the first
  2700. * ref of an unprocessed inode gets overwritten and for all non empty
  2701. * directories.
  2702. */
  2703. static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
  2704. struct fs_path *path)
  2705. {
  2706. int ret;
  2707. struct fs_path *orphan;
  2708. orphan = fs_path_alloc();
  2709. if (!orphan)
  2710. return -ENOMEM;
  2711. ret = gen_unique_name(sctx, ino, gen, orphan);
  2712. if (ret < 0)
  2713. goto out;
  2714. ret = send_rename(sctx, path, orphan);
  2715. if (ret < 0)
  2716. goto out;
  2717. if (ino == sctx->cur_ino && gen == sctx->cur_inode_gen)
  2718. ret = fs_path_copy(&sctx->cur_inode_path, orphan);
  2719. out:
  2720. fs_path_free(orphan);
  2721. return ret;
  2722. }
  2723. static struct orphan_dir_info *add_orphan_dir_info(struct send_ctx *sctx,
  2724. u64 dir_ino, u64 dir_gen)
  2725. {
  2726. struct rb_node **p = &sctx->orphan_dirs.rb_node;
  2727. struct rb_node *parent = NULL;
  2728. struct orphan_dir_info *entry, *odi;
  2729. while (*p) {
  2730. parent = *p;
  2731. entry = rb_entry(parent, struct orphan_dir_info, node);
  2732. if (dir_ino < entry->ino)
  2733. p = &(*p)->rb_left;
  2734. else if (dir_ino > entry->ino)
  2735. p = &(*p)->rb_right;
  2736. else if (dir_gen < entry->gen)
  2737. p = &(*p)->rb_left;
  2738. else if (dir_gen > entry->gen)
  2739. p = &(*p)->rb_right;
  2740. else
  2741. return entry;
  2742. }
  2743. odi = kmalloc(sizeof(*odi), GFP_KERNEL);
  2744. if (!odi)
  2745. return ERR_PTR(-ENOMEM);
  2746. odi->ino = dir_ino;
  2747. odi->gen = dir_gen;
  2748. odi->last_dir_index_offset = 0;
  2749. odi->dir_high_seq_ino = 0;
  2750. rb_link_node(&odi->node, parent, p);
  2751. rb_insert_color(&odi->node, &sctx->orphan_dirs);
  2752. return odi;
  2753. }
  2754. static struct orphan_dir_info *get_orphan_dir_info(struct send_ctx *sctx,
  2755. u64 dir_ino, u64 gen)
  2756. {
  2757. struct rb_node *n = sctx->orphan_dirs.rb_node;
  2758. struct orphan_dir_info *entry;
  2759. while (n) {
  2760. entry = rb_entry(n, struct orphan_dir_info, node);
  2761. if (dir_ino < entry->ino)
  2762. n = n->rb_left;
  2763. else if (dir_ino > entry->ino)
  2764. n = n->rb_right;
  2765. else if (gen < entry->gen)
  2766. n = n->rb_left;
  2767. else if (gen > entry->gen)
  2768. n = n->rb_right;
  2769. else
  2770. return entry;
  2771. }
  2772. return NULL;
  2773. }
  2774. static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino, u64 gen)
  2775. {
  2776. struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino, gen);
  2777. return odi != NULL;
  2778. }
  2779. static void free_orphan_dir_info(struct send_ctx *sctx,
  2780. struct orphan_dir_info *odi)
  2781. {
  2782. if (!odi)
  2783. return;
  2784. rb_erase(&odi->node, &sctx->orphan_dirs);
  2785. kfree(odi);
  2786. }
  2787. /*
  2788. * Returns 1 if a directory can be removed at this point in time.
  2789. * We check this by iterating all dir items and checking if the inode behind
  2790. * the dir item was already processed.
  2791. */
  2792. static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen)
  2793. {
  2794. int ret = 0;
  2795. int iter_ret = 0;
  2796. struct btrfs_root *root = sctx->parent_root;
  2797. struct btrfs_path *path;
  2798. struct btrfs_key key;
  2799. struct btrfs_key found_key;
  2800. struct btrfs_key loc;
  2801. struct btrfs_dir_item *di;
  2802. struct orphan_dir_info *odi = NULL;
  2803. u64 dir_high_seq_ino = 0;
  2804. u64 last_dir_index_offset = 0;
  2805. /*
  2806. * Don't try to rmdir the top/root subvolume dir.
  2807. */
  2808. if (dir == BTRFS_FIRST_FREE_OBJECTID)
  2809. return 0;
  2810. odi = get_orphan_dir_info(sctx, dir, dir_gen);
  2811. if (odi && sctx->cur_ino < odi->dir_high_seq_ino)
  2812. return 0;
  2813. path = alloc_path_for_send();
  2814. if (!path)
  2815. return -ENOMEM;
  2816. if (!odi) {
  2817. /*
  2818. * Find the inode number associated with the last dir index
  2819. * entry. This is very likely the inode with the highest number
  2820. * of all inodes that have an entry in the directory. We can
  2821. * then use it to avoid future calls to can_rmdir(), when
  2822. * processing inodes with a lower number, from having to search
  2823. * the parent root b+tree for dir index keys.
  2824. */
  2825. key.objectid = dir;
  2826. key.type = BTRFS_DIR_INDEX_KEY;
  2827. key.offset = (u64)-1;
  2828. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2829. if (ret < 0) {
  2830. goto out;
  2831. } else if (ret > 0) {
  2832. /* Can't happen, the root is never empty. */
  2833. ASSERT(path->slots[0] > 0);
  2834. if (WARN_ON(path->slots[0] == 0)) {
  2835. ret = -EUCLEAN;
  2836. goto out;
  2837. }
  2838. path->slots[0]--;
  2839. }
  2840. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  2841. if (key.objectid != dir || key.type != BTRFS_DIR_INDEX_KEY) {
  2842. /* No index keys, dir can be removed. */
  2843. ret = 1;
  2844. goto out;
  2845. }
  2846. di = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2847. struct btrfs_dir_item);
  2848. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
  2849. dir_high_seq_ino = loc.objectid;
  2850. if (sctx->cur_ino < dir_high_seq_ino) {
  2851. ret = 0;
  2852. goto out;
  2853. }
  2854. btrfs_release_path(path);
  2855. }
  2856. key.objectid = dir;
  2857. key.type = BTRFS_DIR_INDEX_KEY;
  2858. key.offset = (odi ? odi->last_dir_index_offset : 0);
  2859. btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) {
  2860. struct waiting_dir_move *dm;
  2861. if (found_key.objectid != key.objectid ||
  2862. found_key.type != key.type)
  2863. break;
  2864. di = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2865. struct btrfs_dir_item);
  2866. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
  2867. dir_high_seq_ino = max(dir_high_seq_ino, loc.objectid);
  2868. last_dir_index_offset = found_key.offset;
  2869. dm = get_waiting_dir_move(sctx, loc.objectid);
  2870. if (dm) {
  2871. dm->rmdir_ino = dir;
  2872. dm->rmdir_gen = dir_gen;
  2873. ret = 0;
  2874. goto out;
  2875. }
  2876. if (loc.objectid > sctx->cur_ino) {
  2877. ret = 0;
  2878. goto out;
  2879. }
  2880. }
  2881. if (iter_ret < 0) {
  2882. ret = iter_ret;
  2883. goto out;
  2884. }
  2885. free_orphan_dir_info(sctx, odi);
  2886. ret = 1;
  2887. out:
  2888. btrfs_free_path(path);
  2889. if (ret)
  2890. return ret;
  2891. if (!odi) {
  2892. odi = add_orphan_dir_info(sctx, dir, dir_gen);
  2893. if (IS_ERR(odi))
  2894. return PTR_ERR(odi);
  2895. odi->gen = dir_gen;
  2896. }
  2897. odi->last_dir_index_offset = last_dir_index_offset;
  2898. odi->dir_high_seq_ino = max(odi->dir_high_seq_ino, dir_high_seq_ino);
  2899. return 0;
  2900. }
  2901. static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
  2902. {
  2903. struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
  2904. return entry != NULL;
  2905. }
  2906. static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino, bool orphanized)
  2907. {
  2908. struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
  2909. struct rb_node *parent = NULL;
  2910. struct waiting_dir_move *entry, *dm;
  2911. dm = kmalloc(sizeof(*dm), GFP_KERNEL);
  2912. if (!dm)
  2913. return -ENOMEM;
  2914. dm->ino = ino;
  2915. dm->rmdir_ino = 0;
  2916. dm->rmdir_gen = 0;
  2917. dm->orphanized = orphanized;
  2918. while (*p) {
  2919. parent = *p;
  2920. entry = rb_entry(parent, struct waiting_dir_move, node);
  2921. if (ino < entry->ino) {
  2922. p = &(*p)->rb_left;
  2923. } else if (ino > entry->ino) {
  2924. p = &(*p)->rb_right;
  2925. } else {
  2926. kfree(dm);
  2927. return -EEXIST;
  2928. }
  2929. }
  2930. rb_link_node(&dm->node, parent, p);
  2931. rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
  2932. return 0;
  2933. }
  2934. static struct waiting_dir_move *
  2935. get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
  2936. {
  2937. struct rb_node *n = sctx->waiting_dir_moves.rb_node;
  2938. struct waiting_dir_move *entry;
  2939. while (n) {
  2940. entry = rb_entry(n, struct waiting_dir_move, node);
  2941. if (ino < entry->ino)
  2942. n = n->rb_left;
  2943. else if (ino > entry->ino)
  2944. n = n->rb_right;
  2945. else
  2946. return entry;
  2947. }
  2948. return NULL;
  2949. }
  2950. static void free_waiting_dir_move(struct send_ctx *sctx,
  2951. struct waiting_dir_move *dm)
  2952. {
  2953. if (!dm)
  2954. return;
  2955. rb_erase(&dm->node, &sctx->waiting_dir_moves);
  2956. kfree(dm);
  2957. }
  2958. static int add_pending_dir_move(struct send_ctx *sctx,
  2959. u64 ino,
  2960. u64 ino_gen,
  2961. u64 parent_ino,
  2962. struct list_head *new_refs,
  2963. struct list_head *deleted_refs,
  2964. const bool is_orphan)
  2965. {
  2966. struct rb_node **p = &sctx->pending_dir_moves.rb_node;
  2967. struct rb_node *parent = NULL;
  2968. struct pending_dir_move *entry = NULL, *pm;
  2969. struct recorded_ref *cur;
  2970. int exists = 0;
  2971. int ret;
  2972. pm = kmalloc(sizeof(*pm), GFP_KERNEL);
  2973. if (!pm)
  2974. return -ENOMEM;
  2975. pm->parent_ino = parent_ino;
  2976. pm->ino = ino;
  2977. pm->gen = ino_gen;
  2978. INIT_LIST_HEAD(&pm->list);
  2979. INIT_LIST_HEAD(&pm->update_refs);
  2980. RB_CLEAR_NODE(&pm->node);
  2981. while (*p) {
  2982. parent = *p;
  2983. entry = rb_entry(parent, struct pending_dir_move, node);
  2984. if (parent_ino < entry->parent_ino) {
  2985. p = &(*p)->rb_left;
  2986. } else if (parent_ino > entry->parent_ino) {
  2987. p = &(*p)->rb_right;
  2988. } else {
  2989. exists = 1;
  2990. break;
  2991. }
  2992. }
  2993. list_for_each_entry(cur, deleted_refs, list) {
  2994. ret = dup_ref(cur, &pm->update_refs);
  2995. if (ret < 0)
  2996. goto out;
  2997. }
  2998. list_for_each_entry(cur, new_refs, list) {
  2999. ret = dup_ref(cur, &pm->update_refs);
  3000. if (ret < 0)
  3001. goto out;
  3002. }
  3003. ret = add_waiting_dir_move(sctx, pm->ino, is_orphan);
  3004. if (ret)
  3005. goto out;
  3006. if (exists) {
  3007. list_add_tail(&pm->list, &entry->list);
  3008. } else {
  3009. rb_link_node(&pm->node, parent, p);
  3010. rb_insert_color(&pm->node, &sctx->pending_dir_moves);
  3011. }
  3012. ret = 0;
  3013. out:
  3014. if (ret) {
  3015. __free_recorded_refs(&pm->update_refs);
  3016. kfree(pm);
  3017. }
  3018. return ret;
  3019. }
  3020. static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
  3021. u64 parent_ino)
  3022. {
  3023. struct rb_node *n = sctx->pending_dir_moves.rb_node;
  3024. struct pending_dir_move *entry;
  3025. while (n) {
  3026. entry = rb_entry(n, struct pending_dir_move, node);
  3027. if (parent_ino < entry->parent_ino)
  3028. n = n->rb_left;
  3029. else if (parent_ino > entry->parent_ino)
  3030. n = n->rb_right;
  3031. else
  3032. return entry;
  3033. }
  3034. return NULL;
  3035. }
  3036. static int path_loop(struct send_ctx *sctx, struct fs_path *name,
  3037. u64 ino, u64 gen, u64 *ancestor_ino)
  3038. {
  3039. int ret = 0;
  3040. u64 parent_inode = 0;
  3041. u64 parent_gen = 0;
  3042. u64 start_ino = ino;
  3043. *ancestor_ino = 0;
  3044. while (ino != BTRFS_FIRST_FREE_OBJECTID) {
  3045. fs_path_reset(name);
  3046. if (is_waiting_for_rm(sctx, ino, gen))
  3047. break;
  3048. if (is_waiting_for_move(sctx, ino)) {
  3049. if (*ancestor_ino == 0)
  3050. *ancestor_ino = ino;
  3051. ret = get_first_ref(sctx->parent_root, ino,
  3052. &parent_inode, &parent_gen, name);
  3053. } else {
  3054. ret = __get_cur_name_and_parent(sctx, ino, gen,
  3055. &parent_inode,
  3056. &parent_gen, name);
  3057. if (ret > 0) {
  3058. ret = 0;
  3059. break;
  3060. }
  3061. }
  3062. if (ret < 0)
  3063. break;
  3064. if (parent_inode == start_ino) {
  3065. ret = 1;
  3066. if (*ancestor_ino == 0)
  3067. *ancestor_ino = ino;
  3068. break;
  3069. }
  3070. ino = parent_inode;
  3071. gen = parent_gen;
  3072. }
  3073. return ret;
  3074. }
  3075. static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
  3076. {
  3077. struct fs_path *from_path = NULL;
  3078. struct fs_path *to_path = NULL;
  3079. struct fs_path *name = NULL;
  3080. u64 orig_progress = sctx->send_progress;
  3081. struct recorded_ref *cur;
  3082. u64 parent_ino, parent_gen;
  3083. struct waiting_dir_move *dm = NULL;
  3084. u64 rmdir_ino = 0;
  3085. u64 rmdir_gen;
  3086. u64 ancestor;
  3087. bool is_orphan;
  3088. int ret;
  3089. name = fs_path_alloc();
  3090. from_path = fs_path_alloc();
  3091. if (!name || !from_path) {
  3092. ret = -ENOMEM;
  3093. goto out;
  3094. }
  3095. dm = get_waiting_dir_move(sctx, pm->ino);
  3096. ASSERT(dm);
  3097. rmdir_ino = dm->rmdir_ino;
  3098. rmdir_gen = dm->rmdir_gen;
  3099. is_orphan = dm->orphanized;
  3100. free_waiting_dir_move(sctx, dm);
  3101. if (is_orphan) {
  3102. ret = gen_unique_name(sctx, pm->ino,
  3103. pm->gen, from_path);
  3104. } else {
  3105. ret = get_first_ref(sctx->parent_root, pm->ino,
  3106. &parent_ino, &parent_gen, name);
  3107. if (ret < 0)
  3108. goto out;
  3109. ret = get_cur_path(sctx, parent_ino, parent_gen,
  3110. from_path);
  3111. if (ret < 0)
  3112. goto out;
  3113. ret = fs_path_add_path(from_path, name);
  3114. }
  3115. if (ret < 0)
  3116. goto out;
  3117. sctx->send_progress = sctx->cur_ino + 1;
  3118. ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor);
  3119. if (ret < 0)
  3120. goto out;
  3121. if (ret) {
  3122. LIST_HEAD(deleted_refs);
  3123. ASSERT(ancestor > BTRFS_FIRST_FREE_OBJECTID);
  3124. ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor,
  3125. &pm->update_refs, &deleted_refs,
  3126. is_orphan);
  3127. if (ret < 0)
  3128. goto out;
  3129. if (rmdir_ino) {
  3130. dm = get_waiting_dir_move(sctx, pm->ino);
  3131. ASSERT(dm);
  3132. dm->rmdir_ino = rmdir_ino;
  3133. dm->rmdir_gen = rmdir_gen;
  3134. }
  3135. goto out;
  3136. }
  3137. fs_path_reset(name);
  3138. to_path = name;
  3139. name = NULL;
  3140. ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
  3141. if (ret < 0)
  3142. goto out;
  3143. ret = send_rename(sctx, from_path, to_path);
  3144. if (ret < 0)
  3145. goto out;
  3146. if (rmdir_ino) {
  3147. struct orphan_dir_info *odi;
  3148. u64 gen;
  3149. odi = get_orphan_dir_info(sctx, rmdir_ino, rmdir_gen);
  3150. if (!odi) {
  3151. /* already deleted */
  3152. goto finish;
  3153. }
  3154. gen = odi->gen;
  3155. ret = can_rmdir(sctx, rmdir_ino, gen);
  3156. if (ret < 0)
  3157. goto out;
  3158. if (!ret)
  3159. goto finish;
  3160. name = fs_path_alloc();
  3161. if (!name) {
  3162. ret = -ENOMEM;
  3163. goto out;
  3164. }
  3165. ret = get_cur_path(sctx, rmdir_ino, gen, name);
  3166. if (ret < 0)
  3167. goto out;
  3168. ret = send_rmdir(sctx, name);
  3169. if (ret < 0)
  3170. goto out;
  3171. }
  3172. finish:
  3173. ret = cache_dir_utimes(sctx, pm->ino, pm->gen);
  3174. if (ret < 0)
  3175. goto out;
  3176. /*
  3177. * After rename/move, need to update the utimes of both new parent(s)
  3178. * and old parent(s).
  3179. */
  3180. list_for_each_entry(cur, &pm->update_refs, list) {
  3181. /*
  3182. * The parent inode might have been deleted in the send snapshot
  3183. */
  3184. ret = get_inode_info(sctx->send_root, cur->dir, NULL);
  3185. if (ret == -ENOENT) {
  3186. ret = 0;
  3187. continue;
  3188. }
  3189. if (ret < 0)
  3190. goto out;
  3191. ret = cache_dir_utimes(sctx, cur->dir, cur->dir_gen);
  3192. if (ret < 0)
  3193. goto out;
  3194. }
  3195. out:
  3196. fs_path_free(name);
  3197. fs_path_free(from_path);
  3198. fs_path_free(to_path);
  3199. sctx->send_progress = orig_progress;
  3200. return ret;
  3201. }
  3202. static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
  3203. {
  3204. if (!list_empty(&m->list))
  3205. list_del(&m->list);
  3206. if (!RB_EMPTY_NODE(&m->node))
  3207. rb_erase(&m->node, &sctx->pending_dir_moves);
  3208. __free_recorded_refs(&m->update_refs);
  3209. kfree(m);
  3210. }
  3211. static void tail_append_pending_moves(struct send_ctx *sctx,
  3212. struct pending_dir_move *moves,
  3213. struct list_head *stack)
  3214. {
  3215. if (list_empty(&moves->list)) {
  3216. list_add_tail(&moves->list, stack);
  3217. } else {
  3218. LIST_HEAD(list);
  3219. list_splice_init(&moves->list, &list);
  3220. list_add_tail(&moves->list, stack);
  3221. list_splice_tail(&list, stack);
  3222. }
  3223. if (!RB_EMPTY_NODE(&moves->node)) {
  3224. rb_erase(&moves->node, &sctx->pending_dir_moves);
  3225. RB_CLEAR_NODE(&moves->node);
  3226. }
  3227. }
  3228. static int apply_children_dir_moves(struct send_ctx *sctx)
  3229. {
  3230. struct pending_dir_move *pm;
  3231. LIST_HEAD(stack);
  3232. u64 parent_ino = sctx->cur_ino;
  3233. int ret = 0;
  3234. pm = get_pending_dir_moves(sctx, parent_ino);
  3235. if (!pm)
  3236. return 0;
  3237. tail_append_pending_moves(sctx, pm, &stack);
  3238. while (!list_empty(&stack)) {
  3239. pm = list_first_entry(&stack, struct pending_dir_move, list);
  3240. parent_ino = pm->ino;
  3241. ret = apply_dir_move(sctx, pm);
  3242. free_pending_move(sctx, pm);
  3243. if (ret)
  3244. goto out;
  3245. pm = get_pending_dir_moves(sctx, parent_ino);
  3246. if (pm)
  3247. tail_append_pending_moves(sctx, pm, &stack);
  3248. }
  3249. return 0;
  3250. out:
  3251. while (!list_empty(&stack)) {
  3252. pm = list_first_entry(&stack, struct pending_dir_move, list);
  3253. free_pending_move(sctx, pm);
  3254. }
  3255. return ret;
  3256. }
  3257. /*
  3258. * We might need to delay a directory rename even when no ancestor directory
  3259. * (in the send root) with a higher inode number than ours (sctx->cur_ino) was
  3260. * renamed. This happens when we rename a directory to the old name (the name
  3261. * in the parent root) of some other unrelated directory that got its rename
  3262. * delayed due to some ancestor with higher number that got renamed.
  3263. *
  3264. * Example:
  3265. *
  3266. * Parent snapshot:
  3267. * . (ino 256)
  3268. * |---- a/ (ino 257)
  3269. * | |---- file (ino 260)
  3270. * |
  3271. * |---- b/ (ino 258)
  3272. * |---- c/ (ino 259)
  3273. *
  3274. * Send snapshot:
  3275. * . (ino 256)
  3276. * |---- a/ (ino 258)
  3277. * |---- x/ (ino 259)
  3278. * |---- y/ (ino 257)
  3279. * |----- file (ino 260)
  3280. *
  3281. * Here we can not rename 258 from 'b' to 'a' without the rename of inode 257
  3282. * from 'a' to 'x/y' happening first, which in turn depends on the rename of
  3283. * inode 259 from 'c' to 'x'. So the order of rename commands the send stream
  3284. * must issue is:
  3285. *
  3286. * 1 - rename 259 from 'c' to 'x'
  3287. * 2 - rename 257 from 'a' to 'x/y'
  3288. * 3 - rename 258 from 'b' to 'a'
  3289. *
  3290. * Returns 1 if the rename of sctx->cur_ino needs to be delayed, 0 if it can
  3291. * be done right away and < 0 on error.
  3292. */
  3293. static int wait_for_dest_dir_move(struct send_ctx *sctx,
  3294. struct recorded_ref *parent_ref,
  3295. const bool is_orphan)
  3296. {
  3297. struct btrfs_fs_info *fs_info = sctx->parent_root->fs_info;
  3298. struct btrfs_path *path;
  3299. struct btrfs_key key;
  3300. struct btrfs_key di_key;
  3301. struct btrfs_dir_item *di;
  3302. u64 left_gen;
  3303. u64 right_gen;
  3304. int ret = 0;
  3305. struct waiting_dir_move *wdm;
  3306. if (RB_EMPTY_ROOT(&sctx->waiting_dir_moves))
  3307. return 0;
  3308. path = alloc_path_for_send();
  3309. if (!path)
  3310. return -ENOMEM;
  3311. key.objectid = parent_ref->dir;
  3312. key.type = BTRFS_DIR_ITEM_KEY;
  3313. key.offset = btrfs_name_hash(parent_ref->name, parent_ref->name_len);
  3314. ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
  3315. if (ret < 0) {
  3316. goto out;
  3317. } else if (ret > 0) {
  3318. ret = 0;
  3319. goto out;
  3320. }
  3321. di = btrfs_match_dir_item_name(fs_info, path, parent_ref->name,
  3322. parent_ref->name_len);
  3323. if (!di) {
  3324. ret = 0;
  3325. goto out;
  3326. }
  3327. /*
  3328. * di_key.objectid has the number of the inode that has a dentry in the
  3329. * parent directory with the same name that sctx->cur_ino is being
  3330. * renamed to. We need to check if that inode is in the send root as
  3331. * well and if it is currently marked as an inode with a pending rename,
  3332. * if it is, we need to delay the rename of sctx->cur_ino as well, so
  3333. * that it happens after that other inode is renamed.
  3334. */
  3335. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &di_key);
  3336. if (di_key.type != BTRFS_INODE_ITEM_KEY) {
  3337. ret = 0;
  3338. goto out;
  3339. }
  3340. ret = get_inode_gen(sctx->parent_root, di_key.objectid, &left_gen);
  3341. if (ret < 0)
  3342. goto out;
  3343. ret = get_inode_gen(sctx->send_root, di_key.objectid, &right_gen);
  3344. if (ret < 0) {
  3345. if (ret == -ENOENT)
  3346. ret = 0;
  3347. goto out;
  3348. }
  3349. /* Different inode, no need to delay the rename of sctx->cur_ino */
  3350. if (right_gen != left_gen) {
  3351. ret = 0;
  3352. goto out;
  3353. }
  3354. wdm = get_waiting_dir_move(sctx, di_key.objectid);
  3355. if (wdm && !wdm->orphanized) {
  3356. ret = add_pending_dir_move(sctx,
  3357. sctx->cur_ino,
  3358. sctx->cur_inode_gen,
  3359. di_key.objectid,
  3360. &sctx->new_refs,
  3361. &sctx->deleted_refs,
  3362. is_orphan);
  3363. if (!ret)
  3364. ret = 1;
  3365. }
  3366. out:
  3367. btrfs_free_path(path);
  3368. return ret;
  3369. }
  3370. /*
  3371. * Check if inode ino2, or any of its ancestors, is inode ino1.
  3372. * Return 1 if true, 0 if false and < 0 on error.
  3373. */
  3374. static int check_ino_in_path(struct btrfs_root *root,
  3375. const u64 ino1,
  3376. const u64 ino1_gen,
  3377. const u64 ino2,
  3378. const u64 ino2_gen,
  3379. struct fs_path *fs_path)
  3380. {
  3381. u64 ino = ino2;
  3382. if (ino1 == ino2)
  3383. return ino1_gen == ino2_gen;
  3384. while (ino > BTRFS_FIRST_FREE_OBJECTID) {
  3385. u64 parent;
  3386. u64 parent_gen;
  3387. int ret;
  3388. fs_path_reset(fs_path);
  3389. ret = get_first_ref(root, ino, &parent, &parent_gen, fs_path);
  3390. if (ret < 0)
  3391. return ret;
  3392. if (parent == ino1)
  3393. return parent_gen == ino1_gen;
  3394. ino = parent;
  3395. }
  3396. return 0;
  3397. }
  3398. /*
  3399. * Check if inode ino1 is an ancestor of inode ino2 in the given root for any
  3400. * possible path (in case ino2 is not a directory and has multiple hard links).
  3401. * Return 1 if true, 0 if false and < 0 on error.
  3402. */
  3403. static int is_ancestor(struct btrfs_root *root,
  3404. const u64 ino1,
  3405. const u64 ino1_gen,
  3406. const u64 ino2,
  3407. struct fs_path *fs_path)
  3408. {
  3409. bool free_fs_path = false;
  3410. int ret = 0;
  3411. int iter_ret = 0;
  3412. struct btrfs_path *path = NULL;
  3413. struct btrfs_key key;
  3414. if (!fs_path) {
  3415. fs_path = fs_path_alloc();
  3416. if (!fs_path)
  3417. return -ENOMEM;
  3418. free_fs_path = true;
  3419. }
  3420. path = alloc_path_for_send();
  3421. if (!path) {
  3422. ret = -ENOMEM;
  3423. goto out;
  3424. }
  3425. key.objectid = ino2;
  3426. key.type = BTRFS_INODE_REF_KEY;
  3427. key.offset = 0;
  3428. btrfs_for_each_slot(root, &key, &key, path, iter_ret) {
  3429. struct extent_buffer *leaf = path->nodes[0];
  3430. int slot = path->slots[0];
  3431. u32 cur_offset = 0;
  3432. u32 item_size;
  3433. if (key.objectid != ino2)
  3434. break;
  3435. if (key.type != BTRFS_INODE_REF_KEY &&
  3436. key.type != BTRFS_INODE_EXTREF_KEY)
  3437. break;
  3438. item_size = btrfs_item_size(leaf, slot);
  3439. while (cur_offset < item_size) {
  3440. u64 parent;
  3441. u64 parent_gen;
  3442. if (key.type == BTRFS_INODE_EXTREF_KEY) {
  3443. unsigned long ptr;
  3444. struct btrfs_inode_extref *extref;
  3445. ptr = btrfs_item_ptr_offset(leaf, slot);
  3446. extref = (struct btrfs_inode_extref *)
  3447. (ptr + cur_offset);
  3448. parent = btrfs_inode_extref_parent(leaf,
  3449. extref);
  3450. cur_offset += sizeof(*extref);
  3451. cur_offset += btrfs_inode_extref_name_len(leaf,
  3452. extref);
  3453. } else {
  3454. parent = key.offset;
  3455. cur_offset = item_size;
  3456. }
  3457. ret = get_inode_gen(root, parent, &parent_gen);
  3458. if (ret < 0)
  3459. goto out;
  3460. ret = check_ino_in_path(root, ino1, ino1_gen,
  3461. parent, parent_gen, fs_path);
  3462. if (ret)
  3463. goto out;
  3464. }
  3465. }
  3466. ret = 0;
  3467. if (iter_ret < 0)
  3468. ret = iter_ret;
  3469. out:
  3470. btrfs_free_path(path);
  3471. if (free_fs_path)
  3472. fs_path_free(fs_path);
  3473. return ret;
  3474. }
  3475. static int wait_for_parent_move(struct send_ctx *sctx,
  3476. struct recorded_ref *parent_ref,
  3477. const bool is_orphan)
  3478. {
  3479. int ret = 0;
  3480. u64 ino = parent_ref->dir;
  3481. u64 ino_gen = parent_ref->dir_gen;
  3482. u64 parent_ino_before, parent_ino_after;
  3483. struct fs_path *path_before = NULL;
  3484. struct fs_path *path_after = NULL;
  3485. int len1, len2;
  3486. path_after = fs_path_alloc();
  3487. path_before = fs_path_alloc();
  3488. if (!path_after || !path_before) {
  3489. ret = -ENOMEM;
  3490. goto out;
  3491. }
  3492. /*
  3493. * Our current directory inode may not yet be renamed/moved because some
  3494. * ancestor (immediate or not) has to be renamed/moved first. So find if
  3495. * such ancestor exists and make sure our own rename/move happens after
  3496. * that ancestor is processed to avoid path build infinite loops (done
  3497. * at get_cur_path()).
  3498. */
  3499. while (ino > BTRFS_FIRST_FREE_OBJECTID) {
  3500. u64 parent_ino_after_gen;
  3501. if (is_waiting_for_move(sctx, ino)) {
  3502. /*
  3503. * If the current inode is an ancestor of ino in the
  3504. * parent root, we need to delay the rename of the
  3505. * current inode, otherwise don't delayed the rename
  3506. * because we can end up with a circular dependency
  3507. * of renames, resulting in some directories never
  3508. * getting the respective rename operations issued in
  3509. * the send stream or getting into infinite path build
  3510. * loops.
  3511. */
  3512. ret = is_ancestor(sctx->parent_root,
  3513. sctx->cur_ino, sctx->cur_inode_gen,
  3514. ino, path_before);
  3515. if (ret)
  3516. break;
  3517. }
  3518. fs_path_reset(path_before);
  3519. fs_path_reset(path_after);
  3520. ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
  3521. &parent_ino_after_gen, path_after);
  3522. if (ret < 0)
  3523. goto out;
  3524. ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
  3525. NULL, path_before);
  3526. if (ret < 0 && ret != -ENOENT) {
  3527. goto out;
  3528. } else if (ret == -ENOENT) {
  3529. ret = 0;
  3530. break;
  3531. }
  3532. len1 = fs_path_len(path_before);
  3533. len2 = fs_path_len(path_after);
  3534. if (ino > sctx->cur_ino &&
  3535. (parent_ino_before != parent_ino_after || len1 != len2 ||
  3536. memcmp(path_before->start, path_after->start, len1))) {
  3537. u64 parent_ino_gen;
  3538. ret = get_inode_gen(sctx->parent_root, ino, &parent_ino_gen);
  3539. if (ret < 0)
  3540. goto out;
  3541. if (ino_gen == parent_ino_gen) {
  3542. ret = 1;
  3543. break;
  3544. }
  3545. }
  3546. ino = parent_ino_after;
  3547. ino_gen = parent_ino_after_gen;
  3548. }
  3549. out:
  3550. fs_path_free(path_before);
  3551. fs_path_free(path_after);
  3552. if (ret == 1) {
  3553. ret = add_pending_dir_move(sctx,
  3554. sctx->cur_ino,
  3555. sctx->cur_inode_gen,
  3556. ino,
  3557. &sctx->new_refs,
  3558. &sctx->deleted_refs,
  3559. is_orphan);
  3560. if (!ret)
  3561. ret = 1;
  3562. }
  3563. return ret;
  3564. }
  3565. static int update_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
  3566. {
  3567. int ret;
  3568. struct fs_path *new_path;
  3569. /*
  3570. * Our reference's name member points to its full_path member string, so
  3571. * we use here a new path.
  3572. */
  3573. new_path = fs_path_alloc();
  3574. if (!new_path)
  3575. return -ENOMEM;
  3576. ret = get_cur_path(sctx, ref->dir, ref->dir_gen, new_path);
  3577. if (ret < 0) {
  3578. fs_path_free(new_path);
  3579. return ret;
  3580. }
  3581. ret = fs_path_add(new_path, ref->name, ref->name_len);
  3582. if (ret < 0) {
  3583. fs_path_free(new_path);
  3584. return ret;
  3585. }
  3586. fs_path_free(ref->full_path);
  3587. set_ref_path(ref, new_path);
  3588. return 0;
  3589. }
  3590. /*
  3591. * When processing the new references for an inode we may orphanize an existing
  3592. * directory inode because its old name conflicts with one of the new references
  3593. * of the current inode. Later, when processing another new reference of our
  3594. * inode, we might need to orphanize another inode, but the path we have in the
  3595. * reference reflects the pre-orphanization name of the directory we previously
  3596. * orphanized. For example:
  3597. *
  3598. * parent snapshot looks like:
  3599. *
  3600. * . (ino 256)
  3601. * |----- f1 (ino 257)
  3602. * |----- f2 (ino 258)
  3603. * |----- d1/ (ino 259)
  3604. * |----- d2/ (ino 260)
  3605. *
  3606. * send snapshot looks like:
  3607. *
  3608. * . (ino 256)
  3609. * |----- d1 (ino 258)
  3610. * |----- f2/ (ino 259)
  3611. * |----- f2_link/ (ino 260)
  3612. * | |----- f1 (ino 257)
  3613. * |
  3614. * |----- d2 (ino 258)
  3615. *
  3616. * When processing inode 257 we compute the name for inode 259 as "d1", and we
  3617. * cache it in the name cache. Later when we start processing inode 258, when
  3618. * collecting all its new references we set a full path of "d1/d2" for its new
  3619. * reference with name "d2". When we start processing the new references we
  3620. * start by processing the new reference with name "d1", and this results in
  3621. * orphanizing inode 259, since its old reference causes a conflict. Then we
  3622. * move on the next new reference, with name "d2", and we find out we must
  3623. * orphanize inode 260, as its old reference conflicts with ours - but for the
  3624. * orphanization we use a source path corresponding to the path we stored in the
  3625. * new reference, which is "d1/d2" and not "o259-6-0/d2" - this makes the
  3626. * receiver fail since the path component "d1/" no longer exists, it was renamed
  3627. * to "o259-6-0/" when processing the previous new reference. So in this case we
  3628. * must recompute the path in the new reference and use it for the new
  3629. * orphanization operation.
  3630. */
  3631. static int refresh_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
  3632. {
  3633. char *name;
  3634. int ret;
  3635. name = kmemdup(ref->name, ref->name_len, GFP_KERNEL);
  3636. if (!name)
  3637. return -ENOMEM;
  3638. fs_path_reset(ref->full_path);
  3639. ret = get_cur_path(sctx, ref->dir, ref->dir_gen, ref->full_path);
  3640. if (ret < 0)
  3641. goto out;
  3642. ret = fs_path_add(ref->full_path, name, ref->name_len);
  3643. if (ret < 0)
  3644. goto out;
  3645. /* Update the reference's base name pointer. */
  3646. set_ref_path(ref, ref->full_path);
  3647. out:
  3648. kfree(name);
  3649. return ret;
  3650. }
  3651. static int rename_current_inode(struct send_ctx *sctx,
  3652. struct fs_path *current_path,
  3653. struct fs_path *new_path)
  3654. {
  3655. int ret;
  3656. ret = send_rename(sctx, current_path, new_path);
  3657. if (ret < 0)
  3658. return ret;
  3659. ret = fs_path_copy(&sctx->cur_inode_path, new_path);
  3660. if (ret < 0)
  3661. return ret;
  3662. return fs_path_copy(current_path, new_path);
  3663. }
  3664. /*
  3665. * This does all the move/link/unlink/rmdir magic.
  3666. */
  3667. static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
  3668. {
  3669. struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
  3670. int ret = 0;
  3671. struct recorded_ref *cur;
  3672. struct recorded_ref *cur2;
  3673. LIST_HEAD(check_dirs);
  3674. struct fs_path *valid_path = NULL;
  3675. u64 ow_inode = 0;
  3676. u64 ow_gen;
  3677. u64 ow_mode;
  3678. u64 last_dir_ino_rm = 0;
  3679. bool did_overwrite = false;
  3680. bool is_orphan = false;
  3681. bool can_rename = true;
  3682. bool orphanized_dir = false;
  3683. bool orphanized_ancestor = false;
  3684. btrfs_debug(fs_info, "process_recorded_refs %llu", sctx->cur_ino);
  3685. /*
  3686. * This should never happen as the root dir always has the same ref
  3687. * which is always '..'
  3688. */
  3689. if (unlikely(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID)) {
  3690. btrfs_err(fs_info,
  3691. "send: unexpected inode %llu in process_recorded_refs()",
  3692. sctx->cur_ino);
  3693. ret = -EINVAL;
  3694. goto out;
  3695. }
  3696. valid_path = fs_path_alloc();
  3697. if (!valid_path) {
  3698. ret = -ENOMEM;
  3699. goto out;
  3700. }
  3701. /*
  3702. * First, check if the first ref of the current inode was overwritten
  3703. * before. If yes, we know that the current inode was already orphanized
  3704. * and thus use the orphan name. If not, we can use get_cur_path to
  3705. * get the path of the first ref as it would like while receiving at
  3706. * this point in time.
  3707. * New inodes are always orphan at the beginning, so force to use the
  3708. * orphan name in this case.
  3709. * The first ref is stored in valid_path and will be updated if it
  3710. * gets moved around.
  3711. */
  3712. if (!sctx->cur_inode_new) {
  3713. ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
  3714. sctx->cur_inode_gen);
  3715. if (ret < 0)
  3716. goto out;
  3717. if (ret)
  3718. did_overwrite = true;
  3719. }
  3720. if (sctx->cur_inode_new || did_overwrite) {
  3721. ret = gen_unique_name(sctx, sctx->cur_ino,
  3722. sctx->cur_inode_gen, valid_path);
  3723. if (ret < 0)
  3724. goto out;
  3725. is_orphan = true;
  3726. } else {
  3727. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  3728. valid_path);
  3729. if (ret < 0)
  3730. goto out;
  3731. }
  3732. /*
  3733. * Before doing any rename and link operations, do a first pass on the
  3734. * new references to orphanize any unprocessed inodes that may have a
  3735. * reference that conflicts with one of the new references of the current
  3736. * inode. This needs to happen first because a new reference may conflict
  3737. * with the old reference of a parent directory, so we must make sure
  3738. * that the path used for link and rename commands don't use an
  3739. * orphanized name when an ancestor was not yet orphanized.
  3740. *
  3741. * Example:
  3742. *
  3743. * Parent snapshot:
  3744. *
  3745. * . (ino 256)
  3746. * |----- testdir/ (ino 259)
  3747. * | |----- a (ino 257)
  3748. * |
  3749. * |----- b (ino 258)
  3750. *
  3751. * Send snapshot:
  3752. *
  3753. * . (ino 256)
  3754. * |----- testdir_2/ (ino 259)
  3755. * | |----- a (ino 260)
  3756. * |
  3757. * |----- testdir (ino 257)
  3758. * |----- b (ino 257)
  3759. * |----- b2 (ino 258)
  3760. *
  3761. * Processing the new reference for inode 257 with name "b" may happen
  3762. * before processing the new reference with name "testdir". If so, we
  3763. * must make sure that by the time we send a link command to create the
  3764. * hard link "b", inode 259 was already orphanized, since the generated
  3765. * path in "valid_path" already contains the orphanized name for 259.
  3766. * We are processing inode 257, so only later when processing 259 we do
  3767. * the rename operation to change its temporary (orphanized) name to
  3768. * "testdir_2".
  3769. */
  3770. list_for_each_entry(cur, &sctx->new_refs, list) {
  3771. ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen, NULL, NULL);
  3772. if (ret < 0)
  3773. goto out;
  3774. if (ret == inode_state_will_create)
  3775. continue;
  3776. /*
  3777. * Check if this new ref would overwrite the first ref of another
  3778. * unprocessed inode. If yes, orphanize the overwritten inode.
  3779. * If we find an overwritten ref that is not the first ref,
  3780. * simply unlink it.
  3781. */
  3782. ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
  3783. cur->name, cur->name_len,
  3784. &ow_inode, &ow_gen, &ow_mode);
  3785. if (ret < 0)
  3786. goto out;
  3787. if (ret) {
  3788. ret = is_first_ref(sctx->parent_root,
  3789. ow_inode, cur->dir, cur->name,
  3790. cur->name_len);
  3791. if (ret < 0)
  3792. goto out;
  3793. if (ret) {
  3794. struct name_cache_entry *nce;
  3795. struct waiting_dir_move *wdm;
  3796. if (orphanized_dir) {
  3797. ret = refresh_ref_path(sctx, cur);
  3798. if (ret < 0)
  3799. goto out;
  3800. }
  3801. ret = orphanize_inode(sctx, ow_inode, ow_gen,
  3802. cur->full_path);
  3803. if (ret < 0)
  3804. goto out;
  3805. if (S_ISDIR(ow_mode))
  3806. orphanized_dir = true;
  3807. /*
  3808. * If ow_inode has its rename operation delayed
  3809. * make sure that its orphanized name is used in
  3810. * the source path when performing its rename
  3811. * operation.
  3812. */
  3813. wdm = get_waiting_dir_move(sctx, ow_inode);
  3814. if (wdm)
  3815. wdm->orphanized = true;
  3816. /*
  3817. * Make sure we clear our orphanized inode's
  3818. * name from the name cache. This is because the
  3819. * inode ow_inode might be an ancestor of some
  3820. * other inode that will be orphanized as well
  3821. * later and has an inode number greater than
  3822. * sctx->send_progress. We need to prevent
  3823. * future name lookups from using the old name
  3824. * and get instead the orphan name.
  3825. */
  3826. nce = name_cache_search(sctx, ow_inode, ow_gen);
  3827. if (nce)
  3828. btrfs_lru_cache_remove(&sctx->name_cache,
  3829. &nce->entry);
  3830. /*
  3831. * ow_inode might currently be an ancestor of
  3832. * cur_ino, therefore compute valid_path (the
  3833. * current path of cur_ino) again because it
  3834. * might contain the pre-orphanization name of
  3835. * ow_inode, which is no longer valid.
  3836. */
  3837. ret = is_ancestor(sctx->parent_root,
  3838. ow_inode, ow_gen,
  3839. sctx->cur_ino, NULL);
  3840. if (ret > 0) {
  3841. orphanized_ancestor = true;
  3842. fs_path_reset(valid_path);
  3843. fs_path_reset(&sctx->cur_inode_path);
  3844. ret = get_cur_path(sctx, sctx->cur_ino,
  3845. sctx->cur_inode_gen,
  3846. valid_path);
  3847. }
  3848. if (ret < 0)
  3849. goto out;
  3850. } else {
  3851. /*
  3852. * If we previously orphanized a directory that
  3853. * collided with a new reference that we already
  3854. * processed, recompute the current path because
  3855. * that directory may be part of the path.
  3856. */
  3857. if (orphanized_dir) {
  3858. ret = refresh_ref_path(sctx, cur);
  3859. if (ret < 0)
  3860. goto out;
  3861. }
  3862. ret = send_unlink(sctx, cur->full_path);
  3863. if (ret < 0)
  3864. goto out;
  3865. }
  3866. }
  3867. }
  3868. list_for_each_entry(cur, &sctx->new_refs, list) {
  3869. /*
  3870. * We may have refs where the parent directory does not exist
  3871. * yet. This happens if the parent directories inum is higher
  3872. * than the current inum. To handle this case, we create the
  3873. * parent directory out of order. But we need to check if this
  3874. * did already happen before due to other refs in the same dir.
  3875. */
  3876. ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen, NULL, NULL);
  3877. if (ret < 0)
  3878. goto out;
  3879. if (ret == inode_state_will_create) {
  3880. ret = 0;
  3881. /*
  3882. * First check if any of the current inodes refs did
  3883. * already create the dir.
  3884. */
  3885. list_for_each_entry(cur2, &sctx->new_refs, list) {
  3886. if (cur == cur2)
  3887. break;
  3888. if (cur2->dir == cur->dir) {
  3889. ret = 1;
  3890. break;
  3891. }
  3892. }
  3893. /*
  3894. * If that did not happen, check if a previous inode
  3895. * did already create the dir.
  3896. */
  3897. if (!ret)
  3898. ret = did_create_dir(sctx, cur->dir);
  3899. if (ret < 0)
  3900. goto out;
  3901. if (!ret) {
  3902. ret = send_create_inode(sctx, cur->dir);
  3903. if (ret < 0)
  3904. goto out;
  3905. cache_dir_created(sctx, cur->dir);
  3906. }
  3907. }
  3908. if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root) {
  3909. ret = wait_for_dest_dir_move(sctx, cur, is_orphan);
  3910. if (ret < 0)
  3911. goto out;
  3912. if (ret == 1) {
  3913. can_rename = false;
  3914. *pending_move = 1;
  3915. }
  3916. }
  3917. if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root &&
  3918. can_rename) {
  3919. ret = wait_for_parent_move(sctx, cur, is_orphan);
  3920. if (ret < 0)
  3921. goto out;
  3922. if (ret == 1) {
  3923. can_rename = false;
  3924. *pending_move = 1;
  3925. }
  3926. }
  3927. /*
  3928. * link/move the ref to the new place. If we have an orphan
  3929. * inode, move it and update valid_path. If not, link or move
  3930. * it depending on the inode mode.
  3931. */
  3932. if (is_orphan && can_rename) {
  3933. ret = rename_current_inode(sctx, valid_path, cur->full_path);
  3934. if (ret < 0)
  3935. goto out;
  3936. is_orphan = false;
  3937. } else if (can_rename) {
  3938. if (S_ISDIR(sctx->cur_inode_mode)) {
  3939. /*
  3940. * Dirs can't be linked, so move it. For moved
  3941. * dirs, we always have one new and one deleted
  3942. * ref. The deleted ref is ignored later.
  3943. */
  3944. ret = rename_current_inode(sctx, valid_path,
  3945. cur->full_path);
  3946. if (ret < 0)
  3947. goto out;
  3948. } else {
  3949. /*
  3950. * We might have previously orphanized an inode
  3951. * which is an ancestor of our current inode,
  3952. * so our reference's full path, which was
  3953. * computed before any such orphanizations, must
  3954. * be updated.
  3955. */
  3956. if (orphanized_dir) {
  3957. ret = update_ref_path(sctx, cur);
  3958. if (ret < 0)
  3959. goto out;
  3960. }
  3961. ret = send_link(sctx, cur->full_path,
  3962. valid_path);
  3963. if (ret < 0)
  3964. goto out;
  3965. }
  3966. }
  3967. ret = dup_ref(cur, &check_dirs);
  3968. if (ret < 0)
  3969. goto out;
  3970. }
  3971. if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
  3972. /*
  3973. * Check if we can already rmdir the directory. If not,
  3974. * orphanize it. For every dir item inside that gets deleted
  3975. * later, we do this check again and rmdir it then if possible.
  3976. * See the use of check_dirs for more details.
  3977. */
  3978. ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen);
  3979. if (ret < 0)
  3980. goto out;
  3981. if (ret) {
  3982. ret = send_rmdir(sctx, valid_path);
  3983. if (ret < 0)
  3984. goto out;
  3985. } else if (!is_orphan) {
  3986. ret = orphanize_inode(sctx, sctx->cur_ino,
  3987. sctx->cur_inode_gen, valid_path);
  3988. if (ret < 0)
  3989. goto out;
  3990. is_orphan = true;
  3991. }
  3992. list_for_each_entry(cur, &sctx->deleted_refs, list) {
  3993. ret = dup_ref(cur, &check_dirs);
  3994. if (ret < 0)
  3995. goto out;
  3996. }
  3997. } else if (S_ISDIR(sctx->cur_inode_mode) &&
  3998. !list_empty(&sctx->deleted_refs)) {
  3999. /*
  4000. * We have a moved dir. Add the old parent to check_dirs
  4001. */
  4002. cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
  4003. list);
  4004. ret = dup_ref(cur, &check_dirs);
  4005. if (ret < 0)
  4006. goto out;
  4007. } else if (!S_ISDIR(sctx->cur_inode_mode)) {
  4008. /*
  4009. * We have a non dir inode. Go through all deleted refs and
  4010. * unlink them if they were not already overwritten by other
  4011. * inodes.
  4012. */
  4013. list_for_each_entry(cur, &sctx->deleted_refs, list) {
  4014. ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
  4015. sctx->cur_ino, sctx->cur_inode_gen,
  4016. cur->name, cur->name_len);
  4017. if (ret < 0)
  4018. goto out;
  4019. if (!ret) {
  4020. /*
  4021. * If we orphanized any ancestor before, we need
  4022. * to recompute the full path for deleted names,
  4023. * since any such path was computed before we
  4024. * processed any references and orphanized any
  4025. * ancestor inode.
  4026. */
  4027. if (orphanized_ancestor) {
  4028. ret = update_ref_path(sctx, cur);
  4029. if (ret < 0)
  4030. goto out;
  4031. }
  4032. ret = send_unlink(sctx, cur->full_path);
  4033. if (ret < 0)
  4034. goto out;
  4035. if (is_current_inode_path(sctx, cur->full_path))
  4036. fs_path_reset(&sctx->cur_inode_path);
  4037. }
  4038. ret = dup_ref(cur, &check_dirs);
  4039. if (ret < 0)
  4040. goto out;
  4041. }
  4042. /*
  4043. * If the inode is still orphan, unlink the orphan. This may
  4044. * happen when a previous inode did overwrite the first ref
  4045. * of this inode and no new refs were added for the current
  4046. * inode. Unlinking does not mean that the inode is deleted in
  4047. * all cases. There may still be links to this inode in other
  4048. * places.
  4049. */
  4050. if (is_orphan) {
  4051. ret = send_unlink(sctx, valid_path);
  4052. if (ret < 0)
  4053. goto out;
  4054. }
  4055. }
  4056. /*
  4057. * We did collect all parent dirs where cur_inode was once located. We
  4058. * now go through all these dirs and check if they are pending for
  4059. * deletion and if it's finally possible to perform the rmdir now.
  4060. * We also update the inode stats of the parent dirs here.
  4061. */
  4062. list_for_each_entry(cur, &check_dirs, list) {
  4063. /*
  4064. * In case we had refs into dirs that were not processed yet,
  4065. * we don't need to do the utime and rmdir logic for these dirs.
  4066. * The dir will be processed later.
  4067. */
  4068. if (cur->dir > sctx->cur_ino)
  4069. continue;
  4070. ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen, NULL, NULL);
  4071. if (ret < 0)
  4072. goto out;
  4073. if (ret == inode_state_did_create ||
  4074. ret == inode_state_no_change) {
  4075. ret = cache_dir_utimes(sctx, cur->dir, cur->dir_gen);
  4076. if (ret < 0)
  4077. goto out;
  4078. } else if (ret == inode_state_did_delete &&
  4079. cur->dir != last_dir_ino_rm) {
  4080. ret = can_rmdir(sctx, cur->dir, cur->dir_gen);
  4081. if (ret < 0)
  4082. goto out;
  4083. if (ret) {
  4084. ret = get_cur_path(sctx, cur->dir,
  4085. cur->dir_gen, valid_path);
  4086. if (ret < 0)
  4087. goto out;
  4088. ret = send_rmdir(sctx, valid_path);
  4089. if (ret < 0)
  4090. goto out;
  4091. last_dir_ino_rm = cur->dir;
  4092. }
  4093. }
  4094. }
  4095. ret = 0;
  4096. out:
  4097. __free_recorded_refs(&check_dirs);
  4098. free_recorded_refs(sctx);
  4099. fs_path_free(valid_path);
  4100. return ret;
  4101. }
  4102. static int rbtree_ref_comp(const void *k, const struct rb_node *node)
  4103. {
  4104. const struct recorded_ref *data = k;
  4105. const struct recorded_ref *ref = rb_entry(node, struct recorded_ref, node);
  4106. int result;
  4107. if (data->dir > ref->dir)
  4108. return 1;
  4109. if (data->dir < ref->dir)
  4110. return -1;
  4111. if (data->dir_gen > ref->dir_gen)
  4112. return 1;
  4113. if (data->dir_gen < ref->dir_gen)
  4114. return -1;
  4115. if (data->name_len > ref->name_len)
  4116. return 1;
  4117. if (data->name_len < ref->name_len)
  4118. return -1;
  4119. result = strcmp(data->name, ref->name);
  4120. if (result > 0)
  4121. return 1;
  4122. if (result < 0)
  4123. return -1;
  4124. return 0;
  4125. }
  4126. static bool rbtree_ref_less(struct rb_node *node, const struct rb_node *parent)
  4127. {
  4128. const struct recorded_ref *entry = rb_entry(node, struct recorded_ref, node);
  4129. return rbtree_ref_comp(entry, parent) < 0;
  4130. }
  4131. static int record_ref_in_tree(struct rb_root *root, struct list_head *refs,
  4132. struct fs_path *name, u64 dir, u64 dir_gen,
  4133. struct send_ctx *sctx)
  4134. {
  4135. int ret = 0;
  4136. struct fs_path *path = NULL;
  4137. struct recorded_ref *ref = NULL;
  4138. path = fs_path_alloc();
  4139. if (!path) {
  4140. ret = -ENOMEM;
  4141. goto out;
  4142. }
  4143. ref = recorded_ref_alloc();
  4144. if (!ref) {
  4145. ret = -ENOMEM;
  4146. goto out;
  4147. }
  4148. ret = get_cur_path(sctx, dir, dir_gen, path);
  4149. if (ret < 0)
  4150. goto out;
  4151. ret = fs_path_add_path(path, name);
  4152. if (ret < 0)
  4153. goto out;
  4154. ref->dir = dir;
  4155. ref->dir_gen = dir_gen;
  4156. set_ref_path(ref, path);
  4157. list_add_tail(&ref->list, refs);
  4158. rb_add(&ref->node, root, rbtree_ref_less);
  4159. ref->root = root;
  4160. out:
  4161. if (ret) {
  4162. if (path && (!ref || !ref->full_path))
  4163. fs_path_free(path);
  4164. recorded_ref_free(ref);
  4165. }
  4166. return ret;
  4167. }
  4168. static int record_new_ref_if_needed(int num, u64 dir, int index,
  4169. struct fs_path *name, void *ctx)
  4170. {
  4171. int ret = 0;
  4172. struct send_ctx *sctx = ctx;
  4173. struct rb_node *node = NULL;
  4174. struct recorded_ref data;
  4175. struct recorded_ref *ref;
  4176. u64 dir_gen;
  4177. ret = get_inode_gen(sctx->send_root, dir, &dir_gen);
  4178. if (ret < 0)
  4179. goto out;
  4180. data.dir = dir;
  4181. data.dir_gen = dir_gen;
  4182. set_ref_path(&data, name);
  4183. node = rb_find(&data, &sctx->rbtree_deleted_refs, rbtree_ref_comp);
  4184. if (node) {
  4185. ref = rb_entry(node, struct recorded_ref, node);
  4186. recorded_ref_free(ref);
  4187. } else {
  4188. ret = record_ref_in_tree(&sctx->rbtree_new_refs,
  4189. &sctx->new_refs, name, dir, dir_gen,
  4190. sctx);
  4191. }
  4192. out:
  4193. return ret;
  4194. }
  4195. static int record_deleted_ref_if_needed(int num, u64 dir, int index,
  4196. struct fs_path *name, void *ctx)
  4197. {
  4198. int ret = 0;
  4199. struct send_ctx *sctx = ctx;
  4200. struct rb_node *node = NULL;
  4201. struct recorded_ref data;
  4202. struct recorded_ref *ref;
  4203. u64 dir_gen;
  4204. ret = get_inode_gen(sctx->parent_root, dir, &dir_gen);
  4205. if (ret < 0)
  4206. goto out;
  4207. data.dir = dir;
  4208. data.dir_gen = dir_gen;
  4209. set_ref_path(&data, name);
  4210. node = rb_find(&data, &sctx->rbtree_new_refs, rbtree_ref_comp);
  4211. if (node) {
  4212. ref = rb_entry(node, struct recorded_ref, node);
  4213. recorded_ref_free(ref);
  4214. } else {
  4215. ret = record_ref_in_tree(&sctx->rbtree_deleted_refs,
  4216. &sctx->deleted_refs, name, dir,
  4217. dir_gen, sctx);
  4218. }
  4219. out:
  4220. return ret;
  4221. }
  4222. static int record_new_ref(struct send_ctx *sctx)
  4223. {
  4224. int ret;
  4225. ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
  4226. sctx->cmp_key, 0, record_new_ref_if_needed, sctx);
  4227. if (ret < 0)
  4228. goto out;
  4229. ret = 0;
  4230. out:
  4231. return ret;
  4232. }
  4233. static int record_deleted_ref(struct send_ctx *sctx)
  4234. {
  4235. int ret;
  4236. ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
  4237. sctx->cmp_key, 0, record_deleted_ref_if_needed,
  4238. sctx);
  4239. if (ret < 0)
  4240. goto out;
  4241. ret = 0;
  4242. out:
  4243. return ret;
  4244. }
  4245. static int record_changed_ref(struct send_ctx *sctx)
  4246. {
  4247. int ret = 0;
  4248. ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
  4249. sctx->cmp_key, 0, record_new_ref_if_needed, sctx);
  4250. if (ret < 0)
  4251. goto out;
  4252. ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
  4253. sctx->cmp_key, 0, record_deleted_ref_if_needed, sctx);
  4254. if (ret < 0)
  4255. goto out;
  4256. ret = 0;
  4257. out:
  4258. return ret;
  4259. }
  4260. /*
  4261. * Record and process all refs at once. Needed when an inode changes the
  4262. * generation number, which means that it was deleted and recreated.
  4263. */
  4264. static int process_all_refs(struct send_ctx *sctx,
  4265. enum btrfs_compare_tree_result cmd)
  4266. {
  4267. int ret = 0;
  4268. int iter_ret = 0;
  4269. struct btrfs_root *root;
  4270. struct btrfs_path *path;
  4271. struct btrfs_key key;
  4272. struct btrfs_key found_key;
  4273. iterate_inode_ref_t cb;
  4274. int pending_move = 0;
  4275. path = alloc_path_for_send();
  4276. if (!path)
  4277. return -ENOMEM;
  4278. if (cmd == BTRFS_COMPARE_TREE_NEW) {
  4279. root = sctx->send_root;
  4280. cb = record_new_ref_if_needed;
  4281. } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
  4282. root = sctx->parent_root;
  4283. cb = record_deleted_ref_if_needed;
  4284. } else {
  4285. btrfs_err(sctx->send_root->fs_info,
  4286. "Wrong command %d in process_all_refs", cmd);
  4287. ret = -EINVAL;
  4288. goto out;
  4289. }
  4290. key.objectid = sctx->cmp_key->objectid;
  4291. key.type = BTRFS_INODE_REF_KEY;
  4292. key.offset = 0;
  4293. btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) {
  4294. if (found_key.objectid != key.objectid ||
  4295. (found_key.type != BTRFS_INODE_REF_KEY &&
  4296. found_key.type != BTRFS_INODE_EXTREF_KEY))
  4297. break;
  4298. ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
  4299. if (ret < 0)
  4300. goto out;
  4301. }
  4302. /* Catch error found during iteration */
  4303. if (iter_ret < 0) {
  4304. ret = iter_ret;
  4305. goto out;
  4306. }
  4307. btrfs_release_path(path);
  4308. /*
  4309. * We don't actually care about pending_move as we are simply
  4310. * re-creating this inode and will be rename'ing it into place once we
  4311. * rename the parent directory.
  4312. */
  4313. ret = process_recorded_refs(sctx, &pending_move);
  4314. out:
  4315. btrfs_free_path(path);
  4316. return ret;
  4317. }
  4318. static int send_set_xattr(struct send_ctx *sctx,
  4319. const char *name, int name_len,
  4320. const char *data, int data_len)
  4321. {
  4322. struct fs_path *path;
  4323. int ret;
  4324. path = get_cur_inode_path(sctx);
  4325. if (IS_ERR(path))
  4326. return PTR_ERR(path);
  4327. ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
  4328. if (ret < 0)
  4329. goto out;
  4330. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
  4331. TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
  4332. TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
  4333. ret = send_cmd(sctx);
  4334. tlv_put_failure:
  4335. out:
  4336. return ret;
  4337. }
  4338. static int send_remove_xattr(struct send_ctx *sctx,
  4339. struct fs_path *path,
  4340. const char *name, int name_len)
  4341. {
  4342. int ret = 0;
  4343. ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
  4344. if (ret < 0)
  4345. goto out;
  4346. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
  4347. TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
  4348. ret = send_cmd(sctx);
  4349. tlv_put_failure:
  4350. out:
  4351. return ret;
  4352. }
  4353. static int __process_new_xattr(int num, struct btrfs_key *di_key,
  4354. const char *name, int name_len, const char *data,
  4355. int data_len, void *ctx)
  4356. {
  4357. struct send_ctx *sctx = ctx;
  4358. struct posix_acl_xattr_header dummy_acl;
  4359. /* Capabilities are emitted by finish_inode_if_needed */
  4360. if (!strncmp(name, XATTR_NAME_CAPS, name_len))
  4361. return 0;
  4362. /*
  4363. * This hack is needed because empty acls are stored as zero byte
  4364. * data in xattrs. Problem with that is, that receiving these zero byte
  4365. * acls will fail later. To fix this, we send a dummy acl list that
  4366. * only contains the version number and no entries.
  4367. */
  4368. if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
  4369. !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
  4370. if (data_len == 0) {
  4371. dummy_acl.a_version =
  4372. cpu_to_le32(POSIX_ACL_XATTR_VERSION);
  4373. data = (char *)&dummy_acl;
  4374. data_len = sizeof(dummy_acl);
  4375. }
  4376. }
  4377. return send_set_xattr(sctx, name, name_len, data, data_len);
  4378. }
  4379. static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
  4380. const char *name, int name_len,
  4381. const char *data, int data_len, void *ctx)
  4382. {
  4383. struct send_ctx *sctx = ctx;
  4384. struct fs_path *p;
  4385. p = get_cur_inode_path(sctx);
  4386. if (IS_ERR(p))
  4387. return PTR_ERR(p);
  4388. return send_remove_xattr(sctx, p, name, name_len);
  4389. }
  4390. static int process_new_xattr(struct send_ctx *sctx)
  4391. {
  4392. int ret = 0;
  4393. ret = iterate_dir_item(sctx->send_root, sctx->left_path,
  4394. __process_new_xattr, sctx);
  4395. return ret;
  4396. }
  4397. static int process_deleted_xattr(struct send_ctx *sctx)
  4398. {
  4399. return iterate_dir_item(sctx->parent_root, sctx->right_path,
  4400. __process_deleted_xattr, sctx);
  4401. }
  4402. struct find_xattr_ctx {
  4403. const char *name;
  4404. int name_len;
  4405. int found_idx;
  4406. char *found_data;
  4407. int found_data_len;
  4408. };
  4409. static int __find_xattr(int num, struct btrfs_key *di_key, const char *name,
  4410. int name_len, const char *data, int data_len, void *vctx)
  4411. {
  4412. struct find_xattr_ctx *ctx = vctx;
  4413. if (name_len == ctx->name_len &&
  4414. strncmp(name, ctx->name, name_len) == 0) {
  4415. ctx->found_idx = num;
  4416. ctx->found_data_len = data_len;
  4417. ctx->found_data = kmemdup(data, data_len, GFP_KERNEL);
  4418. if (!ctx->found_data)
  4419. return -ENOMEM;
  4420. return 1;
  4421. }
  4422. return 0;
  4423. }
  4424. static int find_xattr(struct btrfs_root *root,
  4425. struct btrfs_path *path,
  4426. struct btrfs_key *key,
  4427. const char *name, int name_len,
  4428. char **data, int *data_len)
  4429. {
  4430. int ret;
  4431. struct find_xattr_ctx ctx;
  4432. ctx.name = name;
  4433. ctx.name_len = name_len;
  4434. ctx.found_idx = -1;
  4435. ctx.found_data = NULL;
  4436. ctx.found_data_len = 0;
  4437. ret = iterate_dir_item(root, path, __find_xattr, &ctx);
  4438. if (ret < 0)
  4439. return ret;
  4440. if (ctx.found_idx == -1)
  4441. return -ENOENT;
  4442. if (data) {
  4443. *data = ctx.found_data;
  4444. *data_len = ctx.found_data_len;
  4445. } else {
  4446. kfree(ctx.found_data);
  4447. }
  4448. return ctx.found_idx;
  4449. }
  4450. static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
  4451. const char *name, int name_len,
  4452. const char *data, int data_len,
  4453. void *ctx)
  4454. {
  4455. int ret;
  4456. struct send_ctx *sctx = ctx;
  4457. char *found_data = NULL;
  4458. int found_data_len = 0;
  4459. ret = find_xattr(sctx->parent_root, sctx->right_path,
  4460. sctx->cmp_key, name, name_len, &found_data,
  4461. &found_data_len);
  4462. if (ret == -ENOENT) {
  4463. ret = __process_new_xattr(num, di_key, name, name_len, data,
  4464. data_len, ctx);
  4465. } else if (ret >= 0) {
  4466. if (data_len != found_data_len ||
  4467. memcmp(data, found_data, data_len)) {
  4468. ret = __process_new_xattr(num, di_key, name, name_len,
  4469. data, data_len, ctx);
  4470. } else {
  4471. ret = 0;
  4472. }
  4473. }
  4474. kfree(found_data);
  4475. return ret;
  4476. }
  4477. static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
  4478. const char *name, int name_len,
  4479. const char *data, int data_len,
  4480. void *ctx)
  4481. {
  4482. int ret;
  4483. struct send_ctx *sctx = ctx;
  4484. ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
  4485. name, name_len, NULL, NULL);
  4486. if (ret == -ENOENT)
  4487. ret = __process_deleted_xattr(num, di_key, name, name_len, data,
  4488. data_len, ctx);
  4489. else if (ret >= 0)
  4490. ret = 0;
  4491. return ret;
  4492. }
  4493. static int process_changed_xattr(struct send_ctx *sctx)
  4494. {
  4495. int ret = 0;
  4496. ret = iterate_dir_item(sctx->send_root, sctx->left_path,
  4497. __process_changed_new_xattr, sctx);
  4498. if (ret < 0)
  4499. goto out;
  4500. ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
  4501. __process_changed_deleted_xattr, sctx);
  4502. out:
  4503. return ret;
  4504. }
  4505. static int process_all_new_xattrs(struct send_ctx *sctx)
  4506. {
  4507. int ret = 0;
  4508. int iter_ret = 0;
  4509. struct btrfs_root *root;
  4510. struct btrfs_path *path;
  4511. struct btrfs_key key;
  4512. struct btrfs_key found_key;
  4513. path = alloc_path_for_send();
  4514. if (!path)
  4515. return -ENOMEM;
  4516. root = sctx->send_root;
  4517. key.objectid = sctx->cmp_key->objectid;
  4518. key.type = BTRFS_XATTR_ITEM_KEY;
  4519. key.offset = 0;
  4520. btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) {
  4521. if (found_key.objectid != key.objectid ||
  4522. found_key.type != key.type) {
  4523. ret = 0;
  4524. break;
  4525. }
  4526. ret = iterate_dir_item(root, path, __process_new_xattr, sctx);
  4527. if (ret < 0)
  4528. break;
  4529. }
  4530. /* Catch error found during iteration */
  4531. if (iter_ret < 0)
  4532. ret = iter_ret;
  4533. btrfs_free_path(path);
  4534. return ret;
  4535. }
  4536. static int send_verity(struct send_ctx *sctx, struct fs_path *path,
  4537. struct fsverity_descriptor *desc)
  4538. {
  4539. int ret;
  4540. ret = begin_cmd(sctx, BTRFS_SEND_C_ENABLE_VERITY);
  4541. if (ret < 0)
  4542. goto out;
  4543. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
  4544. TLV_PUT_U8(sctx, BTRFS_SEND_A_VERITY_ALGORITHM,
  4545. le8_to_cpu(desc->hash_algorithm));
  4546. TLV_PUT_U32(sctx, BTRFS_SEND_A_VERITY_BLOCK_SIZE,
  4547. 1U << le8_to_cpu(desc->log_blocksize));
  4548. TLV_PUT(sctx, BTRFS_SEND_A_VERITY_SALT_DATA, desc->salt,
  4549. le8_to_cpu(desc->salt_size));
  4550. TLV_PUT(sctx, BTRFS_SEND_A_VERITY_SIG_DATA, desc->signature,
  4551. le32_to_cpu(desc->sig_size));
  4552. ret = send_cmd(sctx);
  4553. tlv_put_failure:
  4554. out:
  4555. return ret;
  4556. }
  4557. static int process_verity(struct send_ctx *sctx)
  4558. {
  4559. int ret = 0;
  4560. struct inode *inode;
  4561. struct fs_path *p;
  4562. inode = btrfs_iget(sctx->cur_ino, sctx->send_root);
  4563. if (IS_ERR(inode))
  4564. return PTR_ERR(inode);
  4565. ret = btrfs_get_verity_descriptor(inode, NULL, 0);
  4566. if (ret < 0)
  4567. goto iput;
  4568. if (ret > FS_VERITY_MAX_DESCRIPTOR_SIZE) {
  4569. ret = -EMSGSIZE;
  4570. goto iput;
  4571. }
  4572. if (!sctx->verity_descriptor) {
  4573. sctx->verity_descriptor = kvmalloc(FS_VERITY_MAX_DESCRIPTOR_SIZE,
  4574. GFP_KERNEL);
  4575. if (!sctx->verity_descriptor) {
  4576. ret = -ENOMEM;
  4577. goto iput;
  4578. }
  4579. }
  4580. ret = btrfs_get_verity_descriptor(inode, sctx->verity_descriptor, ret);
  4581. if (ret < 0)
  4582. goto iput;
  4583. p = get_cur_inode_path(sctx);
  4584. if (IS_ERR(p)) {
  4585. ret = PTR_ERR(p);
  4586. goto iput;
  4587. }
  4588. ret = send_verity(sctx, p, sctx->verity_descriptor);
  4589. iput:
  4590. iput(inode);
  4591. return ret;
  4592. }
  4593. static inline u64 max_send_read_size(const struct send_ctx *sctx)
  4594. {
  4595. return sctx->send_max_size - SZ_16K;
  4596. }
  4597. static int put_data_header(struct send_ctx *sctx, u32 len)
  4598. {
  4599. if (WARN_ON_ONCE(sctx->put_data))
  4600. return -EINVAL;
  4601. sctx->put_data = true;
  4602. if (sctx->proto >= 2) {
  4603. /*
  4604. * Since v2, the data attribute header doesn't include a length,
  4605. * it is implicitly to the end of the command.
  4606. */
  4607. if (sctx->send_max_size - sctx->send_size < sizeof(__le16) + len)
  4608. return -EOVERFLOW;
  4609. put_unaligned_le16(BTRFS_SEND_A_DATA, sctx->send_buf + sctx->send_size);
  4610. sctx->send_size += sizeof(__le16);
  4611. } else {
  4612. struct btrfs_tlv_header *hdr;
  4613. if (sctx->send_max_size - sctx->send_size < sizeof(*hdr) + len)
  4614. return -EOVERFLOW;
  4615. hdr = (struct btrfs_tlv_header *)(sctx->send_buf + sctx->send_size);
  4616. put_unaligned_le16(BTRFS_SEND_A_DATA, &hdr->tlv_type);
  4617. put_unaligned_le16(len, &hdr->tlv_len);
  4618. sctx->send_size += sizeof(*hdr);
  4619. }
  4620. return 0;
  4621. }
  4622. static int put_file_data(struct send_ctx *sctx, u64 offset, u32 len)
  4623. {
  4624. struct btrfs_root *root = sctx->send_root;
  4625. struct btrfs_fs_info *fs_info = root->fs_info;
  4626. struct folio *folio;
  4627. pgoff_t index = offset >> PAGE_SHIFT;
  4628. pgoff_t last_index;
  4629. unsigned pg_offset = offset_in_page(offset);
  4630. struct address_space *mapping = sctx->cur_inode->i_mapping;
  4631. int ret;
  4632. ret = put_data_header(sctx, len);
  4633. if (ret)
  4634. return ret;
  4635. last_index = (offset + len - 1) >> PAGE_SHIFT;
  4636. while (index <= last_index) {
  4637. unsigned cur_len = min_t(unsigned, len,
  4638. PAGE_SIZE - pg_offset);
  4639. again:
  4640. folio = filemap_lock_folio(mapping, index);
  4641. if (IS_ERR(folio)) {
  4642. page_cache_sync_readahead(mapping,
  4643. &sctx->ra, NULL, index,
  4644. last_index + 1 - index);
  4645. folio = filemap_grab_folio(mapping, index);
  4646. if (IS_ERR(folio)) {
  4647. ret = PTR_ERR(folio);
  4648. break;
  4649. }
  4650. }
  4651. WARN_ON(folio_order(folio));
  4652. if (folio_test_readahead(folio))
  4653. page_cache_async_readahead(mapping, &sctx->ra, NULL, folio,
  4654. last_index + 1 - index);
  4655. if (!folio_test_uptodate(folio)) {
  4656. btrfs_read_folio(NULL, folio);
  4657. folio_lock(folio);
  4658. if (!folio_test_uptodate(folio)) {
  4659. folio_unlock(folio);
  4660. btrfs_err(fs_info,
  4661. "send: IO error at offset %llu for inode %llu root %llu",
  4662. folio_pos(folio), sctx->cur_ino,
  4663. btrfs_root_id(sctx->send_root));
  4664. folio_put(folio);
  4665. ret = -EIO;
  4666. break;
  4667. }
  4668. if (folio->mapping != mapping) {
  4669. folio_unlock(folio);
  4670. folio_put(folio);
  4671. goto again;
  4672. }
  4673. }
  4674. memcpy_from_folio(sctx->send_buf + sctx->send_size, folio,
  4675. pg_offset, cur_len);
  4676. folio_unlock(folio);
  4677. folio_put(folio);
  4678. index++;
  4679. pg_offset = 0;
  4680. len -= cur_len;
  4681. sctx->send_size += cur_len;
  4682. }
  4683. return ret;
  4684. }
  4685. /*
  4686. * Read some bytes from the current inode/file and send a write command to
  4687. * user space.
  4688. */
  4689. static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
  4690. {
  4691. struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
  4692. int ret = 0;
  4693. struct fs_path *p;
  4694. btrfs_debug(fs_info, "send_write offset=%llu, len=%d", offset, len);
  4695. p = get_cur_inode_path(sctx);
  4696. if (IS_ERR(p))
  4697. return PTR_ERR(p);
  4698. ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
  4699. if (ret < 0)
  4700. return ret;
  4701. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  4702. TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
  4703. ret = put_file_data(sctx, offset, len);
  4704. if (ret < 0)
  4705. return ret;
  4706. ret = send_cmd(sctx);
  4707. tlv_put_failure:
  4708. return ret;
  4709. }
  4710. /*
  4711. * Send a clone command to user space.
  4712. */
  4713. static int send_clone(struct send_ctx *sctx,
  4714. u64 offset, u32 len,
  4715. struct clone_root *clone_root)
  4716. {
  4717. int ret = 0;
  4718. struct fs_path *p;
  4719. struct fs_path *cur_inode_path;
  4720. u64 gen;
  4721. btrfs_debug(sctx->send_root->fs_info,
  4722. "send_clone offset=%llu, len=%d, clone_root=%llu, clone_inode=%llu, clone_offset=%llu",
  4723. offset, len, btrfs_root_id(clone_root->root),
  4724. clone_root->ino, clone_root->offset);
  4725. cur_inode_path = get_cur_inode_path(sctx);
  4726. if (IS_ERR(cur_inode_path))
  4727. return PTR_ERR(cur_inode_path);
  4728. p = fs_path_alloc();
  4729. if (!p)
  4730. return -ENOMEM;
  4731. ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
  4732. if (ret < 0)
  4733. goto out;
  4734. TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
  4735. TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
  4736. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, cur_inode_path);
  4737. if (clone_root->root == sctx->send_root) {
  4738. ret = get_inode_gen(sctx->send_root, clone_root->ino, &gen);
  4739. if (ret < 0)
  4740. goto out;
  4741. ret = get_cur_path(sctx, clone_root->ino, gen, p);
  4742. } else {
  4743. ret = get_inode_path(clone_root->root, clone_root->ino, p);
  4744. }
  4745. if (ret < 0)
  4746. goto out;
  4747. /*
  4748. * If the parent we're using has a received_uuid set then use that as
  4749. * our clone source as that is what we will look for when doing a
  4750. * receive.
  4751. *
  4752. * This covers the case that we create a snapshot off of a received
  4753. * subvolume and then use that as the parent and try to receive on a
  4754. * different host.
  4755. */
  4756. if (!btrfs_is_empty_uuid(clone_root->root->root_item.received_uuid))
  4757. TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
  4758. clone_root->root->root_item.received_uuid);
  4759. else
  4760. TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
  4761. clone_root->root->root_item.uuid);
  4762. TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
  4763. btrfs_root_ctransid(&clone_root->root->root_item));
  4764. TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
  4765. TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
  4766. clone_root->offset);
  4767. ret = send_cmd(sctx);
  4768. tlv_put_failure:
  4769. out:
  4770. fs_path_free(p);
  4771. return ret;
  4772. }
  4773. /*
  4774. * Send an update extent command to user space.
  4775. */
  4776. static int send_update_extent(struct send_ctx *sctx,
  4777. u64 offset, u32 len)
  4778. {
  4779. int ret = 0;
  4780. struct fs_path *p;
  4781. p = get_cur_inode_path(sctx);
  4782. if (IS_ERR(p))
  4783. return PTR_ERR(p);
  4784. ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
  4785. if (ret < 0)
  4786. return ret;
  4787. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  4788. TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
  4789. TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
  4790. ret = send_cmd(sctx);
  4791. tlv_put_failure:
  4792. return ret;
  4793. }
  4794. static int send_fallocate(struct send_ctx *sctx, u32 mode, u64 offset, u64 len)
  4795. {
  4796. struct fs_path *path;
  4797. int ret;
  4798. path = get_cur_inode_path(sctx);
  4799. if (IS_ERR(path))
  4800. return PTR_ERR(path);
  4801. ret = begin_cmd(sctx, BTRFS_SEND_C_FALLOCATE);
  4802. if (ret < 0)
  4803. return ret;
  4804. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
  4805. TLV_PUT_U32(sctx, BTRFS_SEND_A_FALLOCATE_MODE, mode);
  4806. TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
  4807. TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
  4808. ret = send_cmd(sctx);
  4809. tlv_put_failure:
  4810. return ret;
  4811. }
  4812. static int send_hole(struct send_ctx *sctx, u64 end)
  4813. {
  4814. struct fs_path *p = NULL;
  4815. u64 read_size = max_send_read_size(sctx);
  4816. u64 offset = sctx->cur_inode_last_extent;
  4817. int ret = 0;
  4818. /*
  4819. * Starting with send stream v2 we have fallocate and can use it to
  4820. * punch holes instead of sending writes full of zeroes.
  4821. */
  4822. if (proto_cmd_ok(sctx, BTRFS_SEND_C_FALLOCATE))
  4823. return send_fallocate(sctx, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
  4824. offset, end - offset);
  4825. /*
  4826. * A hole that starts at EOF or beyond it. Since we do not yet support
  4827. * fallocate (for extent preallocation and hole punching), sending a
  4828. * write of zeroes starting at EOF or beyond would later require issuing
  4829. * a truncate operation which would undo the write and achieve nothing.
  4830. */
  4831. if (offset >= sctx->cur_inode_size)
  4832. return 0;
  4833. /*
  4834. * Don't go beyond the inode's i_size due to prealloc extents that start
  4835. * after the i_size.
  4836. */
  4837. end = min_t(u64, end, sctx->cur_inode_size);
  4838. if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
  4839. return send_update_extent(sctx, offset, end - offset);
  4840. p = get_cur_inode_path(sctx);
  4841. if (IS_ERR(p))
  4842. return PTR_ERR(p);
  4843. while (offset < end) {
  4844. u64 len = min(end - offset, read_size);
  4845. ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
  4846. if (ret < 0)
  4847. break;
  4848. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  4849. TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
  4850. ret = put_data_header(sctx, len);
  4851. if (ret < 0)
  4852. break;
  4853. memset(sctx->send_buf + sctx->send_size, 0, len);
  4854. sctx->send_size += len;
  4855. ret = send_cmd(sctx);
  4856. if (ret < 0)
  4857. break;
  4858. offset += len;
  4859. }
  4860. sctx->cur_inode_next_write_offset = offset;
  4861. tlv_put_failure:
  4862. return ret;
  4863. }
  4864. static int send_encoded_inline_extent(struct send_ctx *sctx,
  4865. struct btrfs_path *path, u64 offset,
  4866. u64 len)
  4867. {
  4868. struct btrfs_root *root = sctx->send_root;
  4869. struct btrfs_fs_info *fs_info = root->fs_info;
  4870. struct inode *inode;
  4871. struct fs_path *fspath;
  4872. struct extent_buffer *leaf = path->nodes[0];
  4873. struct btrfs_key key;
  4874. struct btrfs_file_extent_item *ei;
  4875. u64 ram_bytes;
  4876. size_t inline_size;
  4877. int ret;
  4878. inode = btrfs_iget(sctx->cur_ino, root);
  4879. if (IS_ERR(inode))
  4880. return PTR_ERR(inode);
  4881. fspath = get_cur_inode_path(sctx);
  4882. if (IS_ERR(fspath)) {
  4883. ret = PTR_ERR(fspath);
  4884. goto out;
  4885. }
  4886. ret = begin_cmd(sctx, BTRFS_SEND_C_ENCODED_WRITE);
  4887. if (ret < 0)
  4888. goto out;
  4889. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  4890. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item);
  4891. ram_bytes = btrfs_file_extent_ram_bytes(leaf, ei);
  4892. inline_size = btrfs_file_extent_inline_item_len(leaf, path->slots[0]);
  4893. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, fspath);
  4894. TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
  4895. TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_FILE_LEN,
  4896. min(key.offset + ram_bytes - offset, len));
  4897. TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_LEN, ram_bytes);
  4898. TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_OFFSET, offset - key.offset);
  4899. ret = btrfs_encoded_io_compression_from_extent(fs_info,
  4900. btrfs_file_extent_compression(leaf, ei));
  4901. if (ret < 0)
  4902. goto out;
  4903. TLV_PUT_U32(sctx, BTRFS_SEND_A_COMPRESSION, ret);
  4904. ret = put_data_header(sctx, inline_size);
  4905. if (ret < 0)
  4906. goto out;
  4907. read_extent_buffer(leaf, sctx->send_buf + sctx->send_size,
  4908. btrfs_file_extent_inline_start(ei), inline_size);
  4909. sctx->send_size += inline_size;
  4910. ret = send_cmd(sctx);
  4911. tlv_put_failure:
  4912. out:
  4913. iput(inode);
  4914. return ret;
  4915. }
  4916. static int send_encoded_extent(struct send_ctx *sctx, struct btrfs_path *path,
  4917. u64 offset, u64 len)
  4918. {
  4919. struct btrfs_root *root = sctx->send_root;
  4920. struct btrfs_fs_info *fs_info = root->fs_info;
  4921. struct inode *inode;
  4922. struct fs_path *fspath;
  4923. struct extent_buffer *leaf = path->nodes[0];
  4924. struct btrfs_key key;
  4925. struct btrfs_file_extent_item *ei;
  4926. u64 disk_bytenr, disk_num_bytes;
  4927. u32 data_offset;
  4928. struct btrfs_cmd_header *hdr;
  4929. u32 crc;
  4930. int ret;
  4931. inode = btrfs_iget(sctx->cur_ino, root);
  4932. if (IS_ERR(inode))
  4933. return PTR_ERR(inode);
  4934. fspath = get_cur_inode_path(sctx);
  4935. if (IS_ERR(fspath)) {
  4936. ret = PTR_ERR(fspath);
  4937. goto out;
  4938. }
  4939. ret = begin_cmd(sctx, BTRFS_SEND_C_ENCODED_WRITE);
  4940. if (ret < 0)
  4941. goto out;
  4942. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  4943. ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item);
  4944. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, ei);
  4945. disk_num_bytes = btrfs_file_extent_disk_num_bytes(leaf, ei);
  4946. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, fspath);
  4947. TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
  4948. TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_FILE_LEN,
  4949. min(key.offset + btrfs_file_extent_num_bytes(leaf, ei) - offset,
  4950. len));
  4951. TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_LEN,
  4952. btrfs_file_extent_ram_bytes(leaf, ei));
  4953. TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_OFFSET,
  4954. offset - key.offset + btrfs_file_extent_offset(leaf, ei));
  4955. ret = btrfs_encoded_io_compression_from_extent(fs_info,
  4956. btrfs_file_extent_compression(leaf, ei));
  4957. if (ret < 0)
  4958. goto out;
  4959. TLV_PUT_U32(sctx, BTRFS_SEND_A_COMPRESSION, ret);
  4960. TLV_PUT_U32(sctx, BTRFS_SEND_A_ENCRYPTION, 0);
  4961. ret = put_data_header(sctx, disk_num_bytes);
  4962. if (ret < 0)
  4963. goto out;
  4964. /*
  4965. * We want to do I/O directly into the send buffer, so get the next page
  4966. * boundary in the send buffer. This means that there may be a gap
  4967. * between the beginning of the command and the file data.
  4968. */
  4969. data_offset = PAGE_ALIGN(sctx->send_size);
  4970. if (data_offset > sctx->send_max_size ||
  4971. sctx->send_max_size - data_offset < disk_num_bytes) {
  4972. ret = -EOVERFLOW;
  4973. goto out;
  4974. }
  4975. /*
  4976. * Note that send_buf is a mapping of send_buf_pages, so this is really
  4977. * reading into send_buf.
  4978. */
  4979. ret = btrfs_encoded_read_regular_fill_pages(BTRFS_I(inode),
  4980. disk_bytenr, disk_num_bytes,
  4981. sctx->send_buf_pages +
  4982. (data_offset >> PAGE_SHIFT));
  4983. if (ret)
  4984. goto out;
  4985. hdr = (struct btrfs_cmd_header *)sctx->send_buf;
  4986. hdr->len = cpu_to_le32(sctx->send_size + disk_num_bytes - sizeof(*hdr));
  4987. hdr->crc = 0;
  4988. crc = crc32c(0, sctx->send_buf, sctx->send_size);
  4989. crc = crc32c(crc, sctx->send_buf + data_offset, disk_num_bytes);
  4990. hdr->crc = cpu_to_le32(crc);
  4991. ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
  4992. &sctx->send_off);
  4993. if (!ret) {
  4994. ret = write_buf(sctx->send_filp, sctx->send_buf + data_offset,
  4995. disk_num_bytes, &sctx->send_off);
  4996. }
  4997. sctx->send_size = 0;
  4998. sctx->put_data = false;
  4999. tlv_put_failure:
  5000. out:
  5001. iput(inode);
  5002. return ret;
  5003. }
  5004. static int send_extent_data(struct send_ctx *sctx, struct btrfs_path *path,
  5005. const u64 offset, const u64 len)
  5006. {
  5007. const u64 end = offset + len;
  5008. struct extent_buffer *leaf = path->nodes[0];
  5009. struct btrfs_file_extent_item *ei;
  5010. u64 read_size = max_send_read_size(sctx);
  5011. u64 sent = 0;
  5012. if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
  5013. return send_update_extent(sctx, offset, len);
  5014. ei = btrfs_item_ptr(leaf, path->slots[0],
  5015. struct btrfs_file_extent_item);
  5016. if ((sctx->flags & BTRFS_SEND_FLAG_COMPRESSED) &&
  5017. btrfs_file_extent_compression(leaf, ei) != BTRFS_COMPRESS_NONE) {
  5018. bool is_inline = (btrfs_file_extent_type(leaf, ei) ==
  5019. BTRFS_FILE_EXTENT_INLINE);
  5020. /*
  5021. * Send the compressed extent unless the compressed data is
  5022. * larger than the decompressed data. This can happen if we're
  5023. * not sending the entire extent, either because it has been
  5024. * partially overwritten/truncated or because this is a part of
  5025. * the extent that we couldn't clone in clone_range().
  5026. */
  5027. if (is_inline &&
  5028. btrfs_file_extent_inline_item_len(leaf,
  5029. path->slots[0]) <= len) {
  5030. return send_encoded_inline_extent(sctx, path, offset,
  5031. len);
  5032. } else if (!is_inline &&
  5033. btrfs_file_extent_disk_num_bytes(leaf, ei) <= len) {
  5034. return send_encoded_extent(sctx, path, offset, len);
  5035. }
  5036. }
  5037. if (sctx->cur_inode == NULL) {
  5038. struct btrfs_root *root = sctx->send_root;
  5039. sctx->cur_inode = btrfs_iget(sctx->cur_ino, root);
  5040. if (IS_ERR(sctx->cur_inode)) {
  5041. int err = PTR_ERR(sctx->cur_inode);
  5042. sctx->cur_inode = NULL;
  5043. return err;
  5044. }
  5045. memset(&sctx->ra, 0, sizeof(struct file_ra_state));
  5046. file_ra_state_init(&sctx->ra, sctx->cur_inode->i_mapping);
  5047. /*
  5048. * It's very likely there are no pages from this inode in the page
  5049. * cache, so after reading extents and sending their data, we clean
  5050. * the page cache to avoid trashing the page cache (adding pressure
  5051. * to the page cache and forcing eviction of other data more useful
  5052. * for applications).
  5053. *
  5054. * We decide if we should clean the page cache simply by checking
  5055. * if the inode's mapping nrpages is 0 when we first open it, and
  5056. * not by using something like filemap_range_has_page() before
  5057. * reading an extent because when we ask the readahead code to
  5058. * read a given file range, it may (and almost always does) read
  5059. * pages from beyond that range (see the documentation for
  5060. * page_cache_sync_readahead()), so it would not be reliable,
  5061. * because after reading the first extent future calls to
  5062. * filemap_range_has_page() would return true because the readahead
  5063. * on the previous extent resulted in reading pages of the current
  5064. * extent as well.
  5065. */
  5066. sctx->clean_page_cache = (sctx->cur_inode->i_mapping->nrpages == 0);
  5067. sctx->page_cache_clear_start = round_down(offset, PAGE_SIZE);
  5068. }
  5069. while (sent < len) {
  5070. u64 size = min(len - sent, read_size);
  5071. int ret;
  5072. ret = send_write(sctx, offset + sent, size);
  5073. if (ret < 0)
  5074. return ret;
  5075. sent += size;
  5076. }
  5077. if (sctx->clean_page_cache && PAGE_ALIGNED(end)) {
  5078. /*
  5079. * Always operate only on ranges that are a multiple of the page
  5080. * size. This is not only to prevent zeroing parts of a page in
  5081. * the case of subpage sector size, but also to guarantee we evict
  5082. * pages, as passing a range that is smaller than page size does
  5083. * not evict the respective page (only zeroes part of its content).
  5084. *
  5085. * Always start from the end offset of the last range cleared.
  5086. * This is because the readahead code may (and very often does)
  5087. * reads pages beyond the range we request for readahead. So if
  5088. * we have an extent layout like this:
  5089. *
  5090. * [ extent A ] [ extent B ] [ extent C ]
  5091. *
  5092. * When we ask page_cache_sync_readahead() to read extent A, it
  5093. * may also trigger reads for pages of extent B. If we are doing
  5094. * an incremental send and extent B has not changed between the
  5095. * parent and send snapshots, some or all of its pages may end
  5096. * up being read and placed in the page cache. So when truncating
  5097. * the page cache we always start from the end offset of the
  5098. * previously processed extent up to the end of the current
  5099. * extent.
  5100. */
  5101. truncate_inode_pages_range(&sctx->cur_inode->i_data,
  5102. sctx->page_cache_clear_start,
  5103. end - 1);
  5104. sctx->page_cache_clear_start = end;
  5105. }
  5106. return 0;
  5107. }
  5108. /*
  5109. * Search for a capability xattr related to sctx->cur_ino. If the capability is
  5110. * found, call send_set_xattr function to emit it.
  5111. *
  5112. * Return 0 if there isn't a capability, or when the capability was emitted
  5113. * successfully, or < 0 if an error occurred.
  5114. */
  5115. static int send_capabilities(struct send_ctx *sctx)
  5116. {
  5117. struct btrfs_path *path;
  5118. struct btrfs_dir_item *di;
  5119. struct extent_buffer *leaf;
  5120. unsigned long data_ptr;
  5121. char *buf = NULL;
  5122. int buf_len;
  5123. int ret = 0;
  5124. path = alloc_path_for_send();
  5125. if (!path)
  5126. return -ENOMEM;
  5127. di = btrfs_lookup_xattr(NULL, sctx->send_root, path, sctx->cur_ino,
  5128. XATTR_NAME_CAPS, strlen(XATTR_NAME_CAPS), 0);
  5129. if (!di) {
  5130. /* There is no xattr for this inode */
  5131. goto out;
  5132. } else if (IS_ERR(di)) {
  5133. ret = PTR_ERR(di);
  5134. goto out;
  5135. }
  5136. leaf = path->nodes[0];
  5137. buf_len = btrfs_dir_data_len(leaf, di);
  5138. buf = kmalloc(buf_len, GFP_KERNEL);
  5139. if (!buf) {
  5140. ret = -ENOMEM;
  5141. goto out;
  5142. }
  5143. data_ptr = (unsigned long)(di + 1) + btrfs_dir_name_len(leaf, di);
  5144. read_extent_buffer(leaf, buf, data_ptr, buf_len);
  5145. ret = send_set_xattr(sctx, XATTR_NAME_CAPS,
  5146. strlen(XATTR_NAME_CAPS), buf, buf_len);
  5147. out:
  5148. kfree(buf);
  5149. btrfs_free_path(path);
  5150. return ret;
  5151. }
  5152. static int clone_range(struct send_ctx *sctx, struct btrfs_path *dst_path,
  5153. struct clone_root *clone_root, const u64 disk_byte,
  5154. u64 data_offset, u64 offset, u64 len)
  5155. {
  5156. struct btrfs_path *path;
  5157. struct btrfs_key key;
  5158. int ret;
  5159. struct btrfs_inode_info info;
  5160. u64 clone_src_i_size = 0;
  5161. /*
  5162. * Prevent cloning from a zero offset with a length matching the sector
  5163. * size because in some scenarios this will make the receiver fail.
  5164. *
  5165. * For example, if in the source filesystem the extent at offset 0
  5166. * has a length of sectorsize and it was written using direct IO, then
  5167. * it can never be an inline extent (even if compression is enabled).
  5168. * Then this extent can be cloned in the original filesystem to a non
  5169. * zero file offset, but it may not be possible to clone in the
  5170. * destination filesystem because it can be inlined due to compression
  5171. * on the destination filesystem (as the receiver's write operations are
  5172. * always done using buffered IO). The same happens when the original
  5173. * filesystem does not have compression enabled but the destination
  5174. * filesystem has.
  5175. */
  5176. if (clone_root->offset == 0 &&
  5177. len == sctx->send_root->fs_info->sectorsize)
  5178. return send_extent_data(sctx, dst_path, offset, len);
  5179. path = alloc_path_for_send();
  5180. if (!path)
  5181. return -ENOMEM;
  5182. /*
  5183. * There are inodes that have extents that lie behind its i_size. Don't
  5184. * accept clones from these extents.
  5185. */
  5186. ret = get_inode_info(clone_root->root, clone_root->ino, &info);
  5187. btrfs_release_path(path);
  5188. if (ret < 0)
  5189. goto out;
  5190. clone_src_i_size = info.size;
  5191. /*
  5192. * We can't send a clone operation for the entire range if we find
  5193. * extent items in the respective range in the source file that
  5194. * refer to different extents or if we find holes.
  5195. * So check for that and do a mix of clone and regular write/copy
  5196. * operations if needed.
  5197. *
  5198. * Example:
  5199. *
  5200. * mkfs.btrfs -f /dev/sda
  5201. * mount /dev/sda /mnt
  5202. * xfs_io -f -c "pwrite -S 0xaa 0K 100K" /mnt/foo
  5203. * cp --reflink=always /mnt/foo /mnt/bar
  5204. * xfs_io -c "pwrite -S 0xbb 50K 50K" /mnt/foo
  5205. * btrfs subvolume snapshot -r /mnt /mnt/snap
  5206. *
  5207. * If when we send the snapshot and we are processing file bar (which
  5208. * has a higher inode number than foo) we blindly send a clone operation
  5209. * for the [0, 100K[ range from foo to bar, the receiver ends up getting
  5210. * a file bar that matches the content of file foo - iow, doesn't match
  5211. * the content from bar in the original filesystem.
  5212. */
  5213. key.objectid = clone_root->ino;
  5214. key.type = BTRFS_EXTENT_DATA_KEY;
  5215. key.offset = clone_root->offset;
  5216. ret = btrfs_search_slot(NULL, clone_root->root, &key, path, 0, 0);
  5217. if (ret < 0)
  5218. goto out;
  5219. if (ret > 0 && path->slots[0] > 0) {
  5220. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1);
  5221. if (key.objectid == clone_root->ino &&
  5222. key.type == BTRFS_EXTENT_DATA_KEY)
  5223. path->slots[0]--;
  5224. }
  5225. while (true) {
  5226. struct extent_buffer *leaf = path->nodes[0];
  5227. int slot = path->slots[0];
  5228. struct btrfs_file_extent_item *ei;
  5229. u8 type;
  5230. u64 ext_len;
  5231. u64 clone_len;
  5232. u64 clone_data_offset;
  5233. bool crossed_src_i_size = false;
  5234. if (slot >= btrfs_header_nritems(leaf)) {
  5235. ret = btrfs_next_leaf(clone_root->root, path);
  5236. if (ret < 0)
  5237. goto out;
  5238. else if (ret > 0)
  5239. break;
  5240. continue;
  5241. }
  5242. btrfs_item_key_to_cpu(leaf, &key, slot);
  5243. /*
  5244. * We might have an implicit trailing hole (NO_HOLES feature
  5245. * enabled). We deal with it after leaving this loop.
  5246. */
  5247. if (key.objectid != clone_root->ino ||
  5248. key.type != BTRFS_EXTENT_DATA_KEY)
  5249. break;
  5250. ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  5251. type = btrfs_file_extent_type(leaf, ei);
  5252. if (type == BTRFS_FILE_EXTENT_INLINE) {
  5253. ext_len = btrfs_file_extent_ram_bytes(leaf, ei);
  5254. ext_len = PAGE_ALIGN(ext_len);
  5255. } else {
  5256. ext_len = btrfs_file_extent_num_bytes(leaf, ei);
  5257. }
  5258. if (key.offset + ext_len <= clone_root->offset)
  5259. goto next;
  5260. if (key.offset > clone_root->offset) {
  5261. /* Implicit hole, NO_HOLES feature enabled. */
  5262. u64 hole_len = key.offset - clone_root->offset;
  5263. if (hole_len > len)
  5264. hole_len = len;
  5265. ret = send_extent_data(sctx, dst_path, offset,
  5266. hole_len);
  5267. if (ret < 0)
  5268. goto out;
  5269. len -= hole_len;
  5270. if (len == 0)
  5271. break;
  5272. offset += hole_len;
  5273. clone_root->offset += hole_len;
  5274. data_offset += hole_len;
  5275. }
  5276. if (key.offset >= clone_root->offset + len)
  5277. break;
  5278. if (key.offset >= clone_src_i_size)
  5279. break;
  5280. if (key.offset + ext_len > clone_src_i_size) {
  5281. ext_len = clone_src_i_size - key.offset;
  5282. crossed_src_i_size = true;
  5283. }
  5284. clone_data_offset = btrfs_file_extent_offset(leaf, ei);
  5285. if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte) {
  5286. clone_root->offset = key.offset;
  5287. if (clone_data_offset < data_offset &&
  5288. clone_data_offset + ext_len > data_offset) {
  5289. u64 extent_offset;
  5290. extent_offset = data_offset - clone_data_offset;
  5291. ext_len -= extent_offset;
  5292. clone_data_offset += extent_offset;
  5293. clone_root->offset += extent_offset;
  5294. }
  5295. }
  5296. clone_len = min_t(u64, ext_len, len);
  5297. if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte &&
  5298. clone_data_offset == data_offset) {
  5299. const u64 src_end = clone_root->offset + clone_len;
  5300. const u64 sectorsize = SZ_64K;
  5301. /*
  5302. * We can't clone the last block, when its size is not
  5303. * sector size aligned, into the middle of a file. If we
  5304. * do so, the receiver will get a failure (-EINVAL) when
  5305. * trying to clone or will silently corrupt the data in
  5306. * the destination file if it's on a kernel without the
  5307. * fix introduced by commit ac765f83f1397646
  5308. * ("Btrfs: fix data corruption due to cloning of eof
  5309. * block).
  5310. *
  5311. * So issue a clone of the aligned down range plus a
  5312. * regular write for the eof block, if we hit that case.
  5313. *
  5314. * Also, we use the maximum possible sector size, 64K,
  5315. * because we don't know what's the sector size of the
  5316. * filesystem that receives the stream, so we have to
  5317. * assume the largest possible sector size.
  5318. */
  5319. if (src_end == clone_src_i_size &&
  5320. !IS_ALIGNED(src_end, sectorsize) &&
  5321. offset + clone_len < sctx->cur_inode_size) {
  5322. u64 slen;
  5323. slen = ALIGN_DOWN(src_end - clone_root->offset,
  5324. sectorsize);
  5325. if (slen > 0) {
  5326. ret = send_clone(sctx, offset, slen,
  5327. clone_root);
  5328. if (ret < 0)
  5329. goto out;
  5330. }
  5331. ret = send_extent_data(sctx, dst_path,
  5332. offset + slen,
  5333. clone_len - slen);
  5334. } else {
  5335. ret = send_clone(sctx, offset, clone_len,
  5336. clone_root);
  5337. }
  5338. } else if (crossed_src_i_size && clone_len < len) {
  5339. /*
  5340. * If we are at i_size of the clone source inode and we
  5341. * can not clone from it, terminate the loop. This is
  5342. * to avoid sending two write operations, one with a
  5343. * length matching clone_len and the final one after
  5344. * this loop with a length of len - clone_len.
  5345. *
  5346. * When using encoded writes (BTRFS_SEND_FLAG_COMPRESSED
  5347. * was passed to the send ioctl), this helps avoid
  5348. * sending an encoded write for an offset that is not
  5349. * sector size aligned, in case the i_size of the source
  5350. * inode is not sector size aligned. That will make the
  5351. * receiver fallback to decompression of the data and
  5352. * writing it using regular buffered IO, therefore while
  5353. * not incorrect, it's not optimal due decompression and
  5354. * possible re-compression at the receiver.
  5355. */
  5356. break;
  5357. } else {
  5358. ret = send_extent_data(sctx, dst_path, offset,
  5359. clone_len);
  5360. }
  5361. if (ret < 0)
  5362. goto out;
  5363. len -= clone_len;
  5364. if (len == 0)
  5365. break;
  5366. offset += clone_len;
  5367. clone_root->offset += clone_len;
  5368. /*
  5369. * If we are cloning from the file we are currently processing,
  5370. * and using the send root as the clone root, we must stop once
  5371. * the current clone offset reaches the current eof of the file
  5372. * at the receiver, otherwise we would issue an invalid clone
  5373. * operation (source range going beyond eof) and cause the
  5374. * receiver to fail. So if we reach the current eof, bail out
  5375. * and fallback to a regular write.
  5376. */
  5377. if (clone_root->root == sctx->send_root &&
  5378. clone_root->ino == sctx->cur_ino &&
  5379. clone_root->offset >= sctx->cur_inode_next_write_offset)
  5380. break;
  5381. data_offset += clone_len;
  5382. next:
  5383. path->slots[0]++;
  5384. }
  5385. if (len > 0)
  5386. ret = send_extent_data(sctx, dst_path, offset, len);
  5387. else
  5388. ret = 0;
  5389. out:
  5390. btrfs_free_path(path);
  5391. return ret;
  5392. }
  5393. static int send_write_or_clone(struct send_ctx *sctx,
  5394. struct btrfs_path *path,
  5395. struct btrfs_key *key,
  5396. struct clone_root *clone_root)
  5397. {
  5398. int ret = 0;
  5399. u64 offset = key->offset;
  5400. u64 end;
  5401. u64 bs = sctx->send_root->fs_info->sectorsize;
  5402. struct btrfs_file_extent_item *ei;
  5403. u64 disk_byte;
  5404. u64 data_offset;
  5405. u64 num_bytes;
  5406. struct btrfs_inode_info info = { 0 };
  5407. end = min_t(u64, btrfs_file_extent_end(path), sctx->cur_inode_size);
  5408. if (offset >= end)
  5409. return 0;
  5410. num_bytes = end - offset;
  5411. if (!clone_root)
  5412. goto write_data;
  5413. if (IS_ALIGNED(end, bs))
  5414. goto clone_data;
  5415. /*
  5416. * If the extent end is not aligned, we can clone if the extent ends at
  5417. * the i_size of the inode and the clone range ends at the i_size of the
  5418. * source inode, otherwise the clone operation fails with -EINVAL.
  5419. */
  5420. if (end != sctx->cur_inode_size)
  5421. goto write_data;
  5422. ret = get_inode_info(clone_root->root, clone_root->ino, &info);
  5423. if (ret < 0)
  5424. return ret;
  5425. if (clone_root->offset + num_bytes == info.size) {
  5426. /*
  5427. * The final size of our file matches the end offset, but it may
  5428. * be that its current size is larger, so we have to truncate it
  5429. * to any value between the start offset of the range and the
  5430. * final i_size, otherwise the clone operation is invalid
  5431. * because it's unaligned and it ends before the current EOF.
  5432. * We do this truncate to the final i_size when we finish
  5433. * processing the inode, but it's too late by then. And here we
  5434. * truncate to the start offset of the range because it's always
  5435. * sector size aligned while if it were the final i_size it
  5436. * would result in dirtying part of a page, filling part of a
  5437. * page with zeroes and then having the clone operation at the
  5438. * receiver trigger IO and wait for it due to the dirty page.
  5439. */
  5440. if (sctx->parent_root != NULL) {
  5441. ret = send_truncate(sctx, sctx->cur_ino,
  5442. sctx->cur_inode_gen, offset);
  5443. if (ret < 0)
  5444. return ret;
  5445. }
  5446. goto clone_data;
  5447. }
  5448. write_data:
  5449. ret = send_extent_data(sctx, path, offset, num_bytes);
  5450. sctx->cur_inode_next_write_offset = end;
  5451. return ret;
  5452. clone_data:
  5453. ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
  5454. struct btrfs_file_extent_item);
  5455. disk_byte = btrfs_file_extent_disk_bytenr(path->nodes[0], ei);
  5456. data_offset = btrfs_file_extent_offset(path->nodes[0], ei);
  5457. ret = clone_range(sctx, path, clone_root, disk_byte, data_offset, offset,
  5458. num_bytes);
  5459. sctx->cur_inode_next_write_offset = end;
  5460. return ret;
  5461. }
  5462. static int is_extent_unchanged(struct send_ctx *sctx,
  5463. struct btrfs_path *left_path,
  5464. struct btrfs_key *ekey)
  5465. {
  5466. int ret = 0;
  5467. struct btrfs_key key;
  5468. struct btrfs_path *path = NULL;
  5469. struct extent_buffer *eb;
  5470. int slot;
  5471. struct btrfs_key found_key;
  5472. struct btrfs_file_extent_item *ei;
  5473. u64 left_disknr;
  5474. u64 right_disknr;
  5475. u64 left_offset;
  5476. u64 right_offset;
  5477. u64 left_offset_fixed;
  5478. u64 left_len;
  5479. u64 right_len;
  5480. u64 left_gen;
  5481. u64 right_gen;
  5482. u8 left_type;
  5483. u8 right_type;
  5484. path = alloc_path_for_send();
  5485. if (!path)
  5486. return -ENOMEM;
  5487. eb = left_path->nodes[0];
  5488. slot = left_path->slots[0];
  5489. ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  5490. left_type = btrfs_file_extent_type(eb, ei);
  5491. if (left_type != BTRFS_FILE_EXTENT_REG) {
  5492. ret = 0;
  5493. goto out;
  5494. }
  5495. left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
  5496. left_len = btrfs_file_extent_num_bytes(eb, ei);
  5497. left_offset = btrfs_file_extent_offset(eb, ei);
  5498. left_gen = btrfs_file_extent_generation(eb, ei);
  5499. /*
  5500. * Following comments will refer to these graphics. L is the left
  5501. * extents which we are checking at the moment. 1-8 are the right
  5502. * extents that we iterate.
  5503. *
  5504. * |-----L-----|
  5505. * |-1-|-2a-|-3-|-4-|-5-|-6-|
  5506. *
  5507. * |-----L-----|
  5508. * |--1--|-2b-|...(same as above)
  5509. *
  5510. * Alternative situation. Happens on files where extents got split.
  5511. * |-----L-----|
  5512. * |-----------7-----------|-6-|
  5513. *
  5514. * Alternative situation. Happens on files which got larger.
  5515. * |-----L-----|
  5516. * |-8-|
  5517. * Nothing follows after 8.
  5518. */
  5519. key.objectid = ekey->objectid;
  5520. key.type = BTRFS_EXTENT_DATA_KEY;
  5521. key.offset = ekey->offset;
  5522. ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
  5523. if (ret < 0)
  5524. goto out;
  5525. if (ret) {
  5526. ret = 0;
  5527. goto out;
  5528. }
  5529. /*
  5530. * Handle special case where the right side has no extents at all.
  5531. */
  5532. eb = path->nodes[0];
  5533. slot = path->slots[0];
  5534. btrfs_item_key_to_cpu(eb, &found_key, slot);
  5535. if (found_key.objectid != key.objectid ||
  5536. found_key.type != key.type) {
  5537. /* If we're a hole then just pretend nothing changed */
  5538. ret = (left_disknr) ? 0 : 1;
  5539. goto out;
  5540. }
  5541. /*
  5542. * We're now on 2a, 2b or 7.
  5543. */
  5544. key = found_key;
  5545. while (key.offset < ekey->offset + left_len) {
  5546. ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  5547. right_type = btrfs_file_extent_type(eb, ei);
  5548. if (right_type != BTRFS_FILE_EXTENT_REG &&
  5549. right_type != BTRFS_FILE_EXTENT_INLINE) {
  5550. ret = 0;
  5551. goto out;
  5552. }
  5553. if (right_type == BTRFS_FILE_EXTENT_INLINE) {
  5554. right_len = btrfs_file_extent_ram_bytes(eb, ei);
  5555. right_len = PAGE_ALIGN(right_len);
  5556. } else {
  5557. right_len = btrfs_file_extent_num_bytes(eb, ei);
  5558. }
  5559. /*
  5560. * Are we at extent 8? If yes, we know the extent is changed.
  5561. * This may only happen on the first iteration.
  5562. */
  5563. if (found_key.offset + right_len <= ekey->offset) {
  5564. /* If we're a hole just pretend nothing changed */
  5565. ret = (left_disknr) ? 0 : 1;
  5566. goto out;
  5567. }
  5568. /*
  5569. * We just wanted to see if when we have an inline extent, what
  5570. * follows it is a regular extent (wanted to check the above
  5571. * condition for inline extents too). This should normally not
  5572. * happen but it's possible for example when we have an inline
  5573. * compressed extent representing data with a size matching
  5574. * the page size (currently the same as sector size).
  5575. */
  5576. if (right_type == BTRFS_FILE_EXTENT_INLINE) {
  5577. ret = 0;
  5578. goto out;
  5579. }
  5580. right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
  5581. right_offset = btrfs_file_extent_offset(eb, ei);
  5582. right_gen = btrfs_file_extent_generation(eb, ei);
  5583. left_offset_fixed = left_offset;
  5584. if (key.offset < ekey->offset) {
  5585. /* Fix the right offset for 2a and 7. */
  5586. right_offset += ekey->offset - key.offset;
  5587. } else {
  5588. /* Fix the left offset for all behind 2a and 2b */
  5589. left_offset_fixed += key.offset - ekey->offset;
  5590. }
  5591. /*
  5592. * Check if we have the same extent.
  5593. */
  5594. if (left_disknr != right_disknr ||
  5595. left_offset_fixed != right_offset ||
  5596. left_gen != right_gen) {
  5597. ret = 0;
  5598. goto out;
  5599. }
  5600. /*
  5601. * Go to the next extent.
  5602. */
  5603. ret = btrfs_next_item(sctx->parent_root, path);
  5604. if (ret < 0)
  5605. goto out;
  5606. if (!ret) {
  5607. eb = path->nodes[0];
  5608. slot = path->slots[0];
  5609. btrfs_item_key_to_cpu(eb, &found_key, slot);
  5610. }
  5611. if (ret || found_key.objectid != key.objectid ||
  5612. found_key.type != key.type) {
  5613. key.offset += right_len;
  5614. break;
  5615. }
  5616. if (found_key.offset != key.offset + right_len) {
  5617. ret = 0;
  5618. goto out;
  5619. }
  5620. key = found_key;
  5621. }
  5622. /*
  5623. * We're now behind the left extent (treat as unchanged) or at the end
  5624. * of the right side (treat as changed).
  5625. */
  5626. if (key.offset >= ekey->offset + left_len)
  5627. ret = 1;
  5628. else
  5629. ret = 0;
  5630. out:
  5631. btrfs_free_path(path);
  5632. return ret;
  5633. }
  5634. static int get_last_extent(struct send_ctx *sctx, u64 offset)
  5635. {
  5636. struct btrfs_path *path;
  5637. struct btrfs_root *root = sctx->send_root;
  5638. struct btrfs_key key;
  5639. int ret;
  5640. path = alloc_path_for_send();
  5641. if (!path)
  5642. return -ENOMEM;
  5643. sctx->cur_inode_last_extent = 0;
  5644. key.objectid = sctx->cur_ino;
  5645. key.type = BTRFS_EXTENT_DATA_KEY;
  5646. key.offset = offset;
  5647. ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
  5648. if (ret < 0)
  5649. goto out;
  5650. ret = 0;
  5651. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  5652. if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
  5653. goto out;
  5654. sctx->cur_inode_last_extent = btrfs_file_extent_end(path);
  5655. out:
  5656. btrfs_free_path(path);
  5657. return ret;
  5658. }
  5659. static int range_is_hole_in_parent(struct send_ctx *sctx,
  5660. const u64 start,
  5661. const u64 end)
  5662. {
  5663. struct btrfs_path *path;
  5664. struct btrfs_key key;
  5665. struct btrfs_root *root = sctx->parent_root;
  5666. u64 search_start = start;
  5667. int ret;
  5668. path = alloc_path_for_send();
  5669. if (!path)
  5670. return -ENOMEM;
  5671. key.objectid = sctx->cur_ino;
  5672. key.type = BTRFS_EXTENT_DATA_KEY;
  5673. key.offset = search_start;
  5674. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  5675. if (ret < 0)
  5676. goto out;
  5677. if (ret > 0 && path->slots[0] > 0)
  5678. path->slots[0]--;
  5679. while (search_start < end) {
  5680. struct extent_buffer *leaf = path->nodes[0];
  5681. int slot = path->slots[0];
  5682. struct btrfs_file_extent_item *fi;
  5683. u64 extent_end;
  5684. if (slot >= btrfs_header_nritems(leaf)) {
  5685. ret = btrfs_next_leaf(root, path);
  5686. if (ret < 0)
  5687. goto out;
  5688. else if (ret > 0)
  5689. break;
  5690. continue;
  5691. }
  5692. btrfs_item_key_to_cpu(leaf, &key, slot);
  5693. if (key.objectid < sctx->cur_ino ||
  5694. key.type < BTRFS_EXTENT_DATA_KEY)
  5695. goto next;
  5696. if (key.objectid > sctx->cur_ino ||
  5697. key.type > BTRFS_EXTENT_DATA_KEY ||
  5698. key.offset >= end)
  5699. break;
  5700. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  5701. extent_end = btrfs_file_extent_end(path);
  5702. if (extent_end <= start)
  5703. goto next;
  5704. if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) {
  5705. search_start = extent_end;
  5706. goto next;
  5707. }
  5708. ret = 0;
  5709. goto out;
  5710. next:
  5711. path->slots[0]++;
  5712. }
  5713. ret = 1;
  5714. out:
  5715. btrfs_free_path(path);
  5716. return ret;
  5717. }
  5718. static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
  5719. struct btrfs_key *key)
  5720. {
  5721. int ret = 0;
  5722. if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
  5723. return 0;
  5724. /*
  5725. * Get last extent's end offset (exclusive) if we haven't determined it
  5726. * yet (we're processing the first file extent item that is new), or if
  5727. * we're at the first slot of a leaf and the last extent's end is less
  5728. * than the current extent's offset, because we might have skipped
  5729. * entire leaves that contained only file extent items for our current
  5730. * inode. These leaves have a generation number smaller (older) than the
  5731. * one in the current leaf and the leaf our last extent came from, and
  5732. * are located between these 2 leaves.
  5733. */
  5734. if ((sctx->cur_inode_last_extent == (u64)-1) ||
  5735. (path->slots[0] == 0 && sctx->cur_inode_last_extent < key->offset)) {
  5736. ret = get_last_extent(sctx, key->offset - 1);
  5737. if (ret)
  5738. return ret;
  5739. }
  5740. if (sctx->cur_inode_last_extent < key->offset) {
  5741. ret = range_is_hole_in_parent(sctx,
  5742. sctx->cur_inode_last_extent,
  5743. key->offset);
  5744. if (ret < 0)
  5745. return ret;
  5746. else if (ret == 0)
  5747. ret = send_hole(sctx, key->offset);
  5748. else
  5749. ret = 0;
  5750. }
  5751. sctx->cur_inode_last_extent = btrfs_file_extent_end(path);
  5752. return ret;
  5753. }
  5754. static int process_extent(struct send_ctx *sctx,
  5755. struct btrfs_path *path,
  5756. struct btrfs_key *key)
  5757. {
  5758. struct clone_root *found_clone = NULL;
  5759. int ret = 0;
  5760. if (S_ISLNK(sctx->cur_inode_mode))
  5761. return 0;
  5762. if (sctx->parent_root && !sctx->cur_inode_new) {
  5763. ret = is_extent_unchanged(sctx, path, key);
  5764. if (ret < 0)
  5765. goto out;
  5766. if (ret) {
  5767. ret = 0;
  5768. goto out_hole;
  5769. }
  5770. } else {
  5771. struct btrfs_file_extent_item *ei;
  5772. u8 type;
  5773. ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
  5774. struct btrfs_file_extent_item);
  5775. type = btrfs_file_extent_type(path->nodes[0], ei);
  5776. if (type == BTRFS_FILE_EXTENT_PREALLOC ||
  5777. type == BTRFS_FILE_EXTENT_REG) {
  5778. /*
  5779. * The send spec does not have a prealloc command yet,
  5780. * so just leave a hole for prealloc'ed extents until
  5781. * we have enough commands queued up to justify rev'ing
  5782. * the send spec.
  5783. */
  5784. if (type == BTRFS_FILE_EXTENT_PREALLOC) {
  5785. ret = 0;
  5786. goto out;
  5787. }
  5788. /* Have a hole, just skip it. */
  5789. if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
  5790. ret = 0;
  5791. goto out;
  5792. }
  5793. }
  5794. }
  5795. ret = find_extent_clone(sctx, path, key->objectid, key->offset,
  5796. sctx->cur_inode_size, &found_clone);
  5797. if (ret != -ENOENT && ret < 0)
  5798. goto out;
  5799. ret = send_write_or_clone(sctx, path, key, found_clone);
  5800. if (ret)
  5801. goto out;
  5802. out_hole:
  5803. ret = maybe_send_hole(sctx, path, key);
  5804. out:
  5805. return ret;
  5806. }
  5807. static int process_all_extents(struct send_ctx *sctx)
  5808. {
  5809. int ret = 0;
  5810. int iter_ret = 0;
  5811. struct btrfs_root *root;
  5812. struct btrfs_path *path;
  5813. struct btrfs_key key;
  5814. struct btrfs_key found_key;
  5815. root = sctx->send_root;
  5816. path = alloc_path_for_send();
  5817. if (!path)
  5818. return -ENOMEM;
  5819. key.objectid = sctx->cmp_key->objectid;
  5820. key.type = BTRFS_EXTENT_DATA_KEY;
  5821. key.offset = 0;
  5822. btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) {
  5823. if (found_key.objectid != key.objectid ||
  5824. found_key.type != key.type) {
  5825. ret = 0;
  5826. break;
  5827. }
  5828. ret = process_extent(sctx, path, &found_key);
  5829. if (ret < 0)
  5830. break;
  5831. }
  5832. /* Catch error found during iteration */
  5833. if (iter_ret < 0)
  5834. ret = iter_ret;
  5835. btrfs_free_path(path);
  5836. return ret;
  5837. }
  5838. static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
  5839. int *pending_move,
  5840. int *refs_processed)
  5841. {
  5842. int ret = 0;
  5843. if (sctx->cur_ino == 0)
  5844. goto out;
  5845. if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
  5846. sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
  5847. goto out;
  5848. if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
  5849. goto out;
  5850. ret = process_recorded_refs(sctx, pending_move);
  5851. if (ret < 0)
  5852. goto out;
  5853. *refs_processed = 1;
  5854. out:
  5855. return ret;
  5856. }
  5857. static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
  5858. {
  5859. int ret = 0;
  5860. struct btrfs_inode_info info;
  5861. u64 left_mode;
  5862. u64 left_uid;
  5863. u64 left_gid;
  5864. u64 left_fileattr;
  5865. u64 right_mode;
  5866. u64 right_uid;
  5867. u64 right_gid;
  5868. u64 right_fileattr;
  5869. int need_chmod = 0;
  5870. int need_chown = 0;
  5871. bool need_fileattr = false;
  5872. int need_truncate = 1;
  5873. int pending_move = 0;
  5874. int refs_processed = 0;
  5875. if (sctx->ignore_cur_inode)
  5876. return 0;
  5877. ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
  5878. &refs_processed);
  5879. if (ret < 0)
  5880. goto out;
  5881. /*
  5882. * We have processed the refs and thus need to advance send_progress.
  5883. * Now, calls to get_cur_xxx will take the updated refs of the current
  5884. * inode into account.
  5885. *
  5886. * On the other hand, if our current inode is a directory and couldn't
  5887. * be moved/renamed because its parent was renamed/moved too and it has
  5888. * a higher inode number, we can only move/rename our current inode
  5889. * after we moved/renamed its parent. Therefore in this case operate on
  5890. * the old path (pre move/rename) of our current inode, and the
  5891. * move/rename will be performed later.
  5892. */
  5893. if (refs_processed && !pending_move)
  5894. sctx->send_progress = sctx->cur_ino + 1;
  5895. if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
  5896. goto out;
  5897. if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
  5898. goto out;
  5899. ret = get_inode_info(sctx->send_root, sctx->cur_ino, &info);
  5900. if (ret < 0)
  5901. goto out;
  5902. left_mode = info.mode;
  5903. left_uid = info.uid;
  5904. left_gid = info.gid;
  5905. left_fileattr = info.fileattr;
  5906. if (!sctx->parent_root || sctx->cur_inode_new) {
  5907. need_chown = 1;
  5908. if (!S_ISLNK(sctx->cur_inode_mode))
  5909. need_chmod = 1;
  5910. if (sctx->cur_inode_next_write_offset == sctx->cur_inode_size)
  5911. need_truncate = 0;
  5912. } else {
  5913. u64 old_size;
  5914. ret = get_inode_info(sctx->parent_root, sctx->cur_ino, &info);
  5915. if (ret < 0)
  5916. goto out;
  5917. old_size = info.size;
  5918. right_mode = info.mode;
  5919. right_uid = info.uid;
  5920. right_gid = info.gid;
  5921. right_fileattr = info.fileattr;
  5922. if (left_uid != right_uid || left_gid != right_gid)
  5923. need_chown = 1;
  5924. if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
  5925. need_chmod = 1;
  5926. if (!S_ISLNK(sctx->cur_inode_mode) && left_fileattr != right_fileattr)
  5927. need_fileattr = true;
  5928. if ((old_size == sctx->cur_inode_size) ||
  5929. (sctx->cur_inode_size > old_size &&
  5930. sctx->cur_inode_next_write_offset == sctx->cur_inode_size))
  5931. need_truncate = 0;
  5932. }
  5933. if (S_ISREG(sctx->cur_inode_mode)) {
  5934. if (need_send_hole(sctx)) {
  5935. if (sctx->cur_inode_last_extent == (u64)-1 ||
  5936. sctx->cur_inode_last_extent <
  5937. sctx->cur_inode_size) {
  5938. ret = get_last_extent(sctx, (u64)-1);
  5939. if (ret)
  5940. goto out;
  5941. }
  5942. if (sctx->cur_inode_last_extent < sctx->cur_inode_size) {
  5943. ret = range_is_hole_in_parent(sctx,
  5944. sctx->cur_inode_last_extent,
  5945. sctx->cur_inode_size);
  5946. if (ret < 0) {
  5947. goto out;
  5948. } else if (ret == 0) {
  5949. ret = send_hole(sctx, sctx->cur_inode_size);
  5950. if (ret < 0)
  5951. goto out;
  5952. } else {
  5953. /* Range is already a hole, skip. */
  5954. ret = 0;
  5955. }
  5956. }
  5957. }
  5958. if (need_truncate) {
  5959. ret = send_truncate(sctx, sctx->cur_ino,
  5960. sctx->cur_inode_gen,
  5961. sctx->cur_inode_size);
  5962. if (ret < 0)
  5963. goto out;
  5964. }
  5965. }
  5966. if (need_chown) {
  5967. ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  5968. left_uid, left_gid);
  5969. if (ret < 0)
  5970. goto out;
  5971. }
  5972. if (need_chmod) {
  5973. ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  5974. left_mode);
  5975. if (ret < 0)
  5976. goto out;
  5977. }
  5978. if (need_fileattr) {
  5979. ret = send_fileattr(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  5980. left_fileattr);
  5981. if (ret < 0)
  5982. goto out;
  5983. }
  5984. if (proto_cmd_ok(sctx, BTRFS_SEND_C_ENABLE_VERITY)
  5985. && sctx->cur_inode_needs_verity) {
  5986. ret = process_verity(sctx);
  5987. if (ret < 0)
  5988. goto out;
  5989. }
  5990. ret = send_capabilities(sctx);
  5991. if (ret < 0)
  5992. goto out;
  5993. /*
  5994. * If other directory inodes depended on our current directory
  5995. * inode's move/rename, now do their move/rename operations.
  5996. */
  5997. if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
  5998. ret = apply_children_dir_moves(sctx);
  5999. if (ret)
  6000. goto out;
  6001. /*
  6002. * Need to send that every time, no matter if it actually
  6003. * changed between the two trees as we have done changes to
  6004. * the inode before. If our inode is a directory and it's
  6005. * waiting to be moved/renamed, we will send its utimes when
  6006. * it's moved/renamed, therefore we don't need to do it here.
  6007. */
  6008. sctx->send_progress = sctx->cur_ino + 1;
  6009. /*
  6010. * If the current inode is a non-empty directory, delay issuing
  6011. * the utimes command for it, as it's very likely we have inodes
  6012. * with an higher number inside it. We want to issue the utimes
  6013. * command only after adding all dentries to it.
  6014. */
  6015. if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_size > 0)
  6016. ret = cache_dir_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
  6017. else
  6018. ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
  6019. if (ret < 0)
  6020. goto out;
  6021. }
  6022. out:
  6023. if (!ret)
  6024. ret = trim_dir_utimes_cache(sctx);
  6025. return ret;
  6026. }
  6027. static void close_current_inode(struct send_ctx *sctx)
  6028. {
  6029. u64 i_size;
  6030. if (sctx->cur_inode == NULL)
  6031. return;
  6032. i_size = i_size_read(sctx->cur_inode);
  6033. /*
  6034. * If we are doing an incremental send, we may have extents between the
  6035. * last processed extent and the i_size that have not been processed
  6036. * because they haven't changed but we may have read some of their pages
  6037. * through readahead, see the comments at send_extent_data().
  6038. */
  6039. if (sctx->clean_page_cache && sctx->page_cache_clear_start < i_size)
  6040. truncate_inode_pages_range(&sctx->cur_inode->i_data,
  6041. sctx->page_cache_clear_start,
  6042. round_up(i_size, PAGE_SIZE) - 1);
  6043. iput(sctx->cur_inode);
  6044. sctx->cur_inode = NULL;
  6045. }
  6046. static int changed_inode(struct send_ctx *sctx,
  6047. enum btrfs_compare_tree_result result)
  6048. {
  6049. int ret = 0;
  6050. struct btrfs_key *key = sctx->cmp_key;
  6051. struct btrfs_inode_item *left_ii = NULL;
  6052. struct btrfs_inode_item *right_ii = NULL;
  6053. u64 left_gen = 0;
  6054. u64 right_gen = 0;
  6055. close_current_inode(sctx);
  6056. sctx->cur_ino = key->objectid;
  6057. sctx->cur_inode_new_gen = false;
  6058. sctx->cur_inode_last_extent = (u64)-1;
  6059. sctx->cur_inode_next_write_offset = 0;
  6060. sctx->ignore_cur_inode = false;
  6061. fs_path_reset(&sctx->cur_inode_path);
  6062. /*
  6063. * Set send_progress to current inode. This will tell all get_cur_xxx
  6064. * functions that the current inode's refs are not updated yet. Later,
  6065. * when process_recorded_refs is finished, it is set to cur_ino + 1.
  6066. */
  6067. sctx->send_progress = sctx->cur_ino;
  6068. if (result == BTRFS_COMPARE_TREE_NEW ||
  6069. result == BTRFS_COMPARE_TREE_CHANGED) {
  6070. left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
  6071. sctx->left_path->slots[0],
  6072. struct btrfs_inode_item);
  6073. left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
  6074. left_ii);
  6075. } else {
  6076. right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
  6077. sctx->right_path->slots[0],
  6078. struct btrfs_inode_item);
  6079. right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
  6080. right_ii);
  6081. }
  6082. if (result == BTRFS_COMPARE_TREE_CHANGED) {
  6083. right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
  6084. sctx->right_path->slots[0],
  6085. struct btrfs_inode_item);
  6086. right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
  6087. right_ii);
  6088. /*
  6089. * The cur_ino = root dir case is special here. We can't treat
  6090. * the inode as deleted+reused because it would generate a
  6091. * stream that tries to delete/mkdir the root dir.
  6092. */
  6093. if (left_gen != right_gen &&
  6094. sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
  6095. sctx->cur_inode_new_gen = true;
  6096. }
  6097. /*
  6098. * Normally we do not find inodes with a link count of zero (orphans)
  6099. * because the most common case is to create a snapshot and use it
  6100. * for a send operation. However other less common use cases involve
  6101. * using a subvolume and send it after turning it to RO mode just
  6102. * after deleting all hard links of a file while holding an open
  6103. * file descriptor against it or turning a RO snapshot into RW mode,
  6104. * keep an open file descriptor against a file, delete it and then
  6105. * turn the snapshot back to RO mode before using it for a send
  6106. * operation. The former is what the receiver operation does.
  6107. * Therefore, if we want to send these snapshots soon after they're
  6108. * received, we need to handle orphan inodes as well. Moreover, orphans
  6109. * can appear not only in the send snapshot but also in the parent
  6110. * snapshot. Here are several cases:
  6111. *
  6112. * Case 1: BTRFS_COMPARE_TREE_NEW
  6113. * | send snapshot | action
  6114. * --------------------------------
  6115. * nlink | 0 | ignore
  6116. *
  6117. * Case 2: BTRFS_COMPARE_TREE_DELETED
  6118. * | parent snapshot | action
  6119. * ----------------------------------
  6120. * nlink | 0 | as usual
  6121. * Note: No unlinks will be sent because there're no paths for it.
  6122. *
  6123. * Case 3: BTRFS_COMPARE_TREE_CHANGED
  6124. * | | parent snapshot | send snapshot | action
  6125. * -----------------------------------------------------------------------
  6126. * subcase 1 | nlink | 0 | 0 | ignore
  6127. * subcase 2 | nlink | >0 | 0 | new_gen(deletion)
  6128. * subcase 3 | nlink | 0 | >0 | new_gen(creation)
  6129. *
  6130. */
  6131. if (result == BTRFS_COMPARE_TREE_NEW) {
  6132. if (btrfs_inode_nlink(sctx->left_path->nodes[0], left_ii) == 0) {
  6133. sctx->ignore_cur_inode = true;
  6134. goto out;
  6135. }
  6136. sctx->cur_inode_gen = left_gen;
  6137. sctx->cur_inode_new = true;
  6138. sctx->cur_inode_deleted = false;
  6139. sctx->cur_inode_size = btrfs_inode_size(
  6140. sctx->left_path->nodes[0], left_ii);
  6141. sctx->cur_inode_mode = btrfs_inode_mode(
  6142. sctx->left_path->nodes[0], left_ii);
  6143. sctx->cur_inode_rdev = btrfs_inode_rdev(
  6144. sctx->left_path->nodes[0], left_ii);
  6145. if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
  6146. ret = send_create_inode_if_needed(sctx);
  6147. } else if (result == BTRFS_COMPARE_TREE_DELETED) {
  6148. sctx->cur_inode_gen = right_gen;
  6149. sctx->cur_inode_new = false;
  6150. sctx->cur_inode_deleted = true;
  6151. sctx->cur_inode_size = btrfs_inode_size(
  6152. sctx->right_path->nodes[0], right_ii);
  6153. sctx->cur_inode_mode = btrfs_inode_mode(
  6154. sctx->right_path->nodes[0], right_ii);
  6155. } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
  6156. u32 new_nlinks, old_nlinks;
  6157. new_nlinks = btrfs_inode_nlink(sctx->left_path->nodes[0], left_ii);
  6158. old_nlinks = btrfs_inode_nlink(sctx->right_path->nodes[0], right_ii);
  6159. if (new_nlinks == 0 && old_nlinks == 0) {
  6160. sctx->ignore_cur_inode = true;
  6161. goto out;
  6162. } else if (new_nlinks == 0 || old_nlinks == 0) {
  6163. sctx->cur_inode_new_gen = 1;
  6164. }
  6165. /*
  6166. * We need to do some special handling in case the inode was
  6167. * reported as changed with a changed generation number. This
  6168. * means that the original inode was deleted and new inode
  6169. * reused the same inum. So we have to treat the old inode as
  6170. * deleted and the new one as new.
  6171. */
  6172. if (sctx->cur_inode_new_gen) {
  6173. /*
  6174. * First, process the inode as if it was deleted.
  6175. */
  6176. if (old_nlinks > 0) {
  6177. sctx->cur_inode_gen = right_gen;
  6178. sctx->cur_inode_new = false;
  6179. sctx->cur_inode_deleted = true;
  6180. sctx->cur_inode_size = btrfs_inode_size(
  6181. sctx->right_path->nodes[0], right_ii);
  6182. sctx->cur_inode_mode = btrfs_inode_mode(
  6183. sctx->right_path->nodes[0], right_ii);
  6184. ret = process_all_refs(sctx,
  6185. BTRFS_COMPARE_TREE_DELETED);
  6186. if (ret < 0)
  6187. goto out;
  6188. }
  6189. /*
  6190. * Now process the inode as if it was new.
  6191. */
  6192. if (new_nlinks > 0) {
  6193. sctx->cur_inode_gen = left_gen;
  6194. sctx->cur_inode_new = true;
  6195. sctx->cur_inode_deleted = false;
  6196. sctx->cur_inode_size = btrfs_inode_size(
  6197. sctx->left_path->nodes[0],
  6198. left_ii);
  6199. sctx->cur_inode_mode = btrfs_inode_mode(
  6200. sctx->left_path->nodes[0],
  6201. left_ii);
  6202. sctx->cur_inode_rdev = btrfs_inode_rdev(
  6203. sctx->left_path->nodes[0],
  6204. left_ii);
  6205. ret = send_create_inode_if_needed(sctx);
  6206. if (ret < 0)
  6207. goto out;
  6208. ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
  6209. if (ret < 0)
  6210. goto out;
  6211. /*
  6212. * Advance send_progress now as we did not get
  6213. * into process_recorded_refs_if_needed in the
  6214. * new_gen case.
  6215. */
  6216. sctx->send_progress = sctx->cur_ino + 1;
  6217. /*
  6218. * Now process all extents and xattrs of the
  6219. * inode as if they were all new.
  6220. */
  6221. ret = process_all_extents(sctx);
  6222. if (ret < 0)
  6223. goto out;
  6224. ret = process_all_new_xattrs(sctx);
  6225. if (ret < 0)
  6226. goto out;
  6227. }
  6228. } else {
  6229. sctx->cur_inode_gen = left_gen;
  6230. sctx->cur_inode_new = false;
  6231. sctx->cur_inode_new_gen = false;
  6232. sctx->cur_inode_deleted = false;
  6233. sctx->cur_inode_size = btrfs_inode_size(
  6234. sctx->left_path->nodes[0], left_ii);
  6235. sctx->cur_inode_mode = btrfs_inode_mode(
  6236. sctx->left_path->nodes[0], left_ii);
  6237. }
  6238. }
  6239. out:
  6240. return ret;
  6241. }
  6242. /*
  6243. * We have to process new refs before deleted refs, but compare_trees gives us
  6244. * the new and deleted refs mixed. To fix this, we record the new/deleted refs
  6245. * first and later process them in process_recorded_refs.
  6246. * For the cur_inode_new_gen case, we skip recording completely because
  6247. * changed_inode did already initiate processing of refs. The reason for this is
  6248. * that in this case, compare_tree actually compares the refs of 2 different
  6249. * inodes. To fix this, process_all_refs is used in changed_inode to handle all
  6250. * refs of the right tree as deleted and all refs of the left tree as new.
  6251. */
  6252. static int changed_ref(struct send_ctx *sctx,
  6253. enum btrfs_compare_tree_result result)
  6254. {
  6255. int ret = 0;
  6256. if (sctx->cur_ino != sctx->cmp_key->objectid) {
  6257. inconsistent_snapshot_error(sctx, result, "reference");
  6258. return -EIO;
  6259. }
  6260. if (!sctx->cur_inode_new_gen &&
  6261. sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
  6262. if (result == BTRFS_COMPARE_TREE_NEW)
  6263. ret = record_new_ref(sctx);
  6264. else if (result == BTRFS_COMPARE_TREE_DELETED)
  6265. ret = record_deleted_ref(sctx);
  6266. else if (result == BTRFS_COMPARE_TREE_CHANGED)
  6267. ret = record_changed_ref(sctx);
  6268. }
  6269. return ret;
  6270. }
  6271. /*
  6272. * Process new/deleted/changed xattrs. We skip processing in the
  6273. * cur_inode_new_gen case because changed_inode did already initiate processing
  6274. * of xattrs. The reason is the same as in changed_ref
  6275. */
  6276. static int changed_xattr(struct send_ctx *sctx,
  6277. enum btrfs_compare_tree_result result)
  6278. {
  6279. int ret = 0;
  6280. if (sctx->cur_ino != sctx->cmp_key->objectid) {
  6281. inconsistent_snapshot_error(sctx, result, "xattr");
  6282. return -EIO;
  6283. }
  6284. if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
  6285. if (result == BTRFS_COMPARE_TREE_NEW)
  6286. ret = process_new_xattr(sctx);
  6287. else if (result == BTRFS_COMPARE_TREE_DELETED)
  6288. ret = process_deleted_xattr(sctx);
  6289. else if (result == BTRFS_COMPARE_TREE_CHANGED)
  6290. ret = process_changed_xattr(sctx);
  6291. }
  6292. return ret;
  6293. }
  6294. /*
  6295. * Process new/deleted/changed extents. We skip processing in the
  6296. * cur_inode_new_gen case because changed_inode did already initiate processing
  6297. * of extents. The reason is the same as in changed_ref
  6298. */
  6299. static int changed_extent(struct send_ctx *sctx,
  6300. enum btrfs_compare_tree_result result)
  6301. {
  6302. int ret = 0;
  6303. /*
  6304. * We have found an extent item that changed without the inode item
  6305. * having changed. This can happen either after relocation (where the
  6306. * disk_bytenr of an extent item is replaced at
  6307. * relocation.c:replace_file_extents()) or after deduplication into a
  6308. * file in both the parent and send snapshots (where an extent item can
  6309. * get modified or replaced with a new one). Note that deduplication
  6310. * updates the inode item, but it only changes the iversion (sequence
  6311. * field in the inode item) of the inode, so if a file is deduplicated
  6312. * the same amount of times in both the parent and send snapshots, its
  6313. * iversion becomes the same in both snapshots, whence the inode item is
  6314. * the same on both snapshots.
  6315. */
  6316. if (sctx->cur_ino != sctx->cmp_key->objectid)
  6317. return 0;
  6318. if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
  6319. if (result != BTRFS_COMPARE_TREE_DELETED)
  6320. ret = process_extent(sctx, sctx->left_path,
  6321. sctx->cmp_key);
  6322. }
  6323. return ret;
  6324. }
  6325. static int changed_verity(struct send_ctx *sctx, enum btrfs_compare_tree_result result)
  6326. {
  6327. if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
  6328. if (result == BTRFS_COMPARE_TREE_NEW)
  6329. sctx->cur_inode_needs_verity = true;
  6330. }
  6331. return 0;
  6332. }
  6333. static int dir_changed(struct send_ctx *sctx, u64 dir)
  6334. {
  6335. u64 orig_gen, new_gen;
  6336. int ret;
  6337. ret = get_inode_gen(sctx->send_root, dir, &new_gen);
  6338. if (ret)
  6339. return ret;
  6340. ret = get_inode_gen(sctx->parent_root, dir, &orig_gen);
  6341. if (ret)
  6342. return ret;
  6343. return (orig_gen != new_gen) ? 1 : 0;
  6344. }
  6345. static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
  6346. struct btrfs_key *key)
  6347. {
  6348. struct btrfs_inode_extref *extref;
  6349. struct extent_buffer *leaf;
  6350. u64 dirid = 0, last_dirid = 0;
  6351. unsigned long ptr;
  6352. u32 item_size;
  6353. u32 cur_offset = 0;
  6354. int ref_name_len;
  6355. int ret = 0;
  6356. /* Easy case, just check this one dirid */
  6357. if (key->type == BTRFS_INODE_REF_KEY) {
  6358. dirid = key->offset;
  6359. ret = dir_changed(sctx, dirid);
  6360. goto out;
  6361. }
  6362. leaf = path->nodes[0];
  6363. item_size = btrfs_item_size(leaf, path->slots[0]);
  6364. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  6365. while (cur_offset < item_size) {
  6366. extref = (struct btrfs_inode_extref *)(ptr +
  6367. cur_offset);
  6368. dirid = btrfs_inode_extref_parent(leaf, extref);
  6369. ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
  6370. cur_offset += ref_name_len + sizeof(*extref);
  6371. if (dirid == last_dirid)
  6372. continue;
  6373. ret = dir_changed(sctx, dirid);
  6374. if (ret)
  6375. break;
  6376. last_dirid = dirid;
  6377. }
  6378. out:
  6379. return ret;
  6380. }
  6381. /*
  6382. * Updates compare related fields in sctx and simply forwards to the actual
  6383. * changed_xxx functions.
  6384. */
  6385. static int changed_cb(struct btrfs_path *left_path,
  6386. struct btrfs_path *right_path,
  6387. struct btrfs_key *key,
  6388. enum btrfs_compare_tree_result result,
  6389. struct send_ctx *sctx)
  6390. {
  6391. int ret = 0;
  6392. /*
  6393. * We can not hold the commit root semaphore here. This is because in
  6394. * the case of sending and receiving to the same filesystem, using a
  6395. * pipe, could result in a deadlock:
  6396. *
  6397. * 1) The task running send blocks on the pipe because it's full;
  6398. *
  6399. * 2) The task running receive, which is the only consumer of the pipe,
  6400. * is waiting for a transaction commit (for example due to a space
  6401. * reservation when doing a write or triggering a transaction commit
  6402. * when creating a subvolume);
  6403. *
  6404. * 3) The transaction is waiting to write lock the commit root semaphore,
  6405. * but can not acquire it since it's being held at 1).
  6406. *
  6407. * Down this call chain we write to the pipe through kernel_write().
  6408. * The same type of problem can also happen when sending to a file that
  6409. * is stored in the same filesystem - when reserving space for a write
  6410. * into the file, we can trigger a transaction commit.
  6411. *
  6412. * Our caller has supplied us with clones of leaves from the send and
  6413. * parent roots, so we're safe here from a concurrent relocation and
  6414. * further reallocation of metadata extents while we are here. Below we
  6415. * also assert that the leaves are clones.
  6416. */
  6417. lockdep_assert_not_held(&sctx->send_root->fs_info->commit_root_sem);
  6418. /*
  6419. * We always have a send root, so left_path is never NULL. We will not
  6420. * have a leaf when we have reached the end of the send root but have
  6421. * not yet reached the end of the parent root.
  6422. */
  6423. if (left_path->nodes[0])
  6424. ASSERT(test_bit(EXTENT_BUFFER_UNMAPPED,
  6425. &left_path->nodes[0]->bflags));
  6426. /*
  6427. * When doing a full send we don't have a parent root, so right_path is
  6428. * NULL. When doing an incremental send, we may have reached the end of
  6429. * the parent root already, so we don't have a leaf at right_path.
  6430. */
  6431. if (right_path && right_path->nodes[0])
  6432. ASSERT(test_bit(EXTENT_BUFFER_UNMAPPED,
  6433. &right_path->nodes[0]->bflags));
  6434. if (result == BTRFS_COMPARE_TREE_SAME) {
  6435. if (key->type == BTRFS_INODE_REF_KEY ||
  6436. key->type == BTRFS_INODE_EXTREF_KEY) {
  6437. ret = compare_refs(sctx, left_path, key);
  6438. if (!ret)
  6439. return 0;
  6440. if (ret < 0)
  6441. return ret;
  6442. } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
  6443. return maybe_send_hole(sctx, left_path, key);
  6444. } else {
  6445. return 0;
  6446. }
  6447. result = BTRFS_COMPARE_TREE_CHANGED;
  6448. ret = 0;
  6449. }
  6450. sctx->left_path = left_path;
  6451. sctx->right_path = right_path;
  6452. sctx->cmp_key = key;
  6453. ret = finish_inode_if_needed(sctx, 0);
  6454. if (ret < 0)
  6455. goto out;
  6456. /* Ignore non-FS objects */
  6457. if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
  6458. key->objectid == BTRFS_FREE_SPACE_OBJECTID)
  6459. goto out;
  6460. if (key->type == BTRFS_INODE_ITEM_KEY) {
  6461. ret = changed_inode(sctx, result);
  6462. } else if (!sctx->ignore_cur_inode) {
  6463. if (key->type == BTRFS_INODE_REF_KEY ||
  6464. key->type == BTRFS_INODE_EXTREF_KEY)
  6465. ret = changed_ref(sctx, result);
  6466. else if (key->type == BTRFS_XATTR_ITEM_KEY)
  6467. ret = changed_xattr(sctx, result);
  6468. else if (key->type == BTRFS_EXTENT_DATA_KEY)
  6469. ret = changed_extent(sctx, result);
  6470. else if (key->type == BTRFS_VERITY_DESC_ITEM_KEY &&
  6471. key->offset == 0)
  6472. ret = changed_verity(sctx, result);
  6473. }
  6474. out:
  6475. return ret;
  6476. }
  6477. static int search_key_again(const struct send_ctx *sctx,
  6478. struct btrfs_root *root,
  6479. struct btrfs_path *path,
  6480. const struct btrfs_key *key)
  6481. {
  6482. int ret;
  6483. if (!path->need_commit_sem)
  6484. lockdep_assert_held_read(&root->fs_info->commit_root_sem);
  6485. /*
  6486. * Roots used for send operations are readonly and no one can add,
  6487. * update or remove keys from them, so we should be able to find our
  6488. * key again. The only exception is deduplication, which can operate on
  6489. * readonly roots and add, update or remove keys to/from them - but at
  6490. * the moment we don't allow it to run in parallel with send.
  6491. */
  6492. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  6493. ASSERT(ret <= 0);
  6494. if (ret > 0) {
  6495. btrfs_print_tree(path->nodes[path->lowest_level], false);
  6496. btrfs_err(root->fs_info,
  6497. "send: key (%llu %u %llu) not found in %s root %llu, lowest_level %d, slot %d",
  6498. key->objectid, key->type, key->offset,
  6499. (root == sctx->parent_root ? "parent" : "send"),
  6500. btrfs_root_id(root), path->lowest_level,
  6501. path->slots[path->lowest_level]);
  6502. return -EUCLEAN;
  6503. }
  6504. return ret;
  6505. }
  6506. static int full_send_tree(struct send_ctx *sctx)
  6507. {
  6508. int ret;
  6509. struct btrfs_root *send_root = sctx->send_root;
  6510. struct btrfs_key key;
  6511. struct btrfs_fs_info *fs_info = send_root->fs_info;
  6512. struct btrfs_path *path;
  6513. path = alloc_path_for_send();
  6514. if (!path)
  6515. return -ENOMEM;
  6516. path->reada = READA_FORWARD_ALWAYS;
  6517. key.objectid = BTRFS_FIRST_FREE_OBJECTID;
  6518. key.type = BTRFS_INODE_ITEM_KEY;
  6519. key.offset = 0;
  6520. down_read(&fs_info->commit_root_sem);
  6521. sctx->last_reloc_trans = fs_info->last_reloc_trans;
  6522. up_read(&fs_info->commit_root_sem);
  6523. ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
  6524. if (ret < 0)
  6525. goto out;
  6526. if (ret)
  6527. goto out_finish;
  6528. while (1) {
  6529. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  6530. ret = changed_cb(path, NULL, &key,
  6531. BTRFS_COMPARE_TREE_NEW, sctx);
  6532. if (ret < 0)
  6533. goto out;
  6534. down_read(&fs_info->commit_root_sem);
  6535. if (fs_info->last_reloc_trans > sctx->last_reloc_trans) {
  6536. sctx->last_reloc_trans = fs_info->last_reloc_trans;
  6537. up_read(&fs_info->commit_root_sem);
  6538. /*
  6539. * A transaction used for relocating a block group was
  6540. * committed or is about to finish its commit. Release
  6541. * our path (leaf) and restart the search, so that we
  6542. * avoid operating on any file extent items that are
  6543. * stale, with a disk_bytenr that reflects a pre
  6544. * relocation value. This way we avoid as much as
  6545. * possible to fallback to regular writes when checking
  6546. * if we can clone file ranges.
  6547. */
  6548. btrfs_release_path(path);
  6549. ret = search_key_again(sctx, send_root, path, &key);
  6550. if (ret < 0)
  6551. goto out;
  6552. } else {
  6553. up_read(&fs_info->commit_root_sem);
  6554. }
  6555. ret = btrfs_next_item(send_root, path);
  6556. if (ret < 0)
  6557. goto out;
  6558. if (ret) {
  6559. ret = 0;
  6560. break;
  6561. }
  6562. }
  6563. out_finish:
  6564. ret = finish_inode_if_needed(sctx, 1);
  6565. out:
  6566. btrfs_free_path(path);
  6567. return ret;
  6568. }
  6569. static int replace_node_with_clone(struct btrfs_path *path, int level)
  6570. {
  6571. struct extent_buffer *clone;
  6572. clone = btrfs_clone_extent_buffer(path->nodes[level]);
  6573. if (!clone)
  6574. return -ENOMEM;
  6575. free_extent_buffer(path->nodes[level]);
  6576. path->nodes[level] = clone;
  6577. return 0;
  6578. }
  6579. static int tree_move_down(struct btrfs_path *path, int *level, u64 reada_min_gen)
  6580. {
  6581. struct extent_buffer *eb;
  6582. struct extent_buffer *parent = path->nodes[*level];
  6583. int slot = path->slots[*level];
  6584. const int nritems = btrfs_header_nritems(parent);
  6585. u64 reada_max;
  6586. u64 reada_done = 0;
  6587. lockdep_assert_held_read(&parent->fs_info->commit_root_sem);
  6588. ASSERT(*level != 0);
  6589. eb = btrfs_read_node_slot(parent, slot);
  6590. if (IS_ERR(eb))
  6591. return PTR_ERR(eb);
  6592. /*
  6593. * Trigger readahead for the next leaves we will process, so that it is
  6594. * very likely that when we need them they are already in memory and we
  6595. * will not block on disk IO. For nodes we only do readahead for one,
  6596. * since the time window between processing nodes is typically larger.
  6597. */
  6598. reada_max = (*level == 1 ? SZ_128K : eb->fs_info->nodesize);
  6599. for (slot++; slot < nritems && reada_done < reada_max; slot++) {
  6600. if (btrfs_node_ptr_generation(parent, slot) > reada_min_gen) {
  6601. btrfs_readahead_node_child(parent, slot);
  6602. reada_done += eb->fs_info->nodesize;
  6603. }
  6604. }
  6605. path->nodes[*level - 1] = eb;
  6606. path->slots[*level - 1] = 0;
  6607. (*level)--;
  6608. if (*level == 0)
  6609. return replace_node_with_clone(path, 0);
  6610. return 0;
  6611. }
  6612. static int tree_move_next_or_upnext(struct btrfs_path *path,
  6613. int *level, int root_level)
  6614. {
  6615. int ret = 0;
  6616. int nritems;
  6617. nritems = btrfs_header_nritems(path->nodes[*level]);
  6618. path->slots[*level]++;
  6619. while (path->slots[*level] >= nritems) {
  6620. if (*level == root_level) {
  6621. path->slots[*level] = nritems - 1;
  6622. return -1;
  6623. }
  6624. /* move upnext */
  6625. path->slots[*level] = 0;
  6626. free_extent_buffer(path->nodes[*level]);
  6627. path->nodes[*level] = NULL;
  6628. (*level)++;
  6629. path->slots[*level]++;
  6630. nritems = btrfs_header_nritems(path->nodes[*level]);
  6631. ret = 1;
  6632. }
  6633. return ret;
  6634. }
  6635. /*
  6636. * Returns 1 if it had to move up and next. 0 is returned if it moved only next
  6637. * or down.
  6638. */
  6639. static int tree_advance(struct btrfs_path *path,
  6640. int *level, int root_level,
  6641. int allow_down,
  6642. struct btrfs_key *key,
  6643. u64 reada_min_gen)
  6644. {
  6645. int ret;
  6646. if (*level == 0 || !allow_down) {
  6647. ret = tree_move_next_or_upnext(path, level, root_level);
  6648. } else {
  6649. ret = tree_move_down(path, level, reada_min_gen);
  6650. }
  6651. /*
  6652. * Even if we have reached the end of a tree, ret is -1, update the key
  6653. * anyway, so that in case we need to restart due to a block group
  6654. * relocation, we can assert that the last key of the root node still
  6655. * exists in the tree.
  6656. */
  6657. if (*level == 0)
  6658. btrfs_item_key_to_cpu(path->nodes[*level], key,
  6659. path->slots[*level]);
  6660. else
  6661. btrfs_node_key_to_cpu(path->nodes[*level], key,
  6662. path->slots[*level]);
  6663. return ret;
  6664. }
  6665. static int tree_compare_item(struct btrfs_path *left_path,
  6666. struct btrfs_path *right_path,
  6667. char *tmp_buf)
  6668. {
  6669. int cmp;
  6670. int len1, len2;
  6671. unsigned long off1, off2;
  6672. len1 = btrfs_item_size(left_path->nodes[0], left_path->slots[0]);
  6673. len2 = btrfs_item_size(right_path->nodes[0], right_path->slots[0]);
  6674. if (len1 != len2)
  6675. return 1;
  6676. off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
  6677. off2 = btrfs_item_ptr_offset(right_path->nodes[0],
  6678. right_path->slots[0]);
  6679. read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
  6680. cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
  6681. if (cmp)
  6682. return 1;
  6683. return 0;
  6684. }
  6685. /*
  6686. * A transaction used for relocating a block group was committed or is about to
  6687. * finish its commit. Release our paths and restart the search, so that we are
  6688. * not using stale extent buffers:
  6689. *
  6690. * 1) For levels > 0, we are only holding references of extent buffers, without
  6691. * any locks on them, which does not prevent them from having been relocated
  6692. * and reallocated after the last time we released the commit root semaphore.
  6693. * The exception are the root nodes, for which we always have a clone, see
  6694. * the comment at btrfs_compare_trees();
  6695. *
  6696. * 2) For leaves, level 0, we are holding copies (clones) of extent buffers, so
  6697. * we are safe from the concurrent relocation and reallocation. However they
  6698. * can have file extent items with a pre relocation disk_bytenr value, so we
  6699. * restart the start from the current commit roots and clone the new leaves so
  6700. * that we get the post relocation disk_bytenr values. Not doing so, could
  6701. * make us clone the wrong data in case there are new extents using the old
  6702. * disk_bytenr that happen to be shared.
  6703. */
  6704. static int restart_after_relocation(struct btrfs_path *left_path,
  6705. struct btrfs_path *right_path,
  6706. const struct btrfs_key *left_key,
  6707. const struct btrfs_key *right_key,
  6708. int left_level,
  6709. int right_level,
  6710. const struct send_ctx *sctx)
  6711. {
  6712. int root_level;
  6713. int ret;
  6714. lockdep_assert_held_read(&sctx->send_root->fs_info->commit_root_sem);
  6715. btrfs_release_path(left_path);
  6716. btrfs_release_path(right_path);
  6717. /*
  6718. * Since keys can not be added or removed to/from our roots because they
  6719. * are readonly and we do not allow deduplication to run in parallel
  6720. * (which can add, remove or change keys), the layout of the trees should
  6721. * not change.
  6722. */
  6723. left_path->lowest_level = left_level;
  6724. ret = search_key_again(sctx, sctx->send_root, left_path, left_key);
  6725. if (ret < 0)
  6726. return ret;
  6727. right_path->lowest_level = right_level;
  6728. ret = search_key_again(sctx, sctx->parent_root, right_path, right_key);
  6729. if (ret < 0)
  6730. return ret;
  6731. /*
  6732. * If the lowest level nodes are leaves, clone them so that they can be
  6733. * safely used by changed_cb() while not under the protection of the
  6734. * commit root semaphore, even if relocation and reallocation happens in
  6735. * parallel.
  6736. */
  6737. if (left_level == 0) {
  6738. ret = replace_node_with_clone(left_path, 0);
  6739. if (ret < 0)
  6740. return ret;
  6741. }
  6742. if (right_level == 0) {
  6743. ret = replace_node_with_clone(right_path, 0);
  6744. if (ret < 0)
  6745. return ret;
  6746. }
  6747. /*
  6748. * Now clone the root nodes (unless they happen to be the leaves we have
  6749. * already cloned). This is to protect against concurrent snapshotting of
  6750. * the send and parent roots (see the comment at btrfs_compare_trees()).
  6751. */
  6752. root_level = btrfs_header_level(sctx->send_root->commit_root);
  6753. if (root_level > 0) {
  6754. ret = replace_node_with_clone(left_path, root_level);
  6755. if (ret < 0)
  6756. return ret;
  6757. }
  6758. root_level = btrfs_header_level(sctx->parent_root->commit_root);
  6759. if (root_level > 0) {
  6760. ret = replace_node_with_clone(right_path, root_level);
  6761. if (ret < 0)
  6762. return ret;
  6763. }
  6764. return 0;
  6765. }
  6766. /*
  6767. * This function compares two trees and calls the provided callback for
  6768. * every changed/new/deleted item it finds.
  6769. * If shared tree blocks are encountered, whole subtrees are skipped, making
  6770. * the compare pretty fast on snapshotted subvolumes.
  6771. *
  6772. * This currently works on commit roots only. As commit roots are read only,
  6773. * we don't do any locking. The commit roots are protected with transactions.
  6774. * Transactions are ended and rejoined when a commit is tried in between.
  6775. *
  6776. * This function checks for modifications done to the trees while comparing.
  6777. * If it detects a change, it aborts immediately.
  6778. */
  6779. static int btrfs_compare_trees(struct btrfs_root *left_root,
  6780. struct btrfs_root *right_root, struct send_ctx *sctx)
  6781. {
  6782. struct btrfs_fs_info *fs_info = left_root->fs_info;
  6783. int ret;
  6784. int cmp;
  6785. struct btrfs_path *left_path = NULL;
  6786. struct btrfs_path *right_path = NULL;
  6787. struct btrfs_key left_key;
  6788. struct btrfs_key right_key;
  6789. char *tmp_buf = NULL;
  6790. int left_root_level;
  6791. int right_root_level;
  6792. int left_level;
  6793. int right_level;
  6794. int left_end_reached = 0;
  6795. int right_end_reached = 0;
  6796. int advance_left = 0;
  6797. int advance_right = 0;
  6798. u64 left_blockptr;
  6799. u64 right_blockptr;
  6800. u64 left_gen;
  6801. u64 right_gen;
  6802. u64 reada_min_gen;
  6803. left_path = btrfs_alloc_path();
  6804. if (!left_path) {
  6805. ret = -ENOMEM;
  6806. goto out;
  6807. }
  6808. right_path = btrfs_alloc_path();
  6809. if (!right_path) {
  6810. ret = -ENOMEM;
  6811. goto out;
  6812. }
  6813. tmp_buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
  6814. if (!tmp_buf) {
  6815. ret = -ENOMEM;
  6816. goto out;
  6817. }
  6818. left_path->search_commit_root = 1;
  6819. left_path->skip_locking = 1;
  6820. right_path->search_commit_root = 1;
  6821. right_path->skip_locking = 1;
  6822. /*
  6823. * Strategy: Go to the first items of both trees. Then do
  6824. *
  6825. * If both trees are at level 0
  6826. * Compare keys of current items
  6827. * If left < right treat left item as new, advance left tree
  6828. * and repeat
  6829. * If left > right treat right item as deleted, advance right tree
  6830. * and repeat
  6831. * If left == right do deep compare of items, treat as changed if
  6832. * needed, advance both trees and repeat
  6833. * If both trees are at the same level but not at level 0
  6834. * Compare keys of current nodes/leafs
  6835. * If left < right advance left tree and repeat
  6836. * If left > right advance right tree and repeat
  6837. * If left == right compare blockptrs of the next nodes/leafs
  6838. * If they match advance both trees but stay at the same level
  6839. * and repeat
  6840. * If they don't match advance both trees while allowing to go
  6841. * deeper and repeat
  6842. * If tree levels are different
  6843. * Advance the tree that needs it and repeat
  6844. *
  6845. * Advancing a tree means:
  6846. * If we are at level 0, try to go to the next slot. If that's not
  6847. * possible, go one level up and repeat. Stop when we found a level
  6848. * where we could go to the next slot. We may at this point be on a
  6849. * node or a leaf.
  6850. *
  6851. * If we are not at level 0 and not on shared tree blocks, go one
  6852. * level deeper.
  6853. *
  6854. * If we are not at level 0 and on shared tree blocks, go one slot to
  6855. * the right if possible or go up and right.
  6856. */
  6857. down_read(&fs_info->commit_root_sem);
  6858. left_level = btrfs_header_level(left_root->commit_root);
  6859. left_root_level = left_level;
  6860. /*
  6861. * We clone the root node of the send and parent roots to prevent races
  6862. * with snapshot creation of these roots. Snapshot creation COWs the
  6863. * root node of a tree, so after the transaction is committed the old
  6864. * extent can be reallocated while this send operation is still ongoing.
  6865. * So we clone them, under the commit root semaphore, to be race free.
  6866. */
  6867. left_path->nodes[left_level] =
  6868. btrfs_clone_extent_buffer(left_root->commit_root);
  6869. if (!left_path->nodes[left_level]) {
  6870. ret = -ENOMEM;
  6871. goto out_unlock;
  6872. }
  6873. right_level = btrfs_header_level(right_root->commit_root);
  6874. right_root_level = right_level;
  6875. right_path->nodes[right_level] =
  6876. btrfs_clone_extent_buffer(right_root->commit_root);
  6877. if (!right_path->nodes[right_level]) {
  6878. ret = -ENOMEM;
  6879. goto out_unlock;
  6880. }
  6881. /*
  6882. * Our right root is the parent root, while the left root is the "send"
  6883. * root. We know that all new nodes/leaves in the left root must have
  6884. * a generation greater than the right root's generation, so we trigger
  6885. * readahead for those nodes and leaves of the left root, as we know we
  6886. * will need to read them at some point.
  6887. */
  6888. reada_min_gen = btrfs_header_generation(right_root->commit_root);
  6889. if (left_level == 0)
  6890. btrfs_item_key_to_cpu(left_path->nodes[left_level],
  6891. &left_key, left_path->slots[left_level]);
  6892. else
  6893. btrfs_node_key_to_cpu(left_path->nodes[left_level],
  6894. &left_key, left_path->slots[left_level]);
  6895. if (right_level == 0)
  6896. btrfs_item_key_to_cpu(right_path->nodes[right_level],
  6897. &right_key, right_path->slots[right_level]);
  6898. else
  6899. btrfs_node_key_to_cpu(right_path->nodes[right_level],
  6900. &right_key, right_path->slots[right_level]);
  6901. sctx->last_reloc_trans = fs_info->last_reloc_trans;
  6902. while (1) {
  6903. if (need_resched() ||
  6904. rwsem_is_contended(&fs_info->commit_root_sem)) {
  6905. up_read(&fs_info->commit_root_sem);
  6906. cond_resched();
  6907. down_read(&fs_info->commit_root_sem);
  6908. }
  6909. if (fs_info->last_reloc_trans > sctx->last_reloc_trans) {
  6910. ret = restart_after_relocation(left_path, right_path,
  6911. &left_key, &right_key,
  6912. left_level, right_level,
  6913. sctx);
  6914. if (ret < 0)
  6915. goto out_unlock;
  6916. sctx->last_reloc_trans = fs_info->last_reloc_trans;
  6917. }
  6918. if (advance_left && !left_end_reached) {
  6919. ret = tree_advance(left_path, &left_level,
  6920. left_root_level,
  6921. advance_left != ADVANCE_ONLY_NEXT,
  6922. &left_key, reada_min_gen);
  6923. if (ret == -1)
  6924. left_end_reached = ADVANCE;
  6925. else if (ret < 0)
  6926. goto out_unlock;
  6927. advance_left = 0;
  6928. }
  6929. if (advance_right && !right_end_reached) {
  6930. ret = tree_advance(right_path, &right_level,
  6931. right_root_level,
  6932. advance_right != ADVANCE_ONLY_NEXT,
  6933. &right_key, reada_min_gen);
  6934. if (ret == -1)
  6935. right_end_reached = ADVANCE;
  6936. else if (ret < 0)
  6937. goto out_unlock;
  6938. advance_right = 0;
  6939. }
  6940. if (left_end_reached && right_end_reached) {
  6941. ret = 0;
  6942. goto out_unlock;
  6943. } else if (left_end_reached) {
  6944. if (right_level == 0) {
  6945. up_read(&fs_info->commit_root_sem);
  6946. ret = changed_cb(left_path, right_path,
  6947. &right_key,
  6948. BTRFS_COMPARE_TREE_DELETED,
  6949. sctx);
  6950. if (ret < 0)
  6951. goto out;
  6952. down_read(&fs_info->commit_root_sem);
  6953. }
  6954. advance_right = ADVANCE;
  6955. continue;
  6956. } else if (right_end_reached) {
  6957. if (left_level == 0) {
  6958. up_read(&fs_info->commit_root_sem);
  6959. ret = changed_cb(left_path, right_path,
  6960. &left_key,
  6961. BTRFS_COMPARE_TREE_NEW,
  6962. sctx);
  6963. if (ret < 0)
  6964. goto out;
  6965. down_read(&fs_info->commit_root_sem);
  6966. }
  6967. advance_left = ADVANCE;
  6968. continue;
  6969. }
  6970. if (left_level == 0 && right_level == 0) {
  6971. up_read(&fs_info->commit_root_sem);
  6972. cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
  6973. if (cmp < 0) {
  6974. ret = changed_cb(left_path, right_path,
  6975. &left_key,
  6976. BTRFS_COMPARE_TREE_NEW,
  6977. sctx);
  6978. advance_left = ADVANCE;
  6979. } else if (cmp > 0) {
  6980. ret = changed_cb(left_path, right_path,
  6981. &right_key,
  6982. BTRFS_COMPARE_TREE_DELETED,
  6983. sctx);
  6984. advance_right = ADVANCE;
  6985. } else {
  6986. enum btrfs_compare_tree_result result;
  6987. WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
  6988. ret = tree_compare_item(left_path, right_path,
  6989. tmp_buf);
  6990. if (ret)
  6991. result = BTRFS_COMPARE_TREE_CHANGED;
  6992. else
  6993. result = BTRFS_COMPARE_TREE_SAME;
  6994. ret = changed_cb(left_path, right_path,
  6995. &left_key, result, sctx);
  6996. advance_left = ADVANCE;
  6997. advance_right = ADVANCE;
  6998. }
  6999. if (ret < 0)
  7000. goto out;
  7001. down_read(&fs_info->commit_root_sem);
  7002. } else if (left_level == right_level) {
  7003. cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
  7004. if (cmp < 0) {
  7005. advance_left = ADVANCE;
  7006. } else if (cmp > 0) {
  7007. advance_right = ADVANCE;
  7008. } else {
  7009. left_blockptr = btrfs_node_blockptr(
  7010. left_path->nodes[left_level],
  7011. left_path->slots[left_level]);
  7012. right_blockptr = btrfs_node_blockptr(
  7013. right_path->nodes[right_level],
  7014. right_path->slots[right_level]);
  7015. left_gen = btrfs_node_ptr_generation(
  7016. left_path->nodes[left_level],
  7017. left_path->slots[left_level]);
  7018. right_gen = btrfs_node_ptr_generation(
  7019. right_path->nodes[right_level],
  7020. right_path->slots[right_level]);
  7021. if (left_blockptr == right_blockptr &&
  7022. left_gen == right_gen) {
  7023. /*
  7024. * As we're on a shared block, don't
  7025. * allow to go deeper.
  7026. */
  7027. advance_left = ADVANCE_ONLY_NEXT;
  7028. advance_right = ADVANCE_ONLY_NEXT;
  7029. } else {
  7030. advance_left = ADVANCE;
  7031. advance_right = ADVANCE;
  7032. }
  7033. }
  7034. } else if (left_level < right_level) {
  7035. advance_right = ADVANCE;
  7036. } else {
  7037. advance_left = ADVANCE;
  7038. }
  7039. }
  7040. out_unlock:
  7041. up_read(&fs_info->commit_root_sem);
  7042. out:
  7043. btrfs_free_path(left_path);
  7044. btrfs_free_path(right_path);
  7045. kvfree(tmp_buf);
  7046. return ret;
  7047. }
  7048. static int send_subvol(struct send_ctx *sctx)
  7049. {
  7050. int ret;
  7051. if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
  7052. ret = send_header(sctx);
  7053. if (ret < 0)
  7054. goto out;
  7055. }
  7056. ret = send_subvol_begin(sctx);
  7057. if (ret < 0)
  7058. goto out;
  7059. if (sctx->parent_root) {
  7060. ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root, sctx);
  7061. if (ret < 0)
  7062. goto out;
  7063. ret = finish_inode_if_needed(sctx, 1);
  7064. if (ret < 0)
  7065. goto out;
  7066. } else {
  7067. ret = full_send_tree(sctx);
  7068. if (ret < 0)
  7069. goto out;
  7070. }
  7071. out:
  7072. free_recorded_refs(sctx);
  7073. return ret;
  7074. }
  7075. /*
  7076. * If orphan cleanup did remove any orphans from a root, it means the tree
  7077. * was modified and therefore the commit root is not the same as the current
  7078. * root anymore. This is a problem, because send uses the commit root and
  7079. * therefore can see inode items that don't exist in the current root anymore,
  7080. * and for example make calls to btrfs_iget, which will do tree lookups based
  7081. * on the current root and not on the commit root. Those lookups will fail,
  7082. * returning a -ESTALE error, and making send fail with that error. So make
  7083. * sure a send does not see any orphans we have just removed, and that it will
  7084. * see the same inodes regardless of whether a transaction commit happened
  7085. * before it started (meaning that the commit root will be the same as the
  7086. * current root) or not.
  7087. */
  7088. static int ensure_commit_roots_uptodate(struct send_ctx *sctx)
  7089. {
  7090. struct btrfs_root *root = sctx->parent_root;
  7091. if (root && root->node != root->commit_root)
  7092. return btrfs_commit_current_transaction(root);
  7093. for (int i = 0; i < sctx->clone_roots_cnt; i++) {
  7094. root = sctx->clone_roots[i].root;
  7095. if (root->node != root->commit_root)
  7096. return btrfs_commit_current_transaction(root);
  7097. }
  7098. return 0;
  7099. }
  7100. /*
  7101. * Make sure any existing dellaloc is flushed for any root used by a send
  7102. * operation so that we do not miss any data and we do not race with writeback
  7103. * finishing and changing a tree while send is using the tree. This could
  7104. * happen if a subvolume is in RW mode, has delalloc, is turned to RO mode and
  7105. * a send operation then uses the subvolume.
  7106. * After flushing delalloc ensure_commit_roots_uptodate() must be called.
  7107. */
  7108. static int flush_delalloc_roots(struct send_ctx *sctx)
  7109. {
  7110. struct btrfs_root *root = sctx->parent_root;
  7111. int ret;
  7112. int i;
  7113. if (root) {
  7114. ret = btrfs_start_delalloc_snapshot(root, false);
  7115. if (ret)
  7116. return ret;
  7117. btrfs_wait_ordered_extents(root, U64_MAX, NULL);
  7118. }
  7119. for (i = 0; i < sctx->clone_roots_cnt; i++) {
  7120. root = sctx->clone_roots[i].root;
  7121. ret = btrfs_start_delalloc_snapshot(root, false);
  7122. if (ret)
  7123. return ret;
  7124. btrfs_wait_ordered_extents(root, U64_MAX, NULL);
  7125. }
  7126. return 0;
  7127. }
  7128. static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
  7129. {
  7130. spin_lock(&root->root_item_lock);
  7131. root->send_in_progress--;
  7132. /*
  7133. * Not much left to do, we don't know why it's unbalanced and
  7134. * can't blindly reset it to 0.
  7135. */
  7136. if (root->send_in_progress < 0)
  7137. btrfs_err(root->fs_info,
  7138. "send_in_progress unbalanced %d root %llu",
  7139. root->send_in_progress, btrfs_root_id(root));
  7140. spin_unlock(&root->root_item_lock);
  7141. }
  7142. static void dedupe_in_progress_warn(const struct btrfs_root *root)
  7143. {
  7144. btrfs_warn_rl(root->fs_info,
  7145. "cannot use root %llu for send while deduplications on it are in progress (%d in progress)",
  7146. btrfs_root_id(root), root->dedupe_in_progress);
  7147. }
  7148. long btrfs_ioctl_send(struct btrfs_inode *inode, const struct btrfs_ioctl_send_args *arg)
  7149. {
  7150. int ret = 0;
  7151. struct btrfs_root *send_root = inode->root;
  7152. struct btrfs_fs_info *fs_info = send_root->fs_info;
  7153. struct btrfs_root *clone_root;
  7154. struct send_ctx *sctx = NULL;
  7155. u32 i;
  7156. u64 *clone_sources_tmp = NULL;
  7157. int clone_sources_to_rollback = 0;
  7158. size_t alloc_size;
  7159. int sort_clone_roots = 0;
  7160. struct btrfs_lru_cache_entry *entry;
  7161. struct btrfs_lru_cache_entry *tmp;
  7162. if (!capable(CAP_SYS_ADMIN))
  7163. return -EPERM;
  7164. /*
  7165. * The subvolume must remain read-only during send, protect against
  7166. * making it RW. This also protects against deletion.
  7167. */
  7168. spin_lock(&send_root->root_item_lock);
  7169. if (btrfs_root_readonly(send_root) && send_root->dedupe_in_progress) {
  7170. dedupe_in_progress_warn(send_root);
  7171. spin_unlock(&send_root->root_item_lock);
  7172. return -EAGAIN;
  7173. }
  7174. send_root->send_in_progress++;
  7175. spin_unlock(&send_root->root_item_lock);
  7176. /*
  7177. * Userspace tools do the checks and warn the user if it's
  7178. * not RO.
  7179. */
  7180. if (!btrfs_root_readonly(send_root)) {
  7181. ret = -EPERM;
  7182. goto out;
  7183. }
  7184. /*
  7185. * Check that we don't overflow at later allocations, we request
  7186. * clone_sources_count + 1 items, and compare to unsigned long inside
  7187. * access_ok. Also set an upper limit for allocation size so this can't
  7188. * easily exhaust memory. Max number of clone sources is about 200K.
  7189. */
  7190. if (arg->clone_sources_count > SZ_8M / sizeof(struct clone_root)) {
  7191. ret = -EINVAL;
  7192. goto out;
  7193. }
  7194. if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
  7195. ret = -EOPNOTSUPP;
  7196. goto out;
  7197. }
  7198. sctx = kzalloc(sizeof(struct send_ctx), GFP_KERNEL);
  7199. if (!sctx) {
  7200. ret = -ENOMEM;
  7201. goto out;
  7202. }
  7203. init_path(&sctx->cur_inode_path);
  7204. INIT_LIST_HEAD(&sctx->new_refs);
  7205. INIT_LIST_HEAD(&sctx->deleted_refs);
  7206. btrfs_lru_cache_init(&sctx->name_cache, SEND_MAX_NAME_CACHE_SIZE);
  7207. btrfs_lru_cache_init(&sctx->backref_cache, SEND_MAX_BACKREF_CACHE_SIZE);
  7208. btrfs_lru_cache_init(&sctx->dir_created_cache,
  7209. SEND_MAX_DIR_CREATED_CACHE_SIZE);
  7210. /*
  7211. * This cache is periodically trimmed to a fixed size elsewhere, see
  7212. * cache_dir_utimes() and trim_dir_utimes_cache().
  7213. */
  7214. btrfs_lru_cache_init(&sctx->dir_utimes_cache, 0);
  7215. sctx->pending_dir_moves = RB_ROOT;
  7216. sctx->waiting_dir_moves = RB_ROOT;
  7217. sctx->orphan_dirs = RB_ROOT;
  7218. sctx->rbtree_new_refs = RB_ROOT;
  7219. sctx->rbtree_deleted_refs = RB_ROOT;
  7220. sctx->flags = arg->flags;
  7221. if (arg->flags & BTRFS_SEND_FLAG_VERSION) {
  7222. if (arg->version > BTRFS_SEND_STREAM_VERSION) {
  7223. ret = -EPROTO;
  7224. goto out;
  7225. }
  7226. /* Zero means "use the highest version" */
  7227. sctx->proto = arg->version ?: BTRFS_SEND_STREAM_VERSION;
  7228. } else {
  7229. sctx->proto = 1;
  7230. }
  7231. if ((arg->flags & BTRFS_SEND_FLAG_COMPRESSED) && sctx->proto < 2) {
  7232. ret = -EINVAL;
  7233. goto out;
  7234. }
  7235. sctx->send_filp = fget(arg->send_fd);
  7236. if (!sctx->send_filp || !(sctx->send_filp->f_mode & FMODE_WRITE)) {
  7237. ret = -EBADF;
  7238. goto out;
  7239. }
  7240. sctx->send_root = send_root;
  7241. /*
  7242. * Unlikely but possible, if the subvolume is marked for deletion but
  7243. * is slow to remove the directory entry, send can still be started
  7244. */
  7245. if (btrfs_root_dead(sctx->send_root)) {
  7246. ret = -EPERM;
  7247. goto out;
  7248. }
  7249. sctx->clone_roots_cnt = arg->clone_sources_count;
  7250. if (sctx->proto >= 2) {
  7251. u32 send_buf_num_pages;
  7252. sctx->send_max_size = BTRFS_SEND_BUF_SIZE_V2;
  7253. sctx->send_buf = vmalloc(sctx->send_max_size);
  7254. if (!sctx->send_buf) {
  7255. ret = -ENOMEM;
  7256. goto out;
  7257. }
  7258. send_buf_num_pages = sctx->send_max_size >> PAGE_SHIFT;
  7259. sctx->send_buf_pages = kcalloc(send_buf_num_pages,
  7260. sizeof(*sctx->send_buf_pages),
  7261. GFP_KERNEL);
  7262. if (!sctx->send_buf_pages) {
  7263. ret = -ENOMEM;
  7264. goto out;
  7265. }
  7266. for (i = 0; i < send_buf_num_pages; i++) {
  7267. sctx->send_buf_pages[i] =
  7268. vmalloc_to_page(sctx->send_buf + (i << PAGE_SHIFT));
  7269. }
  7270. } else {
  7271. sctx->send_max_size = BTRFS_SEND_BUF_SIZE_V1;
  7272. sctx->send_buf = kvmalloc(sctx->send_max_size, GFP_KERNEL);
  7273. }
  7274. if (!sctx->send_buf) {
  7275. ret = -ENOMEM;
  7276. goto out;
  7277. }
  7278. sctx->clone_roots = kvcalloc(arg->clone_sources_count + 1,
  7279. sizeof(*sctx->clone_roots),
  7280. GFP_KERNEL);
  7281. if (!sctx->clone_roots) {
  7282. ret = -ENOMEM;
  7283. goto out;
  7284. }
  7285. alloc_size = array_size(sizeof(*arg->clone_sources),
  7286. arg->clone_sources_count);
  7287. if (arg->clone_sources_count) {
  7288. clone_sources_tmp = kvmalloc(alloc_size, GFP_KERNEL);
  7289. if (!clone_sources_tmp) {
  7290. ret = -ENOMEM;
  7291. goto out;
  7292. }
  7293. ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
  7294. alloc_size);
  7295. if (ret) {
  7296. ret = -EFAULT;
  7297. goto out;
  7298. }
  7299. for (i = 0; i < arg->clone_sources_count; i++) {
  7300. clone_root = btrfs_get_fs_root(fs_info,
  7301. clone_sources_tmp[i], true);
  7302. if (IS_ERR(clone_root)) {
  7303. ret = PTR_ERR(clone_root);
  7304. goto out;
  7305. }
  7306. spin_lock(&clone_root->root_item_lock);
  7307. if (!btrfs_root_readonly(clone_root) ||
  7308. btrfs_root_dead(clone_root)) {
  7309. spin_unlock(&clone_root->root_item_lock);
  7310. btrfs_put_root(clone_root);
  7311. ret = -EPERM;
  7312. goto out;
  7313. }
  7314. if (clone_root->dedupe_in_progress) {
  7315. dedupe_in_progress_warn(clone_root);
  7316. spin_unlock(&clone_root->root_item_lock);
  7317. btrfs_put_root(clone_root);
  7318. ret = -EAGAIN;
  7319. goto out;
  7320. }
  7321. clone_root->send_in_progress++;
  7322. spin_unlock(&clone_root->root_item_lock);
  7323. sctx->clone_roots[i].root = clone_root;
  7324. clone_sources_to_rollback = i + 1;
  7325. }
  7326. kvfree(clone_sources_tmp);
  7327. clone_sources_tmp = NULL;
  7328. }
  7329. if (arg->parent_root) {
  7330. sctx->parent_root = btrfs_get_fs_root(fs_info, arg->parent_root,
  7331. true);
  7332. if (IS_ERR(sctx->parent_root)) {
  7333. ret = PTR_ERR(sctx->parent_root);
  7334. goto out;
  7335. }
  7336. spin_lock(&sctx->parent_root->root_item_lock);
  7337. sctx->parent_root->send_in_progress++;
  7338. if (!btrfs_root_readonly(sctx->parent_root) ||
  7339. btrfs_root_dead(sctx->parent_root)) {
  7340. spin_unlock(&sctx->parent_root->root_item_lock);
  7341. ret = -EPERM;
  7342. goto out;
  7343. }
  7344. if (sctx->parent_root->dedupe_in_progress) {
  7345. dedupe_in_progress_warn(sctx->parent_root);
  7346. spin_unlock(&sctx->parent_root->root_item_lock);
  7347. ret = -EAGAIN;
  7348. goto out;
  7349. }
  7350. spin_unlock(&sctx->parent_root->root_item_lock);
  7351. }
  7352. /*
  7353. * Clones from send_root are allowed, but only if the clone source
  7354. * is behind the current send position. This is checked while searching
  7355. * for possible clone sources.
  7356. */
  7357. sctx->clone_roots[sctx->clone_roots_cnt++].root =
  7358. btrfs_grab_root(sctx->send_root);
  7359. /* We do a bsearch later */
  7360. sort(sctx->clone_roots, sctx->clone_roots_cnt,
  7361. sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
  7362. NULL);
  7363. sort_clone_roots = 1;
  7364. ret = flush_delalloc_roots(sctx);
  7365. if (ret)
  7366. goto out;
  7367. ret = ensure_commit_roots_uptodate(sctx);
  7368. if (ret)
  7369. goto out;
  7370. ret = send_subvol(sctx);
  7371. if (ret < 0)
  7372. goto out;
  7373. btrfs_lru_cache_for_each_entry_safe(&sctx->dir_utimes_cache, entry, tmp) {
  7374. ret = send_utimes(sctx, entry->key, entry->gen);
  7375. if (ret < 0)
  7376. goto out;
  7377. btrfs_lru_cache_remove(&sctx->dir_utimes_cache, entry);
  7378. }
  7379. if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
  7380. ret = begin_cmd(sctx, BTRFS_SEND_C_END);
  7381. if (ret < 0)
  7382. goto out;
  7383. ret = send_cmd(sctx);
  7384. if (ret < 0)
  7385. goto out;
  7386. }
  7387. out:
  7388. WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
  7389. while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
  7390. struct rb_node *n;
  7391. struct pending_dir_move *pm;
  7392. n = rb_first(&sctx->pending_dir_moves);
  7393. pm = rb_entry(n, struct pending_dir_move, node);
  7394. while (!list_empty(&pm->list)) {
  7395. struct pending_dir_move *pm2;
  7396. pm2 = list_first_entry(&pm->list,
  7397. struct pending_dir_move, list);
  7398. free_pending_move(sctx, pm2);
  7399. }
  7400. free_pending_move(sctx, pm);
  7401. }
  7402. WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
  7403. while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
  7404. struct rb_node *n;
  7405. struct waiting_dir_move *dm;
  7406. n = rb_first(&sctx->waiting_dir_moves);
  7407. dm = rb_entry(n, struct waiting_dir_move, node);
  7408. rb_erase(&dm->node, &sctx->waiting_dir_moves);
  7409. kfree(dm);
  7410. }
  7411. WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
  7412. while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
  7413. struct rb_node *n;
  7414. struct orphan_dir_info *odi;
  7415. n = rb_first(&sctx->orphan_dirs);
  7416. odi = rb_entry(n, struct orphan_dir_info, node);
  7417. free_orphan_dir_info(sctx, odi);
  7418. }
  7419. if (sort_clone_roots) {
  7420. for (i = 0; i < sctx->clone_roots_cnt; i++) {
  7421. btrfs_root_dec_send_in_progress(
  7422. sctx->clone_roots[i].root);
  7423. btrfs_put_root(sctx->clone_roots[i].root);
  7424. }
  7425. } else {
  7426. for (i = 0; sctx && i < clone_sources_to_rollback; i++) {
  7427. btrfs_root_dec_send_in_progress(
  7428. sctx->clone_roots[i].root);
  7429. btrfs_put_root(sctx->clone_roots[i].root);
  7430. }
  7431. btrfs_root_dec_send_in_progress(send_root);
  7432. }
  7433. if (sctx && !IS_ERR_OR_NULL(sctx->parent_root)) {
  7434. btrfs_root_dec_send_in_progress(sctx->parent_root);
  7435. btrfs_put_root(sctx->parent_root);
  7436. }
  7437. kvfree(clone_sources_tmp);
  7438. if (sctx) {
  7439. if (sctx->send_filp)
  7440. fput(sctx->send_filp);
  7441. kvfree(sctx->clone_roots);
  7442. kfree(sctx->send_buf_pages);
  7443. kvfree(sctx->send_buf);
  7444. kvfree(sctx->verity_descriptor);
  7445. close_current_inode(sctx);
  7446. btrfs_lru_cache_clear(&sctx->name_cache);
  7447. btrfs_lru_cache_clear(&sctx->backref_cache);
  7448. btrfs_lru_cache_clear(&sctx->dir_created_cache);
  7449. btrfs_lru_cache_clear(&sctx->dir_utimes_cache);
  7450. if (sctx->cur_inode_path.buf != sctx->cur_inode_path.inline_buf)
  7451. kfree(sctx->cur_inode_path.buf);
  7452. kfree(sctx);
  7453. }
  7454. return ret;
  7455. }