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 $
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>
52 #include <sys/filio.h>
53 #include <sys/ttycom.h>
55 #include <sys/syslog.h>
57 static int vn_closefile __P((struct file *fp, struct proc *p));
58 static int vn_ioctl __P((struct file *fp, u_long com, caddr_t data,
60 static int vn_read __P((struct file *fp, struct uio *uio,
61 struct ucred *cred, int flags, struct proc *p));
62 static int vn_poll __P((struct file *fp, int events, struct ucred *cred,
64 static int vn_kqfilter __P((struct file *fp, struct knote *kn));
65 static int vn_statfile __P((struct file *fp, struct stat *sb, struct proc *p));
66 static int vn_write __P((struct file *fp, struct uio *uio,
67 struct ucred *cred, int flags, struct proc *p));
69 struct fileops vnops = {
70 vn_read, vn_write, vn_ioctl, vn_poll, vn_kqfilter,
71 vn_statfile, vn_closefile
75 * Common code for vnode open operations.
76 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
78 * Note that this does NOT free nameidata for the successful case,
79 * due to the NDINIT being done elsewhere.
82 vn_open(ndp, fmode, cmode)
83 register struct nameidata *ndp;
86 register struct vnode *vp;
87 register struct proc *p = ndp->ni_cnd.cn_proc;
88 register struct ucred *cred = p->p_ucred;
90 struct vattr *vap = &vat;
93 if (fmode & O_CREAT) {
94 ndp->ni_cnd.cn_nameiop = CREATE;
95 ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
96 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
97 ndp->ni_cnd.cn_flags |= FOLLOW;
102 if (ndp->ni_vp == NULL) {
105 vap->va_mode = cmode;
107 vap->va_vaflags |= VA_EXCLUSIVE;
108 VOP_LEASE(ndp->ni_dvp, p, cred, LEASE_WRITE);
109 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
112 NDFREE(ndp, NDF_ONLY_PNBUF);
117 ASSERT_VOP_UNLOCKED(ndp->ni_dvp, "create");
118 ASSERT_VOP_LOCKED(ndp->ni_vp, "create");
122 if (ndp->ni_dvp == ndp->ni_vp)
128 if (fmode & O_EXCL) {
135 ndp->ni_cnd.cn_nameiop = LOOKUP;
136 ndp->ni_cnd.cn_flags =
137 ((fmode & O_NOFOLLOW) ? NOFOLLOW : FOLLOW) | LOCKLEAF;
143 if (vp->v_type == VLNK) {
147 if (vp->v_type == VSOCK) {
151 if ((fmode & O_CREAT) == 0) {
153 if (fmode & (FWRITE | O_TRUNC)) {
154 if (vp->v_type == VDIR) {
158 error = vn_writechk(vp);
166 error = VOP_ACCESS(vp, mode, cred, p);
171 if (fmode & O_TRUNC) {
172 VOP_UNLOCK(vp, 0, p); /* XXX */
173 VOP_LEASE(vp, p, cred, LEASE_WRITE);
174 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); /* XXX */
177 error = VOP_SETATTR(vp, vap, cred, p);
181 error = VOP_OPEN(vp, fmode, cred, p);
185 * Make sure that a VM object is created for VMIO support.
187 if (vn_canvmio(vp) == TRUE) {
188 if ((error = vfs_object_create(vp, p, cred)) != 0)
196 NDFREE(ndp, NDF_ONLY_PNBUF);
202 * Check for write permissions on the specified vnode.
203 * Prototype text segments cannot be written.
207 register struct vnode *vp;
211 * If there's shared text associated with
212 * the vnode, try to free it up once. If
213 * we fail, we can't allow writing.
215 if (vp->v_flag & VTEXT)
224 vn_close(vp, flags, cred, p)
225 register struct vnode *vp;
234 error = VOP_CLOSE(vp, flags, cred, p);
241 sequential_heuristic(struct uio *uio, struct file *fp)
244 * Sequential heuristic - detect sequential operation
246 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
247 uio->uio_offset == fp->f_nextoff) {
248 int tmpseq = fp->f_seqcount;
250 * XXX we assume that the filesystem block size is
251 * the default. Not true, but still gives us a pretty
252 * good indicator of how sequential the read operations
255 tmpseq += (uio->uio_resid + BKVASIZE - 1) / BKVASIZE;
256 if (tmpseq > IO_SEQMAX)
258 fp->f_seqcount = tmpseq;
259 return(fp->f_seqcount << IO_SEQSHIFT);
263 * Not sequential, quick draw-down of seqcount
265 if (fp->f_seqcount > 1)
273 * Package up an I/O request on a vnode into a uio and do it.
276 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p)
292 if ((ioflg & IO_NODELOCKED) == 0)
293 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
294 auio.uio_iov = &aiov;
296 aiov.iov_base = base;
298 auio.uio_resid = len;
299 auio.uio_offset = offset;
300 auio.uio_segflg = segflg;
303 if (rw == UIO_READ) {
304 error = VOP_READ(vp, &auio, ioflg, cred);
306 error = VOP_WRITE(vp, &auio, ioflg, cred);
309 *aresid = auio.uio_resid;
311 if (auio.uio_resid && error == 0)
313 if ((ioflg & IO_NODELOCKED) == 0)
314 VOP_UNLOCK(vp, 0, p);
319 * Package up an I/O request on a vnode into a uio and do it. The I/O
320 * request is split up into smaller chunks and we try to avoid saturating
321 * the buffer cache while potentially holding a vnode locked, so we
322 * check bwillwrite() before calling vn_rdwr(). We also call uio_yield()
323 * to give other processes a chance to lock the vnode (either other processes
324 * core'ing the same binary, or unrelated processes scanning the directory).
327 vn_rdwr_inchunks(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p)
342 int chunk = (len > MAXBSIZE) ? MAXBSIZE : len;
344 if (rw != UIO_READ && vp->v_type == VREG)
346 error = vn_rdwr(rw, vp, base, chunk, offset, segflg,
347 ioflg, cred, aresid, p);
348 len -= chunk; /* aresid calc already includes length */
361 * File table vnode read routine.
364 vn_read(fp, uio, cred, flags, p)
374 KASSERT(uio->uio_procp == p, ("uio_procp %p is not p %p",
376 vp = (struct vnode *)fp->f_data;
378 if (fp->f_flag & FNONBLOCK)
380 if (fp->f_flag & O_DIRECT)
382 VOP_LEASE(vp, p, cred, LEASE_READ);
383 vn_lock(vp, LK_SHARED | LK_NOPAUSE | LK_RETRY, p);
384 if ((flags & FOF_OFFSET) == 0)
385 uio->uio_offset = fp->f_offset;
387 ioflag |= sequential_heuristic(uio, fp);
389 error = VOP_READ(vp, uio, ioflag, cred);
390 if ((flags & FOF_OFFSET) == 0)
391 fp->f_offset = uio->uio_offset;
392 fp->f_nextoff = uio->uio_offset;
393 VOP_UNLOCK(vp, 0, p);
398 * File table vnode write routine.
401 vn_write(fp, uio, cred, flags, p)
411 KASSERT(uio->uio_procp == p, ("uio_procp %p is not p %p",
413 vp = (struct vnode *)fp->f_data;
414 if (vp->v_type == VREG)
416 vp = (struct vnode *)fp->f_data; /* XXX needed? */
418 if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
420 if (fp->f_flag & FNONBLOCK)
422 if (fp->f_flag & O_DIRECT)
424 if ((fp->f_flag & O_FSYNC) ||
425 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
427 VOP_LEASE(vp, p, cred, LEASE_WRITE);
428 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
429 if ((flags & FOF_OFFSET) == 0)
430 uio->uio_offset = fp->f_offset;
431 ioflag |= sequential_heuristic(uio, fp);
432 error = VOP_WRITE(vp, uio, ioflag, cred);
433 if ((flags & FOF_OFFSET) == 0)
434 fp->f_offset = uio->uio_offset;
435 fp->f_nextoff = uio->uio_offset;
436 VOP_UNLOCK(vp, 0, p);
441 * File table vnode stat routine.
444 vn_statfile(fp, sb, p)
449 struct vnode *vp = (struct vnode *)fp->f_data;
451 return vn_stat(vp, sb, p);
457 register struct stat *sb;
461 register struct vattr *vap;
466 error = VOP_GETATTR(vp, vap, p->p_ucred, p);
471 * Zero the spare stat fields
474 sb->st_qspare[0] = 0;
475 sb->st_qspare[1] = 0;
478 * Copy from vattr table
480 if (vap->va_fsid != VNOVAL)
481 sb->st_dev = vap->va_fsid;
483 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
484 sb->st_ino = vap->va_fileid;
486 switch (vap->va_type) {
501 /* This is a cosmetic change, symlinks do not have a mode. */
502 if (vp->v_mount->mnt_flag & MNT_NOSYMFOLLOW)
503 sb->st_mode &= ~ACCESSPERMS; /* 0000 */
505 sb->st_mode |= ACCESSPERMS; /* 0777 */
517 sb->st_nlink = vap->va_nlink;
518 sb->st_uid = vap->va_uid;
519 sb->st_gid = vap->va_gid;
520 sb->st_rdev = vap->va_rdev;
521 sb->st_size = vap->va_size;
522 sb->st_atimespec = vap->va_atime;
523 sb->st_mtimespec = vap->va_mtime;
524 sb->st_ctimespec = vap->va_ctime;
527 * According to www.opengroup.org, the meaning of st_blksize is
528 * "a filesystem-specific preferred I/O block size for this
529 * object. In some filesystem types, this may vary from file
531 * Default to PAGE_SIZE after much discussion.
534 if (vap->va_type == VREG) {
535 sb->st_blksize = vap->va_blocksize;
536 } else if (vn_isdisk(vp, NULL)) {
537 sb->st_blksize = vp->v_rdev->si_bsize_best;
538 if (sb->st_blksize < vp->v_rdev->si_bsize_phys)
539 sb->st_blksize = vp->v_rdev->si_bsize_phys;
540 if (sb->st_blksize < BLKDEV_IOSIZE)
541 sb->st_blksize = BLKDEV_IOSIZE;
543 sb->st_blksize = PAGE_SIZE;
546 sb->st_flags = vap->va_flags;
547 if (suser_xxx(p->p_ucred, 0, 0))
550 sb->st_gen = vap->va_gen;
552 #if (S_BLKSIZE == 512)
553 /* Optimize this case */
554 sb->st_blocks = vap->va_bytes >> 9;
556 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
562 * File table vnode ioctl routine.
565 vn_ioctl(fp, com, data, p)
571 register struct vnode *vp = ((struct vnode *)fp->f_data);
575 switch (vp->v_type) {
579 if (com == FIONREAD) {
580 error = VOP_GETATTR(vp, &vattr, p->p_ucred, p);
583 *(int *)data = vattr.va_size - fp->f_offset;
586 if (com == FIONBIO || com == FIOASYNC) /* XXX */
587 return (0); /* XXX */
597 if (com == FIODTYPE) {
598 if (vp->v_type != VCHR && vp->v_type != VBLK)
600 *(int *)data = devsw(vp->v_rdev)->d_flags & D_TYPEMASK;
603 error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p);
604 if (error == 0 && com == TIOCSCTTY) {
606 /* Do nothing if reassigning same control tty */
607 if (p->p_session->s_ttyvp == vp)
610 /* Get rid of reference to old control tty */
611 if (p->p_session->s_ttyvp)
612 vrele(p->p_session->s_ttyvp);
614 p->p_session->s_ttyvp = vp;
622 * File table vnode poll routine.
625 vn_poll(fp, events, cred, p)
632 return (VOP_POLL(((struct vnode *)fp->f_data), events, cred, p));
636 * Check that the vnode is still valid, and if so
637 * acquire requested lock.
641 vn_lock(vp, flags, p)
643 debug_vn_lock(vp, flags, p, filename, line)
649 const char *filename;
656 if ((flags & LK_INTERLOCK) == 0)
657 simple_lock(&vp->v_interlock);
658 if ((vp->v_flag & VXLOCK) && vp->v_vxproc != curproc) {
659 vp->v_flag |= VXWANT;
660 simple_unlock(&vp->v_interlock);
661 tsleep((caddr_t)vp, PINOD, "vn_lock", 0);
665 /* this can now occur in normal operation */
666 if (vp->v_vxproc != NULL)
667 log(LOG_INFO, "VXLOCK interlock avoided in vn_lock\n");
670 vp->filename = filename;
674 flags | LK_NOPAUSE | LK_INTERLOCK, p);
678 flags &= ~LK_INTERLOCK;
679 } while (flags & LK_RETRY);
684 * File table vnode close routine.
692 fp->f_ops = &badfileops;
693 return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
698 vn_kqfilter(struct file *fp, struct knote *kn)
701 return (VOP_KQFILTER(((struct vnode *)fp->f_data), kn));