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
28 * Copyright (c) 2011 Pawel Jakub Dawidek <pawel@dawidek.net>.
29 * All rights reserved.
42 #include "libzfs_impl.h"
45 * Structure to keep track of dataset state. Before changing the 'sharenfs' or
46 * 'mountpoint' property, we record whether the filesystem was previously
47 * mounted/shared. This prior state dictates whether we remount/reshare the
48 * dataset after the property has been changed.
50 * The interface consists of the following sequence of functions:
55 * changelist_postfix()
60 * changelist_remove() - remove a node from a gathered list
61 * changelist_rename() - renames all datasets appropriately when doing a rename
62 * changelist_unshare() - unshares all the nodes in a given changelist
63 * changelist_haszonedchild() - check if there is any child exported to
66 typedef struct prop_changenode {
67 zfs_handle_t *cn_handle;
71 boolean_t cn_needpost; /* is postfix() needed? */
72 uu_list_node_t cn_listnode;
75 struct prop_changelist {
77 zfs_prop_t cl_realprop;
78 zfs_prop_t cl_shareprop; /* used with sharenfs/sharesmb */
79 uu_list_pool_t *cl_pool;
81 boolean_t cl_waslegacy;
82 boolean_t cl_allchildren;
83 boolean_t cl_alldependents;
84 int cl_mflags; /* Mount flags */
85 int cl_gflags; /* Gather request flags */
86 boolean_t cl_haszonedchild;
91 * If the property is 'mountpoint', go through and unmount filesystems as
92 * necessary. We don't do the same for 'sharenfs', because we can just re-share
93 * with different options without interrupting service. We do handle 'sharesmb'
94 * since there may be old resource names that need to be removed.
97 changelist_prefix(prop_changelist_t *clp)
99 prop_changenode_t *cn;
102 if (clp->cl_prop != ZFS_PROP_MOUNTPOINT &&
103 clp->cl_prop != ZFS_PROP_SHARESMB)
106 for (cn = uu_list_first(clp->cl_list); cn != NULL;
107 cn = uu_list_next(clp->cl_list, cn)) {
109 /* if a previous loop failed, set the remaining to false */
111 cn->cn_needpost = B_FALSE;
116 * If we are in the global zone, but this dataset is exported
117 * to a local zone, do nothing.
119 if (getzoneid() == GLOBAL_ZONEID && cn->cn_zoned)
122 if (!ZFS_IS_VOLUME(cn->cn_handle)) {
124 * Do the property specific processing.
126 switch (clp->cl_prop) {
127 case ZFS_PROP_MOUNTPOINT:
128 if (clp->cl_gflags & CL_GATHER_DONT_UNMOUNT)
130 if (zfs_unmount(cn->cn_handle, NULL,
131 clp->cl_mflags) != 0) {
133 cn->cn_needpost = B_FALSE;
136 case ZFS_PROP_SHARESMB:
137 (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;
167 * If we're changing the mountpoint, attempt to destroy the underlying
168 * mountpoint. All other datasets will have inherited from this dataset
169 * (in which case their mountpoints exist in the filesystem in the new
170 * location), or have explicit mountpoints set (in which case they won't
171 * be in the changelist).
173 if ((cn = uu_list_last(clp->cl_list)) == NULL)
176 if (clp->cl_prop == ZFS_PROP_MOUNTPOINT &&
177 !(clp->cl_gflags & CL_GATHER_DONT_UNMOUNT)) {
178 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 = (clp->cl_gflags & CL_GATHER_DONT_UNMOUNT) ||
235 zfs_is_mounted(cn->cn_handle, NULL);
237 if (!mounted && (cn->cn_mounted ||
238 ((sharenfs || sharesmb || clp->cl_waslegacy) &&
239 (zfs_prop_get_int(cn->cn_handle,
240 ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_ON)))) {
242 if (zfs_mount(cn->cn_handle, NULL, 0) != 0)
249 * If the file system is mounted we always re-share even
250 * if the filesystem is currently shared, so that we can
251 * adopt any new options.
253 if (sharenfs && mounted)
254 errors += zfs_share_nfs(cn->cn_handle);
255 else if (cn->cn_shared || clp->cl_waslegacy)
256 errors += zfs_unshare_nfs(cn->cn_handle, NULL);
257 if (sharesmb && mounted)
258 errors += zfs_share_smb(cn->cn_handle);
259 else if (cn->cn_shared || clp->cl_waslegacy)
260 errors += zfs_unshare_smb(cn->cn_handle, NULL);
263 return (errors ? -1 : 0);
267 * Is this "dataset" a child of "parent"?
270 isa_child_of(const char *dataset, const char *parent)
274 len = strlen(parent);
276 if (strncmp(dataset, parent, len) == 0 &&
277 (dataset[len] == '@' || dataset[len] == '/' ||
278 dataset[len] == '\0'))
286 * If we rename a filesystem, child filesystem handles are no longer valid
287 * since we identify each dataset by its name in the ZFS namespace. As a
288 * result, we have to go through and fix up all the names appropriately. We
289 * could do this automatically if libzfs kept track of all open handles, but
290 * this is a lot less work.
293 changelist_rename(prop_changelist_t *clp, const char *src, const char *dst)
295 prop_changenode_t *cn;
296 char newname[ZFS_MAXNAMELEN];
298 for (cn = uu_list_first(clp->cl_list); cn != NULL;
299 cn = uu_list_next(clp->cl_list, cn)) {
301 * Do not rename a clone that's not in the source hierarchy.
303 if (!isa_child_of(cn->cn_handle->zfs_name, src))
307 * Destroy the previous mountpoint if needed.
309 remove_mountpoint(cn->cn_handle);
311 (void) strlcpy(newname, dst, sizeof (newname));
312 (void) strcat(newname, cn->cn_handle->zfs_name + strlen(src));
314 (void) strlcpy(cn->cn_handle->zfs_name, newname,
315 sizeof (cn->cn_handle->zfs_name));
320 * Given a gathered changelist for the 'sharenfs' or 'sharesmb' property,
321 * unshare all the datasets in the list.
324 changelist_unshare(prop_changelist_t *clp, zfs_share_proto_t *proto)
326 prop_changenode_t *cn;
329 if (clp->cl_prop != ZFS_PROP_SHARENFS &&
330 clp->cl_prop != ZFS_PROP_SHARESMB)
333 for (cn = uu_list_first(clp->cl_list); cn != NULL;
334 cn = uu_list_next(clp->cl_list, cn)) {
335 if (zfs_unshare_proto(cn->cn_handle, NULL, proto) != 0)
343 * Check if there is any child exported to a local zone in a given changelist.
344 * This information has already been recorded while gathering the changelist
345 * via changelist_gather().
348 changelist_haszonedchild(prop_changelist_t *clp)
350 return (clp->cl_haszonedchild);
354 * Remove a node from a gathered list.
357 changelist_remove(prop_changelist_t *clp, const char *name)
359 prop_changenode_t *cn;
361 for (cn = uu_list_first(clp->cl_list); cn != NULL;
362 cn = uu_list_next(clp->cl_list, cn)) {
364 if (strcmp(cn->cn_handle->zfs_name, name) == 0) {
365 uu_list_remove(clp->cl_list, cn);
366 zfs_close(cn->cn_handle);
374 * Release any memory associated with a changelist.
377 changelist_free(prop_changelist_t *clp)
379 prop_changenode_t *cn;
384 while ((cn = uu_list_teardown(clp->cl_list, &cookie)) != NULL) {
385 zfs_close(cn->cn_handle);
389 uu_list_destroy(clp->cl_list);
392 uu_list_pool_destroy(clp->cl_pool);
398 change_one(zfs_handle_t *zhp, void *data)
400 prop_changelist_t *clp = data;
401 char property[ZFS_MAXPROPLEN];
403 prop_changenode_t *cn;
404 zprop_source_t sourcetype;
405 zprop_source_t share_sourcetype;
408 * We only want to unmount/unshare those filesystems that may inherit
409 * from the target filesystem. If we find any filesystem with a
410 * locally set mountpoint, we ignore any children since changing the
411 * property will not affect them. If this is a rename, we iterate
412 * over all children regardless, since we need them unmounted in
413 * order to do the rename. Also, if this is a volume and we're doing
414 * a rename, then always add it to the changelist.
417 if (!(ZFS_IS_VOLUME(zhp) && clp->cl_realprop == ZFS_PROP_NAME) &&
418 zfs_prop_get(zhp, clp->cl_prop, property,
419 sizeof (property), &sourcetype, where, sizeof (where),
426 * If we are "watching" sharenfs or sharesmb
427 * then check out the companion property which is tracked
430 if (clp->cl_shareprop != ZPROP_INVAL &&
431 zfs_prop_get(zhp, clp->cl_shareprop, property,
432 sizeof (property), &share_sourcetype, where, sizeof (where),
438 if (clp->cl_alldependents || clp->cl_allchildren ||
439 sourcetype == ZPROP_SRC_DEFAULT ||
440 sourcetype == ZPROP_SRC_INHERITED ||
441 (clp->cl_shareprop != ZPROP_INVAL &&
442 (share_sourcetype == ZPROP_SRC_DEFAULT ||
443 share_sourcetype == ZPROP_SRC_INHERITED))) {
444 if ((cn = zfs_alloc(zfs_get_handle(zhp),
445 sizeof (prop_changenode_t))) == NULL) {
451 cn->cn_mounted = (clp->cl_gflags & CL_GATHER_MOUNT_ALWAYS) ||
452 zfs_is_mounted(zhp, NULL);
453 cn->cn_shared = zfs_is_shared(zhp);
454 cn->cn_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
455 cn->cn_needpost = B_TRUE;
457 /* Indicate if any child is exported to a local zone. */
458 if (getzoneid() == GLOBAL_ZONEID && cn->cn_zoned)
459 clp->cl_haszonedchild = B_TRUE;
461 uu_list_node_init(cn, &cn->cn_listnode, clp->cl_pool);
463 if (clp->cl_sorted) {
466 (void) uu_list_find(clp->cl_list, cn, NULL,
468 uu_list_insert(clp->cl_list, cn, idx);
471 * Add this child to beginning of the list. Children
472 * below this one in the hierarchy will get added above
473 * this one in the list. This produces a list in
474 * reverse dataset name order.
475 * This is necessary when the original mountpoint
478 verify(uu_list_insert_before(clp->cl_list,
479 uu_list_first(clp->cl_list), cn) == 0);
482 if (!clp->cl_alldependents)
483 return (zfs_iter_children(zhp, change_one, data));
493 compare_mountpoints(const void *a, const void *b, void *unused)
495 const prop_changenode_t *ca = a;
496 const prop_changenode_t *cb = b;
498 char mounta[MAXPATHLEN];
499 char mountb[MAXPATHLEN];
501 boolean_t hasmounta, hasmountb;
504 * When unsharing or unmounting filesystems, we need to do it in
505 * mountpoint order. This allows the user to have a mountpoint
506 * hierarchy that is different from the dataset hierarchy, and still
507 * allow it to be changed. However, if either dataset doesn't have a
508 * mountpoint (because it is a volume or a snapshot), we place it at the
509 * end of the list, because it doesn't affect our change at all.
511 hasmounta = (zfs_prop_get(ca->cn_handle, ZFS_PROP_MOUNTPOINT, mounta,
512 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
513 hasmountb = (zfs_prop_get(cb->cn_handle, ZFS_PROP_MOUNTPOINT, mountb,
514 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
516 if (!hasmounta && hasmountb)
518 else if (hasmounta && !hasmountb)
520 else if (!hasmounta && !hasmountb)
523 return (strcmp(mountb, mounta));
527 * Given a ZFS handle and a property, construct a complete list of datasets
528 * that need to be modified as part of this process. For anything but the
529 * 'mountpoint' and 'sharenfs' properties, this just returns an empty list.
530 * Otherwise, we iterate over all children and look for any datasets that
531 * inherit the property. For each such dataset, we add it to the list and
532 * mark whether it was shared beforehand.
535 changelist_gather(zfs_handle_t *zhp, zfs_prop_t prop, int gather_flags,
538 prop_changelist_t *clp;
539 prop_changenode_t *cn;
541 char property[ZFS_MAXPROPLEN];
542 uu_compare_fn_t *compare = NULL;
543 boolean_t legacy = B_FALSE;
545 if ((clp = zfs_alloc(zhp->zfs_hdl, sizeof (prop_changelist_t))) == NULL)
549 * For mountpoint-related tasks, we want to sort everything by
550 * mountpoint, so that we mount and unmount them in the appropriate
551 * order, regardless of their position in the hierarchy.
553 if (prop == ZFS_PROP_NAME || prop == ZFS_PROP_ZONED ||
554 prop == ZFS_PROP_MOUNTPOINT || prop == ZFS_PROP_SHARENFS ||
555 prop == ZFS_PROP_SHARESMB) {
557 if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
558 property, sizeof (property),
559 NULL, NULL, 0, B_FALSE) == 0 &&
560 (strcmp(property, "legacy") == 0 ||
561 strcmp(property, "none") == 0)) {
566 compare = compare_mountpoints;
567 clp->cl_sorted = B_TRUE;
571 clp->cl_pool = uu_list_pool_create("changelist_pool",
572 sizeof (prop_changenode_t),
573 offsetof(prop_changenode_t, cn_listnode),
575 if (clp->cl_pool == NULL) {
576 assert(uu_error() == UU_ERROR_NO_MEMORY);
577 (void) zfs_error(zhp->zfs_hdl, EZFS_NOMEM, "internal error");
578 changelist_free(clp);
582 clp->cl_list = uu_list_create(clp->cl_pool, NULL,
583 clp->cl_sorted ? UU_LIST_SORTED : 0);
584 clp->cl_gflags = gather_flags;
585 clp->cl_mflags = mnt_flags;
587 if (clp->cl_list == NULL) {
588 assert(uu_error() == UU_ERROR_NO_MEMORY);
589 (void) zfs_error(zhp->zfs_hdl, EZFS_NOMEM, "internal error");
590 changelist_free(clp);
595 * If this is a rename or the 'zoned' property, we pretend we're
596 * changing the mountpoint and flag it so we can catch all children in
599 * Flag cl_alldependents to catch all children plus the dependents
600 * (clones) that are not in the hierarchy.
602 if (prop == ZFS_PROP_NAME) {
603 clp->cl_prop = ZFS_PROP_MOUNTPOINT;
604 clp->cl_alldependents = B_TRUE;
605 } else if (prop == ZFS_PROP_ZONED) {
606 clp->cl_prop = ZFS_PROP_MOUNTPOINT;
607 clp->cl_allchildren = B_TRUE;
608 } else if (prop == ZFS_PROP_CANMOUNT) {
609 clp->cl_prop = ZFS_PROP_MOUNTPOINT;
610 } else if (prop == ZFS_PROP_VOLSIZE) {
611 clp->cl_prop = ZFS_PROP_MOUNTPOINT;
615 clp->cl_realprop = prop;
617 if (clp->cl_prop != ZFS_PROP_MOUNTPOINT &&
618 clp->cl_prop != ZFS_PROP_SHARENFS &&
619 clp->cl_prop != ZFS_PROP_SHARESMB)
623 * If watching SHARENFS or SHARESMB then
624 * also watch its companion property.
626 if (clp->cl_prop == ZFS_PROP_SHARENFS)
627 clp->cl_shareprop = ZFS_PROP_SHARESMB;
628 else if (clp->cl_prop == ZFS_PROP_SHARESMB)
629 clp->cl_shareprop = ZFS_PROP_SHARENFS;
631 if (clp->cl_alldependents) {
632 if (zfs_iter_dependents(zhp, B_TRUE, change_one, clp) != 0) {
633 changelist_free(clp);
636 } else if (zfs_iter_children(zhp, change_one, clp) != 0) {
637 changelist_free(clp);
642 * We have to re-open ourselves because we auto-close all the handles
643 * and can't tell the difference.
645 if ((temp = zfs_open(zhp->zfs_hdl, zfs_get_name(zhp),
646 ZFS_TYPE_DATASET)) == NULL) {
647 changelist_free(clp);
652 * Always add ourself to the list. We add ourselves to the end so that
653 * we're the last to be unmounted.
655 if ((cn = zfs_alloc(zhp->zfs_hdl,
656 sizeof (prop_changenode_t))) == NULL) {
658 changelist_free(clp);
662 cn->cn_handle = temp;
663 cn->cn_mounted = (clp->cl_gflags & CL_GATHER_MOUNT_ALWAYS) ||
664 zfs_is_mounted(temp, NULL);
665 cn->cn_shared = zfs_is_shared(temp);
666 cn->cn_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
667 cn->cn_needpost = B_TRUE;
669 uu_list_node_init(cn, &cn->cn_listnode, clp->cl_pool);
670 if (clp->cl_sorted) {
672 (void) uu_list_find(clp->cl_list, cn, NULL, &idx);
673 uu_list_insert(clp->cl_list, cn, idx);
676 * Add the target dataset to the end of the list.
677 * The list is not really unsorted. The list will be
678 * in reverse dataset name order. This is necessary
679 * when the original mountpoint is legacy or none.
681 verify(uu_list_insert_after(clp->cl_list,
682 uu_list_last(clp->cl_list), cn) == 0);
686 * If the mountpoint property was previously 'legacy', or 'none',
687 * record it as the behavior of changelist_postfix() will be different.
689 if ((clp->cl_prop == ZFS_PROP_MOUNTPOINT) && legacy) {
691 * do not automatically mount ex-legacy datasets if
692 * we specifically set canmount to noauto
694 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) !=
696 clp->cl_waslegacy = B_TRUE;