Home | History | Annotate | Line # | Download | only in kern
vfs_vnops.c revision 1.98
      1 /*	$NetBSD: vfs_vnops.c,v 1.98 2005/10/15 21:18:54 elad 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. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	@(#)vfs_vnops.c	8.14 (Berkeley) 6/15/95
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.98 2005/10/15 21:18:54 elad Exp $");
     41 
     42 #include "opt_verified_exec.h"
     43 
     44 #include "fs_union.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/malloc.h>
     54 #include <sys/mount.h>
     55 #include <sys/namei.h>
     56 #include <sys/vnode.h>
     57 #include <sys/ioctl.h>
     58 #include <sys/tty.h>
     59 #include <sys/poll.h>
     60 
     61 #include <miscfs/specfs/specdev.h>
     62 
     63 #include <uvm/uvm_extern.h>
     64 
     65 #ifdef UNION
     66 #include <fs/union/union.h>
     67 #endif
     68 
     69 #if defined(LKM) || defined(UNION)
     70 int (*vn_union_readdir_hook) (struct vnode **, struct file *, struct proc *);
     71 #endif
     72 
     73 #ifdef VERIFIED_EXEC
     74 #include <sys/verified_exec.h>
     75 #endif
     76 
     77 static int vn_read(struct file *fp, off_t *offset, struct uio *uio,
     78 	    struct ucred *cred, int flags);
     79 static int vn_write(struct file *fp, off_t *offset, struct uio *uio,
     80 	    struct ucred *cred, int flags);
     81 static int vn_closefile(struct file *fp, struct proc *p);
     82 static int vn_poll(struct file *fp, int events, struct proc *p);
     83 static int vn_fcntl(struct file *fp, u_int com, void *data, struct proc *p);
     84 static int vn_statfile(struct file *fp, struct stat *sb, struct proc *p);
     85 static int vn_ioctl(struct file *fp, u_long com, void *data, struct proc *p);
     86 
     87 const struct fileops vnops = {
     88 	vn_read, vn_write, vn_ioctl, vn_fcntl, vn_poll,
     89 	vn_statfile, vn_closefile, vn_kqfilter
     90 };
     91 
     92 /*
     93  * Common code for vnode open operations.
     94  * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
     95  */
     96 int
     97 vn_open(struct nameidata *ndp, int fmode, int cmode)
     98 {
     99 	struct vnode *vp;
    100 	struct mount *mp;
    101 	struct proc *p = ndp->ni_cnd.cn_proc;
    102 	struct ucred *cred = p->p_ucred;
    103 	struct vattr va;
    104 	int error;
    105 #ifdef VERIFIED_EXEC
    106 	struct veriexec_hash_entry *vhe = NULL;
    107 	char pathbuf[MAXPATHLEN];
    108 	size_t pathlen;
    109 	int (*copyfun)(const void *, void *, size_t, size_t *) =
    110 	    ndp->ni_segflg == UIO_SYSSPACE ? copystr : copyinstr;
    111 #endif /* VERIFIED_EXEC */
    112 
    113 #ifdef VERIFIED_EXEC
    114 	error = (*copyfun)(ndp->ni_dirp, pathbuf, sizeof(pathbuf), &pathlen);
    115 	if (error) {
    116 		if (veriexec_verbose >= 1)
    117 			printf("veriexec: Can't copy path. (error=%d)\n",
    118 			    error);
    119 
    120 		return (error);
    121 	}
    122 #endif /* VERIFIED_EXEC */
    123 
    124 restart:
    125 	if (fmode & O_CREAT) {
    126 		ndp->ni_cnd.cn_nameiop = CREATE;
    127 		ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
    128 		if ((fmode & O_EXCL) == 0 &&
    129 		    ((fmode & O_NOFOLLOW) == 0))
    130 			ndp->ni_cnd.cn_flags |= FOLLOW;
    131 		if ((error = namei(ndp)) != 0)
    132 			return (error);
    133 		if (ndp->ni_vp == NULL) {
    134 #ifdef VERIFIED_EXEC
    135 			/* Lockdown mode: Prevent creation of new files. */
    136 			if (veriexec_strict >= 3) {
    137 				VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
    138 
    139 				printf("Veriexec: vn_open: Preventing "
    140 				       "new file creation in %s.\n",
    141 				       pathbuf);
    142 
    143 				vp = ndp->ni_dvp;
    144 				error = EPERM;
    145 				goto bad;
    146 			}
    147 #endif /* VERIFIED_EXEC */
    148 
    149 			VATTR_NULL(&va);
    150 			va.va_type = VREG;
    151 			va.va_mode = cmode;
    152 			if (fmode & O_EXCL)
    153 				 va.va_vaflags |= VA_EXCLUSIVE;
    154 			if (vn_start_write(ndp->ni_dvp, &mp, V_NOWAIT) != 0) {
    155 				VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
    156 				vput(ndp->ni_dvp);
    157 				if ((error = vn_start_write(NULL, &mp,
    158 				    V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
    159 					return (error);
    160 				goto restart;
    161 			}
    162 			VOP_LEASE(ndp->ni_dvp, p, cred, LEASE_WRITE);
    163 			error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
    164 					   &ndp->ni_cnd, &va);
    165 			vn_finished_write(mp, 0);
    166 			if (error)
    167 				return (error);
    168 			fmode &= ~O_TRUNC;
    169 			vp = ndp->ni_vp;
    170 		} else {
    171 			VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
    172 			if (ndp->ni_dvp == ndp->ni_vp)
    173 				vrele(ndp->ni_dvp);
    174 			else
    175 				vput(ndp->ni_dvp);
    176 			ndp->ni_dvp = NULL;
    177 			vp = ndp->ni_vp;
    178 			if (fmode & O_EXCL) {
    179 				error = EEXIST;
    180 				goto bad;
    181 			}
    182 			fmode &= ~O_CREAT;
    183 		}
    184 	} else {
    185 		ndp->ni_cnd.cn_nameiop = LOOKUP;
    186 		ndp->ni_cnd.cn_flags = LOCKLEAF;
    187 		if ((fmode & O_NOFOLLOW) == 0)
    188 			ndp->ni_cnd.cn_flags |= FOLLOW;
    189 		if ((error = namei(ndp)) != 0)
    190 			return (error);
    191 		vp = ndp->ni_vp;
    192 	}
    193 	if (vp->v_type == VSOCK) {
    194 		error = EOPNOTSUPP;
    195 		goto bad;
    196 	}
    197 	if (ndp->ni_vp->v_type == VLNK) {
    198 		error = EFTYPE;
    199 		goto bad;
    200 	}
    201 
    202 #ifdef VERIFIED_EXEC
    203 	if ((error = VOP_GETATTR(vp, &va, cred, p)) != 0)
    204 		goto bad;
    205 #endif
    206 
    207 	if ((fmode & O_CREAT) == 0) {
    208 #ifdef VERIFIED_EXEC
    209 		if ((error = veriexec_verify(p, vp, &va, pathbuf,
    210 					     VERIEXEC_FILE, &vhe)) != 0)
    211 			goto bad;
    212 #endif
    213 
    214 		if (fmode & FREAD) {
    215 			if ((error = VOP_ACCESS(vp, VREAD, cred, p)) != 0)
    216 				goto bad;
    217 		}
    218 
    219 		if (fmode & (FWRITE | O_TRUNC)) {
    220 			if (vp->v_type == VDIR) {
    221 				error = EISDIR;
    222 				goto bad;
    223 			}
    224 			if ((error = vn_writechk(vp)) != 0 ||
    225 			    (error = VOP_ACCESS(vp, VWRITE, cred, p)) != 0)
    226 				goto bad;
    227 #ifdef VERIFIED_EXEC
    228 			if (vhe != NULL) {
    229 				veriexec_report("Write access request.",
    230 						pathbuf, &va, p,
    231 						REPORT_NOVERBOSE,
    232 						REPORT_ALARM,
    233 						REPORT_NOPANIC);
    234 
    235 				/* IPS mode: Deny writing to monitored files. */
    236 				if (veriexec_strict >= 2) {
    237 					error = EPERM;
    238 					goto bad;
    239 				} else {
    240 					vhe->status = FINGERPRINT_NOTEVAL;
    241 				}
    242 			}
    243 #endif
    244 		}
    245 	}
    246 
    247 	if (fmode & O_TRUNC) {
    248 		VOP_UNLOCK(vp, 0);			/* XXX */
    249 		if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) {
    250 			vput(vp);
    251 			return (error);
    252 		}
    253 		VOP_LEASE(vp, p, cred, LEASE_WRITE);
    254 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);	/* XXX */
    255 		VATTR_NULL(&va);
    256 		va.va_size = 0;
    257 		error = VOP_SETATTR(vp, &va, cred, p);
    258 		vn_finished_write(mp, 0);
    259 		if (error != 0)
    260 			goto bad;
    261 	}
    262 	if ((error = VOP_OPEN(vp, fmode, cred, p)) != 0)
    263 		goto bad;
    264 	if (vp->v_type == VREG &&
    265 	    uvn_attach(vp, fmode & FWRITE ? VM_PROT_WRITE : 0) == NULL) {
    266 		error = EIO;
    267 		goto bad;
    268 	}
    269 	if (fmode & FWRITE)
    270 		vp->v_writecount++;
    271 
    272 	return (0);
    273 bad:
    274 	vput(vp);
    275 	return (error);
    276 }
    277 
    278 /*
    279  * Check for write permissions on the specified vnode.
    280  * Prototype text segments cannot be written.
    281  */
    282 int
    283 vn_writechk(struct vnode *vp)
    284 {
    285 
    286 	/*
    287 	 * If the vnode is in use as a process's text,
    288 	 * we can't allow writing.
    289 	 */
    290 	if (vp->v_flag & VTEXT)
    291 		return (ETXTBSY);
    292 	return (0);
    293 }
    294 
    295 /*
    296  * Mark a vnode as having executable mappings.
    297  */
    298 void
    299 vn_markexec(struct vnode *vp)
    300 {
    301 	if ((vp->v_flag & VEXECMAP) == 0) {
    302 		uvmexp.filepages -= vp->v_uobj.uo_npages;
    303 		uvmexp.execpages += vp->v_uobj.uo_npages;
    304 	}
    305 	vp->v_flag |= VEXECMAP;
    306 }
    307 
    308 /*
    309  * Mark a vnode as being the text of a process.
    310  * Fail if the vnode is currently writable.
    311  */
    312 int
    313 vn_marktext(struct vnode *vp)
    314 {
    315 
    316 	if (vp->v_writecount != 0) {
    317 		KASSERT((vp->v_flag & VTEXT) == 0);
    318 		return (ETXTBSY);
    319 	}
    320 	vp->v_flag |= VTEXT;
    321 	vn_markexec(vp);
    322 	return (0);
    323 }
    324 
    325 /*
    326  * Vnode close call
    327  *
    328  * Note: takes an unlocked vnode, while VOP_CLOSE takes a locked node.
    329  */
    330 int
    331 vn_close(struct vnode *vp, int flags, struct ucred *cred, struct proc *p)
    332 {
    333 	int error;
    334 
    335 	if (flags & FWRITE)
    336 		vp->v_writecount--;
    337 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    338 	error = VOP_CLOSE(vp, flags, cred, p);
    339 	vput(vp);
    340 	return (error);
    341 }
    342 
    343 /*
    344  * Package up an I/O request on a vnode into a uio and do it.
    345  */
    346 int
    347 vn_rdwr(enum uio_rw rw, struct vnode *vp, caddr_t base, int len, off_t offset,
    348     enum uio_seg segflg, int ioflg, struct ucred *cred, size_t *aresid,
    349     struct proc *p)
    350 {
    351 	struct uio auio;
    352 	struct iovec aiov;
    353 	struct mount *mp;
    354 	int error;
    355 
    356 	if ((ioflg & IO_NODELOCKED) == 0) {
    357 		if (rw == UIO_READ) {
    358 			vn_lock(vp, LK_SHARED | LK_RETRY);
    359 		} else /* UIO_WRITE */ {
    360 			if (vp->v_type != VCHR &&
    361 			    (error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH))
    362 			    != 0)
    363 				return (error);
    364 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    365 		}
    366 	}
    367 	auio.uio_iov = &aiov;
    368 	auio.uio_iovcnt = 1;
    369 	aiov.iov_base = base;
    370 	aiov.iov_len = len;
    371 	auio.uio_resid = len;
    372 	auio.uio_offset = offset;
    373 	auio.uio_segflg = segflg;
    374 	auio.uio_rw = rw;
    375 	auio.uio_procp = p;
    376 	if (rw == UIO_READ) {
    377 		error = VOP_READ(vp, &auio, ioflg, cred);
    378 	} else {
    379 		error = VOP_WRITE(vp, &auio, ioflg, cred);
    380 	}
    381 	if (aresid)
    382 		*aresid = auio.uio_resid;
    383 	else
    384 		if (auio.uio_resid && error == 0)
    385 			error = EIO;
    386 	if ((ioflg & IO_NODELOCKED) == 0) {
    387 		if (rw == UIO_WRITE)
    388 			vn_finished_write(mp, 0);
    389 		VOP_UNLOCK(vp, 0);
    390 	}
    391 	return (error);
    392 }
    393 
    394 int
    395 vn_readdir(struct file *fp, char *bf, int segflg, u_int count, int *done,
    396     struct proc *p, off_t **cookies, int *ncookies)
    397 {
    398 	struct vnode *vp = (struct vnode *)fp->f_data;
    399 	struct iovec aiov;
    400 	struct uio auio;
    401 	int error, eofflag;
    402 
    403 unionread:
    404 	if (vp->v_type != VDIR)
    405 		return (EINVAL);
    406 	aiov.iov_base = bf;
    407 	aiov.iov_len = count;
    408 	auio.uio_iov = &aiov;
    409 	auio.uio_iovcnt = 1;
    410 	auio.uio_rw = UIO_READ;
    411 	auio.uio_segflg = segflg;
    412 	auio.uio_procp = p;
    413 	auio.uio_resid = count;
    414 	vn_lock(vp, LK_SHARED | LK_RETRY);
    415 	auio.uio_offset = fp->f_offset;
    416 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookies,
    417 		    ncookies);
    418 	fp->f_offset = auio.uio_offset;
    419 	VOP_UNLOCK(vp, 0);
    420 	if (error)
    421 		return (error);
    422 
    423 #if defined(UNION) || defined(LKM)
    424 	if (count == auio.uio_resid && vn_union_readdir_hook) {
    425 		struct vnode *ovp = vp;
    426 
    427 		error = (*vn_union_readdir_hook)(&vp, fp, p);
    428 		if (error)
    429 			return (error);
    430 		if (vp != ovp)
    431 			goto unionread;
    432 	}
    433 #endif /* UNION || LKM */
    434 
    435 	if (count == auio.uio_resid && (vp->v_flag & VROOT) &&
    436 	    (vp->v_mount->mnt_flag & MNT_UNION)) {
    437 		struct vnode *tvp = vp;
    438 		vp = vp->v_mount->mnt_vnodecovered;
    439 		VREF(vp);
    440 		fp->f_data = vp;
    441 		fp->f_offset = 0;
    442 		vrele(tvp);
    443 		goto unionread;
    444 	}
    445 	*done = count - auio.uio_resid;
    446 	return error;
    447 }
    448 
    449 /*
    450  * File table vnode read routine.
    451  */
    452 static int
    453 vn_read(struct file *fp, off_t *offset, struct uio *uio, struct ucred *cred,
    454     int flags)
    455 {
    456 	struct vnode *vp = (struct vnode *)fp->f_data;
    457 	int count, error, ioflag = 0;
    458 
    459 	VOP_LEASE(vp, uio->uio_procp, cred, LEASE_READ);
    460 	if (fp->f_flag & FNONBLOCK)
    461 		ioflag |= IO_NDELAY;
    462 	if ((fp->f_flag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC))
    463 		ioflag |= IO_SYNC;
    464 	if (fp->f_flag & FALTIO)
    465 		ioflag |= IO_ALTSEMANTICS;
    466 	vn_lock(vp, LK_SHARED | LK_RETRY);
    467 	uio->uio_offset = *offset;
    468 	count = uio->uio_resid;
    469 	error = VOP_READ(vp, uio, ioflag, cred);
    470 	if (flags & FOF_UPDATE_OFFSET)
    471 		*offset += count - uio->uio_resid;
    472 	VOP_UNLOCK(vp, 0);
    473 	return (error);
    474 }
    475 
    476 /*
    477  * File table vnode write routine.
    478  */
    479 static int
    480 vn_write(struct file *fp, off_t *offset, struct uio *uio, struct ucred *cred,
    481     int flags)
    482 {
    483 	struct vnode *vp = (struct vnode *)fp->f_data;
    484 	struct mount *mp;
    485 	int count, error, ioflag = IO_UNIT;
    486 
    487 	if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
    488 		ioflag |= IO_APPEND;
    489 	if (fp->f_flag & FNONBLOCK)
    490 		ioflag |= IO_NDELAY;
    491 	if (fp->f_flag & FFSYNC ||
    492 	    (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
    493 		ioflag |= IO_SYNC;
    494 	else if (fp->f_flag & FDSYNC)
    495 		ioflag |= IO_DSYNC;
    496 	if (fp->f_flag & FALTIO)
    497 		ioflag |= IO_ALTSEMANTICS;
    498 	mp = NULL;
    499 	if (vp->v_type != VCHR &&
    500 	    (error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
    501 		return (error);
    502 	VOP_LEASE(vp, uio->uio_procp, cred, LEASE_WRITE);
    503 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    504 	uio->uio_offset = *offset;
    505 	count = uio->uio_resid;
    506 	error = VOP_WRITE(vp, uio, ioflag, cred);
    507 	if (flags & FOF_UPDATE_OFFSET) {
    508 		if (ioflag & IO_APPEND)
    509 			*offset = uio->uio_offset;
    510 		else
    511 			*offset += count - uio->uio_resid;
    512 	}
    513 	VOP_UNLOCK(vp, 0);
    514 	vn_finished_write(mp, 0);
    515 	return (error);
    516 }
    517 
    518 /*
    519  * File table vnode stat routine.
    520  */
    521 static int
    522 vn_statfile(struct file *fp, struct stat *sb, struct proc *p)
    523 {
    524 	struct vnode *vp = (struct vnode *)fp->f_data;
    525 
    526 	return vn_stat(vp, sb, p);
    527 }
    528 
    529 int
    530 vn_stat(struct vnode *vp, struct stat *sb, struct proc *p)
    531 {
    532 	struct vattr va;
    533 	int error;
    534 	mode_t mode;
    535 
    536 	error = VOP_GETATTR(vp, &va, p->p_ucred, p);
    537 	if (error)
    538 		return (error);
    539 	/*
    540 	 * Copy from vattr table
    541 	 */
    542 	sb->st_dev = va.va_fsid;
    543 	sb->st_ino = va.va_fileid;
    544 	mode = va.va_mode;
    545 	switch (vp->v_type) {
    546 	case VREG:
    547 		mode |= S_IFREG;
    548 		break;
    549 	case VDIR:
    550 		mode |= S_IFDIR;
    551 		break;
    552 	case VBLK:
    553 		mode |= S_IFBLK;
    554 		break;
    555 	case VCHR:
    556 		mode |= S_IFCHR;
    557 		break;
    558 	case VLNK:
    559 		mode |= S_IFLNK;
    560 		break;
    561 	case VSOCK:
    562 		mode |= S_IFSOCK;
    563 		break;
    564 	case VFIFO:
    565 		mode |= S_IFIFO;
    566 		break;
    567 	default:
    568 		return (EBADF);
    569 	};
    570 	sb->st_mode = mode;
    571 	sb->st_nlink = va.va_nlink;
    572 	sb->st_uid = va.va_uid;
    573 	sb->st_gid = va.va_gid;
    574 	sb->st_rdev = va.va_rdev;
    575 	sb->st_size = va.va_size;
    576 	sb->st_atimespec = va.va_atime;
    577 	sb->st_mtimespec = va.va_mtime;
    578 	sb->st_ctimespec = va.va_ctime;
    579 	sb->st_birthtimespec = va.va_birthtime;
    580 	sb->st_blksize = va.va_blocksize;
    581 	sb->st_flags = va.va_flags;
    582 	sb->st_gen = 0;
    583 	sb->st_blocks = va.va_bytes / S_BLKSIZE;
    584 	return (0);
    585 }
    586 
    587 /*
    588  * File table vnode fcntl routine.
    589  */
    590 static int
    591 vn_fcntl(struct file *fp, u_int com, void *data, struct proc *p)
    592 {
    593 	struct vnode *vp = ((struct vnode *)fp->f_data);
    594 	int error;
    595 
    596 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    597 	error = VOP_FCNTL(vp, com, data, fp->f_flag, p->p_ucred, p);
    598 	VOP_UNLOCK(vp, 0);
    599 	return (error);
    600 }
    601 
    602 /*
    603  * File table vnode ioctl routine.
    604  */
    605 static int
    606 vn_ioctl(struct file *fp, u_long com, void *data, struct proc *p)
    607 {
    608 	struct vnode *vp = ((struct vnode *)fp->f_data);
    609 	struct vattr vattr;
    610 	int error;
    611 
    612 	switch (vp->v_type) {
    613 
    614 	case VREG:
    615 	case VDIR:
    616 		if (com == FIONREAD) {
    617 			error = VOP_GETATTR(vp, &vattr, p->p_ucred, p);
    618 			if (error)
    619 				return (error);
    620 			*(int *)data = vattr.va_size - fp->f_offset;
    621 			return (0);
    622 		}
    623 		if ((com == FIONWRITE) || (com == FIONSPACE)) {
    624 			/*
    625 			 * Files don't have send queues, so there never
    626 			 * are any bytes in them, nor is there any
    627 			 * open space in them.
    628 			 */
    629 			*(int *)data = 0;
    630 			return (0);
    631 		}
    632 		if (com == FIOGETBMAP) {
    633 			daddr_t *block;
    634 
    635 			if (*(daddr_t *)data < 0)
    636 				return (EINVAL);
    637 			block = (daddr_t *)data;
    638 			return (VOP_BMAP(vp, *block, NULL, block, NULL));
    639 		}
    640 		if (com == OFIOGETBMAP) {
    641 			daddr_t ibn, obn;
    642 
    643 			if (*(int32_t *)data < 0)
    644 				return (EINVAL);
    645 			ibn = (daddr_t)*(int32_t *)data;
    646 			error = VOP_BMAP(vp, ibn, NULL, &obn, NULL);
    647 			*(int32_t *)data = (int32_t)obn;
    648 			return error;
    649 		}
    650 		if (com == FIONBIO || com == FIOASYNC)	/* XXX */
    651 			return (0);			/* XXX */
    652 		/* fall into ... */
    653 	case VFIFO:
    654 	case VCHR:
    655 	case VBLK:
    656 		error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p);
    657 		if (error == 0 && com == TIOCSCTTY) {
    658 			if (p->p_session->s_ttyvp)
    659 				vrele(p->p_session->s_ttyvp);
    660 			p->p_session->s_ttyvp = vp;
    661 			VREF(vp);
    662 		}
    663 		return (error);
    664 
    665 	default:
    666 		return (EPASSTHROUGH);
    667 	}
    668 }
    669 
    670 /*
    671  * File table vnode poll routine.
    672  */
    673 static int
    674 vn_poll(struct file *fp, int events, struct proc *p)
    675 {
    676 
    677 	return (VOP_POLL(((struct vnode *)fp->f_data), events, p));
    678 }
    679 
    680 /*
    681  * File table vnode kqfilter routine.
    682  */
    683 int
    684 vn_kqfilter(struct file *fp, struct knote *kn)
    685 {
    686 
    687 	return (VOP_KQFILTER((struct vnode *)fp->f_data, kn));
    688 }
    689 
    690 /*
    691  * Check that the vnode is still valid, and if so
    692  * acquire requested lock.
    693  */
    694 int
    695 vn_lock(struct vnode *vp, int flags)
    696 {
    697 	int error;
    698 
    699 #if 0
    700 	KASSERT(vp->v_usecount > 0 || (flags & LK_INTERLOCK) != 0
    701 	    || (vp->v_flag & VONWORKLST) != 0);
    702 #endif
    703 
    704 	do {
    705 		if ((flags & LK_INTERLOCK) == 0)
    706 			simple_lock(&vp->v_interlock);
    707 		if (vp->v_flag & VXLOCK) {
    708 			if (flags & LK_NOWAIT) {
    709 				simple_unlock(&vp->v_interlock);
    710 				return EBUSY;
    711 			}
    712 			vp->v_flag |= VXWANT;
    713 			ltsleep(vp, PINOD | PNORELOCK,
    714 			    "vn_lock", 0, &vp->v_interlock);
    715 			error = ENOENT;
    716 		} else {
    717 			error = VOP_LOCK(vp,
    718 			    (flags & ~LK_RETRY) | LK_INTERLOCK);
    719 			if (error == 0 || error == EDEADLK || error == EBUSY)
    720 				return (error);
    721 		}
    722 		flags &= ~LK_INTERLOCK;
    723 	} while (flags & LK_RETRY);
    724 	return (error);
    725 }
    726 
    727 /*
    728  * File table vnode close routine.
    729  */
    730 static int
    731 vn_closefile(struct file *fp, struct proc *p)
    732 {
    733 
    734 	return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
    735 		fp->f_cred, p));
    736 }
    737 
    738 /*
    739  * Enable LK_CANRECURSE on lock. Return prior status.
    740  */
    741 u_int
    742 vn_setrecurse(struct vnode *vp)
    743 {
    744 	struct lock *lkp = &vp->v_lock;
    745 	u_int retval = lkp->lk_flags & LK_CANRECURSE;
    746 
    747 	lkp->lk_flags |= LK_CANRECURSE;
    748 	return retval;
    749 }
    750 
    751 /*
    752  * Called when done with locksetrecurse.
    753  */
    754 void
    755 vn_restorerecurse(struct vnode *vp, u_int flags)
    756 {
    757 	struct lock *lkp = &vp->v_lock;
    758 
    759 	lkp->lk_flags &= ~LK_CANRECURSE;
    760 	lkp->lk_flags |= flags;
    761 }
    762 
    763 int
    764 vn_cow_establish(struct vnode *vp,
    765     int (*func)(void *, struct buf *), void *cookie)
    766 {
    767 	int s;
    768 	struct spec_cow_entry *e;
    769 
    770 	MALLOC(e, struct spec_cow_entry *, sizeof(struct spec_cow_entry),
    771 	    M_DEVBUF, M_WAITOK);
    772 	e->ce_func = func;
    773 	e->ce_cookie = cookie;
    774 
    775 	SPEC_COW_LOCK(vp->v_specinfo, s);
    776 	vp->v_spec_cow_req++;
    777 	while (vp->v_spec_cow_count > 0)
    778 		ltsleep(&vp->v_spec_cow_req, PRIBIO, "cowlist", 0,
    779 		    &vp->v_spec_cow_slock);
    780 
    781 	SLIST_INSERT_HEAD(&vp->v_spec_cow_head, e, ce_list);
    782 
    783 	vp->v_spec_cow_req--;
    784 	if (vp->v_spec_cow_req == 0)
    785 		wakeup(&vp->v_spec_cow_req);
    786 	SPEC_COW_UNLOCK(vp->v_specinfo, s);
    787 
    788 	return 0;
    789 }
    790 
    791 int
    792 vn_cow_disestablish(struct vnode *vp,
    793     int (*func)(void *, struct buf *), void *cookie)
    794 {
    795 	int s;
    796 	struct spec_cow_entry *e;
    797 
    798 	SPEC_COW_LOCK(vp->v_specinfo, s);
    799 	vp->v_spec_cow_req++;
    800 	while (vp->v_spec_cow_count > 0)
    801 		ltsleep(&vp->v_spec_cow_req, PRIBIO, "cowlist", 0,
    802 		    &vp->v_spec_cow_slock);
    803 
    804 	SLIST_FOREACH(e, &vp->v_spec_cow_head, ce_list)
    805 		if (e->ce_func == func && e->ce_cookie == cookie) {
    806 			SLIST_REMOVE(&vp->v_spec_cow_head, e,
    807 			    spec_cow_entry, ce_list);
    808 			FREE(e, M_DEVBUF);
    809 			break;
    810 		}
    811 
    812 	vp->v_spec_cow_req--;
    813 	if (vp->v_spec_cow_req == 0)
    814 		wakeup(&vp->v_spec_cow_req);
    815 	SPEC_COW_UNLOCK(vp->v_specinfo, s);
    816 
    817 	return e ? 0 : EINVAL;
    818 }
    819 
    820 /*
    821  * Simplified in-kernel wrapper calls for extended attribute access.
    822  * Both calls pass in a NULL credential, authorizing a "kernel" access.
    823  * Set IO_NODELOCKED in ioflg if the vnode is already locked.
    824  */
    825 int
    826 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
    827     const char *attrname, size_t *buflen, void *bf, struct proc *p)
    828 {
    829 	struct uio auio;
    830 	struct iovec aiov;
    831 	int error;
    832 
    833 	aiov.iov_len = *buflen;
    834 	aiov.iov_base = bf;
    835 
    836 	auio.uio_iov = &aiov;
    837 	auio.uio_iovcnt = 1;
    838 	auio.uio_rw = UIO_READ;
    839 	auio.uio_segflg = UIO_SYSSPACE;
    840 	auio.uio_procp = p;
    841 	auio.uio_offset = 0;
    842 	auio.uio_resid = *buflen;
    843 
    844 	if ((ioflg & IO_NODELOCKED) == 0)
    845 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    846 
    847 	error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL,
    848 	    p);
    849 
    850 	if ((ioflg & IO_NODELOCKED) == 0)
    851 		VOP_UNLOCK(vp, 0);
    852 
    853 	if (error == 0)
    854 		*buflen = *buflen - auio.uio_resid;
    855 
    856 	return (error);
    857 }
    858 
    859 /*
    860  * XXX Failure mode if partially written?
    861  */
    862 int
    863 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
    864     const char *attrname, size_t buflen, const void *bf, struct proc *p)
    865 {
    866 	struct uio auio;
    867 	struct iovec aiov;
    868 	struct mount *mp;
    869 	int error;
    870 
    871 	aiov.iov_len = buflen;
    872 	aiov.iov_base = __UNCONST(bf);		/* XXXUNCONST kills const */
    873 
    874 	auio.uio_iov = &aiov;
    875 	auio.uio_iovcnt = 1;
    876 	auio.uio_rw = UIO_WRITE;
    877 	auio.uio_segflg = UIO_SYSSPACE;
    878 	auio.uio_procp = p;
    879 	auio.uio_offset = 0;
    880 	auio.uio_resid = buflen;
    881 
    882 	if ((ioflg & IO_NODELOCKED) == 0) {
    883 		if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
    884 			return (error);
    885 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    886 	}
    887 
    888 	error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, p);
    889 
    890 	if ((ioflg & IO_NODELOCKED) == 0) {
    891 		vn_finished_write(mp, 0);
    892 		VOP_UNLOCK(vp, 0);
    893 	}
    894 
    895 	return (error);
    896 }
    897 
    898 int
    899 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
    900     const char *attrname, struct proc *p)
    901 {
    902 	struct mount *mp;
    903 	int error;
    904 
    905 	if ((ioflg & IO_NODELOCKED) == 0) {
    906 		if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
    907 			return (error);
    908 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    909 	}
    910 
    911 	error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL, p);
    912 	if (error == EOPNOTSUPP)
    913 		error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
    914 		    NULL, p);
    915 
    916 	if ((ioflg & IO_NODELOCKED) == 0) {
    917 		vn_finished_write(mp, 0);
    918 		VOP_UNLOCK(vp, 0);
    919 	}
    920 
    921 	return (error);
    922 }
    923 
    924 /*
    925  * Preparing to start a filesystem write operation. If the operation is
    926  * permitted, then we bump the count of operations in progress and
    927  * proceed. If a suspend request is in progress, we wait until the
    928  * suspension is over, and then proceed.
    929  * V_PCATCH    adds PCATCH to the tsleep flags.
    930  * V_WAIT      waits until suspension is over. Otherwise returns EWOULDBLOCK.
    931  * V_SLEEPONLY wait, but do not bump the operations count.
    932  * V_LOWER     this is a lower level operation. No further vnodes should be
    933  *             locked. Otherwise it is a upper level operation. No vnodes
    934  *             should be locked.
    935  */
    936 int
    937 vn_start_write(struct vnode *vp, struct mount **mpp, int flags)
    938 {
    939 	struct mount *mp;
    940 	int error, mask, prio;
    941 
    942 	/*
    943 	 * If a vnode is provided, get and return the mount point that
    944 	 * to which it will write.
    945 	 */
    946 	if (vp != NULL) {
    947 		*mpp = vp->v_mount;
    948 	}
    949 	if ((mp = *mpp) == NULL)
    950 		return (0);
    951 	mp = mp->mnt_leaf;
    952 	/*
    953 	 * Check on status of suspension.
    954 	 */
    955 	prio = PUSER - 1;
    956 	if (flags & V_PCATCH)
    957 		prio |= PCATCH;
    958 
    959 	if ((flags & V_LOWER) == 0)
    960 		mask = IMNT_SUSPEND;
    961 	else
    962 		mask = IMNT_SUSPENDLOW;
    963 
    964 	while ((mp->mnt_iflag & mask) != 0) {
    965 		if ((flags & V_WAIT) == 0)
    966 			return (EWOULDBLOCK);
    967 		error = tsleep(&mp->mnt_flag, prio, "suspfs", 0);
    968 		if (error)
    969 			return (error);
    970 	}
    971 	if (flags & V_SLEEPONLY)
    972 		return (0);
    973 	simple_lock(&mp->mnt_slock);
    974 	if ((flags & V_LOWER) == 0)
    975 		mp->mnt_writeopcountupper++;
    976 	else
    977 		mp->mnt_writeopcountlower++;
    978 	simple_unlock(&mp->mnt_slock);
    979 	return (0);
    980 }
    981 
    982 /*
    983  * Filesystem write operation has completed. If we are suspending and this
    984  * operation is the last one, notify the suspender that the suspension is
    985  * now in effect.
    986  */
    987 void
    988 vn_finished_write(struct mount *mp, int flags)
    989 {
    990 	if (mp == NULL)
    991 		return;
    992 	mp = mp->mnt_leaf;
    993 	simple_lock(&mp->mnt_slock);
    994 	if ((flags & V_LOWER) == 0) {
    995 		mp->mnt_writeopcountupper--;
    996 		if (mp->mnt_writeopcountupper < 0)
    997 			printf("vn_finished_write: neg cnt upper=%d\n",
    998 			       mp->mnt_writeopcountupper);
    999 		if ((mp->mnt_iflag & IMNT_SUSPEND) != 0 &&
   1000 		    mp->mnt_writeopcountupper <= 0)
   1001 			wakeup(&mp->mnt_writeopcountupper);
   1002 	} else {
   1003 		mp->mnt_writeopcountlower--;
   1004 		if (mp->mnt_writeopcountlower < 0)
   1005 			printf("vn_finished_write: neg cnt lower=%d\n",
   1006 			       mp->mnt_writeopcountlower);
   1007 		if ((mp->mnt_iflag & IMNT_SUSPENDLOW) != 0 &&
   1008 		    mp->mnt_writeopcountupper <= 0)
   1009 			wakeup(&mp->mnt_writeopcountlower);
   1010 	}
   1011 	simple_unlock(&mp->mnt_slock);
   1012 }
   1013