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