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 */
85 static int zfs_share_proto(zfs_handle_t *, zfs_share_proto_t *);
86 zfs_share_type_t zfs_is_shared_proto(zfs_handle_t *, char **,
90 * The share protocols table must be in the same order as the zfs_share_prot_t
91 * enum in libzfs_impl.h
100 proto_table_t proto_table[PROTO_END] = {
101 {ZFS_PROP_SHARENFS, "nfs", EZFS_SHARENFSFAILED, EZFS_UNSHARENFSFAILED},
102 {ZFS_PROP_SHARESMB, "smb", EZFS_SHARESMBFAILED, EZFS_UNSHARESMBFAILED},
105 zfs_share_proto_t nfs_only[] = {
110 zfs_share_proto_t smb_only[] = {
114 zfs_share_proto_t share_all_proto[] = {
121 * Search the sharetab for the given mountpoint and protocol, returning
122 * a zfs_share_type_t value.
124 static zfs_share_type_t
125 is_shared(libzfs_handle_t *hdl, const char *mountpoint, zfs_share_proto_t proto)
127 char buf[MAXPATHLEN], *tab;
130 if (hdl->libzfs_sharetab == NULL)
131 return (SHARED_NOT_SHARED);
133 (void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET);
135 while (fgets(buf, sizeof (buf), hdl->libzfs_sharetab) != NULL) {
137 /* the mountpoint is the first entry on each line */
138 if ((tab = strchr(buf, '\t')) == NULL)
142 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) {
168 return (SHARED_NOT_SHARED);
172 * Returns true if the specified directory is empty. If we can't open the
173 * directory at all, return true so that the mount can fail with a more
174 * informative error message.
177 dir_is_empty(const char *dirname)
182 if ((dirp = opendir(dirname)) == NULL)
185 while ((dp = readdir64(dirp)) != NULL) {
187 if (strcmp(dp->d_name, ".") == 0 ||
188 strcmp(dp->d_name, "..") == 0)
191 (void) closedir(dirp);
195 (void) closedir(dirp);
200 * Checks to see if the mount is active. If the filesystem is mounted, we fill
201 * in 'where' with the current mountpoint, and return 1. Otherwise, we return
205 is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where)
209 if (libzfs_mnttab_find(zfs_hdl, special, &entry) != 0)
213 *where = zfs_strdup(zfs_hdl, entry.mnt_mountp);
219 zfs_is_mounted(zfs_handle_t *zhp, char **where)
221 return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where));
225 * Returns true if the given dataset is mountable, false otherwise. Returns the
226 * mountpoint in 'buf'.
229 zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen,
230 zprop_source_t *source)
232 char sourceloc[ZFS_MAXNAMELEN];
233 zprop_source_t sourcetype;
235 if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type))
238 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen,
239 &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0);
241 if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 ||
242 strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0)
245 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF)
248 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
249 getzoneid() == GLOBAL_ZONEID)
253 *source = sourcetype;
259 * Mount the given filesystem.
262 zfs_mount(zfs_handle_t *zhp, const char *options, int flags)
265 char mountpoint[ZFS_MAXPROPLEN];
266 char mntopts[MNT_LINE_MAX];
267 libzfs_handle_t *hdl = zhp->zfs_hdl;
272 (void) strlcpy(mntopts, options, sizeof (mntopts));
275 * If the pool is imported read-only then all mounts must be read-only
277 if (zpool_get_prop_int(zhp->zpool_hdl, ZPOOL_PROP_READONLY, NULL))
280 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
283 /* Create the directory if it doesn't already exist */
284 if (lstat(mountpoint, &buf) != 0) {
285 if (mkdirp(mountpoint, 0755) != 0) {
286 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
287 "failed to create mountpoint"));
288 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
289 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
295 * Determine if the mountpoint is empty. If so, refuse to perform the
296 * mount. We don't perform this check if MS_OVERLAY is specified, which
297 * would defeat the point. We also avoid this check if 'remount' is
300 if ((flags & MS_OVERLAY) == 0 &&
301 strstr(mntopts, MNTOPT_REMOUNT) == NULL &&
302 !dir_is_empty(mountpoint)) {
303 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
304 "directory is not empty"));
305 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
306 dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint));
309 /* perform the mount */
310 if (mount(zfs_get_name(zhp), mountpoint, MS_OPTIONSTR | flags,
311 MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) {
313 * Generic errors are nasty, but there are just way too many
314 * from mount(), and they're well-understood. We pick a few
315 * common ones to improve upon.
317 if (errno == EBUSY) {
318 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
319 "mountpoint or dataset is busy"));
320 } else if (errno == EPERM) {
321 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
322 "Insufficient privileges"));
323 } else if (errno == ENOTSUP) {
327 VERIFY(zfs_spa_version(zhp, &spa_version) == 0);
328 (void) snprintf(buf, sizeof (buf),
329 dgettext(TEXT_DOMAIN, "Can't mount a version %lld "
330 "file system on a version %d pool. Pool must be"
331 " upgraded to mount this file system."),
332 (u_longlong_t)zfs_prop_get_int(zhp,
333 ZFS_PROP_VERSION), spa_version);
334 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, buf));
336 zfs_error_aux(hdl, strerror(errno));
338 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
339 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
343 /* add the mounted entry into our cache */
344 libzfs_mnttab_add(hdl, zfs_get_name(zhp), mountpoint,
350 * Unmount a single filesystem.
353 unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags)
355 if (umount2(mountpoint, flags) != 0) {
356 zfs_error_aux(hdl, strerror(errno));
357 return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED,
358 dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
366 * Unmount the given filesystem.
369 zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
371 libzfs_handle_t *hdl = zhp->zfs_hdl;
375 /* check to see if we need to unmount the filesystem */
376 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
377 libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)) {
379 * mountpoint may have come from a call to
380 * getmnt/getmntany if it isn't NULL. If it is NULL,
381 * we know it comes from libzfs_mnttab_find which can
382 * then get freed later. We strdup it to play it safe.
384 if (mountpoint == NULL)
385 mntpt = zfs_strdup(hdl, entry.mnt_mountp);
387 mntpt = zfs_strdup(hdl, mountpoint);
390 * Unshare and unmount the filesystem
392 if (zfs_unshare_proto(zhp, mntpt, share_all_proto) != 0)
395 if (unmount_one(hdl, mntpt, flags) != 0) {
397 (void) zfs_shareall(zhp);
400 libzfs_mnttab_remove(hdl, zhp->zfs_name);
408 * Unmount this filesystem and any children inheriting the mountpoint property.
409 * To do this, just act like we're changing the mountpoint property, but don't
410 * remount the filesystems afterwards.
413 zfs_unmountall(zfs_handle_t *zhp, int flags)
415 prop_changelist_t *clp;
418 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, 0, flags);
422 ret = changelist_prefix(clp);
423 changelist_free(clp);
429 zfs_is_shared(zfs_handle_t *zhp)
431 zfs_share_type_t rc = 0;
432 zfs_share_proto_t *curr_proto;
434 if (ZFS_IS_VOLUME(zhp))
437 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
439 rc |= zfs_is_shared_proto(zhp, NULL, *curr_proto);
441 return (rc ? B_TRUE : B_FALSE);
445 zfs_share(zfs_handle_t *zhp)
447 assert(!ZFS_IS_VOLUME(zhp));
448 return (zfs_share_proto(zhp, share_all_proto));
452 zfs_unshare(zfs_handle_t *zhp)
454 assert(!ZFS_IS_VOLUME(zhp));
455 return (zfs_unshareall(zhp));
459 * Check to see if the filesystem is currently shared.
462 zfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto)
467 if (!zfs_is_mounted(zhp, &mountpoint))
468 return (SHARED_NOT_SHARED);
470 if (rc = is_shared(zhp->zfs_hdl, mountpoint, proto)) {
478 return (SHARED_NOT_SHARED);
483 zfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
485 return (zfs_is_shared_proto(zhp, where,
486 PROTO_NFS) != SHARED_NOT_SHARED);
490 zfs_is_shared_smb(zfs_handle_t *zhp, char **where)
492 return (zfs_is_shared_proto(zhp, where,
493 PROTO_SMB) != SHARED_NOT_SHARED);
497 * Make sure things will work if libshare isn't installed by using
498 * wrapper functions that check to see that the pointers to functions
499 * initialized in _zfs_init_libshare() are actually present.
502 static sa_handle_t (*_sa_init)(int);
503 static void (*_sa_fini)(sa_handle_t);
504 static sa_share_t (*_sa_find_share)(sa_handle_t, char *);
505 static int (*_sa_enable_share)(sa_share_t, char *);
506 static int (*_sa_disable_share)(sa_share_t, char *);
507 static char *(*_sa_errorstr)(int);
508 static int (*_sa_parse_legacy_options)(sa_group_t, char *, char *);
509 static boolean_t (*_sa_needs_refresh)(sa_handle_t *);
510 static libzfs_handle_t *(*_sa_get_zfs_handle)(sa_handle_t);
511 static int (*_sa_zfs_process_share)(sa_handle_t, sa_group_t, sa_share_t,
512 char *, char *, zprop_source_t, char *, char *, char *);
513 static void (*_sa_update_sharetab_ts)(sa_handle_t);
516 * _zfs_init_libshare()
518 * Find the libshare.so.1 entry points that we use here and save the
519 * values to be used later. This is triggered by the runtime loader.
520 * Make sure the correct ISA version is loaded.
524 _zfs_init_libshare(void) __attribute__((constructor));
526 #pragma init(_zfs_init_libshare)
529 _zfs_init_libshare(void)
532 char path[MAXPATHLEN];
536 if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1)
541 (void) snprintf(path, MAXPATHLEN,
542 "/usr/lib/%s/libshare.so.1", isa);
544 if ((libshare = dlopen(path, RTLD_LAZY | RTLD_GLOBAL)) != NULL) {
545 _sa_init = (sa_handle_t (*)(int))dlsym(libshare, "sa_init");
546 _sa_fini = (void (*)(sa_handle_t))dlsym(libshare, "sa_fini");
547 _sa_find_share = (sa_share_t (*)(sa_handle_t, char *))
548 dlsym(libshare, "sa_find_share");
549 _sa_enable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
551 _sa_disable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
553 _sa_errorstr = (char *(*)(int))dlsym(libshare, "sa_errorstr");
554 _sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *))
555 dlsym(libshare, "sa_parse_legacy_options");
556 _sa_needs_refresh = (boolean_t (*)(sa_handle_t *))
557 dlsym(libshare, "sa_needs_refresh");
558 _sa_get_zfs_handle = (libzfs_handle_t *(*)(sa_handle_t))
559 dlsym(libshare, "sa_get_zfs_handle");
560 _sa_zfs_process_share = (int (*)(sa_handle_t, sa_group_t,
561 sa_share_t, char *, char *, zprop_source_t, char *,
562 char *, char *))dlsym(libshare, "sa_zfs_process_share");
563 _sa_update_sharetab_ts = (void (*)(sa_handle_t))
564 dlsym(libshare, "sa_update_sharetab_ts");
565 if (_sa_init == NULL || _sa_fini == NULL ||
566 _sa_find_share == NULL || _sa_enable_share == NULL ||
567 _sa_disable_share == NULL || _sa_errorstr == NULL ||
568 _sa_parse_legacy_options == NULL ||
569 _sa_needs_refresh == NULL || _sa_get_zfs_handle == NULL ||
570 _sa_zfs_process_share == NULL ||
571 _sa_update_sharetab_ts == NULL) {
574 _sa_disable_share = NULL;
575 _sa_enable_share = NULL;
577 _sa_parse_legacy_options = NULL;
578 (void) dlclose(libshare);
579 _sa_needs_refresh = NULL;
580 _sa_get_zfs_handle = NULL;
581 _sa_zfs_process_share = NULL;
582 _sa_update_sharetab_ts = NULL;
588 * zfs_init_libshare(zhandle, service)
590 * Initialize the libshare API if it hasn't already been initialized.
591 * In all cases it returns 0 if it succeeded and an error if not. The
592 * service value is which part(s) of the API to initialize and is a
593 * direct map to the libshare sa_init(service) interface.
596 zfs_init_libshare(libzfs_handle_t *zhandle, int service)
600 if (_sa_init == NULL)
603 if (ret == SA_OK && zhandle->libzfs_shareflags & ZFSSHARE_MISS) {
605 * We had a cache miss. Most likely it is a new ZFS
606 * dataset that was just created. We want to make sure
607 * so check timestamps to see if a different process
608 * has updated any of the configuration. If there was
609 * some non-ZFS change, we need to re-initialize the
612 zhandle->libzfs_shareflags &= ~ZFSSHARE_MISS;
613 if (_sa_needs_refresh != NULL &&
614 _sa_needs_refresh(zhandle->libzfs_sharehdl)) {
615 zfs_uninit_libshare(zhandle);
616 zhandle->libzfs_sharehdl = _sa_init(service);
620 if (ret == SA_OK && zhandle && zhandle->libzfs_sharehdl == NULL)
621 zhandle->libzfs_sharehdl = _sa_init(service);
623 if (ret == SA_OK && zhandle->libzfs_sharehdl == NULL)
630 * zfs_uninit_libshare(zhandle)
632 * Uninitialize the libshare API if it hasn't already been
633 * uninitialized. It is OK to call multiple times.
636 zfs_uninit_libshare(libzfs_handle_t *zhandle)
638 if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) {
639 if (_sa_fini != NULL)
640 _sa_fini(zhandle->libzfs_sharehdl);
641 zhandle->libzfs_sharehdl = NULL;
646 * zfs_parse_options(options, proto)
648 * Call the legacy parse interface to get the protocol specific
649 * options using the NULL arg to indicate that this is a "parse" only.
652 zfs_parse_options(char *options, zfs_share_proto_t proto)
654 if (_sa_parse_legacy_options != NULL) {
655 return (_sa_parse_legacy_options(NULL, options,
656 proto_table[proto].p_name));
658 return (SA_CONFIG_ERR);
662 * zfs_sa_find_share(handle, path)
664 * wrapper around sa_find_share to find a share path in the
668 zfs_sa_find_share(sa_handle_t handle, char *path)
670 if (_sa_find_share != NULL)
671 return (_sa_find_share(handle, path));
676 * zfs_sa_enable_share(share, proto)
678 * Wrapper for sa_enable_share which enables a share for a specified
682 zfs_sa_enable_share(sa_share_t share, char *proto)
684 if (_sa_enable_share != NULL)
685 return (_sa_enable_share(share, proto));
686 return (SA_CONFIG_ERR);
690 * zfs_sa_disable_share(share, proto)
692 * Wrapper for sa_enable_share which disables a share for a specified
696 zfs_sa_disable_share(sa_share_t share, char *proto)
698 if (_sa_disable_share != NULL)
699 return (_sa_disable_share(share, proto));
700 return (SA_CONFIG_ERR);
704 * Share the given filesystem according to the options in the specified
705 * protocol specific properties (sharenfs, sharesmb). We rely
706 * on "libshare" to the dirty work for us.
709 zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
711 char mountpoint[ZFS_MAXPROPLEN];
712 char shareopts[ZFS_MAXPROPLEN];
713 char sourcestr[ZFS_MAXPROPLEN];
714 libzfs_handle_t *hdl = zhp->zfs_hdl;
716 zfs_share_proto_t *curr_proto;
717 zprop_source_t sourcetype;
720 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
723 if ((ret = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
724 (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
725 dgettext(TEXT_DOMAIN, "cannot share '%s': %s"),
726 zfs_get_name(zhp), _sa_errorstr != NULL ?
727 _sa_errorstr(ret) : "");
731 for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) {
733 * Return success if there are no share options.
735 if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop,
736 shareopts, sizeof (shareopts), &sourcetype, sourcestr,
737 ZFS_MAXPROPLEN, B_FALSE) != 0 ||
738 strcmp(shareopts, "off") == 0)
742 * If the 'zoned' property is set, then zfs_is_mountable()
743 * will have already bailed out if we are in the global zone.
744 * But local zones cannot be NFS servers, so we ignore it for
745 * local zones as well.
747 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
750 share = zfs_sa_find_share(hdl->libzfs_sharehdl, mountpoint);
753 * This may be a new file system that was just
754 * created so isn't in the internal cache
755 * (second time through). Rather than
756 * reloading the entire configuration, we can
757 * assume ZFS has done the checking and it is
758 * safe to add this to the internal
761 if (_sa_zfs_process_share(hdl->libzfs_sharehdl,
762 NULL, NULL, mountpoint,
763 proto_table[*curr_proto].p_name, sourcetype,
764 shareopts, sourcestr, zhp->zfs_name) != SA_OK) {
765 (void) zfs_error_fmt(hdl,
766 proto_table[*curr_proto].p_share_err,
767 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
771 hdl->libzfs_shareflags |= ZFSSHARE_MISS;
772 share = zfs_sa_find_share(hdl->libzfs_sharehdl,
777 err = zfs_sa_enable_share(share,
778 proto_table[*curr_proto].p_name);
780 (void) zfs_error_fmt(hdl,
781 proto_table[*curr_proto].p_share_err,
782 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
787 (void) zfs_error_fmt(hdl,
788 proto_table[*curr_proto].p_share_err,
789 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
800 zfs_share_nfs(zfs_handle_t *zhp)
802 return (zfs_share_proto(zhp, nfs_only));
806 zfs_share_smb(zfs_handle_t *zhp)
808 return (zfs_share_proto(zhp, smb_only));
812 zfs_shareall(zfs_handle_t *zhp)
814 return (zfs_share_proto(zhp, share_all_proto));
818 * Unshare a filesystem by mountpoint.
821 unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint,
822 zfs_share_proto_t proto)
828 * Mountpoint could get trashed if libshare calls getmntany
829 * which it does during API initialization, so strdup the
832 mntpt = zfs_strdup(hdl, mountpoint);
834 /* make sure libshare initialized */
835 if ((err = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
836 free(mntpt); /* don't need the copy anymore */
837 return (zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
838 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
839 name, _sa_errorstr(err)));
842 share = zfs_sa_find_share(hdl->libzfs_sharehdl, mntpt);
843 free(mntpt); /* don't need the copy anymore */
846 err = zfs_sa_disable_share(share, proto_table[proto].p_name);
848 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
849 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
850 name, _sa_errorstr(err)));
853 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
854 dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"),
861 * Unshare the given filesystem.
864 zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint,
865 zfs_share_proto_t *proto)
867 libzfs_handle_t *hdl = zhp->zfs_hdl;
871 /* check to see if need to unmount the filesystem */
872 rewind(zhp->zfs_hdl->libzfs_mnttab);
873 if (mountpoint != NULL)
874 mountpoint = mntpt = zfs_strdup(hdl, mountpoint);
876 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
877 libzfs_mnttab_find(hdl, zfs_get_name(zhp), &entry) == 0)) {
878 zfs_share_proto_t *curr_proto;
880 if (mountpoint == NULL)
881 mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp);
883 for (curr_proto = proto; *curr_proto != PROTO_END;
886 if (is_shared(hdl, mntpt, *curr_proto) &&
887 unshare_one(hdl, zhp->zfs_name,
888 mntpt, *curr_proto) != 0) {
902 zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
904 return (zfs_unshare_proto(zhp, mountpoint, nfs_only));
908 zfs_unshare_smb(zfs_handle_t *zhp, const char *mountpoint)
910 return (zfs_unshare_proto(zhp, mountpoint, smb_only));
914 * Same as zfs_unmountall(), but for NFS and SMB unshares.
917 zfs_unshareall_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
919 prop_changelist_t *clp;
922 clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0, 0);
926 ret = changelist_unshare(clp, proto);
927 changelist_free(clp);
933 zfs_unshareall_nfs(zfs_handle_t *zhp)
935 return (zfs_unshareall_proto(zhp, nfs_only));
939 zfs_unshareall_smb(zfs_handle_t *zhp)
941 return (zfs_unshareall_proto(zhp, smb_only));
945 zfs_unshareall(zfs_handle_t *zhp)
947 return (zfs_unshareall_proto(zhp, share_all_proto));
951 zfs_unshareall_bypath(zfs_handle_t *zhp, const char *mountpoint)
953 return (zfs_unshare_proto(zhp, mountpoint, share_all_proto));
957 * Remove the mountpoint associated with the current dataset, if necessary.
958 * We only remove the underlying directory if:
960 * - The mountpoint is not 'none' or 'legacy'
961 * - The mountpoint is non-empty
962 * - The mountpoint is the default or inherited
963 * - The 'zoned' property is set, or we're in a local zone
965 * Any other directories we leave alone.
968 remove_mountpoint(zfs_handle_t *zhp)
970 char mountpoint[ZFS_MAXPROPLEN];
971 zprop_source_t source;
973 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
977 if (source == ZPROP_SRC_DEFAULT ||
978 source == ZPROP_SRC_INHERITED) {
980 * Try to remove the directory, silently ignoring any errors.
981 * The filesystem may have since been removed or moved around,
982 * and this error isn't really useful to the administrator in
985 (void) rmdir(mountpoint);
990 libzfs_add_handle(get_all_cb_t *cbp, zfs_handle_t *zhp)
992 if (cbp->cb_alloc == cbp->cb_used) {
996 newsz = cbp->cb_alloc ? cbp->cb_alloc * 2 : 64;
997 ptr = zfs_realloc(zhp->zfs_hdl,
998 cbp->cb_handles, cbp->cb_alloc * sizeof (void *),
999 newsz * sizeof (void *));
1000 cbp->cb_handles = ptr;
1001 cbp->cb_alloc = newsz;
1003 cbp->cb_handles[cbp->cb_used++] = zhp;
1007 mount_cb(zfs_handle_t *zhp, void *data)
1009 get_all_cb_t *cbp = data;
1011 if (!(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM)) {
1016 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_NOAUTO) {
1021 libzfs_add_handle(cbp, zhp);
1022 if (zfs_iter_filesystems(zhp, mount_cb, cbp) != 0) {
1030 libzfs_dataset_cmp(const void *a, const void *b)
1032 zfs_handle_t **za = (zfs_handle_t **)a;
1033 zfs_handle_t **zb = (zfs_handle_t **)b;
1034 char mounta[MAXPATHLEN];
1035 char mountb[MAXPATHLEN];
1036 boolean_t gota, gotb;
1038 if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
1039 verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
1040 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
1041 if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
1042 verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
1043 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
1046 return (strcmp(mounta, mountb));
1053 return (strcmp(zfs_get_name(a), zfs_get_name(b)));
1057 * Mount and share all datasets within the given pool. This assumes that no
1058 * datasets within the pool are currently mounted. Because users can create
1059 * complicated nested hierarchies of mountpoints, we first gather all the
1060 * datasets and mountpoints within the pool, and sort them by mountpoint. Once
1061 * we have the list of all filesystems, we iterate over them in order and mount
1062 * and/or share each one.
1064 #pragma weak zpool_mount_datasets = zpool_enable_datasets
1066 zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
1068 get_all_cb_t cb = { 0 };
1069 libzfs_handle_t *hdl = zhp->zpool_hdl;
1075 * Gather all non-snap datasets within the pool.
1077 if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_DATASET)) == NULL)
1080 libzfs_add_handle(&cb, zfsp);
1081 if (zfs_iter_filesystems(zfsp, mount_cb, &cb) != 0)
1084 * Sort the datasets by mountpoint.
1086 qsort(cb.cb_handles, cb.cb_used, sizeof (void *),
1087 libzfs_dataset_cmp);
1090 * And mount all the datasets, keeping track of which ones
1091 * succeeded or failed.
1093 if ((good = zfs_alloc(zhp->zpool_hdl,
1094 cb.cb_used * sizeof (int))) == NULL)
1098 for (i = 0; i < cb.cb_used; i++) {
1099 if (zfs_mount(cb.cb_handles[i], mntopts, flags) != 0)
1106 * Then share all the ones that need to be shared. This needs
1107 * to be a separate pass in order to avoid excessive reloading
1108 * of the configuration. Good should never be NULL since
1109 * zfs_alloc is supposed to exit if memory isn't available.
1111 for (i = 0; i < cb.cb_used; i++) {
1112 if (good[i] && zfs_share(cb.cb_handles[i]) != 0)
1119 for (i = 0; i < cb.cb_used; i++)
1120 zfs_close(cb.cb_handles[i]);
1121 free(cb.cb_handles);
1127 mountpoint_compare(const void *a, const void *b)
1129 const char *mounta = *((char **)a);
1130 const char *mountb = *((char **)b);
1132 return (strcmp(mountb, mounta));
1135 /* alias for 2002/240 */
1136 #pragma weak zpool_unmount_datasets = zpool_disable_datasets
1138 * Unshare and unmount all datasets within the given pool. We don't want to
1139 * rely on traversing the DSL to discover the filesystems within the pool,
1140 * because this may be expensive (if not all of them are mounted), and can fail
1141 * arbitrarily (on I/O error, for example). Instead, we walk /etc/mnttab and
1142 * gather all the filesystems that are currently mounted.
1145 zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
1148 struct mnttab entry;
1150 char **mountpoints = NULL;
1151 zfs_handle_t **datasets = NULL;
1152 libzfs_handle_t *hdl = zhp->zpool_hdl;
1155 int flags = (force ? MS_FORCE : 0);
1157 namelen = strlen(zhp->zpool_name);
1159 rewind(hdl->libzfs_mnttab);
1161 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
1163 * Ignore non-ZFS entries.
1165 if (entry.mnt_fstype == NULL ||
1166 strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
1170 * Ignore filesystems not within this pool.
1172 if (entry.mnt_mountp == NULL ||
1173 strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 ||
1174 (entry.mnt_special[namelen] != '/' &&
1175 entry.mnt_special[namelen] != '\0'))
1179 * At this point we've found a filesystem within our pool. Add
1180 * it to our growing list.
1182 if (used == alloc) {
1184 if ((mountpoints = zfs_alloc(hdl,
1185 8 * sizeof (void *))) == NULL)
1188 if ((datasets = zfs_alloc(hdl,
1189 8 * sizeof (void *))) == NULL)
1196 if ((ptr = zfs_realloc(hdl, mountpoints,
1197 alloc * sizeof (void *),
1198 alloc * 2 * sizeof (void *))) == NULL)
1202 if ((ptr = zfs_realloc(hdl, datasets,
1203 alloc * sizeof (void *),
1204 alloc * 2 * sizeof (void *))) == NULL)
1212 if ((mountpoints[used] = zfs_strdup(hdl,
1213 entry.mnt_mountp)) == NULL)
1217 * This is allowed to fail, in case there is some I/O error. It
1218 * is only used to determine if we need to remove the underlying
1219 * mountpoint, so failure is not fatal.
1221 datasets[used] = make_dataset_handle(hdl, entry.mnt_special);
1227 * At this point, we have the entire list of filesystems, so sort it by
1230 qsort(mountpoints, used, sizeof (char *), mountpoint_compare);
1233 * Walk through and first unshare everything.
1235 for (i = 0; i < used; i++) {
1236 zfs_share_proto_t *curr_proto;
1237 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
1239 if (is_shared(hdl, mountpoints[i], *curr_proto) &&
1240 unshare_one(hdl, mountpoints[i],
1241 mountpoints[i], *curr_proto) != 0)
1247 * Now unmount everything, removing the underlying directories as
1250 for (i = 0; i < used; i++) {
1251 if (unmount_one(hdl, mountpoints[i], flags) != 0)
1255 for (i = 0; i < used; i++) {
1257 remove_mountpoint(datasets[i]);
1262 for (i = 0; i < used; i++) {
1264 zfs_close(datasets[i]);
1265 free(mountpoints[i]);
1273 #else /* HAVE_ZPL */
1276 zfs_unshare_iscsi(zfs_handle_t *zhp)
1282 zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
1288 remove_mountpoint(zfs_handle_t *zhp) {
1293 is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where)
1299 zfs_is_mounted(zfs_handle_t *zhp, char **where)
1301 return is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where);
1305 zfs_is_shared(zfs_handle_t *zhp)
1311 zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
1317 zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
1321 #endif /* HAVE_ZPL */