nfs_srvsubs.c revision 30813
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.45 1997/10/28 14:06:22 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	struct proc *p = curproc;
1220
1221	md = *mdp;
1222	t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
1223	if (error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2))
1224		return (error);
1225	fp = (struct nfs_fattr *)cp2;
1226	if (v3) {
1227		vtyp = nfsv3tov_type(fp->fa_type);
1228		vmode = fxdr_unsigned(u_short, fp->fa_mode);
1229		rdev = makedev(fxdr_unsigned(int, fp->fa3_rdev.specdata1),
1230			fxdr_unsigned(int, fp->fa3_rdev.specdata2));
1231		fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
1232	} else {
1233		vtyp = nfsv2tov_type(fp->fa_type);
1234		vmode = fxdr_unsigned(u_short, fp->fa_mode);
1235		/*
1236		 * XXX
1237		 *
1238		 * The duplicate information returned in fa_type and fa_mode
1239		 * is an ambiguity in the NFS version 2 protocol.
1240		 *
1241		 * VREG should be taken literally as a regular file.  If a
1242		 * server intents to return some type information differently
1243		 * in the upper bits of the mode field (e.g. for sockets, or
1244		 * FIFOs), NFSv2 mandates fa_type to be VNON.  Anyway, we
1245		 * leave the examination of the mode bits even in the VREG
1246		 * case to avoid breakage for bogus servers, but we make sure
1247		 * that there are actually type bits set in the upper part of
1248		 * fa_mode (and failing that, trust the va_type field).
1249		 *
1250		 * NFSv3 cleared the issue, and requires fa_mode to not
1251		 * contain any type information (while also introduing sockets
1252		 * and FIFOs for fa_type).
1253		 */
1254		if (vtyp == VNON || (vtyp == VREG && (vmode & S_IFMT) != 0))
1255			vtyp = IFTOVT(vmode);
1256		rdev = fxdr_unsigned(long, fp->fa2_rdev);
1257		fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
1258
1259		/*
1260		 * Really ugly NFSv2 kludge.
1261		 */
1262		if (vtyp == VCHR && rdev == 0xffffffff)
1263			vtyp = VFIFO;
1264	}
1265
1266	/*
1267	 * If v_type == VNON it is a new node, so fill in the v_type,
1268	 * n_mtime fields. Check to see if it represents a special
1269	 * device, and if so, check for a possible alias. Once the
1270	 * correct vnode has been obtained, fill in the rest of the
1271	 * information.
1272	 */
1273	np = VTONFS(vp);
1274	if (vp->v_type != vtyp) {
1275		vp->v_type = vtyp;
1276		if (vp->v_type == VFIFO) {
1277			vp->v_op = fifo_nfsv2nodeop_p;
1278		}
1279		if (vp->v_type == VCHR || vp->v_type == VBLK) {
1280			vp->v_op = spec_nfsv2nodeop_p;
1281			nvp = checkalias(vp, (dev_t)rdev, vp->v_mount);
1282			if (nvp) {
1283				/*
1284				 * Discard unneeded vnode, but save its nfsnode.
1285				 * Since the nfsnode does not have a lock, its
1286				 * vnode lock has to be carried over.
1287				 */
1288				nvp->v_vnlock = vp->v_vnlock;
1289				vp->v_vnlock = NULL;
1290				nvp->v_data = vp->v_data;
1291				vp->v_data = NULL;
1292				vp->v_op = spec_vnodeop_p;
1293				vrele(vp);
1294				vgone(vp);
1295				/*
1296				 * Reinitialize aliased node.
1297				 */
1298				np->n_vnode = nvp;
1299				*vpp = vp = nvp;
1300			}
1301		}
1302		np->n_mtime = mtime.tv_sec;
1303	}
1304	vap = &np->n_vattr;
1305	vap->va_type = vtyp;
1306	vap->va_mode = (vmode & 07777);
1307	vap->va_rdev = (dev_t)rdev;
1308	vap->va_mtime = mtime;
1309	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1310	if (v3) {
1311		vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1312		vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1313		vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1314		fxdr_hyper(&fp->fa3_size, &vap->va_size);
1315		vap->va_blocksize = NFS_FABLKSIZE;
1316		fxdr_hyper(&fp->fa3_used, &vap->va_bytes);
1317		vap->va_fileid = fxdr_unsigned(int, fp->fa3_fileid.nfsuquad[1]);
1318		fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
1319		fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
1320		vap->va_flags = 0;
1321		vap->va_filerev = 0;
1322	} else {
1323		vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1324		vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1325		vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1326		vap->va_size = fxdr_unsigned(u_long, fp->fa2_size);
1327		vap->va_blocksize = fxdr_unsigned(long, fp->fa2_blocksize);
1328		vap->va_bytes = fxdr_unsigned(long, fp->fa2_blocks) * NFS_FABLKSIZE;
1329		vap->va_fileid = fxdr_unsigned(long, fp->fa2_fileid);
1330		fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
1331		vap->va_flags = 0;
1332		vap->va_ctime.tv_sec = fxdr_unsigned(long, fp->fa2_ctime.nfsv2_sec);
1333		vap->va_ctime.tv_nsec = 0;
1334		vap->va_gen = fxdr_unsigned(u_long, fp->fa2_ctime.nfsv2_usec);
1335		vap->va_filerev = 0;
1336	}
1337	if (vap->va_size != np->n_size) {
1338		if (vap->va_type == VREG) {
1339			if (np->n_flag & NMODIFIED) {
1340				if (vap->va_size < np->n_size)
1341					vap->va_size = np->n_size;
1342				else
1343					np->n_size = vap->va_size;
1344			} else
1345				np->n_size = vap->va_size;
1346			vnode_pager_setsize(vp, (u_long)np->n_size);
1347		} else
1348			np->n_size = vap->va_size;
1349	}
1350	np->n_attrstamp = time.tv_sec;
1351	if (vaper != NULL) {
1352		bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
1353		if (np->n_flag & NCHG) {
1354			if (np->n_flag & NACC)
1355				vaper->va_atime = np->n_atim;
1356			if (np->n_flag & NUPD)
1357				vaper->va_mtime = np->n_mtim;
1358		}
1359	}
1360	return (0);
1361}
1362
1363/*
1364 * Check the time stamp
1365 * If the cache is valid, copy contents to *vap and return 0
1366 * otherwise return an error
1367 */
1368int
1369nfs_getattrcache(vp, vaper)
1370	register struct vnode *vp;
1371	struct vattr *vaper;
1372{
1373	register struct nfsnode *np = VTONFS(vp);
1374	register struct vattr *vap;
1375
1376	if ((time.tv_sec - np->n_attrstamp) >= NFS_ATTRTIMEO(np)) {
1377		nfsstats.attrcache_misses++;
1378		return (ENOENT);
1379	}
1380	nfsstats.attrcache_hits++;
1381	vap = &np->n_vattr;
1382	if (vap->va_size != np->n_size) {
1383		if (vap->va_type == VREG) {
1384			if (np->n_flag & NMODIFIED) {
1385				if (vap->va_size < np->n_size)
1386					vap->va_size = np->n_size;
1387				else
1388					np->n_size = vap->va_size;
1389			} else
1390				np->n_size = vap->va_size;
1391			vnode_pager_setsize(vp, (u_long)np->n_size);
1392		} else
1393			np->n_size = vap->va_size;
1394	}
1395	bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
1396	if (np->n_flag & NCHG) {
1397		if (np->n_flag & NACC)
1398			vaper->va_atime = np->n_atim;
1399		if (np->n_flag & NUPD)
1400			vaper->va_mtime = np->n_mtim;
1401	}
1402	return (0);
1403}
1404
1405#ifndef NFS_NOSERVER
1406/*
1407 * Set up nameidata for a lookup() call and do it.
1408 *
1409 * If pubflag is set, this call is done for a lookup operation on the
1410 * public filehandle. In that case we allow crossing mountpoints and
1411 * absolute pathnames. However, the caller is expected to check that
1412 * the lookup result is within the public fs, and deny access if
1413 * it is not.
1414 */
1415int
1416nfs_namei(ndp, fhp, len, slp, nam, mdp, dposp, retdirp, p, kerbflag, pubflag)
1417	register struct nameidata *ndp;
1418	fhandle_t *fhp;
1419	int len;
1420	struct nfssvc_sock *slp;
1421	struct sockaddr *nam;
1422	struct mbuf **mdp;
1423	caddr_t *dposp;
1424	struct vnode **retdirp;
1425	struct proc *p;
1426	int kerbflag, pubflag;
1427{
1428	register int i, rem;
1429	register struct mbuf *md;
1430	register char *fromcp, *tocp, *cp;
1431	struct iovec aiov;
1432	struct uio auio;
1433	struct vnode *dp;
1434	int error, rdonly, linklen;
1435	struct componentname *cnp = &ndp->ni_cnd;
1436
1437	*retdirp = (struct vnode *)0;
1438	cnp->cn_pnbuf = zalloc(namei_zone);
1439
1440	/*
1441	 * Copy the name from the mbuf list to ndp->ni_pnbuf
1442	 * and set the various ndp fields appropriately.
1443	 */
1444	fromcp = *dposp;
1445	tocp = cnp->cn_pnbuf;
1446	md = *mdp;
1447	rem = mtod(md, caddr_t) + md->m_len - fromcp;
1448	cnp->cn_hash = 0;
1449	for (i = 0; i < len; i++) {
1450		while (rem == 0) {
1451			md = md->m_next;
1452			if (md == NULL) {
1453				error = EBADRPC;
1454				goto out;
1455			}
1456			fromcp = mtod(md, caddr_t);
1457			rem = md->m_len;
1458		}
1459		if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
1460			error = EACCES;
1461			goto out;
1462		}
1463		cnp->cn_hash += (unsigned char)*fromcp;
1464		*tocp++ = *fromcp++;
1465		rem--;
1466	}
1467	*tocp = '\0';
1468	*mdp = md;
1469	*dposp = fromcp;
1470	len = nfsm_rndup(len)-len;
1471	if (len > 0) {
1472		if (rem >= len)
1473			*dposp += len;
1474		else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
1475			goto out;
1476	}
1477
1478	/*
1479	 * Extract and set starting directory.
1480	 */
1481	error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp,
1482	    nam, &rdonly, kerbflag, pubflag);
1483	if (error)
1484		goto out;
1485	if (dp->v_type != VDIR) {
1486		vrele(dp);
1487		error = ENOTDIR;
1488		goto out;
1489	}
1490
1491	if (rdonly)
1492		cnp->cn_flags |= RDONLY;
1493
1494	*retdirp = dp;
1495
1496	if (pubflag) {
1497		/*
1498		 * Oh joy. For WebNFS, handle those pesky '%' escapes,
1499		 * and the 'native path' indicator.
1500		 */
1501		cp = zalloc(namei_zone);
1502		fromcp = cnp->cn_pnbuf;
1503		tocp = cp;
1504		if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
1505			switch ((unsigned char)*fromcp) {
1506			case WEBNFS_NATIVE_CHAR:
1507				/*
1508				 * 'Native' path for us is the same
1509				 * as a path according to the NFS spec,
1510				 * just skip the escape char.
1511				 */
1512				fromcp++;
1513				break;
1514			/*
1515			 * More may be added in the future, range 0x80-0xff
1516			 */
1517			default:
1518				error = EIO;
1519				zfree(namei_zone, cp);
1520				goto out;
1521			}
1522		}
1523		/*
1524		 * Translate the '%' escapes, URL-style.
1525		 */
1526		while (*fromcp != '\0') {
1527			if (*fromcp == WEBNFS_ESC_CHAR) {
1528				if (fromcp[1] != '\0' && fromcp[2] != '\0') {
1529					fromcp++;
1530					*tocp++ = HEXSTRTOI(fromcp);
1531					fromcp += 2;
1532					continue;
1533				} else {
1534					error = ENOENT;
1535					zfree(namei_zone, cp);
1536					goto out;
1537				}
1538			} else
1539				*tocp++ = *fromcp++;
1540		}
1541		*tocp = '\0';
1542		zfree(namei_zone, cnp->cn_pnbuf);
1543		cnp->cn_pnbuf = cp;
1544	}
1545
1546	ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1;
1547	ndp->ni_segflg = UIO_SYSSPACE;
1548
1549	if (pubflag) {
1550		ndp->ni_rootdir = rootvnode;
1551		ndp->ni_loopcnt = 0;
1552		if (cnp->cn_pnbuf[0] == '/')
1553			dp = rootvnode;
1554	} else {
1555		cnp->cn_flags |= NOCROSSMOUNT;
1556	}
1557
1558	cnp->cn_proc = p;
1559	VREF(dp);
1560
1561    for (;;) {
1562	cnp->cn_nameptr = cnp->cn_pnbuf;
1563	ndp->ni_startdir = dp;
1564	/*
1565	 * And call lookup() to do the real work
1566	 */
1567	error = lookup(ndp);
1568	if (error)
1569		break;
1570	/*
1571	 * Check for encountering a symbolic link
1572	 */
1573	if ((cnp->cn_flags & ISSYMLINK) == 0) {
1574		nfsrv_object_create(ndp->ni_vp);
1575		if (cnp->cn_flags & (SAVENAME | SAVESTART)) {
1576			cnp->cn_flags |= HASBUF;
1577			return (0);
1578		}
1579		break;
1580	} else {
1581		if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
1582			VOP_UNLOCK(ndp->ni_dvp, 0, p);
1583		if (!pubflag) {
1584			vrele(ndp->ni_dvp);
1585			vput(ndp->ni_vp);
1586			ndp->ni_vp = NULL;
1587			error = EINVAL;
1588			break;
1589		}
1590
1591		if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
1592			error = ELOOP;
1593			break;
1594		}
1595		if (ndp->ni_pathlen > 1)
1596			cp = zalloc(namei_zone);
1597		else
1598			cp = cnp->cn_pnbuf;
1599		aiov.iov_base = cp;
1600		aiov.iov_len = MAXPATHLEN;
1601		auio.uio_iov = &aiov;
1602		auio.uio_iovcnt = 1;
1603		auio.uio_offset = 0;
1604		auio.uio_rw = UIO_READ;
1605		auio.uio_segflg = UIO_SYSSPACE;
1606		auio.uio_procp = (struct proc *)0;
1607		auio.uio_resid = MAXPATHLEN;
1608		error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
1609		if (error) {
1610		badlink:
1611			if (ndp->ni_pathlen > 1)
1612				zfree(namei_zone, cp);
1613			break;
1614		}
1615		linklen = MAXPATHLEN - auio.uio_resid;
1616		if (linklen == 0) {
1617			error = ENOENT;
1618			goto badlink;
1619		}
1620		if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
1621			error = ENAMETOOLONG;
1622			goto badlink;
1623		}
1624		if (ndp->ni_pathlen > 1) {
1625			bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
1626			zfree(namei_zone, cnp->cn_pnbuf);
1627			cnp->cn_pnbuf = cp;
1628		} else
1629			cnp->cn_pnbuf[linklen] = '\0';
1630		ndp->ni_pathlen += linklen;
1631		vput(ndp->ni_vp);
1632		dp = ndp->ni_dvp;
1633		/*
1634		 * Check if root directory should replace current directory.
1635		 */
1636		if (cnp->cn_pnbuf[0] == '/') {
1637			vrele(dp);
1638			dp = ndp->ni_rootdir;
1639			VREF(dp);
1640		}
1641	}
1642   }
1643out:
1644	zfree(namei_zone, cnp->cn_pnbuf);
1645	return (error);
1646}
1647
1648/*
1649 * A fiddled version of m_adj() that ensures null fill to a long
1650 * boundary and only trims off the back end
1651 */
1652void
1653nfsm_adj(mp, len, nul)
1654	struct mbuf *mp;
1655	register int len;
1656	int nul;
1657{
1658	register struct mbuf *m;
1659	register int count, i;
1660	register char *cp;
1661
1662	/*
1663	 * Trim from tail.  Scan the mbuf chain,
1664	 * calculating its length and finding the last mbuf.
1665	 * If the adjustment only affects this mbuf, then just
1666	 * adjust and return.  Otherwise, rescan and truncate
1667	 * after the remaining size.
1668	 */
1669	count = 0;
1670	m = mp;
1671	for (;;) {
1672		count += m->m_len;
1673		if (m->m_next == (struct mbuf *)0)
1674			break;
1675		m = m->m_next;
1676	}
1677	if (m->m_len > len) {
1678		m->m_len -= len;
1679		if (nul > 0) {
1680			cp = mtod(m, caddr_t)+m->m_len-nul;
1681			for (i = 0; i < nul; i++)
1682				*cp++ = '\0';
1683		}
1684		return;
1685	}
1686	count -= len;
1687	if (count < 0)
1688		count = 0;
1689	/*
1690	 * Correct length for chain is "count".
1691	 * Find the mbuf with last data, adjust its length,
1692	 * and toss data from remaining mbufs on chain.
1693	 */
1694	for (m = mp; m; m = m->m_next) {
1695		if (m->m_len >= count) {
1696			m->m_len = count;
1697			if (nul > 0) {
1698				cp = mtod(m, caddr_t)+m->m_len-nul;
1699				for (i = 0; i < nul; i++)
1700					*cp++ = '\0';
1701			}
1702			break;
1703		}
1704		count -= m->m_len;
1705	}
1706	for (m = m->m_next;m;m = m->m_next)
1707		m->m_len = 0;
1708}
1709
1710/*
1711 * Make these functions instead of macros, so that the kernel text size
1712 * doesn't get too big...
1713 */
1714void
1715nfsm_srvwcc(nfsd, before_ret, before_vap, after_ret, after_vap, mbp, bposp)
1716	struct nfsrv_descript *nfsd;
1717	int before_ret;
1718	register struct vattr *before_vap;
1719	int after_ret;
1720	struct vattr *after_vap;
1721	struct mbuf **mbp;
1722	char **bposp;
1723{
1724	register struct mbuf *mb = *mbp, *mb2;
1725	register char *bpos = *bposp;
1726	register u_long *tl;
1727
1728	if (before_ret) {
1729		nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1730		*tl = nfs_false;
1731	} else {
1732		nfsm_build(tl, u_long *, 7 * NFSX_UNSIGNED);
1733		*tl++ = nfs_true;
1734		txdr_hyper(&(before_vap->va_size), tl);
1735		tl += 2;
1736		txdr_nfsv3time(&(before_vap->va_mtime), tl);
1737		tl += 2;
1738		txdr_nfsv3time(&(before_vap->va_ctime), tl);
1739	}
1740	*bposp = bpos;
1741	*mbp = mb;
1742	nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
1743}
1744
1745void
1746nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp)
1747	struct nfsrv_descript *nfsd;
1748	int after_ret;
1749	struct vattr *after_vap;
1750	struct mbuf **mbp;
1751	char **bposp;
1752{
1753	register struct mbuf *mb = *mbp, *mb2;
1754	register char *bpos = *bposp;
1755	register u_long *tl;
1756	register struct nfs_fattr *fp;
1757
1758	if (after_ret) {
1759		nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1760		*tl = nfs_false;
1761	} else {
1762		nfsm_build(tl, u_long *, NFSX_UNSIGNED + NFSX_V3FATTR);
1763		*tl++ = nfs_true;
1764		fp = (struct nfs_fattr *)tl;
1765		nfsm_srvfattr(nfsd, after_vap, fp);
1766	}
1767	*mbp = mb;
1768	*bposp = bpos;
1769}
1770
1771void
1772nfsm_srvfattr(nfsd, vap, fp)
1773	register struct nfsrv_descript *nfsd;
1774	register struct vattr *vap;
1775	register struct nfs_fattr *fp;
1776{
1777
1778	fp->fa_nlink = txdr_unsigned(vap->va_nlink);
1779	fp->fa_uid = txdr_unsigned(vap->va_uid);
1780	fp->fa_gid = txdr_unsigned(vap->va_gid);
1781	if (nfsd->nd_flag & ND_NFSV3) {
1782		fp->fa_type = vtonfsv3_type(vap->va_type);
1783		fp->fa_mode = vtonfsv3_mode(vap->va_mode);
1784		txdr_hyper(&vap->va_size, &fp->fa3_size);
1785		txdr_hyper(&vap->va_bytes, &fp->fa3_used);
1786		fp->fa3_rdev.specdata1 = txdr_unsigned(major(vap->va_rdev));
1787		fp->fa3_rdev.specdata2 = txdr_unsigned(minor(vap->va_rdev));
1788		fp->fa3_fsid.nfsuquad[0] = 0;
1789		fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
1790		fp->fa3_fileid.nfsuquad[0] = 0;
1791		fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid);
1792		txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
1793		txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
1794		txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
1795	} else {
1796		fp->fa_type = vtonfsv2_type(vap->va_type);
1797		fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1798		fp->fa2_size = txdr_unsigned(vap->va_size);
1799		fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
1800		if (vap->va_type == VFIFO)
1801			fp->fa2_rdev = 0xffffffff;
1802		else
1803			fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
1804		fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
1805		fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
1806		fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
1807		txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
1808		txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
1809		txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
1810	}
1811}
1812
1813/*
1814 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
1815 * 	- look up fsid in mount list (if not found ret error)
1816 *	- get vp and export rights by calling VFS_FHTOVP()
1817 *	- if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
1818 *	- if not lockflag unlock it with VOP_UNLOCK()
1819 */
1820int
1821nfsrv_fhtovp(fhp, lockflag, vpp, cred, slp, nam, rdonlyp, kerbflag, pubflag)
1822	fhandle_t *fhp;
1823	int lockflag;
1824	struct vnode **vpp;
1825	struct ucred *cred;
1826	struct nfssvc_sock *slp;
1827	struct sockaddr *nam;
1828	int *rdonlyp;
1829	int kerbflag;
1830	int pubflag;
1831{
1832	struct proc *p = curproc; /* XXX */
1833	register struct mount *mp;
1834	register int i;
1835	struct ucred *credanon;
1836	int error, exflags;
1837
1838	*vpp = (struct vnode *)0;
1839
1840	if (nfs_ispublicfh(fhp)) {
1841		if (!pubflag || !nfs_pub.np_valid)
1842			return (ESTALE);
1843		fhp = &nfs_pub.np_handle;
1844	}
1845
1846	mp = vfs_getvfs(&fhp->fh_fsid);
1847	if (!mp)
1848		return (ESTALE);
1849	error = VFS_FHTOVP(mp, &fhp->fh_fid, nam, vpp, &exflags, &credanon);
1850	if (error)
1851		return (error);
1852	/*
1853	 * Check/setup credentials.
1854	 */
1855	if (exflags & MNT_EXKERB) {
1856		if (!kerbflag) {
1857			vput(*vpp);
1858			return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1859		}
1860	} else if (kerbflag) {
1861		vput(*vpp);
1862		return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1863	} else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1864		cred->cr_uid = credanon->cr_uid;
1865		for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++)
1866			cred->cr_groups[i] = credanon->cr_groups[i];
1867		cred->cr_ngroups = i;
1868	}
1869	if (exflags & MNT_EXRDONLY)
1870		*rdonlyp = 1;
1871	else
1872		*rdonlyp = 0;
1873
1874	nfsrv_object_create(*vpp);
1875
1876	if (!lockflag)
1877		VOP_UNLOCK(*vpp, 0, p);
1878	return (0);
1879}
1880
1881
1882/*
1883 * WebNFS: check if a filehandle is a public filehandle. For v3, this
1884 * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has
1885 * transformed this to all zeroes in both cases, so check for it.
1886 */
1887int
1888nfs_ispublicfh(fhp)
1889	fhandle_t *fhp;
1890{
1891	char *cp = (char *)fhp;
1892	int i;
1893
1894	for (i = 0; i < NFSX_V3FH; i++)
1895		if (*cp++ != 0)
1896			return (FALSE);
1897	return (TRUE);
1898}
1899
1900#endif /* NFS_NOSERVER */
1901/*
1902 * This function compares two net addresses by family and returns TRUE
1903 * if they are the same host.
1904 * If there is any doubt, return FALSE.
1905 * The AF_INET family is handled as a special case so that address mbufs
1906 * don't need to be saved to store "struct in_addr", which is only 4 bytes.
1907 */
1908int
1909netaddr_match(family, haddr, nam)
1910	int family;
1911	union nethostaddr *haddr;
1912	struct sockaddr *nam;
1913{
1914	register struct sockaddr_in *inetaddr;
1915
1916	switch (family) {
1917	case AF_INET:
1918		inetaddr = (struct sockaddr_in *)nam;
1919		if (inetaddr->sin_family == AF_INET &&
1920		    inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
1921			return (1);
1922		break;
1923#ifdef ISO
1924	case AF_ISO:
1925	    {
1926		register struct sockaddr_iso *isoaddr1, *isoaddr2;
1927
1928		isoaddr1 = (struct sockaddr_iso *)nam;
1929		isoaddr2 = (struct sockaddr_iso *)haddr->had_nam;
1930		if (isoaddr1->siso_family == AF_ISO &&
1931		    isoaddr1->siso_nlen > 0 &&
1932		    isoaddr1->siso_nlen == isoaddr2->siso_nlen &&
1933		    SAME_ISOADDR(isoaddr1, isoaddr2))
1934			return (1);
1935		break;
1936	    }
1937#endif	/* ISO */
1938	default:
1939		break;
1940	};
1941	return (0);
1942}
1943
1944static nfsuint64 nfs_nullcookie = { 0, 0 };
1945/*
1946 * This function finds the directory cookie that corresponds to the
1947 * logical byte offset given.
1948 */
1949nfsuint64 *
1950nfs_getcookie(np, off, add)
1951	register struct nfsnode *np;
1952	off_t off;
1953	int add;
1954{
1955	register struct nfsdmap *dp, *dp2;
1956	register int pos;
1957
1958	pos = off / NFS_DIRBLKSIZ;
1959	if (pos == 0) {
1960#ifdef DIAGNOSTIC
1961		if (add)
1962			panic("nfs getcookie add at 0");
1963#endif
1964		return (&nfs_nullcookie);
1965	}
1966	pos--;
1967	dp = np->n_cookies.lh_first;
1968	if (!dp) {
1969		if (add) {
1970			MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap),
1971				M_NFSDIROFF, M_WAITOK);
1972			dp->ndm_eocookie = 0;
1973			LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
1974		} else
1975			return ((nfsuint64 *)0);
1976	}
1977	while (pos >= NFSNUMCOOKIES) {
1978		pos -= NFSNUMCOOKIES;
1979		if (dp->ndm_list.le_next) {
1980			if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
1981				pos >= dp->ndm_eocookie)
1982				return ((nfsuint64 *)0);
1983			dp = dp->ndm_list.le_next;
1984		} else if (add) {
1985			MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap),
1986				M_NFSDIROFF, M_WAITOK);
1987			dp2->ndm_eocookie = 0;
1988			LIST_INSERT_AFTER(dp, dp2, ndm_list);
1989			dp = dp2;
1990		} else
1991			return ((nfsuint64 *)0);
1992	}
1993	if (pos >= dp->ndm_eocookie) {
1994		if (add)
1995			dp->ndm_eocookie = pos + 1;
1996		else
1997			return ((nfsuint64 *)0);
1998	}
1999	return (&dp->ndm_cookies[pos]);
2000}
2001
2002/*
2003 * Invalidate cached directory information, except for the actual directory
2004 * blocks (which are invalidated separately).
2005 * Done mainly to avoid the use of stale offset cookies.
2006 */
2007void
2008nfs_invaldir(vp)
2009	register struct vnode *vp;
2010{
2011	register struct nfsnode *np = VTONFS(vp);
2012
2013#ifdef DIAGNOSTIC
2014	if (vp->v_type != VDIR)
2015		panic("nfs: invaldir not dir");
2016#endif
2017	np->n_direofoffset = 0;
2018	np->n_cookieverf.nfsuquad[0] = 0;
2019	np->n_cookieverf.nfsuquad[1] = 0;
2020	if (np->n_cookies.lh_first)
2021		np->n_cookies.lh_first->ndm_eocookie = 0;
2022}
2023
2024/*
2025 * The write verifier has changed (probably due to a server reboot), so all
2026 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
2027 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
2028 * flag. Once done the new write verifier can be set for the mount point.
2029 */
2030void
2031nfs_clearcommit(mp)
2032	struct mount *mp;
2033{
2034	register struct vnode *vp, *nvp;
2035	register struct buf *bp, *nbp;
2036	int s;
2037
2038	s = splbio();
2039loop:
2040	for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) {
2041		if (vp->v_mount != mp)	/* Paranoia */
2042			goto loop;
2043		nvp = vp->v_mntvnodes.le_next;
2044		for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = nbp) {
2045			nbp = bp->b_vnbufs.le_next;
2046			if ((bp->b_flags & (B_BUSY | B_DELWRI | B_NEEDCOMMIT))
2047				== (B_DELWRI | B_NEEDCOMMIT))
2048				bp->b_flags &= ~B_NEEDCOMMIT;
2049		}
2050	}
2051	splx(s);
2052}
2053
2054#ifndef NFS_NOSERVER
2055/*
2056 * Map errnos to NFS error numbers. For Version 3 also filter out error
2057 * numbers not specified for the associated procedure.
2058 */
2059int
2060nfsrv_errmap(nd, err)
2061	struct nfsrv_descript *nd;
2062	register int err;
2063{
2064	register short *defaulterrp, *errp;
2065
2066	if (nd->nd_flag & ND_NFSV3) {
2067	    if (nd->nd_procnum <= NFSPROC_COMMIT) {
2068		errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
2069		while (*++errp) {
2070			if (*errp == err)
2071				return (err);
2072			else if (*errp > err)
2073				break;
2074		}
2075		return ((int)*defaulterrp);
2076	    } else
2077		return (err & 0xffff);
2078	}
2079	if (err <= ELAST)
2080		return ((int)nfsrv_v2errmap[err - 1]);
2081	return (NFSERR_IO);
2082}
2083
2084int
2085nfsrv_object_create(struct vnode *vp) {
2086
2087	if ((vp == NULL) || (vp->v_type != VREG))
2088		return 1;
2089	return vfs_object_create(vp, curproc, curproc?curproc->p_ucred:NULL, 1);
2090}
2091#endif /* NFS_NOSERVER */
2092
2093