]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/nfsserver/nfs_syscalls.c
When parsing an RPC request in nfsrv_dorec(), KASSERT that there
[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/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, "nfsserver_srvsock", "Nfs server structure");
78
79 MALLOC_DEFINE(M_NFSRVDESC, "nfsserver_srvdesc", "NFS server socket descriptor");
80 MALLOC_DEFINE(M_NFSD, "nfsserver_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                                 if (nd->nd_cr != NULL)
394                                         crfree(nd->nd_cr);
395                                 free((caddr_t)nd, M_NFSRVDESC);
396                                 nd = NULL;
397                         }
398                         nfsd->nfsd_slp = NULL;
399                         nfsd->nfsd_flag &= ~NFSD_REQINPROG;
400                         nfsrv_slpderef(slp);
401                         continue;
402                 }
403                 splx(s);
404                 sotype = slp->ns_so->so_type;
405                 if (nd) {
406                     getmicrotime(&nd->nd_starttime);
407                     if (nd->nd_nam2)
408                         nd->nd_nam = nd->nd_nam2;
409                     else
410                         nd->nd_nam = slp->ns_nam;
411
412                     /*
413                      * Check to see if authorization is needed.
414                      */
415                     cacherep = nfsrv_getcache(nd, &mreq);
416
417                     if (nfs_privport) {
418                         /* Check if source port is privileged */
419                         u_short port;
420                         struct sockaddr *nam = nd->nd_nam;
421                         struct sockaddr_in *sin;
422
423                         sin = (struct sockaddr_in *)nam;
424                         /*
425                          * INET/INET6 - same code:
426                          *    sin_port and sin6_port are at same offset
427                          */
428                         port = ntohs(sin->sin_port);
429                         if (port >= IPPORT_RESERVED &&
430                             nd->nd_procnum != NFSPROC_NULL) {
431 #if defined(INET6) && defined(KLD_MODULE)
432         /* do not use ip6_sprintf: the nfs module should work without INET6 */
433         char b6[INET6_ADDRSTRLEN];
434 #define ip6_sprintf(a) \
435          (sprintf(b6, "%x:%x:%x:%x:%x:%x:%x:%x", \
436                   (a)->s6_addr16[0], (a)->s6_addr16[1], \
437                   (a)->s6_addr16[2], (a)->s6_addr16[3], \
438                   (a)->s6_addr16[4], (a)->s6_addr16[5], \
439                   (a)->s6_addr16[6], (a)->s6_addr16[7]), \
440           b6)
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(&satosin6(sin)->sin6_addr) :
449 #undef ip6_sprintf
450 #endif
451                                    inet_ntoa(sin->sin_addr), port);
452                         }
453                     }
454
455                 }
456
457                 /*
458                  * Loop to get all the write rpc relies that have been
459                  * gathered together.
460                  */
461                 do {
462                     switch (cacherep) {
463                     case RC_DOIT:
464                         if (nd && (nd->nd_flag & ND_NFSV3))
465                             procrastinate = nfsrvw_procrastinate_v3;
466                         else
467                             procrastinate = nfsrvw_procrastinate;
468                         if (writes_todo || (!(nd->nd_flag & ND_NFSV3) &&
469                             nd->nd_procnum == NFSPROC_WRITE &&
470                             procrastinate > 0 && !notstarted))
471                             error = nfsrv_writegather(&nd, slp,
472                                 nfsd->nfsd_td, &mreq);
473                         else
474                             error = (*(nfsrv3_procs[nd->nd_procnum]))(nd,
475                                 slp, nfsd->nfsd_td, &mreq);
476                         if (mreq == NULL)
477                                 break;
478                         if (error != 0 && error != NFSERR_RETVOID) {
479                                 nfsrvstats.srv_errs++;
480                                 nfsrv_updatecache(nd, FALSE, mreq);
481                                 if (nd->nd_nam2)
482                                         FREE(nd->nd_nam2, M_SONAME);
483                                 break;
484                         }
485                         nfsrvstats.srvrpccnt[nd->nd_procnum]++;
486                         nfsrv_updatecache(nd, TRUE, mreq);
487                         nd->nd_mrep = NULL;
488                         /* FALLTHROUGH */
489                     case RC_REPLY:
490                         NFSD_UNLOCK();
491                         siz = m_length(mreq, NULL);
492                         if (siz <= 0 || siz > NFS_MAXPACKET) {
493                                 printf("mbuf siz=%d\n",siz);
494                                 panic("Bad nfs svc reply");
495                         }
496                         m = mreq;
497                         m->m_pkthdr.len = siz;
498                         m->m_pkthdr.rcvif = NULL;
499                         /*
500                          * For stream protocols, prepend a Sun RPC
501                          * Record Mark.
502                          */
503                         if (sotype == SOCK_STREAM) {
504                                 M_PREPEND(m, NFSX_UNSIGNED, M_TRYWAIT);
505                                 *mtod(m, u_int32_t *) = htonl(0x80000000 | siz);
506                         }
507                         NFSD_LOCK();
508                         if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)
509                                 (void) nfs_slplock(slp, 1);
510                         if (slp->ns_flag & SLP_VALID) {
511                             NFSD_UNLOCK();
512                             error = nfsrv_send(slp->ns_so, nd->nd_nam2, m);
513                             NFSD_LOCK();
514                         } else {
515                             error = EPIPE;
516                             m_freem(m);
517                         }
518                         if (nd->nd_nam2)
519                                 FREE(nd->nd_nam2, M_SONAME);
520                         if (nd->nd_mrep)
521                                 m_freem(nd->nd_mrep);
522                         if (error == EPIPE)
523                                 nfsrv_zapsock(slp);
524                         if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)
525                                 nfs_slpunlock(slp);
526                         if (error == EINTR || error == ERESTART) {
527                                 if (nd->nd_cr != NULL)
528                                         crfree(nd->nd_cr);
529                                 free((caddr_t)nd, M_NFSRVDESC);
530                                 nfsrv_slpderef(slp);
531                                 s = splnet();
532                                 goto done;
533                         }
534                         break;
535                     case RC_DROPIT:
536                         m_freem(nd->nd_mrep);
537                         if (nd->nd_nam2)
538                                 FREE(nd->nd_nam2, M_SONAME);
539                         break;
540                     };
541                     if (nd) {
542                         if (nd->nd_cr != NULL)
543                                 crfree(nd->nd_cr);
544                         FREE((caddr_t)nd, M_NFSRVDESC);
545                         nd = NULL;
546                     }
547
548                     /*
549                      * Check to see if there are outstanding writes that
550                      * need to be serviced.
551                      */
552                     cur_usec = nfs_curusec();
553                     s = splsoftclock();
554                     if (LIST_FIRST(&slp->ns_tq) &&
555                         LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec) {
556                         cacherep = RC_DOIT;
557                         writes_todo = 1;
558                     } else
559                         writes_todo = 0;
560                     splx(s);
561                 } while (writes_todo);
562                 s = splnet();
563                 if (nfsrv_dorec(slp, nfsd, &nd)) {
564                         nfsd->nfsd_flag &= ~NFSD_REQINPROG;
565                         nfsd->nfsd_slp = NULL;
566                         nfsrv_slpderef(slp);
567                 }
568                 KASSERT(!(debug_mpsafenet == 0 && !mtx_owned(&Giant)),
569                     ("nfssvc_nfsd(): debug.mpsafenet=0 && !Giant"));
570                 KASSERT(!(debug_mpsafenet == 1 && mtx_owned(&Giant)),
571                     ("nfssvc_nfsd(): debug.mpsafenet=1 && Giant"));
572         }
573 done:
574         KASSERT(!(debug_mpsafenet == 0 && !mtx_owned(&Giant)),
575             ("nfssvc_nfsd(): debug.mpsafenet=0 && !Giant"));
576         KASSERT(!(debug_mpsafenet == 1 && mtx_owned(&Giant)),
577             ("nfssvc_nfsd(): debug.mpsafenet=1 && Giant"));
578         TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
579         splx(s);
580         free((caddr_t)nfsd, M_NFSD);
581         if (--nfsrv_numnfsd == 0)
582                 nfsrv_init(TRUE);       /* Reinitialize everything */
583         NFSD_UNLOCK();
584         return (error);
585 }
586
587 /*
588  * Shut down a socket associated with an nfssvc_sock structure.
589  * Should be called with the send lock set, if required.
590  * The trick here is to increment the sref at the start, so that the nfsds
591  * will stop using it and clear ns_flag at the end so that it will not be
592  * reassigned during cleanup.
593  */
594 static void
595 nfsrv_zapsock(struct nfssvc_sock *slp)
596 {
597         struct nfsrv_descript *nwp, *nnwp;
598         struct socket *so;
599         struct file *fp;
600         struct nfsrv_rec *rec;
601         int s;
602
603         NET_ASSERT_GIANT();
604         NFSD_LOCK_ASSERT();
605
606         /*
607          * XXXRW: By clearing all flags, other threads/etc should ignore
608          * this slp and we can safely release nfsd_mtx so we can clean
609          * up the slp safely.
610          */
611         slp->ns_flag &= ~SLP_ALLFLAGS;
612         fp = slp->ns_fp;
613         if (fp) {
614                 NFSD_UNLOCK();
615                 slp->ns_fp = NULL;
616                 so = slp->ns_so;
617                 SOCKBUF_LOCK(&so->so_rcv);
618                 so->so_rcv.sb_flags &= ~SB_UPCALL;
619                 SOCKBUF_UNLOCK(&so->so_rcv);
620                 so->so_upcall = NULL;
621                 so->so_upcallarg = NULL;
622                 soshutdown(so, SHUT_RDWR);
623                 closef(fp, NULL);
624                 NFSD_LOCK();
625                 if (slp->ns_nam)
626                         FREE(slp->ns_nam, M_SONAME);
627                 m_freem(slp->ns_raw);
628                 while ((rec = STAILQ_FIRST(&slp->ns_rec)) != NULL) {
629                         STAILQ_REMOVE_HEAD(&slp->ns_rec, nr_link);
630                         if (rec->nr_address)
631                                 FREE(rec->nr_address, M_SONAME);
632                         m_freem(rec->nr_packet);
633                         free(rec, M_NFSRVDESC);
634                 }
635                 s = splsoftclock();
636                 for (nwp = LIST_FIRST(&slp->ns_tq); nwp; nwp = nnwp) {
637                         nnwp = LIST_NEXT(nwp, nd_tq);
638                         LIST_REMOVE(nwp, nd_tq);
639                         if (nwp->nd_cr != NULL)
640                                 crfree(nwp->nd_cr);
641                         free((caddr_t)nwp, M_NFSRVDESC);
642                 }
643                 LIST_INIT(&slp->ns_tq);
644                 splx(s);
645         }
646 }
647
648 /*
649  * Derefence a server socket structure. If it has no more references and
650  * is no longer valid, you can throw it away.
651  */
652 void
653 nfsrv_slpderef(struct nfssvc_sock *slp)
654 {
655
656         NFSD_LOCK_ASSERT();
657
658         if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) {
659                 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
660                 free((caddr_t)slp, M_NFSSVC);
661         }
662 }
663
664 /*
665  * Lock a socket against others.
666  *
667  * XXXRW: Wait argument is always 1 in the caller.  Replace with a real
668  * sleep lock?
669  */
670 int
671 nfs_slplock(struct nfssvc_sock *slp, int wait)
672 {
673         int *statep = &slp->ns_solock;
674
675         NFSD_LOCK_ASSERT();
676
677         if (!wait && (*statep & NFSRV_SNDLOCK))
678                 return(0);      /* already locked, fail */
679         while (*statep & NFSRV_SNDLOCK) {
680                 *statep |= NFSRV_WANTSND;
681                 (void) msleep(statep, &nfsd_mtx, PZERO - 1, "nfsslplck", 0);
682         }
683         *statep |= NFSRV_SNDLOCK;
684         return (1);
685 }
686
687 /*
688  * Unlock the stream socket for others.
689  */
690 void
691 nfs_slpunlock(struct nfssvc_sock *slp)
692 {
693         int *statep = &slp->ns_solock;
694
695         NFSD_LOCK_ASSERT();
696
697         if ((*statep & NFSRV_SNDLOCK) == 0)
698                 panic("nfs slpunlock");
699         *statep &= ~NFSRV_SNDLOCK;
700         if (*statep & NFSRV_WANTSND) {
701                 *statep &= ~NFSRV_WANTSND;
702                 wakeup(statep);
703         }
704 }
705
706 /*
707  * Initialize the data structures for the server.
708  * Handshake with any new nfsds starting up to avoid any chance of
709  * corruption.
710  */
711 void
712 nfsrv_init(int terminating)
713 {
714         struct nfssvc_sock *slp, *nslp;
715
716         NET_ASSERT_GIANT();
717         NFSD_LOCK_ASSERT();
718
719         if (nfssvc_sockhead_flag & SLP_INIT)
720                 panic("nfsd init");
721         nfssvc_sockhead_flag |= SLP_INIT;
722         if (terminating) {
723                 for (slp = TAILQ_FIRST(&nfssvc_sockhead); slp != NULL;
724                     slp = nslp) {
725                         nslp = TAILQ_NEXT(slp, ns_chain);
726                         if (slp->ns_flag & SLP_VALID)
727                                 nfsrv_zapsock(slp);
728                         TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
729                         free((caddr_t)slp, M_NFSSVC);
730                 }
731                 nfsrv_cleancache();     /* And clear out server cache */
732         } else
733                 nfs_pub.np_valid = 0;
734
735         TAILQ_INIT(&nfssvc_sockhead);
736         nfssvc_sockhead_flag &= ~SLP_INIT;
737         if (nfssvc_sockhead_flag & SLP_WANTINIT) {
738                 nfssvc_sockhead_flag &= ~SLP_WANTINIT;
739                 wakeup(&nfssvc_sockhead);
740         }
741
742         TAILQ_INIT(&nfsd_head);
743         nfsd_head_flag &= ~NFSD_CHECKSLP;
744
745 #if 0
746         nfs_udpsock = (struct nfssvc_sock *)
747             malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK | M_ZERO);
748         STAILQ_INIT(&nfs_udpsock->ns_rec);
749         TAILQ_INSERT_HEAD(&nfssvc_sockhead, nfs_udpsock, ns_chain);
750
751         nfs_cltpsock = (struct nfssvc_sock *)
752             malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK | M_ZERO);
753         STAILQ_INIT(&nfs_cltpsock->ns_rec);
754         TAILQ_INSERT_TAIL(&nfssvc_sockhead, nfs_cltpsock, ns_chain);
755 #endif
756 }