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