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