xref: /csrg-svn/sys/nfs/nfs_vnops.c (revision 53513)
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.73 (Berkeley) 05/14/92
11  */
12 
13 /*
14  * vnode op calls for sun nfs version 2
15  */
16 
17 #include <sys/param.h>
18 #include <sys/proc.h>
19 #include <sys/kernel.h>
20 #include <sys/systm.h>
21 #include <sys/mount.h>
22 #include <sys/buf.h>
23 #include <sys/malloc.h>
24 #include <sys/mbuf.h>
25 #include <sys/conf.h>
26 #include <sys/namei.h>
27 #include <sys/vnode.h>
28 #include <sys/specdev.h>
29 #include <sys/fifo.h>
30 #include <sys/map.h>
31 
32 #include <vm/vm.h>
33 
34 #include <nfs/rpcv2.h>
35 #include <nfs/nfsv2.h>
36 #include <nfs/nfs.h>
37 #include <nfs/nfsnode.h>
38 #include <nfs/nfsmount.h>
39 #include <nfs/xdr_subs.h>
40 #include <nfs/nfsm_subs.h>
41 #include <nfs/nqnfs.h>
42 
43 /* Defs */
44 #define	TRUE	1
45 #define	FALSE	0
46 
47 /*
48  * Global vfs data structures for nfs
49  */
50 struct vnodeops nfsv2_vnodeops = {
51 	nfs_lookup,		/* lookup */
52 	nfs_create,		/* create */
53 	nfs_mknod,		/* mknod */
54 	nfs_open,		/* open */
55 	nfs_close,		/* close */
56 	nfs_access,		/* access */
57 	nfs_getattr,		/* getattr */
58 	nfs_setattr,		/* setattr */
59 	nfs_read,		/* read */
60 	nfs_write,		/* write */
61 	nfs_ioctl,		/* ioctl */
62 	nfs_select,		/* select */
63 	nfs_mmap,		/* mmap */
64 	nfs_fsync,		/* fsync */
65 	nfs_seek,		/* seek */
66 	nfs_remove,		/* remove */
67 	nfs_link,		/* link */
68 	nfs_rename,		/* rename */
69 	nfs_mkdir,		/* mkdir */
70 	nfs_rmdir,		/* rmdir */
71 	nfs_symlink,		/* symlink */
72 	nfs_readdir,		/* readdir */
73 	nfs_readlink,		/* readlink */
74 	nfs_abortop,		/* abortop */
75 	nfs_inactive,		/* inactive */
76 	nfs_reclaim,		/* reclaim */
77 	nfs_lock,		/* lock */
78 	nfs_unlock,		/* unlock */
79 	nfs_bmap,		/* bmap */
80 	nfs_strategy,		/* strategy */
81 	nfs_print,		/* print */
82 	nfs_islocked,		/* islocked */
83 	nfs_advlock,		/* advlock */
84 	nfs_blkatoff,		/* blkatoff */
85 	nfs_vget,		/* vget */
86 	nfs_valloc,		/* valloc */
87 	nfs_vfree,		/* vfree */
88 	nfs_truncate,		/* truncate */
89 	nfs_update,		/* update */
90 	bwrite,			/* bwrite */
91 };
92 
93 /*
94  * Special device vnode ops
95  */
96 struct vnodeops spec_nfsv2nodeops = {
97 	spec_lookup,		/* lookup */
98 	spec_create,		/* create */
99 	spec_mknod,		/* mknod */
100 	spec_open,		/* open */
101 	spec_close,		/* close */
102 	nfs_access,		/* access */
103 	nfs_getattr,		/* getattr */
104 	nfs_setattr,		/* setattr */
105 	spec_read,		/* read */
106 	spec_write,		/* write */
107 	spec_ioctl,		/* ioctl */
108 	spec_select,		/* select */
109 	spec_mmap,		/* mmap */
110 	spec_fsync,		/* fsync */
111 	spec_seek,		/* seek */
112 	spec_remove,		/* remove */
113 	spec_link,		/* link */
114 	spec_rename,		/* rename */
115 	spec_mkdir,		/* mkdir */
116 	spec_rmdir,		/* rmdir */
117 	spec_symlink,		/* symlink */
118 	spec_readdir,		/* readdir */
119 	spec_readlink,		/* readlink */
120 	spec_abortop,		/* abortop */
121 	nfs_inactive,		/* inactive */
122 	nfs_reclaim,		/* reclaim */
123 	nfs_lock,		/* lock */
124 	nfs_unlock,		/* unlock */
125 	spec_bmap,		/* bmap */
126 	spec_strategy,		/* strategy */
127 	nfs_print,		/* print */
128 	nfs_islocked,		/* islocked */
129 	spec_advlock,		/* advlock */
130 	spec_blkatoff,		/* blkatoff */
131 	spec_vget,		/* vget */
132 	spec_valloc,		/* valloc */
133 	spec_vfree,		/* vfree */
134 	spec_truncate,		/* truncate */
135 	nfs_update,		/* update */
136 	bwrite,			/* bwrite */
137 };
138 
139 #ifdef FIFO
140 struct vnodeops fifo_nfsv2nodeops = {
141 	fifo_lookup,		/* lookup */
142 	fifo_create,		/* create */
143 	fifo_mknod,		/* mknod */
144 	fifo_open,		/* open */
145 	fifo_close,		/* close */
146 	nfs_access,		/* access */
147 	nfs_getattr,		/* getattr */
148 	nfs_setattr,		/* setattr */
149 	fifo_read,		/* read */
150 	fifo_write,		/* write */
151 	fifo_ioctl,		/* ioctl */
152 	fifo_select,		/* select */
153 	fifo_mmap,		/* mmap */
154 	fifo_fsync,		/* fsync */
155 	fifo_seek,		/* seek */
156 	fifo_remove,		/* remove */
157 	fifo_link,		/* link */
158 	fifo_rename,		/* rename */
159 	fifo_mkdir,		/* mkdir */
160 	fifo_rmdir,		/* rmdir */
161 	fifo_symlink,		/* symlink */
162 	fifo_readdir,		/* readdir */
163 	fifo_readlink,		/* readlink */
164 	fifo_abortop,		/* abortop */
165 	nfs_inactive,		/* inactive */
166 	nfs_reclaim,		/* reclaim */
167 	nfs_lock,		/* lock */
168 	nfs_unlock,		/* unlock */
169 	fifo_bmap,		/* bmap */
170 	fifo_badop,		/* strategy */
171 	nfs_print,		/* print */
172 	nfs_islocked,		/* islocked */
173 	fifo_advlock,		/* advlock */
174 	fifo_blkatoff,		/* blkatoff */
175 	fifo_vget,		/* vget */
176 	fifo_valloc,		/* valloc */
177 	fifo_vfree,		/* vfree */
178 	fifo_truncate,		/* truncate */
179 	nfs_update,		/* update */
180 	bwrite,			/* bwrite */
181 };
182 #endif /* FIFO */
183 
184 /*
185  * Global variables
186  */
187 extern u_long nfs_procids[NFS_NPROCS];
188 extern u_long nfs_prog, nfs_vers;
189 extern char nfsiobuf[MAXPHYS+NBPG];
190 struct buf nfs_bqueue;		/* Queue head for nfsiod's */
191 struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON];
192 int nfs_numasync = 0;
193 #define	DIRHDSIZ	(sizeof (struct readdir) - (MAXNAMLEN + 1))
194 
195 /*
196  * nfs null call from vfs.
197  */
198 int
199 nfs_null(vp, cred, procp)
200 	struct vnode *vp;
201 	struct ucred *cred;
202 	struct proc *procp;
203 {
204 	caddr_t bpos, dpos;
205 	int error = 0;
206 	struct mbuf *mreq, *mrep, *md, *mb;
207 
208 	nfsm_reqhead(vp, NFSPROC_NULL, 0);
209 	nfsm_request(vp, NFSPROC_NULL, procp, cred);
210 	nfsm_reqdone;
211 	return (error);
212 }
213 
214 /*
215  * nfs access vnode op.
216  * Essentially just get vattr and then imitate iaccess()
217  */
218 int
219 nfs_access(vp, mode, cred, procp)
220 	struct vnode *vp;
221 	int mode;
222 	register struct ucred *cred;
223 	struct proc *procp;
224 {
225 	register struct vattr *vap;
226 	register gid_t *gp;
227 	struct vattr vattr;
228 	register int i;
229 	int error;
230 
231 	/*
232 	 * If you're the super-user,
233 	 * you always get access.
234 	 */
235 	if (cred->cr_uid == 0)
236 		return (0);
237 	vap = &vattr;
238 	if (error = nfs_getattr(vp, vap, cred, procp))
239 		return (error);
240 	/*
241 	 * Access check is based on only one of owner, group, public.
242 	 * If not owner, then check group. If not a member of the
243 	 * group, then check public access.
244 	 */
245 	if (cred->cr_uid != vap->va_uid) {
246 		mode >>= 3;
247 		gp = cred->cr_groups;
248 		for (i = 0; i < cred->cr_ngroups; i++, gp++)
249 			if (vap->va_gid == *gp)
250 				goto found;
251 		mode >>= 3;
252 found:
253 		;
254 	}
255 	if ((vap->va_mode & mode) != 0)
256 		return (0);
257 	return (EACCES);
258 }
259 
260 /*
261  * nfs open vnode op
262  * Just check to see if the type is ok
263  * and that deletion is not in progress.
264  */
265 /* ARGSUSED */
266 int
267 nfs_open(vp, mode, cred, procp)
268 	register struct vnode *vp;
269 	int mode;
270 	struct ucred *cred;
271 	struct proc *procp;
272 {
273 
274 	if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK)
275 		return (EACCES);
276 	if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) == 0)
277 		VTONFS(vp)->n_attrstamp = 0; /* For Open/Close consistency */
278 	return (0);
279 }
280 
281 /*
282  * nfs close vnode op
283  * For reg files, invalidate any buffer cache entries.
284  */
285 /* ARGSUSED */
286 int
287 nfs_close(vp, fflags, cred, procp)
288 	register struct vnode *vp;
289 	int fflags;
290 	struct ucred *cred;
291 	struct proc *procp;
292 {
293 	register struct nfsnode *np = VTONFS(vp);
294 	int error = 0;
295 
296 	if ((np->n_flag & NMODIFIED) &&
297 	    (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) == 0 &&
298 	    vp->v_type == VREG) {
299 		np->n_flag &= ~NMODIFIED;
300 		vinvalbuf(vp, TRUE);
301 		np->n_attrstamp = 0;
302 		if (np->n_flag & NWRITEERR) {
303 			np->n_flag &= ~NWRITEERR;
304 			error = np->n_error;
305 		}
306 	}
307 	return (error);
308 }
309 
310 /*
311  * nfs getattr call from vfs.
312  */
313 int
314 nfs_getattr(vp, vap, cred, procp)
315 	register struct vnode *vp;
316 	struct vattr *vap;
317 	struct ucred *cred;
318 	struct proc *procp;
319 {
320 	register caddr_t cp;
321 	caddr_t bpos, dpos;
322 	int error = 0;
323 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
324 
325 	/* First look in the cache.. */
326 	if (nfs_getattrcache(vp, vap) == 0)
327 		return (0);
328 	nfsstats.rpccnt[NFSPROC_GETATTR]++;
329 	nfsm_reqhead(vp, NFSPROC_GETATTR, NFSX_FH);
330 	nfsm_fhtom(vp);
331 	nfsm_request(vp, NFSPROC_GETATTR, procp, cred);
332 	nfsm_loadattr(vp, vap);
333 	nfsm_reqdone;
334 	return (error);
335 }
336 
337 /*
338  * nfs setattr call.
339  */
340 int
341 nfs_setattr(vp, vap, cred, procp)
342 	register struct vnode *vp;
343 	register struct vattr *vap;
344 	struct ucred *cred;
345 	struct proc *procp;
346 {
347 	register struct nfsv2_sattr *sp;
348 	register caddr_t cp;
349 	register long t1;
350 	caddr_t bpos, dpos, cp2;
351 	u_long *tl;
352 	int error = 0;
353 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
354 	struct nfsnode *np = VTONFS(vp);
355 	u_quad_t frev;
356 
357 	nfsstats.rpccnt[NFSPROC_SETATTR]++;
358 	nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH+NFSX_SATTR);
359 	nfsm_fhtom(vp);
360 	nfsm_build(sp, struct nfsv2_sattr *, NFSX_SATTR);
361 	if (vap->va_mode == 0xffff)
362 		sp->sa_mode = VNOVAL;
363 	else
364 		sp->sa_mode = vtonfs_mode(vp->v_type, vap->va_mode);
365 	if (vap->va_uid == 0xffff)
366 		sp->sa_uid = VNOVAL;
367 	else
368 		sp->sa_uid = txdr_unsigned(vap->va_uid);
369 	if (vap->va_gid == 0xffff)
370 		sp->sa_gid = VNOVAL;
371 	else
372 		sp->sa_gid = txdr_unsigned(vap->va_gid);
373 	sp->sa_size = txdr_unsigned(vap->va_size);
374 	sp->sa_atime.tv_sec = txdr_unsigned(vap->va_atime.tv_sec);
375 	sp->sa_atime.tv_usec = txdr_unsigned(vap->va_flags);
376 	txdr_time(&vap->va_mtime, &sp->sa_mtime);
377 	if (vap->va_size != VNOVAL || vap->va_mtime.tv_sec != VNOVAL ||
378 	    vap->va_atime.tv_sec != VNOVAL) {
379 		if (np->n_flag & NMODIFIED) {
380 			np->n_flag &= ~NMODIFIED;
381 			if (vap->va_size == 0)
382 				vinvalbuf(vp, FALSE);
383 			else
384 				vinvalbuf(vp, TRUE);
385 		}
386 	}
387 	nfsm_request(vp, NFSPROC_SETATTR, procp, cred);
388 	nfsm_loadattr(vp, (struct vattr *)0);
389 	if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) &&
390 	    NQNFS_CKCACHABLE(vp, NQL_WRITE)) {
391 		nfsm_dissect(tl, u_long *, 2*NFSX_UNSIGNED);
392 		fxdr_hyper(tl, &frev);
393 		if (QUADGT(frev, np->n_brev))
394 			np->n_brev = frev;
395 	}
396 	nfsm_reqdone;
397 	return (error);
398 }
399 
400 /*
401  * nfs lookup call, one step at a time...
402  * First look in cache
403  * If not found, unlock the directory nfsnode and do the rpc
404  */
405 int
406 nfs_lookup(dvp, vpp, cnp)
407 	struct vnode *dvp;
408 	struct vnode **vpp;
409 	struct componentname *cnp;
410 {
411 	register struct vnode *vdp;
412 	register u_long *tl;
413 	register caddr_t cp;
414 	register long t1, t2;
415 	struct nfsmount *nmp;
416 	struct nfsnode *tp;
417 	caddr_t bpos, dpos, cp2;
418 	time_t reqtime;
419 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
420 	struct vnode *newvp;
421 	long len;
422 	nfsv2fh_t *fhp;
423 	struct nfsnode *np;
424 	int lockparent, wantparent, error = 0;
425 	int nqlflag, cachable;
426 	u_quad_t frev;
427 
428 	*vpp = NULL;
429 	if (dvp->v_type != VDIR)
430 		return (ENOTDIR);
431 	lockparent = cnp->cn_flags & LOCKPARENT;
432 	wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
433 	nmp = VFSTONFS(dvp->v_mount);
434 	np = VTONFS(dvp);
435 	if ((error = cache_lookup(dvp, vpp, cnp)) && error != ENOENT) {
436 		struct vattr vattr;
437 		int vpid;
438 
439 		vdp = *vpp;
440 		vpid = vdp->v_id;
441 		/*
442 		 * See the comment starting `Step through' in ufs/ufs_lookup.c
443 		 * for an explanation of the locking protocol
444 		 */
445 		if (dvp == vdp) {
446 			VREF(vdp);
447 			error = 0;
448 		} else
449 			error = vget(vdp);
450 		if (!error) {
451 			if (vpid == vdp->v_id) {
452 			   if (nmp->nm_flag & NFSMNT_NQNFS) {
453 			        if (NQNFS_CKCACHABLE(dvp, NQL_READ)) {
454 					if (QUADNE(np->n_lrev, np->n_brev) ||
455 					    (np->n_flag & NMODIFIED)) {
456 						np->n_direofoffset = 0;
457 						cache_purge(dvp);
458 						np->n_flag &= ~NMODIFIED;
459 						vinvalbuf(dvp, FALSE);
460 						np->n_brev = np->n_lrev;
461 					} else {
462 						nfsstats.lookupcache_hits++;
463 						if (cnp->cn_nameiop != LOOKUP &&
464 						    (cnp->cn_flags&ISLASTCN))
465 						    cnp->cn_flags |= SAVENAME;
466 						return (0);
467 					}
468 				}
469 			   } else if (!nfs_getattr(vdp, &vattr, cnp->cn_cred, cnp->cn_proc) &&
470 			       vattr.va_ctime.tv_sec == VTONFS(vdp)->n_ctime) {
471 				nfsstats.lookupcache_hits++;
472 				if (cnp->cn_nameiop != LOOKUP && (cnp->cn_flags&ISLASTCN))
473 					cnp->cn_flags |= SAVENAME;
474 				return (0);
475 			   }
476 			   cache_purge(vdp);
477 			}
478 			vrele(vdp);
479 		}
480 		*vpp = NULLVP;
481 	}
482 	error = 0;
483 	nfsstats.lookupcache_misses++;
484 	nfsstats.rpccnt[NFSPROC_LOOKUP]++;
485 	len = cnp->cn_namelen;
486 	nfsm_reqhead(dvp, NFSPROC_LOOKUP, NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(len));
487 
488 	/*
489 	 * For nqnfs optionally piggyback a getlease request for the name
490 	 * being looked up.
491 	 */
492 	if (nmp->nm_flag & NFSMNT_NQNFS) {
493 		if ((nmp->nm_flag & NFSMNT_NQLOOKLEASE) &&
494 		    ((cnp->cn_flags&MAKEENTRY) && (cnp->cn_nameiop != DELETE || !(cnp->cn_flags&ISLASTCN)))) {
495 			nfsm_build(tl, u_long *, 2*NFSX_UNSIGNED);
496 			*tl++ = txdr_unsigned(NQL_READ);
497 			*tl = txdr_unsigned(nmp->nm_leaseterm);
498 		} else {
499 			nfsm_build(tl, u_long *, NFSX_UNSIGNED);
500 			*tl = 0;
501 		}
502 	}
503 	nfsm_fhtom(dvp);
504 	nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
505 	reqtime = time.tv_sec;
506 	nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_proc, cnp->cn_cred);
507 nfsmout:
508 	if (error) {
509 		if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
510 		    (cnp->cn_flags & ISLASTCN) && error == ENOENT)
511 			error = EJUSTRETURN;
512 		if (cnp->cn_nameiop != LOOKUP && (cnp->cn_flags&ISLASTCN))
513 			cnp->cn_flags |= SAVENAME;
514 		return (error);
515 	}
516 	if (nmp->nm_flag & NFSMNT_NQNFS) {
517 		nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
518 		if (*tl) {
519 			nqlflag = fxdr_unsigned(int, *tl);
520 			nfsm_dissect(tl, u_long *, 4*NFSX_UNSIGNED);
521 			cachable = fxdr_unsigned(int, *tl++);
522 			reqtime += fxdr_unsigned(int, *tl++);
523 			fxdr_hyper(tl, &frev);
524 		} else
525 			nqlflag = 0;
526 	}
527 	nfsm_dissect(fhp, nfsv2fh_t *, NFSX_FH);
528 
529 	/*
530 	 * Handle RENAME case...
531 	 */
532 	if (cnp->cn_nameiop == RENAME && wantparent && (cnp->cn_flags&ISLASTCN)) {
533 		if (!bcmp(np->n_fh.fh_bytes, (caddr_t)fhp, NFSX_FH)) {
534 			m_freem(mrep);
535 			return (EISDIR);
536 		}
537 		if (error = nfs_nget(dvp->v_mount, fhp, &np)) {
538 			m_freem(mrep);
539 			return (error);
540 		}
541 		newvp = NFSTOV(np);
542 		if (error =
543 		    nfs_loadattrcache(&newvp, &md, &dpos, (struct vattr *)0)) {
544 			vrele(newvp);
545 			m_freem(mrep);
546 			return (error);
547 		}
548 		*vpp = newvp;
549 		m_freem(mrep);
550 		cnp->cn_flags |= SAVENAME;
551 		return (0);
552 	}
553 
554 	if (!bcmp(np->n_fh.fh_bytes, (caddr_t)fhp, NFSX_FH)) {
555 		VREF(dvp);
556 		newvp = dvp;
557 	} else {
558 		if (error = nfs_nget(dvp->v_mount, fhp, &np)) {
559 			m_freem(mrep);
560 			return (error);
561 		}
562 		newvp = NFSTOV(np);
563 	}
564 	if (error = nfs_loadattrcache(&newvp, &md, &dpos, (struct vattr *)0)) {
565 		vrele(newvp);
566 		m_freem(mrep);
567 		return (error);
568 	}
569 	m_freem(mrep);
570 	*vpp = newvp;
571 	if (cnp->cn_nameiop != LOOKUP && (cnp->cn_flags&ISLASTCN))
572 		cnp->cn_flags |= SAVENAME;
573 	if ((cnp->cn_flags&MAKEENTRY) && (cnp->cn_nameiop != DELETE || !(cnp->cn_flags&ISLASTCN))) {
574 		if ((nmp->nm_flag & NFSMNT_NQNFS) == 0)
575 			np->n_ctime = np->n_vattr.va_ctime.tv_sec;
576 		else if (nqlflag && reqtime > time.tv_sec) {
577 			if (np->n_tnext) {
578 				if (np->n_tnext == (struct nfsnode *)nmp)
579 					nmp->nm_tprev = np->n_tprev;
580 				else
581 					np->n_tnext->n_tprev = np->n_tprev;
582 				if (np->n_tprev == (struct nfsnode *)nmp)
583 					nmp->nm_tnext = np->n_tnext;
584 				else
585 					np->n_tprev->n_tnext = np->n_tnext;
586 				if (nqlflag == NQL_WRITE)
587 					np->n_flag |= NQNFSWRITE;
588 			} else if (nqlflag == NQL_READ)
589 				np->n_flag &= ~NQNFSWRITE;
590 			else
591 				np->n_flag |= NQNFSWRITE;
592 			if (cachable)
593 				np->n_flag &= ~NQNFSNONCACHE;
594 			else
595 				np->n_flag |= NQNFSNONCACHE;
596 			np->n_expiry = reqtime;
597 			np->n_lrev = frev;
598 			tp = nmp->nm_tprev;
599 			while (tp != (struct nfsnode *)nmp && tp->n_expiry > np->n_expiry)
600 				tp = tp->n_tprev;
601 			if (tp == (struct nfsnode *)nmp) {
602 				np->n_tnext = nmp->nm_tnext;
603 				nmp->nm_tnext = np;
604 			} else {
605 				np->n_tnext = tp->n_tnext;
606 				tp->n_tnext = np;
607 			}
608 			np->n_tprev = tp;
609 			if (np->n_tnext == (struct nfsnode *)nmp)
610 				nmp->nm_tprev = np;
611 			else
612 				np->n_tnext->n_tprev = np;
613 		}
614 		cache_enter(dvp, *vpp, cnp);
615 	}
616 	return (0);
617 }
618 
619 /*
620  * nfs read call.
621  * Just call nfs_bioread() to do the work.
622  */
623 int
624 nfs_read(vp, uiop, ioflag, cred)
625 	register struct vnode *vp;
626 	struct uio *uiop;
627 	int ioflag;
628 	struct ucred *cred;
629 {
630 	if (vp->v_type != VREG)
631 		return (EPERM);
632 	return (nfs_bioread(vp, uiop, ioflag, cred));
633 }
634 
635 /*
636  * nfs readlink call
637  */
638 int
639 nfs_readlink(vp, uiop, cred)
640 	struct vnode *vp;
641 	struct uio *uiop;
642 	struct ucred *cred;
643 {
644 	if (vp->v_type != VLNK)
645 		return (EPERM);
646 	return (nfs_bioread(vp, uiop, 0, cred));
647 }
648 
649 /*
650  * Do a readlink rpc.
651  * Called by nfs_doio() from below the buffer cache.
652  */
653 int
654 nfs_readlinkrpc(vp, uiop, cred)
655 	register struct vnode *vp;
656 	struct uio *uiop;
657 	struct ucred *cred;
658 {
659 	register u_long *tl;
660 	register caddr_t cp;
661 	register long t1;
662 	caddr_t bpos, dpos, cp2;
663 	int error = 0;
664 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
665 	long len;
666 
667 	nfsstats.rpccnt[NFSPROC_READLINK]++;
668 	nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH);
669 	nfsm_fhtom(vp);
670 	nfsm_request(vp, NFSPROC_READLINK, uiop->uio_procp, cred);
671 	nfsm_strsiz(len, NFS_MAXPATHLEN);
672 	nfsm_mtouio(uiop, len);
673 	nfsm_reqdone;
674 	return (error);
675 }
676 
677 /*
678  * nfs read rpc call
679  * Ditto above
680  */
681 int
682 nfs_readrpc(vp, uiop, cred)
683 	register struct vnode *vp;
684 	struct uio *uiop;
685 	struct ucred *cred;
686 {
687 	register u_long *tl;
688 	register caddr_t cp;
689 	register long t1;
690 	caddr_t bpos, dpos, cp2;
691 	int error = 0;
692 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
693 	struct nfsmount *nmp;
694 	long len, retlen, tsiz;
695 
696 	nmp = VFSTONFS(vp->v_mount);
697 	tsiz = uiop->uio_resid;
698 	while (tsiz > 0) {
699 		nfsstats.rpccnt[NFSPROC_READ]++;
700 		len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
701 		nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH+NFSX_UNSIGNED*3);
702 		nfsm_fhtom(vp);
703 		nfsm_build(tl, u_long *, NFSX_UNSIGNED*3);
704 		*tl++ = txdr_unsigned(uiop->uio_offset);
705 		*tl++ = txdr_unsigned(len);
706 		*tl = 0;
707 		nfsm_request(vp, NFSPROC_READ, uiop->uio_procp, cred);
708 		nfsm_loadattr(vp, (struct vattr *)0);
709 		nfsm_strsiz(retlen, nmp->nm_rsize);
710 		nfsm_mtouio(uiop, retlen);
711 		m_freem(mrep);
712 		if (retlen < len)
713 			tsiz = 0;
714 		else
715 			tsiz -= len;
716 	}
717 nfsmout:
718 	return (error);
719 }
720 
721 /*
722  * nfs write call
723  */
724 int
725 nfs_writerpc(vp, uiop, cred)
726 	register struct vnode *vp;
727 	struct uio *uiop;
728 	struct ucred *cred;
729 {
730 	register u_long *tl;
731 	register caddr_t cp;
732 	register long t1;
733 	caddr_t bpos, dpos, cp2;
734 	int error = 0;
735 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
736 	struct nfsmount *nmp;
737 	struct nfsnode *np = VTONFS(vp);
738 	u_quad_t frev;
739 	long len, tsiz;
740 
741 	nmp = VFSTONFS(vp->v_mount);
742 	tsiz = uiop->uio_resid;
743 	while (tsiz > 0) {
744 		nfsstats.rpccnt[NFSPROC_WRITE]++;
745 		len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
746 		nfsm_reqhead(vp, NFSPROC_WRITE,
747 			NFSX_FH+NFSX_UNSIGNED*4+nfsm_rndup(len));
748 		nfsm_fhtom(vp);
749 		nfsm_build(tl, u_long *, NFSX_UNSIGNED*4);
750 		*(tl+1) = txdr_unsigned(uiop->uio_offset);
751 		*(tl+3) = txdr_unsigned(len);
752 		nfsm_uiotom(uiop, len);
753 		nfsm_request(vp, NFSPROC_WRITE, uiop->uio_procp, cred);
754 		nfsm_loadattr(vp, (struct vattr *)0);
755 		if (nmp->nm_flag & NFSMNT_MYWRITE)
756 			VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr.va_mtime.tv_sec;
757 		else if ((nmp->nm_flag & NFSMNT_NQNFS) &&
758 			 NQNFS_CKCACHABLE(vp, NQL_WRITE)) {
759 			nfsm_dissect(tl, u_long *, 2*NFSX_UNSIGNED);
760 			fxdr_hyper(tl, &frev);
761 			if (QUADGT(frev, np->n_brev))
762 				np->n_brev = frev;
763 		}
764 		m_freem(mrep);
765 		tsiz -= len;
766 	}
767 nfsmout:
768 	if (error)
769 		uiop->uio_resid = tsiz;
770 	return (error);
771 }
772 
773 /*
774  * nfs mknod call
775  * This is a kludge. Use a create rpc but with the IFMT bits of the mode
776  * set to specify the file type and the size field for rdev.
777  */
778 /* ARGSUSED */
779 int
780 nfs_mknod(dvp, vpp, cnp, vap)
781 	struct vnode *dvp;
782 	struct vnode **vpp;
783 	struct componentname *cnp;
784 	struct vattr *vap;
785 {
786 	register struct nfsv2_sattr *sp;
787 	register u_long *tl;
788 	register caddr_t cp;
789 	register long t2;
790 	caddr_t bpos, dpos;
791 	int error = 0;
792 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
793 	u_long rdev;
794 
795 	if (vap->va_type == VCHR || vap->va_type == VBLK)
796 		rdev = txdr_unsigned(vap->va_rdev);
797 #ifdef FIFO
798 	else if (vap->va_type == VFIFO)
799 		rdev = 0xffffffff;
800 #endif /* FIFO */
801 	else {
802 		VOP_ABORTOP(dvp, cnp);
803 		vput(dvp);
804 		return (EOPNOTSUPP);
805 	}
806 	nfsstats.rpccnt[NFSPROC_CREATE]++;
807 	nfsm_reqhead(dvp, NFSPROC_CREATE,
808 	  NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(cnp->cn_namelen)+NFSX_SATTR);
809 	nfsm_fhtom(dvp);
810 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
811 	nfsm_build(sp, struct nfsv2_sattr *, NFSX_SATTR);
812 	sp->sa_mode = vtonfs_mode(vap->va_type, vap->va_mode);
813 	sp->sa_uid = txdr_unsigned(cnp->cn_cred->cr_uid);
814 	sp->sa_gid = txdr_unsigned(cnp->cn_cred->cr_gid);
815 	sp->sa_size = rdev;
816 	/* or should these be VNOVAL ?? */
817 	txdr_time(&vap->va_atime, &sp->sa_atime);
818 	txdr_time(&vap->va_mtime, &sp->sa_mtime);
819 	nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_proc, cnp->cn_cred);
820 	nfsm_reqdone;
821 	FREE(cnp->cn_pnbuf, M_NAMEI);
822 	VTONFS(dvp)->n_flag |= NMODIFIED;
823 	vrele(dvp);
824 	return (error);
825 }
826 
827 /*
828  * nfs file create call
829  */
830 int
831 nfs_create(dvp, vpp, cnp, vap)
832 	struct vnode *dvp;
833 	struct vnode **vpp;
834 	struct componentname *cnp;
835 	struct vattr *vap;
836 {
837 	register struct nfsv2_sattr *sp;
838 	register u_long *tl;
839 	register caddr_t cp;
840 	register long t1, t2;
841 	caddr_t bpos, dpos, cp2;
842 	int error = 0;
843 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
844 
845 	nfsstats.rpccnt[NFSPROC_CREATE]++;
846 	nfsm_reqhead(dvp, NFSPROC_CREATE,
847 	  NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(cnp->cn_namelen)+NFSX_SATTR);
848 	nfsm_fhtom(dvp);
849 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
850 	nfsm_build(sp, struct nfsv2_sattr *, NFSX_SATTR);
851 	sp->sa_mode = vtonfs_mode(vap->va_type, vap->va_mode);
852 	sp->sa_uid = txdr_unsigned(cnp->cn_cred->cr_uid);
853 	sp->sa_gid = txdr_unsigned(cnp->cn_cred->cr_gid);
854 	sp->sa_size = txdr_unsigned(0);
855 	/* or should these be VNOVAL ?? */
856 	txdr_time(&vap->va_atime, &sp->sa_atime);
857 	txdr_time(&vap->va_mtime, &sp->sa_mtime);
858 	nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_proc, cnp->cn_cred);
859 	nfsm_mtofh(dvp, *vpp);
860 	nfsm_reqdone;
861 	FREE(cnp->cn_pnbuf, M_NAMEI);
862 	VTONFS(dvp)->n_flag |= NMODIFIED;
863 	vrele(dvp);
864 	return (error);
865 }
866 
867 /*
868  * nfs file remove call
869  * To try and make nfs semantics closer to ufs semantics, a file that has
870  * other processes using the vnode is renamed instead of removed and then
871  * removed later on the last close.
872  * - If v_usecount > 1
873  *	  If a rename is not already in the works
874  *	     call nfs_sillyrename() to set it up
875  *     else
876  *	  do the remove rpc
877  */
878 int
879 nfs_remove(dvp, vp, cnp)
880 	struct vnode *dvp, *vp;
881 	struct componentname *cnp;
882 {
883 	register struct nfsnode *np = VTONFS(vp);
884 	register u_long *tl;
885 	register caddr_t cp;
886 	register long t2;
887 	caddr_t bpos, dpos;
888 	int error = 0;
889 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
890 
891 	if (vp->v_usecount > 1) {
892 		if (!np->n_sillyrename)
893 			error = nfs_sillyrename(dvp, vp, cnp);
894 	} else {
895 		/*
896 		 * Purge the name cache so that the chance of a lookup for
897 		 * the name succeeding while the remove is in progress is
898 		 * minimized. Without node locking it can still happen, such
899 		 * that an I/O op returns ESTALE, but since you get this if
900 		 * another host removes the file..
901 		 */
902 		cache_purge(vp);
903 		/*
904 		 * Throw away biocache buffers. Mainly to avoid
905 		 * unnecessary delayed writes.
906 		 */
907 		vinvalbuf(vp, FALSE);
908 		/* Do the rpc */
909 		nfsstats.rpccnt[NFSPROC_REMOVE]++;
910 		nfsm_reqhead(dvp, NFSPROC_REMOVE,
911 			NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(cnp->cn_namelen));
912 		nfsm_fhtom(dvp);
913 		nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
914 		nfsm_request(dvp, NFSPROC_REMOVE, cnp->cn_proc, cnp->cn_cred);
915 		nfsm_reqdone;
916 		FREE(cnp->cn_pnbuf, M_NAMEI);
917 		VTONFS(dvp)->n_flag |= NMODIFIED;
918 		/*
919 		 * Kludge City: If the first reply to the remove rpc is lost..
920 		 *   the reply to the retransmitted request will be ENOENT
921 		 *   since the file was in fact removed
922 		 *   Therefore, we cheat and return success.
923 		 */
924 		if (error == ENOENT)
925 			error = 0;
926 	}
927 	np->n_attrstamp = 0;
928 	vrele(dvp);
929 	vrele(vp);
930 	return (error);
931 }
932 
933 /*
934  * nfs file remove rpc called from nfs_inactive
935  */
936 int
937 nfs_removeit(sp, procp)
938 	register struct sillyrename *sp;
939 	struct proc *procp;
940 {
941 	register u_long *tl;
942 	register caddr_t cp;
943 	register long t2;
944 	caddr_t bpos, dpos;
945 	int error = 0;
946 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
947 
948 	nfsstats.rpccnt[NFSPROC_REMOVE]++;
949 	nfsm_reqhead(sp->s_dvp, NFSPROC_REMOVE,
950 		NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(sp->s_namlen));
951 	nfsm_fhtom(sp->s_dvp);
952 	nfsm_strtom(sp->s_name, sp->s_namlen, NFS_MAXNAMLEN);
953 	nfsm_request(sp->s_dvp, NFSPROC_REMOVE, procp, sp->s_cred);
954 	nfsm_reqdone;
955 	VTONFS(sp->s_dvp)->n_flag |= NMODIFIED;
956 	return (error);
957 }
958 
959 /*
960  * nfs file rename call
961  */
962 int
963 nfs_rename(fdvp, fvp, fcnp, 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(tdvp, vp, cnp)
1045 	struct vnode *tdvp;
1046 	register struct vnode *vp;   /* source vnode */
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 readdir *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 readdir *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 readdir *)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 readdir *)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 readdir *)
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 readdir *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 readdir *)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, cred)
2056 	struct vnode *vp;
2057 	off_t length;
2058 	int flags;
2059 	struct ucred *cred;
2060 {
2061 
2062 	/* Use nfs_setattr */
2063 	printf("nfs_truncate: need to implement!!");
2064 	return (EOPNOTSUPP);
2065 }
2066 
2067 /*
2068  * NFS update.
2069  */
2070 int
2071 nfs_update(vp, ta, tm, waitfor)
2072 	struct vnode *vp;
2073 	struct timeval *ta;
2074 	struct timeval *tm;
2075 	int waitfor;
2076 {
2077 
2078 	/* Use nfs_setattr */
2079 	printf("nfs_update: need to implement!!");
2080 	return (EOPNOTSUPP);
2081 }
2082