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