nfs_syscalls.c revision 1.34 1 /* $NetBSD: nfs_syscalls.c,v 1.34 1999/05/04 16:08:02 sommerfe 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 #ifdef DIAGNOSTIC
628 int lockcount;
629 #endif
630 switch (cacherep) {
631 case RC_DOIT:
632 #ifdef DIAGNOSTIC
633 /*
634 * NFS server procs should neither release
635 * locks already held, nor leave things
636 * locked. Catch this sooner, rather than
637 * later (when we try to relock something we
638 * already have locked). Careful inspection
639 * of the failing routine usually turns up the
640 * lock leak.. once we know what it is..
641 */
642 lockcount = p->p_locks;
643 #endif
644 if (writes_todo || (nd->nd_procnum == NFSPROC_WRITE &&
645 nfsrvw_procrastinate > 0 && !notstarted))
646 error = nfsrv_writegather(&nd, slp,
647 nfsd->nfsd_procp, &mreq);
648 else
649 error = (*(nfsrv3_procs[nd->nd_procnum]))(nd,
650 slp, nfsd->nfsd_procp, &mreq);
651 #ifdef DIAGNOSTIC
652 if (p->p_locks != lockcount) {
653 /*
654 * If you see this panic, audit
655 * nfsrv3_procs[nd->nd_procnum] for vnode
656 * locking errors (usually, it's due to
657 * forgetting to vput() something).
658 */
659 panic("nfsd: locking botch in op %d",
660 nd ? nd->nd_procnum : -1);
661 }
662 #endif
663 if (mreq == NULL)
664 break;
665 if (error) {
666 if (nd->nd_procnum != NQNFSPROC_VACATED)
667 nfsstats.srv_errs++;
668 nfsrv_updatecache(nd, FALSE, mreq);
669 if (nd->nd_nam2)
670 m_freem(nd->nd_nam2);
671 break;
672 }
673 nfsstats.srvrpccnt[nd->nd_procnum]++;
674 nfsrv_updatecache(nd, TRUE, mreq);
675 nd->nd_mrep = (struct mbuf *)0;
676 case RC_REPLY:
677 m = mreq;
678 siz = 0;
679 while (m) {
680 siz += m->m_len;
681 m = m->m_next;
682 }
683 if (siz <= 0 || siz > NFS_MAXPACKET) {
684 printf("mbuf siz=%d\n",siz);
685 panic("Bad nfs svc reply");
686 }
687 m = mreq;
688 m->m_pkthdr.len = siz;
689 m->m_pkthdr.rcvif = (struct ifnet *)0;
690 /*
691 * For stream protocols, prepend a Sun RPC
692 * Record Mark.
693 */
694 if (sotype == SOCK_STREAM) {
695 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
696 *mtod(m, u_int32_t *) = htonl(0x80000000 | siz);
697 }
698 if (solockp)
699 (void) nfs_sndlock(solockp, (struct nfsreq *)0);
700 if (slp->ns_flag & SLP_VALID)
701 error = nfs_send(so, nd->nd_nam2, m, NULL);
702 else {
703 error = EPIPE;
704 m_freem(m);
705 }
706 if (nfsrtton)
707 nfsd_rt(sotype, nd, cacherep);
708 if (nd->nd_nam2)
709 MFREE(nd->nd_nam2, m);
710 if (nd->nd_mrep)
711 m_freem(nd->nd_mrep);
712 if (error == EPIPE)
713 nfsrv_zapsock(slp);
714 if (solockp)
715 nfs_sndunlock(solockp);
716 if (error == EINTR || error == ERESTART) {
717 free((caddr_t)nd, M_NFSRVDESC);
718 nfsrv_slpderef(slp);
719 s = splsoftnet();
720 goto done;
721 }
722 break;
723 case RC_DROPIT:
724 if (nfsrtton)
725 nfsd_rt(sotype, nd, cacherep);
726 m_freem(nd->nd_mrep);
727 m_freem(nd->nd_nam2);
728 break;
729 };
730 if (nd) {
731 FREE((caddr_t)nd, M_NFSRVDESC);
732 nd = NULL;
733 }
734
735 /*
736 * Check to see if there are outstanding writes that
737 * need to be serviced.
738 */
739 cur_usec = (u_quad_t)time.tv_sec * 1000000 +
740 (u_quad_t)time.tv_usec;
741 s = splsoftclock();
742 if (slp->ns_tq.lh_first &&
743 slp->ns_tq.lh_first->nd_time <= cur_usec) {
744 cacherep = RC_DOIT;
745 writes_todo = 1;
746 } else
747 writes_todo = 0;
748 splx(s);
749 } while (writes_todo);
750 s = splsoftnet();
751 if (nfsrv_dorec(slp, nfsd, &nd)) {
752 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
753 nfsd->nfsd_slp = NULL;
754 nfsrv_slpderef(slp);
755 }
756 }
757 done:
758 p->p_holdcnt--;
759 TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
760 splx(s);
761 free((caddr_t)nfsd, M_NFSD);
762 nsd->nsd_nfsd = (struct nfsd *)0;
763 if (--nfs_numnfsd == 0)
764 nfsrv_init(TRUE); /* Reinitialize everything */
765 return (error);
766 }
767
768 /*
769 * Shut down a socket associated with an nfssvc_sock structure.
770 * Should be called with the send lock set, if required.
771 * The trick here is to increment the sref at the start, so that the nfsds
772 * will stop using it and clear ns_flag at the end so that it will not be
773 * reassigned during cleanup.
774 */
775 void
776 nfsrv_zapsock(slp)
777 register struct nfssvc_sock *slp;
778 {
779 register struct nfsuid *nuidp, *nnuidp;
780 register struct nfsrv_descript *nwp, *nnwp;
781 struct socket *so;
782 struct file *fp;
783 struct mbuf *m;
784 int s;
785
786 slp->ns_flag &= ~SLP_ALLFLAGS;
787 fp = slp->ns_fp;
788 if (fp) {
789 slp->ns_fp = (struct file *)0;
790 so = slp->ns_so;
791 so->so_upcall = NULL;
792 so->so_upcallarg = NULL;
793 so->so_rcv.sb_flags &= ~SB_UPCALL;
794 soshutdown(so, 2);
795 closef(fp, (struct proc *)0);
796 if (slp->ns_nam)
797 MFREE(slp->ns_nam, m);
798 m_freem(slp->ns_raw);
799 m_freem(slp->ns_rec);
800 for (nuidp = slp->ns_uidlruhead.tqh_first; nuidp != 0;
801 nuidp = nnuidp) {
802 nnuidp = nuidp->nu_lru.tqe_next;
803 LIST_REMOVE(nuidp, nu_hash);
804 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
805 if (nuidp->nu_flag & NU_NAM)
806 m_freem(nuidp->nu_nam);
807 free((caddr_t)nuidp, M_NFSUID);
808 }
809 s = splsoftclock();
810 for (nwp = slp->ns_tq.lh_first; nwp; nwp = nnwp) {
811 nnwp = nwp->nd_tq.le_next;
812 LIST_REMOVE(nwp, nd_tq);
813 free((caddr_t)nwp, M_NFSRVDESC);
814 }
815 LIST_INIT(&slp->ns_tq);
816 splx(s);
817 }
818 }
819
820 /*
821 * Derefence a server socket structure. If it has no more references and
822 * is no longer valid, you can throw it away.
823 */
824 void
825 nfsrv_slpderef(slp)
826 register struct nfssvc_sock *slp;
827 {
828 if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) {
829 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
830 free((caddr_t)slp, M_NFSSVC);
831 }
832 }
833
834 /*
835 * Initialize the data structures for the server.
836 * Handshake with any new nfsds starting up to avoid any chance of
837 * corruption.
838 */
839 void
840 nfsrv_init(terminating)
841 int terminating;
842 {
843 register struct nfssvc_sock *slp, *nslp;
844
845 if (nfssvc_sockhead_flag & SLP_INIT)
846 panic("nfsd init");
847 nfssvc_sockhead_flag |= SLP_INIT;
848 if (terminating) {
849 for (slp = nfssvc_sockhead.tqh_first; slp != 0; slp = nslp) {
850 nslp = slp->ns_chain.tqe_next;
851 if (slp->ns_flag & SLP_VALID)
852 nfsrv_zapsock(slp);
853 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
854 free((caddr_t)slp, M_NFSSVC);
855 }
856 nfsrv_cleancache(); /* And clear out server cache */
857 } else
858 nfs_pub.np_valid = 0;
859
860 TAILQ_INIT(&nfssvc_sockhead);
861 nfssvc_sockhead_flag &= ~SLP_INIT;
862 if (nfssvc_sockhead_flag & SLP_WANTINIT) {
863 nfssvc_sockhead_flag &= ~SLP_WANTINIT;
864 wakeup((caddr_t)&nfssvc_sockhead);
865 }
866
867 TAILQ_INIT(&nfsd_head);
868 nfsd_head_flag &= ~NFSD_CHECKSLP;
869
870 nfs_udpsock = (struct nfssvc_sock *)
871 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
872 memset((caddr_t)nfs_udpsock, 0, sizeof (struct nfssvc_sock));
873 TAILQ_INIT(&nfs_udpsock->ns_uidlruhead);
874 TAILQ_INSERT_HEAD(&nfssvc_sockhead, nfs_udpsock, ns_chain);
875
876 nfs_cltpsock = (struct nfssvc_sock *)
877 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
878 memset((caddr_t)nfs_cltpsock, 0, sizeof (struct nfssvc_sock));
879 TAILQ_INIT(&nfs_cltpsock->ns_uidlruhead);
880 TAILQ_INSERT_TAIL(&nfssvc_sockhead, nfs_cltpsock, ns_chain);
881 }
882
883 /*
884 * Add entries to the server monitor log.
885 */
886 static void
887 nfsd_rt(sotype, nd, cacherep)
888 int sotype;
889 register struct nfsrv_descript *nd;
890 int cacherep;
891 {
892 register struct drt *rt;
893
894 rt = &nfsdrt.drt[nfsdrt.pos];
895 if (cacherep == RC_DOIT)
896 rt->flag = 0;
897 else if (cacherep == RC_REPLY)
898 rt->flag = DRT_CACHEREPLY;
899 else
900 rt->flag = DRT_CACHEDROP;
901 if (sotype == SOCK_STREAM)
902 rt->flag |= DRT_TCP;
903 if (nd->nd_flag & ND_NQNFS)
904 rt->flag |= DRT_NQNFS;
905 else if (nd->nd_flag & ND_NFSV3)
906 rt->flag |= DRT_NFSV3;
907 rt->proc = nd->nd_procnum;
908 if (mtod(nd->nd_nam, struct sockaddr *)->sa_family == AF_INET)
909 rt->ipadr = mtod(nd->nd_nam, struct sockaddr_in *)->sin_addr.s_addr;
910 else
911 rt->ipadr = INADDR_ANY;
912 rt->resptime = ((time.tv_sec - nd->nd_starttime.tv_sec) * 1000000) +
913 (time.tv_usec - nd->nd_starttime.tv_usec);
914 rt->tstamp = time;
915 nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
916 }
917 #endif /* NFSSERVER */
918
919 #ifdef NFS
920
921 int nfs_defect = 0;
922 /*
923 * Asynchronous I/O daemons for client nfs.
924 * They do read-ahead and write-behind operations on the block I/O cache.
925 * Never returns unless it fails or gets killed.
926 */
927 int
928 nfssvc_iod(p)
929 struct proc *p;
930 {
931 register struct buf *bp;
932 register int i, myiod;
933 struct nfsmount *nmp;
934 int error = 0;
935
936 /*
937 * Assign my position or return error if too many already running
938 */
939 myiod = -1;
940 for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
941 if (nfs_asyncdaemon[i] == 0) {
942 myiod = i;
943 break;
944 }
945 if (myiod == -1)
946 return (EBUSY);
947 nfs_asyncdaemon[myiod] = 1;
948 nfs_numasync++;
949 p->p_holdcnt++;
950 /*
951 * Just loop around doin our stuff until SIGKILL
952 */
953 for (;;) {
954 while (((nmp = nfs_iodmount[myiod]) == NULL
955 || nmp->nm_bufq.tqh_first == NULL)
956 && error == 0) {
957 if (nmp)
958 nmp->nm_bufqiods--;
959 nfs_iodwant[myiod] = p;
960 nfs_iodmount[myiod] = NULL;
961 error = tsleep((caddr_t)&nfs_iodwant[myiod],
962 PWAIT | PCATCH, "nfsidl", 0);
963 }
964 if (error) {
965 if (nmp)
966 nmp->nm_bufqiods--;
967 nfs_iodmount[myiod] = NULL;
968 break;
969 }
970 while ((bp = nmp->nm_bufq.tqh_first) != NULL) {
971 /* Take one off the front of the list */
972 TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
973 nmp->nm_bufqlen--;
974 if (nmp->nm_bufqwant && nmp->nm_bufqlen < 2 * nfs_numasync) {
975 nmp->nm_bufqwant = FALSE;
976 wakeup(&nmp->nm_bufq);
977 }
978 if (bp->b_flags & B_READ)
979 (void) nfs_doio(bp, bp->b_rcred, (struct proc *)0);
980 else
981 (void) nfs_doio(bp, bp->b_wcred, (struct proc *)0);
982 /*
983 * If there are more than one iod on this mount, then defect
984 * so that the iods can be shared out fairly between the mounts
985 */
986 if (nfs_defect && nmp->nm_bufqiods > 1) {
987 nfs_iodmount[myiod] = NULL;
988 nmp->nm_bufqiods--;
989 break;
990 }
991 }
992 }
993 p->p_holdcnt--;
994 nfs_asyncdaemon[myiod] = 0;
995 nfs_numasync--;
996 return (error);
997 }
998
999
1000 /*
1001 * Get an authorization string for the uid by having the mount_nfs sitting
1002 * on this mount point porpous out of the kernel and do it.
1003 */
1004 int
1005 nfs_getauth(nmp, rep, cred, auth_str, auth_len, verf_str, verf_len, key)
1006 register struct nfsmount *nmp;
1007 struct nfsreq *rep;
1008 struct ucred *cred;
1009 char **auth_str;
1010 int *auth_len;
1011 char *verf_str;
1012 int *verf_len;
1013 NFSKERBKEY_T key; /* return session key */
1014 {
1015 int error = 0;
1016
1017 while ((nmp->nm_iflag & NFSMNT_WAITAUTH) == 0) {
1018 nmp->nm_iflag |= NFSMNT_WANTAUTH;
1019 (void) tsleep((caddr_t)&nmp->nm_authtype, PSOCK,
1020 "nfsauth1", 2 * hz);
1021 error = nfs_sigintr(nmp, rep, rep->r_procp);
1022 if (error) {
1023 nmp->nm_iflag &= ~NFSMNT_WANTAUTH;
1024 return (error);
1025 }
1026 }
1027 nmp->nm_iflag &= ~(NFSMNT_WAITAUTH | NFSMNT_WANTAUTH);
1028 nmp->nm_authstr = *auth_str = (char *)malloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK);
1029 nmp->nm_authlen = RPCAUTH_MAXSIZ;
1030 nmp->nm_verfstr = verf_str;
1031 nmp->nm_verflen = *verf_len;
1032 nmp->nm_authuid = cred->cr_uid;
1033 wakeup((caddr_t)&nmp->nm_authstr);
1034
1035 /*
1036 * And wait for mount_nfs to do its stuff.
1037 */
1038 while ((nmp->nm_iflag & NFSMNT_HASAUTH) == 0 && error == 0) {
1039 (void) tsleep((caddr_t)&nmp->nm_authlen, PSOCK,
1040 "nfsauth2", 2 * hz);
1041 error = nfs_sigintr(nmp, rep, rep->r_procp);
1042 }
1043 if (nmp->nm_iflag & NFSMNT_AUTHERR) {
1044 nmp->nm_iflag &= ~NFSMNT_AUTHERR;
1045 error = EAUTH;
1046 }
1047 if (error)
1048 free((caddr_t)*auth_str, M_TEMP);
1049 else {
1050 *auth_len = nmp->nm_authlen;
1051 *verf_len = nmp->nm_verflen;
1052 memcpy((caddr_t)key, (caddr_t)nmp->nm_key, sizeof (key));
1053 }
1054 nmp->nm_iflag &= ~NFSMNT_HASAUTH;
1055 nmp->nm_iflag |= NFSMNT_WAITAUTH;
1056 if (nmp->nm_iflag & NFSMNT_WANTAUTH) {
1057 nmp->nm_iflag &= ~NFSMNT_WANTAUTH;
1058 wakeup((caddr_t)&nmp->nm_authtype);
1059 }
1060 return (error);
1061 }
1062
1063 /*
1064 * Get a nickname authenticator and verifier.
1065 */
1066 int
1067 nfs_getnickauth(nmp, cred, auth_str, auth_len, verf_str, verf_len)
1068 struct nfsmount *nmp;
1069 struct ucred *cred;
1070 char **auth_str;
1071 int *auth_len;
1072 char *verf_str;
1073 int verf_len;
1074 {
1075 register struct nfsuid *nuidp;
1076 register u_int32_t *nickp, *verfp;
1077 struct timeval ktvin, ktvout;
1078
1079 #ifdef DIAGNOSTIC
1080 if (verf_len < (4 * NFSX_UNSIGNED))
1081 panic("nfs_getnickauth verf too small");
1082 #endif
1083 for (nuidp = NMUIDHASH(nmp, cred->cr_uid)->lh_first;
1084 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
1085 if (nuidp->nu_cr.cr_uid == cred->cr_uid)
1086 break;
1087 }
1088 if (!nuidp || nuidp->nu_expire < time.tv_sec)
1089 return (EACCES);
1090
1091 /*
1092 * Move to the end of the lru list (end of lru == most recently used).
1093 */
1094 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
1095 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru);
1096
1097 nickp = (u_int32_t *)malloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK);
1098 *nickp++ = txdr_unsigned(RPCAKN_NICKNAME);
1099 *nickp = txdr_unsigned(nuidp->nu_nickname);
1100 *auth_str = (char *)nickp;
1101 *auth_len = 2 * NFSX_UNSIGNED;
1102
1103 /*
1104 * Now we must encrypt the verifier and package it up.
1105 */
1106 verfp = (u_int32_t *)verf_str;
1107 *verfp++ = txdr_unsigned(RPCAKN_NICKNAME);
1108 if (time.tv_sec > nuidp->nu_timestamp.tv_sec ||
1109 (time.tv_sec == nuidp->nu_timestamp.tv_sec &&
1110 time.tv_usec > nuidp->nu_timestamp.tv_usec))
1111 nuidp->nu_timestamp = time;
1112 else
1113 nuidp->nu_timestamp.tv_usec++;
1114 ktvin.tv_sec = txdr_unsigned(nuidp->nu_timestamp.tv_sec);
1115 ktvin.tv_usec = txdr_unsigned(nuidp->nu_timestamp.tv_usec);
1116
1117 /*
1118 * Now encrypt the timestamp verifier in ecb mode using the session
1119 * key.
1120 */
1121 #ifdef NFSKERB
1122 XXX
1123 #endif
1124
1125 *verfp++ = ktvout.tv_sec;
1126 *verfp++ = ktvout.tv_usec;
1127 *verfp = 0;
1128 return (0);
1129 }
1130
1131 /*
1132 * Save the current nickname in a hash list entry on the mount point.
1133 */
1134 int
1135 nfs_savenickauth(nmp, cred, len, key, mdp, dposp, mrep)
1136 register struct nfsmount *nmp;
1137 struct ucred *cred;
1138 int len;
1139 NFSKERBKEY_T key;
1140 struct mbuf **mdp;
1141 char **dposp;
1142 struct mbuf *mrep;
1143 {
1144 register struct nfsuid *nuidp;
1145 register u_int32_t *tl;
1146 register int32_t t1;
1147 struct mbuf *md = *mdp;
1148 struct timeval ktvin, ktvout;
1149 u_int32_t nick;
1150 char *dpos = *dposp, *cp2;
1151 int deltasec, error = 0;
1152
1153 if (len == (3 * NFSX_UNSIGNED)) {
1154 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1155 ktvin.tv_sec = *tl++;
1156 ktvin.tv_usec = *tl++;
1157 nick = fxdr_unsigned(u_int32_t, *tl);
1158
1159 /*
1160 * Decrypt the timestamp in ecb mode.
1161 */
1162 #ifdef NFSKERB
1163 XXX
1164 #endif
1165 ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec);
1166 ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec);
1167 deltasec = time.tv_sec - ktvout.tv_sec;
1168 if (deltasec < 0)
1169 deltasec = -deltasec;
1170 /*
1171 * If ok, add it to the hash list for the mount point.
1172 */
1173 if (deltasec <= NFS_KERBCLOCKSKEW) {
1174 if (nmp->nm_numuids < nuidhash_max) {
1175 nmp->nm_numuids++;
1176 nuidp = (struct nfsuid *)
1177 malloc(sizeof (struct nfsuid), M_NFSUID,
1178 M_WAITOK);
1179 } else {
1180 nuidp = nmp->nm_uidlruhead.tqh_first;
1181 LIST_REMOVE(nuidp, nu_hash);
1182 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp,
1183 nu_lru);
1184 }
1185 nuidp->nu_flag = 0;
1186 nuidp->nu_cr.cr_uid = cred->cr_uid;
1187 nuidp->nu_expire = time.tv_sec + NFS_KERBTTL;
1188 nuidp->nu_timestamp = ktvout;
1189 nuidp->nu_nickname = nick;
1190 memcpy(nuidp->nu_key, key, sizeof (key));
1191 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp,
1192 nu_lru);
1193 LIST_INSERT_HEAD(NMUIDHASH(nmp, cred->cr_uid),
1194 nuidp, nu_hash);
1195 }
1196 } else
1197 nfsm_adv(nfsm_rndup(len));
1198 nfsmout:
1199 *mdp = md;
1200 *dposp = dpos;
1201 return (error);
1202 }
1203 #endif /* NFS */
1204