]> git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/nfs_syscalls.c
3ec010a9312caf60663b8cac20b90371b78fa5b9
[apple/xnu.git] / bsd / nfs / nfs_syscalls.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
23 /*
24 * Copyright (c) 1989, 1993
25 * The Regents of the University of California. All rights reserved.
26 *
27 * This code is derived from software contributed to Berkeley by
28 * Rick Macklem at The University of Guelph.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
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.
45 *
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
56 * SUCH DAMAGE.
57 *
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 $
60 */
61
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 /* XXX CSM 11/25/97 FreeBSD's generated syscall prototypes */
65 #ifdef notyet
66 #include <sys/sysproto.h>
67 #endif
68 #include <sys/kernel.h>
69 #include <sys/file.h>
70 #include <sys/filedesc.h>
71 #include <sys/stat.h>
72 #include <sys/vnode.h>
73 #include <sys/mount.h>
74 #include <sys/proc.h>
75 #include <sys/sysctl.h>
76 #include <sys/uio.h>
77 #include <sys/malloc.h>
78 #include <sys/buf.h>
79 #include <sys/mbuf.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>
86 #include <sys/user.h>
87 #include <machine/spl.h>
88
89 #include <netinet/in.h>
90 #include <netinet/tcp.h>
91 #if ISO
92 #include <netiso/iso.h>
93 #endif
94 #include <nfs/xdr_subs.h>
95 #include <nfs/rpcv2.h>
96 #include <nfs/nfsproto.h>
97 #include <nfs/nfs.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>
104
105
106 /* Global defs. */
107 extern int (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *nd,
108 struct nfssvc_sock *slp,
109 struct proc *procp,
110 struct mbuf **mreqp));
111 extern int nfs_numasync;
112 extern time_t nqnfsstarttime;
113 extern int nqsrv_writeslack;
114 extern int nfsrtton;
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;
120
121 static void nfsrv_zapsock __P((struct nfssvc_sock *slp));
122 static int nfssvc_iod __P((struct proc *));
123
124 #define TRUE 1
125 #define FALSE 0
126
127 static int nfs_asyncdaemon[NFS_MAXASYNCDAEMON];
128
129 #ifndef NFS_NOSERVER
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,
136 int cacherep));
137 static int nfssvc_addsock __P((struct file *, struct mbuf *,
138 struct proc *));
139 static int nfssvc_nfsd __P((struct nfsd_srvargs *,caddr_t,struct proc *));
140
141 static int nfs_privport = 0;
142 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
143 #ifdef notyet
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, "");
147 #endif
148
149 /*
150 * NFS server system calls
151 * getfh() lives here too, but maybe should move to kern/vfs_syscalls.c
152 */
153
154 /*
155 * Get file handle system call
156 */
157 #ifndef _SYS_SYSPROTO_H_
158 struct getfh_args {
159 char *fname;
160 fhandle_t *fhp;
161 };
162 #endif
163 int
164 getfh(p, uap)
165 struct proc *p;
166 register struct getfh_args *uap;
167 {
168 register struct vnode *vp;
169 fhandle_t fh;
170 int error;
171 struct nameidata nd;
172
173 /*
174 * Must be super user
175 */
176 error = suser(p->p_ucred, &p->p_acflag);
177 if(error)
178 return (error);
179 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->fname, p);
180 error = namei(&nd);
181 if (error)
182 return (error);
183 vp = nd.ni_vp;
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);
187 vput(vp);
188 if (error)
189 return (error);
190 error = copyout((caddr_t)&fh, (caddr_t)uap->fhp, sizeof (fh));
191 return (error);
192 }
193
194 #endif /* NFS_NOSERVER */
195 /*
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
201 */
202 #ifndef _SYS_SYSPROTO_H_
203 struct nfssvc_args {
204 int flag;
205 caddr_t argp;
206 };
207 #endif
208 int
209 nfssvc(p, uap)
210 struct proc *p;
211 register struct nfssvc_args *uap;
212 {
213 #ifndef NFS_NOSERVER
214 struct nameidata nd;
215 struct file *fp;
216 struct mbuf *nam;
217 struct nfsd_args nfsdarg;
218 struct nfsd_srvargs nfsd_srvargs, *nsd = &nfsd_srvargs;
219 struct nfsd_cargs ncd;
220 struct nfsd *nfsd;
221 struct nfssvc_sock *slp;
222 struct nfsuid *nuidp;
223 struct nfsmount *nmp;
224 #endif /* NFS_NOSERVER */
225 int error;
226
227 /*
228 * Must be super user
229 */
230 error = suser(p->p_ucred, &p->p_acflag);
231 if(error)
232 return (error);
233 while (nfssvc_sockhead_flag & SLP_INIT) {
234 nfssvc_sockhead_flag |= SLP_WANTINIT;
235 (void) tsleep((caddr_t)&nfssvc_sockhead, PSOCK, "nfsd init", 0);
236 }
237 if (uap->flag & NFSSVC_BIOD)
238 error = nfssvc_iod(p);
239 #ifdef NFS_NOSERVER
240 else
241 error = ENXIO;
242 #else /* !NFS_NOSERVER */
243 else if (uap->flag & NFSSVC_MNTD) {
244 error = copyin(uap->argp, (caddr_t)&ncd, sizeof (ncd));
245 if (error)
246 return (error);
247 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
248 ncd.ncd_dirp, p);
249 error = namei(&nd);
250 if (error)
251 return (error);
252 if ((nd.ni_vp->v_flag & VROOT) == 0)
253 error = EINVAL;
254 nmp = VFSTONFS(nd.ni_vp->v_mount);
255 vput(nd.ni_vp);
256 if (error)
257 return (error);
258
259 /* disable split funnels now */
260 thread_funnel_merge(kernel_flock, network_flock);
261
262 if ((nmp->nm_flag & NFSMNT_MNTD) &&
263 (uap->flag & NFSSVC_GOTAUTH) == 0)
264 return (0);
265 nmp->nm_flag |= NFSMNT_MNTD;
266 error = nqnfs_clientd(nmp, p->p_ucred, &ncd, uap->flag,
267 uap->argp, p);
268 } else if (uap->flag & NFSSVC_ADDSOCK) {
269 error = copyin(uap->argp, (caddr_t)&nfsdarg, sizeof(nfsdarg));
270 if (error)
271 return (error);
272 error = getsock(p->p_fd, nfsdarg.sock, &fp);
273 if (error)
274 return (error);
275 /*
276 * Get the client address for connected sockets.
277 */
278 if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
279 nam = (struct mbuf *)0;
280 else {
281 error = sockargs(&nam, nfsdarg.name, nfsdarg.namelen,
282 MT_SONAME);
283 if (error)
284 return (error);
285 }
286 error = nfssvc_addsock(fp, nam, p);
287 } else {
288 error = copyin(uap->argp, (caddr_t)nsd, sizeof (*nsd));
289 if (error)
290 return (error);
291
292 /* disable split funnels now */
293 thread_funnel_merge(kernel_flock, network_flock);
294
295 if ((uap->flag & NFSSVC_AUTHIN) && ((nfsd = nsd->nsd_nfsd)) &&
296 (nfsd->nfsd_slp->ns_flag & SLP_VALID)) {
297 slp = nfsd->nfsd_slp;
298
299 /*
300 * First check to see if another nfsd has already
301 * added this credential.
302 */
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)))
309 break;
310 }
311 if (nuidp) {
312 nfsrv_setcred(&nuidp->nu_cr,&nfsd->nfsd_nd->nd_cr);
313 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
314 } else {
315 /*
316 * Nope, so we will.
317 */
318 if (slp->ns_numuids < nuidhash_max) {
319 slp->ns_numuids++;
320 nuidp = (struct nfsuid *)
321 _MALLOC_ZONE(sizeof (struct nfsuid),
322 M_NFSUID, M_WAITOK);
323 } else
324 nuidp = (struct nfsuid *)0;
325 if ((slp->ns_flag & SLP_VALID) == 0) {
326 if (nuidp)
327 _FREE_ZONE((caddr_t)nuidp,
328 sizeof (struct nfsuid), M_NFSUID);
329 } else {
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,
334 nu_lru);
335 if (nuidp->nu_flag & NU_NAM)
336 m_freem(nuidp->nu_nam);
337 }
338 nuidp->nu_flag = 0;
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;
345 /*
346 * and save the session key in nu_key.
347 */
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;
352
353 saddr = mtod(nfsd->nfsd_nd->nd_nam2,
354 struct sockaddr_in *);
355 switch (saddr->sin_family) {
356 case AF_INET:
357 nuidp->nu_flag |= NU_INETADDR;
358 nuidp->nu_inetaddr =
359 saddr->sin_addr.s_addr;
360 break;
361 case AF_ISO:
362 default:
363 nuidp->nu_flag |= NU_NAM;
364 nuidp->nu_nam = m_copym(
365 nfsd->nfsd_nd->nd_nam2, 0,
366 M_COPYALL, M_WAIT);
367 break;
368 };
369 }
370 TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp,
371 nu_lru);
372 LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid),
373 nuidp, nu_hash);
374 nfsrv_setcred(&nuidp->nu_cr,
375 &nfsd->nfsd_nd->nd_cr);
376 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
377 }
378 }
379 }
380 if ((uap->flag & NFSSVC_AUTHINFAIL) && (nfsd = nsd->nsd_nfsd))
381 nfsd->nfsd_flag |= NFSD_AUTHFAIL;
382 error = nfssvc_nfsd(nsd, uap->argp, p);
383 }
384 #endif /* NFS_NOSERVER */
385 if (error == EINTR || error == ERESTART)
386 error = 0;
387 return (error);
388 }
389
390 #ifndef NFS_NOSERVER
391 /*
392 * Adds a socket to the list for servicing by nfsds.
393 */
394 static int
395 nfssvc_addsock(fp, mynam, p)
396 struct file *fp;
397 struct mbuf *mynam;
398 struct proc *p;
399 {
400 register struct mbuf *m;
401 register int siz;
402 register struct nfssvc_sock *slp;
403 register struct socket *so;
404 struct nfssvc_sock *tslp;
405 int error, s;
406
407 so = (struct socket *)fp->f_data;
408 tslp = (struct nfssvc_sock *)0;
409 /*
410 * Add it to the list, as required.
411 */
412 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
413 if (so->so_proto->pr_protocol == IPPROTO_UDP) {
414 tslp = nfs_udpsock;
415 if (tslp->ns_flag & SLP_VALID) {
416 m_freem(mynam);
417 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
418 return (EPERM);
419 }
420 #if ISO
421 } else if (so->so_proto->pr_protocol == ISOPROTO_CLTP) {
422 tslp = nfs_cltpsock;
423 if (tslp->ns_flag & SLP_VALID) {
424 m_freem(mynam);
425 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
426 return (EPERM);
427 }
428 #endif /* ISO */
429 }
430 if (so->so_type == SOCK_STREAM)
431 siz = NFS_MAXPACKET + sizeof (u_long);
432 else
433 siz = NFS_MAXPACKET;
434 error = soreserve(so, siz, siz);
435 if (error) {
436 m_freem(mynam);
437 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
438 return (error);
439 }
440
441 /*
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.
445 */
446 if (so->so_type == SOCK_STREAM) {
447 struct sockopt sopt;
448 int val;
449
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;
455 val = 1;
456 sosetopt(so, &sopt);
457 }
458 if (so->so_proto->pr_domain->dom_family == AF_INET &&
459 so->so_proto->pr_protocol == IPPROTO_TCP) {
460 struct sockopt sopt;
461 int val;
462
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;
468 val = 1;
469 sosetopt(so, &sopt);
470 }
471
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);
477 if (tslp)
478 slp = tslp;
479 else {
480 MALLOC(slp, struct nfssvc_sock *, sizeof(struct nfssvc_sock),
481 M_NFSSVC, M_WAITOK);
482 bzero((caddr_t)slp, sizeof (struct nfssvc_sock));
483 TAILQ_INIT(&slp->ns_uidlruhead);
484 TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
485 }
486 slp->ns_so = so;
487 slp->ns_nam = mynam;
488 slp->ns_fp = fp;
489 (void)fref(fp);
490 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
491 s = splnet();
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);
497 nfsrv_wakenfsd(slp);
498 splx(s);
499 return (0);
500 }
501
502 /*
503 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
504 * until it is killed by a signal.
505 */
506 static int
507 nfssvc_nfsd(nsd, argp, p)
508 struct nfsd_srvargs *nsd;
509 caddr_t argp;
510 struct proc *p;
511 {
512 register struct mbuf *m;
513 register int siz;
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;
519 struct mbuf *mreq;
520 int error = 0, cacherep, s, sotype, writes_todo;
521 int procrastinate;
522 u_quad_t cur_usec;
523 extern void nfs_aio_thread_init();
524
525 #ifndef nolint
526 cacherep = RC_DOIT;
527 writes_todo = 0;
528 #endif
529 s = splnet();
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);
536 nfs_numnfsd++;
537 nfs_aio_thread_init();
538 }
539 /*
540 * Loop getting rpc requests until SIGKILL.
541 */
542 for (;;) {
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;
547 nfsd_waiting++;
548 error = tsleep((caddr_t)nfsd, PSOCK | PCATCH,
549 "nfsd", 0);
550 nfsd_waiting--;
551 if (error)
552 goto done;
553 }
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;
561 slp->ns_sref++;
562 nfsd->nfsd_slp = slp;
563 break;
564 }
565 }
566 if (slp == 0)
567 nfsd_head_flag &= ~NFSD_CHECKSLP;
568 }
569 if ((slp = nfsd->nfsd_slp) == (struct nfssvc_sock *)0)
570 continue;
571 if (slp->ns_flag & SLP_VALID) {
572 if (slp->ns_flag & SLP_DISCONN)
573 nfsrv_zapsock(slp);
574 else if (slp->ns_flag & SLP_NEEDQ) {
575 slp->ns_flag &= ~SLP_NEEDQ;
576 (void) nfs_sndlock(&slp->ns_solock,
577 (struct nfsreq *)0);
578 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
579 nfsrv_rcv(slp->ns_so, (caddr_t)slp,
580 M_WAIT);
581 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
582 nfs_sndunlock(&slp->ns_solock);
583 }
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) {
589 error = 0;
590 cacherep = RC_DOIT;
591 writes_todo = 1;
592 } else
593 writes_todo = 0;
594 nfsd->nfsd_flag |= NFSD_REQINPROG;
595 }
596 } else {
597 error = 0;
598 slp = nfsd->nfsd_slp;
599 }
600 if (error || (slp->ns_flag & SLP_VALID) == 0) {
601 if (nd) {
602 _FREE_ZONE((caddr_t)nd,
603 sizeof *nd, M_NFSRVDESC);
604 nd = NULL;
605 }
606 nfsd->nfsd_slp = (struct nfssvc_sock *)0;
607 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
608 nfsrv_slpderef(slp);
609 continue;
610 }
611 splx(s);
612 so = slp->ns_so;
613 sotype = so->so_type;
614 if (so->so_proto->pr_flags & PR_CONNREQUIRED)
615 solockp = &slp->ns_solock;
616 else
617 solockp = (int *)0;
618 if (nd) {
619 nd->nd_starttime = time;
620 if (nd->nd_nam2)
621 nd->nd_nam = nd->nd_nam2;
622 else
623 nd->nd_nam = slp->ns_nam;
624
625 /*
626 * Check to see if authorization is needed.
627 */
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)))
639 return (ENEEDAUTH);
640 cacherep = RC_DROPIT;
641 } else
642 cacherep = nfsrv_getcache(nd, slp, &mreq);
643
644 /*
645 * Check for just starting up for NQNFS and send
646 * fake "try again later" replies to the NQNFS clients.
647 */
648 if (notstarted && nqnfsstarttime <= time.tv_sec) {
649 if (modify_flag) {
650 nqnfsstarttime = time.tv_sec + nqsrv_writeslack;
651 modify_flag = 0;
652 } else
653 notstarted = 0;
654 }
655 if (notstarted) {
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;
661 cacherep = RC_DOIT;
662 } else
663 modify_flag = 1;
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);
668 cacherep = RC_DOIT;
669 } else if (nfs_privport) {
670 /* Check if source port is privileged */
671 u_short port;
672 struct sockaddr *nam = nd->nd_nam;
673 struct sockaddr_in *sin;
674
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);
681 cacherep = RC_DOIT;
682 printf("NFS request from unprivileged port (%s:%d)\n",
683 (char *)(inet_ntoa(sin->sin_addr)), port);
684 }
685 }
686
687 }
688
689 /*
690 * Loop to get all the write rpc relies that have been
691 * gathered together.
692 */
693 do {
694 switch (cacherep) {
695 case RC_DOIT:
696 if (nd && (nd->nd_flag & ND_NFSV3))
697 procrastinate = nfsrvw_procrastinate_v3;
698 else
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);
704 else
705 error = (*(nfsrv3_procs[nd->nd_procnum]))(nd,
706 slp, nfsd->nfsd_procp, &mreq);
707 if (mreq == NULL)
708 break;
709 if (error) {
710 if (nd->nd_procnum != NQNFSPROC_VACATED)
711 nfsstats.srv_errs++;
712 nfsrv_updatecache(nd, FALSE, mreq);
713 if (nd->nd_nam2)
714 m_freem(nd->nd_nam2);
715 break;
716 }
717 nfsstats.srvrpccnt[nd->nd_procnum]++;
718 nfsrv_updatecache(nd, TRUE, mreq);
719 nd->nd_mrep = (struct mbuf *)0;
720 case RC_REPLY:
721 m = mreq;
722 siz = 0;
723 while (m) {
724 siz += m->m_len;
725 m = m->m_next;
726 }
727 if (siz <= 0 || siz > NFS_MAXPACKET) {
728 printf("mbuf siz=%d\n",siz);
729 panic("Bad nfs svc reply");
730 }
731 m = mreq;
732 m->m_pkthdr.len = siz;
733 m->m_pkthdr.rcvif = (struct ifnet *)0;
734 /*
735 * For stream protocols, prepend a Sun RPC
736 * Record Mark.
737 */
738 if (sotype == SOCK_STREAM) {
739 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
740 *mtod(m, u_long *) = htonl(0x80000000 | siz);
741 }
742 if (solockp)
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);
746 else {
747 error = EPIPE;
748 m_freem(m);
749 }
750 if (nfsrtton)
751 nfsd_rt(sotype, nd, cacherep);
752 if (nd->nd_nam2)
753 MFREE(nd->nd_nam2, m);
754 if (nd->nd_mrep)
755 m_freem(nd->nd_mrep);
756 if (error == EPIPE)
757 nfsrv_zapsock(slp);
758 if (solockp)
759 nfs_sndunlock(solockp);
760 if (error == EINTR || error == ERESTART) {
761 _FREE_ZONE((caddr_t)nd,
762 sizeof *nd, M_NFSRVDESC);
763 nfsrv_slpderef(slp);
764 s = splnet();
765 goto done;
766 }
767 break;
768 case RC_DROPIT:
769 if (nfsrtton)
770 nfsd_rt(sotype, nd, cacherep);
771 m_freem(nd->nd_mrep);
772 m_freem(nd->nd_nam2);
773 break;
774 };
775 if (nd) {
776 FREE_ZONE((caddr_t)nd, sizeof *nd, M_NFSRVDESC);
777 nd = NULL;
778 }
779
780 /*
781 * Check to see if there are outstanding writes that
782 * need to be serviced.
783 */
784 cur_usec = (u_quad_t)time.tv_sec * 1000000 +
785 (u_quad_t)time.tv_usec;
786 s = splsoftclock();
787 if (slp->ns_tq.lh_first &&
788 slp->ns_tq.lh_first->nd_time <= cur_usec) {
789 cacherep = RC_DOIT;
790 writes_todo = 1;
791 } else
792 writes_todo = 0;
793 splx(s);
794 } while (writes_todo);
795 s = splnet();
796 if (nfsrv_dorec(slp, nfsd, &nd)) {
797 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
798 nfsd->nfsd_slp = NULL;
799 nfsrv_slpderef(slp);
800 }
801 }
802 done:
803 TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
804 splx(s);
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 */
809 return (error);
810 }
811 #endif /* NFS_NOSERVER */
812
813 int nfs_defect = 0;
814 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
815 #ifdef notyet
816 SYSCTL_INT(_vfs_nfs, OID_AUTO, defect, CTLFLAG_RW, &nfs_defect, 0, "");
817 #endif
818
819 static int nfssvc_iod_continue(int);
820
821 /*
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.
825 */
826 static int
827 nfssvc_iod(p)
828 struct proc *p;
829 {
830 register struct buf *bp;
831 register int i, myiod;
832 struct nfsmount *nmp;
833 int error = 0;
834 struct uthread *ut;
835
836 /*
837 * Assign my position or return error if too many already running
838 */
839 myiod = -1;
840 for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
841 if (nfs_asyncdaemon[i] == 0) {
842 nfs_asyncdaemon[i]++;
843 myiod = i;
844 break;
845 }
846 if (myiod == -1)
847 return (EBUSY);
848 nfs_numasync++;
849
850 /* stuff myiod into uthread to get off local stack for
851 continuation */
852
853 ut = get_bsdthread_info(current_act());
854 ut->uu_state.uu_nfs_myiod = myiod; /* squirrel away for continuation */
855
856 nfssvc_iod_continue(0);
857 /* NOTREACHED */
858
859 }
860
861 /*
862 * Continuation for Asynchronous I/O daemons for client nfs.
863 */
864 static int
865 nfssvc_iod_continue(error)
866 {
867 register struct buf *bp;
868 register int i, myiod;
869 struct nfsmount *nmp;
870 struct uthread *ut;
871 struct proc *p;
872
873 /*
874 * real myiod is stored in uthread, recover it
875 */
876 ut = get_bsdthread_info(current_act());
877 myiod = ut->uu_state.uu_nfs_myiod;
878 p = get_bsdtask_info(current_task());
879
880 /*
881 * Just loop around doin our stuff until SIGKILL
882 * - actually we don't loop with continuations...
883 */
884 for (;;) {
885 while (((nmp = nfs_iodmount[myiod]) == NULL
886 || nmp->nm_bufq.tqh_first == NULL)
887 && error == 0) {
888 if (nmp)
889 nmp->nm_bufqiods--;
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);
894 /* NOTREACHED */
895 }
896 if (error) {
897 nfs_asyncdaemon[myiod] = 0;
898 if (nmp) nmp->nm_bufqiods--;
899 nfs_iodwant[myiod] = NULL;
900 nfs_iodmount[myiod] = NULL;
901 nfs_numasync--;
902 if (error == EINTR || error == ERESTART)
903 error = 0;
904 #if defined (__i386__)
905 return(error);
906 #else
907 unix_syscall_return(error);
908 #endif
909 }
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);
913 nmp->nm_bufqlen--;
914 if (nmp->nm_bufqwant && nmp->nm_bufqlen < 2 * nfs_numasync) {
915 nmp->nm_bufqwant = FALSE;
916 wakeup(&nmp->nm_bufq);
917 }
918 if (ISSET(bp->b_flags, B_READ))
919 (void) nfs_doio(bp, bp->b_rcred, (struct proc *)0);
920 else
921 (void) nfs_doio(bp, bp->b_wcred, (struct proc *)0);
922
923 /*
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
926 */
927 if (nfs_defect && nmp->nm_bufqiods > 1) {
928 NFS_DPF(ASYNCIO,
929 ("nfssvc_iod: iod %d defecting from mount %p\n",
930 myiod, nmp));
931 nfs_iodmount[myiod] = NULL;
932 nmp->nm_bufqiods--;
933 break;
934 }
935 }
936 }
937 }
938
939 /*
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.
945 */
946 static void
947 nfsrv_zapsock(slp)
948 register struct nfssvc_sock *slp;
949 {
950 register struct nfsuid *nuidp, *nnuidp;
951 register struct nfsrv_descript *nwp, *nnwp;
952 struct socket *so;
953 struct file *fp;
954 struct mbuf *m;
955 int s;
956
957 slp->ns_flag &= ~SLP_ALLFLAGS;
958 fp = slp->ns_fp;
959 if (fp) {
960 slp->ns_fp = (struct file *)0;
961 so = slp->ns_so;
962 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
963 so->so_upcall = NULL;
964 so->so_rcv.sb_flags &= ~SB_UPCALL;
965 soshutdown(so, 2);
966 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
967 closef(fp, (struct proc *)0);
968 if (slp->ns_nam)
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;
973 nuidp = nnuidp) {
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);
981 }
982 s = splsoftclock();
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);
987 }
988 LIST_INIT(&slp->ns_tq);
989 splx(s);
990 }
991 }
992
993 /*
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.
996 */
997 int
998 nfs_getauth(nmp, rep, cred, auth_str, auth_len, verf_str, verf_len, key)
999 register struct nfsmount *nmp;
1000 struct nfsreq *rep;
1001 struct ucred *cred;
1002 char **auth_str;
1003 int *auth_len;
1004 char *verf_str;
1005 int *verf_len;
1006 NFSKERBKEY_T key; /* return session key */
1007 {
1008 int error = 0;
1009
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);
1015 if (error) {
1016 nmp->nm_flag &= ~NFSMNT_WANTAUTH;
1017 return (error);
1018 }
1019 }
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);
1028
1029 /*
1030 * And wait for mount_nfs to do its stuff.
1031 */
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);
1036 }
1037 if (nmp->nm_flag & NFSMNT_AUTHERR) {
1038 nmp->nm_flag &= ~NFSMNT_AUTHERR;
1039 error = EAUTH;
1040 }
1041 if (error)
1042 _FREE((caddr_t)*auth_str, M_TEMP);
1043 else {
1044 *auth_len = nmp->nm_authlen;
1045 *verf_len = nmp->nm_verflen;
1046 bcopy((caddr_t)nmp->nm_key, (caddr_t)key, sizeof (key));
1047 }
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);
1053 }
1054 return (error);
1055 }
1056
1057 /*
1058 * Get a nickname authenticator and verifier.
1059 */
1060 int
1061 nfs_getnickauth(nmp, cred, auth_str, auth_len, verf_str, verf_len)
1062 struct nfsmount *nmp;
1063 struct ucred *cred;
1064 char **auth_str;
1065 int *auth_len;
1066 char *verf_str;
1067 int verf_len;
1068 {
1069 register struct nfsuid *nuidp;
1070 register u_long *nickp, *verfp;
1071 struct timeval ktvin, ktvout;
1072
1073 #if DIAGNOSTIC
1074 if (verf_len < (4 * NFSX_UNSIGNED))
1075 panic("nfs_getnickauth verf too small");
1076 #endif
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)
1080 break;
1081 }
1082 if (!nuidp || nuidp->nu_expire < time.tv_sec)
1083 return (EACCES);
1084
1085 /*
1086 * Move to the end of the lru list (end of lru == most recently used).
1087 */
1088 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
1089 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru);
1090
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;
1096
1097 /*
1098 * Now we must encrypt the verifier and package it up.
1099 */
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;
1106 else
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);
1110
1111 /*
1112 * Now encrypt the timestamp verifier in ecb mode using the session
1113 * key.
1114 */
1115 #if NFSKERB
1116 XXX
1117 #endif
1118
1119 *verfp++ = ktvout.tv_sec;
1120 *verfp++ = ktvout.tv_usec;
1121 *verfp = 0;
1122 return (0);
1123 }
1124
1125 /*
1126 * Save the current nickname in a hash list entry on the mount point.
1127 */
1128 int
1129 nfs_savenickauth(nmp, cred, len, key, mdp, dposp, mrep)
1130 register struct nfsmount *nmp;
1131 struct ucred *cred;
1132 int len;
1133 NFSKERBKEY_T key;
1134 struct mbuf **mdp;
1135 char **dposp;
1136 struct mbuf *mrep;
1137 {
1138 register struct nfsuid *nuidp;
1139 register u_long *tl;
1140 register long t1;
1141 struct mbuf *md = *mdp;
1142 struct timeval ktvin, ktvout;
1143 u_long nick;
1144 char *dpos = *dposp, *cp2;
1145 int deltasec, error = 0;
1146
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);
1152
1153 /*
1154 * Decrypt the timestamp in ecb mode.
1155 */
1156 #if NFSKERB
1157 XXX
1158 #endif
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;
1162 if (deltasec < 0)
1163 deltasec = -deltasec;
1164 /*
1165 * If ok, add it to the hash list for the mount point.
1166 */
1167 if (deltasec <= NFS_KERBCLOCKSKEW) {
1168 if (nmp->nm_numuids < nuidhash_max) {
1169 nmp->nm_numuids++;
1170 MALLOC_ZONE(nuidp, struct nfsuid *,
1171 sizeof (struct nfsuid),
1172 M_NFSUID, M_WAITOK);
1173 } else {
1174 nuidp = nmp->nm_uidlruhead.tqh_first;
1175 LIST_REMOVE(nuidp, nu_hash);
1176 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp,
1177 nu_lru);
1178 }
1179 nuidp->nu_flag = 0;
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,
1186 nu_lru);
1187 LIST_INSERT_HEAD(NMUIDHASH(nmp, cred->cr_uid),
1188 nuidp, nu_hash);
1189 }
1190 } else
1191 nfsm_adv(nfsm_rndup(len));
1192 nfsmout:
1193 *mdp = md;
1194 *dposp = dpos;
1195 return (error);
1196 }
1197
1198 #ifndef NFS_NOSERVER
1199
1200 /*
1201 * Derefence a server socket structure. If it has no more references and
1202 * is no longer valid, you can throw it away.
1203 */
1204 void
1205 nfsrv_slpderef(slp)
1206 register struct nfssvc_sock *slp;
1207 {
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);
1211 }
1212 }
1213
1214 /*
1215 * Initialize the data structures for the server.
1216 * Handshake with any new nfsds starting up to avoid any chance of
1217 * corruption.
1218 */
1219 void
1220 nfsrv_init(terminating)
1221 int terminating;
1222 {
1223 register struct nfssvc_sock *slp, *nslp;
1224
1225 if (nfssvc_sockhead_flag & SLP_INIT)
1226 panic("nfsd init");
1227 nfssvc_sockhead_flag |= SLP_INIT;
1228 if (terminating) {
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)
1232 nfsrv_zapsock(slp);
1233 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
1234 _FREE((caddr_t)slp, M_NFSSVC);
1235 }
1236 nfsrv_cleancache(); /* And clear out server cache */
1237 /* XXX CSM 12/4/97 Revisit when enabling WebNFS */
1238 #ifdef notyet
1239 } else
1240 nfs_pub.np_valid = 0;
1241 #else
1242 }
1243 #endif
1244
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);
1250 }
1251
1252 TAILQ_INIT(&nfsd_head);
1253 nfsd_head_flag &= ~NFSD_CHECKSLP;
1254
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);
1260
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);
1266 }
1267
1268 /*
1269 * Add entries to the server monitor log.
1270 */
1271 static void
1272 nfsd_rt(sotype, nd, cacherep)
1273 int sotype;
1274 register struct nfsrv_descript *nd;
1275 int cacherep;
1276 {
1277 register struct drt *rt;
1278
1279 rt = &nfsdrt.drt[nfsdrt.pos];
1280 if (cacherep == RC_DOIT)
1281 rt->flag = 0;
1282 else if (cacherep == RC_REPLY)
1283 rt->flag = DRT_CACHEREPLY;
1284 else
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;
1295 else
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);
1299 rt->tstamp = time;
1300 nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
1301 }
1302 #endif /* NFS_NOSERVER */