xref: /netbsd-src/sys/compat/linux/common/linux_mtio.c (revision eab380320695b8901777f834601151f48dd29933)
1 /* $NetBSD: linux_mtio.c,v 1.9 2024/10/01 16:41:29 riastradh Exp $ */
2 
3 /*
4  * Copyright (c) 2005 Soren S. Jorvang.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions, and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: linux_mtio.c,v 1.9 2024/10/01 16:41:29 riastradh Exp $");
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/ioctl.h>
34 #include <sys/file.h>
35 #include <sys/filedesc.h>
36 #include <sys/mount.h>
37 #include <sys/proc.h>
38 
39 #include <sys/mtio.h>
40 
41 #include <sys/syscallargs.h>
42 
43 #include <compat/linux/common/linux_types.h>
44 #include <compat/linux/common/linux_ioctl.h>
45 #include <compat/linux/common/linux_signal.h>
46 #include <compat/linux/common/linux_mtio.h>
47 #include <compat/linux/common/linux_ipc.h>
48 #include <compat/linux/common/linux_sem.h>
49 
50 #include <compat/linux/linux_syscallargs.h>
51 
52 static const struct mtop_mapping {
53 	short	lop;
54 	short	op;
55 } mtop_map[] = {
56 	{ LINUX_MTFSF,		MTFSF },
57 	{ LINUX_MTBSF,		MTBSF },
58 	{ LINUX_MTFSR,		MTFSR },
59 	{ LINUX_MTBSR,		MTBSR },
60 	{ LINUX_MTWEOF,		MTWEOF },
61 	{ LINUX_MTREW,		MTREW },
62 	{ LINUX_MTOFFL,		MTOFFL },
63 	{ LINUX_MTNOP,		MTNOP },
64 	{ LINUX_MTRETEN,	MTRETEN },
65 	{ LINUX_MTEOM,		MTEOM },
66 	{ LINUX_MTERASE,	MTERASE },
67 	{ LINUX_MTSETBLK,	MTSETBSIZ },
68 	{ LINUX_MTSETDENSITY,	MTSETDNSTY },
69 	{ LINUX_MTCOMPRESSION,	MTCMPRESS },
70 	{ -1, -1 }
71 };
72 
73 int
74 linux_ioctl_mtio(struct lwp *l, const struct linux_sys_ioctl_args *uap,
75     register_t *retval)
76 {
77 	file_t *fp;
78 	u_long com = SCARG(uap, com);
79 	int i, error = 0;
80 	int (*ioctlf)(file_t *, u_long, void *);
81 	struct linux_mtop lmtop;
82 	struct linux_mtget lmtget;
83 	struct mtop mt;
84 
85 	if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
86 		return EBADF;
87 
88 	ioctlf = fp->f_ops->fo_ioctl;
89 
90 	*retval = 0;
91 	switch (com) {
92 	case LINUX_MTIOCTOP:
93 		error = copyin(SCARG(uap, data), &lmtop, sizeof lmtop);
94 		for (i = 0; mtop_map[i].lop >= 0; i++) {
95 			if (mtop_map[i].lop == lmtop.mt_op)
96 				break;
97 		}
98 
99 		if (mtop_map[i].lop == -1) {
100 			error = EINVAL;
101 			break;
102 		}
103 
104 		mt.mt_op = mtop_map[i].op;
105 		mt.mt_count = lmtop.mt_count;
106 		error = ioctlf(fp, MTIOCTOP, &mt);
107 		break;
108 	case LINUX_MTIOCGET:
109 		memset(&lmtget, 0, sizeof(lmtget));
110 		lmtget.mt_type = LINUX_MT_ISUNKNOWN;
111 		lmtget.mt_resid = 0;
112 		lmtget.mt_dsreg = 0;
113 		lmtget.mt_gstat = 0;
114 		lmtget.mt_erreg = 0;
115 		lmtget.mt_fileno = 0;
116 		lmtget.mt_blkno = 0;
117                 error = copyout(&lmtget, SCARG(uap, data), sizeof lmtget);
118 		break;
119 	case LINUX_MTIOCPOS:
120 	default:
121 		printf("linux_mtio unsupported ioctl 0x%lx\n", com);
122 		error = ENODEV;
123 		break;
124 	}
125 
126 	fd_putfile(SCARG(uap, fd));
127 
128 	return error;
129 }
130