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