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