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 2015 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright (c) 2014, 2016 by Delphix. All rights reserved.
26 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
27 * Copyright 2017 RackTop Systems.
31 * Routines to manage ZFS mounts. We separate all the nasty routines that have
32 * to deal with the OS. The following functions are the main entry points --
33 * they are used by mount and unmount and when changing a filesystem's
41 * This file also contains the functions used to manage sharing filesystems via
54 * zfs_unshareall_nfs()
55 * zfs_unshareall_smb()
57 * zfs_unshareall_bypath()
59 * The following functions are available for pool consumers, and will
60 * mount/unmount and share/unshare all datasets within pool:
62 * zpool_enable_datasets()
63 * zpool_disable_datasets()
77 #include <sys/mntent.h>
78 #include <sys/mount.h>
80 #include <sys/statvfs.h>
84 #include "libzfs_impl.h"
87 #include <sys/systeminfo.h>
88 #define MAXISALEN 257 /* based on sysinfo(2) man page */
90 static int zfs_share_proto(zfs_handle_t
*, zfs_share_proto_t
*);
91 zfs_share_type_t
zfs_is_shared_proto(zfs_handle_t
*, char **,
95 * The share protocols table must be in the same order as the zfs_share_proto_t
96 * enum in libzfs_impl.h
105 proto_table_t proto_table
[PROTO_END
] = {
106 {ZFS_PROP_SHARENFS
, "nfs", EZFS_SHARENFSFAILED
, EZFS_UNSHARENFSFAILED
},
107 {ZFS_PROP_SHARESMB
, "smb", EZFS_SHARESMBFAILED
, EZFS_UNSHARESMBFAILED
},
110 zfs_share_proto_t nfs_only
[] = {
115 zfs_share_proto_t smb_only
[] = {
119 zfs_share_proto_t share_all_proto
[] = {
126 * Search the sharetab for the given mountpoint and protocol, returning
127 * a zfs_share_type_t value.
129 static zfs_share_type_t
130 is_shared(libzfs_handle_t
*hdl
, const char *mountpoint
, zfs_share_proto_t proto
)
132 char buf
[MAXPATHLEN
], *tab
;
135 if (hdl
->libzfs_sharetab
== NULL
)
136 return (SHARED_NOT_SHARED
);
138 (void) fseek(hdl
->libzfs_sharetab
, 0, SEEK_SET
);
140 while (fgets(buf
, sizeof (buf
), hdl
->libzfs_sharetab
) != NULL
) {
142 /* the mountpoint is the first entry on each line */
143 if ((tab
= strchr(buf
, '\t')) == NULL
)
147 if (strcmp(buf
, mountpoint
) == 0) {
149 * the protocol field is the third field
150 * skip over second field
153 if ((tab
= strchr(ptr
, '\t')) == NULL
)
156 if ((tab
= strchr(ptr
, '\t')) == NULL
)
160 proto_table
[proto
].p_name
) == 0) {
173 return (SHARED_NOT_SHARED
);
177 dir_is_empty_stat(const char *dirname
)
182 * We only want to return false if the given path is a non empty
183 * directory, all other errors are handled elsewhere.
185 if (stat(dirname
, &st
) < 0 || !S_ISDIR(st
.st_mode
)) {
190 * An empty directory will still have two entries in it, one
191 * entry for each of "." and "..".
193 if (st
.st_size
> 2) {
201 dir_is_empty_readdir(const char *dirname
)
207 if ((dirfd
= openat(AT_FDCWD
, dirname
,
208 O_RDONLY
| O_NDELAY
| O_LARGEFILE
| O_CLOEXEC
, 0)) < 0) {
212 if ((dirp
= fdopendir(dirfd
)) == NULL
) {
217 while ((dp
= readdir64(dirp
)) != NULL
) {
219 if (strcmp(dp
->d_name
, ".") == 0 ||
220 strcmp(dp
->d_name
, "..") == 0)
223 (void) closedir(dirp
);
227 (void) closedir(dirp
);
232 * Returns true if the specified directory is empty. If we can't open the
233 * directory at all, return true so that the mount can fail with a more
234 * informative error message.
237 dir_is_empty(const char *dirname
)
242 * If the statvfs call fails or the filesystem is not a ZFS
243 * filesystem, fall back to the slow path which uses readdir.
245 if ((statvfs64(dirname
, &st
) != 0) ||
246 (strcmp(st
.f_basetype
, "zfs") != 0)) {
247 return (dir_is_empty_readdir(dirname
));
251 * At this point, we know the provided path is on a ZFS
252 * filesystem, so we can use stat instead of readdir to
253 * determine if the directory is empty or not. We try to avoid
254 * using readdir because that requires opening "dirname"; this
255 * open file descriptor can potentially end up in a child
256 * process if there's a concurrent fork, thus preventing the
257 * zfs_mount() from otherwise succeeding (the open file
258 * descriptor inherited by the child process will cause the
259 * parent's mount to fail with EBUSY). The performance
260 * implications of replacing the open, read, and close with a
261 * single stat is nice; but is not the main motivation for the
264 return (dir_is_empty_stat(dirname
));
268 * Checks to see if the mount is active. If the filesystem is mounted, we fill
269 * in 'where' with the current mountpoint, and return 1. Otherwise, we return
273 is_mounted(libzfs_handle_t
*zfs_hdl
, const char *special
, char **where
)
277 if (libzfs_mnttab_find(zfs_hdl
, special
, &entry
) != 0)
281 *where
= zfs_strdup(zfs_hdl
, entry
.mnt_mountp
);
287 zfs_is_mounted(zfs_handle_t
*zhp
, char **where
)
289 return (is_mounted(zhp
->zfs_hdl
, zfs_get_name(zhp
), where
));
293 * Returns true if the given dataset is mountable, false otherwise. Returns the
294 * mountpoint in 'buf'.
297 zfs_is_mountable(zfs_handle_t
*zhp
, char *buf
, size_t buflen
,
298 zprop_source_t
*source
)
300 char sourceloc
[MAXNAMELEN
];
301 zprop_source_t sourcetype
;
303 if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT
, zhp
->zfs_type
))
306 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, buf
, buflen
,
307 &sourcetype
, sourceloc
, sizeof (sourceloc
), B_FALSE
) == 0);
309 if (strcmp(buf
, ZFS_MOUNTPOINT_NONE
) == 0 ||
310 strcmp(buf
, ZFS_MOUNTPOINT_LEGACY
) == 0)
313 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) == ZFS_CANMOUNT_OFF
)
316 if (zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
) &&
317 getzoneid() == GLOBAL_ZONEID
)
321 *source
= sourcetype
;
327 * Mount the given filesystem.
330 zfs_mount(zfs_handle_t
*zhp
, const char *options
, int flags
)
333 char mountpoint
[ZFS_MAXPROPLEN
];
334 char mntopts
[MNT_LINE_MAX
];
335 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
340 (void) strlcpy(mntopts
, options
, sizeof (mntopts
));
343 * If the pool is imported read-only then all mounts must be read-only
345 if (zpool_get_prop_int(zhp
->zpool_hdl
, ZPOOL_PROP_READONLY
, NULL
))
348 if (!zfs_is_mountable(zhp
, mountpoint
, sizeof (mountpoint
), NULL
))
351 /* Create the directory if it doesn't already exist */
352 if (lstat(mountpoint
, &buf
) != 0) {
353 if (mkdirp(mountpoint
, 0755) != 0) {
354 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
355 "failed to create mountpoint"));
356 return (zfs_error_fmt(hdl
, EZFS_MOUNTFAILED
,
357 dgettext(TEXT_DOMAIN
, "cannot mount '%s'"),
363 * Determine if the mountpoint is empty. If so, refuse to perform the
364 * mount. We don't perform this check if MS_OVERLAY is specified, which
365 * would defeat the point. We also avoid this check if 'remount' is
368 if ((flags
& MS_OVERLAY
) == 0 &&
369 strstr(mntopts
, MNTOPT_REMOUNT
) == NULL
&&
370 !dir_is_empty(mountpoint
)) {
371 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
372 "directory is not empty"));
373 return (zfs_error_fmt(hdl
, EZFS_MOUNTFAILED
,
374 dgettext(TEXT_DOMAIN
, "cannot mount '%s'"), mountpoint
));
377 /* perform the mount */
378 if (mount(zfs_get_name(zhp
), mountpoint
, MS_OPTIONSTR
| flags
,
379 MNTTYPE_ZFS
, NULL
, 0, mntopts
, sizeof (mntopts
)) != 0) {
381 * Generic errors are nasty, but there are just way too many
382 * from mount(), and they're well-understood. We pick a few
383 * common ones to improve upon.
385 if (errno
== EBUSY
) {
386 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
387 "mountpoint or dataset is busy"));
388 } else if (errno
== EPERM
) {
389 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
390 "Insufficient privileges"));
391 } else if (errno
== ENOTSUP
) {
395 VERIFY(zfs_spa_version(zhp
, &spa_version
) == 0);
396 (void) snprintf(buf
, sizeof (buf
),
397 dgettext(TEXT_DOMAIN
, "Can't mount a version %lld "
398 "file system on a version %d pool. Pool must be"
399 " upgraded to mount this file system."),
400 (u_longlong_t
)zfs_prop_get_int(zhp
,
401 ZFS_PROP_VERSION
), spa_version
);
402 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, buf
));
404 zfs_error_aux(hdl
, strerror(errno
));
406 return (zfs_error_fmt(hdl
, EZFS_MOUNTFAILED
,
407 dgettext(TEXT_DOMAIN
, "cannot mount '%s'"),
411 /* add the mounted entry into our cache */
412 libzfs_mnttab_add(hdl
, zfs_get_name(zhp
), mountpoint
,
418 * Unmount a single filesystem.
421 unmount_one(libzfs_handle_t
*hdl
, const char *mountpoint
, int flags
)
423 if (umount2(mountpoint
, flags
) != 0) {
424 zfs_error_aux(hdl
, strerror(errno
));
425 return (zfs_error_fmt(hdl
, EZFS_UMOUNTFAILED
,
426 dgettext(TEXT_DOMAIN
, "cannot unmount '%s'"),
434 * Unmount the given filesystem.
437 zfs_unmount(zfs_handle_t
*zhp
, const char *mountpoint
, int flags
)
439 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
443 /* check to see if we need to unmount the filesystem */
444 if (mountpoint
!= NULL
|| ((zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) &&
445 libzfs_mnttab_find(hdl
, zhp
->zfs_name
, &entry
) == 0)) {
447 * mountpoint may have come from a call to
448 * getmnt/getmntany if it isn't NULL. If it is NULL,
449 * we know it comes from libzfs_mnttab_find which can
450 * then get freed later. We strdup it to play it safe.
452 if (mountpoint
== NULL
)
453 mntpt
= zfs_strdup(hdl
, entry
.mnt_mountp
);
455 mntpt
= zfs_strdup(hdl
, mountpoint
);
458 * Unshare and unmount the filesystem
460 if (zfs_unshare_proto(zhp
, mntpt
, share_all_proto
) != 0)
463 if (unmount_one(hdl
, mntpt
, flags
) != 0) {
465 (void) zfs_shareall(zhp
);
468 libzfs_mnttab_remove(hdl
, zhp
->zfs_name
);
476 * Unmount this filesystem and any children inheriting the mountpoint property.
477 * To do this, just act like we're changing the mountpoint property, but don't
478 * remount the filesystems afterwards.
481 zfs_unmountall(zfs_handle_t
*zhp
, int flags
)
483 prop_changelist_t
*clp
;
486 clp
= changelist_gather(zhp
, ZFS_PROP_MOUNTPOINT
, 0, flags
);
490 ret
= changelist_prefix(clp
);
491 changelist_free(clp
);
497 zfs_is_shared(zfs_handle_t
*zhp
)
499 zfs_share_type_t rc
= 0;
500 zfs_share_proto_t
*curr_proto
;
502 if (ZFS_IS_VOLUME(zhp
))
505 for (curr_proto
= share_all_proto
; *curr_proto
!= PROTO_END
;
507 rc
|= zfs_is_shared_proto(zhp
, NULL
, *curr_proto
);
509 return (rc
? B_TRUE
: B_FALSE
);
513 zfs_share(zfs_handle_t
*zhp
)
515 assert(!ZFS_IS_VOLUME(zhp
));
516 return (zfs_share_proto(zhp
, share_all_proto
));
520 zfs_unshare(zfs_handle_t
*zhp
)
522 assert(!ZFS_IS_VOLUME(zhp
));
523 return (zfs_unshareall(zhp
));
527 * Check to see if the filesystem is currently shared.
530 zfs_is_shared_proto(zfs_handle_t
*zhp
, char **where
, zfs_share_proto_t proto
)
535 if (!zfs_is_mounted(zhp
, &mountpoint
))
536 return (SHARED_NOT_SHARED
);
538 if ((rc
= is_shared(zhp
->zfs_hdl
, mountpoint
, proto
))
539 != SHARED_NOT_SHARED
) {
547 return (SHARED_NOT_SHARED
);
552 zfs_is_shared_nfs(zfs_handle_t
*zhp
, char **where
)
554 return (zfs_is_shared_proto(zhp
, where
,
555 PROTO_NFS
) != SHARED_NOT_SHARED
);
559 zfs_is_shared_smb(zfs_handle_t
*zhp
, char **where
)
561 return (zfs_is_shared_proto(zhp
, where
,
562 PROTO_SMB
) != SHARED_NOT_SHARED
);
566 * Make sure things will work if libshare isn't installed by using
567 * wrapper functions that check to see that the pointers to functions
568 * initialized in _zfs_init_libshare() are actually present.
571 static sa_handle_t (*_sa_init
)(int);
572 static sa_handle_t (*_sa_init_arg
)(int, void *);
573 static void (*_sa_fini
)(sa_handle_t
);
574 static sa_share_t (*_sa_find_share
)(sa_handle_t
, char *);
575 static int (*_sa_enable_share
)(sa_share_t
, char *);
576 static int (*_sa_disable_share
)(sa_share_t
, char *);
577 static char *(*_sa_errorstr
)(int);
578 static int (*_sa_parse_legacy_options
)(sa_group_t
, char *, char *);
579 static boolean_t (*_sa_needs_refresh
)(sa_handle_t
*);
580 static libzfs_handle_t
*(*_sa_get_zfs_handle
)(sa_handle_t
);
581 static int (*_sa_zfs_process_share
)(sa_handle_t
, sa_group_t
, sa_share_t
,
582 char *, char *, zprop_source_t
, char *, char *, char *);
583 static void (*_sa_update_sharetab_ts
)(sa_handle_t
);
586 * _zfs_init_libshare()
588 * Find the libshare.so.1 entry points that we use here and save the
589 * values to be used later. This is triggered by the runtime loader.
590 * Make sure the correct ISA version is loaded.
593 #pragma init(_zfs_init_libshare)
595 _zfs_init_libshare(void)
598 char path
[MAXPATHLEN
];
602 if (sysinfo(SI_ARCHITECTURE_64
, isa
, MAXISALEN
) == -1)
607 (void) snprintf(path
, MAXPATHLEN
,
608 "/usr/lib/%s/libshare.so.1", isa
);
610 if ((libshare
= dlopen(path
, RTLD_LAZY
| RTLD_GLOBAL
)) != NULL
) {
611 _sa_init
= (sa_handle_t (*)(int))dlsym(libshare
, "sa_init");
612 _sa_init_arg
= (sa_handle_t (*)(int, void *))dlsym(libshare
,
614 _sa_fini
= (void (*)(sa_handle_t
))dlsym(libshare
, "sa_fini");
615 _sa_find_share
= (sa_share_t (*)(sa_handle_t
, char *))
616 dlsym(libshare
, "sa_find_share");
617 _sa_enable_share
= (int (*)(sa_share_t
, char *))dlsym(libshare
,
619 _sa_disable_share
= (int (*)(sa_share_t
, char *))dlsym(libshare
,
621 _sa_errorstr
= (char *(*)(int))dlsym(libshare
, "sa_errorstr");
622 _sa_parse_legacy_options
= (int (*)(sa_group_t
, char *, char *))
623 dlsym(libshare
, "sa_parse_legacy_options");
624 _sa_needs_refresh
= (boolean_t (*)(sa_handle_t
*))
625 dlsym(libshare
, "sa_needs_refresh");
626 _sa_get_zfs_handle
= (libzfs_handle_t
*(*)(sa_handle_t
))
627 dlsym(libshare
, "sa_get_zfs_handle");
628 _sa_zfs_process_share
= (int (*)(sa_handle_t
, sa_group_t
,
629 sa_share_t
, char *, char *, zprop_source_t
, char *,
630 char *, char *))dlsym(libshare
, "sa_zfs_process_share");
631 _sa_update_sharetab_ts
= (void (*)(sa_handle_t
))
632 dlsym(libshare
, "sa_update_sharetab_ts");
633 if (_sa_init
== NULL
|| _sa_init_arg
== NULL
||
634 _sa_fini
== NULL
|| _sa_find_share
== NULL
||
635 _sa_enable_share
== NULL
|| _sa_disable_share
== NULL
||
636 _sa_errorstr
== NULL
|| _sa_parse_legacy_options
== NULL
||
637 _sa_needs_refresh
== NULL
|| _sa_get_zfs_handle
== NULL
||
638 _sa_zfs_process_share
== NULL
||
639 _sa_update_sharetab_ts
== NULL
) {
643 _sa_disable_share
= NULL
;
644 _sa_enable_share
= NULL
;
646 _sa_parse_legacy_options
= NULL
;
647 (void) dlclose(libshare
);
648 _sa_needs_refresh
= NULL
;
649 _sa_get_zfs_handle
= NULL
;
650 _sa_zfs_process_share
= NULL
;
651 _sa_update_sharetab_ts
= NULL
;
657 * zfs_init_libshare(zhandle, service)
659 * Initialize the libshare API if it hasn't already been initialized.
660 * In all cases it returns 0 if it succeeded and an error if not. The
661 * service value is which part(s) of the API to initialize and is a
662 * direct map to the libshare sa_init(service) interface.
665 zfs_init_libshare_impl(libzfs_handle_t
*zhandle
, int service
, void *arg
)
667 if (_sa_init
== NULL
)
668 return (SA_CONFIG_ERR
);
671 * Attempt to refresh libshare. This is necessary if there was a cache
672 * miss for a new ZFS dataset that was just created, or if state of the
673 * sharetab file has changed since libshare was last initialized. We
674 * want to make sure so check timestamps to see if a different process
675 * has updated any of the configuration. If there was some non-ZFS
676 * change, we need to re-initialize the internal cache.
678 if (_sa_needs_refresh
!= NULL
&&
679 _sa_needs_refresh(zhandle
->libzfs_sharehdl
)) {
680 zfs_uninit_libshare(zhandle
);
681 zhandle
->libzfs_sharehdl
= _sa_init_arg(service
, arg
);
684 if (zhandle
&& zhandle
->libzfs_sharehdl
== NULL
)
685 zhandle
->libzfs_sharehdl
= _sa_init_arg(service
, arg
);
687 if (zhandle
->libzfs_sharehdl
== NULL
)
688 return (SA_NO_MEMORY
);
693 zfs_init_libshare(libzfs_handle_t
*zhandle
, int service
)
695 return (zfs_init_libshare_impl(zhandle
, service
, NULL
));
699 zfs_init_libshare_arg(libzfs_handle_t
*zhandle
, int service
, void *arg
)
701 return (zfs_init_libshare_impl(zhandle
, service
, arg
));
706 * zfs_uninit_libshare(zhandle)
708 * Uninitialize the libshare API if it hasn't already been
709 * uninitialized. It is OK to call multiple times.
712 zfs_uninit_libshare(libzfs_handle_t
*zhandle
)
714 if (zhandle
!= NULL
&& zhandle
->libzfs_sharehdl
!= NULL
) {
715 if (_sa_fini
!= NULL
)
716 _sa_fini(zhandle
->libzfs_sharehdl
);
717 zhandle
->libzfs_sharehdl
= NULL
;
722 * zfs_parse_options(options, proto)
724 * Call the legacy parse interface to get the protocol specific
725 * options using the NULL arg to indicate that this is a "parse" only.
728 zfs_parse_options(char *options
, zfs_share_proto_t proto
)
730 if (_sa_parse_legacy_options
!= NULL
) {
731 return (_sa_parse_legacy_options(NULL
, options
,
732 proto_table
[proto
].p_name
));
734 return (SA_CONFIG_ERR
);
738 * zfs_sa_find_share(handle, path)
740 * wrapper around sa_find_share to find a share path in the
744 zfs_sa_find_share(sa_handle_t handle
, char *path
)
746 if (_sa_find_share
!= NULL
)
747 return (_sa_find_share(handle
, path
));
752 * zfs_sa_enable_share(share, proto)
754 * Wrapper for sa_enable_share which enables a share for a specified
758 zfs_sa_enable_share(sa_share_t share
, char *proto
)
760 if (_sa_enable_share
!= NULL
)
761 return (_sa_enable_share(share
, proto
));
762 return (SA_CONFIG_ERR
);
766 * zfs_sa_disable_share(share, proto)
768 * Wrapper for sa_enable_share which disables a share for a specified
772 zfs_sa_disable_share(sa_share_t share
, char *proto
)
774 if (_sa_disable_share
!= NULL
)
775 return (_sa_disable_share(share
, proto
));
776 return (SA_CONFIG_ERR
);
780 * Share the given filesystem according to the options in the specified
781 * protocol specific properties (sharenfs, sharesmb). We rely
782 * on "libshare" to the dirty work for us.
785 zfs_share_proto(zfs_handle_t
*zhp
, zfs_share_proto_t
*proto
)
787 char mountpoint
[ZFS_MAXPROPLEN
];
788 char shareopts
[ZFS_MAXPROPLEN
];
789 char sourcestr
[ZFS_MAXPROPLEN
];
790 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
792 zfs_share_proto_t
*curr_proto
;
793 zprop_source_t sourcetype
;
796 if (!zfs_is_mountable(zhp
, mountpoint
, sizeof (mountpoint
), NULL
))
799 for (curr_proto
= proto
; *curr_proto
!= PROTO_END
; curr_proto
++) {
801 * Return success if there are no share options.
803 if (zfs_prop_get(zhp
, proto_table
[*curr_proto
].p_prop
,
804 shareopts
, sizeof (shareopts
), &sourcetype
, sourcestr
,
805 ZFS_MAXPROPLEN
, B_FALSE
) != 0 ||
806 strcmp(shareopts
, "off") == 0)
808 ret
= zfs_init_libshare_arg(hdl
, SA_INIT_ONE_SHARE_FROM_HANDLE
,
811 (void) zfs_error_fmt(hdl
, EZFS_SHARENFSFAILED
,
812 dgettext(TEXT_DOMAIN
, "cannot share '%s': %s"),
813 zfs_get_name(zhp
), _sa_errorstr
!= NULL
?
814 _sa_errorstr(ret
) : "");
819 * If the 'zoned' property is set, then zfs_is_mountable()
820 * will have already bailed out if we are in the global zone.
821 * But local zones cannot be NFS servers, so we ignore it for
822 * local zones as well.
824 if (zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
))
827 share
= zfs_sa_find_share(hdl
->libzfs_sharehdl
, mountpoint
);
830 * This may be a new file system that was just
831 * created so isn't in the internal cache
832 * (second time through). Rather than
833 * reloading the entire configuration, we can
834 * assume ZFS has done the checking and it is
835 * safe to add this to the internal
838 if (_sa_zfs_process_share(hdl
->libzfs_sharehdl
,
839 NULL
, NULL
, mountpoint
,
840 proto_table
[*curr_proto
].p_name
, sourcetype
,
841 shareopts
, sourcestr
, zhp
->zfs_name
) != SA_OK
) {
842 (void) zfs_error_fmt(hdl
,
843 proto_table
[*curr_proto
].p_share_err
,
844 dgettext(TEXT_DOMAIN
, "cannot share '%s'"),
848 share
= zfs_sa_find_share(hdl
->libzfs_sharehdl
,
853 err
= zfs_sa_enable_share(share
,
854 proto_table
[*curr_proto
].p_name
);
856 (void) zfs_error_fmt(hdl
,
857 proto_table
[*curr_proto
].p_share_err
,
858 dgettext(TEXT_DOMAIN
, "cannot share '%s'"),
863 (void) zfs_error_fmt(hdl
,
864 proto_table
[*curr_proto
].p_share_err
,
865 dgettext(TEXT_DOMAIN
, "cannot share '%s'"),
876 zfs_share_nfs(zfs_handle_t
*zhp
)
878 return (zfs_share_proto(zhp
, nfs_only
));
882 zfs_share_smb(zfs_handle_t
*zhp
)
884 return (zfs_share_proto(zhp
, smb_only
));
888 zfs_shareall(zfs_handle_t
*zhp
)
890 return (zfs_share_proto(zhp
, share_all_proto
));
894 * Unshare a filesystem by mountpoint.
897 unshare_one(libzfs_handle_t
*hdl
, const char *name
, const char *mountpoint
,
898 zfs_share_proto_t proto
)
905 * Mountpoint could get trashed if libshare calls getmntany
906 * which it does during API initialization, so strdup the
909 mntpt
= zfs_strdup(hdl
, mountpoint
);
912 * make sure libshare initialized, initialize everything because we
913 * don't know what other unsharing may happen later. Functions up the
914 * stack are allowed to initialize instead a subset of shares at the
915 * time the set is known.
917 if ((err
= zfs_init_libshare_arg(hdl
, SA_INIT_ONE_SHARE_FROM_NAME
,
918 (void *)name
)) != SA_OK
) {
919 free(mntpt
); /* don't need the copy anymore */
920 return (zfs_error_fmt(hdl
, proto_table
[proto
].p_unshare_err
,
921 dgettext(TEXT_DOMAIN
, "cannot unshare '%s': %s"),
922 name
, _sa_errorstr(err
)));
925 share
= zfs_sa_find_share(hdl
->libzfs_sharehdl
, mntpt
);
926 free(mntpt
); /* don't need the copy anymore */
929 err
= zfs_sa_disable_share(share
, proto_table
[proto
].p_name
);
931 return (zfs_error_fmt(hdl
,
932 proto_table
[proto
].p_unshare_err
,
933 dgettext(TEXT_DOMAIN
, "cannot unshare '%s': %s"),
934 name
, _sa_errorstr(err
)));
937 return (zfs_error_fmt(hdl
, proto_table
[proto
].p_unshare_err
,
938 dgettext(TEXT_DOMAIN
, "cannot unshare '%s': not found"),
945 * Unshare the given filesystem.
948 zfs_unshare_proto(zfs_handle_t
*zhp
, const char *mountpoint
,
949 zfs_share_proto_t
*proto
)
951 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
955 /* check to see if need to unmount the filesystem */
956 rewind(zhp
->zfs_hdl
->libzfs_mnttab
);
957 if (mountpoint
!= NULL
)
958 mountpoint
= mntpt
= zfs_strdup(hdl
, mountpoint
);
960 if (mountpoint
!= NULL
|| ((zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) &&
961 libzfs_mnttab_find(hdl
, zfs_get_name(zhp
), &entry
) == 0)) {
962 zfs_share_proto_t
*curr_proto
;
964 if (mountpoint
== NULL
)
965 mntpt
= zfs_strdup(zhp
->zfs_hdl
, entry
.mnt_mountp
);
967 for (curr_proto
= proto
; *curr_proto
!= PROTO_END
;
970 if (is_shared(hdl
, mntpt
, *curr_proto
) &&
971 unshare_one(hdl
, zhp
->zfs_name
,
972 mntpt
, *curr_proto
) != 0) {
984 zfs_unshare_nfs(zfs_handle_t
*zhp
, const char *mountpoint
)
986 return (zfs_unshare_proto(zhp
, mountpoint
, nfs_only
));
990 zfs_unshare_smb(zfs_handle_t
*zhp
, const char *mountpoint
)
992 return (zfs_unshare_proto(zhp
, mountpoint
, smb_only
));
996 * Same as zfs_unmountall(), but for NFS and SMB unshares.
999 zfs_unshareall_proto(zfs_handle_t
*zhp
, zfs_share_proto_t
*proto
)
1001 prop_changelist_t
*clp
;
1004 clp
= changelist_gather(zhp
, ZFS_PROP_SHARENFS
, 0, 0);
1008 ret
= changelist_unshare(clp
, proto
);
1009 changelist_free(clp
);
1015 zfs_unshareall_nfs(zfs_handle_t
*zhp
)
1017 return (zfs_unshareall_proto(zhp
, nfs_only
));
1021 zfs_unshareall_smb(zfs_handle_t
*zhp
)
1023 return (zfs_unshareall_proto(zhp
, smb_only
));
1027 zfs_unshareall(zfs_handle_t
*zhp
)
1029 return (zfs_unshareall_proto(zhp
, share_all_proto
));
1033 zfs_unshareall_bypath(zfs_handle_t
*zhp
, const char *mountpoint
)
1035 return (zfs_unshare_proto(zhp
, mountpoint
, share_all_proto
));
1039 * Remove the mountpoint associated with the current dataset, if necessary.
1040 * We only remove the underlying directory if:
1042 * - The mountpoint is not 'none' or 'legacy'
1043 * - The mountpoint is non-empty
1044 * - The mountpoint is the default or inherited
1045 * - The 'zoned' property is set, or we're in a local zone
1047 * Any other directories we leave alone.
1050 remove_mountpoint(zfs_handle_t
*zhp
)
1052 char mountpoint
[ZFS_MAXPROPLEN
];
1053 zprop_source_t source
;
1055 if (!zfs_is_mountable(zhp
, mountpoint
, sizeof (mountpoint
),
1059 if (source
== ZPROP_SRC_DEFAULT
||
1060 source
== ZPROP_SRC_INHERITED
) {
1062 * Try to remove the directory, silently ignoring any errors.
1063 * The filesystem may have since been removed or moved around,
1064 * and this error isn't really useful to the administrator in
1067 (void) rmdir(mountpoint
);
1072 libzfs_add_handle(get_all_cb_t
*cbp
, zfs_handle_t
*zhp
)
1074 if (cbp
->cb_alloc
== cbp
->cb_used
) {
1078 newsz
= cbp
->cb_alloc
? cbp
->cb_alloc
* 2 : 64;
1079 ptr
= zfs_realloc(zhp
->zfs_hdl
,
1080 cbp
->cb_handles
, cbp
->cb_alloc
* sizeof (void *),
1081 newsz
* sizeof (void *));
1082 cbp
->cb_handles
= ptr
;
1083 cbp
->cb_alloc
= newsz
;
1085 cbp
->cb_handles
[cbp
->cb_used
++] = zhp
;
1089 mount_cb(zfs_handle_t
*zhp
, void *data
)
1091 get_all_cb_t
*cbp
= data
;
1093 if (!(zfs_get_type(zhp
) & ZFS_TYPE_FILESYSTEM
)) {
1098 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) == ZFS_CANMOUNT_NOAUTO
) {
1104 * If this filesystem is inconsistent and has a receive resume
1105 * token, we can not mount it.
1107 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) &&
1108 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
1109 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == 0) {
1114 libzfs_add_handle(cbp
, zhp
);
1115 if (zfs_iter_filesystems(zhp
, mount_cb
, cbp
) != 0) {
1123 libzfs_dataset_cmp(const void *a
, const void *b
)
1125 zfs_handle_t
**za
= (zfs_handle_t
**)a
;
1126 zfs_handle_t
**zb
= (zfs_handle_t
**)b
;
1127 char mounta
[MAXPATHLEN
];
1128 char mountb
[MAXPATHLEN
];
1129 boolean_t gota
, gotb
;
1131 if ((gota
= (zfs_get_type(*za
) == ZFS_TYPE_FILESYSTEM
)) != 0)
1132 verify(zfs_prop_get(*za
, ZFS_PROP_MOUNTPOINT
, mounta
,
1133 sizeof (mounta
), NULL
, NULL
, 0, B_FALSE
) == 0);
1134 if ((gotb
= (zfs_get_type(*zb
) == ZFS_TYPE_FILESYSTEM
)) != 0)
1135 verify(zfs_prop_get(*zb
, ZFS_PROP_MOUNTPOINT
, mountb
,
1136 sizeof (mountb
), NULL
, NULL
, 0, B_FALSE
) == 0);
1139 return (strcmp(mounta
, mountb
));
1146 return (strcmp(zfs_get_name(a
), zfs_get_name(b
)));
1150 * Mount and share all datasets within the given pool. This assumes that no
1151 * datasets within the pool are currently mounted. Because users can create
1152 * complicated nested hierarchies of mountpoints, we first gather all the
1153 * datasets and mountpoints within the pool, and sort them by mountpoint. Once
1154 * we have the list of all filesystems, we iterate over them in order and mount
1155 * and/or share each one.
1157 #pragma weak zpool_mount_datasets = zpool_enable_datasets
1159 zpool_enable_datasets(zpool_handle_t
*zhp
, const char *mntopts
, int flags
)
1161 get_all_cb_t cb
= { 0 };
1162 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1168 * Gather all non-snap datasets within the pool.
1170 if ((zfsp
= zfs_open(hdl
, zhp
->zpool_name
, ZFS_TYPE_DATASET
)) == NULL
)
1173 libzfs_add_handle(&cb
, zfsp
);
1174 if (zfs_iter_filesystems(zfsp
, mount_cb
, &cb
) != 0)
1177 * Sort the datasets by mountpoint.
1179 qsort(cb
.cb_handles
, cb
.cb_used
, sizeof (void *),
1180 libzfs_dataset_cmp
);
1183 * And mount all the datasets, keeping track of which ones
1184 * succeeded or failed.
1186 if ((good
= zfs_alloc(zhp
->zpool_hdl
,
1187 cb
.cb_used
* sizeof (int))) == NULL
)
1191 for (i
= 0; i
< cb
.cb_used
; i
++) {
1192 if (zfs_mount(cb
.cb_handles
[i
], mntopts
, flags
) != 0)
1199 * Then share all the ones that need to be shared. This needs
1200 * to be a separate pass in order to avoid excessive reloading
1201 * of the configuration. Good should never be NULL since
1202 * zfs_alloc is supposed to exit if memory isn't available.
1204 for (i
= 0; i
< cb
.cb_used
; i
++) {
1205 if (good
[i
] && zfs_share(cb
.cb_handles
[i
]) != 0)
1212 for (i
= 0; i
< cb
.cb_used
; i
++)
1213 zfs_close(cb
.cb_handles
[i
]);
1214 free(cb
.cb_handles
);
1220 mountpoint_compare(const void *a
, const void *b
)
1222 const char *mounta
= *((char **)a
);
1223 const char *mountb
= *((char **)b
);
1225 return (strcmp(mountb
, mounta
));
1228 /* alias for 2002/240 */
1229 #pragma weak zpool_unmount_datasets = zpool_disable_datasets
1231 * Unshare and unmount all datasets within the given pool. We don't want to
1232 * rely on traversing the DSL to discover the filesystems within the pool,
1233 * because this may be expensive (if not all of them are mounted), and can fail
1234 * arbitrarily (on I/O error, for example). Instead, we walk /etc/mnttab and
1235 * gather all the filesystems that are currently mounted.
1238 zpool_disable_datasets(zpool_handle_t
*zhp
, boolean_t force
)
1241 struct mnttab entry
;
1243 char **mountpoints
= NULL
;
1244 zfs_handle_t
**datasets
= NULL
;
1245 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1248 int flags
= (force
? MS_FORCE
: 0);
1249 sa_init_selective_arg_t sharearg
;
1251 namelen
= strlen(zhp
->zpool_name
);
1253 rewind(hdl
->libzfs_mnttab
);
1255 while (getmntent(hdl
->libzfs_mnttab
, &entry
) == 0) {
1257 * Ignore non-ZFS entries.
1259 if (entry
.mnt_fstype
== NULL
||
1260 strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
1264 * Ignore filesystems not within this pool.
1266 if (entry
.mnt_mountp
== NULL
||
1267 strncmp(entry
.mnt_special
, zhp
->zpool_name
, namelen
) != 0 ||
1268 (entry
.mnt_special
[namelen
] != '/' &&
1269 entry
.mnt_special
[namelen
] != '\0'))
1273 * At this point we've found a filesystem within our pool. Add
1274 * it to our growing list.
1276 if (used
== alloc
) {
1278 if ((mountpoints
= zfs_alloc(hdl
,
1279 8 * sizeof (void *))) == NULL
)
1282 if ((datasets
= zfs_alloc(hdl
,
1283 8 * sizeof (void *))) == NULL
)
1290 if ((ptr
= zfs_realloc(hdl
, mountpoints
,
1291 alloc
* sizeof (void *),
1292 alloc
* 2 * sizeof (void *))) == NULL
)
1296 if ((ptr
= zfs_realloc(hdl
, datasets
,
1297 alloc
* sizeof (void *),
1298 alloc
* 2 * sizeof (void *))) == NULL
)
1306 if ((mountpoints
[used
] = zfs_strdup(hdl
,
1307 entry
.mnt_mountp
)) == NULL
)
1311 * This is allowed to fail, in case there is some I/O error. It
1312 * is only used to determine if we need to remove the underlying
1313 * mountpoint, so failure is not fatal.
1315 datasets
[used
] = make_dataset_handle(hdl
, entry
.mnt_special
);
1321 * At this point, we have the entire list of filesystems, so sort it by
1324 sharearg
.zhandle_arr
= datasets
;
1325 sharearg
.zhandle_len
= used
;
1326 ret
= zfs_init_libshare_arg(hdl
, SA_INIT_SHARE_API_SELECTIVE
,
1330 qsort(mountpoints
, used
, sizeof (char *), mountpoint_compare
);
1333 * Walk through and first unshare everything.
1335 for (i
= 0; i
< used
; i
++) {
1336 zfs_share_proto_t
*curr_proto
;
1337 for (curr_proto
= share_all_proto
; *curr_proto
!= PROTO_END
;
1339 if (is_shared(hdl
, mountpoints
[i
], *curr_proto
) &&
1340 unshare_one(hdl
, mountpoints
[i
],
1341 mountpoints
[i
], *curr_proto
) != 0)
1347 * Now unmount everything, removing the underlying directories as
1350 for (i
= 0; i
< used
; i
++) {
1351 if (unmount_one(hdl
, mountpoints
[i
], flags
) != 0)
1355 for (i
= 0; i
< used
; i
++) {
1357 remove_mountpoint(datasets
[i
]);
1362 for (i
= 0; i
< used
; i
++) {
1364 zfs_close(datasets
[i
]);
1365 free(mountpoints
[i
]);