xref: /original-bsd/sys/nfs/nfs_subs.c (revision 860e07fc)
1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
3  * 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  * %sccs.include.redist.c%
9  *
10  *	@(#)nfs_subs.c	7.62 (Berkeley) 07/25/92
11  */
12 
13 /*
14  * These functions support the macros and help fiddle mbuf chains for
15  * the nfs op functions. They do things like create the rpc header and
16  * copy data between mbuf chains and uio lists.
17  */
18 #include <sys/param.h>
19 #include <sys/proc.h>
20 #include <sys/systm.h>
21 #include <sys/kernel.h>
22 #include <sys/mount.h>
23 #include <sys/vnode.h>
24 #include <sys/namei.h>
25 #include <sys/mbuf.h>
26 #include <sys/socket.h>
27 #include <sys/stat.h>
28 
29 #include <nfs/rpcv2.h>
30 #include <nfs/nfsv2.h>
31 #include <nfs/nfsnode.h>
32 #include <nfs/nfs.h>
33 #include <nfs/xdr_subs.h>
34 #include <nfs/nfsm_subs.h>
35 #include <nfs/nfsmount.h>
36 #include <nfs/nqnfs.h>
37 #include <nfs/nfsrtt.h>
38 
39 #include <miscfs/specfs/specdev.h>
40 
41 #include <netinet/in.h>
42 #ifdef ISO
43 #include <netiso/iso.h>
44 #endif
45 
46 #define TRUE	1
47 #define	FALSE	0
48 
49 /*
50  * Data items converted to xdr at startup, since they are constant
51  * This is kinda hokey, but may save a little time doing byte swaps
52  */
53 u_long nfs_procids[NFS_NPROCS];
54 u_long nfs_xdrneg1;
55 u_long rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr,
56 	rpc_mismatch, rpc_auth_unix, rpc_msgaccepted, rpc_rejectedcred,
57 	rpc_auth_kerb;
58 u_long nfs_vers, nfs_prog, nfs_true, nfs_false;
59 
60 /* And other global data */
61 static u_long nfs_xid = 0;
62 enum vtype ntov_type[7] = { VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON };
63 extern struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON];
64 extern struct nfsreq nfsreqh;
65 extern int nqnfs_piggy[NFS_NPROCS];
66 extern struct nfsrtt nfsrtt;
67 extern time_t nqnfsstarttime;
68 extern u_long nqnfs_prog, nqnfs_vers;
69 extern int nqsrv_clockskew;
70 extern int nqsrv_writeslack;
71 extern int nqsrv_maxlease;
72 
73 /*
74  * Create the header for an rpc request packet
75  * The hsiz is the size of the rest of the nfs request header.
76  * (just used to decide if a cluster is a good idea)
77  */
78 struct mbuf *
79 nfsm_reqh(vp, procid, hsiz, bposp)
80 	struct vnode *vp;
81 	u_long procid;
82 	int hsiz;
83 	caddr_t *bposp;
84 {
85 	register struct mbuf *mb;
86 	register u_long *tl;
87 	register caddr_t bpos;
88 	struct mbuf *mb2;
89 	struct nfsmount *nmp;
90 	int nqflag;
91 
92 	MGET(mb, M_WAIT, MT_DATA);
93 	if (hsiz >= MINCLSIZE)
94 		MCLGET(mb, M_WAIT);
95 	mb->m_len = 0;
96 	bpos = mtod(mb, caddr_t);
97 
98 	/*
99 	 * For NQNFS, add lease request.
100 	 */
101 	if (vp) {
102 		nmp = VFSTONFS(vp->v_mount);
103 		if (nmp->nm_flag & NFSMNT_NQNFS) {
104 			nqflag = NQNFS_NEEDLEASE(vp, procid);
105 			if (nqflag) {
106 				nfsm_build(tl, u_long *, 2*NFSX_UNSIGNED);
107 				*tl++ = txdr_unsigned(nqflag);
108 				*tl = txdr_unsigned(nmp->nm_leaseterm);
109 			} else {
110 				nfsm_build(tl, u_long *, NFSX_UNSIGNED);
111 				*tl = 0;
112 			}
113 		}
114 	}
115 	/* Finally, return values */
116 	*bposp = bpos;
117 	return (mb);
118 }
119 
120 /*
121  * Build the RPC header and fill in the authorization info.
122  * The authorization string argument is only used when the credentials
123  * come from outside of the kernel.
124  * Returns the head of the mbuf list.
125  */
126 struct mbuf *
127 nfsm_rpchead(cr, nqnfs, procid, auth_type, auth_len, auth_str, mrest,
128 	mrest_len, mbp, xidp)
129 	register struct ucred *cr;
130 	int nqnfs;
131 	int procid;
132 	int auth_type;
133 	int auth_len;
134 	char *auth_str;
135 	struct mbuf *mrest;
136 	int mrest_len;
137 	struct mbuf **mbp;
138 	u_long *xidp;
139 {
140 	register struct mbuf *mb;
141 	register u_long *tl;
142 	register caddr_t bpos;
143 	register int i;
144 	struct mbuf *mreq, *mb2;
145 	int siz, grpsiz, authsiz;
146 
147 	authsiz = nfsm_rndup(auth_len);
148 	if (auth_type == RPCAUTH_NQNFS)
149 		authsiz += 2 * NFSX_UNSIGNED;
150 	MGETHDR(mb, M_WAIT, MT_DATA);
151 	if ((authsiz + 10*NFSX_UNSIGNED) >= MINCLSIZE) {
152 		MCLGET(mb, M_WAIT);
153 	} else if ((authsiz + 10*NFSX_UNSIGNED) < MHLEN) {
154 		MH_ALIGN(mb, authsiz + 10*NFSX_UNSIGNED);
155 	} else {
156 		MH_ALIGN(mb, 8*NFSX_UNSIGNED);
157 	}
158 	mb->m_len = 0;
159 	mreq = mb;
160 	bpos = mtod(mb, caddr_t);
161 
162 	/*
163 	 * First the RPC header.
164 	 */
165 	nfsm_build(tl, u_long *, 8*NFSX_UNSIGNED);
166 	if (++nfs_xid == 0)
167 		nfs_xid++;
168 	*tl++ = *xidp = txdr_unsigned(nfs_xid);
169 	*tl++ = rpc_call;
170 	*tl++ = rpc_vers;
171 	if (nqnfs) {
172 		*tl++ = txdr_unsigned(NQNFS_PROG);
173 		*tl++ = txdr_unsigned(NQNFS_VER1);
174 	} else {
175 		*tl++ = txdr_unsigned(NFS_PROG);
176 		*tl++ = txdr_unsigned(NFS_VER2);
177 	}
178 	*tl++ = txdr_unsigned(procid);
179 
180 	/*
181 	 * And then the authorization cred.
182 	 */
183 	*tl++ = txdr_unsigned(auth_type);
184 	*tl = txdr_unsigned(authsiz);
185 	switch (auth_type) {
186 	case RPCAUTH_UNIX:
187 		nfsm_build(tl, u_long *, auth_len);
188 		*tl++ = 0;		/* stamp ?? */
189 		*tl++ = 0;		/* NULL hostname */
190 		*tl++ = txdr_unsigned(cr->cr_uid);
191 		*tl++ = txdr_unsigned(cr->cr_groups[0]);
192 		grpsiz = (auth_len >> 2) - 5;
193 		*tl++ = txdr_unsigned(grpsiz);
194 		for (i = 1; i <= grpsiz; i++)
195 			*tl++ = txdr_unsigned(cr->cr_groups[i]);
196 		break;
197 	case RPCAUTH_NQNFS:
198 		nfsm_build(tl, u_long *, 2*NFSX_UNSIGNED);
199 		*tl++ = txdr_unsigned(cr->cr_uid);
200 		*tl = txdr_unsigned(auth_len);
201 		siz = auth_len;
202 		while (siz > 0) {
203 			if (M_TRAILINGSPACE(mb) == 0) {
204 				MGET(mb2, M_WAIT, MT_DATA);
205 				if (siz >= MINCLSIZE)
206 					MCLGET(mb2, M_WAIT);
207 				mb->m_next = mb2;
208 				mb = mb2;
209 				mb->m_len = 0;
210 				bpos = mtod(mb, caddr_t);
211 			}
212 			i = min(siz, M_TRAILINGSPACE(mb));
213 			bcopy(auth_str, bpos, i);
214 			mb->m_len += i;
215 			auth_str += i;
216 			bpos += i;
217 			siz -= i;
218 		}
219 		if ((siz = nfsm_rndup(auth_len) - auth_len) > 0) {
220 			for (i = 0; i < siz; i++)
221 				*bpos++ = '\0';
222 			mb->m_len += siz;
223 		}
224 		break;
225 	};
226 	nfsm_build(tl, u_long *, 2*NFSX_UNSIGNED);
227 	*tl++ = txdr_unsigned(RPCAUTH_NULL);
228 	*tl = 0;
229 	mb->m_next = mrest;
230 	mreq->m_pkthdr.len = authsiz + 10*NFSX_UNSIGNED + mrest_len;
231 	mreq->m_pkthdr.rcvif = (struct ifnet *)0;
232 	*mbp = mb;
233 	return (mreq);
234 }
235 
236 /*
237  * copies mbuf chain to the uio scatter/gather list
238  */
239 nfsm_mbuftouio(mrep, uiop, siz, dpos)
240 	struct mbuf **mrep;
241 	register struct uio *uiop;
242 	int siz;
243 	caddr_t *dpos;
244 {
245 	register char *mbufcp, *uiocp;
246 	register int xfer, left, len;
247 	register struct mbuf *mp;
248 	long uiosiz, rem;
249 	int error = 0;
250 
251 	mp = *mrep;
252 	mbufcp = *dpos;
253 	len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
254 	rem = nfsm_rndup(siz)-siz;
255 	while (siz > 0) {
256 		if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
257 			return (EFBIG);
258 		left = uiop->uio_iov->iov_len;
259 		uiocp = uiop->uio_iov->iov_base;
260 		if (left > siz)
261 			left = siz;
262 		uiosiz = left;
263 		while (left > 0) {
264 			while (len == 0) {
265 				mp = mp->m_next;
266 				if (mp == NULL)
267 					return (EBADRPC);
268 				mbufcp = mtod(mp, caddr_t);
269 				len = mp->m_len;
270 			}
271 			xfer = (left > len) ? len : left;
272 #ifdef notdef
273 			/* Not Yet.. */
274 			if (uiop->uio_iov->iov_op != NULL)
275 				(*(uiop->uio_iov->iov_op))
276 				(mbufcp, uiocp, xfer);
277 			else
278 #endif
279 			if (uiop->uio_segflg == UIO_SYSSPACE)
280 				bcopy(mbufcp, uiocp, xfer);
281 			else
282 				copyout(mbufcp, uiocp, xfer);
283 			left -= xfer;
284 			len -= xfer;
285 			mbufcp += xfer;
286 			uiocp += xfer;
287 			uiop->uio_offset += xfer;
288 			uiop->uio_resid -= xfer;
289 		}
290 		if (uiop->uio_iov->iov_len <= siz) {
291 			uiop->uio_iovcnt--;
292 			uiop->uio_iov++;
293 		} else {
294 			uiop->uio_iov->iov_base += uiosiz;
295 			uiop->uio_iov->iov_len -= uiosiz;
296 		}
297 		siz -= uiosiz;
298 	}
299 	*dpos = mbufcp;
300 	*mrep = mp;
301 	if (rem > 0) {
302 		if (len < rem)
303 			error = nfs_adv(mrep, dpos, rem, len);
304 		else
305 			*dpos += rem;
306 	}
307 	return (error);
308 }
309 
310 /*
311  * copies a uio scatter/gather list to an mbuf chain...
312  */
313 nfsm_uiotombuf(uiop, mq, siz, bpos)
314 	register struct uio *uiop;
315 	struct mbuf **mq;
316 	int siz;
317 	caddr_t *bpos;
318 {
319 	register char *uiocp;
320 	register struct mbuf *mp, *mp2;
321 	register int xfer, left, mlen;
322 	int uiosiz, clflg, rem;
323 	char *cp;
324 
325 	if (siz > MLEN)		/* or should it >= MCLBYTES ?? */
326 		clflg = 1;
327 	else
328 		clflg = 0;
329 	rem = nfsm_rndup(siz)-siz;
330 	mp = mp2 = *mq;
331 	while (siz > 0) {
332 		if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
333 			return (EINVAL);
334 		left = uiop->uio_iov->iov_len;
335 		uiocp = uiop->uio_iov->iov_base;
336 		if (left > siz)
337 			left = siz;
338 		uiosiz = left;
339 		while (left > 0) {
340 			mlen = M_TRAILINGSPACE(mp);
341 			if (mlen == 0) {
342 				MGET(mp, M_WAIT, MT_DATA);
343 				if (clflg)
344 					MCLGET(mp, M_WAIT);
345 				mp->m_len = 0;
346 				mp2->m_next = mp;
347 				mp2 = mp;
348 				mlen = M_TRAILINGSPACE(mp);
349 			}
350 			xfer = (left > mlen) ? mlen : left;
351 #ifdef notdef
352 			/* Not Yet.. */
353 			if (uiop->uio_iov->iov_op != NULL)
354 				(*(uiop->uio_iov->iov_op))
355 				(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
356 			else
357 #endif
358 			if (uiop->uio_segflg == UIO_SYSSPACE)
359 				bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
360 			else
361 				copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
362 			mp->m_len += xfer;
363 			left -= xfer;
364 			uiocp += xfer;
365 			uiop->uio_offset += xfer;
366 			uiop->uio_resid -= xfer;
367 		}
368 		if (uiop->uio_iov->iov_len <= siz) {
369 			uiop->uio_iovcnt--;
370 			uiop->uio_iov++;
371 		} else {
372 			uiop->uio_iov->iov_base += uiosiz;
373 			uiop->uio_iov->iov_len -= uiosiz;
374 		}
375 		siz -= uiosiz;
376 	}
377 	if (rem > 0) {
378 		if (rem > M_TRAILINGSPACE(mp)) {
379 			MGET(mp, M_WAIT, MT_DATA);
380 			mp->m_len = 0;
381 			mp2->m_next = mp;
382 		}
383 		cp = mtod(mp, caddr_t)+mp->m_len;
384 		for (left = 0; left < rem; left++)
385 			*cp++ = '\0';
386 		mp->m_len += rem;
387 		*bpos = cp;
388 	} else
389 		*bpos = mtod(mp, caddr_t)+mp->m_len;
390 	*mq = mp;
391 	return (0);
392 }
393 
394 /*
395  * Help break down an mbuf chain by setting the first siz bytes contiguous
396  * pointed to by returned val.
397  * If Updateflg == True we can overwrite the first part of the mbuf data
398  * (in this case it can never sleep, so it can be called from interrupt level)
399  * it may however block when Updateflg == False
400  * This is used by the macros nfsm_dissect and nfsm_dissecton for tough
401  * cases. (The macros use the vars. dpos and dpos2)
402  */
403 nfsm_disct(mdp, dposp, siz, left, updateflg, cp2)
404 	struct mbuf **mdp;
405 	caddr_t *dposp;
406 	int siz;
407 	int left;
408 	int updateflg;
409 	caddr_t *cp2;
410 {
411 	register struct mbuf *mp, *mp2;
412 	register int siz2, xfer;
413 	register caddr_t p;
414 
415 	mp = *mdp;
416 	while (left == 0) {
417 		*mdp = mp = mp->m_next;
418 		if (mp == NULL)
419 			return (EBADRPC);
420 		left = mp->m_len;
421 		*dposp = mtod(mp, caddr_t);
422 	}
423 	if (left >= siz) {
424 		*cp2 = *dposp;
425 		*dposp += siz;
426 	} else if (mp->m_next == NULL) {
427 		return (EBADRPC);
428 	} else if (siz > MHLEN) {
429 		panic("nfs S too big");
430 	} else {
431 		/* Iff update, you can overwrite, else must alloc new mbuf */
432 		if (updateflg) {
433 			NFSMINOFF(mp);
434 		} else {
435 			MGET(mp2, M_WAIT, MT_DATA);
436 			mp2->m_next = mp->m_next;
437 			mp->m_next = mp2;
438 			mp->m_len -= left;
439 			mp = mp2;
440 		}
441 		*cp2 = p = mtod(mp, caddr_t);
442 		bcopy(*dposp, p, left);		/* Copy what was left */
443 		siz2 = siz-left;
444 		p += left;
445 		mp2 = mp->m_next;
446 		/* Loop around copying up the siz2 bytes */
447 		while (siz2 > 0) {
448 			if (mp2 == NULL)
449 				return (EBADRPC);
450 			xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
451 			if (xfer > 0) {
452 				bcopy(mtod(mp2, caddr_t), p, xfer);
453 				NFSMADV(mp2, xfer);
454 				mp2->m_len -= xfer;
455 				p += xfer;
456 				siz2 -= xfer;
457 			}
458 			if (siz2 > 0)
459 				mp2 = mp2->m_next;
460 		}
461 		mp->m_len = siz;
462 		*mdp = mp2;
463 		*dposp = mtod(mp2, caddr_t);
464 	}
465 	return (0);
466 }
467 
468 /*
469  * Advance the position in the mbuf chain.
470  */
471 nfs_adv(mdp, dposp, offs, left)
472 	struct mbuf **mdp;
473 	caddr_t *dposp;
474 	int offs;
475 	int left;
476 {
477 	register struct mbuf *m;
478 	register int s;
479 
480 	m = *mdp;
481 	s = left;
482 	while (s < offs) {
483 		offs -= s;
484 		m = m->m_next;
485 		if (m == NULL)
486 			return (EBADRPC);
487 		s = m->m_len;
488 	}
489 	*mdp = m;
490 	*dposp = mtod(m, caddr_t)+offs;
491 	return (0);
492 }
493 
494 /*
495  * Copy a string into mbufs for the hard cases...
496  */
497 nfsm_strtmbuf(mb, bpos, cp, siz)
498 	struct mbuf **mb;
499 	char **bpos;
500 	char *cp;
501 	long siz;
502 {
503 	register struct mbuf *m1, *m2;
504 	long left, xfer, len, tlen;
505 	u_long *tl;
506 	int putsize;
507 
508 	putsize = 1;
509 	m2 = *mb;
510 	left = M_TRAILINGSPACE(m2);
511 	if (left > 0) {
512 		tl = ((u_long *)(*bpos));
513 		*tl++ = txdr_unsigned(siz);
514 		putsize = 0;
515 		left -= NFSX_UNSIGNED;
516 		m2->m_len += NFSX_UNSIGNED;
517 		if (left > 0) {
518 			bcopy(cp, (caddr_t) tl, left);
519 			siz -= left;
520 			cp += left;
521 			m2->m_len += left;
522 			left = 0;
523 		}
524 	}
525 	/* Loop around adding mbufs */
526 	while (siz > 0) {
527 		MGET(m1, M_WAIT, MT_DATA);
528 		if (siz > MLEN)
529 			MCLGET(m1, M_WAIT);
530 		m1->m_len = NFSMSIZ(m1);
531 		m2->m_next = m1;
532 		m2 = m1;
533 		tl = mtod(m1, u_long *);
534 		tlen = 0;
535 		if (putsize) {
536 			*tl++ = txdr_unsigned(siz);
537 			m1->m_len -= NFSX_UNSIGNED;
538 			tlen = NFSX_UNSIGNED;
539 			putsize = 0;
540 		}
541 		if (siz < m1->m_len) {
542 			len = nfsm_rndup(siz);
543 			xfer = siz;
544 			if (xfer < len)
545 				*(tl+(xfer>>2)) = 0;
546 		} else {
547 			xfer = len = m1->m_len;
548 		}
549 		bcopy(cp, (caddr_t) tl, xfer);
550 		m1->m_len = len+tlen;
551 		siz -= xfer;
552 		cp += xfer;
553 	}
554 	*mb = m1;
555 	*bpos = mtod(m1, caddr_t)+m1->m_len;
556 	return (0);
557 }
558 
559 /*
560  * Called once to initialize data structures...
561  */
562 nfs_init()
563 {
564 	register int i;
565 	union nqsrvthead *lhp;
566 
567 	nfsrtt.pos = 0;
568 	rpc_vers = txdr_unsigned(RPC_VER2);
569 	rpc_call = txdr_unsigned(RPC_CALL);
570 	rpc_reply = txdr_unsigned(RPC_REPLY);
571 	rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
572 	rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
573 	rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
574 	rpc_autherr = txdr_unsigned(RPC_AUTHERR);
575 	rpc_rejectedcred = txdr_unsigned(AUTH_REJECTCRED);
576 	rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
577 	rpc_auth_kerb = txdr_unsigned(RPCAUTH_NQNFS);
578 	nfs_vers = txdr_unsigned(NFS_VER2);
579 	nfs_prog = txdr_unsigned(NFS_PROG);
580 	nfs_true = txdr_unsigned(TRUE);
581 	nfs_false = txdr_unsigned(FALSE);
582 	/* Loop thru nfs procids */
583 	for (i = 0; i < NFS_NPROCS; i++)
584 		nfs_procids[i] = txdr_unsigned(i);
585 	/* Ensure async daemons disabled */
586 	for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
587 		nfs_iodwant[i] = (struct proc *)0;
588 	nfs_xdrneg1 = txdr_unsigned(-1);
589 	nfs_nhinit();			/* Init the nfsnode table */
590 	nfsrv_init(0);			/* Init server data structures */
591 	nfsrv_initcache();		/* Init the server request cache */
592 
593 	/*
594 	 * Initialize the nqnfs server stuff.
595 	 */
596 	if (nqnfsstarttime == 0) {
597 		nqnfsstarttime = boottime.tv_sec + nqsrv_maxlease
598 			+ nqsrv_clockskew + nqsrv_writeslack;
599 		NQLOADNOVRAM(nqnfsstarttime);
600 		nqnfs_prog = txdr_unsigned(NQNFS_PROG);
601 		nqnfs_vers = txdr_unsigned(NQNFS_VER1);
602 		nqthead.th_head[0] = &nqthead;
603 		nqthead.th_head[1] = &nqthead;
604 		nqfhead = hashinit(NQLCHSZ, M_NQLEASE, &nqfheadhash);
605 	}
606 
607 	/*
608 	 * Initialize reply list and start timer
609 	 */
610 	nfsreqh.r_prev = nfsreqh.r_next = &nfsreqh;
611 	nfs_timer();
612 }
613 
614 /*
615  * Attribute cache routines.
616  * nfs_loadattrcache() - loads or updates the cache contents from attributes
617  *	that are on the mbuf list
618  * nfs_getattrcache() - returns valid attributes if found in cache, returns
619  *	error otherwise
620  */
621 
622 /*
623  * Load the attribute cache (that lives in the nfsnode entry) with
624  * the values on the mbuf list and
625  * Iff vap not NULL
626  *    copy the attributes to *vaper
627  */
628 nfs_loadattrcache(vpp, mdp, dposp, vaper)
629 	struct vnode **vpp;
630 	struct mbuf **mdp;
631 	caddr_t *dposp;
632 	struct vattr *vaper;
633 {
634 	register struct vnode *vp = *vpp;
635 	register struct vattr *vap;
636 	register struct nfsv2_fattr *fp;
637 	extern int (**spec_nfsv2nodeop_p)();
638 	register struct nfsnode *np, *nq, **nhpp;
639 	register long t1;
640 	caddr_t dpos, cp2;
641 	int error = 0;
642 	struct mbuf *md;
643 	enum vtype vtyp;
644 	u_short vmode;
645 	long rdev;
646 	struct timeval mtime;
647 	struct vnode *nvp;
648 
649 	md = *mdp;
650 	dpos = *dposp;
651 	t1 = (mtod(md, caddr_t) + md->m_len) - dpos;
652 	if (error = nfsm_disct(&md, &dpos, NFSX_FATTR, t1, TRUE, &cp2))
653 		return (error);
654 	fp = (struct nfsv2_fattr *)cp2;
655 	vtyp = nfstov_type(fp->fa_type);
656 	vmode = fxdr_unsigned(u_short, fp->fa_mode);
657 	if (vtyp == VNON || vtyp == VREG)
658 		vtyp = IFTOVT(vmode);
659 	rdev = fxdr_unsigned(long, fp->fa_rdev);
660 	fxdr_time(&fp->fa_mtime, &mtime);
661 	/*
662 	 * If v_type == VNON it is a new node, so fill in the v_type,
663 	 * n_mtime fields. Check to see if it represents a special
664 	 * device, and if so, check for a possible alias. Once the
665 	 * correct vnode has been obtained, fill in the rest of the
666 	 * information.
667 	 */
668 	np = VTONFS(vp);
669 	if (vp->v_type == VNON) {
670 		if (vtyp == VCHR && rdev == 0xffffffff)
671 			vp->v_type = vtyp = VFIFO;
672 		else
673 			vp->v_type = vtyp;
674 		if (vp->v_type == VFIFO) {
675 #ifdef FIFO
676 			extern int (**fifo_nfsv2nodeop_p)();
677 			vp->v_op = fifo_nfsv2nodeop_p;
678 #else
679 			return (EOPNOTSUPP);
680 #endif /* FIFO */
681 		}
682 		if (vp->v_type == VCHR || vp->v_type == VBLK) {
683 			vp->v_op = spec_nfsv2nodeop_p;
684 			if (nvp = checkalias(vp, (dev_t)rdev, vp->v_mount)) {
685 				/*
686 				 * Discard unneeded vnode, but save its nfsnode.
687 				 */
688 				if (nq = np->n_forw)
689 					nq->n_back = np->n_back;
690 				*np->n_back = nq;
691 				nvp->v_data = vp->v_data;
692 				vp->v_data = NULL;
693 				vp->v_op = spec_vnodeop_p;
694 				vrele(vp);
695 				vgone(vp);
696 				/*
697 				 * Reinitialize aliased node.
698 				 */
699 				np->n_vnode = nvp;
700 				nhpp = (struct nfsnode **)nfs_hash(&np->n_fh);
701 				if (nq = *nhpp)
702 					nq->n_back = &np->n_forw;
703 				np->n_forw = nq;
704 				np->n_back = nhpp;
705 				*nhpp = np;
706 				*vpp = vp = nvp;
707 			}
708 		}
709 		if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) == 0)
710 			np->n_mtime = mtime.tv_sec;
711 	}
712 	vap = &np->n_vattr;
713 	vap->va_type = vtyp;
714 	vap->va_mode = (vmode & 07777);
715 	vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
716 	vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
717 	vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
718 	vap->va_size = fxdr_unsigned(u_long, fp->fa_size);
719 	if ((np->n_flag & NMODIFIED) == 0 || vap->va_size > np->n_size) {
720 		np->n_size = vap->va_size;
721 		vnode_pager_setsize(vp, (u_long)np->n_size);
722 	}
723 	vap->va_blocksize = fxdr_unsigned(long, fp->fa_blocksize);
724 	vap->va_rdev = (dev_t)rdev;
725 	vap->va_bytes = fxdr_unsigned(long, fp->fa_blocks) * NFS_FABLKSIZE;
726 	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
727 	vap->va_fileid = fxdr_unsigned(long, fp->fa_fileid);
728 	vap->va_atime.ts_sec = fxdr_unsigned(long, fp->fa_atime.tv_sec);
729 	vap->va_atime.ts_nsec = 0;
730 	vap->va_flags = fxdr_unsigned(u_long, fp->fa_atime.tv_usec);
731 	vap->va_mtime.ts_sec = mtime.tv_sec;
732 	vap->va_mtime.ts_nsec = mtime.tv_usec * 1000;
733 	vap->va_ctime.ts_sec = fxdr_unsigned(long, fp->fa_ctime.tv_sec);
734 	vap->va_ctime.ts_nsec = 0;
735 	vap->va_gen = fxdr_unsigned(u_long, fp->fa_ctime.tv_usec);
736 	np->n_attrstamp = time.tv_sec;
737 	*dposp = dpos;
738 	*mdp = md;
739 	if (vaper != NULL) {
740 		bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
741 		if ((np->n_flag & NMODIFIED) && (np->n_size > vap->va_size))
742 			vaper->va_size = np->n_size;
743 		if (np->n_flag & NCHG) {
744 			if (np->n_flag & NACC) {
745 				vaper->va_atime.ts_sec = np->n_atim.tv_sec;
746 				vaper->va_atime.ts_nsec =
747 				    np->n_atim.tv_usec * 1000;
748 			}
749 			if (np->n_flag & NUPD) {
750 				vaper->va_mtime.ts_sec = np->n_mtim.tv_sec;
751 				vaper->va_mtime.ts_nsec =
752 				    np->n_mtim.tv_usec * 1000;
753 			}
754 		}
755 	}
756 	return (0);
757 }
758 
759 /*
760  * Check the time stamp
761  * If the cache is valid, copy contents to *vap and return 0
762  * otherwise return an error
763  */
764 nfs_getattrcache(vp, vap)
765 	register struct vnode *vp;
766 	struct vattr *vap;
767 {
768 	register struct nfsnode *np;
769 
770 	np = VTONFS(vp);
771 	if (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) {
772 		if (!NQNFS_CKCACHABLE(vp, NQL_READ) || np->n_attrstamp == 0) {
773 			nfsstats.attrcache_misses++;
774 			return (ENOENT);
775 		}
776 	} else if ((time.tv_sec - np->n_attrstamp) >= NFS_ATTRTIMEO) {
777 		nfsstats.attrcache_misses++;
778 		return (ENOENT);
779 	}
780 	nfsstats.attrcache_hits++;
781 	bcopy((caddr_t)&np->n_vattr,(caddr_t)vap,sizeof(struct vattr));
782 	if ((np->n_flag & NMODIFIED) == 0) {
783 		np->n_size = vap->va_size;
784 		vnode_pager_setsize(vp, (u_long)np->n_size);
785 	} else if (np->n_size > vap->va_size)
786 		vap->va_size = np->n_size;
787 	if (np->n_flag & NCHG) {
788 		if (np->n_flag & NACC) {
789 			vap->va_atime.ts_sec = np->n_atim.tv_sec;
790 			vap->va_atime.ts_nsec = np->n_atim.tv_usec * 1000;
791 		}
792 		if (np->n_flag & NUPD) {
793 			vap->va_mtime.ts_sec = np->n_mtim.tv_sec;
794 			vap->va_mtime.ts_nsec = np->n_mtim.tv_usec * 1000;
795 		}
796 	}
797 	return (0);
798 }
799 
800 /*
801  * Set up nameidata for a lookup() call and do it
802  */
803 nfs_namei(ndp, fhp, len, slp, nam, mdp, dposp, p)
804 	register struct nameidata *ndp;
805 	fhandle_t *fhp;
806 	int len;
807 	struct nfssvc_sock *slp;
808 	struct mbuf *nam;
809 	struct mbuf **mdp;
810 	caddr_t *dposp;
811 	struct proc *p;
812 {
813 	register int i, rem;
814 	register struct mbuf *md;
815 	register char *fromcp, *tocp;
816 	struct vnode *dp;
817 	int error, rdonly;
818 	struct componentname *cnp = &ndp->ni_cnd;
819 
820 	MALLOC(cnp->cn_pnbuf, char *, len + 1, M_NAMEI, M_WAITOK);
821 	/*
822 	 * Copy the name from the mbuf list to ndp->ni_pnbuf
823 	 * and set the various ndp fields appropriately.
824 	 */
825 	fromcp = *dposp;
826 	tocp = cnp->cn_pnbuf;
827 	md = *mdp;
828 	rem = mtod(md, caddr_t) + md->m_len - fromcp;
829 	cnp->cn_hash = 0;
830 	for (i = 0; i < len; i++) {
831 		while (rem == 0) {
832 			md = md->m_next;
833 			if (md == NULL) {
834 				error = EBADRPC;
835 				goto out;
836 			}
837 			fromcp = mtod(md, caddr_t);
838 			rem = md->m_len;
839 		}
840 		if (*fromcp == '\0' || *fromcp == '/') {
841 			error = EINVAL;
842 			goto out;
843 		}
844 		if (*fromcp & 0200)
845 			if ((*fromcp&0377) == ('/'|0200) || cnp->cn_nameiop != DELETE) {
846 				error = EINVAL;
847 				goto out;
848 			}
849 		cnp->cn_hash += (unsigned char)*fromcp;
850 		*tocp++ = *fromcp++;
851 		rem--;
852 	}
853 	*tocp = '\0';
854 	*mdp = md;
855 	*dposp = fromcp;
856 	len = nfsm_rndup(len)-len;
857 	if (len > 0) {
858 		if (rem >= len)
859 			*dposp += len;
860 		else if (error = nfs_adv(mdp, dposp, len, rem))
861 			goto out;
862 	}
863 	ndp->ni_pathlen = tocp - cnp->cn_pnbuf;
864 	cnp->cn_nameptr = cnp->cn_pnbuf;
865 	/*
866 	 * Extract and set starting directory.
867 	 */
868 	if (error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp,
869 	    nam, &rdonly))
870 		goto out;
871 	if (dp->v_type != VDIR) {
872 		vrele(dp);
873 		error = ENOTDIR;
874 		goto out;
875 	}
876 	ndp->ni_startdir = dp;
877 	if (rdonly)
878 		cnp->cn_flags |= (NOCROSSMOUNT | RDONLY);
879 	else
880 		cnp->cn_flags |= NOCROSSMOUNT;
881 	/*
882 	 * And call lookup() to do the real work
883 	 */
884 	cnp->cn_proc = p;
885 	if (error = lookup(ndp))
886 		goto out;
887 	/*
888 	 * Check for encountering a symbolic link
889 	 */
890 	if (cnp->cn_flags & ISSYMLINK) {
891 		if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
892 			vput(ndp->ni_dvp);
893 		else
894 			vrele(ndp->ni_dvp);
895 		vput(ndp->ni_vp);
896 		ndp->ni_vp = NULL;
897 		error = EINVAL;
898 		goto out;
899 	}
900 	/*
901 	 * Check for saved name request
902 	 */
903 	if (cnp->cn_flags & (SAVENAME | SAVESTART)) {
904 		cnp->cn_flags |= HASBUF;
905 		return (0);
906 	}
907 out:
908 	FREE(cnp->cn_pnbuf, M_NAMEI);
909 	return (error);
910 }
911 
912 /*
913  * A fiddled version of m_adj() that ensures null fill to a long
914  * boundary and only trims off the back end
915  */
916 void
917 nfsm_adj(mp, len, nul)
918 	struct mbuf *mp;
919 	register int len;
920 	int nul;
921 {
922 	register struct mbuf *m;
923 	register int count, i;
924 	register char *cp;
925 
926 	/*
927 	 * Trim from tail.  Scan the mbuf chain,
928 	 * calculating its length and finding the last mbuf.
929 	 * If the adjustment only affects this mbuf, then just
930 	 * adjust and return.  Otherwise, rescan and truncate
931 	 * after the remaining size.
932 	 */
933 	count = 0;
934 	m = mp;
935 	for (;;) {
936 		count += m->m_len;
937 		if (m->m_next == (struct mbuf *)0)
938 			break;
939 		m = m->m_next;
940 	}
941 	if (m->m_len > len) {
942 		m->m_len -= len;
943 		if (nul > 0) {
944 			cp = mtod(m, caddr_t)+m->m_len-nul;
945 			for (i = 0; i < nul; i++)
946 				*cp++ = '\0';
947 		}
948 		return;
949 	}
950 	count -= len;
951 	if (count < 0)
952 		count = 0;
953 	/*
954 	 * Correct length for chain is "count".
955 	 * Find the mbuf with last data, adjust its length,
956 	 * and toss data from remaining mbufs on chain.
957 	 */
958 	for (m = mp; m; m = m->m_next) {
959 		if (m->m_len >= count) {
960 			m->m_len = count;
961 			if (nul > 0) {
962 				cp = mtod(m, caddr_t)+m->m_len-nul;
963 				for (i = 0; i < nul; i++)
964 					*cp++ = '\0';
965 			}
966 			break;
967 		}
968 		count -= m->m_len;
969 	}
970 	while (m = m->m_next)
971 		m->m_len = 0;
972 }
973 
974 /*
975  * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
976  * 	- look up fsid in mount list (if not found ret error)
977  *	- get vp and export rights by calling VFS_FHTOVP()
978  *	- if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
979  *	- if not lockflag unlock it with VOP_UNLOCK()
980  */
981 nfsrv_fhtovp(fhp, lockflag, vpp, cred, slp, nam, rdonlyp)
982 	fhandle_t *fhp;
983 	int lockflag;
984 	struct vnode **vpp;
985 	struct ucred *cred;
986 	struct nfssvc_sock *slp;
987 	struct mbuf *nam;
988 	int *rdonlyp;
989 {
990 	register struct mount *mp;
991 	register struct nfsuid *uidp;
992 	struct ucred *credanon;
993 	int error, exflags;
994 
995 	*vpp = (struct vnode *)0;
996 	if ((mp = getvfs(&fhp->fh_fsid)) == NULL)
997 		return (ESTALE);
998 	if (error = VFS_FHTOVP(mp, &fhp->fh_fid, nam, vpp, &exflags, &credanon))
999 		return (error);
1000 	/*
1001 	 * Check/setup credentials.
1002 	 */
1003 	if (exflags & MNT_EXKERB) {
1004 		uidp = slp->ns_uidh[NUIDHASH(cred->cr_uid)];
1005 		while (uidp) {
1006 			if (uidp->nu_uid == cred->cr_uid)
1007 				break;
1008 			uidp = uidp->nu_hnext;
1009 		}
1010 		if (uidp) {
1011 			if (cred->cr_ref != 1)
1012 				panic("nsrv fhtovp");
1013 			*cred = uidp->nu_cr;
1014 		} else
1015 			return (NQNFS_AUTHERR);
1016 	} else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON))
1017 		*cred = *credanon;
1018 	if (exflags & MNT_EXRDONLY)
1019 		*rdonlyp = 1;
1020 	else
1021 		*rdonlyp = 0;
1022 	if (!lockflag)
1023 		VOP_UNLOCK(*vpp);
1024 	return (0);
1025 }
1026 
1027 /*
1028  * This function compares two net addresses by family and returns TRUE
1029  * if they are the same host.
1030  * If there is any doubt, return FALSE.
1031  * The AF_INET family is handled as a special case so that address mbufs
1032  * don't need to be saved to store "struct in_addr", which is only 4 bytes.
1033  */
1034 netaddr_match(family, haddr, hmask, nam)
1035 	int family;
1036 	union nethostaddr *haddr;
1037 	union nethostaddr *hmask;
1038 	struct mbuf *nam;
1039 {
1040 	register struct sockaddr_in *inetaddr;
1041 #ifdef ISO
1042 	register struct sockaddr_iso *isoaddr1, *isoaddr2;
1043 #endif
1044 
1045 
1046 	switch (family) {
1047 	case AF_INET:
1048 		inetaddr = mtod(nam, struct sockaddr_in *);
1049 		if (inetaddr->sin_family != AF_INET)
1050 			return (0);
1051 		if (hmask) {
1052 			if ((inetaddr->sin_addr.s_addr & hmask->had_inetaddr) ==
1053 			    (haddr->had_inetaddr & hmask->had_inetaddr))
1054 				return (1);
1055 		} else if (inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
1056 			return (1);
1057 		break;
1058 #ifdef ISO
1059 	case AF_ISO:
1060 		isoaddr1 = mtod(nam, struct sockaddr_iso *);
1061 		if (isoaddr1->siso_family != AF_ISO)
1062 			return (0);
1063 		isoaddr2 = mtod(haddr->had_nam, struct sockaddr_iso *);
1064 		if (isoaddr1->siso_nlen > 0 &&
1065 		    isoaddr1->siso_nlen == isoaddr2->siso_nlen &&
1066 		    SAME_ISOADDR(isoaddr1, isoaddr2))
1067 			return (1);
1068 		break;
1069 #endif	/* ISO */
1070 	default:
1071 		break;
1072 	};
1073 	return (0);
1074 }
1075 
1076 /*
1077  * Generate a hash code for an iso host address. Used by NETADDRHASH() for
1078  * iso addresses.
1079  */
1080 iso_addrhash(saddr)
1081 	struct sockaddr *saddr;
1082 {
1083 #ifdef ISO
1084 	register struct sockaddr_iso *siso;
1085 	register int i, sum;
1086 
1087 	sum = 0;
1088 	for (i = 0; i < siso->siso_nlen; i++)
1089 		sum += siso->siso_data[i];
1090 	return (sum & (NETHASHSZ - 1));
1091 #else
1092 	return (0);
1093 #endif	/* ISO */
1094 }
1095