]> granicus.if.org Git - strace/blob - block.c
Replace "(unsigned long) -1L" with -1UL
[strace] / block.c
1 /*
2  * Copyright (c) 2009, 2010 Jeff Mahoney <jeffm@suse.com>
3  * Copyright (c) 2011-2016 Dmitry V. Levin <ldv@altlinux.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include "defs.h"
30
31 #include DEF_MPERS_TYPE(struct_blk_user_trace_setup)
32 #include DEF_MPERS_TYPE(struct_blkpg_ioctl_arg)
33 #include DEF_MPERS_TYPE(struct_blkpg_partition)
34
35 #include <linux/ioctl.h>
36 #include <linux/fs.h>
37
38 typedef struct {
39         int op;
40         int flags;
41         int datalen;
42         void *data;
43 } struct_blkpg_ioctl_arg;
44
45 #define BLKPG_DEVNAMELTH        64
46 #define BLKPG_VOLNAMELTH        64
47 typedef struct {
48         long long start;                /* starting offset in bytes */
49         long long length;               /* length in bytes */
50         int pno;                        /* partition number */
51         char devname[BLKPG_DEVNAMELTH]; /* partition name, like sda5 or c0d1p2,
52                                            to be used in kernel messages */
53         char volname[BLKPG_VOLNAMELTH]; /* volume label */
54 } struct_blkpg_partition;
55
56 #define BLKTRACE_BDEV_SIZE      32
57 typedef struct blk_user_trace_setup {
58         char name[BLKTRACE_BDEV_SIZE];  /* output */
59         uint16_t act_mask;              /* input */
60         uint32_t buf_size;              /* input */
61         uint32_t buf_nr;                /* input */
62         uint64_t start_lba;
63         uint64_t end_lba;
64         uint32_t pid;
65 } struct_blk_user_trace_setup;
66
67 #include MPERS_DEFS
68
69 #ifndef BLKPG
70 # define BLKPG      _IO(0x12,105)
71 #endif
72
73 /*
74  * ioctl numbers <= 114 are present in Linux 2.4.  The following ones have been
75  * added since then and headers containing them may not be available on every
76  * system.
77  */
78
79 #ifndef BLKTRACESETUP
80 # define BLKTRACESETUP _IOWR(0x12, 115, struct_blk_user_trace_setup)
81 #endif
82 #ifndef BLKTRACESTART
83 # define BLKTRACESTART _IO(0x12,116)
84 #endif
85 #ifndef BLKTRACESTOP
86 # define BLKTRACESTOP _IO(0x12,117)
87 #endif
88 #ifndef BLKTRACETEARDOWN
89 # define BLKTRACETEARDOWN _IO(0x12,118)
90 #endif
91 #ifndef BLKDISCARD
92 # define BLKDISCARD _IO(0x12,119)
93 #endif
94 #ifndef BLKIOMIN
95 # define BLKIOMIN _IO(0x12,120)
96 #endif
97 #ifndef BLKIOOPT
98 # define BLKIOOPT _IO(0x12,121)
99 #endif
100 #ifndef BLKALIGNOFF
101 # define BLKALIGNOFF _IO(0x12,122)
102 #endif
103 #ifndef BLKPBSZGET
104 # define BLKPBSZGET _IO(0x12,123)
105 #endif
106 #ifndef BLKDISCARDZEROES
107 # define BLKDISCARDZEROES _IO(0x12,124)
108 #endif
109 #ifndef BLKSECDISCARD
110 # define BLKSECDISCARD _IO(0x12,125)
111 #endif
112 #ifndef BLKROTATIONAL
113 # define BLKROTATIONAL _IO(0x12,126)
114 #endif
115 #ifndef BLKZEROOUT
116 # define BLKZEROOUT _IO(0x12,127)
117 #endif
118
119 #include "xlat/blkpg_ops.h"
120
121 static void
122 print_blkpg_req(struct tcb *tcp, const struct_blkpg_ioctl_arg *blkpg)
123 {
124         struct_blkpg_partition p;
125
126         tprints("{");
127         printxval(blkpg_ops, blkpg->op, "BLKPG_???");
128
129         tprintf(", flags=%d, datalen=%d, data=",
130                 blkpg->flags, blkpg->datalen);
131
132         if (!umove_or_printaddr(tcp, (long) blkpg->data, &p)) {
133                 tprintf("{start=%lld, length=%lld, pno=%d, devname=",
134                         (long long) p.start, (long long) p.length, p.pno);
135                 print_quoted_string(p.devname, sizeof(p.devname),
136                                     QUOTE_0_TERMINATED);
137                 tprints(", volname=");
138                 print_quoted_string(p.volname, sizeof(p.volname),
139                                     QUOTE_0_TERMINATED);
140                 tprints("}");
141         }
142         tprints("}");
143 }
144
145 MPERS_PRINTER_DECL(int, block_ioctl, struct tcb *tcp,
146                    const unsigned int code, const long arg)
147 {
148         switch (code) {
149         /* take arg as a value, not as a pointer */
150         case BLKRASET:
151         case BLKFRASET:
152                 tprintf(", %lu", arg);
153                 break;
154
155         /* return an unsigned short */
156         case BLKSECTGET:
157         case BLKROTATIONAL:
158                 if (entering(tcp))
159                         return 0;
160                 tprints(", ");
161                 printnum_short(tcp, arg, "%hu");
162                 break;
163
164         /* return a signed int */
165         case BLKROGET:
166         case BLKBSZGET:
167         case BLKSSZGET:
168         case BLKALIGNOFF:
169                 if (entering(tcp))
170                         return 0;
171                 /* fall through */
172         /* take a signed int */
173         case BLKROSET:
174         case BLKBSZSET:
175                 tprints(", ");
176                 printnum_int(tcp, arg, "%d");
177                 break;
178
179         /* return an unsigned int */
180         case BLKPBSZGET:
181         case BLKIOMIN:
182         case BLKIOOPT:
183         case BLKDISCARDZEROES:
184                 if (entering(tcp))
185                         return 0;
186                 tprints(", ");
187                 printnum_int(tcp, arg, "%u");
188                 break;
189
190         /* return a signed long */
191         case BLKRAGET:
192         case BLKFRAGET:
193                 if (entering(tcp))
194                         return 0;
195                 tprints(", ");
196                 printnum_slong(tcp, arg);
197                 break;
198
199         /* returns an unsigned long */
200         case BLKGETSIZE:
201                 if (entering(tcp))
202                         return 0;
203                 tprints(", ");
204                 printnum_ulong(tcp, arg);
205                 break;
206
207 #ifdef HAVE_BLKGETSIZE64
208         /* returns an uint64_t */
209         case BLKGETSIZE64:
210                 if (entering(tcp))
211                         return 0;
212                 tprints(", ");
213                 printnum_int64(tcp, arg, "%" PRIu64);
214                 break;
215 #endif
216
217         /* takes a pair of uint64_t */
218         case BLKDISCARD:
219         case BLKSECDISCARD:
220         case BLKZEROOUT:
221                 tprints(", ");
222                 printpair_int64(tcp, arg, "%" PRIu64);
223                 break;
224
225         /* More complex types */
226         case BLKPG: {
227                 struct_blkpg_ioctl_arg blkpg;
228
229                 tprints(", ");
230                 if (!umove_or_printaddr(tcp, arg, &blkpg))
231                         print_blkpg_req(tcp, &blkpg);
232                 break;
233         }
234
235         case BLKTRACESETUP:
236                 if (entering(tcp)) {
237                         struct_blk_user_trace_setup buts;
238
239                         tprints(", ");
240                         if (umove_or_printaddr(tcp, arg, &buts))
241                                 break;
242                         tprintf("{act_mask=%u, buf_size=%u, "
243                                 "buf_nr=%u, start_lba=%" PRIu64 ", "
244                                 "end_lba=%" PRIu64 ", pid=%u",
245                                 (unsigned)buts.act_mask, buts.buf_size,
246                                 buts.buf_nr, buts.start_lba,
247                                 buts.end_lba, buts.pid);
248                         return 1;
249                 } else {
250                         struct_blk_user_trace_setup buts;
251
252                         if (!syserror(tcp) && !umove(tcp, arg, &buts)) {
253                                 tprints(", name=");
254                                 print_quoted_string(buts.name, sizeof(buts.name),
255                                                     QUOTE_0_TERMINATED);
256                         }
257                         tprints("}");
258                         break;
259                 }
260
261         /* No arguments */
262         case BLKRRPART:
263         case BLKFLSBUF:
264         case BLKTRACESTART:
265         case BLKTRACESTOP:
266         case BLKTRACETEARDOWN:
267                 break;
268         default:
269                 return RVAL_DECODED;
270         }
271
272         return RVAL_DECODED | 1;
273 }