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
39 #include "libzfs_impl.h"
42 * Structure to keep track of dataset state. Before changing the 'sharenfs' or
43 * 'mountpoint' property, we record whether the filesystem was previously
44 * mounted/shared. This prior state dictates whether we remount/reshare the
45 * dataset after the property has been changed.
47 * The interface consists of the following sequence of functions:
52 * changelist_postfix()
57 * changelist_remove() - remove a node from a gathered list
58 * changelist_rename() - renames all datasets appropriately when doing a rename
59 * changelist_unshare() - unshares all the nodes in a given changelist
60 * changelist_haszonedchild() - check if there is any child exported to
63 typedef struct prop_changenode {
64 zfs_handle_t *cn_handle;
68 boolean_t cn_needpost; /* is postfix() needed? */
69 uu_list_node_t cn_listnode;
72 struct prop_changelist {
74 zfs_prop_t cl_realprop;
75 zfs_prop_t cl_shareprop; /* used with sharenfs/sharesmb */
76 uu_list_pool_t *cl_pool;
78 boolean_t cl_waslegacy;
79 boolean_t cl_allchildren;
80 boolean_t cl_alldependents;
81 int cl_mflags; /* Mount flags */
82 int cl_gflags; /* Gather request flags */
83 boolean_t cl_haszonedchild;
88 * If the property is 'mountpoint', go through and unmount filesystems as
89 * necessary. We don't do the same for 'sharenfs', because we can just re-share
90 * with different options without interrupting service. We do handle 'sharesmb'
91 * since there may be old resource names that need to be removed.
94 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);
142 (void) changelist_postfix(clp);
147 #endif /* HAVE_ZPL */
151 * If the property is 'mountpoint' or 'sharenfs', go through and remount and/or
152 * reshare the filesystems as necessary. In changelist_gather() we recorded
153 * whether the filesystem was previously shared or mounted. The action we take
154 * depends on the previous state, and whether the value was previously 'legacy'.
155 * For non-legacy properties, we only remount/reshare the filesystem if it was
156 * previously mounted/shared. Otherwise, we always remount/reshare the
160 changelist_postfix(prop_changelist_t *clp)
163 prop_changenode_t *cn;
164 char shareopts[ZFS_MAXPROPLEN];
166 libzfs_handle_t *hdl;
169 * If we're changing the mountpoint, attempt to destroy the underlying
170 * mountpoint. All other datasets will have inherited from this dataset
171 * (in which case their mountpoints exist in the filesystem in the new
172 * location), or have explicit mountpoints set (in which case they won't
173 * be in the changelist).
175 if ((cn = uu_list_last(clp->cl_list)) == NULL)
178 if (clp->cl_prop == ZFS_PROP_MOUNTPOINT)
179 remove_mountpoint(cn->cn_handle);
182 * It is possible that the changelist_prefix() used libshare
183 * to unshare some entries. Since libshare caches data, an
184 * attempt to reshare during postfix can fail unless libshare
185 * is uninitialized here so that it will reinitialize later.
187 if (cn->cn_handle != NULL) {
188 hdl = cn->cn_handle->zfs_hdl;
190 zfs_uninit_libshare(hdl);
194 * We walk the datasets in reverse, because we want to mount any parent
195 * datasets before mounting the children. We walk all datasets even if
198 for (cn = uu_list_last(clp->cl_list); cn != NULL;
199 cn = uu_list_prev(clp->cl_list, cn)) {
206 * If we are in the global zone, but this dataset is exported
207 * to a local zone, do nothing.
209 if (getzoneid() == GLOBAL_ZONEID && cn->cn_zoned)
212 /* Only do post-processing if it's required */
213 if (!cn->cn_needpost)
215 cn->cn_needpost = B_FALSE;
217 zfs_refresh_properties(cn->cn_handle);
219 if (ZFS_IS_VOLUME(cn->cn_handle))
223 * Remount if previously mounted or mountpoint was legacy,
224 * or sharenfs or sharesmb property is set.
226 sharenfs = ((zfs_prop_get(cn->cn_handle, ZFS_PROP_SHARENFS,
227 shareopts, sizeof (shareopts), NULL, NULL, 0,
228 B_FALSE) == 0) && (strcmp(shareopts, "off") != 0));
230 sharesmb = ((zfs_prop_get(cn->cn_handle, ZFS_PROP_SHARESMB,
231 shareopts, sizeof (shareopts), NULL, NULL, 0,
232 B_FALSE) == 0) && (strcmp(shareopts, "off") != 0));
234 mounted = zfs_is_mounted(cn->cn_handle, NULL);
236 if (!mounted && (cn->cn_mounted ||
237 ((sharenfs || sharesmb || clp->cl_waslegacy) &&
238 (zfs_prop_get_int(cn->cn_handle,
239 ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_ON)))) {
241 if (zfs_mount(cn->cn_handle, NULL, 0) != 0)
248 * If the file system is mounted we always re-share even
249 * if the filesystem is currently shared, so that we can
250 * adopt any new options.
252 if (sharenfs && mounted)
253 errors += zfs_share_nfs(cn->cn_handle);
254 else if (cn->cn_shared || clp->cl_waslegacy)
255 errors += zfs_unshare_nfs(cn->cn_handle, NULL);
256 if (sharesmb && mounted)
257 errors += zfs_share_smb(cn->cn_handle);
258 else if (cn->cn_shared || clp->cl_waslegacy)
259 errors += zfs_unshare_smb(cn->cn_handle, NULL);
262 return (errors ? -1 : 0);
265 #endif /* HAVE_ZPL */
269 * Is this "dataset" a child of "parent"?
272 isa_child_of(const char *dataset, const char *parent)
276 len = strlen(parent);
278 if (strncmp(dataset, parent, len) == 0 &&
279 (dataset[len] == '@' || dataset[len] == '/' ||
280 dataset[len] == '\0'))
288 * If we rename a filesystem, child filesystem handles are no longer valid
289 * since we identify each dataset by its name in the ZFS namespace. As a
290 * result, we have to go through and fix up all the names appropriately. We
291 * could do this automatically if libzfs kept track of all open handles, but
292 * this is a lot less work.
295 changelist_rename(prop_changelist_t *clp, const char *src, const char *dst)
297 prop_changenode_t *cn;
298 char newname[ZFS_MAXNAMELEN];
300 for (cn = uu_list_first(clp->cl_list); cn != NULL;
301 cn = uu_list_next(clp->cl_list, cn)) {
303 * Do not rename a clone that's not in the source hierarchy.
305 if (!isa_child_of(cn->cn_handle->zfs_name, src))
309 * Destroy the previous mountpoint if needed.
311 remove_mountpoint(cn->cn_handle);
313 (void) strlcpy(newname, dst, sizeof (newname));
314 (void) strcat(newname, cn->cn_handle->zfs_name + strlen(src));
316 (void) strlcpy(cn->cn_handle->zfs_name, newname,
317 sizeof (cn->cn_handle->zfs_name));
322 * Given a gathered changelist for the 'sharenfs' or 'sharesmb' property,
323 * unshare all the datasets in the list.
326 changelist_unshare(prop_changelist_t *clp, zfs_share_proto_t *proto)
329 prop_changenode_t *cn;
332 if (clp->cl_prop != ZFS_PROP_SHARENFS &&
333 clp->cl_prop != ZFS_PROP_SHARESMB)
336 for (cn = uu_list_first(clp->cl_list); cn != NULL;
337 cn = uu_list_next(clp->cl_list, cn)) {
338 if (zfs_unshare_proto(cn->cn_handle, NULL, proto) != 0)
349 * Check if there is any child exported to a local zone in a given changelist.
350 * This information has already been recorded while gathering the changelist
351 * via changelist_gather().
354 changelist_haszonedchild(prop_changelist_t *clp)
356 return (clp->cl_haszonedchild);
360 * Remove a node from a gathered list.
363 changelist_remove(prop_changelist_t *clp, const char *name)
365 prop_changenode_t *cn;
367 for (cn = uu_list_first(clp->cl_list); cn != NULL;
368 cn = uu_list_next(clp->cl_list, cn)) {
370 if (strcmp(cn->cn_handle->zfs_name, name) == 0) {
371 uu_list_remove(clp->cl_list, cn);
372 zfs_close(cn->cn_handle);
380 * Release any memory associated with a changelist.
383 changelist_free(prop_changelist_t *clp)
385 prop_changenode_t *cn;
390 while ((cn = uu_list_teardown(clp->cl_list, &cookie)) != NULL) {
391 zfs_close(cn->cn_handle);
395 uu_list_destroy(clp->cl_list);
398 uu_list_pool_destroy(clp->cl_pool);
404 change_one(zfs_handle_t *zhp, void *data)
406 prop_changelist_t *clp = data;
407 char property[ZFS_MAXPROPLEN];
409 prop_changenode_t *cn;
410 zprop_source_t sourcetype;
411 zprop_source_t share_sourcetype;
414 * We only want to unmount/unshare those filesystems that may inherit
415 * from the target filesystem. If we find any filesystem with a
416 * locally set mountpoint, we ignore any children since changing the
417 * property will not affect them. If this is a rename, we iterate
418 * over all children regardless, since we need them unmounted in
419 * order to do the rename. Also, if this is a volume and we're doing
420 * a rename, then always add it to the changelist.
423 if (!(ZFS_IS_VOLUME(zhp) && clp->cl_realprop == ZFS_PROP_NAME) &&
424 zfs_prop_get(zhp, clp->cl_prop, property,
425 sizeof (property), &sourcetype, where, sizeof (where),
432 * If we are "watching" sharenfs or sharesmb
433 * then check out the companion property which is tracked
436 if (clp->cl_shareprop != ZPROP_INVAL &&
437 zfs_prop_get(zhp, clp->cl_shareprop, property,
438 sizeof (property), &share_sourcetype, where, sizeof (where),
444 if (clp->cl_alldependents || clp->cl_allchildren ||
445 sourcetype == ZPROP_SRC_DEFAULT ||
446 sourcetype == ZPROP_SRC_INHERITED ||
447 (clp->cl_shareprop != ZPROP_INVAL &&
448 (share_sourcetype == ZPROP_SRC_DEFAULT ||
449 share_sourcetype == ZPROP_SRC_INHERITED))) {
450 if ((cn = zfs_alloc(zfs_get_handle(zhp),
451 sizeof (prop_changenode_t))) == NULL) {
457 cn->cn_mounted = (clp->cl_gflags & CL_GATHER_MOUNT_ALWAYS) ||
458 zfs_is_mounted(zhp, NULL);
459 cn->cn_shared = zfs_is_shared(zhp);
460 cn->cn_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
461 cn->cn_needpost = B_TRUE;
463 /* Indicate if any child is exported to a local zone. */
464 if (getzoneid() == GLOBAL_ZONEID && cn->cn_zoned)
465 clp->cl_haszonedchild = B_TRUE;
467 uu_list_node_init(cn, &cn->cn_listnode, clp->cl_pool);
469 if (clp->cl_sorted) {
472 (void) uu_list_find(clp->cl_list, cn, NULL,
474 uu_list_insert(clp->cl_list, cn, idx);
477 * Add this child to beginning of the list. Children
478 * below this one in the hierarchy will get added above
479 * this one in the list. This produces a list in
480 * reverse dataset name order.
481 * This is necessary when the original mountpoint
484 ASSERT(!clp->cl_alldependents);
485 verify(uu_list_insert_before(clp->cl_list,
486 uu_list_first(clp->cl_list), cn) == 0);
489 if (!clp->cl_alldependents)
490 return (zfs_iter_children(zhp, change_one, data));
500 compare_mountpoints(const void *a, const void *b, void *unused)
502 const prop_changenode_t *ca = a;
503 const prop_changenode_t *cb = b;
505 char mounta[MAXPATHLEN];
506 char mountb[MAXPATHLEN];
508 boolean_t hasmounta, hasmountb;
511 * When unsharing or unmounting filesystems, we need to do it in
512 * mountpoint order. This allows the user to have a mountpoint
513 * hierarchy that is different from the dataset hierarchy, and still
514 * allow it to be changed. However, if either dataset doesn't have a
515 * mountpoint (because it is a volume or a snapshot), we place it at the
516 * end of the list, because it doesn't affect our change at all.
518 hasmounta = (zfs_prop_get(ca->cn_handle, ZFS_PROP_MOUNTPOINT, mounta,
519 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
520 hasmountb = (zfs_prop_get(cb->cn_handle, ZFS_PROP_MOUNTPOINT, mountb,
521 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
523 if (!hasmounta && hasmountb)
525 else if (hasmounta && !hasmountb)
527 else if (!hasmounta && !hasmountb)
530 return (strcmp(mountb, mounta));
534 * Given a ZFS handle and a property, construct a complete list of datasets
535 * that need to be modified as part of this process. For anything but the
536 * 'mountpoint' and 'sharenfs' properties, this just returns an empty list.
537 * Otherwise, we iterate over all children and look for any datasets that
538 * inherit the property. For each such dataset, we add it to the list and
539 * mark whether it was shared beforehand.
542 changelist_gather(zfs_handle_t *zhp, zfs_prop_t prop, int gather_flags,
545 prop_changelist_t *clp;
546 prop_changenode_t *cn;
548 char property[ZFS_MAXPROPLEN];
549 uu_compare_fn_t *compare = NULL;
550 boolean_t legacy = B_FALSE;
552 if ((clp = zfs_alloc(zhp->zfs_hdl, sizeof (prop_changelist_t))) == NULL)
556 * For mountpoint-related tasks, we want to sort everything by
557 * mountpoint, so that we mount and unmount them in the appropriate
558 * order, regardless of their position in the hierarchy.
560 if (prop == ZFS_PROP_NAME || prop == ZFS_PROP_ZONED ||
561 prop == ZFS_PROP_MOUNTPOINT || prop == ZFS_PROP_SHARENFS ||
562 prop == ZFS_PROP_SHARESMB) {
564 if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
565 property, sizeof (property),
566 NULL, NULL, 0, B_FALSE) == 0 &&
567 (strcmp(property, "legacy") == 0 ||
568 strcmp(property, "none") == 0)) {
573 compare = compare_mountpoints;
574 clp->cl_sorted = B_TRUE;
578 clp->cl_pool = uu_list_pool_create("changelist_pool",
579 sizeof (prop_changenode_t),
580 offsetof(prop_changenode_t, cn_listnode),
582 if (clp->cl_pool == NULL) {
583 assert(uu_error() == UU_ERROR_NO_MEMORY);
584 (void) zfs_error(zhp->zfs_hdl, EZFS_NOMEM, "internal error");
585 changelist_free(clp);
589 clp->cl_list = uu_list_create(clp->cl_pool, NULL,
590 clp->cl_sorted ? UU_LIST_SORTED : 0);
591 clp->cl_gflags = gather_flags;
592 clp->cl_mflags = mnt_flags;
594 if (clp->cl_list == NULL) {
595 assert(uu_error() == UU_ERROR_NO_MEMORY);
596 (void) zfs_error(zhp->zfs_hdl, EZFS_NOMEM, "internal error");
597 changelist_free(clp);
602 * If this is a rename or the 'zoned' property, we pretend we're
603 * changing the mountpoint and flag it so we can catch all children in
606 * Flag cl_alldependents to catch all children plus the dependents
607 * (clones) that are not in the hierarchy.
609 if (prop == ZFS_PROP_NAME) {
610 clp->cl_prop = ZFS_PROP_MOUNTPOINT;
611 clp->cl_alldependents = B_TRUE;
612 } else if (prop == ZFS_PROP_ZONED) {
613 clp->cl_prop = ZFS_PROP_MOUNTPOINT;
614 clp->cl_allchildren = B_TRUE;
615 } else if (prop == ZFS_PROP_CANMOUNT) {
616 clp->cl_prop = ZFS_PROP_MOUNTPOINT;
617 } else if (prop == ZFS_PROP_VOLSIZE) {
618 clp->cl_prop = ZFS_PROP_MOUNTPOINT;
622 clp->cl_realprop = prop;
624 if (clp->cl_prop != ZFS_PROP_MOUNTPOINT &&
625 clp->cl_prop != ZFS_PROP_SHARENFS &&
626 clp->cl_prop != ZFS_PROP_SHARESMB)
630 * If watching SHARENFS or SHARESMB then
631 * also watch its companion property.
633 if (clp->cl_prop == ZFS_PROP_SHARENFS)
634 clp->cl_shareprop = ZFS_PROP_SHARESMB;
635 else if (clp->cl_prop == ZFS_PROP_SHARESMB)
636 clp->cl_shareprop = ZFS_PROP_SHARENFS;
638 if (clp->cl_alldependents) {
639 if (zfs_iter_dependents(zhp, B_TRUE, change_one, clp) != 0) {
640 changelist_free(clp);
643 } else if (zfs_iter_children(zhp, change_one, clp) != 0) {
644 changelist_free(clp);
649 * We have to re-open ourselves because we auto-close all the handles
650 * and can't tell the difference.
652 if ((temp = zfs_open(zhp->zfs_hdl, zfs_get_name(zhp),
653 ZFS_TYPE_DATASET)) == NULL) {
654 changelist_free(clp);
659 * Always add ourself to the list. We add ourselves to the end so that
660 * we're the last to be unmounted.
662 if ((cn = zfs_alloc(zhp->zfs_hdl,
663 sizeof (prop_changenode_t))) == NULL) {
665 changelist_free(clp);
669 cn->cn_handle = temp;
670 cn->cn_mounted = (clp->cl_gflags & CL_GATHER_MOUNT_ALWAYS) ||
671 zfs_is_mounted(temp, NULL);
672 cn->cn_shared = zfs_is_shared(temp);
673 cn->cn_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
674 cn->cn_needpost = B_TRUE;
676 uu_list_node_init(cn, &cn->cn_listnode, clp->cl_pool);
677 if (clp->cl_sorted) {
679 (void) uu_list_find(clp->cl_list, cn, NULL, &idx);
680 uu_list_insert(clp->cl_list, cn, idx);
683 * Add the target dataset to the end of the list.
684 * The list is not really unsorted. The list will be
685 * in reverse dataset name order. This is necessary
686 * when the original mountpoint is legacy or none.
688 verify(uu_list_insert_after(clp->cl_list,
689 uu_list_last(clp->cl_list), cn) == 0);
693 * If the mountpoint property was previously 'legacy', or 'none',
694 * record it as the behavior of changelist_postfix() will be different.
696 if ((clp->cl_prop == ZFS_PROP_MOUNTPOINT) && legacy) {
698 * do not automatically mount ex-legacy datasets if
699 * we specifically set canmount to noauto
701 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) !=
703 clp->cl_waslegacy = B_TRUE;