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