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