send.c 148 KB

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