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. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)vfs_vnops.c 8.2 (Berkeley) 1/21/94 35 * $FreeBSD: src/sys/kern/vfs_vnops.c,v 1.87.2.13 2002/12/29 18:19:53 dillon Exp $ 36 */ 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/fcntl.h> 41 #include <sys/file.h> 42 #include <sys/stat.h> 43 #include <sys/proc.h> 44 #include <sys/priv.h> 45 #include <sys/mount.h> 46 #include <sys/nlookup.h> 47 #include <sys/vnode.h> 48 #include <sys/buf.h> 49 #include <sys/filio.h> 50 #include <sys/ttycom.h> 51 #include <sys/conf.h> 52 #include <sys/sysctl.h> 53 #include <sys/syslog.h> 54 55 #include <sys/mplock2.h> 56 57 static int vn_closefile (struct file *fp); 58 static int vn_ioctl (struct file *fp, u_long com, caddr_t data, 59 struct ucred *cred, struct sysmsg *msg); 60 static int vn_read (struct file *fp, struct uio *uio, 61 struct ucred *cred, int flags); 62 static int vn_kqfilter (struct file *fp, struct knote *kn); 63 static int vn_statfile (struct file *fp, struct stat *sb, struct ucred *cred); 64 static int vn_write (struct file *fp, struct uio *uio, 65 struct ucred *cred, int flags); 66 67 struct fileops vnode_fileops = { 68 .fo_read = vn_read, 69 .fo_write = vn_write, 70 .fo_ioctl = vn_ioctl, 71 .fo_kqfilter = vn_kqfilter, 72 .fo_stat = vn_statfile, 73 .fo_close = vn_closefile, 74 .fo_shutdown = nofo_shutdown 75 }; 76 77 /* 78 * Common code for vnode open operations. Check permissions, and call 79 * the VOP_NOPEN or VOP_NCREATE routine. 80 * 81 * The caller is responsible for setting up nd with nlookup_init() and 82 * for cleaning it up with nlookup_done(), whether we return an error 83 * or not. 84 * 85 * On success nd->nl_open_vp will hold a referenced and, if requested, 86 * locked vnode. A locked vnode is requested via NLC_LOCKVP. If fp 87 * is non-NULL the vnode will be installed in the file pointer. 88 * 89 * NOTE: If the caller wishes the namecache entry to be operated with 90 * a shared lock it must use NLC_SHAREDLOCK. If NLC_LOCKVP is set 91 * then the vnode lock will also be shared. 92 * 93 * NOTE: The vnode is referenced just once on return whether or not it 94 * is also installed in the file pointer. 95 */ 96 int 97 vn_open(struct nlookupdata *nd, struct file *fp, int fmode, int cmode) 98 { 99 struct vnode *vp; 100 struct ucred *cred = nd->nl_cred; 101 struct vattr vat; 102 struct vattr *vap = &vat; 103 int error; 104 u_int flags; 105 uint64_t osize; 106 struct mount *mp; 107 108 /* 109 * Certain combinations are illegal 110 */ 111 if ((fmode & (FWRITE | O_TRUNC)) == O_TRUNC) 112 return(EACCES); 113 114 /* 115 * Lookup the path and create or obtain the vnode. After a 116 * successful lookup a locked nd->nl_nch will be returned. 117 * 118 * The result of this section should be a locked vnode. 119 * 120 * XXX with only a little work we should be able to avoid locking 121 * the vnode if FWRITE, O_CREAT, and O_TRUNC are *not* set. 122 */ 123 nd->nl_flags |= NLC_OPEN; 124 if (fmode & O_APPEND) 125 nd->nl_flags |= NLC_APPEND; 126 if (fmode & O_TRUNC) 127 nd->nl_flags |= NLC_TRUNCATE; 128 if (fmode & FREAD) 129 nd->nl_flags |= NLC_READ; 130 if (fmode & FWRITE) 131 nd->nl_flags |= NLC_WRITE; 132 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0) 133 nd->nl_flags |= NLC_FOLLOW; 134 135 if (fmode & O_CREAT) { 136 /* 137 * CONDITIONAL CREATE FILE CASE 138 * 139 * Setting NLC_CREATE causes a negative hit to store 140 * the negative hit ncp and not return an error. Then 141 * nc_error or nc_vp may be checked to see if the ncp 142 * represents a negative hit. NLC_CREATE also requires 143 * write permission on the governing directory or EPERM 144 * is returned. 145 */ 146 nd->nl_flags |= NLC_CREATE; 147 nd->nl_flags |= NLC_REFDVP; 148 bwillinode(1); 149 error = nlookup(nd); 150 } else { 151 /* 152 * NORMAL OPEN FILE CASE 153 */ 154 error = nlookup(nd); 155 } 156 157 if (error) 158 return (error); 159 160 /* 161 * split case to allow us to re-resolve and retry the ncp in case 162 * we get ESTALE. 163 */ 164 again: 165 if (fmode & O_CREAT) { 166 if (nd->nl_nch.ncp->nc_vp == NULL) { 167 if ((error = ncp_writechk(&nd->nl_nch)) != 0) 168 return (error); 169 VATTR_NULL(vap); 170 vap->va_type = VREG; 171 vap->va_mode = cmode; 172 if (fmode & O_EXCL) 173 vap->va_vaflags |= VA_EXCLUSIVE; 174 error = VOP_NCREATE(&nd->nl_nch, nd->nl_dvp, &vp, 175 nd->nl_cred, vap); 176 if (error) 177 return (error); 178 fmode &= ~O_TRUNC; 179 /* locked vnode is returned */ 180 } else { 181 if (fmode & O_EXCL) { 182 error = EEXIST; 183 } else { 184 error = cache_vget(&nd->nl_nch, cred, 185 LK_EXCLUSIVE, &vp); 186 } 187 if (error) 188 return (error); 189 fmode &= ~O_CREAT; 190 } 191 } else { 192 if (nd->nl_flags & NLC_SHAREDLOCK) { 193 error = cache_vget(&nd->nl_nch, cred, LK_SHARED, &vp); 194 } else { 195 error = cache_vget(&nd->nl_nch, cred, 196 LK_EXCLUSIVE, &vp); 197 } 198 if (error) 199 return (error); 200 } 201 202 /* 203 * We have a locked vnode and ncp now. Note that the ncp will 204 * be cleaned up by the caller if nd->nl_nch is left intact. 205 */ 206 if (vp->v_type == VLNK) { 207 error = EMLINK; 208 goto bad; 209 } 210 if (vp->v_type == VSOCK) { 211 error = EOPNOTSUPP; 212 goto bad; 213 } 214 if (vp->v_type != VDIR && (fmode & O_DIRECTORY)) { 215 error = ENOTDIR; 216 goto bad; 217 } 218 if ((fmode & O_CREAT) == 0) { 219 if (fmode & (FWRITE | O_TRUNC)) { 220 if (vp->v_type == VDIR) { 221 error = EISDIR; 222 goto bad; 223 } 224 error = vn_writechk(vp, &nd->nl_nch); 225 if (error) { 226 /* 227 * Special stale handling, re-resolve the 228 * vnode. 229 */ 230 if (error == ESTALE) { 231 vput(vp); 232 vp = NULL; 233 if (nd->nl_flags & NLC_SHAREDLOCK) { 234 cache_unlock(&nd->nl_nch); 235 cache_lock(&nd->nl_nch); 236 } 237 cache_setunresolved(&nd->nl_nch); 238 error = cache_resolve(&nd->nl_nch, 239 cred); 240 if (error == 0) 241 goto again; 242 } 243 goto bad; 244 } 245 } 246 } 247 if (fmode & O_TRUNC) { 248 vn_unlock(vp); /* XXX */ 249 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */ 250 osize = vp->v_filesize; 251 VATTR_NULL(vap); 252 vap->va_size = 0; 253 error = VOP_SETATTR(vp, vap, cred); 254 if (error) 255 goto bad; 256 error = VOP_GETATTR(vp, vap); 257 if (error) 258 goto bad; 259 mp = vq_vptomp(vp); 260 VFS_ACCOUNT(mp, vap->va_uid, vap->va_gid, -osize); 261 } 262 263 /* 264 * Set or clear VNSWAPCACHE on the vp based on nd->nl_nch.ncp->nc_flag. 265 * These particular bits a tracked all the way from the root. 266 * 267 * NOTE: Might not work properly on NFS servers due to the 268 * disconnected namecache. 269 */ 270 flags = nd->nl_nch.ncp->nc_flag; 271 if ((flags & (NCF_UF_CACHE | NCF_UF_PCACHE)) && 272 (flags & (NCF_SF_NOCACHE | NCF_SF_PNOCACHE)) == 0) { 273 vsetflags(vp, VSWAPCACHE); 274 } else { 275 vclrflags(vp, VSWAPCACHE); 276 } 277 278 /* 279 * Setup the fp so VOP_OPEN can override it. No descriptor has been 280 * associated with the fp yet so we own it clean. 281 * 282 * f_nchandle inherits nl_nch. This used to be necessary only for 283 * directories but now we do it unconditionally so f*() ops 284 * such as fchmod() can access the actual namespace that was 285 * used to open the file. 286 */ 287 if (fp) { 288 if (nd->nl_flags & NLC_APPENDONLY) 289 fmode |= FAPPENDONLY; 290 fp->f_nchandle = nd->nl_nch; 291 cache_zero(&nd->nl_nch); 292 cache_unlock(&fp->f_nchandle); 293 } 294 295 /* 296 * Get rid of nl_nch. vn_open does not return it (it returns the 297 * vnode or the file pointer). Note: we can't leave nl_nch locked 298 * through the VOP_OPEN anyway since the VOP_OPEN may block, e.g. 299 * on /dev/ttyd0 300 */ 301 if (nd->nl_nch.ncp) 302 cache_put(&nd->nl_nch); 303 304 error = VOP_OPEN(vp, fmode, cred, fp); 305 if (error) { 306 /* 307 * setting f_ops to &badfileops will prevent the descriptor 308 * code from trying to close and release the vnode, since 309 * the open failed we do not want to call close. 310 */ 311 if (fp) { 312 fp->f_data = NULL; 313 fp->f_ops = &badfileops; 314 } 315 goto bad; 316 } 317 318 #if 0 319 /* 320 * Assert that VREG files have been setup for vmio. 321 */ 322 KASSERT(vp->v_type != VREG || vp->v_object != NULL, 323 ("vn_open: regular file was not VMIO enabled!")); 324 #endif 325 326 /* 327 * Return the vnode. XXX needs some cleaning up. The vnode is 328 * only returned in the fp == NULL case. 329 */ 330 if (fp == NULL) { 331 nd->nl_open_vp = vp; 332 nd->nl_vp_fmode = fmode; 333 if ((nd->nl_flags & NLC_LOCKVP) == 0) 334 vn_unlock(vp); 335 } else { 336 vput(vp); 337 } 338 return (0); 339 bad: 340 if (vp) 341 vput(vp); 342 return (error); 343 } 344 345 int 346 vn_opendisk(const char *devname, int fmode, struct vnode **vpp) 347 { 348 struct vnode *vp; 349 int error; 350 351 if (strncmp(devname, "/dev/", 5) == 0) 352 devname += 5; 353 if ((vp = getsynthvnode(devname)) == NULL) { 354 error = ENODEV; 355 } else { 356 error = VOP_OPEN(vp, fmode, proc0.p_ucred, NULL); 357 vn_unlock(vp); 358 if (error) { 359 vrele(vp); 360 vp = NULL; 361 } 362 } 363 *vpp = vp; 364 return (error); 365 } 366 367 /* 368 * Check for write permissions on the specified vnode. nch may be NULL. 369 */ 370 int 371 vn_writechk(struct vnode *vp, struct nchandle *nch) 372 { 373 /* 374 * If there's shared text associated with 375 * the vnode, try to free it up once. If 376 * we fail, we can't allow writing. 377 */ 378 if (vp->v_flag & VTEXT) 379 return (ETXTBSY); 380 381 /* 382 * If the vnode represents a regular file, check the mount 383 * point via the nch. This may be a different mount point 384 * then the one embedded in the vnode (e.g. nullfs). 385 * 386 * We can still write to non-regular files (e.g. devices) 387 * via read-only mounts. 388 */ 389 if (nch && nch->ncp && vp->v_type == VREG) 390 return (ncp_writechk(nch)); 391 return (0); 392 } 393 394 /* 395 * Check whether the underlying mount is read-only. The mount point 396 * referenced by the namecache may be different from the mount point 397 * used by the underlying vnode in the case of NULLFS, so a separate 398 * check is needed. 399 */ 400 int 401 ncp_writechk(struct nchandle *nch) 402 { 403 struct mount *mp; 404 405 if ((mp = nch->mount) != NULL) { 406 if (mp->mnt_flag & MNT_RDONLY) 407 return (EROFS); 408 if (mp->mnt_op->vfs_modifying != vfs_stdmodifying) 409 VFS_MODIFYING(mp); 410 } 411 return(0); 412 } 413 414 /* 415 * Vnode close call 416 * 417 * MPSAFE 418 */ 419 int 420 vn_close(struct vnode *vp, int flags, struct file *fp) 421 { 422 int error; 423 424 error = vn_lock(vp, LK_SHARED | LK_RETRY | LK_FAILRECLAIM); 425 if (error == 0) { 426 error = VOP_CLOSE(vp, flags, fp); 427 vn_unlock(vp); 428 } 429 vrele(vp); 430 return (error); 431 } 432 433 /* 434 * Sequential heuristic. 435 * 436 * MPSAFE (f_seqcount and f_nextoff are allowed to race) 437 */ 438 static __inline 439 int 440 sequential_heuristic(struct uio *uio, struct file *fp) 441 { 442 /* 443 * Sequential heuristic - detect sequential operation 444 * 445 * NOTE: SMP: We allow f_seqcount updates to race. 446 */ 447 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) || 448 uio->uio_offset == fp->f_nextoff) { 449 int tmpseq = fp->f_seqcount; 450 451 tmpseq += (uio->uio_resid + MAXBSIZE - 1) / MAXBSIZE; 452 if (tmpseq > IO_SEQMAX) 453 tmpseq = IO_SEQMAX; 454 fp->f_seqcount = tmpseq; 455 return(fp->f_seqcount << IO_SEQSHIFT); 456 } 457 458 /* 459 * Not sequential, quick draw-down of seqcount 460 * 461 * NOTE: SMP: We allow f_seqcount updates to race. 462 */ 463 if (fp->f_seqcount > 1) 464 fp->f_seqcount = 1; 465 else 466 fp->f_seqcount = 0; 467 return(0); 468 } 469 470 /* 471 * get - lock and return the f_offset field. 472 * set - set and unlock the f_offset field. 473 * 474 * These routines serve the dual purpose of serializing access to the 475 * f_offset field (at least on x86) and guaranteeing operational integrity 476 * when multiple read()ers and write()ers are present on the same fp. 477 * 478 * MPSAFE 479 */ 480 static __inline off_t 481 vn_get_fpf_offset(struct file *fp) 482 { 483 u_int flags; 484 u_int nflags; 485 486 /* 487 * Shortcut critical path. 488 */ 489 flags = fp->f_flag & ~FOFFSETLOCK; 490 if (atomic_cmpset_int(&fp->f_flag, flags, flags | FOFFSETLOCK)) 491 return(fp->f_offset); 492 493 /* 494 * The hard way 495 */ 496 for (;;) { 497 flags = fp->f_flag; 498 if (flags & FOFFSETLOCK) { 499 nflags = flags | FOFFSETWAKE; 500 tsleep_interlock(&fp->f_flag, 0); 501 if (atomic_cmpset_int(&fp->f_flag, flags, nflags)) 502 tsleep(&fp->f_flag, PINTERLOCKED, "fpoff", 0); 503 } else { 504 nflags = flags | FOFFSETLOCK; 505 if (atomic_cmpset_int(&fp->f_flag, flags, nflags)) 506 break; 507 } 508 } 509 return(fp->f_offset); 510 } 511 512 /* 513 * MPSAFE 514 */ 515 static __inline void 516 vn_set_fpf_offset(struct file *fp, off_t offset) 517 { 518 u_int flags; 519 u_int nflags; 520 521 /* 522 * We hold the lock so we can set the offset without interference. 523 */ 524 fp->f_offset = offset; 525 526 /* 527 * Normal release is already a reasonably critical path. 528 */ 529 for (;;) { 530 flags = fp->f_flag; 531 nflags = flags & ~(FOFFSETLOCK | FOFFSETWAKE); 532 if (atomic_cmpset_int(&fp->f_flag, flags, nflags)) { 533 if (flags & FOFFSETWAKE) 534 wakeup(&fp->f_flag); 535 break; 536 } 537 } 538 } 539 540 /* 541 * MPSAFE 542 */ 543 static __inline off_t 544 vn_poll_fpf_offset(struct file *fp) 545 { 546 #if defined(__x86_64__) 547 return(fp->f_offset); 548 #else 549 off_t off = vn_get_fpf_offset(fp); 550 vn_set_fpf_offset(fp, off); 551 return(off); 552 #endif 553 } 554 555 /* 556 * Package up an I/O request on a vnode into a uio and do it. 557 * 558 * MPSAFE 559 */ 560 int 561 vn_rdwr(enum uio_rw rw, struct vnode *vp, caddr_t base, int len, 562 off_t offset, enum uio_seg segflg, int ioflg, 563 struct ucred *cred, int *aresid) 564 { 565 struct uio auio; 566 struct iovec aiov; 567 int error; 568 569 if ((ioflg & IO_NODELOCKED) == 0) 570 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 571 auio.uio_iov = &aiov; 572 auio.uio_iovcnt = 1; 573 aiov.iov_base = base; 574 aiov.iov_len = len; 575 auio.uio_resid = len; 576 auio.uio_offset = offset; 577 auio.uio_segflg = segflg; 578 auio.uio_rw = rw; 579 auio.uio_td = curthread; 580 if (rw == UIO_READ) { 581 error = VOP_READ(vp, &auio, ioflg, cred); 582 } else { 583 error = VOP_WRITE(vp, &auio, ioflg, cred); 584 } 585 if (aresid) 586 *aresid = auio.uio_resid; 587 else 588 if (auio.uio_resid && error == 0) 589 error = EIO; 590 if ((ioflg & IO_NODELOCKED) == 0) 591 vn_unlock(vp); 592 return (error); 593 } 594 595 /* 596 * Package up an I/O request on a vnode into a uio and do it. The I/O 597 * request is split up into smaller chunks and we try to avoid saturating 598 * the buffer cache while potentially holding a vnode locked, so we 599 * check bwillwrite() before calling vn_rdwr(). We also call lwkt_user_yield() 600 * to give other processes a chance to lock the vnode (either other processes 601 * core'ing the same binary, or unrelated processes scanning the directory). 602 * 603 * MPSAFE 604 */ 605 int 606 vn_rdwr_inchunks(enum uio_rw rw, struct vnode *vp, caddr_t base, int len, 607 off_t offset, enum uio_seg segflg, int ioflg, 608 struct ucred *cred, int *aresid) 609 { 610 int error = 0; 611 612 do { 613 int chunk; 614 615 /* 616 * Force `offset' to a multiple of MAXBSIZE except possibly 617 * for the first chunk, so that filesystems only need to 618 * write full blocks except possibly for the first and last 619 * chunks. 620 */ 621 chunk = MAXBSIZE - (uoff_t)offset % MAXBSIZE; 622 623 if (chunk > len) 624 chunk = len; 625 if (vp->v_type == VREG && (ioflg & IO_RECURSE) == 0) { 626 switch(rw) { 627 case UIO_READ: 628 bwillread(chunk); 629 break; 630 case UIO_WRITE: 631 bwillwrite(chunk); 632 break; 633 } 634 } 635 error = vn_rdwr(rw, vp, base, chunk, offset, segflg, 636 ioflg, cred, aresid); 637 len -= chunk; /* aresid calc already includes length */ 638 if (error) 639 break; 640 offset += chunk; 641 base += chunk; 642 lwkt_user_yield(); 643 } while (len); 644 if (aresid) 645 *aresid += len; 646 return (error); 647 } 648 649 /* 650 * File pointers can no longer get ripped up by revoke so 651 * we don't need to lock access to the vp. 652 * 653 * f_offset updates are not guaranteed against multiple readers 654 */ 655 static int 656 vn_read(struct file *fp, struct uio *uio, struct ucred *cred, int flags) 657 { 658 struct vnode *vp; 659 int error, ioflag; 660 661 KASSERT(uio->uio_td == curthread, 662 ("uio_td %p is not td %p", uio->uio_td, curthread)); 663 vp = (struct vnode *)fp->f_data; 664 665 ioflag = 0; 666 if (flags & O_FBLOCKING) { 667 /* ioflag &= ~IO_NDELAY; */ 668 } else if (flags & O_FNONBLOCKING) { 669 ioflag |= IO_NDELAY; 670 } else if (fp->f_flag & FNONBLOCK) { 671 ioflag |= IO_NDELAY; 672 } 673 if (fp->f_flag & O_DIRECT) { 674 ioflag |= IO_DIRECT; 675 } 676 if ((flags & O_FOFFSET) == 0 && (vp->v_flag & VNOTSEEKABLE) == 0) 677 uio->uio_offset = vn_get_fpf_offset(fp); 678 vn_lock(vp, LK_SHARED | LK_RETRY); 679 ioflag |= sequential_heuristic(uio, fp); 680 681 error = VOP_READ(vp, uio, ioflag, cred); 682 fp->f_nextoff = uio->uio_offset; 683 vn_unlock(vp); 684 if ((flags & O_FOFFSET) == 0 && (vp->v_flag & VNOTSEEKABLE) == 0) 685 vn_set_fpf_offset(fp, uio->uio_offset); 686 return (error); 687 } 688 689 /* 690 * MPSAFE 691 */ 692 static int 693 vn_write(struct file *fp, struct uio *uio, struct ucred *cred, int flags) 694 { 695 struct vnode *vp; 696 int error, ioflag; 697 698 KASSERT(uio->uio_td == curthread, 699 ("uio_td %p is not p %p", uio->uio_td, curthread)); 700 vp = (struct vnode *)fp->f_data; 701 702 ioflag = IO_UNIT; 703 if (vp->v_type == VREG && 704 ((fp->f_flag & O_APPEND) || (flags & O_FAPPEND))) { 705 ioflag |= IO_APPEND; 706 } 707 708 if (flags & O_FBLOCKING) { 709 /* ioflag &= ~IO_NDELAY; */ 710 } else if (flags & O_FNONBLOCKING) { 711 ioflag |= IO_NDELAY; 712 } else if (fp->f_flag & FNONBLOCK) { 713 ioflag |= IO_NDELAY; 714 } 715 if (fp->f_flag & O_DIRECT) { 716 ioflag |= IO_DIRECT; 717 } 718 if (flags & O_FASYNCWRITE) { 719 /* ioflag &= ~IO_SYNC; */ 720 } else if (flags & O_FSYNCWRITE) { 721 ioflag |= IO_SYNC; 722 } else if (fp->f_flag & O_FSYNC) { 723 ioflag |= IO_SYNC; 724 } 725 726 if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)) 727 ioflag |= IO_SYNC; 728 if ((flags & O_FOFFSET) == 0) 729 uio->uio_offset = vn_get_fpf_offset(fp); 730 if (vp->v_mount) 731 VFS_MODIFYING(vp->v_mount); 732 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 733 ioflag |= sequential_heuristic(uio, fp); 734 error = VOP_WRITE(vp, uio, ioflag, cred); 735 fp->f_nextoff = uio->uio_offset; 736 vn_unlock(vp); 737 if ((flags & O_FOFFSET) == 0) 738 vn_set_fpf_offset(fp, uio->uio_offset); 739 return (error); 740 } 741 742 /* 743 * MPSAFE 744 */ 745 static int 746 vn_statfile(struct file *fp, struct stat *sb, struct ucred *cred) 747 { 748 struct vnode *vp; 749 int error; 750 751 vp = (struct vnode *)fp->f_data; 752 error = vn_stat(vp, sb, cred); 753 return (error); 754 } 755 756 /* 757 * MPSAFE 758 */ 759 int 760 vn_stat(struct vnode *vp, struct stat *sb, struct ucred *cred) 761 { 762 struct vattr vattr; 763 struct vattr *vap; 764 int error; 765 u_short mode; 766 cdev_t dev; 767 768 vap = &vattr; 769 error = VOP_GETATTR(vp, vap); 770 if (error) 771 return (error); 772 773 /* 774 * Zero the spare stat fields 775 */ 776 sb->st_lspare = 0; 777 sb->st_qspare1 = 0; 778 sb->st_qspare2 = 0; 779 780 /* 781 * Copy from vattr table 782 */ 783 if (vap->va_fsid != VNOVAL) 784 sb->st_dev = vap->va_fsid; 785 else 786 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0]; 787 sb->st_ino = vap->va_fileid; 788 mode = vap->va_mode; 789 switch (vap->va_type) { 790 case VREG: 791 mode |= S_IFREG; 792 break; 793 case VDATABASE: 794 mode |= S_IFDB; 795 break; 796 case VDIR: 797 mode |= S_IFDIR; 798 break; 799 case VBLK: 800 mode |= S_IFBLK; 801 break; 802 case VCHR: 803 mode |= S_IFCHR; 804 break; 805 case VLNK: 806 mode |= S_IFLNK; 807 /* This is a cosmetic change, symlinks do not have a mode. */ 808 if (vp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) 809 sb->st_mode &= ~ACCESSPERMS; /* 0000 */ 810 else 811 sb->st_mode |= ACCESSPERMS; /* 0777 */ 812 break; 813 case VSOCK: 814 mode |= S_IFSOCK; 815 break; 816 case VFIFO: 817 mode |= S_IFIFO; 818 break; 819 default: 820 return (EBADF); 821 } 822 sb->st_mode = mode; 823 if (vap->va_nlink > (nlink_t)-1) 824 sb->st_nlink = (nlink_t)-1; 825 else 826 sb->st_nlink = vap->va_nlink; 827 sb->st_uid = vap->va_uid; 828 sb->st_gid = vap->va_gid; 829 sb->st_rdev = dev2udev(vp->v_rdev); 830 sb->st_size = vap->va_size; 831 sb->st_atimespec = vap->va_atime; 832 sb->st_mtimespec = vap->va_mtime; 833 sb->st_ctimespec = vap->va_ctime; 834 835 /* 836 * A VCHR and VBLK device may track the last access and last modified 837 * time independantly of the filesystem. This is particularly true 838 * because device read and write calls may bypass the filesystem. 839 */ 840 if (vp->v_type == VCHR || vp->v_type == VBLK) { 841 dev = vp->v_rdev; 842 if (dev != NULL) { 843 if (dev->si_lastread) { 844 sb->st_atimespec.tv_sec = time_second + 845 (time_uptime - 846 dev->si_lastread); 847 sb->st_atimespec.tv_nsec = 0; 848 } 849 if (dev->si_lastwrite) { 850 sb->st_atimespec.tv_sec = time_second + 851 (time_uptime - 852 dev->si_lastwrite); 853 sb->st_atimespec.tv_nsec = 0; 854 } 855 } 856 } 857 858 /* 859 * According to www.opengroup.org, the meaning of st_blksize is 860 * "a filesystem-specific preferred I/O block size for this 861 * object. In some filesystem types, this may vary from file 862 * to file" 863 * Default to PAGE_SIZE after much discussion. 864 */ 865 866 if (vap->va_type == VREG) { 867 sb->st_blksize = vap->va_blocksize; 868 } else if (vn_isdisk(vp, NULL)) { 869 /* 870 * XXX this is broken. If the device is not yet open (aka 871 * stat() call, aka v_rdev == NULL), how are we supposed 872 * to get a valid block size out of it? 873 */ 874 dev = vp->v_rdev; 875 876 sb->st_blksize = dev->si_bsize_best; 877 if (sb->st_blksize < dev->si_bsize_phys) 878 sb->st_blksize = dev->si_bsize_phys; 879 if (sb->st_blksize < BLKDEV_IOSIZE) 880 sb->st_blksize = BLKDEV_IOSIZE; 881 } else { 882 sb->st_blksize = PAGE_SIZE; 883 } 884 885 sb->st_flags = vap->va_flags; 886 887 error = priv_check_cred(cred, PRIV_VFS_GENERATION, 0); 888 if (error) 889 sb->st_gen = 0; 890 else 891 sb->st_gen = (u_int32_t)vap->va_gen; 892 893 sb->st_blocks = vap->va_bytes / S_BLKSIZE; 894 return (0); 895 } 896 897 /* 898 * MPALMOSTSAFE - acquires mplock 899 */ 900 static int 901 vn_ioctl(struct file *fp, u_long com, caddr_t data, struct ucred *ucred, 902 struct sysmsg *msg) 903 { 904 struct vnode *vp = ((struct vnode *)fp->f_data); 905 struct vnode *ovp; 906 struct vattr vattr; 907 int error; 908 off_t size; 909 910 switch (vp->v_type) { 911 case VREG: 912 case VDIR: 913 if (com == FIONREAD) { 914 error = VOP_GETATTR(vp, &vattr); 915 if (error) 916 break; 917 size = vattr.va_size; 918 if ((vp->v_flag & VNOTSEEKABLE) == 0) 919 size -= vn_poll_fpf_offset(fp); 920 if (size > 0x7FFFFFFF) 921 size = 0x7FFFFFFF; 922 *(int *)data = size; 923 error = 0; 924 break; 925 } 926 if (com == FIOASYNC) { /* XXX */ 927 error = 0; /* XXX */ 928 break; 929 } 930 /* fall into ... */ 931 default: 932 #if 0 933 return (ENOTTY); 934 #endif 935 case VFIFO: 936 case VCHR: 937 case VBLK: 938 if (com == FIODTYPE) { 939 if (vp->v_type != VCHR && vp->v_type != VBLK) { 940 error = ENOTTY; 941 break; 942 } 943 *(int *)data = dev_dflags(vp->v_rdev) & D_TYPEMASK; 944 error = 0; 945 break; 946 } 947 error = VOP_IOCTL(vp, com, data, fp->f_flag, ucred, msg); 948 if (error == 0 && com == TIOCSCTTY) { 949 struct proc *p = curthread->td_proc; 950 struct session *sess; 951 952 if (p == NULL) { 953 error = ENOTTY; 954 break; 955 } 956 957 get_mplock(); 958 sess = p->p_session; 959 /* Do nothing if reassigning same control tty */ 960 if (sess->s_ttyvp == vp) { 961 error = 0; 962 rel_mplock(); 963 break; 964 } 965 966 /* Get rid of reference to old control tty */ 967 ovp = sess->s_ttyvp; 968 vref(vp); 969 sess->s_ttyvp = vp; 970 if (ovp) 971 vrele(ovp); 972 rel_mplock(); 973 } 974 break; 975 } 976 return (error); 977 } 978 979 /* 980 * Obtain the requested vnode lock 981 * 982 * LK_RETRY Automatically retry on timeout 983 * LK_FAILRECLAIM Fail if the vnode is being reclaimed 984 * 985 * Failures will occur if the vnode is undergoing recyclement, but not 986 * all callers expect that the function will fail so the caller must pass 987 * LK_FAILOK if it wants to process an error code. 988 * 989 * Errors can occur for other reasons if you pass in other LK_ flags, 990 * regardless of whether you pass in LK_FAILRECLAIM 991 */ 992 int 993 vn_lock(struct vnode *vp, int flags) 994 { 995 int error; 996 997 do { 998 error = lockmgr(&vp->v_lock, flags); 999 if (error == 0) 1000 break; 1001 } while (flags & LK_RETRY); 1002 1003 /* 1004 * Because we (had better!) have a ref on the vnode, once it 1005 * goes to VRECLAIMED state it will not be recycled until all 1006 * refs go away. So we can just check the flag. 1007 */ 1008 if (error == 0 && (vp->v_flag & VRECLAIMED)) { 1009 if (flags & LK_FAILRECLAIM) { 1010 lockmgr(&vp->v_lock, LK_RELEASE); 1011 error = ENOENT; 1012 } 1013 } 1014 return (error); 1015 } 1016 1017 #ifdef DEBUG_VN_UNLOCK 1018 1019 void 1020 debug_vn_unlock(struct vnode *vp, const char *filename, int line) 1021 { 1022 kprintf("vn_unlock from %s:%d\n", filename, line); 1023 lockmgr(&vp->v_lock, LK_RELEASE); 1024 } 1025 1026 #else 1027 1028 void 1029 vn_unlock(struct vnode *vp) 1030 { 1031 lockmgr(&vp->v_lock, LK_RELEASE); 1032 } 1033 1034 #endif 1035 1036 /* 1037 * MPSAFE 1038 */ 1039 int 1040 vn_islocked(struct vnode *vp) 1041 { 1042 return (lockstatus(&vp->v_lock, curthread)); 1043 } 1044 1045 /* 1046 * Return the lock status of a vnode and unlock the vnode 1047 * if we owned the lock. This is not a boolean, if the 1048 * caller cares what the lock status is the caller must 1049 * check the various possible values. 1050 * 1051 * This only unlocks exclusive locks held by the caller, 1052 * it will NOT unlock shared locks (there is no way to 1053 * tell who the shared lock belongs to). 1054 * 1055 * MPSAFE 1056 */ 1057 int 1058 vn_islocked_unlock(struct vnode *vp) 1059 { 1060 int vpls; 1061 1062 vpls = lockstatus(&vp->v_lock, curthread); 1063 if (vpls == LK_EXCLUSIVE) 1064 lockmgr(&vp->v_lock, LK_RELEASE); 1065 return(vpls); 1066 } 1067 1068 /* 1069 * Restore a vnode lock that we previously released via 1070 * vn_islocked_unlock(). This is a NOP if we did not 1071 * own the original lock. 1072 * 1073 * MPSAFE 1074 */ 1075 void 1076 vn_islocked_relock(struct vnode *vp, int vpls) 1077 { 1078 int error; 1079 1080 if (vpls == LK_EXCLUSIVE) 1081 error = lockmgr(&vp->v_lock, vpls); 1082 } 1083 1084 /* 1085 * MPSAFE 1086 */ 1087 static int 1088 vn_closefile(struct file *fp) 1089 { 1090 int error; 1091 1092 fp->f_ops = &badfileops; 1093 error = vn_close(((struct vnode *)fp->f_data), fp->f_flag, fp); 1094 return (error); 1095 } 1096 1097 /* 1098 * MPSAFE 1099 */ 1100 static int 1101 vn_kqfilter(struct file *fp, struct knote *kn) 1102 { 1103 int error; 1104 1105 error = VOP_KQFILTER(((struct vnode *)fp->f_data), kn); 1106 return (error); 1107 } 1108