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