1f169c0eaSGlenn Lagasse /* 2f169c0eaSGlenn Lagasse * CDDL HEADER START 3f169c0eaSGlenn Lagasse * 4f169c0eaSGlenn Lagasse * The contents of this file are subject to the terms of the 5f169c0eaSGlenn Lagasse * Common Development and Distribution License (the "License"). 6f169c0eaSGlenn Lagasse * You may not use this file except in compliance with the License. 7f169c0eaSGlenn Lagasse * 8f169c0eaSGlenn Lagasse * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9f169c0eaSGlenn Lagasse * or http://www.opensolaris.org/os/licensing. 10f169c0eaSGlenn Lagasse * See the License for the specific language governing permissions 11f169c0eaSGlenn Lagasse * and limitations under the License. 12f169c0eaSGlenn Lagasse * 13f169c0eaSGlenn Lagasse * When distributing Covered Code, include this CDDL HEADER in each 14f169c0eaSGlenn Lagasse * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15f169c0eaSGlenn Lagasse * If applicable, add the following below this CDDL HEADER, with the 16f169c0eaSGlenn Lagasse * fields enclosed by brackets "[]" replaced with your own identifying 17f169c0eaSGlenn Lagasse * information: Portions Copyright [yyyy] [name of copyright owner] 18f169c0eaSGlenn Lagasse * 19f169c0eaSGlenn Lagasse * CDDL HEADER END 20f169c0eaSGlenn Lagasse */ 21f169c0eaSGlenn Lagasse 22f169c0eaSGlenn Lagasse /* 23f169c0eaSGlenn Lagasse * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. 247e0e2549SAlexander Eremin * Copyright 2013 Nexenta Systems, Inc. All rights reserved. 25*9adfa60dSMatthew Ahrens * Copyright (c) 2014, 2015 by Delphix. All rights reserved. 260d8fa8f8SMartin Matuska * Copyright (c) 2016 Martin Matuska. All rights reserved. 27de1ab35cSAlexander Eremin */ 28de1ab35cSAlexander Eremin 29f169c0eaSGlenn Lagasse /* 30f169c0eaSGlenn Lagasse * System includes 31f169c0eaSGlenn Lagasse */ 32f169c0eaSGlenn Lagasse 33f169c0eaSGlenn Lagasse #include <assert.h> 34f169c0eaSGlenn Lagasse #include <ctype.h> 35f169c0eaSGlenn Lagasse #include <errno.h> 36f169c0eaSGlenn Lagasse #include <libgen.h> 37f169c0eaSGlenn Lagasse #include <libintl.h> 38f169c0eaSGlenn Lagasse #include <libnvpair.h> 39f169c0eaSGlenn Lagasse #include <libzfs.h> 40f169c0eaSGlenn Lagasse #include <stdio.h> 41f169c0eaSGlenn Lagasse #include <stdlib.h> 42f169c0eaSGlenn Lagasse #include <string.h> 43f169c0eaSGlenn Lagasse #include <sys/mnttab.h> 44f169c0eaSGlenn Lagasse #include <sys/mount.h> 45f169c0eaSGlenn Lagasse #include <sys/stat.h> 46f169c0eaSGlenn Lagasse #include <sys/types.h> 47f169c0eaSGlenn Lagasse #include <sys/wait.h> 48f169c0eaSGlenn Lagasse #include <unistd.h> 49f169c0eaSGlenn Lagasse 50f169c0eaSGlenn Lagasse #include <libbe.h> 51f169c0eaSGlenn Lagasse #include <libbe_priv.h> 52f169c0eaSGlenn Lagasse 53f169c0eaSGlenn Lagasse /* Library wide variables */ 54f169c0eaSGlenn Lagasse libzfs_handle_t *g_zfs = NULL; 55f169c0eaSGlenn Lagasse 56f169c0eaSGlenn Lagasse /* Private function prototypes */ 57f169c0eaSGlenn Lagasse static int _be_destroy(const char *, be_destroy_data_t *); 58f169c0eaSGlenn Lagasse static int be_destroy_zones(char *, char *, be_destroy_data_t *); 59f169c0eaSGlenn Lagasse static int be_destroy_zone_roots(char *, be_destroy_data_t *); 60f169c0eaSGlenn Lagasse static int be_destroy_zone_roots_callback(zfs_handle_t *, void *); 61f169c0eaSGlenn Lagasse static int be_copy_zones(char *, char *, char *); 62f169c0eaSGlenn Lagasse static int be_clone_fs_callback(zfs_handle_t *, void *); 63f169c0eaSGlenn Lagasse static int be_destroy_callback(zfs_handle_t *, void *); 64f169c0eaSGlenn Lagasse static int be_send_fs_callback(zfs_handle_t *, void *); 65f169c0eaSGlenn Lagasse static int be_demote_callback(zfs_handle_t *, void *); 66f169c0eaSGlenn Lagasse static int be_demote_find_clone_callback(zfs_handle_t *, void *); 675ee7c793SAlexander Eremin static int be_has_snapshot_callback(zfs_handle_t *, void *); 68f169c0eaSGlenn Lagasse static int be_demote_get_one_clone(zfs_handle_t *, void *); 69f169c0eaSGlenn Lagasse static int be_get_snap(char *, char **); 70f169c0eaSGlenn Lagasse static int be_prep_clone_send_fs(zfs_handle_t *, be_transaction_data_t *, 71f169c0eaSGlenn Lagasse char *, int); 72f169c0eaSGlenn Lagasse static boolean_t be_create_container_ds(char *); 73f169c0eaSGlenn Lagasse static char *be_get_zone_be_name(char *root_ds, char *container_ds); 74f169c0eaSGlenn Lagasse static int be_zone_root_exists_callback(zfs_handle_t *, void *); 75f169c0eaSGlenn Lagasse 76f169c0eaSGlenn Lagasse /* ******************************************************************** */ 77f169c0eaSGlenn Lagasse /* Public Functions */ 78f169c0eaSGlenn Lagasse /* ******************************************************************** */ 79f169c0eaSGlenn Lagasse 80f169c0eaSGlenn Lagasse /* 81f169c0eaSGlenn Lagasse * Function: be_init 82f169c0eaSGlenn Lagasse * Description: Creates the initial datasets for a BE and leaves them 83f169c0eaSGlenn Lagasse * unpopulated. The resultant BE can be mounted but can't 84f169c0eaSGlenn Lagasse * yet be activated or booted. 85f169c0eaSGlenn Lagasse * Parameters: 86f169c0eaSGlenn Lagasse * be_attrs - pointer to nvlist_t of attributes being passed in. 87f169c0eaSGlenn Lagasse * The following attributes are used by this function: 88f169c0eaSGlenn Lagasse * 89f169c0eaSGlenn Lagasse * BE_ATTR_NEW_BE_NAME *required 90f169c0eaSGlenn Lagasse * BE_ATTR_NEW_BE_POOL *required 91f169c0eaSGlenn Lagasse * BE_ATTR_ZFS_PROPERTIES *optional 92f169c0eaSGlenn Lagasse * BE_ATTR_FS_NAMES *optional 93f169c0eaSGlenn Lagasse * BE_ATTR_FS_NUM *optional 94f169c0eaSGlenn Lagasse * BE_ATTR_SHARED_FS_NAMES *optional 95f169c0eaSGlenn Lagasse * BE_ATTR_SHARED_FS_NUM *optional 96f169c0eaSGlenn Lagasse * Return: 97f169c0eaSGlenn Lagasse * BE_SUCCESS - Success 98f169c0eaSGlenn Lagasse * be_errno_t - Failure 99f169c0eaSGlenn Lagasse * Scope: 100f169c0eaSGlenn Lagasse * Public 101f169c0eaSGlenn Lagasse */ 102f169c0eaSGlenn Lagasse int 103f169c0eaSGlenn Lagasse be_init(nvlist_t *be_attrs) 104f169c0eaSGlenn Lagasse { 105f169c0eaSGlenn Lagasse be_transaction_data_t bt = { 0 }; 106f169c0eaSGlenn Lagasse zpool_handle_t *zlp; 107f169c0eaSGlenn Lagasse nvlist_t *zfs_props = NULL; 108f169c0eaSGlenn Lagasse char nbe_root_ds[MAXPATHLEN]; 109f169c0eaSGlenn Lagasse char child_fs[MAXPATHLEN]; 110f169c0eaSGlenn Lagasse char **fs_names = NULL; 111f169c0eaSGlenn Lagasse char **shared_fs_names = NULL; 112f169c0eaSGlenn Lagasse uint16_t fs_num = 0; 113f169c0eaSGlenn Lagasse uint16_t shared_fs_num = 0; 114f169c0eaSGlenn Lagasse int nelem; 115f169c0eaSGlenn Lagasse int i; 116f169c0eaSGlenn Lagasse int zret = 0, ret = BE_SUCCESS; 117f169c0eaSGlenn Lagasse 118f169c0eaSGlenn Lagasse /* Initialize libzfs handle */ 119f169c0eaSGlenn Lagasse if (!be_zfs_init()) 120f169c0eaSGlenn Lagasse return (BE_ERR_INIT); 121f169c0eaSGlenn Lagasse 122f169c0eaSGlenn Lagasse /* Get new BE name */ 123f169c0eaSGlenn Lagasse if (nvlist_lookup_string(be_attrs, BE_ATTR_NEW_BE_NAME, &bt.nbe_name) 124f169c0eaSGlenn Lagasse != 0) { 125f169c0eaSGlenn Lagasse be_print_err(gettext("be_init: failed to lookup " 126f169c0eaSGlenn Lagasse "BE_ATTR_NEW_BE_NAME attribute\n")); 127f169c0eaSGlenn Lagasse return (BE_ERR_INVAL); 128f169c0eaSGlenn Lagasse } 129f169c0eaSGlenn Lagasse 130f169c0eaSGlenn Lagasse /* Validate new BE name */ 131f169c0eaSGlenn Lagasse if (!be_valid_be_name(bt.nbe_name)) { 132f169c0eaSGlenn Lagasse be_print_err(gettext("be_init: invalid BE name %s\n"), 133f169c0eaSGlenn Lagasse bt.nbe_name); 134f169c0eaSGlenn Lagasse return (BE_ERR_INVAL); 135f169c0eaSGlenn Lagasse } 136f169c0eaSGlenn Lagasse 137f169c0eaSGlenn Lagasse /* Get zpool name */ 138f169c0eaSGlenn Lagasse if (nvlist_lookup_string(be_attrs, BE_ATTR_NEW_BE_POOL, &bt.nbe_zpool) 139f169c0eaSGlenn Lagasse != 0) { 140f169c0eaSGlenn Lagasse be_print_err(gettext("be_init: failed to lookup " 141f169c0eaSGlenn Lagasse "BE_ATTR_NEW_BE_POOL attribute\n")); 142f169c0eaSGlenn Lagasse return (BE_ERR_INVAL); 143f169c0eaSGlenn Lagasse } 144f169c0eaSGlenn Lagasse 145f169c0eaSGlenn Lagasse /* Get file system attributes */ 146f169c0eaSGlenn Lagasse nelem = 0; 147f169c0eaSGlenn Lagasse if (nvlist_lookup_pairs(be_attrs, 0, 148f169c0eaSGlenn Lagasse BE_ATTR_FS_NUM, DATA_TYPE_UINT16, &fs_num, 149f169c0eaSGlenn Lagasse BE_ATTR_FS_NAMES, DATA_TYPE_STRING_ARRAY, &fs_names, &nelem, 150f169c0eaSGlenn Lagasse NULL) != 0) { 151f169c0eaSGlenn Lagasse be_print_err(gettext("be_init: failed to lookup fs " 152f169c0eaSGlenn Lagasse "attributes\n")); 153f169c0eaSGlenn Lagasse return (BE_ERR_INVAL); 154f169c0eaSGlenn Lagasse } 155f169c0eaSGlenn Lagasse if (nelem != fs_num) { 156f169c0eaSGlenn Lagasse be_print_err(gettext("be_init: size of FS_NAMES array (%d) " 157f169c0eaSGlenn Lagasse "does not match FS_NUM (%d)\n"), nelem, fs_num); 158f169c0eaSGlenn Lagasse return (BE_ERR_INVAL); 159f169c0eaSGlenn Lagasse } 160f169c0eaSGlenn Lagasse 161f169c0eaSGlenn Lagasse /* Get shared file system attributes */ 162f169c0eaSGlenn Lagasse nelem = 0; 163f169c0eaSGlenn Lagasse if (nvlist_lookup_pairs(be_attrs, NV_FLAG_NOENTOK, 164f169c0eaSGlenn Lagasse BE_ATTR_SHARED_FS_NUM, DATA_TYPE_UINT16, &shared_fs_num, 165f169c0eaSGlenn Lagasse BE_ATTR_SHARED_FS_NAMES, DATA_TYPE_STRING_ARRAY, &shared_fs_names, 166f169c0eaSGlenn Lagasse &nelem, NULL) != 0) { 167f169c0eaSGlenn Lagasse be_print_err(gettext("be_init: failed to lookup " 168f169c0eaSGlenn Lagasse "shared fs attributes\n")); 169f169c0eaSGlenn Lagasse return (BE_ERR_INVAL); 170f169c0eaSGlenn Lagasse } 171f169c0eaSGlenn Lagasse if (nelem != shared_fs_num) { 172f169c0eaSGlenn Lagasse be_print_err(gettext("be_init: size of SHARED_FS_NAMES " 173f169c0eaSGlenn Lagasse "array does not match SHARED_FS_NUM\n")); 174f169c0eaSGlenn Lagasse return (BE_ERR_INVAL); 175f169c0eaSGlenn Lagasse } 176f169c0eaSGlenn Lagasse 177f169c0eaSGlenn Lagasse /* Verify that nbe_zpool exists */ 178f169c0eaSGlenn Lagasse if ((zlp = zpool_open(g_zfs, bt.nbe_zpool)) == NULL) { 179f169c0eaSGlenn Lagasse be_print_err(gettext("be_init: failed to " 180f169c0eaSGlenn Lagasse "find existing zpool (%s): %s\n"), bt.nbe_zpool, 181f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs)); 182f169c0eaSGlenn Lagasse return (zfs_err_to_be_err(g_zfs)); 183f169c0eaSGlenn Lagasse } 184f169c0eaSGlenn Lagasse zpool_close(zlp); 185f169c0eaSGlenn Lagasse 186f169c0eaSGlenn Lagasse /* 187f169c0eaSGlenn Lagasse * Verify BE container dataset in nbe_zpool exists. 188f169c0eaSGlenn Lagasse * If not, create it. 189f169c0eaSGlenn Lagasse */ 190f169c0eaSGlenn Lagasse if (!be_create_container_ds(bt.nbe_zpool)) 191f169c0eaSGlenn Lagasse return (BE_ERR_CREATDS); 192f169c0eaSGlenn Lagasse 193f169c0eaSGlenn Lagasse /* 194f169c0eaSGlenn Lagasse * Verify that nbe_name doesn't already exist in some pool. 195f169c0eaSGlenn Lagasse */ 196f169c0eaSGlenn Lagasse if ((zret = zpool_iter(g_zfs, be_exists_callback, bt.nbe_name)) > 0) { 197f169c0eaSGlenn Lagasse be_print_err(gettext("be_init: BE (%s) already exists\n"), 198f169c0eaSGlenn Lagasse bt.nbe_name); 199f169c0eaSGlenn Lagasse return (BE_ERR_BE_EXISTS); 200f169c0eaSGlenn Lagasse } else if (zret < 0) { 201f169c0eaSGlenn Lagasse be_print_err(gettext("be_init: zpool_iter failed: %s\n"), 202f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs)); 203f169c0eaSGlenn Lagasse return (zfs_err_to_be_err(g_zfs)); 204f169c0eaSGlenn Lagasse } 205f169c0eaSGlenn Lagasse 206f169c0eaSGlenn Lagasse /* Generate string for BE's root dataset */ 207f169c0eaSGlenn Lagasse be_make_root_ds(bt.nbe_zpool, bt.nbe_name, nbe_root_ds, 208f169c0eaSGlenn Lagasse sizeof (nbe_root_ds)); 209f169c0eaSGlenn Lagasse 210f169c0eaSGlenn Lagasse /* 211f169c0eaSGlenn Lagasse * Create property list for new BE root dataset. If some 212f169c0eaSGlenn Lagasse * zfs properties were already provided by the caller, dup 213f169c0eaSGlenn Lagasse * that list. Otherwise initialize a new property list. 214f169c0eaSGlenn Lagasse */ 215f169c0eaSGlenn Lagasse if (nvlist_lookup_pairs(be_attrs, NV_FLAG_NOENTOK, 216f169c0eaSGlenn Lagasse BE_ATTR_ZFS_PROPERTIES, DATA_TYPE_NVLIST, &zfs_props, NULL) 217f169c0eaSGlenn Lagasse != 0) { 218f169c0eaSGlenn Lagasse be_print_err(gettext("be_init: failed to lookup " 219f169c0eaSGlenn Lagasse "BE_ATTR_ZFS_PROPERTIES attribute\n")); 220f169c0eaSGlenn Lagasse return (BE_ERR_INVAL); 221f169c0eaSGlenn Lagasse } 222f169c0eaSGlenn Lagasse if (zfs_props != NULL) { 223f169c0eaSGlenn Lagasse /* Make sure its a unique nvlist */ 224f169c0eaSGlenn Lagasse if (!(zfs_props->nvl_nvflag & NV_UNIQUE_NAME) && 225f169c0eaSGlenn Lagasse !(zfs_props->nvl_nvflag & NV_UNIQUE_NAME_TYPE)) { 226f169c0eaSGlenn Lagasse be_print_err(gettext("be_init: ZFS property list " 227f169c0eaSGlenn Lagasse "not unique\n")); 228f169c0eaSGlenn Lagasse return (BE_ERR_INVAL); 229f169c0eaSGlenn Lagasse } 230f169c0eaSGlenn Lagasse 231f169c0eaSGlenn Lagasse /* Dup the list */ 232f169c0eaSGlenn Lagasse if (nvlist_dup(zfs_props, &bt.nbe_zfs_props, 0) != 0) { 233f169c0eaSGlenn Lagasse be_print_err(gettext("be_init: failed to dup ZFS " 234f169c0eaSGlenn Lagasse "property list\n")); 235f169c0eaSGlenn Lagasse return (BE_ERR_NOMEM); 236f169c0eaSGlenn Lagasse } 237f169c0eaSGlenn Lagasse } else { 238f169c0eaSGlenn Lagasse /* Initialize new nvlist */ 239f169c0eaSGlenn Lagasse if (nvlist_alloc(&bt.nbe_zfs_props, NV_UNIQUE_NAME, 0) != 0) { 240f169c0eaSGlenn Lagasse be_print_err(gettext("be_init: internal " 241f169c0eaSGlenn Lagasse "error: out of memory\n")); 242f169c0eaSGlenn Lagasse return (BE_ERR_NOMEM); 243f169c0eaSGlenn Lagasse } 244f169c0eaSGlenn Lagasse } 245f169c0eaSGlenn Lagasse 246f169c0eaSGlenn Lagasse /* Set the mountpoint property for the root dataset */ 247f169c0eaSGlenn Lagasse if (nvlist_add_string(bt.nbe_zfs_props, 248f169c0eaSGlenn Lagasse zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), "/") != 0) { 249f169c0eaSGlenn Lagasse be_print_err(gettext("be_init: internal error " 250f169c0eaSGlenn Lagasse "out of memory\n")); 251f169c0eaSGlenn Lagasse ret = BE_ERR_NOMEM; 252f169c0eaSGlenn Lagasse goto done; 253f169c0eaSGlenn Lagasse } 254f169c0eaSGlenn Lagasse 255f169c0eaSGlenn Lagasse /* Set the 'canmount' property */ 256f169c0eaSGlenn Lagasse if (nvlist_add_string(bt.nbe_zfs_props, 257f169c0eaSGlenn Lagasse zfs_prop_to_name(ZFS_PROP_CANMOUNT), "noauto") != 0) { 258f169c0eaSGlenn Lagasse be_print_err(gettext("be_init: internal error " 259f169c0eaSGlenn Lagasse "out of memory\n")); 260f169c0eaSGlenn Lagasse ret = BE_ERR_NOMEM; 261f169c0eaSGlenn Lagasse goto done; 262f169c0eaSGlenn Lagasse } 263f169c0eaSGlenn Lagasse 264f169c0eaSGlenn Lagasse /* Create BE root dataset for the new BE */ 265f169c0eaSGlenn Lagasse if (zfs_create(g_zfs, nbe_root_ds, ZFS_TYPE_FILESYSTEM, 266f169c0eaSGlenn Lagasse bt.nbe_zfs_props) != 0) { 267f169c0eaSGlenn Lagasse be_print_err(gettext("be_init: failed to " 268f169c0eaSGlenn Lagasse "create BE root dataset (%s): %s\n"), nbe_root_ds, 269f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs)); 270f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs); 271f169c0eaSGlenn Lagasse goto done; 272f169c0eaSGlenn Lagasse } 273f169c0eaSGlenn Lagasse 274f169c0eaSGlenn Lagasse /* Set UUID for new BE */ 275f169c0eaSGlenn Lagasse if ((ret = be_set_uuid(nbe_root_ds)) != BE_SUCCESS) { 276f169c0eaSGlenn Lagasse be_print_err(gettext("be_init: failed to " 277f169c0eaSGlenn Lagasse "set uuid for new BE\n")); 278f169c0eaSGlenn Lagasse } 279f169c0eaSGlenn Lagasse 280f169c0eaSGlenn Lagasse /* 281f169c0eaSGlenn Lagasse * Clear the mountpoint property so that the non-shared 282f169c0eaSGlenn Lagasse * file systems created below inherit their mountpoints. 283f169c0eaSGlenn Lagasse */ 284f169c0eaSGlenn Lagasse (void) nvlist_remove(bt.nbe_zfs_props, 285f169c0eaSGlenn Lagasse zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), DATA_TYPE_STRING); 286f169c0eaSGlenn Lagasse 287f169c0eaSGlenn Lagasse /* Create the new BE's non-shared file systems */ 288f169c0eaSGlenn Lagasse for (i = 0; i < fs_num && fs_names[i]; i++) { 289f169c0eaSGlenn Lagasse /* 290f169c0eaSGlenn Lagasse * If fs == "/", skip it; 291f169c0eaSGlenn Lagasse * we already created the root dataset 292f169c0eaSGlenn Lagasse */ 293f169c0eaSGlenn Lagasse if (strcmp(fs_names[i], "/") == 0) 294f169c0eaSGlenn Lagasse continue; 295f169c0eaSGlenn Lagasse 296f169c0eaSGlenn Lagasse /* Generate string for file system */ 297f169c0eaSGlenn Lagasse (void) snprintf(child_fs, sizeof (child_fs), "%s%s", 298f169c0eaSGlenn Lagasse nbe_root_ds, fs_names[i]); 299f169c0eaSGlenn Lagasse 300f169c0eaSGlenn Lagasse /* Create file system */ 301f169c0eaSGlenn Lagasse if (zfs_create(g_zfs, child_fs, ZFS_TYPE_FILESYSTEM, 302f169c0eaSGlenn Lagasse bt.nbe_zfs_props) != 0) { 303f169c0eaSGlenn Lagasse be_print_err(gettext("be_init: failed to create " 304f169c0eaSGlenn Lagasse "BE's child dataset (%s): %s\n"), child_fs, 305f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs)); 306f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs); 307f169c0eaSGlenn Lagasse goto done; 308f169c0eaSGlenn Lagasse } 309f169c0eaSGlenn Lagasse } 310f169c0eaSGlenn Lagasse 311f169c0eaSGlenn Lagasse /* Create the new BE's shared file systems */ 312f169c0eaSGlenn Lagasse if (shared_fs_num > 0) { 313f169c0eaSGlenn Lagasse nvlist_t *props = NULL; 314f169c0eaSGlenn Lagasse 315f169c0eaSGlenn Lagasse if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) { 316f169c0eaSGlenn Lagasse be_print_err(gettext("be_init: nvlist_alloc failed\n")); 317f169c0eaSGlenn Lagasse ret = BE_ERR_NOMEM; 318f169c0eaSGlenn Lagasse goto done; 319f169c0eaSGlenn Lagasse } 320f169c0eaSGlenn Lagasse 321f169c0eaSGlenn Lagasse for (i = 0; i < shared_fs_num; i++) { 322f169c0eaSGlenn Lagasse /* Generate string for shared file system */ 323f169c0eaSGlenn Lagasse (void) snprintf(child_fs, sizeof (child_fs), "%s%s", 324f169c0eaSGlenn Lagasse bt.nbe_zpool, shared_fs_names[i]); 325f169c0eaSGlenn Lagasse 326f169c0eaSGlenn Lagasse if (nvlist_add_string(props, 327f169c0eaSGlenn Lagasse zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), 328f169c0eaSGlenn Lagasse shared_fs_names[i]) != 0) { 329f169c0eaSGlenn Lagasse be_print_err(gettext("be_init: " 330f169c0eaSGlenn Lagasse "internal error: out of memory\n")); 331f169c0eaSGlenn Lagasse nvlist_free(props); 332f169c0eaSGlenn Lagasse ret = BE_ERR_NOMEM; 333f169c0eaSGlenn Lagasse goto done; 334f169c0eaSGlenn Lagasse } 335f169c0eaSGlenn Lagasse 336f169c0eaSGlenn Lagasse /* Create file system if it doesn't already exist */ 337f169c0eaSGlenn Lagasse if (zfs_dataset_exists(g_zfs, child_fs, 338f169c0eaSGlenn Lagasse ZFS_TYPE_FILESYSTEM)) { 339f169c0eaSGlenn Lagasse continue; 340f169c0eaSGlenn Lagasse } 341f169c0eaSGlenn Lagasse if (zfs_create(g_zfs, child_fs, ZFS_TYPE_FILESYSTEM, 342f169c0eaSGlenn Lagasse props) != 0) { 343f169c0eaSGlenn Lagasse be_print_err(gettext("be_init: failed to " 344f169c0eaSGlenn Lagasse "create BE's shared dataset (%s): %s\n"), 345f169c0eaSGlenn Lagasse child_fs, libzfs_error_description(g_zfs)); 346f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs); 347f169c0eaSGlenn Lagasse nvlist_free(props); 348f169c0eaSGlenn Lagasse goto done; 349f169c0eaSGlenn Lagasse } 350f169c0eaSGlenn Lagasse } 351f169c0eaSGlenn Lagasse 352f169c0eaSGlenn Lagasse nvlist_free(props); 353f169c0eaSGlenn Lagasse } 354f169c0eaSGlenn Lagasse 355f169c0eaSGlenn Lagasse done: 356aab83bb8SJosef 'Jeff' Sipek nvlist_free(bt.nbe_zfs_props); 357f169c0eaSGlenn Lagasse 358f169c0eaSGlenn Lagasse be_zfs_fini(); 359f169c0eaSGlenn Lagasse 360f169c0eaSGlenn Lagasse return (ret); 361f169c0eaSGlenn Lagasse } 362f169c0eaSGlenn Lagasse 363f169c0eaSGlenn Lagasse /* 364f169c0eaSGlenn Lagasse * Function: be_destroy 365f169c0eaSGlenn Lagasse * Description: Destroy a BE and all of its children datasets, snapshots and 366f169c0eaSGlenn Lagasse * zones that belong to the parent BE. 367f169c0eaSGlenn Lagasse * Parameters: 368f169c0eaSGlenn Lagasse * be_attrs - pointer to nvlist_t of attributes being passed in. 369f169c0eaSGlenn Lagasse * The following attributes are used by this function: 370f169c0eaSGlenn Lagasse * 371f169c0eaSGlenn Lagasse * BE_ATTR_ORIG_BE_NAME *required 372f169c0eaSGlenn Lagasse * BE_ATTR_DESTROY_FLAGS *optional 373f169c0eaSGlenn Lagasse * Return: 374f169c0eaSGlenn Lagasse * BE_SUCCESS - Success 375f169c0eaSGlenn Lagasse * be_errno_t - Failure 376f169c0eaSGlenn Lagasse * Scope: 377f169c0eaSGlenn Lagasse * Public 378f169c0eaSGlenn Lagasse */ 379f169c0eaSGlenn Lagasse int 380f169c0eaSGlenn Lagasse be_destroy(nvlist_t *be_attrs) 381f169c0eaSGlenn Lagasse { 382f169c0eaSGlenn Lagasse zfs_handle_t *zhp = NULL; 383f169c0eaSGlenn Lagasse be_transaction_data_t bt = { 0 }; 384f169c0eaSGlenn Lagasse be_transaction_data_t cur_bt = { 0 }; 385f169c0eaSGlenn Lagasse be_destroy_data_t dd = { 0 }; 386f169c0eaSGlenn Lagasse int ret = BE_SUCCESS; 387f169c0eaSGlenn Lagasse uint16_t flags = 0; 3885ee7c793SAlexander Eremin boolean_t bs_found = B_FALSE; 389f169c0eaSGlenn Lagasse int zret; 390f169c0eaSGlenn Lagasse char obe_root_ds[MAXPATHLEN]; 391f169c0eaSGlenn Lagasse char *mp = NULL; 392f169c0eaSGlenn Lagasse 393f169c0eaSGlenn Lagasse /* Initialize libzfs handle */ 394f169c0eaSGlenn Lagasse if (!be_zfs_init()) 395f169c0eaSGlenn Lagasse return (BE_ERR_INIT); 396f169c0eaSGlenn Lagasse 397f169c0eaSGlenn Lagasse /* Get name of BE to delete */ 398f169c0eaSGlenn Lagasse if (nvlist_lookup_string(be_attrs, BE_ATTR_ORIG_BE_NAME, &bt.obe_name) 399f169c0eaSGlenn Lagasse != 0) { 400f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy: failed to lookup " 401f169c0eaSGlenn Lagasse "BE_ATTR_ORIG_BE_NAME attribute\n")); 402f169c0eaSGlenn Lagasse return (BE_ERR_INVAL); 403f169c0eaSGlenn Lagasse } 404f169c0eaSGlenn Lagasse 405f169c0eaSGlenn Lagasse /* 406f169c0eaSGlenn Lagasse * Validate BE name. If valid, then check that the original BE is not 407f169c0eaSGlenn Lagasse * the active BE. If it is the 'active' BE then return an error code 408f169c0eaSGlenn Lagasse * since we can't destroy the active BE. 409f169c0eaSGlenn Lagasse */ 410f169c0eaSGlenn Lagasse if (!be_valid_be_name(bt.obe_name)) { 411f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy: invalid BE name %s\n"), 412f169c0eaSGlenn Lagasse bt.obe_name); 413f169c0eaSGlenn Lagasse return (BE_ERR_INVAL); 414f169c0eaSGlenn Lagasse } else if (bt.obe_name != NULL) { 415f169c0eaSGlenn Lagasse if ((ret = be_find_current_be(&cur_bt)) != BE_SUCCESS) { 416f169c0eaSGlenn Lagasse return (ret); 417f169c0eaSGlenn Lagasse } 418f169c0eaSGlenn Lagasse if (strcmp(cur_bt.obe_name, bt.obe_name) == 0) { 419f169c0eaSGlenn Lagasse return (BE_ERR_DESTROY_CURR_BE); 420f169c0eaSGlenn Lagasse } 421f169c0eaSGlenn Lagasse } 422f169c0eaSGlenn Lagasse 423f169c0eaSGlenn Lagasse /* Get destroy flags if provided */ 424f169c0eaSGlenn Lagasse if (nvlist_lookup_pairs(be_attrs, NV_FLAG_NOENTOK, 425f169c0eaSGlenn Lagasse BE_ATTR_DESTROY_FLAGS, DATA_TYPE_UINT16, &flags, NULL) 426f169c0eaSGlenn Lagasse != 0) { 427f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy: failed to lookup " 428f169c0eaSGlenn Lagasse "BE_ATTR_DESTROY_FLAGS attribute\n")); 429f169c0eaSGlenn Lagasse return (BE_ERR_INVAL); 430f169c0eaSGlenn Lagasse } 431f169c0eaSGlenn Lagasse 432f169c0eaSGlenn Lagasse dd.destroy_snaps = flags & BE_DESTROY_FLAG_SNAPSHOTS; 433f169c0eaSGlenn Lagasse dd.force_unmount = flags & BE_DESTROY_FLAG_FORCE_UNMOUNT; 434f169c0eaSGlenn Lagasse 435f169c0eaSGlenn Lagasse /* Find which zpool obe_name lives in */ 436f169c0eaSGlenn Lagasse if ((zret = zpool_iter(g_zfs, be_find_zpool_callback, &bt)) == 0) { 437f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy: failed to find zpool " 438f169c0eaSGlenn Lagasse "for BE (%s)\n"), bt.obe_name); 439f169c0eaSGlenn Lagasse return (BE_ERR_BE_NOENT); 440f169c0eaSGlenn Lagasse } else if (zret < 0) { 441f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy: zpool_iter failed: %s\n"), 442f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs)); 443f169c0eaSGlenn Lagasse return (zfs_err_to_be_err(g_zfs)); 444f169c0eaSGlenn Lagasse } 445f169c0eaSGlenn Lagasse 446f169c0eaSGlenn Lagasse /* Generate string for obe_name's root dataset */ 447f169c0eaSGlenn Lagasse be_make_root_ds(bt.obe_zpool, bt.obe_name, obe_root_ds, 448f169c0eaSGlenn Lagasse sizeof (obe_root_ds)); 449f169c0eaSGlenn Lagasse bt.obe_root_ds = obe_root_ds; 450f169c0eaSGlenn Lagasse 4517e0e2549SAlexander Eremin if (getzoneid() != GLOBAL_ZONEID) { 4527e0e2549SAlexander Eremin if (!be_zone_compare_uuids(bt.obe_root_ds)) { 4537e0e2549SAlexander Eremin if (be_is_active_on_boot(bt.obe_name)) { 4547e0e2549SAlexander Eremin be_print_err(gettext("be_destroy: destroying " 4557e0e2549SAlexander Eremin "active zone root dataset from non-active " 4567e0e2549SAlexander Eremin "global BE is not supported\n")); 4577e0e2549SAlexander Eremin return (BE_ERR_NOTSUP); 4587e0e2549SAlexander Eremin } 4597e0e2549SAlexander Eremin } 4607e0e2549SAlexander Eremin } 4617e0e2549SAlexander Eremin 462f169c0eaSGlenn Lagasse /* 463f169c0eaSGlenn Lagasse * Detect if the BE to destroy has the 'active on boot' property set. 464f169c0eaSGlenn Lagasse * If so, set the 'active on boot' property on the the 'active' BE. 465f169c0eaSGlenn Lagasse */ 466f169c0eaSGlenn Lagasse if (be_is_active_on_boot(bt.obe_name)) { 467f169c0eaSGlenn Lagasse if ((ret = be_activate_current_be()) != BE_SUCCESS) { 468f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy: failed to " 469f169c0eaSGlenn Lagasse "make the current BE 'active on boot'\n")); 470f169c0eaSGlenn Lagasse return (ret); 471f169c0eaSGlenn Lagasse } 472f169c0eaSGlenn Lagasse } 473f169c0eaSGlenn Lagasse 474f169c0eaSGlenn Lagasse /* Get handle to BE's root dataset */ 475f169c0eaSGlenn Lagasse if ((zhp = zfs_open(g_zfs, bt.obe_root_ds, ZFS_TYPE_FILESYSTEM)) == 476f169c0eaSGlenn Lagasse NULL) { 477f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy: failed to " 478f169c0eaSGlenn Lagasse "open BE root dataset (%s): %s\n"), bt.obe_root_ds, 479f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs)); 480f169c0eaSGlenn Lagasse return (zfs_err_to_be_err(g_zfs)); 481f169c0eaSGlenn Lagasse } 482f169c0eaSGlenn Lagasse 4835ee7c793SAlexander Eremin /* 4845ee7c793SAlexander Eremin * Check if BE has snapshots and BE_DESTROY_FLAG_SNAPSHOTS 4855ee7c793SAlexander Eremin * is not set. 4865ee7c793SAlexander Eremin */ 4870d8fa8f8SMartin Matuska (void) zfs_iter_snapshots(zhp, B_FALSE, be_has_snapshot_callback, 4880d8fa8f8SMartin Matuska &bs_found); 4895ee7c793SAlexander Eremin if (!dd.destroy_snaps && bs_found) { 4905ee7c793SAlexander Eremin ZFS_CLOSE(zhp); 4915ee7c793SAlexander Eremin return (BE_ERR_SS_EXISTS); 4925ee7c793SAlexander Eremin } 4935ee7c793SAlexander Eremin 494f169c0eaSGlenn Lagasse /* Get the UUID of the global BE */ 4957e0e2549SAlexander Eremin if (getzoneid() == GLOBAL_ZONEID) { 4967e0e2549SAlexander Eremin if (be_get_uuid(zfs_get_name(zhp), 4977e0e2549SAlexander Eremin &dd.gz_be_uuid) != BE_SUCCESS) { 4987e0e2549SAlexander Eremin be_print_err(gettext("be_destroy: BE has no " 4997e0e2549SAlexander Eremin "UUID (%s)\n"), zfs_get_name(zhp)); 5007e0e2549SAlexander Eremin } 501f169c0eaSGlenn Lagasse } 502f169c0eaSGlenn Lagasse 503f169c0eaSGlenn Lagasse /* 504f169c0eaSGlenn Lagasse * If the global BE is mounted, make sure we've been given the 505f169c0eaSGlenn Lagasse * flag to forcibly unmount it. 506f169c0eaSGlenn Lagasse */ 507f169c0eaSGlenn Lagasse if (zfs_is_mounted(zhp, &mp)) { 508f169c0eaSGlenn Lagasse if (!(dd.force_unmount)) { 509f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy: " 510f169c0eaSGlenn Lagasse "%s is currently mounted at %s, cannot destroy\n"), 511f169c0eaSGlenn Lagasse bt.obe_name, mp != NULL ? mp : "<unknown>"); 512f169c0eaSGlenn Lagasse 513f169c0eaSGlenn Lagasse free(mp); 514f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 515f169c0eaSGlenn Lagasse return (BE_ERR_MOUNTED); 516f169c0eaSGlenn Lagasse } 517f169c0eaSGlenn Lagasse free(mp); 518f169c0eaSGlenn Lagasse } 519f169c0eaSGlenn Lagasse 520f169c0eaSGlenn Lagasse /* 521f169c0eaSGlenn Lagasse * Destroy the non-global zone BE's if we are in the global zone 522f169c0eaSGlenn Lagasse * and there is a UUID associated with the global zone BE 523f169c0eaSGlenn Lagasse */ 524f169c0eaSGlenn Lagasse if (getzoneid() == GLOBAL_ZONEID && !uuid_is_null(dd.gz_be_uuid)) { 525f169c0eaSGlenn Lagasse if ((ret = be_destroy_zones(bt.obe_name, bt.obe_root_ds, &dd)) 526f169c0eaSGlenn Lagasse != BE_SUCCESS) { 527f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy: failed to " 528f169c0eaSGlenn Lagasse "destroy one or more zones for BE %s\n"), 529f169c0eaSGlenn Lagasse bt.obe_name); 530f169c0eaSGlenn Lagasse goto done; 531f169c0eaSGlenn Lagasse } 532f169c0eaSGlenn Lagasse } 533f169c0eaSGlenn Lagasse 534f169c0eaSGlenn Lagasse /* Unmount the BE if it was mounted */ 535f169c0eaSGlenn Lagasse if (zfs_is_mounted(zhp, NULL)) { 536f169c0eaSGlenn Lagasse if ((ret = _be_unmount(bt.obe_name, BE_UNMOUNT_FLAG_FORCE)) 537f169c0eaSGlenn Lagasse != BE_SUCCESS) { 538f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy: " 539f169c0eaSGlenn Lagasse "failed to unmount %s\n"), bt.obe_name); 540f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 541f169c0eaSGlenn Lagasse return (ret); 542f169c0eaSGlenn Lagasse } 543f169c0eaSGlenn Lagasse } 544f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 545f169c0eaSGlenn Lagasse 546f169c0eaSGlenn Lagasse /* Destroy this BE */ 547f169c0eaSGlenn Lagasse if ((ret = _be_destroy((const char *)bt.obe_root_ds, &dd)) 548f169c0eaSGlenn Lagasse != BE_SUCCESS) { 549f169c0eaSGlenn Lagasse goto done; 550f169c0eaSGlenn Lagasse } 551f169c0eaSGlenn Lagasse 552f169c0eaSGlenn Lagasse /* Remove BE's entry from the boot menu */ 553f169c0eaSGlenn Lagasse if (getzoneid() == GLOBAL_ZONEID) { 554f169c0eaSGlenn Lagasse if ((ret = be_remove_menu(bt.obe_name, bt.obe_zpool, NULL)) 555f169c0eaSGlenn Lagasse != BE_SUCCESS) { 556f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy: failed to " 557f169c0eaSGlenn Lagasse "remove BE %s from the boot menu\n"), 558f169c0eaSGlenn Lagasse bt.obe_root_ds); 559f169c0eaSGlenn Lagasse goto done; 560f169c0eaSGlenn Lagasse } 561f169c0eaSGlenn Lagasse } 562f169c0eaSGlenn Lagasse 563f169c0eaSGlenn Lagasse done: 564f169c0eaSGlenn Lagasse be_zfs_fini(); 565f169c0eaSGlenn Lagasse 566f169c0eaSGlenn Lagasse return (ret); 567f169c0eaSGlenn Lagasse } 568f169c0eaSGlenn Lagasse 569f169c0eaSGlenn Lagasse /* 570f169c0eaSGlenn Lagasse * Function: be_copy 571f169c0eaSGlenn Lagasse * Description: This function makes a copy of an existing BE. If the original 572f169c0eaSGlenn Lagasse * BE and the new BE are in the same pool, it uses zfs cloning to 573f169c0eaSGlenn Lagasse * create the new BE, otherwise it does a physical copy. 574f169c0eaSGlenn Lagasse * If the original BE name isn't provided, it uses the currently 575f169c0eaSGlenn Lagasse * booted BE. If the new BE name isn't provided, it creates an 576f169c0eaSGlenn Lagasse * auto named BE and returns that name to the caller. 577f169c0eaSGlenn Lagasse * Parameters: 578f169c0eaSGlenn Lagasse * be_attrs - pointer to nvlist_t of attributes being passed in. 579f169c0eaSGlenn Lagasse * The following attributes are used by this function: 580f169c0eaSGlenn Lagasse * 581f169c0eaSGlenn Lagasse * BE_ATTR_ORIG_BE_NAME *optional 582f169c0eaSGlenn Lagasse * BE_ATTR_SNAP_NAME *optional 583f169c0eaSGlenn Lagasse * BE_ATTR_NEW_BE_NAME *optional 584f169c0eaSGlenn Lagasse * BE_ATTR_NEW_BE_POOL *optional 585f169c0eaSGlenn Lagasse * BE_ATTR_NEW_BE_DESC *optional 586f169c0eaSGlenn Lagasse * BE_ATTR_ZFS_PROPERTIES *optional 587f169c0eaSGlenn Lagasse * BE_ATTR_POLICY *optional 588f169c0eaSGlenn Lagasse * 589f169c0eaSGlenn Lagasse * If the BE_ATTR_NEW_BE_NAME was not passed in, upon 590f169c0eaSGlenn Lagasse * successful BE creation, the following attribute values 591f169c0eaSGlenn Lagasse * will be returned to the caller by setting them in the 592f169c0eaSGlenn Lagasse * be_attrs parameter passed in: 593f169c0eaSGlenn Lagasse * 594f169c0eaSGlenn Lagasse * BE_ATTR_SNAP_NAME 595f169c0eaSGlenn Lagasse * BE_ATTR_NEW_BE_NAME 596f169c0eaSGlenn Lagasse * Return: 597f169c0eaSGlenn Lagasse * BE_SUCCESS - Success 598f169c0eaSGlenn Lagasse * be_errno_t - Failure 599f169c0eaSGlenn Lagasse * Scope: 600f169c0eaSGlenn Lagasse * Public 601f169c0eaSGlenn Lagasse */ 602f169c0eaSGlenn Lagasse int 603f169c0eaSGlenn Lagasse be_copy(nvlist_t *be_attrs) 604f169c0eaSGlenn Lagasse { 605f169c0eaSGlenn Lagasse be_transaction_data_t bt = { 0 }; 606f169c0eaSGlenn Lagasse be_fs_list_data_t fld = { 0 }; 607f169c0eaSGlenn Lagasse zfs_handle_t *zhp = NULL; 608de1ab35cSAlexander Eremin zpool_handle_t *zphp = NULL; 609f169c0eaSGlenn Lagasse nvlist_t *zfs_props = NULL; 610f169c0eaSGlenn Lagasse uuid_t uu = { 0 }; 6117e0e2549SAlexander Eremin uuid_t parent_uu = { 0 }; 612f169c0eaSGlenn Lagasse char obe_root_ds[MAXPATHLEN]; 613f169c0eaSGlenn Lagasse char nbe_root_ds[MAXPATHLEN]; 614f169c0eaSGlenn Lagasse char ss[MAXPATHLEN]; 615f169c0eaSGlenn Lagasse char *new_mp = NULL; 616de1ab35cSAlexander Eremin char *obe_name = NULL; 617f169c0eaSGlenn Lagasse boolean_t autoname = B_FALSE; 618f169c0eaSGlenn Lagasse boolean_t be_created = B_FALSE; 619f169c0eaSGlenn Lagasse int i; 620f169c0eaSGlenn Lagasse int zret; 621f169c0eaSGlenn Lagasse int ret = BE_SUCCESS; 622de1ab35cSAlexander Eremin struct be_defaults be_defaults; 623f169c0eaSGlenn Lagasse 624f169c0eaSGlenn Lagasse /* Initialize libzfs handle */ 625f169c0eaSGlenn Lagasse if (!be_zfs_init()) 626f169c0eaSGlenn Lagasse return (BE_ERR_INIT); 627f169c0eaSGlenn Lagasse 628f169c0eaSGlenn Lagasse /* Get original BE name */ 629f169c0eaSGlenn Lagasse if (nvlist_lookup_pairs(be_attrs, NV_FLAG_NOENTOK, 630de1ab35cSAlexander Eremin BE_ATTR_ORIG_BE_NAME, DATA_TYPE_STRING, &obe_name, NULL) != 0) { 631f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: failed to lookup " 632f169c0eaSGlenn Lagasse "BE_ATTR_ORIG_BE_NAME attribute\n")); 633f169c0eaSGlenn Lagasse return (BE_ERR_INVAL); 634f169c0eaSGlenn Lagasse } 635f169c0eaSGlenn Lagasse 636de1ab35cSAlexander Eremin if ((ret = be_find_current_be(&bt)) != BE_SUCCESS) { 637de1ab35cSAlexander Eremin return (ret); 638de1ab35cSAlexander Eremin } 639de1ab35cSAlexander Eremin 640de1ab35cSAlexander Eremin be_get_defaults(&be_defaults); 641de1ab35cSAlexander Eremin 642f169c0eaSGlenn Lagasse /* If original BE name not provided, use current BE */ 643de1ab35cSAlexander Eremin if (obe_name != NULL) { 644de1ab35cSAlexander Eremin bt.obe_name = obe_name; 645f169c0eaSGlenn Lagasse /* Validate original BE name */ 646f169c0eaSGlenn Lagasse if (!be_valid_be_name(bt.obe_name)) { 647f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: " 648f169c0eaSGlenn Lagasse "invalid BE name %s\n"), bt.obe_name); 649f169c0eaSGlenn Lagasse return (BE_ERR_INVAL); 650f169c0eaSGlenn Lagasse } 651f169c0eaSGlenn Lagasse } 652f169c0eaSGlenn Lagasse 653de1ab35cSAlexander Eremin if (be_defaults.be_deflt_rpool_container) { 654de1ab35cSAlexander Eremin if ((zphp = zpool_open(g_zfs, bt.obe_zpool)) == NULL) { 655de1ab35cSAlexander Eremin be_print_err(gettext("be_get_node_data: failed to " 656de1ab35cSAlexander Eremin "open rpool (%s): %s\n"), bt.obe_zpool, 657de1ab35cSAlexander Eremin libzfs_error_description(g_zfs)); 658de1ab35cSAlexander Eremin return (zfs_err_to_be_err(g_zfs)); 659de1ab35cSAlexander Eremin } 660de1ab35cSAlexander Eremin if (be_find_zpool_callback(zphp, &bt) == 0) { 661de1ab35cSAlexander Eremin return (BE_ERR_BE_NOENT); 662de1ab35cSAlexander Eremin } 663de1ab35cSAlexander Eremin } else { 664de1ab35cSAlexander Eremin /* Find which zpool obe_name lives in */ 665de1ab35cSAlexander Eremin if ((zret = zpool_iter(g_zfs, be_find_zpool_callback, &bt)) == 666de1ab35cSAlexander Eremin 0) { 667de1ab35cSAlexander Eremin be_print_err(gettext("be_copy: failed to " 668de1ab35cSAlexander Eremin "find zpool for BE (%s)\n"), bt.obe_name); 669de1ab35cSAlexander Eremin return (BE_ERR_BE_NOENT); 670de1ab35cSAlexander Eremin } else if (zret < 0) { 671de1ab35cSAlexander Eremin be_print_err(gettext("be_copy: " 672de1ab35cSAlexander Eremin "zpool_iter failed: %s\n"), 673de1ab35cSAlexander Eremin libzfs_error_description(g_zfs)); 674de1ab35cSAlexander Eremin return (zfs_err_to_be_err(g_zfs)); 675de1ab35cSAlexander Eremin } 676f169c0eaSGlenn Lagasse } 677f169c0eaSGlenn Lagasse 678f169c0eaSGlenn Lagasse /* Get snapshot name of original BE if one was provided */ 679f169c0eaSGlenn Lagasse if (nvlist_lookup_pairs(be_attrs, NV_FLAG_NOENTOK, 680f169c0eaSGlenn Lagasse BE_ATTR_SNAP_NAME, DATA_TYPE_STRING, &bt.obe_snap_name, NULL) 681f169c0eaSGlenn Lagasse != 0) { 682f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: failed to lookup " 683f169c0eaSGlenn Lagasse "BE_ATTR_SNAP_NAME attribute\n")); 684f169c0eaSGlenn Lagasse return (BE_ERR_INVAL); 685f169c0eaSGlenn Lagasse } 686f169c0eaSGlenn Lagasse 687f169c0eaSGlenn Lagasse /* Get new BE name */ 688f169c0eaSGlenn Lagasse if (nvlist_lookup_pairs(be_attrs, NV_FLAG_NOENTOK, 689f169c0eaSGlenn Lagasse BE_ATTR_NEW_BE_NAME, DATA_TYPE_STRING, &bt.nbe_name, NULL) 690f169c0eaSGlenn Lagasse != 0) { 691f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: failed to lookup " 692f169c0eaSGlenn Lagasse "BE_ATTR_NEW_BE_NAME attribute\n")); 693f169c0eaSGlenn Lagasse return (BE_ERR_INVAL); 694f169c0eaSGlenn Lagasse } 695f169c0eaSGlenn Lagasse 696f169c0eaSGlenn Lagasse /* Get zpool name to create new BE in */ 697f169c0eaSGlenn Lagasse if (nvlist_lookup_pairs(be_attrs, NV_FLAG_NOENTOK, 698f169c0eaSGlenn Lagasse BE_ATTR_NEW_BE_POOL, DATA_TYPE_STRING, &bt.nbe_zpool, NULL) != 0) { 699f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: failed to lookup " 700f169c0eaSGlenn Lagasse "BE_ATTR_NEW_BE_POOL attribute\n")); 701f169c0eaSGlenn Lagasse return (BE_ERR_INVAL); 702f169c0eaSGlenn Lagasse } 703f169c0eaSGlenn Lagasse 704f169c0eaSGlenn Lagasse /* Get new BE's description if one was provided */ 705f169c0eaSGlenn Lagasse if (nvlist_lookup_pairs(be_attrs, NV_FLAG_NOENTOK, 706f169c0eaSGlenn Lagasse BE_ATTR_NEW_BE_DESC, DATA_TYPE_STRING, &bt.nbe_desc, NULL) != 0) { 707f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: failed to lookup " 708f169c0eaSGlenn Lagasse "BE_ATTR_NEW_BE_DESC attribute\n")); 709f169c0eaSGlenn Lagasse return (BE_ERR_INVAL); 710f169c0eaSGlenn Lagasse } 711f169c0eaSGlenn Lagasse 712f169c0eaSGlenn Lagasse /* Get BE policy to create this snapshot under */ 713f169c0eaSGlenn Lagasse if (nvlist_lookup_pairs(be_attrs, NV_FLAG_NOENTOK, 714f169c0eaSGlenn Lagasse BE_ATTR_POLICY, DATA_TYPE_STRING, &bt.policy, NULL) != 0) { 715f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: failed to lookup " 716f169c0eaSGlenn Lagasse "BE_ATTR_POLICY attribute\n")); 717f169c0eaSGlenn Lagasse return (BE_ERR_INVAL); 718f169c0eaSGlenn Lagasse } 719f169c0eaSGlenn Lagasse 720f169c0eaSGlenn Lagasse /* 721f169c0eaSGlenn Lagasse * Create property list for new BE root dataset. If some 722f169c0eaSGlenn Lagasse * zfs properties were already provided by the caller, dup 723f169c0eaSGlenn Lagasse * that list. Otherwise initialize a new property list. 724f169c0eaSGlenn Lagasse */ 725f169c0eaSGlenn Lagasse if (nvlist_lookup_pairs(be_attrs, NV_FLAG_NOENTOK, 726f169c0eaSGlenn Lagasse BE_ATTR_ZFS_PROPERTIES, DATA_TYPE_NVLIST, &zfs_props, NULL) 727f169c0eaSGlenn Lagasse != 0) { 728f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: failed to lookup " 729f169c0eaSGlenn Lagasse "BE_ATTR_ZFS_PROPERTIES attribute\n")); 730f169c0eaSGlenn Lagasse return (BE_ERR_INVAL); 731f169c0eaSGlenn Lagasse } 732f169c0eaSGlenn Lagasse if (zfs_props != NULL) { 733f169c0eaSGlenn Lagasse /* Make sure its a unique nvlist */ 734f169c0eaSGlenn Lagasse if (!(zfs_props->nvl_nvflag & NV_UNIQUE_NAME) && 735f169c0eaSGlenn Lagasse !(zfs_props->nvl_nvflag & NV_UNIQUE_NAME_TYPE)) { 736f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: ZFS property list " 737f169c0eaSGlenn Lagasse "not unique\n")); 738f169c0eaSGlenn Lagasse return (BE_ERR_INVAL); 739f169c0eaSGlenn Lagasse } 740f169c0eaSGlenn Lagasse 741f169c0eaSGlenn Lagasse /* Dup the list */ 742f169c0eaSGlenn Lagasse if (nvlist_dup(zfs_props, &bt.nbe_zfs_props, 0) != 0) { 743f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: " 744f169c0eaSGlenn Lagasse "failed to dup ZFS property list\n")); 745f169c0eaSGlenn Lagasse return (BE_ERR_NOMEM); 746f169c0eaSGlenn Lagasse } 747f169c0eaSGlenn Lagasse } else { 748f169c0eaSGlenn Lagasse /* Initialize new nvlist */ 749f169c0eaSGlenn Lagasse if (nvlist_alloc(&bt.nbe_zfs_props, NV_UNIQUE_NAME, 0) != 0) { 750f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: internal " 751f169c0eaSGlenn Lagasse "error: out of memory\n")); 752f169c0eaSGlenn Lagasse return (BE_ERR_NOMEM); 753f169c0eaSGlenn Lagasse } 754f169c0eaSGlenn Lagasse } 755f169c0eaSGlenn Lagasse 756f169c0eaSGlenn Lagasse /* 757f169c0eaSGlenn Lagasse * If new BE name provided, validate the BE name and then verify 758f169c0eaSGlenn Lagasse * that new BE name doesn't already exist in some pool. 759f169c0eaSGlenn Lagasse */ 760f169c0eaSGlenn Lagasse if (bt.nbe_name) { 761f169c0eaSGlenn Lagasse /* Validate original BE name */ 762f169c0eaSGlenn Lagasse if (!be_valid_be_name(bt.nbe_name)) { 763f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: " 764f169c0eaSGlenn Lagasse "invalid BE name %s\n"), bt.nbe_name); 765f169c0eaSGlenn Lagasse ret = BE_ERR_INVAL; 766f169c0eaSGlenn Lagasse goto done; 767f169c0eaSGlenn Lagasse } 768f169c0eaSGlenn Lagasse 769f169c0eaSGlenn Lagasse /* Verify it doesn't already exist */ 7707e0e2549SAlexander Eremin if (getzoneid() == GLOBAL_ZONEID) { 7717e0e2549SAlexander Eremin if ((zret = zpool_iter(g_zfs, be_exists_callback, 7727e0e2549SAlexander Eremin bt.nbe_name)) > 0) { 7737e0e2549SAlexander Eremin be_print_err(gettext("be_copy: BE (%s) already " 7747e0e2549SAlexander Eremin "exists\n"), bt.nbe_name); 7757e0e2549SAlexander Eremin ret = BE_ERR_BE_EXISTS; 7767e0e2549SAlexander Eremin goto done; 7777e0e2549SAlexander Eremin } else if (zret < 0) { 7787e0e2549SAlexander Eremin be_print_err(gettext("be_copy: zpool_iter " 7797e0e2549SAlexander Eremin "failed: %s\n"), 7807e0e2549SAlexander Eremin libzfs_error_description(g_zfs)); 7817e0e2549SAlexander Eremin ret = zfs_err_to_be_err(g_zfs); 7827e0e2549SAlexander Eremin goto done; 7837e0e2549SAlexander Eremin } 7847e0e2549SAlexander Eremin } else { 7857e0e2549SAlexander Eremin be_make_root_ds(bt.nbe_zpool, bt.nbe_name, nbe_root_ds, 7867e0e2549SAlexander Eremin sizeof (nbe_root_ds)); 7877e0e2549SAlexander Eremin if (zfs_dataset_exists(g_zfs, nbe_root_ds, 7887e0e2549SAlexander Eremin ZFS_TYPE_FILESYSTEM)) { 7897e0e2549SAlexander Eremin be_print_err(gettext("be_copy: BE (%s) already " 7907e0e2549SAlexander Eremin "exists\n"), bt.nbe_name); 7917e0e2549SAlexander Eremin ret = BE_ERR_BE_EXISTS; 7927e0e2549SAlexander Eremin goto done; 7937e0e2549SAlexander Eremin } 794f169c0eaSGlenn Lagasse } 795f169c0eaSGlenn Lagasse } else { 796f169c0eaSGlenn Lagasse /* 797f169c0eaSGlenn Lagasse * If an auto named BE is desired, it must be in the same 798f169c0eaSGlenn Lagasse * pool is the original BE. 799f169c0eaSGlenn Lagasse */ 800f169c0eaSGlenn Lagasse if (bt.nbe_zpool != NULL) { 801f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: cannot specify pool " 802f169c0eaSGlenn Lagasse "name when creating an auto named BE\n")); 803f169c0eaSGlenn Lagasse ret = BE_ERR_INVAL; 804f169c0eaSGlenn Lagasse goto done; 805f169c0eaSGlenn Lagasse } 806f169c0eaSGlenn Lagasse 807f169c0eaSGlenn Lagasse /* 808f169c0eaSGlenn Lagasse * Generate auto named BE 809f169c0eaSGlenn Lagasse */ 810f169c0eaSGlenn Lagasse if ((bt.nbe_name = be_auto_be_name(bt.obe_name)) 811f169c0eaSGlenn Lagasse == NULL) { 812f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: " 813f169c0eaSGlenn Lagasse "failed to generate auto BE name\n")); 814f169c0eaSGlenn Lagasse ret = BE_ERR_AUTONAME; 815f169c0eaSGlenn Lagasse goto done; 816f169c0eaSGlenn Lagasse } 817f169c0eaSGlenn Lagasse 818f169c0eaSGlenn Lagasse autoname = B_TRUE; 819f169c0eaSGlenn Lagasse } 820f169c0eaSGlenn Lagasse 821f169c0eaSGlenn Lagasse /* 822f169c0eaSGlenn Lagasse * If zpool name to create new BE in is not provided, 823f169c0eaSGlenn Lagasse * create new BE in original BE's pool. 824f169c0eaSGlenn Lagasse */ 825f169c0eaSGlenn Lagasse if (bt.nbe_zpool == NULL) { 826f169c0eaSGlenn Lagasse bt.nbe_zpool = bt.obe_zpool; 827f169c0eaSGlenn Lagasse } 828f169c0eaSGlenn Lagasse 829f169c0eaSGlenn Lagasse /* Get root dataset names for obe_name and nbe_name */ 830f169c0eaSGlenn Lagasse be_make_root_ds(bt.obe_zpool, bt.obe_name, obe_root_ds, 831f169c0eaSGlenn Lagasse sizeof (obe_root_ds)); 832f169c0eaSGlenn Lagasse be_make_root_ds(bt.nbe_zpool, bt.nbe_name, nbe_root_ds, 833f169c0eaSGlenn Lagasse sizeof (nbe_root_ds)); 834f169c0eaSGlenn Lagasse 835f169c0eaSGlenn Lagasse bt.obe_root_ds = obe_root_ds; 836f169c0eaSGlenn Lagasse bt.nbe_root_ds = nbe_root_ds; 837f169c0eaSGlenn Lagasse 838f169c0eaSGlenn Lagasse /* 839f169c0eaSGlenn Lagasse * If an existing snapshot name has been provided to create from, 840f169c0eaSGlenn Lagasse * verify that it exists for the original BE's root dataset. 841f169c0eaSGlenn Lagasse */ 842f169c0eaSGlenn Lagasse if (bt.obe_snap_name != NULL) { 843f169c0eaSGlenn Lagasse 844f169c0eaSGlenn Lagasse /* Generate dataset name for snapshot to use. */ 845f169c0eaSGlenn Lagasse (void) snprintf(ss, sizeof (ss), "%s@%s", bt.obe_root_ds, 846f169c0eaSGlenn Lagasse bt.obe_snap_name); 847f169c0eaSGlenn Lagasse 848f169c0eaSGlenn Lagasse /* Verify snapshot exists */ 849f169c0eaSGlenn Lagasse if (!zfs_dataset_exists(g_zfs, ss, ZFS_TYPE_SNAPSHOT)) { 850f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: " 851f169c0eaSGlenn Lagasse "snapshot does not exist (%s): %s\n"), ss, 852f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs)); 853f169c0eaSGlenn Lagasse ret = BE_ERR_SS_NOENT; 854f169c0eaSGlenn Lagasse goto done; 855f169c0eaSGlenn Lagasse } 856f169c0eaSGlenn Lagasse } else { 857f169c0eaSGlenn Lagasse /* 858f169c0eaSGlenn Lagasse * Else snapshot name was not provided, generate an 859f169c0eaSGlenn Lagasse * auto named snapshot to use as its origin. 860f169c0eaSGlenn Lagasse */ 861f169c0eaSGlenn Lagasse if ((ret = _be_create_snapshot(bt.obe_name, 862f169c0eaSGlenn Lagasse &bt.obe_snap_name, bt.policy)) != BE_SUCCESS) { 863f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: " 864f169c0eaSGlenn Lagasse "failed to create auto named snapshot\n")); 865f169c0eaSGlenn Lagasse goto done; 866f169c0eaSGlenn Lagasse } 867f169c0eaSGlenn Lagasse 868f169c0eaSGlenn Lagasse if (nvlist_add_string(be_attrs, BE_ATTR_SNAP_NAME, 869f169c0eaSGlenn Lagasse bt.obe_snap_name) != 0) { 870f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: " 871f169c0eaSGlenn Lagasse "failed to add snap name to be_attrs\n")); 872f169c0eaSGlenn Lagasse ret = BE_ERR_NOMEM; 873f169c0eaSGlenn Lagasse goto done; 874f169c0eaSGlenn Lagasse } 875f169c0eaSGlenn Lagasse } 876f169c0eaSGlenn Lagasse 877f169c0eaSGlenn Lagasse /* Get handle to original BE's root dataset. */ 878f169c0eaSGlenn Lagasse if ((zhp = zfs_open(g_zfs, bt.obe_root_ds, ZFS_TYPE_FILESYSTEM)) 879f169c0eaSGlenn Lagasse == NULL) { 880f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: failed to " 881f169c0eaSGlenn Lagasse "open BE root dataset (%s): %s\n"), bt.obe_root_ds, 882f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs)); 883f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs); 884f169c0eaSGlenn Lagasse goto done; 885f169c0eaSGlenn Lagasse } 886f169c0eaSGlenn Lagasse 887f169c0eaSGlenn Lagasse /* If original BE is currently mounted, record its altroot. */ 888f169c0eaSGlenn Lagasse if (zfs_is_mounted(zhp, &bt.obe_altroot) && bt.obe_altroot == NULL) { 889f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: failed to " 890f169c0eaSGlenn Lagasse "get altroot of mounted BE %s: %s\n"), 891f169c0eaSGlenn Lagasse bt.obe_name, libzfs_error_description(g_zfs)); 892f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs); 893f169c0eaSGlenn Lagasse goto done; 894f169c0eaSGlenn Lagasse } 895f169c0eaSGlenn Lagasse 896f169c0eaSGlenn Lagasse if (strcmp(bt.obe_zpool, bt.nbe_zpool) == 0) { 897f169c0eaSGlenn Lagasse 898f169c0eaSGlenn Lagasse /* Do clone */ 899f169c0eaSGlenn Lagasse 900f169c0eaSGlenn Lagasse /* 901f169c0eaSGlenn Lagasse * Iterate through original BE's datasets and clone 902f169c0eaSGlenn Lagasse * them to create new BE. This call will end up closing 903f169c0eaSGlenn Lagasse * the zfs handle passed in whether it succeeds for fails. 904f169c0eaSGlenn Lagasse */ 905f169c0eaSGlenn Lagasse if ((ret = be_clone_fs_callback(zhp, &bt)) != 0) { 906f169c0eaSGlenn Lagasse zhp = NULL; 907f169c0eaSGlenn Lagasse /* Creating clone BE failed */ 908f169c0eaSGlenn Lagasse if (!autoname || ret != BE_ERR_BE_EXISTS) { 909f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: " 910f169c0eaSGlenn Lagasse "failed to clone new BE (%s) from " 911f169c0eaSGlenn Lagasse "orig BE (%s)\n"), 912f169c0eaSGlenn Lagasse bt.nbe_name, bt.obe_name); 913f169c0eaSGlenn Lagasse ret = BE_ERR_CLONE; 914f169c0eaSGlenn Lagasse goto done; 915f169c0eaSGlenn Lagasse } 916f169c0eaSGlenn Lagasse 917f169c0eaSGlenn Lagasse /* 918f169c0eaSGlenn Lagasse * We failed to create the new BE because a BE with 919f169c0eaSGlenn Lagasse * the auto-name we generated above has since come 920f169c0eaSGlenn Lagasse * into existence. Regenerate a new auto-name 921f169c0eaSGlenn Lagasse * and retry. 922f169c0eaSGlenn Lagasse */ 923f169c0eaSGlenn Lagasse for (i = 1; i < BE_AUTO_NAME_MAX_TRY; i++) { 924f169c0eaSGlenn Lagasse 925f169c0eaSGlenn Lagasse /* Sleep 1 before retrying */ 926f169c0eaSGlenn Lagasse (void) sleep(1); 927f169c0eaSGlenn Lagasse 928f169c0eaSGlenn Lagasse /* Generate new auto BE name */ 929f169c0eaSGlenn Lagasse free(bt.nbe_name); 930f169c0eaSGlenn Lagasse if ((bt.nbe_name = be_auto_be_name(bt.obe_name)) 931f169c0eaSGlenn Lagasse == NULL) { 932f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: " 933f169c0eaSGlenn Lagasse "failed to generate auto " 934f169c0eaSGlenn Lagasse "BE name\n")); 935f169c0eaSGlenn Lagasse ret = BE_ERR_AUTONAME; 936f169c0eaSGlenn Lagasse goto done; 937f169c0eaSGlenn Lagasse } 938f169c0eaSGlenn Lagasse 939f169c0eaSGlenn Lagasse /* 940f169c0eaSGlenn Lagasse * Regenerate string for new BE's 941f169c0eaSGlenn Lagasse * root dataset name 942f169c0eaSGlenn Lagasse */ 943f169c0eaSGlenn Lagasse be_make_root_ds(bt.nbe_zpool, bt.nbe_name, 944f169c0eaSGlenn Lagasse nbe_root_ds, sizeof (nbe_root_ds)); 945f169c0eaSGlenn Lagasse bt.nbe_root_ds = nbe_root_ds; 946f169c0eaSGlenn Lagasse 947f169c0eaSGlenn Lagasse /* 948f169c0eaSGlenn Lagasse * Get handle to original BE's root dataset. 949f169c0eaSGlenn Lagasse */ 950f169c0eaSGlenn Lagasse if ((zhp = zfs_open(g_zfs, bt.obe_root_ds, 951f169c0eaSGlenn Lagasse ZFS_TYPE_FILESYSTEM)) == NULL) { 952f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: " 953f169c0eaSGlenn Lagasse "failed to open BE root dataset " 954f169c0eaSGlenn Lagasse "(%s): %s\n"), bt.obe_root_ds, 955f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs)); 956f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs); 957f169c0eaSGlenn Lagasse goto done; 958f169c0eaSGlenn Lagasse } 959f169c0eaSGlenn Lagasse 960f169c0eaSGlenn Lagasse /* 961f169c0eaSGlenn Lagasse * Try to clone the BE again. This 962f169c0eaSGlenn Lagasse * call will end up closing the zfs 963f169c0eaSGlenn Lagasse * handle passed in whether it 964f169c0eaSGlenn Lagasse * succeeds or fails. 965f169c0eaSGlenn Lagasse */ 966f169c0eaSGlenn Lagasse ret = be_clone_fs_callback(zhp, &bt); 967f169c0eaSGlenn Lagasse zhp = NULL; 968f169c0eaSGlenn Lagasse if (ret == 0) { 969f169c0eaSGlenn Lagasse break; 970f169c0eaSGlenn Lagasse } else if (ret != BE_ERR_BE_EXISTS) { 971f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: " 972f169c0eaSGlenn Lagasse "failed to clone new BE " 973f169c0eaSGlenn Lagasse "(%s) from orig BE (%s)\n"), 974f169c0eaSGlenn Lagasse bt.nbe_name, bt.obe_name); 975f169c0eaSGlenn Lagasse ret = BE_ERR_CLONE; 976f169c0eaSGlenn Lagasse goto done; 977f169c0eaSGlenn Lagasse } 978f169c0eaSGlenn Lagasse } 979f169c0eaSGlenn Lagasse 980f169c0eaSGlenn Lagasse /* 981f169c0eaSGlenn Lagasse * If we've exhausted the maximum number of 982f169c0eaSGlenn Lagasse * tries, free the auto BE name and return 983f169c0eaSGlenn Lagasse * error. 984f169c0eaSGlenn Lagasse */ 985f169c0eaSGlenn Lagasse if (i == BE_AUTO_NAME_MAX_TRY) { 986f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: failed " 987f169c0eaSGlenn Lagasse "to create unique auto BE name\n")); 988f169c0eaSGlenn Lagasse free(bt.nbe_name); 989f169c0eaSGlenn Lagasse bt.nbe_name = NULL; 990f169c0eaSGlenn Lagasse ret = BE_ERR_AUTONAME; 991f169c0eaSGlenn Lagasse goto done; 992f169c0eaSGlenn Lagasse } 993f169c0eaSGlenn Lagasse } 994f169c0eaSGlenn Lagasse zhp = NULL; 995f169c0eaSGlenn Lagasse 996f169c0eaSGlenn Lagasse } else { 997f169c0eaSGlenn Lagasse 998f169c0eaSGlenn Lagasse /* Do copy (i.e. send BE datasets via zfs_send/recv) */ 999f169c0eaSGlenn Lagasse 1000f169c0eaSGlenn Lagasse /* 1001f169c0eaSGlenn Lagasse * Verify BE container dataset in nbe_zpool exists. 1002f169c0eaSGlenn Lagasse * If not, create it. 1003f169c0eaSGlenn Lagasse */ 1004f169c0eaSGlenn Lagasse if (!be_create_container_ds(bt.nbe_zpool)) { 1005f169c0eaSGlenn Lagasse ret = BE_ERR_CREATDS; 1006f169c0eaSGlenn Lagasse goto done; 1007f169c0eaSGlenn Lagasse } 1008f169c0eaSGlenn Lagasse 1009f169c0eaSGlenn Lagasse /* 1010f169c0eaSGlenn Lagasse * Iterate through original BE's datasets and send 1011f169c0eaSGlenn Lagasse * them to the other pool. This call will end up closing 1012f169c0eaSGlenn Lagasse * the zfs handle passed in whether it succeeds or fails. 1013f169c0eaSGlenn Lagasse */ 1014f169c0eaSGlenn Lagasse if ((ret = be_send_fs_callback(zhp, &bt)) != 0) { 1015f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: failed to " 1016f169c0eaSGlenn Lagasse "send BE (%s) to pool (%s)\n"), bt.obe_name, 1017f169c0eaSGlenn Lagasse bt.nbe_zpool); 1018f169c0eaSGlenn Lagasse ret = BE_ERR_COPY; 1019f169c0eaSGlenn Lagasse zhp = NULL; 1020f169c0eaSGlenn Lagasse goto done; 1021f169c0eaSGlenn Lagasse } 1022f169c0eaSGlenn Lagasse zhp = NULL; 1023f169c0eaSGlenn Lagasse } 1024f169c0eaSGlenn Lagasse 1025f169c0eaSGlenn Lagasse /* 1026f169c0eaSGlenn Lagasse * Set flag to note that the dataset(s) for the new BE have been 1027f169c0eaSGlenn Lagasse * successfully created so that if a failure happens from this point 1028f169c0eaSGlenn Lagasse * on, we know to cleanup these datasets. 1029f169c0eaSGlenn Lagasse */ 1030f169c0eaSGlenn Lagasse be_created = B_TRUE; 1031f169c0eaSGlenn Lagasse 1032f169c0eaSGlenn Lagasse /* 1033f169c0eaSGlenn Lagasse * Validate that the new BE is mountable. 1034f169c0eaSGlenn Lagasse * Do not attempt to mount non-global zone datasets 1035f169c0eaSGlenn Lagasse * since they are not cloned yet. 1036f169c0eaSGlenn Lagasse */ 1037f169c0eaSGlenn Lagasse if ((ret = _be_mount(bt.nbe_name, &new_mp, BE_MOUNT_FLAG_NO_ZONES)) 1038f169c0eaSGlenn Lagasse != BE_SUCCESS) { 1039f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: failed to " 1040f169c0eaSGlenn Lagasse "mount newly created BE\n")); 1041f169c0eaSGlenn Lagasse (void) _be_unmount(bt.nbe_name, 0); 1042f169c0eaSGlenn Lagasse goto done; 1043f169c0eaSGlenn Lagasse } 1044f169c0eaSGlenn Lagasse 1045f169c0eaSGlenn Lagasse /* Set UUID for new BE */ 10467e0e2549SAlexander Eremin if (getzoneid() == GLOBAL_ZONEID) { 10477e0e2549SAlexander Eremin if (be_set_uuid(bt.nbe_root_ds) != BE_SUCCESS) { 10487e0e2549SAlexander Eremin be_print_err(gettext("be_copy: failed to " 10497e0e2549SAlexander Eremin "set uuid for new BE\n")); 10507e0e2549SAlexander Eremin } 10517e0e2549SAlexander Eremin } else { 10527e0e2549SAlexander Eremin if ((ret = be_zone_get_parent_uuid(bt.obe_root_ds, 10537e0e2549SAlexander Eremin &parent_uu)) != BE_SUCCESS) { 10547e0e2549SAlexander Eremin be_print_err(gettext("be_copy: failed to get " 10557e0e2549SAlexander Eremin "parentbe uuid from orig BE\n")); 10567e0e2549SAlexander Eremin ret = BE_ERR_ZONE_NO_PARENTBE; 10577e0e2549SAlexander Eremin goto done; 10587e0e2549SAlexander Eremin } else if ((ret = be_zone_set_parent_uuid(bt.nbe_root_ds, 10597e0e2549SAlexander Eremin parent_uu)) != BE_SUCCESS) { 10607e0e2549SAlexander Eremin be_print_err(gettext("be_copy: failed to set " 10617e0e2549SAlexander Eremin "parentbe uuid for newly created BE\n")); 10627e0e2549SAlexander Eremin goto done; 10637e0e2549SAlexander Eremin } 1064f169c0eaSGlenn Lagasse } 1065f169c0eaSGlenn Lagasse 1066f169c0eaSGlenn Lagasse /* 1067f169c0eaSGlenn Lagasse * Process zones outside of the private BE namespace. 1068f169c0eaSGlenn Lagasse * This has to be done here because we need the uuid set in the 1069f169c0eaSGlenn Lagasse * root dataset of the new BE. The uuid is use to set the parentbe 1070f169c0eaSGlenn Lagasse * property for the new zones datasets. 1071f169c0eaSGlenn Lagasse */ 1072f169c0eaSGlenn Lagasse if (getzoneid() == GLOBAL_ZONEID && 1073f169c0eaSGlenn Lagasse be_get_uuid(bt.obe_root_ds, &uu) == BE_SUCCESS) { 1074f169c0eaSGlenn Lagasse if ((ret = be_copy_zones(bt.obe_name, bt.obe_root_ds, 1075f169c0eaSGlenn Lagasse bt.nbe_root_ds)) != BE_SUCCESS) { 1076f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: failed to process " 1077f169c0eaSGlenn Lagasse "zones\n")); 1078f169c0eaSGlenn Lagasse goto done; 1079f169c0eaSGlenn Lagasse } 1080f169c0eaSGlenn Lagasse } 1081f169c0eaSGlenn Lagasse 1082f169c0eaSGlenn Lagasse /* 1083f169c0eaSGlenn Lagasse * Generate a list of file systems from the original BE that are 1084f169c0eaSGlenn Lagasse * legacy mounted. We use this list to determine which entries in 1085f169c0eaSGlenn Lagasse * vfstab we need to update for the new BE we've just created. 1086f169c0eaSGlenn Lagasse */ 1087f169c0eaSGlenn Lagasse if ((ret = be_get_legacy_fs(bt.obe_name, bt.obe_root_ds, NULL, NULL, 1088f169c0eaSGlenn Lagasse &fld)) != BE_SUCCESS) { 1089f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: failed to " 1090f169c0eaSGlenn Lagasse "get legacy mounted file system list for %s\n"), 1091f169c0eaSGlenn Lagasse bt.obe_name); 1092f169c0eaSGlenn Lagasse goto done; 1093f169c0eaSGlenn Lagasse } 1094f169c0eaSGlenn Lagasse 1095f169c0eaSGlenn Lagasse /* 1096f169c0eaSGlenn Lagasse * Update new BE's vfstab. 1097f169c0eaSGlenn Lagasse */ 1098f169c0eaSGlenn Lagasse if ((ret = be_update_vfstab(bt.nbe_name, bt.obe_zpool, bt.nbe_zpool, 1099f169c0eaSGlenn Lagasse &fld, new_mp)) != BE_SUCCESS) { 1100f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: failed to " 1101f169c0eaSGlenn Lagasse "update new BE's vfstab (%s)\n"), bt.nbe_name); 1102f169c0eaSGlenn Lagasse goto done; 1103f169c0eaSGlenn Lagasse } 1104f169c0eaSGlenn Lagasse 1105f169c0eaSGlenn Lagasse /* Unmount the new BE */ 1106f169c0eaSGlenn Lagasse if ((ret = _be_unmount(bt.nbe_name, 0)) != BE_SUCCESS) { 1107f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: failed to " 1108f169c0eaSGlenn Lagasse "unmount newly created BE\n")); 1109f169c0eaSGlenn Lagasse goto done; 1110f169c0eaSGlenn Lagasse } 1111f169c0eaSGlenn Lagasse 1112f169c0eaSGlenn Lagasse /* 1113f169c0eaSGlenn Lagasse * Add boot menu entry for newly created clone 1114f169c0eaSGlenn Lagasse */ 1115f169c0eaSGlenn Lagasse if (getzoneid() == GLOBAL_ZONEID && 1116f169c0eaSGlenn Lagasse (ret = be_append_menu(bt.nbe_name, bt.nbe_zpool, 1117f169c0eaSGlenn Lagasse NULL, bt.obe_root_ds, bt.nbe_desc)) != BE_SUCCESS) { 1118f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: failed to " 1119f169c0eaSGlenn Lagasse "add BE (%s) to boot menu\n"), bt.nbe_name); 1120f169c0eaSGlenn Lagasse goto done; 1121f169c0eaSGlenn Lagasse } 1122f169c0eaSGlenn Lagasse 1123f169c0eaSGlenn Lagasse /* 1124f169c0eaSGlenn Lagasse * If we succeeded in creating an auto named BE, set its policy 1125f169c0eaSGlenn Lagasse * type and return the auto generated name to the caller by storing 1126f169c0eaSGlenn Lagasse * it in the nvlist passed in by the caller. 1127f169c0eaSGlenn Lagasse */ 1128f169c0eaSGlenn Lagasse if (autoname) { 1129f169c0eaSGlenn Lagasse /* Get handle to new BE's root dataset. */ 1130f169c0eaSGlenn Lagasse if ((zhp = zfs_open(g_zfs, bt.nbe_root_ds, 1131f169c0eaSGlenn Lagasse ZFS_TYPE_FILESYSTEM)) == NULL) { 1132f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: failed to " 1133f169c0eaSGlenn Lagasse "open BE root dataset (%s): %s\n"), bt.nbe_root_ds, 1134f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs)); 1135f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs); 1136f169c0eaSGlenn Lagasse goto done; 1137f169c0eaSGlenn Lagasse } 1138f169c0eaSGlenn Lagasse 1139f169c0eaSGlenn Lagasse /* 1140f169c0eaSGlenn Lagasse * Set the policy type property into the new BE's root dataset 1141f169c0eaSGlenn Lagasse */ 1142f169c0eaSGlenn Lagasse if (bt.policy == NULL) { 1143f169c0eaSGlenn Lagasse /* If no policy type provided, use default type */ 1144f169c0eaSGlenn Lagasse bt.policy = be_default_policy(); 1145f169c0eaSGlenn Lagasse } 1146f169c0eaSGlenn Lagasse 1147f169c0eaSGlenn Lagasse if (zfs_prop_set(zhp, BE_POLICY_PROPERTY, bt.policy) != 0) { 1148f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: failed to " 1149f169c0eaSGlenn Lagasse "set BE policy for %s: %s\n"), bt.nbe_name, 1150f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs)); 1151f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs); 1152f169c0eaSGlenn Lagasse goto done; 1153f169c0eaSGlenn Lagasse } 1154f169c0eaSGlenn Lagasse 1155f169c0eaSGlenn Lagasse /* 1156f169c0eaSGlenn Lagasse * Return the auto generated name to the caller 1157f169c0eaSGlenn Lagasse */ 1158f169c0eaSGlenn Lagasse if (bt.nbe_name) { 1159f169c0eaSGlenn Lagasse if (nvlist_add_string(be_attrs, BE_ATTR_NEW_BE_NAME, 1160f169c0eaSGlenn Lagasse bt.nbe_name) != 0) { 1161f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: failed to " 1162f169c0eaSGlenn Lagasse "add snap name to be_attrs\n")); 1163f169c0eaSGlenn Lagasse } 1164f169c0eaSGlenn Lagasse } 1165f169c0eaSGlenn Lagasse } 1166f169c0eaSGlenn Lagasse 1167f169c0eaSGlenn Lagasse done: 1168f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 1169f169c0eaSGlenn Lagasse be_free_fs_list(&fld); 1170f169c0eaSGlenn Lagasse 1171aab83bb8SJosef 'Jeff' Sipek nvlist_free(bt.nbe_zfs_props); 1172f169c0eaSGlenn Lagasse 1173f169c0eaSGlenn Lagasse free(bt.obe_altroot); 1174f169c0eaSGlenn Lagasse free(new_mp); 1175f169c0eaSGlenn Lagasse 1176f169c0eaSGlenn Lagasse /* 1177f169c0eaSGlenn Lagasse * If a failure occurred and we already created the datasets for 1178f169c0eaSGlenn Lagasse * the new boot environment, destroy them. 1179f169c0eaSGlenn Lagasse */ 1180f169c0eaSGlenn Lagasse if (ret != BE_SUCCESS && be_created) { 1181f169c0eaSGlenn Lagasse be_destroy_data_t cdd = { 0 }; 1182f169c0eaSGlenn Lagasse 1183f169c0eaSGlenn Lagasse cdd.force_unmount = B_TRUE; 1184f169c0eaSGlenn Lagasse 1185f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy: " 1186f169c0eaSGlenn Lagasse "destroying partially created boot environment\n")); 1187f169c0eaSGlenn Lagasse 1188f169c0eaSGlenn Lagasse if (getzoneid() == GLOBAL_ZONEID && be_get_uuid(bt.nbe_root_ds, 1189f169c0eaSGlenn Lagasse &cdd.gz_be_uuid) == 0) 1190f169c0eaSGlenn Lagasse (void) be_destroy_zones(bt.nbe_name, bt.nbe_root_ds, 1191f169c0eaSGlenn Lagasse &cdd); 1192f169c0eaSGlenn Lagasse 1193f169c0eaSGlenn Lagasse (void) _be_destroy(bt.nbe_root_ds, &cdd); 1194f169c0eaSGlenn Lagasse } 1195f169c0eaSGlenn Lagasse 1196f169c0eaSGlenn Lagasse be_zfs_fini(); 1197f169c0eaSGlenn Lagasse 1198f169c0eaSGlenn Lagasse return (ret); 1199f169c0eaSGlenn Lagasse } 1200f169c0eaSGlenn Lagasse 1201f169c0eaSGlenn Lagasse /* ******************************************************************** */ 1202f169c0eaSGlenn Lagasse /* Semi-Private Functions */ 1203f169c0eaSGlenn Lagasse /* ******************************************************************** */ 1204f169c0eaSGlenn Lagasse 1205f169c0eaSGlenn Lagasse /* 1206f169c0eaSGlenn Lagasse * Function: be_find_zpool_callback 1207f169c0eaSGlenn Lagasse * Description: Callback function used to find the pool that a BE lives in. 1208f169c0eaSGlenn Lagasse * Parameters: 1209f169c0eaSGlenn Lagasse * zlp - zpool_handle_t pointer for the current pool being 1210f169c0eaSGlenn Lagasse * looked at. 1211f169c0eaSGlenn Lagasse * data - be_transaction_data_t pointer providing information 1212f169c0eaSGlenn Lagasse * about the BE that's being searched for. 1213f169c0eaSGlenn Lagasse * This function uses the obe_name member of this 1214f169c0eaSGlenn Lagasse * parameter to use as the BE name to search for. 1215f169c0eaSGlenn Lagasse * Upon successfully locating the BE, it populates 1216f169c0eaSGlenn Lagasse * obe_zpool with the pool name that the BE is found in. 1217f169c0eaSGlenn Lagasse * Returns: 1218f169c0eaSGlenn Lagasse * 1 - BE exists in this pool. 1219f169c0eaSGlenn Lagasse * 0 - BE does not exist in this pool. 1220f169c0eaSGlenn Lagasse * Scope: 1221f169c0eaSGlenn Lagasse * Semi-private (library wide use only) 1222f169c0eaSGlenn Lagasse */ 1223f169c0eaSGlenn Lagasse int 1224f169c0eaSGlenn Lagasse be_find_zpool_callback(zpool_handle_t *zlp, void *data) 1225f169c0eaSGlenn Lagasse { 1226f169c0eaSGlenn Lagasse be_transaction_data_t *bt = data; 1227f169c0eaSGlenn Lagasse const char *zpool = zpool_get_name(zlp); 1228f169c0eaSGlenn Lagasse char be_root_ds[MAXPATHLEN]; 1229f169c0eaSGlenn Lagasse 1230f169c0eaSGlenn Lagasse /* 1231f169c0eaSGlenn Lagasse * Generate string for the BE's root dataset 1232f169c0eaSGlenn Lagasse */ 1233f169c0eaSGlenn Lagasse be_make_root_ds(zpool, bt->obe_name, be_root_ds, sizeof (be_root_ds)); 1234f169c0eaSGlenn Lagasse 1235f169c0eaSGlenn Lagasse /* 1236f169c0eaSGlenn Lagasse * Check if dataset exists 1237f169c0eaSGlenn Lagasse */ 1238f169c0eaSGlenn Lagasse if (zfs_dataset_exists(g_zfs, be_root_ds, ZFS_TYPE_FILESYSTEM)) { 1239f169c0eaSGlenn Lagasse /* BE's root dataset exists in zpool */ 1240f169c0eaSGlenn Lagasse bt->obe_zpool = strdup(zpool); 1241f169c0eaSGlenn Lagasse zpool_close(zlp); 1242f169c0eaSGlenn Lagasse return (1); 1243f169c0eaSGlenn Lagasse } 1244f169c0eaSGlenn Lagasse 1245f169c0eaSGlenn Lagasse zpool_close(zlp); 1246f169c0eaSGlenn Lagasse return (0); 1247f169c0eaSGlenn Lagasse } 1248f169c0eaSGlenn Lagasse 1249f169c0eaSGlenn Lagasse /* 1250f169c0eaSGlenn Lagasse * Function: be_exists_callback 1251f169c0eaSGlenn Lagasse * Description: Callback function used to find out if a BE exists. 1252f169c0eaSGlenn Lagasse * Parameters: 1253f169c0eaSGlenn Lagasse * zlp - zpool_handle_t pointer to the current pool being 1254f169c0eaSGlenn Lagasse * looked at. 1255f169c0eaSGlenn Lagasse * data - BE name to look for. 1256f169c0eaSGlenn Lagasse * Return: 1257f169c0eaSGlenn Lagasse * 1 - BE exists in this pool. 1258f169c0eaSGlenn Lagasse * 0 - BE does not exist in this pool. 1259f169c0eaSGlenn Lagasse * Scope: 1260f169c0eaSGlenn Lagasse * Semi-private (library wide use only) 1261f169c0eaSGlenn Lagasse */ 1262f169c0eaSGlenn Lagasse int 1263f169c0eaSGlenn Lagasse be_exists_callback(zpool_handle_t *zlp, void *data) 1264f169c0eaSGlenn Lagasse { 1265f169c0eaSGlenn Lagasse const char *zpool = zpool_get_name(zlp); 1266f169c0eaSGlenn Lagasse char *be_name = data; 1267f169c0eaSGlenn Lagasse char be_root_ds[MAXPATHLEN]; 1268f169c0eaSGlenn Lagasse 1269f169c0eaSGlenn Lagasse /* 1270f169c0eaSGlenn Lagasse * Generate string for the BE's root dataset 1271f169c0eaSGlenn Lagasse */ 1272f169c0eaSGlenn Lagasse be_make_root_ds(zpool, be_name, be_root_ds, sizeof (be_root_ds)); 1273f169c0eaSGlenn Lagasse 1274f169c0eaSGlenn Lagasse /* 1275f169c0eaSGlenn Lagasse * Check if dataset exists 1276f169c0eaSGlenn Lagasse */ 1277f169c0eaSGlenn Lagasse if (zfs_dataset_exists(g_zfs, be_root_ds, ZFS_TYPE_FILESYSTEM)) { 1278f169c0eaSGlenn Lagasse /* BE's root dataset exists in zpool */ 1279f169c0eaSGlenn Lagasse zpool_close(zlp); 1280f169c0eaSGlenn Lagasse return (1); 1281f169c0eaSGlenn Lagasse } 1282f169c0eaSGlenn Lagasse 1283f169c0eaSGlenn Lagasse zpool_close(zlp); 1284f169c0eaSGlenn Lagasse return (0); 1285f169c0eaSGlenn Lagasse } 1286f169c0eaSGlenn Lagasse 12875ee7c793SAlexander Eremin /* 12885ee7c793SAlexander Eremin * Function: be_has_snapshots_callback 12895ee7c793SAlexander Eremin * Description: Callback function used to find out if a BE has snapshots. 12905ee7c793SAlexander Eremin * Parameters: 12915ee7c793SAlexander Eremin * zlp - zpool_handle_t pointer to the current pool being 12925ee7c793SAlexander Eremin * looked at. 12935ee7c793SAlexander Eremin * data - be_snap_found_t pointer. 12945ee7c793SAlexander Eremin * Return: 12955ee7c793SAlexander Eremin * 1 - BE has no snapshots. 12965ee7c793SAlexander Eremin * 0 - BE has snapshots. 12975ee7c793SAlexander Eremin * Scope: 12985ee7c793SAlexander Eremin * Private 12995ee7c793SAlexander Eremin */ 13005ee7c793SAlexander Eremin static int 13015ee7c793SAlexander Eremin be_has_snapshot_callback(zfs_handle_t *zhp, void *data) 13025ee7c793SAlexander Eremin { 13035ee7c793SAlexander Eremin boolean_t *bs = data; 13045ee7c793SAlexander Eremin if (zfs_get_name(zhp) == NULL) { 13055ee7c793SAlexander Eremin zfs_close(zhp); 13065ee7c793SAlexander Eremin return (1); 13075ee7c793SAlexander Eremin } 13085ee7c793SAlexander Eremin *bs = B_TRUE; 13095ee7c793SAlexander Eremin zfs_close(zhp); 13105ee7c793SAlexander Eremin return (0); 13115ee7c793SAlexander Eremin } 13125ee7c793SAlexander Eremin 1313f169c0eaSGlenn Lagasse /* 1314f169c0eaSGlenn Lagasse * Function: be_set_uuid 1315f169c0eaSGlenn Lagasse * Description: This function generates a uuid, unparses it into 1316f169c0eaSGlenn Lagasse * string representation, and sets that string into 1317f169c0eaSGlenn Lagasse * a zfs user property for a root dataset of a BE. 1318f169c0eaSGlenn Lagasse * The name of the user property used to store the 1319f169c0eaSGlenn Lagasse * uuid is org.opensolaris.libbe:uuid 1320f169c0eaSGlenn Lagasse * 1321f169c0eaSGlenn Lagasse * Parameters: 1322f169c0eaSGlenn Lagasse * root_ds - Root dataset of the BE to set a uuid on. 1323f169c0eaSGlenn Lagasse * Return: 1324f169c0eaSGlenn Lagasse * be_errno_t - Failure 1325f169c0eaSGlenn Lagasse * BE_SUCCESS - Success 1326f169c0eaSGlenn Lagasse * Scope: 1327f169c0eaSGlenn Lagasse * Semi-private (library wide ues only) 1328f169c0eaSGlenn Lagasse */ 1329f169c0eaSGlenn Lagasse int 1330f169c0eaSGlenn Lagasse be_set_uuid(char *root_ds) 1331f169c0eaSGlenn Lagasse { 1332f169c0eaSGlenn Lagasse zfs_handle_t *zhp = NULL; 1333f169c0eaSGlenn Lagasse uuid_t uu = { 0 }; 1334f169c0eaSGlenn Lagasse char uu_string[UUID_PRINTABLE_STRING_LENGTH] = { 0 }; 1335f169c0eaSGlenn Lagasse int ret = BE_SUCCESS; 1336f169c0eaSGlenn Lagasse 1337f169c0eaSGlenn Lagasse /* Generate a UUID and unparse it into string form */ 1338f169c0eaSGlenn Lagasse uuid_generate(uu); 1339f169c0eaSGlenn Lagasse if (uuid_is_null(uu) != 0) { 1340f169c0eaSGlenn Lagasse be_print_err(gettext("be_set_uuid: failed to " 1341f169c0eaSGlenn Lagasse "generate uuid\n")); 1342f169c0eaSGlenn Lagasse return (BE_ERR_GEN_UUID); 1343f169c0eaSGlenn Lagasse } 1344f169c0eaSGlenn Lagasse uuid_unparse(uu, uu_string); 1345f169c0eaSGlenn Lagasse 1346f169c0eaSGlenn Lagasse /* Get handle to the BE's root dataset. */ 1347f169c0eaSGlenn Lagasse if ((zhp = zfs_open(g_zfs, root_ds, ZFS_TYPE_FILESYSTEM)) == NULL) { 1348f169c0eaSGlenn Lagasse be_print_err(gettext("be_set_uuid: failed to " 1349f169c0eaSGlenn Lagasse "open BE root dataset (%s): %s\n"), root_ds, 1350f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs)); 1351f169c0eaSGlenn Lagasse return (zfs_err_to_be_err(g_zfs)); 1352f169c0eaSGlenn Lagasse } 1353f169c0eaSGlenn Lagasse 1354f169c0eaSGlenn Lagasse /* Set uuid property for the BE */ 1355f169c0eaSGlenn Lagasse if (zfs_prop_set(zhp, BE_UUID_PROPERTY, uu_string) != 0) { 1356f169c0eaSGlenn Lagasse be_print_err(gettext("be_set_uuid: failed to " 1357f169c0eaSGlenn Lagasse "set uuid property for BE: %s\n"), 1358f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs)); 1359f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs); 1360f169c0eaSGlenn Lagasse } 1361f169c0eaSGlenn Lagasse 1362f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 1363f169c0eaSGlenn Lagasse 1364f169c0eaSGlenn Lagasse return (ret); 1365f169c0eaSGlenn Lagasse } 1366f169c0eaSGlenn Lagasse 1367f169c0eaSGlenn Lagasse /* 1368f169c0eaSGlenn Lagasse * Function: be_get_uuid 1369f169c0eaSGlenn Lagasse * Description: This function gets the uuid string from a BE root 1370f169c0eaSGlenn Lagasse * dataset, parses it into internal format, and returns 1371f169c0eaSGlenn Lagasse * it the caller via a reference pointer passed in. 1372f169c0eaSGlenn Lagasse * 1373f169c0eaSGlenn Lagasse * Parameters: 1374f169c0eaSGlenn Lagasse * rootds - Root dataset of the BE to get the uuid from. 1375f169c0eaSGlenn Lagasse * uu - reference pointer to a uuid_t to return uuid in. 1376f169c0eaSGlenn Lagasse * Return: 1377f169c0eaSGlenn Lagasse * be_errno_t - Failure 1378f169c0eaSGlenn Lagasse * BE_SUCCESS - Success 1379f169c0eaSGlenn Lagasse * Scope: 1380f169c0eaSGlenn Lagasse * Semi-private (library wide use only) 1381f169c0eaSGlenn Lagasse */ 1382f169c0eaSGlenn Lagasse int 1383f169c0eaSGlenn Lagasse be_get_uuid(const char *root_ds, uuid_t *uu) 1384f169c0eaSGlenn Lagasse { 1385f169c0eaSGlenn Lagasse zfs_handle_t *zhp = NULL; 1386f169c0eaSGlenn Lagasse nvlist_t *userprops = NULL; 1387f169c0eaSGlenn Lagasse nvlist_t *propname = NULL; 1388f169c0eaSGlenn Lagasse char *uu_string = NULL; 1389f169c0eaSGlenn Lagasse int ret = BE_SUCCESS; 1390f169c0eaSGlenn Lagasse 1391f169c0eaSGlenn Lagasse /* Get handle to the BE's root dataset. */ 1392f169c0eaSGlenn Lagasse if ((zhp = zfs_open(g_zfs, root_ds, ZFS_TYPE_FILESYSTEM)) == NULL) { 1393f169c0eaSGlenn Lagasse be_print_err(gettext("be_get_uuid: failed to " 1394f169c0eaSGlenn Lagasse "open BE root dataset (%s): %s\n"), root_ds, 1395f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs)); 1396f169c0eaSGlenn Lagasse return (zfs_err_to_be_err(g_zfs)); 1397f169c0eaSGlenn Lagasse } 1398f169c0eaSGlenn Lagasse 1399f169c0eaSGlenn Lagasse /* Get user properties for BE's root dataset */ 1400f169c0eaSGlenn Lagasse if ((userprops = zfs_get_user_props(zhp)) == NULL) { 1401f169c0eaSGlenn Lagasse be_print_err(gettext("be_get_uuid: failed to " 1402f169c0eaSGlenn Lagasse "get user properties for BE root dataset (%s): %s\n"), 1403f169c0eaSGlenn Lagasse root_ds, libzfs_error_description(g_zfs)); 1404f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs); 1405f169c0eaSGlenn Lagasse goto done; 1406f169c0eaSGlenn Lagasse } 1407f169c0eaSGlenn Lagasse 1408f169c0eaSGlenn Lagasse /* Get UUID string from BE's root dataset user properties */ 1409f169c0eaSGlenn Lagasse if (nvlist_lookup_nvlist(userprops, BE_UUID_PROPERTY, &propname) != 0 || 1410f169c0eaSGlenn Lagasse nvlist_lookup_string(propname, ZPROP_VALUE, &uu_string) != 0) { 1411f169c0eaSGlenn Lagasse /* 1412f169c0eaSGlenn Lagasse * This probably just means that the BE is simply too old 1413f169c0eaSGlenn Lagasse * to have a uuid or that we haven't created a uuid for 1414f169c0eaSGlenn Lagasse * this BE yet. 1415f169c0eaSGlenn Lagasse */ 1416f169c0eaSGlenn Lagasse be_print_err(gettext("be_get_uuid: failed to " 1417f169c0eaSGlenn Lagasse "get uuid property from BE root dataset user " 1418f169c0eaSGlenn Lagasse "properties.\n")); 1419f169c0eaSGlenn Lagasse ret = BE_ERR_NO_UUID; 1420f169c0eaSGlenn Lagasse goto done; 1421f169c0eaSGlenn Lagasse } 1422f169c0eaSGlenn Lagasse /* Parse uuid string into internal format */ 1423f169c0eaSGlenn Lagasse if (uuid_parse(uu_string, *uu) != 0 || uuid_is_null(*uu)) { 1424f169c0eaSGlenn Lagasse be_print_err(gettext("be_get_uuid: failed to " 1425f169c0eaSGlenn Lagasse "parse uuid\n")); 1426f169c0eaSGlenn Lagasse ret = BE_ERR_PARSE_UUID; 1427f169c0eaSGlenn Lagasse goto done; 1428f169c0eaSGlenn Lagasse } 1429f169c0eaSGlenn Lagasse 1430f169c0eaSGlenn Lagasse done: 1431f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 1432f169c0eaSGlenn Lagasse return (ret); 1433f169c0eaSGlenn Lagasse } 1434f169c0eaSGlenn Lagasse 1435f169c0eaSGlenn Lagasse /* ******************************************************************** */ 1436f169c0eaSGlenn Lagasse /* Private Functions */ 1437f169c0eaSGlenn Lagasse /* ******************************************************************** */ 1438f169c0eaSGlenn Lagasse 1439f169c0eaSGlenn Lagasse /* 1440f169c0eaSGlenn Lagasse * Function: _be_destroy 1441f169c0eaSGlenn Lagasse * Description: Destroy a BE and all of its children datasets and snapshots. 1442f169c0eaSGlenn Lagasse * This function is called for both global BEs and non-global BEs. 1443f169c0eaSGlenn Lagasse * The root dataset of either the global BE or non-global BE to be 1444f169c0eaSGlenn Lagasse * destroyed is passed in. 1445f169c0eaSGlenn Lagasse * Parameters: 1446f169c0eaSGlenn Lagasse * root_ds - pointer to the name of the root dataset of the 1447f169c0eaSGlenn Lagasse * BE to destroy. 1448f169c0eaSGlenn Lagasse * dd - pointer to a be_destroy_data_t structure. 1449f169c0eaSGlenn Lagasse * 1450f169c0eaSGlenn Lagasse * Return: 1451f169c0eaSGlenn Lagasse * BE_SUCCESS - Success 1452f169c0eaSGlenn Lagasse * be_errno_t - Failure 1453f169c0eaSGlenn Lagasse * Scope: 1454f169c0eaSGlenn Lagasse * Private 1455f169c0eaSGlenn Lagasse */ 1456f169c0eaSGlenn Lagasse static int 1457f169c0eaSGlenn Lagasse _be_destroy(const char *root_ds, be_destroy_data_t *dd) 1458f169c0eaSGlenn Lagasse { 1459f169c0eaSGlenn Lagasse zfs_handle_t *zhp = NULL; 1460f169c0eaSGlenn Lagasse char origin[MAXPATHLEN]; 1461f169c0eaSGlenn Lagasse char parent[MAXPATHLEN]; 1462f169c0eaSGlenn Lagasse char *snap = NULL; 1463f169c0eaSGlenn Lagasse boolean_t has_origin = B_FALSE; 1464f169c0eaSGlenn Lagasse int ret = BE_SUCCESS; 1465f169c0eaSGlenn Lagasse 1466f169c0eaSGlenn Lagasse /* Get handle to BE's root dataset */ 1467f169c0eaSGlenn Lagasse if ((zhp = zfs_open(g_zfs, root_ds, ZFS_TYPE_FILESYSTEM)) == 1468f169c0eaSGlenn Lagasse NULL) { 1469f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy: failed to " 1470f169c0eaSGlenn Lagasse "open BE root dataset (%s): %s\n"), root_ds, 1471f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs)); 1472f169c0eaSGlenn Lagasse return (zfs_err_to_be_err(g_zfs)); 1473f169c0eaSGlenn Lagasse } 1474f169c0eaSGlenn Lagasse 1475f169c0eaSGlenn Lagasse /* 1476f169c0eaSGlenn Lagasse * Demote this BE in case it has dependent clones. This call 1477f169c0eaSGlenn Lagasse * will end up closing the zfs handle passed in whether it 1478f169c0eaSGlenn Lagasse * succeeds or fails. 1479f169c0eaSGlenn Lagasse */ 1480f169c0eaSGlenn Lagasse if (be_demote_callback(zhp, NULL) != 0) { 1481f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy: " 1482f169c0eaSGlenn Lagasse "failed to demote BE %s\n"), root_ds); 1483f169c0eaSGlenn Lagasse return (BE_ERR_DEMOTE); 1484f169c0eaSGlenn Lagasse } 1485f169c0eaSGlenn Lagasse 1486f169c0eaSGlenn Lagasse /* Get handle to BE's root dataset */ 1487f169c0eaSGlenn Lagasse if ((zhp = zfs_open(g_zfs, root_ds, ZFS_TYPE_FILESYSTEM)) == 1488f169c0eaSGlenn Lagasse NULL) { 1489f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy: failed to " 1490f169c0eaSGlenn Lagasse "open BE root dataset (%s): %s\n"), root_ds, 1491f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs)); 1492f169c0eaSGlenn Lagasse return (zfs_err_to_be_err(g_zfs)); 1493f169c0eaSGlenn Lagasse } 1494f169c0eaSGlenn Lagasse 1495f169c0eaSGlenn Lagasse /* 1496f169c0eaSGlenn Lagasse * Get the origin of this BE's root dataset. This will be used 1497f169c0eaSGlenn Lagasse * later to destroy the snapshots originally used to create this BE. 1498f169c0eaSGlenn Lagasse */ 1499f169c0eaSGlenn Lagasse if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, origin, sizeof (origin), NULL, 1500f169c0eaSGlenn Lagasse NULL, 0, B_FALSE) == 0) { 1501f169c0eaSGlenn Lagasse (void) strlcpy(parent, origin, sizeof (parent)); 1502f169c0eaSGlenn Lagasse if (be_get_snap(parent, &snap) != BE_SUCCESS) { 1503f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 1504f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy: failed to " 1505f169c0eaSGlenn Lagasse "get snapshot name from origin %s\n"), origin); 1506f169c0eaSGlenn Lagasse return (BE_ERR_INVAL); 1507f169c0eaSGlenn Lagasse } 1508f169c0eaSGlenn Lagasse has_origin = B_TRUE; 1509f169c0eaSGlenn Lagasse } 1510f169c0eaSGlenn Lagasse 1511f169c0eaSGlenn Lagasse /* 1512f169c0eaSGlenn Lagasse * Destroy the BE's root and its hierarchical children. This call 1513f169c0eaSGlenn Lagasse * will end up closing the zfs handle passed in whether it succeeds 1514f169c0eaSGlenn Lagasse * or fails. 1515f169c0eaSGlenn Lagasse */ 1516f169c0eaSGlenn Lagasse if (be_destroy_callback(zhp, dd) != 0) { 1517f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy: failed to " 1518f169c0eaSGlenn Lagasse "destroy BE %s\n"), root_ds); 1519de1ab35cSAlexander Eremin ret = zfs_err_to_be_err(g_zfs); 1520de1ab35cSAlexander Eremin return (ret); 1521f169c0eaSGlenn Lagasse } 1522f169c0eaSGlenn Lagasse 1523f169c0eaSGlenn Lagasse /* If BE has an origin */ 1524f169c0eaSGlenn Lagasse if (has_origin) { 1525f169c0eaSGlenn Lagasse 1526f169c0eaSGlenn Lagasse /* 1527f169c0eaSGlenn Lagasse * If origin snapshot doesn't have any other 1528f169c0eaSGlenn Lagasse * dependents, delete the origin. 1529f169c0eaSGlenn Lagasse */ 1530f169c0eaSGlenn Lagasse if ((zhp = zfs_open(g_zfs, origin, ZFS_TYPE_SNAPSHOT)) == 1531f169c0eaSGlenn Lagasse NULL) { 1532f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy: failed to " 1533f169c0eaSGlenn Lagasse "open BE's origin (%s): %s\n"), origin, 1534f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs)); 1535f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs); 1536f169c0eaSGlenn Lagasse return (ret); 1537f169c0eaSGlenn Lagasse } 1538f169c0eaSGlenn Lagasse 1539f169c0eaSGlenn Lagasse /* If origin has dependents, don't delete it. */ 1540f169c0eaSGlenn Lagasse if (zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES) != 0) { 1541f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 1542f169c0eaSGlenn Lagasse return (ret); 1543f169c0eaSGlenn Lagasse } 1544f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 1545f169c0eaSGlenn Lagasse 1546f169c0eaSGlenn Lagasse /* Get handle to BE's parent's root dataset */ 1547f169c0eaSGlenn Lagasse if ((zhp = zfs_open(g_zfs, parent, ZFS_TYPE_FILESYSTEM)) == 1548f169c0eaSGlenn Lagasse NULL) { 1549f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy: failed to " 1550f169c0eaSGlenn Lagasse "open BE's parent root dataset (%s): %s\n"), parent, 1551f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs)); 1552f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs); 1553f169c0eaSGlenn Lagasse return (ret); 1554f169c0eaSGlenn Lagasse } 1555f169c0eaSGlenn Lagasse 1556f169c0eaSGlenn Lagasse /* Destroy the snapshot origin used to create this BE. */ 1557f169c0eaSGlenn Lagasse /* 1558f169c0eaSGlenn Lagasse * The boolean set to B_FALSE and passed to zfs_destroy_snaps() 1559f169c0eaSGlenn Lagasse * tells zfs to process and destroy the snapshots now. 1560f169c0eaSGlenn Lagasse * Otherwise the call will potentially return where the 1561f169c0eaSGlenn Lagasse * snapshot isn't actually destroyed yet, and ZFS is waiting 1562f169c0eaSGlenn Lagasse * until all the references to the snapshot have been 1563f169c0eaSGlenn Lagasse * released before actually destroying the snapshot. 1564f169c0eaSGlenn Lagasse */ 1565f169c0eaSGlenn Lagasse if (zfs_destroy_snaps(zhp, snap, B_FALSE) != 0) { 1566f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy: failed to " 1567f169c0eaSGlenn Lagasse "destroy original snapshots used to create " 1568f169c0eaSGlenn Lagasse "BE: %s\n"), libzfs_error_description(g_zfs)); 1569f169c0eaSGlenn Lagasse 1570f169c0eaSGlenn Lagasse /* 1571f169c0eaSGlenn Lagasse * If a failure happened because a clone exists, 1572f169c0eaSGlenn Lagasse * don't return a failure to the user. Above, we're 1573f169c0eaSGlenn Lagasse * only checking that the root dataset's origin 1574f169c0eaSGlenn Lagasse * snapshot doesn't have dependent clones, but its 1575f169c0eaSGlenn Lagasse * possible that a subordinate dataset origin snapshot 1576f169c0eaSGlenn Lagasse * has a clone. We really need to check for that 1577f169c0eaSGlenn Lagasse * before trying to destroy the origin snapshot. 1578f169c0eaSGlenn Lagasse */ 1579f169c0eaSGlenn Lagasse if (libzfs_errno(g_zfs) != EZFS_EXISTS) { 1580f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs); 1581f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 1582f169c0eaSGlenn Lagasse return (ret); 1583f169c0eaSGlenn Lagasse } 1584f169c0eaSGlenn Lagasse } 1585f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 1586f169c0eaSGlenn Lagasse } 1587f169c0eaSGlenn Lagasse 1588f169c0eaSGlenn Lagasse return (ret); 1589f169c0eaSGlenn Lagasse } 1590f169c0eaSGlenn Lagasse 1591f169c0eaSGlenn Lagasse /* 1592f169c0eaSGlenn Lagasse * Function: be_destroy_zones 1593f169c0eaSGlenn Lagasse * Description: Find valid zone's and call be_destroy_zone_roots to destroy its 1594f169c0eaSGlenn Lagasse * corresponding dataset and all of its children datasets 1595f169c0eaSGlenn Lagasse * and snapshots. 1596f169c0eaSGlenn Lagasse * Parameters: 1597f169c0eaSGlenn Lagasse * be_name - name of global boot environment being destroyed 1598f169c0eaSGlenn Lagasse * be_root_ds - root dataset of global boot environment being 1599f169c0eaSGlenn Lagasse * destroyed. 1600f169c0eaSGlenn Lagasse * dd - be_destroy_data_t pointer 1601f169c0eaSGlenn Lagasse * Return: 1602f169c0eaSGlenn Lagasse * BE_SUCCESS - Success 1603f169c0eaSGlenn Lagasse * be_errno_t - Failure 1604f169c0eaSGlenn Lagasse * Scope: 1605f169c0eaSGlenn Lagasse * Private 1606f169c0eaSGlenn Lagasse * 1607f169c0eaSGlenn Lagasse * NOTES - Requires that the BE being deleted has no dependent BEs. If it 1608f169c0eaSGlenn Lagasse * does, the destroy will fail. 1609f169c0eaSGlenn Lagasse */ 1610f169c0eaSGlenn Lagasse static int 1611f169c0eaSGlenn Lagasse be_destroy_zones(char *be_name, char *be_root_ds, be_destroy_data_t *dd) 1612f169c0eaSGlenn Lagasse { 1613f169c0eaSGlenn Lagasse int i; 1614f169c0eaSGlenn Lagasse int ret = BE_SUCCESS; 1615f169c0eaSGlenn Lagasse int force_umnt = BE_UNMOUNT_FLAG_NULL; 1616f169c0eaSGlenn Lagasse char *zonepath = NULL; 1617f169c0eaSGlenn Lagasse char *zonename = NULL; 1618f169c0eaSGlenn Lagasse char *zonepath_ds = NULL; 1619f169c0eaSGlenn Lagasse char *mp = NULL; 1620f169c0eaSGlenn Lagasse zoneList_t zlist = NULL; 1621f169c0eaSGlenn Lagasse zoneBrandList_t *brands = NULL; 1622f169c0eaSGlenn Lagasse zfs_handle_t *zhp = NULL; 1623f169c0eaSGlenn Lagasse 1624f169c0eaSGlenn Lagasse /* If zones are not implemented, then get out. */ 1625f169c0eaSGlenn Lagasse if (!z_zones_are_implemented()) { 1626f169c0eaSGlenn Lagasse return (BE_SUCCESS); 1627f169c0eaSGlenn Lagasse } 1628f169c0eaSGlenn Lagasse 1629f169c0eaSGlenn Lagasse /* Get list of supported brands */ 1630f169c0eaSGlenn Lagasse if ((brands = be_get_supported_brandlist()) == NULL) { 1631f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy_zones: " 1632f169c0eaSGlenn Lagasse "no supported brands\n")); 1633f169c0eaSGlenn Lagasse return (BE_SUCCESS); 1634f169c0eaSGlenn Lagasse } 1635f169c0eaSGlenn Lagasse 1636f169c0eaSGlenn Lagasse /* Get handle to BE's root dataset */ 1637f169c0eaSGlenn Lagasse if ((zhp = zfs_open(g_zfs, be_root_ds, ZFS_TYPE_FILESYSTEM)) == 1638f169c0eaSGlenn Lagasse NULL) { 1639f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy_zones: failed to " 1640f169c0eaSGlenn Lagasse "open BE root dataset (%s): %s\n"), be_root_ds, 1641f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs)); 1642f169c0eaSGlenn Lagasse z_free_brand_list(brands); 1643f169c0eaSGlenn Lagasse return (zfs_err_to_be_err(g_zfs)); 1644f169c0eaSGlenn Lagasse } 1645f169c0eaSGlenn Lagasse 1646f169c0eaSGlenn Lagasse /* 1647f169c0eaSGlenn Lagasse * If the global BE is not mounted, we must mount it here to 1648f169c0eaSGlenn Lagasse * gather data about the non-global zones in it. 1649f169c0eaSGlenn Lagasse */ 1650f169c0eaSGlenn Lagasse if (!zfs_is_mounted(zhp, &mp)) { 1651f169c0eaSGlenn Lagasse if ((ret = _be_mount(be_name, &mp, 1652f169c0eaSGlenn Lagasse BE_MOUNT_FLAG_NO_ZONES)) != BE_SUCCESS) { 1653f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy_zones: failed to " 1654f169c0eaSGlenn Lagasse "mount the BE (%s) for zones processing.\n"), 1655f169c0eaSGlenn Lagasse be_name); 1656f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 1657f169c0eaSGlenn Lagasse z_free_brand_list(brands); 1658f169c0eaSGlenn Lagasse return (ret); 1659f169c0eaSGlenn Lagasse } 1660f169c0eaSGlenn Lagasse } 1661f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 1662f169c0eaSGlenn Lagasse 1663f169c0eaSGlenn Lagasse z_set_zone_root(mp); 1664f169c0eaSGlenn Lagasse free(mp); 1665f169c0eaSGlenn Lagasse 1666f169c0eaSGlenn Lagasse /* Get list of supported zones. */ 1667f169c0eaSGlenn Lagasse if ((zlist = z_get_nonglobal_zone_list_by_brand(brands)) == NULL) { 1668f169c0eaSGlenn Lagasse z_free_brand_list(brands); 1669f169c0eaSGlenn Lagasse return (BE_SUCCESS); 1670f169c0eaSGlenn Lagasse } 1671f169c0eaSGlenn Lagasse 1672f169c0eaSGlenn Lagasse /* Unmount the BE before destroying the zones in it. */ 1673f169c0eaSGlenn Lagasse if (dd->force_unmount) 1674f169c0eaSGlenn Lagasse force_umnt = BE_UNMOUNT_FLAG_FORCE; 1675f169c0eaSGlenn Lagasse if ((ret = _be_unmount(be_name, force_umnt)) != BE_SUCCESS) { 1676f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy_zones: failed to " 1677f169c0eaSGlenn Lagasse "unmount the BE (%s)\n"), be_name); 1678f169c0eaSGlenn Lagasse goto done; 1679f169c0eaSGlenn Lagasse } 1680f169c0eaSGlenn Lagasse 1681f169c0eaSGlenn Lagasse /* Iterate through the zones and destroy them. */ 1682f169c0eaSGlenn Lagasse for (i = 0; (zonename = z_zlist_get_zonename(zlist, i)) != NULL; i++) { 1683f169c0eaSGlenn Lagasse 1684f169c0eaSGlenn Lagasse /* Skip zones that aren't at least installed */ 1685f169c0eaSGlenn Lagasse if (z_zlist_get_current_state(zlist, i) < ZONE_STATE_INSTALLED) 1686f169c0eaSGlenn Lagasse continue; 1687f169c0eaSGlenn Lagasse 1688f169c0eaSGlenn Lagasse zonepath = z_zlist_get_zonepath(zlist, i); 1689f169c0eaSGlenn Lagasse 1690f169c0eaSGlenn Lagasse /* 1691f169c0eaSGlenn Lagasse * Get the dataset of this zonepath. If its not 1692f169c0eaSGlenn Lagasse * a dataset, skip it. 1693f169c0eaSGlenn Lagasse */ 1694f169c0eaSGlenn Lagasse if ((zonepath_ds = be_get_ds_from_dir(zonepath)) == NULL) 1695f169c0eaSGlenn Lagasse continue; 1696f169c0eaSGlenn Lagasse 1697f169c0eaSGlenn Lagasse /* 1698f169c0eaSGlenn Lagasse * Check if this zone is supported based on the 1699f169c0eaSGlenn Lagasse * dataset of its zonepath. 1700f169c0eaSGlenn Lagasse */ 1701f169c0eaSGlenn Lagasse if (!be_zone_supported(zonepath_ds)) { 1702f169c0eaSGlenn Lagasse free(zonepath_ds); 1703f169c0eaSGlenn Lagasse continue; 1704f169c0eaSGlenn Lagasse } 1705f169c0eaSGlenn Lagasse 1706f169c0eaSGlenn Lagasse /* Find the zone BE root datasets for this zone. */ 1707f169c0eaSGlenn Lagasse if ((ret = be_destroy_zone_roots(zonepath_ds, dd)) 1708f169c0eaSGlenn Lagasse != BE_SUCCESS) { 1709f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy_zones: failed to " 1710f169c0eaSGlenn Lagasse "find and destroy zone roots for zone %s\n"), 1711f169c0eaSGlenn Lagasse zonename); 1712f169c0eaSGlenn Lagasse free(zonepath_ds); 1713f169c0eaSGlenn Lagasse goto done; 1714f169c0eaSGlenn Lagasse } 1715f169c0eaSGlenn Lagasse free(zonepath_ds); 1716f169c0eaSGlenn Lagasse } 1717f169c0eaSGlenn Lagasse 1718f169c0eaSGlenn Lagasse done: 1719f169c0eaSGlenn Lagasse z_free_brand_list(brands); 1720f169c0eaSGlenn Lagasse z_free_zone_list(zlist); 1721f169c0eaSGlenn Lagasse 1722f169c0eaSGlenn Lagasse return (ret); 1723f169c0eaSGlenn Lagasse } 1724f169c0eaSGlenn Lagasse 1725f169c0eaSGlenn Lagasse /* 1726f169c0eaSGlenn Lagasse * Function: be_destroy_zone_roots 1727f169c0eaSGlenn Lagasse * Description: This function will open the zone's root container dataset 1728f169c0eaSGlenn Lagasse * and iterate the datasets within, looking for roots that 1729f169c0eaSGlenn Lagasse * belong to the given global BE and destroying them. 1730f169c0eaSGlenn Lagasse * If no other zone roots remain in the zone's root container 1731f169c0eaSGlenn Lagasse * dataset, the function will destroy it and the zone's 1732f169c0eaSGlenn Lagasse * zonepath dataset as well. 1733f169c0eaSGlenn Lagasse * Parameters: 1734f169c0eaSGlenn Lagasse * zonepath_ds - pointer to zone's zonepath dataset. 1735f169c0eaSGlenn Lagasse * dd - pointer to a linked destroy data. 1736f169c0eaSGlenn Lagasse * Returns: 1737f169c0eaSGlenn Lagasse * BE_SUCCESS - Success 1738f169c0eaSGlenn Lagasse * be_errno_t - Failure 1739f169c0eaSGlenn Lagasse * Scope: 1740f169c0eaSGlenn Lagasse * Private 1741f169c0eaSGlenn Lagasse */ 1742f169c0eaSGlenn Lagasse static int 1743f169c0eaSGlenn Lagasse be_destroy_zone_roots(char *zonepath_ds, be_destroy_data_t *dd) 1744f169c0eaSGlenn Lagasse { 1745f169c0eaSGlenn Lagasse zfs_handle_t *zhp; 1746f169c0eaSGlenn Lagasse char zone_container_ds[MAXPATHLEN]; 1747f169c0eaSGlenn Lagasse int ret = BE_SUCCESS; 1748f169c0eaSGlenn Lagasse 1749f169c0eaSGlenn Lagasse /* Generate string for the root container dataset for this zone. */ 1750f169c0eaSGlenn Lagasse be_make_container_ds(zonepath_ds, zone_container_ds, 1751f169c0eaSGlenn Lagasse sizeof (zone_container_ds)); 1752f169c0eaSGlenn Lagasse 1753f169c0eaSGlenn Lagasse /* Get handle to this zone's root container dataset. */ 1754f169c0eaSGlenn Lagasse if ((zhp = zfs_open(g_zfs, zone_container_ds, ZFS_TYPE_FILESYSTEM)) 1755f169c0eaSGlenn Lagasse == NULL) { 1756f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy_zone_roots: failed to " 1757f169c0eaSGlenn Lagasse "open zone root container dataset (%s): %s\n"), 1758f169c0eaSGlenn Lagasse zone_container_ds, libzfs_error_description(g_zfs)); 1759f169c0eaSGlenn Lagasse return (zfs_err_to_be_err(g_zfs)); 1760f169c0eaSGlenn Lagasse } 1761f169c0eaSGlenn Lagasse 1762f169c0eaSGlenn Lagasse /* 1763f169c0eaSGlenn Lagasse * Iterate through all of this zone's BEs, destroying the ones 1764f169c0eaSGlenn Lagasse * that belong to the parent global BE. 1765f169c0eaSGlenn Lagasse */ 1766f169c0eaSGlenn Lagasse if ((ret = zfs_iter_filesystems(zhp, be_destroy_zone_roots_callback, 1767f169c0eaSGlenn Lagasse dd)) != 0) { 1768f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy_zone_roots: failed to " 1769f169c0eaSGlenn Lagasse "destroy zone roots under zonepath dataset %s: %s\n"), 1770f169c0eaSGlenn Lagasse zonepath_ds, libzfs_error_description(g_zfs)); 1771f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 1772f169c0eaSGlenn Lagasse return (ret); 1773f169c0eaSGlenn Lagasse } 1774f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 1775f169c0eaSGlenn Lagasse 1776f169c0eaSGlenn Lagasse /* Get handle to this zone's root container dataset. */ 1777f169c0eaSGlenn Lagasse if ((zhp = zfs_open(g_zfs, zone_container_ds, ZFS_TYPE_FILESYSTEM)) 1778f169c0eaSGlenn Lagasse == NULL) { 1779f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy_zone_roots: failed to " 1780f169c0eaSGlenn Lagasse "open zone root container dataset (%s): %s\n"), 1781f169c0eaSGlenn Lagasse zone_container_ds, libzfs_error_description(g_zfs)); 1782f169c0eaSGlenn Lagasse return (zfs_err_to_be_err(g_zfs)); 1783f169c0eaSGlenn Lagasse } 1784f169c0eaSGlenn Lagasse 1785f169c0eaSGlenn Lagasse /* 1786f169c0eaSGlenn Lagasse * If there are no more zone roots in this zone's root container, 1787f169c0eaSGlenn Lagasse * dataset, destroy it and the zonepath dataset as well. 1788f169c0eaSGlenn Lagasse */ 1789f169c0eaSGlenn Lagasse if (zfs_iter_filesystems(zhp, be_zone_root_exists_callback, NULL) 1790f169c0eaSGlenn Lagasse == 0) { 1791f169c0eaSGlenn Lagasse /* Destroy the zone root container dataset */ 1792f169c0eaSGlenn Lagasse if (zfs_unmount(zhp, NULL, MS_FORCE) != 0 || 1793f169c0eaSGlenn Lagasse zfs_destroy(zhp, B_FALSE) != 0) { 1794f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy_zone_roots: failed to " 1795f169c0eaSGlenn Lagasse "destroy zone root container dataset (%s): %s\n"), 1796f169c0eaSGlenn Lagasse zone_container_ds, libzfs_error_description(g_zfs)); 1797f169c0eaSGlenn Lagasse goto done; 1798f169c0eaSGlenn Lagasse } 1799f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 1800f169c0eaSGlenn Lagasse 1801f169c0eaSGlenn Lagasse /* Get handle to zonepath dataset */ 1802f169c0eaSGlenn Lagasse if ((zhp = zfs_open(g_zfs, zonepath_ds, ZFS_TYPE_FILESYSTEM)) 1803f169c0eaSGlenn Lagasse == NULL) { 1804f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy_zone_roots: failed to " 1805f169c0eaSGlenn Lagasse "open zonepath dataset (%s): %s\n"), 1806f169c0eaSGlenn Lagasse zonepath_ds, libzfs_error_description(g_zfs)); 1807f169c0eaSGlenn Lagasse goto done; 1808f169c0eaSGlenn Lagasse } 1809f169c0eaSGlenn Lagasse 1810f169c0eaSGlenn Lagasse /* Destroy zonepath dataset */ 1811f169c0eaSGlenn Lagasse if (zfs_unmount(zhp, NULL, MS_FORCE) != 0 || 1812f169c0eaSGlenn Lagasse zfs_destroy(zhp, B_FALSE) != 0) { 1813f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy_zone_roots: " 1814f169c0eaSGlenn Lagasse "failed to destroy zonepath dataest %s: %s\n"), 1815f169c0eaSGlenn Lagasse zonepath_ds, libzfs_error_description(g_zfs)); 1816f169c0eaSGlenn Lagasse goto done; 1817f169c0eaSGlenn Lagasse } 1818f169c0eaSGlenn Lagasse } 1819f169c0eaSGlenn Lagasse 1820f169c0eaSGlenn Lagasse done: 1821f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 1822f169c0eaSGlenn Lagasse return (ret); 1823f169c0eaSGlenn Lagasse } 1824f169c0eaSGlenn Lagasse 1825f169c0eaSGlenn Lagasse /* 1826f169c0eaSGlenn Lagasse * Function: be_destroy_zone_roots_callback 1827f169c0eaSGlenn Lagasse * Description: This function is used as a callback to iterate over all of 1828f169c0eaSGlenn Lagasse * a zone's root datasets, finding the one's that 1829f169c0eaSGlenn Lagasse * correspond to the current BE. The name's 1830f169c0eaSGlenn Lagasse * of the zone root datasets are then destroyed by _be_destroy(). 1831f169c0eaSGlenn Lagasse * Parameters: 1832f169c0eaSGlenn Lagasse * zhp - zfs_handle_t pointer to current dataset being processed 1833f169c0eaSGlenn Lagasse * data - be_destroy_data_t pointer 1834f169c0eaSGlenn Lagasse * Returns: 1835f169c0eaSGlenn Lagasse * 0 - Success 1836f169c0eaSGlenn Lagasse * be_errno_t - Failure 1837f169c0eaSGlenn Lagasse * Scope: 1838f169c0eaSGlenn Lagasse * Private 1839f169c0eaSGlenn Lagasse */ 1840f169c0eaSGlenn Lagasse static int 1841f169c0eaSGlenn Lagasse be_destroy_zone_roots_callback(zfs_handle_t *zhp, void *data) 1842f169c0eaSGlenn Lagasse { 1843f169c0eaSGlenn Lagasse be_destroy_data_t *dd = data; 1844f169c0eaSGlenn Lagasse uuid_t parent_uuid = { 0 }; 1845f169c0eaSGlenn Lagasse int ret = 0; 1846f169c0eaSGlenn Lagasse 1847f169c0eaSGlenn Lagasse if (be_zone_get_parent_uuid(zfs_get_name(zhp), &parent_uuid) 1848f169c0eaSGlenn Lagasse != BE_SUCCESS) { 1849f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy_zone_roots_callback: " 1850f169c0eaSGlenn Lagasse "could not get parentuuid for zone root dataset %s\n"), 1851f169c0eaSGlenn Lagasse zfs_get_name(zhp)); 1852f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 1853f169c0eaSGlenn Lagasse return (0); 1854f169c0eaSGlenn Lagasse } 1855f169c0eaSGlenn Lagasse 1856f169c0eaSGlenn Lagasse if (uuid_compare(dd->gz_be_uuid, parent_uuid) == 0) { 1857f169c0eaSGlenn Lagasse /* 1858f169c0eaSGlenn Lagasse * Found a zone root dataset belonging to the parent 1859f169c0eaSGlenn Lagasse * BE being destroyed. Destroy this zone BE. 1860f169c0eaSGlenn Lagasse */ 1861f169c0eaSGlenn Lagasse if ((ret = _be_destroy(zfs_get_name(zhp), dd)) != BE_SUCCESS) { 1862f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy_zone_root_callback: " 1863f169c0eaSGlenn Lagasse "failed to destroy zone root %s\n"), 1864f169c0eaSGlenn Lagasse zfs_get_name(zhp)); 1865f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 1866f169c0eaSGlenn Lagasse return (ret); 1867f169c0eaSGlenn Lagasse } 1868f169c0eaSGlenn Lagasse } 1869f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 1870f169c0eaSGlenn Lagasse 1871f169c0eaSGlenn Lagasse return (ret); 1872f169c0eaSGlenn Lagasse } 1873f169c0eaSGlenn Lagasse 1874f169c0eaSGlenn Lagasse /* 1875f169c0eaSGlenn Lagasse * Function: be_copy_zones 1876f169c0eaSGlenn Lagasse * Description: Find valid zones and clone them to create their 1877f169c0eaSGlenn Lagasse * corresponding datasets for the BE being created. 1878f169c0eaSGlenn Lagasse * Parameters: 1879f169c0eaSGlenn Lagasse * obe_name - name of source global BE being copied. 1880f169c0eaSGlenn Lagasse * obe_root_ds - root dataset of source global BE being copied. 1881f169c0eaSGlenn Lagasse * nbe_root_ds - root dataset of target global BE. 1882f169c0eaSGlenn Lagasse * Return: 1883f169c0eaSGlenn Lagasse * BE_SUCCESS - Success 1884f169c0eaSGlenn Lagasse * be_errno_t - Failure 1885f169c0eaSGlenn Lagasse * Scope: 1886f169c0eaSGlenn Lagasse * Private 1887f169c0eaSGlenn Lagasse */ 1888f169c0eaSGlenn Lagasse static int 1889f169c0eaSGlenn Lagasse be_copy_zones(char *obe_name, char *obe_root_ds, char *nbe_root_ds) 1890f169c0eaSGlenn Lagasse { 1891f169c0eaSGlenn Lagasse int i, num_retries; 1892f169c0eaSGlenn Lagasse int ret = BE_SUCCESS; 1893f169c0eaSGlenn Lagasse int iret = 0; 1894f169c0eaSGlenn Lagasse char *zonename = NULL; 1895f169c0eaSGlenn Lagasse char *zonepath = NULL; 1896f169c0eaSGlenn Lagasse char *zone_be_name = NULL; 1897f169c0eaSGlenn Lagasse char *temp_mntpt = NULL; 1898f169c0eaSGlenn Lagasse char *new_zone_be_name = NULL; 1899f169c0eaSGlenn Lagasse char zoneroot[MAXPATHLEN]; 1900f169c0eaSGlenn Lagasse char zoneroot_ds[MAXPATHLEN]; 1901f169c0eaSGlenn Lagasse char zone_container_ds[MAXPATHLEN]; 1902f169c0eaSGlenn Lagasse char new_zoneroot_ds[MAXPATHLEN]; 1903f169c0eaSGlenn Lagasse char ss[MAXPATHLEN]; 1904f169c0eaSGlenn Lagasse uuid_t uu = { 0 }; 1905f169c0eaSGlenn Lagasse char uu_string[UUID_PRINTABLE_STRING_LENGTH] = { 0 }; 1906f169c0eaSGlenn Lagasse be_transaction_data_t bt = { 0 }; 1907f169c0eaSGlenn Lagasse zfs_handle_t *obe_zhp = NULL; 1908f169c0eaSGlenn Lagasse zfs_handle_t *nbe_zhp = NULL; 1909f169c0eaSGlenn Lagasse zfs_handle_t *z_zhp = NULL; 1910f169c0eaSGlenn Lagasse zoneList_t zlist = NULL; 1911f169c0eaSGlenn Lagasse zoneBrandList_t *brands = NULL; 1912f169c0eaSGlenn Lagasse boolean_t mounted_here = B_FALSE; 1913f169c0eaSGlenn Lagasse char *snap_name = NULL; 1914f169c0eaSGlenn Lagasse 1915f169c0eaSGlenn Lagasse /* If zones are not implemented, then get out. */ 1916f169c0eaSGlenn Lagasse if (!z_zones_are_implemented()) { 1917f169c0eaSGlenn Lagasse return (BE_SUCCESS); 1918f169c0eaSGlenn Lagasse } 1919f169c0eaSGlenn Lagasse 1920f169c0eaSGlenn Lagasse /* Get list of supported brands */ 1921f169c0eaSGlenn Lagasse if ((brands = be_get_supported_brandlist()) == NULL) { 1922f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy_zones: " 1923f169c0eaSGlenn Lagasse "no supported brands\n")); 1924f169c0eaSGlenn Lagasse return (BE_SUCCESS); 1925f169c0eaSGlenn Lagasse } 1926f169c0eaSGlenn Lagasse 1927f169c0eaSGlenn Lagasse /* Get handle to origin BE's root dataset */ 1928f169c0eaSGlenn Lagasse if ((obe_zhp = zfs_open(g_zfs, obe_root_ds, ZFS_TYPE_FILESYSTEM)) 1929f169c0eaSGlenn Lagasse == NULL) { 1930f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy_zones: failed to open " 1931f169c0eaSGlenn Lagasse "the origin BE root dataset (%s) for zones processing: " 1932f169c0eaSGlenn Lagasse "%s\n"), obe_root_ds, libzfs_error_description(g_zfs)); 1933f169c0eaSGlenn Lagasse return (zfs_err_to_be_err(g_zfs)); 1934f169c0eaSGlenn Lagasse } 1935f169c0eaSGlenn Lagasse 1936f169c0eaSGlenn Lagasse /* Get handle to newly cloned BE's root dataset */ 1937f169c0eaSGlenn Lagasse if ((nbe_zhp = zfs_open(g_zfs, nbe_root_ds, ZFS_TYPE_FILESYSTEM)) 1938f169c0eaSGlenn Lagasse == NULL) { 1939f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy_zones: failed to open " 1940f169c0eaSGlenn Lagasse "the new BE root dataset (%s): %s\n"), nbe_root_ds, 1941f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs)); 1942f169c0eaSGlenn Lagasse ZFS_CLOSE(obe_zhp); 1943f169c0eaSGlenn Lagasse return (zfs_err_to_be_err(g_zfs)); 1944f169c0eaSGlenn Lagasse } 1945f169c0eaSGlenn Lagasse 1946f169c0eaSGlenn Lagasse /* Get the uuid of the newly cloned parent BE. */ 1947f169c0eaSGlenn Lagasse if (be_get_uuid(zfs_get_name(nbe_zhp), &uu) != BE_SUCCESS) { 1948f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy_zones: " 1949f169c0eaSGlenn Lagasse "failed to get uuid for BE root " 1950f169c0eaSGlenn Lagasse "dataset %s\n"), zfs_get_name(nbe_zhp)); 1951f169c0eaSGlenn Lagasse ZFS_CLOSE(nbe_zhp); 1952f169c0eaSGlenn Lagasse goto done; 1953f169c0eaSGlenn Lagasse } 1954f169c0eaSGlenn Lagasse ZFS_CLOSE(nbe_zhp); 1955f169c0eaSGlenn Lagasse uuid_unparse(uu, uu_string); 1956f169c0eaSGlenn Lagasse 1957f169c0eaSGlenn Lagasse /* 1958f169c0eaSGlenn Lagasse * If the origin BE is not mounted, we must mount it here to 1959f169c0eaSGlenn Lagasse * gather data about the non-global zones in it. 1960f169c0eaSGlenn Lagasse */ 1961f169c0eaSGlenn Lagasse if (!zfs_is_mounted(obe_zhp, &temp_mntpt)) { 1962f169c0eaSGlenn Lagasse if ((ret = _be_mount(obe_name, &temp_mntpt, 1963f169c0eaSGlenn Lagasse BE_MOUNT_FLAG_NULL)) != BE_SUCCESS) { 1964f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy_zones: failed to " 1965f169c0eaSGlenn Lagasse "mount the BE (%s) for zones procesing.\n"), 1966f169c0eaSGlenn Lagasse obe_name); 1967f169c0eaSGlenn Lagasse goto done; 1968f169c0eaSGlenn Lagasse } 1969f169c0eaSGlenn Lagasse mounted_here = B_TRUE; 1970f169c0eaSGlenn Lagasse } 1971f169c0eaSGlenn Lagasse 1972f169c0eaSGlenn Lagasse z_set_zone_root(temp_mntpt); 1973f169c0eaSGlenn Lagasse 1974f169c0eaSGlenn Lagasse /* Get list of supported zones. */ 1975f169c0eaSGlenn Lagasse if ((zlist = z_get_nonglobal_zone_list_by_brand(brands)) == NULL) { 1976f169c0eaSGlenn Lagasse ret = BE_SUCCESS; 1977f169c0eaSGlenn Lagasse goto done; 1978f169c0eaSGlenn Lagasse } 1979f169c0eaSGlenn Lagasse 1980f169c0eaSGlenn Lagasse for (i = 0; (zonename = z_zlist_get_zonename(zlist, i)) != NULL; i++) { 1981f169c0eaSGlenn Lagasse 1982f169c0eaSGlenn Lagasse be_fs_list_data_t fld = { 0 }; 1983f169c0eaSGlenn Lagasse char zonepath_ds[MAXPATHLEN]; 1984f169c0eaSGlenn Lagasse char *ds = NULL; 1985f169c0eaSGlenn Lagasse 1986f169c0eaSGlenn Lagasse /* Get zonepath of zone */ 1987f169c0eaSGlenn Lagasse zonepath = z_zlist_get_zonepath(zlist, i); 1988f169c0eaSGlenn Lagasse 1989f169c0eaSGlenn Lagasse /* Skip zones that aren't at least installed */ 1990f169c0eaSGlenn Lagasse if (z_zlist_get_current_state(zlist, i) < ZONE_STATE_INSTALLED) 1991f169c0eaSGlenn Lagasse continue; 1992f169c0eaSGlenn Lagasse 1993f169c0eaSGlenn Lagasse /* 1994f169c0eaSGlenn Lagasse * Get the dataset of this zonepath. If its not 1995f169c0eaSGlenn Lagasse * a dataset, skip it. 1996f169c0eaSGlenn Lagasse */ 1997f169c0eaSGlenn Lagasse if ((ds = be_get_ds_from_dir(zonepath)) == NULL) 1998f169c0eaSGlenn Lagasse continue; 1999f169c0eaSGlenn Lagasse 2000f169c0eaSGlenn Lagasse (void) strlcpy(zonepath_ds, ds, sizeof (zonepath_ds)); 2001f169c0eaSGlenn Lagasse free(ds); 2002f169c0eaSGlenn Lagasse ds = NULL; 2003f169c0eaSGlenn Lagasse 2004f169c0eaSGlenn Lagasse /* Get zoneroot directory */ 2005f169c0eaSGlenn Lagasse be_make_zoneroot(zonepath, zoneroot, sizeof (zoneroot)); 2006f169c0eaSGlenn Lagasse 2007f169c0eaSGlenn Lagasse /* If zonepath dataset not supported, skip it. */ 2008f169c0eaSGlenn Lagasse if (!be_zone_supported(zonepath_ds)) { 2009f169c0eaSGlenn Lagasse continue; 2010f169c0eaSGlenn Lagasse } 2011f169c0eaSGlenn Lagasse 2012f169c0eaSGlenn Lagasse if ((ret = be_find_active_zone_root(obe_zhp, zonepath_ds, 2013f169c0eaSGlenn Lagasse zoneroot_ds, sizeof (zoneroot_ds))) != BE_SUCCESS) { 2014f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy_zones: " 2015f169c0eaSGlenn Lagasse "failed to find active zone root for zone %s " 2016f169c0eaSGlenn Lagasse "in BE %s\n"), zonename, obe_name); 2017f169c0eaSGlenn Lagasse goto done; 2018f169c0eaSGlenn Lagasse } 2019f169c0eaSGlenn Lagasse 2020f169c0eaSGlenn Lagasse be_make_container_ds(zonepath_ds, zone_container_ds, 2021f169c0eaSGlenn Lagasse sizeof (zone_container_ds)); 2022f169c0eaSGlenn Lagasse 2023f169c0eaSGlenn Lagasse if ((z_zhp = zfs_open(g_zfs, zoneroot_ds, 2024f169c0eaSGlenn Lagasse ZFS_TYPE_FILESYSTEM)) == NULL) { 2025f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy_zones: " 2026f169c0eaSGlenn Lagasse "failed to open zone root dataset (%s): %s\n"), 2027f169c0eaSGlenn Lagasse zoneroot_ds, libzfs_error_description(g_zfs)); 2028f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs); 2029f169c0eaSGlenn Lagasse goto done; 2030f169c0eaSGlenn Lagasse } 2031f169c0eaSGlenn Lagasse 2032f169c0eaSGlenn Lagasse zone_be_name = 2033f169c0eaSGlenn Lagasse be_get_zone_be_name(zoneroot_ds, zone_container_ds); 2034f169c0eaSGlenn Lagasse 2035f169c0eaSGlenn Lagasse if ((new_zone_be_name = be_auto_zone_be_name(zone_container_ds, 2036f169c0eaSGlenn Lagasse zone_be_name)) == NULL) { 2037f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy_zones: failed " 2038f169c0eaSGlenn Lagasse "to generate auto name for zone BE.\n")); 2039f169c0eaSGlenn Lagasse ret = BE_ERR_AUTONAME; 2040f169c0eaSGlenn Lagasse goto done; 2041f169c0eaSGlenn Lagasse } 2042f169c0eaSGlenn Lagasse 2043f169c0eaSGlenn Lagasse if ((snap_name = be_auto_snap_name()) == NULL) { 2044f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy_zones: failed to " 2045f169c0eaSGlenn Lagasse "generate snapshot name for zone BE.\n")); 2046f169c0eaSGlenn Lagasse ret = BE_ERR_AUTONAME; 2047f169c0eaSGlenn Lagasse goto done; 2048f169c0eaSGlenn Lagasse } 2049f169c0eaSGlenn Lagasse 2050f169c0eaSGlenn Lagasse (void) snprintf(ss, sizeof (ss), "%s@%s", zoneroot_ds, 2051f169c0eaSGlenn Lagasse snap_name); 2052f169c0eaSGlenn Lagasse 2053f169c0eaSGlenn Lagasse if (zfs_snapshot(g_zfs, ss, B_TRUE, NULL) != 0) { 2054f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy_zones: " 2055f169c0eaSGlenn Lagasse "failed to snapshot zone BE (%s): %s\n"), 2056f169c0eaSGlenn Lagasse ss, libzfs_error_description(g_zfs)); 2057f169c0eaSGlenn Lagasse if (libzfs_errno(g_zfs) == EZFS_EXISTS) 2058f169c0eaSGlenn Lagasse ret = BE_ERR_ZONE_SS_EXISTS; 2059f169c0eaSGlenn Lagasse else 2060f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs); 2061f169c0eaSGlenn Lagasse 2062f169c0eaSGlenn Lagasse goto done; 2063f169c0eaSGlenn Lagasse } 2064f169c0eaSGlenn Lagasse 2065f169c0eaSGlenn Lagasse (void) snprintf(new_zoneroot_ds, sizeof (new_zoneroot_ds), 2066f169c0eaSGlenn Lagasse "%s/%s", zone_container_ds, new_zone_be_name); 2067f169c0eaSGlenn Lagasse 2068f169c0eaSGlenn Lagasse bt.obe_name = zone_be_name; 2069f169c0eaSGlenn Lagasse bt.obe_root_ds = zoneroot_ds; 2070f169c0eaSGlenn Lagasse bt.obe_snap_name = snap_name; 2071f169c0eaSGlenn Lagasse bt.obe_altroot = temp_mntpt; 2072f169c0eaSGlenn Lagasse bt.nbe_name = new_zone_be_name; 2073f169c0eaSGlenn Lagasse bt.nbe_root_ds = new_zoneroot_ds; 2074f169c0eaSGlenn Lagasse 2075f169c0eaSGlenn Lagasse if (nvlist_alloc(&bt.nbe_zfs_props, NV_UNIQUE_NAME, 0) != 0) { 2076f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy_zones: " 2077f169c0eaSGlenn Lagasse "internal error: out of memory\n")); 2078f169c0eaSGlenn Lagasse ret = BE_ERR_NOMEM; 2079f169c0eaSGlenn Lagasse goto done; 2080f169c0eaSGlenn Lagasse } 2081f169c0eaSGlenn Lagasse 2082f169c0eaSGlenn Lagasse /* 2083f169c0eaSGlenn Lagasse * The call to be_clone_fs_callback always closes the 2084f169c0eaSGlenn Lagasse * zfs_handle so there's no need to close z_zhp. 2085f169c0eaSGlenn Lagasse */ 2086f169c0eaSGlenn Lagasse if ((iret = be_clone_fs_callback(z_zhp, &bt)) != 0) { 2087f169c0eaSGlenn Lagasse z_zhp = NULL; 2088f169c0eaSGlenn Lagasse if (iret != BE_ERR_BE_EXISTS) { 2089f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy_zones: " 2090f169c0eaSGlenn Lagasse "failed to create zone BE clone for new " 2091f169c0eaSGlenn Lagasse "zone BE %s\n"), new_zone_be_name); 2092f169c0eaSGlenn Lagasse ret = iret; 2093aab83bb8SJosef 'Jeff' Sipek nvlist_free(bt.nbe_zfs_props); 2094f169c0eaSGlenn Lagasse goto done; 2095f169c0eaSGlenn Lagasse } 2096f169c0eaSGlenn Lagasse /* 2097f169c0eaSGlenn Lagasse * We failed to create the new zone BE because a zone 2098f169c0eaSGlenn Lagasse * BE with the auto-name we generated above has since 2099f169c0eaSGlenn Lagasse * come into existence. Regenerate a new auto-name 2100f169c0eaSGlenn Lagasse * and retry. 2101f169c0eaSGlenn Lagasse */ 2102f169c0eaSGlenn Lagasse for (num_retries = 1; 2103f169c0eaSGlenn Lagasse num_retries < BE_AUTO_NAME_MAX_TRY; 2104f169c0eaSGlenn Lagasse num_retries++) { 2105f169c0eaSGlenn Lagasse 2106f169c0eaSGlenn Lagasse /* Sleep 1 before retrying */ 2107f169c0eaSGlenn Lagasse (void) sleep(1); 2108f169c0eaSGlenn Lagasse 2109f169c0eaSGlenn Lagasse /* Generate new auto zone BE name */ 2110f169c0eaSGlenn Lagasse free(new_zone_be_name); 2111f169c0eaSGlenn Lagasse if ((new_zone_be_name = be_auto_zone_be_name( 2112f169c0eaSGlenn Lagasse zone_container_ds, 2113f169c0eaSGlenn Lagasse zone_be_name)) == NULL) { 2114f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy_zones: " 2115f169c0eaSGlenn Lagasse "failed to generate auto name " 2116f169c0eaSGlenn Lagasse "for zone BE.\n")); 2117f169c0eaSGlenn Lagasse ret = BE_ERR_AUTONAME; 2118aab83bb8SJosef 'Jeff' Sipek nvlist_free(bt.nbe_zfs_props); 2119f169c0eaSGlenn Lagasse goto done; 2120f169c0eaSGlenn Lagasse } 2121f169c0eaSGlenn Lagasse 2122f169c0eaSGlenn Lagasse (void) snprintf(new_zoneroot_ds, 2123f169c0eaSGlenn Lagasse sizeof (new_zoneroot_ds), 2124f169c0eaSGlenn Lagasse "%s/%s", zone_container_ds, 2125f169c0eaSGlenn Lagasse new_zone_be_name); 2126f169c0eaSGlenn Lagasse bt.nbe_name = new_zone_be_name; 2127f169c0eaSGlenn Lagasse bt.nbe_root_ds = new_zoneroot_ds; 2128f169c0eaSGlenn Lagasse 2129f169c0eaSGlenn Lagasse /* 2130f169c0eaSGlenn Lagasse * Get handle to original zone BE's root 2131f169c0eaSGlenn Lagasse * dataset. 2132f169c0eaSGlenn Lagasse */ 2133f169c0eaSGlenn Lagasse if ((z_zhp = zfs_open(g_zfs, zoneroot_ds, 2134f169c0eaSGlenn Lagasse ZFS_TYPE_FILESYSTEM)) == NULL) { 2135f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy_zones: " 2136f169c0eaSGlenn Lagasse "failed to open zone root " 2137f169c0eaSGlenn Lagasse "dataset (%s): %s\n"), 2138f169c0eaSGlenn Lagasse zoneroot_ds, 2139f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs)); 2140f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs); 2141aab83bb8SJosef 'Jeff' Sipek nvlist_free(bt.nbe_zfs_props); 2142f169c0eaSGlenn Lagasse goto done; 2143f169c0eaSGlenn Lagasse } 2144f169c0eaSGlenn Lagasse 2145f169c0eaSGlenn Lagasse /* 2146f169c0eaSGlenn Lagasse * Try to clone the zone BE again. This 2147f169c0eaSGlenn Lagasse * call will end up closing the zfs 2148f169c0eaSGlenn Lagasse * handle passed in whether it 2149f169c0eaSGlenn Lagasse * succeeds or fails. 2150f169c0eaSGlenn Lagasse */ 2151f169c0eaSGlenn Lagasse iret = be_clone_fs_callback(z_zhp, &bt); 2152f169c0eaSGlenn Lagasse z_zhp = NULL; 2153f169c0eaSGlenn Lagasse if (iret == 0) { 2154f169c0eaSGlenn Lagasse break; 2155f169c0eaSGlenn Lagasse } else if (iret != BE_ERR_BE_EXISTS) { 2156f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy_zones: " 2157f169c0eaSGlenn Lagasse "failed to create zone BE clone " 2158f169c0eaSGlenn Lagasse "for new zone BE %s\n"), 2159f169c0eaSGlenn Lagasse new_zone_be_name); 2160f169c0eaSGlenn Lagasse ret = iret; 2161aab83bb8SJosef 'Jeff' Sipek nvlist_free(bt.nbe_zfs_props); 2162f169c0eaSGlenn Lagasse goto done; 2163f169c0eaSGlenn Lagasse } 2164f169c0eaSGlenn Lagasse } 2165f169c0eaSGlenn Lagasse /* 2166f169c0eaSGlenn Lagasse * If we've exhausted the maximum number of 2167f169c0eaSGlenn Lagasse * tries, free the auto zone BE name and return 2168f169c0eaSGlenn Lagasse * error. 2169f169c0eaSGlenn Lagasse */ 2170f169c0eaSGlenn Lagasse if (num_retries == BE_AUTO_NAME_MAX_TRY) { 2171f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy_zones: failed " 2172f169c0eaSGlenn Lagasse "to create a unique auto zone BE name\n")); 2173f169c0eaSGlenn Lagasse free(bt.nbe_name); 2174f169c0eaSGlenn Lagasse bt.nbe_name = NULL; 2175f169c0eaSGlenn Lagasse ret = BE_ERR_AUTONAME; 2176aab83bb8SJosef 'Jeff' Sipek nvlist_free(bt.nbe_zfs_props); 2177f169c0eaSGlenn Lagasse goto done; 2178f169c0eaSGlenn Lagasse } 2179f169c0eaSGlenn Lagasse } 2180f169c0eaSGlenn Lagasse 2181aab83bb8SJosef 'Jeff' Sipek nvlist_free(bt.nbe_zfs_props); 2182f169c0eaSGlenn Lagasse 2183f169c0eaSGlenn Lagasse z_zhp = NULL; 2184f169c0eaSGlenn Lagasse 2185f169c0eaSGlenn Lagasse if ((z_zhp = zfs_open(g_zfs, new_zoneroot_ds, 2186f169c0eaSGlenn Lagasse ZFS_TYPE_FILESYSTEM)) == NULL) { 2187f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy_zones: " 2188f169c0eaSGlenn Lagasse "failed to open the new zone BE root dataset " 2189f169c0eaSGlenn Lagasse "(%s): %s\n"), new_zoneroot_ds, 2190f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs)); 2191f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs); 2192f169c0eaSGlenn Lagasse goto done; 2193f169c0eaSGlenn Lagasse } 2194f169c0eaSGlenn Lagasse 2195f169c0eaSGlenn Lagasse if (zfs_prop_set(z_zhp, BE_ZONE_PARENTBE_PROPERTY, 2196f169c0eaSGlenn Lagasse uu_string) != 0) { 2197f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy_zones: " 2198f169c0eaSGlenn Lagasse "failed to set parentbe property\n")); 2199f169c0eaSGlenn Lagasse ZFS_CLOSE(z_zhp); 2200f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs); 2201f169c0eaSGlenn Lagasse goto done; 2202f169c0eaSGlenn Lagasse } 2203f169c0eaSGlenn Lagasse 2204f169c0eaSGlenn Lagasse if (zfs_prop_set(z_zhp, BE_ZONE_ACTIVE_PROPERTY, "on") != 0) { 2205f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy_zones: " 2206f169c0eaSGlenn Lagasse "failed to set active property\n")); 2207f169c0eaSGlenn Lagasse ZFS_CLOSE(z_zhp); 2208f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs); 2209f169c0eaSGlenn Lagasse goto done; 2210f169c0eaSGlenn Lagasse } 2211f169c0eaSGlenn Lagasse 2212f169c0eaSGlenn Lagasse /* 2213f169c0eaSGlenn Lagasse * Generate a list of file systems from the original 2214f169c0eaSGlenn Lagasse * zone BE that are legacy mounted. We use this list 2215f169c0eaSGlenn Lagasse * to determine which entries in the vfstab we need to 2216f169c0eaSGlenn Lagasse * update for the new zone BE we've just created. 2217f169c0eaSGlenn Lagasse */ 2218f169c0eaSGlenn Lagasse if ((ret = be_get_legacy_fs(obe_name, obe_root_ds, 2219f169c0eaSGlenn Lagasse zoneroot_ds, zoneroot, &fld)) != BE_SUCCESS) { 2220f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy_zones: " 2221f169c0eaSGlenn Lagasse "failed to get legacy mounted file system " 2222f169c0eaSGlenn Lagasse "list for zone %s\n"), zonename); 2223f169c0eaSGlenn Lagasse ZFS_CLOSE(z_zhp); 2224f169c0eaSGlenn Lagasse goto done; 2225f169c0eaSGlenn Lagasse } 2226f169c0eaSGlenn Lagasse 2227f169c0eaSGlenn Lagasse /* 2228f169c0eaSGlenn Lagasse * Update new zone BE's vfstab. 2229f169c0eaSGlenn Lagasse */ 2230f169c0eaSGlenn Lagasse if ((ret = be_update_zone_vfstab(z_zhp, bt.nbe_name, 2231f169c0eaSGlenn Lagasse zonepath_ds, zonepath_ds, &fld)) != BE_SUCCESS) { 2232f169c0eaSGlenn Lagasse be_print_err(gettext("be_copy_zones: " 2233f169c0eaSGlenn Lagasse "failed to update new BE's vfstab (%s)\n"), 2234f169c0eaSGlenn Lagasse bt.nbe_name); 2235f169c0eaSGlenn Lagasse ZFS_CLOSE(z_zhp); 2236f169c0eaSGlenn Lagasse be_free_fs_list(&fld); 2237f169c0eaSGlenn Lagasse goto done; 2238f169c0eaSGlenn Lagasse } 2239f169c0eaSGlenn Lagasse 2240f169c0eaSGlenn Lagasse be_free_fs_list(&fld); 2241f169c0eaSGlenn Lagasse ZFS_CLOSE(z_zhp); 2242f169c0eaSGlenn Lagasse } 2243f169c0eaSGlenn Lagasse 2244f169c0eaSGlenn Lagasse done: 2245f169c0eaSGlenn Lagasse free(snap_name); 2246f169c0eaSGlenn Lagasse if (brands != NULL) 2247f169c0eaSGlenn Lagasse z_free_brand_list(brands); 2248f169c0eaSGlenn Lagasse if (zlist != NULL) 2249f169c0eaSGlenn Lagasse z_free_zone_list(zlist); 2250f169c0eaSGlenn Lagasse 2251f169c0eaSGlenn Lagasse if (mounted_here) 2252f169c0eaSGlenn Lagasse (void) _be_unmount(obe_name, 0); 2253f169c0eaSGlenn Lagasse 2254f169c0eaSGlenn Lagasse ZFS_CLOSE(obe_zhp); 2255f169c0eaSGlenn Lagasse return (ret); 2256f169c0eaSGlenn Lagasse } 2257f169c0eaSGlenn Lagasse 2258f169c0eaSGlenn Lagasse /* 2259f169c0eaSGlenn Lagasse * Function: be_clone_fs_callback 2260f169c0eaSGlenn Lagasse * Description: Callback function used to iterate through a BE's filesystems 2261f169c0eaSGlenn Lagasse * to clone them for the new BE. 2262f169c0eaSGlenn Lagasse * Parameters: 2263f169c0eaSGlenn Lagasse * zhp - zfs_handle_t pointer for the filesystem being processed. 2264f169c0eaSGlenn Lagasse * data - be_transaction_data_t pointer providing information 2265f169c0eaSGlenn Lagasse * about original BE and new BE. 2266f169c0eaSGlenn Lagasse * Return: 2267f169c0eaSGlenn Lagasse * 0 - Success 2268f169c0eaSGlenn Lagasse * be_errno_t - Failure 2269f169c0eaSGlenn Lagasse * Scope: 2270f169c0eaSGlenn Lagasse * Private 2271f169c0eaSGlenn Lagasse */ 2272f169c0eaSGlenn Lagasse static int 2273f169c0eaSGlenn Lagasse be_clone_fs_callback(zfs_handle_t *zhp, void *data) 2274f169c0eaSGlenn Lagasse { 2275f169c0eaSGlenn Lagasse be_transaction_data_t *bt = data; 2276f169c0eaSGlenn Lagasse zfs_handle_t *zhp_ss = NULL; 2277f169c0eaSGlenn Lagasse char prop_buf[MAXPATHLEN]; 2278*9adfa60dSMatthew Ahrens char zhp_name[ZFS_MAX_DATASET_NAME_LEN]; 2279f169c0eaSGlenn Lagasse char clone_ds[MAXPATHLEN]; 2280f169c0eaSGlenn Lagasse char ss[MAXPATHLEN]; 2281f169c0eaSGlenn Lagasse int ret = 0; 2282f169c0eaSGlenn Lagasse 2283f169c0eaSGlenn Lagasse if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, prop_buf, 2284f169c0eaSGlenn Lagasse ZFS_MAXPROPLEN, NULL, NULL, 0, B_FALSE) != 0) { 2285f169c0eaSGlenn Lagasse be_print_err(gettext("be_clone_fs_callback: " 2286f169c0eaSGlenn Lagasse "failed to get dataset mountpoint (%s): %s\n"), 2287f169c0eaSGlenn Lagasse zfs_get_name(zhp), libzfs_error_description(g_zfs)); 2288f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs); 2289f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 2290f169c0eaSGlenn Lagasse return (ret); 2291f169c0eaSGlenn Lagasse } 2292f169c0eaSGlenn Lagasse 2293f169c0eaSGlenn Lagasse if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) != 0 && 2294f169c0eaSGlenn Lagasse strcmp(prop_buf, "legacy") != 0) { 2295f169c0eaSGlenn Lagasse /* 2296f169c0eaSGlenn Lagasse * Since zfs can't currently handle setting the 2297f169c0eaSGlenn Lagasse * mountpoint for a zoned dataset we'll have to skip 2298f169c0eaSGlenn Lagasse * this dataset. This is because the mountpoint is not 2299f169c0eaSGlenn Lagasse * set to "legacy". 2300f169c0eaSGlenn Lagasse */ 2301f169c0eaSGlenn Lagasse goto zoned; 2302f169c0eaSGlenn Lagasse } 2303f169c0eaSGlenn Lagasse /* 2304f169c0eaSGlenn Lagasse * Get a copy of the dataset name from the zfs handle 2305f169c0eaSGlenn Lagasse */ 2306f169c0eaSGlenn Lagasse (void) strlcpy(zhp_name, zfs_get_name(zhp), sizeof (zhp_name)); 2307f169c0eaSGlenn Lagasse 2308f169c0eaSGlenn Lagasse /* 2309f169c0eaSGlenn Lagasse * Get the clone dataset name and prepare the zfs properties for it. 2310f169c0eaSGlenn Lagasse */ 2311f169c0eaSGlenn Lagasse if ((ret = be_prep_clone_send_fs(zhp, bt, clone_ds, 2312f169c0eaSGlenn Lagasse sizeof (clone_ds))) != BE_SUCCESS) { 2313f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 2314f169c0eaSGlenn Lagasse return (ret); 2315f169c0eaSGlenn Lagasse } 2316f169c0eaSGlenn Lagasse 2317f169c0eaSGlenn Lagasse /* 2318f169c0eaSGlenn Lagasse * Generate the name of the snapshot to use. 2319f169c0eaSGlenn Lagasse */ 2320f169c0eaSGlenn Lagasse (void) snprintf(ss, sizeof (ss), "%s@%s", zhp_name, 2321f169c0eaSGlenn Lagasse bt->obe_snap_name); 2322f169c0eaSGlenn Lagasse 2323f169c0eaSGlenn Lagasse /* 2324f169c0eaSGlenn Lagasse * Get handle to snapshot. 2325f169c0eaSGlenn Lagasse */ 2326f169c0eaSGlenn Lagasse if ((zhp_ss = zfs_open(g_zfs, ss, ZFS_TYPE_SNAPSHOT)) == NULL) { 2327f169c0eaSGlenn Lagasse be_print_err(gettext("be_clone_fs_callback: " 2328f169c0eaSGlenn Lagasse "failed to get handle to snapshot (%s): %s\n"), ss, 2329f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs)); 2330f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs); 2331f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 2332f169c0eaSGlenn Lagasse return (ret); 2333f169c0eaSGlenn Lagasse } 2334f169c0eaSGlenn Lagasse 2335f169c0eaSGlenn Lagasse /* 2336f169c0eaSGlenn Lagasse * Clone the dataset. 2337f169c0eaSGlenn Lagasse */ 2338f169c0eaSGlenn Lagasse if (zfs_clone(zhp_ss, clone_ds, bt->nbe_zfs_props) != 0) { 2339f169c0eaSGlenn Lagasse be_print_err(gettext("be_clone_fs_callback: " 2340f169c0eaSGlenn Lagasse "failed to create clone dataset (%s): %s\n"), 2341f169c0eaSGlenn Lagasse clone_ds, libzfs_error_description(g_zfs)); 2342f169c0eaSGlenn Lagasse 2343f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp_ss); 2344f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 2345f169c0eaSGlenn Lagasse 2346f169c0eaSGlenn Lagasse return (zfs_err_to_be_err(g_zfs)); 2347f169c0eaSGlenn Lagasse } 2348f169c0eaSGlenn Lagasse 2349f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp_ss); 2350f169c0eaSGlenn Lagasse 2351f169c0eaSGlenn Lagasse zoned: 2352f169c0eaSGlenn Lagasse /* 2353f169c0eaSGlenn Lagasse * Iterate through zhp's children datasets (if any) 2354f169c0eaSGlenn Lagasse * and clone them accordingly. 2355f169c0eaSGlenn Lagasse */ 2356f169c0eaSGlenn Lagasse if ((ret = zfs_iter_filesystems(zhp, be_clone_fs_callback, bt)) != 0) { 2357f169c0eaSGlenn Lagasse /* 2358f169c0eaSGlenn Lagasse * Error occurred while processing a child dataset. 2359f169c0eaSGlenn Lagasse * Destroy this dataset and return error. 2360f169c0eaSGlenn Lagasse */ 2361f169c0eaSGlenn Lagasse zfs_handle_t *d_zhp = NULL; 2362f169c0eaSGlenn Lagasse 2363f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 2364f169c0eaSGlenn Lagasse 2365f169c0eaSGlenn Lagasse if ((d_zhp = zfs_open(g_zfs, clone_ds, ZFS_TYPE_FILESYSTEM)) 2366f169c0eaSGlenn Lagasse == NULL) { 2367f169c0eaSGlenn Lagasse return (ret); 2368f169c0eaSGlenn Lagasse } 2369f169c0eaSGlenn Lagasse 2370f169c0eaSGlenn Lagasse (void) zfs_destroy(d_zhp, B_FALSE); 2371f169c0eaSGlenn Lagasse ZFS_CLOSE(d_zhp); 2372f169c0eaSGlenn Lagasse return (ret); 2373f169c0eaSGlenn Lagasse } 2374f169c0eaSGlenn Lagasse 2375f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 2376f169c0eaSGlenn Lagasse return (0); 2377f169c0eaSGlenn Lagasse } 2378f169c0eaSGlenn Lagasse 2379f169c0eaSGlenn Lagasse /* 2380f169c0eaSGlenn Lagasse * Function: be_send_fs_callback 2381f169c0eaSGlenn Lagasse * Description: Callback function used to iterate through a BE's filesystems 2382f169c0eaSGlenn Lagasse * to copy them for the new BE. 2383f169c0eaSGlenn Lagasse * Parameters: 2384f169c0eaSGlenn Lagasse * zhp - zfs_handle_t pointer for the filesystem being processed. 2385f169c0eaSGlenn Lagasse * data - be_transaction_data_t pointer providing information 2386f169c0eaSGlenn Lagasse * about original BE and new BE. 2387f169c0eaSGlenn Lagasse * Return: 2388f169c0eaSGlenn Lagasse * 0 - Success 2389f169c0eaSGlenn Lagasse * be_errnot_t - Failure 2390f169c0eaSGlenn Lagasse * Scope: 2391f169c0eaSGlenn Lagasse * Private 2392f169c0eaSGlenn Lagasse */ 2393f169c0eaSGlenn Lagasse static int 2394f169c0eaSGlenn Lagasse be_send_fs_callback(zfs_handle_t *zhp, void *data) 2395f169c0eaSGlenn Lagasse { 2396f169c0eaSGlenn Lagasse be_transaction_data_t *bt = data; 2397f169c0eaSGlenn Lagasse recvflags_t flags = { 0 }; 2398*9adfa60dSMatthew Ahrens char zhp_name[ZFS_MAX_DATASET_NAME_LEN]; 2399f169c0eaSGlenn Lagasse char clone_ds[MAXPATHLEN]; 2400f169c0eaSGlenn Lagasse sendflags_t send_flags = { 0 }; 2401f169c0eaSGlenn Lagasse int pid, status, retval; 2402f169c0eaSGlenn Lagasse int srpipe[2]; 2403f169c0eaSGlenn Lagasse int ret = 0; 2404f169c0eaSGlenn Lagasse 2405f169c0eaSGlenn Lagasse /* 2406f169c0eaSGlenn Lagasse * Get a copy of the dataset name from the zfs handle 2407f169c0eaSGlenn Lagasse */ 2408f169c0eaSGlenn Lagasse (void) strlcpy(zhp_name, zfs_get_name(zhp), sizeof (zhp_name)); 2409f169c0eaSGlenn Lagasse 2410f169c0eaSGlenn Lagasse /* 2411f169c0eaSGlenn Lagasse * Get the clone dataset name and prepare the zfs properties for it. 2412f169c0eaSGlenn Lagasse */ 2413f169c0eaSGlenn Lagasse if ((ret = be_prep_clone_send_fs(zhp, bt, clone_ds, 2414f169c0eaSGlenn Lagasse sizeof (clone_ds))) != BE_SUCCESS) { 2415f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 2416f169c0eaSGlenn Lagasse return (ret); 2417f169c0eaSGlenn Lagasse } 2418f169c0eaSGlenn Lagasse 2419f169c0eaSGlenn Lagasse /* 2420f169c0eaSGlenn Lagasse * Create the new dataset. 2421f169c0eaSGlenn Lagasse */ 2422f169c0eaSGlenn Lagasse if (zfs_create(g_zfs, clone_ds, ZFS_TYPE_FILESYSTEM, bt->nbe_zfs_props) 2423f169c0eaSGlenn Lagasse != 0) { 2424f169c0eaSGlenn Lagasse be_print_err(gettext("be_send_fs_callback: " 2425f169c0eaSGlenn Lagasse "failed to create new dataset '%s': %s\n"), 2426f169c0eaSGlenn Lagasse clone_ds, libzfs_error_description(g_zfs)); 2427f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs); 2428f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 2429f169c0eaSGlenn Lagasse return (ret); 2430f169c0eaSGlenn Lagasse } 2431f169c0eaSGlenn Lagasse 2432f169c0eaSGlenn Lagasse /* 2433f169c0eaSGlenn Lagasse * Destination file system is already created 2434f169c0eaSGlenn Lagasse * hence we need to set the force flag on 2435f169c0eaSGlenn Lagasse */ 2436f169c0eaSGlenn Lagasse flags.force = B_TRUE; 2437f169c0eaSGlenn Lagasse 2438f169c0eaSGlenn Lagasse /* 2439f169c0eaSGlenn Lagasse * Initiate the pipe to be used for the send and recv 2440f169c0eaSGlenn Lagasse */ 2441f169c0eaSGlenn Lagasse if (pipe(srpipe) != 0) { 2442f169c0eaSGlenn Lagasse int err = errno; 2443f169c0eaSGlenn Lagasse be_print_err(gettext("be_send_fs_callback: failed to " 2444f169c0eaSGlenn Lagasse "open pipe\n")); 2445f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 2446f169c0eaSGlenn Lagasse return (errno_to_be_err(err)); 2447f169c0eaSGlenn Lagasse } 2448f169c0eaSGlenn Lagasse 2449f169c0eaSGlenn Lagasse /* 2450f169c0eaSGlenn Lagasse * Fork off a child to send the dataset 2451f169c0eaSGlenn Lagasse */ 2452f169c0eaSGlenn Lagasse if ((pid = fork()) == -1) { 2453f169c0eaSGlenn Lagasse int err = errno; 2454f169c0eaSGlenn Lagasse be_print_err(gettext("be_send_fs_callback: failed to fork\n")); 2455f169c0eaSGlenn Lagasse (void) close(srpipe[0]); 2456f169c0eaSGlenn Lagasse (void) close(srpipe[1]); 2457f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 2458f169c0eaSGlenn Lagasse return (errno_to_be_err(err)); 2459f169c0eaSGlenn Lagasse } else if (pid == 0) { /* child process */ 2460f169c0eaSGlenn Lagasse (void) close(srpipe[0]); 2461f169c0eaSGlenn Lagasse 2462f169c0eaSGlenn Lagasse /* Send dataset */ 246319b94df9SMatthew Ahrens if (zfs_send(zhp, NULL, bt->obe_snap_name, &send_flags, 2464f169c0eaSGlenn Lagasse srpipe[1], NULL, NULL, NULL) != 0) { 2465f169c0eaSGlenn Lagasse _exit(1); 2466f169c0eaSGlenn Lagasse } 2467f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 2468f169c0eaSGlenn Lagasse 2469f169c0eaSGlenn Lagasse _exit(0); 2470f169c0eaSGlenn Lagasse } 2471f169c0eaSGlenn Lagasse 2472f169c0eaSGlenn Lagasse (void) close(srpipe[1]); 2473f169c0eaSGlenn Lagasse 2474f169c0eaSGlenn Lagasse /* Receive dataset */ 2475a2cdcdd2SPaul Dagnelie if (zfs_receive(g_zfs, clone_ds, NULL, &flags, srpipe[0], NULL) != 0) { 2476f169c0eaSGlenn Lagasse be_print_err(gettext("be_send_fs_callback: failed to " 2477f169c0eaSGlenn Lagasse "recv dataset (%s)\n"), clone_ds); 2478f169c0eaSGlenn Lagasse } 2479f169c0eaSGlenn Lagasse (void) close(srpipe[0]); 2480f169c0eaSGlenn Lagasse 2481f169c0eaSGlenn Lagasse /* wait for child to exit */ 2482f169c0eaSGlenn Lagasse do { 2483f169c0eaSGlenn Lagasse retval = waitpid(pid, &status, 0); 2484f169c0eaSGlenn Lagasse if (retval == -1) { 2485f169c0eaSGlenn Lagasse status = 0; 2486f169c0eaSGlenn Lagasse } 2487f169c0eaSGlenn Lagasse } while (retval != pid); 2488f169c0eaSGlenn Lagasse 2489f169c0eaSGlenn Lagasse if (WEXITSTATUS(status) != 0) { 2490f169c0eaSGlenn Lagasse be_print_err(gettext("be_send_fs_callback: failed to " 2491f169c0eaSGlenn Lagasse "send dataset (%s)\n"), zhp_name); 2492f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 2493f169c0eaSGlenn Lagasse return (BE_ERR_ZFS); 2494f169c0eaSGlenn Lagasse } 2495f169c0eaSGlenn Lagasse 2496f169c0eaSGlenn Lagasse 2497f169c0eaSGlenn Lagasse /* 2498f169c0eaSGlenn Lagasse * Iterate through zhp's children datasets (if any) 2499f169c0eaSGlenn Lagasse * and send them accordingly. 2500f169c0eaSGlenn Lagasse */ 2501f169c0eaSGlenn Lagasse if ((ret = zfs_iter_filesystems(zhp, be_send_fs_callback, bt)) != 0) { 2502f169c0eaSGlenn Lagasse /* 2503f169c0eaSGlenn Lagasse * Error occurred while processing a child dataset. 2504f169c0eaSGlenn Lagasse * Destroy this dataset and return error. 2505f169c0eaSGlenn Lagasse */ 2506f169c0eaSGlenn Lagasse zfs_handle_t *d_zhp = NULL; 2507f169c0eaSGlenn Lagasse 2508f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 2509f169c0eaSGlenn Lagasse 2510f169c0eaSGlenn Lagasse if ((d_zhp = zfs_open(g_zfs, clone_ds, ZFS_TYPE_FILESYSTEM)) 2511f169c0eaSGlenn Lagasse == NULL) { 2512f169c0eaSGlenn Lagasse return (ret); 2513f169c0eaSGlenn Lagasse } 2514f169c0eaSGlenn Lagasse 2515f169c0eaSGlenn Lagasse (void) zfs_destroy(d_zhp, B_FALSE); 2516f169c0eaSGlenn Lagasse ZFS_CLOSE(d_zhp); 2517f169c0eaSGlenn Lagasse return (ret); 2518f169c0eaSGlenn Lagasse } 2519f169c0eaSGlenn Lagasse 2520f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 2521f169c0eaSGlenn Lagasse return (0); 2522f169c0eaSGlenn Lagasse } 2523f169c0eaSGlenn Lagasse 2524f169c0eaSGlenn Lagasse /* 2525f169c0eaSGlenn Lagasse * Function: be_destroy_callback 2526f169c0eaSGlenn Lagasse * Description: Callback function used to destroy a BEs children datasets 2527f169c0eaSGlenn Lagasse * and snapshots. 2528f169c0eaSGlenn Lagasse * Parameters: 2529f169c0eaSGlenn Lagasse * zhp - zfs_handle_t pointer to the filesystem being processed. 2530f169c0eaSGlenn Lagasse * data - Not used. 2531f169c0eaSGlenn Lagasse * Returns: 2532f169c0eaSGlenn Lagasse * 0 - Success 2533f169c0eaSGlenn Lagasse * be_errno_t - Failure 2534f169c0eaSGlenn Lagasse * Scope: 2535f169c0eaSGlenn Lagasse * Private 2536f169c0eaSGlenn Lagasse */ 2537f169c0eaSGlenn Lagasse static int 2538f169c0eaSGlenn Lagasse be_destroy_callback(zfs_handle_t *zhp, void *data) 2539f169c0eaSGlenn Lagasse { 2540f169c0eaSGlenn Lagasse be_destroy_data_t *dd = data; 2541f169c0eaSGlenn Lagasse int ret = 0; 2542f169c0eaSGlenn Lagasse 2543f169c0eaSGlenn Lagasse /* 2544f169c0eaSGlenn Lagasse * Iterate down this file system's hierarchical children 2545f169c0eaSGlenn Lagasse * and destroy them first. 2546f169c0eaSGlenn Lagasse */ 2547f169c0eaSGlenn Lagasse if ((ret = zfs_iter_filesystems(zhp, be_destroy_callback, dd)) != 0) { 2548f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 2549f169c0eaSGlenn Lagasse return (ret); 2550f169c0eaSGlenn Lagasse } 2551f169c0eaSGlenn Lagasse 2552f169c0eaSGlenn Lagasse if (dd->destroy_snaps) { 2553f169c0eaSGlenn Lagasse /* 2554f169c0eaSGlenn Lagasse * Iterate through this file system's snapshots and 2555f169c0eaSGlenn Lagasse * destroy them before destroying the file system itself. 2556f169c0eaSGlenn Lagasse */ 25570d8fa8f8SMartin Matuska if ((ret = zfs_iter_snapshots(zhp, B_FALSE, be_destroy_callback, 25580d8fa8f8SMartin Matuska dd)) 2559f169c0eaSGlenn Lagasse != 0) { 2560f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 2561f169c0eaSGlenn Lagasse return (ret); 2562f169c0eaSGlenn Lagasse } 2563f169c0eaSGlenn Lagasse } 2564f169c0eaSGlenn Lagasse 2565f169c0eaSGlenn Lagasse /* Attempt to unmount the dataset before destroying it */ 2566f169c0eaSGlenn Lagasse if (dd->force_unmount) { 2567f169c0eaSGlenn Lagasse if ((ret = zfs_unmount(zhp, NULL, MS_FORCE)) != 0) { 2568f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy_callback: " 2569f169c0eaSGlenn Lagasse "failed to unmount %s: %s\n"), zfs_get_name(zhp), 2570f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs)); 2571f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs); 2572f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 2573f169c0eaSGlenn Lagasse return (ret); 2574f169c0eaSGlenn Lagasse } 2575f169c0eaSGlenn Lagasse } 2576f169c0eaSGlenn Lagasse 2577f169c0eaSGlenn Lagasse if (zfs_destroy(zhp, B_FALSE) != 0) { 2578f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy_callback: " 2579f169c0eaSGlenn Lagasse "failed to destroy %s: %s\n"), zfs_get_name(zhp), 2580f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs)); 2581f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs); 2582f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 2583f169c0eaSGlenn Lagasse return (ret); 2584f169c0eaSGlenn Lagasse } 2585f169c0eaSGlenn Lagasse 2586f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 2587f169c0eaSGlenn Lagasse return (0); 2588f169c0eaSGlenn Lagasse } 2589f169c0eaSGlenn Lagasse 2590f169c0eaSGlenn Lagasse /* 2591f169c0eaSGlenn Lagasse * Function: be_demote_callback 2592f169c0eaSGlenn Lagasse * Description: This callback function is used to iterate through the file 2593f169c0eaSGlenn Lagasse * systems of a BE, looking for the right clone to promote such 2594f169c0eaSGlenn Lagasse * that this file system is left without any dependent clones. 2595f169c0eaSGlenn Lagasse * If the file system has no dependent clones, it doesn't need 2596f169c0eaSGlenn Lagasse * to get demoted, and the function will return success. 2597f169c0eaSGlenn Lagasse * 2598f169c0eaSGlenn Lagasse * The demotion will be done in two passes. The first pass 2599f169c0eaSGlenn Lagasse * will attempt to find the youngest snapshot that has a clone 2600f169c0eaSGlenn Lagasse * that is part of some other BE. The second pass will attempt 2601f169c0eaSGlenn Lagasse * to find the youngest snapshot that has a clone that is not 2602f169c0eaSGlenn Lagasse * part of a BE. Doing this helps ensure the aggregated set of 2603f169c0eaSGlenn Lagasse * file systems that compose a BE stay coordinated wrt BE 2604f169c0eaSGlenn Lagasse * snapshots and BE dependents. It also prevents a random user 2605f169c0eaSGlenn Lagasse * generated clone of a BE dataset to become the parent of other 2606f169c0eaSGlenn Lagasse * BE datasets after demoting this dataset. 2607f169c0eaSGlenn Lagasse * 2608f169c0eaSGlenn Lagasse * Parameters: 2609f169c0eaSGlenn Lagasse * zhp - zfs_handle_t pointer to the current file system being 2610f169c0eaSGlenn Lagasse * processed. 2611f169c0eaSGlenn Lagasse * data - not used. 2612f169c0eaSGlenn Lagasse * Return: 2613f169c0eaSGlenn Lagasse * 0 - Success 2614f169c0eaSGlenn Lagasse * be_errno_t - Failure 2615f169c0eaSGlenn Lagasse * Scope: 2616f169c0eaSGlenn Lagasse * Private 2617f169c0eaSGlenn Lagasse */ 2618f169c0eaSGlenn Lagasse static int 2619f169c0eaSGlenn Lagasse /* LINTED */ 2620f169c0eaSGlenn Lagasse be_demote_callback(zfs_handle_t *zhp, void *data) 2621f169c0eaSGlenn Lagasse { 2622f169c0eaSGlenn Lagasse be_demote_data_t dd = { 0 }; 2623f169c0eaSGlenn Lagasse int i, ret = 0; 2624f169c0eaSGlenn Lagasse 2625f169c0eaSGlenn Lagasse /* 2626f169c0eaSGlenn Lagasse * Initialize be_demote_data for the first pass - this will find a 2627f169c0eaSGlenn Lagasse * clone in another BE, if one exists. 2628f169c0eaSGlenn Lagasse */ 2629f169c0eaSGlenn Lagasse dd.find_in_BE = B_TRUE; 2630f169c0eaSGlenn Lagasse 2631f169c0eaSGlenn Lagasse for (i = 0; i < 2; i++) { 2632f169c0eaSGlenn Lagasse 26330d8fa8f8SMartin Matuska if (zfs_iter_snapshots(zhp, B_FALSE, 26340d8fa8f8SMartin Matuska be_demote_find_clone_callback, &dd) != 0) { 2635f169c0eaSGlenn Lagasse be_print_err(gettext("be_demote_callback: " 2636f169c0eaSGlenn Lagasse "failed to iterate snapshots for %s: %s\n"), 2637f169c0eaSGlenn Lagasse zfs_get_name(zhp), libzfs_error_description(g_zfs)); 2638f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs); 2639f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 2640f169c0eaSGlenn Lagasse return (ret); 2641f169c0eaSGlenn Lagasse } 2642f169c0eaSGlenn Lagasse if (dd.clone_zhp != NULL) { 2643f169c0eaSGlenn Lagasse /* Found the clone to promote. Promote it. */ 2644f169c0eaSGlenn Lagasse if (zfs_promote(dd.clone_zhp) != 0) { 2645f169c0eaSGlenn Lagasse be_print_err(gettext("be_demote_callback: " 2646f169c0eaSGlenn Lagasse "failed to promote %s: %s\n"), 2647f169c0eaSGlenn Lagasse zfs_get_name(dd.clone_zhp), 2648f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs)); 2649f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs); 2650f169c0eaSGlenn Lagasse ZFS_CLOSE(dd.clone_zhp); 2651f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 2652f169c0eaSGlenn Lagasse return (ret); 2653f169c0eaSGlenn Lagasse } 2654f169c0eaSGlenn Lagasse 2655f169c0eaSGlenn Lagasse ZFS_CLOSE(dd.clone_zhp); 2656f169c0eaSGlenn Lagasse } 2657f169c0eaSGlenn Lagasse 2658f169c0eaSGlenn Lagasse /* 2659f169c0eaSGlenn Lagasse * Reinitialize be_demote_data for the second pass. 2660f169c0eaSGlenn Lagasse * This will find a user created clone outside of any BE 2661f169c0eaSGlenn Lagasse * namespace, if one exists. 2662f169c0eaSGlenn Lagasse */ 2663f169c0eaSGlenn Lagasse dd.clone_zhp = NULL; 2664f169c0eaSGlenn Lagasse dd.origin_creation = 0; 2665f169c0eaSGlenn Lagasse dd.snapshot = NULL; 2666f169c0eaSGlenn Lagasse dd.find_in_BE = B_FALSE; 2667f169c0eaSGlenn Lagasse } 2668f169c0eaSGlenn Lagasse 2669f169c0eaSGlenn Lagasse /* Iterate down this file system's children and demote them */ 2670f169c0eaSGlenn Lagasse if ((ret = zfs_iter_filesystems(zhp, be_demote_callback, NULL)) != 0) { 2671f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 2672f169c0eaSGlenn Lagasse return (ret); 2673f169c0eaSGlenn Lagasse } 2674f169c0eaSGlenn Lagasse 2675f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 2676f169c0eaSGlenn Lagasse return (0); 2677f169c0eaSGlenn Lagasse } 2678f169c0eaSGlenn Lagasse 2679f169c0eaSGlenn Lagasse /* 2680f169c0eaSGlenn Lagasse * Function: be_demote_find_clone_callback 2681f169c0eaSGlenn Lagasse * Description: This callback function is used to iterate through the 2682f169c0eaSGlenn Lagasse * snapshots of a dataset, looking for the youngest snapshot 2683f169c0eaSGlenn Lagasse * that has a clone. If found, it returns a reference to the 2684f169c0eaSGlenn Lagasse * clone back to the caller in the callback data. 2685f169c0eaSGlenn Lagasse * Parameters: 2686f169c0eaSGlenn Lagasse * zhp - zfs_handle_t pointer to current snapshot being looked at 2687f169c0eaSGlenn Lagasse * data - be_demote_data_t pointer used to store the clone that 2688f169c0eaSGlenn Lagasse * is found. 2689f169c0eaSGlenn Lagasse * Returns: 2690f169c0eaSGlenn Lagasse * 0 - Successfully iterated through all snapshots. 2691f169c0eaSGlenn Lagasse * 1 - Failed to iterate through all snapshots. 2692f169c0eaSGlenn Lagasse * Scope: 2693f169c0eaSGlenn Lagasse * Private 2694f169c0eaSGlenn Lagasse */ 2695f169c0eaSGlenn Lagasse static int 2696f169c0eaSGlenn Lagasse be_demote_find_clone_callback(zfs_handle_t *zhp, void *data) 2697f169c0eaSGlenn Lagasse { 2698f169c0eaSGlenn Lagasse be_demote_data_t *dd = data; 2699f169c0eaSGlenn Lagasse time_t snap_creation; 2700f169c0eaSGlenn Lagasse int zret = 0; 2701f169c0eaSGlenn Lagasse 2702f169c0eaSGlenn Lagasse /* If snapshot has no clones, no need to look at it */ 2703f169c0eaSGlenn Lagasse if (zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES) == 0) { 2704f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 2705f169c0eaSGlenn Lagasse return (0); 2706f169c0eaSGlenn Lagasse } 2707f169c0eaSGlenn Lagasse 2708f169c0eaSGlenn Lagasse dd->snapshot = zfs_get_name(zhp); 2709f169c0eaSGlenn Lagasse 2710f169c0eaSGlenn Lagasse /* Get the creation time of this snapshot */ 2711f169c0eaSGlenn Lagasse snap_creation = (time_t)zfs_prop_get_int(zhp, ZFS_PROP_CREATION); 2712f169c0eaSGlenn Lagasse 2713f169c0eaSGlenn Lagasse /* 2714f169c0eaSGlenn Lagasse * If this snapshot's creation time is greater than (or younger than) 2715f169c0eaSGlenn Lagasse * the current youngest snapshot found, iterate this snapshot to 2716f169c0eaSGlenn Lagasse * check if it has a clone that we're looking for. 2717f169c0eaSGlenn Lagasse */ 2718f169c0eaSGlenn Lagasse if (snap_creation >= dd->origin_creation) { 2719f169c0eaSGlenn Lagasse /* 2720f169c0eaSGlenn Lagasse * Iterate the dependents of this snapshot to find a 2721f169c0eaSGlenn Lagasse * a clone that's a direct dependent. 2722f169c0eaSGlenn Lagasse */ 2723f169c0eaSGlenn Lagasse if ((zret = zfs_iter_dependents(zhp, B_FALSE, 2724f169c0eaSGlenn Lagasse be_demote_get_one_clone, dd)) == -1) { 2725f169c0eaSGlenn Lagasse be_print_err(gettext("be_demote_find_clone_callback: " 2726f169c0eaSGlenn Lagasse "failed to iterate dependents of %s\n"), 2727f169c0eaSGlenn Lagasse zfs_get_name(zhp)); 2728f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 2729f169c0eaSGlenn Lagasse return (1); 2730f169c0eaSGlenn Lagasse } else if (zret == 1) { 2731f169c0eaSGlenn Lagasse /* 2732f169c0eaSGlenn Lagasse * Found a clone, update the origin_creation time 2733f169c0eaSGlenn Lagasse * in the callback data. 2734f169c0eaSGlenn Lagasse */ 2735f169c0eaSGlenn Lagasse dd->origin_creation = snap_creation; 2736f169c0eaSGlenn Lagasse } 2737f169c0eaSGlenn Lagasse } 2738f169c0eaSGlenn Lagasse 2739f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 2740f169c0eaSGlenn Lagasse return (0); 2741f169c0eaSGlenn Lagasse } 2742f169c0eaSGlenn Lagasse 2743f169c0eaSGlenn Lagasse /* 2744f169c0eaSGlenn Lagasse * Function: be_demote_get_one_clone 2745f169c0eaSGlenn Lagasse * Description: This callback function is used to iterate through a 2746f169c0eaSGlenn Lagasse * snapshot's dependencies to find a filesystem that is a 2747f169c0eaSGlenn Lagasse * direct clone of the snapshot being iterated. 2748f169c0eaSGlenn Lagasse * Parameters: 2749f169c0eaSGlenn Lagasse * zhp - zfs_handle_t pointer to current dataset being looked at 2750f169c0eaSGlenn Lagasse * data - be_demote_data_t pointer used to store the clone 2751f169c0eaSGlenn Lagasse * that is found, and also provides flag to note 2752f169c0eaSGlenn Lagasse * whether or not the clone filesystem being searched 2753f169c0eaSGlenn Lagasse * for needs to be found in a BE dataset hierarchy. 2754f169c0eaSGlenn Lagasse * Return: 2755f169c0eaSGlenn Lagasse * 1 - Success, found clone and its also a BE's root dataset. 2756f169c0eaSGlenn Lagasse * 0 - Failure, clone not found. 2757f169c0eaSGlenn Lagasse * Scope: 2758f169c0eaSGlenn Lagasse * Private 2759f169c0eaSGlenn Lagasse */ 2760f169c0eaSGlenn Lagasse static int 2761f169c0eaSGlenn Lagasse be_demote_get_one_clone(zfs_handle_t *zhp, void *data) 2762f169c0eaSGlenn Lagasse { 2763f169c0eaSGlenn Lagasse be_demote_data_t *dd = data; 2764*9adfa60dSMatthew Ahrens char origin[ZFS_MAX_DATASET_NAME_LEN]; 2765*9adfa60dSMatthew Ahrens char ds_path[ZFS_MAX_DATASET_NAME_LEN]; 2766f169c0eaSGlenn Lagasse 2767f169c0eaSGlenn Lagasse if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 2768f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 2769f169c0eaSGlenn Lagasse return (0); 2770f169c0eaSGlenn Lagasse } 2771f169c0eaSGlenn Lagasse 2772f169c0eaSGlenn Lagasse (void) strlcpy(ds_path, zfs_get_name(zhp), sizeof (ds_path)); 2773f169c0eaSGlenn Lagasse 2774f169c0eaSGlenn Lagasse /* 2775f169c0eaSGlenn Lagasse * Make sure this is a direct clone of the snapshot 2776f169c0eaSGlenn Lagasse * we're iterating. 2777f169c0eaSGlenn Lagasse */ 2778f169c0eaSGlenn Lagasse if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, origin, sizeof (origin), NULL, 2779f169c0eaSGlenn Lagasse NULL, 0, B_FALSE) != 0) { 2780f169c0eaSGlenn Lagasse be_print_err(gettext("be_demote_get_one_clone: " 2781f169c0eaSGlenn Lagasse "failed to get origin of %s: %s\n"), ds_path, 2782f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs)); 2783f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 2784f169c0eaSGlenn Lagasse return (0); 2785f169c0eaSGlenn Lagasse } 2786f169c0eaSGlenn Lagasse if (strcmp(origin, dd->snapshot) != 0) { 2787f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 2788f169c0eaSGlenn Lagasse return (0); 2789f169c0eaSGlenn Lagasse } 2790f169c0eaSGlenn Lagasse 2791f169c0eaSGlenn Lagasse if (dd->find_in_BE) { 2792f169c0eaSGlenn Lagasse if ((zpool_iter(g_zfs, be_check_be_roots_callback, ds_path)) 2793f169c0eaSGlenn Lagasse > 0) { 2794f169c0eaSGlenn Lagasse if (dd->clone_zhp != NULL) 2795f169c0eaSGlenn Lagasse ZFS_CLOSE(dd->clone_zhp); 2796f169c0eaSGlenn Lagasse dd->clone_zhp = zhp; 2797f169c0eaSGlenn Lagasse return (1); 2798f169c0eaSGlenn Lagasse } 2799f169c0eaSGlenn Lagasse 2800f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 2801f169c0eaSGlenn Lagasse return (0); 2802f169c0eaSGlenn Lagasse } 2803f169c0eaSGlenn Lagasse 2804f169c0eaSGlenn Lagasse if (dd->clone_zhp != NULL) 2805f169c0eaSGlenn Lagasse ZFS_CLOSE(dd->clone_zhp); 2806f169c0eaSGlenn Lagasse 2807f169c0eaSGlenn Lagasse dd->clone_zhp = zhp; 2808f169c0eaSGlenn Lagasse return (1); 2809f169c0eaSGlenn Lagasse } 2810f169c0eaSGlenn Lagasse 2811f169c0eaSGlenn Lagasse /* 2812f169c0eaSGlenn Lagasse * Function: be_get_snap 2813f169c0eaSGlenn Lagasse * Description: This function takes a snapshot dataset name and separates 2814f169c0eaSGlenn Lagasse * out the parent dataset portion from the snapshot name. 2815f169c0eaSGlenn Lagasse * I.e. it finds the '@' in the snapshot dataset name and 2816f169c0eaSGlenn Lagasse * replaces it with a '\0'. 2817f169c0eaSGlenn Lagasse * Parameters: 2818f169c0eaSGlenn Lagasse * origin - char pointer to a snapshot dataset name. Its 2819f169c0eaSGlenn Lagasse * contents will be modified by this function. 2820f169c0eaSGlenn Lagasse * *snap - pointer to a char pointer. Will be set to the 2821f169c0eaSGlenn Lagasse * snapshot name portion upon success. 2822f169c0eaSGlenn Lagasse * Return: 2823f169c0eaSGlenn Lagasse * BE_SUCCESS - Success 2824f169c0eaSGlenn Lagasse * 1 - Failure 2825f169c0eaSGlenn Lagasse * Scope: 2826f169c0eaSGlenn Lagasse * Private 2827f169c0eaSGlenn Lagasse */ 2828f169c0eaSGlenn Lagasse static int 2829f169c0eaSGlenn Lagasse be_get_snap(char *origin, char **snap) 2830f169c0eaSGlenn Lagasse { 2831f169c0eaSGlenn Lagasse char *cp; 2832f169c0eaSGlenn Lagasse 2833f169c0eaSGlenn Lagasse /* 2834f169c0eaSGlenn Lagasse * Separate out the origin's dataset and snapshot portions by 2835f169c0eaSGlenn Lagasse * replacing the @ with a '\0' 2836f169c0eaSGlenn Lagasse */ 2837f169c0eaSGlenn Lagasse cp = strrchr(origin, '@'); 2838f169c0eaSGlenn Lagasse if (cp != NULL) { 2839f169c0eaSGlenn Lagasse if (cp[1] != NULL && cp[1] != '\0') { 2840f169c0eaSGlenn Lagasse cp[0] = '\0'; 2841f169c0eaSGlenn Lagasse *snap = cp+1; 2842f169c0eaSGlenn Lagasse } else { 2843f169c0eaSGlenn Lagasse return (1); 2844f169c0eaSGlenn Lagasse } 2845f169c0eaSGlenn Lagasse } else { 2846f169c0eaSGlenn Lagasse return (1); 2847f169c0eaSGlenn Lagasse } 2848f169c0eaSGlenn Lagasse 2849f169c0eaSGlenn Lagasse return (BE_SUCCESS); 2850f169c0eaSGlenn Lagasse } 2851f169c0eaSGlenn Lagasse 2852f169c0eaSGlenn Lagasse /* 2853f169c0eaSGlenn Lagasse * Function: be_create_container_ds 2854f169c0eaSGlenn Lagasse * Description: This function checks that the zpool passed has the BE 2855f169c0eaSGlenn Lagasse * container dataset, and if not, then creates it. 2856f169c0eaSGlenn Lagasse * Parameters: 2857f169c0eaSGlenn Lagasse * zpool - name of pool to create BE container dataset in. 2858f169c0eaSGlenn Lagasse * Return: 2859f169c0eaSGlenn Lagasse * B_TRUE - Successfully created BE container dataset, or it 2860f169c0eaSGlenn Lagasse * already existed. 2861f169c0eaSGlenn Lagasse * B_FALSE - Failed to create container dataset. 2862f169c0eaSGlenn Lagasse * Scope: 2863f169c0eaSGlenn Lagasse * Private 2864f169c0eaSGlenn Lagasse */ 2865f169c0eaSGlenn Lagasse static boolean_t 2866f169c0eaSGlenn Lagasse be_create_container_ds(char *zpool) 2867f169c0eaSGlenn Lagasse { 2868f169c0eaSGlenn Lagasse nvlist_t *props = NULL; 2869f169c0eaSGlenn Lagasse char be_container_ds[MAXPATHLEN]; 2870f169c0eaSGlenn Lagasse 2871f169c0eaSGlenn Lagasse /* Generate string for BE container dataset for this pool */ 2872f169c0eaSGlenn Lagasse be_make_container_ds(zpool, be_container_ds, 2873f169c0eaSGlenn Lagasse sizeof (be_container_ds)); 2874f169c0eaSGlenn Lagasse 2875f169c0eaSGlenn Lagasse if (!zfs_dataset_exists(g_zfs, be_container_ds, ZFS_TYPE_FILESYSTEM)) { 2876f169c0eaSGlenn Lagasse 2877f169c0eaSGlenn Lagasse if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) { 2878f169c0eaSGlenn Lagasse be_print_err(gettext("be_create_container_ds: " 2879f169c0eaSGlenn Lagasse "nvlist_alloc failed\n")); 2880f169c0eaSGlenn Lagasse return (B_FALSE); 2881f169c0eaSGlenn Lagasse } 2882f169c0eaSGlenn Lagasse 2883f169c0eaSGlenn Lagasse if (nvlist_add_string(props, 2884f169c0eaSGlenn Lagasse zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), 2885f169c0eaSGlenn Lagasse ZFS_MOUNTPOINT_LEGACY) != 0) { 2886f169c0eaSGlenn Lagasse be_print_err(gettext("be_create_container_ds: " 2887f169c0eaSGlenn Lagasse "internal error: out of memory\n")); 2888f169c0eaSGlenn Lagasse nvlist_free(props); 2889f169c0eaSGlenn Lagasse return (B_FALSE); 2890f169c0eaSGlenn Lagasse } 2891f169c0eaSGlenn Lagasse 2892f169c0eaSGlenn Lagasse if (nvlist_add_string(props, 2893f169c0eaSGlenn Lagasse zfs_prop_to_name(ZFS_PROP_CANMOUNT), "off") != 0) { 2894f169c0eaSGlenn Lagasse be_print_err(gettext("be_create_container_ds: " 2895f169c0eaSGlenn Lagasse "internal error: out of memory\n")); 2896f169c0eaSGlenn Lagasse nvlist_free(props); 2897f169c0eaSGlenn Lagasse return (B_FALSE); 2898f169c0eaSGlenn Lagasse } 2899f169c0eaSGlenn Lagasse 2900f169c0eaSGlenn Lagasse if (zfs_create(g_zfs, be_container_ds, ZFS_TYPE_FILESYSTEM, 2901f169c0eaSGlenn Lagasse props) != 0) { 2902f169c0eaSGlenn Lagasse be_print_err(gettext("be_create_container_ds: " 2903f169c0eaSGlenn Lagasse "failed to create container dataset (%s): %s\n"), 2904f169c0eaSGlenn Lagasse be_container_ds, libzfs_error_description(g_zfs)); 2905f169c0eaSGlenn Lagasse nvlist_free(props); 2906f169c0eaSGlenn Lagasse return (B_FALSE); 2907f169c0eaSGlenn Lagasse } 2908f169c0eaSGlenn Lagasse 2909f169c0eaSGlenn Lagasse nvlist_free(props); 2910f169c0eaSGlenn Lagasse } 2911f169c0eaSGlenn Lagasse 2912f169c0eaSGlenn Lagasse return (B_TRUE); 2913f169c0eaSGlenn Lagasse } 2914f169c0eaSGlenn Lagasse 2915f169c0eaSGlenn Lagasse /* 2916f169c0eaSGlenn Lagasse * Function: be_prep_clone_send_fs 2917f169c0eaSGlenn Lagasse * Description: This function takes a zfs handle to a dataset from the 2918f169c0eaSGlenn Lagasse * original BE, and generates the name of the clone dataset 2919f169c0eaSGlenn Lagasse * to create for the new BE. It also prepares the zfs 2920f169c0eaSGlenn Lagasse * properties to be used for the new BE. 2921f169c0eaSGlenn Lagasse * Parameters: 2922f169c0eaSGlenn Lagasse * zhp - pointer to zfs_handle_t of the file system being 2923f169c0eaSGlenn Lagasse * cloned/copied. 2924f169c0eaSGlenn Lagasse * bt - be_transaction_data pointer providing information 2925f169c0eaSGlenn Lagasse * about the original BE and new BE. 2926f169c0eaSGlenn Lagasse * clone_ds - buffer to store the name of the dataset 2927f169c0eaSGlenn Lagasse * for the new BE. 2928f169c0eaSGlenn Lagasse * clone_ds_len - length of clone_ds buffer 2929f169c0eaSGlenn Lagasse * Return: 2930f169c0eaSGlenn Lagasse * BE_SUCCESS - Success 2931f169c0eaSGlenn Lagasse * be_errno_t - Failure 2932f169c0eaSGlenn Lagasse * Scope: 2933f169c0eaSGlenn Lagasse * Private 2934f169c0eaSGlenn Lagasse */ 2935f169c0eaSGlenn Lagasse static int 2936f169c0eaSGlenn Lagasse be_prep_clone_send_fs(zfs_handle_t *zhp, be_transaction_data_t *bt, 2937f169c0eaSGlenn Lagasse char *clone_ds, int clone_ds_len) 2938f169c0eaSGlenn Lagasse { 2939f169c0eaSGlenn Lagasse zprop_source_t sourcetype; 2940*9adfa60dSMatthew Ahrens char source[ZFS_MAX_DATASET_NAME_LEN]; 2941*9adfa60dSMatthew Ahrens char zhp_name[ZFS_MAX_DATASET_NAME_LEN]; 2942f169c0eaSGlenn Lagasse char mountpoint[MAXPATHLEN]; 2943f169c0eaSGlenn Lagasse char *child_fs = NULL; 2944f169c0eaSGlenn Lagasse char *zhp_mountpoint = NULL; 2945f169c0eaSGlenn Lagasse int err = 0; 2946f169c0eaSGlenn Lagasse 2947f169c0eaSGlenn Lagasse /* 2948f169c0eaSGlenn Lagasse * Get a copy of the dataset name zfs_name from zhp 2949f169c0eaSGlenn Lagasse */ 2950f169c0eaSGlenn Lagasse (void) strlcpy(zhp_name, zfs_get_name(zhp), sizeof (zhp_name)); 2951f169c0eaSGlenn Lagasse 2952f169c0eaSGlenn Lagasse /* 2953f169c0eaSGlenn Lagasse * Get file system name relative to the root. 2954f169c0eaSGlenn Lagasse */ 2955f169c0eaSGlenn Lagasse if (strncmp(zhp_name, bt->obe_root_ds, strlen(bt->obe_root_ds)) 2956f169c0eaSGlenn Lagasse == 0) { 2957f169c0eaSGlenn Lagasse child_fs = zhp_name + strlen(bt->obe_root_ds); 2958f169c0eaSGlenn Lagasse 2959f169c0eaSGlenn Lagasse /* 2960f169c0eaSGlenn Lagasse * if child_fs is NULL, this means we're processing the 2961f169c0eaSGlenn Lagasse * root dataset itself; set child_fs to the empty string. 2962f169c0eaSGlenn Lagasse */ 2963f169c0eaSGlenn Lagasse if (child_fs == NULL) 2964f169c0eaSGlenn Lagasse child_fs = ""; 2965f169c0eaSGlenn Lagasse } else { 2966f169c0eaSGlenn Lagasse return (BE_ERR_INVAL); 2967f169c0eaSGlenn Lagasse } 2968f169c0eaSGlenn Lagasse 2969f169c0eaSGlenn Lagasse /* 2970f169c0eaSGlenn Lagasse * Generate the name of the clone file system. 2971f169c0eaSGlenn Lagasse */ 2972f169c0eaSGlenn Lagasse (void) snprintf(clone_ds, clone_ds_len, "%s%s", bt->nbe_root_ds, 2973f169c0eaSGlenn Lagasse child_fs); 2974f169c0eaSGlenn Lagasse 2975f169c0eaSGlenn Lagasse /* Get the mountpoint and source properties of the existing dataset */ 2976f169c0eaSGlenn Lagasse if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint, 2977f169c0eaSGlenn Lagasse sizeof (mountpoint), &sourcetype, source, sizeof (source), 2978f169c0eaSGlenn Lagasse B_FALSE) != 0) { 2979f169c0eaSGlenn Lagasse be_print_err(gettext("be_prep_clone_send_fs: " 2980f169c0eaSGlenn Lagasse "failed to get mountpoint for (%s): %s\n"), 2981f169c0eaSGlenn Lagasse zhp_name, libzfs_error_description(g_zfs)); 2982f169c0eaSGlenn Lagasse return (zfs_err_to_be_err(g_zfs)); 2983f169c0eaSGlenn Lagasse } 2984f169c0eaSGlenn Lagasse 2985f169c0eaSGlenn Lagasse /* 2986f169c0eaSGlenn Lagasse * Workaround for 6668667 where a mountpoint property of "/" comes 2987f169c0eaSGlenn Lagasse * back as "". 2988f169c0eaSGlenn Lagasse */ 2989f169c0eaSGlenn Lagasse if (strcmp(mountpoint, "") == 0) { 2990f169c0eaSGlenn Lagasse (void) snprintf(mountpoint, sizeof (mountpoint), "/"); 2991f169c0eaSGlenn Lagasse } 2992f169c0eaSGlenn Lagasse 2993f169c0eaSGlenn Lagasse /* 2994f169c0eaSGlenn Lagasse * Figure out what to set as the mountpoint for the new dataset. 2995f169c0eaSGlenn Lagasse * If the source of the mountpoint property is local, use the 2996f169c0eaSGlenn Lagasse * mountpoint value itself. Otherwise, remove it from the 2997f169c0eaSGlenn Lagasse * zfs properties list so that it gets inherited. 2998f169c0eaSGlenn Lagasse */ 2999f169c0eaSGlenn Lagasse if (sourcetype & ZPROP_SRC_LOCAL) { 3000f169c0eaSGlenn Lagasse /* 3001f169c0eaSGlenn Lagasse * If the BE that this file system is a part of is 3002f169c0eaSGlenn Lagasse * currently mounted, strip off the BE altroot portion 3003f169c0eaSGlenn Lagasse * from the mountpoint. 3004f169c0eaSGlenn Lagasse */ 3005f169c0eaSGlenn Lagasse zhp_mountpoint = mountpoint; 3006f169c0eaSGlenn Lagasse 3007f169c0eaSGlenn Lagasse if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0 && 3008f169c0eaSGlenn Lagasse bt->obe_altroot != NULL && strcmp(bt->obe_altroot, 3009f169c0eaSGlenn Lagasse "/") != 0 && zfs_is_mounted(zhp, NULL)) { 3010f169c0eaSGlenn Lagasse 3011f169c0eaSGlenn Lagasse int altroot_len = strlen(bt->obe_altroot); 3012f169c0eaSGlenn Lagasse 3013f169c0eaSGlenn Lagasse if (strncmp(bt->obe_altroot, mountpoint, altroot_len) 3014f169c0eaSGlenn Lagasse == 0) { 3015f169c0eaSGlenn Lagasse if (mountpoint[altroot_len] == '/') 3016f169c0eaSGlenn Lagasse zhp_mountpoint = mountpoint + 3017f169c0eaSGlenn Lagasse altroot_len; 3018f169c0eaSGlenn Lagasse else if (mountpoint[altroot_len] == '\0') 3019f169c0eaSGlenn Lagasse (void) snprintf(mountpoint, 3020f169c0eaSGlenn Lagasse sizeof (mountpoint), "/"); 3021f169c0eaSGlenn Lagasse } 3022f169c0eaSGlenn Lagasse } 3023f169c0eaSGlenn Lagasse 3024f169c0eaSGlenn Lagasse if (nvlist_add_string(bt->nbe_zfs_props, 3025f169c0eaSGlenn Lagasse zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), 3026f169c0eaSGlenn Lagasse zhp_mountpoint) != 0) { 3027f169c0eaSGlenn Lagasse be_print_err(gettext("be_prep_clone_send_fs: " 3028f169c0eaSGlenn Lagasse "internal error: out of memory\n")); 3029f169c0eaSGlenn Lagasse return (BE_ERR_NOMEM); 3030f169c0eaSGlenn Lagasse } 3031f169c0eaSGlenn Lagasse } else { 3032f169c0eaSGlenn Lagasse err = nvlist_remove_all(bt->nbe_zfs_props, 3033f169c0eaSGlenn Lagasse zfs_prop_to_name(ZFS_PROP_MOUNTPOINT)); 3034f169c0eaSGlenn Lagasse if (err != 0 && err != ENOENT) { 3035f169c0eaSGlenn Lagasse be_print_err(gettext("be_prep_clone_send_fs: " 3036f169c0eaSGlenn Lagasse "failed to remove mountpoint from " 3037f169c0eaSGlenn Lagasse "nvlist\n")); 3038f169c0eaSGlenn Lagasse return (BE_ERR_INVAL); 3039f169c0eaSGlenn Lagasse } 3040f169c0eaSGlenn Lagasse } 3041f169c0eaSGlenn Lagasse 3042f169c0eaSGlenn Lagasse /* 3043f169c0eaSGlenn Lagasse * Set the 'canmount' property 3044f169c0eaSGlenn Lagasse */ 3045f169c0eaSGlenn Lagasse if (nvlist_add_string(bt->nbe_zfs_props, 3046f169c0eaSGlenn Lagasse zfs_prop_to_name(ZFS_PROP_CANMOUNT), "noauto") != 0) { 3047f169c0eaSGlenn Lagasse be_print_err(gettext("be_prep_clone_send_fs: " 3048f169c0eaSGlenn Lagasse "internal error: out of memory\n")); 3049f169c0eaSGlenn Lagasse return (BE_ERR_NOMEM); 3050f169c0eaSGlenn Lagasse } 3051f169c0eaSGlenn Lagasse 3052f169c0eaSGlenn Lagasse return (BE_SUCCESS); 3053f169c0eaSGlenn Lagasse } 3054f169c0eaSGlenn Lagasse 3055f169c0eaSGlenn Lagasse /* 3056f169c0eaSGlenn Lagasse * Function: be_get_zone_be_name 3057f169c0eaSGlenn Lagasse * Description: This function takes the zones root dataset, the container 3058f169c0eaSGlenn Lagasse * dataset and returns the zones BE name based on the zone 3059f169c0eaSGlenn Lagasse * root dataset. 3060f169c0eaSGlenn Lagasse * Parameters: 3061f169c0eaSGlenn Lagasse * root_ds - the zones root dataset. 3062f169c0eaSGlenn Lagasse * container_ds - the container dataset for the zone. 3063f169c0eaSGlenn Lagasse * Returns: 3064f169c0eaSGlenn Lagasse * char * - the BE name of this zone based on the root dataset. 3065f169c0eaSGlenn Lagasse */ 3066f169c0eaSGlenn Lagasse static char * 3067f169c0eaSGlenn Lagasse be_get_zone_be_name(char *root_ds, char *container_ds) 3068f169c0eaSGlenn Lagasse { 3069f169c0eaSGlenn Lagasse return (root_ds + (strlen(container_ds) + 1)); 3070f169c0eaSGlenn Lagasse } 3071f169c0eaSGlenn Lagasse 3072f169c0eaSGlenn Lagasse /* 3073f169c0eaSGlenn Lagasse * Function: be_zone_root_exists_callback 3074f169c0eaSGlenn Lagasse * Description: This callback function is used to determine if a 3075f169c0eaSGlenn Lagasse * zone root container dataset has any children. It always 3076f169c0eaSGlenn Lagasse * returns 1, signifying a hierarchical child of the zone 3077f169c0eaSGlenn Lagasse * root container dataset has been traversed and therefore 3078f169c0eaSGlenn Lagasse * it has children. 3079f169c0eaSGlenn Lagasse * Parameters: 3080f169c0eaSGlenn Lagasse * zhp - zfs_handle_t pointer to current dataset being processed. 3081f169c0eaSGlenn Lagasse * data - not used. 3082f169c0eaSGlenn Lagasse * Returns: 3083f169c0eaSGlenn Lagasse * 1 - dataset exists 3084f169c0eaSGlenn Lagasse * Scope: 3085f169c0eaSGlenn Lagasse * Private 3086f169c0eaSGlenn Lagasse */ 3087f169c0eaSGlenn Lagasse static int 3088f169c0eaSGlenn Lagasse /* LINTED */ 3089f169c0eaSGlenn Lagasse be_zone_root_exists_callback(zfs_handle_t *zhp, void *data) 3090f169c0eaSGlenn Lagasse { 3091f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp); 3092f169c0eaSGlenn Lagasse return (1); 3093f169c0eaSGlenn Lagasse } 3094