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