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