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