1 /*	$OpenBSD: linux_blkio.c,v 1.5 2002/03/14 01:26:50 millert Exp $	*/
2 /*	$NetBSD: linux_blkio.c,v 1.3 2001/01/18 17:48:04 tv Exp $	*/
3 
4 /*
5  * Copyright (c) 2001 Wasabi Systems, Inc.
6  * All rights reserved.
7  *
8  * Written by Frank van der Linden for Wasabi Systems, Inc.
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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed for the NetBSD Project by
21  *      Wasabi Systems, Inc.
22  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
23  *    or promote products derived from this software without specific prior
24  *    written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/ioctl.h>
42 #include <sys/file.h>
43 #include <sys/filedesc.h>
44 #include <sys/mount.h>
45 #include <sys/proc.h>
46 #include <sys/disklabel.h>
47 
48 #include <sys/syscallargs.h>
49 
50 #include <compat/linux/linux_types.h>
51 #include <compat/linux/linux_ioctl.h>
52 #include <compat/linux/linux_signal.h>
53 #include <compat/linux/linux_util.h>
54 #include <compat/linux/linux_blkio.h>
55 
56 #include <compat/linux/linux_syscallargs.h>
57 
58 int
linux_ioctl_blkio(struct proc * p,struct linux_sys_ioctl_args * uap,register_t * retval)59 linux_ioctl_blkio(struct proc *p, struct linux_sys_ioctl_args *uap,
60 		  register_t *retval)
61 {
62 	u_long com;
63 	long size;
64 	int error;
65 	struct filedesc *fdp;
66 	struct file *fp;
67 	int (*ioctlf)(struct file *, u_long, caddr_t, struct proc *);
68 	struct partinfo partp;
69 	struct disklabel label;
70 
71         fdp = p->p_fd;
72 	if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
73 		return (EBADF);
74 	FREF(fp);
75 	error = 0;
76 	ioctlf = fp->f_ops->fo_ioctl;
77 	com = SCARG(uap, com);
78 
79 	switch (com) {
80 	case LINUX_BLKGETSIZE:
81 		/*
82 		 * Try to get the partition size of this device. If that
83 		 * fails, it may be a disk without label; try to get
84 		 * the default label and compute the size from it.
85 		 */
86 		error = ioctlf(fp, DIOCGPART, (caddr_t)&partp, p);
87 		if (error != 0) {
88 			error = ioctlf(fp, DIOCGDINFO, (caddr_t)&label, p);
89 			if (error != 0)
90 				break;
91 			size = label.d_nsectors * label.d_ntracks *
92 			    label.d_ncylinders;
93 		} else
94 			size = partp.part->p_size;
95 		error = copyout(&size, SCARG(uap, data), sizeof size);
96 		break;
97 	case LINUX_BLKSECTGET:
98 		error = ioctlf(fp, DIOCGDINFO, (caddr_t)&label, p);
99 		if (error != 0)
100 			break;
101 		error = copyout(&label.d_secsize, SCARG(uap, data),
102 		    sizeof label.d_secsize);
103 		break;
104 	case LINUX_BLKROSET:
105 	case LINUX_BLKROGET:
106 	case LINUX_BLKRRPART:
107 	case LINUX_BLKFLSBUF:
108 	case LINUX_BLKRASET:
109 	case LINUX_BLKRAGET:
110 	case LINUX_BLKFRASET:
111 	case LINUX_BLKFRAGET:
112 	case LINUX_BLKSECTSET:
113 	case LINUX_BLKSSZGET:
114 	case LINUX_BLKPG:
115 	default:
116 		error = ENOTTY;
117 	}
118 
119 	FRELE(fp);
120 	return error;
121 }
122