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