1 /* $NetBSD: vfs_vnops.c,v 1.156 2008/04/24 15:35:30 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.156 2008/04/24 15:35:30 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(file_t *fp, off_t *offset, struct uio *uio, 81 kauth_cred_t cred, int flags); 82 static int vn_write(file_t *fp, off_t *offset, struct uio *uio, 83 kauth_cred_t cred, int flags); 84 static int vn_closefile(file_t *fp); 85 static int vn_poll(file_t *fp, int events); 86 static int vn_fcntl(file_t *fp, u_int com, void *data); 87 static int vn_statfile(file_t *fp, struct stat *sb); 88 static int vn_ioctl(file_t *fp, u_long com, void *data); 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) 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(file_t *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(file_t *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, fflag; 427 428 ioflag = IO_ADV_ENCODE(fp->f_advice); 429 fflag = fp->f_flag; 430 if (fflag & FNONBLOCK) 431 ioflag |= IO_NDELAY; 432 if ((fflag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC)) 433 ioflag |= IO_SYNC; 434 if (fflag & FALTIO) 435 ioflag |= IO_ALTSEMANTICS; 436 if (fflag & FDIRECT) 437 ioflag |= IO_DIRECT; 438 vn_lock(vp, LK_SHARED | LK_RETRY); 439 uio->uio_offset = *offset; 440 count = uio->uio_resid; 441 error = VOP_READ(vp, uio, ioflag, cred); 442 if (flags & FOF_UPDATE_OFFSET) 443 *offset += count - uio->uio_resid; 444 VOP_UNLOCK(vp, 0); 445 return (error); 446 } 447 448 /* 449 * File table vnode write routine. 450 */ 451 static int 452 vn_write(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred, 453 int flags) 454 { 455 struct vnode *vp = (struct vnode *)fp->f_data; 456 int count, error, ioflag, fflag; 457 458 ioflag = IO_ADV_ENCODE(fp->f_advice) | IO_UNIT; 459 fflag = fp->f_flag; 460 if (vp->v_type == VREG && (fflag & O_APPEND)) 461 ioflag |= IO_APPEND; 462 if (fflag & FNONBLOCK) 463 ioflag |= IO_NDELAY; 464 if (fflag & FFSYNC || 465 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))) 466 ioflag |= IO_SYNC; 467 else if (fflag & FDSYNC) 468 ioflag |= IO_DSYNC; 469 if (fflag & FALTIO) 470 ioflag |= IO_ALTSEMANTICS; 471 if (fflag & FDIRECT) 472 ioflag |= IO_DIRECT; 473 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 474 uio->uio_offset = *offset; 475 count = uio->uio_resid; 476 error = VOP_WRITE(vp, uio, ioflag, cred); 477 if (flags & FOF_UPDATE_OFFSET) { 478 if (ioflag & IO_APPEND) 479 *offset = uio->uio_offset; 480 else 481 *offset += count - uio->uio_resid; 482 } 483 VOP_UNLOCK(vp, 0); 484 return (error); 485 } 486 487 /* 488 * File table vnode stat routine. 489 */ 490 static int 491 vn_statfile(file_t *fp, struct stat *sb) 492 { 493 struct vnode *vp = (struct vnode *)fp->f_data; 494 495 return vn_stat(vp, sb); 496 } 497 498 int 499 vn_stat(struct vnode *vp, struct stat *sb) 500 { 501 struct vattr va; 502 int error; 503 mode_t mode; 504 505 error = VOP_GETATTR(vp, &va, kauth_cred_get()); 506 if (error) 507 return (error); 508 /* 509 * Copy from vattr table 510 */ 511 sb->st_dev = va.va_fsid; 512 sb->st_ino = va.va_fileid; 513 mode = va.va_mode; 514 switch (vp->v_type) { 515 case VREG: 516 mode |= S_IFREG; 517 break; 518 case VDIR: 519 mode |= S_IFDIR; 520 break; 521 case VBLK: 522 mode |= S_IFBLK; 523 break; 524 case VCHR: 525 mode |= S_IFCHR; 526 break; 527 case VLNK: 528 mode |= S_IFLNK; 529 break; 530 case VSOCK: 531 mode |= S_IFSOCK; 532 break; 533 case VFIFO: 534 mode |= S_IFIFO; 535 break; 536 default: 537 return (EBADF); 538 }; 539 sb->st_mode = mode; 540 sb->st_nlink = va.va_nlink; 541 sb->st_uid = va.va_uid; 542 sb->st_gid = va.va_gid; 543 sb->st_rdev = va.va_rdev; 544 sb->st_size = va.va_size; 545 sb->st_atimespec = va.va_atime; 546 sb->st_mtimespec = va.va_mtime; 547 sb->st_ctimespec = va.va_ctime; 548 sb->st_birthtimespec = va.va_birthtime; 549 sb->st_blksize = va.va_blocksize; 550 sb->st_flags = va.va_flags; 551 sb->st_gen = 0; 552 sb->st_blocks = va.va_bytes / S_BLKSIZE; 553 return (0); 554 } 555 556 /* 557 * File table vnode fcntl routine. 558 */ 559 static int 560 vn_fcntl(file_t *fp, u_int com, void *data) 561 { 562 struct vnode *vp = fp->f_data; 563 int error; 564 565 error = VOP_FCNTL(vp, com, data, fp->f_flag, kauth_cred_get()); 566 return (error); 567 } 568 569 /* 570 * File table vnode ioctl routine. 571 */ 572 static int 573 vn_ioctl(file_t *fp, u_long com, void *data) 574 { 575 struct vnode *vp = fp->f_data, *ovp; 576 struct vattr vattr; 577 int error; 578 579 switch (vp->v_type) { 580 581 case VREG: 582 case VDIR: 583 if (com == FIONREAD) { 584 error = VOP_GETATTR(vp, &vattr, 585 kauth_cred_get()); 586 if (error) 587 return (error); 588 *(int *)data = vattr.va_size - fp->f_offset; 589 return (0); 590 } 591 if ((com == FIONWRITE) || (com == FIONSPACE)) { 592 /* 593 * Files don't have send queues, so there never 594 * are any bytes in them, nor is there any 595 * open space in them. 596 */ 597 *(int *)data = 0; 598 return (0); 599 } 600 if (com == FIOGETBMAP) { 601 daddr_t *block; 602 603 if (*(daddr_t *)data < 0) 604 return (EINVAL); 605 block = (daddr_t *)data; 606 return (VOP_BMAP(vp, *block, NULL, block, NULL)); 607 } 608 if (com == OFIOGETBMAP) { 609 daddr_t ibn, obn; 610 611 if (*(int32_t *)data < 0) 612 return (EINVAL); 613 ibn = (daddr_t)*(int32_t *)data; 614 error = VOP_BMAP(vp, ibn, NULL, &obn, NULL); 615 *(int32_t *)data = (int32_t)obn; 616 return error; 617 } 618 if (com == FIONBIO || com == FIOASYNC) /* XXX */ 619 return (0); /* XXX */ 620 /* fall into ... */ 621 case VFIFO: 622 case VCHR: 623 case VBLK: 624 error = VOP_IOCTL(vp, com, data, fp->f_flag, 625 kauth_cred_get()); 626 if (error == 0 && com == TIOCSCTTY) { 627 VREF(vp); 628 mutex_enter(proc_lock); 629 ovp = curproc->p_session->s_ttyvp; 630 curproc->p_session->s_ttyvp = vp; 631 mutex_exit(proc_lock); 632 if (ovp != NULL) 633 vrele(ovp); 634 } 635 return (error); 636 637 default: 638 return (EPASSTHROUGH); 639 } 640 } 641 642 /* 643 * File table vnode poll routine. 644 */ 645 static int 646 vn_poll(file_t *fp, int events) 647 { 648 649 return (VOP_POLL(fp->f_data, events)); 650 } 651 652 /* 653 * File table vnode kqfilter routine. 654 */ 655 int 656 vn_kqfilter(file_t *fp, struct knote *kn) 657 { 658 659 return (VOP_KQFILTER(fp->f_data, kn)); 660 } 661 662 /* 663 * Check that the vnode is still valid, and if so 664 * acquire requested lock. 665 */ 666 int 667 vn_lock(struct vnode *vp, int flags) 668 { 669 int error; 670 671 #if 0 672 KASSERT(vp->v_usecount > 0 || (flags & LK_INTERLOCK) != 0 673 || (vp->v_iflag & VI_ONWORKLST) != 0); 674 #endif 675 KASSERT((flags & 676 ~(LK_INTERLOCK|LK_SHARED|LK_EXCLUSIVE|LK_NOWAIT|LK_RETRY| 677 LK_CANRECURSE)) 678 == 0); 679 680 do { 681 if ((flags & LK_INTERLOCK) == 0) 682 mutex_enter(&vp->v_interlock); 683 if (vp->v_iflag & VI_XLOCK) { 684 if (flags & LK_NOWAIT) { 685 mutex_exit(&vp->v_interlock); 686 return EBUSY; 687 } 688 vwait(vp, VI_XLOCK); 689 mutex_exit(&vp->v_interlock); 690 error = ENOENT; 691 } else { 692 error = VOP_LOCK(vp, 693 (flags & ~LK_RETRY) | LK_INTERLOCK); 694 if (error == 0 || error == EDEADLK || error == EBUSY) 695 return (error); 696 } 697 flags &= ~LK_INTERLOCK; 698 } while (flags & LK_RETRY); 699 return (error); 700 } 701 702 /* 703 * File table vnode close routine. 704 */ 705 static int 706 vn_closefile(file_t *fp) 707 { 708 709 return vn_close(fp->f_data, fp->f_flag, fp->f_cred); 710 } 711 712 /* 713 * Enable LK_CANRECURSE on lock. Return prior status. 714 */ 715 u_int 716 vn_setrecurse(struct vnode *vp) 717 { 718 struct vnlock *lkp; 719 720 lkp = (vp->v_vnlock != NULL ? vp->v_vnlock : &vp->v_lock); 721 atomic_inc_uint(&lkp->vl_canrecurse); 722 723 return 0; 724 } 725 726 /* 727 * Called when done with locksetrecurse. 728 */ 729 void 730 vn_restorerecurse(struct vnode *vp, u_int flags) 731 { 732 struct vnlock *lkp; 733 734 lkp = (vp->v_vnlock != NULL ? vp->v_vnlock : &vp->v_lock); 735 atomic_dec_uint(&lkp->vl_canrecurse); 736 } 737 738 /* 739 * Simplified in-kernel wrapper calls for extended attribute access. 740 * Both calls pass in a NULL credential, authorizing a "kernel" access. 741 * Set IO_NODELOCKED in ioflg if the vnode is already locked. 742 */ 743 int 744 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace, 745 const char *attrname, size_t *buflen, void *bf, struct lwp *l) 746 { 747 struct uio auio; 748 struct iovec aiov; 749 int error; 750 751 aiov.iov_len = *buflen; 752 aiov.iov_base = bf; 753 754 auio.uio_iov = &aiov; 755 auio.uio_iovcnt = 1; 756 auio.uio_rw = UIO_READ; 757 auio.uio_offset = 0; 758 auio.uio_resid = *buflen; 759 UIO_SETUP_SYSSPACE(&auio); 760 761 if ((ioflg & IO_NODELOCKED) == 0) 762 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 763 764 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL); 765 766 if ((ioflg & IO_NODELOCKED) == 0) 767 VOP_UNLOCK(vp, 0); 768 769 if (error == 0) 770 *buflen = *buflen - auio.uio_resid; 771 772 return (error); 773 } 774 775 /* 776 * XXX Failure mode if partially written? 777 */ 778 int 779 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace, 780 const char *attrname, size_t buflen, const void *bf, struct lwp *l) 781 { 782 struct uio auio; 783 struct iovec aiov; 784 int error; 785 786 aiov.iov_len = buflen; 787 aiov.iov_base = __UNCONST(bf); /* XXXUNCONST kills const */ 788 789 auio.uio_iov = &aiov; 790 auio.uio_iovcnt = 1; 791 auio.uio_rw = UIO_WRITE; 792 auio.uio_offset = 0; 793 auio.uio_resid = buflen; 794 UIO_SETUP_SYSSPACE(&auio); 795 796 if ((ioflg & IO_NODELOCKED) == 0) { 797 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 798 } 799 800 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL); 801 802 if ((ioflg & IO_NODELOCKED) == 0) { 803 VOP_UNLOCK(vp, 0); 804 } 805 806 return (error); 807 } 808 809 int 810 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace, 811 const char *attrname, struct lwp *l) 812 { 813 int error; 814 815 if ((ioflg & IO_NODELOCKED) == 0) { 816 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 817 } 818 819 error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL); 820 if (error == EOPNOTSUPP) 821 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL, NULL); 822 823 if ((ioflg & IO_NODELOCKED) == 0) { 824 VOP_UNLOCK(vp, 0); 825 } 826 827 return (error); 828 } 829 830 void 831 vn_ra_allocctx(struct vnode *vp) 832 { 833 struct uvm_ractx *ra = NULL; 834 835 KASSERT(mutex_owned(&vp->v_interlock)); 836 837 if (vp->v_type != VREG) { 838 return; 839 } 840 if (vp->v_ractx != NULL) { 841 return; 842 } 843 if (vp->v_ractx == NULL) { 844 mutex_exit(&vp->v_interlock); 845 ra = uvm_ra_allocctx(); 846 mutex_enter(&vp->v_interlock); 847 if (ra != NULL && vp->v_ractx == NULL) { 848 vp->v_ractx = ra; 849 ra = NULL; 850 } 851 } 852 if (ra != NULL) { 853 uvm_ra_freectx(ra); 854 } 855 } 856