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