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