Remove the VREF() macro and uses of it.
[dragonfly.git] / sys / vfs / nfs / nfs_subs.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_subs.c  8.8 (Berkeley) 5/22/95
37  * $FreeBSD: /repoman/r/ncvs/src/sys/nfsclient/nfs_subs.c,v 1.128 2004/04/14 23:23:55 peadar Exp $
38  * $DragonFly: src/sys/vfs/nfs/nfs_subs.c,v 1.16 2004/04/24 04:32:04 drhodus Exp $
39  */
40
41 /*
42  * These functions support the macros and help fiddle mbuf chains for
43  * the nfs op functions. They do things like create the rpc header and
44  * copy data between mbuf chains and uio lists.
45  */
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/buf.h>
50 #include <sys/proc.h>
51 #include <sys/mount.h>
52 #include <sys/vnode.h>
53 #include <sys/namei.h>
54 #include <sys/mbuf.h>
55 #include <sys/socket.h>
56 #include <sys/stat.h>
57 #include <sys/malloc.h>
58 #include <sys/sysent.h>
59 #include <sys/syscall.h>
60 #include <sys/conf.h>
61
62 #include <vm/vm.h>
63 #include <vm/vm_object.h>
64 #include <vm/vm_extern.h>
65 #include <vm/vm_zone.h>
66
67 #include <sys/buf2.h>
68
69 #include "rpcv2.h"
70 #include "nfsproto.h"
71 #include "nfs.h"
72 #include "nfsmount.h"
73 #include "nfsnode.h"
74 #include "xdr_subs.h"
75 #include "nfsm_subs.h"
76 #include "nqnfs.h"
77 #include "nfsrtt.h"
78
79 #include <netinet/in.h>
80
81 /*
82  * Data items converted to xdr at startup, since they are constant
83  * This is kinda hokey, but may save a little time doing byte swaps
84  */
85 u_int32_t nfs_xdrneg1;
86 u_int32_t rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr,
87         rpc_mismatch, rpc_auth_unix, rpc_msgaccepted,
88         rpc_auth_kerb;
89 u_int32_t nfs_prog, nqnfs_prog, nfs_true, nfs_false;
90
91 /* And other global data */
92 static u_int32_t nfs_xid = 0;
93 static enum vtype nv2tov_type[8]= {
94         VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON,  VNON 
95 };
96 enum vtype nv3tov_type[8]= {
97         VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO
98 };
99
100 int nfs_ticks;
101 int nfs_pbuf_freecnt = -1;      /* start out unlimited */
102
103 struct nfs_reqq nfs_reqq;
104 struct nfssvc_sockhead nfssvc_sockhead;
105 int nfssvc_sockhead_flag;
106 struct nfsd_head nfsd_head;
107 int nfsd_head_flag;
108 struct nfs_bufq nfs_bufq;
109 struct nqtimerhead nqtimerhead;
110 struct nqfhhashhead *nqfhhashtbl;
111 u_long nqfhhash;
112
113 static void (*nfs_prev_lease_updatetime) (int);
114 static int nfs_prev_nfssvc_sy_narg;
115 static sy_call_t *nfs_prev_nfssvc_sy_call;
116
117 #ifndef NFS_NOSERVER
118
119 static vop_t *nfs_prev_vop_lease_check;
120
121 /*
122  * Mapping of old NFS Version 2 RPC numbers to generic numbers.
123  */
124 int nfsv3_procid[NFS_NPROCS] = {
125         NFSPROC_NULL,
126         NFSPROC_GETATTR,
127         NFSPROC_SETATTR,
128         NFSPROC_NOOP,
129         NFSPROC_LOOKUP,
130         NFSPROC_READLINK,
131         NFSPROC_READ,
132         NFSPROC_NOOP,
133         NFSPROC_WRITE,
134         NFSPROC_CREATE,
135         NFSPROC_REMOVE,
136         NFSPROC_RENAME,
137         NFSPROC_LINK,
138         NFSPROC_SYMLINK,
139         NFSPROC_MKDIR,
140         NFSPROC_RMDIR,
141         NFSPROC_READDIR,
142         NFSPROC_FSSTAT,
143         NFSPROC_NOOP,
144         NFSPROC_NOOP,
145         NFSPROC_NOOP,
146         NFSPROC_NOOP,
147         NFSPROC_NOOP,
148         NFSPROC_NOOP,
149         NFSPROC_NOOP,
150         NFSPROC_NOOP
151 };
152
153 #endif /* NFS_NOSERVER */
154 /*
155  * and the reverse mapping from generic to Version 2 procedure numbers
156  */
157 int nfsv2_procid[NFS_NPROCS] = {
158         NFSV2PROC_NULL,
159         NFSV2PROC_GETATTR,
160         NFSV2PROC_SETATTR,
161         NFSV2PROC_LOOKUP,
162         NFSV2PROC_NOOP,
163         NFSV2PROC_READLINK,
164         NFSV2PROC_READ,
165         NFSV2PROC_WRITE,
166         NFSV2PROC_CREATE,
167         NFSV2PROC_MKDIR,
168         NFSV2PROC_SYMLINK,
169         NFSV2PROC_CREATE,
170         NFSV2PROC_REMOVE,
171         NFSV2PROC_RMDIR,
172         NFSV2PROC_RENAME,
173         NFSV2PROC_LINK,
174         NFSV2PROC_READDIR,
175         NFSV2PROC_NOOP,
176         NFSV2PROC_STATFS,
177         NFSV2PROC_NOOP,
178         NFSV2PROC_NOOP,
179         NFSV2PROC_NOOP,
180         NFSV2PROC_NOOP,
181         NFSV2PROC_NOOP,
182         NFSV2PROC_NOOP,
183         NFSV2PROC_NOOP,
184 };
185
186 #ifndef NFS_NOSERVER
187 /*
188  * Maps errno values to nfs error numbers.
189  * Use NFSERR_IO as the catch all for ones not specifically defined in
190  * RFC 1094.
191  */
192 static u_char nfsrv_v2errmap[ELAST] = {
193   NFSERR_PERM,  NFSERR_NOENT,   NFSERR_IO,      NFSERR_IO,      NFSERR_IO,
194   NFSERR_NXIO,  NFSERR_IO,      NFSERR_IO,      NFSERR_IO,      NFSERR_IO,
195   NFSERR_IO,    NFSERR_IO,      NFSERR_ACCES,   NFSERR_IO,      NFSERR_IO,
196   NFSERR_IO,    NFSERR_EXIST,   NFSERR_IO,      NFSERR_NODEV,   NFSERR_NOTDIR,
197   NFSERR_ISDIR, NFSERR_IO,      NFSERR_IO,      NFSERR_IO,      NFSERR_IO,
198   NFSERR_IO,    NFSERR_FBIG,    NFSERR_NOSPC,   NFSERR_IO,      NFSERR_ROFS,
199   NFSERR_IO,    NFSERR_IO,      NFSERR_IO,      NFSERR_IO,      NFSERR_IO,
200   NFSERR_IO,    NFSERR_IO,      NFSERR_IO,      NFSERR_IO,      NFSERR_IO,
201   NFSERR_IO,    NFSERR_IO,      NFSERR_IO,      NFSERR_IO,      NFSERR_IO,
202   NFSERR_IO,    NFSERR_IO,      NFSERR_IO,      NFSERR_IO,      NFSERR_IO,
203   NFSERR_IO,    NFSERR_IO,      NFSERR_IO,      NFSERR_IO,      NFSERR_IO,
204   NFSERR_IO,    NFSERR_IO,      NFSERR_IO,      NFSERR_IO,      NFSERR_IO,
205   NFSERR_IO,    NFSERR_IO,      NFSERR_NAMETOL, NFSERR_IO,      NFSERR_IO,
206   NFSERR_NOTEMPTY, NFSERR_IO,   NFSERR_IO,      NFSERR_DQUOT,   NFSERR_STALE,
207   NFSERR_IO,    NFSERR_IO,      NFSERR_IO,      NFSERR_IO,      NFSERR_IO,
208   NFSERR_IO,    NFSERR_IO,      NFSERR_IO,      NFSERR_IO,      NFSERR_IO,
209   NFSERR_IO,    NFSERR_IO,      NFSERR_IO,      NFSERR_IO,      NFSERR_IO,
210   NFSERR_IO /* << Last is 86 */
211 };
212
213 /*
214  * Maps errno values to nfs error numbers.
215  * Although it is not obvious whether or not NFS clients really care if
216  * a returned error value is in the specified list for the procedure, the
217  * safest thing to do is filter them appropriately. For Version 2, the
218  * X/Open XNFS document is the only specification that defines error values
219  * for each RPC (The RFC simply lists all possible error values for all RPCs),
220  * so I have decided to not do this for Version 2.
221  * The first entry is the default error return and the rest are the valid
222  * errors for that RPC in increasing numeric order.
223  */
224 static short nfsv3err_null[] = {
225         0,
226         0,
227 };
228
229 static short nfsv3err_getattr[] = {
230         NFSERR_IO,
231         NFSERR_IO,
232         NFSERR_STALE,
233         NFSERR_BADHANDLE,
234         NFSERR_SERVERFAULT,
235         0,
236 };
237
238 static short nfsv3err_setattr[] = {
239         NFSERR_IO,
240         NFSERR_PERM,
241         NFSERR_IO,
242         NFSERR_ACCES,
243         NFSERR_INVAL,
244         NFSERR_NOSPC,
245         NFSERR_ROFS,
246         NFSERR_DQUOT,
247         NFSERR_STALE,
248         NFSERR_BADHANDLE,
249         NFSERR_NOT_SYNC,
250         NFSERR_SERVERFAULT,
251         0,
252 };
253
254 static short nfsv3err_lookup[] = {
255         NFSERR_IO,
256         NFSERR_NOENT,
257         NFSERR_IO,
258         NFSERR_ACCES,
259         NFSERR_NOTDIR,
260         NFSERR_NAMETOL,
261         NFSERR_STALE,
262         NFSERR_BADHANDLE,
263         NFSERR_SERVERFAULT,
264         0,
265 };
266
267 static short nfsv3err_access[] = {
268         NFSERR_IO,
269         NFSERR_IO,
270         NFSERR_STALE,
271         NFSERR_BADHANDLE,
272         NFSERR_SERVERFAULT,
273         0,
274 };
275
276 static short nfsv3err_readlink[] = {
277         NFSERR_IO,
278         NFSERR_IO,
279         NFSERR_ACCES,
280         NFSERR_INVAL,
281         NFSERR_STALE,
282         NFSERR_BADHANDLE,
283         NFSERR_NOTSUPP,
284         NFSERR_SERVERFAULT,
285         0,
286 };
287
288 static short nfsv3err_read[] = {
289         NFSERR_IO,
290         NFSERR_IO,
291         NFSERR_NXIO,
292         NFSERR_ACCES,
293         NFSERR_INVAL,
294         NFSERR_STALE,
295         NFSERR_BADHANDLE,
296         NFSERR_SERVERFAULT,
297         0,
298 };
299
300 static short nfsv3err_write[] = {
301         NFSERR_IO,
302         NFSERR_IO,
303         NFSERR_ACCES,
304         NFSERR_INVAL,
305         NFSERR_FBIG,
306         NFSERR_NOSPC,
307         NFSERR_ROFS,
308         NFSERR_DQUOT,
309         NFSERR_STALE,
310         NFSERR_BADHANDLE,
311         NFSERR_SERVERFAULT,
312         0,
313 };
314
315 static short nfsv3err_create[] = {
316         NFSERR_IO,
317         NFSERR_IO,
318         NFSERR_ACCES,
319         NFSERR_EXIST,
320         NFSERR_NOTDIR,
321         NFSERR_NOSPC,
322         NFSERR_ROFS,
323         NFSERR_NAMETOL,
324         NFSERR_DQUOT,
325         NFSERR_STALE,
326         NFSERR_BADHANDLE,
327         NFSERR_NOTSUPP,
328         NFSERR_SERVERFAULT,
329         0,
330 };
331
332 static short nfsv3err_mkdir[] = {
333         NFSERR_IO,
334         NFSERR_IO,
335         NFSERR_ACCES,
336         NFSERR_EXIST,
337         NFSERR_NOTDIR,
338         NFSERR_NOSPC,
339         NFSERR_ROFS,
340         NFSERR_NAMETOL,
341         NFSERR_DQUOT,
342         NFSERR_STALE,
343         NFSERR_BADHANDLE,
344         NFSERR_NOTSUPP,
345         NFSERR_SERVERFAULT,
346         0,
347 };
348
349 static short nfsv3err_symlink[] = {
350         NFSERR_IO,
351         NFSERR_IO,
352         NFSERR_ACCES,
353         NFSERR_EXIST,
354         NFSERR_NOTDIR,
355         NFSERR_NOSPC,
356         NFSERR_ROFS,
357         NFSERR_NAMETOL,
358         NFSERR_DQUOT,
359         NFSERR_STALE,
360         NFSERR_BADHANDLE,
361         NFSERR_NOTSUPP,
362         NFSERR_SERVERFAULT,
363         0,
364 };
365
366 static short nfsv3err_mknod[] = {
367         NFSERR_IO,
368         NFSERR_IO,
369         NFSERR_ACCES,
370         NFSERR_EXIST,
371         NFSERR_NOTDIR,
372         NFSERR_NOSPC,
373         NFSERR_ROFS,
374         NFSERR_NAMETOL,
375         NFSERR_DQUOT,
376         NFSERR_STALE,
377         NFSERR_BADHANDLE,
378         NFSERR_NOTSUPP,
379         NFSERR_SERVERFAULT,
380         NFSERR_BADTYPE,
381         0,
382 };
383
384 static short nfsv3err_remove[] = {
385         NFSERR_IO,
386         NFSERR_NOENT,
387         NFSERR_IO,
388         NFSERR_ACCES,
389         NFSERR_NOTDIR,
390         NFSERR_ROFS,
391         NFSERR_NAMETOL,
392         NFSERR_STALE,
393         NFSERR_BADHANDLE,
394         NFSERR_SERVERFAULT,
395         0,
396 };
397
398 static short nfsv3err_rmdir[] = {
399         NFSERR_IO,
400         NFSERR_NOENT,
401         NFSERR_IO,
402         NFSERR_ACCES,
403         NFSERR_EXIST,
404         NFSERR_NOTDIR,
405         NFSERR_INVAL,
406         NFSERR_ROFS,
407         NFSERR_NAMETOL,
408         NFSERR_NOTEMPTY,
409         NFSERR_STALE,
410         NFSERR_BADHANDLE,
411         NFSERR_NOTSUPP,
412         NFSERR_SERVERFAULT,
413         0,
414 };
415
416 static short nfsv3err_rename[] = {
417         NFSERR_IO,
418         NFSERR_NOENT,
419         NFSERR_IO,
420         NFSERR_ACCES,
421         NFSERR_EXIST,
422         NFSERR_XDEV,
423         NFSERR_NOTDIR,
424         NFSERR_ISDIR,
425         NFSERR_INVAL,
426         NFSERR_NOSPC,
427         NFSERR_ROFS,
428         NFSERR_MLINK,
429         NFSERR_NAMETOL,
430         NFSERR_NOTEMPTY,
431         NFSERR_DQUOT,
432         NFSERR_STALE,
433         NFSERR_BADHANDLE,
434         NFSERR_NOTSUPP,
435         NFSERR_SERVERFAULT,
436         0,
437 };
438
439 static short nfsv3err_link[] = {
440         NFSERR_IO,
441         NFSERR_IO,
442         NFSERR_ACCES,
443         NFSERR_EXIST,
444         NFSERR_XDEV,
445         NFSERR_NOTDIR,
446         NFSERR_INVAL,
447         NFSERR_NOSPC,
448         NFSERR_ROFS,
449         NFSERR_MLINK,
450         NFSERR_NAMETOL,
451         NFSERR_DQUOT,
452         NFSERR_STALE,
453         NFSERR_BADHANDLE,
454         NFSERR_NOTSUPP,
455         NFSERR_SERVERFAULT,
456         0,
457 };
458
459 static short nfsv3err_readdir[] = {
460         NFSERR_IO,
461         NFSERR_IO,
462         NFSERR_ACCES,
463         NFSERR_NOTDIR,
464         NFSERR_STALE,
465         NFSERR_BADHANDLE,
466         NFSERR_BAD_COOKIE,
467         NFSERR_TOOSMALL,
468         NFSERR_SERVERFAULT,
469         0,
470 };
471
472 static short nfsv3err_readdirplus[] = {
473         NFSERR_IO,
474         NFSERR_IO,
475         NFSERR_ACCES,
476         NFSERR_NOTDIR,
477         NFSERR_STALE,
478         NFSERR_BADHANDLE,
479         NFSERR_BAD_COOKIE,
480         NFSERR_NOTSUPP,
481         NFSERR_TOOSMALL,
482         NFSERR_SERVERFAULT,
483         0,
484 };
485
486 static short nfsv3err_fsstat[] = {
487         NFSERR_IO,
488         NFSERR_IO,
489         NFSERR_STALE,
490         NFSERR_BADHANDLE,
491         NFSERR_SERVERFAULT,
492         0,
493 };
494
495 static short nfsv3err_fsinfo[] = {
496         NFSERR_STALE,
497         NFSERR_STALE,
498         NFSERR_BADHANDLE,
499         NFSERR_SERVERFAULT,
500         0,
501 };
502
503 static short nfsv3err_pathconf[] = {
504         NFSERR_STALE,
505         NFSERR_STALE,
506         NFSERR_BADHANDLE,
507         NFSERR_SERVERFAULT,
508         0,
509 };
510
511 static short nfsv3err_commit[] = {
512         NFSERR_IO,
513         NFSERR_IO,
514         NFSERR_STALE,
515         NFSERR_BADHANDLE,
516         NFSERR_SERVERFAULT,
517         0,
518 };
519
520 static short *nfsrv_v3errmap[] = {
521         nfsv3err_null,
522         nfsv3err_getattr,
523         nfsv3err_setattr,
524         nfsv3err_lookup,
525         nfsv3err_access,
526         nfsv3err_readlink,
527         nfsv3err_read,
528         nfsv3err_write,
529         nfsv3err_create,
530         nfsv3err_mkdir,
531         nfsv3err_symlink,
532         nfsv3err_mknod,
533         nfsv3err_remove,
534         nfsv3err_rmdir,
535         nfsv3err_rename,
536         nfsv3err_link,
537         nfsv3err_readdir,
538         nfsv3err_readdirplus,
539         nfsv3err_fsstat,
540         nfsv3err_fsinfo,
541         nfsv3err_pathconf,
542         nfsv3err_commit,
543 };
544
545 #endif /* NFS_NOSERVER */
546
547 extern struct nfsrtt nfsrtt;
548 extern time_t nqnfsstarttime;
549 extern int nqsrv_clockskew;
550 extern int nqsrv_writeslack;
551 extern int nqsrv_maxlease;
552 extern struct nfsstats nfsstats;
553 extern int nqnfs_piggy[NFS_NPROCS];
554 extern nfstype nfsv2_type[9];
555 extern nfstype nfsv3_type[9];
556 extern struct nfsnodehashhead *nfsnodehashtbl;
557 extern u_long nfsnodehash;
558
559 struct nfssvc_args;
560 extern int nfssvc(struct proc *, struct nfssvc_args *, int *);
561
562 LIST_HEAD(nfsnodehashhead, nfsnode);
563
564 int nfs_webnamei (struct nameidata *, struct vnode *, struct proc *);
565
566 u_quad_t
567 nfs_curusec(void) 
568 {
569         struct timeval tv;
570         
571         getmicrotime(&tv);
572         return ((u_quad_t)tv.tv_sec * 1000000 + (u_quad_t)tv.tv_usec);
573 }
574
575 /*
576  * Create the header for an rpc request packet
577  * The hsiz is the size of the rest of the nfs request header.
578  * (just used to decide if a cluster is a good idea)
579  */
580 struct mbuf *
581 nfsm_reqh(struct vnode *vp, u_long procid, int hsiz, caddr_t *bposp)
582 {
583         struct mbuf *mb;
584         u_int32_t *tl;
585         caddr_t bpos;
586         struct mbuf *mb2;
587         struct nfsmount *nmp;
588         int nqflag;
589
590         MGET(mb, M_WAIT, MT_DATA);
591         if (hsiz >= MINCLSIZE)
592                 MCLGET(mb, M_WAIT);
593         mb->m_len = 0;
594         bpos = mtod(mb, caddr_t);
595
596         /*
597          * For NQNFS, add lease request.
598          */
599         if (vp) {
600                 nmp = VFSTONFS(vp->v_mount);
601                 if (nmp->nm_flag & NFSMNT_NQNFS) {
602                         nqflag = NQNFS_NEEDLEASE(vp, procid);
603                         if (nqflag) {
604                                 nfsm_build(tl, u_int32_t *, 2*NFSX_UNSIGNED);
605                                 *tl++ = txdr_unsigned(nqflag);
606                                 *tl = txdr_unsigned(nmp->nm_leaseterm);
607                         } else {
608                                 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
609                                 *tl = 0;
610                         }
611                 }
612         }
613         /* Finally, return values */
614         *bposp = bpos;
615         return (mb);
616 }
617
618 /*
619  * Build the RPC header and fill in the authorization info.
620  * The authorization string argument is only used when the credentials
621  * come from outside of the kernel.
622  * Returns the head of the mbuf list.
623  */
624 struct mbuf *
625 nfsm_rpchead(struct ucred *cr, int nmflag, int procid, int auth_type,
626              int auth_len, char *auth_str, int verf_len, char *verf_str,
627              struct mbuf *mrest, int mrest_len, struct mbuf **mbp,
628              u_int32_t *xidp)
629 {
630         struct mbuf *mb;
631         u_int32_t *tl;
632         caddr_t bpos;
633         int i;
634         struct mbuf *mreq, *mb2;
635         int siz, grpsiz, authsiz;
636
637         authsiz = nfsm_rndup(auth_len);
638         MGETHDR(mb, M_WAIT, MT_DATA);
639         if ((authsiz + 10 * NFSX_UNSIGNED) >= MINCLSIZE) {
640                 MCLGET(mb, M_WAIT);
641         } else if ((authsiz + 10 * NFSX_UNSIGNED) < MHLEN) {
642                 MH_ALIGN(mb, authsiz + 10 * NFSX_UNSIGNED);
643         } else {
644                 MH_ALIGN(mb, 8 * NFSX_UNSIGNED);
645         }
646         mb->m_len = 0;
647         mreq = mb;
648         bpos = mtod(mb, caddr_t);
649
650         /*
651          * First the RPC header.
652          */
653         nfsm_build(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
654
655         /* Get a pretty random xid to start with */
656         if (!nfs_xid) 
657                 nfs_xid = random();
658         /*
659          * Skip zero xid if it should ever happen.
660          */
661         if (++nfs_xid == 0)
662                 nfs_xid++;
663
664         *tl++ = *xidp = txdr_unsigned(nfs_xid);
665         *tl++ = rpc_call;
666         *tl++ = rpc_vers;
667         if (nmflag & NFSMNT_NQNFS) {
668                 *tl++ = txdr_unsigned(NQNFS_PROG);
669                 *tl++ = txdr_unsigned(NQNFS_VER3);
670         } else {
671                 *tl++ = txdr_unsigned(NFS_PROG);
672                 if (nmflag & NFSMNT_NFSV3)
673                         *tl++ = txdr_unsigned(NFS_VER3);
674                 else
675                         *tl++ = txdr_unsigned(NFS_VER2);
676         }
677         if (nmflag & NFSMNT_NFSV3)
678                 *tl++ = txdr_unsigned(procid);
679         else
680                 *tl++ = txdr_unsigned(nfsv2_procid[procid]);
681
682         /*
683          * And then the authorization cred.
684          */
685         *tl++ = txdr_unsigned(auth_type);
686         *tl = txdr_unsigned(authsiz);
687         switch (auth_type) {
688         case RPCAUTH_UNIX:
689                 nfsm_build(tl, u_int32_t *, auth_len);
690                 *tl++ = 0;              /* stamp ?? */
691                 *tl++ = 0;              /* NULL hostname */
692                 *tl++ = txdr_unsigned(cr->cr_uid);
693                 *tl++ = txdr_unsigned(cr->cr_groups[0]);
694                 grpsiz = (auth_len >> 2) - 5;
695                 *tl++ = txdr_unsigned(grpsiz);
696                 for (i = 1; i <= grpsiz; i++)
697                         *tl++ = txdr_unsigned(cr->cr_groups[i]);
698                 break;
699         case RPCAUTH_KERB4:
700                 siz = auth_len;
701                 while (siz > 0) {
702                         if (M_TRAILINGSPACE(mb) == 0) {
703                                 MGET(mb2, M_WAIT, MT_DATA);
704                                 if (siz >= MINCLSIZE)
705                                         MCLGET(mb2, M_WAIT);
706                                 mb->m_next = mb2;
707                                 mb = mb2;
708                                 mb->m_len = 0;
709                                 bpos = mtod(mb, caddr_t);
710                         }
711                         i = min(siz, M_TRAILINGSPACE(mb));
712                         bcopy(auth_str, bpos, i);
713                         mb->m_len += i;
714                         auth_str += i;
715                         bpos += i;
716                         siz -= i;
717                 }
718                 if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) {
719                         for (i = 0; i < siz; i++)
720                                 *bpos++ = '\0';
721                         mb->m_len += siz;
722                 }
723                 break;
724         };
725
726         /*
727          * And the verifier...
728          */
729         nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
730         if (verf_str) {
731                 *tl++ = txdr_unsigned(RPCAUTH_KERB4);
732                 *tl = txdr_unsigned(verf_len);
733                 siz = verf_len;
734                 while (siz > 0) {
735                         if (M_TRAILINGSPACE(mb) == 0) {
736                                 MGET(mb2, M_WAIT, MT_DATA);
737                                 if (siz >= MINCLSIZE)
738                                         MCLGET(mb2, M_WAIT);
739                                 mb->m_next = mb2;
740                                 mb = mb2;
741                                 mb->m_len = 0;
742                                 bpos = mtod(mb, caddr_t);
743                         }
744                         i = min(siz, M_TRAILINGSPACE(mb));
745                         bcopy(verf_str, bpos, i);
746                         mb->m_len += i;
747                         verf_str += i;
748                         bpos += i;
749                         siz -= i;
750                 }
751                 if ((siz = (nfsm_rndup(verf_len) - verf_len)) > 0) {
752                         for (i = 0; i < siz; i++)
753                                 *bpos++ = '\0';
754                         mb->m_len += siz;
755                 }
756         } else {
757                 *tl++ = txdr_unsigned(RPCAUTH_NULL);
758                 *tl = 0;
759         }
760         mb->m_next = mrest;
761         mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len;
762         mreq->m_pkthdr.rcvif = (struct ifnet *)0;
763         *mbp = mb;
764         return (mreq);
765 }
766
767 /*
768  * copies mbuf chain to the uio scatter/gather list
769  */
770 int
771 nfsm_mbuftouio(struct mbuf **mrep, struct uio *uiop, int siz, caddr_t *dpos)
772 {
773         char *mbufcp, *uiocp;
774         int xfer, left, len;
775         struct mbuf *mp;
776         long uiosiz, rem;
777         int error = 0;
778
779         mp = *mrep;
780         mbufcp = *dpos;
781         len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
782         rem = nfsm_rndup(siz)-siz;
783         while (siz > 0) {
784                 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
785                         return (EFBIG);
786                 left = uiop->uio_iov->iov_len;
787                 uiocp = uiop->uio_iov->iov_base;
788                 if (left > siz)
789                         left = siz;
790                 uiosiz = left;
791                 while (left > 0) {
792                         while (len == 0) {
793                                 mp = mp->m_next;
794                                 if (mp == NULL)
795                                         return (EBADRPC);
796                                 mbufcp = mtod(mp, caddr_t);
797                                 len = mp->m_len;
798                         }
799                         xfer = (left > len) ? len : left;
800 #ifdef notdef
801                         /* Not Yet.. */
802                         if (uiop->uio_iov->iov_op != NULL)
803                                 (*(uiop->uio_iov->iov_op))
804                                 (mbufcp, uiocp, xfer);
805                         else
806 #endif
807                         if (uiop->uio_segflg == UIO_SYSSPACE)
808                                 bcopy(mbufcp, uiocp, xfer);
809                         else
810                                 copyout(mbufcp, uiocp, xfer);
811                         left -= xfer;
812                         len -= xfer;
813                         mbufcp += xfer;
814                         uiocp += xfer;
815                         uiop->uio_offset += xfer;
816                         uiop->uio_resid -= xfer;
817                 }
818                 if (uiop->uio_iov->iov_len <= siz) {
819                         uiop->uio_iovcnt--;
820                         uiop->uio_iov++;
821                 } else {
822                         uiop->uio_iov->iov_base += uiosiz;
823                         uiop->uio_iov->iov_len -= uiosiz;
824                 }
825                 siz -= uiosiz;
826         }
827         *dpos = mbufcp;
828         *mrep = mp;
829         if (rem > 0) {
830                 if (len < rem)
831                         error = nfs_adv(mrep, dpos, rem, len);
832                 else
833                         *dpos += rem;
834         }
835         return (error);
836 }
837
838 /*
839  * copies a uio scatter/gather list to an mbuf chain.
840  * NOTE: can ony handle iovcnt == 1
841  */
842 int
843 nfsm_uiotombuf(struct uio *uiop, struct mbuf **mq, int siz, caddr_t *bpos)
844 {
845         char *uiocp;
846         struct mbuf *mp, *mp2;
847         int xfer, left, mlen;
848         int uiosiz, clflg, rem;
849         char *cp;
850
851 #ifdef DIAGNOSTIC
852         if (uiop->uio_iovcnt != 1)
853                 panic("nfsm_uiotombuf: iovcnt != 1");
854 #endif
855
856         if (siz > MLEN)         /* or should it >= MCLBYTES ?? */
857                 clflg = 1;
858         else
859                 clflg = 0;
860         rem = nfsm_rndup(siz)-siz;
861         mp = mp2 = *mq;
862         while (siz > 0) {
863                 left = uiop->uio_iov->iov_len;
864                 uiocp = uiop->uio_iov->iov_base;
865                 if (left > siz)
866                         left = siz;
867                 uiosiz = left;
868                 while (left > 0) {
869                         mlen = M_TRAILINGSPACE(mp);
870                         if (mlen == 0) {
871                                 MGET(mp, M_WAIT, MT_DATA);
872                                 if (clflg)
873                                         MCLGET(mp, M_WAIT);
874                                 mp->m_len = 0;
875                                 mp2->m_next = mp;
876                                 mp2 = mp;
877                                 mlen = M_TRAILINGSPACE(mp);
878                         }
879                         xfer = (left > mlen) ? mlen : left;
880 #ifdef notdef
881                         /* Not Yet.. */
882                         if (uiop->uio_iov->iov_op != NULL)
883                                 (*(uiop->uio_iov->iov_op))
884                                 (uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
885                         else
886 #endif
887                         if (uiop->uio_segflg == UIO_SYSSPACE)
888                                 bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
889                         else
890                                 copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
891                         mp->m_len += xfer;
892                         left -= xfer;
893                         uiocp += xfer;
894                         uiop->uio_offset += xfer;
895                         uiop->uio_resid -= xfer;
896                 }
897                 uiop->uio_iov->iov_base += uiosiz;
898                 uiop->uio_iov->iov_len -= uiosiz;
899                 siz -= uiosiz;
900         }
901         if (rem > 0) {
902                 if (rem > M_TRAILINGSPACE(mp)) {
903                         MGET(mp, M_WAIT, MT_DATA);
904                         mp->m_len = 0;
905                         mp2->m_next = mp;
906                 }
907                 cp = mtod(mp, caddr_t)+mp->m_len;
908                 for (left = 0; left < rem; left++)
909                         *cp++ = '\0';
910                 mp->m_len += rem;
911                 *bpos = cp;
912         } else
913                 *bpos = mtod(mp, caddr_t)+mp->m_len;
914         *mq = mp;
915         return (0);
916 }
917
918 /*
919  * Help break down an mbuf chain by setting the first siz bytes contiguous
920  * pointed to by returned val.
921  * This is used by the macros nfsm_dissect and nfsm_dissecton for tough
922  * cases. (The macros use the vars. dpos and dpos2)
923  */
924 int
925 nfsm_disct(struct mbuf **mdp, caddr_t *dposp, int siz, int left, caddr_t *cp2)
926 {
927         struct mbuf *mp, *mp2;
928         int siz2, xfer;
929         caddr_t p;
930
931         mp = *mdp;
932         while (left == 0) {
933                 *mdp = mp = mp->m_next;
934                 if (mp == NULL)
935                         return (EBADRPC);
936                 left = mp->m_len;
937                 *dposp = mtod(mp, caddr_t);
938         }
939         if (left >= siz) {
940                 *cp2 = *dposp;
941                 *dposp += siz;
942         } else if (mp->m_next == NULL) {
943                 return (EBADRPC);
944         } else if (siz > MHLEN) {
945                 panic("nfs S too big");
946         } else {
947                 MGET(mp2, M_WAIT, MT_DATA);
948                 mp2->m_next = mp->m_next;
949                 mp->m_next = mp2;
950                 mp->m_len -= left;
951                 mp = mp2;
952                 *cp2 = p = mtod(mp, caddr_t);
953                 bcopy(*dposp, p, left);         /* Copy what was left */
954                 siz2 = siz-left;
955                 p += left;
956                 mp2 = mp->m_next;
957                 /* Loop around copying up the siz2 bytes */
958                 while (siz2 > 0) {
959                         if (mp2 == NULL)
960                                 return (EBADRPC);
961                         xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
962                         if (xfer > 0) {
963                                 bcopy(mtod(mp2, caddr_t), p, xfer);
964                                 NFSMADV(mp2, xfer);
965                                 mp2->m_len -= xfer;
966                                 p += xfer;
967                                 siz2 -= xfer;
968                         }
969                         if (siz2 > 0)
970                                 mp2 = mp2->m_next;
971                 }
972                 mp->m_len = siz;
973                 *mdp = mp2;
974                 *dposp = mtod(mp2, caddr_t);
975         }
976         return (0);
977 }
978
979 /*
980  * Advance the position in the mbuf chain.
981  */
982 int
983 nfs_adv(struct mbuf **mdp, caddr_t *dposp, int offs, int left)
984 {
985         struct mbuf *m;
986         int s;
987
988         m = *mdp;
989         s = left;
990         while (s < offs) {
991                 offs -= s;
992                 m = m->m_next;
993                 if (m == NULL)
994                         return (EBADRPC);
995                 s = m->m_len;
996         }
997         *mdp = m;
998         *dposp = mtod(m, caddr_t)+offs;
999         return (0);
1000 }
1001
1002 /*
1003  * Copy a string into mbufs for the hard cases...
1004  */
1005 int
1006 nfsm_strtmbuf(struct mbuf **mb, char **bpos, const char *cp, long siz)
1007 {
1008         struct mbuf *m1 = NULL, *m2;
1009         long left, xfer, len, tlen;
1010         u_int32_t *tl;
1011         int putsize;
1012
1013         putsize = 1;
1014         m2 = *mb;
1015         left = M_TRAILINGSPACE(m2);
1016         if (left > 0) {
1017                 tl = ((u_int32_t *)(*bpos));
1018                 *tl++ = txdr_unsigned(siz);
1019                 putsize = 0;
1020                 left -= NFSX_UNSIGNED;
1021                 m2->m_len += NFSX_UNSIGNED;
1022                 if (left > 0) {
1023                         bcopy(cp, (caddr_t) tl, left);
1024                         siz -= left;
1025                         cp += left;
1026                         m2->m_len += left;
1027                         left = 0;
1028                 }
1029         }
1030         /* Loop around adding mbufs */
1031         while (siz > 0) {
1032                 MGET(m1, M_WAIT, MT_DATA);
1033                 if (siz > MLEN)
1034                         MCLGET(m1, M_WAIT);
1035                 m1->m_len = NFSMSIZ(m1);
1036                 m2->m_next = m1;
1037                 m2 = m1;
1038                 tl = mtod(m1, u_int32_t *);
1039                 tlen = 0;
1040                 if (putsize) {
1041                         *tl++ = txdr_unsigned(siz);
1042                         m1->m_len -= NFSX_UNSIGNED;
1043                         tlen = NFSX_UNSIGNED;
1044                         putsize = 0;
1045                 }
1046                 if (siz < m1->m_len) {
1047                         len = nfsm_rndup(siz);
1048                         xfer = siz;
1049                         if (xfer < len)
1050                                 *(tl+(xfer>>2)) = 0;
1051                 } else {
1052                         xfer = len = m1->m_len;
1053                 }
1054                 bcopy(cp, (caddr_t) tl, xfer);
1055                 m1->m_len = len+tlen;
1056                 siz -= xfer;
1057                 cp += xfer;
1058         }
1059         *mb = m1;
1060         *bpos = mtod(m1, caddr_t)+m1->m_len;
1061         return (0);
1062 }
1063
1064 /*
1065  * Called once to initialize data structures...
1066  */
1067 int
1068 nfs_init(struct vfsconf *vfsp)
1069 {
1070         int i;
1071
1072         nfsmount_zone = zinit("NFSMOUNT", sizeof(struct nfsmount), 0, 0, 1);
1073
1074         nfs_mount_type = vfsp->vfc_typenum;
1075         nfsrtt.pos = 0;
1076         rpc_vers = txdr_unsigned(RPC_VER2);
1077         rpc_call = txdr_unsigned(RPC_CALL);
1078         rpc_reply = txdr_unsigned(RPC_REPLY);
1079         rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
1080         rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
1081         rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
1082         rpc_autherr = txdr_unsigned(RPC_AUTHERR);
1083         rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
1084         rpc_auth_kerb = txdr_unsigned(RPCAUTH_KERB4);
1085         nfs_prog = txdr_unsigned(NFS_PROG);
1086         nqnfs_prog = txdr_unsigned(NQNFS_PROG);
1087         nfs_true = txdr_unsigned(TRUE);
1088         nfs_false = txdr_unsigned(FALSE);
1089         nfs_xdrneg1 = txdr_unsigned(-1);
1090         nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
1091         if (nfs_ticks < 1)
1092                 nfs_ticks = 1;
1093         /* Ensure async daemons disabled */
1094         for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
1095                 nfs_iodwant[i] = NULL;
1096                 nfs_iodmount[i] = (struct nfsmount *)0;
1097         }
1098         nfs_nhinit();                   /* Init the nfsnode table */
1099 #ifndef NFS_NOSERVER
1100         nfsrv_init(0);                  /* Init server data structures */
1101         nfsrv_initcache();              /* Init the server request cache */
1102 #endif
1103
1104         /*
1105          * Initialize the nqnfs server stuff.
1106          */
1107         if (nqnfsstarttime == 0) {
1108                 nqnfsstarttime = boottime.tv_sec + nqsrv_maxlease
1109                         + nqsrv_clockskew + nqsrv_writeslack;
1110                 NQLOADNOVRAM(nqnfsstarttime);
1111                 CIRCLEQ_INIT(&nqtimerhead);
1112                 nqfhhashtbl = hashinit(NQLCHSZ, M_NQLEASE, &nqfhhash);
1113         }
1114
1115         /*
1116          * Initialize reply list and start timer
1117          */
1118         TAILQ_INIT(&nfs_reqq);
1119
1120         nfs_timer(0);
1121
1122         /*
1123          * Set up lease_check and lease_updatetime so that other parts
1124          * of the system can call us, if we are loadable.
1125          */
1126 #ifndef NFS_NOSERVER
1127         nfs_prev_vop_lease_check = default_vnodeop_p[VOFFSET(vop_lease)];
1128         default_vnodeop_p[VOFFSET(vop_lease)] = (vop_t *)nqnfs_vop_lease_check;
1129 #endif
1130         nfs_prev_lease_updatetime = lease_updatetime;
1131         lease_updatetime = nfs_lease_updatetime;
1132         nfs_prev_nfssvc_sy_narg = sysent[SYS_nfssvc].sy_narg;
1133         sysent[SYS_nfssvc].sy_narg = 2;
1134         nfs_prev_nfssvc_sy_call = sysent[SYS_nfssvc].sy_call;
1135         sysent[SYS_nfssvc].sy_call = (sy_call_t *)nfssvc;
1136
1137         nfs_pbuf_freecnt = nswbuf / 2 + 1;
1138
1139         return (0);
1140 }
1141
1142 int
1143 nfs_uninit(struct vfsconf *vfsp)
1144 {
1145
1146         untimeout(nfs_timer, (void *)NULL, nfs_timer_handle);
1147         nfs_mount_type = -1;
1148 #ifndef NFS_NOSERVER
1149         default_vnodeop_p[VOFFSET(vop_lease)] = nfs_prev_vop_lease_check;
1150 #endif
1151         lease_updatetime = nfs_prev_lease_updatetime;
1152         sysent[SYS_nfssvc].sy_narg = nfs_prev_nfssvc_sy_narg;
1153         sysent[SYS_nfssvc].sy_call = nfs_prev_nfssvc_sy_call;
1154         return (0);
1155 }
1156
1157 /*
1158  * Attribute cache routines.
1159  * nfs_loadattrcache() - loads or updates the cache contents from attributes
1160  *      that are on the mbuf list
1161  * nfs_getattrcache() - returns valid attributes if found in cache, returns
1162  *      error otherwise
1163  */
1164
1165 /*
1166  * Load the attribute cache (that lives in the nfsnode entry) with
1167  * the values on the mbuf list and
1168  * Iff vap not NULL
1169  *    copy the attributes to *vaper
1170  */
1171 int
1172 nfs_loadattrcache(struct vnode **vpp, struct mbuf **mdp, caddr_t *dposp,
1173                   struct vattr *vaper, int dontshrink)
1174 {
1175         struct vnode *vp = *vpp;
1176         struct vattr *vap;
1177         struct nfs_fattr *fp;
1178         struct nfsnode *np;
1179         int32_t t1;
1180         caddr_t cp2;
1181         int error = 0, rdev;
1182         struct mbuf *md;
1183         enum vtype vtyp;
1184         u_short vmode;
1185         struct timespec mtime;
1186         int v3 = NFS_ISV3(vp);
1187
1188         md = *mdp;
1189         t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
1190         if ((error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2)) != 0)
1191                 return (error);
1192         fp = (struct nfs_fattr *)cp2;
1193         if (v3) {
1194                 vtyp = nfsv3tov_type(fp->fa_type);
1195                 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1196                 rdev = makeudev(fxdr_unsigned(int, fp->fa3_rdev.specdata1),
1197                         fxdr_unsigned(int, fp->fa3_rdev.specdata2));
1198                 fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
1199         } else {
1200                 vtyp = nfsv2tov_type(fp->fa_type);
1201                 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1202                 /*
1203                  * XXX
1204                  *
1205                  * The duplicate information returned in fa_type and fa_mode
1206                  * is an ambiguity in the NFS version 2 protocol.
1207                  *
1208                  * VREG should be taken literally as a regular file.  If a
1209                  * server intents to return some type information differently
1210                  * in the upper bits of the mode field (e.g. for sockets, or
1211                  * FIFOs), NFSv2 mandates fa_type to be VNON.  Anyway, we
1212                  * leave the examination of the mode bits even in the VREG
1213                  * case to avoid breakage for bogus servers, but we make sure
1214                  * that there are actually type bits set in the upper part of
1215                  * fa_mode (and failing that, trust the va_type field).
1216                  *
1217                  * NFSv3 cleared the issue, and requires fa_mode to not
1218                  * contain any type information (while also introduing sockets
1219                  * and FIFOs for fa_type).
1220                  */
1221                 if (vtyp == VNON || (vtyp == VREG && (vmode & S_IFMT) != 0))
1222                         vtyp = IFTOVT(vmode);
1223                 rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
1224                 fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
1225
1226                 /*
1227                  * Really ugly NFSv2 kludge.
1228                  */
1229                 if (vtyp == VCHR && rdev == 0xffffffff)
1230                         vtyp = VFIFO;
1231         }
1232
1233         /*
1234          * If v_type == VNON it is a new node, so fill in the v_type,
1235          * n_mtime fields. Check to see if it represents a special
1236          * device, and if so, check for a possible alias. Once the
1237          * correct vnode has been obtained, fill in the rest of the
1238          * information.
1239          */
1240         np = VTONFS(vp);
1241         if (vp->v_type != vtyp) {
1242                 vp->v_type = vtyp;
1243                 if (vp->v_type == VFIFO) {
1244                         vp->v_op = fifo_nfsv2nodeop_p;
1245                 }
1246                 if (vp->v_type == VCHR || vp->v_type == VBLK) {
1247                         vp->v_op = spec_nfsv2nodeop_p;
1248                         addaliasu(vp, rdev);
1249                 }
1250                 np->n_mtime = mtime.tv_sec;
1251         }
1252         vap = &np->n_vattr;
1253         vap->va_type = vtyp;
1254         vap->va_mode = (vmode & 07777);
1255         vap->va_rdev = rdev;
1256         vap->va_mtime = mtime;
1257         vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1258         if (v3) {
1259                 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1260                 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1261                 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1262                 vap->va_size = fxdr_hyper(&fp->fa3_size);
1263                 vap->va_blocksize = NFS_FABLKSIZE;
1264                 vap->va_bytes = fxdr_hyper(&fp->fa3_used);
1265                 vap->va_fileid = fxdr_unsigned(int32_t,
1266                     fp->fa3_fileid.nfsuquad[1]);
1267                 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
1268                 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
1269                 vap->va_flags = 0;
1270                 vap->va_filerev = 0;
1271         } else {
1272                 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1273                 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1274                 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1275                 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
1276                 vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
1277                 vap->va_bytes = (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks)
1278                     * NFS_FABLKSIZE;
1279                 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
1280                 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
1281                 vap->va_flags = 0;
1282                 vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
1283                     fp->fa2_ctime.nfsv2_sec);
1284                 vap->va_ctime.tv_nsec = 0;
1285                 vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec);
1286                 vap->va_filerev = 0;
1287         }
1288         np->n_attrstamp = time_second;
1289         if (vap->va_size != np->n_size) {
1290                 if (vap->va_type == VREG) {
1291                         if (dontshrink && vap->va_size < np->n_size) {
1292                                 /*
1293                                  * We've been told not to shrink the file;
1294                                  * zero np->n_attrstamp to indicate that
1295                                  * the attributes are stale.
1296                                  */
1297                                 vap->va_size = np->n_size;
1298                                 np->n_attrstamp = 0;
1299                         } else if (np->n_flag & NMODIFIED) {
1300                                 /*
1301                                  * We've modified the file: Use the larger
1302                                  * of our size, and the server's size.
1303                                  */
1304                                 if (vap->va_size < np->n_size) {
1305                                         vap->va_size = np->n_size;
1306                                 } else {
1307                                         np->n_size = vap->va_size;
1308                                         np->n_flag |= NSIZECHANGED;
1309                                 }
1310                         } else {
1311                                 np->n_size = vap->va_size;
1312                                 np->n_flag |= NSIZECHANGED;
1313                         }
1314                         vnode_pager_setsize(vp, np->n_size);
1315                 } else {
1316                         np->n_size = vap->va_size;
1317                 }
1318         }
1319         if (vaper != NULL) {
1320                 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
1321                 if (np->n_flag & NCHG) {
1322                         if (np->n_flag & NACC)
1323                                 vaper->va_atime = np->n_atim;
1324                         if (np->n_flag & NUPD)
1325                                 vaper->va_mtime = np->n_mtim;
1326                 }
1327         }
1328         return (0);
1329 }
1330
1331 #ifdef NFS_ACDEBUG
1332 #include <sys/sysctl.h>
1333 SYSCTL_DECL(_vfs_nfs);
1334 static int nfs_acdebug;
1335 SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, "");
1336 #endif
1337
1338 /*
1339  * Check the time stamp
1340  * If the cache is valid, copy contents to *vap and return 0
1341  * otherwise return an error
1342  */
1343 int
1344 nfs_getattrcache(struct vnode *vp, struct vattr *vaper)
1345 {
1346         struct nfsnode *np;
1347         struct vattr *vap;
1348         struct nfsmount *nmp;
1349         int timeo;
1350
1351         np = VTONFS(vp);
1352         vap = &np->n_vattr;
1353         nmp = VFSTONFS(vp->v_mount);
1354         /* XXX n_mtime doesn't seem to be updated on a miss-and-reload */
1355         timeo = (time_second - np->n_mtime) / 10;
1356
1357 #ifdef NFS_ACDEBUG
1358         if (nfs_acdebug>1)
1359                 printf("nfs_getattrcache: initial timeo = %d\n", timeo);
1360 #endif
1361
1362         if (vap->va_type == VDIR) {
1363                 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin)
1364                         timeo = nmp->nm_acdirmin;
1365                 else if (timeo > nmp->nm_acdirmax)
1366                         timeo = nmp->nm_acdirmax;
1367         } else {
1368                 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin)
1369                         timeo = nmp->nm_acregmin;
1370                 else if (timeo > nmp->nm_acregmax)
1371                         timeo = nmp->nm_acregmax;
1372         }
1373
1374 #ifdef NFS_ACDEBUG
1375         if (nfs_acdebug > 2)
1376                 printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
1377                         nmp->nm_acregmin, nmp->nm_acregmax,
1378                         nmp->nm_acdirmin, nmp->nm_acdirmax);
1379
1380         if (nfs_acdebug)
1381                 printf("nfs_getattrcache: age = %d; final timeo = %d\n",
1382                         (time_second - np->n_attrstamp), timeo);
1383 #endif
1384
1385         if ((time_second - np->n_attrstamp) >= timeo) {
1386                 nfsstats.attrcache_misses++;
1387                 return (ENOENT);
1388         }
1389         nfsstats.attrcache_hits++;
1390         if (vap->va_size != np->n_size) {
1391                 if (vap->va_type == VREG) {
1392                         if (np->n_flag & NMODIFIED) {
1393                                 if (vap->va_size < np->n_size)
1394                                         vap->va_size = np->n_size;
1395                                 else
1396                                         np->n_size = vap->va_size;
1397                         } else {
1398                                 np->n_size = vap->va_size;
1399                         }
1400                         vnode_pager_setsize(vp, np->n_size);
1401                 } else {
1402                         np->n_size = vap->va_size;
1403                 }
1404         }
1405         bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
1406         if (np->n_flag & NCHG) {
1407                 if (np->n_flag & NACC)
1408                         vaper->va_atime = np->n_atim;
1409                 if (np->n_flag & NUPD)
1410                         vaper->va_mtime = np->n_mtim;
1411         }
1412         return (0);
1413 }
1414
1415 #ifndef NFS_NOSERVER
1416 /*
1417  * Set up nameidata for a lookup() call and do it.
1418  *
1419  * If pubflag is set, this call is done for a lookup operation on the
1420  * public filehandle. In that case we allow crossing mountpoints and
1421  * absolute pathnames. However, the caller is expected to check that
1422  * the lookup result is within the public fs, and deny access if
1423  * it is not.
1424  *
1425  * nfs_namei() clears out garbage fields that namei() might leave garbage.
1426  * This is mainly ni_vp and ni_dvp when an error occurs, and ni_dvp when no
1427  * error occurs but the parent was not requested.
1428  *
1429  * dirp may be set whether an error is returned or not, and must be 
1430  * released by the caller.
1431  */
1432 int
1433 nfs_namei(struct nameidata *ndp, fhandle_t *fhp, int len,
1434           struct nfssvc_sock *slp, struct sockaddr *nam, struct mbuf **mdp,
1435           caddr_t *dposp, struct vnode **retdirp, struct thread *td,
1436           int kerbflag, int pubflag)
1437 {
1438         int i, rem;
1439         struct mbuf *md;
1440         char *fromcp, *tocp, *cp;
1441         struct iovec aiov;
1442         struct uio auio;
1443         struct vnode *dp;
1444         int error, rdonly, linklen;
1445         struct componentname *cnp = &ndp->ni_cnd;
1446
1447         *retdirp = (struct vnode *)0;
1448         cnp->cn_pnbuf = zalloc(namei_zone);
1449
1450         /*
1451          * Copy the name from the mbuf list to ndp->ni_pnbuf
1452          * and set the various ndp fields appropriately.
1453          */
1454         fromcp = *dposp;
1455         tocp = cnp->cn_pnbuf;
1456         md = *mdp;
1457         rem = mtod(md, caddr_t) + md->m_len - fromcp;
1458         for (i = 0; i < len; i++) {
1459                 while (rem == 0) {
1460                         md = md->m_next;
1461                         if (md == NULL) {
1462                                 error = EBADRPC;
1463                                 goto out;
1464                         }
1465                         fromcp = mtod(md, caddr_t);
1466                         rem = md->m_len;
1467                 }
1468                 if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
1469                         error = EACCES;
1470                         goto out;
1471                 }
1472                 *tocp++ = *fromcp++;
1473                 rem--;
1474         }
1475         *tocp = '\0';
1476         *mdp = md;
1477         *dposp = fromcp;
1478         len = nfsm_rndup(len)-len;
1479         if (len > 0) {
1480                 if (rem >= len)
1481                         *dposp += len;
1482                 else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
1483                         goto out;
1484         }
1485
1486         /*
1487          * Extract and set starting directory.
1488          */
1489         error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp,
1490             nam, &rdonly, kerbflag, pubflag);
1491         if (error)
1492                 goto out;
1493         if (dp->v_type != VDIR) {
1494                 vrele(dp);
1495                 error = ENOTDIR;
1496                 goto out;
1497         }
1498
1499         if (rdonly)
1500                 cnp->cn_flags |= CNP_RDONLY;
1501
1502         /*
1503          * Set return directory.  Reference to dp is implicitly transfered 
1504          * to the returned pointer
1505          */
1506         *retdirp = dp;
1507
1508         if (pubflag) {
1509                 /*
1510                  * Oh joy. For WebNFS, handle those pesky '%' escapes,
1511                  * and the 'native path' indicator.
1512                  */
1513                 cp = zalloc(namei_zone);
1514                 fromcp = cnp->cn_pnbuf;
1515                 tocp = cp;
1516                 if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
1517                         switch ((unsigned char)*fromcp) {
1518                         case WEBNFS_NATIVE_CHAR:
1519                                 /*
1520                                  * 'Native' path for us is the same
1521                                  * as a path according to the NFS spec,
1522                                  * just skip the escape char.
1523                                  */
1524                                 fromcp++;
1525                                 break;
1526                         /*
1527                          * More may be added in the future, range 0x80-0xff
1528                          */
1529                         default:
1530                                 error = EIO;
1531                                 zfree(namei_zone, cp);
1532                                 goto out;
1533                         }
1534                 }
1535                 /*
1536                  * Translate the '%' escapes, URL-style.
1537                  */
1538                 while (*fromcp != '\0') {
1539                         if (*fromcp == WEBNFS_ESC_CHAR) {
1540                                 if (fromcp[1] != '\0' && fromcp[2] != '\0') {
1541                                         fromcp++;
1542                                         *tocp++ = HEXSTRTOI(fromcp);
1543                                         fromcp += 2;
1544                                         continue;
1545                                 } else {
1546                                         error = ENOENT;
1547                                         zfree(namei_zone, cp);
1548                                         goto out;
1549                                 }
1550                         } else
1551                                 *tocp++ = *fromcp++;
1552                 }
1553                 *tocp = '\0';
1554                 zfree(namei_zone, cnp->cn_pnbuf);
1555                 cnp->cn_pnbuf = cp;
1556         }
1557
1558         ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1;
1559         ndp->ni_segflg = UIO_SYSSPACE;
1560
1561         if (pubflag) {
1562                 ndp->ni_rootdir = rootvnode;
1563                 ndp->ni_loopcnt = 0;
1564                 if (cnp->cn_pnbuf[0] == '/')
1565                         dp = rootvnode;
1566         } else {
1567                 cnp->cn_flags |= CNP_NOCROSSMOUNT;
1568         }
1569
1570         /*
1571          * Initialize for scan, set ni_startdir and bump ref on dp again
1572          * because lookup() will dereference ni_startdir.
1573          */
1574
1575         cnp->cn_td = td;
1576         vref(dp);
1577         ndp->ni_startdir = dp;
1578
1579         for (;;) {
1580                 cnp->cn_nameptr = cnp->cn_pnbuf;
1581                 /*
1582                  * Call lookup() to do the real work.  If an error occurs,
1583                  * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
1584                  * we do not have to dereference anything before returning.
1585                  * In either case ni_startdir will be dereferenced and NULLed
1586                  * out.
1587                  */
1588                 error = lookup(ndp);
1589                 if (error)
1590                         break;
1591
1592                 /*
1593                  * Check for encountering a symbolic link.  Trivial 
1594                  * termination occurs if no symlink encountered.
1595                  * Note: zfree is safe because error is 0, so we will
1596                  * not zfree it again when we break.
1597                  */
1598                 if ((cnp->cn_flags & CNP_ISSYMLINK) == 0) {
1599                         nfsrv_object_create(ndp->ni_vp);
1600                         if (cnp->cn_flags & (CNP_SAVENAME | CNP_SAVESTART))
1601                                 cnp->cn_flags |= CNP_HASBUF;
1602                         else
1603                                 zfree(namei_zone, cnp->cn_pnbuf);
1604                         break;
1605                 }
1606
1607                 /*
1608                  * Validate symlink
1609                  */
1610                 if ((cnp->cn_flags & CNP_LOCKPARENT) && ndp->ni_pathlen == 1)
1611                         VOP_UNLOCK(ndp->ni_dvp, NULL, 0, td);
1612                 if (!pubflag) {
1613                         error = EINVAL;
1614                         goto badlink2;
1615                 }
1616
1617                 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
1618                         error = ELOOP;
1619                         goto badlink2;
1620                 }
1621                 if (ndp->ni_pathlen > 1)
1622                         cp = zalloc(namei_zone);
1623                 else
1624                         cp = cnp->cn_pnbuf;
1625                 aiov.iov_base = cp;
1626                 aiov.iov_len = MAXPATHLEN;
1627                 auio.uio_iov = &aiov;
1628                 auio.uio_iovcnt = 1;
1629                 auio.uio_offset = 0;
1630                 auio.uio_rw = UIO_READ;
1631                 auio.uio_segflg = UIO_SYSSPACE;
1632                 auio.uio_td = NULL;
1633                 auio.uio_resid = MAXPATHLEN;
1634                 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
1635                 if (error) {
1636                 badlink1:
1637                         if (ndp->ni_pathlen > 1)
1638                                 zfree(namei_zone, cp);
1639                 badlink2:
1640                         vrele(ndp->ni_dvp);
1641                         vput(ndp->ni_vp);
1642                         break;
1643                 }
1644                 linklen = MAXPATHLEN - auio.uio_resid;
1645                 if (linklen == 0) {
1646                         error = ENOENT;
1647                         goto badlink1;
1648                 }
1649                 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
1650                         error = ENAMETOOLONG;
1651                         goto badlink1;
1652                 }
1653
1654                 /*
1655                  * Adjust or replace path
1656                  */
1657                 if (ndp->ni_pathlen > 1) {
1658                         bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
1659                         zfree(namei_zone, cnp->cn_pnbuf);
1660                         cnp->cn_pnbuf = cp;
1661                 } else
1662                         cnp->cn_pnbuf[linklen] = '\0';
1663                 ndp->ni_pathlen += linklen;
1664
1665                 /*
1666                  * Cleanup refs for next loop and check if root directory 
1667                  * should replace current directory.  Normally ni_dvp 
1668                  * becomes the new base directory and is cleaned up when
1669                  * we loop.  Explicitly null pointers after invalidation
1670                  * to clarify operation.
1671                  */
1672                 vput(ndp->ni_vp);
1673                 ndp->ni_vp = NULL;
1674
1675                 if (cnp->cn_pnbuf[0] == '/') {
1676                         vrele(ndp->ni_dvp);
1677                         ndp->ni_dvp = ndp->ni_rootdir;
1678                         vref(ndp->ni_dvp);
1679                 }
1680                 ndp->ni_startdir = ndp->ni_dvp;
1681                 ndp->ni_dvp = NULL;
1682         }
1683
1684         /*
1685          * nfs_namei() guarentees that fields will not contain garbage
1686          * whether an error occurs or not.  This allows the caller to track
1687          * cleanup state trivially.
1688          */
1689 out:
1690         if (error) {
1691                 zfree(namei_zone, cnp->cn_pnbuf);
1692                 ndp->ni_vp = NULL;
1693                 ndp->ni_dvp = NULL;
1694                 ndp->ni_startdir = NULL;
1695                 cnp->cn_flags &= ~CNP_HASBUF;
1696         } else if ((ndp->ni_cnd.cn_flags & (CNP_WANTPARENT|CNP_LOCKPARENT)) == 0) {
1697                 ndp->ni_dvp = NULL;
1698         }
1699         return (error);
1700 }
1701
1702 /*
1703  * A fiddled version of m_adj() that ensures null fill to a long
1704  * boundary and only trims off the back end
1705  */
1706 void
1707 nfsm_adj(struct mbuf *mp, int len, int nul)
1708 {
1709         struct mbuf *m;
1710         int count, i;
1711         char *cp;
1712
1713         /*
1714          * Trim from tail.  Scan the mbuf chain,
1715          * calculating its length and finding the last mbuf.
1716          * If the adjustment only affects this mbuf, then just
1717          * adjust and return.  Otherwise, rescan and truncate
1718          * after the remaining size.
1719          */
1720         count = 0;
1721         m = mp;
1722         for (;;) {
1723                 count += m->m_len;
1724                 if (m->m_next == (struct mbuf *)0)
1725                         break;
1726                 m = m->m_next;
1727         }
1728         if (m->m_len > len) {
1729                 m->m_len -= len;
1730                 if (nul > 0) {
1731                         cp = mtod(m, caddr_t)+m->m_len-nul;
1732                         for (i = 0; i < nul; i++)
1733                                 *cp++ = '\0';
1734                 }
1735                 return;
1736         }
1737         count -= len;
1738         if (count < 0)
1739                 count = 0;
1740         /*
1741          * Correct length for chain is "count".
1742          * Find the mbuf with last data, adjust its length,
1743          * and toss data from remaining mbufs on chain.
1744          */
1745         for (m = mp; m; m = m->m_next) {
1746                 if (m->m_len >= count) {
1747                         m->m_len = count;
1748                         if (nul > 0) {
1749                                 cp = mtod(m, caddr_t)+m->m_len-nul;
1750                                 for (i = 0; i < nul; i++)
1751                                         *cp++ = '\0';
1752                         }
1753                         break;
1754                 }
1755                 count -= m->m_len;
1756         }
1757         for (m = m->m_next;m;m = m->m_next)
1758                 m->m_len = 0;
1759 }
1760
1761 /*
1762  * Make these functions instead of macros, so that the kernel text size
1763  * doesn't get too big...
1764  */
1765 void
1766 nfsm_srvwcc(struct nfsrv_descript *nfsd, int before_ret,
1767             struct vattr *before_vap, int after_ret, struct vattr *after_vap,
1768             struct mbuf **mbp, char **bposp)
1769 {
1770         struct mbuf *mb = *mbp, *mb2;
1771         char *bpos = *bposp;
1772         u_int32_t *tl;
1773
1774         if (before_ret) {
1775                 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1776                 *tl = nfs_false;
1777         } else {
1778                 nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
1779                 *tl++ = nfs_true;
1780                 txdr_hyper(before_vap->va_size, tl);
1781                 tl += 2;
1782                 txdr_nfsv3time(&(before_vap->va_mtime), tl);
1783                 tl += 2;
1784                 txdr_nfsv3time(&(before_vap->va_ctime), tl);
1785         }
1786         *bposp = bpos;
1787         *mbp = mb;
1788         nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
1789 }
1790
1791 void
1792 nfsm_srvpostopattr(struct nfsrv_descript *nfsd, int after_ret,
1793                    struct vattr *after_vap, struct mbuf **mbp, char **bposp)
1794 {
1795         struct mbuf *mb = *mbp, *mb2;
1796         char *bpos = *bposp;
1797         u_int32_t *tl;
1798         struct nfs_fattr *fp;
1799
1800         if (after_ret) {
1801                 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1802                 *tl = nfs_false;
1803         } else {
1804                 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR);
1805                 *tl++ = nfs_true;
1806                 fp = (struct nfs_fattr *)tl;
1807                 nfsm_srvfattr(nfsd, after_vap, fp);
1808         }
1809         *mbp = mb;
1810         *bposp = bpos;
1811 }
1812
1813 void
1814 nfsm_srvfattr(struct nfsrv_descript *nfsd, struct vattr *vap,
1815               struct nfs_fattr *fp)
1816 {
1817
1818         fp->fa_nlink = txdr_unsigned(vap->va_nlink);
1819         fp->fa_uid = txdr_unsigned(vap->va_uid);
1820         fp->fa_gid = txdr_unsigned(vap->va_gid);
1821         if (nfsd->nd_flag & ND_NFSV3) {
1822                 fp->fa_type = vtonfsv3_type(vap->va_type);
1823                 fp->fa_mode = vtonfsv3_mode(vap->va_mode);
1824                 txdr_hyper(vap->va_size, &fp->fa3_size);
1825                 txdr_hyper(vap->va_bytes, &fp->fa3_used);
1826                 fp->fa3_rdev.specdata1 = txdr_unsigned(umajor(vap->va_rdev));
1827                 fp->fa3_rdev.specdata2 = txdr_unsigned(uminor(vap->va_rdev));
1828                 fp->fa3_fsid.nfsuquad[0] = 0;
1829                 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
1830                 fp->fa3_fileid.nfsuquad[0] = 0;
1831                 fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid);
1832                 txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
1833                 txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
1834                 txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
1835         } else {
1836                 fp->fa_type = vtonfsv2_type(vap->va_type);
1837                 fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1838                 fp->fa2_size = txdr_unsigned(vap->va_size);
1839                 fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
1840                 if (vap->va_type == VFIFO)
1841                         fp->fa2_rdev = 0xffffffff;
1842                 else
1843                         fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
1844                 fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
1845                 fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
1846                 fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
1847                 txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
1848                 txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
1849                 txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
1850         }
1851 }
1852
1853 /*
1854  * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
1855  *      - look up fsid in mount list (if not found ret error)
1856  *      - get vp and export rights by calling VFS_FHTOVP()
1857  *      - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
1858  *      - if not lockflag unlock it with VOP_UNLOCK()
1859  */
1860 int
1861 nfsrv_fhtovp(fhandle_t *fhp, int lockflag, struct vnode **vpp,
1862              struct ucred *cred, struct nfssvc_sock *slp, struct sockaddr *nam,
1863              int *rdonlyp, int kerbflag, int pubflag)
1864 {
1865         struct thread *td = curthread; /* XXX */
1866         struct mount *mp;
1867         int i;
1868         struct ucred *credanon;
1869         int error, exflags;
1870 #ifdef MNT_EXNORESPORT          /* XXX needs mountd and /etc/exports help yet */
1871         struct sockaddr_int *saddr;
1872 #endif
1873
1874         *vpp = (struct vnode *)0;
1875
1876         if (nfs_ispublicfh(fhp)) {
1877                 if (!pubflag || !nfs_pub.np_valid)
1878                         return (ESTALE);
1879                 fhp = &nfs_pub.np_handle;
1880         }
1881
1882         mp = vfs_getvfs(&fhp->fh_fsid);
1883         if (!mp)
1884                 return (ESTALE);
1885         error = VFS_CHECKEXP(mp, nam, &exflags, &credanon);
1886         if (error)
1887                 return (error); 
1888         error = VFS_FHTOVP(mp, &fhp->fh_fid, vpp);
1889         if (error)
1890                 return (error);
1891 #ifdef MNT_EXNORESPORT
1892         if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) {
1893                 saddr = (struct sockaddr_in *)nam;
1894                 if (saddr->sin_family == AF_INET &&
1895                     ntohs(saddr->sin_port) >= IPPORT_RESERVED) {
1896                         vput(*vpp);
1897                         *vpp = NULL;
1898                         return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1899                 }
1900         }
1901 #endif
1902         /*
1903          * Check/setup credentials.
1904          */
1905         if (exflags & MNT_EXKERB) {
1906                 if (!kerbflag) {
1907                         vput(*vpp);
1908                         *vpp = NULL;
1909                         return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1910                 }
1911         } else if (kerbflag) {
1912                 vput(*vpp);
1913                 *vpp = NULL;
1914                 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1915         } else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1916                 cred->cr_uid = credanon->cr_uid;
1917                 for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++)
1918                         cred->cr_groups[i] = credanon->cr_groups[i];
1919                 cred->cr_ngroups = i;
1920         }
1921         if (exflags & MNT_EXRDONLY)
1922                 *rdonlyp = 1;
1923         else
1924                 *rdonlyp = 0;
1925
1926         nfsrv_object_create(*vpp);
1927
1928         if (!lockflag)
1929                 VOP_UNLOCK(*vpp, NULL, 0, td);
1930         return (0);
1931 }
1932
1933
1934 /*
1935  * WebNFS: check if a filehandle is a public filehandle. For v3, this
1936  * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has
1937  * transformed this to all zeroes in both cases, so check for it.
1938  */
1939 int
1940 nfs_ispublicfh(fhandle_t *fhp)
1941 {
1942         char *cp = (char *)fhp;
1943         int i;
1944
1945         for (i = 0; i < NFSX_V3FH; i++)
1946                 if (*cp++ != 0)
1947                         return (FALSE);
1948         return (TRUE);
1949 }
1950   
1951 #endif /* NFS_NOSERVER */
1952 /*
1953  * This function compares two net addresses by family and returns TRUE
1954  * if they are the same host.
1955  * If there is any doubt, return FALSE.
1956  * The AF_INET family is handled as a special case so that address mbufs
1957  * don't need to be saved to store "struct in_addr", which is only 4 bytes.
1958  */
1959 int
1960 netaddr_match(int family, union nethostaddr *haddr, struct sockaddr *nam)
1961 {
1962         struct sockaddr_in *inetaddr;
1963
1964         switch (family) {
1965         case AF_INET:
1966                 inetaddr = (struct sockaddr_in *)nam;
1967                 if (inetaddr->sin_family == AF_INET &&
1968                     inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
1969                         return (1);
1970                 break;
1971         default:
1972                 break;
1973         };
1974         return (0);
1975 }
1976
1977 static nfsuint64 nfs_nullcookie = { { 0, 0 } };
1978 /*
1979  * This function finds the directory cookie that corresponds to the
1980  * logical byte offset given.
1981  */
1982 nfsuint64 *
1983 nfs_getcookie(struct nfsnode *np, off_t off, int add)
1984 {
1985         struct nfsdmap *dp, *dp2;
1986         int pos;
1987
1988         pos = (uoff_t)off / NFS_DIRBLKSIZ;
1989         if (pos == 0 || off < 0) {
1990 #ifdef DIAGNOSTIC
1991                 if (add)
1992                         panic("nfs getcookie add at <= 0");
1993 #endif
1994                 return (&nfs_nullcookie);
1995         }
1996         pos--;
1997         dp = np->n_cookies.lh_first;
1998         if (!dp) {
1999                 if (add) {
2000                         MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap),
2001                                 M_NFSDIROFF, M_WAITOK);
2002                         dp->ndm_eocookie = 0;
2003                         LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
2004                 } else
2005                         return ((nfsuint64 *)0);
2006         }
2007         while (pos >= NFSNUMCOOKIES) {
2008                 pos -= NFSNUMCOOKIES;
2009                 if (dp->ndm_list.le_next) {
2010                         if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
2011                                 pos >= dp->ndm_eocookie)
2012                                 return ((nfsuint64 *)0);
2013                         dp = dp->ndm_list.le_next;
2014                 } else if (add) {
2015                         MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap),
2016                                 M_NFSDIROFF, M_WAITOK);
2017                         dp2->ndm_eocookie = 0;
2018                         LIST_INSERT_AFTER(dp, dp2, ndm_list);
2019                         dp = dp2;
2020                 } else
2021                         return ((nfsuint64 *)0);
2022         }
2023         if (pos >= dp->ndm_eocookie) {
2024                 if (add)
2025                         dp->ndm_eocookie = pos + 1;
2026                 else
2027                         return ((nfsuint64 *)0);
2028         }
2029         return (&dp->ndm_cookies[pos]);
2030 }
2031
2032 /*
2033  * Invalidate cached directory information, except for the actual directory
2034  * blocks (which are invalidated separately).
2035  * Done mainly to avoid the use of stale offset cookies.
2036  */
2037 void
2038 nfs_invaldir(struct vnode *vp)
2039 {
2040         struct nfsnode *np = VTONFS(vp);
2041
2042 #ifdef DIAGNOSTIC
2043         if (vp->v_type != VDIR)
2044                 panic("nfs: invaldir not dir");
2045 #endif
2046         np->n_direofoffset = 0;
2047         np->n_cookieverf.nfsuquad[0] = 0;
2048         np->n_cookieverf.nfsuquad[1] = 0;
2049         if (np->n_cookies.lh_first)
2050                 np->n_cookies.lh_first->ndm_eocookie = 0;
2051 }
2052
2053 /*
2054  * The write verifier has changed (probably due to a server reboot), so all
2055  * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
2056  * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
2057  * and B_CLUSTEROK flags.  Once done the new write verifier can be set for the
2058  * mount point.
2059  *
2060  * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data 
2061  * writes are not clusterable.
2062  */
2063 void
2064 nfs_clearcommit(struct mount *mp)
2065 {
2066         struct vnode *vp, *nvp;
2067         struct buf *bp, *nbp;
2068         lwkt_tokref ilock;
2069         int s;
2070
2071         lwkt_gettoken(&ilock, &mntvnode_token);
2072         s = splbio();
2073         for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp; vp = nvp) {
2074                 nvp = TAILQ_NEXT(vp, v_nmntvnodes);     /* ZZZ */
2075                 if (vp->v_flag & VPLACEMARKER)
2076                         continue;
2077                 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2078                         nbp = TAILQ_NEXT(bp, b_vnbufs);
2079                         if (BUF_REFCNT(bp) == 0 &&
2080                             (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2081                              == (B_DELWRI | B_NEEDCOMMIT)) {
2082                                 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
2083                         }
2084                 }
2085         }
2086         splx(s);
2087         lwkt_reltoken(&ilock);
2088 }
2089
2090 #ifndef NFS_NOSERVER
2091 /*
2092  * Map errnos to NFS error numbers. For Version 3 also filter out error
2093  * numbers not specified for the associated procedure.
2094  */
2095 int
2096 nfsrv_errmap(struct nfsrv_descript *nd, int err)
2097 {
2098         short *defaulterrp, *errp;
2099
2100         if (nd->nd_flag & ND_NFSV3) {
2101             if (nd->nd_procnum <= NFSPROC_COMMIT) {
2102                 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
2103                 while (*++errp) {
2104                         if (*errp == err)
2105                                 return (err);
2106                         else if (*errp > err)
2107                                 break;
2108                 }
2109                 return ((int)*defaulterrp);
2110             } else
2111                 return (err & 0xffff);
2112         }
2113         if (err <= ELAST)
2114                 return ((int)nfsrv_v2errmap[err - 1]);
2115         return (NFSERR_IO);
2116 }
2117
2118 int
2119 nfsrv_object_create(struct vnode *vp)
2120 {
2121         struct thread *td = curthread;
2122
2123         if (vp == NULL || vp->v_type != VREG)
2124                 return (1);
2125         return (vfs_object_create(vp, td));
2126 }
2127
2128 /*
2129  * Sort the group list in increasing numerical order.
2130  * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
2131  *  that used to be here.)
2132  */
2133 void
2134 nfsrvw_sort(gid_t *list, int num)
2135 {
2136         int i, j;
2137         gid_t v;
2138
2139         /* Insertion sort. */
2140         for (i = 1; i < num; i++) {
2141                 v = list[i];
2142                 /* find correct slot for value v, moving others up */
2143                 for (j = i; --j >= 0 && v < list[j];)
2144                         list[j + 1] = list[j];
2145                 list[j + 1] = v;
2146         }
2147 }
2148
2149 /*
2150  * copy credentials making sure that the result can be compared with bcmp().
2151  */
2152 void
2153 nfsrv_setcred(struct ucred *incred, struct ucred *outcred)
2154 {
2155         int i;
2156
2157         bzero((caddr_t)outcred, sizeof (struct ucred));
2158         outcred->cr_ref = 1;
2159         outcred->cr_uid = incred->cr_uid;
2160         outcred->cr_ngroups = incred->cr_ngroups;
2161         for (i = 0; i < incred->cr_ngroups; i++)
2162                 outcred->cr_groups[i] = incred->cr_groups[i];
2163         nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups);
2164 }
2165 #endif /* NFS_NOSERVER */