1 /* $NetBSD: vfs_vnops.c,v 1.170 2010/03/29 13:11:32 pooka 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.170 2010/03/29 13:11:32 pooka 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 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 235 bad: 236 if (error) 237 vput(vp); 238 out: 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 */ 247 int 248 vn_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 260 int 261 vn_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); 280 bad: 281 return error; 282 } 283 284 /* 285 * Mark a vnode as having executable mappings. 286 */ 287 void 288 vn_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 */ 309 int 310 vn_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 */ 338 int 339 vn_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 354 /* 355 * Package up an I/O request on a vnode into a uio and do it. 356 */ 357 int 358 vn_rdwr(enum uio_rw rw, struct vnode *vp, void *base, int len, off_t offset, 359 enum uio_seg segflg, int ioflg, kauth_cred_t cred, size_t *aresid, 360 struct lwp *l) 361 { 362 struct uio auio; 363 struct iovec aiov; 364 int error; 365 366 if ((ioflg & IO_NODELOCKED) == 0) { 367 if (rw == UIO_READ) { 368 vn_lock(vp, LK_SHARED | LK_RETRY); 369 } else /* UIO_WRITE */ { 370 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 371 } 372 } 373 auio.uio_iov = &aiov; 374 auio.uio_iovcnt = 1; 375 aiov.iov_base = base; 376 aiov.iov_len = len; 377 auio.uio_resid = len; 378 auio.uio_offset = offset; 379 auio.uio_rw = rw; 380 if (segflg == UIO_SYSSPACE) { 381 UIO_SETUP_SYSSPACE(&auio); 382 } else { 383 auio.uio_vmspace = l->l_proc->p_vmspace; 384 } 385 if (rw == UIO_READ) { 386 error = VOP_READ(vp, &auio, ioflg, cred); 387 } else { 388 error = VOP_WRITE(vp, &auio, ioflg, cred); 389 } 390 if (aresid) 391 *aresid = auio.uio_resid; 392 else 393 if (auio.uio_resid && error == 0) 394 error = EIO; 395 if ((ioflg & IO_NODELOCKED) == 0) { 396 VOP_UNLOCK(vp, 0); 397 } 398 return (error); 399 } 400 401 int 402 vn_readdir(file_t *fp, char *bf, int segflg, u_int count, int *done, 403 struct lwp *l, off_t **cookies, int *ncookies) 404 { 405 struct vnode *vp = (struct vnode *)fp->f_data; 406 struct iovec aiov; 407 struct uio auio; 408 int error, eofflag; 409 410 /* Limit the size on any kernel buffers used by VOP_READDIR */ 411 count = min(MAXBSIZE, count); 412 413 unionread: 414 if (vp->v_type != VDIR) 415 return (EINVAL); 416 aiov.iov_base = bf; 417 aiov.iov_len = count; 418 auio.uio_iov = &aiov; 419 auio.uio_iovcnt = 1; 420 auio.uio_rw = UIO_READ; 421 if (segflg == UIO_SYSSPACE) { 422 UIO_SETUP_SYSSPACE(&auio); 423 } else { 424 KASSERT(l == curlwp); 425 auio.uio_vmspace = l->l_proc->p_vmspace; 426 } 427 auio.uio_resid = count; 428 vn_lock(vp, LK_SHARED | LK_RETRY); 429 auio.uio_offset = fp->f_offset; 430 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookies, 431 ncookies); 432 mutex_enter(&fp->f_lock); 433 fp->f_offset = auio.uio_offset; 434 mutex_exit(&fp->f_lock); 435 VOP_UNLOCK(vp, 0); 436 if (error) 437 return (error); 438 439 if (count == auio.uio_resid && vn_union_readdir_hook) { 440 struct vnode *ovp = vp; 441 442 error = (*vn_union_readdir_hook)(&vp, fp, l); 443 if (error) 444 return (error); 445 if (vp != ovp) 446 goto unionread; 447 } 448 449 if (count == auio.uio_resid && (vp->v_vflag & VV_ROOT) && 450 (vp->v_mount->mnt_flag & MNT_UNION)) { 451 struct vnode *tvp = vp; 452 vp = vp->v_mount->mnt_vnodecovered; 453 vref(vp); 454 mutex_enter(&fp->f_lock); 455 fp->f_data = vp; 456 fp->f_offset = 0; 457 mutex_exit(&fp->f_lock); 458 vrele(tvp); 459 goto unionread; 460 } 461 *done = count - auio.uio_resid; 462 return error; 463 } 464 465 /* 466 * File table vnode read routine. 467 */ 468 static int 469 vn_read(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred, 470 int flags) 471 { 472 struct vnode *vp = (struct vnode *)fp->f_data; 473 int count, error, ioflag, fflag; 474 475 ioflag = IO_ADV_ENCODE(fp->f_advice); 476 fflag = fp->f_flag; 477 if (fflag & FNONBLOCK) 478 ioflag |= IO_NDELAY; 479 if ((fflag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC)) 480 ioflag |= IO_SYNC; 481 if (fflag & FALTIO) 482 ioflag |= IO_ALTSEMANTICS; 483 if (fflag & FDIRECT) 484 ioflag |= IO_DIRECT; 485 vn_lock(vp, LK_SHARED | LK_RETRY); 486 uio->uio_offset = *offset; 487 count = uio->uio_resid; 488 error = VOP_READ(vp, uio, ioflag, cred); 489 if (flags & FOF_UPDATE_OFFSET) 490 *offset += count - uio->uio_resid; 491 VOP_UNLOCK(vp, 0); 492 return (error); 493 } 494 495 /* 496 * File table vnode write routine. 497 */ 498 static int 499 vn_write(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred, 500 int flags) 501 { 502 struct vnode *vp = (struct vnode *)fp->f_data; 503 int count, error, ioflag, fflag; 504 505 ioflag = IO_ADV_ENCODE(fp->f_advice) | IO_UNIT; 506 fflag = fp->f_flag; 507 if (vp->v_type == VREG && (fflag & O_APPEND)) 508 ioflag |= IO_APPEND; 509 if (fflag & FNONBLOCK) 510 ioflag |= IO_NDELAY; 511 if (fflag & FFSYNC || 512 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))) 513 ioflag |= IO_SYNC; 514 else if (fflag & FDSYNC) 515 ioflag |= IO_DSYNC; 516 if (fflag & FALTIO) 517 ioflag |= IO_ALTSEMANTICS; 518 if (fflag & FDIRECT) 519 ioflag |= IO_DIRECT; 520 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 521 uio->uio_offset = *offset; 522 count = uio->uio_resid; 523 error = VOP_WRITE(vp, uio, ioflag, cred); 524 if (flags & FOF_UPDATE_OFFSET) { 525 if (ioflag & IO_APPEND) { 526 /* 527 * SUSv3 describes behaviour for count = 0 as following: 528 * "Before any action ... is taken, and if nbyte is zero 529 * and the file is a regular file, the write() function 530 * ... in the absence of errors ... shall return zero 531 * and have no other results." 532 */ 533 if (count) 534 *offset = uio->uio_offset; 535 } else 536 *offset += count - uio->uio_resid; 537 } 538 VOP_UNLOCK(vp, 0); 539 return (error); 540 } 541 542 /* 543 * File table vnode stat routine. 544 */ 545 static int 546 vn_statfile(file_t *fp, struct stat *sb) 547 { 548 struct vnode *vp = fp->f_data; 549 int error; 550 551 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 552 error = vn_stat(vp, sb); 553 VOP_UNLOCK(vp, 0); 554 return error; 555 } 556 557 int 558 vn_stat(struct vnode *vp, struct stat *sb) 559 { 560 struct vattr va; 561 int error; 562 mode_t mode; 563 564 error = VOP_GETATTR(vp, &va, kauth_cred_get()); 565 if (error) 566 return (error); 567 /* 568 * Copy from vattr table 569 */ 570 sb->st_dev = va.va_fsid; 571 sb->st_ino = va.va_fileid; 572 mode = va.va_mode; 573 switch (vp->v_type) { 574 case VREG: 575 mode |= S_IFREG; 576 break; 577 case VDIR: 578 mode |= S_IFDIR; 579 break; 580 case VBLK: 581 mode |= S_IFBLK; 582 break; 583 case VCHR: 584 mode |= S_IFCHR; 585 break; 586 case VLNK: 587 mode |= S_IFLNK; 588 break; 589 case VSOCK: 590 mode |= S_IFSOCK; 591 break; 592 case VFIFO: 593 mode |= S_IFIFO; 594 break; 595 default: 596 return (EBADF); 597 }; 598 sb->st_mode = mode; 599 sb->st_nlink = va.va_nlink; 600 sb->st_uid = va.va_uid; 601 sb->st_gid = va.va_gid; 602 sb->st_rdev = va.va_rdev; 603 sb->st_size = va.va_size; 604 sb->st_atimespec = va.va_atime; 605 sb->st_mtimespec = va.va_mtime; 606 sb->st_ctimespec = va.va_ctime; 607 sb->st_birthtimespec = va.va_birthtime; 608 sb->st_blksize = va.va_blocksize; 609 sb->st_flags = va.va_flags; 610 sb->st_gen = 0; 611 sb->st_blocks = va.va_bytes / S_BLKSIZE; 612 return (0); 613 } 614 615 /* 616 * File table vnode fcntl routine. 617 */ 618 static int 619 vn_fcntl(file_t *fp, u_int com, void *data) 620 { 621 struct vnode *vp = fp->f_data; 622 int error; 623 624 error = VOP_FCNTL(vp, com, data, fp->f_flag, kauth_cred_get()); 625 return (error); 626 } 627 628 /* 629 * File table vnode ioctl routine. 630 */ 631 static int 632 vn_ioctl(file_t *fp, u_long com, void *data) 633 { 634 struct vnode *vp = fp->f_data, *ovp; 635 struct vattr vattr; 636 int error; 637 638 switch (vp->v_type) { 639 640 case VREG: 641 case VDIR: 642 if (com == FIONREAD) { 643 error = VOP_GETATTR(vp, &vattr, 644 kauth_cred_get()); 645 if (error) 646 return (error); 647 *(int *)data = vattr.va_size - fp->f_offset; 648 return (0); 649 } 650 if ((com == FIONWRITE) || (com == FIONSPACE)) { 651 /* 652 * Files don't have send queues, so there never 653 * are any bytes in them, nor is there any 654 * open space in them. 655 */ 656 *(int *)data = 0; 657 return (0); 658 } 659 if (com == FIOGETBMAP) { 660 daddr_t *block; 661 662 if (*(daddr_t *)data < 0) 663 return (EINVAL); 664 block = (daddr_t *)data; 665 return (VOP_BMAP(vp, *block, NULL, block, NULL)); 666 } 667 if (com == OFIOGETBMAP) { 668 daddr_t ibn, obn; 669 670 if (*(int32_t *)data < 0) 671 return (EINVAL); 672 ibn = (daddr_t)*(int32_t *)data; 673 error = VOP_BMAP(vp, ibn, NULL, &obn, NULL); 674 *(int32_t *)data = (int32_t)obn; 675 return error; 676 } 677 if (com == FIONBIO || com == FIOASYNC) /* XXX */ 678 return (0); /* XXX */ 679 /* fall into ... */ 680 case VFIFO: 681 case VCHR: 682 case VBLK: 683 error = VOP_IOCTL(vp, com, data, fp->f_flag, 684 kauth_cred_get()); 685 if (error == 0 && com == TIOCSCTTY) { 686 vref(vp); 687 mutex_enter(proc_lock); 688 ovp = curproc->p_session->s_ttyvp; 689 curproc->p_session->s_ttyvp = vp; 690 mutex_exit(proc_lock); 691 if (ovp != NULL) 692 vrele(ovp); 693 } 694 return (error); 695 696 default: 697 return (EPASSTHROUGH); 698 } 699 } 700 701 /* 702 * File table vnode poll routine. 703 */ 704 static int 705 vn_poll(file_t *fp, int events) 706 { 707 708 return (VOP_POLL(fp->f_data, events)); 709 } 710 711 /* 712 * File table vnode kqfilter routine. 713 */ 714 int 715 vn_kqfilter(file_t *fp, struct knote *kn) 716 { 717 718 return (VOP_KQFILTER(fp->f_data, kn)); 719 } 720 721 /* 722 * Check that the vnode is still valid, and if so 723 * acquire requested lock. 724 */ 725 int 726 vn_lock(struct vnode *vp, int flags) 727 { 728 int error; 729 730 #if 0 731 KASSERT(vp->v_usecount > 0 || (flags & LK_INTERLOCK) != 0 732 || (vp->v_iflag & VI_ONWORKLST) != 0); 733 #endif 734 KASSERT((flags & 735 ~(LK_INTERLOCK|LK_SHARED|LK_EXCLUSIVE|LK_NOWAIT|LK_RETRY| 736 LK_CANRECURSE)) 737 == 0); 738 739 #ifdef DIAGNOSTIC 740 if (wapbl_vphaswapbl(vp)) 741 WAPBL_JUNLOCK_ASSERT(wapbl_vptomp(vp)); 742 #endif 743 744 do { 745 /* 746 * XXX PR 37706 forced unmount of file systems is unsafe. 747 * Race between vclean() and this the remaining problem. 748 */ 749 if (vp->v_iflag & VI_XLOCK) { 750 if ((flags & LK_INTERLOCK) == 0) { 751 mutex_enter(&vp->v_interlock); 752 } 753 flags &= ~LK_INTERLOCK; 754 if (flags & LK_NOWAIT) { 755 mutex_exit(&vp->v_interlock); 756 return EBUSY; 757 } 758 vwait(vp, VI_XLOCK); 759 mutex_exit(&vp->v_interlock); 760 error = ENOENT; 761 } else { 762 if ((flags & LK_INTERLOCK) != 0) { 763 mutex_exit(&vp->v_interlock); 764 } 765 flags &= ~LK_INTERLOCK; 766 error = VOP_LOCK(vp, (flags & ~LK_RETRY)); 767 if (error == 0 || error == EDEADLK || error == EBUSY) 768 return (error); 769 } 770 } while (flags & LK_RETRY); 771 return (error); 772 } 773 774 /* 775 * File table vnode close routine. 776 */ 777 static int 778 vn_closefile(file_t *fp) 779 { 780 781 return vn_close(fp->f_data, fp->f_flag, fp->f_cred); 782 } 783 784 /* 785 * Enable LK_CANRECURSE on lock. Return prior status. 786 */ 787 u_int 788 vn_setrecurse(struct vnode *vp) 789 { 790 struct vnlock *lkp; 791 792 lkp = (vp->v_vnlock != NULL ? vp->v_vnlock : &vp->v_lock); 793 atomic_inc_uint(&lkp->vl_canrecurse); 794 795 return 0; 796 } 797 798 /* 799 * Called when done with locksetrecurse. 800 */ 801 void 802 vn_restorerecurse(struct vnode *vp, u_int flags) 803 { 804 struct vnlock *lkp; 805 806 lkp = (vp->v_vnlock != NULL ? vp->v_vnlock : &vp->v_lock); 807 atomic_dec_uint(&lkp->vl_canrecurse); 808 } 809 810 /* 811 * Simplified in-kernel wrapper calls for extended attribute access. 812 * Both calls pass in a NULL credential, authorizing a "kernel" access. 813 * Set IO_NODELOCKED in ioflg if the vnode is already locked. 814 */ 815 int 816 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace, 817 const char *attrname, size_t *buflen, void *bf, struct lwp *l) 818 { 819 struct uio auio; 820 struct iovec aiov; 821 int error; 822 823 aiov.iov_len = *buflen; 824 aiov.iov_base = bf; 825 826 auio.uio_iov = &aiov; 827 auio.uio_iovcnt = 1; 828 auio.uio_rw = UIO_READ; 829 auio.uio_offset = 0; 830 auio.uio_resid = *buflen; 831 UIO_SETUP_SYSSPACE(&auio); 832 833 if ((ioflg & IO_NODELOCKED) == 0) 834 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 835 836 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL); 837 838 if ((ioflg & IO_NODELOCKED) == 0) 839 VOP_UNLOCK(vp, 0); 840 841 if (error == 0) 842 *buflen = *buflen - auio.uio_resid; 843 844 return (error); 845 } 846 847 /* 848 * XXX Failure mode if partially written? 849 */ 850 int 851 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace, 852 const char *attrname, size_t buflen, const void *bf, struct lwp *l) 853 { 854 struct uio auio; 855 struct iovec aiov; 856 int error; 857 858 aiov.iov_len = buflen; 859 aiov.iov_base = __UNCONST(bf); /* XXXUNCONST kills const */ 860 861 auio.uio_iov = &aiov; 862 auio.uio_iovcnt = 1; 863 auio.uio_rw = UIO_WRITE; 864 auio.uio_offset = 0; 865 auio.uio_resid = buflen; 866 UIO_SETUP_SYSSPACE(&auio); 867 868 if ((ioflg & IO_NODELOCKED) == 0) { 869 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 870 } 871 872 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL); 873 874 if ((ioflg & IO_NODELOCKED) == 0) { 875 VOP_UNLOCK(vp, 0); 876 } 877 878 return (error); 879 } 880 881 int 882 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace, 883 const char *attrname, struct lwp *l) 884 { 885 int error; 886 887 if ((ioflg & IO_NODELOCKED) == 0) { 888 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 889 } 890 891 error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL); 892 if (error == EOPNOTSUPP) 893 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL, NULL); 894 895 if ((ioflg & IO_NODELOCKED) == 0) { 896 VOP_UNLOCK(vp, 0); 897 } 898 899 return (error); 900 } 901 902 void 903 vn_ra_allocctx(struct vnode *vp) 904 { 905 struct uvm_ractx *ra = NULL; 906 907 KASSERT(mutex_owned(&vp->v_interlock)); 908 909 if (vp->v_type != VREG) { 910 return; 911 } 912 if (vp->v_ractx != NULL) { 913 return; 914 } 915 if (vp->v_ractx == NULL) { 916 mutex_exit(&vp->v_interlock); 917 ra = uvm_ra_allocctx(); 918 mutex_enter(&vp->v_interlock); 919 if (ra != NULL && vp->v_ractx == NULL) { 920 vp->v_ractx = ra; 921 ra = NULL; 922 } 923 } 924 if (ra != NULL) { 925 uvm_ra_freectx(ra); 926 } 927 } 928 929 int 930 vn_fifo_bypass(void *v) 931 { 932 struct vop_generic_args *ap = v; 933 934 return VOCALL(fifo_vnodeop_p, ap->a_desc->vdesc_offset, v); 935 } 936