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.40 2006/05/26 00:33:09 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 = {
76 vn_read, vn_write, vn_ioctl, vn_poll, vn_kqfilter,
77 vn_statfile, vn_closefile, nofo_shutdown
80 struct fileops specvnode_fileops = {
83 svn_read, svn_write, vn_ioctl, vn_poll, vn_kqfilter,
84 vn_statfile, vn_closefile, nofo_shutdown
88 * Shortcut the device read/write. This avoids a lot of vnode junk.
89 * Basically the specfs vnops for read and write take the locked vnode,
90 * unlock it (because we can't hold the vnode locked while reading or writing
91 * a device which may block indefinitely), issues the device operation, then
92 * relock the vnode before returning, plus other junk. This bypasses all
93 * of that and just does the device operation.
96 vn_setspecops(struct file *fp)
98 if (vfs_fastdev && fp->f_ops == &vnode_fileops) {
99 fp->f_ops = &specvnode_fileops;
104 * Common code for vnode open operations. Check permissions, and call
105 * the VOP_NOPEN or VOP_NCREATE routine.
107 * The caller is responsible for setting up nd with nlookup_init() and
108 * for cleaning it up with nlookup_done(), whether we return an error
111 * On success nd->nl_open_vp will hold a referenced and, if requested,
112 * locked vnode. A locked vnode is requested via NLC_LOCKVP. If fp
113 * is non-NULL the vnode will be installed in the file pointer.
115 * NOTE: The vnode is referenced just once on return whether or not it
116 * is also installed in the file pointer.
119 vn_open(struct nlookupdata *nd, struct file *fp, int fmode, int cmode)
122 struct ucred *cred = nd->nl_cred;
124 struct vattr *vap = &vat;
125 struct namecache *ncp;
129 * Lookup the path and create or obtain the vnode. After a
130 * successful lookup a locked nd->nl_ncp will be returned.
132 * The result of this section should be a locked vnode.
134 * XXX with only a little work we should be able to avoid locking
135 * the vnode if FWRITE, O_CREAT, and O_TRUNC are *not* set.
137 if (fmode & O_CREAT) {
139 * CONDITIONAL CREATE FILE CASE
141 * Setting NLC_CREATE causes a negative hit to store
142 * the negative hit ncp and not return an error. Then
143 * nc_error or nc_vp may be checked to see if the ncp
144 * represents a negative hit. NLC_CREATE also requires
145 * write permission on the governing directory or EPERM
148 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
149 nd->nl_flags |= NLC_FOLLOW;
150 nd->nl_flags |= NLC_CREATE;
155 * NORMAL OPEN FILE CASE
165 * split case to allow us to re-resolve and retry the ncp in case
169 if (fmode & O_CREAT) {
170 if (ncp->nc_vp == NULL) {
173 vap->va_mode = cmode;
175 vap->va_vaflags |= VA_EXCLUSIVE;
176 error = VOP_NCREATE(ncp, &vp, nd->nl_cred, vap);
180 ASSERT_VOP_LOCKED(vp, "create");
181 /* locked vnode is returned */
183 if (fmode & O_EXCL) {
186 error = cache_vget(ncp, cred,
194 error = cache_vget(ncp, cred, LK_EXCLUSIVE, &vp);
200 * We have a locked vnode and ncp now. Note that the ncp will
201 * be cleaned up by the caller if nd->nl_ncp is left intact.
203 if (vp->v_type == VLNK) {
207 if (vp->v_type == VSOCK) {
211 if ((fmode & O_CREAT) == 0) {
213 if (fmode & (FWRITE | O_TRUNC)) {
214 if (vp->v_type == VDIR) {
218 error = vn_writechk(vp);
221 * Special stale handling, re-resolve the
224 if (error == ESTALE) {
227 cache_setunresolved(ncp);
228 error = cache_resolve(ncp, cred);
239 error = VOP_ACCESS(vp, mode, cred);
242 * Special stale handling, re-resolve the
245 if (error == ESTALE) {
248 cache_setunresolved(ncp);
249 error = cache_resolve(ncp, cred);
257 if (fmode & O_TRUNC) {
258 VOP_UNLOCK(vp, 0); /* XXX */
259 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
262 error = VOP_SETATTR(vp, vap, cred);
268 * Setup the fp so VOP_OPEN can override it. No descriptor has been
269 * associated with the fp yet so we own it clean. f_ncp inherits
273 if (vp->v_type == VDIR) {
274 fp->f_ncp = nd->nl_ncp;
276 cache_unlock(fp->f_ncp);
281 * Get rid of nl_ncp. vn_open does not return it (it returns the
282 * vnode or the file pointer). Note: we can't leave nl_ncp locked
283 * through the VOP_OPEN anyway since the VOP_OPEN may block, e.g.
287 cache_put(nd->nl_ncp);
291 error = VOP_OPEN(vp, fmode, cred, fp);
294 * setting f_ops to &badfileops will prevent the descriptor
295 * code from trying to close and release the vnode, since
296 * the open failed we do not want to call close.
300 fp->f_ops = &badfileops;
307 * Assert that VREG files have been setup for vmio.
309 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
310 ("vn_open: regular file was not VMIO enabled!"));
314 * Return the vnode. XXX needs some cleaning up. The vnode is
315 * only returned in the fp == NULL case.
319 nd->nl_vp_fmode = fmode;
320 if ((nd->nl_flags & NLC_LOCKVP) == 0)
333 * Check for write permissions on the specified vnode.
334 * Prototype text segments cannot be written.
342 * If there's shared text associated with
343 * the vnode, try to free it up once. If
344 * we fail, we can't allow writing.
346 if (vp->v_flag & VTEXT)
355 vn_close(struct vnode *vp, int flags)
359 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) == 0) {
360 error = VOP_CLOSE(vp, flags);
369 sequential_heuristic(struct uio *uio, struct file *fp)
372 * Sequential heuristic - detect sequential operation
374 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
375 uio->uio_offset == fp->f_nextoff) {
376 int tmpseq = fp->f_seqcount;
378 * XXX we assume that the filesystem block size is
379 * the default. Not true, but still gives us a pretty
380 * good indicator of how sequential the read operations
383 tmpseq += (uio->uio_resid + BKVASIZE - 1) / BKVASIZE;
384 if (tmpseq > IO_SEQMAX)
386 fp->f_seqcount = tmpseq;
387 return(fp->f_seqcount << IO_SEQSHIFT);
391 * Not sequential, quick draw-down of seqcount
393 if (fp->f_seqcount > 1)
401 * Package up an I/O request on a vnode into a uio and do it.
404 vn_rdwr(enum uio_rw rw, struct vnode *vp, caddr_t base, int len,
405 off_t offset, enum uio_seg segflg, int ioflg,
406 struct ucred *cred, int *aresid)
412 if ((ioflg & IO_NODELOCKED) == 0)
413 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
414 auio.uio_iov = &aiov;
416 aiov.iov_base = base;
418 auio.uio_resid = len;
419 auio.uio_offset = offset;
420 auio.uio_segflg = segflg;
422 auio.uio_td = curthread;
423 if (rw == UIO_READ) {
424 error = VOP_READ(vp, &auio, ioflg, cred);
426 error = VOP_WRITE(vp, &auio, ioflg, cred);
429 *aresid = auio.uio_resid;
431 if (auio.uio_resid && error == 0)
433 if ((ioflg & IO_NODELOCKED) == 0)
439 * Package up an I/O request on a vnode into a uio and do it. The I/O
440 * request is split up into smaller chunks and we try to avoid saturating
441 * the buffer cache while potentially holding a vnode locked, so we
442 * check bwillwrite() before calling vn_rdwr(). We also call uio_yield()
443 * to give other processes a chance to lock the vnode (either other processes
444 * core'ing the same binary, or unrelated processes scanning the directory).
447 vn_rdwr_inchunks(enum uio_rw rw, struct vnode *vp, caddr_t base, int len,
448 off_t offset, enum uio_seg segflg, int ioflg,
449 struct ucred *cred, int *aresid)
457 * Force `offset' to a multiple of MAXBSIZE except possibly
458 * for the first chunk, so that filesystems only need to
459 * write full blocks except possibly for the first and last
462 chunk = MAXBSIZE - (uoff_t)offset % MAXBSIZE;
466 if (rw != UIO_READ && vp->v_type == VREG)
468 error = vn_rdwr(rw, vp, base, chunk, offset, segflg,
469 ioflg, cred, aresid);
470 len -= chunk; /* aresid calc already includes length */
483 * MPALMOSTSAFE - acquires mplock
486 vn_read(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
492 KASSERT(uio->uio_td == curthread,
493 ("uio_td %p is not td %p", uio->uio_td, curthread));
494 vp = (struct vnode *)fp->f_data;
496 if (fp->f_flag & FNONBLOCK)
498 if (fp->f_flag & O_DIRECT)
500 vn_lock(vp, LK_SHARED | LK_NOPAUSE | LK_RETRY);
501 if ((flags & FOF_OFFSET) == 0)
502 uio->uio_offset = fp->f_offset;
504 ioflag |= sequential_heuristic(uio, fp);
506 error = VOP_READ(vp, uio, ioflag, cred);
507 if ((flags & FOF_OFFSET) == 0)
508 fp->f_offset = uio->uio_offset;
509 fp->f_nextoff = uio->uio_offset;
516 * Device-optimized file table vnode read routine.
518 * This bypasses the VOP table and talks directly to the device. Most
519 * filesystems just route to specfs and can make this optimization.
521 * MPALMOSTSAFE - acquires mplock
524 svn_read(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
532 KASSERT(uio->uio_td == curthread,
533 ("uio_td %p is not td %p", uio->uio_td, curthread));
535 vp = (struct vnode *)fp->f_data;
536 if (vp == NULL || vp->v_type == VBAD) {
541 if ((dev = vp->v_rdev) == NULL) {
547 if (uio->uio_resid == 0) {
551 if ((flags & FOF_OFFSET) == 0)
552 uio->uio_offset = fp->f_offset;
555 if (fp->f_flag & FNONBLOCK)
557 if (fp->f_flag & O_DIRECT)
559 ioflag |= sequential_heuristic(uio, fp);
561 error = dev_dread(dev, uio, ioflag);
564 if ((flags & FOF_OFFSET) == 0)
565 fp->f_offset = uio->uio_offset;
566 fp->f_nextoff = uio->uio_offset;
573 * MPALMOSTSAFE - acquires mplock
576 vn_write(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
582 KASSERT(uio->uio_td == curthread,
583 ("uio_procp %p is not p %p", uio->uio_td, curthread));
584 vp = (struct vnode *)fp->f_data;
585 if (vp->v_type == VREG)
587 vp = (struct vnode *)fp->f_data; /* XXX needed? */
589 if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
591 if (fp->f_flag & FNONBLOCK)
593 if (fp->f_flag & O_DIRECT)
595 if ((fp->f_flag & O_FSYNC) ||
596 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
598 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
599 if ((flags & FOF_OFFSET) == 0)
600 uio->uio_offset = fp->f_offset;
601 ioflag |= sequential_heuristic(uio, fp);
602 error = VOP_WRITE(vp, uio, ioflag, cred);
603 if ((flags & FOF_OFFSET) == 0)
604 fp->f_offset = uio->uio_offset;
605 fp->f_nextoff = uio->uio_offset;
612 * Device-optimized file table vnode write routine.
614 * This bypasses the VOP table and talks directly to the device. Most
615 * filesystems just route to specfs and can make this optimization.
617 * MPALMOSTSAFE - acquires mplock
620 svn_write(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
628 KASSERT(uio->uio_td == curthread,
629 ("uio_procp %p is not p %p", uio->uio_td, curthread));
631 vp = (struct vnode *)fp->f_data;
632 if (vp == NULL || vp->v_type == VBAD) {
636 if (vp->v_type == VREG)
638 vp = (struct vnode *)fp->f_data; /* XXX needed? */
640 if ((dev = vp->v_rdev) == NULL) {
646 if ((flags & FOF_OFFSET) == 0)
647 uio->uio_offset = fp->f_offset;
650 if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
652 if (fp->f_flag & FNONBLOCK)
654 if (fp->f_flag & O_DIRECT)
656 if ((fp->f_flag & O_FSYNC) ||
657 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
659 ioflag |= sequential_heuristic(uio, fp);
661 error = dev_dwrite(dev, uio, ioflag);
664 if ((flags & FOF_OFFSET) == 0)
665 fp->f_offset = uio->uio_offset;
666 fp->f_nextoff = uio->uio_offset;
673 * MPALMOSTSAFE - acquires mplock
676 vn_statfile(struct file *fp, struct stat *sb, struct ucred *cred)
682 vp = (struct vnode *)fp->f_data;
683 error = vn_stat(vp, sb, cred);
689 vn_stat(struct vnode *vp, struct stat *sb, struct ucred *cred)
698 error = VOP_GETATTR(vp, vap);
703 * Zero the spare stat fields
709 * Copy from vattr table
711 if (vap->va_fsid != VNOVAL)
712 sb->st_dev = vap->va_fsid;
714 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
715 sb->st_ino = vap->va_fileid;
717 switch (vap->va_type) {
732 /* This is a cosmetic change, symlinks do not have a mode. */
733 if (vp->v_mount->mnt_flag & MNT_NOSYMFOLLOW)
734 sb->st_mode &= ~ACCESSPERMS; /* 0000 */
736 sb->st_mode |= ACCESSPERMS; /* 0777 */
748 sb->st_nlink = vap->va_nlink;
749 sb->st_uid = vap->va_uid;
750 sb->st_gid = vap->va_gid;
751 sb->st_rdev = vap->va_rdev;
752 sb->st_size = vap->va_size;
753 sb->st_atimespec = vap->va_atime;
754 sb->st_mtimespec = vap->va_mtime;
755 sb->st_ctimespec = vap->va_ctime;
758 * A VCHR and VBLK device may track the last access and last modified
759 * time independantly of the filesystem. This is particularly true
760 * because device read and write calls may bypass the filesystem.
762 if (vp->v_type == VCHR || vp->v_type == VBLK) {
763 if ((dev = vp->v_rdev) != NULL) {
764 if (dev->si_lastread) {
765 sb->st_atimespec.tv_sec = dev->si_lastread;
766 sb->st_atimespec.tv_nsec = 0;
768 if (dev->si_lastwrite) {
769 sb->st_atimespec.tv_sec = dev->si_lastwrite;
770 sb->st_atimespec.tv_nsec = 0;
776 * According to www.opengroup.org, the meaning of st_blksize is
777 * "a filesystem-specific preferred I/O block size for this
778 * object. In some filesystem types, this may vary from file
780 * Default to PAGE_SIZE after much discussion.
783 if (vap->va_type == VREG) {
784 sb->st_blksize = vap->va_blocksize;
785 } else if (vn_isdisk(vp, NULL)) {
787 * XXX this is broken. If the device is not yet open (aka
788 * stat() call, aka v_rdev == NULL), how are we supposed
789 * to get a valid block size out of it?
793 if ((dev = vp->v_rdev) == NULL)
794 dev = udev2dev(vp->v_udev, vp->v_type == VBLK);
795 sb->st_blksize = dev->si_bsize_best;
796 if (sb->st_blksize < dev->si_bsize_phys)
797 sb->st_blksize = dev->si_bsize_phys;
798 if (sb->st_blksize < BLKDEV_IOSIZE)
799 sb->st_blksize = BLKDEV_IOSIZE;
801 sb->st_blksize = PAGE_SIZE;
804 sb->st_flags = vap->va_flags;
805 if (suser_cred(cred, 0))
808 sb->st_gen = vap->va_gen;
810 #if (S_BLKSIZE == 512)
811 /* Optimize this case */
812 sb->st_blocks = vap->va_bytes >> 9;
814 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
816 sb->st_fsmid = vap->va_fsmid;
821 * MPALMOSTSAFE - acquires mplock
824 vn_ioctl(struct file *fp, u_long com, caddr_t data, struct ucred *ucred)
826 struct vnode *vp = ((struct vnode *)fp->f_data);
833 switch (vp->v_type) {
836 if (com == FIONREAD) {
837 if ((error = VOP_GETATTR(vp, &vattr)) != 0)
839 *(int *)data = vattr.va_size - fp->f_offset;
843 if (com == FIONBIO || com == FIOASYNC) { /* XXX */
855 if (com == FIODTYPE) {
856 if (vp->v_type != VCHR && vp->v_type != VBLK) {
860 *(int *)data = dev_dflags(vp->v_rdev) & D_TYPEMASK;
864 error = VOP_IOCTL(vp, com, data, fp->f_flag, ucred);
865 if (error == 0 && com == TIOCSCTTY) {
866 struct proc *p = curthread->td_proc;
867 struct session *sess;
875 /* Do nothing if reassigning same control tty */
876 if (sess->s_ttyvp == vp) {
881 /* Get rid of reference to old control tty */
895 * MPALMOSTSAFE - acquires mplock
898 vn_poll(struct file *fp, int events, struct ucred *cred)
903 error = VOP_POLL(((struct vnode *)fp->f_data), events, cred);
909 * Check that the vnode is still valid, and if so
910 * acquire requested lock.
914 vn_lock(struct vnode *vp, int flags)
916 debug_vn_lock(struct vnode *vp, int flags, const char *filename, int line)
923 vp->filename = filename;
926 error = VOP_LOCK(vp, flags | LK_NOPAUSE);
929 } while (flags & LK_RETRY);
932 * Because we (had better!) have a ref on the vnode, once it
933 * goes to VRECLAIMED state it will not be recycled until all
934 * refs go away. So we can just check the flag.
936 if (error == 0 && (vp->v_flag & VRECLAIMED)) {
944 * MPALMOSTSAFE - acquires mplock
947 vn_closefile(struct file *fp)
952 fp->f_ops = &badfileops;
953 error = vn_close(((struct vnode *)fp->f_data), fp->f_flag);
959 * MPALMOSTSAFE - acquires mplock
962 vn_kqfilter(struct file *fp, struct knote *kn)
967 error = VOP_KQFILTER(((struct vnode *)fp->f_data), kn);