vfs_vnops.c revision 1.173
1/* $NetBSD: vfs_vnops.c,v 1.173 2010/06/18 16:29:02 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.173 2010/06/18 16:29:02 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 103int (*vn_union_readdir_hook) (struct vnode **, struct file *, struct lwp *); 104 105#include <sys/verified_exec.h> 106 107static int vn_read(file_t *fp, off_t *offset, struct uio *uio, 108 kauth_cred_t cred, int flags); 109static int vn_write(file_t *fp, off_t *offset, struct uio *uio, 110 kauth_cred_t cred, int flags); 111static int vn_closefile(file_t *fp); 112static int vn_poll(file_t *fp, int events); 113static int vn_fcntl(file_t *fp, u_int com, void *data); 114static int vn_statfile(file_t *fp, struct stat *sb); 115static int vn_ioctl(file_t *fp, u_long com, void *data); 116 117const 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 */ 133int 134vn_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 char *path; 142 143 ndp->ni_cnd.cn_flags &= TRYEMULROOT | NOCHROOT; 144 145 if (fmode & O_CREAT) { 146 ndp->ni_cnd.cn_nameiop = CREATE; 147 ndp->ni_cnd.cn_flags |= LOCKPARENT | LOCKLEAF; 148 if ((fmode & O_EXCL) == 0 && 149 ((fmode & O_NOFOLLOW) == 0)) 150 ndp->ni_cnd.cn_flags |= FOLLOW; 151 } else { 152 ndp->ni_cnd.cn_nameiop = LOOKUP; 153 ndp->ni_cnd.cn_flags |= LOCKLEAF; 154 if ((fmode & O_NOFOLLOW) == 0) 155 ndp->ni_cnd.cn_flags |= FOLLOW; 156 } 157 158 VERIEXEC_PATH_GET(ndp->ni_dirp, ndp->ni_segflg, ndp->ni_dirp, path); 159 160 error = namei(ndp); 161 if (error) 162 goto out; 163 164 vp = ndp->ni_vp; 165 166#if NVERIEXEC > 0 167 error = veriexec_openchk(l, ndp->ni_vp, ndp->ni_dirp, fmode); 168 if (error) 169 goto bad; 170#endif /* NVERIEXEC > 0 */ 171 172 if (fmode & O_CREAT) { 173 if (ndp->ni_vp == NULL) { 174 vattr_null(&va); 175 va.va_type = VREG; 176 va.va_mode = cmode; 177 if (fmode & O_EXCL) 178 va.va_vaflags |= VA_EXCLUSIVE; 179 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp, 180 &ndp->ni_cnd, &va); 181 if (error) 182 goto out; 183 fmode &= ~O_TRUNC; 184 vp = ndp->ni_vp; 185 } else { 186 VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd); 187 if (ndp->ni_dvp == ndp->ni_vp) 188 vrele(ndp->ni_dvp); 189 else 190 vput(ndp->ni_dvp); 191 ndp->ni_dvp = NULL; 192 vp = ndp->ni_vp; 193 if (fmode & O_EXCL) { 194 error = EEXIST; 195 goto bad; 196 } 197 fmode &= ~O_CREAT; 198 } 199 } else { 200 vp = ndp->ni_vp; 201 } 202 if (vp->v_type == VSOCK) { 203 error = EOPNOTSUPP; 204 goto bad; 205 } 206 if (ndp->ni_vp->v_type == VLNK) { 207 error = EFTYPE; 208 goto bad; 209 } 210 211 if ((fmode & O_CREAT) == 0) { 212 error = vn_openchk(vp, cred, fmode); 213 if (error != 0) 214 goto bad; 215 } 216 217 if (fmode & O_TRUNC) { 218 VOP_UNLOCK(vp, 0); /* XXX */ 219 220 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */ 221 vattr_null(&va); 222 va.va_size = 0; 223 error = VOP_SETATTR(vp, &va, cred); 224 if (error != 0) 225 goto bad; 226 } 227 if ((error = VOP_OPEN(vp, fmode, cred)) != 0) 228 goto bad; 229 if (fmode & FWRITE) { 230 mutex_enter(&vp->v_interlock); 231 vp->v_writecount++; 232 mutex_exit(&vp->v_interlock); 233 } 234 235bad: 236 if (error) 237 vput(vp); 238out: 239 VERIEXEC_PATH_PUT(path); 240 return (error); 241} 242 243/* 244 * Check for write permissions on the specified vnode. 245 * Prototype text segments cannot be written. 246 */ 247int 248vn_writechk(struct vnode *vp) 249{ 250 251 /* 252 * If the vnode is in use as a process's text, 253 * we can't allow writing. 254 */ 255 if (vp->v_iflag & VI_TEXT) 256 return (ETXTBSY); 257 return (0); 258} 259 260int 261vn_openchk(struct vnode *vp, kauth_cred_t cred, int fflags) 262{ 263 int permbits = 0; 264 int error; 265 266 if ((fflags & FREAD) != 0) { 267 permbits = VREAD; 268 } 269 if ((fflags & (FWRITE | O_TRUNC)) != 0) { 270 permbits |= VWRITE; 271 if (vp->v_type == VDIR) { 272 error = EISDIR; 273 goto bad; 274 } 275 error = vn_writechk(vp); 276 if (error != 0) 277 goto bad; 278 } 279 error = VOP_ACCESS(vp, permbits, cred); 280bad: 281 return error; 282} 283 284/* 285 * Mark a vnode as having executable mappings. 286 */ 287void 288vn_markexec(struct vnode *vp) 289{ 290 291 if ((vp->v_iflag & VI_EXECMAP) != 0) { 292 /* Safe unlocked, as long as caller holds a reference. */ 293 return; 294 } 295 296 mutex_enter(&vp->v_interlock); 297 if ((vp->v_iflag & VI_EXECMAP) == 0) { 298 atomic_add_int(&uvmexp.filepages, -vp->v_uobj.uo_npages); 299 atomic_add_int(&uvmexp.execpages, vp->v_uobj.uo_npages); 300 vp->v_iflag |= VI_EXECMAP; 301 } 302 mutex_exit(&vp->v_interlock); 303} 304 305/* 306 * Mark a vnode as being the text of a process. 307 * Fail if the vnode is currently writable. 308 */ 309int 310vn_marktext(struct vnode *vp) 311{ 312 313 if ((vp->v_iflag & (VI_TEXT|VI_EXECMAP)) == (VI_TEXT|VI_EXECMAP)) { 314 /* Safe unlocked, as long as caller holds a reference. */ 315 return (0); 316 } 317 318 mutex_enter(&vp->v_interlock); 319 if (vp->v_writecount != 0) { 320 KASSERT((vp->v_iflag & VI_TEXT) == 0); 321 mutex_exit(&vp->v_interlock); 322 return (ETXTBSY); 323 } 324 if ((vp->v_iflag & VI_EXECMAP) == 0) { 325 atomic_add_int(&uvmexp.filepages, -vp->v_uobj.uo_npages); 326 atomic_add_int(&uvmexp.execpages, vp->v_uobj.uo_npages); 327 } 328 vp->v_iflag |= (VI_TEXT | VI_EXECMAP); 329 mutex_exit(&vp->v_interlock); 330 return (0); 331} 332 333/* 334 * Vnode close call 335 * 336 * Note: takes an unlocked vnode, while VOP_CLOSE takes a locked node. 337 */ 338int 339vn_close(struct vnode *vp, int flags, kauth_cred_t cred) 340{ 341 int error; 342 343 if (flags & FWRITE) { 344 mutex_enter(&vp->v_interlock); 345 vp->v_writecount--; 346 mutex_exit(&vp->v_interlock); 347 } 348 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 349 error = VOP_CLOSE(vp, flags, cred); 350 vput(vp); 351 return (error); 352} 353 354static int 355enforce_rlimit_fsize(struct vnode *vp, struct uio *uio, int ioflag) 356{ 357 struct lwp *l = curlwp; 358 off_t testoff; 359 360 if (uio->uio_rw != UIO_WRITE || vp->v_type != VREG) 361 return 0; 362 363 KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE); 364 if (ioflag & IO_APPEND) 365 testoff = vp->v_size; 366 else 367 testoff = uio->uio_offset; 368 369 if (testoff + uio->uio_resid > 370 l->l_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) { 371 mutex_enter(proc_lock); 372 psignal(l->l_proc, SIGXFSZ); 373 mutex_exit(proc_lock); 374 return EFBIG; 375 } 376 377 return 0; 378} 379 380/* 381 * Package up an I/O request on a vnode into a uio and do it. 382 */ 383int 384vn_rdwr(enum uio_rw rw, struct vnode *vp, void *base, int len, off_t offset, 385 enum uio_seg segflg, int ioflg, kauth_cred_t cred, size_t *aresid, 386 struct lwp *l) 387{ 388 struct uio auio; 389 struct iovec aiov; 390 int error; 391 392 if ((ioflg & IO_NODELOCKED) == 0) { 393 if (rw == UIO_READ) { 394 vn_lock(vp, LK_SHARED | LK_RETRY); 395 } else /* UIO_WRITE */ { 396 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 397 } 398 } 399 auio.uio_iov = &aiov; 400 auio.uio_iovcnt = 1; 401 aiov.iov_base = base; 402 aiov.iov_len = len; 403 auio.uio_resid = len; 404 auio.uio_offset = offset; 405 auio.uio_rw = rw; 406 if (segflg == UIO_SYSSPACE) { 407 UIO_SETUP_SYSSPACE(&auio); 408 } else { 409 auio.uio_vmspace = l->l_proc->p_vmspace; 410 } 411 412 if ((error = enforce_rlimit_fsize(vp, &auio, ioflg)) != 0) 413 goto out; 414 415 if (rw == UIO_READ) { 416 error = VOP_READ(vp, &auio, ioflg, cred); 417 } else { 418 error = VOP_WRITE(vp, &auio, ioflg, cred); 419 } 420 421 if (aresid) 422 *aresid = auio.uio_resid; 423 else 424 if (auio.uio_resid && error == 0) 425 error = EIO; 426 427 out: 428 if ((ioflg & IO_NODELOCKED) == 0) { 429 VOP_UNLOCK(vp, 0); 430 } 431 return (error); 432} 433 434int 435vn_readdir(file_t *fp, char *bf, int segflg, u_int count, int *done, 436 struct lwp *l, off_t **cookies, int *ncookies) 437{ 438 struct vnode *vp = (struct vnode *)fp->f_data; 439 struct iovec aiov; 440 struct uio auio; 441 int error, eofflag; 442 443 /* Limit the size on any kernel buffers used by VOP_READDIR */ 444 count = min(MAXBSIZE, count); 445 446unionread: 447 if (vp->v_type != VDIR) 448 return (EINVAL); 449 aiov.iov_base = bf; 450 aiov.iov_len = count; 451 auio.uio_iov = &aiov; 452 auio.uio_iovcnt = 1; 453 auio.uio_rw = UIO_READ; 454 if (segflg == UIO_SYSSPACE) { 455 UIO_SETUP_SYSSPACE(&auio); 456 } else { 457 KASSERT(l == curlwp); 458 auio.uio_vmspace = l->l_proc->p_vmspace; 459 } 460 auio.uio_resid = count; 461 vn_lock(vp, LK_SHARED | LK_RETRY); 462 auio.uio_offset = fp->f_offset; 463 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookies, 464 ncookies); 465 mutex_enter(&fp->f_lock); 466 fp->f_offset = auio.uio_offset; 467 mutex_exit(&fp->f_lock); 468 VOP_UNLOCK(vp, 0); 469 if (error) 470 return (error); 471 472 if (count == auio.uio_resid && vn_union_readdir_hook) { 473 struct vnode *ovp = vp; 474 475 error = (*vn_union_readdir_hook)(&vp, fp, l); 476 if (error) 477 return (error); 478 if (vp != ovp) 479 goto unionread; 480 } 481 482 if (count == auio.uio_resid && (vp->v_vflag & VV_ROOT) && 483 (vp->v_mount->mnt_flag & MNT_UNION)) { 484 struct vnode *tvp = vp; 485 vp = vp->v_mount->mnt_vnodecovered; 486 vref(vp); 487 mutex_enter(&fp->f_lock); 488 fp->f_data = vp; 489 fp->f_offset = 0; 490 mutex_exit(&fp->f_lock); 491 vrele(tvp); 492 goto unionread; 493 } 494 *done = count - auio.uio_resid; 495 return error; 496} 497 498/* 499 * File table vnode read routine. 500 */ 501static int 502vn_read(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred, 503 int flags) 504{ 505 struct vnode *vp = (struct vnode *)fp->f_data; 506 int count, error, ioflag, fflag; 507 508 ioflag = IO_ADV_ENCODE(fp->f_advice); 509 fflag = fp->f_flag; 510 if (fflag & FNONBLOCK) 511 ioflag |= IO_NDELAY; 512 if ((fflag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC)) 513 ioflag |= IO_SYNC; 514 if (fflag & FALTIO) 515 ioflag |= IO_ALTSEMANTICS; 516 if (fflag & FDIRECT) 517 ioflag |= IO_DIRECT; 518 vn_lock(vp, LK_SHARED | LK_RETRY); 519 uio->uio_offset = *offset; 520 count = uio->uio_resid; 521 error = VOP_READ(vp, uio, ioflag, cred); 522 if (flags & FOF_UPDATE_OFFSET) 523 *offset += count - uio->uio_resid; 524 VOP_UNLOCK(vp, 0); 525 return (error); 526} 527 528/* 529 * File table vnode write routine. 530 */ 531static int 532vn_write(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred, 533 int flags) 534{ 535 struct vnode *vp = (struct vnode *)fp->f_data; 536 int count, error, ioflag, fflag; 537 538 ioflag = IO_ADV_ENCODE(fp->f_advice) | IO_UNIT; 539 fflag = fp->f_flag; 540 if (vp->v_type == VREG && (fflag & O_APPEND)) 541 ioflag |= IO_APPEND; 542 if (fflag & FNONBLOCK) 543 ioflag |= IO_NDELAY; 544 if (fflag & FFSYNC || 545 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))) 546 ioflag |= IO_SYNC; 547 else if (fflag & FDSYNC) 548 ioflag |= IO_DSYNC; 549 if (fflag & FALTIO) 550 ioflag |= IO_ALTSEMANTICS; 551 if (fflag & FDIRECT) 552 ioflag |= IO_DIRECT; 553 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 554 uio->uio_offset = *offset; 555 count = uio->uio_resid; 556 557 if ((error = enforce_rlimit_fsize(vp, uio, ioflag)) != 0) 558 goto out; 559 560 error = VOP_WRITE(vp, uio, ioflag, cred); 561 562 if (flags & FOF_UPDATE_OFFSET) { 563 if (ioflag & IO_APPEND) { 564 /* 565 * SUSv3 describes behaviour for count = 0 as following: 566 * "Before any action ... is taken, and if nbyte is zero 567 * and the file is a regular file, the write() function 568 * ... in the absence of errors ... shall return zero 569 * and have no other results." 570 */ 571 if (count) 572 *offset = uio->uio_offset; 573 } else 574 *offset += count - uio->uio_resid; 575 } 576 577 out: 578 VOP_UNLOCK(vp, 0); 579 return (error); 580} 581 582/* 583 * File table vnode stat routine. 584 */ 585static int 586vn_statfile(file_t *fp, struct stat *sb) 587{ 588 struct vnode *vp = fp->f_data; 589 int error; 590 591 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 592 error = vn_stat(vp, sb); 593 VOP_UNLOCK(vp, 0); 594 return error; 595} 596 597int 598vn_stat(struct vnode *vp, struct stat *sb) 599{ 600 struct vattr va; 601 int error; 602 mode_t mode; 603 604 error = VOP_GETATTR(vp, &va, kauth_cred_get()); 605 if (error) 606 return (error); 607 /* 608 * Copy from vattr table 609 */ 610 sb->st_dev = va.va_fsid; 611 sb->st_ino = va.va_fileid; 612 mode = va.va_mode; 613 switch (vp->v_type) { 614 case VREG: 615 mode |= S_IFREG; 616 break; 617 case VDIR: 618 mode |= S_IFDIR; 619 break; 620 case VBLK: 621 mode |= S_IFBLK; 622 break; 623 case VCHR: 624 mode |= S_IFCHR; 625 break; 626 case VLNK: 627 mode |= S_IFLNK; 628 break; 629 case VSOCK: 630 mode |= S_IFSOCK; 631 break; 632 case VFIFO: 633 mode |= S_IFIFO; 634 break; 635 default: 636 return (EBADF); 637 }; 638 sb->st_mode = mode; 639 sb->st_nlink = va.va_nlink; 640 sb->st_uid = va.va_uid; 641 sb->st_gid = va.va_gid; 642 sb->st_rdev = va.va_rdev; 643 sb->st_size = va.va_size; 644 sb->st_atimespec = va.va_atime; 645 sb->st_mtimespec = va.va_mtime; 646 sb->st_ctimespec = va.va_ctime; 647 sb->st_birthtimespec = va.va_birthtime; 648 sb->st_blksize = va.va_blocksize; 649 sb->st_flags = va.va_flags; 650 sb->st_gen = 0; 651 sb->st_blocks = va.va_bytes / S_BLKSIZE; 652 return (0); 653} 654 655/* 656 * File table vnode fcntl routine. 657 */ 658static int 659vn_fcntl(file_t *fp, u_int com, void *data) 660{ 661 struct vnode *vp = fp->f_data; 662 int error; 663 664 error = VOP_FCNTL(vp, com, data, fp->f_flag, kauth_cred_get()); 665 return (error); 666} 667 668/* 669 * File table vnode ioctl routine. 670 */ 671static int 672vn_ioctl(file_t *fp, u_long com, void *data) 673{ 674 struct vnode *vp = fp->f_data, *ovp; 675 struct vattr vattr; 676 int error; 677 678 switch (vp->v_type) { 679 680 case VREG: 681 case VDIR: 682 if (com == FIONREAD) { 683 error = VOP_GETATTR(vp, &vattr, 684 kauth_cred_get()); 685 if (error) 686 return (error); 687 *(int *)data = vattr.va_size - fp->f_offset; 688 return (0); 689 } 690 if ((com == FIONWRITE) || (com == FIONSPACE)) { 691 /* 692 * Files don't have send queues, so there never 693 * are any bytes in them, nor is there any 694 * open space in them. 695 */ 696 *(int *)data = 0; 697 return (0); 698 } 699 if (com == FIOGETBMAP) { 700 daddr_t *block; 701 702 if (*(daddr_t *)data < 0) 703 return (EINVAL); 704 block = (daddr_t *)data; 705 return (VOP_BMAP(vp, *block, NULL, block, NULL)); 706 } 707 if (com == OFIOGETBMAP) { 708 daddr_t ibn, obn; 709 710 if (*(int32_t *)data < 0) 711 return (EINVAL); 712 ibn = (daddr_t)*(int32_t *)data; 713 error = VOP_BMAP(vp, ibn, NULL, &obn, NULL); 714 *(int32_t *)data = (int32_t)obn; 715 return error; 716 } 717 if (com == FIONBIO || com == FIOASYNC) /* XXX */ 718 return (0); /* XXX */ 719 /* fall into ... */ 720 case VFIFO: 721 case VCHR: 722 case VBLK: 723 error = VOP_IOCTL(vp, com, data, fp->f_flag, 724 kauth_cred_get()); 725 if (error == 0 && com == TIOCSCTTY) { 726 vref(vp); 727 mutex_enter(proc_lock); 728 ovp = curproc->p_session->s_ttyvp; 729 curproc->p_session->s_ttyvp = vp; 730 mutex_exit(proc_lock); 731 if (ovp != NULL) 732 vrele(ovp); 733 } 734 return (error); 735 736 default: 737 return (EPASSTHROUGH); 738 } 739} 740 741/* 742 * File table vnode poll routine. 743 */ 744static int 745vn_poll(file_t *fp, int events) 746{ 747 748 return (VOP_POLL(fp->f_data, events)); 749} 750 751/* 752 * File table vnode kqfilter routine. 753 */ 754int 755vn_kqfilter(file_t *fp, struct knote *kn) 756{ 757 758 return (VOP_KQFILTER(fp->f_data, kn)); 759} 760 761/* 762 * Check that the vnode is still valid, and if so 763 * acquire requested lock. 764 */ 765int 766vn_lock(struct vnode *vp, int flags) 767{ 768 int error; 769 770#if 0 771 KASSERT(vp->v_usecount > 0 || (flags & LK_INTERLOCK) != 0 772 || (vp->v_iflag & VI_ONWORKLST) != 0); 773#endif 774 KASSERT((flags & 775 ~(LK_INTERLOCK|LK_SHARED|LK_EXCLUSIVE|LK_NOWAIT|LK_RETRY)) 776 == 0); 777 778#ifdef DIAGNOSTIC 779 if (wapbl_vphaswapbl(vp)) 780 WAPBL_JUNLOCK_ASSERT(wapbl_vptomp(vp)); 781#endif 782 783 do { 784 /* 785 * XXX PR 37706 forced unmount of file systems is unsafe. 786 * Race between vclean() and this the remaining problem. 787 */ 788 if (vp->v_iflag & VI_XLOCK) { 789 if ((flags & LK_INTERLOCK) == 0) { 790 mutex_enter(&vp->v_interlock); 791 } 792 flags &= ~LK_INTERLOCK; 793 if (flags & LK_NOWAIT) { 794 mutex_exit(&vp->v_interlock); 795 return EBUSY; 796 } 797 vwait(vp, VI_XLOCK); 798 mutex_exit(&vp->v_interlock); 799 error = ENOENT; 800 } else { 801 if ((flags & LK_INTERLOCK) != 0) { 802 mutex_exit(&vp->v_interlock); 803 } 804 flags &= ~LK_INTERLOCK; 805 error = VOP_LOCK(vp, (flags & ~LK_RETRY)); 806 if (error == 0 || error == EDEADLK || error == EBUSY) 807 return (error); 808 } 809 } while (flags & LK_RETRY); 810 return (error); 811} 812 813/* 814 * File table vnode close routine. 815 */ 816static int 817vn_closefile(file_t *fp) 818{ 819 820 return vn_close(fp->f_data, fp->f_flag, fp->f_cred); 821} 822 823/* 824 * Simplified in-kernel wrapper calls for extended attribute access. 825 * Both calls pass in a NULL credential, authorizing a "kernel" access. 826 * Set IO_NODELOCKED in ioflg if the vnode is already locked. 827 */ 828int 829vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace, 830 const char *attrname, size_t *buflen, void *bf, struct lwp *l) 831{ 832 struct uio auio; 833 struct iovec aiov; 834 int error; 835 836 aiov.iov_len = *buflen; 837 aiov.iov_base = bf; 838 839 auio.uio_iov = &aiov; 840 auio.uio_iovcnt = 1; 841 auio.uio_rw = UIO_READ; 842 auio.uio_offset = 0; 843 auio.uio_resid = *buflen; 844 UIO_SETUP_SYSSPACE(&auio); 845 846 if ((ioflg & IO_NODELOCKED) == 0) 847 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 848 849 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL); 850 851 if ((ioflg & IO_NODELOCKED) == 0) 852 VOP_UNLOCK(vp, 0); 853 854 if (error == 0) 855 *buflen = *buflen - auio.uio_resid; 856 857 return (error); 858} 859 860/* 861 * XXX Failure mode if partially written? 862 */ 863int 864vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace, 865 const char *attrname, size_t buflen, const void *bf, struct lwp *l) 866{ 867 struct uio auio; 868 struct iovec aiov; 869 int error; 870 871 aiov.iov_len = buflen; 872 aiov.iov_base = __UNCONST(bf); /* XXXUNCONST kills const */ 873 874 auio.uio_iov = &aiov; 875 auio.uio_iovcnt = 1; 876 auio.uio_rw = UIO_WRITE; 877 auio.uio_offset = 0; 878 auio.uio_resid = buflen; 879 UIO_SETUP_SYSSPACE(&auio); 880 881 if ((ioflg & IO_NODELOCKED) == 0) { 882 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 883 } 884 885 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL); 886 887 if ((ioflg & IO_NODELOCKED) == 0) { 888 VOP_UNLOCK(vp, 0); 889 } 890 891 return (error); 892} 893 894int 895vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace, 896 const char *attrname, struct lwp *l) 897{ 898 int error; 899 900 if ((ioflg & IO_NODELOCKED) == 0) { 901 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 902 } 903 904 error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL); 905 if (error == EOPNOTSUPP) 906 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL, NULL); 907 908 if ((ioflg & IO_NODELOCKED) == 0) { 909 VOP_UNLOCK(vp, 0); 910 } 911 912 return (error); 913} 914 915void 916vn_ra_allocctx(struct vnode *vp) 917{ 918 struct uvm_ractx *ra = NULL; 919 920 KASSERT(mutex_owned(&vp->v_interlock)); 921 922 if (vp->v_type != VREG) { 923 return; 924 } 925 if (vp->v_ractx != NULL) { 926 return; 927 } 928 if (vp->v_ractx == NULL) { 929 mutex_exit(&vp->v_interlock); 930 ra = uvm_ra_allocctx(); 931 mutex_enter(&vp->v_interlock); 932 if (ra != NULL && vp->v_ractx == NULL) { 933 vp->v_ractx = ra; 934 ra = NULL; 935 } 936 } 937 if (ra != NULL) { 938 uvm_ra_freectx(ra); 939 } 940} 941 942int 943vn_fifo_bypass(void *v) 944{ 945 struct vop_generic_args *ap = v; 946 947 return VOCALL(fifo_vnodeop_p, ap->a_desc->vdesc_offset, v); 948} 949