freebsd-skq/sys/nfs4client/nfs4_vfsops.c
Julian Elischer 8b07e49a00 Add code to allow the system to handle multiple routing tables.
This particular implementation is designed to be fully backwards compatible
and to be MFC-able to 7.x (and 6.x)

Currently the only protocol that can make use of the multiple tables is IPv4
Similar functionality exists in OpenBSD and Linux.

From my notes:

-----

  One thing where FreeBSD has been falling behind, and which by chance I
  have some time to work on is "policy based routing", which allows
  different
  packet streams to be routed by more than just the destination address.

  Constraints:
  ------------

  I want to make some form of this available in the 6.x tree
  (and by extension 7.x) , but FreeBSD in general needs it so I might as
  well do it in -current and back port the portions I need.

  One of the ways that this can be done is to have the ability to
  instantiate multiple kernel routing tables (which I will now
  refer to as "Forwarding Information Bases" or "FIBs" for political
  correctness reasons). Which FIB a particular packet uses to make
  the next hop decision can be decided by a number of mechanisms.
  The policies these mechanisms implement are the "Policies" referred
  to in "Policy based routing".

  One of the constraints I have if I try to back port this work to
  6.x is that it must be implemented as a EXTENSION to the existing
  ABIs in 6.x so that third party applications do not need to be
  recompiled in timespan of the branch.

  This first version will not have some of the bells and whistles that
  will come with later versions. It will, for example, be limited to 16
  tables in the first commit.
  Implementation method, Compatible version. (part 1)
  -------------------------------
  For this reason I have implemented a "sufficient subset" of a
  multiple routing table solution in Perforce, and back-ported it
  to 6.x. (also in Perforce though not  always caught up with what I
  have done in -current/P4). The subset allows a number of FIBs
  to be defined at compile time (8 is sufficient for my purposes in 6.x)
  and implements the changes needed to allow IPV4 to use them. I have not
  done the changes for ipv6 simply because I do not need it, and I do not
  have enough knowledge of ipv6 (e.g. neighbor discovery) needed to do it.

  Other protocol families are left untouched and should there be
  users with proprietary protocol families, they should continue to work
  and be oblivious to the existence of the extra FIBs.

  To understand how this is done, one must know that the current FIB
  code starts everything off with a single dimensional array of
  pointers to FIB head structures (One per protocol family), each of
  which in turn points to the trie of routes available to that family.

  The basic change in the ABI compatible version of the change is to
  extent that array to be a 2 dimensional array, so that
  instead of protocol family X looking at rt_tables[X] for the
  table it needs, it looks at rt_tables[Y][X] when for all
  protocol families except ipv4 Y is always 0.
  Code that is unaware of the change always just sees the first row
  of the table, which of course looks just like the one dimensional
  array that existed before.

  The entry points rtrequest(), rtalloc(), rtalloc1(), rtalloc_ign()
  are all maintained, but refer only to the first row of the array,
  so that existing callers in proprietary protocols can continue to
  do the "right thing".
  Some new entry points are added, for the exclusive use of ipv4 code
  called in_rtrequest(), in_rtalloc(), in_rtalloc1() and in_rtalloc_ign(),
  which have an extra argument which refers the code to the correct row.

  In addition, there are some new entry points (currently called
  rtalloc_fib() and friends) that check the Address family being
  looked up and call either rtalloc() (and friends) if the protocol
  is not IPv4 forcing the action to row 0 or to the appropriate row
  if it IS IPv4 (and that info is available). These are for calling
  from code that is not specific to any particular protocol. The way
  these are implemented would change in the non ABI preserving code
  to be added later.

  One feature of the first version of the code is that for ipv4,
  the interface routes show up automatically on all the FIBs, so
  that no matter what FIB you select you always have the basic
  direct attached hosts available to you. (rtinit() does this
  automatically).

  You CAN delete an interface route from one FIB should you want
  to but by default it's there. ARP information is also available
  in each FIB. It's assumed that the same machine would have the
  same MAC address, regardless of which FIB you are using to get
  to it.

  This brings us as to how the correct FIB is selected for an outgoing
  IPV4 packet.

  Firstly, all packets have a FIB associated with them. if nothing
  has been done to change it, it will be FIB 0. The FIB is changed
  in the following ways.

  Packets fall into one of a number of classes.

  1/ locally generated packets, coming from a socket/PCB.
     Such packets select a FIB from a number associated with the
     socket/PCB. This in turn is inherited from the process,
     but can be changed by a socket option. The process in turn
     inherits it on fork. I have written a utility call setfib
     that acts a bit like nice..

         setfib -3 ping target.example.com # will use fib 3 for ping.

     It is an obvious extension to make it a property of a jail
     but I have not done so. It can be achieved by combining the setfib and
     jail commands.

  2/ packets received on an interface for forwarding.
     By default these packets would use table 0,
     (or possibly a number settable in a sysctl(not yet)).
     but prior to routing the firewall can inspect them (see below).
     (possibly in the future you may be able to associate a FIB
     with packets received on an interface..  An ifconfig arg, but not yet.)

  3/ packets inspected by a packet classifier, which can arbitrarily
     associate a fib with it on a packet by packet basis.
     A fib assigned to a packet by a packet classifier
     (such as ipfw) would over-ride a fib associated by
     a more default source. (such as cases 1 or 2).

  4/ a tcp listen socket associated with a fib will generate
     accept sockets that are associated with that same fib.

  5/ Packets generated in response to some other packet (e.g. reset
     or icmp packets). These should use the FIB associated with the
     packet being reponded to.

  6/ Packets generated during encapsulation.
     gif, tun and other tunnel interfaces will encapsulate using the FIB
     that was in effect withthe proces that set up the tunnel.
     thus setfib 1 ifconfig gif0 [tunnel instructions]
     will set the fib for the tunnel to use to be fib 1.

  Routing messages would be associated with their
  process, and thus select one FIB or another.
  messages from the kernel would be associated with the fib they
  refer to and would only be received by a routing socket associated
  with that fib. (not yet implemented)

  In addition Netstat has been edited to be able to cope with the
  fact that the array is now 2 dimensional. (It looks in system
  memory using libkvm (!)). Old versions of netstat see only the first FIB.

  In addition two sysctls are added to give:
  a) the number of FIBs compiled in (active)
  b) the default FIB of the calling process.

  Early testing experience:
  -------------------------

  Basically our (IronPort's) appliance does this functionality already
  using ipfw fwd but that method has some drawbacks.

  For example,
  It can't fully simulate a routing table because it can't influence the
  socket's choice of local address when a connect() is done.

  Testing during the generating of these changes has been
  remarkably smooth so far. Multiple tables have co-existed
  with no notable side effects, and packets have been routes
  accordingly.

  ipfw has grown 2 new keywords:

  setfib N ip from anay to any
  count ip from any to any fib N

  In pf there seems to be a requirement to be able to give symbolic names to the
  fibs but I do not have that capacity. I am not sure if it is required.

  SCTP has interestingly enough built in support for this, called VRFs
  in Cisco parlance. it will be interesting to see how that handles it
  when it suddenly actually does something.

  Where to next:
  --------------------

  After committing the ABI compatible version and MFCing it, I'd
  like to proceed in a forward direction in -current. this will
  result in some roto-tilling in the routing code.

  Firstly: the current code's idea of having a separate tree per
  protocol family, all of the same format, and pointed to by the
  1 dimensional array is a bit silly. Especially when one considers that
  there is code that makes assumptions about every protocol having the
  same internal structures there. Some protocols don't WANT that
  sort of structure. (for example the whole idea of a netmask is foreign
  to appletalk). This needs to be made opaque to the external code.

  My suggested first change is to add routing method pointers to the
  'domain' structure, along with information pointing the data.
  instead of having an array of pointers to uniform structures,
  there would be an array pointing to the 'domain' structures
  for each protocol address domain (protocol family),
  and the methods this reached would be called. The methods would have
  an argument that gives FIB number, but the protocol would be free
  to ignore it.

  When the ABI can be changed it raises the possibilty of the
  addition of a fib entry into the "struct route". Currently,
  the structure contains the sockaddr of the desination, and the resulting
  fib entry. To make this work fully, one could add a fib number
  so that given an address and a fib, one can find the third element, the
  fib entry.

  Interaction with the ARP layer/ LL layer would need to be
  revisited as well. Qing Li has been working on this already.

  This work was sponsored by Ironport Systems/Cisco

Reviewed by:    several including rwatson, bz and mlair (parts each)
Obtained from:  Ironport systems/Cisco
2008-05-09 23:03:00 +00:00

880 lines
22 KiB
C

/* $Id: nfs_vfsops.c,v 1.38 2003/11/05 14:59:01 rees Exp $ */
/*-
* copyright (c) 2003
* the regents of the university of michigan
* all rights reserved
*
* permission is granted to use, copy, create derivative works and redistribute
* this software and such derivative works for any purpose, so long as the name
* of the university of michigan is not used in any advertising or publicity
* pertaining to the use or distribution of this software without specific,
* written prior authorization. if the above copyright notice or any other
* identification of the university of michigan is included in any copy of any
* portion of this software, then the disclaimer below must also be included.
*
* this software is provided as is, without representation from the university
* of michigan as to its fitness for any purpose, and without warranty by the
* university of michigan of any kind, either express or implied, including
* without limitation the implied warranties of merchantability and fitness for
* a particular purpose. the regents of the university of michigan shall not be
* liable for any damages, including special, indirect, incidental, or
* consequential damages, with respect to any claim arising out of or in
* connection with the use of the software, even if it has been or is hereafter
* advised of the possibility of such damages.
*/
/*-
* Copyright (c) 1989, 1993, 1995
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Rick Macklem at The University of Guelph.
*
* 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.
* 4. 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.
*
* @(#)nfs_vfsops.c 8.12 (Berkeley) 5/20/95
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "opt_bootp.h"
#include "opt_nfsroot.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/kthread.h>
#include <sys/limits.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/module.h>
#include <sys/mount.h>
#include <sys/proc.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/sockio.h>
#include <sys/sysctl.h>
#include <sys/unistd.h>
#include <sys/vnode.h>
#include <sys/signalvar.h>
#include <vm/vm.h>
#include <vm/vm_extern.h>
#include <vm/uma.h>
#include <net/if.h>
#include <net/route.h>
#include <netinet/in.h>
#include <netinet/in_var.h>
#include <rpc/rpcclnt.h>
#include <nfs/rpcv2.h>
#include <nfs/nfsproto.h>
#include <nfsclient/nfs.h>
#include <nfs4client/nfs4.h>
#include <nfsclient/nfsnode.h>
#include <nfsclient/nfsmount.h>
#include <nfs/xdr_subs.h>
#include <nfsclient/nfsm_subs.h>
#include <nfsclient/nfsdiskless.h>
#include <nfs4client/nfs4m_subs.h>
#include <nfs4client/nfs4_vfs.h>
#include <nfs4client/nfs4_dev.h>
#include <nfs4client/nfs4_idmap.h>
SYSCTL_NODE(_vfs, OID_AUTO, nfs4, CTLFLAG_RW, 0, "NFS4 filesystem");
SYSCTL_STRUCT(_vfs_nfs4, NFS_NFSSTATS, nfsstats, CTLFLAG_RD,
&nfsstats, nfsstats, "S,nfsstats");
static void nfs4_decode_args(struct nfsmount *nmp, struct nfs_args *argp);
static void nfs4_daemon(void *arg);
static int mountnfs(struct nfs_args *, struct mount *,
struct sockaddr *, char *, struct vnode **,
struct ucred *cred);
static int nfs4_do_setclientid(struct nfsmount *nmp, struct ucred *cred);
static vfs_mount_t nfs4_mount;
static vfs_cmount_t nfs4_cmount;
static vfs_unmount_t nfs4_unmount;
static vfs_root_t nfs4_root;
static vfs_statfs_t nfs4_statfs;
static vfs_sync_t nfs4_sync;
/*
* nfs vfs operations.
*/
static struct vfsops nfs4_vfsops = {
.vfs_init = nfs4_init,
.vfs_mount = nfs4_mount,
.vfs_cmount = nfs4_cmount,
.vfs_root = nfs4_root,
.vfs_statfs = nfs4_statfs,
.vfs_sync = nfs4_sync,
.vfs_uninit = nfs4_uninit,
.vfs_unmount = nfs4_unmount,
};
VFS_SET(nfs4_vfsops, nfs4, VFCF_NETWORK);
static struct nfs_rpcops nfs4_rpcops = {
nfs4_readrpc,
nfs4_writerpc,
nfs4_writebp,
nfs4_readlinkrpc,
nfs4_invaldir,
nfs4_commit,
};
/* So that loader and kldload(2) can find us, wherever we are.. */
MODULE_VERSION(nfs4, 1);
void nfsargs_ntoh(struct nfs_args *);
int
nfs4_init(struct vfsconf *vfsp)
{
rpcclnt_init();
nfs4dev_init();
idmap_init();
nfsm_v4init();
return (0);
}
int
nfs4_uninit(struct vfsconf *vfsp)
{
rpcclnt_uninit();
nfs4dev_uninit();
idmap_uninit();
return (0);
}
/*
* nfs statfs call
*/
static int
nfs4_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
{
struct vnode *vp;
struct nfs_statfs *sfp;
caddr_t bpos, dpos;
struct nfsmount *nmp = VFSTONFS(mp);
int error = 0;
struct mbuf *mreq, *mrep = NULL, *md, *mb;
struct nfsnode *np;
struct nfs4_compound cp;
struct nfs4_oparg_getattr ga;
struct nfsv4_fattr *fap = &ga.fa;
#ifndef nolint
sfp = NULL;
#endif
error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np, LK_EXCLUSIVE);
if (error)
return (error);
vp = NFSTOV(np);
nfsstats.rpccnt[NFSPROC_FSSTAT]++;
mreq = nfsm_reqhead(vp, NFSV4PROC_COMPOUND, NFSX_FH(1));
mb = mreq;
bpos = mtod(mb, caddr_t);
ga.bm = &nfsv4_fsattrbm;
nfs_v4initcompound(&cp);
nfsm_v4build_compound(&cp, "statfs()");
nfsm_v4build_putfh(&cp, vp);
nfsm_v4build_getattr(&cp, &ga);
nfsm_v4build_finalize(&cp);
nfsm_request(vp, NFSV4PROC_COMPOUND, td, td->td_ucred);
if (error != 0)
goto nfsmout;
nfsm_v4dissect_compound(&cp);
nfsm_v4dissect_putfh(&cp);
nfsm_v4dissect_getattr(&cp, &ga);
nfs4_vfsop_statfs(fap, sbp, mp);
nfsmout:
error = nfs_v4postop(&cp, error);
vput(vp);
if (mrep != NULL)
m_freem(mrep);
return (error);
}
static void
nfs4_decode_args(struct nfsmount *nmp, struct nfs_args *argp)
{
int s;
int adjsock;
int maxio;
s = splnet();
/*
* Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
* no sense in that context. Also, set appropriate retransmit
* and soft timeout behavior.
*/
if (argp->sotype == SOCK_STREAM) {
nmp->nm_flag &= ~NFSMNT_NOCONN;
nmp->nm_flag |= NFSMNT_DUMBTIMR;
nmp->nm_timeo = NFS_MAXTIMEO;
nmp->nm_retry = NFS_RETRANS_TCP;
}
nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
/* Re-bind if rsrvd port requested and wasn't on one */
adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT)
&& (argp->flags & NFSMNT_RESVPORT);
/* Also re-bind if we're switching to/from a connected UDP socket */
adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) !=
(argp->flags & NFSMNT_NOCONN));
/* Update flags atomically. Don't change the lock bits. */
nmp->nm_flag = argp->flags | nmp->nm_flag;
splx(s);
if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
if (nmp->nm_timeo < NFS_MINTIMEO)
nmp->nm_timeo = NFS_MINTIMEO;
else if (nmp->nm_timeo > NFS_MAXTIMEO)
nmp->nm_timeo = NFS_MAXTIMEO;
}
if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
nmp->nm_retry = argp->retrans;
if (nmp->nm_retry > NFS_MAXREXMIT)
nmp->nm_retry = NFS_MAXREXMIT;
}
if (argp->flags & NFSMNT_NFSV3) {
if (argp->sotype == SOCK_DGRAM)
maxio = NFS_MAXDGRAMDATA;
else
maxio = NFS_MAXDATA;
} else
maxio = NFS_V2MAXDATA;
if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
nmp->nm_wsize = argp->wsize;
/* Round down to multiple of blocksize */
nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
if (nmp->nm_wsize <= 0)
nmp->nm_wsize = NFS_FABLKSIZE;
}
if (nmp->nm_wsize > maxio)
nmp->nm_wsize = maxio;
if (nmp->nm_wsize > MAXBSIZE)
nmp->nm_wsize = MAXBSIZE;
if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
nmp->nm_rsize = argp->rsize;
/* Round down to multiple of blocksize */
nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
if (nmp->nm_rsize <= 0)
nmp->nm_rsize = NFS_FABLKSIZE;
}
if (nmp->nm_rsize > maxio)
nmp->nm_rsize = maxio;
if (nmp->nm_rsize > MAXBSIZE)
nmp->nm_rsize = MAXBSIZE;
if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
nmp->nm_readdirsize = argp->readdirsize;
}
if (nmp->nm_readdirsize > maxio)
nmp->nm_readdirsize = maxio;
if (nmp->nm_readdirsize > nmp->nm_rsize)
nmp->nm_readdirsize = nmp->nm_rsize;
if ((argp->flags & NFSMNT_ACREGMIN) && argp->acregmin >= 0)
nmp->nm_acregmin = argp->acregmin;
else
nmp->nm_acregmin = NFS_MINATTRTIMO;
if ((argp->flags & NFSMNT_ACREGMAX) && argp->acregmax >= 0)
nmp->nm_acregmax = argp->acregmax;
else
nmp->nm_acregmax = NFS_MAXATTRTIMO;
if ((argp->flags & NFSMNT_ACDIRMIN) && argp->acdirmin >= 0)
nmp->nm_acdirmin = argp->acdirmin;
else
nmp->nm_acdirmin = NFS_MINDIRATTRTIMO;
if ((argp->flags & NFSMNT_ACDIRMAX) && argp->acdirmax >= 0)
nmp->nm_acdirmax = argp->acdirmax;
else
nmp->nm_acdirmax = NFS_MAXDIRATTRTIMO;
if (nmp->nm_acdirmin > nmp->nm_acdirmax)
nmp->nm_acdirmin = nmp->nm_acdirmax;
if (nmp->nm_acregmin > nmp->nm_acregmax)
nmp->nm_acregmin = nmp->nm_acregmax;
if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0) {
if (argp->maxgrouplist <= NFS_MAXGRPS)
nmp->nm_numgrps = argp->maxgrouplist;
else
nmp->nm_numgrps = NFS_MAXGRPS;
}
if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0) {
if (argp->readahead <= NFS_MAXRAHEAD)
nmp->nm_readahead = argp->readahead;
else
nmp->nm_readahead = NFS_MAXRAHEAD;
}
if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 0) {
if (argp->deadthresh <= NFS_MAXDEADTHRESH)
nmp->nm_deadthresh = argp->deadthresh;
else
nmp->nm_deadthresh = NFS_MAXDEADTHRESH;
}
adjsock |= ((nmp->nm_sotype != argp->sotype) ||
(nmp->nm_soproto != argp->proto));
nmp->nm_sotype = argp->sotype;
nmp->nm_soproto = argp->proto;
if (nmp->nm_rpcclnt.rc_so && adjsock) {
nfs_safedisconnect(nmp);
if (nmp->nm_sotype == SOCK_DGRAM) {
while (nfs4_connect(nmp)) {
printf("nfs4_decode_args: retrying connect\n");
(void)tsleep(&lbolt, PSOCK, "nfscon", 0);
}
}
}
}
/*
* VFS Operations.
*
* mount system call
* It seems a bit dumb to copyinstr() the host and path here and then
* bcopy() them in mountnfs(), but I wanted to detect errors before
* doing the sockargs() call because sockargs() allocates an mbuf and
* an error after that means that I have to release the mbuf.
*/
/* ARGSUSED */
static int
nfs4_cmount(struct mntarg *ma, void *data, int flags, struct thread *td)
{
struct nfs_args args;
int error;
error = copyin(data, &args, sizeof(struct nfs_args));
if (error)
return (error);
ma = mount_arg(ma, "nfs_args", &args, sizeof args);
error = kernel_mount(ma, flags);
return (error);
}
static int
nfs4_mount(struct mount *mp, struct thread *td)
{
int error;
struct nfs_args args;
struct sockaddr *nam;
struct vnode *vp;
char hst[MNAMELEN];
size_t len;
if (mp->mnt_flag & MNT_ROOTFS) {
printf("nfs4_mountroot not supported\n");
return (EINVAL);
}
error = vfs_copyopt(mp->mnt_optnew, "nfs_args", &args, sizeof args);
if (error)
return (error);
if (args.version != NFS_ARGSVERSION)
return (EPROGMISMATCH);
if (mp->mnt_flag & MNT_UPDATE) {
struct nfsmount *nmp = VFSTONFS(mp);
if (nmp == NULL)
return (EIO);
/*
* When doing an update, we can't change from or to
* v3, switch lockd strategies or change cookie translation
*/
args.flags = (args.flags &
~(NFSMNT_NFSV3 | NFSMNT_NFSV4 | NFSMNT_NOLOCKD)) |
(nmp->nm_flag &
(NFSMNT_NFSV3 | NFSMNT_NFSV4 | NFSMNT_NOLOCKD));
nfs4_decode_args(nmp, &args);
return (0);
}
error = copyinstr(args.hostname, hst, MNAMELEN-1, &len);
if (error)
return (error);
bzero(&hst[len], MNAMELEN - len);
/* sockargs() call must be after above copyin() calls */
error = getsockaddr(&nam, (caddr_t)args.addr, args.addrlen);
if (error)
return (error);
error = mountnfs(&args, mp, nam, hst, &vp, td->td_ucred);
return (error);
}
/*
* renew should be done async
* should re-scan mount queue each time
*/
struct proc *nfs4_daemonproc;
static int
nfs4_do_renew(struct nfsmount *nmp, struct ucred *cred)
{
struct nfs4_compound cp;
struct mbuf *mreq, *mrep = NULL, *md, *mb;
caddr_t bpos, dpos;
int error;
mreq = nfsm_reqhead(NULL, NFSV4PROC_COMPOUND, sizeof(uint64_t));
mb = mreq;
bpos = mtod(mb, caddr_t);
nfs_v4initcompound(&cp);
nfsm_v4build_compound(&cp, "nfs4_do_renew()");
nfsm_v4build_renew(&cp, nmp->nm_clientid);
nfsm_v4build_finalize(&cp);
nfsm_request_mnt(nmp, NFSV4PROC_COMPOUND, curthread, cred);
if (error != 0)
goto nfsmout;
nfsm_v4dissect_compound(&cp);
nfsm_v4dissect_renew(&cp);
nmp->nm_last_renewal = time_second;
return (0);
nfsmout:
error = nfs_v4postop(&cp, error);
/* XXX */
if (mrep != NULL)
m_freem(mrep);
return (error);
}
static void
nfs4_daemon(void *arg)
{
struct mount *mp;
struct nfsmount *nmp;
int nmounts;
while (1) {
nmounts = 0;
mtx_lock(&mountlist_mtx);
TAILQ_FOREACH(mp, &mountlist, mnt_list) {
if (strcmp(mp->mnt_vfc->vfc_name, "nfs4") != 0)
continue;
nmounts++;
nmp = VFSTONFS(mp);
if (time_second < nmp->nm_last_renewal + nmp->nm_lease_time - 4)
continue;
mtx_unlock(&mountlist_mtx);
mtx_lock(&Giant);
nfs4_do_renew(nmp, (struct ucred *) arg);
mtx_unlock(&Giant);
mtx_lock(&mountlist_mtx);
}
mtx_unlock(&mountlist_mtx);
/* Must kill the daemon here, or module unload will cause a panic */
if (nmounts == 0) {
mtx_lock(&Giant);
nfs4_daemonproc = NULL;
mtx_unlock(&Giant);
/*printf("nfsv4 renewd exiting\n");*/
kproc_exit(0);
}
tsleep(&nfs4_daemonproc, PVFS, "nfs4", 2 * hz);
}
}
/*
* Common code for mount and mountroot
*/
static int
mountnfs(struct nfs_args *argp, struct mount *mp, struct sockaddr *nam,
char *hst, struct vnode **vpp, struct ucred *cred)
{
struct nfsmount *nmp;
char *rpth, *cp1, *cp2;
int nlkup = 0, error;
struct nfs4_compound cp;
struct mbuf *mreq, *mrep = NULL, *md, *mb;
caddr_t bpos, dpos;
struct nfs4_oparg_lookup lkup;
struct nfs4_oparg_getfh gfh;
struct nfs4_oparg_getattr ga;
struct thread *td = curthread; /* XXX */
if (mp->mnt_flag & MNT_UPDATE) {
nmp = VFSTONFS(mp);
/* update paths, file handles, etc, here XXX */
FREE(nam, M_SONAME);
return (0);
} else {
nmp = uma_zalloc(nfsmount_zone, M_WAITOK);
bzero((caddr_t)nmp, sizeof (struct nfsmount));
TAILQ_INIT(&nmp->nm_bufq);
mp->mnt_data = nmp;
}
vfs_getnewfsid(mp);
nmp->nm_mountp = mp;
mtx_init(&nmp->nm_mtx, "NFS4mount lock", NULL, MTX_DEF);
nmp->nm_maxfilesize = 0xffffffffLL;
nmp->nm_timeo = NFS_TIMEO;
nmp->nm_retry = NFS_RETRANS;
nmp->nm_wsize = NFS_WSIZE;
nmp->nm_rsize = NFS_RSIZE;
nmp->nm_readdirsize = NFS_READDIRSIZE;
nmp->nm_numgrps = NFS_MAXGRPS;
nmp->nm_readahead = NFS_DEFRAHEAD;
nmp->nm_deadthresh = NFS_MAXDEADTHRESH;
vfs_mountedfrom(mp, hst);
nmp->nm_nam = nam;
/* Set up the sockets and per-host congestion */
nmp->nm_sotype = argp->sotype;
nmp->nm_soproto = argp->proto;
nmp->nm_rpcops = &nfs4_rpcops;
/* XXX */
mp->mnt_stat.f_iosize = PAGE_SIZE;
argp->flags |= (NFSMNT_NFSV3 | NFSMNT_NFSV4);
nfs4_decode_args(nmp, argp);
if ((error = nfs4_connect(nmp)))
goto bad;
mreq = nfsm_reqhead(NULL, NFSV4PROC_COMPOUND, NFSX_FH(1));
mb = mreq;
bpos = mtod(mb, caddr_t);
ga.bm = &nfsv4_fsinfobm;
nfs_v4initcompound(&cp);
/* Get remote path */
rpth = hst;
strsep(&rpth, ":");
nfsm_v4build_compound(&cp, "mountnfs()");
nfsm_v4build_putrootfh(&cp);
for (cp1 = rpth; cp1 && *cp1; cp1 = cp2) {
while (*cp1 == '/')
cp1++;
if (!*cp1)
break;
for (cp2 = cp1; *cp2 && *cp2 != '/'; cp2++)
;
lkup.name = cp1;
lkup.namelen = cp2 - cp1;
nfsm_v4build_lookup(&cp, &lkup);
nlkup++;
}
nfsm_v4build_getfh(&cp, &gfh);
nfsm_v4build_getattr(&cp, &ga);
nfsm_v4build_finalize(&cp);
nfsm_request_mnt(nmp, NFSV4PROC_COMPOUND, td, cred);
if (error != 0)
goto nfsmout;
nfsm_v4dissect_compound(&cp);
nfsm_v4dissect_putrootfh(&cp);
while (nlkup--)
nfsm_v4dissect_lookup(&cp);
nfsm_v4dissect_getfh(&cp, &gfh);
nfsm_v4dissect_getattr(&cp, &ga);
nfs4_vfsop_fsinfo(&ga.fa, nmp);
nmp->nm_state |= NFSSTA_GOTFSINFO;
/* Copy root fh into nfsmount. */
nmp->nm_fhsize = gfh.fh_len;
bcopy(&gfh.fh_val, nmp->nm_fh, nmp->nm_fhsize);
nmp->nm_last_renewal = time_second;
if ((error = nfs4_do_setclientid(nmp, cred)) != 0)
goto nfsmout;
/* Start renewd if it isn't already running */
if (nfs4_daemonproc == NULL)
kproc_create(nfs4_daemon, crdup(cred), &nfs4_daemonproc,
(RFPROC|RFMEM), 0, "nfs4rd");
return (0);
nfsmout:
error = nfs_v4postop(&cp, error);
/* XXX */
if (mrep != NULL)
m_freem(mrep);
bad:
mtx_destroy(&nmp->nm_mtx);
nfs4_disconnect(nmp);
uma_zfree(nfsmount_zone, nmp);
FREE(nam, M_SONAME);
return (error);
}
/*
* unmount system call
*/
static int
nfs4_unmount(struct mount *mp, int mntflags, struct thread *td)
{
struct nfsmount *nmp;
int error, flags = 0;
if (mntflags & MNT_FORCE)
flags |= FORCECLOSE;
nmp = VFSTONFS(mp);
/*
* Goes something like this..
* - Call vflush(, td) to clear out vnodes for this filesystem
* - Close the socket
* - Free up the data structures
*/
/* In the forced case, cancel any outstanding requests. */
if (flags & FORCECLOSE) {
error = nfs_nmcancelreqs(nmp);
if (error)
return (error);
nfs4dev_purge();
}
error = vflush(mp, 0, flags, td);
if (error)
return (error);
/*
* We are now committed to the unmount.
*/
nfs4_disconnect(nmp);
FREE(nmp->nm_nam, M_SONAME);
/* XXX there's a race condition here for SMP */
wakeup(&nfs4_daemonproc);
mtx_destroy(&nmp->nm_mtx);
uma_zfree(nfsmount_zone, nmp);
return (0);
}
/*
* Return root of a filesystem
*/
static int
nfs4_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td)
{
struct vnode *vp;
struct nfsmount *nmp;
struct nfsnode *np;
int error;
nmp = VFSTONFS(mp);
error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np,
LK_EXCLUSIVE);
if (error)
return (error);
vp = NFSTOV(np);
if (vp->v_type == VNON)
vp->v_type = VDIR;
vp->v_vflag |= VV_ROOT;
*vpp = vp;
return (0);
}
/*
* Flush out the buffer cache
*/
static int
nfs4_sync(struct mount *mp, int waitfor, struct thread *td)
{
struct vnode *vp, *mvp;
int error, allerror = 0;
/*
* Force stale buffer cache information to be flushed.
*/
MNT_ILOCK(mp);
loop:
MNT_VNODE_FOREACH(vp, mp, mvp) {
VI_LOCK(vp);
MNT_IUNLOCK(mp);
/* XXX racy bv_cnt check. */
if (VOP_ISLOCKED(vp) || vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
waitfor == MNT_LAZY) {
VI_UNLOCK(vp);
MNT_ILOCK(mp);
continue;
}
if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
MNT_ILOCK(mp);
MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
goto loop;
}
error = VOP_FSYNC(vp, waitfor, td);
if (error)
allerror = error;
VOP_UNLOCK(vp, 0);
vrele(vp);
MNT_ILOCK(mp);
}
MNT_IUNLOCK(mp);
return (allerror);
}
static int
nfs4_do_setclientid(struct nfsmount *nmp, struct ucred *cred)
{
struct nfs4_oparg_setclientid scid;
struct nfs4_compound cp;
struct mbuf *mreq, *mrep = NULL, *md, *mb;
caddr_t bpos, dpos;
struct route ro;
char *ipsrc = NULL, uaddr[24], name[24];
int try = 0;
static unsigned long seq;
int error;
#ifndef NFS4_USE_RPCCLNT
return (0);
#endif
if (nmp->nm_clientid) {
printf("nfs4_do_setclientid: already have clientid!\n");
error = 0;
goto nfsmout;
}
/* Try not to re-use clientids */
if (seq == 0)
seq = time_second;
#ifdef NFS4_USE_RPCCLNT
scid.cb_netid = (nmp->nm_rpcclnt.rc_sotype == SOCK_STREAM) ? "tcp" : "udp";
#endif
scid.cb_netid = "tcp";
scid.cb_netidlen = 3;
scid.cb_prog = 0x1234; /* XXX */
/* Do a route lookup to find our source address for talking to this server */
bzero(&ro, sizeof ro);
#ifdef NFS4_USE_RPCCLNT
ro.ro_dst = *nmp->nm_rpcclnt.rc_name;
#endif
/* XXX MRT NFS uses table 0 */
in_rtalloc(&ro, 0);
if (ro.ro_rt == NULL) {
error = EHOSTUNREACH;
goto nfsmout;
}
ipsrc = inet_ntoa(IA_SIN(ifatoia(ro.ro_rt->rt_ifa))->sin_addr);
sprintf(uaddr, "%s.12.48", ipsrc);
scid.cb_univaddr = uaddr;
scid.cb_univaddrlen = strlen(uaddr);
RTFREE(ro.ro_rt);
try_again:
sprintf(name, "%s-%d", ipsrc, (int) ((seq + try) % 1000000L));
scid.namelen = strlen(name);
scid.name = name;
nfs_v4initcompound(&cp);
mreq = nfsm_reqhead(NULL, NFSV4PROC_COMPOUND, NFSX_FH(1));
mb = mreq;
bpos = mtod(mb, caddr_t);
nfsm_v4build_compound(&cp, "nfs4_do_setclientid()");
nfsm_v4build_setclientid(&cp, &scid);
nfsm_v4build_finalize(&cp);
nfsm_request_mnt(nmp, NFSV4PROC_COMPOUND, curthread, cred);
if (error != 0)
goto nfsmout;
nfsm_v4dissect_compound(&cp);
nfsm_v4dissect_setclientid(&cp, &scid);
nmp->nm_clientid = scid.clientid;
error = nfs_v4postop(&cp, error);
/* Confirm */
m_freem(mrep);
mreq = nfsm_reqhead(NULL, NFSV4PROC_COMPOUND, NFSX_FH(1));
mb = mreq;
bpos = mtod(mb, caddr_t);
nfs_v4initcompound(&cp);
nfsm_v4build_compound(&cp, "nfs4_do_setclientid() (confirm)");
nfsm_v4build_setclientid_confirm(&cp, &scid);
nfsm_v4build_finalize(&cp);
nfsm_request_mnt(nmp, NFSV4PROC_COMPOUND, curthread, cred);
if (error != 0)
goto nfsmout;
nfsm_v4dissect_compound(&cp);
nfsm_v4dissect_setclientid_confirm(&cp);
nfsmout:
error = nfs_v4postop(&cp, error);
if (mrep)
m_freem(mrep);
if (error == NFSERR_CLID_INUSE && (++try < NFS4_SETCLIENTID_MAXTRIES))
goto try_again;
return (error);
}