Home | History | Annotate | Download | only in common
      1 /*
      2  * CDDL HEADER START
      3  *
      4  * The contents of this file are subject to the terms of the
      5  * Common Development and Distribution License (the "License").
      6  * You may not use this file except in compliance with the License.
      7  *
      8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9  * or http://www.opensolaris.org/os/licensing.
     10  * See the License for the specific language governing permissions
     11  * and limitations under the License.
     12  *
     13  * When distributing Covered Code, include this CDDL HEADER in each
     14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  * If applicable, add the following below this CDDL HEADER, with the
     16  * fields enclosed by brackets "[]" replaced with your own identifying
     17  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  *
     19  * CDDL HEADER END
     20  */
     21 
     22 /*
     23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
     24  * Use is subject to license terms.
     25  *
     26  * Portions Copyright 2007 Ramprakash Jelari
     27  */
     28 
     29 #include <libintl.h>
     30 #include <libuutil.h>
     31 #include <stddef.h>
     32 #include <stdlib.h>
     33 #include <string.h>
     34 #include <unistd.h>
     35 #include <zone.h>
     36 
     37 #include <libzfs.h>
     38 
     39 #include "libzfs_impl.h"
     40 
     41 /*
     42  * Structure to keep track of dataset state.  Before changing the 'sharenfs' or
     43  * 'mountpoint' property, we record whether the filesystem was previously
     44  * mounted/shared.  This prior state dictates whether we remount/reshare the
     45  * dataset after the property has been changed.
     46  *
     47  * The interface consists of the following sequence of functions:
     48  *
     49  * 	changelist_gather()
     50  * 	changelist_prefix()
     51  * 	< change property >
     52  * 	changelist_postfix()
     53  * 	changelist_free()
     54  *
     55  * Other interfaces:
     56  *
     57  * changelist_remove() - remove a node from a gathered list
     58  * changelist_rename() - renames all datasets appropriately when doing a rename
     59  * changelist_unshare() - unshares all the nodes in a given changelist
     60  * changelist_haszonedchild() - check if there is any child exported to
     61  *				a local zone
     62  */
     63 typedef struct prop_changenode {
     64 	zfs_handle_t		*cn_handle;
     65 	int			cn_shared;
     66 	int			cn_mounted;
     67 	int			cn_zoned;
     68 	boolean_t		cn_needpost;	/* is postfix() needed? */
     69 	uu_list_node_t		cn_listnode;
     70 } prop_changenode_t;
     71 
     72 struct prop_changelist {
     73 	zfs_prop_t		cl_prop;
     74 	zfs_prop_t		cl_realprop;
     75 	zfs_prop_t		cl_shareprop;  /* used with sharenfs/sharesmb */
     76 	uu_list_pool_t		*cl_pool;
     77 	uu_list_t		*cl_list;
     78 	boolean_t		cl_waslegacy;
     79 	boolean_t		cl_allchildren;
     80 	boolean_t		cl_alldependents;
     81 	int			cl_mflags;	/* Mount flags */
     82 	int			cl_gflags;	/* Gather request flags */
     83 	boolean_t		cl_haszonedchild;
     84 	boolean_t		cl_sorted;
     85 };
     86 
     87 /*
     88  * If the property is 'mountpoint', go through and unmount filesystems as
     89  * necessary.  We don't do the same for 'sharenfs', because we can just re-share
     90  * with different options without interrupting service. We do handle 'sharesmb'
     91  * since there may be old resource names that need to be removed.
     92  */
     93 int
     94 changelist_prefix(prop_changelist_t *clp)
     95 {
     96 	prop_changenode_t *cn;
     97 	int ret = 0;
     98 
     99 	if (clp->cl_prop != ZFS_PROP_MOUNTPOINT &&
    100 	    clp->cl_prop != ZFS_PROP_SHARESMB)
    101 		return (0);
    102 
    103 	for (cn = uu_list_first(clp->cl_list); cn != NULL;
    104 	    cn = uu_list_next(clp->cl_list, cn)) {
    105 
    106 		/* if a previous loop failed, set the remaining to false */
    107 		if (ret == -1) {
    108 			cn->cn_needpost = B_FALSE;
    109 			continue;
    110 		}
    111 
    112 		/*
    113 		 * If we are in the global zone, but this dataset is exported
    114 		 * to a local zone, do nothing.
    115 		 */
    116 		if (getzoneid() == GLOBAL_ZONEID && cn->cn_zoned)
    117 			continue;
    118 
    119 		if (ZFS_IS_VOLUME(cn->cn_handle)) {
    120 			switch (clp->cl_realprop) {
    121 			case ZFS_PROP_NAME:
    122 				/*
    123 				 * If this was a rename, unshare the zvol, and
    124 				 * remove the /dev/zvol links.
    125 				 */
    126 				(void) zfs_unshare_iscsi(cn->cn_handle);
    127 
    128 				if (zvol_remove_link(cn->cn_handle->zfs_hdl,
    129 				    cn->cn_handle->zfs_name) != 0) {
    130 					ret = -1;
    131 					cn->cn_needpost = B_FALSE;
    132 					(void) zfs_share_iscsi(cn->cn_handle);
    133 				}
    134 				break;
    135 
    136 			case ZFS_PROP_VOLSIZE:
    137 				/*
    138 				 * If this was a change to the volume size, we
    139 				 * need to unshare and reshare the volume.
    140 				 */
    141 				(void) zfs_unshare_iscsi(cn->cn_handle);
    142 				break;
    143 			}
    144 		} else {
    145 			/*
    146 			 * Do the property specific processing.
    147 			 */
    148 			switch (clp->cl_prop) {
    149 			case ZFS_PROP_MOUNTPOINT:
    150 				if (zfs_unmount(cn->cn_handle, NULL,
    151 				    clp->cl_mflags) != 0) {
    152 					ret = -1;
    153 					cn->cn_needpost = B_FALSE;
    154 				}
    155 				break;
    156 			case ZFS_PROP_SHARESMB:
    157 				(void) zfs_unshare_smb(cn->cn_handle, NULL);
    158 				break;
    159 			}
    160 		}
    161 	}
    162 
    163 	if (ret == -1)
    164 		(void) changelist_postfix(clp);
    165 
    166 	return (ret);
    167 }
    168 
    169 /*
    170  * If the property is 'mountpoint' or 'sharenfs', go through and remount and/or
    171  * reshare the filesystems as necessary.  In changelist_gather() we recorded
    172  * whether the filesystem was previously shared or mounted.  The action we take
    173  * depends on the previous state, and whether the value was previously 'legacy'.
    174  * For non-legacy properties, we only remount/reshare the filesystem if it was
    175  * previously mounted/shared.  Otherwise, we always remount/reshare the
    176  * filesystem.
    177  */
    178 int
    179 changelist_postfix(prop_changelist_t *clp)
    180 {
    181 	prop_changenode_t *cn;
    182 	char shareopts[ZFS_MAXPROPLEN];
    183 	int errors = 0;
    184 	libzfs_handle_t *hdl;
    185 
    186 	/*
    187 	 * If we're changing the mountpoint, attempt to destroy the underlying
    188 	 * mountpoint.  All other datasets will have inherited from this dataset
    189 	 * (in which case their mountpoints exist in the filesystem in the new
    190 	 * location), or have explicit mountpoints set (in which case they won't
    191 	 * be in the changelist).
    192 	 */
    193 	if ((cn = uu_list_last(clp->cl_list)) == NULL)
    194 		return (0);
    195 
    196 	if (clp->cl_prop == ZFS_PROP_MOUNTPOINT)
    197 		remove_mountpoint(cn->cn_handle);
    198 
    199 	/*
    200 	 * It is possible that the changelist_prefix() used libshare
    201 	 * to unshare some entries. Since libshare caches data, an
    202 	 * attempt to reshare during postfix can fail unless libshare
    203 	 * is uninitialized here so that it will reinitialize later.
    204 	 */
    205 	if (cn->cn_handle != NULL) {
    206 		hdl = cn->cn_handle->zfs_hdl;
    207 		assert(hdl != NULL);
    208 		zfs_uninit_libshare(hdl);
    209 	}
    210 
    211 	/*
    212 	 * We walk the datasets in reverse, because we want to mount any parent
    213 	 * datasets before mounting the children.  We walk all datasets even if
    214 	 * there are errors.
    215 	 */
    216 	for (cn = uu_list_last(clp->cl_list); cn != NULL;
    217 	    cn = uu_list_prev(clp->cl_list, cn)) {
    218 
    219 		boolean_t sharenfs;
    220 		boolean_t sharesmb;
    221 		boolean_t mounted;
    222 
    223 		/*
    224 		 * If we are in the global zone, but this dataset is exported
    225 		 * to a local zone, do nothing.
    226 		 */
    227 		if (getzoneid() == GLOBAL_ZONEID && cn->cn_zoned)
    228 			continue;
    229 
    230 		/* Only do post-processing if it's required */
    231 		if (!cn->cn_needpost)
    232 			continue;
    233 		cn->cn_needpost = B_FALSE;
    234 
    235 		zfs_refresh_properties(cn->cn_handle);
    236 
    237 		if (ZFS_IS_VOLUME(cn->cn_handle)) {
    238 			/*
    239 			 * If we're doing a rename, recreate the /dev/zvol
    240 			 * links.
    241 			 */
    242 			if (clp->cl_realprop == ZFS_PROP_NAME &&
    243 			    zvol_create_link(cn->cn_handle->zfs_hdl,
    244 			    cn->cn_handle->zfs_name) != 0) {
    245 				errors++;
    246 			} else if (cn->cn_shared ||
    247 			    clp->cl_prop == ZFS_PROP_SHAREISCSI) {
    248 				if (zfs_prop_get(cn->cn_handle,
    249 				    ZFS_PROP_SHAREISCSI, shareopts,
    250 				    sizeof (shareopts), NULL, NULL, 0,
    251 				    B_FALSE) == 0 &&
    252 				    strcmp(shareopts, "off") == 0) {
    253 					errors +=
    254 					    zfs_unshare_iscsi(cn->cn_handle);
    255 				} else {
    256 					errors +=
    257 					    zfs_share_iscsi(cn->cn_handle);
    258 				}
    259 			}
    260 
    261 			continue;
    262 		}
    263 
    264 		/*
    265 		 * Remount if previously mounted or mountpoint was legacy,
    266 		 * or sharenfs or sharesmb  property is set.
    267 		 */
    268 		sharenfs = ((zfs_prop_get(cn->cn_handle, ZFS_PROP_SHARENFS,
    269 		    shareopts, sizeof (shareopts), NULL, NULL, 0,
    270 		    B_FALSE) == 0) && (strcmp(shareopts, "off") != 0));
    271 
    272 		sharesmb = ((zfs_prop_get(cn->cn_handle, ZFS_PROP_SHARESMB,
    273 		    shareopts, sizeof (shareopts), NULL, NULL, 0,
    274 		    B_FALSE) == 0) && (strcmp(shareopts, "off") != 0));
    275 
    276 		mounted = zfs_is_mounted(cn->cn_handle, NULL);
    277 
    278 		if (!mounted && (cn->cn_mounted ||
    279 		    ((sharenfs || sharesmb || clp->cl_waslegacy) &&
    280 		    (zfs_prop_get_int(cn->cn_handle,
    281 		    ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_ON)))) {
    282 
    283 			if (zfs_mount(cn->cn_handle, NULL, 0) != 0)
    284 				errors++;
    285 			else
    286 				mounted = TRUE;
    287 		}
    288 
    289 		/*
    290 		 * If the file system is mounted we always re-share even
    291 		 * if the filesystem is currently shared, so that we can
    292 		 * adopt any new options.
    293 		 */
    294 		if (sharenfs && mounted)
    295 			errors += zfs_share_nfs(cn->cn_handle);
    296 		else if (cn->cn_shared || clp->cl_waslegacy)
    297 			errors += zfs_unshare_nfs(cn->cn_handle, NULL);
    298 		if (sharesmb && mounted)
    299 			errors += zfs_share_smb(cn->cn_handle);
    300 		else if (cn->cn_shared || clp->cl_waslegacy)
    301 			errors += zfs_unshare_smb(cn->cn_handle, NULL);
    302 	}
    303 
    304 	return (errors ? -1 : 0);
    305 }
    306 
    307 /*
    308  * Is this "dataset" a child of "parent"?
    309  */
    310 boolean_t
    311 isa_child_of(const char *dataset, const char *parent)
    312 {
    313 	int len;
    314 
    315 	len = strlen(parent);
    316 
    317 	if (strncmp(dataset, parent, len) == 0 &&
    318 	    (dataset[len] == '@' || dataset[len] == '/' ||
    319 	    dataset[len] == '\0'))
    320 		return (B_TRUE);
    321 	else
    322 		return (B_FALSE);
    323 
    324 }
    325 
    326 /*
    327  * If we rename a filesystem, child filesystem handles are no longer valid
    328  * since we identify each dataset by its name in the ZFS namespace.  As a
    329  * result, we have to go through and fix up all the names appropriately.  We
    330  * could do this automatically if libzfs kept track of all open handles, but
    331  * this is a lot less work.
    332  */
    333 void
    334 changelist_rename(prop_changelist_t *clp, const char *src, const char *dst)
    335 {
    336 	prop_changenode_t *cn;
    337 	char newname[ZFS_MAXNAMELEN];
    338 
    339 	for (cn = uu_list_first(clp->cl_list); cn != NULL;
    340 	    cn = uu_list_next(clp->cl_list, cn)) {
    341 		/*
    342 		 * Do not rename a clone that's not in the source hierarchy.
    343 		 */
    344 		if (!isa_child_of(cn->cn_handle->zfs_name, src))
    345 			continue;
    346 
    347 		/*
    348 		 * Destroy the previous mountpoint if needed.
    349 		 */
    350 		remove_mountpoint(cn->cn_handle);
    351 
    352 		(void) strlcpy(newname, dst, sizeof (newname));
    353 		(void) strcat(newname, cn->cn_handle->zfs_name + strlen(src));
    354 
    355 		(void) strlcpy(cn->cn_handle->zfs_name, newname,
    356 		    sizeof (cn->cn_handle->zfs_name));
    357 	}
    358 }
    359 
    360 /*
    361  * Given a gathered changelist for the 'sharenfs' or 'sharesmb' property,
    362  * unshare all the datasets in the list.
    363  */
    364 int
    365 changelist_unshare(prop_changelist_t *clp, zfs_share_proto_t *proto)
    366 {
    367 	prop_changenode_t *cn;
    368 	int ret = 0;
    369 
    370 	if (clp->cl_prop != ZFS_PROP_SHARENFS &&
    371 	    clp->cl_prop != ZFS_PROP_SHARESMB)
    372 		return (0);
    373 
    374 	for (cn = uu_list_first(clp->cl_list); cn != NULL;
    375 	    cn = uu_list_next(clp->cl_list, cn)) {
    376 		if (zfs_unshare_proto(cn->cn_handle, NULL, proto) != 0)
    377 			ret = -1;
    378 	}
    379 
    380 	return (ret);
    381 }
    382 
    383 /*
    384  * Check if there is any child exported to a local zone in a given changelist.
    385  * This information has already been recorded while gathering the changelist
    386  * via changelist_gather().
    387  */
    388 int
    389 changelist_haszonedchild(prop_changelist_t *clp)
    390 {
    391 	return (clp->cl_haszonedchild);
    392 }
    393 
    394 /*
    395  * Remove a node from a gathered list.
    396  */
    397 void
    398 changelist_remove(prop_changelist_t *clp, const char *name)
    399 {
    400 	prop_changenode_t *cn;
    401 
    402 	for (cn = uu_list_first(clp->cl_list); cn != NULL;
    403 	    cn = uu_list_next(clp->cl_list, cn)) {
    404 
    405 		if (strcmp(cn->cn_handle->zfs_name, name) == 0) {
    406 			uu_list_remove(clp->cl_list, cn);
    407 			zfs_close(cn->cn_handle);
    408 			free(cn);
    409 			return;
    410 		}
    411 	}
    412 }
    413 
    414 /*
    415  * Release any memory associated with a changelist.
    416  */
    417 void
    418 changelist_free(prop_changelist_t *clp)
    419 {
    420 	prop_changenode_t *cn;
    421 	void *cookie;
    422 
    423 	if (clp->cl_list) {
    424 		cookie = NULL;
    425 		while ((cn = uu_list_teardown(clp->cl_list, &cookie)) != NULL) {
    426 			zfs_close(cn->cn_handle);
    427 			free(cn);
    428 		}
    429 
    430 		uu_list_destroy(clp->cl_list);
    431 	}
    432 	if (clp->cl_pool)
    433 		uu_list_pool_destroy(clp->cl_pool);
    434 
    435 	free(clp);
    436 }
    437 
    438 static int
    439 change_one(zfs_handle_t *zhp, void *data)
    440 {
    441 	prop_changelist_t *clp = data;
    442 	char property[ZFS_MAXPROPLEN];
    443 	char where[64];
    444 	prop_changenode_t *cn;
    445 	zprop_source_t sourcetype;
    446 	zprop_source_t share_sourcetype;
    447 
    448 	/*
    449 	 * We only want to unmount/unshare those filesystems that may inherit
    450 	 * from the target filesystem.  If we find any filesystem with a
    451 	 * locally set mountpoint, we ignore any children since changing the
    452 	 * property will not affect them.  If this is a rename, we iterate
    453 	 * over all children regardless, since we need them unmounted in
    454 	 * order to do the rename.  Also, if this is a volume and we're doing
    455 	 * a rename, then always add it to the changelist.
    456 	 */
    457 
    458 	if (!(ZFS_IS_VOLUME(zhp) && clp->cl_realprop == ZFS_PROP_NAME) &&
    459 	    zfs_prop_get(zhp, clp->cl_prop, property,
    460 	    sizeof (property), &sourcetype, where, sizeof (where),
    461 	    B_FALSE) != 0) {
    462 		zfs_close(zhp);
    463 		return (0);
    464 	}
    465 
    466 	/*
    467 	 * If we are "watching" sharenfs or sharesmb
    468 	 * then check out the companion property which is tracked
    469 	 * in cl_shareprop
    470 	 */
    471 	if (clp->cl_shareprop != ZPROP_INVAL &&
    472 	    zfs_prop_get(zhp, clp->cl_shareprop, property,
    473 	    sizeof (property), &share_sourcetype, where, sizeof (where),
    474 	    B_FALSE) != 0) {
    475 		zfs_close(zhp);
    476 		return (0);
    477 	}
    478 
    479 	if (clp->cl_alldependents || clp->cl_allchildren ||
    480 	    sourcetype == ZPROP_SRC_DEFAULT ||
    481 	    sourcetype == ZPROP_SRC_INHERITED ||
    482 	    (clp->cl_shareprop != ZPROP_INVAL &&
    483 	    (share_sourcetype == ZPROP_SRC_DEFAULT ||
    484 	    share_sourcetype == ZPROP_SRC_INHERITED))) {
    485 		if ((cn = zfs_alloc(zfs_get_handle(zhp),
    486 		    sizeof (prop_changenode_t))) == NULL) {
    487 			zfs_close(zhp);
    488 			return (-1);
    489 		}
    490 
    491 		cn->cn_handle = zhp;
    492 		cn->cn_mounted = (clp->cl_gflags & CL_GATHER_MOUNT_ALWAYS) ||
    493 		    zfs_is_mounted(zhp, NULL);
    494 		cn->cn_shared = zfs_is_shared(zhp);
    495 		cn->cn_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
    496 		cn->cn_needpost = B_TRUE;
    497 
    498 		/* Indicate if any child is exported to a local zone. */
    499 		if (getzoneid() == GLOBAL_ZONEID && cn->cn_zoned)
    500 			clp->cl_haszonedchild = B_TRUE;
    501 
    502 		uu_list_node_init(cn, &cn->cn_listnode, clp->cl_pool);
    503 
    504 		if (clp->cl_sorted) {
    505 			uu_list_index_t idx;
    506 
    507 			(void) uu_list_find(clp->cl_list, cn, NULL,
    508 			    &idx);
    509 			uu_list_insert(clp->cl_list, cn, idx);
    510 		} else {
    511 			ASSERT(!clp->cl_alldependents);
    512 			verify(uu_list_insert_before(clp->cl_list,
    513 			    uu_list_first(clp->cl_list), cn) == 0);
    514 		}
    515 
    516 		if (!clp->cl_alldependents)
    517 			return (zfs_iter_children(zhp, change_one, data));
    518 	} else {
    519 		zfs_close(zhp);
    520 	}
    521 
    522 	return (0);
    523 }
    524 
    525 /*ARGSUSED*/
    526 static int
    527 compare_mountpoints(const void *a, const void *b, void *unused)
    528 {
    529 	const prop_changenode_t *ca = a;
    530 	const prop_changenode_t *cb = b;
    531 
    532 	char mounta[MAXPATHLEN];
    533 	char mountb[MAXPATHLEN];
    534 
    535 	boolean_t hasmounta, hasmountb;
    536 
    537 	/*
    538 	 * When unsharing or unmounting filesystems, we need to do it in
    539 	 * mountpoint order.  This allows the user to have a mountpoint
    540 	 * hierarchy that is different from the dataset hierarchy, and still
    541 	 * allow it to be changed.  However, if either dataset doesn't have a
    542 	 * mountpoint (because it is a volume or a snapshot), we place it at the
    543 	 * end of the list, because it doesn't affect our change at all.
    544 	 */
    545 	hasmounta = (zfs_prop_get(ca->cn_handle, ZFS_PROP_MOUNTPOINT, mounta,
    546 	    sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
    547 	hasmountb = (zfs_prop_get(cb->cn_handle, ZFS_PROP_MOUNTPOINT, mountb,
    548 	    sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
    549 
    550 	if (!hasmounta && hasmountb)
    551 		return (-1);
    552 	else if (hasmounta && !hasmountb)
    553 		return (1);
    554 	else if (!hasmounta && !hasmountb)
    555 		return (0);
    556 	else
    557 		return (strcmp(mountb, mounta));
    558 }
    559 
    560 /*
    561  * Given a ZFS handle and a property, construct a complete list of datasets
    562  * that need to be modified as part of this process.  For anything but the
    563  * 'mountpoint' and 'sharenfs' properties, this just returns an empty list.
    564  * Otherwise, we iterate over all children and look for any datasets that
    565  * inherit the property.  For each such dataset, we add it to the list and
    566  * mark whether it was shared beforehand.
    567  */
    568 prop_changelist_t *
    569 changelist_gather(zfs_handle_t *zhp, zfs_prop_t prop, int gather_flags,
    570     int mnt_flags)
    571 {
    572 	prop_changelist_t *clp;
    573 	prop_changenode_t *cn;
    574 	zfs_handle_t *temp;
    575 	char property[ZFS_MAXPROPLEN];
    576 	uu_compare_fn_t *compare = NULL;
    577 
    578 	if ((clp = zfs_alloc(zhp->zfs_hdl, sizeof (prop_changelist_t))) == NULL)
    579 		return (NULL);
    580 
    581 	/*
    582 	 * For mountpoint-related tasks, we want to sort everything by
    583 	 * mountpoint, so that we mount and unmount them in the appropriate
    584 	 * order, regardless of their position in the hierarchy.
    585 	 */
    586 	if (prop == ZFS_PROP_NAME || prop == ZFS_PROP_ZONED ||
    587 	    prop == ZFS_PROP_MOUNTPOINT || prop == ZFS_PROP_SHARENFS ||
    588 	    prop == ZFS_PROP_SHARESMB) {
    589 		compare = compare_mountpoints;
    590 		clp->cl_sorted = B_TRUE;
    591 	}
    592 
    593 	clp->cl_pool = uu_list_pool_create("changelist_pool",
    594 	    sizeof (prop_changenode_t),
    595 	    offsetof(prop_changenode_t, cn_listnode),
    596 	    compare, 0);
    597 	if (clp->cl_pool == NULL) {
    598 		assert(uu_error() == UU_ERROR_NO_MEMORY);
    599 		(void) zfs_error(zhp->zfs_hdl, EZFS_NOMEM, "internal error");
    600 		changelist_free(clp);
    601 		return (NULL);
    602 	}
    603 
    604 	clp->cl_list = uu_list_create(clp->cl_pool, NULL,
    605 	    clp->cl_sorted ? UU_LIST_SORTED : 0);
    606 	clp->cl_gflags = gather_flags;
    607 	clp->cl_mflags = mnt_flags;
    608 
    609 	if (clp->cl_list == NULL) {
    610 		assert(uu_error() == UU_ERROR_NO_MEMORY);
    611 		(void) zfs_error(zhp->zfs_hdl, EZFS_NOMEM, "internal error");
    612 		changelist_free(clp);
    613 		return (NULL);
    614 	}
    615 
    616 	/*
    617 	 * If this is a rename or the 'zoned' property, we pretend we're
    618 	 * changing the mountpoint and flag it so we can catch all children in
    619 	 * change_one().
    620 	 *
    621 	 * Flag cl_alldependents to catch all children plus the dependents
    622 	 * (clones) that are not in the hierarchy.
    623 	 */
    624 	if (prop == ZFS_PROP_NAME) {
    625 		clp->cl_prop = ZFS_PROP_MOUNTPOINT;
    626 		clp->cl_alldependents = B_TRUE;
    627 	} else if (prop == ZFS_PROP_ZONED) {
    628 		clp->cl_prop = ZFS_PROP_MOUNTPOINT;
    629 		clp->cl_allchildren = B_TRUE;
    630 	} else if (prop == ZFS_PROP_CANMOUNT) {
    631 		clp->cl_prop = ZFS_PROP_MOUNTPOINT;
    632 	} else if (prop == ZFS_PROP_VOLSIZE) {
    633 		clp->cl_prop = ZFS_PROP_MOUNTPOINT;
    634 	} else {
    635 		clp->cl_prop = prop;
    636 	}
    637 	clp->cl_realprop = prop;
    638 
    639 	if (clp->cl_prop != ZFS_PROP_MOUNTPOINT &&
    640 	    clp->cl_prop != ZFS_PROP_SHARENFS &&
    641 	    clp->cl_prop != ZFS_PROP_SHARESMB &&
    642 	    clp->cl_prop != ZFS_PROP_SHAREISCSI)
    643 		return (clp);
    644 
    645 	/*
    646 	 * If watching SHARENFS or SHARESMB then
    647 	 * also watch its companion property.
    648 	 */
    649 	if (clp->cl_prop == ZFS_PROP_SHARENFS)
    650 		clp->cl_shareprop = ZFS_PROP_SHARESMB;
    651 	else if (clp->cl_prop == ZFS_PROP_SHARESMB)
    652 		clp->cl_shareprop = ZFS_PROP_SHARENFS;
    653 
    654 	if (clp->cl_alldependents) {
    655 		if (zfs_iter_dependents(zhp, B_TRUE, change_one, clp) != 0) {
    656 			changelist_free(clp);
    657 			return (NULL);
    658 		}
    659 	} else if (zfs_iter_children(zhp, change_one, clp) != 0) {
    660 		changelist_free(clp);
    661 		return (NULL);
    662 	}
    663 
    664 	/*
    665 	 * We have to re-open ourselves because we auto-close all the handles
    666 	 * and can't tell the difference.
    667 	 */
    668 	if ((temp = zfs_open(zhp->zfs_hdl, zfs_get_name(zhp),
    669 	    ZFS_TYPE_DATASET)) == NULL) {
    670 		changelist_free(clp);
    671 		return (NULL);
    672 	}
    673 
    674 	/*
    675 	 * Always add ourself to the list.  We add ourselves to the end so that
    676 	 * we're the last to be unmounted.
    677 	 */
    678 	if ((cn = zfs_alloc(zhp->zfs_hdl,
    679 	    sizeof (prop_changenode_t))) == NULL) {
    680 		zfs_close(temp);
    681 		changelist_free(clp);
    682 		return (NULL);
    683 	}
    684 
    685 	cn->cn_handle = temp;
    686 	cn->cn_mounted = (clp->cl_gflags & CL_GATHER_MOUNT_ALWAYS) ||
    687 	    zfs_is_mounted(temp, NULL);
    688 	cn->cn_shared = zfs_is_shared(temp);
    689 	cn->cn_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
    690 	cn->cn_needpost = B_TRUE;
    691 
    692 	uu_list_node_init(cn, &cn->cn_listnode, clp->cl_pool);
    693 	if (clp->cl_sorted) {
    694 		uu_list_index_t idx;
    695 		(void) uu_list_find(clp->cl_list, cn, NULL, &idx);
    696 		uu_list_insert(clp->cl_list, cn, idx);
    697 	} else {
    698 		verify(uu_list_insert_after(clp->cl_list,
    699 		    uu_list_last(clp->cl_list), cn) == 0);
    700 	}
    701 
    702 	/*
    703 	 * If the mountpoint property was previously 'legacy', or 'none',
    704 	 * record it as the behavior of changelist_postfix() will be different.
    705 	 */
    706 	if ((clp->cl_prop == ZFS_PROP_MOUNTPOINT) &&
    707 	    (zfs_prop_get(zhp, prop, property, sizeof (property),
    708 	    NULL, NULL, 0, B_FALSE) == 0 &&
    709 	    (strcmp(property, "legacy") == 0 ||
    710 	    strcmp(property, "none") == 0))) {
    711 		/*
    712 		 * do not automatically mount ex-legacy datasets if
    713 		 * we specifically set canmount to noauto
    714 		 */
    715 		if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) !=
    716 		    ZFS_CANMOUNT_NOAUTO)
    717 			clp->cl_waslegacy = B_TRUE;
    718 	}
    719 
    720 	return (clp);
    721 }
    722