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>
62 static int vn_closefile (struct file *fp);
63 static int vn_ioctl (struct file *fp, u_long com, caddr_t data,
65 static int vn_read (struct file *fp, struct uio *uio,
66 struct ucred *cred, int flags);
67 static int svn_read (struct file *fp, struct uio *uio,
68 struct ucred *cred, int flags);
69 static int vn_poll (struct file *fp, int events, struct ucred *cred);
70 static int vn_kqfilter (struct file *fp, struct knote *kn);
71 static int vn_statfile (struct file *fp, struct stat *sb, struct ucred *cred);
72 static int vn_write (struct file *fp, struct uio *uio,
73 struct ucred *cred, int flags);
74 static int svn_write (struct file *fp, struct uio *uio,
75 struct ucred *cred, int flags);
78 static int read_mpsafe = 0;
79 SYSCTL_INT(_vfs, OID_AUTO, read_mpsafe, CTLFLAG_RW, &read_mpsafe, 0, "");
80 static int write_mpsafe = 0;
81 SYSCTL_INT(_vfs, OID_AUTO, write_mpsafe, CTLFLAG_RW, &write_mpsafe, 0, "");
82 static int getattr_mpsafe = 0;
83 SYSCTL_INT(_vfs, OID_AUTO, getattr_mpsafe, CTLFLAG_RW, &getattr_mpsafe, 0, "");
86 #define write_mpsafe 0
87 #define getattr_mpsafe 0
90 struct fileops vnode_fileops = {
95 .fo_kqfilter = vn_kqfilter,
96 .fo_stat = vn_statfile,
97 .fo_close = vn_closefile,
98 .fo_shutdown = nofo_shutdown
101 struct fileops specvnode_fileops = {
103 .fo_write = svn_write,
104 .fo_ioctl = vn_ioctl,
106 .fo_kqfilter = vn_kqfilter,
107 .fo_stat = vn_statfile,
108 .fo_close = vn_closefile,
109 .fo_shutdown = nofo_shutdown
113 * Shortcut the device read/write. This avoids a lot of vnode junk.
114 * Basically the specfs vnops for read and write take the locked vnode,
115 * unlock it (because we can't hold the vnode locked while reading or writing
116 * a device which may block indefinitely), issues the device operation, then
117 * relock the vnode before returning, plus other junk. This bypasses all
118 * of that and just does the device operation.
121 vn_setspecops(struct file *fp)
123 if (vfs_fastdev && fp->f_ops == &vnode_fileops) {
124 fp->f_ops = &specvnode_fileops;
129 * Common code for vnode open operations. Check permissions, and call
130 * the VOP_NOPEN or VOP_NCREATE routine.
132 * The caller is responsible for setting up nd with nlookup_init() and
133 * for cleaning it up with nlookup_done(), whether we return an error
136 * On success nd->nl_open_vp will hold a referenced and, if requested,
137 * locked vnode. A locked vnode is requested via NLC_LOCKVP. If fp
138 * is non-NULL the vnode will be installed in the file pointer.
140 * NOTE: The vnode is referenced just once on return whether or not it
141 * is also installed in the file pointer.
144 vn_open(struct nlookupdata *nd, struct file *fp, int fmode, int cmode)
147 struct ucred *cred = nd->nl_cred;
149 struct vattr *vap = &vat;
153 * Certain combinations are illegal
155 if ((fmode & (FWRITE | O_TRUNC)) == O_TRUNC)
159 * Lookup the path and create or obtain the vnode. After a
160 * successful lookup a locked nd->nl_nch will be returned.
162 * The result of this section should be a locked vnode.
164 * XXX with only a little work we should be able to avoid locking
165 * the vnode if FWRITE, O_CREAT, and O_TRUNC are *not* set.
167 nd->nl_flags |= NLC_OPEN;
168 if (fmode & O_APPEND)
169 nd->nl_flags |= NLC_APPEND;
171 nd->nl_flags |= NLC_TRUNCATE;
173 nd->nl_flags |= NLC_READ;
175 nd->nl_flags |= NLC_WRITE;
176 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
177 nd->nl_flags |= NLC_FOLLOW;
179 if (fmode & O_CREAT) {
181 * CONDITIONAL CREATE FILE CASE
183 * Setting NLC_CREATE causes a negative hit to store
184 * the negative hit ncp and not return an error. Then
185 * nc_error or nc_vp may be checked to see if the ncp
186 * represents a negative hit. NLC_CREATE also requires
187 * write permission on the governing directory or EPERM
190 nd->nl_flags |= NLC_CREATE;
191 nd->nl_flags |= NLC_REFDVP;
196 * NORMAL OPEN FILE CASE
205 * split case to allow us to re-resolve and retry the ncp in case
209 if (fmode & O_CREAT) {
210 if (nd->nl_nch.ncp->nc_vp == NULL) {
211 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
215 vap->va_mode = cmode;
217 vap->va_vaflags |= VA_EXCLUSIVE;
218 error = VOP_NCREATE(&nd->nl_nch, nd->nl_dvp, &vp,
223 /* locked vnode is returned */
225 if (fmode & O_EXCL) {
228 error = cache_vget(&nd->nl_nch, cred,
236 error = cache_vget(&nd->nl_nch, cred, LK_EXCLUSIVE, &vp);
242 * We have a locked vnode and ncp now. Note that the ncp will
243 * be cleaned up by the caller if nd->nl_nch is left intact.
245 if (vp->v_type == VLNK) {
249 if (vp->v_type == VSOCK) {
253 if ((fmode & O_CREAT) == 0) {
254 if (fmode & (FWRITE | O_TRUNC)) {
255 if (vp->v_type == VDIR) {
259 error = vn_writechk(vp, &nd->nl_nch);
262 * Special stale handling, re-resolve the
265 if (error == ESTALE) {
268 cache_setunresolved(&nd->nl_nch);
269 error = cache_resolve(&nd->nl_nch, cred);
277 if (fmode & O_TRUNC) {
278 vn_unlock(vp); /* XXX */
279 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
282 error = VOP_SETATTR(vp, vap, cred);
288 * Setup the fp so VOP_OPEN can override it. No descriptor has been
289 * associated with the fp yet so we own it clean.
291 * f_nchandle inherits nl_nch. This used to be necessary only for
292 * directories but now we do it unconditionally so f*() ops
293 * such as fchmod() can access the actual namespace that was
294 * used to open the file.
297 if (nd->nl_flags & NLC_APPENDONLY)
298 fmode |= FAPPENDONLY;
299 fp->f_nchandle = nd->nl_nch;
300 cache_zero(&nd->nl_nch);
301 cache_unlock(&fp->f_nchandle);
305 * Get rid of nl_nch. vn_open does not return it (it returns the
306 * vnode or the file pointer). Note: we can't leave nl_nch locked
307 * through the VOP_OPEN anyway since the VOP_OPEN may block, e.g.
311 cache_put(&nd->nl_nch);
313 error = VOP_OPEN(vp, fmode, cred, fp);
316 * setting f_ops to &badfileops will prevent the descriptor
317 * code from trying to close and release the vnode, since
318 * the open failed we do not want to call close.
322 fp->f_ops = &badfileops;
329 * Assert that VREG files have been setup for vmio.
331 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
332 ("vn_open: regular file was not VMIO enabled!"));
336 * Return the vnode. XXX needs some cleaning up. The vnode is
337 * only returned in the fp == NULL case.
341 nd->nl_vp_fmode = fmode;
342 if ((nd->nl_flags & NLC_LOCKVP) == 0)
355 vn_opendisk(const char *devname, int fmode, struct vnode **vpp)
360 if (strncmp(devname, "/dev/", 5) == 0)
362 if ((vp = getsynthvnode(devname)) == NULL) {
365 error = VOP_OPEN(vp, fmode, proc0.p_ucred, NULL);
377 * Check for write permissions on the specified vnode. nch may be NULL.
380 vn_writechk(struct vnode *vp, struct nchandle *nch)
383 * If there's shared text associated with
384 * the vnode, try to free it up once. If
385 * we fail, we can't allow writing.
387 if (vp->v_flag & VTEXT)
391 * If the vnode represents a regular file, check the mount
392 * point via the nch. This may be a different mount point
393 * then the one embedded in the vnode (e.g. nullfs).
395 * We can still write to non-regular files (e.g. devices)
396 * via read-only mounts.
398 if (nch && nch->ncp && vp->v_type == VREG)
399 return (ncp_writechk(nch));
404 * Check whether the underlying mount is read-only. The mount point
405 * referenced by the namecache may be different from the mount point
406 * used by the underlying vnode in the case of NULLFS, so a separate
410 ncp_writechk(struct nchandle *nch)
412 if (nch->mount && (nch->mount->mnt_flag & MNT_RDONLY))
421 vn_close(struct vnode *vp, int flags)
425 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
427 error = VOP_CLOSE(vp, flags);
436 sequential_heuristic(struct uio *uio, struct file *fp)
439 * Sequential heuristic - detect sequential operation
441 * NOTE: SMP: We allow f_seqcount updates to race.
443 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
444 uio->uio_offset == fp->f_nextoff) {
445 int tmpseq = fp->f_seqcount;
447 * XXX we assume that the filesystem block size is
448 * the default. Not true, but still gives us a pretty
449 * good indicator of how sequential the read operations
452 tmpseq += (uio->uio_resid + BKVASIZE - 1) / BKVASIZE;
453 if (tmpseq > IO_SEQMAX)
455 fp->f_seqcount = tmpseq;
456 return(fp->f_seqcount << IO_SEQSHIFT);
460 * Not sequential, quick draw-down of seqcount
462 * NOTE: SMP: We allow f_seqcount updates to race.
464 if (fp->f_seqcount > 1)
472 * get - lock and return the f_offset field.
473 * set - set and unlock the f_offset field.
475 * These routines serve the dual purpose of serializing access to the
476 * f_offset field (at least on i386) and guaranteeing operational integrity
477 * when multiple read()ers and write()ers are present on the same fp.
479 static __inline off_t
480 vn_get_fpf_offset(struct file *fp)
486 * Shortcut critical path.
488 flags = fp->f_flag & ~FOFFSETLOCK;
489 if (atomic_cmpset_int(&fp->f_flag, flags, flags | FOFFSETLOCK))
490 return(fp->f_offset);
497 if (flags & FOFFSETLOCK) {
498 nflags = flags | FOFFSETWAKE;
499 tsleep_interlock(&fp->f_flag, 0);
500 if (atomic_cmpset_int(&fp->f_flag, flags, nflags))
501 tsleep(&fp->f_flag, PINTERLOCKED, "fpoff", 0);
503 nflags = flags | FOFFSETLOCK;
504 if (atomic_cmpset_int(&fp->f_flag, flags, nflags))
508 return(fp->f_offset);
512 vn_set_fpf_offset(struct file *fp, off_t offset)
518 * We hold the lock so we can set the offset without interference.
520 fp->f_offset = offset;
523 * Normal release is already a reasonably critical path.
527 nflags = flags & ~(FOFFSETLOCK | FOFFSETWAKE);
528 if (atomic_cmpset_int(&fp->f_flag, flags, nflags)) {
529 if (flags & FOFFSETWAKE)
536 static __inline off_t
537 vn_poll_fpf_offset(struct file *fp)
539 #if defined(__amd64__) || !defined(SMP)
540 return(fp->f_offset);
542 off_t off = vn_get_fpf_offset(fp);
543 vn_set_fpf_offset(fp, off);
549 * Package up an I/O request on a vnode into a uio and do it.
552 vn_rdwr(enum uio_rw rw, struct vnode *vp, caddr_t base, int len,
553 off_t offset, enum uio_seg segflg, int ioflg,
554 struct ucred *cred, int *aresid)
558 struct ccms_lock ccms_lock;
561 if ((ioflg & IO_NODELOCKED) == 0)
562 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
563 auio.uio_iov = &aiov;
565 aiov.iov_base = base;
567 auio.uio_resid = len;
568 auio.uio_offset = offset;
569 auio.uio_segflg = segflg;
571 auio.uio_td = curthread;
572 ccms_lock_get_uio(&vp->v_ccms, &ccms_lock, &auio);
573 if (rw == UIO_READ) {
574 error = VOP_READ(vp, &auio, ioflg, cred);
576 error = VOP_WRITE(vp, &auio, ioflg, cred);
578 ccms_lock_put(&vp->v_ccms, &ccms_lock);
580 *aresid = auio.uio_resid;
582 if (auio.uio_resid && error == 0)
584 if ((ioflg & IO_NODELOCKED) == 0)
590 * Package up an I/O request on a vnode into a uio and do it. The I/O
591 * request is split up into smaller chunks and we try to avoid saturating
592 * the buffer cache while potentially holding a vnode locked, so we
593 * check bwillwrite() before calling vn_rdwr(). We also call uio_yield()
594 * to give other processes a chance to lock the vnode (either other processes
595 * core'ing the same binary, or unrelated processes scanning the directory).
598 vn_rdwr_inchunks(enum uio_rw rw, struct vnode *vp, caddr_t base, int len,
599 off_t offset, enum uio_seg segflg, int ioflg,
600 struct ucred *cred, int *aresid)
608 * Force `offset' to a multiple of MAXBSIZE except possibly
609 * for the first chunk, so that filesystems only need to
610 * write full blocks except possibly for the first and last
613 chunk = MAXBSIZE - (uoff_t)offset % MAXBSIZE;
617 if (vp->v_type == VREG) {
627 error = vn_rdwr(rw, vp, base, chunk, offset, segflg,
628 ioflg, cred, aresid);
629 len -= chunk; /* aresid calc already includes length */
642 * MPALMOSTSAFE - acquires mplock
644 * File pointers can no longer get ripped up by revoke so
645 * we don't need to lock access to the vp.
647 * f_offset updates are not guaranteed against multiple readers
650 vn_read(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
652 struct ccms_lock ccms_lock;
656 KASSERT(uio->uio_td == curthread,
657 ("uio_td %p is not td %p", uio->uio_td, curthread));
658 vp = (struct vnode *)fp->f_data;
661 if (flags & O_FBLOCKING) {
662 /* ioflag &= ~IO_NDELAY; */
663 } else if (flags & O_FNONBLOCKING) {
665 } else if (fp->f_flag & FNONBLOCK) {
668 if (flags & O_FBUFFERED) {
669 /* ioflag &= ~IO_DIRECT; */
670 } else if (flags & O_FUNBUFFERED) {
672 } else if (fp->f_flag & O_DIRECT) {
675 if ((flags & O_FOFFSET) == 0 && (vp->v_flag & VNOTSEEKABLE) == 0)
676 uio->uio_offset = vn_get_fpf_offset(fp);
677 vn_lock(vp, LK_SHARED | LK_RETRY);
678 ioflag |= sequential_heuristic(uio, fp);
680 ccms_lock_get_uio(&vp->v_ccms, &ccms_lock, uio);
681 if (read_mpsafe && (vp->v_flag & VMP_READ)) {
682 error = VOP_READ(vp, uio, ioflag, cred);
685 error = VOP_READ(vp, uio, ioflag, cred);
688 ccms_lock_put(&vp->v_ccms, &ccms_lock);
689 fp->f_nextoff = uio->uio_offset;
691 if ((flags & O_FOFFSET) == 0 && (vp->v_flag & VNOTSEEKABLE) == 0)
692 vn_set_fpf_offset(fp, uio->uio_offset);
697 * Device-optimized file table vnode read routine.
699 * This bypasses the VOP table and talks directly to the device. Most
700 * filesystems just route to specfs and can make this optimization.
702 * MPALMOSTSAFE - acquires mplock
705 svn_read(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
713 KASSERT(uio->uio_td == curthread,
714 ("uio_td %p is not td %p", uio->uio_td, curthread));
716 vp = (struct vnode *)fp->f_data;
717 if (vp == NULL || vp->v_type == VBAD) {
722 if ((dev = vp->v_rdev) == NULL) {
728 if (uio->uio_resid == 0) {
732 if ((flags & O_FOFFSET) == 0 && (vp->v_flag & VNOTSEEKABLE) == 0)
733 uio->uio_offset = vn_get_fpf_offset(fp);
736 if (flags & O_FBLOCKING) {
737 /* ioflag &= ~IO_NDELAY; */
738 } else if (flags & O_FNONBLOCKING) {
740 } else if (fp->f_flag & FNONBLOCK) {
743 if (flags & O_FBUFFERED) {
744 /* ioflag &= ~IO_DIRECT; */
745 } else if (flags & O_FUNBUFFERED) {
747 } else if (fp->f_flag & O_DIRECT) {
750 ioflag |= sequential_heuristic(uio, fp);
752 error = dev_dread(dev, uio, ioflag);
755 fp->f_nextoff = uio->uio_offset;
756 if ((flags & O_FOFFSET) == 0 && (vp->v_flag & VNOTSEEKABLE) == 0)
757 vn_set_fpf_offset(fp, uio->uio_offset);
764 * MPALMOSTSAFE - acquires mplock
767 vn_write(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
769 struct ccms_lock ccms_lock;
773 KASSERT(uio->uio_td == curthread,
774 ("uio_td %p is not p %p", uio->uio_td, curthread));
775 vp = (struct vnode *)fp->f_data;
778 if (vp->v_type == VREG &&
779 ((fp->f_flag & O_APPEND) || (flags & O_FAPPEND))) {
783 if (flags & O_FBLOCKING) {
784 /* ioflag &= ~IO_NDELAY; */
785 } else if (flags & O_FNONBLOCKING) {
787 } else if (fp->f_flag & FNONBLOCK) {
790 if (flags & O_FBUFFERED) {
791 /* ioflag &= ~IO_DIRECT; */
792 } else if (flags & O_FUNBUFFERED) {
794 } else if (fp->f_flag & O_DIRECT) {
797 if (flags & O_FASYNCWRITE) {
798 /* ioflag &= ~IO_SYNC; */
799 } else if (flags & O_FSYNCWRITE) {
801 } else if (fp->f_flag & O_FSYNC) {
805 if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))
807 if ((flags & O_FOFFSET) == 0)
808 uio->uio_offset = vn_get_fpf_offset(fp);
809 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
810 ioflag |= sequential_heuristic(uio, fp);
811 ccms_lock_get_uio(&vp->v_ccms, &ccms_lock, uio);
812 if (write_mpsafe && (vp->v_flag & VMP_WRITE)) {
813 error = VOP_WRITE(vp, uio, ioflag, cred);
816 error = VOP_WRITE(vp, uio, ioflag, cred);
819 ccms_lock_put(&vp->v_ccms, &ccms_lock);
820 fp->f_nextoff = uio->uio_offset;
822 if ((flags & O_FOFFSET) == 0)
823 vn_set_fpf_offset(fp, uio->uio_offset);
828 * Device-optimized file table vnode write routine.
830 * This bypasses the VOP table and talks directly to the device. Most
831 * filesystems just route to specfs and can make this optimization.
833 * MPALMOSTSAFE - acquires mplock
836 svn_write(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
844 KASSERT(uio->uio_td == curthread,
845 ("uio_td %p is not p %p", uio->uio_td, curthread));
847 vp = (struct vnode *)fp->f_data;
848 if (vp == NULL || vp->v_type == VBAD) {
852 if (vp->v_type == VREG)
853 bwillwrite(uio->uio_resid);
854 vp = (struct vnode *)fp->f_data; /* XXX needed? */
856 if ((dev = vp->v_rdev) == NULL) {
862 if ((flags & O_FOFFSET) == 0)
863 uio->uio_offset = vn_get_fpf_offset(fp);
866 if (vp->v_type == VREG &&
867 ((fp->f_flag & O_APPEND) || (flags & O_FAPPEND))) {
871 if (flags & O_FBLOCKING) {
872 /* ioflag &= ~IO_NDELAY; */
873 } else if (flags & O_FNONBLOCKING) {
875 } else if (fp->f_flag & FNONBLOCK) {
878 if (flags & O_FBUFFERED) {
879 /* ioflag &= ~IO_DIRECT; */
880 } else if (flags & O_FUNBUFFERED) {
882 } else if (fp->f_flag & O_DIRECT) {
885 if (flags & O_FASYNCWRITE) {
886 /* ioflag &= ~IO_SYNC; */
887 } else if (flags & O_FSYNCWRITE) {
889 } else if (fp->f_flag & O_FSYNC) {
893 if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))
895 ioflag |= sequential_heuristic(uio, fp);
897 error = dev_dwrite(dev, uio, ioflag);
900 fp->f_nextoff = uio->uio_offset;
901 if ((flags & O_FOFFSET) == 0)
902 vn_set_fpf_offset(fp, uio->uio_offset);
912 vn_statfile(struct file *fp, struct stat *sb, struct ucred *cred)
917 vp = (struct vnode *)fp->f_data;
918 error = vn_stat(vp, sb, cred);
923 * MPSAFE (if vnode has VMP_GETATTR)
926 vn_stat(struct vnode *vp, struct stat *sb, struct ucred *cred)
935 if (getattr_mpsafe && (vp->v_flag & VMP_GETATTR)) {
936 error = VOP_GETATTR(vp, vap);
939 error = VOP_GETATTR(vp, vap);
946 * Zero the spare stat fields
952 * Copy from vattr table
954 if (vap->va_fsid != VNOVAL)
955 sb->st_dev = vap->va_fsid;
957 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
958 sb->st_ino = vap->va_fileid;
960 switch (vap->va_type) {
978 /* This is a cosmetic change, symlinks do not have a mode. */
979 if (vp->v_mount->mnt_flag & MNT_NOSYMFOLLOW)
980 sb->st_mode &= ~ACCESSPERMS; /* 0000 */
982 sb->st_mode |= ACCESSPERMS; /* 0777 */
994 if (vap->va_nlink > (nlink_t)-1)
995 sb->st_nlink = (nlink_t)-1;
997 sb->st_nlink = vap->va_nlink;
998 sb->st_uid = vap->va_uid;
999 sb->st_gid = vap->va_gid;
1000 sb->st_rdev = makeudev(vap->va_rmajor, vap->va_rminor);
1001 sb->st_size = vap->va_size;
1002 sb->st_atimespec = vap->va_atime;
1003 sb->st_mtimespec = vap->va_mtime;
1004 sb->st_ctimespec = vap->va_ctime;
1007 * A VCHR and VBLK device may track the last access and last modified
1008 * time independantly of the filesystem. This is particularly true
1009 * because device read and write calls may bypass the filesystem.
1011 if (vp->v_type == VCHR || vp->v_type == VBLK) {
1014 if (dev->si_lastread) {
1015 sb->st_atimespec.tv_sec = dev->si_lastread;
1016 sb->st_atimespec.tv_nsec = 0;
1018 if (dev->si_lastwrite) {
1019 sb->st_atimespec.tv_sec = dev->si_lastwrite;
1020 sb->st_atimespec.tv_nsec = 0;
1026 * According to www.opengroup.org, the meaning of st_blksize is
1027 * "a filesystem-specific preferred I/O block size for this
1028 * object. In some filesystem types, this may vary from file
1030 * Default to PAGE_SIZE after much discussion.
1033 if (vap->va_type == VREG) {
1034 sb->st_blksize = vap->va_blocksize;
1035 } else if (vn_isdisk(vp, NULL)) {
1037 * XXX this is broken. If the device is not yet open (aka
1038 * stat() call, aka v_rdev == NULL), how are we supposed
1039 * to get a valid block size out of it?
1042 if (dev == NULL && vp->v_type == VCHR) {
1044 dev = get_dev(vp->v_umajor, vp->v_uminor);
1047 sb->st_blksize = dev->si_bsize_best;
1048 if (sb->st_blksize < dev->si_bsize_phys)
1049 sb->st_blksize = dev->si_bsize_phys;
1050 if (sb->st_blksize < BLKDEV_IOSIZE)
1051 sb->st_blksize = BLKDEV_IOSIZE;
1053 sb->st_blksize = PAGE_SIZE;
1056 sb->st_flags = vap->va_flags;
1058 error = priv_check_cred(cred, PRIV_VFS_GENERATION, 0);
1062 sb->st_gen = (u_int32_t)vap->va_gen;
1064 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
1065 sb->st_fsmid = vap->va_fsmid;
1070 * MPALMOSTSAFE - acquires mplock
1073 vn_ioctl(struct file *fp, u_long com, caddr_t data, struct ucred *ucred)
1075 struct vnode *vp = ((struct vnode *)fp->f_data);
1083 switch (vp->v_type) {
1086 if (com == FIONREAD) {
1087 error = VOP_GETATTR(vp, &vattr);
1090 size = vattr.va_size;
1091 if ((vp->v_flag & VNOTSEEKABLE) == 0)
1092 size -= vn_poll_fpf_offset(fp);
1093 if (size > 0x7FFFFFFF)
1095 *(int *)data = size;
1099 if (com == FIOASYNC) { /* XXX */
1100 error = 0; /* XXX */
1111 if (com == FIODTYPE) {
1112 if (vp->v_type != VCHR && vp->v_type != VBLK) {
1116 *(int *)data = dev_dflags(vp->v_rdev) & D_TYPEMASK;
1120 error = VOP_IOCTL(vp, com, data, fp->f_flag, ucred);
1121 if (error == 0 && com == TIOCSCTTY) {
1122 struct proc *p = curthread->td_proc;
1123 struct session *sess;
1130 sess = p->p_session;
1131 /* Do nothing if reassigning same control tty */
1132 if (sess->s_ttyvp == vp) {
1137 /* Get rid of reference to old control tty */
1138 ovp = sess->s_ttyvp;
1151 * MPALMOSTSAFE - acquires mplock
1154 vn_poll(struct file *fp, int events, struct ucred *cred)
1159 error = VOP_POLL(((struct vnode *)fp->f_data), events, cred);
1165 * Check that the vnode is still valid, and if so
1166 * acquire requested lock.
1170 vn_lock(struct vnode *vp, int flags)
1172 debug_vn_lock(struct vnode *vp, int flags, const char *filename, int line)
1179 vp->filename = filename;
1181 error = debuglockmgr(&vp->v_lock, flags,
1182 "vn_lock", filename, line);
1184 error = lockmgr(&vp->v_lock, flags);
1188 } while (flags & LK_RETRY);
1191 * Because we (had better!) have a ref on the vnode, once it
1192 * goes to VRECLAIMED state it will not be recycled until all
1193 * refs go away. So we can just check the flag.
1195 if (error == 0 && (vp->v_flag & VRECLAIMED)) {
1196 lockmgr(&vp->v_lock, LK_RELEASE);
1203 vn_unlock(struct vnode *vp)
1205 lockmgr(&vp->v_lock, LK_RELEASE);
1209 vn_islocked(struct vnode *vp)
1211 return (lockstatus(&vp->v_lock, curthread));
1215 * MPALMOSTSAFE - acquires mplock
1218 vn_closefile(struct file *fp)
1223 fp->f_ops = &badfileops;
1224 error = vn_close(((struct vnode *)fp->f_data), fp->f_flag);
1230 * MPALMOSTSAFE - acquires mplock
1233 vn_kqfilter(struct file *fp, struct knote *kn)
1238 error = VOP_KQFILTER(((struct vnode *)fp->f_data), kn);