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