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(vp, 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_ARCHIVE, 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_ARCHIVE | UF_HIDDEN | UF_SYSTEM)) != 0 ||
1090 ((vap->va_flags & UF_ARCHIVE) != 0 &&
1091 !NFSISSET_ATTRBIT(&np->n_vattr.na_suppattr, NFSATTRBIT_ARCHIVE)) ||
1092 ((vap->va_flags & UF_HIDDEN) != 0 &&
1093 !NFSISSET_ATTRBIT(&np->n_vattr.na_suppattr, NFSATTRBIT_HIDDEN)) ||
1094 ((vap->va_flags & UF_SYSTEM) != 0 &&
1095 !NFSISSET_ATTRBIT(&np->n_vattr.na_suppattr, NFSATTRBIT_SYSTEM))))
1096 return (EOPNOTSUPP);
1097
1098 /*
1099 * Disallow write attempts if the filesystem is mounted read-only.
1100 */
1101 if ((vap->va_flags != (u_long)VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
1102 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
1103 vap->va_mtime.tv_sec != VNOVAL ||
1104 vap->va_birthtime.tv_sec != VNOVAL ||
1105 vap->va_mode != (mode_t)VNOVAL) &&
1106 (vp->v_mount->mnt_flag & MNT_RDONLY))
1107 return (EROFS);
1108 if (vap->va_size != VNOVAL) {
1109 switch (vp->v_type) {
1110 case VDIR:
1111 return (EISDIR);
1112 case VCHR:
1113 case VBLK:
1114 case VSOCK:
1115 case VFIFO:
1116 if (vap->va_mtime.tv_sec == VNOVAL &&
1117 vap->va_atime.tv_sec == VNOVAL &&
1118 vap->va_birthtime.tv_sec == VNOVAL &&
1119 vap->va_mode == (mode_t)VNOVAL &&
1120 vap->va_uid == (uid_t)VNOVAL &&
1121 vap->va_gid == (gid_t)VNOVAL)
1122 return (0);
1123 vap->va_size = VNOVAL;
1124 break;
1125 default:
1126 /*
1127 * Disallow write attempts if the filesystem is
1128 * mounted read-only.
1129 */
1130 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1131 return (EROFS);
1132 /*
1133 * We run vnode_pager_setsize() early (why?),
1134 * we must set np->n_size now to avoid vinvalbuf
1135 * V_SAVE races that might setsize a lower
1136 * value.
1137 */
1138 NFSLOCKNODE(np);
1139 tsize = np->n_size;
1140 NFSUNLOCKNODE(np);
1141 error = ncl_meta_setsize(vp, td, vap->va_size);
1142 NFSLOCKNODE(np);
1143 if (np->n_flag & NMODIFIED) {
1144 tsize = np->n_size;
1145 NFSUNLOCKNODE(np);
1146 error = ncl_vinvalbuf(vp, vap->va_size == 0 ?
1147 0 : V_SAVE, td, 1);
1148 if (error != 0) {
1149 vnode_pager_setsize(vp, tsize);
1150 return (error);
1151 }
1152 /*
1153 * Call nfscl_delegmodtime() to set the modify time
1154 * locally, as required.
1155 */
1156 nfscl_delegmodtime(vp, NULL);
1157 } else
1158 NFSUNLOCKNODE(np);
1159 /*
1160 * np->n_size has already been set to vap->va_size
1161 * in ncl_meta_setsize(). We must set it again since
1162 * nfs_loadattrcache() could be called through
1163 * ncl_meta_setsize() and could modify np->n_size.
1164 */
1165 NFSLOCKNODE(np);
1166 np->n_vattr.na_size = np->n_size = vap->va_size;
1167 NFSUNLOCKNODE(np);
1168 }
1169 } else {
1170 NFSLOCKNODE(np);
1171 if ((vap->va_mtime.tv_sec != VNOVAL || vap->va_atime.tv_sec != VNOVAL) &&
1172 (np->n_flag & NMODIFIED) && vp->v_type == VREG) {
1173 NFSUNLOCKNODE(np);
1174 error = ncl_vinvalbuf(vp, V_SAVE, td, 1);
1175 if (error == EINTR || error == EIO)
1176 return (error);
1177 } else
1178 NFSUNLOCKNODE(np);
1179 }
1180 error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
1181 if (vap->va_size != VNOVAL) {
1182 if (error == 0) {
1183 nanouptime(&ts);
1184 NFSLOCKNODE(np);
1185 np->n_localmodtime = ts;
1186 NFSUNLOCKNODE(np);
1187 } else {
1188 NFSLOCKNODE(np);
1189 np->n_size = np->n_vattr.na_size = tsize;
1190 vnode_pager_setsize(vp, tsize);
1191 NFSUNLOCKNODE(np);
1192 }
1193 }
1194 if (vap->va_mtime.tv_sec != VNOVAL && error == 0)
1195 nfscl_delegmodtime(vp, &vap->va_mtime);
1196 return (error);
1197 }
1198
1199 /*
1200 * Do an nfs setattr rpc.
1201 */
1202 static int
nfs_setattrrpc(struct vnode * vp,struct vattr * vap,struct ucred * cred,struct thread * td)1203 nfs_setattrrpc(struct vnode *vp, struct vattr *vap, struct ucred *cred,
1204 struct thread *td)
1205 {
1206 struct nfsnode *np = VTONFS(vp);
1207 int error, ret, attrflag, i;
1208 struct nfsvattr nfsva;
1209
1210 if (NFS_ISV34(vp)) {
1211 NFSLOCKNODE(np);
1212 for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
1213 np->n_accesscache[i].stamp = 0;
1214 np->n_flag |= NDELEGMOD;
1215 NFSUNLOCKNODE(np);
1216 KDTRACE_NFS_ACCESSCACHE_FLUSH_DONE(vp);
1217 }
1218 error = nfsrpc_setattr(vp, vap, NULL, cred, td, &nfsva, &attrflag);
1219 if (attrflag) {
1220 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
1221 if (ret && !error)
1222 error = ret;
1223 }
1224 if (error && NFS_ISV4(vp))
1225 error = nfscl_maperr(td, error, vap->va_uid, vap->va_gid);
1226 return (error);
1227 }
1228
1229 /*
1230 * Get a named attribute directory for the vnode.
1231 */
1232 static int
nfs_get_namedattrdir(struct vnode * vp,struct componentname * cnp,struct vnode ** vpp)1233 nfs_get_namedattrdir(struct vnode *vp, struct componentname *cnp,
1234 struct vnode **vpp)
1235 {
1236 struct nfsfh *nfhp;
1237 struct nfsnode *np;
1238 struct vnode *newvp;
1239 struct nfsvattr nfsva;
1240 int attrflag, error;
1241
1242 attrflag = 0;
1243 *vpp = NULL;
1244 np = VTONFS(vp);
1245 error = nfsrpc_openattr(VFSTONFS(vp->v_mount), vp, np->n_fhp->nfh_fh,
1246 np->n_fhp->nfh_len, (cnp->cn_flags & CREATENAMED),
1247 cnp->cn_cred, curthread, &nfsva, &nfhp, &attrflag);
1248 if (error == NFSERR_NOTSUPP)
1249 error = ENOATTR;
1250 if (error == 0)
1251 error = nfscl_nget(vp->v_mount, vp, nfhp, cnp, curthread, &np,
1252 cnp->cn_lkflags);
1253 if (error != 0)
1254 return (error);
1255 newvp = NFSTOV(np);
1256 vn_irflag_set_cond(newvp, VIRF_NAMEDDIR);
1257 if (attrflag != 0)
1258 (void)nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
1259 *vpp = newvp;
1260 return (0);
1261 }
1262
1263 /*
1264 * nfs lookup call, one step at a time...
1265 * First look in cache
1266 * If not found, unlock the directory nfsnode and do the rpc
1267 */
1268 static int
nfs_lookup(struct vop_lookup_args * ap)1269 nfs_lookup(struct vop_lookup_args *ap)
1270 {
1271 struct componentname *cnp = ap->a_cnp;
1272 struct vnode *dvp = ap->a_dvp;
1273 struct vnode **vpp = ap->a_vpp;
1274 struct mount *mp = dvp->v_mount;
1275 uint64_t flags = cnp->cn_flags;
1276 struct vnode *newvp;
1277 struct nfsmount *nmp;
1278 struct nfsnode *np, *newnp;
1279 int error = 0, attrflag, dattrflag, ltype, ncticks;
1280 struct thread *td = curthread;
1281 struct nfsfh *nfhp;
1282 struct nfsvattr dnfsva, nfsva;
1283 struct vattr vattr;
1284 struct timespec nctime, ts;
1285 uint32_t openmode;
1286 bool is_nameddir, needs_nameddir, opennamed;
1287
1288 dattrflag = 0;
1289 *vpp = NULL;
1290 nmp = VFSTONFS(mp);
1291 opennamed = (flags & (OPENNAMED | ISLASTCN)) == (OPENNAMED | ISLASTCN);
1292 if (opennamed && (!NFSHASNFSV4(nmp) || !NFSHASNFSV4N(nmp)))
1293 return (ENOATTR);
1294 is_nameddir = (vn_irflag_read(dvp) & VIRF_NAMEDDIR) != 0;
1295 if ((is_nameddir && (flags & ISLASTCN) == 0 && (cnp->cn_namelen > 1 ||
1296 *cnp->cn_nameptr != '.')) ||
1297 (opennamed && !is_nameddir && (flags & ISDOTDOT) != 0))
1298 return (ENOATTR);
1299 if ((flags & ISLASTCN) && (mp->mnt_flag & MNT_RDONLY) &&
1300 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
1301 return (EROFS);
1302 np = VTONFS(dvp);
1303
1304 needs_nameddir = false;
1305 if (opennamed || is_nameddir) {
1306 cnp->cn_flags &= ~MAKEENTRY;
1307 if (!is_nameddir)
1308 needs_nameddir = true;
1309 }
1310
1311 /*
1312 * If the named attribute directory is needed, acquire it now.
1313 */
1314 newvp = NULL;
1315 if (needs_nameddir) {
1316 KASSERT(np->n_v4 == NULL, ("nfs_lookup: O_NAMEDATTR when"
1317 " n_v4 not NULL"));
1318 error = nfs_get_namedattrdir(dvp, cnp, &newvp);
1319 if (error != 0)
1320 goto handle_error;
1321 if (cnp->cn_namelen == 1 && *cnp->cn_nameptr == '.') {
1322 *vpp = newvp;
1323 return (0);
1324 }
1325 dvp = newvp;
1326 np = VTONFS(dvp);
1327 newvp = NULL;
1328 } else if (opennamed && cnp->cn_namelen == 1 &&
1329 *cnp->cn_nameptr == '.') {
1330 vref(dvp);
1331 *vpp = dvp;
1332 return (0);
1333 }
1334
1335 if (dvp->v_type != VDIR)
1336 return (ENOTDIR);
1337
1338 /* For NFSv4, wait until any remove is done. */
1339 NFSLOCKNODE(np);
1340 while (NFSHASNFSV4(nmp) && (np->n_flag & NREMOVEINPROG)) {
1341 np->n_flag |= NREMOVEWANT;
1342 (void) msleep((caddr_t)np, &np->n_mtx, PZERO, "nfslkup", 0);
1343 }
1344 NFSUNLOCKNODE(np);
1345
1346 error = vn_dir_check_exec(dvp, cnp);
1347 if (error != 0)
1348 return (error);
1349
1350 if (!opennamed && !is_nameddir) {
1351 error = cache_lookup(dvp, vpp, cnp, &nctime, &ncticks);
1352 if (error > 0 && error != ENOENT)
1353 return (error);
1354 if (error == -1) {
1355 /*
1356 * Lookups of "." are special and always return the
1357 * current directory. cache_lookup() already handles
1358 * associated locking bookkeeping, etc.
1359 */
1360 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
1361 return (0);
1362 }
1363
1364 /*
1365 * We only accept a positive hit in the cache if the
1366 * change time of the file matches our cached copy.
1367 * Otherwise, we discard the cache entry and fallback
1368 * to doing a lookup RPC. We also only trust cache
1369 * entries for less than nm_nametimeo seconds.
1370 *
1371 * To better handle stale file handles and attributes,
1372 * clear the attribute cache of this node if it is a
1373 * leaf component, part of an open() call, and not
1374 * locally modified before fetching the attributes.
1375 * This should allow stale file handles to be detected
1376 * here where we can fall back to a LOOKUP RPC to
1377 * recover rather than having nfs_open() detect the
1378 * stale file handle and failing open(2) with ESTALE.
1379 */
1380 newvp = *vpp;
1381 newnp = VTONFS(newvp);
1382 if (!(nmp->nm_flag & NFSMNT_NOCTO) &&
1383 (flags & (ISLASTCN | ISOPEN)) ==
1384 (ISLASTCN | ISOPEN) &&
1385 !(newnp->n_flag & NMODIFIED)) {
1386 NFSLOCKNODE(newnp);
1387 newnp->n_attrstamp = 0;
1388 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1389 NFSUNLOCKNODE(newnp);
1390 }
1391 if (nfscl_nodeleg(newvp, 0) == 0 ||
1392 ((u_int)(ticks - ncticks) <
1393 (nmp->nm_nametimeo * hz) &&
1394 VOP_GETATTR(newvp, &vattr, cnp->cn_cred) == 0 &&
1395 timespeccmp(&vattr.va_ctime, &nctime, ==))) {
1396 NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits);
1397 return (0);
1398 }
1399 cache_purge(newvp);
1400 if (dvp != newvp)
1401 vput(newvp);
1402 else
1403 vrele(newvp);
1404 *vpp = NULL;
1405 } else if (error == ENOENT) {
1406 if (VN_IS_DOOMED(dvp))
1407 return (ENOENT);
1408 /*
1409 * We only accept a negative hit in the cache if the
1410 * modification time of the parent directory matches
1411 * the cached copy in the name cache entry.
1412 * Otherwise, we discard all of the negative cache
1413 * entries for this directory. We also only trust
1414 * negative cache entries for up to nm_negnametimeo
1415 * seconds.
1416 */
1417 if ((u_int)(ticks - ncticks) <
1418 (nmp->nm_negnametimeo * hz) &&
1419 VOP_GETATTR(dvp, &vattr, cnp->cn_cred) == 0 &&
1420 timespeccmp(&vattr.va_mtime, &nctime, ==)) {
1421 NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits);
1422 return (ENOENT);
1423 }
1424 cache_purge_negative(dvp);
1425 }
1426 }
1427
1428 openmode = 0;
1429 #if 0
1430 /*
1431 * The use of LookupOpen breaks some builds. It is disabled
1432 * until that is fixed.
1433 */
1434 /*
1435 * If this an NFSv4.1/4.2 mount using the "oneopenown" mount
1436 * option, it is possible to do the Open operation in the same
1437 * compound as Lookup, so long as delegations are not being
1438 * issued. This saves doing a separate RPC for Open.
1439 * For pnfs, do not do this, since the Open+LayoutGet will
1440 * be needed as a separate RPC.
1441 */
1442 NFSLOCKMNT(nmp);
1443 if (NFSHASNFSV4N(nmp) && NFSHASONEOPENOWN(nmp) && !NFSHASPNFS(nmp) &&
1444 (nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0 &&
1445 (!NFSMNT_RDONLY(mp) || (flags & OPENWRITE) == 0) &&
1446 (flags & (ISLASTCN | ISOPEN | OPENNAMED))) == (ISLASTCN | ISOPEN)) {
1447 if ((flags & OPENREAD) != 0)
1448 openmode |= NFSV4OPEN_ACCESSREAD;
1449 if ((flags & OPENWRITE) != 0)
1450 openmode |= NFSV4OPEN_ACCESSWRITE;
1451 }
1452 NFSUNLOCKMNT(nmp);
1453 #endif
1454
1455 newvp = NULL;
1456 NFSINCRGLOBAL(nfsstatsv1.lookupcache_misses);
1457 nanouptime(&ts);
1458 error = nfsrpc_lookup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1459 cnp->cn_cred, td, &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1460 openmode);
1461 if (dattrflag)
1462 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
1463 if (needs_nameddir) {
1464 vput(dvp);
1465 dvp = ap->a_dvp;
1466 }
1467 handle_error:
1468 if (error) {
1469 if (newvp != NULL) {
1470 vput(newvp);
1471 *vpp = NULL;
1472 }
1473
1474 if (error != ENOENT) {
1475 if (NFS_ISV4(dvp))
1476 error = nfscl_maperr(td, error,
1477 (uid_t)0, (gid_t)0);
1478 return (error);
1479 }
1480
1481 /* The requested file was not found. */
1482 if ((cnp->cn_nameiop == CREATE ||
1483 cnp->cn_nameiop == RENAME) &&
1484 (flags & ISLASTCN)) {
1485 /*
1486 * XXX: UFS does a full VOP_ACCESS(dvp,
1487 * VWRITE) here instead of just checking
1488 * MNT_RDONLY.
1489 */
1490 if (mp->mnt_flag & MNT_RDONLY)
1491 return (EROFS);
1492 return (EJUSTRETURN);
1493 }
1494
1495 if ((cnp->cn_flags & MAKEENTRY) != 0 && dattrflag) {
1496 /*
1497 * Cache the modification time of the parent
1498 * directory from the post-op attributes in
1499 * the name cache entry. The negative cache
1500 * entry will be ignored once the directory
1501 * has changed. Don't bother adding the entry
1502 * if the directory has already changed.
1503 */
1504 NFSLOCKNODE(np);
1505 if (timespeccmp(&np->n_vattr.na_mtime,
1506 &dnfsva.na_mtime, ==)) {
1507 NFSUNLOCKNODE(np);
1508 cache_enter_time(dvp, NULL, cnp,
1509 &dnfsva.na_mtime, NULL);
1510 } else
1511 NFSUNLOCKNODE(np);
1512 }
1513 return (ENOENT);
1514 }
1515
1516 /*
1517 * Handle RENAME case...
1518 */
1519 if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
1520 if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1521 free(nfhp, M_NFSFH);
1522 return (EISDIR);
1523 }
1524 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np,
1525 LK_EXCLUSIVE);
1526 if (error)
1527 return (error);
1528 newvp = NFSTOV(np);
1529 /*
1530 * If n_localmodtime >= time before RPC, then
1531 * a file modification operation, such as
1532 * VOP_SETATTR() of size, has occurred while
1533 * the Lookup RPC and acquisition of the vnode
1534 * happened. As such, the attributes might
1535 * be stale, with possibly an incorrect size.
1536 */
1537 NFSLOCKNODE(np);
1538 if (timespecisset(&np->n_localmodtime) &&
1539 timespeccmp(&np->n_localmodtime, &ts, >=)) {
1540 NFSCL_DEBUG(4, "nfs_lookup: rename localmod "
1541 "stale attributes\n");
1542 attrflag = 0;
1543 }
1544 NFSUNLOCKNODE(np);
1545 if (attrflag)
1546 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
1547 0, 1);
1548 *vpp = newvp;
1549 return (0);
1550 }
1551
1552 if (flags & ISDOTDOT) {
1553 ltype = NFSVOPISLOCKED(dvp);
1554 error = vfs_busy(mp, MBF_NOWAIT);
1555 if (error != 0) {
1556 vfs_ref(mp);
1557 NFSVOPUNLOCK(dvp);
1558 error = vfs_busy(mp, 0);
1559 NFSVOPLOCK(dvp, ltype | LK_RETRY);
1560 vfs_rel(mp);
1561 if (error == 0 && VN_IS_DOOMED(dvp)) {
1562 vfs_unbusy(mp);
1563 error = ENOENT;
1564 }
1565 if (error != 0)
1566 return (error);
1567 }
1568 NFSVOPUNLOCK(dvp);
1569 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np,
1570 cnp->cn_lkflags);
1571 if (error == 0)
1572 newvp = NFSTOV(np);
1573 vfs_unbusy(mp);
1574 if (newvp != dvp)
1575 NFSVOPLOCK(dvp, ltype | LK_RETRY);
1576 if (VN_IS_DOOMED(dvp)) {
1577 if (error == 0) {
1578 if (newvp == dvp)
1579 vrele(newvp);
1580 else
1581 vput(newvp);
1582 }
1583 error = ENOENT;
1584 }
1585 if (error != 0)
1586 return (error);
1587 if (attrflag)
1588 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
1589 0, 1);
1590 } else if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1591 free(nfhp, M_NFSFH);
1592 vref(dvp);
1593 newvp = dvp;
1594 if (attrflag)
1595 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
1596 0, 1);
1597 } else {
1598 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np,
1599 cnp->cn_lkflags);
1600 if (error)
1601 return (error);
1602 newvp = NFSTOV(np);
1603 if (opennamed)
1604 vn_irflag_set_cond(newvp, VIRF_NAMEDATTR);
1605 /*
1606 * If n_localmodtime >= time before RPC, then
1607 * a file modification operation, such as
1608 * VOP_SETATTR() of size, has occurred while
1609 * the Lookup RPC and acquisition of the vnode
1610 * happened. As such, the attributes might
1611 * be stale, with possibly an incorrect size.
1612 */
1613 NFSLOCKNODE(np);
1614 if (timespecisset(&np->n_localmodtime) &&
1615 timespeccmp(&np->n_localmodtime, &ts, >=)) {
1616 NFSCL_DEBUG(4, "nfs_lookup: localmod "
1617 "stale attributes\n");
1618 attrflag = 0;
1619 }
1620 NFSUNLOCKNODE(np);
1621 if (attrflag)
1622 (void)nfscl_loadattrcache(&newvp, &nfsva, NULL,
1623 0, 1);
1624 else if ((flags & (ISLASTCN | ISOPEN)) ==
1625 (ISLASTCN | ISOPEN) &&
1626 !(np->n_flag & NMODIFIED)) {
1627 /*
1628 * Flush the attribute cache when opening a
1629 * leaf node to ensure that fresh attributes
1630 * are fetched in nfs_open() since we did not
1631 * fetch attributes from the LOOKUP reply.
1632 */
1633 NFSLOCKNODE(np);
1634 np->n_attrstamp = 0;
1635 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1636 NFSUNLOCKNODE(np);
1637 }
1638 }
1639 if ((cnp->cn_flags & MAKEENTRY) && dvp != newvp &&
1640 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN)) &&
1641 attrflag != 0 && (newvp->v_type != VDIR || dattrflag != 0))
1642 cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
1643 newvp->v_type != VDIR ? NULL : &dnfsva.na_ctime);
1644 *vpp = newvp;
1645 return (0);
1646 }
1647
1648 /*
1649 * nfs read call.
1650 * Just call ncl_bioread() to do the work.
1651 */
1652 static int
nfs_read(struct vop_read_args * ap)1653 nfs_read(struct vop_read_args *ap)
1654 {
1655 struct vnode *vp = ap->a_vp;
1656
1657 switch (vp->v_type) {
1658 case VREG:
1659 return (ncl_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
1660 case VDIR:
1661 return (EISDIR);
1662 default:
1663 return (EOPNOTSUPP);
1664 }
1665 }
1666
1667 /*
1668 * nfs readlink call
1669 */
1670 static int
nfs_readlink(struct vop_readlink_args * ap)1671 nfs_readlink(struct vop_readlink_args *ap)
1672 {
1673 struct vnode *vp = ap->a_vp;
1674
1675 if (vp->v_type != VLNK)
1676 return (EINVAL);
1677 return (ncl_bioread(vp, ap->a_uio, 0, ap->a_cred));
1678 }
1679
1680 /*
1681 * Do a readlink rpc.
1682 * Called by ncl_doio() from below the buffer cache.
1683 */
1684 int
ncl_readlinkrpc(struct vnode * vp,struct uio * uiop,struct ucred * cred)1685 ncl_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1686 {
1687 int error, ret, attrflag;
1688 struct nfsvattr nfsva;
1689
1690 error = nfsrpc_readlink(vp, uiop, cred, uiop->uio_td, &nfsva,
1691 &attrflag);
1692 if (attrflag) {
1693 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
1694 if (ret && !error)
1695 error = ret;
1696 }
1697 if (error && NFS_ISV4(vp))
1698 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1699 return (error);
1700 }
1701
1702 /*
1703 * nfs read rpc call
1704 * Ditto above
1705 */
1706 int
ncl_readrpc(struct vnode * vp,struct uio * uiop,struct ucred * cred)1707 ncl_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1708 {
1709 int error, ret, attrflag;
1710 struct nfsvattr nfsva;
1711 struct nfsmount *nmp;
1712
1713 nmp = VFSTONFS(vp->v_mount);
1714 error = EIO;
1715 attrflag = 0;
1716 if (NFSHASPNFS(nmp))
1717 error = nfscl_doiods(vp, uiop, NULL, NULL,
1718 NFSV4OPEN_ACCESSREAD, 0, cred, uiop->uio_td);
1719 NFSCL_DEBUG(4, "readrpc: aft doiods=%d\n", error);
1720 if (error != 0 && error != EFAULT)
1721 error = nfsrpc_read(vp, uiop, cred, uiop->uio_td, &nfsva,
1722 &attrflag);
1723 if (attrflag) {
1724 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
1725 if (ret && !error)
1726 error = ret;
1727 }
1728 if (error && NFS_ISV4(vp))
1729 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1730 return (error);
1731 }
1732
1733 /*
1734 * nfs write call
1735 */
1736 int
ncl_writerpc(struct vnode * vp,struct uio * uiop,struct ucred * cred,int * iomode,int * must_commit,int called_from_strategy,int ioflag)1737 ncl_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
1738 int *iomode, int *must_commit, int called_from_strategy, int ioflag)
1739 {
1740 struct nfsvattr nfsva;
1741 int error, attrflag, ret;
1742 struct nfsmount *nmp;
1743
1744 nmp = VFSTONFS(vp->v_mount);
1745 error = EIO;
1746 attrflag = 0;
1747 if (NFSHASPNFS(nmp))
1748 error = nfscl_doiods(vp, uiop, iomode, must_commit,
1749 NFSV4OPEN_ACCESSWRITE, 0, cred, uiop->uio_td);
1750 NFSCL_DEBUG(4, "writerpc: aft doiods=%d\n", error);
1751 if (error != 0 && error != EFAULT)
1752 error = nfsrpc_write(vp, uiop, iomode, must_commit, cred,
1753 uiop->uio_td, &nfsva, &attrflag, called_from_strategy,
1754 ioflag);
1755 if (attrflag) {
1756 if (VTONFS(vp)->n_flag & ND_NFSV4)
1757 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 1, 1);
1758 else
1759 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
1760 if (ret && !error)
1761 error = ret;
1762 }
1763 if (DOINGASYNC(vp))
1764 *iomode = NFSWRITE_FILESYNC;
1765 if (error && NFS_ISV4(vp))
1766 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1767 return (error);
1768 }
1769
1770 /*
1771 * nfs mknod rpc
1772 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1773 * mode set to specify the file type and the size field for rdev.
1774 */
1775 static int
nfs_mknodrpc(struct vnode * dvp,struct vnode ** vpp,struct componentname * cnp,struct vattr * vap)1776 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1777 struct vattr *vap)
1778 {
1779 struct nfsvattr nfsva, dnfsva;
1780 struct vnode *newvp = NULL;
1781 struct nfsnode *np = NULL, *dnp;
1782 struct nfsfh *nfhp;
1783 struct vattr vattr;
1784 int error = 0, attrflag, dattrflag;
1785 u_int32_t rdev;
1786
1787 if (VATTR_ISDEV(vap))
1788 rdev = vap->va_rdev;
1789 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1790 rdev = 0xffffffff;
1791 else
1792 return (EOPNOTSUPP);
1793 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1794 return (error);
1795 error = nfsrpc_mknod(dvp, cnp->cn_nameptr, cnp->cn_namelen, vap,
1796 rdev, vap->va_type, cnp->cn_cred, curthread, &dnfsva,
1797 &nfsva, &nfhp, &attrflag, &dattrflag);
1798 if (!error) {
1799 if (!nfhp)
1800 (void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1801 cnp->cn_namelen, cnp->cn_cred, curthread,
1802 &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag, 0);
1803 if (nfhp)
1804 error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1805 curthread, &np, LK_EXCLUSIVE);
1806 }
1807 if (dattrflag)
1808 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
1809 if (!error) {
1810 newvp = NFSTOV(np);
1811 if (attrflag != 0) {
1812 error = nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
1813 if (error != 0)
1814 vput(newvp);
1815 }
1816 }
1817 if (!error) {
1818 *vpp = newvp;
1819 } else if (NFS_ISV4(dvp)) {
1820 error = nfscl_maperr(curthread, error, vap->va_uid,
1821 vap->va_gid);
1822 }
1823 dnp = VTONFS(dvp);
1824 NFSLOCKNODE(dnp);
1825 dnp->n_flag |= NMODIFIED;
1826 if (!dattrflag) {
1827 dnp->n_attrstamp = 0;
1828 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1829 }
1830 NFSUNLOCKNODE(dnp);
1831 return (error);
1832 }
1833
1834 /*
1835 * nfs mknod vop
1836 * just call nfs_mknodrpc() to do the work.
1837 */
1838 /* ARGSUSED */
1839 static int
nfs_mknod(struct vop_mknod_args * ap)1840 nfs_mknod(struct vop_mknod_args *ap)
1841 {
1842 return (nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap));
1843 }
1844
1845 static struct mtx nfs_cverf_mtx;
1846 MTX_SYSINIT(nfs_cverf_mtx, &nfs_cverf_mtx, "NFS create verifier mutex",
1847 MTX_DEF);
1848
1849 static nfsquad_t
nfs_get_cverf(void)1850 nfs_get_cverf(void)
1851 {
1852 static nfsquad_t cverf;
1853 nfsquad_t ret;
1854 static int cverf_initialized = 0;
1855
1856 mtx_lock(&nfs_cverf_mtx);
1857 if (cverf_initialized == 0) {
1858 cverf.lval[0] = arc4random();
1859 cverf.lval[1] = arc4random();
1860 cverf_initialized = 1;
1861 } else
1862 cverf.qval++;
1863 ret = cverf;
1864 mtx_unlock(&nfs_cverf_mtx);
1865
1866 return (ret);
1867 }
1868
1869 /*
1870 * nfs file create call
1871 */
1872 static int
nfs_create(struct vop_create_args * ap)1873 nfs_create(struct vop_create_args *ap)
1874 {
1875 struct vnode *dvp = ap->a_dvp;
1876 struct vattr *vap = ap->a_vap;
1877 struct componentname *cnp = ap->a_cnp;
1878 struct nfsnode *np = NULL, *dnp;
1879 struct vnode *newvp = NULL;
1880 struct nfsmount *nmp;
1881 struct nfsvattr dnfsva, nfsva;
1882 struct nfsfh *nfhp;
1883 nfsquad_t cverf;
1884 int error = 0, attrflag, dattrflag, fmode = 0;
1885 struct vattr vattr;
1886 bool is_nameddir, needs_nameddir, opennamed;
1887
1888 /*
1889 * Oops, not for me..
1890 */
1891 if (vap->va_type == VSOCK)
1892 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1893
1894 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1895 return (error);
1896 if (vap->va_vaflags & VA_EXCLUSIVE)
1897 fmode |= O_EXCL;
1898 dnp = VTONFS(dvp);
1899 nmp = VFSTONFS(dvp->v_mount);
1900 needs_nameddir = false;
1901 if (NFSHASNFSV4(nmp) && NFSHASNFSV4N(nmp)) {
1902 opennamed = (cnp->cn_flags & (OPENNAMED | ISLASTCN)) ==
1903 (OPENNAMED | ISLASTCN);
1904 is_nameddir = (vn_irflag_read(dvp) & VIRF_NAMEDDIR) != 0;
1905 if (opennamed || is_nameddir) {
1906 cnp->cn_flags &= ~MAKEENTRY;
1907 if (!is_nameddir)
1908 needs_nameddir = true;
1909 }
1910 }
1911
1912 /*
1913 * If the named attribute directory is needed, acquire it now.
1914 */
1915 if (needs_nameddir) {
1916 KASSERT(dnp->n_v4 == NULL, ("nfs_create: O_NAMEDATTR when"
1917 " n_v4 not NULL"));
1918 error = nfs_get_namedattrdir(dvp, cnp, &newvp);
1919 if (error != 0)
1920 return (error);
1921 dvp = newvp;
1922 dnp = VTONFS(dvp);
1923 newvp = NULL;
1924 }
1925
1926 again:
1927 /* For NFSv4, wait until any remove is done. */
1928 NFSLOCKNODE(dnp);
1929 while (NFSHASNFSV4(nmp) && (dnp->n_flag & NREMOVEINPROG)) {
1930 dnp->n_flag |= NREMOVEWANT;
1931 (void) msleep((caddr_t)dnp, &dnp->n_mtx, PZERO, "nfscrt", 0);
1932 }
1933 NFSUNLOCKNODE(dnp);
1934
1935 cverf = nfs_get_cverf();
1936 error = nfsrpc_create(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1937 vap, cverf, fmode, cnp->cn_cred, curthread, &dnfsva, &nfsva,
1938 &nfhp, &attrflag, &dattrflag);
1939 if (!error) {
1940 if (nfhp == NULL)
1941 (void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1942 cnp->cn_namelen, cnp->cn_cred, curthread,
1943 &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag, 0);
1944 if (nfhp != NULL)
1945 error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1946 curthread, &np, LK_EXCLUSIVE);
1947 }
1948 if (dattrflag)
1949 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
1950 if (!error) {
1951 newvp = NFSTOV(np);
1952 if (attrflag == 0)
1953 error = nfsrpc_getattr(newvp, cnp->cn_cred, curthread,
1954 &nfsva);
1955 if (error == 0)
1956 error = nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
1957 }
1958 if (error) {
1959 if (newvp != NULL) {
1960 vput(newvp);
1961 newvp = NULL;
1962 }
1963 if (NFS_ISV34(dvp) && (fmode & O_EXCL) &&
1964 error == NFSERR_NOTSUPP) {
1965 fmode &= ~O_EXCL;
1966 goto again;
1967 }
1968 } else if (NFS_ISV34(dvp) && (fmode & O_EXCL)) {
1969 if (nfscl_checksattr(vap, &nfsva)) {
1970 error = nfsrpc_setattr(newvp, vap, NULL, cnp->cn_cred,
1971 curthread, &nfsva, &attrflag);
1972 if (error && (vap->va_uid != (uid_t)VNOVAL ||
1973 vap->va_gid != (gid_t)VNOVAL)) {
1974 /* try again without setting uid/gid */
1975 vap->va_uid = (uid_t)VNOVAL;
1976 vap->va_gid = (uid_t)VNOVAL;
1977 error = nfsrpc_setattr(newvp, vap, NULL,
1978 cnp->cn_cred, curthread, &nfsva, &attrflag);
1979 }
1980 if (attrflag)
1981 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
1982 0, 1);
1983 if (error != 0)
1984 vput(newvp);
1985 }
1986 }
1987 if (!error) {
1988 if ((cnp->cn_flags & MAKEENTRY) && attrflag) {
1989 if (dvp != newvp)
1990 cache_enter_time(dvp, newvp, cnp,
1991 &nfsva.na_ctime, NULL);
1992 else
1993 printf("nfs_create: bogus NFS server returned "
1994 "the directory as the new file object\n");
1995 }
1996 *ap->a_vpp = newvp;
1997 } else if (NFS_ISV4(dvp)) {
1998 error = nfscl_maperr(curthread, error, vap->va_uid,
1999 vap->va_gid);
2000 }
2001 NFSLOCKNODE(dnp);
2002 dnp->n_flag |= NMODIFIED;
2003 if (!dattrflag) {
2004 dnp->n_attrstamp = 0;
2005 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2006 }
2007 NFSUNLOCKNODE(dnp);
2008 if (needs_nameddir)
2009 vput(dvp);
2010 return (error);
2011 }
2012
2013 /*
2014 * nfs file remove call
2015 * To try and make nfs semantics closer to ufs semantics, a file that has
2016 * other processes using the vnode is renamed instead of removed and then
2017 * removed later on the last close.
2018 * - If v_usecount > 1
2019 * If a rename is not already in the works
2020 * call nfs_sillyrename() to set it up
2021 * else
2022 * do the remove rpc
2023 */
2024 static int
nfs_remove(struct vop_remove_args * ap)2025 nfs_remove(struct vop_remove_args *ap)
2026 {
2027 struct vnode *vp = ap->a_vp;
2028 struct vnode *dvp = ap->a_dvp;
2029 struct componentname *cnp = ap->a_cnp;
2030 struct nfsnode *np = VTONFS(vp);
2031 int error = 0;
2032 struct vattr vattr;
2033 struct nfsmount *nmp;
2034
2035 KASSERT(vrefcnt(vp) > 0, ("nfs_remove: bad v_usecount"));
2036 if (vp->v_type == VDIR)
2037 error = EPERM;
2038 else if (vrefcnt(vp) == 1 || (np->n_sillyrename &&
2039 VOP_GETATTR(vp, &vattr, cnp->cn_cred) == 0 &&
2040 vattr.va_nlink > 1)) {
2041 nmp = VFSTONFS(vp->v_mount);
2042 /*
2043 * Purge the name cache so that the chance of a lookup for
2044 * the name succeeding while the remove is in progress is
2045 * minimized. Without node locking it can still happen, such
2046 * that an I/O op returns ESTALE, but since you get this if
2047 * another host removes the file..
2048 */
2049 cache_purge(vp);
2050 /*
2051 * throw away biocache buffers, mainly to avoid
2052 * unnecessary delayed writes later.
2053 * Flushing here would be more correct for the case
2054 * where nfs_close() did not do a flush. However, it
2055 * could be a large performance hit for some servers
2056 * and only matters when the file name being removed is
2057 * one of multiple hard links.
2058 */
2059 if (!NFSHASNFSV4(nmp) || !NFSHASNFSV4N(nmp) ||
2060 (nmp->nm_flag & NFSMNT_NOCTO) == 0)
2061 error = ncl_vinvalbuf(vp, 0, curthread, 1);
2062 if (error != EINTR && error != EIO)
2063 /* Do the rpc */
2064 error = nfs_removerpc(dvp, vp, cnp->cn_nameptr,
2065 cnp->cn_namelen, cnp->cn_cred, curthread, false);
2066 /*
2067 * Kludge City: If the first reply to the remove rpc is lost..
2068 * the reply to the retransmitted request will be ENOENT
2069 * since the file was in fact removed
2070 * Therefore, we cheat and return success.
2071 */
2072 if (error == ENOENT)
2073 error = 0;
2074 } else if (!np->n_sillyrename)
2075 error = nfs_sillyrename(dvp, vp, cnp);
2076 NFSLOCKNODE(np);
2077 np->n_attrstamp = 0;
2078 NFSUNLOCKNODE(np);
2079 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
2080 return (error);
2081 }
2082
2083 /*
2084 * nfs file remove rpc called from nfs_inactive
2085 */
2086 int
ncl_removeit(struct sillyrename * sp,struct vnode * vp)2087 ncl_removeit(struct sillyrename *sp, struct vnode *vp)
2088 {
2089 /*
2090 * Make sure that the directory vnode is still valid.
2091 * XXX we should lock sp->s_dvp here.
2092 */
2093 if (sp->s_dvp->v_type == VBAD)
2094 return (0);
2095 return (nfs_removerpc(sp->s_dvp, vp, sp->s_name, sp->s_namlen,
2096 sp->s_cred, NULL, true));
2097 }
2098
2099 /*
2100 * Handle the nfsremove_status reply from the RPC function.
2101 */
2102 static void
nfs_removestatus(struct vnode * vp,nfsremove_status file_status,bool silly,struct thread * td)2103 nfs_removestatus(struct vnode *vp, nfsremove_status file_status,
2104 bool silly, struct thread *td)
2105 {
2106
2107 switch (file_status) {
2108 case NLINK_ZERO:
2109 /* Get rid of any delegation. */
2110 nfscl_delegreturnvp(vp, false, td);
2111 /* FALLTHROUGH */
2112 case DELETED:
2113 /* Throw away buffer cache blocks. */
2114 (void)ncl_vinvalbuf(vp, 0, td, 1);
2115 break;
2116 case VALID:
2117 /* Nothing to do, delegation is still ok. */
2118 break;
2119 default:
2120 break;
2121 }
2122 }
2123
2124 /*
2125 * Nfs remove rpc, called from nfs_remove() and ncl_removeit().
2126 */
2127 static int
nfs_removerpc(struct vnode * dvp,struct vnode * vp,char * name,int namelen,struct ucred * cred,struct thread * td,bool silly)2128 nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
2129 int namelen, struct ucred *cred, struct thread *td, bool silly)
2130 {
2131 struct nfsvattr dnfsva, nfsva;
2132 struct nfsnode *dnp = VTONFS(dvp);
2133 struct nfsmount *nmp;
2134 int attrflag, error = 0, dattrflag;
2135 nfsremove_status file_status;
2136
2137 nmp = VFSTONFS(dvp->v_mount);
2138 NFSLOCKNODE(dnp);
2139 dnp->n_flag |= NREMOVEINPROG;
2140 NFSUNLOCKNODE(dnp);
2141 error = nfsrpc_remove(dvp, name, namelen, vp, &nfsva, &attrflag,
2142 &file_status, &dnfsva, &dattrflag, cred, td);
2143 NFSLOCKNODE(dnp);
2144 if ((dnp->n_flag & NREMOVEWANT)) {
2145 dnp->n_flag &= ~(NREMOVEWANT | NREMOVEINPROG);
2146 NFSUNLOCKNODE(dnp);
2147 wakeup((caddr_t)dnp);
2148 } else {
2149 dnp->n_flag &= ~NREMOVEINPROG;
2150 NFSUNLOCKNODE(dnp);
2151 }
2152
2153 if (NFSHASNFSV4(nmp) && NFSHASNFSV4N(nmp)) {
2154 if (file_status != DELETED && attrflag != 0)
2155 (void)nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
2156 if ((nmp->nm_flag & NFSMNT_NOCTO) != 0)
2157 nfs_removestatus(vp, file_status, silly, td);
2158 }
2159
2160 if (dattrflag != 0)
2161 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
2162 NFSLOCKNODE(dnp);
2163 dnp->n_flag |= NMODIFIED;
2164 if (dattrflag == 0) {
2165 dnp->n_attrstamp = 0;
2166 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2167 }
2168 NFSUNLOCKNODE(dnp);
2169 if (error && NFS_ISV4(dvp))
2170 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2171 return (error);
2172 }
2173
2174 /*
2175 * nfs file rename call
2176 */
2177 static int
nfs_rename(struct vop_rename_args * ap)2178 nfs_rename(struct vop_rename_args *ap)
2179 {
2180 struct vnode *fvp = ap->a_fvp;
2181 struct vnode *tvp = ap->a_tvp;
2182 struct vnode *fdvp = ap->a_fdvp;
2183 struct vnode *tdvp = ap->a_tdvp;
2184 struct componentname *tcnp = ap->a_tcnp;
2185 struct componentname *fcnp = ap->a_fcnp;
2186 struct nfsnode *fnp = VTONFS(ap->a_fvp);
2187 struct nfsnode *tdnp = VTONFS(ap->a_tdvp);
2188 struct nfsv4node *newv4 = NULL;
2189 struct nfsmount *nmp;
2190 int error;
2191
2192 /* Check for cross-device rename */
2193 if ((fvp->v_mount != tdvp->v_mount) ||
2194 (tvp && (fvp->v_mount != tvp->v_mount))) {
2195 error = EXDEV;
2196 goto out;
2197 }
2198 nmp = VFSTONFS(fvp->v_mount);
2199
2200 if (fvp == tvp) {
2201 printf("nfs_rename: fvp == tvp (can't happen)\n");
2202 error = 0;
2203 goto out;
2204 }
2205 if ((error = NFSVOPLOCK(fvp, LK_EXCLUSIVE)) != 0)
2206 goto out;
2207
2208 /*
2209 * We have to flush B_DELWRI data prior to renaming
2210 * the file. If we don't, the delayed-write buffers
2211 * can be flushed out later after the file has gone stale
2212 * under NFSV3. NFSV2 does not have this problem because
2213 * ( as far as I can tell ) it flushes dirty buffers more
2214 * often.
2215 *
2216 * Skip the rename operation if the fsync fails, this can happen
2217 * due to the server's volume being full, when we pushed out data
2218 * that was written back to our cache earlier. Not checking for
2219 * this condition can result in potential (silent) data loss.
2220 */
2221 if ((nmp->nm_flag & NFSMNT_NOCTO) == 0 || !NFSHASNFSV4(nmp) ||
2222 !NFSHASNFSV4N(nmp) || nfscl_mustflush(fvp) != 0)
2223 error = VOP_FSYNC(fvp, MNT_WAIT, curthread);
2224 NFSVOPUNLOCK(fvp);
2225 if (error == 0 && tvp != NULL && ((nmp->nm_flag & NFSMNT_NOCTO) == 0 ||
2226 !NFSHASNFSV4(nmp) || !NFSHASNFSV4N(nmp) ||
2227 nfscl_mustflush(tvp) != 0))
2228 error = VOP_FSYNC(tvp, MNT_WAIT, curthread);
2229 if (error != 0)
2230 goto out;
2231
2232 /*
2233 * If the tvp exists and is in use, sillyrename it before doing the
2234 * rename of the new file over it.
2235 * XXX Can't sillyrename a directory.
2236 */
2237 if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
2238 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
2239 vput(tvp);
2240 tvp = NULL;
2241 }
2242
2243 error = nfs_renamerpc(fdvp, fvp, fcnp->cn_nameptr, fcnp->cn_namelen,
2244 tdvp, tvp, tcnp->cn_nameptr, tcnp->cn_namelen, false, tcnp->cn_cred,
2245 curthread);
2246
2247 if (error == 0 && NFS_ISV4(tdvp)) {
2248 /*
2249 * For NFSv4, check to see if it is the same name and
2250 * replace the name, if it is different.
2251 */
2252 newv4 = malloc(
2253 sizeof (struct nfsv4node) +
2254 tdnp->n_fhp->nfh_len + tcnp->cn_namelen - 1,
2255 M_NFSV4NODE, M_WAITOK);
2256 NFSLOCKNODE(tdnp);
2257 NFSLOCKNODE(fnp);
2258 if (fnp->n_v4 != NULL && fvp->v_type == VREG &&
2259 (fnp->n_v4->n4_namelen != tcnp->cn_namelen ||
2260 NFSBCMP(tcnp->cn_nameptr, NFS4NODENAME(fnp->n_v4),
2261 tcnp->cn_namelen) ||
2262 tdnp->n_fhp->nfh_len != fnp->n_v4->n4_fhlen ||
2263 NFSBCMP(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
2264 tdnp->n_fhp->nfh_len))) {
2265 free(fnp->n_v4, M_NFSV4NODE);
2266 fnp->n_v4 = newv4;
2267 newv4 = NULL;
2268 fnp->n_v4->n4_fhlen = tdnp->n_fhp->nfh_len;
2269 fnp->n_v4->n4_namelen = tcnp->cn_namelen;
2270 NFSBCOPY(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
2271 tdnp->n_fhp->nfh_len);
2272 NFSBCOPY(tcnp->cn_nameptr,
2273 NFS4NODENAME(fnp->n_v4), tcnp->cn_namelen);
2274 }
2275 NFSUNLOCKNODE(tdnp);
2276 NFSUNLOCKNODE(fnp);
2277 if (newv4 != NULL)
2278 free(newv4, M_NFSV4NODE);
2279 }
2280
2281 if (fvp->v_type == VDIR) {
2282 if (tvp != NULL && tvp->v_type == VDIR)
2283 cache_purge(tdvp);
2284 cache_purge(fdvp);
2285 }
2286
2287 out:
2288 if (tdvp == tvp)
2289 vrele(tdvp);
2290 else
2291 vput(tdvp);
2292 if (tvp)
2293 vput(tvp);
2294 vrele(fdvp);
2295 vrele(fvp);
2296 /*
2297 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
2298 */
2299 if (error == ENOENT)
2300 error = 0;
2301 return (error);
2302 }
2303
2304 /*
2305 * nfs file rename rpc called from nfs_remove() above
2306 */
2307 static int
nfs_renameit(struct vnode * sdvp,struct vnode * svp,struct componentname * scnp,struct sillyrename * sp)2308 nfs_renameit(struct vnode *sdvp, struct vnode *svp, struct componentname *scnp,
2309 struct sillyrename *sp)
2310 {
2311
2312 return (nfs_renamerpc(sdvp, svp, scnp->cn_nameptr, scnp->cn_namelen,
2313 sdvp, NULL, sp->s_name, sp->s_namlen, true, scnp->cn_cred,
2314 curthread));
2315 }
2316
2317 /*
2318 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
2319 */
2320 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)2321 nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp, char *fnameptr,
2322 int fnamelen, struct vnode *tdvp, struct vnode *tvp, char *tnameptr,
2323 int tnamelen, bool silly, struct ucred *cred, struct thread *td)
2324 {
2325 struct nfsvattr fnfsva, tnfsva, tvpnfsva;
2326 struct nfsnode *fdnp = VTONFS(fdvp);
2327 struct nfsnode *tdnp = VTONFS(tdvp);
2328 struct nfsmount *nmp;
2329 int error = 0, fattrflag, tattrflag, tvpattrflag;
2330 nfsremove_status tvp_status;
2331
2332 nmp = VFSTONFS(fdvp->v_mount);
2333 error = nfsrpc_rename(fdvp, fvp, fnameptr, fnamelen, tdvp, tvp,
2334 tnameptr, tnamelen, &tvp_status, &fnfsva, &tnfsva, &fattrflag,
2335 &tattrflag, &tvpnfsva, &tvpattrflag, cred, td);
2336 NFSLOCKNODE(fdnp);
2337 fdnp->n_flag |= NMODIFIED;
2338 if (fattrflag != 0) {
2339 NFSUNLOCKNODE(fdnp);
2340 (void) nfscl_loadattrcache(&fdvp, &fnfsva, NULL, 0, 1);
2341 } else {
2342 fdnp->n_attrstamp = 0;
2343 NFSUNLOCKNODE(fdnp);
2344 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(fdvp);
2345 }
2346 NFSLOCKNODE(tdnp);
2347 tdnp->n_flag |= NMODIFIED;
2348 if (tattrflag != 0) {
2349 NFSUNLOCKNODE(tdnp);
2350 (void) nfscl_loadattrcache(&tdvp, &tnfsva, NULL, 0, 1);
2351 } else {
2352 tdnp->n_attrstamp = 0;
2353 NFSUNLOCKNODE(tdnp);
2354 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
2355 }
2356
2357 if (tvp != NULL) {
2358 if (NFSHASNFSV4(nmp) && NFSHASNFSV4N(nmp) &&
2359 (nmp->nm_flag & NFSMNT_NOCTO) != 0)
2360 nfs_removestatus(tvp, tvp_status, silly, td);
2361 if (!silly && tvpattrflag != 0)
2362 (void)nfscl_loadattrcache(&tvp, &tvpnfsva, NULL, 0, 1);
2363 }
2364
2365 if (error && NFS_ISV4(fdvp))
2366 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2367 return (error);
2368 }
2369
2370 /*
2371 * nfs hard link create call
2372 */
2373 static int
nfs_link(struct vop_link_args * ap)2374 nfs_link(struct vop_link_args *ap)
2375 {
2376 struct vnode *vp = ap->a_vp;
2377 struct vnode *tdvp = ap->a_tdvp;
2378 struct componentname *cnp = ap->a_cnp;
2379 struct nfsnode *np, *tdnp;
2380 struct nfsvattr nfsva, dnfsva;
2381 int error = 0, attrflag, dattrflag;
2382
2383 /*
2384 * Push all writes to the server, so that the attribute cache
2385 * doesn't get "out of sync" with the server.
2386 * XXX There should be a better way!
2387 */
2388 #ifdef notnow
2389 VOP_FSYNC(vp, MNT_WAIT, curthread);
2390 #endif
2391
2392 error = nfsrpc_link(tdvp, vp, cnp->cn_nameptr, cnp->cn_namelen,
2393 cnp->cn_cred, curthread, &dnfsva, &nfsva, &attrflag, &dattrflag);
2394 tdnp = VTONFS(tdvp);
2395 NFSLOCKNODE(tdnp);
2396 tdnp->n_flag |= NMODIFIED;
2397 if (dattrflag != 0) {
2398 NFSUNLOCKNODE(tdnp);
2399 (void) nfscl_loadattrcache(&tdvp, &dnfsva, NULL, 0, 1);
2400 } else {
2401 tdnp->n_attrstamp = 0;
2402 NFSUNLOCKNODE(tdnp);
2403 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
2404 }
2405 if (attrflag)
2406 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
2407 else {
2408 np = VTONFS(vp);
2409 NFSLOCKNODE(np);
2410 np->n_attrstamp = 0;
2411 NFSUNLOCKNODE(np);
2412 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
2413 }
2414 /*
2415 * If negative lookup caching is enabled, I might as well
2416 * add an entry for this node. Not necessary for correctness,
2417 * but if negative caching is enabled, then the system
2418 * must care about lookup caching hit rate, so...
2419 */
2420 if (VFSTONFS(vp->v_mount)->nm_negnametimeo != 0 &&
2421 (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
2422 if (tdvp != vp)
2423 cache_enter_time(tdvp, vp, cnp, &nfsva.na_ctime, NULL);
2424 else
2425 printf("nfs_link: bogus NFS server returned "
2426 "the directory as the new link\n");
2427 }
2428 if (error && NFS_ISV4(vp))
2429 error = nfscl_maperr(curthread, error, (uid_t)0,
2430 (gid_t)0);
2431 return (error);
2432 }
2433
2434 /*
2435 * nfs symbolic link create call
2436 */
2437 static int
nfs_symlink(struct vop_symlink_args * ap)2438 nfs_symlink(struct vop_symlink_args *ap)
2439 {
2440 struct vnode *dvp = ap->a_dvp;
2441 struct vattr *vap = ap->a_vap;
2442 struct componentname *cnp = ap->a_cnp;
2443 struct nfsvattr nfsva, dnfsva;
2444 struct nfsfh *nfhp;
2445 struct nfsnode *np = NULL, *dnp;
2446 struct vnode *newvp = NULL;
2447 int error = 0, attrflag, dattrflag, ret;
2448
2449 vap->va_type = VLNK;
2450 error = nfsrpc_symlink(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2451 ap->a_target, vap, cnp->cn_cred, curthread, &dnfsva,
2452 &nfsva, &nfhp, &attrflag, &dattrflag);
2453 if (nfhp) {
2454 ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, curthread,
2455 &np, LK_EXCLUSIVE);
2456 if (!ret)
2457 newvp = NFSTOV(np);
2458 else if (!error)
2459 error = ret;
2460 }
2461 if (newvp != NULL) {
2462 if (attrflag)
2463 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
2464 } else if (!error) {
2465 /*
2466 * If we do not have an error and we could not extract the
2467 * newvp from the response due to the request being NFSv2, we
2468 * have to do a lookup in order to obtain a newvp to return.
2469 */
2470 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2471 cnp->cn_cred, curthread, &np);
2472 if (!error)
2473 newvp = NFSTOV(np);
2474 }
2475 if (error) {
2476 if (newvp)
2477 vput(newvp);
2478 if (NFS_ISV4(dvp))
2479 error = nfscl_maperr(curthread, error,
2480 vap->va_uid, vap->va_gid);
2481 } else {
2482 *ap->a_vpp = newvp;
2483 }
2484
2485 dnp = VTONFS(dvp);
2486 NFSLOCKNODE(dnp);
2487 dnp->n_flag |= NMODIFIED;
2488 if (dattrflag != 0) {
2489 NFSUNLOCKNODE(dnp);
2490 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
2491 } else {
2492 dnp->n_attrstamp = 0;
2493 NFSUNLOCKNODE(dnp);
2494 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2495 }
2496 /*
2497 * If negative lookup caching is enabled, I might as well
2498 * add an entry for this node. Not necessary for correctness,
2499 * but if negative caching is enabled, then the system
2500 * must care about lookup caching hit rate, so...
2501 */
2502 if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2503 (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
2504 if (dvp != newvp)
2505 cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
2506 NULL);
2507 else
2508 printf("nfs_symlink: bogus NFS server returned "
2509 "the directory as the new file object\n");
2510 }
2511 return (error);
2512 }
2513
2514 /*
2515 * nfs make dir call
2516 */
2517 static int
nfs_mkdir(struct vop_mkdir_args * ap)2518 nfs_mkdir(struct vop_mkdir_args *ap)
2519 {
2520 struct vnode *dvp = ap->a_dvp;
2521 struct vattr *vap = ap->a_vap;
2522 struct componentname *cnp = ap->a_cnp;
2523 struct nfsnode *np = NULL, *dnp;
2524 struct vnode *newvp = NULL;
2525 struct vattr vattr;
2526 struct nfsfh *nfhp;
2527 struct nfsvattr nfsva, dnfsva;
2528 int error = 0, attrflag, dattrflag, ret;
2529
2530 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)) != 0)
2531 return (error);
2532 vap->va_type = VDIR;
2533 error = nfsrpc_mkdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2534 vap, cnp->cn_cred, curthread, &dnfsva, &nfsva, &nfhp,
2535 &attrflag, &dattrflag);
2536 dnp = VTONFS(dvp);
2537 NFSLOCKNODE(dnp);
2538 dnp->n_flag |= NMODIFIED;
2539 if (dattrflag != 0) {
2540 NFSUNLOCKNODE(dnp);
2541 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
2542 } else {
2543 dnp->n_attrstamp = 0;
2544 NFSUNLOCKNODE(dnp);
2545 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2546 }
2547 if (nfhp) {
2548 ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, curthread,
2549 &np, LK_EXCLUSIVE);
2550 if (!ret) {
2551 newvp = NFSTOV(np);
2552 if (attrflag)
2553 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
2554 0, 1);
2555 } else if (!error)
2556 error = ret;
2557 }
2558 if (!error && newvp == NULL) {
2559 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2560 cnp->cn_cred, curthread, &np);
2561 if (!error) {
2562 newvp = NFSTOV(np);
2563 if (newvp->v_type != VDIR)
2564 error = EEXIST;
2565 }
2566 }
2567 if (error) {
2568 if (newvp)
2569 vput(newvp);
2570 if (NFS_ISV4(dvp))
2571 error = nfscl_maperr(curthread, error,
2572 vap->va_uid, vap->va_gid);
2573 } else {
2574 /*
2575 * If negative lookup caching is enabled, I might as well
2576 * add an entry for this node. Not necessary for correctness,
2577 * but if negative caching is enabled, then the system
2578 * must care about lookup caching hit rate, so...
2579 */
2580 if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2581 (cnp->cn_flags & MAKEENTRY) &&
2582 attrflag != 0 && dattrflag != 0) {
2583 if (dvp != newvp)
2584 cache_enter_time(dvp, newvp, cnp,
2585 &nfsva.na_ctime, &dnfsva.na_ctime);
2586 else
2587 printf("nfs_mkdir: bogus NFS server returned "
2588 "the directory that the directory was "
2589 "created in as the new file object\n");
2590 }
2591 *ap->a_vpp = newvp;
2592 }
2593 return (error);
2594 }
2595
2596 /*
2597 * nfs remove directory call
2598 */
2599 static int
nfs_rmdir(struct vop_rmdir_args * ap)2600 nfs_rmdir(struct vop_rmdir_args *ap)
2601 {
2602 struct vnode *vp = ap->a_vp;
2603 struct vnode *dvp = ap->a_dvp;
2604 struct componentname *cnp = ap->a_cnp;
2605 struct nfsnode *dnp;
2606 struct nfsvattr dnfsva;
2607 int error, dattrflag;
2608
2609 if (dvp == vp)
2610 return (EINVAL);
2611 error = nfsrpc_rmdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2612 cnp->cn_cred, curthread, &dnfsva, &dattrflag);
2613 dnp = VTONFS(dvp);
2614 NFSLOCKNODE(dnp);
2615 dnp->n_flag |= NMODIFIED;
2616 if (dattrflag != 0) {
2617 NFSUNLOCKNODE(dnp);
2618 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
2619 } else {
2620 dnp->n_attrstamp = 0;
2621 NFSUNLOCKNODE(dnp);
2622 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2623 }
2624
2625 cache_purge(dvp);
2626 cache_purge(vp);
2627 if (error && NFS_ISV4(dvp))
2628 error = nfscl_maperr(curthread, error, (uid_t)0,
2629 (gid_t)0);
2630 /*
2631 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2632 */
2633 if (error == ENOENT)
2634 error = 0;
2635 return (error);
2636 }
2637
2638 /*
2639 * nfs readdir call
2640 */
2641 static int
nfs_readdir(struct vop_readdir_args * ap)2642 nfs_readdir(struct vop_readdir_args *ap)
2643 {
2644 struct vnode *vp = ap->a_vp;
2645 struct nfsnode *np = VTONFS(vp);
2646 struct uio *uio = ap->a_uio;
2647 ssize_t tresid, left;
2648 int error = 0;
2649 struct vattr vattr;
2650
2651 if (ap->a_eofflag != NULL)
2652 *ap->a_eofflag = 0;
2653 if (vp->v_type != VDIR)
2654 return(EPERM);
2655
2656 /*
2657 * First, check for hit on the EOF offset cache
2658 */
2659 NFSLOCKNODE(np);
2660 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2661 (np->n_flag & NMODIFIED) == 0) {
2662 NFSUNLOCKNODE(np);
2663 if (VOP_GETATTR(vp, &vattr, ap->a_cred) == 0) {
2664 NFSLOCKNODE(np);
2665 if ((NFS_ISV4(vp) && np->n_change == vattr.va_filerev) ||
2666 !NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
2667 NFSUNLOCKNODE(np);
2668 NFSINCRGLOBAL(nfsstatsv1.direofcache_hits);
2669 if (ap->a_eofflag != NULL)
2670 *ap->a_eofflag = 1;
2671 return (0);
2672 } else
2673 NFSUNLOCKNODE(np);
2674 }
2675 } else
2676 NFSUNLOCKNODE(np);
2677
2678 /*
2679 * NFS always guarantees that directory entries don't straddle
2680 * DIRBLKSIZ boundaries. As such, we need to limit the size
2681 * to an exact multiple of DIRBLKSIZ, to avoid copying a partial
2682 * directory entry.
2683 */
2684 left = uio->uio_resid % DIRBLKSIZ;
2685 if (left == uio->uio_resid)
2686 return (EINVAL);
2687 uio->uio_resid -= left;
2688
2689 /*
2690 * For readdirplus, if starting to read the directory,
2691 * purge the name cache, since it will be reloaded by
2692 * this directory read.
2693 * This removes potentially stale name cache entries.
2694 */
2695 if (uio->uio_offset == 0 &&
2696 (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_RDIRPLUS) != 0)
2697 cache_purge(vp);
2698
2699 /*
2700 * Call ncl_bioread() to do the real work.
2701 */
2702 tresid = uio->uio_resid;
2703 error = ncl_bioread(vp, uio, 0, ap->a_cred);
2704
2705 if (!error && uio->uio_resid == tresid) {
2706 NFSINCRGLOBAL(nfsstatsv1.direofcache_misses);
2707 if (ap->a_eofflag != NULL)
2708 *ap->a_eofflag = 1;
2709 }
2710
2711 /* Add the partial DIRBLKSIZ (left) back in. */
2712 uio->uio_resid += left;
2713 return (error);
2714 }
2715
2716 /*
2717 * Readdir rpc call.
2718 * Called from below the buffer cache by ncl_doio().
2719 */
2720 int
ncl_readdirrpc(struct vnode * vp,struct uio * uiop,struct ucred * cred,struct thread * td)2721 ncl_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2722 struct thread *td)
2723 {
2724 struct nfsvattr nfsva;
2725 nfsuint64 *cookiep, cookie;
2726 struct nfsnode *dnp = VTONFS(vp);
2727 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2728 int error = 0, eof, attrflag;
2729
2730 KASSERT(uiop->uio_iovcnt == 1 &&
2731 (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2732 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2733 ("nfs readdirrpc bad uio"));
2734
2735 /*
2736 * If there is no cookie, assume directory was stale.
2737 */
2738 ncl_dircookie_lock(dnp);
2739 NFSUNLOCKNODE(dnp);
2740 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2741 if (cookiep) {
2742 cookie = *cookiep;
2743 ncl_dircookie_unlock(dnp);
2744 } else {
2745 ncl_dircookie_unlock(dnp);
2746 return (NFSERR_BAD_COOKIE);
2747 }
2748
2749 if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2750 (void)ncl_fsinfo(nmp, vp, cred, td);
2751
2752 error = nfsrpc_readdir(vp, uiop, &cookie, cred, td, &nfsva,
2753 &attrflag, &eof);
2754 if (attrflag)
2755 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
2756
2757 if (!error) {
2758 /*
2759 * We are now either at the end of the directory or have filled
2760 * the block.
2761 */
2762 if (eof) {
2763 NFSLOCKNODE(dnp);
2764 dnp->n_direofoffset = uiop->uio_offset;
2765 NFSUNLOCKNODE(dnp);
2766 } else {
2767 if (uiop->uio_resid > 0)
2768 printf("EEK! readdirrpc resid > 0\n");
2769 ncl_dircookie_lock(dnp);
2770 NFSUNLOCKNODE(dnp);
2771 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2772 *cookiep = cookie;
2773 ncl_dircookie_unlock(dnp);
2774 }
2775 } else if (NFS_ISV4(vp)) {
2776 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2777 }
2778 return (error);
2779 }
2780
2781 /*
2782 * NFS V3 readdir plus RPC. Used in place of ncl_readdirrpc().
2783 */
2784 int
ncl_readdirplusrpc(struct vnode * vp,struct uio * uiop,struct ucred * cred,struct thread * td)2785 ncl_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2786 struct thread *td)
2787 {
2788 struct nfsvattr nfsva;
2789 nfsuint64 *cookiep, cookie;
2790 struct nfsnode *dnp = VTONFS(vp);
2791 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2792 int error = 0, attrflag, eof;
2793
2794 KASSERT(uiop->uio_iovcnt == 1 &&
2795 (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2796 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2797 ("nfs readdirplusrpc bad uio"));
2798
2799 /*
2800 * If there is no cookie, assume directory was stale.
2801 */
2802 ncl_dircookie_lock(dnp);
2803 NFSUNLOCKNODE(dnp);
2804 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2805 if (cookiep) {
2806 cookie = *cookiep;
2807 ncl_dircookie_unlock(dnp);
2808 } else {
2809 ncl_dircookie_unlock(dnp);
2810 return (NFSERR_BAD_COOKIE);
2811 }
2812
2813 if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2814 (void)ncl_fsinfo(nmp, vp, cred, td);
2815 error = nfsrpc_readdirplus(vp, uiop, &cookie, cred, td, &nfsva,
2816 &attrflag, &eof);
2817 if (attrflag)
2818 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
2819
2820 if (!error) {
2821 /*
2822 * We are now either at end of the directory or have filled the
2823 * the block.
2824 */
2825 if (eof) {
2826 NFSLOCKNODE(dnp);
2827 dnp->n_direofoffset = uiop->uio_offset;
2828 NFSUNLOCKNODE(dnp);
2829 } else {
2830 if (uiop->uio_resid > 0)
2831 printf("EEK! readdirplusrpc resid > 0\n");
2832 ncl_dircookie_lock(dnp);
2833 NFSUNLOCKNODE(dnp);
2834 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2835 *cookiep = cookie;
2836 ncl_dircookie_unlock(dnp);
2837 }
2838 } else if (NFS_ISV4(vp)) {
2839 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2840 }
2841 return (error);
2842 }
2843
2844 /*
2845 * Silly rename. To make the NFS filesystem that is stateless look a little
2846 * more like the "ufs" a remove of an active vnode is translated to a rename
2847 * to a funny looking filename that is removed by nfs_inactive on the
2848 * nfsnode. There is the potential for another process on a different client
2849 * to create the same funny name between the nfs_lookitup() fails and the
2850 * nfs_rename() completes, but...
2851 */
2852 static int
nfs_sillyrename(struct vnode * dvp,struct vnode * vp,struct componentname * cnp)2853 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2854 {
2855 struct sillyrename *sp;
2856 struct nfsnode *np;
2857 int error;
2858 short pid;
2859 unsigned int lticks;
2860
2861 cache_purge(dvp);
2862 np = VTONFS(vp);
2863 KASSERT(vp->v_type != VDIR, ("nfs: sillyrename dir"));
2864 sp = malloc(sizeof (struct sillyrename),
2865 M_NEWNFSREQ, M_WAITOK);
2866 sp->s_cred = crhold(cnp->cn_cred);
2867 sp->s_dvp = dvp;
2868 vref(dvp);
2869
2870 /*
2871 * Fudge together a funny name.
2872 * Changing the format of the funny name to accommodate more
2873 * sillynames per directory.
2874 * The name is now changed to .nfs.<ticks>.<pid>.4, where ticks is
2875 * CPU ticks since boot.
2876 */
2877 pid = curthread->td_proc->p_pid;
2878 lticks = (unsigned int)ticks;
2879 for ( ; ; ) {
2880 sp->s_namlen = sprintf(sp->s_name,
2881 ".nfs.%08x.%04x4.4", lticks,
2882 pid);
2883 if (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2884 curthread, NULL))
2885 break;
2886 lticks++;
2887 }
2888 error = nfs_renameit(dvp, vp, cnp, sp);
2889 if (error)
2890 goto bad;
2891 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2892 curthread, &np);
2893 np->n_sillyrename = sp;
2894 return (0);
2895 bad:
2896 vrele(sp->s_dvp);
2897 crfree(sp->s_cred);
2898 free(sp, M_NEWNFSREQ);
2899 return (error);
2900 }
2901
2902 /*
2903 * Look up a file name and optionally either update the file handle or
2904 * allocate an nfsnode, depending on the value of npp.
2905 * npp == NULL --> just do the lookup
2906 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2907 * handled too
2908 * *npp != NULL --> update the file handle in the vnode
2909 */
2910 static int
nfs_lookitup(struct vnode * dvp,char * name,int len,struct ucred * cred,struct thread * td,struct nfsnode ** npp)2911 nfs_lookitup(struct vnode *dvp, char *name, int len, struct ucred *cred,
2912 struct thread *td, struct nfsnode **npp)
2913 {
2914 struct vnode *newvp = NULL, *vp;
2915 struct nfsnode *np, *dnp = VTONFS(dvp);
2916 struct nfsfh *nfhp, *onfhp;
2917 struct nfsvattr nfsva, dnfsva;
2918 struct componentname cn;
2919 int error = 0, attrflag, dattrflag;
2920 u_int hash;
2921 struct timespec ts;
2922
2923 nanouptime(&ts);
2924 error = nfsrpc_lookup(dvp, name, len, cred, td, &dnfsva, &nfsva,
2925 &nfhp, &attrflag, &dattrflag, 0);
2926 if (dattrflag)
2927 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
2928 if (npp && !error) {
2929 if (*npp != NULL) {
2930 np = *npp;
2931 vp = NFSTOV(np);
2932 /*
2933 * For NFSv4, check to see if it is the same name and
2934 * replace the name, if it is different.
2935 */
2936 if (np->n_v4 != NULL && nfsva.na_type == VREG &&
2937 (np->n_v4->n4_namelen != len ||
2938 NFSBCMP(name, NFS4NODENAME(np->n_v4), len) ||
2939 dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
2940 NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2941 dnp->n_fhp->nfh_len))) {
2942 free(np->n_v4, M_NFSV4NODE);
2943 np->n_v4 = malloc(
2944 sizeof (struct nfsv4node) +
2945 dnp->n_fhp->nfh_len + len - 1,
2946 M_NFSV4NODE, M_WAITOK);
2947 np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
2948 np->n_v4->n4_namelen = len;
2949 NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2950 dnp->n_fhp->nfh_len);
2951 NFSBCOPY(name, NFS4NODENAME(np->n_v4), len);
2952 }
2953 hash = fnv_32_buf(nfhp->nfh_fh, nfhp->nfh_len,
2954 FNV1_32_INIT);
2955 onfhp = np->n_fhp;
2956 /*
2957 * Rehash node for new file handle.
2958 */
2959 vfs_hash_rehash(vp, hash);
2960 np->n_fhp = nfhp;
2961 if (onfhp != NULL)
2962 free(onfhp, M_NFSFH);
2963 newvp = NFSTOV(np);
2964 } else if (NFS_CMPFH(dnp, nfhp->nfh_fh, nfhp->nfh_len)) {
2965 free(nfhp, M_NFSFH);
2966 vref(dvp);
2967 newvp = dvp;
2968 } else {
2969 cn.cn_nameptr = name;
2970 cn.cn_namelen = len;
2971 error = nfscl_nget(dvp->v_mount, dvp, nfhp, &cn, td,
2972 &np, LK_EXCLUSIVE);
2973 if (error)
2974 return (error);
2975 newvp = NFSTOV(np);
2976 /*
2977 * If n_localmodtime >= time before RPC, then
2978 * a file modification operation, such as
2979 * VOP_SETATTR() of size, has occurred while
2980 * the Lookup RPC and acquisition of the vnode
2981 * happened. As such, the attributes might
2982 * be stale, with possibly an incorrect size.
2983 */
2984 NFSLOCKNODE(np);
2985 if (timespecisset(&np->n_localmodtime) &&
2986 timespeccmp(&np->n_localmodtime, &ts, >=)) {
2987 NFSCL_DEBUG(4, "nfs_lookitup: localmod "
2988 "stale attributes\n");
2989 attrflag = 0;
2990 }
2991 NFSUNLOCKNODE(np);
2992 }
2993 if (!attrflag && *npp == NULL) {
2994 if (newvp == dvp)
2995 vrele(newvp);
2996 else
2997 vput(newvp);
2998 return (ENOENT);
2999 }
3000 if (attrflag)
3001 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
3002 }
3003 if (npp && *npp == NULL) {
3004 if (error) {
3005 if (newvp) {
3006 if (newvp == dvp)
3007 vrele(newvp);
3008 else
3009 vput(newvp);
3010 }
3011 } else
3012 *npp = np;
3013 }
3014 if (error && NFS_ISV4(dvp))
3015 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
3016 return (error);
3017 }
3018
3019 /*
3020 * Nfs Version 3 and 4 commit rpc
3021 */
3022 int
ncl_commit(struct vnode * vp,u_quad_t offset,int cnt,struct ucred * cred,struct thread * td)3023 ncl_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
3024 struct thread *td)
3025 {
3026 struct nfsvattr nfsva;
3027 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3028 struct nfsnode *np;
3029 struct uio uio;
3030 int error, attrflag;
3031
3032 np = VTONFS(vp);
3033 error = EIO;
3034 attrflag = 0;
3035 if (NFSHASPNFS(nmp) && (np->n_flag & NDSCOMMIT) != 0) {
3036 uio.uio_offset = offset;
3037 uio.uio_resid = cnt;
3038 error = nfscl_doiods(vp, &uio, NULL, NULL,
3039 NFSV4OPEN_ACCESSWRITE, 1, cred, td);
3040 if (error != 0) {
3041 NFSLOCKNODE(np);
3042 np->n_flag &= ~NDSCOMMIT;
3043 NFSUNLOCKNODE(np);
3044 }
3045 }
3046 if (error != 0) {
3047 mtx_lock(&nmp->nm_mtx);
3048 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
3049 mtx_unlock(&nmp->nm_mtx);
3050 return (0);
3051 }
3052 mtx_unlock(&nmp->nm_mtx);
3053 error = nfsrpc_commit(vp, offset, cnt, cred, td, &nfsva,
3054 &attrflag);
3055 }
3056 if (attrflag != 0)
3057 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
3058 if (error != 0 && NFS_ISV4(vp))
3059 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
3060 return (error);
3061 }
3062
3063 /*
3064 * Strategy routine.
3065 * For async requests when nfsiod(s) are running, queue the request by
3066 * calling ncl_asyncio(), otherwise just all ncl_doio() to do the
3067 * request.
3068 */
3069 static int
nfs_strategy(struct vop_strategy_args * ap)3070 nfs_strategy(struct vop_strategy_args *ap)
3071 {
3072 struct buf *bp;
3073 struct vnode *vp;
3074 struct ucred *cr;
3075
3076 bp = ap->a_bp;
3077 vp = ap->a_vp;
3078 KASSERT(bp->b_vp == vp, ("missing b_getvp"));
3079 KASSERT(!(bp->b_flags & B_DONE),
3080 ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
3081
3082 if (vp->v_type == VREG && bp->b_blkno == bp->b_lblkno)
3083 bp->b_blkno = bp->b_lblkno * (vp->v_bufobj.bo_bsize /
3084 DEV_BSIZE);
3085 if (bp->b_iocmd == BIO_READ)
3086 cr = bp->b_rcred;
3087 else
3088 cr = bp->b_wcred;
3089
3090 /*
3091 * If the op is asynchronous and an i/o daemon is waiting
3092 * queue the request, wake it up and wait for completion
3093 * otherwise just do it ourselves.
3094 */
3095 if ((bp->b_flags & B_ASYNC) == 0 ||
3096 ncl_asyncio(VFSTONFS(vp->v_mount), bp, NOCRED, curthread))
3097 (void) ncl_doio(vp, bp, cr, curthread, 1);
3098 return (0);
3099 }
3100
3101 /*
3102 * fsync vnode op. Just call ncl_flush() with commit == 1.
3103 */
3104 /* ARGSUSED */
3105 static int
nfs_fsync(struct vop_fsync_args * ap)3106 nfs_fsync(struct vop_fsync_args *ap)
3107 {
3108
3109 if (ap->a_vp->v_type != VREG) {
3110 /*
3111 * For NFS, metadata is changed synchronously on the server,
3112 * so there is nothing to flush. Also, ncl_flush() clears
3113 * the NMODIFIED flag and that shouldn't be done here for
3114 * directories.
3115 */
3116 return (0);
3117 }
3118 return (ncl_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1, 0));
3119 }
3120
3121 /*
3122 * Flush all the blocks associated with a vnode.
3123 * Walk through the buffer pool and push any dirty pages
3124 * associated with the vnode.
3125 * If the called_from_renewthread argument is TRUE, it has been called
3126 * from the NFSv4 renew thread and, as such, cannot block indefinitely
3127 * waiting for a buffer write to complete.
3128 */
3129 int
ncl_flush(struct vnode * vp,int waitfor,struct thread * td,int commit,int called_from_renewthread)3130 ncl_flush(struct vnode *vp, int waitfor, struct thread *td,
3131 int commit, int called_from_renewthread)
3132 {
3133 struct nfsnode *np = VTONFS(vp);
3134 struct buf *bp;
3135 int i;
3136 struct buf *nbp;
3137 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3138 int error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
3139 int passone = 1, trycnt = 0;
3140 u_quad_t off, endoff, toff;
3141 struct ucred* wcred = NULL;
3142 struct buf **bvec = NULL;
3143 struct bufobj *bo;
3144 #ifndef NFS_COMMITBVECSIZ
3145 #define NFS_COMMITBVECSIZ 20
3146 #endif
3147 struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
3148 u_int bvecsize = 0, bveccount;
3149 struct timespec ts;
3150
3151 if (called_from_renewthread != 0)
3152 slptimeo = hz;
3153 if (nmp->nm_flag & NFSMNT_INT)
3154 slpflag = PCATCH;
3155 if (!commit)
3156 passone = 0;
3157 bo = &vp->v_bufobj;
3158 /*
3159 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
3160 * server, but has not been committed to stable storage on the server
3161 * yet. On the first pass, the byte range is worked out and the commit
3162 * rpc is done. On the second pass, bwrite() is called to do the
3163 * job.
3164 */
3165 again:
3166 off = (u_quad_t)-1;
3167 endoff = 0;
3168 bvecpos = 0;
3169 if (NFS_ISV34(vp) && commit) {
3170 if (bvec != NULL && bvec != bvec_on_stack)
3171 free(bvec, M_TEMP);
3172 /*
3173 * Count up how many buffers waiting for a commit.
3174 */
3175 bveccount = 0;
3176 BO_LOCK(bo);
3177 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
3178 if (!BUF_ISLOCKED(bp) &&
3179 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
3180 == (B_DELWRI | B_NEEDCOMMIT))
3181 bveccount++;
3182 }
3183 /*
3184 * Allocate space to remember the list of bufs to commit. It is
3185 * important to use M_NOWAIT here to avoid a race with nfs_write.
3186 * If we can't get memory (for whatever reason), we will end up
3187 * committing the buffers one-by-one in the loop below.
3188 */
3189 if (bveccount > NFS_COMMITBVECSIZ) {
3190 /*
3191 * Release the vnode interlock to avoid a lock
3192 * order reversal.
3193 */
3194 BO_UNLOCK(bo);
3195 bvec = (struct buf **)
3196 malloc(bveccount * sizeof(struct buf *),
3197 M_TEMP, M_NOWAIT);
3198 BO_LOCK(bo);
3199 if (bvec == NULL) {
3200 bvec = bvec_on_stack;
3201 bvecsize = NFS_COMMITBVECSIZ;
3202 } else
3203 bvecsize = bveccount;
3204 } else {
3205 bvec = bvec_on_stack;
3206 bvecsize = NFS_COMMITBVECSIZ;
3207 }
3208 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
3209 if (bvecpos >= bvecsize)
3210 break;
3211 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
3212 nbp = TAILQ_NEXT(bp, b_bobufs);
3213 continue;
3214 }
3215 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3216 (B_DELWRI | B_NEEDCOMMIT)) {
3217 BUF_UNLOCK(bp);
3218 nbp = TAILQ_NEXT(bp, b_bobufs);
3219 continue;
3220 }
3221 BO_UNLOCK(bo);
3222 bremfree(bp);
3223 /*
3224 * Work out if all buffers are using the same cred
3225 * so we can deal with them all with one commit.
3226 *
3227 * NOTE: we are not clearing B_DONE here, so we have
3228 * to do it later on in this routine if we intend to
3229 * initiate I/O on the bp.
3230 *
3231 * Note: to avoid loopback deadlocks, we do not
3232 * assign b_runningbufspace.
3233 */
3234 if (wcred == NULL)
3235 wcred = bp->b_wcred;
3236 else if (wcred != bp->b_wcred)
3237 wcred = NOCRED;
3238 vfs_busy_pages(bp, 0);
3239
3240 BO_LOCK(bo);
3241 /*
3242 * bp is protected by being locked, but nbp is not
3243 * and vfs_busy_pages() may sleep. We have to
3244 * recalculate nbp.
3245 */
3246 nbp = TAILQ_NEXT(bp, b_bobufs);
3247
3248 /*
3249 * A list of these buffers is kept so that the
3250 * second loop knows which buffers have actually
3251 * been committed. This is necessary, since there
3252 * may be a race between the commit rpc and new
3253 * uncommitted writes on the file.
3254 */
3255 bvec[bvecpos++] = bp;
3256 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
3257 bp->b_dirtyoff;
3258 if (toff < off)
3259 off = toff;
3260 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
3261 if (toff > endoff)
3262 endoff = toff;
3263 }
3264 BO_UNLOCK(bo);
3265 }
3266 if (bvecpos > 0) {
3267 /*
3268 * Commit data on the server, as required.
3269 * If all bufs are using the same wcred, then use that with
3270 * one call for all of them, otherwise commit each one
3271 * separately.
3272 */
3273 if (wcred != NOCRED)
3274 retv = ncl_commit(vp, off, (int)(endoff - off),
3275 wcred, td);
3276 else {
3277 retv = 0;
3278 for (i = 0; i < bvecpos; i++) {
3279 off_t off, size;
3280 bp = bvec[i];
3281 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
3282 bp->b_dirtyoff;
3283 size = (u_quad_t)(bp->b_dirtyend
3284 - bp->b_dirtyoff);
3285 retv = ncl_commit(vp, off, (int)size,
3286 bp->b_wcred, td);
3287 if (retv) break;
3288 }
3289 }
3290
3291 if (retv == NFSERR_STALEWRITEVERF)
3292 ncl_clearcommit(vp->v_mount);
3293
3294 /*
3295 * Now, either mark the blocks I/O done or mark the
3296 * blocks dirty, depending on whether the commit
3297 * succeeded.
3298 */
3299 for (i = 0; i < bvecpos; i++) {
3300 bp = bvec[i];
3301 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3302 if (!NFSCL_FORCEDISM(vp->v_mount) && retv) {
3303 /*
3304 * Error, leave B_DELWRI intact
3305 */
3306 vfs_unbusy_pages(bp);
3307 brelse(bp);
3308 } else {
3309 /*
3310 * Success, remove B_DELWRI ( bundirty() ).
3311 *
3312 * b_dirtyoff/b_dirtyend seem to be NFS
3313 * specific. We should probably move that
3314 * into bundirty(). XXX
3315 */
3316 bufobj_wref(bo);
3317 bp->b_flags |= B_ASYNC;
3318 bundirty(bp);
3319 bp->b_flags &= ~B_DONE;
3320 bp->b_ioflags &= ~BIO_ERROR;
3321 bp->b_dirtyoff = bp->b_dirtyend = 0;
3322 bufdone(bp);
3323 }
3324 }
3325 }
3326
3327 /*
3328 * Start/do any write(s) that are required.
3329 */
3330 loop:
3331 BO_LOCK(bo);
3332 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
3333 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
3334 if (waitfor != MNT_WAIT || passone)
3335 continue;
3336
3337 error = BUF_TIMELOCK(bp,
3338 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
3339 BO_LOCKPTR(bo), "nfsfsync", slpflag, slptimeo);
3340 if (error == 0) {
3341 BUF_UNLOCK(bp);
3342 goto loop;
3343 }
3344 if (error == ENOLCK) {
3345 error = 0;
3346 goto loop;
3347 }
3348 if (called_from_renewthread != 0) {
3349 /*
3350 * Return EIO so the flush will be retried
3351 * later.
3352 */
3353 error = EIO;
3354 goto done;
3355 }
3356 if (newnfs_sigintr(nmp, td)) {
3357 error = EINTR;
3358 goto done;
3359 }
3360 if (slpflag == PCATCH) {
3361 slpflag = 0;
3362 slptimeo = 2 * hz;
3363 }
3364 goto loop;
3365 }
3366 if ((bp->b_flags & B_DELWRI) == 0)
3367 panic("nfs_fsync: not dirty");
3368 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
3369 BUF_UNLOCK(bp);
3370 continue;
3371 }
3372 BO_UNLOCK(bo);
3373 bremfree(bp);
3374 bp->b_flags |= B_ASYNC;
3375 bwrite(bp);
3376 if (newnfs_sigintr(nmp, td)) {
3377 error = EINTR;
3378 goto done;
3379 }
3380 goto loop;
3381 }
3382 if (passone) {
3383 passone = 0;
3384 BO_UNLOCK(bo);
3385 goto again;
3386 }
3387 if (waitfor == MNT_WAIT) {
3388 while (bo->bo_numoutput) {
3389 error = bufobj_wwait(bo, slpflag, slptimeo);
3390 if (error) {
3391 BO_UNLOCK(bo);
3392 if (called_from_renewthread != 0) {
3393 /*
3394 * Return EIO so that the flush will be
3395 * retried later.
3396 */
3397 error = EIO;
3398 goto done;
3399 }
3400 error = newnfs_sigintr(nmp, td);
3401 if (error)
3402 goto done;
3403 if (slpflag == PCATCH) {
3404 slpflag = 0;
3405 slptimeo = 2 * hz;
3406 }
3407 BO_LOCK(bo);
3408 }
3409 }
3410 if (bo->bo_dirty.bv_cnt != 0 && commit) {
3411 BO_UNLOCK(bo);
3412 goto loop;
3413 }
3414 /*
3415 * Wait for all the async IO requests to drain
3416 */
3417 BO_UNLOCK(bo);
3418 } else
3419 BO_UNLOCK(bo);
3420 if (NFSHASPNFS(nmp)) {
3421 nfscl_layoutcommit(vp, td);
3422 /*
3423 * Invalidate the attribute cache, since writes to a DS
3424 * won't update the size attribute.
3425 */
3426 NFSLOCKNODE(np);
3427 np->n_attrstamp = 0;
3428 } else
3429 NFSLOCKNODE(np);
3430 if (np->n_flag & NWRITEERR) {
3431 error = np->n_error;
3432 np->n_flag &= ~NWRITEERR;
3433 }
3434 if (commit && bo->bo_dirty.bv_cnt == 0 &&
3435 bo->bo_numoutput == 0)
3436 np->n_flag &= ~NMODIFIED;
3437 NFSUNLOCKNODE(np);
3438 done:
3439 if (bvec != NULL && bvec != bvec_on_stack)
3440 free(bvec, M_TEMP);
3441 if (error == 0 && commit != 0 && waitfor == MNT_WAIT &&
3442 (bo->bo_dirty.bv_cnt != 0 || bo->bo_numoutput != 0)) {
3443 if (trycnt++ < 5) {
3444 /* try, try again... */
3445 passone = 1;
3446 wcred = NULL;
3447 bvec = NULL;
3448 bvecsize = 0;
3449 goto again;
3450 }
3451 vn_printf(vp, "ncl_flush failed");
3452 error = called_from_renewthread != 0 ? EIO : EBUSY;
3453 }
3454 if (error == 0) {
3455 nanouptime(&ts);
3456 NFSLOCKNODE(np);
3457 np->n_localmodtime = ts;
3458 NFSUNLOCKNODE(np);
3459 }
3460 return (error);
3461 }
3462
3463 /*
3464 * NFS advisory byte-level locks.
3465 */
3466 static int
nfs_advlock(struct vop_advlock_args * ap)3467 nfs_advlock(struct vop_advlock_args *ap)
3468 {
3469 struct vnode *vp = ap->a_vp;
3470 struct ucred *cred;
3471 struct nfsnode *np = VTONFS(ap->a_vp);
3472 struct proc *p = (struct proc *)ap->a_id;
3473 struct thread *td = curthread; /* XXX */
3474 struct vattr va;
3475 int ret, error;
3476 u_quad_t size;
3477 struct nfsmount *nmp;
3478
3479 error = NFSVOPLOCK(vp, LK_EXCLUSIVE);
3480 if (error != 0)
3481 return (EBADF);
3482 nmp = VFSTONFS(vp->v_mount);
3483 if (!NFS_ISV4(vp) || (nmp->nm_flag & NFSMNT_NOLOCKD) != 0) {
3484 if ((nmp->nm_flag & NFSMNT_NOLOCKD) != 0) {
3485 size = np->n_size;
3486 NFSVOPUNLOCK(vp);
3487 error = lf_advlock(ap, &(vp->v_lockf), size);
3488 } else {
3489 if (nfs_advlock_p != NULL)
3490 error = nfs_advlock_p(ap);
3491 else {
3492 NFSVOPUNLOCK(vp);
3493 error = ENOLCK;
3494 }
3495 }
3496 if (error == 0 && ap->a_op == F_SETLK) {
3497 error = NFSVOPLOCK(vp, LK_SHARED);
3498 if (error == 0) {
3499 /* Mark that a file lock has been acquired. */
3500 NFSLOCKNODE(np);
3501 np->n_flag |= NHASBEENLOCKED;
3502 NFSUNLOCKNODE(np);
3503 NFSVOPUNLOCK(vp);
3504 }
3505 }
3506 return (error);
3507 } else if ((ap->a_flags & (F_POSIX | F_FLOCK)) != 0) {
3508 if (vp->v_type != VREG) {
3509 error = EINVAL;
3510 goto out;
3511 }
3512 if ((ap->a_flags & F_POSIX) != 0)
3513 cred = p->p_ucred;
3514 else
3515 cred = td->td_ucred;
3516
3517 /*
3518 * If this is unlocking a write locked region, flush and
3519 * commit them before unlocking. This is required by
3520 * RFC3530 Sec. 9.3.2.
3521 */
3522 if (ap->a_op == F_UNLCK &&
3523 nfscl_checkwritelocked(vp, ap->a_fl, cred, td, ap->a_id,
3524 ap->a_flags))
3525 (void) ncl_flush(vp, MNT_WAIT, td, 1, 0);
3526
3527 /*
3528 * Mark NFS node as might have acquired a lock.
3529 * This is separate from NHASBEENLOCKED, because it must
3530 * be done before the nfsrpc_advlock() call, which might
3531 * add a nfscllock structure to the client state.
3532 * It is used to check for the case where a nfscllock
3533 * state structure cannot exist for the file.
3534 * Only done for "oneopenown" NFSv4.1/4.2 mounts.
3535 */
3536 if (NFSHASNFSV4N(nmp) && NFSHASONEOPENOWN(nmp)) {
3537 NFSLOCKNODE(np);
3538 np->n_flag |= NMIGHTBELOCKED;
3539 NFSUNLOCKNODE(np);
3540 }
3541
3542 /*
3543 * Loop around doing the lock op, while a blocking lock
3544 * must wait for the lock op to succeed.
3545 */
3546 do {
3547 ret = nfsrpc_advlock(vp, np->n_size, ap->a_op,
3548 ap->a_fl, 0, cred, td, ap->a_id, ap->a_flags);
3549 if (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3550 ap->a_op == F_SETLK) {
3551 NFSVOPUNLOCK(vp);
3552 error = nfs_catnap(PZERO | PCATCH, ret,
3553 "ncladvl");
3554 if (error)
3555 return (EINTR);
3556 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3557 if (VN_IS_DOOMED(vp)) {
3558 error = EBADF;
3559 goto out;
3560 }
3561 }
3562 } while (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3563 ap->a_op == F_SETLK);
3564 if (ret == NFSERR_DENIED) {
3565 error = EAGAIN;
3566 goto out;
3567 } else if (ret == EINVAL || ret == EBADF || ret == EINTR) {
3568 error = ret;
3569 goto out;
3570 } else if (ret != 0) {
3571 error = EACCES;
3572 goto out;
3573 }
3574
3575 /*
3576 * Now, if we just got a lock, invalidate data in the buffer
3577 * cache, as required, so that the coherency conforms with
3578 * RFC3530 Sec. 9.3.2.
3579 */
3580 if (ap->a_op == F_SETLK) {
3581 if ((np->n_flag & NMODIFIED) == 0) {
3582 np->n_attrstamp = 0;
3583 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3584 ret = VOP_GETATTR(vp, &va, cred);
3585 }
3586 if ((np->n_flag & NMODIFIED) || ret ||
3587 np->n_change != va.va_filerev) {
3588 (void) ncl_vinvalbuf(vp, V_SAVE, td, 1);
3589 np->n_attrstamp = 0;
3590 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3591 ret = VOP_GETATTR(vp, &va, cred);
3592 if (!ret) {
3593 np->n_mtime = va.va_mtime;
3594 np->n_change = va.va_filerev;
3595 }
3596 }
3597 /* Mark that a file lock has been acquired. */
3598 NFSLOCKNODE(np);
3599 np->n_flag |= NHASBEENLOCKED;
3600 NFSUNLOCKNODE(np);
3601 }
3602 } else
3603 error = EOPNOTSUPP;
3604 out:
3605 NFSVOPUNLOCK(vp);
3606 return (error);
3607 }
3608
3609 /*
3610 * NFS advisory byte-level locks.
3611 */
3612 static int
nfs_advlockasync(struct vop_advlockasync_args * ap)3613 nfs_advlockasync(struct vop_advlockasync_args *ap)
3614 {
3615 struct vnode *vp = ap->a_vp;
3616 u_quad_t size;
3617 int error;
3618
3619 error = NFSVOPLOCK(vp, LK_SHARED);
3620 if (error)
3621 return (error);
3622 if (NFS_ISV4(vp)) {
3623 NFSVOPUNLOCK(vp);
3624 return (EOPNOTSUPP);
3625 }
3626 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3627 size = VTONFS(vp)->n_size;
3628 NFSVOPUNLOCK(vp);
3629 error = lf_advlockasync(ap, &(vp->v_lockf), size);
3630 } else {
3631 NFSVOPUNLOCK(vp);
3632 error = EOPNOTSUPP;
3633 }
3634 return (error);
3635 }
3636
3637 /*
3638 * Print out the contents of an nfsnode.
3639 */
3640 static int
nfs_print(struct vop_print_args * ap)3641 nfs_print(struct vop_print_args *ap)
3642 {
3643 struct vnode *vp = ap->a_vp;
3644 struct nfsnode *np = VTONFS(vp);
3645
3646 printf("\tfileid %jd fsid 0x%jx", (uintmax_t)np->n_vattr.na_fileid,
3647 (uintmax_t)np->n_vattr.na_fsid);
3648 if (vp->v_type == VFIFO)
3649 fifo_printinfo(vp);
3650 printf("\n");
3651 return (0);
3652 }
3653
3654 /*
3655 * nfs special file access vnode op.
3656 * Essentially just get vattr and then imitate iaccess() since the device is
3657 * local to the client.
3658 */
3659 static int
nfsspec_access(struct vop_access_args * ap)3660 nfsspec_access(struct vop_access_args *ap)
3661 {
3662 struct vattr *vap;
3663 struct ucred *cred = ap->a_cred;
3664 struct vnode *vp = ap->a_vp;
3665 accmode_t accmode = ap->a_accmode;
3666 struct vattr vattr;
3667 int error;
3668
3669 /*
3670 * Disallow write attempts on filesystems mounted read-only;
3671 * unless the file is a socket, fifo, or a block or character
3672 * device resident on the filesystem.
3673 */
3674 if ((accmode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3675 switch (vp->v_type) {
3676 case VREG:
3677 case VDIR:
3678 case VLNK:
3679 return (EROFS);
3680 default:
3681 break;
3682 }
3683 }
3684 vap = &vattr;
3685 error = VOP_GETATTR(vp, vap, cred);
3686 if (error)
3687 goto out;
3688 error = vaccess(vp->v_type, vap->va_mode, vap->va_uid, vap->va_gid,
3689 accmode, cred);
3690 out:
3691 return error;
3692 }
3693
3694 /*
3695 * Read wrapper for fifos.
3696 */
3697 static int
nfsfifo_read(struct vop_read_args * ap)3698 nfsfifo_read(struct vop_read_args *ap)
3699 {
3700 struct nfsnode *np = VTONFS(ap->a_vp);
3701 int error;
3702
3703 /*
3704 * Set access flag.
3705 */
3706 NFSLOCKNODE(np);
3707 np->n_flag |= NACC;
3708 vfs_timestamp(&np->n_atim);
3709 NFSUNLOCKNODE(np);
3710 error = fifo_specops.vop_read(ap);
3711 return error;
3712 }
3713
3714 /*
3715 * Write wrapper for fifos.
3716 */
3717 static int
nfsfifo_write(struct vop_write_args * ap)3718 nfsfifo_write(struct vop_write_args *ap)
3719 {
3720 struct nfsnode *np = VTONFS(ap->a_vp);
3721
3722 /*
3723 * Set update flag.
3724 */
3725 NFSLOCKNODE(np);
3726 np->n_flag |= NUPD;
3727 vfs_timestamp(&np->n_mtim);
3728 NFSUNLOCKNODE(np);
3729 return(fifo_specops.vop_write(ap));
3730 }
3731
3732 /*
3733 * Close wrapper for fifos.
3734 *
3735 * Update the times on the nfsnode then do fifo close.
3736 */
3737 static int
nfsfifo_close(struct vop_close_args * ap)3738 nfsfifo_close(struct vop_close_args *ap)
3739 {
3740 struct vnode *vp = ap->a_vp;
3741 struct nfsnode *np = VTONFS(vp);
3742 struct vattr vattr;
3743 struct timespec ts;
3744
3745 NFSLOCKNODE(np);
3746 if (np->n_flag & (NACC | NUPD)) {
3747 vfs_timestamp(&ts);
3748 if (np->n_flag & NACC)
3749 np->n_atim = ts;
3750 if (np->n_flag & NUPD)
3751 np->n_mtim = ts;
3752 np->n_flag |= NCHG;
3753 if (vrefcnt(vp) == 1 &&
3754 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3755 VATTR_NULL(&vattr);
3756 if (np->n_flag & NACC)
3757 vattr.va_atime = np->n_atim;
3758 if (np->n_flag & NUPD)
3759 vattr.va_mtime = np->n_mtim;
3760 NFSUNLOCKNODE(np);
3761 (void)VOP_SETATTR(vp, &vattr, ap->a_cred);
3762 goto out;
3763 }
3764 }
3765 NFSUNLOCKNODE(np);
3766 out:
3767 return (fifo_specops.vop_close(ap));
3768 }
3769
3770 static int
nfs_getacl(struct vop_getacl_args * ap)3771 nfs_getacl(struct vop_getacl_args *ap)
3772 {
3773 int error;
3774
3775 if (ap->a_type != ACL_TYPE_NFS4)
3776 return (EOPNOTSUPP);
3777 error = nfsrpc_getacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp);
3778 if (error > NFSERR_STALE) {
3779 (void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3780 error = EPERM;
3781 }
3782 return (error);
3783 }
3784
3785 static int
nfs_setacl(struct vop_setacl_args * ap)3786 nfs_setacl(struct vop_setacl_args *ap)
3787 {
3788 int error;
3789
3790 if (ap->a_type != ACL_TYPE_NFS4)
3791 return (EOPNOTSUPP);
3792 error = nfsrpc_setacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp);
3793 if (error > NFSERR_STALE) {
3794 (void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3795 error = EPERM;
3796 }
3797 return (error);
3798 }
3799
3800 /*
3801 * VOP_ADVISE for NFS.
3802 * Just return 0 for any errors, since it is just a hint.
3803 */
3804 static int
nfs_advise(struct vop_advise_args * ap)3805 nfs_advise(struct vop_advise_args *ap)
3806 {
3807 struct thread *td = curthread;
3808 struct nfsmount *nmp;
3809 uint64_t len;
3810 int error;
3811
3812 /*
3813 * First do vop_stdadvise() to handle the buffer cache.
3814 */
3815 error = vop_stdadvise(ap);
3816 if (error != 0)
3817 return (error);
3818 if (ap->a_start < 0 || ap->a_end < 0)
3819 return (0);
3820 if (ap->a_end == OFF_MAX)
3821 len = 0;
3822 else if (ap->a_end < ap->a_start)
3823 return (0);
3824 else
3825 len = ap->a_end - ap->a_start + 1;
3826 nmp = VFSTONFS(ap->a_vp->v_mount);
3827 mtx_lock(&nmp->nm_mtx);
3828 if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
3829 (NFSHASPNFS(nmp) && (nmp->nm_privflag & NFSMNTP_IOADVISETHRUMDS) ==
3830 0) || (nmp->nm_privflag & NFSMNTP_NOADVISE) != 0) {
3831 mtx_unlock(&nmp->nm_mtx);
3832 return (0);
3833 }
3834 mtx_unlock(&nmp->nm_mtx);
3835 error = nfsrpc_advise(ap->a_vp, ap->a_start, len, ap->a_advice,
3836 td->td_ucred, td);
3837 if (error == NFSERR_NOTSUPP) {
3838 mtx_lock(&nmp->nm_mtx);
3839 nmp->nm_privflag |= NFSMNTP_NOADVISE;
3840 mtx_unlock(&nmp->nm_mtx);
3841 }
3842 return (0);
3843 }
3844
3845 /*
3846 * nfs allocate call
3847 */
3848 static int
nfs_allocate(struct vop_allocate_args * ap)3849 nfs_allocate(struct vop_allocate_args *ap)
3850 {
3851 struct vnode *vp = ap->a_vp;
3852 struct thread *td = curthread;
3853 struct nfsvattr nfsva;
3854 struct nfsmount *nmp;
3855 struct nfsnode *np;
3856 off_t alen;
3857 int attrflag, error, ret;
3858 struct timespec ts;
3859 struct uio io;
3860
3861 attrflag = 0;
3862 nmp = VFSTONFS(vp->v_mount);
3863 np = VTONFS(vp);
3864 mtx_lock(&nmp->nm_mtx);
3865 if (NFSHASNFSV4(nmp) && nmp->nm_minorvers >= NFSV42_MINORVERSION &&
3866 (nmp->nm_privflag & NFSMNTP_NOALLOCATE) == 0) {
3867 mtx_unlock(&nmp->nm_mtx);
3868 alen = *ap->a_len;
3869 if ((uint64_t)alen > nfs_maxalloclen)
3870 alen = nfs_maxalloclen;
3871
3872 /* Check the file size limit. */
3873 io.uio_offset = *ap->a_offset;
3874 io.uio_resid = alen;
3875 error = vn_rlimit_fsize(vp, &io, td);
3876
3877 /*
3878 * Flush first to ensure that the allocate adds to the
3879 * file's allocation on the server.
3880 */
3881 if (error == 0) {
3882 vnode_pager_clean_sync(vp);
3883 error = ncl_flush(vp, MNT_WAIT, td, 1, 0);
3884 }
3885 if (error == 0)
3886 error = nfsrpc_allocate(vp, *ap->a_offset, alen,
3887 &nfsva, &attrflag, ap->a_cred, td);
3888 if (error == 0) {
3889 *ap->a_offset += alen;
3890 *ap->a_len -= alen;
3891 nanouptime(&ts);
3892 NFSLOCKNODE(np);
3893 np->n_localmodtime = ts;
3894 NFSUNLOCKNODE(np);
3895 } else if (error == NFSERR_NOTSUPP) {
3896 mtx_lock(&nmp->nm_mtx);
3897 nmp->nm_privflag |= NFSMNTP_NOALLOCATE;
3898 mtx_unlock(&nmp->nm_mtx);
3899 error = EINVAL;
3900 }
3901 } else {
3902 mtx_unlock(&nmp->nm_mtx);
3903 error = EINVAL;
3904 }
3905 if (attrflag != 0) {
3906 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
3907 if (error == 0 && ret != 0)
3908 error = ret;
3909 }
3910 if (error != 0)
3911 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
3912 return (error);
3913 }
3914
3915 /*
3916 * nfs deallocate call
3917 */
3918 static int
nfs_deallocate(struct vop_deallocate_args * ap)3919 nfs_deallocate(struct vop_deallocate_args *ap)
3920 {
3921 struct vnode *vp = ap->a_vp;
3922 struct thread *td = curthread;
3923 struct nfsvattr nfsva;
3924 struct nfsmount *nmp;
3925 struct nfsnode *np;
3926 off_t tlen, mlen;
3927 int attrflag, error, ret;
3928 bool clipped;
3929 struct timespec ts;
3930
3931 error = 0;
3932 attrflag = 0;
3933 nmp = VFSTONFS(vp->v_mount);
3934 np = VTONFS(vp);
3935 mtx_lock(&nmp->nm_mtx);
3936 if (NFSHASNFSV4(nmp) && nmp->nm_minorvers >= NFSV42_MINORVERSION &&
3937 (nmp->nm_privflag & NFSMNTP_NODEALLOCATE) == 0) {
3938 mtx_unlock(&nmp->nm_mtx);
3939 tlen = omin(OFF_MAX - *ap->a_offset, *ap->a_len);
3940 NFSCL_DEBUG(4, "dealloc: off=%jd len=%jd maxfilesize=%ju\n",
3941 (intmax_t)*ap->a_offset, (intmax_t)tlen,
3942 (uintmax_t)nmp->nm_maxfilesize);
3943 if ((uint64_t)*ap->a_offset >= nmp->nm_maxfilesize) {
3944 /* Avoid EFBIG error return from the NFSv4.2 server. */
3945 *ap->a_len = 0;
3946 return (0);
3947 }
3948 clipped = false;
3949 if ((uint64_t)*ap->a_offset + tlen > nmp->nm_maxfilesize)
3950 tlen = nmp->nm_maxfilesize - *ap->a_offset;
3951 if ((uint64_t)*ap->a_offset < np->n_size) {
3952 /* Limit the len to nfs_maxalloclen before EOF. */
3953 mlen = omin((off_t)np->n_size - *ap->a_offset, tlen);
3954 if ((uint64_t)mlen > nfs_maxalloclen) {
3955 NFSCL_DEBUG(4, "dealloc: tlen maxalloclen\n");
3956 tlen = nfs_maxalloclen;
3957 clipped = true;
3958 }
3959 }
3960 if (error == 0)
3961 error = ncl_vinvalbuf(vp, V_SAVE, td, 1);
3962 if (error == 0) {
3963 vnode_pager_purge_range(vp, *ap->a_offset,
3964 *ap->a_offset + tlen);
3965 error = nfsrpc_deallocate(vp, *ap->a_offset, tlen,
3966 &nfsva, &attrflag, ap->a_cred, td);
3967 NFSCL_DEBUG(4, "dealloc: rpc=%d\n", error);
3968 }
3969 if (error == 0) {
3970 NFSCL_DEBUG(4, "dealloc: attrflag=%d na_size=%ju\n",
3971 attrflag, (uintmax_t)nfsva.na_size);
3972 nanouptime(&ts);
3973 NFSLOCKNODE(np);
3974 np->n_localmodtime = ts;
3975 NFSUNLOCKNODE(np);
3976 if (attrflag != 0) {
3977 if ((uint64_t)*ap->a_offset < nfsva.na_size)
3978 *ap->a_offset += omin((off_t)
3979 nfsva.na_size - *ap->a_offset,
3980 tlen);
3981 }
3982 if (clipped && tlen < *ap->a_len)
3983 *ap->a_len -= tlen;
3984 else
3985 *ap->a_len = 0;
3986 } else if (error == NFSERR_NOTSUPP) {
3987 mtx_lock(&nmp->nm_mtx);
3988 nmp->nm_privflag |= NFSMNTP_NODEALLOCATE;
3989 mtx_unlock(&nmp->nm_mtx);
3990 }
3991 } else {
3992 mtx_unlock(&nmp->nm_mtx);
3993 error = EIO;
3994 }
3995 /*
3996 * If the NFS server cannot perform the Deallocate operation, just call
3997 * vop_stddeallocate() to perform it.
3998 */
3999 if (error != 0 && error != NFSERR_FBIG && error != NFSERR_INVAL) {
4000 error = vop_stddeallocate(ap);
4001 NFSCL_DEBUG(4, "dealloc: stddeallocate=%d\n", error);
4002 }
4003 if (attrflag != 0) {
4004 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4005 if (error == 0 && ret != 0)
4006 error = ret;
4007 }
4008 if (error != 0)
4009 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
4010 return (error);
4011 }
4012
4013 /*
4014 * nfs copy_file_range call
4015 */
4016 static int
nfs_copy_file_range(struct vop_copy_file_range_args * ap)4017 nfs_copy_file_range(struct vop_copy_file_range_args *ap)
4018 {
4019 struct vnode *invp = ap->a_invp;
4020 struct vnode *outvp = ap->a_outvp;
4021 struct mount *mp;
4022 vm_object_t invp_obj;
4023 struct nfsvattr innfsva, outnfsva;
4024 struct vattr va, *vap;
4025 struct uio io;
4026 struct nfsmount *nmp;
4027 struct nfsnode *np;
4028 size_t len, len2;
4029 ssize_t r;
4030 int error, inattrflag, outattrflag, ret, ret2, invp_lock;
4031 off_t inoff, outoff;
4032 bool consecutive, must_commit, onevp, toeof, tryclone, tryoutcred;
4033 bool mustclone;
4034
4035 /*
4036 * NFSv4.2 Copy is not permitted for infile == outfile.
4037 * The NFSv4.2 Clone operation does work on non-overlapping
4038 * byte ranges in the same file, but only if offsets
4039 * (and len if not to EOF) are aligned properly.
4040 * TODO: copy_file_range() between multiple NFS mountpoints
4041 * --> This is not possible now, since each mount appears to
4042 * the NFSv4.n server as a separate client.
4043 */
4044 if ((invp == outvp && (ap->a_flags & COPY_FILE_RANGE_CLONE) == 0) ||
4045 (invp != outvp && invp->v_mount != outvp->v_mount)) {
4046 generic_copy:
4047 return (ENOSYS);
4048 }
4049 if (invp == outvp) {
4050 onevp = true;
4051 invp_lock = LK_EXCLUSIVE;
4052 } else {
4053 onevp = false;
4054 invp_lock = LK_SHARED;
4055 }
4056 mustclone = false;
4057 if (onevp || (ap->a_flags & COPY_FILE_RANGE_CLONE) != 0)
4058 mustclone = true;
4059 relock:
4060 inoff = *ap->a_inoffp;
4061 outoff = *ap->a_outoffp;
4062
4063 /* Lock vnode(s), avoiding risk of deadlock. */
4064 do {
4065 mp = NULL;
4066 error = vn_start_write(outvp, &mp, V_WAIT);
4067 if (error == 0) {
4068 error = vn_lock(outvp, LK_EXCLUSIVE);
4069 if (error == 0) {
4070 if (onevp)
4071 break;
4072 error = vn_lock(invp, invp_lock | LK_NOWAIT);
4073 if (error == 0)
4074 break;
4075 VOP_UNLOCK(outvp);
4076 if (mp != NULL)
4077 vn_finished_write(mp);
4078 mp = NULL;
4079 error = vn_lock(invp, invp_lock);
4080 if (error == 0)
4081 VOP_UNLOCK(invp);
4082 }
4083 }
4084 if (mp != NULL)
4085 vn_finished_write(mp);
4086 } while (error == 0);
4087 if (error != 0)
4088 return (error);
4089
4090 /*
4091 * More reasons to avoid nfs copy/clone: not NFSv4.2, explicitly
4092 * disabled or requires cloning and unable to clone.
4093 * Only clone if the clone_blksize attribute is supported
4094 * and the clone_blksize is greater than 0.
4095 * Alignment of offsets and length will be checked later.
4096 */
4097 nmp = VFSTONFS(invp->v_mount);
4098 np = VTONFS(invp);
4099 mtx_lock(&nmp->nm_mtx);
4100 if ((nmp->nm_privflag & NFSMNTP_NOCOPY) != 0)
4101 mustclone = true;
4102 if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4103 (mustclone && (!NFSISSET_ATTRBIT(&np->n_vattr.na_suppattr,
4104 NFSATTRBIT_CLONEBLKSIZE) || nmp->nm_cloneblksize == 0))) {
4105 mtx_unlock(&nmp->nm_mtx);
4106 VOP_UNLOCK(invp);
4107 if (!onevp)
4108 VOP_UNLOCK(outvp); /* For onevp, same as invp. */
4109 if (mp != NULL)
4110 vn_finished_write(mp);
4111 goto generic_copy;
4112 }
4113 mtx_unlock(&nmp->nm_mtx);
4114
4115 /*
4116 * Do the vn_rlimit_fsize() check. Should this be above the VOP layer?
4117 */
4118 io.uio_offset = *ap->a_outoffp;
4119 io.uio_resid = *ap->a_lenp;
4120 error = vn_rlimit_fsizex(outvp, &io, 0, &r, ap->a_fsizetd);
4121 *ap->a_lenp = io.uio_resid;
4122 /*
4123 * No need to call vn_rlimit_fsizex_res before return, since the uio is
4124 * local.
4125 */
4126
4127 /*
4128 * Flush the input file so that the data is up to date before
4129 * the copy. Flush writes for the output file so that they
4130 * do not overwrite the data copied to the output file by the Copy.
4131 * Set the commit argument for both flushes so that the data is on
4132 * stable storage before the Copy RPC. This is done in case the
4133 * server reboots during the Copy and needs to be redone.
4134 */
4135 if (error == 0) {
4136 invp_obj = invp->v_object;
4137 if (invp_obj != NULL && vm_object_mightbedirty(invp_obj)) {
4138 if (invp_lock != LK_EXCLUSIVE) {
4139 KASSERT(!onevp, ("nfs_copy_file_range: "
4140 "invp_lock LK_SHARED for onevp"));
4141 invp_lock = LK_EXCLUSIVE;
4142 VOP_UNLOCK(invp);
4143 VOP_UNLOCK(outvp);
4144 if (mp != NULL)
4145 vn_finished_write(mp);
4146 goto relock;
4147 }
4148 vnode_pager_clean_sync(invp);
4149 }
4150 error = ncl_flush(invp, MNT_WAIT, curthread, 1, 0);
4151 }
4152 if (error == 0)
4153 error = ncl_vinvalbuf(outvp, V_SAVE, curthread, 0);
4154
4155 /* Do the actual NFSv4.2 RPC. */
4156 ret = ret2 = 0;
4157 len = *ap->a_lenp;
4158 mtx_lock(&nmp->nm_mtx);
4159 if ((nmp->nm_privflag & NFSMNTP_NOCONSECUTIVE) == 0)
4160 consecutive = true;
4161 else
4162 consecutive = false;
4163 mtx_unlock(&nmp->nm_mtx);
4164 tryoutcred = true;
4165 must_commit = false;
4166 toeof = false;
4167
4168 if (error == 0) {
4169 vap = &VTONFS(invp)->n_vattr.na_vattr;
4170 error = VOP_GETATTR(invp, vap, ap->a_incred);
4171 if (error == 0) {
4172 /*
4173 * Clip "len" at va_size so that RFC compliant servers
4174 * will not reply NFSERR_INVAL.
4175 * Setting "len == 0" for the RPC would be preferred,
4176 * but some Linux servers do not support that.
4177 * If the len is being set to 0, do a Setattr RPC to
4178 * set the server's atime. This behaviour was the
4179 * preferred one for the FreeBSD "collective".
4180 */
4181 if (inoff >= vap->va_size) {
4182 *ap->a_lenp = len = 0;
4183 if ((nmp->nm_mountp->mnt_flag & MNT_NOATIME) ==
4184 0) {
4185 VATTR_NULL(&va);
4186 va.va_atime.tv_sec = 0;
4187 va.va_atime.tv_nsec = 0;
4188 va.va_vaflags = VA_UTIMES_NULL;
4189 inattrflag = 0;
4190 error = nfsrpc_setattr(invp, &va, NULL,
4191 ap->a_incred, curthread, &innfsva,
4192 &inattrflag);
4193 if (inattrflag != 0)
4194 ret = nfscl_loadattrcache(&invp,
4195 &innfsva, NULL, 0, 1);
4196 if (error == 0 && ret != 0)
4197 error = ret;
4198 }
4199 } else if (inoff + len >= vap->va_size) {
4200 toeof = true;
4201 *ap->a_lenp = len = vap->va_size - inoff;
4202 }
4203 } else
4204 error = 0;
4205 }
4206
4207 /*
4208 * For cloning, the offsets must be clone blksize aligned and
4209 * the len must be blksize aligned unless it goes to EOF on
4210 * the input file.
4211 */
4212 tryclone = false;
4213 if (len > 0) {
4214 if (error == 0 && NFSISSET_ATTRBIT(&np->n_vattr.na_suppattr,
4215 NFSATTRBIT_CLONEBLKSIZE) && nmp->nm_cloneblksize != 0 &&
4216 (inoff % nmp->nm_cloneblksize) == 0 &&
4217 (outoff % nmp->nm_cloneblksize) == 0 &&
4218 (toeof || (len % nmp->nm_cloneblksize) == 0))
4219 tryclone = true;
4220 else if (mustclone)
4221 error = ENOSYS;
4222 }
4223
4224 /*
4225 * len will be set to 0 upon a successful Copy RPC.
4226 * As such, this only loops when the Copy/Clone RPC needs to be retried.
4227 */
4228 while (len > 0 && error == 0) {
4229 inattrflag = outattrflag = 0;
4230 len2 = len;
4231 if (tryclone) {
4232 if (tryoutcred)
4233 error = nfsrpc_clone(invp, ap->a_inoffp, outvp,
4234 ap->a_outoffp, &len2, toeof, &inattrflag,
4235 &innfsva, &outattrflag, &outnfsva,
4236 ap->a_outcred);
4237 else
4238 error = nfsrpc_clone(invp, ap->a_inoffp, outvp,
4239 ap->a_outoffp, &len2, toeof, &inattrflag,
4240 &innfsva, &outattrflag, &outnfsva,
4241 ap->a_incred);
4242 } else {
4243 if (tryoutcred)
4244 error = nfsrpc_copy_file_range(invp,
4245 ap->a_inoffp, outvp, ap->a_outoffp, &len2,
4246 ap->a_flags, &inattrflag, &innfsva,
4247 &outattrflag, &outnfsva,
4248 ap->a_outcred, consecutive, &must_commit);
4249 else
4250 error = nfsrpc_copy_file_range(invp,
4251 ap->a_inoffp, outvp, ap->a_outoffp, &len2,
4252 ap->a_flags, &inattrflag, &innfsva,
4253 &outattrflag, &outnfsva,
4254 ap->a_incred, consecutive, &must_commit);
4255 }
4256 if (inattrflag != 0)
4257 ret = nfscl_loadattrcache(&invp, &innfsva, NULL, 0, 1);
4258 if (outattrflag != 0)
4259 ret2 = nfscl_loadattrcache(&outvp, &outnfsva, NULL,
4260 1, 1);
4261 if (error == 0) {
4262 if (consecutive == false) {
4263 if (len2 == len) {
4264 mtx_lock(&nmp->nm_mtx);
4265 nmp->nm_privflag |=
4266 NFSMNTP_NOCONSECUTIVE;
4267 mtx_unlock(&nmp->nm_mtx);
4268 } else
4269 error = NFSERR_OFFLOADNOREQS;
4270 }
4271 *ap->a_lenp = len2;
4272 len = 0;
4273 if (len2 > 0 && must_commit && error == 0)
4274 error = ncl_commit(outvp, outoff, *ap->a_lenp,
4275 ap->a_outcred, curthread);
4276 if (error == 0 && ret != 0)
4277 error = ret;
4278 if (error == 0 && ret2 != 0)
4279 error = ret2;
4280 } else if (error == NFSERR_OFFLOADNOREQS && consecutive) {
4281 /*
4282 * Try consecutive == false, which is ok only if all
4283 * bytes are copied.
4284 * If only some bytes were copied when consecutive
4285 * is false, there is no way to know which bytes
4286 * still need to be written.
4287 */
4288 consecutive = false;
4289 error = 0;
4290 } else if (error == NFSERR_ACCES && tryoutcred) {
4291 /* Try again with incred. */
4292 tryoutcred = false;
4293 error = 0;
4294 } else if (tryclone && error != 0) {
4295 if (mustclone) {
4296 error = ENOSYS;
4297 } else {
4298 tryclone = false;
4299 error = 0;
4300 }
4301 }
4302 if (error == NFSERR_STALEWRITEVERF) {
4303 /*
4304 * Server rebooted, so do it all again.
4305 */
4306 *ap->a_inoffp = inoff;
4307 *ap->a_outoffp = outoff;
4308 len = *ap->a_lenp;
4309 must_commit = false;
4310 error = 0;
4311 }
4312 }
4313 VOP_UNLOCK(invp);
4314 if (!onevp)
4315 VOP_UNLOCK(outvp); /* For onevp, same as invp. */
4316 if (mp != NULL)
4317 vn_finished_write(mp);
4318 if (error == NFSERR_NOTSUPP || error == NFSERR_OFFLOADNOREQS ||
4319 error == NFSERR_ACCES || error == ENOSYS) {
4320 /*
4321 * Unlike the NFSv4.2 Copy, vn_generic_copy_file_range() can
4322 * use a_incred for the read and a_outcred for the write, so
4323 * try this for NFSERR_ACCES failures for the Copy.
4324 * For NFSERR_NOTSUPP and NFSERR_OFFLOADNOREQS, the Copy can
4325 * never succeed, so disable it.
4326 */
4327 if (error != NFSERR_ACCES && error != ENOSYS) {
4328 /* Can never do Copy on this mount. */
4329 mtx_lock(&nmp->nm_mtx);
4330 nmp->nm_privflag |= NFSMNTP_NOCOPY;
4331 mtx_unlock(&nmp->nm_mtx);
4332 }
4333 *ap->a_inoffp = inoff;
4334 *ap->a_outoffp = outoff;
4335 error = vn_generic_copy_file_range(ap->a_invp, ap->a_inoffp,
4336 ap->a_outvp, ap->a_outoffp, ap->a_lenp, ap->a_flags,
4337 ap->a_incred, ap->a_outcred, ap->a_fsizetd);
4338 } else if (error != 0)
4339 *ap->a_lenp = 0;
4340
4341 if (error != 0)
4342 error = nfscl_maperr(curthread, error, (uid_t)0, (gid_t)0);
4343 return (error);
4344 }
4345
4346 /*
4347 * nfs ioctl call
4348 */
4349 static int
nfs_ioctl(struct vop_ioctl_args * ap)4350 nfs_ioctl(struct vop_ioctl_args *ap)
4351 {
4352 struct vnode *vp = ap->a_vp;
4353 struct nfsvattr nfsva;
4354 struct nfsmount *nmp;
4355 int attrflag, content, error, ret;
4356 bool eof = false; /* shut up compiler. */
4357
4358 /* Do the actual NFSv4.2 RPC. */
4359 switch (ap->a_command) {
4360 case FIOSEEKDATA:
4361 content = NFSV4CONTENT_DATA;
4362 break;
4363 case FIOSEEKHOLE:
4364 content = NFSV4CONTENT_HOLE;
4365 break;
4366 default:
4367 return (ENOTTY);
4368 }
4369
4370 error = vn_lock(vp, LK_EXCLUSIVE);
4371 if (error != 0)
4372 return (EBADF);
4373
4374 if (vp->v_type != VREG) {
4375 VOP_UNLOCK(vp);
4376 return (ENOTTY);
4377 }
4378 nmp = VFSTONFS(vp->v_mount);
4379 if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION) {
4380 VOP_UNLOCK(vp);
4381 error = vop_stdioctl(ap);
4382 return (error);
4383 }
4384
4385 attrflag = 0;
4386 if (*((off_t *)ap->a_data) >= VTONFS(vp)->n_size)
4387 error = ENXIO;
4388 else {
4389 /*
4390 * Flush all writes, so that the server is up to date.
4391 * Although a Commit is not required, the commit argument
4392 * is set so that, for a pNFS File/Flexible File Layout
4393 * server, the LayoutCommit will be done to ensure the file
4394 * size is up to date on the Metadata Server.
4395 */
4396
4397 vnode_pager_clean_sync(vp);
4398 error = ncl_flush(vp, MNT_WAIT, ap->a_td, 1, 0);
4399 if (error == 0)
4400 error = nfsrpc_seek(vp, (off_t *)ap->a_data, &eof,
4401 content, ap->a_cred, &nfsva, &attrflag);
4402 /* If at eof for FIOSEEKDATA, return ENXIO. */
4403 if (eof && error == 0 && content == NFSV4CONTENT_DATA)
4404 error = ENXIO;
4405 }
4406 if (attrflag != 0) {
4407 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4408 if (error == 0 && ret != 0)
4409 error = ret;
4410 }
4411 NFSVOPUNLOCK(vp);
4412
4413 if (error != 0)
4414 error = ENXIO;
4415 return (error);
4416 }
4417
4418 /*
4419 * nfs getextattr call
4420 */
4421 static int
nfs_getextattr(struct vop_getextattr_args * ap)4422 nfs_getextattr(struct vop_getextattr_args *ap)
4423 {
4424 struct vnode *vp = ap->a_vp;
4425 struct nfsmount *nmp;
4426 struct ucred *cred;
4427 struct thread *td = ap->a_td;
4428 struct nfsvattr nfsva;
4429 ssize_t len;
4430 int attrflag, error, ret;
4431
4432 nmp = VFSTONFS(vp->v_mount);
4433 mtx_lock(&nmp->nm_mtx);
4434 if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4435 (nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4436 ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4437 mtx_unlock(&nmp->nm_mtx);
4438 return (EOPNOTSUPP);
4439 }
4440 mtx_unlock(&nmp->nm_mtx);
4441
4442 cred = ap->a_cred;
4443 if (cred == NULL)
4444 cred = td->td_ucred;
4445 /* Do the actual NFSv4.2 Optional Extended Attribute (RFC-8276) RPC. */
4446 attrflag = 0;
4447 error = nfsrpc_getextattr(vp, ap->a_name, ap->a_uio, &len, &nfsva,
4448 &attrflag, cred, td);
4449 if (attrflag != 0) {
4450 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4451 if (error == 0 && ret != 0)
4452 error = ret;
4453 }
4454 if (error == 0 && ap->a_size != NULL)
4455 *ap->a_size = len;
4456
4457 switch (error) {
4458 case NFSERR_NOTSUPP:
4459 case NFSERR_OPILLEGAL:
4460 mtx_lock(&nmp->nm_mtx);
4461 nmp->nm_privflag |= NFSMNTP_NOXATTR;
4462 mtx_unlock(&nmp->nm_mtx);
4463 error = EOPNOTSUPP;
4464 break;
4465 case NFSERR_NOXATTR:
4466 case NFSERR_XATTR2BIG:
4467 error = ENOATTR;
4468 break;
4469 default:
4470 error = nfscl_maperr(td, error, 0, 0);
4471 break;
4472 }
4473 return (error);
4474 }
4475
4476 /*
4477 * nfs setextattr call
4478 */
4479 static int
nfs_setextattr(struct vop_setextattr_args * ap)4480 nfs_setextattr(struct vop_setextattr_args *ap)
4481 {
4482 struct vnode *vp = ap->a_vp;
4483 struct nfsmount *nmp;
4484 struct ucred *cred;
4485 struct thread *td = ap->a_td;
4486 struct nfsvattr nfsva;
4487 int attrflag, error, ret;
4488
4489 nmp = VFSTONFS(vp->v_mount);
4490 mtx_lock(&nmp->nm_mtx);
4491 if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4492 (nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4493 ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4494 mtx_unlock(&nmp->nm_mtx);
4495 return (EOPNOTSUPP);
4496 }
4497 mtx_unlock(&nmp->nm_mtx);
4498
4499 if (ap->a_uio->uio_resid < 0)
4500 return (EINVAL);
4501 cred = ap->a_cred;
4502 if (cred == NULL)
4503 cred = td->td_ucred;
4504 /* Do the actual NFSv4.2 Optional Extended Attribute (RFC-8276) RPC. */
4505 attrflag = 0;
4506 error = nfsrpc_setextattr(vp, ap->a_name, ap->a_uio, &nfsva,
4507 &attrflag, cred, td);
4508 if (attrflag != 0) {
4509 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4510 if (error == 0 && ret != 0)
4511 error = ret;
4512 }
4513
4514 switch (error) {
4515 case NFSERR_NOTSUPP:
4516 case NFSERR_OPILLEGAL:
4517 mtx_lock(&nmp->nm_mtx);
4518 nmp->nm_privflag |= NFSMNTP_NOXATTR;
4519 mtx_unlock(&nmp->nm_mtx);
4520 error = EOPNOTSUPP;
4521 break;
4522 case NFSERR_NOXATTR:
4523 case NFSERR_XATTR2BIG:
4524 error = ENOATTR;
4525 break;
4526 default:
4527 error = nfscl_maperr(td, error, 0, 0);
4528 break;
4529 }
4530 return (error);
4531 }
4532
4533 /*
4534 * nfs listextattr call
4535 */
4536 static int
nfs_listextattr(struct vop_listextattr_args * ap)4537 nfs_listextattr(struct vop_listextattr_args *ap)
4538 {
4539 struct vnode *vp = ap->a_vp;
4540 struct nfsmount *nmp;
4541 struct ucred *cred;
4542 struct thread *td = ap->a_td;
4543 struct nfsvattr nfsva;
4544 size_t len, len2;
4545 uint64_t cookie;
4546 int attrflag, error, ret;
4547 bool eof;
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 cred = ap->a_cred;
4560 if (cred == NULL)
4561 cred = td->td_ucred;
4562
4563 /* Loop around doing List Extended Attribute RPCs. */
4564 eof = false;
4565 cookie = 0;
4566 len2 = 0;
4567 error = 0;
4568 while (!eof && error == 0) {
4569 len = nmp->nm_rsize;
4570 attrflag = 0;
4571 error = nfsrpc_listextattr(vp, &cookie, ap->a_uio, &len, &eof,
4572 &nfsva, &attrflag, cred, td);
4573 if (attrflag != 0) {
4574 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4575 if (error == 0 && ret != 0)
4576 error = ret;
4577 }
4578 if (error == 0) {
4579 len2 += len;
4580 if (len2 > SSIZE_MAX)
4581 error = ENOATTR;
4582 }
4583 }
4584 if (error == 0 && ap->a_size != NULL)
4585 *ap->a_size = len2;
4586
4587 switch (error) {
4588 case NFSERR_NOTSUPP:
4589 case NFSERR_OPILLEGAL:
4590 mtx_lock(&nmp->nm_mtx);
4591 nmp->nm_privflag |= NFSMNTP_NOXATTR;
4592 mtx_unlock(&nmp->nm_mtx);
4593 error = EOPNOTSUPP;
4594 break;
4595 case NFSERR_NOXATTR:
4596 case NFSERR_XATTR2BIG:
4597 error = ENOATTR;
4598 break;
4599 default:
4600 error = nfscl_maperr(td, error, 0, 0);
4601 break;
4602 }
4603 return (error);
4604 }
4605
4606 /*
4607 * nfs setextattr call
4608 */
4609 static int
nfs_deleteextattr(struct vop_deleteextattr_args * ap)4610 nfs_deleteextattr(struct vop_deleteextattr_args *ap)
4611 {
4612 struct vnode *vp = ap->a_vp;
4613 struct nfsmount *nmp;
4614 struct nfsvattr nfsva;
4615 int attrflag, error, ret;
4616
4617 nmp = VFSTONFS(vp->v_mount);
4618 mtx_lock(&nmp->nm_mtx);
4619 if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4620 (nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4621 ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4622 mtx_unlock(&nmp->nm_mtx);
4623 return (EOPNOTSUPP);
4624 }
4625 mtx_unlock(&nmp->nm_mtx);
4626
4627 /* Do the actual NFSv4.2 Optional Extended Attribute (RFC-8276) RPC. */
4628 attrflag = 0;
4629 error = nfsrpc_rmextattr(vp, ap->a_name, &nfsva, &attrflag, ap->a_cred,
4630 ap->a_td);
4631 if (attrflag != 0) {
4632 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4633 if (error == 0 && ret != 0)
4634 error = ret;
4635 }
4636
4637 switch (error) {
4638 case NFSERR_NOTSUPP:
4639 case NFSERR_OPILLEGAL:
4640 mtx_lock(&nmp->nm_mtx);
4641 nmp->nm_privflag |= NFSMNTP_NOXATTR;
4642 mtx_unlock(&nmp->nm_mtx);
4643 error = EOPNOTSUPP;
4644 break;
4645 case NFSERR_NOXATTR:
4646 case NFSERR_XATTR2BIG:
4647 error = ENOATTR;
4648 break;
4649 default:
4650 error = nfscl_maperr(ap->a_td, error, 0, 0);
4651 break;
4652 }
4653 return (error);
4654 }
4655
4656 /*
4657 * Return POSIX pathconf information applicable to nfs filesystems.
4658 */
4659 static int
nfs_pathconf(struct vop_pathconf_args * ap)4660 nfs_pathconf(struct vop_pathconf_args *ap)
4661 {
4662 struct nfsv3_pathconf pc;
4663 struct nfsvattr nfsva;
4664 struct vnode *vp = ap->a_vp;
4665 struct nfsmount *nmp;
4666 struct thread *td = curthread;
4667 off_t off;
4668 uint32_t clone_blksize;
4669 bool eof, has_namedattr, named_enabled;
4670 int attrflag, error;
4671 struct nfsnode *np;
4672
4673 nmp = VFSTONFS(vp->v_mount);
4674 np = VTONFS(vp);
4675 named_enabled = false;
4676 has_namedattr = false;
4677 clone_blksize = 0;
4678 if ((NFS_ISV34(vp) && (ap->a_name == _PC_LINK_MAX ||
4679 ap->a_name == _PC_NAME_MAX || ap->a_name == _PC_CHOWN_RESTRICTED ||
4680 ap->a_name == _PC_NO_TRUNC)) ||
4681 (NFS_ISV4(vp) && (ap->a_name == _PC_ACL_NFS4 ||
4682 ap->a_name == _PC_HAS_NAMEDATTR ||
4683 ap->a_name == _PC_CLONE_BLKSIZE))) {
4684 /*
4685 * Since only the above 4 a_names are returned by the NFSv3
4686 * Pathconf RPC, there is no point in doing it for others.
4687 * For NFSv4, the Pathconf RPC (actually a Getattr Op.) can
4688 * be used for _PC_ACL_NFS4, _PC_HAS_NAMEDATTR and
4689 * _PC_CLONE_BLKSIZE as well.
4690 */
4691 error = nfsrpc_pathconf(vp, &pc, &has_namedattr, &clone_blksize,
4692 td->td_ucred, td, &nfsva, &attrflag);
4693 if (attrflag != 0)
4694 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4695 if (error != 0)
4696 return (error);
4697 } else if (NFS_ISV4(vp) && ap->a_name == _PC_NAMEDATTR_ENABLED &&
4698 (np->n_flag & NNAMEDNOTSUPP) == 0) {
4699 struct nfsfh *nfhp;
4700
4701 error = nfsrpc_openattr(nmp, vp, np->n_fhp->nfh_fh,
4702 np->n_fhp->nfh_len, false, td->td_ucred, td, &nfsva, &nfhp,
4703 &attrflag);
4704 named_enabled = true;
4705 if (error == 0) {
4706 free(nfhp, M_NFSFH);
4707 } else if (error == NFSERR_NOTSUPP) {
4708 named_enabled = false;
4709 NFSLOCKNODE(np);
4710 np->n_flag |= NNAMEDNOTSUPP;
4711 NFSUNLOCKNODE(np);
4712 }
4713 error = 0;
4714 } else {
4715 /*
4716 * For NFSv2 (or NFSv3 when not one of the above 4 a_names),
4717 * just fake them.
4718 */
4719 pc.pc_linkmax = NFS_LINK_MAX;
4720 pc.pc_namemax = NFS_MAXNAMLEN;
4721 pc.pc_notrunc = 1;
4722 pc.pc_chownrestricted = 1;
4723 pc.pc_caseinsensitive = 0;
4724 pc.pc_casepreserving = 1;
4725 error = 0;
4726 }
4727 switch (ap->a_name) {
4728 case _PC_LINK_MAX:
4729 #ifdef _LP64
4730 *ap->a_retval = pc.pc_linkmax;
4731 #else
4732 *ap->a_retval = MIN(LONG_MAX, pc.pc_linkmax);
4733 #endif
4734 break;
4735 case _PC_NAME_MAX:
4736 *ap->a_retval = pc.pc_namemax;
4737 break;
4738 case _PC_PIPE_BUF:
4739 if (ap->a_vp->v_type == VDIR || ap->a_vp->v_type == VFIFO)
4740 *ap->a_retval = PIPE_BUF;
4741 else
4742 error = EINVAL;
4743 break;
4744 case _PC_CHOWN_RESTRICTED:
4745 *ap->a_retval = pc.pc_chownrestricted;
4746 break;
4747 case _PC_NO_TRUNC:
4748 *ap->a_retval = pc.pc_notrunc;
4749 break;
4750 case _PC_ACL_NFS4:
4751 if (NFS_ISV4(vp) && nfsrv_useacl != 0 && attrflag != 0 &&
4752 NFSISSET_ATTRBIT(&nfsva.na_suppattr, NFSATTRBIT_ACL))
4753 *ap->a_retval = 1;
4754 else
4755 *ap->a_retval = 0;
4756 break;
4757 case _PC_ACL_PATH_MAX:
4758 if (NFS_ISV4(vp))
4759 *ap->a_retval = ACL_MAX_ENTRIES;
4760 else
4761 *ap->a_retval = 3;
4762 break;
4763 case _PC_PRIO_IO:
4764 *ap->a_retval = 0;
4765 break;
4766 case _PC_SYNC_IO:
4767 *ap->a_retval = 0;
4768 break;
4769 case _PC_ALLOC_SIZE_MIN:
4770 *ap->a_retval = vp->v_mount->mnt_stat.f_bsize;
4771 break;
4772 case _PC_FILESIZEBITS:
4773 if (NFS_ISV34(vp))
4774 *ap->a_retval = 64;
4775 else
4776 *ap->a_retval = 32;
4777 break;
4778 case _PC_REC_INCR_XFER_SIZE:
4779 *ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
4780 break;
4781 case _PC_REC_MAX_XFER_SIZE:
4782 *ap->a_retval = -1; /* means ``unlimited'' */
4783 break;
4784 case _PC_REC_MIN_XFER_SIZE:
4785 *ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
4786 break;
4787 case _PC_REC_XFER_ALIGN:
4788 *ap->a_retval = PAGE_SIZE;
4789 break;
4790 case _PC_SYMLINK_MAX:
4791 *ap->a_retval = NFS_MAXPATHLEN;
4792 break;
4793 case _PC_MIN_HOLE_SIZE:
4794 /* Only some NFSv4.2 servers support Seek for Holes. */
4795 *ap->a_retval = 0;
4796 if (NFS_ISV4(vp) && nmp->nm_minorvers == NFSV42_MINORVERSION) {
4797 /*
4798 * NFSv4.2 doesn't have an attribute for hole size,
4799 * so all we can do is see if the Seek operation is
4800 * supported and then use f_iosize as a "best guess".
4801 */
4802 mtx_lock(&nmp->nm_mtx);
4803 if ((nmp->nm_privflag & NFSMNTP_SEEKTESTED) == 0) {
4804 mtx_unlock(&nmp->nm_mtx);
4805 off = 0;
4806 attrflag = 0;
4807 error = nfsrpc_seek(vp, &off, &eof,
4808 NFSV4CONTENT_HOLE, td->td_ucred, &nfsva,
4809 &attrflag);
4810 if (attrflag != 0)
4811 (void) nfscl_loadattrcache(&vp, &nfsva,
4812 NULL, 0, 1);
4813 mtx_lock(&nmp->nm_mtx);
4814 if (error == NFSERR_NOTSUPP)
4815 nmp->nm_privflag |= NFSMNTP_SEEKTESTED;
4816 else
4817 nmp->nm_privflag |= NFSMNTP_SEEKTESTED |
4818 NFSMNTP_SEEK;
4819 error = 0;
4820 }
4821 if ((nmp->nm_privflag & NFSMNTP_SEEK) != 0)
4822 *ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
4823 mtx_unlock(&nmp->nm_mtx);
4824 }
4825 break;
4826 case _PC_NAMEDATTR_ENABLED:
4827 if (named_enabled)
4828 *ap->a_retval = 1;
4829 else
4830 *ap->a_retval = 0;
4831 break;
4832 case _PC_HAS_NAMEDATTR:
4833 if (has_namedattr)
4834 *ap->a_retval = 1;
4835 else
4836 *ap->a_retval = 0;
4837 break;
4838 case _PC_HAS_HIDDENSYSTEM:
4839 if (NFS_ISV4(vp) && NFSISSET_ATTRBIT(&np->n_vattr.na_suppattr,
4840 NFSATTRBIT_ARCHIVE) &&
4841 NFSISSET_ATTRBIT(&np->n_vattr.na_suppattr,
4842 NFSATTRBIT_HIDDEN) &&
4843 NFSISSET_ATTRBIT(&np->n_vattr.na_suppattr,
4844 NFSATTRBIT_SYSTEM))
4845 *ap->a_retval = 1;
4846 else
4847 *ap->a_retval = 0;
4848 break;
4849 case _PC_CLONE_BLKSIZE:
4850 *ap->a_retval = clone_blksize;
4851 break;
4852
4853 default:
4854 error = vop_stdpathconf(ap);
4855 break;
4856 }
4857 return (error);
4858 }
4859