1*8d10f962Schristos /* $NetBSD: linux_blkio.c,v 1.18 2015/12/08 20:36:14 christos Exp $ */
2da9e8a98Sfvdl
3da9e8a98Sfvdl /*
407ca2a03Sfvdl * Copyright (c) 2001 Wasabi Systems, Inc.
5da9e8a98Sfvdl * All rights reserved.
6da9e8a98Sfvdl *
7da9e8a98Sfvdl * Written by Frank van der Linden for Wasabi Systems, Inc.
8da9e8a98Sfvdl *
9da9e8a98Sfvdl * Redistribution and use in source and binary forms, with or without
10da9e8a98Sfvdl * modification, are permitted provided that the following conditions
11da9e8a98Sfvdl * are met:
12da9e8a98Sfvdl * 1. Redistributions of source code must retain the above copyright
13da9e8a98Sfvdl * notice, this list of conditions and the following disclaimer.
14da9e8a98Sfvdl * 2. Redistributions in binary form must reproduce the above copyright
15da9e8a98Sfvdl * notice, this list of conditions and the following disclaimer in the
16da9e8a98Sfvdl * documentation and/or other materials provided with the distribution.
17da9e8a98Sfvdl * 3. All advertising materials mentioning features or use of this software
18da9e8a98Sfvdl * must display the following acknowledgement:
19da9e8a98Sfvdl * This product includes software developed for the NetBSD Project by
20da9e8a98Sfvdl * Wasabi Systems, Inc.
21da9e8a98Sfvdl * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22da9e8a98Sfvdl * or promote products derived from this software without specific prior
23da9e8a98Sfvdl * written permission.
24da9e8a98Sfvdl *
25da9e8a98Sfvdl * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26da9e8a98Sfvdl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27da9e8a98Sfvdl * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28da9e8a98Sfvdl * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29da9e8a98Sfvdl * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30da9e8a98Sfvdl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31da9e8a98Sfvdl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32da9e8a98Sfvdl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33da9e8a98Sfvdl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34da9e8a98Sfvdl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35da9e8a98Sfvdl * POSSIBILITY OF SUCH DAMAGE.
36da9e8a98Sfvdl */
37da9e8a98Sfvdl
38dab6ef8bSlukem #include <sys/cdefs.h>
39*8d10f962Schristos __KERNEL_RCSID(0, "$NetBSD: linux_blkio.c,v 1.18 2015/12/08 20:36:14 christos Exp $");
40dab6ef8bSlukem
41da9e8a98Sfvdl #include <sys/param.h>
42da9e8a98Sfvdl #include <sys/systm.h>
43da9e8a98Sfvdl #include <sys/ioctl.h>
44da9e8a98Sfvdl #include <sys/file.h>
45da9e8a98Sfvdl #include <sys/filedesc.h>
46da9e8a98Sfvdl #include <sys/mount.h>
47da9e8a98Sfvdl #include <sys/proc.h>
48da9e8a98Sfvdl #include <sys/disklabel.h>
49da9e8a98Sfvdl
50da9e8a98Sfvdl #include <sys/syscallargs.h>
51da9e8a98Sfvdl
52da9e8a98Sfvdl #include <compat/linux/common/linux_types.h>
53da9e8a98Sfvdl #include <compat/linux/common/linux_ioctl.h>
54da9e8a98Sfvdl #include <compat/linux/common/linux_signal.h>
55da9e8a98Sfvdl #include <compat/linux/common/linux_util.h>
56da9e8a98Sfvdl #include <compat/linux/common/linux_blkio.h>
57a478f23bSnjoly #include <compat/linux/common/linux_ipc.h>
58a478f23bSnjoly #include <compat/linux/common/linux_sem.h>
59da9e8a98Sfvdl
60da9e8a98Sfvdl #include <compat/linux/linux_syscallargs.h>
61da9e8a98Sfvdl
62da9e8a98Sfvdl int
linux_ioctl_blkio(struct lwp * l,const struct linux_sys_ioctl_args * uap,register_t * retval)637e2790cfSdsl linux_ioctl_blkio(struct lwp *l, const struct linux_sys_ioctl_args *uap,
64168cd830Schristos register_t *retval)
65da9e8a98Sfvdl {
66da9e8a98Sfvdl u_long com;
67da9e8a98Sfvdl long size;
68da9e8a98Sfvdl int error;
69a9ca7a37Sad file_t *fp;
70a9ca7a37Sad int (*ioctlf)(file_t *, u_long, void *);
71*8d10f962Schristos struct partinfo pi;
72da9e8a98Sfvdl struct disklabel label;
73da9e8a98Sfvdl
74a9ca7a37Sad if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
75da9e8a98Sfvdl return (EBADF);
76da9e8a98Sfvdl
77da9e8a98Sfvdl error = 0;
78da9e8a98Sfvdl ioctlf = fp->f_ops->fo_ioctl;
79da9e8a98Sfvdl com = SCARG(uap, com);
80da9e8a98Sfvdl
81da9e8a98Sfvdl switch (com) {
82da9e8a98Sfvdl case LINUX_BLKGETSIZE:
83da9e8a98Sfvdl /*
84da9e8a98Sfvdl * Try to get the partition size of this device. If that
85da9e8a98Sfvdl * fails, it may be a disk without label; try to get
86da9e8a98Sfvdl * the default label and compute the size from it.
87da9e8a98Sfvdl */
88*8d10f962Schristos error = ioctlf(fp, DIOCGPARTINFO, &pi);
89da9e8a98Sfvdl if (error != 0) {
90*8d10f962Schristos error = ioctlf(fp, DIOCGDINFO, &label);
91da9e8a98Sfvdl if (error != 0)
92da9e8a98Sfvdl break;
93da9e8a98Sfvdl size = label.d_nsectors * label.d_ntracks *
94da9e8a98Sfvdl label.d_ncylinders;
95da9e8a98Sfvdl } else
96*8d10f962Schristos size = pi.pi_size;
97da9e8a98Sfvdl error = copyout(&size, SCARG(uap, data), sizeof size);
98da9e8a98Sfvdl break;
99da9e8a98Sfvdl case LINUX_BLKSECTGET:
100*8d10f962Schristos error = ioctlf(fp, DIOCGDINFO, &label);
101da9e8a98Sfvdl if (error != 0)
102da9e8a98Sfvdl break;
103da9e8a98Sfvdl error = copyout(&label.d_secsize, SCARG(uap, data),
104da9e8a98Sfvdl sizeof label.d_secsize);
105da9e8a98Sfvdl break;
106da9e8a98Sfvdl case LINUX_BLKROSET:
107da9e8a98Sfvdl case LINUX_BLKROGET:
108da9e8a98Sfvdl case LINUX_BLKRRPART:
109da9e8a98Sfvdl case LINUX_BLKFLSBUF:
110da9e8a98Sfvdl case LINUX_BLKRASET:
111da9e8a98Sfvdl case LINUX_BLKRAGET:
112da9e8a98Sfvdl case LINUX_BLKFRASET:
113da9e8a98Sfvdl case LINUX_BLKFRAGET:
114da9e8a98Sfvdl case LINUX_BLKSECTSET:
115da9e8a98Sfvdl case LINUX_BLKSSZGET:
116da9e8a98Sfvdl case LINUX_BLKPG:
117da9e8a98Sfvdl default:
118da9e8a98Sfvdl error = ENOTTY;
119da9e8a98Sfvdl }
120da9e8a98Sfvdl
121a9ca7a37Sad fd_putfile(SCARG(uap, fd));
122da9e8a98Sfvdl
123da9e8a98Sfvdl return error;
124da9e8a98Sfvdl }
125