xref: /freebsd/sys/fs/nfs/nfs_commonport.c (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Rick Macklem at The University of Guelph.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  */
35 
36 #include <sys/cdefs.h>
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 void (*nfsd_call_recall)(struct vnode *, int, struct ucred *,
58     struct thread *);
59 extern int nfsrv_useacl;
60 int newnfs_numnfsd = 0;
61 struct nfsstatsv1 nfsstatsv1;
62 int nfs_numnfscbd = 0;
63 int nfscl_debuglevel = 0;
64 char nfsv4_callbackaddr[INET6_ADDRSTRLEN];
65 int nfsrv_lughashsize = 100;
66 struct mtx nfsrv_dslock_mtx;
67 struct nfsdevicehead nfsrv_devidhead;
68 volatile int nfsrv_devidcnt = 0;
69 void (*ncl_call_invalcaches)(struct vnode *) = NULL;
70 vop_advlock_t *nfs_advlock_p = NULL;
71 vop_reclaim_t *nfs_reclaim_p = NULL;
72 uint32_t nfs_srvmaxio = NFS_SRVMAXIO;
73 
74 NFSD_VNET_DEFINE(struct nfsstatsv1 *, nfsstatsv1_p);
75 
76 NFSD_VNET_DECLARE(struct nfssockreq, nfsrv_nfsuserdsock);
77 NFSD_VNET_DECLARE(nfsuserd_state, nfsrv_nfsuserd);
78 
79 int nfs_pnfsio(task_fn_t *, void *);
80 
81 static int nfs_realign_test;
82 static int nfs_realign_count;
83 static struct ext_nfsstats oldnfsstats;
84 static struct nfsstatsov1 nfsstatsov1;
85 
86 SYSCTL_NODE(_vfs, OID_AUTO, nfs, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
87     "NFS filesystem");
88 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_test, CTLFLAG_RW, &nfs_realign_test,
89     0, "Number of realign tests done");
90 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_count, CTLFLAG_RW, &nfs_realign_count,
91     0, "Number of mbuf realignments done");
92 SYSCTL_STRING(_vfs_nfs, OID_AUTO, callback_addr, CTLFLAG_RW,
93     nfsv4_callbackaddr, sizeof(nfsv4_callbackaddr),
94     "NFSv4 callback addr for server to use");
95 SYSCTL_INT(_vfs_nfs, OID_AUTO, debuglevel, CTLFLAG_RW, &nfscl_debuglevel,
96     0, "Debug level for NFS client");
97 SYSCTL_INT(_vfs_nfs, OID_AUTO, userhashsize, CTLFLAG_RDTUN, &nfsrv_lughashsize,
98     0, "Size of hash tables for uid/name mapping");
99 int nfs_pnfsiothreads = -1;
100 SYSCTL_INT(_vfs_nfs, OID_AUTO, pnfsiothreads, CTLFLAG_RW, &nfs_pnfsiothreads,
101     0, "Number of pNFS mirror I/O threads");
102 
103 /*
104  * Defines for malloc
105  * (Here for FreeBSD, since they allocate storage.)
106  */
107 MALLOC_DEFINE(M_NEWNFSRVCACHE, "NFSD srvcache", "NFSD Server Request Cache");
108 MALLOC_DEFINE(M_NEWNFSDCLIENT, "NFSD V4client", "NFSD V4 Client Id");
109 MALLOC_DEFINE(M_NEWNFSDSTATE, "NFSD V4state",
110     "NFSD V4 State (Openowner, Open, Lockowner, Delegation");
111 MALLOC_DEFINE(M_NEWNFSDLOCK, "NFSD V4lock", "NFSD V4 byte range lock");
112 MALLOC_DEFINE(M_NEWNFSDLOCKFILE, "NFSD lckfile", "NFSD Open/Lock file");
113 MALLOC_DEFINE(M_NEWNFSSTRING, "NFSD string", "NFSD V4 long string");
114 MALLOC_DEFINE(M_NEWNFSUSERGROUP, "NFSD usrgroup", "NFSD V4 User/group map");
115 MALLOC_DEFINE(M_NEWNFSDREQ, "NFS req", "NFS request header");
116 MALLOC_DEFINE(M_NEWNFSFH, "NFS fh", "NFS file handle");
117 MALLOC_DEFINE(M_NEWNFSCLOWNER, "NFSCL owner", "NFSCL Open Owner");
118 MALLOC_DEFINE(M_NEWNFSCLOPEN, "NFSCL open", "NFSCL Open");
119 MALLOC_DEFINE(M_NEWNFSCLDELEG, "NFSCL deleg", "NFSCL Delegation");
120 MALLOC_DEFINE(M_NEWNFSCLCLIENT, "NFSCL client", "NFSCL Client");
121 MALLOC_DEFINE(M_NEWNFSCLLOCKOWNER, "NFSCL lckown", "NFSCL Lock Owner");
122 MALLOC_DEFINE(M_NEWNFSCLLOCK, "NFSCL lck", "NFSCL Lock");
123 MALLOC_DEFINE(M_NEWNFSV4NODE, "NEWNFSnode", "NFS vnode");
124 MALLOC_DEFINE(M_NEWNFSDIROFF, "NFSCL diroff",
125     "NFS directory offset data");
126 MALLOC_DEFINE(M_NEWNFSDROLLBACK, "NFSD rollback",
127     "NFS local lock rollback");
128 MALLOC_DEFINE(M_NEWNFSLAYOUT, "NFSCL layout", "NFSv4.1 Layout");
129 MALLOC_DEFINE(M_NEWNFSFLAYOUT, "NFSCL flayout", "NFSv4.1 File Layout");
130 MALLOC_DEFINE(M_NEWNFSDEVINFO, "NFSCL devinfo", "NFSv4.1 Device Info");
131 MALLOC_DEFINE(M_NEWNFSSOCKREQ, "NFSCL sockreq", "NFS Sock Req");
132 MALLOC_DEFINE(M_NEWNFSCLDS, "NFSCL session", "NFSv4.1 Session");
133 MALLOC_DEFINE(M_NEWNFSLAYRECALL, "NFSCL layrecall", "NFSv4.1 Layout Recall");
134 MALLOC_DEFINE(M_NEWNFSDSESSION, "NFSD session", "NFSD Session for a client");
135 
136 /*
137  * Definition of mutex locks.
138  * newnfsd_mtx is used in nfsrvd_nfsd() to protect the nfs socket list
139  * and assorted other nfsd structures.
140  */
141 struct mtx newnfsd_mtx;
142 struct mtx nfs_sockl_mutex;
143 struct mtx nfs_state_mutex;
144 struct mtx nfs_nameid_mutex;
145 struct mtx nfs_req_mutex;
146 struct mtx nfs_slock_mutex;
147 struct mtx nfs_clstate_mutex;
148 
149 /* local functions */
150 static int nfssvc_call(struct thread *, struct nfssvc_args *, struct ucred *);
151 
152 #ifdef __NO_STRICT_ALIGNMENT
153 /*
154  * These architectures don't need re-alignment, so just return.
155  */
156 int
157 newnfs_realign(struct mbuf **pm, int how)
158 {
159 
160 	return (0);
161 }
162 #else	/* !__NO_STRICT_ALIGNMENT */
163 /*
164  *	newnfs_realign:
165  *
166  *	Check for badly aligned mbuf data and realign by copying the unaligned
167  *	portion of the data into a new mbuf chain and freeing the portions
168  *	of the old chain that were replaced.
169  *
170  *	We cannot simply realign the data within the existing mbuf chain
171  *	because the underlying buffers may contain other rpc commands and
172  *	we cannot afford to overwrite them.
173  *
174  *	We would prefer to avoid this situation entirely.  The situation does
175  *	not occur with NFS/UDP and is supposed to only occasionally occur
176  *	with TCP.  Use vfs.nfs.realign_count and realign_test to check this.
177  *
178  */
179 int
180 newnfs_realign(struct mbuf **pm, int how)
181 {
182 	struct mbuf *m, *n;
183 	int off, space;
184 
185 	++nfs_realign_test;
186 	while ((m = *pm) != NULL) {
187 		if ((m->m_len & 0x3) || (mtod(m, intptr_t) & 0x3)) {
188 			/*
189 			 * NB: we can't depend on m_pkthdr.len to help us
190 			 * decide what to do here.  May not be worth doing
191 			 * the m_length calculation as m_copyback will
192 			 * expand the mbuf chain below as needed.
193 			 */
194 			space = m_length(m, NULL);
195 			if (space >= MINCLSIZE) {
196 				/* NB: m_copyback handles space > MCLBYTES */
197 				n = m_getcl(how, MT_DATA, 0);
198 			} else
199 				n = m_get(how, MT_DATA);
200 			if (n == NULL)
201 				return (ENOMEM);
202 			/*
203 			 * Align the remainder of the mbuf chain.
204 			 */
205 			n->m_len = 0;
206 			off = 0;
207 			while (m != NULL) {
208 				m_copyback(n, off, m->m_len, mtod(m, caddr_t));
209 				off += m->m_len;
210 				m = m->m_next;
211 			}
212 			m_freem(*pm);
213 			*pm = n;
214 			++nfs_realign_count;
215 			break;
216 		}
217 		pm = &m->m_next;
218 	}
219 
220 	return (0);
221 }
222 #endif	/* __NO_STRICT_ALIGNMENT */
223 
224 #ifdef notdef
225 static void
226 nfsrv_object_create(struct vnode *vp, struct thread *td)
227 {
228 
229 	if (vp == NULL || vp->v_type != VREG)
230 		return;
231 	(void) vfs_object_create(vp, td, td->td_ucred);
232 }
233 #endif
234 
235 /*
236  * Look up a file name. Basically just initialize stuff and call namei().
237  */
238 int
239 nfsrv_lookupfilename(struct nameidata *ndp, char *fname, NFSPROC_T *p __unused)
240 {
241 	int error;
242 
243 	NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, fname);
244 	error = namei(ndp);
245 	if (!error) {
246 		NDFREE_PNBUF(ndp);
247 	}
248 	return (error);
249 }
250 
251 /*
252  * Copy NFS uid, gids to the cred structure.
253  */
254 void
255 newnfs_copycred(struct nfscred *nfscr, struct ucred *cr)
256 {
257 
258 	KASSERT(nfscr->nfsc_ngroups >= 0,
259 	    ("newnfs_copycred: negative nfsc_ngroups"));
260 	cr->cr_uid = nfscr->nfsc_uid;
261 	crsetgroups(cr, nfscr->nfsc_ngroups, nfscr->nfsc_groups);
262 }
263 
264 /*
265  * Map args from nfsmsleep() to msleep().
266  */
267 int
268 nfsmsleep(void *chan, void *mutex, int prio, const char *wmesg,
269     struct timespec *ts)
270 {
271 	u_int64_t nsecval;
272 	int error, timeo;
273 
274 	if (ts) {
275 		timeo = hz * ts->tv_sec;
276 		nsecval = (u_int64_t)ts->tv_nsec;
277 		nsecval = ((nsecval * ((u_int64_t)hz)) + 500000000) /
278 		    1000000000;
279 		timeo += (int)nsecval;
280 	} else {
281 		timeo = 0;
282 	}
283 	error = msleep(chan, (struct mtx *)mutex, prio, wmesg, timeo);
284 	return (error);
285 }
286 
287 /*
288  * Get the file system info for the server. For now, just assume FFS.
289  */
290 void
291 nfsvno_getfs(struct nfsfsinfo *sip, int isdgram)
292 {
293 	int pref;
294 
295 	/*
296 	 * XXX
297 	 * There should be file system VFS OP(s) to get this information.
298 	 * For now, assume ufs.
299 	 */
300 	if (isdgram)
301 		pref = NFS_MAXDGRAMDATA;
302 	else
303 		pref = nfs_srvmaxio;
304 	sip->fs_rtmax = nfs_srvmaxio;
305 	sip->fs_rtpref = pref;
306 	sip->fs_rtmult = NFS_FABLKSIZE;
307 	sip->fs_wtmax = nfs_srvmaxio;
308 	sip->fs_wtpref = pref;
309 	sip->fs_wtmult = NFS_FABLKSIZE;
310 	sip->fs_dtpref = pref;
311 	sip->fs_maxfilesize = 0xffffffffffffffffull;
312 	sip->fs_timedelta.tv_sec = 0;
313 	sip->fs_timedelta.tv_nsec = 1;
314 	sip->fs_properties = (NFSV3FSINFO_LINK |
315 	    NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
316 	    NFSV3FSINFO_CANSETTIME);
317 }
318 
319 /*
320  * Do the pathconf vnode op.
321  */
322 int
323 nfsvno_pathconf(struct vnode *vp, int flag, long *retf,
324     struct ucred *cred, struct thread *p)
325 {
326 	int error;
327 
328 	error = VOP_PATHCONF(vp, flag, retf);
329 	if (error == EOPNOTSUPP || error == EINVAL) {
330 		/*
331 		 * Some file systems return EINVAL for name arguments not
332 		 * supported and some return EOPNOTSUPP for this case.
333 		 * So the NFSv3 Pathconf RPC doesn't fail for these cases,
334 		 * just fake them.
335 		 */
336 		switch (flag) {
337 		case _PC_LINK_MAX:
338 			*retf = NFS_LINK_MAX;
339 			break;
340 		case _PC_NAME_MAX:
341 			*retf = NAME_MAX;
342 			break;
343 		case _PC_CHOWN_RESTRICTED:
344 			*retf = 1;
345 			break;
346 		case _PC_NO_TRUNC:
347 			*retf = 1;
348 			break;
349 		default:
350 			/*
351 			 * Only happens if a _PC_xxx is added to the server,
352 			 * but this isn't updated.
353 			 */
354 			*retf = 0;
355 			printf("nfsrvd pathconf flag=%d not supp\n", flag);
356 		}
357 		error = 0;
358 	}
359 	NFSEXITCODE(error);
360 	return (error);
361 }
362 
363 /* Fake nfsrv_atroot. Just return 0 */
364 int
365 nfsrv_atroot(struct vnode *vp, uint64_t *retp)
366 {
367 
368 	return (0);
369 }
370 
371 /*
372  * Set the credentials to refer to root.
373  * If only the various BSDen could agree on whether cr_gid is a separate
374  * field or cr_groups[0]...
375  */
376 void
377 newnfs_setroot(struct ucred *cred)
378 {
379 
380 	cred->cr_uid = 0;
381 	cred->cr_groups[0] = 0;
382 	cred->cr_ngroups = 1;
383 }
384 
385 /*
386  * Get the client credential. Used for Renew and recovery.
387  */
388 struct ucred *
389 newnfs_getcred(void)
390 {
391 	struct ucred *cred;
392 	struct thread *td = curthread;
393 
394 	cred = crdup(td->td_ucred);
395 	newnfs_setroot(cred);
396 	return (cred);
397 }
398 
399 /*
400  * Sleep for a short period of time unless errval == NFSERR_GRACE, where
401  * the sleep should be for 5 seconds.
402  * Since lbolt doesn't exist in FreeBSD-CURRENT, just use a timeout on
403  * an event that never gets a wakeup. Only return EINTR or 0.
404  */
405 int
406 nfs_catnap(int prio, int errval, const char *wmesg)
407 {
408 	static int non_event;
409 	int ret;
410 
411 	if (errval == NFSERR_GRACE)
412 		ret = tsleep(&non_event, prio, wmesg, 5 * hz);
413 	else
414 		ret = tsleep(&non_event, prio, wmesg, 1);
415 	if (ret != EINTR)
416 		ret = 0;
417 	return (ret);
418 }
419 
420 /*
421  * Get referral. For now, just fail.
422  */
423 struct nfsreferral *
424 nfsv4root_getreferral(struct vnode *vp, struct vnode *dvp, u_int32_t fileno)
425 {
426 
427 	return (NULL);
428 }
429 
430 static int
431 nfssvc_nfscommon(struct thread *td, struct nfssvc_args *uap)
432 {
433 	int error;
434 
435 	NFSD_CURVNET_SET(NFSD_TD_TO_VNET(td));
436 	error = nfssvc_call(td, uap, td->td_ucred);
437 	NFSD_CURVNET_RESTORE();
438 	NFSEXITCODE(error);
439 	return (error);
440 }
441 
442 static int
443 nfssvc_call(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
444 {
445 	int error = EINVAL, i, j;
446 	struct nfsd_idargs nid;
447 	struct nfsd_oidargs onid;
448 	struct {
449 		int vers;	/* Just the first field of nfsstats. */
450 	} nfsstatver;
451 
452 	if (uap->flag & NFSSVC_IDNAME) {
453 		if ((uap->flag & NFSSVC_NEWSTRUCT) != 0)
454 			error = copyin(uap->argp, &nid, sizeof(nid));
455 		else {
456 			error = copyin(uap->argp, &onid, sizeof(onid));
457 			if (error == 0) {
458 				nid.nid_flag = onid.nid_flag;
459 				nid.nid_uid = onid.nid_uid;
460 				nid.nid_gid = onid.nid_gid;
461 				nid.nid_usermax = onid.nid_usermax;
462 				nid.nid_usertimeout = onid.nid_usertimeout;
463 				nid.nid_name = onid.nid_name;
464 				nid.nid_namelen = onid.nid_namelen;
465 				nid.nid_ngroup = 0;
466 				nid.nid_grps = NULL;
467 			}
468 		}
469 		if (error)
470 			goto out;
471 		error = nfssvc_idname(&nid);
472 		goto out;
473 	} else if (uap->flag & NFSSVC_GETSTATS) {
474 		if ((uap->flag & NFSSVC_NEWSTRUCT) == 0) {
475 			/* Copy fields to the old ext_nfsstat structure. */
476 			oldnfsstats.attrcache_hits =
477 			    NFSD_VNET(nfsstatsv1_p)->attrcache_hits;
478 			oldnfsstats.attrcache_misses =
479 			    NFSD_VNET(nfsstatsv1_p)->attrcache_misses;
480 			oldnfsstats.lookupcache_hits =
481 			    NFSD_VNET(nfsstatsv1_p)->lookupcache_hits;
482 			oldnfsstats.lookupcache_misses =
483 			    NFSD_VNET(nfsstatsv1_p)->lookupcache_misses;
484 			oldnfsstats.direofcache_hits =
485 			    NFSD_VNET(nfsstatsv1_p)->direofcache_hits;
486 			oldnfsstats.direofcache_misses =
487 			    NFSD_VNET(nfsstatsv1_p)->direofcache_misses;
488 			oldnfsstats.accesscache_hits =
489 			    NFSD_VNET(nfsstatsv1_p)->accesscache_hits;
490 			oldnfsstats.accesscache_misses =
491 			    NFSD_VNET(nfsstatsv1_p)->accesscache_misses;
492 			oldnfsstats.biocache_reads =
493 			    NFSD_VNET(nfsstatsv1_p)->biocache_reads;
494 			oldnfsstats.read_bios =
495 			    NFSD_VNET(nfsstatsv1_p)->read_bios;
496 			oldnfsstats.read_physios =
497 			    NFSD_VNET(nfsstatsv1_p)->read_physios;
498 			oldnfsstats.biocache_writes =
499 			    NFSD_VNET(nfsstatsv1_p)->biocache_writes;
500 			oldnfsstats.write_bios =
501 			    NFSD_VNET(nfsstatsv1_p)->write_bios;
502 			oldnfsstats.write_physios =
503 			    NFSD_VNET(nfsstatsv1_p)->write_physios;
504 			oldnfsstats.biocache_readlinks =
505 			    NFSD_VNET(nfsstatsv1_p)->biocache_readlinks;
506 			oldnfsstats.readlink_bios =
507 			    NFSD_VNET(nfsstatsv1_p)->readlink_bios;
508 			oldnfsstats.biocache_readdirs =
509 			    NFSD_VNET(nfsstatsv1_p)->biocache_readdirs;
510 			oldnfsstats.readdir_bios =
511 			    NFSD_VNET(nfsstatsv1_p)->readdir_bios;
512 			for (i = 0; i < NFSV4_NPROCS; i++)
513 				oldnfsstats.rpccnt[i] =
514 				    NFSD_VNET(nfsstatsv1_p)->rpccnt[i];
515 			oldnfsstats.rpcretries =
516 			    NFSD_VNET(nfsstatsv1_p)->rpcretries;
517 			for (i = 0; i < NFSV4OP_NOPS; i++)
518 				oldnfsstats.srvrpccnt[i] =
519 				    NFSD_VNET(nfsstatsv1_p)->srvrpccnt[i];
520 			for (i = NFSV42_NOPS, j = NFSV4OP_NOPS;
521 			    i < NFSV42_NOPS + NFSV4OP_FAKENOPS; i++, j++)
522 				oldnfsstats.srvrpccnt[j] =
523 				    NFSD_VNET(nfsstatsv1_p)->srvrpccnt[i];
524 			oldnfsstats.reserved_0 = 0;
525 			oldnfsstats.reserved_1 = 0;
526 			oldnfsstats.rpcrequests =
527 			    NFSD_VNET(nfsstatsv1_p)->rpcrequests;
528 			oldnfsstats.rpctimeouts =
529 			    NFSD_VNET(nfsstatsv1_p)->rpctimeouts;
530 			oldnfsstats.rpcunexpected =
531 			    NFSD_VNET(nfsstatsv1_p)->rpcunexpected;
532 			oldnfsstats.rpcinvalid =
533 			    NFSD_VNET(nfsstatsv1_p)->rpcinvalid;
534 			oldnfsstats.srvcache_inproghits =
535 			    NFSD_VNET(nfsstatsv1_p)->srvcache_inproghits;
536 			oldnfsstats.reserved_2 = 0;
537 			oldnfsstats.srvcache_nonidemdonehits =
538 			    NFSD_VNET(nfsstatsv1_p)->srvcache_nonidemdonehits;
539 			oldnfsstats.srvcache_misses =
540 			    NFSD_VNET(nfsstatsv1_p)->srvcache_misses;
541 			oldnfsstats.srvcache_tcppeak =
542 			    NFSD_VNET(nfsstatsv1_p)->srvcache_tcppeak;
543 			oldnfsstats.srvcache_size =
544 			    NFSD_VNET(nfsstatsv1_p)->srvcache_size;
545 			oldnfsstats.srvclients =
546 			    NFSD_VNET(nfsstatsv1_p)->srvclients;
547 			oldnfsstats.srvopenowners =
548 			    NFSD_VNET(nfsstatsv1_p)->srvopenowners;
549 			oldnfsstats.srvopens =
550 			    NFSD_VNET(nfsstatsv1_p)->srvopens;
551 			oldnfsstats.srvlockowners =
552 			    NFSD_VNET(nfsstatsv1_p)->srvlockowners;
553 			oldnfsstats.srvlocks =
554 			    NFSD_VNET(nfsstatsv1_p)->srvlocks;
555 			oldnfsstats.srvdelegates =
556 			    NFSD_VNET(nfsstatsv1_p)->srvdelegates;
557 			for (i = 0; i < NFSV4OP_CBNOPS; i++)
558 				oldnfsstats.cbrpccnt[i] =
559 				    NFSD_VNET(nfsstatsv1_p)->cbrpccnt[i];
560 			oldnfsstats.clopenowners =
561 			    NFSD_VNET(nfsstatsv1_p)->clopenowners;
562 			oldnfsstats.clopens = NFSD_VNET(nfsstatsv1_p)->clopens;
563 			oldnfsstats.cllockowners =
564 			    NFSD_VNET(nfsstatsv1_p)->cllockowners;
565 			oldnfsstats.cllocks = NFSD_VNET(nfsstatsv1_p)->cllocks;
566 			oldnfsstats.cldelegates =
567 			    NFSD_VNET(nfsstatsv1_p)->cldelegates;
568 			oldnfsstats.cllocalopenowners =
569 			    NFSD_VNET(nfsstatsv1_p)->cllocalopenowners;
570 			oldnfsstats.cllocalopens =
571 			    NFSD_VNET(nfsstatsv1_p)->cllocalopens;
572 			oldnfsstats.cllocallockowners =
573 			    NFSD_VNET(nfsstatsv1_p)->cllocallockowners;
574 			oldnfsstats.cllocallocks =
575 			    NFSD_VNET(nfsstatsv1_p)->cllocallocks;
576 			error = copyout(&oldnfsstats, uap->argp,
577 			    sizeof (oldnfsstats));
578 		} else {
579 			error = copyin(uap->argp, &nfsstatver,
580 			    sizeof(nfsstatver));
581 			if (error == 0) {
582 				if (nfsstatver.vers == NFSSTATS_OV1) {
583 					/* Copy nfsstatsv1 to nfsstatsov1. */
584 					nfsstatsov1.attrcache_hits =
585 					    NFSD_VNET(nfsstatsv1_p)->attrcache_hits;
586 					nfsstatsov1.attrcache_misses =
587 					    NFSD_VNET(nfsstatsv1_p)->attrcache_misses;
588 					nfsstatsov1.lookupcache_hits =
589 					    NFSD_VNET(nfsstatsv1_p)->lookupcache_hits;
590 					nfsstatsov1.lookupcache_misses =
591 					    NFSD_VNET(nfsstatsv1_p)->lookupcache_misses;
592 					nfsstatsov1.direofcache_hits =
593 					    NFSD_VNET(nfsstatsv1_p)->direofcache_hits;
594 					nfsstatsov1.direofcache_misses =
595 					    NFSD_VNET(nfsstatsv1_p)->direofcache_misses;
596 					nfsstatsov1.accesscache_hits =
597 					    NFSD_VNET(nfsstatsv1_p)->accesscache_hits;
598 					nfsstatsov1.accesscache_misses =
599 					    NFSD_VNET(nfsstatsv1_p)->accesscache_misses;
600 					nfsstatsov1.biocache_reads =
601 					    NFSD_VNET(nfsstatsv1_p)->biocache_reads;
602 					nfsstatsov1.read_bios =
603 					    NFSD_VNET(nfsstatsv1_p)->read_bios;
604 					nfsstatsov1.read_physios =
605 					    NFSD_VNET(nfsstatsv1_p)->read_physios;
606 					nfsstatsov1.biocache_writes =
607 					    NFSD_VNET(nfsstatsv1_p)->biocache_writes;
608 					nfsstatsov1.write_bios =
609 					    NFSD_VNET(nfsstatsv1_p)->write_bios;
610 					nfsstatsov1.write_physios =
611 					    NFSD_VNET(nfsstatsv1_p)->write_physios;
612 					nfsstatsov1.biocache_readlinks =
613 					    NFSD_VNET(nfsstatsv1_p)->biocache_readlinks;
614 					nfsstatsov1.readlink_bios =
615 					    NFSD_VNET(nfsstatsv1_p)->readlink_bios;
616 					nfsstatsov1.biocache_readdirs =
617 					    NFSD_VNET(nfsstatsv1_p)->biocache_readdirs;
618 					nfsstatsov1.readdir_bios =
619 					    NFSD_VNET(nfsstatsv1_p)->readdir_bios;
620 					for (i = 0; i < NFSV42_OLDNPROCS; i++)
621 						nfsstatsov1.rpccnt[i] =
622 						    NFSD_VNET(nfsstatsv1_p)->rpccnt[i];
623 					nfsstatsov1.rpcretries =
624 					    NFSD_VNET(nfsstatsv1_p)->rpcretries;
625 					for (i = 0; i < NFSV42_PURENOPS; i++)
626 						nfsstatsov1.srvrpccnt[i] =
627 						    NFSD_VNET(nfsstatsv1_p)->srvrpccnt[i];
628 					for (i = NFSV42_NOPS,
629 					     j = NFSV42_PURENOPS;
630 					     i < NFSV42_NOPS + NFSV4OP_FAKENOPS;
631 					     i++, j++)
632 						nfsstatsov1.srvrpccnt[j] =
633 						    NFSD_VNET(nfsstatsv1_p)->srvrpccnt[i];
634 					nfsstatsov1.reserved_0 = 0;
635 					nfsstatsov1.reserved_1 = 0;
636 					nfsstatsov1.rpcrequests =
637 					    NFSD_VNET(nfsstatsv1_p)->rpcrequests;
638 					nfsstatsov1.rpctimeouts =
639 					    NFSD_VNET(nfsstatsv1_p)->rpctimeouts;
640 					nfsstatsov1.rpcunexpected =
641 					    NFSD_VNET(nfsstatsv1_p)->rpcunexpected;
642 					nfsstatsov1.rpcinvalid =
643 					    NFSD_VNET(nfsstatsv1_p)->rpcinvalid;
644 					nfsstatsov1.srvcache_inproghits =
645 					    NFSD_VNET(nfsstatsv1_p)->srvcache_inproghits;
646 					nfsstatsov1.reserved_2 = 0;
647 					nfsstatsov1.srvcache_nonidemdonehits =
648 					    NFSD_VNET(nfsstatsv1_p)->srvcache_nonidemdonehits;
649 					nfsstatsov1.srvcache_misses =
650 					    NFSD_VNET(nfsstatsv1_p)->srvcache_misses;
651 					nfsstatsov1.srvcache_tcppeak =
652 					    NFSD_VNET(nfsstatsv1_p)->srvcache_tcppeak;
653 					nfsstatsov1.srvcache_size =
654 					    NFSD_VNET(nfsstatsv1_p)->srvcache_size;
655 					nfsstatsov1.srvclients =
656 					    NFSD_VNET(nfsstatsv1_p)->srvclients;
657 					nfsstatsov1.srvopenowners =
658 					    NFSD_VNET(nfsstatsv1_p)->srvopenowners;
659 					nfsstatsov1.srvopens =
660 					    NFSD_VNET(nfsstatsv1_p)->srvopens;
661 					nfsstatsov1.srvlockowners =
662 					    NFSD_VNET(nfsstatsv1_p)->srvlockowners;
663 					nfsstatsov1.srvlocks =
664 					    NFSD_VNET(nfsstatsv1_p)->srvlocks;
665 					nfsstatsov1.srvdelegates =
666 					    NFSD_VNET(nfsstatsv1_p)->srvdelegates;
667 					for (i = 0; i < NFSV42_CBNOPS; i++)
668 						nfsstatsov1.cbrpccnt[i] =
669 						    NFSD_VNET(nfsstatsv1_p)->cbrpccnt[i];
670 					nfsstatsov1.clopenowners =
671 					    NFSD_VNET(nfsstatsv1_p)->clopenowners;
672 					nfsstatsov1.clopens =
673 					    NFSD_VNET(nfsstatsv1_p)->clopens;
674 					nfsstatsov1.cllockowners =
675 					    NFSD_VNET(nfsstatsv1_p)->cllockowners;
676 					nfsstatsov1.cllocks =
677 					    NFSD_VNET(nfsstatsv1_p)->cllocks;
678 					nfsstatsov1.cldelegates =
679 					    NFSD_VNET(nfsstatsv1_p)->cldelegates;
680 					nfsstatsov1.cllocalopenowners =
681 					    NFSD_VNET(nfsstatsv1_p)->cllocalopenowners;
682 					nfsstatsov1.cllocalopens =
683 					    NFSD_VNET(nfsstatsv1_p)->cllocalopens;
684 					nfsstatsov1.cllocallockowners =
685 					    NFSD_VNET(nfsstatsv1_p)->cllocallockowners;
686 					nfsstatsov1.cllocallocks =
687 					    NFSD_VNET(nfsstatsv1_p)->cllocallocks;
688 					nfsstatsov1.srvstartcnt =
689 					    NFSD_VNET(nfsstatsv1_p)->srvstartcnt;
690 					nfsstatsov1.srvdonecnt =
691 					    NFSD_VNET(nfsstatsv1_p)->srvdonecnt;
692 					for (i = NFSV42_NOPS,
693 					     j = NFSV42_PURENOPS;
694 					     i < NFSV42_NOPS + NFSV4OP_FAKENOPS;
695 					     i++, j++) {
696 						nfsstatsov1.srvbytes[j] =
697 						    NFSD_VNET(nfsstatsv1_p)->srvbytes[i];
698 						nfsstatsov1.srvops[j] =
699 						    NFSD_VNET(nfsstatsv1_p)->srvops[i];
700 						nfsstatsov1.srvduration[j] =
701 						    NFSD_VNET(nfsstatsv1_p)->srvduration[i];
702 					}
703 					nfsstatsov1.busyfrom =
704 					    NFSD_VNET(nfsstatsv1_p)->busyfrom;
705 					nfsstatsov1.busyfrom =
706 					    NFSD_VNET(nfsstatsv1_p)->busyfrom;
707 					error = copyout(&nfsstatsov1, uap->argp,
708 					    sizeof(nfsstatsov1));
709 				} else if (nfsstatver.vers != NFSSTATS_V1)
710 					error = EPERM;
711 				else
712 					error = copyout(NFSD_VNET(nfsstatsv1_p),
713 					    uap->argp, sizeof(nfsstatsv1));
714 			}
715 		}
716 		if (error == 0) {
717 			if ((uap->flag & NFSSVC_ZEROCLTSTATS) != 0) {
718 				NFSD_VNET(nfsstatsv1_p)->attrcache_hits = 0;
719 				NFSD_VNET(nfsstatsv1_p)->attrcache_misses = 0;
720 				NFSD_VNET(nfsstatsv1_p)->lookupcache_hits = 0;
721 				NFSD_VNET(nfsstatsv1_p)->lookupcache_misses = 0;
722 				NFSD_VNET(nfsstatsv1_p)->direofcache_hits = 0;
723 				NFSD_VNET(nfsstatsv1_p)->direofcache_misses = 0;
724 				NFSD_VNET(nfsstatsv1_p)->accesscache_hits = 0;
725 				NFSD_VNET(nfsstatsv1_p)->accesscache_misses = 0;
726 				NFSD_VNET(nfsstatsv1_p)->biocache_reads = 0;
727 				NFSD_VNET(nfsstatsv1_p)->read_bios = 0;
728 				NFSD_VNET(nfsstatsv1_p)->read_physios = 0;
729 				NFSD_VNET(nfsstatsv1_p)->biocache_writes = 0;
730 				NFSD_VNET(nfsstatsv1_p)->write_bios = 0;
731 				NFSD_VNET(nfsstatsv1_p)->write_physios = 0;
732 				NFSD_VNET(nfsstatsv1_p)->biocache_readlinks = 0;
733 				NFSD_VNET(nfsstatsv1_p)->readlink_bios = 0;
734 				NFSD_VNET(nfsstatsv1_p)->biocache_readdirs = 0;
735 				NFSD_VNET(nfsstatsv1_p)->readdir_bios = 0;
736 				NFSD_VNET(nfsstatsv1_p)->rpcretries = 0;
737 				NFSD_VNET(nfsstatsv1_p)->rpcrequests = 0;
738 				NFSD_VNET(nfsstatsv1_p)->rpctimeouts = 0;
739 				NFSD_VNET(nfsstatsv1_p)->rpcunexpected = 0;
740 				NFSD_VNET(nfsstatsv1_p)->rpcinvalid = 0;
741 				bzero(NFSD_VNET(nfsstatsv1_p)->rpccnt,
742 				    sizeof(NFSD_VNET(nfsstatsv1_p)->rpccnt));
743 			}
744 			if ((uap->flag & NFSSVC_ZEROSRVSTATS) != 0) {
745 				NFSD_VNET(nfsstatsv1_p)->srvcache_inproghits = 0;
746 				NFSD_VNET(nfsstatsv1_p)->srvcache_nonidemdonehits = 0;
747 				NFSD_VNET(nfsstatsv1_p)->srvcache_misses = 0;
748 				NFSD_VNET(nfsstatsv1_p)->srvcache_tcppeak = 0;
749 				bzero(NFSD_VNET(nfsstatsv1_p)->srvrpccnt,
750 				    sizeof(NFSD_VNET(nfsstatsv1_p)->srvrpccnt));
751 				bzero(NFSD_VNET(nfsstatsv1_p)->cbrpccnt,
752 				    sizeof(NFSD_VNET(nfsstatsv1_p)->cbrpccnt));
753 			}
754 		}
755 		goto out;
756 	} else if (uap->flag & NFSSVC_NFSUSERDPORT) {
757 		u_short sockport;
758 		struct nfsuserd_args nargs;
759 
760 		if ((uap->flag & NFSSVC_NEWSTRUCT) == 0) {
761 			error = copyin(uap->argp, (caddr_t)&sockport,
762 			    sizeof (u_short));
763 			if (error == 0) {
764 				nargs.nuserd_family = AF_INET;
765 				nargs.nuserd_port = sockport;
766 			}
767 		} else {
768 			/*
769 			 * New nfsuserd_args structure, which indicates
770 			 * which IP version to use along with the port#.
771 			 */
772 			error = copyin(uap->argp, &nargs, sizeof(nargs));
773 		}
774 		if (!error)
775 			error = nfsrv_nfsuserdport(&nargs, p);
776 	} else if (uap->flag & NFSSVC_NFSUSERDDELPORT) {
777 		nfsrv_nfsuserddelport();
778 		error = 0;
779 	}
780 
781 out:
782 	NFSEXITCODE(error);
783 	return (error);
784 }
785 
786 /*
787  * called by all three modevent routines, so that it gets things
788  * initialized soon enough.
789  */
790 void
791 newnfs_portinit(void)
792 {
793 	static int inited = 0;
794 
795 	if (inited)
796 		return;
797 	inited = 1;
798 	/* Initialize SMP locks used by both client and server. */
799 	mtx_init(&newnfsd_mtx, "newnfsd_mtx", NULL, MTX_DEF);
800 	mtx_init(&nfs_state_mutex, "nfs_state_mutex", NULL, MTX_DEF);
801 	mtx_init(&nfs_clstate_mutex, "nfs_clstate_mutex", NULL, MTX_DEF);
802 }
803 
804 /*
805  * Determine if the file system supports NFSv4 ACLs.
806  * Return 1 if it does, 0 otherwise.
807  */
808 int
809 nfs_supportsnfsv4acls(struct vnode *vp)
810 {
811 	int error;
812 	long retval;
813 
814 	ASSERT_VOP_LOCKED(vp, "nfs supports nfsv4acls");
815 
816 	if (nfsrv_useacl == 0)
817 		return (0);
818 	error = VOP_PATHCONF(vp, _PC_ACL_NFS4, &retval);
819 	if (error == 0 && retval != 0)
820 		return (1);
821 	return (0);
822 }
823 
824 /*
825  * These are the first fields of all the context structures passed into
826  * nfs_pnfsio().
827  */
828 struct pnfsio {
829 	int		done;
830 	int		inprog;
831 	struct task	tsk;
832 };
833 
834 /*
835  * Do a mirror I/O on a pNFS thread.
836  */
837 int
838 nfs_pnfsio(task_fn_t *func, void *context)
839 {
840 	struct pnfsio *pio;
841 	int ret;
842 	static struct taskqueue *pnfsioq = NULL;
843 
844 	pio = (struct pnfsio *)context;
845 	if (pnfsioq == NULL) {
846 		if (nfs_pnfsiothreads == 0)
847 			return (EPERM);
848 		if (nfs_pnfsiothreads < 0)
849 			nfs_pnfsiothreads = mp_ncpus * 4;
850 		pnfsioq = taskqueue_create("pnfsioq", M_WAITOK,
851 		    taskqueue_thread_enqueue, &pnfsioq);
852 		if (pnfsioq == NULL)
853 			return (ENOMEM);
854 		ret = taskqueue_start_threads(&pnfsioq, nfs_pnfsiothreads,
855 		    0, "pnfsiot");
856 		if (ret != 0) {
857 			taskqueue_free(pnfsioq);
858 			pnfsioq = NULL;
859 			return (ret);
860 		}
861 	}
862 	pio->inprog = 1;
863 	TASK_INIT(&pio->tsk, 0, func, context);
864 	ret = taskqueue_enqueue(pnfsioq, &pio->tsk);
865 	if (ret != 0)
866 		pio->inprog = 0;
867 	return (ret);
868 }
869 
870 /*
871  * Initialize everything that needs to be initialized for a vnet.
872  */
873 static void
874 nfs_vnetinit(const void *unused __unused)
875 {
876 
877 	if (IS_DEFAULT_VNET(curvnet))
878 		NFSD_VNET(nfsstatsv1_p) = &nfsstatsv1;
879 	else
880 		NFSD_VNET(nfsstatsv1_p) = malloc(sizeof(struct nfsstatsv1),
881 		    M_TEMP, M_WAITOK | M_ZERO);
882 	mtx_init(&NFSD_VNET(nfsrv_nfsuserdsock).nr_mtx, "nfsuserd",
883 	    NULL, MTX_DEF);
884 }
885 VNET_SYSINIT(nfs_vnetinit, SI_SUB_VNET_DONE, SI_ORDER_FIRST,
886     nfs_vnetinit, NULL);
887 
888 static void
889 nfs_cleanup(void *unused __unused)
890 {
891 
892 	mtx_destroy(&NFSD_VNET(nfsrv_nfsuserdsock).nr_mtx);
893 	if (!IS_DEFAULT_VNET(curvnet)) {
894 		free(NFSD_VNET(nfsstatsv1_p), M_TEMP);
895 		NFSD_VNET(nfsstatsv1_p) = NULL;
896 	}
897 	/* Clean out the name<-->id cache. */
898 	nfsrv_cleanusergroup();
899 }
900 VNET_SYSUNINIT(nfs_cleanup, SI_SUB_VNET_DONE, SI_ORDER_FIRST,
901     nfs_cleanup, NULL);
902 
903 extern int (*nfsd_call_nfscommon)(struct thread *, struct nfssvc_args *);
904 
905 /*
906  * Called once to initialize data structures...
907  */
908 static int
909 nfscommon_modevent(module_t mod, int type, void *data)
910 {
911 	int error = 0;
912 	static int loaded = 0;
913 
914 	switch (type) {
915 	case MOD_LOAD:
916 		if (loaded)
917 			goto out;
918 		newnfs_portinit();
919 		mtx_init(&nfs_nameid_mutex, "nfs_nameid_mutex", NULL, MTX_DEF);
920 		mtx_init(&nfs_sockl_mutex, "nfs_sockl_mutex", NULL, MTX_DEF);
921 		mtx_init(&nfs_slock_mutex, "nfs_slock_mutex", NULL, MTX_DEF);
922 		mtx_init(&nfs_req_mutex, "nfs_req_mutex", NULL, MTX_DEF);
923 		mtx_init(&nfsrv_dslock_mtx, "nfs4ds", NULL, MTX_DEF);
924 		TAILQ_INIT(&nfsrv_devidhead);
925 		newnfs_init();
926 		nfsd_call_nfscommon = nfssvc_nfscommon;
927 		loaded = 1;
928 		break;
929 
930 	case MOD_UNLOAD:
931 		if (newnfs_numnfsd != 0 ||
932 		    NFSD_VNET(nfsrv_nfsuserd) != NOTRUNNING ||
933 		    nfs_numnfscbd != 0) {
934 			error = EBUSY;
935 			break;
936 		}
937 
938 		nfsd_call_nfscommon = NULL;
939 		/* and get rid of the mutexes */
940 		mtx_destroy(&nfs_nameid_mutex);
941 		mtx_destroy(&newnfsd_mtx);
942 		mtx_destroy(&nfs_state_mutex);
943 		mtx_destroy(&nfs_clstate_mutex);
944 		mtx_destroy(&nfs_sockl_mutex);
945 		mtx_destroy(&nfs_slock_mutex);
946 		mtx_destroy(&nfs_req_mutex);
947 		mtx_destroy(&nfsrv_dslock_mtx);
948 		loaded = 0;
949 		break;
950 	default:
951 		error = EOPNOTSUPP;
952 		break;
953 	}
954 
955 out:
956 	NFSEXITCODE(error);
957 	return error;
958 }
959 static moduledata_t nfscommon_mod = {
960 	"nfscommon",
961 	nfscommon_modevent,
962 	NULL,
963 };
964 DECLARE_MODULE(nfscommon, nfscommon_mod, SI_SUB_VFS, SI_ORDER_ANY);
965 
966 /* So that loader and kldload(2) can find us, wherever we are.. */
967 MODULE_VERSION(nfscommon, 1);
968 MODULE_DEPEND(nfscommon, nfssvc, 1, 1, 1);
969 MODULE_DEPEND(nfscommon, krpc, 1, 1, 1);
970