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