Style(9) cleanup.
[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.14 2004/04/19 16:33:49 cpressey 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                                 if (vap->va_size < np->n_size)
1301                                         vap->va_size = np->n_size;
1302                                 else
1303                                         np->n_size = vap->va_size;
1304                         } else {
1305                                 np->n_size = vap->va_size;
1306                         }
1307                         vnode_pager_setsize(vp, np->n_size);
1308                 } else {
1309                         np->n_size = vap->va_size;
1310                 }
1311         }
1312         if (vaper != NULL) {
1313                 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
1314                 if (np->n_flag & NCHG) {
1315                         if (np->n_flag & NACC)
1316                                 vaper->va_atime = np->n_atim;
1317                         if (np->n_flag & NUPD)
1318                                 vaper->va_mtime = np->n_mtim;
1319                 }
1320         }
1321         return (0);
1322 }
1323
1324 #ifdef NFS_ACDEBUG
1325 #include <sys/sysctl.h>
1326 SYSCTL_DECL(_vfs_nfs);
1327 static int nfs_acdebug;
1328 SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, "");
1329 #endif
1330
1331 /*
1332  * Check the time stamp
1333  * If the cache is valid, copy contents to *vap and return 0
1334  * otherwise return an error
1335  */
1336 int
1337 nfs_getattrcache(struct vnode *vp, struct vattr *vaper)
1338 {
1339         struct nfsnode *np;
1340         struct vattr *vap;
1341         struct nfsmount *nmp;
1342         int timeo;
1343
1344         np = VTONFS(vp);
1345         vap = &np->n_vattr;
1346         nmp = VFSTONFS(vp->v_mount);
1347         /* XXX n_mtime doesn't seem to be updated on a miss-and-reload */
1348         timeo = (time_second - np->n_mtime) / 10;
1349
1350 #ifdef NFS_ACDEBUG
1351         if (nfs_acdebug>1)
1352                 printf("nfs_getattrcache: initial timeo = %d\n", timeo);
1353 #endif
1354
1355         if (vap->va_type == VDIR) {
1356                 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin)
1357                         timeo = nmp->nm_acdirmin;
1358                 else if (timeo > nmp->nm_acdirmax)
1359                         timeo = nmp->nm_acdirmax;
1360         } else {
1361                 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin)
1362                         timeo = nmp->nm_acregmin;
1363                 else if (timeo > nmp->nm_acregmax)
1364                         timeo = nmp->nm_acregmax;
1365         }
1366
1367 #ifdef NFS_ACDEBUG
1368         if (nfs_acdebug > 2)
1369                 printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
1370                         nmp->nm_acregmin, nmp->nm_acregmax,
1371                         nmp->nm_acdirmin, nmp->nm_acdirmax);
1372
1373         if (nfs_acdebug)
1374                 printf("nfs_getattrcache: age = %d; final timeo = %d\n",
1375                         (time_second - np->n_attrstamp), timeo);
1376 #endif
1377
1378         if ((time_second - np->n_attrstamp) >= timeo) {
1379                 nfsstats.attrcache_misses++;
1380                 return (ENOENT);
1381         }
1382         nfsstats.attrcache_hits++;
1383         if (vap->va_size != np->n_size) {
1384                 if (vap->va_type == VREG) {
1385                         if (np->n_flag & NMODIFIED) {
1386                                 if (vap->va_size < np->n_size)
1387                                         vap->va_size = np->n_size;
1388                                 else
1389                                         np->n_size = vap->va_size;
1390                         } else {
1391                                 np->n_size = vap->va_size;
1392                         }
1393                         vnode_pager_setsize(vp, np->n_size);
1394                 } else {
1395                         np->n_size = vap->va_size;
1396                 }
1397         }
1398         bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
1399         if (np->n_flag & NCHG) {
1400                 if (np->n_flag & NACC)
1401                         vaper->va_atime = np->n_atim;
1402                 if (np->n_flag & NUPD)
1403                         vaper->va_mtime = np->n_mtim;
1404         }
1405         return (0);
1406 }
1407
1408 #ifndef NFS_NOSERVER
1409 /*
1410  * Set up nameidata for a lookup() call and do it.
1411  *
1412  * If pubflag is set, this call is done for a lookup operation on the
1413  * public filehandle. In that case we allow crossing mountpoints and
1414  * absolute pathnames. However, the caller is expected to check that
1415  * the lookup result is within the public fs, and deny access if
1416  * it is not.
1417  *
1418  * nfs_namei() clears out garbage fields that namei() might leave garbage.
1419  * This is mainly ni_vp and ni_dvp when an error occurs, and ni_dvp when no
1420  * error occurs but the parent was not requested.
1421  *
1422  * dirp may be set whether an error is returned or not, and must be 
1423  * released by the caller.
1424  */
1425 int
1426 nfs_namei(struct nameidata *ndp, fhandle_t *fhp, int len,
1427           struct nfssvc_sock *slp, struct sockaddr *nam, struct mbuf **mdp,
1428           caddr_t *dposp, struct vnode **retdirp, struct thread *td,
1429           int kerbflag, int pubflag)
1430 {
1431         int i, rem;
1432         struct mbuf *md;
1433         char *fromcp, *tocp, *cp;
1434         struct iovec aiov;
1435         struct uio auio;
1436         struct vnode *dp;
1437         int error, rdonly, linklen;
1438         struct componentname *cnp = &ndp->ni_cnd;
1439
1440         *retdirp = (struct vnode *)0;
1441         cnp->cn_pnbuf = zalloc(namei_zone);
1442
1443         /*
1444          * Copy the name from the mbuf list to ndp->ni_pnbuf
1445          * and set the various ndp fields appropriately.
1446          */
1447         fromcp = *dposp;
1448         tocp = cnp->cn_pnbuf;
1449         md = *mdp;
1450         rem = mtod(md, caddr_t) + md->m_len - fromcp;
1451         for (i = 0; i < len; i++) {
1452                 while (rem == 0) {
1453                         md = md->m_next;
1454                         if (md == NULL) {
1455                                 error = EBADRPC;
1456                                 goto out;
1457                         }
1458                         fromcp = mtod(md, caddr_t);
1459                         rem = md->m_len;
1460                 }
1461                 if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
1462                         error = EACCES;
1463                         goto out;
1464                 }
1465                 *tocp++ = *fromcp++;
1466                 rem--;
1467         }
1468         *tocp = '\0';
1469         *mdp = md;
1470         *dposp = fromcp;
1471         len = nfsm_rndup(len)-len;
1472         if (len > 0) {
1473                 if (rem >= len)
1474                         *dposp += len;
1475                 else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
1476                         goto out;
1477         }
1478
1479         /*
1480          * Extract and set starting directory.
1481          */
1482         error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp,
1483             nam, &rdonly, kerbflag, pubflag);
1484         if (error)
1485                 goto out;
1486         if (dp->v_type != VDIR) {
1487                 vrele(dp);
1488                 error = ENOTDIR;
1489                 goto out;
1490         }
1491
1492         if (rdonly)
1493                 cnp->cn_flags |= CNP_RDONLY;
1494
1495         /*
1496          * Set return directory.  Reference to dp is implicitly transfered 
1497          * to the returned pointer
1498          */
1499         *retdirp = dp;
1500
1501         if (pubflag) {
1502                 /*
1503                  * Oh joy. For WebNFS, handle those pesky '%' escapes,
1504                  * and the 'native path' indicator.
1505                  */
1506                 cp = zalloc(namei_zone);
1507                 fromcp = cnp->cn_pnbuf;
1508                 tocp = cp;
1509                 if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
1510                         switch ((unsigned char)*fromcp) {
1511                         case WEBNFS_NATIVE_CHAR:
1512                                 /*
1513                                  * 'Native' path for us is the same
1514                                  * as a path according to the NFS spec,
1515                                  * just skip the escape char.
1516                                  */
1517                                 fromcp++;
1518                                 break;
1519                         /*
1520                          * More may be added in the future, range 0x80-0xff
1521                          */
1522                         default:
1523                                 error = EIO;
1524                                 zfree(namei_zone, cp);
1525                                 goto out;
1526                         }
1527                 }
1528                 /*
1529                  * Translate the '%' escapes, URL-style.
1530                  */
1531                 while (*fromcp != '\0') {
1532                         if (*fromcp == WEBNFS_ESC_CHAR) {
1533                                 if (fromcp[1] != '\0' && fromcp[2] != '\0') {
1534                                         fromcp++;
1535                                         *tocp++ = HEXSTRTOI(fromcp);
1536                                         fromcp += 2;
1537                                         continue;
1538                                 } else {
1539                                         error = ENOENT;
1540                                         zfree(namei_zone, cp);
1541                                         goto out;
1542                                 }
1543                         } else
1544                                 *tocp++ = *fromcp++;
1545                 }
1546                 *tocp = '\0';
1547                 zfree(namei_zone, cnp->cn_pnbuf);
1548                 cnp->cn_pnbuf = cp;
1549         }
1550
1551         ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1;
1552         ndp->ni_segflg = UIO_SYSSPACE;
1553
1554         if (pubflag) {
1555                 ndp->ni_rootdir = rootvnode;
1556                 ndp->ni_loopcnt = 0;
1557                 if (cnp->cn_pnbuf[0] == '/')
1558                         dp = rootvnode;
1559         } else {
1560                 cnp->cn_flags |= CNP_NOCROSSMOUNT;
1561         }
1562
1563         /*
1564          * Initialize for scan, set ni_startdir and bump ref on dp again
1565          * because lookup() will dereference ni_startdir.
1566          */
1567
1568         cnp->cn_td = td;
1569         VREF(dp);
1570         ndp->ni_startdir = dp;
1571
1572         for (;;) {
1573                 cnp->cn_nameptr = cnp->cn_pnbuf;
1574                 /*
1575                  * Call lookup() to do the real work.  If an error occurs,
1576                  * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
1577                  * we do not have to dereference anything before returning.
1578                  * In either case ni_startdir will be dereferenced and NULLed
1579                  * out.
1580                  */
1581                 error = lookup(ndp);
1582                 if (error)
1583                         break;
1584
1585                 /*
1586                  * Check for encountering a symbolic link.  Trivial 
1587                  * termination occurs if no symlink encountered.
1588                  * Note: zfree is safe because error is 0, so we will
1589                  * not zfree it again when we break.
1590                  */
1591                 if ((cnp->cn_flags & CNP_ISSYMLINK) == 0) {
1592                         nfsrv_object_create(ndp->ni_vp);
1593                         if (cnp->cn_flags & (CNP_SAVENAME | CNP_SAVESTART))
1594                                 cnp->cn_flags |= CNP_HASBUF;
1595                         else
1596                                 zfree(namei_zone, cnp->cn_pnbuf);
1597                         break;
1598                 }
1599
1600                 /*
1601                  * Validate symlink
1602                  */
1603                 if ((cnp->cn_flags & CNP_LOCKPARENT) && ndp->ni_pathlen == 1)
1604                         VOP_UNLOCK(ndp->ni_dvp, NULL, 0, td);
1605                 if (!pubflag) {
1606                         error = EINVAL;
1607                         goto badlink2;
1608                 }
1609
1610                 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
1611                         error = ELOOP;
1612                         goto badlink2;
1613                 }
1614                 if (ndp->ni_pathlen > 1)
1615                         cp = zalloc(namei_zone);
1616                 else
1617                         cp = cnp->cn_pnbuf;
1618                 aiov.iov_base = cp;
1619                 aiov.iov_len = MAXPATHLEN;
1620                 auio.uio_iov = &aiov;
1621                 auio.uio_iovcnt = 1;
1622                 auio.uio_offset = 0;
1623                 auio.uio_rw = UIO_READ;
1624                 auio.uio_segflg = UIO_SYSSPACE;
1625                 auio.uio_td = NULL;
1626                 auio.uio_resid = MAXPATHLEN;
1627                 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
1628                 if (error) {
1629                 badlink1:
1630                         if (ndp->ni_pathlen > 1)
1631                                 zfree(namei_zone, cp);
1632                 badlink2:
1633                         vrele(ndp->ni_dvp);
1634                         vput(ndp->ni_vp);
1635                         break;
1636                 }
1637                 linklen = MAXPATHLEN - auio.uio_resid;
1638                 if (linklen == 0) {
1639                         error = ENOENT;
1640                         goto badlink1;
1641                 }
1642                 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
1643                         error = ENAMETOOLONG;
1644                         goto badlink1;
1645                 }
1646
1647                 /*
1648                  * Adjust or replace path
1649                  */
1650                 if (ndp->ni_pathlen > 1) {
1651                         bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
1652                         zfree(namei_zone, cnp->cn_pnbuf);
1653                         cnp->cn_pnbuf = cp;
1654                 } else
1655                         cnp->cn_pnbuf[linklen] = '\0';
1656                 ndp->ni_pathlen += linklen;
1657
1658                 /*
1659                  * Cleanup refs for next loop and check if root directory 
1660                  * should replace current directory.  Normally ni_dvp 
1661                  * becomes the new base directory and is cleaned up when
1662                  * we loop.  Explicitly null pointers after invalidation
1663                  * to clarify operation.
1664                  */
1665                 vput(ndp->ni_vp);
1666                 ndp->ni_vp = NULL;
1667
1668                 if (cnp->cn_pnbuf[0] == '/') {
1669                         vrele(ndp->ni_dvp);
1670                         ndp->ni_dvp = ndp->ni_rootdir;
1671                         VREF(ndp->ni_dvp);
1672                 }
1673                 ndp->ni_startdir = ndp->ni_dvp;
1674                 ndp->ni_dvp = NULL;
1675         }
1676
1677         /*
1678          * nfs_namei() guarentees that fields will not contain garbage
1679          * whether an error occurs or not.  This allows the caller to track
1680          * cleanup state trivially.
1681          */
1682 out:
1683         if (error) {
1684                 zfree(namei_zone, cnp->cn_pnbuf);
1685                 ndp->ni_vp = NULL;
1686                 ndp->ni_dvp = NULL;
1687                 ndp->ni_startdir = NULL;
1688                 cnp->cn_flags &= ~CNP_HASBUF;
1689         } else if ((ndp->ni_cnd.cn_flags & (CNP_WANTPARENT|CNP_LOCKPARENT)) == 0) {
1690                 ndp->ni_dvp = NULL;
1691         }
1692         return (error);
1693 }
1694
1695 /*
1696  * A fiddled version of m_adj() that ensures null fill to a long
1697  * boundary and only trims off the back end
1698  */
1699 void
1700 nfsm_adj(struct mbuf *mp, int len, int nul)
1701 {
1702         struct mbuf *m;
1703         int count, i;
1704         char *cp;
1705
1706         /*
1707          * Trim from tail.  Scan the mbuf chain,
1708          * calculating its length and finding the last mbuf.
1709          * If the adjustment only affects this mbuf, then just
1710          * adjust and return.  Otherwise, rescan and truncate
1711          * after the remaining size.
1712          */
1713         count = 0;
1714         m = mp;
1715         for (;;) {
1716                 count += m->m_len;
1717                 if (m->m_next == (struct mbuf *)0)
1718                         break;
1719                 m = m->m_next;
1720         }
1721         if (m->m_len > len) {
1722                 m->m_len -= len;
1723                 if (nul > 0) {
1724                         cp = mtod(m, caddr_t)+m->m_len-nul;
1725                         for (i = 0; i < nul; i++)
1726                                 *cp++ = '\0';
1727                 }
1728                 return;
1729         }
1730         count -= len;
1731         if (count < 0)
1732                 count = 0;
1733         /*
1734          * Correct length for chain is "count".
1735          * Find the mbuf with last data, adjust its length,
1736          * and toss data from remaining mbufs on chain.
1737          */
1738         for (m = mp; m; m = m->m_next) {
1739                 if (m->m_len >= count) {
1740                         m->m_len = count;
1741                         if (nul > 0) {
1742                                 cp = mtod(m, caddr_t)+m->m_len-nul;
1743                                 for (i = 0; i < nul; i++)
1744                                         *cp++ = '\0';
1745                         }
1746                         break;
1747                 }
1748                 count -= m->m_len;
1749         }
1750         for (m = m->m_next;m;m = m->m_next)
1751                 m->m_len = 0;
1752 }
1753
1754 /*
1755  * Make these functions instead of macros, so that the kernel text size
1756  * doesn't get too big...
1757  */
1758 void
1759 nfsm_srvwcc(struct nfsrv_descript *nfsd, int before_ret,
1760             struct vattr *before_vap, int after_ret, struct vattr *after_vap,
1761             struct mbuf **mbp, char **bposp)
1762 {
1763         struct mbuf *mb = *mbp, *mb2;
1764         char *bpos = *bposp;
1765         u_int32_t *tl;
1766
1767         if (before_ret) {
1768                 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1769                 *tl = nfs_false;
1770         } else {
1771                 nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
1772                 *tl++ = nfs_true;
1773                 txdr_hyper(before_vap->va_size, tl);
1774                 tl += 2;
1775                 txdr_nfsv3time(&(before_vap->va_mtime), tl);
1776                 tl += 2;
1777                 txdr_nfsv3time(&(before_vap->va_ctime), tl);
1778         }
1779         *bposp = bpos;
1780         *mbp = mb;
1781         nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
1782 }
1783
1784 void
1785 nfsm_srvpostopattr(struct nfsrv_descript *nfsd, int after_ret,
1786                    struct vattr *after_vap, struct mbuf **mbp, char **bposp)
1787 {
1788         struct mbuf *mb = *mbp, *mb2;
1789         char *bpos = *bposp;
1790         u_int32_t *tl;
1791         struct nfs_fattr *fp;
1792
1793         if (after_ret) {
1794                 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1795                 *tl = nfs_false;
1796         } else {
1797                 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR);
1798                 *tl++ = nfs_true;
1799                 fp = (struct nfs_fattr *)tl;
1800                 nfsm_srvfattr(nfsd, after_vap, fp);
1801         }
1802         *mbp = mb;
1803         *bposp = bpos;
1804 }
1805
1806 void
1807 nfsm_srvfattr(struct nfsrv_descript *nfsd, struct vattr *vap,
1808               struct nfs_fattr *fp)
1809 {
1810
1811         fp->fa_nlink = txdr_unsigned(vap->va_nlink);
1812         fp->fa_uid = txdr_unsigned(vap->va_uid);
1813         fp->fa_gid = txdr_unsigned(vap->va_gid);
1814         if (nfsd->nd_flag & ND_NFSV3) {
1815                 fp->fa_type = vtonfsv3_type(vap->va_type);
1816                 fp->fa_mode = vtonfsv3_mode(vap->va_mode);
1817                 txdr_hyper(vap->va_size, &fp->fa3_size);
1818                 txdr_hyper(vap->va_bytes, &fp->fa3_used);
1819                 fp->fa3_rdev.specdata1 = txdr_unsigned(umajor(vap->va_rdev));
1820                 fp->fa3_rdev.specdata2 = txdr_unsigned(uminor(vap->va_rdev));
1821                 fp->fa3_fsid.nfsuquad[0] = 0;
1822                 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
1823                 fp->fa3_fileid.nfsuquad[0] = 0;
1824                 fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid);
1825                 txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
1826                 txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
1827                 txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
1828         } else {
1829                 fp->fa_type = vtonfsv2_type(vap->va_type);
1830                 fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1831                 fp->fa2_size = txdr_unsigned(vap->va_size);
1832                 fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
1833                 if (vap->va_type == VFIFO)
1834                         fp->fa2_rdev = 0xffffffff;
1835                 else
1836                         fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
1837                 fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
1838                 fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
1839                 fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
1840                 txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
1841                 txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
1842                 txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
1843         }
1844 }
1845
1846 /*
1847  * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
1848  *      - look up fsid in mount list (if not found ret error)
1849  *      - get vp and export rights by calling VFS_FHTOVP()
1850  *      - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
1851  *      - if not lockflag unlock it with VOP_UNLOCK()
1852  */
1853 int
1854 nfsrv_fhtovp(fhandle_t *fhp, int lockflag, struct vnode **vpp,
1855              struct ucred *cred, struct nfssvc_sock *slp, struct sockaddr *nam,
1856              int *rdonlyp, int kerbflag, int pubflag)
1857 {
1858         struct thread *td = curthread; /* XXX */
1859         struct mount *mp;
1860         int i;
1861         struct ucred *credanon;
1862         int error, exflags;
1863 #ifdef MNT_EXNORESPORT          /* XXX needs mountd and /etc/exports help yet */
1864         struct sockaddr_int *saddr;
1865 #endif
1866
1867         *vpp = (struct vnode *)0;
1868
1869         if (nfs_ispublicfh(fhp)) {
1870                 if (!pubflag || !nfs_pub.np_valid)
1871                         return (ESTALE);
1872                 fhp = &nfs_pub.np_handle;
1873         }
1874
1875         mp = vfs_getvfs(&fhp->fh_fsid);
1876         if (!mp)
1877                 return (ESTALE);
1878         error = VFS_CHECKEXP(mp, nam, &exflags, &credanon);
1879         if (error)
1880                 return (error); 
1881         error = VFS_FHTOVP(mp, &fhp->fh_fid, vpp);
1882         if (error)
1883                 return (error);
1884 #ifdef MNT_EXNORESPORT
1885         if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) {
1886                 saddr = (struct sockaddr_in *)nam;
1887                 if (saddr->sin_family == AF_INET &&
1888                     ntohs(saddr->sin_port) >= IPPORT_RESERVED) {
1889                         vput(*vpp);
1890                         *vpp = NULL;
1891                         return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1892                 }
1893         }
1894 #endif
1895         /*
1896          * Check/setup credentials.
1897          */
1898         if (exflags & MNT_EXKERB) {
1899                 if (!kerbflag) {
1900                         vput(*vpp);
1901                         *vpp = NULL;
1902                         return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1903                 }
1904         } else if (kerbflag) {
1905                 vput(*vpp);
1906                 *vpp = NULL;
1907                 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1908         } else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1909                 cred->cr_uid = credanon->cr_uid;
1910                 for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++)
1911                         cred->cr_groups[i] = credanon->cr_groups[i];
1912                 cred->cr_ngroups = i;
1913         }
1914         if (exflags & MNT_EXRDONLY)
1915                 *rdonlyp = 1;
1916         else
1917                 *rdonlyp = 0;
1918
1919         nfsrv_object_create(*vpp);
1920
1921         if (!lockflag)
1922                 VOP_UNLOCK(*vpp, NULL, 0, td);
1923         return (0);
1924 }
1925
1926
1927 /*
1928  * WebNFS: check if a filehandle is a public filehandle. For v3, this
1929  * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has
1930  * transformed this to all zeroes in both cases, so check for it.
1931  */
1932 int
1933 nfs_ispublicfh(fhandle_t *fhp)
1934 {
1935         char *cp = (char *)fhp;
1936         int i;
1937
1938         for (i = 0; i < NFSX_V3FH; i++)
1939                 if (*cp++ != 0)
1940                         return (FALSE);
1941         return (TRUE);
1942 }
1943   
1944 #endif /* NFS_NOSERVER */
1945 /*
1946  * This function compares two net addresses by family and returns TRUE
1947  * if they are the same host.
1948  * If there is any doubt, return FALSE.
1949  * The AF_INET family is handled as a special case so that address mbufs
1950  * don't need to be saved to store "struct in_addr", which is only 4 bytes.
1951  */
1952 int
1953 netaddr_match(int family, union nethostaddr *haddr, struct sockaddr *nam)
1954 {
1955         struct sockaddr_in *inetaddr;
1956
1957         switch (family) {
1958         case AF_INET:
1959                 inetaddr = (struct sockaddr_in *)nam;
1960                 if (inetaddr->sin_family == AF_INET &&
1961                     inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
1962                         return (1);
1963                 break;
1964         default:
1965                 break;
1966         };
1967         return (0);
1968 }
1969
1970 static nfsuint64 nfs_nullcookie = { { 0, 0 } };
1971 /*
1972  * This function finds the directory cookie that corresponds to the
1973  * logical byte offset given.
1974  */
1975 nfsuint64 *
1976 nfs_getcookie(struct nfsnode *np, off_t off, int add)
1977 {
1978         struct nfsdmap *dp, *dp2;
1979         int pos;
1980
1981         pos = (uoff_t)off / NFS_DIRBLKSIZ;
1982         if (pos == 0 || off < 0) {
1983 #ifdef DIAGNOSTIC
1984                 if (add)
1985                         panic("nfs getcookie add at <= 0");
1986 #endif
1987                 return (&nfs_nullcookie);
1988         }
1989         pos--;
1990         dp = np->n_cookies.lh_first;
1991         if (!dp) {
1992                 if (add) {
1993                         MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap),
1994                                 M_NFSDIROFF, M_WAITOK);
1995                         dp->ndm_eocookie = 0;
1996                         LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
1997                 } else
1998                         return ((nfsuint64 *)0);
1999         }
2000         while (pos >= NFSNUMCOOKIES) {
2001                 pos -= NFSNUMCOOKIES;
2002                 if (dp->ndm_list.le_next) {
2003                         if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
2004                                 pos >= dp->ndm_eocookie)
2005                                 return ((nfsuint64 *)0);
2006                         dp = dp->ndm_list.le_next;
2007                 } else if (add) {
2008                         MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap),
2009                                 M_NFSDIROFF, M_WAITOK);
2010                         dp2->ndm_eocookie = 0;
2011                         LIST_INSERT_AFTER(dp, dp2, ndm_list);
2012                         dp = dp2;
2013                 } else
2014                         return ((nfsuint64 *)0);
2015         }
2016         if (pos >= dp->ndm_eocookie) {
2017                 if (add)
2018                         dp->ndm_eocookie = pos + 1;
2019                 else
2020                         return ((nfsuint64 *)0);
2021         }
2022         return (&dp->ndm_cookies[pos]);
2023 }
2024
2025 /*
2026  * Invalidate cached directory information, except for the actual directory
2027  * blocks (which are invalidated separately).
2028  * Done mainly to avoid the use of stale offset cookies.
2029  */
2030 void
2031 nfs_invaldir(struct vnode *vp)
2032 {
2033         struct nfsnode *np = VTONFS(vp);
2034
2035 #ifdef DIAGNOSTIC
2036         if (vp->v_type != VDIR)
2037                 panic("nfs: invaldir not dir");
2038 #endif
2039         np->n_direofoffset = 0;
2040         np->n_cookieverf.nfsuquad[0] = 0;
2041         np->n_cookieverf.nfsuquad[1] = 0;
2042         if (np->n_cookies.lh_first)
2043                 np->n_cookies.lh_first->ndm_eocookie = 0;
2044 }
2045
2046 /*
2047  * The write verifier has changed (probably due to a server reboot), so all
2048  * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
2049  * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
2050  * and B_CLUSTEROK flags.  Once done the new write verifier can be set for the
2051  * mount point.
2052  *
2053  * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data 
2054  * writes are not clusterable.
2055  */
2056 void
2057 nfs_clearcommit(struct mount *mp)
2058 {
2059         struct vnode *vp, *nvp;
2060         struct buf *bp, *nbp;
2061         lwkt_tokref ilock;
2062         int s;
2063
2064         lwkt_gettoken(&ilock, &mntvnode_token);
2065         s = splbio();
2066         for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp; vp = nvp) {
2067                 nvp = TAILQ_NEXT(vp, v_nmntvnodes);     /* ZZZ */
2068                 if (vp->v_flag & VPLACEMARKER)
2069                         continue;
2070                 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2071                         nbp = TAILQ_NEXT(bp, b_vnbufs);
2072                         if (BUF_REFCNT(bp) == 0 &&
2073                             (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2074                              == (B_DELWRI | B_NEEDCOMMIT)) {
2075                                 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
2076                         }
2077                 }
2078         }
2079         splx(s);
2080         lwkt_reltoken(&ilock);
2081 }
2082
2083 #ifndef NFS_NOSERVER
2084 /*
2085  * Map errnos to NFS error numbers. For Version 3 also filter out error
2086  * numbers not specified for the associated procedure.
2087  */
2088 int
2089 nfsrv_errmap(struct nfsrv_descript *nd, int err)
2090 {
2091         short *defaulterrp, *errp;
2092
2093         if (nd->nd_flag & ND_NFSV3) {
2094             if (nd->nd_procnum <= NFSPROC_COMMIT) {
2095                 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
2096                 while (*++errp) {
2097                         if (*errp == err)
2098                                 return (err);
2099                         else if (*errp > err)
2100                                 break;
2101                 }
2102                 return ((int)*defaulterrp);
2103             } else
2104                 return (err & 0xffff);
2105         }
2106         if (err <= ELAST)
2107                 return ((int)nfsrv_v2errmap[err - 1]);
2108         return (NFSERR_IO);
2109 }
2110
2111 int
2112 nfsrv_object_create(struct vnode *vp)
2113 {
2114         struct thread *td = curthread;
2115
2116         if (vp == NULL || vp->v_type != VREG)
2117                 return (1);
2118         return (vfs_object_create(vp, td));
2119 }
2120
2121 /*
2122  * Sort the group list in increasing numerical order.
2123  * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
2124  *  that used to be here.)
2125  */
2126 void
2127 nfsrvw_sort(gid_t *list, int num)
2128 {
2129         int i, j;
2130         gid_t v;
2131
2132         /* Insertion sort. */
2133         for (i = 1; i < num; i++) {
2134                 v = list[i];
2135                 /* find correct slot for value v, moving others up */
2136                 for (j = i; --j >= 0 && v < list[j];)
2137                         list[j + 1] = list[j];
2138                 list[j + 1] = v;
2139         }
2140 }
2141
2142 /*
2143  * copy credentials making sure that the result can be compared with bcmp().
2144  */
2145 void
2146 nfsrv_setcred(struct ucred *incred, struct ucred *outcred)
2147 {
2148         int i;
2149
2150         bzero((caddr_t)outcred, sizeof (struct ucred));
2151         outcred->cr_ref = 1;
2152         outcred->cr_uid = incred->cr_uid;
2153         outcred->cr_ngroups = incred->cr_ngroups;
2154         for (i = 0; i < incred->cr_ngroups; i++)
2155                 outcred->cr_groups[i] = incred->cr_groups[i];
2156         nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups);
2157 }
2158 #endif /* NFS_NOSERVER */