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, 2015 by Delphix. All rights reserved.
26 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
27 * Copyright 2017 Joyent, Inc.
28 * Copyright 2017 RackTop Systems.
32 * Routines to manage ZFS mounts. We separate all the nasty routines that have
33 * to deal with the OS. The following functions are the main entry points --
34 * they are used by mount and unmount and when changing a filesystem's
42 * This file also contains the functions used to manage sharing filesystems via
55 * zfs_unshareall_nfs()
56 * zfs_unshareall_smb()
58 * zfs_unshareall_bypath()
60 * The following functions are available for pool consumers, and will
61 * mount/unmount and share/unshare all datasets within pool:
63 * zpool_enable_datasets()
64 * zpool_disable_datasets()
78 #include <sys/mntent.h>
79 #include <sys/mount.h>
81 #include <sys/statvfs.h>
85 #include "libzfs_impl.h"
88 #define MAXISALEN 257 /* based on sysinfo(2) man page */
90 static int zfs_share_proto(zfs_handle_t *, zfs_share_proto_t *);
91 zfs_share_type_t zfs_is_shared_proto(zfs_handle_t *, char **,
95 * The share protocols table must be in the same order as the zfs_share_proto_t
96 * 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[] = {
126 * Search the sharetab for the given mountpoint and protocol, returning
127 * a zfs_share_type_t value.
129 static zfs_share_type_t
130 is_shared(libzfs_handle_t *hdl, const char *mountpoint, zfs_share_proto_t proto)
132 char buf[MAXPATHLEN], *tab;
135 if (hdl->libzfs_sharetab == NULL)
136 return (SHARED_NOT_SHARED);
138 (void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET);
140 while (fgets(buf, sizeof (buf), hdl->libzfs_sharetab) != NULL) {
142 /* the mountpoint is the first entry on each line */
143 if ((tab = strchr(buf, '\t')) == NULL)
147 if (strcmp(buf, mountpoint) == 0) {
150 * the protocol field is the third field
151 * skip over second field
154 if ((tab = strchr(ptr, '\t')) == NULL)
157 if ((tab = strchr(ptr, '\t')) == NULL)
161 proto_table[proto].p_name) == 0) {
172 if (proto == PROTO_NFS)
178 return (SHARED_NOT_SHARED);
183 dir_is_empty_stat(const char *dirname)
188 * We only want to return false if the given path is a non empty
189 * directory, all other errors are handled elsewhere.
191 if (stat(dirname, &st) < 0 || !S_ISDIR(st.st_mode)) {
196 * An empty directory will still have two entries in it, one
197 * entry for each of "." and "..".
199 if (st.st_size > 2) {
207 dir_is_empty_readdir(const char *dirname)
213 if ((dirfd = openat(AT_FDCWD, dirname,
214 O_RDONLY | O_NDELAY | O_LARGEFILE | O_CLOEXEC, 0)) < 0) {
218 if ((dirp = fdopendir(dirfd)) == NULL) {
222 while ((dp = readdir64(dirp)) != NULL) {
224 if (strcmp(dp->d_name, ".") == 0 ||
225 strcmp(dp->d_name, "..") == 0)
228 (void) closedir(dirp);
232 (void) closedir(dirp);
237 * Returns true if the specified directory is empty. If we can't open the
238 * directory at all, return true so that the mount can fail with a more
239 * informative error message.
242 dir_is_empty(const char *dirname)
247 * If the statvfs call fails or the filesystem is not a ZFS
248 * filesystem, fall back to the slow path which uses readdir.
250 if ((statvfs64(dirname, &st) != 0) ||
251 (strcmp(st.f_basetype, "zfs") != 0)) {
252 return (dir_is_empty_readdir(dirname));
256 * At this point, we know the provided path is on a ZFS
257 * filesystem, so we can use stat instead of readdir to
258 * determine if the directory is empty or not. We try to avoid
259 * using readdir because that requires opening "dirname"; this
260 * open file descriptor can potentially end up in a child
261 * process if there's a concurrent fork, thus preventing the
262 * zfs_mount() from otherwise succeeding (the open file
263 * descriptor inherited by the child process will cause the
264 * parent's mount to fail with EBUSY). The performance
265 * implications of replacing the open, read, and close with a
266 * single stat is nice; but is not the main motivation for the
269 return (dir_is_empty_stat(dirname));
274 * Checks to see if the mount is active. If the filesystem is mounted, we fill
275 * in 'where' with the current mountpoint, and return 1. Otherwise, we return
279 is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where)
283 if (libzfs_mnttab_find(zfs_hdl, special, &entry) != 0)
287 *where = zfs_strdup(zfs_hdl, entry.mnt_mountp);
293 zfs_is_mounted(zfs_handle_t *zhp, char **where)
295 return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where));
299 * Returns true if the given dataset is mountable, false otherwise. Returns the
300 * mountpoint in 'buf'.
303 zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen,
304 zprop_source_t *source)
306 char sourceloc[MAXNAMELEN];
307 zprop_source_t sourcetype;
309 if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type))
312 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen,
313 &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0);
315 if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 ||
316 strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0)
319 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF)
322 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
323 getzoneid() == GLOBAL_ZONEID)
327 *source = sourcetype;
333 * Mount the given filesystem.
336 zfs_mount(zfs_handle_t *zhp, const char *options, int flags)
339 char mountpoint[ZFS_MAXPROPLEN];
340 char mntopts[MNT_LINE_MAX];
341 libzfs_handle_t *hdl = zhp->zfs_hdl;
346 (void) strlcpy(mntopts, options, sizeof (mntopts));
349 * If the pool is imported read-only then all mounts must be read-only
351 if (zpool_get_prop_int(zhp->zpool_hdl, ZPOOL_PROP_READONLY, NULL))
354 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
357 /* Create the directory if it doesn't already exist */
358 if (lstat(mountpoint, &buf) != 0) {
359 if (mkdirp(mountpoint, 0755) != 0) {
360 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
361 "failed to create mountpoint"));
362 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
363 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
368 #ifdef illumos /* FreeBSD: overlay mounts are not checked. */
370 * Determine if the mountpoint is empty. If so, refuse to perform the
371 * mount. We don't perform this check if MS_OVERLAY is specified, which
372 * would defeat the point. We also avoid this check if 'remount' is
375 if ((flags & MS_OVERLAY) == 0 &&
376 strstr(mntopts, MNTOPT_REMOUNT) == NULL &&
377 !dir_is_empty(mountpoint)) {
378 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
379 "directory is not empty"));
380 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
381 dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint));
385 /* perform the mount */
386 if (zmount(zfs_get_name(zhp), mountpoint, flags,
387 MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) {
389 * Generic errors are nasty, but there are just way too many
390 * from mount(), and they're well-understood. We pick a few
391 * common ones to improve upon.
393 if (errno == EBUSY) {
394 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
395 "mountpoint or dataset is busy"));
396 } else if (errno == EPERM) {
397 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
398 "Insufficient privileges"));
399 } else if (errno == ENOTSUP) {
403 VERIFY(zfs_spa_version(zhp, &spa_version) == 0);
404 (void) snprintf(buf, sizeof (buf),
405 dgettext(TEXT_DOMAIN, "Can't mount a version %lld "
406 "file system on a version %d pool. Pool must be"
407 " upgraded to mount this file system."),
408 (u_longlong_t)zfs_prop_get_int(zhp,
409 ZFS_PROP_VERSION), spa_version);
410 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, buf));
412 zfs_error_aux(hdl, strerror(errno));
414 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
415 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
419 /* add the mounted entry into our cache */
420 libzfs_mnttab_add(hdl, zfs_get_name(zhp), mountpoint,
426 * Unmount a single filesystem.
429 unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags)
431 if (umount2(mountpoint, flags) != 0) {
432 zfs_error_aux(hdl, strerror(errno));
433 return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED,
434 dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
442 * Unmount the given filesystem.
445 zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
447 libzfs_handle_t *hdl = zhp->zfs_hdl;
451 /* check to see if we need to unmount the filesystem */
452 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
453 libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)) {
455 * mountpoint may have come from a call to
456 * getmnt/getmntany if it isn't NULL. If it is NULL,
457 * we know it comes from libzfs_mnttab_find which can
458 * then get freed later. We strdup it to play it safe.
460 if (mountpoint == NULL)
461 mntpt = zfs_strdup(hdl, entry.mnt_mountp);
463 mntpt = zfs_strdup(hdl, mountpoint);
466 * Unshare and unmount the filesystem
468 if (zfs_unshare_proto(zhp, mntpt, share_all_proto) != 0)
471 if (unmount_one(hdl, mntpt, flags) != 0) {
473 (void) zfs_shareall(zhp);
476 libzfs_mnttab_remove(hdl, zhp->zfs_name);
484 * Unmount this filesystem and any children inheriting the mountpoint property.
485 * To do this, just act like we're changing the mountpoint property, but don't
486 * remount the filesystems afterwards.
489 zfs_unmountall(zfs_handle_t *zhp, int flags)
491 prop_changelist_t *clp;
494 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, 0, flags);
498 ret = changelist_prefix(clp);
499 changelist_free(clp);
505 zfs_is_shared(zfs_handle_t *zhp)
507 zfs_share_type_t rc = 0;
508 zfs_share_proto_t *curr_proto;
510 if (ZFS_IS_VOLUME(zhp))
513 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
515 rc |= zfs_is_shared_proto(zhp, NULL, *curr_proto);
517 return (rc ? B_TRUE : B_FALSE);
521 zfs_share(zfs_handle_t *zhp)
523 assert(!ZFS_IS_VOLUME(zhp));
524 return (zfs_share_proto(zhp, share_all_proto));
528 zfs_unshare(zfs_handle_t *zhp)
530 assert(!ZFS_IS_VOLUME(zhp));
531 return (zfs_unshareall(zhp));
535 * Check to see if the filesystem is currently shared.
538 zfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto)
543 if (!zfs_is_mounted(zhp, &mountpoint))
544 return (SHARED_NOT_SHARED);
546 if ((rc = is_shared(zhp->zfs_hdl, mountpoint, proto))
547 != SHARED_NOT_SHARED) {
555 return (SHARED_NOT_SHARED);
560 zfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
562 return (zfs_is_shared_proto(zhp, where,
563 PROTO_NFS) != SHARED_NOT_SHARED);
567 zfs_is_shared_smb(zfs_handle_t *zhp, char **where)
569 return (zfs_is_shared_proto(zhp, where,
570 PROTO_SMB) != SHARED_NOT_SHARED);
574 * Make sure things will work if libshare isn't installed by using
575 * wrapper functions that check to see that the pointers to functions
576 * initialized in _zfs_init_libshare() are actually present.
580 static sa_handle_t (*_sa_init)(int);
581 static void (*_sa_fini)(sa_handle_t);
582 static sa_share_t (*_sa_find_share)(sa_handle_t, char *);
583 static int (*_sa_enable_share)(sa_share_t, char *);
584 static int (*_sa_disable_share)(sa_share_t, char *);
585 static char *(*_sa_errorstr)(int);
586 static int (*_sa_parse_legacy_options)(sa_group_t, char *, char *);
587 static boolean_t (*_sa_needs_refresh)(sa_handle_t *);
588 static libzfs_handle_t *(*_sa_get_zfs_handle)(sa_handle_t);
589 static int (*_sa_zfs_process_share)(sa_handle_t, sa_group_t, sa_share_t,
590 char *, char *, zprop_source_t, char *, char *, char *);
591 static void (*_sa_update_sharetab_ts)(sa_handle_t);
595 * _zfs_init_libshare()
597 * Find the libshare.so.1 entry points that we use here and save the
598 * values to be used later. This is triggered by the runtime loader.
599 * Make sure the correct ISA version is loaded.
602 #pragma init(_zfs_init_libshare)
604 _zfs_init_libshare(void)
608 char path[MAXPATHLEN];
612 if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1)
617 (void) snprintf(path, MAXPATHLEN,
618 "/usr/lib/%s/libshare.so.1", isa);
620 if ((libshare = dlopen(path, RTLD_LAZY | RTLD_GLOBAL)) != NULL) {
621 _sa_init = (sa_handle_t (*)(int))dlsym(libshare, "sa_init");
622 _sa_fini = (void (*)(sa_handle_t))dlsym(libshare, "sa_fini");
623 _sa_find_share = (sa_share_t (*)(sa_handle_t, char *))
624 dlsym(libshare, "sa_find_share");
625 _sa_enable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
627 _sa_disable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
629 _sa_errorstr = (char *(*)(int))dlsym(libshare, "sa_errorstr");
630 _sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *))
631 dlsym(libshare, "sa_parse_legacy_options");
632 _sa_needs_refresh = (boolean_t (*)(sa_handle_t *))
633 dlsym(libshare, "sa_needs_refresh");
634 _sa_get_zfs_handle = (libzfs_handle_t *(*)(sa_handle_t))
635 dlsym(libshare, "sa_get_zfs_handle");
636 _sa_zfs_process_share = (int (*)(sa_handle_t, sa_group_t,
637 sa_share_t, char *, char *, zprop_source_t, char *,
638 char *, char *))dlsym(libshare, "sa_zfs_process_share");
639 _sa_update_sharetab_ts = (void (*)(sa_handle_t))
640 dlsym(libshare, "sa_update_sharetab_ts");
641 if (_sa_init == NULL || _sa_fini == NULL ||
642 _sa_find_share == NULL || _sa_enable_share == NULL ||
643 _sa_disable_share == NULL || _sa_errorstr == NULL ||
644 _sa_parse_legacy_options == NULL ||
645 _sa_needs_refresh == NULL || _sa_get_zfs_handle == NULL ||
646 _sa_zfs_process_share == NULL ||
647 _sa_update_sharetab_ts == NULL) {
650 _sa_disable_share = NULL;
651 _sa_enable_share = NULL;
653 _sa_parse_legacy_options = NULL;
654 (void) dlclose(libshare);
655 _sa_needs_refresh = NULL;
656 _sa_get_zfs_handle = NULL;
657 _sa_zfs_process_share = NULL;
658 _sa_update_sharetab_ts = NULL;
665 * zfs_init_libshare(zhandle, service)
667 * Initialize the libshare API if it hasn't already been initialized.
668 * In all cases it returns 0 if it succeeded and an error if not. The
669 * service value is which part(s) of the API to initialize and is a
670 * direct map to the libshare sa_init(service) interface.
673 zfs_init_libshare(libzfs_handle_t *zhandle, int service)
679 * libshare is either not installed or we're in a branded zone. The
680 * rest of the wrapper functions around the libshare calls already
681 * handle NULL function pointers, but we don't want the callers of
682 * zfs_init_libshare() to fail prematurely if libshare is not available.
684 if (_sa_init == NULL)
687 if (ret == SA_OK && zhandle->libzfs_shareflags & ZFSSHARE_MISS) {
689 * We had a cache miss. Most likely it is a new ZFS
690 * dataset that was just created. We want to make sure
691 * so check timestamps to see if a different process
692 * has updated any of the configuration. If there was
693 * some non-ZFS change, we need to re-initialize the
696 zhandle->libzfs_shareflags &= ~ZFSSHARE_MISS;
697 if (_sa_needs_refresh != NULL &&
698 _sa_needs_refresh(zhandle->libzfs_sharehdl)) {
699 zfs_uninit_libshare(zhandle);
700 zhandle->libzfs_sharehdl = _sa_init(service);
704 if (ret == SA_OK && zhandle && zhandle->libzfs_sharehdl == NULL)
705 zhandle->libzfs_sharehdl = _sa_init(service);
707 if (ret == SA_OK && zhandle->libzfs_sharehdl == NULL)
715 * zfs_uninit_libshare(zhandle)
717 * Uninitialize the libshare API if it hasn't already been
718 * uninitialized. It is OK to call multiple times.
721 zfs_uninit_libshare(libzfs_handle_t *zhandle)
723 if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) {
725 if (_sa_fini != NULL)
726 _sa_fini(zhandle->libzfs_sharehdl);
728 zhandle->libzfs_sharehdl = NULL;
733 * zfs_parse_options(options, proto)
735 * Call the legacy parse interface to get the protocol specific
736 * options using the NULL arg to indicate that this is a "parse" only.
739 zfs_parse_options(char *options, zfs_share_proto_t proto)
742 if (_sa_parse_legacy_options != NULL) {
743 return (_sa_parse_legacy_options(NULL, options,
744 proto_table[proto].p_name));
746 return (SA_CONFIG_ERR);
754 * zfs_sa_find_share(handle, path)
756 * wrapper around sa_find_share to find a share path in the
760 zfs_sa_find_share(sa_handle_t handle, char *path)
762 if (_sa_find_share != NULL)
763 return (_sa_find_share(handle, path));
768 * zfs_sa_enable_share(share, proto)
770 * Wrapper for sa_enable_share which enables a share for a specified
774 zfs_sa_enable_share(sa_share_t share, char *proto)
776 if (_sa_enable_share != NULL)
777 return (_sa_enable_share(share, proto));
778 return (SA_CONFIG_ERR);
782 * zfs_sa_disable_share(share, proto)
784 * Wrapper for sa_enable_share which disables a share for a specified
788 zfs_sa_disable_share(sa_share_t share, char *proto)
790 if (_sa_disable_share != NULL)
791 return (_sa_disable_share(share, proto));
792 return (SA_CONFIG_ERR);
797 * Share the given filesystem according to the options in the specified
798 * protocol specific properties (sharenfs, sharesmb). We rely
799 * on "libshare" to the dirty work for us.
802 zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
804 char mountpoint[ZFS_MAXPROPLEN];
805 char shareopts[ZFS_MAXPROPLEN];
806 char sourcestr[ZFS_MAXPROPLEN];
807 libzfs_handle_t *hdl = zhp->zfs_hdl;
808 zfs_share_proto_t *curr_proto;
809 zprop_source_t sourcetype;
812 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
815 for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) {
817 * Return success if there are no share options.
819 if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop,
820 shareopts, sizeof (shareopts), &sourcetype, sourcestr,
821 ZFS_MAXPROPLEN, B_FALSE) != 0 ||
822 strcmp(shareopts, "off") == 0)
826 ret = zfs_init_libshare(hdl, SA_INIT_SHARE_API);
828 (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
829 dgettext(TEXT_DOMAIN, "cannot share '%s': %s"),
830 zfs_get_name(zhp), _sa_errorstr != NULL ?
831 _sa_errorstr(ret) : "");
837 * If the 'zoned' property is set, then zfs_is_mountable()
838 * will have already bailed out if we are in the global zone.
839 * But local zones cannot be NFS servers, so we ignore it for
840 * local zones as well.
842 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
846 share = zfs_sa_find_share(hdl->libzfs_sharehdl, mountpoint);
849 * This may be a new file system that was just
850 * created so isn't in the internal cache
851 * (second time through). Rather than
852 * reloading the entire configuration, we can
853 * assume ZFS has done the checking and it is
854 * safe to add this to the internal
857 if (_sa_zfs_process_share(hdl->libzfs_sharehdl,
858 NULL, NULL, mountpoint,
859 proto_table[*curr_proto].p_name, sourcetype,
860 shareopts, sourcestr, zhp->zfs_name) != SA_OK) {
861 (void) zfs_error_fmt(hdl,
862 proto_table[*curr_proto].p_share_err,
863 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
867 hdl->libzfs_shareflags |= ZFSSHARE_MISS;
868 share = zfs_sa_find_share(hdl->libzfs_sharehdl,
873 err = zfs_sa_enable_share(share,
874 proto_table[*curr_proto].p_name);
876 (void) zfs_error_fmt(hdl,
877 proto_table[*curr_proto].p_share_err,
878 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
884 if (*curr_proto != PROTO_NFS) {
885 fprintf(stderr, "Unsupported share protocol: %d.\n",
890 if (strcmp(shareopts, "on") == 0)
891 error = fsshare(ZFS_EXPORTS_PATH, mountpoint, "");
893 error = fsshare(ZFS_EXPORTS_PATH, mountpoint, shareopts);
897 (void) zfs_error_fmt(hdl,
898 proto_table[*curr_proto].p_share_err,
899 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
910 zfs_share_nfs(zfs_handle_t *zhp)
912 return (zfs_share_proto(zhp, nfs_only));
916 zfs_share_smb(zfs_handle_t *zhp)
918 return (zfs_share_proto(zhp, smb_only));
922 zfs_shareall(zfs_handle_t *zhp)
924 return (zfs_share_proto(zhp, share_all_proto));
928 * Unshare a filesystem by mountpoint.
931 unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint,
932 zfs_share_proto_t proto)
939 * Mountpoint could get trashed if libshare calls getmntany
940 * which it does during API initialization, so strdup the
943 mntpt = zfs_strdup(hdl, mountpoint);
945 /* make sure libshare initialized */
946 if ((err = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
947 free(mntpt); /* don't need the copy anymore */
948 return (zfs_error_fmt(hdl, proto_table[proto].p_unshare_err,
949 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
950 name, _sa_errorstr(err)));
953 share = zfs_sa_find_share(hdl->libzfs_sharehdl, mntpt);
954 free(mntpt); /* don't need the copy anymore */
957 err = zfs_sa_disable_share(share, proto_table[proto].p_name);
959 return (zfs_error_fmt(hdl,
960 proto_table[proto].p_unshare_err,
961 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
962 name, _sa_errorstr(err)));
965 return (zfs_error_fmt(hdl, proto_table[proto].p_unshare_err,
966 dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"),
970 char buf[MAXPATHLEN];
974 if (proto != PROTO_NFS) {
975 fprintf(stderr, "No SMB support in FreeBSD yet.\n");
979 err = fsunshare(ZFS_EXPORTS_PATH, mountpoint);
981 zfs_error_aux(hdl, "%s", strerror(err));
982 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
983 dgettext(TEXT_DOMAIN,
984 "cannot unshare '%s'"), name));
991 * Unshare the given filesystem.
994 zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint,
995 zfs_share_proto_t *proto)
997 libzfs_handle_t *hdl = zhp->zfs_hdl;
1001 /* check to see if need to unmount the filesystem */
1002 rewind(zhp->zfs_hdl->libzfs_mnttab);
1003 if (mountpoint != NULL)
1004 mountpoint = mntpt = zfs_strdup(hdl, mountpoint);
1006 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
1007 libzfs_mnttab_find(hdl, zfs_get_name(zhp), &entry) == 0)) {
1008 zfs_share_proto_t *curr_proto;
1010 if (mountpoint == NULL)
1011 mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp);
1013 for (curr_proto = proto; *curr_proto != PROTO_END;
1016 if (is_shared(hdl, mntpt, *curr_proto) &&
1017 unshare_one(hdl, zhp->zfs_name,
1018 mntpt, *curr_proto) != 0) {
1032 zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
1034 return (zfs_unshare_proto(zhp, mountpoint, nfs_only));
1038 zfs_unshare_smb(zfs_handle_t *zhp, const char *mountpoint)
1040 return (zfs_unshare_proto(zhp, mountpoint, smb_only));
1044 * Same as zfs_unmountall(), but for NFS and SMB unshares.
1047 zfs_unshareall_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
1049 prop_changelist_t *clp;
1052 clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0, 0);
1056 ret = changelist_unshare(clp, proto);
1057 changelist_free(clp);
1063 zfs_unshareall_nfs(zfs_handle_t *zhp)
1065 return (zfs_unshareall_proto(zhp, nfs_only));
1069 zfs_unshareall_smb(zfs_handle_t *zhp)
1071 return (zfs_unshareall_proto(zhp, smb_only));
1075 zfs_unshareall(zfs_handle_t *zhp)
1077 return (zfs_unshareall_proto(zhp, share_all_proto));
1081 zfs_unshareall_bypath(zfs_handle_t *zhp, const char *mountpoint)
1083 return (zfs_unshare_proto(zhp, mountpoint, share_all_proto));
1087 * Remove the mountpoint associated with the current dataset, if necessary.
1088 * We only remove the underlying directory if:
1090 * - The mountpoint is not 'none' or 'legacy'
1091 * - The mountpoint is non-empty
1092 * - The mountpoint is the default or inherited
1093 * - The 'zoned' property is set, or we're in a local zone
1095 * Any other directories we leave alone.
1098 remove_mountpoint(zfs_handle_t *zhp)
1100 char mountpoint[ZFS_MAXPROPLEN];
1101 zprop_source_t source;
1103 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
1107 if (source == ZPROP_SRC_DEFAULT ||
1108 source == ZPROP_SRC_INHERITED) {
1110 * Try to remove the directory, silently ignoring any errors.
1111 * The filesystem may have since been removed or moved around,
1112 * and this error isn't really useful to the administrator in
1115 (void) rmdir(mountpoint);
1120 libzfs_add_handle(get_all_cb_t *cbp, zfs_handle_t *zhp)
1122 if (cbp->cb_alloc == cbp->cb_used) {
1126 newsz = cbp->cb_alloc ? cbp->cb_alloc * 2 : 64;
1127 ptr = zfs_realloc(zhp->zfs_hdl,
1128 cbp->cb_handles, cbp->cb_alloc * sizeof (void *),
1129 newsz * sizeof (void *));
1130 cbp->cb_handles = ptr;
1131 cbp->cb_alloc = newsz;
1133 cbp->cb_handles[cbp->cb_used++] = zhp;
1137 mount_cb(zfs_handle_t *zhp, void *data)
1139 get_all_cb_t *cbp = data;
1141 if (!(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM)) {
1146 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_NOAUTO) {
1152 * If this filesystem is inconsistent and has a receive resume
1153 * token, we can not mount it.
1155 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
1156 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
1157 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
1162 libzfs_add_handle(cbp, zhp);
1163 if (zfs_iter_filesystems(zhp, mount_cb, cbp) != 0) {
1171 libzfs_dataset_cmp(const void *a, const void *b)
1173 zfs_handle_t **za = (zfs_handle_t **)a;
1174 zfs_handle_t **zb = (zfs_handle_t **)b;
1175 char mounta[MAXPATHLEN];
1176 char mountb[MAXPATHLEN];
1177 boolean_t gota, gotb;
1179 if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
1180 verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
1181 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
1182 if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
1183 verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
1184 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
1187 return (strcmp(mounta, mountb));
1194 return (strcmp(zfs_get_name(a), zfs_get_name(b)));
1198 * Mount and share all datasets within the given pool. This assumes that no
1199 * datasets within the pool are currently mounted. Because users can create
1200 * complicated nested hierarchies of mountpoints, we first gather all the
1201 * datasets and mountpoints within the pool, and sort them by mountpoint. Once
1202 * we have the list of all filesystems, we iterate over them in order and mount
1203 * and/or share each one.
1205 #pragma weak zpool_mount_datasets = zpool_enable_datasets
1207 zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
1209 get_all_cb_t cb = { 0 };
1210 libzfs_handle_t *hdl = zhp->zpool_hdl;
1216 * Gather all non-snap datasets within the pool.
1218 if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_DATASET)) == NULL)
1221 libzfs_add_handle(&cb, zfsp);
1222 if (zfs_iter_filesystems(zfsp, mount_cb, &cb) != 0)
1225 * Sort the datasets by mountpoint.
1227 qsort(cb.cb_handles, cb.cb_used, sizeof (void *),
1228 libzfs_dataset_cmp);
1231 * And mount all the datasets, keeping track of which ones
1232 * succeeded or failed.
1234 if ((good = zfs_alloc(zhp->zpool_hdl,
1235 cb.cb_used * sizeof (int))) == NULL)
1239 for (i = 0; i < cb.cb_used; i++) {
1240 if (zfs_mount(cb.cb_handles[i], mntopts, flags) != 0)
1247 * Then share all the ones that need to be shared. This needs
1248 * to be a separate pass in order to avoid excessive reloading
1249 * of the configuration. Good should never be NULL since
1250 * zfs_alloc is supposed to exit if memory isn't available.
1252 for (i = 0; i < cb.cb_used; i++) {
1253 if (good[i] && zfs_share(cb.cb_handles[i]) != 0)
1260 for (i = 0; i < cb.cb_used; i++)
1261 zfs_close(cb.cb_handles[i]);
1262 free(cb.cb_handles);
1268 mountpoint_compare(const void *a, const void *b)
1270 const char *mounta = *((char **)a);
1271 const char *mountb = *((char **)b);
1273 return (strcmp(mountb, mounta));
1276 /* alias for 2002/240 */
1277 #pragma weak zpool_unmount_datasets = zpool_disable_datasets
1279 * Unshare and unmount all datasets within the given pool. We don't want to
1280 * rely on traversing the DSL to discover the filesystems within the pool,
1281 * because this may be expensive (if not all of them are mounted), and can fail
1282 * arbitrarily (on I/O error, for example). Instead, we walk /etc/mnttab and
1283 * gather all the filesystems that are currently mounted.
1286 zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
1289 struct mnttab entry;
1291 char **mountpoints = NULL;
1292 zfs_handle_t **datasets = NULL;
1293 libzfs_handle_t *hdl = zhp->zpool_hdl;
1296 int flags = (force ? MS_FORCE : 0);
1298 namelen = strlen(zhp->zpool_name);
1300 rewind(hdl->libzfs_mnttab);
1302 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
1304 * Ignore non-ZFS entries.
1306 if (entry.mnt_fstype == NULL ||
1307 strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
1311 * Ignore filesystems not within this pool.
1313 if (entry.mnt_mountp == NULL ||
1314 strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 ||
1315 (entry.mnt_special[namelen] != '/' &&
1316 entry.mnt_special[namelen] != '\0'))
1320 * At this point we've found a filesystem within our pool. Add
1321 * it to our growing list.
1323 if (used == alloc) {
1325 if ((mountpoints = zfs_alloc(hdl,
1326 8 * sizeof (void *))) == NULL)
1329 if ((datasets = zfs_alloc(hdl,
1330 8 * sizeof (void *))) == NULL)
1337 if ((ptr = zfs_realloc(hdl, mountpoints,
1338 alloc * sizeof (void *),
1339 alloc * 2 * sizeof (void *))) == NULL)
1343 if ((ptr = zfs_realloc(hdl, datasets,
1344 alloc * sizeof (void *),
1345 alloc * 2 * sizeof (void *))) == NULL)
1353 if ((mountpoints[used] = zfs_strdup(hdl,
1354 entry.mnt_mountp)) == NULL)
1358 * This is allowed to fail, in case there is some I/O error. It
1359 * is only used to determine if we need to remove the underlying
1360 * mountpoint, so failure is not fatal.
1362 datasets[used] = make_dataset_handle(hdl, entry.mnt_special);
1368 * At this point, we have the entire list of filesystems, so sort it by
1371 qsort(mountpoints, used, sizeof (char *), mountpoint_compare);
1374 * Walk through and first unshare everything.
1376 for (i = 0; i < used; i++) {
1377 zfs_share_proto_t *curr_proto;
1378 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
1380 if (is_shared(hdl, mountpoints[i], *curr_proto) &&
1381 unshare_one(hdl, mountpoints[i],
1382 mountpoints[i], *curr_proto) != 0)
1388 * Now unmount everything, removing the underlying directories as
1391 for (i = 0; i < used; i++) {
1392 if (unmount_one(hdl, mountpoints[i], flags) != 0)
1396 for (i = 0; i < used; i++) {
1398 remove_mountpoint(datasets[i]);
1403 for (i = 0; i < used; i++) {
1405 zfs_close(datasets[i]);
1406 free(mountpoints[i]);