Merge from vendor branch CVS:
[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.20 2005/03/27 23:51:42 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, 1);
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 == NULL &&
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 == NULL &&
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                         --slp->ns_numrec;
753                         STAILQ_REMOVE_HEAD(&slp->ns_rec, nr_link);
754                         if (rec->nr_address)
755                                 FREE(rec->nr_address, M_SONAME);
756                         m_freem(rec->nr_packet);
757                         free(rec, M_NFSRVDESC);
758                 }
759                 TAILQ_FOREACH_MUTABLE(nuidp, &slp->ns_uidlruhead, nu_lru,
760                                       nnuidp) {
761                         LIST_REMOVE(nuidp, nu_hash);
762                         TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
763                         if (nuidp->nu_flag & NU_NAM)
764                                 FREE(nuidp->nu_nam, M_SONAME);
765                         free((caddr_t)nuidp, M_NFSUID);
766                 }
767                 s = splsoftclock();
768                 for (nwp = slp->ns_tq.lh_first; nwp; nwp = nnwp) {
769                         nnwp = nwp->nd_tq.le_next;
770                         LIST_REMOVE(nwp, nd_tq);
771                         free((caddr_t)nwp, M_NFSRVDESC);
772                 }
773                 LIST_INIT(&slp->ns_tq);
774                 splx(s);
775         }
776 }
777
778 /*
779  * Derefence a server socket structure. If it has no more references and
780  * is no longer valid, you can throw it away.
781  */
782 void
783 nfsrv_slpderef(struct nfssvc_sock *slp)
784 {
785         if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) {
786                 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
787                 free((caddr_t)slp, M_NFSSVC);
788         }
789 }
790
791 /*
792  * Lock a socket against others.
793  */
794 int
795 nfs_slplock(struct nfssvc_sock *slp, int wait)
796 {
797         int *statep = &slp->ns_solock;
798
799         if (!wait && (*statep & NFSSTA_SNDLOCK))
800                 return(0);      /* already locked, fail */
801         while (*statep & NFSSTA_SNDLOCK) {
802                 *statep |= NFSSTA_WANTSND;
803                 (void) tsleep((caddr_t)statep, 0, "nfsslplck", 0);
804         }
805         *statep |= NFSSTA_SNDLOCK;
806         return (1);
807 }
808
809 /*
810  * Unlock the stream socket for others.
811  */
812 void
813 nfs_slpunlock(struct nfssvc_sock *slp)
814 {
815         int *statep = &slp->ns_solock;
816
817         if ((*statep & NFSSTA_SNDLOCK) == 0)
818                 panic("nfs slpunlock");
819         *statep &= ~NFSSTA_SNDLOCK;
820         if (*statep & NFSSTA_WANTSND) {
821                 *statep &= ~NFSSTA_WANTSND;
822                 wakeup((caddr_t)statep);
823         }
824 }
825
826 /*
827  * Initialize the data structures for the server.
828  * Handshake with any new nfsds starting up to avoid any chance of
829  * corruption.
830  */
831 void
832 nfsrv_init(int terminating)
833 {
834         struct nfssvc_sock *slp, *nslp;
835
836         if (nfssvc_sockhead_flag & SLP_INIT)
837                 panic("nfsd init");
838         nfssvc_sockhead_flag |= SLP_INIT;
839         if (terminating) {
840                 TAILQ_FOREACH_MUTABLE(slp, &nfssvc_sockhead, ns_chain, nslp) {
841                         if (slp->ns_flag & SLP_VALID)
842                                 nfsrv_zapsock(slp);
843                         TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
844                         free((caddr_t)slp, M_NFSSVC);
845                 }
846                 nfsrv_cleancache();     /* And clear out server cache */
847         } else
848                 nfs_pub.np_valid = 0;
849
850         TAILQ_INIT(&nfssvc_sockhead);
851         nfssvc_sockhead_flag &= ~SLP_INIT;
852         if (nfssvc_sockhead_flag & SLP_WANTINIT) {
853                 nfssvc_sockhead_flag &= ~SLP_WANTINIT;
854                 wakeup((caddr_t)&nfssvc_sockhead);
855         }
856
857         TAILQ_INIT(&nfsd_head);
858         nfsd_head_flag &= ~NFSD_CHECKSLP;
859
860 #if 0
861         nfs_udpsock = (struct nfssvc_sock *)
862             malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
863         bzero((caddr_t)nfs_udpsock, sizeof (struct nfssvc_sock));
864         STAILQ_INIT(&nfs_udpsock->ns_rec);
865         TAILQ_INIT(&nfs_udpsock->ns_uidlruhead);
866         TAILQ_INSERT_HEAD(&nfssvc_sockhead, nfs_udpsock, ns_chain);
867
868         nfs_cltpsock = (struct nfssvc_sock *)
869             malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
870         bzero((caddr_t)nfs_cltpsock, sizeof (struct nfssvc_sock));
871         STAILQ_INIT(&nfs_cltpsock->ns_rec);
872         TAILQ_INIT(&nfs_cltpsock->ns_uidlruhead);
873         TAILQ_INSERT_TAIL(&nfssvc_sockhead, nfs_cltpsock, ns_chain);
874 #endif
875 }
876
877 /*
878  * Add entries to the server monitor log.
879  */
880 static void
881 nfsd_rt(int sotype, struct nfsrv_descript *nd, int cacherep)
882 {
883         struct drt *rt;
884
885         rt = &nfsdrt.drt[nfsdrt.pos];
886         if (cacherep == RC_DOIT)
887                 rt->flag = 0;
888         else if (cacherep == RC_REPLY)
889                 rt->flag = DRT_CACHEREPLY;
890         else
891                 rt->flag = DRT_CACHEDROP;
892         if (sotype == SOCK_STREAM)
893                 rt->flag |= DRT_TCP;
894         if (nd->nd_flag & ND_NQNFS)
895                 rt->flag |= DRT_NQNFS;
896         else if (nd->nd_flag & ND_NFSV3)
897                 rt->flag |= DRT_NFSV3;
898         rt->proc = nd->nd_procnum;
899         if (nd->nd_nam->sa_family == AF_INET)
900             rt->ipadr = ((struct sockaddr_in *)nd->nd_nam)->sin_addr.s_addr;
901         else
902             rt->ipadr = INADDR_ANY;
903         rt->resptime = nfs_curusec() - (nd->nd_starttime.tv_sec * 1000000 + nd->nd_starttime.tv_usec);
904         getmicrotime(&rt->tstamp);
905         nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
906 }
907 #endif /* NFS_NOSERVER */
908
909 static int nfs_defect = 0;
910 SYSCTL_INT(_vfs_nfs, OID_AUTO, defect, CTLFLAG_RW, &nfs_defect, 0, "");
911
912 /*
913  * Asynchronous I/O daemons for client nfs.
914  * They do read-ahead and write-behind operations on the block I/O cache.
915  * Never returns unless it fails or gets killed.
916  */
917 static int
918 nfssvc_iod(struct thread *td)
919 {
920         struct buf *bp;
921         int i, myiod;
922         struct nfsmount *nmp;
923         int error = 0;
924
925         /*
926          * Assign my position or return error if too many already running
927          */
928         myiod = -1;
929         for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
930                 if (nfs_asyncdaemon[i] == 0) {
931                         nfs_asyncdaemon[i]++;
932                         myiod = i;
933                         break;
934                 }
935         if (myiod == -1)
936                 return (EBUSY);
937         nfs_numasync++;
938         /*
939          * Just loop around doin our stuff until SIGKILL
940          */
941         for (;;) {
942             while (((nmp = nfs_iodmount[myiod]) == NULL
943                      || TAILQ_EMPTY(&nmp->nm_bufq))
944                    && error == 0) {
945                 if (nmp)
946                     nmp->nm_bufqiods--;
947                 nfs_iodwant[myiod] = td;
948                 nfs_iodmount[myiod] = NULL;
949                 error = tsleep((caddr_t)&nfs_iodwant[myiod],
950                         PCATCH, "nfsidl", 0);
951             }
952             if (error) {
953                 nfs_asyncdaemon[myiod] = 0;
954                 if (nmp)
955                     nmp->nm_bufqiods--;
956                 nfs_iodwant[myiod] = NULL;
957                 nfs_iodmount[myiod] = NULL;
958                 nfs_numasync--;
959                 return (error);
960             }
961             while ((bp = TAILQ_FIRST(&nmp->nm_bufq)) != NULL) {
962                 /* Take one off the front of the list */
963                 TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
964                 nmp->nm_bufqlen--;
965                 if (nmp->nm_bufqwant && nmp->nm_bufqlen <= nfs_numasync) {
966                     nmp->nm_bufqwant = FALSE;
967                     wakeup(&nmp->nm_bufq);
968                 }
969                 (void) nfs_doio(bp, NULL);
970                 /*
971                  * If there are more than one iod on this mount, then defect
972                  * so that the iods can be shared out fairly between the mounts
973                  */
974                 if (nfs_defect && nmp->nm_bufqiods > 1) {
975                     NFS_DPF(ASYNCIO,
976                             ("nfssvc_iod: iod %d defecting from mount %p\n",
977                              myiod, nmp));
978                     nfs_iodmount[myiod] = NULL;
979                     nmp->nm_bufqiods--;
980                     break;
981                 }
982             }
983         }
984 }
985
986
987 /*
988  * Get an authorization string for the uid by having the mount_nfs sitting
989  * on this mount point porpous out of the kernel and do it.
990  */
991 int
992 nfs_getauth(struct nfsmount *nmp, struct nfsreq *rep,
993             struct ucred *cred, char **auth_str, int *auth_len, char *verf_str,
994             int *verf_len, NFSKERBKEY_T key /* return session key */)
995 {
996         int error = 0;
997
998         while ((nmp->nm_state & NFSSTA_WAITAUTH) == 0) {
999                 nmp->nm_state |= NFSSTA_WANTAUTH;
1000                 (void) tsleep((caddr_t)&nmp->nm_authtype, 0,
1001                         "nfsauth1", 2 * hz);
1002                 error = nfs_sigintr(nmp, rep, rep->r_td);
1003                 if (error) {
1004                         nmp->nm_state &= ~NFSSTA_WANTAUTH;
1005                         return (error);
1006                 }
1007         }
1008         nmp->nm_state &= ~(NFSSTA_WAITAUTH | NFSSTA_WANTAUTH);
1009         nmp->nm_authstr = *auth_str = (char *)malloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK);
1010         nmp->nm_authlen = RPCAUTH_MAXSIZ;
1011         nmp->nm_verfstr = verf_str;
1012         nmp->nm_verflen = *verf_len;
1013         nmp->nm_authuid = cred->cr_uid;
1014         wakeup((caddr_t)&nmp->nm_authstr);
1015
1016         /*
1017          * And wait for mount_nfs to do its stuff.
1018          */
1019         while ((nmp->nm_state & NFSSTA_HASAUTH) == 0 && error == 0) {
1020                 (void) tsleep((caddr_t)&nmp->nm_authlen, 0,
1021                         "nfsauth2", 2 * hz);
1022                 error = nfs_sigintr(nmp, rep, rep->r_td);
1023         }
1024         if (nmp->nm_state & NFSSTA_AUTHERR) {
1025                 nmp->nm_state &= ~NFSSTA_AUTHERR;
1026                 error = EAUTH;
1027         }
1028         if (error)
1029                 free((caddr_t)*auth_str, M_TEMP);
1030         else {
1031                 *auth_len = nmp->nm_authlen;
1032                 *verf_len = nmp->nm_verflen;
1033                 bcopy((caddr_t)nmp->nm_key, (caddr_t)key, sizeof (key));
1034         }
1035         nmp->nm_state &= ~NFSSTA_HASAUTH;
1036         nmp->nm_state |= NFSSTA_WAITAUTH;
1037         if (nmp->nm_state & NFSSTA_WANTAUTH) {
1038                 nmp->nm_state &= ~NFSSTA_WANTAUTH;
1039                 wakeup((caddr_t)&nmp->nm_authtype);
1040         }
1041         return (error);
1042 }
1043
1044 /*
1045  * Get a nickname authenticator and verifier.
1046  */
1047 int
1048 nfs_getnickauth(struct nfsmount *nmp, struct ucred *cred, char **auth_str,
1049                 int *auth_len, char *verf_str, int verf_len)
1050 {
1051         struct nfsuid *nuidp;
1052         u_int32_t *nickp, *verfp;
1053         struct timeval ktvin, ktvout;
1054
1055 #ifdef DIAGNOSTIC
1056         if (verf_len < (4 * NFSX_UNSIGNED))
1057                 panic("nfs_getnickauth verf too small");
1058 #endif
1059         for (nuidp = NMUIDHASH(nmp, cred->cr_uid)->lh_first;
1060             nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
1061                 if (nuidp->nu_cr.cr_uid == cred->cr_uid)
1062                         break;
1063         }
1064         if (!nuidp || nuidp->nu_expire < time_second)
1065                 return (EACCES);
1066
1067         /*
1068          * Move to the end of the lru list (end of lru == most recently used).
1069          */
1070         TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
1071         TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru);
1072
1073         nickp = (u_int32_t *)malloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK);
1074         *nickp++ = txdr_unsigned(RPCAKN_NICKNAME);
1075         *nickp = txdr_unsigned(nuidp->nu_nickname);
1076         *auth_str = (char *)nickp;
1077         *auth_len = 2 * NFSX_UNSIGNED;
1078
1079         /*
1080          * Now we must encrypt the verifier and package it up.
1081          */
1082         verfp = (u_int32_t *)verf_str;
1083         *verfp++ = txdr_unsigned(RPCAKN_NICKNAME);
1084         if (time_second > nuidp->nu_timestamp.tv_sec ||
1085             (time_second == nuidp->nu_timestamp.tv_sec &&
1086              time_second > nuidp->nu_timestamp.tv_usec))
1087                 getmicrotime(&nuidp->nu_timestamp);
1088         else
1089                 nuidp->nu_timestamp.tv_usec++;
1090         ktvin.tv_sec = txdr_unsigned(nuidp->nu_timestamp.tv_sec);
1091         ktvin.tv_usec = txdr_unsigned(nuidp->nu_timestamp.tv_usec);
1092
1093         /*
1094          * Now encrypt the timestamp verifier in ecb mode using the session
1095          * key.
1096          */
1097 #ifdef NFSKERB
1098         XXX
1099 #endif
1100
1101         *verfp++ = ktvout.tv_sec;
1102         *verfp++ = ktvout.tv_usec;
1103         *verfp = 0;
1104         return (0);
1105 }
1106
1107 /*
1108  * Save the current nickname in a hash list entry on the mount point.
1109  */
1110 int
1111 nfs_savenickauth(struct nfsmount *nmp, struct ucred *cred, int len,
1112                  NFSKERBKEY_T key, struct mbuf **mdp, char **dposp,
1113                  struct mbuf *mrep)
1114 {
1115         struct nfsuid *nuidp;
1116         u_int32_t *tl;
1117         int32_t t1;
1118         struct mbuf *md = *mdp;
1119         struct timeval ktvin, ktvout;
1120         u_int32_t nick;
1121         char *dpos = *dposp, *cp2;
1122         int deltasec, error = 0;
1123
1124         if (len == (3 * NFSX_UNSIGNED)) {
1125                 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1126                 ktvin.tv_sec = *tl++;
1127                 ktvin.tv_usec = *tl++;
1128                 nick = fxdr_unsigned(u_int32_t, *tl);
1129
1130                 /*
1131                  * Decrypt the timestamp in ecb mode.
1132                  */
1133 #ifdef NFSKERB
1134                 XXX
1135 #endif
1136                 ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec);
1137                 ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec);
1138                 deltasec = time_second - ktvout.tv_sec;
1139                 if (deltasec < 0)
1140                         deltasec = -deltasec;
1141                 /*
1142                  * If ok, add it to the hash list for the mount point.
1143                  */
1144                 if (deltasec <= NFS_KERBCLOCKSKEW) {
1145                         if (nmp->nm_numuids < nuidhash_max) {
1146                                 nmp->nm_numuids++;
1147                                 nuidp = (struct nfsuid *)
1148                                    malloc(sizeof (struct nfsuid), M_NFSUID,
1149                                         M_WAITOK);
1150                         } else {
1151                                 nuidp = TAILQ_FIRST(&nmp->nm_uidlruhead);
1152                                 LIST_REMOVE(nuidp, nu_hash);
1153                                 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp,
1154                                         nu_lru);
1155                         }
1156                         nuidp->nu_flag = 0;
1157                         nuidp->nu_cr.cr_uid = cred->cr_uid;
1158                         nuidp->nu_expire = time_second + NFS_KERBTTL;
1159                         nuidp->nu_timestamp = ktvout;
1160                         nuidp->nu_nickname = nick;
1161                         bcopy(key, nuidp->nu_key, sizeof (key));
1162                         TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp,
1163                                 nu_lru);
1164                         LIST_INSERT_HEAD(NMUIDHASH(nmp, cred->cr_uid),
1165                                 nuidp, nu_hash);
1166                 }
1167         } else
1168                 nfsm_adv(nfsm_rndup(len));
1169 nfsmout:
1170         *mdp = md;
1171         *dposp = dpos;
1172         return (error);
1173 }