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