libzfs_mount.c revision 57b448de658d89a2c88a001d58073c46ed2180f3
/*
* 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 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* 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_share_nfs()
* zfs_unshare_nfs()
* zfs_unshareall_nfs()
* zfs_is_shared_iscsi()
* zfs_share_iscsi()
* zfs_unshare_iscsi()
*
* 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 <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>
static int (*iscsitgt_zfs_share)(const char *);
static int (*iscsitgt_zfs_unshare)(const char *);
static int (*iscsitgt_zfs_is_shared)(const char *);
#pragma init(zfs_iscsi_init)
static void
zfs_iscsi_init(void)
{
void *libiscsitgt;
"iscsitgt_zfs_share")) == NULL ||
"iscsitgt_zfs_unshare")) == NULL ||
"iscsitgt_zfs_is_shared")) == NULL) {
}
}
/*
* Search the sharetab for the given mountpoint, returning true if it is found.
*/
static boolean_t
{
return (0);
/* the mountpoint is the first entry on each line */
*tab = '\0';
return (B_TRUE);
}
}
return (B_FALSE);
}
/*
* 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
dir_is_empty(const char *dirname)
{
return (B_TRUE);
continue;
return (B_FALSE);
}
return (B_TRUE);
}
/*
* 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.
*/
{
/*
* mountpoint, as we can just search for the special device. This will
* also let us find mounts when the mountpoint is 'legacy'.
*/
return (B_FALSE);
return (B_TRUE);
}
{
}
/*
* Returns true if the given dataset is mountable, false otherwise. Returns the
* mountpoint in 'buf'.
*/
static boolean_t
{
char sourceloc[ZFS_MAXNAMELEN];
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
{
char mountpoint[ZFS_MAXPROPLEN];
char mntopts[MNT_LINE_MAX];
mntopts[0] = '\0';
else
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"));
} else {
}
}
return (0);
}
/*
* Unmount a single filesystem.
*/
static int
{
mountpoint));
}
return (0);
}
/*
* Unmount the given filesystem.
*/
int
{
/* check to see if need to unmount the filesystem */
/*
* mountpoint may have come from a call to
* we know it comes from getmntany which can then get
* overwritten later. We strdup it to play it safe.
*/
if (mountpoint == NULL)
else
/*
* Unshare and unmount the filesystem
*/
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 (zfs_is_shared_iscsi(zhp));
}
int
{
if (ZFS_IS_VOLUME(zhp))
return (zfs_share_iscsi(zhp));
return (zfs_share_nfs(zhp));
}
int
{
if (ZFS_IS_VOLUME(zhp))
return (zfs_unshare_iscsi(zhp));
}
/*
* Check to see if the filesystem is currently shared.
*/
{
char *mountpoint;
return (B_FALSE);
*where = mountpoint;
else
return (B_TRUE);
} else {
return (B_FALSE);
}
}
/*
* 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 sa_handle_t (*_sa_init)(int);
static void (*_sa_fini)(sa_handle_t);
static int (*_sa_enable_share)(sa_share_t, char *);
static int (*_sa_disable_share)(sa_share_t, char *);
static char *(*_sa_errorstr)(int);
static int (*_sa_parse_legacy_options)(sa_group_t, 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;
char path[MAXPATHLEN];
#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_parse_legacy_options == 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
{
ret = SA_CONFIG_ERR;
ret = SA_NO_MEMORY;
return (ret);
}
/*
* 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
{
int ret;
if (_sa_parse_legacy_options != NULL)
else
ret = SA_CONFIG_ERR;
return (ret);
}
/*
* 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 'sharenfs'. We rely
* on "libshare" to the dirty work for us.
*/
int
{
char mountpoint[ZFS_MAXPROPLEN];
char shareopts[ZFS_MAXPROPLEN];
int ret;
return (0);
/*
* Return success if there are no share options.
*/
return (0);
/*
* 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.
*/
return (0);
return (-1);
}
int err;
zfs_get_name(zhp));
return (-1);
}
} else {
zfs_get_name(zhp));
return (-1);
}
return (0);
}
/*
* Unshare a filesystem by mountpoint.
*/
static int
{
int err;
char *mntpt;
/*
* Mountpoint could get trashed if libshare calls getmntany
* which id 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)
return (-1);
}
}
return (0);
}
/*
* Same as zfs_unmountall(), but for NFS unshares.
*/
int
{
int ret;
return (-1);
return (ret);
}
/*
* 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
{
char mountpoint[ZFS_MAXPROPLEN];
&source))
return;
if (source == ZFS_SRC_DEFAULT ||
source == ZFS_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);
}
}
{
return (iscsitgt_zfs_is_shared != NULL &&
}
int
{
char shareopts[ZFS_MAXPROPLEN];
/*
* Return success if there are no share options.
*/
return (0);
return (0);
}
int
{
/*
* Return if the volume is not shared
*/
if (!zfs_is_shared_iscsi(zhp))
return (0);
/*
* If this fails with ENODEV it indicates that zvol wasn't shared so
* we should return success in that case.
*/
if (iscsitgt_zfs_unshare == NULL ||
return (0);
}
typedef struct mount_cbdata {
int cb_used;
int cb_alloc;
static int
{
return (0);
}
void *ptr;
return (-1);
}
}
static int
dataset_cmp(const void *a, const void *b)
{
char mounta[MAXPATHLEN];
char mountb[MAXPATHLEN];
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
{
mount_cbdata_t cb = { 0 };
int i, ret = -1;
int *good;
/*
* Gather all datasets within the pool.
*/
return (-1);
goto out;
goto out;
/*
* Sort the datasets by mountpoint.
*/
/*
* And mount all the datasets, keeping track of which ones
* succeeded or failed. By using zfs_alloc(), the good pointer
* will always be non-NULL.
*/
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
{
/*
* Ignore snapshots and ignore failures from non-existant datasets.
*/
return (0);
(void) zfs_unshare_iscsi(zhp);
return (0);
}
static int
mountpoint_compare(const void *a, const void *b)
{
const char *mounta = *((char **)a);
const char *mountb = *((char **)b);
}
/*
* 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
{
char **mountpoints = NULL;
int i;
int ret = -1;
/*
* First unshare all zvols.
*/
return (-1);
/*
* 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++) {
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);
}