Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 1 | /* |
| 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" |
| 29 | #ifdef LINUX |
| 30 | #include <stdint.h> |
Dmitry V. Levin | be284ca | 2011-01-16 23:07:51 +0000 | [diff] [blame] | 31 | #include <inttypes.h> |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 32 | #include <linux/blkpg.h> |
| 33 | #include <linux/fs.h> |
| 34 | #include <linux/hdreg.h> |
| 35 | |
| 36 | /* ioctls <= 114 are present in Linux 2.4. The following ones have been |
| 37 | * added since then and headers containing them may not be available on |
| 38 | * every system. */ |
| 39 | |
| 40 | #define BLKTRACE_BDEV_SIZE 32 |
| 41 | struct blk_user_trace_setup { |
| 42 | char name[BLKTRACE_BDEV_SIZE]; /* output */ |
| 43 | uint16_t act_mask; /* input */ |
| 44 | uint32_t buf_size; /* input */ |
| 45 | uint32_t buf_nr; /* input */ |
| 46 | uint64_t start_lba; |
| 47 | uint64_t end_lba; |
| 48 | uint32_t pid; |
| 49 | }; |
| 50 | |
| 51 | #ifndef BLKTRACESETUP |
| 52 | #define BLKTRACESETUP _IOWR(0x12,115,struct blk_user_trace_setup) |
| 53 | #endif |
| 54 | #ifndef BLKTRACESTART |
| 55 | #define BLKTRACESTART _IO(0x12,116) |
| 56 | #endif |
Dmitry V. Levin | 4cee0af | 2011-04-07 19:58:10 +0000 | [diff] [blame] | 57 | #ifndef BLKTRACESTOP |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 58 | #define BLKTRACESTOP _IO(0x12,117) |
| 59 | #endif |
| 60 | #ifndef BLKTRACETEARDOWN |
| 61 | #define BLKTRACETEARDOWN _IO(0x12,118) |
| 62 | #endif |
| 63 | #ifndef BLKDISCARD |
| 64 | #define BLKDISCARD _IO(0x12,119) |
| 65 | #endif |
| 66 | #ifndef BLKIOMIN |
| 67 | #define BLKIOMIN _IO(0x12,120) |
| 68 | #endif |
| 69 | #ifndef BLKIOOPT |
| 70 | #define BLKIOOPT _IO(0x12,121) |
| 71 | #endif |
| 72 | #ifndef BLKALIGNOFF |
| 73 | #define BLKALIGNOFF _IO(0x12,122) |
| 74 | #endif |
| 75 | #ifndef BLKPBSZGET |
| 76 | #define BLKPBSZGET _IO(0x12,123) |
| 77 | #endif |
| 78 | #ifndef BLKDISCARDZEROES |
| 79 | #define BLKDISCARDZEROES _IO(0x12,124) |
| 80 | #endif |
| 81 | #ifndef BLKSECDISCARD |
| 82 | #define BLKSECDISCARD _IO(0x12,125) |
| 83 | #endif |
| 84 | |
| 85 | static const struct xlat blkpg_ops[] = { |
| 86 | { BLKPG_ADD_PARTITION, "BLKPG_ADD_PARTITION", }, |
| 87 | { BLKPG_DEL_PARTITION, "BLKPG_DEL_PARTITION", }, |
| 88 | { 0, NULL }, |
| 89 | }; |
| 90 | |
| 91 | static void |
| 92 | print_blkpg_req(struct tcb *tcp, struct blkpg_ioctl_arg *blkpg) |
| 93 | { |
| 94 | struct blkpg_partition p; |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 95 | |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 96 | tprints("{"); |
Dmitry V. Levin | be284ca | 2011-01-16 23:07:51 +0000 | [diff] [blame] | 97 | printxval(blkpg_ops, blkpg->op, "BLKPG_???"); |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 98 | |
Dmitry V. Levin | be284ca | 2011-01-16 23:07:51 +0000 | [diff] [blame] | 99 | tprintf(", flags=%d, datalen=%d, ", |
| 100 | blkpg->flags, blkpg->datalen); |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 101 | |
Dmitry V. Levin | be284ca | 2011-01-16 23:07:51 +0000 | [diff] [blame] | 102 | if (umove(tcp, (long) blkpg->data, &p) < 0) |
| 103 | tprintf("%#lx}", (long) blkpg->data); |
| 104 | else |
| 105 | tprintf("{start=%lld, length=%lld, pno=%d, " |
| 106 | "devname=\"%.*s\", volname=\"%.*s\"}}", |
| 107 | p.start, p.length, p.pno, |
| 108 | (int) sizeof(p.devname), p.devname, |
| 109 | (int) sizeof(p.volname), p.volname); |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | int |
| 113 | block_ioctl(struct tcb *tcp, long code, long arg) |
| 114 | { |
| 115 | switch (code) { |
Dmitry V. Levin | be284ca | 2011-01-16 23:07:51 +0000 | [diff] [blame] | 116 | /* take arg as a value, not as a pointer */ |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 117 | case BLKRASET: |
| 118 | case BLKFRASET: |
| 119 | if (entering(tcp)) |
| 120 | tprintf(", %ld", arg); |
| 121 | break; |
| 122 | |
Dmitry V. Levin | be284ca | 2011-01-16 23:07:51 +0000 | [diff] [blame] | 123 | /* take a signed int */ |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 124 | case BLKROSET: |
| 125 | case BLKBSZSET: |
| 126 | if (entering(tcp)) { |
Dmitry V. Levin | be284ca | 2011-01-16 23:07:51 +0000 | [diff] [blame] | 127 | int val; |
| 128 | if (umove(tcp, arg, &val) < 0) |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 129 | tprintf(", %#lx", arg); |
| 130 | else |
Dmitry V. Levin | be284ca | 2011-01-16 23:07:51 +0000 | [diff] [blame] | 131 | tprintf(", %d", val); |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 132 | } |
| 133 | break; |
| 134 | |
Dmitry V. Levin | be284ca | 2011-01-16 23:07:51 +0000 | [diff] [blame] | 135 | /* returns an unsigned short */ |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 136 | case BLKSECTGET: |
| 137 | if (exiting(tcp)) { |
Dmitry V. Levin | be284ca | 2011-01-16 23:07:51 +0000 | [diff] [blame] | 138 | unsigned short val; |
| 139 | if (syserror(tcp) || umove(tcp, arg, &val) < 0) |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 140 | tprintf(", %#lx", arg); |
| 141 | else |
Dmitry V. Levin | be284ca | 2011-01-16 23:07:51 +0000 | [diff] [blame] | 142 | tprintf(", %hu", val); |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 143 | } |
| 144 | break; |
| 145 | |
Dmitry V. Levin | be284ca | 2011-01-16 23:07:51 +0000 | [diff] [blame] | 146 | /* return a signed int */ |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 147 | case BLKROGET: |
| 148 | case BLKBSZGET: |
| 149 | case BLKSSZGET: |
| 150 | case BLKALIGNOFF: |
| 151 | if (exiting(tcp)) { |
Dmitry V. Levin | be284ca | 2011-01-16 23:07:51 +0000 | [diff] [blame] | 152 | int val; |
| 153 | if (syserror(tcp) || umove(tcp, arg, &val) < 0) |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 154 | tprintf(", %#lx", arg); |
| 155 | else |
Dmitry V. Levin | be284ca | 2011-01-16 23:07:51 +0000 | [diff] [blame] | 156 | tprintf(", %d", val); |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 157 | } |
| 158 | break; |
| 159 | |
Dmitry V. Levin | be284ca | 2011-01-16 23:07:51 +0000 | [diff] [blame] | 160 | /* return an unsigned int */ |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 161 | case BLKPBSZGET: |
| 162 | case BLKIOMIN: |
| 163 | case BLKIOOPT: |
| 164 | case BLKDISCARDZEROES: |
| 165 | if (exiting(tcp)) { |
Dmitry V. Levin | be284ca | 2011-01-16 23:07:51 +0000 | [diff] [blame] | 166 | unsigned int val; |
| 167 | if (syserror(tcp) || umove(tcp, arg, &val) < 0) |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 168 | tprintf(", %#lx", arg); |
| 169 | else |
Dmitry V. Levin | be284ca | 2011-01-16 23:07:51 +0000 | [diff] [blame] | 170 | tprintf(", %u", val); |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 171 | } |
| 172 | break; |
| 173 | |
Dmitry V. Levin | be284ca | 2011-01-16 23:07:51 +0000 | [diff] [blame] | 174 | /* return a signed long */ |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 175 | case BLKRAGET: |
| 176 | case BLKFRAGET: |
| 177 | if (exiting(tcp)) { |
Dmitry V. Levin | be284ca | 2011-01-16 23:07:51 +0000 | [diff] [blame] | 178 | long val; |
| 179 | if (syserror(tcp) || umove(tcp, arg, &val) < 0) |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 180 | tprintf(", %#lx", arg); |
| 181 | else |
Dmitry V. Levin | be284ca | 2011-01-16 23:07:51 +0000 | [diff] [blame] | 182 | tprintf(", %ld", val); |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 183 | } |
| 184 | break; |
| 185 | |
Dmitry V. Levin | be284ca | 2011-01-16 23:07:51 +0000 | [diff] [blame] | 186 | /* returns an unsigned long */ |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 187 | case BLKGETSIZE: |
| 188 | if (exiting(tcp)) { |
Dmitry V. Levin | be284ca | 2011-01-16 23:07:51 +0000 | [diff] [blame] | 189 | unsigned long val; |
| 190 | if (syserror(tcp) || umove(tcp, arg, &val) < 0) |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 191 | tprintf(", %#lx", arg); |
| 192 | else |
Dmitry V. Levin | be284ca | 2011-01-16 23:07:51 +0000 | [diff] [blame] | 193 | tprintf(", %lu", val); |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 194 | } |
| 195 | break; |
| 196 | |
Dmitry V. Levin | 25caa31 | 2011-08-16 21:36:16 +0000 | [diff] [blame] | 197 | #ifdef HAVE_BLKGETSIZE64 |
Dmitry V. Levin | be284ca | 2011-01-16 23:07:51 +0000 | [diff] [blame] | 198 | /* return an uint64_t */ |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 199 | case BLKGETSIZE64: |
| 200 | if (exiting(tcp)) { |
Dmitry V. Levin | be284ca | 2011-01-16 23:07:51 +0000 | [diff] [blame] | 201 | uint64_t val; |
| 202 | if (syserror(tcp) || umove(tcp, arg, &val) < 0) |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 203 | tprintf(", %#lx", arg); |
| 204 | else |
Dmitry V. Levin | be284ca | 2011-01-16 23:07:51 +0000 | [diff] [blame] | 205 | tprintf(", %" PRIu64, val); |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 206 | } |
| 207 | break; |
Denys Vlasenko | 5bd67c8 | 2011-08-15 11:36:09 +0200 | [diff] [blame] | 208 | #endif |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 209 | |
| 210 | /* More complex types */ |
| 211 | case BLKDISCARD: |
| 212 | case BLKSECDISCARD: |
| 213 | if (entering(tcp)) { |
| 214 | uint64_t range[2]; |
| 215 | if (umove(tcp, arg, range) < 0) |
| 216 | tprintf(", %#lx", arg); |
| 217 | else |
Dmitry V. Levin | be284ca | 2011-01-16 23:07:51 +0000 | [diff] [blame] | 218 | tprintf(", {%" PRIx64 ", %" PRIx64 "}", |
| 219 | range[0], range[1]); |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 220 | } |
| 221 | break; |
| 222 | |
| 223 | case HDIO_GETGEO: |
| 224 | if (exiting(tcp)) { |
| 225 | struct hd_geometry geo; |
Dmitry V. Levin | be284ca | 2011-01-16 23:07:51 +0000 | [diff] [blame] | 226 | if (syserror(tcp) || umove(tcp, arg, &geo) < 0) |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 227 | tprintf(", %#lx", arg); |
| 228 | else |
| 229 | tprintf(", {heads=%hhu, sectors=%hhu, " |
| 230 | "cylinders=%hu, start=%lu}", |
| 231 | geo.heads, geo.sectors, |
| 232 | geo.cylinders, geo.start); |
| 233 | } |
| 234 | break; |
Dmitry V. Levin | be284ca | 2011-01-16 23:07:51 +0000 | [diff] [blame] | 235 | |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 236 | case BLKPG: |
| 237 | if (entering(tcp)) { |
| 238 | struct blkpg_ioctl_arg blkpg; |
| 239 | if (umove(tcp, arg, &blkpg) < 0) |
| 240 | tprintf(", %#lx", arg); |
| 241 | else { |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 242 | tprints(", "); |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 243 | print_blkpg_req(tcp, &blkpg); |
| 244 | } |
| 245 | } |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 246 | break; |
Dmitry V. Levin | be284ca | 2011-01-16 23:07:51 +0000 | [diff] [blame] | 247 | |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 248 | case BLKTRACESETUP: |
| 249 | if (entering(tcp)) { |
| 250 | struct blk_user_trace_setup buts; |
| 251 | if (umove(tcp, arg, &buts) < 0) |
| 252 | tprintf(", %#lx", arg); |
Dmitry V. Levin | be284ca | 2011-01-16 23:07:51 +0000 | [diff] [blame] | 253 | else |
| 254 | tprintf(", {act_mask=%hu, buf_size=%u, " |
| 255 | "buf_nr=%u, start_lba=%" PRIu64 ", " |
| 256 | "end_lba=%" PRIu64 ", pid=%u}", |
| 257 | buts.act_mask, buts.buf_size, |
| 258 | buts.buf_nr, buts.start_lba, |
| 259 | buts.end_lba, buts.pid); |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 260 | } |
| 261 | if (exiting(tcp)) { |
| 262 | struct blk_user_trace_setup buts; |
Dmitry V. Levin | be284ca | 2011-01-16 23:07:51 +0000 | [diff] [blame] | 263 | if (syserror(tcp) || umove(tcp, arg, &buts) < 0) |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 264 | tprintf(", %#lx", arg); |
| 265 | else |
Dmitry V. Levin | be284ca | 2011-01-16 23:07:51 +0000 | [diff] [blame] | 266 | tprintf(", {name=\"%.*s\"}", |
| 267 | (int) sizeof(buts.name), buts.name); |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 268 | } |
| 269 | break; |
Dmitry V. Levin | be284ca | 2011-01-16 23:07:51 +0000 | [diff] [blame] | 270 | |
Dmitry V. Levin | 4ef6db4 | 2011-01-15 20:15:31 +0000 | [diff] [blame] | 271 | /* No arguments or unhandled */ |
| 272 | case BLKTRACESTART: |
| 273 | case BLKTRACESTOP: |
| 274 | case BLKTRACETEARDOWN: |
| 275 | case BLKFLSBUF: /* Requires driver knowlege */ |
| 276 | case BLKRRPART: /* No args */ |
| 277 | default: |
| 278 | if (entering(tcp)) |
| 279 | tprintf(", %#lx", arg); |
| 280 | break; |
| 281 | |
| 282 | }; |
| 283 | return 1; |
| 284 | } |
| 285 | #endif /* LINUX */ |