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 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
40 #include "libzfs_impl.h"
43 * Structure to keep track of dataset state. Before changing the 'sharenfs' or
44 * 'mountpoint' property, we record whether the filesystem was previously
45 * mounted/shared. This prior state dictates whether we remount/reshare the
46 * dataset after the property has been changed.
48 * The interface consists of the following sequence of functions:
53 * changelist_postfix()
58 * changelist_remove() - remove a node from a gathered list
59 * changelist_rename() - renames all datasets appropriately when doing a rename
60 * changelist_unshare() - unshares all the nodes in a given changelist
61 * changelist_haszonedchild() - check if there is any child exported to
64 typedef struct prop_changenode
{
65 zfs_handle_t
*cn_handle
;
69 boolean_t cn_needpost
; /* is postfix() needed? */
70 uu_list_node_t cn_listnode
;
73 struct prop_changelist
{
75 zfs_prop_t cl_realprop
;
76 zfs_prop_t cl_shareprop
; /* used with sharenfs/sharesmb */
77 uu_list_pool_t
*cl_pool
;
79 boolean_t cl_waslegacy
;
80 boolean_t cl_allchildren
;
81 boolean_t cl_alldependents
;
82 int cl_mflags
; /* Mount flags */
83 int cl_gflags
; /* Gather request flags */
84 boolean_t cl_haszonedchild
;
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.
95 changelist_prefix(prop_changelist_t
*clp
)
97 prop_changenode_t
*cn
;
100 if (clp
->cl_prop
!= ZFS_PROP_MOUNTPOINT
&&
101 clp
->cl_prop
!= ZFS_PROP_SHARESMB
)
104 for (cn
= uu_list_first(clp
->cl_list
); cn
!= NULL
;
105 cn
= uu_list_next(clp
->cl_list
, cn
)) {
107 /* if a previous loop failed, set the remaining to false */
109 cn
->cn_needpost
= B_FALSE
;
114 * If we are in the global zone, but this dataset is exported
115 * to a local zone, do nothing.
117 if (getzoneid() == GLOBAL_ZONEID
&& cn
->cn_zoned
)
120 if (!ZFS_IS_VOLUME(cn
->cn_handle
)) {
122 * Do the property specific processing.
124 switch (clp
->cl_prop
) {
125 case ZFS_PROP_MOUNTPOINT
:
126 if (zfs_unmount(cn
->cn_handle
, NULL
,
127 clp
->cl_mflags
) != 0) {
129 cn
->cn_needpost
= B_FALSE
;
132 case ZFS_PROP_SHARESMB
:
133 (void) zfs_unshare_smb(cn
->cn_handle
, NULL
);
143 (void) changelist_postfix(clp
);
149 * If the property is 'mountpoint' or 'sharenfs', go through and remount and/or
150 * reshare the filesystems as necessary. In changelist_gather() we recorded
151 * whether the filesystem was previously shared or mounted. The action we take
152 * depends on the previous state, and whether the value was previously 'legacy'.
153 * For non-legacy properties, we only remount/reshare the filesystem if it was
154 * previously mounted/shared. Otherwise, we always remount/reshare the
158 changelist_postfix(prop_changelist_t
*clp
)
160 prop_changenode_t
*cn
;
161 char shareopts
[ZFS_MAXPROPLEN
];
163 libzfs_handle_t
*hdl
;
166 * If we're changing the mountpoint, attempt to destroy the underlying
167 * mountpoint. All other datasets will have inherited from this dataset
168 * (in which case their mountpoints exist in the filesystem in the new
169 * location), or have explicit mountpoints set (in which case they won't
170 * be in the changelist).
172 if ((cn
= uu_list_last(clp
->cl_list
)) == NULL
)
175 if (clp
->cl_prop
== ZFS_PROP_MOUNTPOINT
)
176 remove_mountpoint(cn
->cn_handle
);
179 * It is possible that the changelist_prefix() used libshare
180 * to unshare some entries. Since libshare caches data, an
181 * attempt to reshare during postfix can fail unless libshare
182 * is uninitialized here so that it will reinitialize later.
184 if (cn
->cn_handle
!= NULL
) {
185 hdl
= cn
->cn_handle
->zfs_hdl
;
187 zfs_uninit_libshare(hdl
);
191 * We walk the datasets in reverse, because we want to mount any parent
192 * datasets before mounting the children. We walk all datasets even if
195 for (cn
= uu_list_last(clp
->cl_list
); cn
!= NULL
;
196 cn
= uu_list_prev(clp
->cl_list
, cn
)) {
203 * If we are in the global zone, but this dataset is exported
204 * to a local zone, do nothing.
206 if (getzoneid() == GLOBAL_ZONEID
&& cn
->cn_zoned
)
209 /* Only do post-processing if it's required */
210 if (!cn
->cn_needpost
)
212 cn
->cn_needpost
= B_FALSE
;
214 zfs_refresh_properties(cn
->cn_handle
);
216 if (ZFS_IS_VOLUME(cn
->cn_handle
))
220 * Remount if previously mounted or mountpoint was legacy,
221 * or sharenfs or sharesmb property is set.
223 sharenfs
= ((zfs_prop_get(cn
->cn_handle
, ZFS_PROP_SHARENFS
,
224 shareopts
, sizeof (shareopts
), NULL
, NULL
, 0,
225 B_FALSE
) == 0) && (strcmp(shareopts
, "off") != 0));
227 sharesmb
= ((zfs_prop_get(cn
->cn_handle
, ZFS_PROP_SHARESMB
,
228 shareopts
, sizeof (shareopts
), NULL
, NULL
, 0,
229 B_FALSE
) == 0) && (strcmp(shareopts
, "off") != 0));
231 mounted
= zfs_is_mounted(cn
->cn_handle
, NULL
);
233 if (!mounted
&& (cn
->cn_mounted
||
234 ((sharenfs
|| sharesmb
|| clp
->cl_waslegacy
) &&
235 (zfs_prop_get_int(cn
->cn_handle
,
236 ZFS_PROP_CANMOUNT
) == ZFS_CANMOUNT_ON
)))) {
238 if (zfs_mount(cn
->cn_handle
, NULL
, 0) != 0)
245 * If the file system is mounted we always re-share even
246 * if the filesystem is currently shared, so that we can
247 * adopt any new options.
249 if (sharenfs
&& mounted
)
250 errors
+= zfs_share_nfs(cn
->cn_handle
);
251 else if (cn
->cn_shared
|| clp
->cl_waslegacy
)
252 errors
+= zfs_unshare_nfs(cn
->cn_handle
, NULL
);
253 if (sharesmb
&& mounted
)
254 errors
+= zfs_share_smb(cn
->cn_handle
);
255 else if (cn
->cn_shared
|| clp
->cl_waslegacy
)
256 errors
+= zfs_unshare_smb(cn
->cn_handle
, NULL
);
259 return (errors
? -1 : 0);
263 * Is this "dataset" a child of "parent"?
266 isa_child_of(const char *dataset
, const char *parent
)
270 len
= strlen(parent
);
272 if (strncmp(dataset
, parent
, len
) == 0 &&
273 (dataset
[len
] == '@' || dataset
[len
] == '/' ||
274 dataset
[len
] == '\0'))
282 * If we rename a filesystem, child filesystem handles are no longer valid
283 * since we identify each dataset by its name in the ZFS namespace. As a
284 * result, we have to go through and fix up all the names appropriately. We
285 * could do this automatically if libzfs kept track of all open handles, but
286 * this is a lot less work.
289 changelist_rename(prop_changelist_t
*clp
, const char *src
, const char *dst
)
291 prop_changenode_t
*cn
;
292 char newname
[ZFS_MAXNAMELEN
];
294 for (cn
= uu_list_first(clp
->cl_list
); cn
!= NULL
;
295 cn
= uu_list_next(clp
->cl_list
, cn
)) {
297 * Do not rename a clone that's not in the source hierarchy.
299 if (!isa_child_of(cn
->cn_handle
->zfs_name
, src
))
303 * Destroy the previous mountpoint if needed.
305 remove_mountpoint(cn
->cn_handle
);
307 (void) strlcpy(newname
, dst
, sizeof (newname
));
308 (void) strcat(newname
, cn
->cn_handle
->zfs_name
+ strlen(src
));
310 (void) strlcpy(cn
->cn_handle
->zfs_name
, newname
,
311 sizeof (cn
->cn_handle
->zfs_name
));
316 * Given a gathered changelist for the 'sharenfs' or 'sharesmb' property,
317 * unshare all the datasets in the list.
320 changelist_unshare(prop_changelist_t
*clp
, zfs_share_proto_t
*proto
)
322 prop_changenode_t
*cn
;
325 if (clp
->cl_prop
!= ZFS_PROP_SHARENFS
&&
326 clp
->cl_prop
!= ZFS_PROP_SHARESMB
)
329 for (cn
= uu_list_first(clp
->cl_list
); cn
!= NULL
;
330 cn
= uu_list_next(clp
->cl_list
, cn
)) {
331 if (zfs_unshare_proto(cn
->cn_handle
, NULL
, proto
) != 0)
339 * Check if there is any child exported to a local zone in a given changelist.
340 * This information has already been recorded while gathering the changelist
341 * via changelist_gather().
344 changelist_haszonedchild(prop_changelist_t
*clp
)
346 return (clp
->cl_haszonedchild
);
350 * Remove a node from a gathered list.
353 changelist_remove(prop_changelist_t
*clp
, const char *name
)
355 prop_changenode_t
*cn
;
357 for (cn
= uu_list_first(clp
->cl_list
); cn
!= NULL
;
358 cn
= uu_list_next(clp
->cl_list
, cn
)) {
360 if (strcmp(cn
->cn_handle
->zfs_name
, name
) == 0) {
361 uu_list_remove(clp
->cl_list
, cn
);
362 zfs_close(cn
->cn_handle
);
370 * Release any memory associated with a changelist.
373 changelist_free(prop_changelist_t
*clp
)
375 prop_changenode_t
*cn
;
380 while ((cn
= uu_list_teardown(clp
->cl_list
, &cookie
)) != NULL
) {
381 zfs_close(cn
->cn_handle
);
385 uu_list_destroy(clp
->cl_list
);
388 uu_list_pool_destroy(clp
->cl_pool
);
394 change_one(zfs_handle_t
*zhp
, void *data
)
396 prop_changelist_t
*clp
= data
;
397 char property
[ZFS_MAXPROPLEN
];
399 prop_changenode_t
*cn
;
400 zprop_source_t sourcetype
;
401 zprop_source_t share_sourcetype
;
404 * We only want to unmount/unshare those filesystems that may inherit
405 * from the target filesystem. If we find any filesystem with a
406 * locally set mountpoint, we ignore any children since changing the
407 * property will not affect them. If this is a rename, we iterate
408 * over all children regardless, since we need them unmounted in
409 * order to do the rename. Also, if this is a volume and we're doing
410 * a rename, then always add it to the changelist.
413 if (!(ZFS_IS_VOLUME(zhp
) && clp
->cl_realprop
== ZFS_PROP_NAME
) &&
414 zfs_prop_get(zhp
, clp
->cl_prop
, property
,
415 sizeof (property
), &sourcetype
, where
, sizeof (where
),
422 * If we are "watching" sharenfs or sharesmb
423 * then check out the companion property which is tracked
426 if (clp
->cl_shareprop
!= ZPROP_INVAL
&&
427 zfs_prop_get(zhp
, clp
->cl_shareprop
, property
,
428 sizeof (property
), &share_sourcetype
, where
, sizeof (where
),
434 if (clp
->cl_alldependents
|| clp
->cl_allchildren
||
435 sourcetype
== ZPROP_SRC_DEFAULT
||
436 sourcetype
== ZPROP_SRC_INHERITED
||
437 (clp
->cl_shareprop
!= ZPROP_INVAL
&&
438 (share_sourcetype
== ZPROP_SRC_DEFAULT
||
439 share_sourcetype
== ZPROP_SRC_INHERITED
))) {
440 if ((cn
= zfs_alloc(zfs_get_handle(zhp
),
441 sizeof (prop_changenode_t
))) == NULL
) {
447 cn
->cn_mounted
= (clp
->cl_gflags
& CL_GATHER_MOUNT_ALWAYS
) ||
448 zfs_is_mounted(zhp
, NULL
);
449 cn
->cn_shared
= zfs_is_shared(zhp
);
450 cn
->cn_zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
451 cn
->cn_needpost
= B_TRUE
;
453 /* Indicate if any child is exported to a local zone. */
454 if (getzoneid() == GLOBAL_ZONEID
&& cn
->cn_zoned
)
455 clp
->cl_haszonedchild
= B_TRUE
;
457 uu_list_node_init(cn
, &cn
->cn_listnode
, clp
->cl_pool
);
459 if (clp
->cl_sorted
) {
462 (void) uu_list_find(clp
->cl_list
, cn
, NULL
,
464 uu_list_insert(clp
->cl_list
, cn
, idx
);
467 * Add this child to beginning of the list. Children
468 * below this one in the hierarchy will get added above
469 * this one in the list. This produces a list in
470 * reverse dataset name order.
471 * This is necessary when the original mountpoint
474 ASSERT(!clp
->cl_alldependents
);
475 verify(uu_list_insert_before(clp
->cl_list
,
476 uu_list_first(clp
->cl_list
), cn
) == 0);
479 if (!clp
->cl_alldependents
)
480 return (zfs_iter_children(zhp
, change_one
, data
));
490 compare_mountpoints(const void *a
, const void *b
, void *unused
)
492 const prop_changenode_t
*ca
= a
;
493 const prop_changenode_t
*cb
= b
;
495 char mounta
[MAXPATHLEN
];
496 char mountb
[MAXPATHLEN
];
498 boolean_t hasmounta
, hasmountb
;
501 * When unsharing or unmounting filesystems, we need to do it in
502 * mountpoint order. This allows the user to have a mountpoint
503 * hierarchy that is different from the dataset hierarchy, and still
504 * allow it to be changed. However, if either dataset doesn't have a
505 * mountpoint (because it is a volume or a snapshot), we place it at the
506 * end of the list, because it doesn't affect our change at all.
508 hasmounta
= (zfs_prop_get(ca
->cn_handle
, ZFS_PROP_MOUNTPOINT
, mounta
,
509 sizeof (mounta
), NULL
, NULL
, 0, B_FALSE
) == 0);
510 hasmountb
= (zfs_prop_get(cb
->cn_handle
, ZFS_PROP_MOUNTPOINT
, mountb
,
511 sizeof (mountb
), NULL
, NULL
, 0, B_FALSE
) == 0);
513 if (!hasmounta
&& hasmountb
)
515 else if (hasmounta
&& !hasmountb
)
517 else if (!hasmounta
&& !hasmountb
)
520 return (strcmp(mountb
, mounta
));
524 * Given a ZFS handle and a property, construct a complete list of datasets
525 * that need to be modified as part of this process. For anything but the
526 * 'mountpoint' and 'sharenfs' properties, this just returns an empty list.
527 * Otherwise, we iterate over all children and look for any datasets that
528 * inherit the property. For each such dataset, we add it to the list and
529 * mark whether it was shared beforehand.
532 changelist_gather(zfs_handle_t
*zhp
, zfs_prop_t prop
, int gather_flags
,
535 prop_changelist_t
*clp
;
536 prop_changenode_t
*cn
;
538 char property
[ZFS_MAXPROPLEN
];
539 uu_compare_fn_t
*compare
= NULL
;
540 boolean_t legacy
= B_FALSE
;
542 if ((clp
= zfs_alloc(zhp
->zfs_hdl
, sizeof (prop_changelist_t
))) == NULL
)
546 * For mountpoint-related tasks, we want to sort everything by
547 * mountpoint, so that we mount and unmount them in the appropriate
548 * order, regardless of their position in the hierarchy.
550 if (prop
== ZFS_PROP_NAME
|| prop
== ZFS_PROP_ZONED
||
551 prop
== ZFS_PROP_MOUNTPOINT
|| prop
== ZFS_PROP_SHARENFS
||
552 prop
== ZFS_PROP_SHARESMB
) {
554 if (zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
,
555 property
, sizeof (property
),
556 NULL
, NULL
, 0, B_FALSE
) == 0 &&
557 (strcmp(property
, "legacy") == 0 ||
558 strcmp(property
, "none") == 0)) {
563 compare
= compare_mountpoints
;
564 clp
->cl_sorted
= B_TRUE
;
568 clp
->cl_pool
= uu_list_pool_create("changelist_pool",
569 sizeof (prop_changenode_t
),
570 offsetof(prop_changenode_t
, cn_listnode
),
572 if (clp
->cl_pool
== NULL
) {
573 assert(uu_error() == UU_ERROR_NO_MEMORY
);
574 (void) zfs_error(zhp
->zfs_hdl
, EZFS_NOMEM
, "internal error");
575 changelist_free(clp
);
579 clp
->cl_list
= uu_list_create(clp
->cl_pool
, NULL
,
580 clp
->cl_sorted
? UU_LIST_SORTED
: 0);
581 clp
->cl_gflags
= gather_flags
;
582 clp
->cl_mflags
= mnt_flags
;
584 if (clp
->cl_list
== NULL
) {
585 assert(uu_error() == UU_ERROR_NO_MEMORY
);
586 (void) zfs_error(zhp
->zfs_hdl
, EZFS_NOMEM
, "internal error");
587 changelist_free(clp
);
592 * If this is a rename or the 'zoned' property, we pretend we're
593 * changing the mountpoint and flag it so we can catch all children in
596 * Flag cl_alldependents to catch all children plus the dependents
597 * (clones) that are not in the hierarchy.
599 if (prop
== ZFS_PROP_NAME
) {
600 clp
->cl_prop
= ZFS_PROP_MOUNTPOINT
;
601 clp
->cl_alldependents
= B_TRUE
;
602 } else if (prop
== ZFS_PROP_ZONED
) {
603 clp
->cl_prop
= ZFS_PROP_MOUNTPOINT
;
604 clp
->cl_allchildren
= B_TRUE
;
605 } else if (prop
== ZFS_PROP_CANMOUNT
) {
606 clp
->cl_prop
= ZFS_PROP_MOUNTPOINT
;
607 } else if (prop
== ZFS_PROP_VOLSIZE
) {
608 clp
->cl_prop
= ZFS_PROP_MOUNTPOINT
;
612 clp
->cl_realprop
= prop
;
614 if (clp
->cl_prop
!= ZFS_PROP_MOUNTPOINT
&&
615 clp
->cl_prop
!= ZFS_PROP_SHARENFS
&&
616 clp
->cl_prop
!= ZFS_PROP_SHARESMB
)
620 * If watching SHARENFS or SHARESMB then
621 * also watch its companion property.
623 if (clp
->cl_prop
== ZFS_PROP_SHARENFS
)
624 clp
->cl_shareprop
= ZFS_PROP_SHARESMB
;
625 else if (clp
->cl_prop
== ZFS_PROP_SHARESMB
)
626 clp
->cl_shareprop
= ZFS_PROP_SHARENFS
;
628 if (clp
->cl_alldependents
) {
629 if (zfs_iter_dependents(zhp
, B_TRUE
, change_one
, clp
) != 0) {
630 changelist_free(clp
);
633 } else if (zfs_iter_children(zhp
, change_one
, clp
) != 0) {
634 changelist_free(clp
);
639 * We have to re-open ourselves because we auto-close all the handles
640 * and can't tell the difference.
642 if ((temp
= zfs_open(zhp
->zfs_hdl
, zfs_get_name(zhp
),
643 ZFS_TYPE_DATASET
)) == NULL
) {
644 changelist_free(clp
);
649 * Always add ourself to the list. We add ourselves to the end so that
650 * we're the last to be unmounted.
652 if ((cn
= zfs_alloc(zhp
->zfs_hdl
,
653 sizeof (prop_changenode_t
))) == NULL
) {
655 changelist_free(clp
);
659 cn
->cn_handle
= temp
;
660 cn
->cn_mounted
= (clp
->cl_gflags
& CL_GATHER_MOUNT_ALWAYS
) ||
661 zfs_is_mounted(temp
, NULL
);
662 cn
->cn_shared
= zfs_is_shared(temp
);
663 cn
->cn_zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
664 cn
->cn_needpost
= B_TRUE
;
666 uu_list_node_init(cn
, &cn
->cn_listnode
, clp
->cl_pool
);
667 if (clp
->cl_sorted
) {
669 (void) uu_list_find(clp
->cl_list
, cn
, NULL
, &idx
);
670 uu_list_insert(clp
->cl_list
, cn
, idx
);
673 * Add the target dataset to the end of the list.
674 * The list is not really unsorted. The list will be
675 * in reverse dataset name order. This is necessary
676 * when the original mountpoint is legacy or none.
678 verify(uu_list_insert_after(clp
->cl_list
,
679 uu_list_last(clp
->cl_list
), cn
) == 0);
683 * If the mountpoint property was previously 'legacy', or 'none',
684 * record it as the behavior of changelist_postfix() will be different.
686 if ((clp
->cl_prop
== ZFS_PROP_MOUNTPOINT
) && legacy
) {
688 * do not automatically mount ex-legacy datasets if
689 * we specifically set canmount to noauto
691 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) !=
693 clp
->cl_waslegacy
= B_TRUE
;