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