1 /* $NetBSD: vfs_vnops.c,v 1.183 2011/10/14 09:23:31 hannken Exp $ */ 2 3 /*- 4 * Copyright (c) 2009 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Andrew Doran. 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 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (c) 1982, 1986, 1989, 1993 34 * The Regents of the University of California. All rights reserved. 35 * (c) UNIX System Laboratories, Inc. 36 * All or some portions of this file are derived from material licensed 37 * to the University of California by American Telephone and Telegraph 38 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 39 * the permission of UNIX System Laboratories, Inc. 40 * 41 * Redistribution and use in source and binary forms, with or without 42 * modification, are permitted provided that the following conditions 43 * are met: 44 * 1. Redistributions of source code must retain the above copyright 45 * notice, this list of conditions and the following disclaimer. 46 * 2. Redistributions in binary form must reproduce the above copyright 47 * notice, this list of conditions and the following disclaimer in the 48 * documentation and/or other materials provided with the distribution. 49 * 3. Neither the name of the University nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 63 * SUCH DAMAGE. 64 * 65 * @(#)vfs_vnops.c 8.14 (Berkeley) 6/15/95 66 */ 67 68 #include <sys/cdefs.h> 69 __KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.183 2011/10/14 09:23:31 hannken Exp $"); 70 71 #include "veriexec.h" 72 73 #include <sys/param.h> 74 #include <sys/systm.h> 75 #include <sys/kernel.h> 76 #include <sys/file.h> 77 #include <sys/stat.h> 78 #include <sys/buf.h> 79 #include <sys/proc.h> 80 #include <sys/mount.h> 81 #include <sys/namei.h> 82 #include <sys/vnode.h> 83 #include <sys/ioctl.h> 84 #include <sys/tty.h> 85 #include <sys/poll.h> 86 #include <sys/kauth.h> 87 #include <sys/syslog.h> 88 #include <sys/fstrans.h> 89 #include <sys/atomic.h> 90 #include <sys/filedesc.h> 91 #include <sys/wapbl.h> 92 93 #include <miscfs/specfs/specdev.h> 94 #include <miscfs/fifofs/fifo.h> 95 96 #include <uvm/uvm_extern.h> 97 #include <uvm/uvm_readahead.h> 98 99 #ifdef UNION 100 #include <fs/union/union.h> 101 #endif 102 103 int (*vn_union_readdir_hook) (struct vnode **, struct file *, struct lwp *); 104 105 #include <sys/verified_exec.h> 106 107 static int vn_read(file_t *fp, off_t *offset, struct uio *uio, 108 kauth_cred_t cred, int flags); 109 static int vn_write(file_t *fp, off_t *offset, struct uio *uio, 110 kauth_cred_t cred, int flags); 111 static int vn_closefile(file_t *fp); 112 static int vn_poll(file_t *fp, int events); 113 static int vn_fcntl(file_t *fp, u_int com, void *data); 114 static int vn_statfile(file_t *fp, struct stat *sb); 115 static int vn_ioctl(file_t *fp, u_long com, void *data); 116 117 const struct fileops vnops = { 118 .fo_read = vn_read, 119 .fo_write = vn_write, 120 .fo_ioctl = vn_ioctl, 121 .fo_fcntl = vn_fcntl, 122 .fo_poll = vn_poll, 123 .fo_stat = vn_statfile, 124 .fo_close = vn_closefile, 125 .fo_kqfilter = vn_kqfilter, 126 .fo_restart = fnullop_restart, 127 }; 128 129 /* 130 * Common code for vnode open operations. 131 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine. 132 */ 133 int 134 vn_open(struct nameidata *ndp, int fmode, int cmode) 135 { 136 struct vnode *vp; 137 struct lwp *l = curlwp; 138 kauth_cred_t cred = l->l_cred; 139 struct vattr va; 140 int error; 141 const char *pathstring; 142 143 if ((fmode & (O_CREAT | O_DIRECTORY)) == (O_CREAT | O_DIRECTORY)) 144 return EINVAL; 145 146 ndp->ni_cnd.cn_flags &= TRYEMULROOT | NOCHROOT; 147 148 if (fmode & O_CREAT) { 149 ndp->ni_cnd.cn_nameiop = CREATE; 150 ndp->ni_cnd.cn_flags |= LOCKPARENT | LOCKLEAF; 151 if ((fmode & O_EXCL) == 0 && 152 ((fmode & O_NOFOLLOW) == 0)) 153 ndp->ni_cnd.cn_flags |= FOLLOW; 154 } else { 155 ndp->ni_cnd.cn_nameiop = LOOKUP; 156 ndp->ni_cnd.cn_flags |= LOCKLEAF; 157 if ((fmode & O_NOFOLLOW) == 0) 158 ndp->ni_cnd.cn_flags |= FOLLOW; 159 } 160 161 pathstring = pathbuf_stringcopy_get(ndp->ni_pathbuf); 162 if (pathstring == NULL) { 163 return ENOMEM; 164 } 165 166 error = namei(ndp); 167 if (error) 168 goto out; 169 170 vp = ndp->ni_vp; 171 172 #if NVERIEXEC > 0 173 error = veriexec_openchk(l, ndp->ni_vp, pathstring, fmode); 174 if (error) 175 goto bad; 176 #endif /* NVERIEXEC > 0 */ 177 178 if (fmode & O_CREAT) { 179 if (ndp->ni_vp == NULL) { 180 vattr_null(&va); 181 va.va_type = VREG; 182 va.va_mode = cmode; 183 if (fmode & O_EXCL) 184 va.va_vaflags |= VA_EXCLUSIVE; 185 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp, 186 &ndp->ni_cnd, &va); 187 if (error) 188 goto out; 189 fmode &= ~O_TRUNC; 190 vp = ndp->ni_vp; 191 } else { 192 VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd); 193 if (ndp->ni_dvp == ndp->ni_vp) 194 vrele(ndp->ni_dvp); 195 else 196 vput(ndp->ni_dvp); 197 ndp->ni_dvp = NULL; 198 vp = ndp->ni_vp; 199 if (fmode & O_EXCL) { 200 error = EEXIST; 201 goto bad; 202 } 203 fmode &= ~O_CREAT; 204 } 205 } else { 206 vp = ndp->ni_vp; 207 } 208 if (vp->v_type == VSOCK) { 209 error = EOPNOTSUPP; 210 goto bad; 211 } 212 if (ndp->ni_vp->v_type == VLNK) { 213 error = EFTYPE; 214 goto bad; 215 } 216 217 if ((fmode & O_CREAT) == 0) { 218 error = vn_openchk(vp, cred, fmode); 219 if (error != 0) 220 goto bad; 221 } 222 223 if (fmode & O_TRUNC) { 224 vattr_null(&va); 225 va.va_size = 0; 226 error = VOP_SETATTR(vp, &va, cred); 227 if (error != 0) 228 goto bad; 229 } 230 if ((error = VOP_OPEN(vp, fmode, cred)) != 0) 231 goto bad; 232 if (fmode & FWRITE) { 233 mutex_enter(vp->v_interlock); 234 vp->v_writecount++; 235 mutex_exit(vp->v_interlock); 236 } 237 238 bad: 239 if (error) 240 vput(vp); 241 out: 242 pathbuf_stringcopy_put(ndp->ni_pathbuf, pathstring); 243 return (error); 244 } 245 246 /* 247 * Check for write permissions on the specified vnode. 248 * Prototype text segments cannot be written. 249 */ 250 int 251 vn_writechk(struct vnode *vp) 252 { 253 254 /* 255 * If the vnode is in use as a process's text, 256 * we can't allow writing. 257 */ 258 if (vp->v_iflag & VI_TEXT) 259 return (ETXTBSY); 260 return (0); 261 } 262 263 int 264 vn_openchk(struct vnode *vp, kauth_cred_t cred, int fflags) 265 { 266 int permbits = 0; 267 int error; 268 269 if ((fflags & O_DIRECTORY) != 0 && vp->v_type != VDIR) 270 return ENOTDIR; 271 272 if ((fflags & FREAD) != 0) { 273 permbits = VREAD; 274 } 275 if ((fflags & (FWRITE | O_TRUNC)) != 0) { 276 permbits |= VWRITE; 277 if (vp->v_type == VDIR) { 278 error = EISDIR; 279 goto bad; 280 } 281 error = vn_writechk(vp); 282 if (error != 0) 283 goto bad; 284 } 285 error = VOP_ACCESS(vp, permbits, cred); 286 bad: 287 return error; 288 } 289 290 /* 291 * Mark a vnode as having executable mappings. 292 */ 293 void 294 vn_markexec(struct vnode *vp) 295 { 296 297 if ((vp->v_iflag & VI_EXECMAP) != 0) { 298 /* Safe unlocked, as long as caller holds a reference. */ 299 return; 300 } 301 302 mutex_enter(vp->v_interlock); 303 if ((vp->v_iflag & VI_EXECMAP) == 0) { 304 atomic_add_int(&uvmexp.filepages, -vp->v_uobj.uo_npages); 305 atomic_add_int(&uvmexp.execpages, vp->v_uobj.uo_npages); 306 vp->v_iflag |= VI_EXECMAP; 307 } 308 mutex_exit(vp->v_interlock); 309 } 310 311 /* 312 * Mark a vnode as being the text of a process. 313 * Fail if the vnode is currently writable. 314 */ 315 int 316 vn_marktext(struct vnode *vp) 317 { 318 319 if ((vp->v_iflag & (VI_TEXT|VI_EXECMAP)) == (VI_TEXT|VI_EXECMAP)) { 320 /* Safe unlocked, as long as caller holds a reference. */ 321 return (0); 322 } 323 324 mutex_enter(vp->v_interlock); 325 if (vp->v_writecount != 0) { 326 KASSERT((vp->v_iflag & VI_TEXT) == 0); 327 mutex_exit(vp->v_interlock); 328 return (ETXTBSY); 329 } 330 if ((vp->v_iflag & VI_EXECMAP) == 0) { 331 atomic_add_int(&uvmexp.filepages, -vp->v_uobj.uo_npages); 332 atomic_add_int(&uvmexp.execpages, vp->v_uobj.uo_npages); 333 } 334 vp->v_iflag |= (VI_TEXT | VI_EXECMAP); 335 mutex_exit(vp->v_interlock); 336 return (0); 337 } 338 339 /* 340 * Vnode close call 341 * 342 * Note: takes an unlocked vnode, while VOP_CLOSE takes a locked node. 343 */ 344 int 345 vn_close(struct vnode *vp, int flags, kauth_cred_t cred) 346 { 347 int error; 348 349 if (flags & FWRITE) { 350 mutex_enter(vp->v_interlock); 351 KASSERT(vp->v_writecount > 0); 352 vp->v_writecount--; 353 mutex_exit(vp->v_interlock); 354 } 355 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 356 error = VOP_CLOSE(vp, flags, cred); 357 vput(vp); 358 return (error); 359 } 360 361 static int 362 enforce_rlimit_fsize(struct vnode *vp, struct uio *uio, int ioflag) 363 { 364 struct lwp *l = curlwp; 365 off_t testoff; 366 367 if (uio->uio_rw != UIO_WRITE || vp->v_type != VREG) 368 return 0; 369 370 KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE); 371 if (ioflag & IO_APPEND) 372 testoff = vp->v_size; 373 else 374 testoff = uio->uio_offset; 375 376 if (testoff + uio->uio_resid > 377 l->l_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) { 378 mutex_enter(proc_lock); 379 psignal(l->l_proc, SIGXFSZ); 380 mutex_exit(proc_lock); 381 return EFBIG; 382 } 383 384 return 0; 385 } 386 387 /* 388 * Package up an I/O request on a vnode into a uio and do it. 389 */ 390 int 391 vn_rdwr(enum uio_rw rw, struct vnode *vp, void *base, int len, off_t offset, 392 enum uio_seg segflg, int ioflg, kauth_cred_t cred, size_t *aresid, 393 struct lwp *l) 394 { 395 struct uio auio; 396 struct iovec aiov; 397 int error; 398 399 if ((ioflg & IO_NODELOCKED) == 0) { 400 if (rw == UIO_READ) { 401 vn_lock(vp, LK_SHARED | LK_RETRY); 402 } else /* UIO_WRITE */ { 403 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 404 } 405 } 406 auio.uio_iov = &aiov; 407 auio.uio_iovcnt = 1; 408 aiov.iov_base = base; 409 aiov.iov_len = len; 410 auio.uio_resid = len; 411 auio.uio_offset = offset; 412 auio.uio_rw = rw; 413 if (segflg == UIO_SYSSPACE) { 414 UIO_SETUP_SYSSPACE(&auio); 415 } else { 416 auio.uio_vmspace = l->l_proc->p_vmspace; 417 } 418 419 if ((error = enforce_rlimit_fsize(vp, &auio, ioflg)) != 0) 420 goto out; 421 422 if (rw == UIO_READ) { 423 error = VOP_READ(vp, &auio, ioflg, cred); 424 } else { 425 error = VOP_WRITE(vp, &auio, ioflg, cred); 426 } 427 428 if (aresid) 429 *aresid = auio.uio_resid; 430 else 431 if (auio.uio_resid && error == 0) 432 error = EIO; 433 434 out: 435 if ((ioflg & IO_NODELOCKED) == 0) { 436 VOP_UNLOCK(vp); 437 } 438 return (error); 439 } 440 441 int 442 vn_readdir(file_t *fp, char *bf, int segflg, u_int count, int *done, 443 struct lwp *l, off_t **cookies, int *ncookies) 444 { 445 struct vnode *vp = (struct vnode *)fp->f_data; 446 struct iovec aiov; 447 struct uio auio; 448 int error, eofflag; 449 450 /* Limit the size on any kernel buffers used by VOP_READDIR */ 451 count = min(MAXBSIZE, count); 452 453 unionread: 454 if (vp->v_type != VDIR) 455 return (EINVAL); 456 aiov.iov_base = bf; 457 aiov.iov_len = count; 458 auio.uio_iov = &aiov; 459 auio.uio_iovcnt = 1; 460 auio.uio_rw = UIO_READ; 461 if (segflg == UIO_SYSSPACE) { 462 UIO_SETUP_SYSSPACE(&auio); 463 } else { 464 KASSERT(l == curlwp); 465 auio.uio_vmspace = l->l_proc->p_vmspace; 466 } 467 auio.uio_resid = count; 468 vn_lock(vp, LK_SHARED | LK_RETRY); 469 auio.uio_offset = fp->f_offset; 470 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookies, 471 ncookies); 472 mutex_enter(&fp->f_lock); 473 fp->f_offset = auio.uio_offset; 474 mutex_exit(&fp->f_lock); 475 VOP_UNLOCK(vp); 476 if (error) 477 return (error); 478 479 if (count == auio.uio_resid && vn_union_readdir_hook) { 480 struct vnode *ovp = vp; 481 482 error = (*vn_union_readdir_hook)(&vp, fp, l); 483 if (error) 484 return (error); 485 if (vp != ovp) 486 goto unionread; 487 } 488 489 if (count == auio.uio_resid && (vp->v_vflag & VV_ROOT) && 490 (vp->v_mount->mnt_flag & MNT_UNION)) { 491 struct vnode *tvp = vp; 492 vp = vp->v_mount->mnt_vnodecovered; 493 vref(vp); 494 mutex_enter(&fp->f_lock); 495 fp->f_data = vp; 496 fp->f_offset = 0; 497 mutex_exit(&fp->f_lock); 498 vrele(tvp); 499 goto unionread; 500 } 501 *done = count - auio.uio_resid; 502 return error; 503 } 504 505 /* 506 * File table vnode read routine. 507 */ 508 static int 509 vn_read(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred, 510 int flags) 511 { 512 struct vnode *vp = (struct vnode *)fp->f_data; 513 int count, error, ioflag, fflag; 514 515 ioflag = IO_ADV_ENCODE(fp->f_advice); 516 fflag = fp->f_flag; 517 if (fflag & FNONBLOCK) 518 ioflag |= IO_NDELAY; 519 if ((fflag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC)) 520 ioflag |= IO_SYNC; 521 if (fflag & FALTIO) 522 ioflag |= IO_ALTSEMANTICS; 523 if (fflag & FDIRECT) 524 ioflag |= IO_DIRECT; 525 vn_lock(vp, LK_SHARED | LK_RETRY); 526 uio->uio_offset = *offset; 527 count = uio->uio_resid; 528 error = VOP_READ(vp, uio, ioflag, cred); 529 if (flags & FOF_UPDATE_OFFSET) 530 *offset += count - uio->uio_resid; 531 VOP_UNLOCK(vp); 532 return (error); 533 } 534 535 /* 536 * File table vnode write routine. 537 */ 538 static int 539 vn_write(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred, 540 int flags) 541 { 542 struct vnode *vp = (struct vnode *)fp->f_data; 543 int count, error, ioflag, fflag; 544 545 ioflag = IO_ADV_ENCODE(fp->f_advice) | IO_UNIT; 546 fflag = fp->f_flag; 547 if (vp->v_type == VREG && (fflag & O_APPEND)) 548 ioflag |= IO_APPEND; 549 if (fflag & FNONBLOCK) 550 ioflag |= IO_NDELAY; 551 if (fflag & FFSYNC || 552 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))) 553 ioflag |= IO_SYNC; 554 else if (fflag & FDSYNC) 555 ioflag |= IO_DSYNC; 556 if (fflag & FALTIO) 557 ioflag |= IO_ALTSEMANTICS; 558 if (fflag & FDIRECT) 559 ioflag |= IO_DIRECT; 560 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 561 uio->uio_offset = *offset; 562 count = uio->uio_resid; 563 564 if ((error = enforce_rlimit_fsize(vp, uio, ioflag)) != 0) 565 goto out; 566 567 error = VOP_WRITE(vp, uio, ioflag, cred); 568 569 if (flags & FOF_UPDATE_OFFSET) { 570 if (ioflag & IO_APPEND) { 571 /* 572 * SUSv3 describes behaviour for count = 0 as following: 573 * "Before any action ... is taken, and if nbyte is zero 574 * and the file is a regular file, the write() function 575 * ... in the absence of errors ... shall return zero 576 * and have no other results." 577 */ 578 if (count) 579 *offset = uio->uio_offset; 580 } else 581 *offset += count - uio->uio_resid; 582 } 583 584 out: 585 VOP_UNLOCK(vp); 586 return (error); 587 } 588 589 /* 590 * File table vnode stat routine. 591 */ 592 static int 593 vn_statfile(file_t *fp, struct stat *sb) 594 { 595 struct vnode *vp = fp->f_data; 596 int error; 597 598 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 599 error = vn_stat(vp, sb); 600 VOP_UNLOCK(vp); 601 return error; 602 } 603 604 int 605 vn_stat(struct vnode *vp, struct stat *sb) 606 { 607 struct vattr va; 608 int error; 609 mode_t mode; 610 611 memset(&va, 0, sizeof(va)); 612 error = VOP_GETATTR(vp, &va, kauth_cred_get()); 613 if (error) 614 return (error); 615 /* 616 * Copy from vattr table 617 */ 618 memset(sb, 0, sizeof(*sb)); 619 sb->st_dev = va.va_fsid; 620 sb->st_ino = va.va_fileid; 621 mode = va.va_mode; 622 switch (vp->v_type) { 623 case VREG: 624 mode |= S_IFREG; 625 break; 626 case VDIR: 627 mode |= S_IFDIR; 628 break; 629 case VBLK: 630 mode |= S_IFBLK; 631 break; 632 case VCHR: 633 mode |= S_IFCHR; 634 break; 635 case VLNK: 636 mode |= S_IFLNK; 637 break; 638 case VSOCK: 639 mode |= S_IFSOCK; 640 break; 641 case VFIFO: 642 mode |= S_IFIFO; 643 break; 644 default: 645 return (EBADF); 646 }; 647 sb->st_mode = mode; 648 sb->st_nlink = va.va_nlink; 649 sb->st_uid = va.va_uid; 650 sb->st_gid = va.va_gid; 651 sb->st_rdev = va.va_rdev; 652 sb->st_size = va.va_size; 653 sb->st_atimespec = va.va_atime; 654 sb->st_mtimespec = va.va_mtime; 655 sb->st_ctimespec = va.va_ctime; 656 sb->st_birthtimespec = va.va_birthtime; 657 sb->st_blksize = va.va_blocksize; 658 sb->st_flags = va.va_flags; 659 sb->st_gen = 0; 660 sb->st_blocks = va.va_bytes / S_BLKSIZE; 661 return (0); 662 } 663 664 /* 665 * File table vnode fcntl routine. 666 */ 667 static int 668 vn_fcntl(file_t *fp, u_int com, void *data) 669 { 670 struct vnode *vp = fp->f_data; 671 int error; 672 673 error = VOP_FCNTL(vp, com, data, fp->f_flag, kauth_cred_get()); 674 return (error); 675 } 676 677 /* 678 * File table vnode ioctl routine. 679 */ 680 static int 681 vn_ioctl(file_t *fp, u_long com, void *data) 682 { 683 struct vnode *vp = fp->f_data, *ovp; 684 struct vattr vattr; 685 int error; 686 687 switch (vp->v_type) { 688 689 case VREG: 690 case VDIR: 691 if (com == FIONREAD) { 692 vn_lock(vp, LK_SHARED | LK_RETRY); 693 error = VOP_GETATTR(vp, &vattr, kauth_cred_get()); 694 VOP_UNLOCK(vp); 695 if (error) 696 return (error); 697 *(int *)data = vattr.va_size - fp->f_offset; 698 return (0); 699 } 700 if ((com == FIONWRITE) || (com == FIONSPACE)) { 701 /* 702 * Files don't have send queues, so there never 703 * are any bytes in them, nor is there any 704 * open space in them. 705 */ 706 *(int *)data = 0; 707 return (0); 708 } 709 if (com == FIOGETBMAP) { 710 daddr_t *block; 711 712 if (*(daddr_t *)data < 0) 713 return (EINVAL); 714 block = (daddr_t *)data; 715 return (VOP_BMAP(vp, *block, NULL, block, NULL)); 716 } 717 if (com == OFIOGETBMAP) { 718 daddr_t ibn, obn; 719 720 if (*(int32_t *)data < 0) 721 return (EINVAL); 722 ibn = (daddr_t)*(int32_t *)data; 723 error = VOP_BMAP(vp, ibn, NULL, &obn, NULL); 724 *(int32_t *)data = (int32_t)obn; 725 return error; 726 } 727 if (com == FIONBIO || com == FIOASYNC) /* XXX */ 728 return (0); /* XXX */ 729 /* fall into ... */ 730 case VFIFO: 731 case VCHR: 732 case VBLK: 733 error = VOP_IOCTL(vp, com, data, fp->f_flag, 734 kauth_cred_get()); 735 if (error == 0 && com == TIOCSCTTY) { 736 vref(vp); 737 mutex_enter(proc_lock); 738 ovp = curproc->p_session->s_ttyvp; 739 curproc->p_session->s_ttyvp = vp; 740 mutex_exit(proc_lock); 741 if (ovp != NULL) 742 vrele(ovp); 743 } 744 return (error); 745 746 default: 747 return (EPASSTHROUGH); 748 } 749 } 750 751 /* 752 * File table vnode poll routine. 753 */ 754 static int 755 vn_poll(file_t *fp, int events) 756 { 757 758 return (VOP_POLL(fp->f_data, events)); 759 } 760 761 /* 762 * File table vnode kqfilter routine. 763 */ 764 int 765 vn_kqfilter(file_t *fp, struct knote *kn) 766 { 767 768 return (VOP_KQFILTER(fp->f_data, kn)); 769 } 770 771 /* 772 * Check that the vnode is still valid, and if so 773 * acquire requested lock. 774 */ 775 int 776 vn_lock(struct vnode *vp, int flags) 777 { 778 int error; 779 780 #if 0 781 KASSERT(vp->v_usecount > 0 || (vp->v_iflag & VI_ONWORKLST) != 0); 782 #endif 783 KASSERT((flags & ~(LK_SHARED|LK_EXCLUSIVE|LK_NOWAIT|LK_RETRY)) == 0); 784 KASSERT(!mutex_owned(vp->v_interlock)); 785 786 #ifdef DIAGNOSTIC 787 if (wapbl_vphaswapbl(vp)) 788 WAPBL_JUNLOCK_ASSERT(wapbl_vptomp(vp)); 789 #endif 790 791 do { 792 /* 793 * XXX PR 37706 forced unmount of file systems is unsafe. 794 * Race between vclean() and this the remaining problem. 795 */ 796 mutex_enter(vp->v_interlock); 797 if (vp->v_iflag & VI_XLOCK) { 798 if (flags & LK_NOWAIT) { 799 mutex_exit(vp->v_interlock); 800 return EBUSY; 801 } 802 vwait(vp, VI_XLOCK); 803 mutex_exit(vp->v_interlock); 804 error = ENOENT; 805 } else { 806 mutex_exit(vp->v_interlock); 807 error = VOP_LOCK(vp, (flags & ~LK_RETRY)); 808 if (error == 0 || error == EDEADLK || error == EBUSY) 809 return (error); 810 } 811 } while (flags & LK_RETRY); 812 return (error); 813 } 814 815 /* 816 * File table vnode close routine. 817 */ 818 static int 819 vn_closefile(file_t *fp) 820 { 821 822 return vn_close(fp->f_data, fp->f_flag, fp->f_cred); 823 } 824 825 /* 826 * Simplified in-kernel wrapper calls for extended attribute access. 827 * Both calls pass in a NULL credential, authorizing a "kernel" access. 828 * Set IO_NODELOCKED in ioflg if the vnode is already locked. 829 */ 830 int 831 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace, 832 const char *attrname, size_t *buflen, void *bf, struct lwp *l) 833 { 834 struct uio auio; 835 struct iovec aiov; 836 int error; 837 838 aiov.iov_len = *buflen; 839 aiov.iov_base = bf; 840 841 auio.uio_iov = &aiov; 842 auio.uio_iovcnt = 1; 843 auio.uio_rw = UIO_READ; 844 auio.uio_offset = 0; 845 auio.uio_resid = *buflen; 846 UIO_SETUP_SYSSPACE(&auio); 847 848 if ((ioflg & IO_NODELOCKED) == 0) 849 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 850 851 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL); 852 853 if ((ioflg & IO_NODELOCKED) == 0) 854 VOP_UNLOCK(vp); 855 856 if (error == 0) 857 *buflen = *buflen - auio.uio_resid; 858 859 return (error); 860 } 861 862 /* 863 * XXX Failure mode if partially written? 864 */ 865 int 866 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace, 867 const char *attrname, size_t buflen, const void *bf, struct lwp *l) 868 { 869 struct uio auio; 870 struct iovec aiov; 871 int error; 872 873 aiov.iov_len = buflen; 874 aiov.iov_base = __UNCONST(bf); /* XXXUNCONST kills const */ 875 876 auio.uio_iov = &aiov; 877 auio.uio_iovcnt = 1; 878 auio.uio_rw = UIO_WRITE; 879 auio.uio_offset = 0; 880 auio.uio_resid = buflen; 881 UIO_SETUP_SYSSPACE(&auio); 882 883 if ((ioflg & IO_NODELOCKED) == 0) { 884 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 885 } 886 887 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL); 888 889 if ((ioflg & IO_NODELOCKED) == 0) { 890 VOP_UNLOCK(vp); 891 } 892 893 return (error); 894 } 895 896 int 897 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace, 898 const char *attrname, struct lwp *l) 899 { 900 int error; 901 902 if ((ioflg & IO_NODELOCKED) == 0) { 903 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 904 } 905 906 error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL); 907 if (error == EOPNOTSUPP) 908 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL, NULL); 909 910 if ((ioflg & IO_NODELOCKED) == 0) { 911 VOP_UNLOCK(vp); 912 } 913 914 return (error); 915 } 916 917 void 918 vn_ra_allocctx(struct vnode *vp) 919 { 920 struct uvm_ractx *ra = NULL; 921 922 KASSERT(mutex_owned(vp->v_interlock)); 923 924 if (vp->v_type != VREG) { 925 return; 926 } 927 if (vp->v_ractx != NULL) { 928 return; 929 } 930 if (vp->v_ractx == NULL) { 931 mutex_exit(vp->v_interlock); 932 ra = uvm_ra_allocctx(); 933 mutex_enter(vp->v_interlock); 934 if (ra != NULL && vp->v_ractx == NULL) { 935 vp->v_ractx = ra; 936 ra = NULL; 937 } 938 } 939 if (ra != NULL) { 940 uvm_ra_freectx(ra); 941 } 942 } 943 944 int 945 vn_fifo_bypass(void *v) 946 { 947 struct vop_generic_args *ap = v; 948 949 return VOCALL(fifo_vnodeop_p, ap->a_desc->vdesc_offset, v); 950 } 951