/*
* Copyright (c) 1997-2014 Erez Zadok
* Copyright (c) 1990 Jan-Simon Pendry
* Copyright (c) 1990 Imperial College of Science, Technology & Medicine
* Copyright (c) 1990 The Regents of the University of California.
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Jan-Simon Pendry at Imperial College, London.
*
* 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. Neither the name of the University 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 REGENTS 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 REGENTS 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.
*
*
* File: am-utils/libamu/mount_fs.c
*
*/
/*
* Do not define MNT2_NFS_OPT_* entries here! This is for generic
* mount(2) options only, not for NFS mount options. If you need to put
* something here, it's probably not the right place: see
* include/am_compat.h.
*/
{0, 0}
};
/* compute generic mount flags */
int
compute_mount_flags(mntent_t *mntp)
{
struct opt_tab *opt;
int flags = 0;
if (error < 0) {
plog(XLOG_ERROR, "'%s': mount: %m", mnt_dir);
/*
* The following code handles conditions which shouldn't
* occur. They are possible either because amd screws up
* in preparing for the mount, or because some human
* messed with the mount point. Both have been known to
* happen. -- stolcke 2/22/95
*/
if (errno == EBUSY) {
/*
* Also, sometimes unmount isn't called, e.g., because
* our mountlist is garbled. This leaves old mount
* points around which need to be removed before we
* can mount something new in their place.
*/
errno = umount_fs(mnt_dir, mnttabname, on_autofs);
if (errno != 0)
plog(XLOG_ERROR, "'%s': umount: %m", mnt_dir);
else {
plog(XLOG_WARNING, "extra umount required for '%s'", mnt_dir);
error = MOUNT_TRAP(type, mnt, flags, mnt_data);
}
}
}
/*
* Compute all NFS attribute cache related flags separately. Note that this
* function now computes attribute-cache flags for both Amd's automount
* points (NFS) as well as any normal NFS mount that Amd performs. Edit
* with caution.
*/
static void
compute_nfs_attrcache_flags(struct nfs_common_args *nap, mntent_t *mntp)
{
int acval = 0;
int err_acval = 1; /* 1 means we found no 'actimeo' value */
#if defined(HAVE_NFS_ARGS_T_ACREGMIN) || defined(HAVE_NFS_ARGS_T_ACREGMAX) || defined(HAVE_NFS_ARGS_T_ACDIRMIN) || defined(HAVE_NFS_ARGS_T_ACDIRMAX)
int err_acrdmm; /* for ac{reg,dir}{min,max} */
#endif /* HAVE_NFS_ARGS_T_AC{REG,DIR}{MIN,MAX} */
/************************************************************************/
/*** ATTRIBUTE CACHES ***/
/************************************************************************/
/*
* acval is set to 0 at the top of the function. If actimeo mount option
* exists and defined in mntopts, then its acval is set to it.
* If the value is non-zero, then we set all attribute cache fields to it.
* If acval is zero, it means it was never defined in mntopts or the
* actimeo mount option does not exist, in which case we check for
* individual mount options per attribute cache.
* Regardless of the value of acval, mount flags are set based directly
* on the values of the attribute caches.
*/
#ifdef MNTTAB_OPT_ACTIMEO
err_acval = hasmntvalerr(mntp, MNTTAB_OPT_ACTIMEO, &acval); /* attr cache timeout (sec) */
#endif /* MNTTAB_OPT_ACTIMEO */
/*** acregmin ***/
#ifdef HAVE_NFS_ARGS_T_ACREGMIN
err_acrdmm = 1; /* 1 means we found no acregmin value */
if (!err_acval) {
nap->acregmin = acval; /* min ac timeout for reg files (sec) */
} else {
# ifdef MNTTAB_OPT_ACREGMIN
int tmp;
err_acrdmm = hasmntvalerr(mntp, MNTTAB_OPT_ACREGMIN, &tmp);
nap->acregmin = tmp;
# else /* not MNTTAB_OPT_ACREGMIN */
nap->acregmin = 0;
# endif /* not MNTTAB_OPT_ACREGMIN */
}
/* set this flag iff we changed acregmin (possibly to zero) */
# ifdef MNT2_NFS_OPT_ACREGMIN
if (!err_acval || !err_acrdmm)
nap->flags |= MNT2_NFS_OPT_ACREGMIN;
# endif /* MNT2_NFS_OPT_ACREGMIN */
#endif /* HAVE_NFS_ARGS_T_ACREGMIN */
/*** acregmax ***/
#ifdef HAVE_NFS_ARGS_T_ACREGMAX
err_acrdmm = 1; /* 1 means we found no acregmax value */
if (!err_acval) {
nap->acregmax = acval; /* max ac timeout for reg files (sec) */
} else {
# ifdef MNTTAB_OPT_ACREGMAX
int tmp;
err_acrdmm = hasmntvalerr(mntp, MNTTAB_OPT_ACREGMAX, &tmp);
nap->acregmax = tmp;
# else /* not MNTTAB_OPT_ACREGMAX */
nap->acregmax = 0;
# endif /* not MNTTAB_OPT_ACREGMAX */
}
/* set this flag iff we changed acregmax (possibly to zero) */
# ifdef MNT2_NFS_OPT_ACREGMAX
if (!err_acval || !err_acrdmm)
nap->flags |= MNT2_NFS_OPT_ACREGMAX;
# endif /* MNT2_NFS_OPT_ACREGMAX */
#endif /* HAVE_NFS_ARGS_T_ACREGMAX */
/*** acdirmin ***/
#ifdef HAVE_NFS_ARGS_T_ACDIRMIN
err_acrdmm = 1; /* 1 means we found no acdirmin value */
if (!err_acval) {
nap->acdirmin = acval; /* min ac timeout for dirs (sec) */
} else {
# ifdef MNTTAB_OPT_ACDIRMIN
int tmp;
err_acrdmm = hasmntvalerr(mntp, MNTTAB_OPT_ACDIRMIN, &tmp);
nap->acdirmin = tmp;
# else /* not MNTTAB_OPT_ACDIRMIN */
nap->acdirmin = 0;
# endif /* not MNTTAB_OPT_ACDIRMIN */
}
/* set this flag iff we changed acdirmin (possibly to zero) */
# ifdef MNT2_NFS_OPT_ACDIRMIN
if (!err_acval || !err_acrdmm)
nap->flags |= MNT2_NFS_OPT_ACDIRMIN;
# endif /* MNT2_NFS_OPT_ACDIRMIN */
#endif /* HAVE_NFS_ARGS_T_ACDIRMIN */
/*** acdirmax ***/
#ifdef HAVE_NFS_ARGS_T_ACDIRMAX
err_acrdmm = 1; /* 1 means we found no acdirmax value */
if (!err_acval) {
nap->acdirmax = acval; /* max ac timeout for dirs (sec) */
} else {
# ifdef MNTTAB_OPT_ACDIRMAX
int tmp;
err_acrdmm = hasmntvalerr(mntp, MNTTAB_OPT_ACDIRMAX, &tmp);
nap->acdirmax = tmp;
# else /* not MNTTAB_OPT_ACDIRMAX */
nap->acdirmax = 0;
# endif /* not MNTTAB_OPT_ACDIRMAX */
}
/* set this flag iff we changed acdirmax (possibly to zero) */
# ifdef MNT2_NFS_OPT_ACDIRMAX
if (!err_acval || !err_acrdmm)
nap->flags |= MNT2_NFS_OPT_ACDIRMAX;
# endif /* MNT2_NFS_OPT_ACDIRMAX */
#endif /* HAVE_NFS_ARGS_T_ACDIRMAX */
#ifdef MNT2_NFS_OPT_NOCONN
/* check if user specified to use unconnected or connected sockets */
if (amu_hasmntopt(mntp, MNTTAB_OPT_NOCONN) != NULL)
nap->flags |= MNT2_NFS_OPT_NOCONN;
else if (amu_hasmntopt(mntp, MNTTAB_OPT_CONN) != NULL)
nap->flags &= ~MNT2_NFS_OPT_NOCONN;
else {
/*
* Some OSs want you to set noconn always. Some want you to always turn
* it off. Others want you to turn it on/off only if NFS V.3 is used.
* And all of that changes from revision to another. This is
* particularly true of OpenBSD, NetBSD, and FreeBSD. So, rather than
* attempt to auto-detect this, I'm forced to "fix" it in the individual
* conf/nfs_prot/nfs_prot_*.h files.
*/
# ifdef USE_UNCONNECTED_NFS_SOCKETS
if (!(nap->flags & MNT2_NFS_OPT_NOCONN)) {
nap->flags |= MNT2_NFS_OPT_NOCONN;
plog(XLOG_WARNING, "noconn option not specified, and was just turned ON (OS override)! (May cause NFS hangs on some systems...)");
}
# endif /* USE_UNCONNECTED_NFS_SOCKETS */
# ifdef USE_CONNECTED_NFS_SOCKETS
if (nap->flags & MNT2_NFS_OPT_NOCONN) {
nap->flags &= ~MNT2_NFS_OPT_NOCONN;
plog(XLOG_WARNING, "noconn option specified, and was just turned OFF (OS override)! (May cause NFS hangs on some systems...)");
}
# endif /* USE_CONNECTED_NFS_SOCKETS */
}
#endif /* MNT2_NFS_OPT_NOCONN */
/*
* XXX: need to figure out how to correctly print file handles,
* since some times they are pointers, and sometimes the real structure
* is stored in nfs_args. Even if it is a pointer, it can be the actual
* char[] array, or a structure containing multiple fields.
*/
plog(XLOG_DEBUG, "NA->filehandle = \"%s\"",
get_hex_string(fhlen, (const char *) &nap->NFS_FH_FIELD));
/*
* Fill in the many possible fields and flags of struct nfs_args.
*
* nap: pre-allocated structure to fill in.
* mntp: mount entry structure (includes options)
* genflags: generic mount flags already determined
* nfsncp: (TLI only) netconfig entry for this NFS mount
* ip_addr: IP address of file server
* nfs_version: 2, 3, or 0 if unknown
* nfs_proto: "udp", "tcp", or NULL.
* fhp: file handle structure pointer
* host_name: name of remote NFS host
* fs_name: remote file system name to mount
*/
static void
compute_nfs23_args(nfs_args_t *nap,
mntent_t *mntp,
int genflags,
struct netconfig *nfsncp,
struct sockaddr_in *ip_addr,
u_long nfs_version,
char *nfs_proto,
am_nfs_handle_t *fhp,
char *host_name,
char *fs_name)
{
struct nfs_common_args a;
/* initialize just in case */
memset((voidp) nap, 0, sizeof(nfs_args_t));
/* compute all of the NFS attribute-cache flags */
memset(&a, 0, sizeof(a));
compute_nfs_attrcache_flags(&a, mntp);
compute_nfs_common_args(&a, mntp, nfs_proto, nfs_version);
get_nfs_common_args(nap, a);
/************************************************************************/
/*** FILEHANDLE DATA AND LENGTH ***/
/************************************************************************/
#ifdef HAVE_FS_NFS3
if (nfs_version == NFS_VERSION3) {
if (fhp == NULL) {
plog(XLOG_FATAL, "cannot pass NULL fh for NFSv%lu", nfs_version);
going_down(1);
return;
}
# if defined(HAVE_NFS_ARGS_T_FHSIZE) || defined(HAVE_NFS_ARGS_T_FH_LEN)
/*
* Some systems (Irix/bsdi3) have a separate field in nfs_args for
* the length of the file handle for NFS V3. They insist that
* the file handle set in nfs_args be plain bytes, and not
* include the length field.
*/
NFS_FH_DREF(nap->NFS_FH_FIELD, &fhp->v3.am_fh3_data);
# else /* not defined(HAVE_NFS_ARGS_T_FHSIZE) || defined(HAVE_NFS_ARGS_T_FH_LEN) */
NFS_FH_DREF(nap->NFS_FH_FIELD, &fhp->v3);
# endif /* not defined(HAVE_NFS_ARGS_T_FHSIZE) || defined(HAVE_NFS_ARGS_T_FH_LEN) */
# ifdef MNT2_NFS_OPT_NFSV3
nap->flags |= MNT2_NFS_OPT_NFSV3;
# endif /* MNT2_NFS_OPT_NFSV3 */
# ifdef MNT2_NFS_OPT_VER3
nap->flags |= MNT2_NFS_OPT_VER3;
# endif /* MNT2_NFS_OPT_VER3 */
} else
#endif /* HAVE_FS_NFS3 */
{
if (fhp == NULL) {
plog(XLOG_FATAL, "cannot pass NULL fh for NFSv%lu", nfs_version);
going_down(1);
return;
}
NFS_FH_DREF(nap->NFS_FH_FIELD, &fhp->v2);
}
/* this is the version of the nfs_args structure, not of NFS! */
#ifdef HAVE_NFS_ARGS_T_FH_LEN
# ifdef HAVE_FS_NFS3
if (nfs_version == NFS_VERSION3)
nap->fh_len = fhp->v3.am_fh3_length;
else
# endif /* HAVE_FS_NFS3 */
nap->fh_len = FHSIZE;
#endif /* HAVE_NFS_ARGS_T_FH_LEN */
/************************************************************************/
/*** HOST NAME ***/
/************************************************************************/
/*
* XXX: warning, using xstrlcpy in NFS_HN_DREF, which may corrupt a
* struct nfs_args, or truncate our concocted "hostname:/path"
* string prematurely.
*/
NFS_HN_DREF(nap->hostname, host_name);
#ifdef MNT2_NFS_OPT_HOSTNAME
nap->flags |= MNT2_NFS_OPT_HOSTNAME;
#endif /* MNT2_NFS_OPT_HOSTNAME */
/************************************************************************/
/*** IP ADDRESS OF REMOTE HOST ***/
/************************************************************************/
if (ip_addr) {
#ifdef HAVE_TRANSPORT_TYPE_TLI
nap->addr = ALLOC(struct netbuf); /* free()'ed at end of mount_nfs_fh() */
#endif /* HAVE_TRANSPORT_TYPE_TLI */
NFS_SA_DREF(nap, ip_addr);
}
/************************************************************************/
/*** NFS PROTOCOL (UDP, TCP) AND VERSION ***/
/************************************************************************/
#ifdef HAVE_NFS_ARGS_T_SOTYPE
/* bsdi3 uses this */
if (nfs_proto) {
if (STREQ(nfs_proto, "tcp"))
nap->sotype = SOCK_STREAM;
else if (STREQ(nfs_proto, "udp"))
nap->sotype = SOCK_DGRAM;
}
#endif /* HAVE_NFS_ARGS_T_SOTYPE */
#ifdef HAVE_NFS_ARGS_T_PROTO
nap->proto = 0; /* bsdi3 sets this field to zero */
# ifdef IPPROTO_TCP
if (nfs_proto) {
if (STREQ(nfs_proto, "tcp")) /* AIX 4.2.x needs this */
nap->proto = IPPROTO_TCP;
else if (STREQ(nfs_proto, "udp"))
nap->proto = IPPROTO_UDP;
}
# endif /* IPPROTO_TCP */
#endif /* HAVE_NFS_ARGS_T_SOTYPE */
/************************************************************************/
/*** OTHER NFS SOCKET RELATED OPTIONS AND FLAGS ***/
/************************************************************************/
/************************************************************************/
/*** OTHER FLAGS AND OPTIONS ***/
/************************************************************************/
#ifdef HAVE_TRANSPORT_TYPE_TLI
/* set up syncaddr field */
nap->syncaddr = (struct netbuf *) NULL;
/* set up knconf field */
if (get_knetconfig(&nap->knconf, nfsncp, nfs_proto) < 0) {
plog(XLOG_FATAL, "cannot fill knetconfig structure for nfs_args");
going_down(1);
return;
}
/* update the flags field for knconf */
nap->args.flags |= MNT2_NFS_OPT_KNCONF;
#endif /* HAVE_TRANSPORT_TYPE_TLI */
static void
discard_nfs4_args(nfs4_args_t *nap)
{
if (nap->client_addr.data)
free(nap->client_addr.data);
if (nap->hostname.data)
free(nap->hostname.data);
if (nap->mnt_path.data)
free(nap->mnt_path.data);
if (nap->host_addr)
free(nap->host_addr);
if (nap->auth_flavours)
free(nap->auth_flavours);
}
/*
* Fill in the many possible fields and flags of struct nfs4_args.
*
* nap: pre-allocated structure to fill in.
* mntp: mount entry structure (includes options)
* genflags: generic mount flags already determined
* nfsncp: (TLI only) netconfig entry for this NFS mount
* ip_addr: IP address of file server
* nfs_version: 4, or 0 if unknown
* nfs_proto: "udp", "tcp", or NULL.
* fhp: file handle structure pointer
* host_name: name of remote NFS host
* fs_name: remote file system name to mount
*/
static void
compute_nfs4_args(nfs4_args_t *nap,
mntent_t *mntp,
int genflags,
struct netconfig *nfsncp,
struct sockaddr_in *ip_addr,
u_long nfs_version,
char *nfs_proto,
am_nfs_handle_t *fhp,
char *host_name,
char *fs_name)
{
char *s;
struct nfs_common_args a;
uint16_t nfs_port;
/* initialize just in case */
memset((voidp) nap, 0, sizeof(nfs4_args_t));
/* compute all of the NFS attribute-cache flags */
memset(&a, 0, sizeof(a));
compute_nfs_attrcache_flags(&a, mntp);
compute_nfs_common_args(&a, mntp, nfs_proto, nfs_version);
get_nfs_common_args(nap, a);
nap->proto = 0; /* bsdi3 sets this field to zero */
if (nfs_proto) {
if (STREQ(nfs_proto, "tcp")) /* AIX 4.2.x needs this */
nap->proto = IPPROTO_TCP;
else if (STREQ(nfs_proto, "udp"))
nap->proto = IPPROTO_UDP;
}
nap->version = NFS4_MOUNT_VERSION; /* BSDI 3.0 and OpenBSD 2.2 */
/************************************************************************/
/*** OTHER NFS SOCKET RELATED OPTIONS AND FLAGS ***/
/************************************************************************/
/************************************************************************/
/*** OTHER FLAGS AND OPTIONS ***/
/************************************************************************/
/*
* Fill in special values for flags and fields of nfs_args, for an
* automounter NFS mount.
*/
void
compute_automounter_nfs_args(nfs_args_t *nap, mntent_t *mntp)
{
struct nfs_common_args a;
#ifdef MNT2_NFS_OPT_SYMTTL
/*
* Don't let the kernel cache symbolic links we generate, or else lookups
* will bypass amd and fail to remount stuff as needed.
*/
plog(XLOG_INFO, "turning on NFS option symttl and setting value to 0");
nap->flags |= MNT2_NFS_OPT_SYMTTL;
nap->symttl = 0;
#endif /* MNT2_NFS_OPT_SYMTTL */
/*
* This completes the flags for the HIDE_MOUNT_TYPE code in the
* mount_amfs_toplvl() function in amd/amfs_toplvl.c.
* Some systems don't have a mount type, but a mount flag.
*/
#ifdef MNT2_NFS_OPT_AUTO
nap->flags |= MNT2_NFS_OPT_AUTO;
#endif /* MNT2_NFS_OPT_AUTO */
#ifdef MNT2_NFS_OPT_IGNORE
nap->flags |= MNT2_NFS_OPT_IGNORE;
#endif /* MNT2_NFS_OPT_IGNORE */
#ifdef MNT2_GEN_OPT_AUTOMNTFS
nap->flags |= MNT2_GEN_OPT_AUTOMNTFS;
#endif /* not MNT2_GEN_OPT_AUTOMNTFS */
#ifdef MNT2_NFS_OPT_DUMBTIMR
/*
* Don't let the kernel start computing throughput of Amd. The numbers
* will be meaningless because of the way Amd does mount retries.
*/
plog(XLOG_INFO, "%s: disabling nfs congestion window", mntp->mnt_dir);
nap->flags |= MNT2_NFS_OPT_DUMBTIMR;
#endif /* MNT2_NFS_OPT_DUMBTIMR */
/* compute all of the NFS attribute-cache flags */
memset(&a, 0, sizeof(a));
a.flags = nap->flags;
compute_nfs_attrcache_flags(&a, mntp);
get_nfs_common_args(nap, a);
/*
* Provide a slight bit more security by requiring the kernel to use
* reserved ports.
*/
#ifdef MNT2_NFS_OPT_RESVPORT
nap->flags |= MNT2_NFS_OPT_RESVPORT;
#endif /* MNT2_NFS_OPT_RESVPORT */
}
int
nfs_valid_version(u_long v)
{
return v >= NFS_VERS_MIN && v <= NFS_VERS_MAX;
}