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