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