1 /* 2 * Copyright (c) 1982, 1986, 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 4. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)vfs_vnops.c 8.2 (Berkeley) 1/21/94 35 */ 36 37 #include <sys/cdefs.h> 38 __FBSDID("$FreeBSD$"); 39 40 #include "opt_mac.h" 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/fcntl.h> 45 #include <sys/file.h> 46 #include <sys/stat.h> 47 #include <sys/proc.h> 48 #include <sys/limits.h> 49 #include <sys/lock.h> 50 #include <sys/mac.h> 51 #include <sys/mount.h> 52 #include <sys/mutex.h> 53 #include <sys/namei.h> 54 #include <sys/vnode.h> 55 #include <sys/bio.h> 56 #include <sys/buf.h> 57 #include <sys/filio.h> 58 #include <sys/sx.h> 59 #include <sys/ttycom.h> 60 #include <sys/conf.h> 61 #include <sys/syslog.h> 62 #include <sys/unistd.h> 63 64 static fo_rdwr_t vn_read; 65 static fo_rdwr_t vn_write; 66 static fo_ioctl_t vn_ioctl; 67 static fo_poll_t vn_poll; 68 static fo_kqfilter_t vn_kqfilter; 69 static fo_stat_t vn_statfile; 70 static fo_close_t vn_closefile; 71 72 struct fileops vnops = { 73 .fo_read = vn_read, 74 .fo_write = vn_write, 75 .fo_ioctl = vn_ioctl, 76 .fo_poll = vn_poll, 77 .fo_kqfilter = vn_kqfilter, 78 .fo_stat = vn_statfile, 79 .fo_close = vn_closefile, 80 .fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE 81 }; 82 83 int 84 vn_open(ndp, flagp, cmode, fdidx) 85 struct nameidata *ndp; 86 int *flagp, cmode, fdidx; 87 { 88 struct thread *td = ndp->ni_cnd.cn_thread; 89 90 return (vn_open_cred(ndp, flagp, cmode, td->td_ucred, fdidx)); 91 } 92 93 /* 94 * Common code for vnode open operations. 95 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine. 96 * 97 * Note that this does NOT free nameidata for the successful case, 98 * due to the NDINIT being done elsewhere. 99 */ 100 int 101 vn_open_cred(ndp, flagp, cmode, cred, fdidx) 102 struct nameidata *ndp; 103 int *flagp, cmode; 104 struct ucred *cred; 105 int fdidx; 106 { 107 struct vnode *vp; 108 struct mount *mp; 109 struct thread *td = ndp->ni_cnd.cn_thread; 110 struct vattr vat; 111 struct vattr *vap = &vat; 112 int mode, fmode, error; 113 #ifdef LOOKUP_SHARED 114 int exclusive; /* The current intended lock state */ 115 116 exclusive = 0; 117 #endif 118 119 restart: 120 fmode = *flagp; 121 if (fmode & O_CREAT) { 122 ndp->ni_cnd.cn_nameiop = CREATE; 123 ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF; 124 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0) 125 ndp->ni_cnd.cn_flags |= FOLLOW; 126 bwillwrite(); 127 if ((error = namei(ndp)) != 0) 128 return (error); 129 if (ndp->ni_vp == NULL) { 130 VATTR_NULL(vap); 131 vap->va_type = VREG; 132 vap->va_mode = cmode; 133 if (fmode & O_EXCL) 134 vap->va_vaflags |= VA_EXCLUSIVE; 135 if (vn_start_write(ndp->ni_dvp, &mp, V_NOWAIT) != 0) { 136 NDFREE(ndp, NDF_ONLY_PNBUF); 137 vput(ndp->ni_dvp); 138 if ((error = vn_start_write(NULL, &mp, 139 V_XSLEEP | PCATCH)) != 0) 140 return (error); 141 goto restart; 142 } 143 #ifdef MAC 144 error = mac_check_vnode_create(cred, ndp->ni_dvp, 145 &ndp->ni_cnd, vap); 146 if (error == 0) { 147 #endif 148 VOP_LEASE(ndp->ni_dvp, td, cred, LEASE_WRITE); 149 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp, 150 &ndp->ni_cnd, vap); 151 #ifdef MAC 152 } 153 #endif 154 vput(ndp->ni_dvp); 155 vn_finished_write(mp); 156 if (error) { 157 NDFREE(ndp, NDF_ONLY_PNBUF); 158 return (error); 159 } 160 ASSERT_VOP_UNLOCKED(ndp->ni_dvp, "create"); 161 ASSERT_VOP_LOCKED(ndp->ni_vp, "create"); 162 fmode &= ~O_TRUNC; 163 vp = ndp->ni_vp; 164 #ifdef LOOKUP_SHARED 165 exclusive = 1; 166 #endif 167 } else { 168 if (ndp->ni_dvp == ndp->ni_vp) 169 vrele(ndp->ni_dvp); 170 else 171 vput(ndp->ni_dvp); 172 ndp->ni_dvp = NULL; 173 vp = ndp->ni_vp; 174 if (fmode & O_EXCL) { 175 error = EEXIST; 176 goto bad; 177 } 178 fmode &= ~O_CREAT; 179 } 180 } else { 181 ndp->ni_cnd.cn_nameiop = LOOKUP; 182 #ifdef LOOKUP_SHARED 183 ndp->ni_cnd.cn_flags = 184 ((fmode & O_NOFOLLOW) ? NOFOLLOW : FOLLOW) | 185 LOCKSHARED | LOCKLEAF; 186 #else 187 ndp->ni_cnd.cn_flags = 188 ((fmode & O_NOFOLLOW) ? NOFOLLOW : FOLLOW) | LOCKLEAF; 189 #endif 190 if ((error = namei(ndp)) != 0) 191 return (error); 192 vp = ndp->ni_vp; 193 } 194 if (vp->v_type == VLNK) { 195 error = EMLINK; 196 goto bad; 197 } 198 if (vp->v_type == VSOCK) { 199 error = EOPNOTSUPP; 200 goto bad; 201 } 202 mode = 0; 203 if (fmode & (FWRITE | O_TRUNC)) { 204 if (vp->v_type == VDIR) { 205 error = EISDIR; 206 goto bad; 207 } 208 mode |= VWRITE; 209 } 210 if (fmode & FREAD) 211 mode |= VREAD; 212 if (fmode & O_APPEND) 213 mode |= VAPPEND; 214 #ifdef MAC 215 error = mac_check_vnode_open(cred, vp, mode); 216 if (error) 217 goto bad; 218 #endif 219 if ((fmode & O_CREAT) == 0) { 220 if (mode & VWRITE) { 221 error = vn_writechk(vp); 222 if (error) 223 goto bad; 224 } 225 if (mode) { 226 error = VOP_ACCESS(vp, mode, cred, td); 227 if (error) 228 goto bad; 229 } 230 } 231 if ((error = VOP_GETATTR(vp, vap, cred, td)) == 0) { 232 vp->v_cachedfs = vap->va_fsid; 233 vp->v_cachedid = vap->va_fileid; 234 } 235 if ((error = VOP_OPEN(vp, fmode, cred, td, fdidx)) != 0) 236 goto bad; 237 /* 238 * Make sure that a VM object is created for VMIO support. 239 */ 240 if (vn_canvmio(vp) == TRUE) { 241 #ifdef LOOKUP_SHARED 242 int flock; 243 244 if (!exclusive && VOP_GETVOBJECT(vp, NULL) != 0) 245 VOP_LOCK(vp, LK_UPGRADE, td); 246 /* 247 * In cases where the object is marked as dead object_create 248 * will unlock and relock exclusive. It is safe to call in 249 * here with a shared lock because we only examine fields that 250 * the shared lock guarantees will be stable. In the UPGRADE 251 * case it is not likely that anyone has used this vnode yet 252 * so there will be no contention. The logic after this call 253 * restores the requested locking state. 254 */ 255 #endif 256 if ((error = vfs_object_create(vp, td, cred)) != 0) { 257 VOP_UNLOCK(vp, 0, td); 258 VOP_CLOSE(vp, fmode, cred, td); 259 NDFREE(ndp, NDF_ONLY_PNBUF); 260 vrele(vp); 261 *flagp = fmode; 262 return (error); 263 } 264 #ifdef LOOKUP_SHARED 265 flock = VOP_ISLOCKED(vp, td); 266 if (!exclusive && flock == LK_EXCLUSIVE) 267 VOP_LOCK(vp, LK_DOWNGRADE, td); 268 #endif 269 } 270 271 if (fmode & FWRITE) 272 vp->v_writecount++; 273 *flagp = fmode; 274 ASSERT_VOP_LOCKED(vp, "vn_open_cred"); 275 return (0); 276 bad: 277 NDFREE(ndp, NDF_ONLY_PNBUF); 278 vput(vp); 279 *flagp = fmode; 280 ndp->ni_vp = NULL; 281 return (error); 282 } 283 284 /* 285 * Check for write permissions on the specified vnode. 286 * Prototype text segments cannot be written. 287 */ 288 int 289 vn_writechk(vp) 290 register struct vnode *vp; 291 { 292 293 ASSERT_VOP_LOCKED(vp, "vn_writechk"); 294 /* 295 * If there's shared text associated with 296 * the vnode, try to free it up once. If 297 * we fail, we can't allow writing. 298 */ 299 if (vp->v_vflag & VV_TEXT) 300 return (ETXTBSY); 301 302 return (0); 303 } 304 305 /* 306 * Vnode close call 307 */ 308 int 309 vn_close(vp, flags, file_cred, td) 310 register struct vnode *vp; 311 int flags; 312 struct ucred *file_cred; 313 struct thread *td; 314 { 315 int error; 316 317 if (flags & FWRITE) 318 vp->v_writecount--; 319 error = VOP_CLOSE(vp, flags, file_cred, td); 320 /* 321 * XXX - In certain instances VOP_CLOSE has to do the vrele 322 * itself. If the vrele has been done, it will return EAGAIN 323 * to indicate that the vrele should not be done again. When 324 * this happens, we just return success. The correct thing to 325 * do would be to have all VOP_CLOSE instances do the vrele. 326 */ 327 if (error == EAGAIN) 328 return (0); 329 vrele(vp); 330 return (error); 331 } 332 333 /* 334 * Sequential heuristic - detect sequential operation 335 */ 336 static __inline 337 int 338 sequential_heuristic(struct uio *uio, struct file *fp) 339 { 340 341 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) || 342 uio->uio_offset == fp->f_nextoff) { 343 /* 344 * XXX we assume that the filesystem block size is 345 * the default. Not true, but still gives us a pretty 346 * good indicator of how sequential the read operations 347 * are. 348 */ 349 fp->f_seqcount += (uio->uio_resid + BKVASIZE - 1) / BKVASIZE; 350 if (fp->f_seqcount > IO_SEQMAX) 351 fp->f_seqcount = IO_SEQMAX; 352 return(fp->f_seqcount << IO_SEQSHIFT); 353 } 354 355 /* 356 * Not sequential, quick draw-down of seqcount 357 */ 358 if (fp->f_seqcount > 1) 359 fp->f_seqcount = 1; 360 else 361 fp->f_seqcount = 0; 362 return(0); 363 } 364 365 /* 366 * Package up an I/O request on a vnode into a uio and do it. 367 */ 368 int 369 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, active_cred, file_cred, 370 aresid, td) 371 enum uio_rw rw; 372 struct vnode *vp; 373 caddr_t base; 374 int len; 375 off_t offset; 376 enum uio_seg segflg; 377 int ioflg; 378 struct ucred *active_cred; 379 struct ucred *file_cred; 380 int *aresid; 381 struct thread *td; 382 { 383 struct uio auio; 384 struct iovec aiov; 385 struct mount *mp; 386 struct ucred *cred; 387 int error; 388 389 if ((ioflg & IO_NODELOCKED) == 0) { 390 mp = NULL; 391 if (rw == UIO_WRITE) { 392 if (vp->v_type != VCHR && 393 (error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) 394 != 0) 395 return (error); 396 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 397 } else { 398 /* 399 * XXX This should be LK_SHARED but I don't trust VFS 400 * enough to leave it like that until it has been 401 * reviewed further. 402 */ 403 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 404 } 405 406 } 407 auio.uio_iov = &aiov; 408 auio.uio_iovcnt = 1; 409 aiov.iov_base = base; 410 aiov.iov_len = len; 411 auio.uio_resid = len; 412 auio.uio_offset = offset; 413 auio.uio_segflg = segflg; 414 auio.uio_rw = rw; 415 auio.uio_td = td; 416 error = 0; 417 #ifdef MAC 418 if ((ioflg & IO_NOMACCHECK) == 0) { 419 if (rw == UIO_READ) 420 error = mac_check_vnode_read(active_cred, file_cred, 421 vp); 422 else 423 error = mac_check_vnode_write(active_cred, file_cred, 424 vp); 425 } 426 #endif 427 if (error == 0) { 428 if (file_cred) 429 cred = file_cred; 430 else 431 cred = active_cred; 432 if (rw == UIO_READ) 433 error = VOP_READ(vp, &auio, ioflg, cred); 434 else 435 error = VOP_WRITE(vp, &auio, ioflg, cred); 436 } 437 if (aresid) 438 *aresid = auio.uio_resid; 439 else 440 if (auio.uio_resid && error == 0) 441 error = EIO; 442 if ((ioflg & IO_NODELOCKED) == 0) { 443 if (rw == UIO_WRITE) 444 vn_finished_write(mp); 445 VOP_UNLOCK(vp, 0, td); 446 } 447 return (error); 448 } 449 450 /* 451 * Package up an I/O request on a vnode into a uio and do it. The I/O 452 * request is split up into smaller chunks and we try to avoid saturating 453 * the buffer cache while potentially holding a vnode locked, so we 454 * check bwillwrite() before calling vn_rdwr(). We also call uio_yield() 455 * to give other processes a chance to lock the vnode (either other processes 456 * core'ing the same binary, or unrelated processes scanning the directory). 457 */ 458 int 459 vn_rdwr_inchunks(rw, vp, base, len, offset, segflg, ioflg, active_cred, 460 file_cred, aresid, td) 461 enum uio_rw rw; 462 struct vnode *vp; 463 caddr_t base; 464 size_t len; 465 off_t offset; 466 enum uio_seg segflg; 467 int ioflg; 468 struct ucred *active_cred; 469 struct ucred *file_cred; 470 size_t *aresid; 471 struct thread *td; 472 { 473 int error = 0; 474 int iaresid; 475 476 do { 477 int chunk; 478 479 /* 480 * Force `offset' to a multiple of MAXBSIZE except possibly 481 * for the first chunk, so that filesystems only need to 482 * write full blocks except possibly for the first and last 483 * chunks. 484 */ 485 chunk = MAXBSIZE - (uoff_t)offset % MAXBSIZE; 486 487 if (chunk > len) 488 chunk = len; 489 if (rw != UIO_READ && vp->v_type == VREG) 490 bwillwrite(); 491 iaresid = 0; 492 error = vn_rdwr(rw, vp, base, chunk, offset, segflg, 493 ioflg, active_cred, file_cred, &iaresid, td); 494 len -= chunk; /* aresid calc already includes length */ 495 if (error) 496 break; 497 offset += chunk; 498 base += chunk; 499 uio_yield(); 500 } while (len); 501 if (aresid) 502 *aresid = len + iaresid; 503 return (error); 504 } 505 506 /* 507 * File table vnode read routine. 508 */ 509 static int 510 vn_read(fp, uio, active_cred, flags, td) 511 struct file *fp; 512 struct uio *uio; 513 struct ucred *active_cred; 514 struct thread *td; 515 int flags; 516 { 517 struct vnode *vp; 518 int error, ioflag; 519 520 mtx_lock(&Giant); 521 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", 522 uio->uio_td, td)); 523 vp = fp->f_vnode; 524 ioflag = 0; 525 if (fp->f_flag & FNONBLOCK) 526 ioflag |= IO_NDELAY; 527 if (fp->f_flag & O_DIRECT) 528 ioflag |= IO_DIRECT; 529 VOP_LEASE(vp, td, fp->f_cred, LEASE_READ); 530 /* 531 * According to McKusick the vn lock is protecting f_offset here. 532 * Once this field has it's own lock we can acquire this shared. 533 */ 534 if ((flags & FOF_OFFSET) == 0) { 535 vn_lock(vp, LK_EXCLUSIVE | LK_NOPAUSE | LK_RETRY, td); 536 uio->uio_offset = fp->f_offset; 537 } else 538 vn_lock(vp, LK_SHARED | LK_NOPAUSE | LK_RETRY, td); 539 540 ioflag |= sequential_heuristic(uio, fp); 541 542 #ifdef MAC 543 error = mac_check_vnode_read(active_cred, fp->f_cred, vp); 544 if (error == 0) 545 #endif 546 error = VOP_READ(vp, uio, ioflag, fp->f_cred); 547 if ((flags & FOF_OFFSET) == 0) 548 fp->f_offset = uio->uio_offset; 549 fp->f_nextoff = uio->uio_offset; 550 VOP_UNLOCK(vp, 0, td); 551 mtx_unlock(&Giant); 552 return (error); 553 } 554 555 /* 556 * File table vnode write routine. 557 */ 558 static int 559 vn_write(fp, uio, active_cred, flags, td) 560 struct file *fp; 561 struct uio *uio; 562 struct ucred *active_cred; 563 struct thread *td; 564 int flags; 565 { 566 struct vnode *vp; 567 struct mount *mp; 568 int error, ioflag; 569 570 mtx_lock(&Giant); 571 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", 572 uio->uio_td, td)); 573 vp = fp->f_vnode; 574 if (vp->v_type == VREG) 575 bwillwrite(); 576 ioflag = IO_UNIT; 577 if (vp->v_type == VREG && (fp->f_flag & O_APPEND)) 578 ioflag |= IO_APPEND; 579 if (fp->f_flag & FNONBLOCK) 580 ioflag |= IO_NDELAY; 581 if (fp->f_flag & O_DIRECT) 582 ioflag |= IO_DIRECT; 583 if ((fp->f_flag & O_FSYNC) || 584 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))) 585 ioflag |= IO_SYNC; 586 mp = NULL; 587 if (vp->v_type != VCHR && 588 (error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) { 589 mtx_unlock(&Giant); 590 return (error); 591 } 592 VOP_LEASE(vp, td, fp->f_cred, LEASE_WRITE); 593 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 594 if ((flags & FOF_OFFSET) == 0) 595 uio->uio_offset = fp->f_offset; 596 ioflag |= sequential_heuristic(uio, fp); 597 #ifdef MAC 598 error = mac_check_vnode_write(active_cred, fp->f_cred, vp); 599 if (error == 0) 600 #endif 601 error = VOP_WRITE(vp, uio, ioflag, fp->f_cred); 602 if ((flags & FOF_OFFSET) == 0) 603 fp->f_offset = uio->uio_offset; 604 fp->f_nextoff = uio->uio_offset; 605 VOP_UNLOCK(vp, 0, td); 606 vn_finished_write(mp); 607 mtx_unlock(&Giant); 608 return (error); 609 } 610 611 /* 612 * File table vnode stat routine. 613 */ 614 static int 615 vn_statfile(fp, sb, active_cred, td) 616 struct file *fp; 617 struct stat *sb; 618 struct ucred *active_cred; 619 struct thread *td; 620 { 621 struct vnode *vp = fp->f_vnode; 622 int error; 623 624 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 625 error = vn_stat(vp, sb, active_cred, fp->f_cred, td); 626 VOP_UNLOCK(vp, 0, td); 627 628 return (error); 629 } 630 631 /* 632 * Stat a vnode; implementation for the stat syscall 633 */ 634 int 635 vn_stat(vp, sb, active_cred, file_cred, td) 636 struct vnode *vp; 637 register struct stat *sb; 638 struct ucred *active_cred; 639 struct ucred *file_cred; 640 struct thread *td; 641 { 642 struct vattr vattr; 643 register struct vattr *vap; 644 int error; 645 u_short mode; 646 647 #ifdef MAC 648 error = mac_check_vnode_stat(active_cred, file_cred, vp); 649 if (error) 650 return (error); 651 #endif 652 653 vap = &vattr; 654 error = VOP_GETATTR(vp, vap, active_cred, td); 655 if (error) 656 return (error); 657 658 vp->v_cachedfs = vap->va_fsid; 659 vp->v_cachedid = vap->va_fileid; 660 661 /* 662 * Zero the spare stat fields 663 */ 664 bzero(sb, sizeof *sb); 665 666 /* 667 * Copy from vattr table 668 */ 669 if (vap->va_fsid != VNOVAL) 670 sb->st_dev = vap->va_fsid; 671 else 672 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0]; 673 sb->st_ino = vap->va_fileid; 674 mode = vap->va_mode; 675 switch (vap->va_type) { 676 case VREG: 677 mode |= S_IFREG; 678 break; 679 case VDIR: 680 mode |= S_IFDIR; 681 break; 682 case VBLK: 683 mode |= S_IFBLK; 684 break; 685 case VCHR: 686 mode |= S_IFCHR; 687 break; 688 case VLNK: 689 mode |= S_IFLNK; 690 /* This is a cosmetic change, symlinks do not have a mode. */ 691 if (vp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) 692 sb->st_mode &= ~ACCESSPERMS; /* 0000 */ 693 else 694 sb->st_mode |= ACCESSPERMS; /* 0777 */ 695 break; 696 case VSOCK: 697 mode |= S_IFSOCK; 698 break; 699 case VFIFO: 700 mode |= S_IFIFO; 701 break; 702 default: 703 return (EBADF); 704 }; 705 sb->st_mode = mode; 706 sb->st_nlink = vap->va_nlink; 707 sb->st_uid = vap->va_uid; 708 sb->st_gid = vap->va_gid; 709 sb->st_rdev = vap->va_rdev; 710 if (vap->va_size > OFF_MAX) 711 return (EOVERFLOW); 712 sb->st_size = vap->va_size; 713 sb->st_atimespec = vap->va_atime; 714 sb->st_mtimespec = vap->va_mtime; 715 sb->st_ctimespec = vap->va_ctime; 716 sb->st_birthtimespec = vap->va_birthtime; 717 718 /* 719 * According to www.opengroup.org, the meaning of st_blksize is 720 * "a filesystem-specific preferred I/O block size for this 721 * object. In some filesystem types, this may vary from file 722 * to file" 723 * Default to PAGE_SIZE after much discussion. 724 */ 725 726 if (vap->va_type == VREG) { 727 sb->st_blksize = vap->va_blocksize; 728 } else if (vn_isdisk(vp, NULL)) { 729 sb->st_blksize = vp->v_rdev->si_bsize_best; 730 if (sb->st_blksize < vp->v_rdev->si_bsize_phys) 731 sb->st_blksize = vp->v_rdev->si_bsize_phys; 732 if (sb->st_blksize < BLKDEV_IOSIZE) 733 sb->st_blksize = BLKDEV_IOSIZE; 734 } else { 735 sb->st_blksize = PAGE_SIZE; 736 } 737 738 sb->st_flags = vap->va_flags; 739 if (suser(td)) 740 sb->st_gen = 0; 741 else 742 sb->st_gen = vap->va_gen; 743 744 #if (S_BLKSIZE == 512) 745 /* Optimize this case */ 746 sb->st_blocks = vap->va_bytes >> 9; 747 #else 748 sb->st_blocks = vap->va_bytes / S_BLKSIZE; 749 #endif 750 return (0); 751 } 752 753 /* 754 * File table vnode ioctl routine. 755 */ 756 static int 757 vn_ioctl(fp, com, data, active_cred, td) 758 struct file *fp; 759 u_long com; 760 void *data; 761 struct ucred *active_cred; 762 struct thread *td; 763 { 764 struct vnode *vp = fp->f_vnode; 765 struct vnode *vpold; 766 struct vattr vattr; 767 int error; 768 769 switch (vp->v_type) { 770 771 case VREG: 772 case VDIR: 773 if (com == FIONREAD) { 774 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 775 error = VOP_GETATTR(vp, &vattr, active_cred, td); 776 VOP_UNLOCK(vp, 0, td); 777 if (error) 778 return (error); 779 *(int *)data = vattr.va_size - fp->f_offset; 780 return (0); 781 } 782 if (com == FIONBIO || com == FIOASYNC) /* XXX */ 783 return (0); /* XXX */ 784 /* FALLTHROUGH */ 785 786 default: 787 #if 0 788 return (ENOTTY); 789 #endif 790 case VFIFO: 791 case VCHR: 792 case VBLK: 793 if (com == FIODTYPE) { 794 if (vp->v_type != VCHR && vp->v_type != VBLK) 795 return (ENOTTY); 796 *(int *)data = devsw(vp->v_rdev)->d_flags & D_TYPEMASK; 797 return (0); 798 } 799 error = VOP_IOCTL(vp, com, data, fp->f_flag, active_cred, td); 800 if (error == ENOIOCTL) { 801 #ifdef DIAGNOSTIC 802 Debugger("ENOIOCTL leaked through"); 803 #endif 804 error = ENOTTY; 805 } 806 if (error == 0 && com == TIOCSCTTY) { 807 808 /* Do nothing if reassigning same control tty */ 809 sx_slock(&proctree_lock); 810 if (td->td_proc->p_session->s_ttyvp == vp) { 811 sx_sunlock(&proctree_lock); 812 return (0); 813 } 814 815 vpold = td->td_proc->p_session->s_ttyvp; 816 VREF(vp); 817 SESS_LOCK(td->td_proc->p_session); 818 td->td_proc->p_session->s_ttyvp = vp; 819 SESS_UNLOCK(td->td_proc->p_session); 820 821 sx_sunlock(&proctree_lock); 822 823 /* Get rid of reference to old control tty */ 824 if (vpold) 825 vrele(vpold); 826 } 827 return (error); 828 } 829 } 830 831 /* 832 * File table vnode poll routine. 833 */ 834 static int 835 vn_poll(fp, events, active_cred, td) 836 struct file *fp; 837 int events; 838 struct ucred *active_cred; 839 struct thread *td; 840 { 841 struct vnode *vp; 842 #ifdef MAC 843 int error; 844 #endif 845 846 vp = fp->f_vnode; 847 #ifdef MAC 848 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 849 error = mac_check_vnode_poll(active_cred, fp->f_cred, vp); 850 VOP_UNLOCK(vp, 0, td); 851 if (error) 852 return (error); 853 #endif 854 855 return (VOP_POLL(vp, events, fp->f_cred, td)); 856 } 857 858 /* 859 * Check that the vnode is still valid, and if so 860 * acquire requested lock. 861 */ 862 int 863 #ifndef DEBUG_LOCKS 864 vn_lock(vp, flags, td) 865 #else 866 debug_vn_lock(vp, flags, td, filename, line) 867 #endif 868 struct vnode *vp; 869 int flags; 870 struct thread *td; 871 #ifdef DEBUG_LOCKS 872 const char *filename; 873 int line; 874 #endif 875 { 876 int error; 877 878 do { 879 if ((flags & LK_INTERLOCK) == 0) 880 VI_LOCK(vp); 881 if ((vp->v_iflag & VI_XLOCK) && vp->v_vxthread != curthread) { 882 if ((flags & LK_NOWAIT) != 0) { 883 VI_UNLOCK(vp); 884 return (ENOENT); 885 } 886 vp->v_iflag |= VI_XWANT; 887 msleep(vp, VI_MTX(vp), PINOD, "vn_lock", 0); 888 if ((flags & LK_RETRY) == 0) { 889 VI_UNLOCK(vp); 890 return (ENOENT); 891 } 892 } 893 #ifdef DEBUG_LOCKS 894 vp->filename = filename; 895 vp->line = line; 896 #endif 897 /* 898 * lockmgr drops interlock before it will return for 899 * any reason. So force the code above to relock it. 900 */ 901 error = VOP_LOCK(vp, flags | LK_NOPAUSE | LK_INTERLOCK, td); 902 flags &= ~LK_INTERLOCK; 903 } while (flags & LK_RETRY && error != 0); 904 return (error); 905 } 906 907 /* 908 * File table vnode close routine. 909 */ 910 static int 911 vn_closefile(fp, td) 912 struct file *fp; 913 struct thread *td; 914 { 915 struct vnode *vp; 916 struct flock lf; 917 918 GIANT_REQUIRED; 919 920 vp = fp->f_vnode; 921 922 if (fp->f_type == DTYPE_VNODE && fp->f_flag & FHASLOCK) { 923 lf.l_whence = SEEK_SET; 924 lf.l_start = 0; 925 lf.l_len = 0; 926 lf.l_type = F_UNLCK; 927 (void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK); 928 } 929 930 fp->f_ops = &badfileops; 931 932 return (vn_close(vp, fp->f_flag, fp->f_cred, td)); 933 } 934 935 /* 936 * Preparing to start a filesystem write operation. If the operation is 937 * permitted, then we bump the count of operations in progress and 938 * proceed. If a suspend request is in progress, we wait until the 939 * suspension is over, and then proceed. 940 */ 941 int 942 vn_start_write(vp, mpp, flags) 943 struct vnode *vp; 944 struct mount **mpp; 945 int flags; 946 { 947 struct mount *mp; 948 int error; 949 950 GIANT_REQUIRED; 951 952 /* 953 * If a vnode is provided, get and return the mount point that 954 * to which it will write. 955 */ 956 if (vp != NULL) { 957 if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) { 958 *mpp = NULL; 959 if (error != EOPNOTSUPP) 960 return (error); 961 return (0); 962 } 963 } 964 if ((mp = *mpp) == NULL) 965 return (0); 966 /* 967 * Check on status of suspension. 968 */ 969 while ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) { 970 if (flags & V_NOWAIT) 971 return (EWOULDBLOCK); 972 error = tsleep(&mp->mnt_flag, (PUSER - 1) | (flags & PCATCH), 973 "suspfs", 0); 974 if (error) 975 return (error); 976 } 977 if (flags & V_XSLEEP) 978 return (0); 979 mp->mnt_writeopcount++; 980 return (0); 981 } 982 983 /* 984 * Secondary suspension. Used by operations such as vop_inactive 985 * routines that are needed by the higher level functions. These 986 * are allowed to proceed until all the higher level functions have 987 * completed (indicated by mnt_writeopcount dropping to zero). At that 988 * time, these operations are halted until the suspension is over. 989 */ 990 int 991 vn_write_suspend_wait(vp, mp, flags) 992 struct vnode *vp; 993 struct mount *mp; 994 int flags; 995 { 996 int error; 997 998 if (vp != NULL) { 999 if ((error = VOP_GETWRITEMOUNT(vp, &mp)) != 0) { 1000 if (error != EOPNOTSUPP) 1001 return (error); 1002 return (0); 1003 } 1004 } 1005 /* 1006 * If we are not suspended or have not yet reached suspended 1007 * mode, then let the operation proceed. 1008 */ 1009 if (mp == NULL || (mp->mnt_kern_flag & MNTK_SUSPENDED) == 0) 1010 return (0); 1011 if (flags & V_NOWAIT) 1012 return (EWOULDBLOCK); 1013 /* 1014 * Wait for the suspension to finish. 1015 */ 1016 return (tsleep(&mp->mnt_flag, (PUSER - 1) | (flags & PCATCH), 1017 "suspfs", 0)); 1018 } 1019 1020 /* 1021 * Filesystem write operation has completed. If we are suspending and this 1022 * operation is the last one, notify the suspender that the suspension is 1023 * now in effect. 1024 */ 1025 void 1026 vn_finished_write(mp) 1027 struct mount *mp; 1028 { 1029 1030 GIANT_REQUIRED; 1031 1032 if (mp == NULL) 1033 return; 1034 mp->mnt_writeopcount--; 1035 if (mp->mnt_writeopcount < 0) 1036 panic("vn_finished_write: neg cnt"); 1037 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 && 1038 mp->mnt_writeopcount <= 0) 1039 wakeup(&mp->mnt_writeopcount); 1040 } 1041 1042 /* 1043 * Request a filesystem to suspend write operations. 1044 */ 1045 int 1046 vfs_write_suspend(mp) 1047 struct mount *mp; 1048 { 1049 struct thread *td = curthread; 1050 int error; 1051 1052 if (mp->mnt_kern_flag & MNTK_SUSPEND) 1053 return (0); 1054 mp->mnt_kern_flag |= MNTK_SUSPEND; 1055 if (mp->mnt_writeopcount > 0) 1056 (void) tsleep(&mp->mnt_writeopcount, PUSER - 1, "suspwt", 0); 1057 if ((error = VFS_SYNC(mp, MNT_WAIT, td->td_ucred, td)) != 0) { 1058 vfs_write_resume(mp); 1059 return (error); 1060 } 1061 mp->mnt_kern_flag |= MNTK_SUSPENDED; 1062 return (0); 1063 } 1064 1065 /* 1066 * Request a filesystem to resume write operations. 1067 */ 1068 void 1069 vfs_write_resume(mp) 1070 struct mount *mp; 1071 { 1072 1073 if ((mp->mnt_kern_flag & MNTK_SUSPEND) == 0) 1074 return; 1075 mp->mnt_kern_flag &= ~(MNTK_SUSPEND | MNTK_SUSPENDED); 1076 wakeup(&mp->mnt_writeopcount); 1077 wakeup(&mp->mnt_flag); 1078 } 1079 1080 /* 1081 * Implement kqueues for files by translating it to vnode operation. 1082 */ 1083 static int 1084 vn_kqfilter(struct file *fp, struct knote *kn) 1085 { 1086 1087 return (VOP_KQFILTER(fp->f_vnode, kn)); 1088 } 1089 1090 /* 1091 * Simplified in-kernel wrapper calls for extended attribute access. 1092 * Both calls pass in a NULL credential, authorizing as "kernel" access. 1093 * Set IO_NODELOCKED in ioflg if the vnode is already locked. 1094 */ 1095 int 1096 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace, 1097 const char *attrname, int *buflen, char *buf, struct thread *td) 1098 { 1099 struct uio auio; 1100 struct iovec iov; 1101 int error; 1102 1103 iov.iov_len = *buflen; 1104 iov.iov_base = buf; 1105 1106 auio.uio_iov = &iov; 1107 auio.uio_iovcnt = 1; 1108 auio.uio_rw = UIO_READ; 1109 auio.uio_segflg = UIO_SYSSPACE; 1110 auio.uio_td = td; 1111 auio.uio_offset = 0; 1112 auio.uio_resid = *buflen; 1113 1114 if ((ioflg & IO_NODELOCKED) == 0) 1115 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 1116 1117 /* authorize attribute retrieval as kernel */ 1118 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL, 1119 td); 1120 1121 if ((ioflg & IO_NODELOCKED) == 0) 1122 VOP_UNLOCK(vp, 0, td); 1123 1124 if (error == 0) { 1125 *buflen = *buflen - auio.uio_resid; 1126 } 1127 1128 return (error); 1129 } 1130 1131 /* 1132 * XXX failure mode if partially written? 1133 */ 1134 int 1135 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace, 1136 const char *attrname, int buflen, char *buf, struct thread *td) 1137 { 1138 struct uio auio; 1139 struct iovec iov; 1140 struct mount *mp; 1141 int error; 1142 1143 iov.iov_len = buflen; 1144 iov.iov_base = buf; 1145 1146 auio.uio_iov = &iov; 1147 auio.uio_iovcnt = 1; 1148 auio.uio_rw = UIO_WRITE; 1149 auio.uio_segflg = UIO_SYSSPACE; 1150 auio.uio_td = td; 1151 auio.uio_offset = 0; 1152 auio.uio_resid = buflen; 1153 1154 if ((ioflg & IO_NODELOCKED) == 0) { 1155 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0) 1156 return (error); 1157 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 1158 } 1159 1160 /* authorize attribute setting as kernel */ 1161 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, td); 1162 1163 if ((ioflg & IO_NODELOCKED) == 0) { 1164 vn_finished_write(mp); 1165 VOP_UNLOCK(vp, 0, td); 1166 } 1167 1168 return (error); 1169 } 1170 1171 int 1172 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace, 1173 const char *attrname, struct thread *td) 1174 { 1175 struct mount *mp; 1176 int error; 1177 1178 if ((ioflg & IO_NODELOCKED) == 0) { 1179 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0) 1180 return (error); 1181 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 1182 } 1183 1184 /* authorize attribute removal as kernel */ 1185 error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL, td); 1186 if (error == EOPNOTSUPP) 1187 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL, 1188 NULL, td); 1189 1190 if ((ioflg & IO_NODELOCKED) == 0) { 1191 vn_finished_write(mp); 1192 VOP_UNLOCK(vp, 0, td); 1193 } 1194 1195 return (error); 1196 } 1197