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.57 2008/05/09 17:52:17 dillon Exp $
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/fcntl.h>
49 #include <sys/mount.h>
50 #include <sys/nlookup.h>
51 #include <sys/vnode.h>
53 #include <sys/filio.h>
54 #include <sys/ttycom.h>
56 #include <sys/syslog.h>
58 static int vn_closefile (struct file *fp);
59 static int vn_ioctl (struct file *fp, u_long com, caddr_t data,
61 static int vn_read (struct file *fp, struct uio *uio,
62 struct ucred *cred, int flags);
63 static int svn_read (struct file *fp, struct uio *uio,
64 struct ucred *cred, int flags);
65 static int vn_poll (struct file *fp, int events, struct ucred *cred);
66 static int vn_kqfilter (struct file *fp, struct knote *kn);
67 static int vn_statfile (struct file *fp, struct stat *sb, struct ucred *cred);
68 static int vn_write (struct file *fp, struct uio *uio,
69 struct ucred *cred, int flags);
70 static int svn_write (struct file *fp, struct uio *uio,
71 struct ucred *cred, int flags);
73 struct fileops vnode_fileops = {
78 .fo_kqfilter = vn_kqfilter,
79 .fo_stat = vn_statfile,
80 .fo_close = vn_closefile,
81 .fo_shutdown = nofo_shutdown
84 struct fileops specvnode_fileops = {
86 .fo_write = svn_write,
89 .fo_kqfilter = vn_kqfilter,
90 .fo_stat = vn_statfile,
91 .fo_close = vn_closefile,
92 .fo_shutdown = nofo_shutdown
96 * Shortcut the device read/write. This avoids a lot of vnode junk.
97 * Basically the specfs vnops for read and write take the locked vnode,
98 * unlock it (because we can't hold the vnode locked while reading or writing
99 * a device which may block indefinitely), issues the device operation, then
100 * relock the vnode before returning, plus other junk. This bypasses all
101 * of that and just does the device operation.
104 vn_setspecops(struct file *fp)
106 if (vfs_fastdev && fp->f_ops == &vnode_fileops) {
107 fp->f_ops = &specvnode_fileops;
112 * Common code for vnode open operations. Check permissions, and call
113 * the VOP_NOPEN or VOP_NCREATE routine.
115 * The caller is responsible for setting up nd with nlookup_init() and
116 * for cleaning it up with nlookup_done(), whether we return an error
119 * On success nd->nl_open_vp will hold a referenced and, if requested,
120 * locked vnode. A locked vnode is requested via NLC_LOCKVP. If fp
121 * is non-NULL the vnode will be installed in the file pointer.
123 * NOTE: The vnode is referenced just once on return whether or not it
124 * is also installed in the file pointer.
127 vn_open(struct nlookupdata *nd, struct file *fp, int fmode, int cmode)
130 struct ucred *cred = nd->nl_cred;
132 struct vattr *vap = &vat;
136 * Lookup the path and create or obtain the vnode. After a
137 * successful lookup a locked nd->nl_nch will be returned.
139 * The result of this section should be a locked vnode.
141 * XXX with only a little work we should be able to avoid locking
142 * the vnode if FWRITE, O_CREAT, and O_TRUNC are *not* set.
144 if (fmode & O_CREAT) {
146 * CONDITIONAL CREATE FILE CASE
148 * Setting NLC_CREATE causes a negative hit to store
149 * the negative hit ncp and not return an error. Then
150 * nc_error or nc_vp may be checked to see if the ncp
151 * represents a negative hit. NLC_CREATE also requires
152 * write permission on the governing directory or EPERM
155 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
156 nd->nl_flags |= NLC_FOLLOW;
157 nd->nl_flags |= NLC_CREATE;
158 nd->nl_flags |= NLC_REFDVP;
163 * NORMAL OPEN FILE CASE
172 * split case to allow us to re-resolve and retry the ncp in case
176 if (fmode & O_CREAT) {
177 if (nd->nl_nch.ncp->nc_vp == NULL) {
178 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
182 vap->va_mode = cmode;
184 vap->va_vaflags |= VA_EXCLUSIVE;
185 error = VOP_NCREATE(&nd->nl_nch, nd->nl_dvp, &vp,
190 /* locked vnode is returned */
192 if (fmode & O_EXCL) {
195 error = cache_vget(&nd->nl_nch, cred,
203 error = cache_vget(&nd->nl_nch, cred, LK_EXCLUSIVE, &vp);
209 * We have a locked vnode and ncp now. Note that the ncp will
210 * be cleaned up by the caller if nd->nl_nch is left intact.
212 if (vp->v_type == VLNK) {
216 if (vp->v_type == VSOCK) {
220 if ((fmode & O_CREAT) == 0) {
222 if (fmode & (FWRITE | O_TRUNC)) {
223 if (vp->v_type == VDIR) {
227 error = vn_writechk(vp, &nd->nl_nch);
230 * Special stale handling, re-resolve the
233 if (error == ESTALE) {
236 cache_setunresolved(&nd->nl_nch);
237 error = cache_resolve(&nd->nl_nch, cred);
248 error = VOP_ACCESS(vp, mode, cred);
251 * Special stale handling, re-resolve the
254 if (error == ESTALE) {
257 cache_setunresolved(&nd->nl_nch);
258 error = cache_resolve(&nd->nl_nch, cred);
266 if (fmode & O_TRUNC) {
267 vn_unlock(vp); /* XXX */
268 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
271 error = VOP_SETATTR(vp, vap, cred);
277 * Setup the fp so VOP_OPEN can override it. No descriptor has been
278 * associated with the fp yet so we own it clean.
280 * f_nchandle inherits nl_nch. This used to be necessary only for
281 * directories but now we do it unconditionally so f*() ops
282 * such as fchmod() can access the actual namespace that was
283 * used to open the file.
286 fp->f_nchandle = nd->nl_nch;
287 cache_zero(&nd->nl_nch);
288 cache_unlock(&fp->f_nchandle);
292 * Get rid of nl_nch. vn_open does not return it (it returns the
293 * vnode or the file pointer). Note: we can't leave nl_nch locked
294 * through the VOP_OPEN anyway since the VOP_OPEN may block, e.g.
298 cache_put(&nd->nl_nch);
300 error = VOP_OPEN(vp, fmode, cred, fp);
303 * setting f_ops to &badfileops will prevent the descriptor
304 * code from trying to close and release the vnode, since
305 * the open failed we do not want to call close.
309 fp->f_ops = &badfileops;
316 * Assert that VREG files have been setup for vmio.
318 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
319 ("vn_open: regular file was not VMIO enabled!"));
323 * Return the vnode. XXX needs some cleaning up. The vnode is
324 * only returned in the fp == NULL case.
328 nd->nl_vp_fmode = fmode;
329 if ((nd->nl_flags & NLC_LOCKVP) == 0)
342 vn_opendisk(const char *devname, int fmode, struct vnode **vpp)
347 if (strncmp(devname, "/dev/", 5) == 0)
349 if ((vp = getsynthvnode(devname)) == NULL) {
352 error = VOP_OPEN(vp, fmode, proc0.p_ucred, NULL);
364 * Check for write permissions on the specified vnode. nch may be NULL.
367 vn_writechk(struct vnode *vp, struct nchandle *nch)
370 * If there's shared text associated with
371 * the vnode, try to free it up once. If
372 * we fail, we can't allow writing.
374 if (vp->v_flag & VTEXT)
378 * If the vnode represents a regular file, check the mount
379 * point via the nch. This may be a different mount point
380 * then the one embedded in the vnode (e.g. nullfs).
382 * We can still write to non-regular files (e.g. devices)
383 * via read-only mounts.
385 if (nch && nch->ncp && vp->v_type == VREG)
386 return (ncp_writechk(nch));
391 * Check whether the underlying mount is read-only. The mount point
392 * referenced by the namecache may be different from the mount point
393 * used by the underlying vnode in the case of NULLFS, so a separate
397 ncp_writechk(struct nchandle *nch)
399 if (nch->mount && (nch->mount->mnt_flag & MNT_RDONLY))
408 vn_close(struct vnode *vp, int flags)
412 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) == 0) {
413 error = VOP_CLOSE(vp, flags);
422 sequential_heuristic(struct uio *uio, struct file *fp)
425 * Sequential heuristic - detect sequential operation
427 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
428 uio->uio_offset == fp->f_nextoff) {
429 int tmpseq = fp->f_seqcount;
431 * XXX we assume that the filesystem block size is
432 * the default. Not true, but still gives us a pretty
433 * good indicator of how sequential the read operations
436 tmpseq += (uio->uio_resid + BKVASIZE - 1) / BKVASIZE;
437 if (tmpseq > IO_SEQMAX)
439 fp->f_seqcount = tmpseq;
440 return(fp->f_seqcount << IO_SEQSHIFT);
444 * Not sequential, quick draw-down of seqcount
446 if (fp->f_seqcount > 1)
454 * Package up an I/O request on a vnode into a uio and do it.
457 vn_rdwr(enum uio_rw rw, struct vnode *vp, caddr_t base, int len,
458 off_t offset, enum uio_seg segflg, int ioflg,
459 struct ucred *cred, int *aresid)
463 struct ccms_lock ccms_lock;
466 if ((ioflg & IO_NODELOCKED) == 0)
467 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
468 auio.uio_iov = &aiov;
470 aiov.iov_base = base;
472 auio.uio_resid = len;
473 auio.uio_offset = offset;
474 auio.uio_segflg = segflg;
476 auio.uio_td = curthread;
477 ccms_lock_get_uio(&vp->v_ccms, &ccms_lock, &auio);
478 if (rw == UIO_READ) {
479 error = VOP_READ(vp, &auio, ioflg, cred);
481 error = VOP_WRITE(vp, &auio, ioflg, cred);
483 ccms_lock_put(&vp->v_ccms, &ccms_lock);
485 *aresid = auio.uio_resid;
487 if (auio.uio_resid && error == 0)
489 if ((ioflg & IO_NODELOCKED) == 0)
495 * Package up an I/O request on a vnode into a uio and do it. The I/O
496 * request is split up into smaller chunks and we try to avoid saturating
497 * the buffer cache while potentially holding a vnode locked, so we
498 * check bwillwrite() before calling vn_rdwr(). We also call uio_yield()
499 * to give other processes a chance to lock the vnode (either other processes
500 * core'ing the same binary, or unrelated processes scanning the directory).
503 vn_rdwr_inchunks(enum uio_rw rw, struct vnode *vp, caddr_t base, int len,
504 off_t offset, enum uio_seg segflg, int ioflg,
505 struct ucred *cred, int *aresid)
513 * Force `offset' to a multiple of MAXBSIZE except possibly
514 * for the first chunk, so that filesystems only need to
515 * write full blocks except possibly for the first and last
518 chunk = MAXBSIZE - (uoff_t)offset % MAXBSIZE;
522 if (rw != UIO_READ && vp->v_type == VREG)
524 error = vn_rdwr(rw, vp, base, chunk, offset, segflg,
525 ioflg, cred, aresid);
526 len -= chunk; /* aresid calc already includes length */
539 * MPALMOSTSAFE - acquires mplock
542 vn_read(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
544 struct ccms_lock ccms_lock;
549 KASSERT(uio->uio_td == curthread,
550 ("uio_td %p is not td %p", uio->uio_td, curthread));
551 vp = (struct vnode *)fp->f_data;
554 if (flags & O_FBLOCKING) {
555 /* ioflag &= ~IO_NDELAY; */
556 } else if (flags & O_FNONBLOCKING) {
558 } else if (fp->f_flag & FNONBLOCK) {
561 if (flags & O_FBUFFERED) {
562 /* ioflag &= ~IO_DIRECT; */
563 } else if (flags & O_FUNBUFFERED) {
565 } else if (fp->f_flag & O_DIRECT) {
568 vn_lock(vp, LK_SHARED | LK_RETRY);
569 if ((flags & O_FOFFSET) == 0)
570 uio->uio_offset = fp->f_offset;
571 ioflag |= sequential_heuristic(uio, fp);
573 ccms_lock_get_uio(&vp->v_ccms, &ccms_lock, uio);
574 error = VOP_READ(vp, uio, ioflag, cred);
575 ccms_lock_put(&vp->v_ccms, &ccms_lock);
576 if ((flags & O_FOFFSET) == 0)
577 fp->f_offset = uio->uio_offset;
578 fp->f_nextoff = uio->uio_offset;
585 * Device-optimized file table vnode read routine.
587 * This bypasses the VOP table and talks directly to the device. Most
588 * filesystems just route to specfs and can make this optimization.
590 * MPALMOSTSAFE - acquires mplock
593 svn_read(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
601 KASSERT(uio->uio_td == curthread,
602 ("uio_td %p is not td %p", uio->uio_td, curthread));
604 vp = (struct vnode *)fp->f_data;
605 if (vp == NULL || vp->v_type == VBAD) {
610 if ((dev = vp->v_rdev) == NULL) {
616 if (uio->uio_resid == 0) {
620 if ((flags & O_FOFFSET) == 0)
621 uio->uio_offset = fp->f_offset;
624 if (flags & O_FBLOCKING) {
625 /* ioflag &= ~IO_NDELAY; */
626 } else if (flags & O_FNONBLOCKING) {
628 } else if (fp->f_flag & FNONBLOCK) {
631 if (flags & O_FBUFFERED) {
632 /* ioflag &= ~IO_DIRECT; */
633 } else if (flags & O_FUNBUFFERED) {
635 } else if (fp->f_flag & O_DIRECT) {
638 ioflag |= sequential_heuristic(uio, fp);
640 error = dev_dread(dev, uio, ioflag);
643 if ((flags & O_FOFFSET) == 0)
644 fp->f_offset = uio->uio_offset;
645 fp->f_nextoff = uio->uio_offset;
652 * MPALMOSTSAFE - acquires mplock
655 vn_write(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
657 struct ccms_lock ccms_lock;
662 KASSERT(uio->uio_td == curthread,
663 ("uio_td %p is not p %p", uio->uio_td, curthread));
664 vp = (struct vnode *)fp->f_data;
665 if (vp->v_type == VREG || vp->v_type == VDATABASE)
667 vp = (struct vnode *)fp->f_data; /* XXX needed? */
670 if (vp->v_type == VREG &&
671 ((fp->f_flag & O_APPEND) || (flags & O_FAPPEND))) {
675 if (flags & O_FBLOCKING) {
676 /* ioflag &= ~IO_NDELAY; */
677 } else if (flags & O_FNONBLOCKING) {
679 } else if (fp->f_flag & FNONBLOCK) {
682 if (flags & O_FBUFFERED) {
683 /* ioflag &= ~IO_DIRECT; */
684 } else if (flags & O_FUNBUFFERED) {
686 } else if (fp->f_flag & O_DIRECT) {
689 if (flags & O_FASYNCWRITE) {
690 /* ioflag &= ~IO_SYNC; */
691 } else if (flags & O_FSYNCWRITE) {
693 } else if (fp->f_flag & O_FSYNC) {
697 if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))
699 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
700 if ((flags & O_FOFFSET) == 0)
701 uio->uio_offset = fp->f_offset;
702 ioflag |= sequential_heuristic(uio, fp);
703 ccms_lock_get_uio(&vp->v_ccms, &ccms_lock, uio);
704 error = VOP_WRITE(vp, uio, ioflag, cred);
705 ccms_lock_put(&vp->v_ccms, &ccms_lock);
706 if ((flags & O_FOFFSET) == 0)
707 fp->f_offset = uio->uio_offset;
708 fp->f_nextoff = uio->uio_offset;
715 * Device-optimized file table vnode write routine.
717 * This bypasses the VOP table and talks directly to the device. Most
718 * filesystems just route to specfs and can make this optimization.
720 * MPALMOSTSAFE - acquires mplock
723 svn_write(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
731 KASSERT(uio->uio_td == curthread,
732 ("uio_td %p is not p %p", uio->uio_td, curthread));
734 vp = (struct vnode *)fp->f_data;
735 if (vp == NULL || vp->v_type == VBAD) {
739 if (vp->v_type == VREG)
741 vp = (struct vnode *)fp->f_data; /* XXX needed? */
743 if ((dev = vp->v_rdev) == NULL) {
749 if ((flags & O_FOFFSET) == 0)
750 uio->uio_offset = fp->f_offset;
753 if (vp->v_type == VREG &&
754 ((fp->f_flag & O_APPEND) || (flags & O_FAPPEND))) {
758 if (flags & O_FBLOCKING) {
759 /* ioflag &= ~IO_NDELAY; */
760 } else if (flags & O_FNONBLOCKING) {
762 } else if (fp->f_flag & FNONBLOCK) {
765 if (flags & O_FBUFFERED) {
766 /* ioflag &= ~IO_DIRECT; */
767 } else if (flags & O_FUNBUFFERED) {
769 } else if (fp->f_flag & O_DIRECT) {
772 if (flags & O_FASYNCWRITE) {
773 /* ioflag &= ~IO_SYNC; */
774 } else if (flags & O_FSYNCWRITE) {
776 } else if (fp->f_flag & O_FSYNC) {
780 if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))
782 ioflag |= sequential_heuristic(uio, fp);
784 error = dev_dwrite(dev, uio, ioflag);
787 if ((flags & O_FOFFSET) == 0)
788 fp->f_offset = uio->uio_offset;
789 fp->f_nextoff = uio->uio_offset;
796 * MPALMOSTSAFE - acquires mplock
799 vn_statfile(struct file *fp, struct stat *sb, struct ucred *cred)
805 vp = (struct vnode *)fp->f_data;
806 error = vn_stat(vp, sb, cred);
812 vn_stat(struct vnode *vp, struct stat *sb, struct ucred *cred)
821 error = VOP_GETATTR(vp, vap);
826 * Zero the spare stat fields
832 * Copy from vattr table
834 if (vap->va_fsid != VNOVAL)
835 sb->st_dev = vap->va_fsid;
837 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
838 sb->st_ino = vap->va_fileid;
840 switch (vap->va_type) {
858 /* This is a cosmetic change, symlinks do not have a mode. */
859 if (vp->v_mount->mnt_flag & MNT_NOSYMFOLLOW)
860 sb->st_mode &= ~ACCESSPERMS; /* 0000 */
862 sb->st_mode |= ACCESSPERMS; /* 0777 */
874 if (vap->va_nlink > (nlink_t)-1)
875 sb->st_nlink = (nlink_t)-1;
877 sb->st_nlink = vap->va_nlink;
878 sb->st_uid = vap->va_uid;
879 sb->st_gid = vap->va_gid;
880 sb->st_rdev = makeudev(vap->va_rmajor, vap->va_rminor);
881 sb->st_size = vap->va_size;
882 sb->st_atimespec = vap->va_atime;
883 sb->st_mtimespec = vap->va_mtime;
884 sb->st_ctimespec = vap->va_ctime;
887 * A VCHR and VBLK device may track the last access and last modified
888 * time independantly of the filesystem. This is particularly true
889 * because device read and write calls may bypass the filesystem.
891 if (vp->v_type == VCHR || vp->v_type == VBLK) {
892 if ((dev = vp->v_rdev) != NULL) {
893 if (dev->si_lastread) {
894 sb->st_atimespec.tv_sec = dev->si_lastread;
895 sb->st_atimespec.tv_nsec = 0;
897 if (dev->si_lastwrite) {
898 sb->st_atimespec.tv_sec = dev->si_lastwrite;
899 sb->st_atimespec.tv_nsec = 0;
905 * According to www.opengroup.org, the meaning of st_blksize is
906 * "a filesystem-specific preferred I/O block size for this
907 * object. In some filesystem types, this may vary from file
909 * Default to PAGE_SIZE after much discussion.
912 if (vap->va_type == VREG) {
913 sb->st_blksize = vap->va_blocksize;
914 } else if (vn_isdisk(vp, NULL)) {
916 * XXX this is broken. If the device is not yet open (aka
917 * stat() call, aka v_rdev == NULL), how are we supposed
918 * to get a valid block size out of it?
922 if ((dev = vp->v_rdev) == NULL) {
923 if (vp->v_type == VCHR)
924 dev = get_dev(vp->v_umajor, vp->v_uminor);
926 sb->st_blksize = dev->si_bsize_best;
927 if (sb->st_blksize < dev->si_bsize_phys)
928 sb->st_blksize = dev->si_bsize_phys;
929 if (sb->st_blksize < BLKDEV_IOSIZE)
930 sb->st_blksize = BLKDEV_IOSIZE;
932 sb->st_blksize = PAGE_SIZE;
935 sb->st_flags = vap->va_flags;
936 if (suser_cred(cred, 0))
939 sb->st_gen = (u_int32_t)vap->va_gen;
941 #if (S_BLKSIZE == 512)
942 /* Optimize this case */
943 sb->st_blocks = vap->va_bytes >> 9;
945 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
947 sb->st_fsmid = vap->va_fsmid;
952 * MPALMOSTSAFE - acquires mplock
955 vn_ioctl(struct file *fp, u_long com, caddr_t data, struct ucred *ucred)
957 struct vnode *vp = ((struct vnode *)fp->f_data);
964 switch (vp->v_type) {
967 if (com == FIONREAD) {
968 if ((error = VOP_GETATTR(vp, &vattr)) != 0)
970 *(int *)data = vattr.va_size - fp->f_offset;
974 if (com == FIOASYNC) { /* XXX */
986 if (com == FIODTYPE) {
987 if (vp->v_type != VCHR && vp->v_type != VBLK) {
991 *(int *)data = dev_dflags(vp->v_rdev) & D_TYPEMASK;
995 error = VOP_IOCTL(vp, com, data, fp->f_flag, ucred);
996 if (error == 0 && com == TIOCSCTTY) {
997 struct proc *p = curthread->td_proc;
998 struct session *sess;
1005 sess = p->p_session;
1006 /* Do nothing if reassigning same control tty */
1007 if (sess->s_ttyvp == vp) {
1012 /* Get rid of reference to old control tty */
1013 ovp = sess->s_ttyvp;
1026 * MPALMOSTSAFE - acquires mplock
1029 vn_poll(struct file *fp, int events, struct ucred *cred)
1034 error = VOP_POLL(((struct vnode *)fp->f_data), events, cred);
1040 * Check that the vnode is still valid, and if so
1041 * acquire requested lock.
1045 vn_lock(struct vnode *vp, int flags)
1047 debug_vn_lock(struct vnode *vp, int flags, const char *filename, int line)
1054 vp->filename = filename;
1056 error = debuglockmgr(&vp->v_lock, flags,
1057 "vn_lock", filename, line);
1059 error = lockmgr(&vp->v_lock, flags);
1063 } while (flags & LK_RETRY);
1066 * Because we (had better!) have a ref on the vnode, once it
1067 * goes to VRECLAIMED state it will not be recycled until all
1068 * refs go away. So we can just check the flag.
1070 if (error == 0 && (vp->v_flag & VRECLAIMED)) {
1071 lockmgr(&vp->v_lock, LK_RELEASE);
1078 vn_unlock(struct vnode *vp)
1080 lockmgr(&vp->v_lock, LK_RELEASE);
1084 vn_islocked(struct vnode *vp)
1086 return (lockstatus(&vp->v_lock, curthread));
1090 * MPALMOSTSAFE - acquires mplock
1093 vn_closefile(struct file *fp)
1098 fp->f_ops = &badfileops;
1099 error = vn_close(((struct vnode *)fp->f_data), fp->f_flag);
1105 * MPALMOSTSAFE - acquires mplock
1108 vn_kqfilter(struct file *fp, struct knote *kn)
1113 error = VOP_KQFILTER(((struct vnode *)fp->f_data), kn);