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 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * Routines to manage ZFS mounts. We separate all the nasty routines that have
29 * to deal with the OS. The following functions are the main entry points --
30 * they are used by mount and unmount and when changing a filesystem's
38 * This file also contains the functions used to manage sharing filesystems via
47 * zfs_is_shared_iscsi()
53 * zfs_unshareall_nfs()
54 * zfs_unshareall_smb()
56 * zfs_unshareall_bypath()
59 * The following functions are available for pool consumers, and will
60 * mount/unmount and share/unshare all datasets within pool:
62 * zpool_enable_datasets()
63 * zpool_disable_datasets()
76 #include <sys/mntent.h>
77 #include <sys/mnttab.h>
78 #include <sys/mount.h>
83 #include "libzfs_impl.h"
87 #define MAXISALEN 257 /* based on sysinfo(2) man page */
89 static int zfs_share_proto(zfs_handle_t *, zfs_share_proto_t *);
90 zfs_share_type_t zfs_is_shared_proto(zfs_handle_t *, char **,
93 static int (*iscsitgt_zfs_share)(const char *);
94 static int (*iscsitgt_zfs_unshare)(const char *);
95 static int (*iscsitgt_zfs_is_shared)(const char *);
96 static int (*iscsitgt_svc_online)();
99 * The share protocols table must be in the same order as the zfs_share_prot_t
100 * enum in libzfs_impl.h
109 proto_table_t proto_table[PROTO_END] = {
110 {ZFS_PROP_SHARENFS, "nfs", EZFS_SHARENFSFAILED, EZFS_UNSHARENFSFAILED},
111 {ZFS_PROP_SHARESMB, "smb", EZFS_SHARESMBFAILED, EZFS_UNSHARESMBFAILED},
114 zfs_share_proto_t nfs_only[] = {
119 zfs_share_proto_t smb_only[] = {
123 zfs_share_proto_t share_all_proto[] = {
129 #pragma init(zfs_iscsi_init)
135 if ((libiscsitgt = dlopen("/lib/libiscsitgt.so.1",
136 RTLD_LAZY | RTLD_GLOBAL)) == NULL ||
137 (iscsitgt_zfs_share = (int (*)(const char *))dlsym(libiscsitgt,
138 "iscsitgt_zfs_share")) == NULL ||
139 (iscsitgt_zfs_unshare = (int (*)(const char *))dlsym(libiscsitgt,
140 "iscsitgt_zfs_unshare")) == NULL ||
141 (iscsitgt_zfs_is_shared = (int (*)(const char *))dlsym(libiscsitgt,
142 "iscsitgt_zfs_is_shared")) == NULL ||
143 (iscsitgt_svc_online = (int (*)(const char *))dlsym(libiscsitgt,
144 "iscsitgt_svc_online")) == NULL) {
145 iscsitgt_zfs_share = NULL;
146 iscsitgt_zfs_unshare = NULL;
147 iscsitgt_zfs_is_shared = NULL;
148 iscsitgt_svc_online = NULL;
153 * Search the sharetab for the given mountpoint and protocol, returning
154 * a zfs_share_type_t value.
156 static zfs_share_type_t
157 is_shared(libzfs_handle_t *hdl, const char *mountpoint, zfs_share_proto_t proto)
159 char buf[MAXPATHLEN], *tab;
162 if (hdl->libzfs_sharetab == NULL)
163 return (SHARED_NOT_SHARED);
165 (void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET);
167 while (fgets(buf, sizeof (buf), hdl->libzfs_sharetab) != NULL) {
169 /* the mountpoint is the first entry on each line */
170 if ((tab = strchr(buf, '\t')) == NULL)
174 if (strcmp(buf, mountpoint) == 0) {
176 * the protocol field is the third field
177 * skip over second field
180 if ((tab = strchr(ptr, '\t')) == NULL)
183 if ((tab = strchr(ptr, '\t')) == NULL)
187 proto_table[proto].p_name) == 0) {
200 return (SHARED_NOT_SHARED);
205 * Returns true if the specified directory is empty. If we can't open the
206 * directory at all, return true so that the mount can fail with a more
207 * informative error message.
210 dir_is_empty(const char *dirname)
215 if ((dirp = opendir(dirname)) == NULL)
218 while ((dp = readdir64(dirp)) != NULL) {
220 if (strcmp(dp->d_name, ".") == 0 ||
221 strcmp(dp->d_name, "..") == 0)
224 (void) closedir(dirp);
228 (void) closedir(dirp);
234 * Checks to see if the mount is active. If the filesystem is mounted, we fill
235 * in 'where' with the current mountpoint, and return 1. Otherwise, we return
239 is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where)
241 struct mnttab search = { 0 }, entry;
244 * Search for the entry in /etc/mnttab. We don't bother getting the
245 * mountpoint, as we can just search for the special device. This will
246 * also let us find mounts when the mountpoint is 'legacy'.
248 search.mnt_special = (char *)special;
249 search.mnt_fstype = MNTTYPE_ZFS;
251 rewind(zfs_hdl->libzfs_mnttab);
252 if (getmntany(zfs_hdl->libzfs_mnttab, &entry, &search) != 0)
256 *where = zfs_strdup(zfs_hdl, entry.mnt_mountp);
262 zfs_is_mounted(zfs_handle_t *zhp, char **where)
264 return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where));
268 * Returns true if the given dataset is mountable, false otherwise. Returns the
269 * mountpoint in 'buf'.
272 zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen,
273 zprop_source_t *source)
275 char sourceloc[ZFS_MAXNAMELEN];
276 zprop_source_t sourcetype;
278 if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type))
281 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen,
282 &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0);
284 if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 ||
285 strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0)
288 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF)
291 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
292 getzoneid() == GLOBAL_ZONEID)
296 *source = sourcetype;
302 * Mount the given filesystem.
305 zfs_mount(zfs_handle_t *zhp, const char *options, int flags)
308 char mountpoint[ZFS_MAXPROPLEN];
309 char mntopts[MNT_LINE_MAX];
310 libzfs_handle_t *hdl = zhp->zfs_hdl;
315 (void) strlcpy(mntopts, options, sizeof (mntopts));
317 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
320 /* Create the directory if it doesn't already exist */
321 if (lstat(mountpoint, &buf) != 0) {
322 if (mkdirp(mountpoint, 0755) != 0) {
323 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
324 "failed to create mountpoint"));
325 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
326 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
331 #if 0 /* FreeBSD: overlay mounts are not checked. */
333 * Determine if the mountpoint is empty. If so, refuse to perform the
334 * mount. We don't perform this check if MS_OVERLAY is specified, which
335 * would defeat the point. We also avoid this check if 'remount' is
338 if ((flags & MS_OVERLAY) == 0 &&
339 strstr(mntopts, MNTOPT_REMOUNT) == NULL &&
340 !dir_is_empty(mountpoint)) {
341 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
342 "directory is not empty"));
343 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
344 dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint));
348 /* perform the mount */
349 if (zmount(zfs_get_name(zhp), mountpoint, flags,
350 MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) {
352 * Generic errors are nasty, but there are just way too many
353 * from mount(), and they're well-understood. We pick a few
354 * common ones to improve upon.
356 if (errno == EBUSY) {
357 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
358 "mountpoint or dataset is busy"));
359 } else if (errno == EPERM) {
360 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
361 "Insufficient privileges"));
363 zfs_error_aux(hdl, strerror(errno));
366 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
367 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
375 * Unmount a single filesystem.
378 unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags)
380 if (unmount(mountpoint, flags) != 0) {
381 zfs_error_aux(hdl, strerror(errno));
382 return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED,
383 dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
391 * Unmount the given filesystem.
394 zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
396 struct mnttab search = { 0 }, entry;
399 /* check to see if need to unmount the filesystem */
400 search.mnt_special = zhp->zfs_name;
401 search.mnt_fstype = MNTTYPE_ZFS;
402 rewind(zhp->zfs_hdl->libzfs_mnttab);
403 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
404 getmntany(zhp->zfs_hdl->libzfs_mnttab, &entry, &search) == 0)) {
407 * mountpoint may have come from a call to
408 * getmnt/getmntany if it isn't NULL. If it is NULL,
409 * we know it comes from getmntany which can then get
410 * overwritten later. We strdup it to play it safe.
412 if (mountpoint == NULL)
413 mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp);
415 mntpt = zfs_strdup(zhp->zfs_hdl, mountpoint);
418 * Unshare and unmount the filesystem
420 if (zfs_unshare_proto(zhp, mntpt, share_all_proto) != 0)
423 if (unmount_one(zhp->zfs_hdl, mntpt, flags) != 0) {
425 (void) zfs_shareall(zhp);
435 * Unmount this filesystem and any children inheriting the mountpoint property.
436 * To do this, just act like we're changing the mountpoint property, but don't
437 * remount the filesystems afterwards.
440 zfs_unmountall(zfs_handle_t *zhp, int flags)
442 prop_changelist_t *clp;
445 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, 0, flags);
449 ret = changelist_prefix(clp);
450 changelist_free(clp);
456 zfs_is_shared(zfs_handle_t *zhp)
458 zfs_share_type_t rc = 0;
459 zfs_share_proto_t *curr_proto;
461 if (ZFS_IS_VOLUME(zhp))
462 return (zfs_is_shared_iscsi(zhp));
464 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
466 rc |= zfs_is_shared_proto(zhp, NULL, *curr_proto);
468 return (rc ? B_TRUE : B_FALSE);
472 zfs_share(zfs_handle_t *zhp)
474 if (ZFS_IS_VOLUME(zhp))
475 return (zfs_share_iscsi(zhp));
477 return (zfs_share_proto(zhp, share_all_proto));
481 zfs_unshare(zfs_handle_t *zhp)
483 if (ZFS_IS_VOLUME(zhp))
484 return (zfs_unshare_iscsi(zhp));
486 return (zfs_unshareall(zhp));
490 * Check to see if the filesystem is currently shared.
493 zfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto)
498 if (!zfs_is_mounted(zhp, &mountpoint))
499 return (SHARED_NOT_SHARED);
501 if (rc = is_shared(zhp->zfs_hdl, mountpoint, proto)) {
509 return (SHARED_NOT_SHARED);
514 zfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
516 return (zfs_is_shared_proto(zhp, where,
517 PROTO_NFS) != SHARED_NOT_SHARED);
521 zfs_is_shared_smb(zfs_handle_t *zhp, char **where)
523 return (zfs_is_shared_proto(zhp, where,
524 PROTO_SMB) != SHARED_NOT_SHARED);
528 * Make sure things will work if libshare isn't installed by using
529 * wrapper functions that check to see that the pointers to functions
530 * initialized in _zfs_init_libshare() are actually present.
534 static sa_handle_t (*_sa_init)(int);
535 static void (*_sa_fini)(sa_handle_t);
536 static sa_share_t (*_sa_find_share)(sa_handle_t, char *);
537 static int (*_sa_enable_share)(sa_share_t, char *);
538 static int (*_sa_disable_share)(sa_share_t, char *);
539 static char *(*_sa_errorstr)(int);
540 static int (*_sa_parse_legacy_options)(sa_group_t, char *, char *);
541 static boolean_t (*_sa_needs_refresh)(sa_handle_t *);
542 static libzfs_handle_t *(*_sa_get_zfs_handle)(sa_handle_t);
543 static int (*_sa_zfs_process_share)(sa_handle_t, sa_group_t, sa_share_t,
544 char *, char *, zprop_source_t, char *, char *, char *);
545 static void (*_sa_update_sharetab_ts)(sa_handle_t);
549 * _zfs_init_libshare()
551 * Find the libshare.so.1 entry points that we use here and save the
552 * values to be used later. This is triggered by the runtime loader.
553 * Make sure the correct ISA version is loaded.
556 #pragma init(_zfs_init_libshare)
558 _zfs_init_libshare(void)
562 char path[MAXPATHLEN];
566 if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1)
571 (void) snprintf(path, MAXPATHLEN,
572 "/usr/lib/%s/libshare.so.1", isa);
574 if ((libshare = dlopen(path, RTLD_LAZY | RTLD_GLOBAL)) != NULL) {
575 _sa_init = (sa_handle_t (*)(int))dlsym(libshare, "sa_init");
576 _sa_fini = (void (*)(sa_handle_t))dlsym(libshare, "sa_fini");
577 _sa_find_share = (sa_share_t (*)(sa_handle_t, char *))
578 dlsym(libshare, "sa_find_share");
579 _sa_enable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
581 _sa_disable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
583 _sa_errorstr = (char *(*)(int))dlsym(libshare, "sa_errorstr");
584 _sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *))
585 dlsym(libshare, "sa_parse_legacy_options");
586 _sa_needs_refresh = (boolean_t (*)(sa_handle_t *))
587 dlsym(libshare, "sa_needs_refresh");
588 _sa_get_zfs_handle = (libzfs_handle_t *(*)(sa_handle_t))
589 dlsym(libshare, "sa_get_zfs_handle");
590 _sa_zfs_process_share = (int (*)(sa_handle_t, sa_group_t,
591 sa_share_t, char *, char *, zprop_source_t, char *,
592 char *, char *))dlsym(libshare, "sa_zfs_process_share");
593 _sa_update_sharetab_ts = (void (*)(sa_handle_t))
594 dlsym(libshare, "sa_update_sharetab_ts");
595 if (_sa_init == NULL || _sa_fini == NULL ||
596 _sa_find_share == NULL || _sa_enable_share == NULL ||
597 _sa_disable_share == NULL || _sa_errorstr == NULL ||
598 _sa_parse_legacy_options == NULL ||
599 _sa_needs_refresh == NULL || _sa_get_zfs_handle == NULL ||
600 _sa_zfs_process_share == NULL ||
601 _sa_update_sharetab_ts == NULL) {
604 _sa_disable_share = NULL;
605 _sa_enable_share = NULL;
607 _sa_parse_legacy_options = NULL;
608 (void) dlclose(libshare);
609 _sa_needs_refresh = NULL;
610 _sa_get_zfs_handle = NULL;
611 _sa_zfs_process_share = NULL;
612 _sa_update_sharetab_ts = NULL;
619 * zfs_init_libshare(zhandle, service)
621 * Initialize the libshare API if it hasn't already been initialized.
622 * In all cases it returns 0 if it succeeded and an error if not. The
623 * service value is which part(s) of the API to initialize and is a
624 * direct map to the libshare sa_init(service) interface.
627 zfs_init_libshare(libzfs_handle_t *zhandle, int service)
632 if (_sa_init == NULL)
635 if (ret == SA_OK && zhandle->libzfs_shareflags & ZFSSHARE_MISS) {
637 * We had a cache miss. Most likely it is a new ZFS
638 * dataset that was just created. We want to make sure
639 * so check timestamps to see if a different process
640 * has updated any of the configuration. If there was
641 * some non-ZFS change, we need to re-initialize the
644 zhandle->libzfs_shareflags &= ~ZFSSHARE_MISS;
645 if (_sa_needs_refresh != NULL &&
646 _sa_needs_refresh(zhandle->libzfs_sharehdl)) {
647 zfs_uninit_libshare(zhandle);
648 zhandle->libzfs_sharehdl = _sa_init(service);
652 if (ret == SA_OK && zhandle && zhandle->libzfs_sharehdl == NULL)
653 zhandle->libzfs_sharehdl = _sa_init(service);
655 if (ret == SA_OK && zhandle->libzfs_sharehdl == NULL)
663 * zfs_uninit_libshare(zhandle)
665 * Uninitialize the libshare API if it hasn't already been
666 * uninitialized. It is OK to call multiple times.
669 zfs_uninit_libshare(libzfs_handle_t *zhandle)
671 if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) {
673 if (_sa_fini != NULL)
674 _sa_fini(zhandle->libzfs_sharehdl);
676 zhandle->libzfs_sharehdl = NULL;
681 * zfs_parse_options(options, proto)
683 * Call the legacy parse interface to get the protocol specific
684 * options using the NULL arg to indicate that this is a "parse" only.
687 zfs_parse_options(char *options, zfs_share_proto_t proto)
690 if (_sa_parse_legacy_options != NULL) {
691 return (_sa_parse_legacy_options(NULL, options,
692 proto_table[proto].p_name));
694 return (SA_CONFIG_ERR);
702 * zfs_sa_find_share(handle, path)
704 * wrapper around sa_find_share to find a share path in the
708 zfs_sa_find_share(sa_handle_t handle, char *path)
710 if (_sa_find_share != NULL)
711 return (_sa_find_share(handle, path));
716 * zfs_sa_enable_share(share, proto)
718 * Wrapper for sa_enable_share which enables a share for a specified
722 zfs_sa_enable_share(sa_share_t share, char *proto)
724 if (_sa_enable_share != NULL)
725 return (_sa_enable_share(share, proto));
726 return (SA_CONFIG_ERR);
730 * zfs_sa_disable_share(share, proto)
732 * Wrapper for sa_enable_share which disables a share for a specified
736 zfs_sa_disable_share(sa_share_t share, char *proto)
738 if (_sa_disable_share != NULL)
739 return (_sa_disable_share(share, proto));
740 return (SA_CONFIG_ERR);
745 * Share the given filesystem according to the options in the specified
746 * protocol specific properties (sharenfs, sharesmb). We rely
747 * on "libshare" to the dirty work for us.
750 zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
752 char mountpoint[ZFS_MAXPROPLEN];
753 char shareopts[ZFS_MAXPROPLEN];
754 char sourcestr[ZFS_MAXPROPLEN];
755 libzfs_handle_t *hdl = zhp->zfs_hdl;
756 zfs_share_proto_t *curr_proto;
757 zprop_source_t sourcetype;
760 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
763 for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) {
765 * Return success if there are no share options.
767 if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop,
768 shareopts, sizeof (shareopts), &sourcetype, sourcestr,
769 ZFS_MAXPROPLEN, B_FALSE) != 0 ||
770 strcmp(shareopts, "off") == 0)
774 * If the 'zoned' property is set, then zfs_is_mountable()
775 * will have already bailed out if we are in the global zone.
776 * But local zones cannot be NFS servers, so we ignore it for
777 * local zones as well.
779 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
782 if (*curr_proto != PROTO_NFS) {
783 fprintf(stderr, "Unsupported share protocol: %d.\n",
789 share = zfs_sa_find_share(hdl->libzfs_sharehdl, mountpoint);
792 * This may be a new file system that was just
793 * created so isn't in the internal cache
794 * (second time through). Rather than
795 * reloading the entire configuration, we can
796 * assume ZFS has done the checking and it is
797 * safe to add this to the internal
800 if (_sa_zfs_process_share(hdl->libzfs_sharehdl,
801 NULL, NULL, mountpoint,
802 proto_table[*curr_proto].p_name, sourcetype,
803 shareopts, sourcestr, zhp->zfs_name) != SA_OK) {
804 (void) zfs_error_fmt(hdl,
805 proto_table[*curr_proto].p_share_err,
806 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
810 hdl->libzfs_shareflags |= ZFSSHARE_MISS;
811 share = zfs_sa_find_share(hdl->libzfs_sharehdl,
816 err = zfs_sa_enable_share(share,
817 proto_table[*curr_proto].p_name);
819 (void) zfs_error_fmt(hdl,
820 proto_table[*curr_proto].p_share_err,
821 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
827 if (strcmp(shareopts, "on") == 0)
828 error = fsshare(ZFS_EXPORTS_PATH, mountpoint, "");
830 error = fsshare(ZFS_EXPORTS_PATH, mountpoint, shareopts);
834 (void) zfs_error_fmt(hdl,
835 proto_table[*curr_proto].p_share_err,
836 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
846 zfs_share_nfs(zfs_handle_t *zhp)
848 return (zfs_share_proto(zhp, nfs_only));
852 zfs_share_smb(zfs_handle_t *zhp)
854 return (zfs_share_proto(zhp, smb_only));
858 zfs_shareall(zfs_handle_t *zhp)
860 return (zfs_share_proto(zhp, share_all_proto));
864 * Unshare a filesystem by mountpoint.
867 unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint,
868 zfs_share_proto_t proto)
870 char buf[MAXPATHLEN];
874 if (proto != PROTO_NFS) {
875 fprintf(stderr, "No SMB support in FreeBSD yet.\n");
879 error = fsunshare(ZFS_EXPORTS_PATH, mountpoint);
881 zfs_error_aux(hdl, "%s", strerror(error));
882 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
883 dgettext(TEXT_DOMAIN,
884 "cannot unshare '%s'"), name));
891 * Unshare the given filesystem.
894 zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint,
895 zfs_share_proto_t *proto)
897 struct mnttab search = { 0 }, entry;
900 /* check to see if need to unmount the filesystem */
901 search.mnt_special = (char *)zfs_get_name(zhp);
902 search.mnt_fstype = MNTTYPE_ZFS;
903 rewind(zhp->zfs_hdl->libzfs_mnttab);
904 if (mountpoint != NULL)
905 mntpt = zfs_strdup(zhp->zfs_hdl, mountpoint);
907 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
908 getmntany(zhp->zfs_hdl->libzfs_mnttab, &entry, &search) == 0)) {
909 zfs_share_proto_t *curr_proto;
911 if (mountpoint == NULL)
912 mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp);
914 for (curr_proto = proto; *curr_proto != PROTO_END;
917 if (is_shared(zhp->zfs_hdl, mntpt, *curr_proto) &&
918 unshare_one(zhp->zfs_hdl, zhp->zfs_name,
919 mntpt, *curr_proto) != 0) {
933 zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
935 return (zfs_unshare_proto(zhp, mountpoint, nfs_only));
939 zfs_unshare_smb(zfs_handle_t *zhp, const char *mountpoint)
941 return (zfs_unshare_proto(zhp, mountpoint, smb_only));
945 * Same as zfs_unmountall(), but for NFS and SMB unshares.
948 zfs_unshareall_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
950 prop_changelist_t *clp;
953 clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0, 0);
957 ret = changelist_unshare(clp, proto);
958 changelist_free(clp);
964 zfs_unshareall_nfs(zfs_handle_t *zhp)
966 return (zfs_unshareall_proto(zhp, nfs_only));
970 zfs_unshareall_smb(zfs_handle_t *zhp)
972 return (zfs_unshareall_proto(zhp, smb_only));
976 zfs_unshareall(zfs_handle_t *zhp)
978 return (zfs_unshareall_proto(zhp, share_all_proto));
982 zfs_unshareall_bypath(zfs_handle_t *zhp, const char *mountpoint)
984 return (zfs_unshare_proto(zhp, mountpoint, share_all_proto));
988 * Remove the mountpoint associated with the current dataset, if necessary.
989 * We only remove the underlying directory if:
991 * - The mountpoint is not 'none' or 'legacy'
992 * - The mountpoint is non-empty
993 * - The mountpoint is the default or inherited
994 * - The 'zoned' property is set, or we're in a local zone
996 * Any other directories we leave alone.
999 remove_mountpoint(zfs_handle_t *zhp)
1001 char mountpoint[ZFS_MAXPROPLEN];
1002 zprop_source_t source;
1004 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
1008 if (source == ZPROP_SRC_DEFAULT ||
1009 source == ZPROP_SRC_INHERITED) {
1011 * Try to remove the directory, silently ignoring any errors.
1012 * The filesystem may have since been removed or moved around,
1013 * and this error isn't really useful to the administrator in
1016 (void) rmdir(mountpoint);
1021 zfs_is_shared_iscsi(zfs_handle_t *zhp)
1025 * If iscsi deamon isn't running then we aren't shared
1027 if (iscsitgt_svc_online && iscsitgt_svc_online() == 1)
1030 return (iscsitgt_zfs_is_shared != NULL &&
1031 iscsitgt_zfs_is_shared(zhp->zfs_name) != 0);
1035 zfs_share_iscsi(zfs_handle_t *zhp)
1037 char shareopts[ZFS_MAXPROPLEN];
1038 const char *dataset = zhp->zfs_name;
1039 libzfs_handle_t *hdl = zhp->zfs_hdl;
1042 * Return success if there are no share options.
1044 if (zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI, shareopts,
1045 sizeof (shareopts), NULL, NULL, 0, B_FALSE) != 0 ||
1046 strcmp(shareopts, "off") == 0)
1049 /* We don't support iSCSI on FreeBSD yet. */
1051 if (iscsitgt_zfs_share == NULL || iscsitgt_zfs_share(dataset) != 0) {
1052 int error = EZFS_SHAREISCSIFAILED;
1055 * If service isn't availabele and EPERM was
1056 * returned then use special error.
1058 if (iscsitgt_svc_online && errno == EPERM &&
1059 (iscsitgt_svc_online() != 0))
1060 error = EZFS_ISCSISVCUNAVAIL;
1062 return (zfs_error_fmt(hdl, error,
1063 dgettext(TEXT_DOMAIN, "cannot share '%s'"), dataset));
1071 zfs_unshare_iscsi(zfs_handle_t *zhp)
1073 const char *dataset = zfs_get_name(zhp);
1074 libzfs_handle_t *hdl = zhp->zfs_hdl;
1076 /* We don't support iSCSI on FreeBSD yet. */
1079 * Return if the volume is not shared
1081 if (zfs_is_shared_iscsi(zhp) != SHARED_ISCSI)
1085 * If this fails with ENODEV it indicates that zvol wasn't shared so
1086 * we should return success in that case.
1088 if (iscsitgt_zfs_unshare == NULL ||
1089 (iscsitgt_zfs_unshare(dataset) != 0 && errno != ENODEV)) {
1091 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1092 "Insufficient privileges to unshare iscsi"));
1093 return (zfs_error_fmt(hdl, EZFS_UNSHAREISCSIFAILED,
1094 dgettext(TEXT_DOMAIN, "cannot unshare '%s'"), dataset));
1101 typedef struct mount_cbdata {
1102 zfs_handle_t **cb_datasets;
1108 mount_cb(zfs_handle_t *zhp, void *data)
1110 mount_cbdata_t *cbp = data;
1112 if (!(zfs_get_type(zhp) & (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME))) {
1117 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_NOAUTO) {
1122 if (cbp->cb_alloc == cbp->cb_used) {
1125 if ((ptr = zfs_realloc(zhp->zfs_hdl,
1126 cbp->cb_datasets, cbp->cb_alloc * sizeof (void *),
1127 cbp->cb_alloc * 2 * sizeof (void *))) == NULL)
1129 cbp->cb_datasets = ptr;
1134 cbp->cb_datasets[cbp->cb_used++] = zhp;
1136 return (zfs_iter_filesystems(zhp, mount_cb, cbp));
1140 dataset_cmp(const void *a, const void *b)
1142 zfs_handle_t **za = (zfs_handle_t **)a;
1143 zfs_handle_t **zb = (zfs_handle_t **)b;
1144 char mounta[MAXPATHLEN];
1145 char mountb[MAXPATHLEN];
1146 boolean_t gota, gotb;
1148 if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
1149 verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
1150 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
1151 if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
1152 verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
1153 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
1156 return (strcmp(mounta, mountb));
1163 return (strcmp(zfs_get_name(a), zfs_get_name(b)));
1167 * Mount and share all datasets within the given pool. This assumes that no
1168 * datasets within the pool are currently mounted. Because users can create
1169 * complicated nested hierarchies of mountpoints, we first gather all the
1170 * datasets and mountpoints within the pool, and sort them by mountpoint. Once
1171 * we have the list of all filesystems, we iterate over them in order and mount
1172 * and/or share each one.
1174 #pragma weak zpool_mount_datasets = zpool_enable_datasets
1176 zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
1178 mount_cbdata_t cb = { 0 };
1179 libzfs_handle_t *hdl = zhp->zpool_hdl;
1185 * Gather all non-snap datasets within the pool.
1187 if ((cb.cb_datasets = zfs_alloc(hdl, 4 * sizeof (void *))) == NULL)
1191 if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_DATASET)) == NULL)
1194 cb.cb_datasets[0] = zfsp;
1197 if (zfs_iter_filesystems(zfsp, mount_cb, &cb) != 0)
1201 * Sort the datasets by mountpoint.
1203 qsort(cb.cb_datasets, cb.cb_used, sizeof (void *), dataset_cmp);
1206 * And mount all the datasets, keeping track of which ones
1207 * succeeded or failed. By using zfs_alloc(), the good pointer
1208 * will always be non-NULL.
1210 good = zfs_alloc(zhp->zpool_hdl, cb.cb_used * sizeof (int));
1212 for (i = 0; i < cb.cb_used; i++) {
1213 if (zfs_mount(cb.cb_datasets[i], mntopts, flags) != 0)
1220 * Then share all the ones that need to be shared. This needs
1221 * to be a separate pass in order to avoid excessive reloading
1222 * of the configuration. Good should never be NULL since
1223 * zfs_alloc is supposed to exit if memory isn't available.
1225 for (i = 0; i < cb.cb_used; i++) {
1226 if (good[i] && zfs_share(cb.cb_datasets[i]) != 0)
1233 for (i = 0; i < cb.cb_used; i++)
1234 zfs_close(cb.cb_datasets[i]);
1235 free(cb.cb_datasets);
1242 zvol_cb(const char *dataset, void *data)
1244 libzfs_handle_t *hdl = data;
1248 * Ignore snapshots and ignore failures from non-existant datasets.
1250 if (strchr(dataset, '@') != NULL ||
1251 (zhp = zfs_open(hdl, dataset, ZFS_TYPE_VOLUME)) == NULL)
1254 if (zfs_unshare_iscsi(zhp) != 0)
1263 mountpoint_compare(const void *a, const void *b)
1265 const char *mounta = *((char **)a);
1266 const char *mountb = *((char **)b);
1268 return (strcmp(mountb, mounta));
1272 * Unshare and unmount all datasets within the given pool. We don't want to
1273 * rely on traversing the DSL to discover the filesystems within the pool,
1274 * because this may be expensive (if not all of them are mounted), and can fail
1275 * arbitrarily (on I/O error, for example). Instead, we walk /etc/mnttab and
1276 * gather all the filesystems that are currently mounted.
1278 #pragma weak zpool_unmount_datasets = zpool_disable_datasets
1280 zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
1285 char **mountpoints = NULL;
1286 zfs_handle_t **datasets = NULL;
1287 libzfs_handle_t *hdl = zhp->zpool_hdl;
1290 int flags = (force ? MS_FORCE : 0);
1293 * First unshare all zvols.
1295 if (zpool_iter_zvol(zhp, zvol_cb, hdl) != 0)
1298 namelen = strlen(zhp->zpool_name);
1301 if ((n = getmntinfo(&sfs, MNT_WAIT)) == 0) {
1302 fprintf(stderr, "getmntinfo(): %s\n", strerror(errno));
1305 for (j = 0; j < n; j++) {
1307 * Ignore non-ZFS entries.
1309 if (strcmp(sfs[j].f_fstypename, MNTTYPE_ZFS) != 0)
1313 * Ignore filesystems not within this pool.
1315 if (strncmp(sfs[j].f_mntfromname, zhp->zpool_name, namelen) != 0 ||
1316 (sfs[j].f_mntfromname[namelen] != '/' &&
1317 sfs[j].f_mntfromname[namelen] != '\0'))
1321 * At this point we've found a filesystem within our pool. Add
1322 * it to our growing list.
1324 if (used == alloc) {
1326 if ((mountpoints = zfs_alloc(hdl,
1327 8 * sizeof (void *))) == NULL)
1330 if ((datasets = zfs_alloc(hdl,
1331 8 * sizeof (void *))) == NULL)
1338 if ((ptr = zfs_realloc(hdl, mountpoints,
1339 alloc * sizeof (void *),
1340 alloc * 2 * sizeof (void *))) == NULL)
1344 if ((ptr = zfs_realloc(hdl, datasets,
1345 alloc * sizeof (void *),
1346 alloc * 2 * sizeof (void *))) == NULL)
1354 if ((mountpoints[used] = zfs_strdup(hdl,
1355 sfs[j].f_mntonname)) == NULL)
1359 * This is allowed to fail, in case there is some I/O error. It
1360 * is only used to determine if we need to remove the underlying
1361 * mountpoint, so failure is not fatal.
1363 datasets[used] = make_dataset_handle(hdl, sfs[j].f_mntfromname);
1369 * At this point, we have the entire list of filesystems, so sort it by
1372 qsort(mountpoints, used, sizeof (char *), mountpoint_compare);
1375 * Walk through and first unshare everything.
1377 for (i = 0; i < used; i++) {
1378 zfs_share_proto_t *curr_proto;
1379 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
1381 if (is_shared(hdl, mountpoints[i], *curr_proto) &&
1382 unshare_one(hdl, mountpoints[i],
1383 mountpoints[i], *curr_proto) != 0)
1389 * Now unmount everything, removing the underlying directories as
1392 for (i = 0; i < used; i++) {
1393 if (unmount_one(hdl, mountpoints[i], flags) != 0)
1397 for (i = 0; i < used; i++) {
1399 remove_mountpoint(datasets[i]);
1404 for (i = 0; i < used; i++) {
1406 zfs_close(datasets[i]);
1407 free(mountpoints[i]);