2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
27 * Copyright (c) 1989, 1993
28 * The Regents of the University of California. All rights reserved.
30 * This code is derived from software contributed to Berkeley by
31 * Rick Macklem at The University of Guelph.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)nfs_syscalls.c 8.5 (Berkeley) 3/30/95
62 * FreeBSD-Id: nfs_syscalls.c,v 1.32 1997/11/07 08:53:25 phk Exp $
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 /* XXX CSM 11/25/97 FreeBSD's generated syscall prototypes */
69 #include <sys/sysproto.h>
71 #include <sys/kernel.h>
73 #include <sys/filedesc.h>
75 #include <sys/vnode.h>
76 #include <sys/mount.h>
78 #include <sys/sysctl.h>
81 #include <sys/malloc.h>
83 #include <sys/socket.h>
84 #include <sys/socketvar.h>
85 #include <sys/domain.h>
86 #include <sys/protosw.h>
87 #include <sys/namei.h>
88 #include <sys/fcntl.h>
89 #include <sys/lockf.h>
90 #include <sys/syslog.h>
92 #include <machine/spl.h>
94 #include <netinet/in.h>
95 #include <netinet/tcp.h>
97 #include <netiso/iso.h>
99 #include <nfs/xdr_subs.h>
100 #include <nfs/rpcv2.h>
101 #include <nfs/nfsproto.h>
103 #include <nfs/nfsm_subs.h>
104 #include <nfs/nfsrvcache.h>
105 #include <nfs/nfsmount.h>
106 #include <nfs/nfsnode.h>
107 #include <nfs/nqnfs.h>
108 #include <nfs/nfsrtt.h>
109 #include <nfs/nfs_lock.h>
112 extern int (*nfsrv3_procs
[NFS_NPROCS
]) __P((struct nfsrv_descript
*nd
,
113 struct nfssvc_sock
*slp
,
115 struct mbuf
**mreqp
));
116 extern int nfs_numasync
;
117 extern int nfs_ioddelwri
;
118 extern time_t nqnfsstarttime
;
119 extern int nqsrv_writeslack
;
121 extern struct nfsstats nfsstats
;
122 extern int nfsrvw_procrastinate
;
123 extern int nfsrvw_procrastinate_v3
;
124 struct nfssvc_sock
*nfs_udpsock
, *nfs_cltpsock
;
125 static int nuidhash_max
= NFS_MAXUIDHASH
;
127 static void nfsrv_zapsock
__P((struct nfssvc_sock
*slp
));
128 static int nfssvc_iod
__P((struct proc
*));
133 static int nfs_asyncdaemon
[NFS_MAXASYNCDAEMON
];
136 int nfsd_waiting
= 0;
137 static struct nfsdrt nfsdrt
;
138 static int nfs_numnfsd
= 0;
139 static int notstarted
= 1;
140 static int modify_flag
= 0;
141 static void nfsd_rt
__P((int sotype
, struct nfsrv_descript
*nd
,
143 static int nfssvc_addsock
__P((struct file
*, struct mbuf
*,
145 static int nfssvc_nfsd
__P((struct nfsd_srvargs
*,caddr_t
,struct proc
*));
147 static int nfs_privport
= 0;
148 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
150 SYSCTL_INT(_vfs_nfs
, NFS_NFSPRIVPORT
, nfs_privport
, CTLFLAG_RW
, &nfs_privport
, 0, "");
151 SYSCTL_INT(_vfs_nfs
, OID_AUTO
, gatherdelay
, CTLFLAG_RW
, &nfsrvw_procrastinate
, 0, "");
152 SYSCTL_INT(_vfs_nfs
, OID_AUTO
, gatherdelay_v3
, CTLFLAG_RW
, &nfsrvw_procrastinate_v3
, 0, "");
156 * NFS server system calls
157 * getfh() lives here too, but maybe should move to kern/vfs_syscalls.c
161 * Get file handle system call
163 #ifndef _SYS_SYSPROTO_H_
172 register struct getfh_args
*uap
;
174 register struct vnode
*vp
;
182 error
= suser(p
->p_ucred
, &p
->p_acflag
);
185 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
| AUDITVNPATH1
, UIO_USERSPACE
, uap
->fname
, p
);
190 bzero((caddr_t
)&fh
, sizeof(fh
));
191 fh
.fh_fsid
= vp
->v_mount
->mnt_stat
.f_fsid
;
192 error
= VFS_VPTOFH(vp
, &fh
.fh_fid
);
196 error
= copyout((caddr_t
)&fh
, (caddr_t
)uap
->fhp
, sizeof (fh
));
200 #endif /* NFS_NOSERVER */
203 * syscall for the rpc.lockd to use to translate a NFS file handle into
204 * an open descriptor.
206 * warning: do not remove the suser() call or this becomes one giant
209 #ifndef _SYS_SYSPROTO_H_
211 const struct fhandle
*u_fhp
;
216 fhopen(p
, uap
, retval
)
218 register struct fhopen_args
*uap
;
225 struct vattr
*vap
= &vat
;
228 register struct filedesc
*fdp
= p
->p_fd
;
229 int fmode
, mode
, error
, type
;
232 struct ucred
*credanon
;
234 struct ucred
*cred
= p
->p_ucred
;
236 extern struct fileops vnops
;
241 error
= suser(cred
, &p
->p_acflag
);
245 fmode
= FFLAGS(uap
->flags
);
246 /* why not allow a non-read/write open for our lockd? */
247 if (((fmode
& (FREAD
| FWRITE
)) == 0) || (fmode
& O_CREAT
))
249 error
= copyin((void*)uap
->u_fhp
, &fhp
, sizeof(fhp
));
252 /* find the mount point */
253 mp
= vfs_getvfs(&fhp
.fh_fsid
);
256 /* now give me my vnode, it gets returned to me locked */
257 /* XXX CSM need to split VFS_CHECKEXP out of VFS_FHTOVP? */
258 error
= VFS_FHTOVP(mp
, &fhp
.fh_fid
, NULL
, &vp
, &exflags
, &credanon
);
262 * from now on we have to make sure not
263 * to forget about the vnode
264 * any error that causes an abort must vput(vp)
265 * just set error = err and 'goto bad;'.
271 if (vp
->v_type
== VSOCK
) {
276 if (UBCINFOEXISTS(vp
) && ((didhold
= ubc_hold(vp
)) == 0)) {
281 if (fmode
& FREAD
&& fmode
& (FWRITE
| O_TRUNC
)) {
283 if (vp
->v_type
== VDIR
)
286 err
= vn_writechk(vp
);
287 if (err
&& !(error
= VOP_ACCESS(vp
, VREAD
, cred
, p
)))
289 if (error
|| (error
= VOP_ACCESS(vp
, VREAD
|VWRITE
, cred
, p
)))
291 } else if (fmode
& FREAD
) {
292 if ((error
= VOP_ACCESS(vp
, VREAD
, cred
, p
)))
294 } else if (fmode
& (FWRITE
| O_TRUNC
)) {
295 if (vp
->v_type
== VDIR
) {
299 if ((error
= vn_writechk(vp
)) ||
300 (error
= VOP_ACCESS(vp
, VWRITE
, cred
, p
)))
303 if (fmode
& O_TRUNC
) {
304 VOP_UNLOCK(vp
, 0, p
); /* XXX */
305 VOP_LEASE(vp
, p
, cred
, LEASE_WRITE
);
306 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
); /* XXX */
309 error
= VOP_SETATTR(vp
, vap
, cred
, p
);
314 error
= VOP_OPEN(vp
, fmode
, cred
, p
);
319 if (++vp
->v_writecount
<= 0)
320 panic("fhopen: v_writecount");
322 * end of vn_open code
325 if ((error
= falloc(p
, &nfp
, &indx
)) != 0) {
333 * Hold an extra reference to avoid having fp ripped out
334 * from under us while we block in the lock op
337 nfp
->f_data
= (caddr_t
)vp
;
338 nfp
->f_flag
= fmode
& FMASK
;
340 nfp
->f_type
= DTYPE_VNODE
;
341 if (fmode
& (O_EXLOCK
| O_SHLOCK
)) {
342 lf
.l_whence
= SEEK_SET
;
345 if (fmode
& O_EXLOCK
)
350 if ((fmode
& FNONBLOCK
) == 0)
352 VOP_UNLOCK(vp
, 0, p
);
353 if ((error
= VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_SETLK
, &lf
,
355 (void) vn_close(vp
, fp
->f_flag
, fp
->f_cred
, p
);
359 * release our private reference
365 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
366 fp
->f_flag
|= FHASLOCK
;
369 VOP_UNLOCK(vp
, 0, p
);
370 *fdflags(p
, indx
) &= ~UF_RESERVED
;
376 VOP_UNLOCK(vp
, 0, p
);
384 * Nfs server psuedo system call for the nfsd's
385 * Based on the flag value it either:
386 * - adds a socket to the selection list
387 * - remains in the kernel as an nfsd
388 * - remains in the kernel as an nfsiod
390 #ifndef _SYS_SYSPROTO_H_
399 register struct nfssvc_args
*uap
;
405 struct nfsd_args nfsdarg
;
406 struct nfsd_srvargs nfsd_srvargs
, *nsd
= &nfsd_srvargs
;
407 struct nfsd_cargs ncd
;
409 struct nfssvc_sock
*slp
;
410 struct nfsuid
*nuidp
;
411 struct nfsmount
*nmp
;
413 #endif /* NFS_NOSERVER */
419 error
= suser(p
->p_ucred
, &p
->p_acflag
);
422 while (nfssvc_sockhead_flag
& SLP_INIT
) {
423 nfssvc_sockhead_flag
|= SLP_WANTINIT
;
424 (void) tsleep((caddr_t
)&nfssvc_sockhead
, PSOCK
, "nfsd init", 0);
426 if (uap
->flag
& NFSSVC_BIOD
)
427 error
= nfssvc_iod(p
);
431 #else /* !NFS_NOSERVER */
432 else if (uap
->flag
& NFSSVC_MNTD
) {
433 error
= copyin(uap
->argp
, (caddr_t
)&ncd
, sizeof (ncd
));
436 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_USERSPACE
,
441 if ((nd
.ni_vp
->v_flag
& VROOT
) == 0)
443 nmp
= VFSTONFS(nd
.ni_vp
->v_mount
);
448 if ((nmp
->nm_state
& NFSSTA_MNTD
) &&
449 (uap
->flag
& NFSSVC_GOTAUTH
) == 0)
451 nmp
->nm_state
|= NFSSTA_MNTD
;
452 error
= nqnfs_clientd(nmp
, p
->p_ucred
, &ncd
, uap
->flag
,
454 } else if (uap
->flag
& NFSSVC_ADDSOCK
) {
455 error
= copyin(uap
->argp
, (caddr_t
)&nfsdarg
, sizeof(nfsdarg
));
458 error
= getsock(p
->p_fd
, nfsdarg
.sock
, &fp
);
462 * Get the client address for connected sockets.
464 if (nfsdarg
.name
== NULL
|| nfsdarg
.namelen
== 0)
465 nam
= (struct mbuf
*)0;
467 error
= sockargs(&nam
, nfsdarg
.name
, nfsdarg
.namelen
,
472 error
= nfssvc_addsock(fp
, nam
, p
);
474 error
= copyin(uap
->argp
, (caddr_t
)nsd
, sizeof (*nsd
));
478 if ((uap
->flag
& NFSSVC_AUTHIN
) && ((nfsd
= nsd
->nsd_nfsd
)) &&
479 (nfsd
->nfsd_slp
->ns_flag
& SLP_VALID
)) {
480 slp
= nfsd
->nfsd_slp
;
483 * First check to see if another nfsd has already
484 * added this credential.
486 for (nuidp
= NUIDHASH(slp
,nsd
->nsd_cr
.cr_uid
)->lh_first
;
487 nuidp
!= 0; nuidp
= nuidp
->nu_hash
.le_next
) {
488 if (nuidp
->nu_cr
.cr_uid
== nsd
->nsd_cr
.cr_uid
&&
489 (!nfsd
->nfsd_nd
->nd_nam2
||
490 netaddr_match(NU_NETFAM(nuidp
),
491 &nuidp
->nu_haddr
, nfsd
->nfsd_nd
->nd_nam2
)))
495 nfsrv_setcred(&nuidp
->nu_cr
,&nfsd
->nfsd_nd
->nd_cr
);
496 nfsd
->nfsd_nd
->nd_flag
|= ND_KERBFULL
;
501 if (slp
->ns_numuids
< nuidhash_max
) {
503 nuidp
= (struct nfsuid
*)
504 _MALLOC_ZONE(sizeof (struct nfsuid
),
507 nuidp
= (struct nfsuid
*)0;
508 if ((slp
->ns_flag
& SLP_VALID
) == 0) {
510 FREE_ZONE((caddr_t
)nuidp
,
511 sizeof (struct nfsuid
), M_NFSUID
);
513 if (nuidp
== (struct nfsuid
*)0) {
514 nuidp
= slp
->ns_uidlruhead
.tqh_first
;
515 LIST_REMOVE(nuidp
, nu_hash
);
516 TAILQ_REMOVE(&slp
->ns_uidlruhead
, nuidp
,
518 if (nuidp
->nu_flag
& NU_NAM
)
519 m_freem(nuidp
->nu_nam
);
522 nuidp
->nu_cr
= nsd
->nsd_cr
;
523 if (nuidp
->nu_cr
.cr_ngroups
> NGROUPS
)
524 nuidp
->nu_cr
.cr_ngroups
= NGROUPS
;
525 nuidp
->nu_cr
.cr_ref
= 1;
526 nuidp
->nu_timestamp
= nsd
->nsd_timestamp
;
528 nuidp
->nu_expire
= now
.tv_sec
+ nsd
->nsd_ttl
;
530 * and save the session key in nu_key.
532 bcopy(nsd
->nsd_key
, nuidp
->nu_key
,
533 sizeof (nsd
->nsd_key
));
534 if (nfsd
->nfsd_nd
->nd_nam2
) {
535 struct sockaddr_in
*saddr
;
537 saddr
= mtod(nfsd
->nfsd_nd
->nd_nam2
,
538 struct sockaddr_in
*);
539 switch (saddr
->sin_family
) {
541 nuidp
->nu_flag
|= NU_INETADDR
;
543 saddr
->sin_addr
.s_addr
;
547 nuidp
->nu_flag
|= NU_NAM
;
548 nuidp
->nu_nam
= m_copym(
549 nfsd
->nfsd_nd
->nd_nam2
, 0,
554 TAILQ_INSERT_TAIL(&slp
->ns_uidlruhead
, nuidp
,
556 LIST_INSERT_HEAD(NUIDHASH(slp
, nsd
->nsd_uid
),
558 nfsrv_setcred(&nuidp
->nu_cr
,
559 &nfsd
->nfsd_nd
->nd_cr
);
560 nfsd
->nfsd_nd
->nd_flag
|= ND_KERBFULL
;
564 if ((uap
->flag
& NFSSVC_AUTHINFAIL
) && (nfsd
= nsd
->nsd_nfsd
))
565 nfsd
->nfsd_flag
|= NFSD_AUTHFAIL
;
566 error
= nfssvc_nfsd(nsd
, uap
->argp
, p
);
568 #endif /* NFS_NOSERVER */
569 if (error
== EINTR
|| error
== ERESTART
)
576 * Adds a socket to the list for servicing by nfsds.
579 nfssvc_addsock(fp
, mynam
, p
)
584 register struct mbuf
*m
;
586 register struct nfssvc_sock
*slp
;
587 register struct socket
*so
;
588 struct nfssvc_sock
*tslp
;
591 so
= (struct socket
*)fp
->f_data
;
592 tslp
= (struct nfssvc_sock
*)0;
594 * Add it to the list, as required.
596 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
597 if (so
->so_proto
->pr_protocol
== IPPROTO_UDP
) {
599 if (tslp
->ns_flag
& SLP_VALID
) {
601 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
605 } else if (so
->so_proto
->pr_protocol
== ISOPROTO_CLTP
) {
607 if (tslp
->ns_flag
& SLP_VALID
) {
609 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
614 /* reserve buffer space for 2 maximally-sized packets */
616 if (so
->so_type
== SOCK_STREAM
)
617 siz
+= sizeof (u_long
);
619 if (siz
> NFS_MAXSOCKBUF
)
620 siz
= NFS_MAXSOCKBUF
;
621 error
= soreserve(so
, siz
, siz
);
624 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
629 * Set protocol specific options { for now TCP only } and
630 * reserve some space. For datagram sockets, this can get called
631 * repeatedly for the same socket, but that isn't harmful.
633 if (so
->so_type
== SOCK_STREAM
) {
637 bzero(&sopt
, sizeof sopt
);
638 sopt
.sopt_dir
= SOPT_SET
;
639 sopt
.sopt_level
= SOL_SOCKET
;
640 sopt
.sopt_name
= SO_KEEPALIVE
;
641 sopt
.sopt_val
= &val
;
642 sopt
.sopt_valsize
= sizeof val
;
646 if (so
->so_proto
->pr_domain
->dom_family
== AF_INET
&&
647 so
->so_proto
->pr_protocol
== IPPROTO_TCP
) {
651 bzero(&sopt
, sizeof sopt
);
652 sopt
.sopt_dir
= SOPT_SET
;
653 sopt
.sopt_level
= IPPROTO_TCP
;
654 sopt
.sopt_name
= TCP_NODELAY
;
655 sopt
.sopt_val
= &val
;
656 sopt
.sopt_valsize
= sizeof val
;
661 so
->so_rcv
.sb_flags
&= ~SB_NOINTR
;
662 so
->so_rcv
.sb_timeo
= 0;
663 so
->so_snd
.sb_flags
&= ~SB_NOINTR
;
664 so
->so_snd
.sb_timeo
= 0;
665 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
669 MALLOC(slp
, struct nfssvc_sock
*, sizeof(struct nfssvc_sock
),
671 bzero((caddr_t
)slp
, sizeof (struct nfssvc_sock
));
672 TAILQ_INIT(&slp
->ns_uidlruhead
);
673 TAILQ_INSERT_TAIL(&nfssvc_sockhead
, slp
, ns_chain
);
679 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
681 so
->so_upcallarg
= (caddr_t
)slp
;
682 so
->so_upcall
= nfsrv_rcv
;
683 so
->so_rcv
.sb_flags
|= SB_UPCALL
; /* required for freebsd merge */
684 slp
->ns_nflag
= SLPN_NEEDQ
;
685 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
686 slp
->ns_flag
= SLP_VALID
;
693 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
694 * until it is killed by a signal.
697 nfssvc_nfsd(nsd
, argp
, p
)
698 struct nfsd_srvargs
*nsd
;
702 register struct mbuf
*m
;
704 register struct nfssvc_sock
*slp
;
705 register struct socket
*so
;
706 struct nfsd
*nfsd
= nsd
->nsd_nfsd
;
707 struct nfsrv_descript
*nd
= NULL
;
709 int error
= 0, cacherep
, s
, sotype
, writes_todo
;
719 if (nfsd
== (struct nfsd
*)0) {
720 MALLOC(nfsd
, struct nfsd
*, sizeof(struct nfsd
), M_NFSD
, M_WAITOK
);
721 nsd
->nsd_nfsd
= nfsd
;
722 bzero((caddr_t
)nfsd
, sizeof (struct nfsd
));
723 nfsd
->nfsd_procp
= p
;
724 TAILQ_INSERT_TAIL(&nfsd_head
, nfsd
, nfsd_chain
);
728 * Loop getting rpc requests until SIGKILL.
731 if ((nfsd
->nfsd_flag
& NFSD_REQINPROG
) == 0) {
732 while (nfsd
->nfsd_slp
== (struct nfssvc_sock
*)0 &&
733 (nfsd_head_flag
& NFSD_CHECKSLP
) == 0) {
734 nfsd
->nfsd_flag
|= NFSD_WAITING
;
736 error
= tsleep((caddr_t
)nfsd
, PSOCK
| PCATCH
,
742 if (nfsd
->nfsd_slp
== (struct nfssvc_sock
*)0 &&
743 (nfsd_head_flag
& NFSD_CHECKSLP
) != 0) {
744 for (slp
= nfssvc_sockhead
.tqh_first
; slp
!= 0;
745 slp
= slp
->ns_chain
.tqe_next
) {
746 if ((slp
->ns_flag
& (SLP_VALID
| SLP_DOREC
))
747 == (SLP_VALID
| SLP_DOREC
)) {
748 slp
->ns_flag
&= ~SLP_DOREC
;
750 nfsd
->nfsd_slp
= slp
;
755 nfsd_head_flag
&= ~NFSD_CHECKSLP
;
757 if ((slp
= nfsd
->nfsd_slp
) == (struct nfssvc_sock
*)0)
759 if (slp
->ns_flag
& SLP_VALID
) {
761 if (slp
->ns_nflag
& SLPN_DISCONN
) {
763 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
764 } else if (slp
->ns_nflag
& SLPN_NEEDQ
) {
765 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
766 slp
->ns_nflag
&= ~SLPN_NEEDQ
;
767 nfsrv_rcv(slp
->ns_so
, (caddr_t
)slp
,
770 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
771 error
= nfsrv_dorec(slp
, nfsd
, &nd
);
772 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
775 cur_usec
= (u_quad_t
)now
.tv_sec
* 1000000 +
776 (u_quad_t
)now
.tv_usec
;
777 if (error
&& slp
->ns_tq
.lh_first
&&
778 slp
->ns_tq
.lh_first
->nd_time
<= cur_usec
) {
784 nfsd
->nfsd_flag
|= NFSD_REQINPROG
;
788 slp
= nfsd
->nfsd_slp
;
790 if (error
|| (slp
->ns_flag
& SLP_VALID
) == 0) {
793 m_freem(nd
->nd_nam2
);
794 FREE_ZONE((caddr_t
)nd
,
795 sizeof *nd
, M_NFSRVDESC
);
798 nfsd
->nfsd_slp
= (struct nfssvc_sock
*)0;
799 nfsd
->nfsd_flag
&= ~NFSD_REQINPROG
;
805 sotype
= so
->so_type
;
807 microuptime(&nd
->nd_starttime
);
809 nd
->nd_nam
= nd
->nd_nam2
;
811 nd
->nd_nam
= slp
->ns_nam
;
814 * Check to see if authorization is needed.
816 if (nfsd
->nfsd_flag
& NFSD_NEEDAUTH
) {
817 nfsd
->nfsd_flag
&= ~NFSD_NEEDAUTH
;
818 nsd
->nsd_haddr
= mtod(nd
->nd_nam
,
819 struct sockaddr_in
*)->sin_addr
.s_addr
;
820 nsd
->nsd_authlen
= nfsd
->nfsd_authlen
;
821 nsd
->nsd_verflen
= nfsd
->nfsd_verflen
;
822 if (!copyout(nfsd
->nfsd_authstr
,nsd
->nsd_authstr
,
823 nfsd
->nfsd_authlen
) &&
824 !copyout(nfsd
->nfsd_verfstr
, nsd
->nsd_verfstr
,
825 nfsd
->nfsd_verflen
) &&
826 !copyout((caddr_t
)nsd
, argp
, sizeof (*nsd
)))
828 cacherep
= RC_DROPIT
;
830 cacherep
= nfsrv_getcache(nd
, slp
, &mreq
);
833 * Check for just starting up for NQNFS and send
834 * fake "try again later" replies to the NQNFS clients.
837 if (notstarted
&& nqnfsstarttime
<= now
.tv_sec
) {
839 nqnfsstarttime
= now
.tv_sec
+ nqsrv_writeslack
;
845 if ((nd
->nd_flag
& ND_NQNFS
) == 0)
846 cacherep
= RC_DROPIT
;
847 else if (nd
->nd_procnum
!= NFSPROC_WRITE
) {
848 nd
->nd_procnum
= NFSPROC_NOOP
;
849 nd
->nd_repstat
= NQNFS_TRYLATER
;
853 } else if (nfsd
->nfsd_flag
& NFSD_AUTHFAIL
) {
854 nfsd
->nfsd_flag
&= ~NFSD_AUTHFAIL
;
855 nd
->nd_procnum
= NFSPROC_NOOP
;
856 nd
->nd_repstat
= (NFSERR_AUTHERR
| AUTH_TOOWEAK
);
858 } else if (nfs_privport
) {
859 /* Check if source port is privileged */
861 struct sockaddr
*nam
= mtod(nd
->nd_nam
, struct sockaddr
*);
862 struct sockaddr_in
*sin
;
864 sin
= (struct sockaddr_in
*)nam
;
865 port
= ntohs(sin
->sin_port
);
866 if (port
>= IPPORT_RESERVED
&&
867 nd
->nd_procnum
!= NFSPROC_NULL
) {
868 nd
->nd_procnum
= NFSPROC_NOOP
;
869 nd
->nd_repstat
= (NFSERR_AUTHERR
| AUTH_TOOWEAK
);
871 printf("NFS request from unprivileged port (%s:%d)\n",
872 (char *)(inet_ntoa(sin
->sin_addr
)), port
);
879 * Loop to get all the write rpc relies that have been
885 if (nd
&& (nd
->nd_flag
& ND_NFSV3
))
886 procrastinate
= nfsrvw_procrastinate_v3
;
888 procrastinate
= nfsrvw_procrastinate
;
889 if (writes_todo
|| (nd
->nd_procnum
== NFSPROC_WRITE
&&
890 procrastinate
> 0 && !notstarted
))
891 error
= nfsrv_writegather(&nd
, slp
,
892 nfsd
->nfsd_procp
, &mreq
);
894 error
= (*(nfsrv3_procs
[nd
->nd_procnum
]))(nd
,
895 slp
, nfsd
->nfsd_procp
, &mreq
);
899 if (nd
->nd_procnum
!= NQNFSPROC_VACATED
)
901 nfsrv_updatecache(nd
, FALSE
, mreq
);
903 m_freem(nd
->nd_nam2
);
908 nfsstats
.srvrpccnt
[nd
->nd_procnum
]++;
909 nfsrv_updatecache(nd
, TRUE
, mreq
);
910 nd
->nd_mrep
= (struct mbuf
*)0;
918 if (siz
<= 0 || siz
> NFS_MAXPACKET
) {
919 printf("mbuf siz=%d\n",siz
);
920 panic("Bad nfs svc reply");
923 m
->m_pkthdr
.len
= siz
;
924 m
->m_pkthdr
.rcvif
= (struct ifnet
*)0;
926 * For stream protocols, prepend a Sun RPC
929 if (sotype
== SOCK_STREAM
) {
930 M_PREPEND(m
, NFSX_UNSIGNED
, M_WAIT
);
931 *mtod(m
, u_long
*) = htonl(0x80000000 | siz
);
933 if (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
)
934 (void) nfs_slplock(slp
, 1);
935 if (slp
->ns_flag
& SLP_VALID
)
936 error
= nfs_send(so
, nd
->nd_nam2
, m
, NULL
);
943 nfsd_rt(sotype
, nd
, cacherep
);
945 MFREE(nd
->nd_nam2
, m
);
949 m_freem(nd
->nd_mrep
);
954 if (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
)
956 if (error
== EINTR
|| error
== ERESTART
) {
957 FREE_ZONE((caddr_t
)nd
,
958 sizeof *nd
, M_NFSRVDESC
);
966 nfsd_rt(sotype
, nd
, cacherep
);
967 m_freem(nd
->nd_mrep
);
968 m_freem(nd
->nd_nam2
);
969 nd
->nd_mrep
= nd
->nd_nam2
= NULL
;
974 m_freem(nd
->nd_mrep
);
976 m_freem(nd
->nd_nam2
);
977 FREE_ZONE((caddr_t
)nd
, sizeof *nd
, M_NFSRVDESC
);
982 * Check to see if there are outstanding writes that
983 * need to be serviced.
986 cur_usec
= (u_quad_t
)now
.tv_sec
* 1000000 +
987 (u_quad_t
)now
.tv_usec
;
989 if (slp
->ns_tq
.lh_first
&&
990 slp
->ns_tq
.lh_first
->nd_time
<= cur_usec
) {
996 } while (writes_todo
);
998 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
999 if (nfsrv_dorec(slp
, nfsd
, &nd
)) {
1000 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1001 nfsd
->nfsd_flag
&= ~NFSD_REQINPROG
;
1002 nfsd
->nfsd_slp
= NULL
;
1003 nfsrv_slpderef(slp
);
1005 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1008 TAILQ_REMOVE(&nfsd_head
, nfsd
, nfsd_chain
);
1010 _FREE((caddr_t
)nfsd
, M_NFSD
);
1011 nsd
->nsd_nfsd
= (struct nfsd
*)0;
1012 if (--nfs_numnfsd
== 0)
1013 nfsrv_init(TRUE
); /* Reinitialize everything */
1016 #endif /* NFS_NOSERVER */
1019 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
1021 SYSCTL_INT(_vfs_nfs
, OID_AUTO
, defect
, CTLFLAG_RW
, &nfs_defect
, 0, "");
1024 #ifndef _SYS_SYSPROTO_H_
1025 struct nfsclnt_args
{
1031 nfsclnt(struct proc
*p
, struct nfsclnt_args
*uap
)
1033 struct lockd_ans la
;
1036 if (uap
->flag
== NFSCLNT_LOCKDWAIT
) {
1037 return (nfslockdwait(p
));
1039 if (uap
->flag
== NFSCLNT_LOCKDANS
) {
1040 error
= copyin(uap
->argp
, &la
, sizeof(la
));
1041 return (error
!= 0 ? error
: nfslockdans(p
, &la
));
1043 if (uap
->flag
== NFSCLNT_LOCKDFD
)
1044 return (nfslockdfd(p
, (int)uap
->argp
));
1049 static int nfssvc_iod_continue(int);
1052 * Asynchronous I/O daemons for client nfs.
1053 * They do read-ahead and write-behind operations on the block I/O cache.
1054 * Never returns unless it fails or gets killed.
1060 register int i
, myiod
;
1061 struct nfsmount
*nmp
;
1066 * Assign my position or return error if too many already running
1069 for (i
= 0; i
< NFS_MAXASYNCDAEMON
; i
++)
1070 if (nfs_asyncdaemon
[i
] == 0) {
1071 nfs_asyncdaemon
[i
]++;
1079 /* stuff myiod into uthread to get off local stack for continuation */
1081 ut
= (struct uthread
*)get_bsdthread_info(current_act());
1082 ut
->uu_state
.uu_nfs_myiod
= myiod
; /* squirrel away for continuation */
1084 nfssvc_iod_continue(0);
1090 * Continuation for Asynchronous I/O daemons for client nfs.
1093 nfssvc_iod_continue(error
)
1095 register struct nfsbuf
*bp
;
1096 register int i
, myiod
;
1097 struct nfsmount
*nmp
;
1102 * real myiod is stored in uthread, recover it
1104 ut
= (struct uthread
*)get_bsdthread_info(current_act());
1105 myiod
= ut
->uu_state
.uu_nfs_myiod
;
1109 * Just loop around doin our stuff until SIGKILL
1110 * - actually we don't loop with continuations...
1113 while (((nmp
= nfs_iodmount
[myiod
]) == NULL
1114 || nmp
->nm_bufq
.tqh_first
== NULL
)
1115 && error
== 0 && nfs_ioddelwri
== 0) {
1118 nfs_iodwant
[myiod
] = p
;
1119 nfs_iodmount
[myiod
] = NULL
;
1120 error
= tsleep0((caddr_t
)&nfs_iodwant
[myiod
],
1121 PWAIT
| PCATCH
, "nfsidl", 0, nfssvc_iod_continue
);
1125 nfs_asyncdaemon
[myiod
] = 0;
1126 if (nmp
) nmp
->nm_bufqiods
--;
1127 nfs_iodwant
[myiod
] = NULL
;
1128 nfs_iodmount
[myiod
] = NULL
;
1130 if (error
== EINTR
|| error
== ERESTART
)
1132 unix_syscall_return(error
);
1135 while ((bp
= nmp
->nm_bufq
.tqh_first
) != NULL
) {
1136 /* Take one off the front of the list */
1137 TAILQ_REMOVE(&nmp
->nm_bufq
, bp
, nb_free
);
1138 bp
->nb_free
.tqe_next
= NFSNOLIST
;
1140 if (nmp
->nm_bufqwant
&& nmp
->nm_bufqlen
< 2 * nfs_numasync
) {
1141 nmp
->nm_bufqwant
= FALSE
;
1142 wakeup(&nmp
->nm_bufq
);
1144 if (ISSET(bp
->nb_flags
, NB_READ
))
1145 (void) nfs_doio(bp
, bp
->nb_rcred
, (struct proc
*)0);
1147 (void) nfs_doio(bp
, bp
->nb_wcred
, (struct proc
*)0);
1150 * If there are more than one iod on this mount, then defect
1151 * so that the iods can be shared out fairly between the mounts
1153 if (nfs_defect
&& nmp
->nm_bufqiods
> 1) {
1155 ("nfssvc_iod: iod %d defecting from mount %p\n",
1157 nfs_iodmount
[myiod
] = NULL
;
1163 if (nfs_ioddelwri
) {
1166 while (i
< 8 && (bp
= TAILQ_FIRST(&nfsbufdelwri
)) != NULL
) {
1167 struct nfsnode
*np
= VTONFS(bp
->nb_vp
);
1168 nfs_buf_remfree(bp
);
1169 if (ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
)) {
1170 /* put buffer at end of delwri list */
1171 TAILQ_INSERT_TAIL(&nfsbufdelwri
, bp
, nb_free
);
1173 nfs_flushcommits(np
->n_vnode
, (struct proc
*)0);
1175 SET(bp
->nb_flags
, (NB_BUSY
| NB_ASYNC
| NB_IOD
));
1185 * Shut down a socket associated with an nfssvc_sock structure.
1186 * Should be called with the send lock set, if required.
1187 * The trick here is to increment the sref at the start, so that the nfsds
1188 * will stop using it and clear ns_flag at the end so that it will not be
1189 * reassigned during cleanup.
1193 register struct nfssvc_sock
*slp
;
1195 register struct nfsuid
*nuidp
, *nnuidp
;
1196 register struct nfsrv_descript
*nwp
, *nnwp
;
1202 slp
->ns_flag
&= ~SLP_ALLFLAGS
;
1203 slp
->ns_nflag
&= ~SLP_ALLFLAGS
;
1206 slp
->ns_fp
= (struct file
*)0;
1208 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1209 so
->so_upcall
= NULL
;
1210 so
->so_rcv
.sb_flags
&= ~SB_UPCALL
;
1213 MFREE(slp
->ns_nam
, m
);
1214 m_freem(slp
->ns_raw
);
1215 m_freem(slp
->ns_rec
);
1216 slp
->ns_nam
= slp
->ns_raw
= slp
->ns_rec
= NULL
;
1217 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1218 closef(fp
, (struct proc
*)0);
1219 for (nuidp
= slp
->ns_uidlruhead
.tqh_first
; nuidp
!= 0;
1221 nnuidp
= nuidp
->nu_lru
.tqe_next
;
1222 LIST_REMOVE(nuidp
, nu_hash
);
1223 TAILQ_REMOVE(&slp
->ns_uidlruhead
, nuidp
, nu_lru
);
1224 if (nuidp
->nu_flag
& NU_NAM
)
1225 m_freem(nuidp
->nu_nam
);
1226 FREE_ZONE((caddr_t
)nuidp
,
1227 sizeof (struct nfsuid
), M_NFSUID
);
1230 for (nwp
= slp
->ns_tq
.lh_first
; nwp
; nwp
= nnwp
) {
1231 nnwp
= nwp
->nd_tq
.le_next
;
1232 LIST_REMOVE(nwp
, nd_tq
);
1233 FREE_ZONE((caddr_t
)nwp
, sizeof *nwp
, M_NFSRVDESC
);
1235 LIST_INIT(&slp
->ns_tq
);
1241 * Get an authorization string for the uid by having the mount_nfs sitting
1242 * on this mount point porpous out of the kernel and do it.
1245 nfs_getauth(nmp
, rep
, cred
, auth_str
, auth_len
, verf_str
, verf_len
, key
)
1246 register struct nfsmount
*nmp
;
1253 NFSKERBKEY_T key
; /* return session key */
1257 while ((nmp
->nm_state
& NFSSTA_WAITAUTH
) == 0) {
1258 nmp
->nm_state
|= NFSSTA_WANTAUTH
;
1259 (void) tsleep((caddr_t
)&nmp
->nm_authtype
, PSOCK
,
1260 "nfsauth1", 2 * hz
);
1261 error
= nfs_sigintr(nmp
, rep
, rep
->r_procp
);
1263 nmp
->nm_state
&= ~NFSSTA_WANTAUTH
;
1267 nmp
->nm_state
&= ~(NFSSTA_WAITAUTH
| NFSSTA_WANTAUTH
);
1268 MALLOC(*auth_str
, char *, RPCAUTH_MAXSIZ
, M_TEMP
, M_WAITOK
);
1269 nmp
->nm_authstr
= *auth_str
;
1270 nmp
->nm_authlen
= RPCAUTH_MAXSIZ
;
1271 nmp
->nm_verfstr
= verf_str
;
1272 nmp
->nm_verflen
= *verf_len
;
1273 nmp
->nm_authuid
= cred
->cr_uid
;
1274 wakeup((caddr_t
)&nmp
->nm_authstr
);
1277 * And wait for mount_nfs to do its stuff.
1279 while ((nmp
->nm_state
& NFSSTA_HASAUTH
) == 0 && error
== 0) {
1280 (void) tsleep((caddr_t
)&nmp
->nm_authlen
, PSOCK
,
1281 "nfsauth2", 2 * hz
);
1282 error
= nfs_sigintr(nmp
, rep
, rep
->r_procp
);
1284 if (nmp
->nm_state
& NFSSTA_AUTHERR
) {
1285 nmp
->nm_state
&= ~NFSSTA_AUTHERR
;
1289 _FREE((caddr_t
)*auth_str
, M_TEMP
);
1291 *auth_len
= nmp
->nm_authlen
;
1292 *verf_len
= nmp
->nm_verflen
;
1293 bcopy((caddr_t
)nmp
->nm_key
, (caddr_t
)key
, sizeof (key
));
1295 nmp
->nm_state
&= ~NFSSTA_HASAUTH
;
1296 nmp
->nm_state
|= NFSSTA_WAITAUTH
;
1297 if (nmp
->nm_state
& NFSSTA_WANTAUTH
) {
1298 nmp
->nm_state
&= ~NFSSTA_WANTAUTH
;
1299 wakeup((caddr_t
)&nmp
->nm_authtype
);
1305 * Get a nickname authenticator and verifier.
1308 nfs_getnickauth(nmp
, cred
, auth_str
, auth_len
, verf_str
, verf_len
)
1309 struct nfsmount
*nmp
;
1316 register struct nfsuid
*nuidp
;
1317 register u_long
*nickp
, *verfp
;
1318 struct timeval ktvin
, ktvout
, now
;
1321 if (verf_len
< (4 * NFSX_UNSIGNED
))
1322 panic("nfs_getnickauth verf too small");
1324 for (nuidp
= NMUIDHASH(nmp
, cred
->cr_uid
)->lh_first
;
1325 nuidp
!= 0; nuidp
= nuidp
->nu_hash
.le_next
) {
1326 if (nuidp
->nu_cr
.cr_uid
== cred
->cr_uid
)
1330 if (!nuidp
|| nuidp
->nu_expire
< now
.tv_sec
)
1334 * Move to the end of the lru list (end of lru == most recently used).
1336 TAILQ_REMOVE(&nmp
->nm_uidlruhead
, nuidp
, nu_lru
);
1337 TAILQ_INSERT_TAIL(&nmp
->nm_uidlruhead
, nuidp
, nu_lru
);
1339 MALLOC(nickp
, u_long
*, 2 * NFSX_UNSIGNED
, M_TEMP
, M_WAITOK
);
1340 *nickp
++ = txdr_unsigned(RPCAKN_NICKNAME
);
1341 *nickp
= txdr_unsigned(nuidp
->nu_nickname
);
1342 *auth_str
= (char *)nickp
;
1343 *auth_len
= 2 * NFSX_UNSIGNED
;
1346 * Now we must encrypt the verifier and package it up.
1348 verfp
= (u_long
*)verf_str
;
1349 *verfp
++ = txdr_unsigned(RPCAKN_NICKNAME
);
1351 if (now
.tv_sec
> nuidp
->nu_timestamp
.tv_sec
||
1352 (now
.tv_sec
== nuidp
->nu_timestamp
.tv_sec
&&
1353 now
.tv_usec
> nuidp
->nu_timestamp
.tv_usec
))
1354 nuidp
->nu_timestamp
= now
;
1356 nuidp
->nu_timestamp
.tv_usec
++;
1357 ktvin
.tv_sec
= txdr_unsigned(nuidp
->nu_timestamp
.tv_sec
);
1358 ktvin
.tv_usec
= txdr_unsigned(nuidp
->nu_timestamp
.tv_usec
);
1361 * Now encrypt the timestamp verifier in ecb mode using the session
1368 *verfp
++ = ktvout
.tv_sec
;
1369 *verfp
++ = ktvout
.tv_usec
;
1375 * Save the current nickname in a hash list entry on the mount point.
1378 nfs_savenickauth(nmp
, cred
, len
, key
, mdp
, dposp
, mrep
)
1379 register struct nfsmount
*nmp
;
1387 register struct nfsuid
*nuidp
;
1388 register u_long
*tl
;
1390 struct mbuf
*md
= *mdp
;
1391 struct timeval ktvin
, ktvout
, now
;
1393 char *dpos
= *dposp
, *cp2
;
1394 int deltasec
, error
= 0;
1396 if (len
== (3 * NFSX_UNSIGNED
)) {
1397 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
1398 ktvin
.tv_sec
= *tl
++;
1399 ktvin
.tv_usec
= *tl
++;
1400 nick
= fxdr_unsigned(u_long
, *tl
);
1403 * Decrypt the timestamp in ecb mode.
1408 ktvout
.tv_sec
= fxdr_unsigned(long, ktvout
.tv_sec
);
1409 ktvout
.tv_usec
= fxdr_unsigned(long, ktvout
.tv_usec
);
1411 deltasec
= now
.tv_sec
- ktvout
.tv_sec
;
1413 deltasec
= -deltasec
;
1415 * If ok, add it to the hash list for the mount point.
1417 if (deltasec
<= NFS_KERBCLOCKSKEW
) {
1418 if (nmp
->nm_numuids
< nuidhash_max
) {
1420 MALLOC_ZONE(nuidp
, struct nfsuid
*,
1421 sizeof (struct nfsuid
),
1422 M_NFSUID
, M_WAITOK
);
1424 nuidp
= nmp
->nm_uidlruhead
.tqh_first
;
1425 LIST_REMOVE(nuidp
, nu_hash
);
1426 TAILQ_REMOVE(&nmp
->nm_uidlruhead
, nuidp
,
1430 nuidp
->nu_cr
.cr_uid
= cred
->cr_uid
;
1431 nuidp
->nu_expire
= now
.tv_sec
+ NFS_KERBTTL
;
1432 nuidp
->nu_timestamp
= ktvout
;
1433 nuidp
->nu_nickname
= nick
;
1434 bcopy(key
, nuidp
->nu_key
, sizeof (key
));
1435 TAILQ_INSERT_TAIL(&nmp
->nm_uidlruhead
, nuidp
,
1437 LIST_INSERT_HEAD(NMUIDHASH(nmp
, cred
->cr_uid
),
1441 nfsm_adv(nfsm_rndup(len
));
1448 #ifndef NFS_NOSERVER
1451 * Derefence a server socket structure. If it has no more references and
1452 * is no longer valid, you can throw it away.
1456 register struct nfssvc_sock
*slp
;
1458 if (--(slp
->ns_sref
) == 0 && (slp
->ns_flag
& SLP_VALID
) == 0) {
1459 TAILQ_REMOVE(&nfssvc_sockhead
, slp
, ns_chain
);
1460 _FREE((caddr_t
)slp
, M_NFSSVC
);
1465 * Lock a socket against others.
1468 nfs_slplock(slp
, wait
)
1469 register struct nfssvc_sock
*slp
;
1472 int *statep
= &slp
->ns_solock
;
1474 if (!wait
&& (*statep
& NFSSTA_SNDLOCK
))
1475 return(0); /* already locked, fail */
1476 while (*statep
& NFSSTA_SNDLOCK
) {
1477 *statep
|= NFSSTA_WANTSND
;
1478 (void) tsleep((caddr_t
)statep
, PZERO
- 1, "nfsslplck", 0);
1480 *statep
|= NFSSTA_SNDLOCK
;
1485 * Unlock the stream socket for others.
1489 struct nfssvc_sock
*slp
;
1491 int *statep
= &slp
->ns_solock
;
1493 if ((*statep
& NFSSTA_SNDLOCK
) == 0)
1494 panic("nfs slpunlock");
1495 *statep
&= ~NFSSTA_SNDLOCK
;
1496 if (*statep
& NFSSTA_WANTSND
) {
1497 *statep
&= ~NFSSTA_WANTSND
;
1498 wakeup((caddr_t
)statep
);
1503 * Initialize the data structures for the server.
1504 * Handshake with any new nfsds starting up to avoid any chance of
1508 nfsrv_init(terminating
)
1511 register struct nfssvc_sock
*slp
, *nslp
;
1513 if (nfssvc_sockhead_flag
& SLP_INIT
)
1515 nfssvc_sockhead_flag
|= SLP_INIT
;
1517 for (slp
= nfssvc_sockhead
.tqh_first
; slp
!= 0; slp
= nslp
) {
1518 nslp
= slp
->ns_chain
.tqe_next
;
1519 if (slp
->ns_flag
& SLP_VALID
)
1521 TAILQ_REMOVE(&nfssvc_sockhead
, slp
, ns_chain
);
1522 _FREE((caddr_t
)slp
, M_NFSSVC
);
1524 nfsrv_cleancache(); /* And clear out server cache */
1525 /* XXX CSM 12/4/97 Revisit when enabling WebNFS */
1528 nfs_pub
.np_valid
= 0;
1533 TAILQ_INIT(&nfssvc_sockhead
);
1534 nfssvc_sockhead_flag
&= ~SLP_INIT
;
1535 if (nfssvc_sockhead_flag
& SLP_WANTINIT
) {
1536 nfssvc_sockhead_flag
&= ~SLP_WANTINIT
;
1537 wakeup((caddr_t
)&nfssvc_sockhead
);
1540 TAILQ_INIT(&nfsd_head
);
1541 nfsd_head_flag
&= ~NFSD_CHECKSLP
;
1543 MALLOC(nfs_udpsock
, struct nfssvc_sock
*, sizeof(struct nfssvc_sock
),
1544 M_NFSSVC
, M_WAITOK
);
1545 bzero((caddr_t
)nfs_udpsock
, sizeof (struct nfssvc_sock
));
1546 TAILQ_INIT(&nfs_udpsock
->ns_uidlruhead
);
1547 TAILQ_INSERT_HEAD(&nfssvc_sockhead
, nfs_udpsock
, ns_chain
);
1549 MALLOC(nfs_cltpsock
, struct nfssvc_sock
*, sizeof(struct nfssvc_sock
),
1550 M_NFSSVC
, M_WAITOK
);
1551 bzero((caddr_t
)nfs_cltpsock
, sizeof (struct nfssvc_sock
));
1552 TAILQ_INIT(&nfs_cltpsock
->ns_uidlruhead
);
1553 TAILQ_INSERT_TAIL(&nfssvc_sockhead
, nfs_cltpsock
, ns_chain
);
1557 * Add entries to the server monitor log.
1560 nfsd_rt(sotype
, nd
, cacherep
)
1562 register struct nfsrv_descript
*nd
;
1565 register struct drt
*rt
;
1568 rt
= &nfsdrt
.drt
[nfsdrt
.pos
];
1569 if (cacherep
== RC_DOIT
)
1571 else if (cacherep
== RC_REPLY
)
1572 rt
->flag
= DRT_CACHEREPLY
;
1574 rt
->flag
= DRT_CACHEDROP
;
1575 if (sotype
== SOCK_STREAM
)
1576 rt
->flag
|= DRT_TCP
;
1577 if (nd
->nd_flag
& ND_NQNFS
)
1578 rt
->flag
|= DRT_NQNFS
;
1579 else if (nd
->nd_flag
& ND_NFSV3
)
1580 rt
->flag
|= DRT_NFSV3
;
1581 rt
->proc
= nd
->nd_procnum
;
1582 if (mtod(nd
->nd_nam
, struct sockaddr
*)->sa_family
== AF_INET
)
1583 rt
->ipadr
= mtod(nd
->nd_nam
, struct sockaddr_in
*)->sin_addr
.s_addr
;
1585 rt
->ipadr
= INADDR_ANY
;
1587 rt
->resptime
= ((now
.tv_sec
- nd
->nd_starttime
.tv_sec
) * 1000000) +
1588 (now
.tv_usec
- nd
->nd_starttime
.tv_usec
);
1589 microtime(&rt
->tstamp
); // XXX unused
1590 nfsdrt
.pos
= (nfsdrt
.pos
+ 1) % NFSRTTLOGSIZ
;
1592 #endif /* NFS_NOSERVER */