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