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