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