Home | History | Annotate | Line # | Download | only in common
linux_mtio.c revision 1.5
      1 /* $NetBSD: linux_mtio.c,v 1.5 2007/10/19 18:52:11 njoly 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.5 2007/10/19 18:52:11 njoly 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, struct linux_sys_ioctl_args *uap,
     75     register_t *retval)
     76 {
     77 	struct file *fp;
     78 	struct filedesc *fdp;
     79 	u_long com = SCARG(uap, com);
     80 	int i, error = 0;
     81 	int (*ioctlf)(struct file *, u_long, void *, struct lwp *);
     82 	struct linux_mtop lmtop;
     83 	struct linux_mtget lmtget;
     84 	struct mtop mt;
     85 
     86         fdp = l->l_proc->p_fd;
     87 	if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
     88 		return EBADF;
     89 
     90 	FILE_USE(fp);
     91 	ioctlf = fp->f_ops->fo_ioctl;
     92 
     93 	*retval = 0;
     94 	switch (com) {
     95 	case LINUX_MTIOCTOP:
     96 		error = copyin(SCARG(uap, data), &lmtop, sizeof lmtop);
     97 		for (i = 0; mtop_map[i].lop >= 0; i++) {
     98 			if (mtop_map[i].lop == lmtop.mt_op)
     99 				break;
    100 		}
    101 
    102 		if (mtop_map[i].lop == -1) {
    103 			error = EINVAL;
    104 			break;
    105 		}
    106 
    107 		mt.mt_op = mtop_map[i].op;
    108 		mt.mt_count = lmtop.mt_count;
    109 		error = ioctlf(fp, MTIOCTOP, (void *)&mt, l);
    110 		break;
    111 	case LINUX_MTIOCGET:
    112 		lmtget.mt_type = LINUX_MT_ISUNKNOWN;
    113 		lmtget.mt_resid = 0;
    114 		lmtget.mt_dsreg = 0;
    115 		lmtget.mt_gstat = 0;
    116 		lmtget.mt_erreg = 0;
    117 		lmtget.mt_fileno = 0;
    118 		lmtget.mt_blkno = 0;
    119                 error = copyout(&lmtget, SCARG(uap, data), sizeof lmtget);
    120 		break;
    121 	case LINUX_MTIOCPOS:
    122 	default:
    123 		printf("linux_mtio unsupported ioctl 0x%lx\n", com);
    124 		error = ENODEV;
    125 		break;
    126 	}
    127 
    128 	FILE_UNUSE(fp, l);
    129 
    130 	return error;
    131 }
    132