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