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