blob: f40b75c6e4f3ec321dc7a7f3ed1370cd3e3e1450 [file] [log] [blame]
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +00001/*
2 * Copyright (c) 2009, 2010 Jeff Mahoney <jeffm@suse.com>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "defs.h"
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +000029#include <linux/blkpg.h>
30#include <linux/fs.h>
31#include <linux/hdreg.h>
32
33/* ioctls <= 114 are present in Linux 2.4. The following ones have been
34 * added since then and headers containing them may not be available on
35 * every system. */
36
37#define BLKTRACE_BDEV_SIZE 32
38struct blk_user_trace_setup {
39 char name[BLKTRACE_BDEV_SIZE]; /* output */
40 uint16_t act_mask; /* input */
41 uint32_t buf_size; /* input */
42 uint32_t buf_nr; /* input */
43 uint64_t start_lba;
44 uint64_t end_lba;
45 uint32_t pid;
46};
47
48#ifndef BLKTRACESETUP
49#define BLKTRACESETUP _IOWR(0x12,115,struct blk_user_trace_setup)
50#endif
51#ifndef BLKTRACESTART
52#define BLKTRACESTART _IO(0x12,116)
53#endif
Dmitry V. Levin4cee0af2011-04-07 19:58:10 +000054#ifndef BLKTRACESTOP
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +000055#define BLKTRACESTOP _IO(0x12,117)
56#endif
57#ifndef BLKTRACETEARDOWN
58#define BLKTRACETEARDOWN _IO(0x12,118)
59#endif
60#ifndef BLKDISCARD
61#define BLKDISCARD _IO(0x12,119)
62#endif
63#ifndef BLKIOMIN
64#define BLKIOMIN _IO(0x12,120)
65#endif
66#ifndef BLKIOOPT
67#define BLKIOOPT _IO(0x12,121)
68#endif
69#ifndef BLKALIGNOFF
70#define BLKALIGNOFF _IO(0x12,122)
71#endif
72#ifndef BLKPBSZGET
73#define BLKPBSZGET _IO(0x12,123)
74#endif
75#ifndef BLKDISCARDZEROES
76#define BLKDISCARDZEROES _IO(0x12,124)
77#endif
78#ifndef BLKSECDISCARD
79#define BLKSECDISCARD _IO(0x12,125)
80#endif
81
82static const struct xlat blkpg_ops[] = {
Dmitry V. Levinbce0cc62014-02-05 01:33:50 +000083 XLAT(BLKPG_ADD_PARTITION),
84 XLAT(BLKPG_DEL_PARTITION),
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +000085 { 0, NULL },
86};
87
88static void
89print_blkpg_req(struct tcb *tcp, struct blkpg_ioctl_arg *blkpg)
90{
91 struct blkpg_partition p;
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +000092
Denys Vlasenko60fe8c12011-09-01 10:00:28 +020093 tprints("{");
Dmitry V. Levinbe284ca2011-01-16 23:07:51 +000094 printxval(blkpg_ops, blkpg->op, "BLKPG_???");
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +000095
Dmitry V. Levinbe284ca2011-01-16 23:07:51 +000096 tprintf(", flags=%d, datalen=%d, ",
97 blkpg->flags, blkpg->datalen);
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +000098
Dmitry V. Levinbe284ca2011-01-16 23:07:51 +000099 if (umove(tcp, (long) blkpg->data, &p) < 0)
100 tprintf("%#lx}", (long) blkpg->data);
101 else
102 tprintf("{start=%lld, length=%lld, pno=%d, "
103 "devname=\"%.*s\", volname=\"%.*s\"}}",
104 p.start, p.length, p.pno,
105 (int) sizeof(p.devname), p.devname,
106 (int) sizeof(p.volname), p.volname);
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000107}
108
109int
110block_ioctl(struct tcb *tcp, long code, long arg)
111{
112 switch (code) {
Dmitry V. Levinbe284ca2011-01-16 23:07:51 +0000113 /* take arg as a value, not as a pointer */
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000114 case BLKRASET:
115 case BLKFRASET:
116 if (entering(tcp))
117 tprintf(", %ld", arg);
118 break;
119
Dmitry V. Levinbe284ca2011-01-16 23:07:51 +0000120 /* take a signed int */
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000121 case BLKROSET:
122 case BLKBSZSET:
123 if (entering(tcp)) {
Dmitry V. Levinbe284ca2011-01-16 23:07:51 +0000124 int val;
125 if (umove(tcp, arg, &val) < 0)
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000126 tprintf(", %#lx", arg);
127 else
Dmitry V. Levinbe284ca2011-01-16 23:07:51 +0000128 tprintf(", %d", val);
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000129 }
130 break;
131
Dmitry V. Levinbe284ca2011-01-16 23:07:51 +0000132 /* returns an unsigned short */
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000133 case BLKSECTGET:
134 if (exiting(tcp)) {
Dmitry V. Levinbe284ca2011-01-16 23:07:51 +0000135 unsigned short val;
136 if (syserror(tcp) || umove(tcp, arg, &val) < 0)
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000137 tprintf(", %#lx", arg);
138 else
Denys Vlasenko61d62cf2012-04-16 18:12:27 +0200139 tprintf(", %u", (unsigned)val);
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000140 }
141 break;
142
Dmitry V. Levinbe284ca2011-01-16 23:07:51 +0000143 /* return a signed int */
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000144 case BLKROGET:
145 case BLKBSZGET:
146 case BLKSSZGET:
147 case BLKALIGNOFF:
148 if (exiting(tcp)) {
Dmitry V. Levinbe284ca2011-01-16 23:07:51 +0000149 int val;
150 if (syserror(tcp) || umove(tcp, arg, &val) < 0)
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000151 tprintf(", %#lx", arg);
152 else
Dmitry V. Levinbe284ca2011-01-16 23:07:51 +0000153 tprintf(", %d", val);
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000154 }
155 break;
156
Dmitry V. Levinbe284ca2011-01-16 23:07:51 +0000157 /* return an unsigned int */
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000158 case BLKPBSZGET:
159 case BLKIOMIN:
160 case BLKIOOPT:
161 case BLKDISCARDZEROES:
162 if (exiting(tcp)) {
Dmitry V. Levinbe284ca2011-01-16 23:07:51 +0000163 unsigned int val;
164 if (syserror(tcp) || umove(tcp, arg, &val) < 0)
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000165 tprintf(", %#lx", arg);
166 else
Dmitry V. Levinbe284ca2011-01-16 23:07:51 +0000167 tprintf(", %u", val);
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000168 }
169 break;
170
Dmitry V. Levinbe284ca2011-01-16 23:07:51 +0000171 /* return a signed long */
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000172 case BLKRAGET:
173 case BLKFRAGET:
174 if (exiting(tcp)) {
Dmitry V. Levinbe284ca2011-01-16 23:07:51 +0000175 long val;
176 if (syserror(tcp) || umove(tcp, arg, &val) < 0)
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000177 tprintf(", %#lx", arg);
178 else
Dmitry V. Levinbe284ca2011-01-16 23:07:51 +0000179 tprintf(", %ld", val);
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000180 }
181 break;
182
Dmitry V. Levinbe284ca2011-01-16 23:07:51 +0000183 /* returns an unsigned long */
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000184 case BLKGETSIZE:
185 if (exiting(tcp)) {
Dmitry V. Levinbe284ca2011-01-16 23:07:51 +0000186 unsigned long val;
187 if (syserror(tcp) || umove(tcp, arg, &val) < 0)
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000188 tprintf(", %#lx", arg);
189 else
Dmitry V. Levinbe284ca2011-01-16 23:07:51 +0000190 tprintf(", %lu", val);
Denys Vlasenko76729462013-03-05 17:46:19 +0100191 }
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000192 break;
193
Dmitry V. Levin25caa312011-08-16 21:36:16 +0000194#ifdef HAVE_BLKGETSIZE64
Dmitry V. Levinbe284ca2011-01-16 23:07:51 +0000195 /* return an uint64_t */
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000196 case BLKGETSIZE64:
197 if (exiting(tcp)) {
Dmitry V. Levinbe284ca2011-01-16 23:07:51 +0000198 uint64_t val;
199 if (syserror(tcp) || umove(tcp, arg, &val) < 0)
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000200 tprintf(", %#lx", arg);
201 else
Dmitry V. Levinbe284ca2011-01-16 23:07:51 +0000202 tprintf(", %" PRIu64, val);
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000203 }
204 break;
Denys Vlasenko5bd67c82011-08-15 11:36:09 +0200205#endif
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000206
207 /* More complex types */
208 case BLKDISCARD:
209 case BLKSECDISCARD:
210 if (entering(tcp)) {
211 uint64_t range[2];
212 if (umove(tcp, arg, range) < 0)
213 tprintf(", %#lx", arg);
214 else
Dmitry V. Levinbe284ca2011-01-16 23:07:51 +0000215 tprintf(", {%" PRIx64 ", %" PRIx64 "}",
216 range[0], range[1]);
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000217 }
218 break;
219
220 case HDIO_GETGEO:
221 if (exiting(tcp)) {
222 struct hd_geometry geo;
Dmitry V. Levinbe284ca2011-01-16 23:07:51 +0000223 if (syserror(tcp) || umove(tcp, arg, &geo) < 0)
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000224 tprintf(", %#lx", arg);
225 else
Denys Vlasenko61d62cf2012-04-16 18:12:27 +0200226 tprintf(", {heads=%u, sectors=%u, "
227 "cylinders=%u, start=%lu}",
228 (unsigned)geo.heads,
229 (unsigned)geo.sectors,
230 (unsigned)geo.cylinders,
231 geo.start);
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000232 }
233 break;
Dmitry V. Levinbe284ca2011-01-16 23:07:51 +0000234
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000235 case BLKPG:
236 if (entering(tcp)) {
237 struct blkpg_ioctl_arg blkpg;
238 if (umove(tcp, arg, &blkpg) < 0)
239 tprintf(", %#lx", arg);
240 else {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200241 tprints(", ");
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000242 print_blkpg_req(tcp, &blkpg);
243 }
244 }
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000245 break;
Dmitry V. Levinbe284ca2011-01-16 23:07:51 +0000246
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000247 case BLKTRACESETUP:
248 if (entering(tcp)) {
249 struct blk_user_trace_setup buts;
250 if (umove(tcp, arg, &buts) < 0)
251 tprintf(", %#lx", arg);
Dmitry V. Levinbe284ca2011-01-16 23:07:51 +0000252 else
Denys Vlasenko61d62cf2012-04-16 18:12:27 +0200253 tprintf(", {act_mask=%u, buf_size=%u, "
Dmitry V. Levinbe284ca2011-01-16 23:07:51 +0000254 "buf_nr=%u, start_lba=%" PRIu64 ", "
255 "end_lba=%" PRIu64 ", pid=%u}",
Denys Vlasenko61d62cf2012-04-16 18:12:27 +0200256 (unsigned)buts.act_mask, buts.buf_size,
Dmitry V. Levinbe284ca2011-01-16 23:07:51 +0000257 buts.buf_nr, buts.start_lba,
258 buts.end_lba, buts.pid);
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000259 }
260 if (exiting(tcp)) {
261 struct blk_user_trace_setup buts;
Dmitry V. Levinbe284ca2011-01-16 23:07:51 +0000262 if (syserror(tcp) || umove(tcp, arg, &buts) < 0)
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000263 tprintf(", %#lx", arg);
264 else
Dmitry V. Levinbe284ca2011-01-16 23:07:51 +0000265 tprintf(", {name=\"%.*s\"}",
266 (int) sizeof(buts.name), buts.name);
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000267 }
268 break;
Dmitry V. Levinbe284ca2011-01-16 23:07:51 +0000269
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000270 /* No arguments or unhandled */
271 case BLKTRACESTART:
272 case BLKTRACESTOP:
273 case BLKTRACETEARDOWN:
274 case BLKFLSBUF: /* Requires driver knowlege */
275 case BLKRRPART: /* No args */
276 default:
277 if (entering(tcp))
278 tprintf(", %#lx", arg);
279 break;
280
281 };
282 return 1;
283}