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.
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) {
223 while ((dp = readdir64(dirp)) != NULL) {
225 if (strcmp(dp->d_name, ".") == 0 ||
226 strcmp(dp->d_name, "..") == 0)
229 (void) closedir(dirp);
233 (void) closedir(dirp);
238 * Returns true if the specified directory is empty. If we can't open the
239 * directory at all, return true so that the mount can fail with a more
240 * informative error message.
243 dir_is_empty(const char *dirname)
248 * If the statvfs call fails or the filesystem is not a ZFS
249 * filesystem, fall back to the slow path which uses readdir.
251 if ((statvfs64(dirname, &st) != 0) ||
252 (strcmp(st.f_basetype, "zfs") != 0)) {
253 return (dir_is_empty_readdir(dirname));
257 * At this point, we know the provided path is on a ZFS
258 * filesystem, so we can use stat instead of readdir to
259 * determine if the directory is empty or not. We try to avoid
260 * using readdir because that requires opening "dirname"; this
261 * open file descriptor can potentially end up in a child
262 * process if there's a concurrent fork, thus preventing the
263 * zfs_mount() from otherwise succeeding (the open file
264 * descriptor inherited by the child process will cause the
265 * parent's mount to fail with EBUSY). The performance
266 * implications of replacing the open, read, and close with a
267 * single stat is nice; but is not the main motivation for the
270 return (dir_is_empty_stat(dirname));
275 * Checks to see if the mount is active. If the filesystem is mounted, we fill
276 * in 'where' with the current mountpoint, and return 1. Otherwise, we return
280 is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where)
284 if (libzfs_mnttab_find(zfs_hdl, special, &entry) != 0)
288 *where = zfs_strdup(zfs_hdl, entry.mnt_mountp);
294 zfs_is_mounted(zfs_handle_t *zhp, char **where)
296 return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where));
300 * Returns true if the given dataset is mountable, false otherwise. Returns the
301 * mountpoint in 'buf'.
304 zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen,
305 zprop_source_t *source)
307 char sourceloc[MAXNAMELEN];
308 zprop_source_t sourcetype;
310 if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type))
313 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen,
314 &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0);
316 if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 ||
317 strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0)
320 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF)
323 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
324 getzoneid() == GLOBAL_ZONEID)
328 *source = sourcetype;
334 * Mount the given filesystem.
337 zfs_mount(zfs_handle_t *zhp, const char *options, int flags)
340 char mountpoint[ZFS_MAXPROPLEN];
341 char mntopts[MNT_LINE_MAX];
342 libzfs_handle_t *hdl = zhp->zfs_hdl;
347 (void) strlcpy(mntopts, options, sizeof (mntopts));
350 * If the pool is imported read-only then all mounts must be read-only
352 if (zpool_get_prop_int(zhp->zpool_hdl, ZPOOL_PROP_READONLY, NULL))
355 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
358 /* Create the directory if it doesn't already exist */
359 if (lstat(mountpoint, &buf) != 0) {
360 if (mkdirp(mountpoint, 0755) != 0) {
361 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
362 "failed to create mountpoint"));
363 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
364 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
369 #ifdef illumos /* FreeBSD: overlay mounts are not checked. */
371 * Determine if the mountpoint is empty. If so, refuse to perform the
372 * mount. We don't perform this check if MS_OVERLAY is specified, which
373 * would defeat the point. We also avoid this check if 'remount' is
376 if ((flags & MS_OVERLAY) == 0 &&
377 strstr(mntopts, MNTOPT_REMOUNT) == NULL &&
378 !dir_is_empty(mountpoint)) {
379 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
380 "directory is not empty"));
381 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
382 dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint));
386 /* perform the mount */
387 if (zmount(zfs_get_name(zhp), mountpoint, flags,
388 MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) {
390 * Generic errors are nasty, but there are just way too many
391 * from mount(), and they're well-understood. We pick a few
392 * common ones to improve upon.
394 if (errno == EBUSY) {
395 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
396 "mountpoint or dataset is busy"));
397 } else if (errno == EPERM) {
398 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
399 "Insufficient privileges"));
400 } else if (errno == ENOTSUP) {
404 VERIFY(zfs_spa_version(zhp, &spa_version) == 0);
405 (void) snprintf(buf, sizeof (buf),
406 dgettext(TEXT_DOMAIN, "Can't mount a version %lld "
407 "file system on a version %d pool. Pool must be"
408 " upgraded to mount this file system."),
409 (u_longlong_t)zfs_prop_get_int(zhp,
410 ZFS_PROP_VERSION), spa_version);
411 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, buf));
413 zfs_error_aux(hdl, strerror(errno));
415 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
416 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
420 /* add the mounted entry into our cache */
421 libzfs_mnttab_add(hdl, zfs_get_name(zhp), mountpoint,
427 * Unmount a single filesystem.
430 unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags)
432 if (umount2(mountpoint, flags) != 0) {
433 zfs_error_aux(hdl, strerror(errno));
434 return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED,
435 dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
443 * Unmount the given filesystem.
446 zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
448 libzfs_handle_t *hdl = zhp->zfs_hdl;
452 /* check to see if we need to unmount the filesystem */
453 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
454 libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)) {
456 * mountpoint may have come from a call to
457 * getmnt/getmntany if it isn't NULL. If it is NULL,
458 * we know it comes from libzfs_mnttab_find which can
459 * then get freed later. We strdup it to play it safe.
461 if (mountpoint == NULL)
462 mntpt = zfs_strdup(hdl, entry.mnt_mountp);
464 mntpt = zfs_strdup(hdl, mountpoint);
467 * Unshare and unmount the filesystem
469 if (zfs_unshare_proto(zhp, mntpt, share_all_proto) != 0)
472 if (unmount_one(hdl, mntpt, flags) != 0) {
474 (void) zfs_shareall(zhp);
477 libzfs_mnttab_remove(hdl, zhp->zfs_name);
485 * Unmount this filesystem and any children inheriting the mountpoint property.
486 * To do this, just act like we're changing the mountpoint property, but don't
487 * remount the filesystems afterwards.
490 zfs_unmountall(zfs_handle_t *zhp, int flags)
492 prop_changelist_t *clp;
495 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, 0, flags);
499 ret = changelist_prefix(clp);
500 changelist_free(clp);
506 zfs_is_shared(zfs_handle_t *zhp)
508 zfs_share_type_t rc = 0;
509 zfs_share_proto_t *curr_proto;
511 if (ZFS_IS_VOLUME(zhp))
514 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
516 rc |= zfs_is_shared_proto(zhp, NULL, *curr_proto);
518 return (rc ? B_TRUE : B_FALSE);
522 zfs_share(zfs_handle_t *zhp)
524 assert(!ZFS_IS_VOLUME(zhp));
525 return (zfs_share_proto(zhp, share_all_proto));
529 zfs_unshare(zfs_handle_t *zhp)
531 assert(!ZFS_IS_VOLUME(zhp));
532 return (zfs_unshareall(zhp));
536 * Check to see if the filesystem is currently shared.
539 zfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto)
544 if (!zfs_is_mounted(zhp, &mountpoint))
545 return (SHARED_NOT_SHARED);
547 if ((rc = is_shared(zhp->zfs_hdl, mountpoint, proto))
548 != SHARED_NOT_SHARED) {
556 return (SHARED_NOT_SHARED);
561 zfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
563 return (zfs_is_shared_proto(zhp, where,
564 PROTO_NFS) != SHARED_NOT_SHARED);
568 zfs_is_shared_smb(zfs_handle_t *zhp, char **where)
570 return (zfs_is_shared_proto(zhp, where,
571 PROTO_SMB) != SHARED_NOT_SHARED);
575 * Make sure things will work if libshare isn't installed by using
576 * wrapper functions that check to see that the pointers to functions
577 * initialized in _zfs_init_libshare() are actually present.
581 static sa_handle_t (*_sa_init)(int);
582 static sa_handle_t (*_sa_init_arg)(int, void *);
583 static void (*_sa_fini)(sa_handle_t);
584 static sa_share_t (*_sa_find_share)(sa_handle_t, char *);
585 static int (*_sa_enable_share)(sa_share_t, char *);
586 static int (*_sa_disable_share)(sa_share_t, char *);
587 static char *(*_sa_errorstr)(int);
588 static int (*_sa_parse_legacy_options)(sa_group_t, char *, char *);
589 static boolean_t (*_sa_needs_refresh)(sa_handle_t *);
590 static libzfs_handle_t *(*_sa_get_zfs_handle)(sa_handle_t);
591 static int (*_sa_zfs_process_share)(sa_handle_t, sa_group_t, sa_share_t,
592 char *, char *, zprop_source_t, char *, char *, char *);
593 static void (*_sa_update_sharetab_ts)(sa_handle_t);
597 * _zfs_init_libshare()
599 * Find the libshare.so.1 entry points that we use here and save the
600 * values to be used later. This is triggered by the runtime loader.
601 * Make sure the correct ISA version is loaded.
604 #pragma init(_zfs_init_libshare)
606 _zfs_init_libshare(void)
610 char path[MAXPATHLEN];
614 if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1)
619 (void) snprintf(path, MAXPATHLEN,
620 "/usr/lib/%s/libshare.so.1", isa);
622 if ((libshare = dlopen(path, RTLD_LAZY | RTLD_GLOBAL)) != NULL) {
623 _sa_init = (sa_handle_t (*)(int))dlsym(libshare, "sa_init");
624 _sa_init_arg = (sa_handle_t (*)(int, void *))dlsym(libshare,
626 _sa_fini = (void (*)(sa_handle_t))dlsym(libshare, "sa_fini");
627 _sa_find_share = (sa_share_t (*)(sa_handle_t, char *))
628 dlsym(libshare, "sa_find_share");
629 _sa_enable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
631 _sa_disable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
633 _sa_errorstr = (char *(*)(int))dlsym(libshare, "sa_errorstr");
634 _sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *))
635 dlsym(libshare, "sa_parse_legacy_options");
636 _sa_needs_refresh = (boolean_t (*)(sa_handle_t *))
637 dlsym(libshare, "sa_needs_refresh");
638 _sa_get_zfs_handle = (libzfs_handle_t *(*)(sa_handle_t))
639 dlsym(libshare, "sa_get_zfs_handle");
640 _sa_zfs_process_share = (int (*)(sa_handle_t, sa_group_t,
641 sa_share_t, char *, char *, zprop_source_t, char *,
642 char *, char *))dlsym(libshare, "sa_zfs_process_share");
643 _sa_update_sharetab_ts = (void (*)(sa_handle_t))
644 dlsym(libshare, "sa_update_sharetab_ts");
645 if (_sa_init == NULL || _sa_init_arg == NULL ||
646 _sa_fini == NULL || _sa_find_share == NULL ||
647 _sa_enable_share == NULL || _sa_disable_share == NULL ||
648 _sa_errorstr == NULL || _sa_parse_legacy_options == NULL ||
649 _sa_needs_refresh == NULL || _sa_get_zfs_handle == NULL ||
650 _sa_zfs_process_share == NULL ||
651 _sa_update_sharetab_ts == NULL) {
655 _sa_disable_share = NULL;
656 _sa_enable_share = NULL;
658 _sa_parse_legacy_options = NULL;
659 (void) dlclose(libshare);
660 _sa_needs_refresh = NULL;
661 _sa_get_zfs_handle = NULL;
662 _sa_zfs_process_share = NULL;
663 _sa_update_sharetab_ts = NULL;
670 * zfs_init_libshare(zhandle, service)
672 * Initialize the libshare API if it hasn't already been initialized.
673 * In all cases it returns 0 if it succeeded and an error if not. The
674 * service value is which part(s) of the API to initialize and is a
675 * direct map to the libshare sa_init(service) interface.
678 zfs_init_libshare_impl(libzfs_handle_t *zhandle, int service, void *arg)
682 * libshare is either not installed or we're in a branded zone. The
683 * rest of the wrapper functions around the libshare calls already
684 * handle NULL function pointers, but we don't want the callers of
685 * zfs_init_libshare() to fail prematurely if libshare is not available.
687 if (_sa_init == NULL)
691 * Attempt to refresh libshare. This is necessary if there was a cache
692 * miss for a new ZFS dataset that was just created, or if state of the
693 * sharetab file has changed since libshare was last initialized. We
694 * want to make sure so check timestamps to see if a different process
695 * has updated any of the configuration. If there was some non-ZFS
696 * change, we need to re-initialize the internal cache.
698 if (_sa_needs_refresh != NULL &&
699 _sa_needs_refresh(zhandle->libzfs_sharehdl)) {
700 zfs_uninit_libshare(zhandle);
701 zhandle->libzfs_sharehdl = _sa_init_arg(service, arg);
704 if (zhandle && zhandle->libzfs_sharehdl == NULL)
705 zhandle->libzfs_sharehdl = _sa_init_arg(service, arg);
707 if (zhandle->libzfs_sharehdl == NULL)
708 return (SA_NO_MEMORY);
714 zfs_init_libshare(libzfs_handle_t *zhandle, int service)
716 return (zfs_init_libshare_impl(zhandle, service, NULL));
720 zfs_init_libshare_arg(libzfs_handle_t *zhandle, int service, void *arg)
722 return (zfs_init_libshare_impl(zhandle, service, arg));
727 * zfs_uninit_libshare(zhandle)
729 * Uninitialize the libshare API if it hasn't already been
730 * uninitialized. It is OK to call multiple times.
733 zfs_uninit_libshare(libzfs_handle_t *zhandle)
735 if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) {
737 if (_sa_fini != NULL)
738 _sa_fini(zhandle->libzfs_sharehdl);
740 zhandle->libzfs_sharehdl = NULL;
745 * zfs_parse_options(options, proto)
747 * Call the legacy parse interface to get the protocol specific
748 * options using the NULL arg to indicate that this is a "parse" only.
751 zfs_parse_options(char *options, zfs_share_proto_t proto)
754 if (_sa_parse_legacy_options != NULL) {
755 return (_sa_parse_legacy_options(NULL, options,
756 proto_table[proto].p_name));
758 return (SA_CONFIG_ERR);
766 * zfs_sa_find_share(handle, path)
768 * wrapper around sa_find_share to find a share path in the
772 zfs_sa_find_share(sa_handle_t handle, char *path)
774 if (_sa_find_share != NULL)
775 return (_sa_find_share(handle, path));
780 * zfs_sa_enable_share(share, proto)
782 * Wrapper for sa_enable_share which enables a share for a specified
786 zfs_sa_enable_share(sa_share_t share, char *proto)
788 if (_sa_enable_share != NULL)
789 return (_sa_enable_share(share, proto));
790 return (SA_CONFIG_ERR);
794 * zfs_sa_disable_share(share, proto)
796 * Wrapper for sa_enable_share which disables a share for a specified
800 zfs_sa_disable_share(sa_share_t share, char *proto)
802 if (_sa_disable_share != NULL)
803 return (_sa_disable_share(share, proto));
804 return (SA_CONFIG_ERR);
809 * Share the given filesystem according to the options in the specified
810 * protocol specific properties (sharenfs, sharesmb). We rely
811 * on "libshare" to the dirty work for us.
814 zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
816 char mountpoint[ZFS_MAXPROPLEN];
817 char shareopts[ZFS_MAXPROPLEN];
818 char sourcestr[ZFS_MAXPROPLEN];
819 libzfs_handle_t *hdl = zhp->zfs_hdl;
820 zfs_share_proto_t *curr_proto;
821 zprop_source_t sourcetype;
824 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
827 for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) {
829 * Return success if there are no share options.
831 if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop,
832 shareopts, sizeof (shareopts), &sourcetype, sourcestr,
833 ZFS_MAXPROPLEN, B_FALSE) != 0 ||
834 strcmp(shareopts, "off") == 0)
837 ret = zfs_init_libshare_arg(hdl, SA_INIT_ONE_SHARE_FROM_HANDLE,
840 (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
841 dgettext(TEXT_DOMAIN, "cannot share '%s': %s"),
842 zfs_get_name(zhp), _sa_errorstr != NULL ?
843 _sa_errorstr(ret) : "");
849 * If the 'zoned' property is set, then zfs_is_mountable()
850 * will have already bailed out if we are in the global zone.
851 * But local zones cannot be NFS servers, so we ignore it for
852 * local zones as well.
854 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
858 share = zfs_sa_find_share(hdl->libzfs_sharehdl, mountpoint);
861 * This may be a new file system that was just
862 * created so isn't in the internal cache
863 * (second time through). Rather than
864 * reloading the entire configuration, we can
865 * assume ZFS has done the checking and it is
866 * safe to add this to the internal
869 if (_sa_zfs_process_share(hdl->libzfs_sharehdl,
870 NULL, NULL, mountpoint,
871 proto_table[*curr_proto].p_name, sourcetype,
872 shareopts, sourcestr, zhp->zfs_name) != SA_OK) {
873 (void) zfs_error_fmt(hdl,
874 proto_table[*curr_proto].p_share_err,
875 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
879 share = zfs_sa_find_share(hdl->libzfs_sharehdl,
884 err = zfs_sa_enable_share(share,
885 proto_table[*curr_proto].p_name);
887 (void) zfs_error_fmt(hdl,
888 proto_table[*curr_proto].p_share_err,
889 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
895 if (*curr_proto != PROTO_NFS) {
896 fprintf(stderr, "Unsupported share protocol: %d.\n",
901 if (strcmp(shareopts, "on") == 0)
902 error = fsshare(ZFS_EXPORTS_PATH, mountpoint, "");
904 error = fsshare(ZFS_EXPORTS_PATH, mountpoint, shareopts);
908 (void) zfs_error_fmt(hdl,
909 proto_table[*curr_proto].p_share_err,
910 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
921 zfs_share_nfs(zfs_handle_t *zhp)
923 return (zfs_share_proto(zhp, nfs_only));
927 zfs_share_smb(zfs_handle_t *zhp)
929 return (zfs_share_proto(zhp, smb_only));
933 zfs_shareall(zfs_handle_t *zhp)
935 return (zfs_share_proto(zhp, share_all_proto));
939 * Unshare a filesystem by mountpoint.
942 unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint,
943 zfs_share_proto_t proto)
951 * Mountpoint could get trashed if libshare calls getmntany
952 * which it does during API initialization, so strdup the
955 mntpt = zfs_strdup(hdl, mountpoint);
958 * make sure libshare initialized, initialize everything because we
959 * don't know what other unsharing may happen later. Functions up the
960 * stack are allowed to initialize instead a subset of shares at the
961 * time the set is known.
963 if ((err = zfs_init_libshare_arg(hdl, SA_INIT_ONE_SHARE_FROM_NAME,
964 (void *)name)) != SA_OK) {
965 free(mntpt); /* don't need the copy anymore */
966 return (zfs_error_fmt(hdl, proto_table[proto].p_unshare_err,
967 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
968 name, _sa_errorstr(err)));
971 share = zfs_sa_find_share(hdl->libzfs_sharehdl, mntpt);
972 free(mntpt); /* don't need the copy anymore */
975 err = zfs_sa_disable_share(share, proto_table[proto].p_name);
977 return (zfs_error_fmt(hdl,
978 proto_table[proto].p_unshare_err,
979 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
980 name, _sa_errorstr(err)));
983 return (zfs_error_fmt(hdl, proto_table[proto].p_unshare_err,
984 dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"),
988 char buf[MAXPATHLEN];
992 if (proto != PROTO_NFS) {
993 fprintf(stderr, "No SMB support in FreeBSD yet.\n");
997 err = fsunshare(ZFS_EXPORTS_PATH, mountpoint);
999 zfs_error_aux(hdl, "%s", strerror(err));
1000 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
1001 dgettext(TEXT_DOMAIN,
1002 "cannot unshare '%s'"), name));
1009 * Unshare the given filesystem.
1012 zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint,
1013 zfs_share_proto_t *proto)
1015 libzfs_handle_t *hdl = zhp->zfs_hdl;
1016 struct mnttab entry;
1019 /* check to see if need to unmount the filesystem */
1020 rewind(zhp->zfs_hdl->libzfs_mnttab);
1021 if (mountpoint != NULL)
1022 mountpoint = mntpt = zfs_strdup(hdl, mountpoint);
1024 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
1025 libzfs_mnttab_find(hdl, zfs_get_name(zhp), &entry) == 0)) {
1026 zfs_share_proto_t *curr_proto;
1028 if (mountpoint == NULL)
1029 mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp);
1031 for (curr_proto = proto; *curr_proto != PROTO_END;
1034 if (is_shared(hdl, mntpt, *curr_proto) &&
1035 unshare_one(hdl, zhp->zfs_name,
1036 mntpt, *curr_proto) != 0) {
1050 zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
1052 return (zfs_unshare_proto(zhp, mountpoint, nfs_only));
1056 zfs_unshare_smb(zfs_handle_t *zhp, const char *mountpoint)
1058 return (zfs_unshare_proto(zhp, mountpoint, smb_only));
1062 * Same as zfs_unmountall(), but for NFS and SMB unshares.
1065 zfs_unshareall_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
1067 prop_changelist_t *clp;
1070 clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0, 0);
1074 ret = changelist_unshare(clp, proto);
1075 changelist_free(clp);
1081 zfs_unshareall_nfs(zfs_handle_t *zhp)
1083 return (zfs_unshareall_proto(zhp, nfs_only));
1087 zfs_unshareall_smb(zfs_handle_t *zhp)
1089 return (zfs_unshareall_proto(zhp, smb_only));
1093 zfs_unshareall(zfs_handle_t *zhp)
1095 return (zfs_unshareall_proto(zhp, share_all_proto));
1099 zfs_unshareall_bypath(zfs_handle_t *zhp, const char *mountpoint)
1101 return (zfs_unshare_proto(zhp, mountpoint, share_all_proto));
1105 * Remove the mountpoint associated with the current dataset, if necessary.
1106 * We only remove the underlying directory if:
1108 * - The mountpoint is not 'none' or 'legacy'
1109 * - The mountpoint is non-empty
1110 * - The mountpoint is the default or inherited
1111 * - The 'zoned' property is set, or we're in a local zone
1113 * Any other directories we leave alone.
1116 remove_mountpoint(zfs_handle_t *zhp)
1118 char mountpoint[ZFS_MAXPROPLEN];
1119 zprop_source_t source;
1121 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
1125 if (source == ZPROP_SRC_DEFAULT ||
1126 source == ZPROP_SRC_INHERITED) {
1128 * Try to remove the directory, silently ignoring any errors.
1129 * The filesystem may have since been removed or moved around,
1130 * and this error isn't really useful to the administrator in
1133 (void) rmdir(mountpoint);
1138 libzfs_add_handle(get_all_cb_t *cbp, zfs_handle_t *zhp)
1140 if (cbp->cb_alloc == cbp->cb_used) {
1144 newsz = cbp->cb_alloc ? cbp->cb_alloc * 2 : 64;
1145 ptr = zfs_realloc(zhp->zfs_hdl,
1146 cbp->cb_handles, cbp->cb_alloc * sizeof (void *),
1147 newsz * sizeof (void *));
1148 cbp->cb_handles = ptr;
1149 cbp->cb_alloc = newsz;
1151 cbp->cb_handles[cbp->cb_used++] = zhp;
1155 mount_cb(zfs_handle_t *zhp, void *data)
1157 get_all_cb_t *cbp = data;
1159 if (!(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM)) {
1164 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_NOAUTO) {
1170 * If this filesystem is inconsistent and has a receive resume
1171 * token, we can not mount it.
1173 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
1174 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
1175 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
1180 libzfs_add_handle(cbp, zhp);
1181 if (zfs_iter_filesystems(zhp, mount_cb, cbp) != 0) {
1189 libzfs_dataset_cmp(const void *a, const void *b)
1191 zfs_handle_t **za = (zfs_handle_t **)a;
1192 zfs_handle_t **zb = (zfs_handle_t **)b;
1193 char mounta[MAXPATHLEN];
1194 char mountb[MAXPATHLEN];
1195 boolean_t gota, gotb;
1197 if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
1198 verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
1199 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
1200 if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
1201 verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
1202 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
1205 return (strcmp(mounta, mountb));
1212 return (strcmp(zfs_get_name(a), zfs_get_name(b)));
1216 * Mount and share all datasets within the given pool. This assumes that no
1217 * datasets within the pool are currently mounted. Because users can create
1218 * complicated nested hierarchies of mountpoints, we first gather all the
1219 * datasets and mountpoints within the pool, and sort them by mountpoint. Once
1220 * we have the list of all filesystems, we iterate over them in order and mount
1221 * and/or share each one.
1223 #pragma weak zpool_mount_datasets = zpool_enable_datasets
1225 zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
1227 get_all_cb_t cb = { 0 };
1228 libzfs_handle_t *hdl = zhp->zpool_hdl;
1234 * Gather all non-snap datasets within the pool.
1236 if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_DATASET)) == NULL)
1239 libzfs_add_handle(&cb, zfsp);
1240 if (zfs_iter_filesystems(zfsp, mount_cb, &cb) != 0)
1243 * Sort the datasets by mountpoint.
1245 qsort(cb.cb_handles, cb.cb_used, sizeof (void *),
1246 libzfs_dataset_cmp);
1249 * And mount all the datasets, keeping track of which ones
1250 * succeeded or failed.
1252 if ((good = zfs_alloc(zhp->zpool_hdl,
1253 cb.cb_used * sizeof (int))) == NULL)
1257 for (i = 0; i < cb.cb_used; i++) {
1258 if (zfs_mount(cb.cb_handles[i], mntopts, flags) != 0)
1265 * Then share all the ones that need to be shared. This needs
1266 * to be a separate pass in order to avoid excessive reloading
1267 * of the configuration. Good should never be NULL since
1268 * zfs_alloc is supposed to exit if memory isn't available.
1270 for (i = 0; i < cb.cb_used; i++) {
1271 if (good[i] && zfs_share(cb.cb_handles[i]) != 0)
1278 for (i = 0; i < cb.cb_used; i++)
1279 zfs_close(cb.cb_handles[i]);
1280 free(cb.cb_handles);
1286 mountpoint_compare(const void *a, const void *b)
1288 const char *mounta = *((char **)a);
1289 const char *mountb = *((char **)b);
1291 return (strcmp(mountb, mounta));
1294 /* alias for 2002/240 */
1295 #pragma weak zpool_unmount_datasets = zpool_disable_datasets
1297 * Unshare and unmount all datasets within the given pool. We don't want to
1298 * rely on traversing the DSL to discover the filesystems within the pool,
1299 * because this may be expensive (if not all of them are mounted), and can fail
1300 * arbitrarily (on I/O error, for example). Instead, we walk /etc/mnttab and
1301 * gather all the filesystems that are currently mounted.
1304 zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
1307 struct mnttab entry;
1309 char **mountpoints = NULL;
1310 zfs_handle_t **datasets = NULL;
1311 libzfs_handle_t *hdl = zhp->zpool_hdl;
1314 int flags = (force ? MS_FORCE : 0);
1316 sa_init_selective_arg_t sharearg;
1319 namelen = strlen(zhp->zpool_name);
1321 rewind(hdl->libzfs_mnttab);
1323 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
1325 * Ignore non-ZFS entries.
1327 if (entry.mnt_fstype == NULL ||
1328 strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
1332 * Ignore filesystems not within this pool.
1334 if (entry.mnt_mountp == NULL ||
1335 strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 ||
1336 (entry.mnt_special[namelen] != '/' &&
1337 entry.mnt_special[namelen] != '\0'))
1341 * At this point we've found a filesystem within our pool. Add
1342 * it to our growing list.
1344 if (used == alloc) {
1346 if ((mountpoints = zfs_alloc(hdl,
1347 8 * sizeof (void *))) == NULL)
1350 if ((datasets = zfs_alloc(hdl,
1351 8 * sizeof (void *))) == NULL)
1358 if ((ptr = zfs_realloc(hdl, mountpoints,
1359 alloc * sizeof (void *),
1360 alloc * 2 * sizeof (void *))) == NULL)
1364 if ((ptr = zfs_realloc(hdl, datasets,
1365 alloc * sizeof (void *),
1366 alloc * 2 * sizeof (void *))) == NULL)
1374 if ((mountpoints[used] = zfs_strdup(hdl,
1375 entry.mnt_mountp)) == NULL)
1379 * This is allowed to fail, in case there is some I/O error. It
1380 * is only used to determine if we need to remove the underlying
1381 * mountpoint, so failure is not fatal.
1383 datasets[used] = make_dataset_handle(hdl, entry.mnt_special);
1389 * At this point, we have the entire list of filesystems, so sort it by
1393 sharearg.zhandle_arr = datasets;
1394 sharearg.zhandle_len = used;
1395 ret = zfs_init_libshare_arg(hdl, SA_INIT_SHARE_API_SELECTIVE,
1400 qsort(mountpoints, used, sizeof (char *), mountpoint_compare);
1403 * Walk through and first unshare everything.
1405 for (i = 0; i < used; i++) {
1406 zfs_share_proto_t *curr_proto;
1407 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
1409 if (is_shared(hdl, mountpoints[i], *curr_proto) &&
1410 unshare_one(hdl, mountpoints[i],
1411 mountpoints[i], *curr_proto) != 0)
1417 * Now unmount everything, removing the underlying directories as
1420 for (i = 0; i < used; i++) {
1421 if (unmount_one(hdl, mountpoints[i], flags) != 0)
1425 for (i = 0; i < used; i++) {
1427 remove_mountpoint(datasets[i]);
1432 for (i = 0; i < used; i++) {
1434 zfs_close(datasets[i]);
1435 free(mountpoints[i]);