/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2015 Nexenta Systems, Inc. All rights reserved.
* Copyright (c) 2014, 2016 by Delphix. All rights reserved.
* Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
*/
/*
* Routines to manage ZFS mounts. We separate all the nasty routines that have
* to deal with the OS. The following functions are the main entry points --
* they are used by mount and unmount and when changing a filesystem's
* mountpoint.
*
* zfs_is_mounted()
* zfs_mount()
* zfs_unmount()
* zfs_unmountall()
*
* This file also contains the functions used to manage sharing filesystems via
* NFS and iSCSI:
*
* zfs_is_shared()
* zfs_share()
* zfs_unshare()
*
* zfs_is_shared_nfs()
* zfs_is_shared_smb()
* zfs_share_proto()
* zfs_shareall();
* zfs_unshare_nfs()
* zfs_unshare_smb()
* zfs_unshareall_nfs()
* zfs_unshareall_smb()
* zfs_unshareall()
* zfs_unshareall_bypath()
*
* The following functions are available for pool consumers, and will
*
* zpool_enable_datasets()
* zpool_disable_datasets()
*/
#include <dirent.h>
#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <libintl.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <zone.h>
#include <libzfs.h>
#include "libzfs_impl.h"
#include <libshare.h>
#include <sys/systeminfo.h>
/*
* The share protocols table must be in the same order as the zfs_share_prot_t
* enum in libzfs_impl.h
*/
typedef struct {
char *p_name;
int p_share_err;
int p_unshare_err;
};
};
};
};
/*
* Search the sharetab for the given mountpoint and protocol, returning
* a zfs_share_type_t value.
*/
static zfs_share_type_t
{
char *ptr;
return (SHARED_NOT_SHARED);
/* the mountpoint is the first entry on each line */
continue;
*tab = '\0';
/*
* the protocol field is the third field
* skip over second field
*/
continue;
continue;
*tab = '\0';
switch (proto) {
case PROTO_NFS:
return (SHARED_NFS);
case PROTO_SMB:
return (SHARED_SMB);
default:
return (0);
}
}
}
}
return (SHARED_NOT_SHARED);
}
static boolean_t
{
/*
* We only want to return false if the given path is a non empty
* directory, all other errors are handled elsewhere.
*/
return (B_TRUE);
}
/*
* An empty directory will still have two entries in it, one
* entry for each of "." and "..".
*/
return (B_FALSE);
}
return (B_TRUE);
}
static boolean_t
{
int dirfd;
return (B_TRUE);
}
return (B_TRUE);
}
continue;
return (B_FALSE);
}
return (B_TRUE);
}
/*
* Returns true if the specified directory is empty. If we can't open the
* directory at all, return true so that the mount can fail with a more
* informative error message.
*/
static boolean_t
{
/*
* If the statvfs call fails or the filesystem is not a ZFS
* filesystem, fall back to the slow path which uses readdir.
*/
return (dir_is_empty_readdir(dirname));
}
/*
* At this point, we know the provided path is on a ZFS
* filesystem, so we can use stat instead of readdir to
* determine if the directory is empty or not. We try to avoid
* using readdir because that requires opening "dirname"; this
* open file descriptor can potentially end up in a child
* process if there's a concurrent fork, thus preventing the
* zfs_mount() from otherwise succeeding (the open file
* descriptor inherited by the child process will cause the
* parent's mount to fail with EBUSY). The performance
* implications of replacing the open, read, and close with a
* single stat is nice; but is not the main motivation for the
* added complexity.
*/
return (dir_is_empty_stat(dirname));
}
/*
* Checks to see if the mount is active. If the filesystem is mounted, we fill
* in 'where' with the current mountpoint, and return 1. Otherwise, we return
* 0.
*/
{
return (B_FALSE);
return (B_TRUE);
}
{
}
/*
* Returns true if the given dataset is mountable, false otherwise. Returns the
* mountpoint in 'buf'.
*/
static boolean_t
{
return (B_FALSE);
return (B_FALSE);
return (B_FALSE);
getzoneid() == GLOBAL_ZONEID)
return (B_FALSE);
if (source)
*source = sourcetype;
return (B_TRUE);
}
/*
* Mount the given filesystem.
*/
int
{
mntopts[0] = '\0';
else
/*
* If the pool is imported read-only then all mounts must be read-only
*/
return (0);
/* Create the directory if it doesn't already exist */
"failed to create mountpoint"));
mountpoint));
}
}
/*
* Determine if the mountpoint is empty. If so, refuse to perform the
* mount. We don't perform this check if MS_OVERLAY is specified, which
* would defeat the point. We also avoid this check if 'remount' is
* specified.
*/
if ((flags & MS_OVERLAY) == 0 &&
!dir_is_empty(mountpoint)) {
"directory is not empty"));
}
/* perform the mount */
/*
* Generic errors are nasty, but there are just way too many
* from mount(), and they're well-understood. We pick a few
* common ones to improve upon.
*/
"mountpoint or dataset is busy"));
"Insufficient privileges"));
int spa_version;
"file system on a version %d pool. Pool must be"
" upgraded to mount this file system."),
} else {
}
}
/* add the mounted entry into our cache */
mntopts);
return (0);
}
/*
* Unmount a single filesystem.
*/
static int
{
mountpoint));
}
return (0);
}
/*
* Unmount the given filesystem.
*/
int
{
/* check to see if we need to unmount the filesystem */
/*
* mountpoint may have come from a call to
* we know it comes from libzfs_mnttab_find which can
* then get freed later. We strdup it to play it safe.
*/
if (mountpoint == NULL)
else
/*
* Unshare and unmount the filesystem
*/
return (-1);
(void) zfs_shareall(zhp);
return (-1);
}
}
return (0);
}
/*
* Unmount this filesystem and any children inheriting the mountpoint property.
* To do this, just act like we're changing the mountpoint property, but don't
* remount the filesystems afterwards.
*/
int
{
int ret;
return (-1);
return (ret);
}
{
if (ZFS_IS_VOLUME(zhp))
return (B_FALSE);
curr_proto++)
}
int
{
}
int
{
return (zfs_unshareall(zhp));
}
/*
* Check to see if the filesystem is currently shared.
*/
{
char *mountpoint;
return (SHARED_NOT_SHARED);
!= SHARED_NOT_SHARED) {
*where = mountpoint;
else
return (rc);
} else {
return (SHARED_NOT_SHARED);
}
}
{
PROTO_NFS) != SHARED_NOT_SHARED);
}
{
PROTO_SMB) != SHARED_NOT_SHARED);
}
/*
* Make sure things will work if libshare isn't installed by using
* wrapper functions that check to see that the pointers to functions
* initialized in _zfs_init_libshare() are actually present.
*/
static char *(*_sa_errorstr)(int);
char *, char *, zprop_source_t, char *, char *, char *);
/*
* _zfs_init_libshare()
*
* Find the libshare.so.1 entry points that we use here and save the
* values to be used later. This is triggered by the runtime loader.
* Make sure the correct ISA version is loaded.
*/
#pragma init(_zfs_init_libshare)
static void
_zfs_init_libshare(void)
{
void *libshare;
#if defined(_LP64)
isa[0] = '\0';
#else
isa[0] = '\0';
#endif
"/usr/lib/%s/libshare.so.1", isa);
"sa_enable_share");
"sa_disable_share");
_sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *))
sa_share_t, char *, char *, zprop_source_t, char *,
_sa_update_sharetab_ts = (void (*)(sa_handle_t))
_sa_parse_legacy_options == NULL ||
_sa_zfs_process_share == NULL ||
_sa_update_sharetab_ts == NULL) {
_sa_errorstr = NULL;
}
}
}
/*
* zfs_init_libshare(zhandle, service)
*
* Initialize the libshare API if it hasn't already been initialized.
* In all cases it returns 0 if it succeeded and an error if not. The
* service value is which part(s) of the API to initialize and is a
* direct map to the libshare sa_init(service) interface.
*/
int
{
return (SA_CONFIG_ERR);
/*
* Attempt to refresh libshare. This is necessary if there was a cache
* miss for a new ZFS dataset that was just created, or if state of the
* sharetab file has changed since libshare was last initialized. We
* want to make sure so check timestamps to see if a different process
* has updated any of the configuration. If there was some non-ZFS
* change, we need to re-initialize the internal cache.
*/
if (_sa_needs_refresh != NULL &&
}
return (SA_NO_MEMORY);
return (SA_OK);
}
/*
* zfs_uninit_libshare(zhandle)
*
* Uninitialize the libshare API if it hasn't already been
* uninitialized. It is OK to call multiple times.
*/
void
{
}
}
/*
* zfs_parse_options(options, proto)
*
* Call the legacy parse interface to get the protocol specific
* options using the NULL arg to indicate that this is a "parse" only.
*/
int
{
if (_sa_parse_legacy_options != NULL) {
}
return (SA_CONFIG_ERR);
}
/*
* zfs_sa_find_share(handle, path)
*
* wrapper around sa_find_share to find a share path in the
* configuration.
*/
static sa_share_t
{
if (_sa_find_share != NULL)
return (NULL);
}
/*
* zfs_sa_enable_share(share, proto)
*
* Wrapper for sa_enable_share which enables a share for a specified
* protocol.
*/
static int
{
if (_sa_enable_share != NULL)
return (SA_CONFIG_ERR);
}
/*
* zfs_sa_disable_share(share, proto)
*
* Wrapper for sa_enable_share which disables a share for a specified
* protocol.
*/
static int
{
if (_sa_disable_share != NULL)
return (SA_CONFIG_ERR);
}
/*
* Share the given filesystem according to the options in the specified
* protocol specific properties (sharenfs, sharesmb). We rely
* on "libshare" to the dirty work for us.
*/
static int
{
int ret;
return (0);
/*
* Return success if there are no share options.
*/
ZFS_MAXPROPLEN, B_FALSE) != 0 ||
continue;
return (-1);
}
/*
* If the 'zoned' property is set, then zfs_is_mountable()
* will have already bailed out if we are in the global zone.
* But local zones cannot be NFS servers, so we ignore it for
* local zones as well.
*/
continue;
/*
* This may be a new file system that was just
* created so isn't in the internal cache
* (second time through). Rather than
* reloading the entire configuration, we can
* assume ZFS has done the checking and it is
* safe to add this to the internal
* configuration.
*/
(void) zfs_error_fmt(hdl,
zfs_get_name(zhp));
return (-1);
}
}
int err;
(void) zfs_error_fmt(hdl,
zfs_get_name(zhp));
return (-1);
}
} else {
(void) zfs_error_fmt(hdl,
zfs_get_name(zhp));
return (-1);
}
}
return (0);
}
int
{
}
int
{
}
int
{
}
/*
* Unshare a filesystem by mountpoint.
*/
static int
{
int err;
char *mntpt;
/*
* Mountpoint could get trashed if libshare calls getmntany
* which it does during API initialization, so strdup the
* value.
*/
/* make sure libshare initialized */
}
}
} else {
name));
}
return (0);
}
/*
* Unshare the given filesystem.
*/
int
{
/* check to see if need to unmount the filesystem */
if (mountpoint != NULL)
if (mountpoint == NULL)
curr_proto++) {
mntpt, *curr_proto) != 0) {
return (-1);
}
}
}
return (0);
}
int
{
}
int
{
}
/*
* Same as zfs_unmountall(), but for NFS and SMB unshares.
*/
int
{
int ret;
return (-1);
return (ret);
}
int
{
}
int
{
}
int
{
}
int
{
}
/*
* Remove the mountpoint associated with the current dataset, if necessary.
* We only remove the underlying directory if:
*
* - The mountpoint is not 'none' or 'legacy'
* - The mountpoint is non-empty
* - The mountpoint is the default or inherited
* - The 'zoned' property is set, or we're in a local zone
*
* Any other directories we leave alone.
*/
void
{
&source))
return;
if (source == ZPROP_SRC_DEFAULT ||
source == ZPROP_SRC_INHERITED) {
/*
* Try to remove the directory, silently ignoring any errors.
* The filesystem may have since been removed or moved around,
* and this error isn't really useful to the administrator in
* any way.
*/
(void) rmdir(mountpoint);
}
}
void
{
void *ptr;
newsz * sizeof (void *));
}
}
static int
{
return (0);
}
return (0);
}
/*
* If this filesystem is inconsistent and has a receive resume
* token, we can not mount it.
*/
return (0);
}
return (-1);
}
return (0);
}
int
libzfs_dataset_cmp(const void *a, const void *b)
{
if (gota)
return (-1);
if (gotb)
return (1);
}
/*
* Mount and share all datasets within the given pool. This assumes that no
* datasets within the pool are currently mounted. Because users can create
* complicated nested hierarchies of mountpoints, we first gather all the
* datasets and mountpoints within the pool, and sort them by mountpoint. Once
* we have the list of all filesystems, we iterate over them in order and mount
*/
int
{
int *good;
/*
* Gather all non-snap datasets within the pool.
*/
goto out;
goto out;
/*
* Sort the datasets by mountpoint.
*/
/*
* And mount all the datasets, keeping track of which ones
* succeeded or failed.
*/
goto out;
ret = 0;
ret = -1;
else
good[i] = 1;
}
/*
* Then share all the ones that need to be shared. This needs
* to be a separate pass in order to avoid excessive reloading
* of the configuration. Good should never be NULL since
* zfs_alloc is supposed to exit if memory isn't available.
*/
ret = -1;
}
out:
return (ret);
}
static int
mountpoint_compare(const void *a, const void *b)
{
const char *mounta = *((char **)a);
const char *mountb = *((char **)b);
}
/* alias for 2002/240 */
/*
* Unshare and unmount all datasets within the given pool. We don't want to
* rely on traversing the DSL to discover the filesystems within the pool,
* because this may be expensive (if not all of them are mounted), and can fail
* gather all the filesystems that are currently mounted.
*/
int
{
int i;
/*
* Ignore non-ZFS entries.
*/
continue;
/*
* Ignore filesystems not within this pool.
*/
continue;
/*
* At this point we've found a filesystem within our pool. Add
* it to our growing list.
*/
if (alloc == 0) {
8 * sizeof (void *))) == NULL)
goto out;
8 * sizeof (void *))) == NULL)
goto out;
alloc = 8;
} else {
void *ptr;
alloc * sizeof (void *),
goto out;
mountpoints = ptr;
alloc * sizeof (void *),
goto out;
alloc *= 2;
}
}
goto out;
/*
* This is allowed to fail, in case there is some I/O error. It
* is only used to determine if we need to remove the underlying
* mountpoint, so failure is not fatal.
*/
used++;
}
/*
* At this point, we have the entire list of filesystems, so sort it by
* mountpoint.
*/
/*
* Walk through and first unshare everything.
*/
for (i = 0; i < used; i++) {
curr_proto++) {
mountpoints[i], *curr_proto) != 0)
goto out;
}
}
/*
* Now unmount everything, removing the underlying directories as
* appropriate.
*/
for (i = 0; i < used; i++) {
goto out;
}
for (i = 0; i < used; i++) {
if (datasets[i])
}
ret = 0;
out:
for (i = 0; i < used; i++) {
if (datasets[i])
free(mountpoints[i]);
}
return (ret);
}