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