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