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
29 #pragma ident "@(#)libzfs_changelist.c 1.23 08/03/04 SMI"
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;
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)) {
121 switch (clp->cl_realprop) {
124 * If this was a rename, unshare the zvol, and
125 * remove the /dev/zvol links.
127 (void) zfs_unshare_iscsi(cn->cn_handle);
129 if (zvol_remove_link(cn->cn_handle->zfs_hdl,
130 cn->cn_handle->zfs_name) != 0) {
132 cn->cn_needpost = B_FALSE;
133 (void) zfs_share_iscsi(cn->cn_handle);
137 case ZFS_PROP_VOLSIZE:
139 * If this was a change to the volume size, we
140 * need to unshare and reshare the volume.
142 (void) zfs_unshare_iscsi(cn->cn_handle);
147 * Do the property specific processing.
149 switch (clp->cl_prop) {
150 case ZFS_PROP_MOUNTPOINT:
151 if (zfs_unmount(cn->cn_handle, NULL,
152 clp->cl_flags) != 0) {
154 cn->cn_needpost = B_FALSE;
157 case ZFS_PROP_SHARESMB:
158 (void) zfs_unshare_smb(cn->cn_handle, NULL);
165 (void) changelist_postfix(clp);
171 * If the property is 'mountpoint' or 'sharenfs', go through and remount and/or
172 * reshare the filesystems as necessary. In changelist_gather() we recorded
173 * whether the filesystem was previously shared or mounted. The action we take
174 * depends on the previous state, and whether the value was previously 'legacy'.
175 * For non-legacy properties, we only remount/reshare the filesystem if it was
176 * previously mounted/shared. Otherwise, we always remount/reshare the
180 changelist_postfix(prop_changelist_t *clp)
182 prop_changenode_t *cn;
183 char shareopts[ZFS_MAXPROPLEN];
185 libzfs_handle_t *hdl;
188 * If we're changing the mountpoint, attempt to destroy the underlying
189 * mountpoint. All other datasets will have inherited from this dataset
190 * (in which case their mountpoints exist in the filesystem in the new
191 * location), or have explicit mountpoints set (in which case they won't
192 * be in the changelist).
194 if ((cn = uu_list_last(clp->cl_list)) == NULL)
197 if (clp->cl_prop == ZFS_PROP_MOUNTPOINT)
198 remove_mountpoint(cn->cn_handle);
201 * It is possible that the changelist_prefix() used libshare
202 * to unshare some entries. Since libshare caches data, an
203 * attempt to reshare during postfix can fail unless libshare
204 * is uninitialized here so that it will reinitialize later.
206 if (cn->cn_handle != NULL) {
207 hdl = cn->cn_handle->zfs_hdl;
209 zfs_uninit_libshare(hdl);
213 * We walk the datasets in reverse, because we want to mount any parent
214 * datasets before mounting the children. We walk all datasets even if
217 for (cn = uu_list_last(clp->cl_list); cn != NULL;
218 cn = uu_list_prev(clp->cl_list, cn)) {
224 * If we are in the global zone, but this dataset is exported
225 * to a local zone, do nothing.
227 if (getzoneid() == GLOBAL_ZONEID && cn->cn_zoned)
230 /* Only do post-processing if it's required */
231 if (!cn->cn_needpost)
233 cn->cn_needpost = B_FALSE;
235 zfs_refresh_properties(cn->cn_handle);
237 if (ZFS_IS_VOLUME(cn->cn_handle)) {
239 * If we're doing a rename, recreate the /dev/zvol
242 if (clp->cl_realprop == ZFS_PROP_NAME &&
243 zvol_create_link(cn->cn_handle->zfs_hdl,
244 cn->cn_handle->zfs_name) != 0) {
246 } else if (cn->cn_shared ||
247 clp->cl_prop == ZFS_PROP_SHAREISCSI) {
248 if (zfs_prop_get(cn->cn_handle,
249 ZFS_PROP_SHAREISCSI, shareopts,
250 sizeof (shareopts), NULL, NULL, 0,
252 strcmp(shareopts, "off") == 0) {
254 zfs_unshare_iscsi(cn->cn_handle);
257 zfs_share_iscsi(cn->cn_handle);
265 * Remount if previously mounted or mountpoint was legacy,
266 * or sharenfs or sharesmb property is set.
268 sharenfs = ((zfs_prop_get(cn->cn_handle, ZFS_PROP_SHARENFS,
269 shareopts, sizeof (shareopts), NULL, NULL, 0,
270 B_FALSE) == 0) && (strcmp(shareopts, "off") != 0));
272 sharesmb = ((zfs_prop_get(cn->cn_handle, ZFS_PROP_SHARESMB,
273 shareopts, sizeof (shareopts), NULL, NULL, 0,
274 B_FALSE) == 0) && (strcmp(shareopts, "off") != 0));
276 if ((cn->cn_mounted || clp->cl_waslegacy || sharenfs ||
277 sharesmb) && !zfs_is_mounted(cn->cn_handle, NULL) &&
278 zfs_mount(cn->cn_handle, NULL, 0) != 0)
282 * We always re-share even if the filesystem is currently
283 * shared, so that we can adopt any new options.
286 errors += zfs_share_nfs(cn->cn_handle);
287 else if (cn->cn_shared || clp->cl_waslegacy)
288 errors += zfs_unshare_nfs(cn->cn_handle, NULL);
290 errors += zfs_share_smb(cn->cn_handle);
291 else if (cn->cn_shared || clp->cl_waslegacy)
292 errors += zfs_unshare_smb(cn->cn_handle, NULL);
295 return (errors ? -1 : 0);
299 * Is this "dataset" a child of "parent"?
302 isa_child_of(const char *dataset, const char *parent)
306 len = strlen(parent);
308 if (strncmp(dataset, parent, len) == 0 &&
309 (dataset[len] == '@' || dataset[len] == '/' ||
310 dataset[len] == '\0'))
318 * If we rename a filesystem, child filesystem handles are no longer valid
319 * since we identify each dataset by its name in the ZFS namespace. As a
320 * result, we have to go through and fix up all the names appropriately. We
321 * could do this automatically if libzfs kept track of all open handles, but
322 * this is a lot less work.
325 changelist_rename(prop_changelist_t *clp, const char *src, const char *dst)
327 prop_changenode_t *cn;
328 char newname[ZFS_MAXNAMELEN];
330 for (cn = uu_list_first(clp->cl_list); cn != NULL;
331 cn = uu_list_next(clp->cl_list, cn)) {
333 * Do not rename a clone that's not in the source hierarchy.
335 if (!isa_child_of(cn->cn_handle->zfs_name, src))
339 * Destroy the previous mountpoint if needed.
341 remove_mountpoint(cn->cn_handle);
343 (void) strlcpy(newname, dst, sizeof (newname));
344 (void) strcat(newname, cn->cn_handle->zfs_name + strlen(src));
346 (void) strlcpy(cn->cn_handle->zfs_name, newname,
347 sizeof (cn->cn_handle->zfs_name));
352 * Given a gathered changelist for the 'sharenfs' or 'sharesmb' property,
353 * unshare all the datasets in the list.
356 changelist_unshare(prop_changelist_t *clp, zfs_share_proto_t *proto)
358 prop_changenode_t *cn;
361 if (clp->cl_prop != ZFS_PROP_SHARENFS &&
362 clp->cl_prop != ZFS_PROP_SHARESMB)
365 for (cn = uu_list_first(clp->cl_list); cn != NULL;
366 cn = uu_list_next(clp->cl_list, cn)) {
367 if (zfs_unshare_proto(cn->cn_handle, NULL, proto) != 0)
375 * Check if there is any child exported to a local zone in a given changelist.
376 * This information has already been recorded while gathering the changelist
377 * via changelist_gather().
380 changelist_haszonedchild(prop_changelist_t *clp)
382 return (clp->cl_haszonedchild);
386 * Remove a node from a gathered list.
389 changelist_remove(prop_changelist_t *clp, const char *name)
391 prop_changenode_t *cn;
393 for (cn = uu_list_first(clp->cl_list); cn != NULL;
394 cn = uu_list_next(clp->cl_list, cn)) {
396 if (strcmp(cn->cn_handle->zfs_name, name) == 0) {
397 uu_list_remove(clp->cl_list, cn);
398 zfs_close(cn->cn_handle);
406 * Release any memory associated with a changelist.
409 changelist_free(prop_changelist_t *clp)
411 prop_changenode_t *cn;
416 while ((cn = uu_list_teardown(clp->cl_list, &cookie)) != NULL) {
417 zfs_close(cn->cn_handle);
421 uu_list_destroy(clp->cl_list);
424 uu_list_pool_destroy(clp->cl_pool);
430 change_one(zfs_handle_t *zhp, void *data)
432 prop_changelist_t *clp = data;
433 char property[ZFS_MAXPROPLEN];
435 prop_changenode_t *cn;
436 zprop_source_t sourcetype;
437 zprop_source_t share_sourcetype;
440 * We only want to unmount/unshare those filesystems that may inherit
441 * from the target filesystem. If we find any filesystem with a
442 * locally set mountpoint, we ignore any children since changing the
443 * property will not affect them. If this is a rename, we iterate
444 * over all children regardless, since we need them unmounted in
445 * order to do the rename. Also, if this is a volume and we're doing
446 * a rename, then always add it to the changelist.
449 if (!(ZFS_IS_VOLUME(zhp) && clp->cl_realprop == ZFS_PROP_NAME) &&
450 zfs_prop_get(zhp, clp->cl_prop, property,
451 sizeof (property), &sourcetype, where, sizeof (where),
458 * If we are "watching" sharenfs or sharesmb
459 * then check out the companion property which is tracked
462 if (clp->cl_shareprop != ZPROP_INVAL &&
463 zfs_prop_get(zhp, clp->cl_shareprop, property,
464 sizeof (property), &share_sourcetype, where, sizeof (where),
470 if (clp->cl_alldependents || clp->cl_allchildren ||
471 sourcetype == ZPROP_SRC_DEFAULT ||
472 sourcetype == ZPROP_SRC_INHERITED ||
473 (clp->cl_shareprop != ZPROP_INVAL &&
474 (share_sourcetype == ZPROP_SRC_DEFAULT ||
475 share_sourcetype == ZPROP_SRC_INHERITED))) {
476 if ((cn = zfs_alloc(zfs_get_handle(zhp),
477 sizeof (prop_changenode_t))) == NULL) {
483 cn->cn_mounted = 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 flags)
561 prop_changelist_t *clp;
562 prop_changenode_t *cn;
564 char property[ZFS_MAXPROPLEN];
565 uu_compare_fn_t *compare = NULL;
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) {
578 compare = compare_mountpoints;
579 clp->cl_sorted = B_TRUE;
582 clp->cl_pool = uu_list_pool_create("changelist_pool",
583 sizeof (prop_changenode_t),
584 offsetof(prop_changenode_t, cn_listnode),
586 if (clp->cl_pool == NULL) {
587 assert(uu_error() == UU_ERROR_NO_MEMORY);
588 (void) zfs_error(zhp->zfs_hdl, EZFS_NOMEM, "internal error");
589 changelist_free(clp);
593 clp->cl_list = uu_list_create(clp->cl_pool, NULL,
594 clp->cl_sorted ? UU_LIST_SORTED : 0);
595 clp->cl_flags = flags;
597 if (clp->cl_list == NULL) {
598 assert(uu_error() == UU_ERROR_NO_MEMORY);
599 (void) zfs_error(zhp->zfs_hdl, EZFS_NOMEM, "internal error");
600 changelist_free(clp);
605 * If this is a rename or the 'zoned' property, we pretend we're
606 * changing the mountpoint and flag it so we can catch all children in
609 * Flag cl_alldependents to catch all children plus the dependents
610 * (clones) that are not in the hierarchy.
612 if (prop == ZFS_PROP_NAME) {
613 clp->cl_prop = ZFS_PROP_MOUNTPOINT;
614 clp->cl_alldependents = B_TRUE;
615 } else if (prop == ZFS_PROP_ZONED) {
616 clp->cl_prop = ZFS_PROP_MOUNTPOINT;
617 clp->cl_allchildren = B_TRUE;
618 } else if (prop == ZFS_PROP_CANMOUNT) {
619 clp->cl_prop = ZFS_PROP_MOUNTPOINT;
620 } else if (prop == ZFS_PROP_VOLSIZE) {
621 clp->cl_prop = ZFS_PROP_MOUNTPOINT;
622 } else if (prop == ZFS_PROP_VERSION) {
623 clp->cl_prop = ZFS_PROP_MOUNTPOINT;
627 clp->cl_realprop = prop;
629 if (clp->cl_prop != ZFS_PROP_MOUNTPOINT &&
630 clp->cl_prop != ZFS_PROP_SHARENFS &&
631 clp->cl_prop != ZFS_PROP_SHARESMB &&
632 clp->cl_prop != ZFS_PROP_SHAREISCSI)
636 * If watching SHARENFS or SHARESMB then
637 * also watch its companion property.
639 if (clp->cl_prop == ZFS_PROP_SHARENFS)
640 clp->cl_shareprop = ZFS_PROP_SHARESMB;
641 else if (clp->cl_prop == ZFS_PROP_SHARESMB)
642 clp->cl_shareprop = ZFS_PROP_SHARENFS;
644 if (clp->cl_alldependents) {
645 if (zfs_iter_dependents(zhp, B_TRUE, change_one, clp) != 0) {
646 changelist_free(clp);
649 } else if (zfs_iter_children(zhp, change_one, clp) != 0) {
650 changelist_free(clp);
655 * We have to re-open ourselves because we auto-close all the handles
656 * and can't tell the difference.
658 if ((temp = zfs_open(zhp->zfs_hdl, zfs_get_name(zhp),
659 ZFS_TYPE_DATASET)) == NULL) {
660 changelist_free(clp);
665 * Always add ourself to the list. We add ourselves to the end so that
666 * we're the last to be unmounted.
668 if ((cn = zfs_alloc(zhp->zfs_hdl,
669 sizeof (prop_changenode_t))) == NULL) {
671 changelist_free(clp);
675 cn->cn_handle = temp;
676 cn->cn_mounted = zfs_is_mounted(temp, NULL);
677 cn->cn_shared = zfs_is_shared(temp);
678 cn->cn_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
679 cn->cn_needpost = B_TRUE;
681 uu_list_node_init(cn, &cn->cn_listnode, clp->cl_pool);
682 if (clp->cl_sorted) {
684 (void) uu_list_find(clp->cl_list, cn, NULL, &idx);
685 uu_list_insert(clp->cl_list, cn, idx);
687 verify(uu_list_insert_after(clp->cl_list,
688 uu_list_last(clp->cl_list), cn) == 0);
692 * If the mountpoint property was previously 'legacy', or 'none',
693 * record it as the behavior of changelist_postfix() will be different.
695 if ((clp->cl_prop == ZFS_PROP_MOUNTPOINT) &&
696 (zfs_prop_get(zhp, prop, property, sizeof (property),
697 NULL, NULL, 0, B_FALSE) == 0 &&
698 (strcmp(property, "legacy") == 0 ||
699 strcmp(property, "none") == 0))) {
701 * do not automatically mount ex-legacy datasets if
702 * we specifically set canmount to noauto
704 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) !=
706 clp->cl_waslegacy = B_TRUE;