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