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