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