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.
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.
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]
23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
26 * Portions Copyright 2007 Ramprakash Jelari
27 * Copyright (c) 2014, 2016 by Delphix. All rights reserved.
28 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
41 #include "libzfs_impl.h"
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.
49 * The interface consists of the following sequence of functions:
54 * changelist_postfix()
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
65 typedef struct prop_changenode
{
66 zfs_handle_t
*cn_handle
;
70 boolean_t cn_needpost
; /* is postfix() needed? */
71 uu_list_node_t cn_listnode
;
74 struct prop_changelist
{
76 zfs_prop_t cl_realprop
;
77 zfs_prop_t cl_shareprop
; /* used with sharenfs/sharesmb */
78 uu_list_pool_t
*cl_pool
;
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
;
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.
96 changelist_prefix(prop_changelist_t
*clp
)
98 prop_changenode_t
*cn
;
101 if (clp
->cl_prop
!= ZFS_PROP_MOUNTPOINT
&&
102 clp
->cl_prop
!= ZFS_PROP_SHARESMB
)
105 for (cn
= uu_list_first(clp
->cl_list
); cn
!= NULL
;
106 cn
= uu_list_next(clp
->cl_list
, cn
)) {
108 /* if a previous loop failed, set the remaining to false */
110 cn
->cn_needpost
= B_FALSE
;
115 * If we are in the global zone, but this dataset is exported
116 * to a local zone, do nothing.
118 if (getzoneid() == GLOBAL_ZONEID
&& cn
->cn_zoned
)
121 if (!ZFS_IS_VOLUME(cn
->cn_handle
)) {
123 * Do the property specific processing.
125 switch (clp
->cl_prop
) {
126 case ZFS_PROP_MOUNTPOINT
:
127 if (zfs_unmount(cn
->cn_handle
, NULL
,
128 clp
->cl_mflags
) != 0) {
130 cn
->cn_needpost
= B_FALSE
;
133 case ZFS_PROP_SHARESMB
:
134 (void) zfs_unshare_smb(cn
->cn_handle
, NULL
);
144 (void) changelist_postfix(clp
);
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
159 changelist_postfix(prop_changelist_t
*clp
)
161 prop_changenode_t
*cn
;
162 char shareopts
[ZFS_MAXPROPLEN
];
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
;
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).
176 if ((cn
= uu_list_last(clp
->cl_list
)) == NULL
)
179 if (clp
->cl_prop
== ZFS_PROP_MOUNTPOINT
)
180 remove_mountpoint(cn
->cn_handle
);
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.
188 if (cn
->cn_handle
!= NULL
) {
189 hdl
= cn
->cn_handle
->zfs_hdl
;
191 zfs_uninit_libshare(hdl
);
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.
198 for (cn
= uu_list_last(clp
->cl_list
); cn
!= NULL
;
199 cn
= uu_list_prev(clp
->cl_list
, cn
)) {
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
);
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
,
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
222 for (cn
= uu_list_last(clp
->cl_list
); cn
!= NULL
;
223 cn
= uu_list_prev(clp
->cl_list
, cn
)) {
230 * If we are in the global zone, but this dataset is exported
231 * to a local zone, do nothing.
233 if (getzoneid() == GLOBAL_ZONEID
&& cn
->cn_zoned
)
236 /* Only do post-processing if it's required */
237 if (!cn
->cn_needpost
)
239 cn
->cn_needpost
= B_FALSE
;
241 if (ZFS_IS_VOLUME(cn
->cn_handle
))
245 * Remount if previously mounted or mountpoint was legacy,
246 * or sharenfs or sharesmb property is set.
248 sharenfs
= ((zfs_prop_get(cn
->cn_handle
, ZFS_PROP_SHARENFS
,
249 shareopts
, sizeof (shareopts
), NULL
, NULL
, 0,
250 B_FALSE
) == 0) && (strcmp(shareopts
, "off") != 0));
252 sharesmb
= ((zfs_prop_get(cn
->cn_handle
, ZFS_PROP_SHARESMB
,
253 shareopts
, sizeof (shareopts
), NULL
, NULL
, 0,
254 B_FALSE
) == 0) && (strcmp(shareopts
, "off") != 0));
256 mounted
= zfs_is_mounted(cn
->cn_handle
, NULL
);
258 if (!mounted
&& (cn
->cn_mounted
||
259 ((sharenfs
|| sharesmb
|| clp
->cl_waslegacy
) &&
260 (zfs_prop_get_int(cn
->cn_handle
,
261 ZFS_PROP_CANMOUNT
) == ZFS_CANMOUNT_ON
)))) {
263 if (zfs_mount(cn
->cn_handle
, NULL
, 0) != 0)
270 * If the file system is mounted we always re-share even
271 * if the filesystem is currently shared, so that we can
272 * adopt any new options.
274 if (sharenfs
&& mounted
)
275 errors
+= zfs_share_nfs(cn
->cn_handle
);
276 else if (cn
->cn_shared
|| clp
->cl_waslegacy
)
277 errors
+= zfs_unshare_nfs(cn
->cn_handle
, NULL
);
278 if (sharesmb
&& mounted
)
279 errors
+= zfs_share_smb(cn
->cn_handle
);
280 else if (cn
->cn_shared
|| clp
->cl_waslegacy
)
281 errors
+= zfs_unshare_smb(cn
->cn_handle
, NULL
);
284 return (errors
? -1 : 0);
288 * Is this "dataset" a child of "parent"?
291 isa_child_of(const char *dataset
, const char *parent
)
295 len
= strlen(parent
);
297 if (strncmp(dataset
, parent
, len
) == 0 &&
298 (dataset
[len
] == '@' || dataset
[len
] == '/' ||
299 dataset
[len
] == '\0'))
307 * If we rename a filesystem, child filesystem handles are no longer valid
308 * since we identify each dataset by its name in the ZFS namespace. As a
309 * result, we have to go through and fix up all the names appropriately. We
310 * could do this automatically if libzfs kept track of all open handles, but
311 * this is a lot less work.
314 changelist_rename(prop_changelist_t
*clp
, const char *src
, const char *dst
)
316 prop_changenode_t
*cn
;
317 char newname
[ZFS_MAX_DATASET_NAME_LEN
];
319 for (cn
= uu_list_first(clp
->cl_list
); cn
!= NULL
;
320 cn
= uu_list_next(clp
->cl_list
, cn
)) {
322 * Do not rename a clone that's not in the source hierarchy.
324 if (!isa_child_of(cn
->cn_handle
->zfs_name
, src
))
328 * Destroy the previous mountpoint if needed.
330 remove_mountpoint(cn
->cn_handle
);
332 (void) strlcpy(newname
, dst
, sizeof (newname
));
333 (void) strcat(newname
, cn
->cn_handle
->zfs_name
+ strlen(src
));
335 (void) strlcpy(cn
->cn_handle
->zfs_name
, newname
,
336 sizeof (cn
->cn_handle
->zfs_name
));
341 * Given a gathered changelist for the 'sharenfs' or 'sharesmb' property,
342 * unshare all the datasets in the list.
345 changelist_unshare(prop_changelist_t
*clp
, zfs_share_proto_t
*proto
)
347 prop_changenode_t
*cn
;
350 if (clp
->cl_prop
!= ZFS_PROP_SHARENFS
&&
351 clp
->cl_prop
!= ZFS_PROP_SHARESMB
)
354 for (cn
= uu_list_first(clp
->cl_list
); cn
!= NULL
;
355 cn
= uu_list_next(clp
->cl_list
, cn
)) {
356 if (zfs_unshare_proto(cn
->cn_handle
, NULL
, proto
) != 0)
364 * Check if there is any child exported to a local zone in a given changelist.
365 * This information has already been recorded while gathering the changelist
366 * via changelist_gather().
369 changelist_haszonedchild(prop_changelist_t
*clp
)
371 return (clp
->cl_haszonedchild
);
375 * Remove a node from a gathered list.
378 changelist_remove(prop_changelist_t
*clp
, const char *name
)
380 prop_changenode_t
*cn
;
382 for (cn
= uu_list_first(clp
->cl_list
); cn
!= NULL
;
383 cn
= uu_list_next(clp
->cl_list
, cn
)) {
385 if (strcmp(cn
->cn_handle
->zfs_name
, name
) == 0) {
386 uu_list_remove(clp
->cl_list
, cn
);
387 zfs_close(cn
->cn_handle
);
395 * Release any memory associated with a changelist.
398 changelist_free(prop_changelist_t
*clp
)
400 prop_changenode_t
*cn
;
405 while ((cn
= uu_list_teardown(clp
->cl_list
, &cookie
)) != NULL
) {
406 zfs_close(cn
->cn_handle
);
410 uu_list_destroy(clp
->cl_list
);
413 uu_list_pool_destroy(clp
->cl_pool
);
419 change_one(zfs_handle_t
*zhp
, void *data
)
421 prop_changelist_t
*clp
= data
;
422 char property
[ZFS_MAXPROPLEN
];
424 prop_changenode_t
*cn
;
425 zprop_source_t sourcetype
;
426 zprop_source_t share_sourcetype
;
429 * We only want to unmount/unshare those filesystems that may inherit
430 * from the target filesystem. If we find any filesystem with a
431 * locally set mountpoint, we ignore any children since changing the
432 * property will not affect them. If this is a rename, we iterate
433 * over all children regardless, since we need them unmounted in
434 * order to do the rename. Also, if this is a volume and we're doing
435 * a rename, then always add it to the changelist.
438 if (!(ZFS_IS_VOLUME(zhp
) && clp
->cl_realprop
== ZFS_PROP_NAME
) &&
439 zfs_prop_get(zhp
, clp
->cl_prop
, property
,
440 sizeof (property
), &sourcetype
, where
, sizeof (where
),
447 * If we are "watching" sharenfs or sharesmb
448 * then check out the companion property which is tracked
451 if (clp
->cl_shareprop
!= ZPROP_INVAL
&&
452 zfs_prop_get(zhp
, clp
->cl_shareprop
, property
,
453 sizeof (property
), &share_sourcetype
, where
, sizeof (where
),
459 if (clp
->cl_alldependents
|| clp
->cl_allchildren
||
460 sourcetype
== ZPROP_SRC_DEFAULT
||
461 sourcetype
== ZPROP_SRC_INHERITED
||
462 (clp
->cl_shareprop
!= ZPROP_INVAL
&&
463 (share_sourcetype
== ZPROP_SRC_DEFAULT
||
464 share_sourcetype
== ZPROP_SRC_INHERITED
))) {
465 if ((cn
= zfs_alloc(zfs_get_handle(zhp
),
466 sizeof (prop_changenode_t
))) == NULL
) {
472 cn
->cn_mounted
= (clp
->cl_gflags
& CL_GATHER_MOUNT_ALWAYS
) ||
473 zfs_is_mounted(zhp
, NULL
);
474 cn
->cn_shared
= zfs_is_shared(zhp
);
475 cn
->cn_zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
476 cn
->cn_needpost
= B_TRUE
;
478 /* Indicate if any child is exported to a local zone. */
479 if (getzoneid() == GLOBAL_ZONEID
&& cn
->cn_zoned
)
480 clp
->cl_haszonedchild
= B_TRUE
;
482 uu_list_node_init(cn
, &cn
->cn_listnode
, clp
->cl_pool
);
484 if (clp
->cl_sorted
) {
487 (void) uu_list_find(clp
->cl_list
, cn
, NULL
,
489 uu_list_insert(clp
->cl_list
, cn
, idx
);
492 * Add this child to beginning of the list. Children
493 * below this one in the hierarchy will get added above
494 * this one in the list. This produces a list in
495 * reverse dataset name order.
496 * This is necessary when the original mountpoint
499 ASSERT(!clp
->cl_alldependents
);
500 verify(uu_list_insert_before(clp
->cl_list
,
501 uu_list_first(clp
->cl_list
), cn
) == 0);
504 if (!clp
->cl_alldependents
)
505 return (zfs_iter_children(zhp
, change_one
, data
));
515 compare_mountpoints(const void *a
, const void *b
, void *unused
)
517 const prop_changenode_t
*ca
= a
;
518 const prop_changenode_t
*cb
= b
;
520 char mounta
[MAXPATHLEN
];
521 char mountb
[MAXPATHLEN
];
523 boolean_t hasmounta
, hasmountb
;
526 * When unsharing or unmounting filesystems, we need to do it in
527 * mountpoint order. This allows the user to have a mountpoint
528 * hierarchy that is different from the dataset hierarchy, and still
529 * allow it to be changed. However, if either dataset doesn't have a
530 * mountpoint (because it is a volume or a snapshot), we place it at the
531 * end of the list, because it doesn't affect our change at all.
533 hasmounta
= (zfs_prop_get(ca
->cn_handle
, ZFS_PROP_MOUNTPOINT
, mounta
,
534 sizeof (mounta
), NULL
, NULL
, 0, B_FALSE
) == 0);
535 hasmountb
= (zfs_prop_get(cb
->cn_handle
, ZFS_PROP_MOUNTPOINT
, mountb
,
536 sizeof (mountb
), NULL
, NULL
, 0, B_FALSE
) == 0);
538 if (!hasmounta
&& hasmountb
)
540 else if (hasmounta
&& !hasmountb
)
542 else if (!hasmounta
&& !hasmountb
)
545 return (strcmp(mountb
, mounta
));
549 * Given a ZFS handle and a property, construct a complete list of datasets
550 * that need to be modified as part of this process. For anything but the
551 * 'mountpoint' and 'sharenfs' properties, this just returns an empty list.
552 * Otherwise, we iterate over all children and look for any datasets that
553 * inherit the property. For each such dataset, we add it to the list and
554 * mark whether it was shared beforehand.
557 changelist_gather(zfs_handle_t
*zhp
, zfs_prop_t prop
, int gather_flags
,
560 prop_changelist_t
*clp
;
561 prop_changenode_t
*cn
;
563 char property
[ZFS_MAXPROPLEN
];
564 uu_compare_fn_t
*compare
= NULL
;
565 boolean_t legacy
= B_FALSE
;
567 if ((clp
= zfs_alloc(zhp
->zfs_hdl
, sizeof (prop_changelist_t
))) == NULL
)
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.
575 if (prop
== ZFS_PROP_NAME
|| prop
== ZFS_PROP_ZONED
||
576 prop
== ZFS_PROP_MOUNTPOINT
|| prop
== ZFS_PROP_SHARENFS
||
577 prop
== ZFS_PROP_SHARESMB
) {
579 if (zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
,
580 property
, sizeof (property
),
581 NULL
, NULL
, 0, B_FALSE
) == 0 &&
582 (strcmp(property
, "legacy") == 0 ||
583 strcmp(property
, "none") == 0)) {
588 compare
= compare_mountpoints
;
589 clp
->cl_sorted
= B_TRUE
;
593 clp
->cl_pool
= uu_list_pool_create("changelist_pool",
594 sizeof (prop_changenode_t
),
595 offsetof(prop_changenode_t
, cn_listnode
),
597 if (clp
->cl_pool
== NULL
) {
598 assert(uu_error() == UU_ERROR_NO_MEMORY
);
599 (void) zfs_error(zhp
->zfs_hdl
, EZFS_NOMEM
, "internal error");
600 changelist_free(clp
);
604 clp
->cl_list
= uu_list_create(clp
->cl_pool
, NULL
,
605 clp
->cl_sorted
? UU_LIST_SORTED
: 0);
606 clp
->cl_gflags
= gather_flags
;
607 clp
->cl_mflags
= mnt_flags
;
609 if (clp
->cl_list
== NULL
) {
610 assert(uu_error() == UU_ERROR_NO_MEMORY
);
611 (void) zfs_error(zhp
->zfs_hdl
, EZFS_NOMEM
, "internal error");
612 changelist_free(clp
);
617 * If this is a rename or the 'zoned' property, we pretend we're
618 * changing the mountpoint and flag it so we can catch all children in
621 * Flag cl_alldependents to catch all children plus the dependents
622 * (clones) that are not in the hierarchy.
624 if (prop
== ZFS_PROP_NAME
) {
625 clp
->cl_prop
= ZFS_PROP_MOUNTPOINT
;
626 clp
->cl_alldependents
= B_TRUE
;
627 } else if (prop
== ZFS_PROP_ZONED
) {
628 clp
->cl_prop
= ZFS_PROP_MOUNTPOINT
;
629 clp
->cl_allchildren
= B_TRUE
;
630 } else if (prop
== ZFS_PROP_CANMOUNT
) {
631 clp
->cl_prop
= ZFS_PROP_MOUNTPOINT
;
632 } else if (prop
== ZFS_PROP_VOLSIZE
) {
633 clp
->cl_prop
= ZFS_PROP_MOUNTPOINT
;
637 clp
->cl_realprop
= prop
;
639 if (clp
->cl_prop
!= ZFS_PROP_MOUNTPOINT
&&
640 clp
->cl_prop
!= ZFS_PROP_SHARENFS
&&
641 clp
->cl_prop
!= ZFS_PROP_SHARESMB
)
645 * If watching SHARENFS or SHARESMB then
646 * also watch its companion property.
648 if (clp
->cl_prop
== ZFS_PROP_SHARENFS
)
649 clp
->cl_shareprop
= ZFS_PROP_SHARESMB
;
650 else if (clp
->cl_prop
== ZFS_PROP_SHARESMB
)
651 clp
->cl_shareprop
= ZFS_PROP_SHARENFS
;
653 if (clp
->cl_alldependents
) {
654 if (zfs_iter_dependents(zhp
, B_TRUE
, change_one
, clp
) != 0) {
655 changelist_free(clp
);
658 } else if (zfs_iter_children(zhp
, change_one
, clp
) != 0) {
659 changelist_free(clp
);
664 * We have to re-open ourselves because we auto-close all the handles
665 * and can't tell the difference.
667 if ((temp
= zfs_open(zhp
->zfs_hdl
, zfs_get_name(zhp
),
668 ZFS_TYPE_DATASET
)) == NULL
) {
669 changelist_free(clp
);
674 * Always add ourself to the list. We add ourselves to the end so that
675 * we're the last to be unmounted.
677 if ((cn
= zfs_alloc(zhp
->zfs_hdl
,
678 sizeof (prop_changenode_t
))) == NULL
) {
680 changelist_free(clp
);
684 cn
->cn_handle
= temp
;
685 cn
->cn_mounted
= (clp
->cl_gflags
& CL_GATHER_MOUNT_ALWAYS
) ||
686 zfs_is_mounted(temp
, NULL
);
687 cn
->cn_shared
= zfs_is_shared(temp
);
688 cn
->cn_zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
689 cn
->cn_needpost
= B_TRUE
;
691 uu_list_node_init(cn
, &cn
->cn_listnode
, clp
->cl_pool
);
692 if (clp
->cl_sorted
) {
694 (void) uu_list_find(clp
->cl_list
, cn
, NULL
, &idx
);
695 uu_list_insert(clp
->cl_list
, cn
, idx
);
698 * Add the target dataset to the end of the list.
699 * The list is not really unsorted. The list will be
700 * in reverse dataset name order. This is necessary
701 * when the original mountpoint is legacy or none.
703 verify(uu_list_insert_after(clp
->cl_list
,
704 uu_list_last(clp
->cl_list
), cn
) == 0);
708 * If the mountpoint property was previously 'legacy', or 'none',
709 * record it as the behavior of changelist_postfix() will be different.
711 if ((clp
->cl_prop
== ZFS_PROP_MOUNTPOINT
) && legacy
) {
713 * do not automatically mount ex-legacy datasets if
714 * we specifically set canmount to noauto
716 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) !=
718 clp
->cl_waslegacy
= B_TRUE
;