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