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