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