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