nfs_vnops.c revision 1.204 1 /* $NetBSD: nfs_vnops.c,v 1.204 2004/07/08 11:12:14 yamt 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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)nfs_vnops.c 8.19 (Berkeley) 7/31/95
35 */
36
37 /*
38 * vnode op calls for Sun NFS version 2 and 3
39 */
40
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: nfs_vnops.c,v 1.204 2004/07/08 11:12:14 yamt Exp $");
43
44 #include "opt_nfs.h"
45 #include "opt_uvmhist.h"
46
47 #include <sys/param.h>
48 #include <sys/proc.h>
49 #include <sys/kernel.h>
50 #include <sys/systm.h>
51 #include <sys/resourcevar.h>
52 #include <sys/proc.h>
53 #include <sys/mount.h>
54 #include <sys/buf.h>
55 #include <sys/malloc.h>
56 #include <sys/mbuf.h>
57 #include <sys/namei.h>
58 #include <sys/vnode.h>
59 #include <sys/dirent.h>
60 #include <sys/fcntl.h>
61 #include <sys/hash.h>
62 #include <sys/lockf.h>
63 #include <sys/stat.h>
64 #include <sys/unistd.h>
65
66 #include <uvm/uvm_extern.h>
67 #include <uvm/uvm.h>
68
69 #include <miscfs/fifofs/fifo.h>
70 #include <miscfs/genfs/genfs.h>
71 #include <miscfs/specfs/specdev.h>
72
73 #include <nfs/rpcv2.h>
74 #include <nfs/nfsproto.h>
75 #include <nfs/nfs.h>
76 #include <nfs/nfsnode.h>
77 #include <nfs/nfsmount.h>
78 #include <nfs/xdr_subs.h>
79 #include <nfs/nfsm_subs.h>
80 #include <nfs/nqnfs.h>
81 #include <nfs/nfs_var.h>
82
83 #include <net/if.h>
84 #include <netinet/in.h>
85 #include <netinet/in_var.h>
86
87 /*
88 * Global vfs data structures for nfs
89 */
90 int (**nfsv2_vnodeop_p) __P((void *));
91 const struct vnodeopv_entry_desc nfsv2_vnodeop_entries[] = {
92 { &vop_default_desc, vn_default_error },
93 { &vop_lookup_desc, nfs_lookup }, /* lookup */
94 { &vop_create_desc, nfs_create }, /* create */
95 { &vop_mknod_desc, nfs_mknod }, /* mknod */
96 { &vop_open_desc, nfs_open }, /* open */
97 { &vop_close_desc, nfs_close }, /* close */
98 { &vop_access_desc, nfs_access }, /* access */
99 { &vop_getattr_desc, nfs_getattr }, /* getattr */
100 { &vop_setattr_desc, nfs_setattr }, /* setattr */
101 { &vop_read_desc, nfs_read }, /* read */
102 { &vop_write_desc, nfs_write }, /* write */
103 { &vop_lease_desc, nfs_lease_check }, /* lease */
104 { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */
105 { &vop_ioctl_desc, nfs_ioctl }, /* ioctl */
106 { &vop_poll_desc, nfs_poll }, /* poll */
107 { &vop_kqfilter_desc, nfs_kqfilter }, /* kqfilter */
108 { &vop_revoke_desc, nfs_revoke }, /* revoke */
109 { &vop_mmap_desc, nfs_mmap }, /* mmap */
110 { &vop_fsync_desc, nfs_fsync }, /* fsync */
111 { &vop_seek_desc, nfs_seek }, /* seek */
112 { &vop_remove_desc, nfs_remove }, /* remove */
113 { &vop_link_desc, nfs_link }, /* link */
114 { &vop_rename_desc, nfs_rename }, /* rename */
115 { &vop_mkdir_desc, nfs_mkdir }, /* mkdir */
116 { &vop_rmdir_desc, nfs_rmdir }, /* rmdir */
117 { &vop_symlink_desc, nfs_symlink }, /* symlink */
118 { &vop_readdir_desc, nfs_readdir }, /* readdir */
119 { &vop_readlink_desc, nfs_readlink }, /* readlink */
120 { &vop_abortop_desc, nfs_abortop }, /* abortop */
121 { &vop_inactive_desc, nfs_inactive }, /* inactive */
122 { &vop_reclaim_desc, nfs_reclaim }, /* reclaim */
123 { &vop_lock_desc, nfs_lock }, /* lock */
124 { &vop_unlock_desc, nfs_unlock }, /* unlock */
125 { &vop_bmap_desc, nfs_bmap }, /* bmap */
126 { &vop_strategy_desc, nfs_strategy }, /* strategy */
127 { &vop_print_desc, nfs_print }, /* print */
128 { &vop_islocked_desc, nfs_islocked }, /* islocked */
129 { &vop_pathconf_desc, nfs_pathconf }, /* pathconf */
130 { &vop_advlock_desc, nfs_advlock }, /* advlock */
131 { &vop_blkatoff_desc, nfs_blkatoff }, /* blkatoff */
132 { &vop_valloc_desc, nfs_valloc }, /* valloc */
133 { &vop_reallocblks_desc, nfs_reallocblks }, /* reallocblks */
134 { &vop_vfree_desc, nfs_vfree }, /* vfree */
135 { &vop_truncate_desc, nfs_truncate }, /* truncate */
136 { &vop_bwrite_desc, nfs_bwrite }, /* bwrite */
137 { &vop_getpages_desc, nfs_getpages }, /* getpages */
138 { &vop_putpages_desc, genfs_putpages }, /* putpages */
139 { NULL, NULL }
140 };
141 const struct vnodeopv_desc nfsv2_vnodeop_opv_desc =
142 { &nfsv2_vnodeop_p, nfsv2_vnodeop_entries };
143
144 /*
145 * Special device vnode ops
146 */
147 int (**spec_nfsv2nodeop_p) __P((void *));
148 const struct vnodeopv_entry_desc spec_nfsv2nodeop_entries[] = {
149 { &vop_default_desc, vn_default_error },
150 { &vop_lookup_desc, spec_lookup }, /* lookup */
151 { &vop_create_desc, spec_create }, /* create */
152 { &vop_mknod_desc, spec_mknod }, /* mknod */
153 { &vop_open_desc, spec_open }, /* open */
154 { &vop_close_desc, nfsspec_close }, /* close */
155 { &vop_access_desc, nfsspec_access }, /* access */
156 { &vop_getattr_desc, nfs_getattr }, /* getattr */
157 { &vop_setattr_desc, nfs_setattr }, /* setattr */
158 { &vop_read_desc, nfsspec_read }, /* read */
159 { &vop_write_desc, nfsspec_write }, /* write */
160 { &vop_lease_desc, spec_lease_check }, /* lease */
161 { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */
162 { &vop_ioctl_desc, spec_ioctl }, /* ioctl */
163 { &vop_poll_desc, spec_poll }, /* poll */
164 { &vop_kqfilter_desc, spec_kqfilter }, /* kqfilter */
165 { &vop_revoke_desc, spec_revoke }, /* revoke */
166 { &vop_mmap_desc, spec_mmap }, /* mmap */
167 { &vop_fsync_desc, spec_fsync }, /* fsync */
168 { &vop_seek_desc, spec_seek }, /* seek */
169 { &vop_remove_desc, spec_remove }, /* remove */
170 { &vop_link_desc, spec_link }, /* link */
171 { &vop_rename_desc, spec_rename }, /* rename */
172 { &vop_mkdir_desc, spec_mkdir }, /* mkdir */
173 { &vop_rmdir_desc, spec_rmdir }, /* rmdir */
174 { &vop_symlink_desc, spec_symlink }, /* symlink */
175 { &vop_readdir_desc, spec_readdir }, /* readdir */
176 { &vop_readlink_desc, spec_readlink }, /* readlink */
177 { &vop_abortop_desc, spec_abortop }, /* abortop */
178 { &vop_inactive_desc, nfs_inactive }, /* inactive */
179 { &vop_reclaim_desc, nfs_reclaim }, /* reclaim */
180 { &vop_lock_desc, nfs_lock }, /* lock */
181 { &vop_unlock_desc, nfs_unlock }, /* unlock */
182 { &vop_bmap_desc, spec_bmap }, /* bmap */
183 { &vop_strategy_desc, spec_strategy }, /* strategy */
184 { &vop_print_desc, nfs_print }, /* print */
185 { &vop_islocked_desc, nfs_islocked }, /* islocked */
186 { &vop_pathconf_desc, spec_pathconf }, /* pathconf */
187 { &vop_advlock_desc, spec_advlock }, /* advlock */
188 { &vop_blkatoff_desc, spec_blkatoff }, /* blkatoff */
189 { &vop_valloc_desc, spec_valloc }, /* valloc */
190 { &vop_reallocblks_desc, spec_reallocblks }, /* reallocblks */
191 { &vop_vfree_desc, spec_vfree }, /* vfree */
192 { &vop_truncate_desc, spec_truncate }, /* truncate */
193 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
194 { &vop_getpages_desc, spec_getpages }, /* getpages */
195 { &vop_putpages_desc, spec_putpages }, /* putpages */
196 { NULL, NULL }
197 };
198 const struct vnodeopv_desc spec_nfsv2nodeop_opv_desc =
199 { &spec_nfsv2nodeop_p, spec_nfsv2nodeop_entries };
200
201 int (**fifo_nfsv2nodeop_p) __P((void *));
202 const struct vnodeopv_entry_desc fifo_nfsv2nodeop_entries[] = {
203 { &vop_default_desc, vn_default_error },
204 { &vop_lookup_desc, fifo_lookup }, /* lookup */
205 { &vop_create_desc, fifo_create }, /* create */
206 { &vop_mknod_desc, fifo_mknod }, /* mknod */
207 { &vop_open_desc, fifo_open }, /* open */
208 { &vop_close_desc, nfsfifo_close }, /* close */
209 { &vop_access_desc, nfsspec_access }, /* access */
210 { &vop_getattr_desc, nfs_getattr }, /* getattr */
211 { &vop_setattr_desc, nfs_setattr }, /* setattr */
212 { &vop_read_desc, nfsfifo_read }, /* read */
213 { &vop_write_desc, nfsfifo_write }, /* write */
214 { &vop_lease_desc, fifo_lease_check }, /* lease */
215 { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */
216 { &vop_ioctl_desc, fifo_ioctl }, /* ioctl */
217 { &vop_poll_desc, fifo_poll }, /* poll */
218 { &vop_kqfilter_desc, fifo_kqfilter }, /* kqfilter */
219 { &vop_revoke_desc, fifo_revoke }, /* revoke */
220 { &vop_mmap_desc, fifo_mmap }, /* mmap */
221 { &vop_fsync_desc, nfs_fsync }, /* fsync */
222 { &vop_seek_desc, fifo_seek }, /* seek */
223 { &vop_remove_desc, fifo_remove }, /* remove */
224 { &vop_link_desc, fifo_link }, /* link */
225 { &vop_rename_desc, fifo_rename }, /* rename */
226 { &vop_mkdir_desc, fifo_mkdir }, /* mkdir */
227 { &vop_rmdir_desc, fifo_rmdir }, /* rmdir */
228 { &vop_symlink_desc, fifo_symlink }, /* symlink */
229 { &vop_readdir_desc, fifo_readdir }, /* readdir */
230 { &vop_readlink_desc, fifo_readlink }, /* readlink */
231 { &vop_abortop_desc, fifo_abortop }, /* abortop */
232 { &vop_inactive_desc, nfs_inactive }, /* inactive */
233 { &vop_reclaim_desc, nfs_reclaim }, /* reclaim */
234 { &vop_lock_desc, nfs_lock }, /* lock */
235 { &vop_unlock_desc, nfs_unlock }, /* unlock */
236 { &vop_bmap_desc, fifo_bmap }, /* bmap */
237 { &vop_strategy_desc, genfs_badop }, /* strategy */
238 { &vop_print_desc, nfs_print }, /* print */
239 { &vop_islocked_desc, nfs_islocked }, /* islocked */
240 { &vop_pathconf_desc, fifo_pathconf }, /* pathconf */
241 { &vop_advlock_desc, fifo_advlock }, /* advlock */
242 { &vop_blkatoff_desc, fifo_blkatoff }, /* blkatoff */
243 { &vop_valloc_desc, fifo_valloc }, /* valloc */
244 { &vop_reallocblks_desc, fifo_reallocblks }, /* reallocblks */
245 { &vop_vfree_desc, fifo_vfree }, /* vfree */
246 { &vop_truncate_desc, fifo_truncate }, /* truncate */
247 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
248 { &vop_putpages_desc, fifo_putpages }, /* putpages */
249 { NULL, NULL }
250 };
251 const struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc =
252 { &fifo_nfsv2nodeop_p, fifo_nfsv2nodeop_entries };
253
254 static void nfs_writerpc_extfree(struct mbuf *, caddr_t, size_t, void *);
255
256 /*
257 * Global variables
258 */
259 extern u_int32_t nfs_true, nfs_false;
260 extern u_int32_t nfs_xdrneg1;
261 extern const nfstype nfsv3_type[9];
262
263 int nfs_numasync = 0;
264 #define DIRHDSIZ (sizeof (struct dirent) - (MAXNAMLEN + 1))
265
266 static void nfs_cache_enter(struct vnode *, struct vnode *,
267 struct componentname *);
268
269 static void
270 nfs_cache_enter(struct vnode *dvp, struct vnode *vp,
271 struct componentname *cnp)
272 {
273
274 if (vp != NULL) {
275 struct nfsnode *np = VTONFS(vp);
276
277 np->n_ctime = np->n_vattr->va_ctime.tv_sec;
278 } else {
279 struct nfsnode *dnp = VTONFS(dvp);
280
281 if (!timespecisset(&dnp->n_nctime))
282 dnp->n_nctime = dnp->n_vattr->va_mtime;
283 }
284
285 cache_enter(dvp, vp, cnp);
286 }
287
288 /*
289 * nfs null call from vfs.
290 */
291 int
292 nfs_null(vp, cred, procp)
293 struct vnode *vp;
294 struct ucred *cred;
295 struct proc *procp;
296 {
297 caddr_t bpos, dpos;
298 int error = 0;
299 struct mbuf *mreq, *mrep, *md, *mb;
300 struct nfsnode *np = VTONFS(vp);
301
302 nfsm_reqhead(np, NFSPROC_NULL, 0);
303 nfsm_request(np, NFSPROC_NULL, procp, cred);
304 nfsm_reqdone;
305 return (error);
306 }
307
308 /*
309 * nfs access vnode op.
310 * For nfs version 2, just return ok. File accesses may fail later.
311 * For nfs version 3, use the access rpc to check accessibility. If file modes
312 * are changed on the server, accesses might still fail later.
313 */
314 int
315 nfs_access(v)
316 void *v;
317 {
318 struct vop_access_args /* {
319 struct vnode *a_vp;
320 int a_mode;
321 struct ucred *a_cred;
322 struct proc *a_p;
323 } */ *ap = v;
324 struct vnode *vp = ap->a_vp;
325 #ifndef NFS_V2_ONLY
326 u_int32_t *tl;
327 caddr_t cp;
328 int32_t t1, t2;
329 caddr_t bpos, dpos, cp2;
330 int error = 0, attrflag;
331 struct mbuf *mreq, *mrep, *md, *mb;
332 u_int32_t mode, rmode;
333 const int v3 = NFS_ISV3(vp);
334 #endif
335 int cachevalid;
336 struct nfsnode *np = VTONFS(vp);
337
338 cachevalid = (np->n_accstamp != -1 &&
339 (time.tv_sec - np->n_accstamp) < NFS_ATTRTIMEO(np) &&
340 np->n_accuid == ap->a_cred->cr_uid);
341
342 /*
343 * Check access cache first. If this request has been made for this
344 * uid shortly before, use the cached result.
345 */
346 if (cachevalid) {
347 if (!np->n_accerror) {
348 if ((np->n_accmode & ap->a_mode) == ap->a_mode)
349 return np->n_accerror;
350 } else if ((np->n_accmode & ap->a_mode) == np->n_accmode)
351 return np->n_accerror;
352 }
353
354 #ifndef NFS_V2_ONLY
355 /*
356 * For nfs v3, do an access rpc, otherwise you are stuck emulating
357 * ufs_access() locally using the vattr. This may not be correct,
358 * since the server may apply other access criteria such as
359 * client uid-->server uid mapping that we do not know about, but
360 * this is better than just returning anything that is lying about
361 * in the cache.
362 */
363 if (v3) {
364 nfsstats.rpccnt[NFSPROC_ACCESS]++;
365 nfsm_reqhead(np, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED);
366 nfsm_fhtom(np, v3);
367 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
368 if (ap->a_mode & VREAD)
369 mode = NFSV3ACCESS_READ;
370 else
371 mode = 0;
372 if (vp->v_type != VDIR) {
373 if (ap->a_mode & VWRITE)
374 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
375 if (ap->a_mode & VEXEC)
376 mode |= NFSV3ACCESS_EXECUTE;
377 } else {
378 if (ap->a_mode & VWRITE)
379 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
380 NFSV3ACCESS_DELETE);
381 if (ap->a_mode & VEXEC)
382 mode |= NFSV3ACCESS_LOOKUP;
383 }
384 *tl = txdr_unsigned(mode);
385 nfsm_request(np, NFSPROC_ACCESS, ap->a_p, ap->a_cred);
386 nfsm_postop_attr(vp, attrflag, 0);
387 if (!error) {
388 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
389 rmode = fxdr_unsigned(u_int32_t, *tl);
390 /*
391 * The NFS V3 spec does not clarify whether or not
392 * the returned access bits can be a superset of
393 * the ones requested, so...
394 */
395 if ((rmode & mode) != mode)
396 error = EACCES;
397 }
398 nfsm_reqdone;
399 } else
400 #endif
401 return (nfsspec_access(ap));
402 #ifndef NFS_V2_ONLY
403 /*
404 * Disallow write attempts on filesystems mounted read-only;
405 * unless the file is a socket, fifo, or a block or character
406 * device resident on the filesystem.
407 */
408 if (!error && (ap->a_mode & VWRITE) &&
409 (vp->v_mount->mnt_flag & MNT_RDONLY)) {
410 switch (vp->v_type) {
411 case VREG:
412 case VDIR:
413 case VLNK:
414 error = EROFS;
415 default:
416 break;
417 }
418 }
419
420 if (!error || error == EACCES) {
421 /*
422 * If we got the same result as for a previous,
423 * different request, OR it in. Don't update
424 * the timestamp in that case.
425 */
426 if (cachevalid && np->n_accstamp != -1 &&
427 error == np->n_accerror) {
428 if (!error)
429 np->n_accmode |= ap->a_mode;
430 else if ((np->n_accmode & ap->a_mode) == ap->a_mode)
431 np->n_accmode = ap->a_mode;
432 } else {
433 np->n_accstamp = time.tv_sec;
434 np->n_accuid = ap->a_cred->cr_uid;
435 np->n_accmode = ap->a_mode;
436 np->n_accerror = error;
437 }
438 }
439
440 return (error);
441 #endif
442 }
443
444 /*
445 * nfs open vnode op
446 * Check to see if the type is ok
447 * and that deletion is not in progress.
448 * For paged in text files, you will need to flush the page cache
449 * if consistency is lost.
450 */
451 /* ARGSUSED */
452 int
453 nfs_open(v)
454 void *v;
455 {
456 struct vop_open_args /* {
457 struct vnode *a_vp;
458 int a_mode;
459 struct ucred *a_cred;
460 struct proc *a_p;
461 } */ *ap = v;
462 struct vnode *vp = ap->a_vp;
463 struct nfsnode *np = VTONFS(vp);
464 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
465 struct vattr vattr;
466 int error;
467
468 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
469 return (EACCES);
470 }
471
472 if (ap->a_mode & FREAD) {
473 if (np->n_rcred != NULL)
474 crfree(np->n_rcred);
475 np->n_rcred = ap->a_cred;
476 crhold(np->n_rcred);
477 }
478 if (ap->a_mode & FWRITE) {
479 if (np->n_wcred != NULL)
480 crfree(np->n_wcred);
481 np->n_wcred = ap->a_cred;
482 crhold(np->n_wcred);
483 }
484
485 #ifndef NFS_V2_ONLY
486 /*
487 * Get a valid lease. If cached data is stale, flush it.
488 */
489 if (nmp->nm_flag & NFSMNT_NQNFS) {
490 if (NQNFS_CKINVALID(vp, np, ND_READ)) {
491 do {
492 error = nqnfs_getlease(vp, ND_READ, ap->a_cred,
493 ap->a_p);
494 } while (error == NQNFS_EXPIRED);
495 if (error)
496 return (error);
497 if (np->n_lrev != np->n_brev ||
498 (np->n_flag & NQNFSNONCACHE)) {
499 if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred,
500 ap->a_p, 1)) == EINTR)
501 return (error);
502 np->n_brev = np->n_lrev;
503 }
504 }
505 } else
506 #endif
507 {
508 if (np->n_flag & NMODIFIED) {
509 if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred,
510 ap->a_p, 1)) == EINTR)
511 return (error);
512 NFS_INVALIDATE_ATTRCACHE(np);
513 if (vp->v_type == VDIR) {
514 nfs_invaldircache(vp, 0);
515 np->n_direofoffset = 0;
516 }
517 error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_p);
518 if (error)
519 return (error);
520 np->n_mtime = vattr.va_mtime;
521 } else {
522 error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_p);
523 if (error)
524 return (error);
525 if (timespeccmp(&np->n_mtime, &vattr.va_mtime, !=)) {
526 if (vp->v_type == VDIR) {
527 nfs_invaldircache(vp, 0);
528 np->n_direofoffset = 0;
529 }
530 if ((error = nfs_vinvalbuf(vp, V_SAVE,
531 ap->a_cred, ap->a_p, 1)) == EINTR)
532 return (error);
533 np->n_mtime = vattr.va_mtime;
534 }
535 }
536 }
537 if ((nmp->nm_flag & NFSMNT_NQNFS) == 0)
538 NFS_INVALIDATE_ATTRCACHE(np); /* For Open/Close consistency */
539 return (0);
540 }
541
542 /*
543 * nfs close vnode op
544 * What an NFS client should do upon close after writing is a debatable issue.
545 * Most NFS clients push delayed writes to the server upon close, basically for
546 * two reasons:
547 * 1 - So that any write errors may be reported back to the client process
548 * doing the close system call. By far the two most likely errors are
549 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
550 * 2 - To put a worst case upper bound on cache inconsistency between
551 * multiple clients for the file.
552 * There is also a consistency problem for Version 2 of the protocol w.r.t.
553 * not being able to tell if other clients are writing a file concurrently,
554 * since there is no way of knowing if the changed modify time in the reply
555 * is only due to the write for this client.
556 * (NFS Version 3 provides weak cache consistency data in the reply that
557 * should be sufficient to detect and handle this case.)
558 *
559 * The current code does the following:
560 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
561 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
562 * or commit them (this satisfies 1 and 2 except for the
563 * case where the server crashes after this close but
564 * before the commit RPC, which is felt to be "good
565 * enough". Changing the last argument to nfs_flush() to
566 * a 1 would force a commit operation, if it is felt a
567 * commit is necessary now.
568 * for NQNFS - do nothing now, since 2 is dealt with via leases and
569 * 1 should be dealt with via an fsync() system call for
570 * cases where write errors are important.
571 */
572 /* ARGSUSED */
573 int
574 nfs_close(v)
575 void *v;
576 {
577 struct vop_close_args /* {
578 struct vnodeop_desc *a_desc;
579 struct vnode *a_vp;
580 int a_fflag;
581 struct ucred *a_cred;
582 struct proc *a_p;
583 } */ *ap = v;
584 struct vnode *vp = ap->a_vp;
585 struct nfsnode *np = VTONFS(vp);
586 int error = 0;
587 UVMHIST_FUNC("nfs_close"); UVMHIST_CALLED(ubchist);
588
589 if (vp->v_type == VREG) {
590 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) == 0 &&
591 (np->n_flag & NMODIFIED)) {
592 #ifndef NFS_V2_ONLY
593 if (NFS_ISV3(vp)) {
594 error = nfs_flush(vp, ap->a_cred, MNT_WAIT, ap->a_p, 0);
595 np->n_flag &= ~NMODIFIED;
596 } else
597 #endif
598 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_p, 1);
599 NFS_INVALIDATE_ATTRCACHE(np);
600 }
601 if (np->n_flag & NWRITEERR) {
602 np->n_flag &= ~NWRITEERR;
603 error = np->n_error;
604 }
605 }
606 UVMHIST_LOG(ubchist, "returning %d", error,0,0,0);
607 return (error);
608 }
609
610 /*
611 * nfs getattr call from vfs.
612 */
613 int
614 nfs_getattr(v)
615 void *v;
616 {
617 struct vop_getattr_args /* {
618 struct vnode *a_vp;
619 struct vattr *a_vap;
620 struct ucred *a_cred;
621 struct proc *a_p;
622 } */ *ap = v;
623 struct vnode *vp = ap->a_vp;
624 struct nfsnode *np = VTONFS(vp);
625 caddr_t cp;
626 u_int32_t *tl;
627 int32_t t1, t2;
628 caddr_t bpos, dpos;
629 int error = 0;
630 struct mbuf *mreq, *mrep, *md, *mb;
631 const int v3 = NFS_ISV3(vp);
632
633 /*
634 * Update local times for special files.
635 */
636 if (np->n_flag & (NACC | NUPD))
637 np->n_flag |= NCHG;
638
639 /*
640 * if we have delayed truncation, do it now.
641 */
642 nfs_delayedtruncate(vp);
643
644 /*
645 * First look in the cache.
646 */
647 if (nfs_getattrcache(vp, ap->a_vap) == 0)
648 return (0);
649 nfsstats.rpccnt[NFSPROC_GETATTR]++;
650 nfsm_reqhead(np, NFSPROC_GETATTR, NFSX_FH(v3));
651 nfsm_fhtom(np, v3);
652 nfsm_request(np, NFSPROC_GETATTR, ap->a_p, ap->a_cred);
653 if (!error) {
654 nfsm_loadattr(vp, ap->a_vap, 0);
655 if (vp->v_type == VDIR &&
656 ap->a_vap->va_blocksize < NFS_DIRFRAGSIZ)
657 ap->a_vap->va_blocksize = NFS_DIRFRAGSIZ;
658 }
659 nfsm_reqdone;
660 return (error);
661 }
662
663 /*
664 * nfs setattr call.
665 */
666 int
667 nfs_setattr(v)
668 void *v;
669 {
670 struct vop_setattr_args /* {
671 struct vnodeop_desc *a_desc;
672 struct vnode *a_vp;
673 struct vattr *a_vap;
674 struct ucred *a_cred;
675 struct proc *a_p;
676 } */ *ap = v;
677 struct vnode *vp = ap->a_vp;
678 struct nfsnode *np = VTONFS(vp);
679 struct vattr *vap = ap->a_vap;
680 int error = 0;
681 u_quad_t tsize = 0;
682
683 /*
684 * Setting of flags is not supported.
685 */
686 if (vap->va_flags != VNOVAL)
687 return (EOPNOTSUPP);
688
689 /*
690 * Disallow write attempts if the filesystem is mounted read-only.
691 */
692 if ((vap->va_uid != (uid_t)VNOVAL ||
693 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
694 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
695 (vp->v_mount->mnt_flag & MNT_RDONLY))
696 return (EROFS);
697 if (vap->va_size != VNOVAL) {
698 switch (vp->v_type) {
699 case VDIR:
700 return (EISDIR);
701 case VCHR:
702 case VBLK:
703 case VSOCK:
704 case VFIFO:
705 if (vap->va_mtime.tv_sec == VNOVAL &&
706 vap->va_atime.tv_sec == VNOVAL &&
707 vap->va_mode == (mode_t)VNOVAL &&
708 vap->va_uid == (uid_t)VNOVAL &&
709 vap->va_gid == (gid_t)VNOVAL)
710 return (0);
711 vap->va_size = VNOVAL;
712 break;
713 default:
714 /*
715 * Disallow write attempts if the filesystem is
716 * mounted read-only.
717 */
718 if (vp->v_mount->mnt_flag & MNT_RDONLY)
719 return (EROFS);
720 uvm_vnp_setsize(vp, vap->va_size);
721 tsize = np->n_size;
722 np->n_size = vap->va_size;
723 if (vap->va_size == 0)
724 error = nfs_vinvalbuf(vp, 0,
725 ap->a_cred, ap->a_p, 1);
726 else
727 error = nfs_vinvalbuf(vp, V_SAVE,
728 ap->a_cred, ap->a_p, 1);
729 if (error) {
730 uvm_vnp_setsize(vp, tsize);
731 return (error);
732 }
733 np->n_vattr->va_size = vap->va_size;
734 }
735 } else if ((vap->va_mtime.tv_sec != VNOVAL ||
736 vap->va_atime.tv_sec != VNOVAL) &&
737 vp->v_type == VREG &&
738 (error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred,
739 ap->a_p, 1)) == EINTR)
740 return (error);
741 error = nfs_setattrrpc(vp, vap, ap->a_cred, ap->a_p);
742 if (error && vap->va_size != VNOVAL) {
743 np->n_size = np->n_vattr->va_size = tsize;
744 uvm_vnp_setsize(vp, np->n_size);
745 }
746 VN_KNOTE(vp, NOTE_ATTRIB);
747 return (error);
748 }
749
750 /*
751 * Do an nfs setattr rpc.
752 */
753 int
754 nfs_setattrrpc(vp, vap, cred, procp)
755 struct vnode *vp;
756 struct vattr *vap;
757 struct ucred *cred;
758 struct proc *procp;
759 {
760 struct nfsv2_sattr *sp;
761 caddr_t cp;
762 int32_t t1, t2;
763 caddr_t bpos, dpos;
764 u_int32_t *tl;
765 int error = 0;
766 struct mbuf *mreq, *mrep, *md, *mb;
767 const int v3 = NFS_ISV3(vp);
768 struct nfsnode *np = VTONFS(vp);
769 #ifndef NFS_V2_ONLY
770 int wccflag = NFSV3_WCCRATTR;
771 caddr_t cp2;
772 #endif
773
774 nfsstats.rpccnt[NFSPROC_SETATTR]++;
775 nfsm_reqhead(np, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3));
776 nfsm_fhtom(np, v3);
777 #ifndef NFS_V2_ONLY
778 if (v3) {
779 nfsm_v3attrbuild(vap, TRUE);
780 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
781 *tl = nfs_false;
782 } else {
783 #endif
784 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
785 if (vap->va_mode == (mode_t)VNOVAL)
786 sp->sa_mode = nfs_xdrneg1;
787 else
788 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
789 if (vap->va_uid == (uid_t)VNOVAL)
790 sp->sa_uid = nfs_xdrneg1;
791 else
792 sp->sa_uid = txdr_unsigned(vap->va_uid);
793 if (vap->va_gid == (gid_t)VNOVAL)
794 sp->sa_gid = nfs_xdrneg1;
795 else
796 sp->sa_gid = txdr_unsigned(vap->va_gid);
797 sp->sa_size = txdr_unsigned(vap->va_size);
798 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
799 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
800 #ifndef NFS_V2_ONLY
801 }
802 #endif
803 nfsm_request(np, NFSPROC_SETATTR, procp, cred);
804 #ifndef NFS_V2_ONLY
805 if (v3) {
806 nfsm_wcc_data(vp, wccflag, 0);
807 } else
808 #endif
809 nfsm_loadattr(vp, (struct vattr *)0, 0);
810 nfsm_reqdone;
811 return (error);
812 }
813
814 /*
815 * nfs lookup call, one step at a time...
816 * First look in cache
817 * If not found, unlock the directory nfsnode and do the rpc
818 *
819 * This code is full of lock/unlock statements and checks, because
820 * we continue after cache_lookup has finished (we need to check
821 * with the attr cache and do an rpc if it has timed out). This means
822 * that the locking effects of cache_lookup have to be taken into
823 * account.
824 */
825 int
826 nfs_lookup(v)
827 void *v;
828 {
829 struct vop_lookup_args /* {
830 struct vnodeop_desc *a_desc;
831 struct vnode *a_dvp;
832 struct vnode **a_vpp;
833 struct componentname *a_cnp;
834 } */ *ap = v;
835 struct componentname *cnp = ap->a_cnp;
836 struct vnode *dvp = ap->a_dvp;
837 struct vnode **vpp = ap->a_vpp;
838 int flags;
839 struct vnode *newvp;
840 u_int32_t *tl;
841 caddr_t cp;
842 int32_t t1, t2;
843 caddr_t bpos, dpos, cp2;
844 struct mbuf *mreq, *mrep, *md, *mb;
845 long len;
846 nfsfh_t *fhp;
847 struct nfsnode *np;
848 int lockparent, wantparent, error = 0, attrflag, fhsize;
849 const int v3 = NFS_ISV3(dvp);
850
851 cnp->cn_flags &= ~PDIRUNLOCK;
852 flags = cnp->cn_flags;
853
854 *vpp = NULLVP;
855 newvp = NULLVP;
856 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
857 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
858 return (EROFS);
859 if (dvp->v_type != VDIR)
860 return (ENOTDIR);
861
862 /*
863 * RFC1813(nfsv3) 3.2 says clients should handle "." by themselves.
864 */
865 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
866 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_proc);
867 if (error)
868 return error;
869 if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN))
870 return EISDIR;
871 VREF(dvp);
872 *vpp = dvp;
873 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
874 cnp->cn_flags |= SAVENAME;
875 return 0;
876 }
877
878 lockparent = flags & LOCKPARENT;
879 wantparent = flags & (LOCKPARENT|WANTPARENT);
880 np = VTONFS(dvp);
881
882 /*
883 * Before tediously performing a linear scan of the directory,
884 * check the name cache to see if the directory/name pair
885 * we are looking for is known already.
886 * If the directory/name pair is found in the name cache,
887 * we have to ensure the directory has not changed from
888 * the time the cache entry has been created. If it has,
889 * the cache entry has to be ignored.
890 */
891 error = cache_lookup_raw(dvp, vpp, cnp);
892 KASSERT(dvp != *vpp);
893 if (error >= 0) {
894 struct vattr vattr;
895 int err2;
896
897 if (error && error != ENOENT) {
898 *vpp = NULLVP;
899 return error;
900 }
901
902 err2 = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_proc);
903 if (err2 != 0) {
904 if (error == 0)
905 vrele(*vpp);
906 *vpp = NULLVP;
907 return err2;
908 }
909
910 if (error == ENOENT) {
911 if (!VOP_GETATTR(dvp, &vattr, cnp->cn_cred,
912 cnp->cn_proc) && timespeccmp(&vattr.va_mtime,
913 &VTONFS(dvp)->n_nctime, ==)) {
914 goto noentry;
915 }
916 cache_purge1(dvp, NULL, PURGE_CHILDREN);
917 timespecclear(&np->n_nctime);
918 goto dorpc;
919 }
920
921 newvp = *vpp;
922 if (!VOP_GETATTR(newvp, &vattr, cnp->cn_cred, cnp->cn_proc)
923 && vattr.va_ctime.tv_sec == VTONFS(newvp)->n_ctime) {
924 nfsstats.lookupcache_hits++;
925 if ((flags & ISDOTDOT) != 0 ||
926 (~flags & (LOCKPARENT|ISLASTCN)) != 0) {
927 VOP_UNLOCK(dvp, 0);
928 cnp->cn_flags |= PDIRUNLOCK;
929 }
930 error = vn_lock(newvp, LK_EXCLUSIVE);
931 if (error) {
932 /* newvp has been revoked. */
933 vrele(newvp);
934 *vpp = NULL;
935 return error;
936 }
937 if ((~flags & (LOCKPARENT|ISLASTCN)) == 0
938 && (cnp->cn_flags & PDIRUNLOCK)) {
939 KASSERT(flags & ISDOTDOT);
940 error = vn_lock(dvp, LK_EXCLUSIVE);
941 if (error) {
942 vput(newvp);
943 *vpp = NULL;
944 return error;
945 }
946 cnp->cn_flags &= ~PDIRUNLOCK;
947 }
948 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
949 cnp->cn_flags |= SAVENAME;
950 KASSERT(newvp->v_type != VNON);
951 return (0);
952 }
953 cache_purge1(newvp, NULL, PURGE_PARENTS);
954 vrele(newvp);
955 *vpp = NULLVP;
956 }
957 dorpc:
958 #if 0
959 /*
960 * because nfsv3 has the same CREATE semantics as ours,
961 * we don't have to perform LOOKUPs beforehand.
962 *
963 * XXX ideally we can do the same for nfsv2 in the case of !O_EXCL.
964 * XXX although we have no way to know if O_EXCL is requested or not.
965 */
966
967 if (v3 && cnp->cn_nameiop == CREATE &&
968 (flags & (ISLASTCN|ISDOTDOT)) == ISLASTCN &&
969 (dvp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
970 KASSERT(lockparent);
971 cnp->cn_flags |= SAVENAME;
972 return (EJUSTRETURN);
973 }
974 #endif /* 0 */
975
976 error = 0;
977 newvp = NULLVP;
978 nfsstats.lookupcache_misses++;
979 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
980 len = cnp->cn_namelen;
981 nfsm_reqhead(np, NFSPROC_LOOKUP,
982 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
983 nfsm_fhtom(np, v3);
984 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
985 nfsm_request(np, NFSPROC_LOOKUP, cnp->cn_proc, cnp->cn_cred);
986 if (error) {
987 nfsm_postop_attr(dvp, attrflag, 0);
988 m_freem(mrep);
989 goto nfsmout;
990 }
991 nfsm_getfh(fhp, fhsize, v3);
992
993 /*
994 * Handle RENAME case...
995 */
996 if (cnp->cn_nameiop == RENAME && wantparent && (flags & ISLASTCN)) {
997 if (NFS_CMPFH(np, fhp, fhsize)) {
998 m_freem(mrep);
999 return (EISDIR);
1000 }
1001 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1002 if (error) {
1003 m_freem(mrep);
1004 return error;
1005 }
1006 newvp = NFSTOV(np);
1007 #ifndef NFS_V2_ONLY
1008 if (v3) {
1009 nfsm_postop_attr(newvp, attrflag, 0);
1010 nfsm_postop_attr(dvp, attrflag, 0);
1011 } else
1012 #endif
1013 nfsm_loadattr(newvp, (struct vattr *)0, 0);
1014 *vpp = newvp;
1015 m_freem(mrep);
1016 cnp->cn_flags |= SAVENAME;
1017 if (!lockparent) {
1018 VOP_UNLOCK(dvp, 0);
1019 cnp->cn_flags |= PDIRUNLOCK;
1020 }
1021 goto validate;
1022 }
1023
1024 /*
1025 * The postop attr handling is duplicated for each if case,
1026 * because it should be done while dvp is locked (unlocking
1027 * dvp is different for each case).
1028 */
1029
1030 if (NFS_CMPFH(np, fhp, fhsize)) {
1031 /*
1032 * "." lookup
1033 */
1034 VREF(dvp);
1035 newvp = dvp;
1036 #ifndef NFS_V2_ONLY
1037 if (v3) {
1038 nfsm_postop_attr(newvp, attrflag, 0);
1039 nfsm_postop_attr(dvp, attrflag, 0);
1040 } else
1041 #endif
1042 nfsm_loadattr(newvp, (struct vattr *)0, 0);
1043 } else if (flags & ISDOTDOT) {
1044 /*
1045 * ".." lookup
1046 */
1047 VOP_UNLOCK(dvp, 0);
1048 cnp->cn_flags |= PDIRUNLOCK;
1049
1050 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1051 if (error) {
1052 if (vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY) == 0)
1053 cnp->cn_flags &= ~PDIRUNLOCK;
1054 m_freem(mrep);
1055 return error;
1056 }
1057 newvp = NFSTOV(np);
1058
1059 #ifndef NFS_V2_ONLY
1060 if (v3) {
1061 nfsm_postop_attr(newvp, attrflag, 0);
1062 nfsm_postop_attr(dvp, attrflag, 0);
1063 } else
1064 #endif
1065 nfsm_loadattr(newvp, (struct vattr *)0, 0);
1066
1067 if (lockparent && (flags & ISLASTCN)) {
1068 if ((error = vn_lock(dvp, LK_EXCLUSIVE))) {
1069 m_freem(mrep);
1070 vput(newvp);
1071 return error;
1072 }
1073 cnp->cn_flags &= ~PDIRUNLOCK;
1074 }
1075 } else {
1076 /*
1077 * Other lookups.
1078 */
1079 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1080 if (error) {
1081 m_freem(mrep);
1082 return error;
1083 }
1084 newvp = NFSTOV(np);
1085 #ifndef NFS_V2_ONLY
1086 if (v3) {
1087 nfsm_postop_attr(newvp, attrflag, 0);
1088 nfsm_postop_attr(dvp, attrflag, 0);
1089 } else
1090 #endif
1091 nfsm_loadattr(newvp, (struct vattr *)0, 0);
1092 if (!lockparent || !(flags & ISLASTCN)) {
1093 VOP_UNLOCK(dvp, 0);
1094 cnp->cn_flags |= PDIRUNLOCK;
1095 }
1096 }
1097 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
1098 cnp->cn_flags |= SAVENAME;
1099 if ((cnp->cn_flags & MAKEENTRY) &&
1100 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN))) {
1101 nfs_cache_enter(dvp, newvp, cnp);
1102 }
1103 *vpp = newvp;
1104 nfsm_reqdone;
1105 if (error) {
1106 /*
1107 * We get here only because of errors returned by
1108 * the RPC. Otherwise we'll have returned above
1109 * (the nfsm_* macros will jump to nfsm_reqdone
1110 * on error).
1111 */
1112 if (error == ENOENT && (cnp->cn_flags & MAKEENTRY) &&
1113 cnp->cn_nameiop != CREATE) {
1114 nfs_cache_enter(dvp, NULL, cnp);
1115 }
1116 if (newvp != NULLVP) {
1117 vrele(newvp);
1118 if (newvp != dvp)
1119 VOP_UNLOCK(newvp, 0);
1120 }
1121 noentry:
1122 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
1123 (flags & ISLASTCN) && error == ENOENT) {
1124 if (dvp->v_mount->mnt_flag & MNT_RDONLY) {
1125 error = EROFS;
1126 } else {
1127 error = EJUSTRETURN;
1128 cnp->cn_flags |= SAVENAME;
1129 }
1130 }
1131 *vpp = NULL;
1132 return error;
1133 }
1134
1135 validate:
1136 /*
1137 * make sure we have valid type and size.
1138 */
1139
1140 newvp = *vpp;
1141 if (newvp->v_type == VNON) {
1142 struct vattr vattr; /* dummy */
1143
1144 KASSERT(VTONFS(newvp)->n_attrstamp == 0);
1145 error = VOP_GETATTR(newvp, &vattr, cnp->cn_cred, cnp->cn_proc);
1146 if (error) {
1147 vput(newvp);
1148 *vpp = NULL;
1149 }
1150 }
1151
1152 return error;
1153 }
1154
1155 /*
1156 * nfs read call.
1157 * Just call nfs_bioread() to do the work.
1158 */
1159 int
1160 nfs_read(v)
1161 void *v;
1162 {
1163 struct vop_read_args /* {
1164 struct vnode *a_vp;
1165 struct uio *a_uio;
1166 int a_ioflag;
1167 struct ucred *a_cred;
1168 } */ *ap = v;
1169 struct vnode *vp = ap->a_vp;
1170
1171 if (vp->v_type != VREG)
1172 return (EPERM);
1173 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred, 0));
1174 }
1175
1176 /*
1177 * nfs readlink call
1178 */
1179 int
1180 nfs_readlink(v)
1181 void *v;
1182 {
1183 struct vop_readlink_args /* {
1184 struct vnode *a_vp;
1185 struct uio *a_uio;
1186 struct ucred *a_cred;
1187 } */ *ap = v;
1188 struct vnode *vp = ap->a_vp;
1189
1190 if (vp->v_type != VLNK)
1191 return (EPERM);
1192 return (nfs_bioread(vp, ap->a_uio, 0, ap->a_cred, 0));
1193 }
1194
1195 /*
1196 * Do a readlink rpc.
1197 * Called by nfs_doio() from below the buffer cache.
1198 */
1199 int
1200 nfs_readlinkrpc(vp, uiop, cred)
1201 struct vnode *vp;
1202 struct uio *uiop;
1203 struct ucred *cred;
1204 {
1205 u_int32_t *tl;
1206 caddr_t cp;
1207 int32_t t1, t2;
1208 caddr_t bpos, dpos, cp2;
1209 int error = 0;
1210 uint32_t len;
1211 struct mbuf *mreq, *mrep, *md, *mb;
1212 const int v3 = NFS_ISV3(vp);
1213 struct nfsnode *np = VTONFS(vp);
1214 #ifndef NFS_V2_ONLY
1215 int attrflag;
1216 #endif
1217
1218 nfsstats.rpccnt[NFSPROC_READLINK]++;
1219 nfsm_reqhead(np, NFSPROC_READLINK, NFSX_FH(v3));
1220 nfsm_fhtom(np, v3);
1221 nfsm_request(np, NFSPROC_READLINK, uiop->uio_procp, cred);
1222 #ifndef NFS_V2_ONLY
1223 if (v3)
1224 nfsm_postop_attr(vp, attrflag, 0);
1225 #endif
1226 if (!error) {
1227 #ifndef NFS_V2_ONLY
1228 if (v3) {
1229 nfsm_dissect(tl, uint32_t *, NFSX_UNSIGNED);
1230 len = fxdr_unsigned(uint32_t, *tl);
1231 if (len > MAXPATHLEN) {
1232 /*
1233 * this pathname is too long for us.
1234 */
1235 m_freem(mrep);
1236 /* Solaris returns EINVAL. should we follow? */
1237 error = ENAMETOOLONG;
1238 goto nfsmout;
1239 }
1240 } else
1241 #endif
1242 {
1243 nfsm_strsiz(len, NFS_MAXPATHLEN);
1244 }
1245 nfsm_mtouio(uiop, len);
1246 }
1247 nfsm_reqdone;
1248 return (error);
1249 }
1250
1251 /*
1252 * nfs read rpc call
1253 * Ditto above
1254 */
1255 int
1256 nfs_readrpc(vp, uiop)
1257 struct vnode *vp;
1258 struct uio *uiop;
1259 {
1260 u_int32_t *tl;
1261 caddr_t cp;
1262 int32_t t1, t2;
1263 caddr_t bpos, dpos, cp2;
1264 struct mbuf *mreq, *mrep, *md, *mb;
1265 struct nfsmount *nmp;
1266 int error = 0, len, retlen, tsiz, eof;
1267 const int v3 = NFS_ISV3(vp);
1268 struct nfsnode *np = VTONFS(vp);
1269 #ifndef NFS_V2_ONLY
1270 int attrflag;
1271 #endif
1272
1273 #ifndef nolint
1274 eof = 0;
1275 #endif
1276 nmp = VFSTONFS(vp->v_mount);
1277 tsiz = uiop->uio_resid;
1278 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1279 return (EFBIG);
1280 while (tsiz > 0) {
1281 nfsstats.rpccnt[NFSPROC_READ]++;
1282 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1283 nfsm_reqhead(np, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1284 nfsm_fhtom(np, v3);
1285 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1286 #ifndef NFS_V2_ONLY
1287 if (v3) {
1288 txdr_hyper(uiop->uio_offset, tl);
1289 *(tl + 2) = txdr_unsigned(len);
1290 } else
1291 #endif
1292 {
1293 *tl++ = txdr_unsigned(uiop->uio_offset);
1294 *tl++ = txdr_unsigned(len);
1295 *tl = 0;
1296 }
1297 nfsm_request(np, NFSPROC_READ, uiop->uio_procp, np->n_rcred);
1298 #ifndef NFS_V2_ONLY
1299 if (v3) {
1300 nfsm_postop_attr(vp, attrflag, NAC_NOTRUNC);
1301 if (error) {
1302 m_freem(mrep);
1303 goto nfsmout;
1304 }
1305 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1306 eof = fxdr_unsigned(int, *(tl + 1));
1307 } else
1308 #endif
1309 nfsm_loadattr(vp, (struct vattr *)0, NAC_NOTRUNC);
1310 nfsm_strsiz(retlen, nmp->nm_rsize);
1311 nfsm_mtouio(uiop, retlen);
1312 m_freem(mrep);
1313 tsiz -= retlen;
1314 #ifndef NFS_V2_ONLY
1315 if (v3) {
1316 if (eof || retlen == 0)
1317 tsiz = 0;
1318 } else
1319 #endif
1320 if (retlen < len)
1321 tsiz = 0;
1322 }
1323 nfsmout:
1324 return (error);
1325 }
1326
1327 struct nfs_writerpc_context {
1328 struct simplelock nwc_slock;
1329 volatile int nwc_mbufcount;
1330 };
1331
1332 /*
1333 * free mbuf used to refer protected pages while write rpc call.
1334 * called at splvm.
1335 */
1336 static void
1337 nfs_writerpc_extfree(struct mbuf *m, caddr_t buf, size_t size, void *arg)
1338 {
1339 struct nfs_writerpc_context *ctx = arg;
1340
1341 KASSERT(m != NULL);
1342 KASSERT(ctx != NULL);
1343 pool_cache_put(&mbpool_cache, m);
1344 simple_lock(&ctx->nwc_slock);
1345 if (--ctx->nwc_mbufcount == 0) {
1346 wakeup(ctx);
1347 }
1348 simple_unlock(&ctx->nwc_slock);
1349 }
1350
1351 /*
1352 * nfs write call
1353 */
1354 int
1355 nfs_writerpc(vp, uiop, iomode, pageprotected, stalewriteverfp)
1356 struct vnode *vp;
1357 struct uio *uiop;
1358 int *iomode;
1359 boolean_t pageprotected;
1360 boolean_t *stalewriteverfp;
1361 {
1362 u_int32_t *tl;
1363 caddr_t cp;
1364 int32_t t1, t2;
1365 caddr_t bpos, dpos;
1366 struct mbuf *mreq, *mrep, *md, *mb;
1367 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1368 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR;
1369 const int v3 = NFS_ISV3(vp);
1370 int committed = NFSV3WRITE_FILESYNC;
1371 struct nfsnode *np = VTONFS(vp);
1372 struct nfs_writerpc_context ctx;
1373 int s;
1374 struct lwp *l = NULL;
1375 size_t origresid;
1376 #ifndef NFS_V2_ONLY
1377 caddr_t cp2;
1378 int rlen, commit;
1379 #endif
1380
1381 simple_lock_init(&ctx.nwc_slock);
1382 ctx.nwc_mbufcount = 1;
1383
1384 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
1385 panic("writerpc readonly vp %p", vp);
1386 }
1387
1388 #ifdef DIAGNOSTIC
1389 if (uiop->uio_iovcnt != 1)
1390 panic("nfs: writerpc iovcnt > 1");
1391 #endif
1392 tsiz = uiop->uio_resid;
1393 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1394 return (EFBIG);
1395 if (pageprotected) {
1396 l = curlwp;
1397 PHOLD(l);
1398 }
1399 retry:
1400 origresid = uiop->uio_resid;
1401 KASSERT(origresid == uiop->uio_iov->iov_len);
1402 while (tsiz > 0) {
1403 uint32_t datalen; /* data bytes need to be allocated in mbuf */
1404 uint32_t backup;
1405 boolean_t stalewriteverf = FALSE;
1406
1407 nfsstats.rpccnt[NFSPROC_WRITE]++;
1408 len = min(tsiz, nmp->nm_wsize);
1409 datalen = pageprotected ? 0 : nfsm_rndup(len);
1410 nfsm_reqhead(np, NFSPROC_WRITE,
1411 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + datalen);
1412 nfsm_fhtom(np, v3);
1413 #ifndef NFS_V2_ONLY
1414 if (v3) {
1415 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1416 txdr_hyper(uiop->uio_offset, tl);
1417 tl += 2;
1418 *tl++ = txdr_unsigned(len);
1419 *tl++ = txdr_unsigned(*iomode);
1420 *tl = txdr_unsigned(len);
1421 } else
1422 #endif
1423 {
1424 u_int32_t x;
1425
1426 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1427 /* Set both "begin" and "current" to non-garbage. */
1428 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1429 *tl++ = x; /* "begin offset" */
1430 *tl++ = x; /* "current offset" */
1431 x = txdr_unsigned(len);
1432 *tl++ = x; /* total to this offset */
1433 *tl = x; /* size of this write */
1434
1435 }
1436 if (pageprotected) {
1437 /*
1438 * since we know pages can't be modified during i/o,
1439 * no need to copy them for us.
1440 */
1441 struct mbuf *m;
1442 struct iovec *iovp = uiop->uio_iov;
1443
1444 m = m_get(M_WAIT, MT_DATA);
1445 MCLAIM(m, &nfs_mowner);
1446 MEXTADD(m, iovp->iov_base, len, M_MBUF,
1447 nfs_writerpc_extfree, &ctx);
1448 m->m_flags |= M_EXT_ROMAP;
1449 m->m_len = len;
1450 mb->m_next = m;
1451 /*
1452 * no need to maintain mb and bpos here
1453 * because no one care them later.
1454 */
1455 #if 0
1456 mb = m;
1457 bpos = mtod(caddr_t, mb) + mb->m_len;
1458 #endif
1459 iovp->iov_base = (char *)iovp->iov_base + len;
1460 iovp->iov_len -= len;
1461 uiop->uio_offset += len;
1462 uiop->uio_resid -= len;
1463 s = splvm();
1464 simple_lock(&ctx.nwc_slock);
1465 ctx.nwc_mbufcount++;
1466 simple_unlock(&ctx.nwc_slock);
1467 splx(s);
1468 nfs_zeropad(mb, 0, nfsm_padlen(len));
1469 } else {
1470 nfsm_uiotom(uiop, len);
1471 }
1472 nfsm_request(np, NFSPROC_WRITE, uiop->uio_procp, np->n_wcred);
1473 #ifndef NFS_V2_ONLY
1474 if (v3) {
1475 wccflag = NFSV3_WCCCHK;
1476 nfsm_wcc_data(vp, wccflag, NAC_NOTRUNC);
1477 if (!error) {
1478 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1479 + NFSX_V3WRITEVERF);
1480 rlen = fxdr_unsigned(int, *tl++);
1481 if (rlen == 0) {
1482 error = NFSERR_IO;
1483 m_freem(mrep);
1484 break;
1485 } else if (rlen < len) {
1486 backup = len - rlen;
1487 uiop->uio_iov->iov_base =
1488 (caddr_t)uiop->uio_iov->iov_base -
1489 backup;
1490 uiop->uio_iov->iov_len += backup;
1491 uiop->uio_offset -= backup;
1492 uiop->uio_resid += backup;
1493 len = rlen;
1494 }
1495 commit = fxdr_unsigned(int, *tl++);
1496
1497 /*
1498 * Return the lowest committment level
1499 * obtained by any of the RPCs.
1500 */
1501 if (committed == NFSV3WRITE_FILESYNC)
1502 committed = commit;
1503 else if (committed == NFSV3WRITE_DATASYNC &&
1504 commit == NFSV3WRITE_UNSTABLE)
1505 committed = commit;
1506 simple_lock(&nmp->nm_slock);
1507 if ((nmp->nm_iflag & NFSMNT_HASWRITEVERF) == 0){
1508 memcpy(nmp->nm_writeverf, tl,
1509 NFSX_V3WRITEVERF);
1510 nmp->nm_iflag |= NFSMNT_HASWRITEVERF;
1511 } else if ((nmp->nm_iflag &
1512 NFSMNT_STALEWRITEVERF) ||
1513 memcmp(tl, nmp->nm_writeverf,
1514 NFSX_V3WRITEVERF)) {
1515 memcpy(nmp->nm_writeverf, tl,
1516 NFSX_V3WRITEVERF);
1517 /*
1518 * note NFSMNT_STALEWRITEVERF
1519 * if we're the first thread to
1520 * notice it.
1521 */
1522 if ((nmp->nm_iflag &
1523 NFSMNT_STALEWRITEVERF) == 0) {
1524 stalewriteverf = TRUE;
1525 nmp->nm_iflag |=
1526 NFSMNT_STALEWRITEVERF;
1527 }
1528 }
1529 simple_unlock(&nmp->nm_slock);
1530 }
1531 } else
1532 #endif
1533 nfsm_loadattr(vp, (struct vattr *)0, NAC_NOTRUNC);
1534 if (wccflag)
1535 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr->va_mtime;
1536 m_freem(mrep);
1537 if (error)
1538 break;
1539 tsiz -= len;
1540 if (stalewriteverf) {
1541 *stalewriteverfp = TRUE;
1542 stalewriteverf = FALSE;
1543 if (committed == NFSV3WRITE_UNSTABLE &&
1544 len != origresid) {
1545 /*
1546 * if our write requests weren't atomic but
1547 * unstable, datas in previous iterations
1548 * might have already been lost now.
1549 * then, we should resend them to nfsd.
1550 */
1551 backup = origresid - tsiz;
1552 uiop->uio_iov->iov_base =
1553 (caddr_t)uiop->uio_iov->iov_base - backup;
1554 uiop->uio_iov->iov_len += backup;
1555 uiop->uio_offset -= backup;
1556 uiop->uio_resid += backup;
1557 tsiz = origresid;
1558 goto retry;
1559 }
1560 }
1561 }
1562 if (pageprotected) {
1563 /*
1564 * wait until mbufs go away.
1565 * retransmitted mbufs can survive longer than rpc requests
1566 * themselves.
1567 */
1568 s = splvm();
1569 simple_lock(&ctx.nwc_slock);
1570 ctx.nwc_mbufcount--;
1571 while (ctx.nwc_mbufcount > 0) {
1572 ltsleep(&ctx, PRIBIO, "nfsmblk", 0, &ctx.nwc_slock);
1573 }
1574 simple_unlock(&ctx.nwc_slock);
1575 splx(s);
1576 PRELE(l);
1577 }
1578 nfsmout:
1579 *iomode = committed;
1580 if (error)
1581 uiop->uio_resid = tsiz;
1582 return (error);
1583 }
1584
1585 /*
1586 * nfs mknod rpc
1587 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1588 * mode set to specify the file type and the size field for rdev.
1589 */
1590 int
1591 nfs_mknodrpc(dvp, vpp, cnp, vap)
1592 struct vnode *dvp;
1593 struct vnode **vpp;
1594 struct componentname *cnp;
1595 struct vattr *vap;
1596 {
1597 struct nfsv2_sattr *sp;
1598 u_int32_t *tl;
1599 caddr_t cp;
1600 int32_t t1, t2;
1601 struct vnode *newvp = (struct vnode *)0;
1602 struct nfsnode *dnp, *np;
1603 char *cp2;
1604 caddr_t bpos, dpos;
1605 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1606 struct mbuf *mreq, *mrep, *md, *mb;
1607 u_int32_t rdev;
1608 const int v3 = NFS_ISV3(dvp);
1609
1610 if (vap->va_type == VCHR || vap->va_type == VBLK)
1611 rdev = txdr_unsigned(vap->va_rdev);
1612 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1613 rdev = nfs_xdrneg1;
1614 else {
1615 VOP_ABORTOP(dvp, cnp);
1616 vput(dvp);
1617 return (EOPNOTSUPP);
1618 }
1619 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1620 dnp = VTONFS(dvp);
1621 nfsm_reqhead(dnp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1622 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1623 nfsm_fhtom(dnp, v3);
1624 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1625 #ifndef NFS_V2_ONLY
1626 if (v3) {
1627 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1628 *tl++ = vtonfsv3_type(vap->va_type);
1629 nfsm_v3attrbuild(vap, FALSE);
1630 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1631 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1632 *tl++ = txdr_unsigned(major(vap->va_rdev));
1633 *tl = txdr_unsigned(minor(vap->va_rdev));
1634 }
1635 } else
1636 #endif
1637 {
1638 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1639 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1640 sp->sa_uid = nfs_xdrneg1;
1641 sp->sa_gid = nfs_xdrneg1;
1642 sp->sa_size = rdev;
1643 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1644 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1645 }
1646 nfsm_request(dnp, NFSPROC_MKNOD, cnp->cn_proc, cnp->cn_cred);
1647 if (!error) {
1648 nfsm_mtofh(dvp, newvp, v3, gotvp);
1649 if (!gotvp) {
1650 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1651 cnp->cn_namelen, cnp->cn_cred, cnp->cn_proc, &np);
1652 if (!error)
1653 newvp = NFSTOV(np);
1654 }
1655 }
1656 #ifndef NFS_V2_ONLY
1657 if (v3)
1658 nfsm_wcc_data(dvp, wccflag, 0);
1659 #endif
1660 nfsm_reqdone;
1661 if (error) {
1662 if (newvp)
1663 vput(newvp);
1664 } else {
1665 if (cnp->cn_flags & MAKEENTRY)
1666 nfs_cache_enter(dvp, newvp, cnp);
1667 *vpp = newvp;
1668 }
1669 PNBUF_PUT(cnp->cn_pnbuf);
1670 VTONFS(dvp)->n_flag |= NMODIFIED;
1671 if (!wccflag)
1672 NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
1673 vput(dvp);
1674 return (error);
1675 }
1676
1677 /*
1678 * nfs mknod vop
1679 * just call nfs_mknodrpc() to do the work.
1680 */
1681 /* ARGSUSED */
1682 int
1683 nfs_mknod(v)
1684 void *v;
1685 {
1686 struct vop_mknod_args /* {
1687 struct vnode *a_dvp;
1688 struct vnode **a_vpp;
1689 struct componentname *a_cnp;
1690 struct vattr *a_vap;
1691 } */ *ap = v;
1692 struct vnode *dvp = ap->a_dvp;
1693 struct componentname *cnp = ap->a_cnp;
1694 int error;
1695
1696 error = nfs_mknodrpc(dvp, ap->a_vpp, cnp, ap->a_vap);
1697 VN_KNOTE(dvp, NOTE_WRITE);
1698 if (error == 0 || error == EEXIST)
1699 cache_purge1(dvp, cnp, 0);
1700 return (error);
1701 }
1702
1703 #ifndef NFS_V2_ONLY
1704 static u_long create_verf;
1705 #endif
1706 /*
1707 * nfs file create call
1708 */
1709 int
1710 nfs_create(v)
1711 void *v;
1712 {
1713 struct vop_create_args /* {
1714 struct vnode *a_dvp;
1715 struct vnode **a_vpp;
1716 struct componentname *a_cnp;
1717 struct vattr *a_vap;
1718 } */ *ap = v;
1719 struct vnode *dvp = ap->a_dvp;
1720 struct vattr *vap = ap->a_vap;
1721 struct componentname *cnp = ap->a_cnp;
1722 struct nfsv2_sattr *sp;
1723 u_int32_t *tl;
1724 caddr_t cp;
1725 int32_t t1, t2;
1726 struct nfsnode *dnp, *np = (struct nfsnode *)0;
1727 struct vnode *newvp = (struct vnode *)0;
1728 caddr_t bpos, dpos, cp2;
1729 int error, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1730 struct mbuf *mreq, *mrep, *md, *mb;
1731 const int v3 = NFS_ISV3(dvp);
1732
1733 /*
1734 * Oops, not for me..
1735 */
1736 if (vap->va_type == VSOCK)
1737 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1738
1739 KASSERT(vap->va_type == VREG);
1740
1741 #ifdef VA_EXCLUSIVE
1742 if (vap->va_vaflags & VA_EXCLUSIVE)
1743 fmode |= O_EXCL;
1744 #endif
1745 again:
1746 error = 0;
1747 nfsstats.rpccnt[NFSPROC_CREATE]++;
1748 dnp = VTONFS(dvp);
1749 nfsm_reqhead(dnp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1750 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1751 nfsm_fhtom(dnp, v3);
1752 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1753 #ifndef NFS_V2_ONLY
1754 if (v3) {
1755 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1756 if (fmode & O_EXCL) {
1757 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1758 nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF);
1759 #ifdef INET
1760 if (TAILQ_FIRST(&in_ifaddr))
1761 *tl++ = TAILQ_FIRST(&in_ifaddr)->ia_addr.sin_addr.s_addr;
1762 else
1763 *tl++ = create_verf;
1764 #else
1765 *tl++ = create_verf;
1766 #endif
1767 *tl = ++create_verf;
1768 } else {
1769 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1770 nfsm_v3attrbuild(vap, FALSE);
1771 }
1772 } else
1773 #endif
1774 {
1775 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1776 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1777 sp->sa_uid = nfs_xdrneg1;
1778 sp->sa_gid = nfs_xdrneg1;
1779 sp->sa_size = 0;
1780 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1781 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1782 }
1783 nfsm_request(dnp, NFSPROC_CREATE, cnp->cn_proc, cnp->cn_cred);
1784 if (!error) {
1785 nfsm_mtofh(dvp, newvp, v3, gotvp);
1786 if (!gotvp) {
1787 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1788 cnp->cn_namelen, cnp->cn_cred, cnp->cn_proc, &np);
1789 if (!error)
1790 newvp = NFSTOV(np);
1791 }
1792 }
1793 #ifndef NFS_V2_ONLY
1794 if (v3)
1795 nfsm_wcc_data(dvp, wccflag, 0);
1796 #endif
1797 nfsm_reqdone;
1798 if (error) {
1799 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1800 fmode &= ~O_EXCL;
1801 goto again;
1802 }
1803 } else if (v3 && (fmode & O_EXCL)) {
1804 struct timeval tm = time;
1805
1806 /*
1807 * make sure that we'll update timestamps as
1808 * most server implementations use them to store
1809 * the create verifier.
1810 *
1811 * XXX it's better to use TOSERVER always.
1812 */
1813
1814 if (vap->va_atime.tv_sec == VNOVAL) {
1815 vap->va_atime.tv_sec = tm.tv_sec;
1816 vap->va_atime.tv_nsec = tm.tv_usec * 1000;
1817 }
1818 if (vap->va_mtime.tv_sec == VNOVAL) {
1819 vap->va_mtime.tv_sec = tm.tv_sec;
1820 vap->va_mtime.tv_nsec = tm.tv_usec * 1000;
1821 }
1822
1823 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_proc);
1824 }
1825 if (error == 0) {
1826 if (cnp->cn_flags & MAKEENTRY)
1827 nfs_cache_enter(dvp, newvp, cnp);
1828 else
1829 cache_purge1(dvp, cnp, 0);
1830 *ap->a_vpp = newvp;
1831 } else {
1832 if (newvp)
1833 vput(newvp);
1834 if (error == EEXIST)
1835 cache_purge1(dvp, cnp, 0);
1836 }
1837 PNBUF_PUT(cnp->cn_pnbuf);
1838 VTONFS(dvp)->n_flag |= NMODIFIED;
1839 if (!wccflag)
1840 NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
1841 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
1842 vput(dvp);
1843 return (error);
1844 }
1845
1846 /*
1847 * nfs file remove call
1848 * To try and make nfs semantics closer to ufs semantics, a file that has
1849 * other processes using the vnode is renamed instead of removed and then
1850 * removed later on the last close.
1851 * - If v_usecount > 1
1852 * If a rename is not already in the works
1853 * call nfs_sillyrename() to set it up
1854 * else
1855 * do the remove rpc
1856 */
1857 int
1858 nfs_remove(v)
1859 void *v;
1860 {
1861 struct vop_remove_args /* {
1862 struct vnodeop_desc *a_desc;
1863 struct vnode * a_dvp;
1864 struct vnode * a_vp;
1865 struct componentname * a_cnp;
1866 } */ *ap = v;
1867 struct vnode *vp = ap->a_vp;
1868 struct vnode *dvp = ap->a_dvp;
1869 struct componentname *cnp = ap->a_cnp;
1870 struct nfsnode *np = VTONFS(vp);
1871 int error = 0;
1872 struct vattr vattr;
1873
1874 #ifndef DIAGNOSTIC
1875 if ((cnp->cn_flags & HASBUF) == 0)
1876 panic("nfs_remove: no name");
1877 if (vp->v_usecount < 1)
1878 panic("nfs_remove: bad v_usecount");
1879 #endif
1880 if (vp->v_type == VDIR)
1881 error = EPERM;
1882 else if (vp->v_usecount == 1 || (np->n_sillyrename &&
1883 VOP_GETATTR(vp, &vattr, cnp->cn_cred, cnp->cn_proc) == 0 &&
1884 vattr.va_nlink > 1)) {
1885 /*
1886 * Purge the name cache so that the chance of a lookup for
1887 * the name succeeding while the remove is in progress is
1888 * minimized. Without node locking it can still happen, such
1889 * that an I/O op returns ESTALE, but since you get this if
1890 * another host removes the file..
1891 */
1892 cache_purge(vp);
1893 /*
1894 * throw away biocache buffers, mainly to avoid
1895 * unnecessary delayed writes later.
1896 */
1897 error = nfs_vinvalbuf(vp, 0, cnp->cn_cred, cnp->cn_proc, 1);
1898 /* Do the rpc */
1899 if (error != EINTR)
1900 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1901 cnp->cn_namelen, cnp->cn_cred, cnp->cn_proc);
1902 } else if (!np->n_sillyrename)
1903 error = nfs_sillyrename(dvp, vp, cnp);
1904 PNBUF_PUT(cnp->cn_pnbuf);
1905 if (!error && nfs_getattrcache(vp, &vattr) == 0 &&
1906 vattr.va_nlink == 1) {
1907 np->n_flag |= NREMOVED;
1908 }
1909 NFS_INVALIDATE_ATTRCACHE(np);
1910 VN_KNOTE(vp, NOTE_DELETE);
1911 VN_KNOTE(dvp, NOTE_WRITE);
1912 if (dvp == vp)
1913 vrele(vp);
1914 else
1915 vput(vp);
1916 vput(dvp);
1917 return (error);
1918 }
1919
1920 /*
1921 * nfs file remove rpc called from nfs_inactive
1922 */
1923 int
1924 nfs_removeit(sp)
1925 struct sillyrename *sp;
1926 {
1927
1928 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen, sp->s_cred,
1929 (struct proc *)0));
1930 }
1931
1932 /*
1933 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1934 */
1935 int
1936 nfs_removerpc(dvp, name, namelen, cred, proc)
1937 struct vnode *dvp;
1938 const char *name;
1939 int namelen;
1940 struct ucred *cred;
1941 struct proc *proc;
1942 {
1943 u_int32_t *tl;
1944 caddr_t cp;
1945 #ifndef NFS_V2_ONLY
1946 int32_t t1;
1947 caddr_t cp2;
1948 #endif
1949 int32_t t2;
1950 caddr_t bpos, dpos;
1951 int error = 0, wccflag = NFSV3_WCCRATTR;
1952 struct mbuf *mreq, *mrep, *md, *mb;
1953 const int v3 = NFS_ISV3(dvp);
1954 int rexmit;
1955 struct nfsnode *dnp = VTONFS(dvp);
1956
1957 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1958 nfsm_reqhead(dnp, NFSPROC_REMOVE,
1959 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1960 nfsm_fhtom(dnp, v3);
1961 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1962 nfsm_request1(dnp, NFSPROC_REMOVE, proc, cred, &rexmit);
1963 #ifndef NFS_V2_ONLY
1964 if (v3)
1965 nfsm_wcc_data(dvp, wccflag, 0);
1966 #endif
1967 nfsm_reqdone;
1968 VTONFS(dvp)->n_flag |= NMODIFIED;
1969 if (!wccflag)
1970 NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
1971 /*
1972 * Kludge City: If the first reply to the remove rpc is lost..
1973 * the reply to the retransmitted request will be ENOENT
1974 * since the file was in fact removed
1975 * Therefore, we cheat and return success.
1976 */
1977 if (rexmit && error == ENOENT)
1978 error = 0;
1979 return (error);
1980 }
1981
1982 /*
1983 * nfs file rename call
1984 */
1985 int
1986 nfs_rename(v)
1987 void *v;
1988 {
1989 struct vop_rename_args /* {
1990 struct vnode *a_fdvp;
1991 struct vnode *a_fvp;
1992 struct componentname *a_fcnp;
1993 struct vnode *a_tdvp;
1994 struct vnode *a_tvp;
1995 struct componentname *a_tcnp;
1996 } */ *ap = v;
1997 struct vnode *fvp = ap->a_fvp;
1998 struct vnode *tvp = ap->a_tvp;
1999 struct vnode *fdvp = ap->a_fdvp;
2000 struct vnode *tdvp = ap->a_tdvp;
2001 struct componentname *tcnp = ap->a_tcnp;
2002 struct componentname *fcnp = ap->a_fcnp;
2003 int error;
2004
2005 #ifndef DIAGNOSTIC
2006 if ((tcnp->cn_flags & HASBUF) == 0 ||
2007 (fcnp->cn_flags & HASBUF) == 0)
2008 panic("nfs_rename: no name");
2009 #endif
2010 /* Check for cross-device rename */
2011 if ((fvp->v_mount != tdvp->v_mount) ||
2012 (tvp && (fvp->v_mount != tvp->v_mount))) {
2013 error = EXDEV;
2014 goto out;
2015 }
2016
2017 /*
2018 * If the tvp exists and is in use, sillyrename it before doing the
2019 * rename of the new file over it.
2020 */
2021 if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename &&
2022 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
2023 VN_KNOTE(tvp, NOTE_DELETE);
2024 vput(tvp);
2025 tvp = NULL;
2026 }
2027
2028 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
2029 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
2030 tcnp->cn_proc);
2031
2032 VN_KNOTE(fdvp, NOTE_WRITE);
2033 VN_KNOTE(tdvp, NOTE_WRITE);
2034 if (error == 0 || error == EEXIST) {
2035 if (fvp->v_type == VDIR)
2036 cache_purge(fvp);
2037 else
2038 cache_purge1(fdvp, fcnp, 0);
2039 if (tvp != NULL && tvp->v_type == VDIR)
2040 cache_purge(tvp);
2041 else
2042 cache_purge1(tdvp, tcnp, 0);
2043 }
2044 out:
2045 if (tdvp == tvp)
2046 vrele(tdvp);
2047 else
2048 vput(tdvp);
2049 if (tvp)
2050 vput(tvp);
2051 vrele(fdvp);
2052 vrele(fvp);
2053 return (error);
2054 }
2055
2056 /*
2057 * nfs file rename rpc called from nfs_remove() above
2058 */
2059 int
2060 nfs_renameit(sdvp, scnp, sp)
2061 struct vnode *sdvp;
2062 struct componentname *scnp;
2063 struct sillyrename *sp;
2064 {
2065 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
2066 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_proc));
2067 }
2068
2069 /*
2070 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
2071 */
2072 int
2073 nfs_renamerpc(fdvp, fnameptr, fnamelen, tdvp, tnameptr, tnamelen, cred, proc)
2074 struct vnode *fdvp;
2075 const char *fnameptr;
2076 int fnamelen;
2077 struct vnode *tdvp;
2078 const char *tnameptr;
2079 int tnamelen;
2080 struct ucred *cred;
2081 struct proc *proc;
2082 {
2083 u_int32_t *tl;
2084 caddr_t cp;
2085 #ifndef NFS_V2_ONLY
2086 int32_t t1;
2087 caddr_t cp2;
2088 #endif
2089 int32_t t2;
2090 caddr_t bpos, dpos;
2091 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
2092 struct mbuf *mreq, *mrep, *md, *mb;
2093 const int v3 = NFS_ISV3(fdvp);
2094 int rexmit;
2095 struct nfsnode *fdnp = VTONFS(fdvp);
2096
2097 nfsstats.rpccnt[NFSPROC_RENAME]++;
2098 nfsm_reqhead(fdnp, NFSPROC_RENAME,
2099 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
2100 nfsm_rndup(tnamelen));
2101 nfsm_fhtom(fdnp, v3);
2102 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
2103 nfsm_fhtom(VTONFS(tdvp), v3);
2104 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
2105 nfsm_request1(fdnp, NFSPROC_RENAME, proc, cred, &rexmit);
2106 #ifndef NFS_V2_ONLY
2107 if (v3) {
2108 nfsm_wcc_data(fdvp, fwccflag, 0);
2109 nfsm_wcc_data(tdvp, twccflag, 0);
2110 }
2111 #endif
2112 nfsm_reqdone;
2113 VTONFS(fdvp)->n_flag |= NMODIFIED;
2114 VTONFS(tdvp)->n_flag |= NMODIFIED;
2115 if (!fwccflag)
2116 NFS_INVALIDATE_ATTRCACHE(VTONFS(fdvp));
2117 if (!twccflag)
2118 NFS_INVALIDATE_ATTRCACHE(VTONFS(tdvp));
2119 /*
2120 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
2121 */
2122 if (rexmit && error == ENOENT)
2123 error = 0;
2124 return (error);
2125 }
2126
2127 /*
2128 * nfs hard link create call
2129 */
2130 int
2131 nfs_link(v)
2132 void *v;
2133 {
2134 struct vop_link_args /* {
2135 struct vnode *a_dvp;
2136 struct vnode *a_vp;
2137 struct componentname *a_cnp;
2138 } */ *ap = v;
2139 struct vnode *vp = ap->a_vp;
2140 struct vnode *dvp = ap->a_dvp;
2141 struct componentname *cnp = ap->a_cnp;
2142 u_int32_t *tl;
2143 caddr_t cp;
2144 #ifndef NFS_V2_ONLY
2145 int32_t t1;
2146 caddr_t cp2;
2147 #endif
2148 int32_t t2;
2149 caddr_t bpos, dpos;
2150 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
2151 struct mbuf *mreq, *mrep, *md, *mb;
2152 /* XXX Should be const and initialised? */
2153 int v3;
2154 int rexmit;
2155 struct nfsnode *np;
2156
2157 if (dvp->v_mount != vp->v_mount) {
2158 VOP_ABORTOP(dvp, cnp);
2159 vput(dvp);
2160 return (EXDEV);
2161 }
2162 if (dvp != vp) {
2163 error = vn_lock(vp, LK_EXCLUSIVE);
2164 if (error != 0) {
2165 VOP_ABORTOP(dvp, cnp);
2166 vput(dvp);
2167 return error;
2168 }
2169 }
2170
2171 /*
2172 * Push all writes to the server, so that the attribute cache
2173 * doesn't get "out of sync" with the server.
2174 * XXX There should be a better way!
2175 */
2176 VOP_FSYNC(vp, cnp->cn_cred, FSYNC_WAIT, 0, 0, cnp->cn_proc);
2177
2178 v3 = NFS_ISV3(vp);
2179 nfsstats.rpccnt[NFSPROC_LINK]++;
2180 np = VTONFS(vp);
2181 nfsm_reqhead(np, NFSPROC_LINK,
2182 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2183 nfsm_fhtom(np, v3);
2184 nfsm_fhtom(VTONFS(dvp), v3);
2185 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2186 nfsm_request1(np, NFSPROC_LINK, cnp->cn_proc, cnp->cn_cred, &rexmit);
2187 #ifndef NFS_V2_ONLY
2188 if (v3) {
2189 nfsm_postop_attr(vp, attrflag, 0);
2190 nfsm_wcc_data(dvp, wccflag, 0);
2191 }
2192 #endif
2193 nfsm_reqdone;
2194 if (error == 0 || error == EEXIST)
2195 cache_purge1(dvp, cnp, 0);
2196 PNBUF_PUT(cnp->cn_pnbuf);
2197 VTONFS(dvp)->n_flag |= NMODIFIED;
2198 if (!attrflag)
2199 NFS_INVALIDATE_ATTRCACHE(VTONFS(vp));
2200 if (!wccflag)
2201 NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
2202 if (dvp != vp)
2203 VOP_UNLOCK(vp, 0);
2204 VN_KNOTE(vp, NOTE_LINK);
2205 VN_KNOTE(dvp, NOTE_WRITE);
2206 vput(dvp);
2207 /*
2208 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2209 */
2210 if (rexmit && error == EEXIST)
2211 error = 0;
2212 return (error);
2213 }
2214
2215 /*
2216 * nfs symbolic link create call
2217 */
2218 int
2219 nfs_symlink(v)
2220 void *v;
2221 {
2222 struct vop_symlink_args /* {
2223 struct vnode *a_dvp;
2224 struct vnode **a_vpp;
2225 struct componentname *a_cnp;
2226 struct vattr *a_vap;
2227 char *a_target;
2228 } */ *ap = v;
2229 struct vnode *dvp = ap->a_dvp;
2230 struct vattr *vap = ap->a_vap;
2231 struct componentname *cnp = ap->a_cnp;
2232 struct nfsv2_sattr *sp;
2233 u_int32_t *tl;
2234 caddr_t cp;
2235 int32_t t1, t2;
2236 caddr_t bpos, dpos, cp2;
2237 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
2238 struct mbuf *mreq, *mrep, *md, *mb;
2239 struct vnode *newvp = (struct vnode *)0;
2240 const int v3 = NFS_ISV3(dvp);
2241 int rexmit;
2242 struct nfsnode *dnp = VTONFS(dvp);
2243
2244 *ap->a_vpp = NULL;
2245 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
2246 slen = strlen(ap->a_target);
2247 nfsm_reqhead(dnp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
2248 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
2249 nfsm_fhtom(dnp, v3);
2250 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2251 #ifndef NFS_V2_ONlY
2252 if (v3)
2253 nfsm_v3attrbuild(vap, FALSE);
2254 #endif
2255 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
2256 #ifndef NFS_V2_ONlY
2257 if (!v3) {
2258 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2259 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
2260 sp->sa_uid = nfs_xdrneg1;
2261 sp->sa_gid = nfs_xdrneg1;
2262 sp->sa_size = nfs_xdrneg1;
2263 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2264 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2265 }
2266 #endif
2267 nfsm_request1(dnp, NFSPROC_SYMLINK, cnp->cn_proc, cnp->cn_cred,
2268 &rexmit);
2269 #ifndef NFS_V2_ONlY
2270 if (v3) {
2271 if (!error)
2272 nfsm_mtofh(dvp, newvp, v3, gotvp);
2273 nfsm_wcc_data(dvp, wccflag, 0);
2274 }
2275 #endif
2276 nfsm_reqdone;
2277 /*
2278 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2279 */
2280 if (rexmit && error == EEXIST)
2281 error = 0;
2282 if (error == 0 || error == EEXIST)
2283 cache_purge1(dvp, cnp, 0);
2284 if (error == 0 && newvp == NULL) {
2285 struct nfsnode *np = NULL;
2286
2287 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2288 cnp->cn_cred, cnp->cn_proc, &np);
2289 if (error == 0)
2290 newvp = NFSTOV(np);
2291 }
2292 if (error) {
2293 if (newvp != NULL)
2294 vput(newvp);
2295 } else {
2296 *ap->a_vpp = newvp;
2297 }
2298 PNBUF_PUT(cnp->cn_pnbuf);
2299 VTONFS(dvp)->n_flag |= NMODIFIED;
2300 if (!wccflag)
2301 NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
2302 VN_KNOTE(dvp, NOTE_WRITE);
2303 vput(dvp);
2304 return (error);
2305 }
2306
2307 /*
2308 * nfs make dir call
2309 */
2310 int
2311 nfs_mkdir(v)
2312 void *v;
2313 {
2314 struct vop_mkdir_args /* {
2315 struct vnode *a_dvp;
2316 struct vnode **a_vpp;
2317 struct componentname *a_cnp;
2318 struct vattr *a_vap;
2319 } */ *ap = v;
2320 struct vnode *dvp = ap->a_dvp;
2321 struct vattr *vap = ap->a_vap;
2322 struct componentname *cnp = ap->a_cnp;
2323 struct nfsv2_sattr *sp;
2324 u_int32_t *tl;
2325 caddr_t cp;
2326 int32_t t1, t2;
2327 int len;
2328 struct nfsnode *dnp = VTONFS(dvp), *np = (struct nfsnode *)0;
2329 struct vnode *newvp = (struct vnode *)0;
2330 caddr_t bpos, dpos, cp2;
2331 int error = 0, wccflag = NFSV3_WCCRATTR;
2332 int gotvp = 0;
2333 int rexmit;
2334 struct mbuf *mreq, *mrep, *md, *mb;
2335 const int v3 = NFS_ISV3(dvp);
2336
2337 len = cnp->cn_namelen;
2338 nfsstats.rpccnt[NFSPROC_MKDIR]++;
2339 nfsm_reqhead(dnp, NFSPROC_MKDIR,
2340 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
2341 nfsm_fhtom(dnp, v3);
2342 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
2343 #ifndef NFS_V2_ONLY
2344 if (v3) {
2345 nfsm_v3attrbuild(vap, FALSE);
2346 } else
2347 #endif
2348 {
2349 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2350 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
2351 sp->sa_uid = nfs_xdrneg1;
2352 sp->sa_gid = nfs_xdrneg1;
2353 sp->sa_size = nfs_xdrneg1;
2354 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2355 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2356 }
2357 nfsm_request1(dnp, NFSPROC_MKDIR, cnp->cn_proc, cnp->cn_cred, &rexmit);
2358 if (!error)
2359 nfsm_mtofh(dvp, newvp, v3, gotvp);
2360 if (v3)
2361 nfsm_wcc_data(dvp, wccflag, 0);
2362 nfsm_reqdone;
2363 VTONFS(dvp)->n_flag |= NMODIFIED;
2364 if (!wccflag)
2365 NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
2366 /*
2367 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2368 * if we can succeed in looking up the directory.
2369 */
2370 if ((rexmit && error == EEXIST) || (!error && !gotvp)) {
2371 if (newvp) {
2372 vput(newvp);
2373 newvp = (struct vnode *)0;
2374 }
2375 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2376 cnp->cn_proc, &np);
2377 if (!error) {
2378 newvp = NFSTOV(np);
2379 if (newvp->v_type != VDIR || newvp == dvp)
2380 error = EEXIST;
2381 }
2382 }
2383 if (error) {
2384 if (newvp) {
2385 if (dvp != newvp)
2386 vput(newvp);
2387 else
2388 vrele(newvp);
2389 }
2390 } else {
2391 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
2392 if (cnp->cn_flags & MAKEENTRY)
2393 nfs_cache_enter(dvp, newvp, cnp);
2394 *ap->a_vpp = newvp;
2395 }
2396 PNBUF_PUT(cnp->cn_pnbuf);
2397 vput(dvp);
2398 return (error);
2399 }
2400
2401 /*
2402 * nfs remove directory call
2403 */
2404 int
2405 nfs_rmdir(v)
2406 void *v;
2407 {
2408 struct vop_rmdir_args /* {
2409 struct vnode *a_dvp;
2410 struct vnode *a_vp;
2411 struct componentname *a_cnp;
2412 } */ *ap = v;
2413 struct vnode *vp = ap->a_vp;
2414 struct vnode *dvp = ap->a_dvp;
2415 struct componentname *cnp = ap->a_cnp;
2416 u_int32_t *tl;
2417 caddr_t cp;
2418 #ifndef NFS_V2_ONLY
2419 int32_t t1;
2420 caddr_t cp2;
2421 #endif
2422 int32_t t2;
2423 caddr_t bpos, dpos;
2424 int error = 0, wccflag = NFSV3_WCCRATTR;
2425 int rexmit;
2426 struct mbuf *mreq, *mrep, *md, *mb;
2427 const int v3 = NFS_ISV3(dvp);
2428 struct nfsnode *dnp;
2429
2430 if (dvp == vp) {
2431 vrele(dvp);
2432 vput(dvp);
2433 PNBUF_PUT(cnp->cn_pnbuf);
2434 return (EINVAL);
2435 }
2436 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2437 dnp = VTONFS(dvp);
2438 nfsm_reqhead(dnp, NFSPROC_RMDIR,
2439 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2440 nfsm_fhtom(dnp, v3);
2441 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2442 nfsm_request1(dnp, NFSPROC_RMDIR, cnp->cn_proc, cnp->cn_cred, &rexmit);
2443 #ifndef NFS_V2_ONLY
2444 if (v3)
2445 nfsm_wcc_data(dvp, wccflag, 0);
2446 #endif
2447 nfsm_reqdone;
2448 PNBUF_PUT(cnp->cn_pnbuf);
2449 VTONFS(dvp)->n_flag |= NMODIFIED;
2450 if (!wccflag)
2451 NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
2452 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
2453 VN_KNOTE(vp, NOTE_DELETE);
2454 cache_purge(vp);
2455 vput(vp);
2456 vput(dvp);
2457 /*
2458 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2459 */
2460 if (rexmit && error == ENOENT)
2461 error = 0;
2462 return (error);
2463 }
2464
2465 /*
2466 * nfs readdir call
2467 */
2468 int
2469 nfs_readdir(v)
2470 void *v;
2471 {
2472 struct vop_readdir_args /* {
2473 struct vnode *a_vp;
2474 struct uio *a_uio;
2475 struct ucred *a_cred;
2476 int *a_eofflag;
2477 off_t **a_cookies;
2478 int *a_ncookies;
2479 } */ *ap = v;
2480 struct vnode *vp = ap->a_vp;
2481 struct uio *uio = ap->a_uio;
2482 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2483 char *base = uio->uio_iov->iov_base;
2484 int tresid, error;
2485 size_t count, lost;
2486 struct dirent *dp;
2487 off_t *cookies = NULL;
2488 int ncookies = 0, nc;
2489
2490 if (vp->v_type != VDIR)
2491 return (EPERM);
2492
2493 lost = uio->uio_resid & (NFS_DIRFRAGSIZ - 1);
2494 count = uio->uio_resid - lost;
2495 if (count <= 0)
2496 return (EINVAL);
2497
2498 /*
2499 * Call nfs_bioread() to do the real work.
2500 */
2501 tresid = uio->uio_resid = count;
2502 error = nfs_bioread(vp, uio, 0, ap->a_cred,
2503 ap->a_cookies ? NFSBIO_CACHECOOKIES : 0);
2504
2505 if (!error && ap->a_cookies) {
2506 ncookies = count / 16;
2507 cookies = malloc(sizeof (off_t) * ncookies, M_TEMP, M_WAITOK);
2508 *ap->a_cookies = cookies;
2509 }
2510
2511 if (!error && uio->uio_resid == tresid) {
2512 uio->uio_resid += lost;
2513 nfsstats.direofcache_misses++;
2514 if (ap->a_cookies)
2515 *ap->a_ncookies = 0;
2516 *ap->a_eofflag = 1;
2517 return (0);
2518 }
2519
2520 if (!error && ap->a_cookies) {
2521 /*
2522 * Only the NFS server and emulations use cookies, and they
2523 * load the directory block into system space, so we can
2524 * just look at it directly.
2525 */
2526 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2527 panic("nfs_readdir: lost in space");
2528 for (nc = 0; ncookies-- &&
2529 base < (char *)uio->uio_iov->iov_base; nc++){
2530 dp = (struct dirent *) base;
2531 if (dp->d_reclen == 0)
2532 break;
2533 if (nmp->nm_flag & NFSMNT_XLATECOOKIE)
2534 *(cookies++) = (off_t)NFS_GETCOOKIE32(dp);
2535 else
2536 *(cookies++) = NFS_GETCOOKIE(dp);
2537 base += dp->d_reclen;
2538 }
2539 uio->uio_resid +=
2540 ((caddr_t)uio->uio_iov->iov_base - base);
2541 uio->uio_iov->iov_len +=
2542 ((caddr_t)uio->uio_iov->iov_base - base);
2543 uio->uio_iov->iov_base = base;
2544 *ap->a_ncookies = nc;
2545 }
2546
2547 uio->uio_resid += lost;
2548 *ap->a_eofflag = 0;
2549 return (error);
2550 }
2551
2552 /*
2553 * Readdir rpc call.
2554 * Called from below the buffer cache by nfs_doio().
2555 */
2556 int
2557 nfs_readdirrpc(vp, uiop, cred)
2558 struct vnode *vp;
2559 struct uio *uiop;
2560 struct ucred *cred;
2561 {
2562 int len, left;
2563 struct dirent *dp = NULL;
2564 u_int32_t *tl;
2565 caddr_t cp;
2566 int32_t t1, t2;
2567 caddr_t bpos, dpos, cp2;
2568 struct mbuf *mreq, *mrep, *md, *mb;
2569 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2570 struct nfsnode *dnp = VTONFS(vp);
2571 u_quad_t fileno;
2572 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2573 #ifndef NFS_V2_ONLY
2574 int attrflag;
2575 #endif
2576 int nrpcs = 0, reclen;
2577 const int v3 = NFS_ISV3(vp);
2578
2579 #ifdef DIAGNOSTIC
2580 /*
2581 * Should be called from buffer cache, so only amount of
2582 * NFS_DIRBLKSIZ will be requested.
2583 */
2584 if (uiop->uio_iovcnt != 1 || (uiop->uio_resid & (NFS_DIRBLKSIZ - 1)))
2585 panic("nfs readdirrpc bad uio");
2586 #endif
2587
2588 /*
2589 * Loop around doing readdir rpc's of size nm_readdirsize
2590 * truncated to a multiple of NFS_DIRFRAGSIZ.
2591 * The stopping criteria is EOF or buffer full.
2592 */
2593 while (more_dirs && bigenough) {
2594 /*
2595 * Heuristic: don't bother to do another RPC to further
2596 * fill up this block if there is not much room left. (< 50%
2597 * of the readdir RPC size). This wastes some buffer space
2598 * but can save up to 50% in RPC calls.
2599 */
2600 if (nrpcs > 0 && uiop->uio_resid < (nmp->nm_readdirsize / 2)) {
2601 bigenough = 0;
2602 break;
2603 }
2604 nfsstats.rpccnt[NFSPROC_READDIR]++;
2605 nfsm_reqhead(dnp, NFSPROC_READDIR, NFSX_FH(v3) +
2606 NFSX_READDIR(v3));
2607 nfsm_fhtom(dnp, v3);
2608 #ifndef NFS_V2_ONLY
2609 if (v3) {
2610 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2611 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE) {
2612 txdr_swapcookie3(uiop->uio_offset, tl);
2613 } else {
2614 txdr_cookie3(uiop->uio_offset, tl);
2615 }
2616 tl += 2;
2617 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2618 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2619 } else
2620 #endif
2621 {
2622 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2623 *tl++ = txdr_unsigned(uiop->uio_offset);
2624 }
2625 *tl = txdr_unsigned(nmp->nm_readdirsize);
2626 nfsm_request(dnp, NFSPROC_READDIR, uiop->uio_procp, cred);
2627 nrpcs++;
2628 #ifndef NFS_V2_ONLY
2629 if (v3) {
2630 nfsm_postop_attr(vp, attrflag, 0);
2631 if (!error) {
2632 nfsm_dissect(tl, u_int32_t *,
2633 2 * NFSX_UNSIGNED);
2634 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2635 dnp->n_cookieverf.nfsuquad[1] = *tl;
2636 } else {
2637 m_freem(mrep);
2638 goto nfsmout;
2639 }
2640 }
2641 #endif
2642 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2643 more_dirs = fxdr_unsigned(int, *tl);
2644
2645 /* loop thru the dir entries, doctoring them to 4bsd form */
2646 while (more_dirs && bigenough) {
2647 #ifndef NFS_V2_ONLY
2648 if (v3) {
2649 nfsm_dissect(tl, u_int32_t *,
2650 3 * NFSX_UNSIGNED);
2651 fileno = fxdr_hyper(tl);
2652 len = fxdr_unsigned(int, *(tl + 2));
2653 } else
2654 #endif
2655 {
2656 nfsm_dissect(tl, u_int32_t *,
2657 2 * NFSX_UNSIGNED);
2658 fileno = fxdr_unsigned(u_quad_t, *tl++);
2659 len = fxdr_unsigned(int, *tl);
2660 }
2661 if (len <= 0 || len > NFS_MAXNAMLEN) {
2662 error = EBADRPC;
2663 m_freem(mrep);
2664 goto nfsmout;
2665 }
2666 tlen = nfsm_rndup(len);
2667 if (tlen == len)
2668 tlen += 4; /* To ensure null termination */
2669 tlen += sizeof (off_t) + sizeof (int);
2670 reclen = ALIGN(tlen + DIRHDSIZ);
2671 tlen = reclen - DIRHDSIZ;
2672 left = NFS_DIRFRAGSIZ - blksiz;
2673 if (reclen > left) {
2674 dp->d_reclen += left;
2675 uiop->uio_iov->iov_base =
2676 (caddr_t)uiop->uio_iov->iov_base + left;
2677 uiop->uio_iov->iov_len -= left;
2678 uiop->uio_resid -= left;
2679 blksiz = 0;
2680 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2681 }
2682 if (reclen > uiop->uio_resid)
2683 bigenough = 0;
2684 if (bigenough) {
2685 dp = (struct dirent *)uiop->uio_iov->iov_base;
2686 dp->d_fileno = (int)fileno;
2687 dp->d_namlen = len;
2688 dp->d_reclen = reclen;
2689 dp->d_type = DT_UNKNOWN;
2690 blksiz += dp->d_reclen;
2691 if (blksiz == NFS_DIRFRAGSIZ)
2692 blksiz = 0;
2693 uiop->uio_resid -= DIRHDSIZ;
2694 uiop->uio_iov->iov_base =
2695 (caddr_t)uiop->uio_iov->iov_base + DIRHDSIZ;
2696 uiop->uio_iov->iov_len -= DIRHDSIZ;
2697 nfsm_mtouio(uiop, len);
2698 cp = uiop->uio_iov->iov_base;
2699 tlen -= len;
2700 *cp = '\0'; /* null terminate */
2701 uiop->uio_iov->iov_base =
2702 (caddr_t)uiop->uio_iov->iov_base + tlen;
2703 uiop->uio_iov->iov_len -= tlen;
2704 uiop->uio_resid -= tlen;
2705 } else
2706 nfsm_adv(nfsm_rndup(len));
2707 #ifndef NFS_V2_ONLY
2708 if (v3) {
2709 nfsm_dissect(tl, u_int32_t *,
2710 3 * NFSX_UNSIGNED);
2711 } else
2712 #endif
2713 {
2714 nfsm_dissect(tl, u_int32_t *,
2715 2 * NFSX_UNSIGNED);
2716 }
2717 if (bigenough) {
2718 #ifndef NFS_V2_ONLY
2719 if (v3) {
2720 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE)
2721 uiop->uio_offset =
2722 fxdr_swapcookie3(tl);
2723 else
2724 uiop->uio_offset =
2725 fxdr_cookie3(tl);
2726 }
2727 else
2728 #endif
2729 {
2730 uiop->uio_offset =
2731 fxdr_unsigned(off_t, *tl);
2732 }
2733 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2734 }
2735 if (v3)
2736 tl += 2;
2737 else
2738 tl++;
2739 more_dirs = fxdr_unsigned(int, *tl);
2740 }
2741 /*
2742 * If at end of rpc data, get the eof boolean
2743 */
2744 if (!more_dirs) {
2745 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2746 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2747 }
2748 m_freem(mrep);
2749 }
2750 /*
2751 * Fill last record, iff any, out to a multiple of NFS_DIRFRAGSIZ
2752 * by increasing d_reclen for the last record.
2753 */
2754 if (blksiz > 0) {
2755 left = NFS_DIRFRAGSIZ - blksiz;
2756 dp->d_reclen += left;
2757 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2758 uiop->uio_iov->iov_base = (caddr_t)uiop->uio_iov->iov_base +
2759 left;
2760 uiop->uio_iov->iov_len -= left;
2761 uiop->uio_resid -= left;
2762 }
2763
2764 /*
2765 * We are now either at the end of the directory or have filled the
2766 * block.
2767 */
2768 if (bigenough)
2769 dnp->n_direofoffset = uiop->uio_offset;
2770 nfsmout:
2771 return (error);
2772 }
2773
2774 #ifndef NFS_V2_ONLY
2775 /*
2776 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2777 */
2778 int
2779 nfs_readdirplusrpc(vp, uiop, cred)
2780 struct vnode *vp;
2781 struct uio *uiop;
2782 struct ucred *cred;
2783 {
2784 int len, left;
2785 struct dirent *dp = NULL;
2786 u_int32_t *tl;
2787 caddr_t cp;
2788 int32_t t1, t2;
2789 struct vnode *newvp;
2790 caddr_t bpos, dpos, cp2;
2791 struct mbuf *mreq, *mrep, *md, *mb;
2792 struct nameidata nami, *ndp = &nami;
2793 struct componentname *cnp = &ndp->ni_cnd;
2794 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2795 struct nfsnode *dnp = VTONFS(vp), *np;
2796 nfsfh_t *fhp;
2797 u_quad_t fileno;
2798 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2799 int attrflag, fhsize, nrpcs = 0, reclen;
2800 struct nfs_fattr fattr, *fp;
2801
2802 #ifdef DIAGNOSTIC
2803 if (uiop->uio_iovcnt != 1 || (uiop->uio_resid & (NFS_DIRBLKSIZ - 1)))
2804 panic("nfs readdirplusrpc bad uio");
2805 #endif
2806 ndp->ni_dvp = vp;
2807 newvp = NULLVP;
2808
2809 /*
2810 * Loop around doing readdir rpc's of size nm_readdirsize
2811 * truncated to a multiple of NFS_DIRFRAGSIZ.
2812 * The stopping criteria is EOF or buffer full.
2813 */
2814 while (more_dirs && bigenough) {
2815 if (nrpcs > 0 && uiop->uio_resid < (nmp->nm_readdirsize / 2)) {
2816 bigenough = 0;
2817 break;
2818 }
2819 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2820 nfsm_reqhead(dnp, NFSPROC_READDIRPLUS,
2821 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2822 nfsm_fhtom(dnp, 1);
2823 nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2824 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE) {
2825 txdr_swapcookie3(uiop->uio_offset, tl);
2826 } else {
2827 txdr_cookie3(uiop->uio_offset, tl);
2828 }
2829 tl += 2;
2830 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2831 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2832 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2833 *tl = txdr_unsigned(nmp->nm_rsize);
2834 nfsm_request(dnp, NFSPROC_READDIRPLUS, uiop->uio_procp, cred);
2835 nfsm_postop_attr(vp, attrflag, 0);
2836 if (error) {
2837 m_freem(mrep);
2838 goto nfsmout;
2839 }
2840 nrpcs++;
2841 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2842 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2843 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2844 more_dirs = fxdr_unsigned(int, *tl);
2845
2846 /* loop thru the dir entries, doctoring them to 4bsd form */
2847 while (more_dirs && bigenough) {
2848 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2849 fileno = fxdr_hyper(tl);
2850 len = fxdr_unsigned(int, *(tl + 2));
2851 if (len <= 0 || len > NFS_MAXNAMLEN) {
2852 error = EBADRPC;
2853 m_freem(mrep);
2854 goto nfsmout;
2855 }
2856 tlen = nfsm_rndup(len);
2857 if (tlen == len)
2858 tlen += 4; /* To ensure null termination*/
2859 tlen += sizeof (off_t) + sizeof (int);
2860 reclen = ALIGN(tlen + DIRHDSIZ);
2861 tlen = reclen - DIRHDSIZ;
2862 left = NFS_DIRFRAGSIZ - blksiz;
2863 if (reclen > left) {
2864 /*
2865 * DIRFRAGSIZ is aligned, no need to align
2866 * again here.
2867 */
2868 dp->d_reclen += left;
2869 uiop->uio_iov->iov_base =
2870 (caddr_t)uiop->uio_iov->iov_base + left;
2871 uiop->uio_iov->iov_len -= left;
2872 uiop->uio_resid -= left;
2873 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2874 blksiz = 0;
2875 }
2876 if (reclen > uiop->uio_resid)
2877 bigenough = 0;
2878 if (bigenough) {
2879 dp = (struct dirent *)uiop->uio_iov->iov_base;
2880 dp->d_fileno = (int)fileno;
2881 dp->d_namlen = len;
2882 dp->d_reclen = reclen;
2883 dp->d_type = DT_UNKNOWN;
2884 blksiz += dp->d_reclen;
2885 if (blksiz == NFS_DIRFRAGSIZ)
2886 blksiz = 0;
2887 uiop->uio_resid -= DIRHDSIZ;
2888 uiop->uio_iov->iov_base =
2889 (caddr_t)uiop->uio_iov->iov_base +
2890 DIRHDSIZ;
2891 uiop->uio_iov->iov_len -= DIRHDSIZ;
2892 cnp->cn_nameptr = uiop->uio_iov->iov_base;
2893 cnp->cn_namelen = len;
2894 nfsm_mtouio(uiop, len);
2895 cp = uiop->uio_iov->iov_base;
2896 tlen -= len;
2897 *cp = '\0';
2898 uiop->uio_iov->iov_base =
2899 (caddr_t)uiop->uio_iov->iov_base + tlen;
2900 uiop->uio_iov->iov_len -= tlen;
2901 uiop->uio_resid -= tlen;
2902 } else
2903 nfsm_adv(nfsm_rndup(len));
2904 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2905 if (bigenough) {
2906 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE)
2907 uiop->uio_offset =
2908 fxdr_swapcookie3(tl);
2909 else
2910 uiop->uio_offset =
2911 fxdr_cookie3(tl);
2912 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2913 }
2914 tl += 2;
2915
2916 /*
2917 * Since the attributes are before the file handle
2918 * (sigh), we must skip over the attributes and then
2919 * come back and get them.
2920 */
2921 attrflag = fxdr_unsigned(int, *tl);
2922 if (attrflag) {
2923 nfsm_dissect(fp, struct nfs_fattr *, NFSX_V3FATTR);
2924 memcpy(&fattr, fp, NFSX_V3FATTR);
2925 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2926 doit = fxdr_unsigned(int, *tl);
2927 if (doit) {
2928 nfsm_getfh(fhp, fhsize, 1);
2929 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2930 VREF(vp);
2931 newvp = vp;
2932 np = dnp;
2933 } else {
2934 error = nfs_nget1(vp->v_mount, fhp,
2935 fhsize, &np, LK_NOWAIT);
2936 if (!error)
2937 newvp = NFSTOV(np);
2938 }
2939 if (!error) {
2940 const char *cp;
2941
2942 nfs_loadattrcache(&newvp, &fattr, 0, 0);
2943 dp->d_type =
2944 IFTODT(VTTOIF(np->n_vattr->va_type));
2945 ndp->ni_vp = newvp;
2946 cp = cnp->cn_nameptr + cnp->cn_namelen;
2947 cnp->cn_hash =
2948 namei_hash(cnp->cn_nameptr, &cp);
2949 if (cnp->cn_namelen <= NCHNAMLEN) {
2950 cache_purge1(ndp->ni_dvp, cnp, 0);
2951 nfs_cache_enter(ndp->ni_dvp, ndp->ni_vp,
2952 cnp);
2953 }
2954 }
2955 }
2956 } else {
2957 /* Just skip over the file handle */
2958 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2959 i = fxdr_unsigned(int, *tl);
2960 nfsm_adv(nfsm_rndup(i));
2961 }
2962 if (newvp != NULLVP) {
2963 if (newvp == vp)
2964 vrele(newvp);
2965 else
2966 vput(newvp);
2967 newvp = NULLVP;
2968 }
2969 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2970 more_dirs = fxdr_unsigned(int, *tl);
2971 }
2972 /*
2973 * If at end of rpc data, get the eof boolean
2974 */
2975 if (!more_dirs) {
2976 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2977 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2978 }
2979 m_freem(mrep);
2980 }
2981 /*
2982 * Fill last record, iff any, out to a multiple of NFS_DIRFRAGSIZ
2983 * by increasing d_reclen for the last record.
2984 */
2985 if (blksiz > 0) {
2986 left = NFS_DIRFRAGSIZ - blksiz;
2987 dp->d_reclen += left;
2988 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2989 uiop->uio_iov->iov_base = (caddr_t)uiop->uio_iov->iov_base +
2990 left;
2991 uiop->uio_iov->iov_len -= left;
2992 uiop->uio_resid -= left;
2993 }
2994
2995 /*
2996 * We are now either at the end of the directory or have filled the
2997 * block.
2998 */
2999 if (bigenough)
3000 dnp->n_direofoffset = uiop->uio_offset;
3001 nfsmout:
3002 if (newvp != NULLVP) {
3003 if(newvp == vp)
3004 vrele(newvp);
3005 else
3006 vput(newvp);
3007 }
3008 return (error);
3009 }
3010 #endif
3011
3012 static char hextoasc[] = "0123456789abcdef";
3013
3014 /*
3015 * Silly rename. To make the NFS filesystem that is stateless look a little
3016 * more like the "ufs" a remove of an active vnode is translated to a rename
3017 * to a funny looking filename that is removed by nfs_inactive on the
3018 * nfsnode. There is the potential for another process on a different client
3019 * to create the same funny name between the nfs_lookitup() fails and the
3020 * nfs_rename() completes, but...
3021 */
3022 int
3023 nfs_sillyrename(dvp, vp, cnp)
3024 struct vnode *dvp, *vp;
3025 struct componentname *cnp;
3026 {
3027 struct sillyrename *sp;
3028 struct nfsnode *np;
3029 int error;
3030 short pid;
3031
3032 cache_purge(dvp);
3033 np = VTONFS(vp);
3034 #ifndef DIAGNOSTIC
3035 if (vp->v_type == VDIR)
3036 panic("nfs: sillyrename dir");
3037 #endif
3038 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
3039 M_NFSREQ, M_WAITOK);
3040 sp->s_cred = crdup(cnp->cn_cred);
3041 sp->s_dvp = dvp;
3042 VREF(dvp);
3043
3044 /* Fudge together a funny name */
3045 pid = cnp->cn_proc->p_pid;
3046 memcpy(sp->s_name, ".nfsAxxxx4.4", 13);
3047 sp->s_namlen = 12;
3048 sp->s_name[8] = hextoasc[pid & 0xf];
3049 sp->s_name[7] = hextoasc[(pid >> 4) & 0xf];
3050 sp->s_name[6] = hextoasc[(pid >> 8) & 0xf];
3051 sp->s_name[5] = hextoasc[(pid >> 12) & 0xf];
3052
3053 /* Try lookitups until we get one that isn't there */
3054 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
3055 cnp->cn_proc, (struct nfsnode **)0) == 0) {
3056 sp->s_name[4]++;
3057 if (sp->s_name[4] > 'z') {
3058 error = EINVAL;
3059 goto bad;
3060 }
3061 }
3062 error = nfs_renameit(dvp, cnp, sp);
3063 if (error)
3064 goto bad;
3065 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
3066 cnp->cn_proc, &np);
3067 np->n_sillyrename = sp;
3068 return (0);
3069 bad:
3070 vrele(sp->s_dvp);
3071 crfree(sp->s_cred);
3072 free((caddr_t)sp, M_NFSREQ);
3073 return (error);
3074 }
3075
3076 /*
3077 * Look up a file name and optionally either update the file handle or
3078 * allocate an nfsnode, depending on the value of npp.
3079 * npp == NULL --> just do the lookup
3080 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
3081 * handled too
3082 * *npp != NULL --> update the file handle in the vnode
3083 */
3084 int
3085 nfs_lookitup(dvp, name, len, cred, procp, npp)
3086 struct vnode *dvp;
3087 const char *name;
3088 int len;
3089 struct ucred *cred;
3090 struct proc *procp;
3091 struct nfsnode **npp;
3092 {
3093 u_int32_t *tl;
3094 caddr_t cp;
3095 int32_t t1, t2;
3096 struct vnode *newvp = (struct vnode *)0;
3097 struct nfsnode *np, *dnp = VTONFS(dvp);
3098 caddr_t bpos, dpos, cp2;
3099 int error = 0, fhlen;
3100 #ifndef NFS_V2_ONLY
3101 int attrflag;
3102 #endif
3103 struct mbuf *mreq, *mrep, *md, *mb;
3104 nfsfh_t *nfhp;
3105 const int v3 = NFS_ISV3(dvp);
3106
3107 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
3108 nfsm_reqhead(dnp, NFSPROC_LOOKUP,
3109 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
3110 nfsm_fhtom(dnp, v3);
3111 nfsm_strtom(name, len, NFS_MAXNAMLEN);
3112 nfsm_request(dnp, NFSPROC_LOOKUP, procp, cred);
3113 if (npp && !error) {
3114 nfsm_getfh(nfhp, fhlen, v3);
3115 if (*npp) {
3116 np = *npp;
3117 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
3118 free((caddr_t)np->n_fhp, M_NFSBIGFH);
3119 np->n_fhp = &np->n_fh;
3120 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
3121 np->n_fhp =(nfsfh_t *)malloc(fhlen,M_NFSBIGFH,M_WAITOK);
3122 memcpy((caddr_t)np->n_fhp, (caddr_t)nfhp, fhlen);
3123 np->n_fhsize = fhlen;
3124 newvp = NFSTOV(np);
3125 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
3126 VREF(dvp);
3127 newvp = dvp;
3128 np = dnp;
3129 } else {
3130 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
3131 if (error) {
3132 m_freem(mrep);
3133 return (error);
3134 }
3135 newvp = NFSTOV(np);
3136 }
3137 #ifndef NFS_V2_ONLY
3138 if (v3) {
3139 nfsm_postop_attr(newvp, attrflag, 0);
3140 if (!attrflag && *npp == NULL) {
3141 m_freem(mrep);
3142 vput(newvp);
3143 return (ENOENT);
3144 }
3145 } else
3146 #endif
3147 nfsm_loadattr(newvp, (struct vattr *)0, 0);
3148 }
3149 nfsm_reqdone;
3150 if (npp && *npp == NULL) {
3151 if (error) {
3152 if (newvp)
3153 vput(newvp);
3154 } else
3155 *npp = np;
3156 }
3157 return (error);
3158 }
3159
3160 #ifndef NFS_V2_ONLY
3161 /*
3162 * Nfs Version 3 commit rpc
3163 */
3164 int
3165 nfs_commit(vp, offset, cnt, procp)
3166 struct vnode *vp;
3167 off_t offset;
3168 uint32_t cnt;
3169 struct proc *procp;
3170 {
3171 caddr_t cp;
3172 u_int32_t *tl;
3173 int32_t t1, t2;
3174 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3175 caddr_t bpos, dpos, cp2;
3176 int error = 0, wccflag = NFSV3_WCCRATTR;
3177 struct mbuf *mreq, *mrep, *md, *mb;
3178 struct nfsnode *np;
3179
3180 KASSERT(NFS_ISV3(vp));
3181
3182 #ifdef NFS_DEBUG_COMMIT
3183 printf("commit %lu - %lu\n", (unsigned long)offset,
3184 (unsigned long)(offset + cnt));
3185 #endif
3186
3187 simple_lock(&nmp->nm_slock);
3188 if ((nmp->nm_iflag & NFSMNT_HASWRITEVERF) == 0) {
3189 simple_unlock(&nmp->nm_slock);
3190 return (0);
3191 }
3192 simple_unlock(&nmp->nm_slock);
3193 nfsstats.rpccnt[NFSPROC_COMMIT]++;
3194 np = VTONFS(vp);
3195 nfsm_reqhead(np, NFSPROC_COMMIT, NFSX_FH(1));
3196 nfsm_fhtom(np, 1);
3197 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3198 txdr_hyper(offset, tl);
3199 tl += 2;
3200 *tl = txdr_unsigned(cnt);
3201 nfsm_request(np, NFSPROC_COMMIT, procp, np->n_wcred);
3202 nfsm_wcc_data(vp, wccflag, 0);
3203 if (!error) {
3204 nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
3205 simple_lock(&nmp->nm_slock);
3206 if ((nmp->nm_iflag & NFSMNT_STALEWRITEVERF) ||
3207 memcmp(nmp->nm_writeverf, tl, NFSX_V3WRITEVERF)) {
3208 memcpy(nmp->nm_writeverf, tl, NFSX_V3WRITEVERF);
3209 error = NFSERR_STALEWRITEVERF;
3210 nmp->nm_iflag |= NFSMNT_STALEWRITEVERF;
3211 }
3212 simple_unlock(&nmp->nm_slock);
3213 }
3214 nfsm_reqdone;
3215 return (error);
3216 }
3217 #endif
3218
3219 /*
3220 * Kludge City..
3221 * - make nfs_bmap() essentially a no-op that does no translation
3222 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
3223 * (Maybe I could use the process's page mapping, but I was concerned that
3224 * Kernel Write might not be enabled and also figured copyout() would do
3225 * a lot more work than memcpy() and also it currently happens in the
3226 * context of the swapper process (2).
3227 */
3228 int
3229 nfs_bmap(v)
3230 void *v;
3231 {
3232 struct vop_bmap_args /* {
3233 struct vnode *a_vp;
3234 daddr_t a_bn;
3235 struct vnode **a_vpp;
3236 daddr_t *a_bnp;
3237 int *a_runp;
3238 } */ *ap = v;
3239 struct vnode *vp = ap->a_vp;
3240 int bshift = vp->v_mount->mnt_fs_bshift - vp->v_mount->mnt_dev_bshift;
3241
3242 if (ap->a_vpp != NULL)
3243 *ap->a_vpp = vp;
3244 if (ap->a_bnp != NULL)
3245 *ap->a_bnp = ap->a_bn << bshift;
3246 if (ap->a_runp != NULL)
3247 *ap->a_runp = 1024 * 1024; /* XXX */
3248 return (0);
3249 }
3250
3251 /*
3252 * Strategy routine.
3253 * For async requests when nfsiod(s) are running, queue the request by
3254 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
3255 * request.
3256 */
3257 int
3258 nfs_strategy(v)
3259 void *v;
3260 {
3261 struct vop_strategy_args *ap = v;
3262 struct buf *bp = ap->a_bp;
3263 struct proc *p;
3264 int error = 0;
3265
3266 if ((bp->b_flags & (B_PHYS|B_ASYNC)) == (B_PHYS|B_ASYNC))
3267 panic("nfs physio/async");
3268 if (bp->b_flags & B_ASYNC)
3269 p = NULL;
3270 else
3271 p = curproc; /* XXX */
3272
3273 /*
3274 * If the op is asynchronous and an i/o daemon is waiting
3275 * queue the request, wake it up and wait for completion
3276 * otherwise just do it ourselves.
3277 */
3278
3279 if ((bp->b_flags & B_ASYNC) == 0 ||
3280 nfs_asyncio(bp))
3281 error = nfs_doio(bp, p);
3282 return (error);
3283 }
3284
3285 /*
3286 * fsync vnode op. Just call nfs_flush() with commit == 1.
3287 */
3288 /* ARGSUSED */
3289 int
3290 nfs_fsync(v)
3291 void *v;
3292 {
3293 struct vop_fsync_args /* {
3294 struct vnodeop_desc *a_desc;
3295 struct vnode * a_vp;
3296 struct ucred * a_cred;
3297 int a_flags;
3298 off_t offlo;
3299 off_t offhi;
3300 struct proc * a_p;
3301 } */ *ap = v;
3302
3303 struct vnode *vp = ap->a_vp;
3304
3305 if (vp->v_type != VREG)
3306 return 0;
3307
3308 return (nfs_flush(vp, ap->a_cred,
3309 (ap->a_flags & FSYNC_WAIT) != 0 ? MNT_WAIT : 0, ap->a_p, 1));
3310 }
3311
3312 /*
3313 * Flush all the data associated with a vnode.
3314 */
3315 int
3316 nfs_flush(vp, cred, waitfor, p, commit)
3317 struct vnode *vp;
3318 struct ucred *cred;
3319 int waitfor;
3320 struct proc *p;
3321 int commit;
3322 {
3323 struct nfsnode *np = VTONFS(vp);
3324 int error;
3325 int flushflags = PGO_ALLPAGES|PGO_CLEANIT|PGO_SYNCIO;
3326 UVMHIST_FUNC("nfs_flush"); UVMHIST_CALLED(ubchist);
3327
3328 simple_lock(&vp->v_interlock);
3329 error = VOP_PUTPAGES(vp, 0, 0, flushflags);
3330 if (np->n_flag & NWRITEERR) {
3331 error = np->n_error;
3332 np->n_flag &= ~NWRITEERR;
3333 }
3334 UVMHIST_LOG(ubchist, "returning %d", error,0,0,0);
3335 return (error);
3336 }
3337
3338 /*
3339 * Return POSIX pathconf information applicable to nfs.
3340 *
3341 * N.B. The NFS V2 protocol doesn't support this RPC.
3342 */
3343 /* ARGSUSED */
3344 int
3345 nfs_pathconf(v)
3346 void *v;
3347 {
3348 struct vop_pathconf_args /* {
3349 struct vnode *a_vp;
3350 int a_name;
3351 register_t *a_retval;
3352 } */ *ap = v;
3353 struct nfsv3_pathconf *pcp;
3354 struct vnode *vp = ap->a_vp;
3355 struct mbuf *mreq, *mrep, *md, *mb;
3356 int32_t t1, t2;
3357 u_int32_t *tl;
3358 caddr_t bpos, dpos, cp, cp2;
3359 int error = 0, attrflag;
3360 #ifndef NFS_V2_ONLY
3361 struct nfsmount *nmp;
3362 unsigned int l;
3363 u_int64_t maxsize;
3364 #endif
3365 const int v3 = NFS_ISV3(vp);
3366 struct nfsnode *np = VTONFS(vp);
3367
3368 switch (ap->a_name) {
3369 /* Names that can be resolved locally. */
3370 case _PC_PIPE_BUF:
3371 *ap->a_retval = PIPE_BUF;
3372 break;
3373 case _PC_SYNC_IO:
3374 *ap->a_retval = 1;
3375 break;
3376 /* Names that cannot be resolved locally; do an RPC, if possible. */
3377 case _PC_LINK_MAX:
3378 case _PC_NAME_MAX:
3379 case _PC_CHOWN_RESTRICTED:
3380 case _PC_NO_TRUNC:
3381 if (!v3) {
3382 error = EINVAL;
3383 break;
3384 }
3385 nfsstats.rpccnt[NFSPROC_PATHCONF]++;
3386 nfsm_reqhead(np, NFSPROC_PATHCONF, NFSX_FH(1));
3387 nfsm_fhtom(np, 1);
3388 nfsm_request(np, NFSPROC_PATHCONF,
3389 curproc, curproc->p_ucred); /* XXX */
3390 nfsm_postop_attr(vp, attrflag, 0);
3391 if (!error) {
3392 nfsm_dissect(pcp, struct nfsv3_pathconf *,
3393 NFSX_V3PATHCONF);
3394 switch (ap->a_name) {
3395 case _PC_LINK_MAX:
3396 *ap->a_retval =
3397 fxdr_unsigned(register_t, pcp->pc_linkmax);
3398 break;
3399 case _PC_NAME_MAX:
3400 *ap->a_retval =
3401 fxdr_unsigned(register_t, pcp->pc_namemax);
3402 break;
3403 case _PC_CHOWN_RESTRICTED:
3404 *ap->a_retval =
3405 (pcp->pc_chownrestricted == nfs_true);
3406 break;
3407 case _PC_NO_TRUNC:
3408 *ap->a_retval =
3409 (pcp->pc_notrunc == nfs_true);
3410 break;
3411 }
3412 }
3413 nfsm_reqdone;
3414 break;
3415 case _PC_FILESIZEBITS:
3416 #ifndef NFS_V2_ONLY
3417 if (v3) {
3418 nmp = VFSTONFS(vp->v_mount);
3419 if ((nmp->nm_iflag & NFSMNT_GOTFSINFO) == 0)
3420 if ((error = nfs_fsinfo(nmp, vp,
3421 curproc->p_ucred, curproc)) != 0) /* XXX */
3422 break;
3423 for (l = 0, maxsize = nmp->nm_maxfilesize;
3424 (maxsize >> l) > 0; l++)
3425 ;
3426 *ap->a_retval = l + 1;
3427 } else
3428 #endif
3429 {
3430 *ap->a_retval = 32; /* NFS V2 limitation */
3431 }
3432 break;
3433 default:
3434 error = EINVAL;
3435 break;
3436 }
3437
3438 return (error);
3439 }
3440
3441 /*
3442 * NFS advisory byte-level locks.
3443 */
3444 int
3445 nfs_advlock(v)
3446 void *v;
3447 {
3448 struct vop_advlock_args /* {
3449 struct vnode *a_vp;
3450 caddr_t a_id;
3451 int a_op;
3452 struct flock *a_fl;
3453 int a_flags;
3454 } */ *ap = v;
3455 struct nfsnode *np = VTONFS(ap->a_vp);
3456
3457 return lf_advlock(ap, &np->n_lockf, np->n_size);
3458 }
3459
3460 /*
3461 * Print out the contents of an nfsnode.
3462 */
3463 int
3464 nfs_print(v)
3465 void *v;
3466 {
3467 struct vop_print_args /* {
3468 struct vnode *a_vp;
3469 } */ *ap = v;
3470 struct vnode *vp = ap->a_vp;
3471 struct nfsnode *np = VTONFS(vp);
3472
3473 printf("tag VT_NFS, fileid %ld fsid 0x%lx",
3474 np->n_vattr->va_fileid, np->n_vattr->va_fsid);
3475 if (vp->v_type == VFIFO)
3476 fifo_printinfo(vp);
3477 printf("\n");
3478 return (0);
3479 }
3480
3481 /*
3482 * NFS file truncation.
3483 */
3484 int
3485 nfs_truncate(v)
3486 void *v;
3487 {
3488 #if 0
3489 struct vop_truncate_args /* {
3490 struct vnode *a_vp;
3491 off_t a_length;
3492 int a_flags;
3493 struct ucred *a_cred;
3494 struct proc *a_p;
3495 } */ *ap = v;
3496 #endif
3497
3498 /* Use nfs_setattr */
3499 return (EOPNOTSUPP);
3500 }
3501
3502 /*
3503 * Just call bwrite().
3504 */
3505 int
3506 nfs_bwrite(v)
3507 void *v;
3508 {
3509 struct vop_bwrite_args /* {
3510 struct vnode *a_bp;
3511 } */ *ap = v;
3512
3513 return (bwrite(ap->a_bp));
3514 }
3515
3516 /*
3517 * nfs unlock wrapper.
3518 */
3519 int
3520 nfs_unlock(void *v)
3521 {
3522 struct vop_unlock_args /* {
3523 struct vnode *a_vp;
3524 int a_flags;
3525 } */ *ap = v;
3526 struct vnode *vp = ap->a_vp;
3527
3528 /*
3529 * VOP_UNLOCK can be called by nfs_loadattrcache
3530 * with v_data == 0.
3531 */
3532 if (VTONFS(vp)) {
3533 nfs_delayedtruncate(vp);
3534 }
3535
3536 return genfs_unlock(v);
3537 }
3538
3539 /*
3540 * nfs special file access vnode op.
3541 * Essentially just get vattr and then imitate iaccess() since the device is
3542 * local to the client.
3543 */
3544 int
3545 nfsspec_access(v)
3546 void *v;
3547 {
3548 struct vop_access_args /* {
3549 struct vnode *a_vp;
3550 int a_mode;
3551 struct ucred *a_cred;
3552 struct proc *a_p;
3553 } */ *ap = v;
3554 struct vattr va;
3555 struct vnode *vp = ap->a_vp;
3556 int error;
3557
3558 error = VOP_GETATTR(vp, &va, ap->a_cred, ap->a_p);
3559 if (error)
3560 return (error);
3561
3562 /*
3563 * Disallow write attempts on filesystems mounted read-only;
3564 * unless the file is a socket, fifo, or a block or character
3565 * device resident on the filesystem.
3566 */
3567 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3568 switch (vp->v_type) {
3569 case VREG:
3570 case VDIR:
3571 case VLNK:
3572 return (EROFS);
3573 default:
3574 break;
3575 }
3576 }
3577
3578 return (vaccess(va.va_type, va.va_mode,
3579 va.va_uid, va.va_gid, ap->a_mode, ap->a_cred));
3580 }
3581
3582 /*
3583 * Read wrapper for special devices.
3584 */
3585 int
3586 nfsspec_read(v)
3587 void *v;
3588 {
3589 struct vop_read_args /* {
3590 struct vnode *a_vp;
3591 struct uio *a_uio;
3592 int a_ioflag;
3593 struct ucred *a_cred;
3594 } */ *ap = v;
3595 struct nfsnode *np = VTONFS(ap->a_vp);
3596
3597 /*
3598 * Set access flag.
3599 */
3600 np->n_flag |= NACC;
3601 np->n_atim.tv_sec = time.tv_sec;
3602 np->n_atim.tv_nsec = time.tv_usec * 1000;
3603 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_read), ap));
3604 }
3605
3606 /*
3607 * Write wrapper for special devices.
3608 */
3609 int
3610 nfsspec_write(v)
3611 void *v;
3612 {
3613 struct vop_write_args /* {
3614 struct vnode *a_vp;
3615 struct uio *a_uio;
3616 int a_ioflag;
3617 struct ucred *a_cred;
3618 } */ *ap = v;
3619 struct nfsnode *np = VTONFS(ap->a_vp);
3620
3621 /*
3622 * Set update flag.
3623 */
3624 np->n_flag |= NUPD;
3625 np->n_mtim.tv_sec = time.tv_sec;
3626 np->n_mtim.tv_nsec = time.tv_usec * 1000;
3627 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_write), ap));
3628 }
3629
3630 /*
3631 * Close wrapper for special devices.
3632 *
3633 * Update the times on the nfsnode then do device close.
3634 */
3635 int
3636 nfsspec_close(v)
3637 void *v;
3638 {
3639 struct vop_close_args /* {
3640 struct vnode *a_vp;
3641 int a_fflag;
3642 struct ucred *a_cred;
3643 struct proc *a_p;
3644 } */ *ap = v;
3645 struct vnode *vp = ap->a_vp;
3646 struct nfsnode *np = VTONFS(vp);
3647 struct vattr vattr;
3648
3649 if (np->n_flag & (NACC | NUPD)) {
3650 np->n_flag |= NCHG;
3651 if (vp->v_usecount == 1 &&
3652 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3653 VATTR_NULL(&vattr);
3654 if (np->n_flag & NACC)
3655 vattr.va_atime = np->n_atim;
3656 if (np->n_flag & NUPD)
3657 vattr.va_mtime = np->n_mtim;
3658 (void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_p);
3659 }
3660 }
3661 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_close), ap));
3662 }
3663
3664 /*
3665 * Read wrapper for fifos.
3666 */
3667 int
3668 nfsfifo_read(v)
3669 void *v;
3670 {
3671 struct vop_read_args /* {
3672 struct vnode *a_vp;
3673 struct uio *a_uio;
3674 int a_ioflag;
3675 struct ucred *a_cred;
3676 } */ *ap = v;
3677 struct nfsnode *np = VTONFS(ap->a_vp);
3678
3679 /*
3680 * Set access flag.
3681 */
3682 np->n_flag |= NACC;
3683 np->n_atim.tv_sec = time.tv_sec;
3684 np->n_atim.tv_nsec = time.tv_usec * 1000;
3685 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), ap));
3686 }
3687
3688 /*
3689 * Write wrapper for fifos.
3690 */
3691 int
3692 nfsfifo_write(v)
3693 void *v;
3694 {
3695 struct vop_write_args /* {
3696 struct vnode *a_vp;
3697 struct uio *a_uio;
3698 int a_ioflag;
3699 struct ucred *a_cred;
3700 } */ *ap = v;
3701 struct nfsnode *np = VTONFS(ap->a_vp);
3702
3703 /*
3704 * Set update flag.
3705 */
3706 np->n_flag |= NUPD;
3707 np->n_mtim.tv_sec = time.tv_sec;
3708 np->n_mtim.tv_nsec = time.tv_usec * 1000;
3709 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), ap));
3710 }
3711
3712 /*
3713 * Close wrapper for fifos.
3714 *
3715 * Update the times on the nfsnode then do fifo close.
3716 */
3717 int
3718 nfsfifo_close(v)
3719 void *v;
3720 {
3721 struct vop_close_args /* {
3722 struct vnode *a_vp;
3723 int a_fflag;
3724 struct ucred *a_cred;
3725 struct proc *a_p;
3726 } */ *ap = v;
3727 struct vnode *vp = ap->a_vp;
3728 struct nfsnode *np = VTONFS(vp);
3729 struct vattr vattr;
3730
3731 if (np->n_flag & (NACC | NUPD)) {
3732 if (np->n_flag & NACC) {
3733 np->n_atim.tv_sec = time.tv_sec;
3734 np->n_atim.tv_nsec = time.tv_usec * 1000;
3735 }
3736 if (np->n_flag & NUPD) {
3737 np->n_mtim.tv_sec = time.tv_sec;
3738 np->n_mtim.tv_nsec = time.tv_usec * 1000;
3739 }
3740 np->n_flag |= NCHG;
3741 if (vp->v_usecount == 1 &&
3742 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3743 VATTR_NULL(&vattr);
3744 if (np->n_flag & NACC)
3745 vattr.va_atime = np->n_atim;
3746 if (np->n_flag & NUPD)
3747 vattr.va_mtime = np->n_mtim;
3748 (void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_p);
3749 }
3750 }
3751 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_close), ap));
3752 }
3753