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