1 /* 2 * Copyright (c) 1982, 1986, 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 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 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)vfs_vnops.c 8.2 (Berkeley) 1/21/94 39 * $FreeBSD$ 40 */ 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/fcntl.h> 45 #include <sys/file.h> 46 #include <sys/stat.h> 47 #include <sys/proc.h> 48 #include <sys/mount.h> 49 #include <sys/mutex.h> 50 #include <sys/namei.h> 51 #include <sys/vnode.h> 52 #include <sys/bio.h> 53 #include <sys/buf.h> 54 #include <sys/filio.h> 55 #include <sys/ttycom.h> 56 #include <sys/conf.h> 57 58 static int vn_closefile __P((struct file *fp, struct proc *p)); 59 static int vn_ioctl __P((struct file *fp, u_long com, caddr_t data, 60 struct proc *p)); 61 static int vn_read __P((struct file *fp, struct uio *uio, 62 struct ucred *cred, int flags, struct proc *p)); 63 static int vn_poll __P((struct file *fp, int events, struct ucred *cred, 64 struct proc *p)); 65 static int vn_kqfilter __P((struct file *fp, struct knote *kn)); 66 static int vn_statfile __P((struct file *fp, struct stat *sb, struct proc *p)); 67 static int vn_write __P((struct file *fp, struct uio *uio, 68 struct ucred *cred, int flags, struct proc *p)); 69 70 struct fileops vnops = { 71 vn_read, vn_write, vn_ioctl, vn_poll, vn_kqfilter, 72 vn_statfile, vn_closefile 73 }; 74 75 /* 76 * Common code for vnode open operations. 77 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine. 78 * 79 * Note that this does NOT free nameidata for the successful case, 80 * due to the NDINIT being done elsewhere. 81 */ 82 int 83 vn_open(ndp, flagp, cmode) 84 register struct nameidata *ndp; 85 int *flagp, cmode; 86 { 87 struct vnode *vp; 88 struct mount *mp; 89 struct proc *p = ndp->ni_cnd.cn_proc; 90 struct ucred *cred = p->p_ucred; 91 struct vattr vat; 92 struct vattr *vap = &vat; 93 int mode, fmode, error; 94 95 restart: 96 fmode = *flagp; 97 if (fmode & O_CREAT) { 98 ndp->ni_cnd.cn_nameiop = CREATE; 99 ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF; 100 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0) 101 ndp->ni_cnd.cn_flags |= FOLLOW; 102 bwillwrite(); 103 if ((error = namei(ndp)) != 0) 104 return (error); 105 if (ndp->ni_vp == NULL) { 106 VATTR_NULL(vap); 107 vap->va_type = VREG; 108 vap->va_mode = cmode; 109 if (fmode & O_EXCL) 110 vap->va_vaflags |= VA_EXCLUSIVE; 111 if (vn_start_write(ndp->ni_dvp, &mp, V_NOWAIT) != 0) { 112 NDFREE(ndp, NDF_ONLY_PNBUF); 113 vput(ndp->ni_dvp); 114 if ((error = vn_start_write(NULL, &mp, 115 V_XSLEEP | PCATCH)) != 0) 116 return (error); 117 goto restart; 118 } 119 VOP_LEASE(ndp->ni_dvp, p, cred, LEASE_WRITE); 120 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp, 121 &ndp->ni_cnd, vap); 122 vput(ndp->ni_dvp); 123 vn_finished_write(mp); 124 if (error) { 125 NDFREE(ndp, NDF_ONLY_PNBUF); 126 return (error); 127 } 128 ASSERT_VOP_UNLOCKED(ndp->ni_dvp, "create"); 129 ASSERT_VOP_LOCKED(ndp->ni_vp, "create"); 130 fmode &= ~O_TRUNC; 131 vp = ndp->ni_vp; 132 } else { 133 if (ndp->ni_dvp == ndp->ni_vp) 134 vrele(ndp->ni_dvp); 135 else 136 vput(ndp->ni_dvp); 137 ndp->ni_dvp = NULL; 138 vp = ndp->ni_vp; 139 if (fmode & O_EXCL) { 140 error = EEXIST; 141 goto bad; 142 } 143 fmode &= ~O_CREAT; 144 } 145 } else { 146 ndp->ni_cnd.cn_nameiop = LOOKUP; 147 ndp->ni_cnd.cn_flags = 148 ((fmode & O_NOFOLLOW) ? NOFOLLOW : FOLLOW) | LOCKLEAF; 149 if ((error = namei(ndp)) != 0) 150 return (error); 151 vp = ndp->ni_vp; 152 } 153 if (vp->v_type == VLNK) { 154 error = EMLINK; 155 goto bad; 156 } 157 if (vp->v_type == VSOCK) { 158 error = EOPNOTSUPP; 159 goto bad; 160 } 161 if ((fmode & O_CREAT) == 0) { 162 mode = 0; 163 if (fmode & (FWRITE | O_TRUNC)) { 164 if (vp->v_type == VDIR) { 165 error = EISDIR; 166 goto bad; 167 } 168 error = vn_writechk(vp); 169 if (error) 170 goto bad; 171 mode |= VWRITE; 172 } 173 if (fmode & FREAD) 174 mode |= VREAD; 175 if (mode) { 176 error = VOP_ACCESS(vp, mode, cred, p); 177 if (error) 178 goto bad; 179 } 180 } 181 if ((error = VOP_OPEN(vp, fmode, cred, p)) != 0) 182 goto bad; 183 /* 184 * Make sure that a VM object is created for VMIO support. 185 */ 186 if (vn_canvmio(vp) == TRUE) { 187 if ((error = vfs_object_create(vp, p, cred)) != 0) 188 goto bad; 189 } 190 191 if (fmode & FWRITE) 192 vp->v_writecount++; 193 *flagp = fmode; 194 return (0); 195 bad: 196 NDFREE(ndp, NDF_ONLY_PNBUF); 197 vput(vp); 198 *flagp = fmode; 199 return (error); 200 } 201 202 /* 203 * Check for write permissions on the specified vnode. 204 * Prototype text segments cannot be written. 205 */ 206 int 207 vn_writechk(vp) 208 register struct vnode *vp; 209 { 210 211 /* 212 * If there's shared text associated with 213 * the vnode, try to free it up once. If 214 * we fail, we can't allow writing. 215 */ 216 if (vp->v_flag & VTEXT) 217 return (ETXTBSY); 218 return (0); 219 } 220 221 /* 222 * Vnode close call 223 */ 224 int 225 vn_close(vp, flags, cred, p) 226 register struct vnode *vp; 227 int flags; 228 struct ucred *cred; 229 struct proc *p; 230 { 231 int error; 232 233 if (flags & FWRITE) 234 vp->v_writecount--; 235 error = VOP_CLOSE(vp, flags, cred, p); 236 /* 237 * XXX - In certain instances VOP_CLOSE has to do the vrele 238 * itself. If the vrele has been done, it will return EAGAIN 239 * to indicate that the vrele should not be done again. When 240 * this happens, we just return success. The correct thing to 241 * do would be to have all VOP_CLOSE instances do the vrele. 242 */ 243 if (error == EAGAIN) 244 return (0); 245 vrele(vp); 246 return (error); 247 } 248 249 static __inline 250 int 251 sequential_heuristic(struct uio *uio, struct file *fp) 252 { 253 /* 254 * Sequential heuristic - detect sequential operation 255 */ 256 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) || 257 uio->uio_offset == fp->f_nextoff) { 258 /* 259 * XXX we assume that the filesystem block size is 260 * the default. Not true, but still gives us a pretty 261 * good indicator of how sequential the read operations 262 * are. 263 */ 264 fp->f_seqcount += (uio->uio_resid + BKVASIZE - 1) / BKVASIZE; 265 if (fp->f_seqcount >= 127) 266 fp->f_seqcount = 127; 267 return(fp->f_seqcount << 16); 268 } 269 270 /* 271 * Not sequential, quick draw-down of seqcount 272 */ 273 if (fp->f_seqcount > 1) 274 fp->f_seqcount = 1; 275 else 276 fp->f_seqcount = 0; 277 return(0); 278 } 279 280 /* 281 * Package up an I/O request on a vnode into a uio and do it. 282 */ 283 int 284 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p) 285 enum uio_rw rw; 286 struct vnode *vp; 287 caddr_t base; 288 int len; 289 off_t offset; 290 enum uio_seg segflg; 291 int ioflg; 292 struct ucred *cred; 293 int *aresid; 294 struct proc *p; 295 { 296 struct uio auio; 297 struct iovec aiov; 298 struct mount *mp; 299 int error; 300 301 if ((ioflg & IO_NODELOCKED) == 0) { 302 mp = NULL; 303 if (rw == UIO_WRITE && 304 vp->v_type != VCHR && 305 (error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) 306 return (error); 307 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 308 } 309 auio.uio_iov = &aiov; 310 auio.uio_iovcnt = 1; 311 aiov.iov_base = base; 312 aiov.iov_len = len; 313 auio.uio_resid = len; 314 auio.uio_offset = offset; 315 auio.uio_segflg = segflg; 316 auio.uio_rw = rw; 317 auio.uio_procp = p; 318 if (rw == UIO_READ) { 319 error = VOP_READ(vp, &auio, ioflg, cred); 320 } else { 321 error = VOP_WRITE(vp, &auio, ioflg, cred); 322 } 323 if (aresid) 324 *aresid = auio.uio_resid; 325 else 326 if (auio.uio_resid && error == 0) 327 error = EIO; 328 if ((ioflg & IO_NODELOCKED) == 0) { 329 vn_finished_write(mp); 330 VOP_UNLOCK(vp, 0, p); 331 } 332 return (error); 333 } 334 335 /* 336 * File table vnode read routine. 337 */ 338 static int 339 vn_read(fp, uio, cred, flags, p) 340 struct file *fp; 341 struct uio *uio; 342 struct ucred *cred; 343 struct proc *p; 344 int flags; 345 { 346 struct vnode *vp; 347 int error, ioflag; 348 349 KASSERT(uio->uio_procp == p, ("uio_procp %p is not p %p", 350 uio->uio_procp, p)); 351 vp = (struct vnode *)fp->f_data; 352 ioflag = 0; 353 if (fp->f_flag & FNONBLOCK) 354 ioflag |= IO_NDELAY; 355 VOP_LEASE(vp, p, cred, LEASE_READ); 356 vn_lock(vp, LK_SHARED | LK_NOPAUSE | LK_RETRY, p); 357 if ((flags & FOF_OFFSET) == 0) 358 uio->uio_offset = fp->f_offset; 359 360 ioflag |= sequential_heuristic(uio, fp); 361 362 error = VOP_READ(vp, uio, ioflag, cred); 363 if ((flags & FOF_OFFSET) == 0) 364 fp->f_offset = uio->uio_offset; 365 fp->f_nextoff = uio->uio_offset; 366 VOP_UNLOCK(vp, 0, p); 367 return (error); 368 } 369 370 /* 371 * File table vnode write routine. 372 */ 373 static int 374 vn_write(fp, uio, cred, flags, p) 375 struct file *fp; 376 struct uio *uio; 377 struct ucred *cred; 378 struct proc *p; 379 int flags; 380 { 381 struct vnode *vp; 382 struct mount *mp; 383 int error, ioflag; 384 385 KASSERT(uio->uio_procp == p, ("uio_procp %p is not p %p", 386 uio->uio_procp, p)); 387 vp = (struct vnode *)fp->f_data; 388 if (vp->v_type == VREG) 389 bwillwrite(); 390 vp = (struct vnode *)fp->f_data; /* XXX needed? */ 391 ioflag = IO_UNIT; 392 if (vp->v_type == VREG && (fp->f_flag & O_APPEND)) 393 ioflag |= IO_APPEND; 394 if (fp->f_flag & FNONBLOCK) 395 ioflag |= IO_NDELAY; 396 if ((fp->f_flag & O_FSYNC) || 397 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))) 398 ioflag |= IO_SYNC; 399 mp = NULL; 400 if (vp->v_type != VCHR && 401 (error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) 402 return (error); 403 VOP_LEASE(vp, p, cred, LEASE_WRITE); 404 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 405 if ((flags & FOF_OFFSET) == 0) 406 uio->uio_offset = fp->f_offset; 407 ioflag |= sequential_heuristic(uio, fp); 408 error = VOP_WRITE(vp, uio, ioflag, cred); 409 if ((flags & FOF_OFFSET) == 0) 410 fp->f_offset = uio->uio_offset; 411 fp->f_nextoff = uio->uio_offset; 412 VOP_UNLOCK(vp, 0, p); 413 vn_finished_write(mp); 414 return (error); 415 } 416 417 /* 418 * File table vnode stat routine. 419 */ 420 static int 421 vn_statfile(fp, sb, p) 422 struct file *fp; 423 struct stat *sb; 424 struct proc *p; 425 { 426 struct vnode *vp = (struct vnode *)fp->f_data; 427 428 return vn_stat(vp, sb, p); 429 } 430 431 int 432 vn_stat(vp, sb, p) 433 struct vnode *vp; 434 register struct stat *sb; 435 struct proc *p; 436 { 437 struct vattr vattr; 438 register struct vattr *vap; 439 int error; 440 u_short mode; 441 442 vap = &vattr; 443 error = VOP_GETATTR(vp, vap, p->p_ucred, p); 444 if (error) 445 return (error); 446 447 /* 448 * Zero the spare stat fields 449 */ 450 sb->st_lspare = 0; 451 sb->st_qspare[0] = 0; 452 sb->st_qspare[1] = 0; 453 454 /* 455 * Copy from vattr table 456 */ 457 if (vap->va_fsid != VNOVAL) 458 sb->st_dev = vap->va_fsid; 459 else 460 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0]; 461 sb->st_ino = vap->va_fileid; 462 mode = vap->va_mode; 463 switch (vap->va_type) { 464 case VREG: 465 mode |= S_IFREG; 466 break; 467 case VDIR: 468 mode |= S_IFDIR; 469 break; 470 case VBLK: 471 mode |= S_IFBLK; 472 break; 473 case VCHR: 474 mode |= S_IFCHR; 475 break; 476 case VLNK: 477 mode |= S_IFLNK; 478 /* This is a cosmetic change, symlinks do not have a mode. */ 479 if (vp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) 480 sb->st_mode &= ~ACCESSPERMS; /* 0000 */ 481 else 482 sb->st_mode |= ACCESSPERMS; /* 0777 */ 483 break; 484 case VSOCK: 485 mode |= S_IFSOCK; 486 break; 487 case VFIFO: 488 mode |= S_IFIFO; 489 break; 490 default: 491 return (EBADF); 492 }; 493 sb->st_mode = mode; 494 sb->st_nlink = vap->va_nlink; 495 sb->st_uid = vap->va_uid; 496 sb->st_gid = vap->va_gid; 497 sb->st_rdev = vap->va_rdev; 498 sb->st_size = vap->va_size; 499 sb->st_atimespec = vap->va_atime; 500 sb->st_mtimespec = vap->va_mtime; 501 sb->st_ctimespec = vap->va_ctime; 502 503 /* 504 * According to www.opengroup.org, the meaning of st_blksize is 505 * "a filesystem-specific preferred I/O block size for this 506 * object. In some filesystem types, this may vary from file 507 * to file" 508 * Default to zero to catch bogus uses of this field. 509 */ 510 511 if (vap->va_type == VREG) { 512 sb->st_blksize = vap->va_blocksize; 513 } else if (vn_isdisk(vp, NULL)) { 514 sb->st_blksize = vp->v_rdev->si_bsize_best; 515 if (sb->st_blksize < vp->v_rdev->si_bsize_phys) 516 sb->st_blksize = vp->v_rdev->si_bsize_phys; 517 if (sb->st_blksize < BLKDEV_IOSIZE) 518 sb->st_blksize = BLKDEV_IOSIZE; 519 } else { 520 sb->st_blksize = 0; 521 } 522 523 sb->st_flags = vap->va_flags; 524 if (suser_xxx(p->p_ucred, 0, 0)) 525 sb->st_gen = 0; 526 else 527 sb->st_gen = vap->va_gen; 528 529 #if (S_BLKSIZE == 512) 530 /* Optimize this case */ 531 sb->st_blocks = vap->va_bytes >> 9; 532 #else 533 sb->st_blocks = vap->va_bytes / S_BLKSIZE; 534 #endif 535 return (0); 536 } 537 538 /* 539 * File table vnode ioctl routine. 540 */ 541 static int 542 vn_ioctl(fp, com, data, p) 543 struct file *fp; 544 u_long com; 545 caddr_t data; 546 struct proc *p; 547 { 548 register struct vnode *vp = ((struct vnode *)fp->f_data); 549 struct vattr vattr; 550 int error; 551 552 switch (vp->v_type) { 553 554 case VREG: 555 case VDIR: 556 if (com == FIONREAD) { 557 error = VOP_GETATTR(vp, &vattr, p->p_ucred, p); 558 if (error) 559 return (error); 560 *(int *)data = vattr.va_size - fp->f_offset; 561 return (0); 562 } 563 if (com == FIONBIO || com == FIOASYNC) /* XXX */ 564 return (0); /* XXX */ 565 /* fall into ... */ 566 567 default: 568 #if 0 569 return (ENOTTY); 570 #endif 571 case VFIFO: 572 case VCHR: 573 case VBLK: 574 if (com == FIODTYPE) { 575 if (vp->v_type != VCHR && vp->v_type != VBLK) 576 return (ENOTTY); 577 *(int *)data = devsw(vp->v_rdev)->d_flags & D_TYPEMASK; 578 return (0); 579 } 580 error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p); 581 if (error == 0 && com == TIOCSCTTY) { 582 583 /* Do nothing if reassigning same control tty */ 584 if (p->p_session->s_ttyvp == vp) 585 return (0); 586 587 /* Get rid of reference to old control tty */ 588 if (p->p_session->s_ttyvp) 589 vrele(p->p_session->s_ttyvp); 590 591 p->p_session->s_ttyvp = vp; 592 VREF(vp); 593 } 594 return (error); 595 } 596 } 597 598 /* 599 * File table vnode poll routine. 600 */ 601 static int 602 vn_poll(fp, events, cred, p) 603 struct file *fp; 604 int events; 605 struct ucred *cred; 606 struct proc *p; 607 { 608 609 return (VOP_POLL(((struct vnode *)fp->f_data), events, cred, p)); 610 } 611 612 /* 613 * Check that the vnode is still valid, and if so 614 * acquire requested lock. 615 */ 616 int 617 #ifndef DEBUG_LOCKS 618 vn_lock(vp, flags, p) 619 #else 620 debug_vn_lock(vp, flags, p, filename, line) 621 #endif 622 struct vnode *vp; 623 int flags; 624 struct proc *p; 625 #ifdef DEBUG_LOCKS 626 const char *filename; 627 int line; 628 #endif 629 { 630 int error; 631 632 do { 633 if ((flags & LK_INTERLOCK) == 0) 634 mtx_lock(&vp->v_interlock); 635 if ((vp->v_flag & VXLOCK) && vp->v_vxproc != curproc) { 636 vp->v_flag |= VXWANT; 637 msleep(vp, &vp->v_interlock, PINOD | PDROP, 638 "vn_lock", 0); 639 error = ENOENT; 640 } else { 641 if (vp->v_vxproc != NULL) 642 printf("VXLOCK interlock avoided in vn_lock\n"); 643 #ifdef DEBUG_LOCKS 644 vp->filename = filename; 645 vp->line = line; 646 #endif 647 error = VOP_LOCK(vp, 648 flags | LK_NOPAUSE | LK_INTERLOCK, p); 649 if (error == 0) 650 return (error); 651 } 652 flags &= ~LK_INTERLOCK; 653 } while (flags & LK_RETRY); 654 return (error); 655 } 656 657 /* 658 * File table vnode close routine. 659 */ 660 static int 661 vn_closefile(fp, p) 662 struct file *fp; 663 struct proc *p; 664 { 665 666 fp->f_ops = &badfileops; 667 return (vn_close(((struct vnode *)fp->f_data), fp->f_flag, 668 fp->f_cred, p)); 669 } 670 671 /* 672 * Preparing to start a filesystem write operation. If the operation is 673 * permitted, then we bump the count of operations in progress and 674 * proceed. If a suspend request is in progress, we wait until the 675 * suspension is over, and then proceed. 676 */ 677 int 678 vn_start_write(vp, mpp, flags) 679 struct vnode *vp; 680 struct mount **mpp; 681 int flags; 682 { 683 struct mount *mp; 684 int error; 685 686 /* 687 * If a vnode is provided, get and return the mount point that 688 * to which it will write. 689 */ 690 if (vp != NULL) { 691 if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) { 692 *mpp = NULL; 693 if (error != EOPNOTSUPP) 694 return (error); 695 return (0); 696 } 697 } 698 if ((mp = *mpp) == NULL) 699 return (0); 700 /* 701 * Check on status of suspension. 702 */ 703 while ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) { 704 if (flags & V_NOWAIT) 705 return (EWOULDBLOCK); 706 error = tsleep(&mp->mnt_flag, (PUSER - 1) | (flags & PCATCH), 707 "suspfs", 0); 708 if (error) 709 return (error); 710 } 711 if (flags & V_XSLEEP) 712 return (0); 713 mp->mnt_writeopcount++; 714 return (0); 715 } 716 717 /* 718 * Secondary suspension. Used by operations such as vop_inactive 719 * routines that are needed by the higher level functions. These 720 * are allowed to proceed until all the higher level functions have 721 * completed (indicated by mnt_writeopcount dropping to zero). At that 722 * time, these operations are halted until the suspension is over. 723 */ 724 int 725 vn_write_suspend_wait(vp, mp, flags) 726 struct vnode *vp; 727 struct mount *mp; 728 int flags; 729 { 730 int error; 731 732 if (vp != NULL) { 733 if ((error = VOP_GETWRITEMOUNT(vp, &mp)) != 0) { 734 if (error != EOPNOTSUPP) 735 return (error); 736 return (0); 737 } 738 } 739 /* 740 * If we are not suspended or have not yet reached suspended 741 * mode, then let the operation proceed. 742 */ 743 if (mp == NULL || (mp->mnt_kern_flag & MNTK_SUSPENDED) == 0) 744 return (0); 745 if (flags & V_NOWAIT) 746 return (EWOULDBLOCK); 747 /* 748 * Wait for the suspension to finish. 749 */ 750 return (tsleep(&mp->mnt_flag, (PUSER - 1) | (flags & PCATCH), 751 "suspfs", 0)); 752 } 753 754 /* 755 * Filesystem write operation has completed. If we are suspending and this 756 * operation is the last one, notify the suspender that the suspension is 757 * now in effect. 758 */ 759 void 760 vn_finished_write(mp) 761 struct mount *mp; 762 { 763 764 if (mp == NULL) 765 return; 766 mp->mnt_writeopcount--; 767 if (mp->mnt_writeopcount < 0) 768 panic("vn_finished_write: neg cnt"); 769 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 && 770 mp->mnt_writeopcount <= 0) 771 wakeup(&mp->mnt_writeopcount); 772 } 773 774 /* 775 * Request a filesystem to suspend write operations. 776 */ 777 void 778 vfs_write_suspend(mp) 779 struct mount *mp; 780 { 781 struct proc *p = curproc; 782 783 if (mp->mnt_kern_flag & MNTK_SUSPEND) 784 return; 785 mp->mnt_kern_flag |= MNTK_SUSPEND; 786 if (mp->mnt_writeopcount > 0) 787 (void) tsleep(&mp->mnt_writeopcount, PUSER - 1, "suspwt", 0); 788 VFS_SYNC(mp, MNT_WAIT, p->p_ucred, p); 789 mp->mnt_kern_flag |= MNTK_SUSPENDED; 790 } 791 792 /* 793 * Request a filesystem to resume write operations. 794 */ 795 void 796 vfs_write_resume(mp) 797 struct mount *mp; 798 { 799 800 if ((mp->mnt_kern_flag & MNTK_SUSPEND) == 0) 801 return; 802 mp->mnt_kern_flag &= ~(MNTK_SUSPEND | MNTK_SUSPENDED); 803 wakeup(&mp->mnt_writeopcount); 804 wakeup(&mp->mnt_flag); 805 } 806 807 static int 808 vn_kqfilter(struct file *fp, struct knote *kn) 809 { 810 811 return (VOP_KQFILTER(((struct vnode *)fp->f_data), kn)); 812 } 813 814 /* 815 * Simplified in-kernel wrapper calls for extended attribute access. 816 * Both calls pass in a NULL credential, authorizing as "kernel" access. 817 * Set IO_NODELOCKED in ioflg if the vnode is already locked. 818 */ 819 int 820 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace, 821 const char *attrname, int *buflen, char *buf, struct proc *p) 822 { 823 struct uio auio; 824 struct iovec iov; 825 int error; 826 827 iov.iov_len = *buflen; 828 iov.iov_base = buf; 829 830 auio.uio_iov = &iov; 831 auio.uio_iovcnt = 1; 832 auio.uio_rw = UIO_READ; 833 auio.uio_segflg = UIO_SYSSPACE; 834 auio.uio_procp = p; 835 auio.uio_offset = 0; 836 auio.uio_resid = *buflen; 837 838 if ((ioflg & IO_NODELOCKED) == 0) 839 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 840 841 /* authorize attribute retrieval as kernel */ 842 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, p); 843 844 if ((ioflg & IO_NODELOCKED) == 0) 845 VOP_UNLOCK(vp, 0, p); 846 847 if (error == 0) { 848 *buflen = *buflen - auio.uio_resid; 849 } 850 851 return (error); 852 } 853 854 /* 855 * XXX failure mode if partially written? 856 */ 857 int 858 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace, 859 const char *attrname, int buflen, char *buf, struct proc *p) 860 { 861 struct uio auio; 862 struct iovec iov; 863 struct mount *mp; 864 int error; 865 866 iov.iov_len = buflen; 867 iov.iov_base = buf; 868 869 auio.uio_iov = &iov; 870 auio.uio_iovcnt = 1; 871 auio.uio_rw = UIO_WRITE; 872 auio.uio_segflg = UIO_SYSSPACE; 873 auio.uio_procp = p; 874 auio.uio_offset = 0; 875 auio.uio_resid = buflen; 876 877 if ((ioflg & IO_NODELOCKED) == 0) { 878 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0) 879 return (error); 880 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 881 } 882 883 /* authorize attribute setting as kernel */ 884 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, p); 885 886 if ((ioflg & IO_NODELOCKED) == 0) { 887 vn_finished_write(mp); 888 VOP_UNLOCK(vp, 0, p); 889 } 890 891 return (error); 892 } 893 894 int 895 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace, 896 const char *attrname, struct proc *p) 897 { 898 struct mount *mp; 899 int error; 900 901 if ((ioflg & IO_NODELOCKED) == 0) { 902 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0) 903 return (error); 904 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 905 } 906 907 /* authorize attribute removal as kernel */ 908 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL, NULL, p); 909 910 if ((ioflg & IO_NODELOCKED) == 0) { 911 vn_finished_write(mp); 912 VOP_UNLOCK(vp, 0, p); 913 } 914 915 return (error); 916 } 917