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
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/fcntl.h>
48 #include <sys/mount.h>
49 #include <sys/namei.h>
50 #include <sys/vnode.h>
53 #include <sys/filio.h>
54 #include <sys/ttycom.h>
57 #include <ufs/ufs/quota.h>
58 #include <ufs/ufs/inode.h>
60 static int vn_closefile __P((struct file *fp, struct proc *p));
61 static int vn_ioctl __P((struct file *fp, u_long com, caddr_t data,
63 static int vn_read __P((struct file *fp, struct uio *uio,
64 struct ucred *cred, int flags, struct proc *p));
65 static int vn_poll __P((struct file *fp, int events, struct ucred *cred,
67 static int vn_statfile __P((struct file *fp, struct stat *sb, struct proc *p));
68 static int vn_write __P((struct file *fp, struct uio *uio,
69 struct ucred *cred, int flags, struct proc *p));
71 struct fileops vnops =
72 { vn_read, vn_write, vn_ioctl, vn_poll, vn_statfile, vn_closefile };
74 static int filt_nullattach(struct knote *kn);
75 static int filt_vnattach(struct knote *kn);
76 static void filt_vndetach(struct knote *kn);
77 static int filt_vnode(struct knote *kn, long hint);
78 static int filt_vnread(struct knote *kn, long hint);
80 struct filterops vn_filtops =
81 { 1, filt_vnattach, filt_vndetach, filt_vnode };
85 * filt_vnread is ufs-specific, so the attach routine should really
86 * switch out to different filterops based on the vn filetype
88 struct filterops vn_rwfiltops[] = {
89 { 1, filt_vnattach, filt_vndetach, filt_vnread },
90 { 1, filt_nullattach, NULL, NULL },
94 * Common code for vnode open operations.
95 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
97 * Note that this does NOT free nameidata for the successful case,
98 * due to the NDINIT being done elsewhere.
101 vn_open(ndp, fmode, cmode)
102 register struct nameidata *ndp;
105 register struct vnode *vp;
106 register struct proc *p = ndp->ni_cnd.cn_proc;
107 register struct ucred *cred = p->p_ucred;
109 struct vattr *vap = &vat;
112 if (fmode & O_CREAT) {
113 ndp->ni_cnd.cn_nameiop = CREATE;
114 ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
115 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
116 ndp->ni_cnd.cn_flags |= FOLLOW;
121 if (ndp->ni_vp == NULL) {
124 vap->va_mode = cmode;
126 vap->va_vaflags |= VA_EXCLUSIVE;
127 VOP_LEASE(ndp->ni_dvp, p, cred, LEASE_WRITE);
128 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
131 NDFREE(ndp, NDF_ONLY_PNBUF);
136 ASSERT_VOP_UNLOCKED(ndp->ni_dvp, "create");
137 ASSERT_VOP_LOCKED(ndp->ni_vp, "create");
141 if (ndp->ni_dvp == ndp->ni_vp)
147 if (fmode & O_EXCL) {
154 ndp->ni_cnd.cn_nameiop = LOOKUP;
155 ndp->ni_cnd.cn_flags =
156 ((fmode & O_NOFOLLOW) ? NOFOLLOW : FOLLOW) | LOCKLEAF;
162 if (vp->v_type == VLNK) {
166 if (vp->v_type == VSOCK) {
170 if ((fmode & O_CREAT) == 0) {
172 if (fmode & (FWRITE | O_TRUNC)) {
173 if (vp->v_type == VDIR) {
177 error = vn_writechk(vp);
185 error = VOP_ACCESS(vp, mode, cred, p);
190 if (fmode & O_TRUNC) {
191 VOP_UNLOCK(vp, 0, p); /* XXX */
192 VOP_LEASE(vp, p, cred, LEASE_WRITE);
193 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); /* XXX */
196 error = VOP_SETATTR(vp, vap, cred, p);
200 error = VOP_OPEN(vp, fmode, cred, p);
204 * Make sure that a VM object is created for VMIO support.
206 if (vn_canvmio(vp) == TRUE) {
207 if ((error = vfs_object_create(vp, p, cred)) != 0)
215 NDFREE(ndp, NDF_ONLY_PNBUF);
221 * Check for write permissions on the specified vnode.
222 * Prototype text segments cannot be written.
226 register struct vnode *vp;
230 * If there's shared text associated with
231 * the vnode, try to free it up once. If
232 * we fail, we can't allow writing.
234 if (vp->v_flag & VTEXT)
243 vn_close(vp, flags, cred, p)
244 register struct vnode *vp;
253 error = VOP_CLOSE(vp, flags, cred, p);
260 sequential_heuristic(struct uio *uio, struct file *fp)
263 * Sequential heuristic - detect sequential operation
265 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
266 uio->uio_offset == fp->f_nextoff) {
268 * XXX we assume that the filesystem block size is
269 * the default. Not true, but still gives us a pretty
270 * good indicator of how sequential the read operations
273 fp->f_seqcount += (uio->uio_resid + BKVASIZE - 1) / BKVASIZE;
274 if (fp->f_seqcount >= 127)
275 fp->f_seqcount = 127;
276 return(fp->f_seqcount << 16);
280 * Not sequential, quick draw-down of seqcount
282 if (fp->f_seqcount > 1)
290 * Package up an I/O request on a vnode into a uio and do it.
293 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p)
309 if ((ioflg & IO_NODELOCKED) == 0)
310 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
311 auio.uio_iov = &aiov;
313 aiov.iov_base = base;
315 auio.uio_resid = len;
316 auio.uio_offset = offset;
317 auio.uio_segflg = segflg;
320 if (rw == UIO_READ) {
321 error = VOP_READ(vp, &auio, ioflg, cred);
323 error = VOP_WRITE(vp, &auio, ioflg, cred);
326 *aresid = auio.uio_resid;
328 if (auio.uio_resid && error == 0)
330 if ((ioflg & IO_NODELOCKED) == 0)
331 VOP_UNLOCK(vp, 0, p);
336 * File table vnode read routine.
339 vn_read(fp, uio, cred, flags, p)
349 KASSERT(uio->uio_procp == p, ("uio_procp %p is not p %p",
351 vp = (struct vnode *)fp->f_data;
353 if (fp->f_flag & FNONBLOCK)
355 VOP_LEASE(vp, p, cred, LEASE_READ);
356 vn_lock(vp, LK_SHARED | LK_NOPAUSE | LK_RETRY, p);
357 if ((flags & FOF_OFFSET) == 0)
358 uio->uio_offset = fp->f_offset;
360 ioflag |= sequential_heuristic(uio, fp);
362 error = VOP_READ(vp, uio, ioflag, cred);
363 if ((flags & FOF_OFFSET) == 0)
364 fp->f_offset = uio->uio_offset;
365 fp->f_nextoff = uio->uio_offset;
366 VOP_UNLOCK(vp, 0, p);
371 * File table vnode write routine.
374 vn_write(fp, uio, cred, flags, p)
384 KASSERT(uio->uio_procp == p, ("uio_procp %p is not p %p",
386 vp = (struct vnode *)fp->f_data;
387 if (vp->v_type == VREG)
389 vp = (struct vnode *)fp->f_data; /* XXX needed? */
391 if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
393 if (fp->f_flag & FNONBLOCK)
395 if ((fp->f_flag & O_FSYNC) ||
396 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
398 VOP_LEASE(vp, p, cred, LEASE_WRITE);
399 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
400 if ((flags & FOF_OFFSET) == 0)
401 uio->uio_offset = fp->f_offset;
402 ioflag |= sequential_heuristic(uio, fp);
403 error = VOP_WRITE(vp, uio, ioflag, cred);
404 if ((flags & FOF_OFFSET) == 0)
405 fp->f_offset = uio->uio_offset;
406 fp->f_nextoff = uio->uio_offset;
407 VOP_UNLOCK(vp, 0, p);
412 * File table vnode stat routine.
415 vn_statfile(fp, sb, p)
420 struct vnode *vp = (struct vnode *)fp->f_data;
422 return vn_stat(vp, sb, p);
428 register struct stat *sb;
432 register struct vattr *vap;
437 error = VOP_GETATTR(vp, vap, p->p_ucred, p);
442 * Zero the spare stat fields
445 sb->st_qspare[0] = 0;
446 sb->st_qspare[1] = 0;
449 * Copy from vattr table
451 if (vap->va_fsid != VNOVAL)
452 sb->st_dev = vap->va_fsid;
454 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
455 sb->st_ino = vap->va_fileid;
457 switch (vap->va_type) {
472 /* This is a cosmetic change, symlinks do not have a mode. */
473 if (vp->v_mount->mnt_flag & MNT_NOSYMFOLLOW)
474 sb->st_mode &= ~ACCESSPERMS; /* 0000 */
476 sb->st_mode |= ACCESSPERMS; /* 0777 */
488 sb->st_nlink = vap->va_nlink;
489 sb->st_uid = vap->va_uid;
490 sb->st_gid = vap->va_gid;
491 sb->st_rdev = vap->va_rdev;
492 sb->st_size = vap->va_size;
493 sb->st_atimespec = vap->va_atime;
494 sb->st_mtimespec = vap->va_mtime;
495 sb->st_ctimespec = vap->va_ctime;
498 * According to www.opengroup.org, the meaning of st_blksize is
499 * "a filesystem-specific preferred I/O block size for this
500 * object. In some filesystem types, this may vary from file
502 * Default to zero to catch bogus uses of this field.
505 if (vap->va_type == VREG) {
506 sb->st_blksize = vap->va_blocksize;
507 } else if (vn_isdisk(vp, NULL)) {
508 sb->st_blksize = vp->v_rdev->si_bsize_best;
509 if (sb->st_blksize < vp->v_rdev->si_bsize_phys)
510 sb->st_blksize = vp->v_rdev->si_bsize_phys;
511 if (sb->st_blksize < BLKDEV_IOSIZE)
512 sb->st_blksize = BLKDEV_IOSIZE;
517 sb->st_flags = vap->va_flags;
518 if (suser_xxx(p->p_ucred, 0, 0))
521 sb->st_gen = vap->va_gen;
523 #if (S_BLKSIZE == 512)
524 /* Optimize this case */
525 sb->st_blocks = vap->va_bytes >> 9;
527 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
533 * File table vnode ioctl routine.
536 vn_ioctl(fp, com, data, p)
542 register struct vnode *vp = ((struct vnode *)fp->f_data);
546 switch (vp->v_type) {
550 if (com == FIONREAD) {
551 error = VOP_GETATTR(vp, &vattr, p->p_ucred, p);
554 *(int *)data = vattr.va_size - fp->f_offset;
557 if (com == FIONBIO || com == FIOASYNC) /* XXX */
558 return (0); /* XXX */
568 if (com == FIODTYPE) {
569 if (vp->v_type != VCHR && vp->v_type != VBLK)
571 *(int *)data = devsw(vp->v_rdev)->d_flags & D_TYPEMASK;
574 error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p);
575 if (error == 0 && com == TIOCSCTTY) {
577 /* Do nothing if reassigning same control tty */
578 if (p->p_session->s_ttyvp == vp)
581 /* Get rid of reference to old control tty */
582 if (p->p_session->s_ttyvp)
583 vrele(p->p_session->s_ttyvp);
585 p->p_session->s_ttyvp = vp;
593 * File table vnode poll routine.
596 vn_poll(fp, events, cred, p)
603 return (VOP_POLL(((struct vnode *)fp->f_data), events, cred, p));
607 * Check that the vnode is still valid, and if so
608 * acquire requested lock.
612 vn_lock(vp, flags, p)
614 debug_vn_lock(vp, flags, p, filename, line)
620 const char *filename;
627 if ((flags & LK_INTERLOCK) == 0)
628 simple_lock(&vp->v_interlock);
629 if (vp->v_flag & VXLOCK) {
630 vp->v_flag |= VXWANT;
631 simple_unlock(&vp->v_interlock);
632 tsleep((caddr_t)vp, PINOD, "vn_lock", 0);
636 vp->filename = filename;
640 flags | LK_NOPAUSE | LK_INTERLOCK, p);
644 flags &= ~LK_INTERLOCK;
645 } while (flags & LK_RETRY);
650 * File table vnode close routine.
658 fp->f_ops = &badfileops;
659 return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
664 filt_vnattach(struct knote *kn)
668 if (kn->kn_fp->f_type != DTYPE_VNODE &&
669 kn->kn_fp->f_type != DTYPE_FIFO)
672 vp = (struct vnode *)kn->kn_fp->f_data;
674 simple_lock(&vp->v_pollinfo.vpi_lock);
675 SLIST_INSERT_HEAD(&vp->v_pollinfo.vpi_selinfo.si_note, kn, kn_selnext);
676 simple_unlock(&vp->v_pollinfo.vpi_lock);
682 filt_vndetach(struct knote *kn)
684 struct vnode *vp = (struct vnode *)kn->kn_fp->f_data;
686 simple_lock(&vp->v_pollinfo.vpi_lock);
687 SLIST_REMOVE(&vp->v_pollinfo.vpi_selinfo.si_note,
688 kn, struct knote, kn_selnext);
689 simple_unlock(&vp->v_pollinfo.vpi_lock);
693 filt_vnode(struct knote *kn, long hint)
696 if (kn->kn_sfflags & hint)
697 kn->kn_fflags |= hint;
698 return (kn->kn_fflags != 0);
702 filt_nullattach(struct knote *kn)
709 filt_vnread(struct knote *kn, long hint)
711 struct vnode *vp = (struct vnode *)kn->kn_fp->f_data;
712 struct inode *ip = VTOI(vp);
714 kn->kn_data = ip->i_size - kn->kn_fp->f_offset;
715 return (kn->kn_data != 0);