Home | History | Annotate | Line # | Download | only in kern
vfs_vnops.c revision 1.20
      1 /*	$NetBSD: vfs_vnops.c,v 1.20 1996/02/04 02:18:41 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  * (c) UNIX System Laboratories, Inc.
      7  * All or some portions of this file are derived from material licensed
      8  * to the University of California by American Telephone and Telegraph
      9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  * the permission of UNIX System Laboratories, Inc.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the University of
     23  *	California, Berkeley and its contributors.
     24  * 4. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	@(#)vfs_vnops.c	8.5 (Berkeley) 12/8/94
     41  */
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/kernel.h>
     46 #include <sys/file.h>
     47 #include <sys/stat.h>
     48 #include <sys/buf.h>
     49 #include <sys/proc.h>
     50 #include <sys/mount.h>
     51 #include <sys/namei.h>
     52 #include <sys/vnode.h>
     53 #include <sys/ioctl.h>
     54 #include <sys/tty.h>
     55 
     56 #include <vm/vm.h>
     57 
     58 struct 	fileops vnops =
     59 	{ vn_read, vn_write, vn_ioctl, vn_select, vn_closefile };
     60 
     61 /*
     62  * Common code for vnode open operations.
     63  * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
     64  */
     65 int
     66 vn_open(ndp, fmode, cmode)
     67 	register struct nameidata *ndp;
     68 	int fmode, cmode;
     69 {
     70 	register struct vnode *vp;
     71 	register struct proc *p = ndp->ni_cnd.cn_proc;
     72 	register struct ucred *cred = p->p_ucred;
     73 	struct vattr va;
     74 	int error;
     75 
     76 	if (fmode & O_CREAT) {
     77 		ndp->ni_cnd.cn_nameiop = CREATE;
     78 		ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
     79 		if ((fmode & O_EXCL) == 0)
     80 			ndp->ni_cnd.cn_flags |= FOLLOW;
     81 		if ((error = namei(ndp)) != 0)
     82 			return (error);
     83 		if (ndp->ni_vp == NULL) {
     84 			VATTR_NULL(&va);
     85 			va.va_type = VREG;
     86 			va.va_mode = cmode;
     87 			VOP_LEASE(ndp->ni_dvp, p, cred, LEASE_WRITE);
     88 			error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
     89 					   &ndp->ni_cnd, &va);
     90 			if (error)
     91 				return (error);
     92 			fmode &= ~O_TRUNC;
     93 			vp = ndp->ni_vp;
     94 		} else {
     95 			VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
     96 			if (ndp->ni_dvp == ndp->ni_vp)
     97 				vrele(ndp->ni_dvp);
     98 			else
     99 				vput(ndp->ni_dvp);
    100 			ndp->ni_dvp = NULL;
    101 			vp = ndp->ni_vp;
    102 			if (fmode & O_EXCL) {
    103 				error = EEXIST;
    104 				goto bad;
    105 			}
    106 			fmode &= ~O_CREAT;
    107 		}
    108 	} else {
    109 		ndp->ni_cnd.cn_nameiop = LOOKUP;
    110 		ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF;
    111 		if ((error = namei(ndp)) != 0)
    112 			return (error);
    113 		vp = ndp->ni_vp;
    114 	}
    115 	if (vp->v_type == VSOCK) {
    116 		error = EOPNOTSUPP;
    117 		goto bad;
    118 	}
    119 	if ((fmode & O_CREAT) == 0) {
    120 		if (fmode & FREAD) {
    121 			if ((error = VOP_ACCESS(vp, VREAD, cred, p)) != 0)
    122 				goto bad;
    123 		}
    124 		if (fmode & (FWRITE | O_TRUNC)) {
    125 			if (vp->v_type == VDIR) {
    126 				error = EISDIR;
    127 				goto bad;
    128 			}
    129 			if ((error = vn_writechk(vp)) != 0 ||
    130 			    (error = VOP_ACCESS(vp, VWRITE, cred, p)) != 0)
    131 				goto bad;
    132 		}
    133 	}
    134 	if (fmode & O_TRUNC) {
    135 		VOP_UNLOCK(vp);				/* XXX */
    136 		VOP_LEASE(vp, p, cred, LEASE_WRITE);
    137 		VOP_LOCK(vp);				/* XXX */
    138 		VATTR_NULL(&va);
    139 		va.va_size = 0;
    140 		if ((error = VOP_SETATTR(vp, &va, cred, p)) != 0)
    141 			goto bad;
    142 	}
    143 	if ((error = VOP_OPEN(vp, fmode, cred, p)) != 0)
    144 		goto bad;
    145 	if (fmode & FWRITE)
    146 		vp->v_writecount++;
    147 	return (0);
    148 bad:
    149 	vput(vp);
    150 	return (error);
    151 }
    152 
    153 /*
    154  * Check for write permissions on the specified vnode.
    155  * The read-only status of the file system is checked.
    156  * Also, prototype text segments cannot be written.
    157  */
    158 int
    159 vn_writechk(vp)
    160 	register struct vnode *vp;
    161 {
    162 
    163 	/*
    164 	 * Disallow write attempts on read-only file systems;
    165 	 * unless the file is a socket or a block or character
    166 	 * device resident on the file system.
    167 	 */
    168 	if (vp->v_mount->mnt_flag & MNT_RDONLY) {
    169 		switch (vp->v_type) {
    170 		case VREG: case VDIR: case VLNK:
    171 			return (EROFS);
    172 		case VNON: case VCHR: case VSOCK:
    173 		case VFIFO: case VBAD: case VBLK:
    174 			break;
    175 		}
    176 	}
    177 	/*
    178 	 * If there's shared text associated with
    179 	 * the vnode, try to free it up once.  If
    180 	 * we fail, we can't allow writing.
    181 	 */
    182 	if ((vp->v_flag & VTEXT) && !vnode_pager_uncache(vp))
    183 		return (ETXTBSY);
    184 	return (0);
    185 }
    186 
    187 /*
    188  * Vnode close call
    189  */
    190 int
    191 vn_close(vp, flags, cred, p)
    192 	register struct vnode *vp;
    193 	int flags;
    194 	struct ucred *cred;
    195 	struct proc *p;
    196 {
    197 	int error;
    198 
    199 	if (flags & FWRITE)
    200 		vp->v_writecount--;
    201 	error = VOP_CLOSE(vp, flags, cred, p);
    202 	vrele(vp);
    203 	return (error);
    204 }
    205 
    206 /*
    207  * Package up an I/O request on a vnode into a uio and do it.
    208  */
    209 int
    210 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p)
    211 	enum uio_rw rw;
    212 	struct vnode *vp;
    213 	caddr_t base;
    214 	int len;
    215 	off_t offset;
    216 	enum uio_seg segflg;
    217 	int ioflg;
    218 	struct ucred *cred;
    219 	int *aresid;
    220 	struct proc *p;
    221 {
    222 	struct uio auio;
    223 	struct iovec aiov;
    224 	int error;
    225 
    226 	if ((ioflg & IO_NODELOCKED) == 0)
    227 		VOP_LOCK(vp);
    228 	auio.uio_iov = &aiov;
    229 	auio.uio_iovcnt = 1;
    230 	aiov.iov_base = base;
    231 	aiov.iov_len = len;
    232 	auio.uio_resid = len;
    233 	auio.uio_offset = offset;
    234 	auio.uio_segflg = segflg;
    235 	auio.uio_rw = rw;
    236 	auio.uio_procp = p;
    237 	if (rw == UIO_READ) {
    238 		error = VOP_READ(vp, &auio, ioflg, cred);
    239 	} else {
    240 		error = VOP_WRITE(vp, &auio, ioflg, cred);
    241 	}
    242 	if (aresid)
    243 		*aresid = auio.uio_resid;
    244 	else
    245 		if (auio.uio_resid && error == 0)
    246 			error = EIO;
    247 	if ((ioflg & IO_NODELOCKED) == 0)
    248 		VOP_UNLOCK(vp);
    249 	return (error);
    250 }
    251 
    252 /*
    253  * File table vnode read routine.
    254  */
    255 int
    256 vn_read(fp, uio, cred)
    257 	struct file *fp;
    258 	struct uio *uio;
    259 	struct ucred *cred;
    260 {
    261 	register struct vnode *vp = (struct vnode *)fp->f_data;
    262 	int count, error;
    263 
    264 	VOP_LEASE(vp, uio->uio_procp, cred, LEASE_READ);
    265 	VOP_LOCK(vp);
    266 	uio->uio_offset = fp->f_offset;
    267 	count = uio->uio_resid;
    268 	error = VOP_READ(vp, uio, (fp->f_flag & FNONBLOCK) ? IO_NDELAY : 0,
    269 		cred);
    270 	fp->f_offset += count - uio->uio_resid;
    271 	VOP_UNLOCK(vp);
    272 	return (error);
    273 }
    274 
    275 /*
    276  * File table vnode write routine.
    277  */
    278 int
    279 vn_write(fp, uio, cred)
    280 	struct file *fp;
    281 	struct uio *uio;
    282 	struct ucred *cred;
    283 {
    284 	register struct vnode *vp = (struct vnode *)fp->f_data;
    285 	int count, error, ioflag = IO_UNIT;
    286 
    287 	if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
    288 		ioflag |= IO_APPEND;
    289 	if (fp->f_flag & FNONBLOCK)
    290 		ioflag |= IO_NDELAY;
    291 	VOP_LEASE(vp, uio->uio_procp, cred, LEASE_WRITE);
    292 	VOP_LOCK(vp);
    293 	uio->uio_offset = fp->f_offset;
    294 	count = uio->uio_resid;
    295 	error = VOP_WRITE(vp, uio, ioflag, cred);
    296 	if (ioflag & IO_APPEND)
    297 		fp->f_offset = uio->uio_offset;
    298 	else
    299 		fp->f_offset += count - uio->uio_resid;
    300 	VOP_UNLOCK(vp);
    301 	return (error);
    302 }
    303 
    304 /*
    305  * File table vnode stat routine.
    306  */
    307 int
    308 vn_stat(vp, sb, p)
    309 	struct vnode *vp;
    310 	register struct stat *sb;
    311 	struct proc *p;
    312 {
    313 	struct vattr va;
    314 	int error;
    315 	u_short mode;
    316 
    317 	error = VOP_GETATTR(vp, &va, p->p_ucred, p);
    318 	if (error)
    319 		return (error);
    320 	/*
    321 	 * Copy from vattr table
    322 	 */
    323 	sb->st_dev = va.va_fsid;
    324 	sb->st_ino = va.va_fileid;
    325 	mode = va.va_mode;
    326 	switch (vp->v_type) {
    327 	case VREG:
    328 		mode |= S_IFREG;
    329 		break;
    330 	case VDIR:
    331 		mode |= S_IFDIR;
    332 		break;
    333 	case VBLK:
    334 		mode |= S_IFBLK;
    335 		break;
    336 	case VCHR:
    337 		mode |= S_IFCHR;
    338 		break;
    339 	case VLNK:
    340 		mode |= S_IFLNK;
    341 		break;
    342 	case VSOCK:
    343 		mode |= S_IFSOCK;
    344 		break;
    345 	case VFIFO:
    346 		mode |= S_IFIFO;
    347 		break;
    348 	default:
    349 		return (EBADF);
    350 	};
    351 	sb->st_mode = mode;
    352 	sb->st_nlink = va.va_nlink;
    353 	sb->st_uid = va.va_uid;
    354 	sb->st_gid = va.va_gid;
    355 	sb->st_rdev = va.va_rdev;
    356 	sb->st_size = va.va_size;
    357 	sb->st_atimespec = va.va_atime;
    358 	sb->st_mtimespec = va.va_mtime;
    359 	sb->st_ctimespec = va.va_ctime;
    360 	sb->st_blksize = va.va_blocksize;
    361 	sb->st_flags = va.va_flags;
    362 	sb->st_gen = va.va_gen;
    363 	sb->st_blocks = va.va_bytes / S_BLKSIZE;
    364 	return (0);
    365 }
    366 
    367 /*
    368  * File table vnode ioctl routine.
    369  */
    370 int
    371 vn_ioctl(fp, com, data, p)
    372 	struct file *fp;
    373 	u_long com;
    374 	caddr_t data;
    375 	struct proc *p;
    376 {
    377 	register struct vnode *vp = ((struct vnode *)fp->f_data);
    378 	struct vattr vattr;
    379 	int error;
    380 
    381 	switch (vp->v_type) {
    382 
    383 	case VREG:
    384 	case VDIR:
    385 		if (com == FIONREAD) {
    386 			error = VOP_GETATTR(vp, &vattr, p->p_ucred, p);
    387 			if (error)
    388 				return (error);
    389 			*(int *)data = vattr.va_size - fp->f_offset;
    390 			return (0);
    391 		}
    392 		if (com == FIONBIO || com == FIOASYNC)	/* XXX */
    393 			return (0);			/* XXX */
    394 		/* fall into ... */
    395 
    396 	default:
    397 		return (ENOTTY);
    398 
    399 	case VFIFO:
    400 	case VCHR:
    401 	case VBLK:
    402 		error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p);
    403 		if (error == 0 && com == TIOCSCTTY) {
    404 			if (p->p_session->s_ttyvp)
    405 				vrele(p->p_session->s_ttyvp);
    406 			p->p_session->s_ttyvp = vp;
    407 			VREF(vp);
    408 		}
    409 		return (error);
    410 	}
    411 }
    412 
    413 /*
    414  * File table vnode select routine.
    415  */
    416 int
    417 vn_select(fp, which, p)
    418 	struct file *fp;
    419 	int which;
    420 	struct proc *p;
    421 {
    422 
    423 	return (VOP_SELECT(((struct vnode *)fp->f_data), which, fp->f_flag,
    424 			   fp->f_cred, p));
    425 }
    426 
    427 /*
    428  * File table vnode close routine.
    429  */
    430 int
    431 vn_closefile(fp, p)
    432 	struct file *fp;
    433 	struct proc *p;
    434 {
    435 
    436 	return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
    437 		fp->f_cred, p));
    438 }
    439