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