libzfs_mount.c revision 260921a4509dd7666c6613f413be409b66181642
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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 main entry points are:
*
* zfs_is_mounted()
* zfs_mount()
* zfs_unmount()
* zfs_unmountall()
*
* These functions are used by mount and unmount, and when changing a
* filesystem's mountpoint. This file also contains the functions used to
* manage sharing filesystems via NFS:
*
* zfs_is_shared()
* zfs_share()
* zfs_unshare()
* zfs_unshareall()
*/
#include <dirent.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"
/*
* The following two files are opened as part of zfs_init(). It's OK to for
* the sharetab to be NULL, but mnttab must always be non-NULL;
*/
/*
* Search the sharetab for the given mountpoint, returning TRUE if it is found.
*/
static int
is_shared(const char *mountpoint)
{
if (sharetab_file == NULL)
return (0);
/* the mountpoint is the first entry on each line */
*tab = '\0';
return (1);
}
}
return (0);
}
/*
* 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 int
dir_is_empty(const char *dirname)
{
return (TRUE);
continue;
return (FALSE);
}
return (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.
*/
int
{
/*
* mountpoint, as we can just search for the special device. This will
* also let us find mounts when the mountpoint is 'legacy'.
*/
return (FALSE);
return (TRUE);
}
/*
* Mount the given filesystem.
*/
int
{
char mountpoint[ZFS_MAXPROPLEN];
char mntopts[MNT_LINE_MAX];
mntopts[0] = '\0';
else
/* ignore non-filesystems */
return (0);
/* return success if there is no mountpoint set */
return (0);
/*
* If the 'zoned' property is set, and we're in the global zone, simply
* return success.
*/
char zonename[ZONENAME_MAX];
sizeof (zonename)) < 0) {
"cannot determine current zone"));
return (1);
}
return (0);
}
/* Create the directory if it doesn't already exist */
"unable to create mountpoint"), mountpoint);
return (1);
}
}
/*
* 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"), mountpoint);
"allow this behavior, or use the -O flag"));
return (1);
}
/* 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.
*/
switch (errno) {
case EBUSY:
break;
case EPERM:
case EACCES:
break;
default:
"cannot mount '%s': %s"),
break;
}
return (1);
}
return (0);
}
/*
* Unmount the given filesystem.
*/
int
{
/* check to see if need to unmount the filesystem */
if (mountpoint == NULL)
/*
* Always unshare the filesystem first.
*/
return (-1);
/*
* Try to unmount the filesystem. There is no reason to try a
* forced unmount because the vnodes will still carry a
* reference to the underlying dataset, so we can't destroy it
* anyway.
*
* In the unmount case, we print out a slightly more informative
* error message, though we'll be relying on the poor error
* semantics from the kernel.
*/
"cannot unmount '%s': %s"),
return (-1);
}
/*
* Don't actually destroy the underlying directory
*/
}
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);
}
/*
* Check to see if the filesystem is currently shared.
*/
int
{
char *mountpoint;
return (FALSE);
if (is_shared(mountpoint)) {
*where = mountpoint;
else
return (TRUE);
} else {
return (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];
/* ignore non-filesystems */
return (0);
/* return success if there is no mountpoint set */
return (0);
/* return success if there are no share options */
return (0);
/*
* If the 'zoned' property is set, simply return success since:
* 1. in a global zone, a dataset should not be shared if it's
* managed in a local zone.
* 2. in a local zone, NFS server is not available.
*/
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,
return (-1);
}
/*
* 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.
*/
"'%s': share(1M) failed"),
zfs_get_name(zhp));
else
colon + 2);
return (-1);
}
return (0);
}
/*
* Unshare the given filesystem.
*/
int
{
char buf[MAXPATHLEN];
/* check to see if need to unmount the filesystem */
if (mountpoint == NULL)
if (is_shared(mountpoint)) {
"unshare '%s': unshare(1M) failed"),
zfs_get_name(zhp));
return (-1);
}
/*
* 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': unshare(1M) "
else
"cannot unshare '%s': %s"),
return (-1);
}
}
}
return (0);
}
/*
* Same as zfs_unmountall(), but for 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];
char source[ZFS_MAXNAMELEN];
char zonename[ZONENAME_MAX];
/* ignore non-filesystems */
FALSE) != 0)
return;
"cannot determine current zone"));
(sourcetype == ZFS_SRC_DEFAULT ||
sourcetype == ZFS_SRC_INHERITED) &&
/*
* Try to remove the directory, silently ignoring any errors.
* The filesystem may have since been removed or moved around,
* and this isn't really useful to the administrator in any
* way.
*/
(void) rmdir(mountpoint);
}
}