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