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