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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
27 * Routines to manage ZFS mounts. We separate all the nasty routines that have
28 * to deal with the OS. The following functions are the main entry points --
29 * they are used by mount and unmount and when changing a filesystem's
37 * This file also contains the functions used to manage sharing filesystems via
50 * zfs_unshareall_nfs()
51 * zfs_unshareall_smb()
53 * zfs_unshareall_bypath()
55 * The following functions are available for pool consumers, and will
56 * mount/unmount and share/unshare all datasets within pool:
58 * zpool_enable_datasets()
59 * zpool_disable_datasets()
72 #include <sys/mntent.h>
73 #include <sys/mount.h>
75 #ifdef HAVE_LIBSELINUX
76 #include <selinux/selinux.h>
77 #endif /* HAVE_LIBSELINUX */
81 #include "libzfs_impl.h"
84 #include <sys/systeminfo.h>
85 #define MAXISALEN 257 /* based on sysinfo(2) man page */
87 static int zfs_share_proto(zfs_handle_t *, zfs_share_proto_t *);
88 zfs_share_type_t zfs_is_shared_proto(zfs_handle_t *, char **,
92 * The share protocols table must be in the same order as the zfs_share_prot_t
93 * enum in libzfs_impl.h
102 proto_table_t proto_table[PROTO_END] = {
103 {ZFS_PROP_SHARENFS, "nfs", EZFS_SHARENFSFAILED, EZFS_UNSHARENFSFAILED},
104 {ZFS_PROP_SHARESMB, "smb", EZFS_SHARESMBFAILED, EZFS_UNSHARESMBFAILED},
107 zfs_share_proto_t nfs_only[] = {
112 zfs_share_proto_t smb_only[] = {
116 zfs_share_proto_t share_all_proto[] = {
123 * Search for NFS and SMB exports for the given mountpoint and protocol, returning
124 * a zfs_share_type_t value.
126 static zfs_share_type_t
127 is_shared(libzfs_handle_t *hdl, const char *mountpoint, zfs_share_proto_t proto)
129 char buf[MAXPATHLEN], *tab;
131 if (hdl->libzfs_sharetab == NULL)
132 return (SHARED_NOT_SHARED);
134 (void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET);
136 /* Search /etc/exports for NFS exports */
137 /* FIXME: Assumes the file is tab delimited. */
138 while (fgets(buf, sizeof (buf), hdl->libzfs_sharetab) != NULL) {
140 /* the mountpoint is the first entry on each line */
141 if ((tab = strchr(buf, '\t')) == NULL)
145 if (strcmp(buf, mountpoint) == 0) {
146 if (proto == PROTO_NFS)
149 return (SHARED_NOT_SHARED);
153 /* XXX: Search /etc/samba/smb.conf for SMB exports, return SHARED_SMB */
155 return (SHARED_NOT_SHARED);
159 * Returns true if the specified directory is empty. If we can't open the
160 * directory at all, return true so that the mount can fail with a more
161 * informative error message.
164 dir_is_empty(const char *dirname)
169 if ((dirp = opendir(dirname)) == NULL)
172 while ((dp = readdir64(dirp)) != NULL) {
174 if (strcmp(dp->d_name, ".") == 0 ||
175 strcmp(dp->d_name, "..") == 0)
178 (void) closedir(dirp);
182 (void) closedir(dirp);
187 * Checks to see if the mount is active. If the filesystem is mounted, we fill
188 * in 'where' with the current mountpoint, and return 1. Otherwise, we return
192 is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where)
196 if (libzfs_mnttab_find(zfs_hdl, special, &entry) != 0)
200 *where = zfs_strdup(zfs_hdl, entry.mnt_mountp);
206 zfs_is_mounted(zfs_handle_t *zhp, char **where)
208 return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where));
212 * Returns true if the given dataset is mountable, false otherwise. Returns the
213 * mountpoint in 'buf'.
216 zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen,
217 zprop_source_t *source)
219 char sourceloc[ZFS_MAXNAMELEN];
220 zprop_source_t sourcetype;
222 if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type))
225 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen,
226 &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0);
228 if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 ||
229 strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0)
232 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF)
235 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
236 getzoneid() == GLOBAL_ZONEID)
240 *source = sourcetype;
246 * The filesystem is mounted by invoking the system mount utility rather
247 * than by the system call mount(2). This ensures that the /etc/mtab
248 * file is correctly locked for the update. Performing our own locking
249 * and /etc/mtab update requires making an unsafe assumption about how
250 * the mount utility performs its locking. Unfortunately, this also means
251 * in the case of a mount failure we do not have the exact errno. We must
252 * make due with return value from the mount process.
254 * In the long term a shared library called libmount is under development
255 * which provides a common API to address the locking and errno issues.
256 * Once the standard mount utility has been updated to use this library
257 * we can add an autoconf check to conditionally use it.
259 * http://www.kernel.org/pub/linux/utils/util-linux/libmount-docs/index.html
263 do_mount(const char *src, const char *mntpt, char *opts)
274 /* Return only the most critical mount error */
275 rc = libzfs_run_process(argv[0], argv);
277 if (rc & MOUNT_FILEIO)
281 if (rc & MOUNT_SOFTWARE)
283 if (rc & MOUNT_SYSERR)
285 if (rc & MOUNT_USAGE)
288 return ENXIO; /* Generic error */
295 do_unmount(const char *mntpt, int flags)
297 char force_opt[] = "-f";
298 char lazy_opt[] = "-l";
302 NULL, NULL, NULL, NULL };
305 if (flags & MS_FORCE) {
306 argv[count] = force_opt;
310 if (flags & MS_DETACH) {
311 argv[count] = lazy_opt;
315 argv[count] = (char *)mntpt;
316 rc = libzfs_run_process(argv[0], argv);
318 return (rc ? EINVAL : 0);
322 * Mount the given filesystem.
325 zfs_mount(zfs_handle_t *zhp, const char *options, int flags)
328 char mountpoint[ZFS_MAXPROPLEN];
329 char mntopts[MNT_LINE_MAX];
330 libzfs_handle_t *hdl = zhp->zfs_hdl;
334 (void) strlcpy(mntopts, MNTOPT_DEFAULTS, sizeof (mntopts));
336 (void) strlcpy(mntopts, options, sizeof (mntopts));
339 * If the pool is imported read-only then all mounts must be read-only
341 if (zpool_get_prop_int(zhp->zpool_hdl, ZPOOL_PROP_READONLY, NULL))
342 (void) strlcat(mntopts, "," MNTOPT_RO, sizeof (mntopts));
345 * Append zfsutil option so the mount helper allow the mount
347 strlcat(mntopts, "," MNTOPT_ZFSUTIL, sizeof (mntopts));
349 #ifdef HAVE_LIBSELINUX
350 if (is_selinux_enabled())
351 (void) strlcat(mntopts, ",context=\"system_u:"
352 "object_r:file_t:s0\"", sizeof (mntopts));
353 #endif /* HAVE_LIBSELINUX */
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'"),
370 * Determine if the mountpoint is empty. If so, refuse to perform the
371 * mount. We don't perform this check if 'remount' is specified.
373 if (strstr(mntopts, MNTOPT_REMOUNT) == NULL &&
374 !dir_is_empty(mountpoint)) {
375 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
376 "directory is not empty"));
377 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
378 dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint));
381 /* perform the mount */
382 rc = do_mount(zfs_get_name(zhp), mountpoint, mntopts);
385 * Generic errors are nasty, but there are just way too many
386 * from mount(), and they're well-understood. We pick a few
387 * common ones to improve upon.
390 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
391 "mountpoint or dataset is busy"));
392 } else if (rc == EPERM) {
393 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
394 "Insufficient privileges"));
395 } else if (rc == ENOTSUP) {
399 VERIFY(zfs_spa_version(zhp, &spa_version) == 0);
400 (void) snprintf(buf, sizeof (buf),
401 dgettext(TEXT_DOMAIN, "Can't mount a version %lld "
402 "file system on a version %d pool. Pool must be"
403 " upgraded to mount this file system."),
404 (u_longlong_t)zfs_prop_get_int(zhp,
405 ZFS_PROP_VERSION), spa_version);
406 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, buf));
408 zfs_error_aux(hdl, strerror(rc));
410 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
411 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
415 /* add the mounted entry into our cache */
416 libzfs_mnttab_add(hdl, zfs_get_name(zhp), mountpoint, mntopts);
421 * Unmount a single filesystem.
424 unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags)
426 if (do_unmount(mountpoint, flags) != 0) {
427 zfs_error_aux(hdl, strerror(errno));
428 return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED,
429 dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
437 * Unmount the given filesystem.
440 zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
442 libzfs_handle_t *hdl = zhp->zfs_hdl;
446 /* check to see if we need to unmount the filesystem */
447 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
448 libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)) {
450 * mountpoint may have come from a call to
451 * getmnt/getmntany if it isn't NULL. If it is NULL,
452 * we know it comes from libzfs_mnttab_find which can
453 * then get freed later. We strdup it to play it safe.
455 if (mountpoint == NULL)
456 mntpt = zfs_strdup(hdl, entry.mnt_mountp);
458 mntpt = zfs_strdup(hdl, mountpoint);
461 * Unshare and unmount the filesystem
463 if (zfs_unshare_proto(zhp, mntpt, share_all_proto) != 0)
466 if (unmount_one(hdl, mntpt, flags) != 0) {
468 (void) zfs_shareall(zhp);
471 libzfs_mnttab_remove(hdl, zhp->zfs_name);
479 * Unmount this filesystem and any children inheriting the mountpoint property.
480 * To do this, just act like we're changing the mountpoint property, but don't
481 * remount the filesystems afterwards.
484 zfs_unmountall(zfs_handle_t *zhp, int flags)
486 prop_changelist_t *clp;
489 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, 0, flags);
493 ret = changelist_prefix(clp);
494 changelist_free(clp);
500 zfs_is_shared(zfs_handle_t *zhp)
502 zfs_share_type_t rc = 0;
503 zfs_share_proto_t *curr_proto;
505 if (ZFS_IS_VOLUME(zhp))
508 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
510 rc |= zfs_is_shared_proto(zhp, NULL, *curr_proto);
512 return (rc ? B_TRUE : B_FALSE);
516 zfs_share(zfs_handle_t *zhp)
518 assert(!ZFS_IS_VOLUME(zhp));
519 return (zfs_share_proto(zhp, share_all_proto));
523 zfs_unshare(zfs_handle_t *zhp)
525 assert(!ZFS_IS_VOLUME(zhp));
526 return (zfs_unshareall(zhp));
530 * Check to see if the filesystem is currently shared.
533 zfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto)
538 if (!zfs_is_mounted(zhp, &mountpoint))
539 return (SHARED_NOT_SHARED);
541 if ((rc = is_shared(zhp->zfs_hdl, mountpoint, proto))) {
549 return (SHARED_NOT_SHARED);
554 zfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
556 return (zfs_is_shared_proto(zhp, where,
557 PROTO_NFS) != SHARED_NOT_SHARED);
561 zfs_is_shared_smb(zfs_handle_t *zhp, char **where)
563 return (zfs_is_shared_proto(zhp, where,
564 PROTO_SMB) != SHARED_NOT_SHARED);
568 * Make sure things will work if libshare isn't installed by using
569 * wrapper functions that check to see that the pointers to functions
570 * initialized in _zfs_init_libshare() are actually present.
573 static sa_handle_t (*_sa_init)(int);
574 static void (*_sa_fini)(sa_handle_t);
575 static sa_share_t (*_sa_find_share)(sa_handle_t, char *);
576 static int (*_sa_enable_share)(sa_share_t, char *);
577 static int (*_sa_disable_share)(sa_share_t, char *);
578 static char *(*_sa_errorstr)(int);
579 static int (*_sa_parse_legacy_options)(sa_group_t, char *, char *);
580 static boolean_t (*_sa_needs_refresh)(sa_handle_t *);
581 static libzfs_handle_t *(*_sa_get_zfs_handle)(sa_handle_t);
582 static int (*_sa_zfs_process_share)(sa_handle_t, sa_group_t, sa_share_t,
583 char *, char *, zprop_source_t, char *, char *, char *);
584 static void (*_sa_update_sharetab_ts)(sa_handle_t);
587 * _zfs_init_libshare()
589 * Find the libshare.so.1 entry points that we use here and save the
590 * values to be used later. This is triggered by the runtime loader.
591 * Make sure the correct ISA version is loaded.
595 _zfs_init_libshare(void) __attribute__((constructor));
597 #pragma init(_zfs_init_libshare)
600 _zfs_init_libshare(void)
603 char path[MAXPATHLEN];
607 if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1)
612 (void) snprintf(path, MAXPATHLEN,
613 "/usr/lib/%s/libshare.so.1", isa);
615 if ((libshare = dlopen(path, RTLD_LAZY | RTLD_GLOBAL)) != NULL) {
616 _sa_init = (sa_handle_t (*)(int))dlsym(libshare, "sa_init");
617 _sa_fini = (void (*)(sa_handle_t))dlsym(libshare, "sa_fini");
618 _sa_find_share = (sa_share_t (*)(sa_handle_t, char *))
619 dlsym(libshare, "sa_find_share");
620 _sa_enable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
622 _sa_disable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
624 _sa_errorstr = (char *(*)(int))dlsym(libshare, "sa_errorstr");
625 _sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *))
626 dlsym(libshare, "sa_parse_legacy_options");
627 _sa_needs_refresh = (boolean_t (*)(sa_handle_t *))
628 dlsym(libshare, "sa_needs_refresh");
629 _sa_get_zfs_handle = (libzfs_handle_t *(*)(sa_handle_t))
630 dlsym(libshare, "sa_get_zfs_handle");
631 _sa_zfs_process_share = (int (*)(sa_handle_t, sa_group_t,
632 sa_share_t, char *, char *, zprop_source_t, char *,
633 char *, char *))dlsym(libshare, "sa_zfs_process_share");
634 _sa_update_sharetab_ts = (void (*)(sa_handle_t))
635 dlsym(libshare, "sa_update_sharetab_ts");
636 if (_sa_init == NULL || _sa_fini == NULL ||
637 _sa_find_share == NULL || _sa_enable_share == NULL ||
638 _sa_disable_share == NULL || _sa_errorstr == NULL ||
639 _sa_parse_legacy_options == NULL ||
640 _sa_needs_refresh == NULL || _sa_get_zfs_handle == NULL ||
641 _sa_zfs_process_share == NULL ||
642 _sa_update_sharetab_ts == NULL) {
645 _sa_disable_share = NULL;
646 _sa_enable_share = NULL;
648 _sa_parse_legacy_options = NULL;
649 (void) dlclose(libshare);
650 _sa_needs_refresh = NULL;
651 _sa_get_zfs_handle = NULL;
652 _sa_zfs_process_share = NULL;
653 _sa_update_sharetab_ts = NULL;
659 * zfs_init_libshare(zhandle, service)
661 * Initialize the libshare API if it hasn't already been initialized.
662 * In all cases it returns 0 if it succeeded and an error if not. The
663 * service value is which part(s) of the API to initialize and is a
664 * direct map to the libshare sa_init(service) interface.
667 zfs_init_libshare(libzfs_handle_t *zhandle, int service)
671 if (_sa_init == NULL)
674 if (ret == SA_OK && zhandle->libzfs_shareflags & ZFSSHARE_MISS) {
676 * We had a cache miss. Most likely it is a new ZFS
677 * dataset that was just created. We want to make sure
678 * so check timestamps to see if a different process
679 * has updated any of the configuration. If there was
680 * some non-ZFS change, we need to re-initialize the
683 zhandle->libzfs_shareflags &= ~ZFSSHARE_MISS;
684 if (_sa_needs_refresh != NULL &&
685 _sa_needs_refresh(zhandle->libzfs_sharehdl)) {
686 zfs_uninit_libshare(zhandle);
687 zhandle->libzfs_sharehdl = _sa_init(service);
691 if (ret == SA_OK && zhandle && zhandle->libzfs_sharehdl == NULL)
692 zhandle->libzfs_sharehdl = _sa_init(service);
694 if (ret == SA_OK && zhandle->libzfs_sharehdl == NULL)
701 * zfs_uninit_libshare(zhandle)
703 * Uninitialize the libshare API if it hasn't already been
704 * uninitialized. It is OK to call multiple times.
707 zfs_uninit_libshare(libzfs_handle_t *zhandle)
709 if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) {
710 if (_sa_fini != NULL)
711 _sa_fini(zhandle->libzfs_sharehdl);
712 zhandle->libzfs_sharehdl = NULL;
717 * zfs_parse_options(options, proto)
719 * Call the legacy parse interface to get the protocol specific
720 * options using the NULL arg to indicate that this is a "parse" only.
723 zfs_parse_options(char *options, zfs_share_proto_t proto)
725 if (_sa_parse_legacy_options != NULL) {
726 return (_sa_parse_legacy_options(NULL, options,
727 proto_table[proto].p_name));
729 return (SA_CONFIG_ERR);
733 * zfs_sa_find_share(handle, path)
735 * wrapper around sa_find_share to find a share path in the
739 zfs_sa_find_share(sa_handle_t handle, char *path)
741 if (_sa_find_share != NULL)
742 return (_sa_find_share(handle, path));
747 * zfs_sa_enable_share(share, proto)
749 * Wrapper for sa_enable_share which enables a share for a specified
753 zfs_sa_enable_share(sa_share_t share, char *proto)
755 if (_sa_enable_share != NULL)
756 return (_sa_enable_share(share, proto));
757 return (SA_CONFIG_ERR);
761 * zfs_sa_disable_share(share, proto)
763 * Wrapper for sa_enable_share which disables a share for a specified
767 zfs_sa_disable_share(sa_share_t share, char *proto)
769 if (_sa_disable_share != NULL)
770 return (_sa_disable_share(share, proto));
771 return (SA_CONFIG_ERR);
775 * Share the given filesystem according to the options in the specified
776 * protocol specific properties (sharenfs, sharesmb). We rely
777 * on "libshare" to the dirty work for us.
780 zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
782 char mountpoint[ZFS_MAXPROPLEN];
783 char shareopts[ZFS_MAXPROPLEN];
784 char sourcestr[ZFS_MAXPROPLEN];
785 libzfs_handle_t *hdl = zhp->zfs_hdl;
787 zfs_share_proto_t *curr_proto;
788 zprop_source_t sourcetype;
791 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
794 if ((ret = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
796 (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
797 dgettext(TEXT_DOMAIN, "cannot share '%s': %s"),
798 zfs_get_name(zhp), _sa_errorstr != NULL ?
799 _sa_errorstr(ret) : "");
800 #endif /* HAVE_SHARE */
804 for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) {
806 * Return success if there are no share options.
808 if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop,
809 shareopts, sizeof (shareopts), &sourcetype, sourcestr,
810 ZFS_MAXPROPLEN, B_FALSE) != 0 ||
811 strcmp(shareopts, "off") == 0)
815 * If the 'zoned' property is set, then zfs_is_mountable()
816 * will have already bailed out if we are in the global zone.
817 * But local zones cannot be NFS servers, so we ignore it for
818 * local zones as well.
820 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
823 share = zfs_sa_find_share(hdl->libzfs_sharehdl, mountpoint);
826 * This may be a new file system that was just
827 * created so isn't in the internal cache
828 * (second time through). Rather than
829 * reloading the entire configuration, we can
830 * assume ZFS has done the checking and it is
831 * safe to add this to the internal
834 if (_sa_zfs_process_share(hdl->libzfs_sharehdl,
835 NULL, NULL, mountpoint,
836 proto_table[*curr_proto].p_name, sourcetype,
837 shareopts, sourcestr, zhp->zfs_name) != SA_OK) {
838 (void) zfs_error_fmt(hdl,
839 proto_table[*curr_proto].p_share_err,
840 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
844 hdl->libzfs_shareflags |= ZFSSHARE_MISS;
845 share = zfs_sa_find_share(hdl->libzfs_sharehdl,
850 err = zfs_sa_enable_share(share,
851 proto_table[*curr_proto].p_name);
853 (void) zfs_error_fmt(hdl,
854 proto_table[*curr_proto].p_share_err,
855 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
860 (void) zfs_error_fmt(hdl,
861 proto_table[*curr_proto].p_share_err,
862 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
873 zfs_share_nfs(zfs_handle_t *zhp)
875 return (zfs_share_proto(zhp, nfs_only));
879 zfs_share_smb(zfs_handle_t *zhp)
881 return (zfs_share_proto(zhp, smb_only));
885 zfs_shareall(zfs_handle_t *zhp)
887 return (zfs_share_proto(zhp, share_all_proto));
891 * Unshare a filesystem by mountpoint.
894 unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint,
895 zfs_share_proto_t proto)
901 * Mountpoint could get trashed if libshare calls getmntany
902 * which it does during API initialization, so strdup the
905 mntpt = zfs_strdup(hdl, mountpoint);
907 /* make sure libshare initialized */
908 if ((err = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
909 free(mntpt); /* don't need the copy anymore */
910 return (zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
911 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
912 name, _sa_errorstr(err)));
915 share = zfs_sa_find_share(hdl->libzfs_sharehdl, mntpt);
916 free(mntpt); /* don't need the copy anymore */
919 err = zfs_sa_disable_share(share, proto_table[proto].p_name);
921 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
922 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
923 name, _sa_errorstr(err)));
926 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
927 dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"),
934 * Unshare the given filesystem.
937 zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint,
938 zfs_share_proto_t *proto)
940 libzfs_handle_t *hdl = zhp->zfs_hdl;
944 /* check to see if need to unmount the filesystem */
945 rewind(zhp->zfs_hdl->libzfs_mnttab);
946 if (mountpoint != NULL)
947 mountpoint = mntpt = zfs_strdup(hdl, mountpoint);
949 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
950 libzfs_mnttab_find(hdl, zfs_get_name(zhp), &entry) == 0)) {
951 zfs_share_proto_t *curr_proto;
953 if (mountpoint == NULL)
954 mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp);
956 for (curr_proto = proto; *curr_proto != PROTO_END;
959 if (is_shared(hdl, mntpt, *curr_proto) &&
960 unshare_one(hdl, zhp->zfs_name,
961 mntpt, *curr_proto) != 0) {
975 zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
977 return (zfs_unshare_proto(zhp, mountpoint, nfs_only));
981 zfs_unshare_smb(zfs_handle_t *zhp, const char *mountpoint)
983 return (zfs_unshare_proto(zhp, mountpoint, smb_only));
987 * Same as zfs_unmountall(), but for NFS and SMB unshares.
990 zfs_unshareall_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
992 prop_changelist_t *clp;
995 clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0, 0);
999 ret = changelist_unshare(clp, proto);
1000 changelist_free(clp);
1006 zfs_unshareall_nfs(zfs_handle_t *zhp)
1008 return (zfs_unshareall_proto(zhp, nfs_only));
1012 zfs_unshareall_smb(zfs_handle_t *zhp)
1014 return (zfs_unshareall_proto(zhp, smb_only));
1018 zfs_unshareall(zfs_handle_t *zhp)
1020 return (zfs_unshareall_proto(zhp, share_all_proto));
1024 zfs_unshareall_bypath(zfs_handle_t *zhp, const char *mountpoint)
1026 return (zfs_unshare_proto(zhp, mountpoint, share_all_proto));
1030 * Remove the mountpoint associated with the current dataset, if necessary.
1031 * We only remove the underlying directory if:
1033 * - The mountpoint is not 'none' or 'legacy'
1034 * - The mountpoint is non-empty
1035 * - The mountpoint is the default or inherited
1036 * - The 'zoned' property is set, or we're in a local zone
1038 * Any other directories we leave alone.
1041 remove_mountpoint(zfs_handle_t *zhp)
1043 char mountpoint[ZFS_MAXPROPLEN];
1044 zprop_source_t source;
1046 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
1050 if (source == ZPROP_SRC_DEFAULT ||
1051 source == ZPROP_SRC_INHERITED) {
1053 * Try to remove the directory, silently ignoring any errors.
1054 * The filesystem may have since been removed or moved around,
1055 * and this error isn't really useful to the administrator in
1058 (void) rmdir(mountpoint);
1063 libzfs_add_handle(get_all_cb_t *cbp, zfs_handle_t *zhp)
1065 if (cbp->cb_alloc == cbp->cb_used) {
1069 newsz = cbp->cb_alloc ? cbp->cb_alloc * 2 : 64;
1070 ptr = zfs_realloc(zhp->zfs_hdl,
1071 cbp->cb_handles, cbp->cb_alloc * sizeof (void *),
1072 newsz * sizeof (void *));
1073 cbp->cb_handles = ptr;
1074 cbp->cb_alloc = newsz;
1076 cbp->cb_handles[cbp->cb_used++] = zhp;
1080 mount_cb(zfs_handle_t *zhp, void *data)
1082 get_all_cb_t *cbp = data;
1084 if (!(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM)) {
1089 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_NOAUTO) {
1094 libzfs_add_handle(cbp, zhp);
1095 if (zfs_iter_filesystems(zhp, mount_cb, cbp) != 0) {
1103 libzfs_dataset_cmp(const void *a, const void *b)
1105 zfs_handle_t **za = (zfs_handle_t **)a;
1106 zfs_handle_t **zb = (zfs_handle_t **)b;
1107 char mounta[MAXPATHLEN];
1108 char mountb[MAXPATHLEN];
1109 boolean_t gota, gotb;
1111 if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
1112 verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
1113 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
1114 if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
1115 verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
1116 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
1119 return (strcmp(mounta, mountb));
1126 return (strcmp(zfs_get_name(a), zfs_get_name(b)));
1130 * Mount and share all datasets within the given pool. This assumes that no
1131 * datasets within the pool are currently mounted. Because users can create
1132 * complicated nested hierarchies of mountpoints, we first gather all the
1133 * datasets and mountpoints within the pool, and sort them by mountpoint. Once
1134 * we have the list of all filesystems, we iterate over them in order and mount
1135 * and/or share each one.
1137 #pragma weak zpool_mount_datasets = zpool_enable_datasets
1139 zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
1141 get_all_cb_t cb = { 0 };
1142 libzfs_handle_t *hdl = zhp->zpool_hdl;
1148 * Gather all non-snap datasets within the pool.
1150 if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_DATASET)) == NULL)
1153 libzfs_add_handle(&cb, zfsp);
1154 if (zfs_iter_filesystems(zfsp, mount_cb, &cb) != 0)
1157 * Sort the datasets by mountpoint.
1159 qsort(cb.cb_handles, cb.cb_used, sizeof (void *),
1160 libzfs_dataset_cmp);
1163 * And mount all the datasets, keeping track of which ones
1164 * succeeded or failed.
1166 if ((good = zfs_alloc(zhp->zpool_hdl,
1167 cb.cb_used * sizeof (int))) == NULL)
1171 for (i = 0; i < cb.cb_used; i++) {
1172 if (zfs_mount(cb.cb_handles[i], mntopts, flags) != 0)
1179 * Then share all the ones that need to be shared. This needs
1180 * to be a separate pass in order to avoid excessive reloading
1181 * of the configuration. Good should never be NULL since
1182 * zfs_alloc is supposed to exit if memory isn't available.
1184 for (i = 0; i < cb.cb_used; i++) {
1185 if (good[i] && zfs_share(cb.cb_handles[i]) != 0)
1192 for (i = 0; i < cb.cb_used; i++)
1193 zfs_close(cb.cb_handles[i]);
1194 free(cb.cb_handles);
1200 mountpoint_compare(const void *a, const void *b)
1202 const char *mounta = *((char **)a);
1203 const char *mountb = *((char **)b);
1205 return (strcmp(mountb, mounta));
1208 /* alias for 2002/240 */
1209 #pragma weak zpool_unmount_datasets = zpool_disable_datasets
1211 * Unshare and unmount all datasets within the given pool. We don't want to
1212 * rely on traversing the DSL to discover the filesystems within the pool,
1213 * because this may be expensive (if not all of them are mounted), and can fail
1214 * arbitrarily (on I/O error, for example). Instead, we walk /etc/mtab and
1215 * gather all the filesystems that are currently mounted.
1218 zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
1221 struct mnttab entry;
1223 char **mountpoints = NULL;
1224 zfs_handle_t **datasets = NULL;
1225 libzfs_handle_t *hdl = zhp->zpool_hdl;
1228 int flags = (force ? MS_FORCE : 0);
1230 namelen = strlen(zhp->zpool_name);
1232 rewind(hdl->libzfs_mnttab);
1234 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
1236 * Ignore non-ZFS entries.
1238 if (entry.mnt_fstype == NULL ||
1239 strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
1243 * Ignore filesystems not within this pool.
1245 if (entry.mnt_mountp == NULL ||
1246 strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 ||
1247 (entry.mnt_special[namelen] != '/' &&
1248 entry.mnt_special[namelen] != '\0'))
1252 * At this point we've found a filesystem within our pool. Add
1253 * it to our growing list.
1255 if (used == alloc) {
1257 if ((mountpoints = zfs_alloc(hdl,
1258 8 * sizeof (void *))) == NULL)
1261 if ((datasets = zfs_alloc(hdl,
1262 8 * sizeof (void *))) == NULL)
1269 if ((ptr = zfs_realloc(hdl, mountpoints,
1270 alloc * sizeof (void *),
1271 alloc * 2 * sizeof (void *))) == NULL)
1275 if ((ptr = zfs_realloc(hdl, datasets,
1276 alloc * sizeof (void *),
1277 alloc * 2 * sizeof (void *))) == NULL)
1285 if ((mountpoints[used] = zfs_strdup(hdl,
1286 entry.mnt_mountp)) == NULL)
1290 * This is allowed to fail, in case there is some I/O error. It
1291 * is only used to determine if we need to remove the underlying
1292 * mountpoint, so failure is not fatal.
1294 datasets[used] = make_dataset_handle(hdl, entry.mnt_special);
1300 * At this point, we have the entire list of filesystems, so sort it by
1303 qsort(mountpoints, used, sizeof (char *), mountpoint_compare);
1306 * Walk through and first unshare everything.
1308 for (i = 0; i < used; i++) {
1309 zfs_share_proto_t *curr_proto;
1310 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
1312 if (is_shared(hdl, mountpoints[i], *curr_proto) &&
1313 unshare_one(hdl, mountpoints[i],
1314 mountpoints[i], *curr_proto) != 0)
1320 * Now unmount everything, removing the underlying directories as
1323 for (i = 0; i < used; i++) {
1324 if (unmount_one(hdl, mountpoints[i], flags) != 0)
1328 for (i = 0; i < used; i++) {
1330 remove_mountpoint(datasets[i]);
1335 for (i = 0; i < used; i++) {
1337 zfs_close(datasets[i]);
1338 free(mountpoints[i]);