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