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