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