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