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/sysctl.h>
58 #include <sys/syslog.h>
60 #include <sys/thread2.h>
61 #include <sys/mplock2.h>
63 static int vn_closefile (struct file
*fp
);
64 static int vn_ioctl (struct file
*fp
, u_long com
, caddr_t data
,
65 struct ucred
*cred
, struct sysmsg
*msg
);
66 static int vn_read (struct file
*fp
, struct uio
*uio
,
67 struct ucred
*cred
, int flags
);
68 static int vn_kqfilter (struct file
*fp
, struct knote
*kn
);
69 static int vn_statfile (struct file
*fp
, struct stat
*sb
, struct ucred
*cred
);
70 static int vn_write (struct file
*fp
, struct uio
*uio
,
71 struct ucred
*cred
, int flags
);
73 struct fileops vnode_fileops
= {
77 .fo_kqfilter
= vn_kqfilter
,
78 .fo_stat
= vn_statfile
,
79 .fo_close
= vn_closefile
,
80 .fo_shutdown
= nofo_shutdown
84 * Common code for vnode open operations. Check permissions, and call
85 * the VOP_NOPEN or VOP_NCREATE routine.
87 * The caller is responsible for setting up nd with nlookup_init() and
88 * for cleaning it up with nlookup_done(), whether we return an error
91 * On success nd->nl_open_vp will hold a referenced and, if requested,
92 * locked vnode. A locked vnode is requested via NLC_LOCKVP. If fp
93 * is non-NULL the vnode will be installed in the file pointer.
95 * NOTE: The vnode is referenced just once on return whether or not it
96 * is also installed in the file pointer.
99 vn_open(struct nlookupdata
*nd
, struct file
*fp
, int fmode
, int cmode
)
102 struct ucred
*cred
= nd
->nl_cred
;
104 struct vattr
*vap
= &vat
;
109 * Certain combinations are illegal
111 if ((fmode
& (FWRITE
| O_TRUNC
)) == O_TRUNC
)
115 * Lookup the path and create or obtain the vnode. After a
116 * successful lookup a locked nd->nl_nch will be returned.
118 * The result of this section should be a locked vnode.
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.
123 nd
->nl_flags
|= NLC_OPEN
;
124 if (fmode
& O_APPEND
)
125 nd
->nl_flags
|= NLC_APPEND
;
127 nd
->nl_flags
|= NLC_TRUNCATE
;
129 nd
->nl_flags
|= NLC_READ
;
131 nd
->nl_flags
|= NLC_WRITE
;
132 if ((fmode
& O_EXCL
) == 0 && (fmode
& O_NOFOLLOW
) == 0)
133 nd
->nl_flags
|= NLC_FOLLOW
;
135 if (fmode
& O_CREAT
) {
137 * CONDITIONAL CREATE FILE CASE
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
146 nd
->nl_flags
|= NLC_CREATE
;
147 nd
->nl_flags
|= NLC_REFDVP
;
152 * NORMAL OPEN FILE CASE
161 * split case to allow us to re-resolve and retry the ncp in case
165 if (fmode
& O_CREAT
) {
166 if (nd
->nl_nch
.ncp
->nc_vp
== NULL
) {
167 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
171 vap
->va_mode
= cmode
;
173 vap
->va_vaflags
|= VA_EXCLUSIVE
;
174 error
= VOP_NCREATE(&nd
->nl_nch
, nd
->nl_dvp
, &vp
,
179 /* locked vnode is returned */
181 if (fmode
& O_EXCL
) {
184 error
= cache_vget(&nd
->nl_nch
, cred
,
192 error
= cache_vget(&nd
->nl_nch
, cred
, LK_EXCLUSIVE
, &vp
);
198 * We have a locked vnode and ncp now. Note that the ncp will
199 * be cleaned up by the caller if nd->nl_nch is left intact.
201 if (vp
->v_type
== VLNK
) {
205 if (vp
->v_type
== VSOCK
) {
209 if ((fmode
& O_CREAT
) == 0) {
210 if (fmode
& (FWRITE
| O_TRUNC
)) {
211 if (vp
->v_type
== VDIR
) {
215 error
= vn_writechk(vp
, &nd
->nl_nch
);
218 * Special stale handling, re-resolve the
221 if (error
== ESTALE
) {
224 cache_setunresolved(&nd
->nl_nch
);
225 error
= cache_resolve(&nd
->nl_nch
, cred
);
233 if (fmode
& O_TRUNC
) {
234 vn_unlock(vp
); /* XXX */
235 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
); /* XXX */
238 error
= VOP_SETATTR(vp
, vap
, cred
);
244 * Set or clear VNSWAPCACHE on the vp based on nd->nl_nch.ncp->nc_flag.
245 * These particular bits a tracked all the way from the root.
247 * NOTE: Might not work properly on NFS servers due to the
248 * disconnected namecache.
250 flags
= nd
->nl_nch
.ncp
->nc_flag
;
251 if ((flags
& (NCF_UF_CACHE
| NCF_UF_PCACHE
)) &&
252 (flags
& (NCF_SF_NOCACHE
| NCF_SF_PNOCACHE
)) == 0) {
253 vsetflags(vp
, VSWAPCACHE
);
255 vclrflags(vp
, VSWAPCACHE
);
259 * Setup the fp so VOP_OPEN can override it. No descriptor has been
260 * associated with the fp yet so we own it clean.
262 * f_nchandle inherits nl_nch. This used to be necessary only for
263 * directories but now we do it unconditionally so f*() ops
264 * such as fchmod() can access the actual namespace that was
265 * used to open the file.
268 if (nd
->nl_flags
& NLC_APPENDONLY
)
269 fmode
|= FAPPENDONLY
;
270 fp
->f_nchandle
= nd
->nl_nch
;
271 cache_zero(&nd
->nl_nch
);
272 cache_unlock(&fp
->f_nchandle
);
276 * Get rid of nl_nch. vn_open does not return it (it returns the
277 * vnode or the file pointer). Note: we can't leave nl_nch locked
278 * through the VOP_OPEN anyway since the VOP_OPEN may block, e.g.
282 cache_put(&nd
->nl_nch
);
284 error
= VOP_OPEN(vp
, fmode
, cred
, fp
);
287 * setting f_ops to &badfileops will prevent the descriptor
288 * code from trying to close and release the vnode, since
289 * the open failed we do not want to call close.
293 fp
->f_ops
= &badfileops
;
300 * Assert that VREG files have been setup for vmio.
302 KASSERT(vp
->v_type
!= VREG
|| vp
->v_object
!= NULL
,
303 ("vn_open: regular file was not VMIO enabled!"));
307 * Return the vnode. XXX needs some cleaning up. The vnode is
308 * only returned in the fp == NULL case.
312 nd
->nl_vp_fmode
= fmode
;
313 if ((nd
->nl_flags
& NLC_LOCKVP
) == 0)
326 vn_opendisk(const char *devname
, int fmode
, struct vnode
**vpp
)
331 if (strncmp(devname
, "/dev/", 5) == 0)
333 if ((vp
= getsynthvnode(devname
)) == NULL
) {
336 error
= VOP_OPEN(vp
, fmode
, proc0
.p_ucred
, NULL
);
348 * Check for write permissions on the specified vnode. nch may be NULL.
351 vn_writechk(struct vnode
*vp
, struct nchandle
*nch
)
354 * If there's shared text associated with
355 * the vnode, try to free it up once. If
356 * we fail, we can't allow writing.
358 if (vp
->v_flag
& VTEXT
)
362 * If the vnode represents a regular file, check the mount
363 * point via the nch. This may be a different mount point
364 * then the one embedded in the vnode (e.g. nullfs).
366 * We can still write to non-regular files (e.g. devices)
367 * via read-only mounts.
369 if (nch
&& nch
->ncp
&& vp
->v_type
== VREG
)
370 return (ncp_writechk(nch
));
375 * Check whether the underlying mount is read-only. The mount point
376 * referenced by the namecache may be different from the mount point
377 * used by the underlying vnode in the case of NULLFS, so a separate
381 ncp_writechk(struct nchandle
*nch
)
383 if (nch
->mount
&& (nch
->mount
->mnt_flag
& MNT_RDONLY
))
394 vn_close(struct vnode
*vp
, int flags
)
398 error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
400 error
= VOP_CLOSE(vp
, flags
);
408 * Sequential heuristic.
410 * MPSAFE (f_seqcount and f_nextoff are allowed to race)
414 sequential_heuristic(struct uio
*uio
, struct file
*fp
)
417 * Sequential heuristic - detect sequential operation
419 * NOTE: SMP: We allow f_seqcount updates to race.
421 if ((uio
->uio_offset
== 0 && fp
->f_seqcount
> 0) ||
422 uio
->uio_offset
== fp
->f_nextoff
) {
423 int tmpseq
= fp
->f_seqcount
;
425 tmpseq
+= (uio
->uio_resid
+ BKVASIZE
- 1) / BKVASIZE
;
426 if (tmpseq
> IO_SEQMAX
)
428 fp
->f_seqcount
= tmpseq
;
429 return(fp
->f_seqcount
<< IO_SEQSHIFT
);
433 * Not sequential, quick draw-down of seqcount
435 * NOTE: SMP: We allow f_seqcount updates to race.
437 if (fp
->f_seqcount
> 1)
445 * get - lock and return the f_offset field.
446 * set - set and unlock the f_offset field.
448 * These routines serve the dual purpose of serializing access to the
449 * f_offset field (at least on i386) and guaranteeing operational integrity
450 * when multiple read()ers and write()ers are present on the same fp.
454 static __inline off_t
455 vn_get_fpf_offset(struct file
*fp
)
461 * Shortcut critical path.
463 flags
= fp
->f_flag
& ~FOFFSETLOCK
;
464 if (atomic_cmpset_int(&fp
->f_flag
, flags
, flags
| FOFFSETLOCK
))
465 return(fp
->f_offset
);
472 if (flags
& FOFFSETLOCK
) {
473 nflags
= flags
| FOFFSETWAKE
;
474 tsleep_interlock(&fp
->f_flag
, 0);
475 if (atomic_cmpset_int(&fp
->f_flag
, flags
, nflags
))
476 tsleep(&fp
->f_flag
, PINTERLOCKED
, "fpoff", 0);
478 nflags
= flags
| FOFFSETLOCK
;
479 if (atomic_cmpset_int(&fp
->f_flag
, flags
, nflags
))
483 return(fp
->f_offset
);
490 vn_set_fpf_offset(struct file
*fp
, off_t offset
)
496 * We hold the lock so we can set the offset without interference.
498 fp
->f_offset
= offset
;
501 * Normal release is already a reasonably critical path.
505 nflags
= flags
& ~(FOFFSETLOCK
| FOFFSETWAKE
);
506 if (atomic_cmpset_int(&fp
->f_flag
, flags
, nflags
)) {
507 if (flags
& FOFFSETWAKE
)
517 static __inline off_t
518 vn_poll_fpf_offset(struct file
*fp
)
520 #if defined(__x86_64__) || !defined(SMP)
521 return(fp
->f_offset
);
523 off_t off
= vn_get_fpf_offset(fp
);
524 vn_set_fpf_offset(fp
, off
);
530 * Package up an I/O request on a vnode into a uio and do it.
535 vn_rdwr(enum uio_rw rw
, struct vnode
*vp
, caddr_t base
, int len
,
536 off_t offset
, enum uio_seg segflg
, int ioflg
,
537 struct ucred
*cred
, int *aresid
)
541 struct ccms_lock ccms_lock
;
544 if ((ioflg
& IO_NODELOCKED
) == 0)
545 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
546 auio
.uio_iov
= &aiov
;
548 aiov
.iov_base
= base
;
550 auio
.uio_resid
= len
;
551 auio
.uio_offset
= offset
;
552 auio
.uio_segflg
= segflg
;
554 auio
.uio_td
= curthread
;
555 ccms_lock_get_uio(&vp
->v_ccms
, &ccms_lock
, &auio
);
556 if (rw
== UIO_READ
) {
557 error
= VOP_READ(vp
, &auio
, ioflg
, cred
);
559 error
= VOP_WRITE(vp
, &auio
, ioflg
, cred
);
561 ccms_lock_put(&vp
->v_ccms
, &ccms_lock
);
563 *aresid
= auio
.uio_resid
;
565 if (auio
.uio_resid
&& error
== 0)
567 if ((ioflg
& IO_NODELOCKED
) == 0)
573 * Package up an I/O request on a vnode into a uio and do it. The I/O
574 * request is split up into smaller chunks and we try to avoid saturating
575 * the buffer cache while potentially holding a vnode locked, so we
576 * check bwillwrite() before calling vn_rdwr(). We also call lwkt_user_yield()
577 * to give other processes a chance to lock the vnode (either other processes
578 * core'ing the same binary, or unrelated processes scanning the directory).
583 vn_rdwr_inchunks(enum uio_rw rw
, struct vnode
*vp
, caddr_t base
, int len
,
584 off_t offset
, enum uio_seg segflg
, int ioflg
,
585 struct ucred
*cred
, int *aresid
)
593 * Force `offset' to a multiple of MAXBSIZE except possibly
594 * for the first chunk, so that filesystems only need to
595 * write full blocks except possibly for the first and last
598 chunk
= MAXBSIZE
- (uoff_t
)offset
% MAXBSIZE
;
602 if (vp
->v_type
== VREG
) {
612 error
= vn_rdwr(rw
, vp
, base
, chunk
, offset
, segflg
,
613 ioflg
, cred
, aresid
);
614 len
-= chunk
; /* aresid calc already includes length */
627 * File pointers can no longer get ripped up by revoke so
628 * we don't need to lock access to the vp.
630 * f_offset updates are not guaranteed against multiple readers
635 vn_read(struct file
*fp
, struct uio
*uio
, struct ucred
*cred
, int flags
)
637 struct ccms_lock ccms_lock
;
641 KASSERT(uio
->uio_td
== curthread
,
642 ("uio_td %p is not td %p", uio
->uio_td
, curthread
));
643 vp
= (struct vnode
*)fp
->f_data
;
646 if (flags
& O_FRNONBLOCKING
) {
647 ioflag
|= (IO_NDELAY
| IO_NRDELAY
);
648 } else if (flags
& O_FBLOCKING
) {
649 /* ioflag &= ~IO_NDELAY; */
650 } else if (flags
& O_FNONBLOCKING
) {
652 } else if (fp
->f_flag
& FNONBLOCK
) {
655 if (flags
& O_FBUFFERED
) {
656 /* ioflag &= ~IO_DIRECT; */
657 } else if (flags
& O_FUNBUFFERED
) {
659 } else if (fp
->f_flag
& O_DIRECT
) {
662 if ((flags
& O_FOFFSET
) == 0 && (vp
->v_flag
& VNOTSEEKABLE
) == 0)
663 uio
->uio_offset
= vn_get_fpf_offset(fp
);
664 vn_lock(vp
, LK_SHARED
| LK_RETRY
);
665 ioflag
|= sequential_heuristic(uio
, fp
);
667 ccms_lock_get_uio(&vp
->v_ccms
, &ccms_lock
, uio
);
668 error
= VOP_READ(vp
, uio
, ioflag
, cred
);
669 ccms_lock_put(&vp
->v_ccms
, &ccms_lock
);
670 fp
->f_nextoff
= uio
->uio_offset
;
672 if ((flags
& O_FOFFSET
) == 0 && (vp
->v_flag
& VNOTSEEKABLE
) == 0)
673 vn_set_fpf_offset(fp
, uio
->uio_offset
);
681 vn_write(struct file
*fp
, struct uio
*uio
, struct ucred
*cred
, int flags
)
683 struct ccms_lock ccms_lock
;
687 KASSERT(uio
->uio_td
== curthread
,
688 ("uio_td %p is not p %p", uio
->uio_td
, curthread
));
689 vp
= (struct vnode
*)fp
->f_data
;
692 if (vp
->v_type
== VREG
&&
693 ((fp
->f_flag
& O_APPEND
) || (flags
& O_FAPPEND
))) {
697 if (flags
& O_FBLOCKING
) {
698 /* ioflag &= ~IO_NDELAY; */
699 } else if (flags
& O_FNONBLOCKING
) {
701 } else if (fp
->f_flag
& FNONBLOCK
) {
704 if (flags
& O_FBUFFERED
) {
705 /* ioflag &= ~IO_DIRECT; */
706 } else if (flags
& O_FUNBUFFERED
) {
708 } else if (fp
->f_flag
& O_DIRECT
) {
711 if (flags
& O_FASYNCWRITE
) {
712 /* ioflag &= ~IO_SYNC; */
713 } else if (flags
& O_FSYNCWRITE
) {
715 } else if (fp
->f_flag
& O_FSYNC
) {
719 if (vp
->v_mount
&& (vp
->v_mount
->mnt_flag
& MNT_SYNCHRONOUS
))
721 if ((flags
& O_FOFFSET
) == 0)
722 uio
->uio_offset
= vn_get_fpf_offset(fp
);
723 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
724 ioflag
|= sequential_heuristic(uio
, fp
);
725 ccms_lock_get_uio(&vp
->v_ccms
, &ccms_lock
, uio
);
726 error
= VOP_WRITE(vp
, uio
, ioflag
, cred
);
727 ccms_lock_put(&vp
->v_ccms
, &ccms_lock
);
728 fp
->f_nextoff
= uio
->uio_offset
;
730 if ((flags
& O_FOFFSET
) == 0)
731 vn_set_fpf_offset(fp
, uio
->uio_offset
);
739 vn_statfile(struct file
*fp
, struct stat
*sb
, struct ucred
*cred
)
744 vp
= (struct vnode
*)fp
->f_data
;
745 error
= vn_stat(vp
, sb
, cred
);
753 vn_stat(struct vnode
*vp
, struct stat
*sb
, struct ucred
*cred
)
762 error
= VOP_GETATTR(vp
, vap
);
767 * Zero the spare stat fields
774 * Copy from vattr table
776 if (vap
->va_fsid
!= VNOVAL
)
777 sb
->st_dev
= vap
->va_fsid
;
779 sb
->st_dev
= vp
->v_mount
->mnt_stat
.f_fsid
.val
[0];
780 sb
->st_ino
= vap
->va_fileid
;
782 switch (vap
->va_type
) {
800 /* This is a cosmetic change, symlinks do not have a mode. */
801 if (vp
->v_mount
->mnt_flag
& MNT_NOSYMFOLLOW
)
802 sb
->st_mode
&= ~ACCESSPERMS
; /* 0000 */
804 sb
->st_mode
|= ACCESSPERMS
; /* 0777 */
816 if (vap
->va_nlink
> (nlink_t
)-1)
817 sb
->st_nlink
= (nlink_t
)-1;
819 sb
->st_nlink
= vap
->va_nlink
;
820 sb
->st_uid
= vap
->va_uid
;
821 sb
->st_gid
= vap
->va_gid
;
822 sb
->st_rdev
= dev2udev(vp
->v_rdev
);
823 sb
->st_size
= vap
->va_size
;
824 sb
->st_atimespec
= vap
->va_atime
;
825 sb
->st_mtimespec
= vap
->va_mtime
;
826 sb
->st_ctimespec
= vap
->va_ctime
;
829 * A VCHR and VBLK device may track the last access and last modified
830 * time independantly of the filesystem. This is particularly true
831 * because device read and write calls may bypass the filesystem.
833 if (vp
->v_type
== VCHR
|| vp
->v_type
== VBLK
) {
836 if (dev
->si_lastread
) {
837 sb
->st_atimespec
.tv_sec
= dev
->si_lastread
;
838 sb
->st_atimespec
.tv_nsec
= 0;
840 if (dev
->si_lastwrite
) {
841 sb
->st_atimespec
.tv_sec
= dev
->si_lastwrite
;
842 sb
->st_atimespec
.tv_nsec
= 0;
848 * According to www.opengroup.org, the meaning of st_blksize is
849 * "a filesystem-specific preferred I/O block size for this
850 * object. In some filesystem types, this may vary from file
852 * Default to PAGE_SIZE after much discussion.
855 if (vap
->va_type
== VREG
) {
856 sb
->st_blksize
= vap
->va_blocksize
;
857 } else if (vn_isdisk(vp
, NULL
)) {
859 * XXX this is broken. If the device is not yet open (aka
860 * stat() call, aka v_rdev == NULL), how are we supposed
861 * to get a valid block size out of it?
865 sb
->st_blksize
= dev
->si_bsize_best
;
866 if (sb
->st_blksize
< dev
->si_bsize_phys
)
867 sb
->st_blksize
= dev
->si_bsize_phys
;
868 if (sb
->st_blksize
< BLKDEV_IOSIZE
)
869 sb
->st_blksize
= BLKDEV_IOSIZE
;
871 sb
->st_blksize
= PAGE_SIZE
;
874 sb
->st_flags
= vap
->va_flags
;
876 error
= priv_check_cred(cred
, PRIV_VFS_GENERATION
, 0);
880 sb
->st_gen
= (u_int32_t
)vap
->va_gen
;
882 sb
->st_blocks
= vap
->va_bytes
/ S_BLKSIZE
;
887 * MPALMOSTSAFE - acquires mplock
890 vn_ioctl(struct file
*fp
, u_long com
, caddr_t data
, struct ucred
*ucred
,
893 struct vnode
*vp
= ((struct vnode
*)fp
->f_data
);
899 switch (vp
->v_type
) {
902 if (com
== FIONREAD
) {
903 error
= VOP_GETATTR(vp
, &vattr
);
906 size
= vattr
.va_size
;
907 if ((vp
->v_flag
& VNOTSEEKABLE
) == 0)
908 size
-= vn_poll_fpf_offset(fp
);
909 if (size
> 0x7FFFFFFF)
915 if (com
== FIOASYNC
) { /* XXX */
927 if (com
== FIODTYPE
) {
928 if (vp
->v_type
!= VCHR
&& vp
->v_type
!= VBLK
) {
932 *(int *)data
= dev_dflags(vp
->v_rdev
) & D_TYPEMASK
;
936 error
= VOP_IOCTL(vp
, com
, data
, fp
->f_flag
, ucred
, msg
);
937 if (error
== 0 && com
== TIOCSCTTY
) {
938 struct proc
*p
= curthread
->td_proc
;
939 struct session
*sess
;
948 /* Do nothing if reassigning same control tty */
949 if (sess
->s_ttyvp
== vp
) {
955 /* Get rid of reference to old control tty */
969 * Check that the vnode is still valid, and if so
970 * acquire requested lock.
974 vn_lock(struct vnode
*vp
, int flags
)
976 debug_vn_lock(struct vnode
*vp
, int flags
, const char *filename
, int line
)
983 vp
->filename
= filename
;
985 error
= debuglockmgr(&vp
->v_lock
, flags
,
986 "vn_lock", filename
, line
);
988 error
= lockmgr(&vp
->v_lock
, flags
);
992 } while (flags
& LK_RETRY
);
995 * Because we (had better!) have a ref on the vnode, once it
996 * goes to VRECLAIMED state it will not be recycled until all
997 * refs go away. So we can just check the flag.
999 if (error
== 0 && (vp
->v_flag
& VRECLAIMED
)) {
1000 lockmgr(&vp
->v_lock
, LK_RELEASE
);
1010 vn_unlock(struct vnode
*vp
)
1012 lockmgr(&vp
->v_lock
, LK_RELEASE
);
1019 vn_islocked(struct vnode
*vp
)
1021 return (lockstatus(&vp
->v_lock
, curthread
));
1025 * Return the lock status of a vnode and unlock the vnode
1026 * if we owned the lock. This is not a boolean, if the
1027 * caller cares what the lock status is the caller must
1028 * check the various possible values.
1030 * This only unlocks exclusive locks held by the caller,
1031 * it will NOT unlock shared locks (there is no way to
1032 * tell who the shared lock belongs to).
1037 vn_islocked_unlock(struct vnode
*vp
)
1041 vpls
= lockstatus(&vp
->v_lock
, curthread
);
1042 if (vpls
== LK_EXCLUSIVE
)
1043 lockmgr(&vp
->v_lock
, LK_RELEASE
);
1048 * Restore a vnode lock that we previously released via
1049 * vn_islocked_unlock(). This is a NOP if we did not
1050 * own the original lock.
1055 vn_islocked_relock(struct vnode
*vp
, int vpls
)
1059 if (vpls
== LK_EXCLUSIVE
)
1060 error
= lockmgr(&vp
->v_lock
, vpls
);
1067 vn_closefile(struct file
*fp
)
1071 fp
->f_ops
= &badfileops
;
1072 error
= vn_close(((struct vnode
*)fp
->f_data
), fp
->f_flag
);
1080 vn_kqfilter(struct file
*fp
, struct knote
*kn
)
1084 error
= VOP_KQFILTER(((struct vnode
*)fp
->f_data
), kn
);