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