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