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