Home | History | Annotate | Line # | Download | only in kern
vfs_vnops.c revision 1.41
      1 /*	$NetBSD: vfs_vnops.c,v 1.41 2000/03/30 09:27:15 augustss 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  * Vnode close call
    190  *
    191  * Note: takes an unlocked vnode, while VOP_CLOSE takes a locked node.
    192  */
    193 int
    194 vn_close(vp, flags, cred, p)
    195 	struct vnode *vp;
    196 	int flags;
    197 	struct ucred *cred;
    198 	struct proc *p;
    199 {
    200 	int error;
    201 
    202 	if (flags & FWRITE)
    203 		vp->v_writecount--;
    204 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    205 	error = VOP_CLOSE(vp, flags, cred, p);
    206 	vput(vp);
    207 	return (error);
    208 }
    209 
    210 /*
    211  * Package up an I/O request on a vnode into a uio and do it.
    212  */
    213 int
    214 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p)
    215 	enum uio_rw rw;
    216 	struct vnode *vp;
    217 	caddr_t base;
    218 	int len;
    219 	off_t offset;
    220 	enum uio_seg segflg;
    221 	int ioflg;
    222 	struct ucred *cred;
    223 	size_t *aresid;
    224 	struct proc *p;
    225 {
    226 	struct uio auio;
    227 	struct iovec aiov;
    228 	int error;
    229 
    230 	if ((ioflg & IO_NODELOCKED) == 0)
    231 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    232 	auio.uio_iov = &aiov;
    233 	auio.uio_iovcnt = 1;
    234 	aiov.iov_base = base;
    235 	aiov.iov_len = len;
    236 	auio.uio_resid = len;
    237 	auio.uio_offset = offset;
    238 	auio.uio_segflg = segflg;
    239 	auio.uio_rw = rw;
    240 	auio.uio_procp = p;
    241 	if (rw == UIO_READ) {
    242 		error = VOP_READ(vp, &auio, ioflg, cred);
    243 	} else {
    244 		error = VOP_WRITE(vp, &auio, ioflg, cred);
    245 	}
    246 	if (aresid)
    247 		*aresid = auio.uio_resid;
    248 	else
    249 		if (auio.uio_resid && error == 0)
    250 			error = EIO;
    251 	if ((ioflg & IO_NODELOCKED) == 0)
    252 		VOP_UNLOCK(vp, 0);
    253 	return (error);
    254 }
    255 
    256 int
    257 vn_readdir(fp, buf, segflg, count, done, p, cookies, ncookies)
    258 	struct file *fp;
    259 	char *buf;
    260 	int segflg, *done, *ncookies;
    261 	u_int count;
    262 	struct proc *p;
    263 	off_t **cookies;
    264 {
    265 	struct vnode *vp = (struct vnode *)fp->f_data;
    266 	struct iovec aiov;
    267 	struct uio auio;
    268 	int error, eofflag;
    269 
    270 unionread:
    271 	if (vp->v_type != VDIR)
    272 		return (EINVAL);
    273 	aiov.iov_base = buf;
    274 	aiov.iov_len = count;
    275 	auio.uio_iov = &aiov;
    276 	auio.uio_iovcnt = 1;
    277 	auio.uio_rw = UIO_READ;
    278 	auio.uio_segflg = segflg;
    279 	auio.uio_procp = p;
    280 	auio.uio_resid = count;
    281 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    282 	auio.uio_offset = fp->f_offset;
    283 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookies,
    284 		    ncookies);
    285 	fp->f_offset = auio.uio_offset;
    286 	VOP_UNLOCK(vp, 0);
    287 	if (error)
    288 		return (error);
    289 
    290 #ifdef UNION
    291 {
    292 	extern struct vnode *union_dircache __P((struct vnode *));
    293 
    294 	if (count == auio.uio_resid && (vp->v_op == union_vnodeop_p)) {
    295 		struct vnode *lvp;
    296 
    297 		lvp = union_dircache(vp);
    298 		if (lvp != NULLVP) {
    299 			struct vattr va;
    300 
    301 			/*
    302 			 * If the directory is opaque,
    303 			 * then don't show lower entries
    304 			 */
    305 			error = VOP_GETATTR(vp, &va, fp->f_cred, p);
    306 			if (va.va_flags & OPAQUE) {
    307 				vput(lvp);
    308 				lvp = NULL;
    309 			}
    310 		}
    311 
    312 		if (lvp != NULLVP) {
    313 			error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
    314 			if (error) {
    315 				vput(lvp);
    316 				return (error);
    317 			}
    318 			VOP_UNLOCK(lvp, 0);
    319 			fp->f_data = (caddr_t) lvp;
    320 			fp->f_offset = 0;
    321 			error = vn_close(vp, FREAD, fp->f_cred, p);
    322 			if (error)
    323 				return (error);
    324 			vp = lvp;
    325 			goto unionread;
    326 		}
    327 	}
    328 }
    329 #endif /* UNION */
    330 
    331 	if (count == auio.uio_resid && (vp->v_flag & VROOT) &&
    332 	    (vp->v_mount->mnt_flag & MNT_UNION)) {
    333 		struct vnode *tvp = vp;
    334 		vp = vp->v_mount->mnt_vnodecovered;
    335 		VREF(vp);
    336 		fp->f_data = (caddr_t) vp;
    337 		fp->f_offset = 0;
    338 		vrele(tvp);
    339 		goto unionread;
    340 	}
    341 	*done = count - auio.uio_resid;
    342 	return error;
    343 }
    344 
    345 /*
    346  * File table vnode read routine.
    347  */
    348 int
    349 vn_read(fp, offset, uio, cred, flags)
    350 	struct file *fp;
    351 	off_t *offset;
    352 	struct uio *uio;
    353 	struct ucred *cred;
    354 	int flags;
    355 {
    356 	struct vnode *vp = (struct vnode *)fp->f_data;
    357 	int count, error, ioflag = 0;
    358 
    359 	VOP_LEASE(vp, uio->uio_procp, cred, LEASE_READ);
    360 	if (fp->f_flag & FNONBLOCK)
    361 		ioflag |= IO_NDELAY;
    362 	if ((fp->f_flag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC))
    363 		ioflag |= IO_SYNC;
    364 	if (fp->f_flag & FALTIO)
    365 		ioflag |= IO_ALTSEMANTICS;
    366 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    367 	uio->uio_offset = *offset;
    368 	count = uio->uio_resid;
    369 	error = VOP_READ(vp, uio, ioflag, cred);
    370 	if (flags & FOF_UPDATE_OFFSET)
    371 		*offset += count - uio->uio_resid;
    372 	VOP_UNLOCK(vp, 0);
    373 	return (error);
    374 }
    375 
    376 /*
    377  * File table vnode write routine.
    378  */
    379 int
    380 vn_write(fp, offset, uio, cred, flags)
    381 	struct file *fp;
    382 	off_t *offset;
    383 	struct uio *uio;
    384 	struct ucred *cred;
    385 	int flags;
    386 {
    387 	struct vnode *vp = (struct vnode *)fp->f_data;
    388 	int count, error, ioflag = IO_UNIT;
    389 
    390 	if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
    391 		ioflag |= IO_APPEND;
    392 	if (fp->f_flag & FNONBLOCK)
    393 		ioflag |= IO_NDELAY;
    394 	if (fp->f_flag & FFSYNC ||
    395 	    (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
    396 		ioflag |= IO_SYNC;
    397 	else if (fp->f_flag & FDSYNC)
    398 		ioflag |= IO_DSYNC;
    399 	if (fp->f_flag & FALTIO)
    400 		ioflag |= IO_ALTSEMANTICS;
    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 	struct stat *sb;
    423 	struct proc *p;
    424 {
    425 	struct vattr va;
    426 	int error;
    427 	mode_t 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 fcntl routine.
    481  */
    482 int
    483 vn_fcntl(fp, com, data, p)
    484 	struct file *fp;
    485 	u_int com;
    486 	caddr_t data;
    487 	struct proc *p;
    488 {
    489 	struct vnode *vp = ((struct vnode *)fp->f_data);
    490 	int error;
    491 
    492 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    493 	error = VOP_FCNTL(vp, com, data, fp->f_flag, p->p_ucred, p);
    494 	VOP_UNLOCK(vp, 0);
    495 	return (error);
    496 }
    497 
    498 /*
    499  * File table vnode ioctl routine.
    500  */
    501 int
    502 vn_ioctl(fp, com, data, p)
    503 	struct file *fp;
    504 	u_long com;
    505 	caddr_t data;
    506 	struct proc *p;
    507 {
    508 	struct vnode *vp = ((struct vnode *)fp->f_data);
    509 	struct vattr vattr;
    510 	int error;
    511 
    512 	switch (vp->v_type) {
    513 
    514 	case VREG:
    515 	case VDIR:
    516 		if (com == FIONREAD) {
    517 			error = VOP_GETATTR(vp, &vattr, p->p_ucred, p);
    518 			if (error)
    519 				return (error);
    520 			*(int *)data = vattr.va_size - fp->f_offset;
    521 			return (0);
    522 		}
    523 		if (com == FIONBIO || com == FIOASYNC)	/* XXX */
    524 			return (0);			/* XXX */
    525 		/* fall into ... */
    526 
    527 	default:
    528 		return (ENOTTY);
    529 
    530 	case VFIFO:
    531 	case VCHR:
    532 	case VBLK:
    533 		error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p);
    534 		if (error == 0 && com == TIOCSCTTY) {
    535 			if (p->p_session->s_ttyvp)
    536 				vrele(p->p_session->s_ttyvp);
    537 			p->p_session->s_ttyvp = vp;
    538 			VREF(vp);
    539 		}
    540 		return (error);
    541 	}
    542 }
    543 
    544 /*
    545  * File table vnode poll routine.
    546  */
    547 int
    548 vn_poll(fp, events, p)
    549 	struct file *fp;
    550 	int events;
    551 	struct proc *p;
    552 {
    553 
    554 	return (VOP_POLL(((struct vnode *)fp->f_data), events, p));
    555 }
    556 
    557 /*
    558  * Check that the vnode is still valid, and if so
    559  * acquire requested lock.
    560  */
    561 int
    562 vn_lock(vp, flags)
    563 	struct vnode *vp;
    564 	int flags;
    565 {
    566 	int error;
    567 
    568 	do {
    569 		if ((flags & LK_INTERLOCK) == 0)
    570 			simple_lock(&vp->v_interlock);
    571 		if (vp->v_flag & VXLOCK) {
    572 			vp->v_flag |= VXWANT;
    573 			simple_unlock(&vp->v_interlock);
    574 			tsleep((caddr_t)vp, PINOD, "vn_lock", 0);
    575 			error = ENOENT;
    576 		} else {
    577 			error = VOP_LOCK(vp, flags | LK_INTERLOCK);
    578 			if (error == 0 || error == EDEADLK)
    579 				return (error);
    580 		}
    581 		flags &= ~LK_INTERLOCK;
    582 	} while (flags & LK_RETRY);
    583 	return (error);
    584 }
    585 
    586 /*
    587  * File table vnode close routine.
    588  */
    589 int
    590 vn_closefile(fp, p)
    591 	struct file *fp;
    592 	struct proc *p;
    593 {
    594 
    595 	return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
    596 		fp->f_cred, p));
    597 }
    598 
    599 /*
    600  * Enable LK_CANRECURSE on lock. Return prior status.
    601  */
    602 u_int
    603 vn_setrecurse(vp)
    604 	struct vnode *vp;
    605 {
    606 	struct lock *lkp = &vp->v_lock;
    607 	u_int retval = lkp->lk_flags & LK_CANRECURSE;
    608 
    609 	lkp->lk_flags |= LK_CANRECURSE;
    610 	return retval;
    611 }
    612 
    613 /*
    614  * Called when done with locksetrecurse.
    615  */
    616 void
    617 vn_restorerecurse(vp, flags)
    618 	struct vnode *vp;
    619 	u_int flags;
    620 {
    621 	struct lock *lkp = &vp->v_lock;
    622 
    623 	lkp->lk_flags &= ~LK_CANRECURSE;
    624 	lkp->lk_flags |= flags;
    625 }
    626