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.
24 * Copyright (c) 2014 by Delphix. All rights reserved.
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
51 * zfs_unshareall_nfs()
52 * zfs_unshareall_smb()
54 * zfs_unshareall_bypath()
56 * The following functions are available for pool consumers, and will
57 * mount/unmount and share/unshare all datasets within pool:
59 * zpool_enable_datasets()
60 * zpool_disable_datasets()
73 #include <sys/mntent.h>
74 #include <sys/mount.h>
79 #include "libzfs_impl.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) {
144 * the protocol field is the third field
145 * skip over second field
148 if ((tab = strchr(ptr, '\t')) == NULL)
151 if ((tab = strchr(ptr, '\t')) == NULL)
155 proto_table[proto].p_name) == 0) {
166 if (proto == PROTO_NFS)
172 return (SHARED_NOT_SHARED);
177 * Returns true if the specified directory is empty. If we can't open the
178 * directory at all, return true so that the mount can fail with a more
179 * informative error message.
182 dir_is_empty(const char *dirname)
187 if ((dirp = opendir(dirname)) == NULL)
190 while ((dp = readdir64(dirp)) != NULL) {
192 if (strcmp(dp->d_name, ".") == 0 ||
193 strcmp(dp->d_name, "..") == 0)
196 (void) closedir(dirp);
200 (void) closedir(dirp);
206 * Checks to see if the mount is active. If the filesystem is mounted, we fill
207 * in 'where' with the current mountpoint, and return 1. Otherwise, we return
211 is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where)
215 if (libzfs_mnttab_find(zfs_hdl, special, &entry) != 0)
219 *where = zfs_strdup(zfs_hdl, entry.mnt_mountp);
225 zfs_is_mounted(zfs_handle_t *zhp, char **where)
227 return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where));
231 * Returns true if the given dataset is mountable, false otherwise. Returns the
232 * mountpoint in 'buf'.
235 zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen,
236 zprop_source_t *source)
238 char sourceloc[ZFS_MAXNAMELEN];
239 zprop_source_t sourcetype;
241 if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type))
244 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen,
245 &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0);
247 if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 ||
248 strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0)
251 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF)
254 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
255 getzoneid() == GLOBAL_ZONEID)
259 *source = sourcetype;
265 * Mount the given filesystem.
268 zfs_mount(zfs_handle_t *zhp, const char *options, int flags)
271 char mountpoint[ZFS_MAXPROPLEN];
272 char mntopts[MNT_LINE_MAX];
273 libzfs_handle_t *hdl = zhp->zfs_hdl;
278 (void) strlcpy(mntopts, options, sizeof (mntopts));
281 * If the pool is imported read-only then all mounts must be read-only
283 if (zpool_get_prop_int(zhp->zpool_hdl, ZPOOL_PROP_READONLY, NULL))
286 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
289 /* Create the directory if it doesn't already exist */
290 if (lstat(mountpoint, &buf) != 0) {
291 if (mkdirp(mountpoint, 0755) != 0) {
292 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
293 "failed to create mountpoint"));
294 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
295 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
300 #ifdef illumos /* FreeBSD: overlay mounts are not checked. */
302 * Determine if the mountpoint is empty. If so, refuse to perform the
303 * mount. We don't perform this check if MS_OVERLAY is specified, which
304 * would defeat the point. We also avoid this check if 'remount' is
307 if ((flags & MS_OVERLAY) == 0 &&
308 strstr(mntopts, MNTOPT_REMOUNT) == NULL &&
309 !dir_is_empty(mountpoint)) {
310 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
311 "directory is not empty"));
312 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
313 dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint));
317 /* perform the mount */
318 if (zmount(zfs_get_name(zhp), mountpoint, flags,
319 MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) {
321 * Generic errors are nasty, but there are just way too many
322 * from mount(), and they're well-understood. We pick a few
323 * common ones to improve upon.
325 if (errno == EBUSY) {
326 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
327 "mountpoint or dataset is busy"));
328 } else if (errno == EPERM) {
329 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
330 "Insufficient privileges"));
331 } else if (errno == ENOTSUP) {
335 VERIFY(zfs_spa_version(zhp, &spa_version) == 0);
336 (void) snprintf(buf, sizeof (buf),
337 dgettext(TEXT_DOMAIN, "Can't mount a version %lld "
338 "file system on a version %d pool. Pool must be"
339 " upgraded to mount this file system."),
340 (u_longlong_t)zfs_prop_get_int(zhp,
341 ZFS_PROP_VERSION), spa_version);
342 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, buf));
344 zfs_error_aux(hdl, strerror(errno));
346 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
347 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
351 /* add the mounted entry into our cache */
352 libzfs_mnttab_add(hdl, zfs_get_name(zhp), mountpoint,
358 * Unmount a single filesystem.
361 unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags)
363 if (umount2(mountpoint, flags) != 0) {
364 zfs_error_aux(hdl, strerror(errno));
365 return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED,
366 dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
374 * Unmount the given filesystem.
377 zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
379 libzfs_handle_t *hdl = zhp->zfs_hdl;
383 /* check to see if we need to unmount the filesystem */
384 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
385 libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)) {
387 * mountpoint may have come from a call to
388 * getmnt/getmntany if it isn't NULL. If it is NULL,
389 * we know it comes from libzfs_mnttab_find which can
390 * then get freed later. We strdup it to play it safe.
392 if (mountpoint == NULL)
393 mntpt = zfs_strdup(hdl, entry.mnt_mountp);
395 mntpt = zfs_strdup(hdl, mountpoint);
398 * Unshare and unmount the filesystem
400 if (zfs_unshare_proto(zhp, mntpt, share_all_proto) != 0)
403 if (unmount_one(hdl, mntpt, flags) != 0) {
405 (void) zfs_shareall(zhp);
408 libzfs_mnttab_remove(hdl, zhp->zfs_name);
416 * Unmount this filesystem and any children inheriting the mountpoint property.
417 * To do this, just act like we're changing the mountpoint property, but don't
418 * remount the filesystems afterwards.
421 zfs_unmountall(zfs_handle_t *zhp, int flags)
423 prop_changelist_t *clp;
426 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, 0, flags);
430 ret = changelist_prefix(clp);
431 changelist_free(clp);
437 zfs_is_shared(zfs_handle_t *zhp)
439 zfs_share_type_t rc = 0;
440 zfs_share_proto_t *curr_proto;
442 if (ZFS_IS_VOLUME(zhp))
445 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
447 rc |= zfs_is_shared_proto(zhp, NULL, *curr_proto);
449 return (rc ? B_TRUE : B_FALSE);
453 zfs_share(zfs_handle_t *zhp)
455 assert(!ZFS_IS_VOLUME(zhp));
456 return (zfs_share_proto(zhp, share_all_proto));
460 zfs_unshare(zfs_handle_t *zhp)
462 assert(!ZFS_IS_VOLUME(zhp));
463 return (zfs_unshareall(zhp));
467 * Check to see if the filesystem is currently shared.
470 zfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto)
475 if (!zfs_is_mounted(zhp, &mountpoint))
476 return (SHARED_NOT_SHARED);
478 if (rc = is_shared(zhp->zfs_hdl, mountpoint, proto)) {
486 return (SHARED_NOT_SHARED);
491 zfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
493 return (zfs_is_shared_proto(zhp, where,
494 PROTO_NFS) != SHARED_NOT_SHARED);
498 zfs_is_shared_smb(zfs_handle_t *zhp, char **where)
500 return (zfs_is_shared_proto(zhp, where,
501 PROTO_SMB) != SHARED_NOT_SHARED);
505 * Make sure things will work if libshare isn't installed by using
506 * wrapper functions that check to see that the pointers to functions
507 * initialized in _zfs_init_libshare() are actually present.
511 static sa_handle_t (*_sa_init)(int);
512 static void (*_sa_fini)(sa_handle_t);
513 static sa_share_t (*_sa_find_share)(sa_handle_t, char *);
514 static int (*_sa_enable_share)(sa_share_t, char *);
515 static int (*_sa_disable_share)(sa_share_t, char *);
516 static char *(*_sa_errorstr)(int);
517 static int (*_sa_parse_legacy_options)(sa_group_t, char *, char *);
518 static boolean_t (*_sa_needs_refresh)(sa_handle_t *);
519 static libzfs_handle_t *(*_sa_get_zfs_handle)(sa_handle_t);
520 static int (*_sa_zfs_process_share)(sa_handle_t, sa_group_t, sa_share_t,
521 char *, char *, zprop_source_t, char *, char *, char *);
522 static void (*_sa_update_sharetab_ts)(sa_handle_t);
526 * _zfs_init_libshare()
528 * Find the libshare.so.1 entry points that we use here and save the
529 * values to be used later. This is triggered by the runtime loader.
530 * Make sure the correct ISA version is loaded.
533 #pragma init(_zfs_init_libshare)
535 _zfs_init_libshare(void)
539 char path[MAXPATHLEN];
543 if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1)
548 (void) snprintf(path, MAXPATHLEN,
549 "/usr/lib/%s/libshare.so.1", isa);
551 if ((libshare = dlopen(path, RTLD_LAZY | RTLD_GLOBAL)) != NULL) {
552 _sa_init = (sa_handle_t (*)(int))dlsym(libshare, "sa_init");
553 _sa_fini = (void (*)(sa_handle_t))dlsym(libshare, "sa_fini");
554 _sa_find_share = (sa_share_t (*)(sa_handle_t, char *))
555 dlsym(libshare, "sa_find_share");
556 _sa_enable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
558 _sa_disable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
560 _sa_errorstr = (char *(*)(int))dlsym(libshare, "sa_errorstr");
561 _sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *))
562 dlsym(libshare, "sa_parse_legacy_options");
563 _sa_needs_refresh = (boolean_t (*)(sa_handle_t *))
564 dlsym(libshare, "sa_needs_refresh");
565 _sa_get_zfs_handle = (libzfs_handle_t *(*)(sa_handle_t))
566 dlsym(libshare, "sa_get_zfs_handle");
567 _sa_zfs_process_share = (int (*)(sa_handle_t, sa_group_t,
568 sa_share_t, char *, char *, zprop_source_t, char *,
569 char *, char *))dlsym(libshare, "sa_zfs_process_share");
570 _sa_update_sharetab_ts = (void (*)(sa_handle_t))
571 dlsym(libshare, "sa_update_sharetab_ts");
572 if (_sa_init == NULL || _sa_fini == NULL ||
573 _sa_find_share == NULL || _sa_enable_share == NULL ||
574 _sa_disable_share == NULL || _sa_errorstr == NULL ||
575 _sa_parse_legacy_options == NULL ||
576 _sa_needs_refresh == NULL || _sa_get_zfs_handle == NULL ||
577 _sa_zfs_process_share == NULL ||
578 _sa_update_sharetab_ts == NULL) {
581 _sa_disable_share = NULL;
582 _sa_enable_share = NULL;
584 _sa_parse_legacy_options = NULL;
585 (void) dlclose(libshare);
586 _sa_needs_refresh = NULL;
587 _sa_get_zfs_handle = NULL;
588 _sa_zfs_process_share = NULL;
589 _sa_update_sharetab_ts = NULL;
596 * zfs_init_libshare(zhandle, service)
598 * Initialize the libshare API if it hasn't already been initialized.
599 * In all cases it returns 0 if it succeeded and an error if not. The
600 * service value is which part(s) of the API to initialize and is a
601 * direct map to the libshare sa_init(service) interface.
604 zfs_init_libshare(libzfs_handle_t *zhandle, int service)
609 if (_sa_init == NULL)
612 if (ret == SA_OK && zhandle->libzfs_shareflags & ZFSSHARE_MISS) {
614 * We had a cache miss. Most likely it is a new ZFS
615 * dataset that was just created. We want to make sure
616 * so check timestamps to see if a different process
617 * has updated any of the configuration. If there was
618 * some non-ZFS change, we need to re-initialize the
621 zhandle->libzfs_shareflags &= ~ZFSSHARE_MISS;
622 if (_sa_needs_refresh != NULL &&
623 _sa_needs_refresh(zhandle->libzfs_sharehdl)) {
624 zfs_uninit_libshare(zhandle);
625 zhandle->libzfs_sharehdl = _sa_init(service);
629 if (ret == SA_OK && zhandle && zhandle->libzfs_sharehdl == NULL)
630 zhandle->libzfs_sharehdl = _sa_init(service);
632 if (ret == SA_OK && zhandle->libzfs_sharehdl == NULL)
640 * zfs_uninit_libshare(zhandle)
642 * Uninitialize the libshare API if it hasn't already been
643 * uninitialized. It is OK to call multiple times.
646 zfs_uninit_libshare(libzfs_handle_t *zhandle)
648 if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) {
650 if (_sa_fini != NULL)
651 _sa_fini(zhandle->libzfs_sharehdl);
653 zhandle->libzfs_sharehdl = NULL;
658 * zfs_parse_options(options, proto)
660 * Call the legacy parse interface to get the protocol specific
661 * options using the NULL arg to indicate that this is a "parse" only.
664 zfs_parse_options(char *options, zfs_share_proto_t proto)
667 if (_sa_parse_legacy_options != NULL) {
668 return (_sa_parse_legacy_options(NULL, options,
669 proto_table[proto].p_name));
671 return (SA_CONFIG_ERR);
679 * zfs_sa_find_share(handle, path)
681 * wrapper around sa_find_share to find a share path in the
685 zfs_sa_find_share(sa_handle_t handle, char *path)
687 if (_sa_find_share != NULL)
688 return (_sa_find_share(handle, path));
693 * zfs_sa_enable_share(share, proto)
695 * Wrapper for sa_enable_share which enables a share for a specified
699 zfs_sa_enable_share(sa_share_t share, char *proto)
701 if (_sa_enable_share != NULL)
702 return (_sa_enable_share(share, proto));
703 return (SA_CONFIG_ERR);
707 * zfs_sa_disable_share(share, proto)
709 * Wrapper for sa_enable_share which disables a share for a specified
713 zfs_sa_disable_share(sa_share_t share, char *proto)
715 if (_sa_disable_share != NULL)
716 return (_sa_disable_share(share, proto));
717 return (SA_CONFIG_ERR);
722 * Share the given filesystem according to the options in the specified
723 * protocol specific properties (sharenfs, sharesmb). We rely
724 * on "libshare" to the dirty work for us.
727 zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
729 char mountpoint[ZFS_MAXPROPLEN];
730 char shareopts[ZFS_MAXPROPLEN];
731 char sourcestr[ZFS_MAXPROPLEN];
732 libzfs_handle_t *hdl = zhp->zfs_hdl;
733 zfs_share_proto_t *curr_proto;
734 zprop_source_t sourcetype;
737 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
740 for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) {
742 * Return success if there are no share options.
744 if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop,
745 shareopts, sizeof (shareopts), &sourcetype, sourcestr,
746 ZFS_MAXPROPLEN, B_FALSE) != 0 ||
747 strcmp(shareopts, "off") == 0)
751 ret = zfs_init_libshare(hdl, SA_INIT_SHARE_API);
753 (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
754 dgettext(TEXT_DOMAIN, "cannot share '%s': %s"),
755 zfs_get_name(zhp), _sa_errorstr != NULL ?
756 _sa_errorstr(ret) : "");
762 * If the 'zoned' property is set, then zfs_is_mountable()
763 * will have already bailed out if we are in the global zone.
764 * But local zones cannot be NFS servers, so we ignore it for
765 * local zones as well.
767 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
771 share = zfs_sa_find_share(hdl->libzfs_sharehdl, mountpoint);
774 * This may be a new file system that was just
775 * created so isn't in the internal cache
776 * (second time through). Rather than
777 * reloading the entire configuration, we can
778 * assume ZFS has done the checking and it is
779 * safe to add this to the internal
782 if (_sa_zfs_process_share(hdl->libzfs_sharehdl,
783 NULL, NULL, mountpoint,
784 proto_table[*curr_proto].p_name, sourcetype,
785 shareopts, sourcestr, zhp->zfs_name) != SA_OK) {
786 (void) zfs_error_fmt(hdl,
787 proto_table[*curr_proto].p_share_err,
788 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
792 hdl->libzfs_shareflags |= ZFSSHARE_MISS;
793 share = zfs_sa_find_share(hdl->libzfs_sharehdl,
798 err = zfs_sa_enable_share(share,
799 proto_table[*curr_proto].p_name);
801 (void) zfs_error_fmt(hdl,
802 proto_table[*curr_proto].p_share_err,
803 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
809 if (*curr_proto != PROTO_NFS) {
810 fprintf(stderr, "Unsupported share protocol: %d.\n",
815 if (strcmp(shareopts, "on") == 0)
816 error = fsshare(ZFS_EXPORTS_PATH, mountpoint, "");
818 error = fsshare(ZFS_EXPORTS_PATH, mountpoint, shareopts);
822 (void) zfs_error_fmt(hdl,
823 proto_table[*curr_proto].p_share_err,
824 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
835 zfs_share_nfs(zfs_handle_t *zhp)
837 return (zfs_share_proto(zhp, nfs_only));
841 zfs_share_smb(zfs_handle_t *zhp)
843 return (zfs_share_proto(zhp, smb_only));
847 zfs_shareall(zfs_handle_t *zhp)
849 return (zfs_share_proto(zhp, share_all_proto));
853 * Unshare a filesystem by mountpoint.
856 unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint,
857 zfs_share_proto_t proto)
864 * Mountpoint could get trashed if libshare calls getmntany
865 * which it does during API initialization, so strdup the
868 mntpt = zfs_strdup(hdl, mountpoint);
870 /* make sure libshare initialized */
871 if ((err = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
872 free(mntpt); /* don't need the copy anymore */
873 return (zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
874 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
875 name, _sa_errorstr(err)));
878 share = zfs_sa_find_share(hdl->libzfs_sharehdl, mntpt);
879 free(mntpt); /* don't need the copy anymore */
882 err = zfs_sa_disable_share(share, proto_table[proto].p_name);
884 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
885 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
886 name, _sa_errorstr(err)));
889 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
890 dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"),
894 char buf[MAXPATHLEN];
898 if (proto != PROTO_NFS) {
899 fprintf(stderr, "No SMB support in FreeBSD yet.\n");
903 err = fsunshare(ZFS_EXPORTS_PATH, mountpoint);
905 zfs_error_aux(hdl, "%s", strerror(err));
906 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
907 dgettext(TEXT_DOMAIN,
908 "cannot unshare '%s'"), name));
915 * Unshare the given filesystem.
918 zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint,
919 zfs_share_proto_t *proto)
921 libzfs_handle_t *hdl = zhp->zfs_hdl;
925 /* check to see if need to unmount the filesystem */
926 rewind(zhp->zfs_hdl->libzfs_mnttab);
927 if (mountpoint != NULL)
928 mountpoint = mntpt = zfs_strdup(hdl, mountpoint);
930 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
931 libzfs_mnttab_find(hdl, zfs_get_name(zhp), &entry) == 0)) {
932 zfs_share_proto_t *curr_proto;
934 if (mountpoint == NULL)
935 mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp);
937 for (curr_proto = proto; *curr_proto != PROTO_END;
940 if (is_shared(hdl, mntpt, *curr_proto) &&
941 unshare_one(hdl, zhp->zfs_name,
942 mntpt, *curr_proto) != 0) {
956 zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
958 return (zfs_unshare_proto(zhp, mountpoint, nfs_only));
962 zfs_unshare_smb(zfs_handle_t *zhp, const char *mountpoint)
964 return (zfs_unshare_proto(zhp, mountpoint, smb_only));
968 * Same as zfs_unmountall(), but for NFS and SMB unshares.
971 zfs_unshareall_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
973 prop_changelist_t *clp;
976 clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0, 0);
980 ret = changelist_unshare(clp, proto);
981 changelist_free(clp);
987 zfs_unshareall_nfs(zfs_handle_t *zhp)
989 return (zfs_unshareall_proto(zhp, nfs_only));
993 zfs_unshareall_smb(zfs_handle_t *zhp)
995 return (zfs_unshareall_proto(zhp, smb_only));
999 zfs_unshareall(zfs_handle_t *zhp)
1001 return (zfs_unshareall_proto(zhp, share_all_proto));
1005 zfs_unshareall_bypath(zfs_handle_t *zhp, const char *mountpoint)
1007 return (zfs_unshare_proto(zhp, mountpoint, share_all_proto));
1011 * Remove the mountpoint associated with the current dataset, if necessary.
1012 * We only remove the underlying directory if:
1014 * - The mountpoint is not 'none' or 'legacy'
1015 * - The mountpoint is non-empty
1016 * - The mountpoint is the default or inherited
1017 * - The 'zoned' property is set, or we're in a local zone
1019 * Any other directories we leave alone.
1022 remove_mountpoint(zfs_handle_t *zhp)
1024 char mountpoint[ZFS_MAXPROPLEN];
1025 zprop_source_t source;
1027 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
1031 if (source == ZPROP_SRC_DEFAULT ||
1032 source == ZPROP_SRC_INHERITED) {
1034 * Try to remove the directory, silently ignoring any errors.
1035 * The filesystem may have since been removed or moved around,
1036 * and this error isn't really useful to the administrator in
1039 (void) rmdir(mountpoint);
1044 libzfs_add_handle(get_all_cb_t *cbp, zfs_handle_t *zhp)
1046 if (cbp->cb_alloc == cbp->cb_used) {
1050 newsz = cbp->cb_alloc ? cbp->cb_alloc * 2 : 64;
1051 ptr = zfs_realloc(zhp->zfs_hdl,
1052 cbp->cb_handles, cbp->cb_alloc * sizeof (void *),
1053 newsz * sizeof (void *));
1054 cbp->cb_handles = ptr;
1055 cbp->cb_alloc = newsz;
1057 cbp->cb_handles[cbp->cb_used++] = zhp;
1061 mount_cb(zfs_handle_t *zhp, void *data)
1063 get_all_cb_t *cbp = data;
1065 if (!(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM)) {
1070 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_NOAUTO) {
1076 * If this filesystem is inconsistent and has a receive resume
1077 * token, we can not mount it.
1079 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
1080 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
1081 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
1086 libzfs_add_handle(cbp, zhp);
1087 if (zfs_iter_filesystems(zhp, mount_cb, cbp) != 0) {
1095 libzfs_dataset_cmp(const void *a, const void *b)
1097 zfs_handle_t **za = (zfs_handle_t **)a;
1098 zfs_handle_t **zb = (zfs_handle_t **)b;
1099 char mounta[MAXPATHLEN];
1100 char mountb[MAXPATHLEN];
1101 boolean_t gota, gotb;
1103 if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
1104 verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
1105 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
1106 if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
1107 verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
1108 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
1111 return (strcmp(mounta, mountb));
1118 return (strcmp(zfs_get_name(a), zfs_get_name(b)));
1122 * Mount and share all datasets within the given pool. This assumes that no
1123 * datasets within the pool are currently mounted. Because users can create
1124 * complicated nested hierarchies of mountpoints, we first gather all the
1125 * datasets and mountpoints within the pool, and sort them by mountpoint. Once
1126 * we have the list of all filesystems, we iterate over them in order and mount
1127 * and/or share each one.
1129 #pragma weak zpool_mount_datasets = zpool_enable_datasets
1131 zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
1133 get_all_cb_t cb = { 0 };
1134 libzfs_handle_t *hdl = zhp->zpool_hdl;
1140 * Gather all non-snap datasets within the pool.
1142 if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_DATASET)) == NULL)
1145 libzfs_add_handle(&cb, zfsp);
1146 if (zfs_iter_filesystems(zfsp, mount_cb, &cb) != 0)
1149 * Sort the datasets by mountpoint.
1151 qsort(cb.cb_handles, cb.cb_used, sizeof (void *),
1152 libzfs_dataset_cmp);
1155 * And mount all the datasets, keeping track of which ones
1156 * succeeded or failed.
1158 if ((good = zfs_alloc(zhp->zpool_hdl,
1159 cb.cb_used * sizeof (int))) == NULL)
1163 for (i = 0; i < cb.cb_used; i++) {
1164 if (zfs_mount(cb.cb_handles[i], mntopts, flags) != 0)
1171 * Then share all the ones that need to be shared. This needs
1172 * to be a separate pass in order to avoid excessive reloading
1173 * of the configuration. Good should never be NULL since
1174 * zfs_alloc is supposed to exit if memory isn't available.
1176 for (i = 0; i < cb.cb_used; i++) {
1177 if (good[i] && zfs_share(cb.cb_handles[i]) != 0)
1184 for (i = 0; i < cb.cb_used; i++)
1185 zfs_close(cb.cb_handles[i]);
1186 free(cb.cb_handles);
1192 mountpoint_compare(const void *a, const void *b)
1194 const char *mounta = *((char **)a);
1195 const char *mountb = *((char **)b);
1197 return (strcmp(mountb, mounta));
1200 /* alias for 2002/240 */
1201 #pragma weak zpool_unmount_datasets = zpool_disable_datasets
1203 * Unshare and unmount all datasets within the given pool. We don't want to
1204 * rely on traversing the DSL to discover the filesystems within the pool,
1205 * because this may be expensive (if not all of them are mounted), and can fail
1206 * arbitrarily (on I/O error, for example). Instead, we walk /etc/mnttab and
1207 * gather all the filesystems that are currently mounted.
1210 zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
1213 struct mnttab entry;
1215 char **mountpoints = NULL;
1216 zfs_handle_t **datasets = NULL;
1217 libzfs_handle_t *hdl = zhp->zpool_hdl;
1220 int flags = (force ? MS_FORCE : 0);
1222 namelen = strlen(zhp->zpool_name);
1224 rewind(hdl->libzfs_mnttab);
1226 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
1228 * Ignore non-ZFS entries.
1230 if (entry.mnt_fstype == NULL ||
1231 strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
1235 * Ignore filesystems not within this pool.
1237 if (entry.mnt_mountp == NULL ||
1238 strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 ||
1239 (entry.mnt_special[namelen] != '/' &&
1240 entry.mnt_special[namelen] != '\0'))
1244 * At this point we've found a filesystem within our pool. Add
1245 * it to our growing list.
1247 if (used == alloc) {
1249 if ((mountpoints = zfs_alloc(hdl,
1250 8 * sizeof (void *))) == NULL)
1253 if ((datasets = zfs_alloc(hdl,
1254 8 * sizeof (void *))) == NULL)
1261 if ((ptr = zfs_realloc(hdl, mountpoints,
1262 alloc * sizeof (void *),
1263 alloc * 2 * sizeof (void *))) == NULL)
1267 if ((ptr = zfs_realloc(hdl, datasets,
1268 alloc * sizeof (void *),
1269 alloc * 2 * sizeof (void *))) == NULL)
1277 if ((mountpoints[used] = zfs_strdup(hdl,
1278 entry.mnt_mountp)) == NULL)
1282 * This is allowed to fail, in case there is some I/O error. It
1283 * is only used to determine if we need to remove the underlying
1284 * mountpoint, so failure is not fatal.
1286 datasets[used] = make_dataset_handle(hdl, entry.mnt_special);
1292 * At this point, we have the entire list of filesystems, so sort it by
1295 qsort(mountpoints, used, sizeof (char *), mountpoint_compare);
1298 * Walk through and first unshare everything.
1300 for (i = 0; i < used; i++) {
1301 zfs_share_proto_t *curr_proto;
1302 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
1304 if (is_shared(hdl, mountpoints[i], *curr_proto) &&
1305 unshare_one(hdl, mountpoints[i],
1306 mountpoints[i], *curr_proto) != 0)
1312 * Now unmount everything, removing the underlying directories as
1315 for (i = 0; i < used; i++) {
1316 if (unmount_one(hdl, mountpoints[i], flags) != 0)
1320 for (i = 0; i < used; i++) {
1322 remove_mountpoint(datasets[i]);
1327 for (i = 0; i < used; i++) {
1329 zfs_close(datasets[i]);
1330 free(mountpoints[i]);