xref: /freebsd/sys/fs/nfs/nfs_commonport.c (revision 2a243b9539a45b392a515569cab2091844cf2bdf)
1 /*-
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rick Macklem at The University of Guelph.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 /*
38  * Functions that need to be different for different versions of BSD
39  * kernel should be kept here, along with any global storage specific
40  * to this BSD variant.
41  */
42 #include <fs/nfs/nfsport.h>
43 #include <sys/sysctl.h>
44 #include <rpc/rpc_com.h>
45 #include <vm/vm.h>
46 #include <vm/vm_object.h>
47 #include <vm/vm_page.h>
48 #include <vm/vm_param.h>
49 #include <vm/vm_map.h>
50 #include <vm/vm_kern.h>
51 #include <vm/vm_extern.h>
52 #include <vm/uma.h>
53 
54 extern int nfscl_ticks;
55 extern int nfsrv_nfsuserd;
56 extern struct nfssockreq nfsrv_nfsuserdsock;
57 extern void (*nfsd_call_recall)(struct vnode *, int, struct ucred *,
58     struct thread *);
59 extern int nfsrv_useacl;
60 struct mount nfsv4root_mnt;
61 int newnfs_numnfsd = 0;
62 struct nfsstatsv1 nfsstatsv1;
63 int nfs_numnfscbd = 0;
64 int nfscl_debuglevel = 0;
65 char nfsv4_callbackaddr[INET6_ADDRSTRLEN];
66 struct callout newnfsd_callout;
67 int nfsrv_lughashsize = 100;
68 void (*nfsd_call_servertimer)(void) = NULL;
69 void (*ncl_call_invalcaches)(struct vnode *) = NULL;
70 
71 static int nfs_realign_test;
72 static int nfs_realign_count;
73 static struct ext_nfsstats oldnfsstats;
74 
75 SYSCTL_NODE(_vfs, OID_AUTO, nfs, CTLFLAG_RW, 0, "NFS filesystem");
76 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_test, CTLFLAG_RW, &nfs_realign_test,
77     0, "Number of realign tests done");
78 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_count, CTLFLAG_RW, &nfs_realign_count,
79     0, "Number of mbuf realignments done");
80 SYSCTL_STRING(_vfs_nfs, OID_AUTO, callback_addr, CTLFLAG_RW,
81     nfsv4_callbackaddr, sizeof(nfsv4_callbackaddr),
82     "NFSv4 callback addr for server to use");
83 SYSCTL_INT(_vfs_nfs, OID_AUTO, debuglevel, CTLFLAG_RW, &nfscl_debuglevel,
84     0, "Debug level for NFS client");
85 SYSCTL_INT(_vfs_nfs, OID_AUTO, userhashsize, CTLFLAG_RDTUN, &nfsrv_lughashsize,
86     0, "Size of hash tables for uid/name mapping");
87 
88 /*
89  * Defines for malloc
90  * (Here for FreeBSD, since they allocate storage.)
91  */
92 MALLOC_DEFINE(M_NEWNFSRVCACHE, "NFSD srvcache", "NFSD Server Request Cache");
93 MALLOC_DEFINE(M_NEWNFSDCLIENT, "NFSD V4client", "NFSD V4 Client Id");
94 MALLOC_DEFINE(M_NEWNFSDSTATE, "NFSD V4state",
95     "NFSD V4 State (Openowner, Open, Lockowner, Delegation");
96 MALLOC_DEFINE(M_NEWNFSDLOCK, "NFSD V4lock", "NFSD V4 byte range lock");
97 MALLOC_DEFINE(M_NEWNFSDLOCKFILE, "NFSD lckfile", "NFSD Open/Lock file");
98 MALLOC_DEFINE(M_NEWNFSSTRING, "NFSD string", "NFSD V4 long string");
99 MALLOC_DEFINE(M_NEWNFSUSERGROUP, "NFSD usrgroup", "NFSD V4 User/group map");
100 MALLOC_DEFINE(M_NEWNFSDREQ, "NFS req", "NFS request header");
101 MALLOC_DEFINE(M_NEWNFSFH, "NFS fh", "NFS file handle");
102 MALLOC_DEFINE(M_NEWNFSCLOWNER, "NFSCL owner", "NFSCL Open Owner");
103 MALLOC_DEFINE(M_NEWNFSCLOPEN, "NFSCL open", "NFSCL Open");
104 MALLOC_DEFINE(M_NEWNFSCLDELEG, "NFSCL deleg", "NFSCL Delegation");
105 MALLOC_DEFINE(M_NEWNFSCLCLIENT, "NFSCL client", "NFSCL Client");
106 MALLOC_DEFINE(M_NEWNFSCLLOCKOWNER, "NFSCL lckown", "NFSCL Lock Owner");
107 MALLOC_DEFINE(M_NEWNFSCLLOCK, "NFSCL lck", "NFSCL Lock");
108 MALLOC_DEFINE(M_NEWNFSV4NODE, "NEWNFSnode", "NFS vnode");
109 MALLOC_DEFINE(M_NEWNFSDIRECTIO, "NEWdirectio", "NFS Direct IO buffer");
110 MALLOC_DEFINE(M_NEWNFSDIROFF, "NFSCL diroffdiroff",
111     "NFS directory offset data");
112 MALLOC_DEFINE(M_NEWNFSDROLLBACK, "NFSD rollback",
113     "NFS local lock rollback");
114 MALLOC_DEFINE(M_NEWNFSLAYOUT, "NFSCL layout", "NFSv4.1 Layout");
115 MALLOC_DEFINE(M_NEWNFSFLAYOUT, "NFSCL flayout", "NFSv4.1 File Layout");
116 MALLOC_DEFINE(M_NEWNFSDEVINFO, "NFSCL devinfo", "NFSv4.1 Device Info");
117 MALLOC_DEFINE(M_NEWNFSSOCKREQ, "NFSCL sockreq", "NFS Sock Req");
118 MALLOC_DEFINE(M_NEWNFSCLDS, "NFSCL session", "NFSv4.1 Session");
119 MALLOC_DEFINE(M_NEWNFSLAYRECALL, "NFSCL layrecall", "NFSv4.1 Layout Recall");
120 MALLOC_DEFINE(M_NEWNFSDSESSION, "NFSD session", "NFSD Session for a client");
121 
122 /*
123  * Definition of mutex locks.
124  * newnfsd_mtx is used in nfsrvd_nfsd() to protect the nfs socket list
125  * and assorted other nfsd structures.
126  */
127 struct mtx newnfsd_mtx;
128 struct mtx nfs_sockl_mutex;
129 struct mtx nfs_state_mutex;
130 struct mtx nfs_nameid_mutex;
131 struct mtx nfs_req_mutex;
132 struct mtx nfs_slock_mutex;
133 struct mtx nfs_clstate_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, uint64_t *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 				bzero(nfsstatsv1.srvrpccnt,
614 				    sizeof(nfsstatsv1.srvrpccnt));
615 				bzero(nfsstatsv1.cbrpccnt,
616 				    sizeof(nfsstatsv1.cbrpccnt));
617 			}
618 		}
619 		goto out;
620 	} else if (uap->flag & NFSSVC_NFSUSERDPORT) {
621 		u_short sockport;
622 		struct sockaddr *sad;
623 		struct sockaddr_un *sun;
624 
625 		if ((uap->flag & NFSSVC_NEWSTRUCT) != 0) {
626 			/* New nfsuserd using an AF_LOCAL socket. */
627 			sun = malloc(sizeof(struct sockaddr_un), M_SONAME,
628 			    M_WAITOK | M_ZERO);
629 			error = copyinstr(uap->argp, sun->sun_path,
630 			    sizeof(sun->sun_path), NULL);
631 			if (error != 0) {
632 				free(sun, M_SONAME);
633 				return (error);
634 			}
635 		        sun->sun_family = AF_LOCAL;
636 		        sun->sun_len = SUN_LEN(sun);
637 			sockport = 0;
638 			sad = (struct sockaddr *)sun;
639 		} else {
640 			error = copyin(uap->argp, (caddr_t)&sockport,
641 			    sizeof (u_short));
642 			sad = NULL;
643 		}
644 		if (error == 0)
645 			error = nfsrv_nfsuserdport(sad, sockport, p);
646 	} else if (uap->flag & NFSSVC_NFSUSERDDELPORT) {
647 		nfsrv_nfsuserddelport();
648 		error = 0;
649 	}
650 
651 out:
652 	NFSEXITCODE(error);
653 	return (error);
654 }
655 
656 /*
657  * called by all three modevent routines, so that it gets things
658  * initialized soon enough.
659  */
660 void
661 newnfs_portinit(void)
662 {
663 	static int inited = 0;
664 
665 	if (inited)
666 		return;
667 	inited = 1;
668 	/* Initialize SMP locks used by both client and server. */
669 	mtx_init(&newnfsd_mtx, "newnfsd_mtx", NULL, MTX_DEF);
670 	mtx_init(&nfs_state_mutex, "nfs_state_mutex", NULL, MTX_DEF);
671 	mtx_init(&nfs_clstate_mutex, "nfs_clstate_mutex", NULL, MTX_DEF);
672 }
673 
674 /*
675  * Determine if the file system supports NFSv4 ACLs.
676  * Return 1 if it does, 0 otherwise.
677  */
678 int
679 nfs_supportsnfsv4acls(struct vnode *vp)
680 {
681 	int error;
682 	register_t retval;
683 
684 	ASSERT_VOP_LOCKED(vp, "nfs supports nfsv4acls");
685 
686 	if (nfsrv_useacl == 0)
687 		return (0);
688 	error = VOP_PATHCONF(vp, _PC_ACL_NFS4, &retval);
689 	if (error == 0 && retval != 0)
690 		return (1);
691 	return (0);
692 }
693 
694 extern int (*nfsd_call_nfscommon)(struct thread *, struct nfssvc_args *);
695 
696 /*
697  * Called once to initialize data structures...
698  */
699 static int
700 nfscommon_modevent(module_t mod, int type, void *data)
701 {
702 	int error = 0;
703 	static int loaded = 0;
704 
705 	switch (type) {
706 	case MOD_LOAD:
707 		if (loaded)
708 			goto out;
709 		newnfs_portinit();
710 		mtx_init(&nfs_nameid_mutex, "nfs_nameid_mutex", NULL, MTX_DEF);
711 		mtx_init(&nfs_sockl_mutex, "nfs_sockl_mutex", NULL, MTX_DEF);
712 		mtx_init(&nfs_slock_mutex, "nfs_slock_mutex", NULL, MTX_DEF);
713 		mtx_init(&nfs_req_mutex, "nfs_req_mutex", NULL, MTX_DEF);
714 		mtx_init(&nfsrv_nfsuserdsock.nr_mtx, "nfsuserd", NULL,
715 		    MTX_DEF);
716 		callout_init(&newnfsd_callout, 1);
717 		newnfs_init();
718 		nfsd_call_nfscommon = nfssvc_nfscommon;
719 		loaded = 1;
720 		break;
721 
722 	case MOD_UNLOAD:
723 		if (newnfs_numnfsd != 0 || nfsrv_nfsuserd != 0 ||
724 		    nfs_numnfscbd != 0) {
725 			error = EBUSY;
726 			break;
727 		}
728 
729 		nfsd_call_nfscommon = NULL;
730 		callout_drain(&newnfsd_callout);
731 		/* Clean out the name<-->id cache. */
732 		nfsrv_cleanusergroup();
733 		/* and get rid of the mutexes */
734 		mtx_destroy(&nfs_nameid_mutex);
735 		mtx_destroy(&newnfsd_mtx);
736 		mtx_destroy(&nfs_state_mutex);
737 		mtx_destroy(&nfs_clstate_mutex);
738 		mtx_destroy(&nfs_sockl_mutex);
739 		mtx_destroy(&nfs_slock_mutex);
740 		mtx_destroy(&nfs_req_mutex);
741 		mtx_destroy(&nfsrv_nfsuserdsock.nr_mtx);
742 		loaded = 0;
743 		break;
744 	default:
745 		error = EOPNOTSUPP;
746 		break;
747 	}
748 
749 out:
750 	NFSEXITCODE(error);
751 	return error;
752 }
753 static moduledata_t nfscommon_mod = {
754 	"nfscommon",
755 	nfscommon_modevent,
756 	NULL,
757 };
758 DECLARE_MODULE(nfscommon, nfscommon_mod, SI_SUB_VFS, SI_ORDER_ANY);
759 
760 /* So that loader and kldload(2) can find us, wherever we are.. */
761 MODULE_VERSION(nfscommon, 1);
762 MODULE_DEPEND(nfscommon, nfssvc, 1, 1, 1);
763 MODULE_DEPEND(nfscommon, krpc, 1, 1, 1);
764 
765