Apply the NFS exports list rototill patch:
- Remove all NFS related stuff from file system specific code.
- Drop the vfs_checkexp hook and generalize it in the new nfs_check_export
function, thus removing redundancy from all file systems.
- Move all NFS export-related stuff from kern/vfs_subr.c to the new
file sys/nfs/nfs_export.c. The former was becoming large and its code
is always compiled, regardless of the build options. Using the latter,
the code is only compiled in when NFSSERVER is enabled. While doing this,
also make some functions in nfs_subs.c conditional to NFSSERVER.
- Add a new command in nfssvc(2), called NFSSVC_SETEXPORTSLIST, that takes a
path and a set of export entries. At the moment it can only clear the
exports list or append entries, one by one, but it is done in a way that
allows setting the whole set of entries atomically in the future (see the
comment in mountd_set_exports_list or in doc/TODO).
- Change mountd(8) to use the nfssvc(2) system call instead of mount(2) so
that it becomes file system agnostic. In fact, all this whole thing was
done to remove a 'XXX' block from this utility!
- Change the mount*, newfs and fsck* userland utilities to not deal with NFS
exports initialization; done internally by the kernel when initializing
the NFS support for each file system.
- Implement an interface for VFS (called VFS hooks) so that several kernel
subsystems can run arbitrary code upon receipt of specific VFS events.
At the moment, this only provides support for unmount and is used to
destroy NFS exports lists from the file systems being unmounted, though it
has room for extension.
Thanks go to yamt@, chs@, thorpej@, wrstuden@ and others for their comments
and advice in the development of this patch.
2005-09-23 16:10:31 +04:00
|
|
|
/* $NetBSD: mount_tmpfs.c,v 1.2 2005/09/23 12:10:35 jmmv Exp $ */
|
2005-09-10 23:20:48 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2005 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Julio M. Merino Vidal.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#ifndef lint
|
Apply the NFS exports list rototill patch:
- Remove all NFS related stuff from file system specific code.
- Drop the vfs_checkexp hook and generalize it in the new nfs_check_export
function, thus removing redundancy from all file systems.
- Move all NFS export-related stuff from kern/vfs_subr.c to the new
file sys/nfs/nfs_export.c. The former was becoming large and its code
is always compiled, regardless of the build options. Using the latter,
the code is only compiled in when NFSSERVER is enabled. While doing this,
also make some functions in nfs_subs.c conditional to NFSSERVER.
- Add a new command in nfssvc(2), called NFSSVC_SETEXPORTSLIST, that takes a
path and a set of export entries. At the moment it can only clear the
exports list or append entries, one by one, but it is done in a way that
allows setting the whole set of entries atomically in the future (see the
comment in mountd_set_exports_list or in doc/TODO).
- Change mountd(8) to use the nfssvc(2) system call instead of mount(2) so
that it becomes file system agnostic. In fact, all this whole thing was
done to remove a 'XXX' block from this utility!
- Change the mount*, newfs and fsck* userland utilities to not deal with NFS
exports initialization; done internally by the kernel when initializing
the NFS support for each file system.
- Implement an interface for VFS (called VFS hooks) so that several kernel
subsystems can run arbitrary code upon receipt of specific VFS events.
At the moment, this only provides support for unmount and is used to
destroy NFS exports lists from the file systems being unmounted, though it
has room for extension.
Thanks go to yamt@, chs@, thorpej@, wrstuden@ and others for their comments
and advice in the development of this patch.
2005-09-23 16:10:31 +04:00
|
|
|
__RCSID("$NetBSD: mount_tmpfs.c,v 1.2 2005/09/23 12:10:35 jmmv Exp $");
|
2005-09-10 23:20:48 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
|
|
|
|
#include <fs/tmpfs/tmpfs.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <mntopts.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static const struct mntopt mopts[] = {
|
|
|
|
MOPT_STDOPTS,
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static int mount_tmpfs(int argc, char **argv);
|
|
|
|
static void usage(void);
|
|
|
|
static int dehumanize_group(const char *str, gid_t *gid);
|
|
|
|
static int dehumanize_mode(const char *str, mode_t *mode);
|
|
|
|
static int dehumanize_off(const char *str, off_t *size);
|
|
|
|
static int dehumanize_user(const char *str, uid_t *uid);
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
int
|
|
|
|
mount_tmpfs(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char canon_dir[MAXPATHLEN];
|
|
|
|
int ch, mntflags;
|
|
|
|
off_t offtmp;
|
|
|
|
mntoptparse_t mo;
|
|
|
|
struct tmpfs_args args;
|
|
|
|
|
|
|
|
setprogname(argv[0]);
|
|
|
|
|
|
|
|
/* Set default values for mount point arguments. */
|
|
|
|
args.ta_version = TMPFS_ARGS_VERSION;
|
|
|
|
args.ta_size_max = 0;
|
|
|
|
args.ta_nodes_max = 0;
|
|
|
|
args.ta_root_uid = getuid();
|
|
|
|
args.ta_root_gid = getgid();
|
|
|
|
args.ta_root_mode = 0755;
|
|
|
|
mntflags = 0;
|
|
|
|
|
|
|
|
optind = optreset = 1;
|
|
|
|
while ((ch = getopt(argc, argv, "g:m:n:o:s:u:")) != -1 ) {
|
|
|
|
switch (ch) {
|
|
|
|
case 'g':
|
|
|
|
if (!dehumanize_group(optarg, &args.ta_root_gid)) {
|
|
|
|
errx(EXIT_FAILURE, "failed to parse group "
|
|
|
|
"'%s'", optarg);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'm':
|
|
|
|
if (!dehumanize_mode(optarg, &args.ta_root_mode)) {
|
|
|
|
errx(EXIT_FAILURE, "failed to parse mode "
|
|
|
|
"'%s'", optarg);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'n':
|
|
|
|
if (!dehumanize_off(optarg, &offtmp)) {
|
|
|
|
errx(EXIT_FAILURE, "failed to parse size "
|
|
|
|
"'%s'", optarg);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
args.ta_nodes_max = offtmp;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'o':
|
|
|
|
mo = getmntopts(optarg, mopts, &mntflags, 0);
|
|
|
|
freemntopts(mo);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
if (!dehumanize_off(optarg, &offtmp)) {
|
|
|
|
errx(EXIT_FAILURE, "failed to parse size "
|
|
|
|
"'%s'", optarg);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
args.ta_size_max = offtmp;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'u':
|
|
|
|
if (!dehumanize_user(optarg, &args.ta_root_uid)) {
|
|
|
|
errx(EXIT_FAILURE, "failed to parse user "
|
|
|
|
"'%s'", optarg);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc != 2) {
|
|
|
|
usage();
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (realpath(argv[1], canon_dir) == NULL) {
|
|
|
|
err(EXIT_FAILURE, "realpath %s", argv[0]);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strncmp(argv[1], canon_dir, MAXPATHLEN) != 0) {
|
|
|
|
warnx("\"%s\" is a relative path", argv[0]);
|
|
|
|
warnx("using \"%s\" instead", canon_dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mount(MOUNT_TMPFS, canon_dir, mntflags, &args)) {
|
|
|
|
err(EXIT_FAILURE, "tmpfs on %s", canon_dir);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
(void)fprintf(stderr,
|
|
|
|
"Usage: %s [-g group] [-m mode] [-n nodes] [-o options] [-s size]\n"
|
|
|
|
" [-u user] tmpfs mountpoint\n", getprogname());
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Obtains a GID number based on the contents of 'str'. If it is a string
|
|
|
|
* and is found in group(5), its corresponding ID is used. Otherwise, an
|
|
|
|
* attempt is made to convert the string to a number and use that as a GID.
|
|
|
|
* In case of success, true is returned and *gid holds the GID value.
|
|
|
|
* Otherwise, false is returned and *gid is untouched.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
dehumanize_group(const char *str, gid_t *gid)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
struct group *gr;
|
|
|
|
|
|
|
|
gr = getgrnam(str);
|
|
|
|
if (gr != NULL) {
|
|
|
|
*gid = gr->gr_gid;
|
|
|
|
error = 1;
|
|
|
|
} else {
|
|
|
|
char *ep;
|
|
|
|
long tmp;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
tmp = strtol(str, &ep, 10);
|
|
|
|
if (str[0] == '\0' || *ep != '\0')
|
|
|
|
error = 0; /* Not a number. */
|
|
|
|
else if (errno == ERANGE &&
|
|
|
|
(tmp == LONG_MAX || tmp == LONG_MIN))
|
|
|
|
error = 0; /* Out of range. */
|
|
|
|
else {
|
|
|
|
*gid = (gid_t)tmp;
|
|
|
|
error = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Obtains a mode number based on the contents of 'str'.
|
|
|
|
* In case of success, true is returned and *mode holds the mode value.
|
|
|
|
* Otherwise, false is returned and *mode is untouched.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
dehumanize_mode(const char *str, mode_t *mode)
|
|
|
|
{
|
|
|
|
char *ep;
|
|
|
|
int error;
|
|
|
|
long tmp;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
tmp = strtol(str, &ep, 8);
|
|
|
|
if (str[0] == '\0' || *ep != '\0')
|
|
|
|
error = 0; /* Not a number. */
|
|
|
|
else if (errno == ERANGE &&
|
|
|
|
(tmp == LONG_MAX || tmp == LONG_MIN))
|
|
|
|
error = 0; /* Out of range. */
|
|
|
|
else {
|
|
|
|
*mode = (mode_t)tmp;
|
|
|
|
error = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Converts the number given in 'str', which may be given in a humanized
|
|
|
|
* form (as described in humanize_number(3), but with some limitations),
|
|
|
|
* to a file size (off_t) without units.
|
|
|
|
* In case of success, true is returned and *size holds the value.
|
|
|
|
* Otherwise, false is returned and *size is untouched.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
dehumanize_off(const char *str, off_t *size)
|
|
|
|
{
|
|
|
|
char *ep, unit;
|
|
|
|
const char *delimit;
|
|
|
|
size_t len, multiplier, tmp;
|
|
|
|
|
|
|
|
len = strlen(str);
|
|
|
|
if (len < 1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
multiplier = 1;
|
|
|
|
|
|
|
|
unit = str[len - 1];
|
|
|
|
if (isalpha((int)unit)) {
|
|
|
|
switch (unit) {
|
|
|
|
case 'b':
|
|
|
|
multiplier = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'k':
|
|
|
|
multiplier = 1024;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'M':
|
|
|
|
multiplier = 1024 * 1024;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'G':
|
|
|
|
multiplier = 1024 * 1024 * 1024;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 0; /* Invalid suffix. */
|
|
|
|
}
|
|
|
|
|
|
|
|
delimit = &str[len - 1];
|
|
|
|
} else
|
|
|
|
delimit = NULL;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
tmp = strtol(str, &ep, 10);
|
|
|
|
if (str[0] == '\0' || (ep != delimit && *ep != '\0'))
|
|
|
|
return 0; /* Not a number. */
|
|
|
|
else if (errno == ERANGE &&
|
|
|
|
(tmp == LONG_MAX || tmp == LONG_MIN))
|
|
|
|
return 0; /* Out of range. */
|
|
|
|
|
|
|
|
*size = tmp * multiplier;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Obtains a UID number based on the contents of 'str'. If it is a string
|
|
|
|
* and is found in passwd(5), its corresponding ID is used. Otherwise, an
|
|
|
|
* attempt is made to convert the string to a number and use that as a UID.
|
|
|
|
* In case of success, true is returned and *uid holds the UID value.
|
|
|
|
* Otherwise, false is returned and *uid is untouched.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
dehumanize_user(const char *str, uid_t *uid)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
struct passwd *pw;
|
|
|
|
|
|
|
|
pw = getpwnam(str);
|
|
|
|
if (pw != NULL) {
|
|
|
|
*uid = pw->pw_uid;
|
|
|
|
error = 1;
|
|
|
|
} else {
|
|
|
|
char *ep;
|
|
|
|
long tmp;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
tmp = strtol(str, &ep, 10);
|
|
|
|
if (str[0] == '\0' || *ep != '\0')
|
|
|
|
error = 0; /* Not a number. */
|
|
|
|
else if (errno == ERANGE &&
|
|
|
|
(tmp == LONG_MAX || tmp == LONG_MIN))
|
|
|
|
error = 0; /* Out of range. */
|
|
|
|
else {
|
|
|
|
*uid = (uid_t)tmp;
|
|
|
|
error = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
#ifndef MOUNT_NOMAIN
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
|
|
|
|
return mount_tmpfs(argc, argv);
|
|
|
|
}
|
|
|
|
#endif
|