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