1 /* $NetBSD: nfs_commonport.c,v 1.1.1.1 2013/09/30 07:19:36 dholland Exp $ */
2 /*-
3 * Copyright (c) 1989, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Rick Macklem at The University of Guelph.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 */
34
35 #include <sys/cdefs.h>
36 /* __FBSDID("FreeBSD: head/sys/fs/nfs/nfs_commonport.c 249592 2013-04-17 21:00:22Z ken "); */
37 __RCSID("$NetBSD: nfs_commonport.c,v 1.1.1.1 2013/09/30 07:19:36 dholland Exp $");
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/sysctl.h>
46 #include <vm/vm.h>
47 #include <vm/vm_object.h>
48 #include <vm/vm_page.h>
49 #include <vm/vm_param.h>
50 #include <vm/vm_map.h>
51 #include <vm/vm_kern.h>
52 #include <vm/vm_extern.h>
53 #include <vm/uma.h>
54
55 extern int nfscl_ticks;
56 extern int nfsrv_nfsuserd;
57 extern struct nfssockreq nfsrv_nfsuserdsock;
58 extern void (*nfsd_call_recall)(struct vnode *, int, struct ucred *,
59 struct thread *);
60 extern int nfsrv_useacl;
61 struct mount nfsv4root_mnt;
62 int newnfs_numnfsd = 0;
63 struct nfsstats newnfsstats;
64 int nfs_numnfscbd = 0;
65 int nfscl_debuglevel = 0;
66 char nfsv4_callbackaddr[INET6_ADDRSTRLEN];
67 struct callout newnfsd_callout;
68 void (*nfsd_call_servertimer)(void) = NULL;
69 void (*ncl_call_invalcaches)(struct vnode *) = NULL;
70
71 static int nfs_realign_test;
72 static int nfs_realign_count;
73
74 SYSCTL_NODE(_vfs, OID_AUTO, nfs, CTLFLAG_RW, 0, "New NFS filesystem");
75 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_test, CTLFLAG_RW, &nfs_realign_test,
76 0, "Number of realign tests done");
77 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_count, CTLFLAG_RW, &nfs_realign_count,
78 0, "Number of mbuf realignments done");
79 SYSCTL_STRING(_vfs_nfs, OID_AUTO, callback_addr, CTLFLAG_RW,
80 nfsv4_callbackaddr, sizeof(nfsv4_callbackaddr),
81 "NFSv4 callback addr for server to use");
82 SYSCTL_INT(_vfs_nfs, OID_AUTO, debuglevel, CTLFLAG_RW, &nfscl_debuglevel,
83 0, "Debug level for new nfs client");
84
85 /*
86 * Defines for malloc
87 * (Here for FreeBSD, since they allocate storage.)
88 */
89 MALLOC_DEFINE(M_NEWNFSRVCACHE, "NFSD srvcache", "NFSD Server Request Cache");
90 MALLOC_DEFINE(M_NEWNFSDCLIENT, "NFSD V4client", "NFSD V4 Client Id");
91 MALLOC_DEFINE(M_NEWNFSDSTATE, "NFSD V4state",
92 "NFSD V4 State (Openowner, Open, Lockowner, Delegation");
93 MALLOC_DEFINE(M_NEWNFSDLOCK, "NFSD V4lock", "NFSD V4 byte range lock");
94 MALLOC_DEFINE(M_NEWNFSDLOCKFILE, "NFSD lckfile", "NFSD Open/Lock file");
95 MALLOC_DEFINE(M_NEWNFSSTRING, "NFSD string", "NFSD V4 long string");
96 MALLOC_DEFINE(M_NEWNFSUSERGROUP, "NFSD usrgroup", "NFSD V4 User/group map");
97 MALLOC_DEFINE(M_NEWNFSDREQ, "NFS req", "NFS request header");
98 MALLOC_DEFINE(M_NEWNFSFH, "NFS fh", "NFS file handle");
99 MALLOC_DEFINE(M_NEWNFSCLOWNER, "NFSCL owner", "NFSCL Open Owner");
100 MALLOC_DEFINE(M_NEWNFSCLOPEN, "NFSCL open", "NFSCL Open");
101 MALLOC_DEFINE(M_NEWNFSCLDELEG, "NFSCL deleg", "NFSCL Delegation");
102 MALLOC_DEFINE(M_NEWNFSCLCLIENT, "NFSCL client", "NFSCL Client");
103 MALLOC_DEFINE(M_NEWNFSCLLOCKOWNER, "NFSCL lckown", "NFSCL Lock Owner");
104 MALLOC_DEFINE(M_NEWNFSCLLOCK, "NFSCL lck", "NFSCL Lock");
105 MALLOC_DEFINE(M_NEWNFSV4NODE, "NEWNFSnode", "New nfs vnode");
106 MALLOC_DEFINE(M_NEWNFSDIRECTIO, "NEWdirectio", "New nfs Direct IO buffer");
107 MALLOC_DEFINE(M_NEWNFSDIROFF, "NFSCL diroffdiroff",
108 "New NFS directory offset data");
109 MALLOC_DEFINE(M_NEWNFSDROLLBACK, "NFSD rollback",
110 "New NFS local lock rollback");
111 MALLOC_DEFINE(M_NEWNFSLAYOUT, "NFSCL layout", "NFSv4.1 Layout");
112 MALLOC_DEFINE(M_NEWNFSFLAYOUT, "NFSCL flayout", "NFSv4.1 File Layout");
113 MALLOC_DEFINE(M_NEWNFSDEVINFO, "NFSCL devinfo", "NFSv4.1 Device Info");
114 MALLOC_DEFINE(M_NEWNFSSOCKREQ, "NFSCL sockreq", "NFS Sock Req");
115 MALLOC_DEFINE(M_NEWNFSCLDS, "NFSCL session", "NFSv4.1 Session");
116 MALLOC_DEFINE(M_NEWNFSLAYRECALL, "NFSCL layrecall", "NFSv4.1 Layout Recall");
117
118 /*
119 * Definition of mutex locks.
120 * newnfsd_mtx is used in nfsrvd_nfsd() to protect the nfs socket list
121 * and assorted other nfsd structures.
122 */
123 struct mtx newnfsd_mtx;
124 struct mtx nfs_sockl_mutex;
125 struct mtx nfs_state_mutex;
126 struct mtx nfs_nameid_mutex;
127 struct mtx nfs_req_mutex;
128 struct mtx nfs_slock_mutex;
129
130 /* local functions */
131 static int nfssvc_call(struct thread *, struct nfssvc_args *, struct ucred *);
132
133 #ifdef __NO_STRICT_ALIGNMENT
134 /*
135 * These architectures don't need re-alignment, so just return.
136 */
137 int
newnfs_realign(struct mbuf ** pm,int how)138 newnfs_realign(struct mbuf **pm, int how)
139 {
140
141 return (0);
142 }
143 #else /* !__NO_STRICT_ALIGNMENT */
144 /*
145 * newnfs_realign:
146 *
147 * Check for badly aligned mbuf data and realign by copying the unaligned
148 * portion of the data into a new mbuf chain and freeing the portions
149 * of the old chain that were replaced.
150 *
151 * We cannot simply realign the data within the existing mbuf chain
152 * because the underlying buffers may contain other rpc commands and
153 * we cannot afford to overwrite them.
154 *
155 * We would prefer to avoid this situation entirely. The situation does
156 * not occur with NFS/UDP and is supposed to only occassionally occur
157 * with TCP. Use vfs.nfs.realign_count and realign_test to check this.
158 *
159 */
160 int
newnfs_realign(struct mbuf ** pm,int how)161 newnfs_realign(struct mbuf **pm, int how)
162 {
163 struct mbuf *m, *n;
164 int off, space;
165
166 ++nfs_realign_test;
167 while ((m = *pm) != NULL) {
168 if ((m->m_len & 0x3) || (mtod(m, intptr_t) & 0x3)) {
169 /*
170 * NB: we can't depend on m_pkthdr.len to help us
171 * decide what to do here. May not be worth doing
172 * the m_length calculation as m_copyback will
173 * expand the mbuf chain below as needed.
174 */
175 space = m_length(m, NULL);
176 if (space >= MINCLSIZE) {
177 /* NB: m_copyback handles space > MCLBYTES */
178 n = m_getcl(how, MT_DATA, 0);
179 } else
180 n = m_get(how, MT_DATA);
181 if (n == NULL)
182 return (ENOMEM);
183 /*
184 * Align the remainder of the mbuf chain.
185 */
186 n->m_len = 0;
187 off = 0;
188 while (m != NULL) {
189 m_copyback(n, off, m->m_len, mtod(m, caddr_t));
190 off += m->m_len;
191 m = m->m_next;
192 }
193 m_freem(*pm);
194 *pm = n;
195 ++nfs_realign_count;
196 break;
197 }
198 pm = &m->m_next;
199 }
200
201 return (0);
202 }
203 #endif /* __NO_STRICT_ALIGNMENT */
204
205 #ifdef notdef
206 static void
nfsrv_object_create(struct vnode * vp,struct thread * td)207 nfsrv_object_create(struct vnode *vp, struct thread *td)
208 {
209
210 if (vp == NULL || vp->v_type != VREG)
211 return;
212 (void) vfs_object_create(vp, td, td->td_ucred);
213 }
214 #endif
215
216 /*
217 * Look up a file name. Basically just initialize stuff and call namei().
218 */
219 int
nfsrv_lookupfilename(struct nameidata * ndp,char * fname,NFSPROC_T * p)220 nfsrv_lookupfilename(struct nameidata *ndp, char *fname, NFSPROC_T *p)
221 {
222 int error;
223
224 NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, fname,
225 p);
226 error = namei(ndp);
227 if (!error) {
228 NDFREE(ndp, NDF_ONLY_PNBUF);
229 }
230 return (error);
231 }
232
233 /*
234 * Copy NFS uid, gids to the cred structure.
235 */
236 void
newnfs_copycred(struct nfscred * nfscr,struct ucred * cr)237 newnfs_copycred(struct nfscred *nfscr, struct ucred *cr)
238 {
239
240 KASSERT(nfscr->nfsc_ngroups >= 0,
241 ("newnfs_copycred: negative nfsc_ngroups"));
242 cr->cr_uid = nfscr->nfsc_uid;
243 crsetgroups(cr, nfscr->nfsc_ngroups, nfscr->nfsc_groups);
244 }
245
246 /*
247 * Map args from nfsmsleep() to msleep().
248 */
249 int
nfsmsleep(void * chan,void * mutex,int prio,const char * wmesg,struct timespec * ts)250 nfsmsleep(void *chan, void *mutex, int prio, const char *wmesg,
251 struct timespec *ts)
252 {
253 u_int64_t nsecval;
254 int error, timeo;
255
256 if (ts) {
257 timeo = hz * ts->tv_sec;
258 nsecval = (u_int64_t)ts->tv_nsec;
259 nsecval = ((nsecval * ((u_int64_t)hz)) + 500000000) /
260 1000000000;
261 timeo += (int)nsecval;
262 } else {
263 timeo = 0;
264 }
265 error = msleep(chan, (struct mtx *)mutex, prio, wmesg, timeo);
266 return (error);
267 }
268
269 /*
270 * Get the file system info for the server. For now, just assume FFS.
271 */
272 void
nfsvno_getfs(struct nfsfsinfo * sip,int isdgram)273 nfsvno_getfs(struct nfsfsinfo *sip, int isdgram)
274 {
275 int pref;
276
277 /*
278 * XXX
279 * There should be file system VFS OP(s) to get this information.
280 * For now, assume ufs.
281 */
282 if (isdgram)
283 pref = NFS_MAXDGRAMDATA;
284 else
285 pref = NFS_MAXDATA;
286 sip->fs_rtmax = NFS_MAXDATA;
287 sip->fs_rtpref = pref;
288 sip->fs_rtmult = NFS_FABLKSIZE;
289 sip->fs_wtmax = NFS_MAXDATA;
290 sip->fs_wtpref = pref;
291 sip->fs_wtmult = NFS_FABLKSIZE;
292 sip->fs_dtpref = pref;
293 sip->fs_maxfilesize = 0xffffffffffffffffull;
294 sip->fs_timedelta.tv_sec = 0;
295 sip->fs_timedelta.tv_nsec = 1;
296 sip->fs_properties = (NFSV3FSINFO_LINK |
297 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
298 NFSV3FSINFO_CANSETTIME);
299 }
300
301 /*
302 * Do the pathconf vnode op.
303 */
304 int
nfsvno_pathconf(struct vnode * vp,int flag,register_t * retf,struct ucred * cred,struct thread * p)305 nfsvno_pathconf(struct vnode *vp, int flag, register_t *retf,
306 struct ucred *cred, struct thread *p)
307 {
308 int error;
309
310 error = VOP_PATHCONF(vp, flag, retf);
311 if (error == EOPNOTSUPP || error == EINVAL) {
312 /*
313 * Some file systems return EINVAL for name arguments not
314 * supported and some return EOPNOTSUPP for this case.
315 * So the NFSv3 Pathconf RPC doesn't fail for these cases,
316 * just fake them.
317 */
318 switch (flag) {
319 case _PC_LINK_MAX:
320 *retf = LINK_MAX;
321 break;
322 case _PC_NAME_MAX:
323 *retf = NAME_MAX;
324 break;
325 case _PC_CHOWN_RESTRICTED:
326 *retf = 1;
327 break;
328 case _PC_NO_TRUNC:
329 *retf = 1;
330 break;
331 default:
332 /*
333 * Only happens if a _PC_xxx is added to the server,
334 * but this isn't updated.
335 */
336 *retf = 0;
337 printf("nfsrvd pathconf flag=%d not supp\n", flag);
338 };
339 error = 0;
340 }
341 NFSEXITCODE(error);
342 return (error);
343 }
344
345 /* Fake nfsrv_atroot. Just return 0 */
346 int
nfsrv_atroot(struct vnode * vp,long * retp)347 nfsrv_atroot(struct vnode *vp, long *retp)
348 {
349
350 return (0);
351 }
352
353 /*
354 * Set the credentials to refer to root.
355 * If only the various BSDen could agree on whether cr_gid is a separate
356 * field or cr_groups[0]...
357 */
358 void
newnfs_setroot(struct ucred * cred)359 newnfs_setroot(struct ucred *cred)
360 {
361
362 cred->cr_uid = 0;
363 cred->cr_groups[0] = 0;
364 cred->cr_ngroups = 1;
365 }
366
367 /*
368 * Get the client credential. Used for Renew and recovery.
369 */
370 struct ucred *
newnfs_getcred(void)371 newnfs_getcred(void)
372 {
373 struct ucred *cred;
374 struct thread *td = curthread;
375
376 cred = crdup(td->td_ucred);
377 newnfs_setroot(cred);
378 return (cred);
379 }
380
381 /*
382 * Nfs timer routine
383 * Call the nfsd's timer function once/sec.
384 */
385 void
newnfs_timer(void * arg)386 newnfs_timer(void *arg)
387 {
388 static time_t lasttime = 0;
389 /*
390 * Call the server timer, if set up.
391 * The argument indicates if it is the next second and therefore
392 * leases should be checked.
393 */
394 if (lasttime != NFSD_MONOSEC) {
395 lasttime = NFSD_MONOSEC;
396 if (nfsd_call_servertimer != NULL)
397 (*nfsd_call_servertimer)();
398 }
399 callout_reset(&newnfsd_callout, nfscl_ticks, newnfs_timer, NULL);
400 }
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
nfs_catnap(int prio,int errval,const char * wmesg)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 *
nfsv4root_getreferral(struct vnode * vp,struct vnode * dvp,u_int32_t fileno)428 nfsv4root_getreferral(struct vnode *vp, struct vnode *dvp, u_int32_t fileno)
429 {
430
431 return (NULL);
432 }
433
434 static int
nfssvc_nfscommon(struct thread * td,struct nfssvc_args * uap)435 nfssvc_nfscommon(struct thread *td, struct nfssvc_args *uap)
436 {
437 int error;
438
439 error = nfssvc_call(td, uap, td->td_ucred);
440 NFSEXITCODE(error);
441 return (error);
442 }
443
444 static int
nfssvc_call(struct thread * p,struct nfssvc_args * uap,struct ucred * cred)445 nfssvc_call(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
446 {
447 int error = EINVAL;
448 struct nfsd_idargs nid;
449
450 if (uap->flag & NFSSVC_IDNAME) {
451 error = copyin(uap->argp, (caddr_t)&nid, sizeof (nid));
452 if (error)
453 goto out;
454 error = nfssvc_idname(&nid);
455 goto out;
456 } else if (uap->flag & NFSSVC_GETSTATS) {
457 error = copyout(&newnfsstats,
458 CAST_USER_ADDR_T(uap->argp), sizeof (newnfsstats));
459 if (error == 0) {
460 if ((uap->flag & NFSSVC_ZEROCLTSTATS) != 0) {
461 newnfsstats.attrcache_hits = 0;
462 newnfsstats.attrcache_misses = 0;
463 newnfsstats.lookupcache_hits = 0;
464 newnfsstats.lookupcache_misses = 0;
465 newnfsstats.direofcache_hits = 0;
466 newnfsstats.direofcache_misses = 0;
467 newnfsstats.accesscache_hits = 0;
468 newnfsstats.accesscache_misses = 0;
469 newnfsstats.biocache_reads = 0;
470 newnfsstats.read_bios = 0;
471 newnfsstats.read_physios = 0;
472 newnfsstats.biocache_writes = 0;
473 newnfsstats.write_bios = 0;
474 newnfsstats.write_physios = 0;
475 newnfsstats.biocache_readlinks = 0;
476 newnfsstats.readlink_bios = 0;
477 newnfsstats.biocache_readdirs = 0;
478 newnfsstats.readdir_bios = 0;
479 newnfsstats.rpcretries = 0;
480 newnfsstats.rpcrequests = 0;
481 newnfsstats.rpctimeouts = 0;
482 newnfsstats.rpcunexpected = 0;
483 newnfsstats.rpcinvalid = 0;
484 bzero(newnfsstats.rpccnt,
485 sizeof(newnfsstats.rpccnt));
486 }
487 if ((uap->flag & NFSSVC_ZEROSRVSTATS) != 0) {
488 newnfsstats.srvrpc_errs = 0;
489 newnfsstats.srv_errs = 0;
490 newnfsstats.srvcache_inproghits = 0;
491 newnfsstats.srvcache_idemdonehits = 0;
492 newnfsstats.srvcache_nonidemdonehits = 0;
493 newnfsstats.srvcache_misses = 0;
494 newnfsstats.srvcache_tcppeak = 0;
495 newnfsstats.srvclients = 0;
496 newnfsstats.srvopenowners = 0;
497 newnfsstats.srvopens = 0;
498 newnfsstats.srvlockowners = 0;
499 newnfsstats.srvlocks = 0;
500 newnfsstats.srvdelegates = 0;
501 newnfsstats.clopenowners = 0;
502 newnfsstats.clopens = 0;
503 newnfsstats.cllockowners = 0;
504 newnfsstats.cllocks = 0;
505 newnfsstats.cldelegates = 0;
506 newnfsstats.cllocalopenowners = 0;
507 newnfsstats.cllocalopens = 0;
508 newnfsstats.cllocallockowners = 0;
509 newnfsstats.cllocallocks = 0;
510 bzero(newnfsstats.srvrpccnt,
511 sizeof(newnfsstats.srvrpccnt));
512 bzero(newnfsstats.cbrpccnt,
513 sizeof(newnfsstats.cbrpccnt));
514 }
515 }
516 goto out;
517 } else if (uap->flag & NFSSVC_NFSUSERDPORT) {
518 u_short sockport;
519
520 error = copyin(uap->argp, (caddr_t)&sockport,
521 sizeof (u_short));
522 if (!error)
523 error = nfsrv_nfsuserdport(sockport, p);
524 } else if (uap->flag & NFSSVC_NFSUSERDDELPORT) {
525 nfsrv_nfsuserddelport();
526 error = 0;
527 }
528
529 out:
530 NFSEXITCODE(error);
531 return (error);
532 }
533
534 /*
535 * called by all three modevent routines, so that it gets things
536 * initialized soon enough.
537 */
538 void
newnfs_portinit(void)539 newnfs_portinit(void)
540 {
541 static int inited = 0;
542
543 if (inited)
544 return;
545 inited = 1;
546 /* Initialize SMP locks used by both client and server. */
547 mtx_init(&newnfsd_mtx, "newnfsd_mtx", NULL, MTX_DEF);
548 mtx_init(&nfs_state_mutex, "nfs_state_mutex", NULL, MTX_DEF);
549 }
550
551 /*
552 * Determine if the file system supports NFSv4 ACLs.
553 * Return 1 if it does, 0 otherwise.
554 */
555 int
nfs_supportsnfsv4acls(struct vnode * vp)556 nfs_supportsnfsv4acls(struct vnode *vp)
557 {
558 int error;
559 register_t retval;
560
561 ASSERT_VOP_LOCKED(vp, "nfs supports nfsv4acls");
562
563 if (nfsrv_useacl == 0)
564 return (0);
565 error = VOP_PATHCONF(vp, _PC_ACL_NFS4, &retval);
566 if (error == 0 && retval != 0)
567 return (1);
568 return (0);
569 }
570
571 extern int (*nfsd_call_nfscommon)(struct thread *, struct nfssvc_args *);
572
573 /*
574 * Called once to initialize data structures...
575 */
576 static int
nfscommon_modevent(module_t mod,int type,void * data)577 nfscommon_modevent(module_t mod, int type, void *data)
578 {
579 int error = 0;
580 static int loaded = 0;
581
582 switch (type) {
583 case MOD_LOAD:
584 if (loaded)
585 goto out;
586 newnfs_portinit();
587 mtx_init(&nfs_nameid_mutex, "nfs_nameid_mutex", NULL, MTX_DEF);
588 mtx_init(&nfs_sockl_mutex, "nfs_sockl_mutex", NULL, MTX_DEF);
589 mtx_init(&nfs_slock_mutex, "nfs_slock_mutex", NULL, MTX_DEF);
590 mtx_init(&nfs_req_mutex, "nfs_req_mutex", NULL, MTX_DEF);
591 mtx_init(&nfsrv_nfsuserdsock.nr_mtx, "nfsuserd", NULL,
592 MTX_DEF);
593 callout_init(&newnfsd_callout, CALLOUT_MPSAFE);
594 newnfs_init();
595 nfsd_call_nfscommon = nfssvc_nfscommon;
596 loaded = 1;
597 break;
598
599 case MOD_UNLOAD:
600 if (newnfs_numnfsd != 0 || nfsrv_nfsuserd != 0 ||
601 nfs_numnfscbd != 0) {
602 error = EBUSY;
603 break;
604 }
605
606 nfsd_call_nfscommon = NULL;
607 callout_drain(&newnfsd_callout);
608 /* and get rid of the mutexes */
609 mtx_destroy(&nfs_nameid_mutex);
610 mtx_destroy(&newnfsd_mtx);
611 mtx_destroy(&nfs_state_mutex);
612 mtx_destroy(&nfs_sockl_mutex);
613 mtx_destroy(&nfs_slock_mutex);
614 mtx_destroy(&nfs_req_mutex);
615 mtx_destroy(&nfsrv_nfsuserdsock.nr_mtx);
616 loaded = 0;
617 break;
618 default:
619 error = EOPNOTSUPP;
620 break;
621 }
622
623 out:
624 NFSEXITCODE(error);
625 return error;
626 }
627 static moduledata_t nfscommon_mod = {
628 "nfscommon",
629 nfscommon_modevent,
630 NULL,
631 };
632 DECLARE_MODULE(nfscommon, nfscommon_mod, SI_SUB_VFS, SI_ORDER_ANY);
633
634 /* So that loader and kldload(2) can find us, wherever we are.. */
635 MODULE_VERSION(nfscommon, 1);
636 MODULE_DEPEND(nfscommon, nfssvc, 1, 1, 1);
637 MODULE_DEPEND(nfscommon, krpc, 1, 1, 1);
638
639