xref: /freebsd/sys/contrib/openzfs/lib/libzfs/libzfs_changelist.c (revision 81f36fbc98dd74ca923938e0329919d426811b0c)
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 https://opensource.org/licenses/CDDL-1.0.
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, 2020 by Delphix. All rights reserved.
28  * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
29  * Copyright (c) 2018 Datto Inc.
30  */
31 
32 #include <libintl.h>
33 #include <libuutil.h>
34 #include <stddef.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <zone.h>
39 
40 #include <libzfs.h>
41 
42 #include "libzfs_impl.h"
43 
44 /*
45  * Structure to keep track of dataset state.  Before changing the 'sharenfs' or
46  * 'mountpoint' property, we record whether the filesystem was previously
47  * mounted/shared.  This prior state dictates whether we remount/reshare the
48  * dataset after the property has been changed.
49  *
50  * The interface consists of the following sequence of functions:
51  *
52  * 	changelist_gather()
53  * 	changelist_prefix()
54  * 	< change property >
55  * 	changelist_postfix()
56  * 	changelist_free()
57  *
58  * Other interfaces:
59  *
60  * changelist_remove() - remove a node from a gathered list
61  * changelist_rename() - renames all datasets appropriately when doing a rename
62  * changelist_unshare() - unshares all the nodes in a given changelist
63  * changelist_haszonedchild() - check if there is any child exported to
64  *				a local zone
65  */
66 typedef struct prop_changenode {
67 	zfs_handle_t		*cn_handle;
68 	int			cn_shared;
69 	int			cn_mounted;
70 	int			cn_zoned;
71 	boolean_t		cn_needpost;	/* is postfix() needed? */
72 	uu_avl_node_t		cn_treenode;
73 } prop_changenode_t;
74 
75 struct prop_changelist {
76 	zfs_prop_t		cl_prop;
77 	zfs_prop_t		cl_realprop;
78 	zfs_prop_t		cl_shareprop;  /* used with sharenfs/sharesmb */
79 	uu_avl_pool_t		*cl_pool;
80 	uu_avl_t		*cl_tree;
81 	boolean_t		cl_waslegacy;
82 	boolean_t		cl_allchildren;
83 	boolean_t		cl_alldependents;
84 	int			cl_mflags;	/* Mount flags */
85 	int			cl_gflags;	/* Gather request flags */
86 	boolean_t		cl_haszonedchild;
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 	uu_avl_walk_t *walk;
100 	int ret = 0;
101 	const enum sa_protocol smb[] = {SA_PROTOCOL_SMB, SA_NO_PROTOCOL};
102 	boolean_t commit_smb_shares = B_FALSE;
103 
104 	if (clp->cl_prop != ZFS_PROP_MOUNTPOINT &&
105 	    clp->cl_prop != ZFS_PROP_SHARESMB)
106 		return (0);
107 
108 	if ((walk = uu_avl_walk_start(clp->cl_tree, UU_WALK_ROBUST)) == NULL)
109 		return (-1);
110 
111 	while ((cn = uu_avl_walk_next(walk)) != NULL) {
112 
113 		/* if a previous loop failed, set the remaining to false */
114 		if (ret == -1) {
115 			cn->cn_needpost = B_FALSE;
116 			continue;
117 		}
118 
119 		/*
120 		 * If we are in the global zone, but this dataset is exported
121 		 * to a local zone, do nothing.
122 		 */
123 		if (getzoneid() == GLOBAL_ZONEID && cn->cn_zoned)
124 			continue;
125 
126 		if (!ZFS_IS_VOLUME(cn->cn_handle)) {
127 			/*
128 			 * Do the property specific processing.
129 			 */
130 			switch (clp->cl_prop) {
131 			case ZFS_PROP_MOUNTPOINT:
132 				if (clp->cl_gflags & CL_GATHER_DONT_UNMOUNT)
133 					break;
134 				if (zfs_unmount(cn->cn_handle, NULL,
135 				    clp->cl_mflags) != 0) {
136 					ret = -1;
137 					cn->cn_needpost = B_FALSE;
138 				}
139 				break;
140 			case ZFS_PROP_SHARESMB:
141 				(void) zfs_unshare(cn->cn_handle, NULL,
142 				    smb);
143 				commit_smb_shares = B_TRUE;
144 				break;
145 
146 			default:
147 				break;
148 			}
149 		}
150 	}
151 
152 	if (commit_smb_shares)
153 		zfs_commit_shares(smb);
154 	uu_avl_walk_end(walk);
155 
156 	if (ret == -1)
157 		(void) changelist_postfix(clp);
158 
159 	return (ret);
160 }
161 
162 /*
163  * If the property is 'mountpoint' or 'sharenfs', go through and remount and/or
164  * reshare the filesystems as necessary.  In changelist_gather() we recorded
165  * whether the filesystem was previously shared or mounted.  The action we take
166  * depends on the previous state, and whether the value was previously 'legacy'.
167  * For non-legacy properties, we only remount/reshare the filesystem if it was
168  * previously mounted/shared.  Otherwise, we always remount/reshare the
169  * filesystem.
170  */
171 int
172 changelist_postfix(prop_changelist_t *clp)
173 {
174 	prop_changenode_t *cn;
175 	uu_avl_walk_t *walk;
176 	char shareopts[ZFS_MAXPROPLEN];
177 	boolean_t commit_smb_shares = B_FALSE;
178 	boolean_t commit_nfs_shares = B_FALSE;
179 
180 	/*
181 	 * If we're changing the mountpoint, attempt to destroy the underlying
182 	 * mountpoint.  All other datasets will have inherited from this dataset
183 	 * (in which case their mountpoints exist in the filesystem in the new
184 	 * location), or have explicit mountpoints set (in which case they won't
185 	 * be in the changelist).
186 	 */
187 	if ((cn = uu_avl_last(clp->cl_tree)) == NULL)
188 		return (0);
189 
190 	if (clp->cl_prop == ZFS_PROP_MOUNTPOINT &&
191 	    !(clp->cl_gflags & CL_GATHER_DONT_UNMOUNT))
192 		remove_mountpoint(cn->cn_handle);
193 
194 	/*
195 	 * We walk the datasets in reverse, because we want to mount any parent
196 	 * datasets before mounting the children.  We walk all datasets even if
197 	 * there are errors.
198 	 */
199 	if ((walk = uu_avl_walk_start(clp->cl_tree,
200 	    UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
201 		return (-1);
202 
203 	while ((cn = uu_avl_walk_next(walk)) != NULL) {
204 
205 		boolean_t sharenfs;
206 		boolean_t sharesmb;
207 		boolean_t mounted;
208 		boolean_t needs_key;
209 
210 		/*
211 		 * If we are in the global zone, but this dataset is exported
212 		 * to a local zone, do nothing.
213 		 */
214 		if (getzoneid() == GLOBAL_ZONEID && cn->cn_zoned)
215 			continue;
216 
217 		/* Only do post-processing if it's required */
218 		if (!cn->cn_needpost)
219 			continue;
220 		cn->cn_needpost = B_FALSE;
221 
222 		zfs_refresh_properties(cn->cn_handle);
223 
224 		if (ZFS_IS_VOLUME(cn->cn_handle))
225 			continue;
226 
227 		/*
228 		 * Remount if previously mounted or mountpoint was legacy,
229 		 * or sharenfs or sharesmb  property is set.
230 		 */
231 		sharenfs = ((zfs_prop_get(cn->cn_handle, ZFS_PROP_SHARENFS,
232 		    shareopts, sizeof (shareopts), NULL, NULL, 0,
233 		    B_FALSE) == 0) && (strcmp(shareopts, "off") != 0));
234 
235 		sharesmb = ((zfs_prop_get(cn->cn_handle, ZFS_PROP_SHARESMB,
236 		    shareopts, sizeof (shareopts), NULL, NULL, 0,
237 		    B_FALSE) == 0) && (strcmp(shareopts, "off") != 0));
238 
239 		needs_key = (zfs_prop_get_int(cn->cn_handle,
240 		    ZFS_PROP_KEYSTATUS) == ZFS_KEYSTATUS_UNAVAILABLE);
241 
242 		mounted = (clp->cl_gflags & CL_GATHER_DONT_UNMOUNT) ||
243 		    zfs_is_mounted(cn->cn_handle, NULL);
244 
245 		if (!mounted && !needs_key && (cn->cn_mounted ||
246 		    (((clp->cl_prop == ZFS_PROP_MOUNTPOINT &&
247 		    clp->cl_prop == clp->cl_realprop) ||
248 		    sharenfs || sharesmb || clp->cl_waslegacy) &&
249 		    (zfs_prop_get_int(cn->cn_handle,
250 		    ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_ON)))) {
251 
252 			if (zfs_mount(cn->cn_handle, NULL, 0) == 0)
253 				mounted = TRUE;
254 		}
255 
256 		/*
257 		 * If the file system is mounted we always re-share even
258 		 * if the filesystem is currently shared, so that we can
259 		 * adopt any new options.
260 		 */
261 		const enum sa_protocol nfs[] =
262 		    {SA_PROTOCOL_NFS, SA_NO_PROTOCOL};
263 		if (sharenfs && mounted) {
264 			zfs_share(cn->cn_handle, nfs);
265 			commit_nfs_shares = B_TRUE;
266 		} else if (cn->cn_shared || clp->cl_waslegacy) {
267 			zfs_unshare(cn->cn_handle, NULL, nfs);
268 			commit_nfs_shares = B_TRUE;
269 		}
270 		const enum sa_protocol smb[] =
271 		    {SA_PROTOCOL_SMB, SA_NO_PROTOCOL};
272 		if (sharesmb && mounted) {
273 			zfs_share(cn->cn_handle, smb);
274 			commit_smb_shares = B_TRUE;
275 		} else if (cn->cn_shared || clp->cl_waslegacy) {
276 			zfs_unshare(cn->cn_handle, NULL, smb);
277 			commit_smb_shares = B_TRUE;
278 		}
279 	}
280 
281 	enum sa_protocol proto[SA_PROTOCOL_COUNT + 1], *p = proto;
282 	if (commit_nfs_shares)
283 		*p++ = SA_PROTOCOL_NFS;
284 	if (commit_smb_shares)
285 		*p++ = SA_PROTOCOL_SMB;
286 	*p++ = SA_NO_PROTOCOL;
287 	zfs_commit_shares(proto);
288 	uu_avl_walk_end(walk);
289 
290 	return (0);
291 }
292 
293 /*
294  * Is this "dataset" a child of "parent"?
295  */
296 static boolean_t
297 isa_child_of(const char *dataset, const char *parent)
298 {
299 	int len;
300 
301 	len = strlen(parent);
302 
303 	if (strncmp(dataset, parent, len) == 0 &&
304 	    (dataset[len] == '@' || dataset[len] == '/' ||
305 	    dataset[len] == '\0'))
306 		return (B_TRUE);
307 	else
308 		return (B_FALSE);
309 
310 }
311 
312 /*
313  * If we rename a filesystem, child filesystem handles are no longer valid
314  * since we identify each dataset by its name in the ZFS namespace.  As a
315  * result, we have to go through and fix up all the names appropriately.  We
316  * could do this automatically if libzfs kept track of all open handles, but
317  * this is a lot less work.
318  */
319 void
320 changelist_rename(prop_changelist_t *clp, const char *src, const char *dst)
321 {
322 	prop_changenode_t *cn;
323 	uu_avl_walk_t *walk;
324 	char newname[ZFS_MAX_DATASET_NAME_LEN];
325 
326 	if ((walk = uu_avl_walk_start(clp->cl_tree, UU_WALK_ROBUST)) == NULL)
327 		return;
328 
329 	while ((cn = uu_avl_walk_next(walk)) != NULL) {
330 		/*
331 		 * Do not rename a clone that's not in the source hierarchy.
332 		 */
333 		if (!isa_child_of(cn->cn_handle->zfs_name, src))
334 			continue;
335 
336 		/*
337 		 * Destroy the previous mountpoint if needed.
338 		 */
339 		remove_mountpoint(cn->cn_handle);
340 
341 		(void) strlcpy(newname, dst, sizeof (newname));
342 		(void) strlcat(newname, cn->cn_handle->zfs_name + strlen(src),
343 		    sizeof (newname));
344 
345 		(void) strlcpy(cn->cn_handle->zfs_name, newname,
346 		    sizeof (cn->cn_handle->zfs_name));
347 	}
348 
349 	uu_avl_walk_end(walk);
350 }
351 
352 /*
353  * Given a gathered changelist for the 'sharenfs' or 'sharesmb' property,
354  * unshare all the datasets in the list.
355  */
356 int
357 changelist_unshare(prop_changelist_t *clp, const enum sa_protocol *proto)
358 {
359 	prop_changenode_t *cn;
360 	uu_avl_walk_t *walk;
361 	int ret = 0;
362 
363 	if (clp->cl_prop != ZFS_PROP_SHARENFS &&
364 	    clp->cl_prop != ZFS_PROP_SHARESMB)
365 		return (0);
366 
367 	if ((walk = uu_avl_walk_start(clp->cl_tree, UU_WALK_ROBUST)) == NULL)
368 		return (-1);
369 
370 	while ((cn = uu_avl_walk_next(walk)) != NULL) {
371 		if (zfs_unshare(cn->cn_handle, NULL, proto) != 0)
372 			ret = -1;
373 	}
374 
375 	for (const enum sa_protocol *p = proto; *p != SA_NO_PROTOCOL; ++p)
376 		sa_commit_shares(*p);
377 	uu_avl_walk_end(walk);
378 
379 	return (ret);
380 }
381 
382 /*
383  * Check if there is any child exported to a local zone in a given changelist.
384  * This information has already been recorded while gathering the changelist
385  * via changelist_gather().
386  */
387 int
388 changelist_haszonedchild(prop_changelist_t *clp)
389 {
390 	return (clp->cl_haszonedchild);
391 }
392 
393 /*
394  * Remove a node from a gathered list.
395  */
396 void
397 changelist_remove(prop_changelist_t *clp, const char *name)
398 {
399 	prop_changenode_t *cn;
400 	uu_avl_walk_t *walk;
401 
402 	if ((walk = uu_avl_walk_start(clp->cl_tree, UU_WALK_ROBUST)) == NULL)
403 		return;
404 
405 	while ((cn = uu_avl_walk_next(walk)) != NULL) {
406 		if (strcmp(cn->cn_handle->zfs_name, name) == 0) {
407 			uu_avl_remove(clp->cl_tree, cn);
408 			zfs_close(cn->cn_handle);
409 			free(cn);
410 			uu_avl_walk_end(walk);
411 			return;
412 		}
413 	}
414 
415 	uu_avl_walk_end(walk);
416 }
417 
418 /*
419  * Release any memory associated with a changelist.
420  */
421 void
422 changelist_free(prop_changelist_t *clp)
423 {
424 	prop_changenode_t *cn;
425 
426 	if (clp->cl_tree) {
427 		uu_avl_walk_t *walk;
428 
429 		if ((walk = uu_avl_walk_start(clp->cl_tree,
430 		    UU_WALK_ROBUST)) == NULL)
431 			return;
432 
433 		while ((cn = uu_avl_walk_next(walk)) != NULL) {
434 			uu_avl_remove(clp->cl_tree, cn);
435 			zfs_close(cn->cn_handle);
436 			free(cn);
437 		}
438 
439 		uu_avl_walk_end(walk);
440 		uu_avl_destroy(clp->cl_tree);
441 	}
442 	if (clp->cl_pool)
443 		uu_avl_pool_destroy(clp->cl_pool);
444 
445 	free(clp);
446 }
447 
448 /*
449  * Add one dataset to changelist
450  */
451 static int
452 changelist_add_mounted(zfs_handle_t *zhp, void *data)
453 {
454 	prop_changelist_t *clp = data;
455 	prop_changenode_t *cn;
456 	uu_avl_index_t idx;
457 
458 	ASSERT3U(clp->cl_prop, ==, ZFS_PROP_MOUNTPOINT);
459 
460 	cn = zfs_alloc(zfs_get_handle(zhp), sizeof (prop_changenode_t));
461 	cn->cn_handle = zhp;
462 	cn->cn_mounted = zfs_is_mounted(zhp, NULL);
463 	ASSERT3U(cn->cn_mounted, ==, B_TRUE);
464 	cn->cn_shared = zfs_is_shared(zhp, NULL, NULL);
465 	cn->cn_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
466 	cn->cn_needpost = B_TRUE;
467 
468 	/* Indicate if any child is exported to a local zone. */
469 	if (getzoneid() == GLOBAL_ZONEID && cn->cn_zoned)
470 		clp->cl_haszonedchild = B_TRUE;
471 
472 	uu_avl_node_init(cn, &cn->cn_treenode, clp->cl_pool);
473 
474 	if (uu_avl_find(clp->cl_tree, cn, NULL, &idx) == NULL) {
475 		uu_avl_insert(clp->cl_tree, cn, idx);
476 	} else {
477 		free(cn);
478 		zfs_close(zhp);
479 	}
480 
481 	return (0);
482 }
483 
484 static int
485 change_one(zfs_handle_t *zhp, void *data)
486 {
487 	prop_changelist_t *clp = data;
488 	char property[ZFS_MAXPROPLEN];
489 	char where[64];
490 	prop_changenode_t *cn = NULL;
491 	zprop_source_t sourcetype = ZPROP_SRC_NONE;
492 	zprop_source_t share_sourcetype = ZPROP_SRC_NONE;
493 	int ret = 0;
494 
495 	/*
496 	 * We only want to unmount/unshare those filesystems that may inherit
497 	 * from the target filesystem.  If we find any filesystem with a
498 	 * locally set mountpoint, we ignore any children since changing the
499 	 * property will not affect them.  If this is a rename, we iterate
500 	 * over all children regardless, since we need them unmounted in
501 	 * order to do the rename.  Also, if this is a volume and we're doing
502 	 * a rename, then always add it to the changelist.
503 	 */
504 
505 	if (!(ZFS_IS_VOLUME(zhp) && clp->cl_realprop == ZFS_PROP_NAME) &&
506 	    zfs_prop_get(zhp, clp->cl_prop, property,
507 	    sizeof (property), &sourcetype, where, sizeof (where),
508 	    B_FALSE) != 0) {
509 		goto out;
510 	}
511 
512 	/*
513 	 * If we are "watching" sharenfs or sharesmb
514 	 * then check out the companion property which is tracked
515 	 * in cl_shareprop
516 	 */
517 	if (clp->cl_shareprop != ZPROP_INVAL &&
518 	    zfs_prop_get(zhp, clp->cl_shareprop, property,
519 	    sizeof (property), &share_sourcetype, where, sizeof (where),
520 	    B_FALSE) != 0) {
521 		goto out;
522 	}
523 
524 	if (clp->cl_alldependents || clp->cl_allchildren ||
525 	    sourcetype == ZPROP_SRC_DEFAULT ||
526 	    sourcetype == ZPROP_SRC_INHERITED ||
527 	    (clp->cl_shareprop != ZPROP_INVAL &&
528 	    (share_sourcetype == ZPROP_SRC_DEFAULT ||
529 	    share_sourcetype == ZPROP_SRC_INHERITED))) {
530 		cn = zfs_alloc(zfs_get_handle(zhp), sizeof (prop_changenode_t));
531 		cn->cn_handle = zhp;
532 		cn->cn_mounted = (clp->cl_gflags & CL_GATHER_MOUNT_ALWAYS) ||
533 		    zfs_is_mounted(zhp, NULL);
534 		cn->cn_shared = zfs_is_shared(zhp, NULL, NULL);
535 		cn->cn_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
536 		cn->cn_needpost = B_TRUE;
537 
538 		/* Indicate if any child is exported to a local zone. */
539 		if (getzoneid() == GLOBAL_ZONEID && cn->cn_zoned)
540 			clp->cl_haszonedchild = B_TRUE;
541 
542 		uu_avl_node_init(cn, &cn->cn_treenode, clp->cl_pool);
543 
544 		uu_avl_index_t idx;
545 
546 		if (uu_avl_find(clp->cl_tree, cn, NULL, &idx) == NULL) {
547 			uu_avl_insert(clp->cl_tree, cn, idx);
548 		} else {
549 			free(cn);
550 			cn = NULL;
551 		}
552 
553 		if (!clp->cl_alldependents)
554 			ret = zfs_iter_children_v2(zhp, 0, change_one, data);
555 
556 		/*
557 		 * If we added the handle to the changelist, we will re-use it
558 		 * later so return without closing it.
559 		 */
560 		if (cn != NULL)
561 			return (ret);
562 	}
563 
564 out:
565 	zfs_close(zhp);
566 	return (ret);
567 }
568 
569 static int
570 compare_props(const void *a, const void *b, zfs_prop_t prop)
571 {
572 	const prop_changenode_t *ca = a;
573 	const prop_changenode_t *cb = b;
574 
575 	char propa[MAXPATHLEN];
576 	char propb[MAXPATHLEN];
577 
578 	boolean_t haspropa, haspropb;
579 
580 	haspropa = (zfs_prop_get(ca->cn_handle, prop, propa, sizeof (propa),
581 	    NULL, NULL, 0, B_FALSE) == 0);
582 	haspropb = (zfs_prop_get(cb->cn_handle, prop, propb, sizeof (propb),
583 	    NULL, NULL, 0, B_FALSE) == 0);
584 
585 	if (!haspropa && haspropb)
586 		return (-1);
587 	else if (haspropa && !haspropb)
588 		return (1);
589 	else if (!haspropa && !haspropb)
590 		return (0);
591 	else
592 		return (strcmp(propb, propa));
593 }
594 
595 static int
596 compare_mountpoints(const void *a, const void *b, void *unused)
597 {
598 	/*
599 	 * When unsharing or unmounting filesystems, we need to do it in
600 	 * mountpoint order.  This allows the user to have a mountpoint
601 	 * hierarchy that is different from the dataset hierarchy, and still
602 	 * allow it to be changed.
603 	 */
604 	(void) unused;
605 	return (compare_props(a, b, ZFS_PROP_MOUNTPOINT));
606 }
607 
608 static int
609 compare_dataset_names(const void *a, const void *b, void *unused)
610 {
611 	(void) unused;
612 	return (compare_props(a, b, ZFS_PROP_NAME));
613 }
614 
615 /*
616  * Given a ZFS handle and a property, construct a complete list of datasets
617  * that need to be modified as part of this process.  For anything but the
618  * 'mountpoint' and 'sharenfs' properties, this just returns an empty list.
619  * Otherwise, we iterate over all children and look for any datasets that
620  * inherit the property.  For each such dataset, we add it to the list and
621  * mark whether it was shared beforehand.
622  */
623 prop_changelist_t *
624 changelist_gather(zfs_handle_t *zhp, zfs_prop_t prop, int gather_flags,
625     int mnt_flags)
626 {
627 	prop_changelist_t *clp;
628 	prop_changenode_t *cn;
629 	zfs_handle_t *temp;
630 	char property[ZFS_MAXPROPLEN];
631 	boolean_t legacy = B_FALSE;
632 
633 	clp = zfs_alloc(zhp->zfs_hdl, sizeof (prop_changelist_t));
634 
635 	/*
636 	 * For mountpoint-related tasks, we want to sort everything by
637 	 * mountpoint, so that we mount and unmount them in the appropriate
638 	 * order, regardless of their position in the hierarchy.
639 	 */
640 	if (prop == ZFS_PROP_NAME || prop == ZFS_PROP_ZONED ||
641 	    prop == ZFS_PROP_MOUNTPOINT || prop == ZFS_PROP_SHARENFS ||
642 	    prop == ZFS_PROP_SHARESMB) {
643 
644 		if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
645 		    property, sizeof (property),
646 		    NULL, NULL, 0, B_FALSE) == 0 &&
647 		    (strcmp(property, "legacy") == 0 ||
648 		    strcmp(property, "none") == 0)) {
649 			legacy = B_TRUE;
650 		}
651 	}
652 
653 	clp->cl_pool = uu_avl_pool_create("changelist_pool",
654 	    sizeof (prop_changenode_t),
655 	    offsetof(prop_changenode_t, cn_treenode),
656 	    legacy ? compare_dataset_names : compare_mountpoints, 0);
657 	if (clp->cl_pool == NULL) {
658 		assert(uu_error() == UU_ERROR_NO_MEMORY);
659 		(void) zfs_error(zhp->zfs_hdl, EZFS_NOMEM, "internal error");
660 		changelist_free(clp);
661 		return (NULL);
662 	}
663 
664 	clp->cl_tree = uu_avl_create(clp->cl_pool, NULL, UU_DEFAULT);
665 	clp->cl_gflags = gather_flags;
666 	clp->cl_mflags = mnt_flags;
667 
668 	if (clp->cl_tree == NULL) {
669 		assert(uu_error() == UU_ERROR_NO_MEMORY);
670 		(void) zfs_error(zhp->zfs_hdl, EZFS_NOMEM, "internal error");
671 		changelist_free(clp);
672 		return (NULL);
673 	}
674 
675 	/*
676 	 * If this is a rename or the 'zoned' property, we pretend we're
677 	 * changing the mountpoint and flag it so we can catch all children in
678 	 * change_one().
679 	 *
680 	 * Flag cl_alldependents to catch all children plus the dependents
681 	 * (clones) that are not in the hierarchy.
682 	 */
683 	if (prop == ZFS_PROP_NAME) {
684 		clp->cl_prop = ZFS_PROP_MOUNTPOINT;
685 		clp->cl_alldependents = B_TRUE;
686 	} else if (prop == ZFS_PROP_ZONED) {
687 		clp->cl_prop = ZFS_PROP_MOUNTPOINT;
688 		clp->cl_allchildren = B_TRUE;
689 	} else if (prop == ZFS_PROP_CANMOUNT) {
690 		clp->cl_prop = ZFS_PROP_MOUNTPOINT;
691 	} else if (prop == ZFS_PROP_VOLSIZE) {
692 		clp->cl_prop = ZFS_PROP_MOUNTPOINT;
693 	} else {
694 		clp->cl_prop = prop;
695 	}
696 	clp->cl_realprop = prop;
697 
698 	if (clp->cl_prop != ZFS_PROP_MOUNTPOINT &&
699 	    clp->cl_prop != ZFS_PROP_SHARENFS &&
700 	    clp->cl_prop != ZFS_PROP_SHARESMB)
701 		return (clp);
702 
703 	/*
704 	 * If watching SHARENFS or SHARESMB then
705 	 * also watch its companion property.
706 	 */
707 	if (clp->cl_prop == ZFS_PROP_SHARENFS)
708 		clp->cl_shareprop = ZFS_PROP_SHARESMB;
709 	else if (clp->cl_prop == ZFS_PROP_SHARESMB)
710 		clp->cl_shareprop = ZFS_PROP_SHARENFS;
711 
712 	if (clp->cl_prop == ZFS_PROP_MOUNTPOINT &&
713 	    (clp->cl_gflags & CL_GATHER_ITER_MOUNTED)) {
714 		/*
715 		 * Instead of iterating through all of the dataset children we
716 		 * gather mounted dataset children from MNTTAB
717 		 */
718 		if (zfs_iter_mounted(zhp, changelist_add_mounted, clp) != 0) {
719 			changelist_free(clp);
720 			return (NULL);
721 		}
722 	} else if (clp->cl_alldependents) {
723 		if (zfs_iter_dependents_v2(zhp, 0, B_TRUE, change_one,
724 		    clp) != 0) {
725 			changelist_free(clp);
726 			return (NULL);
727 		}
728 	} else if (zfs_iter_children_v2(zhp, 0, change_one, clp) != 0) {
729 		changelist_free(clp);
730 		return (NULL);
731 	}
732 
733 	/*
734 	 * We have to re-open ourselves because we auto-close all the handles
735 	 * and can't tell the difference.
736 	 */
737 	if ((temp = zfs_open(zhp->zfs_hdl, zfs_get_name(zhp),
738 	    ZFS_TYPE_DATASET)) == NULL) {
739 		changelist_free(clp);
740 		return (NULL);
741 	}
742 
743 	/*
744 	 * Always add ourself to the list.  We add ourselves to the end so that
745 	 * we're the last to be unmounted.
746 	 */
747 	cn = zfs_alloc(zhp->zfs_hdl, sizeof (prop_changenode_t));
748 	cn->cn_handle = temp;
749 	cn->cn_mounted = (clp->cl_gflags & CL_GATHER_MOUNT_ALWAYS) ||
750 	    zfs_is_mounted(temp, NULL);
751 	cn->cn_shared = zfs_is_shared(temp, NULL, NULL);
752 	cn->cn_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
753 	cn->cn_needpost = B_TRUE;
754 
755 	uu_avl_node_init(cn, &cn->cn_treenode, clp->cl_pool);
756 	uu_avl_index_t idx;
757 	if (uu_avl_find(clp->cl_tree, cn, NULL, &idx) == NULL) {
758 		uu_avl_insert(clp->cl_tree, cn, idx);
759 	} else {
760 		free(cn);
761 		zfs_close(temp);
762 	}
763 
764 	/*
765 	 * If the mountpoint property was previously 'legacy', or 'none',
766 	 * record it as the behavior of changelist_postfix() will be different.
767 	 */
768 	if ((clp->cl_prop == ZFS_PROP_MOUNTPOINT) && legacy) {
769 		/*
770 		 * do not automatically mount ex-legacy datasets if
771 		 * we specifically set canmount to noauto
772 		 */
773 		if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) !=
774 		    ZFS_CANMOUNT_NOAUTO)
775 			clp->cl_waslegacy = B_TRUE;
776 	}
777 
778 	return (clp);
779 }
780