nfs_syscalls.c revision 1.30 1 /* $NetBSD: nfs_syscalls.c,v 1.30 1998/06/25 22:18:10 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)nfs_syscalls.c 8.5 (Berkeley) 3/30/95
39 */
40
41 #include "fs_nfs.h"
42 #include "opt_nfsserver.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/file.h>
48 #include <sys/stat.h>
49 #include <sys/vnode.h>
50 #include <sys/mount.h>
51 #include <sys/proc.h>
52 #include <sys/uio.h>
53 #include <sys/malloc.h>
54 #include <sys/buf.h>
55 #include <sys/mbuf.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
58 #include <sys/domain.h>
59 #include <sys/protosw.h>
60 #include <sys/namei.h>
61 #include <sys/syslog.h>
62 #include <sys/filedesc.h>
63
64 #include <sys/syscallargs.h>
65
66 #include <netinet/in.h>
67 #include <netinet/tcp.h>
68 #ifdef ISO
69 #include <netiso/iso.h>
70 #endif
71 #include <nfs/xdr_subs.h>
72 #include <nfs/rpcv2.h>
73 #include <nfs/nfsproto.h>
74 #include <nfs/nfs.h>
75 #include <nfs/nfsm_subs.h>
76 #include <nfs/nfsrvcache.h>
77 #include <nfs/nfsmount.h>
78 #include <nfs/nfsnode.h>
79 #include <nfs/nqnfs.h>
80 #include <nfs/nfsrtt.h>
81 #include <nfs/nfs_var.h>
82
83 void nfsrv_zapsock __P((struct nfssvc_sock *));
84
85 /* Global defs. */
86 extern int32_t (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *,
87 struct nfssvc_sock *,
88 struct proc *, struct mbuf **));
89 extern int nfs_numasync;
90 extern time_t nqnfsstarttime;
91 extern int nqsrv_writeslack;
92 extern int nfsrtton;
93 extern struct nfsstats nfsstats;
94 extern int nfsrvw_procrastinate;
95 struct nfssvc_sock *nfs_udpsock, *nfs_cltpsock;
96 int nuidhash_max = NFS_MAXUIDHASH;
97 int nfsd_waiting = 0;
98 #ifdef NFSSERVER
99 static int nfs_numnfsd = 0;
100 static int notstarted = 1;
101 static int modify_flag = 0;
102 static struct nfsdrt nfsdrt;
103 extern struct nfs_public nfs_pub;
104 #endif
105
106 #define TRUE 1
107 #define FALSE 0
108
109 #ifdef NFS
110 static int nfs_asyncdaemon[NFS_MAXASYNCDAEMON];
111 #endif
112
113 #ifdef NFSSERVER
114 static void nfsd_rt __P((int, struct nfsrv_descript *, int));
115 #endif
116
117 /*
118 * NFS server system calls
119 * getfh() lives here too, but maybe should move to kern/vfs_syscalls.c
120 */
121
122 /*
123 * Get file handle system call
124 */
125 int
126 sys_getfh(p, v, retval)
127 struct proc *p;
128 register void *v;
129 register_t *retval;
130 {
131 register struct sys_getfh_args /* {
132 syscallarg(char *) fname;
133 syscallarg(fhandle_t *) fhp;
134 } */ *uap = v;
135 register struct vnode *vp;
136 fhandle_t fh;
137 int error;
138 struct nameidata nd;
139
140 /*
141 * Must be super user
142 */
143 error = suser(p->p_ucred, &p->p_acflag);
144 if (error)
145 return (error);
146 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
147 SCARG(uap, fname), p);
148 error = namei(&nd);
149 if (error)
150 return (error);
151 vp = nd.ni_vp;
152 bzero((caddr_t)&fh, sizeof(fh));
153 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
154 error = VFS_VPTOFH(vp, &fh.fh_fid);
155 vput(vp);
156 if (error)
157 return (error);
158 error = copyout((caddr_t)&fh, (caddr_t)SCARG(uap, fhp), sizeof (fh));
159 return (error);
160 }
161
162 /*
163 * Nfs server pseudo system call for the nfsd's
164 * Based on the flag value it either:
165 * - adds a socket to the selection list
166 * - remains in the kernel as an nfsd
167 * - remains in the kernel as an nfsiod
168 */
169 int
170 sys_nfssvc(p, v, retval)
171 struct proc *p;
172 void *v;
173 register_t *retval;
174 {
175 register struct sys_nfssvc_args /* {
176 syscallarg(int) flag;
177 syscallarg(caddr_t) argp;
178 } */ *uap = v;
179 int error;
180 #ifdef NFS
181 struct nameidata nd;
182 struct nfsmount *nmp;
183 struct nfsd_cargs ncd;
184 #endif
185 #ifdef NFSSERVER
186 struct file *fp;
187 struct mbuf *nam;
188 struct nfsd_args nfsdarg;
189 struct nfsd_srvargs nfsd_srvargs, *nsd = &nfsd_srvargs;
190 struct nfsd *nfsd;
191 struct nfssvc_sock *slp;
192 struct nfsuid *nuidp;
193 #endif
194
195 /*
196 * Must be super user
197 */
198 error = suser(p->p_ucred, &p->p_acflag);
199 if(error)
200 return (error);
201 while (nfssvc_sockhead_flag & SLP_INIT) {
202 nfssvc_sockhead_flag |= SLP_WANTINIT;
203 (void) tsleep((caddr_t)&nfssvc_sockhead, PSOCK, "nfsd init", 0);
204 }
205 if (SCARG(uap, flag) & NFSSVC_BIOD) {
206 #ifdef NFS
207 error = nfssvc_iod(p);
208 #else
209 error = ENOSYS;
210 #endif
211 } else if (SCARG(uap, flag) & NFSSVC_MNTD) {
212 #ifndef NFS
213 error = ENOSYS;
214 #else
215 error = copyin(SCARG(uap, argp), (caddr_t)&ncd, sizeof (ncd));
216 if (error)
217 return (error);
218 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
219 ncd.ncd_dirp, p);
220 error = namei(&nd);
221 if (error)
222 return (error);
223 if ((nd.ni_vp->v_flag & VROOT) == 0)
224 error = EINVAL;
225 nmp = VFSTONFS(nd.ni_vp->v_mount);
226 vput(nd.ni_vp);
227 if (error)
228 return (error);
229 if ((nmp->nm_iflag & NFSMNT_MNTD) &&
230 (SCARG(uap, flag) & NFSSVC_GOTAUTH) == 0)
231 return (0);
232 nmp->nm_iflag |= NFSMNT_MNTD;
233 error = nqnfs_clientd(nmp, p->p_ucred, &ncd, SCARG(uap, flag),
234 SCARG(uap, argp), p);
235 #endif /* NFS */
236 } else if (SCARG(uap, flag) & NFSSVC_ADDSOCK) {
237 #ifndef NFSSERVER
238 error = ENOSYS;
239 #else
240 error = copyin(SCARG(uap, argp), (caddr_t)&nfsdarg,
241 sizeof(nfsdarg));
242 if (error)
243 return (error);
244 error = getsock(p->p_fd, nfsdarg.sock, &fp);
245 if (error)
246 return (error);
247 /*
248 * Get the client address for connected sockets.
249 */
250 if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
251 nam = (struct mbuf *)0;
252 else {
253 error = sockargs(&nam, nfsdarg.name, nfsdarg.namelen,
254 MT_SONAME);
255 if (error)
256 return (error);
257 }
258 error = nfssvc_addsock(fp, nam);
259 #endif /* !NFSSERVER */
260 } else {
261 #ifndef NFSSERVER
262 error = ENOSYS;
263 #else
264 error = copyin(SCARG(uap, argp), (caddr_t)nsd, sizeof (*nsd));
265 if (error)
266 return (error);
267 if ((SCARG(uap, flag) & NFSSVC_AUTHIN) &&
268 ((nfsd = nsd->nsd_nfsd)) != NULL &&
269 (nfsd->nfsd_slp->ns_flag & SLP_VALID)) {
270 slp = nfsd->nfsd_slp;
271
272 /*
273 * First check to see if another nfsd has already
274 * added this credential.
275 */
276 for (nuidp = NUIDHASH(slp,nsd->nsd_cr.cr_uid)->lh_first;
277 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
278 if (nuidp->nu_cr.cr_uid == nsd->nsd_cr.cr_uid &&
279 (!nfsd->nfsd_nd->nd_nam2 ||
280 netaddr_match(NU_NETFAM(nuidp),
281 &nuidp->nu_haddr, nfsd->nfsd_nd->nd_nam2)))
282 break;
283 }
284 if (nuidp) {
285 nfsrv_setcred(&nuidp->nu_cr,&nfsd->nfsd_nd->nd_cr);
286 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
287 } else {
288 /*
289 * Nope, so we will.
290 */
291 if (slp->ns_numuids < nuidhash_max) {
292 slp->ns_numuids++;
293 nuidp = (struct nfsuid *)
294 malloc(sizeof (struct nfsuid), M_NFSUID,
295 M_WAITOK);
296 } else
297 nuidp = (struct nfsuid *)0;
298 if ((slp->ns_flag & SLP_VALID) == 0) {
299 if (nuidp)
300 free((caddr_t)nuidp, M_NFSUID);
301 } else {
302 if (nuidp == (struct nfsuid *)0) {
303 nuidp = slp->ns_uidlruhead.tqh_first;
304 LIST_REMOVE(nuidp, nu_hash);
305 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp,
306 nu_lru);
307 if (nuidp->nu_flag & NU_NAM)
308 m_freem(nuidp->nu_nam);
309 }
310 nuidp->nu_flag = 0;
311 nuidp->nu_cr = nsd->nsd_cr;
312 if (nuidp->nu_cr.cr_ngroups > NGROUPS)
313 nuidp->nu_cr.cr_ngroups = NGROUPS;
314 nuidp->nu_cr.cr_ref = 1;
315 nuidp->nu_timestamp = nsd->nsd_timestamp;
316 nuidp->nu_expire = time.tv_sec + nsd->nsd_ttl;
317 /*
318 * and save the session key in nu_key.
319 */
320 bcopy(nsd->nsd_key, nuidp->nu_key,
321 sizeof (nsd->nsd_key));
322 if (nfsd->nfsd_nd->nd_nam2) {
323 struct sockaddr_in *saddr;
324
325 saddr = mtod(nfsd->nfsd_nd->nd_nam2,
326 struct sockaddr_in *);
327 switch (saddr->sin_family) {
328 case AF_INET:
329 nuidp->nu_flag |= NU_INETADDR;
330 nuidp->nu_inetaddr =
331 saddr->sin_addr.s_addr;
332 break;
333 case AF_ISO:
334 default:
335 nuidp->nu_flag |= NU_NAM;
336 nuidp->nu_nam = m_copym(
337 nfsd->nfsd_nd->nd_nam2, 0,
338 M_COPYALL, M_WAIT);
339 break;
340 };
341 }
342 TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp,
343 nu_lru);
344 LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid),
345 nuidp, nu_hash);
346 nfsrv_setcred(&nuidp->nu_cr,
347 &nfsd->nfsd_nd->nd_cr);
348 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
349 }
350 }
351 }
352 if ((SCARG(uap, flag) & NFSSVC_AUTHINFAIL) &&
353 (nfsd = nsd->nsd_nfsd))
354 nfsd->nfsd_flag |= NFSD_AUTHFAIL;
355 error = nfssvc_nfsd(nsd, SCARG(uap, argp), p);
356 #endif /* !NFSSERVER */
357 }
358 if (error == EINTR || error == ERESTART)
359 error = 0;
360 return (error);
361 }
362
363 #ifdef NFSSERVER
364 /*
365 * Adds a socket to the list for servicing by nfsds.
366 */
367 int
368 nfssvc_addsock(fp, mynam)
369 struct file *fp;
370 struct mbuf *mynam;
371 {
372 register struct mbuf *m;
373 register int siz;
374 register struct nfssvc_sock *slp;
375 register struct socket *so;
376 struct nfssvc_sock *tslp;
377 int error, s;
378
379 so = (struct socket *)fp->f_data;
380 tslp = (struct nfssvc_sock *)0;
381 /*
382 * Add it to the list, as required.
383 */
384 if (so->so_proto->pr_protocol == IPPROTO_UDP) {
385 tslp = nfs_udpsock;
386 if (tslp->ns_flag & SLP_VALID) {
387 m_freem(mynam);
388 return (EPERM);
389 }
390 #ifdef ISO
391 } else if (so->so_proto->pr_protocol == ISOPROTO_CLTP) {
392 tslp = nfs_cltpsock;
393 if (tslp->ns_flag & SLP_VALID) {
394 m_freem(mynam);
395 return (EPERM);
396 }
397 #endif /* ISO */
398 }
399 if (so->so_type == SOCK_STREAM)
400 siz = NFS_MAXPACKET + sizeof (u_long);
401 else
402 siz = NFS_MAXPACKET;
403 error = soreserve(so, siz, siz);
404 if (error) {
405 m_freem(mynam);
406 return (error);
407 }
408
409 /*
410 * Set protocol specific options { for now TCP only } and
411 * reserve some space. For datagram sockets, this can get called
412 * repeatedly for the same socket, but that isn't harmful.
413 */
414 if (so->so_type == SOCK_STREAM) {
415 MGET(m, M_WAIT, MT_SOOPTS);
416 *mtod(m, int32_t *) = 1;
417 m->m_len = sizeof(int32_t);
418 sosetopt(so, SOL_SOCKET, SO_KEEPALIVE, m);
419 }
420 if (so->so_proto->pr_domain->dom_family == AF_INET &&
421 so->so_proto->pr_protocol == IPPROTO_TCP) {
422 MGET(m, M_WAIT, MT_SOOPTS);
423 *mtod(m, int32_t *) = 1;
424 m->m_len = sizeof(int32_t);
425 sosetopt(so, IPPROTO_TCP, TCP_NODELAY, m);
426 }
427 so->so_rcv.sb_flags &= ~SB_NOINTR;
428 so->so_rcv.sb_timeo = 0;
429 so->so_snd.sb_flags &= ~SB_NOINTR;
430 so->so_snd.sb_timeo = 0;
431 if (tslp)
432 slp = tslp;
433 else {
434 slp = (struct nfssvc_sock *)
435 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
436 bzero((caddr_t)slp, sizeof (struct nfssvc_sock));
437 TAILQ_INIT(&slp->ns_uidlruhead);
438 TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
439 }
440 slp->ns_so = so;
441 slp->ns_nam = mynam;
442 fp->f_count++;
443 slp->ns_fp = fp;
444 s = splsoftnet();
445 so->so_upcallarg = (caddr_t)slp;
446 so->so_upcall = nfsrv_rcv;
447 so->so_rcv.sb_flags |= SB_UPCALL;
448 slp->ns_flag = (SLP_VALID | SLP_NEEDQ);
449 nfsrv_wakenfsd(slp);
450 splx(s);
451 return (0);
452 }
453
454 /*
455 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
456 * until it is killed by a signal.
457 */
458 int
459 nfssvc_nfsd(nsd, argp, p)
460 struct nfsd_srvargs *nsd;
461 caddr_t argp;
462 struct proc *p;
463 {
464 register struct mbuf *m;
465 register int siz;
466 register struct nfssvc_sock *slp;
467 register struct socket *so;
468 register int *solockp;
469 struct nfsd *nfsd = nsd->nsd_nfsd;
470 struct nfsrv_descript *nd = NULL;
471 struct mbuf *mreq;
472 int error = 0, cacherep, s, sotype, writes_todo;
473 u_quad_t cur_usec;
474
475 #ifndef nolint
476 cacherep = RC_DOIT;
477 writes_todo = 0;
478 #endif
479 s = splsoftnet();
480 if (nfsd == (struct nfsd *)0) {
481 nsd->nsd_nfsd = nfsd = (struct nfsd *)
482 malloc(sizeof (struct nfsd), M_NFSD, M_WAITOK);
483 bzero((caddr_t)nfsd, sizeof (struct nfsd));
484 nfsd->nfsd_procp = p;
485 TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
486 nfs_numnfsd++;
487 }
488 /*
489 * Loop getting rpc requests until SIGKILL.
490 */
491 for (;;) {
492 if ((nfsd->nfsd_flag & NFSD_REQINPROG) == 0) {
493 while (nfsd->nfsd_slp == (struct nfssvc_sock *)0 &&
494 (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
495 nfsd->nfsd_flag |= NFSD_WAITING;
496 nfsd_waiting++;
497 error = tsleep((caddr_t)nfsd, PSOCK | PCATCH,
498 "nfsd", 0);
499 nfsd_waiting--;
500 if (error)
501 goto done;
502 }
503 if (nfsd->nfsd_slp == (struct nfssvc_sock *)0 &&
504 (nfsd_head_flag & NFSD_CHECKSLP) != 0) {
505 for (slp = nfssvc_sockhead.tqh_first; slp != 0;
506 slp = slp->ns_chain.tqe_next) {
507 if ((slp->ns_flag & (SLP_VALID | SLP_DOREC))
508 == (SLP_VALID | SLP_DOREC)) {
509 slp->ns_flag &= ~SLP_DOREC;
510 slp->ns_sref++;
511 nfsd->nfsd_slp = slp;
512 break;
513 }
514 }
515 if (slp == 0)
516 nfsd_head_flag &= ~NFSD_CHECKSLP;
517 }
518 if ((slp = nfsd->nfsd_slp) == (struct nfssvc_sock *)0)
519 continue;
520 if (slp->ns_flag & SLP_VALID) {
521 if (slp->ns_flag & SLP_DISCONN)
522 nfsrv_zapsock(slp);
523 else if (slp->ns_flag & SLP_NEEDQ) {
524 slp->ns_flag &= ~SLP_NEEDQ;
525 (void) nfs_sndlock(&slp->ns_solock,
526 (struct nfsreq *)0);
527 nfsrv_rcv(slp->ns_so, (caddr_t)slp,
528 M_WAIT);
529 nfs_sndunlock(&slp->ns_solock);
530 }
531 error = nfsrv_dorec(slp, nfsd, &nd);
532 cur_usec = (u_quad_t)time.tv_sec * 1000000 +
533 (u_quad_t)time.tv_usec;
534 if (error && slp->ns_tq.lh_first &&
535 slp->ns_tq.lh_first->nd_time <= cur_usec) {
536 error = 0;
537 cacherep = RC_DOIT;
538 writes_todo = 1;
539 } else
540 writes_todo = 0;
541 nfsd->nfsd_flag |= NFSD_REQINPROG;
542 }
543 } else {
544 error = 0;
545 slp = nfsd->nfsd_slp;
546 }
547 if (error || (slp->ns_flag & SLP_VALID) == 0) {
548 if (nd) {
549 free((caddr_t)nd, M_NFSRVDESC);
550 nd = NULL;
551 }
552 nfsd->nfsd_slp = (struct nfssvc_sock *)0;
553 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
554 nfsrv_slpderef(slp);
555 continue;
556 }
557 splx(s);
558 so = slp->ns_so;
559 sotype = so->so_type;
560 if (so->so_proto->pr_flags & PR_CONNREQUIRED)
561 solockp = &slp->ns_solock;
562 else
563 solockp = (int *)0;
564 if (nd) {
565 nd->nd_starttime = time;
566 if (nd->nd_nam2)
567 nd->nd_nam = nd->nd_nam2;
568 else
569 nd->nd_nam = slp->ns_nam;
570
571 /*
572 * Check to see if authorization is needed.
573 */
574 if (nfsd->nfsd_flag & NFSD_NEEDAUTH) {
575 nfsd->nfsd_flag &= ~NFSD_NEEDAUTH;
576 nsd->nsd_haddr = mtod(nd->nd_nam,
577 struct sockaddr_in *)->sin_addr.s_addr;
578 nsd->nsd_authlen = nfsd->nfsd_authlen;
579 nsd->nsd_verflen = nfsd->nfsd_verflen;
580 if (!copyout(nfsd->nfsd_authstr,nsd->nsd_authstr,
581 nfsd->nfsd_authlen) &&
582 !copyout(nfsd->nfsd_verfstr, nsd->nsd_verfstr,
583 nfsd->nfsd_verflen) &&
584 !copyout((caddr_t)nsd, argp, sizeof (*nsd)))
585 return (ENEEDAUTH);
586 cacherep = RC_DROPIT;
587 } else
588 cacherep = nfsrv_getcache(nd, slp, &mreq);
589
590 /*
591 * Check for just starting up for NQNFS and send
592 * fake "try again later" replies to the NQNFS clients.
593 */
594 if (notstarted && nqnfsstarttime <= time.tv_sec) {
595 if (modify_flag) {
596 nqnfsstarttime = time.tv_sec + nqsrv_writeslack;
597 modify_flag = 0;
598 } else
599 notstarted = 0;
600 }
601 if (notstarted) {
602 if ((nd->nd_flag & ND_NQNFS) == 0)
603 cacherep = RC_DROPIT;
604 else if (nd->nd_procnum != NFSPROC_WRITE) {
605 nd->nd_procnum = NFSPROC_NOOP;
606 nd->nd_repstat = NQNFS_TRYLATER;
607 cacherep = RC_DOIT;
608 } else
609 modify_flag = 1;
610 } else if (nfsd->nfsd_flag & NFSD_AUTHFAIL) {
611 nfsd->nfsd_flag &= ~NFSD_AUTHFAIL;
612 nd->nd_procnum = NFSPROC_NOOP;
613 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
614 cacherep = RC_DOIT;
615 }
616 }
617
618 /*
619 * Loop to get all the write rpc relies that have been
620 * gathered together.
621 */
622 do {
623 switch (cacherep) {
624 case RC_DOIT:
625 if (writes_todo || (nd->nd_procnum == NFSPROC_WRITE &&
626 nfsrvw_procrastinate > 0 && !notstarted))
627 error = nfsrv_writegather(&nd, slp,
628 nfsd->nfsd_procp, &mreq);
629 else
630 error = (*(nfsrv3_procs[nd->nd_procnum]))(nd,
631 slp, nfsd->nfsd_procp, &mreq);
632 if (mreq == NULL)
633 break;
634 if (error) {
635 if (nd->nd_procnum != NQNFSPROC_VACATED)
636 nfsstats.srv_errs++;
637 nfsrv_updatecache(nd, FALSE, mreq);
638 if (nd->nd_nam2)
639 m_freem(nd->nd_nam2);
640 break;
641 }
642 nfsstats.srvrpccnt[nd->nd_procnum]++;
643 nfsrv_updatecache(nd, TRUE, mreq);
644 nd->nd_mrep = (struct mbuf *)0;
645 case RC_REPLY:
646 m = mreq;
647 siz = 0;
648 while (m) {
649 siz += m->m_len;
650 m = m->m_next;
651 }
652 if (siz <= 0 || siz > NFS_MAXPACKET) {
653 printf("mbuf siz=%d\n",siz);
654 panic("Bad nfs svc reply");
655 }
656 m = mreq;
657 m->m_pkthdr.len = siz;
658 m->m_pkthdr.rcvif = (struct ifnet *)0;
659 /*
660 * For stream protocols, prepend a Sun RPC
661 * Record Mark.
662 */
663 if (sotype == SOCK_STREAM) {
664 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
665 *mtod(m, u_int32_t *) = htonl(0x80000000 | siz);
666 }
667 if (solockp)
668 (void) nfs_sndlock(solockp, (struct nfsreq *)0);
669 if (slp->ns_flag & SLP_VALID)
670 error = nfs_send(so, nd->nd_nam2, m, NULL);
671 else {
672 error = EPIPE;
673 m_freem(m);
674 }
675 if (nfsrtton)
676 nfsd_rt(sotype, nd, cacherep);
677 if (nd->nd_nam2)
678 MFREE(nd->nd_nam2, m);
679 if (nd->nd_mrep)
680 m_freem(nd->nd_mrep);
681 if (error == EPIPE)
682 nfsrv_zapsock(slp);
683 if (solockp)
684 nfs_sndunlock(solockp);
685 if (error == EINTR || error == ERESTART) {
686 free((caddr_t)nd, M_NFSRVDESC);
687 nfsrv_slpderef(slp);
688 s = splsoftnet();
689 goto done;
690 }
691 break;
692 case RC_DROPIT:
693 if (nfsrtton)
694 nfsd_rt(sotype, nd, cacherep);
695 m_freem(nd->nd_mrep);
696 m_freem(nd->nd_nam2);
697 break;
698 };
699 if (nd) {
700 FREE((caddr_t)nd, M_NFSRVDESC);
701 nd = NULL;
702 }
703
704 /*
705 * Check to see if there are outstanding writes that
706 * need to be serviced.
707 */
708 cur_usec = (u_quad_t)time.tv_sec * 1000000 +
709 (u_quad_t)time.tv_usec;
710 s = splsoftclock();
711 if (slp->ns_tq.lh_first &&
712 slp->ns_tq.lh_first->nd_time <= cur_usec) {
713 cacherep = RC_DOIT;
714 writes_todo = 1;
715 } else
716 writes_todo = 0;
717 splx(s);
718 } while (writes_todo);
719 s = splsoftnet();
720 if (nfsrv_dorec(slp, nfsd, &nd)) {
721 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
722 nfsd->nfsd_slp = NULL;
723 nfsrv_slpderef(slp);
724 }
725 }
726 done:
727 TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
728 splx(s);
729 free((caddr_t)nfsd, M_NFSD);
730 nsd->nsd_nfsd = (struct nfsd *)0;
731 if (--nfs_numnfsd == 0)
732 nfsrv_init(TRUE); /* Reinitialize everything */
733 return (error);
734 }
735
736 /*
737 * Shut down a socket associated with an nfssvc_sock structure.
738 * Should be called with the send lock set, if required.
739 * The trick here is to increment the sref at the start, so that the nfsds
740 * will stop using it and clear ns_flag at the end so that it will not be
741 * reassigned during cleanup.
742 */
743 void
744 nfsrv_zapsock(slp)
745 register struct nfssvc_sock *slp;
746 {
747 register struct nfsuid *nuidp, *nnuidp;
748 register struct nfsrv_descript *nwp, *nnwp;
749 struct socket *so;
750 struct file *fp;
751 struct mbuf *m;
752 int s;
753
754 slp->ns_flag &= ~SLP_ALLFLAGS;
755 fp = slp->ns_fp;
756 if (fp) {
757 slp->ns_fp = (struct file *)0;
758 so = slp->ns_so;
759 so->so_upcall = NULL;
760 so->so_upcallarg = NULL;
761 so->so_rcv.sb_flags &= ~SB_UPCALL;
762 soshutdown(so, 2);
763 closef(fp, (struct proc *)0);
764 if (slp->ns_nam)
765 MFREE(slp->ns_nam, m);
766 m_freem(slp->ns_raw);
767 m_freem(slp->ns_rec);
768 for (nuidp = slp->ns_uidlruhead.tqh_first; nuidp != 0;
769 nuidp = nnuidp) {
770 nnuidp = nuidp->nu_lru.tqe_next;
771 LIST_REMOVE(nuidp, nu_hash);
772 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
773 if (nuidp->nu_flag & NU_NAM)
774 m_freem(nuidp->nu_nam);
775 free((caddr_t)nuidp, M_NFSUID);
776 }
777 s = splsoftclock();
778 for (nwp = slp->ns_tq.lh_first; nwp; nwp = nnwp) {
779 nnwp = nwp->nd_tq.le_next;
780 LIST_REMOVE(nwp, nd_tq);
781 free((caddr_t)nwp, M_NFSRVDESC);
782 }
783 LIST_INIT(&slp->ns_tq);
784 splx(s);
785 }
786 }
787
788 /*
789 * Derefence a server socket structure. If it has no more references and
790 * is no longer valid, you can throw it away.
791 */
792 void
793 nfsrv_slpderef(slp)
794 register struct nfssvc_sock *slp;
795 {
796 if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) {
797 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
798 free((caddr_t)slp, M_NFSSVC);
799 }
800 }
801
802 /*
803 * Initialize the data structures for the server.
804 * Handshake with any new nfsds starting up to avoid any chance of
805 * corruption.
806 */
807 void
808 nfsrv_init(terminating)
809 int terminating;
810 {
811 register struct nfssvc_sock *slp, *nslp;
812
813 if (nfssvc_sockhead_flag & SLP_INIT)
814 panic("nfsd init");
815 nfssvc_sockhead_flag |= SLP_INIT;
816 if (terminating) {
817 for (slp = nfssvc_sockhead.tqh_first; slp != 0; slp = nslp) {
818 nslp = slp->ns_chain.tqe_next;
819 if (slp->ns_flag & SLP_VALID)
820 nfsrv_zapsock(slp);
821 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
822 free((caddr_t)slp, M_NFSSVC);
823 }
824 nfsrv_cleancache(); /* And clear out server cache */
825 } else
826 nfs_pub.np_valid = 0;
827
828 TAILQ_INIT(&nfssvc_sockhead);
829 nfssvc_sockhead_flag &= ~SLP_INIT;
830 if (nfssvc_sockhead_flag & SLP_WANTINIT) {
831 nfssvc_sockhead_flag &= ~SLP_WANTINIT;
832 wakeup((caddr_t)&nfssvc_sockhead);
833 }
834
835 TAILQ_INIT(&nfsd_head);
836 nfsd_head_flag &= ~NFSD_CHECKSLP;
837
838 nfs_udpsock = (struct nfssvc_sock *)
839 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
840 bzero((caddr_t)nfs_udpsock, sizeof (struct nfssvc_sock));
841 TAILQ_INIT(&nfs_udpsock->ns_uidlruhead);
842 TAILQ_INSERT_HEAD(&nfssvc_sockhead, nfs_udpsock, ns_chain);
843
844 nfs_cltpsock = (struct nfssvc_sock *)
845 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
846 bzero((caddr_t)nfs_cltpsock, sizeof (struct nfssvc_sock));
847 TAILQ_INIT(&nfs_cltpsock->ns_uidlruhead);
848 TAILQ_INSERT_TAIL(&nfssvc_sockhead, nfs_cltpsock, ns_chain);
849 }
850
851 /*
852 * Add entries to the server monitor log.
853 */
854 static void
855 nfsd_rt(sotype, nd, cacherep)
856 int sotype;
857 register struct nfsrv_descript *nd;
858 int cacherep;
859 {
860 register struct drt *rt;
861
862 rt = &nfsdrt.drt[nfsdrt.pos];
863 if (cacherep == RC_DOIT)
864 rt->flag = 0;
865 else if (cacherep == RC_REPLY)
866 rt->flag = DRT_CACHEREPLY;
867 else
868 rt->flag = DRT_CACHEDROP;
869 if (sotype == SOCK_STREAM)
870 rt->flag |= DRT_TCP;
871 if (nd->nd_flag & ND_NQNFS)
872 rt->flag |= DRT_NQNFS;
873 else if (nd->nd_flag & ND_NFSV3)
874 rt->flag |= DRT_NFSV3;
875 rt->proc = nd->nd_procnum;
876 if (mtod(nd->nd_nam, struct sockaddr *)->sa_family == AF_INET)
877 rt->ipadr = mtod(nd->nd_nam, struct sockaddr_in *)->sin_addr.s_addr;
878 else
879 rt->ipadr = INADDR_ANY;
880 rt->resptime = ((time.tv_sec - nd->nd_starttime.tv_sec) * 1000000) +
881 (time.tv_usec - nd->nd_starttime.tv_usec);
882 rt->tstamp = time;
883 nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
884 }
885 #endif /* NFSSERVER */
886
887 #ifdef NFS
888
889 int nfs_defect = 0;
890 /*
891 * Asynchronous I/O daemons for client nfs.
892 * They do read-ahead and write-behind operations on the block I/O cache.
893 * Never returns unless it fails or gets killed.
894 */
895 int
896 nfssvc_iod(p)
897 struct proc *p;
898 {
899 register struct buf *bp;
900 register int i, myiod;
901 struct nfsmount *nmp;
902 int error = 0;
903
904 /*
905 * Assign my position or return error if too many already running
906 */
907 myiod = -1;
908 for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
909 if (nfs_asyncdaemon[i] == 0) {
910 nfs_asyncdaemon[i]++;
911 myiod = i;
912 break;
913 }
914 if (myiod == -1)
915 return (EBUSY);
916 nfs_numasync++;
917 /*
918 * Just loop around doin our stuff until SIGKILL
919 */
920 for (;;) {
921 while (((nmp = nfs_iodmount[myiod]) == NULL
922 || nmp->nm_bufq.tqh_first == NULL)
923 && error == 0) {
924 if (nmp)
925 nmp->nm_bufqiods--;
926 nfs_iodwant[myiod] = p;
927 nfs_iodmount[myiod] = NULL;
928 error = tsleep((caddr_t)&nfs_iodwant[myiod],
929 PWAIT | PCATCH, "nfsidl", 0);
930 }
931 if (error) {
932 nfs_asyncdaemon[myiod] = 0;
933 if (nmp)
934 nmp->nm_bufqiods--;
935 nfs_iodmount[myiod] = NULL;
936 nfs_numasync--;
937 return (error);
938 }
939 while ((bp = nmp->nm_bufq.tqh_first) != NULL) {
940 /* Take one off the front of the list */
941 TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
942 nmp->nm_bufqlen--;
943 if (nmp->nm_bufqwant && nmp->nm_bufqlen < 2 * nfs_numasync) {
944 nmp->nm_bufqwant = FALSE;
945 wakeup(&nmp->nm_bufq);
946 }
947 if (bp->b_flags & B_READ)
948 (void) nfs_doio(bp, bp->b_rcred, (struct proc *)0);
949 else
950 (void) nfs_doio(bp, bp->b_wcred, (struct proc *)0);
951 /*
952 * If there are more than one iod on this mount, then defect
953 * so that the iods can be shared out fairly between the mounts
954 */
955 if (nfs_defect && nmp->nm_bufqiods > 1) {
956 nfs_iodmount[myiod] = NULL;
957 nmp->nm_bufqiods--;
958 break;
959 }
960 }
961 }
962 }
963
964
965 /*
966 * Get an authorization string for the uid by having the mount_nfs sitting
967 * on this mount point porpous out of the kernel and do it.
968 */
969 int
970 nfs_getauth(nmp, rep, cred, auth_str, auth_len, verf_str, verf_len, key)
971 register struct nfsmount *nmp;
972 struct nfsreq *rep;
973 struct ucred *cred;
974 char **auth_str;
975 int *auth_len;
976 char *verf_str;
977 int *verf_len;
978 NFSKERBKEY_T key; /* return session key */
979 {
980 int error = 0;
981
982 while ((nmp->nm_iflag & NFSMNT_WAITAUTH) == 0) {
983 nmp->nm_iflag |= NFSMNT_WANTAUTH;
984 (void) tsleep((caddr_t)&nmp->nm_authtype, PSOCK,
985 "nfsauth1", 2 * hz);
986 error = nfs_sigintr(nmp, rep, rep->r_procp);
987 if (error) {
988 nmp->nm_iflag &= ~NFSMNT_WANTAUTH;
989 return (error);
990 }
991 }
992 nmp->nm_iflag &= ~(NFSMNT_WAITAUTH | NFSMNT_WANTAUTH);
993 nmp->nm_authstr = *auth_str = (char *)malloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK);
994 nmp->nm_authlen = RPCAUTH_MAXSIZ;
995 nmp->nm_verfstr = verf_str;
996 nmp->nm_verflen = *verf_len;
997 nmp->nm_authuid = cred->cr_uid;
998 wakeup((caddr_t)&nmp->nm_authstr);
999
1000 /*
1001 * And wait for mount_nfs to do its stuff.
1002 */
1003 while ((nmp->nm_iflag & NFSMNT_HASAUTH) == 0 && error == 0) {
1004 (void) tsleep((caddr_t)&nmp->nm_authlen, PSOCK,
1005 "nfsauth2", 2 * hz);
1006 error = nfs_sigintr(nmp, rep, rep->r_procp);
1007 }
1008 if (nmp->nm_iflag & NFSMNT_AUTHERR) {
1009 nmp->nm_iflag &= ~NFSMNT_AUTHERR;
1010 error = EAUTH;
1011 }
1012 if (error)
1013 free((caddr_t)*auth_str, M_TEMP);
1014 else {
1015 *auth_len = nmp->nm_authlen;
1016 *verf_len = nmp->nm_verflen;
1017 bcopy((caddr_t)nmp->nm_key, (caddr_t)key, sizeof (key));
1018 }
1019 nmp->nm_iflag &= ~NFSMNT_HASAUTH;
1020 nmp->nm_iflag |= NFSMNT_WAITAUTH;
1021 if (nmp->nm_iflag & NFSMNT_WANTAUTH) {
1022 nmp->nm_iflag &= ~NFSMNT_WANTAUTH;
1023 wakeup((caddr_t)&nmp->nm_authtype);
1024 }
1025 return (error);
1026 }
1027
1028 /*
1029 * Get a nickname authenticator and verifier.
1030 */
1031 int
1032 nfs_getnickauth(nmp, cred, auth_str, auth_len, verf_str, verf_len)
1033 struct nfsmount *nmp;
1034 struct ucred *cred;
1035 char **auth_str;
1036 int *auth_len;
1037 char *verf_str;
1038 int verf_len;
1039 {
1040 register struct nfsuid *nuidp;
1041 register u_int32_t *nickp, *verfp;
1042 struct timeval ktvin, ktvout;
1043
1044 #ifdef DIAGNOSTIC
1045 if (verf_len < (4 * NFSX_UNSIGNED))
1046 panic("nfs_getnickauth verf too small");
1047 #endif
1048 for (nuidp = NMUIDHASH(nmp, cred->cr_uid)->lh_first;
1049 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
1050 if (nuidp->nu_cr.cr_uid == cred->cr_uid)
1051 break;
1052 }
1053 if (!nuidp || nuidp->nu_expire < time.tv_sec)
1054 return (EACCES);
1055
1056 /*
1057 * Move to the end of the lru list (end of lru == most recently used).
1058 */
1059 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
1060 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru);
1061
1062 nickp = (u_int32_t *)malloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK);
1063 *nickp++ = txdr_unsigned(RPCAKN_NICKNAME);
1064 *nickp = txdr_unsigned(nuidp->nu_nickname);
1065 *auth_str = (char *)nickp;
1066 *auth_len = 2 * NFSX_UNSIGNED;
1067
1068 /*
1069 * Now we must encrypt the verifier and package it up.
1070 */
1071 verfp = (u_int32_t *)verf_str;
1072 *verfp++ = txdr_unsigned(RPCAKN_NICKNAME);
1073 if (time.tv_sec > nuidp->nu_timestamp.tv_sec ||
1074 (time.tv_sec == nuidp->nu_timestamp.tv_sec &&
1075 time.tv_usec > nuidp->nu_timestamp.tv_usec))
1076 nuidp->nu_timestamp = time;
1077 else
1078 nuidp->nu_timestamp.tv_usec++;
1079 ktvin.tv_sec = txdr_unsigned(nuidp->nu_timestamp.tv_sec);
1080 ktvin.tv_usec = txdr_unsigned(nuidp->nu_timestamp.tv_usec);
1081
1082 /*
1083 * Now encrypt the timestamp verifier in ecb mode using the session
1084 * key.
1085 */
1086 #ifdef NFSKERB
1087 XXX
1088 #endif
1089
1090 *verfp++ = ktvout.tv_sec;
1091 *verfp++ = ktvout.tv_usec;
1092 *verfp = 0;
1093 return (0);
1094 }
1095
1096 /*
1097 * Save the current nickname in a hash list entry on the mount point.
1098 */
1099 int
1100 nfs_savenickauth(nmp, cred, len, key, mdp, dposp, mrep)
1101 register struct nfsmount *nmp;
1102 struct ucred *cred;
1103 int len;
1104 NFSKERBKEY_T key;
1105 struct mbuf **mdp;
1106 char **dposp;
1107 struct mbuf *mrep;
1108 {
1109 register struct nfsuid *nuidp;
1110 register u_int32_t *tl;
1111 register int32_t t1;
1112 struct mbuf *md = *mdp;
1113 struct timeval ktvin, ktvout;
1114 u_int32_t nick;
1115 char *dpos = *dposp, *cp2;
1116 int deltasec, error = 0;
1117
1118 if (len == (3 * NFSX_UNSIGNED)) {
1119 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1120 ktvin.tv_sec = *tl++;
1121 ktvin.tv_usec = *tl++;
1122 nick = fxdr_unsigned(u_int32_t, *tl);
1123
1124 /*
1125 * Decrypt the timestamp in ecb mode.
1126 */
1127 #ifdef NFSKERB
1128 XXX
1129 #endif
1130 ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec);
1131 ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec);
1132 deltasec = time.tv_sec - ktvout.tv_sec;
1133 if (deltasec < 0)
1134 deltasec = -deltasec;
1135 /*
1136 * If ok, add it to the hash list for the mount point.
1137 */
1138 if (deltasec <= NFS_KERBCLOCKSKEW) {
1139 if (nmp->nm_numuids < nuidhash_max) {
1140 nmp->nm_numuids++;
1141 nuidp = (struct nfsuid *)
1142 malloc(sizeof (struct nfsuid), M_NFSUID,
1143 M_WAITOK);
1144 } else {
1145 nuidp = nmp->nm_uidlruhead.tqh_first;
1146 LIST_REMOVE(nuidp, nu_hash);
1147 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp,
1148 nu_lru);
1149 }
1150 nuidp->nu_flag = 0;
1151 nuidp->nu_cr.cr_uid = cred->cr_uid;
1152 nuidp->nu_expire = time.tv_sec + NFS_KERBTTL;
1153 nuidp->nu_timestamp = ktvout;
1154 nuidp->nu_nickname = nick;
1155 bcopy(key, nuidp->nu_key, sizeof (key));
1156 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp,
1157 nu_lru);
1158 LIST_INSERT_HEAD(NMUIDHASH(nmp, cred->cr_uid),
1159 nuidp, nu_hash);
1160 }
1161 } else
1162 nfsm_adv(nfsm_rndup(len));
1163 nfsmout:
1164 *mdp = md;
1165 *dposp = dpos;
1166 return (error);
1167 }
1168 #endif /* NFS */
1169