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