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