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 #define MAXISALEN 257 /* based on sysinfo(2) man page */
83 static int zfs_share_proto(zfs_handle_t *, zfs_share_proto_t *);
84 zfs_share_type_t zfs_is_shared_proto(zfs_handle_t *, char **,
88 * The share protocols table must be in the same order as the zfs_share_prot_t
89 * enum in libzfs_impl.h
98 proto_table_t proto_table[PROTO_END] = {
99 {ZFS_PROP_SHARENFS, "nfs", EZFS_SHARENFSFAILED, EZFS_UNSHARENFSFAILED},
100 {ZFS_PROP_SHARESMB, "smb", EZFS_SHARESMBFAILED, EZFS_UNSHARESMBFAILED},
103 zfs_share_proto_t nfs_only[] = {
108 zfs_share_proto_t smb_only[] = {
112 zfs_share_proto_t share_all_proto[] = {
119 * Search the sharetab for the given mountpoint and protocol, returning
120 * a zfs_share_type_t value.
122 static zfs_share_type_t
123 is_shared(libzfs_handle_t *hdl, const char *mountpoint, zfs_share_proto_t proto)
125 char buf[MAXPATHLEN], *tab;
128 if (hdl->libzfs_sharetab == NULL)
129 return (SHARED_NOT_SHARED);
131 (void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET);
133 while (fgets(buf, sizeof (buf), hdl->libzfs_sharetab) != NULL) {
135 /* the mountpoint is the first entry on each line */
136 if ((tab = strchr(buf, '\t')) == NULL)
140 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) {
165 if (proto == PROTO_NFS)
171 return (SHARED_NOT_SHARED);
176 * Returns true if the specified directory is empty. If we can't open the
177 * directory at all, return true so that the mount can fail with a more
178 * informative error message.
181 dir_is_empty(const char *dirname)
186 if ((dirp = opendir(dirname)) == NULL)
189 while ((dp = readdir64(dirp)) != NULL) {
191 if (strcmp(dp->d_name, ".") == 0 ||
192 strcmp(dp->d_name, "..") == 0)
195 (void) closedir(dirp);
199 (void) closedir(dirp);
205 * Checks to see if the mount is active. If the filesystem is mounted, we fill
206 * in 'where' with the current mountpoint, and return 1. Otherwise, we return
210 is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where)
214 if (libzfs_mnttab_find(zfs_hdl, special, &entry) != 0)
218 *where = zfs_strdup(zfs_hdl, entry.mnt_mountp);
224 zfs_is_mounted(zfs_handle_t *zhp, char **where)
226 return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where));
230 * Returns true if the given dataset is mountable, false otherwise. Returns the
231 * mountpoint in 'buf'.
234 zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen,
235 zprop_source_t *source)
237 char sourceloc[ZFS_MAXNAMELEN];
238 zprop_source_t sourcetype;
240 if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type))
243 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen,
244 &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0);
246 if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 ||
247 strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0)
250 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF)
253 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
254 getzoneid() == GLOBAL_ZONEID)
258 *source = sourcetype;
264 * Mount the given filesystem.
267 zfs_mount(zfs_handle_t *zhp, const char *options, int flags)
270 char mountpoint[ZFS_MAXPROPLEN];
271 char mntopts[MNT_LINE_MAX];
272 libzfs_handle_t *hdl = zhp->zfs_hdl;
277 (void) strlcpy(mntopts, options, sizeof (mntopts));
280 * If the pool is imported read-only then all mounts must be read-only
282 if (zpool_get_prop_int(zhp->zpool_hdl, ZPOOL_PROP_READONLY, NULL))
285 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
288 /* Create the directory if it doesn't already exist */
289 if (lstat(mountpoint, &buf) != 0) {
290 if (mkdirp(mountpoint, 0755) != 0) {
291 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
292 "failed to create mountpoint"));
293 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
294 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
299 #ifdef sun /* FreeBSD: overlay mounts are not checked. */
301 * Determine if the mountpoint is empty. If so, refuse to perform the
302 * mount. We don't perform this check if MS_OVERLAY is specified, which
303 * would defeat the point. We also avoid this check if 'remount' is
306 if ((flags & MS_OVERLAY) == 0 &&
307 strstr(mntopts, MNTOPT_REMOUNT) == NULL &&
308 !dir_is_empty(mountpoint)) {
309 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
310 "directory is not empty"));
311 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
312 dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint));
316 /* perform the mount */
317 if (zmount(zfs_get_name(zhp), mountpoint, flags,
318 MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) {
320 * Generic errors are nasty, but there are just way too many
321 * from mount(), and they're well-understood. We pick a few
322 * common ones to improve upon.
324 if (errno == EBUSY) {
325 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
326 "mountpoint or dataset is busy"));
327 } else if (errno == EPERM) {
328 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
329 "Insufficient privileges"));
330 } else if (errno == ENOTSUP) {
334 VERIFY(zfs_spa_version(zhp, &spa_version) == 0);
335 (void) snprintf(buf, sizeof (buf),
336 dgettext(TEXT_DOMAIN, "Can't mount a version %lld "
337 "file system on a version %d pool. Pool must be"
338 " upgraded to mount this file system."),
339 (u_longlong_t)zfs_prop_get_int(zhp,
340 ZFS_PROP_VERSION), spa_version);
341 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, buf));
343 zfs_error_aux(hdl, strerror(errno));
345 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
346 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
350 /* add the mounted entry into our cache */
351 libzfs_mnttab_add(hdl, zfs_get_name(zhp), mountpoint,
357 * Unmount a single filesystem.
360 unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags)
362 if (umount2(mountpoint, flags) != 0) {
363 zfs_error_aux(hdl, strerror(errno));
364 return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED,
365 dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
373 * Unmount the given filesystem.
376 zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
378 libzfs_handle_t *hdl = zhp->zfs_hdl;
382 /* check to see if we need to unmount the filesystem */
383 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
384 libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)) {
386 * mountpoint may have come from a call to
387 * getmnt/getmntany if it isn't NULL. If it is NULL,
388 * we know it comes from libzfs_mnttab_find which can
389 * then get freed later. We strdup it to play it safe.
391 if (mountpoint == NULL)
392 mntpt = zfs_strdup(hdl, entry.mnt_mountp);
394 mntpt = zfs_strdup(hdl, mountpoint);
397 * Unshare and unmount the filesystem
399 if (zfs_unshare_proto(zhp, mntpt, share_all_proto) != 0)
402 if (unmount_one(hdl, mntpt, flags) != 0) {
404 (void) zfs_shareall(zhp);
407 libzfs_mnttab_remove(hdl, zhp->zfs_name);
415 * Unmount this filesystem and any children inheriting the mountpoint property.
416 * To do this, just act like we're changing the mountpoint property, but don't
417 * remount the filesystems afterwards.
420 zfs_unmountall(zfs_handle_t *zhp, int flags)
422 prop_changelist_t *clp;
425 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, 0, flags);
429 ret = changelist_prefix(clp);
430 changelist_free(clp);
436 zfs_is_shared(zfs_handle_t *zhp)
438 zfs_share_type_t rc = 0;
439 zfs_share_proto_t *curr_proto;
441 if (ZFS_IS_VOLUME(zhp))
444 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
446 rc |= zfs_is_shared_proto(zhp, NULL, *curr_proto);
448 return (rc ? B_TRUE : B_FALSE);
452 zfs_share(zfs_handle_t *zhp)
454 assert(!ZFS_IS_VOLUME(zhp));
455 return (zfs_share_proto(zhp, share_all_proto));
459 zfs_unshare(zfs_handle_t *zhp)
461 assert(!ZFS_IS_VOLUME(zhp));
462 return (zfs_unshareall(zhp));
466 * Check to see if the filesystem is currently shared.
469 zfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto)
474 if (!zfs_is_mounted(zhp, &mountpoint))
475 return (SHARED_NOT_SHARED);
477 if (rc = is_shared(zhp->zfs_hdl, mountpoint, proto)) {
485 return (SHARED_NOT_SHARED);
490 zfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
492 return (zfs_is_shared_proto(zhp, where,
493 PROTO_NFS) != SHARED_NOT_SHARED);
497 zfs_is_shared_smb(zfs_handle_t *zhp, char **where)
499 return (zfs_is_shared_proto(zhp, where,
500 PROTO_SMB) != SHARED_NOT_SHARED);
504 * Make sure things will work if libshare isn't installed by using
505 * wrapper functions that check to see that the pointers to functions
506 * initialized in _zfs_init_libshare() are actually present.
510 static sa_handle_t (*_sa_init)(int);
511 static void (*_sa_fini)(sa_handle_t);
512 static sa_share_t (*_sa_find_share)(sa_handle_t, char *);
513 static int (*_sa_enable_share)(sa_share_t, char *);
514 static int (*_sa_disable_share)(sa_share_t, char *);
515 static char *(*_sa_errorstr)(int);
516 static int (*_sa_parse_legacy_options)(sa_group_t, char *, char *);
517 static boolean_t (*_sa_needs_refresh)(sa_handle_t *);
518 static libzfs_handle_t *(*_sa_get_zfs_handle)(sa_handle_t);
519 static int (*_sa_zfs_process_share)(sa_handle_t, sa_group_t, sa_share_t,
520 char *, char *, zprop_source_t, char *, char *, char *);
521 static void (*_sa_update_sharetab_ts)(sa_handle_t);
525 * _zfs_init_libshare()
527 * Find the libshare.so.1 entry points that we use here and save the
528 * values to be used later. This is triggered by the runtime loader.
529 * Make sure the correct ISA version is loaded.
532 #pragma init(_zfs_init_libshare)
534 _zfs_init_libshare(void)
538 char path[MAXPATHLEN];
542 if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1)
547 (void) snprintf(path, MAXPATHLEN,
548 "/usr/lib/%s/libshare.so.1", isa);
550 if ((libshare = dlopen(path, RTLD_LAZY | RTLD_GLOBAL)) != NULL) {
551 _sa_init = (sa_handle_t (*)(int))dlsym(libshare, "sa_init");
552 _sa_fini = (void (*)(sa_handle_t))dlsym(libshare, "sa_fini");
553 _sa_find_share = (sa_share_t (*)(sa_handle_t, char *))
554 dlsym(libshare, "sa_find_share");
555 _sa_enable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
557 _sa_disable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
559 _sa_errorstr = (char *(*)(int))dlsym(libshare, "sa_errorstr");
560 _sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *))
561 dlsym(libshare, "sa_parse_legacy_options");
562 _sa_needs_refresh = (boolean_t (*)(sa_handle_t *))
563 dlsym(libshare, "sa_needs_refresh");
564 _sa_get_zfs_handle = (libzfs_handle_t *(*)(sa_handle_t))
565 dlsym(libshare, "sa_get_zfs_handle");
566 _sa_zfs_process_share = (int (*)(sa_handle_t, sa_group_t,
567 sa_share_t, char *, char *, zprop_source_t, char *,
568 char *, char *))dlsym(libshare, "sa_zfs_process_share");
569 _sa_update_sharetab_ts = (void (*)(sa_handle_t))
570 dlsym(libshare, "sa_update_sharetab_ts");
571 if (_sa_init == NULL || _sa_fini == NULL ||
572 _sa_find_share == NULL || _sa_enable_share == NULL ||
573 _sa_disable_share == NULL || _sa_errorstr == NULL ||
574 _sa_parse_legacy_options == NULL ||
575 _sa_needs_refresh == NULL || _sa_get_zfs_handle == NULL ||
576 _sa_zfs_process_share == NULL ||
577 _sa_update_sharetab_ts == NULL) {
580 _sa_disable_share = NULL;
581 _sa_enable_share = NULL;
583 _sa_parse_legacy_options = NULL;
584 (void) dlclose(libshare);
585 _sa_needs_refresh = NULL;
586 _sa_get_zfs_handle = NULL;
587 _sa_zfs_process_share = NULL;
588 _sa_update_sharetab_ts = NULL;
595 * zfs_init_libshare(zhandle, service)
597 * Initialize the libshare API if it hasn't already been initialized.
598 * In all cases it returns 0 if it succeeded and an error if not. The
599 * service value is which part(s) of the API to initialize and is a
600 * direct map to the libshare sa_init(service) interface.
603 zfs_init_libshare(libzfs_handle_t *zhandle, int service)
608 if (_sa_init == NULL)
611 if (ret == SA_OK && zhandle->libzfs_shareflags & ZFSSHARE_MISS) {
613 * We had a cache miss. Most likely it is a new ZFS
614 * dataset that was just created. We want to make sure
615 * so check timestamps to see if a different process
616 * has updated any of the configuration. If there was
617 * some non-ZFS change, we need to re-initialize the
620 zhandle->libzfs_shareflags &= ~ZFSSHARE_MISS;
621 if (_sa_needs_refresh != NULL &&
622 _sa_needs_refresh(zhandle->libzfs_sharehdl)) {
623 zfs_uninit_libshare(zhandle);
624 zhandle->libzfs_sharehdl = _sa_init(service);
628 if (ret == SA_OK && zhandle && zhandle->libzfs_sharehdl == NULL)
629 zhandle->libzfs_sharehdl = _sa_init(service);
631 if (ret == SA_OK && zhandle->libzfs_sharehdl == NULL)
639 * zfs_uninit_libshare(zhandle)
641 * Uninitialize the libshare API if it hasn't already been
642 * uninitialized. It is OK to call multiple times.
645 zfs_uninit_libshare(libzfs_handle_t *zhandle)
647 if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) {
649 if (_sa_fini != NULL)
650 _sa_fini(zhandle->libzfs_sharehdl);
652 zhandle->libzfs_sharehdl = NULL;
657 * zfs_parse_options(options, proto)
659 * Call the legacy parse interface to get the protocol specific
660 * options using the NULL arg to indicate that this is a "parse" only.
663 zfs_parse_options(char *options, zfs_share_proto_t proto)
666 if (_sa_parse_legacy_options != NULL) {
667 return (_sa_parse_legacy_options(NULL, options,
668 proto_table[proto].p_name));
670 return (SA_CONFIG_ERR);
678 * zfs_sa_find_share(handle, path)
680 * wrapper around sa_find_share to find a share path in the
684 zfs_sa_find_share(sa_handle_t handle, char *path)
686 if (_sa_find_share != NULL)
687 return (_sa_find_share(handle, path));
692 * zfs_sa_enable_share(share, proto)
694 * Wrapper for sa_enable_share which enables a share for a specified
698 zfs_sa_enable_share(sa_share_t share, char *proto)
700 if (_sa_enable_share != NULL)
701 return (_sa_enable_share(share, proto));
702 return (SA_CONFIG_ERR);
706 * zfs_sa_disable_share(share, proto)
708 * Wrapper for sa_enable_share which disables a share for a specified
712 zfs_sa_disable_share(sa_share_t share, char *proto)
714 if (_sa_disable_share != NULL)
715 return (_sa_disable_share(share, proto));
716 return (SA_CONFIG_ERR);
721 * Share the given filesystem according to the options in the specified
722 * protocol specific properties (sharenfs, sharesmb). We rely
723 * on "libshare" to the dirty work for us.
726 zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
728 char mountpoint[ZFS_MAXPROPLEN];
729 char shareopts[ZFS_MAXPROPLEN];
730 char sourcestr[ZFS_MAXPROPLEN];
731 libzfs_handle_t *hdl = zhp->zfs_hdl;
732 zfs_share_proto_t *curr_proto;
733 zprop_source_t sourcetype;
736 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
740 if ((ret = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
741 (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
742 dgettext(TEXT_DOMAIN, "cannot share '%s': %s"),
743 zfs_get_name(zhp), _sa_errorstr != NULL ?
744 _sa_errorstr(ret) : "");
749 for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) {
751 * Return success if there are no share options.
753 if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop,
754 shareopts, sizeof (shareopts), &sourcetype, sourcestr,
755 ZFS_MAXPROPLEN, B_FALSE) != 0 ||
756 strcmp(shareopts, "off") == 0)
760 * If the 'zoned' property is set, then zfs_is_mountable()
761 * will have already bailed out if we are in the global zone.
762 * But local zones cannot be NFS servers, so we ignore it for
763 * local zones as well.
765 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
769 share = zfs_sa_find_share(hdl->libzfs_sharehdl, mountpoint);
772 * This may be a new file system that was just
773 * created so isn't in the internal cache
774 * (second time through). Rather than
775 * reloading the entire configuration, we can
776 * assume ZFS has done the checking and it is
777 * safe to add this to the internal
780 if (_sa_zfs_process_share(hdl->libzfs_sharehdl,
781 NULL, NULL, mountpoint,
782 proto_table[*curr_proto].p_name, sourcetype,
783 shareopts, sourcestr, zhp->zfs_name) != SA_OK) {
784 (void) zfs_error_fmt(hdl,
785 proto_table[*curr_proto].p_share_err,
786 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
790 hdl->libzfs_shareflags |= ZFSSHARE_MISS;
791 share = zfs_sa_find_share(hdl->libzfs_sharehdl,
796 err = zfs_sa_enable_share(share,
797 proto_table[*curr_proto].p_name);
799 (void) zfs_error_fmt(hdl,
800 proto_table[*curr_proto].p_share_err,
801 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
807 if (*curr_proto != PROTO_NFS) {
808 fprintf(stderr, "Unsupported share protocol: %d.\n",
813 if (strcmp(shareopts, "on") == 0)
814 error = fsshare(ZFS_EXPORTS_PATH, mountpoint, "");
816 error = fsshare(ZFS_EXPORTS_PATH, mountpoint, shareopts);
820 (void) zfs_error_fmt(hdl,
821 proto_table[*curr_proto].p_share_err,
822 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
833 zfs_share_nfs(zfs_handle_t *zhp)
835 return (zfs_share_proto(zhp, nfs_only));
839 zfs_share_smb(zfs_handle_t *zhp)
841 return (zfs_share_proto(zhp, smb_only));
845 zfs_shareall(zfs_handle_t *zhp)
847 return (zfs_share_proto(zhp, share_all_proto));
851 * Unshare a filesystem by mountpoint.
854 unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint,
855 zfs_share_proto_t proto)
862 * Mountpoint could get trashed if libshare calls getmntany
863 * which it does during API initialization, so strdup the
866 mntpt = zfs_strdup(hdl, mountpoint);
868 /* make sure libshare initialized */
869 if ((err = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
870 free(mntpt); /* don't need the copy anymore */
871 return (zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
872 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
873 name, _sa_errorstr(err)));
876 share = zfs_sa_find_share(hdl->libzfs_sharehdl, mntpt);
877 free(mntpt); /* don't need the copy anymore */
880 err = zfs_sa_disable_share(share, proto_table[proto].p_name);
882 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
883 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
884 name, _sa_errorstr(err)));
887 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
888 dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"),
892 char buf[MAXPATHLEN];
896 if (proto != PROTO_NFS) {
897 fprintf(stderr, "No SMB support in FreeBSD yet.\n");
901 err = fsunshare(ZFS_EXPORTS_PATH, mountpoint);
903 zfs_error_aux(hdl, "%s", strerror(err));
904 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
905 dgettext(TEXT_DOMAIN,
906 "cannot unshare '%s'"), name));
913 * Unshare the given filesystem.
916 zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint,
917 zfs_share_proto_t *proto)
919 libzfs_handle_t *hdl = zhp->zfs_hdl;
923 /* check to see if need to unmount the filesystem */
924 rewind(zhp->zfs_hdl->libzfs_mnttab);
925 if (mountpoint != NULL)
926 mountpoint = mntpt = zfs_strdup(hdl, mountpoint);
928 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
929 libzfs_mnttab_find(hdl, zfs_get_name(zhp), &entry) == 0)) {
930 zfs_share_proto_t *curr_proto;
932 if (mountpoint == NULL)
933 mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp);
935 for (curr_proto = proto; *curr_proto != PROTO_END;
938 if (is_shared(hdl, mntpt, *curr_proto) &&
939 unshare_one(hdl, zhp->zfs_name,
940 mntpt, *curr_proto) != 0) {
954 zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
956 return (zfs_unshare_proto(zhp, mountpoint, nfs_only));
960 zfs_unshare_smb(zfs_handle_t *zhp, const char *mountpoint)
962 return (zfs_unshare_proto(zhp, mountpoint, smb_only));
966 * Same as zfs_unmountall(), but for NFS and SMB unshares.
969 zfs_unshareall_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
971 prop_changelist_t *clp;
974 clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0, 0);
978 ret = changelist_unshare(clp, proto);
979 changelist_free(clp);
985 zfs_unshareall_nfs(zfs_handle_t *zhp)
987 return (zfs_unshareall_proto(zhp, nfs_only));
991 zfs_unshareall_smb(zfs_handle_t *zhp)
993 return (zfs_unshareall_proto(zhp, smb_only));
997 zfs_unshareall(zfs_handle_t *zhp)
999 return (zfs_unshareall_proto(zhp, share_all_proto));
1003 zfs_unshareall_bypath(zfs_handle_t *zhp, const char *mountpoint)
1005 return (zfs_unshare_proto(zhp, mountpoint, share_all_proto));
1009 * Remove the mountpoint associated with the current dataset, if necessary.
1010 * We only remove the underlying directory if:
1012 * - The mountpoint is not 'none' or 'legacy'
1013 * - The mountpoint is non-empty
1014 * - The mountpoint is the default or inherited
1015 * - The 'zoned' property is set, or we're in a local zone
1017 * Any other directories we leave alone.
1020 remove_mountpoint(zfs_handle_t *zhp)
1022 char mountpoint[ZFS_MAXPROPLEN];
1023 zprop_source_t source;
1025 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
1029 if (source == ZPROP_SRC_DEFAULT ||
1030 source == ZPROP_SRC_INHERITED) {
1032 * Try to remove the directory, silently ignoring any errors.
1033 * The filesystem may have since been removed or moved around,
1034 * and this error isn't really useful to the administrator in
1037 (void) rmdir(mountpoint);
1042 libzfs_add_handle(get_all_cb_t *cbp, zfs_handle_t *zhp)
1044 if (cbp->cb_alloc == cbp->cb_used) {
1048 newsz = cbp->cb_alloc ? cbp->cb_alloc * 2 : 64;
1049 ptr = zfs_realloc(zhp->zfs_hdl,
1050 cbp->cb_handles, cbp->cb_alloc * sizeof (void *),
1051 newsz * sizeof (void *));
1052 cbp->cb_handles = ptr;
1053 cbp->cb_alloc = newsz;
1055 cbp->cb_handles[cbp->cb_used++] = zhp;
1059 mount_cb(zfs_handle_t *zhp, void *data)
1061 get_all_cb_t *cbp = data;
1063 if (!(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM)) {
1068 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_NOAUTO) {
1073 libzfs_add_handle(cbp, zhp);
1074 if (zfs_iter_filesystems(zhp, mount_cb, cbp) != 0) {
1082 libzfs_dataset_cmp(const void *a, const void *b)
1084 zfs_handle_t **za = (zfs_handle_t **)a;
1085 zfs_handle_t **zb = (zfs_handle_t **)b;
1086 char mounta[MAXPATHLEN];
1087 char mountb[MAXPATHLEN];
1088 boolean_t gota, gotb;
1090 if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
1091 verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
1092 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
1093 if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
1094 verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
1095 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
1098 return (strcmp(mounta, mountb));
1105 return (strcmp(zfs_get_name(a), zfs_get_name(b)));
1109 * Mount and share all datasets within the given pool. This assumes that no
1110 * datasets within the pool are currently mounted. Because users can create
1111 * complicated nested hierarchies of mountpoints, we first gather all the
1112 * datasets and mountpoints within the pool, and sort them by mountpoint. Once
1113 * we have the list of all filesystems, we iterate over them in order and mount
1114 * and/or share each one.
1116 #pragma weak zpool_mount_datasets = zpool_enable_datasets
1118 zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
1120 get_all_cb_t cb = { 0 };
1121 libzfs_handle_t *hdl = zhp->zpool_hdl;
1127 * Gather all non-snap datasets within the pool.
1129 if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_DATASET)) == NULL)
1132 libzfs_add_handle(&cb, zfsp);
1133 if (zfs_iter_filesystems(zfsp, mount_cb, &cb) != 0)
1136 * Sort the datasets by mountpoint.
1138 qsort(cb.cb_handles, cb.cb_used, sizeof (void *),
1139 libzfs_dataset_cmp);
1142 * And mount all the datasets, keeping track of which ones
1143 * succeeded or failed.
1145 if ((good = zfs_alloc(zhp->zpool_hdl,
1146 cb.cb_used * sizeof (int))) == NULL)
1150 for (i = 0; i < cb.cb_used; i++) {
1151 if (zfs_mount(cb.cb_handles[i], mntopts, flags) != 0)
1158 * Then share all the ones that need to be shared. This needs
1159 * to be a separate pass in order to avoid excessive reloading
1160 * of the configuration. Good should never be NULL since
1161 * zfs_alloc is supposed to exit if memory isn't available.
1163 for (i = 0; i < cb.cb_used; i++) {
1164 if (good[i] && zfs_share(cb.cb_handles[i]) != 0)
1171 for (i = 0; i < cb.cb_used; i++)
1172 zfs_close(cb.cb_handles[i]);
1173 free(cb.cb_handles);
1179 mountpoint_compare(const void *a, const void *b)
1181 const char *mounta = *((char **)a);
1182 const char *mountb = *((char **)b);
1184 return (strcmp(mountb, mounta));
1187 /* alias for 2002/240 */
1188 #pragma weak zpool_unmount_datasets = zpool_disable_datasets
1190 * Unshare and unmount all datasets within the given pool. We don't want to
1191 * rely on traversing the DSL to discover the filesystems within the pool,
1192 * because this may be expensive (if not all of them are mounted), and can fail
1193 * arbitrarily (on I/O error, for example). Instead, we walk /etc/mnttab and
1194 * gather all the filesystems that are currently mounted.
1197 zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
1200 struct mnttab entry;
1202 char **mountpoints = NULL;
1203 zfs_handle_t **datasets = NULL;
1204 libzfs_handle_t *hdl = zhp->zpool_hdl;
1207 int flags = (force ? MS_FORCE : 0);
1209 namelen = strlen(zhp->zpool_name);
1211 rewind(hdl->libzfs_mnttab);
1213 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
1215 * Ignore non-ZFS entries.
1217 if (entry.mnt_fstype == NULL ||
1218 strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
1222 * Ignore filesystems not within this pool.
1224 if (entry.mnt_mountp == NULL ||
1225 strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 ||
1226 (entry.mnt_special[namelen] != '/' &&
1227 entry.mnt_special[namelen] != '\0'))
1231 * At this point we've found a filesystem within our pool. Add
1232 * it to our growing list.
1234 if (used == alloc) {
1236 if ((mountpoints = zfs_alloc(hdl,
1237 8 * sizeof (void *))) == NULL)
1240 if ((datasets = zfs_alloc(hdl,
1241 8 * sizeof (void *))) == NULL)
1248 if ((ptr = zfs_realloc(hdl, mountpoints,
1249 alloc * sizeof (void *),
1250 alloc * 2 * sizeof (void *))) == NULL)
1254 if ((ptr = zfs_realloc(hdl, datasets,
1255 alloc * sizeof (void *),
1256 alloc * 2 * sizeof (void *))) == NULL)
1264 if ((mountpoints[used] = zfs_strdup(hdl,
1265 entry.mnt_mountp)) == NULL)
1269 * This is allowed to fail, in case there is some I/O error. It
1270 * is only used to determine if we need to remove the underlying
1271 * mountpoint, so failure is not fatal.
1273 datasets[used] = make_dataset_handle(hdl, entry.mnt_special);
1279 * At this point, we have the entire list of filesystems, so sort it by
1282 qsort(mountpoints, used, sizeof (char *), mountpoint_compare);
1285 * Walk through and first unshare everything.
1287 for (i = 0; i < used; i++) {
1288 zfs_share_proto_t *curr_proto;
1289 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
1291 if (is_shared(hdl, mountpoints[i], *curr_proto) &&
1292 unshare_one(hdl, mountpoints[i],
1293 mountpoints[i], *curr_proto) != 0)
1299 * Now unmount everything, removing the underlying directories as
1302 for (i = 0; i < used; i++) {
1303 if (unmount_one(hdl, mountpoints[i], flags) != 0)
1307 for (i = 0; i < used; i++) {
1309 remove_mountpoint(datasets[i]);
1314 for (i = 0; i < used; i++) {
1316 zfs_close(datasets[i]);
1317 free(mountpoints[i]);