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