Home | History | Annotate | Line # | Download | only in common
nfs_commonport.c revision 1.1
      1  1.1  dholland /*	$NetBSD: nfs_commonport.c,v 1.1 2013/09/30 07:19:36 dholland Exp $	*/
      2  1.1  dholland /*-
      3  1.1  dholland  * Copyright (c) 1989, 1993
      4  1.1  dholland  *	The Regents of the University of California.  All rights reserved.
      5  1.1  dholland  *
      6  1.1  dholland  * This code is derived from software contributed to Berkeley by
      7  1.1  dholland  * Rick Macklem at The University of Guelph.
      8  1.1  dholland  *
      9  1.1  dholland  * Redistribution and use in source and binary forms, with or without
     10  1.1  dholland  * modification, are permitted provided that the following conditions
     11  1.1  dholland  * are met:
     12  1.1  dholland  * 1. Redistributions of source code must retain the above copyright
     13  1.1  dholland  *    notice, this list of conditions and the following disclaimer.
     14  1.1  dholland  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  dholland  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  dholland  *    documentation and/or other materials provided with the distribution.
     17  1.1  dholland  * 4. Neither the name of the University nor the names of its contributors
     18  1.1  dholland  *    may be used to endorse or promote products derived from this software
     19  1.1  dholland  *    without specific prior written permission.
     20  1.1  dholland  *
     21  1.1  dholland  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1  dholland  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1  dholland  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1  dholland  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1  dholland  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1  dholland  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1  dholland  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1  dholland  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1  dholland  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1  dholland  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1  dholland  * SUCH DAMAGE.
     32  1.1  dholland  *
     33  1.1  dholland  */
     34  1.1  dholland 
     35  1.1  dholland #include <sys/cdefs.h>
     36  1.1  dholland /* __FBSDID("FreeBSD: head/sys/fs/nfs/nfs_commonport.c 249592 2013-04-17 21:00:22Z ken "); */
     37  1.1  dholland __RCSID("$NetBSD: nfs_commonport.c,v 1.1 2013/09/30 07:19:36 dholland Exp $");
     38  1.1  dholland 
     39  1.1  dholland /*
     40  1.1  dholland  * Functions that need to be different for different versions of BSD
     41  1.1  dholland  * kernel should be kept here, along with any global storage specific
     42  1.1  dholland  * to this BSD variant.
     43  1.1  dholland  */
     44  1.1  dholland #include <fs/nfs/nfsport.h>
     45  1.1  dholland #include <sys/sysctl.h>
     46  1.1  dholland #include <vm/vm.h>
     47  1.1  dholland #include <vm/vm_object.h>
     48  1.1  dholland #include <vm/vm_page.h>
     49  1.1  dholland #include <vm/vm_param.h>
     50  1.1  dholland #include <vm/vm_map.h>
     51  1.1  dholland #include <vm/vm_kern.h>
     52  1.1  dholland #include <vm/vm_extern.h>
     53  1.1  dholland #include <vm/uma.h>
     54  1.1  dholland 
     55  1.1  dholland extern int nfscl_ticks;
     56  1.1  dholland extern int nfsrv_nfsuserd;
     57  1.1  dholland extern struct nfssockreq nfsrv_nfsuserdsock;
     58  1.1  dholland extern void (*nfsd_call_recall)(struct vnode *, int, struct ucred *,
     59  1.1  dholland     struct thread *);
     60  1.1  dholland extern int nfsrv_useacl;
     61  1.1  dholland struct mount nfsv4root_mnt;
     62  1.1  dholland int newnfs_numnfsd = 0;
     63  1.1  dholland struct nfsstats newnfsstats;
     64  1.1  dholland int nfs_numnfscbd = 0;
     65  1.1  dholland int nfscl_debuglevel = 0;
     66  1.1  dholland char nfsv4_callbackaddr[INET6_ADDRSTRLEN];
     67  1.1  dholland struct callout newnfsd_callout;
     68  1.1  dholland void (*nfsd_call_servertimer)(void) = NULL;
     69  1.1  dholland void (*ncl_call_invalcaches)(struct vnode *) = NULL;
     70  1.1  dholland 
     71  1.1  dholland static int nfs_realign_test;
     72  1.1  dholland static int nfs_realign_count;
     73  1.1  dholland 
     74  1.1  dholland SYSCTL_NODE(_vfs, OID_AUTO, nfs, CTLFLAG_RW, 0, "New NFS filesystem");
     75  1.1  dholland SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_test, CTLFLAG_RW, &nfs_realign_test,
     76  1.1  dholland     0, "Number of realign tests done");
     77  1.1  dholland SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_count, CTLFLAG_RW, &nfs_realign_count,
     78  1.1  dholland     0, "Number of mbuf realignments done");
     79  1.1  dholland SYSCTL_STRING(_vfs_nfs, OID_AUTO, callback_addr, CTLFLAG_RW,
     80  1.1  dholland     nfsv4_callbackaddr, sizeof(nfsv4_callbackaddr),
     81  1.1  dholland     "NFSv4 callback addr for server to use");
     82  1.1  dholland SYSCTL_INT(_vfs_nfs, OID_AUTO, debuglevel, CTLFLAG_RW, &nfscl_debuglevel,
     83  1.1  dholland     0, "Debug level for new nfs client");
     84  1.1  dholland 
     85  1.1  dholland /*
     86  1.1  dholland  * Defines for malloc
     87  1.1  dholland  * (Here for FreeBSD, since they allocate storage.)
     88  1.1  dholland  */
     89  1.1  dholland MALLOC_DEFINE(M_NEWNFSRVCACHE, "NFSD srvcache", "NFSD Server Request Cache");
     90  1.1  dholland MALLOC_DEFINE(M_NEWNFSDCLIENT, "NFSD V4client", "NFSD V4 Client Id");
     91  1.1  dholland MALLOC_DEFINE(M_NEWNFSDSTATE, "NFSD V4state",
     92  1.1  dholland     "NFSD V4 State (Openowner, Open, Lockowner, Delegation");
     93  1.1  dholland MALLOC_DEFINE(M_NEWNFSDLOCK, "NFSD V4lock", "NFSD V4 byte range lock");
     94  1.1  dholland MALLOC_DEFINE(M_NEWNFSDLOCKFILE, "NFSD lckfile", "NFSD Open/Lock file");
     95  1.1  dholland MALLOC_DEFINE(M_NEWNFSSTRING, "NFSD string", "NFSD V4 long string");
     96  1.1  dholland MALLOC_DEFINE(M_NEWNFSUSERGROUP, "NFSD usrgroup", "NFSD V4 User/group map");
     97  1.1  dholland MALLOC_DEFINE(M_NEWNFSDREQ, "NFS req", "NFS request header");
     98  1.1  dholland MALLOC_DEFINE(M_NEWNFSFH, "NFS fh", "NFS file handle");
     99  1.1  dholland MALLOC_DEFINE(M_NEWNFSCLOWNER, "NFSCL owner", "NFSCL Open Owner");
    100  1.1  dholland MALLOC_DEFINE(M_NEWNFSCLOPEN, "NFSCL open", "NFSCL Open");
    101  1.1  dholland MALLOC_DEFINE(M_NEWNFSCLDELEG, "NFSCL deleg", "NFSCL Delegation");
    102  1.1  dholland MALLOC_DEFINE(M_NEWNFSCLCLIENT, "NFSCL client", "NFSCL Client");
    103  1.1  dholland MALLOC_DEFINE(M_NEWNFSCLLOCKOWNER, "NFSCL lckown", "NFSCL Lock Owner");
    104  1.1  dholland MALLOC_DEFINE(M_NEWNFSCLLOCK, "NFSCL lck", "NFSCL Lock");
    105  1.1  dholland MALLOC_DEFINE(M_NEWNFSV4NODE, "NEWNFSnode", "New nfs vnode");
    106  1.1  dholland MALLOC_DEFINE(M_NEWNFSDIRECTIO, "NEWdirectio", "New nfs Direct IO buffer");
    107  1.1  dholland MALLOC_DEFINE(M_NEWNFSDIROFF, "NFSCL diroffdiroff",
    108  1.1  dholland     "New NFS directory offset data");
    109  1.1  dholland MALLOC_DEFINE(M_NEWNFSDROLLBACK, "NFSD rollback",
    110  1.1  dholland     "New NFS local lock rollback");
    111  1.1  dholland MALLOC_DEFINE(M_NEWNFSLAYOUT, "NFSCL layout", "NFSv4.1 Layout");
    112  1.1  dholland MALLOC_DEFINE(M_NEWNFSFLAYOUT, "NFSCL flayout", "NFSv4.1 File Layout");
    113  1.1  dholland MALLOC_DEFINE(M_NEWNFSDEVINFO, "NFSCL devinfo", "NFSv4.1 Device Info");
    114  1.1  dholland MALLOC_DEFINE(M_NEWNFSSOCKREQ, "NFSCL sockreq", "NFS Sock Req");
    115  1.1  dholland MALLOC_DEFINE(M_NEWNFSCLDS, "NFSCL session", "NFSv4.1 Session");
    116  1.1  dholland MALLOC_DEFINE(M_NEWNFSLAYRECALL, "NFSCL layrecall", "NFSv4.1 Layout Recall");
    117  1.1  dholland 
    118  1.1  dholland /*
    119  1.1  dholland  * Definition of mutex locks.
    120  1.1  dholland  * newnfsd_mtx is used in nfsrvd_nfsd() to protect the nfs socket list
    121  1.1  dholland  * and assorted other nfsd structures.
    122  1.1  dholland  */
    123  1.1  dholland struct mtx newnfsd_mtx;
    124  1.1  dholland struct mtx nfs_sockl_mutex;
    125  1.1  dholland struct mtx nfs_state_mutex;
    126  1.1  dholland struct mtx nfs_nameid_mutex;
    127  1.1  dholland struct mtx nfs_req_mutex;
    128  1.1  dholland struct mtx nfs_slock_mutex;
    129  1.1  dholland 
    130  1.1  dholland /* local functions */
    131  1.1  dholland static int nfssvc_call(struct thread *, struct nfssvc_args *, struct ucred *);
    132  1.1  dholland 
    133  1.1  dholland #ifdef __NO_STRICT_ALIGNMENT
    134  1.1  dholland /*
    135  1.1  dholland  * These architectures don't need re-alignment, so just return.
    136  1.1  dholland  */
    137  1.1  dholland int
    138  1.1  dholland newnfs_realign(struct mbuf **pm, int how)
    139  1.1  dholland {
    140  1.1  dholland 
    141  1.1  dholland 	return (0);
    142  1.1  dholland }
    143  1.1  dholland #else	/* !__NO_STRICT_ALIGNMENT */
    144  1.1  dholland /*
    145  1.1  dholland  *	newnfs_realign:
    146  1.1  dholland  *
    147  1.1  dholland  *	Check for badly aligned mbuf data and realign by copying the unaligned
    148  1.1  dholland  *	portion of the data into a new mbuf chain and freeing the portions
    149  1.1  dholland  *	of the old chain that were replaced.
    150  1.1  dholland  *
    151  1.1  dholland  *	We cannot simply realign the data within the existing mbuf chain
    152  1.1  dholland  *	because the underlying buffers may contain other rpc commands and
    153  1.1  dholland  *	we cannot afford to overwrite them.
    154  1.1  dholland  *
    155  1.1  dholland  *	We would prefer to avoid this situation entirely.  The situation does
    156  1.1  dholland  *	not occur with NFS/UDP and is supposed to only occassionally occur
    157  1.1  dholland  *	with TCP.  Use vfs.nfs.realign_count and realign_test to check this.
    158  1.1  dholland  *
    159  1.1  dholland  */
    160  1.1  dholland int
    161  1.1  dholland newnfs_realign(struct mbuf **pm, int how)
    162  1.1  dholland {
    163  1.1  dholland 	struct mbuf *m, *n;
    164  1.1  dholland 	int off, space;
    165  1.1  dholland 
    166  1.1  dholland 	++nfs_realign_test;
    167  1.1  dholland 	while ((m = *pm) != NULL) {
    168  1.1  dholland 		if ((m->m_len & 0x3) || (mtod(m, intptr_t) & 0x3)) {
    169  1.1  dholland 			/*
    170  1.1  dholland 			 * NB: we can't depend on m_pkthdr.len to help us
    171  1.1  dholland 			 * decide what to do here.  May not be worth doing
    172  1.1  dholland 			 * the m_length calculation as m_copyback will
    173  1.1  dholland 			 * expand the mbuf chain below as needed.
    174  1.1  dholland 			 */
    175  1.1  dholland 			space = m_length(m, NULL);
    176  1.1  dholland 			if (space >= MINCLSIZE) {
    177  1.1  dholland 				/* NB: m_copyback handles space > MCLBYTES */
    178  1.1  dholland 				n = m_getcl(how, MT_DATA, 0);
    179  1.1  dholland 			} else
    180  1.1  dholland 				n = m_get(how, MT_DATA);
    181  1.1  dholland 			if (n == NULL)
    182  1.1  dholland 				return (ENOMEM);
    183  1.1  dholland 			/*
    184  1.1  dholland 			 * Align the remainder of the mbuf chain.
    185  1.1  dholland 			 */
    186  1.1  dholland 			n->m_len = 0;
    187  1.1  dholland 			off = 0;
    188  1.1  dholland 			while (m != NULL) {
    189  1.1  dholland 				m_copyback(n, off, m->m_len, mtod(m, caddr_t));
    190  1.1  dholland 				off += m->m_len;
    191  1.1  dholland 				m = m->m_next;
    192  1.1  dholland 			}
    193  1.1  dholland 			m_freem(*pm);
    194  1.1  dholland 			*pm = n;
    195  1.1  dholland 			++nfs_realign_count;
    196  1.1  dholland 			break;
    197  1.1  dholland 		}
    198  1.1  dholland 		pm = &m->m_next;
    199  1.1  dholland 	}
    200  1.1  dholland 
    201  1.1  dholland 	return (0);
    202  1.1  dholland }
    203  1.1  dholland #endif	/* __NO_STRICT_ALIGNMENT */
    204  1.1  dholland 
    205  1.1  dholland #ifdef notdef
    206  1.1  dholland static void
    207  1.1  dholland nfsrv_object_create(struct vnode *vp, struct thread *td)
    208  1.1  dholland {
    209  1.1  dholland 
    210  1.1  dholland 	if (vp == NULL || vp->v_type != VREG)
    211  1.1  dholland 		return;
    212  1.1  dholland 	(void) vfs_object_create(vp, td, td->td_ucred);
    213  1.1  dholland }
    214  1.1  dholland #endif
    215  1.1  dholland 
    216  1.1  dholland /*
    217  1.1  dholland  * Look up a file name. Basically just initialize stuff and call namei().
    218  1.1  dholland  */
    219  1.1  dholland int
    220  1.1  dholland nfsrv_lookupfilename(struct nameidata *ndp, char *fname, NFSPROC_T *p)
    221  1.1  dholland {
    222  1.1  dholland 	int error;
    223  1.1  dholland 
    224  1.1  dholland 	NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, fname,
    225  1.1  dholland 	    p);
    226  1.1  dholland 	error = namei(ndp);
    227  1.1  dholland 	if (!error) {
    228  1.1  dholland 		NDFREE(ndp, NDF_ONLY_PNBUF);
    229  1.1  dholland 	}
    230  1.1  dholland 	return (error);
    231  1.1  dholland }
    232  1.1  dholland 
    233  1.1  dholland /*
    234  1.1  dholland  * Copy NFS uid, gids to the cred structure.
    235  1.1  dholland  */
    236  1.1  dholland void
    237  1.1  dholland newnfs_copycred(struct nfscred *nfscr, struct ucred *cr)
    238  1.1  dholland {
    239  1.1  dholland 
    240  1.1  dholland 	KASSERT(nfscr->nfsc_ngroups >= 0,
    241  1.1  dholland 	    ("newnfs_copycred: negative nfsc_ngroups"));
    242  1.1  dholland 	cr->cr_uid = nfscr->nfsc_uid;
    243  1.1  dholland 	crsetgroups(cr, nfscr->nfsc_ngroups, nfscr->nfsc_groups);
    244  1.1  dholland }
    245  1.1  dholland 
    246  1.1  dholland /*
    247  1.1  dholland  * Map args from nfsmsleep() to msleep().
    248  1.1  dholland  */
    249  1.1  dholland int
    250  1.1  dholland nfsmsleep(void *chan, void *mutex, int prio, const char *wmesg,
    251  1.1  dholland     struct timespec *ts)
    252  1.1  dholland {
    253  1.1  dholland 	u_int64_t nsecval;
    254  1.1  dholland 	int error, timeo;
    255  1.1  dholland 
    256  1.1  dholland 	if (ts) {
    257  1.1  dholland 		timeo = hz * ts->tv_sec;
    258  1.1  dholland 		nsecval = (u_int64_t)ts->tv_nsec;
    259  1.1  dholland 		nsecval = ((nsecval * ((u_int64_t)hz)) + 500000000) /
    260  1.1  dholland 		    1000000000;
    261  1.1  dholland 		timeo += (int)nsecval;
    262  1.1  dholland 	} else {
    263  1.1  dholland 		timeo = 0;
    264  1.1  dholland 	}
    265  1.1  dholland 	error = msleep(chan, (struct mtx *)mutex, prio, wmesg, timeo);
    266  1.1  dholland 	return (error);
    267  1.1  dholland }
    268  1.1  dholland 
    269  1.1  dholland /*
    270  1.1  dholland  * Get the file system info for the server. For now, just assume FFS.
    271  1.1  dholland  */
    272  1.1  dholland void
    273  1.1  dholland nfsvno_getfs(struct nfsfsinfo *sip, int isdgram)
    274  1.1  dholland {
    275  1.1  dholland 	int pref;
    276  1.1  dholland 
    277  1.1  dholland 	/*
    278  1.1  dholland 	 * XXX
    279  1.1  dholland 	 * There should be file system VFS OP(s) to get this information.
    280  1.1  dholland 	 * For now, assume ufs.
    281  1.1  dholland 	 */
    282  1.1  dholland 	if (isdgram)
    283  1.1  dholland 		pref = NFS_MAXDGRAMDATA;
    284  1.1  dholland 	else
    285  1.1  dholland 		pref = NFS_MAXDATA;
    286  1.1  dholland 	sip->fs_rtmax = NFS_MAXDATA;
    287  1.1  dholland 	sip->fs_rtpref = pref;
    288  1.1  dholland 	sip->fs_rtmult = NFS_FABLKSIZE;
    289  1.1  dholland 	sip->fs_wtmax = NFS_MAXDATA;
    290  1.1  dholland 	sip->fs_wtpref = pref;
    291  1.1  dholland 	sip->fs_wtmult = NFS_FABLKSIZE;
    292  1.1  dholland 	sip->fs_dtpref = pref;
    293  1.1  dholland 	sip->fs_maxfilesize = 0xffffffffffffffffull;
    294  1.1  dholland 	sip->fs_timedelta.tv_sec = 0;
    295  1.1  dholland 	sip->fs_timedelta.tv_nsec = 1;
    296  1.1  dholland 	sip->fs_properties = (NFSV3FSINFO_LINK |
    297  1.1  dholland 	    NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
    298  1.1  dholland 	    NFSV3FSINFO_CANSETTIME);
    299  1.1  dholland }
    300  1.1  dholland 
    301  1.1  dholland /*
    302  1.1  dholland  * Do the pathconf vnode op.
    303  1.1  dholland  */
    304  1.1  dholland int
    305  1.1  dholland nfsvno_pathconf(struct vnode *vp, int flag, register_t *retf,
    306  1.1  dholland     struct ucred *cred, struct thread *p)
    307  1.1  dholland {
    308  1.1  dholland 	int error;
    309  1.1  dholland 
    310  1.1  dholland 	error = VOP_PATHCONF(vp, flag, retf);
    311  1.1  dholland 	if (error == EOPNOTSUPP || error == EINVAL) {
    312  1.1  dholland 		/*
    313  1.1  dholland 		 * Some file systems return EINVAL for name arguments not
    314  1.1  dholland 		 * supported and some return EOPNOTSUPP for this case.
    315  1.1  dholland 		 * So the NFSv3 Pathconf RPC doesn't fail for these cases,
    316  1.1  dholland 		 * just fake them.
    317  1.1  dholland 		 */
    318  1.1  dholland 		switch (flag) {
    319  1.1  dholland 		case _PC_LINK_MAX:
    320  1.1  dholland 			*retf = LINK_MAX;
    321  1.1  dholland 			break;
    322  1.1  dholland 		case _PC_NAME_MAX:
    323  1.1  dholland 			*retf = NAME_MAX;
    324  1.1  dholland 			break;
    325  1.1  dholland 		case _PC_CHOWN_RESTRICTED:
    326  1.1  dholland 			*retf = 1;
    327  1.1  dholland 			break;
    328  1.1  dholland 		case _PC_NO_TRUNC:
    329  1.1  dholland 			*retf = 1;
    330  1.1  dholland 			break;
    331  1.1  dholland 		default:
    332  1.1  dholland 			/*
    333  1.1  dholland 			 * Only happens if a _PC_xxx is added to the server,
    334  1.1  dholland 			 * but this isn't updated.
    335  1.1  dholland 			 */
    336  1.1  dholland 			*retf = 0;
    337  1.1  dholland 			printf("nfsrvd pathconf flag=%d not supp\n", flag);
    338  1.1  dholland 		};
    339  1.1  dholland 		error = 0;
    340  1.1  dholland 	}
    341  1.1  dholland 	NFSEXITCODE(error);
    342  1.1  dholland 	return (error);
    343  1.1  dholland }
    344  1.1  dholland 
    345  1.1  dholland /* Fake nfsrv_atroot. Just return 0 */
    346  1.1  dholland int
    347  1.1  dholland nfsrv_atroot(struct vnode *vp, long *retp)
    348  1.1  dholland {
    349  1.1  dholland 
    350  1.1  dholland 	return (0);
    351  1.1  dholland }
    352  1.1  dholland 
    353  1.1  dholland /*
    354  1.1  dholland  * Set the credentials to refer to root.
    355  1.1  dholland  * If only the various BSDen could agree on whether cr_gid is a separate
    356  1.1  dholland  * field or cr_groups[0]...
    357  1.1  dholland  */
    358  1.1  dholland void
    359  1.1  dholland newnfs_setroot(struct ucred *cred)
    360  1.1  dholland {
    361  1.1  dholland 
    362  1.1  dholland 	cred->cr_uid = 0;
    363  1.1  dholland 	cred->cr_groups[0] = 0;
    364  1.1  dholland 	cred->cr_ngroups = 1;
    365  1.1  dholland }
    366  1.1  dholland 
    367  1.1  dholland /*
    368  1.1  dholland  * Get the client credential. Used for Renew and recovery.
    369  1.1  dholland  */
    370  1.1  dholland struct ucred *
    371  1.1  dholland newnfs_getcred(void)
    372  1.1  dholland {
    373  1.1  dholland 	struct ucred *cred;
    374  1.1  dholland 	struct thread *td = curthread;
    375  1.1  dholland 
    376  1.1  dholland 	cred = crdup(td->td_ucred);
    377  1.1  dholland 	newnfs_setroot(cred);
    378  1.1  dholland 	return (cred);
    379  1.1  dholland }
    380  1.1  dholland 
    381  1.1  dholland /*
    382  1.1  dholland  * Nfs timer routine
    383  1.1  dholland  * Call the nfsd's timer function once/sec.
    384  1.1  dholland  */
    385  1.1  dholland void
    386  1.1  dholland newnfs_timer(void *arg)
    387  1.1  dholland {
    388  1.1  dholland 	static time_t lasttime = 0;
    389  1.1  dholland 	/*
    390  1.1  dholland 	 * Call the server timer, if set up.
    391  1.1  dholland 	 * The argument indicates if it is the next second and therefore
    392  1.1  dholland 	 * leases should be checked.
    393  1.1  dholland 	 */
    394  1.1  dholland 	if (lasttime != NFSD_MONOSEC) {
    395  1.1  dholland 		lasttime = NFSD_MONOSEC;
    396  1.1  dholland 		if (nfsd_call_servertimer != NULL)
    397  1.1  dholland 			(*nfsd_call_servertimer)();
    398  1.1  dholland 	}
    399  1.1  dholland 	callout_reset(&newnfsd_callout, nfscl_ticks, newnfs_timer, NULL);
    400  1.1  dholland }
    401  1.1  dholland 
    402  1.1  dholland 
    403  1.1  dholland /*
    404  1.1  dholland  * Sleep for a short period of time unless errval == NFSERR_GRACE, where
    405  1.1  dholland  * the sleep should be for 5 seconds.
    406  1.1  dholland  * Since lbolt doesn't exist in FreeBSD-CURRENT, just use a timeout on
    407  1.1  dholland  * an event that never gets a wakeup. Only return EINTR or 0.
    408  1.1  dholland  */
    409  1.1  dholland int
    410  1.1  dholland nfs_catnap(int prio, int errval, const char *wmesg)
    411  1.1  dholland {
    412  1.1  dholland 	static int non_event;
    413  1.1  dholland 	int ret;
    414  1.1  dholland 
    415  1.1  dholland 	if (errval == NFSERR_GRACE)
    416  1.1  dholland 		ret = tsleep(&non_event, prio, wmesg, 5 * hz);
    417  1.1  dholland 	else
    418  1.1  dholland 		ret = tsleep(&non_event, prio, wmesg, 1);
    419  1.1  dholland 	if (ret != EINTR)
    420  1.1  dholland 		ret = 0;
    421  1.1  dholland 	return (ret);
    422  1.1  dholland }
    423  1.1  dholland 
    424  1.1  dholland /*
    425  1.1  dholland  * Get referral. For now, just fail.
    426  1.1  dholland  */
    427  1.1  dholland struct nfsreferral *
    428  1.1  dholland nfsv4root_getreferral(struct vnode *vp, struct vnode *dvp, u_int32_t fileno)
    429  1.1  dholland {
    430  1.1  dholland 
    431  1.1  dholland 	return (NULL);
    432  1.1  dholland }
    433  1.1  dholland 
    434  1.1  dholland static int
    435  1.1  dholland nfssvc_nfscommon(struct thread *td, struct nfssvc_args *uap)
    436  1.1  dholland {
    437  1.1  dholland 	int error;
    438  1.1  dholland 
    439  1.1  dholland 	error = nfssvc_call(td, uap, td->td_ucred);
    440  1.1  dholland 	NFSEXITCODE(error);
    441  1.1  dholland 	return (error);
    442  1.1  dholland }
    443  1.1  dholland 
    444  1.1  dholland static int
    445  1.1  dholland nfssvc_call(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
    446  1.1  dholland {
    447  1.1  dholland 	int error = EINVAL;
    448  1.1  dholland 	struct nfsd_idargs nid;
    449  1.1  dholland 
    450  1.1  dholland 	if (uap->flag & NFSSVC_IDNAME) {
    451  1.1  dholland 		error = copyin(uap->argp, (caddr_t)&nid, sizeof (nid));
    452  1.1  dholland 		if (error)
    453  1.1  dholland 			goto out;
    454  1.1  dholland 		error = nfssvc_idname(&nid);
    455  1.1  dholland 		goto out;
    456  1.1  dholland 	} else if (uap->flag & NFSSVC_GETSTATS) {
    457  1.1  dholland 		error = copyout(&newnfsstats,
    458  1.1  dholland 		    CAST_USER_ADDR_T(uap->argp), sizeof (newnfsstats));
    459  1.1  dholland 		if (error == 0) {
    460  1.1  dholland 			if ((uap->flag & NFSSVC_ZEROCLTSTATS) != 0) {
    461  1.1  dholland 				newnfsstats.attrcache_hits = 0;
    462  1.1  dholland 				newnfsstats.attrcache_misses = 0;
    463  1.1  dholland 				newnfsstats.lookupcache_hits = 0;
    464  1.1  dholland 				newnfsstats.lookupcache_misses = 0;
    465  1.1  dholland 				newnfsstats.direofcache_hits = 0;
    466  1.1  dholland 				newnfsstats.direofcache_misses = 0;
    467  1.1  dholland 				newnfsstats.accesscache_hits = 0;
    468  1.1  dholland 				newnfsstats.accesscache_misses = 0;
    469  1.1  dholland 				newnfsstats.biocache_reads = 0;
    470  1.1  dholland 				newnfsstats.read_bios = 0;
    471  1.1  dholland 				newnfsstats.read_physios = 0;
    472  1.1  dholland 				newnfsstats.biocache_writes = 0;
    473  1.1  dholland 				newnfsstats.write_bios = 0;
    474  1.1  dholland 				newnfsstats.write_physios = 0;
    475  1.1  dholland 				newnfsstats.biocache_readlinks = 0;
    476  1.1  dholland 				newnfsstats.readlink_bios = 0;
    477  1.1  dholland 				newnfsstats.biocache_readdirs = 0;
    478  1.1  dholland 				newnfsstats.readdir_bios = 0;
    479  1.1  dholland 				newnfsstats.rpcretries = 0;
    480  1.1  dholland 				newnfsstats.rpcrequests = 0;
    481  1.1  dholland 				newnfsstats.rpctimeouts = 0;
    482  1.1  dholland 				newnfsstats.rpcunexpected = 0;
    483  1.1  dholland 				newnfsstats.rpcinvalid = 0;
    484  1.1  dholland 				bzero(newnfsstats.rpccnt,
    485  1.1  dholland 				    sizeof(newnfsstats.rpccnt));
    486  1.1  dholland 			}
    487  1.1  dholland 			if ((uap->flag & NFSSVC_ZEROSRVSTATS) != 0) {
    488  1.1  dholland 				newnfsstats.srvrpc_errs = 0;
    489  1.1  dholland 				newnfsstats.srv_errs = 0;
    490  1.1  dholland 				newnfsstats.srvcache_inproghits = 0;
    491  1.1  dholland 				newnfsstats.srvcache_idemdonehits = 0;
    492  1.1  dholland 				newnfsstats.srvcache_nonidemdonehits = 0;
    493  1.1  dholland 				newnfsstats.srvcache_misses = 0;
    494  1.1  dholland 				newnfsstats.srvcache_tcppeak = 0;
    495  1.1  dholland 				newnfsstats.srvclients = 0;
    496  1.1  dholland 				newnfsstats.srvopenowners = 0;
    497  1.1  dholland 				newnfsstats.srvopens = 0;
    498  1.1  dholland 				newnfsstats.srvlockowners = 0;
    499  1.1  dholland 				newnfsstats.srvlocks = 0;
    500  1.1  dholland 				newnfsstats.srvdelegates = 0;
    501  1.1  dholland 				newnfsstats.clopenowners = 0;
    502  1.1  dholland 				newnfsstats.clopens = 0;
    503  1.1  dholland 				newnfsstats.cllockowners = 0;
    504  1.1  dholland 				newnfsstats.cllocks = 0;
    505  1.1  dholland 				newnfsstats.cldelegates = 0;
    506  1.1  dholland 				newnfsstats.cllocalopenowners = 0;
    507  1.1  dholland 				newnfsstats.cllocalopens = 0;
    508  1.1  dholland 				newnfsstats.cllocallockowners = 0;
    509  1.1  dholland 				newnfsstats.cllocallocks = 0;
    510  1.1  dholland 				bzero(newnfsstats.srvrpccnt,
    511  1.1  dholland 				    sizeof(newnfsstats.srvrpccnt));
    512  1.1  dholland 				bzero(newnfsstats.cbrpccnt,
    513  1.1  dholland 				    sizeof(newnfsstats.cbrpccnt));
    514  1.1  dholland 			}
    515  1.1  dholland 		}
    516  1.1  dholland 		goto out;
    517  1.1  dholland 	} else if (uap->flag & NFSSVC_NFSUSERDPORT) {
    518  1.1  dholland 		u_short sockport;
    519  1.1  dholland 
    520  1.1  dholland 		error = copyin(uap->argp, (caddr_t)&sockport,
    521  1.1  dholland 		    sizeof (u_short));
    522  1.1  dholland 		if (!error)
    523  1.1  dholland 			error = nfsrv_nfsuserdport(sockport, p);
    524  1.1  dholland 	} else if (uap->flag & NFSSVC_NFSUSERDDELPORT) {
    525  1.1  dholland 		nfsrv_nfsuserddelport();
    526  1.1  dholland 		error = 0;
    527  1.1  dholland 	}
    528  1.1  dholland 
    529  1.1  dholland out:
    530  1.1  dholland 	NFSEXITCODE(error);
    531  1.1  dholland 	return (error);
    532  1.1  dholland }
    533  1.1  dholland 
    534  1.1  dholland /*
    535  1.1  dholland  * called by all three modevent routines, so that it gets things
    536  1.1  dholland  * initialized soon enough.
    537  1.1  dholland  */
    538  1.1  dholland void
    539  1.1  dholland newnfs_portinit(void)
    540  1.1  dholland {
    541  1.1  dholland 	static int inited = 0;
    542  1.1  dholland 
    543  1.1  dholland 	if (inited)
    544  1.1  dholland 		return;
    545  1.1  dholland 	inited = 1;
    546  1.1  dholland 	/* Initialize SMP locks used by both client and server. */
    547  1.1  dholland 	mtx_init(&newnfsd_mtx, "newnfsd_mtx", NULL, MTX_DEF);
    548  1.1  dholland 	mtx_init(&nfs_state_mutex, "nfs_state_mutex", NULL, MTX_DEF);
    549  1.1  dholland }
    550  1.1  dholland 
    551  1.1  dholland /*
    552  1.1  dholland  * Determine if the file system supports NFSv4 ACLs.
    553  1.1  dholland  * Return 1 if it does, 0 otherwise.
    554  1.1  dholland  */
    555  1.1  dholland int
    556  1.1  dholland nfs_supportsnfsv4acls(struct vnode *vp)
    557  1.1  dholland {
    558  1.1  dholland 	int error;
    559  1.1  dholland 	register_t retval;
    560  1.1  dholland 
    561  1.1  dholland 	ASSERT_VOP_LOCKED(vp, "nfs supports nfsv4acls");
    562  1.1  dholland 
    563  1.1  dholland 	if (nfsrv_useacl == 0)
    564  1.1  dholland 		return (0);
    565  1.1  dholland 	error = VOP_PATHCONF(vp, _PC_ACL_NFS4, &retval);
    566  1.1  dholland 	if (error == 0 && retval != 0)
    567  1.1  dholland 		return (1);
    568  1.1  dholland 	return (0);
    569  1.1  dholland }
    570  1.1  dholland 
    571  1.1  dholland extern int (*nfsd_call_nfscommon)(struct thread *, struct nfssvc_args *);
    572  1.1  dholland 
    573  1.1  dholland /*
    574  1.1  dholland  * Called once to initialize data structures...
    575  1.1  dholland  */
    576  1.1  dholland static int
    577  1.1  dholland nfscommon_modevent(module_t mod, int type, void *data)
    578  1.1  dholland {
    579  1.1  dholland 	int error = 0;
    580  1.1  dholland 	static int loaded = 0;
    581  1.1  dholland 
    582  1.1  dholland 	switch (type) {
    583  1.1  dholland 	case MOD_LOAD:
    584  1.1  dholland 		if (loaded)
    585  1.1  dholland 			goto out;
    586  1.1  dholland 		newnfs_portinit();
    587  1.1  dholland 		mtx_init(&nfs_nameid_mutex, "nfs_nameid_mutex", NULL, MTX_DEF);
    588  1.1  dholland 		mtx_init(&nfs_sockl_mutex, "nfs_sockl_mutex", NULL, MTX_DEF);
    589  1.1  dholland 		mtx_init(&nfs_slock_mutex, "nfs_slock_mutex", NULL, MTX_DEF);
    590  1.1  dholland 		mtx_init(&nfs_req_mutex, "nfs_req_mutex", NULL, MTX_DEF);
    591  1.1  dholland 		mtx_init(&nfsrv_nfsuserdsock.nr_mtx, "nfsuserd", NULL,
    592  1.1  dholland 		    MTX_DEF);
    593  1.1  dholland 		callout_init(&newnfsd_callout, CALLOUT_MPSAFE);
    594  1.1  dholland 		newnfs_init();
    595  1.1  dholland 		nfsd_call_nfscommon = nfssvc_nfscommon;
    596  1.1  dholland 		loaded = 1;
    597  1.1  dholland 		break;
    598  1.1  dholland 
    599  1.1  dholland 	case MOD_UNLOAD:
    600  1.1  dholland 		if (newnfs_numnfsd != 0 || nfsrv_nfsuserd != 0 ||
    601  1.1  dholland 		    nfs_numnfscbd != 0) {
    602  1.1  dholland 			error = EBUSY;
    603  1.1  dholland 			break;
    604  1.1  dholland 		}
    605  1.1  dholland 
    606  1.1  dholland 		nfsd_call_nfscommon = NULL;
    607  1.1  dholland 		callout_drain(&newnfsd_callout);
    608  1.1  dholland 		/* and get rid of the mutexes */
    609  1.1  dholland 		mtx_destroy(&nfs_nameid_mutex);
    610  1.1  dholland 		mtx_destroy(&newnfsd_mtx);
    611  1.1  dholland 		mtx_destroy(&nfs_state_mutex);
    612  1.1  dholland 		mtx_destroy(&nfs_sockl_mutex);
    613  1.1  dholland 		mtx_destroy(&nfs_slock_mutex);
    614  1.1  dholland 		mtx_destroy(&nfs_req_mutex);
    615  1.1  dholland 		mtx_destroy(&nfsrv_nfsuserdsock.nr_mtx);
    616  1.1  dholland 		loaded = 0;
    617  1.1  dholland 		break;
    618  1.1  dholland 	default:
    619  1.1  dholland 		error = EOPNOTSUPP;
    620  1.1  dholland 		break;
    621  1.1  dholland 	}
    622  1.1  dholland 
    623  1.1  dholland out:
    624  1.1  dholland 	NFSEXITCODE(error);
    625  1.1  dholland 	return error;
    626  1.1  dholland }
    627  1.1  dholland static moduledata_t nfscommon_mod = {
    628  1.1  dholland 	"nfscommon",
    629  1.1  dholland 	nfscommon_modevent,
    630  1.1  dholland 	NULL,
    631  1.1  dholland };
    632  1.1  dholland DECLARE_MODULE(nfscommon, nfscommon_mod, SI_SUB_VFS, SI_ORDER_ANY);
    633  1.1  dholland 
    634  1.1  dholland /* So that loader and kldload(2) can find us, wherever we are.. */
    635  1.1  dholland MODULE_VERSION(nfscommon, 1);
    636  1.1  dholland MODULE_DEPEND(nfscommon, nfssvc, 1, 1, 1);
    637  1.1  dholland MODULE_DEPEND(nfscommon, krpc, 1, 1, 1);
    638  1.1  dholland 
    639