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