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