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