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