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