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.9 2003/07/22 17:03:33 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 __P((struct file *fp, struct thread *td));
59 static int vn_ioctl __P((struct file *fp, u_long com, caddr_t data,
61 static int vn_read __P((struct file *fp, struct uio *uio,
62 struct ucred *cred, int flags, struct thread *td));
63 static int vn_poll __P((struct file *fp, int events, struct ucred *cred,
65 static int vn_kqfilter __P((struct file *fp, struct knote *kn));
66 static int vn_statfile __P((struct file *fp, struct stat *sb, struct thread *td));
67 static int vn_write __P((struct file *fp, struct uio *uio,
68 struct ucred *cred, int flags, struct thread *td));
70 struct fileops vnops = {
71 vn_read, vn_write, vn_ioctl, vn_poll, vn_kqfilter,
72 vn_statfile, vn_closefile
76 * Common code for vnode open operations.
77 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
79 * Note that this does NOT free nameidata for the successful case,
80 * due to the NDINIT being done elsewhere.
83 vn_open(ndp, fmode, cmode)
84 register struct nameidata *ndp;
87 register struct vnode *vp;
88 register struct thread *td = ndp->ni_cnd.cn_td;
89 register struct ucred *cred = ndp->ni_cnd.cn_cred;
91 struct vattr *vap = &vat;
94 KKASSERT(cred == td->td_proc->p_ucred);
96 if (fmode & O_CREAT) {
97 ndp->ni_cnd.cn_nameiop = CREATE;
98 ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
99 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
100 ndp->ni_cnd.cn_flags |= FOLLOW;
105 if (ndp->ni_vp == NULL) {
108 vap->va_mode = cmode;
110 vap->va_vaflags |= VA_EXCLUSIVE;
111 VOP_LEASE(ndp->ni_dvp, td, cred, LEASE_WRITE);
112 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
115 NDFREE(ndp, NDF_ONLY_PNBUF);
120 ASSERT_VOP_UNLOCKED(ndp->ni_dvp, "create");
121 ASSERT_VOP_LOCKED(ndp->ni_vp, "create");
125 if (ndp->ni_dvp == ndp->ni_vp)
131 if (fmode & O_EXCL) {
138 ndp->ni_cnd.cn_nameiop = LOOKUP;
139 ndp->ni_cnd.cn_flags =
140 ((fmode & O_NOFOLLOW) ? NOFOLLOW : FOLLOW) | LOCKLEAF;
146 if (vp->v_type == VLNK) {
150 if (vp->v_type == VSOCK) {
154 if ((fmode & O_CREAT) == 0) {
156 if (fmode & (FWRITE | O_TRUNC)) {
157 if (vp->v_type == VDIR) {
161 error = vn_writechk(vp);
169 error = VOP_ACCESS(vp, mode, cred, td);
174 if (fmode & O_TRUNC) {
175 VOP_UNLOCK(vp, 0, td); /* XXX */
176 VOP_LEASE(vp, td, cred, LEASE_WRITE);
177 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */
180 error = VOP_SETATTR(vp, vap, cred, td);
184 error = VOP_OPEN(vp, fmode, cred, td);
188 * Make sure that a VM object is created for VMIO support.
190 if (vn_canvmio(vp) == TRUE) {
191 if ((error = vfs_object_create(vp, td)) != 0)
199 NDFREE(ndp, NDF_ONLY_PNBUF);
205 * Check for write permissions on the specified vnode.
206 * Prototype text segments cannot be written.
210 register struct vnode *vp;
214 * If there's shared text associated with
215 * the vnode, try to free it up once. If
216 * we fail, we can't allow writing.
218 if (vp->v_flag & VTEXT)
227 vn_close(struct vnode *vp, int flags, struct thread *td)
233 error = VOP_CLOSE(vp, flags, td);
240 sequential_heuristic(struct uio *uio, struct file *fp)
243 * Sequential heuristic - detect sequential operation
245 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
246 uio->uio_offset == fp->f_nextoff) {
247 int tmpseq = fp->f_seqcount;
249 * XXX we assume that the filesystem block size is
250 * the default. Not true, but still gives us a pretty
251 * good indicator of how sequential the read operations
254 tmpseq += (uio->uio_resid + BKVASIZE - 1) / BKVASIZE;
255 if (tmpseq > IO_SEQMAX)
257 fp->f_seqcount = tmpseq;
258 return(fp->f_seqcount << IO_SEQSHIFT);
262 * Not sequential, quick draw-down of seqcount
264 if (fp->f_seqcount > 1)
272 * Package up an I/O request on a vnode into a uio and do it.
275 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, td)
291 if ((ioflg & IO_NODELOCKED) == 0)
292 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
293 auio.uio_iov = &aiov;
295 aiov.iov_base = base;
297 auio.uio_resid = len;
298 auio.uio_offset = offset;
299 auio.uio_segflg = segflg;
302 if (rw == UIO_READ) {
303 error = VOP_READ(vp, &auio, ioflg, cred);
305 error = VOP_WRITE(vp, &auio, ioflg, cred);
308 *aresid = auio.uio_resid;
310 if (auio.uio_resid && error == 0)
312 if ((ioflg & IO_NODELOCKED) == 0)
313 VOP_UNLOCK(vp, 0, td);
318 * Package up an I/O request on a vnode into a uio and do it. The I/O
319 * request is split up into smaller chunks and we try to avoid saturating
320 * the buffer cache while potentially holding a vnode locked, so we
321 * check bwillwrite() before calling vn_rdwr(). We also call uio_yield()
322 * to give other processes a chance to lock the vnode (either other processes
323 * core'ing the same binary, or unrelated processes scanning the directory).
326 vn_rdwr_inchunks(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, td)
341 int chunk = (len > MAXBSIZE) ? MAXBSIZE : len;
343 if (rw != UIO_READ && vp->v_type == VREG)
345 error = vn_rdwr(rw, vp, base, chunk, offset, segflg,
346 ioflg, cred, aresid, td);
347 len -= chunk; /* aresid calc already includes length */
360 * File table vnode read routine.
363 vn_read(fp, uio, cred, flags, td)
373 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", uio->uio_td, td));
374 vp = (struct vnode *)fp->f_data;
376 if (fp->f_flag & FNONBLOCK)
378 if (fp->f_flag & O_DIRECT)
380 VOP_LEASE(vp, td, cred, LEASE_READ);
381 vn_lock(vp, LK_SHARED | LK_NOPAUSE | LK_RETRY, td);
382 if ((flags & FOF_OFFSET) == 0)
383 uio->uio_offset = fp->f_offset;
385 ioflag |= sequential_heuristic(uio, fp);
387 error = VOP_READ(vp, uio, ioflag, cred);
388 if ((flags & FOF_OFFSET) == 0)
389 fp->f_offset = uio->uio_offset;
390 fp->f_nextoff = uio->uio_offset;
391 VOP_UNLOCK(vp, 0, td);
396 * File table vnode write routine.
399 vn_write(fp, uio, cred, flags, td)
409 KASSERT(uio->uio_td == td, ("uio_procp %p is not p %p",
411 vp = (struct vnode *)fp->f_data;
412 if (vp->v_type == VREG)
414 vp = (struct vnode *)fp->f_data; /* XXX needed? */
416 if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
418 if (fp->f_flag & FNONBLOCK)
420 if (fp->f_flag & O_DIRECT)
422 if ((fp->f_flag & O_FSYNC) ||
423 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
425 VOP_LEASE(vp, td, cred, LEASE_WRITE);
426 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
427 if ((flags & FOF_OFFSET) == 0)
428 uio->uio_offset = fp->f_offset;
429 ioflag |= sequential_heuristic(uio, fp);
430 error = VOP_WRITE(vp, uio, ioflag, cred);
431 if ((flags & FOF_OFFSET) == 0)
432 fp->f_offset = uio->uio_offset;
433 fp->f_nextoff = uio->uio_offset;
434 VOP_UNLOCK(vp, 0, td);
439 * File table vnode stat routine.
442 vn_statfile(struct file *fp, struct stat *sb, struct thread *td)
444 struct vnode *vp = (struct vnode *)fp->f_data;
446 return vn_stat(vp, sb, td);
450 vn_stat(struct vnode *vp, struct stat *sb, struct thread *td)
458 error = VOP_GETATTR(vp, vap, td);
463 * Zero the spare stat fields
466 sb->st_qspare[0] = 0;
467 sb->st_qspare[1] = 0;
470 * Copy from vattr table
472 if (vap->va_fsid != VNOVAL)
473 sb->st_dev = vap->va_fsid;
475 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
476 sb->st_ino = vap->va_fileid;
478 switch (vap->va_type) {
493 /* This is a cosmetic change, symlinks do not have a mode. */
494 if (vp->v_mount->mnt_flag & MNT_NOSYMFOLLOW)
495 sb->st_mode &= ~ACCESSPERMS; /* 0000 */
497 sb->st_mode |= ACCESSPERMS; /* 0777 */
509 sb->st_nlink = vap->va_nlink;
510 sb->st_uid = vap->va_uid;
511 sb->st_gid = vap->va_gid;
512 sb->st_rdev = vap->va_rdev;
513 sb->st_size = vap->va_size;
514 sb->st_atimespec = vap->va_atime;
515 sb->st_mtimespec = vap->va_mtime;
516 sb->st_ctimespec = vap->va_ctime;
519 * According to www.opengroup.org, the meaning of st_blksize is
520 * "a filesystem-specific preferred I/O block size for this
521 * object. In some filesystem types, this may vary from file
523 * Default to PAGE_SIZE after much discussion.
526 if (vap->va_type == VREG) {
527 sb->st_blksize = vap->va_blocksize;
528 } else if (vn_isdisk(vp, NULL)) {
529 sb->st_blksize = vp->v_rdev->si_bsize_best;
530 if (sb->st_blksize < vp->v_rdev->si_bsize_phys)
531 sb->st_blksize = vp->v_rdev->si_bsize_phys;
532 if (sb->st_blksize < BLKDEV_IOSIZE)
533 sb->st_blksize = BLKDEV_IOSIZE;
535 sb->st_blksize = PAGE_SIZE;
538 sb->st_flags = vap->va_flags;
542 sb->st_gen = vap->va_gen;
544 #if (S_BLKSIZE == 512)
545 /* Optimize this case */
546 sb->st_blocks = vap->va_bytes >> 9;
548 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
554 * File table vnode ioctl routine.
557 vn_ioctl(struct file *fp, u_long com, caddr_t data, struct thread *td)
559 struct vnode *vp = ((struct vnode *)fp->f_data);
564 KKASSERT(td->td_proc != NULL);
565 ucred = td->td_proc->p_ucred;
567 switch (vp->v_type) {
570 if (com == FIONREAD) {
571 error = VOP_GETATTR(vp, &vattr, td);
574 *(int *)data = vattr.va_size - fp->f_offset;
577 if (com == FIONBIO || com == FIOASYNC) /* XXX */
578 return (0); /* XXX */
587 if (com == FIODTYPE) {
588 if (vp->v_type != VCHR && vp->v_type != VBLK)
590 *(int *)data = dev_dflags(vp->v_rdev) & D_TYPEMASK;
593 error = VOP_IOCTL(vp, com, data, fp->f_flag, ucred, td);
594 if (error == 0 && com == TIOCSCTTY) {
595 struct session *sess = td->td_proc->p_session;
597 /* Do nothing if reassigning same control tty */
598 if (sess->s_ttyvp == vp)
601 /* Get rid of reference to old control tty */
603 vrele(sess->s_ttyvp);
613 * File table vnode poll routine.
616 vn_poll(struct file *fp, int events, struct ucred *cred, struct thread *td)
618 return (VOP_POLL(((struct vnode *)fp->f_data), events, cred, td));
622 * Check that the vnode is still valid, and if so
623 * acquire requested lock.
627 vn_lock(struct vnode *vp, int flags, struct thread *td)
629 debug_vn_lock(struct vnode *vp, int flags, struct thread *td,
630 const char *filename, int line)
636 if ((flags & LK_INTERLOCK) == 0)
637 lwkt_gettoken(&vp->v_interlock);
638 if ((vp->v_flag & VXLOCK) && vp->v_vxproc != curproc) {
639 vp->v_flag |= VXWANT;
640 lwkt_reltoken(&vp->v_interlock);
641 tsleep((caddr_t)vp, 0, "vn_lock", 0);
645 /* this can now occur in normal operation */
646 if (vp->v_vxproc != NULL)
647 log(LOG_INFO, "VXLOCK interlock avoided in vn_lock\n");
650 vp->filename = filename;
654 flags | LK_NOPAUSE | LK_INTERLOCK, td);
658 flags &= ~LK_INTERLOCK;
659 } while (flags & LK_RETRY);
664 * File table vnode close routine.
667 vn_closefile(struct file *fp, struct thread *td)
671 fp->f_ops = &badfileops;
672 err = vn_close(((struct vnode *)fp->f_data), fp->f_flag, td);
677 vn_kqfilter(struct file *fp, struct knote *kn)
680 return (VOP_KQFILTER(((struct vnode *)fp->f_data), kn));