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