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 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "@(#)libzfs_mount.c 1.25 08/03/04 SMI"
30 * Routines to manage ZFS mounts. We separate all the nasty routines that have
31 * to deal with the OS. The following functions are the main entry points --
32 * they are used by mount and unmount and when changing a filesystem's
40 * This file also contains the functions used to manage sharing filesystems via
49 * zfs_is_shared_iscsi()
55 * zfs_unshareall_nfs()
56 * zfs_unshareall_smb()
58 * zfs_unshareall_bypath()
61 * The following functions are available for pool consumers, and will
62 * mount/unmount and share/unshare all datasets within pool:
64 * zpool_enable_datasets()
65 * zpool_disable_datasets()
78 #include <sys/mntent.h>
79 #include <sys/mnttab.h>
80 #include <sys/mount.h>
85 #include "libzfs_impl.h"
88 #include <sys/systeminfo.h>
89 #define MAXISALEN 257 /* based on sysinfo(2) man page */
91 static int zfs_share_proto(zfs_handle_t *, zfs_share_proto_t *);
92 zfs_share_type_t zfs_is_shared_proto(zfs_handle_t *, char **,
95 static int (*iscsitgt_zfs_share)(const char *);
96 static int (*iscsitgt_zfs_unshare)(const char *);
97 static int (*iscsitgt_zfs_is_shared)(const char *);
98 static int (*iscsitgt_svc_online)();
101 * The share protocols table must be in the same order as the zfs_share_prot_t
102 * enum in libzfs_impl.h
111 proto_table_t proto_table[PROTO_END] = {
112 {ZFS_PROP_SHARENFS, "nfs", EZFS_SHARENFSFAILED, EZFS_UNSHARENFSFAILED},
113 {ZFS_PROP_SHARESMB, "smb", EZFS_SHARESMBFAILED, EZFS_UNSHARESMBFAILED},
116 zfs_share_proto_t nfs_only[] = {
121 zfs_share_proto_t smb_only[] = {
125 zfs_share_proto_t share_all_proto[] = {
131 #pragma init(zfs_iscsi_init)
137 if ((libiscsitgt = dlopen("/lib/libiscsitgt.so.1",
138 RTLD_LAZY | RTLD_GLOBAL)) == NULL ||
139 (iscsitgt_zfs_share = (int (*)(const char *))dlsym(libiscsitgt,
140 "iscsitgt_zfs_share")) == NULL ||
141 (iscsitgt_zfs_unshare = (int (*)(const char *))dlsym(libiscsitgt,
142 "iscsitgt_zfs_unshare")) == NULL ||
143 (iscsitgt_zfs_is_shared = (int (*)(const char *))dlsym(libiscsitgt,
144 "iscsitgt_zfs_is_shared")) == NULL ||
145 (iscsitgt_svc_online = (int (*)(const char *))dlsym(libiscsitgt,
146 "iscsitgt_svc_online")) == NULL) {
147 iscsitgt_zfs_share = NULL;
148 iscsitgt_zfs_unshare = NULL;
149 iscsitgt_zfs_is_shared = NULL;
150 iscsitgt_svc_online = NULL;
155 * Search the sharetab for the given mountpoint and protocol, returning
156 * a zfs_share_type_t value.
158 static zfs_share_type_t
159 is_shared(libzfs_handle_t *hdl, const char *mountpoint, zfs_share_proto_t proto)
161 char buf[MAXPATHLEN], *tab;
164 if (hdl->libzfs_sharetab == NULL)
165 return (SHARED_NOT_SHARED);
167 (void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET);
169 while (fgets(buf, sizeof (buf), hdl->libzfs_sharetab) != NULL) {
171 /* the mountpoint is the first entry on each line */
172 if ((tab = strchr(buf, '\t')) == NULL)
176 if (strcmp(buf, mountpoint) == 0) {
178 * the protocol field is the third field
179 * skip over second field
182 if ((tab = strchr(ptr, '\t')) == NULL)
185 if ((tab = strchr(ptr, '\t')) == NULL)
189 proto_table[proto].p_name) == 0) {
202 return (SHARED_NOT_SHARED);
206 * Returns true if the specified directory is empty. If we can't open the
207 * directory at all, return true so that the mount can fail with a more
208 * informative error message.
211 dir_is_empty(const char *dirname)
216 if ((dirp = opendir(dirname)) == NULL)
219 while ((dp = readdir64(dirp)) != NULL) {
221 if (strcmp(dp->d_name, ".") == 0 ||
222 strcmp(dp->d_name, "..") == 0)
225 (void) closedir(dirp);
229 (void) closedir(dirp);
234 * Checks to see if the mount is active. If the filesystem is mounted, we fill
235 * in 'where' with the current mountpoint, and return 1. Otherwise, we return
239 is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where)
241 struct mnttab search = { 0 }, entry;
244 * Search for the entry in /etc/mnttab. We don't bother getting the
245 * mountpoint, as we can just search for the special device. This will
246 * also let us find mounts when the mountpoint is 'legacy'.
248 search.mnt_special = (char *)special;
249 search.mnt_fstype = MNTTYPE_ZFS;
251 rewind(zfs_hdl->libzfs_mnttab);
252 if (getmntany(zfs_hdl->libzfs_mnttab, &entry, &search) != 0)
256 *where = zfs_strdup(zfs_hdl, entry.mnt_mountp);
262 zfs_is_mounted(zfs_handle_t *zhp, char **where)
264 return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where));
268 * Returns true if the given dataset is mountable, false otherwise. Returns the
269 * mountpoint in 'buf'.
272 zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen,
273 zprop_source_t *source)
275 char sourceloc[ZFS_MAXNAMELEN];
276 zprop_source_t sourcetype;
278 if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type))
281 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen,
282 &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0);
284 if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 ||
285 strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0)
288 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF)
291 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
292 getzoneid() == GLOBAL_ZONEID)
296 *source = sourcetype;
302 * Mount the given filesystem.
305 zfs_mount(zfs_handle_t *zhp, const char *options, int flags)
308 char mountpoint[ZFS_MAXPROPLEN];
309 char mntopts[MNT_LINE_MAX];
310 libzfs_handle_t *hdl = zhp->zfs_hdl;
315 (void) strlcpy(mntopts, options, sizeof (mntopts));
317 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
320 /* Create the directory if it doesn't already exist */
321 if (lstat(mountpoint, &buf) != 0) {
322 if (mkdirp(mountpoint, 0755) != 0) {
323 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
324 "failed to create mountpoint"));
325 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
326 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
332 * Determine if the mountpoint is empty. If so, refuse to perform the
333 * mount. We don't perform this check if MS_OVERLAY is specified, which
334 * would defeat the point. We also avoid this check if 'remount' is
337 if ((flags & MS_OVERLAY) == 0 &&
338 strstr(mntopts, MNTOPT_REMOUNT) == NULL &&
339 !dir_is_empty(mountpoint)) {
340 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
341 "directory is not empty"));
342 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
343 dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint));
346 /* perform the mount */
347 if (mount(zfs_get_name(zhp), mountpoint, MS_OPTIONSTR | flags,
348 MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) {
350 * Generic errors are nasty, but there are just way too many
351 * from mount(), and they're well-understood. We pick a few
352 * common ones to improve upon.
354 if (errno == EBUSY) {
355 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
356 "mountpoint or dataset is busy"));
357 } else if (errno == EPERM) {
358 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
359 "Insufficient privileges"));
361 zfs_error_aux(hdl, strerror(errno));
364 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
365 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
373 * Unmount a single filesystem.
376 unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags)
378 if (umount2(mountpoint, flags) != 0) {
379 zfs_error_aux(hdl, strerror(errno));
380 return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED,
381 dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
389 * Unmount the given filesystem.
392 zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
394 struct mnttab search = { 0 }, entry;
397 /* check to see if need to unmount the filesystem */
398 search.mnt_special = zhp->zfs_name;
399 search.mnt_fstype = MNTTYPE_ZFS;
400 rewind(zhp->zfs_hdl->libzfs_mnttab);
401 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
402 getmntany(zhp->zfs_hdl->libzfs_mnttab, &entry, &search) == 0)) {
405 * mountpoint may have come from a call to
406 * getmnt/getmntany if it isn't NULL. If it is NULL,
407 * we know it comes from getmntany which can then get
408 * overwritten later. We strdup it to play it safe.
410 if (mountpoint == NULL)
411 mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp);
413 mntpt = zfs_strdup(zhp->zfs_hdl, mountpoint);
416 * Unshare and unmount the filesystem
418 if (zfs_unshare_proto(zhp, mntpt, share_all_proto) != 0)
421 if (unmount_one(zhp->zfs_hdl, mntpt, flags) != 0) {
423 (void) zfs_shareall(zhp);
433 * Unmount this filesystem and any children inheriting the mountpoint property.
434 * To do this, just act like we're changing the mountpoint property, but don't
435 * remount the filesystems afterwards.
438 zfs_unmountall(zfs_handle_t *zhp, int flags)
440 prop_changelist_t *clp;
443 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, flags);
447 ret = changelist_prefix(clp);
448 changelist_free(clp);
454 zfs_is_shared(zfs_handle_t *zhp)
456 zfs_share_type_t rc = 0;
457 zfs_share_proto_t *curr_proto;
459 if (ZFS_IS_VOLUME(zhp))
460 return (zfs_is_shared_iscsi(zhp));
462 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
464 rc |= zfs_is_shared_proto(zhp, NULL, *curr_proto);
466 return (rc ? B_TRUE : B_FALSE);
470 zfs_share(zfs_handle_t *zhp)
472 if (ZFS_IS_VOLUME(zhp))
473 return (zfs_share_iscsi(zhp));
475 return (zfs_share_proto(zhp, share_all_proto));
479 zfs_unshare(zfs_handle_t *zhp)
481 if (ZFS_IS_VOLUME(zhp))
482 return (zfs_unshare_iscsi(zhp));
484 return (zfs_unshareall(zhp));
488 * Check to see if the filesystem is currently shared.
491 zfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto)
496 if (!zfs_is_mounted(zhp, &mountpoint))
497 return (SHARED_NOT_SHARED);
499 if (rc = is_shared(zhp->zfs_hdl, mountpoint, proto)) {
507 return (SHARED_NOT_SHARED);
512 zfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
514 return (zfs_is_shared_proto(zhp, where,
515 PROTO_NFS) != SHARED_NOT_SHARED);
519 zfs_is_shared_smb(zfs_handle_t *zhp, char **where)
521 return (zfs_is_shared_proto(zhp, where,
522 PROTO_SMB) != SHARED_NOT_SHARED);
526 * Make sure things will work if libshare isn't installed by using
527 * wrapper functions that check to see that the pointers to functions
528 * initialized in _zfs_init_libshare() are actually present.
531 static sa_handle_t (*_sa_init)(int);
532 static void (*_sa_fini)(sa_handle_t);
533 static sa_share_t (*_sa_find_share)(sa_handle_t, char *);
534 static int (*_sa_enable_share)(sa_share_t, char *);
535 static int (*_sa_disable_share)(sa_share_t, char *);
536 static char *(*_sa_errorstr)(int);
537 static int (*_sa_parse_legacy_options)(sa_group_t, char *, char *);
538 static boolean_t (*_sa_needs_refresh)(sa_handle_t *);
539 static libzfs_handle_t *(*_sa_get_zfs_handle)(sa_handle_t);
540 static int (*_sa_zfs_process_share)(sa_handle_t, sa_group_t, sa_share_t,
541 char *, char *, zprop_source_t, char *, char *, char *);
542 static void (*_sa_update_sharetab_ts)(sa_handle_t);
545 * _zfs_init_libshare()
547 * Find the libshare.so.1 entry points that we use here and save the
548 * values to be used later. This is triggered by the runtime loader.
549 * Make sure the correct ISA version is loaded.
552 #pragma init(_zfs_init_libshare)
554 _zfs_init_libshare(void)
557 char path[MAXPATHLEN];
561 if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1)
566 (void) snprintf(path, MAXPATHLEN,
567 "/usr/lib/%s/libshare.so.1", isa);
569 if ((libshare = dlopen(path, RTLD_LAZY | RTLD_GLOBAL)) != NULL) {
570 _sa_init = (sa_handle_t (*)(int))dlsym(libshare, "sa_init");
571 _sa_fini = (void (*)(sa_handle_t))dlsym(libshare, "sa_fini");
572 _sa_find_share = (sa_share_t (*)(sa_handle_t, char *))
573 dlsym(libshare, "sa_find_share");
574 _sa_enable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
576 _sa_disable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
578 _sa_errorstr = (char *(*)(int))dlsym(libshare, "sa_errorstr");
579 _sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *))
580 dlsym(libshare, "sa_parse_legacy_options");
581 _sa_needs_refresh = (boolean_t (*)(sa_handle_t *))
582 dlsym(libshare, "sa_needs_refresh");
583 _sa_get_zfs_handle = (libzfs_handle_t *(*)(sa_handle_t))
584 dlsym(libshare, "sa_get_zfs_handle");
585 _sa_zfs_process_share = (int (*)(sa_handle_t, sa_group_t,
586 sa_share_t, char *, char *, zprop_source_t, char *,
587 char *, char *))dlsym(libshare, "sa_zfs_process_share");
588 _sa_update_sharetab_ts = (void (*)(sa_handle_t))
589 dlsym(libshare, "sa_update_sharetab_ts");
590 if (_sa_init == NULL || _sa_fini == NULL ||
591 _sa_find_share == NULL || _sa_enable_share == NULL ||
592 _sa_disable_share == NULL || _sa_errorstr == NULL ||
593 _sa_parse_legacy_options == NULL ||
594 _sa_needs_refresh == NULL || _sa_get_zfs_handle == NULL ||
595 _sa_zfs_process_share == NULL ||
596 _sa_update_sharetab_ts == NULL) {
599 _sa_disable_share = NULL;
600 _sa_enable_share = NULL;
602 _sa_parse_legacy_options = NULL;
603 (void) dlclose(libshare);
604 _sa_needs_refresh = NULL;
605 _sa_get_zfs_handle = NULL;
606 _sa_zfs_process_share = NULL;
607 _sa_update_sharetab_ts = NULL;
613 * zfs_init_libshare(zhandle, service)
615 * Initialize the libshare API if it hasn't already been initialized.
616 * In all cases it returns 0 if it succeeded and an error if not. The
617 * service value is which part(s) of the API to initialize and is a
618 * direct map to the libshare sa_init(service) interface.
621 zfs_init_libshare(libzfs_handle_t *zhandle, int service)
625 if (_sa_init == NULL)
628 if (ret == SA_OK && zhandle->libzfs_shareflags & ZFSSHARE_MISS) {
630 * We had a cache miss. Most likely it is a new ZFS
631 * dataset that was just created. We want to make sure
632 * so check timestamps to see if a different process
633 * has updated any of the configuration. If there was
634 * some non-ZFS change, we need to re-initialize the
637 zhandle->libzfs_shareflags &= ~ZFSSHARE_MISS;
638 if (_sa_needs_refresh != NULL &&
639 _sa_needs_refresh(zhandle->libzfs_sharehdl)) {
640 zfs_uninit_libshare(zhandle);
641 zhandle->libzfs_sharehdl = _sa_init(service);
645 if (ret == SA_OK && zhandle && zhandle->libzfs_sharehdl == NULL)
646 zhandle->libzfs_sharehdl = _sa_init(service);
648 if (ret == SA_OK && zhandle->libzfs_sharehdl == NULL)
655 * zfs_uninit_libshare(zhandle)
657 * Uninitialize the libshare API if it hasn't already been
658 * uninitialized. It is OK to call multiple times.
661 zfs_uninit_libshare(libzfs_handle_t *zhandle)
663 if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) {
664 if (_sa_fini != NULL)
665 _sa_fini(zhandle->libzfs_sharehdl);
666 zhandle->libzfs_sharehdl = NULL;
671 * zfs_parse_options(options, proto)
673 * Call the legacy parse interface to get the protocol specific
674 * options using the NULL arg to indicate that this is a "parse" only.
677 zfs_parse_options(char *options, zfs_share_proto_t proto)
679 if (_sa_parse_legacy_options != NULL) {
680 return (_sa_parse_legacy_options(NULL, options,
681 proto_table[proto].p_name));
683 return (SA_CONFIG_ERR);
687 * zfs_sa_find_share(handle, path)
689 * wrapper around sa_find_share to find a share path in the
693 zfs_sa_find_share(sa_handle_t handle, char *path)
695 if (_sa_find_share != NULL)
696 return (_sa_find_share(handle, path));
701 * zfs_sa_enable_share(share, proto)
703 * Wrapper for sa_enable_share which enables a share for a specified
707 zfs_sa_enable_share(sa_share_t share, char *proto)
709 if (_sa_enable_share != NULL)
710 return (_sa_enable_share(share, proto));
711 return (SA_CONFIG_ERR);
715 * zfs_sa_disable_share(share, proto)
717 * Wrapper for sa_enable_share which disables a share for a specified
721 zfs_sa_disable_share(sa_share_t share, char *proto)
723 if (_sa_disable_share != NULL)
724 return (_sa_disable_share(share, proto));
725 return (SA_CONFIG_ERR);
729 * Share the given filesystem according to the options in the specified
730 * protocol specific properties (sharenfs, sharesmb). We rely
731 * on "libshare" to the dirty work for us.
734 zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
736 char mountpoint[ZFS_MAXPROPLEN];
737 char shareopts[ZFS_MAXPROPLEN];
738 char sourcestr[ZFS_MAXPROPLEN];
739 libzfs_handle_t *hdl = zhp->zfs_hdl;
741 zfs_share_proto_t *curr_proto;
742 zprop_source_t sourcetype;
745 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
748 if ((ret = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
749 (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
750 dgettext(TEXT_DOMAIN, "cannot share '%s': %s"),
751 zfs_get_name(zhp), _sa_errorstr != NULL ?
752 _sa_errorstr(ret) : "");
756 for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) {
758 * Return success if there are no share options.
760 if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop,
761 shareopts, sizeof (shareopts), &sourcetype, sourcestr,
762 ZFS_MAXPROPLEN, B_FALSE) != 0 ||
763 strcmp(shareopts, "off") == 0)
767 * If the 'zoned' property is set, then zfs_is_mountable()
768 * will have already bailed out if we are in the global zone.
769 * But local zones cannot be NFS servers, so we ignore it for
770 * local zones as well.
772 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
775 share = zfs_sa_find_share(hdl->libzfs_sharehdl, mountpoint);
778 * This may be a new file system that was just
779 * created so isn't in the internal cache
780 * (second time through). Rather than
781 * reloading the entire configuration, we can
782 * assume ZFS has done the checking and it is
783 * safe to add this to the internal
786 if (_sa_zfs_process_share(hdl->libzfs_sharehdl,
787 NULL, NULL, mountpoint,
788 proto_table[*curr_proto].p_name, sourcetype,
789 shareopts, sourcestr, zhp->zfs_name) != SA_OK) {
790 (void) zfs_error_fmt(hdl,
791 proto_table[*curr_proto].p_share_err,
792 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
796 hdl->libzfs_shareflags |= ZFSSHARE_MISS;
797 share = zfs_sa_find_share(hdl->libzfs_sharehdl,
802 err = zfs_sa_enable_share(share,
803 proto_table[*curr_proto].p_name);
805 (void) zfs_error_fmt(hdl,
806 proto_table[*curr_proto].p_share_err,
807 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
812 (void) zfs_error_fmt(hdl,
813 proto_table[*curr_proto].p_share_err,
814 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
825 zfs_share_nfs(zfs_handle_t *zhp)
827 return (zfs_share_proto(zhp, nfs_only));
831 zfs_share_smb(zfs_handle_t *zhp)
833 return (zfs_share_proto(zhp, smb_only));
837 zfs_shareall(zfs_handle_t *zhp)
839 return (zfs_share_proto(zhp, share_all_proto));
843 * Unshare a filesystem by mountpoint.
846 unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint,
847 zfs_share_proto_t proto)
853 * Mountpoint could get trashed if libshare calls getmntany
854 * which id does during API initialization, so strdup the
857 mntpt = zfs_strdup(hdl, mountpoint);
859 /* make sure libshare initialized */
860 if ((err = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
861 free(mntpt); /* don't need the copy anymore */
862 return (zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
863 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
864 name, _sa_errorstr(err)));
867 share = zfs_sa_find_share(hdl->libzfs_sharehdl, mntpt);
868 free(mntpt); /* don't need the copy anymore */
871 err = zfs_sa_disable_share(share, proto_table[proto].p_name);
873 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
874 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
875 name, _sa_errorstr(err)));
878 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
879 dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"),
886 * Unshare the given filesystem.
889 zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint,
890 zfs_share_proto_t *proto)
892 struct mnttab search = { 0 }, entry;
895 /* check to see if need to unmount the filesystem */
896 search.mnt_special = (char *)zfs_get_name(zhp);
897 search.mnt_fstype = MNTTYPE_ZFS;
898 rewind(zhp->zfs_hdl->libzfs_mnttab);
899 if (mountpoint != NULL)
900 mntpt = zfs_strdup(zhp->zfs_hdl, mountpoint);
902 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
903 getmntany(zhp->zfs_hdl->libzfs_mnttab, &entry, &search) == 0)) {
904 zfs_share_proto_t *curr_proto;
906 if (mountpoint == NULL)
907 mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp);
909 for (curr_proto = proto; *curr_proto != PROTO_END;
912 if (is_shared(zhp->zfs_hdl, mntpt, *curr_proto) &&
913 unshare_one(zhp->zfs_hdl, zhp->zfs_name,
914 mntpt, *curr_proto) != 0) {
928 zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
930 return (zfs_unshare_proto(zhp, mountpoint, nfs_only));
934 zfs_unshare_smb(zfs_handle_t *zhp, const char *mountpoint)
936 return (zfs_unshare_proto(zhp, mountpoint, smb_only));
940 * Same as zfs_unmountall(), but for NFS and SMB unshares.
943 zfs_unshareall_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
945 prop_changelist_t *clp;
948 clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0);
952 ret = changelist_unshare(clp, proto);
953 changelist_free(clp);
959 zfs_unshareall_nfs(zfs_handle_t *zhp)
961 return (zfs_unshareall_proto(zhp, nfs_only));
965 zfs_unshareall_smb(zfs_handle_t *zhp)
967 return (zfs_unshareall_proto(zhp, smb_only));
971 zfs_unshareall(zfs_handle_t *zhp)
973 return (zfs_unshareall_proto(zhp, share_all_proto));
977 zfs_unshareall_bypath(zfs_handle_t *zhp, const char *mountpoint)
979 return (zfs_unshare_proto(zhp, mountpoint, share_all_proto));
983 * Remove the mountpoint associated with the current dataset, if necessary.
984 * We only remove the underlying directory if:
986 * - The mountpoint is not 'none' or 'legacy'
987 * - The mountpoint is non-empty
988 * - The mountpoint is the default or inherited
989 * - The 'zoned' property is set, or we're in a local zone
991 * Any other directories we leave alone.
994 remove_mountpoint(zfs_handle_t *zhp)
996 char mountpoint[ZFS_MAXPROPLEN];
997 zprop_source_t source;
999 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
1003 if (source == ZPROP_SRC_DEFAULT ||
1004 source == ZPROP_SRC_INHERITED) {
1006 * Try to remove the directory, silently ignoring any errors.
1007 * The filesystem may have since been removed or moved around,
1008 * and this error isn't really useful to the administrator in
1011 (void) rmdir(mountpoint);
1016 zfs_is_shared_iscsi(zfs_handle_t *zhp)
1020 * If iscsi deamon isn't running then we aren't shared
1022 if (iscsitgt_svc_online && iscsitgt_svc_online() == 1)
1025 return (iscsitgt_zfs_is_shared != NULL &&
1026 iscsitgt_zfs_is_shared(zhp->zfs_name) != 0);
1030 zfs_share_iscsi(zfs_handle_t *zhp)
1032 char shareopts[ZFS_MAXPROPLEN];
1033 const char *dataset = zhp->zfs_name;
1034 libzfs_handle_t *hdl = zhp->zfs_hdl;
1037 * Return success if there are no share options.
1039 if (zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI, shareopts,
1040 sizeof (shareopts), NULL, NULL, 0, B_FALSE) != 0 ||
1041 strcmp(shareopts, "off") == 0)
1044 if (iscsitgt_zfs_share == NULL || iscsitgt_zfs_share(dataset) != 0) {
1045 int error = EZFS_SHAREISCSIFAILED;
1048 * If service isn't availabele and EPERM was
1049 * returned then use special error.
1051 if (iscsitgt_svc_online && errno == EPERM &&
1052 (iscsitgt_svc_online() != 0))
1053 error = EZFS_ISCSISVCUNAVAIL;
1055 return (zfs_error_fmt(hdl, error,
1056 dgettext(TEXT_DOMAIN, "cannot share '%s'"), dataset));
1063 zfs_unshare_iscsi(zfs_handle_t *zhp)
1065 const char *dataset = zfs_get_name(zhp);
1066 libzfs_handle_t *hdl = zhp->zfs_hdl;
1069 * Return if the volume is not shared
1071 if (zfs_is_shared_iscsi(zhp) != SHARED_ISCSI)
1075 * If this fails with ENODEV it indicates that zvol wasn't shared so
1076 * we should return success in that case.
1078 if (iscsitgt_zfs_unshare == NULL ||
1079 (iscsitgt_zfs_unshare(dataset) != 0 && errno != ENODEV)) {
1081 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1082 "Insufficient privileges to unshare iscsi"));
1083 return (zfs_error_fmt(hdl, EZFS_UNSHAREISCSIFAILED,
1084 dgettext(TEXT_DOMAIN, "cannot unshare '%s'"), dataset));
1090 typedef struct mount_cbdata {
1091 zfs_handle_t **cb_datasets;
1097 mount_cb(zfs_handle_t *zhp, void *data)
1099 mount_cbdata_t *cbp = data;
1101 if (!(zfs_get_type(zhp) & (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME))) {
1106 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_NOAUTO) {
1111 if (cbp->cb_alloc == cbp->cb_used) {
1114 if ((ptr = zfs_realloc(zhp->zfs_hdl,
1115 cbp->cb_datasets, cbp->cb_alloc * sizeof (void *),
1116 cbp->cb_alloc * 2 * sizeof (void *))) == NULL)
1118 cbp->cb_datasets = ptr;
1123 cbp->cb_datasets[cbp->cb_used++] = zhp;
1125 return (zfs_iter_filesystems(zhp, mount_cb, cbp));
1129 dataset_cmp(const void *a, const void *b)
1131 zfs_handle_t **za = (zfs_handle_t **)a;
1132 zfs_handle_t **zb = (zfs_handle_t **)b;
1133 char mounta[MAXPATHLEN];
1134 char mountb[MAXPATHLEN];
1135 boolean_t gota, gotb;
1137 if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
1138 verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
1139 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
1140 if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
1141 verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
1142 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
1145 return (strcmp(mounta, mountb));
1152 return (strcmp(zfs_get_name(a), zfs_get_name(b)));
1156 * Mount and share all datasets within the given pool. This assumes that no
1157 * datasets within the pool are currently mounted. Because users can create
1158 * complicated nested hierarchies of mountpoints, we first gather all the
1159 * datasets and mountpoints within the pool, and sort them by mountpoint. Once
1160 * we have the list of all filesystems, we iterate over them in order and mount
1161 * and/or share each one.
1163 #pragma weak zpool_mount_datasets = zpool_enable_datasets
1165 zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
1167 mount_cbdata_t cb = { 0 };
1168 libzfs_handle_t *hdl = zhp->zpool_hdl;
1174 * Gather all non-snap datasets within the pool.
1176 if ((cb.cb_datasets = zfs_alloc(hdl, 4 * sizeof (void *))) == NULL)
1180 if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_DATASET)) == NULL)
1183 cb.cb_datasets[0] = zfsp;
1186 if (zfs_iter_filesystems(zfsp, mount_cb, &cb) != 0)
1190 * Sort the datasets by mountpoint.
1192 qsort(cb.cb_datasets, cb.cb_used, sizeof (void *), dataset_cmp);
1195 * And mount all the datasets, keeping track of which ones
1196 * succeeded or failed. By using zfs_alloc(), the good pointer
1197 * will always be non-NULL.
1199 good = zfs_alloc(zhp->zpool_hdl, cb.cb_used * sizeof (int));
1201 for (i = 0; i < cb.cb_used; i++) {
1202 if (zfs_mount(cb.cb_datasets[i], mntopts, flags) != 0)
1209 * Then share all the ones that need to be shared. This needs
1210 * to be a separate pass in order to avoid excessive reloading
1211 * of the configuration. Good should never be NULL since
1212 * zfs_alloc is supposed to exit if memory isn't available.
1214 for (i = 0; i < cb.cb_used; i++) {
1215 if (good[i] && zfs_share(cb.cb_datasets[i]) != 0)
1222 for (i = 0; i < cb.cb_used; i++)
1223 zfs_close(cb.cb_datasets[i]);
1224 free(cb.cb_datasets);
1231 zvol_cb(const char *dataset, void *data)
1233 libzfs_handle_t *hdl = data;
1237 * Ignore snapshots and ignore failures from non-existant datasets.
1239 if (strchr(dataset, '@') != NULL ||
1240 (zhp = zfs_open(hdl, dataset, ZFS_TYPE_VOLUME)) == NULL)
1243 if (zfs_unshare_iscsi(zhp) != 0)
1252 mountpoint_compare(const void *a, const void *b)
1254 const char *mounta = *((char **)a);
1255 const char *mountb = *((char **)b);
1257 return (strcmp(mountb, mounta));
1261 * Unshare and unmount all datasets within the given pool. We don't want to
1262 * rely on traversing the DSL to discover the filesystems within the pool,
1263 * because this may be expensive (if not all of them are mounted), and can fail
1264 * arbitrarily (on I/O error, for example). Instead, we walk /etc/mnttab and
1265 * gather all the filesystems that are currently mounted.
1267 #pragma weak zpool_unmount_datasets = zpool_disable_datasets
1269 zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
1272 struct mnttab entry;
1274 char **mountpoints = NULL;
1275 zfs_handle_t **datasets = NULL;
1276 libzfs_handle_t *hdl = zhp->zpool_hdl;
1279 int flags = (force ? MS_FORCE : 0);
1282 * First unshare all zvols.
1284 if (zpool_iter_zvol(zhp, zvol_cb, hdl) != 0)
1287 namelen = strlen(zhp->zpool_name);
1289 rewind(hdl->libzfs_mnttab);
1291 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
1293 * Ignore non-ZFS entries.
1295 if (entry.mnt_fstype == NULL ||
1296 strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
1300 * Ignore filesystems not within this pool.
1302 if (entry.mnt_mountp == NULL ||
1303 strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 ||
1304 (entry.mnt_special[namelen] != '/' &&
1305 entry.mnt_special[namelen] != '\0'))
1309 * At this point we've found a filesystem within our pool. Add
1310 * it to our growing list.
1312 if (used == alloc) {
1314 if ((mountpoints = zfs_alloc(hdl,
1315 8 * sizeof (void *))) == NULL)
1318 if ((datasets = zfs_alloc(hdl,
1319 8 * sizeof (void *))) == NULL)
1326 if ((ptr = zfs_realloc(hdl, mountpoints,
1327 alloc * sizeof (void *),
1328 alloc * 2 * sizeof (void *))) == NULL)
1332 if ((ptr = zfs_realloc(hdl, datasets,
1333 alloc * sizeof (void *),
1334 alloc * 2 * sizeof (void *))) == NULL)
1342 if ((mountpoints[used] = zfs_strdup(hdl,
1343 entry.mnt_mountp)) == NULL)
1347 * This is allowed to fail, in case there is some I/O error. It
1348 * is only used to determine if we need to remove the underlying
1349 * mountpoint, so failure is not fatal.
1351 datasets[used] = make_dataset_handle(hdl, entry.mnt_special);
1357 * At this point, we have the entire list of filesystems, so sort it by
1360 qsort(mountpoints, used, sizeof (char *), mountpoint_compare);
1363 * Walk through and first unshare everything.
1365 for (i = 0; i < used; i++) {
1366 zfs_share_proto_t *curr_proto;
1367 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
1369 if (is_shared(hdl, mountpoints[i], *curr_proto) &&
1370 unshare_one(hdl, mountpoints[i],
1371 mountpoints[i], *curr_proto) != 0)
1377 * Now unmount everything, removing the underlying directories as
1380 for (i = 0; i < used; i++) {
1381 if (unmount_one(hdl, mountpoints[i], flags) != 0)
1385 for (i = 0; i < used; i++) {
1387 remove_mountpoint(datasets[i]);
1392 for (i = 0; i < used; i++) {
1394 zfs_close(datasets[i]);
1395 free(mountpoints[i]);