2 * Copyright (c) 2000 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>
77 #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/syslog.h>
87 #include <machine/spl.h>
89 #include <netinet/in.h>
90 #include <netinet/tcp.h>
92 #include <netiso/iso.h>
94 #include <nfs/xdr_subs.h>
95 #include <nfs/rpcv2.h>
96 #include <nfs/nfsproto.h>
98 #include <nfs/nfsm_subs.h>
99 #include <nfs/nfsrvcache.h>
100 #include <nfs/nfsmount.h>
101 #include <nfs/nfsnode.h>
102 #include <nfs/nqnfs.h>
103 #include <nfs/nfsrtt.h>
107 extern int (*nfsrv3_procs
[NFS_NPROCS
]) __P((struct nfsrv_descript
*nd
,
108 struct nfssvc_sock
*slp
,
110 struct mbuf
**mreqp
));
111 extern int nfs_numasync
;
112 extern time_t nqnfsstarttime
;
113 extern int nqsrv_writeslack
;
115 extern struct nfsstats nfsstats
;
116 extern int nfsrvw_procrastinate
;
117 extern int nfsrvw_procrastinate_v3
;
118 struct nfssvc_sock
*nfs_udpsock
, *nfs_cltpsock
;
119 static int nuidhash_max
= NFS_MAXUIDHASH
;
121 static void nfsrv_zapsock
__P((struct nfssvc_sock
*slp
));
122 static int nfssvc_iod
__P((struct proc
*));
127 static int nfs_asyncdaemon
[NFS_MAXASYNCDAEMON
];
130 int nfsd_waiting
= 0;
131 static struct nfsdrt nfsdrt
;
132 static int nfs_numnfsd
= 0;
133 static int notstarted
= 1;
134 static int modify_flag
= 0;
135 static void nfsd_rt
__P((int sotype
, struct nfsrv_descript
*nd
,
137 static int nfssvc_addsock
__P((struct file
*, struct mbuf
*,
139 static int nfssvc_nfsd
__P((struct nfsd_srvargs
*,caddr_t
,struct proc
*));
141 static int nfs_privport
= 0;
142 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
144 SYSCTL_INT(_vfs_nfs
, NFS_NFSPRIVPORT
, nfs_privport
, CTLFLAG_RW
, &nfs_privport
, 0, "");
145 SYSCTL_INT(_vfs_nfs
, OID_AUTO
, gatherdelay
, CTLFLAG_RW
, &nfsrvw_procrastinate
, 0, "");
146 SYSCTL_INT(_vfs_nfs
, OID_AUTO
, gatherdelay_v3
, CTLFLAG_RW
, &nfsrvw_procrastinate_v3
, 0, "");
150 * NFS server system calls
151 * getfh() lives here too, but maybe should move to kern/vfs_syscalls.c
155 * Get file handle system call
157 #ifndef _SYS_SYSPROTO_H_
166 register struct getfh_args
*uap
;
168 register struct vnode
*vp
;
176 error
= suser(p
->p_ucred
, &p
->p_acflag
);
179 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_USERSPACE
, uap
->fname
, p
);
184 bzero((caddr_t
)&fh
, sizeof(fh
));
185 fh
.fh_fsid
= vp
->v_mount
->mnt_stat
.f_fsid
;
186 error
= VFS_VPTOFH(vp
, &fh
.fh_fid
);
190 error
= copyout((caddr_t
)&fh
, (caddr_t
)uap
->fhp
, sizeof (fh
));
194 #endif /* NFS_NOSERVER */
196 * Nfs server psuedo system call for the nfsd's
197 * Based on the flag value it either:
198 * - adds a socket to the selection list
199 * - remains in the kernel as an nfsd
200 * - remains in the kernel as an nfsiod
202 #ifndef _SYS_SYSPROTO_H_
211 register struct nfssvc_args
*uap
;
217 struct nfsd_args nfsdarg
;
218 struct nfsd_srvargs nfsd_srvargs
, *nsd
= &nfsd_srvargs
;
219 struct nfsd_cargs ncd
;
221 struct nfssvc_sock
*slp
;
222 struct nfsuid
*nuidp
;
223 struct nfsmount
*nmp
;
224 #endif /* NFS_NOSERVER */
230 error
= suser(p
->p_ucred
, &p
->p_acflag
);
233 while (nfssvc_sockhead_flag
& SLP_INIT
) {
234 nfssvc_sockhead_flag
|= SLP_WANTINIT
;
235 (void) tsleep((caddr_t
)&nfssvc_sockhead
, PSOCK
, "nfsd init", 0);
237 if (uap
->flag
& NFSSVC_BIOD
)
238 error
= nfssvc_iod(p
);
242 #else /* !NFS_NOSERVER */
243 else if (uap
->flag
& NFSSVC_MNTD
) {
244 error
= copyin(uap
->argp
, (caddr_t
)&ncd
, sizeof (ncd
));
247 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_USERSPACE
,
252 if ((nd
.ni_vp
->v_flag
& VROOT
) == 0)
254 nmp
= VFSTONFS(nd
.ni_vp
->v_mount
);
259 /* disable split funnels now */
260 thread_funnel_merge(kernel_flock
, network_flock
);
262 if ((nmp
->nm_flag
& NFSMNT_MNTD
) &&
263 (uap
->flag
& NFSSVC_GOTAUTH
) == 0)
265 nmp
->nm_flag
|= NFSMNT_MNTD
;
266 error
= nqnfs_clientd(nmp
, p
->p_ucred
, &ncd
, uap
->flag
,
268 } else if (uap
->flag
& NFSSVC_ADDSOCK
) {
269 error
= copyin(uap
->argp
, (caddr_t
)&nfsdarg
, sizeof(nfsdarg
));
272 error
= getsock(p
->p_fd
, nfsdarg
.sock
, &fp
);
276 * Get the client address for connected sockets.
278 if (nfsdarg
.name
== NULL
|| nfsdarg
.namelen
== 0)
279 nam
= (struct mbuf
*)0;
281 error
= sockargs(&nam
, nfsdarg
.name
, nfsdarg
.namelen
,
286 error
= nfssvc_addsock(fp
, nam
, p
);
288 error
= copyin(uap
->argp
, (caddr_t
)nsd
, sizeof (*nsd
));
292 /* disable split funnels now */
293 thread_funnel_merge(kernel_flock
, network_flock
);
295 if ((uap
->flag
& NFSSVC_AUTHIN
) && ((nfsd
= nsd
->nsd_nfsd
)) &&
296 (nfsd
->nfsd_slp
->ns_flag
& SLP_VALID
)) {
297 slp
= nfsd
->nfsd_slp
;
300 * First check to see if another nfsd has already
301 * added this credential.
303 for (nuidp
= NUIDHASH(slp
,nsd
->nsd_cr
.cr_uid
)->lh_first
;
304 nuidp
!= 0; nuidp
= nuidp
->nu_hash
.le_next
) {
305 if (nuidp
->nu_cr
.cr_uid
== nsd
->nsd_cr
.cr_uid
&&
306 (!nfsd
->nfsd_nd
->nd_nam2
||
307 netaddr_match(NU_NETFAM(nuidp
),
308 &nuidp
->nu_haddr
, nfsd
->nfsd_nd
->nd_nam2
)))
312 nfsrv_setcred(&nuidp
->nu_cr
,&nfsd
->nfsd_nd
->nd_cr
);
313 nfsd
->nfsd_nd
->nd_flag
|= ND_KERBFULL
;
318 if (slp
->ns_numuids
< nuidhash_max
) {
320 nuidp
= (struct nfsuid
*)
321 _MALLOC_ZONE(sizeof (struct nfsuid
),
324 nuidp
= (struct nfsuid
*)0;
325 if ((slp
->ns_flag
& SLP_VALID
) == 0) {
327 _FREE_ZONE((caddr_t
)nuidp
,
328 sizeof (struct nfsuid
), M_NFSUID
);
330 if (nuidp
== (struct nfsuid
*)0) {
331 nuidp
= slp
->ns_uidlruhead
.tqh_first
;
332 LIST_REMOVE(nuidp
, nu_hash
);
333 TAILQ_REMOVE(&slp
->ns_uidlruhead
, nuidp
,
335 if (nuidp
->nu_flag
& NU_NAM
)
336 m_freem(nuidp
->nu_nam
);
339 nuidp
->nu_cr
= nsd
->nsd_cr
;
340 if (nuidp
->nu_cr
.cr_ngroups
> NGROUPS
)
341 nuidp
->nu_cr
.cr_ngroups
= NGROUPS
;
342 nuidp
->nu_cr
.cr_ref
= 1;
343 nuidp
->nu_timestamp
= nsd
->nsd_timestamp
;
344 nuidp
->nu_expire
= time
.tv_sec
+ nsd
->nsd_ttl
;
346 * and save the session key in nu_key.
348 bcopy(nsd
->nsd_key
, nuidp
->nu_key
,
349 sizeof (nsd
->nsd_key
));
350 if (nfsd
->nfsd_nd
->nd_nam2
) {
351 struct sockaddr_in
*saddr
;
353 saddr
= mtod(nfsd
->nfsd_nd
->nd_nam2
,
354 struct sockaddr_in
*);
355 switch (saddr
->sin_family
) {
357 nuidp
->nu_flag
|= NU_INETADDR
;
359 saddr
->sin_addr
.s_addr
;
363 nuidp
->nu_flag
|= NU_NAM
;
364 nuidp
->nu_nam
= m_copym(
365 nfsd
->nfsd_nd
->nd_nam2
, 0,
370 TAILQ_INSERT_TAIL(&slp
->ns_uidlruhead
, nuidp
,
372 LIST_INSERT_HEAD(NUIDHASH(slp
, nsd
->nsd_uid
),
374 nfsrv_setcred(&nuidp
->nu_cr
,
375 &nfsd
->nfsd_nd
->nd_cr
);
376 nfsd
->nfsd_nd
->nd_flag
|= ND_KERBFULL
;
380 if ((uap
->flag
& NFSSVC_AUTHINFAIL
) && (nfsd
= nsd
->nsd_nfsd
))
381 nfsd
->nfsd_flag
|= NFSD_AUTHFAIL
;
382 error
= nfssvc_nfsd(nsd
, uap
->argp
, p
);
384 #endif /* NFS_NOSERVER */
385 if (error
== EINTR
|| error
== ERESTART
)
392 * Adds a socket to the list for servicing by nfsds.
395 nfssvc_addsock(fp
, mynam
, p
)
400 register struct mbuf
*m
;
402 register struct nfssvc_sock
*slp
;
403 register struct socket
*so
;
404 struct nfssvc_sock
*tslp
;
407 so
= (struct socket
*)fp
->f_data
;
408 tslp
= (struct nfssvc_sock
*)0;
410 * Add it to the list, as required.
412 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
413 if (so
->so_proto
->pr_protocol
== IPPROTO_UDP
) {
415 if (tslp
->ns_flag
& SLP_VALID
) {
417 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
421 } else if (so
->so_proto
->pr_protocol
== ISOPROTO_CLTP
) {
423 if (tslp
->ns_flag
& SLP_VALID
) {
425 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
430 if (so
->so_type
== SOCK_STREAM
)
431 siz
= NFS_MAXPACKET
+ sizeof (u_long
);
434 error
= soreserve(so
, siz
, siz
);
437 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
442 * Set protocol specific options { for now TCP only } and
443 * reserve some space. For datagram sockets, this can get called
444 * repeatedly for the same socket, but that isn't harmful.
446 if (so
->so_type
== SOCK_STREAM
) {
450 bzero(&sopt
, sizeof sopt
);
451 sopt
.sopt_level
= SOL_SOCKET
;
452 sopt
.sopt_name
= SO_KEEPALIVE
;
453 sopt
.sopt_val
= &val
;
454 sopt
.sopt_valsize
= sizeof val
;
458 if (so
->so_proto
->pr_domain
->dom_family
== AF_INET
&&
459 so
->so_proto
->pr_protocol
== IPPROTO_TCP
) {
463 bzero(&sopt
, sizeof sopt
);
464 sopt
.sopt_level
= IPPROTO_TCP
;
465 sopt
.sopt_name
= TCP_NODELAY
;
466 sopt
.sopt_val
= &val
;
467 sopt
.sopt_valsize
= sizeof val
;
472 so
->so_rcv
.sb_flags
&= ~SB_NOINTR
;
473 so
->so_rcv
.sb_timeo
= 0;
474 so
->so_snd
.sb_flags
&= ~SB_NOINTR
;
475 so
->so_snd
.sb_timeo
= 0;
476 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
480 MALLOC(slp
, struct nfssvc_sock
*, sizeof(struct nfssvc_sock
),
482 bzero((caddr_t
)slp
, sizeof (struct nfssvc_sock
));
483 TAILQ_INIT(&slp
->ns_uidlruhead
);
484 TAILQ_INSERT_TAIL(&nfssvc_sockhead
, slp
, ns_chain
);
490 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
492 so
->so_upcallarg
= (caddr_t
)slp
;
493 so
->so_upcall
= nfsrv_rcv
;
494 so
->so_rcv
.sb_flags
|= SB_UPCALL
; /* required for freebsd merge */
495 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
496 slp
->ns_flag
= (SLP_VALID
| SLP_NEEDQ
);
503 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
504 * until it is killed by a signal.
507 nfssvc_nfsd(nsd
, argp
, p
)
508 struct nfsd_srvargs
*nsd
;
512 register struct mbuf
*m
;
514 register struct nfssvc_sock
*slp
;
515 register struct socket
*so
;
516 register int *solockp
;
517 struct nfsd
*nfsd
= nsd
->nsd_nfsd
;
518 struct nfsrv_descript
*nd
= NULL
;
520 int error
= 0, cacherep
, s
, sotype
, writes_todo
;
523 extern void nfs_aio_thread_init();
530 if (nfsd
== (struct nfsd
*)0) {
531 MALLOC(nfsd
, struct nfsd
*, sizeof(struct nfsd
), M_NFSD
, M_WAITOK
);
532 nsd
->nsd_nfsd
= nfsd
;
533 bzero((caddr_t
)nfsd
, sizeof (struct nfsd
));
534 nfsd
->nfsd_procp
= p
;
535 TAILQ_INSERT_TAIL(&nfsd_head
, nfsd
, nfsd_chain
);
537 nfs_aio_thread_init();
540 * Loop getting rpc requests until SIGKILL.
543 if ((nfsd
->nfsd_flag
& NFSD_REQINPROG
) == 0) {
544 while (nfsd
->nfsd_slp
== (struct nfssvc_sock
*)0 &&
545 (nfsd_head_flag
& NFSD_CHECKSLP
) == 0) {
546 nfsd
->nfsd_flag
|= NFSD_WAITING
;
548 error
= tsleep((caddr_t
)nfsd
, PSOCK
| PCATCH
,
554 if (nfsd
->nfsd_slp
== (struct nfssvc_sock
*)0 &&
555 (nfsd_head_flag
& NFSD_CHECKSLP
) != 0) {
556 for (slp
= nfssvc_sockhead
.tqh_first
; slp
!= 0;
557 slp
= slp
->ns_chain
.tqe_next
) {
558 if ((slp
->ns_flag
& (SLP_VALID
| SLP_DOREC
))
559 == (SLP_VALID
| SLP_DOREC
)) {
560 slp
->ns_flag
&= ~SLP_DOREC
;
562 nfsd
->nfsd_slp
= slp
;
567 nfsd_head_flag
&= ~NFSD_CHECKSLP
;
569 if ((slp
= nfsd
->nfsd_slp
) == (struct nfssvc_sock
*)0)
571 if (slp
->ns_flag
& SLP_VALID
) {
572 if (slp
->ns_flag
& SLP_DISCONN
)
574 else if (slp
->ns_flag
& SLP_NEEDQ
) {
575 slp
->ns_flag
&= ~SLP_NEEDQ
;
576 (void) nfs_sndlock(&slp
->ns_solock
,
578 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
579 nfsrv_rcv(slp
->ns_so
, (caddr_t
)slp
,
581 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
582 nfs_sndunlock(&slp
->ns_solock
);
584 error
= nfsrv_dorec(slp
, nfsd
, &nd
);
585 cur_usec
= (u_quad_t
)time
.tv_sec
* 1000000 +
586 (u_quad_t
)time
.tv_usec
;
587 if (error
&& slp
->ns_tq
.lh_first
&&
588 slp
->ns_tq
.lh_first
->nd_time
<= cur_usec
) {
594 nfsd
->nfsd_flag
|= NFSD_REQINPROG
;
598 slp
= nfsd
->nfsd_slp
;
600 if (error
|| (slp
->ns_flag
& SLP_VALID
) == 0) {
602 _FREE_ZONE((caddr_t
)nd
,
603 sizeof *nd
, M_NFSRVDESC
);
606 nfsd
->nfsd_slp
= (struct nfssvc_sock
*)0;
607 nfsd
->nfsd_flag
&= ~NFSD_REQINPROG
;
613 sotype
= so
->so_type
;
614 if (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
)
615 solockp
= &slp
->ns_solock
;
619 nd
->nd_starttime
= time
;
621 nd
->nd_nam
= nd
->nd_nam2
;
623 nd
->nd_nam
= slp
->ns_nam
;
626 * Check to see if authorization is needed.
628 if (nfsd
->nfsd_flag
& NFSD_NEEDAUTH
) {
629 nfsd
->nfsd_flag
&= ~NFSD_NEEDAUTH
;
630 nsd
->nsd_haddr
= mtod(nd
->nd_nam
,
631 struct sockaddr_in
*)->sin_addr
.s_addr
;
632 nsd
->nsd_authlen
= nfsd
->nfsd_authlen
;
633 nsd
->nsd_verflen
= nfsd
->nfsd_verflen
;
634 if (!copyout(nfsd
->nfsd_authstr
,nsd
->nsd_authstr
,
635 nfsd
->nfsd_authlen
) &&
636 !copyout(nfsd
->nfsd_verfstr
, nsd
->nsd_verfstr
,
637 nfsd
->nfsd_verflen
) &&
638 !copyout((caddr_t
)nsd
, argp
, sizeof (*nsd
)))
640 cacherep
= RC_DROPIT
;
642 cacherep
= nfsrv_getcache(nd
, slp
, &mreq
);
645 * Check for just starting up for NQNFS and send
646 * fake "try again later" replies to the NQNFS clients.
648 if (notstarted
&& nqnfsstarttime
<= time
.tv_sec
) {
650 nqnfsstarttime
= time
.tv_sec
+ nqsrv_writeslack
;
656 if ((nd
->nd_flag
& ND_NQNFS
) == 0)
657 cacherep
= RC_DROPIT
;
658 else if (nd
->nd_procnum
!= NFSPROC_WRITE
) {
659 nd
->nd_procnum
= NFSPROC_NOOP
;
660 nd
->nd_repstat
= NQNFS_TRYLATER
;
664 } else if (nfsd
->nfsd_flag
& NFSD_AUTHFAIL
) {
665 nfsd
->nfsd_flag
&= ~NFSD_AUTHFAIL
;
666 nd
->nd_procnum
= NFSPROC_NOOP
;
667 nd
->nd_repstat
= (NFSERR_AUTHERR
| AUTH_TOOWEAK
);
669 } else if (nfs_privport
) {
670 /* Check if source port is privileged */
672 struct sockaddr
*nam
= nd
->nd_nam
;
673 struct sockaddr_in
*sin
;
675 sin
= (struct sockaddr_in
*)nam
;
676 port
= ntohs(sin
->sin_port
);
677 if (port
>= IPPORT_RESERVED
&&
678 nd
->nd_procnum
!= NFSPROC_NULL
) {
679 nd
->nd_procnum
= NFSPROC_NOOP
;
680 nd
->nd_repstat
= (NFSERR_AUTHERR
| AUTH_TOOWEAK
);
682 printf("NFS request from unprivileged port (%s:%d)\n",
683 (char *)(inet_ntoa(sin
->sin_addr
)), port
);
690 * Loop to get all the write rpc relies that have been
696 if (nd
&& (nd
->nd_flag
& ND_NFSV3
))
697 procrastinate
= nfsrvw_procrastinate_v3
;
699 procrastinate
= nfsrvw_procrastinate
;
700 if (writes_todo
|| (nd
->nd_procnum
== NFSPROC_WRITE
&&
701 procrastinate
> 0 && !notstarted
))
702 error
= nfsrv_writegather(&nd
, slp
,
703 nfsd
->nfsd_procp
, &mreq
);
705 error
= (*(nfsrv3_procs
[nd
->nd_procnum
]))(nd
,
706 slp
, nfsd
->nfsd_procp
, &mreq
);
710 if (nd
->nd_procnum
!= NQNFSPROC_VACATED
)
712 nfsrv_updatecache(nd
, FALSE
, mreq
);
714 m_freem(nd
->nd_nam2
);
717 nfsstats
.srvrpccnt
[nd
->nd_procnum
]++;
718 nfsrv_updatecache(nd
, TRUE
, mreq
);
719 nd
->nd_mrep
= (struct mbuf
*)0;
727 if (siz
<= 0 || siz
> NFS_MAXPACKET
) {
728 printf("mbuf siz=%d\n",siz
);
729 panic("Bad nfs svc reply");
732 m
->m_pkthdr
.len
= siz
;
733 m
->m_pkthdr
.rcvif
= (struct ifnet
*)0;
735 * For stream protocols, prepend a Sun RPC
738 if (sotype
== SOCK_STREAM
) {
739 M_PREPEND(m
, NFSX_UNSIGNED
, M_WAIT
);
740 *mtod(m
, u_long
*) = htonl(0x80000000 | siz
);
743 (void) nfs_sndlock(solockp
, (struct nfsreq
*)0);
744 if (slp
->ns_flag
& SLP_VALID
)
745 error
= nfs_send(so
, nd
->nd_nam2
, m
, NULL
);
751 nfsd_rt(sotype
, nd
, cacherep
);
753 MFREE(nd
->nd_nam2
, m
);
755 m_freem(nd
->nd_mrep
);
759 nfs_sndunlock(solockp
);
760 if (error
== EINTR
|| error
== ERESTART
) {
761 _FREE_ZONE((caddr_t
)nd
,
762 sizeof *nd
, M_NFSRVDESC
);
770 nfsd_rt(sotype
, nd
, cacherep
);
771 m_freem(nd
->nd_mrep
);
772 m_freem(nd
->nd_nam2
);
776 FREE_ZONE((caddr_t
)nd
, sizeof *nd
, M_NFSRVDESC
);
781 * Check to see if there are outstanding writes that
782 * need to be serviced.
784 cur_usec
= (u_quad_t
)time
.tv_sec
* 1000000 +
785 (u_quad_t
)time
.tv_usec
;
787 if (slp
->ns_tq
.lh_first
&&
788 slp
->ns_tq
.lh_first
->nd_time
<= cur_usec
) {
794 } while (writes_todo
);
796 if (nfsrv_dorec(slp
, nfsd
, &nd
)) {
797 nfsd
->nfsd_flag
&= ~NFSD_REQINPROG
;
798 nfsd
->nfsd_slp
= NULL
;
803 TAILQ_REMOVE(&nfsd_head
, nfsd
, nfsd_chain
);
805 _FREE((caddr_t
)nfsd
, M_NFSD
);
806 nsd
->nsd_nfsd
= (struct nfsd
*)0;
807 if (--nfs_numnfsd
== 0)
808 nfsrv_init(TRUE
); /* Reinitialize everything */
811 #endif /* NFS_NOSERVER */
814 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
816 SYSCTL_INT(_vfs_nfs
, OID_AUTO
, defect
, CTLFLAG_RW
, &nfs_defect
, 0, "");
819 static int nfssvc_iod_continue(int);
822 * Asynchronous I/O daemons for client nfs.
823 * They do read-ahead and write-behind operations on the block I/O cache.
824 * Never returns unless it fails or gets killed.
830 register struct buf
*bp
;
831 register int i
, myiod
;
832 struct nfsmount
*nmp
;
837 * Assign my position or return error if too many already running
840 for (i
= 0; i
< NFS_MAXASYNCDAEMON
; i
++)
841 if (nfs_asyncdaemon
[i
] == 0) {
842 nfs_asyncdaemon
[i
]++;
850 /* stuff myiod into uthread to get off local stack for
853 ut
= get_bsdthread_info(current_act());
854 ut
->uu_state
.uu_nfs_myiod
= myiod
; /* squirrel away for continuation */
856 nfssvc_iod_continue(0);
862 * Continuation for Asynchronous I/O daemons for client nfs.
865 nfssvc_iod_continue(error
)
867 register struct buf
*bp
;
868 register int i
, myiod
;
869 struct nfsmount
*nmp
;
874 * real myiod is stored in uthread, recover it
876 ut
= get_bsdthread_info(current_act());
877 myiod
= ut
->uu_state
.uu_nfs_myiod
;
878 p
= get_bsdtask_info(current_task());
881 * Just loop around doin our stuff until SIGKILL
882 * - actually we don't loop with continuations...
885 while (((nmp
= nfs_iodmount
[myiod
]) == NULL
886 || nmp
->nm_bufq
.tqh_first
== NULL
)
890 nfs_iodwant
[myiod
] = p
;
891 nfs_iodmount
[myiod
] = NULL
;
892 error
= tsleep0((caddr_t
)&nfs_iodwant
[myiod
],
893 PWAIT
| PCATCH
, "nfsidl", 0, nfssvc_iod_continue
);
897 nfs_asyncdaemon
[myiod
] = 0;
898 if (nmp
) nmp
->nm_bufqiods
--;
899 nfs_iodwant
[myiod
] = NULL
;
900 nfs_iodmount
[myiod
] = NULL
;
902 if (error
== EINTR
|| error
== ERESTART
)
904 #if defined (__i386__)
907 unix_syscall_return(error
);
910 while ((bp
= nmp
->nm_bufq
.tqh_first
) != NULL
) {
911 /* Take one off the front of the list */
912 TAILQ_REMOVE(&nmp
->nm_bufq
, bp
, b_freelist
);
914 if (nmp
->nm_bufqwant
&& nmp
->nm_bufqlen
< 2 * nfs_numasync
) {
915 nmp
->nm_bufqwant
= FALSE
;
916 wakeup(&nmp
->nm_bufq
);
918 if (ISSET(bp
->b_flags
, B_READ
))
919 (void) nfs_doio(bp
, bp
->b_rcred
, (struct proc
*)0);
921 (void) nfs_doio(bp
, bp
->b_wcred
, (struct proc
*)0);
924 * If there are more than one iod on this mount, then defect
925 * so that the iods can be shared out fairly between the mounts
927 if (nfs_defect
&& nmp
->nm_bufqiods
> 1) {
929 ("nfssvc_iod: iod %d defecting from mount %p\n",
931 nfs_iodmount
[myiod
] = NULL
;
940 * Shut down a socket associated with an nfssvc_sock structure.
941 * Should be called with the send lock set, if required.
942 * The trick here is to increment the sref at the start, so that the nfsds
943 * will stop using it and clear ns_flag at the end so that it will not be
944 * reassigned during cleanup.
948 register struct nfssvc_sock
*slp
;
950 register struct nfsuid
*nuidp
, *nnuidp
;
951 register struct nfsrv_descript
*nwp
, *nnwp
;
957 slp
->ns_flag
&= ~SLP_ALLFLAGS
;
960 slp
->ns_fp
= (struct file
*)0;
962 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
963 so
->so_upcall
= NULL
;
964 so
->so_rcv
.sb_flags
&= ~SB_UPCALL
;
966 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
967 closef(fp
, (struct proc
*)0);
969 MFREE(slp
->ns_nam
, m
);
970 m_freem(slp
->ns_raw
);
971 m_freem(slp
->ns_rec
);
972 for (nuidp
= slp
->ns_uidlruhead
.tqh_first
; nuidp
!= 0;
974 nnuidp
= nuidp
->nu_lru
.tqe_next
;
975 LIST_REMOVE(nuidp
, nu_hash
);
976 TAILQ_REMOVE(&slp
->ns_uidlruhead
, nuidp
, nu_lru
);
977 if (nuidp
->nu_flag
& NU_NAM
)
978 m_freem(nuidp
->nu_nam
);
979 _FREE_ZONE((caddr_t
)nuidp
,
980 sizeof (struct nfsuid
), M_NFSUID
);
983 for (nwp
= slp
->ns_tq
.lh_first
; nwp
; nwp
= nnwp
) {
984 nnwp
= nwp
->nd_tq
.le_next
;
985 LIST_REMOVE(nwp
, nd_tq
);
986 _FREE_ZONE((caddr_t
)nwp
, sizeof *nwp
, M_NFSRVDESC
);
988 LIST_INIT(&slp
->ns_tq
);
994 * Get an authorization string for the uid by having the mount_nfs sitting
995 * on this mount point porpous out of the kernel and do it.
998 nfs_getauth(nmp
, rep
, cred
, auth_str
, auth_len
, verf_str
, verf_len
, key
)
999 register struct nfsmount
*nmp
;
1006 NFSKERBKEY_T key
; /* return session key */
1010 while ((nmp
->nm_flag
& NFSMNT_WAITAUTH
) == 0) {
1011 nmp
->nm_flag
|= NFSMNT_WANTAUTH
;
1012 (void) tsleep((caddr_t
)&nmp
->nm_authtype
, PSOCK
,
1013 "nfsauth1", 2 * hz
);
1014 error
= nfs_sigintr(nmp
, rep
, rep
->r_procp
);
1016 nmp
->nm_flag
&= ~NFSMNT_WANTAUTH
;
1020 nmp
->nm_flag
&= ~(NFSMNT_WAITAUTH
| NFSMNT_WANTAUTH
);
1021 MALLOC(*auth_str
, char *, RPCAUTH_MAXSIZ
, M_TEMP
, M_WAITOK
);
1022 nmp
->nm_authstr
= *auth_str
;
1023 nmp
->nm_authlen
= RPCAUTH_MAXSIZ
;
1024 nmp
->nm_verfstr
= verf_str
;
1025 nmp
->nm_verflen
= *verf_len
;
1026 nmp
->nm_authuid
= cred
->cr_uid
;
1027 wakeup((caddr_t
)&nmp
->nm_authstr
);
1030 * And wait for mount_nfs to do its stuff.
1032 while ((nmp
->nm_flag
& NFSMNT_HASAUTH
) == 0 && error
== 0) {
1033 (void) tsleep((caddr_t
)&nmp
->nm_authlen
, PSOCK
,
1034 "nfsauth2", 2 * hz
);
1035 error
= nfs_sigintr(nmp
, rep
, rep
->r_procp
);
1037 if (nmp
->nm_flag
& NFSMNT_AUTHERR
) {
1038 nmp
->nm_flag
&= ~NFSMNT_AUTHERR
;
1042 _FREE((caddr_t
)*auth_str
, M_TEMP
);
1044 *auth_len
= nmp
->nm_authlen
;
1045 *verf_len
= nmp
->nm_verflen
;
1046 bcopy((caddr_t
)nmp
->nm_key
, (caddr_t
)key
, sizeof (key
));
1048 nmp
->nm_flag
&= ~NFSMNT_HASAUTH
;
1049 nmp
->nm_flag
|= NFSMNT_WAITAUTH
;
1050 if (nmp
->nm_flag
& NFSMNT_WANTAUTH
) {
1051 nmp
->nm_flag
&= ~NFSMNT_WANTAUTH
;
1052 wakeup((caddr_t
)&nmp
->nm_authtype
);
1058 * Get a nickname authenticator and verifier.
1061 nfs_getnickauth(nmp
, cred
, auth_str
, auth_len
, verf_str
, verf_len
)
1062 struct nfsmount
*nmp
;
1069 register struct nfsuid
*nuidp
;
1070 register u_long
*nickp
, *verfp
;
1071 struct timeval ktvin
, ktvout
;
1074 if (verf_len
< (4 * NFSX_UNSIGNED
))
1075 panic("nfs_getnickauth verf too small");
1077 for (nuidp
= NMUIDHASH(nmp
, cred
->cr_uid
)->lh_first
;
1078 nuidp
!= 0; nuidp
= nuidp
->nu_hash
.le_next
) {
1079 if (nuidp
->nu_cr
.cr_uid
== cred
->cr_uid
)
1082 if (!nuidp
|| nuidp
->nu_expire
< time
.tv_sec
)
1086 * Move to the end of the lru list (end of lru == most recently used).
1088 TAILQ_REMOVE(&nmp
->nm_uidlruhead
, nuidp
, nu_lru
);
1089 TAILQ_INSERT_TAIL(&nmp
->nm_uidlruhead
, nuidp
, nu_lru
);
1091 MALLOC(nickp
, u_long
*, 2 * NFSX_UNSIGNED
, M_TEMP
, M_WAITOK
);
1092 *nickp
++ = txdr_unsigned(RPCAKN_NICKNAME
);
1093 *nickp
= txdr_unsigned(nuidp
->nu_nickname
);
1094 *auth_str
= (char *)nickp
;
1095 *auth_len
= 2 * NFSX_UNSIGNED
;
1098 * Now we must encrypt the verifier and package it up.
1100 verfp
= (u_long
*)verf_str
;
1101 *verfp
++ = txdr_unsigned(RPCAKN_NICKNAME
);
1102 if (time
.tv_sec
> nuidp
->nu_timestamp
.tv_sec
||
1103 (time
.tv_sec
== nuidp
->nu_timestamp
.tv_sec
&&
1104 time
.tv_usec
> nuidp
->nu_timestamp
.tv_usec
))
1105 nuidp
->nu_timestamp
= time
;
1107 nuidp
->nu_timestamp
.tv_usec
++;
1108 ktvin
.tv_sec
= txdr_unsigned(nuidp
->nu_timestamp
.tv_sec
);
1109 ktvin
.tv_usec
= txdr_unsigned(nuidp
->nu_timestamp
.tv_usec
);
1112 * Now encrypt the timestamp verifier in ecb mode using the session
1119 *verfp
++ = ktvout
.tv_sec
;
1120 *verfp
++ = ktvout
.tv_usec
;
1126 * Save the current nickname in a hash list entry on the mount point.
1129 nfs_savenickauth(nmp
, cred
, len
, key
, mdp
, dposp
, mrep
)
1130 register struct nfsmount
*nmp
;
1138 register struct nfsuid
*nuidp
;
1139 register u_long
*tl
;
1141 struct mbuf
*md
= *mdp
;
1142 struct timeval ktvin
, ktvout
;
1144 char *dpos
= *dposp
, *cp2
;
1145 int deltasec
, error
= 0;
1147 if (len
== (3 * NFSX_UNSIGNED
)) {
1148 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
1149 ktvin
.tv_sec
= *tl
++;
1150 ktvin
.tv_usec
= *tl
++;
1151 nick
= fxdr_unsigned(u_long
, *tl
);
1154 * Decrypt the timestamp in ecb mode.
1159 ktvout
.tv_sec
= fxdr_unsigned(long, ktvout
.tv_sec
);
1160 ktvout
.tv_usec
= fxdr_unsigned(long, ktvout
.tv_usec
);
1161 deltasec
= time
.tv_sec
- ktvout
.tv_sec
;
1163 deltasec
= -deltasec
;
1165 * If ok, add it to the hash list for the mount point.
1167 if (deltasec
<= NFS_KERBCLOCKSKEW
) {
1168 if (nmp
->nm_numuids
< nuidhash_max
) {
1170 MALLOC_ZONE(nuidp
, struct nfsuid
*,
1171 sizeof (struct nfsuid
),
1172 M_NFSUID
, M_WAITOK
);
1174 nuidp
= nmp
->nm_uidlruhead
.tqh_first
;
1175 LIST_REMOVE(nuidp
, nu_hash
);
1176 TAILQ_REMOVE(&nmp
->nm_uidlruhead
, nuidp
,
1180 nuidp
->nu_cr
.cr_uid
= cred
->cr_uid
;
1181 nuidp
->nu_expire
= time
.tv_sec
+ NFS_KERBTTL
;
1182 nuidp
->nu_timestamp
= ktvout
;
1183 nuidp
->nu_nickname
= nick
;
1184 bcopy(key
, nuidp
->nu_key
, sizeof (key
));
1185 TAILQ_INSERT_TAIL(&nmp
->nm_uidlruhead
, nuidp
,
1187 LIST_INSERT_HEAD(NMUIDHASH(nmp
, cred
->cr_uid
),
1191 nfsm_adv(nfsm_rndup(len
));
1198 #ifndef NFS_NOSERVER
1201 * Derefence a server socket structure. If it has no more references and
1202 * is no longer valid, you can throw it away.
1206 register struct nfssvc_sock
*slp
;
1208 if (--(slp
->ns_sref
) == 0 && (slp
->ns_flag
& SLP_VALID
) == 0) {
1209 TAILQ_REMOVE(&nfssvc_sockhead
, slp
, ns_chain
);
1210 _FREE((caddr_t
)slp
, M_NFSSVC
);
1215 * Initialize the data structures for the server.
1216 * Handshake with any new nfsds starting up to avoid any chance of
1220 nfsrv_init(terminating
)
1223 register struct nfssvc_sock
*slp
, *nslp
;
1225 if (nfssvc_sockhead_flag
& SLP_INIT
)
1227 nfssvc_sockhead_flag
|= SLP_INIT
;
1229 for (slp
= nfssvc_sockhead
.tqh_first
; slp
!= 0; slp
= nslp
) {
1230 nslp
= slp
->ns_chain
.tqe_next
;
1231 if (slp
->ns_flag
& SLP_VALID
)
1233 TAILQ_REMOVE(&nfssvc_sockhead
, slp
, ns_chain
);
1234 _FREE((caddr_t
)slp
, M_NFSSVC
);
1236 nfsrv_cleancache(); /* And clear out server cache */
1237 /* XXX CSM 12/4/97 Revisit when enabling WebNFS */
1240 nfs_pub
.np_valid
= 0;
1245 TAILQ_INIT(&nfssvc_sockhead
);
1246 nfssvc_sockhead_flag
&= ~SLP_INIT
;
1247 if (nfssvc_sockhead_flag
& SLP_WANTINIT
) {
1248 nfssvc_sockhead_flag
&= ~SLP_WANTINIT
;
1249 wakeup((caddr_t
)&nfssvc_sockhead
);
1252 TAILQ_INIT(&nfsd_head
);
1253 nfsd_head_flag
&= ~NFSD_CHECKSLP
;
1255 MALLOC(nfs_udpsock
, struct nfssvc_sock
*, sizeof(struct nfssvc_sock
),
1256 M_NFSSVC
, M_WAITOK
);
1257 bzero((caddr_t
)nfs_udpsock
, sizeof (struct nfssvc_sock
));
1258 TAILQ_INIT(&nfs_udpsock
->ns_uidlruhead
);
1259 TAILQ_INSERT_HEAD(&nfssvc_sockhead
, nfs_udpsock
, ns_chain
);
1261 MALLOC(nfs_cltpsock
, struct nfssvc_sock
*, sizeof(struct nfssvc_sock
),
1262 M_NFSSVC
, M_WAITOK
);
1263 bzero((caddr_t
)nfs_cltpsock
, sizeof (struct nfssvc_sock
));
1264 TAILQ_INIT(&nfs_cltpsock
->ns_uidlruhead
);
1265 TAILQ_INSERT_TAIL(&nfssvc_sockhead
, nfs_cltpsock
, ns_chain
);
1269 * Add entries to the server monitor log.
1272 nfsd_rt(sotype
, nd
, cacherep
)
1274 register struct nfsrv_descript
*nd
;
1277 register struct drt
*rt
;
1279 rt
= &nfsdrt
.drt
[nfsdrt
.pos
];
1280 if (cacherep
== RC_DOIT
)
1282 else if (cacherep
== RC_REPLY
)
1283 rt
->flag
= DRT_CACHEREPLY
;
1285 rt
->flag
= DRT_CACHEDROP
;
1286 if (sotype
== SOCK_STREAM
)
1287 rt
->flag
|= DRT_TCP
;
1288 if (nd
->nd_flag
& ND_NQNFS
)
1289 rt
->flag
|= DRT_NQNFS
;
1290 else if (nd
->nd_flag
& ND_NFSV3
)
1291 rt
->flag
|= DRT_NFSV3
;
1292 rt
->proc
= nd
->nd_procnum
;
1293 if (mtod(nd
->nd_nam
, struct sockaddr
*)->sa_family
== AF_INET
)
1294 rt
->ipadr
= mtod(nd
->nd_nam
, struct sockaddr_in
*)->sin_addr
.s_addr
;
1296 rt
->ipadr
= INADDR_ANY
;
1297 rt
->resptime
= ((time
.tv_sec
- nd
->nd_starttime
.tv_sec
) * 1000000) +
1298 (time
.tv_usec
- nd
->nd_starttime
.tv_usec
);
1300 nfsdrt
.pos
= (nfsdrt
.pos
+ 1) % NFSRTTLOGSIZ
;
1302 #endif /* NFS_NOSERVER */