libzfs_mount.c revision d8d59944799a436698b0d88b5e6ae6d7d2422797
/*
* 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 2006 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"
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 */
if (mountpoint == NULL)
/*
* 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);
}
}
/*
* Share the given filesystem according to the options in 'sharenfs'. We rely
* on share(1M) to the dirty work for us.
*/
int
{
char mountpoint[ZFS_MAXPROPLEN];
char shareopts[ZFS_MAXPROPLEN];
char buf[MAXPATHLEN];
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);
/*
* Invoke the share(1M) command. We always do this, even if it's
* currently shared, as the options may have changed.
*/
"-F nfs \"%s\" 2>&1", mountpoint);
else
"-F nfs -o \"%s\" \"%s\" 2>&1", shareopts,
zfs_get_name(zhp)));
/*
* share(1M) should only produce output if there is some kind
* of error. All output begins with "share_nfs: ", so we trim
* this off to get to the real error.
*/
zfs_get_name(zhp));
return (-1);
}
return (0);
}
/*
* Unshare a filesystem by mountpoint.
*/
static int
{
char buf[MAXPATHLEN];
"cannot unshare '%s'"), name));
/*
* unshare(1M) should only produce output if there is
* some kind of error. All output begins with "unshare
* nfs: ", so we trim this off to get to the real error.
*/
"cannot unshare '%s'"), name));
}
return (0);
}
/*
* Unshare the given filesystem.
*/
int
{
/* check to see if need to unmount the filesystem */
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
{
/*
* 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;
/*
* Gather all datasets within the pool.
*/
return (-1);
goto out;
goto out;
/*
* Sort the datasets by mountpoint.
*/
/*
* And mount all the datasets.
*/
ret = 0;
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);
}