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