Home | History | Annotate | Line # | Download | only in kern
sys_generic.c revision 1.122
      1  1.122        ad /*	$NetBSD: sys_generic.c,v 1.122 2009/05/17 10:08:38 ad Exp $	*/
      2  1.103        ad 
      3  1.103        ad /*-
      4  1.122        ad  * Copyright (c) 2007, 2008, 2009 The NetBSD Foundation, Inc.
      5  1.103        ad  * All rights reserved.
      6  1.103        ad  *
      7  1.103        ad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.103        ad  * by Andrew Doran.
      9  1.103        ad  *
     10  1.103        ad  * Redistribution and use in source and binary forms, with or without
     11  1.103        ad  * modification, are permitted provided that the following conditions
     12  1.103        ad  * are met:
     13  1.103        ad  * 1. Redistributions of source code must retain the above copyright
     14  1.103        ad  *    notice, this list of conditions and the following disclaimer.
     15  1.103        ad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.103        ad  *    notice, this list of conditions and the following disclaimer in the
     17  1.103        ad  *    documentation and/or other materials provided with the distribution.
     18  1.103        ad  *
     19  1.103        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.103        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.103        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.103        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.103        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.103        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.103        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.103        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.103        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.103        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.103        ad  * POSSIBILITY OF SUCH DAMAGE.
     30  1.103        ad  */
     31   1.15       cgd 
     32   1.15       cgd /*
     33   1.15       cgd  * Copyright (c) 1982, 1986, 1989, 1993
     34   1.15       cgd  *	The Regents of the University of California.  All rights reserved.
     35   1.15       cgd  * (c) UNIX System Laboratories, Inc.
     36   1.15       cgd  * All or some portions of this file are derived from material licensed
     37   1.15       cgd  * to the University of California by American Telephone and Telegraph
     38   1.15       cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     39   1.15       cgd  * the permission of UNIX System Laboratories, Inc.
     40   1.15       cgd  *
     41   1.15       cgd  * Redistribution and use in source and binary forms, with or without
     42   1.15       cgd  * modification, are permitted provided that the following conditions
     43   1.15       cgd  * are met:
     44   1.15       cgd  * 1. Redistributions of source code must retain the above copyright
     45   1.15       cgd  *    notice, this list of conditions and the following disclaimer.
     46   1.15       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     47   1.15       cgd  *    notice, this list of conditions and the following disclaimer in the
     48   1.15       cgd  *    documentation and/or other materials provided with the distribution.
     49   1.77       agc  * 3. Neither the name of the University nor the names of its contributors
     50   1.15       cgd  *    may be used to endorse or promote products derived from this software
     51   1.15       cgd  *    without specific prior written permission.
     52   1.15       cgd  *
     53   1.15       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     54   1.15       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     55   1.15       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     56   1.15       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     57   1.15       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     58   1.15       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     59   1.15       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60   1.15       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61   1.15       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62   1.15       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63   1.15       cgd  * SUCH DAMAGE.
     64   1.15       cgd  *
     65   1.36      fvdl  *	@(#)sys_generic.c	8.9 (Berkeley) 2/14/95
     66   1.15       cgd  */
     67   1.59     lukem 
     68  1.103        ad /*
     69  1.103        ad  * System calls relating to files.
     70  1.103        ad  */
     71  1.103        ad 
     72   1.59     lukem #include <sys/cdefs.h>
     73  1.122        ad __KERNEL_RCSID(0, "$NetBSD: sys_generic.c,v 1.122 2009/05/17 10:08:38 ad Exp $");
     74   1.15       cgd 
     75   1.15       cgd #include <sys/param.h>
     76   1.15       cgd #include <sys/systm.h>
     77   1.15       cgd #include <sys/filedesc.h>
     78   1.15       cgd #include <sys/ioctl.h>
     79   1.15       cgd #include <sys/file.h>
     80   1.15       cgd #include <sys/proc.h>
     81   1.15       cgd #include <sys/socketvar.h>
     82   1.22  christos #include <sys/signalvar.h>
     83   1.15       cgd #include <sys/uio.h>
     84   1.15       cgd #include <sys/kernel.h>
     85   1.15       cgd #include <sys/stat.h>
     86  1.103        ad #include <sys/kmem.h>
     87  1.103        ad #include <sys/poll.h>
     88  1.102       dsl #include <sys/vnode.h>
     89  1.103        ad #include <sys/mount.h>
     90  1.103        ad #include <sys/syscallargs.h>
     91   1.15       cgd #include <sys/ktrace.h>
     92  1.122        ad #include <sys/atomic.h>
     93   1.15       cgd 
     94   1.85      yamt #include <uvm/uvm_extern.h>
     95   1.85      yamt 
     96   1.15       cgd /*
     97   1.15       cgd  * Read system call.
     98   1.15       cgd  */
     99   1.15       cgd /* ARGSUSED */
    100   1.22  christos int
    101  1.110       dsl sys_read(struct lwp *l, const struct sys_read_args *uap, register_t *retval)
    102   1.20   thorpej {
    103  1.110       dsl 	/* {
    104   1.53     lukem 		syscallarg(int)		fd;
    105   1.53     lukem 		syscallarg(void *)	buf;
    106   1.53     lukem 		syscallarg(size_t)	nbyte;
    107  1.110       dsl 	} */
    108  1.115        ad 	file_t *fp;
    109  1.115        ad 	int fd;
    110   1.39   thorpej 
    111   1.53     lukem 	fd = SCARG(uap, fd);
    112   1.56   thorpej 
    113  1.115        ad 	if ((fp = fd_getfile(fd)) == NULL)
    114   1.56   thorpej 		return (EBADF);
    115   1.56   thorpej 
    116   1.70        pk 	if ((fp->f_flag & FREAD) == 0) {
    117  1.115        ad 		fd_putfile(fd);
    118   1.39   thorpej 		return (EBADF);
    119   1.70        pk 	}
    120   1.39   thorpej 
    121   1.45   thorpej 	/* dofileread() will unuse the descriptor for us */
    122  1.108        ad 	return (dofileread(fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
    123   1.39   thorpej 	    &fp->f_offset, FOF_UPDATE_OFFSET, retval));
    124   1.39   thorpej }
    125   1.39   thorpej 
    126   1.39   thorpej int
    127  1.108        ad dofileread(int fd, struct file *fp, void *buf, size_t nbyte,
    128   1.53     lukem 	off_t *offset, int flags, register_t *retval)
    129   1.53     lukem {
    130   1.84  christos 	struct iovec aiov;
    131   1.84  christos 	struct uio auio;
    132   1.84  christos 	size_t cnt;
    133   1.84  christos 	int error;
    134  1.108        ad 	lwp_t *l;
    135   1.85      yamt 
    136  1.108        ad 	l = curlwp;
    137   1.15       cgd 
    138  1.100  christos 	aiov.iov_base = (void *)buf;
    139   1.39   thorpej 	aiov.iov_len = nbyte;
    140   1.15       cgd 	auio.uio_iov = &aiov;
    141   1.15       cgd 	auio.uio_iovcnt = 1;
    142   1.39   thorpej 	auio.uio_resid = nbyte;
    143   1.15       cgd 	auio.uio_rw = UIO_READ;
    144  1.108        ad 	auio.uio_vmspace = l->l_proc->p_vmspace;
    145   1.40   thorpej 
    146   1.40   thorpej 	/*
    147   1.40   thorpej 	 * Reads return ssize_t because -1 is returned on error.  Therefore
    148   1.40   thorpej 	 * we must restrict the length to SSIZE_MAX to avoid garbage return
    149   1.40   thorpej 	 * values.
    150   1.40   thorpej 	 */
    151   1.45   thorpej 	if (auio.uio_resid > SSIZE_MAX) {
    152   1.45   thorpej 		error = EINVAL;
    153   1.45   thorpej 		goto out;
    154   1.45   thorpej 	}
    155   1.40   thorpej 
    156   1.38   thorpej 	cnt = auio.uio_resid;
    157   1.39   thorpej 	error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags);
    158   1.22  christos 	if (error)
    159   1.15       cgd 		if (auio.uio_resid != cnt && (error == ERESTART ||
    160   1.15       cgd 		    error == EINTR || error == EWOULDBLOCK))
    161   1.15       cgd 			error = 0;
    162   1.15       cgd 	cnt -= auio.uio_resid;
    163  1.105       dsl 	ktrgenio(fd, UIO_READ, buf, cnt, error);
    164   1.15       cgd 	*retval = cnt;
    165   1.45   thorpej  out:
    166  1.115        ad 	fd_putfile(fd);
    167   1.15       cgd 	return (error);
    168   1.15       cgd }
    169   1.15       cgd 
    170   1.15       cgd /*
    171   1.15       cgd  * Scatter read system call.
    172   1.15       cgd  */
    173   1.22  christos int
    174  1.110       dsl sys_readv(struct lwp *l, const struct sys_readv_args *uap, register_t *retval)
    175   1.20   thorpej {
    176  1.110       dsl 	/* {
    177   1.53     lukem 		syscallarg(int)				fd;
    178   1.53     lukem 		syscallarg(const struct iovec *)	iovp;
    179   1.53     lukem 		syscallarg(int)				iovcnt;
    180  1.110       dsl 	} */
    181  1.102       dsl 
    182  1.108        ad 	return do_filereadv(SCARG(uap, fd), SCARG(uap, iovp),
    183  1.102       dsl 	    SCARG(uap, iovcnt), NULL, FOF_UPDATE_OFFSET, retval);
    184  1.102       dsl }
    185  1.102       dsl 
    186  1.102       dsl int
    187  1.108        ad do_filereadv(int fd, const struct iovec *iovp, int iovcnt,
    188  1.102       dsl     off_t *offset, int flags, register_t *retval)
    189  1.102       dsl {
    190  1.102       dsl 	struct uio	auio;
    191  1.102       dsl 	struct iovec	*iov, *needfree = NULL, aiov[UIO_SMALLIOV];
    192  1.102       dsl 	int		i, error;
    193  1.102       dsl 	size_t		cnt;
    194  1.102       dsl 	u_int		iovlen;
    195  1.102       dsl 	struct file	*fp;
    196  1.102       dsl 	struct iovec	*ktriov = NULL;
    197  1.102       dsl 
    198  1.102       dsl 	if (iovcnt == 0)
    199  1.102       dsl 		return EINVAL;
    200   1.39   thorpej 
    201  1.115        ad 	if ((fp = fd_getfile(fd)) == NULL)
    202  1.102       dsl 		return EBADF;
    203   1.56   thorpej 
    204   1.70        pk 	if ((fp->f_flag & FREAD) == 0) {
    205  1.115        ad 		fd_putfile(fd);
    206  1.102       dsl 		return EBADF;
    207   1.70        pk 	}
    208   1.39   thorpej 
    209  1.102       dsl 	if (offset == NULL)
    210  1.102       dsl 		offset = &fp->f_offset;
    211  1.102       dsl 	else {
    212  1.102       dsl 		struct vnode *vp = fp->f_data;
    213  1.102       dsl 		if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
    214  1.102       dsl 			error = ESPIPE;
    215  1.102       dsl 			goto out;
    216  1.102       dsl 		}
    217  1.102       dsl 		/*
    218  1.102       dsl 		 * Test that the device is seekable ?
    219  1.102       dsl 		 * XXX This works because no file systems actually
    220  1.102       dsl 		 * XXX take any action on the seek operation.
    221  1.102       dsl 		 */
    222  1.102       dsl 		error = VOP_SEEK(vp, fp->f_offset, *offset, fp->f_cred);
    223  1.102       dsl 		if (error != 0)
    224  1.102       dsl 			goto out;
    225  1.102       dsl 	}
    226   1.15       cgd 
    227   1.42     perry 	iovlen = iovcnt * sizeof(struct iovec);
    228  1.102       dsl 	if (flags & FOF_IOV_SYSSPACE)
    229  1.102       dsl 		iov = __UNCONST(iovp);
    230  1.102       dsl 	else {
    231  1.102       dsl 		iov = aiov;
    232  1.102       dsl 		if ((u_int)iovcnt > UIO_SMALLIOV) {
    233  1.102       dsl 			if ((u_int)iovcnt > IOV_MAX) {
    234  1.102       dsl 				error = EINVAL;
    235  1.102       dsl 				goto out;
    236  1.102       dsl 			}
    237  1.103        ad 			iov = kmem_alloc(iovlen, KM_SLEEP);
    238  1.103        ad 			if (iov == NULL) {
    239  1.103        ad 				error = ENOMEM;
    240  1.103        ad 				goto out;
    241  1.103        ad 			}
    242  1.102       dsl 			needfree = iov;
    243   1.45   thorpej 		}
    244  1.102       dsl 		error = copyin(iovp, iov, iovlen);
    245  1.102       dsl 		if (error)
    246  1.102       dsl 			goto done;
    247   1.45   thorpej 	}
    248   1.41    kleink 
    249   1.15       cgd 	auio.uio_iov = iov;
    250   1.34   mycroft 	auio.uio_iovcnt = iovcnt;
    251   1.15       cgd 	auio.uio_rw = UIO_READ;
    252  1.115        ad 	auio.uio_vmspace = curproc->p_vmspace;
    253  1.102       dsl 
    254   1.15       cgd 	auio.uio_resid = 0;
    255  1.102       dsl 	for (i = 0; i < iovcnt; i++, iov++) {
    256   1.15       cgd 		auio.uio_resid += iov->iov_len;
    257   1.40   thorpej 		/*
    258   1.40   thorpej 		 * Reads return ssize_t because -1 is returned on error.
    259   1.40   thorpej 		 * Therefore we must restrict the length to SSIZE_MAX to
    260   1.40   thorpej 		 * avoid garbage return values.
    261   1.40   thorpej 		 */
    262   1.40   thorpej 		if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
    263   1.15       cgd 			error = EINVAL;
    264   1.15       cgd 			goto done;
    265   1.15       cgd 		}
    266   1.15       cgd 	}
    267  1.102       dsl 
    268   1.15       cgd 	/*
    269   1.15       cgd 	 * if tracing, save a copy of iovec
    270   1.15       cgd 	 */
    271  1.104        ad 	if (ktrpoint(KTR_GENIO))  {
    272  1.103        ad 		ktriov = kmem_alloc(iovlen, KM_SLEEP);
    273  1.103        ad 		if (ktriov != NULL)
    274  1.103        ad 			memcpy(ktriov, auio.uio_iov, iovlen);
    275   1.15       cgd 	}
    276  1.102       dsl 
    277   1.15       cgd 	cnt = auio.uio_resid;
    278   1.39   thorpej 	error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags);
    279   1.22  christos 	if (error)
    280   1.15       cgd 		if (auio.uio_resid != cnt && (error == ERESTART ||
    281   1.15       cgd 		    error == EINTR || error == EWOULDBLOCK))
    282   1.15       cgd 			error = 0;
    283   1.15       cgd 	cnt -= auio.uio_resid;
    284  1.102       dsl 	*retval = cnt;
    285  1.102       dsl 
    286   1.58     itohy 	if (ktriov != NULL) {
    287  1.104        ad 		ktrgeniov(fd, UIO_READ, ktriov, cnt, error);
    288  1.103        ad 		kmem_free(ktriov, iovlen);
    289   1.15       cgd 	}
    290  1.102       dsl 
    291   1.45   thorpej  done:
    292   1.15       cgd 	if (needfree)
    293  1.103        ad 		kmem_free(needfree, iovlen);
    294   1.45   thorpej  out:
    295  1.115        ad 	fd_putfile(fd);
    296   1.15       cgd 	return (error);
    297   1.15       cgd }
    298   1.15       cgd 
    299   1.15       cgd /*
    300   1.15       cgd  * Write system call
    301   1.15       cgd  */
    302   1.22  christos int
    303  1.110       dsl sys_write(struct lwp *l, const struct sys_write_args *uap, register_t *retval)
    304   1.20   thorpej {
    305  1.110       dsl 	/* {
    306   1.53     lukem 		syscallarg(int)			fd;
    307   1.53     lukem 		syscallarg(const void *)	buf;
    308   1.53     lukem 		syscallarg(size_t)		nbyte;
    309  1.110       dsl 	} */
    310  1.115        ad 	file_t *fp;
    311  1.115        ad 	int fd;
    312   1.39   thorpej 
    313   1.53     lukem 	fd = SCARG(uap, fd);
    314   1.56   thorpej 
    315  1.115        ad 	if ((fp = fd_getfile(fd)) == NULL)
    316   1.56   thorpej 		return (EBADF);
    317   1.56   thorpej 
    318   1.70        pk 	if ((fp->f_flag & FWRITE) == 0) {
    319  1.115        ad 		fd_putfile(fd);
    320   1.39   thorpej 		return (EBADF);
    321   1.70        pk 	}
    322   1.39   thorpej 
    323   1.45   thorpej 	/* dofilewrite() will unuse the descriptor for us */
    324  1.108        ad 	return (dofilewrite(fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
    325   1.39   thorpej 	    &fp->f_offset, FOF_UPDATE_OFFSET, retval));
    326   1.39   thorpej }
    327   1.39   thorpej 
    328   1.39   thorpej int
    329  1.108        ad dofilewrite(int fd, struct file *fp, const void *buf,
    330   1.53     lukem 	size_t nbyte, off_t *offset, int flags, register_t *retval)
    331   1.53     lukem {
    332   1.84  christos 	struct iovec aiov;
    333   1.84  christos 	struct uio auio;
    334   1.84  christos 	size_t cnt;
    335   1.84  christos 	int error;
    336   1.15       cgd 
    337   1.83  christos 	aiov.iov_base = __UNCONST(buf);		/* XXXUNCONST kills const */
    338   1.39   thorpej 	aiov.iov_len = nbyte;
    339   1.15       cgd 	auio.uio_iov = &aiov;
    340   1.15       cgd 	auio.uio_iovcnt = 1;
    341   1.39   thorpej 	auio.uio_resid = nbyte;
    342   1.15       cgd 	auio.uio_rw = UIO_WRITE;
    343  1.115        ad 	auio.uio_vmspace = curproc->p_vmspace;
    344   1.40   thorpej 
    345   1.40   thorpej 	/*
    346   1.40   thorpej 	 * Writes return ssize_t because -1 is returned on error.  Therefore
    347   1.40   thorpej 	 * we must restrict the length to SSIZE_MAX to avoid garbage return
    348   1.40   thorpej 	 * values.
    349   1.40   thorpej 	 */
    350   1.45   thorpej 	if (auio.uio_resid > SSIZE_MAX) {
    351   1.45   thorpej 		error = EINVAL;
    352   1.45   thorpej 		goto out;
    353   1.45   thorpej 	}
    354   1.40   thorpej 
    355   1.38   thorpej 	cnt = auio.uio_resid;
    356   1.39   thorpej 	error = (*fp->f_ops->fo_write)(fp, offset, &auio, fp->f_cred, flags);
    357   1.22  christos 	if (error) {
    358   1.15       cgd 		if (auio.uio_resid != cnt && (error == ERESTART ||
    359   1.15       cgd 		    error == EINTR || error == EWOULDBLOCK))
    360   1.15       cgd 			error = 0;
    361   1.98        ad 		if (error == EPIPE) {
    362  1.118        ad 			mutex_enter(proc_lock);
    363  1.115        ad 			psignal(curproc, SIGPIPE);
    364  1.118        ad 			mutex_exit(proc_lock);
    365   1.98        ad 		}
    366   1.15       cgd 	}
    367   1.15       cgd 	cnt -= auio.uio_resid;
    368  1.105       dsl 	ktrgenio(fd, UIO_WRITE, buf, cnt, error);
    369   1.15       cgd 	*retval = cnt;
    370   1.45   thorpej  out:
    371  1.115        ad 	fd_putfile(fd);
    372   1.15       cgd 	return (error);
    373   1.15       cgd }
    374   1.15       cgd 
    375   1.15       cgd /*
    376   1.15       cgd  * Gather write system call
    377   1.15       cgd  */
    378   1.22  christos int
    379  1.110       dsl sys_writev(struct lwp *l, const struct sys_writev_args *uap, register_t *retval)
    380   1.20   thorpej {
    381  1.110       dsl 	/* {
    382   1.53     lukem 		syscallarg(int)				fd;
    383   1.53     lukem 		syscallarg(const struct iovec *)	iovp;
    384   1.53     lukem 		syscallarg(int)				iovcnt;
    385  1.110       dsl 	} */
    386  1.102       dsl 
    387  1.108        ad 	return do_filewritev(SCARG(uap, fd), SCARG(uap, iovp),
    388  1.102       dsl 	    SCARG(uap, iovcnt), NULL, FOF_UPDATE_OFFSET, retval);
    389  1.102       dsl }
    390  1.102       dsl 
    391  1.102       dsl int
    392  1.108        ad do_filewritev(int fd, const struct iovec *iovp, int iovcnt,
    393  1.102       dsl     off_t *offset, int flags, register_t *retval)
    394  1.102       dsl {
    395  1.102       dsl 	struct uio	auio;
    396  1.102       dsl 	struct iovec	*iov, *needfree = NULL, aiov[UIO_SMALLIOV];
    397  1.102       dsl 	int		i, error;
    398  1.102       dsl 	size_t		cnt;
    399  1.102       dsl 	u_int		iovlen;
    400   1.53     lukem 	struct file	*fp;
    401  1.102       dsl 	struct iovec	*ktriov = NULL;
    402  1.102       dsl 
    403  1.102       dsl 	if (iovcnt == 0)
    404  1.102       dsl 		return EINVAL;
    405   1.39   thorpej 
    406  1.115        ad 	if ((fp = fd_getfile(fd)) == NULL)
    407  1.102       dsl 		return EBADF;
    408   1.56   thorpej 
    409   1.70        pk 	if ((fp->f_flag & FWRITE) == 0) {
    410  1.115        ad 		fd_putfile(fd);
    411  1.102       dsl 		return EBADF;
    412   1.70        pk 	}
    413   1.39   thorpej 
    414  1.102       dsl 	if (offset == NULL)
    415  1.102       dsl 		offset = &fp->f_offset;
    416  1.102       dsl 	else {
    417  1.102       dsl 		struct vnode *vp = fp->f_data;
    418  1.102       dsl 		if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
    419  1.102       dsl 			error = ESPIPE;
    420  1.102       dsl 			goto out;
    421  1.102       dsl 		}
    422  1.102       dsl 		/*
    423  1.102       dsl 		 * Test that the device is seekable ?
    424  1.102       dsl 		 * XXX This works because no file systems actually
    425  1.102       dsl 		 * XXX take any action on the seek operation.
    426  1.102       dsl 		 */
    427  1.102       dsl 		error = VOP_SEEK(vp, fp->f_offset, *offset, fp->f_cred);
    428  1.102       dsl 		if (error != 0)
    429  1.102       dsl 			goto out;
    430  1.102       dsl 	}
    431   1.39   thorpej 
    432   1.42     perry 	iovlen = iovcnt * sizeof(struct iovec);
    433  1.102       dsl 	if (flags & FOF_IOV_SYSSPACE)
    434  1.102       dsl 		iov = __UNCONST(iovp);
    435  1.102       dsl 	else {
    436  1.102       dsl 		iov = aiov;
    437  1.102       dsl 		if ((u_int)iovcnt > UIO_SMALLIOV) {
    438  1.102       dsl 			if ((u_int)iovcnt > IOV_MAX) {
    439  1.102       dsl 				error = EINVAL;
    440  1.102       dsl 				goto out;
    441  1.102       dsl 			}
    442  1.103        ad 			iov = kmem_alloc(iovlen, KM_SLEEP);
    443  1.103        ad 			if (iov == NULL) {
    444  1.103        ad 				error = ENOMEM;
    445  1.103        ad 				goto out;
    446  1.103        ad 			}
    447  1.102       dsl 			needfree = iov;
    448   1.62  jdolecek 		}
    449  1.102       dsl 		error = copyin(iovp, iov, iovlen);
    450  1.102       dsl 		if (error)
    451  1.102       dsl 			goto done;
    452   1.45   thorpej 	}
    453   1.41    kleink 
    454   1.15       cgd 	auio.uio_iov = iov;
    455   1.34   mycroft 	auio.uio_iovcnt = iovcnt;
    456   1.15       cgd 	auio.uio_rw = UIO_WRITE;
    457  1.108        ad 	auio.uio_vmspace = curproc->p_vmspace;
    458  1.102       dsl 
    459   1.15       cgd 	auio.uio_resid = 0;
    460  1.102       dsl 	for (i = 0; i < iovcnt; i++, iov++) {
    461   1.15       cgd 		auio.uio_resid += iov->iov_len;
    462   1.40   thorpej 		/*
    463   1.40   thorpej 		 * Writes return ssize_t because -1 is returned on error.
    464   1.40   thorpej 		 * Therefore we must restrict the length to SSIZE_MAX to
    465   1.40   thorpej 		 * avoid garbage return values.
    466   1.40   thorpej 		 */
    467   1.40   thorpej 		if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
    468   1.15       cgd 			error = EINVAL;
    469   1.15       cgd 			goto done;
    470   1.15       cgd 		}
    471   1.15       cgd 	}
    472  1.102       dsl 
    473   1.15       cgd 	/*
    474   1.15       cgd 	 * if tracing, save a copy of iovec
    475   1.15       cgd 	 */
    476  1.104        ad 	if (ktrpoint(KTR_GENIO))  {
    477  1.103        ad 		ktriov = kmem_alloc(iovlen, KM_SLEEP);
    478  1.103        ad 		if (ktriov != NULL)
    479  1.103        ad 			memcpy(ktriov, auio.uio_iov, iovlen);
    480   1.15       cgd 	}
    481  1.104        ad 
    482   1.15       cgd 	cnt = auio.uio_resid;
    483   1.39   thorpej 	error = (*fp->f_ops->fo_write)(fp, offset, &auio, fp->f_cred, flags);
    484   1.22  christos 	if (error) {
    485   1.15       cgd 		if (auio.uio_resid != cnt && (error == ERESTART ||
    486   1.15       cgd 		    error == EINTR || error == EWOULDBLOCK))
    487   1.15       cgd 			error = 0;
    488   1.98        ad 		if (error == EPIPE) {
    489  1.118        ad 			mutex_enter(proc_lock);
    490  1.115        ad 			psignal(curproc, SIGPIPE);
    491  1.118        ad 			mutex_exit(proc_lock);
    492   1.98        ad 		}
    493   1.15       cgd 	}
    494   1.15       cgd 	cnt -= auio.uio_resid;
    495  1.102       dsl 	*retval = cnt;
    496  1.102       dsl 
    497   1.78  drochner 	if (ktriov != NULL) {
    498  1.104        ad 		ktrgeniov(fd, UIO_WRITE, ktriov, cnt, error);
    499  1.103        ad 		kmem_free(ktriov, iovlen);
    500   1.15       cgd 	}
    501  1.102       dsl 
    502   1.45   thorpej  done:
    503   1.15       cgd 	if (needfree)
    504  1.103        ad 		kmem_free(needfree, iovlen);
    505   1.45   thorpej  out:
    506  1.115        ad 	fd_putfile(fd);
    507   1.15       cgd 	return (error);
    508   1.15       cgd }
    509   1.15       cgd 
    510   1.15       cgd /*
    511   1.15       cgd  * Ioctl system call
    512   1.15       cgd  */
    513   1.15       cgd /* ARGSUSED */
    514   1.22  christos int
    515  1.110       dsl sys_ioctl(struct lwp *l, const struct sys_ioctl_args *uap, register_t *retval)
    516   1.20   thorpej {
    517  1.110       dsl 	/* {
    518   1.53     lukem 		syscallarg(int)		fd;
    519   1.53     lukem 		syscallarg(u_long)	com;
    520  1.100  christos 		syscallarg(void *)	data;
    521  1.110       dsl 	} */
    522   1.53     lukem 	struct file	*fp;
    523  1.103        ad 	proc_t		*p;
    524   1.53     lukem 	struct filedesc	*fdp;
    525   1.53     lukem 	u_long		com;
    526   1.53     lukem 	int		error;
    527   1.53     lukem 	u_int		size;
    528  1.100  christos 	void 		*data, *memp;
    529   1.53     lukem #define	STK_PARAMS	128
    530   1.53     lukem 	u_long		stkbuf[STK_PARAMS/sizeof(u_long)];
    531  1.115        ad 	fdfile_t	*ff;
    532   1.15       cgd 
    533   1.53     lukem 	error = 0;
    534   1.69   thorpej 	p = l->l_proc;
    535   1.15       cgd 	fdp = p->p_fd;
    536   1.56   thorpej 
    537  1.115        ad 	if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
    538   1.15       cgd 		return (EBADF);
    539   1.15       cgd 
    540   1.45   thorpej 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
    541   1.45   thorpej 		error = EBADF;
    542   1.65       scw 		com = 0;
    543   1.45   thorpej 		goto out;
    544   1.45   thorpej 	}
    545   1.15       cgd 
    546  1.115        ad 	ff = fdp->fd_ofiles[SCARG(uap, fd)];
    547   1.16       cgd 	switch (com = SCARG(uap, com)) {
    548   1.15       cgd 	case FIONCLEX:
    549  1.120      matt 		ff->ff_exclose = false;
    550   1.45   thorpej 		goto out;
    551   1.45   thorpej 
    552   1.15       cgd 	case FIOCLEX:
    553  1.120      matt 		ff->ff_exclose = true;
    554  1.120      matt 		fdp->fd_exclose = true;
    555   1.45   thorpej 		goto out;
    556   1.15       cgd 	}
    557   1.15       cgd 
    558   1.15       cgd 	/*
    559   1.15       cgd 	 * Interpret high order word to find amount of data to be
    560   1.15       cgd 	 * copied to/from the user's address space.
    561   1.15       cgd 	 */
    562   1.15       cgd 	size = IOCPARM_LEN(com);
    563   1.45   thorpej 	if (size > IOCPARM_MAX) {
    564   1.45   thorpej 		error = ENOTTY;
    565   1.45   thorpej 		goto out;
    566   1.45   thorpej 	}
    567   1.15       cgd 	memp = NULL;
    568   1.42     perry 	if (size > sizeof(stkbuf)) {
    569  1.103        ad 		memp = kmem_alloc(size, KM_SLEEP);
    570   1.15       cgd 		data = memp;
    571   1.15       cgd 	} else
    572  1.100  christos 		data = (void *)stkbuf;
    573   1.15       cgd 	if (com&IOC_IN) {
    574   1.15       cgd 		if (size) {
    575   1.31       cgd 			error = copyin(SCARG(uap, data), data, size);
    576   1.15       cgd 			if (error) {
    577   1.15       cgd 				if (memp)
    578  1.103        ad 					kmem_free(memp, size);
    579   1.45   thorpej 				goto out;
    580   1.15       cgd 			}
    581  1.104        ad 			ktrgenio(SCARG(uap, fd), UIO_WRITE, SCARG(uap, data),
    582  1.104        ad 			    size, 0);
    583   1.15       cgd 		} else
    584  1.100  christos 			*(void **)data = SCARG(uap, data);
    585   1.15       cgd 	} else if ((com&IOC_OUT) && size)
    586   1.15       cgd 		/*
    587   1.15       cgd 		 * Zero the buffer so the user always
    588   1.15       cgd 		 * gets back something deterministic.
    589   1.15       cgd 		 */
    590   1.44     perry 		memset(data, 0, size);
    591   1.15       cgd 	else if (com&IOC_VOID)
    592  1.100  christos 		*(void **)data = SCARG(uap, data);
    593   1.15       cgd 
    594   1.15       cgd 	switch (com) {
    595   1.15       cgd 
    596   1.15       cgd 	case FIONBIO:
    597  1.122        ad 		/* XXX Code block is not atomic */
    598   1.79  jdolecek 		if (*(int *)data != 0)
    599  1.122        ad 			atomic_or_uint(&fp->f_flag, FNONBLOCK);
    600   1.15       cgd 		else
    601  1.122        ad 			atomic_and_uint(&fp->f_flag, ~FNONBLOCK);
    602  1.115        ad 		error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, data);
    603   1.15       cgd 		break;
    604   1.15       cgd 
    605   1.15       cgd 	case FIOASYNC:
    606  1.122        ad 		/* XXX Code block is not atomic */
    607   1.79  jdolecek 		if (*(int *)data != 0)
    608  1.122        ad 			atomic_or_uint(&fp->f_flag, FASYNC);
    609   1.15       cgd 		else
    610  1.122        ad 			atomic_and_uint(&fp->f_flag, ~FASYNC);
    611  1.115        ad 		error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, data);
    612   1.15       cgd 		break;
    613   1.15       cgd 
    614   1.15       cgd 	default:
    615  1.115        ad 		error = (*fp->f_ops->fo_ioctl)(fp, com, data);
    616   1.15       cgd 		/*
    617   1.15       cgd 		 * Copy any data to user, size was
    618   1.15       cgd 		 * already set and checked above.
    619   1.15       cgd 		 */
    620   1.73       dsl 		if (error == 0 && (com&IOC_OUT) && size) {
    621   1.31       cgd 			error = copyout(data, SCARG(uap, data), size);
    622  1.104        ad 			ktrgenio(SCARG(uap, fd), UIO_READ, SCARG(uap, data),
    623  1.104        ad 			    size, error);
    624   1.73       dsl 		}
    625   1.15       cgd 		break;
    626   1.15       cgd 	}
    627   1.15       cgd 	if (memp)
    628  1.103        ad 		kmem_free(memp, size);
    629   1.45   thorpej  out:
    630  1.115        ad 	fd_putfile(SCARG(uap, fd));
    631   1.61    atatat 	switch (error) {
    632   1.61    atatat 	case -1:
    633   1.61    atatat 		printf("sys_ioctl: _IO%s%s('%c', %lu, %lu) returned -1: "
    634   1.61    atatat 		    "pid=%d comm=%s\n",
    635   1.61    atatat 		    (com & IOC_IN) ? "W" : "", (com & IOC_OUT) ? "R" : "",
    636   1.61    atatat 		    (char)IOCGROUP(com), (com & 0xff), IOCPARM_LEN(com),
    637   1.61    atatat 		    p->p_pid, p->p_comm);
    638   1.61    atatat 		/* FALLTHROUGH */
    639   1.61    atatat 	case EPASSTHROUGH:
    640   1.61    atatat 		error = ENOTTY;
    641   1.61    atatat 		/* FALLTHROUGH */
    642   1.61    atatat 	default:
    643   1.61    atatat 		return (error);
    644   1.61    atatat 	}
    645   1.15       cgd }
    646