2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1989, 1993
25 * The Regents of the University of California. All rights reserved.
27 * This code is derived from software contributed to Berkeley by
28 * Rick Macklem at The University of Guelph.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * @(#)nfs_syscalls.c 8.5 (Berkeley) 3/30/95
59 * FreeBSD-Id: nfs_syscalls.c,v 1.32 1997/11/07 08:53:25 phk Exp $
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 /* XXX CSM 11/25/97 FreeBSD's generated syscall prototypes */
66 #include <sys/sysproto.h>
68 #include <sys/kernel.h>
70 #include <sys/filedesc.h>
72 #include <sys/vnode.h>
73 #include <sys/mount.h>
75 #include <sys/sysctl.h>
78 #include <sys/malloc.h>
80 #include <sys/socket.h>
81 #include <sys/socketvar.h>
82 #include <sys/domain.h>
83 #include <sys/protosw.h>
84 #include <sys/namei.h>
85 #include <sys/fcntl.h>
86 #include <sys/lockf.h>
87 #include <sys/syslog.h>
89 #include <machine/spl.h>
91 #include <netinet/in.h>
92 #include <netinet/tcp.h>
94 #include <netiso/iso.h>
96 #include <nfs/xdr_subs.h>
97 #include <nfs/rpcv2.h>
98 #include <nfs/nfsproto.h>
100 #include <nfs/nfsm_subs.h>
101 #include <nfs/nfsrvcache.h>
102 #include <nfs/nfsmount.h>
103 #include <nfs/nfsnode.h>
104 #include <nfs/nqnfs.h>
105 #include <nfs/nfsrtt.h>
106 #include <nfs/nfs_lock.h>
109 extern int (*nfsrv3_procs
[NFS_NPROCS
]) __P((struct nfsrv_descript
*nd
,
110 struct nfssvc_sock
*slp
,
112 struct mbuf
**mreqp
));
113 extern int nfs_numasync
;
114 extern int nfs_ioddelwri
;
115 extern time_t nqnfsstarttime
;
116 extern int nqsrv_writeslack
;
118 extern struct nfsstats nfsstats
;
119 extern int nfsrvw_procrastinate
;
120 extern int nfsrvw_procrastinate_v3
;
121 struct nfssvc_sock
*nfs_udpsock
, *nfs_cltpsock
;
122 static int nuidhash_max
= NFS_MAXUIDHASH
;
124 static void nfsrv_zapsock
__P((struct nfssvc_sock
*slp
));
125 static int nfssvc_iod
__P((struct proc
*));
130 static int nfs_asyncdaemon
[NFS_MAXASYNCDAEMON
];
133 int nfsd_waiting
= 0;
134 static struct nfsdrt nfsdrt
;
135 static int nfs_numnfsd
= 0;
136 static int notstarted
= 1;
137 static int modify_flag
= 0;
138 static void nfsd_rt
__P((int sotype
, struct nfsrv_descript
*nd
,
140 static int nfssvc_addsock
__P((struct file
*, struct mbuf
*,
142 static int nfssvc_nfsd
__P((struct nfsd_srvargs
*,caddr_t
,struct proc
*));
144 static int nfs_privport
= 0;
145 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
147 SYSCTL_INT(_vfs_nfs
, NFS_NFSPRIVPORT
, nfs_privport
, CTLFLAG_RW
, &nfs_privport
, 0, "");
148 SYSCTL_INT(_vfs_nfs
, OID_AUTO
, gatherdelay
, CTLFLAG_RW
, &nfsrvw_procrastinate
, 0, "");
149 SYSCTL_INT(_vfs_nfs
, OID_AUTO
, gatherdelay_v3
, CTLFLAG_RW
, &nfsrvw_procrastinate_v3
, 0, "");
153 * NFS server system calls
154 * getfh() lives here too, but maybe should move to kern/vfs_syscalls.c
158 * Get file handle system call
160 #ifndef _SYS_SYSPROTO_H_
169 register struct getfh_args
*uap
;
171 register struct vnode
*vp
;
179 error
= suser(p
->p_ucred
, &p
->p_acflag
);
182 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
| AUDITVNPATH1
, UIO_USERSPACE
, uap
->fname
, p
);
187 bzero((caddr_t
)&fh
, sizeof(fh
));
188 fh
.fh_fsid
= vp
->v_mount
->mnt_stat
.f_fsid
;
189 error
= VFS_VPTOFH(vp
, &fh
.fh_fid
);
193 error
= copyout((caddr_t
)&fh
, (caddr_t
)uap
->fhp
, sizeof (fh
));
197 #endif /* NFS_NOSERVER */
200 * syscall for the rpc.lockd to use to translate a NFS file handle into
201 * an open descriptor.
203 * warning: do not remove the suser() call or this becomes one giant
206 #ifndef _SYS_SYSPROTO_H_
208 const struct fhandle
*u_fhp
;
213 fhopen(p
, uap
, retval
)
215 register struct fhopen_args
*uap
;
222 struct vattr
*vap
= &vat
;
225 register struct filedesc
*fdp
= p
->p_fd
;
226 int fmode
, mode
, error
, type
;
229 struct ucred
*credanon
;
231 struct ucred
*cred
= p
->p_ucred
;
233 extern struct fileops vnops
;
238 error
= suser(cred
, &p
->p_acflag
);
242 fmode
= FFLAGS(uap
->flags
);
243 /* why not allow a non-read/write open for our lockd? */
244 if (((fmode
& (FREAD
| FWRITE
)) == 0) || (fmode
& O_CREAT
))
246 error
= copyin((void*)uap
->u_fhp
, &fhp
, sizeof(fhp
));
249 /* find the mount point */
250 mp
= vfs_getvfs(&fhp
.fh_fsid
);
253 /* now give me my vnode, it gets returned to me locked */
254 /* XXX CSM need to split VFS_CHECKEXP out of VFS_FHTOVP? */
255 error
= VFS_FHTOVP(mp
, &fhp
.fh_fid
, NULL
, &vp
, &exflags
, &credanon
);
259 * from now on we have to make sure not
260 * to forget about the vnode
261 * any error that causes an abort must vput(vp)
262 * just set error = err and 'goto bad;'.
268 if (vp
->v_type
== VSOCK
) {
273 if (UBCINFOEXISTS(vp
) && ((didhold
= ubc_hold(vp
)) == 0)) {
278 if (fmode
& FREAD
&& fmode
& (FWRITE
| O_TRUNC
)) {
280 if (vp
->v_type
== VDIR
)
283 err
= vn_writechk(vp
);
284 if (err
&& !(error
= VOP_ACCESS(vp
, VREAD
, cred
, p
)))
286 if (error
|| (error
= VOP_ACCESS(vp
, VREAD
|VWRITE
, cred
, p
)))
288 } else if (fmode
& FREAD
) {
289 if ((error
= VOP_ACCESS(vp
, VREAD
, cred
, p
)))
291 } else if (fmode
& (FWRITE
| O_TRUNC
)) {
292 if (vp
->v_type
== VDIR
) {
296 if ((error
= vn_writechk(vp
)) ||
297 (error
= VOP_ACCESS(vp
, VWRITE
, cred
, p
)))
300 if (fmode
& O_TRUNC
) {
301 VOP_UNLOCK(vp
, 0, p
); /* XXX */
302 VOP_LEASE(vp
, p
, cred
, LEASE_WRITE
);
303 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
); /* XXX */
306 error
= VOP_SETATTR(vp
, vap
, cred
, p
);
311 error
= VOP_OPEN(vp
, fmode
, cred
, p
);
316 if (++vp
->v_writecount
<= 0)
317 panic("fhopen: v_writecount");
319 * end of vn_open code
322 if ((error
= falloc(p
, &nfp
, &indx
)) != 0) {
330 * Hold an extra reference to avoid having fp ripped out
331 * from under us while we block in the lock op
334 nfp
->f_data
= (caddr_t
)vp
;
335 nfp
->f_flag
= fmode
& FMASK
;
337 nfp
->f_type
= DTYPE_VNODE
;
338 if (fmode
& (O_EXLOCK
| O_SHLOCK
)) {
339 lf
.l_whence
= SEEK_SET
;
342 if (fmode
& O_EXLOCK
)
347 if ((fmode
& FNONBLOCK
) == 0)
349 VOP_UNLOCK(vp
, 0, p
);
350 if ((error
= VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_SETLK
, &lf
,
352 (void) vn_close(vp
, fp
->f_flag
, fp
->f_cred
, p
);
356 * release our private reference
362 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
363 fp
->f_flag
|= FHASLOCK
;
366 VOP_UNLOCK(vp
, 0, p
);
367 *fdflags(p
, indx
) &= ~UF_RESERVED
;
373 VOP_UNLOCK(vp
, 0, p
);
381 * Nfs server psuedo system call for the nfsd's
382 * Based on the flag value it either:
383 * - adds a socket to the selection list
384 * - remains in the kernel as an nfsd
385 * - remains in the kernel as an nfsiod
387 #ifndef _SYS_SYSPROTO_H_
396 register struct nfssvc_args
*uap
;
402 struct nfsd_args nfsdarg
;
403 struct nfsd_srvargs nfsd_srvargs
, *nsd
= &nfsd_srvargs
;
404 struct nfsd_cargs ncd
;
406 struct nfssvc_sock
*slp
;
407 struct nfsuid
*nuidp
;
408 struct nfsmount
*nmp
;
410 #endif /* NFS_NOSERVER */
416 error
= suser(p
->p_ucred
, &p
->p_acflag
);
419 while (nfssvc_sockhead_flag
& SLP_INIT
) {
420 nfssvc_sockhead_flag
|= SLP_WANTINIT
;
421 (void) tsleep((caddr_t
)&nfssvc_sockhead
, PSOCK
, "nfsd init", 0);
423 if (uap
->flag
& NFSSVC_BIOD
)
424 error
= nfssvc_iod(p
);
428 #else /* !NFS_NOSERVER */
429 else if (uap
->flag
& NFSSVC_MNTD
) {
430 error
= copyin(uap
->argp
, (caddr_t
)&ncd
, sizeof (ncd
));
433 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_USERSPACE
,
438 if ((nd
.ni_vp
->v_flag
& VROOT
) == 0)
440 nmp
= VFSTONFS(nd
.ni_vp
->v_mount
);
445 if ((nmp
->nm_state
& NFSSTA_MNTD
) &&
446 (uap
->flag
& NFSSVC_GOTAUTH
) == 0)
448 nmp
->nm_state
|= NFSSTA_MNTD
;
449 error
= nqnfs_clientd(nmp
, p
->p_ucred
, &ncd
, uap
->flag
,
451 } else if (uap
->flag
& NFSSVC_ADDSOCK
) {
452 error
= copyin(uap
->argp
, (caddr_t
)&nfsdarg
, sizeof(nfsdarg
));
455 error
= getsock(p
->p_fd
, nfsdarg
.sock
, &fp
);
459 * Get the client address for connected sockets.
461 if (nfsdarg
.name
== NULL
|| nfsdarg
.namelen
== 0)
462 nam
= (struct mbuf
*)0;
464 error
= sockargs(&nam
, nfsdarg
.name
, nfsdarg
.namelen
,
469 error
= nfssvc_addsock(fp
, nam
, p
);
471 error
= copyin(uap
->argp
, (caddr_t
)nsd
, sizeof (*nsd
));
475 if ((uap
->flag
& NFSSVC_AUTHIN
) && ((nfsd
= nsd
->nsd_nfsd
)) &&
476 (nfsd
->nfsd_slp
->ns_flag
& SLP_VALID
)) {
477 slp
= nfsd
->nfsd_slp
;
480 * First check to see if another nfsd has already
481 * added this credential.
483 for (nuidp
= NUIDHASH(slp
,nsd
->nsd_cr
.cr_uid
)->lh_first
;
484 nuidp
!= 0; nuidp
= nuidp
->nu_hash
.le_next
) {
485 if (nuidp
->nu_cr
.cr_uid
== nsd
->nsd_cr
.cr_uid
&&
486 (!nfsd
->nfsd_nd
->nd_nam2
||
487 netaddr_match(NU_NETFAM(nuidp
),
488 &nuidp
->nu_haddr
, nfsd
->nfsd_nd
->nd_nam2
)))
492 nfsrv_setcred(&nuidp
->nu_cr
,&nfsd
->nfsd_nd
->nd_cr
);
493 nfsd
->nfsd_nd
->nd_flag
|= ND_KERBFULL
;
498 if (slp
->ns_numuids
< nuidhash_max
) {
500 nuidp
= (struct nfsuid
*)
501 _MALLOC_ZONE(sizeof (struct nfsuid
),
504 nuidp
= (struct nfsuid
*)0;
505 if ((slp
->ns_flag
& SLP_VALID
) == 0) {
507 FREE_ZONE((caddr_t
)nuidp
,
508 sizeof (struct nfsuid
), M_NFSUID
);
510 if (nuidp
== (struct nfsuid
*)0) {
511 nuidp
= slp
->ns_uidlruhead
.tqh_first
;
512 LIST_REMOVE(nuidp
, nu_hash
);
513 TAILQ_REMOVE(&slp
->ns_uidlruhead
, nuidp
,
515 if (nuidp
->nu_flag
& NU_NAM
)
516 m_freem(nuidp
->nu_nam
);
519 nuidp
->nu_cr
= nsd
->nsd_cr
;
520 if (nuidp
->nu_cr
.cr_ngroups
> NGROUPS
)
521 nuidp
->nu_cr
.cr_ngroups
= NGROUPS
;
522 nuidp
->nu_cr
.cr_ref
= 1;
523 nuidp
->nu_timestamp
= nsd
->nsd_timestamp
;
525 nuidp
->nu_expire
= now
.tv_sec
+ nsd
->nsd_ttl
;
527 * and save the session key in nu_key.
529 bcopy(nsd
->nsd_key
, nuidp
->nu_key
,
530 sizeof (nsd
->nsd_key
));
531 if (nfsd
->nfsd_nd
->nd_nam2
) {
532 struct sockaddr_in
*saddr
;
534 saddr
= mtod(nfsd
->nfsd_nd
->nd_nam2
,
535 struct sockaddr_in
*);
536 switch (saddr
->sin_family
) {
538 nuidp
->nu_flag
|= NU_INETADDR
;
540 saddr
->sin_addr
.s_addr
;
544 nuidp
->nu_flag
|= NU_NAM
;
545 nuidp
->nu_nam
= m_copym(
546 nfsd
->nfsd_nd
->nd_nam2
, 0,
551 TAILQ_INSERT_TAIL(&slp
->ns_uidlruhead
, nuidp
,
553 LIST_INSERT_HEAD(NUIDHASH(slp
, nsd
->nsd_uid
),
555 nfsrv_setcred(&nuidp
->nu_cr
,
556 &nfsd
->nfsd_nd
->nd_cr
);
557 nfsd
->nfsd_nd
->nd_flag
|= ND_KERBFULL
;
561 if ((uap
->flag
& NFSSVC_AUTHINFAIL
) && (nfsd
= nsd
->nsd_nfsd
))
562 nfsd
->nfsd_flag
|= NFSD_AUTHFAIL
;
563 error
= nfssvc_nfsd(nsd
, uap
->argp
, p
);
565 #endif /* NFS_NOSERVER */
566 if (error
== EINTR
|| error
== ERESTART
)
573 * Adds a socket to the list for servicing by nfsds.
576 nfssvc_addsock(fp
, mynam
, p
)
581 register struct mbuf
*m
;
583 register struct nfssvc_sock
*slp
;
584 register struct socket
*so
;
585 struct nfssvc_sock
*tslp
;
588 so
= (struct socket
*)fp
->f_data
;
589 tslp
= (struct nfssvc_sock
*)0;
591 * Add it to the list, as required.
593 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
594 if (so
->so_proto
->pr_protocol
== IPPROTO_UDP
) {
596 if (tslp
->ns_flag
& SLP_VALID
) {
598 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
602 } else if (so
->so_proto
->pr_protocol
== ISOPROTO_CLTP
) {
604 if (tslp
->ns_flag
& SLP_VALID
) {
606 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
611 /* reserve buffer space for 2 maximally-sized packets */
613 if (so
->so_type
== SOCK_STREAM
)
614 siz
+= sizeof (u_long
);
616 if (siz
> NFS_MAXSOCKBUF
)
617 siz
= NFS_MAXSOCKBUF
;
618 error
= soreserve(so
, siz
, siz
);
621 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
626 * Set protocol specific options { for now TCP only } and
627 * reserve some space. For datagram sockets, this can get called
628 * repeatedly for the same socket, but that isn't harmful.
630 if (so
->so_type
== SOCK_STREAM
) {
634 bzero(&sopt
, sizeof sopt
);
635 sopt
.sopt_dir
= SOPT_SET
;
636 sopt
.sopt_level
= SOL_SOCKET
;
637 sopt
.sopt_name
= SO_KEEPALIVE
;
638 sopt
.sopt_val
= &val
;
639 sopt
.sopt_valsize
= sizeof val
;
643 if (so
->so_proto
->pr_domain
->dom_family
== AF_INET
&&
644 so
->so_proto
->pr_protocol
== IPPROTO_TCP
) {
648 bzero(&sopt
, sizeof sopt
);
649 sopt
.sopt_dir
= SOPT_SET
;
650 sopt
.sopt_level
= IPPROTO_TCP
;
651 sopt
.sopt_name
= TCP_NODELAY
;
652 sopt
.sopt_val
= &val
;
653 sopt
.sopt_valsize
= sizeof val
;
658 so
->so_rcv
.sb_flags
&= ~SB_NOINTR
;
659 so
->so_rcv
.sb_timeo
= 0;
660 so
->so_snd
.sb_flags
&= ~SB_NOINTR
;
661 so
->so_snd
.sb_timeo
= 0;
662 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
666 MALLOC(slp
, struct nfssvc_sock
*, sizeof(struct nfssvc_sock
),
668 bzero((caddr_t
)slp
, sizeof (struct nfssvc_sock
));
669 TAILQ_INIT(&slp
->ns_uidlruhead
);
670 TAILQ_INSERT_TAIL(&nfssvc_sockhead
, slp
, ns_chain
);
676 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
678 so
->so_upcallarg
= (caddr_t
)slp
;
679 so
->so_upcall
= nfsrv_rcv
;
680 so
->so_rcv
.sb_flags
|= SB_UPCALL
; /* required for freebsd merge */
681 slp
->ns_nflag
= SLPN_NEEDQ
;
682 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
683 slp
->ns_flag
= SLP_VALID
;
690 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
691 * until it is killed by a signal.
694 nfssvc_nfsd(nsd
, argp
, p
)
695 struct nfsd_srvargs
*nsd
;
699 register struct mbuf
*m
;
701 register struct nfssvc_sock
*slp
;
702 register struct socket
*so
;
703 struct nfsd
*nfsd
= nsd
->nsd_nfsd
;
704 struct nfsrv_descript
*nd
= NULL
;
706 int error
= 0, cacherep
, s
, sotype
, writes_todo
;
716 if (nfsd
== (struct nfsd
*)0) {
717 MALLOC(nfsd
, struct nfsd
*, sizeof(struct nfsd
), M_NFSD
, M_WAITOK
);
718 nsd
->nsd_nfsd
= nfsd
;
719 bzero((caddr_t
)nfsd
, sizeof (struct nfsd
));
720 nfsd
->nfsd_procp
= p
;
721 TAILQ_INSERT_TAIL(&nfsd_head
, nfsd
, nfsd_chain
);
725 * Loop getting rpc requests until SIGKILL.
728 if ((nfsd
->nfsd_flag
& NFSD_REQINPROG
) == 0) {
729 while (nfsd
->nfsd_slp
== (struct nfssvc_sock
*)0 &&
730 (nfsd_head_flag
& NFSD_CHECKSLP
) == 0) {
731 nfsd
->nfsd_flag
|= NFSD_WAITING
;
733 error
= tsleep((caddr_t
)nfsd
, PSOCK
| PCATCH
,
739 if (nfsd
->nfsd_slp
== (struct nfssvc_sock
*)0 &&
740 (nfsd_head_flag
& NFSD_CHECKSLP
) != 0) {
741 for (slp
= nfssvc_sockhead
.tqh_first
; slp
!= 0;
742 slp
= slp
->ns_chain
.tqe_next
) {
743 if ((slp
->ns_flag
& (SLP_VALID
| SLP_DOREC
))
744 == (SLP_VALID
| SLP_DOREC
)) {
745 slp
->ns_flag
&= ~SLP_DOREC
;
747 nfsd
->nfsd_slp
= slp
;
752 nfsd_head_flag
&= ~NFSD_CHECKSLP
;
754 if ((slp
= nfsd
->nfsd_slp
) == (struct nfssvc_sock
*)0)
756 if (slp
->ns_flag
& SLP_VALID
) {
758 if (slp
->ns_nflag
& SLPN_DISCONN
) {
760 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
761 } else if (slp
->ns_nflag
& SLPN_NEEDQ
) {
762 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
763 slp
->ns_nflag
&= ~SLPN_NEEDQ
;
764 nfsrv_rcv(slp
->ns_so
, (caddr_t
)slp
,
767 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
768 error
= nfsrv_dorec(slp
, nfsd
, &nd
);
769 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
772 cur_usec
= (u_quad_t
)now
.tv_sec
* 1000000 +
773 (u_quad_t
)now
.tv_usec
;
774 if (error
&& slp
->ns_tq
.lh_first
&&
775 slp
->ns_tq
.lh_first
->nd_time
<= cur_usec
) {
781 nfsd
->nfsd_flag
|= NFSD_REQINPROG
;
785 slp
= nfsd
->nfsd_slp
;
787 if (error
|| (slp
->ns_flag
& SLP_VALID
) == 0) {
790 m_freem(nd
->nd_nam2
);
791 FREE_ZONE((caddr_t
)nd
,
792 sizeof *nd
, M_NFSRVDESC
);
795 nfsd
->nfsd_slp
= (struct nfssvc_sock
*)0;
796 nfsd
->nfsd_flag
&= ~NFSD_REQINPROG
;
802 sotype
= so
->so_type
;
804 microuptime(&nd
->nd_starttime
);
806 nd
->nd_nam
= nd
->nd_nam2
;
808 nd
->nd_nam
= slp
->ns_nam
;
811 * Check to see if authorization is needed.
813 if (nfsd
->nfsd_flag
& NFSD_NEEDAUTH
) {
814 nfsd
->nfsd_flag
&= ~NFSD_NEEDAUTH
;
815 nsd
->nsd_haddr
= mtod(nd
->nd_nam
,
816 struct sockaddr_in
*)->sin_addr
.s_addr
;
817 nsd
->nsd_authlen
= nfsd
->nfsd_authlen
;
818 nsd
->nsd_verflen
= nfsd
->nfsd_verflen
;
819 if (!copyout(nfsd
->nfsd_authstr
,nsd
->nsd_authstr
,
820 nfsd
->nfsd_authlen
) &&
821 !copyout(nfsd
->nfsd_verfstr
, nsd
->nsd_verfstr
,
822 nfsd
->nfsd_verflen
) &&
823 !copyout((caddr_t
)nsd
, argp
, sizeof (*nsd
)))
825 cacherep
= RC_DROPIT
;
827 cacherep
= nfsrv_getcache(nd
, slp
, &mreq
);
830 * Check for just starting up for NQNFS and send
831 * fake "try again later" replies to the NQNFS clients.
834 if (notstarted
&& nqnfsstarttime
<= now
.tv_sec
) {
836 nqnfsstarttime
= now
.tv_sec
+ nqsrv_writeslack
;
842 if ((nd
->nd_flag
& ND_NQNFS
) == 0)
843 cacherep
= RC_DROPIT
;
844 else if (nd
->nd_procnum
!= NFSPROC_WRITE
) {
845 nd
->nd_procnum
= NFSPROC_NOOP
;
846 nd
->nd_repstat
= NQNFS_TRYLATER
;
850 } else if (nfsd
->nfsd_flag
& NFSD_AUTHFAIL
) {
851 nfsd
->nfsd_flag
&= ~NFSD_AUTHFAIL
;
852 nd
->nd_procnum
= NFSPROC_NOOP
;
853 nd
->nd_repstat
= (NFSERR_AUTHERR
| AUTH_TOOWEAK
);
855 } else if (nfs_privport
) {
856 /* Check if source port is privileged */
858 struct sockaddr
*nam
= mtod(nd
->nd_nam
, struct sockaddr
*);
859 struct sockaddr_in
*sin
;
861 sin
= (struct sockaddr_in
*)nam
;
862 port
= ntohs(sin
->sin_port
);
863 if (port
>= IPPORT_RESERVED
&&
864 nd
->nd_procnum
!= NFSPROC_NULL
) {
865 nd
->nd_procnum
= NFSPROC_NOOP
;
866 nd
->nd_repstat
= (NFSERR_AUTHERR
| AUTH_TOOWEAK
);
868 printf("NFS request from unprivileged port (%s:%d)\n",
869 (char *)(inet_ntoa(sin
->sin_addr
)), port
);
876 * Loop to get all the write rpc relies that have been
882 if (nd
&& (nd
->nd_flag
& ND_NFSV3
))
883 procrastinate
= nfsrvw_procrastinate_v3
;
885 procrastinate
= nfsrvw_procrastinate
;
886 if (writes_todo
|| (nd
->nd_procnum
== NFSPROC_WRITE
&&
887 procrastinate
> 0 && !notstarted
))
888 error
= nfsrv_writegather(&nd
, slp
,
889 nfsd
->nfsd_procp
, &mreq
);
891 error
= (*(nfsrv3_procs
[nd
->nd_procnum
]))(nd
,
892 slp
, nfsd
->nfsd_procp
, &mreq
);
896 if (nd
->nd_procnum
!= NQNFSPROC_VACATED
)
898 nfsrv_updatecache(nd
, FALSE
, mreq
);
900 m_freem(nd
->nd_nam2
);
905 nfsstats
.srvrpccnt
[nd
->nd_procnum
]++;
906 nfsrv_updatecache(nd
, TRUE
, mreq
);
907 nd
->nd_mrep
= (struct mbuf
*)0;
915 if (siz
<= 0 || siz
> NFS_MAXPACKET
) {
916 printf("mbuf siz=%d\n",siz
);
917 panic("Bad nfs svc reply");
920 m
->m_pkthdr
.len
= siz
;
921 m
->m_pkthdr
.rcvif
= (struct ifnet
*)0;
923 * For stream protocols, prepend a Sun RPC
926 if (sotype
== SOCK_STREAM
) {
927 M_PREPEND(m
, NFSX_UNSIGNED
, M_WAIT
);
928 *mtod(m
, u_long
*) = htonl(0x80000000 | siz
);
930 if (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
)
931 (void) nfs_slplock(slp
, 1);
932 if (slp
->ns_flag
& SLP_VALID
)
933 error
= nfs_send(so
, nd
->nd_nam2
, m
, NULL
);
940 nfsd_rt(sotype
, nd
, cacherep
);
942 MFREE(nd
->nd_nam2
, m
);
946 m_freem(nd
->nd_mrep
);
951 if (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
)
953 if (error
== EINTR
|| error
== ERESTART
) {
954 FREE_ZONE((caddr_t
)nd
,
955 sizeof *nd
, M_NFSRVDESC
);
963 nfsd_rt(sotype
, nd
, cacherep
);
964 m_freem(nd
->nd_mrep
);
965 m_freem(nd
->nd_nam2
);
966 nd
->nd_mrep
= nd
->nd_nam2
= NULL
;
971 m_freem(nd
->nd_mrep
);
973 m_freem(nd
->nd_nam2
);
974 FREE_ZONE((caddr_t
)nd
, sizeof *nd
, M_NFSRVDESC
);
979 * Check to see if there are outstanding writes that
980 * need to be serviced.
983 cur_usec
= (u_quad_t
)now
.tv_sec
* 1000000 +
984 (u_quad_t
)now
.tv_usec
;
986 if (slp
->ns_tq
.lh_first
&&
987 slp
->ns_tq
.lh_first
->nd_time
<= cur_usec
) {
993 } while (writes_todo
);
995 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
996 if (nfsrv_dorec(slp
, nfsd
, &nd
)) {
997 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
998 nfsd
->nfsd_flag
&= ~NFSD_REQINPROG
;
999 nfsd
->nfsd_slp
= NULL
;
1000 nfsrv_slpderef(slp
);
1002 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1005 TAILQ_REMOVE(&nfsd_head
, nfsd
, nfsd_chain
);
1007 _FREE((caddr_t
)nfsd
, M_NFSD
);
1008 nsd
->nsd_nfsd
= (struct nfsd
*)0;
1009 if (--nfs_numnfsd
== 0)
1010 nfsrv_init(TRUE
); /* Reinitialize everything */
1013 #endif /* NFS_NOSERVER */
1016 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
1018 SYSCTL_INT(_vfs_nfs
, OID_AUTO
, defect
, CTLFLAG_RW
, &nfs_defect
, 0, "");
1021 #ifndef _SYS_SYSPROTO_H_
1022 struct nfsclnt_args
{
1028 nfsclnt(struct proc
*p
, struct nfsclnt_args
*uap
)
1030 struct lockd_ans la
;
1033 if (uap
->flag
== NFSCLNT_LOCKDWAIT
) {
1034 return (nfslockdwait(p
));
1036 if (uap
->flag
== NFSCLNT_LOCKDANS
) {
1037 error
= copyin(uap
->argp
, &la
, sizeof(la
));
1038 return (error
!= 0 ? error
: nfslockdans(p
, &la
));
1040 if (uap
->flag
== NFSCLNT_LOCKDFD
)
1041 return (nfslockdfd(p
, (int)uap
->argp
));
1046 static int nfssvc_iod_continue(int);
1049 * Asynchronous I/O daemons for client nfs.
1050 * They do read-ahead and write-behind operations on the block I/O cache.
1051 * Never returns unless it fails or gets killed.
1057 register int i
, myiod
;
1058 struct nfsmount
*nmp
;
1063 * Assign my position or return error if too many already running
1066 for (i
= 0; i
< NFS_MAXASYNCDAEMON
; i
++)
1067 if (nfs_asyncdaemon
[i
] == 0) {
1068 nfs_asyncdaemon
[i
]++;
1076 /* stuff myiod into uthread to get off local stack for continuation */
1078 ut
= (struct uthread
*)get_bsdthread_info(current_act());
1079 ut
->uu_state
.uu_nfs_myiod
= myiod
; /* squirrel away for continuation */
1081 nfssvc_iod_continue(0);
1087 * Continuation for Asynchronous I/O daemons for client nfs.
1090 nfssvc_iod_continue(error
)
1092 register struct nfsbuf
*bp
;
1093 register int i
, myiod
;
1094 struct nfsmount
*nmp
;
1099 * real myiod is stored in uthread, recover it
1101 ut
= (struct uthread
*)get_bsdthread_info(current_act());
1102 myiod
= ut
->uu_state
.uu_nfs_myiod
;
1106 * Just loop around doin our stuff until SIGKILL
1107 * - actually we don't loop with continuations...
1110 while (((nmp
= nfs_iodmount
[myiod
]) == NULL
1111 || nmp
->nm_bufq
.tqh_first
== NULL
)
1112 && error
== 0 && nfs_ioddelwri
== 0) {
1115 nfs_iodwant
[myiod
] = p
;
1116 nfs_iodmount
[myiod
] = NULL
;
1117 error
= tsleep0((caddr_t
)&nfs_iodwant
[myiod
],
1118 PWAIT
| PCATCH
, "nfsidl", 0, nfssvc_iod_continue
);
1122 nfs_asyncdaemon
[myiod
] = 0;
1123 if (nmp
) nmp
->nm_bufqiods
--;
1124 nfs_iodwant
[myiod
] = NULL
;
1125 nfs_iodmount
[myiod
] = NULL
;
1127 if (error
== EINTR
|| error
== ERESTART
)
1129 unix_syscall_return(error
);
1132 while ((bp
= nmp
->nm_bufq
.tqh_first
) != NULL
) {
1133 /* Take one off the front of the list */
1134 TAILQ_REMOVE(&nmp
->nm_bufq
, bp
, nb_free
);
1135 bp
->nb_free
.tqe_next
= NFSNOLIST
;
1137 if (nmp
->nm_bufqwant
&& nmp
->nm_bufqlen
< 2 * nfs_numasync
) {
1138 nmp
->nm_bufqwant
= FALSE
;
1139 wakeup(&nmp
->nm_bufq
);
1141 if (ISSET(bp
->nb_flags
, NB_READ
))
1142 (void) nfs_doio(bp
, bp
->nb_rcred
, (struct proc
*)0);
1144 (void) nfs_doio(bp
, bp
->nb_wcred
, (struct proc
*)0);
1147 * If there are more than one iod on this mount, then defect
1148 * so that the iods can be shared out fairly between the mounts
1150 if (nfs_defect
&& nmp
->nm_bufqiods
> 1) {
1152 ("nfssvc_iod: iod %d defecting from mount %p\n",
1154 nfs_iodmount
[myiod
] = NULL
;
1160 if (nfs_ioddelwri
) {
1163 while (i
< 8 && (bp
= TAILQ_FIRST(&nfsbufdelwri
)) != NULL
) {
1164 struct nfsnode
*np
= VTONFS(bp
->nb_vp
);
1165 nfs_buf_remfree(bp
);
1166 if (ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
)) {
1167 /* put buffer at end of delwri list */
1168 TAILQ_INSERT_TAIL(&nfsbufdelwri
, bp
, nb_free
);
1170 nfs_flushcommits(np
->n_vnode
, (struct proc
*)0);
1172 SET(bp
->nb_flags
, (NB_BUSY
| NB_ASYNC
| NB_IOD
));
1182 * Shut down a socket associated with an nfssvc_sock structure.
1183 * Should be called with the send lock set, if required.
1184 * The trick here is to increment the sref at the start, so that the nfsds
1185 * will stop using it and clear ns_flag at the end so that it will not be
1186 * reassigned during cleanup.
1190 register struct nfssvc_sock
*slp
;
1192 register struct nfsuid
*nuidp
, *nnuidp
;
1193 register struct nfsrv_descript
*nwp
, *nnwp
;
1199 slp
->ns_flag
&= ~SLP_ALLFLAGS
;
1200 slp
->ns_nflag
&= ~SLP_ALLFLAGS
;
1203 slp
->ns_fp
= (struct file
*)0;
1205 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1206 so
->so_upcall
= NULL
;
1207 so
->so_rcv
.sb_flags
&= ~SB_UPCALL
;
1210 MFREE(slp
->ns_nam
, m
);
1211 m_freem(slp
->ns_raw
);
1212 m_freem(slp
->ns_rec
);
1213 slp
->ns_nam
= slp
->ns_raw
= slp
->ns_rec
= NULL
;
1214 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1215 closef(fp
, (struct proc
*)0);
1216 for (nuidp
= slp
->ns_uidlruhead
.tqh_first
; nuidp
!= 0;
1218 nnuidp
= nuidp
->nu_lru
.tqe_next
;
1219 LIST_REMOVE(nuidp
, nu_hash
);
1220 TAILQ_REMOVE(&slp
->ns_uidlruhead
, nuidp
, nu_lru
);
1221 if (nuidp
->nu_flag
& NU_NAM
)
1222 m_freem(nuidp
->nu_nam
);
1223 FREE_ZONE((caddr_t
)nuidp
,
1224 sizeof (struct nfsuid
), M_NFSUID
);
1227 for (nwp
= slp
->ns_tq
.lh_first
; nwp
; nwp
= nnwp
) {
1228 nnwp
= nwp
->nd_tq
.le_next
;
1229 LIST_REMOVE(nwp
, nd_tq
);
1230 FREE_ZONE((caddr_t
)nwp
, sizeof *nwp
, M_NFSRVDESC
);
1232 LIST_INIT(&slp
->ns_tq
);
1238 * Get an authorization string for the uid by having the mount_nfs sitting
1239 * on this mount point porpous out of the kernel and do it.
1242 nfs_getauth(nmp
, rep
, cred
, auth_str
, auth_len
, verf_str
, verf_len
, key
)
1243 register struct nfsmount
*nmp
;
1250 NFSKERBKEY_T key
; /* return session key */
1254 while ((nmp
->nm_state
& NFSSTA_WAITAUTH
) == 0) {
1255 nmp
->nm_state
|= NFSSTA_WANTAUTH
;
1256 (void) tsleep((caddr_t
)&nmp
->nm_authtype
, PSOCK
,
1257 "nfsauth1", 2 * hz
);
1258 error
= nfs_sigintr(nmp
, rep
, rep
->r_procp
);
1260 nmp
->nm_state
&= ~NFSSTA_WANTAUTH
;
1264 nmp
->nm_state
&= ~(NFSSTA_WAITAUTH
| NFSSTA_WANTAUTH
);
1265 MALLOC(*auth_str
, char *, RPCAUTH_MAXSIZ
, M_TEMP
, M_WAITOK
);
1266 nmp
->nm_authstr
= *auth_str
;
1267 nmp
->nm_authlen
= RPCAUTH_MAXSIZ
;
1268 nmp
->nm_verfstr
= verf_str
;
1269 nmp
->nm_verflen
= *verf_len
;
1270 nmp
->nm_authuid
= cred
->cr_uid
;
1271 wakeup((caddr_t
)&nmp
->nm_authstr
);
1274 * And wait for mount_nfs to do its stuff.
1276 while ((nmp
->nm_state
& NFSSTA_HASAUTH
) == 0 && error
== 0) {
1277 (void) tsleep((caddr_t
)&nmp
->nm_authlen
, PSOCK
,
1278 "nfsauth2", 2 * hz
);
1279 error
= nfs_sigintr(nmp
, rep
, rep
->r_procp
);
1281 if (nmp
->nm_state
& NFSSTA_AUTHERR
) {
1282 nmp
->nm_state
&= ~NFSSTA_AUTHERR
;
1286 _FREE((caddr_t
)*auth_str
, M_TEMP
);
1288 *auth_len
= nmp
->nm_authlen
;
1289 *verf_len
= nmp
->nm_verflen
;
1290 bcopy((caddr_t
)nmp
->nm_key
, (caddr_t
)key
, sizeof (key
));
1292 nmp
->nm_state
&= ~NFSSTA_HASAUTH
;
1293 nmp
->nm_state
|= NFSSTA_WAITAUTH
;
1294 if (nmp
->nm_state
& NFSSTA_WANTAUTH
) {
1295 nmp
->nm_state
&= ~NFSSTA_WANTAUTH
;
1296 wakeup((caddr_t
)&nmp
->nm_authtype
);
1302 * Get a nickname authenticator and verifier.
1305 nfs_getnickauth(nmp
, cred
, auth_str
, auth_len
, verf_str
, verf_len
)
1306 struct nfsmount
*nmp
;
1313 register struct nfsuid
*nuidp
;
1314 register u_long
*nickp
, *verfp
;
1315 struct timeval ktvin
, ktvout
, now
;
1318 if (verf_len
< (4 * NFSX_UNSIGNED
))
1319 panic("nfs_getnickauth verf too small");
1321 for (nuidp
= NMUIDHASH(nmp
, cred
->cr_uid
)->lh_first
;
1322 nuidp
!= 0; nuidp
= nuidp
->nu_hash
.le_next
) {
1323 if (nuidp
->nu_cr
.cr_uid
== cred
->cr_uid
)
1327 if (!nuidp
|| nuidp
->nu_expire
< now
.tv_sec
)
1331 * Move to the end of the lru list (end of lru == most recently used).
1333 TAILQ_REMOVE(&nmp
->nm_uidlruhead
, nuidp
, nu_lru
);
1334 TAILQ_INSERT_TAIL(&nmp
->nm_uidlruhead
, nuidp
, nu_lru
);
1336 MALLOC(nickp
, u_long
*, 2 * NFSX_UNSIGNED
, M_TEMP
, M_WAITOK
);
1337 *nickp
++ = txdr_unsigned(RPCAKN_NICKNAME
);
1338 *nickp
= txdr_unsigned(nuidp
->nu_nickname
);
1339 *auth_str
= (char *)nickp
;
1340 *auth_len
= 2 * NFSX_UNSIGNED
;
1343 * Now we must encrypt the verifier and package it up.
1345 verfp
= (u_long
*)verf_str
;
1346 *verfp
++ = txdr_unsigned(RPCAKN_NICKNAME
);
1348 if (now
.tv_sec
> nuidp
->nu_timestamp
.tv_sec
||
1349 (now
.tv_sec
== nuidp
->nu_timestamp
.tv_sec
&&
1350 now
.tv_usec
> nuidp
->nu_timestamp
.tv_usec
))
1351 nuidp
->nu_timestamp
= now
;
1353 nuidp
->nu_timestamp
.tv_usec
++;
1354 ktvin
.tv_sec
= txdr_unsigned(nuidp
->nu_timestamp
.tv_sec
);
1355 ktvin
.tv_usec
= txdr_unsigned(nuidp
->nu_timestamp
.tv_usec
);
1358 * Now encrypt the timestamp verifier in ecb mode using the session
1365 *verfp
++ = ktvout
.tv_sec
;
1366 *verfp
++ = ktvout
.tv_usec
;
1372 * Save the current nickname in a hash list entry on the mount point.
1375 nfs_savenickauth(nmp
, cred
, len
, key
, mdp
, dposp
, mrep
)
1376 register struct nfsmount
*nmp
;
1384 register struct nfsuid
*nuidp
;
1385 register u_long
*tl
;
1387 struct mbuf
*md
= *mdp
;
1388 struct timeval ktvin
, ktvout
, now
;
1390 char *dpos
= *dposp
, *cp2
;
1391 int deltasec
, error
= 0;
1393 if (len
== (3 * NFSX_UNSIGNED
)) {
1394 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
1395 ktvin
.tv_sec
= *tl
++;
1396 ktvin
.tv_usec
= *tl
++;
1397 nick
= fxdr_unsigned(u_long
, *tl
);
1400 * Decrypt the timestamp in ecb mode.
1405 ktvout
.tv_sec
= fxdr_unsigned(long, ktvout
.tv_sec
);
1406 ktvout
.tv_usec
= fxdr_unsigned(long, ktvout
.tv_usec
);
1408 deltasec
= now
.tv_sec
- ktvout
.tv_sec
;
1410 deltasec
= -deltasec
;
1412 * If ok, add it to the hash list for the mount point.
1414 if (deltasec
<= NFS_KERBCLOCKSKEW
) {
1415 if (nmp
->nm_numuids
< nuidhash_max
) {
1417 MALLOC_ZONE(nuidp
, struct nfsuid
*,
1418 sizeof (struct nfsuid
),
1419 M_NFSUID
, M_WAITOK
);
1421 nuidp
= nmp
->nm_uidlruhead
.tqh_first
;
1422 LIST_REMOVE(nuidp
, nu_hash
);
1423 TAILQ_REMOVE(&nmp
->nm_uidlruhead
, nuidp
,
1427 nuidp
->nu_cr
.cr_uid
= cred
->cr_uid
;
1428 nuidp
->nu_expire
= now
.tv_sec
+ NFS_KERBTTL
;
1429 nuidp
->nu_timestamp
= ktvout
;
1430 nuidp
->nu_nickname
= nick
;
1431 bcopy(key
, nuidp
->nu_key
, sizeof (key
));
1432 TAILQ_INSERT_TAIL(&nmp
->nm_uidlruhead
, nuidp
,
1434 LIST_INSERT_HEAD(NMUIDHASH(nmp
, cred
->cr_uid
),
1438 nfsm_adv(nfsm_rndup(len
));
1445 #ifndef NFS_NOSERVER
1448 * Derefence a server socket structure. If it has no more references and
1449 * is no longer valid, you can throw it away.
1453 register struct nfssvc_sock
*slp
;
1455 if (--(slp
->ns_sref
) == 0 && (slp
->ns_flag
& SLP_VALID
) == 0) {
1456 TAILQ_REMOVE(&nfssvc_sockhead
, slp
, ns_chain
);
1457 _FREE((caddr_t
)slp
, M_NFSSVC
);
1462 * Lock a socket against others.
1465 nfs_slplock(slp
, wait
)
1466 register struct nfssvc_sock
*slp
;
1469 int *statep
= &slp
->ns_solock
;
1471 if (!wait
&& (*statep
& NFSSTA_SNDLOCK
))
1472 return(0); /* already locked, fail */
1473 while (*statep
& NFSSTA_SNDLOCK
) {
1474 *statep
|= NFSSTA_WANTSND
;
1475 (void) tsleep((caddr_t
)statep
, PZERO
- 1, "nfsslplck", 0);
1477 *statep
|= NFSSTA_SNDLOCK
;
1482 * Unlock the stream socket for others.
1486 struct nfssvc_sock
*slp
;
1488 int *statep
= &slp
->ns_solock
;
1490 if ((*statep
& NFSSTA_SNDLOCK
) == 0)
1491 panic("nfs slpunlock");
1492 *statep
&= ~NFSSTA_SNDLOCK
;
1493 if (*statep
& NFSSTA_WANTSND
) {
1494 *statep
&= ~NFSSTA_WANTSND
;
1495 wakeup((caddr_t
)statep
);
1500 * Initialize the data structures for the server.
1501 * Handshake with any new nfsds starting up to avoid any chance of
1505 nfsrv_init(terminating
)
1508 register struct nfssvc_sock
*slp
, *nslp
;
1510 if (nfssvc_sockhead_flag
& SLP_INIT
)
1512 nfssvc_sockhead_flag
|= SLP_INIT
;
1514 for (slp
= nfssvc_sockhead
.tqh_first
; slp
!= 0; slp
= nslp
) {
1515 nslp
= slp
->ns_chain
.tqe_next
;
1516 if (slp
->ns_flag
& SLP_VALID
)
1518 TAILQ_REMOVE(&nfssvc_sockhead
, slp
, ns_chain
);
1519 _FREE((caddr_t
)slp
, M_NFSSVC
);
1521 nfsrv_cleancache(); /* And clear out server cache */
1522 /* XXX CSM 12/4/97 Revisit when enabling WebNFS */
1525 nfs_pub
.np_valid
= 0;
1530 TAILQ_INIT(&nfssvc_sockhead
);
1531 nfssvc_sockhead_flag
&= ~SLP_INIT
;
1532 if (nfssvc_sockhead_flag
& SLP_WANTINIT
) {
1533 nfssvc_sockhead_flag
&= ~SLP_WANTINIT
;
1534 wakeup((caddr_t
)&nfssvc_sockhead
);
1537 TAILQ_INIT(&nfsd_head
);
1538 nfsd_head_flag
&= ~NFSD_CHECKSLP
;
1540 MALLOC(nfs_udpsock
, struct nfssvc_sock
*, sizeof(struct nfssvc_sock
),
1541 M_NFSSVC
, M_WAITOK
);
1542 bzero((caddr_t
)nfs_udpsock
, sizeof (struct nfssvc_sock
));
1543 TAILQ_INIT(&nfs_udpsock
->ns_uidlruhead
);
1544 TAILQ_INSERT_HEAD(&nfssvc_sockhead
, nfs_udpsock
, ns_chain
);
1546 MALLOC(nfs_cltpsock
, struct nfssvc_sock
*, sizeof(struct nfssvc_sock
),
1547 M_NFSSVC
, M_WAITOK
);
1548 bzero((caddr_t
)nfs_cltpsock
, sizeof (struct nfssvc_sock
));
1549 TAILQ_INIT(&nfs_cltpsock
->ns_uidlruhead
);
1550 TAILQ_INSERT_TAIL(&nfssvc_sockhead
, nfs_cltpsock
, ns_chain
);
1554 * Add entries to the server monitor log.
1557 nfsd_rt(sotype
, nd
, cacherep
)
1559 register struct nfsrv_descript
*nd
;
1562 register struct drt
*rt
;
1565 rt
= &nfsdrt
.drt
[nfsdrt
.pos
];
1566 if (cacherep
== RC_DOIT
)
1568 else if (cacherep
== RC_REPLY
)
1569 rt
->flag
= DRT_CACHEREPLY
;
1571 rt
->flag
= DRT_CACHEDROP
;
1572 if (sotype
== SOCK_STREAM
)
1573 rt
->flag
|= DRT_TCP
;
1574 if (nd
->nd_flag
& ND_NQNFS
)
1575 rt
->flag
|= DRT_NQNFS
;
1576 else if (nd
->nd_flag
& ND_NFSV3
)
1577 rt
->flag
|= DRT_NFSV3
;
1578 rt
->proc
= nd
->nd_procnum
;
1579 if (mtod(nd
->nd_nam
, struct sockaddr
*)->sa_family
== AF_INET
)
1580 rt
->ipadr
= mtod(nd
->nd_nam
, struct sockaddr_in
*)->sin_addr
.s_addr
;
1582 rt
->ipadr
= INADDR_ANY
;
1584 rt
->resptime
= ((now
.tv_sec
- nd
->nd_starttime
.tv_sec
) * 1000000) +
1585 (now
.tv_usec
- nd
->nd_starttime
.tv_usec
);
1586 microtime(&rt
->tstamp
); // XXX unused
1587 nfsdrt
.pos
= (nfsdrt
.pos
+ 1) % NFSRTTLOGSIZ
;
1589 #endif /* NFS_NOSERVER */