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 error = vn_dir_check_exec(dvp, cnp); 1199 if (error != 0) 1200 return (error); 1201 error = cache_lookup(dvp, vpp, cnp, &nctime, &ncticks); 1202 if (error > 0 && error != ENOENT) 1203 return (error); 1204 if (error == -1) { 1205 /* 1206 * Lookups of "." are special and always return the 1207 * current directory. cache_lookup() already handles 1208 * associated locking bookkeeping, etc. 1209 */ 1210 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') { 1211 /* XXX: Is this really correct? */ 1212 if (cnp->cn_nameiop != LOOKUP && 1213 (flags & ISLASTCN)) 1214 cnp->cn_flags |= SAVENAME; 1215 return (0); 1216 } 1217 1218 /* 1219 * We only accept a positive hit in the cache if the 1220 * change time of the file matches our cached copy. 1221 * Otherwise, we discard the cache entry and fallback 1222 * to doing a lookup RPC. We also only trust cache 1223 * entries for less than nm_nametimeo seconds. 1224 * 1225 * To better handle stale file handles and attributes, 1226 * clear the attribute cache of this node if it is a 1227 * leaf component, part of an open() call, and not 1228 * locally modified before fetching the attributes. 1229 * This should allow stale file handles to be detected 1230 * here where we can fall back to a LOOKUP RPC to 1231 * recover rather than having nfs_open() detect the 1232 * stale file handle and failing open(2) with ESTALE. 1233 */ 1234 newvp = *vpp; 1235 newnp = VTONFS(newvp); 1236 if (!(nmp->nm_flag & NFSMNT_NOCTO) && 1237 (flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) && 1238 !(newnp->n_flag & NMODIFIED)) { 1239 NFSLOCKNODE(newnp); 1240 newnp->n_attrstamp = 0; 1241 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp); 1242 NFSUNLOCKNODE(newnp); 1243 } 1244 if (nfscl_nodeleg(newvp, 0) == 0 || 1245 ((u_int)(ticks - ncticks) < (nmp->nm_nametimeo * hz) && 1246 VOP_GETATTR(newvp, &vattr, cnp->cn_cred) == 0 && 1247 timespeccmp(&vattr.va_ctime, &nctime, ==))) { 1248 NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits); 1249 if (cnp->cn_nameiop != LOOKUP && 1250 (flags & ISLASTCN)) 1251 cnp->cn_flags |= SAVENAME; 1252 return (0); 1253 } 1254 cache_purge(newvp); 1255 if (dvp != newvp) 1256 vput(newvp); 1257 else 1258 vrele(newvp); 1259 *vpp = NULLVP; 1260 } else if (error == ENOENT) { 1261 if (VN_IS_DOOMED(dvp)) 1262 return (ENOENT); 1263 /* 1264 * We only accept a negative hit in the cache if the 1265 * modification time of the parent directory matches 1266 * the cached copy in the name cache entry. 1267 * Otherwise, we discard all of the negative cache 1268 * entries for this directory. We also only trust 1269 * negative cache entries for up to nm_negnametimeo 1270 * seconds. 1271 */ 1272 if ((u_int)(ticks - ncticks) < (nmp->nm_negnametimeo * hz) && 1273 VOP_GETATTR(dvp, &vattr, cnp->cn_cred) == 0 && 1274 timespeccmp(&vattr.va_mtime, &nctime, ==)) { 1275 NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits); 1276 return (ENOENT); 1277 } 1278 cache_purge_negative(dvp); 1279 } 1280 1281 newvp = NULLVP; 1282 NFSINCRGLOBAL(nfsstatsv1.lookupcache_misses); 1283 error = nfsrpc_lookup(dvp, cnp->cn_nameptr, cnp->cn_namelen, 1284 cnp->cn_cred, td, &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag, 1285 NULL); 1286 if (dattrflag) 1287 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1); 1288 if (error) { 1289 if (newvp != NULLVP) { 1290 vput(newvp); 1291 *vpp = NULLVP; 1292 } 1293 1294 if (error != ENOENT) { 1295 if (NFS_ISV4(dvp)) 1296 error = nfscl_maperr(td, error, (uid_t)0, 1297 (gid_t)0); 1298 return (error); 1299 } 1300 1301 /* The requested file was not found. */ 1302 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) && 1303 (flags & ISLASTCN)) { 1304 /* 1305 * XXX: UFS does a full VOP_ACCESS(dvp, 1306 * VWRITE) here instead of just checking 1307 * MNT_RDONLY. 1308 */ 1309 if (mp->mnt_flag & MNT_RDONLY) 1310 return (EROFS); 1311 cnp->cn_flags |= SAVENAME; 1312 return (EJUSTRETURN); 1313 } 1314 1315 if ((cnp->cn_flags & MAKEENTRY) != 0 && dattrflag) { 1316 /* 1317 * Cache the modification time of the parent 1318 * directory from the post-op attributes in 1319 * the name cache entry. The negative cache 1320 * entry will be ignored once the directory 1321 * has changed. Don't bother adding the entry 1322 * if the directory has already changed. 1323 */ 1324 NFSLOCKNODE(np); 1325 if (timespeccmp(&np->n_vattr.na_mtime, 1326 &dnfsva.na_mtime, ==)) { 1327 NFSUNLOCKNODE(np); 1328 cache_enter_time(dvp, NULL, cnp, 1329 &dnfsva.na_mtime, NULL); 1330 } else 1331 NFSUNLOCKNODE(np); 1332 } 1333 return (ENOENT); 1334 } 1335 1336 /* 1337 * Handle RENAME case... 1338 */ 1339 if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) { 1340 if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) { 1341 free(nfhp, M_NFSFH); 1342 return (EISDIR); 1343 } 1344 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL, 1345 LK_EXCLUSIVE); 1346 if (error) 1347 return (error); 1348 newvp = NFSTOV(np); 1349 if (attrflag) 1350 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL, 1351 0, 1); 1352 *vpp = newvp; 1353 cnp->cn_flags |= SAVENAME; 1354 return (0); 1355 } 1356 1357 if (flags & ISDOTDOT) { 1358 ltype = NFSVOPISLOCKED(dvp); 1359 error = vfs_busy(mp, MBF_NOWAIT); 1360 if (error != 0) { 1361 vfs_ref(mp); 1362 NFSVOPUNLOCK(dvp); 1363 error = vfs_busy(mp, 0); 1364 NFSVOPLOCK(dvp, ltype | LK_RETRY); 1365 vfs_rel(mp); 1366 if (error == 0 && VN_IS_DOOMED(dvp)) { 1367 vfs_unbusy(mp); 1368 error = ENOENT; 1369 } 1370 if (error != 0) 1371 return (error); 1372 } 1373 NFSVOPUNLOCK(dvp); 1374 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL, 1375 cnp->cn_lkflags); 1376 if (error == 0) 1377 newvp = NFSTOV(np); 1378 vfs_unbusy(mp); 1379 if (newvp != dvp) 1380 NFSVOPLOCK(dvp, ltype | LK_RETRY); 1381 if (VN_IS_DOOMED(dvp)) { 1382 if (error == 0) { 1383 if (newvp == dvp) 1384 vrele(newvp); 1385 else 1386 vput(newvp); 1387 } 1388 error = ENOENT; 1389 } 1390 if (error != 0) 1391 return (error); 1392 if (attrflag) 1393 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL, 1394 0, 1); 1395 } else if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) { 1396 free(nfhp, M_NFSFH); 1397 VREF(dvp); 1398 newvp = dvp; 1399 if (attrflag) 1400 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL, 1401 0, 1); 1402 } else { 1403 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL, 1404 cnp->cn_lkflags); 1405 if (error) 1406 return (error); 1407 newvp = NFSTOV(np); 1408 if (attrflag) 1409 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL, 1410 0, 1); 1411 else if ((flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) && 1412 !(np->n_flag & NMODIFIED)) { 1413 /* 1414 * Flush the attribute cache when opening a 1415 * leaf node to ensure that fresh attributes 1416 * are fetched in nfs_open() since we did not 1417 * fetch attributes from the LOOKUP reply. 1418 */ 1419 NFSLOCKNODE(np); 1420 np->n_attrstamp = 0; 1421 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp); 1422 NFSUNLOCKNODE(np); 1423 } 1424 } 1425 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN)) 1426 cnp->cn_flags |= SAVENAME; 1427 if ((cnp->cn_flags & MAKEENTRY) && 1428 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN)) && 1429 attrflag != 0 && (newvp->v_type != VDIR || dattrflag != 0)) 1430 cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime, 1431 newvp->v_type != VDIR ? NULL : &dnfsva.na_ctime); 1432 *vpp = newvp; 1433 return (0); 1434 } 1435 1436 /* 1437 * nfs read call. 1438 * Just call ncl_bioread() to do the work. 1439 */ 1440 static int 1441 nfs_read(struct vop_read_args *ap) 1442 { 1443 struct vnode *vp = ap->a_vp; 1444 1445 switch (vp->v_type) { 1446 case VREG: 1447 return (ncl_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred)); 1448 case VDIR: 1449 return (EISDIR); 1450 default: 1451 return (EOPNOTSUPP); 1452 } 1453 } 1454 1455 /* 1456 * nfs readlink call 1457 */ 1458 static int 1459 nfs_readlink(struct vop_readlink_args *ap) 1460 { 1461 struct vnode *vp = ap->a_vp; 1462 1463 if (vp->v_type != VLNK) 1464 return (EINVAL); 1465 return (ncl_bioread(vp, ap->a_uio, 0, ap->a_cred)); 1466 } 1467 1468 /* 1469 * Do a readlink rpc. 1470 * Called by ncl_doio() from below the buffer cache. 1471 */ 1472 int 1473 ncl_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred) 1474 { 1475 int error, ret, attrflag; 1476 struct nfsvattr nfsva; 1477 1478 error = nfsrpc_readlink(vp, uiop, cred, uiop->uio_td, &nfsva, 1479 &attrflag, NULL); 1480 if (attrflag) { 1481 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1); 1482 if (ret && !error) 1483 error = ret; 1484 } 1485 if (error && NFS_ISV4(vp)) 1486 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0); 1487 return (error); 1488 } 1489 1490 /* 1491 * nfs read rpc call 1492 * Ditto above 1493 */ 1494 int 1495 ncl_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred) 1496 { 1497 int error, ret, attrflag; 1498 struct nfsvattr nfsva; 1499 struct nfsmount *nmp; 1500 1501 nmp = VFSTONFS(vp->v_mount); 1502 error = EIO; 1503 attrflag = 0; 1504 if (NFSHASPNFS(nmp)) 1505 error = nfscl_doiods(vp, uiop, NULL, NULL, 1506 NFSV4OPEN_ACCESSREAD, 0, cred, uiop->uio_td); 1507 NFSCL_DEBUG(4, "readrpc: aft doiods=%d\n", error); 1508 if (error != 0) 1509 error = nfsrpc_read(vp, uiop, cred, uiop->uio_td, &nfsva, 1510 &attrflag, NULL); 1511 if (attrflag) { 1512 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1); 1513 if (ret && !error) 1514 error = ret; 1515 } 1516 if (error && NFS_ISV4(vp)) 1517 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0); 1518 return (error); 1519 } 1520 1521 /* 1522 * nfs write call 1523 */ 1524 int 1525 ncl_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred, 1526 int *iomode, int *must_commit, int called_from_strategy) 1527 { 1528 struct nfsvattr nfsva; 1529 int error, attrflag, ret; 1530 struct nfsmount *nmp; 1531 1532 nmp = VFSTONFS(vp->v_mount); 1533 error = EIO; 1534 attrflag = 0; 1535 if (NFSHASPNFS(nmp)) 1536 error = nfscl_doiods(vp, uiop, iomode, must_commit, 1537 NFSV4OPEN_ACCESSWRITE, 0, cred, uiop->uio_td); 1538 NFSCL_DEBUG(4, "writerpc: aft doiods=%d\n", error); 1539 if (error != 0) 1540 error = nfsrpc_write(vp, uiop, iomode, must_commit, cred, 1541 uiop->uio_td, &nfsva, &attrflag, NULL, 1542 called_from_strategy); 1543 if (attrflag) { 1544 if (VTONFS(vp)->n_flag & ND_NFSV4) 1545 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 1, 1546 1); 1547 else 1548 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1549 1); 1550 if (ret && !error) 1551 error = ret; 1552 } 1553 if (DOINGASYNC(vp)) 1554 *iomode = NFSWRITE_FILESYNC; 1555 if (error && NFS_ISV4(vp)) 1556 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0); 1557 return (error); 1558 } 1559 1560 /* 1561 * nfs mknod rpc 1562 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the 1563 * mode set to specify the file type and the size field for rdev. 1564 */ 1565 static int 1566 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp, 1567 struct vattr *vap) 1568 { 1569 struct nfsvattr nfsva, dnfsva; 1570 struct vnode *newvp = NULL; 1571 struct nfsnode *np = NULL, *dnp; 1572 struct nfsfh *nfhp; 1573 struct vattr vattr; 1574 int error = 0, attrflag, dattrflag; 1575 u_int32_t rdev; 1576 1577 if (vap->va_type == VCHR || vap->va_type == VBLK) 1578 rdev = vap->va_rdev; 1579 else if (vap->va_type == VFIFO || vap->va_type == VSOCK) 1580 rdev = 0xffffffff; 1581 else 1582 return (EOPNOTSUPP); 1583 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred))) 1584 return (error); 1585 error = nfsrpc_mknod(dvp, cnp->cn_nameptr, cnp->cn_namelen, vap, 1586 rdev, vap->va_type, cnp->cn_cred, cnp->cn_thread, &dnfsva, 1587 &nfsva, &nfhp, &attrflag, &dattrflag, NULL); 1588 if (!error) { 1589 if (!nfhp) 1590 (void) nfsrpc_lookup(dvp, cnp->cn_nameptr, 1591 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread, 1592 &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag, 1593 NULL); 1594 if (nfhp) 1595 error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, 1596 cnp->cn_thread, &np, NULL, LK_EXCLUSIVE); 1597 } 1598 if (dattrflag) 1599 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1); 1600 if (!error) { 1601 newvp = NFSTOV(np); 1602 if (attrflag != 0) { 1603 error = nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL, 1604 0, 1); 1605 if (error != 0) 1606 vput(newvp); 1607 } 1608 } 1609 if (!error) { 1610 *vpp = newvp; 1611 } else if (NFS_ISV4(dvp)) { 1612 error = nfscl_maperr(cnp->cn_thread, error, vap->va_uid, 1613 vap->va_gid); 1614 } 1615 dnp = VTONFS(dvp); 1616 NFSLOCKNODE(dnp); 1617 dnp->n_flag |= NMODIFIED; 1618 if (!dattrflag) { 1619 dnp->n_attrstamp = 0; 1620 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp); 1621 } 1622 NFSUNLOCKNODE(dnp); 1623 return (error); 1624 } 1625 1626 /* 1627 * nfs mknod vop 1628 * just call nfs_mknodrpc() to do the work. 1629 */ 1630 /* ARGSUSED */ 1631 static int 1632 nfs_mknod(struct vop_mknod_args *ap) 1633 { 1634 return (nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap)); 1635 } 1636 1637 static struct mtx nfs_cverf_mtx; 1638 MTX_SYSINIT(nfs_cverf_mtx, &nfs_cverf_mtx, "NFS create verifier mutex", 1639 MTX_DEF); 1640 1641 static nfsquad_t 1642 nfs_get_cverf(void) 1643 { 1644 static nfsquad_t cverf; 1645 nfsquad_t ret; 1646 static int cverf_initialized = 0; 1647 1648 mtx_lock(&nfs_cverf_mtx); 1649 if (cverf_initialized == 0) { 1650 cverf.lval[0] = arc4random(); 1651 cverf.lval[1] = arc4random(); 1652 cverf_initialized = 1; 1653 } else 1654 cverf.qval++; 1655 ret = cverf; 1656 mtx_unlock(&nfs_cverf_mtx); 1657 1658 return (ret); 1659 } 1660 1661 /* 1662 * nfs file create call 1663 */ 1664 static int 1665 nfs_create(struct vop_create_args *ap) 1666 { 1667 struct vnode *dvp = ap->a_dvp; 1668 struct vattr *vap = ap->a_vap; 1669 struct componentname *cnp = ap->a_cnp; 1670 struct nfsnode *np = NULL, *dnp; 1671 struct vnode *newvp = NULL; 1672 struct nfsmount *nmp; 1673 struct nfsvattr dnfsva, nfsva; 1674 struct nfsfh *nfhp; 1675 nfsquad_t cverf; 1676 int error = 0, attrflag, dattrflag, fmode = 0; 1677 struct vattr vattr; 1678 1679 /* 1680 * Oops, not for me.. 1681 */ 1682 if (vap->va_type == VSOCK) 1683 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap)); 1684 1685 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred))) 1686 return (error); 1687 if (vap->va_vaflags & VA_EXCLUSIVE) 1688 fmode |= O_EXCL; 1689 dnp = VTONFS(dvp); 1690 nmp = VFSTONFS(dvp->v_mount); 1691 again: 1692 /* For NFSv4, wait until any remove is done. */ 1693 NFSLOCKNODE(dnp); 1694 while (NFSHASNFSV4(nmp) && (dnp->n_flag & NREMOVEINPROG)) { 1695 dnp->n_flag |= NREMOVEWANT; 1696 (void) msleep((caddr_t)dnp, &dnp->n_mtx, PZERO, "nfscrt", 0); 1697 } 1698 NFSUNLOCKNODE(dnp); 1699 1700 cverf = nfs_get_cverf(); 1701 error = nfsrpc_create(dvp, cnp->cn_nameptr, cnp->cn_namelen, 1702 vap, cverf, fmode, cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva, 1703 &nfhp, &attrflag, &dattrflag, NULL); 1704 if (!error) { 1705 if (nfhp == NULL) 1706 (void) nfsrpc_lookup(dvp, cnp->cn_nameptr, 1707 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread, 1708 &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag, 1709 NULL); 1710 if (nfhp != NULL) 1711 error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, 1712 cnp->cn_thread, &np, NULL, LK_EXCLUSIVE); 1713 } 1714 if (dattrflag) 1715 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1); 1716 if (!error) { 1717 newvp = NFSTOV(np); 1718 if (attrflag == 0) 1719 error = nfsrpc_getattr(newvp, cnp->cn_cred, 1720 cnp->cn_thread, &nfsva, NULL); 1721 if (error == 0) 1722 error = nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL, 1723 0, 1); 1724 } 1725 if (error) { 1726 if (newvp != NULL) { 1727 vput(newvp); 1728 newvp = NULL; 1729 } 1730 if (NFS_ISV34(dvp) && (fmode & O_EXCL) && 1731 error == NFSERR_NOTSUPP) { 1732 fmode &= ~O_EXCL; 1733 goto again; 1734 } 1735 } else if (NFS_ISV34(dvp) && (fmode & O_EXCL)) { 1736 if (nfscl_checksattr(vap, &nfsva)) { 1737 error = nfsrpc_setattr(newvp, vap, NULL, cnp->cn_cred, 1738 cnp->cn_thread, &nfsva, &attrflag, NULL); 1739 if (error && (vap->va_uid != (uid_t)VNOVAL || 1740 vap->va_gid != (gid_t)VNOVAL)) { 1741 /* try again without setting uid/gid */ 1742 vap->va_uid = (uid_t)VNOVAL; 1743 vap->va_gid = (uid_t)VNOVAL; 1744 error = nfsrpc_setattr(newvp, vap, NULL, 1745 cnp->cn_cred, cnp->cn_thread, &nfsva, 1746 &attrflag, NULL); 1747 } 1748 if (attrflag) 1749 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, 1750 NULL, 0, 1); 1751 if (error != 0) 1752 vput(newvp); 1753 } 1754 } 1755 if (!error) { 1756 if ((cnp->cn_flags & MAKEENTRY) && attrflag) 1757 cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime, 1758 NULL); 1759 *ap->a_vpp = newvp; 1760 } else if (NFS_ISV4(dvp)) { 1761 error = nfscl_maperr(cnp->cn_thread, error, vap->va_uid, 1762 vap->va_gid); 1763 } 1764 NFSLOCKNODE(dnp); 1765 dnp->n_flag |= NMODIFIED; 1766 if (!dattrflag) { 1767 dnp->n_attrstamp = 0; 1768 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp); 1769 } 1770 NFSUNLOCKNODE(dnp); 1771 return (error); 1772 } 1773 1774 /* 1775 * nfs file remove call 1776 * To try and make nfs semantics closer to ufs semantics, a file that has 1777 * other processes using the vnode is renamed instead of removed and then 1778 * removed later on the last close. 1779 * - If v_usecount > 1 1780 * If a rename is not already in the works 1781 * call nfs_sillyrename() to set it up 1782 * else 1783 * do the remove rpc 1784 */ 1785 static int 1786 nfs_remove(struct vop_remove_args *ap) 1787 { 1788 struct vnode *vp = ap->a_vp; 1789 struct vnode *dvp = ap->a_dvp; 1790 struct componentname *cnp = ap->a_cnp; 1791 struct nfsnode *np = VTONFS(vp); 1792 int error = 0; 1793 struct vattr vattr; 1794 1795 KASSERT((cnp->cn_flags & HASBUF) != 0, ("nfs_remove: no name")); 1796 KASSERT(vrefcnt(vp) > 0, ("nfs_remove: bad v_usecount")); 1797 if (vp->v_type == VDIR) 1798 error = EPERM; 1799 else if (vrefcnt(vp) == 1 || (np->n_sillyrename && 1800 VOP_GETATTR(vp, &vattr, cnp->cn_cred) == 0 && 1801 vattr.va_nlink > 1)) { 1802 /* 1803 * Purge the name cache so that the chance of a lookup for 1804 * the name succeeding while the remove is in progress is 1805 * minimized. Without node locking it can still happen, such 1806 * that an I/O op returns ESTALE, but since you get this if 1807 * another host removes the file.. 1808 */ 1809 cache_purge(vp); 1810 /* 1811 * throw away biocache buffers, mainly to avoid 1812 * unnecessary delayed writes later. 1813 */ 1814 error = ncl_vinvalbuf(vp, 0, cnp->cn_thread, 1); 1815 if (error != EINTR && error != EIO) 1816 /* Do the rpc */ 1817 error = nfs_removerpc(dvp, vp, cnp->cn_nameptr, 1818 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread); 1819 /* 1820 * Kludge City: If the first reply to the remove rpc is lost.. 1821 * the reply to the retransmitted request will be ENOENT 1822 * since the file was in fact removed 1823 * Therefore, we cheat and return success. 1824 */ 1825 if (error == ENOENT) 1826 error = 0; 1827 } else if (!np->n_sillyrename) 1828 error = nfs_sillyrename(dvp, vp, cnp); 1829 NFSLOCKNODE(np); 1830 np->n_attrstamp = 0; 1831 NFSUNLOCKNODE(np); 1832 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp); 1833 return (error); 1834 } 1835 1836 /* 1837 * nfs file remove rpc called from nfs_inactive 1838 */ 1839 int 1840 ncl_removeit(struct sillyrename *sp, struct vnode *vp) 1841 { 1842 /* 1843 * Make sure that the directory vnode is still valid. 1844 * XXX we should lock sp->s_dvp here. 1845 */ 1846 if (sp->s_dvp->v_type == VBAD) 1847 return (0); 1848 return (nfs_removerpc(sp->s_dvp, vp, sp->s_name, sp->s_namlen, 1849 sp->s_cred, NULL)); 1850 } 1851 1852 /* 1853 * Nfs remove rpc, called from nfs_remove() and ncl_removeit(). 1854 */ 1855 static int 1856 nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name, 1857 int namelen, struct ucred *cred, struct thread *td) 1858 { 1859 struct nfsvattr dnfsva; 1860 struct nfsnode *dnp = VTONFS(dvp); 1861 int error = 0, dattrflag; 1862 1863 NFSLOCKNODE(dnp); 1864 dnp->n_flag |= NREMOVEINPROG; 1865 NFSUNLOCKNODE(dnp); 1866 error = nfsrpc_remove(dvp, name, namelen, vp, cred, td, &dnfsva, 1867 &dattrflag, NULL); 1868 NFSLOCKNODE(dnp); 1869 if ((dnp->n_flag & NREMOVEWANT)) { 1870 dnp->n_flag &= ~(NREMOVEWANT | NREMOVEINPROG); 1871 NFSUNLOCKNODE(dnp); 1872 wakeup((caddr_t)dnp); 1873 } else { 1874 dnp->n_flag &= ~NREMOVEINPROG; 1875 NFSUNLOCKNODE(dnp); 1876 } 1877 if (dattrflag) 1878 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1); 1879 NFSLOCKNODE(dnp); 1880 dnp->n_flag |= NMODIFIED; 1881 if (!dattrflag) { 1882 dnp->n_attrstamp = 0; 1883 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp); 1884 } 1885 NFSUNLOCKNODE(dnp); 1886 if (error && NFS_ISV4(dvp)) 1887 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0); 1888 return (error); 1889 } 1890 1891 /* 1892 * nfs file rename call 1893 */ 1894 static int 1895 nfs_rename(struct vop_rename_args *ap) 1896 { 1897 struct vnode *fvp = ap->a_fvp; 1898 struct vnode *tvp = ap->a_tvp; 1899 struct vnode *fdvp = ap->a_fdvp; 1900 struct vnode *tdvp = ap->a_tdvp; 1901 struct componentname *tcnp = ap->a_tcnp; 1902 struct componentname *fcnp = ap->a_fcnp; 1903 struct nfsnode *fnp = VTONFS(ap->a_fvp); 1904 struct nfsnode *tdnp = VTONFS(ap->a_tdvp); 1905 struct nfsv4node *newv4 = NULL; 1906 int error; 1907 1908 KASSERT((tcnp->cn_flags & HASBUF) != 0 && 1909 (fcnp->cn_flags & HASBUF) != 0, ("nfs_rename: no name")); 1910 /* Check for cross-device rename */ 1911 if ((fvp->v_mount != tdvp->v_mount) || 1912 (tvp && (fvp->v_mount != tvp->v_mount))) { 1913 error = EXDEV; 1914 goto out; 1915 } 1916 1917 if (fvp == tvp) { 1918 printf("nfs_rename: fvp == tvp (can't happen)\n"); 1919 error = 0; 1920 goto out; 1921 } 1922 if ((error = NFSVOPLOCK(fvp, LK_EXCLUSIVE)) != 0) 1923 goto out; 1924 1925 /* 1926 * We have to flush B_DELWRI data prior to renaming 1927 * the file. If we don't, the delayed-write buffers 1928 * can be flushed out later after the file has gone stale 1929 * under NFSV3. NFSV2 does not have this problem because 1930 * ( as far as I can tell ) it flushes dirty buffers more 1931 * often. 1932 * 1933 * Skip the rename operation if the fsync fails, this can happen 1934 * due to the server's volume being full, when we pushed out data 1935 * that was written back to our cache earlier. Not checking for 1936 * this condition can result in potential (silent) data loss. 1937 */ 1938 error = VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_thread); 1939 NFSVOPUNLOCK(fvp); 1940 if (!error && tvp) 1941 error = VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_thread); 1942 if (error) 1943 goto out; 1944 1945 /* 1946 * If the tvp exists and is in use, sillyrename it before doing the 1947 * rename of the new file over it. 1948 * XXX Can't sillyrename a directory. 1949 */ 1950 if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename && 1951 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) { 1952 vput(tvp); 1953 tvp = NULL; 1954 } 1955 1956 error = nfs_renamerpc(fdvp, fvp, fcnp->cn_nameptr, fcnp->cn_namelen, 1957 tdvp, tvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred, 1958 tcnp->cn_thread); 1959 1960 if (error == 0 && NFS_ISV4(tdvp)) { 1961 /* 1962 * For NFSv4, check to see if it is the same name and 1963 * replace the name, if it is different. 1964 */ 1965 newv4 = malloc( 1966 sizeof (struct nfsv4node) + 1967 tdnp->n_fhp->nfh_len + tcnp->cn_namelen - 1, 1968 M_NFSV4NODE, M_WAITOK); 1969 NFSLOCKNODE(tdnp); 1970 NFSLOCKNODE(fnp); 1971 if (fnp->n_v4 != NULL && fvp->v_type == VREG && 1972 (fnp->n_v4->n4_namelen != tcnp->cn_namelen || 1973 NFSBCMP(tcnp->cn_nameptr, NFS4NODENAME(fnp->n_v4), 1974 tcnp->cn_namelen) || 1975 tdnp->n_fhp->nfh_len != fnp->n_v4->n4_fhlen || 1976 NFSBCMP(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data, 1977 tdnp->n_fhp->nfh_len))) { 1978 #ifdef notdef 1979 { char nnn[100]; int nnnl; 1980 nnnl = (tcnp->cn_namelen < 100) ? tcnp->cn_namelen : 99; 1981 bcopy(tcnp->cn_nameptr, nnn, nnnl); 1982 nnn[nnnl] = '\0'; 1983 printf("ren replace=%s\n",nnn); 1984 } 1985 #endif 1986 free(fnp->n_v4, M_NFSV4NODE); 1987 fnp->n_v4 = newv4; 1988 newv4 = NULL; 1989 fnp->n_v4->n4_fhlen = tdnp->n_fhp->nfh_len; 1990 fnp->n_v4->n4_namelen = tcnp->cn_namelen; 1991 NFSBCOPY(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data, 1992 tdnp->n_fhp->nfh_len); 1993 NFSBCOPY(tcnp->cn_nameptr, 1994 NFS4NODENAME(fnp->n_v4), tcnp->cn_namelen); 1995 } 1996 NFSUNLOCKNODE(tdnp); 1997 NFSUNLOCKNODE(fnp); 1998 if (newv4 != NULL) 1999 free(newv4, M_NFSV4NODE); 2000 } 2001 2002 if (fvp->v_type == VDIR) { 2003 if (tvp != NULL && tvp->v_type == VDIR) 2004 cache_purge(tdvp); 2005 cache_purge(fdvp); 2006 } 2007 2008 out: 2009 if (tdvp == tvp) 2010 vrele(tdvp); 2011 else 2012 vput(tdvp); 2013 if (tvp) 2014 vput(tvp); 2015 vrele(fdvp); 2016 vrele(fvp); 2017 /* 2018 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry. 2019 */ 2020 if (error == ENOENT) 2021 error = 0; 2022 return (error); 2023 } 2024 2025 /* 2026 * nfs file rename rpc called from nfs_remove() above 2027 */ 2028 static int 2029 nfs_renameit(struct vnode *sdvp, struct vnode *svp, struct componentname *scnp, 2030 struct sillyrename *sp) 2031 { 2032 2033 return (nfs_renamerpc(sdvp, svp, scnp->cn_nameptr, scnp->cn_namelen, 2034 sdvp, NULL, sp->s_name, sp->s_namlen, scnp->cn_cred, 2035 scnp->cn_thread)); 2036 } 2037 2038 /* 2039 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit(). 2040 */ 2041 static int 2042 nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp, char *fnameptr, 2043 int fnamelen, struct vnode *tdvp, struct vnode *tvp, char *tnameptr, 2044 int tnamelen, struct ucred *cred, struct thread *td) 2045 { 2046 struct nfsvattr fnfsva, tnfsva; 2047 struct nfsnode *fdnp = VTONFS(fdvp); 2048 struct nfsnode *tdnp = VTONFS(tdvp); 2049 int error = 0, fattrflag, tattrflag; 2050 2051 error = nfsrpc_rename(fdvp, fvp, fnameptr, fnamelen, tdvp, tvp, 2052 tnameptr, tnamelen, cred, td, &fnfsva, &tnfsva, &fattrflag, 2053 &tattrflag, NULL, NULL); 2054 NFSLOCKNODE(fdnp); 2055 fdnp->n_flag |= NMODIFIED; 2056 if (fattrflag != 0) { 2057 NFSUNLOCKNODE(fdnp); 2058 (void) nfscl_loadattrcache(&fdvp, &fnfsva, NULL, NULL, 0, 1); 2059 } else { 2060 fdnp->n_attrstamp = 0; 2061 NFSUNLOCKNODE(fdnp); 2062 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(fdvp); 2063 } 2064 NFSLOCKNODE(tdnp); 2065 tdnp->n_flag |= NMODIFIED; 2066 if (tattrflag != 0) { 2067 NFSUNLOCKNODE(tdnp); 2068 (void) nfscl_loadattrcache(&tdvp, &tnfsva, NULL, NULL, 0, 1); 2069 } else { 2070 tdnp->n_attrstamp = 0; 2071 NFSUNLOCKNODE(tdnp); 2072 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp); 2073 } 2074 if (error && NFS_ISV4(fdvp)) 2075 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0); 2076 return (error); 2077 } 2078 2079 /* 2080 * nfs hard link create call 2081 */ 2082 static int 2083 nfs_link(struct vop_link_args *ap) 2084 { 2085 struct vnode *vp = ap->a_vp; 2086 struct vnode *tdvp = ap->a_tdvp; 2087 struct componentname *cnp = ap->a_cnp; 2088 struct nfsnode *np, *tdnp; 2089 struct nfsvattr nfsva, dnfsva; 2090 int error = 0, attrflag, dattrflag; 2091 2092 /* 2093 * Push all writes to the server, so that the attribute cache 2094 * doesn't get "out of sync" with the server. 2095 * XXX There should be a better way! 2096 */ 2097 VOP_FSYNC(vp, MNT_WAIT, cnp->cn_thread); 2098 2099 error = nfsrpc_link(tdvp, vp, cnp->cn_nameptr, cnp->cn_namelen, 2100 cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva, &attrflag, 2101 &dattrflag, NULL); 2102 tdnp = VTONFS(tdvp); 2103 NFSLOCKNODE(tdnp); 2104 tdnp->n_flag |= NMODIFIED; 2105 if (dattrflag != 0) { 2106 NFSUNLOCKNODE(tdnp); 2107 (void) nfscl_loadattrcache(&tdvp, &dnfsva, NULL, NULL, 0, 1); 2108 } else { 2109 tdnp->n_attrstamp = 0; 2110 NFSUNLOCKNODE(tdnp); 2111 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp); 2112 } 2113 if (attrflag) 2114 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1); 2115 else { 2116 np = VTONFS(vp); 2117 NFSLOCKNODE(np); 2118 np->n_attrstamp = 0; 2119 NFSUNLOCKNODE(np); 2120 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp); 2121 } 2122 /* 2123 * If negative lookup caching is enabled, I might as well 2124 * add an entry for this node. Not necessary for correctness, 2125 * but if negative caching is enabled, then the system 2126 * must care about lookup caching hit rate, so... 2127 */ 2128 if (VFSTONFS(vp->v_mount)->nm_negnametimeo != 0 && 2129 (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) { 2130 cache_enter_time(tdvp, vp, cnp, &nfsva.na_ctime, NULL); 2131 } 2132 if (error && NFS_ISV4(vp)) 2133 error = nfscl_maperr(cnp->cn_thread, error, (uid_t)0, 2134 (gid_t)0); 2135 return (error); 2136 } 2137 2138 /* 2139 * nfs symbolic link create call 2140 */ 2141 static int 2142 nfs_symlink(struct vop_symlink_args *ap) 2143 { 2144 struct vnode *dvp = ap->a_dvp; 2145 struct vattr *vap = ap->a_vap; 2146 struct componentname *cnp = ap->a_cnp; 2147 struct nfsvattr nfsva, dnfsva; 2148 struct nfsfh *nfhp; 2149 struct nfsnode *np = NULL, *dnp; 2150 struct vnode *newvp = NULL; 2151 int error = 0, attrflag, dattrflag, ret; 2152 2153 vap->va_type = VLNK; 2154 error = nfsrpc_symlink(dvp, cnp->cn_nameptr, cnp->cn_namelen, 2155 ap->a_target, vap, cnp->cn_cred, cnp->cn_thread, &dnfsva, 2156 &nfsva, &nfhp, &attrflag, &dattrflag, NULL); 2157 if (nfhp) { 2158 ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, cnp->cn_thread, 2159 &np, NULL, LK_EXCLUSIVE); 2160 if (!ret) 2161 newvp = NFSTOV(np); 2162 else if (!error) 2163 error = ret; 2164 } 2165 if (newvp != NULL) { 2166 if (attrflag) 2167 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL, 2168 0, 1); 2169 } else if (!error) { 2170 /* 2171 * If we do not have an error and we could not extract the 2172 * newvp from the response due to the request being NFSv2, we 2173 * have to do a lookup in order to obtain a newvp to return. 2174 */ 2175 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen, 2176 cnp->cn_cred, cnp->cn_thread, &np); 2177 if (!error) 2178 newvp = NFSTOV(np); 2179 } 2180 if (error) { 2181 if (newvp) 2182 vput(newvp); 2183 if (NFS_ISV4(dvp)) 2184 error = nfscl_maperr(cnp->cn_thread, error, 2185 vap->va_uid, vap->va_gid); 2186 } else { 2187 *ap->a_vpp = newvp; 2188 } 2189 2190 dnp = VTONFS(dvp); 2191 NFSLOCKNODE(dnp); 2192 dnp->n_flag |= NMODIFIED; 2193 if (dattrflag != 0) { 2194 NFSUNLOCKNODE(dnp); 2195 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1); 2196 } else { 2197 dnp->n_attrstamp = 0; 2198 NFSUNLOCKNODE(dnp); 2199 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp); 2200 } 2201 /* 2202 * If negative lookup caching is enabled, I might as well 2203 * add an entry for this node. Not necessary for correctness, 2204 * but if negative caching is enabled, then the system 2205 * must care about lookup caching hit rate, so... 2206 */ 2207 if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 && 2208 (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) { 2209 cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime, NULL); 2210 } 2211 return (error); 2212 } 2213 2214 /* 2215 * nfs make dir call 2216 */ 2217 static int 2218 nfs_mkdir(struct vop_mkdir_args *ap) 2219 { 2220 struct vnode *dvp = ap->a_dvp; 2221 struct vattr *vap = ap->a_vap; 2222 struct componentname *cnp = ap->a_cnp; 2223 struct nfsnode *np = NULL, *dnp; 2224 struct vnode *newvp = NULL; 2225 struct vattr vattr; 2226 struct nfsfh *nfhp; 2227 struct nfsvattr nfsva, dnfsva; 2228 int error = 0, attrflag, dattrflag, ret; 2229 2230 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)) != 0) 2231 return (error); 2232 vap->va_type = VDIR; 2233 error = nfsrpc_mkdir(dvp, cnp->cn_nameptr, cnp->cn_namelen, 2234 vap, cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva, &nfhp, 2235 &attrflag, &dattrflag, NULL); 2236 dnp = VTONFS(dvp); 2237 NFSLOCKNODE(dnp); 2238 dnp->n_flag |= NMODIFIED; 2239 if (dattrflag != 0) { 2240 NFSUNLOCKNODE(dnp); 2241 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1); 2242 } else { 2243 dnp->n_attrstamp = 0; 2244 NFSUNLOCKNODE(dnp); 2245 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp); 2246 } 2247 if (nfhp) { 2248 ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, cnp->cn_thread, 2249 &np, NULL, LK_EXCLUSIVE); 2250 if (!ret) { 2251 newvp = NFSTOV(np); 2252 if (attrflag) 2253 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, 2254 NULL, 0, 1); 2255 } else if (!error) 2256 error = ret; 2257 } 2258 if (!error && newvp == NULL) { 2259 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen, 2260 cnp->cn_cred, cnp->cn_thread, &np); 2261 if (!error) { 2262 newvp = NFSTOV(np); 2263 if (newvp->v_type != VDIR) 2264 error = EEXIST; 2265 } 2266 } 2267 if (error) { 2268 if (newvp) 2269 vput(newvp); 2270 if (NFS_ISV4(dvp)) 2271 error = nfscl_maperr(cnp->cn_thread, error, 2272 vap->va_uid, vap->va_gid); 2273 } else { 2274 /* 2275 * If negative lookup caching is enabled, I might as well 2276 * add an entry for this node. Not necessary for correctness, 2277 * but if negative caching is enabled, then the system 2278 * must care about lookup caching hit rate, so... 2279 */ 2280 if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 && 2281 (cnp->cn_flags & MAKEENTRY) && 2282 attrflag != 0 && dattrflag != 0) 2283 cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime, 2284 &dnfsva.na_ctime); 2285 *ap->a_vpp = newvp; 2286 } 2287 return (error); 2288 } 2289 2290 /* 2291 * nfs remove directory call 2292 */ 2293 static int 2294 nfs_rmdir(struct vop_rmdir_args *ap) 2295 { 2296 struct vnode *vp = ap->a_vp; 2297 struct vnode *dvp = ap->a_dvp; 2298 struct componentname *cnp = ap->a_cnp; 2299 struct nfsnode *dnp; 2300 struct nfsvattr dnfsva; 2301 int error, dattrflag; 2302 2303 if (dvp == vp) 2304 return (EINVAL); 2305 error = nfsrpc_rmdir(dvp, cnp->cn_nameptr, cnp->cn_namelen, 2306 cnp->cn_cred, cnp->cn_thread, &dnfsva, &dattrflag, NULL); 2307 dnp = VTONFS(dvp); 2308 NFSLOCKNODE(dnp); 2309 dnp->n_flag |= NMODIFIED; 2310 if (dattrflag != 0) { 2311 NFSUNLOCKNODE(dnp); 2312 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1); 2313 } else { 2314 dnp->n_attrstamp = 0; 2315 NFSUNLOCKNODE(dnp); 2316 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp); 2317 } 2318 2319 cache_purge(dvp); 2320 cache_purge(vp); 2321 if (error && NFS_ISV4(dvp)) 2322 error = nfscl_maperr(cnp->cn_thread, error, (uid_t)0, 2323 (gid_t)0); 2324 /* 2325 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry. 2326 */ 2327 if (error == ENOENT) 2328 error = 0; 2329 return (error); 2330 } 2331 2332 /* 2333 * nfs readdir call 2334 */ 2335 static int 2336 nfs_readdir(struct vop_readdir_args *ap) 2337 { 2338 struct vnode *vp = ap->a_vp; 2339 struct nfsnode *np = VTONFS(vp); 2340 struct uio *uio = ap->a_uio; 2341 ssize_t tresid, left; 2342 int error = 0; 2343 struct vattr vattr; 2344 2345 if (ap->a_eofflag != NULL) 2346 *ap->a_eofflag = 0; 2347 if (vp->v_type != VDIR) 2348 return(EPERM); 2349 2350 /* 2351 * First, check for hit on the EOF offset cache 2352 */ 2353 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset && 2354 (np->n_flag & NMODIFIED) == 0) { 2355 if (VOP_GETATTR(vp, &vattr, ap->a_cred) == 0) { 2356 NFSLOCKNODE(np); 2357 if ((NFS_ISV4(vp) && np->n_change == vattr.va_filerev) || 2358 !NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) { 2359 NFSUNLOCKNODE(np); 2360 NFSINCRGLOBAL(nfsstatsv1.direofcache_hits); 2361 if (ap->a_eofflag != NULL) 2362 *ap->a_eofflag = 1; 2363 return (0); 2364 } else 2365 NFSUNLOCKNODE(np); 2366 } 2367 } 2368 2369 /* 2370 * NFS always guarantees that directory entries don't straddle 2371 * DIRBLKSIZ boundaries. As such, we need to limit the size 2372 * to an exact multiple of DIRBLKSIZ, to avoid copying a partial 2373 * directory entry. 2374 */ 2375 left = uio->uio_resid % DIRBLKSIZ; 2376 if (left == uio->uio_resid) 2377 return (EINVAL); 2378 uio->uio_resid -= left; 2379 2380 /* 2381 * Call ncl_bioread() to do the real work. 2382 */ 2383 tresid = uio->uio_resid; 2384 error = ncl_bioread(vp, uio, 0, ap->a_cred); 2385 2386 if (!error && uio->uio_resid == tresid) { 2387 NFSINCRGLOBAL(nfsstatsv1.direofcache_misses); 2388 if (ap->a_eofflag != NULL) 2389 *ap->a_eofflag = 1; 2390 } 2391 2392 /* Add the partial DIRBLKSIZ (left) back in. */ 2393 uio->uio_resid += left; 2394 return (error); 2395 } 2396 2397 /* 2398 * Readdir rpc call. 2399 * Called from below the buffer cache by ncl_doio(). 2400 */ 2401 int 2402 ncl_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred, 2403 struct thread *td) 2404 { 2405 struct nfsvattr nfsva; 2406 nfsuint64 *cookiep, cookie; 2407 struct nfsnode *dnp = VTONFS(vp); 2408 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 2409 int error = 0, eof, attrflag; 2410 2411 KASSERT(uiop->uio_iovcnt == 1 && 2412 (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 && 2413 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0, 2414 ("nfs readdirrpc bad uio")); 2415 2416 /* 2417 * If there is no cookie, assume directory was stale. 2418 */ 2419 ncl_dircookie_lock(dnp); 2420 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0); 2421 if (cookiep) { 2422 cookie = *cookiep; 2423 ncl_dircookie_unlock(dnp); 2424 } else { 2425 ncl_dircookie_unlock(dnp); 2426 return (NFSERR_BAD_COOKIE); 2427 } 2428 2429 if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp)) 2430 (void)ncl_fsinfo(nmp, vp, cred, td); 2431 2432 error = nfsrpc_readdir(vp, uiop, &cookie, cred, td, &nfsva, 2433 &attrflag, &eof, NULL); 2434 if (attrflag) 2435 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1); 2436 2437 if (!error) { 2438 /* 2439 * We are now either at the end of the directory or have filled 2440 * the block. 2441 */ 2442 if (eof) 2443 dnp->n_direofoffset = uiop->uio_offset; 2444 else { 2445 if (uiop->uio_resid > 0) 2446 printf("EEK! readdirrpc resid > 0\n"); 2447 ncl_dircookie_lock(dnp); 2448 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1); 2449 *cookiep = cookie; 2450 ncl_dircookie_unlock(dnp); 2451 } 2452 } else if (NFS_ISV4(vp)) { 2453 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0); 2454 } 2455 return (error); 2456 } 2457 2458 /* 2459 * NFS V3 readdir plus RPC. Used in place of ncl_readdirrpc(). 2460 */ 2461 int 2462 ncl_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred, 2463 struct thread *td) 2464 { 2465 struct nfsvattr nfsva; 2466 nfsuint64 *cookiep, cookie; 2467 struct nfsnode *dnp = VTONFS(vp); 2468 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 2469 int error = 0, attrflag, eof; 2470 2471 KASSERT(uiop->uio_iovcnt == 1 && 2472 (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 && 2473 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0, 2474 ("nfs readdirplusrpc bad uio")); 2475 2476 /* 2477 * If there is no cookie, assume directory was stale. 2478 */ 2479 ncl_dircookie_lock(dnp); 2480 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0); 2481 if (cookiep) { 2482 cookie = *cookiep; 2483 ncl_dircookie_unlock(dnp); 2484 } else { 2485 ncl_dircookie_unlock(dnp); 2486 return (NFSERR_BAD_COOKIE); 2487 } 2488 2489 if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp)) 2490 (void)ncl_fsinfo(nmp, vp, cred, td); 2491 error = nfsrpc_readdirplus(vp, uiop, &cookie, cred, td, &nfsva, 2492 &attrflag, &eof, NULL); 2493 if (attrflag) 2494 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1); 2495 2496 if (!error) { 2497 /* 2498 * We are now either at end of the directory or have filled the 2499 * the block. 2500 */ 2501 if (eof) 2502 dnp->n_direofoffset = uiop->uio_offset; 2503 else { 2504 if (uiop->uio_resid > 0) 2505 printf("EEK! readdirplusrpc resid > 0\n"); 2506 ncl_dircookie_lock(dnp); 2507 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1); 2508 *cookiep = cookie; 2509 ncl_dircookie_unlock(dnp); 2510 } 2511 } else if (NFS_ISV4(vp)) { 2512 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0); 2513 } 2514 return (error); 2515 } 2516 2517 /* 2518 * Silly rename. To make the NFS filesystem that is stateless look a little 2519 * more like the "ufs" a remove of an active vnode is translated to a rename 2520 * to a funny looking filename that is removed by nfs_inactive on the 2521 * nfsnode. There is the potential for another process on a different client 2522 * to create the same funny name between the nfs_lookitup() fails and the 2523 * nfs_rename() completes, but... 2524 */ 2525 static int 2526 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp) 2527 { 2528 struct sillyrename *sp; 2529 struct nfsnode *np; 2530 int error; 2531 short pid; 2532 unsigned int lticks; 2533 2534 cache_purge(dvp); 2535 np = VTONFS(vp); 2536 KASSERT(vp->v_type != VDIR, ("nfs: sillyrename dir")); 2537 sp = malloc(sizeof (struct sillyrename), 2538 M_NEWNFSREQ, M_WAITOK); 2539 sp->s_cred = crhold(cnp->cn_cred); 2540 sp->s_dvp = dvp; 2541 VREF(dvp); 2542 2543 /* 2544 * Fudge together a funny name. 2545 * Changing the format of the funny name to accommodate more 2546 * sillynames per directory. 2547 * The name is now changed to .nfs.<ticks>.<pid>.4, where ticks is 2548 * CPU ticks since boot. 2549 */ 2550 pid = cnp->cn_thread->td_proc->p_pid; 2551 lticks = (unsigned int)ticks; 2552 for ( ; ; ) { 2553 sp->s_namlen = sprintf(sp->s_name, 2554 ".nfs.%08x.%04x4.4", lticks, 2555 pid); 2556 if (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred, 2557 cnp->cn_thread, NULL)) 2558 break; 2559 lticks++; 2560 } 2561 error = nfs_renameit(dvp, vp, cnp, sp); 2562 if (error) 2563 goto bad; 2564 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred, 2565 cnp->cn_thread, &np); 2566 np->n_sillyrename = sp; 2567 return (0); 2568 bad: 2569 vrele(sp->s_dvp); 2570 crfree(sp->s_cred); 2571 free(sp, M_NEWNFSREQ); 2572 return (error); 2573 } 2574 2575 /* 2576 * Look up a file name and optionally either update the file handle or 2577 * allocate an nfsnode, depending on the value of npp. 2578 * npp == NULL --> just do the lookup 2579 * *npp == NULL --> allocate a new nfsnode and make sure attributes are 2580 * handled too 2581 * *npp != NULL --> update the file handle in the vnode 2582 */ 2583 static int 2584 nfs_lookitup(struct vnode *dvp, char *name, int len, struct ucred *cred, 2585 struct thread *td, struct nfsnode **npp) 2586 { 2587 struct vnode *newvp = NULL, *vp; 2588 struct nfsnode *np, *dnp = VTONFS(dvp); 2589 struct nfsfh *nfhp, *onfhp; 2590 struct nfsvattr nfsva, dnfsva; 2591 struct componentname cn; 2592 int error = 0, attrflag, dattrflag; 2593 u_int hash; 2594 2595 error = nfsrpc_lookup(dvp, name, len, cred, td, &dnfsva, &nfsva, 2596 &nfhp, &attrflag, &dattrflag, NULL); 2597 if (dattrflag) 2598 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1); 2599 if (npp && !error) { 2600 if (*npp != NULL) { 2601 np = *npp; 2602 vp = NFSTOV(np); 2603 /* 2604 * For NFSv4, check to see if it is the same name and 2605 * replace the name, if it is different. 2606 */ 2607 if (np->n_v4 != NULL && nfsva.na_type == VREG && 2608 (np->n_v4->n4_namelen != len || 2609 NFSBCMP(name, NFS4NODENAME(np->n_v4), len) || 2610 dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen || 2611 NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data, 2612 dnp->n_fhp->nfh_len))) { 2613 #ifdef notdef 2614 { char nnn[100]; int nnnl; 2615 nnnl = (len < 100) ? len : 99; 2616 bcopy(name, nnn, nnnl); 2617 nnn[nnnl] = '\0'; 2618 printf("replace=%s\n",nnn); 2619 } 2620 #endif 2621 free(np->n_v4, M_NFSV4NODE); 2622 np->n_v4 = malloc( 2623 sizeof (struct nfsv4node) + 2624 dnp->n_fhp->nfh_len + len - 1, 2625 M_NFSV4NODE, M_WAITOK); 2626 np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len; 2627 np->n_v4->n4_namelen = len; 2628 NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data, 2629 dnp->n_fhp->nfh_len); 2630 NFSBCOPY(name, NFS4NODENAME(np->n_v4), len); 2631 } 2632 hash = fnv_32_buf(nfhp->nfh_fh, nfhp->nfh_len, 2633 FNV1_32_INIT); 2634 onfhp = np->n_fhp; 2635 /* 2636 * Rehash node for new file handle. 2637 */ 2638 vfs_hash_rehash(vp, hash); 2639 np->n_fhp = nfhp; 2640 if (onfhp != NULL) 2641 free(onfhp, M_NFSFH); 2642 newvp = NFSTOV(np); 2643 } else if (NFS_CMPFH(dnp, nfhp->nfh_fh, nfhp->nfh_len)) { 2644 free(nfhp, M_NFSFH); 2645 VREF(dvp); 2646 newvp = dvp; 2647 } else { 2648 cn.cn_nameptr = name; 2649 cn.cn_namelen = len; 2650 error = nfscl_nget(dvp->v_mount, dvp, nfhp, &cn, td, 2651 &np, NULL, LK_EXCLUSIVE); 2652 if (error) 2653 return (error); 2654 newvp = NFSTOV(np); 2655 } 2656 if (!attrflag && *npp == NULL) { 2657 if (newvp == dvp) 2658 vrele(newvp); 2659 else 2660 vput(newvp); 2661 return (ENOENT); 2662 } 2663 if (attrflag) 2664 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL, 2665 0, 1); 2666 } 2667 if (npp && *npp == NULL) { 2668 if (error) { 2669 if (newvp) { 2670 if (newvp == dvp) 2671 vrele(newvp); 2672 else 2673 vput(newvp); 2674 } 2675 } else 2676 *npp = np; 2677 } 2678 if (error && NFS_ISV4(dvp)) 2679 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0); 2680 return (error); 2681 } 2682 2683 /* 2684 * Nfs Version 3 and 4 commit rpc 2685 */ 2686 int 2687 ncl_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred, 2688 struct thread *td) 2689 { 2690 struct nfsvattr nfsva; 2691 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 2692 struct nfsnode *np; 2693 struct uio uio; 2694 int error, attrflag; 2695 2696 np = VTONFS(vp); 2697 error = EIO; 2698 attrflag = 0; 2699 if (NFSHASPNFS(nmp) && (np->n_flag & NDSCOMMIT) != 0) { 2700 uio.uio_offset = offset; 2701 uio.uio_resid = cnt; 2702 error = nfscl_doiods(vp, &uio, NULL, NULL, 2703 NFSV4OPEN_ACCESSWRITE, 1, cred, td); 2704 if (error != 0) { 2705 NFSLOCKNODE(np); 2706 np->n_flag &= ~NDSCOMMIT; 2707 NFSUNLOCKNODE(np); 2708 } 2709 } 2710 if (error != 0) { 2711 mtx_lock(&nmp->nm_mtx); 2712 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) { 2713 mtx_unlock(&nmp->nm_mtx); 2714 return (0); 2715 } 2716 mtx_unlock(&nmp->nm_mtx); 2717 error = nfsrpc_commit(vp, offset, cnt, cred, td, &nfsva, 2718 &attrflag, NULL); 2719 } 2720 if (attrflag != 0) 2721 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 2722 0, 1); 2723 if (error != 0 && NFS_ISV4(vp)) 2724 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0); 2725 return (error); 2726 } 2727 2728 /* 2729 * Strategy routine. 2730 * For async requests when nfsiod(s) are running, queue the request by 2731 * calling ncl_asyncio(), otherwise just all ncl_doio() to do the 2732 * request. 2733 */ 2734 static int 2735 nfs_strategy(struct vop_strategy_args *ap) 2736 { 2737 struct buf *bp; 2738 struct vnode *vp; 2739 struct ucred *cr; 2740 2741 bp = ap->a_bp; 2742 vp = ap->a_vp; 2743 KASSERT(bp->b_vp == vp, ("missing b_getvp")); 2744 KASSERT(!(bp->b_flags & B_DONE), 2745 ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp)); 2746 2747 if (vp->v_type == VREG && bp->b_blkno == bp->b_lblkno) 2748 bp->b_blkno = bp->b_lblkno * (vp->v_bufobj.bo_bsize / 2749 DEV_BSIZE); 2750 if (bp->b_iocmd == BIO_READ) 2751 cr = bp->b_rcred; 2752 else 2753 cr = bp->b_wcred; 2754 2755 /* 2756 * If the op is asynchronous and an i/o daemon is waiting 2757 * queue the request, wake it up and wait for completion 2758 * otherwise just do it ourselves. 2759 */ 2760 if ((bp->b_flags & B_ASYNC) == 0 || 2761 ncl_asyncio(VFSTONFS(vp->v_mount), bp, NOCRED, curthread)) 2762 (void) ncl_doio(vp, bp, cr, curthread, 1); 2763 return (0); 2764 } 2765 2766 /* 2767 * fsync vnode op. Just call ncl_flush() with commit == 1. 2768 */ 2769 /* ARGSUSED */ 2770 static int 2771 nfs_fsync(struct vop_fsync_args *ap) 2772 { 2773 2774 if (ap->a_vp->v_type != VREG) { 2775 /* 2776 * For NFS, metadata is changed synchronously on the server, 2777 * so there is nothing to flush. Also, ncl_flush() clears 2778 * the NMODIFIED flag and that shouldn't be done here for 2779 * directories. 2780 */ 2781 return (0); 2782 } 2783 return (ncl_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1, 0)); 2784 } 2785 2786 /* 2787 * Flush all the blocks associated with a vnode. 2788 * Walk through the buffer pool and push any dirty pages 2789 * associated with the vnode. 2790 * If the called_from_renewthread argument is TRUE, it has been called 2791 * from the NFSv4 renew thread and, as such, cannot block indefinitely 2792 * waiting for a buffer write to complete. 2793 */ 2794 int 2795 ncl_flush(struct vnode *vp, int waitfor, struct thread *td, 2796 int commit, int called_from_renewthread) 2797 { 2798 struct nfsnode *np = VTONFS(vp); 2799 struct buf *bp; 2800 int i; 2801 struct buf *nbp; 2802 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 2803 int error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos; 2804 int passone = 1, trycnt = 0; 2805 u_quad_t off, endoff, toff; 2806 struct ucred* wcred = NULL; 2807 struct buf **bvec = NULL; 2808 struct bufobj *bo; 2809 #ifndef NFS_COMMITBVECSIZ 2810 #define NFS_COMMITBVECSIZ 20 2811 #endif 2812 struct buf *bvec_on_stack[NFS_COMMITBVECSIZ]; 2813 u_int bvecsize = 0, bveccount; 2814 2815 if (called_from_renewthread != 0) 2816 slptimeo = hz; 2817 if (nmp->nm_flag & NFSMNT_INT) 2818 slpflag = PCATCH; 2819 if (!commit) 2820 passone = 0; 2821 bo = &vp->v_bufobj; 2822 /* 2823 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the 2824 * server, but has not been committed to stable storage on the server 2825 * yet. On the first pass, the byte range is worked out and the commit 2826 * rpc is done. On the second pass, ncl_writebp() is called to do the 2827 * job. 2828 */ 2829 again: 2830 off = (u_quad_t)-1; 2831 endoff = 0; 2832 bvecpos = 0; 2833 if (NFS_ISV34(vp) && commit) { 2834 if (bvec != NULL && bvec != bvec_on_stack) 2835 free(bvec, M_TEMP); 2836 /* 2837 * Count up how many buffers waiting for a commit. 2838 */ 2839 bveccount = 0; 2840 BO_LOCK(bo); 2841 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 2842 if (!BUF_ISLOCKED(bp) && 2843 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) 2844 == (B_DELWRI | B_NEEDCOMMIT)) 2845 bveccount++; 2846 } 2847 /* 2848 * Allocate space to remember the list of bufs to commit. It is 2849 * important to use M_NOWAIT here to avoid a race with nfs_write. 2850 * If we can't get memory (for whatever reason), we will end up 2851 * committing the buffers one-by-one in the loop below. 2852 */ 2853 if (bveccount > NFS_COMMITBVECSIZ) { 2854 /* 2855 * Release the vnode interlock to avoid a lock 2856 * order reversal. 2857 */ 2858 BO_UNLOCK(bo); 2859 bvec = (struct buf **) 2860 malloc(bveccount * sizeof(struct buf *), 2861 M_TEMP, M_NOWAIT); 2862 BO_LOCK(bo); 2863 if (bvec == NULL) { 2864 bvec = bvec_on_stack; 2865 bvecsize = NFS_COMMITBVECSIZ; 2866 } else 2867 bvecsize = bveccount; 2868 } else { 2869 bvec = bvec_on_stack; 2870 bvecsize = NFS_COMMITBVECSIZ; 2871 } 2872 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 2873 if (bvecpos >= bvecsize) 2874 break; 2875 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) { 2876 nbp = TAILQ_NEXT(bp, b_bobufs); 2877 continue; 2878 } 2879 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) != 2880 (B_DELWRI | B_NEEDCOMMIT)) { 2881 BUF_UNLOCK(bp); 2882 nbp = TAILQ_NEXT(bp, b_bobufs); 2883 continue; 2884 } 2885 BO_UNLOCK(bo); 2886 bremfree(bp); 2887 /* 2888 * Work out if all buffers are using the same cred 2889 * so we can deal with them all with one commit. 2890 * 2891 * NOTE: we are not clearing B_DONE here, so we have 2892 * to do it later on in this routine if we intend to 2893 * initiate I/O on the bp. 2894 * 2895 * Note: to avoid loopback deadlocks, we do not 2896 * assign b_runningbufspace. 2897 */ 2898 if (wcred == NULL) 2899 wcred = bp->b_wcred; 2900 else if (wcred != bp->b_wcred) 2901 wcred = NOCRED; 2902 vfs_busy_pages(bp, 1); 2903 2904 BO_LOCK(bo); 2905 /* 2906 * bp is protected by being locked, but nbp is not 2907 * and vfs_busy_pages() may sleep. We have to 2908 * recalculate nbp. 2909 */ 2910 nbp = TAILQ_NEXT(bp, b_bobufs); 2911 2912 /* 2913 * A list of these buffers is kept so that the 2914 * second loop knows which buffers have actually 2915 * been committed. This is necessary, since there 2916 * may be a race between the commit rpc and new 2917 * uncommitted writes on the file. 2918 */ 2919 bvec[bvecpos++] = bp; 2920 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE + 2921 bp->b_dirtyoff; 2922 if (toff < off) 2923 off = toff; 2924 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff); 2925 if (toff > endoff) 2926 endoff = toff; 2927 } 2928 BO_UNLOCK(bo); 2929 } 2930 if (bvecpos > 0) { 2931 /* 2932 * Commit data on the server, as required. 2933 * If all bufs are using the same wcred, then use that with 2934 * one call for all of them, otherwise commit each one 2935 * separately. 2936 */ 2937 if (wcred != NOCRED) 2938 retv = ncl_commit(vp, off, (int)(endoff - off), 2939 wcred, td); 2940 else { 2941 retv = 0; 2942 for (i = 0; i < bvecpos; i++) { 2943 off_t off, size; 2944 bp = bvec[i]; 2945 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE + 2946 bp->b_dirtyoff; 2947 size = (u_quad_t)(bp->b_dirtyend 2948 - bp->b_dirtyoff); 2949 retv = ncl_commit(vp, off, (int)size, 2950 bp->b_wcred, td); 2951 if (retv) break; 2952 } 2953 } 2954 2955 if (retv == NFSERR_STALEWRITEVERF) 2956 ncl_clearcommit(vp->v_mount); 2957 2958 /* 2959 * Now, either mark the blocks I/O done or mark the 2960 * blocks dirty, depending on whether the commit 2961 * succeeded. 2962 */ 2963 for (i = 0; i < bvecpos; i++) { 2964 bp = bvec[i]; 2965 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 2966 if (retv) { 2967 /* 2968 * Error, leave B_DELWRI intact 2969 */ 2970 vfs_unbusy_pages(bp); 2971 brelse(bp); 2972 } else { 2973 /* 2974 * Success, remove B_DELWRI ( bundirty() ). 2975 * 2976 * b_dirtyoff/b_dirtyend seem to be NFS 2977 * specific. We should probably move that 2978 * into bundirty(). XXX 2979 */ 2980 bufobj_wref(bo); 2981 bp->b_flags |= B_ASYNC; 2982 bundirty(bp); 2983 bp->b_flags &= ~B_DONE; 2984 bp->b_ioflags &= ~BIO_ERROR; 2985 bp->b_dirtyoff = bp->b_dirtyend = 0; 2986 bufdone(bp); 2987 } 2988 } 2989 } 2990 2991 /* 2992 * Start/do any write(s) that are required. 2993 */ 2994 loop: 2995 BO_LOCK(bo); 2996 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 2997 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) { 2998 if (waitfor != MNT_WAIT || passone) 2999 continue; 3000 3001 error = BUF_TIMELOCK(bp, 3002 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 3003 BO_LOCKPTR(bo), "nfsfsync", slpflag, slptimeo); 3004 if (error == 0) { 3005 BUF_UNLOCK(bp); 3006 goto loop; 3007 } 3008 if (error == ENOLCK) { 3009 error = 0; 3010 goto loop; 3011 } 3012 if (called_from_renewthread != 0) { 3013 /* 3014 * Return EIO so the flush will be retried 3015 * later. 3016 */ 3017 error = EIO; 3018 goto done; 3019 } 3020 if (newnfs_sigintr(nmp, td)) { 3021 error = EINTR; 3022 goto done; 3023 } 3024 if (slpflag == PCATCH) { 3025 slpflag = 0; 3026 slptimeo = 2 * hz; 3027 } 3028 goto loop; 3029 } 3030 if ((bp->b_flags & B_DELWRI) == 0) 3031 panic("nfs_fsync: not dirty"); 3032 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) { 3033 BUF_UNLOCK(bp); 3034 continue; 3035 } 3036 BO_UNLOCK(bo); 3037 bremfree(bp); 3038 bp->b_flags |= B_ASYNC; 3039 bwrite(bp); 3040 if (newnfs_sigintr(nmp, td)) { 3041 error = EINTR; 3042 goto done; 3043 } 3044 goto loop; 3045 } 3046 if (passone) { 3047 passone = 0; 3048 BO_UNLOCK(bo); 3049 goto again; 3050 } 3051 if (waitfor == MNT_WAIT) { 3052 while (bo->bo_numoutput) { 3053 error = bufobj_wwait(bo, slpflag, slptimeo); 3054 if (error) { 3055 BO_UNLOCK(bo); 3056 if (called_from_renewthread != 0) { 3057 /* 3058 * Return EIO so that the flush will be 3059 * retried later. 3060 */ 3061 error = EIO; 3062 goto done; 3063 } 3064 error = newnfs_sigintr(nmp, td); 3065 if (error) 3066 goto done; 3067 if (slpflag == PCATCH) { 3068 slpflag = 0; 3069 slptimeo = 2 * hz; 3070 } 3071 BO_LOCK(bo); 3072 } 3073 } 3074 if (bo->bo_dirty.bv_cnt != 0 && commit) { 3075 BO_UNLOCK(bo); 3076 goto loop; 3077 } 3078 /* 3079 * Wait for all the async IO requests to drain 3080 */ 3081 BO_UNLOCK(bo); 3082 NFSLOCKNODE(np); 3083 while (np->n_directio_asyncwr > 0) { 3084 np->n_flag |= NFSYNCWAIT; 3085 error = newnfs_msleep(td, &np->n_directio_asyncwr, 3086 &np->n_mtx, slpflag | (PRIBIO + 1), 3087 "nfsfsync", 0); 3088 if (error) { 3089 if (newnfs_sigintr(nmp, td)) { 3090 NFSUNLOCKNODE(np); 3091 error = EINTR; 3092 goto done; 3093 } 3094 } 3095 } 3096 NFSUNLOCKNODE(np); 3097 } else 3098 BO_UNLOCK(bo); 3099 if (NFSHASPNFS(nmp)) { 3100 nfscl_layoutcommit(vp, td); 3101 /* 3102 * Invalidate the attribute cache, since writes to a DS 3103 * won't update the size attribute. 3104 */ 3105 NFSLOCKNODE(np); 3106 np->n_attrstamp = 0; 3107 } else 3108 NFSLOCKNODE(np); 3109 if (np->n_flag & NWRITEERR) { 3110 error = np->n_error; 3111 np->n_flag &= ~NWRITEERR; 3112 } 3113 if (commit && bo->bo_dirty.bv_cnt == 0 && 3114 bo->bo_numoutput == 0 && np->n_directio_asyncwr == 0) 3115 np->n_flag &= ~NMODIFIED; 3116 NFSUNLOCKNODE(np); 3117 done: 3118 if (bvec != NULL && bvec != bvec_on_stack) 3119 free(bvec, M_TEMP); 3120 if (error == 0 && commit != 0 && waitfor == MNT_WAIT && 3121 (bo->bo_dirty.bv_cnt != 0 || bo->bo_numoutput != 0 || 3122 np->n_directio_asyncwr != 0)) { 3123 if (trycnt++ < 5) { 3124 /* try, try again... */ 3125 passone = 1; 3126 wcred = NULL; 3127 bvec = NULL; 3128 bvecsize = 0; 3129 goto again; 3130 } 3131 vn_printf(vp, "ncl_flush failed"); 3132 error = called_from_renewthread != 0 ? EIO : EBUSY; 3133 } 3134 return (error); 3135 } 3136 3137 /* 3138 * NFS advisory byte-level locks. 3139 */ 3140 static int 3141 nfs_advlock(struct vop_advlock_args *ap) 3142 { 3143 struct vnode *vp = ap->a_vp; 3144 struct ucred *cred; 3145 struct nfsnode *np = VTONFS(ap->a_vp); 3146 struct proc *p = (struct proc *)ap->a_id; 3147 struct thread *td = curthread; /* XXX */ 3148 struct vattr va; 3149 int ret, error; 3150 u_quad_t size; 3151 3152 error = NFSVOPLOCK(vp, LK_SHARED); 3153 if (error != 0) 3154 return (EBADF); 3155 if (NFS_ISV4(vp) && (ap->a_flags & (F_POSIX | F_FLOCK)) != 0) { 3156 if (vp->v_type != VREG) { 3157 error = EINVAL; 3158 goto out; 3159 } 3160 if ((ap->a_flags & F_POSIX) != 0) 3161 cred = p->p_ucred; 3162 else 3163 cred = td->td_ucred; 3164 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY); 3165 if (VN_IS_DOOMED(vp)) { 3166 error = EBADF; 3167 goto out; 3168 } 3169 3170 /* 3171 * If this is unlocking a write locked region, flush and 3172 * commit them before unlocking. This is required by 3173 * RFC3530 Sec. 9.3.2. 3174 */ 3175 if (ap->a_op == F_UNLCK && 3176 nfscl_checkwritelocked(vp, ap->a_fl, cred, td, ap->a_id, 3177 ap->a_flags)) 3178 (void) ncl_flush(vp, MNT_WAIT, td, 1, 0); 3179 3180 /* 3181 * Loop around doing the lock op, while a blocking lock 3182 * must wait for the lock op to succeed. 3183 */ 3184 do { 3185 ret = nfsrpc_advlock(vp, np->n_size, ap->a_op, 3186 ap->a_fl, 0, cred, td, ap->a_id, ap->a_flags); 3187 if (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) && 3188 ap->a_op == F_SETLK) { 3189 NFSVOPUNLOCK(vp); 3190 error = nfs_catnap(PZERO | PCATCH, ret, 3191 "ncladvl"); 3192 if (error) 3193 return (EINTR); 3194 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY); 3195 if (VN_IS_DOOMED(vp)) { 3196 error = EBADF; 3197 goto out; 3198 } 3199 } 3200 } while (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) && 3201 ap->a_op == F_SETLK); 3202 if (ret == NFSERR_DENIED) { 3203 error = EAGAIN; 3204 goto out; 3205 } else if (ret == EINVAL || ret == EBADF || ret == EINTR) { 3206 error = ret; 3207 goto out; 3208 } else if (ret != 0) { 3209 error = EACCES; 3210 goto out; 3211 } 3212 3213 /* 3214 * Now, if we just got a lock, invalidate data in the buffer 3215 * cache, as required, so that the coherency conforms with 3216 * RFC3530 Sec. 9.3.2. 3217 */ 3218 if (ap->a_op == F_SETLK) { 3219 if ((np->n_flag & NMODIFIED) == 0) { 3220 np->n_attrstamp = 0; 3221 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp); 3222 ret = VOP_GETATTR(vp, &va, cred); 3223 } 3224 if ((np->n_flag & NMODIFIED) || ret || 3225 np->n_change != va.va_filerev) { 3226 (void) ncl_vinvalbuf(vp, V_SAVE, td, 1); 3227 np->n_attrstamp = 0; 3228 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp); 3229 ret = VOP_GETATTR(vp, &va, cred); 3230 if (!ret) { 3231 np->n_mtime = va.va_mtime; 3232 np->n_change = va.va_filerev; 3233 } 3234 } 3235 /* Mark that a file lock has been acquired. */ 3236 NFSLOCKNODE(np); 3237 np->n_flag |= NHASBEENLOCKED; 3238 NFSUNLOCKNODE(np); 3239 } 3240 } else if (!NFS_ISV4(vp)) { 3241 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) { 3242 size = VTONFS(vp)->n_size; 3243 NFSVOPUNLOCK(vp); 3244 error = lf_advlock(ap, &(vp->v_lockf), size); 3245 } else { 3246 if (nfs_advlock_p != NULL) 3247 error = nfs_advlock_p(ap); 3248 else { 3249 NFSVOPUNLOCK(vp); 3250 error = ENOLCK; 3251 } 3252 } 3253 if (error == 0 && ap->a_op == F_SETLK) { 3254 error = NFSVOPLOCK(vp, LK_SHARED); 3255 if (error == 0) { 3256 /* Mark that a file lock has been acquired. */ 3257 NFSLOCKNODE(np); 3258 np->n_flag |= NHASBEENLOCKED; 3259 NFSUNLOCKNODE(np); 3260 NFSVOPUNLOCK(vp); 3261 } 3262 } 3263 return (error); 3264 } else 3265 error = EOPNOTSUPP; 3266 out: 3267 NFSVOPUNLOCK(vp); 3268 return (error); 3269 } 3270 3271 /* 3272 * NFS advisory byte-level locks. 3273 */ 3274 static int 3275 nfs_advlockasync(struct vop_advlockasync_args *ap) 3276 { 3277 struct vnode *vp = ap->a_vp; 3278 u_quad_t size; 3279 int error; 3280 3281 if (NFS_ISV4(vp)) 3282 return (EOPNOTSUPP); 3283 error = NFSVOPLOCK(vp, LK_SHARED); 3284 if (error) 3285 return (error); 3286 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) { 3287 size = VTONFS(vp)->n_size; 3288 NFSVOPUNLOCK(vp); 3289 error = lf_advlockasync(ap, &(vp->v_lockf), size); 3290 } else { 3291 NFSVOPUNLOCK(vp); 3292 error = EOPNOTSUPP; 3293 } 3294 return (error); 3295 } 3296 3297 /* 3298 * Print out the contents of an nfsnode. 3299 */ 3300 static int 3301 nfs_print(struct vop_print_args *ap) 3302 { 3303 struct vnode *vp = ap->a_vp; 3304 struct nfsnode *np = VTONFS(vp); 3305 3306 printf("\tfileid %jd fsid 0x%jx", (uintmax_t)np->n_vattr.na_fileid, 3307 (uintmax_t)np->n_vattr.na_fsid); 3308 if (vp->v_type == VFIFO) 3309 fifo_printinfo(vp); 3310 printf("\n"); 3311 return (0); 3312 } 3313 3314 /* 3315 * This is the "real" nfs::bwrite(struct buf*). 3316 * We set B_CACHE if this is a VMIO buffer. 3317 */ 3318 int 3319 ncl_writebp(struct buf *bp, int force __unused, struct thread *td) 3320 { 3321 int oldflags, rtval; 3322 3323 if (bp->b_flags & B_INVAL) { 3324 brelse(bp); 3325 return (0); 3326 } 3327 3328 oldflags = bp->b_flags; 3329 bp->b_flags |= B_CACHE; 3330 3331 /* 3332 * Undirty the bp. We will redirty it later if the I/O fails. 3333 */ 3334 bundirty(bp); 3335 bp->b_flags &= ~B_DONE; 3336 bp->b_ioflags &= ~BIO_ERROR; 3337 bp->b_iocmd = BIO_WRITE; 3338 3339 bufobj_wref(bp->b_bufobj); 3340 curthread->td_ru.ru_oublock++; 3341 3342 /* 3343 * Note: to avoid loopback deadlocks, we do not 3344 * assign b_runningbufspace. 3345 */ 3346 vfs_busy_pages(bp, 1); 3347 3348 BUF_KERNPROC(bp); 3349 bp->b_iooffset = dbtob(bp->b_blkno); 3350 bstrategy(bp); 3351 3352 if ((oldflags & B_ASYNC) != 0) 3353 return (0); 3354 3355 rtval = bufwait(bp); 3356 if (oldflags & B_DELWRI) 3357 reassignbuf(bp); 3358 brelse(bp); 3359 return (rtval); 3360 } 3361 3362 /* 3363 * nfs special file access vnode op. 3364 * Essentially just get vattr and then imitate iaccess() since the device is 3365 * local to the client. 3366 */ 3367 static int 3368 nfsspec_access(struct vop_access_args *ap) 3369 { 3370 struct vattr *vap; 3371 struct ucred *cred = ap->a_cred; 3372 struct vnode *vp = ap->a_vp; 3373 accmode_t accmode = ap->a_accmode; 3374 struct vattr vattr; 3375 int error; 3376 3377 /* 3378 * Disallow write attempts on filesystems mounted read-only; 3379 * unless the file is a socket, fifo, or a block or character 3380 * device resident on the filesystem. 3381 */ 3382 if ((accmode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) { 3383 switch (vp->v_type) { 3384 case VREG: 3385 case VDIR: 3386 case VLNK: 3387 return (EROFS); 3388 default: 3389 break; 3390 } 3391 } 3392 vap = &vattr; 3393 error = VOP_GETATTR(vp, vap, cred); 3394 if (error) 3395 goto out; 3396 error = vaccess(vp->v_type, vap->va_mode, vap->va_uid, vap->va_gid, 3397 accmode, cred, NULL); 3398 out: 3399 return error; 3400 } 3401 3402 /* 3403 * Read wrapper for fifos. 3404 */ 3405 static int 3406 nfsfifo_read(struct vop_read_args *ap) 3407 { 3408 struct nfsnode *np = VTONFS(ap->a_vp); 3409 int error; 3410 3411 /* 3412 * Set access flag. 3413 */ 3414 NFSLOCKNODE(np); 3415 np->n_flag |= NACC; 3416 vfs_timestamp(&np->n_atim); 3417 NFSUNLOCKNODE(np); 3418 error = fifo_specops.vop_read(ap); 3419 return error; 3420 } 3421 3422 /* 3423 * Write wrapper for fifos. 3424 */ 3425 static int 3426 nfsfifo_write(struct vop_write_args *ap) 3427 { 3428 struct nfsnode *np = VTONFS(ap->a_vp); 3429 3430 /* 3431 * Set update flag. 3432 */ 3433 NFSLOCKNODE(np); 3434 np->n_flag |= NUPD; 3435 vfs_timestamp(&np->n_mtim); 3436 NFSUNLOCKNODE(np); 3437 return(fifo_specops.vop_write(ap)); 3438 } 3439 3440 /* 3441 * Close wrapper for fifos. 3442 * 3443 * Update the times on the nfsnode then do fifo close. 3444 */ 3445 static int 3446 nfsfifo_close(struct vop_close_args *ap) 3447 { 3448 struct vnode *vp = ap->a_vp; 3449 struct nfsnode *np = VTONFS(vp); 3450 struct vattr vattr; 3451 struct timespec ts; 3452 3453 NFSLOCKNODE(np); 3454 if (np->n_flag & (NACC | NUPD)) { 3455 vfs_timestamp(&ts); 3456 if (np->n_flag & NACC) 3457 np->n_atim = ts; 3458 if (np->n_flag & NUPD) 3459 np->n_mtim = ts; 3460 np->n_flag |= NCHG; 3461 if (vrefcnt(vp) == 1 && 3462 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 3463 VATTR_NULL(&vattr); 3464 if (np->n_flag & NACC) 3465 vattr.va_atime = np->n_atim; 3466 if (np->n_flag & NUPD) 3467 vattr.va_mtime = np->n_mtim; 3468 NFSUNLOCKNODE(np); 3469 (void)VOP_SETATTR(vp, &vattr, ap->a_cred); 3470 goto out; 3471 } 3472 } 3473 NFSUNLOCKNODE(np); 3474 out: 3475 return (fifo_specops.vop_close(ap)); 3476 } 3477 3478 /* 3479 * Just call ncl_writebp() with the force argument set to 1. 3480 * 3481 * NOTE: B_DONE may or may not be set in a_bp on call. 3482 */ 3483 static int 3484 nfs_bwrite(struct buf *bp) 3485 { 3486 3487 return (ncl_writebp(bp, 1, curthread)); 3488 } 3489 3490 struct buf_ops buf_ops_newnfs = { 3491 .bop_name = "buf_ops_nfs", 3492 .bop_write = nfs_bwrite, 3493 .bop_strategy = bufstrategy, 3494 .bop_sync = bufsync, 3495 .bop_bdflush = bufbdflush, 3496 }; 3497 3498 static int 3499 nfs_getacl(struct vop_getacl_args *ap) 3500 { 3501 int error; 3502 3503 if (ap->a_type != ACL_TYPE_NFS4) 3504 return (EOPNOTSUPP); 3505 error = nfsrpc_getacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp, 3506 NULL); 3507 if (error > NFSERR_STALE) { 3508 (void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0); 3509 error = EPERM; 3510 } 3511 return (error); 3512 } 3513 3514 static int 3515 nfs_setacl(struct vop_setacl_args *ap) 3516 { 3517 int error; 3518 3519 if (ap->a_type != ACL_TYPE_NFS4) 3520 return (EOPNOTSUPP); 3521 error = nfsrpc_setacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp, 3522 NULL); 3523 if (error > NFSERR_STALE) { 3524 (void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0); 3525 error = EPERM; 3526 } 3527 return (error); 3528 } 3529 3530 /* 3531 * VOP_ADVISE for NFS. 3532 * Just return 0 for any errors, since it is just a hint. 3533 */ 3534 static int 3535 nfs_advise(struct vop_advise_args *ap) 3536 { 3537 struct thread *td = curthread; 3538 struct nfsmount *nmp; 3539 uint64_t len; 3540 int error; 3541 3542 /* 3543 * First do vop_stdadvise() to handle the buffer cache. 3544 */ 3545 error = vop_stdadvise(ap); 3546 if (error != 0) 3547 return (error); 3548 if (ap->a_start < 0 || ap->a_end < 0) 3549 return (0); 3550 if (ap->a_end == OFF_MAX) 3551 len = 0; 3552 else if (ap->a_end < ap->a_start) 3553 return (0); 3554 else 3555 len = ap->a_end - ap->a_start + 1; 3556 nmp = VFSTONFS(ap->a_vp->v_mount); 3557 mtx_lock(&nmp->nm_mtx); 3558 if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION || 3559 (NFSHASPNFS(nmp) && (nmp->nm_privflag & NFSMNTP_IOADVISETHRUMDS) == 3560 0) || (nmp->nm_privflag & NFSMNTP_NOADVISE) != 0) { 3561 mtx_unlock(&nmp->nm_mtx); 3562 return (0); 3563 } 3564 mtx_unlock(&nmp->nm_mtx); 3565 error = nfsrpc_advise(ap->a_vp, ap->a_start, len, ap->a_advice, 3566 td->td_ucred, td); 3567 if (error == NFSERR_NOTSUPP) { 3568 mtx_lock(&nmp->nm_mtx); 3569 nmp->nm_privflag |= NFSMNTP_NOADVISE; 3570 mtx_unlock(&nmp->nm_mtx); 3571 } 3572 return (0); 3573 } 3574 3575 /* 3576 * nfs allocate call 3577 */ 3578 static int 3579 nfs_allocate(struct vop_allocate_args *ap) 3580 { 3581 struct vnode *vp = ap->a_vp; 3582 struct thread *td = curthread; 3583 struct nfsvattr nfsva; 3584 struct nfsmount *nmp; 3585 int attrflag, error, ret; 3586 3587 attrflag = 0; 3588 nmp = VFSTONFS(vp->v_mount); 3589 mtx_lock(&nmp->nm_mtx); 3590 if (NFSHASNFSV4(nmp) && nmp->nm_minorvers >= NFSV42_MINORVERSION && 3591 (nmp->nm_privflag & NFSMNTP_NOALLOCATE) == 0) { 3592 mtx_unlock(&nmp->nm_mtx); 3593 /* 3594 * Flush first to ensure that the allocate adds to the 3595 * file's allocation on the server. 3596 */ 3597 error = ncl_flush(vp, MNT_WAIT, td, 1, 0); 3598 if (error == 0) 3599 error = nfsrpc_allocate(vp, *ap->a_offset, *ap->a_len, 3600 &nfsva, &attrflag, td->td_ucred, td, NULL); 3601 if (error == 0) { 3602 *ap->a_offset += *ap->a_len; 3603 *ap->a_len = 0; 3604 } else if (error == NFSERR_NOTSUPP) { 3605 mtx_lock(&nmp->nm_mtx); 3606 nmp->nm_privflag |= NFSMNTP_NOALLOCATE; 3607 mtx_unlock(&nmp->nm_mtx); 3608 } 3609 } else { 3610 mtx_unlock(&nmp->nm_mtx); 3611 error = EIO; 3612 } 3613 /* 3614 * If the NFS server cannot perform the Allocate operation, just call 3615 * vop_stdallocate() to perform it. 3616 */ 3617 if (error != 0) 3618 error = vop_stdallocate(ap); 3619 if (attrflag != 0) { 3620 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1); 3621 if (error == 0 && ret != 0) 3622 error = ret; 3623 } 3624 if (error != 0) 3625 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0); 3626 return (error); 3627 } 3628 3629 /* 3630 * nfs copy_file_range call 3631 */ 3632 static int 3633 nfs_copy_file_range(struct vop_copy_file_range_args *ap) 3634 { 3635 struct vnode *invp = ap->a_invp; 3636 struct vnode *outvp = ap->a_outvp; 3637 struct mount *mp; 3638 struct nfsvattr innfsva, outnfsva; 3639 struct vattr *vap; 3640 struct uio io; 3641 struct nfsmount *nmp; 3642 size_t len, len2, copiedlen; 3643 int error, inattrflag, outattrflag, ret, ret2; 3644 off_t inoff, outoff; 3645 bool consecutive, must_commit, tryoutcred; 3646 3647 ret = ret2 = 0; 3648 nmp = VFSTONFS(invp->v_mount); 3649 mtx_lock(&nmp->nm_mtx); 3650 /* NFSv4.2 Copy is not permitted for infile == outfile. */ 3651 if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION || 3652 (nmp->nm_privflag & NFSMNTP_NOCOPY) != 0 || invp == outvp) { 3653 mtx_unlock(&nmp->nm_mtx); 3654 error = vn_generic_copy_file_range(ap->a_invp, ap->a_inoffp, 3655 ap->a_outvp, ap->a_outoffp, ap->a_lenp, ap->a_flags, 3656 ap->a_incred, ap->a_outcred, ap->a_fsizetd); 3657 return (error); 3658 } 3659 mtx_unlock(&nmp->nm_mtx); 3660 3661 /* Lock both vnodes, avoiding risk of deadlock. */ 3662 do { 3663 mp = NULL; 3664 error = vn_start_write(outvp, &mp, V_WAIT); 3665 if (error == 0) { 3666 error = vn_lock(outvp, LK_EXCLUSIVE); 3667 if (error == 0) { 3668 error = vn_lock(invp, LK_SHARED | LK_NOWAIT); 3669 if (error == 0) 3670 break; 3671 VOP_UNLOCK(outvp); 3672 if (mp != NULL) 3673 vn_finished_write(mp); 3674 mp = NULL; 3675 error = vn_lock(invp, LK_SHARED); 3676 if (error == 0) 3677 VOP_UNLOCK(invp); 3678 } 3679 } 3680 if (mp != NULL) 3681 vn_finished_write(mp); 3682 } while (error == 0); 3683 if (error != 0) 3684 return (error); 3685 3686 /* 3687 * Do the vn_rlimit_fsize() check. Should this be above the VOP layer? 3688 */ 3689 io.uio_offset = *ap->a_outoffp; 3690 io.uio_resid = *ap->a_lenp; 3691 error = vn_rlimit_fsize(outvp, &io, ap->a_fsizetd); 3692 3693 /* 3694 * Flush the input file so that the data is up to date before 3695 * the copy. Flush writes for the output file so that they 3696 * do not overwrite the data copied to the output file by the Copy. 3697 * Set the commit argument for both flushes so that the data is on 3698 * stable storage before the Copy RPC. This is done in case the 3699 * server reboots during the Copy and needs to be redone. 3700 */ 3701 if (error == 0) 3702 error = ncl_flush(invp, MNT_WAIT, curthread, 1, 0); 3703 if (error == 0) 3704 error = ncl_flush(outvp, MNT_WAIT, curthread, 1, 0); 3705 3706 /* Do the actual NFSv4.2 RPC. */ 3707 len = *ap->a_lenp; 3708 mtx_lock(&nmp->nm_mtx); 3709 if ((nmp->nm_privflag & NFSMNTP_NOCONSECUTIVE) == 0) 3710 consecutive = true; 3711 else 3712 consecutive = false; 3713 mtx_unlock(&nmp->nm_mtx); 3714 inoff = *ap->a_inoffp; 3715 outoff = *ap->a_outoffp; 3716 tryoutcred = true; 3717 must_commit = false; 3718 if (error == 0) { 3719 vap = &VTONFS(invp)->n_vattr.na_vattr; 3720 error = VOP_GETATTR(invp, vap, ap->a_incred); 3721 if (error == 0) { 3722 /* 3723 * Clip "len" at va_size so that RFC compliant servers 3724 * will not reply NFSERR_INVAL. 3725 * Setting "len == 0" for the RPC would be preferred, 3726 * but some Linux servers do not support that. 3727 */ 3728 if (inoff >= vap->va_size) 3729 *ap->a_lenp = len = 0; 3730 else if (inoff + len > vap->va_size) 3731 *ap->a_lenp = len = vap->va_size - inoff; 3732 } else 3733 error = 0; 3734 } 3735 copiedlen = 0; 3736 while (len > 0 && error == 0) { 3737 inattrflag = outattrflag = 0; 3738 len2 = len; 3739 if (tryoutcred) 3740 error = nfsrpc_copy_file_range(invp, ap->a_inoffp, 3741 outvp, ap->a_outoffp, &len2, ap->a_flags, 3742 &inattrflag, &innfsva, &outattrflag, &outnfsva, 3743 ap->a_outcred, consecutive, &must_commit); 3744 else 3745 error = nfsrpc_copy_file_range(invp, ap->a_inoffp, 3746 outvp, ap->a_outoffp, &len2, ap->a_flags, 3747 &inattrflag, &innfsva, &outattrflag, &outnfsva, 3748 ap->a_incred, consecutive, &must_commit); 3749 if (inattrflag != 0) 3750 ret = nfscl_loadattrcache(&invp, &innfsva, NULL, NULL, 3751 0, 1); 3752 if (outattrflag != 0) 3753 ret2 = nfscl_loadattrcache(&outvp, &outnfsva, NULL, 3754 NULL, 1, 1); 3755 if (error == 0) { 3756 if (consecutive == false) { 3757 if (len2 == len) { 3758 mtx_lock(&nmp->nm_mtx); 3759 nmp->nm_privflag |= 3760 NFSMNTP_NOCONSECUTIVE; 3761 mtx_unlock(&nmp->nm_mtx); 3762 } else 3763 error = NFSERR_OFFLOADNOREQS; 3764 } 3765 /* 3766 * If the Copy returns a length == 0, it hit the 3767 * EOF on the input file. 3768 */ 3769 if (len2 == 0) { 3770 *ap->a_lenp = copiedlen; 3771 len = 0; 3772 } else { 3773 len -= len2; 3774 copiedlen += len2; 3775 } 3776 if (len == 0 && must_commit && error == 0) 3777 error = ncl_commit(outvp, outoff, *ap->a_lenp, 3778 ap->a_outcred, curthread); 3779 if (error == 0 && ret != 0) 3780 error = ret; 3781 if (error == 0 && ret2 != 0) 3782 error = ret2; 3783 } else if (error == NFSERR_OFFLOADNOREQS && consecutive) { 3784 /* 3785 * Try consecutive == false, which is ok only if all 3786 * bytes are copied. 3787 */ 3788 consecutive = false; 3789 error = 0; 3790 } else if (error == NFSERR_ACCES && tryoutcred) { 3791 /* Try again with incred. */ 3792 tryoutcred = false; 3793 error = 0; 3794 } 3795 if (error == NFSERR_STALEWRITEVERF) { 3796 /* 3797 * Server rebooted, so do it all again. 3798 */ 3799 *ap->a_inoffp = inoff; 3800 *ap->a_outoffp = outoff; 3801 len = *ap->a_lenp; 3802 must_commit = false; 3803 error = 0; 3804 } 3805 } 3806 VOP_UNLOCK(invp); 3807 VOP_UNLOCK(outvp); 3808 if (mp != NULL) 3809 vn_finished_write(mp); 3810 if (error == NFSERR_NOTSUPP || error == NFSERR_OFFLOADNOREQS || 3811 error == NFSERR_ACCES) { 3812 /* 3813 * Unlike the NFSv4.2 Copy, vn_generic_copy_file_range() can 3814 * use a_incred for the read and a_outcred for the write, so 3815 * try this for NFSERR_ACCES failures for the Copy. 3816 * For NFSERR_NOTSUPP and NFSERR_OFFLOADNOREQS, the Copy can 3817 * never succeed, so disable it. 3818 */ 3819 if (error != NFSERR_ACCES) { 3820 /* Can never do Copy on this mount. */ 3821 mtx_lock(&nmp->nm_mtx); 3822 nmp->nm_privflag |= NFSMNTP_NOCOPY; 3823 mtx_unlock(&nmp->nm_mtx); 3824 } 3825 *ap->a_inoffp = inoff; 3826 *ap->a_outoffp = outoff; 3827 error = vn_generic_copy_file_range(ap->a_invp, ap->a_inoffp, 3828 ap->a_outvp, ap->a_outoffp, ap->a_lenp, ap->a_flags, 3829 ap->a_incred, ap->a_outcred, ap->a_fsizetd); 3830 } else if (error != 0) 3831 *ap->a_lenp = 0; 3832 3833 if (error != 0) 3834 error = nfscl_maperr(curthread, error, (uid_t)0, (gid_t)0); 3835 return (error); 3836 } 3837 3838 /* 3839 * nfs ioctl call 3840 */ 3841 static int 3842 nfs_ioctl(struct vop_ioctl_args *ap) 3843 { 3844 struct vnode *vp = ap->a_vp; 3845 struct nfsvattr nfsva; 3846 struct nfsmount *nmp; 3847 int attrflag, content, error, ret; 3848 bool eof = false; /* shut up compiler. */ 3849 3850 if (vp->v_type != VREG) 3851 return (ENOTTY); 3852 nmp = VFSTONFS(vp->v_mount); 3853 if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION) { 3854 error = vop_stdioctl(ap); 3855 return (error); 3856 } 3857 3858 /* Do the actual NFSv4.2 RPC. */ 3859 switch (ap->a_command) { 3860 case FIOSEEKDATA: 3861 content = NFSV4CONTENT_DATA; 3862 break; 3863 case FIOSEEKHOLE: 3864 content = NFSV4CONTENT_HOLE; 3865 break; 3866 default: 3867 return (ENOTTY); 3868 } 3869 3870 error = vn_lock(vp, LK_SHARED); 3871 if (error != 0) 3872 return (EBADF); 3873 attrflag = 0; 3874 if (*((off_t *)ap->a_data) >= VTONFS(vp)->n_size) 3875 error = ENXIO; 3876 else { 3877 /* 3878 * Flush all writes, so that the server is up to date. 3879 * Although a Commit is not required, the commit argument 3880 * is set so that, for a pNFS File/Flexible File Layout 3881 * server, the LayoutCommit will be done to ensure the file 3882 * size is up to date on the Metadata Server. 3883 */ 3884 error = ncl_flush(vp, MNT_WAIT, ap->a_td, 1, 0); 3885 if (error == 0) 3886 error = nfsrpc_seek(vp, (off_t *)ap->a_data, &eof, 3887 content, ap->a_cred, &nfsva, &attrflag); 3888 /* If at eof for FIOSEEKDATA, return ENXIO. */ 3889 if (eof && error == 0 && content == NFSV4CONTENT_DATA) 3890 error = ENXIO; 3891 } 3892 if (attrflag != 0) { 3893 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1); 3894 if (error == 0 && ret != 0) 3895 error = ret; 3896 } 3897 NFSVOPUNLOCK(vp); 3898 3899 if (error != 0) 3900 error = ENXIO; 3901 return (error); 3902 } 3903 3904 /* 3905 * nfs getextattr call 3906 */ 3907 static int 3908 nfs_getextattr(struct vop_getextattr_args *ap) 3909 { 3910 struct vnode *vp = ap->a_vp; 3911 struct nfsmount *nmp; 3912 struct ucred *cred; 3913 struct thread *td = ap->a_td; 3914 struct nfsvattr nfsva; 3915 ssize_t len; 3916 int attrflag, error, ret; 3917 3918 nmp = VFSTONFS(vp->v_mount); 3919 mtx_lock(&nmp->nm_mtx); 3920 if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION || 3921 (nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 || 3922 ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) { 3923 mtx_unlock(&nmp->nm_mtx); 3924 return (EOPNOTSUPP); 3925 } 3926 mtx_unlock(&nmp->nm_mtx); 3927 3928 cred = ap->a_cred; 3929 if (cred == NULL) 3930 cred = td->td_ucred; 3931 /* Do the actual NFSv4.2 Optional Extended Attribute (RFC-8276) RPC. */ 3932 attrflag = 0; 3933 error = nfsrpc_getextattr(vp, ap->a_name, ap->a_uio, &len, &nfsva, 3934 &attrflag, cred, td); 3935 if (attrflag != 0) { 3936 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1); 3937 if (error == 0 && ret != 0) 3938 error = ret; 3939 } 3940 if (error == 0 && ap->a_size != NULL) 3941 *ap->a_size = len; 3942 3943 switch (error) { 3944 case NFSERR_NOTSUPP: 3945 case NFSERR_OPILLEGAL: 3946 mtx_lock(&nmp->nm_mtx); 3947 nmp->nm_privflag |= NFSMNTP_NOXATTR; 3948 mtx_unlock(&nmp->nm_mtx); 3949 error = EOPNOTSUPP; 3950 break; 3951 case NFSERR_NOXATTR: 3952 case NFSERR_XATTR2BIG: 3953 error = ENOATTR; 3954 break; 3955 default: 3956 error = nfscl_maperr(td, error, 0, 0); 3957 break; 3958 } 3959 return (error); 3960 } 3961 3962 /* 3963 * nfs setextattr call 3964 */ 3965 static int 3966 nfs_setextattr(struct vop_setextattr_args *ap) 3967 { 3968 struct vnode *vp = ap->a_vp; 3969 struct nfsmount *nmp; 3970 struct ucred *cred; 3971 struct thread *td = ap->a_td; 3972 struct nfsvattr nfsva; 3973 int attrflag, error, ret; 3974 3975 nmp = VFSTONFS(vp->v_mount); 3976 mtx_lock(&nmp->nm_mtx); 3977 if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION || 3978 (nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 || 3979 ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) { 3980 mtx_unlock(&nmp->nm_mtx); 3981 return (EOPNOTSUPP); 3982 } 3983 mtx_unlock(&nmp->nm_mtx); 3984 3985 if (ap->a_uio->uio_resid < 0) 3986 return (EINVAL); 3987 cred = ap->a_cred; 3988 if (cred == NULL) 3989 cred = td->td_ucred; 3990 /* Do the actual NFSv4.2 Optional Extended Attribute (RFC-8276) RPC. */ 3991 attrflag = 0; 3992 error = nfsrpc_setextattr(vp, ap->a_name, ap->a_uio, &nfsva, 3993 &attrflag, cred, td); 3994 if (attrflag != 0) { 3995 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1); 3996 if (error == 0 && ret != 0) 3997 error = ret; 3998 } 3999 4000 switch (error) { 4001 case NFSERR_NOTSUPP: 4002 case NFSERR_OPILLEGAL: 4003 mtx_lock(&nmp->nm_mtx); 4004 nmp->nm_privflag |= NFSMNTP_NOXATTR; 4005 mtx_unlock(&nmp->nm_mtx); 4006 error = EOPNOTSUPP; 4007 break; 4008 case NFSERR_NOXATTR: 4009 case NFSERR_XATTR2BIG: 4010 error = ENOATTR; 4011 break; 4012 default: 4013 error = nfscl_maperr(td, error, 0, 0); 4014 break; 4015 } 4016 return (error); 4017 } 4018 4019 /* 4020 * nfs listextattr call 4021 */ 4022 static int 4023 nfs_listextattr(struct vop_listextattr_args *ap) 4024 { 4025 struct vnode *vp = ap->a_vp; 4026 struct nfsmount *nmp; 4027 struct ucred *cred; 4028 struct thread *td = ap->a_td; 4029 struct nfsvattr nfsva; 4030 size_t len, len2; 4031 uint64_t cookie; 4032 int attrflag, error, ret; 4033 bool eof; 4034 4035 nmp = VFSTONFS(vp->v_mount); 4036 mtx_lock(&nmp->nm_mtx); 4037 if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION || 4038 (nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 || 4039 ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) { 4040 mtx_unlock(&nmp->nm_mtx); 4041 return (EOPNOTSUPP); 4042 } 4043 mtx_unlock(&nmp->nm_mtx); 4044 4045 cred = ap->a_cred; 4046 if (cred == NULL) 4047 cred = td->td_ucred; 4048 4049 /* Loop around doing List Extended Attribute RPCs. */ 4050 eof = false; 4051 cookie = 0; 4052 len2 = 0; 4053 error = 0; 4054 while (!eof && error == 0) { 4055 len = nmp->nm_rsize; 4056 attrflag = 0; 4057 error = nfsrpc_listextattr(vp, &cookie, ap->a_uio, &len, &eof, 4058 &nfsva, &attrflag, cred, td); 4059 if (attrflag != 0) { 4060 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 4061 1); 4062 if (error == 0 && ret != 0) 4063 error = ret; 4064 } 4065 if (error == 0) { 4066 len2 += len; 4067 if (len2 > SSIZE_MAX) 4068 error = ENOATTR; 4069 } 4070 } 4071 if (error == 0 && ap->a_size != NULL) 4072 *ap->a_size = len2; 4073 4074 switch (error) { 4075 case NFSERR_NOTSUPP: 4076 case NFSERR_OPILLEGAL: 4077 mtx_lock(&nmp->nm_mtx); 4078 nmp->nm_privflag |= NFSMNTP_NOXATTR; 4079 mtx_unlock(&nmp->nm_mtx); 4080 error = EOPNOTSUPP; 4081 break; 4082 case NFSERR_NOXATTR: 4083 case NFSERR_XATTR2BIG: 4084 error = ENOATTR; 4085 break; 4086 default: 4087 error = nfscl_maperr(td, error, 0, 0); 4088 break; 4089 } 4090 return (error); 4091 } 4092 4093 /* 4094 * nfs setextattr call 4095 */ 4096 static int 4097 nfs_deleteextattr(struct vop_deleteextattr_args *ap) 4098 { 4099 struct vnode *vp = ap->a_vp; 4100 struct nfsmount *nmp; 4101 struct nfsvattr nfsva; 4102 int attrflag, error, ret; 4103 4104 nmp = VFSTONFS(vp->v_mount); 4105 mtx_lock(&nmp->nm_mtx); 4106 if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION || 4107 (nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 || 4108 ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) { 4109 mtx_unlock(&nmp->nm_mtx); 4110 return (EOPNOTSUPP); 4111 } 4112 mtx_unlock(&nmp->nm_mtx); 4113 4114 /* Do the actual NFSv4.2 Optional Extended Attribute (RFC-8276) RPC. */ 4115 attrflag = 0; 4116 error = nfsrpc_rmextattr(vp, ap->a_name, &nfsva, &attrflag, ap->a_cred, 4117 ap->a_td); 4118 if (attrflag != 0) { 4119 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1); 4120 if (error == 0 && ret != 0) 4121 error = ret; 4122 } 4123 4124 switch (error) { 4125 case NFSERR_NOTSUPP: 4126 case NFSERR_OPILLEGAL: 4127 mtx_lock(&nmp->nm_mtx); 4128 nmp->nm_privflag |= NFSMNTP_NOXATTR; 4129 mtx_unlock(&nmp->nm_mtx); 4130 error = EOPNOTSUPP; 4131 break; 4132 case NFSERR_NOXATTR: 4133 case NFSERR_XATTR2BIG: 4134 error = ENOATTR; 4135 break; 4136 default: 4137 error = nfscl_maperr(ap->a_td, error, 0, 0); 4138 break; 4139 } 4140 return (error); 4141 } 4142 4143 /* 4144 * Return POSIX pathconf information applicable to nfs filesystems. 4145 */ 4146 static int 4147 nfs_pathconf(struct vop_pathconf_args *ap) 4148 { 4149 struct nfsv3_pathconf pc; 4150 struct nfsvattr nfsva; 4151 struct vnode *vp = ap->a_vp; 4152 struct nfsmount *nmp; 4153 struct thread *td = curthread; 4154 off_t off; 4155 bool eof; 4156 int attrflag, error; 4157 4158 if ((NFS_ISV34(vp) && (ap->a_name == _PC_LINK_MAX || 4159 ap->a_name == _PC_NAME_MAX || ap->a_name == _PC_CHOWN_RESTRICTED || 4160 ap->a_name == _PC_NO_TRUNC)) || 4161 (NFS_ISV4(vp) && ap->a_name == _PC_ACL_NFS4)) { 4162 /* 4163 * Since only the above 4 a_names are returned by the NFSv3 4164 * Pathconf RPC, there is no point in doing it for others. 4165 * For NFSv4, the Pathconf RPC (actually a Getattr Op.) can 4166 * be used for _PC_NFS4_ACL as well. 4167 */ 4168 error = nfsrpc_pathconf(vp, &pc, td->td_ucred, td, &nfsva, 4169 &attrflag, NULL); 4170 if (attrflag != 0) 4171 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 4172 1); 4173 if (error != 0) 4174 return (error); 4175 } else { 4176 /* 4177 * For NFSv2 (or NFSv3 when not one of the above 4 a_names), 4178 * just fake them. 4179 */ 4180 pc.pc_linkmax = NFS_LINK_MAX; 4181 pc.pc_namemax = NFS_MAXNAMLEN; 4182 pc.pc_notrunc = 1; 4183 pc.pc_chownrestricted = 1; 4184 pc.pc_caseinsensitive = 0; 4185 pc.pc_casepreserving = 1; 4186 error = 0; 4187 } 4188 switch (ap->a_name) { 4189 case _PC_LINK_MAX: 4190 #ifdef _LP64 4191 *ap->a_retval = pc.pc_linkmax; 4192 #else 4193 *ap->a_retval = MIN(LONG_MAX, pc.pc_linkmax); 4194 #endif 4195 break; 4196 case _PC_NAME_MAX: 4197 *ap->a_retval = pc.pc_namemax; 4198 break; 4199 case _PC_PIPE_BUF: 4200 if (ap->a_vp->v_type == VDIR || ap->a_vp->v_type == VFIFO) 4201 *ap->a_retval = PIPE_BUF; 4202 else 4203 error = EINVAL; 4204 break; 4205 case _PC_CHOWN_RESTRICTED: 4206 *ap->a_retval = pc.pc_chownrestricted; 4207 break; 4208 case _PC_NO_TRUNC: 4209 *ap->a_retval = pc.pc_notrunc; 4210 break; 4211 case _PC_ACL_NFS4: 4212 if (NFS_ISV4(vp) && nfsrv_useacl != 0 && attrflag != 0 && 4213 NFSISSET_ATTRBIT(&nfsva.na_suppattr, NFSATTRBIT_ACL)) 4214 *ap->a_retval = 1; 4215 else 4216 *ap->a_retval = 0; 4217 break; 4218 case _PC_ACL_PATH_MAX: 4219 if (NFS_ISV4(vp)) 4220 *ap->a_retval = ACL_MAX_ENTRIES; 4221 else 4222 *ap->a_retval = 3; 4223 break; 4224 case _PC_PRIO_IO: 4225 *ap->a_retval = 0; 4226 break; 4227 case _PC_SYNC_IO: 4228 *ap->a_retval = 0; 4229 break; 4230 case _PC_ALLOC_SIZE_MIN: 4231 *ap->a_retval = vp->v_mount->mnt_stat.f_bsize; 4232 break; 4233 case _PC_FILESIZEBITS: 4234 if (NFS_ISV34(vp)) 4235 *ap->a_retval = 64; 4236 else 4237 *ap->a_retval = 32; 4238 break; 4239 case _PC_REC_INCR_XFER_SIZE: 4240 *ap->a_retval = vp->v_mount->mnt_stat.f_iosize; 4241 break; 4242 case _PC_REC_MAX_XFER_SIZE: 4243 *ap->a_retval = -1; /* means ``unlimited'' */ 4244 break; 4245 case _PC_REC_MIN_XFER_SIZE: 4246 *ap->a_retval = vp->v_mount->mnt_stat.f_iosize; 4247 break; 4248 case _PC_REC_XFER_ALIGN: 4249 *ap->a_retval = PAGE_SIZE; 4250 break; 4251 case _PC_SYMLINK_MAX: 4252 *ap->a_retval = NFS_MAXPATHLEN; 4253 break; 4254 case _PC_MIN_HOLE_SIZE: 4255 /* Only some NFSv4.2 servers support Seek for Holes. */ 4256 *ap->a_retval = 0; 4257 nmp = VFSTONFS(vp->v_mount); 4258 if (NFS_ISV4(vp) && nmp->nm_minorvers == NFSV42_MINORVERSION) { 4259 /* 4260 * NFSv4.2 doesn't have an attribute for hole size, 4261 * so all we can do is see if the Seek operation is 4262 * supported and then use f_iosize as a "best guess". 4263 */ 4264 mtx_lock(&nmp->nm_mtx); 4265 if ((nmp->nm_privflag & NFSMNTP_SEEKTESTED) == 0) { 4266 mtx_unlock(&nmp->nm_mtx); 4267 off = 0; 4268 attrflag = 0; 4269 error = nfsrpc_seek(vp, &off, &eof, 4270 NFSV4CONTENT_HOLE, td->td_ucred, &nfsva, 4271 &attrflag); 4272 if (attrflag != 0) 4273 nfscl_loadattrcache(&vp, &nfsva, 4274 NULL, NULL, 0, 1); 4275 mtx_lock(&nmp->nm_mtx); 4276 if (error == NFSERR_NOTSUPP) 4277 nmp->nm_privflag |= NFSMNTP_SEEKTESTED; 4278 else 4279 nmp->nm_privflag |= NFSMNTP_SEEKTESTED | 4280 NFSMNTP_SEEK; 4281 error = 0; 4282 } 4283 if ((nmp->nm_privflag & NFSMNTP_SEEK) != 0) 4284 *ap->a_retval = vp->v_mount->mnt_stat.f_iosize; 4285 mtx_unlock(&nmp->nm_mtx); 4286 } 4287 break; 4288 4289 default: 4290 error = vop_stdpathconf(ap); 4291 break; 4292 } 4293 return (error); 4294 } 4295 4296