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