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 2015 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright (c) 2014, 2020 by Delphix. All rights reserved.
26 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
27 * Copyright 2017 RackTop Systems.
28 * Copyright (c) 2018 Datto Inc.
29 * Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
33 * Routines to manage ZFS mounts. We separate all the nasty routines that have
34 * to deal with the OS. The following functions are the main entry points --
35 * they are used by mount and unmount and when changing a filesystem's
44 * This file also contains the functions used to manage sharing filesystems via
57 * zfs_unshareall_nfs()
58 * zfs_unshareall_smb()
60 * zfs_unshareall_bypath()
62 * The following functions are available for pool consumers, and will
63 * mount/unmount and share/unshare all datasets within pool:
65 * zpool_enable_datasets()
66 * zpool_disable_datasets()
80 #include <sys/mntent.h>
81 #include <sys/mount.h>
84 #include <sys/dsl_crypt.h>
88 #include "libzfs_impl.h"
89 #include <thread_pool.h>
92 #include <sys/systeminfo.h>
93 #define MAXISALEN 257 /* based on sysinfo(2) man page */
95 static int mount_tp_nthr = 512; /* tpool threads for multi-threaded mounting */
97 static void zfs_mount_task(void *);
98 zfs_share_type_t zfs_is_shared_proto(zfs_handle_t *, char **,
102 * The share protocols table must be in the same order as the zfs_share_proto_t
103 * enum in libzfs_impl.h
105 proto_table_t proto_table[PROTO_END] = {
106 {ZFS_PROP_SHARENFS, "nfs", EZFS_SHARENFSFAILED, EZFS_UNSHARENFSFAILED},
107 {ZFS_PROP_SHARESMB, "smb", EZFS_SHARESMBFAILED, EZFS_UNSHARESMBFAILED},
110 zfs_share_proto_t nfs_only[] = {
115 zfs_share_proto_t smb_only[] = {
119 zfs_share_proto_t share_all_proto[] = {
128 dir_is_empty_stat(const char *dirname)
133 * We only want to return false if the given path is a non empty
134 * directory, all other errors are handled elsewhere.
136 if (stat(dirname, &st) < 0 || !S_ISDIR(st.st_mode)) {
141 * An empty directory will still have two entries in it, one
142 * entry for each of "." and "..".
144 if (st.st_size > 2) {
152 dir_is_empty_readdir(const char *dirname)
158 if ((dirfd = openat(AT_FDCWD, dirname,
159 O_RDONLY | O_NDELAY | O_LARGEFILE | O_CLOEXEC, 0)) < 0) {
163 if ((dirp = fdopendir(dirfd)) == NULL) {
168 while ((dp = readdir64(dirp)) != NULL) {
170 if (strcmp(dp->d_name, ".") == 0 ||
171 strcmp(dp->d_name, "..") == 0)
174 (void) closedir(dirp);
178 (void) closedir(dirp);
183 * Returns true if the specified directory is empty. If we can't open the
184 * directory at all, return true so that the mount can fail with a more
185 * informative error message.
188 dir_is_empty(const char *dirname)
193 * If the statvfs call fails or the filesystem is not a ZFS
194 * filesystem, fall back to the slow path which uses readdir.
196 if ((statfs64(dirname, &st) != 0) ||
197 (st.f_type != ZFS_SUPER_MAGIC)) {
198 return (dir_is_empty_readdir(dirname));
202 * At this point, we know the provided path is on a ZFS
203 * filesystem, so we can use stat instead of readdir to
204 * determine if the directory is empty or not. We try to avoid
205 * using readdir because that requires opening "dirname"; this
206 * open file descriptor can potentially end up in a child
207 * process if there's a concurrent fork, thus preventing the
208 * zfs_mount() from otherwise succeeding (the open file
209 * descriptor inherited by the child process will cause the
210 * parent's mount to fail with EBUSY). The performance
211 * implications of replacing the open, read, and close with a
212 * single stat is nice; but is not the main motivation for the
215 return (dir_is_empty_stat(dirname));
219 * Checks to see if the mount is active. If the filesystem is mounted, we fill
220 * in 'where' with the current mountpoint, and return 1. Otherwise, we return
224 is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where)
228 if (libzfs_mnttab_find(zfs_hdl, special, &entry) != 0)
232 *where = zfs_strdup(zfs_hdl, entry.mnt_mountp);
238 zfs_is_mounted(zfs_handle_t *zhp, char **where)
240 return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where));
244 * Checks any higher order concerns about whether the given dataset is
245 * mountable, false otherwise. zfs_is_mountable_internal specifically assumes
246 * that the caller has verified the sanity of mounting the dataset at
247 * mountpoint to the extent the caller wants.
250 zfs_is_mountable_internal(zfs_handle_t *zhp, const char *mountpoint)
253 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
254 getzoneid() == GLOBAL_ZONEID)
261 * Returns true if the given dataset is mountable, false otherwise. Returns the
262 * mountpoint in 'buf'.
265 zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen,
266 zprop_source_t *source, int flags)
268 char sourceloc[MAXNAMELEN];
269 zprop_source_t sourcetype;
271 if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type,
275 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen,
276 &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0);
278 if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 ||
279 strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0)
282 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF)
285 if (!zfs_is_mountable_internal(zhp, buf))
288 if (zfs_prop_get_int(zhp, ZFS_PROP_REDACTED) && !(flags & MS_FORCE))
292 *source = sourcetype;
298 * The filesystem is mounted by invoking the system mount utility rather
299 * than by the system call mount(2). This ensures that the /etc/mtab
300 * file is correctly locked for the update. Performing our own locking
301 * and /etc/mtab update requires making an unsafe assumption about how
302 * the mount utility performs its locking. Unfortunately, this also means
303 * in the case of a mount failure we do not have the exact errno. We must
304 * make due with return value from the mount process.
306 * In the long term a shared library called libmount is under development
307 * which provides a common API to address the locking and errno issues.
308 * Once the standard mount utility has been updated to use this library
309 * we can add an autoconf check to conditionally use it.
311 * http://www.kernel.org/pub/linux/utils/util-linux/libmount-docs/index.html
315 zfs_add_option(zfs_handle_t *zhp, char *options, int len,
316 zfs_prop_t prop, char *on, char *off)
321 /* Skip adding duplicate default options */
322 if ((strstr(options, on) != NULL) || (strstr(options, off) != NULL))
326 * zfs_prop_get_int() is not used to ensure our mount options
327 * are not influenced by the current /proc/self/mounts contents.
329 value = getprop_uint64(zhp, prop, &source);
331 (void) strlcat(options, ",", len);
332 (void) strlcat(options, value ? on : off, len);
338 zfs_add_options(zfs_handle_t *zhp, char *options, int len)
342 error = zfs_add_option(zhp, options, len,
343 ZFS_PROP_ATIME, MNTOPT_ATIME, MNTOPT_NOATIME);
345 * don't add relatime/strictatime when atime=off, otherwise strictatime
346 * will force atime=on
348 if (strstr(options, MNTOPT_NOATIME) == NULL) {
349 error = zfs_add_option(zhp, options, len,
350 ZFS_PROP_RELATIME, MNTOPT_RELATIME, MNTOPT_STRICTATIME);
352 error = error ? error : zfs_add_option(zhp, options, len,
353 ZFS_PROP_DEVICES, MNTOPT_DEVICES, MNTOPT_NODEVICES);
354 error = error ? error : zfs_add_option(zhp, options, len,
355 ZFS_PROP_EXEC, MNTOPT_EXEC, MNTOPT_NOEXEC);
356 error = error ? error : zfs_add_option(zhp, options, len,
357 ZFS_PROP_READONLY, MNTOPT_RO, MNTOPT_RW);
358 error = error ? error : zfs_add_option(zhp, options, len,
359 ZFS_PROP_SETUID, MNTOPT_SETUID, MNTOPT_NOSETUID);
360 error = error ? error : zfs_add_option(zhp, options, len,
361 ZFS_PROP_NBMAND, MNTOPT_NBMAND, MNTOPT_NONBMAND);
367 zfs_mount(zfs_handle_t *zhp, const char *options, int flags)
369 char mountpoint[ZFS_MAXPROPLEN];
371 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL,
375 return (zfs_mount_at(zhp, options, flags, mountpoint));
379 * Mount the given filesystem.
382 zfs_mount_at(zfs_handle_t *zhp, const char *options, int flags,
383 const char *mountpoint)
386 char mntopts[MNT_LINE_MAX];
387 char overlay[ZFS_MAXPROPLEN];
388 libzfs_handle_t *hdl = zhp->zfs_hdl;
392 if (options == NULL) {
393 (void) strlcpy(mntopts, MNTOPT_DEFAULTS, sizeof (mntopts));
395 (void) strlcpy(mntopts, options, sizeof (mntopts));
398 if (strstr(mntopts, MNTOPT_REMOUNT) != NULL)
401 /* Potentially duplicates some checks if invoked by zfs_mount(). */
402 if (!zfs_is_mountable_internal(zhp, mountpoint))
406 * If the pool is imported read-only then all mounts must be read-only
408 if (zpool_get_prop_int(zhp->zpool_hdl, ZPOOL_PROP_READONLY, NULL))
409 (void) strlcat(mntopts, "," MNTOPT_RO, sizeof (mntopts));
412 * Append default mount options which apply to the mount point.
413 * This is done because under Linux (unlike Solaris) multiple mount
414 * points may reference a single super block. This means that just
415 * given a super block there is no back reference to update the per
416 * mount point options.
418 rc = zfs_add_options(zhp, mntopts, sizeof (mntopts));
420 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
421 "default options unavailable"));
422 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
423 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
428 * If the filesystem is encrypted the key must be loaded in order to
429 * mount. If the key isn't loaded, the MS_CRYPT flag decides whether
430 * or not we attempt to load the keys. Note: we must call
431 * zfs_refresh_properties() here since some callers of this function
432 * (most notably zpool_enable_datasets()) may implicitly load our key
433 * by loading the parent's key first.
435 if (zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF) {
436 zfs_refresh_properties(zhp);
437 keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
440 * If the key is unavailable and MS_CRYPT is set give the
441 * user a chance to enter the key. Otherwise just fail
444 if (keystatus == ZFS_KEYSTATUS_UNAVAILABLE) {
445 if (flags & MS_CRYPT) {
446 rc = zfs_crypto_load_key(zhp, B_FALSE, NULL);
450 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
451 "encryption key not loaded"));
452 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
453 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
461 * Append zfsutil option so the mount helper allow the mount
463 strlcat(mntopts, "," MNTOPT_ZFSUTIL, sizeof (mntopts));
465 /* Create the directory if it doesn't already exist */
466 if (lstat(mountpoint, &buf) != 0) {
467 if (mkdirp(mountpoint, 0755) != 0) {
468 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
469 "failed to create mountpoint: %s"),
471 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
472 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
478 * Overlay mounts are enabled by default but may be disabled
479 * via the 'overlay' property. The -O flag remains for compatibility.
481 if (!(flags & MS_OVERLAY)) {
482 if (zfs_prop_get(zhp, ZFS_PROP_OVERLAY, overlay,
483 sizeof (overlay), NULL, NULL, 0, B_FALSE) == 0) {
484 if (strcmp(overlay, "on") == 0) {
491 * Determine if the mountpoint is empty. If so, refuse to perform the
492 * mount. We don't perform this check if 'remount' is
493 * specified or if overlay option (-O) is given
495 if ((flags & MS_OVERLAY) == 0 && !remount &&
496 !dir_is_empty(mountpoint)) {
497 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
498 "directory is not empty"));
499 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
500 dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint));
503 /* perform the mount */
504 rc = do_mount(zhp, mountpoint, mntopts, flags);
507 * Generic errors are nasty, but there are just way too many
508 * from mount(), and they're well-understood. We pick a few
509 * common ones to improve upon.
512 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
513 "mountpoint or dataset is busy"));
514 } else if (rc == EPERM) {
515 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
516 "Insufficient privileges"));
517 } else if (rc == ENOTSUP) {
521 VERIFY(zfs_spa_version(zhp, &spa_version) == 0);
522 (void) snprintf(buf, sizeof (buf),
523 dgettext(TEXT_DOMAIN, "Can't mount a version %lld "
524 "file system on a version %d pool. Pool must be"
525 " upgraded to mount this file system."),
526 (u_longlong_t)zfs_prop_get_int(zhp,
527 ZFS_PROP_VERSION), spa_version);
528 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, buf));
530 zfs_error_aux(hdl, strerror(rc));
532 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
533 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
537 /* remove the mounted entry before re-adding on remount */
539 libzfs_mnttab_remove(hdl, zhp->zfs_name);
541 /* add the mounted entry into our cache */
542 libzfs_mnttab_add(hdl, zfs_get_name(zhp), mountpoint, mntopts);
547 * Unmount a single filesystem.
550 unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags)
554 error = do_unmount(mountpoint, flags);
556 return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED,
557 dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
565 * Unmount the given filesystem.
568 zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
570 libzfs_handle_t *hdl = zhp->zfs_hdl;
573 boolean_t encroot, unmounted = B_FALSE;
575 /* check to see if we need to unmount the filesystem */
576 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
577 libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)) {
579 * mountpoint may have come from a call to
580 * getmnt/getmntany if it isn't NULL. If it is NULL,
581 * we know it comes from libzfs_mnttab_find which can
582 * then get freed later. We strdup it to play it safe.
584 if (mountpoint == NULL)
585 mntpt = zfs_strdup(hdl, entry.mnt_mountp);
587 mntpt = zfs_strdup(hdl, mountpoint);
590 * Unshare and unmount the filesystem
592 if (zfs_unshare_proto(zhp, mntpt, share_all_proto) != 0) {
596 zfs_commit_all_shares();
598 if (unmount_one(hdl, mntpt, flags) != 0) {
600 (void) zfs_shareall(zhp);
601 zfs_commit_all_shares();
605 libzfs_mnttab_remove(hdl, zhp->zfs_name);
611 * If the MS_CRYPT flag is provided we must ensure we attempt to
612 * unload the dataset's key regardless of whether we did any work
613 * to unmount it. We only do this for encryption roots.
615 if ((flags & MS_CRYPT) != 0 &&
616 zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF) {
617 zfs_refresh_properties(zhp);
619 if (zfs_crypto_get_encryption_root(zhp, &encroot, NULL) != 0 &&
621 (void) zfs_mount(zhp, NULL, 0);
625 if (encroot && zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS) ==
626 ZFS_KEYSTATUS_AVAILABLE &&
627 zfs_crypto_unload_key(zhp) != 0) {
628 (void) zfs_mount(zhp, NULL, 0);
637 * Unmount this filesystem and any children inheriting the mountpoint property.
638 * To do this, just act like we're changing the mountpoint property, but don't
639 * remount the filesystems afterwards.
642 zfs_unmountall(zfs_handle_t *zhp, int flags)
644 prop_changelist_t *clp;
647 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
648 CL_GATHER_ITER_MOUNTED, flags);
652 ret = changelist_prefix(clp);
653 changelist_free(clp);
659 zfs_is_shared(zfs_handle_t *zhp)
661 zfs_share_type_t rc = 0;
662 zfs_share_proto_t *curr_proto;
664 if (ZFS_IS_VOLUME(zhp))
667 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
669 rc |= zfs_is_shared_proto(zhp, NULL, *curr_proto);
671 return (rc ? B_TRUE : B_FALSE);
675 * Unshare a filesystem by mountpoint.
678 unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint,
679 zfs_share_proto_t proto)
683 err = sa_disable_share(mountpoint, proto_table[proto].p_name);
685 return (zfs_error_fmt(hdl, proto_table[proto].p_unshare_err,
686 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
687 name, sa_errorstr(err)));
693 * Query libshare for the given mountpoint and protocol, returning
694 * a zfs_share_type_t value.
697 is_shared(const char *mountpoint, zfs_share_proto_t proto)
699 if (sa_is_shared(mountpoint, proto_table[proto].p_name)) {
706 return (SHARED_NOT_SHARED);
709 return (SHARED_NOT_SHARED);
713 * Share the given filesystem according to the options in the specified
714 * protocol specific properties (sharenfs, sharesmb). We rely
715 * on "libshare" to do the dirty work for us.
718 zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
720 char mountpoint[ZFS_MAXPROPLEN];
721 char shareopts[ZFS_MAXPROPLEN];
722 char sourcestr[ZFS_MAXPROPLEN];
723 zfs_share_proto_t *curr_proto;
724 zprop_source_t sourcetype;
727 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL, 0))
730 for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) {
732 * Return success if there are no share options.
734 if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop,
735 shareopts, sizeof (shareopts), &sourcetype, sourcestr,
736 ZFS_MAXPROPLEN, B_FALSE) != 0 ||
737 strcmp(shareopts, "off") == 0)
741 * If the 'zoned' property is set, then zfs_is_mountable()
742 * will have already bailed out if we are in the global zone.
743 * But local zones cannot be NFS servers, so we ignore it for
744 * local zones as well.
746 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
749 err = sa_enable_share(zfs_get_name(zhp), mountpoint, shareopts,
750 proto_table[*curr_proto].p_name);
752 return (zfs_error_fmt(zhp->zfs_hdl,
753 proto_table[*curr_proto].p_share_err,
754 dgettext(TEXT_DOMAIN, "cannot share '%s: %s'"),
755 zfs_get_name(zhp), sa_errorstr(err)));
763 zfs_share(zfs_handle_t *zhp)
765 assert(!ZFS_IS_VOLUME(zhp));
766 return (zfs_share_proto(zhp, share_all_proto));
770 zfs_unshare(zfs_handle_t *zhp)
772 assert(!ZFS_IS_VOLUME(zhp));
773 return (zfs_unshareall(zhp));
777 * Check to see if the filesystem is currently shared.
780 zfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto)
785 if (!zfs_is_mounted(zhp, &mountpoint))
786 return (SHARED_NOT_SHARED);
788 if ((rc = is_shared(mountpoint, proto))
789 != SHARED_NOT_SHARED) {
797 return (SHARED_NOT_SHARED);
802 zfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
804 return (zfs_is_shared_proto(zhp, where,
805 PROTO_NFS) != SHARED_NOT_SHARED);
809 zfs_is_shared_smb(zfs_handle_t *zhp, char **where)
811 return (zfs_is_shared_proto(zhp, where,
812 PROTO_SMB) != SHARED_NOT_SHARED);
816 * zfs_parse_options(options, proto)
818 * Call the legacy parse interface to get the protocol specific
819 * options using the NULL arg to indicate that this is a "parse" only.
822 zfs_parse_options(char *options, zfs_share_proto_t proto)
824 return (sa_validate_shareopts(options, proto_table[proto].p_name));
828 zfs_commit_proto(zfs_share_proto_t *proto)
830 zfs_share_proto_t *curr_proto;
831 for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) {
832 sa_commit_shares(proto_table[*curr_proto].p_name);
837 zfs_commit_nfs_shares(void)
839 zfs_commit_proto(nfs_only);
843 zfs_commit_smb_shares(void)
845 zfs_commit_proto(smb_only);
849 zfs_commit_all_shares(void)
851 zfs_commit_proto(share_all_proto);
855 zfs_commit_shares(const char *proto)
858 zfs_commit_proto(share_all_proto);
859 else if (strcmp(proto, "nfs") == 0)
860 zfs_commit_proto(nfs_only);
861 else if (strcmp(proto, "smb") == 0)
862 zfs_commit_proto(smb_only);
866 zfs_share_nfs(zfs_handle_t *zhp)
868 return (zfs_share_proto(zhp, nfs_only));
872 zfs_share_smb(zfs_handle_t *zhp)
874 return (zfs_share_proto(zhp, smb_only));
878 zfs_shareall(zfs_handle_t *zhp)
880 return (zfs_share_proto(zhp, share_all_proto));
884 * Unshare the given filesystem.
887 zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint,
888 zfs_share_proto_t *proto)
890 libzfs_handle_t *hdl = zhp->zfs_hdl;
894 /* check to see if need to unmount the filesystem */
895 if (mountpoint != NULL)
896 mntpt = zfs_strdup(hdl, mountpoint);
898 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
899 libzfs_mnttab_find(hdl, zfs_get_name(zhp), &entry) == 0)) {
900 zfs_share_proto_t *curr_proto;
902 if (mountpoint == NULL)
903 mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp);
905 for (curr_proto = proto; *curr_proto != PROTO_END;
908 if (is_shared(mntpt, *curr_proto)) {
909 if (unshare_one(hdl, zhp->zfs_name,
910 mntpt, *curr_proto) != 0) {
925 zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
927 return (zfs_unshare_proto(zhp, mountpoint, nfs_only));
931 zfs_unshare_smb(zfs_handle_t *zhp, const char *mountpoint)
933 return (zfs_unshare_proto(zhp, mountpoint, smb_only));
937 * Same as zfs_unmountall(), but for NFS and SMB unshares.
940 zfs_unshareall_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
942 prop_changelist_t *clp;
945 clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0, 0);
949 ret = changelist_unshare(clp, proto);
950 changelist_free(clp);
956 zfs_unshareall_nfs(zfs_handle_t *zhp)
958 return (zfs_unshareall_proto(zhp, nfs_only));
962 zfs_unshareall_smb(zfs_handle_t *zhp)
964 return (zfs_unshareall_proto(zhp, smb_only));
968 zfs_unshareall(zfs_handle_t *zhp)
970 return (zfs_unshareall_proto(zhp, share_all_proto));
974 zfs_unshareall_bypath(zfs_handle_t *zhp, const char *mountpoint)
976 return (zfs_unshare_proto(zhp, mountpoint, share_all_proto));
980 zfs_unshareall_bytype(zfs_handle_t *zhp, const char *mountpoint,
984 return (zfs_unshare_proto(zhp, mountpoint, share_all_proto));
985 if (strcmp(proto, "nfs") == 0)
986 return (zfs_unshare_proto(zhp, mountpoint, nfs_only));
987 else if (strcmp(proto, "smb") == 0)
988 return (zfs_unshare_proto(zhp, mountpoint, smb_only));
994 * Remove the mountpoint associated with the current dataset, if necessary.
995 * We only remove the underlying directory if:
997 * - The mountpoint is not 'none' or 'legacy'
998 * - The mountpoint is non-empty
999 * - The mountpoint is the default or inherited
1000 * - The 'zoned' property is set, or we're in a local zone
1002 * Any other directories we leave alone.
1005 remove_mountpoint(zfs_handle_t *zhp)
1007 char mountpoint[ZFS_MAXPROPLEN];
1008 zprop_source_t source;
1010 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
1014 if (source == ZPROP_SRC_DEFAULT ||
1015 source == ZPROP_SRC_INHERITED) {
1017 * Try to remove the directory, silently ignoring any errors.
1018 * The filesystem may have since been removed or moved around,
1019 * and this error isn't really useful to the administrator in
1022 (void) rmdir(mountpoint);
1027 * Add the given zfs handle to the cb_handles array, dynamically reallocating
1028 * the array if it is out of space.
1031 libzfs_add_handle(get_all_cb_t *cbp, zfs_handle_t *zhp)
1033 if (cbp->cb_alloc == cbp->cb_used) {
1035 zfs_handle_t **newhandles;
1037 newsz = cbp->cb_alloc != 0 ? cbp->cb_alloc * 2 : 64;
1038 newhandles = zfs_realloc(zhp->zfs_hdl,
1039 cbp->cb_handles, cbp->cb_alloc * sizeof (zfs_handle_t *),
1040 newsz * sizeof (zfs_handle_t *));
1041 cbp->cb_handles = newhandles;
1042 cbp->cb_alloc = newsz;
1044 cbp->cb_handles[cbp->cb_used++] = zhp;
1048 * Recursive helper function used during file system enumeration
1051 zfs_iter_cb(zfs_handle_t *zhp, void *data)
1053 get_all_cb_t *cbp = data;
1055 if (!(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM)) {
1060 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_NOAUTO) {
1065 if (zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS) ==
1066 ZFS_KEYSTATUS_UNAVAILABLE) {
1072 * If this filesystem is inconsistent and has a receive resume
1073 * token, we can not mount it.
1075 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
1076 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
1077 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
1082 libzfs_add_handle(cbp, zhp);
1083 if (zfs_iter_filesystems(zhp, zfs_iter_cb, cbp) != 0) {
1091 * Sort comparator that compares two mountpoint paths. We sort these paths so
1092 * that subdirectories immediately follow their parents. This means that we
1093 * effectively treat the '/' character as the lowest value non-nul char.
1094 * Since filesystems from non-global zones can have the same mountpoint
1095 * as other filesystems, the comparator sorts global zone filesystems to
1096 * the top of the list. This means that the global zone will traverse the
1097 * filesystem list in the correct order and can stop when it sees the
1098 * first zoned filesystem. In a non-global zone, only the delegated
1099 * filesystems are seen.
1101 * An example sorted list using this comparator would look like:
1111 * The mounting code depends on this ordering to deterministically iterate
1112 * over filesystems in order to spawn parallel mount tasks.
1115 mountpoint_cmp(const void *arga, const void *argb)
1117 zfs_handle_t *const *zap = arga;
1118 zfs_handle_t *za = *zap;
1119 zfs_handle_t *const *zbp = argb;
1120 zfs_handle_t *zb = *zbp;
1121 char mounta[MAXPATHLEN];
1122 char mountb[MAXPATHLEN];
1123 const char *a = mounta;
1124 const char *b = mountb;
1125 boolean_t gota, gotb;
1126 uint64_t zoneda, zonedb;
1128 zoneda = zfs_prop_get_int(za, ZFS_PROP_ZONED);
1129 zonedb = zfs_prop_get_int(zb, ZFS_PROP_ZONED);
1130 if (zoneda && !zonedb)
1132 if (!zoneda && zonedb)
1135 gota = (zfs_get_type(za) == ZFS_TYPE_FILESYSTEM);
1137 verify(zfs_prop_get(za, ZFS_PROP_MOUNTPOINT, mounta,
1138 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
1140 gotb = (zfs_get_type(zb) == ZFS_TYPE_FILESYSTEM);
1142 verify(zfs_prop_get(zb, ZFS_PROP_MOUNTPOINT, mountb,
1143 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
1147 while (*a != '\0' && (*a == *b)) {
1161 return (*a < *b ? -1 : *a > *b);
1170 * If neither filesystem has a mountpoint, revert to sorting by
1173 return (strcmp(zfs_get_name(za), zfs_get_name(zb)));
1177 * Return true if path2 is a child of path1 or path2 equals path1 or
1178 * path1 is "/" (path2 is always a child of "/").
1181 libzfs_path_contains(const char *path1, const char *path2)
1183 return (strcmp(path1, path2) == 0 || strcmp(path1, "/") == 0 ||
1184 (strstr(path2, path1) == path2 && path2[strlen(path1)] == '/'));
1188 * Given a mountpoint specified by idx in the handles array, find the first
1189 * non-descendent of that mountpoint and return its index. Descendant paths
1190 * start with the parent's path. This function relies on the ordering
1191 * enforced by mountpoint_cmp().
1194 non_descendant_idx(zfs_handle_t **handles, size_t num_handles, int idx)
1196 char parent[ZFS_MAXPROPLEN];
1197 char child[ZFS_MAXPROPLEN];
1200 verify(zfs_prop_get(handles[idx], ZFS_PROP_MOUNTPOINT, parent,
1201 sizeof (parent), NULL, NULL, 0, B_FALSE) == 0);
1203 for (i = idx + 1; i < num_handles; i++) {
1204 verify(zfs_prop_get(handles[i], ZFS_PROP_MOUNTPOINT, child,
1205 sizeof (child), NULL, NULL, 0, B_FALSE) == 0);
1206 if (!libzfs_path_contains(parent, child))
1212 typedef struct mnt_param {
1213 libzfs_handle_t *mnt_hdl;
1215 zfs_handle_t **mnt_zhps; /* filesystems to mount */
1216 size_t mnt_num_handles;
1217 int mnt_idx; /* Index of selected entry to mount */
1218 zfs_iter_f mnt_func;
1223 * Allocate and populate the parameter struct for mount function, and
1224 * schedule mounting of the entry selected by idx.
1227 zfs_dispatch_mount(libzfs_handle_t *hdl, zfs_handle_t **handles,
1228 size_t num_handles, int idx, zfs_iter_f func, void *data, tpool_t *tp)
1230 mnt_param_t *mnt_param = zfs_alloc(hdl, sizeof (mnt_param_t));
1232 mnt_param->mnt_hdl = hdl;
1233 mnt_param->mnt_tp = tp;
1234 mnt_param->mnt_zhps = handles;
1235 mnt_param->mnt_num_handles = num_handles;
1236 mnt_param->mnt_idx = idx;
1237 mnt_param->mnt_func = func;
1238 mnt_param->mnt_data = data;
1240 (void) tpool_dispatch(tp, zfs_mount_task, (void*)mnt_param);
1244 * This is the structure used to keep state of mounting or sharing operations
1245 * during a call to zpool_enable_datasets().
1247 typedef struct mount_state {
1249 * ms_mntstatus is set to -1 if any mount fails. While multiple threads
1250 * could update this variable concurrently, no synchronization is
1251 * needed as it's only ever set to -1.
1255 const char *ms_mntopts;
1259 zfs_mount_one(zfs_handle_t *zhp, void *arg)
1261 mount_state_t *ms = arg;
1265 * don't attempt to mount encrypted datasets with
1268 if (zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS) ==
1269 ZFS_KEYSTATUS_UNAVAILABLE)
1272 if (zfs_mount(zhp, ms->ms_mntopts, ms->ms_mntflags) != 0)
1273 ret = ms->ms_mntstatus = -1;
1278 zfs_share_one(zfs_handle_t *zhp, void *arg)
1280 mount_state_t *ms = arg;
1283 if (zfs_share(zhp) != 0)
1284 ret = ms->ms_mntstatus = -1;
1289 * Thread pool function to mount one file system. On completion, it finds and
1290 * schedules its children to be mounted. This depends on the sorting done in
1291 * zfs_foreach_mountpoint(). Note that the degenerate case (chain of entries
1292 * each descending from the previous) will have no parallelism since we always
1293 * have to wait for the parent to finish mounting before we can schedule
1297 zfs_mount_task(void *arg)
1299 mnt_param_t *mp = arg;
1300 int idx = mp->mnt_idx;
1301 zfs_handle_t **handles = mp->mnt_zhps;
1302 size_t num_handles = mp->mnt_num_handles;
1303 char mountpoint[ZFS_MAXPROPLEN];
1305 verify(zfs_prop_get(handles[idx], ZFS_PROP_MOUNTPOINT, mountpoint,
1306 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
1308 if (mp->mnt_func(handles[idx], mp->mnt_data) != 0)
1312 * We dispatch tasks to mount filesystems with mountpoints underneath
1313 * this one. We do this by dispatching the next filesystem with a
1314 * descendant mountpoint of the one we just mounted, then skip all of
1315 * its descendants, dispatch the next descendant mountpoint, and so on.
1316 * The non_descendant_idx() function skips over filesystems that are
1317 * descendants of the filesystem we just dispatched.
1319 for (int i = idx + 1; i < num_handles;
1320 i = non_descendant_idx(handles, num_handles, i)) {
1321 char child[ZFS_MAXPROPLEN];
1322 verify(zfs_prop_get(handles[i], ZFS_PROP_MOUNTPOINT,
1323 child, sizeof (child), NULL, NULL, 0, B_FALSE) == 0);
1325 if (!libzfs_path_contains(mountpoint, child))
1326 break; /* not a descendant, return */
1327 zfs_dispatch_mount(mp->mnt_hdl, handles, num_handles, i,
1328 mp->mnt_func, mp->mnt_data, mp->mnt_tp);
1334 * Issue the func callback for each ZFS handle contained in the handles
1335 * array. This function is used to mount all datasets, and so this function
1336 * guarantees that filesystems for parent mountpoints are called before their
1337 * children. As such, before issuing any callbacks, we first sort the array
1338 * of handles by mountpoint.
1340 * Callbacks are issued in one of two ways:
1342 * 1. Sequentially: If the parallel argument is B_FALSE or the ZFS_SERIAL_MOUNT
1343 * environment variable is set, then we issue callbacks sequentially.
1345 * 2. In parallel: If the parallel argument is B_TRUE and the ZFS_SERIAL_MOUNT
1346 * environment variable is not set, then we use a tpool to dispatch threads
1347 * to mount filesystems in parallel. This function dispatches tasks to mount
1348 * the filesystems at the top-level mountpoints, and these tasks in turn
1349 * are responsible for recursively mounting filesystems in their children
1353 zfs_foreach_mountpoint(libzfs_handle_t *hdl, zfs_handle_t **handles,
1354 size_t num_handles, zfs_iter_f func, void *data, boolean_t parallel)
1356 zoneid_t zoneid = getzoneid();
1359 * The ZFS_SERIAL_MOUNT environment variable is an undocumented
1360 * variable that can be used as a convenience to do a/b comparison
1361 * of serial vs. parallel mounting.
1363 boolean_t serial_mount = !parallel ||
1364 (getenv("ZFS_SERIAL_MOUNT") != NULL);
1367 * Sort the datasets by mountpoint. See mountpoint_cmp for details
1368 * of how these are sorted.
1370 qsort(handles, num_handles, sizeof (zfs_handle_t *), mountpoint_cmp);
1373 for (int i = 0; i < num_handles; i++) {
1374 func(handles[i], data);
1380 * Issue the callback function for each dataset using a parallel
1381 * algorithm that uses a thread pool to manage threads.
1383 tpool_t *tp = tpool_create(1, mount_tp_nthr, 0, NULL);
1386 * There may be multiple "top level" mountpoints outside of the pool's
1387 * root mountpoint, e.g.: /foo /bar. Dispatch a mount task for each of
1390 for (int i = 0; i < num_handles;
1391 i = non_descendant_idx(handles, num_handles, i)) {
1393 * Since the mountpoints have been sorted so that the zoned
1394 * filesystems are at the end, a zoned filesystem seen from
1395 * the global zone means that we're done.
1397 if (zoneid == GLOBAL_ZONEID &&
1398 zfs_prop_get_int(handles[i], ZFS_PROP_ZONED))
1400 zfs_dispatch_mount(hdl, handles, num_handles, i, func, data,
1404 tpool_wait(tp); /* wait for all scheduled mounts to complete */
1409 * Mount and share all datasets within the given pool. This assumes that no
1410 * datasets within the pool are currently mounted.
1412 #pragma weak zpool_mount_datasets = zpool_enable_datasets
1414 zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
1416 get_all_cb_t cb = { 0 };
1417 mount_state_t ms = { 0 };
1421 if ((zfsp = zfs_open(zhp->zpool_hdl, zhp->zpool_name,
1422 ZFS_TYPE_DATASET)) == NULL)
1426 * Gather all non-snapshot datasets within the pool. Start by adding
1427 * the root filesystem for this pool to the list, and then iterate
1428 * over all child filesystems.
1430 libzfs_add_handle(&cb, zfsp);
1431 if (zfs_iter_filesystems(zfsp, zfs_iter_cb, &cb) != 0)
1435 * Mount all filesystems
1437 ms.ms_mntopts = mntopts;
1438 ms.ms_mntflags = flags;
1439 zfs_foreach_mountpoint(zhp->zpool_hdl, cb.cb_handles, cb.cb_used,
1440 zfs_mount_one, &ms, B_TRUE);
1441 if (ms.ms_mntstatus != 0)
1442 ret = ms.ms_mntstatus;
1445 * Share all filesystems that need to be shared. This needs to be
1446 * a separate pass because libshare is not mt-safe, and so we need
1447 * to share serially.
1449 ms.ms_mntstatus = 0;
1450 zfs_foreach_mountpoint(zhp->zpool_hdl, cb.cb_handles, cb.cb_used,
1451 zfs_share_one, &ms, B_FALSE);
1452 if (ms.ms_mntstatus != 0)
1453 ret = ms.ms_mntstatus;
1455 zfs_commit_all_shares();
1458 for (int i = 0; i < cb.cb_used; i++)
1459 zfs_close(cb.cb_handles[i]);
1460 free(cb.cb_handles);
1466 mountpoint_compare(const void *a, const void *b)
1468 const char *mounta = *((char **)a);
1469 const char *mountb = *((char **)b);
1471 return (strcmp(mountb, mounta));
1474 /* alias for 2002/240 */
1475 #pragma weak zpool_unmount_datasets = zpool_disable_datasets
1477 * Unshare and unmount all datasets within the given pool. We don't want to
1478 * rely on traversing the DSL to discover the filesystems within the pool,
1479 * because this may be expensive (if not all of them are mounted), and can fail
1480 * arbitrarily (on I/O error, for example). Instead, we walk /proc/self/mounts
1481 * and gather all the filesystems that are currently mounted.
1484 zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
1487 struct mnttab entry;
1489 char **mountpoints = NULL;
1490 zfs_handle_t **datasets = NULL;
1491 libzfs_handle_t *hdl = zhp->zpool_hdl;
1494 int flags = (force ? MS_FORCE : 0);
1496 namelen = strlen(zhp->zpool_name);
1498 /* Reopen MNTTAB to prevent reading stale data from open file */
1499 if (freopen(MNTTAB, "r", hdl->libzfs_mnttab) == NULL)
1503 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
1505 * Ignore non-ZFS entries.
1507 if (entry.mnt_fstype == NULL ||
1508 strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
1512 * Ignore filesystems not within this pool.
1514 if (entry.mnt_mountp == NULL ||
1515 strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 ||
1516 (entry.mnt_special[namelen] != '/' &&
1517 entry.mnt_special[namelen] != '\0'))
1521 * At this point we've found a filesystem within our pool. Add
1522 * it to our growing list.
1524 if (used == alloc) {
1526 if ((mountpoints = zfs_alloc(hdl,
1527 8 * sizeof (void *))) == NULL)
1530 if ((datasets = zfs_alloc(hdl,
1531 8 * sizeof (void *))) == NULL)
1538 if ((ptr = zfs_realloc(hdl, mountpoints,
1539 alloc * sizeof (void *),
1540 alloc * 2 * sizeof (void *))) == NULL)
1544 if ((ptr = zfs_realloc(hdl, datasets,
1545 alloc * sizeof (void *),
1546 alloc * 2 * sizeof (void *))) == NULL)
1554 if ((mountpoints[used] = zfs_strdup(hdl,
1555 entry.mnt_mountp)) == NULL)
1559 * This is allowed to fail, in case there is some I/O error. It
1560 * is only used to determine if we need to remove the underlying
1561 * mountpoint, so failure is not fatal.
1563 datasets[used] = make_dataset_handle(hdl, entry.mnt_special);
1569 * At this point, we have the entire list of filesystems, so sort it by
1572 qsort(mountpoints, used, sizeof (char *), mountpoint_compare);
1575 * Walk through and first unshare everything.
1577 for (i = 0; i < used; i++) {
1578 zfs_share_proto_t *curr_proto;
1579 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
1581 if (is_shared(mountpoints[i], *curr_proto) &&
1582 unshare_one(hdl, mountpoints[i],
1583 mountpoints[i], *curr_proto) != 0)
1587 zfs_commit_all_shares();
1590 * Now unmount everything, removing the underlying directories as
1593 for (i = 0; i < used; i++) {
1594 if (unmount_one(hdl, mountpoints[i], flags) != 0)
1598 for (i = 0; i < used; i++) {
1600 remove_mountpoint(datasets[i]);
1605 for (i = 0; i < used; i++) {
1607 zfs_close(datasets[i]);
1608 free(mountpoints[i]);