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