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