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