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