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 * Returns true if the given dataset is mountable, false otherwise. Returns the
306 * mountpoint in 'buf'.
309 zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen,
310 zprop_source_t *source)
312 char sourceloc[MAXNAMELEN];
313 zprop_source_t sourcetype;
315 if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type))
318 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen,
319 &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0);
321 if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 ||
322 strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0)
325 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF)
328 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
329 getzoneid() == GLOBAL_ZONEID)
333 *source = sourcetype;
339 * Mount the given filesystem.
342 zfs_mount(zfs_handle_t *zhp, const char *options, int flags)
345 char mountpoint[ZFS_MAXPROPLEN];
346 char mntopts[MNT_LINE_MAX];
347 libzfs_handle_t *hdl = zhp->zfs_hdl;
352 (void) strlcpy(mntopts, options, sizeof (mntopts));
355 * If the pool is imported read-only then all mounts must be read-only
357 if (zpool_get_prop_int(zhp->zpool_hdl, ZPOOL_PROP_READONLY, NULL))
360 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
363 /* Create the directory if it doesn't already exist */
364 if (lstat(mountpoint, &buf) != 0) {
365 if (mkdirp(mountpoint, 0755) != 0) {
366 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
367 "failed to create mountpoint"));
368 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
369 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
374 #ifdef illumos /* FreeBSD: overlay mounts are not checked. */
376 * Determine if the mountpoint is empty. If so, refuse to perform the
377 * mount. We don't perform this check if MS_OVERLAY is specified, which
378 * would defeat the point. We also avoid this check if 'remount' is
381 if ((flags & MS_OVERLAY) == 0 &&
382 strstr(mntopts, MNTOPT_REMOUNT) == NULL &&
383 !dir_is_empty(mountpoint)) {
384 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
385 "directory is not empty"));
386 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
387 dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint));
391 /* perform the mount */
392 if (zmount(zfs_get_name(zhp), mountpoint, flags,
393 MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) {
395 * Generic errors are nasty, but there are just way too many
396 * from mount(), and they're well-understood. We pick a few
397 * common ones to improve upon.
399 if (errno == EBUSY) {
400 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
401 "mountpoint or dataset is busy"));
402 } else if (errno == EPERM) {
403 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
404 "Insufficient privileges"));
405 } else if (errno == ENOTSUP) {
409 VERIFY(zfs_spa_version(zhp, &spa_version) == 0);
410 (void) snprintf(buf, sizeof (buf),
411 dgettext(TEXT_DOMAIN, "Can't mount a version %lld "
412 "file system on a version %d pool. Pool must be"
413 " upgraded to mount this file system."),
414 (u_longlong_t)zfs_prop_get_int(zhp,
415 ZFS_PROP_VERSION), spa_version);
416 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, buf));
418 zfs_error_aux(hdl, strerror(errno));
420 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
421 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
425 /* add the mounted entry into our cache */
426 libzfs_mnttab_add(hdl, zfs_get_name(zhp), mountpoint,
432 * Unmount a single filesystem.
435 unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags)
437 if (umount2(mountpoint, flags) != 0) {
438 zfs_error_aux(hdl, strerror(errno));
439 return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED,
440 dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
448 * Unmount the given filesystem.
451 zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
453 libzfs_handle_t *hdl = zhp->zfs_hdl;
457 /* check to see if we need to unmount the filesystem */
458 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
459 libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)) {
461 * mountpoint may have come from a call to
462 * getmnt/getmntany if it isn't NULL. If it is NULL,
463 * we know it comes from libzfs_mnttab_find which can
464 * then get freed later. We strdup it to play it safe.
466 if (mountpoint == NULL)
467 mntpt = zfs_strdup(hdl, entry.mnt_mountp);
469 mntpt = zfs_strdup(hdl, mountpoint);
472 * Unshare and unmount the filesystem
474 if (zfs_unshare_proto(zhp, mntpt, share_all_proto) != 0)
477 if (unmount_one(hdl, mntpt, flags) != 0) {
479 (void) zfs_shareall(zhp);
482 libzfs_mnttab_remove(hdl, zhp->zfs_name);
490 * Unmount this filesystem and any children inheriting the mountpoint property.
491 * To do this, just act like we're changing the mountpoint property, but don't
492 * remount the filesystems afterwards.
495 zfs_unmountall(zfs_handle_t *zhp, int flags)
497 prop_changelist_t *clp;
500 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, 0, flags);
504 ret = changelist_prefix(clp);
505 changelist_free(clp);
511 zfs_is_shared(zfs_handle_t *zhp)
513 zfs_share_type_t rc = 0;
514 zfs_share_proto_t *curr_proto;
516 if (ZFS_IS_VOLUME(zhp))
519 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
521 rc |= zfs_is_shared_proto(zhp, NULL, *curr_proto);
523 return (rc ? B_TRUE : B_FALSE);
527 zfs_share(zfs_handle_t *zhp)
529 assert(!ZFS_IS_VOLUME(zhp));
530 return (zfs_share_proto(zhp, share_all_proto));
534 zfs_unshare(zfs_handle_t *zhp)
536 assert(!ZFS_IS_VOLUME(zhp));
537 return (zfs_unshareall(zhp));
541 * Check to see if the filesystem is currently shared.
544 zfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto)
549 if (!zfs_is_mounted(zhp, &mountpoint))
550 return (SHARED_NOT_SHARED);
552 if ((rc = is_shared(zhp->zfs_hdl, mountpoint, proto))
553 != SHARED_NOT_SHARED) {
561 return (SHARED_NOT_SHARED);
566 zfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
568 return (zfs_is_shared_proto(zhp, where,
569 PROTO_NFS) != SHARED_NOT_SHARED);
573 zfs_is_shared_smb(zfs_handle_t *zhp, char **where)
575 return (zfs_is_shared_proto(zhp, where,
576 PROTO_SMB) != SHARED_NOT_SHARED);
580 * Make sure things will work if libshare isn't installed by using
581 * wrapper functions that check to see that the pointers to functions
582 * initialized in _zfs_init_libshare() are actually present.
586 static sa_handle_t (*_sa_init)(int);
587 static sa_handle_t (*_sa_init_arg)(int, void *);
588 static void (*_sa_fini)(sa_handle_t);
589 static sa_share_t (*_sa_find_share)(sa_handle_t, char *);
590 static int (*_sa_enable_share)(sa_share_t, char *);
591 static int (*_sa_disable_share)(sa_share_t, char *);
592 static char *(*_sa_errorstr)(int);
593 static int (*_sa_parse_legacy_options)(sa_group_t, char *, char *);
594 static boolean_t (*_sa_needs_refresh)(sa_handle_t *);
595 static libzfs_handle_t *(*_sa_get_zfs_handle)(sa_handle_t);
596 static int (*_sa_zfs_process_share)(sa_handle_t, sa_group_t, sa_share_t,
597 char *, char *, zprop_source_t, char *, char *, char *);
598 static void (*_sa_update_sharetab_ts)(sa_handle_t);
602 * _zfs_init_libshare()
604 * Find the libshare.so.1 entry points that we use here and save the
605 * values to be used later. This is triggered by the runtime loader.
606 * Make sure the correct ISA version is loaded.
609 #pragma init(_zfs_init_libshare)
611 _zfs_init_libshare(void)
615 char path[MAXPATHLEN];
619 if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1)
624 (void) snprintf(path, MAXPATHLEN,
625 "/usr/lib/%s/libshare.so.1", isa);
627 if ((libshare = dlopen(path, RTLD_LAZY | RTLD_GLOBAL)) != NULL) {
628 _sa_init = (sa_handle_t (*)(int))dlsym(libshare, "sa_init");
629 _sa_init_arg = (sa_handle_t (*)(int, void *))dlsym(libshare,
631 _sa_fini = (void (*)(sa_handle_t))dlsym(libshare, "sa_fini");
632 _sa_find_share = (sa_share_t (*)(sa_handle_t, char *))
633 dlsym(libshare, "sa_find_share");
634 _sa_enable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
636 _sa_disable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
638 _sa_errorstr = (char *(*)(int))dlsym(libshare, "sa_errorstr");
639 _sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *))
640 dlsym(libshare, "sa_parse_legacy_options");
641 _sa_needs_refresh = (boolean_t (*)(sa_handle_t *))
642 dlsym(libshare, "sa_needs_refresh");
643 _sa_get_zfs_handle = (libzfs_handle_t *(*)(sa_handle_t))
644 dlsym(libshare, "sa_get_zfs_handle");
645 _sa_zfs_process_share = (int (*)(sa_handle_t, sa_group_t,
646 sa_share_t, char *, char *, zprop_source_t, char *,
647 char *, char *))dlsym(libshare, "sa_zfs_process_share");
648 _sa_update_sharetab_ts = (void (*)(sa_handle_t))
649 dlsym(libshare, "sa_update_sharetab_ts");
650 if (_sa_init == NULL || _sa_init_arg == NULL ||
651 _sa_fini == NULL || _sa_find_share == NULL ||
652 _sa_enable_share == NULL || _sa_disable_share == NULL ||
653 _sa_errorstr == NULL || _sa_parse_legacy_options == NULL ||
654 _sa_needs_refresh == NULL || _sa_get_zfs_handle == NULL ||
655 _sa_zfs_process_share == NULL ||
656 _sa_update_sharetab_ts == NULL) {
660 _sa_disable_share = NULL;
661 _sa_enable_share = NULL;
663 _sa_parse_legacy_options = NULL;
664 (void) dlclose(libshare);
665 _sa_needs_refresh = NULL;
666 _sa_get_zfs_handle = NULL;
667 _sa_zfs_process_share = NULL;
668 _sa_update_sharetab_ts = NULL;
675 * zfs_init_libshare(zhandle, service)
677 * Initialize the libshare API if it hasn't already been initialized.
678 * In all cases it returns 0 if it succeeded and an error if not. The
679 * service value is which part(s) of the API to initialize and is a
680 * direct map to the libshare sa_init(service) interface.
683 zfs_init_libshare_impl(libzfs_handle_t *zhandle, int service, void *arg)
687 * libshare is either not installed or we're in a branded zone. The
688 * rest of the wrapper functions around the libshare calls already
689 * handle NULL function pointers, but we don't want the callers of
690 * zfs_init_libshare() to fail prematurely if libshare is not available.
692 if (_sa_init == NULL)
696 * Attempt to refresh libshare. This is necessary if there was a cache
697 * miss for a new ZFS dataset that was just created, or if state of the
698 * sharetab file has changed since libshare was last initialized. We
699 * want to make sure so check timestamps to see if a different process
700 * has updated any of the configuration. If there was some non-ZFS
701 * change, we need to re-initialize the internal cache.
703 if (_sa_needs_refresh != NULL &&
704 _sa_needs_refresh(zhandle->libzfs_sharehdl)) {
705 zfs_uninit_libshare(zhandle);
706 zhandle->libzfs_sharehdl = _sa_init_arg(service, arg);
709 if (zhandle && zhandle->libzfs_sharehdl == NULL)
710 zhandle->libzfs_sharehdl = _sa_init_arg(service, arg);
712 if (zhandle->libzfs_sharehdl == NULL)
713 return (SA_NO_MEMORY);
719 zfs_init_libshare(libzfs_handle_t *zhandle, int service)
721 return (zfs_init_libshare_impl(zhandle, service, NULL));
725 zfs_init_libshare_arg(libzfs_handle_t *zhandle, int service, void *arg)
727 return (zfs_init_libshare_impl(zhandle, service, arg));
732 * zfs_uninit_libshare(zhandle)
734 * Uninitialize the libshare API if it hasn't already been
735 * uninitialized. It is OK to call multiple times.
738 zfs_uninit_libshare(libzfs_handle_t *zhandle)
740 if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) {
742 if (_sa_fini != NULL)
743 _sa_fini(zhandle->libzfs_sharehdl);
745 zhandle->libzfs_sharehdl = NULL;
750 * zfs_parse_options(options, proto)
752 * Call the legacy parse interface to get the protocol specific
753 * options using the NULL arg to indicate that this is a "parse" only.
756 zfs_parse_options(char *options, zfs_share_proto_t proto)
759 if (_sa_parse_legacy_options != NULL) {
760 return (_sa_parse_legacy_options(NULL, options,
761 proto_table[proto].p_name));
763 return (SA_CONFIG_ERR);
771 * zfs_sa_find_share(handle, path)
773 * wrapper around sa_find_share to find a share path in the
777 zfs_sa_find_share(sa_handle_t handle, char *path)
779 if (_sa_find_share != NULL)
780 return (_sa_find_share(handle, path));
785 * zfs_sa_enable_share(share, proto)
787 * Wrapper for sa_enable_share which enables a share for a specified
791 zfs_sa_enable_share(sa_share_t share, char *proto)
793 if (_sa_enable_share != NULL)
794 return (_sa_enable_share(share, proto));
795 return (SA_CONFIG_ERR);
799 * zfs_sa_disable_share(share, proto)
801 * Wrapper for sa_enable_share which disables a share for a specified
805 zfs_sa_disable_share(sa_share_t share, char *proto)
807 if (_sa_disable_share != NULL)
808 return (_sa_disable_share(share, proto));
809 return (SA_CONFIG_ERR);
814 * Share the given filesystem according to the options in the specified
815 * protocol specific properties (sharenfs, sharesmb). We rely
816 * on "libshare" to the dirty work for us.
819 zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
821 char mountpoint[ZFS_MAXPROPLEN];
822 char shareopts[ZFS_MAXPROPLEN];
823 char sourcestr[ZFS_MAXPROPLEN];
824 libzfs_handle_t *hdl = zhp->zfs_hdl;
825 zfs_share_proto_t *curr_proto;
826 zprop_source_t sourcetype;
829 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
832 for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) {
834 * Return success if there are no share options.
836 if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop,
837 shareopts, sizeof (shareopts), &sourcetype, sourcestr,
838 ZFS_MAXPROPLEN, B_FALSE) != 0 ||
839 strcmp(shareopts, "off") == 0)
842 ret = zfs_init_libshare_arg(hdl, SA_INIT_ONE_SHARE_FROM_HANDLE,
845 (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
846 dgettext(TEXT_DOMAIN, "cannot share '%s': %s"),
847 zfs_get_name(zhp), _sa_errorstr != NULL ?
848 _sa_errorstr(ret) : "");
854 * If the 'zoned' property is set, then zfs_is_mountable()
855 * will have already bailed out if we are in the global zone.
856 * But local zones cannot be NFS servers, so we ignore it for
857 * local zones as well.
859 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
863 share = zfs_sa_find_share(hdl->libzfs_sharehdl, mountpoint);
866 * This may be a new file system that was just
867 * created so isn't in the internal cache
868 * (second time through). Rather than
869 * reloading the entire configuration, we can
870 * assume ZFS has done the checking and it is
871 * safe to add this to the internal
874 if (_sa_zfs_process_share(hdl->libzfs_sharehdl,
875 NULL, NULL, mountpoint,
876 proto_table[*curr_proto].p_name, sourcetype,
877 shareopts, sourcestr, zhp->zfs_name) != SA_OK) {
878 (void) zfs_error_fmt(hdl,
879 proto_table[*curr_proto].p_share_err,
880 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
884 share = zfs_sa_find_share(hdl->libzfs_sharehdl,
889 err = zfs_sa_enable_share(share,
890 proto_table[*curr_proto].p_name);
892 (void) zfs_error_fmt(hdl,
893 proto_table[*curr_proto].p_share_err,
894 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
900 if (*curr_proto != PROTO_NFS) {
901 fprintf(stderr, "Unsupported share protocol: %d.\n",
906 if (strcmp(shareopts, "on") == 0)
907 error = fsshare(ZFS_EXPORTS_PATH, mountpoint, "");
909 error = fsshare(ZFS_EXPORTS_PATH, mountpoint, shareopts);
913 (void) zfs_error_fmt(hdl,
914 proto_table[*curr_proto].p_share_err,
915 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
926 zfs_share_nfs(zfs_handle_t *zhp)
928 return (zfs_share_proto(zhp, nfs_only));
932 zfs_share_smb(zfs_handle_t *zhp)
934 return (zfs_share_proto(zhp, smb_only));
938 zfs_shareall(zfs_handle_t *zhp)
940 return (zfs_share_proto(zhp, share_all_proto));
944 * Unshare a filesystem by mountpoint.
947 unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint,
948 zfs_share_proto_t proto)
956 * Mountpoint could get trashed if libshare calls getmntany
957 * which it does during API initialization, so strdup the
960 mntpt = zfs_strdup(hdl, mountpoint);
963 * make sure libshare initialized, initialize everything because we
964 * don't know what other unsharing may happen later. Functions up the
965 * stack are allowed to initialize instead a subset of shares at the
966 * time the set is known.
968 if ((err = zfs_init_libshare_arg(hdl, SA_INIT_ONE_SHARE_FROM_NAME,
969 (void *)name)) != SA_OK) {
970 free(mntpt); /* don't need the copy anymore */
971 return (zfs_error_fmt(hdl, proto_table[proto].p_unshare_err,
972 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
973 name, _sa_errorstr(err)));
976 share = zfs_sa_find_share(hdl->libzfs_sharehdl, mntpt);
977 free(mntpt); /* don't need the copy anymore */
980 err = zfs_sa_disable_share(share, proto_table[proto].p_name);
982 return (zfs_error_fmt(hdl,
983 proto_table[proto].p_unshare_err,
984 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
985 name, _sa_errorstr(err)));
988 return (zfs_error_fmt(hdl, proto_table[proto].p_unshare_err,
989 dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"),
993 char buf[MAXPATHLEN];
997 if (proto != PROTO_NFS) {
998 fprintf(stderr, "No SMB support in FreeBSD yet.\n");
1002 err = fsunshare(ZFS_EXPORTS_PATH, mountpoint);
1004 zfs_error_aux(hdl, "%s", strerror(err));
1005 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
1006 dgettext(TEXT_DOMAIN,
1007 "cannot unshare '%s'"), name));
1014 * Unshare the given filesystem.
1017 zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint,
1018 zfs_share_proto_t *proto)
1020 libzfs_handle_t *hdl = zhp->zfs_hdl;
1021 struct mnttab entry;
1024 /* check to see if need to unmount the filesystem */
1025 rewind(zhp->zfs_hdl->libzfs_mnttab);
1026 if (mountpoint != NULL)
1027 mountpoint = mntpt = zfs_strdup(hdl, mountpoint);
1029 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
1030 libzfs_mnttab_find(hdl, zfs_get_name(zhp), &entry) == 0)) {
1031 zfs_share_proto_t *curr_proto;
1033 if (mountpoint == NULL)
1034 mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp);
1036 for (curr_proto = proto; *curr_proto != PROTO_END;
1039 if (is_shared(hdl, mntpt, *curr_proto) &&
1040 unshare_one(hdl, zhp->zfs_name,
1041 mntpt, *curr_proto) != 0) {
1055 zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
1057 return (zfs_unshare_proto(zhp, mountpoint, nfs_only));
1061 zfs_unshare_smb(zfs_handle_t *zhp, const char *mountpoint)
1063 return (zfs_unshare_proto(zhp, mountpoint, smb_only));
1067 * Same as zfs_unmountall(), but for NFS and SMB unshares.
1070 zfs_unshareall_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
1072 prop_changelist_t *clp;
1075 clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0, 0);
1079 ret = changelist_unshare(clp, proto);
1080 changelist_free(clp);
1086 zfs_unshareall_nfs(zfs_handle_t *zhp)
1088 return (zfs_unshareall_proto(zhp, nfs_only));
1092 zfs_unshareall_smb(zfs_handle_t *zhp)
1094 return (zfs_unshareall_proto(zhp, smb_only));
1098 zfs_unshareall(zfs_handle_t *zhp)
1100 return (zfs_unshareall_proto(zhp, share_all_proto));
1104 zfs_unshareall_bypath(zfs_handle_t *zhp, const char *mountpoint)
1106 return (zfs_unshare_proto(zhp, mountpoint, share_all_proto));
1110 * Remove the mountpoint associated with the current dataset, if necessary.
1111 * We only remove the underlying directory if:
1113 * - The mountpoint is not 'none' or 'legacy'
1114 * - The mountpoint is non-empty
1115 * - The mountpoint is the default or inherited
1116 * - The 'zoned' property is set, or we're in a local zone
1118 * Any other directories we leave alone.
1121 remove_mountpoint(zfs_handle_t *zhp)
1123 char mountpoint[ZFS_MAXPROPLEN];
1124 zprop_source_t source;
1126 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
1130 if (source == ZPROP_SRC_DEFAULT ||
1131 source == ZPROP_SRC_INHERITED) {
1133 * Try to remove the directory, silently ignoring any errors.
1134 * The filesystem may have since been removed or moved around,
1135 * and this error isn't really useful to the administrator in
1138 (void) rmdir(mountpoint);
1143 * Add the given zfs handle to the cb_handles array, dynamically reallocating
1144 * the array if it is out of space
1147 libzfs_add_handle(get_all_cb_t *cbp, zfs_handle_t *zhp)
1149 if (cbp->cb_alloc == cbp->cb_used) {
1151 zfs_handle_t **newhandles;
1153 newsz = cbp->cb_alloc != 0 ? cbp->cb_alloc * 2 : 64;
1154 newhandles = zfs_realloc(zhp->zfs_hdl,
1155 cbp->cb_handles, cbp->cb_alloc * sizeof (zfs_handle_t *),
1156 newsz * sizeof (zfs_handle_t *));
1157 cbp->cb_handles = newhandles;
1158 cbp->cb_alloc = newsz;
1160 cbp->cb_handles[cbp->cb_used++] = zhp;
1164 * Recursive helper function used during file system enumeration
1167 zfs_iter_cb(zfs_handle_t *zhp, void *data)
1169 get_all_cb_t *cbp = data;
1171 if (!(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM)) {
1176 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_NOAUTO) {
1182 * If this filesystem is inconsistent and has a receive resume
1183 * token, we can not mount it.
1185 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
1186 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
1187 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
1192 libzfs_add_handle(cbp, zhp);
1193 if (zfs_iter_filesystems(zhp, zfs_iter_cb, cbp) != 0) {
1201 * Sort comparator that compares two mountpoint paths. We sort these paths so
1202 * that subdirectories immediately follow their parents. This means that we
1203 * effectively treat the '/' character as the lowest value non-nul char.
1204 * Since filesystems from non-global zones can have the same mountpoint
1205 * as other filesystems, the comparator sorts global zone filesystems to
1206 * the top of the list. This means that the global zone will traverse the
1207 * filesystem list in the correct order and can stop when it sees the
1208 * first zoned filesystem. In a non-global zone, only the delegated
1209 * filesystems are seen.
1211 * An example sorted list using this comparator would look like:
1221 * The mount code depend on this ordering to deterministically iterate
1222 * over filesystems in order to spawn parallel mount tasks.
1225 mountpoint_cmp(const void *arga, const void *argb)
1227 zfs_handle_t *const *zap = arga;
1228 zfs_handle_t *za = *zap;
1229 zfs_handle_t *const *zbp = argb;
1230 zfs_handle_t *zb = *zbp;
1231 char mounta[MAXPATHLEN];
1232 char mountb[MAXPATHLEN];
1233 const char *a = mounta;
1234 const char *b = mountb;
1235 boolean_t gota, gotb;
1236 uint64_t zoneda, zonedb;
1238 zoneda = zfs_prop_get_int(za, ZFS_PROP_ZONED);
1239 zonedb = zfs_prop_get_int(zb, ZFS_PROP_ZONED);
1240 if (zoneda && !zonedb)
1242 if (!zoneda && zonedb)
1244 gota = (zfs_get_type(za) == ZFS_TYPE_FILESYSTEM);
1246 verify(zfs_prop_get(za, ZFS_PROP_MOUNTPOINT, mounta,
1247 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
1248 gotb = (zfs_get_type(zb) == ZFS_TYPE_FILESYSTEM);
1250 verify(zfs_prop_get(zb, ZFS_PROP_MOUNTPOINT, mountb,
1251 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
1254 while (*a != '\0' && (*a == *b)) {
1268 return (*a < *b ? -1 : *a > *b);
1277 * If neither filesystem has a mountpoint, revert to sorting by
1280 return (strcmp(zfs_get_name(za), zfs_get_name(zb)));
1284 * Reutrn true if path2 is a child of path1
1287 libzfs_path_contains(const char *path1, const char *path2)
1289 return (strstr(path2, path1) == path2 && path2[strlen(path1)] == '/');
1294 non_descendant_idx(zfs_handle_t **handles, size_t num_handles, int idx)
1296 char parent[ZFS_MAXPROPLEN];
1297 char child[ZFS_MAXPROPLEN];
1300 verify(zfs_prop_get(handles[idx], ZFS_PROP_MOUNTPOINT, parent,
1301 sizeof (parent), NULL, NULL, 0, B_FALSE) == 0);
1303 for (i = idx + 1; i < num_handles; i++) {
1304 verify(zfs_prop_get(handles[i], ZFS_PROP_MOUNTPOINT, child,
1305 sizeof (child), NULL, NULL, 0, B_FALSE) == 0);
1306 if (!libzfs_path_contains(parent, child))
1312 typedef struct mnt_param {
1313 libzfs_handle_t *mnt_hdl;
1315 zfs_handle_t **mnt_zhps; /* filesystems to mount */
1316 size_t mnt_num_handles;
1317 int mnt_idx; /* Index of selected entry to mount */
1318 zfs_iter_f mnt_func;
1323 * Allocate and populate the parameter struct for mount function, and
1324 * schedule mounting of the entry selected by idx.
1327 zfs_dispatch_mount(libzfs_handle_t *hdl, zfs_handle_t **handles,
1328 size_t num_handles, int idx, zfs_iter_f func, void *data, tpool_t *tp)
1330 mnt_param_t *mnt_param = zfs_alloc(hdl, sizeof (mnt_param_t));
1332 mnt_param->mnt_hdl = hdl;
1333 mnt_param->mnt_tp = tp;
1334 mnt_param->mnt_zhps = handles;
1335 mnt_param->mnt_num_handles = num_handles;
1336 mnt_param->mnt_idx = idx;
1337 mnt_param->mnt_func = func;
1338 mnt_param->mnt_data = data;
1340 (void) tpool_dispatch(tp, zfs_mount_task, (void*)mnt_param);
1344 * This is the structure used to keep state of mounting or sharing operations
1345 * during a call to zpool_enable_datasets().
1347 typedef struct mount_state {
1349 * ms_mntstatus is set to -1 if any mount fails. While multiple threads
1350 * could update this variable concurrently, no synchronization is
1351 * needed as it's only ever set to -1.
1355 const char *ms_mntopts;
1359 zfs_mount_one(zfs_handle_t *zhp, void *arg)
1361 mount_state_t *ms = arg;
1364 if (zfs_mount(zhp, ms->ms_mntopts, ms->ms_mntflags) != 0)
1365 ret = ms->ms_mntstatus = -1;
1370 zfs_share_one(zfs_handle_t *zhp, void *arg)
1372 mount_state_t *ms = arg;
1375 if (zfs_share(zhp) != 0)
1376 ret = ms->ms_mntstatus = -1;
1381 * Thread pool function to mount one file system. On completion, it finds and
1382 * schedules its children to be mounted. This depends on the sorting done in
1383 * zfs_foreach_mountpoint(). Note that the degenerate case (chain of entries
1384 * each descending from the previous) will have no parallelism since we always
1385 * have to wait for the parent to finish mounting before we can schedule
1389 zfs_mount_task(void *arg)
1391 mnt_param_t *mp = arg;
1392 int idx = mp->mnt_idx;
1393 zfs_handle_t **handles = mp->mnt_zhps;
1394 size_t num_handles = mp->mnt_num_handles;
1395 char mountpoint[ZFS_MAXPROPLEN];
1397 verify(zfs_prop_get(handles[idx], ZFS_PROP_MOUNTPOINT, mountpoint,
1398 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
1400 if (mp->mnt_func(handles[idx], mp->mnt_data) != 0)
1404 * We dispatch tasks to mount filesystems with mountpoints underneath
1405 * this one. We do this by dispatching the next filesystem with a
1406 * descendant mountpoint of the one we just mounted, then skip all of
1407 * its descendants, dispatch the next descendant mountpoint, and so on.
1408 * The non_descendant_idx() function skips over filesystems that are
1409 * descendants of the filesystem we just dispatched.
1411 for (int i = idx + 1; i < num_handles;
1412 i = non_descendant_idx(handles, num_handles, i)) {
1413 char child[ZFS_MAXPROPLEN];
1414 verify(zfs_prop_get(handles[i], ZFS_PROP_MOUNTPOINT,
1415 child, sizeof (child), NULL, NULL, 0, B_FALSE) == 0);
1417 if (!libzfs_path_contains(mountpoint, child))
1418 break; /* not a descendant, return */
1419 zfs_dispatch_mount(mp->mnt_hdl, handles, num_handles, i,
1420 mp->mnt_func, mp->mnt_data, mp->mnt_tp);
1426 * Issue the func callback for each ZFS handle contained in the handles
1427 * array. This function is used to mount all datasets, and so this function
1428 * guarantees that filesystems for parent mountpoints are called before their
1429 * children. As such, before issuing any callbacks, we first sort the array
1430 * of handles by mountpoint.
1432 * Callbacks are issued in one of two ways:
1434 * 1. Sequentially: If the parallel argument is B_FALSE or the ZFS_SERIAL_MOUNT
1435 * environment variable is set, then we issue callbacks sequentially.
1437 * 2. In parallel: If the parallel argument is B_TRUE and the ZFS_SERIAL_MOUNT
1438 * environment variable is not set, then we use a tpool to dispatch threads
1439 * to mount filesystems in parallel. This function dispatches tasks to mount
1440 * the filesystems at the top-level mountpoints, and these tasks in turn
1441 * are responsible for recursively mounting filesystems in their children
1445 zfs_foreach_mountpoint(libzfs_handle_t *hdl, zfs_handle_t **handles,
1446 size_t num_handles, zfs_iter_f func, void *data, boolean_t parallel)
1448 zoneid_t zoneid = getzoneid();
1451 * The ZFS_SERIAL_MOUNT environment variable is an undocumented
1452 * variable that can be used as a convenience to do a/b comparison
1453 * of serial vs. parallel mounting.
1455 boolean_t serial_mount = !parallel ||
1456 (getenv("ZFS_SERIAL_MOUNT") != NULL);
1459 * Sort the datasets by mountpoint. See mountpoint_cmp for details
1460 * of how these are sorted.
1462 qsort(handles, num_handles, sizeof (zfs_handle_t *), mountpoint_cmp);
1465 for (int i = 0; i < num_handles; i++) {
1466 func(handles[i], data);
1472 * Issue the callback function for each dataset using a parallel
1473 * algorithm that uses a thread pool to manage threads.
1475 tpool_t *tp = tpool_create(1, mount_tp_nthr, 0, NULL);
1478 * There may be multiple "top level" mountpoints outside of the pool's
1479 * root mountpoint, e.g.: /foo /bar. Dispatch a mount task for each of
1482 for (int i = 0; i < num_handles;
1483 i = non_descendant_idx(handles, num_handles, i)) {
1485 * Since the mountpoints have been sorted so that the zoned
1486 * filesystems are at the end, a zoned filesystem seen from
1487 * the global zone means that we're done.
1489 if (zoneid == GLOBAL_ZONEID &&
1490 zfs_prop_get_int(handles[i], ZFS_PROP_ZONED))
1492 zfs_dispatch_mount(hdl, handles, num_handles, i, func, data,
1496 tpool_wait(tp); /* wait for all scheduled mounts to complete */
1501 * Mount and share all datasets within the given pool. This assumes that no
1502 * datasets within the pool are currently mounted.
1504 #pragma weak zpool_mount_datasets = zpool_enable_datasets
1506 zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
1508 get_all_cb_t cb = { 0 };
1509 mount_state_t ms = { 0 };
1513 if ((zfsp = zfs_open(zhp->zpool_hdl, zhp->zpool_name,
1514 ZFS_TYPE_DATASET)) == NULL)
1518 * Gather all non-snapshot datasets within the pool. Start by adding
1519 * the root filesystem for this pool to the list, and then iterate
1520 * over all child filesystems.
1522 libzfs_add_handle(&cb, zfsp);
1523 if (zfs_iter_filesystems(zfsp, zfs_iter_cb, &cb) != 0)
1527 * Mount all filesystems
1529 ms.ms_mntopts = mntopts;
1530 ms.ms_mntflags = flags;
1531 zfs_foreach_mountpoint(zhp->zpool_hdl, cb.cb_handles, cb.cb_used,
1532 zfs_mount_one, &ms, B_TRUE);
1533 if (ms.ms_mntstatus != 0)
1534 ret = ms.ms_mntstatus;
1537 * Share all filesystems that need to be shared. This needs to be
1538 * a separate pass because libshare is not mt-safe, and so we need
1539 * to share serially.
1541 ms.ms_mntstatus = 0;
1542 zfs_foreach_mountpoint(zhp->zpool_hdl, cb.cb_handles, cb.cb_used,
1543 zfs_share_one, &ms, B_FALSE);
1544 if (ms.ms_mntstatus != 0)
1545 ret = ms.ms_mntstatus;
1548 for (int i = 0; i < cb.cb_used; i++)
1549 zfs_close(cb.cb_handles[i]);
1550 free(cb.cb_handles);
1556 mountpoint_compare(const void *a, const void *b)
1558 const char *mounta = *((char **)a);
1559 const char *mountb = *((char **)b);
1561 return (strcmp(mountb, mounta));
1564 /* alias for 2002/240 */
1565 #pragma weak zpool_unmount_datasets = zpool_disable_datasets
1567 * Unshare and unmount all datasets within the given pool. We don't want to
1568 * rely on traversing the DSL to discover the filesystems within the pool,
1569 * because this may be expensive (if not all of them are mounted), and can fail
1570 * arbitrarily (on I/O error, for example). Instead, we walk /etc/mnttab and
1571 * gather all the filesystems that are currently mounted.
1574 zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
1577 struct mnttab entry;
1579 char **mountpoints = NULL;
1580 zfs_handle_t **datasets = NULL;
1581 libzfs_handle_t *hdl = zhp->zpool_hdl;
1584 int flags = (force ? MS_FORCE : 0);
1586 sa_init_selective_arg_t sharearg;
1589 namelen = strlen(zhp->zpool_name);
1591 rewind(hdl->libzfs_mnttab);
1593 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
1595 * Ignore non-ZFS entries.
1597 if (entry.mnt_fstype == NULL ||
1598 strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
1602 * Ignore filesystems not within this pool.
1604 if (entry.mnt_mountp == NULL ||
1605 strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 ||
1606 (entry.mnt_special[namelen] != '/' &&
1607 entry.mnt_special[namelen] != '\0'))
1611 * At this point we've found a filesystem within our pool. Add
1612 * it to our growing list.
1614 if (used == alloc) {
1616 if ((mountpoints = zfs_alloc(hdl,
1617 8 * sizeof (void *))) == NULL)
1620 if ((datasets = zfs_alloc(hdl,
1621 8 * sizeof (void *))) == NULL)
1628 if ((ptr = zfs_realloc(hdl, mountpoints,
1629 alloc * sizeof (void *),
1630 alloc * 2 * sizeof (void *))) == NULL)
1634 if ((ptr = zfs_realloc(hdl, datasets,
1635 alloc * sizeof (void *),
1636 alloc * 2 * sizeof (void *))) == NULL)
1644 if ((mountpoints[used] = zfs_strdup(hdl,
1645 entry.mnt_mountp)) == NULL)
1649 * This is allowed to fail, in case there is some I/O error. It
1650 * is only used to determine if we need to remove the underlying
1651 * mountpoint, so failure is not fatal.
1653 datasets[used] = make_dataset_handle(hdl, entry.mnt_special);
1659 * At this point, we have the entire list of filesystems, so sort it by
1663 sharearg.zhandle_arr = datasets;
1664 sharearg.zhandle_len = used;
1665 ret = zfs_init_libshare_arg(hdl, SA_INIT_SHARE_API_SELECTIVE,
1670 qsort(mountpoints, used, sizeof (char *), mountpoint_compare);
1673 * Walk through and first unshare everything.
1675 for (i = 0; i < used; i++) {
1676 zfs_share_proto_t *curr_proto;
1677 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
1679 if (is_shared(hdl, mountpoints[i], *curr_proto) &&
1680 unshare_one(hdl, mountpoints[i],
1681 mountpoints[i], *curr_proto) != 0)
1687 * Now unmount everything, removing the underlying directories as
1690 for (i = 0; i < used; i++) {
1691 if (unmount_one(hdl, mountpoints[i], flags) != 0)
1695 for (i = 0; i < used; i++) {
1697 remove_mountpoint(datasets[i]);
1702 for (i = 0; i < used; i++) {
1704 zfs_close(datasets[i]);
1705 free(mountpoints[i]);