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>
78 #include "libzfs_impl.h"
81 #include <sys/systeminfo.h>
82 #define MAXISALEN 257 /* based on sysinfo(2) man page */
84 static int zfs_share_proto(zfs_handle_t *, zfs_share_proto_t *);
85 zfs_share_type_t zfs_is_shared_proto(zfs_handle_t *, char **,
89 * The share protocols table must be in the same order as the zfs_share_prot_t
90 * enum in libzfs_impl.h
99 proto_table_t proto_table[PROTO_END] = {
100 {ZFS_PROP_SHARENFS, "nfs", EZFS_SHARENFSFAILED, EZFS_UNSHARENFSFAILED},
101 {ZFS_PROP_SHARESMB, "smb", EZFS_SHARESMBFAILED, EZFS_UNSHARESMBFAILED},
104 zfs_share_proto_t nfs_only[] = {
109 zfs_share_proto_t smb_only[] = {
113 zfs_share_proto_t share_all_proto[] = {
120 * Search the sharetab for the given mountpoint and protocol, returning
121 * a zfs_share_type_t value.
123 static zfs_share_type_t
124 is_shared(libzfs_handle_t *hdl, const char *mountpoint, zfs_share_proto_t proto)
126 char buf[MAXPATHLEN], *tab;
129 if (hdl->libzfs_sharetab == NULL)
130 return (SHARED_NOT_SHARED);
132 (void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET);
134 while (fgets(buf, sizeof (buf), hdl->libzfs_sharetab) != NULL) {
136 /* the mountpoint is the first entry on each line */
137 if ((tab = strchr(buf, '\t')) == NULL)
141 if (strcmp(buf, mountpoint) == 0) {
143 * the protocol field is the third field
144 * skip over second field
147 if ((tab = strchr(ptr, '\t')) == NULL)
150 if ((tab = strchr(ptr, '\t')) == NULL)
154 proto_table[proto].p_name) == 0) {
167 return (SHARED_NOT_SHARED);
171 * Returns true if the specified directory is empty. If we can't open the
172 * directory at all, return true so that the mount can fail with a more
173 * informative error message.
176 dir_is_empty(const char *dirname)
181 if ((dirp = opendir(dirname)) == NULL)
184 while ((dp = readdir64(dirp)) != NULL) {
186 if (strcmp(dp->d_name, ".") == 0 ||
187 strcmp(dp->d_name, "..") == 0)
190 (void) closedir(dirp);
194 (void) closedir(dirp);
199 * Checks to see if the mount is active. If the filesystem is mounted, we fill
200 * in 'where' with the current mountpoint, and return 1. Otherwise, we return
204 is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where)
208 if (libzfs_mnttab_find(zfs_hdl, special, &entry) != 0)
212 *where = zfs_strdup(zfs_hdl, entry.mnt_mountp);
218 zfs_is_mounted(zfs_handle_t *zhp, char **where)
220 return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where));
224 * Returns true if the given dataset is mountable, false otherwise. Returns the
225 * mountpoint in 'buf'.
228 zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen,
229 zprop_source_t *source)
231 char sourceloc[ZFS_MAXNAMELEN];
232 zprop_source_t sourcetype;
234 if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type))
237 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen,
238 &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0);
240 if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 ||
241 strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0)
244 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF)
247 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
248 getzoneid() == GLOBAL_ZONEID)
252 *source = sourcetype;
258 * The filesystem is mounted by invoking the system mount utility rather
259 * than by the system call mount(2). This ensures that the /etc/mtab
260 * file is correctly locked for the update. Performing our own locking
261 * and /etc/mtab update requires making an unsafe assumption about how
262 * the mount utility performs its locking. Unfortunately, this also means
263 * in the case of a mount failure we do not have the exact errno. We must
264 * make due with return value from the mount process.
266 * In the long term a shared library called libmount is under development
267 * which provides a common API to address the locking and errno issues.
268 * Once the standard mount utility has been updated to use this library
269 * we can add an autoconf check to conditionally use it.
271 * http://www.kernel.org/pub/linux/utils/util-linux/libmount-docs/index.html
275 do_mount(const char *src, const char *mntpt, char *opts)
286 /* Return only the most critical mount error */
287 rc = libzfs_run_process(argv[0], argv, STDOUT_VERBOSE|STDERR_VERBOSE);
289 if (rc & MOUNT_FILEIO)
293 if (rc & MOUNT_SOFTWARE)
295 if (rc & MOUNT_SYSERR)
297 if (rc & MOUNT_USAGE)
300 return ENXIO; /* Generic error */
307 do_unmount(const char *mntpt, int flags)
309 char force_opt[] = "-f";
310 char lazy_opt[] = "-l";
314 NULL, NULL, NULL, NULL };
317 if (flags & MS_FORCE) {
318 argv[count] = force_opt;
322 if (flags & MS_DETACH) {
323 argv[count] = lazy_opt;
327 argv[count] = (char *)mntpt;
328 rc = libzfs_run_process(argv[0], argv, STDOUT_VERBOSE|STDERR_VERBOSE);
330 return (rc ? EINVAL : 0);
334 zfs_add_option(zfs_handle_t *zhp, char *options, int len,
335 zfs_prop_t prop, char *on, char *off)
340 /* Skip adding duplicate default options */
341 if ((strstr(options, on) != NULL) || (strstr(options, off) != NULL))
345 * zfs_prop_get_int() to not used to ensure our mount options
346 * are not influenced by the current /etc/mtab contents.
348 value = getprop_uint64(zhp, prop, &source);
350 (void) strlcat(options, ",", len);
351 (void) strlcat(options, value ? on : off, len);
357 zfs_add_options(zfs_handle_t *zhp, char *options, int len)
361 error = zfs_add_option(zhp, options, len,
362 ZFS_PROP_ATIME, MNTOPT_ATIME, MNTOPT_NOATIME);
363 error = error ? error : zfs_add_option(zhp, options, len,
364 ZFS_PROP_DEVICES, MNTOPT_DEVICES, MNTOPT_NODEVICES);
365 error = error ? error : zfs_add_option(zhp, options, len,
366 ZFS_PROP_EXEC, MNTOPT_EXEC, MNTOPT_NOEXEC);
367 error = error ? error : zfs_add_option(zhp, options, len,
368 ZFS_PROP_READONLY, MNTOPT_RO, MNTOPT_RW);
369 error = error ? error : zfs_add_option(zhp, options, len,
370 ZFS_PROP_SETUID, MNTOPT_SETUID, MNTOPT_NOSETUID);
371 error = error ? error : zfs_add_option(zhp, options, len,
372 ZFS_PROP_XATTR, MNTOPT_XATTR, MNTOPT_NOXATTR);
373 error = error ? error : zfs_add_option(zhp, options, len,
374 ZFS_PROP_NBMAND, MNTOPT_NBMAND, MNTOPT_NONBMAND);
380 * Mount the given filesystem.
383 zfs_mount(zfs_handle_t *zhp, const char *options, int flags)
386 char mountpoint[ZFS_MAXPROPLEN];
387 char mntopts[MNT_LINE_MAX];
388 libzfs_handle_t *hdl = zhp->zfs_hdl;
391 if (options == NULL) {
392 (void) strlcpy(mntopts, MNTOPT_DEFAULTS, sizeof (mntopts));
394 (void) strlcpy(mntopts, options, sizeof (mntopts));
397 if (strstr(mntopts, MNTOPT_REMOUNT) != NULL)
401 * If the pool is imported read-only then all mounts must be read-only
403 if (zpool_get_prop_int(zhp->zpool_hdl, ZPOOL_PROP_READONLY, NULL))
404 (void) strlcat(mntopts, "," MNTOPT_RO, sizeof (mntopts));
407 * Append default mount options which apply to the mount point.
408 * This is done because under Linux (unlike Solaris) multiple mount
409 * points may reference a single super block. This means that just
410 * given a super block there is no back reference to update the per
411 * mount point options.
413 rc = zfs_add_options(zhp, mntopts, sizeof (mntopts));
415 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
416 "default options unavailable"));
417 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
418 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
423 * Append zfsutil option so the mount helper allow the mount
425 strlcat(mntopts, "," MNTOPT_ZFSUTIL, sizeof (mntopts));
427 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
430 /* Create the directory if it doesn't already exist */
431 if (lstat(mountpoint, &buf) != 0) {
432 if (mkdirp(mountpoint, 0755) != 0) {
433 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
434 "failed to create mountpoint"));
435 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
436 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
442 * Determine if the mountpoint is empty. If so, refuse to perform the
443 * mount. We don't perform this check if 'remount' is specified.
445 if (!remount && !dir_is_empty(mountpoint)) {
446 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
447 "directory is not empty"));
448 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
449 dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint));
452 /* perform the mount */
453 rc = do_mount(zfs_get_name(zhp), mountpoint, mntopts);
456 * Generic errors are nasty, but there are just way too many
457 * from mount(), and they're well-understood. We pick a few
458 * common ones to improve upon.
461 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
462 "mountpoint or dataset is busy"));
463 } else if (rc == EPERM) {
464 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
465 "Insufficient privileges"));
466 } else if (rc == ENOTSUP) {
470 VERIFY(zfs_spa_version(zhp, &spa_version) == 0);
471 (void) snprintf(buf, sizeof (buf),
472 dgettext(TEXT_DOMAIN, "Can't mount a version %lld "
473 "file system on a version %d pool. Pool must be"
474 " upgraded to mount this file system."),
475 (u_longlong_t)zfs_prop_get_int(zhp,
476 ZFS_PROP_VERSION), spa_version);
477 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, buf));
479 zfs_error_aux(hdl, strerror(rc));
481 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
482 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
486 /* remove the mounted entry before re-adding on remount */
488 libzfs_mnttab_remove(hdl, zhp->zfs_name);
490 /* add the mounted entry into our cache */
491 libzfs_mnttab_add(hdl, zfs_get_name(zhp), mountpoint, mntopts);
496 * Unmount a single filesystem.
499 unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags)
503 error = do_unmount(mountpoint, flags);
505 return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED,
506 dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
514 * Unmount the given filesystem.
517 zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
519 libzfs_handle_t *hdl = zhp->zfs_hdl;
523 /* check to see if we need to unmount the filesystem */
524 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
525 libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)) {
527 * mountpoint may have come from a call to
528 * getmnt/getmntany if it isn't NULL. If it is NULL,
529 * we know it comes from libzfs_mnttab_find which can
530 * then get freed later. We strdup it to play it safe.
532 if (mountpoint == NULL)
533 mntpt = zfs_strdup(hdl, entry.mnt_mountp);
535 mntpt = zfs_strdup(hdl, mountpoint);
538 * Unshare and unmount the filesystem
540 if (zfs_unshare_proto(zhp, mntpt, share_all_proto) != 0)
543 if (unmount_one(hdl, mntpt, flags) != 0) {
545 (void) zfs_shareall(zhp);
548 libzfs_mnttab_remove(hdl, zhp->zfs_name);
556 * Unmount this filesystem and any children inheriting the mountpoint property.
557 * To do this, just act like we're changing the mountpoint property, but don't
558 * remount the filesystems afterwards.
561 zfs_unmountall(zfs_handle_t *zhp, int flags)
563 prop_changelist_t *clp;
566 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, 0, flags);
570 ret = changelist_prefix(clp);
571 changelist_free(clp);
577 zfs_is_shared(zfs_handle_t *zhp)
579 zfs_share_type_t rc = 0;
580 zfs_share_proto_t *curr_proto;
582 if (ZFS_IS_VOLUME(zhp))
585 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
587 rc |= zfs_is_shared_proto(zhp, NULL, *curr_proto);
589 return (rc ? B_TRUE : B_FALSE);
593 zfs_share(zfs_handle_t *zhp)
595 assert(!ZFS_IS_VOLUME(zhp));
596 return (zfs_share_proto(zhp, share_all_proto));
600 zfs_unshare(zfs_handle_t *zhp)
602 assert(!ZFS_IS_VOLUME(zhp));
603 return (zfs_unshareall(zhp));
607 * Check to see if the filesystem is currently shared.
610 zfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto)
615 if (!zfs_is_mounted(zhp, &mountpoint))
616 return (SHARED_NOT_SHARED);
618 if ((rc = is_shared(zhp->zfs_hdl, mountpoint, proto))) {
626 return (SHARED_NOT_SHARED);
631 zfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
633 return (zfs_is_shared_proto(zhp, where,
634 PROTO_NFS) != SHARED_NOT_SHARED);
638 zfs_is_shared_smb(zfs_handle_t *zhp, char **where)
640 return (zfs_is_shared_proto(zhp, where,
641 PROTO_SMB) != SHARED_NOT_SHARED);
645 * zfs_init_libshare(zhandle, service)
647 * Initialize the libshare API if it hasn't already been initialized.
648 * In all cases it returns 0 if it succeeded and an error if not. The
649 * service value is which part(s) of the API to initialize and is a
650 * direct map to the libshare sa_init(service) interface.
653 zfs_init_libshare(libzfs_handle_t *zhandle, int service)
657 if (ret == SA_OK && zhandle->libzfs_shareflags & ZFSSHARE_MISS) {
659 * We had a cache miss. Most likely it is a new ZFS
660 * dataset that was just created. We want to make sure
661 * so check timestamps to see if a different process
662 * has updated any of the configuration. If there was
663 * some non-ZFS change, we need to re-initialize the
666 zhandle->libzfs_shareflags &= ~ZFSSHARE_MISS;
667 if (sa_needs_refresh(zhandle->libzfs_sharehdl)) {
668 zfs_uninit_libshare(zhandle);
669 zhandle->libzfs_sharehdl = sa_init(service);
673 if (ret == SA_OK && zhandle && zhandle->libzfs_sharehdl == NULL)
674 zhandle->libzfs_sharehdl = sa_init(service);
676 if (ret == SA_OK && zhandle->libzfs_sharehdl == NULL)
683 * zfs_uninit_libshare(zhandle)
685 * Uninitialize the libshare API if it hasn't already been
686 * uninitialized. It is OK to call multiple times.
689 zfs_uninit_libshare(libzfs_handle_t *zhandle)
691 if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) {
692 sa_fini(zhandle->libzfs_sharehdl);
693 zhandle->libzfs_sharehdl = NULL;
698 * zfs_parse_options(options, proto)
700 * Call the legacy parse interface to get the protocol specific
701 * options using the NULL arg to indicate that this is a "parse" only.
704 zfs_parse_options(char *options, zfs_share_proto_t proto)
706 return (sa_parse_legacy_options(NULL, options,
707 proto_table[proto].p_name));
711 * Share the given filesystem according to the options in the specified
712 * protocol specific properties (sharenfs, sharesmb). We rely
713 * on "libshare" to the dirty work for us.
716 zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
718 char mountpoint[ZFS_MAXPROPLEN];
719 char shareopts[ZFS_MAXPROPLEN];
720 char sourcestr[ZFS_MAXPROPLEN];
721 libzfs_handle_t *hdl = zhp->zfs_hdl;
723 zfs_share_proto_t *curr_proto;
724 zprop_source_t sourcetype;
727 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
730 if ((ret = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
731 (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
732 dgettext(TEXT_DOMAIN, "cannot share '%s': %s"),
733 zfs_get_name(zhp), sa_errorstr(ret));
737 for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) {
739 * Return success if there are no share options.
741 if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop,
742 shareopts, sizeof (shareopts), &sourcetype, sourcestr,
743 ZFS_MAXPROPLEN, B_FALSE) != 0 ||
744 strcmp(shareopts, "off") == 0)
748 * If the 'zoned' property is set, then zfs_is_mountable()
749 * will have already bailed out if we are in the global zone.
750 * But local zones cannot be NFS servers, so we ignore it for
751 * local zones as well.
753 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
756 share = sa_find_share(hdl->libzfs_sharehdl, mountpoint);
759 * This may be a new file system that was just
760 * created so isn't in the internal cache
761 * (second time through). Rather than
762 * reloading the entire configuration, we can
763 * assume ZFS has done the checking and it is
764 * safe to add this to the internal
767 if (sa_zfs_process_share(hdl->libzfs_sharehdl,
768 NULL, NULL, mountpoint,
769 proto_table[*curr_proto].p_name, sourcetype,
770 shareopts, sourcestr, zhp->zfs_name) != SA_OK) {
771 (void) zfs_error_fmt(hdl,
772 proto_table[*curr_proto].p_share_err,
773 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
777 hdl->libzfs_shareflags |= ZFSSHARE_MISS;
778 share = sa_find_share(hdl->libzfs_sharehdl,
783 err = sa_enable_share(share,
784 proto_table[*curr_proto].p_name);
786 (void) zfs_error_fmt(hdl,
787 proto_table[*curr_proto].p_share_err,
788 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
793 (void) zfs_error_fmt(hdl,
794 proto_table[*curr_proto].p_share_err,
795 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
806 zfs_share_nfs(zfs_handle_t *zhp)
808 return (zfs_share_proto(zhp, nfs_only));
812 zfs_share_smb(zfs_handle_t *zhp)
814 return (zfs_share_proto(zhp, smb_only));
818 zfs_shareall(zfs_handle_t *zhp)
820 return (zfs_share_proto(zhp, share_all_proto));
824 * Unshare a filesystem by mountpoint.
827 unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint,
828 zfs_share_proto_t proto)
834 * Mountpoint could get trashed if libshare calls getmntany
835 * which it does during API initialization, so strdup the
838 mntpt = zfs_strdup(hdl, mountpoint);
840 /* make sure libshare initialized */
841 if ((err = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
842 free(mntpt); /* don't need the copy anymore */
843 return (zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
844 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
845 name, sa_errorstr(err)));
848 share = sa_find_share(hdl->libzfs_sharehdl, mntpt);
849 free(mntpt); /* don't need the copy anymore */
852 err = sa_disable_share(share, proto_table[proto].p_name);
854 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
855 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
856 name, sa_errorstr(err)));
859 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
860 dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"),
867 * Unshare the given filesystem.
870 zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint,
871 zfs_share_proto_t *proto)
873 libzfs_handle_t *hdl = zhp->zfs_hdl;
877 /* check to see if need to unmount the filesystem */
878 rewind(zhp->zfs_hdl->libzfs_mnttab);
879 if (mountpoint != NULL)
880 mountpoint = mntpt = zfs_strdup(hdl, mountpoint);
882 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
883 libzfs_mnttab_find(hdl, zfs_get_name(zhp), &entry) == 0)) {
884 zfs_share_proto_t *curr_proto;
886 if (mountpoint == NULL)
887 mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp);
889 for (curr_proto = proto; *curr_proto != PROTO_END;
892 if (is_shared(hdl, mntpt, *curr_proto) &&
893 unshare_one(hdl, zhp->zfs_name,
894 mntpt, *curr_proto) != 0) {
908 zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
910 return (zfs_unshare_proto(zhp, mountpoint, nfs_only));
914 zfs_unshare_smb(zfs_handle_t *zhp, const char *mountpoint)
916 return (zfs_unshare_proto(zhp, mountpoint, smb_only));
920 * Same as zfs_unmountall(), but for NFS and SMB unshares.
923 zfs_unshareall_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
925 prop_changelist_t *clp;
928 clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0, 0);
932 ret = changelist_unshare(clp, proto);
933 changelist_free(clp);
939 zfs_unshareall_nfs(zfs_handle_t *zhp)
941 return (zfs_unshareall_proto(zhp, nfs_only));
945 zfs_unshareall_smb(zfs_handle_t *zhp)
947 return (zfs_unshareall_proto(zhp, smb_only));
951 zfs_unshareall(zfs_handle_t *zhp)
953 return (zfs_unshareall_proto(zhp, share_all_proto));
957 zfs_unshareall_bypath(zfs_handle_t *zhp, const char *mountpoint)
959 return (zfs_unshare_proto(zhp, mountpoint, share_all_proto));
963 * Remove the mountpoint associated with the current dataset, if necessary.
964 * We only remove the underlying directory if:
966 * - The mountpoint is not 'none' or 'legacy'
967 * - The mountpoint is non-empty
968 * - The mountpoint is the default or inherited
969 * - The 'zoned' property is set, or we're in a local zone
971 * Any other directories we leave alone.
974 remove_mountpoint(zfs_handle_t *zhp)
976 char mountpoint[ZFS_MAXPROPLEN];
977 zprop_source_t source;
979 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
983 if (source == ZPROP_SRC_DEFAULT ||
984 source == ZPROP_SRC_INHERITED) {
986 * Try to remove the directory, silently ignoring any errors.
987 * The filesystem may have since been removed or moved around,
988 * and this error isn't really useful to the administrator in
991 (void) rmdir(mountpoint);
996 libzfs_add_handle(get_all_cb_t *cbp, zfs_handle_t *zhp)
998 if (cbp->cb_alloc == cbp->cb_used) {
1002 newsz = cbp->cb_alloc ? cbp->cb_alloc * 2 : 64;
1003 ptr = zfs_realloc(zhp->zfs_hdl,
1004 cbp->cb_handles, cbp->cb_alloc * sizeof (void *),
1005 newsz * sizeof (void *));
1006 cbp->cb_handles = ptr;
1007 cbp->cb_alloc = newsz;
1009 cbp->cb_handles[cbp->cb_used++] = zhp;
1013 mount_cb(zfs_handle_t *zhp, void *data)
1015 get_all_cb_t *cbp = data;
1017 if (!(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM)) {
1022 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_NOAUTO) {
1027 libzfs_add_handle(cbp, zhp);
1028 if (zfs_iter_filesystems(zhp, mount_cb, cbp) != 0) {
1036 libzfs_dataset_cmp(const void *a, const void *b)
1038 zfs_handle_t **za = (zfs_handle_t **)a;
1039 zfs_handle_t **zb = (zfs_handle_t **)b;
1040 char mounta[MAXPATHLEN];
1041 char mountb[MAXPATHLEN];
1042 boolean_t gota, gotb;
1044 if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
1045 verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
1046 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
1047 if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
1048 verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
1049 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
1052 return (strcmp(mounta, mountb));
1059 return (strcmp(zfs_get_name(a), zfs_get_name(b)));
1063 * Mount and share all datasets within the given pool. This assumes that no
1064 * datasets within the pool are currently mounted. Because users can create
1065 * complicated nested hierarchies of mountpoints, we first gather all the
1066 * datasets and mountpoints within the pool, and sort them by mountpoint. Once
1067 * we have the list of all filesystems, we iterate over them in order and mount
1068 * and/or share each one.
1070 #pragma weak zpool_mount_datasets = zpool_enable_datasets
1072 zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
1074 get_all_cb_t cb = { 0 };
1075 libzfs_handle_t *hdl = zhp->zpool_hdl;
1081 * Gather all non-snap datasets within the pool.
1083 if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_DATASET)) == NULL)
1086 libzfs_add_handle(&cb, zfsp);
1087 if (zfs_iter_filesystems(zfsp, mount_cb, &cb) != 0)
1090 * Sort the datasets by mountpoint.
1092 qsort(cb.cb_handles, cb.cb_used, sizeof (void *),
1093 libzfs_dataset_cmp);
1096 * And mount all the datasets, keeping track of which ones
1097 * succeeded or failed.
1099 if ((good = zfs_alloc(zhp->zpool_hdl,
1100 cb.cb_used * sizeof (int))) == NULL)
1104 for (i = 0; i < cb.cb_used; i++) {
1105 if (zfs_mount(cb.cb_handles[i], mntopts, flags) != 0)
1112 * Then share all the ones that need to be shared. This needs
1113 * to be a separate pass in order to avoid excessive reloading
1114 * of the configuration. Good should never be NULL since
1115 * zfs_alloc is supposed to exit if memory isn't available.
1117 for (i = 0; i < cb.cb_used; i++) {
1118 if (good[i] && zfs_share(cb.cb_handles[i]) != 0)
1125 for (i = 0; i < cb.cb_used; i++)
1126 zfs_close(cb.cb_handles[i]);
1127 free(cb.cb_handles);
1133 mountpoint_compare(const void *a, const void *b)
1135 const char *mounta = *((char **)a);
1136 const char *mountb = *((char **)b);
1138 return (strcmp(mountb, mounta));
1141 /* alias for 2002/240 */
1142 #pragma weak zpool_unmount_datasets = zpool_disable_datasets
1144 * Unshare and unmount all datasets within the given pool. We don't want to
1145 * rely on traversing the DSL to discover the filesystems within the pool,
1146 * because this may be expensive (if not all of them are mounted), and can fail
1147 * arbitrarily (on I/O error, for example). Instead, we walk /etc/mtab and
1148 * gather all the filesystems that are currently mounted.
1151 zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
1154 struct mnttab entry;
1156 char **mountpoints = NULL;
1157 zfs_handle_t **datasets = NULL;
1158 libzfs_handle_t *hdl = zhp->zpool_hdl;
1161 int flags = (force ? MS_FORCE : 0);
1163 namelen = strlen(zhp->zpool_name);
1165 rewind(hdl->libzfs_mnttab);
1167 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
1169 * Ignore non-ZFS entries.
1171 if (entry.mnt_fstype == NULL ||
1172 strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
1176 * Ignore filesystems not within this pool.
1178 if (entry.mnt_mountp == NULL ||
1179 strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 ||
1180 (entry.mnt_special[namelen] != '/' &&
1181 entry.mnt_special[namelen] != '\0'))
1185 * At this point we've found a filesystem within our pool. Add
1186 * it to our growing list.
1188 if (used == alloc) {
1190 if ((mountpoints = zfs_alloc(hdl,
1191 8 * sizeof (void *))) == NULL)
1194 if ((datasets = zfs_alloc(hdl,
1195 8 * sizeof (void *))) == NULL)
1202 if ((ptr = zfs_realloc(hdl, mountpoints,
1203 alloc * sizeof (void *),
1204 alloc * 2 * sizeof (void *))) == NULL)
1208 if ((ptr = zfs_realloc(hdl, datasets,
1209 alloc * sizeof (void *),
1210 alloc * 2 * sizeof (void *))) == NULL)
1218 if ((mountpoints[used] = zfs_strdup(hdl,
1219 entry.mnt_mountp)) == NULL)
1223 * This is allowed to fail, in case there is some I/O error. It
1224 * is only used to determine if we need to remove the underlying
1225 * mountpoint, so failure is not fatal.
1227 datasets[used] = make_dataset_handle(hdl, entry.mnt_special);
1233 * At this point, we have the entire list of filesystems, so sort it by
1236 qsort(mountpoints, used, sizeof (char *), mountpoint_compare);
1239 * Walk through and first unshare everything.
1241 for (i = 0; i < used; i++) {
1242 zfs_share_proto_t *curr_proto;
1243 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
1245 if (is_shared(hdl, mountpoints[i], *curr_proto) &&
1246 unshare_one(hdl, mountpoints[i],
1247 mountpoints[i], *curr_proto) != 0)
1253 * Now unmount everything, removing the underlying directories as
1256 for (i = 0; i < used; i++) {
1257 if (unmount_one(hdl, mountpoints[i], flags) != 0)
1261 for (i = 0; i < used; i++) {
1263 remove_mountpoint(datasets[i]);
1268 for (i = 0; i < used; i++) {
1270 zfs_close(datasets[i]);
1271 free(mountpoints[i]);