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 2008 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)
96 prop_changenode_t *cn;
99 if (clp->cl_prop != ZFS_PROP_MOUNTPOINT &&
100 clp->cl_prop != ZFS_PROP_SHARESMB)
103 for (cn = uu_list_first(clp->cl_list); cn != NULL;
104 cn = uu_list_next(clp->cl_list, cn)) {
106 /* if a previous loop failed, set the remaining to false */
108 cn->cn_needpost = B_FALSE;
113 * If we are in the global zone, but this dataset is exported
114 * to a local zone, do nothing.
116 if (getzoneid() == GLOBAL_ZONEID && cn->cn_zoned)
119 if (ZFS_IS_VOLUME(cn->cn_handle)) {
120 switch (clp->cl_realprop) {
123 * If this was a rename, unshare the zvol, and
124 * remove the /dev/zvol links.
126 (void) zfs_unshare_iscsi(cn->cn_handle);
128 if (zvol_remove_link(cn->cn_handle->zfs_hdl,
129 cn->cn_handle->zfs_name) != 0) {
131 cn->cn_needpost = B_FALSE;
132 (void) zfs_share_iscsi(cn->cn_handle);
136 case ZFS_PROP_VOLSIZE:
138 * If this was a change to the volume size, we
139 * need to unshare and reshare the volume.
141 (void) zfs_unshare_iscsi(cn->cn_handle);
146 * Do the property specific processing.
148 switch (clp->cl_prop) {
149 case ZFS_PROP_MOUNTPOINT:
150 if (zfs_unmount(cn->cn_handle, NULL,
151 clp->cl_mflags) != 0) {
153 cn->cn_needpost = B_FALSE;
156 case ZFS_PROP_SHARESMB:
157 (void) zfs_unshare_smb(cn->cn_handle, NULL);
164 (void) changelist_postfix(clp);
170 * If the property is 'mountpoint' or 'sharenfs', go through and remount and/or
171 * reshare the filesystems as necessary. In changelist_gather() we recorded
172 * whether the filesystem was previously shared or mounted. The action we take
173 * depends on the previous state, and whether the value was previously 'legacy'.
174 * For non-legacy properties, we only remount/reshare the filesystem if it was
175 * previously mounted/shared. Otherwise, we always remount/reshare the
179 changelist_postfix(prop_changelist_t *clp)
181 prop_changenode_t *cn;
182 char shareopts[ZFS_MAXPROPLEN];
184 libzfs_handle_t *hdl;
187 * If we're changing the mountpoint, attempt to destroy the underlying
188 * mountpoint. All other datasets will have inherited from this dataset
189 * (in which case their mountpoints exist in the filesystem in the new
190 * location), or have explicit mountpoints set (in which case they won't
191 * be in the changelist).
193 if ((cn = uu_list_last(clp->cl_list)) == NULL)
196 if (clp->cl_prop == ZFS_PROP_MOUNTPOINT)
197 remove_mountpoint(cn->cn_handle);
200 * It is possible that the changelist_prefix() used libshare
201 * to unshare some entries. Since libshare caches data, an
202 * attempt to reshare during postfix can fail unless libshare
203 * is uninitialized here so that it will reinitialize later.
205 if (cn->cn_handle != NULL) {
206 hdl = cn->cn_handle->zfs_hdl;
208 zfs_uninit_libshare(hdl);
212 * We walk the datasets in reverse, because we want to mount any parent
213 * datasets before mounting the children. We walk all datasets even if
216 for (cn = uu_list_last(clp->cl_list); cn != NULL;
217 cn = uu_list_prev(clp->cl_list, cn)) {
223 * If we are in the global zone, but this dataset is exported
224 * to a local zone, do nothing.
226 if (getzoneid() == GLOBAL_ZONEID && cn->cn_zoned)
229 /* Only do post-processing if it's required */
230 if (!cn->cn_needpost)
232 cn->cn_needpost = B_FALSE;
234 zfs_refresh_properties(cn->cn_handle);
236 if (ZFS_IS_VOLUME(cn->cn_handle)) {
238 * If we're doing a rename, recreate the /dev/zvol
241 if (clp->cl_realprop == ZFS_PROP_NAME &&
242 zvol_create_link(cn->cn_handle->zfs_hdl,
243 cn->cn_handle->zfs_name) != 0) {
245 } else if (cn->cn_shared ||
246 clp->cl_prop == ZFS_PROP_SHAREISCSI) {
247 if (zfs_prop_get(cn->cn_handle,
248 ZFS_PROP_SHAREISCSI, shareopts,
249 sizeof (shareopts), NULL, NULL, 0,
251 strcmp(shareopts, "off") == 0) {
253 zfs_unshare_iscsi(cn->cn_handle);
256 zfs_share_iscsi(cn->cn_handle);
264 * Remount if previously mounted or mountpoint was legacy,
265 * or sharenfs or sharesmb property is set.
267 sharenfs = ((zfs_prop_get(cn->cn_handle, ZFS_PROP_SHARENFS,
268 shareopts, sizeof (shareopts), NULL, NULL, 0,
269 B_FALSE) == 0) && (strcmp(shareopts, "off") != 0));
271 sharesmb = ((zfs_prop_get(cn->cn_handle, ZFS_PROP_SHARESMB,
272 shareopts, sizeof (shareopts), NULL, NULL, 0,
273 B_FALSE) == 0) && (strcmp(shareopts, "off") != 0));
275 if ((cn->cn_mounted || clp->cl_waslegacy || sharenfs ||
276 sharesmb) && !zfs_is_mounted(cn->cn_handle, NULL) &&
277 zfs_mount(cn->cn_handle, NULL, 0) != 0)
281 * We always re-share even if the filesystem is currently
282 * shared, so that we can adopt any new options.
285 errors += zfs_share_nfs(cn->cn_handle);
286 else if (cn->cn_shared || clp->cl_waslegacy)
287 errors += zfs_unshare_nfs(cn->cn_handle, NULL);
289 errors += zfs_share_smb(cn->cn_handle);
290 else if (cn->cn_shared || clp->cl_waslegacy)
291 errors += zfs_unshare_smb(cn->cn_handle, NULL);
294 return (errors ? -1 : 0);
298 * Is this "dataset" a child of "parent"?
301 isa_child_of(const char *dataset, const char *parent)
305 len = strlen(parent);
307 if (strncmp(dataset, parent, len) == 0 &&
308 (dataset[len] == '@' || dataset[len] == '/' ||
309 dataset[len] == '\0'))
317 * If we rename a filesystem, child filesystem handles are no longer valid
318 * since we identify each dataset by its name in the ZFS namespace. As a
319 * result, we have to go through and fix up all the names appropriately. We
320 * could do this automatically if libzfs kept track of all open handles, but
321 * this is a lot less work.
324 changelist_rename(prop_changelist_t *clp, const char *src, const char *dst)
326 prop_changenode_t *cn;
327 char newname[ZFS_MAXNAMELEN];
329 for (cn = uu_list_first(clp->cl_list); cn != NULL;
330 cn = uu_list_next(clp->cl_list, cn)) {
332 * Do not rename a clone that's not in the source hierarchy.
334 if (!isa_child_of(cn->cn_handle->zfs_name, src))
338 * Destroy the previous mountpoint if needed.
340 remove_mountpoint(cn->cn_handle);
342 (void) strlcpy(newname, dst, sizeof (newname));
343 (void) strcat(newname, cn->cn_handle->zfs_name + strlen(src));
345 (void) strlcpy(cn->cn_handle->zfs_name, newname,
346 sizeof (cn->cn_handle->zfs_name));
351 * Given a gathered changelist for the 'sharenfs' or 'sharesmb' property,
352 * unshare all the datasets in the list.
355 changelist_unshare(prop_changelist_t *clp, zfs_share_proto_t *proto)
357 prop_changenode_t *cn;
360 if (clp->cl_prop != ZFS_PROP_SHARENFS &&
361 clp->cl_prop != ZFS_PROP_SHARESMB)
364 for (cn = uu_list_first(clp->cl_list); cn != NULL;
365 cn = uu_list_next(clp->cl_list, cn)) {
366 if (zfs_unshare_proto(cn->cn_handle, NULL, proto) != 0)
374 * Check if there is any child exported to a local zone in a given changelist.
375 * This information has already been recorded while gathering the changelist
376 * via changelist_gather().
379 changelist_haszonedchild(prop_changelist_t *clp)
381 return (clp->cl_haszonedchild);
385 * Remove a node from a gathered list.
388 changelist_remove(prop_changelist_t *clp, const char *name)
390 prop_changenode_t *cn;
392 for (cn = uu_list_first(clp->cl_list); cn != NULL;
393 cn = uu_list_next(clp->cl_list, cn)) {
395 if (strcmp(cn->cn_handle->zfs_name, name) == 0) {
396 uu_list_remove(clp->cl_list, cn);
397 zfs_close(cn->cn_handle);
405 * Release any memory associated with a changelist.
408 changelist_free(prop_changelist_t *clp)
410 prop_changenode_t *cn;
415 while ((cn = uu_list_teardown(clp->cl_list, &cookie)) != NULL) {
416 zfs_close(cn->cn_handle);
420 uu_list_destroy(clp->cl_list);
423 uu_list_pool_destroy(clp->cl_pool);
429 change_one(zfs_handle_t *zhp, void *data)
431 prop_changelist_t *clp = data;
432 char property[ZFS_MAXPROPLEN];
434 prop_changenode_t *cn;
435 zprop_source_t sourcetype;
436 zprop_source_t share_sourcetype;
439 * We only want to unmount/unshare those filesystems that may inherit
440 * from the target filesystem. If we find any filesystem with a
441 * locally set mountpoint, we ignore any children since changing the
442 * property will not affect them. If this is a rename, we iterate
443 * over all children regardless, since we need them unmounted in
444 * order to do the rename. Also, if this is a volume and we're doing
445 * a rename, then always add it to the changelist.
448 if (!(ZFS_IS_VOLUME(zhp) && clp->cl_realprop == ZFS_PROP_NAME) &&
449 zfs_prop_get(zhp, clp->cl_prop, property,
450 sizeof (property), &sourcetype, where, sizeof (where),
457 * If we are "watching" sharenfs or sharesmb
458 * then check out the companion property which is tracked
461 if (clp->cl_shareprop != ZPROP_INVAL &&
462 zfs_prop_get(zhp, clp->cl_shareprop, property,
463 sizeof (property), &share_sourcetype, where, sizeof (where),
469 if (clp->cl_alldependents || clp->cl_allchildren ||
470 sourcetype == ZPROP_SRC_DEFAULT ||
471 sourcetype == ZPROP_SRC_INHERITED ||
472 (clp->cl_shareprop != ZPROP_INVAL &&
473 (share_sourcetype == ZPROP_SRC_DEFAULT ||
474 share_sourcetype == ZPROP_SRC_INHERITED))) {
475 if ((cn = zfs_alloc(zfs_get_handle(zhp),
476 sizeof (prop_changenode_t))) == NULL) {
482 cn->cn_mounted = (clp->cl_gflags & CL_GATHER_MOUNT_ALWAYS) ||
483 zfs_is_mounted(zhp, NULL);
484 cn->cn_shared = zfs_is_shared(zhp);
485 cn->cn_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
486 cn->cn_needpost = B_TRUE;
488 /* Indicate if any child is exported to a local zone. */
489 if (getzoneid() == GLOBAL_ZONEID && cn->cn_zoned)
490 clp->cl_haszonedchild = B_TRUE;
492 uu_list_node_init(cn, &cn->cn_listnode, clp->cl_pool);
494 if (clp->cl_sorted) {
497 (void) uu_list_find(clp->cl_list, cn, NULL,
499 uu_list_insert(clp->cl_list, cn, idx);
501 ASSERT(!clp->cl_alldependents);
502 verify(uu_list_insert_before(clp->cl_list,
503 uu_list_first(clp->cl_list), cn) == 0);
506 if (!clp->cl_alldependents)
507 return (zfs_iter_children(zhp, change_one, data));
517 compare_mountpoints(const void *a, const void *b, void *unused)
519 const prop_changenode_t *ca = a;
520 const prop_changenode_t *cb = b;
522 char mounta[MAXPATHLEN];
523 char mountb[MAXPATHLEN];
525 boolean_t hasmounta, hasmountb;
528 * When unsharing or unmounting filesystems, we need to do it in
529 * mountpoint order. This allows the user to have a mountpoint
530 * hierarchy that is different from the dataset hierarchy, and still
531 * allow it to be changed. However, if either dataset doesn't have a
532 * mountpoint (because it is a volume or a snapshot), we place it at the
533 * end of the list, because it doesn't affect our change at all.
535 hasmounta = (zfs_prop_get(ca->cn_handle, ZFS_PROP_MOUNTPOINT, mounta,
536 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
537 hasmountb = (zfs_prop_get(cb->cn_handle, ZFS_PROP_MOUNTPOINT, mountb,
538 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
540 if (!hasmounta && hasmountb)
542 else if (hasmounta && !hasmountb)
544 else if (!hasmounta && !hasmountb)
547 return (strcmp(mountb, mounta));
551 * Given a ZFS handle and a property, construct a complete list of datasets
552 * that need to be modified as part of this process. For anything but the
553 * 'mountpoint' and 'sharenfs' properties, this just returns an empty list.
554 * Otherwise, we iterate over all children and look for any datasets that
555 * inherit the property. For each such dataset, we add it to the list and
556 * mark whether it was shared beforehand.
559 changelist_gather(zfs_handle_t *zhp, zfs_prop_t prop, int gather_flags,
562 prop_changelist_t *clp;
563 prop_changenode_t *cn;
565 char property[ZFS_MAXPROPLEN];
566 uu_compare_fn_t *compare = NULL;
568 if ((clp = zfs_alloc(zhp->zfs_hdl, sizeof (prop_changelist_t))) == NULL)
572 * For mountpoint-related tasks, we want to sort everything by
573 * mountpoint, so that we mount and unmount them in the appropriate
574 * order, regardless of their position in the hierarchy.
576 if (prop == ZFS_PROP_NAME || prop == ZFS_PROP_ZONED ||
577 prop == ZFS_PROP_MOUNTPOINT || prop == ZFS_PROP_SHARENFS ||
578 prop == ZFS_PROP_SHARESMB) {
579 compare = compare_mountpoints;
580 clp->cl_sorted = B_TRUE;
583 clp->cl_pool = uu_list_pool_create("changelist_pool",
584 sizeof (prop_changenode_t),
585 offsetof(prop_changenode_t, cn_listnode),
587 if (clp->cl_pool == NULL) {
588 assert(uu_error() == UU_ERROR_NO_MEMORY);
589 (void) zfs_error(zhp->zfs_hdl, EZFS_NOMEM, "internal error");
590 changelist_free(clp);
594 clp->cl_list = uu_list_create(clp->cl_pool, NULL,
595 clp->cl_sorted ? UU_LIST_SORTED : 0);
596 clp->cl_gflags = gather_flags;
597 clp->cl_mflags = mnt_flags;
599 if (clp->cl_list == NULL) {
600 assert(uu_error() == UU_ERROR_NO_MEMORY);
601 (void) zfs_error(zhp->zfs_hdl, EZFS_NOMEM, "internal error");
602 changelist_free(clp);
607 * If this is a rename or the 'zoned' property, we pretend we're
608 * changing the mountpoint and flag it so we can catch all children in
611 * Flag cl_alldependents to catch all children plus the dependents
612 * (clones) that are not in the hierarchy.
614 if (prop == ZFS_PROP_NAME) {
615 clp->cl_prop = ZFS_PROP_MOUNTPOINT;
616 clp->cl_alldependents = B_TRUE;
617 } else if (prop == ZFS_PROP_ZONED) {
618 clp->cl_prop = ZFS_PROP_MOUNTPOINT;
619 clp->cl_allchildren = B_TRUE;
620 } else if (prop == ZFS_PROP_CANMOUNT) {
621 clp->cl_prop = ZFS_PROP_MOUNTPOINT;
622 } else if (prop == ZFS_PROP_VOLSIZE) {
623 clp->cl_prop = ZFS_PROP_MOUNTPOINT;
624 } else if (prop == ZFS_PROP_VERSION) {
625 clp->cl_prop = ZFS_PROP_MOUNTPOINT;
629 clp->cl_realprop = prop;
631 if (clp->cl_prop != ZFS_PROP_MOUNTPOINT &&
632 clp->cl_prop != ZFS_PROP_SHARENFS &&
633 clp->cl_prop != ZFS_PROP_SHARESMB &&
634 clp->cl_prop != ZFS_PROP_SHAREISCSI)
638 * If watching SHARENFS or SHARESMB then
639 * also watch its companion property.
641 if (clp->cl_prop == ZFS_PROP_SHARENFS)
642 clp->cl_shareprop = ZFS_PROP_SHARESMB;
643 else if (clp->cl_prop == ZFS_PROP_SHARESMB)
644 clp->cl_shareprop = ZFS_PROP_SHARENFS;
646 if (clp->cl_alldependents) {
647 if (zfs_iter_dependents(zhp, B_TRUE, change_one, clp) != 0) {
648 changelist_free(clp);
651 } else if (zfs_iter_children(zhp, change_one, clp) != 0) {
652 changelist_free(clp);
657 * We have to re-open ourselves because we auto-close all the handles
658 * and can't tell the difference.
660 if ((temp = zfs_open(zhp->zfs_hdl, zfs_get_name(zhp),
661 ZFS_TYPE_DATASET)) == NULL) {
662 changelist_free(clp);
667 * Always add ourself to the list. We add ourselves to the end so that
668 * we're the last to be unmounted.
670 if ((cn = zfs_alloc(zhp->zfs_hdl,
671 sizeof (prop_changenode_t))) == NULL) {
673 changelist_free(clp);
677 cn->cn_handle = temp;
678 cn->cn_mounted = (clp->cl_gflags & CL_GATHER_MOUNT_ALWAYS) ||
679 zfs_is_mounted(temp, NULL);
680 cn->cn_shared = zfs_is_shared(temp);
681 cn->cn_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
682 cn->cn_needpost = B_TRUE;
684 uu_list_node_init(cn, &cn->cn_listnode, clp->cl_pool);
685 if (clp->cl_sorted) {
687 (void) uu_list_find(clp->cl_list, cn, NULL, &idx);
688 uu_list_insert(clp->cl_list, cn, idx);
690 verify(uu_list_insert_after(clp->cl_list,
691 uu_list_last(clp->cl_list), cn) == 0);
695 * If the mountpoint property was previously 'legacy', or 'none',
696 * record it as the behavior of changelist_postfix() will be different.
698 if ((clp->cl_prop == ZFS_PROP_MOUNTPOINT) &&
699 (zfs_prop_get(zhp, prop, property, sizeof (property),
700 NULL, NULL, 0, B_FALSE) == 0 &&
701 (strcmp(property, "legacy") == 0 ||
702 strcmp(property, "none") == 0))) {
704 * do not automatically mount ex-legacy datasets if
705 * we specifically set canmount to noauto
707 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) !=
709 clp->cl_waslegacy = B_TRUE;