1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
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 * from nfs_vnops.c 8.16 (Berkeley) 5/27/95
35 */
36
37 #include <sys/cdefs.h>
38 /*
39 * vnode op calls for Sun NFS version 2, 3 and 4
40 */
41
42 #include "opt_inet.h"
43
44 #include <sys/param.h>
45 #include <sys/kernel.h>
46 #include <sys/systm.h>
47 #include <sys/resourcevar.h>
48 #include <sys/proc.h>
49 #include <sys/mount.h>
50 #include <sys/bio.h>
51 #include <sys/buf.h>
52 #include <sys/extattr.h>
53 #include <sys/filio.h>
54 #include <sys/jail.h>
55 #include <sys/malloc.h>
56 #include <sys/mbuf.h>
57 #include <sys/namei.h>
58 #include <sys/socket.h>
59 #include <sys/vnode.h>
60 #include <sys/dirent.h>
61 #include <sys/fcntl.h>
62 #include <sys/lockf.h>
63 #include <sys/stat.h>
64 #include <sys/sysctl.h>
65 #include <sys/signalvar.h>
66
67 #include <vm/vm.h>
68 #include <vm/vm_extern.h>
69 #include <vm/vm_object.h>
70 #include <vm/vnode_pager.h>
71
72 #include <fs/nfs/nfsport.h>
73 #include <fs/nfsclient/nfsnode.h>
74 #include <fs/nfsclient/nfsmount.h>
75 #include <fs/nfsclient/nfs.h>
76 #include <fs/nfsclient/nfs_kdtrace.h>
77
78 #include <net/if.h>
79 #include <netinet/in.h>
80 #include <netinet/in_var.h>
81
82 #include <nfs/nfs_lock.h>
83
84 #ifdef KDTRACE_HOOKS
85 #include <sys/dtrace_bsd.h>
86
87 dtrace_nfsclient_accesscache_flush_probe_func_t
88 dtrace_nfscl_accesscache_flush_done_probe;
89 uint32_t nfscl_accesscache_flush_done_id;
90
91 dtrace_nfsclient_accesscache_get_probe_func_t
92 dtrace_nfscl_accesscache_get_hit_probe,
93 dtrace_nfscl_accesscache_get_miss_probe;
94 uint32_t nfscl_accesscache_get_hit_id;
95 uint32_t nfscl_accesscache_get_miss_id;
96
97 dtrace_nfsclient_accesscache_load_probe_func_t
98 dtrace_nfscl_accesscache_load_done_probe;
99 uint32_t nfscl_accesscache_load_done_id;
100 #endif /* !KDTRACE_HOOKS */
101
102 /* Defs */
103 #define TRUE 1
104 #define FALSE 0
105
106 extern struct nfsstatsv1 nfsstatsv1;
107 extern int nfsrv_useacl;
108 extern int nfscl_debuglevel;
109 NFSCLSTATEMUTEX;
110 MALLOC_DECLARE(M_NEWNFSREQ);
111
112 static vop_read_t nfsfifo_read;
113 static vop_write_t nfsfifo_write;
114 static vop_close_t nfsfifo_close;
115 static int nfs_setattrrpc(struct vnode *, struct vattr *, struct ucred *,
116 struct thread *);
117 static int nfs_get_namedattrdir(struct vnode *, struct componentname *,
118 struct vnode **);
119 static vop_lookup_t nfs_lookup;
120 static vop_create_t nfs_create;
121 static vop_mknod_t nfs_mknod;
122 static vop_open_t nfs_open;
123 static vop_pathconf_t nfs_pathconf;
124 static vop_close_t nfs_close;
125 static vop_access_t nfs_access;
126 static vop_getattr_t nfs_getattr;
127 static vop_setattr_t nfs_setattr;
128 static vop_read_t nfs_read;
129 static vop_fsync_t nfs_fsync;
130 static vop_remove_t nfs_remove;
131 static vop_link_t nfs_link;
132 static vop_rename_t nfs_rename;
133 static vop_mkdir_t nfs_mkdir;
134 static vop_rmdir_t nfs_rmdir;
135 static vop_symlink_t nfs_symlink;
136 static vop_readdir_t nfs_readdir;
137 static vop_strategy_t nfs_strategy;
138 static int nfs_lookitup(struct vnode *, char *, int,
139 struct ucred *, struct thread *, struct nfsnode **);
140 static int nfs_sillyrename(struct vnode *, struct vnode *,
141 struct componentname *);
142 static vop_access_t nfsspec_access;
143 static vop_readlink_t nfs_readlink;
144 static vop_print_t nfs_print;
145 static vop_advlock_t nfs_advlock;
146 static vop_advlockasync_t nfs_advlockasync;
147 static vop_getacl_t nfs_getacl;
148 static vop_setacl_t nfs_setacl;
149 static vop_advise_t nfs_advise;
150 static vop_allocate_t nfs_allocate;
151 static vop_deallocate_t nfs_deallocate;
152 static vop_copy_file_range_t nfs_copy_file_range;
153 static vop_ioctl_t nfs_ioctl;
154 static vop_getextattr_t nfs_getextattr;
155 static vop_setextattr_t nfs_setextattr;
156 static vop_listextattr_t nfs_listextattr;
157 static vop_deleteextattr_t nfs_deleteextattr;
158 static vop_lock1_t nfs_lock;
159
160 /*
161 * Global vfs data structures for nfs
162 */
163
164 static struct vop_vector newnfs_vnodeops_nosig = {
165 .vop_default = &default_vnodeops,
166 .vop_access = nfs_access,
167 .vop_advlock = nfs_advlock,
168 .vop_advlockasync = nfs_advlockasync,
169 .vop_close = nfs_close,
170 .vop_create = nfs_create,
171 .vop_fsync = nfs_fsync,
172 .vop_getattr = nfs_getattr,
173 .vop_getpages = ncl_getpages,
174 .vop_putpages = ncl_putpages,
175 .vop_inactive = ncl_inactive,
176 .vop_link = nfs_link,
177 .vop_lock1 = nfs_lock,
178 .vop_lookup = nfs_lookup,
179 .vop_mkdir = nfs_mkdir,
180 .vop_mknod = nfs_mknod,
181 .vop_open = nfs_open,
182 .vop_pathconf = nfs_pathconf,
183 .vop_print = nfs_print,
184 .vop_read = nfs_read,
185 .vop_readdir = nfs_readdir,
186 .vop_readlink = nfs_readlink,
187 .vop_reclaim = ncl_reclaim,
188 .vop_remove = nfs_remove,
189 .vop_rename = nfs_rename,
190 .vop_rmdir = nfs_rmdir,
191 .vop_setattr = nfs_setattr,
192 .vop_strategy = nfs_strategy,
193 .vop_symlink = nfs_symlink,
194 .vop_write = ncl_write,
195 .vop_getacl = nfs_getacl,
196 .vop_setacl = nfs_setacl,
197 .vop_advise = nfs_advise,
198 .vop_allocate = nfs_allocate,
199 .vop_deallocate = nfs_deallocate,
200 .vop_copy_file_range = nfs_copy_file_range,
201 .vop_ioctl = nfs_ioctl,
202 .vop_getextattr = nfs_getextattr,
203 .vop_setextattr = nfs_setextattr,
204 .vop_listextattr = nfs_listextattr,
205 .vop_deleteextattr = nfs_deleteextattr,
206 };
207 VFS_VOP_VECTOR_REGISTER(newnfs_vnodeops_nosig);
208
209 static int
nfs_vnodeops_bypass(struct vop_generic_args * a)210 nfs_vnodeops_bypass(struct vop_generic_args *a)
211 {
212
213 return (vop_sigdefer(&newnfs_vnodeops_nosig, a));
214 }
215
216 struct vop_vector newnfs_vnodeops = {
217 .vop_default = &default_vnodeops,
218 .vop_bypass = nfs_vnodeops_bypass,
219 };
220 VFS_VOP_VECTOR_REGISTER(newnfs_vnodeops);
221
222 static struct vop_vector newnfs_fifoops_nosig = {
223 .vop_default = &fifo_specops,
224 .vop_access = nfsspec_access,
225 .vop_close = nfsfifo_close,
226 .vop_fsync = nfs_fsync,
227 .vop_getattr = nfs_getattr,
228 .vop_inactive = ncl_inactive,
229 .vop_pathconf = nfs_pathconf,
230 .vop_print = nfs_print,
231 .vop_read = nfsfifo_read,
232 .vop_reclaim = ncl_reclaim,
233 .vop_setattr = nfs_setattr,
234 .vop_write = nfsfifo_write,
235 };
236 VFS_VOP_VECTOR_REGISTER(newnfs_fifoops_nosig);
237
238 static int
nfs_fifoops_bypass(struct vop_generic_args * a)239 nfs_fifoops_bypass(struct vop_generic_args *a)
240 {
241
242 return (vop_sigdefer(&newnfs_fifoops_nosig, a));
243 }
244
245 struct vop_vector newnfs_fifoops = {
246 .vop_default = &default_vnodeops,
247 .vop_bypass = nfs_fifoops_bypass,
248 };
249 VFS_VOP_VECTOR_REGISTER(newnfs_fifoops);
250
251 static int nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp,
252 struct componentname *cnp, struct vattr *vap);
253 static int nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
254 int namelen, struct ucred *cred, struct thread *td, bool silly);
255 static void nfs_removestatus(struct vnode *vp, nfsremove_status file_status,
256 bool silly, struct thread *td);
257 static int nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp,
258 char *fnameptr, int fnamelen, struct vnode *tdvp, struct vnode *tvp,
259 char *tnameptr, int tnamelen, bool silly, struct ucred *cred,
260 struct thread *td);
261 static int nfs_renameit(struct vnode *sdvp, struct vnode *svp,
262 struct componentname *scnp, struct sillyrename *sp);
263
264 /*
265 * Global variables
266 */
267 SYSCTL_DECL(_vfs_nfs);
268
269 static int nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
270 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
271 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
272
273 static int nfs_prime_access_cache = 0;
274 SYSCTL_INT(_vfs_nfs, OID_AUTO, prime_access_cache, CTLFLAG_RW,
275 &nfs_prime_access_cache, 0,
276 "Prime NFS ACCESS cache when fetching attributes");
277
278 static int newnfs_commit_on_close = 0;
279 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_on_close, CTLFLAG_RW,
280 &newnfs_commit_on_close, 0, "write+commit on close, else only write");
281
282 static int nfs_clean_pages_on_close = 1;
283 SYSCTL_INT(_vfs_nfs, OID_AUTO, clean_pages_on_close, CTLFLAG_RW,
284 &nfs_clean_pages_on_close, 0, "NFS clean dirty pages on close");
285
286 int newnfs_directio_enable = 0;
287 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_enable, CTLFLAG_RW,
288 &newnfs_directio_enable, 0, "Enable NFS directio");
289
290 int nfs_keep_dirty_on_error;
291 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_keep_dirty_on_error, CTLFLAG_RW,
292 &nfs_keep_dirty_on_error, 0, "Retry pageout if error returned");
293
294 /*
295 * This sysctl allows other processes to mmap a file that has been opened
296 * O_DIRECT by a process. In general, having processes mmap the file while
297 * Direct IO is in progress can lead to Data Inconsistencies. But, we allow
298 * this by default to prevent DoS attacks - to prevent a malicious user from
299 * opening up files O_DIRECT preventing other users from mmap'ing these
300 * files. "Protected" environments where stricter consistency guarantees are
301 * required can disable this knob. The process that opened the file O_DIRECT
302 * cannot mmap() the file, because mmap'ed IO on an O_DIRECT open() is not
303 * meaningful.
304 */
305 int newnfs_directio_allow_mmap = 1;
306 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_allow_mmap, CTLFLAG_RW,
307 &newnfs_directio_allow_mmap, 0, "Enable mmaped IO on file with O_DIRECT opens");
308
309 static uint64_t nfs_maxalloclen = 64 * 1024 * 1024;
310 SYSCTL_U64(_vfs_nfs, OID_AUTO, maxalloclen, CTLFLAG_RW,
311 &nfs_maxalloclen, 0, "NFS max allocate/deallocate length");
312
313 #define NFSACCESS_ALL (NFSACCESS_READ | NFSACCESS_MODIFY \
314 | NFSACCESS_EXTEND | NFSACCESS_EXECUTE \
315 | NFSACCESS_DELETE | NFSACCESS_LOOKUP)
316
317 /*
318 * SMP Locking Note :
319 * The list of locks after the description of the lock is the ordering
320 * of other locks acquired with the lock held.
321 * np->n_mtx : Protects the fields in the nfsnode.
322 VM Object Lock
323 VI_MTX (acquired indirectly)
324 * nmp->nm_mtx : Protects the fields in the nfsmount.
325 rep->r_mtx
326 * ncl_iod_mutex : Global lock, protects shared nfsiod state.
327 * nfs_reqq_mtx : Global lock, protects the nfs_reqq list.
328 nmp->nm_mtx
329 rep->r_mtx
330 * rep->r_mtx : Protects the fields in an nfsreq.
331 */
332
333 static int
nfs_lock(struct vop_lock1_args * ap)334 nfs_lock(struct vop_lock1_args *ap)
335 {
336 struct vnode *vp;
337 struct nfsnode *np;
338 u_quad_t nsize;
339 int error, lktype;
340 bool onfault;
341
342 vp = ap->a_vp;
343 lktype = ap->a_flags & LK_TYPE_MASK;
344 error = VOP_LOCK1_APV(&default_vnodeops, ap);
345 if (error != 0 || vp->v_op != &newnfs_vnodeops)
346 return (error);
347 np = VTONFS(vp);
348 if (np == NULL)
349 return (0);
350 NFSLOCKNODE(np);
351 if ((np->n_flag & NVNSETSZSKIP) == 0 || (lktype != LK_SHARED &&
352 lktype != LK_EXCLUSIVE && lktype != LK_UPGRADE &&
353 lktype != LK_TRYUPGRADE)) {
354 NFSUNLOCKNODE(np);
355 return (0);
356 }
357 onfault = (ap->a_flags & LK_EATTR_MASK) == LK_NOWAIT &&
358 (ap->a_flags & LK_INIT_MASK) == LK_CANRECURSE &&
359 (lktype == LK_SHARED || lktype == LK_EXCLUSIVE);
360 if (onfault && vp->v_vnlock->lk_recurse == 0) {
361 /*
362 * Force retry in vm_fault(), to make the lock request
363 * sleepable, which allows us to piggy-back the
364 * sleepable call to vnode_pager_setsize().
365 */
366 NFSUNLOCKNODE(np);
367 VOP_UNLOCK(vp);
368 return (EBUSY);
369 }
370 if ((ap->a_flags & LK_NOWAIT) != 0 ||
371 (lktype == LK_SHARED && vp->v_vnlock->lk_recurse > 0)) {
372 NFSUNLOCKNODE(np);
373 return (0);
374 }
375 if (lktype == LK_SHARED) {
376 NFSUNLOCKNODE(np);
377 VOP_UNLOCK(vp);
378 ap->a_flags &= ~(LK_TYPE_MASK | LK_INTERLOCK);
379 ap->a_flags |= LK_EXCLUSIVE;
380 error = VOP_LOCK1_APV(&default_vnodeops, ap);
381 if (error != 0 || vp->v_op != &newnfs_vnodeops)
382 return (error);
383 if (vp->v_data == NULL)
384 goto downgrade;
385 MPASS(vp->v_data == np);
386 NFSLOCKNODE(np);
387 if ((np->n_flag & NVNSETSZSKIP) == 0) {
388 NFSUNLOCKNODE(np);
389 goto downgrade;
390 }
391 }
392 np->n_flag &= ~NVNSETSZSKIP;
393 nsize = np->n_size;
394 NFSUNLOCKNODE(np);
395 vnode_pager_setsize(vp, nsize);
396 downgrade:
397 if (lktype == LK_SHARED) {
398 ap->a_flags &= ~(LK_TYPE_MASK | LK_INTERLOCK);
399 ap->a_flags |= LK_DOWNGRADE;
400 (void)VOP_LOCK1_APV(&default_vnodeops, ap);
401 }
402 return (0);
403 }
404
405 static int
nfs34_access_otw(struct vnode * vp,int wmode,struct thread * td,struct ucred * cred,u_int32_t * retmode)406 nfs34_access_otw(struct vnode *vp, int wmode, struct thread *td,
407 struct ucred *cred, u_int32_t *retmode)
408 {
409 int error = 0, attrflag, i, lrupos;
410 u_int32_t rmode;
411 struct nfsnode *np = VTONFS(vp);
412 struct nfsvattr nfsva;
413
414 error = nfsrpc_accessrpc(vp, wmode, cred, td, &nfsva, &attrflag,
415 &rmode);
416 if (attrflag)
417 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
418 if (!error) {
419 lrupos = 0;
420 NFSLOCKNODE(np);
421 for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
422 if (np->n_accesscache[i].uid == cred->cr_uid) {
423 np->n_accesscache[i].mode = rmode;
424 np->n_accesscache[i].stamp = time_second;
425 break;
426 }
427 if (i > 0 && np->n_accesscache[i].stamp <
428 np->n_accesscache[lrupos].stamp)
429 lrupos = i;
430 }
431 if (i == NFS_ACCESSCACHESIZE) {
432 np->n_accesscache[lrupos].uid = cred->cr_uid;
433 np->n_accesscache[lrupos].mode = rmode;
434 np->n_accesscache[lrupos].stamp = time_second;
435 }
436 NFSUNLOCKNODE(np);
437 if (retmode != NULL)
438 *retmode = rmode;
439 KDTRACE_NFS_ACCESSCACHE_LOAD_DONE(vp, cred->cr_uid, rmode, 0);
440 } else if (NFS_ISV4(vp)) {
441 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
442 }
443 #ifdef KDTRACE_HOOKS
444 if (error != 0)
445 KDTRACE_NFS_ACCESSCACHE_LOAD_DONE(vp, cred->cr_uid, 0,
446 error);
447 #endif
448 return (error);
449 }
450
451 /*
452 * nfs access vnode op.
453 * For nfs version 2, just return ok. File accesses may fail later.
454 * For nfs version 3, use the access rpc to check accessibility. If file modes
455 * are changed on the server, accesses might still fail later.
456 */
457 static int
nfs_access(struct vop_access_args * ap)458 nfs_access(struct vop_access_args *ap)
459 {
460 struct vnode *vp = ap->a_vp;
461 int error = 0, i, gotahit;
462 u_int32_t mode, wmode, rmode;
463 int v34 = NFS_ISV34(vp);
464 struct nfsnode *np = VTONFS(vp);
465
466 /*
467 * Disallow write attempts on filesystems mounted read-only;
468 * unless the file is a socket, fifo, or a block or character
469 * device resident on the filesystem.
470 */
471 if ((ap->a_accmode & (VWRITE | VAPPEND | VWRITE_NAMED_ATTRS |
472 VDELETE_CHILD | VWRITE_ATTRIBUTES | VDELETE | VWRITE_ACL |
473 VWRITE_OWNER)) != 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) != 0) {
474 switch (vp->v_type) {
475 case VREG:
476 case VDIR:
477 case VLNK:
478 return (EROFS);
479 default:
480 break;
481 }
482 }
483
484 /*
485 * For NFSv4, check for a delegation with an Allow ACE, to see
486 * if that permits access.
487 */
488 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOCTO) != 0) {
489 error = nfscl_delegacecheck(vp, ap->a_accmode, ap->a_cred);
490 if (error == 0)
491 return (error);
492 error = 0;
493 }
494
495 /*
496 * For nfs v3 or v4, check to see if we have done this recently, and if
497 * so return our cached result instead of making an ACCESS call.
498 * If not, do an access rpc, otherwise you are stuck emulating
499 * ufs_access() locally using the vattr. This may not be correct,
500 * since the server may apply other access criteria such as
501 * client uid-->server uid mapping that we do not know about.
502 */
503 if (v34) {
504 if (ap->a_accmode & VREAD)
505 mode = NFSACCESS_READ;
506 else
507 mode = 0;
508 if (vp->v_type != VDIR) {
509 if (ap->a_accmode & VWRITE)
510 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
511 if (ap->a_accmode & VAPPEND)
512 mode |= NFSACCESS_EXTEND;
513 if (ap->a_accmode & VEXEC)
514 mode |= NFSACCESS_EXECUTE;
515 if (ap->a_accmode & VDELETE)
516 mode |= NFSACCESS_DELETE;
517 } else {
518 if (ap->a_accmode & VWRITE)
519 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
520 if (ap->a_accmode & VAPPEND)
521 mode |= NFSACCESS_EXTEND;
522 if (ap->a_accmode & VEXEC)
523 mode |= NFSACCESS_LOOKUP;
524 if (ap->a_accmode & VDELETE)
525 mode |= NFSACCESS_DELETE;
526 if (ap->a_accmode & VDELETE_CHILD)
527 mode |= NFSACCESS_MODIFY;
528 }
529 /* XXX safety belt, only make blanket request if caching */
530 if (nfsaccess_cache_timeout > 0) {
531 wmode = NFSACCESS_READ | NFSACCESS_MODIFY |
532 NFSACCESS_EXTEND | NFSACCESS_EXECUTE |
533 NFSACCESS_DELETE | NFSACCESS_LOOKUP;
534 } else {
535 wmode = mode;
536 }
537
538 /*
539 * Does our cached result allow us to give a definite yes to
540 * this request?
541 */
542 gotahit = 0;
543 NFSLOCKNODE(np);
544 for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
545 if (ap->a_cred->cr_uid == np->n_accesscache[i].uid) {
546 if (time_second < (np->n_accesscache[i].stamp
547 + nfsaccess_cache_timeout) &&
548 (np->n_accesscache[i].mode & mode) == mode) {
549 NFSINCRGLOBAL(nfsstatsv1.accesscache_hits);
550 gotahit = 1;
551 }
552 break;
553 }
554 }
555 NFSUNLOCKNODE(np);
556 #ifdef KDTRACE_HOOKS
557 if (gotahit != 0)
558 KDTRACE_NFS_ACCESSCACHE_GET_HIT(vp,
559 ap->a_cred->cr_uid, mode);
560 else
561 KDTRACE_NFS_ACCESSCACHE_GET_MISS(vp,
562 ap->a_cred->cr_uid, mode);
563 #endif
564 if (gotahit == 0) {
565 /*
566 * Either a no, or a don't know. Go to the wire.
567 */
568 NFSINCRGLOBAL(nfsstatsv1.accesscache_misses);
569 error = nfs34_access_otw(vp, wmode, ap->a_td,
570 ap->a_cred, &rmode);
571 if (!error &&
572 (rmode & mode) != mode)
573 error = EACCES;
574 }
575 return (error);
576 } else {
577 if ((error = nfsspec_access(ap)) != 0) {
578 return (error);
579 }
580 /*
581 * Attempt to prevent a mapped root from accessing a file
582 * which it shouldn't. We try to read a byte from the file
583 * if the user is root and the file is not zero length.
584 * After calling nfsspec_access, we should have the correct
585 * file size cached.
586 */
587 NFSLOCKNODE(np);
588 if (ap->a_cred->cr_uid == 0 && (ap->a_accmode & VREAD)
589 && VTONFS(vp)->n_size > 0) {
590 struct iovec aiov;
591 struct uio auio;
592 char buf[1];
593
594 NFSUNLOCKNODE(np);
595 aiov.iov_base = buf;
596 aiov.iov_len = 1;
597 auio.uio_iov = &aiov;
598 auio.uio_iovcnt = 1;
599 auio.uio_offset = 0;
600 auio.uio_resid = 1;
601 auio.uio_segflg = UIO_SYSSPACE;
602 auio.uio_rw = UIO_READ;
603 auio.uio_td = ap->a_td;
604
605 if (vp->v_type == VREG)
606 error = ncl_readrpc(vp, &auio, ap->a_cred);
607 else if (vp->v_type == VDIR) {
608 char* bp;
609 bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
610 aiov.iov_base = bp;
611 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
612 error = ncl_readdirrpc(vp, &auio, ap->a_cred,
613 ap->a_td);
614 free(bp, M_TEMP);
615 } else if (vp->v_type == VLNK)
616 error = ncl_readlinkrpc(vp, &auio, ap->a_cred);
617 else
618 error = EACCES;
619 } else
620 NFSUNLOCKNODE(np);
621 return (error);
622 }
623 }
624
625 /*
626 * nfs open vnode op
627 * Check to see if the type is ok
628 * and that deletion is not in progress.
629 * For paged in text files, you will need to flush the page cache
630 * if consistency is lost.
631 */
632 /* ARGSUSED */
633 static int
nfs_open(struct vop_open_args * ap)634 nfs_open(struct vop_open_args *ap)
635 {
636 struct vnode *vp = ap->a_vp;
637 struct nfsnode *np = VTONFS(vp);
638 struct vattr vattr;
639 int error;
640 int fmode = ap->a_mode;
641 struct ucred *cred;
642 vm_object_t obj;
643
644 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK)
645 return (EOPNOTSUPP);
646
647 /*
648 * For NFSv4, we need to do the Open Op before cache validation,
649 * so that we conform to RFC3530 Sec. 9.3.1.
650 */
651 if (NFS_ISV4(vp)) {
652 error = nfsrpc_open(vp, fmode, ap->a_cred, ap->a_td);
653 if (error) {
654 error = nfscl_maperr(ap->a_td, error, (uid_t)0,
655 (gid_t)0);
656 return (error);
657 }
658 }
659
660 /*
661 * Now, if this Open will be doing reading, re-validate/flush the
662 * cache, so that Close/Open coherency is maintained.
663 */
664 NFSLOCKNODE(np);
665 if (np->n_flag & NMODIFIED) {
666 NFSUNLOCKNODE(np);
667 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
668 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
669 if (VN_IS_DOOMED(vp))
670 return (EBADF);
671 }
672 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
673 if (error == EINTR || error == EIO) {
674 if (NFS_ISV4(vp))
675 (void) nfsrpc_close(vp, 0, ap->a_td);
676 return (error);
677 }
678 NFSLOCKNODE(np);
679 np->n_attrstamp = 0;
680 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
681 if (vp->v_type == VDIR)
682 np->n_direofoffset = 0;
683 NFSUNLOCKNODE(np);
684 error = VOP_GETATTR(vp, &vattr, ap->a_cred);
685 if (error) {
686 if (NFS_ISV4(vp))
687 (void) nfsrpc_close(vp, 0, ap->a_td);
688 return (error);
689 }
690 NFSLOCKNODE(np);
691 np->n_mtime = vattr.va_mtime;
692 if (NFS_ISV4(vp))
693 np->n_change = vattr.va_filerev;
694 } else {
695 NFSUNLOCKNODE(np);
696 error = VOP_GETATTR(vp, &vattr, ap->a_cred);
697 if (error) {
698 if (NFS_ISV4(vp))
699 (void) nfsrpc_close(vp, 0, ap->a_td);
700 return (error);
701 }
702 NFSLOCKNODE(np);
703 if ((NFS_ISV4(vp) && np->n_change != vattr.va_filerev) ||
704 NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
705 if (vp->v_type == VDIR)
706 np->n_direofoffset = 0;
707 NFSUNLOCKNODE(np);
708 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
709 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
710 if (VN_IS_DOOMED(vp))
711 return (EBADF);
712 }
713 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
714 if (error == EINTR || error == EIO) {
715 if (NFS_ISV4(vp))
716 (void) nfsrpc_close(vp, 0, ap->a_td);
717 return (error);
718 }
719 NFSLOCKNODE(np);
720 np->n_mtime = vattr.va_mtime;
721 if (NFS_ISV4(vp))
722 np->n_change = vattr.va_filerev;
723 }
724 }
725
726 /*
727 * If the object has >= 1 O_DIRECT active opens, we disable caching.
728 */
729 if (newnfs_directio_enable && (fmode & O_DIRECT) &&
730 (vp->v_type == VREG)) {
731 if (np->n_directio_opens == 0) {
732 NFSUNLOCKNODE(np);
733 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
734 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
735 if (VN_IS_DOOMED(vp))
736 return (EBADF);
737 }
738 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
739 if (error) {
740 if (NFS_ISV4(vp))
741 (void) nfsrpc_close(vp, 0, ap->a_td);
742 return (error);
743 }
744 NFSLOCKNODE(np);
745 np->n_flag |= NNONCACHE;
746 }
747 np->n_directio_opens++;
748 }
749
750 /* If opened for writing via NFSv4.1 or later, mark that for pNFS. */
751 if (NFSHASPNFS(VFSTONFS(vp->v_mount)) && (fmode & FWRITE) != 0)
752 np->n_flag |= NWRITEOPENED;
753
754 /*
755 * If this is an open for writing, capture a reference to the
756 * credentials, so they can be used by ncl_putpages(). Using
757 * these write credentials is preferable to the credentials of
758 * whatever thread happens to be doing the VOP_PUTPAGES() since
759 * the write RPCs are less likely to fail with EACCES.
760 */
761 if ((fmode & FWRITE) != 0) {
762 cred = np->n_writecred;
763 np->n_writecred = crhold(ap->a_cred);
764 } else
765 cred = NULL;
766 NFSUNLOCKNODE(np);
767
768 if (cred != NULL)
769 crfree(cred);
770 vnode_create_vobject(vp, vattr.va_size, ap->a_td);
771
772 /*
773 * If the text file has been mmap'd, flush any dirty pages to the
774 * buffer cache and then...
775 * Make sure all writes are pushed to the NFS server. If this is not
776 * done, the modify time of the file can change while the text
777 * file is being executed. This will cause the process that is
778 * executing the text file to be terminated.
779 */
780 if (vp->v_writecount <= -1) {
781 if ((obj = vp->v_object) != NULL &&
782 vm_object_mightbedirty(obj)) {
783 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
784 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
785 if (VN_IS_DOOMED(vp))
786 return (EBADF);
787 }
788 vnode_pager_clean_sync(vp);
789 }
790
791 /* Now, flush the buffer cache. */
792 ncl_flush(vp, MNT_WAIT, curthread, 0, 0);
793
794 /* And, finally, make sure that n_mtime is up to date. */
795 np = VTONFS(vp);
796 NFSLOCKNODE(np);
797 np->n_mtime = np->n_vattr.na_mtime;
798 NFSUNLOCKNODE(np);
799 }
800 return (0);
801 }
802
803 /*
804 * nfs close vnode op
805 * What an NFS client should do upon close after writing is a debatable issue.
806 * Most NFS clients push delayed writes to the server upon close, basically for
807 * two reasons:
808 * 1 - So that any write errors may be reported back to the client process
809 * doing the close system call. By far the two most likely errors are
810 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
811 * 2 - To put a worst case upper bound on cache inconsistency between
812 * multiple clients for the file.
813 * There is also a consistency problem for Version 2 of the protocol w.r.t.
814 * not being able to tell if other clients are writing a file concurrently,
815 * since there is no way of knowing if the changed modify time in the reply
816 * is only due to the write for this client.
817 * (NFS Version 3 provides weak cache consistency data in the reply that
818 * should be sufficient to detect and handle this case.)
819 *
820 * The current code does the following:
821 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
822 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
823 * or commit them (this satisfies 1 and 2 except for the
824 * case where the server crashes after this close but
825 * before the commit RPC, which is felt to be "good
826 * enough". Changing the last argument to ncl_flush() to
827 * a 1 would force a commit operation, if it is felt a
828 * commit is necessary now.
829 * for NFS Version 4 - flush the dirty buffers and commit them, if
830 * nfscl_mustflush() says this is necessary.
831 * It is necessary if there is no write delegation held,
832 * in order to satisfy open/close coherency.
833 * If the file isn't cached on local stable storage,
834 * it may be necessary in order to detect "out of space"
835 * errors from the server, if the write delegation
836 * issued by the server doesn't allow the file to grow.
837 */
838 /* ARGSUSED */
839 static int
nfs_close(struct vop_close_args * ap)840 nfs_close(struct vop_close_args *ap)
841 {
842 struct vnode *vp = ap->a_vp;
843 struct nfsnode *np = VTONFS(vp);
844 struct nfsvattr nfsva;
845 struct ucred *cred;
846 int error = 0, ret, localcred = 0;
847 int fmode = ap->a_fflag;
848 struct nfsmount *nmp;
849
850 if (NFSCL_FORCEDISM(vp->v_mount))
851 return (0);
852 nmp = VFSTONFS(vp->v_mount);
853 /*
854 * During shutdown, a_cred isn't valid, so just use root.
855 */
856 if (ap->a_cred == NOCRED) {
857 cred = newnfs_getcred();
858 localcred = 1;
859 } else {
860 cred = ap->a_cred;
861 }
862 if (vp->v_type == VREG) {
863 /*
864 * Examine and clean dirty pages, regardless of NMODIFIED.
865 * This closes a major hole in close-to-open consistency.
866 * We want to push out all dirty pages (and buffers) on
867 * close, regardless of whether they were dirtied by
868 * mmap'ed writes or via write().
869 */
870 if (nfs_clean_pages_on_close && vp->v_object) {
871 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
872 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
873 if (VN_IS_DOOMED(vp) && ap->a_fflag != FNONBLOCK)
874 return (EBADF);
875 }
876 vnode_pager_clean_async(vp);
877 }
878 NFSLOCKNODE(np);
879 if (np->n_flag & NMODIFIED) {
880 NFSUNLOCKNODE(np);
881 if (NFS_ISV3(vp)) {
882 /*
883 * Under NFSv3 we have dirty buffers to dispose of. We
884 * must flush them to the NFS server. We have the option
885 * of waiting all the way through the commit rpc or just
886 * waiting for the initial write. The default is to only
887 * wait through the initial write so the data is in the
888 * server's cache, which is roughly similar to the state
889 * a standard disk subsystem leaves the file in on close().
890 *
891 * We cannot clear the NMODIFIED bit in np->n_flag due to
892 * potential races with other processes, and certainly
893 * cannot clear it if we don't commit.
894 * These races occur when there is no longer the old
895 * traditional vnode locking implemented for Vnode Ops.
896 */
897 int cm = newnfs_commit_on_close ? 1 : 0;
898 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
899 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
900 if (VN_IS_DOOMED(vp) && ap->a_fflag != FNONBLOCK)
901 return (EBADF);
902 }
903 error = ncl_flush(vp, MNT_WAIT, ap->a_td, cm, 0);
904 /* np->n_flag &= ~NMODIFIED; */
905 } else if (NFS_ISV4(vp)) {
906 if (!NFSHASNFSV4N(nmp) ||
907 (nmp->nm_flag & NFSMNT_NOCTO) == 0 ||
908 nfscl_mustflush(vp) != 0) {
909 int cm = newnfs_commit_on_close ? 1 : 0;
910 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
911 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
912 if (VN_IS_DOOMED(vp) && ap->a_fflag !=
913 FNONBLOCK)
914 return (EBADF);
915 }
916 error = ncl_flush(vp, MNT_WAIT, ap->a_td,
917 cm, 0);
918 /*
919 * as above w.r.t races when clearing
920 * NMODIFIED.
921 * np->n_flag &= ~NMODIFIED;
922 */
923 }
924 } else {
925 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
926 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
927 if (VN_IS_DOOMED(vp) && ap->a_fflag !=
928 FNONBLOCK)
929 return (EBADF);
930 }
931 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
932 }
933 NFSLOCKNODE(np);
934 }
935 /*
936 * Invalidate the attribute cache in all cases.
937 * An open is going to fetch fresh attrs any way, other procs
938 * on this node that have file open will be forced to do an
939 * otw attr fetch, but this is safe.
940 * --> A user found that their RPC count dropped by 20% when
941 * this was commented out and I can't see any requirement
942 * for it, so I've disabled it when negative lookups are
943 * enabled. (What does this have to do with negative lookup
944 * caching? Well nothing, except it was reported by the
945 * same user that needed negative lookup caching and I wanted
946 * there to be a way to disable it to see if it
947 * is the cause of some caching/coherency issue that might
948 * crop up.)
949 */
950 if (nmp->nm_negnametimeo == 0) {
951 np->n_attrstamp = 0;
952 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
953 }
954 if (np->n_flag & NWRITEERR) {
955 np->n_flag &= ~NWRITEERR;
956 error = np->n_error;
957 }
958 NFSUNLOCKNODE(np);
959 }
960
961 if (NFS_ISV4(vp)) {
962 /*
963 * Get attributes so "change" is up to date.
964 */
965 if (error == 0 && nfscl_nodeleg(vp, 0) != 0 &&
966 vp->v_type == VREG &&
967 (nmp->nm_flag & NFSMNT_NOCTO) == 0) {
968 ret = nfsrpc_getattr(vp, cred, ap->a_td, &nfsva);
969 if (!ret) {
970 np->n_change = nfsva.na_filerev;
971 (void) nfscl_loadattrcache(&vp, &nfsva, NULL,
972 0, 0);
973 }
974 }
975
976 /*
977 * and do the close.
978 */
979 ret = nfsrpc_close(vp, 0, ap->a_td);
980 if (!error && ret)
981 error = ret;
982 if (error)
983 error = nfscl_maperr(ap->a_td, error, (uid_t)0,
984 (gid_t)0);
985 }
986 if (newnfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) {
987 NFSLOCKNODE(np);
988 KASSERT((np->n_directio_opens > 0),
989 ("nfs_close: unexpectedly value (0) of n_directio_opens\n"));
990 np->n_directio_opens--;
991 if (np->n_directio_opens == 0)
992 np->n_flag &= ~NNONCACHE;
993 NFSUNLOCKNODE(np);
994 }
995 if (localcred)
996 NFSFREECRED(cred);
997 return (error);
998 }
999
1000 /*
1001 * nfs getattr call from vfs.
1002 */
1003 static int
nfs_getattr(struct vop_getattr_args * ap)1004 nfs_getattr(struct vop_getattr_args *ap)
1005 {
1006 struct vnode *vp = ap->a_vp;
1007 struct thread *td = curthread; /* XXX */
1008 struct nfsnode *np = VTONFS(vp);
1009 int error = 0;
1010 struct nfsvattr nfsva;
1011 struct vattr *vap = ap->a_vap;
1012 struct vattr vattr;
1013 struct nfsmount *nmp;
1014
1015 nmp = VFSTONFS(vp->v_mount);
1016 /*
1017 * Update local times for special files.
1018 */
1019 NFSLOCKNODE(np);
1020 if (np->n_flag & (NACC | NUPD))
1021 np->n_flag |= NCHG;
1022 NFSUNLOCKNODE(np);
1023 /*
1024 * First look in the cache.
1025 * For "syskrb5" mounts, nm_fhsize might still be zero and
1026 * cached attributes should be ignored.
1027 */
1028 if (nmp->nm_fhsize > 0 && ncl_getattrcache(vp, &vattr) == 0) {
1029 ncl_copy_vattr(vap, &vattr);
1030
1031 /*
1032 * Get the local modify time for the case of a write
1033 * delegation.
1034 */
1035 nfscl_deleggetmodtime(vp, &vap->va_mtime);
1036 return (0);
1037 }
1038
1039 if (NFS_ISV34(vp) && nfs_prime_access_cache &&
1040 nfsaccess_cache_timeout > 0) {
1041 NFSINCRGLOBAL(nfsstatsv1.accesscache_misses);
1042 nfs34_access_otw(vp, NFSACCESS_ALL, td, ap->a_cred, NULL);
1043 if (ncl_getattrcache(vp, ap->a_vap) == 0) {
1044 nfscl_deleggetmodtime(vp, &ap->a_vap->va_mtime);
1045 return (0);
1046 }
1047 }
1048
1049 error = nfsrpc_getattr(vp, ap->a_cred, td, &nfsva);
1050 if (error == 0)
1051 error = nfscl_loadattrcache(&vp, &nfsva, vap, 0, 0);
1052 if (!error) {
1053 /*
1054 * Get the local modify time for the case of a write
1055 * delegation.
1056 */
1057 nfscl_deleggetmodtime(vp, &vap->va_mtime);
1058 } else if (NFS_ISV4(vp)) {
1059 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1060 }
1061 return (error);
1062 }
1063
1064 /*
1065 * nfs setattr call.
1066 */
1067 static int
nfs_setattr(struct vop_setattr_args * ap)1068 nfs_setattr(struct vop_setattr_args *ap)
1069 {
1070 struct vnode *vp = ap->a_vp;
1071 struct nfsnode *np = VTONFS(vp);
1072 struct thread *td = curthread; /* XXX */
1073 struct vattr *vap = ap->a_vap;
1074 int error = 0;
1075 u_quad_t tsize;
1076 struct timespec ts;
1077 struct nfsmount *nmp;
1078
1079 #ifndef nolint
1080 tsize = (u_quad_t)0;
1081 #endif
1082
1083 /*
1084 * Only setting of UF_HIDDEN and UF_SYSTEM are supported and
1085 * only for NFSv4 servers that support them.
1086 */
1087 nmp = VFSTONFS(vp->v_mount);
1088 if (vap->va_flags != VNOVAL && (!NFSHASNFSV4(nmp) ||
1089 (vap->va_flags & ~(UF_HIDDEN | UF_SYSTEM)) != 0 ||
1090 ((vap->va_flags & UF_HIDDEN) != 0 &&
1091 !NFSISSET_ATTRBIT(&np->n_vattr.na_suppattr, NFSATTRBIT_HIDDEN)) ||
1092 ((vap->va_flags & UF_SYSTEM) != 0 &&
1093 !NFSISSET_ATTRBIT(&np->n_vattr.na_suppattr, NFSATTRBIT_SYSTEM))))
1094 return (EOPNOTSUPP);
1095
1096 /*
1097 * Disallow write attempts if the filesystem is mounted read-only.
1098 */
1099 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
1100 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
1101 vap->va_mtime.tv_sec != VNOVAL ||
1102 vap->va_birthtime.tv_sec != VNOVAL ||
1103 vap->va_mode != (mode_t)VNOVAL ||
1104 vap->va_flags != (u_long)VNOVAL) &&
1105 (vp->v_mount->mnt_flag & MNT_RDONLY))
1106 return (EROFS);
1107 if (vap->va_size != VNOVAL) {
1108 switch (vp->v_type) {
1109 case VDIR:
1110 return (EISDIR);
1111 case VCHR:
1112 case VBLK:
1113 case VSOCK:
1114 case VFIFO:
1115 if (vap->va_mtime.tv_sec == VNOVAL &&
1116 vap->va_atime.tv_sec == VNOVAL &&
1117 vap->va_birthtime.tv_sec == VNOVAL &&
1118 vap->va_mode == (mode_t)VNOVAL &&
1119 vap->va_uid == (uid_t)VNOVAL &&
1120 vap->va_gid == (gid_t)VNOVAL)
1121 return (0);
1122 vap->va_size = VNOVAL;
1123 break;
1124 default:
1125 /*
1126 * Disallow write attempts if the filesystem is
1127 * mounted read-only.
1128 */
1129 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1130 return (EROFS);
1131 /*
1132 * We run vnode_pager_setsize() early (why?),
1133 * we must set np->n_size now to avoid vinvalbuf
1134 * V_SAVE races that might setsize a lower
1135 * value.
1136 */
1137 NFSLOCKNODE(np);
1138 tsize = np->n_size;
1139 NFSUNLOCKNODE(np);
1140 error = ncl_meta_setsize(vp, td, vap->va_size);
1141 NFSLOCKNODE(np);
1142 if (np->n_flag & NMODIFIED) {
1143 tsize = np->n_size;
1144 NFSUNLOCKNODE(np);
1145 error = ncl_vinvalbuf(vp, vap->va_size == 0 ?
1146 0 : V_SAVE, td, 1);
1147 if (error != 0) {
1148 vnode_pager_setsize(vp, tsize);
1149 return (error);
1150 }
1151 /*
1152 * Call nfscl_delegmodtime() to set the modify time
1153 * locally, as required.
1154 */
1155 nfscl_delegmodtime(vp, NULL);
1156 } else
1157 NFSUNLOCKNODE(np);
1158 /*
1159 * np->n_size has already been set to vap->va_size
1160 * in ncl_meta_setsize(). We must set it again since
1161 * nfs_loadattrcache() could be called through
1162 * ncl_meta_setsize() and could modify np->n_size.
1163 */
1164 NFSLOCKNODE(np);
1165 np->n_vattr.na_size = np->n_size = vap->va_size;
1166 NFSUNLOCKNODE(np);
1167 }
1168 } else {
1169 NFSLOCKNODE(np);
1170 if ((vap->va_mtime.tv_sec != VNOVAL || vap->va_atime.tv_sec != VNOVAL) &&
1171 (np->n_flag & NMODIFIED) && vp->v_type == VREG) {
1172 NFSUNLOCKNODE(np);
1173 error = ncl_vinvalbuf(vp, V_SAVE, td, 1);
1174 if (error == EINTR || error == EIO)
1175 return (error);
1176 } else
1177 NFSUNLOCKNODE(np);
1178 }
1179 error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
1180 if (vap->va_size != VNOVAL) {
1181 if (error == 0) {
1182 nanouptime(&ts);
1183 NFSLOCKNODE(np);
1184 np->n_localmodtime = ts;
1185 NFSUNLOCKNODE(np);
1186 } else {
1187 NFSLOCKNODE(np);
1188 np->n_size = np->n_vattr.na_size = tsize;
1189 vnode_pager_setsize(vp, tsize);
1190 NFSUNLOCKNODE(np);
1191 }
1192 }
1193 if (vap->va_mtime.tv_sec != VNOVAL && error == 0)
1194 nfscl_delegmodtime(vp, &vap->va_mtime);
1195 return (error);
1196 }
1197
1198 /*
1199 * Do an nfs setattr rpc.
1200 */
1201 static int
nfs_setattrrpc(struct vnode * vp,struct vattr * vap,struct ucred * cred,struct thread * td)1202 nfs_setattrrpc(struct vnode *vp, struct vattr *vap, struct ucred *cred,
1203 struct thread *td)
1204 {
1205 struct nfsnode *np = VTONFS(vp);
1206 int error, ret, attrflag, i;
1207 struct nfsvattr nfsva;
1208
1209 if (NFS_ISV34(vp)) {
1210 NFSLOCKNODE(np);
1211 for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
1212 np->n_accesscache[i].stamp = 0;
1213 np->n_flag |= NDELEGMOD;
1214 NFSUNLOCKNODE(np);
1215 KDTRACE_NFS_ACCESSCACHE_FLUSH_DONE(vp);
1216 }
1217 error = nfsrpc_setattr(vp, vap, NULL, cred, td, &nfsva, &attrflag);
1218 if (attrflag) {
1219 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
1220 if (ret && !error)
1221 error = ret;
1222 }
1223 if (error && NFS_ISV4(vp))
1224 error = nfscl_maperr(td, error, vap->va_uid, vap->va_gid);
1225 return (error);
1226 }
1227
1228 /*
1229 * Get a named attribute directory for the vnode.
1230 */
1231 static int
nfs_get_namedattrdir(struct vnode * vp,struct componentname * cnp,struct vnode ** vpp)1232 nfs_get_namedattrdir(struct vnode *vp, struct componentname *cnp,
1233 struct vnode **vpp)
1234 {
1235 struct nfsfh *nfhp;
1236 struct nfsnode *np;
1237 struct vnode *newvp;
1238 struct nfsvattr nfsva;
1239 int attrflag, error;
1240
1241 attrflag = 0;
1242 *vpp = NULL;
1243 np = VTONFS(vp);
1244 error = nfsrpc_openattr(VFSTONFS(vp->v_mount), vp, np->n_fhp->nfh_fh,
1245 np->n_fhp->nfh_len, (cnp->cn_flags & CREATENAMED),
1246 cnp->cn_cred, curthread, &nfsva, &nfhp, &attrflag);
1247 if (error == NFSERR_NOTSUPP)
1248 error = ENOATTR;
1249 if (error == 0)
1250 error = nfscl_nget(vp->v_mount, vp, nfhp, cnp, curthread, &np,
1251 cnp->cn_lkflags);
1252 if (error != 0)
1253 return (error);
1254 newvp = NFSTOV(np);
1255 vn_irflag_set_cond(newvp, VIRF_NAMEDDIR);
1256 if (attrflag != 0)
1257 (void)nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
1258 *vpp = newvp;
1259 return (0);
1260 }
1261
1262 /*
1263 * nfs lookup call, one step at a time...
1264 * First look in cache
1265 * If not found, unlock the directory nfsnode and do the rpc
1266 */
1267 static int
nfs_lookup(struct vop_lookup_args * ap)1268 nfs_lookup(struct vop_lookup_args *ap)
1269 {
1270 struct componentname *cnp = ap->a_cnp;
1271 struct vnode *dvp = ap->a_dvp;
1272 struct vnode **vpp = ap->a_vpp;
1273 struct mount *mp = dvp->v_mount;
1274 uint64_t flags = cnp->cn_flags;
1275 struct vnode *newvp;
1276 struct nfsmount *nmp;
1277 struct nfsnode *np, *newnp;
1278 int error = 0, attrflag, dattrflag, ltype, ncticks;
1279 struct thread *td = curthread;
1280 struct nfsfh *nfhp;
1281 struct nfsvattr dnfsva, nfsva;
1282 struct vattr vattr;
1283 struct timespec nctime, ts;
1284 uint32_t openmode;
1285 bool is_nameddir, needs_nameddir, opennamed;
1286
1287 dattrflag = 0;
1288 *vpp = NULLVP;
1289 nmp = VFSTONFS(mp);
1290 opennamed = (flags & (OPENNAMED | ISLASTCN)) == (OPENNAMED | ISLASTCN);
1291 if (opennamed && (!NFSHASNFSV4(nmp) || !NFSHASNFSV4N(nmp)))
1292 return (ENOATTR);
1293 is_nameddir = (vn_irflag_read(dvp) & VIRF_NAMEDDIR) != 0;
1294 if ((is_nameddir && (flags & ISLASTCN) == 0 && (cnp->cn_namelen > 1 ||
1295 *cnp->cn_nameptr != '.')) ||
1296 (opennamed && !is_nameddir && (flags & ISDOTDOT) != 0))
1297 return (ENOATTR);
1298 if ((flags & ISLASTCN) && (mp->mnt_flag & MNT_RDONLY) &&
1299 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
1300 return (EROFS);
1301 np = VTONFS(dvp);
1302
1303 needs_nameddir = false;
1304 if (opennamed || is_nameddir) {
1305 cnp->cn_flags &= ~MAKEENTRY;
1306 if (!is_nameddir)
1307 needs_nameddir = true;
1308 }
1309
1310 /*
1311 * If the named attribute directory is needed, acquire it now.
1312 */
1313 newvp = NULLVP;
1314 if (needs_nameddir) {
1315 KASSERT(np->n_v4 == NULL, ("nfs_lookup: O_NAMEDATTR when"
1316 " n_v4 not NULL"));
1317 error = nfs_get_namedattrdir(dvp, cnp, &newvp);
1318 if (error != 0)
1319 goto handle_error;
1320 if (cnp->cn_namelen == 1 && *cnp->cn_nameptr == '.') {
1321 *vpp = newvp;
1322 return (0);
1323 }
1324 dvp = newvp;
1325 np = VTONFS(dvp);
1326 newvp = NULLVP;
1327 } else if (opennamed && cnp->cn_namelen == 1 &&
1328 *cnp->cn_nameptr == '.') {
1329 VREF(dvp);
1330 *vpp = dvp;
1331 return (0);
1332 }
1333
1334 if (dvp->v_type != VDIR)
1335 return (ENOTDIR);
1336
1337 /* For NFSv4, wait until any remove is done. */
1338 NFSLOCKNODE(np);
1339 while (NFSHASNFSV4(nmp) && (np->n_flag & NREMOVEINPROG)) {
1340 np->n_flag |= NREMOVEWANT;
1341 (void) msleep((caddr_t)np, &np->n_mtx, PZERO, "nfslkup", 0);
1342 }
1343 NFSUNLOCKNODE(np);
1344
1345 error = vn_dir_check_exec(dvp, cnp);
1346 if (error != 0)
1347 return (error);
1348
1349 if (!opennamed && !is_nameddir) {
1350 error = cache_lookup(dvp, vpp, cnp, &nctime, &ncticks);
1351 if (error > 0 && error != ENOENT)
1352 return (error);
1353 if (error == -1) {
1354 /*
1355 * Lookups of "." are special and always return the
1356 * current directory. cache_lookup() already handles
1357 * associated locking bookkeeping, etc.
1358 */
1359 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
1360 return (0);
1361 }
1362
1363 /*
1364 * We only accept a positive hit in the cache if the
1365 * change time of the file matches our cached copy.
1366 * Otherwise, we discard the cache entry and fallback
1367 * to doing a lookup RPC. We also only trust cache
1368 * entries for less than nm_nametimeo seconds.
1369 *
1370 * To better handle stale file handles and attributes,
1371 * clear the attribute cache of this node if it is a
1372 * leaf component, part of an open() call, and not
1373 * locally modified before fetching the attributes.
1374 * This should allow stale file handles to be detected
1375 * here where we can fall back to a LOOKUP RPC to
1376 * recover rather than having nfs_open() detect the
1377 * stale file handle and failing open(2) with ESTALE.
1378 */
1379 newvp = *vpp;
1380 newnp = VTONFS(newvp);
1381 if (!(nmp->nm_flag & NFSMNT_NOCTO) &&
1382 (flags & (ISLASTCN | ISOPEN)) ==
1383 (ISLASTCN | ISOPEN) &&
1384 !(newnp->n_flag & NMODIFIED)) {
1385 NFSLOCKNODE(newnp);
1386 newnp->n_attrstamp = 0;
1387 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1388 NFSUNLOCKNODE(newnp);
1389 }
1390 if (nfscl_nodeleg(newvp, 0) == 0 ||
1391 ((u_int)(ticks - ncticks) <
1392 (nmp->nm_nametimeo * hz) &&
1393 VOP_GETATTR(newvp, &vattr, cnp->cn_cred) == 0 &&
1394 timespeccmp(&vattr.va_ctime, &nctime, ==))) {
1395 NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits);
1396 return (0);
1397 }
1398 cache_purge(newvp);
1399 if (dvp != newvp)
1400 vput(newvp);
1401 else
1402 vrele(newvp);
1403 *vpp = NULLVP;
1404 } else if (error == ENOENT) {
1405 if (VN_IS_DOOMED(dvp))
1406 return (ENOENT);
1407 /*
1408 * We only accept a negative hit in the cache if the
1409 * modification time of the parent directory matches
1410 * the cached copy in the name cache entry.
1411 * Otherwise, we discard all of the negative cache
1412 * entries for this directory. We also only trust
1413 * negative cache entries for up to nm_negnametimeo
1414 * seconds.
1415 */
1416 if ((u_int)(ticks - ncticks) <
1417 (nmp->nm_negnametimeo * hz) &&
1418 VOP_GETATTR(dvp, &vattr, cnp->cn_cred) == 0 &&
1419 timespeccmp(&vattr.va_mtime, &nctime, ==)) {
1420 NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits);
1421 return (ENOENT);
1422 }
1423 cache_purge_negative(dvp);
1424 }
1425 }
1426
1427 openmode = 0;
1428 #if 0
1429 /*
1430 * The use of LookupOpen breaks some builds. It is disabled
1431 * until that is fixed.
1432 */
1433 /*
1434 * If this an NFSv4.1/4.2 mount using the "oneopenown" mount
1435 * option, it is possible to do the Open operation in the same
1436 * compound as Lookup, so long as delegations are not being
1437 * issued. This saves doing a separate RPC for Open.
1438 * For pnfs, do not do this, since the Open+LayoutGet will
1439 * be needed as a separate RPC.
1440 */
1441 NFSLOCKMNT(nmp);
1442 if (NFSHASNFSV4N(nmp) && NFSHASONEOPENOWN(nmp) && !NFSHASPNFS(nmp) &&
1443 (nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0 &&
1444 (!NFSMNT_RDONLY(mp) || (flags & OPENWRITE) == 0) &&
1445 (flags & (ISLASTCN | ISOPEN | OPENNAMED))) == (ISLASTCN | ISOPEN)) {
1446 if ((flags & OPENREAD) != 0)
1447 openmode |= NFSV4OPEN_ACCESSREAD;
1448 if ((flags & OPENWRITE) != 0)
1449 openmode |= NFSV4OPEN_ACCESSWRITE;
1450 }
1451 NFSUNLOCKMNT(nmp);
1452 #endif
1453
1454 newvp = NULLVP;
1455 NFSINCRGLOBAL(nfsstatsv1.lookupcache_misses);
1456 nanouptime(&ts);
1457 error = nfsrpc_lookup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1458 cnp->cn_cred, td, &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1459 openmode);
1460 if (dattrflag)
1461 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
1462 if (needs_nameddir) {
1463 vput(dvp);
1464 dvp = ap->a_dvp;
1465 }
1466 handle_error:
1467 if (error) {
1468 if (newvp != NULLVP) {
1469 vput(newvp);
1470 *vpp = NULLVP;
1471 }
1472
1473 if (error != ENOENT) {
1474 if (NFS_ISV4(dvp))
1475 error = nfscl_maperr(td, error,
1476 (uid_t)0, (gid_t)0);
1477 return (error);
1478 }
1479
1480 /* The requested file was not found. */
1481 if ((cnp->cn_nameiop == CREATE ||
1482 cnp->cn_nameiop == RENAME) &&
1483 (flags & ISLASTCN)) {
1484 /*
1485 * XXX: UFS does a full VOP_ACCESS(dvp,
1486 * VWRITE) here instead of just checking
1487 * MNT_RDONLY.
1488 */
1489 if (mp->mnt_flag & MNT_RDONLY)
1490 return (EROFS);
1491 return (EJUSTRETURN);
1492 }
1493
1494 if ((cnp->cn_flags & MAKEENTRY) != 0 && dattrflag) {
1495 /*
1496 * Cache the modification time of the parent
1497 * directory from the post-op attributes in
1498 * the name cache entry. The negative cache
1499 * entry will be ignored once the directory
1500 * has changed. Don't bother adding the entry
1501 * if the directory has already changed.
1502 */
1503 NFSLOCKNODE(np);
1504 if (timespeccmp(&np->n_vattr.na_mtime,
1505 &dnfsva.na_mtime, ==)) {
1506 NFSUNLOCKNODE(np);
1507 cache_enter_time(dvp, NULL, cnp,
1508 &dnfsva.na_mtime, NULL);
1509 } else
1510 NFSUNLOCKNODE(np);
1511 }
1512 return (ENOENT);
1513 }
1514
1515 /*
1516 * Handle RENAME case...
1517 */
1518 if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
1519 if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1520 free(nfhp, M_NFSFH);
1521 return (EISDIR);
1522 }
1523 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np,
1524 LK_EXCLUSIVE);
1525 if (error)
1526 return (error);
1527 newvp = NFSTOV(np);
1528 /*
1529 * If n_localmodtime >= time before RPC, then
1530 * a file modification operation, such as
1531 * VOP_SETATTR() of size, has occurred while
1532 * the Lookup RPC and acquisition of the vnode
1533 * happened. As such, the attributes might
1534 * be stale, with possibly an incorrect size.
1535 */
1536 NFSLOCKNODE(np);
1537 if (timespecisset(&np->n_localmodtime) &&
1538 timespeccmp(&np->n_localmodtime, &ts, >=)) {
1539 NFSCL_DEBUG(4, "nfs_lookup: rename localmod "
1540 "stale attributes\n");
1541 attrflag = 0;
1542 }
1543 NFSUNLOCKNODE(np);
1544 if (attrflag)
1545 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
1546 0, 1);
1547 *vpp = newvp;
1548 return (0);
1549 }
1550
1551 if (flags & ISDOTDOT) {
1552 ltype = NFSVOPISLOCKED(dvp);
1553 error = vfs_busy(mp, MBF_NOWAIT);
1554 if (error != 0) {
1555 vfs_ref(mp);
1556 NFSVOPUNLOCK(dvp);
1557 error = vfs_busy(mp, 0);
1558 NFSVOPLOCK(dvp, ltype | LK_RETRY);
1559 vfs_rel(mp);
1560 if (error == 0 && VN_IS_DOOMED(dvp)) {
1561 vfs_unbusy(mp);
1562 error = ENOENT;
1563 }
1564 if (error != 0)
1565 return (error);
1566 }
1567 NFSVOPUNLOCK(dvp);
1568 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np,
1569 cnp->cn_lkflags);
1570 if (error == 0)
1571 newvp = NFSTOV(np);
1572 vfs_unbusy(mp);
1573 if (newvp != dvp)
1574 NFSVOPLOCK(dvp, ltype | LK_RETRY);
1575 if (VN_IS_DOOMED(dvp)) {
1576 if (error == 0) {
1577 if (newvp == dvp)
1578 vrele(newvp);
1579 else
1580 vput(newvp);
1581 }
1582 error = ENOENT;
1583 }
1584 if (error != 0)
1585 return (error);
1586 if (attrflag)
1587 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
1588 0, 1);
1589 } else if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1590 free(nfhp, M_NFSFH);
1591 VREF(dvp);
1592 newvp = dvp;
1593 if (attrflag)
1594 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
1595 0, 1);
1596 } else {
1597 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np,
1598 cnp->cn_lkflags);
1599 if (error)
1600 return (error);
1601 newvp = NFSTOV(np);
1602 if (opennamed)
1603 vn_irflag_set_cond(newvp, VIRF_NAMEDATTR);
1604 /*
1605 * If n_localmodtime >= time before RPC, then
1606 * a file modification operation, such as
1607 * VOP_SETATTR() of size, has occurred while
1608 * the Lookup RPC and acquisition of the vnode
1609 * happened. As such, the attributes might
1610 * be stale, with possibly an incorrect size.
1611 */
1612 NFSLOCKNODE(np);
1613 if (timespecisset(&np->n_localmodtime) &&
1614 timespeccmp(&np->n_localmodtime, &ts, >=)) {
1615 NFSCL_DEBUG(4, "nfs_lookup: localmod "
1616 "stale attributes\n");
1617 attrflag = 0;
1618 }
1619 NFSUNLOCKNODE(np);
1620 if (attrflag)
1621 (void)nfscl_loadattrcache(&newvp, &nfsva, NULL,
1622 0, 1);
1623 else if ((flags & (ISLASTCN | ISOPEN)) ==
1624 (ISLASTCN | ISOPEN) &&
1625 !(np->n_flag & NMODIFIED)) {
1626 /*
1627 * Flush the attribute cache when opening a
1628 * leaf node to ensure that fresh attributes
1629 * are fetched in nfs_open() since we did not
1630 * fetch attributes from the LOOKUP reply.
1631 */
1632 NFSLOCKNODE(np);
1633 np->n_attrstamp = 0;
1634 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1635 NFSUNLOCKNODE(np);
1636 }
1637 }
1638 if ((cnp->cn_flags & MAKEENTRY) && dvp != newvp &&
1639 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN)) &&
1640 attrflag != 0 && (newvp->v_type != VDIR || dattrflag != 0))
1641 cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
1642 newvp->v_type != VDIR ? NULL : &dnfsva.na_ctime);
1643 *vpp = newvp;
1644 return (0);
1645 }
1646
1647 /*
1648 * nfs read call.
1649 * Just call ncl_bioread() to do the work.
1650 */
1651 static int
nfs_read(struct vop_read_args * ap)1652 nfs_read(struct vop_read_args *ap)
1653 {
1654 struct vnode *vp = ap->a_vp;
1655
1656 switch (vp->v_type) {
1657 case VREG:
1658 return (ncl_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
1659 case VDIR:
1660 return (EISDIR);
1661 default:
1662 return (EOPNOTSUPP);
1663 }
1664 }
1665
1666 /*
1667 * nfs readlink call
1668 */
1669 static int
nfs_readlink(struct vop_readlink_args * ap)1670 nfs_readlink(struct vop_readlink_args *ap)
1671 {
1672 struct vnode *vp = ap->a_vp;
1673
1674 if (vp->v_type != VLNK)
1675 return (EINVAL);
1676 return (ncl_bioread(vp, ap->a_uio, 0, ap->a_cred));
1677 }
1678
1679 /*
1680 * Do a readlink rpc.
1681 * Called by ncl_doio() from below the buffer cache.
1682 */
1683 int
ncl_readlinkrpc(struct vnode * vp,struct uio * uiop,struct ucred * cred)1684 ncl_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1685 {
1686 int error, ret, attrflag;
1687 struct nfsvattr nfsva;
1688
1689 error = nfsrpc_readlink(vp, uiop, cred, uiop->uio_td, &nfsva,
1690 &attrflag);
1691 if (attrflag) {
1692 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
1693 if (ret && !error)
1694 error = ret;
1695 }
1696 if (error && NFS_ISV4(vp))
1697 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1698 return (error);
1699 }
1700
1701 /*
1702 * nfs read rpc call
1703 * Ditto above
1704 */
1705 int
ncl_readrpc(struct vnode * vp,struct uio * uiop,struct ucred * cred)1706 ncl_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1707 {
1708 int error, ret, attrflag;
1709 struct nfsvattr nfsva;
1710 struct nfsmount *nmp;
1711
1712 nmp = VFSTONFS(vp->v_mount);
1713 error = EIO;
1714 attrflag = 0;
1715 if (NFSHASPNFS(nmp))
1716 error = nfscl_doiods(vp, uiop, NULL, NULL,
1717 NFSV4OPEN_ACCESSREAD, 0, cred, uiop->uio_td);
1718 NFSCL_DEBUG(4, "readrpc: aft doiods=%d\n", error);
1719 if (error != 0 && error != EFAULT)
1720 error = nfsrpc_read(vp, uiop, cred, uiop->uio_td, &nfsva,
1721 &attrflag);
1722 if (attrflag) {
1723 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
1724 if (ret && !error)
1725 error = ret;
1726 }
1727 if (error && NFS_ISV4(vp))
1728 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1729 return (error);
1730 }
1731
1732 /*
1733 * nfs write call
1734 */
1735 int
ncl_writerpc(struct vnode * vp,struct uio * uiop,struct ucred * cred,int * iomode,int * must_commit,int called_from_strategy,int ioflag)1736 ncl_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
1737 int *iomode, int *must_commit, int called_from_strategy, int ioflag)
1738 {
1739 struct nfsvattr nfsva;
1740 int error, attrflag, ret;
1741 struct nfsmount *nmp;
1742
1743 nmp = VFSTONFS(vp->v_mount);
1744 error = EIO;
1745 attrflag = 0;
1746 if (NFSHASPNFS(nmp))
1747 error = nfscl_doiods(vp, uiop, iomode, must_commit,
1748 NFSV4OPEN_ACCESSWRITE, 0, cred, uiop->uio_td);
1749 NFSCL_DEBUG(4, "writerpc: aft doiods=%d\n", error);
1750 if (error != 0 && error != EFAULT)
1751 error = nfsrpc_write(vp, uiop, iomode, must_commit, cred,
1752 uiop->uio_td, &nfsva, &attrflag, called_from_strategy,
1753 ioflag);
1754 if (attrflag) {
1755 if (VTONFS(vp)->n_flag & ND_NFSV4)
1756 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 1, 1);
1757 else
1758 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
1759 if (ret && !error)
1760 error = ret;
1761 }
1762 if (DOINGASYNC(vp))
1763 *iomode = NFSWRITE_FILESYNC;
1764 if (error && NFS_ISV4(vp))
1765 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1766 return (error);
1767 }
1768
1769 /*
1770 * nfs mknod rpc
1771 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1772 * mode set to specify the file type and the size field for rdev.
1773 */
1774 static int
nfs_mknodrpc(struct vnode * dvp,struct vnode ** vpp,struct componentname * cnp,struct vattr * vap)1775 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1776 struct vattr *vap)
1777 {
1778 struct nfsvattr nfsva, dnfsva;
1779 struct vnode *newvp = NULL;
1780 struct nfsnode *np = NULL, *dnp;
1781 struct nfsfh *nfhp;
1782 struct vattr vattr;
1783 int error = 0, attrflag, dattrflag;
1784 u_int32_t rdev;
1785
1786 if (vap->va_type == VCHR || vap->va_type == VBLK)
1787 rdev = vap->va_rdev;
1788 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1789 rdev = 0xffffffff;
1790 else
1791 return (EOPNOTSUPP);
1792 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1793 return (error);
1794 error = nfsrpc_mknod(dvp, cnp->cn_nameptr, cnp->cn_namelen, vap,
1795 rdev, vap->va_type, cnp->cn_cred, curthread, &dnfsva,
1796 &nfsva, &nfhp, &attrflag, &dattrflag);
1797 if (!error) {
1798 if (!nfhp)
1799 (void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1800 cnp->cn_namelen, cnp->cn_cred, curthread,
1801 &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag, 0);
1802 if (nfhp)
1803 error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1804 curthread, &np, LK_EXCLUSIVE);
1805 }
1806 if (dattrflag)
1807 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
1808 if (!error) {
1809 newvp = NFSTOV(np);
1810 if (attrflag != 0) {
1811 error = nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
1812 if (error != 0)
1813 vput(newvp);
1814 }
1815 }
1816 if (!error) {
1817 *vpp = newvp;
1818 } else if (NFS_ISV4(dvp)) {
1819 error = nfscl_maperr(curthread, error, vap->va_uid,
1820 vap->va_gid);
1821 }
1822 dnp = VTONFS(dvp);
1823 NFSLOCKNODE(dnp);
1824 dnp->n_flag |= NMODIFIED;
1825 if (!dattrflag) {
1826 dnp->n_attrstamp = 0;
1827 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1828 }
1829 NFSUNLOCKNODE(dnp);
1830 return (error);
1831 }
1832
1833 /*
1834 * nfs mknod vop
1835 * just call nfs_mknodrpc() to do the work.
1836 */
1837 /* ARGSUSED */
1838 static int
nfs_mknod(struct vop_mknod_args * ap)1839 nfs_mknod(struct vop_mknod_args *ap)
1840 {
1841 return (nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap));
1842 }
1843
1844 static struct mtx nfs_cverf_mtx;
1845 MTX_SYSINIT(nfs_cverf_mtx, &nfs_cverf_mtx, "NFS create verifier mutex",
1846 MTX_DEF);
1847
1848 static nfsquad_t
nfs_get_cverf(void)1849 nfs_get_cverf(void)
1850 {
1851 static nfsquad_t cverf;
1852 nfsquad_t ret;
1853 static int cverf_initialized = 0;
1854
1855 mtx_lock(&nfs_cverf_mtx);
1856 if (cverf_initialized == 0) {
1857 cverf.lval[0] = arc4random();
1858 cverf.lval[1] = arc4random();
1859 cverf_initialized = 1;
1860 } else
1861 cverf.qval++;
1862 ret = cverf;
1863 mtx_unlock(&nfs_cverf_mtx);
1864
1865 return (ret);
1866 }
1867
1868 /*
1869 * nfs file create call
1870 */
1871 static int
nfs_create(struct vop_create_args * ap)1872 nfs_create(struct vop_create_args *ap)
1873 {
1874 struct vnode *dvp = ap->a_dvp;
1875 struct vattr *vap = ap->a_vap;
1876 struct componentname *cnp = ap->a_cnp;
1877 struct nfsnode *np = NULL, *dnp;
1878 struct vnode *newvp = NULL;
1879 struct nfsmount *nmp;
1880 struct nfsvattr dnfsva, nfsva;
1881 struct nfsfh *nfhp;
1882 nfsquad_t cverf;
1883 int error = 0, attrflag, dattrflag, fmode = 0;
1884 struct vattr vattr;
1885 bool is_nameddir, needs_nameddir, opennamed;
1886
1887 /*
1888 * Oops, not for me..
1889 */
1890 if (vap->va_type == VSOCK)
1891 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1892
1893 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1894 return (error);
1895 if (vap->va_vaflags & VA_EXCLUSIVE)
1896 fmode |= O_EXCL;
1897 dnp = VTONFS(dvp);
1898 nmp = VFSTONFS(dvp->v_mount);
1899 needs_nameddir = false;
1900 if (NFSHASNFSV4(nmp) && NFSHASNFSV4N(nmp)) {
1901 opennamed = (cnp->cn_flags & (OPENNAMED | ISLASTCN)) ==
1902 (OPENNAMED | ISLASTCN);
1903 is_nameddir = (vn_irflag_read(dvp) & VIRF_NAMEDDIR) != 0;
1904 if (opennamed || is_nameddir) {
1905 cnp->cn_flags &= ~MAKEENTRY;
1906 if (!is_nameddir)
1907 needs_nameddir = true;
1908 }
1909 }
1910
1911 /*
1912 * If the named attribute directory is needed, acquire it now.
1913 */
1914 if (needs_nameddir) {
1915 KASSERT(dnp->n_v4 == NULL, ("nfs_create: O_NAMEDATTR when"
1916 " n_v4 not NULL"));
1917 error = nfs_get_namedattrdir(dvp, cnp, &newvp);
1918 if (error != 0)
1919 return (error);
1920 dvp = newvp;
1921 dnp = VTONFS(dvp);
1922 newvp = NULL;
1923 }
1924
1925 again:
1926 /* For NFSv4, wait until any remove is done. */
1927 NFSLOCKNODE(dnp);
1928 while (NFSHASNFSV4(nmp) && (dnp->n_flag & NREMOVEINPROG)) {
1929 dnp->n_flag |= NREMOVEWANT;
1930 (void) msleep((caddr_t)dnp, &dnp->n_mtx, PZERO, "nfscrt", 0);
1931 }
1932 NFSUNLOCKNODE(dnp);
1933
1934 cverf = nfs_get_cverf();
1935 error = nfsrpc_create(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1936 vap, cverf, fmode, cnp->cn_cred, curthread, &dnfsva, &nfsva,
1937 &nfhp, &attrflag, &dattrflag);
1938 if (!error) {
1939 if (nfhp == NULL)
1940 (void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1941 cnp->cn_namelen, cnp->cn_cred, curthread,
1942 &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag, 0);
1943 if (nfhp != NULL)
1944 error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1945 curthread, &np, LK_EXCLUSIVE);
1946 }
1947 if (dattrflag)
1948 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
1949 if (!error) {
1950 newvp = NFSTOV(np);
1951 if (attrflag == 0)
1952 error = nfsrpc_getattr(newvp, cnp->cn_cred, curthread,
1953 &nfsva);
1954 if (error == 0)
1955 error = nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
1956 }
1957 if (error) {
1958 if (newvp != NULL) {
1959 vput(newvp);
1960 newvp = NULL;
1961 }
1962 if (NFS_ISV34(dvp) && (fmode & O_EXCL) &&
1963 error == NFSERR_NOTSUPP) {
1964 fmode &= ~O_EXCL;
1965 goto again;
1966 }
1967 } else if (NFS_ISV34(dvp) && (fmode & O_EXCL)) {
1968 if (nfscl_checksattr(vap, &nfsva)) {
1969 error = nfsrpc_setattr(newvp, vap, NULL, cnp->cn_cred,
1970 curthread, &nfsva, &attrflag);
1971 if (error && (vap->va_uid != (uid_t)VNOVAL ||
1972 vap->va_gid != (gid_t)VNOVAL)) {
1973 /* try again without setting uid/gid */
1974 vap->va_uid = (uid_t)VNOVAL;
1975 vap->va_gid = (uid_t)VNOVAL;
1976 error = nfsrpc_setattr(newvp, vap, NULL,
1977 cnp->cn_cred, curthread, &nfsva, &attrflag);
1978 }
1979 if (attrflag)
1980 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
1981 0, 1);
1982 if (error != 0)
1983 vput(newvp);
1984 }
1985 }
1986 if (!error) {
1987 if ((cnp->cn_flags & MAKEENTRY) && attrflag) {
1988 if (dvp != newvp)
1989 cache_enter_time(dvp, newvp, cnp,
1990 &nfsva.na_ctime, NULL);
1991 else
1992 printf("nfs_create: bogus NFS server returned "
1993 "the directory as the new file object\n");
1994 }
1995 *ap->a_vpp = newvp;
1996 } else if (NFS_ISV4(dvp)) {
1997 error = nfscl_maperr(curthread, error, vap->va_uid,
1998 vap->va_gid);
1999 }
2000 NFSLOCKNODE(dnp);
2001 dnp->n_flag |= NMODIFIED;
2002 if (!dattrflag) {
2003 dnp->n_attrstamp = 0;
2004 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2005 }
2006 NFSUNLOCKNODE(dnp);
2007 if (needs_nameddir)
2008 vput(dvp);
2009 return (error);
2010 }
2011
2012 /*
2013 * nfs file remove call
2014 * To try and make nfs semantics closer to ufs semantics, a file that has
2015 * other processes using the vnode is renamed instead of removed and then
2016 * removed later on the last close.
2017 * - If v_usecount > 1
2018 * If a rename is not already in the works
2019 * call nfs_sillyrename() to set it up
2020 * else
2021 * do the remove rpc
2022 */
2023 static int
nfs_remove(struct vop_remove_args * ap)2024 nfs_remove(struct vop_remove_args *ap)
2025 {
2026 struct vnode *vp = ap->a_vp;
2027 struct vnode *dvp = ap->a_dvp;
2028 struct componentname *cnp = ap->a_cnp;
2029 struct nfsnode *np = VTONFS(vp);
2030 int error = 0;
2031 struct vattr vattr;
2032 struct nfsmount *nmp;
2033
2034 KASSERT(vrefcnt(vp) > 0, ("nfs_remove: bad v_usecount"));
2035 if (vp->v_type == VDIR)
2036 error = EPERM;
2037 else if (vrefcnt(vp) == 1 || (np->n_sillyrename &&
2038 VOP_GETATTR(vp, &vattr, cnp->cn_cred) == 0 &&
2039 vattr.va_nlink > 1)) {
2040 nmp = VFSTONFS(vp->v_mount);
2041 /*
2042 * Purge the name cache so that the chance of a lookup for
2043 * the name succeeding while the remove is in progress is
2044 * minimized. Without node locking it can still happen, such
2045 * that an I/O op returns ESTALE, but since you get this if
2046 * another host removes the file..
2047 */
2048 cache_purge(vp);
2049 /*
2050 * throw away biocache buffers, mainly to avoid
2051 * unnecessary delayed writes later.
2052 * Flushing here would be more correct for the case
2053 * where nfs_close() did not do a flush. However, it
2054 * could be a large performance hit for some servers
2055 * and only matters when the file name being removed is
2056 * one of multiple hard links.
2057 */
2058 if (!NFSHASNFSV4(nmp) || !NFSHASNFSV4N(nmp) ||
2059 (nmp->nm_flag & NFSMNT_NOCTO) == 0)
2060 error = ncl_vinvalbuf(vp, 0, curthread, 1);
2061 if (error != EINTR && error != EIO)
2062 /* Do the rpc */
2063 error = nfs_removerpc(dvp, vp, cnp->cn_nameptr,
2064 cnp->cn_namelen, cnp->cn_cred, curthread, false);
2065 /*
2066 * Kludge City: If the first reply to the remove rpc is lost..
2067 * the reply to the retransmitted request will be ENOENT
2068 * since the file was in fact removed
2069 * Therefore, we cheat and return success.
2070 */
2071 if (error == ENOENT)
2072 error = 0;
2073 } else if (!np->n_sillyrename)
2074 error = nfs_sillyrename(dvp, vp, cnp);
2075 NFSLOCKNODE(np);
2076 np->n_attrstamp = 0;
2077 NFSUNLOCKNODE(np);
2078 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
2079 return (error);
2080 }
2081
2082 /*
2083 * nfs file remove rpc called from nfs_inactive
2084 */
2085 int
ncl_removeit(struct sillyrename * sp,struct vnode * vp)2086 ncl_removeit(struct sillyrename *sp, struct vnode *vp)
2087 {
2088 /*
2089 * Make sure that the directory vnode is still valid.
2090 * XXX we should lock sp->s_dvp here.
2091 */
2092 if (sp->s_dvp->v_type == VBAD)
2093 return (0);
2094 return (nfs_removerpc(sp->s_dvp, vp, sp->s_name, sp->s_namlen,
2095 sp->s_cred, NULL, true));
2096 }
2097
2098 /*
2099 * Handle the nfsremove_status reply from the RPC function.
2100 */
2101 static void
nfs_removestatus(struct vnode * vp,nfsremove_status file_status,bool silly,struct thread * td)2102 nfs_removestatus(struct vnode *vp, nfsremove_status file_status,
2103 bool silly, struct thread *td)
2104 {
2105
2106 switch (file_status) {
2107 case NLINK_ZERO:
2108 /* Get rid of any delegation. */
2109 nfscl_delegreturnvp(vp, false, td);
2110 /* FALLTHROUGH */
2111 case DELETED:
2112 /* Throw away buffer cache blocks. */
2113 (void)ncl_vinvalbuf(vp, 0, td, 1);
2114 break;
2115 case VALID:
2116 /* Nothing to do, delegation is still ok. */
2117 break;
2118 default:
2119 break;
2120 }
2121 }
2122
2123 /*
2124 * Nfs remove rpc, called from nfs_remove() and ncl_removeit().
2125 */
2126 static int
nfs_removerpc(struct vnode * dvp,struct vnode * vp,char * name,int namelen,struct ucred * cred,struct thread * td,bool silly)2127 nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
2128 int namelen, struct ucred *cred, struct thread *td, bool silly)
2129 {
2130 struct nfsvattr dnfsva, nfsva;
2131 struct nfsnode *dnp = VTONFS(dvp);
2132 struct nfsmount *nmp;
2133 int attrflag, error = 0, dattrflag;
2134 nfsremove_status file_status;
2135
2136 nmp = VFSTONFS(dvp->v_mount);
2137 NFSLOCKNODE(dnp);
2138 dnp->n_flag |= NREMOVEINPROG;
2139 NFSUNLOCKNODE(dnp);
2140 error = nfsrpc_remove(dvp, name, namelen, vp, &nfsva, &attrflag,
2141 &file_status, &dnfsva, &dattrflag, cred, td);
2142 NFSLOCKNODE(dnp);
2143 if ((dnp->n_flag & NREMOVEWANT)) {
2144 dnp->n_flag &= ~(NREMOVEWANT | NREMOVEINPROG);
2145 NFSUNLOCKNODE(dnp);
2146 wakeup((caddr_t)dnp);
2147 } else {
2148 dnp->n_flag &= ~NREMOVEINPROG;
2149 NFSUNLOCKNODE(dnp);
2150 }
2151
2152 if (NFSHASNFSV4(nmp) && NFSHASNFSV4N(nmp)) {
2153 if (file_status != DELETED && attrflag != 0)
2154 (void)nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
2155 if ((nmp->nm_flag & NFSMNT_NOCTO) != 0)
2156 nfs_removestatus(vp, file_status, silly, td);
2157 }
2158
2159 if (dattrflag != 0)
2160 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
2161 NFSLOCKNODE(dnp);
2162 dnp->n_flag |= NMODIFIED;
2163 if (dattrflag == 0) {
2164 dnp->n_attrstamp = 0;
2165 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2166 }
2167 NFSUNLOCKNODE(dnp);
2168 if (error && NFS_ISV4(dvp))
2169 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2170 return (error);
2171 }
2172
2173 /*
2174 * nfs file rename call
2175 */
2176 static int
nfs_rename(struct vop_rename_args * ap)2177 nfs_rename(struct vop_rename_args *ap)
2178 {
2179 struct vnode *fvp = ap->a_fvp;
2180 struct vnode *tvp = ap->a_tvp;
2181 struct vnode *fdvp = ap->a_fdvp;
2182 struct vnode *tdvp = ap->a_tdvp;
2183 struct componentname *tcnp = ap->a_tcnp;
2184 struct componentname *fcnp = ap->a_fcnp;
2185 struct nfsnode *fnp = VTONFS(ap->a_fvp);
2186 struct nfsnode *tdnp = VTONFS(ap->a_tdvp);
2187 struct nfsv4node *newv4 = NULL;
2188 struct nfsmount *nmp;
2189 int error;
2190
2191 /* Check for cross-device rename */
2192 if ((fvp->v_mount != tdvp->v_mount) ||
2193 (tvp && (fvp->v_mount != tvp->v_mount))) {
2194 error = EXDEV;
2195 goto out;
2196 }
2197 nmp = VFSTONFS(fvp->v_mount);
2198
2199 if (fvp == tvp) {
2200 printf("nfs_rename: fvp == tvp (can't happen)\n");
2201 error = 0;
2202 goto out;
2203 }
2204 if ((error = NFSVOPLOCK(fvp, LK_EXCLUSIVE)) != 0)
2205 goto out;
2206
2207 /*
2208 * We have to flush B_DELWRI data prior to renaming
2209 * the file. If we don't, the delayed-write buffers
2210 * can be flushed out later after the file has gone stale
2211 * under NFSV3. NFSV2 does not have this problem because
2212 * ( as far as I can tell ) it flushes dirty buffers more
2213 * often.
2214 *
2215 * Skip the rename operation if the fsync fails, this can happen
2216 * due to the server's volume being full, when we pushed out data
2217 * that was written back to our cache earlier. Not checking for
2218 * this condition can result in potential (silent) data loss.
2219 */
2220 if ((nmp->nm_flag & NFSMNT_NOCTO) == 0 || !NFSHASNFSV4(nmp) ||
2221 !NFSHASNFSV4N(nmp) || nfscl_mustflush(fvp) != 0)
2222 error = VOP_FSYNC(fvp, MNT_WAIT, curthread);
2223 NFSVOPUNLOCK(fvp);
2224 if (error == 0 && tvp != NULL && ((nmp->nm_flag & NFSMNT_NOCTO) == 0 ||
2225 !NFSHASNFSV4(nmp) || !NFSHASNFSV4N(nmp) ||
2226 nfscl_mustflush(tvp) != 0))
2227 error = VOP_FSYNC(tvp, MNT_WAIT, curthread);
2228 if (error != 0)
2229 goto out;
2230
2231 /*
2232 * If the tvp exists and is in use, sillyrename it before doing the
2233 * rename of the new file over it.
2234 * XXX Can't sillyrename a directory.
2235 */
2236 if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
2237 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
2238 vput(tvp);
2239 tvp = NULL;
2240 }
2241
2242 error = nfs_renamerpc(fdvp, fvp, fcnp->cn_nameptr, fcnp->cn_namelen,
2243 tdvp, tvp, tcnp->cn_nameptr, tcnp->cn_namelen, false, tcnp->cn_cred,
2244 curthread);
2245
2246 if (error == 0 && NFS_ISV4(tdvp)) {
2247 /*
2248 * For NFSv4, check to see if it is the same name and
2249 * replace the name, if it is different.
2250 */
2251 newv4 = malloc(
2252 sizeof (struct nfsv4node) +
2253 tdnp->n_fhp->nfh_len + tcnp->cn_namelen - 1,
2254 M_NFSV4NODE, M_WAITOK);
2255 NFSLOCKNODE(tdnp);
2256 NFSLOCKNODE(fnp);
2257 if (fnp->n_v4 != NULL && fvp->v_type == VREG &&
2258 (fnp->n_v4->n4_namelen != tcnp->cn_namelen ||
2259 NFSBCMP(tcnp->cn_nameptr, NFS4NODENAME(fnp->n_v4),
2260 tcnp->cn_namelen) ||
2261 tdnp->n_fhp->nfh_len != fnp->n_v4->n4_fhlen ||
2262 NFSBCMP(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
2263 tdnp->n_fhp->nfh_len))) {
2264 free(fnp->n_v4, M_NFSV4NODE);
2265 fnp->n_v4 = newv4;
2266 newv4 = NULL;
2267 fnp->n_v4->n4_fhlen = tdnp->n_fhp->nfh_len;
2268 fnp->n_v4->n4_namelen = tcnp->cn_namelen;
2269 NFSBCOPY(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
2270 tdnp->n_fhp->nfh_len);
2271 NFSBCOPY(tcnp->cn_nameptr,
2272 NFS4NODENAME(fnp->n_v4), tcnp->cn_namelen);
2273 }
2274 NFSUNLOCKNODE(tdnp);
2275 NFSUNLOCKNODE(fnp);
2276 if (newv4 != NULL)
2277 free(newv4, M_NFSV4NODE);
2278 }
2279
2280 if (fvp->v_type == VDIR) {
2281 if (tvp != NULL && tvp->v_type == VDIR)
2282 cache_purge(tdvp);
2283 cache_purge(fdvp);
2284 }
2285
2286 out:
2287 if (tdvp == tvp)
2288 vrele(tdvp);
2289 else
2290 vput(tdvp);
2291 if (tvp)
2292 vput(tvp);
2293 vrele(fdvp);
2294 vrele(fvp);
2295 /*
2296 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
2297 */
2298 if (error == ENOENT)
2299 error = 0;
2300 return (error);
2301 }
2302
2303 /*
2304 * nfs file rename rpc called from nfs_remove() above
2305 */
2306 static int
nfs_renameit(struct vnode * sdvp,struct vnode * svp,struct componentname * scnp,struct sillyrename * sp)2307 nfs_renameit(struct vnode *sdvp, struct vnode *svp, struct componentname *scnp,
2308 struct sillyrename *sp)
2309 {
2310
2311 return (nfs_renamerpc(sdvp, svp, scnp->cn_nameptr, scnp->cn_namelen,
2312 sdvp, NULL, sp->s_name, sp->s_namlen, true, scnp->cn_cred,
2313 curthread));
2314 }
2315
2316 /*
2317 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
2318 */
2319 static int
nfs_renamerpc(struct vnode * fdvp,struct vnode * fvp,char * fnameptr,int fnamelen,struct vnode * tdvp,struct vnode * tvp,char * tnameptr,int tnamelen,bool silly,struct ucred * cred,struct thread * td)2320 nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp, char *fnameptr,
2321 int fnamelen, struct vnode *tdvp, struct vnode *tvp, char *tnameptr,
2322 int tnamelen, bool silly, struct ucred *cred, struct thread *td)
2323 {
2324 struct nfsvattr fnfsva, tnfsva, tvpnfsva;
2325 struct nfsnode *fdnp = VTONFS(fdvp);
2326 struct nfsnode *tdnp = VTONFS(tdvp);
2327 struct nfsmount *nmp;
2328 int error = 0, fattrflag, tattrflag, tvpattrflag;
2329 nfsremove_status tvp_status;
2330
2331 nmp = VFSTONFS(fdvp->v_mount);
2332 error = nfsrpc_rename(fdvp, fvp, fnameptr, fnamelen, tdvp, tvp,
2333 tnameptr, tnamelen, &tvp_status, &fnfsva, &tnfsva, &fattrflag,
2334 &tattrflag, &tvpnfsva, &tvpattrflag, cred, td);
2335 NFSLOCKNODE(fdnp);
2336 fdnp->n_flag |= NMODIFIED;
2337 if (fattrflag != 0) {
2338 NFSUNLOCKNODE(fdnp);
2339 (void) nfscl_loadattrcache(&fdvp, &fnfsva, NULL, 0, 1);
2340 } else {
2341 fdnp->n_attrstamp = 0;
2342 NFSUNLOCKNODE(fdnp);
2343 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(fdvp);
2344 }
2345 NFSLOCKNODE(tdnp);
2346 tdnp->n_flag |= NMODIFIED;
2347 if (tattrflag != 0) {
2348 NFSUNLOCKNODE(tdnp);
2349 (void) nfscl_loadattrcache(&tdvp, &tnfsva, NULL, 0, 1);
2350 } else {
2351 tdnp->n_attrstamp = 0;
2352 NFSUNLOCKNODE(tdnp);
2353 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
2354 }
2355
2356 if (tvp != NULL) {
2357 if (NFSHASNFSV4(nmp) && NFSHASNFSV4N(nmp) &&
2358 (nmp->nm_flag & NFSMNT_NOCTO) != 0)
2359 nfs_removestatus(tvp, tvp_status, silly, td);
2360 if (!silly && tvpattrflag != 0)
2361 (void)nfscl_loadattrcache(&tvp, &tvpnfsva, NULL, 0, 1);
2362 }
2363
2364 if (error && NFS_ISV4(fdvp))
2365 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2366 return (error);
2367 }
2368
2369 /*
2370 * nfs hard link create call
2371 */
2372 static int
nfs_link(struct vop_link_args * ap)2373 nfs_link(struct vop_link_args *ap)
2374 {
2375 struct vnode *vp = ap->a_vp;
2376 struct vnode *tdvp = ap->a_tdvp;
2377 struct componentname *cnp = ap->a_cnp;
2378 struct nfsnode *np, *tdnp;
2379 struct nfsvattr nfsva, dnfsva;
2380 int error = 0, attrflag, dattrflag;
2381
2382 /*
2383 * Push all writes to the server, so that the attribute cache
2384 * doesn't get "out of sync" with the server.
2385 * XXX There should be a better way!
2386 */
2387 #ifdef notnow
2388 VOP_FSYNC(vp, MNT_WAIT, curthread);
2389 #endif
2390
2391 error = nfsrpc_link(tdvp, vp, cnp->cn_nameptr, cnp->cn_namelen,
2392 cnp->cn_cred, curthread, &dnfsva, &nfsva, &attrflag, &dattrflag);
2393 tdnp = VTONFS(tdvp);
2394 NFSLOCKNODE(tdnp);
2395 tdnp->n_flag |= NMODIFIED;
2396 if (dattrflag != 0) {
2397 NFSUNLOCKNODE(tdnp);
2398 (void) nfscl_loadattrcache(&tdvp, &dnfsva, NULL, 0, 1);
2399 } else {
2400 tdnp->n_attrstamp = 0;
2401 NFSUNLOCKNODE(tdnp);
2402 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
2403 }
2404 if (attrflag)
2405 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
2406 else {
2407 np = VTONFS(vp);
2408 NFSLOCKNODE(np);
2409 np->n_attrstamp = 0;
2410 NFSUNLOCKNODE(np);
2411 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
2412 }
2413 /*
2414 * If negative lookup caching is enabled, I might as well
2415 * add an entry for this node. Not necessary for correctness,
2416 * but if negative caching is enabled, then the system
2417 * must care about lookup caching hit rate, so...
2418 */
2419 if (VFSTONFS(vp->v_mount)->nm_negnametimeo != 0 &&
2420 (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
2421 if (tdvp != vp)
2422 cache_enter_time(tdvp, vp, cnp, &nfsva.na_ctime, NULL);
2423 else
2424 printf("nfs_link: bogus NFS server returned "
2425 "the directory as the new link\n");
2426 }
2427 if (error && NFS_ISV4(vp))
2428 error = nfscl_maperr(curthread, error, (uid_t)0,
2429 (gid_t)0);
2430 return (error);
2431 }
2432
2433 /*
2434 * nfs symbolic link create call
2435 */
2436 static int
nfs_symlink(struct vop_symlink_args * ap)2437 nfs_symlink(struct vop_symlink_args *ap)
2438 {
2439 struct vnode *dvp = ap->a_dvp;
2440 struct vattr *vap = ap->a_vap;
2441 struct componentname *cnp = ap->a_cnp;
2442 struct nfsvattr nfsva, dnfsva;
2443 struct nfsfh *nfhp;
2444 struct nfsnode *np = NULL, *dnp;
2445 struct vnode *newvp = NULL;
2446 int error = 0, attrflag, dattrflag, ret;
2447
2448 vap->va_type = VLNK;
2449 error = nfsrpc_symlink(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2450 ap->a_target, vap, cnp->cn_cred, curthread, &dnfsva,
2451 &nfsva, &nfhp, &attrflag, &dattrflag);
2452 if (nfhp) {
2453 ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, curthread,
2454 &np, LK_EXCLUSIVE);
2455 if (!ret)
2456 newvp = NFSTOV(np);
2457 else if (!error)
2458 error = ret;
2459 }
2460 if (newvp != NULL) {
2461 if (attrflag)
2462 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
2463 } else if (!error) {
2464 /*
2465 * If we do not have an error and we could not extract the
2466 * newvp from the response due to the request being NFSv2, we
2467 * have to do a lookup in order to obtain a newvp to return.
2468 */
2469 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2470 cnp->cn_cred, curthread, &np);
2471 if (!error)
2472 newvp = NFSTOV(np);
2473 }
2474 if (error) {
2475 if (newvp)
2476 vput(newvp);
2477 if (NFS_ISV4(dvp))
2478 error = nfscl_maperr(curthread, error,
2479 vap->va_uid, vap->va_gid);
2480 } else {
2481 *ap->a_vpp = newvp;
2482 }
2483
2484 dnp = VTONFS(dvp);
2485 NFSLOCKNODE(dnp);
2486 dnp->n_flag |= NMODIFIED;
2487 if (dattrflag != 0) {
2488 NFSUNLOCKNODE(dnp);
2489 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
2490 } else {
2491 dnp->n_attrstamp = 0;
2492 NFSUNLOCKNODE(dnp);
2493 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2494 }
2495 /*
2496 * If negative lookup caching is enabled, I might as well
2497 * add an entry for this node. Not necessary for correctness,
2498 * but if negative caching is enabled, then the system
2499 * must care about lookup caching hit rate, so...
2500 */
2501 if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2502 (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
2503 if (dvp != newvp)
2504 cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
2505 NULL);
2506 else
2507 printf("nfs_symlink: bogus NFS server returned "
2508 "the directory as the new file object\n");
2509 }
2510 return (error);
2511 }
2512
2513 /*
2514 * nfs make dir call
2515 */
2516 static int
nfs_mkdir(struct vop_mkdir_args * ap)2517 nfs_mkdir(struct vop_mkdir_args *ap)
2518 {
2519 struct vnode *dvp = ap->a_dvp;
2520 struct vattr *vap = ap->a_vap;
2521 struct componentname *cnp = ap->a_cnp;
2522 struct nfsnode *np = NULL, *dnp;
2523 struct vnode *newvp = NULL;
2524 struct vattr vattr;
2525 struct nfsfh *nfhp;
2526 struct nfsvattr nfsva, dnfsva;
2527 int error = 0, attrflag, dattrflag, ret;
2528
2529 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)) != 0)
2530 return (error);
2531 vap->va_type = VDIR;
2532 error = nfsrpc_mkdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2533 vap, cnp->cn_cred, curthread, &dnfsva, &nfsva, &nfhp,
2534 &attrflag, &dattrflag);
2535 dnp = VTONFS(dvp);
2536 NFSLOCKNODE(dnp);
2537 dnp->n_flag |= NMODIFIED;
2538 if (dattrflag != 0) {
2539 NFSUNLOCKNODE(dnp);
2540 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
2541 } else {
2542 dnp->n_attrstamp = 0;
2543 NFSUNLOCKNODE(dnp);
2544 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2545 }
2546 if (nfhp) {
2547 ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, curthread,
2548 &np, LK_EXCLUSIVE);
2549 if (!ret) {
2550 newvp = NFSTOV(np);
2551 if (attrflag)
2552 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
2553 0, 1);
2554 } else if (!error)
2555 error = ret;
2556 }
2557 if (!error && newvp == NULL) {
2558 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2559 cnp->cn_cred, curthread, &np);
2560 if (!error) {
2561 newvp = NFSTOV(np);
2562 if (newvp->v_type != VDIR)
2563 error = EEXIST;
2564 }
2565 }
2566 if (error) {
2567 if (newvp)
2568 vput(newvp);
2569 if (NFS_ISV4(dvp))
2570 error = nfscl_maperr(curthread, error,
2571 vap->va_uid, vap->va_gid);
2572 } else {
2573 /*
2574 * If negative lookup caching is enabled, I might as well
2575 * add an entry for this node. Not necessary for correctness,
2576 * but if negative caching is enabled, then the system
2577 * must care about lookup caching hit rate, so...
2578 */
2579 if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2580 (cnp->cn_flags & MAKEENTRY) &&
2581 attrflag != 0 && dattrflag != 0) {
2582 if (dvp != newvp)
2583 cache_enter_time(dvp, newvp, cnp,
2584 &nfsva.na_ctime, &dnfsva.na_ctime);
2585 else
2586 printf("nfs_mkdir: bogus NFS server returned "
2587 "the directory that the directory was "
2588 "created in as the new file object\n");
2589 }
2590 *ap->a_vpp = newvp;
2591 }
2592 return (error);
2593 }
2594
2595 /*
2596 * nfs remove directory call
2597 */
2598 static int
nfs_rmdir(struct vop_rmdir_args * ap)2599 nfs_rmdir(struct vop_rmdir_args *ap)
2600 {
2601 struct vnode *vp = ap->a_vp;
2602 struct vnode *dvp = ap->a_dvp;
2603 struct componentname *cnp = ap->a_cnp;
2604 struct nfsnode *dnp;
2605 struct nfsvattr dnfsva;
2606 int error, dattrflag;
2607
2608 if (dvp == vp)
2609 return (EINVAL);
2610 error = nfsrpc_rmdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2611 cnp->cn_cred, curthread, &dnfsva, &dattrflag);
2612 dnp = VTONFS(dvp);
2613 NFSLOCKNODE(dnp);
2614 dnp->n_flag |= NMODIFIED;
2615 if (dattrflag != 0) {
2616 NFSUNLOCKNODE(dnp);
2617 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
2618 } else {
2619 dnp->n_attrstamp = 0;
2620 NFSUNLOCKNODE(dnp);
2621 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2622 }
2623
2624 cache_purge(dvp);
2625 cache_purge(vp);
2626 if (error && NFS_ISV4(dvp))
2627 error = nfscl_maperr(curthread, error, (uid_t)0,
2628 (gid_t)0);
2629 /*
2630 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2631 */
2632 if (error == ENOENT)
2633 error = 0;
2634 return (error);
2635 }
2636
2637 /*
2638 * nfs readdir call
2639 */
2640 static int
nfs_readdir(struct vop_readdir_args * ap)2641 nfs_readdir(struct vop_readdir_args *ap)
2642 {
2643 struct vnode *vp = ap->a_vp;
2644 struct nfsnode *np = VTONFS(vp);
2645 struct uio *uio = ap->a_uio;
2646 ssize_t tresid, left;
2647 int error = 0;
2648 struct vattr vattr;
2649
2650 if (ap->a_eofflag != NULL)
2651 *ap->a_eofflag = 0;
2652 if (vp->v_type != VDIR)
2653 return(EPERM);
2654
2655 /*
2656 * First, check for hit on the EOF offset cache
2657 */
2658 NFSLOCKNODE(np);
2659 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2660 (np->n_flag & NMODIFIED) == 0) {
2661 NFSUNLOCKNODE(np);
2662 if (VOP_GETATTR(vp, &vattr, ap->a_cred) == 0) {
2663 NFSLOCKNODE(np);
2664 if ((NFS_ISV4(vp) && np->n_change == vattr.va_filerev) ||
2665 !NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
2666 NFSUNLOCKNODE(np);
2667 NFSINCRGLOBAL(nfsstatsv1.direofcache_hits);
2668 if (ap->a_eofflag != NULL)
2669 *ap->a_eofflag = 1;
2670 return (0);
2671 } else
2672 NFSUNLOCKNODE(np);
2673 }
2674 } else
2675 NFSUNLOCKNODE(np);
2676
2677 /*
2678 * NFS always guarantees that directory entries don't straddle
2679 * DIRBLKSIZ boundaries. As such, we need to limit the size
2680 * to an exact multiple of DIRBLKSIZ, to avoid copying a partial
2681 * directory entry.
2682 */
2683 left = uio->uio_resid % DIRBLKSIZ;
2684 if (left == uio->uio_resid)
2685 return (EINVAL);
2686 uio->uio_resid -= left;
2687
2688 /*
2689 * For readdirplus, if starting to read the directory,
2690 * purge the name cache, since it will be reloaded by
2691 * this directory read.
2692 * This removes potentially stale name cache entries.
2693 */
2694 if (uio->uio_offset == 0 &&
2695 (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_RDIRPLUS) != 0)
2696 cache_purge(vp);
2697
2698 /*
2699 * Call ncl_bioread() to do the real work.
2700 */
2701 tresid = uio->uio_resid;
2702 error = ncl_bioread(vp, uio, 0, ap->a_cred);
2703
2704 if (!error && uio->uio_resid == tresid) {
2705 NFSINCRGLOBAL(nfsstatsv1.direofcache_misses);
2706 if (ap->a_eofflag != NULL)
2707 *ap->a_eofflag = 1;
2708 }
2709
2710 /* Add the partial DIRBLKSIZ (left) back in. */
2711 uio->uio_resid += left;
2712 return (error);
2713 }
2714
2715 /*
2716 * Readdir rpc call.
2717 * Called from below the buffer cache by ncl_doio().
2718 */
2719 int
ncl_readdirrpc(struct vnode * vp,struct uio * uiop,struct ucred * cred,struct thread * td)2720 ncl_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2721 struct thread *td)
2722 {
2723 struct nfsvattr nfsva;
2724 nfsuint64 *cookiep, cookie;
2725 struct nfsnode *dnp = VTONFS(vp);
2726 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2727 int error = 0, eof, attrflag;
2728
2729 KASSERT(uiop->uio_iovcnt == 1 &&
2730 (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2731 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2732 ("nfs readdirrpc bad uio"));
2733
2734 /*
2735 * If there is no cookie, assume directory was stale.
2736 */
2737 ncl_dircookie_lock(dnp);
2738 NFSUNLOCKNODE(dnp);
2739 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2740 if (cookiep) {
2741 cookie = *cookiep;
2742 ncl_dircookie_unlock(dnp);
2743 } else {
2744 ncl_dircookie_unlock(dnp);
2745 return (NFSERR_BAD_COOKIE);
2746 }
2747
2748 if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2749 (void)ncl_fsinfo(nmp, vp, cred, td);
2750
2751 error = nfsrpc_readdir(vp, uiop, &cookie, cred, td, &nfsva,
2752 &attrflag, &eof);
2753 if (attrflag)
2754 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
2755
2756 if (!error) {
2757 /*
2758 * We are now either at the end of the directory or have filled
2759 * the block.
2760 */
2761 if (eof) {
2762 NFSLOCKNODE(dnp);
2763 dnp->n_direofoffset = uiop->uio_offset;
2764 NFSUNLOCKNODE(dnp);
2765 } else {
2766 if (uiop->uio_resid > 0)
2767 printf("EEK! readdirrpc resid > 0\n");
2768 ncl_dircookie_lock(dnp);
2769 NFSUNLOCKNODE(dnp);
2770 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2771 *cookiep = cookie;
2772 ncl_dircookie_unlock(dnp);
2773 }
2774 } else if (NFS_ISV4(vp)) {
2775 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2776 }
2777 return (error);
2778 }
2779
2780 /*
2781 * NFS V3 readdir plus RPC. Used in place of ncl_readdirrpc().
2782 */
2783 int
ncl_readdirplusrpc(struct vnode * vp,struct uio * uiop,struct ucred * cred,struct thread * td)2784 ncl_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2785 struct thread *td)
2786 {
2787 struct nfsvattr nfsva;
2788 nfsuint64 *cookiep, cookie;
2789 struct nfsnode *dnp = VTONFS(vp);
2790 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2791 int error = 0, attrflag, eof;
2792
2793 KASSERT(uiop->uio_iovcnt == 1 &&
2794 (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2795 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2796 ("nfs readdirplusrpc bad uio"));
2797
2798 /*
2799 * If there is no cookie, assume directory was stale.
2800 */
2801 ncl_dircookie_lock(dnp);
2802 NFSUNLOCKNODE(dnp);
2803 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2804 if (cookiep) {
2805 cookie = *cookiep;
2806 ncl_dircookie_unlock(dnp);
2807 } else {
2808 ncl_dircookie_unlock(dnp);
2809 return (NFSERR_BAD_COOKIE);
2810 }
2811
2812 if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2813 (void)ncl_fsinfo(nmp, vp, cred, td);
2814 error = nfsrpc_readdirplus(vp, uiop, &cookie, cred, td, &nfsva,
2815 &attrflag, &eof);
2816 if (attrflag)
2817 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
2818
2819 if (!error) {
2820 /*
2821 * We are now either at end of the directory or have filled the
2822 * the block.
2823 */
2824 if (eof) {
2825 NFSLOCKNODE(dnp);
2826 dnp->n_direofoffset = uiop->uio_offset;
2827 NFSUNLOCKNODE(dnp);
2828 } else {
2829 if (uiop->uio_resid > 0)
2830 printf("EEK! readdirplusrpc resid > 0\n");
2831 ncl_dircookie_lock(dnp);
2832 NFSUNLOCKNODE(dnp);
2833 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2834 *cookiep = cookie;
2835 ncl_dircookie_unlock(dnp);
2836 }
2837 } else if (NFS_ISV4(vp)) {
2838 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2839 }
2840 return (error);
2841 }
2842
2843 /*
2844 * Silly rename. To make the NFS filesystem that is stateless look a little
2845 * more like the "ufs" a remove of an active vnode is translated to a rename
2846 * to a funny looking filename that is removed by nfs_inactive on the
2847 * nfsnode. There is the potential for another process on a different client
2848 * to create the same funny name between the nfs_lookitup() fails and the
2849 * nfs_rename() completes, but...
2850 */
2851 static int
nfs_sillyrename(struct vnode * dvp,struct vnode * vp,struct componentname * cnp)2852 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2853 {
2854 struct sillyrename *sp;
2855 struct nfsnode *np;
2856 int error;
2857 short pid;
2858 unsigned int lticks;
2859
2860 cache_purge(dvp);
2861 np = VTONFS(vp);
2862 KASSERT(vp->v_type != VDIR, ("nfs: sillyrename dir"));
2863 sp = malloc(sizeof (struct sillyrename),
2864 M_NEWNFSREQ, M_WAITOK);
2865 sp->s_cred = crhold(cnp->cn_cred);
2866 sp->s_dvp = dvp;
2867 VREF(dvp);
2868
2869 /*
2870 * Fudge together a funny name.
2871 * Changing the format of the funny name to accommodate more
2872 * sillynames per directory.
2873 * The name is now changed to .nfs.<ticks>.<pid>.4, where ticks is
2874 * CPU ticks since boot.
2875 */
2876 pid = curthread->td_proc->p_pid;
2877 lticks = (unsigned int)ticks;
2878 for ( ; ; ) {
2879 sp->s_namlen = sprintf(sp->s_name,
2880 ".nfs.%08x.%04x4.4", lticks,
2881 pid);
2882 if (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2883 curthread, NULL))
2884 break;
2885 lticks++;
2886 }
2887 error = nfs_renameit(dvp, vp, cnp, sp);
2888 if (error)
2889 goto bad;
2890 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2891 curthread, &np);
2892 np->n_sillyrename = sp;
2893 return (0);
2894 bad:
2895 vrele(sp->s_dvp);
2896 crfree(sp->s_cred);
2897 free(sp, M_NEWNFSREQ);
2898 return (error);
2899 }
2900
2901 /*
2902 * Look up a file name and optionally either update the file handle or
2903 * allocate an nfsnode, depending on the value of npp.
2904 * npp == NULL --> just do the lookup
2905 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2906 * handled too
2907 * *npp != NULL --> update the file handle in the vnode
2908 */
2909 static int
nfs_lookitup(struct vnode * dvp,char * name,int len,struct ucred * cred,struct thread * td,struct nfsnode ** npp)2910 nfs_lookitup(struct vnode *dvp, char *name, int len, struct ucred *cred,
2911 struct thread *td, struct nfsnode **npp)
2912 {
2913 struct vnode *newvp = NULL, *vp;
2914 struct nfsnode *np, *dnp = VTONFS(dvp);
2915 struct nfsfh *nfhp, *onfhp;
2916 struct nfsvattr nfsva, dnfsva;
2917 struct componentname cn;
2918 int error = 0, attrflag, dattrflag;
2919 u_int hash;
2920 struct timespec ts;
2921
2922 nanouptime(&ts);
2923 error = nfsrpc_lookup(dvp, name, len, cred, td, &dnfsva, &nfsva,
2924 &nfhp, &attrflag, &dattrflag, 0);
2925 if (dattrflag)
2926 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
2927 if (npp && !error) {
2928 if (*npp != NULL) {
2929 np = *npp;
2930 vp = NFSTOV(np);
2931 /*
2932 * For NFSv4, check to see if it is the same name and
2933 * replace the name, if it is different.
2934 */
2935 if (np->n_v4 != NULL && nfsva.na_type == VREG &&
2936 (np->n_v4->n4_namelen != len ||
2937 NFSBCMP(name, NFS4NODENAME(np->n_v4), len) ||
2938 dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
2939 NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2940 dnp->n_fhp->nfh_len))) {
2941 free(np->n_v4, M_NFSV4NODE);
2942 np->n_v4 = malloc(
2943 sizeof (struct nfsv4node) +
2944 dnp->n_fhp->nfh_len + len - 1,
2945 M_NFSV4NODE, M_WAITOK);
2946 np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
2947 np->n_v4->n4_namelen = len;
2948 NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2949 dnp->n_fhp->nfh_len);
2950 NFSBCOPY(name, NFS4NODENAME(np->n_v4), len);
2951 }
2952 hash = fnv_32_buf(nfhp->nfh_fh, nfhp->nfh_len,
2953 FNV1_32_INIT);
2954 onfhp = np->n_fhp;
2955 /*
2956 * Rehash node for new file handle.
2957 */
2958 vfs_hash_rehash(vp, hash);
2959 np->n_fhp = nfhp;
2960 if (onfhp != NULL)
2961 free(onfhp, M_NFSFH);
2962 newvp = NFSTOV(np);
2963 } else if (NFS_CMPFH(dnp, nfhp->nfh_fh, nfhp->nfh_len)) {
2964 free(nfhp, M_NFSFH);
2965 VREF(dvp);
2966 newvp = dvp;
2967 } else {
2968 cn.cn_nameptr = name;
2969 cn.cn_namelen = len;
2970 error = nfscl_nget(dvp->v_mount, dvp, nfhp, &cn, td,
2971 &np, LK_EXCLUSIVE);
2972 if (error)
2973 return (error);
2974 newvp = NFSTOV(np);
2975 /*
2976 * If n_localmodtime >= time before RPC, then
2977 * a file modification operation, such as
2978 * VOP_SETATTR() of size, has occurred while
2979 * the Lookup RPC and acquisition of the vnode
2980 * happened. As such, the attributes might
2981 * be stale, with possibly an incorrect size.
2982 */
2983 NFSLOCKNODE(np);
2984 if (timespecisset(&np->n_localmodtime) &&
2985 timespeccmp(&np->n_localmodtime, &ts, >=)) {
2986 NFSCL_DEBUG(4, "nfs_lookitup: localmod "
2987 "stale attributes\n");
2988 attrflag = 0;
2989 }
2990 NFSUNLOCKNODE(np);
2991 }
2992 if (!attrflag && *npp == NULL) {
2993 if (newvp == dvp)
2994 vrele(newvp);
2995 else
2996 vput(newvp);
2997 return (ENOENT);
2998 }
2999 if (attrflag)
3000 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
3001 }
3002 if (npp && *npp == NULL) {
3003 if (error) {
3004 if (newvp) {
3005 if (newvp == dvp)
3006 vrele(newvp);
3007 else
3008 vput(newvp);
3009 }
3010 } else
3011 *npp = np;
3012 }
3013 if (error && NFS_ISV4(dvp))
3014 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
3015 return (error);
3016 }
3017
3018 /*
3019 * Nfs Version 3 and 4 commit rpc
3020 */
3021 int
ncl_commit(struct vnode * vp,u_quad_t offset,int cnt,struct ucred * cred,struct thread * td)3022 ncl_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
3023 struct thread *td)
3024 {
3025 struct nfsvattr nfsva;
3026 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3027 struct nfsnode *np;
3028 struct uio uio;
3029 int error, attrflag;
3030
3031 np = VTONFS(vp);
3032 error = EIO;
3033 attrflag = 0;
3034 if (NFSHASPNFS(nmp) && (np->n_flag & NDSCOMMIT) != 0) {
3035 uio.uio_offset = offset;
3036 uio.uio_resid = cnt;
3037 error = nfscl_doiods(vp, &uio, NULL, NULL,
3038 NFSV4OPEN_ACCESSWRITE, 1, cred, td);
3039 if (error != 0) {
3040 NFSLOCKNODE(np);
3041 np->n_flag &= ~NDSCOMMIT;
3042 NFSUNLOCKNODE(np);
3043 }
3044 }
3045 if (error != 0) {
3046 mtx_lock(&nmp->nm_mtx);
3047 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
3048 mtx_unlock(&nmp->nm_mtx);
3049 return (0);
3050 }
3051 mtx_unlock(&nmp->nm_mtx);
3052 error = nfsrpc_commit(vp, offset, cnt, cred, td, &nfsva,
3053 &attrflag);
3054 }
3055 if (attrflag != 0)
3056 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
3057 if (error != 0 && NFS_ISV4(vp))
3058 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
3059 return (error);
3060 }
3061
3062 /*
3063 * Strategy routine.
3064 * For async requests when nfsiod(s) are running, queue the request by
3065 * calling ncl_asyncio(), otherwise just all ncl_doio() to do the
3066 * request.
3067 */
3068 static int
nfs_strategy(struct vop_strategy_args * ap)3069 nfs_strategy(struct vop_strategy_args *ap)
3070 {
3071 struct buf *bp;
3072 struct vnode *vp;
3073 struct ucred *cr;
3074
3075 bp = ap->a_bp;
3076 vp = ap->a_vp;
3077 KASSERT(bp->b_vp == vp, ("missing b_getvp"));
3078 KASSERT(!(bp->b_flags & B_DONE),
3079 ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
3080
3081 if (vp->v_type == VREG && bp->b_blkno == bp->b_lblkno)
3082 bp->b_blkno = bp->b_lblkno * (vp->v_bufobj.bo_bsize /
3083 DEV_BSIZE);
3084 if (bp->b_iocmd == BIO_READ)
3085 cr = bp->b_rcred;
3086 else
3087 cr = bp->b_wcred;
3088
3089 /*
3090 * If the op is asynchronous and an i/o daemon is waiting
3091 * queue the request, wake it up and wait for completion
3092 * otherwise just do it ourselves.
3093 */
3094 if ((bp->b_flags & B_ASYNC) == 0 ||
3095 ncl_asyncio(VFSTONFS(vp->v_mount), bp, NOCRED, curthread))
3096 (void) ncl_doio(vp, bp, cr, curthread, 1);
3097 return (0);
3098 }
3099
3100 /*
3101 * fsync vnode op. Just call ncl_flush() with commit == 1.
3102 */
3103 /* ARGSUSED */
3104 static int
nfs_fsync(struct vop_fsync_args * ap)3105 nfs_fsync(struct vop_fsync_args *ap)
3106 {
3107
3108 if (ap->a_vp->v_type != VREG) {
3109 /*
3110 * For NFS, metadata is changed synchronously on the server,
3111 * so there is nothing to flush. Also, ncl_flush() clears
3112 * the NMODIFIED flag and that shouldn't be done here for
3113 * directories.
3114 */
3115 return (0);
3116 }
3117 return (ncl_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1, 0));
3118 }
3119
3120 /*
3121 * Flush all the blocks associated with a vnode.
3122 * Walk through the buffer pool and push any dirty pages
3123 * associated with the vnode.
3124 * If the called_from_renewthread argument is TRUE, it has been called
3125 * from the NFSv4 renew thread and, as such, cannot block indefinitely
3126 * waiting for a buffer write to complete.
3127 */
3128 int
ncl_flush(struct vnode * vp,int waitfor,struct thread * td,int commit,int called_from_renewthread)3129 ncl_flush(struct vnode *vp, int waitfor, struct thread *td,
3130 int commit, int called_from_renewthread)
3131 {
3132 struct nfsnode *np = VTONFS(vp);
3133 struct buf *bp;
3134 int i;
3135 struct buf *nbp;
3136 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3137 int error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
3138 int passone = 1, trycnt = 0;
3139 u_quad_t off, endoff, toff;
3140 struct ucred* wcred = NULL;
3141 struct buf **bvec = NULL;
3142 struct bufobj *bo;
3143 #ifndef NFS_COMMITBVECSIZ
3144 #define NFS_COMMITBVECSIZ 20
3145 #endif
3146 struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
3147 u_int bvecsize = 0, bveccount;
3148 struct timespec ts;
3149
3150 if (called_from_renewthread != 0)
3151 slptimeo = hz;
3152 if (nmp->nm_flag & NFSMNT_INT)
3153 slpflag = PCATCH;
3154 if (!commit)
3155 passone = 0;
3156 bo = &vp->v_bufobj;
3157 /*
3158 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
3159 * server, but has not been committed to stable storage on the server
3160 * yet. On the first pass, the byte range is worked out and the commit
3161 * rpc is done. On the second pass, bwrite() is called to do the
3162 * job.
3163 */
3164 again:
3165 off = (u_quad_t)-1;
3166 endoff = 0;
3167 bvecpos = 0;
3168 if (NFS_ISV34(vp) && commit) {
3169 if (bvec != NULL && bvec != bvec_on_stack)
3170 free(bvec, M_TEMP);
3171 /*
3172 * Count up how many buffers waiting for a commit.
3173 */
3174 bveccount = 0;
3175 BO_LOCK(bo);
3176 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
3177 if (!BUF_ISLOCKED(bp) &&
3178 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
3179 == (B_DELWRI | B_NEEDCOMMIT))
3180 bveccount++;
3181 }
3182 /*
3183 * Allocate space to remember the list of bufs to commit. It is
3184 * important to use M_NOWAIT here to avoid a race with nfs_write.
3185 * If we can't get memory (for whatever reason), we will end up
3186 * committing the buffers one-by-one in the loop below.
3187 */
3188 if (bveccount > NFS_COMMITBVECSIZ) {
3189 /*
3190 * Release the vnode interlock to avoid a lock
3191 * order reversal.
3192 */
3193 BO_UNLOCK(bo);
3194 bvec = (struct buf **)
3195 malloc(bveccount * sizeof(struct buf *),
3196 M_TEMP, M_NOWAIT);
3197 BO_LOCK(bo);
3198 if (bvec == NULL) {
3199 bvec = bvec_on_stack;
3200 bvecsize = NFS_COMMITBVECSIZ;
3201 } else
3202 bvecsize = bveccount;
3203 } else {
3204 bvec = bvec_on_stack;
3205 bvecsize = NFS_COMMITBVECSIZ;
3206 }
3207 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
3208 if (bvecpos >= bvecsize)
3209 break;
3210 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
3211 nbp = TAILQ_NEXT(bp, b_bobufs);
3212 continue;
3213 }
3214 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3215 (B_DELWRI | B_NEEDCOMMIT)) {
3216 BUF_UNLOCK(bp);
3217 nbp = TAILQ_NEXT(bp, b_bobufs);
3218 continue;
3219 }
3220 BO_UNLOCK(bo);
3221 bremfree(bp);
3222 /*
3223 * Work out if all buffers are using the same cred
3224 * so we can deal with them all with one commit.
3225 *
3226 * NOTE: we are not clearing B_DONE here, so we have
3227 * to do it later on in this routine if we intend to
3228 * initiate I/O on the bp.
3229 *
3230 * Note: to avoid loopback deadlocks, we do not
3231 * assign b_runningbufspace.
3232 */
3233 if (wcred == NULL)
3234 wcred = bp->b_wcred;
3235 else if (wcred != bp->b_wcred)
3236 wcred = NOCRED;
3237 vfs_busy_pages(bp, 0);
3238
3239 BO_LOCK(bo);
3240 /*
3241 * bp is protected by being locked, but nbp is not
3242 * and vfs_busy_pages() may sleep. We have to
3243 * recalculate nbp.
3244 */
3245 nbp = TAILQ_NEXT(bp, b_bobufs);
3246
3247 /*
3248 * A list of these buffers is kept so that the
3249 * second loop knows which buffers have actually
3250 * been committed. This is necessary, since there
3251 * may be a race between the commit rpc and new
3252 * uncommitted writes on the file.
3253 */
3254 bvec[bvecpos++] = bp;
3255 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
3256 bp->b_dirtyoff;
3257 if (toff < off)
3258 off = toff;
3259 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
3260 if (toff > endoff)
3261 endoff = toff;
3262 }
3263 BO_UNLOCK(bo);
3264 }
3265 if (bvecpos > 0) {
3266 /*
3267 * Commit data on the server, as required.
3268 * If all bufs are using the same wcred, then use that with
3269 * one call for all of them, otherwise commit each one
3270 * separately.
3271 */
3272 if (wcred != NOCRED)
3273 retv = ncl_commit(vp, off, (int)(endoff - off),
3274 wcred, td);
3275 else {
3276 retv = 0;
3277 for (i = 0; i < bvecpos; i++) {
3278 off_t off, size;
3279 bp = bvec[i];
3280 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
3281 bp->b_dirtyoff;
3282 size = (u_quad_t)(bp->b_dirtyend
3283 - bp->b_dirtyoff);
3284 retv = ncl_commit(vp, off, (int)size,
3285 bp->b_wcred, td);
3286 if (retv) break;
3287 }
3288 }
3289
3290 if (retv == NFSERR_STALEWRITEVERF)
3291 ncl_clearcommit(vp->v_mount);
3292
3293 /*
3294 * Now, either mark the blocks I/O done or mark the
3295 * blocks dirty, depending on whether the commit
3296 * succeeded.
3297 */
3298 for (i = 0; i < bvecpos; i++) {
3299 bp = bvec[i];
3300 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3301 if (!NFSCL_FORCEDISM(vp->v_mount) && retv) {
3302 /*
3303 * Error, leave B_DELWRI intact
3304 */
3305 vfs_unbusy_pages(bp);
3306 brelse(bp);
3307 } else {
3308 /*
3309 * Success, remove B_DELWRI ( bundirty() ).
3310 *
3311 * b_dirtyoff/b_dirtyend seem to be NFS
3312 * specific. We should probably move that
3313 * into bundirty(). XXX
3314 */
3315 bufobj_wref(bo);
3316 bp->b_flags |= B_ASYNC;
3317 bundirty(bp);
3318 bp->b_flags &= ~B_DONE;
3319 bp->b_ioflags &= ~BIO_ERROR;
3320 bp->b_dirtyoff = bp->b_dirtyend = 0;
3321 bufdone(bp);
3322 }
3323 }
3324 }
3325
3326 /*
3327 * Start/do any write(s) that are required.
3328 */
3329 loop:
3330 BO_LOCK(bo);
3331 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
3332 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
3333 if (waitfor != MNT_WAIT || passone)
3334 continue;
3335
3336 error = BUF_TIMELOCK(bp,
3337 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
3338 BO_LOCKPTR(bo), "nfsfsync", slpflag, slptimeo);
3339 if (error == 0) {
3340 BUF_UNLOCK(bp);
3341 goto loop;
3342 }
3343 if (error == ENOLCK) {
3344 error = 0;
3345 goto loop;
3346 }
3347 if (called_from_renewthread != 0) {
3348 /*
3349 * Return EIO so the flush will be retried
3350 * later.
3351 */
3352 error = EIO;
3353 goto done;
3354 }
3355 if (newnfs_sigintr(nmp, td)) {
3356 error = EINTR;
3357 goto done;
3358 }
3359 if (slpflag == PCATCH) {
3360 slpflag = 0;
3361 slptimeo = 2 * hz;
3362 }
3363 goto loop;
3364 }
3365 if ((bp->b_flags & B_DELWRI) == 0)
3366 panic("nfs_fsync: not dirty");
3367 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
3368 BUF_UNLOCK(bp);
3369 continue;
3370 }
3371 BO_UNLOCK(bo);
3372 bremfree(bp);
3373 bp->b_flags |= B_ASYNC;
3374 bwrite(bp);
3375 if (newnfs_sigintr(nmp, td)) {
3376 error = EINTR;
3377 goto done;
3378 }
3379 goto loop;
3380 }
3381 if (passone) {
3382 passone = 0;
3383 BO_UNLOCK(bo);
3384 goto again;
3385 }
3386 if (waitfor == MNT_WAIT) {
3387 while (bo->bo_numoutput) {
3388 error = bufobj_wwait(bo, slpflag, slptimeo);
3389 if (error) {
3390 BO_UNLOCK(bo);
3391 if (called_from_renewthread != 0) {
3392 /*
3393 * Return EIO so that the flush will be
3394 * retried later.
3395 */
3396 error = EIO;
3397 goto done;
3398 }
3399 error = newnfs_sigintr(nmp, td);
3400 if (error)
3401 goto done;
3402 if (slpflag == PCATCH) {
3403 slpflag = 0;
3404 slptimeo = 2 * hz;
3405 }
3406 BO_LOCK(bo);
3407 }
3408 }
3409 if (bo->bo_dirty.bv_cnt != 0 && commit) {
3410 BO_UNLOCK(bo);
3411 goto loop;
3412 }
3413 /*
3414 * Wait for all the async IO requests to drain
3415 */
3416 BO_UNLOCK(bo);
3417 } else
3418 BO_UNLOCK(bo);
3419 if (NFSHASPNFS(nmp)) {
3420 nfscl_layoutcommit(vp, td);
3421 /*
3422 * Invalidate the attribute cache, since writes to a DS
3423 * won't update the size attribute.
3424 */
3425 NFSLOCKNODE(np);
3426 np->n_attrstamp = 0;
3427 } else
3428 NFSLOCKNODE(np);
3429 if (np->n_flag & NWRITEERR) {
3430 error = np->n_error;
3431 np->n_flag &= ~NWRITEERR;
3432 }
3433 if (commit && bo->bo_dirty.bv_cnt == 0 &&
3434 bo->bo_numoutput == 0)
3435 np->n_flag &= ~NMODIFIED;
3436 NFSUNLOCKNODE(np);
3437 done:
3438 if (bvec != NULL && bvec != bvec_on_stack)
3439 free(bvec, M_TEMP);
3440 if (error == 0 && commit != 0 && waitfor == MNT_WAIT &&
3441 (bo->bo_dirty.bv_cnt != 0 || bo->bo_numoutput != 0)) {
3442 if (trycnt++ < 5) {
3443 /* try, try again... */
3444 passone = 1;
3445 wcred = NULL;
3446 bvec = NULL;
3447 bvecsize = 0;
3448 goto again;
3449 }
3450 vn_printf(vp, "ncl_flush failed");
3451 error = called_from_renewthread != 0 ? EIO : EBUSY;
3452 }
3453 if (error == 0) {
3454 nanouptime(&ts);
3455 NFSLOCKNODE(np);
3456 np->n_localmodtime = ts;
3457 NFSUNLOCKNODE(np);
3458 }
3459 return (error);
3460 }
3461
3462 /*
3463 * NFS advisory byte-level locks.
3464 */
3465 static int
nfs_advlock(struct vop_advlock_args * ap)3466 nfs_advlock(struct vop_advlock_args *ap)
3467 {
3468 struct vnode *vp = ap->a_vp;
3469 struct ucred *cred;
3470 struct nfsnode *np = VTONFS(ap->a_vp);
3471 struct proc *p = (struct proc *)ap->a_id;
3472 struct thread *td = curthread; /* XXX */
3473 struct vattr va;
3474 int ret, error;
3475 u_quad_t size;
3476 struct nfsmount *nmp;
3477
3478 error = NFSVOPLOCK(vp, LK_SHARED);
3479 if (error != 0)
3480 return (EBADF);
3481 nmp = VFSTONFS(vp->v_mount);
3482 if (!NFS_ISV4(vp) || (nmp->nm_flag & NFSMNT_NOLOCKD) != 0) {
3483 if ((nmp->nm_flag & NFSMNT_NOLOCKD) != 0) {
3484 size = np->n_size;
3485 NFSVOPUNLOCK(vp);
3486 error = lf_advlock(ap, &(vp->v_lockf), size);
3487 } else {
3488 if (nfs_advlock_p != NULL)
3489 error = nfs_advlock_p(ap);
3490 else {
3491 NFSVOPUNLOCK(vp);
3492 error = ENOLCK;
3493 }
3494 }
3495 if (error == 0 && ap->a_op == F_SETLK) {
3496 error = NFSVOPLOCK(vp, LK_SHARED);
3497 if (error == 0) {
3498 /* Mark that a file lock has been acquired. */
3499 NFSLOCKNODE(np);
3500 np->n_flag |= NHASBEENLOCKED;
3501 NFSUNLOCKNODE(np);
3502 NFSVOPUNLOCK(vp);
3503 }
3504 }
3505 return (error);
3506 } else if ((ap->a_flags & (F_POSIX | F_FLOCK)) != 0) {
3507 if (vp->v_type != VREG) {
3508 error = EINVAL;
3509 goto out;
3510 }
3511 if ((ap->a_flags & F_POSIX) != 0)
3512 cred = p->p_ucred;
3513 else
3514 cred = td->td_ucred;
3515 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
3516 if (VN_IS_DOOMED(vp)) {
3517 error = EBADF;
3518 goto out;
3519 }
3520
3521 /*
3522 * If this is unlocking a write locked region, flush and
3523 * commit them before unlocking. This is required by
3524 * RFC3530 Sec. 9.3.2.
3525 */
3526 if (ap->a_op == F_UNLCK &&
3527 nfscl_checkwritelocked(vp, ap->a_fl, cred, td, ap->a_id,
3528 ap->a_flags))
3529 (void) ncl_flush(vp, MNT_WAIT, td, 1, 0);
3530
3531 /*
3532 * Mark NFS node as might have acquired a lock.
3533 * This is separate from NHASBEENLOCKED, because it must
3534 * be done before the nfsrpc_advlock() call, which might
3535 * add a nfscllock structure to the client state.
3536 * It is used to check for the case where a nfscllock
3537 * state structure cannot exist for the file.
3538 * Only done for "oneopenown" NFSv4.1/4.2 mounts.
3539 */
3540 if (NFSHASNFSV4N(nmp) && NFSHASONEOPENOWN(nmp)) {
3541 NFSLOCKNODE(np);
3542 np->n_flag |= NMIGHTBELOCKED;
3543 NFSUNLOCKNODE(np);
3544 }
3545
3546 /*
3547 * Loop around doing the lock op, while a blocking lock
3548 * must wait for the lock op to succeed.
3549 */
3550 do {
3551 ret = nfsrpc_advlock(vp, np->n_size, ap->a_op,
3552 ap->a_fl, 0, cred, td, ap->a_id, ap->a_flags);
3553 if (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3554 ap->a_op == F_SETLK) {
3555 NFSVOPUNLOCK(vp);
3556 error = nfs_catnap(PZERO | PCATCH, ret,
3557 "ncladvl");
3558 if (error)
3559 return (EINTR);
3560 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3561 if (VN_IS_DOOMED(vp)) {
3562 error = EBADF;
3563 goto out;
3564 }
3565 }
3566 } while (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3567 ap->a_op == F_SETLK);
3568 if (ret == NFSERR_DENIED) {
3569 error = EAGAIN;
3570 goto out;
3571 } else if (ret == EINVAL || ret == EBADF || ret == EINTR) {
3572 error = ret;
3573 goto out;
3574 } else if (ret != 0) {
3575 error = EACCES;
3576 goto out;
3577 }
3578
3579 /*
3580 * Now, if we just got a lock, invalidate data in the buffer
3581 * cache, as required, so that the coherency conforms with
3582 * RFC3530 Sec. 9.3.2.
3583 */
3584 if (ap->a_op == F_SETLK) {
3585 if ((np->n_flag & NMODIFIED) == 0) {
3586 np->n_attrstamp = 0;
3587 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3588 ret = VOP_GETATTR(vp, &va, cred);
3589 }
3590 if ((np->n_flag & NMODIFIED) || ret ||
3591 np->n_change != va.va_filerev) {
3592 (void) ncl_vinvalbuf(vp, V_SAVE, td, 1);
3593 np->n_attrstamp = 0;
3594 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3595 ret = VOP_GETATTR(vp, &va, cred);
3596 if (!ret) {
3597 np->n_mtime = va.va_mtime;
3598 np->n_change = va.va_filerev;
3599 }
3600 }
3601 /* Mark that a file lock has been acquired. */
3602 NFSLOCKNODE(np);
3603 np->n_flag |= NHASBEENLOCKED;
3604 NFSUNLOCKNODE(np);
3605 }
3606 } else
3607 error = EOPNOTSUPP;
3608 out:
3609 NFSVOPUNLOCK(vp);
3610 return (error);
3611 }
3612
3613 /*
3614 * NFS advisory byte-level locks.
3615 */
3616 static int
nfs_advlockasync(struct vop_advlockasync_args * ap)3617 nfs_advlockasync(struct vop_advlockasync_args *ap)
3618 {
3619 struct vnode *vp = ap->a_vp;
3620 u_quad_t size;
3621 int error;
3622
3623 error = NFSVOPLOCK(vp, LK_SHARED);
3624 if (error)
3625 return (error);
3626 if (NFS_ISV4(vp)) {
3627 NFSVOPUNLOCK(vp);
3628 return (EOPNOTSUPP);
3629 }
3630 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3631 size = VTONFS(vp)->n_size;
3632 NFSVOPUNLOCK(vp);
3633 error = lf_advlockasync(ap, &(vp->v_lockf), size);
3634 } else {
3635 NFSVOPUNLOCK(vp);
3636 error = EOPNOTSUPP;
3637 }
3638 return (error);
3639 }
3640
3641 /*
3642 * Print out the contents of an nfsnode.
3643 */
3644 static int
nfs_print(struct vop_print_args * ap)3645 nfs_print(struct vop_print_args *ap)
3646 {
3647 struct vnode *vp = ap->a_vp;
3648 struct nfsnode *np = VTONFS(vp);
3649
3650 printf("\tfileid %jd fsid 0x%jx", (uintmax_t)np->n_vattr.na_fileid,
3651 (uintmax_t)np->n_vattr.na_fsid);
3652 if (vp->v_type == VFIFO)
3653 fifo_printinfo(vp);
3654 printf("\n");
3655 return (0);
3656 }
3657
3658 /*
3659 * nfs special file access vnode op.
3660 * Essentially just get vattr and then imitate iaccess() since the device is
3661 * local to the client.
3662 */
3663 static int
nfsspec_access(struct vop_access_args * ap)3664 nfsspec_access(struct vop_access_args *ap)
3665 {
3666 struct vattr *vap;
3667 struct ucred *cred = ap->a_cred;
3668 struct vnode *vp = ap->a_vp;
3669 accmode_t accmode = ap->a_accmode;
3670 struct vattr vattr;
3671 int error;
3672
3673 /*
3674 * Disallow write attempts on filesystems mounted read-only;
3675 * unless the file is a socket, fifo, or a block or character
3676 * device resident on the filesystem.
3677 */
3678 if ((accmode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3679 switch (vp->v_type) {
3680 case VREG:
3681 case VDIR:
3682 case VLNK:
3683 return (EROFS);
3684 default:
3685 break;
3686 }
3687 }
3688 vap = &vattr;
3689 error = VOP_GETATTR(vp, vap, cred);
3690 if (error)
3691 goto out;
3692 error = vaccess(vp->v_type, vap->va_mode, vap->va_uid, vap->va_gid,
3693 accmode, cred);
3694 out:
3695 return error;
3696 }
3697
3698 /*
3699 * Read wrapper for fifos.
3700 */
3701 static int
nfsfifo_read(struct vop_read_args * ap)3702 nfsfifo_read(struct vop_read_args *ap)
3703 {
3704 struct nfsnode *np = VTONFS(ap->a_vp);
3705 int error;
3706
3707 /*
3708 * Set access flag.
3709 */
3710 NFSLOCKNODE(np);
3711 np->n_flag |= NACC;
3712 vfs_timestamp(&np->n_atim);
3713 NFSUNLOCKNODE(np);
3714 error = fifo_specops.vop_read(ap);
3715 return error;
3716 }
3717
3718 /*
3719 * Write wrapper for fifos.
3720 */
3721 static int
nfsfifo_write(struct vop_write_args * ap)3722 nfsfifo_write(struct vop_write_args *ap)
3723 {
3724 struct nfsnode *np = VTONFS(ap->a_vp);
3725
3726 /*
3727 * Set update flag.
3728 */
3729 NFSLOCKNODE(np);
3730 np->n_flag |= NUPD;
3731 vfs_timestamp(&np->n_mtim);
3732 NFSUNLOCKNODE(np);
3733 return(fifo_specops.vop_write(ap));
3734 }
3735
3736 /*
3737 * Close wrapper for fifos.
3738 *
3739 * Update the times on the nfsnode then do fifo close.
3740 */
3741 static int
nfsfifo_close(struct vop_close_args * ap)3742 nfsfifo_close(struct vop_close_args *ap)
3743 {
3744 struct vnode *vp = ap->a_vp;
3745 struct nfsnode *np = VTONFS(vp);
3746 struct vattr vattr;
3747 struct timespec ts;
3748
3749 NFSLOCKNODE(np);
3750 if (np->n_flag & (NACC | NUPD)) {
3751 vfs_timestamp(&ts);
3752 if (np->n_flag & NACC)
3753 np->n_atim = ts;
3754 if (np->n_flag & NUPD)
3755 np->n_mtim = ts;
3756 np->n_flag |= NCHG;
3757 if (vrefcnt(vp) == 1 &&
3758 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3759 VATTR_NULL(&vattr);
3760 if (np->n_flag & NACC)
3761 vattr.va_atime = np->n_atim;
3762 if (np->n_flag & NUPD)
3763 vattr.va_mtime = np->n_mtim;
3764 NFSUNLOCKNODE(np);
3765 (void)VOP_SETATTR(vp, &vattr, ap->a_cred);
3766 goto out;
3767 }
3768 }
3769 NFSUNLOCKNODE(np);
3770 out:
3771 return (fifo_specops.vop_close(ap));
3772 }
3773
3774 static int
nfs_getacl(struct vop_getacl_args * ap)3775 nfs_getacl(struct vop_getacl_args *ap)
3776 {
3777 int error;
3778
3779 if (ap->a_type != ACL_TYPE_NFS4)
3780 return (EOPNOTSUPP);
3781 error = nfsrpc_getacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp);
3782 if (error > NFSERR_STALE) {
3783 (void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3784 error = EPERM;
3785 }
3786 return (error);
3787 }
3788
3789 static int
nfs_setacl(struct vop_setacl_args * ap)3790 nfs_setacl(struct vop_setacl_args *ap)
3791 {
3792 int error;
3793
3794 if (ap->a_type != ACL_TYPE_NFS4)
3795 return (EOPNOTSUPP);
3796 error = nfsrpc_setacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp);
3797 if (error > NFSERR_STALE) {
3798 (void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3799 error = EPERM;
3800 }
3801 return (error);
3802 }
3803
3804 /*
3805 * VOP_ADVISE for NFS.
3806 * Just return 0 for any errors, since it is just a hint.
3807 */
3808 static int
nfs_advise(struct vop_advise_args * ap)3809 nfs_advise(struct vop_advise_args *ap)
3810 {
3811 struct thread *td = curthread;
3812 struct nfsmount *nmp;
3813 uint64_t len;
3814 int error;
3815
3816 /*
3817 * First do vop_stdadvise() to handle the buffer cache.
3818 */
3819 error = vop_stdadvise(ap);
3820 if (error != 0)
3821 return (error);
3822 if (ap->a_start < 0 || ap->a_end < 0)
3823 return (0);
3824 if (ap->a_end == OFF_MAX)
3825 len = 0;
3826 else if (ap->a_end < ap->a_start)
3827 return (0);
3828 else
3829 len = ap->a_end - ap->a_start + 1;
3830 nmp = VFSTONFS(ap->a_vp->v_mount);
3831 mtx_lock(&nmp->nm_mtx);
3832 if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
3833 (NFSHASPNFS(nmp) && (nmp->nm_privflag & NFSMNTP_IOADVISETHRUMDS) ==
3834 0) || (nmp->nm_privflag & NFSMNTP_NOADVISE) != 0) {
3835 mtx_unlock(&nmp->nm_mtx);
3836 return (0);
3837 }
3838 mtx_unlock(&nmp->nm_mtx);
3839 error = nfsrpc_advise(ap->a_vp, ap->a_start, len, ap->a_advice,
3840 td->td_ucred, td);
3841 if (error == NFSERR_NOTSUPP) {
3842 mtx_lock(&nmp->nm_mtx);
3843 nmp->nm_privflag |= NFSMNTP_NOADVISE;
3844 mtx_unlock(&nmp->nm_mtx);
3845 }
3846 return (0);
3847 }
3848
3849 /*
3850 * nfs allocate call
3851 */
3852 static int
nfs_allocate(struct vop_allocate_args * ap)3853 nfs_allocate(struct vop_allocate_args *ap)
3854 {
3855 struct vnode *vp = ap->a_vp;
3856 struct thread *td = curthread;
3857 struct nfsvattr nfsva;
3858 struct nfsmount *nmp;
3859 struct nfsnode *np;
3860 off_t alen;
3861 int attrflag, error, ret;
3862 struct timespec ts;
3863 struct uio io;
3864
3865 attrflag = 0;
3866 nmp = VFSTONFS(vp->v_mount);
3867 np = VTONFS(vp);
3868 mtx_lock(&nmp->nm_mtx);
3869 if (NFSHASNFSV4(nmp) && nmp->nm_minorvers >= NFSV42_MINORVERSION &&
3870 (nmp->nm_privflag & NFSMNTP_NOALLOCATE) == 0) {
3871 mtx_unlock(&nmp->nm_mtx);
3872 alen = *ap->a_len;
3873 if ((uint64_t)alen > nfs_maxalloclen)
3874 alen = nfs_maxalloclen;
3875
3876 /* Check the file size limit. */
3877 io.uio_offset = *ap->a_offset;
3878 io.uio_resid = alen;
3879 error = vn_rlimit_fsize(vp, &io, td);
3880
3881 /*
3882 * Flush first to ensure that the allocate adds to the
3883 * file's allocation on the server.
3884 */
3885 if (error == 0) {
3886 vnode_pager_clean_sync(vp);
3887 error = ncl_flush(vp, MNT_WAIT, td, 1, 0);
3888 }
3889 if (error == 0)
3890 error = nfsrpc_allocate(vp, *ap->a_offset, alen,
3891 &nfsva, &attrflag, ap->a_cred, td);
3892 if (error == 0) {
3893 *ap->a_offset += alen;
3894 *ap->a_len -= alen;
3895 nanouptime(&ts);
3896 NFSLOCKNODE(np);
3897 np->n_localmodtime = ts;
3898 NFSUNLOCKNODE(np);
3899 } else if (error == NFSERR_NOTSUPP) {
3900 mtx_lock(&nmp->nm_mtx);
3901 nmp->nm_privflag |= NFSMNTP_NOALLOCATE;
3902 mtx_unlock(&nmp->nm_mtx);
3903 error = EINVAL;
3904 }
3905 } else {
3906 mtx_unlock(&nmp->nm_mtx);
3907 error = EINVAL;
3908 }
3909 if (attrflag != 0) {
3910 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
3911 if (error == 0 && ret != 0)
3912 error = ret;
3913 }
3914 if (error != 0)
3915 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
3916 return (error);
3917 }
3918
3919 /*
3920 * nfs deallocate call
3921 */
3922 static int
nfs_deallocate(struct vop_deallocate_args * ap)3923 nfs_deallocate(struct vop_deallocate_args *ap)
3924 {
3925 struct vnode *vp = ap->a_vp;
3926 struct thread *td = curthread;
3927 struct nfsvattr nfsva;
3928 struct nfsmount *nmp;
3929 struct nfsnode *np;
3930 off_t tlen, mlen;
3931 int attrflag, error, ret;
3932 bool clipped;
3933 struct timespec ts;
3934
3935 error = 0;
3936 attrflag = 0;
3937 nmp = VFSTONFS(vp->v_mount);
3938 np = VTONFS(vp);
3939 mtx_lock(&nmp->nm_mtx);
3940 if (NFSHASNFSV4(nmp) && nmp->nm_minorvers >= NFSV42_MINORVERSION &&
3941 (nmp->nm_privflag & NFSMNTP_NODEALLOCATE) == 0) {
3942 mtx_unlock(&nmp->nm_mtx);
3943 tlen = omin(OFF_MAX - *ap->a_offset, *ap->a_len);
3944 NFSCL_DEBUG(4, "dealloc: off=%jd len=%jd maxfilesize=%ju\n",
3945 (intmax_t)*ap->a_offset, (intmax_t)tlen,
3946 (uintmax_t)nmp->nm_maxfilesize);
3947 if ((uint64_t)*ap->a_offset >= nmp->nm_maxfilesize) {
3948 /* Avoid EFBIG error return from the NFSv4.2 server. */
3949 *ap->a_len = 0;
3950 return (0);
3951 }
3952 clipped = false;
3953 if ((uint64_t)*ap->a_offset + tlen > nmp->nm_maxfilesize)
3954 tlen = nmp->nm_maxfilesize - *ap->a_offset;
3955 if ((uint64_t)*ap->a_offset < np->n_size) {
3956 /* Limit the len to nfs_maxalloclen before EOF. */
3957 mlen = omin((off_t)np->n_size - *ap->a_offset, tlen);
3958 if ((uint64_t)mlen > nfs_maxalloclen) {
3959 NFSCL_DEBUG(4, "dealloc: tlen maxalloclen\n");
3960 tlen = nfs_maxalloclen;
3961 clipped = true;
3962 }
3963 }
3964 if (error == 0)
3965 error = ncl_vinvalbuf(vp, V_SAVE, td, 1);
3966 if (error == 0) {
3967 vnode_pager_purge_range(vp, *ap->a_offset,
3968 *ap->a_offset + tlen);
3969 error = nfsrpc_deallocate(vp, *ap->a_offset, tlen,
3970 &nfsva, &attrflag, ap->a_cred, td);
3971 NFSCL_DEBUG(4, "dealloc: rpc=%d\n", error);
3972 }
3973 if (error == 0) {
3974 NFSCL_DEBUG(4, "dealloc: attrflag=%d na_size=%ju\n",
3975 attrflag, (uintmax_t)nfsva.na_size);
3976 nanouptime(&ts);
3977 NFSLOCKNODE(np);
3978 np->n_localmodtime = ts;
3979 NFSUNLOCKNODE(np);
3980 if (attrflag != 0) {
3981 if ((uint64_t)*ap->a_offset < nfsva.na_size)
3982 *ap->a_offset += omin((off_t)
3983 nfsva.na_size - *ap->a_offset,
3984 tlen);
3985 }
3986 if (clipped && tlen < *ap->a_len)
3987 *ap->a_len -= tlen;
3988 else
3989 *ap->a_len = 0;
3990 } else if (error == NFSERR_NOTSUPP) {
3991 mtx_lock(&nmp->nm_mtx);
3992 nmp->nm_privflag |= NFSMNTP_NODEALLOCATE;
3993 mtx_unlock(&nmp->nm_mtx);
3994 }
3995 } else {
3996 mtx_unlock(&nmp->nm_mtx);
3997 error = EIO;
3998 }
3999 /*
4000 * If the NFS server cannot perform the Deallocate operation, just call
4001 * vop_stddeallocate() to perform it.
4002 */
4003 if (error != 0 && error != NFSERR_FBIG && error != NFSERR_INVAL) {
4004 error = vop_stddeallocate(ap);
4005 NFSCL_DEBUG(4, "dealloc: stddeallocate=%d\n", error);
4006 }
4007 if (attrflag != 0) {
4008 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4009 if (error == 0 && ret != 0)
4010 error = ret;
4011 }
4012 if (error != 0)
4013 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
4014 return (error);
4015 }
4016
4017 /*
4018 * nfs copy_file_range call
4019 */
4020 static int
nfs_copy_file_range(struct vop_copy_file_range_args * ap)4021 nfs_copy_file_range(struct vop_copy_file_range_args *ap)
4022 {
4023 struct vnode *invp = ap->a_invp;
4024 struct vnode *outvp = ap->a_outvp;
4025 struct mount *mp;
4026 vm_object_t invp_obj;
4027 struct nfsvattr innfsva, outnfsva;
4028 struct vattr va, *vap;
4029 struct uio io;
4030 struct nfsmount *nmp;
4031 size_t len, len2;
4032 ssize_t r;
4033 int error, inattrflag, outattrflag, ret, ret2, invp_lock;
4034 off_t inoff, outoff;
4035 bool consecutive, must_commit, tryoutcred;
4036
4037 /*
4038 * NFSv4.2 Copy is not permitted for infile == outfile.
4039 * TODO: copy_file_range() between multiple NFS mountpoints
4040 */
4041 if (invp == outvp || invp->v_mount != outvp->v_mount) {
4042 generic_copy:
4043 return (ENOSYS);
4044 }
4045
4046 invp_lock = LK_SHARED;
4047 relock:
4048
4049 /* Lock both vnodes, avoiding risk of deadlock. */
4050 do {
4051 mp = NULL;
4052 error = vn_start_write(outvp, &mp, V_WAIT);
4053 if (error == 0) {
4054 error = vn_lock(outvp, LK_EXCLUSIVE);
4055 if (error == 0) {
4056 error = vn_lock(invp, invp_lock | LK_NOWAIT);
4057 if (error == 0)
4058 break;
4059 VOP_UNLOCK(outvp);
4060 if (mp != NULL)
4061 vn_finished_write(mp);
4062 mp = NULL;
4063 error = vn_lock(invp, invp_lock);
4064 if (error == 0)
4065 VOP_UNLOCK(invp);
4066 }
4067 }
4068 if (mp != NULL)
4069 vn_finished_write(mp);
4070 } while (error == 0);
4071 if (error != 0)
4072 return (error);
4073
4074 /*
4075 * More reasons to avoid nfs copy: not NFSv4.2, or explicitly
4076 * disabled.
4077 */
4078 nmp = VFSTONFS(invp->v_mount);
4079 mtx_lock(&nmp->nm_mtx);
4080 if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4081 (nmp->nm_privflag & NFSMNTP_NOCOPY) != 0) {
4082 mtx_unlock(&nmp->nm_mtx);
4083 VOP_UNLOCK(invp);
4084 VOP_UNLOCK(outvp);
4085 if (mp != NULL)
4086 vn_finished_write(mp);
4087 goto generic_copy;
4088 }
4089 mtx_unlock(&nmp->nm_mtx);
4090
4091 /*
4092 * Do the vn_rlimit_fsize() check. Should this be above the VOP layer?
4093 */
4094 io.uio_offset = *ap->a_outoffp;
4095 io.uio_resid = *ap->a_lenp;
4096 error = vn_rlimit_fsizex(outvp, &io, 0, &r, ap->a_fsizetd);
4097 *ap->a_lenp = io.uio_resid;
4098 /*
4099 * No need to call vn_rlimit_fsizex_res before return, since the uio is
4100 * local.
4101 */
4102
4103 /*
4104 * Flush the input file so that the data is up to date before
4105 * the copy. Flush writes for the output file so that they
4106 * do not overwrite the data copied to the output file by the Copy.
4107 * Set the commit argument for both flushes so that the data is on
4108 * stable storage before the Copy RPC. This is done in case the
4109 * server reboots during the Copy and needs to be redone.
4110 */
4111 if (error == 0) {
4112 invp_obj = invp->v_object;
4113 if (invp_obj != NULL && vm_object_mightbedirty(invp_obj)) {
4114 if (invp_lock != LK_EXCLUSIVE) {
4115 invp_lock = LK_EXCLUSIVE;
4116 VOP_UNLOCK(invp);
4117 VOP_UNLOCK(outvp);
4118 if (mp != NULL)
4119 vn_finished_write(mp);
4120 goto relock;
4121 }
4122 vnode_pager_clean_sync(invp);
4123 }
4124 error = ncl_flush(invp, MNT_WAIT, curthread, 1, 0);
4125 }
4126 if (error == 0)
4127 error = ncl_vinvalbuf(outvp, V_SAVE, curthread, 0);
4128
4129 /* Do the actual NFSv4.2 RPC. */
4130 ret = ret2 = 0;
4131 len = *ap->a_lenp;
4132 mtx_lock(&nmp->nm_mtx);
4133 if ((nmp->nm_privflag & NFSMNTP_NOCONSECUTIVE) == 0)
4134 consecutive = true;
4135 else
4136 consecutive = false;
4137 mtx_unlock(&nmp->nm_mtx);
4138 inoff = *ap->a_inoffp;
4139 outoff = *ap->a_outoffp;
4140 tryoutcred = true;
4141 must_commit = false;
4142 if (error == 0) {
4143 vap = &VTONFS(invp)->n_vattr.na_vattr;
4144 error = VOP_GETATTR(invp, vap, ap->a_incred);
4145 if (error == 0) {
4146 /*
4147 * Clip "len" at va_size so that RFC compliant servers
4148 * will not reply NFSERR_INVAL.
4149 * Setting "len == 0" for the RPC would be preferred,
4150 * but some Linux servers do not support that.
4151 * If the len is being set to 0, do a Setattr RPC to
4152 * set the server's atime. This behaviour was the
4153 * preferred one for the FreeBSD "collective".
4154 */
4155 if (inoff >= vap->va_size) {
4156 *ap->a_lenp = len = 0;
4157 if ((nmp->nm_mountp->mnt_flag & MNT_NOATIME) ==
4158 0) {
4159 VATTR_NULL(&va);
4160 va.va_atime.tv_sec = 0;
4161 va.va_atime.tv_nsec = 0;
4162 va.va_vaflags = VA_UTIMES_NULL;
4163 inattrflag = 0;
4164 error = nfsrpc_setattr(invp, &va, NULL,
4165 ap->a_incred, curthread, &innfsva,
4166 &inattrflag);
4167 if (inattrflag != 0)
4168 ret = nfscl_loadattrcache(&invp,
4169 &innfsva, NULL, 0, 1);
4170 if (error == 0 && ret != 0)
4171 error = ret;
4172 }
4173 } else if (inoff + len > vap->va_size)
4174 *ap->a_lenp = len = vap->va_size - inoff;
4175 } else
4176 error = 0;
4177 }
4178
4179 /*
4180 * len will be set to 0 upon a successful Copy RPC.
4181 * As such, this only loops when the Copy RPC needs to be retried.
4182 */
4183 while (len > 0 && error == 0) {
4184 inattrflag = outattrflag = 0;
4185 len2 = len;
4186 if (tryoutcred)
4187 error = nfsrpc_copy_file_range(invp, ap->a_inoffp,
4188 outvp, ap->a_outoffp, &len2, ap->a_flags,
4189 &inattrflag, &innfsva, &outattrflag, &outnfsva,
4190 ap->a_outcred, consecutive, &must_commit);
4191 else
4192 error = nfsrpc_copy_file_range(invp, ap->a_inoffp,
4193 outvp, ap->a_outoffp, &len2, ap->a_flags,
4194 &inattrflag, &innfsva, &outattrflag, &outnfsva,
4195 ap->a_incred, consecutive, &must_commit);
4196 if (inattrflag != 0)
4197 ret = nfscl_loadattrcache(&invp, &innfsva, NULL, 0, 1);
4198 if (outattrflag != 0)
4199 ret2 = nfscl_loadattrcache(&outvp, &outnfsva, NULL,
4200 1, 1);
4201 if (error == 0) {
4202 if (consecutive == false) {
4203 if (len2 == len) {
4204 mtx_lock(&nmp->nm_mtx);
4205 nmp->nm_privflag |=
4206 NFSMNTP_NOCONSECUTIVE;
4207 mtx_unlock(&nmp->nm_mtx);
4208 } else
4209 error = NFSERR_OFFLOADNOREQS;
4210 }
4211 *ap->a_lenp = len2;
4212 len = 0;
4213 if (len2 > 0 && must_commit && error == 0)
4214 error = ncl_commit(outvp, outoff, *ap->a_lenp,
4215 ap->a_outcred, curthread);
4216 if (error == 0 && ret != 0)
4217 error = ret;
4218 if (error == 0 && ret2 != 0)
4219 error = ret2;
4220 } else if (error == NFSERR_OFFLOADNOREQS && consecutive) {
4221 /*
4222 * Try consecutive == false, which is ok only if all
4223 * bytes are copied.
4224 * If only some bytes were copied when consecutive
4225 * is false, there is no way to know which bytes
4226 * still need to be written.
4227 */
4228 consecutive = false;
4229 error = 0;
4230 } else if (error == NFSERR_ACCES && tryoutcred) {
4231 /* Try again with incred. */
4232 tryoutcred = false;
4233 error = 0;
4234 }
4235 if (error == NFSERR_STALEWRITEVERF) {
4236 /*
4237 * Server rebooted, so do it all again.
4238 */
4239 *ap->a_inoffp = inoff;
4240 *ap->a_outoffp = outoff;
4241 len = *ap->a_lenp;
4242 must_commit = false;
4243 error = 0;
4244 }
4245 }
4246 VOP_UNLOCK(invp);
4247 VOP_UNLOCK(outvp);
4248 if (mp != NULL)
4249 vn_finished_write(mp);
4250 if (error == NFSERR_NOTSUPP || error == NFSERR_OFFLOADNOREQS ||
4251 error == NFSERR_ACCES) {
4252 /*
4253 * Unlike the NFSv4.2 Copy, vn_generic_copy_file_range() can
4254 * use a_incred for the read and a_outcred for the write, so
4255 * try this for NFSERR_ACCES failures for the Copy.
4256 * For NFSERR_NOTSUPP and NFSERR_OFFLOADNOREQS, the Copy can
4257 * never succeed, so disable it.
4258 */
4259 if (error != NFSERR_ACCES) {
4260 /* Can never do Copy on this mount. */
4261 mtx_lock(&nmp->nm_mtx);
4262 nmp->nm_privflag |= NFSMNTP_NOCOPY;
4263 mtx_unlock(&nmp->nm_mtx);
4264 }
4265 *ap->a_inoffp = inoff;
4266 *ap->a_outoffp = outoff;
4267 error = vn_generic_copy_file_range(ap->a_invp, ap->a_inoffp,
4268 ap->a_outvp, ap->a_outoffp, ap->a_lenp, ap->a_flags,
4269 ap->a_incred, ap->a_outcred, ap->a_fsizetd);
4270 } else if (error != 0)
4271 *ap->a_lenp = 0;
4272
4273 if (error != 0)
4274 error = nfscl_maperr(curthread, error, (uid_t)0, (gid_t)0);
4275 return (error);
4276 }
4277
4278 /*
4279 * nfs ioctl call
4280 */
4281 static int
nfs_ioctl(struct vop_ioctl_args * ap)4282 nfs_ioctl(struct vop_ioctl_args *ap)
4283 {
4284 struct vnode *vp = ap->a_vp;
4285 struct nfsvattr nfsva;
4286 struct nfsmount *nmp;
4287 int attrflag, content, error, ret;
4288 bool eof = false; /* shut up compiler. */
4289
4290 /* Do the actual NFSv4.2 RPC. */
4291 switch (ap->a_command) {
4292 case FIOSEEKDATA:
4293 content = NFSV4CONTENT_DATA;
4294 break;
4295 case FIOSEEKHOLE:
4296 content = NFSV4CONTENT_HOLE;
4297 break;
4298 default:
4299 return (ENOTTY);
4300 }
4301
4302 error = vn_lock(vp, LK_EXCLUSIVE);
4303 if (error != 0)
4304 return (EBADF);
4305
4306 if (vp->v_type != VREG) {
4307 VOP_UNLOCK(vp);
4308 return (ENOTTY);
4309 }
4310 nmp = VFSTONFS(vp->v_mount);
4311 if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION) {
4312 VOP_UNLOCK(vp);
4313 error = vop_stdioctl(ap);
4314 return (error);
4315 }
4316
4317 attrflag = 0;
4318 if (*((off_t *)ap->a_data) >= VTONFS(vp)->n_size)
4319 error = ENXIO;
4320 else {
4321 /*
4322 * Flush all writes, so that the server is up to date.
4323 * Although a Commit is not required, the commit argument
4324 * is set so that, for a pNFS File/Flexible File Layout
4325 * server, the LayoutCommit will be done to ensure the file
4326 * size is up to date on the Metadata Server.
4327 */
4328
4329 vnode_pager_clean_sync(vp);
4330 error = ncl_flush(vp, MNT_WAIT, ap->a_td, 1, 0);
4331 if (error == 0)
4332 error = nfsrpc_seek(vp, (off_t *)ap->a_data, &eof,
4333 content, ap->a_cred, &nfsva, &attrflag);
4334 /* If at eof for FIOSEEKDATA, return ENXIO. */
4335 if (eof && error == 0 && content == NFSV4CONTENT_DATA)
4336 error = ENXIO;
4337 }
4338 if (attrflag != 0) {
4339 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4340 if (error == 0 && ret != 0)
4341 error = ret;
4342 }
4343 NFSVOPUNLOCK(vp);
4344
4345 if (error != 0)
4346 error = ENXIO;
4347 return (error);
4348 }
4349
4350 /*
4351 * nfs getextattr call
4352 */
4353 static int
nfs_getextattr(struct vop_getextattr_args * ap)4354 nfs_getextattr(struct vop_getextattr_args *ap)
4355 {
4356 struct vnode *vp = ap->a_vp;
4357 struct nfsmount *nmp;
4358 struct ucred *cred;
4359 struct thread *td = ap->a_td;
4360 struct nfsvattr nfsva;
4361 ssize_t len;
4362 int attrflag, error, ret;
4363
4364 nmp = VFSTONFS(vp->v_mount);
4365 mtx_lock(&nmp->nm_mtx);
4366 if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4367 (nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4368 ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4369 mtx_unlock(&nmp->nm_mtx);
4370 return (EOPNOTSUPP);
4371 }
4372 mtx_unlock(&nmp->nm_mtx);
4373
4374 cred = ap->a_cred;
4375 if (cred == NULL)
4376 cred = td->td_ucred;
4377 /* Do the actual NFSv4.2 Optional Extended Attribute (RFC-8276) RPC. */
4378 attrflag = 0;
4379 error = nfsrpc_getextattr(vp, ap->a_name, ap->a_uio, &len, &nfsva,
4380 &attrflag, cred, td);
4381 if (attrflag != 0) {
4382 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4383 if (error == 0 && ret != 0)
4384 error = ret;
4385 }
4386 if (error == 0 && ap->a_size != NULL)
4387 *ap->a_size = len;
4388
4389 switch (error) {
4390 case NFSERR_NOTSUPP:
4391 case NFSERR_OPILLEGAL:
4392 mtx_lock(&nmp->nm_mtx);
4393 nmp->nm_privflag |= NFSMNTP_NOXATTR;
4394 mtx_unlock(&nmp->nm_mtx);
4395 error = EOPNOTSUPP;
4396 break;
4397 case NFSERR_NOXATTR:
4398 case NFSERR_XATTR2BIG:
4399 error = ENOATTR;
4400 break;
4401 default:
4402 error = nfscl_maperr(td, error, 0, 0);
4403 break;
4404 }
4405 return (error);
4406 }
4407
4408 /*
4409 * nfs setextattr call
4410 */
4411 static int
nfs_setextattr(struct vop_setextattr_args * ap)4412 nfs_setextattr(struct vop_setextattr_args *ap)
4413 {
4414 struct vnode *vp = ap->a_vp;
4415 struct nfsmount *nmp;
4416 struct ucred *cred;
4417 struct thread *td = ap->a_td;
4418 struct nfsvattr nfsva;
4419 int attrflag, error, ret;
4420
4421 nmp = VFSTONFS(vp->v_mount);
4422 mtx_lock(&nmp->nm_mtx);
4423 if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4424 (nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4425 ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4426 mtx_unlock(&nmp->nm_mtx);
4427 return (EOPNOTSUPP);
4428 }
4429 mtx_unlock(&nmp->nm_mtx);
4430
4431 if (ap->a_uio->uio_resid < 0)
4432 return (EINVAL);
4433 cred = ap->a_cred;
4434 if (cred == NULL)
4435 cred = td->td_ucred;
4436 /* Do the actual NFSv4.2 Optional Extended Attribute (RFC-8276) RPC. */
4437 attrflag = 0;
4438 error = nfsrpc_setextattr(vp, ap->a_name, ap->a_uio, &nfsva,
4439 &attrflag, cred, td);
4440 if (attrflag != 0) {
4441 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4442 if (error == 0 && ret != 0)
4443 error = ret;
4444 }
4445
4446 switch (error) {
4447 case NFSERR_NOTSUPP:
4448 case NFSERR_OPILLEGAL:
4449 mtx_lock(&nmp->nm_mtx);
4450 nmp->nm_privflag |= NFSMNTP_NOXATTR;
4451 mtx_unlock(&nmp->nm_mtx);
4452 error = EOPNOTSUPP;
4453 break;
4454 case NFSERR_NOXATTR:
4455 case NFSERR_XATTR2BIG:
4456 error = ENOATTR;
4457 break;
4458 default:
4459 error = nfscl_maperr(td, error, 0, 0);
4460 break;
4461 }
4462 return (error);
4463 }
4464
4465 /*
4466 * nfs listextattr call
4467 */
4468 static int
nfs_listextattr(struct vop_listextattr_args * ap)4469 nfs_listextattr(struct vop_listextattr_args *ap)
4470 {
4471 struct vnode *vp = ap->a_vp;
4472 struct nfsmount *nmp;
4473 struct ucred *cred;
4474 struct thread *td = ap->a_td;
4475 struct nfsvattr nfsva;
4476 size_t len, len2;
4477 uint64_t cookie;
4478 int attrflag, error, ret;
4479 bool eof;
4480
4481 nmp = VFSTONFS(vp->v_mount);
4482 mtx_lock(&nmp->nm_mtx);
4483 if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4484 (nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4485 ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4486 mtx_unlock(&nmp->nm_mtx);
4487 return (EOPNOTSUPP);
4488 }
4489 mtx_unlock(&nmp->nm_mtx);
4490
4491 cred = ap->a_cred;
4492 if (cred == NULL)
4493 cred = td->td_ucred;
4494
4495 /* Loop around doing List Extended Attribute RPCs. */
4496 eof = false;
4497 cookie = 0;
4498 len2 = 0;
4499 error = 0;
4500 while (!eof && error == 0) {
4501 len = nmp->nm_rsize;
4502 attrflag = 0;
4503 error = nfsrpc_listextattr(vp, &cookie, ap->a_uio, &len, &eof,
4504 &nfsva, &attrflag, cred, td);
4505 if (attrflag != 0) {
4506 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4507 if (error == 0 && ret != 0)
4508 error = ret;
4509 }
4510 if (error == 0) {
4511 len2 += len;
4512 if (len2 > SSIZE_MAX)
4513 error = ENOATTR;
4514 }
4515 }
4516 if (error == 0 && ap->a_size != NULL)
4517 *ap->a_size = len2;
4518
4519 switch (error) {
4520 case NFSERR_NOTSUPP:
4521 case NFSERR_OPILLEGAL:
4522 mtx_lock(&nmp->nm_mtx);
4523 nmp->nm_privflag |= NFSMNTP_NOXATTR;
4524 mtx_unlock(&nmp->nm_mtx);
4525 error = EOPNOTSUPP;
4526 break;
4527 case NFSERR_NOXATTR:
4528 case NFSERR_XATTR2BIG:
4529 error = ENOATTR;
4530 break;
4531 default:
4532 error = nfscl_maperr(td, error, 0, 0);
4533 break;
4534 }
4535 return (error);
4536 }
4537
4538 /*
4539 * nfs setextattr call
4540 */
4541 static int
nfs_deleteextattr(struct vop_deleteextattr_args * ap)4542 nfs_deleteextattr(struct vop_deleteextattr_args *ap)
4543 {
4544 struct vnode *vp = ap->a_vp;
4545 struct nfsmount *nmp;
4546 struct nfsvattr nfsva;
4547 int attrflag, error, ret;
4548
4549 nmp = VFSTONFS(vp->v_mount);
4550 mtx_lock(&nmp->nm_mtx);
4551 if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4552 (nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4553 ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4554 mtx_unlock(&nmp->nm_mtx);
4555 return (EOPNOTSUPP);
4556 }
4557 mtx_unlock(&nmp->nm_mtx);
4558
4559 /* Do the actual NFSv4.2 Optional Extended Attribute (RFC-8276) RPC. */
4560 attrflag = 0;
4561 error = nfsrpc_rmextattr(vp, ap->a_name, &nfsva, &attrflag, ap->a_cred,
4562 ap->a_td);
4563 if (attrflag != 0) {
4564 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4565 if (error == 0 && ret != 0)
4566 error = ret;
4567 }
4568
4569 switch (error) {
4570 case NFSERR_NOTSUPP:
4571 case NFSERR_OPILLEGAL:
4572 mtx_lock(&nmp->nm_mtx);
4573 nmp->nm_privflag |= NFSMNTP_NOXATTR;
4574 mtx_unlock(&nmp->nm_mtx);
4575 error = EOPNOTSUPP;
4576 break;
4577 case NFSERR_NOXATTR:
4578 case NFSERR_XATTR2BIG:
4579 error = ENOATTR;
4580 break;
4581 default:
4582 error = nfscl_maperr(ap->a_td, error, 0, 0);
4583 break;
4584 }
4585 return (error);
4586 }
4587
4588 /*
4589 * Return POSIX pathconf information applicable to nfs filesystems.
4590 */
4591 static int
nfs_pathconf(struct vop_pathconf_args * ap)4592 nfs_pathconf(struct vop_pathconf_args *ap)
4593 {
4594 struct nfsv3_pathconf pc;
4595 struct nfsvattr nfsva;
4596 struct vnode *vp = ap->a_vp;
4597 struct nfsmount *nmp;
4598 struct thread *td = curthread;
4599 off_t off;
4600 bool eof, has_namedattr, named_enabled;
4601 int attrflag, error;
4602 struct nfsnode *np;
4603
4604 nmp = VFSTONFS(vp->v_mount);
4605 np = VTONFS(vp);
4606 named_enabled = false;
4607 has_namedattr = false;
4608 if ((NFS_ISV34(vp) && (ap->a_name == _PC_LINK_MAX ||
4609 ap->a_name == _PC_NAME_MAX || ap->a_name == _PC_CHOWN_RESTRICTED ||
4610 ap->a_name == _PC_NO_TRUNC)) ||
4611 (NFS_ISV4(vp) && (ap->a_name == _PC_ACL_NFS4 ||
4612 ap->a_name == _PC_HAS_NAMEDATTR))) {
4613 /*
4614 * Since only the above 4 a_names are returned by the NFSv3
4615 * Pathconf RPC, there is no point in doing it for others.
4616 * For NFSv4, the Pathconf RPC (actually a Getattr Op.) can
4617 * be used for _PC_ACL_NFS4 and _PC_HAS_NAMEDATTR as well.
4618 */
4619 error = nfsrpc_pathconf(vp, &pc, &has_namedattr, td->td_ucred,
4620 td, &nfsva, &attrflag);
4621 if (attrflag != 0)
4622 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4623 if (error != 0)
4624 return (error);
4625 } else if (NFS_ISV4(vp) && ap->a_name == _PC_NAMEDATTR_ENABLED &&
4626 (np->n_flag & NNAMEDNOTSUPP) == 0) {
4627 struct nfsfh *nfhp;
4628
4629 error = nfsrpc_openattr(nmp, vp, np->n_fhp->nfh_fh,
4630 np->n_fhp->nfh_len, false, td->td_ucred, td, &nfsva, &nfhp,
4631 &attrflag);
4632 named_enabled = true;
4633 if (error == 0) {
4634 free(nfhp, M_NFSFH);
4635 } else if (error == NFSERR_NOTSUPP) {
4636 named_enabled = false;
4637 NFSLOCKNODE(np);
4638 np->n_flag |= NNAMEDNOTSUPP;
4639 NFSUNLOCKNODE(np);
4640 }
4641 error = 0;
4642 } else {
4643 /*
4644 * For NFSv2 (or NFSv3 when not one of the above 4 a_names),
4645 * just fake them.
4646 */
4647 pc.pc_linkmax = NFS_LINK_MAX;
4648 pc.pc_namemax = NFS_MAXNAMLEN;
4649 pc.pc_notrunc = 1;
4650 pc.pc_chownrestricted = 1;
4651 pc.pc_caseinsensitive = 0;
4652 pc.pc_casepreserving = 1;
4653 error = 0;
4654 }
4655 switch (ap->a_name) {
4656 case _PC_LINK_MAX:
4657 #ifdef _LP64
4658 *ap->a_retval = pc.pc_linkmax;
4659 #else
4660 *ap->a_retval = MIN(LONG_MAX, pc.pc_linkmax);
4661 #endif
4662 break;
4663 case _PC_NAME_MAX:
4664 *ap->a_retval = pc.pc_namemax;
4665 break;
4666 case _PC_PIPE_BUF:
4667 if (ap->a_vp->v_type == VDIR || ap->a_vp->v_type == VFIFO)
4668 *ap->a_retval = PIPE_BUF;
4669 else
4670 error = EINVAL;
4671 break;
4672 case _PC_CHOWN_RESTRICTED:
4673 *ap->a_retval = pc.pc_chownrestricted;
4674 break;
4675 case _PC_NO_TRUNC:
4676 *ap->a_retval = pc.pc_notrunc;
4677 break;
4678 case _PC_ACL_NFS4:
4679 if (NFS_ISV4(vp) && nfsrv_useacl != 0 && attrflag != 0 &&
4680 NFSISSET_ATTRBIT(&nfsva.na_suppattr, NFSATTRBIT_ACL))
4681 *ap->a_retval = 1;
4682 else
4683 *ap->a_retval = 0;
4684 break;
4685 case _PC_ACL_PATH_MAX:
4686 if (NFS_ISV4(vp))
4687 *ap->a_retval = ACL_MAX_ENTRIES;
4688 else
4689 *ap->a_retval = 3;
4690 break;
4691 case _PC_PRIO_IO:
4692 *ap->a_retval = 0;
4693 break;
4694 case _PC_SYNC_IO:
4695 *ap->a_retval = 0;
4696 break;
4697 case _PC_ALLOC_SIZE_MIN:
4698 *ap->a_retval = vp->v_mount->mnt_stat.f_bsize;
4699 break;
4700 case _PC_FILESIZEBITS:
4701 if (NFS_ISV34(vp))
4702 *ap->a_retval = 64;
4703 else
4704 *ap->a_retval = 32;
4705 break;
4706 case _PC_REC_INCR_XFER_SIZE:
4707 *ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
4708 break;
4709 case _PC_REC_MAX_XFER_SIZE:
4710 *ap->a_retval = -1; /* means ``unlimited'' */
4711 break;
4712 case _PC_REC_MIN_XFER_SIZE:
4713 *ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
4714 break;
4715 case _PC_REC_XFER_ALIGN:
4716 *ap->a_retval = PAGE_SIZE;
4717 break;
4718 case _PC_SYMLINK_MAX:
4719 *ap->a_retval = NFS_MAXPATHLEN;
4720 break;
4721 case _PC_MIN_HOLE_SIZE:
4722 /* Only some NFSv4.2 servers support Seek for Holes. */
4723 *ap->a_retval = 0;
4724 if (NFS_ISV4(vp) && nmp->nm_minorvers == NFSV42_MINORVERSION) {
4725 /*
4726 * NFSv4.2 doesn't have an attribute for hole size,
4727 * so all we can do is see if the Seek operation is
4728 * supported and then use f_iosize as a "best guess".
4729 */
4730 mtx_lock(&nmp->nm_mtx);
4731 if ((nmp->nm_privflag & NFSMNTP_SEEKTESTED) == 0) {
4732 mtx_unlock(&nmp->nm_mtx);
4733 off = 0;
4734 attrflag = 0;
4735 error = nfsrpc_seek(vp, &off, &eof,
4736 NFSV4CONTENT_HOLE, td->td_ucred, &nfsva,
4737 &attrflag);
4738 if (attrflag != 0)
4739 (void) nfscl_loadattrcache(&vp, &nfsva,
4740 NULL, 0, 1);
4741 mtx_lock(&nmp->nm_mtx);
4742 if (error == NFSERR_NOTSUPP)
4743 nmp->nm_privflag |= NFSMNTP_SEEKTESTED;
4744 else
4745 nmp->nm_privflag |= NFSMNTP_SEEKTESTED |
4746 NFSMNTP_SEEK;
4747 error = 0;
4748 }
4749 if ((nmp->nm_privflag & NFSMNTP_SEEK) != 0)
4750 *ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
4751 mtx_unlock(&nmp->nm_mtx);
4752 }
4753 break;
4754 case _PC_NAMEDATTR_ENABLED:
4755 if (named_enabled)
4756 *ap->a_retval = 1;
4757 else
4758 *ap->a_retval = 0;
4759 break;
4760 case _PC_HAS_NAMEDATTR:
4761 if (has_namedattr)
4762 *ap->a_retval = 1;
4763 else
4764 *ap->a_retval = 0;
4765 break;
4766 case _PC_HAS_HIDDENSYSTEM:
4767 if (NFS_ISV4(vp) && NFSISSET_ATTRBIT(&np->n_vattr.na_suppattr,
4768 NFSATTRBIT_HIDDEN) &&
4769 NFSISSET_ATTRBIT(&np->n_vattr.na_suppattr,
4770 NFSATTRBIT_SYSTEM))
4771 *ap->a_retval = 1;
4772 else
4773 *ap->a_retval = 0;
4774 break;
4775
4776 default:
4777 error = vop_stdpathconf(ap);
4778 break;
4779 }
4780 return (error);
4781 }
4782