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