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