Home | History | Annotate | Line # | Download | only in kern
vfs_vnops.c revision 1.23.2.1
      1 /*	$NetBSD: vfs_vnops.c,v 1.23.2.1 1998/01/29 10:17:17 mellon 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 #include <sys/poll.h>
     56 
     57 #include <vm/vm.h>
     58 
     59 struct 	fileops vnops =
     60 	{ vn_read, vn_write, vn_ioctl, vn_poll, vn_closefile };
     61 
     62 /*
     63  * Common code for vnode open operations.
     64  * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
     65  */
     66 int
     67 vn_open(ndp, fmode, cmode)
     68 	register struct nameidata *ndp;
     69 	int fmode, cmode;
     70 {
     71 	register struct vnode *vp;
     72 	register struct proc *p = ndp->ni_cnd.cn_proc;
     73 	register struct ucred *cred = p->p_ucred;
     74 	struct vattr va;
     75 	int error;
     76 
     77 	if (fmode & O_CREAT) {
     78 		ndp->ni_cnd.cn_nameiop = CREATE;
     79 		ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
     80 		if ((fmode & O_EXCL) == 0)
     81 			ndp->ni_cnd.cn_flags |= FOLLOW;
     82 		if ((error = namei(ndp)) != 0)
     83 			return (error);
     84 		if (ndp->ni_vp == NULL) {
     85 			VATTR_NULL(&va);
     86 			va.va_type = VREG;
     87 			va.va_mode = cmode;
     88 			VOP_LEASE(ndp->ni_dvp, p, cred, LEASE_WRITE);
     89 			error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
     90 					   &ndp->ni_cnd, &va);
     91 			if (error)
     92 				return (error);
     93 			fmode &= ~O_TRUNC;
     94 			vp = ndp->ni_vp;
     95 		} else {
     96 			VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
     97 			if (ndp->ni_dvp == ndp->ni_vp)
     98 				vrele(ndp->ni_dvp);
     99 			else
    100 				vput(ndp->ni_dvp);
    101 			ndp->ni_dvp = NULL;
    102 			vp = ndp->ni_vp;
    103 			if (fmode & O_EXCL) {
    104 				error = EEXIST;
    105 				goto bad;
    106 			}
    107 			fmode &= ~O_CREAT;
    108 		}
    109 	} else {
    110 		ndp->ni_cnd.cn_nameiop = LOOKUP;
    111 		ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF;
    112 		if ((error = namei(ndp)) != 0)
    113 			return (error);
    114 		vp = ndp->ni_vp;
    115 	}
    116 	if (vp->v_type == VSOCK) {
    117 		error = EOPNOTSUPP;
    118 		goto bad;
    119 	}
    120 	if ((fmode & O_CREAT) == 0) {
    121 		if (fmode & FREAD) {
    122 			if ((error = VOP_ACCESS(vp, VREAD, cred, p)) != 0)
    123 				goto bad;
    124 		}
    125 		if (fmode & (FWRITE | O_TRUNC)) {
    126 			if (vp->v_type == VDIR) {
    127 				error = EISDIR;
    128 				goto bad;
    129 			}
    130 			if ((error = vn_writechk(vp)) != 0 ||
    131 			    (error = VOP_ACCESS(vp, VWRITE, cred, p)) != 0)
    132 				goto bad;
    133 		}
    134 	}
    135 	if (fmode & O_TRUNC) {
    136 		VOP_UNLOCK(vp);				/* XXX */
    137 		VOP_LEASE(vp, p, cred, LEASE_WRITE);
    138 		VOP_LOCK(vp);				/* XXX */
    139 		VATTR_NULL(&va);
    140 		va.va_size = 0;
    141 		if ((error = VOP_SETATTR(vp, &va, cred, p)) != 0)
    142 			goto bad;
    143 	}
    144 	if ((error = VOP_OPEN(vp, fmode, cred, p)) != 0)
    145 		goto bad;
    146 	if (fmode & FWRITE)
    147 		vp->v_writecount++;
    148 	return (0);
    149 bad:
    150 	vput(vp);
    151 	return (error);
    152 }
    153 
    154 /*
    155  * Check for write permissions on the specified vnode.
    156  * The read-only status of the file system is checked.
    157  * Also, prototype text segments cannot be written.
    158  */
    159 int
    160 vn_writechk(vp)
    161 	register struct vnode *vp;
    162 {
    163 
    164 	/*
    165 	 * Disallow write attempts on read-only file systems;
    166 	 * unless the file is a socket or a block or character
    167 	 * device resident on the file system.
    168 	 */
    169 	if (vp->v_mount->mnt_flag & MNT_RDONLY) {
    170 		switch (vp->v_type) {
    171 		case VREG: case VDIR: case VLNK:
    172 			return (EROFS);
    173 		case VNON: case VCHR: case VSOCK:
    174 		case VFIFO: case VBAD: case VBLK:
    175 			break;
    176 		}
    177 	}
    178 	/*
    179 	 * If there's shared text associated with
    180 	 * the vnode, try to free it up once.  If
    181 	 * we fail, we can't allow writing.
    182 	 */
    183 	if ((vp->v_flag & VTEXT) && !vnode_pager_uncache(vp))
    184 		return (ETXTBSY);
    185 	return (0);
    186 }
    187 
    188 /*
    189  * Vnode close call
    190  */
    191 int
    192 vn_close(vp, flags, cred, p)
    193 	register struct vnode *vp;
    194 	int flags;
    195 	struct ucred *cred;
    196 	struct proc *p;
    197 {
    198 	int error;
    199 
    200 	if (flags & FWRITE)
    201 		vp->v_writecount--;
    202 	error = VOP_CLOSE(vp, flags, cred, p);
    203 	vrele(vp);
    204 	return (error);
    205 }
    206 
    207 /*
    208  * Package up an I/O request on a vnode into a uio and do it.
    209  */
    210 int
    211 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p)
    212 	enum uio_rw rw;
    213 	struct vnode *vp;
    214 	caddr_t base;
    215 	int len;
    216 	off_t offset;
    217 	enum uio_seg segflg;
    218 	int ioflg;
    219 	struct ucred *cred;
    220 	int *aresid;
    221 	struct proc *p;
    222 {
    223 	struct uio auio;
    224 	struct iovec aiov;
    225 	int error;
    226 
    227 	if ((ioflg & IO_NODELOCKED) == 0)
    228 		VOP_LOCK(vp);
    229 	auio.uio_iov = &aiov;
    230 	auio.uio_iovcnt = 1;
    231 	aiov.iov_base = base;
    232 	aiov.iov_len = len;
    233 	auio.uio_resid = len;
    234 	auio.uio_offset = offset;
    235 	auio.uio_segflg = segflg;
    236 	auio.uio_rw = rw;
    237 	auio.uio_procp = p;
    238 	if (rw == UIO_READ) {
    239 		error = VOP_READ(vp, &auio, ioflg, cred);
    240 	} else {
    241 		error = VOP_WRITE(vp, &auio, ioflg, cred);
    242 	}
    243 	if (aresid)
    244 		*aresid = auio.uio_resid;
    245 	else
    246 		if (auio.uio_resid && error == 0)
    247 			error = EIO;
    248 	if ((ioflg & IO_NODELOCKED) == 0)
    249 		VOP_UNLOCK(vp);
    250 	return (error);
    251 }
    252 
    253 int
    254 vn_readdir(fp, buf, segflg, count, done, p, cookies, ncookies)
    255 	struct file *fp;
    256 	char *buf;
    257 	int segflg, *done, ncookies;
    258 	u_int count;
    259 	struct proc *p;
    260 	off_t *cookies;
    261 {
    262 	struct vnode *vp = (struct vnode *)fp->f_data;
    263 	struct iovec aiov;
    264 	struct uio auio;
    265 	int error, eofflag;
    266 
    267 unionread:
    268 	if (vp->v_type != VDIR)
    269 		return (EINVAL);
    270 	aiov.iov_base = buf;
    271 	aiov.iov_len = count;
    272 	auio.uio_iov = &aiov;
    273 	auio.uio_iovcnt = 1;
    274 	auio.uio_rw = UIO_READ;
    275 	auio.uio_segflg = segflg;
    276 	auio.uio_procp = p;
    277 	auio.uio_resid = count;
    278 	VOP_LOCK(vp);
    279 	auio.uio_offset = fp->f_offset;
    280 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, (off_t *)cookies,
    281 		    ncookies);
    282 	fp->f_offset = auio.uio_offset;
    283 	VOP_UNLOCK(vp);
    284 	if (error)
    285 		return (error);
    286 
    287 #ifdef UNION
    288 {
    289 	extern int (**union_vnodeop_p) __P((void *));
    290 	extern struct vnode *union_dircache __P((struct vnode *));
    291 
    292 	if (count == auio.uio_resid && (vp->v_op == union_vnodeop_p)) {
    293 		struct vnode *lvp;
    294 
    295 		lvp = union_dircache(vp);
    296 		if (lvp != NULLVP) {
    297 			struct vattr va;
    298 
    299 			/*
    300 			 * If the directory is opaque,
    301 			 * then don't show lower entries
    302 			 */
    303 			error = VOP_GETATTR(vp, &va, fp->f_cred, p);
    304 			if (va.va_flags & OPAQUE) {
    305 				vput(lvp);
    306 				lvp = NULL;
    307 			}
    308 		}
    309 
    310 		if (lvp != NULLVP) {
    311 			error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
    312 			VOP_UNLOCK(lvp);
    313 
    314 			if (error) {
    315 				vrele(lvp);
    316 				return (error);
    317 			}
    318 			fp->f_data = (caddr_t) lvp;
    319 			fp->f_offset = 0;
    320 			error = vn_close(vp, FREAD, fp->f_cred, p);
    321 			if (error)
    322 				return (error);
    323 			vp = lvp;
    324 			goto unionread;
    325 		}
    326 	}
    327 }
    328 #endif /* UNION */
    329 
    330 	if (count == auio.uio_resid && (vp->v_flag & VROOT) &&
    331 	    (vp->v_mount->mnt_flag & MNT_UNION)) {
    332 		struct vnode *tvp = vp;
    333 		vp = vp->v_mount->mnt_vnodecovered;
    334 		VREF(vp);
    335 		fp->f_data = (caddr_t) vp;
    336 		fp->f_offset = 0;
    337 		vrele(tvp);
    338 		goto unionread;
    339 	}
    340 	*done = count - auio.uio_resid;
    341 	return error;
    342 }
    343 
    344 /*
    345  * File table vnode read routine.
    346  */
    347 int
    348 vn_read(fp, uio, cred)
    349 	struct file *fp;
    350 	struct uio *uio;
    351 	struct ucred *cred;
    352 {
    353 	register struct vnode *vp = (struct vnode *)fp->f_data;
    354 	int count, error;
    355 
    356 	VOP_LEASE(vp, uio->uio_procp, cred, LEASE_READ);
    357 	VOP_LOCK(vp);
    358 	uio->uio_offset = fp->f_offset;
    359 	count = uio->uio_resid;
    360 	error = VOP_READ(vp, uio, (fp->f_flag & FNONBLOCK) ? IO_NDELAY : 0,
    361 		cred);
    362 	fp->f_offset += count - uio->uio_resid;
    363 	VOP_UNLOCK(vp);
    364 	return (error);
    365 }
    366 
    367 /*
    368  * File table vnode write routine.
    369  */
    370 int
    371 vn_write(fp, uio, cred)
    372 	struct file *fp;
    373 	struct uio *uio;
    374 	struct ucred *cred;
    375 {
    376 	register struct vnode *vp = (struct vnode *)fp->f_data;
    377 	int count, error, ioflag = IO_UNIT;
    378 
    379 	if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
    380 		ioflag |= IO_APPEND;
    381 	if (fp->f_flag & FNONBLOCK)
    382 		ioflag |= IO_NDELAY;
    383 	if (fp->f_flag & FFSYNC ||
    384 	    (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
    385 		ioflag |= IO_SYNC;
    386 	VOP_LEASE(vp, uio->uio_procp, cred, LEASE_WRITE);
    387 	VOP_LOCK(vp);
    388 	uio->uio_offset = fp->f_offset;
    389 	count = uio->uio_resid;
    390 	error = VOP_WRITE(vp, uio, ioflag, cred);
    391 	if (ioflag & IO_APPEND)
    392 		fp->f_offset = uio->uio_offset;
    393 	else
    394 		fp->f_offset += count - uio->uio_resid;
    395 	VOP_UNLOCK(vp);
    396 	return (error);
    397 }
    398 
    399 /*
    400  * File table vnode stat routine.
    401  */
    402 int
    403 vn_stat(vp, sb, p)
    404 	struct vnode *vp;
    405 	register struct stat *sb;
    406 	struct proc *p;
    407 {
    408 	struct vattr va;
    409 	int error;
    410 	u_short mode;
    411 
    412 	error = VOP_GETATTR(vp, &va, p->p_ucred, p);
    413 	if (error)
    414 		return (error);
    415 	/*
    416 	 * Copy from vattr table
    417 	 */
    418 	sb->st_dev = va.va_fsid;
    419 	sb->st_ino = va.va_fileid;
    420 	mode = va.va_mode;
    421 	switch (vp->v_type) {
    422 	case VREG:
    423 		mode |= S_IFREG;
    424 		break;
    425 	case VDIR:
    426 		mode |= S_IFDIR;
    427 		break;
    428 	case VBLK:
    429 		mode |= S_IFBLK;
    430 		break;
    431 	case VCHR:
    432 		mode |= S_IFCHR;
    433 		break;
    434 	case VLNK:
    435 		mode |= S_IFLNK;
    436 		break;
    437 	case VSOCK:
    438 		mode |= S_IFSOCK;
    439 		break;
    440 	case VFIFO:
    441 		mode |= S_IFIFO;
    442 		break;
    443 	default:
    444 		return (EBADF);
    445 	};
    446 	sb->st_mode = mode;
    447 	sb->st_nlink = va.va_nlink;
    448 	sb->st_uid = va.va_uid;
    449 	sb->st_gid = va.va_gid;
    450 	sb->st_rdev = va.va_rdev;
    451 	sb->st_size = va.va_size;
    452 	sb->st_atimespec = va.va_atime;
    453 	sb->st_mtimespec = va.va_mtime;
    454 	sb->st_ctimespec = va.va_ctime;
    455 	sb->st_blksize = va.va_blocksize;
    456 	sb->st_flags = va.va_flags;
    457 	sb->st_gen = 0;
    458 	sb->st_blocks = va.va_bytes / S_BLKSIZE;
    459 	return (0);
    460 }
    461 
    462 /*
    463  * File table vnode ioctl routine.
    464  */
    465 int
    466 vn_ioctl(fp, com, data, p)
    467 	struct file *fp;
    468 	u_long com;
    469 	caddr_t data;
    470 	struct proc *p;
    471 {
    472 	register struct vnode *vp = ((struct vnode *)fp->f_data);
    473 	struct vattr vattr;
    474 	int error;
    475 
    476 	switch (vp->v_type) {
    477 
    478 	case VREG:
    479 	case VDIR:
    480 		if (com == FIONREAD) {
    481 			error = VOP_GETATTR(vp, &vattr, p->p_ucred, p);
    482 			if (error)
    483 				return (error);
    484 			*(int *)data = vattr.va_size - fp->f_offset;
    485 			return (0);
    486 		}
    487 		if (com == FIONBIO || com == FIOASYNC)	/* XXX */
    488 			return (0);			/* XXX */
    489 		/* fall into ... */
    490 
    491 	default:
    492 		return (ENOTTY);
    493 
    494 	case VFIFO:
    495 	case VCHR:
    496 	case VBLK:
    497 		error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p);
    498 		if (error == 0 && com == TIOCSCTTY) {
    499 			if (p->p_session->s_ttyvp)
    500 				vrele(p->p_session->s_ttyvp);
    501 			p->p_session->s_ttyvp = vp;
    502 			VREF(vp);
    503 		}
    504 		return (error);
    505 	}
    506 }
    507 
    508 /*
    509  * File table vnode poll routine.
    510  */
    511 int
    512 vn_poll(fp, events, p)
    513 	struct file *fp;
    514 	int events;
    515 	struct proc *p;
    516 {
    517 
    518 	return (VOP_POLL(((struct vnode *)fp->f_data), events, p));
    519 }
    520 
    521 /*
    522  * File table vnode close routine.
    523  */
    524 int
    525 vn_closefile(fp, p)
    526 	struct file *fp;
    527 	struct proc *p;
    528 {
    529 
    530 	return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
    531 		fp->f_cred, p));
    532 }
    533