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.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
38 * @(#)vfs_vnops.c 8.2 (Berkeley) 1/21/94
39 * $FreeBSD: src/sys/kern/vfs_vnops.c,v 1.87.2.13 2002/12/29 18:19:53 dillon Exp $
40 * $DragonFly: src/sys/kern/vfs_vnops.c,v 1.58 2008/06/28 17:59:49 dillon Exp $
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/fcntl.h>
50 #include <sys/mount.h>
51 #include <sys/nlookup.h>
52 #include <sys/vnode.h>
54 #include <sys/filio.h>
55 #include <sys/ttycom.h>
57 #include <sys/syslog.h>
59 static int vn_closefile (struct file *fp);
60 static int vn_ioctl (struct file *fp, u_long com, caddr_t data,
62 static int vn_read (struct file *fp, struct uio *uio,
63 struct ucred *cred, int flags);
64 static int svn_read (struct file *fp, struct uio *uio,
65 struct ucred *cred, int flags);
66 static int vn_poll (struct file *fp, int events, struct ucred *cred);
67 static int vn_kqfilter (struct file *fp, struct knote *kn);
68 static int vn_statfile (struct file *fp, struct stat *sb, struct ucred *cred);
69 static int vn_write (struct file *fp, struct uio *uio,
70 struct ucred *cred, int flags);
71 static int svn_write (struct file *fp, struct uio *uio,
72 struct ucred *cred, int flags);
74 struct fileops vnode_fileops = {
79 .fo_kqfilter = vn_kqfilter,
80 .fo_stat = vn_statfile,
81 .fo_close = vn_closefile,
82 .fo_shutdown = nofo_shutdown
85 struct fileops specvnode_fileops = {
87 .fo_write = svn_write,
90 .fo_kqfilter = vn_kqfilter,
91 .fo_stat = vn_statfile,
92 .fo_close = vn_closefile,
93 .fo_shutdown = nofo_shutdown
97 * Shortcut the device read/write. This avoids a lot of vnode junk.
98 * Basically the specfs vnops for read and write take the locked vnode,
99 * unlock it (because we can't hold the vnode locked while reading or writing
100 * a device which may block indefinitely), issues the device operation, then
101 * relock the vnode before returning, plus other junk. This bypasses all
102 * of that and just does the device operation.
105 vn_setspecops(struct file *fp)
107 if (vfs_fastdev && fp->f_ops == &vnode_fileops) {
108 fp->f_ops = &specvnode_fileops;
113 * Common code for vnode open operations. Check permissions, and call
114 * the VOP_NOPEN or VOP_NCREATE routine.
116 * The caller is responsible for setting up nd with nlookup_init() and
117 * for cleaning it up with nlookup_done(), whether we return an error
120 * On success nd->nl_open_vp will hold a referenced and, if requested,
121 * locked vnode. A locked vnode is requested via NLC_LOCKVP. If fp
122 * is non-NULL the vnode will be installed in the file pointer.
124 * NOTE: The vnode is referenced just once on return whether or not it
125 * is also installed in the file pointer.
128 vn_open(struct nlookupdata *nd, struct file *fp, int fmode, int cmode)
131 struct ucred *cred = nd->nl_cred;
133 struct vattr *vap = &vat;
137 * Certain combinations are illegal
139 if ((fmode & (FWRITE | O_TRUNC)) == O_TRUNC)
143 * Lookup the path and create or obtain the vnode. After a
144 * successful lookup a locked nd->nl_nch will be returned.
146 * The result of this section should be a locked vnode.
148 * XXX with only a little work we should be able to avoid locking
149 * the vnode if FWRITE, O_CREAT, and O_TRUNC are *not* set.
152 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
153 nd->nl_flags |= NLC_FOLLOW;
155 if (fmode & O_CREAT) {
157 * CONDITIONAL CREATE FILE CASE
159 * Setting NLC_CREATE causes a negative hit to store
160 * the negative hit ncp and not return an error. Then
161 * nc_error or nc_vp may be checked to see if the ncp
162 * represents a negative hit. NLC_CREATE also requires
163 * write permission on the governing directory or EPERM
166 nd->nl_flags |= NLC_CREATE;
167 nd->nl_flags |= NLC_REFDVP;
172 * NORMAL OPEN FILE CASE
181 * split case to allow us to re-resolve and retry the ncp in case
185 if (fmode & O_CREAT) {
186 if (nd->nl_nch.ncp->nc_vp == NULL) {
187 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
191 vap->va_mode = cmode;
193 vap->va_vaflags |= VA_EXCLUSIVE;
194 error = VOP_NCREATE(&nd->nl_nch, nd->nl_dvp, &vp,
199 /* locked vnode is returned */
201 if (fmode & O_EXCL) {
204 error = cache_vget(&nd->nl_nch, cred,
212 error = cache_vget(&nd->nl_nch, cred, LK_EXCLUSIVE, &vp);
218 * We have a locked vnode and ncp now. Note that the ncp will
219 * be cleaned up by the caller if nd->nl_nch is left intact.
221 if (vp->v_type == VLNK) {
225 if (vp->v_type == VSOCK) {
229 if ((fmode & O_CREAT) == 0) {
231 if (fmode & (FWRITE | O_TRUNC)) {
232 if (vp->v_type == VDIR) {
236 error = vn_writechk(vp, &nd->nl_nch);
239 * Special stale handling, re-resolve the
242 if (error == ESTALE) {
245 cache_setunresolved(&nd->nl_nch);
246 error = cache_resolve(&nd->nl_nch, cred);
257 error = VOP_ACCESS(vp, mode, cred);
260 * Special stale handling, re-resolve the
263 if (error == ESTALE) {
266 cache_setunresolved(&nd->nl_nch);
267 error = cache_resolve(&nd->nl_nch, cred);
275 if (fmode & O_TRUNC) {
276 vn_unlock(vp); /* XXX */
277 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
280 error = VOP_SETATTR(vp, vap, cred);
286 * Setup the fp so VOP_OPEN can override it. No descriptor has been
287 * associated with the fp yet so we own it clean.
289 * f_nchandle inherits nl_nch. This used to be necessary only for
290 * directories but now we do it unconditionally so f*() ops
291 * such as fchmod() can access the actual namespace that was
292 * used to open the file.
295 fp->f_nchandle = nd->nl_nch;
296 cache_zero(&nd->nl_nch);
297 cache_unlock(&fp->f_nchandle);
301 * Get rid of nl_nch. vn_open does not return it (it returns the
302 * vnode or the file pointer). Note: we can't leave nl_nch locked
303 * through the VOP_OPEN anyway since the VOP_OPEN may block, e.g.
307 cache_put(&nd->nl_nch);
309 error = VOP_OPEN(vp, fmode, cred, fp);
312 * setting f_ops to &badfileops will prevent the descriptor
313 * code from trying to close and release the vnode, since
314 * the open failed we do not want to call close.
318 fp->f_ops = &badfileops;
325 * Assert that VREG files have been setup for vmio.
327 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
328 ("vn_open: regular file was not VMIO enabled!"));
332 * Return the vnode. XXX needs some cleaning up. The vnode is
333 * only returned in the fp == NULL case.
337 nd->nl_vp_fmode = fmode;
338 if ((nd->nl_flags & NLC_LOCKVP) == 0)
351 vn_opendisk(const char *devname, int fmode, struct vnode **vpp)
356 if (strncmp(devname, "/dev/", 5) == 0)
358 if ((vp = getsynthvnode(devname)) == NULL) {
361 error = VOP_OPEN(vp, fmode, proc0.p_ucred, NULL);
373 * Check for write permissions on the specified vnode. nch may be NULL.
376 vn_writechk(struct vnode *vp, struct nchandle *nch)
379 * If there's shared text associated with
380 * the vnode, try to free it up once. If
381 * we fail, we can't allow writing.
383 if (vp->v_flag & VTEXT)
387 * If the vnode represents a regular file, check the mount
388 * point via the nch. This may be a different mount point
389 * then the one embedded in the vnode (e.g. nullfs).
391 * We can still write to non-regular files (e.g. devices)
392 * via read-only mounts.
394 if (nch && nch->ncp && vp->v_type == VREG)
395 return (ncp_writechk(nch));
400 * Check whether the underlying mount is read-only. The mount point
401 * referenced by the namecache may be different from the mount point
402 * used by the underlying vnode in the case of NULLFS, so a separate
406 ncp_writechk(struct nchandle *nch)
408 if (nch->mount && (nch->mount->mnt_flag & MNT_RDONLY))
417 vn_close(struct vnode *vp, int flags)
421 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
423 error = VOP_CLOSE(vp, flags);
432 sequential_heuristic(struct uio *uio, struct file *fp)
435 * Sequential heuristic - detect sequential operation
437 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
438 uio->uio_offset == fp->f_nextoff) {
439 int tmpseq = fp->f_seqcount;
441 * XXX we assume that the filesystem block size is
442 * the default. Not true, but still gives us a pretty
443 * good indicator of how sequential the read operations
446 tmpseq += (uio->uio_resid + BKVASIZE - 1) / BKVASIZE;
447 if (tmpseq > IO_SEQMAX)
449 fp->f_seqcount = tmpseq;
450 return(fp->f_seqcount << IO_SEQSHIFT);
454 * Not sequential, quick draw-down of seqcount
456 if (fp->f_seqcount > 1)
464 * Package up an I/O request on a vnode into a uio and do it.
467 vn_rdwr(enum uio_rw rw, struct vnode *vp, caddr_t base, int len,
468 off_t offset, enum uio_seg segflg, int ioflg,
469 struct ucred *cred, int *aresid)
473 struct ccms_lock ccms_lock;
476 if ((ioflg & IO_NODELOCKED) == 0)
477 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
478 auio.uio_iov = &aiov;
480 aiov.iov_base = base;
482 auio.uio_resid = len;
483 auio.uio_offset = offset;
484 auio.uio_segflg = segflg;
486 auio.uio_td = curthread;
487 ccms_lock_get_uio(&vp->v_ccms, &ccms_lock, &auio);
488 if (rw == UIO_READ) {
489 error = VOP_READ(vp, &auio, ioflg, cred);
491 error = VOP_WRITE(vp, &auio, ioflg, cred);
493 ccms_lock_put(&vp->v_ccms, &ccms_lock);
495 *aresid = auio.uio_resid;
497 if (auio.uio_resid && error == 0)
499 if ((ioflg & IO_NODELOCKED) == 0)
505 * Package up an I/O request on a vnode into a uio and do it. The I/O
506 * request is split up into smaller chunks and we try to avoid saturating
507 * the buffer cache while potentially holding a vnode locked, so we
508 * check bwillwrite() before calling vn_rdwr(). We also call uio_yield()
509 * to give other processes a chance to lock the vnode (either other processes
510 * core'ing the same binary, or unrelated processes scanning the directory).
513 vn_rdwr_inchunks(enum uio_rw rw, struct vnode *vp, caddr_t base, int len,
514 off_t offset, enum uio_seg segflg, int ioflg,
515 struct ucred *cred, int *aresid)
523 * Force `offset' to a multiple of MAXBSIZE except possibly
524 * for the first chunk, so that filesystems only need to
525 * write full blocks except possibly for the first and last
528 chunk = MAXBSIZE - (uoff_t)offset % MAXBSIZE;
532 if (vp->v_type == VREG) {
542 error = vn_rdwr(rw, vp, base, chunk, offset, segflg,
543 ioflg, cred, aresid);
544 len -= chunk; /* aresid calc already includes length */
557 * MPALMOSTSAFE - acquires mplock
560 vn_read(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
562 struct ccms_lock ccms_lock;
567 KASSERT(uio->uio_td == curthread,
568 ("uio_td %p is not td %p", uio->uio_td, curthread));
569 vp = (struct vnode *)fp->f_data;
572 if (flags & O_FBLOCKING) {
573 /* ioflag &= ~IO_NDELAY; */
574 } else if (flags & O_FNONBLOCKING) {
576 } else if (fp->f_flag & FNONBLOCK) {
579 if (flags & O_FBUFFERED) {
580 /* ioflag &= ~IO_DIRECT; */
581 } else if (flags & O_FUNBUFFERED) {
583 } else if (fp->f_flag & O_DIRECT) {
586 vn_lock(vp, LK_SHARED | LK_RETRY);
587 if ((flags & O_FOFFSET) == 0)
588 uio->uio_offset = fp->f_offset;
589 ioflag |= sequential_heuristic(uio, fp);
591 ccms_lock_get_uio(&vp->v_ccms, &ccms_lock, uio);
592 error = VOP_READ(vp, uio, ioflag, cred);
593 ccms_lock_put(&vp->v_ccms, &ccms_lock);
594 if ((flags & O_FOFFSET) == 0)
595 fp->f_offset = uio->uio_offset;
596 fp->f_nextoff = uio->uio_offset;
603 * Device-optimized file table vnode read routine.
605 * This bypasses the VOP table and talks directly to the device. Most
606 * filesystems just route to specfs and can make this optimization.
608 * MPALMOSTSAFE - acquires mplock
611 svn_read(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
619 KASSERT(uio->uio_td == curthread,
620 ("uio_td %p is not td %p", uio->uio_td, curthread));
622 vp = (struct vnode *)fp->f_data;
623 if (vp == NULL || vp->v_type == VBAD) {
628 if ((dev = vp->v_rdev) == NULL) {
634 if (uio->uio_resid == 0) {
638 if ((flags & O_FOFFSET) == 0)
639 uio->uio_offset = fp->f_offset;
642 if (flags & O_FBLOCKING) {
643 /* ioflag &= ~IO_NDELAY; */
644 } else if (flags & O_FNONBLOCKING) {
646 } else if (fp->f_flag & FNONBLOCK) {
649 if (flags & O_FBUFFERED) {
650 /* ioflag &= ~IO_DIRECT; */
651 } else if (flags & O_FUNBUFFERED) {
653 } else if (fp->f_flag & O_DIRECT) {
656 ioflag |= sequential_heuristic(uio, fp);
658 error = dev_dread(dev, uio, ioflag);
661 if ((flags & O_FOFFSET) == 0)
662 fp->f_offset = uio->uio_offset;
663 fp->f_nextoff = uio->uio_offset;
670 * MPALMOSTSAFE - acquires mplock
673 vn_write(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
675 struct ccms_lock ccms_lock;
680 KASSERT(uio->uio_td == curthread,
681 ("uio_td %p is not p %p", uio->uio_td, curthread));
682 vp = (struct vnode *)fp->f_data;
684 /* VOP_WRITE should handle this now */
685 if (vp->v_type == VREG || vp->v_type == VDATABASE)
688 vp = (struct vnode *)fp->f_data; /* XXX needed? */
691 if (vp->v_type == VREG &&
692 ((fp->f_flag & O_APPEND) || (flags & O_FAPPEND))) {
696 if (flags & O_FBLOCKING) {
697 /* ioflag &= ~IO_NDELAY; */
698 } else if (flags & O_FNONBLOCKING) {
700 } else if (fp->f_flag & FNONBLOCK) {
703 if (flags & O_FBUFFERED) {
704 /* ioflag &= ~IO_DIRECT; */
705 } else if (flags & O_FUNBUFFERED) {
707 } else if (fp->f_flag & O_DIRECT) {
710 if (flags & O_FASYNCWRITE) {
711 /* ioflag &= ~IO_SYNC; */
712 } else if (flags & O_FSYNCWRITE) {
714 } else if (fp->f_flag & O_FSYNC) {
718 if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))
720 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
721 if ((flags & O_FOFFSET) == 0)
722 uio->uio_offset = fp->f_offset;
723 ioflag |= sequential_heuristic(uio, fp);
724 ccms_lock_get_uio(&vp->v_ccms, &ccms_lock, uio);
725 error = VOP_WRITE(vp, uio, ioflag, cred);
726 ccms_lock_put(&vp->v_ccms, &ccms_lock);
727 if ((flags & O_FOFFSET) == 0)
728 fp->f_offset = uio->uio_offset;
729 fp->f_nextoff = uio->uio_offset;
736 * Device-optimized file table vnode write routine.
738 * This bypasses the VOP table and talks directly to the device. Most
739 * filesystems just route to specfs and can make this optimization.
741 * MPALMOSTSAFE - acquires mplock
744 svn_write(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
752 KASSERT(uio->uio_td == curthread,
753 ("uio_td %p is not p %p", uio->uio_td, curthread));
755 vp = (struct vnode *)fp->f_data;
756 if (vp == NULL || vp->v_type == VBAD) {
760 if (vp->v_type == VREG)
761 bwillwrite(uio->uio_resid);
762 vp = (struct vnode *)fp->f_data; /* XXX needed? */
764 if ((dev = vp->v_rdev) == NULL) {
770 if ((flags & O_FOFFSET) == 0)
771 uio->uio_offset = fp->f_offset;
774 if (vp->v_type == VREG &&
775 ((fp->f_flag & O_APPEND) || (flags & O_FAPPEND))) {
779 if (flags & O_FBLOCKING) {
780 /* ioflag &= ~IO_NDELAY; */
781 } else if (flags & O_FNONBLOCKING) {
783 } else if (fp->f_flag & FNONBLOCK) {
786 if (flags & O_FBUFFERED) {
787 /* ioflag &= ~IO_DIRECT; */
788 } else if (flags & O_FUNBUFFERED) {
790 } else if (fp->f_flag & O_DIRECT) {
793 if (flags & O_FASYNCWRITE) {
794 /* ioflag &= ~IO_SYNC; */
795 } else if (flags & O_FSYNCWRITE) {
797 } else if (fp->f_flag & O_FSYNC) {
801 if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))
803 ioflag |= sequential_heuristic(uio, fp);
805 error = dev_dwrite(dev, uio, ioflag);
808 if ((flags & O_FOFFSET) == 0)
809 fp->f_offset = uio->uio_offset;
810 fp->f_nextoff = uio->uio_offset;
817 * MPALMOSTSAFE - acquires mplock
820 vn_statfile(struct file *fp, struct stat *sb, struct ucred *cred)
826 vp = (struct vnode *)fp->f_data;
827 error = vn_stat(vp, sb, cred);
833 vn_stat(struct vnode *vp, struct stat *sb, struct ucred *cred)
842 error = VOP_GETATTR(vp, vap);
847 * Zero the spare stat fields
853 * Copy from vattr table
855 if (vap->va_fsid != VNOVAL)
856 sb->st_dev = vap->va_fsid;
858 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
859 sb->st_ino = vap->va_fileid;
861 switch (vap->va_type) {
879 /* This is a cosmetic change, symlinks do not have a mode. */
880 if (vp->v_mount->mnt_flag & MNT_NOSYMFOLLOW)
881 sb->st_mode &= ~ACCESSPERMS; /* 0000 */
883 sb->st_mode |= ACCESSPERMS; /* 0777 */
895 if (vap->va_nlink > (nlink_t)-1)
896 sb->st_nlink = (nlink_t)-1;
898 sb->st_nlink = vap->va_nlink;
899 sb->st_uid = vap->va_uid;
900 sb->st_gid = vap->va_gid;
901 sb->st_rdev = makeudev(vap->va_rmajor, vap->va_rminor);
902 sb->st_size = vap->va_size;
903 sb->st_atimespec = vap->va_atime;
904 sb->st_mtimespec = vap->va_mtime;
905 sb->st_ctimespec = vap->va_ctime;
908 * A VCHR and VBLK device may track the last access and last modified
909 * time independantly of the filesystem. This is particularly true
910 * because device read and write calls may bypass the filesystem.
912 if (vp->v_type == VCHR || vp->v_type == VBLK) {
915 if (dev->si_lastread) {
916 sb->st_atimespec.tv_sec = dev->si_lastread;
917 sb->st_atimespec.tv_nsec = 0;
919 if (dev->si_lastwrite) {
920 sb->st_atimespec.tv_sec = dev->si_lastwrite;
921 sb->st_atimespec.tv_nsec = 0;
927 * According to www.opengroup.org, the meaning of st_blksize is
928 * "a filesystem-specific preferred I/O block size for this
929 * object. In some filesystem types, this may vary from file
931 * Default to PAGE_SIZE after much discussion.
934 if (vap->va_type == VREG) {
935 sb->st_blksize = vap->va_blocksize;
936 } else if (vn_isdisk(vp, NULL)) {
938 * XXX this is broken. If the device is not yet open (aka
939 * stat() call, aka v_rdev == NULL), how are we supposed
940 * to get a valid block size out of it?
943 if (dev == NULL && vp->v_type == VCHR) {
944 dev = get_dev(vp->v_umajor, vp->v_uminor);
946 sb->st_blksize = dev->si_bsize_best;
947 if (sb->st_blksize < dev->si_bsize_phys)
948 sb->st_blksize = dev->si_bsize_phys;
949 if (sb->st_blksize < BLKDEV_IOSIZE)
950 sb->st_blksize = BLKDEV_IOSIZE;
952 sb->st_blksize = PAGE_SIZE;
955 sb->st_flags = vap->va_flags;
957 error = priv_check_cred(cred, PRIV_VFS_GENERATION, 0);
961 sb->st_gen = (u_int32_t)vap->va_gen;
963 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
964 sb->st_fsmid = vap->va_fsmid;
969 * MPALMOSTSAFE - acquires mplock
972 vn_ioctl(struct file *fp, u_long com, caddr_t data, struct ucred *ucred)
974 struct vnode *vp = ((struct vnode *)fp->f_data);
981 switch (vp->v_type) {
984 if (com == FIONREAD) {
985 error = VOP_GETATTR(vp, &vattr);
988 *(int *)data = vattr.va_size - fp->f_offset;
992 if (com == FIOASYNC) { /* XXX */
1004 if (com == FIODTYPE) {
1005 if (vp->v_type != VCHR && vp->v_type != VBLK) {
1009 *(int *)data = dev_dflags(vp->v_rdev) & D_TYPEMASK;
1013 error = VOP_IOCTL(vp, com, data, fp->f_flag, ucred);
1014 if (error == 0 && com == TIOCSCTTY) {
1015 struct proc *p = curthread->td_proc;
1016 struct session *sess;
1023 sess = p->p_session;
1024 /* Do nothing if reassigning same control tty */
1025 if (sess->s_ttyvp == vp) {
1030 /* Get rid of reference to old control tty */
1031 ovp = sess->s_ttyvp;
1044 * MPALMOSTSAFE - acquires mplock
1047 vn_poll(struct file *fp, int events, struct ucred *cred)
1052 error = VOP_POLL(((struct vnode *)fp->f_data), events, cred);
1058 * Check that the vnode is still valid, and if so
1059 * acquire requested lock.
1063 vn_lock(struct vnode *vp, int flags)
1065 debug_vn_lock(struct vnode *vp, int flags, const char *filename, int line)
1072 vp->filename = filename;
1074 error = debuglockmgr(&vp->v_lock, flags,
1075 "vn_lock", filename, line);
1077 error = lockmgr(&vp->v_lock, flags);
1081 } while (flags & LK_RETRY);
1084 * Because we (had better!) have a ref on the vnode, once it
1085 * goes to VRECLAIMED state it will not be recycled until all
1086 * refs go away. So we can just check the flag.
1088 if (error == 0 && (vp->v_flag & VRECLAIMED)) {
1089 lockmgr(&vp->v_lock, LK_RELEASE);
1096 vn_unlock(struct vnode *vp)
1098 lockmgr(&vp->v_lock, LK_RELEASE);
1102 vn_islocked(struct vnode *vp)
1104 return (lockstatus(&vp->v_lock, curthread));
1108 * MPALMOSTSAFE - acquires mplock
1111 vn_closefile(struct file *fp)
1116 fp->f_ops = &badfileops;
1117 error = vn_close(((struct vnode *)fp->f_data), fp->f_flag);
1123 * MPALMOSTSAFE - acquires mplock
1126 vn_kqfilter(struct file *fp, struct knote *kn)
1131 error = VOP_KQFILTER(((struct vnode *)fp->f_data), kn);