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