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.22 2004/06/15 00:30:53 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/namei.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, struct thread *td);
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, struct thread *td);
63 static int vn_poll (struct file *fp, int events, struct ucred *cred,
65 static int vn_kqfilter (struct file *fp, struct knote *kn);
66 static int vn_statfile (struct file *fp, struct stat *sb, struct thread *td);
67 static int vn_write (struct file *fp, struct uio *uio,
68 struct ucred *cred, int flags, struct thread *td);
70 struct fileops vnops = {
73 vn_read, vn_write, vn_ioctl, vn_poll, vn_kqfilter,
74 vn_statfile, vn_closefile
78 * Common code for vnode open operations.
79 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
81 * Note that this does NOT free nameidata for the successful case,
82 * due to the NDINIT being done elsewhere.
85 vn_open(ndp, fmode, cmode)
86 struct nameidata *ndp;
90 struct thread *td = ndp->ni_cnd.cn_td;
91 struct ucred *cred = ndp->ni_cnd.cn_cred;
93 struct vattr *vap = &vat;
96 KKASSERT(cred == td->td_proc->p_ucred);
98 if (fmode & O_CREAT) {
99 ndp->ni_cnd.cn_nameiop = NAMEI_CREATE;
100 ndp->ni_cnd.cn_flags = CNP_LOCKPARENT | CNP_LOCKLEAF;
101 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
102 ndp->ni_cnd.cn_flags |= CNP_FOLLOW;
107 if (ndp->ni_vp == NULL) {
110 vap->va_mode = cmode;
112 vap->va_vaflags |= VA_EXCLUSIVE;
113 VOP_LEASE(ndp->ni_dvp, td, cred, LEASE_WRITE);
114 error = VOP_CREATE(ndp->ni_dvp, NCPNULL, &ndp->ni_vp,
117 NDFREE(ndp, NDF_ONLY_PNBUF);
122 ASSERT_VOP_UNLOCKED(ndp->ni_dvp, "create");
123 ASSERT_VOP_LOCKED(ndp->ni_vp, "create");
127 if (ndp->ni_dvp == ndp->ni_vp)
133 if (fmode & O_EXCL) {
140 ndp->ni_cnd.cn_nameiop = NAMEI_LOOKUP;
141 ndp->ni_cnd.cn_flags = CNP_LOCKLEAF |
142 ((fmode & O_NOFOLLOW) ? 0 : CNP_FOLLOW);
148 if (vp->v_type == VLNK) {
152 if (vp->v_type == VSOCK) {
156 if ((fmode & O_CREAT) == 0) {
158 if (fmode & (FWRITE | O_TRUNC)) {
159 if (vp->v_type == VDIR) {
163 error = vn_writechk(vp);
171 error = VOP_ACCESS(vp, mode, cred, td);
176 if (fmode & O_TRUNC) {
177 VOP_UNLOCK(vp, NULL, 0, td); /* XXX */
178 VOP_LEASE(vp, td, cred, LEASE_WRITE);
179 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */
182 error = VOP_SETATTR(vp, vap, cred, td);
186 error = VOP_OPEN(vp, fmode, cred, td);
190 * Make sure that a VM object is created for VMIO support.
192 if (vn_canvmio(vp) == TRUE) {
193 if ((error = vfs_object_create(vp, td)) != 0)
201 NDFREE(ndp, NDF_ONLY_PNBUF);
207 * Check for write permissions on the specified vnode.
208 * Prototype text segments cannot be written.
216 * If there's shared text associated with
217 * the vnode, try to free it up once. If
218 * we fail, we can't allow writing.
220 if (vp->v_flag & VTEXT)
229 vn_close(struct vnode *vp, int flags, struct thread *td)
235 error = VOP_CLOSE(vp, flags, td);
242 sequential_heuristic(struct uio *uio, struct file *fp)
245 * Sequential heuristic - detect sequential operation
247 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
248 uio->uio_offset == fp->f_nextoff) {
249 int tmpseq = fp->f_seqcount;
251 * XXX we assume that the filesystem block size is
252 * the default. Not true, but still gives us a pretty
253 * good indicator of how sequential the read operations
256 tmpseq += (uio->uio_resid + BKVASIZE - 1) / BKVASIZE;
257 if (tmpseq > IO_SEQMAX)
259 fp->f_seqcount = tmpseq;
260 return(fp->f_seqcount << IO_SEQSHIFT);
264 * Not sequential, quick draw-down of seqcount
266 if (fp->f_seqcount > 1)
274 * Package up an I/O request on a vnode into a uio and do it.
276 * We are going to assume the caller has done the appropriate
277 * VOP_LEASE() call before calling vn_rdwr()
280 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, td)
296 if ((ioflg & IO_NODELOCKED) == 0)
297 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
298 auio.uio_iov = &aiov;
300 aiov.iov_base = base;
302 auio.uio_resid = len;
303 auio.uio_offset = offset;
304 auio.uio_segflg = segflg;
307 if (rw == UIO_READ) {
308 error = VOP_READ(vp, &auio, ioflg, cred);
310 error = VOP_WRITE(vp, &auio, ioflg, cred);
313 *aresid = auio.uio_resid;
315 if (auio.uio_resid && error == 0)
317 if ((ioflg & IO_NODELOCKED) == 0)
318 VOP_UNLOCK(vp, NULL, 0, td);
323 * Package up an I/O request on a vnode into a uio and do it. The I/O
324 * request is split up into smaller chunks and we try to avoid saturating
325 * the buffer cache while potentially holding a vnode locked, so we
326 * check bwillwrite() before calling vn_rdwr(). We also call uio_yield()
327 * to give other processes a chance to lock the vnode (either other processes
328 * core'ing the same binary, or unrelated processes scanning the directory).
331 vn_rdwr_inchunks(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, td)
349 * Force `offset' to a multiple of MAXBSIZE except possibly
350 * for the first chunk, so that filesystems only need to
351 * write full blocks except possibly for the first and last
354 chunk = MAXBSIZE - (uoff_t)offset % MAXBSIZE;
358 if (rw != UIO_READ && vp->v_type == VREG)
360 error = vn_rdwr(rw, vp, base, chunk, offset, segflg,
361 ioflg, cred, aresid, td);
362 len -= chunk; /* aresid calc already includes length */
375 * File table vnode read routine.
378 vn_read(fp, uio, cred, flags, td)
388 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", uio->uio_td, td));
389 vp = (struct vnode *)fp->f_data;
391 if (fp->f_flag & FNONBLOCK)
393 if (fp->f_flag & O_DIRECT)
395 VOP_LEASE(vp, td, cred, LEASE_READ);
396 vn_lock(vp, NULL, LK_SHARED | LK_NOPAUSE | LK_RETRY, td);
397 if ((flags & FOF_OFFSET) == 0)
398 uio->uio_offset = fp->f_offset;
400 ioflag |= sequential_heuristic(uio, fp);
402 error = VOP_READ(vp, uio, ioflag, cred);
403 if ((flags & FOF_OFFSET) == 0)
404 fp->f_offset = uio->uio_offset;
405 fp->f_nextoff = uio->uio_offset;
406 VOP_UNLOCK(vp, NULL, 0, td);
411 * File table vnode write routine.
414 vn_write(fp, uio, cred, flags, td)
424 KASSERT(uio->uio_td == td, ("uio_procp %p is not p %p",
426 vp = (struct vnode *)fp->f_data;
427 if (vp->v_type == VREG)
429 vp = (struct vnode *)fp->f_data; /* XXX needed? */
431 if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
433 if (fp->f_flag & FNONBLOCK)
435 if (fp->f_flag & O_DIRECT)
437 if ((fp->f_flag & O_FSYNC) ||
438 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
440 VOP_LEASE(vp, td, cred, LEASE_WRITE);
441 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
442 if ((flags & FOF_OFFSET) == 0)
443 uio->uio_offset = fp->f_offset;
444 ioflag |= sequential_heuristic(uio, fp);
445 error = VOP_WRITE(vp, uio, ioflag, cred);
446 if ((flags & FOF_OFFSET) == 0)
447 fp->f_offset = uio->uio_offset;
448 fp->f_nextoff = uio->uio_offset;
449 VOP_UNLOCK(vp, NULL, 0, td);
454 * File table vnode stat routine.
457 vn_statfile(struct file *fp, struct stat *sb, struct thread *td)
459 struct vnode *vp = (struct vnode *)fp->f_data;
461 return vn_stat(vp, sb, td);
465 vn_stat(struct vnode *vp, struct stat *sb, struct thread *td)
473 error = VOP_GETATTR(vp, vap, td);
478 * Zero the spare stat fields
481 sb->st_qspare[0] = 0;
482 sb->st_qspare[1] = 0;
485 * Copy from vattr table
487 if (vap->va_fsid != VNOVAL)
488 sb->st_dev = vap->va_fsid;
490 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
491 sb->st_ino = vap->va_fileid;
493 switch (vap->va_type) {
508 /* This is a cosmetic change, symlinks do not have a mode. */
509 if (vp->v_mount->mnt_flag & MNT_NOSYMFOLLOW)
510 sb->st_mode &= ~ACCESSPERMS; /* 0000 */
512 sb->st_mode |= ACCESSPERMS; /* 0777 */
524 sb->st_nlink = vap->va_nlink;
525 sb->st_uid = vap->va_uid;
526 sb->st_gid = vap->va_gid;
527 sb->st_rdev = vap->va_rdev;
528 sb->st_size = vap->va_size;
529 sb->st_atimespec = vap->va_atime;
530 sb->st_mtimespec = vap->va_mtime;
531 sb->st_ctimespec = vap->va_ctime;
534 * According to www.opengroup.org, the meaning of st_blksize is
535 * "a filesystem-specific preferred I/O block size for this
536 * object. In some filesystem types, this may vary from file
538 * Default to PAGE_SIZE after much discussion.
541 if (vap->va_type == VREG) {
542 sb->st_blksize = vap->va_blocksize;
543 } else if (vn_isdisk(vp, NULL)) {
545 * XXX this is broken. If the device is not yet open (aka
546 * stat() call, aka v_rdev == NULL), how are we supposed
547 * to get a valid block size out of it?
551 if ((dev = vp->v_rdev) == NULL)
552 dev = udev2dev(vp->v_udev, vp->v_type == VBLK);
553 sb->st_blksize = dev->si_bsize_best;
554 if (sb->st_blksize < dev->si_bsize_phys)
555 sb->st_blksize = dev->si_bsize_phys;
556 if (sb->st_blksize < BLKDEV_IOSIZE)
557 sb->st_blksize = BLKDEV_IOSIZE;
559 sb->st_blksize = PAGE_SIZE;
562 sb->st_flags = vap->va_flags;
566 sb->st_gen = vap->va_gen;
568 #if (S_BLKSIZE == 512)
569 /* Optimize this case */
570 sb->st_blocks = vap->va_bytes >> 9;
572 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
578 * File table vnode ioctl routine.
581 vn_ioctl(struct file *fp, u_long com, caddr_t data, struct thread *td)
583 struct vnode *vp = ((struct vnode *)fp->f_data);
589 KKASSERT(td->td_proc != NULL);
590 ucred = td->td_proc->p_ucred;
592 switch (vp->v_type) {
595 if (com == FIONREAD) {
596 error = VOP_GETATTR(vp, &vattr, td);
599 *(int *)data = vattr.va_size - fp->f_offset;
602 if (com == FIONBIO || com == FIOASYNC) /* XXX */
603 return (0); /* XXX */
612 if (com == FIODTYPE) {
613 if (vp->v_type != VCHR && vp->v_type != VBLK)
615 *(int *)data = dev_dflags(vp->v_rdev) & D_TYPEMASK;
618 error = VOP_IOCTL(vp, com, data, fp->f_flag, ucred, td);
619 if (error == 0 && com == TIOCSCTTY) {
620 struct session *sess = td->td_proc->p_session;
622 /* Do nothing if reassigning same control tty */
623 if (sess->s_ttyvp == vp)
626 /* Get rid of reference to old control tty */
638 * File table vnode poll routine.
641 vn_poll(struct file *fp, int events, struct ucred *cred, struct thread *td)
643 return (VOP_POLL(((struct vnode *)fp->f_data), events, cred, td));
647 * Check that the vnode is still valid, and if so
648 * acquire requested lock.
652 vn_lock(struct vnode *vp, lwkt_tokref_t vlock, int flags, struct thread *td)
654 debug_vn_lock(struct vnode *vp, lwkt_tokref_t vlock, int flags,
655 struct thread *td, const char *filename, int line)
662 if ((flags & LK_INTERLOCK) == 0) {
663 lwkt_gettoken(&vvlock, vp->v_interlock);
666 if ((vp->v_flag & VXLOCK) && vp->v_vxthread != curthread) {
667 vp->v_flag |= VXWANT;
668 lwkt_reltoken(vlock);
669 tsleep((caddr_t)vp, 0, "vn_lock", 0);
673 /* this can now occur in normal operation */
674 if (vp->v_vxthread != NULL)
675 log(LOG_INFO, "VXLOCK interlock avoided in vn_lock\n");
678 vp->filename = filename;
681 error = VOP_LOCK(vp, vlock,
682 flags | LK_NOPAUSE | LK_INTERLOCK, td);
686 flags &= ~LK_INTERLOCK;
687 } while (flags & LK_RETRY);
692 * File table vnode close routine.
695 vn_closefile(struct file *fp, struct thread *td)
699 fp->f_ops = &badfileops;
700 err = vn_close(((struct vnode *)fp->f_data), fp->f_flag, td);
705 vn_kqfilter(struct file *fp, struct knote *kn)
708 return (VOP_KQFILTER(((struct vnode *)fp->f_data), kn));