xref: /csrg-svn/sys/nfs/nfs_vnops.c (revision 52317)
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  * %sccs.include.redist.c%
9  *
10  *	@(#)nfs_vnops.c	7.67 (Berkeley) 02/04/92
11  */
12 
13 /*
14  * vnode op calls for sun nfs version 2
15  */
16 
17 #include "param.h"
18 #include "proc.h"
19 #include "kernel.h"
20 #include "systm.h"
21 #include "mount.h"
22 #include "buf.h"
23 #include "malloc.h"
24 #include "mbuf.h"
25 #include "conf.h"
26 #include "namei.h"
27 #include "vnode.h"
28 #include "specdev.h"
29 #include "fifo.h"
30 #include "map.h"
31 
32 #include "ufs/ufs/quota.h"
33 #include "ufs/ufs/inode.h"
34 #include "ufs/ufs/dir.h"
35 
36 #include "rpcv2.h"
37 #include "nfsv2.h"
38 #include "nfs.h"
39 #include "nfsnode.h"
40 #include "nfsmount.h"
41 #include "xdr_subs.h"
42 #include "nfsm_subs.h"
43 #include "nqnfs.h"
44 
45 /* Defs */
46 #define	TRUE	1
47 #define	FALSE	0
48 
49 /*
50  * Global vfs data structures for nfs
51  */
52 struct vnodeops nfsv2_vnodeops = {
53 	nfs_lookup,		/* lookup */
54 	nfs_create,		/* create */
55 	nfs_mknod,		/* mknod */
56 	nfs_open,		/* open */
57 	nfs_close,		/* close */
58 	nfs_access,		/* access */
59 	nfs_getattr,		/* getattr */
60 	nfs_setattr,		/* setattr */
61 	nfs_read,		/* read */
62 	nfs_write,		/* write */
63 	nfs_ioctl,		/* ioctl */
64 	nfs_select,		/* select */
65 	nfs_mmap,		/* mmap */
66 	nfs_fsync,		/* fsync */
67 	nfs_seek,		/* seek */
68 	nfs_remove,		/* remove */
69 	nfs_link,		/* link */
70 	nfs_rename,		/* rename */
71 	nfs_mkdir,		/* mkdir */
72 	nfs_rmdir,		/* rmdir */
73 	nfs_symlink,		/* symlink */
74 	nfs_readdir,		/* readdir */
75 	nfs_readlink,		/* readlink */
76 	nfs_abortop,		/* abortop */
77 	nfs_inactive,		/* inactive */
78 	nfs_reclaim,		/* reclaim */
79 	nfs_lock,		/* lock */
80 	nfs_unlock,		/* unlock */
81 	nfs_bmap,		/* bmap */
82 	nfs_strategy,		/* strategy */
83 	nfs_print,		/* print */
84 	nfs_islocked,		/* islocked */
85 	nfs_advlock,		/* advlock */
86 	nfs_blkatoff,		/* blkatoff */
87 	nfs_vget,		/* vget */
88 	nfs_valloc,		/* valloc */
89 	nfs_vfree,		/* vfree */
90 	nfs_truncate,		/* truncate */
91 	nfs_update,		/* update */
92 	bwrite,			/* bwrite */
93 };
94 
95 /*
96  * Special device vnode ops
97  */
98 struct vnodeops spec_nfsv2nodeops = {
99 	spec_lookup,		/* lookup */
100 	spec_create,		/* create */
101 	spec_mknod,		/* mknod */
102 	spec_open,		/* open */
103 	spec_close,		/* close */
104 	nfs_access,		/* access */
105 	nfs_getattr,		/* getattr */
106 	nfs_setattr,		/* setattr */
107 	spec_read,		/* read */
108 	spec_write,		/* write */
109 	spec_ioctl,		/* ioctl */
110 	spec_select,		/* select */
111 	spec_mmap,		/* mmap */
112 	spec_fsync,		/* fsync */
113 	spec_seek,		/* seek */
114 	spec_remove,		/* remove */
115 	spec_link,		/* link */
116 	spec_rename,		/* rename */
117 	spec_mkdir,		/* mkdir */
118 	spec_rmdir,		/* rmdir */
119 	spec_symlink,		/* symlink */
120 	spec_readdir,		/* readdir */
121 	spec_readlink,		/* readlink */
122 	spec_abortop,		/* abortop */
123 	nfs_inactive,		/* inactive */
124 	nfs_reclaim,		/* reclaim */
125 	nfs_lock,		/* lock */
126 	nfs_unlock,		/* unlock */
127 	spec_bmap,		/* bmap */
128 	spec_strategy,		/* strategy */
129 	nfs_print,		/* print */
130 	nfs_islocked,		/* islocked */
131 	spec_advlock,		/* advlock */
132 	spec_blkatoff,		/* blkatoff */
133 	spec_vget,		/* vget */
134 	spec_valloc,		/* valloc */
135 	spec_vfree,		/* vfree */
136 	spec_truncate,		/* truncate */
137 	nfs_update,		/* update */
138 	bwrite,			/* bwrite */
139 };
140 
141 #ifdef FIFO
142 struct vnodeops fifo_nfsv2nodeops = {
143 	fifo_lookup,		/* lookup */
144 	fifo_create,		/* create */
145 	fifo_mknod,		/* mknod */
146 	fifo_open,		/* open */
147 	fifo_close,		/* close */
148 	nfs_access,		/* access */
149 	nfs_getattr,		/* getattr */
150 	nfs_setattr,		/* setattr */
151 	fifo_read,		/* read */
152 	fifo_write,		/* write */
153 	fifo_ioctl,		/* ioctl */
154 	fifo_select,		/* select */
155 	fifo_mmap,		/* mmap */
156 	fifo_fsync,		/* fsync */
157 	fifo_seek,		/* seek */
158 	fifo_remove,		/* remove */
159 	fifo_link,		/* link */
160 	fifo_rename,		/* rename */
161 	fifo_mkdir,		/* mkdir */
162 	fifo_rmdir,		/* rmdir */
163 	fifo_symlink,		/* symlink */
164 	fifo_readdir,		/* readdir */
165 	fifo_readlink,		/* readlink */
166 	fifo_abortop,		/* abortop */
167 	nfs_inactive,		/* inactive */
168 	nfs_reclaim,		/* reclaim */
169 	nfs_lock,		/* lock */
170 	nfs_unlock,		/* unlock */
171 	fifo_bmap,		/* bmap */
172 	fifo_badop,		/* strategy */
173 	nfs_print,		/* print */
174 	nfs_islocked,		/* islocked */
175 	fifo_advlock,		/* advlock */
176 	fifo_blkatoff,		/* blkatoff */
177 	fifo_vget,		/* vget */
178 	fifo_valloc,		/* valloc */
179 	fifo_vfree,		/* vfree */
180 	fifo_truncate,		/* truncate */
181 	nfs_update,		/* update */
182 	bwrite,			/* bwrite */
183 };
184 #endif /* FIFO */
185 
186 /*
187  * Global variables
188  */
189 extern u_long nfs_procids[NFS_NPROCS];
190 extern u_long nfs_prog, nfs_vers;
191 extern char nfsiobuf[MAXPHYS+NBPG];
192 struct buf nfs_bqueue;		/* Queue head for nfsiod's */
193 struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON];
194 int nfs_numasync = 0;
195 #define	DIRHDSIZ	(sizeof (struct direct) - (MAXNAMLEN + 1))
196 
197 /*
198  * nfs null call from vfs.
199  */
200 int
201 nfs_null(vp, cred, procp)
202 	struct vnode *vp;
203 	struct ucred *cred;
204 	struct proc *procp;
205 {
206 	caddr_t bpos, dpos;
207 	int error = 0;
208 	struct mbuf *mreq, *mrep, *md, *mb;
209 
210 	nfsm_reqhead(vp, NFSPROC_NULL, 0);
211 	nfsm_request(vp, NFSPROC_NULL, procp, cred);
212 	nfsm_reqdone;
213 	return (error);
214 }
215 
216 /*
217  * nfs access vnode op.
218  * Essentially just get vattr and then imitate iaccess()
219  */
220 int
221 nfs_access(vp, mode, cred, procp)
222 	struct vnode *vp;
223 	int mode;
224 	register struct ucred *cred;
225 	struct proc *procp;
226 {
227 	register struct vattr *vap;
228 	register gid_t *gp;
229 	struct vattr vattr;
230 	register int i;
231 	int error;
232 
233 	/*
234 	 * If you're the super-user,
235 	 * you always get access.
236 	 */
237 	if (cred->cr_uid == 0)
238 		return (0);
239 	vap = &vattr;
240 	if (error = nfs_getattr(vp, vap, cred, procp))
241 		return (error);
242 	/*
243 	 * Access check is based on only one of owner, group, public.
244 	 * If not owner, then check group. If not a member of the
245 	 * group, then check public access.
246 	 */
247 	if (cred->cr_uid != vap->va_uid) {
248 		mode >>= 3;
249 		gp = cred->cr_groups;
250 		for (i = 0; i < cred->cr_ngroups; i++, gp++)
251 			if (vap->va_gid == *gp)
252 				goto found;
253 		mode >>= 3;
254 found:
255 		;
256 	}
257 	if ((vap->va_mode & mode) != 0)
258 		return (0);
259 	return (EACCES);
260 }
261 
262 /*
263  * nfs open vnode op
264  * Just check to see if the type is ok
265  * and that deletion is not in progress.
266  */
267 /* ARGSUSED */
268 int
269 nfs_open(vp, mode, cred, procp)
270 	register struct vnode *vp;
271 	int mode;
272 	struct ucred *cred;
273 	struct proc *procp;
274 {
275 
276 	if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK)
277 		return (EACCES);
278 	if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) == 0)
279 		VTONFS(vp)->n_attrstamp = 0; /* For Open/Close consistency */
280 	return (0);
281 }
282 
283 /*
284  * nfs close vnode op
285  * For reg files, invalidate any buffer cache entries.
286  */
287 /* ARGSUSED */
288 int
289 nfs_close(vp, fflags, cred, procp)
290 	register struct vnode *vp;
291 	int fflags;
292 	struct ucred *cred;
293 	struct proc *procp;
294 {
295 	register struct nfsnode *np = VTONFS(vp);
296 	int error = 0;
297 
298 	if ((np->n_flag & NMODIFIED) &&
299 	    (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) == 0 &&
300 	    vp->v_type == VREG) {
301 		np->n_flag &= ~NMODIFIED;
302 		vinvalbuf(vp, TRUE);
303 		np->n_attrstamp = 0;
304 		if (np->n_flag & NWRITEERR) {
305 			np->n_flag &= ~NWRITEERR;
306 			error = np->n_error;
307 		}
308 	}
309 	return (error);
310 }
311 
312 /*
313  * nfs getattr call from vfs.
314  */
315 int
316 nfs_getattr(vp, vap, cred, procp)
317 	register struct vnode *vp;
318 	struct vattr *vap;
319 	struct ucred *cred;
320 	struct proc *procp;
321 {
322 	register caddr_t cp;
323 	caddr_t bpos, dpos;
324 	int error = 0;
325 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
326 
327 	/* First look in the cache.. */
328 	if (nfs_getattrcache(vp, vap) == 0)
329 		return (0);
330 	nfsstats.rpccnt[NFSPROC_GETATTR]++;
331 	nfsm_reqhead(vp, NFSPROC_GETATTR, NFSX_FH);
332 	nfsm_fhtom(vp);
333 	nfsm_request(vp, NFSPROC_GETATTR, procp, cred);
334 	nfsm_loadattr(vp, vap);
335 	nfsm_reqdone;
336 	return (error);
337 }
338 
339 /*
340  * nfs setattr call.
341  */
342 int
343 nfs_setattr(vp, vap, cred, procp)
344 	register struct vnode *vp;
345 	register struct vattr *vap;
346 	struct ucred *cred;
347 	struct proc *procp;
348 {
349 	register struct nfsv2_sattr *sp;
350 	register caddr_t cp;
351 	register long t1;
352 	caddr_t bpos, dpos, cp2;
353 	u_long *tl;
354 	int error = 0;
355 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
356 	struct nfsnode *np = VTONFS(vp);
357 	u_quad_t frev;
358 
359 	nfsstats.rpccnt[NFSPROC_SETATTR]++;
360 	nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH+NFSX_SATTR);
361 	nfsm_fhtom(vp);
362 	nfsm_build(sp, struct nfsv2_sattr *, NFSX_SATTR);
363 	if (vap->va_mode == 0xffff)
364 		sp->sa_mode = VNOVAL;
365 	else
366 		sp->sa_mode = vtonfs_mode(vp->v_type, vap->va_mode);
367 	if (vap->va_uid == 0xffff)
368 		sp->sa_uid = VNOVAL;
369 	else
370 		sp->sa_uid = txdr_unsigned(vap->va_uid);
371 	if (vap->va_gid == 0xffff)
372 		sp->sa_gid = VNOVAL;
373 	else
374 		sp->sa_gid = txdr_unsigned(vap->va_gid);
375 	sp->sa_size = txdr_unsigned(vap->va_size);
376 	sp->sa_atime.tv_sec = txdr_unsigned(vap->va_atime.tv_sec);
377 	sp->sa_atime.tv_usec = txdr_unsigned(vap->va_flags);
378 	txdr_time(&vap->va_mtime, &sp->sa_mtime);
379 	if (vap->va_size != VNOVAL || vap->va_mtime.tv_sec != VNOVAL ||
380 	    vap->va_atime.tv_sec != VNOVAL) {
381 		if (np->n_flag & NMODIFIED) {
382 			np->n_flag &= ~NMODIFIED;
383 			if (vap->va_size == 0)
384 				vinvalbuf(vp, FALSE);
385 			else
386 				vinvalbuf(vp, TRUE);
387 		}
388 	}
389 	nfsm_request(vp, NFSPROC_SETATTR, procp, cred);
390 	nfsm_loadattr(vp, (struct vattr *)0);
391 	if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) &&
392 	    NQNFS_CKCACHABLE(vp, NQL_WRITE)) {
393 		nfsm_dissect(tl, u_long *, 2*NFSX_UNSIGNED);
394 		fxdr_hyper(tl, &frev);
395 		if (QUADGT(frev, np->n_brev))
396 			np->n_brev = frev;
397 	}
398 	nfsm_reqdone;
399 	return (error);
400 }
401 
402 /*
403  * nfs lookup call, one step at a time...
404  * First look in cache
405  * If not found, unlock the directory nfsnode and do the rpc
406  */
407 int
408 nfs_lookup(dvp, vpp, cnp)
409 	struct vnode *dvp;
410 	struct vnode **vpp;
411 	struct componentname *cnp;
412 {
413 	register struct vnode *vdp;
414 	register u_long *tl;
415 	register caddr_t cp;
416 	register long t1, t2;
417 	struct nfsmount *nmp;
418 	struct nfsnode *tp;
419 	caddr_t bpos, dpos, cp2;
420 	time_t reqtime;
421 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
422 	struct vnode *newvp;
423 	long len;
424 	nfsv2fh_t *fhp;
425 	struct nfsnode *np;
426 	int lockparent, wantparent, error = 0;
427 	int nqlflag, cachable;
428 	u_quad_t frev;
429 
430 	*vpp = NULL;
431 	if (dvp->v_type != VDIR)
432 		return (ENOTDIR);
433 	lockparent = cnp->cn_flags & LOCKPARENT;
434 	wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
435 	nmp = VFSTONFS(dvp->v_mount);
436 	np = VTONFS(dvp);
437 	if ((error = cache_lookup(dvp, vpp, cnp)) && error != ENOENT) {
438 		struct vattr vattr;
439 		int vpid;
440 
441 		vdp = *vpp;
442 		vpid = vdp->v_id;
443 		/*
444 		 * See the comment starting `Step through' in ufs/ufs_lookup.c
445 		 * for an explanation of the locking protocol
446 		 */
447 		if (dvp == vdp) {
448 			VREF(vdp);
449 			error = 0;
450 		} else
451 			error = vget(vdp);
452 		if (!error) {
453 			if (vpid == vdp->v_id) {
454 			   if (nmp->nm_flag & NFSMNT_NQNFS) {
455 			        if (NQNFS_CKCACHABLE(dvp, NQL_READ)) {
456 					if (QUADNE(np->n_lrev, np->n_brev) ||
457 					    (np->n_flag & NMODIFIED)) {
458 						np->n_direofoffset = 0;
459 						cache_purge(dvp);
460 						np->n_flag &= ~NMODIFIED;
461 						vinvalbuf(dvp, FALSE);
462 						np->n_brev = np->n_lrev;
463 					} else {
464 						nfsstats.lookupcache_hits++;
465 						if (cnp->cn_nameiop != LOOKUP &&
466 						    (cnp->cn_flags&ISLASTCN))
467 						    cnp->cn_flags |= SAVENAME;
468 						return (0);
469 					}
470 				}
471 			   } else if (!nfs_getattr(vdp, &vattr, cnp->cn_cred, cnp->cn_proc) &&
472 			       vattr.va_ctime.tv_sec == VTONFS(vdp)->n_ctime) {
473 				nfsstats.lookupcache_hits++;
474 				if (cnp->cn_nameiop != LOOKUP && (cnp->cn_flags&ISLASTCN))
475 					cnp->cn_flags |= SAVENAME;
476 				return (0);
477 			   }
478 			   cache_purge(vdp);
479 			}
480 			vrele(vdp);
481 		}
482 		*vpp = NULLVP;
483 	}
484 	error = 0;
485 	nfsstats.lookupcache_misses++;
486 	nfsstats.rpccnt[NFSPROC_LOOKUP]++;
487 	len = cnp->cn_namelen;
488 	nfsm_reqhead(dvp, NFSPROC_LOOKUP, NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(len));
489 
490 	/*
491 	 * For nqnfs optionally piggyback a getlease request for the name
492 	 * being looked up.
493 	 */
494 	if (nmp->nm_flag & NFSMNT_NQNFS) {
495 		if ((nmp->nm_flag & NFSMNT_NQLOOKLEASE) &&
496 		    ((cnp->cn_flags&MAKEENTRY) && (cnp->cn_nameiop != DELETE || !(cnp->cn_flags&ISLASTCN)))) {
497 			nfsm_build(tl, u_long *, 2*NFSX_UNSIGNED);
498 			*tl++ = txdr_unsigned(NQL_READ);
499 			*tl = txdr_unsigned(nmp->nm_leaseterm);
500 		} else {
501 			nfsm_build(tl, u_long *, NFSX_UNSIGNED);
502 			*tl = 0;
503 		}
504 	}
505 	nfsm_fhtom(dvp);
506 	nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
507 	reqtime = time.tv_sec;
508 	nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_proc, cnp->cn_cred);
509 nfsmout:
510 	if (error) {
511 		if (cnp->cn_nameiop != LOOKUP && (cnp->cn_flags&ISLASTCN))
512 			cnp->cn_flags |= SAVENAME;
513 		return (error);
514 	}
515 	if (nmp->nm_flag & NFSMNT_NQNFS) {
516 		nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
517 		if (*tl) {
518 			nqlflag = fxdr_unsigned(int, *tl);
519 			nfsm_dissect(tl, u_long *, 4*NFSX_UNSIGNED);
520 			cachable = fxdr_unsigned(int, *tl++);
521 			reqtime += fxdr_unsigned(int, *tl++);
522 			fxdr_hyper(tl, &frev);
523 		} else
524 			nqlflag = 0;
525 	}
526 	nfsm_dissect(fhp, nfsv2fh_t *, NFSX_FH);
527 
528 	/*
529 	 * Handle RENAME case...
530 	 */
531 	if (cnp->cn_nameiop == RENAME && wantparent && (cnp->cn_flags&ISLASTCN)) {
532 		if (!bcmp(np->n_fh.fh_bytes, (caddr_t)fhp, NFSX_FH)) {
533 			m_freem(mrep);
534 			return (EISDIR);
535 		}
536 		if (error = nfs_nget(dvp->v_mount, fhp, &np)) {
537 			m_freem(mrep);
538 			return (error);
539 		}
540 		newvp = NFSTOV(np);
541 		if (error =
542 		    nfs_loadattrcache(&newvp, &md, &dpos, (struct vattr *)0)) {
543 			vrele(newvp);
544 			m_freem(mrep);
545 			return (error);
546 		}
547 		*vpp = newvp;
548 		m_freem(mrep);
549 		cnp->cn_flags |= SAVENAME;
550 		return (0);
551 	}
552 
553 	if (!bcmp(np->n_fh.fh_bytes, (caddr_t)fhp, NFSX_FH)) {
554 		VREF(dvp);
555 		newvp = dvp;
556 	} else {
557 		if (error = nfs_nget(dvp->v_mount, fhp, &np)) {
558 			m_freem(mrep);
559 			return (error);
560 		}
561 		newvp = NFSTOV(np);
562 	}
563 	if (error = nfs_loadattrcache(&newvp, &md, &dpos, (struct vattr *)0)) {
564 		vrele(newvp);
565 		m_freem(mrep);
566 		return (error);
567 	}
568 	m_freem(mrep);
569 	*vpp = newvp;
570 	if (cnp->cn_nameiop != LOOKUP && (cnp->cn_flags&ISLASTCN))
571 		cnp->cn_flags |= SAVENAME;
572 	if ((cnp->cn_flags&MAKEENTRY) && (cnp->cn_nameiop != DELETE || !(cnp->cn_flags&ISLASTCN))) {
573 		if ((nmp->nm_flag & NFSMNT_NQNFS) == 0)
574 			np->n_ctime = np->n_vattr.va_ctime.tv_sec;
575 		else if (nqlflag && reqtime > time.tv_sec) {
576 			if (np->n_tnext) {
577 				if (np->n_tnext == (struct nfsnode *)nmp)
578 					nmp->nm_tprev = np->n_tprev;
579 				else
580 					np->n_tnext->n_tprev = np->n_tprev;
581 				if (np->n_tprev == (struct nfsnode *)nmp)
582 					nmp->nm_tnext = np->n_tnext;
583 				else
584 					np->n_tprev->n_tnext = np->n_tnext;
585 				if (nqlflag == NQL_WRITE)
586 					np->n_flag |= NQNFSWRITE;
587 			} else if (nqlflag == NQL_READ)
588 				np->n_flag &= ~NQNFSWRITE;
589 			else
590 				np->n_flag |= NQNFSWRITE;
591 			if (cachable)
592 				np->n_flag &= ~NQNFSNONCACHE;
593 			else
594 				np->n_flag |= NQNFSNONCACHE;
595 			np->n_expiry = reqtime;
596 			np->n_lrev = frev;
597 			tp = nmp->nm_tprev;
598 			while (tp != (struct nfsnode *)nmp && tp->n_expiry > np->n_expiry)
599 				tp = tp->n_tprev;
600 			if (tp == (struct nfsnode *)nmp) {
601 				np->n_tnext = nmp->nm_tnext;
602 				nmp->nm_tnext = np;
603 			} else {
604 				np->n_tnext = tp->n_tnext;
605 				tp->n_tnext = np;
606 			}
607 			np->n_tprev = tp;
608 			if (np->n_tnext == (struct nfsnode *)nmp)
609 				nmp->nm_tprev = np;
610 			else
611 				np->n_tnext->n_tprev = np;
612 		}
613 		cache_enter(dvp, *vpp, cnp);
614 	}
615 	return (0);
616 }
617 
618 /*
619  * nfs read call.
620  * Just call nfs_bioread() to do the work.
621  */
622 int
623 nfs_read(vp, uiop, ioflag, cred)
624 	register struct vnode *vp;
625 	struct uio *uiop;
626 	int ioflag;
627 	struct ucred *cred;
628 {
629 	if (vp->v_type != VREG)
630 		return (EPERM);
631 	return (nfs_bioread(vp, uiop, ioflag, cred));
632 }
633 
634 /*
635  * nfs readlink call
636  */
637 int
638 nfs_readlink(vp, uiop, cred)
639 	struct vnode *vp;
640 	struct uio *uiop;
641 	struct ucred *cred;
642 {
643 	if (vp->v_type != VLNK)
644 		return (EPERM);
645 	return (nfs_bioread(vp, uiop, 0, cred));
646 }
647 
648 /*
649  * Do a readlink rpc.
650  * Called by nfs_doio() from below the buffer cache.
651  */
652 int
653 nfs_readlinkrpc(vp, uiop, cred)
654 	register struct vnode *vp;
655 	struct uio *uiop;
656 	struct ucred *cred;
657 {
658 	register u_long *tl;
659 	register caddr_t cp;
660 	register long t1;
661 	caddr_t bpos, dpos, cp2;
662 	int error = 0;
663 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
664 	long len;
665 
666 	nfsstats.rpccnt[NFSPROC_READLINK]++;
667 	nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH);
668 	nfsm_fhtom(vp);
669 	nfsm_request(vp, NFSPROC_READLINK, uiop->uio_procp, cred);
670 	nfsm_strsiz(len, NFS_MAXPATHLEN);
671 	nfsm_mtouio(uiop, len);
672 	nfsm_reqdone;
673 	return (error);
674 }
675 
676 /*
677  * nfs read rpc call
678  * Ditto above
679  */
680 int
681 nfs_readrpc(vp, uiop, cred)
682 	register struct vnode *vp;
683 	struct uio *uiop;
684 	struct ucred *cred;
685 {
686 	register u_long *tl;
687 	register caddr_t cp;
688 	register long t1;
689 	caddr_t bpos, dpos, cp2;
690 	int error = 0;
691 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
692 	struct nfsmount *nmp;
693 	long len, retlen, tsiz;
694 
695 	nmp = VFSTONFS(vp->v_mount);
696 	tsiz = uiop->uio_resid;
697 	while (tsiz > 0) {
698 		nfsstats.rpccnt[NFSPROC_READ]++;
699 		len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
700 		nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH+NFSX_UNSIGNED*3);
701 		nfsm_fhtom(vp);
702 		nfsm_build(tl, u_long *, NFSX_UNSIGNED*3);
703 		*tl++ = txdr_unsigned(uiop->uio_offset);
704 		*tl++ = txdr_unsigned(len);
705 		*tl = 0;
706 		nfsm_request(vp, NFSPROC_READ, uiop->uio_procp, cred);
707 		nfsm_loadattr(vp, (struct vattr *)0);
708 		nfsm_strsiz(retlen, nmp->nm_rsize);
709 		nfsm_mtouio(uiop, retlen);
710 		m_freem(mrep);
711 		if (retlen < len)
712 			tsiz = 0;
713 		else
714 			tsiz -= len;
715 	}
716 nfsmout:
717 	return (error);
718 }
719 
720 /*
721  * nfs write call
722  */
723 int
724 nfs_writerpc(vp, uiop, cred)
725 	register struct vnode *vp;
726 	struct uio *uiop;
727 	struct ucred *cred;
728 {
729 	register u_long *tl;
730 	register caddr_t cp;
731 	register long t1;
732 	caddr_t bpos, dpos, cp2;
733 	int error = 0;
734 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
735 	struct nfsmount *nmp;
736 	struct nfsnode *np = VTONFS(vp);
737 	u_quad_t frev;
738 	long len, tsiz;
739 
740 	nmp = VFSTONFS(vp->v_mount);
741 	tsiz = uiop->uio_resid;
742 	while (tsiz > 0) {
743 		nfsstats.rpccnt[NFSPROC_WRITE]++;
744 		len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
745 		nfsm_reqhead(vp, NFSPROC_WRITE,
746 			NFSX_FH+NFSX_UNSIGNED*4+nfsm_rndup(len));
747 		nfsm_fhtom(vp);
748 		nfsm_build(tl, u_long *, NFSX_UNSIGNED*4);
749 		*(tl+1) = txdr_unsigned(uiop->uio_offset);
750 		*(tl+3) = txdr_unsigned(len);
751 		nfsm_uiotom(uiop, len);
752 		nfsm_request(vp, NFSPROC_WRITE, uiop->uio_procp, cred);
753 		nfsm_loadattr(vp, (struct vattr *)0);
754 		if (nmp->nm_flag & NFSMNT_MYWRITE)
755 			VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr.va_mtime.tv_sec;
756 		else if ((nmp->nm_flag & NFSMNT_NQNFS) &&
757 			 NQNFS_CKCACHABLE(vp, NQL_WRITE)) {
758 			nfsm_dissect(tl, u_long *, 2*NFSX_UNSIGNED);
759 			fxdr_hyper(tl, &frev);
760 			if (QUADGT(frev, np->n_brev))
761 				np->n_brev = frev;
762 		}
763 		m_freem(mrep);
764 		tsiz -= len;
765 	}
766 nfsmout:
767 	if (error)
768 		uiop->uio_resid = tsiz;
769 	return (error);
770 }
771 
772 /*
773  * nfs mknod call
774  * This is a kludge. Use a create rpc but with the IFMT bits of the mode
775  * set to specify the file type and the size field for rdev.
776  */
777 /* ARGSUSED */
778 int
779 nfs_mknod(dvp, vpp, cnp, vap)
780 	struct vnode *dvp;
781 	struct vnode **vpp;
782 	struct componentname *cnp;
783 	struct vattr *vap;
784 {
785 	register struct nfsv2_sattr *sp;
786 	register u_long *tl;
787 	register caddr_t cp;
788 	register long t2;
789 	caddr_t bpos, dpos;
790 	int error = 0;
791 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
792 	u_long rdev;
793 
794 	if (vap->va_type == VCHR || vap->va_type == VBLK)
795 		rdev = txdr_unsigned(vap->va_rdev);
796 #ifdef FIFO
797 	else if (vap->va_type == VFIFO)
798 		rdev = 0xffffffff;
799 #endif /* FIFO */
800 	else {
801 		VOP_ABORTOP(dvp, cnp);
802 		vput(dvp);
803 		return (EOPNOTSUPP);
804 	}
805 	nfsstats.rpccnt[NFSPROC_CREATE]++;
806 	nfsm_reqhead(dvp, NFSPROC_CREATE,
807 	  NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(cnp->cn_namelen)+NFSX_SATTR);
808 	nfsm_fhtom(dvp);
809 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
810 	nfsm_build(sp, struct nfsv2_sattr *, NFSX_SATTR);
811 	sp->sa_mode = vtonfs_mode(vap->va_type, vap->va_mode);
812 	sp->sa_uid = txdr_unsigned(cnp->cn_cred->cr_uid);
813 	sp->sa_gid = txdr_unsigned(cnp->cn_cred->cr_gid);
814 	sp->sa_size = rdev;
815 	/* or should these be VNOVAL ?? */
816 	txdr_time(&vap->va_atime, &sp->sa_atime);
817 	txdr_time(&vap->va_mtime, &sp->sa_mtime);
818 	nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_proc, cnp->cn_cred);
819 	nfsm_reqdone;
820 	FREE(cnp->cn_pnbuf, M_NAMEI);
821 	VTONFS(dvp)->n_flag |= NMODIFIED;
822 	vrele(dvp);
823 	return (error);
824 }
825 
826 /*
827  * nfs file create call
828  */
829 int
830 nfs_create(dvp, vpp, cnp, vap)
831 	struct vnode *dvp;
832 	struct vnode **vpp;
833 	struct componentname *cnp;
834 	struct vattr *vap;
835 {
836 	register struct nfsv2_sattr *sp;
837 	register u_long *tl;
838 	register caddr_t cp;
839 	register long t1, t2;
840 	caddr_t bpos, dpos, cp2;
841 	int error = 0;
842 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
843 
844 	nfsstats.rpccnt[NFSPROC_CREATE]++;
845 	nfsm_reqhead(dvp, NFSPROC_CREATE,
846 	  NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(cnp->cn_namelen)+NFSX_SATTR);
847 	nfsm_fhtom(dvp);
848 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
849 	nfsm_build(sp, struct nfsv2_sattr *, NFSX_SATTR);
850 	sp->sa_mode = vtonfs_mode(vap->va_type, vap->va_mode);
851 	sp->sa_uid = txdr_unsigned(cnp->cn_cred->cr_uid);
852 	sp->sa_gid = txdr_unsigned(cnp->cn_cred->cr_gid);
853 	sp->sa_size = txdr_unsigned(0);
854 	/* or should these be VNOVAL ?? */
855 	txdr_time(&vap->va_atime, &sp->sa_atime);
856 	txdr_time(&vap->va_mtime, &sp->sa_mtime);
857 	nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_proc, cnp->cn_cred);
858 	nfsm_mtofh(dvp, *vpp);
859 	nfsm_reqdone;
860 	FREE(cnp->cn_pnbuf, M_NAMEI);
861 	VTONFS(dvp)->n_flag |= NMODIFIED;
862 	vrele(dvp);
863 	return (error);
864 }
865 
866 /*
867  * nfs file remove call
868  * To try and make nfs semantics closer to ufs semantics, a file that has
869  * other processes using the vnode is renamed instead of removed and then
870  * removed later on the last close.
871  * - If v_usecount > 1
872  *	  If a rename is not already in the works
873  *	     call nfs_sillyrename() to set it up
874  *     else
875  *	  do the remove rpc
876  */
877 int
878 nfs_remove(dvp, vp, cnp)
879 	struct vnode *dvp, *vp;
880 	struct componentname *cnp;
881 {
882 	register struct nfsnode *np = VTONFS(vp);
883 	register u_long *tl;
884 	register caddr_t cp;
885 	register long t2;
886 	caddr_t bpos, dpos;
887 	int error = 0;
888 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
889 
890 	if (vp->v_usecount > 1) {
891 		if (!np->n_sillyrename)
892 			error = nfs_sillyrename(dvp, vp, cnp);
893 	} else {
894 		/*
895 		 * Purge the name cache so that the chance of a lookup for
896 		 * the name succeeding while the remove is in progress is
897 		 * minimized. Without node locking it can still happen, such
898 		 * that an I/O op returns ESTALE, but since you get this if
899 		 * another host removes the file..
900 		 */
901 		cache_purge(vp);
902 		/*
903 		 * Throw away biocache buffers. Mainly to avoid
904 		 * unnecessary delayed writes.
905 		 */
906 		vinvalbuf(vp, FALSE);
907 		/* Do the rpc */
908 		nfsstats.rpccnt[NFSPROC_REMOVE]++;
909 		nfsm_reqhead(dvp, NFSPROC_REMOVE,
910 			NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(cnp->cn_namelen));
911 		nfsm_fhtom(dvp);
912 		nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
913 		nfsm_request(dvp, NFSPROC_REMOVE, cnp->cn_proc, cnp->cn_cred);
914 		nfsm_reqdone;
915 		FREE(cnp->cn_pnbuf, M_NAMEI);
916 		VTONFS(dvp)->n_flag |= NMODIFIED;
917 		/*
918 		 * Kludge City: If the first reply to the remove rpc is lost..
919 		 *   the reply to the retransmitted request will be ENOENT
920 		 *   since the file was in fact removed
921 		 *   Therefore, we cheat and return success.
922 		 */
923 		if (error == ENOENT)
924 			error = 0;
925 	}
926 	np->n_attrstamp = 0;
927 	vrele(dvp);
928 	vrele(vp);
929 	return (error);
930 }
931 
932 /*
933  * nfs file remove rpc called from nfs_inactive
934  */
935 int
936 nfs_removeit(sp, procp)
937 	register struct sillyrename *sp;
938 	struct proc *procp;
939 {
940 	register u_long *tl;
941 	register caddr_t cp;
942 	register long t2;
943 	caddr_t bpos, dpos;
944 	int error = 0;
945 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
946 
947 	nfsstats.rpccnt[NFSPROC_REMOVE]++;
948 	nfsm_reqhead(sp->s_dvp, NFSPROC_REMOVE,
949 		NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(sp->s_namlen));
950 	nfsm_fhtom(sp->s_dvp);
951 	nfsm_strtom(sp->s_name, sp->s_namlen, NFS_MAXNAMLEN);
952 	nfsm_request(sp->s_dvp, NFSPROC_REMOVE, procp, sp->s_cred);
953 	nfsm_reqdone;
954 	VTONFS(sp->s_dvp)->n_flag |= NMODIFIED;
955 	return (error);
956 }
957 
958 /*
959  * nfs file rename call
960  */
961 int
962 nfs_rename(fdvp, fvp, fcnp,
963 	   tdvp, tvp, tcnp)
964 	struct vnode *fdvp, *fvp;
965 	struct componentname *fcnp;
966 	struct vnode *tdvp, *tvp;
967 	struct componentname *tcnp;
968 {
969 	register u_long *tl;
970 	register caddr_t cp;
971 	register long t2;
972 	caddr_t bpos, dpos;
973 	int error = 0;
974 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
975 
976 	nfsstats.rpccnt[NFSPROC_RENAME]++;
977 	nfsm_reqhead(fdvp, NFSPROC_RENAME,
978 		(NFSX_FH+NFSX_UNSIGNED)*2+nfsm_rndup(fcnp->cn_namelen)+
979 		nfsm_rndup(fcnp->cn_namelen)); /* or fcnp->cn_cred?*/
980 	nfsm_fhtom(fdvp);
981 	nfsm_strtom(fcnp->cn_nameptr, fcnp->cn_namelen, NFS_MAXNAMLEN);
982 	nfsm_fhtom(tdvp);
983 	nfsm_strtom(tcnp->cn_nameptr, tcnp->cn_namelen, NFS_MAXNAMLEN);
984 	nfsm_request(fdvp, NFSPROC_RENAME, tcnp->cn_proc, tcnp->cn_cred);
985 	nfsm_reqdone;
986 	VTONFS(fdvp)->n_flag |= NMODIFIED;
987 	VTONFS(tdvp)->n_flag |= NMODIFIED;
988 	if (fvp->v_type == VDIR) {
989 		if (tvp != NULL && tvp->v_type == VDIR)
990 			cache_purge(tdvp);
991 		cache_purge(fdvp);
992 	}
993 	if (tdvp == tvp)
994 		vrele(tdvp);
995 	else
996 		vput(tdvp);
997 	if (tvp)
998 		vput(tvp);
999 	vrele(fdvp);
1000 	vrele(fvp);
1001 	/*
1002 	 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1003 	 */
1004 	if (error == ENOENT)
1005 		error = 0;
1006 	return (error);
1007 }
1008 
1009 /*
1010  * nfs file rename rpc called from nfs_remove() above
1011  */
1012 int
1013 nfs_renameit(sdvp, scnp, sp)
1014 	struct vnode *sdvp;
1015 	struct componentname *scnp;
1016 	register struct sillyrename *sp;
1017 {
1018 	register u_long *tl;
1019 	register caddr_t cp;
1020 	register long t2;
1021 	caddr_t bpos, dpos;
1022 	int error = 0;
1023 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1024 
1025 	nfsstats.rpccnt[NFSPROC_RENAME]++;
1026 	nfsm_reqhead(sdvp, NFSPROC_RENAME,
1027 		(NFSX_FH+NFSX_UNSIGNED)*2+nfsm_rndup(scnp->cn_namelen)+
1028 		nfsm_rndup(sp->s_namlen));
1029 	nfsm_fhtom(sdvp);
1030 	nfsm_strtom(scnp->cn_nameptr, scnp->cn_namelen, NFS_MAXNAMLEN);
1031 	nfsm_fhtom(sdvp);
1032 	nfsm_strtom(sp->s_name, sp->s_namlen, NFS_MAXNAMLEN);
1033 	nfsm_request(sdvp, NFSPROC_RENAME, scnp->cn_proc, scnp->cn_cred);
1034 	nfsm_reqdone;
1035 	FREE(scnp->cn_pnbuf, M_NAMEI);
1036 	VTONFS(sdvp)->n_flag |= NMODIFIED;
1037 	return (error);
1038 }
1039 
1040 /*
1041  * nfs hard link create call
1042  */
1043 int
1044 nfs_link(vp, tdvp, cnp)
1045 	register struct vnode *vp;   /* source vnode */
1046 	struct vnode *tdvp;
1047 	struct componentname *cnp;
1048 {
1049 	register u_long *tl;
1050 	register caddr_t cp;
1051 	register long t2;
1052 	caddr_t bpos, dpos;
1053 	int error = 0;
1054 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1055 
1056 	nfsstats.rpccnt[NFSPROC_LINK]++;
1057 	nfsm_reqhead(vp, NFSPROC_LINK,
1058 		NFSX_FH*2+NFSX_UNSIGNED+nfsm_rndup(cnp->cn_namelen));
1059 	nfsm_fhtom(vp);
1060 	nfsm_fhtom(tdvp);
1061 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1062 	nfsm_request(vp, NFSPROC_LINK, cnp->cn_proc, cnp->cn_cred);
1063 	nfsm_reqdone;
1064 	FREE(cnp->cn_pnbuf, M_NAMEI);
1065 	VTONFS(vp)->n_attrstamp = 0;
1066 	VTONFS(tdvp)->n_flag |= NMODIFIED;
1067 	vrele(tdvp);
1068 	/*
1069 	 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1070 	 */
1071 	if (error == EEXIST)
1072 		error = 0;
1073 	return (error);
1074 }
1075 
1076 /*
1077  * nfs symbolic link create call
1078  */
1079 /* start here */
1080 int
1081 nfs_symlink(dvp, vpp, cnp, vap, nm)
1082 	struct vnode *dvp;
1083 	struct vnode **vpp;
1084 	struct componentname *cnp;
1085 	struct vattr *vap;
1086 	char *nm;
1087 {
1088 	register struct nfsv2_sattr *sp;
1089 	register u_long *tl;
1090 	register caddr_t cp;
1091 	register long t2;
1092 	caddr_t bpos, dpos;
1093 	int slen, error = 0;
1094 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1095 
1096 	nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1097 	slen = strlen(nm);
1098 	nfsm_reqhead(dvp, NFSPROC_SYMLINK,
1099 	 NFSX_FH+2*NFSX_UNSIGNED+nfsm_rndup(cnp->cn_namelen)+nfsm_rndup(slen)+NFSX_SATTR);
1100 	nfsm_fhtom(dvp);
1101 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1102 	nfsm_strtom(nm, slen, NFS_MAXPATHLEN);
1103 	nfsm_build(sp, struct nfsv2_sattr *, NFSX_SATTR);
1104 	sp->sa_mode = vtonfs_mode(VLNK, vap->va_mode);
1105 	sp->sa_uid = txdr_unsigned(cnp->cn_cred->cr_uid);
1106 	sp->sa_gid = txdr_unsigned(cnp->cn_cred->cr_gid);
1107 	sp->sa_size = txdr_unsigned(VNOVAL);
1108 	txdr_time(&vap->va_atime, &sp->sa_atime);	/* or VNOVAL ?? */
1109 	txdr_time(&vap->va_mtime, &sp->sa_mtime);	/* or VNOVAL ?? */
1110 	nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_proc, cnp->cn_cred);
1111 	nfsm_reqdone;
1112 	FREE(cnp->cn_pnbuf, M_NAMEI);
1113 	VTONFS(dvp)->n_flag |= NMODIFIED;
1114 	vrele(dvp);
1115 	/*
1116 	 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1117 	 */
1118 	if (error == EEXIST)
1119 		error = 0;
1120 	return (error);
1121 }
1122 
1123 /*
1124  * nfs make dir call
1125  */
1126 int
1127 nfs_mkdir(dvp, vpp, cnp, vap)
1128 	struct vnode *dvp;
1129 	struct vnode **vpp;
1130 	struct componentname *cnp;
1131 	struct vattr *vap;
1132 {
1133 	register struct nfsv2_sattr *sp;
1134 	register u_long *tl;
1135 	register caddr_t cp;
1136 	register long t1, t2;
1137 	register int len;
1138 	caddr_t bpos, dpos, cp2;
1139 	int error = 0, firsttry = 1;
1140 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1141 
1142 	len = cnp->cn_namelen;
1143 	nfsstats.rpccnt[NFSPROC_MKDIR]++;
1144 	nfsm_reqhead(dvp, NFSPROC_MKDIR,
1145 	  NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(len)+NFSX_SATTR);
1146 	nfsm_fhtom(dvp);
1147 	nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
1148 	nfsm_build(sp, struct nfsv2_sattr *, NFSX_SATTR);
1149 	sp->sa_mode = vtonfs_mode(VDIR, vap->va_mode);
1150 	sp->sa_uid = txdr_unsigned(cnp->cn_cred->cr_uid);
1151 	sp->sa_gid = txdr_unsigned(cnp->cn_cred->cr_gid);
1152 	sp->sa_size = txdr_unsigned(VNOVAL);
1153 	txdr_time(&vap->va_atime, &sp->sa_atime);	/* or VNOVAL ?? */
1154 	txdr_time(&vap->va_mtime, &sp->sa_mtime);	/* or VNOVAL ?? */
1155 	nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_proc, cnp->cn_cred);
1156 	nfsm_mtofh(dvp, *vpp);
1157 	nfsm_reqdone;
1158 	VTONFS(dvp)->n_flag |= NMODIFIED;
1159 	/*
1160 	 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
1161 	 * if we can succeed in looking up the directory.
1162 	 * "firsttry" is necessary since the macros may "goto nfsmout" which
1163 	 * is above the if on errors. (Ugh)
1164 	 */
1165 	if (error == EEXIST && firsttry) {
1166 		firsttry = 0;
1167 		error = 0;
1168 		nfsstats.rpccnt[NFSPROC_LOOKUP]++;
1169 		*vpp = NULL;
1170 		nfsm_reqhead(dvp, NFSPROC_LOOKUP,
1171 		    NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(len));
1172 		nfsm_fhtom(dvp);
1173 		nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
1174 		nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_proc, cnp->cn_cred);
1175 		nfsm_mtofh(dvp, *vpp);
1176 		if ((*vpp)->v_type != VDIR) {
1177 			vput(*vpp);
1178 			error = EEXIST;
1179 		}
1180 		m_freem(mrep);
1181 	}
1182 	FREE(cnp->cn_pnbuf, M_NAMEI);
1183 	vrele(dvp);
1184 	return (error);
1185 }
1186 
1187 /*
1188  * nfs remove directory call
1189  */
1190 int
1191 nfs_rmdir(dvp, vp, cnp)
1192 	struct vnode *dvp, *vp;
1193 	struct componentname *cnp;
1194 {
1195 	register u_long *tl;
1196 	register caddr_t cp;
1197 	register long t2;
1198 	caddr_t bpos, dpos;
1199 	int error = 0;
1200 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1201 
1202 	if (dvp == vp) {
1203 		vrele(dvp);
1204 		vrele(dvp);
1205 		FREE(cnp->cn_pnbuf, M_NAMEI);
1206 		return (EINVAL);
1207 	}
1208 	nfsstats.rpccnt[NFSPROC_RMDIR]++;
1209 	nfsm_reqhead(dvp, NFSPROC_RMDIR,
1210 		NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(cnp->cn_namelen));
1211 	nfsm_fhtom(dvp);
1212 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1213 	nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_proc, cnp->cn_cred);
1214 	nfsm_reqdone;
1215 	FREE(cnp->cn_pnbuf, M_NAMEI);
1216 	VTONFS(dvp)->n_flag |= NMODIFIED;
1217 	cache_purge(dvp);
1218 	cache_purge(vp);
1219 	vrele(vp);
1220 	vrele(dvp);
1221 	/*
1222 	 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
1223 	 */
1224 	if (error == ENOENT)
1225 		error = 0;
1226 	return (error);
1227 }
1228 
1229 /*
1230  * nfs readdir call
1231  * Although cookie is defined as opaque, I translate it to/from net byte
1232  * order so that it looks more sensible. This appears consistent with the
1233  * Ultrix implementation of NFS.
1234  */
1235 int
1236 nfs_readdir(vp, uiop, cred, eofflagp)
1237 	register struct vnode *vp;
1238 	struct uio *uiop;
1239 	struct ucred *cred;
1240 	int *eofflagp;
1241 {
1242 	register struct nfsnode *np = VTONFS(vp);
1243 	int tresid, error;
1244 	struct vattr vattr;
1245 
1246 	if (vp->v_type != VDIR)
1247 		return (EPERM);
1248 	/*
1249 	 * First, check for hit on the EOF offset cache
1250 	 */
1251 	if (uiop->uio_offset != 0 && uiop->uio_offset == np->n_direofoffset &&
1252 	    (np->n_flag & NMODIFIED) == 0) {
1253 		if (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) {
1254 			if (NQNFS_CKCACHABLE(vp, NQL_READ)) {
1255 				*eofflagp = 1;
1256 				nfsstats.direofcache_hits++;
1257 				return (0);
1258 			}
1259 		} else if (nfs_getattr(vp, &vattr, cred, uiop->uio_procp) == 0 &&
1260 			np->n_mtime == vattr.va_mtime.tv_sec) {
1261 			*eofflagp = 1;
1262 			nfsstats.direofcache_hits++;
1263 			return (0);
1264 		}
1265 	}
1266 
1267 	/*
1268 	 * Call nfs_bioread() to do the real work.
1269 	 */
1270 	tresid = uiop->uio_resid;
1271 	error = nfs_bioread(vp, uiop, 0, cred);
1272 
1273 	if (!error && uiop->uio_resid == tresid) {
1274 		*eofflagp = 1;
1275 		nfsstats.direofcache_misses++;
1276 	} else
1277 		*eofflagp = 0;
1278 	return (error);
1279 }
1280 
1281 /*
1282  * Readdir rpc call.
1283  * Called from below the buffer cache by nfs_doio().
1284  */
1285 int
1286 nfs_readdirrpc(vp, uiop, cred)
1287 	register struct vnode *vp;
1288 	struct uio *uiop;
1289 	struct ucred *cred;
1290 {
1291 	register long len;
1292 	register struct direct *dp;
1293 	register u_long *tl;
1294 	register caddr_t cp;
1295 	register long t1;
1296 	long tlen, lastlen;
1297 	caddr_t bpos, dpos, cp2;
1298 	int error = 0;
1299 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1300 	struct mbuf *md2;
1301 	caddr_t dpos2;
1302 	int siz;
1303 	int more_dirs = 1;
1304 	off_t off, savoff;
1305 	struct direct *savdp;
1306 	struct nfsmount *nmp;
1307 	struct nfsnode *np = VTONFS(vp);
1308 	long tresid;
1309 
1310 	nmp = VFSTONFS(vp->v_mount);
1311 	tresid = uiop->uio_resid;
1312 	/*
1313 	 * Loop around doing readdir rpc's of size uio_resid or nm_rsize,
1314 	 * whichever is smaller, truncated to a multiple of NFS_DIRBLKSIZ.
1315 	 * The stopping criteria is EOF or buffer full.
1316 	 */
1317 	while (more_dirs && uiop->uio_resid >= NFS_DIRBLKSIZ) {
1318 		nfsstats.rpccnt[NFSPROC_READDIR]++;
1319 		nfsm_reqhead(vp, NFSPROC_READDIR,
1320 			NFSX_FH+2*NFSX_UNSIGNED);
1321 		nfsm_fhtom(vp);
1322 		nfsm_build(tl, u_long *, 2*NFSX_UNSIGNED);
1323 		*tl++ = txdr_unsigned(uiop->uio_offset);
1324 		*tl = txdr_unsigned(((uiop->uio_resid > nmp->nm_rsize) ?
1325 			nmp->nm_rsize : uiop->uio_resid) & ~(NFS_DIRBLKSIZ-1));
1326 		nfsm_request(vp, NFSPROC_READDIR, uiop->uio_procp, cred);
1327 		siz = 0;
1328 		nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
1329 		more_dirs = fxdr_unsigned(int, *tl);
1330 
1331 		/* Save the position so that we can do nfsm_mtouio() later */
1332 		dpos2 = dpos;
1333 		md2 = md;
1334 
1335 		/* loop thru the dir entries, doctoring them to 4bsd form */
1336 		off = uiop->uio_offset;
1337 #ifdef lint
1338 		dp = (struct direct *)0;
1339 #endif /* lint */
1340 		while (more_dirs && siz < uiop->uio_resid) {
1341 			savoff = off;		/* Hold onto offset and dp */
1342 			savdp = dp;
1343 			nfsm_dissecton(tl, u_long *, 2*NFSX_UNSIGNED);
1344 			dp = (struct direct *)tl;
1345 			dp->d_ino = fxdr_unsigned(u_long, *tl++);
1346 			len = fxdr_unsigned(int, *tl);
1347 			if (len <= 0 || len > NFS_MAXNAMLEN) {
1348 				error = EBADRPC;
1349 				m_freem(mrep);
1350 				goto nfsmout;
1351 			}
1352 			dp->d_namlen = (u_short)len;
1353 			nfsm_adv(len);		/* Point past name */
1354 			tlen = nfsm_rndup(len);
1355 			/*
1356 			 * This should not be necessary, but some servers have
1357 			 * broken XDR such that these bytes are not null filled.
1358 			 */
1359 			if (tlen != len) {
1360 				*dpos = '\0';	/* Null-terminate */
1361 				nfsm_adv(tlen - len);
1362 				len = tlen;
1363 			}
1364 			nfsm_dissecton(tl, u_long *, 2*NFSX_UNSIGNED);
1365 			off = fxdr_unsigned(off_t, *tl);
1366 			*tl++ = 0;	/* Ensures null termination of name */
1367 			more_dirs = fxdr_unsigned(int, *tl);
1368 			dp->d_reclen = len+4*NFSX_UNSIGNED;
1369 			siz += dp->d_reclen;
1370 		}
1371 		/*
1372 		 * If at end of rpc data, get the eof boolean
1373 		 */
1374 		if (!more_dirs) {
1375 			nfsm_dissecton(tl, u_long *, NFSX_UNSIGNED);
1376 			more_dirs = (fxdr_unsigned(int, *tl) == 0);
1377 
1378 			/*
1379 			 * If at EOF, cache directory offset
1380 			 */
1381 			if (!more_dirs)
1382 				np->n_direofoffset = off;
1383 		}
1384 		/*
1385 		 * If there is too much to fit in the data buffer, use savoff and
1386 		 * savdp to trim off the last record.
1387 		 * --> we are not at eof
1388 		 */
1389 		if (siz > uiop->uio_resid) {
1390 			off = savoff;
1391 			siz -= dp->d_reclen;
1392 			dp = savdp;
1393 			more_dirs = 0;	/* Paranoia */
1394 		}
1395 		if (siz > 0) {
1396 			lastlen = dp->d_reclen;
1397 			md = md2;
1398 			dpos = dpos2;
1399 			nfsm_mtouio(uiop, siz);
1400 			uiop->uio_offset = off;
1401 		} else
1402 			more_dirs = 0;	/* Ugh, never happens, but in case.. */
1403 		m_freem(mrep);
1404 	}
1405 	/*
1406 	 * Fill last record, iff any, out to a multiple of NFS_DIRBLKSIZ
1407 	 * by increasing d_reclen for the last record.
1408 	 */
1409 	if (uiop->uio_resid < tresid) {
1410 		len = uiop->uio_resid & (NFS_DIRBLKSIZ - 1);
1411 		if (len > 0) {
1412 			dp = (struct direct *)
1413 				(uiop->uio_iov->iov_base - lastlen);
1414 			dp->d_reclen += len;
1415 			uiop->uio_iov->iov_base += len;
1416 			uiop->uio_iov->iov_len -= len;
1417 			uiop->uio_resid -= len;
1418 		}
1419 	}
1420 nfsmout:
1421 	return (error);
1422 }
1423 
1424 /*
1425  * Nqnfs readdir_and_lookup RPC. Used in place of nfs_readdirrpc() when
1426  * the "rdirlook" mount option is specified.
1427  */
1428 int
1429 nfs_readdirlookrpc(vp, uiop, cred)
1430 	struct vnode *vp;
1431 	register struct uio *uiop;
1432 	struct ucred *cred;
1433 {
1434 	register int len;
1435 	register struct direct *dp;
1436 	register u_long *tl;
1437 	register caddr_t cp;
1438 	register long t1;
1439 	caddr_t bpos, dpos, cp2;
1440 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1441 	struct nameidata nami, *ndp = &nami;
1442 	struct componentname *cnp = &ndp->ni_cnd;
1443 	off_t off, endoff;
1444 	time_t reqtime, ltime;
1445 	struct nfsmount *nmp;
1446 	struct nfsnode *np, *tp;
1447 	struct vnode *newvp;
1448 	nfsv2fh_t *fhp;
1449 	u_long fileno;
1450 	u_quad_t frev;
1451 	int error = 0, tlen, more_dirs = 1, tresid, doit, bigenough, i;
1452 	int cachable;
1453 
1454 	if (uiop->uio_iovcnt != 1)
1455 		panic("nfs rdirlook");
1456 	nmp = VFSTONFS(vp->v_mount);
1457 	tresid = uiop->uio_resid;
1458 	ndp->ni_dvp = vp;
1459 	newvp = NULLVP;
1460 	/*
1461 	 * Loop around doing readdir rpc's of size uio_resid or nm_rsize,
1462 	 * whichever is smaller, truncated to a multiple of NFS_DIRBLKSIZ.
1463 	 * The stopping criteria is EOF or buffer full.
1464 	 */
1465 	while (more_dirs && uiop->uio_resid >= NFS_DIRBLKSIZ) {
1466 		nfsstats.rpccnt[NQNFSPROC_READDIRLOOK]++;
1467 		nfsm_reqhead(vp, NQNFSPROC_READDIRLOOK,
1468 			NFSX_FH+3*NFSX_UNSIGNED);
1469 		nfsm_fhtom(vp);
1470 		nfsm_build(tl, u_long *, 3*NFSX_UNSIGNED);
1471 		*tl++ = txdr_unsigned(uiop->uio_offset);
1472 		*tl++ = txdr_unsigned(((uiop->uio_resid > nmp->nm_rsize) ?
1473 			nmp->nm_rsize : uiop->uio_resid) & ~(NFS_DIRBLKSIZ-1));
1474 		*tl = txdr_unsigned(nmp->nm_leaseterm);
1475 		reqtime = time.tv_sec;
1476 		nfsm_request(vp, NQNFSPROC_READDIRLOOK, uiop->uio_procp, cred);
1477 		nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
1478 		more_dirs = fxdr_unsigned(int, *tl);
1479 
1480 		/* loop thru the dir entries, doctoring them to 4bsd form */
1481 		off = uiop->uio_offset;
1482 		bigenough = 1;
1483 		while (more_dirs && bigenough) {
1484 			doit = 1;
1485 			nfsm_dissect(tl, u_long *, 4*NFSX_UNSIGNED);
1486 			cachable = fxdr_unsigned(int, *tl++);
1487 			ltime = reqtime + fxdr_unsigned(int, *tl++);
1488 			fxdr_hyper(tl, &frev);
1489 			nfsm_dissect(fhp, nfsv2fh_t *, NFSX_FH);
1490 			if (!bcmp(VTONFS(vp)->n_fh.fh_bytes, (caddr_t)fhp, NFSX_FH)) {
1491 				VREF(vp);
1492 				newvp = vp;
1493 				np = VTONFS(vp);
1494 			} else {
1495 				if (error = nfs_nget(vp->v_mount, fhp, &np))
1496 					doit = 0;
1497 				newvp = NFSTOV(np);
1498 			}
1499 			if (error = nfs_loadattrcache(&newvp, &md, &dpos,
1500 				(struct vattr *)0))
1501 				doit = 0;
1502 			nfsm_dissect(tl, u_long *, 2*NFSX_UNSIGNED);
1503 			fileno = fxdr_unsigned(u_long, *tl++);
1504 			len = fxdr_unsigned(int, *tl);
1505 			if (len <= 0 || len > NFS_MAXNAMLEN) {
1506 				error = EBADRPC;
1507 				m_freem(mrep);
1508 				goto nfsmout;
1509 			}
1510 			tlen = (len + 4) & ~0x3;
1511 			if ((tlen + DIRHDSIZ) > uiop->uio_resid)
1512 				bigenough = 0;
1513 			if (bigenough && doit) {
1514 				dp = (struct direct *)uiop->uio_iov->iov_base;
1515 				dp->d_ino = fileno;
1516 				dp->d_namlen = len;
1517 				dp->d_reclen = tlen + DIRHDSIZ;
1518 				uiop->uio_resid -= DIRHDSIZ;
1519 				uiop->uio_iov->iov_base += DIRHDSIZ;
1520 				uiop->uio_iov->iov_len -= DIRHDSIZ;
1521 				cnp->cn_nameptr = uiop->uio_iov->iov_base;
1522 				cnp->cn_namelen = len;
1523 				ndp->ni_vp = newvp;
1524 				nfsm_mtouio(uiop, len);
1525 				cp = uiop->uio_iov->iov_base;
1526 				tlen -= len;
1527 				for (i = 0; i < tlen; i++)
1528 					*cp++ = '\0';
1529 				uiop->uio_iov->iov_base += tlen;
1530 				uiop->uio_iov->iov_len -= tlen;
1531 				uiop->uio_resid -= tlen;
1532 				cnp->cn_hash = 0;
1533 				for (cp = cnp->cn_nameptr, i = 1; i <= len; i++, cp++)
1534 					cnp->cn_hash += (unsigned char)*cp * i;
1535 				if (ltime > time.tv_sec) {
1536 					if (np->n_tnext) {
1537 						if (np->n_tnext == (struct nfsnode *)nmp)
1538 							nmp->nm_tprev = np->n_tprev;
1539 						else
1540 							np->n_tnext->n_tprev = np->n_tprev;
1541 						if (np->n_tprev == (struct nfsnode *)nmp)
1542 							nmp->nm_tnext = np->n_tnext;
1543 						else
1544 							np->n_tprev->n_tnext = np->n_tnext;
1545 					} else
1546 						np->n_flag &= ~NQNFSWRITE;
1547 					if (cachable)
1548 						np->n_flag &= ~NQNFSNONCACHE;
1549 					else
1550 						np->n_flag |= NQNFSNONCACHE;
1551 					np->n_expiry = ltime;
1552 					np->n_lrev = frev;
1553 					tp = nmp->nm_tprev;
1554 					while (tp != (struct nfsnode *)nmp && tp->n_expiry > np->n_expiry)
1555 						tp = tp->n_tprev;
1556 					if (tp == (struct nfsnode *)nmp) {
1557 						np->n_tnext = nmp->nm_tnext;
1558 						nmp->nm_tnext = np;
1559 					} else {
1560 						np->n_tnext = tp->n_tnext;
1561 						tp->n_tnext = np;
1562 					}
1563 					np->n_tprev = tp;
1564 					if (np->n_tnext == (struct nfsnode *)nmp)
1565 						nmp->nm_tprev = np;
1566 					else
1567 						np->n_tnext->n_tprev = np;
1568 					cache_enter(ndp->ni_dvp, ndp->ni_vp, cnp);
1569 				}
1570 			} else {
1571 				nfsm_adv(nfsm_rndup(len));
1572 			}
1573 			if (newvp != NULLVP) {
1574 				vrele(newvp);
1575 				newvp = NULLVP;
1576 			}
1577 			nfsm_dissect(tl, u_long *, 2*NFSX_UNSIGNED);
1578 			if (bigenough)
1579 				endoff = off = fxdr_unsigned(off_t, *tl++);
1580 			else
1581 				endoff = fxdr_unsigned(off_t, *tl++);
1582 			more_dirs = fxdr_unsigned(int, *tl);
1583 		}
1584 		/*
1585 		 * If at end of rpc data, get the eof boolean
1586 		 */
1587 		if (!more_dirs) {
1588 			nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
1589 			more_dirs = (fxdr_unsigned(int, *tl) == 0);
1590 
1591 			/*
1592 			 * If at EOF, cache directory offset
1593 			 */
1594 			if (!more_dirs)
1595 				VTONFS(vp)->n_direofoffset = endoff;
1596 		}
1597 		if (uiop->uio_resid < tresid)
1598 			uiop->uio_offset = off;
1599 		else
1600 			more_dirs = 0;
1601 		m_freem(mrep);
1602 	}
1603 	/*
1604 	 * Fill last record, iff any, out to a multiple of NFS_DIRBLKSIZ
1605 	 * by increasing d_reclen for the last record.
1606 	 */
1607 	if (uiop->uio_resid < tresid) {
1608 		len = uiop->uio_resid & (NFS_DIRBLKSIZ - 1);
1609 		if (len > 0) {
1610 			dp->d_reclen += len;
1611 			uiop->uio_iov->iov_base += len;
1612 			uiop->uio_iov->iov_len -= len;
1613 			uiop->uio_resid -= len;
1614 		}
1615 	}
1616 nfsmout:
1617 	if (newvp != NULLVP)
1618 		vrele(newvp);
1619 	return (error);
1620 }
1621 static char hextoasc[] = "0123456789abcdef";
1622 
1623 /*
1624  * Silly rename. To make the NFS filesystem that is stateless look a little
1625  * more like the "ufs" a remove of an active vnode is translated to a rename
1626  * to a funny looking filename that is removed by nfs_inactive on the
1627  * nfsnode. There is the potential for another process on a different client
1628  * to create the same funny name between the nfs_lookitup() fails and the
1629  * nfs_rename() completes, but...
1630  */
1631 int
1632 nfs_sillyrename(dvp, vp, cnp)
1633 	struct vnode *dvp, *vp;
1634 	struct componentname *cnp;
1635 {
1636 	register struct nfsnode *np;
1637 	register struct sillyrename *sp;
1638 	int error;
1639 	short pid;
1640 
1641 	cache_purge(dvp);
1642 	np = VTONFS(vp);
1643 #ifdef SILLYSEPARATE
1644 	MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
1645 		M_NFSREQ, M_WAITOK);
1646 #else
1647 	sp = &np->n_silly;
1648 #endif
1649 	sp->s_cred = crdup(cnp->cn_cred);
1650 	sp->s_dvp = dvp;
1651 	VREF(dvp);
1652 
1653 	/* Fudge together a funny name */
1654 	pid = cnp->cn_proc->p_pid;
1655 	bcopy(".nfsAxxxx4.4", sp->s_name, 13);
1656 	sp->s_namlen = 12;
1657 	sp->s_name[8] = hextoasc[pid & 0xf];
1658 	sp->s_name[7] = hextoasc[(pid >> 4) & 0xf];
1659 	sp->s_name[6] = hextoasc[(pid >> 8) & 0xf];
1660 	sp->s_name[5] = hextoasc[(pid >> 12) & 0xf];
1661 
1662 	/* Try lookitups until we get one that isn't there */
1663 	while (nfs_lookitup(sp, (nfsv2fh_t *)0, cnp->cn_proc) == 0) {
1664 		sp->s_name[4]++;
1665 		if (sp->s_name[4] > 'z') {
1666 			error = EINVAL;
1667 			goto bad;
1668 		}
1669 	}
1670 	if (error = nfs_renameit(dvp, cnp, sp))
1671 		goto bad;
1672 	nfs_lookitup(sp, &np->n_fh, cnp->cn_proc);
1673 	np->n_sillyrename = sp;
1674 	return (0);
1675 bad:
1676 	vrele(sp->s_dvp);
1677 	crfree(sp->s_cred);
1678 #ifdef SILLYSEPARATE
1679 	free((caddr_t)sp, M_NFSREQ);
1680 #endif
1681 	return (error);
1682 }
1683 
1684 /*
1685  * Look up a file name for silly rename stuff.
1686  * Just like nfs_lookup() except that it doesn't load returned values
1687  * into the nfsnode table.
1688  * If fhp != NULL it copies the returned file handle out
1689  */
1690 int
1691 nfs_lookitup(sp, fhp, procp)
1692 	register struct sillyrename *sp;
1693 	nfsv2fh_t *fhp;
1694 	struct proc *procp;
1695 {
1696 	register struct vnode *vp = sp->s_dvp;
1697 	register u_long *tl;
1698 	register caddr_t cp;
1699 	register long t1, t2;
1700 	caddr_t bpos, dpos, cp2;
1701 	u_long xid;
1702 	int error = 0;
1703 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1704 	long len;
1705 
1706 	nfsstats.rpccnt[NFSPROC_LOOKUP]++;
1707 	len = sp->s_namlen;
1708 	nfsm_reqhead(vp, NFSPROC_LOOKUP, NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(len));
1709 	nfsm_fhtom(vp);
1710 	nfsm_strtom(sp->s_name, len, NFS_MAXNAMLEN);
1711 	nfsm_request(vp, NFSPROC_LOOKUP, procp, sp->s_cred);
1712 	if (fhp != NULL) {
1713 		nfsm_dissect(cp, caddr_t, NFSX_FH);
1714 		bcopy(cp, (caddr_t)fhp, NFSX_FH);
1715 	}
1716 	nfsm_reqdone;
1717 	return (error);
1718 }
1719 
1720 /*
1721  * Kludge City..
1722  * - make nfs_bmap() essentially a no-op that does no translation
1723  * - do nfs_strategy() by faking physical I/O with nfs_readrpc/nfs_writerpc
1724  *   after mapping the physical addresses into Kernel Virtual space in the
1725  *   nfsiobuf area.
1726  *   (Maybe I could use the process's page mapping, but I was concerned that
1727  *    Kernel Write might not be enabled and also figured copyout() would do
1728  *    a lot more work than bcopy() and also it currently happens in the
1729  *    context of the swapper process (2).
1730  */
1731 int
1732 nfs_bmap(vp, bn, vpp, bnp)
1733 	struct vnode *vp;
1734 	daddr_t bn;
1735 	struct vnode **vpp;
1736 	daddr_t *bnp;
1737 {
1738 	if (vpp != NULL)
1739 		*vpp = vp;
1740 	if (bnp != NULL)
1741 		*bnp = bn * btodb(vp->v_mount->mnt_stat.f_iosize);
1742 	return (0);
1743 }
1744 
1745 /*
1746  * Strategy routine for phys. i/o
1747  * If the biod's are running, queue a request
1748  * otherwise just call nfs_doio() to get it done
1749  */
1750 int
1751 nfs_strategy(bp)
1752 	register struct buf *bp;
1753 {
1754 	register struct buf *dp;
1755 	register int i;
1756 	int error = 0;
1757 	int fnd = 0;
1758 
1759 	/*
1760 	 * Set b_proc. It seems a bit silly to do it here, but since bread()
1761 	 * doesn't set it, I will.
1762 	 * Set b_proc == NULL for asynchronous ops, since these may still
1763 	 * be hanging about after the process terminates.
1764 	 */
1765 	if ((bp->b_flags & B_PHYS) == 0) {
1766 		if (bp->b_flags & B_ASYNC)
1767 			bp->b_proc = (struct proc *)0;
1768 		else
1769 			bp->b_proc = curproc;
1770 	}
1771 	/*
1772 	 * If the op is asynchronous and an i/o daemon is waiting
1773 	 * queue the request, wake it up and wait for completion
1774 	 * otherwise just do it ourselves.
1775 	 */
1776 	if ((bp->b_flags & B_ASYNC) == 0 || nfs_numasync == 0)
1777 		return (nfs_doio(bp));
1778 	for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
1779 		if (nfs_iodwant[i]) {
1780 			dp = &nfs_bqueue;
1781 			if (dp->b_actf == NULL) {
1782 				dp->b_actl = bp;
1783 				bp->b_actf = dp;
1784 			} else {
1785 				dp->b_actf->b_actl = bp;
1786 				bp->b_actf = dp->b_actf;
1787 			}
1788 			dp->b_actf = bp;
1789 			bp->b_actl = dp;
1790 			fnd++;
1791 			wakeup((caddr_t)&nfs_iodwant[i]);
1792 			break;
1793 		}
1794 	}
1795 	if (!fnd)
1796 		error = nfs_doio(bp);
1797 	return (error);
1798 }
1799 
1800 /*
1801  * Fun and games with i/o
1802  * Essentially play ubasetup() and disk interrupt service routine by
1803  * mapping the data buffer into kernel virtual space and doing the
1804  * nfs read or write rpc's from it.
1805  * If the nfsiod's are not running, this is just called from nfs_strategy(),
1806  * otherwise it is called by the nfsiods to do what would normally be
1807  * partially disk interrupt driven.
1808  */
1809 int
1810 nfs_doio(bp)
1811 	register struct buf *bp;
1812 {
1813 	register struct uio *uiop;
1814 	register struct vnode *vp;
1815 	struct nfsnode *np;
1816 	struct ucred *cr;
1817 	int error;
1818 	struct uio uio;
1819 	struct iovec io;
1820 
1821 	vp = bp->b_vp;
1822 	np = VTONFS(vp);
1823 	uiop = &uio;
1824 	uiop->uio_iov = &io;
1825 	uiop->uio_iovcnt = 1;
1826 	uiop->uio_segflg = UIO_SYSSPACE;
1827 	uiop->uio_procp = bp->b_proc;
1828 
1829 	/*
1830 	 * For phys i/o, map the b_addr into kernel virtual space using
1831 	 * the Nfsiomap pte's
1832 	 * Also, add a temporary b_rcred for reading using the process's uid
1833 	 * and a guess at a group
1834 	 */
1835 	if (bp->b_flags & B_PHYS) {
1836 		if (bp->b_flags & B_DIRTY)
1837 			uiop->uio_procp = pageproc;
1838 		cr = crcopy(uiop->uio_procp->p_ucred);
1839 		/* mapping was already done by vmapbuf */
1840 		io.iov_base = bp->b_un.b_addr;
1841 
1842 		/*
1843 		 * And do the i/o rpc
1844 		 */
1845 		io.iov_len = uiop->uio_resid = bp->b_bcount;
1846 		uiop->uio_offset = bp->b_blkno * DEV_BSIZE;
1847 		if (bp->b_flags & B_READ) {
1848 			uiop->uio_rw = UIO_READ;
1849 			nfsstats.read_physios++;
1850 			bp->b_error = error = nfs_readrpc(vp, uiop, cr);
1851 			(void) vnode_pager_uncache(vp);
1852 		} else {
1853 			uiop->uio_rw = UIO_WRITE;
1854 			nfsstats.write_physios++;
1855 			bp->b_error = error = nfs_writerpc(vp, uiop, cr);
1856 		}
1857 
1858 		/*
1859 		 * Finally, release pte's used by physical i/o
1860 		 */
1861 		crfree(cr);
1862 	} else {
1863 		if (bp->b_flags & B_READ) {
1864 			io.iov_len = uiop->uio_resid = bp->b_bcount;
1865 			io.iov_base = bp->b_un.b_addr;
1866 			uiop->uio_rw = UIO_READ;
1867 			switch (vp->v_type) {
1868 			case VREG:
1869 				uiop->uio_offset = bp->b_blkno * DEV_BSIZE;
1870 				nfsstats.read_bios++;
1871 				error = nfs_readrpc(vp, uiop, bp->b_rcred);
1872 				break;
1873 			case VLNK:
1874 				uiop->uio_offset = 0;
1875 				nfsstats.readlink_bios++;
1876 				error = nfs_readlinkrpc(vp, uiop, bp->b_rcred);
1877 				break;
1878 			case VDIR:
1879 				uiop->uio_offset = bp->b_lblkno;
1880 				nfsstats.readdir_bios++;
1881 				if (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_RDIRALOOK)
1882 				    error = nfs_readdirlookrpc(vp, uiop, bp->b_rcred);
1883 				else
1884 				    error = nfs_readdirrpc(vp, uiop, bp->b_rcred);
1885 				/*
1886 				 * Save offset cookie in b_blkno.
1887 				 */
1888 				bp->b_blkno = uiop->uio_offset;
1889 				break;
1890 			};
1891 			bp->b_error = error;
1892 		} else {
1893 			io.iov_len = uiop->uio_resid = bp->b_dirtyend
1894 				- bp->b_dirtyoff;
1895 			uiop->uio_offset = (bp->b_blkno * DEV_BSIZE)
1896 				+ bp->b_dirtyoff;
1897 			io.iov_base = bp->b_un.b_addr + bp->b_dirtyoff;
1898 			uiop->uio_rw = UIO_WRITE;
1899 			nfsstats.write_bios++;
1900 			bp->b_error = error = nfs_writerpc(vp, uiop,
1901 				bp->b_wcred);
1902 			if (error) {
1903 				np->n_error = error;
1904 				np->n_flag |= NWRITEERR;
1905 			}
1906 			bp->b_dirtyoff = bp->b_dirtyend = 0;
1907 		}
1908 	}
1909 	if (error)
1910 		bp->b_flags |= B_ERROR;
1911 	bp->b_resid = uiop->uio_resid;
1912 	biodone(bp);
1913 	return (error);
1914 }
1915 
1916 /*
1917  * Mmap a file
1918  *
1919  * NB Currently unsupported.
1920  */
1921 /* ARGSUSED */
1922 int
1923 nfs_mmap(vp, fflags, cred, p)
1924 	struct vnode *vp;
1925 	int fflags;
1926 	struct ucred *cred;
1927 	struct proc *p;
1928 {
1929 
1930 	return (EINVAL);
1931 }
1932 
1933 /*
1934  * Flush all the blocks associated with a vnode.
1935  * 	Walk through the buffer pool and push any dirty pages
1936  *	associated with the vnode.
1937  */
1938 /* ARGSUSED */
1939 int
1940 nfs_fsync(vp, fflags, cred, waitfor, p)
1941 	register struct vnode *vp;
1942 	int fflags;
1943 	struct ucred *cred;
1944 	int waitfor;
1945 	struct proc *p;
1946 {
1947 	register struct nfsnode *np = VTONFS(vp);
1948 	int error = 0;
1949 
1950 	if (np->n_flag & NMODIFIED) {
1951 		np->n_flag &= ~NMODIFIED;
1952 		vflushbuf(vp, waitfor == MNT_WAIT ? B_SYNC : 0);
1953 	}
1954 	if (!error && (np->n_flag & NWRITEERR))
1955 		error = np->n_error;
1956 	return (error);
1957 }
1958 
1959 /*
1960  * NFS advisory byte-level locks.
1961  * Currently unsupported.
1962  */
1963 int
1964 nfs_advlock(vp, id, op, fl, flags)
1965 	struct vnode *vp;
1966 	caddr_t id;
1967 	int op;
1968 	struct flock *fl;
1969 	int flags;
1970 {
1971 
1972 	return (EOPNOTSUPP);
1973 }
1974 
1975 /*
1976  * Print out the contents of an nfsnode.
1977  */
1978 int
1979 nfs_print(vp)
1980 	struct vnode *vp;
1981 {
1982 	register struct nfsnode *np = VTONFS(vp);
1983 
1984 	printf("tag VT_NFS, fileid %d fsid 0x%x",
1985 		np->n_vattr.va_fileid, np->n_vattr.va_fsid);
1986 #ifdef FIFO
1987 	if (vp->v_type == VFIFO)
1988 		fifo_printinfo(vp);
1989 #endif /* FIFO */
1990 	printf("\n");
1991 }
1992 
1993 /*
1994  * NFS directory offset lookup.
1995  * Currently unsupported.
1996  */
1997 int
1998 nfs_blkatoff(vp, offset, res, bpp)
1999 	struct vnode *vp;
2000 	off_t offset;
2001 	char **res;
2002 	struct buf **bpp;
2003 {
2004 
2005 	return (EOPNOTSUPP);
2006 }
2007 
2008 /*
2009  * NFS flat namespace lookup.
2010  * Currently unsupported.
2011  */
2012 int
2013 nfs_vget(mp, ino, vpp)
2014 	struct mount *mp;
2015 	ino_t ino;
2016 	struct vnode **vpp;
2017 {
2018 
2019 	return (EOPNOTSUPP);
2020 }
2021 
2022 /*
2023  * NFS flat namespace allocation.
2024  * Currently unsupported.
2025  */
2026 int
2027 nfs_valloc(pvp, mode, cred, vpp)
2028 	struct vnode *pvp;
2029 	int mode;
2030 	struct ucred *cred;
2031 	struct vnode **vpp;
2032 {
2033 
2034 	return (EOPNOTSUPP);
2035 }
2036 
2037 /*
2038  * NFS flat namespace free.
2039  * Currently unsupported.
2040  */
2041 void
2042 nfs_vfree(pvp, ino, mode)
2043 	struct vnode *pvp;
2044 	ino_t ino;
2045 	int mode;
2046 {
2047 
2048 	return;
2049 }
2050 
2051 /*
2052  * NFS file truncation.
2053  */
2054 int
2055 nfs_truncate(vp, length, flags)
2056 	struct vnode *vp;
2057 	u_long length;
2058 	int flags;
2059 {
2060 
2061 	/* Use nfs_setattr */
2062 	printf("nfs_truncate: need to implement!!");
2063 	return (EOPNOTSUPP);
2064 }
2065 
2066 /*
2067  * NFS update.
2068  */
2069 int
2070 nfs_update(vp, ta, tm, waitfor)
2071 	struct vnode *vp;
2072 	struct timeval *ta;
2073 	struct timeval *tm;
2074 	int waitfor;
2075 {
2076 
2077 	/* Use nfs_setattr */
2078 	printf("nfs_update: need to implement!!");
2079 	return (EOPNOTSUPP);
2080 }
2081