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