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