send.c 160 KB

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