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