Home | History | Annotate | Line # | Download | only in kern
sys_generic.c revision 1.120
      1  1.120      matt /*	$NetBSD: sys_generic.c,v 1.120 2008/07/02 16:45:20 matt Exp $	*/
      2  1.103        ad 
      3  1.103        ad /*-
      4  1.113        ad  * Copyright (c) 2007, 2008 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.120      matt __KERNEL_RCSID(0, "$NetBSD: sys_generic.c,v 1.120 2008/07/02 16:45:20 matt 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.15       cgd 
     93   1.85      yamt #include <uvm/uvm_extern.h>
     94   1.85      yamt 
     95   1.15       cgd /*
     96   1.15       cgd  * Read system call.
     97   1.15       cgd  */
     98   1.15       cgd /* ARGSUSED */
     99   1.22  christos int
    100  1.110       dsl sys_read(struct lwp *l, const struct sys_read_args *uap, register_t *retval)
    101   1.20   thorpej {
    102  1.110       dsl 	/* {
    103   1.53     lukem 		syscallarg(int)		fd;
    104   1.53     lukem 		syscallarg(void *)	buf;
    105   1.53     lukem 		syscallarg(size_t)	nbyte;
    106  1.110       dsl 	} */
    107  1.115        ad 	file_t *fp;
    108  1.115        ad 	int fd;
    109   1.39   thorpej 
    110   1.53     lukem 	fd = SCARG(uap, fd);
    111   1.56   thorpej 
    112  1.115        ad 	if ((fp = fd_getfile(fd)) == NULL)
    113   1.56   thorpej 		return (EBADF);
    114   1.56   thorpej 
    115   1.70        pk 	if ((fp->f_flag & FREAD) == 0) {
    116  1.115        ad 		fd_putfile(fd);
    117   1.39   thorpej 		return (EBADF);
    118   1.70        pk 	}
    119   1.39   thorpej 
    120   1.45   thorpej 	/* dofileread() will unuse the descriptor for us */
    121  1.108        ad 	return (dofileread(fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
    122   1.39   thorpej 	    &fp->f_offset, FOF_UPDATE_OFFSET, retval));
    123   1.39   thorpej }
    124   1.39   thorpej 
    125   1.39   thorpej int
    126  1.108        ad dofileread(int fd, struct file *fp, void *buf, size_t nbyte,
    127   1.53     lukem 	off_t *offset, int flags, register_t *retval)
    128   1.53     lukem {
    129   1.84  christos 	struct iovec aiov;
    130   1.84  christos 	struct uio auio;
    131   1.84  christos 	size_t cnt;
    132   1.84  christos 	int error;
    133  1.108        ad 	lwp_t *l;
    134   1.85      yamt 
    135  1.108        ad 	l = curlwp;
    136   1.15       cgd 
    137  1.100  christos 	aiov.iov_base = (void *)buf;
    138   1.39   thorpej 	aiov.iov_len = nbyte;
    139   1.15       cgd 	auio.uio_iov = &aiov;
    140   1.15       cgd 	auio.uio_iovcnt = 1;
    141   1.39   thorpej 	auio.uio_resid = nbyte;
    142   1.15       cgd 	auio.uio_rw = UIO_READ;
    143  1.108        ad 	auio.uio_vmspace = l->l_proc->p_vmspace;
    144   1.40   thorpej 
    145   1.40   thorpej 	/*
    146   1.40   thorpej 	 * Reads return ssize_t because -1 is returned on error.  Therefore
    147   1.40   thorpej 	 * we must restrict the length to SSIZE_MAX to avoid garbage return
    148   1.40   thorpej 	 * values.
    149   1.40   thorpej 	 */
    150   1.45   thorpej 	if (auio.uio_resid > SSIZE_MAX) {
    151   1.45   thorpej 		error = EINVAL;
    152   1.45   thorpej 		goto out;
    153   1.45   thorpej 	}
    154   1.40   thorpej 
    155   1.38   thorpej 	cnt = auio.uio_resid;
    156   1.39   thorpej 	error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags);
    157   1.22  christos 	if (error)
    158   1.15       cgd 		if (auio.uio_resid != cnt && (error == ERESTART ||
    159   1.15       cgd 		    error == EINTR || error == EWOULDBLOCK))
    160   1.15       cgd 			error = 0;
    161   1.15       cgd 	cnt -= auio.uio_resid;
    162  1.105       dsl 	ktrgenio(fd, UIO_READ, buf, cnt, error);
    163   1.15       cgd 	*retval = cnt;
    164   1.45   thorpej  out:
    165  1.115        ad 	fd_putfile(fd);
    166   1.15       cgd 	return (error);
    167   1.15       cgd }
    168   1.15       cgd 
    169   1.15       cgd /*
    170   1.15       cgd  * Scatter read system call.
    171   1.15       cgd  */
    172   1.22  christos int
    173  1.110       dsl sys_readv(struct lwp *l, const struct sys_readv_args *uap, register_t *retval)
    174   1.20   thorpej {
    175  1.110       dsl 	/* {
    176   1.53     lukem 		syscallarg(int)				fd;
    177   1.53     lukem 		syscallarg(const struct iovec *)	iovp;
    178   1.53     lukem 		syscallarg(int)				iovcnt;
    179  1.110       dsl 	} */
    180  1.102       dsl 
    181  1.108        ad 	return do_filereadv(SCARG(uap, fd), SCARG(uap, iovp),
    182  1.102       dsl 	    SCARG(uap, iovcnt), NULL, FOF_UPDATE_OFFSET, retval);
    183  1.102       dsl }
    184  1.102       dsl 
    185  1.102       dsl int
    186  1.108        ad do_filereadv(int fd, const struct iovec *iovp, int iovcnt,
    187  1.102       dsl     off_t *offset, int flags, register_t *retval)
    188  1.102       dsl {
    189  1.102       dsl 	struct uio	auio;
    190  1.102       dsl 	struct iovec	*iov, *needfree = NULL, aiov[UIO_SMALLIOV];
    191  1.102       dsl 	int		i, error;
    192  1.102       dsl 	size_t		cnt;
    193  1.102       dsl 	u_int		iovlen;
    194  1.102       dsl 	struct file	*fp;
    195  1.102       dsl 	struct iovec	*ktriov = NULL;
    196  1.102       dsl 
    197  1.102       dsl 	if (iovcnt == 0)
    198  1.102       dsl 		return EINVAL;
    199   1.39   thorpej 
    200  1.115        ad 	if ((fp = fd_getfile(fd)) == NULL)
    201  1.102       dsl 		return EBADF;
    202   1.56   thorpej 
    203   1.70        pk 	if ((fp->f_flag & FREAD) == 0) {
    204  1.115        ad 		fd_putfile(fd);
    205  1.102       dsl 		return EBADF;
    206   1.70        pk 	}
    207   1.39   thorpej 
    208  1.102       dsl 	if (offset == NULL)
    209  1.102       dsl 		offset = &fp->f_offset;
    210  1.102       dsl 	else {
    211  1.102       dsl 		struct vnode *vp = fp->f_data;
    212  1.102       dsl 		if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
    213  1.102       dsl 			error = ESPIPE;
    214  1.102       dsl 			goto out;
    215  1.102       dsl 		}
    216  1.102       dsl 		/*
    217  1.102       dsl 		 * Test that the device is seekable ?
    218  1.102       dsl 		 * XXX This works because no file systems actually
    219  1.102       dsl 		 * XXX take any action on the seek operation.
    220  1.102       dsl 		 */
    221  1.102       dsl 		error = VOP_SEEK(vp, fp->f_offset, *offset, fp->f_cred);
    222  1.102       dsl 		if (error != 0)
    223  1.102       dsl 			goto out;
    224  1.102       dsl 	}
    225   1.15       cgd 
    226   1.42     perry 	iovlen = iovcnt * sizeof(struct iovec);
    227  1.102       dsl 	if (flags & FOF_IOV_SYSSPACE)
    228  1.102       dsl 		iov = __UNCONST(iovp);
    229  1.102       dsl 	else {
    230  1.102       dsl 		iov = aiov;
    231  1.102       dsl 		if ((u_int)iovcnt > UIO_SMALLIOV) {
    232  1.102       dsl 			if ((u_int)iovcnt > IOV_MAX) {
    233  1.102       dsl 				error = EINVAL;
    234  1.102       dsl 				goto out;
    235  1.102       dsl 			}
    236  1.103        ad 			iov = kmem_alloc(iovlen, KM_SLEEP);
    237  1.103        ad 			if (iov == NULL) {
    238  1.103        ad 				error = ENOMEM;
    239  1.103        ad 				goto out;
    240  1.103        ad 			}
    241  1.102       dsl 			needfree = iov;
    242   1.45   thorpej 		}
    243  1.102       dsl 		error = copyin(iovp, iov, iovlen);
    244  1.102       dsl 		if (error)
    245  1.102       dsl 			goto done;
    246   1.45   thorpej 	}
    247   1.41    kleink 
    248   1.15       cgd 	auio.uio_iov = iov;
    249   1.34   mycroft 	auio.uio_iovcnt = iovcnt;
    250   1.15       cgd 	auio.uio_rw = UIO_READ;
    251  1.115        ad 	auio.uio_vmspace = curproc->p_vmspace;
    252  1.102       dsl 
    253   1.15       cgd 	auio.uio_resid = 0;
    254  1.102       dsl 	for (i = 0; i < iovcnt; i++, iov++) {
    255   1.15       cgd 		auio.uio_resid += iov->iov_len;
    256   1.40   thorpej 		/*
    257   1.40   thorpej 		 * Reads return ssize_t because -1 is returned on error.
    258   1.40   thorpej 		 * Therefore we must restrict the length to SSIZE_MAX to
    259   1.40   thorpej 		 * avoid garbage return values.
    260   1.40   thorpej 		 */
    261   1.40   thorpej 		if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
    262   1.15       cgd 			error = EINVAL;
    263   1.15       cgd 			goto done;
    264   1.15       cgd 		}
    265   1.15       cgd 	}
    266  1.102       dsl 
    267   1.15       cgd 	/*
    268   1.15       cgd 	 * if tracing, save a copy of iovec
    269   1.15       cgd 	 */
    270  1.104        ad 	if (ktrpoint(KTR_GENIO))  {
    271  1.103        ad 		ktriov = kmem_alloc(iovlen, KM_SLEEP);
    272  1.103        ad 		if (ktriov != NULL)
    273  1.103        ad 			memcpy(ktriov, auio.uio_iov, iovlen);
    274   1.15       cgd 	}
    275  1.102       dsl 
    276   1.15       cgd 	cnt = auio.uio_resid;
    277   1.39   thorpej 	error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags);
    278   1.22  christos 	if (error)
    279   1.15       cgd 		if (auio.uio_resid != cnt && (error == ERESTART ||
    280   1.15       cgd 		    error == EINTR || error == EWOULDBLOCK))
    281   1.15       cgd 			error = 0;
    282   1.15       cgd 	cnt -= auio.uio_resid;
    283  1.102       dsl 	*retval = cnt;
    284  1.102       dsl 
    285   1.58     itohy 	if (ktriov != NULL) {
    286  1.104        ad 		ktrgeniov(fd, UIO_READ, ktriov, cnt, error);
    287  1.103        ad 		kmem_free(ktriov, iovlen);
    288   1.15       cgd 	}
    289  1.102       dsl 
    290   1.45   thorpej  done:
    291   1.15       cgd 	if (needfree)
    292  1.103        ad 		kmem_free(needfree, iovlen);
    293   1.45   thorpej  out:
    294  1.115        ad 	fd_putfile(fd);
    295   1.15       cgd 	return (error);
    296   1.15       cgd }
    297   1.15       cgd 
    298   1.15       cgd /*
    299   1.15       cgd  * Write system call
    300   1.15       cgd  */
    301   1.22  christos int
    302  1.110       dsl sys_write(struct lwp *l, const struct sys_write_args *uap, register_t *retval)
    303   1.20   thorpej {
    304  1.110       dsl 	/* {
    305   1.53     lukem 		syscallarg(int)			fd;
    306   1.53     lukem 		syscallarg(const void *)	buf;
    307   1.53     lukem 		syscallarg(size_t)		nbyte;
    308  1.110       dsl 	} */
    309  1.115        ad 	file_t *fp;
    310  1.115        ad 	int fd;
    311   1.39   thorpej 
    312   1.53     lukem 	fd = SCARG(uap, fd);
    313   1.56   thorpej 
    314  1.115        ad 	if ((fp = fd_getfile(fd)) == NULL)
    315   1.56   thorpej 		return (EBADF);
    316   1.56   thorpej 
    317   1.70        pk 	if ((fp->f_flag & FWRITE) == 0) {
    318  1.115        ad 		fd_putfile(fd);
    319   1.39   thorpej 		return (EBADF);
    320   1.70        pk 	}
    321   1.39   thorpej 
    322   1.45   thorpej 	/* dofilewrite() will unuse the descriptor for us */
    323  1.108        ad 	return (dofilewrite(fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
    324   1.39   thorpej 	    &fp->f_offset, FOF_UPDATE_OFFSET, retval));
    325   1.39   thorpej }
    326   1.39   thorpej 
    327   1.39   thorpej int
    328  1.108        ad dofilewrite(int fd, struct file *fp, const void *buf,
    329   1.53     lukem 	size_t nbyte, off_t *offset, int flags, register_t *retval)
    330   1.53     lukem {
    331   1.84  christos 	struct iovec aiov;
    332   1.84  christos 	struct uio auio;
    333   1.84  christos 	size_t cnt;
    334   1.84  christos 	int error;
    335   1.15       cgd 
    336   1.83  christos 	aiov.iov_base = __UNCONST(buf);		/* XXXUNCONST kills const */
    337   1.39   thorpej 	aiov.iov_len = nbyte;
    338   1.15       cgd 	auio.uio_iov = &aiov;
    339   1.15       cgd 	auio.uio_iovcnt = 1;
    340   1.39   thorpej 	auio.uio_resid = nbyte;
    341   1.15       cgd 	auio.uio_rw = UIO_WRITE;
    342  1.115        ad 	auio.uio_vmspace = curproc->p_vmspace;
    343   1.40   thorpej 
    344   1.40   thorpej 	/*
    345   1.40   thorpej 	 * Writes return ssize_t because -1 is returned on error.  Therefore
    346   1.40   thorpej 	 * we must restrict the length to SSIZE_MAX to avoid garbage return
    347   1.40   thorpej 	 * values.
    348   1.40   thorpej 	 */
    349   1.45   thorpej 	if (auio.uio_resid > SSIZE_MAX) {
    350   1.45   thorpej 		error = EINVAL;
    351   1.45   thorpej 		goto out;
    352   1.45   thorpej 	}
    353   1.40   thorpej 
    354   1.38   thorpej 	cnt = auio.uio_resid;
    355   1.39   thorpej 	error = (*fp->f_ops->fo_write)(fp, offset, &auio, fp->f_cred, flags);
    356   1.22  christos 	if (error) {
    357   1.15       cgd 		if (auio.uio_resid != cnt && (error == ERESTART ||
    358   1.15       cgd 		    error == EINTR || error == EWOULDBLOCK))
    359   1.15       cgd 			error = 0;
    360   1.98        ad 		if (error == EPIPE) {
    361  1.118        ad 			mutex_enter(proc_lock);
    362  1.115        ad 			psignal(curproc, SIGPIPE);
    363  1.118        ad 			mutex_exit(proc_lock);
    364   1.98        ad 		}
    365   1.15       cgd 	}
    366   1.15       cgd 	cnt -= auio.uio_resid;
    367  1.105       dsl 	ktrgenio(fd, UIO_WRITE, buf, cnt, error);
    368   1.15       cgd 	*retval = cnt;
    369   1.45   thorpej  out:
    370  1.115        ad 	fd_putfile(fd);
    371   1.15       cgd 	return (error);
    372   1.15       cgd }
    373   1.15       cgd 
    374   1.15       cgd /*
    375   1.15       cgd  * Gather write system call
    376   1.15       cgd  */
    377   1.22  christos int
    378  1.110       dsl sys_writev(struct lwp *l, const struct sys_writev_args *uap, register_t *retval)
    379   1.20   thorpej {
    380  1.110       dsl 	/* {
    381   1.53     lukem 		syscallarg(int)				fd;
    382   1.53     lukem 		syscallarg(const struct iovec *)	iovp;
    383   1.53     lukem 		syscallarg(int)				iovcnt;
    384  1.110       dsl 	} */
    385  1.102       dsl 
    386  1.108        ad 	return do_filewritev(SCARG(uap, fd), SCARG(uap, iovp),
    387  1.102       dsl 	    SCARG(uap, iovcnt), NULL, FOF_UPDATE_OFFSET, retval);
    388  1.102       dsl }
    389  1.102       dsl 
    390  1.102       dsl int
    391  1.108        ad do_filewritev(int fd, const struct iovec *iovp, int iovcnt,
    392  1.102       dsl     off_t *offset, int flags, register_t *retval)
    393  1.102       dsl {
    394  1.102       dsl 	struct uio	auio;
    395  1.102       dsl 	struct iovec	*iov, *needfree = NULL, aiov[UIO_SMALLIOV];
    396  1.102       dsl 	int		i, error;
    397  1.102       dsl 	size_t		cnt;
    398  1.102       dsl 	u_int		iovlen;
    399   1.53     lukem 	struct file	*fp;
    400  1.102       dsl 	struct iovec	*ktriov = NULL;
    401  1.102       dsl 
    402  1.102       dsl 	if (iovcnt == 0)
    403  1.102       dsl 		return EINVAL;
    404   1.39   thorpej 
    405  1.115        ad 	if ((fp = fd_getfile(fd)) == NULL)
    406  1.102       dsl 		return EBADF;
    407   1.56   thorpej 
    408   1.70        pk 	if ((fp->f_flag & FWRITE) == 0) {
    409  1.115        ad 		fd_putfile(fd);
    410  1.102       dsl 		return EBADF;
    411   1.70        pk 	}
    412   1.39   thorpej 
    413  1.102       dsl 	if (offset == NULL)
    414  1.102       dsl 		offset = &fp->f_offset;
    415  1.102       dsl 	else {
    416  1.102       dsl 		struct vnode *vp = fp->f_data;
    417  1.102       dsl 		if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
    418  1.102       dsl 			error = ESPIPE;
    419  1.102       dsl 			goto out;
    420  1.102       dsl 		}
    421  1.102       dsl 		/*
    422  1.102       dsl 		 * Test that the device is seekable ?
    423  1.102       dsl 		 * XXX This works because no file systems actually
    424  1.102       dsl 		 * XXX take any action on the seek operation.
    425  1.102       dsl 		 */
    426  1.102       dsl 		error = VOP_SEEK(vp, fp->f_offset, *offset, fp->f_cred);
    427  1.102       dsl 		if (error != 0)
    428  1.102       dsl 			goto out;
    429  1.102       dsl 	}
    430   1.39   thorpej 
    431   1.42     perry 	iovlen = iovcnt * sizeof(struct iovec);
    432  1.102       dsl 	if (flags & FOF_IOV_SYSSPACE)
    433  1.102       dsl 		iov = __UNCONST(iovp);
    434  1.102       dsl 	else {
    435  1.102       dsl 		iov = aiov;
    436  1.102       dsl 		if ((u_int)iovcnt > UIO_SMALLIOV) {
    437  1.102       dsl 			if ((u_int)iovcnt > IOV_MAX) {
    438  1.102       dsl 				error = EINVAL;
    439  1.102       dsl 				goto out;
    440  1.102       dsl 			}
    441  1.103        ad 			iov = kmem_alloc(iovlen, KM_SLEEP);
    442  1.103        ad 			if (iov == NULL) {
    443  1.103        ad 				error = ENOMEM;
    444  1.103        ad 				goto out;
    445  1.103        ad 			}
    446  1.102       dsl 			needfree = iov;
    447   1.62  jdolecek 		}
    448  1.102       dsl 		error = copyin(iovp, iov, iovlen);
    449  1.102       dsl 		if (error)
    450  1.102       dsl 			goto done;
    451   1.45   thorpej 	}
    452   1.41    kleink 
    453   1.15       cgd 	auio.uio_iov = iov;
    454   1.34   mycroft 	auio.uio_iovcnt = iovcnt;
    455   1.15       cgd 	auio.uio_rw = UIO_WRITE;
    456  1.108        ad 	auio.uio_vmspace = curproc->p_vmspace;
    457  1.102       dsl 
    458   1.15       cgd 	auio.uio_resid = 0;
    459  1.102       dsl 	for (i = 0; i < iovcnt; i++, iov++) {
    460   1.15       cgd 		auio.uio_resid += iov->iov_len;
    461   1.40   thorpej 		/*
    462   1.40   thorpej 		 * Writes return ssize_t because -1 is returned on error.
    463   1.40   thorpej 		 * Therefore we must restrict the length to SSIZE_MAX to
    464   1.40   thorpej 		 * avoid garbage return values.
    465   1.40   thorpej 		 */
    466   1.40   thorpej 		if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
    467   1.15       cgd 			error = EINVAL;
    468   1.15       cgd 			goto done;
    469   1.15       cgd 		}
    470   1.15       cgd 	}
    471  1.102       dsl 
    472   1.15       cgd 	/*
    473   1.15       cgd 	 * if tracing, save a copy of iovec
    474   1.15       cgd 	 */
    475  1.104        ad 	if (ktrpoint(KTR_GENIO))  {
    476  1.103        ad 		ktriov = kmem_alloc(iovlen, KM_SLEEP);
    477  1.103        ad 		if (ktriov != NULL)
    478  1.103        ad 			memcpy(ktriov, auio.uio_iov, iovlen);
    479   1.15       cgd 	}
    480  1.104        ad 
    481   1.15       cgd 	cnt = auio.uio_resid;
    482   1.39   thorpej 	error = (*fp->f_ops->fo_write)(fp, offset, &auio, fp->f_cred, flags);
    483   1.22  christos 	if (error) {
    484   1.15       cgd 		if (auio.uio_resid != cnt && (error == ERESTART ||
    485   1.15       cgd 		    error == EINTR || error == EWOULDBLOCK))
    486   1.15       cgd 			error = 0;
    487   1.98        ad 		if (error == EPIPE) {
    488  1.118        ad 			mutex_enter(proc_lock);
    489  1.115        ad 			psignal(curproc, SIGPIPE);
    490  1.118        ad 			mutex_exit(proc_lock);
    491   1.98        ad 		}
    492   1.15       cgd 	}
    493   1.15       cgd 	cnt -= auio.uio_resid;
    494  1.102       dsl 	*retval = cnt;
    495  1.102       dsl 
    496   1.78  drochner 	if (ktriov != NULL) {
    497  1.104        ad 		ktrgeniov(fd, UIO_WRITE, ktriov, cnt, error);
    498  1.103        ad 		kmem_free(ktriov, iovlen);
    499   1.15       cgd 	}
    500  1.102       dsl 
    501   1.45   thorpej  done:
    502   1.15       cgd 	if (needfree)
    503  1.103        ad 		kmem_free(needfree, iovlen);
    504   1.45   thorpej  out:
    505  1.115        ad 	fd_putfile(fd);
    506   1.15       cgd 	return (error);
    507   1.15       cgd }
    508   1.15       cgd 
    509   1.15       cgd /*
    510   1.15       cgd  * Ioctl system call
    511   1.15       cgd  */
    512   1.15       cgd /* ARGSUSED */
    513   1.22  christos int
    514  1.110       dsl sys_ioctl(struct lwp *l, const struct sys_ioctl_args *uap, register_t *retval)
    515   1.20   thorpej {
    516  1.110       dsl 	/* {
    517   1.53     lukem 		syscallarg(int)		fd;
    518   1.53     lukem 		syscallarg(u_long)	com;
    519  1.100  christos 		syscallarg(void *)	data;
    520  1.110       dsl 	} */
    521   1.53     lukem 	struct file	*fp;
    522  1.103        ad 	proc_t		*p;
    523   1.53     lukem 	struct filedesc	*fdp;
    524   1.53     lukem 	u_long		com;
    525   1.53     lukem 	int		error;
    526   1.53     lukem 	u_int		size;
    527  1.100  christos 	void 		*data, *memp;
    528   1.53     lukem #define	STK_PARAMS	128
    529   1.53     lukem 	u_long		stkbuf[STK_PARAMS/sizeof(u_long)];
    530  1.115        ad 	fdfile_t	*ff;
    531   1.15       cgd 
    532   1.53     lukem 	error = 0;
    533   1.69   thorpej 	p = l->l_proc;
    534   1.15       cgd 	fdp = p->p_fd;
    535   1.56   thorpej 
    536  1.115        ad 	if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
    537   1.15       cgd 		return (EBADF);
    538   1.15       cgd 
    539   1.45   thorpej 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
    540   1.45   thorpej 		error = EBADF;
    541   1.65       scw 		com = 0;
    542   1.45   thorpej 		goto out;
    543   1.45   thorpej 	}
    544   1.15       cgd 
    545  1.115        ad 	ff = fdp->fd_ofiles[SCARG(uap, fd)];
    546   1.16       cgd 	switch (com = SCARG(uap, com)) {
    547   1.15       cgd 	case FIONCLEX:
    548  1.120      matt 		ff->ff_exclose = false;
    549   1.45   thorpej 		goto out;
    550   1.45   thorpej 
    551   1.15       cgd 	case FIOCLEX:
    552  1.120      matt 		ff->ff_exclose = true;
    553  1.120      matt 		fdp->fd_exclose = true;
    554   1.45   thorpej 		goto out;
    555   1.15       cgd 	}
    556   1.15       cgd 
    557   1.15       cgd 	/*
    558   1.15       cgd 	 * Interpret high order word to find amount of data to be
    559   1.15       cgd 	 * copied to/from the user's address space.
    560   1.15       cgd 	 */
    561   1.15       cgd 	size = IOCPARM_LEN(com);
    562   1.45   thorpej 	if (size > IOCPARM_MAX) {
    563   1.45   thorpej 		error = ENOTTY;
    564   1.45   thorpej 		goto out;
    565   1.45   thorpej 	}
    566   1.15       cgd 	memp = NULL;
    567   1.42     perry 	if (size > sizeof(stkbuf)) {
    568  1.103        ad 		memp = kmem_alloc(size, KM_SLEEP);
    569   1.15       cgd 		data = memp;
    570   1.15       cgd 	} else
    571  1.100  christos 		data = (void *)stkbuf;
    572   1.15       cgd 	if (com&IOC_IN) {
    573   1.15       cgd 		if (size) {
    574   1.31       cgd 			error = copyin(SCARG(uap, data), data, size);
    575   1.15       cgd 			if (error) {
    576   1.15       cgd 				if (memp)
    577  1.103        ad 					kmem_free(memp, size);
    578   1.45   thorpej 				goto out;
    579   1.15       cgd 			}
    580  1.104        ad 			ktrgenio(SCARG(uap, fd), UIO_WRITE, SCARG(uap, data),
    581  1.104        ad 			    size, 0);
    582   1.15       cgd 		} else
    583  1.100  christos 			*(void **)data = SCARG(uap, data);
    584   1.15       cgd 	} else if ((com&IOC_OUT) && size)
    585   1.15       cgd 		/*
    586   1.15       cgd 		 * Zero the buffer so the user always
    587   1.15       cgd 		 * gets back something deterministic.
    588   1.15       cgd 		 */
    589   1.44     perry 		memset(data, 0, size);
    590   1.15       cgd 	else if (com&IOC_VOID)
    591  1.100  christos 		*(void **)data = SCARG(uap, data);
    592   1.15       cgd 
    593   1.15       cgd 	switch (com) {
    594   1.15       cgd 
    595   1.15       cgd 	case FIONBIO:
    596  1.111       dsl 		FILE_LOCK(fp);
    597   1.79  jdolecek 		if (*(int *)data != 0)
    598   1.15       cgd 			fp->f_flag |= FNONBLOCK;
    599   1.15       cgd 		else
    600   1.15       cgd 			fp->f_flag &= ~FNONBLOCK;
    601  1.111       dsl 		FILE_UNLOCK(fp);
    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.111       dsl 		FILE_LOCK(fp);
    607   1.79  jdolecek 		if (*(int *)data != 0)
    608   1.15       cgd 			fp->f_flag |= FASYNC;
    609   1.15       cgd 		else
    610   1.15       cgd 			fp->f_flag &= ~FASYNC;
    611  1.111       dsl 		FILE_UNLOCK(fp);
    612  1.115        ad 		error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, data);
    613   1.15       cgd 		break;
    614   1.15       cgd 
    615   1.15       cgd 	default:
    616  1.115        ad 		error = (*fp->f_ops->fo_ioctl)(fp, com, data);
    617   1.15       cgd 		/*
    618   1.15       cgd 		 * Copy any data to user, size was
    619   1.15       cgd 		 * already set and checked above.
    620   1.15       cgd 		 */
    621   1.73       dsl 		if (error == 0 && (com&IOC_OUT) && size) {
    622   1.31       cgd 			error = copyout(data, SCARG(uap, data), size);
    623  1.104        ad 			ktrgenio(SCARG(uap, fd), UIO_READ, SCARG(uap, data),
    624  1.104        ad 			    size, error);
    625   1.73       dsl 		}
    626   1.15       cgd 		break;
    627   1.15       cgd 	}
    628   1.15       cgd 	if (memp)
    629  1.103        ad 		kmem_free(memp, size);
    630   1.45   thorpej  out:
    631  1.115        ad 	fd_putfile(SCARG(uap, fd));
    632   1.61    atatat 	switch (error) {
    633   1.61    atatat 	case -1:
    634   1.61    atatat 		printf("sys_ioctl: _IO%s%s('%c', %lu, %lu) returned -1: "
    635   1.61    atatat 		    "pid=%d comm=%s\n",
    636   1.61    atatat 		    (com & IOC_IN) ? "W" : "", (com & IOC_OUT) ? "R" : "",
    637   1.61    atatat 		    (char)IOCGROUP(com), (com & 0xff), IOCPARM_LEN(com),
    638   1.61    atatat 		    p->p_pid, p->p_comm);
    639   1.61    atatat 		/* FALLTHROUGH */
    640   1.61    atatat 	case EPASSTHROUGH:
    641   1.61    atatat 		error = ENOTTY;
    642   1.61    atatat 		/* FALLTHROUGH */
    643   1.61    atatat 	default:
    644   1.61    atatat 		return (error);
    645   1.61    atatat 	}
    646   1.15       cgd }
    647