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, 2016 by Delphix. All rights reserved.
26 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
27 * Copyright 2017 Joyent, Inc.
28 * Copyright 2017 RackTop Systems.
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
43 * This file also contains the functions used to manage sharing filesystems via
56 * zfs_unshareall_nfs()
57 * zfs_unshareall_smb()
59 * zfs_unshareall_bypath()
61 * The following functions are available for pool consumers, and will
62 * mount/unmount and share/unshare all datasets within pool:
64 * zpool_enable_datasets()
65 * zpool_disable_datasets()
79 #include <sys/mntent.h>
80 #include <sys/mount.h>
82 #include <sys/statvfs.h>
86 #include "libzfs_impl.h"
87 #include <thread_pool.h>
90 #define MAXISALEN 257 /* based on sysinfo(2) man page */
92 static int mount_tp_nthr = 512; /* tpool threads for multi-threaded mounting */
94 static void zfs_mount_task(void *);
95 static int zfs_share_proto(zfs_handle_t *, zfs_share_proto_t *);
96 zfs_share_type_t zfs_is_shared_proto(zfs_handle_t *, char **,
100 * The share protocols table must be in the same order as the zfs_share_proto_t
101 * enum in libzfs_impl.h
110 proto_table_t proto_table[PROTO_END] = {
111 {ZFS_PROP_SHARENFS, "nfs", EZFS_SHARENFSFAILED, EZFS_UNSHARENFSFAILED},
112 {ZFS_PROP_SHARESMB, "smb", EZFS_SHARESMBFAILED, EZFS_UNSHARESMBFAILED},
115 zfs_share_proto_t nfs_only[] = {
120 zfs_share_proto_t smb_only[] = {
124 zfs_share_proto_t share_all_proto[] = {
131 * Search the sharetab for the given mountpoint and protocol, returning
132 * a zfs_share_type_t value.
134 static zfs_share_type_t
135 is_shared(libzfs_handle_t *hdl, const char *mountpoint, zfs_share_proto_t proto)
137 char buf[MAXPATHLEN], *tab;
140 if (hdl->libzfs_sharetab == NULL)
141 return (SHARED_NOT_SHARED);
143 (void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET);
145 while (fgets(buf, sizeof (buf), hdl->libzfs_sharetab) != NULL) {
147 /* the mountpoint is the first entry on each line */
148 if ((tab = strchr(buf, '\t')) == NULL)
152 if (strcmp(buf, mountpoint) == 0) {
155 * the protocol field is the third field
156 * skip over second field
159 if ((tab = strchr(ptr, '\t')) == NULL)
162 if ((tab = strchr(ptr, '\t')) == NULL)
166 proto_table[proto].p_name) == 0) {
177 if (proto == PROTO_NFS)
183 return (SHARED_NOT_SHARED);
188 dir_is_empty_stat(const char *dirname)
193 * We only want to return false if the given path is a non empty
194 * directory, all other errors are handled elsewhere.
196 if (stat(dirname, &st) < 0 || !S_ISDIR(st.st_mode)) {
201 * An empty directory will still have two entries in it, one
202 * entry for each of "." and "..".
204 if (st.st_size > 2) {
212 dir_is_empty_readdir(const char *dirname)
218 if ((dirfd = openat(AT_FDCWD, dirname,
219 O_RDONLY | O_NDELAY | O_LARGEFILE | O_CLOEXEC, 0)) < 0) {
223 if ((dirp = fdopendir(dirfd)) == NULL) {
228 while ((dp = readdir64(dirp)) != NULL) {
230 if (strcmp(dp->d_name, ".") == 0 ||
231 strcmp(dp->d_name, "..") == 0)
234 (void) closedir(dirp);
238 (void) closedir(dirp);
243 * Returns true if the specified directory is empty. If we can't open the
244 * directory at all, return true so that the mount can fail with a more
245 * informative error message.
248 dir_is_empty(const char *dirname)
253 * If the statvfs call fails or the filesystem is not a ZFS
254 * filesystem, fall back to the slow path which uses readdir.
256 if ((statvfs64(dirname, &st) != 0) ||
257 (strcmp(st.f_basetype, "zfs") != 0)) {
258 return (dir_is_empty_readdir(dirname));
262 * At this point, we know the provided path is on a ZFS
263 * filesystem, so we can use stat instead of readdir to
264 * determine if the directory is empty or not. We try to avoid
265 * using readdir because that requires opening "dirname"; this
266 * open file descriptor can potentially end up in a child
267 * process if there's a concurrent fork, thus preventing the
268 * zfs_mount() from otherwise succeeding (the open file
269 * descriptor inherited by the child process will cause the
270 * parent's mount to fail with EBUSY). The performance
271 * implications of replacing the open, read, and close with a
272 * single stat is nice; but is not the main motivation for the
275 return (dir_is_empty_stat(dirname));
280 * Checks to see if the mount is active. If the filesystem is mounted, we fill
281 * in 'where' with the current mountpoint, and return 1. Otherwise, we return
285 is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where)
289 if (libzfs_mnttab_find(zfs_hdl, special, &entry) != 0)
293 *where = zfs_strdup(zfs_hdl, entry.mnt_mountp);
299 zfs_is_mounted(zfs_handle_t *zhp, char **where)
301 return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where));
305 zfs_is_mountable_internal(zfs_handle_t *zhp, const char *mountpoint)
308 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
309 getzoneid() == GLOBAL_ZONEID)
316 * Returns true if the given dataset is mountable, false otherwise. Returns the
317 * mountpoint in 'buf'.
320 zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen,
321 zprop_source_t *source)
323 char sourceloc[MAXNAMELEN];
324 zprop_source_t sourcetype;
326 if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type))
329 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen,
330 &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0);
332 if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 ||
333 strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0)
336 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF)
339 if (!zfs_is_mountable_internal(zhp, buf))
343 *source = sourcetype;
349 * Mount the given filesystem.
352 zfs_mount(zfs_handle_t *zhp, const char *options, int flags)
354 char mountpoint[ZFS_MAXPROPLEN];
356 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
359 return (zfs_mount_at(zhp, options, flags, mountpoint));
363 zfs_mount_at(zfs_handle_t *zhp, const char *options, int flags,
364 const char *mountpoint)
367 char mntopts[MNT_LINE_MAX];
368 libzfs_handle_t *hdl = zhp->zfs_hdl;
373 (void) strlcpy(mntopts, options, sizeof (mntopts));
376 * If the pool is imported read-only then all mounts must be read-only
378 if (zpool_get_prop_int(zhp->zpool_hdl, ZPOOL_PROP_READONLY, NULL))
381 if (!zfs_is_mountable_internal(zhp, mountpoint))
384 /* Create the directory if it doesn't already exist */
385 if (lstat(mountpoint, &buf) != 0) {
386 if (mkdirp(mountpoint, 0755) != 0) {
387 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
388 "failed to create mountpoint"));
389 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
390 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
395 #ifdef illumos /* FreeBSD: overlay mounts are not checked. */
397 * Determine if the mountpoint is empty. If so, refuse to perform the
398 * mount. We don't perform this check if MS_OVERLAY is specified, which
399 * would defeat the point. We also avoid this check if 'remount' is
402 if ((flags & MS_OVERLAY) == 0 &&
403 strstr(mntopts, MNTOPT_REMOUNT) == NULL &&
404 !dir_is_empty(mountpoint)) {
405 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
406 "directory is not empty"));
407 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
408 dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint));
412 /* perform the mount */
413 if (zmount(zfs_get_name(zhp), mountpoint, flags,
414 MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) {
416 * Generic errors are nasty, but there are just way too many
417 * from mount(), and they're well-understood. We pick a few
418 * common ones to improve upon.
420 if (errno == EBUSY) {
421 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
422 "mountpoint or dataset is busy"));
423 } else if (errno == EPERM) {
424 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
425 "Insufficient privileges"));
426 } else if (errno == ENOTSUP) {
430 VERIFY(zfs_spa_version(zhp, &spa_version) == 0);
431 (void) snprintf(buf, sizeof (buf),
432 dgettext(TEXT_DOMAIN, "Can't mount a version %lld "
433 "file system on a version %d pool. Pool must be"
434 " upgraded to mount this file system."),
435 (u_longlong_t)zfs_prop_get_int(zhp,
436 ZFS_PROP_VERSION), spa_version);
437 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, buf));
439 zfs_error_aux(hdl, strerror(errno));
441 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
442 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
446 /* add the mounted entry into our cache */
447 libzfs_mnttab_add(hdl, zfs_get_name(zhp), mountpoint,
453 * Unmount a single filesystem.
456 unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags)
458 if (umount2(mountpoint, flags) != 0) {
459 zfs_error_aux(hdl, strerror(errno));
460 return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED,
461 dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
469 * Unmount the given filesystem.
472 zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
474 libzfs_handle_t *hdl = zhp->zfs_hdl;
478 /* check to see if we need to unmount the filesystem */
479 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
480 libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)) {
482 * mountpoint may have come from a call to
483 * getmnt/getmntany if it isn't NULL. If it is NULL,
484 * we know it comes from libzfs_mnttab_find which can
485 * then get freed later. We strdup it to play it safe.
487 if (mountpoint == NULL)
488 mntpt = zfs_strdup(hdl, entry.mnt_mountp);
490 mntpt = zfs_strdup(hdl, mountpoint);
493 * Unshare and unmount the filesystem
495 if (zfs_unshare_proto(zhp, mntpt, share_all_proto) != 0)
498 if (unmount_one(hdl, mntpt, flags) != 0) {
500 (void) zfs_shareall(zhp);
503 libzfs_mnttab_remove(hdl, zhp->zfs_name);
511 * Unmount this filesystem and any children inheriting the mountpoint property.
512 * To do this, just act like we're changing the mountpoint property, but don't
513 * remount the filesystems afterwards.
516 zfs_unmountall(zfs_handle_t *zhp, int flags)
518 prop_changelist_t *clp;
521 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, 0, flags);
525 ret = changelist_prefix(clp);
526 changelist_free(clp);
532 zfs_is_shared(zfs_handle_t *zhp)
534 zfs_share_type_t rc = 0;
535 zfs_share_proto_t *curr_proto;
537 if (ZFS_IS_VOLUME(zhp))
540 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
542 rc |= zfs_is_shared_proto(zhp, NULL, *curr_proto);
544 return (rc ? B_TRUE : B_FALSE);
548 zfs_share(zfs_handle_t *zhp)
550 assert(!ZFS_IS_VOLUME(zhp));
551 return (zfs_share_proto(zhp, share_all_proto));
555 zfs_unshare(zfs_handle_t *zhp)
557 assert(!ZFS_IS_VOLUME(zhp));
558 return (zfs_unshareall(zhp));
562 * Check to see if the filesystem is currently shared.
565 zfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto)
570 if (!zfs_is_mounted(zhp, &mountpoint))
571 return (SHARED_NOT_SHARED);
573 if ((rc = is_shared(zhp->zfs_hdl, mountpoint, proto))
574 != SHARED_NOT_SHARED) {
582 return (SHARED_NOT_SHARED);
587 zfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
589 return (zfs_is_shared_proto(zhp, where,
590 PROTO_NFS) != SHARED_NOT_SHARED);
594 zfs_is_shared_smb(zfs_handle_t *zhp, char **where)
596 return (zfs_is_shared_proto(zhp, where,
597 PROTO_SMB) != SHARED_NOT_SHARED);
601 * Make sure things will work if libshare isn't installed by using
602 * wrapper functions that check to see that the pointers to functions
603 * initialized in _zfs_init_libshare() are actually present.
607 static sa_handle_t (*_sa_init)(int);
608 static sa_handle_t (*_sa_init_arg)(int, void *);
609 static void (*_sa_fini)(sa_handle_t);
610 static sa_share_t (*_sa_find_share)(sa_handle_t, char *);
611 static int (*_sa_enable_share)(sa_share_t, char *);
612 static int (*_sa_disable_share)(sa_share_t, char *);
613 static char *(*_sa_errorstr)(int);
614 static int (*_sa_parse_legacy_options)(sa_group_t, char *, char *);
615 static boolean_t (*_sa_needs_refresh)(sa_handle_t *);
616 static libzfs_handle_t *(*_sa_get_zfs_handle)(sa_handle_t);
617 static int (*_sa_zfs_process_share)(sa_handle_t, sa_group_t, sa_share_t,
618 char *, char *, zprop_source_t, char *, char *, char *);
619 static void (*_sa_update_sharetab_ts)(sa_handle_t);
623 * _zfs_init_libshare()
625 * Find the libshare.so.1 entry points that we use here and save the
626 * values to be used later. This is triggered by the runtime loader.
627 * Make sure the correct ISA version is loaded.
630 #pragma init(_zfs_init_libshare)
632 _zfs_init_libshare(void)
636 char path[MAXPATHLEN];
640 if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1)
645 (void) snprintf(path, MAXPATHLEN,
646 "/usr/lib/%s/libshare.so.1", isa);
648 if ((libshare = dlopen(path, RTLD_LAZY | RTLD_GLOBAL)) != NULL) {
649 _sa_init = (sa_handle_t (*)(int))dlsym(libshare, "sa_init");
650 _sa_init_arg = (sa_handle_t (*)(int, void *))dlsym(libshare,
652 _sa_fini = (void (*)(sa_handle_t))dlsym(libshare, "sa_fini");
653 _sa_find_share = (sa_share_t (*)(sa_handle_t, char *))
654 dlsym(libshare, "sa_find_share");
655 _sa_enable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
657 _sa_disable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
659 _sa_errorstr = (char *(*)(int))dlsym(libshare, "sa_errorstr");
660 _sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *))
661 dlsym(libshare, "sa_parse_legacy_options");
662 _sa_needs_refresh = (boolean_t (*)(sa_handle_t *))
663 dlsym(libshare, "sa_needs_refresh");
664 _sa_get_zfs_handle = (libzfs_handle_t *(*)(sa_handle_t))
665 dlsym(libshare, "sa_get_zfs_handle");
666 _sa_zfs_process_share = (int (*)(sa_handle_t, sa_group_t,
667 sa_share_t, char *, char *, zprop_source_t, char *,
668 char *, char *))dlsym(libshare, "sa_zfs_process_share");
669 _sa_update_sharetab_ts = (void (*)(sa_handle_t))
670 dlsym(libshare, "sa_update_sharetab_ts");
671 if (_sa_init == NULL || _sa_init_arg == NULL ||
672 _sa_fini == NULL || _sa_find_share == NULL ||
673 _sa_enable_share == NULL || _sa_disable_share == NULL ||
674 _sa_errorstr == NULL || _sa_parse_legacy_options == NULL ||
675 _sa_needs_refresh == NULL || _sa_get_zfs_handle == NULL ||
676 _sa_zfs_process_share == NULL ||
677 _sa_update_sharetab_ts == NULL) {
681 _sa_disable_share = NULL;
682 _sa_enable_share = NULL;
684 _sa_parse_legacy_options = NULL;
685 (void) dlclose(libshare);
686 _sa_needs_refresh = NULL;
687 _sa_get_zfs_handle = NULL;
688 _sa_zfs_process_share = NULL;
689 _sa_update_sharetab_ts = NULL;
696 * zfs_init_libshare(zhandle, service)
698 * Initialize the libshare API if it hasn't already been initialized.
699 * In all cases it returns 0 if it succeeded and an error if not. The
700 * service value is which part(s) of the API to initialize and is a
701 * direct map to the libshare sa_init(service) interface.
704 zfs_init_libshare_impl(libzfs_handle_t *zhandle, int service, void *arg)
708 * libshare is either not installed or we're in a branded zone. The
709 * rest of the wrapper functions around the libshare calls already
710 * handle NULL function pointers, but we don't want the callers of
711 * zfs_init_libshare() to fail prematurely if libshare is not available.
713 if (_sa_init == NULL)
717 * Attempt to refresh libshare. This is necessary if there was a cache
718 * miss for a new ZFS dataset that was just created, or if state of the
719 * sharetab file has changed since libshare was last initialized. We
720 * want to make sure so check timestamps to see if a different process
721 * has updated any of the configuration. If there was some non-ZFS
722 * change, we need to re-initialize the internal cache.
724 if (_sa_needs_refresh != NULL &&
725 _sa_needs_refresh(zhandle->libzfs_sharehdl)) {
726 zfs_uninit_libshare(zhandle);
727 zhandle->libzfs_sharehdl = _sa_init_arg(service, arg);
730 if (zhandle && zhandle->libzfs_sharehdl == NULL)
731 zhandle->libzfs_sharehdl = _sa_init_arg(service, arg);
733 if (zhandle->libzfs_sharehdl == NULL)
734 return (SA_NO_MEMORY);
740 zfs_init_libshare(libzfs_handle_t *zhandle, int service)
742 return (zfs_init_libshare_impl(zhandle, service, NULL));
746 zfs_init_libshare_arg(libzfs_handle_t *zhandle, int service, void *arg)
748 return (zfs_init_libshare_impl(zhandle, service, arg));
753 * zfs_uninit_libshare(zhandle)
755 * Uninitialize the libshare API if it hasn't already been
756 * uninitialized. It is OK to call multiple times.
759 zfs_uninit_libshare(libzfs_handle_t *zhandle)
761 if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) {
763 if (_sa_fini != NULL)
764 _sa_fini(zhandle->libzfs_sharehdl);
766 zhandle->libzfs_sharehdl = NULL;
771 * zfs_parse_options(options, proto)
773 * Call the legacy parse interface to get the protocol specific
774 * options using the NULL arg to indicate that this is a "parse" only.
777 zfs_parse_options(char *options, zfs_share_proto_t proto)
780 if (_sa_parse_legacy_options != NULL) {
781 return (_sa_parse_legacy_options(NULL, options,
782 proto_table[proto].p_name));
784 return (SA_CONFIG_ERR);
792 * zfs_sa_find_share(handle, path)
794 * wrapper around sa_find_share to find a share path in the
798 zfs_sa_find_share(sa_handle_t handle, char *path)
800 if (_sa_find_share != NULL)
801 return (_sa_find_share(handle, path));
806 * zfs_sa_enable_share(share, proto)
808 * Wrapper for sa_enable_share which enables a share for a specified
812 zfs_sa_enable_share(sa_share_t share, char *proto)
814 if (_sa_enable_share != NULL)
815 return (_sa_enable_share(share, proto));
816 return (SA_CONFIG_ERR);
820 * zfs_sa_disable_share(share, proto)
822 * Wrapper for sa_enable_share which disables a share for a specified
826 zfs_sa_disable_share(sa_share_t share, char *proto)
828 if (_sa_disable_share != NULL)
829 return (_sa_disable_share(share, proto));
830 return (SA_CONFIG_ERR);
835 * Share the given filesystem according to the options in the specified
836 * protocol specific properties (sharenfs, sharesmb). We rely
837 * on "libshare" to the dirty work for us.
840 zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
842 char mountpoint[ZFS_MAXPROPLEN];
843 char shareopts[ZFS_MAXPROPLEN];
844 char sourcestr[ZFS_MAXPROPLEN];
845 libzfs_handle_t *hdl = zhp->zfs_hdl;
846 zfs_share_proto_t *curr_proto;
847 zprop_source_t sourcetype;
850 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
853 for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) {
855 * Return success if there are no share options.
857 if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop,
858 shareopts, sizeof (shareopts), &sourcetype, sourcestr,
859 ZFS_MAXPROPLEN, B_FALSE) != 0 ||
860 strcmp(shareopts, "off") == 0)
863 ret = zfs_init_libshare_arg(hdl, SA_INIT_ONE_SHARE_FROM_HANDLE,
866 (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
867 dgettext(TEXT_DOMAIN, "cannot share '%s': %s"),
868 zfs_get_name(zhp), _sa_errorstr != NULL ?
869 _sa_errorstr(ret) : "");
875 * If the 'zoned' property is set, then zfs_is_mountable()
876 * will have already bailed out if we are in the global zone.
877 * But local zones cannot be NFS servers, so we ignore it for
878 * local zones as well.
880 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
884 share = zfs_sa_find_share(hdl->libzfs_sharehdl, mountpoint);
887 * This may be a new file system that was just
888 * created so isn't in the internal cache
889 * (second time through). Rather than
890 * reloading the entire configuration, we can
891 * assume ZFS has done the checking and it is
892 * safe to add this to the internal
895 if (_sa_zfs_process_share(hdl->libzfs_sharehdl,
896 NULL, NULL, mountpoint,
897 proto_table[*curr_proto].p_name, sourcetype,
898 shareopts, sourcestr, zhp->zfs_name) != SA_OK) {
899 (void) zfs_error_fmt(hdl,
900 proto_table[*curr_proto].p_share_err,
901 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
905 share = zfs_sa_find_share(hdl->libzfs_sharehdl,
910 err = zfs_sa_enable_share(share,
911 proto_table[*curr_proto].p_name);
913 (void) zfs_error_fmt(hdl,
914 proto_table[*curr_proto].p_share_err,
915 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
921 if (*curr_proto != PROTO_NFS) {
922 fprintf(stderr, "Unsupported share protocol: %d.\n",
927 if (strcmp(shareopts, "on") == 0)
928 error = fsshare(ZFS_EXPORTS_PATH, mountpoint, "");
930 error = fsshare(ZFS_EXPORTS_PATH, mountpoint, shareopts);
934 (void) zfs_error_fmt(hdl,
935 proto_table[*curr_proto].p_share_err,
936 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
947 zfs_share_nfs(zfs_handle_t *zhp)
949 return (zfs_share_proto(zhp, nfs_only));
953 zfs_share_smb(zfs_handle_t *zhp)
955 return (zfs_share_proto(zhp, smb_only));
959 zfs_shareall(zfs_handle_t *zhp)
961 return (zfs_share_proto(zhp, share_all_proto));
965 * Unshare a filesystem by mountpoint.
968 unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint,
969 zfs_share_proto_t proto)
977 * Mountpoint could get trashed if libshare calls getmntany
978 * which it does during API initialization, so strdup the
981 mntpt = zfs_strdup(hdl, mountpoint);
984 * make sure libshare initialized, initialize everything because we
985 * don't know what other unsharing may happen later. Functions up the
986 * stack are allowed to initialize instead a subset of shares at the
987 * time the set is known.
989 if ((err = zfs_init_libshare_arg(hdl, SA_INIT_ONE_SHARE_FROM_NAME,
990 (void *)name)) != SA_OK) {
991 free(mntpt); /* don't need the copy anymore */
992 return (zfs_error_fmt(hdl, proto_table[proto].p_unshare_err,
993 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
994 name, _sa_errorstr(err)));
997 share = zfs_sa_find_share(hdl->libzfs_sharehdl, mntpt);
998 free(mntpt); /* don't need the copy anymore */
1000 if (share != NULL) {
1001 err = zfs_sa_disable_share(share, proto_table[proto].p_name);
1003 return (zfs_error_fmt(hdl,
1004 proto_table[proto].p_unshare_err,
1005 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
1006 name, _sa_errorstr(err)));
1009 return (zfs_error_fmt(hdl, proto_table[proto].p_unshare_err,
1010 dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"),
1014 char buf[MAXPATHLEN];
1018 if (proto != PROTO_NFS) {
1019 fprintf(stderr, "No SMB support in FreeBSD yet.\n");
1020 return (EOPNOTSUPP);
1023 err = fsunshare(ZFS_EXPORTS_PATH, mountpoint);
1025 zfs_error_aux(hdl, "%s", strerror(err));
1026 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
1027 dgettext(TEXT_DOMAIN,
1028 "cannot unshare '%s'"), name));
1035 * Unshare the given filesystem.
1038 zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint,
1039 zfs_share_proto_t *proto)
1041 libzfs_handle_t *hdl = zhp->zfs_hdl;
1042 struct mnttab entry;
1045 /* check to see if need to unmount the filesystem */
1046 rewind(zhp->zfs_hdl->libzfs_mnttab);
1047 if (mountpoint != NULL)
1048 mountpoint = mntpt = zfs_strdup(hdl, mountpoint);
1050 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
1051 libzfs_mnttab_find(hdl, zfs_get_name(zhp), &entry) == 0)) {
1052 zfs_share_proto_t *curr_proto;
1054 if (mountpoint == NULL)
1055 mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp);
1057 for (curr_proto = proto; *curr_proto != PROTO_END;
1060 if (is_shared(hdl, mntpt, *curr_proto) &&
1061 unshare_one(hdl, zhp->zfs_name,
1062 mntpt, *curr_proto) != 0) {
1076 zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
1078 return (zfs_unshare_proto(zhp, mountpoint, nfs_only));
1082 zfs_unshare_smb(zfs_handle_t *zhp, const char *mountpoint)
1084 return (zfs_unshare_proto(zhp, mountpoint, smb_only));
1088 * Same as zfs_unmountall(), but for NFS and SMB unshares.
1091 zfs_unshareall_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
1093 prop_changelist_t *clp;
1096 clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0, 0);
1100 ret = changelist_unshare(clp, proto);
1101 changelist_free(clp);
1107 zfs_unshareall_nfs(zfs_handle_t *zhp)
1109 return (zfs_unshareall_proto(zhp, nfs_only));
1113 zfs_unshareall_smb(zfs_handle_t *zhp)
1115 return (zfs_unshareall_proto(zhp, smb_only));
1119 zfs_unshareall(zfs_handle_t *zhp)
1121 return (zfs_unshareall_proto(zhp, share_all_proto));
1125 zfs_unshareall_bypath(zfs_handle_t *zhp, const char *mountpoint)
1127 return (zfs_unshare_proto(zhp, mountpoint, share_all_proto));
1131 * Remove the mountpoint associated with the current dataset, if necessary.
1132 * We only remove the underlying directory if:
1134 * - The mountpoint is not 'none' or 'legacy'
1135 * - The mountpoint is non-empty
1136 * - The mountpoint is the default or inherited
1137 * - The 'zoned' property is set, or we're in a local zone
1139 * Any other directories we leave alone.
1142 remove_mountpoint(zfs_handle_t *zhp)
1144 char mountpoint[ZFS_MAXPROPLEN];
1145 zprop_source_t source;
1147 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
1151 if (source == ZPROP_SRC_DEFAULT ||
1152 source == ZPROP_SRC_INHERITED) {
1154 * Try to remove the directory, silently ignoring any errors.
1155 * The filesystem may have since been removed or moved around,
1156 * and this error isn't really useful to the administrator in
1159 (void) rmdir(mountpoint);
1164 * Add the given zfs handle to the cb_handles array, dynamically reallocating
1165 * the array if it is out of space
1168 libzfs_add_handle(get_all_cb_t *cbp, zfs_handle_t *zhp)
1170 if (cbp->cb_alloc == cbp->cb_used) {
1172 zfs_handle_t **newhandles;
1174 newsz = cbp->cb_alloc != 0 ? cbp->cb_alloc * 2 : 64;
1175 newhandles = zfs_realloc(zhp->zfs_hdl,
1176 cbp->cb_handles, cbp->cb_alloc * sizeof (zfs_handle_t *),
1177 newsz * sizeof (zfs_handle_t *));
1178 cbp->cb_handles = newhandles;
1179 cbp->cb_alloc = newsz;
1181 cbp->cb_handles[cbp->cb_used++] = zhp;
1185 * Recursive helper function used during file system enumeration
1188 zfs_iter_cb(zfs_handle_t *zhp, void *data)
1190 get_all_cb_t *cbp = data;
1192 if (!(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM)) {
1197 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_NOAUTO) {
1203 * If this filesystem is inconsistent and has a receive resume
1204 * token, we can not mount it.
1206 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
1207 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
1208 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
1213 libzfs_add_handle(cbp, zhp);
1214 if (zfs_iter_filesystems(zhp, zfs_iter_cb, cbp) != 0) {
1222 * Sort comparator that compares two mountpoint paths. We sort these paths so
1223 * that subdirectories immediately follow their parents. This means that we
1224 * effectively treat the '/' character as the lowest value non-nul char.
1225 * Since filesystems from non-global zones can have the same mountpoint
1226 * as other filesystems, the comparator sorts global zone filesystems to
1227 * the top of the list. This means that the global zone will traverse the
1228 * filesystem list in the correct order and can stop when it sees the
1229 * first zoned filesystem. In a non-global zone, only the delegated
1230 * filesystems are seen.
1232 * An example sorted list using this comparator would look like:
1242 * The mount code depend on this ordering to deterministically iterate
1243 * over filesystems in order to spawn parallel mount tasks.
1246 mountpoint_cmp(const void *arga, const void *argb)
1248 zfs_handle_t *const *zap = arga;
1249 zfs_handle_t *za = *zap;
1250 zfs_handle_t *const *zbp = argb;
1251 zfs_handle_t *zb = *zbp;
1252 char mounta[MAXPATHLEN];
1253 char mountb[MAXPATHLEN];
1254 const char *a = mounta;
1255 const char *b = mountb;
1256 boolean_t gota, gotb;
1257 uint64_t zoneda, zonedb;
1259 zoneda = zfs_prop_get_int(za, ZFS_PROP_ZONED);
1260 zonedb = zfs_prop_get_int(zb, ZFS_PROP_ZONED);
1261 if (zoneda && !zonedb)
1263 if (!zoneda && zonedb)
1265 gota = (zfs_get_type(za) == ZFS_TYPE_FILESYSTEM);
1267 verify(zfs_prop_get(za, ZFS_PROP_MOUNTPOINT, mounta,
1268 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
1269 gotb = (zfs_get_type(zb) == ZFS_TYPE_FILESYSTEM);
1271 verify(zfs_prop_get(zb, ZFS_PROP_MOUNTPOINT, mountb,
1272 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
1275 while (*a != '\0' && (*a == *b)) {
1289 return (*a < *b ? -1 : *a > *b);
1298 * If neither filesystem has a mountpoint, revert to sorting by
1301 return (strcmp(zfs_get_name(za), zfs_get_name(zb)));
1305 * Return true if path2 is a child of path1 or path2 equals path1 or
1306 * path1 is "/" (path2 is always a child of "/").
1309 libzfs_path_contains(const char *path1, const char *path2)
1311 return (strcmp(path1, path2) == 0 || strcmp(path1, "/") == 0 ||
1312 (strstr(path2, path1) == path2 && path2[strlen(path1)] == '/'));
1317 non_descendant_idx(zfs_handle_t **handles, size_t num_handles, int idx)
1319 char parent[ZFS_MAXPROPLEN];
1320 char child[ZFS_MAXPROPLEN];
1323 verify(zfs_prop_get(handles[idx], ZFS_PROP_MOUNTPOINT, parent,
1324 sizeof (parent), NULL, NULL, 0, B_FALSE) == 0);
1326 for (i = idx + 1; i < num_handles; i++) {
1327 verify(zfs_prop_get(handles[i], ZFS_PROP_MOUNTPOINT, child,
1328 sizeof (child), NULL, NULL, 0, B_FALSE) == 0);
1329 if (!libzfs_path_contains(parent, child))
1335 typedef struct mnt_param {
1336 libzfs_handle_t *mnt_hdl;
1338 zfs_handle_t **mnt_zhps; /* filesystems to mount */
1339 size_t mnt_num_handles;
1340 int mnt_idx; /* Index of selected entry to mount */
1341 zfs_iter_f mnt_func;
1346 * Allocate and populate the parameter struct for mount function, and
1347 * schedule mounting of the entry selected by idx.
1350 zfs_dispatch_mount(libzfs_handle_t *hdl, zfs_handle_t **handles,
1351 size_t num_handles, int idx, zfs_iter_f func, void *data, tpool_t *tp)
1353 mnt_param_t *mnt_param = zfs_alloc(hdl, sizeof (mnt_param_t));
1355 mnt_param->mnt_hdl = hdl;
1356 mnt_param->mnt_tp = tp;
1357 mnt_param->mnt_zhps = handles;
1358 mnt_param->mnt_num_handles = num_handles;
1359 mnt_param->mnt_idx = idx;
1360 mnt_param->mnt_func = func;
1361 mnt_param->mnt_data = data;
1363 (void) tpool_dispatch(tp, zfs_mount_task, (void*)mnt_param);
1367 * This is the structure used to keep state of mounting or sharing operations
1368 * during a call to zpool_enable_datasets().
1370 typedef struct mount_state {
1372 * ms_mntstatus is set to -1 if any mount fails. While multiple threads
1373 * could update this variable concurrently, no synchronization is
1374 * needed as it's only ever set to -1.
1378 const char *ms_mntopts;
1382 zfs_mount_one(zfs_handle_t *zhp, void *arg)
1384 mount_state_t *ms = arg;
1387 if (zfs_mount(zhp, ms->ms_mntopts, ms->ms_mntflags) != 0)
1388 ret = ms->ms_mntstatus = -1;
1393 zfs_share_one(zfs_handle_t *zhp, void *arg)
1395 mount_state_t *ms = arg;
1398 if (zfs_share(zhp) != 0)
1399 ret = ms->ms_mntstatus = -1;
1404 * Thread pool function to mount one file system. On completion, it finds and
1405 * schedules its children to be mounted. This depends on the sorting done in
1406 * zfs_foreach_mountpoint(). Note that the degenerate case (chain of entries
1407 * each descending from the previous) will have no parallelism since we always
1408 * have to wait for the parent to finish mounting before we can schedule
1412 zfs_mount_task(void *arg)
1414 mnt_param_t *mp = arg;
1415 int idx = mp->mnt_idx;
1416 zfs_handle_t **handles = mp->mnt_zhps;
1417 size_t num_handles = mp->mnt_num_handles;
1418 char mountpoint[ZFS_MAXPROPLEN];
1420 verify(zfs_prop_get(handles[idx], ZFS_PROP_MOUNTPOINT, mountpoint,
1421 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
1423 if (mp->mnt_func(handles[idx], mp->mnt_data) != 0)
1427 * We dispatch tasks to mount filesystems with mountpoints underneath
1428 * this one. We do this by dispatching the next filesystem with a
1429 * descendant mountpoint of the one we just mounted, then skip all of
1430 * its descendants, dispatch the next descendant mountpoint, and so on.
1431 * The non_descendant_idx() function skips over filesystems that are
1432 * descendants of the filesystem we just dispatched.
1434 for (int i = idx + 1; i < num_handles;
1435 i = non_descendant_idx(handles, num_handles, i)) {
1436 char child[ZFS_MAXPROPLEN];
1437 verify(zfs_prop_get(handles[i], ZFS_PROP_MOUNTPOINT,
1438 child, sizeof (child), NULL, NULL, 0, B_FALSE) == 0);
1440 if (!libzfs_path_contains(mountpoint, child))
1441 break; /* not a descendant, return */
1442 zfs_dispatch_mount(mp->mnt_hdl, handles, num_handles, i,
1443 mp->mnt_func, mp->mnt_data, mp->mnt_tp);
1449 * Issue the func callback for each ZFS handle contained in the handles
1450 * array. This function is used to mount all datasets, and so this function
1451 * guarantees that filesystems for parent mountpoints are called before their
1452 * children. As such, before issuing any callbacks, we first sort the array
1453 * of handles by mountpoint.
1455 * Callbacks are issued in one of two ways:
1457 * 1. Sequentially: If the parallel argument is B_FALSE or the ZFS_SERIAL_MOUNT
1458 * environment variable is set, then we issue callbacks sequentially.
1460 * 2. In parallel: If the parallel argument is B_TRUE and the ZFS_SERIAL_MOUNT
1461 * environment variable is not set, then we use a tpool to dispatch threads
1462 * to mount filesystems in parallel. This function dispatches tasks to mount
1463 * the filesystems at the top-level mountpoints, and these tasks in turn
1464 * are responsible for recursively mounting filesystems in their children
1468 zfs_foreach_mountpoint(libzfs_handle_t *hdl, zfs_handle_t **handles,
1469 size_t num_handles, zfs_iter_f func, void *data, boolean_t parallel)
1471 zoneid_t zoneid = getzoneid();
1474 * The ZFS_SERIAL_MOUNT environment variable is an undocumented
1475 * variable that can be used as a convenience to do a/b comparison
1476 * of serial vs. parallel mounting.
1478 boolean_t serial_mount = !parallel ||
1479 (getenv("ZFS_SERIAL_MOUNT") != NULL);
1482 * Sort the datasets by mountpoint. See mountpoint_cmp for details
1483 * of how these are sorted.
1485 qsort(handles, num_handles, sizeof (zfs_handle_t *), mountpoint_cmp);
1488 for (int i = 0; i < num_handles; i++) {
1489 func(handles[i], data);
1495 * Issue the callback function for each dataset using a parallel
1496 * algorithm that uses a thread pool to manage threads.
1498 tpool_t *tp = tpool_create(1, mount_tp_nthr, 0, NULL);
1501 * There may be multiple "top level" mountpoints outside of the pool's
1502 * root mountpoint, e.g.: /foo /bar. Dispatch a mount task for each of
1505 for (int i = 0; i < num_handles;
1506 i = non_descendant_idx(handles, num_handles, i)) {
1508 * Since the mountpoints have been sorted so that the zoned
1509 * filesystems are at the end, a zoned filesystem seen from
1510 * the global zone means that we're done.
1512 if (zoneid == GLOBAL_ZONEID &&
1513 zfs_prop_get_int(handles[i], ZFS_PROP_ZONED))
1515 zfs_dispatch_mount(hdl, handles, num_handles, i, func, data,
1519 tpool_wait(tp); /* wait for all scheduled mounts to complete */
1524 * Mount and share all datasets within the given pool. This assumes that no
1525 * datasets within the pool are currently mounted.
1527 #pragma weak zpool_mount_datasets = zpool_enable_datasets
1529 zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
1531 get_all_cb_t cb = { 0 };
1532 mount_state_t ms = { 0 };
1536 if ((zfsp = zfs_open(zhp->zpool_hdl, zhp->zpool_name,
1537 ZFS_TYPE_DATASET)) == NULL)
1541 * Gather all non-snapshot datasets within the pool. Start by adding
1542 * the root filesystem for this pool to the list, and then iterate
1543 * over all child filesystems.
1545 libzfs_add_handle(&cb, zfsp);
1546 if (zfs_iter_filesystems(zfsp, zfs_iter_cb, &cb) != 0)
1550 * Mount all filesystems
1552 ms.ms_mntopts = mntopts;
1553 ms.ms_mntflags = flags;
1554 zfs_foreach_mountpoint(zhp->zpool_hdl, cb.cb_handles, cb.cb_used,
1555 zfs_mount_one, &ms, B_TRUE);
1556 if (ms.ms_mntstatus != 0)
1557 ret = ms.ms_mntstatus;
1560 * Share all filesystems that need to be shared. This needs to be
1561 * a separate pass because libshare is not mt-safe, and so we need
1562 * to share serially.
1564 ms.ms_mntstatus = 0;
1565 zfs_foreach_mountpoint(zhp->zpool_hdl, cb.cb_handles, cb.cb_used,
1566 zfs_share_one, &ms, B_FALSE);
1567 if (ms.ms_mntstatus != 0)
1568 ret = ms.ms_mntstatus;
1571 for (int i = 0; i < cb.cb_used; i++)
1572 zfs_close(cb.cb_handles[i]);
1573 free(cb.cb_handles);
1579 mountpoint_compare(const void *a, const void *b)
1581 const char *mounta = *((char **)a);
1582 const char *mountb = *((char **)b);
1584 return (strcmp(mountb, mounta));
1587 /* alias for 2002/240 */
1588 #pragma weak zpool_unmount_datasets = zpool_disable_datasets
1590 * Unshare and unmount all datasets within the given pool. We don't want to
1591 * rely on traversing the DSL to discover the filesystems within the pool,
1592 * because this may be expensive (if not all of them are mounted), and can fail
1593 * arbitrarily (on I/O error, for example). Instead, we walk /etc/mnttab and
1594 * gather all the filesystems that are currently mounted.
1597 zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
1600 struct mnttab entry;
1602 char **mountpoints = NULL;
1603 zfs_handle_t **datasets = NULL;
1604 libzfs_handle_t *hdl = zhp->zpool_hdl;
1607 int flags = (force ? MS_FORCE : 0);
1609 sa_init_selective_arg_t sharearg;
1612 namelen = strlen(zhp->zpool_name);
1614 rewind(hdl->libzfs_mnttab);
1616 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
1618 * Ignore non-ZFS entries.
1620 if (entry.mnt_fstype == NULL ||
1621 strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
1625 * Ignore filesystems not within this pool.
1627 if (entry.mnt_mountp == NULL ||
1628 strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 ||
1629 (entry.mnt_special[namelen] != '/' &&
1630 entry.mnt_special[namelen] != '\0'))
1634 * At this point we've found a filesystem within our pool. Add
1635 * it to our growing list.
1637 if (used == alloc) {
1639 if ((mountpoints = zfs_alloc(hdl,
1640 8 * sizeof (void *))) == NULL)
1643 if ((datasets = zfs_alloc(hdl,
1644 8 * sizeof (void *))) == NULL)
1651 if ((ptr = zfs_realloc(hdl, mountpoints,
1652 alloc * sizeof (void *),
1653 alloc * 2 * sizeof (void *))) == NULL)
1657 if ((ptr = zfs_realloc(hdl, datasets,
1658 alloc * sizeof (void *),
1659 alloc * 2 * sizeof (void *))) == NULL)
1667 if ((mountpoints[used] = zfs_strdup(hdl,
1668 entry.mnt_mountp)) == NULL)
1672 * This is allowed to fail, in case there is some I/O error. It
1673 * is only used to determine if we need to remove the underlying
1674 * mountpoint, so failure is not fatal.
1676 datasets[used] = make_dataset_handle(hdl, entry.mnt_special);
1682 * At this point, we have the entire list of filesystems, so sort it by
1686 sharearg.zhandle_arr = datasets;
1687 sharearg.zhandle_len = used;
1688 ret = zfs_init_libshare_arg(hdl, SA_INIT_SHARE_API_SELECTIVE,
1693 qsort(mountpoints, used, sizeof (char *), mountpoint_compare);
1696 * Walk through and first unshare everything.
1698 for (i = 0; i < used; i++) {
1699 zfs_share_proto_t *curr_proto;
1700 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
1702 if (is_shared(hdl, mountpoints[i], *curr_proto) &&
1703 unshare_one(hdl, mountpoints[i],
1704 mountpoints[i], *curr_proto) != 0)
1710 * Now unmount everything, removing the underlying directories as
1713 for (i = 0; i < used; i++) {
1714 if (unmount_one(hdl, mountpoints[i], flags) != 0)
1718 for (i = 0; i < used; i++) {
1720 remove_mountpoint(datasets[i]);
1725 for (i = 0; i < used; i++) {
1727 zfs_close(datasets[i]);
1728 free(mountpoints[i]);