1 /*-
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * @(#)nfs_syscalls.c 8.5 (Berkeley) 3/30/95
33 */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD: src/sys/nfsserver/nfs_syscalls.c,v 1.123 2008/11/03 10:38:00 dfr Exp $");
37
38 #include "opt_inet6.h"
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/sysproto.h>
43 #include <sys/kernel.h>
44 #include <sys/sysctl.h>
45 #include <sys/file.h>
46 #include <sys/filedesc.h>
47 #include <sys/vnode.h>
48 #include <sys/malloc.h>
49 #include <sys/mount.h>
50 #include <sys/priv.h>
51 #include <sys/proc.h>
52 #include <sys/bio.h>
53 #include <sys/buf.h>
54 #include <sys/mbuf.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
57 #include <sys/domain.h>
58 #include <sys/protosw.h>
59 #include <sys/namei.h>
60 #include <sys/fcntl.h>
61 #include <sys/lockf.h>
62
63 #include <netinet/in.h>
64 #include <netinet/tcp.h>
65 #ifdef INET6
66 #include <net/if.h>
67 #include <netinet6/in6_var.h>
68 #endif
69 #include <nfs/xdr_subs.h>
70 #include <nfs/rpcv2.h>
71 #include <nfs/nfsproto.h>
72 #include <nfsserver/nfs.h>
73 #include <nfsserver/nfsm_subs.h>
74 #include <nfsserver/nfsrvcache.h>
75
76 #ifdef NFS_LEGACYRPC
77
78 static MALLOC_DEFINE(M_NFSSVC, "nfss_srvsock", "Nfs server structure");
79
80 MALLOC_DEFINE(M_NFSRVDESC, "nfss_srvdesc", "NFS server socket descriptor");
81 MALLOC_DEFINE(M_NFSD, "nfss_daemon", "Nfs server daemon structure");
82
83 #define TRUE 1
84 #define FALSE 0
85
86 SYSCTL_DECL(_vfs_nfsrv);
87
88 int nfsd_waiting = 0;
89 int nfsrv_numnfsd = 0;
90 static int notstarted = 1;
91
92 static int nfs_privport = 0;
93 SYSCTL_INT(_vfs_nfsrv, NFS_NFSPRIVPORT, nfs_privport, CTLFLAG_RW,
94 &nfs_privport, 0,
95 "Only allow clients using a privileged port");
96 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, gatherdelay, CTLFLAG_RW,
97 &nfsrvw_procrastinate, 0,
98 "Delay value for write gathering");
99 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, gatherdelay_v3, CTLFLAG_RW,
100 &nfsrvw_procrastinate_v3, 0,
101 "Delay in seconds for NFSv3 write gathering");
102
103 static int nfssvc_addsock(struct file *, struct sockaddr *);
104 static void nfsrv_zapsock(struct nfssvc_sock *slp);
105 static int nfssvc_nfsd(void);
106
107 extern u_long sb_max_adj;
108
109 /*
110 * NFS server system calls
111 */
112
113 /*
114 * Nfs server psuedo system call for the nfsd's
115 * Based on the flag value it either:
116 * - adds a socket to the selection list
117 * - remains in the kernel as an nfsd
118 * - remains in the kernel as an nfsiod
119 * For INET6 we suppose that nfsd provides only IN6P_IPV6_V6ONLY sockets
120 * and that mountd provides
121 * - sockaddr with no IPv4-mapped addresses
122 * - mask for both INET and INET6 families if there is IPv4-mapped overlap
123 */
124 #ifndef _SYS_SYSPROTO_H_
125 struct nfssvc_args {
126 int flag;
127 caddr_t argp;
128 };
129 #endif
130 int
131 nfssvc(struct thread *td, struct nfssvc_args *uap)
132 {
133 struct file *fp;
134 struct sockaddr *nam;
135 struct nfsd_addsock_args nfsdarg;
136 int error;
137
138 KASSERT(!mtx_owned(&Giant), ("nfssvc(): called with Giant"));
139
140 error = priv_check(td, PRIV_NFS_DAEMON);
141 if (error)
142 return (error);
143 NFSD_LOCK();
144 while (nfssvc_sockhead_flag & SLP_INIT) {
145 nfssvc_sockhead_flag |= SLP_WANTINIT;
146 (void) msleep(&nfssvc_sockhead, &nfsd_mtx, PSOCK,
147 "nfsd init", 0);
148 }
149 NFSD_UNLOCK();
150 if (uap->flag & NFSSVC_ADDSOCK) {
151 error = copyin(uap->argp, (caddr_t)&nfsdarg, sizeof(nfsdarg));
152 if (error)
153 return (error);
154 if ((error = fget(td, nfsdarg.sock, &fp)) != 0)
155 return (error);
156 if (fp->f_type != DTYPE_SOCKET) {
157 fdrop(fp, td);
158 return (error); /* XXXRW: Should be EINVAL? */
159 }
160 /*
161 * Get the client address for connected sockets.
162 */
163 if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
164 nam = NULL;
165 else {
166 error = getsockaddr(&nam, nfsdarg.name,
167 nfsdarg.namelen);
168 if (error) {
169 fdrop(fp, td);
170 return (error);
171 }
172 }
173 error = nfssvc_addsock(fp, nam);
174 fdrop(fp, td);
175 } else if (uap->flag & NFSSVC_OLDNFSD) {
176 error = nfssvc_nfsd();
177 } else {
178 error = ENXIO;
179 }
180 if (error == EINTR || error == ERESTART)
181 error = 0;
182 return (error);
183 }
184
185 /*
186 * Adds a socket to the list for servicing by nfsds.
187 */
188 static int
189 nfssvc_addsock(struct file *fp, struct sockaddr *mynam)
190 {
191 int siz;
192 struct nfssvc_sock *slp;
193 struct socket *so;
194 int error;
195
196 so = fp->f_data;
197 #if 0
198 /*
199 * XXXRW: If this code is ever enabled, there's a race when running
200 * MPSAFE.
201 */
202 tslp = NULL;
203 /*
204 * Add it to the list, as required.
205 */
206 if (so->so_proto->pr_protocol == IPPROTO_UDP) {
207 tslp = nfs_udpsock;
208 if (tslp->ns_flag & SLP_VALID) {
209 if (mynam != NULL)
210 free(mynam, M_SONAME);
211 return (EPERM);
212 }
213 }
214 #endif
215 siz = sb_max_adj;
216 error = soreserve(so, siz, siz);
217 if (error) {
218 if (mynam != NULL)
219 free(mynam, M_SONAME);
220 return (error);
221 }
222
223 /*
224 * Set protocol specific options { for now TCP only } and
225 * reserve some space. For datagram sockets, this can get called
226 * repeatedly for the same socket, but that isn't harmful.
227 */
228 if (so->so_type == SOCK_STREAM) {
229 struct sockopt sopt;
230 int val;
231
232 bzero(&sopt, sizeof sopt);
233 sopt.sopt_dir = SOPT_SET;
234 sopt.sopt_level = SOL_SOCKET;
235 sopt.sopt_name = SO_KEEPALIVE;
236 sopt.sopt_val = &val;
237 sopt.sopt_valsize = sizeof val;
238 val = 1;
239 sosetopt(so, &sopt);
240 }
241 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
242 struct sockopt sopt;
243 int val;
244
245 bzero(&sopt, sizeof sopt);
246 sopt.sopt_dir = SOPT_SET;
247 sopt.sopt_level = IPPROTO_TCP;
248 sopt.sopt_name = TCP_NODELAY;
249 sopt.sopt_val = &val;
250 sopt.sopt_valsize = sizeof val;
251 val = 1;
252 sosetopt(so, &sopt);
253 }
254 SOCKBUF_LOCK(&so->so_rcv);
255 so->so_rcv.sb_flags &= ~SB_NOINTR;
256 so->so_rcv.sb_timeo = 0;
257 SOCKBUF_UNLOCK(&so->so_rcv);
258 SOCKBUF_LOCK(&so->so_snd);
259 so->so_snd.sb_flags &= ~SB_NOINTR;
260 so->so_snd.sb_timeo = 0;
261 SOCKBUF_UNLOCK(&so->so_snd);
262
263 slp = (struct nfssvc_sock *)
264 malloc(sizeof (struct nfssvc_sock), M_NFSSVC,
265 M_WAITOK | M_ZERO);
266 STAILQ_INIT(&slp->ns_rec);
267 NFSD_LOCK();
268 TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
269
270 slp->ns_so = so;
271 slp->ns_nam = mynam;
272 fhold(fp);
273 slp->ns_fp = fp;
274 SOCKBUF_LOCK(&so->so_rcv);
275 so->so_upcallarg = (caddr_t)slp;
276 so->so_upcall = nfsrv_rcv;
277 so->so_rcv.sb_flags |= SB_UPCALL;
278 SOCKBUF_UNLOCK(&so->so_rcv);
279 slp->ns_flag = (SLP_VALID | SLP_NEEDQ);
280 nfsrv_wakenfsd(slp);
281 NFSD_UNLOCK();
282 return (0);
283 }
284
285 /*
286 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
287 * until it is killed by a signal.
288 */
289 static int
290 nfssvc_nfsd()
291 {
292 int siz;
293 struct nfssvc_sock *slp;
294 struct nfsd *nfsd;
295 struct nfsrv_descript *nd = NULL;
296 struct mbuf *m, *mreq;
297 int error = 0, cacherep, s, sotype, writes_todo;
298 int procrastinate;
299 u_quad_t cur_usec;
300
301 #ifndef nolint
302 cacherep = RC_DOIT;
303 writes_todo = 0;
304 #endif
305 nfsd = (struct nfsd *)
306 malloc(sizeof (struct nfsd), M_NFSD, M_WAITOK | M_ZERO);
307 s = splnet();
308 NFSD_LOCK();
309
310 TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
311 nfsrv_numnfsd++;
312
313 /*
314 * Loop getting rpc requests until SIGKILL.
315 */
316 for (;;) {
317 if ((nfsd->nfsd_flag & NFSD_REQINPROG) == 0) {
318 while (nfsd->nfsd_slp == NULL &&
319 (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
320 nfsd->nfsd_flag |= NFSD_WAITING;
321 nfsd_waiting++;
322 error = msleep(nfsd, &nfsd_mtx,
323 PSOCK | PCATCH, "-", 0);
324 nfsd_waiting--;
325 if (error)
326 goto done;
327 }
328 if (nfsd->nfsd_slp == NULL &&
329 (nfsd_head_flag & NFSD_CHECKSLP) != 0) {
330 TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) {
331 if ((slp->ns_flag & (SLP_VALID | SLP_DOREC))
332 == (SLP_VALID | SLP_DOREC)) {
333 slp->ns_flag &= ~SLP_DOREC;
334 slp->ns_sref++;
335 nfsd->nfsd_slp = slp;
336 break;
337 }
338 }
339 if (slp == NULL)
340 nfsd_head_flag &= ~NFSD_CHECKSLP;
341 }
342 if ((slp = nfsd->nfsd_slp) == NULL)
343 continue;
344 if (slp->ns_flag & SLP_VALID) {
345 if (slp->ns_flag & SLP_DISCONN)
346 nfsrv_zapsock(slp);
347 else if (slp->ns_flag & SLP_NEEDQ) {
348 slp->ns_flag &= ~SLP_NEEDQ;
349 (void) nfs_slplock(slp, 1);
350 NFSD_UNLOCK();
351 nfsrv_rcv(slp->ns_so, (caddr_t)slp,
352 M_WAIT);
353 NFSD_LOCK();
354 nfs_slpunlock(slp);
355 }
356 error = nfsrv_dorec(slp, nfsd, &nd);
357 cur_usec = nfs_curusec();
358 if (error && LIST_FIRST(&slp->ns_tq) &&
359 LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec) {
360 error = 0;
361 cacherep = RC_DOIT;
362 writes_todo = 1;
363 } else
364 writes_todo = 0;
365 nfsd->nfsd_flag |= NFSD_REQINPROG;
366 }
367 } else {
368 error = 0;
369 slp = nfsd->nfsd_slp;
370 }
371 if (error || (slp->ns_flag & SLP_VALID) == 0) {
372 if (nd) {
373 if (nd->nd_cr != NULL)
374 crfree(nd->nd_cr);
375 free((caddr_t)nd, M_NFSRVDESC);
376 nd = NULL;
377 }
378 nfsd->nfsd_slp = NULL;
379 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
380 nfsrv_slpderef(slp);
381 continue;
382 }
383 splx(s);
384 sotype = slp->ns_so->so_type;
385 if (nd) {
386 getmicrotime(&nd->nd_starttime);
387 if (nd->nd_nam2)
388 nd->nd_nam = nd->nd_nam2;
389 else
390 nd->nd_nam = slp->ns_nam;
391
392 /*
393 * Check to see if authorization is needed.
394 */
395 cacherep = nfsrv_getcache(nd, &mreq);
396
397 if (nfs_privport) {
398 /* Check if source port is privileged */
399 u_short port;
400 struct sockaddr *nam = nd->nd_nam;
401 struct sockaddr_in *sin;
402
403 sin = (struct sockaddr_in *)nam;
404 /*
405 * INET/INET6 - same code:
406 * sin_port and sin6_port are at same offset
407 */
408 port = ntohs(sin->sin_port);
409 if (port >= IPPORT_RESERVED &&
410 nd->nd_procnum != NFSPROC_NULL) {
411 #ifdef INET6
412 char b6[INET6_ADDRSTRLEN];
413 #if defined(KLD_MODULE)
414 /* Do not use ip6_sprintf: the nfs module should work without INET6. */
415 #define ip6_sprintf(buf, a) \
416 (sprintf((buf), "%x:%x:%x:%x:%x:%x:%x:%x", \
417 (a)->s6_addr16[0], (a)->s6_addr16[1], \
418 (a)->s6_addr16[2], (a)->s6_addr16[3], \
419 (a)->s6_addr16[4], (a)->s6_addr16[5], \
420 (a)->s6_addr16[6], (a)->s6_addr16[7]), \
421 (buf))
422 #endif
423 #endif
424 nd->nd_procnum = NFSPROC_NOOP;
425 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
426 cacherep = RC_DOIT;
427 printf("NFS request from unprivileged port (%s:%d)\n",
428 #ifdef INET6
429 sin->sin_family == AF_INET6 ?
430 ip6_sprintf(b6, &satosin6(sin)->sin6_addr) :
431 #if defined(KLD_MODULE)
432 #undef ip6_sprintf
433 #endif
434 #endif
435 inet_ntoa(sin->sin_addr), port);
436 }
437 }
438
439 }
440
441 /*
442 * Loop to get all the write rpc relies that have been
443 * gathered together.
444 */
445 do {
446 switch (cacherep) {
447 case RC_DOIT:
448 if (nd && (nd->nd_flag & ND_NFSV3))
449 procrastinate = nfsrvw_procrastinate_v3;
450 else
451 procrastinate = nfsrvw_procrastinate;
452 NFSD_UNLOCK();
453 if (writes_todo || (!(nd->nd_flag & ND_NFSV3) &&
454 nd->nd_procnum == NFSPROC_WRITE &&
455 procrastinate > 0 && !notstarted))
456 error = nfsrv_writegather(&nd, slp, &mreq);
457 else
458 error = (*(nfsrv3_procs[nd->nd_procnum]))(nd,
459 slp, &mreq);
460 NFSD_LOCK();
461 if (mreq == NULL)
462 break;
463 if (error != 0 && error != NFSERR_RETVOID) {
464 nfsrvstats.srv_errs++;
465 nfsrv_updatecache(nd, FALSE, mreq);
466 if (nd->nd_nam2)
467 free(nd->nd_nam2, M_SONAME);
468 break;
469 }
470 nfsrvstats.srvrpccnt[nd->nd_procnum]++;
471 nfsrv_updatecache(nd, TRUE, mreq);
472 nd->nd_mrep = NULL;
473 /* FALLTHROUGH */
474 case RC_REPLY:
475 NFSD_UNLOCK();
476 siz = m_length(mreq, NULL);
477 if (siz <= 0 || siz > NFS_MAXPACKET) {
478 printf("mbuf siz=%d\n",siz);
479 panic("Bad nfs svc reply");
480 }
481 m = mreq;
482 m->m_pkthdr.len = siz;
483 m->m_pkthdr.rcvif = NULL;
484 /*
485 * For stream protocols, prepend a Sun RPC
486 * Record Mark.
487 */
488 if (sotype == SOCK_STREAM) {
489 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
490 *mtod(m, u_int32_t *) = htonl(0x80000000 | siz);
491 }
492 NFSD_LOCK();
493 if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)
494 (void) nfs_slplock(slp, 1);
495 if (slp->ns_flag & SLP_VALID) {
496 NFSD_UNLOCK();
497 error = nfsrv_send(slp->ns_so, nd->nd_nam2, m);
498 NFSD_LOCK();
499 } else {
500 error = EPIPE;
501 m_freem(m);
502 }
503 if (nd->nd_nam2)
504 free(nd->nd_nam2, M_SONAME);
505 if (nd->nd_mrep)
506 m_freem(nd->nd_mrep);
507 if (error == EPIPE)
508 nfsrv_zapsock(slp);
509 if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)
510 nfs_slpunlock(slp);
511 if (error == EINTR || error == ERESTART) {
512 if (nd->nd_cr != NULL)
513 crfree(nd->nd_cr);
514 free((caddr_t)nd, M_NFSRVDESC);
515 nfsrv_slpderef(slp);
516 s = splnet();
517 goto done;
518 }
519 break;
520 case RC_DROPIT:
521 m_freem(nd->nd_mrep);
522 if (nd->nd_nam2)
523 free(nd->nd_nam2, M_SONAME);
524 break;
525 };
526 if (nd) {
527 if (nd->nd_cr != NULL)
528 crfree(nd->nd_cr);
529 free((caddr_t)nd, M_NFSRVDESC);
530 nd = NULL;
531 }
532
533 /*
534 * Check to see if there are outstanding writes that
535 * need to be serviced.
536 */
537 cur_usec = nfs_curusec();
538 s = splsoftclock();
539 if (LIST_FIRST(&slp->ns_tq) &&
540 LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec) {
541 cacherep = RC_DOIT;
542 writes_todo = 1;
543 } else
544 writes_todo = 0;
545 splx(s);
546 } while (writes_todo);
547 s = splnet();
548 if (nfsrv_dorec(slp, nfsd, &nd)) {
549 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
550 nfsd->nfsd_slp = NULL;
551 nfsrv_slpderef(slp);
552 }
553 mtx_assert(&Giant, MA_NOTOWNED);
554 }
555 done:
556 mtx_assert(&Giant, MA_NOTOWNED);
557 TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
558 splx(s);
559 free((caddr_t)nfsd, M_NFSD);
560 if (--nfsrv_numnfsd == 0)
561 nfsrv_init(TRUE); /* Reinitialize everything */
562 NFSD_UNLOCK();
563 return (error);
564 }
565
566 /*
567 * Shut down a socket associated with an nfssvc_sock structure.
568 * Should be called with the send lock set, if required.
569 * The trick here is to increment the sref at the start, so that the nfsds
570 * will stop using it and clear ns_flag at the end so that it will not be
571 * reassigned during cleanup.
572 */
573 static void
574 nfsrv_zapsock(struct nfssvc_sock *slp)
575 {
576 struct nfsrv_descript *nwp, *nnwp;
577 struct socket *so;
578 struct file *fp;
579 struct nfsrv_rec *rec;
580 int s;
581
582 NFSD_LOCK_ASSERT();
583
584 /*
585 * XXXRW: By clearing all flags, other threads/etc should ignore
586 * this slp and we can safely release nfsd_mtx so we can clean
587 * up the slp safely.
588 */
589 slp->ns_flag &= ~SLP_ALLFLAGS;
590 fp = slp->ns_fp;
591 if (fp) {
592 NFSD_UNLOCK();
593 slp->ns_fp = NULL;
594 so = slp->ns_so;
595 SOCKBUF_LOCK(&so->so_rcv);
596 so->so_rcv.sb_flags &= ~SB_UPCALL;
597 so->so_upcall = NULL;
598 so->so_upcallarg = NULL;
599 SOCKBUF_UNLOCK(&so->so_rcv);
600 soshutdown(so, SHUT_RDWR);
601 closef(fp, NULL);
602 NFSD_LOCK();
603 if (slp->ns_nam)
604 free(slp->ns_nam, M_SONAME);
605 m_freem(slp->ns_raw);
606 while ((rec = STAILQ_FIRST(&slp->ns_rec)) != NULL) {
607 STAILQ_REMOVE_HEAD(&slp->ns_rec, nr_link);
608 if (rec->nr_address)
609 free(rec->nr_address, M_SONAME);
610 m_freem(rec->nr_packet);
611 free(rec, M_NFSRVDESC);
612 }
613 s = splsoftclock();
614 for (nwp = LIST_FIRST(&slp->ns_tq); nwp; nwp = nnwp) {
615 nnwp = LIST_NEXT(nwp, nd_tq);
616 LIST_REMOVE(nwp, nd_tq);
617 if (nwp->nd_cr != NULL)
618 crfree(nwp->nd_cr);
619 free((caddr_t)nwp, M_NFSRVDESC);
620 }
621 LIST_INIT(&slp->ns_tq);
622 splx(s);
623 }
624 }
625
626 /*
627 * Derefence a server socket structure. If it has no more references and
628 * is no longer valid, you can throw it away.
629 */
630 void
631 nfsrv_slpderef(struct nfssvc_sock *slp)
632 {
633
634 NFSD_LOCK_ASSERT();
635
636 if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) {
637 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
638 free((caddr_t)slp, M_NFSSVC);
639 }
640 }
641
642 /*
643 * Lock a socket against others.
644 *
645 * XXXRW: Wait argument is always 1 in the caller. Replace with a real
646 * sleep lock?
647 */
648 int
649 nfs_slplock(struct nfssvc_sock *slp, int wait)
650 {
651 int *statep = &slp->ns_solock;
652
653 NFSD_LOCK_ASSERT();
654
655 if (!wait && (*statep & NFSRV_SNDLOCK))
656 return(0); /* already locked, fail */
657 while (*statep & NFSRV_SNDLOCK) {
658 *statep |= NFSRV_WANTSND;
659 (void) msleep(statep, &nfsd_mtx, PZERO - 1, "nfsslplck", 0);
660 }
661 *statep |= NFSRV_SNDLOCK;
662 return (1);
663 }
664
665 /*
666 * Unlock the stream socket for others.
667 */
668 void
669 nfs_slpunlock(struct nfssvc_sock *slp)
670 {
671 int *statep = &slp->ns_solock;
672
673 NFSD_LOCK_ASSERT();
674
675 if ((*statep & NFSRV_SNDLOCK) == 0)
676 panic("nfs slpunlock");
677 *statep &= ~NFSRV_SNDLOCK;
678 if (*statep & NFSRV_WANTSND) {
679 *statep &= ~NFSRV_WANTSND;
680 wakeup(statep);
681 }
682 }
683
684 /*
685 * Initialize the data structures for the server.
686 * Handshake with any new nfsds starting up to avoid any chance of
687 * corruption.
688 */
689 void
690 nfsrv_init(int terminating)
691 {
692 struct nfssvc_sock *slp, *nslp;
693
694 NFSD_LOCK_ASSERT();
695
696 if (nfssvc_sockhead_flag & SLP_INIT)
697 panic("nfsd init");
698 nfssvc_sockhead_flag |= SLP_INIT;
699 if (terminating) {
700 TAILQ_FOREACH_SAFE(slp, &nfssvc_sockhead, ns_chain, nslp) {
701 if (slp->ns_flag & SLP_VALID)
702 nfsrv_zapsock(slp);
703 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
704 free((caddr_t)slp, M_NFSSVC);
705 }
706 nfsrv_cleancache(); /* And clear out server cache */
707 } else
708 nfs_pub.np_valid = 0;
709
710 TAILQ_INIT(&nfssvc_sockhead);
711 nfssvc_sockhead_flag &= ~SLP_INIT;
712 if (nfssvc_sockhead_flag & SLP_WANTINIT) {
713 nfssvc_sockhead_flag &= ~SLP_WANTINIT;
714 wakeup(&nfssvc_sockhead);
715 }
716
717 TAILQ_INIT(&nfsd_head);
718 nfsd_head_flag &= ~NFSD_CHECKSLP;
719
720 #if 0
721 nfs_udpsock = (struct nfssvc_sock *)
722 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK | M_ZERO);
723 STAILQ_INIT(&nfs_udpsock->ns_rec);
724 TAILQ_INSERT_HEAD(&nfssvc_sockhead, nfs_udpsock, ns_chain);
725
726 nfs_cltpsock = (struct nfssvc_sock *)
727 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK | M_ZERO);
728 STAILQ_INIT(&nfs_cltpsock->ns_rec);
729 TAILQ_INSERT_TAIL(&nfssvc_sockhead, nfs_cltpsock, ns_chain);
730 #endif
731 }
732
733 #endif /* NFS_LEGACYRPC */
734
|