Home | History | Annotate | Line # | Download | only in kern
vfs_vnops.c revision 1.43
      1 /*	$NetBSD: vfs_vnops.c,v 1.43 2000/06/27 17:41:54 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.14 (Berkeley) 6/15/95
     41  */
     42 
     43 #include "fs_union.h"
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/kernel.h>
     48 #include <sys/file.h>
     49 #include <sys/stat.h>
     50 #include <sys/buf.h>
     51 #include <sys/proc.h>
     52 #include <sys/mount.h>
     53 #include <sys/namei.h>
     54 #include <sys/vnode.h>
     55 #include <sys/ioctl.h>
     56 #include <sys/tty.h>
     57 #include <sys/poll.h>
     58 
     59 #include <uvm/uvm_extern.h>
     60 
     61 #ifdef UNION
     62 #include <miscfs/union/union.h>
     63 #endif
     64 
     65 struct 	fileops vnops =
     66 	{ vn_read, vn_write, vn_ioctl, vn_fcntl, vn_poll, vn_closefile };
     67 
     68 /*
     69  * Common code for vnode open operations.
     70  * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
     71  */
     72 int
     73 vn_open(ndp, fmode, cmode)
     74 	struct nameidata *ndp;
     75 	int fmode, cmode;
     76 {
     77 	struct vnode *vp;
     78 	struct proc *p = ndp->ni_cnd.cn_proc;
     79 	struct ucred *cred = p->p_ucred;
     80 	struct vattr va;
     81 	int error;
     82 
     83 	if (fmode & O_CREAT) {
     84 		ndp->ni_cnd.cn_nameiop = CREATE;
     85 		ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
     86 		if ((fmode & O_EXCL) == 0 &&
     87 		    ((fmode & FNOSYMLINK) == 0))
     88 			ndp->ni_cnd.cn_flags |= FOLLOW;
     89 		if ((error = namei(ndp)) != 0)
     90 			return (error);
     91 		if (ndp->ni_vp == NULL) {
     92 			VATTR_NULL(&va);
     93 			va.va_type = VREG;
     94 			va.va_mode = cmode;
     95 			if (fmode & O_EXCL)
     96 				 va.va_vaflags |= VA_EXCLUSIVE;
     97 			VOP_LEASE(ndp->ni_dvp, p, cred, LEASE_WRITE);
     98 			error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
     99 					   &ndp->ni_cnd, &va);
    100 			if (error)
    101 				return (error);
    102 			fmode &= ~O_TRUNC;
    103 			vp = ndp->ni_vp;
    104 		} else {
    105 			VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
    106 			if (ndp->ni_dvp == ndp->ni_vp)
    107 				vrele(ndp->ni_dvp);
    108 			else
    109 				vput(ndp->ni_dvp);
    110 			ndp->ni_dvp = NULL;
    111 			vp = ndp->ni_vp;
    112 			if (fmode & O_EXCL) {
    113 				error = EEXIST;
    114 				goto bad;
    115 			}
    116 			if (ndp->ni_vp->v_type == VLNK) {
    117 				error = EFTYPE;
    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 	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 ((vp->v_flag & VTEXT) && !uvm_vnp_uncache(vp))
    182 		return (ETXTBSY);
    183 	return (0);
    184 }
    185 
    186 /*
    187  * Mark a vnode as being the text image of a running process.
    188  */
    189 void
    190 vn_marktext(vp)
    191 	struct vnode *vp;
    192 {
    193 	vp->v_flag |= VTEXT;
    194 }
    195 
    196 /*
    197  * Vnode close call
    198  *
    199  * Note: takes an unlocked vnode, while VOP_CLOSE takes a locked node.
    200  */
    201 int
    202 vn_close(vp, flags, cred, p)
    203 	struct vnode *vp;
    204 	int flags;
    205 	struct ucred *cred;
    206 	struct proc *p;
    207 {
    208 	int error;
    209 
    210 	if (flags & FWRITE)
    211 		vp->v_writecount--;
    212 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    213 	error = VOP_CLOSE(vp, flags, cred, p);
    214 	vput(vp);
    215 	return (error);
    216 }
    217 
    218 /*
    219  * Package up an I/O request on a vnode into a uio and do it.
    220  */
    221 int
    222 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p)
    223 	enum uio_rw rw;
    224 	struct vnode *vp;
    225 	caddr_t base;
    226 	int len;
    227 	off_t offset;
    228 	enum uio_seg segflg;
    229 	int ioflg;
    230 	struct ucred *cred;
    231 	size_t *aresid;
    232 	struct proc *p;
    233 {
    234 	struct uio auio;
    235 	struct iovec aiov;
    236 	int error;
    237 
    238 	if ((ioflg & IO_NODELOCKED) == 0)
    239 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    240 	auio.uio_iov = &aiov;
    241 	auio.uio_iovcnt = 1;
    242 	aiov.iov_base = base;
    243 	aiov.iov_len = len;
    244 	auio.uio_resid = len;
    245 	auio.uio_offset = offset;
    246 	auio.uio_segflg = segflg;
    247 	auio.uio_rw = rw;
    248 	auio.uio_procp = p;
    249 	if (rw == UIO_READ) {
    250 		error = VOP_READ(vp, &auio, ioflg, cred);
    251 	} else {
    252 		error = VOP_WRITE(vp, &auio, ioflg, cred);
    253 	}
    254 	if (aresid)
    255 		*aresid = auio.uio_resid;
    256 	else
    257 		if (auio.uio_resid && error == 0)
    258 			error = EIO;
    259 	if ((ioflg & IO_NODELOCKED) == 0)
    260 		VOP_UNLOCK(vp, 0);
    261 	return (error);
    262 }
    263 
    264 int
    265 vn_readdir(fp, buf, segflg, count, done, p, cookies, ncookies)
    266 	struct file *fp;
    267 	char *buf;
    268 	int segflg, *done, *ncookies;
    269 	u_int count;
    270 	struct proc *p;
    271 	off_t **cookies;
    272 {
    273 	struct vnode *vp = (struct vnode *)fp->f_data;
    274 	struct iovec aiov;
    275 	struct uio auio;
    276 	int error, eofflag;
    277 
    278 unionread:
    279 	if (vp->v_type != VDIR)
    280 		return (EINVAL);
    281 	aiov.iov_base = buf;
    282 	aiov.iov_len = count;
    283 	auio.uio_iov = &aiov;
    284 	auio.uio_iovcnt = 1;
    285 	auio.uio_rw = UIO_READ;
    286 	auio.uio_segflg = segflg;
    287 	auio.uio_procp = p;
    288 	auio.uio_resid = count;
    289 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    290 	auio.uio_offset = fp->f_offset;
    291 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookies,
    292 		    ncookies);
    293 	fp->f_offset = auio.uio_offset;
    294 	VOP_UNLOCK(vp, 0);
    295 	if (error)
    296 		return (error);
    297 
    298 #ifdef UNION
    299 {
    300 	extern struct vnode *union_dircache __P((struct vnode *));
    301 
    302 	if (count == auio.uio_resid && (vp->v_op == union_vnodeop_p)) {
    303 		struct vnode *lvp;
    304 
    305 		lvp = union_dircache(vp);
    306 		if (lvp != NULLVP) {
    307 			struct vattr va;
    308 
    309 			/*
    310 			 * If the directory is opaque,
    311 			 * then don't show lower entries
    312 			 */
    313 			error = VOP_GETATTR(vp, &va, fp->f_cred, p);
    314 			if (va.va_flags & OPAQUE) {
    315 				vput(lvp);
    316 				lvp = NULL;
    317 			}
    318 		}
    319 
    320 		if (lvp != NULLVP) {
    321 			error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
    322 			if (error) {
    323 				vput(lvp);
    324 				return (error);
    325 			}
    326 			VOP_UNLOCK(lvp, 0);
    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, offset, uio, cred, flags)
    358 	struct file *fp;
    359 	off_t *offset;
    360 	struct uio *uio;
    361 	struct ucred *cred;
    362 	int flags;
    363 {
    364 	struct vnode *vp = (struct vnode *)fp->f_data;
    365 	int count, error, ioflag = 0;
    366 
    367 	VOP_LEASE(vp, uio->uio_procp, cred, LEASE_READ);
    368 	if (fp->f_flag & FNONBLOCK)
    369 		ioflag |= IO_NDELAY;
    370 	if ((fp->f_flag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC))
    371 		ioflag |= IO_SYNC;
    372 	if (fp->f_flag & FALTIO)
    373 		ioflag |= IO_ALTSEMANTICS;
    374 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    375 	uio->uio_offset = *offset;
    376 	count = uio->uio_resid;
    377 	error = VOP_READ(vp, uio, ioflag, cred);
    378 	if (flags & FOF_UPDATE_OFFSET)
    379 		*offset += count - uio->uio_resid;
    380 	VOP_UNLOCK(vp, 0);
    381 	return (error);
    382 }
    383 
    384 /*
    385  * File table vnode write routine.
    386  */
    387 int
    388 vn_write(fp, offset, uio, cred, flags)
    389 	struct file *fp;
    390 	off_t *offset;
    391 	struct uio *uio;
    392 	struct ucred *cred;
    393 	int flags;
    394 {
    395 	struct vnode *vp = (struct vnode *)fp->f_data;
    396 	int count, error, ioflag = IO_UNIT;
    397 
    398 	if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
    399 		ioflag |= IO_APPEND;
    400 	if (fp->f_flag & FNONBLOCK)
    401 		ioflag |= IO_NDELAY;
    402 	if (fp->f_flag & FFSYNC ||
    403 	    (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
    404 		ioflag |= IO_SYNC;
    405 	else if (fp->f_flag & FDSYNC)
    406 		ioflag |= IO_DSYNC;
    407 	if (fp->f_flag & FALTIO)
    408 		ioflag |= IO_ALTSEMANTICS;
    409 	VOP_LEASE(vp, uio->uio_procp, cred, LEASE_WRITE);
    410 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    411 	uio->uio_offset = *offset;
    412 	count = uio->uio_resid;
    413 	error = VOP_WRITE(vp, uio, ioflag, cred);
    414 	if (flags & FOF_UPDATE_OFFSET) {
    415 		if (ioflag & IO_APPEND)
    416 			*offset = uio->uio_offset;
    417 		else
    418 			*offset += count - uio->uio_resid;
    419 	}
    420 	VOP_UNLOCK(vp, 0);
    421 	return (error);
    422 }
    423 
    424 /*
    425  * File table vnode stat routine.
    426  */
    427 int
    428 vn_stat(vp, sb, p)
    429 	struct vnode *vp;
    430 	struct stat *sb;
    431 	struct proc *p;
    432 {
    433 	struct vattr va;
    434 	int error;
    435 	mode_t mode;
    436 
    437 	error = VOP_GETATTR(vp, &va, p->p_ucred, p);
    438 	if (error)
    439 		return (error);
    440 	/*
    441 	 * Copy from vattr table
    442 	 */
    443 	sb->st_dev = va.va_fsid;
    444 	sb->st_ino = va.va_fileid;
    445 	mode = va.va_mode;
    446 	switch (vp->v_type) {
    447 	case VREG:
    448 		mode |= S_IFREG;
    449 		break;
    450 	case VDIR:
    451 		mode |= S_IFDIR;
    452 		break;
    453 	case VBLK:
    454 		mode |= S_IFBLK;
    455 		break;
    456 	case VCHR:
    457 		mode |= S_IFCHR;
    458 		break;
    459 	case VLNK:
    460 		mode |= S_IFLNK;
    461 		break;
    462 	case VSOCK:
    463 		mode |= S_IFSOCK;
    464 		break;
    465 	case VFIFO:
    466 		mode |= S_IFIFO;
    467 		break;
    468 	default:
    469 		return (EBADF);
    470 	};
    471 	sb->st_mode = mode;
    472 	sb->st_nlink = va.va_nlink;
    473 	sb->st_uid = va.va_uid;
    474 	sb->st_gid = va.va_gid;
    475 	sb->st_rdev = va.va_rdev;
    476 	sb->st_size = va.va_size;
    477 	sb->st_atimespec = va.va_atime;
    478 	sb->st_mtimespec = va.va_mtime;
    479 	sb->st_ctimespec = va.va_ctime;
    480 	sb->st_blksize = va.va_blocksize;
    481 	sb->st_flags = va.va_flags;
    482 	sb->st_gen = 0;
    483 	sb->st_blocks = va.va_bytes / S_BLKSIZE;
    484 	return (0);
    485 }
    486 
    487 /*
    488  * File table vnode fcntl routine.
    489  */
    490 int
    491 vn_fcntl(fp, com, data, p)
    492 	struct file *fp;
    493 	u_int com;
    494 	caddr_t data;
    495 	struct proc *p;
    496 {
    497 	struct vnode *vp = ((struct vnode *)fp->f_data);
    498 	int error;
    499 
    500 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    501 	error = VOP_FCNTL(vp, com, data, fp->f_flag, p->p_ucred, p);
    502 	VOP_UNLOCK(vp, 0);
    503 	return (error);
    504 }
    505 
    506 /*
    507  * File table vnode ioctl routine.
    508  */
    509 int
    510 vn_ioctl(fp, com, data, p)
    511 	struct file *fp;
    512 	u_long com;
    513 	caddr_t data;
    514 	struct proc *p;
    515 {
    516 	struct vnode *vp = ((struct vnode *)fp->f_data);
    517 	struct vattr vattr;
    518 	int error;
    519 
    520 	switch (vp->v_type) {
    521 
    522 	case VREG:
    523 	case VDIR:
    524 		if (com == FIONREAD) {
    525 			error = VOP_GETATTR(vp, &vattr, p->p_ucred, p);
    526 			if (error)
    527 				return (error);
    528 			*(int *)data = vattr.va_size - fp->f_offset;
    529 			return (0);
    530 		}
    531 		if (com == FIONBIO || com == FIOASYNC)	/* XXX */
    532 			return (0);			/* XXX */
    533 		/* fall into ... */
    534 
    535 	default:
    536 		return (ENOTTY);
    537 
    538 	case VFIFO:
    539 	case VCHR:
    540 	case VBLK:
    541 		error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p);
    542 		if (error == 0 && com == TIOCSCTTY) {
    543 			if (p->p_session->s_ttyvp)
    544 				vrele(p->p_session->s_ttyvp);
    545 			p->p_session->s_ttyvp = vp;
    546 			VREF(vp);
    547 		}
    548 		return (error);
    549 	}
    550 }
    551 
    552 /*
    553  * File table vnode poll routine.
    554  */
    555 int
    556 vn_poll(fp, events, p)
    557 	struct file *fp;
    558 	int events;
    559 	struct proc *p;
    560 {
    561 
    562 	return (VOP_POLL(((struct vnode *)fp->f_data), events, p));
    563 }
    564 
    565 /*
    566  * Check that the vnode is still valid, and if so
    567  * acquire requested lock.
    568  */
    569 int
    570 vn_lock(vp, flags)
    571 	struct vnode *vp;
    572 	int flags;
    573 {
    574 	int error;
    575 
    576 	do {
    577 		if ((flags & LK_INTERLOCK) == 0)
    578 			simple_lock(&vp->v_interlock);
    579 		if (vp->v_flag & VXLOCK) {
    580 			vp->v_flag |= VXWANT;
    581 			simple_unlock(&vp->v_interlock);
    582 			tsleep((caddr_t)vp, PINOD, "vn_lock", 0);
    583 			error = ENOENT;
    584 		} else {
    585 			error = VOP_LOCK(vp, flags | LK_INTERLOCK);
    586 			if (error == 0 || error == EDEADLK)
    587 				return (error);
    588 		}
    589 		flags &= ~LK_INTERLOCK;
    590 	} while (flags & LK_RETRY);
    591 	return (error);
    592 }
    593 
    594 /*
    595  * File table vnode close routine.
    596  */
    597 int
    598 vn_closefile(fp, p)
    599 	struct file *fp;
    600 	struct proc *p;
    601 {
    602 
    603 	return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
    604 		fp->f_cred, p));
    605 }
    606 
    607 /*
    608  * Enable LK_CANRECURSE on lock. Return prior status.
    609  */
    610 u_int
    611 vn_setrecurse(vp)
    612 	struct vnode *vp;
    613 {
    614 	struct lock *lkp = &vp->v_lock;
    615 	u_int retval = lkp->lk_flags & LK_CANRECURSE;
    616 
    617 	lkp->lk_flags |= LK_CANRECURSE;
    618 	return retval;
    619 }
    620 
    621 /*
    622  * Called when done with locksetrecurse.
    623  */
    624 void
    625 vn_restorerecurse(vp, flags)
    626 	struct vnode *vp;
    627 	u_int flags;
    628 {
    629 	struct lock *lkp = &vp->v_lock;
    630 
    631 	lkp->lk_flags &= ~LK_CANRECURSE;
    632 	lkp->lk_flags |= flags;
    633 }
    634