xref: /csrg-svn/sys/nfs/nfs_vnops.c (revision 39362)
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  * Redistribution and use in source and binary forms are permitted
9  * provided that the above copyright notice and this paragraph are
10  * duplicated in all such forms and that any documentation,
11  * advertising materials, and other materials related to such
12  * distribution and use acknowledge that the software was developed
13  * by the University of California, Berkeley.  The name of the
14  * University may not be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19  *
20  *	@(#)nfs_vnops.c	7.8 (Berkeley) 10/21/89
21  */
22 
23 /*
24  * vnode op calls for sun nfs version 2
25  */
26 
27 #include "machine/pte.h"
28 #include "machine/mtpr.h"
29 #include "strings.h"
30 #include "param.h"
31 #include "user.h"
32 #include "proc.h"
33 #include "mount.h"
34 #include "buf.h"
35 #include "vm.h"
36 #include "../ufs/dir.h"
37 #include "malloc.h"
38 #include "mbuf.h"
39 #include "uio.h"
40 #include "ucred.h"
41 #include "namei.h"
42 #include "errno.h"
43 #include "file.h"
44 #include "conf.h"
45 #include "vnode.h"
46 #include "../ufs/inode.h"
47 #include "map.h"
48 #include "nfsv2.h"
49 #include "nfs.h"
50 #include "nfsnode.h"
51 #include "nfsmount.h"
52 #include "xdr_subs.h"
53 #include "nfsm_subs.h"
54 #include "nfsiom.h"
55 
56 /* Defs */
57 #define	TRUE	1
58 #define	FALSE	0
59 
60 /* Global vars */
61 int	nfs_lookup(),
62 	nfs_create(),
63 	nfs_open(),
64 	nfs_close(),
65 	nfs_access(),
66 	nfs_getattr(),
67 	nfs_setattr(),
68 	nfs_read(),
69 	nfs_write(),
70 	vfs_noop(),
71 	vfs_nullop(),
72 	nfs_remove(),
73 	nfs_link(),
74 	nfs_rename(),
75 	nfs_mkdir(),
76 	nfs_rmdir(),
77 	nfs_symlink(),
78 	nfs_readdir(),
79 	nfs_readlink(),
80 	nfs_abortop(),
81 	nfs_lock(),
82 	nfs_unlock(),
83 	nfs_bmap(),
84 	nfs_strategy(),
85 	nfs_fsync(),
86 	nfs_inactive();
87 
88 struct vnodeops nfsv2_vnodeops = {
89 	nfs_lookup,
90 	nfs_create,
91 	vfs_noop,
92 	nfs_open,
93 	nfs_close,
94 	nfs_access,
95 	nfs_getattr,
96 	nfs_setattr,
97 	nfs_read,
98 	nfs_write,
99 	vfs_noop,
100 	vfs_noop,
101 	vfs_noop,
102 	nfs_fsync,
103 	vfs_noop,
104 	nfs_remove,
105 	nfs_link,
106 	nfs_rename,
107 	nfs_mkdir,
108 	nfs_rmdir,
109 	nfs_symlink,
110 	nfs_readdir,
111 	nfs_readlink,
112 	nfs_abortop,
113 	nfs_inactive,
114 	nfs_lock,
115 	nfs_unlock,
116 	nfs_bmap,
117 	nfs_strategy,
118 };
119 
120 /* Special device vnode ops */
121 int	blk_lookup(),
122 	blk_open(),
123 	blk_read(),
124 	blk_write(),
125 	blk_ioctl(),
126 	blk_select();
127 
128 struct vnodeops nfsv2chr_vnodeops = {
129 	blk_lookup,
130 	vfs_noop,
131 	vfs_noop,
132 	blk_open,
133 	nfs_close,
134 	nfs_access,
135 	nfs_getattr,
136 	nfs_setattr,
137 	blk_read,
138 	blk_write,
139 	blk_ioctl,
140 	blk_select,
141 	vfs_noop,
142 	vfs_nullop,
143 	vfs_noop,
144 	nfs_remove,
145 	nfs_link,
146 	nfs_rename,
147 	vfs_noop,
148 	vfs_noop,
149 	nfs_symlink,
150 	vfs_noop,
151 	vfs_noop,
152 	nfs_abortop,
153 	nfs_inactive,
154 	nfs_lock,
155 	nfs_unlock,
156 	vfs_noop,
157 	vfs_noop,
158 };
159 
160 extern u_long nfs_procids[NFS_NPROCS];
161 extern u_long nfs_prog, nfs_vers;
162 extern char nfsiobuf[MAXPHYS+NBPG];
163 struct map nfsmap[NFS_MSIZ];
164 enum vtype v_type[NFLNK+1];
165 struct buf nfs_bqueue;		/* Queue head for nfsiod's */
166 int nfs_asyncdaemons = 0;
167 struct proc *nfs_iodwant[MAX_ASYNCDAEMON];
168 static int nfsmap_want = 0;
169 
170 /*
171  * nfs null call from vfs.
172  */
173 nfs_null(vp, cred)
174 	struct vnode *vp;
175 	struct ucred *cred;
176 {
177 	nfsm_vars;
178 
179 	nfsm_reqhead(nfs_procids[NFSPROC_NULL], cred, 0);
180 	nfsm_request(vp);
181 	nfsm_reqdone;
182 	return (error);
183 }
184 
185 /*
186  * nfs access vnode op.
187  * Essentially just get vattr and then imitate iaccess()
188  */
189 nfs_access(vp, mode, cred)
190 	struct vnode *vp;
191 	int mode;
192 	register struct ucred *cred;
193 {
194 	register struct vattr *vap;
195 	register gid_t *gp;
196 	struct vattr vattr;
197 	register int i;
198 	int error;
199 
200 	/*
201 	 * If you're the super-user,
202 	 * you always get access.
203 	 */
204 	if (cred->cr_uid == 0)
205 		return (0);
206 	vap = &vattr;
207 	if (error = nfs_getattr(vp, vap, cred))
208 		return (error);
209 	/*
210 	 * Access check is based on only one of owner, group, public.
211 	 * If not owner, then check group. If not a member of the
212 	 * group, then check public access.
213 	 */
214 	if (cred->cr_uid != vap->va_uid) {
215 		mode >>= 3;
216 		gp = cred->cr_groups;
217 		for (i = 0; i < cred->cr_ngroups; i++, gp++)
218 			if (vap->va_gid == *gp)
219 				goto found;
220 		mode >>= 3;
221 found:
222 		;
223 	}
224 	if ((vap->va_mode & mode) != 0)
225 		return (0);
226 	return (EACCES);
227 }
228 
229 /*
230  * nfs open vnode op
231  * Just check to see if the type is ok
232  */
233 nfs_open(vp, mode, cred)
234 	struct vnode *vp;
235 	int mode;
236 	struct ucred *cred;
237 {
238 	register enum vtype vtyp;
239 
240 	vtyp = vp->v_type;
241 	if (vtyp == VREG || vtyp == VDIR || vtyp == VLNK)
242 		return (0);
243 	else
244 		return (EACCES);
245 }
246 
247 /*
248  * nfs close vnode op
249  * For reg files, invalidate any buffer cache entries.
250  * For VCHR, do the device close
251  */
252 nfs_close(vp, fflags, cred)
253 	register struct vnode *vp;
254 	int fflags;
255 	struct ucred *cred;
256 {
257 	struct nfsnode *np = VTONFS(vp);
258 	dev_t dev;
259 	int error = 0;
260 
261 	nfs_lock(vp);
262 	if (vp->v_type == VREG && ((np->n_flag & NMODIFIED) ||
263 	   ((np->n_flag & NBUFFERED) && np->n_sillyrename))) {
264 		np->n_flag &= ~(NMODIFIED|NBUFFERED);
265 		error = nfs_blkflush(vp, (daddr_t)0, np->n_size, TRUE);
266 		if (np->n_flag & NWRITEERR) {
267 			np->n_flag &= ~NWRITEERR;
268 			if (!error)
269 				error = np->n_error ? np->n_error : EIO;
270 		}
271 	}
272 	nfs_unlock(vp);
273 	if (vp->v_type != VCHR || vp->v_count > 1)
274 		return (error);
275 	dev = vp->v_rdev;
276 	/* XXX what is this doing below the vnode op call */
277 	if (setjmp(&u.u_qsave)) {
278 		/*
279 		 * If device close routine is interrupted,
280 		 * must return so closef can clean up.
281 		 */
282 		error = EINTR;
283 	} else
284 		error = (*cdevsw[major(dev)].d_close)(dev, fflags, IFCHR);
285 	/*
286 	 * Most device close routines don't return errors,
287 	 * and dup2() doesn't work right on error.
288 	 */
289 	error = 0;		/* XXX */
290 	return (error);
291 }
292 
293 /*
294  * nfs getattr call from vfs.
295  */
296 nfs_getattr(vp, vap, cred)
297 	struct vnode *vp;
298 	register struct vattr *vap;
299 	struct ucred *cred;
300 {
301 	nfsm_vars;
302 
303 	/* First look in the cache.. */
304 	if (nfs_getattrcache(vp, vap) == 0)
305 		return (0);
306 	nfsstats.rpccnt[NFSPROC_GETATTR]++;
307 	nfsm_reqhead(nfs_procids[NFSPROC_GETATTR], cred, NFSX_FH);
308 	nfsm_fhtom(vp);
309 	nfsm_request(vp);
310 	nfsm_loadattr(vp, vap);
311 	nfsm_reqdone;
312 	return (error);
313 }
314 
315 /*
316  * nfs setattr call.
317  */
318 nfs_setattr(vp, vap, cred)
319 	struct vnode *vp;
320 	register struct vattr *vap;
321 	struct ucred *cred;
322 {
323 	register struct nfsv2_sattr *sp;
324 	nfsm_vars;
325 	struct nfsnode *np;
326 
327 	nfsstats.rpccnt[NFSPROC_SETATTR]++;
328 	nfsm_reqhead(nfs_procids[NFSPROC_SETATTR], cred, NFSX_FH+NFSX_SATTR);
329 	nfsm_fhtom(vp);
330 	nfsm_build(sp, struct nfsv2_sattr *, NFSX_SATTR);
331 	if (vap->va_mode == 0xffff)
332 		sp->sa_mode = VNOVAL;
333 	else
334 		sp->sa_mode = vtonfs_mode(vp->v_type, vap->va_mode);
335 	if (vap->va_uid == 0xffff)
336 		sp->sa_uid = VNOVAL;
337 	else
338 		sp->sa_uid = txdr_unsigned(vap->va_uid);
339 	if (vap->va_gid == 0xffff)
340 		sp->sa_gid = VNOVAL;
341 	else
342 		sp->sa_gid = txdr_unsigned(vap->va_gid);
343 	sp->sa_size = txdr_unsigned(vap->va_size);
344 	if (vap->va_size != VNOVAL) {
345 		np = VTONFS(vp);
346 		if (np->n_flag & NMODIFIED) {
347 			np->n_flag &= ~NMODIFIED;
348 			nfs_blkflush(vp, (daddr_t)0, np->n_size, TRUE);
349 		}
350 	}
351 	txdr_time(&vap->va_atime, &sp->sa_atime);
352 	txdr_time(&vap->va_mtime, &sp->sa_mtime);
353 	nfsm_request(vp);
354 	nfsm_loadattr(vp, (struct vattr *)0);
355 	/* should we fill in any vap fields ?? */
356 	nfsm_reqdone;
357 	return (error);
358 }
359 
360 /*
361  * nfs lookup call, one step at a time...
362  * First look in cache
363  * If not found, unlock the directory nfsnode and do the rpc
364  */
365 nfs_lookup(vp, ndp)
366 	register struct vnode *vp;
367 	register struct nameidata *ndp;
368 {
369 	register struct vnode *vdp;
370 	nfsm_vars;
371 	struct vnode *newvp;
372 	long len;
373 	nfsv2fh_t *fhp;
374 	struct nfsnode *np;
375 	int lockparent, wantparent, flag;
376 	dev_t rdev;
377 
378 	ndp->ni_dvp = vp;
379 	ndp->ni_vp = NULL;
380 	if (vp->v_type != VDIR)
381 		return (ENOTDIR);
382 	lockparent = ndp->ni_nameiop & LOCKPARENT;
383 	flag = ndp->ni_nameiop & OPFLAG;
384 	wantparent = ndp->ni_nameiop & (LOCKPARENT|WANTPARENT);
385 	if ((error = cache_lookup(ndp)) && error != ENOENT) {
386 		struct vattr vattr;
387 		int vpid;
388 
389 		if (ndp->ni_vp == ndp->ni_rdir && ndp->ni_isdotdot)
390 			vdp = vp;
391 		else
392 			vdp = ndp->ni_vp;
393 		vpid = vdp->v_id;
394 		/*
395 		 * See the comment starting `Step through' in ufs/ufs_lookup.c
396 		 * for an explanation of the locking protocol
397 		 */
398 		if (vp == vdp) {
399 			VREF(vdp);
400 		} else if (ndp->ni_isdotdot) {
401 			nfs_unlock(vp);
402 			nfs_ngrab(VTONFS(vdp));
403 		} else {
404 			nfs_ngrab(VTONFS(vdp));
405 			nfs_unlock(vp);
406 		}
407 		if (vpid == vdp->v_id &&
408 		   !nfs_getattr(vdp, &vattr, ndp->ni_cred)) {
409 			nfsstats.lookupcache_hits++;
410 			return (0);
411 		}
412 		nfs_nput(vdp);
413 		nfs_lock(vp);
414 		ndp->ni_vp = (struct vnode *)0;
415 	}
416 	error = 0;
417 	nfsstats.lookupcache_misses++;
418 	nfsstats.rpccnt[NFSPROC_LOOKUP]++;
419 	len = ndp->ni_namelen;
420 	nfsm_reqhead(nfs_procids[NFSPROC_LOOKUP], ndp->ni_cred, NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(len));
421 	nfsm_fhtom(vp);
422 	nfsm_strtom(ndp->ni_ptr, len, NFS_MAXNAMLEN);
423 	nfsm_request(vp);
424 nfsmout:
425 	if (error) {
426 		if ((flag == CREATE || flag == RENAME) &&
427 			*ndp->ni_next == 0) {
428 			if (!lockparent)
429 				nfs_unlock(vp);
430 		}
431 		return (ENOENT);
432 	}
433 	nfsm_disect(fhp,nfsv2fh_t *,NFSX_FH);
434 
435 	/*
436 	 * Handle DELETE and RENAME cases...
437 	 */
438 	if (flag == DELETE && *ndp->ni_next == 0) {
439 		if (!bcmp(VTONFS(vp)->n_fh.fh_bytes, (caddr_t)fhp, NFSX_FH)) {
440 			VREF(vp);
441 			newvp = vp;
442 			np = VTONFS(vp);
443 		} else {
444 			if (error = nfs_nget(vp->v_mount, fhp, &np)) {
445 				m_freem(mrep);
446 				return (error);
447 			}
448 			newvp = NFSTOV(np);
449 		}
450 		if (error = nfs_loadattrcache(newvp, &md, &dpos, (struct vattr *)0)) {
451 			if (newvp != vp)
452 				nfs_nput(newvp);
453 			else
454 				vrele(vp);
455 			m_freem(mrep);
456 			return (error);
457 		}
458 		ndp->ni_vp = newvp;
459 		if (!lockparent)
460 			nfs_unlock(vp);
461 		m_freem(mrep);
462 		return (0);
463 	}
464 
465 	if (flag == RENAME && wantparent && *ndp->ni_next == 0) {
466 		if (!bcmp(VTONFS(vp)->n_fh.fh_bytes, (caddr_t)fhp, NFSX_FH)) {
467 			m_freem(mrep);
468 			return (EISDIR);
469 		}
470 		if (error = nfs_nget(vp->v_mount, fhp, &np)) {
471 			m_freem(mrep);
472 			return (error);
473 		}
474 		newvp = NFSTOV(np);
475 		if (error = nfs_loadattrcache(newvp, &md, &dpos, (struct vattr *)0)) {
476 			nfs_nput(newvp);
477 			m_freem(mrep);
478 			return (error);
479 		}
480 		ndp->ni_vp = newvp;
481 		if (!lockparent)
482 			nfs_unlock(vp);
483 		return (0);
484 	}
485 
486 	if (!bcmp(VTONFS(vp)->n_fh.fh_bytes, (caddr_t)fhp, NFSX_FH)) {
487 		VREF(vp);
488 		newvp = vp;
489 		np = VTONFS(vp);
490 	} else if (ndp->ni_isdotdot) {
491 		nfs_unlock(vp);
492 		if (error = nfs_nget(vp->v_mount, fhp, &np)) {
493 			nfs_lock(vp);
494 			m_freem(mrep);
495 			return (error);
496 		}
497 		nfs_lock(vp);
498 		newvp = NFSTOV(np);
499 	} else {
500 		if (error = nfs_nget(vp->v_mount, fhp, &np)) {
501 			m_freem(mrep);
502 			return (error);
503 		}
504 		newvp = NFSTOV(np);
505 	}
506 	if (error = nfs_loadattrcache(newvp, &md, &dpos, (struct vattr *)0)) {
507 		if (newvp != vp)
508 			nfs_nput(newvp);
509 		else
510 			vrele(vp);
511 		m_freem(mrep);
512 		return (error);
513 	}
514 	m_freem(mrep);
515 
516 	if (vp != newvp && (!lockparent || *ndp->ni_next != '\0'))
517 		nfs_unlock(vp);
518 	ndp->ni_vp = newvp;
519 	if (error == 0 && ndp->ni_makeentry)
520 		cache_enter(ndp);
521 	return (error);
522 }
523 
524 /*
525  * nfs readlink call
526  */
527 nfs_readlink(vp, uiop, cred)
528 	struct vnode *vp;
529 	struct uio *uiop;
530 	struct ucred *cred;
531 {
532 	nfsm_vars;
533 	long len;
534 
535 	nfsstats.rpccnt[NFSPROC_READLINK]++;
536 	nfsm_reqhead(nfs_procids[NFSPROC_READLINK], cred, NFSX_FH);
537 	nfsm_fhtom(vp);
538 	nfsm_request(vp);
539 	nfsm_strsiz(len, NFS_MAXPATHLEN);
540 	nfsm_mtouio(uiop, len);
541 	nfsm_reqdone;
542 	return (error);
543 }
544 
545 /*
546  * nfs read call
547  */
548 nfs_readrpc(vp, uiop, offp, cred)
549 	struct vnode *vp;
550 	struct uio *uiop;
551 	off_t *offp;
552 	struct ucred *cred;
553 {
554 	nfsm_vars;
555 	struct nfsmount *nmp;
556 	long len, retlen, tsiz;
557 
558 	nmp = vfs_to_nfs(vp->v_mount);
559 	tsiz = uiop->uio_resid;
560 	while (tsiz > 0) {
561 		nfsstats.rpccnt[NFSPROC_READ]++;
562 		len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
563 		nfsm_reqhead(nfs_procids[NFSPROC_READ], cred, NFSX_FH+NFSX_UNSIGNED*3);
564 		nfsm_fhtom(vp);
565 		nfsm_build(p, u_long *, NFSX_UNSIGNED*3);
566 		*p++ = txdr_unsigned(*offp);
567 		*p++ = txdr_unsigned(len);
568 		*p = 0;
569 		nfsm_request(vp);
570 		nfsm_loadattr(vp, (struct vattr *)0);
571 		nfsm_strsiz(retlen, nmp->nm_rsize);
572 		nfsm_mtouio(uiop, retlen);
573 		m_freem(mrep);
574 		*offp += retlen;
575 		if (retlen < len)
576 			tsiz = 0;
577 		else
578 			tsiz -= len;
579 	}
580 nfsmout:
581 	return (error);
582 }
583 
584 /*
585  * nfs write call
586  */
587 nfs_writerpc(vp, uiop, offp, cred)
588 	struct vnode *vp;
589 	struct uio *uiop;
590 	off_t *offp;
591 	struct ucred *cred;
592 {
593 	nfsm_vars;
594 	struct nfsmount *nmp;
595 	long len, tsiz;
596 
597 	nmp = vfs_to_nfs(vp->v_mount);
598 	tsiz = uiop->uio_resid;
599 	while (tsiz > 0) {
600 		nfsstats.rpccnt[NFSPROC_WRITE]++;
601 		len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
602 		nfsm_reqhead(nfs_procids[NFSPROC_WRITE], cred,
603 			NFSX_FH+NFSX_UNSIGNED*4);
604 		nfsm_fhtom(vp);
605 		nfsm_build(p, u_long *, NFSX_UNSIGNED*4);
606 		*(p+1) = txdr_unsigned(*offp);
607 		*(p+3) = txdr_unsigned(len);
608 		nfsm_uiotom(uiop, len);
609 		nfsm_request(vp);
610 		nfsm_loadattr(vp, (struct vattr *)0);
611 		m_freem(mrep);
612 		tsiz -= len;
613 		*offp += len;
614 	}
615 nfsmout:
616 	return (error);
617 }
618 
619 /*
620  * nfs file create call
621  */
622 nfs_create(ndp, vap)
623 	register struct nameidata *ndp;
624 	register struct vattr *vap;
625 {
626 	register struct nfsv2_sattr *sp;
627 	nfsm_vars;
628 
629 	nfsstats.rpccnt[NFSPROC_CREATE]++;
630 	nfsm_reqhead(nfs_procids[NFSPROC_CREATE], ndp->ni_cred,
631 	  NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen)+NFSX_SATTR);
632 	nfsm_fhtom(ndp->ni_dvp);
633 	nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN);
634 	nfsm_build(sp, struct nfsv2_sattr *, NFSX_SATTR);
635 	sp->sa_mode = vtonfs_mode(VREG, vap->va_mode);
636 	sp->sa_uid = txdr_unsigned(ndp->ni_cred->cr_uid);
637 	sp->sa_gid = txdr_unsigned(ndp->ni_cred->cr_gid);
638 	sp->sa_size = txdr_unsigned(0);
639 	/* or should these be VNOVAL ?? */
640 	txdr_time(&vap->va_atime, &sp->sa_atime);
641 	txdr_time(&vap->va_mtime, &sp->sa_mtime);
642 	nfsm_request(ndp->ni_dvp);
643 	nfsm_mtofh(ndp->ni_dvp, ndp->ni_vp);
644 	nfsm_reqdone;
645 	nfs_nput(ndp->ni_dvp);
646 	return (error);
647 }
648 
649 /*
650  * nfs file remove call
651  * To try and make nfs semantics closer to vfs semantics, a file that has
652  * other references to the vnode is renamed instead of removed and then
653  * removed later on the last close.
654  * Unfortunately you must flush the buffer cache and cmap to get rid of
655  * all extraneous vnode references before you check the reference cnt.
656  * 1 - If the file could have blocks in the buffer cache
657  *	  flush them out and invalidate them
658  *	  mpurge the vnode to flush out cmap references
659  *	  (This is necessary to update the vnode ref cnt as well as sensible
660  *	   for actual removes, to free up the buffers)
661  * 2 - If v_count > 1
662  *	  If a rename is not already in the works
663  *	     call nfs_sillyrename() to set it up
664  *     else
665  *	  do the remove rpc
666  */
667 nfs_remove(ndp)
668 	register struct nameidata *ndp;
669 {
670 	register struct vnode *vp = ndp->ni_vp;
671 	register struct nfsnode *np = VTONFS(ndp->ni_vp);
672 	nfsm_vars;
673 
674 	if (vp->v_type == VREG) {
675 		if (np->n_flag & (NMODIFIED|NBUFFERED)) {
676 			np->n_flag &= ~(NMODIFIED|NBUFFERED);
677 			nfs_blkflush(vp, (daddr_t)0, np->n_size, TRUE);
678 		}
679 		if (np->n_flag & NPAGEDON)
680 			mpurge(vp);	/* In case cmap entries still ref it */
681 	}
682 	if (vp->v_count > 1) {
683 		if (!np->n_sillyrename)
684 			error = nfs_sillyrename(ndp, REMOVE);
685 	} else {
686 		nfsstats.rpccnt[NFSPROC_REMOVE]++;
687 		nfsm_reqhead(nfs_procids[NFSPROC_REMOVE], ndp->ni_cred,
688 			NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen));
689 		nfsm_fhtom(ndp->ni_dvp);
690 		nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN);
691 		nfsm_request(ndp->ni_dvp);
692 		nfsm_reqdone;
693 	}
694 	if (ndp->ni_dvp == ndp->ni_vp)
695 		vrele(ndp->ni_vp);
696 	else
697 		nfs_nput(ndp->ni_vp);
698 	nfs_nput(ndp->ni_dvp);
699 	return (error);
700 }
701 
702 /*
703  * nfs file remove rpc called from nfs_inactive
704  */
705 nfs_removeit(ndp)
706 	register struct nameidata *ndp;
707 {
708 	nfsm_vars;
709 
710 	nfsstats.rpccnt[NFSPROC_REMOVE]++;
711 	nfsm_reqhead(nfs_procids[NFSPROC_REMOVE], ndp->ni_cred,
712 		NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen));
713 	nfsm_fhtom(ndp->ni_dvp);
714 	nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN);
715 	nfsm_request(ndp->ni_dvp);
716 	nfsm_reqdone;
717 	return (error);
718 }
719 
720 /*
721  * nfs file rename call
722  */
723 nfs_rename(sndp, tndp)
724 	register struct nameidata *sndp, *tndp;
725 {
726 	nfsm_vars;
727 
728 	nfsstats.rpccnt[NFSPROC_RENAME]++;
729 	nfsm_reqhead(nfs_procids[NFSPROC_RENAME], tndp->ni_cred,
730 		(NFSX_FH+NFSX_UNSIGNED)*2+nfsm_rndup(sndp->ni_dent.d_namlen)+
731 		nfsm_rndup(tndp->ni_dent.d_namlen)); /* or sndp->ni_cred?*/
732 	nfsm_fhtom(sndp->ni_dvp);
733 	nfsm_strtom(sndp->ni_dent.d_name,sndp->ni_dent.d_namlen,NFS_MAXNAMLEN);
734 	nfsm_fhtom(tndp->ni_dvp);
735 	nfsm_strtom(tndp->ni_dent.d_name,tndp->ni_dent.d_namlen,NFS_MAXNAMLEN);
736 	nfsm_request(sndp->ni_dvp);
737 	nfsm_reqdone;
738 	if (sndp->ni_vp->v_type == VDIR) {
739 		if (tndp->ni_vp != NULL && tndp->ni_vp->v_type == VDIR)
740 			cache_purge(tndp->ni_dvp);
741 		cache_purge(sndp->ni_dvp);
742 	}
743 	nfs_abortop(sndp);
744 	nfs_abortop(tndp);
745 	return (error);
746 }
747 
748 /*
749  * nfs file rename rpc called from above
750  */
751 nfs_renameit(sndp, tndp)
752 	register struct nameidata *sndp, *tndp;
753 {
754 	nfsm_vars;
755 
756 	nfsstats.rpccnt[NFSPROC_RENAME]++;
757 	nfsm_reqhead(nfs_procids[NFSPROC_RENAME], tndp->ni_cred,
758 		(NFSX_FH+NFSX_UNSIGNED)*2+nfsm_rndup(sndp->ni_dent.d_namlen)+
759 		nfsm_rndup(tndp->ni_dent.d_namlen)); /* or sndp->ni_cred?*/
760 	nfsm_fhtom(sndp->ni_dvp);
761 	nfsm_strtom(sndp->ni_dent.d_name,sndp->ni_dent.d_namlen,NFS_MAXNAMLEN);
762 	nfsm_fhtom(tndp->ni_dvp);
763 	nfsm_strtom(tndp->ni_dent.d_name,tndp->ni_dent.d_namlen,NFS_MAXNAMLEN);
764 	nfsm_request(sndp->ni_dvp);
765 	nfsm_reqdone;
766 	return (error);
767 }
768 
769 /*
770  * nfs hard link create call
771  */
772 nfs_link(vp, ndp)
773 	struct vnode *vp;
774 	register struct nameidata *ndp;
775 {
776 	nfsm_vars;
777 
778 	if (ndp->ni_dvp != vp)
779 		nfs_lock(vp);
780 	nfsstats.rpccnt[NFSPROC_LINK]++;
781 	nfsm_reqhead(nfs_procids[NFSPROC_LINK], ndp->ni_cred,
782 		NFSX_FH*2+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen));
783 	nfsm_fhtom(vp);
784 	nfsm_fhtom(ndp->ni_dvp);
785 	nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN);
786 	nfsm_request(vp);
787 	nfsm_reqdone;
788 	if (ndp->ni_dvp != vp)
789 		nfs_unlock(vp);
790 	nfs_nput(ndp->ni_dvp);
791 	return (error);
792 }
793 
794 /*
795  * nfs symbolic link create call
796  */
797 nfs_symlink(ndp, vap, nm)
798 	struct nameidata *ndp;
799 	struct vattr *vap;
800 	char *nm;		/* is this the path ?? */
801 {
802 	register struct nfsv2_sattr *sp;
803 	nfsm_vars;
804 
805 	nfsstats.rpccnt[NFSPROC_SYMLINK]++;
806 	nfsm_reqhead(nfs_procids[NFSPROC_SYMLINK], ndp->ni_cred,
807 	NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen)+NFSX_UNSIGNED);
808 	nfsm_fhtom(ndp->ni_dvp);
809 	nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN);
810 	nfsm_strtom(nm, strlen(nm), NFS_MAXPATHLEN);
811 	nfsm_build(sp, struct nfsv2_sattr *, NFSX_SATTR);
812 	sp->sa_mode = vtonfs_mode(VLNK, vap->va_mode);
813 	sp->sa_uid = txdr_unsigned(ndp->ni_cred->cr_uid);
814 	sp->sa_gid = txdr_unsigned(ndp->ni_cred->cr_gid);
815 	sp->sa_size = txdr_unsigned(VNOVAL);
816 	txdr_time(&vap->va_atime, &sp->sa_atime);		/* or VNOVAL ?? */
817 	txdr_time(&vap->va_mtime, &sp->sa_mtime);	/* or VNOVAL ?? */
818 	nfsm_request(ndp->ni_dvp);
819 	nfsm_reqdone;
820 	nfs_nput(ndp->ni_dvp);
821 	return (error);
822 }
823 
824 /*
825  * nfs make dir call
826  */
827 nfs_mkdir(ndp, vap)
828 	struct nameidata *ndp;
829 	struct vattr *vap;
830 {
831 	register struct nfsv2_sattr *sp;
832 	nfsm_vars;
833 
834 	nfsstats.rpccnt[NFSPROC_MKDIR]++;
835 	nfsm_reqhead(nfs_procids[NFSPROC_MKDIR], ndp->ni_cred,
836 	  NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen)+NFSX_SATTR);
837 	nfsm_fhtom(ndp->ni_dvp);
838 	nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN);
839 	nfsm_build(sp, struct nfsv2_sattr *, NFSX_SATTR);
840 	sp->sa_mode = vtonfs_mode(VDIR, vap->va_mode);
841 	sp->sa_uid = txdr_unsigned(ndp->ni_cred->cr_uid);
842 	sp->sa_gid = txdr_unsigned(ndp->ni_cred->cr_gid);
843 	sp->sa_size = txdr_unsigned(VNOVAL);
844 	txdr_time(&vap->va_atime, &sp->sa_atime);		/* or VNOVAL ?? */
845 	txdr_time(&vap->va_mtime, &sp->sa_mtime);	/* or VNOVAL ?? */
846 	nfsm_request(ndp->ni_dvp);
847 	nfsm_mtofh(ndp->ni_dvp, ndp->ni_vp);
848 	nfsm_reqdone;
849 	nfs_nput(ndp->ni_dvp);
850 	return (error);
851 }
852 
853 /*
854  * nfs remove directory call
855  */
856 nfs_rmdir(ndp)
857 	register struct nameidata *ndp;
858 {
859 	nfsm_vars;
860 
861 	if (ndp->ni_dvp == ndp->ni_vp) {
862 		vrele(ndp->ni_dvp);
863 		nfs_nput(ndp->ni_dvp);
864 		return (EINVAL);
865 	}
866 	nfsstats.rpccnt[NFSPROC_RMDIR]++;
867 	nfsm_reqhead(nfs_procids[NFSPROC_RMDIR], ndp->ni_cred,
868 		NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen));
869 	nfsm_fhtom(ndp->ni_dvp);
870 	nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN);
871 	nfsm_request(ndp->ni_dvp);
872 	nfsm_reqdone;
873 	cache_purge(ndp->ni_dvp);
874 	cache_purge(ndp->ni_vp);
875 	nfs_nput(ndp->ni_vp);
876 	nfs_nput(ndp->ni_dvp);
877 	return (error);
878 }
879 
880 /*
881  * nfs readdir call
882  * Although cookie is defined as opaque, I translate it to/from net byte
883  * order so that it looks more sensible. This appears consistent with the
884  * Ultrix implementation of NFS.
885  */
886 nfs_readdir(vp, uiop, offp, cred)
887 	struct vnode *vp;
888 	struct uio *uiop;
889 	off_t *offp;
890 	struct ucred *cred;
891 {
892 	register long len;
893 	register struct direct *dp;
894 	nfsm_vars;
895 	struct mbuf *md2;
896 	caddr_t dpos2;
897 	int siz;
898 	int more_dirs, eofflg;
899 	off_t off, savoff;
900 	struct direct *savdp;
901 
902 	nfs_lock(vp);
903 	nfsstats.rpccnt[NFSPROC_READDIR]++;
904 	nfsm_reqhead(nfs_procids[NFSPROC_READDIR], cred, xid);
905 	nfsm_fhtom(vp);
906 	nfsm_build(p, u_long *, 2*NFSX_UNSIGNED);
907 	off = *offp;
908 	*p++ = txdr_unsigned(off);
909 	*p = txdr_unsigned(uiop->uio_resid);
910 	nfsm_request(vp);
911 	siz = 0;
912 	nfsm_disect(p, u_long *, NFSX_UNSIGNED);
913 	more_dirs = fxdr_unsigned(int, *p);
914 
915 	/* Save the position so that we can do nfsm_mtouio() later */
916 	dpos2 = dpos;
917 	md2 = md;
918 
919 	/* loop thru the dir entries, doctoring them to 4bsd form */
920 	while (more_dirs && siz < uiop->uio_resid) {
921 		savoff = off;		/* Hold onto offset and dp */
922 		savdp = dp;
923 		nfsm_disecton(p, u_long *, 2*NFSX_UNSIGNED);
924 		dp = (struct direct *)p;
925 		dp->d_ino = fxdr_unsigned(u_long, *p++);
926 		len = fxdr_unsigned(int, *p);
927 		if (len <= 0 || len > NFS_MAXNAMLEN) {
928 			error = EBADRPC;
929 			m_freem(mrep);
930 			goto nfsmout;
931 		}
932 		dp->d_namlen = (u_short)len;
933 		len = nfsm_rndup(len);
934 		nfsm_adv(len);
935 		nfsm_disecton(p, u_long *, 2*NFSX_UNSIGNED);
936 		off = fxdr_unsigned(off_t, *p);
937 		*p++ = 0;		/* Ensures null termination of name */
938 		more_dirs = fxdr_unsigned(int, *p);
939 		dp->d_reclen = len+4*NFSX_UNSIGNED;
940 		siz += dp->d_reclen;
941 	}
942 	/*
943 	 * If at end of rpc data, get the eof boolean
944 	 */
945 	if (!more_dirs) {
946 		nfsm_disecton(p, u_long *, NFSX_UNSIGNED);
947 		eofflg = fxdr_unsigned(long, *p);
948 	}
949 	/*
950 	 * If there is too much to fit in the data buffer, use savoff and
951 	 * savdp to trim off the last record.
952 	 * --> we are not at eof
953 	 */
954 	if (siz > uiop->uio_resid) {
955 		eofflg = FALSE;
956 		off = savoff;
957 		siz -= dp->d_reclen;
958 		dp = savdp;
959 	}
960 	if (siz > 0) {
961 #ifdef notdef
962 		if (!eofflg)
963 			dp->d_reclen += (uiop->uio_resid-siz);
964 #endif
965 		md = md2;
966 		dpos = dpos2;
967 		nfsm_mtouio(uiop, siz);
968 #ifdef notdef
969 		if (!eofflg)
970 			uiop->uio_resid = 0;
971 #endif
972 		*offp = off;
973 	}
974 	nfsm_reqdone;
975 	nfs_unlock(vp);
976 	return (error);
977 }
978 
979 /*
980  * nfs statfs call
981  * (Actually a vfsop, not a vnode op)
982  */
983 nfs_statfs(mp, sbp)
984 	struct mount *mp;
985 	register struct statfs *sbp;
986 {
987 	register struct nfsv2_statfs *sfp;
988 	nfsm_vars;
989 	struct nfsmount *nmp;
990 	struct ucred *cred;
991 	struct nfsnode *np;
992 	struct vnode *vp;
993 
994 	nmp = vfs_to_nfs(mp);
995 	if (error = nfs_nget(mp, &nmp->nm_fh, &np))
996 		return (error);
997 	vp = NFSTOV(np);
998 	nfsstats.rpccnt[NFSPROC_STATFS]++;
999 	cred = crget();
1000 	cred->cr_ngroups = 1;
1001 	nfsm_reqhead(nfs_procids[NFSPROC_STATFS], cred, NFSX_FH);
1002 	nfsm_fhtom(vp);
1003 	nfsm_request(vp);
1004 	nfsm_disect(sfp, struct nfsv2_statfs *, NFSX_STATFS);
1005 	sbp->f_type = MOUNT_NFS;
1006 	sbp->f_flags = nmp->nm_flag;
1007 	sbp->f_bsize = fxdr_unsigned(long, sfp->sf_tsize);
1008 	sbp->f_fsize = fxdr_unsigned(long, sfp->sf_bsize);
1009 	sbp->f_blocks = fxdr_unsigned(long, sfp->sf_blocks);
1010 	sbp->f_bfree = fxdr_unsigned(long, sfp->sf_bfree);
1011 	sbp->f_bavail = fxdr_unsigned(long, sfp->sf_bavail);
1012 	sbp->f_files = 0;
1013 	sbp->f_ffree = 0;
1014 	sbp->f_fsid.val[0] = mp->m_fsid.val[0];
1015 	sbp->f_fsid.val[1] = mp->m_fsid.val[1];
1016 	bcopy(nmp->nm_path, sbp->f_mntonname, MNAMELEN);
1017 	bcopy(nmp->nm_host, sbp->f_mntfromname, MNAMELEN);
1018 	nfsm_reqdone;
1019 	nfs_nput(vp);
1020 	crfree(cred);
1021 	return (error);
1022 }
1023 
1024 #define	HEXTOASC(x)	"0123456789abcdef"[x]
1025 
1026 /*
1027  * Silly rename. To make the NFS filesystem that is stateless look a little
1028  * more like the "ufs" a remove of an active vnode is translated to a rename
1029  * to a funny looking filename that is removed by nfs_inactive on the
1030  * nfsnode. There is the potential for another process on a different client
1031  * to create the same funny name between the nfs_lookitup() fails and the
1032  * nfs_rename() completes, but...
1033  */
1034 nfs_sillyrename(ndp, flag)
1035 	struct nameidata *ndp;
1036 	int flag;
1037 {
1038 	register struct nfsnode *np;
1039 	register struct sillyrename *sp;
1040 	register struct nameidata *tndp;
1041 	int error;
1042 	short pid;
1043 
1044 	np = VTONFS(ndp->ni_dvp);
1045 	cache_purge(ndp->ni_dvp);
1046 	MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
1047 		M_TEMP, M_WAITOK);
1048 	sp->s_flag = flag;
1049 	bcopy((caddr_t)&np->n_fh, (caddr_t)&sp->s_fh, NFSX_FH);
1050 	np = VTONFS(ndp->ni_vp);
1051 	tndp = &sp->s_namei;
1052 	tndp->ni_cred = crdup(ndp->ni_cred);
1053 
1054 	/* Fudge together a funny name */
1055 	pid = u.u_procp->p_pid;
1056 	bcopy(".nfsAxxxx4.4", tndp->ni_dent.d_name, 13);
1057 	tndp->ni_dent.d_namlen = 12;
1058 	tndp->ni_dent.d_name[8] = HEXTOASC(pid & 0xf);
1059 	tndp->ni_dent.d_name[7] = HEXTOASC((pid >> 4) & 0xf);
1060 	tndp->ni_dent.d_name[6] = HEXTOASC((pid >> 8) & 0xf);
1061 	tndp->ni_dent.d_name[5] = HEXTOASC((pid >> 12) & 0xf);
1062 
1063 	/* Try lookitups until we get one that isn't there */
1064 	while (nfs_lookitup(ndp->ni_dvp, tndp, (nfsv2fh_t *)0) == 0) {
1065 		tndp->ni_dent.d_name[4]++;
1066 		if (tndp->ni_dent.d_name[4] > 'z') {
1067 			error = EINVAL;
1068 			goto bad;
1069 		}
1070 	}
1071 	if (error = nfs_renameit(ndp, tndp))
1072 		goto bad;
1073 	nfs_lookitup(ndp->ni_dvp, tndp, &np->n_fh);
1074 	np->n_sillyrename = sp;
1075 	return (0);
1076 bad:
1077 	crfree(tndp->ni_cred);
1078 	free((caddr_t)sp, M_TEMP);
1079 	return (error);
1080 }
1081 
1082 /*
1083  * Look up a file name for silly rename stuff.
1084  * Just like nfs_lookup() except that it doesn't load returned values
1085  * into the nfsnode table.
1086  * If fhp != NULL it copies the returned file handle out
1087  */
1088 nfs_lookitup(vp, ndp, fhp)
1089 	register struct vnode *vp;
1090 	register struct nameidata *ndp;
1091 	nfsv2fh_t *fhp;
1092 {
1093 	nfsm_vars;
1094 	long len;
1095 
1096 	nfsstats.rpccnt[NFSPROC_LOOKUP]++;
1097 	ndp->ni_dvp = vp;
1098 	ndp->ni_vp = NULL;
1099 	len = ndp->ni_dent.d_namlen;
1100 	nfsm_reqhead(nfs_procids[NFSPROC_LOOKUP], ndp->ni_cred, NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(len));
1101 	nfsm_fhtom(vp);
1102 	nfsm_strtom(ndp->ni_dent.d_name, len, NFS_MAXNAMLEN);
1103 	nfsm_request(vp);
1104 	if (fhp != NULL) {
1105 		nfsm_disect(cp, caddr_t, NFSX_FH);
1106 		bcopy(cp, (caddr_t)fhp, NFSX_FH);
1107 	}
1108 	nfsm_reqdone;
1109 	return (error);
1110 }
1111 
1112 /*
1113  * Kludge City..
1114  * - make nfs_bmap() essentially a no-op that does no translation
1115  * - do nfs_strategy() by faking physical I/O with nfs_readit/nfs_writeit
1116  *   after mapping the physical addresses into Kernel Virtual space in the
1117  *   nfsiobuf area.
1118  *   (Maybe I could use the process's page mapping, but I was concerned that
1119  *    Kernel Write might not be enabled and also figured copyout() would do
1120  *    a lot more work than bcopy() and also it currently happens in the
1121  *    context of the swapper process (2).
1122  */
1123 nfs_bmap(vp, bn, vpp, bnp)
1124 	struct vnode *vp;
1125 	daddr_t bn;
1126 	struct vnode **vpp;
1127 	daddr_t *bnp;
1128 {
1129 	if (vpp != NULL)
1130 		*vpp = vp;
1131 	if (bnp != NULL)
1132 		*bnp = bn * btodb(vp->v_mount->m_bsize);
1133 	return (0);
1134 }
1135 
1136 /*
1137  * Strategy routine for phys. i/o
1138  * If the biod's are running, queue a request
1139  * otherwise just call nfs_doio() to get it done
1140  */
1141 nfs_strategy(bp)
1142 	register struct buf *bp;
1143 {
1144 	register struct buf *dp;
1145 	register int i;
1146 	struct proc *rp;
1147 	int error = 0;
1148 	int fnd = 0;
1149 
1150 	/*
1151 	 * If an i/o daemon is waiting
1152 	 * queue the request, wake it up and wait for completion
1153 	 * otherwise just do it ourselves
1154 	 */
1155 	for (i = 0; i < nfs_asyncdaemons; i++) {
1156 		if (rp = nfs_iodwant[i]) {
1157 			/*
1158 			 * Ensure that the async_daemon is still waiting here
1159 			 */
1160 			if (rp->p_stat != SSLEEP ||
1161 			    rp->p_wchan != ((caddr_t)&nfs_iodwant[i])) {
1162 				nfs_iodwant[i] = (struct proc *)0;
1163 				continue;
1164 			}
1165 			dp = &nfs_bqueue;
1166 			if (dp->b_actf == NULL) {
1167 				dp->b_actl = bp;
1168 				bp->b_actf = dp;
1169 			} else {
1170 				dp->b_actf->b_actl = bp;
1171 				bp->b_actf = dp->b_actf;
1172 			}
1173 			dp->b_actf = bp;
1174 			bp->b_actl = dp;
1175 			fnd++;
1176 			nfs_iodwant[i] = (struct proc *)0;
1177 			wakeup((caddr_t)&nfs_iodwant[i]);
1178 			break;
1179 		}
1180 	}
1181 	if (!fnd)
1182 		error = nfs_doio(bp);
1183 	return (error);
1184 }
1185 
1186 /*
1187  * Fun and games with i/o
1188  * Essentially play ubasetup() and disk interrupt service routine by
1189  * mapping the data buffer into kernel virtual space and doing the
1190  * nfs read or write rpc's from it.
1191  * If the biod's are not running, this is just called from nfs_strategy(),
1192  * otherwise it is called by the biod's to do what would normally be
1193  * partially disk interrupt driven.
1194  */
1195 nfs_doio(bp)
1196 	register struct buf *bp;
1197 {
1198 	register struct pte *pte, *ppte;
1199 	register caddr_t vaddr;
1200 	register struct uio *uiop;
1201 	register struct vnode *vp;
1202 	struct nfsnode *np;
1203 	struct ucred *cr;
1204 	int npf, npf2;
1205 	int reg;
1206 	caddr_t vbase;
1207 	caddr_t addr;
1208 	unsigned v;
1209 	struct proc *rp;
1210 	int o, error;
1211 	int bcnt;
1212 	off_t off;
1213 	struct uio uio;
1214 	struct iovec io;
1215 
1216 	vp = bp->b_vp;
1217 	uiop = &uio;
1218 	uiop->uio_iov = &io;
1219 	uiop->uio_iovcnt = 1;
1220 	uiop->uio_segflg = UIO_SYSSPACE;
1221 	if (bp->b_flags & B_READ) {
1222 		io.iov_len = uiop->uio_resid = bp->b_bcount;
1223 		uiop->uio_offset = off = bp->b_blkno*DEV_BSIZE;
1224 		addr = bp->b_un.b_addr;
1225 		bcnt = bp->b_bcount;
1226 	} else {
1227 		io.iov_len = uiop->uio_resid = bp->b_dirtyend-bp->b_dirtyoff;
1228 		uiop->uio_offset = off = (bp->b_blkno*DEV_BSIZE)+bp->b_dirtyoff;
1229 		addr = bp->b_un.b_addr+bp->b_dirtyoff;
1230 		bcnt = bp->b_dirtyend-bp->b_dirtyoff;
1231 	}
1232 	/*
1233 	 * For phys i/o, map the b_addr into kernel virtual space using
1234 	 * the Nfsiomap pte's
1235 	 * Also, add a temporary b_rcred for reading using the process's uid
1236 	 * and a guess at a group
1237 	 */
1238 	if (bp->b_flags & B_PHYS) {
1239 		VTONFS(vp)->n_flag |= NPAGEDON;
1240 		bp->b_rcred = cr = crget();
1241 		rp = (bp->b_flags & B_DIRTY) ? &proc[2] : bp->b_proc;
1242 		cr->cr_uid = rp->p_uid;
1243 		cr->cr_gid = 0;		/* Anything ?? */
1244 		cr->cr_ngroups = 1;
1245 		o = (int)addr & PGOFSET;
1246 		npf2 = npf = btoc(bcnt + o);
1247 		/*
1248 		 * Get some mapping page table entries
1249 		 */
1250 		while ((reg = rmalloc(nfsmap, (long)npf)) == 0) {
1251 			nfsmap_want++;
1252 			sleep((caddr_t)&nfsmap_want, PZERO-1);
1253 		}
1254 		reg--;
1255 		/* I know it is always the else, but that may change someday */
1256 		if ((bp->b_flags & B_PHYS) == 0)
1257 			pte = kvtopte(bp->b_un.b_addr);
1258 		else if (bp->b_flags & B_PAGET)
1259 			pte = &Usrptmap[btokmx((struct pte *)bp->b_un.b_addr)];
1260 		else {
1261 			v = btop(bp->b_un.b_addr);
1262 			if (bp->b_flags & B_UAREA)
1263 				pte = &rp->p_addr[v];
1264 			else
1265 				pte = vtopte(rp, v);
1266 		}
1267 		/*
1268 		 * Play vmaccess() but with the Nfsiomap page table
1269 		 */
1270 		ppte = &Nfsiomap[reg];
1271 		vbase = vaddr = &nfsiobuf[reg*NBPG];
1272 		while (npf != 0) {
1273 			mapin(ppte, (u_int)vaddr, pte->pg_pfnum, (int)(PG_V|PG_KW));
1274 #if defined(tahoe)
1275 			mtpr(P1DC, vaddr);
1276 #endif
1277 			ppte++;
1278 			pte++;
1279 			vaddr += NBPG;
1280 			--npf;
1281 		}
1282 		io.iov_base = vbase+o;
1283 	} else {
1284 		io.iov_base = addr;
1285 	}
1286 	if (bp->b_flags & B_READ) {
1287 		uiop->uio_rw = UIO_READ;
1288 		bp->b_error = error = nfs_readrpc(vp, uiop, &off, bp->b_rcred);
1289 	} else {
1290 		uiop->uio_rw = UIO_WRITE;
1291 		bp->b_error = error = nfs_writerpc(vp, uiop, &off, bp->b_wcred);
1292 		if (error) {
1293 			np = VTONFS(vp);
1294 			np->n_error = error;
1295 			np->n_flag |= NWRITEERR;
1296 		}
1297 		bp->b_dirtyoff = bp->b_dirtyend = 0;
1298 	}
1299 	if (error)
1300 		bp->b_flags |= B_ERROR;
1301 	bp->b_resid = uiop->uio_resid;
1302 	/*
1303 	 * Release pte's used by physical i/o
1304 	 */
1305 	if (bp->b_flags & B_PHYS) {
1306 		crfree(cr);
1307 		rmfree(nfsmap, (long)npf2, (long)++reg);
1308 		if (nfsmap_want) {
1309 			nfsmap_want = 0;
1310 			wakeup((caddr_t)&nfsmap_want);
1311 		}
1312 	}
1313 	biodone(bp);
1314 	return (error);
1315 }
1316 
1317 /*
1318  * Flush all the blocks associated with a vnode.
1319  * 	Walk through the buffer pool and push any dirty pages
1320  *	associated with the vnode.
1321  */
1322 nfs_fsync(vp, fflags, cred)
1323 	register struct vnode *vp;
1324 	int fflags;
1325 	struct ucred *cred;
1326 {
1327 	register struct nfsnode *np = VTONFS(vp);
1328 	int error;
1329 
1330 	nfs_lock(vp);
1331 	if (np->n_flag & NMODIFIED) {
1332 		np->n_flag &= ~NMODIFIED;
1333 		error = nfs_blkflush(vp, (daddr_t)0, np->n_size, FALSE);
1334 	}
1335 	nfs_unlock(vp);
1336 	return (error);
1337 }
1338