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