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