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
= (struct uthread
*)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
= (struct uthread
*)get_bsdthread_info(current_act());
877 myiod
= ut
->uu_state
.uu_nfs_myiod
;
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 unix_syscall_return(error
);
906 while ((bp
= nmp
->nm_bufq
.tqh_first
) != NULL
) {
907 /* Take one off the front of the list */
908 TAILQ_REMOVE(&nmp
->nm_bufq
, bp
, b_freelist
);
910 if (nmp
->nm_bufqwant
&& nmp
->nm_bufqlen
< 2 * nfs_numasync
) {
911 nmp
->nm_bufqwant
= FALSE
;
912 wakeup(&nmp
->nm_bufq
);
914 if (ISSET(bp
->b_flags
, B_READ
))
915 (void) nfs_doio(bp
, bp
->b_rcred
, (struct proc
*)0);
917 (void) nfs_doio(bp
, bp
->b_wcred
, (struct proc
*)0);
920 * If there are more than one iod on this mount, then defect
921 * so that the iods can be shared out fairly between the mounts
923 if (nfs_defect
&& nmp
->nm_bufqiods
> 1) {
925 ("nfssvc_iod: iod %d defecting from mount %p\n",
927 nfs_iodmount
[myiod
] = NULL
;
936 * Shut down a socket associated with an nfssvc_sock structure.
937 * Should be called with the send lock set, if required.
938 * The trick here is to increment the sref at the start, so that the nfsds
939 * will stop using it and clear ns_flag at the end so that it will not be
940 * reassigned during cleanup.
944 register struct nfssvc_sock
*slp
;
946 register struct nfsuid
*nuidp
, *nnuidp
;
947 register struct nfsrv_descript
*nwp
, *nnwp
;
953 slp
->ns_flag
&= ~SLP_ALLFLAGS
;
956 slp
->ns_fp
= (struct file
*)0;
958 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
959 so
->so_upcall
= NULL
;
960 so
->so_rcv
.sb_flags
&= ~SB_UPCALL
;
962 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
963 closef(fp
, (struct proc
*)0);
965 MFREE(slp
->ns_nam
, m
);
966 m_freem(slp
->ns_raw
);
967 m_freem(slp
->ns_rec
);
968 for (nuidp
= slp
->ns_uidlruhead
.tqh_first
; nuidp
!= 0;
970 nnuidp
= nuidp
->nu_lru
.tqe_next
;
971 LIST_REMOVE(nuidp
, nu_hash
);
972 TAILQ_REMOVE(&slp
->ns_uidlruhead
, nuidp
, nu_lru
);
973 if (nuidp
->nu_flag
& NU_NAM
)
974 m_freem(nuidp
->nu_nam
);
975 _FREE_ZONE((caddr_t
)nuidp
,
976 sizeof (struct nfsuid
), M_NFSUID
);
979 for (nwp
= slp
->ns_tq
.lh_first
; nwp
; nwp
= nnwp
) {
980 nnwp
= nwp
->nd_tq
.le_next
;
981 LIST_REMOVE(nwp
, nd_tq
);
982 _FREE_ZONE((caddr_t
)nwp
, sizeof *nwp
, M_NFSRVDESC
);
984 LIST_INIT(&slp
->ns_tq
);
990 * Get an authorization string for the uid by having the mount_nfs sitting
991 * on this mount point porpous out of the kernel and do it.
994 nfs_getauth(nmp
, rep
, cred
, auth_str
, auth_len
, verf_str
, verf_len
, key
)
995 register struct nfsmount
*nmp
;
1002 NFSKERBKEY_T key
; /* return session key */
1006 while ((nmp
->nm_flag
& NFSMNT_WAITAUTH
) == 0) {
1007 nmp
->nm_flag
|= NFSMNT_WANTAUTH
;
1008 (void) tsleep((caddr_t
)&nmp
->nm_authtype
, PSOCK
,
1009 "nfsauth1", 2 * hz
);
1010 error
= nfs_sigintr(nmp
, rep
, rep
->r_procp
);
1012 nmp
->nm_flag
&= ~NFSMNT_WANTAUTH
;
1016 nmp
->nm_flag
&= ~(NFSMNT_WAITAUTH
| NFSMNT_WANTAUTH
);
1017 MALLOC(*auth_str
, char *, RPCAUTH_MAXSIZ
, M_TEMP
, M_WAITOK
);
1018 nmp
->nm_authstr
= *auth_str
;
1019 nmp
->nm_authlen
= RPCAUTH_MAXSIZ
;
1020 nmp
->nm_verfstr
= verf_str
;
1021 nmp
->nm_verflen
= *verf_len
;
1022 nmp
->nm_authuid
= cred
->cr_uid
;
1023 wakeup((caddr_t
)&nmp
->nm_authstr
);
1026 * And wait for mount_nfs to do its stuff.
1028 while ((nmp
->nm_flag
& NFSMNT_HASAUTH
) == 0 && error
== 0) {
1029 (void) tsleep((caddr_t
)&nmp
->nm_authlen
, PSOCK
,
1030 "nfsauth2", 2 * hz
);
1031 error
= nfs_sigintr(nmp
, rep
, rep
->r_procp
);
1033 if (nmp
->nm_flag
& NFSMNT_AUTHERR
) {
1034 nmp
->nm_flag
&= ~NFSMNT_AUTHERR
;
1038 _FREE((caddr_t
)*auth_str
, M_TEMP
);
1040 *auth_len
= nmp
->nm_authlen
;
1041 *verf_len
= nmp
->nm_verflen
;
1042 bcopy((caddr_t
)nmp
->nm_key
, (caddr_t
)key
, sizeof (key
));
1044 nmp
->nm_flag
&= ~NFSMNT_HASAUTH
;
1045 nmp
->nm_flag
|= NFSMNT_WAITAUTH
;
1046 if (nmp
->nm_flag
& NFSMNT_WANTAUTH
) {
1047 nmp
->nm_flag
&= ~NFSMNT_WANTAUTH
;
1048 wakeup((caddr_t
)&nmp
->nm_authtype
);
1054 * Get a nickname authenticator and verifier.
1057 nfs_getnickauth(nmp
, cred
, auth_str
, auth_len
, verf_str
, verf_len
)
1058 struct nfsmount
*nmp
;
1065 register struct nfsuid
*nuidp
;
1066 register u_long
*nickp
, *verfp
;
1067 struct timeval ktvin
, ktvout
;
1070 if (verf_len
< (4 * NFSX_UNSIGNED
))
1071 panic("nfs_getnickauth verf too small");
1073 for (nuidp
= NMUIDHASH(nmp
, cred
->cr_uid
)->lh_first
;
1074 nuidp
!= 0; nuidp
= nuidp
->nu_hash
.le_next
) {
1075 if (nuidp
->nu_cr
.cr_uid
== cred
->cr_uid
)
1078 if (!nuidp
|| nuidp
->nu_expire
< time
.tv_sec
)
1082 * Move to the end of the lru list (end of lru == most recently used).
1084 TAILQ_REMOVE(&nmp
->nm_uidlruhead
, nuidp
, nu_lru
);
1085 TAILQ_INSERT_TAIL(&nmp
->nm_uidlruhead
, nuidp
, nu_lru
);
1087 MALLOC(nickp
, u_long
*, 2 * NFSX_UNSIGNED
, M_TEMP
, M_WAITOK
);
1088 *nickp
++ = txdr_unsigned(RPCAKN_NICKNAME
);
1089 *nickp
= txdr_unsigned(nuidp
->nu_nickname
);
1090 *auth_str
= (char *)nickp
;
1091 *auth_len
= 2 * NFSX_UNSIGNED
;
1094 * Now we must encrypt the verifier and package it up.
1096 verfp
= (u_long
*)verf_str
;
1097 *verfp
++ = txdr_unsigned(RPCAKN_NICKNAME
);
1098 if (time
.tv_sec
> nuidp
->nu_timestamp
.tv_sec
||
1099 (time
.tv_sec
== nuidp
->nu_timestamp
.tv_sec
&&
1100 time
.tv_usec
> nuidp
->nu_timestamp
.tv_usec
))
1101 nuidp
->nu_timestamp
= time
;
1103 nuidp
->nu_timestamp
.tv_usec
++;
1104 ktvin
.tv_sec
= txdr_unsigned(nuidp
->nu_timestamp
.tv_sec
);
1105 ktvin
.tv_usec
= txdr_unsigned(nuidp
->nu_timestamp
.tv_usec
);
1108 * Now encrypt the timestamp verifier in ecb mode using the session
1115 *verfp
++ = ktvout
.tv_sec
;
1116 *verfp
++ = ktvout
.tv_usec
;
1122 * Save the current nickname in a hash list entry on the mount point.
1125 nfs_savenickauth(nmp
, cred
, len
, key
, mdp
, dposp
, mrep
)
1126 register struct nfsmount
*nmp
;
1134 register struct nfsuid
*nuidp
;
1135 register u_long
*tl
;
1137 struct mbuf
*md
= *mdp
;
1138 struct timeval ktvin
, ktvout
;
1140 char *dpos
= *dposp
, *cp2
;
1141 int deltasec
, error
= 0;
1143 if (len
== (3 * NFSX_UNSIGNED
)) {
1144 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
1145 ktvin
.tv_sec
= *tl
++;
1146 ktvin
.tv_usec
= *tl
++;
1147 nick
= fxdr_unsigned(u_long
, *tl
);
1150 * Decrypt the timestamp in ecb mode.
1155 ktvout
.tv_sec
= fxdr_unsigned(long, ktvout
.tv_sec
);
1156 ktvout
.tv_usec
= fxdr_unsigned(long, ktvout
.tv_usec
);
1157 deltasec
= time
.tv_sec
- ktvout
.tv_sec
;
1159 deltasec
= -deltasec
;
1161 * If ok, add it to the hash list for the mount point.
1163 if (deltasec
<= NFS_KERBCLOCKSKEW
) {
1164 if (nmp
->nm_numuids
< nuidhash_max
) {
1166 MALLOC_ZONE(nuidp
, struct nfsuid
*,
1167 sizeof (struct nfsuid
),
1168 M_NFSUID
, M_WAITOK
);
1170 nuidp
= nmp
->nm_uidlruhead
.tqh_first
;
1171 LIST_REMOVE(nuidp
, nu_hash
);
1172 TAILQ_REMOVE(&nmp
->nm_uidlruhead
, nuidp
,
1176 nuidp
->nu_cr
.cr_uid
= cred
->cr_uid
;
1177 nuidp
->nu_expire
= time
.tv_sec
+ NFS_KERBTTL
;
1178 nuidp
->nu_timestamp
= ktvout
;
1179 nuidp
->nu_nickname
= nick
;
1180 bcopy(key
, nuidp
->nu_key
, sizeof (key
));
1181 TAILQ_INSERT_TAIL(&nmp
->nm_uidlruhead
, nuidp
,
1183 LIST_INSERT_HEAD(NMUIDHASH(nmp
, cred
->cr_uid
),
1187 nfsm_adv(nfsm_rndup(len
));
1194 #ifndef NFS_NOSERVER
1197 * Derefence a server socket structure. If it has no more references and
1198 * is no longer valid, you can throw it away.
1202 register struct nfssvc_sock
*slp
;
1204 if (--(slp
->ns_sref
) == 0 && (slp
->ns_flag
& SLP_VALID
) == 0) {
1205 TAILQ_REMOVE(&nfssvc_sockhead
, slp
, ns_chain
);
1206 _FREE((caddr_t
)slp
, M_NFSSVC
);
1211 * Initialize the data structures for the server.
1212 * Handshake with any new nfsds starting up to avoid any chance of
1216 nfsrv_init(terminating
)
1219 register struct nfssvc_sock
*slp
, *nslp
;
1221 if (nfssvc_sockhead_flag
& SLP_INIT
)
1223 nfssvc_sockhead_flag
|= SLP_INIT
;
1225 for (slp
= nfssvc_sockhead
.tqh_first
; slp
!= 0; slp
= nslp
) {
1226 nslp
= slp
->ns_chain
.tqe_next
;
1227 if (slp
->ns_flag
& SLP_VALID
)
1229 TAILQ_REMOVE(&nfssvc_sockhead
, slp
, ns_chain
);
1230 _FREE((caddr_t
)slp
, M_NFSSVC
);
1232 nfsrv_cleancache(); /* And clear out server cache */
1233 /* XXX CSM 12/4/97 Revisit when enabling WebNFS */
1236 nfs_pub
.np_valid
= 0;
1241 TAILQ_INIT(&nfssvc_sockhead
);
1242 nfssvc_sockhead_flag
&= ~SLP_INIT
;
1243 if (nfssvc_sockhead_flag
& SLP_WANTINIT
) {
1244 nfssvc_sockhead_flag
&= ~SLP_WANTINIT
;
1245 wakeup((caddr_t
)&nfssvc_sockhead
);
1248 TAILQ_INIT(&nfsd_head
);
1249 nfsd_head_flag
&= ~NFSD_CHECKSLP
;
1251 MALLOC(nfs_udpsock
, struct nfssvc_sock
*, sizeof(struct nfssvc_sock
),
1252 M_NFSSVC
, M_WAITOK
);
1253 bzero((caddr_t
)nfs_udpsock
, sizeof (struct nfssvc_sock
));
1254 TAILQ_INIT(&nfs_udpsock
->ns_uidlruhead
);
1255 TAILQ_INSERT_HEAD(&nfssvc_sockhead
, nfs_udpsock
, ns_chain
);
1257 MALLOC(nfs_cltpsock
, struct nfssvc_sock
*, sizeof(struct nfssvc_sock
),
1258 M_NFSSVC
, M_WAITOK
);
1259 bzero((caddr_t
)nfs_cltpsock
, sizeof (struct nfssvc_sock
));
1260 TAILQ_INIT(&nfs_cltpsock
->ns_uidlruhead
);
1261 TAILQ_INSERT_TAIL(&nfssvc_sockhead
, nfs_cltpsock
, ns_chain
);
1265 * Add entries to the server monitor log.
1268 nfsd_rt(sotype
, nd
, cacherep
)
1270 register struct nfsrv_descript
*nd
;
1273 register struct drt
*rt
;
1275 rt
= &nfsdrt
.drt
[nfsdrt
.pos
];
1276 if (cacherep
== RC_DOIT
)
1278 else if (cacherep
== RC_REPLY
)
1279 rt
->flag
= DRT_CACHEREPLY
;
1281 rt
->flag
= DRT_CACHEDROP
;
1282 if (sotype
== SOCK_STREAM
)
1283 rt
->flag
|= DRT_TCP
;
1284 if (nd
->nd_flag
& ND_NQNFS
)
1285 rt
->flag
|= DRT_NQNFS
;
1286 else if (nd
->nd_flag
& ND_NFSV3
)
1287 rt
->flag
|= DRT_NFSV3
;
1288 rt
->proc
= nd
->nd_procnum
;
1289 if (mtod(nd
->nd_nam
, struct sockaddr
*)->sa_family
== AF_INET
)
1290 rt
->ipadr
= mtod(nd
->nd_nam
, struct sockaddr_in
*)->sin_addr
.s_addr
;
1292 rt
->ipadr
= INADDR_ANY
;
1293 rt
->resptime
= ((time
.tv_sec
- nd
->nd_starttime
.tv_sec
) * 1000000) +
1294 (time
.tv_usec
- nd
->nd_starttime
.tv_usec
);
1296 nfsdrt
.pos
= (nfsdrt
.pos
+ 1) % NFSRTTLOGSIZ
;
1298 #endif /* NFS_NOSERVER */