send.c 160 KB

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