send.c 182 KB

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