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