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