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