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 if (fp->f_flag & O_DIRECT) 356 ioflag |= IO_DIRECT; 357 VOP_LEASE(vp, p, cred, LEASE_READ); 358 vn_lock(vp, LK_SHARED | LK_NOPAUSE | LK_RETRY, p); 359 if ((flags & FOF_OFFSET) == 0) 360 uio->uio_offset = fp->f_offset; 361 362 ioflag |= sequential_heuristic(uio, fp); 363 364 error = VOP_READ(vp, uio, ioflag, cred); 365 if ((flags & FOF_OFFSET) == 0) 366 fp->f_offset = uio->uio_offset; 367 fp->f_nextoff = uio->uio_offset; 368 VOP_UNLOCK(vp, 0, p); 369 return (error); 370 } 371 372 /* 373 * File table vnode write routine. 374 */ 375 static int 376 vn_write(fp, uio, cred, flags, p) 377 struct file *fp; 378 struct uio *uio; 379 struct ucred *cred; 380 struct proc *p; 381 int flags; 382 { 383 struct vnode *vp; 384 struct mount *mp; 385 int error, ioflag; 386 387 KASSERT(uio->uio_procp == p, ("uio_procp %p is not p %p", 388 uio->uio_procp, p)); 389 vp = (struct vnode *)fp->f_data; 390 if (vp->v_type == VREG) 391 bwillwrite(); 392 vp = (struct vnode *)fp->f_data; /* XXX needed? */ 393 ioflag = IO_UNIT; 394 if (vp->v_type == VREG && (fp->f_flag & O_APPEND)) 395 ioflag |= IO_APPEND; 396 if (fp->f_flag & FNONBLOCK) 397 ioflag |= IO_NDELAY; 398 if (fp->f_flag & O_DIRECT) 399 ioflag |= IO_DIRECT; 400 if ((fp->f_flag & O_FSYNC) || 401 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))) 402 ioflag |= IO_SYNC; 403 mp = NULL; 404 if (vp->v_type != VCHR && 405 (error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) 406 return (error); 407 VOP_LEASE(vp, p, cred, LEASE_WRITE); 408 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 409 if ((flags & FOF_OFFSET) == 0) 410 uio->uio_offset = fp->f_offset; 411 ioflag |= sequential_heuristic(uio, fp); 412 error = VOP_WRITE(vp, uio, ioflag, cred); 413 if ((flags & FOF_OFFSET) == 0) 414 fp->f_offset = uio->uio_offset; 415 fp->f_nextoff = uio->uio_offset; 416 VOP_UNLOCK(vp, 0, p); 417 vn_finished_write(mp); 418 return (error); 419 } 420 421 /* 422 * File table vnode stat routine. 423 */ 424 static int 425 vn_statfile(fp, sb, p) 426 struct file *fp; 427 struct stat *sb; 428 struct proc *p; 429 { 430 struct vnode *vp = (struct vnode *)fp->f_data; 431 432 return vn_stat(vp, sb, p); 433 } 434 435 int 436 vn_stat(vp, sb, p) 437 struct vnode *vp; 438 register struct stat *sb; 439 struct proc *p; 440 { 441 struct vattr vattr; 442 register struct vattr *vap; 443 int error; 444 u_short mode; 445 446 vap = &vattr; 447 error = VOP_GETATTR(vp, vap, p->p_ucred, p); 448 if (error) 449 return (error); 450 451 /* 452 * Zero the spare stat fields 453 */ 454 sb->st_lspare = 0; 455 sb->st_qspare[0] = 0; 456 sb->st_qspare[1] = 0; 457 458 /* 459 * Copy from vattr table 460 */ 461 if (vap->va_fsid != VNOVAL) 462 sb->st_dev = vap->va_fsid; 463 else 464 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0]; 465 sb->st_ino = vap->va_fileid; 466 mode = vap->va_mode; 467 switch (vap->va_type) { 468 case VREG: 469 mode |= S_IFREG; 470 break; 471 case VDIR: 472 mode |= S_IFDIR; 473 break; 474 case VBLK: 475 mode |= S_IFBLK; 476 break; 477 case VCHR: 478 mode |= S_IFCHR; 479 break; 480 case VLNK: 481 mode |= S_IFLNK; 482 /* This is a cosmetic change, symlinks do not have a mode. */ 483 if (vp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) 484 sb->st_mode &= ~ACCESSPERMS; /* 0000 */ 485 else 486 sb->st_mode |= ACCESSPERMS; /* 0777 */ 487 break; 488 case VSOCK: 489 mode |= S_IFSOCK; 490 break; 491 case VFIFO: 492 mode |= S_IFIFO; 493 break; 494 default: 495 return (EBADF); 496 }; 497 sb->st_mode = mode; 498 sb->st_nlink = vap->va_nlink; 499 sb->st_uid = vap->va_uid; 500 sb->st_gid = vap->va_gid; 501 sb->st_rdev = vap->va_rdev; 502 sb->st_size = vap->va_size; 503 sb->st_atimespec = vap->va_atime; 504 sb->st_mtimespec = vap->va_mtime; 505 sb->st_ctimespec = vap->va_ctime; 506 507 /* 508 * According to www.opengroup.org, the meaning of st_blksize is 509 * "a filesystem-specific preferred I/O block size for this 510 * object. In some filesystem types, this may vary from file 511 * to file" 512 * Default to zero to catch bogus uses of this field. 513 */ 514 515 if (vap->va_type == VREG) { 516 sb->st_blksize = vap->va_blocksize; 517 } else if (vn_isdisk(vp, NULL)) { 518 sb->st_blksize = vp->v_rdev->si_bsize_best; 519 if (sb->st_blksize < vp->v_rdev->si_bsize_phys) 520 sb->st_blksize = vp->v_rdev->si_bsize_phys; 521 if (sb->st_blksize < BLKDEV_IOSIZE) 522 sb->st_blksize = BLKDEV_IOSIZE; 523 } else { 524 sb->st_blksize = 0; 525 } 526 527 sb->st_flags = vap->va_flags; 528 if (suser_xxx(p->p_ucred, 0, 0)) 529 sb->st_gen = 0; 530 else 531 sb->st_gen = vap->va_gen; 532 533 #if (S_BLKSIZE == 512) 534 /* Optimize this case */ 535 sb->st_blocks = vap->va_bytes >> 9; 536 #else 537 sb->st_blocks = vap->va_bytes / S_BLKSIZE; 538 #endif 539 return (0); 540 } 541 542 /* 543 * File table vnode ioctl routine. 544 */ 545 static int 546 vn_ioctl(fp, com, data, p) 547 struct file *fp; 548 u_long com; 549 caddr_t data; 550 struct proc *p; 551 { 552 register struct vnode *vp = ((struct vnode *)fp->f_data); 553 struct vattr vattr; 554 int error; 555 556 switch (vp->v_type) { 557 558 case VREG: 559 case VDIR: 560 if (com == FIONREAD) { 561 error = VOP_GETATTR(vp, &vattr, p->p_ucred, p); 562 if (error) 563 return (error); 564 *(int *)data = vattr.va_size - fp->f_offset; 565 return (0); 566 } 567 if (com == FIONBIO || com == FIOASYNC) /* XXX */ 568 return (0); /* XXX */ 569 /* fall into ... */ 570 571 default: 572 #if 0 573 return (ENOTTY); 574 #endif 575 case VFIFO: 576 case VCHR: 577 case VBLK: 578 if (com == FIODTYPE) { 579 if (vp->v_type != VCHR && vp->v_type != VBLK) 580 return (ENOTTY); 581 *(int *)data = devsw(vp->v_rdev)->d_flags & D_TYPEMASK; 582 return (0); 583 } 584 error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p); 585 if (error == 0 && com == TIOCSCTTY) { 586 587 /* Do nothing if reassigning same control tty */ 588 if (p->p_session->s_ttyvp == vp) 589 return (0); 590 591 /* Get rid of reference to old control tty */ 592 if (p->p_session->s_ttyvp) 593 vrele(p->p_session->s_ttyvp); 594 595 p->p_session->s_ttyvp = vp; 596 VREF(vp); 597 } 598 return (error); 599 } 600 } 601 602 /* 603 * File table vnode poll routine. 604 */ 605 static int 606 vn_poll(fp, events, cred, p) 607 struct file *fp; 608 int events; 609 struct ucred *cred; 610 struct proc *p; 611 { 612 613 return (VOP_POLL(((struct vnode *)fp->f_data), events, cred, p)); 614 } 615 616 /* 617 * Check that the vnode is still valid, and if so 618 * acquire requested lock. 619 */ 620 int 621 #ifndef DEBUG_LOCKS 622 vn_lock(vp, flags, p) 623 #else 624 debug_vn_lock(vp, flags, p, filename, line) 625 #endif 626 struct vnode *vp; 627 int flags; 628 struct proc *p; 629 #ifdef DEBUG_LOCKS 630 const char *filename; 631 int line; 632 #endif 633 { 634 int error; 635 636 do { 637 if ((flags & LK_INTERLOCK) == 0) 638 mtx_lock(&vp->v_interlock); 639 if ((vp->v_flag & VXLOCK) && vp->v_vxproc != curproc) { 640 vp->v_flag |= VXWANT; 641 msleep(vp, &vp->v_interlock, PINOD | PDROP, 642 "vn_lock", 0); 643 error = ENOENT; 644 } else { 645 if (vp->v_vxproc != NULL) 646 printf("VXLOCK interlock avoided in vn_lock\n"); 647 #ifdef DEBUG_LOCKS 648 vp->filename = filename; 649 vp->line = line; 650 #endif 651 error = VOP_LOCK(vp, 652 flags | LK_NOPAUSE | LK_INTERLOCK, p); 653 if (error == 0) 654 return (error); 655 } 656 flags &= ~LK_INTERLOCK; 657 } while (flags & LK_RETRY); 658 return (error); 659 } 660 661 /* 662 * File table vnode close routine. 663 */ 664 static int 665 vn_closefile(fp, p) 666 struct file *fp; 667 struct proc *p; 668 { 669 670 fp->f_ops = &badfileops; 671 return (vn_close(((struct vnode *)fp->f_data), fp->f_flag, 672 fp->f_cred, p)); 673 } 674 675 /* 676 * Preparing to start a filesystem write operation. If the operation is 677 * permitted, then we bump the count of operations in progress and 678 * proceed. If a suspend request is in progress, we wait until the 679 * suspension is over, and then proceed. 680 */ 681 int 682 vn_start_write(vp, mpp, flags) 683 struct vnode *vp; 684 struct mount **mpp; 685 int flags; 686 { 687 struct mount *mp; 688 int error; 689 690 /* 691 * If a vnode is provided, get and return the mount point that 692 * to which it will write. 693 */ 694 if (vp != NULL) { 695 if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) { 696 *mpp = NULL; 697 if (error != EOPNOTSUPP) 698 return (error); 699 return (0); 700 } 701 } 702 if ((mp = *mpp) == NULL) 703 return (0); 704 /* 705 * Check on status of suspension. 706 */ 707 while ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) { 708 if (flags & V_NOWAIT) 709 return (EWOULDBLOCK); 710 error = tsleep(&mp->mnt_flag, (PUSER - 1) | (flags & PCATCH), 711 "suspfs", 0); 712 if (error) 713 return (error); 714 } 715 if (flags & V_XSLEEP) 716 return (0); 717 mp->mnt_writeopcount++; 718 return (0); 719 } 720 721 /* 722 * Secondary suspension. Used by operations such as vop_inactive 723 * routines that are needed by the higher level functions. These 724 * are allowed to proceed until all the higher level functions have 725 * completed (indicated by mnt_writeopcount dropping to zero). At that 726 * time, these operations are halted until the suspension is over. 727 */ 728 int 729 vn_write_suspend_wait(vp, mp, flags) 730 struct vnode *vp; 731 struct mount *mp; 732 int flags; 733 { 734 int error; 735 736 if (vp != NULL) { 737 if ((error = VOP_GETWRITEMOUNT(vp, &mp)) != 0) { 738 if (error != EOPNOTSUPP) 739 return (error); 740 return (0); 741 } 742 } 743 /* 744 * If we are not suspended or have not yet reached suspended 745 * mode, then let the operation proceed. 746 */ 747 if (mp == NULL || (mp->mnt_kern_flag & MNTK_SUSPENDED) == 0) 748 return (0); 749 if (flags & V_NOWAIT) 750 return (EWOULDBLOCK); 751 /* 752 * Wait for the suspension to finish. 753 */ 754 return (tsleep(&mp->mnt_flag, (PUSER - 1) | (flags & PCATCH), 755 "suspfs", 0)); 756 } 757 758 /* 759 * Filesystem write operation has completed. If we are suspending and this 760 * operation is the last one, notify the suspender that the suspension is 761 * now in effect. 762 */ 763 void 764 vn_finished_write(mp) 765 struct mount *mp; 766 { 767 768 if (mp == NULL) 769 return; 770 mp->mnt_writeopcount--; 771 if (mp->mnt_writeopcount < 0) 772 panic("vn_finished_write: neg cnt"); 773 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 && 774 mp->mnt_writeopcount <= 0) 775 wakeup(&mp->mnt_writeopcount); 776 } 777 778 /* 779 * Request a filesystem to suspend write operations. 780 */ 781 void 782 vfs_write_suspend(mp) 783 struct mount *mp; 784 { 785 struct proc *p = curproc; 786 787 if (mp->mnt_kern_flag & MNTK_SUSPEND) 788 return; 789 mp->mnt_kern_flag |= MNTK_SUSPEND; 790 if (mp->mnt_writeopcount > 0) 791 (void) tsleep(&mp->mnt_writeopcount, PUSER - 1, "suspwt", 0); 792 VFS_SYNC(mp, MNT_WAIT, p->p_ucred, p); 793 mp->mnt_kern_flag |= MNTK_SUSPENDED; 794 } 795 796 /* 797 * Request a filesystem to resume write operations. 798 */ 799 void 800 vfs_write_resume(mp) 801 struct mount *mp; 802 { 803 804 if ((mp->mnt_kern_flag & MNTK_SUSPEND) == 0) 805 return; 806 mp->mnt_kern_flag &= ~(MNTK_SUSPEND | MNTK_SUSPENDED); 807 wakeup(&mp->mnt_writeopcount); 808 wakeup(&mp->mnt_flag); 809 } 810 811 static int 812 vn_kqfilter(struct file *fp, struct knote *kn) 813 { 814 815 return (VOP_KQFILTER(((struct vnode *)fp->f_data), kn)); 816 } 817 818 /* 819 * Simplified in-kernel wrapper calls for extended attribute access. 820 * Both calls pass in a NULL credential, authorizing as "kernel" access. 821 * Set IO_NODELOCKED in ioflg if the vnode is already locked. 822 */ 823 int 824 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace, 825 const char *attrname, int *buflen, char *buf, struct proc *p) 826 { 827 struct uio auio; 828 struct iovec iov; 829 int error; 830 831 iov.iov_len = *buflen; 832 iov.iov_base = buf; 833 834 auio.uio_iov = &iov; 835 auio.uio_iovcnt = 1; 836 auio.uio_rw = UIO_READ; 837 auio.uio_segflg = UIO_SYSSPACE; 838 auio.uio_procp = p; 839 auio.uio_offset = 0; 840 auio.uio_resid = *buflen; 841 842 if ((ioflg & IO_NODELOCKED) == 0) 843 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 844 845 /* authorize attribute retrieval as kernel */ 846 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, p); 847 848 if ((ioflg & IO_NODELOCKED) == 0) 849 VOP_UNLOCK(vp, 0, p); 850 851 if (error == 0) { 852 *buflen = *buflen - auio.uio_resid; 853 } 854 855 return (error); 856 } 857 858 /* 859 * XXX failure mode if partially written? 860 */ 861 int 862 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace, 863 const char *attrname, int buflen, char *buf, struct proc *p) 864 { 865 struct uio auio; 866 struct iovec iov; 867 struct mount *mp; 868 int error; 869 870 iov.iov_len = buflen; 871 iov.iov_base = buf; 872 873 auio.uio_iov = &iov; 874 auio.uio_iovcnt = 1; 875 auio.uio_rw = UIO_WRITE; 876 auio.uio_segflg = UIO_SYSSPACE; 877 auio.uio_procp = p; 878 auio.uio_offset = 0; 879 auio.uio_resid = buflen; 880 881 if ((ioflg & IO_NODELOCKED) == 0) { 882 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0) 883 return (error); 884 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 885 } 886 887 /* authorize attribute setting as kernel */ 888 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, p); 889 890 if ((ioflg & IO_NODELOCKED) == 0) { 891 vn_finished_write(mp); 892 VOP_UNLOCK(vp, 0, p); 893 } 894 895 return (error); 896 } 897 898 int 899 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace, 900 const char *attrname, struct proc *p) 901 { 902 struct mount *mp; 903 int error; 904 905 if ((ioflg & IO_NODELOCKED) == 0) { 906 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0) 907 return (error); 908 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 909 } 910 911 /* authorize attribute removal as kernel */ 912 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL, NULL, p); 913 914 if ((ioflg & IO_NODELOCKED) == 0) { 915 vn_finished_write(mp); 916 VOP_UNLOCK(vp, 0, p); 917 } 918 919 return (error); 920 } 921