nfs_srvsubs.c revision 50405
1/*
2 * Copyright (c) 1989, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 *	@(#)nfs_subs.c  8.8 (Berkeley) 5/22/95
37 * $Id: nfs_subs.c,v 1.80 1999/08/08 18:42:55 phk Exp $
38 */
39
40/*
41 * These functions support the macros and help fiddle mbuf chains for
42 * the nfs op functions. They do things like create the rpc header and
43 * copy data between mbuf chains and uio lists.
44 */
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/kernel.h>
48#include <sys/buf.h>
49#include <sys/proc.h>
50#include <sys/mount.h>
51#include <sys/vnode.h>
52#include <sys/namei.h>
53#include <sys/mbuf.h>
54#include <sys/socket.h>
55#include <sys/stat.h>
56#include <sys/malloc.h>
57#include <sys/sysent.h>
58#include <sys/syscall.h>
59#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	struct vnode *nvp;
1252	int v3 = NFS_ISV3(vp);
1253
1254	md = *mdp;
1255	t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
1256	if ((error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2)) != 0)
1257		return (error);
1258	fp = (struct nfs_fattr *)cp2;
1259	if (v3) {
1260		vtyp = nfsv3tov_type(fp->fa_type);
1261		vmode = fxdr_unsigned(u_short, fp->fa_mode);
1262		rdev = makeudev(fxdr_unsigned(int, fp->fa3_rdev.specdata1),
1263			fxdr_unsigned(int, fp->fa3_rdev.specdata2));
1264		fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
1265	} else {
1266		vtyp = nfsv2tov_type(fp->fa_type);
1267		vmode = fxdr_unsigned(u_short, fp->fa_mode);
1268		/*
1269		 * XXX
1270		 *
1271		 * The duplicate information returned in fa_type and fa_mode
1272		 * is an ambiguity in the NFS version 2 protocol.
1273		 *
1274		 * VREG should be taken literally as a regular file.  If a
1275		 * server intents to return some type information differently
1276		 * in the upper bits of the mode field (e.g. for sockets, or
1277		 * FIFOs), NFSv2 mandates fa_type to be VNON.  Anyway, we
1278		 * leave the examination of the mode bits even in the VREG
1279		 * case to avoid breakage for bogus servers, but we make sure
1280		 * that there are actually type bits set in the upper part of
1281		 * fa_mode (and failing that, trust the va_type field).
1282		 *
1283		 * NFSv3 cleared the issue, and requires fa_mode to not
1284		 * contain any type information (while also introduing sockets
1285		 * and FIFOs for fa_type).
1286		 */
1287		if (vtyp == VNON || (vtyp == VREG && (vmode & S_IFMT) != 0))
1288			vtyp = IFTOVT(vmode);
1289		rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
1290		fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
1291
1292		/*
1293		 * Really ugly NFSv2 kludge.
1294		 */
1295		if (vtyp == VCHR && rdev == 0xffffffff)
1296			vtyp = VFIFO;
1297	}
1298
1299	/*
1300	 * If v_type == VNON it is a new node, so fill in the v_type,
1301	 * n_mtime fields. Check to see if it represents a special
1302	 * device, and if so, check for a possible alias. Once the
1303	 * correct vnode has been obtained, fill in the rest of the
1304	 * information.
1305	 */
1306	np = VTONFS(vp);
1307	if (vp->v_type != vtyp) {
1308		vp->v_type = vtyp;
1309		if (vp->v_type == VFIFO) {
1310			vp->v_op = fifo_nfsv2nodeop_p;
1311		}
1312		if (vp->v_type == VCHR || vp->v_type == VBLK) {
1313			vp->v_op = spec_nfsv2nodeop_p;
1314			addaliasu(vp, rdev);
1315		}
1316		np->n_mtime = mtime.tv_sec;
1317	}
1318	vap = &np->n_vattr;
1319	vap->va_type = vtyp;
1320	vap->va_mode = (vmode & 07777);
1321	vap->va_rdev = rdev;
1322	vap->va_mtime = mtime;
1323	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1324	if (v3) {
1325		vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1326		vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1327		vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1328		vap->va_size = fxdr_hyper(&fp->fa3_size);
1329		vap->va_blocksize = NFS_FABLKSIZE;
1330		vap->va_bytes = fxdr_hyper(&fp->fa3_used);
1331		vap->va_fileid = fxdr_unsigned(int32_t,
1332		    fp->fa3_fileid.nfsuquad[1]);
1333		fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
1334		fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
1335		vap->va_flags = 0;
1336		vap->va_filerev = 0;
1337	} else {
1338		vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1339		vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1340		vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1341		vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
1342		vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
1343		vap->va_bytes = (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks)
1344		    * NFS_FABLKSIZE;
1345		vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
1346		fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
1347		vap->va_flags = 0;
1348		vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
1349		    fp->fa2_ctime.nfsv2_sec);
1350		vap->va_ctime.tv_nsec = 0;
1351		vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec);
1352		vap->va_filerev = 0;
1353	}
1354	if (vap->va_size != np->n_size) {
1355		if (vap->va_type == VREG) {
1356			if (np->n_flag & NMODIFIED) {
1357				if (vap->va_size < np->n_size)
1358					vap->va_size = np->n_size;
1359				else
1360					np->n_size = vap->va_size;
1361			} else
1362				np->n_size = vap->va_size;
1363			vnode_pager_setsize(vp, np->n_size);
1364		} else
1365			np->n_size = vap->va_size;
1366	}
1367	np->n_attrstamp = time_second;
1368	if (vaper != NULL) {
1369		bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
1370		if (np->n_flag & NCHG) {
1371			if (np->n_flag & NACC)
1372				vaper->va_atime = np->n_atim;
1373			if (np->n_flag & NUPD)
1374				vaper->va_mtime = np->n_mtim;
1375		}
1376	}
1377	return (0);
1378}
1379
1380#ifdef NFS_ACDEBUG
1381#include <sys/sysctl.h>
1382SYSCTL_DECL(_vfs_nfs);
1383static int nfs_acdebug;
1384SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, "");
1385#endif
1386
1387/*
1388 * Check the time stamp
1389 * If the cache is valid, copy contents to *vap and return 0
1390 * otherwise return an error
1391 */
1392int
1393nfs_getattrcache(vp, vaper)
1394	register struct vnode *vp;
1395	struct vattr *vaper;
1396{
1397	register struct nfsnode *np;
1398	register struct vattr *vap;
1399	struct nfsmount *nmp;
1400	int timeo;
1401
1402	np = VTONFS(vp);
1403	vap = &np->n_vattr;
1404	nmp = VFSTONFS(vp->v_mount);
1405	/* XXX n_mtime doesn't seem to be updated on a miss-and-reload */
1406	timeo = (time_second - np->n_mtime) / 10;
1407
1408#ifdef NFS_ACDEBUG
1409	if (nfs_acdebug>1)
1410		printf("nfs_getattrcache: initial timeo = %d\n", timeo);
1411#endif
1412
1413	if (vap->va_type == VDIR) {
1414		if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin)
1415			timeo = nmp->nm_acdirmin;
1416		else if (timeo > nmp->nm_acdirmax)
1417			timeo = nmp->nm_acdirmax;
1418	} else {
1419		if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin)
1420			timeo = nmp->nm_acregmin;
1421		else if (timeo > nmp->nm_acregmax)
1422			timeo = nmp->nm_acregmax;
1423	}
1424
1425#ifdef NFS_ACDEBUG
1426	if (nfs_acdebug > 2)
1427		printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
1428			nmp->nm_acregmin, nmp->nm_acregmax,
1429			nmp->nm_acdirmin, nmp->nm_acdirmax);
1430
1431	if (nfs_acdebug)
1432		printf("nfs_getattrcache: age = %d; final timeo = %d\n",
1433			(time_second - np->n_attrstamp), timeo);
1434#endif
1435
1436	if ((time_second - np->n_attrstamp) >= timeo) {
1437		nfsstats.attrcache_misses++;
1438		return (ENOENT);
1439	}
1440	nfsstats.attrcache_hits++;
1441	if (vap->va_size != np->n_size) {
1442		if (vap->va_type == VREG) {
1443			if (np->n_flag & NMODIFIED) {
1444				if (vap->va_size < np->n_size)
1445					vap->va_size = np->n_size;
1446				else
1447					np->n_size = vap->va_size;
1448			} else
1449				np->n_size = vap->va_size;
1450			vnode_pager_setsize(vp, np->n_size);
1451		} else
1452			np->n_size = vap->va_size;
1453	}
1454	bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
1455	if (np->n_flag & NCHG) {
1456		if (np->n_flag & NACC)
1457			vaper->va_atime = np->n_atim;
1458		if (np->n_flag & NUPD)
1459			vaper->va_mtime = np->n_mtim;
1460	}
1461	return (0);
1462}
1463
1464#ifndef NFS_NOSERVER
1465/*
1466 * Set up nameidata for a lookup() call and do it.
1467 *
1468 * If pubflag is set, this call is done for a lookup operation on the
1469 * public filehandle. In that case we allow crossing mountpoints and
1470 * absolute pathnames. However, the caller is expected to check that
1471 * the lookup result is within the public fs, and deny access if
1472 * it is not.
1473 *
1474 * nfs_namei() clears out garbage fields that namei() might leave garbage.
1475 * This is mainly ni_vp and ni_dvp when an error occurs, and ni_dvp when no
1476 * error occurs but the parent was not requested.
1477 *
1478 * dirp may be set whether an error is returned or not, and must be
1479 * released by the caller.
1480 */
1481int
1482nfs_namei(ndp, fhp, len, slp, nam, mdp, dposp, retdirp, p, kerbflag, pubflag)
1483	register struct nameidata *ndp;
1484	fhandle_t *fhp;
1485	int len;
1486	struct nfssvc_sock *slp;
1487	struct sockaddr *nam;
1488	struct mbuf **mdp;
1489	caddr_t *dposp;
1490	struct vnode **retdirp;
1491	struct proc *p;
1492	int kerbflag, pubflag;
1493{
1494	register int i, rem;
1495	register struct mbuf *md;
1496	register char *fromcp, *tocp, *cp;
1497	struct iovec aiov;
1498	struct uio auio;
1499	struct vnode *dp;
1500	int error, rdonly, linklen;
1501	struct componentname *cnp = &ndp->ni_cnd;
1502
1503	*retdirp = (struct vnode *)0;
1504	cnp->cn_pnbuf = zalloc(namei_zone);
1505
1506	/*
1507	 * Copy the name from the mbuf list to ndp->ni_pnbuf
1508	 * and set the various ndp fields appropriately.
1509	 */
1510	fromcp = *dposp;
1511	tocp = cnp->cn_pnbuf;
1512	md = *mdp;
1513	rem = mtod(md, caddr_t) + md->m_len - fromcp;
1514	cnp->cn_hash = 0;
1515	for (i = 0; i < len; i++) {
1516		while (rem == 0) {
1517			md = md->m_next;
1518			if (md == NULL) {
1519				error = EBADRPC;
1520				goto out;
1521			}
1522			fromcp = mtod(md, caddr_t);
1523			rem = md->m_len;
1524		}
1525		if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
1526			error = EACCES;
1527			goto out;
1528		}
1529		cnp->cn_hash += (unsigned char)*fromcp;
1530		*tocp++ = *fromcp++;
1531		rem--;
1532	}
1533	*tocp = '\0';
1534	*mdp = md;
1535	*dposp = fromcp;
1536	len = nfsm_rndup(len)-len;
1537	if (len > 0) {
1538		if (rem >= len)
1539			*dposp += len;
1540		else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
1541			goto out;
1542	}
1543
1544	/*
1545	 * Extract and set starting directory.
1546	 */
1547	error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp,
1548	    nam, &rdonly, kerbflag, pubflag);
1549	if (error)
1550		goto out;
1551	if (dp->v_type != VDIR) {
1552		vrele(dp);
1553		error = ENOTDIR;
1554		goto out;
1555	}
1556
1557	if (rdonly)
1558		cnp->cn_flags |= RDONLY;
1559
1560	/*
1561	 * Set return directory.  Reference to dp is implicitly transfered
1562	 * to the returned pointer
1563	 */
1564	*retdirp = dp;
1565
1566	if (pubflag) {
1567		/*
1568		 * Oh joy. For WebNFS, handle those pesky '%' escapes,
1569		 * and the 'native path' indicator.
1570		 */
1571		cp = zalloc(namei_zone);
1572		fromcp = cnp->cn_pnbuf;
1573		tocp = cp;
1574		if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
1575			switch ((unsigned char)*fromcp) {
1576			case WEBNFS_NATIVE_CHAR:
1577				/*
1578				 * 'Native' path for us is the same
1579				 * as a path according to the NFS spec,
1580				 * just skip the escape char.
1581				 */
1582				fromcp++;
1583				break;
1584			/*
1585			 * More may be added in the future, range 0x80-0xff
1586			 */
1587			default:
1588				error = EIO;
1589				zfree(namei_zone, cp);
1590				goto out;
1591			}
1592		}
1593		/*
1594		 * Translate the '%' escapes, URL-style.
1595		 */
1596		while (*fromcp != '\0') {
1597			if (*fromcp == WEBNFS_ESC_CHAR) {
1598				if (fromcp[1] != '\0' && fromcp[2] != '\0') {
1599					fromcp++;
1600					*tocp++ = HEXSTRTOI(fromcp);
1601					fromcp += 2;
1602					continue;
1603				} else {
1604					error = ENOENT;
1605					zfree(namei_zone, cp);
1606					goto out;
1607				}
1608			} else
1609				*tocp++ = *fromcp++;
1610		}
1611		*tocp = '\0';
1612		zfree(namei_zone, cnp->cn_pnbuf);
1613		cnp->cn_pnbuf = cp;
1614	}
1615
1616	ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1;
1617	ndp->ni_segflg = UIO_SYSSPACE;
1618
1619	if (pubflag) {
1620		ndp->ni_rootdir = rootvnode;
1621		ndp->ni_loopcnt = 0;
1622		if (cnp->cn_pnbuf[0] == '/')
1623			dp = rootvnode;
1624	} else {
1625		cnp->cn_flags |= NOCROSSMOUNT;
1626	}
1627
1628	/*
1629	 * Initialize for scan, set ni_startdir and bump ref on dp again
1630	 * becuase lookup() will dereference ni_startdir.
1631	 */
1632
1633	cnp->cn_proc = p;
1634	VREF(dp);
1635	ndp->ni_startdir = dp;
1636
1637	for (;;) {
1638		cnp->cn_nameptr = cnp->cn_pnbuf;
1639		/*
1640		 * Call lookup() to do the real work.  If an error occurs,
1641		 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
1642		 * we do not have to dereference anything before returning.
1643		 * In either case ni_startdir will be dereferenced and NULLed
1644		 * out.
1645		 */
1646		error = lookup(ndp);
1647		if (error)
1648			break;
1649
1650		/*
1651		 * Check for encountering a symbolic link.  Trivial
1652		 * termination occurs if no symlink encountered.
1653		 * Note: zfree is safe because error is 0, so we will
1654		 * not zfree it again when we break.
1655		 */
1656		if ((cnp->cn_flags & ISSYMLINK) == 0) {
1657			nfsrv_object_create(ndp->ni_vp);
1658			if (cnp->cn_flags & (SAVENAME | SAVESTART))
1659				cnp->cn_flags |= HASBUF;
1660			else
1661				zfree(namei_zone, cnp->cn_pnbuf);
1662			break;
1663		}
1664
1665		/*
1666		 * Validate symlink
1667		 */
1668		if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
1669			VOP_UNLOCK(ndp->ni_dvp, 0, p);
1670		if (!pubflag) {
1671			error = EINVAL;
1672			goto badlink2;
1673		}
1674
1675		if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
1676			error = ELOOP;
1677			goto badlink2;
1678		}
1679		if (ndp->ni_pathlen > 1)
1680			cp = zalloc(namei_zone);
1681		else
1682			cp = cnp->cn_pnbuf;
1683		aiov.iov_base = cp;
1684		aiov.iov_len = MAXPATHLEN;
1685		auio.uio_iov = &aiov;
1686		auio.uio_iovcnt = 1;
1687		auio.uio_offset = 0;
1688		auio.uio_rw = UIO_READ;
1689		auio.uio_segflg = UIO_SYSSPACE;
1690		auio.uio_procp = (struct proc *)0;
1691		auio.uio_resid = MAXPATHLEN;
1692		error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
1693		if (error) {
1694		badlink1:
1695			if (ndp->ni_pathlen > 1)
1696				zfree(namei_zone, cp);
1697		badlink2:
1698			vrele(ndp->ni_dvp);
1699			vput(ndp->ni_vp);
1700			break;
1701		}
1702		linklen = MAXPATHLEN - auio.uio_resid;
1703		if (linklen == 0) {
1704			error = ENOENT;
1705			goto badlink1;
1706		}
1707		if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
1708			error = ENAMETOOLONG;
1709			goto badlink1;
1710		}
1711
1712		/*
1713		 * Adjust or replace path
1714		 */
1715		if (ndp->ni_pathlen > 1) {
1716			bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
1717			zfree(namei_zone, cnp->cn_pnbuf);
1718			cnp->cn_pnbuf = cp;
1719		} else
1720			cnp->cn_pnbuf[linklen] = '\0';
1721		ndp->ni_pathlen += linklen;
1722
1723		/*
1724		 * Cleanup refs for next loop and check if root directory
1725		 * should replace current directory.  Normally ni_dvp
1726		 * becomes the new base directory and is cleaned up when
1727		 * we loop.  Explicitly null pointers after invalidation
1728		 * to clarify operation.
1729		 */
1730		vput(ndp->ni_vp);
1731		ndp->ni_vp = NULL;
1732
1733		if (cnp->cn_pnbuf[0] == '/') {
1734			vrele(ndp->ni_dvp);
1735			ndp->ni_dvp = ndp->ni_rootdir;
1736			VREF(ndp->ni_dvp);
1737		}
1738		ndp->ni_startdir = ndp->ni_dvp;
1739		ndp->ni_dvp = NULL;
1740	}
1741
1742	/*
1743	 * nfs_namei() guarentees that fields will not contain garbage
1744	 * whether an error occurs or not.  This allows the caller to track
1745	 * cleanup state trivially.
1746	 */
1747out:
1748	if (error) {
1749		zfree(namei_zone, cnp->cn_pnbuf);
1750		ndp->ni_vp = NULL;
1751		ndp->ni_dvp = NULL;
1752		ndp->ni_startdir = NULL;
1753		cnp->cn_flags &= ~HASBUF;
1754	} else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
1755		ndp->ni_dvp = NULL;
1756	}
1757	return (error);
1758}
1759
1760/*
1761 * A fiddled version of m_adj() that ensures null fill to a long
1762 * boundary and only trims off the back end
1763 */
1764void
1765nfsm_adj(mp, len, nul)
1766	struct mbuf *mp;
1767	register int len;
1768	int nul;
1769{
1770	register struct mbuf *m;
1771	register int count, i;
1772	register char *cp;
1773
1774	/*
1775	 * Trim from tail.  Scan the mbuf chain,
1776	 * calculating its length and finding the last mbuf.
1777	 * If the adjustment only affects this mbuf, then just
1778	 * adjust and return.  Otherwise, rescan and truncate
1779	 * after the remaining size.
1780	 */
1781	count = 0;
1782	m = mp;
1783	for (;;) {
1784		count += m->m_len;
1785		if (m->m_next == (struct mbuf *)0)
1786			break;
1787		m = m->m_next;
1788	}
1789	if (m->m_len > len) {
1790		m->m_len -= len;
1791		if (nul > 0) {
1792			cp = mtod(m, caddr_t)+m->m_len-nul;
1793			for (i = 0; i < nul; i++)
1794				*cp++ = '\0';
1795		}
1796		return;
1797	}
1798	count -= len;
1799	if (count < 0)
1800		count = 0;
1801	/*
1802	 * Correct length for chain is "count".
1803	 * Find the mbuf with last data, adjust its length,
1804	 * and toss data from remaining mbufs on chain.
1805	 */
1806	for (m = mp; m; m = m->m_next) {
1807		if (m->m_len >= count) {
1808			m->m_len = count;
1809			if (nul > 0) {
1810				cp = mtod(m, caddr_t)+m->m_len-nul;
1811				for (i = 0; i < nul; i++)
1812					*cp++ = '\0';
1813			}
1814			break;
1815		}
1816		count -= m->m_len;
1817	}
1818	for (m = m->m_next;m;m = m->m_next)
1819		m->m_len = 0;
1820}
1821
1822/*
1823 * Make these functions instead of macros, so that the kernel text size
1824 * doesn't get too big...
1825 */
1826void
1827nfsm_srvwcc(nfsd, before_ret, before_vap, after_ret, after_vap, mbp, bposp)
1828	struct nfsrv_descript *nfsd;
1829	int before_ret;
1830	register struct vattr *before_vap;
1831	int after_ret;
1832	struct vattr *after_vap;
1833	struct mbuf **mbp;
1834	char **bposp;
1835{
1836	register struct mbuf *mb = *mbp, *mb2;
1837	register char *bpos = *bposp;
1838	register u_int32_t *tl;
1839
1840	if (before_ret) {
1841		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1842		*tl = nfs_false;
1843	} else {
1844		nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
1845		*tl++ = nfs_true;
1846		txdr_hyper(before_vap->va_size, tl);
1847		tl += 2;
1848		txdr_nfsv3time(&(before_vap->va_mtime), tl);
1849		tl += 2;
1850		txdr_nfsv3time(&(before_vap->va_ctime), tl);
1851	}
1852	*bposp = bpos;
1853	*mbp = mb;
1854	nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
1855}
1856
1857void
1858nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp)
1859	struct nfsrv_descript *nfsd;
1860	int after_ret;
1861	struct vattr *after_vap;
1862	struct mbuf **mbp;
1863	char **bposp;
1864{
1865	register struct mbuf *mb = *mbp, *mb2;
1866	register char *bpos = *bposp;
1867	register u_int32_t *tl;
1868	register struct nfs_fattr *fp;
1869
1870	if (after_ret) {
1871		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1872		*tl = nfs_false;
1873	} else {
1874		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR);
1875		*tl++ = nfs_true;
1876		fp = (struct nfs_fattr *)tl;
1877		nfsm_srvfattr(nfsd, after_vap, fp);
1878	}
1879	*mbp = mb;
1880	*bposp = bpos;
1881}
1882
1883void
1884nfsm_srvfattr(nfsd, vap, fp)
1885	register struct nfsrv_descript *nfsd;
1886	register struct vattr *vap;
1887	register struct nfs_fattr *fp;
1888{
1889
1890	fp->fa_nlink = txdr_unsigned(vap->va_nlink);
1891	fp->fa_uid = txdr_unsigned(vap->va_uid);
1892	fp->fa_gid = txdr_unsigned(vap->va_gid);
1893	if (nfsd->nd_flag & ND_NFSV3) {
1894		fp->fa_type = vtonfsv3_type(vap->va_type);
1895		fp->fa_mode = vtonfsv3_mode(vap->va_mode);
1896		txdr_hyper(vap->va_size, &fp->fa3_size);
1897		txdr_hyper(vap->va_bytes, &fp->fa3_used);
1898		fp->fa3_rdev.specdata1 = txdr_unsigned(umajor(vap->va_rdev));
1899		fp->fa3_rdev.specdata2 = txdr_unsigned(uminor(vap->va_rdev));
1900		fp->fa3_fsid.nfsuquad[0] = 0;
1901		fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
1902		fp->fa3_fileid.nfsuquad[0] = 0;
1903		fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid);
1904		txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
1905		txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
1906		txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
1907	} else {
1908		fp->fa_type = vtonfsv2_type(vap->va_type);
1909		fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1910		fp->fa2_size = txdr_unsigned(vap->va_size);
1911		fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
1912		if (vap->va_type == VFIFO)
1913			fp->fa2_rdev = 0xffffffff;
1914		else
1915			fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
1916		fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
1917		fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
1918		fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
1919		txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
1920		txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
1921		txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
1922	}
1923}
1924
1925/*
1926 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
1927 * 	- look up fsid in mount list (if not found ret error)
1928 *	- get vp and export rights by calling VFS_FHTOVP()
1929 *	- if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
1930 *	- if not lockflag unlock it with VOP_UNLOCK()
1931 */
1932int
1933nfsrv_fhtovp(fhp, lockflag, vpp, cred, slp, nam, rdonlyp, kerbflag, pubflag)
1934	fhandle_t *fhp;
1935	int lockflag;
1936	struct vnode **vpp;
1937	struct ucred *cred;
1938	struct nfssvc_sock *slp;
1939	struct sockaddr *nam;
1940	int *rdonlyp;
1941	int kerbflag;
1942	int pubflag;
1943{
1944	struct proc *p = curproc; /* XXX */
1945	register struct mount *mp;
1946	register int i;
1947	struct ucred *credanon;
1948	int error, exflags;
1949#ifdef MNT_EXNORESPORT		/* XXX needs mountd and /etc/exports help yet */
1950	struct sockaddr_int *saddr;
1951#endif
1952
1953	*vpp = (struct vnode *)0;
1954
1955	if (nfs_ispublicfh(fhp)) {
1956		if (!pubflag || !nfs_pub.np_valid)
1957			return (ESTALE);
1958		fhp = &nfs_pub.np_handle;
1959	}
1960
1961	mp = vfs_getvfs(&fhp->fh_fsid);
1962	if (!mp)
1963		return (ESTALE);
1964	error = VFS_FHTOVP(mp, &fhp->fh_fid, nam, vpp, &exflags, &credanon);
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