nfs_srvsubs.c revision 47028
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.73 1999/02/17 13:59:29 bde Exp $
38 */
39
40/*
41 * These functions support the macros and help fiddle mbuf chains for
42 * the nfs op functions. They do things like create the rpc header and
43 * copy data between mbuf chains and uio lists.
44 */
45#include <sys/param.h>
46#include <sys/buf.h>
47#include <sys/proc.h>
48#include <sys/systm.h>
49#include <sys/kernel.h>
50#include <sys/mount.h>
51#include <sys/vnode.h>
52#include <sys/namei.h>
53#include <sys/mbuf.h>
54#include <sys/socket.h>
55#include <sys/stat.h>
56#include <sys/malloc.h>
57#include <sys/sysent.h>
58#include <sys/syscall.h>
59
60#include <vm/vm.h>
61#include <vm/vm_object.h>
62#include <vm/vm_extern.h>
63#include <vm/vm_zone.h>
64
65#include <nfs/rpcv2.h>
66#include <nfs/nfsproto.h>
67#include <nfs/nfs.h>
68#include <nfs/nfsnode.h>
69#include <nfs/xdr_subs.h>
70#include <nfs/nfsm_subs.h>
71#include <nfs/nfsmount.h>
72#include <nfs/nqnfs.h>
73#include <nfs/nfsrtt.h>
74
75#include <miscfs/specfs/specdev.h>
76
77#include <netinet/in.h>
78#ifdef ISO
79#include <netiso/iso.h>
80#endif
81
82/*
83 * Data items converted to xdr at startup, since they are constant
84 * This is kinda hokey, but may save a little time doing byte swaps
85 */
86u_int32_t nfs_xdrneg1;
87u_int32_t rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr,
88	rpc_mismatch, rpc_auth_unix, rpc_msgaccepted,
89	rpc_auth_kerb;
90u_int32_t nfs_prog, nqnfs_prog, nfs_true, nfs_false;
91
92/* And other global data */
93static u_int32_t nfs_xid = 0;
94static enum vtype nv2tov_type[8]= {
95	VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON,  VNON
96};
97enum vtype nv3tov_type[8]= {
98	VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO
99};
100
101int nfs_ticks;
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		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>
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 */
1494int
1495nfs_namei(ndp, fhp, len, slp, nam, mdp, dposp, retdirp, p, kerbflag, pubflag)
1496	register struct nameidata *ndp;
1497	fhandle_t *fhp;
1498	int len;
1499	struct nfssvc_sock *slp;
1500	struct sockaddr *nam;
1501	struct mbuf **mdp;
1502	caddr_t *dposp;
1503	struct vnode **retdirp;
1504	struct proc *p;
1505	int kerbflag, pubflag;
1506{
1507	register int i, rem;
1508	register struct mbuf *md;
1509	register char *fromcp, *tocp, *cp;
1510	struct iovec aiov;
1511	struct uio auio;
1512	struct vnode *dp;
1513	int error, rdonly, linklen;
1514	struct componentname *cnp = &ndp->ni_cnd;
1515
1516	*retdirp = (struct vnode *)0;
1517	cnp->cn_pnbuf = zalloc(namei_zone);
1518
1519	/*
1520	 * Copy the name from the mbuf list to ndp->ni_pnbuf
1521	 * and set the various ndp fields appropriately.
1522	 */
1523	fromcp = *dposp;
1524	tocp = cnp->cn_pnbuf;
1525	md = *mdp;
1526	rem = mtod(md, caddr_t) + md->m_len - fromcp;
1527	cnp->cn_hash = 0;
1528	for (i = 0; i < len; i++) {
1529		while (rem == 0) {
1530			md = md->m_next;
1531			if (md == NULL) {
1532				error = EBADRPC;
1533				goto out;
1534			}
1535			fromcp = mtod(md, caddr_t);
1536			rem = md->m_len;
1537		}
1538		if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
1539			error = EACCES;
1540			goto out;
1541		}
1542		cnp->cn_hash += (unsigned char)*fromcp;
1543		*tocp++ = *fromcp++;
1544		rem--;
1545	}
1546	*tocp = '\0';
1547	*mdp = md;
1548	*dposp = fromcp;
1549	len = nfsm_rndup(len)-len;
1550	if (len > 0) {
1551		if (rem >= len)
1552			*dposp += len;
1553		else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
1554			goto out;
1555	}
1556
1557	/*
1558	 * Extract and set starting directory.
1559	 */
1560	error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp,
1561	    nam, &rdonly, kerbflag, pubflag);
1562	if (error)
1563		goto out;
1564	if (dp->v_type != VDIR) {
1565		vrele(dp);
1566		error = ENOTDIR;
1567		goto out;
1568	}
1569
1570	if (rdonly)
1571		cnp->cn_flags |= RDONLY;
1572
1573	*retdirp = dp;
1574
1575	if (pubflag) {
1576		/*
1577		 * Oh joy. For WebNFS, handle those pesky '%' escapes,
1578		 * and the 'native path' indicator.
1579		 */
1580		cp = zalloc(namei_zone);
1581		fromcp = cnp->cn_pnbuf;
1582		tocp = cp;
1583		if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
1584			switch ((unsigned char)*fromcp) {
1585			case WEBNFS_NATIVE_CHAR:
1586				/*
1587				 * 'Native' path for us is the same
1588				 * as a path according to the NFS spec,
1589				 * just skip the escape char.
1590				 */
1591				fromcp++;
1592				break;
1593			/*
1594			 * More may be added in the future, range 0x80-0xff
1595			 */
1596			default:
1597				error = EIO;
1598				zfree(namei_zone, cp);
1599				goto out;
1600			}
1601		}
1602		/*
1603		 * Translate the '%' escapes, URL-style.
1604		 */
1605		while (*fromcp != '\0') {
1606			if (*fromcp == WEBNFS_ESC_CHAR) {
1607				if (fromcp[1] != '\0' && fromcp[2] != '\0') {
1608					fromcp++;
1609					*tocp++ = HEXSTRTOI(fromcp);
1610					fromcp += 2;
1611					continue;
1612				} else {
1613					error = ENOENT;
1614					zfree(namei_zone, cp);
1615					goto out;
1616				}
1617			} else
1618				*tocp++ = *fromcp++;
1619		}
1620		*tocp = '\0';
1621		zfree(namei_zone, cnp->cn_pnbuf);
1622		cnp->cn_pnbuf = cp;
1623	}
1624
1625	ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1;
1626	ndp->ni_segflg = UIO_SYSSPACE;
1627
1628	if (pubflag) {
1629		ndp->ni_rootdir = rootvnode;
1630		ndp->ni_loopcnt = 0;
1631		if (cnp->cn_pnbuf[0] == '/')
1632			dp = rootvnode;
1633	} else {
1634		cnp->cn_flags |= NOCROSSMOUNT;
1635	}
1636
1637	cnp->cn_proc = p;
1638	VREF(dp);
1639
1640    for (;;) {
1641	cnp->cn_nameptr = cnp->cn_pnbuf;
1642	ndp->ni_startdir = dp;
1643	/*
1644	 * And call lookup() to do the real work
1645	 */
1646	error = lookup(ndp);
1647	if (error)
1648		break;
1649	/*
1650	 * Check for encountering a symbolic link
1651	 */
1652	if ((cnp->cn_flags & ISSYMLINK) == 0) {
1653		nfsrv_object_create(ndp->ni_vp);
1654		if (cnp->cn_flags & (SAVENAME | SAVESTART)) {
1655			cnp->cn_flags |= HASBUF;
1656			return (0);
1657		}
1658		break;
1659	} else {
1660		if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
1661			VOP_UNLOCK(ndp->ni_dvp, 0, p);
1662		if (!pubflag) {
1663			vrele(ndp->ni_dvp);
1664			vput(ndp->ni_vp);
1665			ndp->ni_vp = NULL;
1666			error = EINVAL;
1667			break;
1668		}
1669
1670		if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
1671			error = ELOOP;
1672			break;
1673		}
1674		if (ndp->ni_pathlen > 1)
1675			cp = zalloc(namei_zone);
1676		else
1677			cp = cnp->cn_pnbuf;
1678		aiov.iov_base = cp;
1679		aiov.iov_len = MAXPATHLEN;
1680		auio.uio_iov = &aiov;
1681		auio.uio_iovcnt = 1;
1682		auio.uio_offset = 0;
1683		auio.uio_rw = UIO_READ;
1684		auio.uio_segflg = UIO_SYSSPACE;
1685		auio.uio_procp = (struct proc *)0;
1686		auio.uio_resid = MAXPATHLEN;
1687		error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
1688		if (error) {
1689		badlink:
1690			if (ndp->ni_pathlen > 1)
1691				zfree(namei_zone, cp);
1692			break;
1693		}
1694		linklen = MAXPATHLEN - auio.uio_resid;
1695		if (linklen == 0) {
1696			error = ENOENT;
1697			goto badlink;
1698		}
1699		if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
1700			error = ENAMETOOLONG;
1701			goto badlink;
1702		}
1703		if (ndp->ni_pathlen > 1) {
1704			bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
1705			zfree(namei_zone, cnp->cn_pnbuf);
1706			cnp->cn_pnbuf = cp;
1707		} else
1708			cnp->cn_pnbuf[linklen] = '\0';
1709		ndp->ni_pathlen += linklen;
1710		vput(ndp->ni_vp);
1711		dp = ndp->ni_dvp;
1712		/*
1713		 * Check if root directory should replace current directory.
1714		 */
1715		if (cnp->cn_pnbuf[0] == '/') {
1716			vrele(dp);
1717			dp = ndp->ni_rootdir;
1718			VREF(dp);
1719		}
1720	}
1721   }
1722out:
1723	zfree(namei_zone, cnp->cn_pnbuf);
1724	return (error);
1725}
1726
1727/*
1728 * A fiddled version of m_adj() that ensures null fill to a long
1729 * boundary and only trims off the back end
1730 */
1731void
1732nfsm_adj(mp, len, nul)
1733	struct mbuf *mp;
1734	register int len;
1735	int nul;
1736{
1737	register struct mbuf *m;
1738	register int count, i;
1739	register char *cp;
1740
1741	/*
1742	 * Trim from tail.  Scan the mbuf chain,
1743	 * calculating its length and finding the last mbuf.
1744	 * If the adjustment only affects this mbuf, then just
1745	 * adjust and return.  Otherwise, rescan and truncate
1746	 * after the remaining size.
1747	 */
1748	count = 0;
1749	m = mp;
1750	for (;;) {
1751		count += m->m_len;
1752		if (m->m_next == (struct mbuf *)0)
1753			break;
1754		m = m->m_next;
1755	}
1756	if (m->m_len > len) {
1757		m->m_len -= len;
1758		if (nul > 0) {
1759			cp = mtod(m, caddr_t)+m->m_len-nul;
1760			for (i = 0; i < nul; i++)
1761				*cp++ = '\0';
1762		}
1763		return;
1764	}
1765	count -= len;
1766	if (count < 0)
1767		count = 0;
1768	/*
1769	 * Correct length for chain is "count".
1770	 * Find the mbuf with last data, adjust its length,
1771	 * and toss data from remaining mbufs on chain.
1772	 */
1773	for (m = mp; m; m = m->m_next) {
1774		if (m->m_len >= count) {
1775			m->m_len = count;
1776			if (nul > 0) {
1777				cp = mtod(m, caddr_t)+m->m_len-nul;
1778				for (i = 0; i < nul; i++)
1779					*cp++ = '\0';
1780			}
1781			break;
1782		}
1783		count -= m->m_len;
1784	}
1785	for (m = m->m_next;m;m = m->m_next)
1786		m->m_len = 0;
1787}
1788
1789/*
1790 * Make these functions instead of macros, so that the kernel text size
1791 * doesn't get too big...
1792 */
1793void
1794nfsm_srvwcc(nfsd, before_ret, before_vap, after_ret, after_vap, mbp, bposp)
1795	struct nfsrv_descript *nfsd;
1796	int before_ret;
1797	register struct vattr *before_vap;
1798	int after_ret;
1799	struct vattr *after_vap;
1800	struct mbuf **mbp;
1801	char **bposp;
1802{
1803	register struct mbuf *mb = *mbp, *mb2;
1804	register char *bpos = *bposp;
1805	register u_int32_t *tl;
1806
1807	if (before_ret) {
1808		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1809		*tl = nfs_false;
1810	} else {
1811		nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
1812		*tl++ = nfs_true;
1813		txdr_hyper(&(before_vap->va_size), tl);
1814		tl += 2;
1815		txdr_nfsv3time(&(before_vap->va_mtime), tl);
1816		tl += 2;
1817		txdr_nfsv3time(&(before_vap->va_ctime), tl);
1818	}
1819	*bposp = bpos;
1820	*mbp = mb;
1821	nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
1822}
1823
1824void
1825nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp)
1826	struct nfsrv_descript *nfsd;
1827	int after_ret;
1828	struct vattr *after_vap;
1829	struct mbuf **mbp;
1830	char **bposp;
1831{
1832	register struct mbuf *mb = *mbp, *mb2;
1833	register char *bpos = *bposp;
1834	register u_int32_t *tl;
1835	register struct nfs_fattr *fp;
1836
1837	if (after_ret) {
1838		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1839		*tl = nfs_false;
1840	} else {
1841		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR);
1842		*tl++ = nfs_true;
1843		fp = (struct nfs_fattr *)tl;
1844		nfsm_srvfattr(nfsd, after_vap, fp);
1845	}
1846	*mbp = mb;
1847	*bposp = bpos;
1848}
1849
1850void
1851nfsm_srvfattr(nfsd, vap, fp)
1852	register struct nfsrv_descript *nfsd;
1853	register struct vattr *vap;
1854	register struct nfs_fattr *fp;
1855{
1856
1857	fp->fa_nlink = txdr_unsigned(vap->va_nlink);
1858	fp->fa_uid = txdr_unsigned(vap->va_uid);
1859	fp->fa_gid = txdr_unsigned(vap->va_gid);
1860	if (nfsd->nd_flag & ND_NFSV3) {
1861		fp->fa_type = vtonfsv3_type(vap->va_type);
1862		fp->fa_mode = vtonfsv3_mode(vap->va_mode);
1863		txdr_hyper(&vap->va_size, &fp->fa3_size);
1864		txdr_hyper(&vap->va_bytes, &fp->fa3_used);
1865		fp->fa3_rdev.specdata1 = txdr_unsigned(umajor(vap->va_rdev));
1866		fp->fa3_rdev.specdata2 = txdr_unsigned(uminor(vap->va_rdev));
1867		fp->fa3_fsid.nfsuquad[0] = 0;
1868		fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
1869		fp->fa3_fileid.nfsuquad[0] = 0;
1870		fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid);
1871		txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
1872		txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
1873		txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
1874	} else {
1875		fp->fa_type = vtonfsv2_type(vap->va_type);
1876		fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1877		fp->fa2_size = txdr_unsigned(vap->va_size);
1878		fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
1879		if (vap->va_type == VFIFO)
1880			fp->fa2_rdev = 0xffffffff;
1881		else
1882			fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
1883		fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
1884		fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
1885		fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
1886		txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
1887		txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
1888		txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
1889	}
1890}
1891
1892/*
1893 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
1894 * 	- look up fsid in mount list (if not found ret error)
1895 *	- get vp and export rights by calling VFS_FHTOVP()
1896 *	- if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
1897 *	- if not lockflag unlock it with VOP_UNLOCK()
1898 */
1899int
1900nfsrv_fhtovp(fhp, lockflag, vpp, cred, slp, nam, rdonlyp, kerbflag, pubflag)
1901	fhandle_t *fhp;
1902	int lockflag;
1903	struct vnode **vpp;
1904	struct ucred *cred;
1905	struct nfssvc_sock *slp;
1906	struct sockaddr *nam;
1907	int *rdonlyp;
1908	int kerbflag;
1909	int pubflag;
1910{
1911	struct proc *p = curproc; /* XXX */
1912	register struct mount *mp;
1913	register int i;
1914	struct ucred *credanon;
1915	int error, exflags;
1916#ifdef MNT_EXNORESPORT		/* XXX needs mountd and /etc/exports help yet */
1917	struct sockaddr_int *saddr;
1918#endif
1919
1920	*vpp = (struct vnode *)0;
1921
1922	if (nfs_ispublicfh(fhp)) {
1923		if (!pubflag || !nfs_pub.np_valid)
1924			return (ESTALE);
1925		fhp = &nfs_pub.np_handle;
1926	}
1927
1928	mp = vfs_getvfs(&fhp->fh_fsid);
1929	if (!mp)
1930		return (ESTALE);
1931	error = VFS_FHTOVP(mp, &fhp->fh_fid, nam, vpp, &exflags, &credanon);
1932	if (error)
1933		return (error);
1934#ifdef MNT_EXNORESPORT
1935	if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) {
1936		saddr = (struct sockaddr_in *)nam;
1937		if (saddr->sin_family == AF_INET &&
1938		    ntohs(saddr->sin_port) >= IPPORT_RESERVED) {
1939			vput(*vpp);
1940			return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1941		}
1942	}
1943#endif
1944	/*
1945	 * Check/setup credentials.
1946	 */
1947	if (exflags & MNT_EXKERB) {
1948		if (!kerbflag) {
1949			vput(*vpp);
1950			return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1951		}
1952	} else if (kerbflag) {
1953		vput(*vpp);
1954		return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1955	} else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1956		cred->cr_uid = credanon->cr_uid;
1957		for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++)
1958			cred->cr_groups[i] = credanon->cr_groups[i];
1959		cred->cr_ngroups = i;
1960	}
1961	if (exflags & MNT_EXRDONLY)
1962		*rdonlyp = 1;
1963	else
1964		*rdonlyp = 0;
1965
1966	nfsrv_object_create(*vpp);
1967
1968	if (!lockflag)
1969		VOP_UNLOCK(*vpp, 0, p);
1970	return (0);
1971}
1972
1973
1974/*
1975 * WebNFS: check if a filehandle is a public filehandle. For v3, this
1976 * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has
1977 * transformed this to all zeroes in both cases, so check for it.
1978 */
1979int
1980nfs_ispublicfh(fhp)
1981	fhandle_t *fhp;
1982{
1983	char *cp = (char *)fhp;
1984	int i;
1985
1986	for (i = 0; i < NFSX_V3FH; i++)
1987		if (*cp++ != 0)
1988			return (FALSE);
1989	return (TRUE);
1990}
1991
1992#endif /* NFS_NOSERVER */
1993/*
1994 * This function compares two net addresses by family and returns TRUE
1995 * if they are the same host.
1996 * If there is any doubt, return FALSE.
1997 * The AF_INET family is handled as a special case so that address mbufs
1998 * don't need to be saved to store "struct in_addr", which is only 4 bytes.
1999 */
2000int
2001netaddr_match(family, haddr, nam)
2002	int family;
2003	union nethostaddr *haddr;
2004	struct sockaddr *nam;
2005{
2006	register struct sockaddr_in *inetaddr;
2007
2008	switch (family) {
2009	case AF_INET:
2010		inetaddr = (struct sockaddr_in *)nam;
2011		if (inetaddr->sin_family == AF_INET &&
2012		    inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
2013			return (1);
2014		break;
2015#ifdef ISO
2016	case AF_ISO:
2017	    {
2018		register struct sockaddr_iso *isoaddr1, *isoaddr2;
2019
2020		isoaddr1 = (struct sockaddr_iso *)nam;
2021		isoaddr2 = (struct sockaddr_iso *)haddr->had_nam;
2022		if (isoaddr1->siso_family == AF_ISO &&
2023		    isoaddr1->siso_nlen > 0 &&
2024		    isoaddr1->siso_nlen == isoaddr2->siso_nlen &&
2025		    SAME_ISOADDR(isoaddr1, isoaddr2))
2026			return (1);
2027		break;
2028	    }
2029#endif	/* ISO */
2030	default:
2031		break;
2032	};
2033	return (0);
2034}
2035
2036static nfsuint64 nfs_nullcookie = { { 0, 0 } };
2037/*
2038 * This function finds the directory cookie that corresponds to the
2039 * logical byte offset given.
2040 */
2041nfsuint64 *
2042nfs_getcookie(np, off, add)
2043	register struct nfsnode *np;
2044	off_t off;
2045	int add;
2046{
2047	register struct nfsdmap *dp, *dp2;
2048	register int pos;
2049
2050	pos = (uoff_t)off / NFS_DIRBLKSIZ;
2051	if (pos == 0 || off < 0) {
2052#ifdef DIAGNOSTIC
2053		if (add)
2054			panic("nfs getcookie add at <= 0");
2055#endif
2056		return (&nfs_nullcookie);
2057	}
2058	pos--;
2059	dp = np->n_cookies.lh_first;
2060	if (!dp) {
2061		if (add) {
2062			MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap),
2063				M_NFSDIROFF, M_WAITOK);
2064			dp->ndm_eocookie = 0;
2065			LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
2066		} else
2067			return ((nfsuint64 *)0);
2068	}
2069	while (pos >= NFSNUMCOOKIES) {
2070		pos -= NFSNUMCOOKIES;
2071		if (dp->ndm_list.le_next) {
2072			if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
2073				pos >= dp->ndm_eocookie)
2074				return ((nfsuint64 *)0);
2075			dp = dp->ndm_list.le_next;
2076		} else if (add) {
2077			MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap),
2078				M_NFSDIROFF, M_WAITOK);
2079			dp2->ndm_eocookie = 0;
2080			LIST_INSERT_AFTER(dp, dp2, ndm_list);
2081			dp = dp2;
2082		} else
2083			return ((nfsuint64 *)0);
2084	}
2085	if (pos >= dp->ndm_eocookie) {
2086		if (add)
2087			dp->ndm_eocookie = pos + 1;
2088		else
2089			return ((nfsuint64 *)0);
2090	}
2091	return (&dp->ndm_cookies[pos]);
2092}
2093
2094/*
2095 * Invalidate cached directory information, except for the actual directory
2096 * blocks (which are invalidated separately).
2097 * Done mainly to avoid the use of stale offset cookies.
2098 */
2099void
2100nfs_invaldir(vp)
2101	register struct vnode *vp;
2102{
2103	register struct nfsnode *np = VTONFS(vp);
2104
2105#ifdef DIAGNOSTIC
2106	if (vp->v_type != VDIR)
2107		panic("nfs: invaldir not dir");
2108#endif
2109	np->n_direofoffset = 0;
2110	np->n_cookieverf.nfsuquad[0] = 0;
2111	np->n_cookieverf.nfsuquad[1] = 0;
2112	if (np->n_cookies.lh_first)
2113		np->n_cookies.lh_first->ndm_eocookie = 0;
2114}
2115
2116/*
2117 * The write verifier has changed (probably due to a server reboot), so all
2118 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
2119 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
2120 * flag. Once done the new write verifier can be set for the mount point.
2121 */
2122void
2123nfs_clearcommit(mp)
2124	struct mount *mp;
2125{
2126	register struct vnode *vp, *nvp;
2127	register struct buf *bp, *nbp;
2128	int s;
2129
2130	s = splbio();
2131loop:
2132	for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) {
2133		if (vp->v_mount != mp)	/* Paranoia */
2134			goto loop;
2135		nvp = vp->v_mntvnodes.le_next;
2136		for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2137			nbp = TAILQ_NEXT(bp, b_vnbufs);
2138			if ((bp->b_flags & (B_BUSY | B_DELWRI | B_NEEDCOMMIT))
2139				== (B_DELWRI | B_NEEDCOMMIT))
2140				bp->b_flags &= ~B_NEEDCOMMIT;
2141		}
2142	}
2143	splx(s);
2144}
2145
2146#ifndef NFS_NOSERVER
2147/*
2148 * Map errnos to NFS error numbers. For Version 3 also filter out error
2149 * numbers not specified for the associated procedure.
2150 */
2151int
2152nfsrv_errmap(nd, err)
2153	struct nfsrv_descript *nd;
2154	register int err;
2155{
2156	register short *defaulterrp, *errp;
2157
2158	if (nd->nd_flag & ND_NFSV3) {
2159	    if (nd->nd_procnum <= NFSPROC_COMMIT) {
2160		errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
2161		while (*++errp) {
2162			if (*errp == err)
2163				return (err);
2164			else if (*errp > err)
2165				break;
2166		}
2167		return ((int)*defaulterrp);
2168	    } else
2169		return (err & 0xffff);
2170	}
2171	if (err <= ELAST)
2172		return ((int)nfsrv_v2errmap[err - 1]);
2173	return (NFSERR_IO);
2174}
2175
2176int
2177nfsrv_object_create(vp)
2178	struct vnode *vp;
2179{
2180
2181	if (vp == NULL || vp->v_type != VREG)
2182		return (1);
2183	return (vfs_object_create(vp, curproc,
2184				  curproc ? curproc->p_ucred : NULL));
2185}
2186
2187/*
2188 * Sort the group list in increasing numerical order.
2189 * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
2190 *  that used to be here.)
2191 */
2192void
2193nfsrvw_sort(list, num)
2194        register gid_t *list;
2195        register int num;
2196{
2197	register int i, j;
2198	gid_t v;
2199
2200	/* Insertion sort. */
2201	for (i = 1; i < num; i++) {
2202		v = list[i];
2203		/* find correct slot for value v, moving others up */
2204		for (j = i; --j >= 0 && v < list[j];)
2205			list[j + 1] = list[j];
2206		list[j + 1] = v;
2207	}
2208}
2209
2210/*
2211 * copy credentials making sure that the result can be compared with bcmp().
2212 */
2213void
2214nfsrv_setcred(incred, outcred)
2215	register struct ucred *incred, *outcred;
2216{
2217	register int i;
2218
2219	bzero((caddr_t)outcred, sizeof (struct ucred));
2220	outcred->cr_ref = 1;
2221	outcred->cr_uid = incred->cr_uid;
2222	outcred->cr_ngroups = incred->cr_ngroups;
2223	for (i = 0; i < incred->cr_ngroups; i++)
2224		outcred->cr_groups[i] = incred->cr_groups[i];
2225	nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups);
2226}
2227#endif /* NFS_NOSERVER */
2228