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