xref: /csrg-svn/sys/nfs/nfs_vnops.c (revision 43356)
1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rick Macklem at The University of Guelph.
7  *
8  * Redistribution and use in source and binary forms are permitted
9  * provided that the above copyright notice and this paragraph are
10  * duplicated in all such forms and that any documentation,
11  * advertising materials, and other materials related to such
12  * distribution and use acknowledge that the software was developed
13  * by the University of California, Berkeley.  The name of the
14  * University may not be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19  *
20  *	@(#)nfs_vnops.c	7.42 (Berkeley) 06/21/90
21  */
22 
23 /*
24  * vnode op calls for sun nfs version 2
25  */
26 
27 #include "machine/pte.h"
28 #include "machine/mtpr.h"
29 #include "param.h"
30 #include "user.h"
31 #include "proc.h"
32 #include "kernel.h"
33 #include "mount.h"
34 #include "buf.h"
35 #include "vm.h"
36 #include "malloc.h"
37 #include "mbuf.h"
38 #include "errno.h"
39 #include "file.h"
40 #include "conf.h"
41 #include "vnode.h"
42 #include "text.h"
43 #include "map.h"
44 #include "../ufs/quota.h"
45 #include "../ufs/inode.h"
46 #include "nfsv2.h"
47 #include "nfs.h"
48 #include "nfsnode.h"
49 #include "nfsmount.h"
50 #include "xdr_subs.h"
51 #include "nfsm_subs.h"
52 #include "nfsiom.h"
53 
54 /* Defs */
55 #define	TRUE	1
56 #define	FALSE	0
57 
58 /* Global vars */
59 int	nfs_lookup(),
60 	nfs_create(),
61 	nfs_mknod(),
62 	nfs_open(),
63 	nfs_close(),
64 	nfs_access(),
65 	nfs_getattr(),
66 	nfs_setattr(),
67 	nfs_read(),
68 	nfs_write(),
69 	vfs_noop(),
70 	vfs_nullop(),
71 	nfs_remove(),
72 	nfs_link(),
73 	nfs_rename(),
74 	nfs_mkdir(),
75 	nfs_rmdir(),
76 	nfs_symlink(),
77 	nfs_readdir(),
78 	nfs_readlink(),
79 	nfs_abortop(),
80 	nfs_lock(),
81 	nfs_unlock(),
82 	nfs_bmap(),
83 	nfs_strategy(),
84 	nfs_fsync(),
85 	nfs_inactive(),
86 	nfs_reclaim(),
87 	nfs_print(),
88 	nfs_islocked();
89 
90 struct vnodeops nfsv2_vnodeops = {
91 	nfs_lookup,		/* lookup */
92 	nfs_create,		/* create */
93 	nfs_mknod,		/* mknod */
94 	nfs_open,		/* open */
95 	nfs_close,		/* close */
96 	nfs_access,		/* access */
97 	nfs_getattr,		/* getattr */
98 	nfs_setattr,		/* setattr */
99 	nfs_read,		/* read */
100 	nfs_write,		/* write */
101 	vfs_noop,		/* ioctl */
102 	vfs_noop,		/* select */
103 	vfs_noop,		/* mmap */
104 	nfs_fsync,		/* fsync */
105 	vfs_nullop,		/* seek */
106 	nfs_remove,		/* remove */
107 	nfs_link,		/* link */
108 	nfs_rename,		/* rename */
109 	nfs_mkdir,		/* mkdir */
110 	nfs_rmdir,		/* rmdir */
111 	nfs_symlink,		/* symlink */
112 	nfs_readdir,		/* readdir */
113 	nfs_readlink,		/* readlink */
114 	nfs_abortop,		/* abortop */
115 	nfs_inactive,		/* inactive */
116 	nfs_reclaim,		/* reclaim */
117 	nfs_lock,		/* lock */
118 	nfs_unlock,		/* unlock */
119 	nfs_bmap,		/* bmap */
120 	nfs_strategy,		/* strategy */
121 	nfs_print,		/* print */
122 	nfs_islocked,		/* islocked */
123 };
124 
125 /* Special device vnode ops */
126 int	spec_lookup(),
127 	spec_open(),
128 	spec_read(),
129 	spec_write(),
130 	spec_strategy(),
131 	spec_bmap(),
132 	spec_ioctl(),
133 	spec_select(),
134 	spec_close(),
135 	spec_badop(),
136 	spec_nullop();
137 
138 struct vnodeops spec_nfsv2nodeops = {
139 	spec_lookup,		/* lookup */
140 	spec_badop,		/* create */
141 	spec_badop,		/* mknod */
142 	spec_open,		/* open */
143 	spec_close,		/* close */
144 	nfs_access,		/* access */
145 	nfs_getattr,		/* getattr */
146 	nfs_setattr,		/* setattr */
147 	spec_read,		/* read */
148 	spec_write,		/* write */
149 	spec_ioctl,		/* ioctl */
150 	spec_select,		/* select */
151 	spec_badop,		/* mmap */
152 	spec_nullop,		/* fsync */
153 	spec_badop,		/* seek */
154 	spec_badop,		/* remove */
155 	spec_badop,		/* link */
156 	spec_badop,		/* rename */
157 	spec_badop,		/* mkdir */
158 	spec_badop,		/* rmdir */
159 	spec_badop,		/* symlink */
160 	spec_badop,		/* readdir */
161 	spec_badop,		/* readlink */
162 	spec_badop,		/* abortop */
163 	nfs_inactive,		/* inactive */
164 	nfs_reclaim,		/* reclaim */
165 	nfs_lock,		/* lock */
166 	nfs_unlock,		/* unlock */
167 	spec_bmap,		/* bmap */
168 	spec_strategy,		/* strategy */
169 	nfs_print,		/* print */
170 	nfs_islocked,		/* islocked */
171 };
172 
173 #ifdef FIFO
174 int	fifo_lookup(),
175 	fifo_open(),
176 	fifo_read(),
177 	fifo_write(),
178 	fifo_bmap(),
179 	fifo_ioctl(),
180 	fifo_select(),
181 	fifo_close(),
182 	fifo_print(),
183 	fifo_badop(),
184 	fifo_nullop();
185 
186 struct vnodeops fifo_nfsv2nodeops = {
187 	fifo_lookup,		/* lookup */
188 	fifo_badop,		/* create */
189 	fifo_badop,		/* mknod */
190 	fifo_open,		/* open */
191 	fifo_close,		/* close */
192 	nfs_access,		/* access */
193 	nfs_getattr,		/* getattr */
194 	nfs_setattr,		/* setattr */
195 	fifo_read,		/* read */
196 	fifo_write,		/* write */
197 	fifo_ioctl,		/* ioctl */
198 	fifo_select,		/* select */
199 	fifo_badop,		/* mmap */
200 	fifo_nullop,		/* fsync */
201 	fifo_badop,		/* seek */
202 	fifo_badop,		/* remove */
203 	fifo_badop,		/* link */
204 	fifo_badop,		/* rename */
205 	fifo_badop,		/* mkdir */
206 	fifo_badop,		/* rmdir */
207 	fifo_badop,		/* symlink */
208 	fifo_badop,		/* readdir */
209 	fifo_badop,		/* readlink */
210 	fifo_badop,		/* abortop */
211 	nfs_inactive,		/* inactive */
212 	nfs_reclaim,		/* reclaim */
213 	nfs_lock,		/* lock */
214 	nfs_unlock,		/* unlock */
215 	fifo_bmap,		/* bmap */
216 	fifo_badop,		/* strategy */
217 	nfs_print,		/* print */
218 	nfs_islocked,		/* islocked */
219 };
220 #endif /* FIFO */
221 
222 extern u_long nfs_procids[NFS_NPROCS];
223 extern u_long nfs_prog, nfs_vers;
224 extern char nfsiobuf[MAXPHYS+NBPG];
225 struct map nfsmap[NFS_MSIZ];
226 struct buf nfs_bqueue;		/* Queue head for nfsiod's */
227 int nfs_asyncdaemons = 0;
228 struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON];
229 static int nfsmap_want = 0;
230 
231 /*
232  * nfs null call from vfs.
233  */
234 nfs_null(vp, cred)
235 	struct vnode *vp;
236 	struct ucred *cred;
237 {
238 	caddr_t bpos, dpos;
239 	u_long xid;
240 	int error = 0;
241 	struct mbuf *mreq, *mrep, *md, *mb;
242 
243 	nfsm_reqhead(nfs_procids[NFSPROC_NULL], cred, 0);
244 	nfsm_request(vp, NFSPROC_NULL, u.u_procp, 0);
245 	nfsm_reqdone;
246 	return (error);
247 }
248 
249 /*
250  * nfs access vnode op.
251  * Essentially just get vattr and then imitate iaccess()
252  */
253 nfs_access(vp, mode, cred)
254 	struct vnode *vp;
255 	int mode;
256 	register struct ucred *cred;
257 {
258 	register struct vattr *vap;
259 	register gid_t *gp;
260 	struct vattr vattr;
261 	register int i;
262 	int error;
263 
264 	/*
265 	 * If you're the super-user,
266 	 * you always get access.
267 	 */
268 	if (cred->cr_uid == 0)
269 		return (0);
270 	vap = &vattr;
271 	if (error = nfs_dogetattr(vp, vap, cred, 0))
272 		return (error);
273 	/*
274 	 * Access check is based on only one of owner, group, public.
275 	 * If not owner, then check group. If not a member of the
276 	 * group, then check public access.
277 	 */
278 	if (cred->cr_uid != vap->va_uid) {
279 		mode >>= 3;
280 		gp = cred->cr_groups;
281 		for (i = 0; i < cred->cr_ngroups; i++, gp++)
282 			if (vap->va_gid == *gp)
283 				goto found;
284 		mode >>= 3;
285 found:
286 		;
287 	}
288 	if ((vap->va_mode & mode) != 0)
289 		return (0);
290 	return (EACCES);
291 }
292 
293 /*
294  * nfs open vnode op
295  * Just check to see if the type is ok
296  */
297 /* ARGSUSED */
298 nfs_open(vp, mode, cred)
299 	struct vnode *vp;
300 	int mode;
301 	struct ucred *cred;
302 {
303 	register enum vtype vtyp;
304 
305 	vtyp = vp->v_type;
306 	if (vtyp == VREG || vtyp == VDIR || vtyp == VLNK)
307 		return (0);
308 	else
309 		return (EACCES);
310 }
311 
312 /*
313  * nfs close vnode op
314  * For reg files, invalidate any buffer cache entries.
315  */
316 /* ARGSUSED */
317 nfs_close(vp, fflags, cred)
318 	register struct vnode *vp;
319 	int fflags;
320 	struct ucred *cred;
321 {
322 	register struct nfsnode *np = VTONFS(vp);
323 	int error = 0;
324 
325 	if (vp->v_type == VREG && (np->n_flag & NMODIFIED)) {
326 		nfs_lock(vp);
327 		np->n_flag &= ~NMODIFIED;
328 		vinvalbuf(vp, TRUE);
329 		np->n_attrstamp = 0;
330 		if (np->n_flag & NWRITEERR) {
331 			np->n_flag &= ~NWRITEERR;
332 			error = np->n_error;
333 		}
334 		nfs_unlock(vp);
335 	}
336 	return (error);
337 }
338 
339 /*
340  * nfs getattr call from vfs.
341  */
342 nfs_getattr(vp, vap, cred)
343 	register struct vnode *vp;
344 	struct vattr *vap;
345 	struct ucred *cred;
346 {
347 	return (nfs_dogetattr(vp, vap, cred, 0));
348 }
349 
350 nfs_dogetattr(vp, vap, cred, tryhard)
351 	register struct vnode *vp;
352 	struct vattr *vap;
353 	struct ucred *cred;
354 	int tryhard;
355 {
356 	register caddr_t cp;
357 	register long t1;
358 	caddr_t bpos, dpos;
359 	u_long xid;
360 	int error = 0;
361 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
362 
363 	/* First look in the cache.. */
364 	if (nfs_getattrcache(vp, vap) == 0)
365 		return (0);
366 	nfsstats.rpccnt[NFSPROC_GETATTR]++;
367 	nfsm_reqhead(nfs_procids[NFSPROC_GETATTR], cred, NFSX_FH);
368 	nfsm_fhtom(vp);
369 	nfsm_request(vp, NFSPROC_GETATTR, u.u_procp, tryhard);
370 	nfsm_loadattr(vp, vap);
371 	nfsm_reqdone;
372 	return (error);
373 }
374 
375 /*
376  * nfs setattr call.
377  */
378 nfs_setattr(vp, vap, cred)
379 	register struct vnode *vp;
380 	register struct vattr *vap;
381 	struct ucred *cred;
382 {
383 	register struct nfsv2_sattr *sp;
384 	register caddr_t cp;
385 	register long t1;
386 	caddr_t bpos, dpos;
387 	u_long xid;
388 	int error = 0;
389 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
390 	struct nfsnode *np;
391 
392 	nfsstats.rpccnt[NFSPROC_SETATTR]++;
393 	nfsm_reqhead(nfs_procids[NFSPROC_SETATTR], cred, NFSX_FH+NFSX_SATTR);
394 	nfsm_fhtom(vp);
395 	nfsm_build(sp, struct nfsv2_sattr *, NFSX_SATTR);
396 	if (vap->va_mode == 0xffff)
397 		sp->sa_mode = VNOVAL;
398 	else
399 		sp->sa_mode = vtonfs_mode(vp->v_type, vap->va_mode);
400 	if (vap->va_uid == 0xffff)
401 		sp->sa_uid = VNOVAL;
402 	else
403 		sp->sa_uid = txdr_unsigned(vap->va_uid);
404 	if (vap->va_gid == 0xffff)
405 		sp->sa_gid = VNOVAL;
406 	else
407 		sp->sa_gid = txdr_unsigned(vap->va_gid);
408 	sp->sa_size = txdr_unsigned(vap->va_size);
409 	if (vap->va_size != VNOVAL) {
410 		np = VTONFS(vp);
411 		if (np->n_flag & NMODIFIED) {
412 			np->n_flag &= ~NMODIFIED;
413 			vinvalbuf(vp, TRUE);
414 			np->n_attrstamp = 0;
415 		}
416 	}
417 	sp->sa_atime.tv_sec = txdr_unsigned(vap->va_atime.tv_sec);
418 	sp->sa_atime.tv_usec = txdr_unsigned(vap->va_flags);
419 	txdr_time(&vap->va_mtime, &sp->sa_mtime);
420 	nfsm_request(vp, NFSPROC_SETATTR, u.u_procp, 1);
421 	nfsm_loadattr(vp, (struct vattr *)0);
422 	/* should we fill in any vap fields ?? */
423 	nfsm_reqdone;
424 	return (error);
425 }
426 
427 /*
428  * nfs lookup call, one step at a time...
429  * First look in cache
430  * If not found, unlock the directory nfsnode and do the rpc
431  */
432 nfs_lookup(vp, ndp)
433 	register struct vnode *vp;
434 	register struct nameidata *ndp;
435 {
436 	register struct vnode *vdp;
437 	register u_long *p;
438 	register caddr_t cp;
439 	register long t1, t2;
440 	caddr_t bpos, dpos, cp2;
441 	u_long xid;
442 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
443 	struct vnode *newvp;
444 	long len;
445 	nfsv2fh_t *fhp;
446 	struct nfsnode *np;
447 	int lockparent, wantparent, flag, error = 0;
448 
449 	ndp->ni_dvp = vp;
450 	ndp->ni_vp = NULL;
451 	if (vp->v_type != VDIR)
452 		return (ENOTDIR);
453 	lockparent = ndp->ni_nameiop & LOCKPARENT;
454 	flag = ndp->ni_nameiop & OPFLAG;
455 	wantparent = ndp->ni_nameiop & (LOCKPARENT|WANTPARENT);
456 	if ((error = cache_lookup(ndp)) && error != ENOENT) {
457 		struct vattr vattr;
458 		int vpid;
459 
460 		if (vp == ndp->ni_rdir && ndp->ni_isdotdot)
461 			panic("nfs_lookup: .. through root");
462 		vdp = ndp->ni_vp;
463 		vpid = vdp->v_id;
464 		/*
465 		 * See the comment starting `Step through' in ufs/ufs_lookup.c
466 		 * for an explanation of the locking protocol
467 		 */
468 		if (vp == vdp) {
469 			VREF(vdp);
470 			error = 0;
471 		} else if (ndp->ni_isdotdot) {
472 			nfs_unlock(vp);
473 			error = vget(vdp);
474 		} else {
475 			error = vget(vdp);
476 			nfs_unlock(vp);
477 		}
478 		if (!error) {
479 			if (vpid == vdp->v_id) {
480 			   if (!nfs_dogetattr(vdp, &vattr, ndp->ni_cred, 0) &&
481 			       vattr.va_ctime.tv_sec == VTONFS(vdp)->n_ctime) {
482 				nfsstats.lookupcache_hits++;
483 				return (0);
484 			   } else {
485 				cache_purge(vdp);
486 				nfs_nput(vdp);
487 			   }
488 			} else {
489 				nfs_nput(vdp);
490 			}
491 		}
492 		ndp->ni_vp = NULLVP;
493 	} else
494 		nfs_unlock(vp);
495 	error = 0;
496 	nfsstats.lookupcache_misses++;
497 	nfsstats.rpccnt[NFSPROC_LOOKUP]++;
498 	len = ndp->ni_namelen;
499 	nfsm_reqhead(nfs_procids[NFSPROC_LOOKUP], ndp->ni_cred, NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(len));
500 	nfsm_fhtom(vp);
501 	nfsm_strtom(ndp->ni_ptr, len, NFS_MAXNAMLEN);
502 	nfsm_request(vp, NFSPROC_LOOKUP, u.u_procp, 0);
503 nfsmout:
504 	if (error) {
505 		if (lockparent || (flag != CREATE && flag != RENAME) ||
506 		    *ndp->ni_next != 0)
507 			nfs_lock(vp);
508 		return (error);
509 	}
510 	nfsm_disect(fhp,nfsv2fh_t *,NFSX_FH);
511 
512 	/*
513 	 * Handle DELETE and RENAME cases...
514 	 */
515 	if (flag == DELETE && *ndp->ni_next == 0) {
516 		if (!bcmp(VTONFS(vp)->n_fh.fh_bytes, (caddr_t)fhp, NFSX_FH)) {
517 			VREF(vp);
518 			newvp = vp;
519 			np = VTONFS(vp);
520 		} else {
521 			if (error = nfs_nget(vp->v_mount, fhp, &np)) {
522 				nfs_lock(vp);
523 				m_freem(mrep);
524 				return (error);
525 			}
526 			newvp = NFSTOV(np);
527 		}
528 		if (error =
529 		    nfs_loadattrcache(&newvp, &md, &dpos, (struct vattr *)0)) {
530 			nfs_lock(vp);
531 			if (newvp != vp)
532 				nfs_nput(newvp);
533 			else
534 				vrele(vp);
535 			m_freem(mrep);
536 			return (error);
537 		}
538 		ndp->ni_vp = newvp;
539 		if (lockparent || vp == newvp)
540 			nfs_lock(vp);
541 		m_freem(mrep);
542 		return (0);
543 	}
544 
545 	if (flag == RENAME && wantparent && *ndp->ni_next == 0) {
546 		if (!bcmp(VTONFS(vp)->n_fh.fh_bytes, (caddr_t)fhp, NFSX_FH)) {
547 			nfs_lock(vp);
548 			m_freem(mrep);
549 			return (EISDIR);
550 		}
551 		if (error = nfs_nget(vp->v_mount, fhp, &np)) {
552 			nfs_lock(vp);
553 			m_freem(mrep);
554 			return (error);
555 		}
556 		newvp = NFSTOV(np);
557 		if (error =
558 		    nfs_loadattrcache(&newvp, &md, &dpos, (struct vattr *)0)) {
559 			nfs_lock(vp);
560 			nfs_nput(newvp);
561 			m_freem(mrep);
562 			return (error);
563 		}
564 		ndp->ni_vp = newvp;
565 		if (lockparent)
566 			nfs_lock(vp);
567 		return (0);
568 	}
569 
570 	if (!bcmp(VTONFS(vp)->n_fh.fh_bytes, (caddr_t)fhp, NFSX_FH)) {
571 		VREF(vp);
572 		newvp = vp;
573 		np = VTONFS(vp);
574 	} else if (ndp->ni_isdotdot) {
575 		if (error = nfs_nget(vp->v_mount, fhp, &np)) {
576 			nfs_lock(vp);
577 			m_freem(mrep);
578 			return (error);
579 		}
580 		newvp = NFSTOV(np);
581 	} else {
582 		if (error = nfs_nget(vp->v_mount, fhp, &np)) {
583 			nfs_lock(vp);
584 			m_freem(mrep);
585 			return (error);
586 		}
587 		newvp = NFSTOV(np);
588 	}
589 	if (error = nfs_loadattrcache(&newvp, &md, &dpos, (struct vattr *)0)) {
590 		nfs_lock(vp);
591 		if (newvp != vp)
592 			nfs_nput(newvp);
593 		else
594 			vrele(vp);
595 		m_freem(mrep);
596 		return (error);
597 	}
598 	m_freem(mrep);
599 
600 	if (vp == newvp || (lockparent && *ndp->ni_next == '\0'))
601 		nfs_lock(vp);
602 	ndp->ni_vp = newvp;
603 	if (error == 0 && ndp->ni_makeentry) {
604 		np->n_ctime = np->n_vattr.va_ctime.tv_sec;
605 		cache_enter(ndp);
606 	}
607 	return (error);
608 }
609 
610 /*
611  * nfs read call.
612  * Just call nfs_bioread() to do the work.
613  */
614 nfs_read(vp, uiop, ioflag, cred)
615 	register struct vnode *vp;
616 	struct uio *uiop;
617 	int ioflag;
618 	struct ucred *cred;
619 {
620 	if (vp->v_type != VREG)
621 		return (EPERM);
622 	return (nfs_bioread(vp, uiop, ioflag, cred));
623 }
624 
625 /*
626  * nfs readlink call
627  */
628 nfs_readlink(vp, uiop, cred)
629 	struct vnode *vp;
630 	struct uio *uiop;
631 	struct ucred *cred;
632 {
633 	if (vp->v_type != VLNK)
634 		return (EPERM);
635 	return (nfs_bioread(vp, uiop, 0, cred));
636 }
637 
638 /*
639  * Do a readlink rpc.
640  * Called by nfs_doio() from below the buffer cache.
641  */
642 nfs_readlinkrpc(vp, uiop, cred, procp)
643 	register struct vnode *vp;
644 	struct uio *uiop;
645 	struct ucred *cred;
646 	struct proc *procp;
647 {
648 	register u_long *p;
649 	register caddr_t cp;
650 	register long t1;
651 	caddr_t bpos, dpos, cp2;
652 	u_long xid;
653 	int error = 0;
654 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
655 	long len;
656 
657 	nfsstats.rpccnt[NFSPROC_READLINK]++;
658 	nfs_unlock(vp);
659 	nfsm_reqhead(nfs_procids[NFSPROC_READLINK], cred, NFSX_FH);
660 	nfsm_fhtom(vp);
661 	nfsm_request(vp, NFSPROC_READLINK, procp, 0);
662 	nfsm_strsiz(len, NFS_MAXPATHLEN);
663 	nfsm_mtouio(uiop, len);
664 	nfsm_reqdone;
665 	nfs_lock(vp);
666 	return (error);
667 }
668 
669 /*
670  * nfs read rpc call
671  * Ditto above
672  */
673 nfs_readrpc(vp, uiop, cred, procp)
674 	register struct vnode *vp;
675 	struct uio *uiop;
676 	struct ucred *cred;
677 	struct proc *procp;
678 {
679 	register u_long *p;
680 	register caddr_t cp;
681 	register long t1;
682 	caddr_t bpos, dpos, cp2;
683 	u_long xid;
684 	int error = 0;
685 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
686 	struct nfsmount *nmp;
687 	long len, retlen, tsiz;
688 
689 	nmp = VFSTONFS(vp->v_mount);
690 	tsiz = uiop->uio_resid;
691 	while (tsiz > 0) {
692 		nfsstats.rpccnt[NFSPROC_READ]++;
693 		len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
694 		nfsm_reqhead(nfs_procids[NFSPROC_READ], cred, NFSX_FH+NFSX_UNSIGNED*3);
695 		nfsm_fhtom(vp);
696 		nfsm_build(p, u_long *, NFSX_UNSIGNED*3);
697 		*p++ = txdr_unsigned(uiop->uio_offset);
698 		*p++ = txdr_unsigned(len);
699 		*p = 0;
700 		nfsm_request(vp, NFSPROC_READ, procp, 1);
701 		nfsm_loadattr(vp, (struct vattr *)0);
702 		nfsm_strsiz(retlen, nmp->nm_rsize);
703 		nfsm_mtouio(uiop, retlen);
704 		m_freem(mrep);
705 		if (retlen < len)
706 			tsiz = 0;
707 		else
708 			tsiz -= len;
709 	}
710 nfsmout:
711 	return (error);
712 }
713 
714 /*
715  * nfs write call
716  */
717 nfs_writerpc(vp, uiop, cred, procp)
718 	register struct vnode *vp;
719 	struct uio *uiop;
720 	struct ucred *cred;
721 	struct proc *procp;
722 {
723 	register u_long *p;
724 	register caddr_t cp;
725 	register long t1;
726 	caddr_t bpos, dpos;
727 	u_long xid;
728 	int error = 0;
729 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
730 	struct nfsmount *nmp;
731 	long len, tsiz;
732 
733 	nmp = VFSTONFS(vp->v_mount);
734 	tsiz = uiop->uio_resid;
735 	while (tsiz > 0) {
736 		nfsstats.rpccnt[NFSPROC_WRITE]++;
737 		len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
738 		nfsm_reqhead(nfs_procids[NFSPROC_WRITE], cred,
739 			NFSX_FH+NFSX_UNSIGNED*4);
740 		nfsm_fhtom(vp);
741 		nfsm_build(p, u_long *, NFSX_UNSIGNED*4);
742 		*(p+1) = txdr_unsigned(uiop->uio_offset);
743 		*(p+3) = txdr_unsigned(len);
744 		nfsm_uiotom(uiop, len);
745 		nfsm_request(vp, NFSPROC_WRITE, procp, 1);
746 		nfsm_loadattr(vp, (struct vattr *)0);
747 		m_freem(mrep);
748 		tsiz -= len;
749 	}
750 nfsmout:
751 	return (error);
752 }
753 
754 /*
755  * nfs mknod call
756  * This is a kludge. Use a create rpc but with the IFMT bits of the mode
757  * set to specify the file type and the size field for rdev.
758  */
759 /* ARGSUSED */
760 nfs_mknod(ndp, vap, cred)
761 	struct nameidata *ndp;
762 	struct ucred *cred;
763 	register struct vattr *vap;
764 {
765 	register struct nfsv2_sattr *sp;
766 	register u_long *p;
767 	register caddr_t cp;
768 	register long t1, t2;
769 	caddr_t bpos, dpos;
770 	u_long xid;
771 	int error = 0;
772 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
773 	u_long rdev;
774 
775 	if (vap->va_type == VCHR || vap->va_type == VBLK)
776 		rdev = txdr_unsigned(vap->va_rdev);
777 #ifdef FIFO
778 	else if (vap->va_type == VFIFO)
779 		rdev = 0xffffffff;
780 #endif /* FIFO */
781 	else {
782 		VOP_ABORTOP(ndp);
783 		vput(ndp->ni_dvp);
784 		return (EOPNOTSUPP);
785 	}
786 	nfsstats.rpccnt[NFSPROC_CREATE]++;
787 	nfsm_reqhead(nfs_procids[NFSPROC_CREATE], ndp->ni_cred,
788 	  NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen)+NFSX_SATTR);
789 	nfsm_fhtom(ndp->ni_dvp);
790 	nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN);
791 	nfsm_build(sp, struct nfsv2_sattr *, NFSX_SATTR);
792 	sp->sa_mode = vtonfs_mode(vap->va_type, vap->va_mode);
793 	sp->sa_uid = txdr_unsigned(ndp->ni_cred->cr_uid);
794 	sp->sa_gid = txdr_unsigned(ndp->ni_cred->cr_gid);
795 	sp->sa_size = rdev;
796 	/* or should these be VNOVAL ?? */
797 	txdr_time(&vap->va_atime, &sp->sa_atime);
798 	txdr_time(&vap->va_mtime, &sp->sa_mtime);
799 	nfsm_request(ndp->ni_dvp, NFSPROC_CREATE, u.u_procp, 1);
800 	nfsm_reqdone;
801 	VTONFS(ndp->ni_dvp)->n_flag |= NMODIFIED;
802 	nfs_nput(ndp->ni_dvp);
803 	return (error);
804 }
805 
806 /*
807  * nfs file create call
808  */
809 nfs_create(ndp, vap)
810 	register struct nameidata *ndp;
811 	register struct vattr *vap;
812 {
813 	register struct nfsv2_sattr *sp;
814 	register u_long *p;
815 	register caddr_t cp;
816 	register long t1, t2;
817 	caddr_t bpos, dpos, cp2;
818 	u_long xid;
819 	int error = 0;
820 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
821 
822 	nfsstats.rpccnt[NFSPROC_CREATE]++;
823 	nfsm_reqhead(nfs_procids[NFSPROC_CREATE], ndp->ni_cred,
824 	  NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen)+NFSX_SATTR);
825 	nfsm_fhtom(ndp->ni_dvp);
826 	nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN);
827 	nfsm_build(sp, struct nfsv2_sattr *, NFSX_SATTR);
828 	sp->sa_mode = vtonfs_mode(VREG, vap->va_mode);
829 	sp->sa_uid = txdr_unsigned(ndp->ni_cred->cr_uid);
830 	sp->sa_gid = txdr_unsigned(ndp->ni_cred->cr_gid);
831 	sp->sa_size = txdr_unsigned(0);
832 	/* or should these be VNOVAL ?? */
833 	txdr_time(&vap->va_atime, &sp->sa_atime);
834 	txdr_time(&vap->va_mtime, &sp->sa_mtime);
835 	nfsm_request(ndp->ni_dvp, NFSPROC_CREATE, u.u_procp, 1);
836 	nfsm_mtofh(ndp->ni_dvp, ndp->ni_vp);
837 	nfsm_reqdone;
838 	VTONFS(ndp->ni_dvp)->n_flag |= NMODIFIED;
839 	nfs_nput(ndp->ni_dvp);
840 	return (error);
841 }
842 
843 /*
844  * nfs file remove call
845  * To try and make nfs semantics closer to ufs semantics, a file that has
846  * other processes using the vnode is renamed instead of removed and then
847  * removed later on the last close.
848  * - If v_usecount > 1
849  *	  If a rename is not already in the works
850  *	     call nfs_sillyrename() to set it up
851  *     else
852  *	  do the remove rpc
853  */
854 nfs_remove(ndp)
855 	register struct nameidata *ndp;
856 {
857 	register struct vnode *vp = ndp->ni_vp;
858 	register struct nfsnode *np = VTONFS(ndp->ni_vp);
859 	register u_long *p;
860 	register caddr_t cp;
861 	register long t1, t2;
862 	caddr_t bpos, dpos;
863 	u_long xid;
864 	int error = 0;
865 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
866 
867 	if (vp->v_usecount > 1) {
868 		if (!np->n_sillyrename)
869 			error = nfs_sillyrename(ndp, REMOVE);
870 	} else {
871 		nfsstats.rpccnt[NFSPROC_REMOVE]++;
872 		nfsm_reqhead(nfs_procids[NFSPROC_REMOVE], ndp->ni_cred,
873 			NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen));
874 		nfsm_fhtom(ndp->ni_dvp);
875 		nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN);
876 		nfsm_request(ndp->ni_dvp, NFSPROC_REMOVE, u.u_procp, 1);
877 		nfsm_reqdone;
878 		VTONFS(ndp->ni_dvp)->n_flag |= NMODIFIED;
879 		/*
880 		 * Kludge City: If the first reply to the remove rpc is lost..
881 		 *   the reply to the retransmitted request will be ENOENT
882 		 *   since the file was in fact removed
883 		 *   Therefore, we cheat and return success.
884 		 */
885 		if (error == ENOENT)
886 			error = 0;
887 	}
888 	np->n_attrstamp = 0;
889 	if (ndp->ni_dvp == vp)
890 		vrele(vp);
891 	else
892 		nfs_nput(ndp->ni_dvp);
893 	nfs_nput(vp);
894 	return (error);
895 }
896 
897 /*
898  * nfs file remove rpc called from nfs_inactive
899  */
900 nfs_removeit(ndp)
901 	register struct nameidata *ndp;
902 {
903 	register u_long *p;
904 	register caddr_t cp;
905 	register long t1, t2;
906 	caddr_t bpos, dpos;
907 	u_long xid;
908 	int error = 0;
909 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
910 
911 	nfsstats.rpccnt[NFSPROC_REMOVE]++;
912 	nfsm_reqhead(nfs_procids[NFSPROC_REMOVE], ndp->ni_cred,
913 		NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen));
914 	nfsm_fhtom(ndp->ni_dvp);
915 	nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN);
916 	nfsm_request(ndp->ni_dvp, NFSPROC_REMOVE, u.u_procp, 1);
917 	nfsm_reqdone;
918 	VTONFS(ndp->ni_dvp)->n_flag |= NMODIFIED;
919 	return (error);
920 }
921 
922 /*
923  * nfs file rename call
924  */
925 nfs_rename(sndp, tndp)
926 	register struct nameidata *sndp, *tndp;
927 {
928 	register u_long *p;
929 	register caddr_t cp;
930 	register long t1, t2;
931 	caddr_t bpos, dpos;
932 	u_long xid;
933 	int error = 0;
934 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
935 
936 	nfsstats.rpccnt[NFSPROC_RENAME]++;
937 	nfsm_reqhead(nfs_procids[NFSPROC_RENAME], tndp->ni_cred,
938 		(NFSX_FH+NFSX_UNSIGNED)*2+nfsm_rndup(sndp->ni_dent.d_namlen)+
939 		nfsm_rndup(tndp->ni_dent.d_namlen)); /* or sndp->ni_cred?*/
940 	nfsm_fhtom(sndp->ni_dvp);
941 	nfsm_strtom(sndp->ni_dent.d_name,sndp->ni_dent.d_namlen,NFS_MAXNAMLEN);
942 	nfsm_fhtom(tndp->ni_dvp);
943 	nfsm_strtom(tndp->ni_dent.d_name,tndp->ni_dent.d_namlen,NFS_MAXNAMLEN);
944 	nfsm_request(sndp->ni_dvp, NFSPROC_RENAME, u.u_procp, 1);
945 	nfsm_reqdone;
946 	VTONFS(sndp->ni_dvp)->n_flag |= NMODIFIED;
947 	VTONFS(tndp->ni_dvp)->n_flag |= NMODIFIED;
948 	if (sndp->ni_vp->v_type == VDIR) {
949 		if (tndp->ni_vp != NULL && tndp->ni_vp->v_type == VDIR)
950 			cache_purge(tndp->ni_dvp);
951 		cache_purge(sndp->ni_dvp);
952 	}
953 	VOP_ABORTOP(tndp);
954 	vput(tndp->ni_dvp);
955 	if (tndp->ni_vp)
956 		vput(tndp->ni_vp);
957 	VOP_ABORTOP(sndp);
958 	vrele(sndp->ni_dvp);
959 	vrele(sndp->ni_vp);
960 	/*
961 	 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
962 	 */
963 	if (error == ENOENT)
964 		error = 0;
965 	return (error);
966 }
967 
968 /*
969  * nfs file rename rpc called from nfs_remove() above
970  */
971 nfs_renameit(sndp, tndp)
972 	register struct nameidata *sndp, *tndp;
973 {
974 	register u_long *p;
975 	register caddr_t cp;
976 	register long t1, t2;
977 	caddr_t bpos, dpos;
978 	u_long xid;
979 	int error = 0;
980 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
981 
982 	nfsstats.rpccnt[NFSPROC_RENAME]++;
983 	nfsm_reqhead(nfs_procids[NFSPROC_RENAME], tndp->ni_cred,
984 		(NFSX_FH+NFSX_UNSIGNED)*2+nfsm_rndup(sndp->ni_dent.d_namlen)+
985 		nfsm_rndup(tndp->ni_dent.d_namlen)); /* or sndp->ni_cred?*/
986 	nfsm_fhtom(sndp->ni_dvp);
987 	nfsm_strtom(sndp->ni_dent.d_name,sndp->ni_dent.d_namlen,NFS_MAXNAMLEN);
988 	nfsm_fhtom(tndp->ni_dvp);
989 	nfsm_strtom(tndp->ni_dent.d_name,tndp->ni_dent.d_namlen,NFS_MAXNAMLEN);
990 	nfsm_request(sndp->ni_dvp, NFSPROC_RENAME, u.u_procp, 1);
991 	nfsm_reqdone;
992 	VTONFS(sndp->ni_dvp)->n_flag |= NMODIFIED;
993 	VTONFS(tndp->ni_dvp)->n_flag |= NMODIFIED;
994 	return (error);
995 }
996 
997 /*
998  * nfs hard link create call
999  */
1000 nfs_link(vp, ndp)
1001 	register struct vnode *vp;
1002 	register struct nameidata *ndp;
1003 {
1004 	register u_long *p;
1005 	register caddr_t cp;
1006 	register long t1, t2;
1007 	caddr_t bpos, dpos;
1008 	u_long xid;
1009 	int error = 0;
1010 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1011 
1012 	if (ndp->ni_dvp != vp)
1013 		nfs_lock(vp);
1014 	nfsstats.rpccnt[NFSPROC_LINK]++;
1015 	nfsm_reqhead(nfs_procids[NFSPROC_LINK], ndp->ni_cred,
1016 		NFSX_FH*2+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen));
1017 	nfsm_fhtom(vp);
1018 	nfsm_fhtom(ndp->ni_dvp);
1019 	nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN);
1020 	nfsm_request(vp, NFSPROC_LINK, u.u_procp, 1);
1021 	nfsm_reqdone;
1022 	VTONFS(vp)->n_attrstamp = 0;
1023 	VTONFS(ndp->ni_dvp)->n_flag |= NMODIFIED;
1024 	if (ndp->ni_dvp != vp)
1025 		nfs_unlock(vp);
1026 	nfs_nput(ndp->ni_dvp);
1027 	/*
1028 	 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1029 	 */
1030 	if (error == EEXIST)
1031 		error = 0;
1032 	return (error);
1033 }
1034 
1035 /*
1036  * nfs symbolic link create call
1037  */
1038 nfs_symlink(ndp, vap, nm)
1039 	struct nameidata *ndp;
1040 	struct vattr *vap;
1041 	char *nm;		/* is this the path ?? */
1042 {
1043 	register struct nfsv2_sattr *sp;
1044 	register u_long *p;
1045 	register caddr_t cp;
1046 	register long t1, t2;
1047 	caddr_t bpos, dpos;
1048 	u_long xid;
1049 	int error = 0;
1050 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1051 
1052 	nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1053 	nfsm_reqhead(nfs_procids[NFSPROC_SYMLINK], ndp->ni_cred,
1054 	NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen)+NFSX_UNSIGNED);
1055 	nfsm_fhtom(ndp->ni_dvp);
1056 	nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN);
1057 	nfsm_strtom(nm, strlen(nm), NFS_MAXPATHLEN);
1058 	nfsm_build(sp, struct nfsv2_sattr *, NFSX_SATTR);
1059 	sp->sa_mode = vtonfs_mode(VLNK, vap->va_mode);
1060 	sp->sa_uid = txdr_unsigned(ndp->ni_cred->cr_uid);
1061 	sp->sa_gid = txdr_unsigned(ndp->ni_cred->cr_gid);
1062 	sp->sa_size = txdr_unsigned(VNOVAL);
1063 	txdr_time(&vap->va_atime, &sp->sa_atime);	/* or VNOVAL ?? */
1064 	txdr_time(&vap->va_mtime, &sp->sa_mtime);	/* or VNOVAL ?? */
1065 	nfsm_request(ndp->ni_dvp, NFSPROC_SYMLINK, u.u_procp, 1);
1066 	nfsm_reqdone;
1067 	VTONFS(ndp->ni_dvp)->n_flag |= NMODIFIED;
1068 	nfs_nput(ndp->ni_dvp);
1069 	/*
1070 	 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1071 	 */
1072 	if (error == EEXIST)
1073 		error = 0;
1074 	return (error);
1075 }
1076 
1077 /*
1078  * nfs make dir call
1079  */
1080 nfs_mkdir(ndp, vap)
1081 	register struct nameidata *ndp;
1082 	struct vattr *vap;
1083 {
1084 	register struct nfsv2_sattr *sp;
1085 	register u_long *p;
1086 	register caddr_t cp;
1087 	register long t1, t2;
1088 	register int len;
1089 	caddr_t bpos, dpos, cp2;
1090 	u_long xid;
1091 	int error = 0, firsttry = 1;
1092 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1093 
1094 	len = ndp->ni_dent.d_namlen;
1095 	nfsstats.rpccnt[NFSPROC_MKDIR]++;
1096 	nfsm_reqhead(nfs_procids[NFSPROC_MKDIR], ndp->ni_cred,
1097 	  NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(len)+NFSX_SATTR);
1098 	nfsm_fhtom(ndp->ni_dvp);
1099 	nfsm_strtom(ndp->ni_dent.d_name, len, NFS_MAXNAMLEN);
1100 	nfsm_build(sp, struct nfsv2_sattr *, NFSX_SATTR);
1101 	sp->sa_mode = vtonfs_mode(VDIR, vap->va_mode);
1102 	sp->sa_uid = txdr_unsigned(ndp->ni_cred->cr_uid);
1103 	sp->sa_gid = txdr_unsigned(ndp->ni_cred->cr_gid);
1104 	sp->sa_size = txdr_unsigned(VNOVAL);
1105 	txdr_time(&vap->va_atime, &sp->sa_atime);	/* or VNOVAL ?? */
1106 	txdr_time(&vap->va_mtime, &sp->sa_mtime);	/* or VNOVAL ?? */
1107 	nfsm_request(ndp->ni_dvp, NFSPROC_MKDIR, u.u_procp, 1);
1108 	nfsm_mtofh(ndp->ni_dvp, ndp->ni_vp);
1109 	nfsm_reqdone;
1110 	VTONFS(ndp->ni_dvp)->n_flag |= NMODIFIED;
1111 	/*
1112 	 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
1113 	 * if we can succeed in looking up the directory.
1114 	 * "firsttry" is necessary since the macros may "goto nfsmout" which
1115 	 * is above the if on errors. (Ugh)
1116 	 */
1117 	if (error == EEXIST && firsttry) {
1118 		firsttry = 0;
1119 		error = 0;
1120 		nfsstats.rpccnt[NFSPROC_LOOKUP]++;
1121 		ndp->ni_vp = NULL;
1122 		nfsm_reqhead(nfs_procids[NFSPROC_LOOKUP], ndp->ni_cred,
1123 		    NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(len));
1124 		nfsm_fhtom(ndp->ni_dvp);
1125 		nfsm_strtom(ndp->ni_dent.d_name, len, NFS_MAXNAMLEN);
1126 		nfsm_request(ndp->ni_dvp, NFSPROC_LOOKUP, u.u_procp, 1);
1127 		nfsm_mtofh(ndp->ni_dvp, ndp->ni_vp);
1128 		if (ndp->ni_vp->v_type != VDIR) {
1129 			vput(ndp->ni_vp);
1130 			error = EEXIST;
1131 		}
1132 		m_freem(mrep);
1133 	}
1134 	nfs_nput(ndp->ni_dvp);
1135 	return (error);
1136 }
1137 
1138 /*
1139  * nfs remove directory call
1140  */
1141 nfs_rmdir(ndp)
1142 	register struct nameidata *ndp;
1143 {
1144 	register u_long *p;
1145 	register caddr_t cp;
1146 	register long t1, t2;
1147 	caddr_t bpos, dpos;
1148 	u_long xid;
1149 	int error = 0;
1150 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1151 
1152 	if (ndp->ni_dvp == ndp->ni_vp) {
1153 		vrele(ndp->ni_dvp);
1154 		nfs_nput(ndp->ni_dvp);
1155 		return (EINVAL);
1156 	}
1157 	nfsstats.rpccnt[NFSPROC_RMDIR]++;
1158 	nfsm_reqhead(nfs_procids[NFSPROC_RMDIR], ndp->ni_cred,
1159 		NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen));
1160 	nfsm_fhtom(ndp->ni_dvp);
1161 	nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN);
1162 	nfsm_request(ndp->ni_dvp, NFSPROC_RMDIR, u.u_procp, 1);
1163 	nfsm_reqdone;
1164 	VTONFS(ndp->ni_dvp)->n_flag |= NMODIFIED;
1165 	cache_purge(ndp->ni_dvp);
1166 	cache_purge(ndp->ni_vp);
1167 	nfs_nput(ndp->ni_vp);
1168 	nfs_nput(ndp->ni_dvp);
1169 	/*
1170 	 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
1171 	 */
1172 	if (error == ENOENT)
1173 		error = 0;
1174 	return (error);
1175 }
1176 
1177 /*
1178  * nfs readdir call
1179  * Although cookie is defined as opaque, I translate it to/from net byte
1180  * order so that it looks more sensible. This appears consistent with the
1181  * Ultrix implementation of NFS.
1182  */
1183 nfs_readdir(vp, uiop, cred, eofflagp)
1184 	register struct vnode *vp;
1185 	struct uio *uiop;
1186 	struct ucred *cred;
1187 	int *eofflagp;
1188 {
1189 	register struct nfsnode *np = VTONFS(vp);
1190 	int tresid, error;
1191 	struct vattr vattr;
1192 
1193 	if (vp->v_type != VDIR)
1194 		return (EPERM);
1195 	/*
1196 	 * First, check for hit on the EOF offset cache
1197 	 */
1198 	if (uiop->uio_offset != 0 && uiop->uio_offset == np->n_direofoffset &&
1199 	    (np->n_flag & NMODIFIED) == 0 &&
1200 	    nfs_dogetattr(vp, &vattr, cred, 0) == 0 &&
1201 	    np->n_mtime == vattr.va_mtime.tv_sec) {
1202 		*eofflagp = 1;
1203 		nfsstats.direofcache_hits++;
1204 		return (0);
1205 	}
1206 
1207 	/*
1208 	 * Call nfs_bioread() to do the real work.
1209 	 */
1210 	tresid = uiop->uio_resid;
1211 	error = nfs_bioread(vp, uiop, 0, cred);
1212 
1213 	if (!error && uiop->uio_resid == tresid) {
1214 		*eofflagp = 1;
1215 		nfsstats.direofcache_misses++;
1216 	} else
1217 		*eofflagp = 0;
1218 	return (error);
1219 }
1220 
1221 /*
1222  * Readdir rpc call.
1223  * Called from below the buffer cache by nfs_doio().
1224  */
1225 nfs_readdirrpc(vp, uiop, cred, procp)
1226 	register struct vnode *vp;
1227 	struct uio *uiop;
1228 	struct ucred *cred;
1229 	struct proc *procp;
1230 {
1231 	register long len;
1232 	register struct direct *dp;
1233 	register u_long *p;
1234 	register caddr_t cp;
1235 	register long t1;
1236 	long tlen, lastlen;
1237 	caddr_t bpos, dpos, cp2;
1238 	u_long xid;
1239 	int error = 0;
1240 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1241 	struct mbuf *md2;
1242 	caddr_t dpos2;
1243 	int siz;
1244 	int more_dirs = 1;
1245 	off_t off, savoff;
1246 	struct direct *savdp;
1247 	struct nfsmount *nmp;
1248 	struct nfsnode *np = VTONFS(vp);
1249 	long tresid;
1250 
1251 	nmp = VFSTONFS(vp->v_mount);
1252 	tresid = uiop->uio_resid;
1253 	/*
1254 	 * Loop around doing readdir rpc's of size uio_resid or nm_rsize,
1255 	 * whichever is smaller, truncated to a multiple of DIRBLKSIZ.
1256 	 * The stopping criteria is EOF or buffer full.
1257 	 */
1258 	while (more_dirs && uiop->uio_resid >= DIRBLKSIZ) {
1259 		nfsstats.rpccnt[NFSPROC_READDIR]++;
1260 		nfsm_reqhead(nfs_procids[NFSPROC_READDIR], cred, xid);
1261 		nfsm_fhtom(vp);
1262 		nfsm_build(p, u_long *, 2*NFSX_UNSIGNED);
1263 		*p++ = txdr_unsigned(uiop->uio_offset);
1264 		*p = txdr_unsigned(((uiop->uio_resid > nmp->nm_rsize) ?
1265 			nmp->nm_rsize : uiop->uio_resid) & ~(DIRBLKSIZ-1));
1266 		nfsm_request(vp, NFSPROC_READDIR, procp, 0);
1267 		siz = 0;
1268 		nfsm_disect(p, u_long *, NFSX_UNSIGNED);
1269 		more_dirs = fxdr_unsigned(int, *p);
1270 
1271 		/* Save the position so that we can do nfsm_mtouio() later */
1272 		dpos2 = dpos;
1273 		md2 = md;
1274 
1275 		/* loop thru the dir entries, doctoring them to 4bsd form */
1276 		off = uiop->uio_offset;
1277 #ifdef lint
1278 		dp = (struct direct *)0;
1279 #endif /* lint */
1280 		while (more_dirs && siz < uiop->uio_resid) {
1281 			savoff = off;		/* Hold onto offset and dp */
1282 			savdp = dp;
1283 			nfsm_disecton(p, u_long *, 2*NFSX_UNSIGNED);
1284 			dp = (struct direct *)p;
1285 			dp->d_ino = fxdr_unsigned(u_long, *p++);
1286 			len = fxdr_unsigned(int, *p);
1287 			if (len <= 0 || len > NFS_MAXNAMLEN) {
1288 				error = EBADRPC;
1289 				m_freem(mrep);
1290 				goto nfsmout;
1291 			}
1292 			dp->d_namlen = (u_short)len;
1293 			nfsm_adv(len);		/* Point past name */
1294 			tlen = nfsm_rndup(len);
1295 			/*
1296 			 * This should not be necessary, but some servers have
1297 			 * broken XDR such that these bytes are not null filled.
1298 			 */
1299 			if (tlen != len) {
1300 				*dpos = '\0';	/* Null-terminate */
1301 				nfsm_adv(tlen - len);
1302 				len = tlen;
1303 			}
1304 			nfsm_disecton(p, u_long *, 2*NFSX_UNSIGNED);
1305 			off = fxdr_unsigned(off_t, *p);
1306 			*p++ = 0;	/* Ensures null termination of name */
1307 			more_dirs = fxdr_unsigned(int, *p);
1308 			dp->d_reclen = len+4*NFSX_UNSIGNED;
1309 			siz += dp->d_reclen;
1310 		}
1311 		/*
1312 		 * If at end of rpc data, get the eof boolean
1313 		 */
1314 		if (!more_dirs) {
1315 			nfsm_disecton(p, u_long *, NFSX_UNSIGNED);
1316 			more_dirs = (fxdr_unsigned(int, *p) == 0);
1317 
1318 			/*
1319 			 * If at EOF, cache directory offset
1320 			 */
1321 			if (!more_dirs)
1322 				np->n_direofoffset = off;
1323 		}
1324 		/*
1325 		 * If there is too much to fit in the data buffer, use savoff and
1326 		 * savdp to trim off the last record.
1327 		 * --> we are not at eof
1328 		 */
1329 		if (siz > uiop->uio_resid) {
1330 			off = savoff;
1331 			siz -= dp->d_reclen;
1332 			dp = savdp;
1333 			more_dirs = 0;	/* Paranoia */
1334 		}
1335 		if (siz > 0) {
1336 			lastlen = dp->d_reclen;
1337 			md = md2;
1338 			dpos = dpos2;
1339 			nfsm_mtouio(uiop, siz);
1340 			uiop->uio_offset = off;
1341 		} else
1342 			more_dirs = 0;	/* Ugh, never happens, but in case.. */
1343 		m_freem(mrep);
1344 	}
1345 	/*
1346 	 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
1347 	 * by increasing d_reclen for the last record.
1348 	 */
1349 	if (uiop->uio_resid < tresid) {
1350 		len = uiop->uio_resid & (DIRBLKSIZ - 1);
1351 		if (len > 0) {
1352 			dp = (struct direct *)
1353 				(uiop->uio_iov->iov_base - lastlen);
1354 			dp->d_reclen += len;
1355 			uiop->uio_iov->iov_base += len;
1356 			uiop->uio_iov->iov_len -= len;
1357 			uiop->uio_resid -= len;
1358 		}
1359 	}
1360 nfsmout:
1361 	return (error);
1362 }
1363 
1364 static char hextoasc[] = "0123456789abcdef";
1365 
1366 /*
1367  * Silly rename. To make the NFS filesystem that is stateless look a little
1368  * more like the "ufs" a remove of an active vnode is translated to a rename
1369  * to a funny looking filename that is removed by nfs_inactive on the
1370  * nfsnode. There is the potential for another process on a different client
1371  * to create the same funny name between the nfs_lookitup() fails and the
1372  * nfs_rename() completes, but...
1373  */
1374 nfs_sillyrename(ndp, flag)
1375 	register struct nameidata *ndp;
1376 	int flag;
1377 {
1378 	register struct nfsnode *np;
1379 	register struct sillyrename *sp;
1380 	register struct nameidata *tndp;
1381 	int error;
1382 	short pid;
1383 
1384 	np = VTONFS(ndp->ni_dvp);
1385 	cache_purge(ndp->ni_dvp);
1386 	MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
1387 		M_TEMP, M_WAITOK);
1388 	sp->s_flag = flag;
1389 	bcopy((caddr_t)&np->n_fh, (caddr_t)&sp->s_fh, NFSX_FH);
1390 	np = VTONFS(ndp->ni_vp);
1391 	tndp = &sp->s_namei;
1392 	tndp->ni_cred = crdup(ndp->ni_cred);
1393 
1394 	/* Fudge together a funny name */
1395 	pid = u.u_procp->p_pid;
1396 	bcopy(".nfsAxxxx4.4", tndp->ni_dent.d_name, 13);
1397 	tndp->ni_dent.d_namlen = 12;
1398 	tndp->ni_dent.d_name[8] = hextoasc[pid & 0xf];
1399 	tndp->ni_dent.d_name[7] = hextoasc[(pid >> 4) & 0xf];
1400 	tndp->ni_dent.d_name[6] = hextoasc[(pid >> 8) & 0xf];
1401 	tndp->ni_dent.d_name[5] = hextoasc[(pid >> 12) & 0xf];
1402 
1403 	/* Try lookitups until we get one that isn't there */
1404 	while (nfs_lookitup(ndp->ni_dvp, tndp, (nfsv2fh_t *)0) == 0) {
1405 		tndp->ni_dent.d_name[4]++;
1406 		if (tndp->ni_dent.d_name[4] > 'z') {
1407 			error = EINVAL;
1408 			goto bad;
1409 		}
1410 	}
1411 	if (error = nfs_renameit(ndp, tndp))
1412 		goto bad;
1413 	nfs_lookitup(ndp->ni_dvp, tndp, &np->n_fh);
1414 	np->n_sillyrename = sp;
1415 	return (0);
1416 bad:
1417 	crfree(tndp->ni_cred);
1418 	free((caddr_t)sp, M_TEMP);
1419 	return (error);
1420 }
1421 
1422 /*
1423  * Look up a file name for silly rename stuff.
1424  * Just like nfs_lookup() except that it doesn't load returned values
1425  * into the nfsnode table.
1426  * If fhp != NULL it copies the returned file handle out
1427  */
1428 nfs_lookitup(vp, ndp, fhp)
1429 	register struct vnode *vp;
1430 	register struct nameidata *ndp;
1431 	nfsv2fh_t *fhp;
1432 {
1433 	register u_long *p;
1434 	register caddr_t cp;
1435 	register long t1, t2;
1436 	caddr_t bpos, dpos, cp2;
1437 	u_long xid;
1438 	int error = 0;
1439 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1440 	long len;
1441 
1442 	nfsstats.rpccnt[NFSPROC_LOOKUP]++;
1443 	ndp->ni_dvp = vp;
1444 	ndp->ni_vp = NULL;
1445 	len = ndp->ni_dent.d_namlen;
1446 	nfsm_reqhead(nfs_procids[NFSPROC_LOOKUP], ndp->ni_cred, NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(len));
1447 	nfsm_fhtom(vp);
1448 	nfsm_strtom(ndp->ni_dent.d_name, len, NFS_MAXNAMLEN);
1449 	nfsm_request(vp, NFSPROC_LOOKUP, u.u_procp, 1);
1450 	if (fhp != NULL) {
1451 		nfsm_disect(cp, caddr_t, NFSX_FH);
1452 		bcopy(cp, (caddr_t)fhp, NFSX_FH);
1453 	}
1454 	nfsm_reqdone;
1455 	return (error);
1456 }
1457 
1458 /*
1459  * Kludge City..
1460  * - make nfs_bmap() essentially a no-op that does no translation
1461  * - do nfs_strategy() by faking physical I/O with nfs_readrpc/nfs_writerpc
1462  *   after mapping the physical addresses into Kernel Virtual space in the
1463  *   nfsiobuf area.
1464  *   (Maybe I could use the process's page mapping, but I was concerned that
1465  *    Kernel Write might not be enabled and also figured copyout() would do
1466  *    a lot more work than bcopy() and also it currently happens in the
1467  *    context of the swapper process (2).
1468  */
1469 nfs_bmap(vp, bn, vpp, bnp)
1470 	struct vnode *vp;
1471 	daddr_t bn;
1472 	struct vnode **vpp;
1473 	daddr_t *bnp;
1474 {
1475 	if (vpp != NULL)
1476 		*vpp = vp;
1477 	if (bnp != NULL)
1478 		*bnp = bn * btodb(vp->v_mount->mnt_stat.f_bsize);
1479 	return (0);
1480 }
1481 
1482 /*
1483  * Strategy routine for phys. i/o
1484  * If the biod's are running, queue a request
1485  * otherwise just call nfs_doio() to get it done
1486  */
1487 nfs_strategy(bp)
1488 	register struct buf *bp;
1489 {
1490 	register struct buf *dp;
1491 	register int i;
1492 	struct proc *rp;
1493 	int error = 0;
1494 	int fnd = 0;
1495 
1496 	/*
1497 	 * Set b_proc. It seems a bit silly to do it here, but since bread()
1498 	 * doesn't set it, I will.
1499 	 * Set b_proc == NULL for asynchronous reads, since these may still
1500 	 * be hanging about after the process terminates.
1501 	 */
1502 	if ((bp->b_flags & (B_READ | B_ASYNC)) == (B_READ | B_ASYNC))
1503 		bp->b_proc = (struct proc *)0;
1504 	else
1505 		bp->b_proc = u.u_procp;
1506 
1507 	/*
1508 	 * If an i/o daemon is waiting
1509 	 * queue the request, wake it up and wait for completion
1510 	 * otherwise just do it ourselves
1511 	 */
1512 	for (i = 0; i < nfs_asyncdaemons; i++) {
1513 		if (rp = nfs_iodwant[i]) {
1514 			/*
1515 			 * Ensure that the async_daemon is still waiting here
1516 			 */
1517 			if (rp->p_stat != SSLEEP ||
1518 			    rp->p_wchan != ((caddr_t)&nfs_iodwant[i])) {
1519 				nfs_iodwant[i] = (struct proc *)0;
1520 				continue;
1521 			}
1522 			dp = &nfs_bqueue;
1523 			if (dp->b_actf == NULL) {
1524 				dp->b_actl = bp;
1525 				bp->b_actf = dp;
1526 			} else {
1527 				dp->b_actf->b_actl = bp;
1528 				bp->b_actf = dp->b_actf;
1529 			}
1530 			dp->b_actf = bp;
1531 			bp->b_actl = dp;
1532 			fnd++;
1533 			nfs_iodwant[i] = (struct proc *)0;
1534 			wakeup((caddr_t)&nfs_iodwant[i]);
1535 			break;
1536 		}
1537 	}
1538 	if (!fnd)
1539 		error = nfs_doio(bp);
1540 	return (error);
1541 }
1542 
1543 /*
1544  * Fun and games with i/o
1545  * Essentially play ubasetup() and disk interrupt service routine by
1546  * mapping the data buffer into kernel virtual space and doing the
1547  * nfs read or write rpc's from it.
1548  * If the nfsiod's are not running, this is just called from nfs_strategy(),
1549  * otherwise it is called by the nfsiods to do what would normally be
1550  * partially disk interrupt driven.
1551  */
1552 nfs_doio(bp)
1553 	register struct buf *bp;
1554 {
1555 	register struct uio *uiop;
1556 	register struct vnode *vp;
1557 	struct nfsnode *np;
1558 	struct ucred *cr;
1559 	struct proc *rp;
1560 	int error;
1561 	struct uio uio;
1562 	struct iovec io;
1563 #if !defined(hp300)
1564 	register struct pte *pte, *ppte;
1565 	register caddr_t vaddr;
1566 	int npf, npf2;
1567 	int reg, o;
1568 	caddr_t vbase;
1569 	unsigned v;
1570 #endif
1571 
1572 	vp = bp->b_vp;
1573 	np = VTONFS(vp);
1574 	uiop = &uio;
1575 	uiop->uio_iov = &io;
1576 	uiop->uio_iovcnt = 1;
1577 	uiop->uio_segflg = UIO_SYSSPACE;
1578 
1579 	/*
1580 	 * For phys i/o, map the b_addr into kernel virtual space using
1581 	 * the Nfsiomap pte's
1582 	 * Also, add a temporary b_rcred for reading using the process's uid
1583 	 * and a guess at a group
1584 	 */
1585 	if (bp->b_flags & B_PHYS) {
1586 		bp->b_rcred = cr = crget();
1587 		rp = (bp->b_flags & B_DIRTY) ? &proc[2] : bp->b_proc;
1588 		cr->cr_uid = rp->p_uid;
1589 		cr->cr_gid = 0;		/* Anything ?? */
1590 		cr->cr_ngroups = 1;
1591 #if defined(hp300)
1592 		/* mapping was already done by vmapbuf */
1593 		io.iov_base = bp->b_un.b_addr;
1594 #else
1595 		o = (int)bp->b_un.b_addr & PGOFSET;
1596 		npf2 = npf = btoc(bp->b_bcount + o);
1597 
1598 		/*
1599 		 * Get some mapping page table entries
1600 		 */
1601 		while ((reg = rmalloc(nfsmap, (long)npf)) == 0) {
1602 			nfsmap_want++;
1603 			(void) tsleep((caddr_t)&nfsmap_want, PZERO-1, "nfsmap",
1604 					0);
1605 		}
1606 		reg--;
1607 		if (bp->b_flags & B_PAGET)
1608 			pte = &Usrptmap[btokmx((struct pte *)bp->b_un.b_addr)];
1609 		else {
1610 			v = btop(bp->b_un.b_addr);
1611 			if (bp->b_flags & B_UAREA)
1612 				pte = &rp->p_addr[v];
1613 			else
1614 				pte = vtopte(rp, v);
1615 		}
1616 
1617 		/*
1618 		 * Play vmaccess() but with the Nfsiomap page table
1619 		 */
1620 		ppte = &Nfsiomap[reg];
1621 		vbase = vaddr = &nfsiobuf[reg*NBPG];
1622 		while (npf != 0) {
1623 			mapin(ppte, (u_int)vaddr, pte->pg_pfnum, (int)(PG_V|PG_KW));
1624 #if defined(tahoe)
1625 			mtpr(P1DC, vaddr);
1626 #endif
1627 			ppte++;
1628 			pte++;
1629 			vaddr += NBPG;
1630 			--npf;
1631 		}
1632 		io.iov_base = vbase+o;
1633 #endif /* !defined(hp300) */
1634 
1635 		/*
1636 		 * And do the i/o rpc
1637 		 */
1638 		io.iov_len = uiop->uio_resid = bp->b_bcount;
1639 		uiop->uio_offset = bp->b_blkno * DEV_BSIZE;
1640 		if (bp->b_flags & B_READ) {
1641 			uiop->uio_rw = UIO_READ;
1642 			nfsstats.read_physios++;
1643 			bp->b_error = error = nfs_readrpc(vp, uiop,
1644 				bp->b_rcred, bp->b_proc);
1645 			/*
1646 			 * If a text file has been modified since it was exec'd
1647 			 * blow the process' out of the water. This is the
1648 			 * closest we can come to "Text File Busy" in good old
1649 			 * stateless nfs.
1650 			 */
1651 			if ((vp->v_flag & VTEXT) &&
1652 			    (vp->v_text->x_mtime != np->n_vattr.va_mtime.tv_sec))
1653 				xinval(vp);
1654 		} else {
1655 			uiop->uio_rw = UIO_WRITE;
1656 			nfsstats.write_physios++;
1657 			bp->b_error = error = nfs_writerpc(vp, uiop,
1658 				bp->b_wcred, bp->b_proc);
1659 		}
1660 
1661 		/*
1662 		 * Finally, release pte's used by physical i/o
1663 		 */
1664 		crfree(cr);
1665 #if !defined(hp300)
1666 		rmfree(nfsmap, (long)npf2, (long)++reg);
1667 		if (nfsmap_want) {
1668 			nfsmap_want = 0;
1669 			wakeup((caddr_t)&nfsmap_want);
1670 		}
1671 #endif
1672 	} else {
1673 		if (bp->b_flags & B_READ) {
1674 			io.iov_len = uiop->uio_resid = bp->b_bcount;
1675 			io.iov_base = bp->b_un.b_addr;
1676 			uiop->uio_rw = UIO_READ;
1677 			switch (vp->v_type) {
1678 			case VREG:
1679 				uiop->uio_offset = bp->b_blkno * DEV_BSIZE;
1680 				nfsstats.read_bios++;
1681 				error = nfs_readrpc(vp, uiop, bp->b_rcred,
1682 					bp->b_proc);
1683 				break;
1684 			case VLNK:
1685 				uiop->uio_offset = 0;
1686 				nfsstats.readlink_bios++;
1687 				error = nfs_readlinkrpc(vp, uiop, bp->b_rcred,
1688 						bp->b_proc);
1689 				break;
1690 			case VDIR:
1691 				uiop->uio_offset = bp->b_lblkno;
1692 				nfsstats.readdir_bios++;
1693 				error = nfs_readdirrpc(vp, uiop, bp->b_rcred,
1694 					    bp->b_proc);
1695 				/*
1696 				 * Save offset cookie in b_blkno.
1697 				 */
1698 				bp->b_blkno = uiop->uio_offset;
1699 				break;
1700 			};
1701 			bp->b_error = error;
1702 		} else {
1703 			io.iov_len = uiop->uio_resid = bp->b_dirtyend
1704 				- bp->b_dirtyoff;
1705 			uiop->uio_offset = (bp->b_blkno * DEV_BSIZE)
1706 				+ bp->b_dirtyoff;
1707 			io.iov_base = bp->b_un.b_addr + bp->b_dirtyoff;
1708 			uiop->uio_rw = UIO_WRITE;
1709 			nfsstats.write_bios++;
1710 			bp->b_error = error = nfs_writerpc(vp, uiop,
1711 				bp->b_wcred, bp->b_proc);
1712 			if (error) {
1713 				np->n_error = error;
1714 				np->n_flag |= NWRITEERR;
1715 			}
1716 			bp->b_dirtyoff = bp->b_dirtyend = 0;
1717 		}
1718 	}
1719 	if (error)
1720 		bp->b_flags |= B_ERROR;
1721 	bp->b_resid = uiop->uio_resid;
1722 	biodone(bp);
1723 	return (error);
1724 }
1725 
1726 /*
1727  * Flush all the blocks associated with a vnode.
1728  * 	Walk through the buffer pool and push any dirty pages
1729  *	associated with the vnode.
1730  */
1731 /* ARGSUSED */
1732 nfs_fsync(vp, fflags, cred, waitfor)
1733 	register struct vnode *vp;
1734 	int fflags;
1735 	struct ucred *cred;
1736 	int waitfor;
1737 {
1738 	register struct nfsnode *np = VTONFS(vp);
1739 	int error = 0;
1740 
1741 	if (np->n_flag & NMODIFIED) {
1742 		np->n_flag &= ~NMODIFIED;
1743 		vflushbuf(vp, waitfor == MNT_WAIT ? B_SYNC : 0);
1744 	}
1745 	if (!error && (np->n_flag & NWRITEERR))
1746 		error = np->n_error;
1747 	return (error);
1748 }
1749 
1750 /*
1751  * Print out the contents of an nfsnode.
1752  */
1753 nfs_print(vp)
1754 	struct vnode *vp;
1755 {
1756 	register struct nfsnode *np = VTONFS(vp);
1757 
1758 	printf("tag VT_NFS, fileid %d fsid 0x%x",
1759 		np->n_vattr.va_fileid, np->n_vattr.va_fsid);
1760 #ifdef FIFO
1761 	if (vp->v_type == VFIFO)
1762 		fifo_printinfo(vp);
1763 #endif /* FIFO */
1764 	printf("%s\n", (np->n_flag & NLOCKED) ? " (LOCKED)" : "");
1765 	if (np->n_lockholder == 0)
1766 		return;
1767 	printf("\towner pid %d", np->n_lockholder);
1768 	if (np->n_lockwaiter)
1769 		printf(" waiting pid %d", np->n_lockwaiter);
1770 	printf("\n");
1771 }
1772