xref: /netbsd-src/sys/compat/linux/common/linux_mtio.c (revision eab380320695b8901777f834601151f48dd29933)
1*eab38032Sriastradh /* $NetBSD: linux_mtio.c,v 1.9 2024/10/01 16:41:29 riastradh Exp $ */
218d6a304Ssoren 
318d6a304Ssoren /*
418d6a304Ssoren  * Copyright (c) 2005 Soren S. Jorvang.  All rights reserved.
518d6a304Ssoren  *
618d6a304Ssoren  * Redistribution and use in source and binary forms, with or without
718d6a304Ssoren  * modification, are permitted provided that the following conditions
818d6a304Ssoren  * are met:
918d6a304Ssoren  * 1. Redistributions of source code must retain the above copyright
1018d6a304Ssoren  *    notice, this list of conditions, and the following disclaimer.
1118d6a304Ssoren  * 2. Redistributions in binary form must reproduce the above copyright
1218d6a304Ssoren  *    notice, this list of conditions and the following disclaimer in the
1318d6a304Ssoren  *    documentation and/or other materials provided with the distribution.
1418d6a304Ssoren  *
1518d6a304Ssoren  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1618d6a304Ssoren  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1718d6a304Ssoren  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1818d6a304Ssoren  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1918d6a304Ssoren  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2018d6a304Ssoren  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2118d6a304Ssoren  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2218d6a304Ssoren  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2318d6a304Ssoren  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2418d6a304Ssoren  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2518d6a304Ssoren  * SUCH DAMAGE.
2618d6a304Ssoren  */
2718d6a304Ssoren 
2818d6a304Ssoren #include <sys/cdefs.h>
29*eab38032Sriastradh __KERNEL_RCSID(0, "$NetBSD: linux_mtio.c,v 1.9 2024/10/01 16:41:29 riastradh Exp $");
3018d6a304Ssoren 
3118d6a304Ssoren #include <sys/param.h>
3218d6a304Ssoren #include <sys/systm.h>
3318d6a304Ssoren #include <sys/ioctl.h>
3418d6a304Ssoren #include <sys/file.h>
3518d6a304Ssoren #include <sys/filedesc.h>
3618d6a304Ssoren #include <sys/mount.h>
3718d6a304Ssoren #include <sys/proc.h>
3818d6a304Ssoren 
3918d6a304Ssoren #include <sys/mtio.h>
4018d6a304Ssoren 
4118d6a304Ssoren #include <sys/syscallargs.h>
4218d6a304Ssoren 
4318d6a304Ssoren #include <compat/linux/common/linux_types.h>
4418d6a304Ssoren #include <compat/linux/common/linux_ioctl.h>
4518d6a304Ssoren #include <compat/linux/common/linux_signal.h>
4618d6a304Ssoren #include <compat/linux/common/linux_mtio.h>
47a478f23bSnjoly #include <compat/linux/common/linux_ipc.h>
48a478f23bSnjoly #include <compat/linux/common/linux_sem.h>
4918d6a304Ssoren 
5018d6a304Ssoren #include <compat/linux/linux_syscallargs.h>
5118d6a304Ssoren 
5218d6a304Ssoren static const struct mtop_mapping {
5318d6a304Ssoren 	short	lop;
5418d6a304Ssoren 	short	op;
5518d6a304Ssoren } mtop_map[] = {
5618d6a304Ssoren 	{ LINUX_MTFSF,		MTFSF },
5718d6a304Ssoren 	{ LINUX_MTBSF,		MTBSF },
5818d6a304Ssoren 	{ LINUX_MTFSR,		MTFSR },
5918d6a304Ssoren 	{ LINUX_MTBSR,		MTBSR },
6018d6a304Ssoren 	{ LINUX_MTWEOF,		MTWEOF },
6118d6a304Ssoren 	{ LINUX_MTREW,		MTREW },
6218d6a304Ssoren 	{ LINUX_MTOFFL,		MTOFFL },
6318d6a304Ssoren 	{ LINUX_MTNOP,		MTNOP },
6418d6a304Ssoren 	{ LINUX_MTRETEN,	MTRETEN },
6518d6a304Ssoren 	{ LINUX_MTEOM,		MTEOM },
6618d6a304Ssoren 	{ LINUX_MTERASE,	MTERASE },
6718d6a304Ssoren 	{ LINUX_MTSETBLK,	MTSETBSIZ },
6818d6a304Ssoren 	{ LINUX_MTSETDENSITY,	MTSETDNSTY },
6918d6a304Ssoren 	{ LINUX_MTCOMPRESSION,	MTCMPRESS },
7018d6a304Ssoren 	{ -1, -1 }
7118d6a304Ssoren };
7218d6a304Ssoren 
7318d6a304Ssoren int
747e2790cfSdsl linux_ioctl_mtio(struct lwp *l, const struct linux_sys_ioctl_args *uap,
7518d6a304Ssoren     register_t *retval)
7618d6a304Ssoren {
77a9ca7a37Sad 	file_t *fp;
7818d6a304Ssoren 	u_long com = SCARG(uap, com);
7918d6a304Ssoren 	int i, error = 0;
80a9ca7a37Sad 	int (*ioctlf)(file_t *, u_long, void *);
8118d6a304Ssoren 	struct linux_mtop lmtop;
8218d6a304Ssoren 	struct linux_mtget lmtget;
8318d6a304Ssoren 	struct mtop mt;
8418d6a304Ssoren 
85a9ca7a37Sad 	if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
8618d6a304Ssoren 		return EBADF;
8718d6a304Ssoren 
8818d6a304Ssoren 	ioctlf = fp->f_ops->fo_ioctl;
8918d6a304Ssoren 
9018d6a304Ssoren 	*retval = 0;
9118d6a304Ssoren 	switch (com) {
9218d6a304Ssoren 	case LINUX_MTIOCTOP:
9318d6a304Ssoren 		error = copyin(SCARG(uap, data), &lmtop, sizeof lmtop);
9418d6a304Ssoren 		for (i = 0; mtop_map[i].lop >= 0; i++) {
9518d6a304Ssoren 			if (mtop_map[i].lop == lmtop.mt_op)
9618d6a304Ssoren 				break;
9718d6a304Ssoren 		}
9818d6a304Ssoren 
9918d6a304Ssoren 		if (mtop_map[i].lop == -1) {
10018d6a304Ssoren 			error = EINVAL;
10118d6a304Ssoren 			break;
10218d6a304Ssoren 		}
10318d6a304Ssoren 
10418d6a304Ssoren 		mt.mt_op = mtop_map[i].op;
10518d6a304Ssoren 		mt.mt_count = lmtop.mt_count;
106a9ca7a37Sad 		error = ioctlf(fp, MTIOCTOP, &mt);
10718d6a304Ssoren 		break;
10818d6a304Ssoren 	case LINUX_MTIOCGET:
10941aa5859Sriastradh 		memset(&lmtget, 0, sizeof(lmtget));
11018d6a304Ssoren 		lmtget.mt_type = LINUX_MT_ISUNKNOWN;
11118d6a304Ssoren 		lmtget.mt_resid = 0;
11218d6a304Ssoren 		lmtget.mt_dsreg = 0;
11318d6a304Ssoren 		lmtget.mt_gstat = 0;
11418d6a304Ssoren 		lmtget.mt_erreg = 0;
11518d6a304Ssoren 		lmtget.mt_fileno = 0;
11618d6a304Ssoren 		lmtget.mt_blkno = 0;
11718d6a304Ssoren                 error = copyout(&lmtget, SCARG(uap, data), sizeof lmtget);
11818d6a304Ssoren 		break;
11918d6a304Ssoren 	case LINUX_MTIOCPOS:
12018d6a304Ssoren 	default:
12118d6a304Ssoren 		printf("linux_mtio unsupported ioctl 0x%lx\n", com);
12218d6a304Ssoren 		error = ENODEV;
12318d6a304Ssoren 		break;
12418d6a304Ssoren 	}
12518d6a304Ssoren 
126a9ca7a37Sad 	fd_putfile(SCARG(uap, fd));
12718d6a304Ssoren 
12818d6a304Ssoren 	return error;
12918d6a304Ssoren }
130