]> granicus.if.org Git - strace/blob - block.c
ioctl: assume that all ioctl commands have unsigned int type
[strace] / block.c
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 #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
38 struct 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
54 #ifndef BLKTRACESTOP
55 #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
82 #include "xlat/blkpg_ops.h"
83
84 static void
85 print_blkpg_req(struct tcb *tcp, struct blkpg_ioctl_arg *blkpg)
86 {
87         struct blkpg_partition p;
88
89         tprints("{");
90         printxval(blkpg_ops, blkpg->op, "BLKPG_???");
91
92         tprintf(", flags=%d, datalen=%d, ",
93                 blkpg->flags, blkpg->datalen);
94
95         if (umove(tcp, (long) blkpg->data, &p) < 0)
96                 tprintf("%#lx}", (long) blkpg->data);
97         else
98                 tprintf("{start=%lld, length=%lld, pno=%d, "
99                         "devname=\"%.*s\", volname=\"%.*s\"}}",
100                         p.start, p.length, p.pno,
101                         (int) sizeof(p.devname), p.devname,
102                         (int) sizeof(p.volname), p.volname);
103 }
104
105 int
106 block_ioctl(struct tcb *tcp, const unsigned int code, long arg)
107 {
108         switch (code) {
109         /* take arg as a value, not as a pointer */
110         case BLKRASET:
111         case BLKFRASET:
112                 if (entering(tcp))
113                         tprintf(", %ld", arg);
114                 break;
115
116         /* take a signed int */
117         case BLKROSET:
118         case BLKBSZSET:
119 #ifdef FIFREEZE
120         /* First seen in linux-2.6.29 */
121         case FIFREEZE:
122         case FITHAW:
123 #endif
124                 if (entering(tcp)) {
125                         int val;
126                         if (umove(tcp, arg, &val) < 0)
127                                 tprintf(", %#lx", arg);
128                         else
129                                 tprintf(", %d", val);
130                 }
131                 break;
132
133         /* returns an unsigned short */
134         case BLKSECTGET:
135                 if (exiting(tcp)) {
136                         unsigned short val;
137                         if (syserror(tcp) || umove(tcp, arg, &val) < 0)
138                                 tprintf(", %#lx", arg);
139                         else
140                                 tprintf(", %u", (unsigned)val);
141                 }
142                 break;
143
144         /* return a signed int */
145         case BLKROGET:
146         case BLKBSZGET:
147         case BLKSSZGET:
148         case BLKALIGNOFF:
149                 if (exiting(tcp)) {
150                         int val;
151                         if (syserror(tcp) || umove(tcp, arg, &val) < 0)
152                                 tprintf(", %#lx", arg);
153                         else
154                                 tprintf(", %d", val);
155                 }
156                 break;
157
158         /* return an unsigned int */
159         case BLKPBSZGET:
160         case BLKIOMIN:
161         case BLKIOOPT:
162         case BLKDISCARDZEROES:
163                 if (exiting(tcp)) {
164                         unsigned int val;
165                         if (syserror(tcp) || umove(tcp, arg, &val) < 0)
166                                 tprintf(", %#lx", arg);
167                         else
168                                 tprintf(", %u", val);
169                 }
170                 break;
171
172         /* return a signed long */
173         case BLKRAGET:
174         case BLKFRAGET:
175                 if (exiting(tcp)) {
176                         long val;
177                         if (syserror(tcp) || umove(tcp, arg, &val) < 0)
178                                 tprintf(", %#lx", arg);
179                         else
180                                 tprintf(", %ld", val);
181                 }
182                 break;
183
184         /* returns an unsigned long */
185         case BLKGETSIZE:
186                 if (exiting(tcp)) {
187                         unsigned long val;
188                         if (syserror(tcp) || umove(tcp, arg, &val) < 0)
189                                 tprintf(", %#lx", arg);
190                         else
191                                 tprintf(", %lu", val);
192                 }
193                 break;
194
195 #ifdef HAVE_BLKGETSIZE64
196         /* return an uint64_t */
197         case BLKGETSIZE64:
198                 if (exiting(tcp)) {
199                         uint64_t val;
200                         if (syserror(tcp) || umove(tcp, arg, &val) < 0)
201                                 tprintf(", %#lx", arg);
202                         else
203                                 tprintf(", %" PRIu64, val);
204                 }
205                 break;
206 #endif
207
208         /* More complex types */
209         case BLKDISCARD:
210         case BLKSECDISCARD:
211                 if (entering(tcp)) {
212                         uint64_t range[2];
213                         if (umove(tcp, arg, range) < 0)
214                                 tprintf(", %#lx", arg);
215                         else
216                                 tprintf(", {%" PRIx64 ", %" PRIx64 "}",
217                                         range[0], range[1]);
218                 }
219                 break;
220
221         case HDIO_GETGEO:
222                 if (exiting(tcp)) {
223                         struct hd_geometry geo;
224                         if (syserror(tcp) || umove(tcp, arg, &geo) < 0)
225                                 tprintf(", %#lx", arg);
226                         else
227                                 tprintf(", {heads=%u, sectors=%u, "
228                                         "cylinders=%u, start=%lu}",
229                                         (unsigned)geo.heads,
230                                         (unsigned)geo.sectors,
231                                         (unsigned)geo.cylinders,
232                                         geo.start);
233                 }
234                 break;
235
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 {
242                                 tprints(", ");
243                                 print_blkpg_req(tcp, &blkpg);
244                         }
245                 }
246                 break;
247
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);
253                         else
254                                 tprintf(", {act_mask=%u, buf_size=%u, "
255                                         "buf_nr=%u, start_lba=%" PRIu64 ", "
256                                         "end_lba=%" PRIu64 ", pid=%u}",
257                                         (unsigned)buts.act_mask, buts.buf_size,
258                                         buts.buf_nr, buts.start_lba,
259                                         buts.end_lba, buts.pid);
260                 }
261                 if (exiting(tcp)) {
262                         struct blk_user_trace_setup buts;
263                         if (syserror(tcp) || umove(tcp, arg, &buts) < 0)
264                                 tprintf(", %#lx", arg);
265                         else
266                                 tprintf(", {name=\"%.*s\"}",
267                                         (int) sizeof(buts.name), buts.name);
268                 }
269                 break;
270
271 #ifdef FITRIM
272         /* First seen in linux-2.6.37 */
273         case FITRIM:
274                 if (entering(tcp)) {
275                         struct fstrim_range fstrim;
276                         if (umove(tcp, arg, &fstrim))
277                                 tprintf(", %#lx", arg);
278                         else
279                                 tprintf(", {start=%#" PRIx64 ", len=%#" PRIx64 ", "
280                                         "minlen=%#" PRIx64 "}", (uint64_t) fstrim.start,
281                                         (uint64_t) fstrim.len, (uint64_t) fstrim.minlen);
282                 }
283                 break;
284 #endif
285
286         /* No arguments or unhandled */
287         case BLKTRACESTART:
288         case BLKTRACESTOP:
289         case BLKTRACETEARDOWN:
290         case BLKFLSBUF: /* Requires driver knowlege */
291         case BLKRRPART: /* No args */
292         default:
293                 if (entering(tcp))
294                         tprintf(", %#lx", arg);
295                 break;
296
297         };
298         return 1;
299 }