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