2 * Support for decoding of DM_* ioctl commands.
4 * Copyright (c) 2016 Mikulas Patocka <mpatocka@redhat.com>
5 * Copyright (c) 2016 Masatake Yamato <yamato@redhat.com>
6 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
7 * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
8 * Copyright (c) 2016-2017 The strace developers.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #ifdef HAVE_LINUX_DM_IOCTL_H
38 # include "print_fields.h"
39 # include <linux/dm-ioctl.h>
40 # include <linux/ioctl.h>
42 # if DM_VERSION_MAJOR == 4
44 /* Definitions for command which have been added later */
46 # ifndef DM_LIST_VERSIONS
47 # define DM_LIST_VERSIONS _IOWR(DM_IOCTL, 0x0d, struct dm_ioctl)
49 # ifndef DM_TARGET_MSG
50 # define DM_TARGET_MSG _IOWR(DM_IOCTL, 0x0e, struct dm_ioctl)
52 # ifndef DM_DEV_SET_GEOMETRY
53 # define DM_DEV_SET_GEOMETRY _IOWR(DM_IOCTL, 0x0f, struct dm_ioctl)
55 # ifndef DM_DEV_ARM_POLL
56 # define DM_DEV_ARM_POLL _IOWR(DM_IOCTL, 0x10, struct dm_ioctl)
61 dm_decode_device(const unsigned int code, const struct dm_ioctl *ioc)
66 case DM_LIST_VERSIONS:
70 PRINT_FIELD_DEV(", ", *ioc, dev);
73 PRINT_FIELD_CSTRING(", ", *ioc, name);
76 PRINT_FIELD_CSTRING(", ", *ioc, uuid);
83 dm_decode_values(struct tcb *tcp, const unsigned int code,
84 const struct dm_ioctl *ioc)
89 PRINT_FIELD_U(", ", *ioc, target_count);
92 if (ioc->flags & DM_SUSPEND_FLAG)
94 ATTRIBUTE_FALLTHROUGH;
98 PRINT_FIELD_U(", ", *ioc, event_nr);
101 } else if (!syserror(tcp)) {
111 case DM_TABLE_STATUS:
113 PRINT_FIELD_U(", ", *ioc, target_count);
114 PRINT_FIELD_U(", ", *ioc, open_count);
115 PRINT_FIELD_U(", ", *ioc, event_nr);
121 #include "xlat/dm_flags.h"
124 dm_decode_flags(const struct dm_ioctl *ioc)
126 PRINT_FIELD_FLAGS(", ", *ioc, flags, dm_flags, "DM_???");
130 dm_decode_dm_target_spec(struct tcb *const tcp, const kernel_ulong_t addr,
131 const struct dm_ioctl *const ioc)
133 static const uint32_t target_spec_size =
134 sizeof(struct dm_target_spec);
136 uint32_t offset = ioc->data_start;
137 uint32_t offset_end = 0;
140 if (ioc->target_count)
146 for (i = 0; i < ioc->target_count; i++) {
149 if (i && offset <= offset_end)
152 offset_end = offset + target_spec_size;
154 if (offset_end <= offset || offset_end > ioc->data_size)
157 if (i >= max_strlen) {
162 struct dm_target_spec s;
164 if (umove_or_printaddr(tcp, addr + offset, &s))
167 PRINT_FIELD_U("{", s, sector_start);
168 PRINT_FIELD_U(", ", s, length);
171 PRINT_FIELD_D(", ", s, status);
173 PRINT_FIELD_CSTRING(", ", s, target_type);
175 tprints(", string=");
176 printstr_ex(tcp, addr + offset_end, ioc->data_size - offset_end,
183 offset = ioc->data_start + s.next;
190 tprints_comment("misplaced struct dm_target_spec");
194 dm_print_dev(struct tcb *tcp, void *dev_ptr, size_t dev_size, void *dummy)
196 uint64_t *dev = (uint64_t *) dev_ptr;
204 dm_decode_dm_target_deps(struct tcb *const tcp, const kernel_ulong_t addr,
205 const struct dm_ioctl *const ioc)
207 if (ioc->data_start == ioc->data_size)
217 static const uint32_t target_deps_dev_offs =
218 offsetof(struct dm_target_deps, dev);
220 struct dm_target_deps s;
221 uint32_t offset = ioc->data_start;
222 uint32_t offset_end = offset + target_deps_dev_offs;
225 if (offset_end <= offset || offset_end > ioc->data_size)
228 if (umove_or_printaddr(tcp, addr + offset, &s))
231 space = (ioc->data_size - offset_end) / sizeof(dev_buf);
236 PRINT_FIELD_U("{", s, count);
239 print_array(tcp, addr + offset_end, s.count, &dev_buf, sizeof(dev_buf),
240 umoven_or_printaddr, dm_print_dev, NULL);
248 tprints_comment("misplaced struct dm_target_deps");
252 dm_decode_dm_name_list(struct tcb *const tcp, const kernel_ulong_t addr,
253 const struct dm_ioctl *const ioc)
255 static const uint32_t name_list_name_offs =
256 offsetof(struct dm_name_list, name);
257 struct dm_name_list s;
258 uint32_t offset = ioc->data_start;
259 uint32_t offset_end = 0;
263 if (ioc->data_start == ioc->data_size)
271 for (count = 0;; count++) {
274 if (count && offset <= offset_end)
277 offset_end = offset + name_list_name_offs;
279 if (offset_end <= offset || offset_end > ioc->data_size)
282 if (count >= max_strlen) {
287 if (umove_or_printaddr(tcp, addr + offset, &s))
290 PRINT_FIELD_DEV("{", s, dev);
292 rc = printstr_ex(tcp, addr + offset_end,
293 ioc->data_size - offset_end,
297 * In Linux v4.13-rc1~137^2~13 it has been decided to cram in
298 * one more undocumented field after the device name, as if the
299 * format decoding was not twisted enough already. So, we have
300 * to check "next" now, and if it _looks like_ that there is
301 * a space for one additional integer, let's print it. As if the
302 * perversity with "name string going further than pointer to
303 * the next one" wasn't enough. Moreover, the calculation was
304 * broken for m32 on 64-bit kernels until v4.14-rc4~20^2~3, and
305 * we have no ability to detect kernel bit-ness (on x86, at
306 * least), so refrain from printing it for the DM versions below
307 * 4.37 (the original version was also aligned differently than
308 * now even on 64 bit).
311 if ((rc > 0) && ioc->version[1] >= 37) {
312 kernel_ulong_t event_addr =
313 (addr + offset_end + rc + 7) & ~7;
316 if ((event_addr + sizeof(event_nr)) <=
317 (addr + offset + s.next) &&
318 !umove(tcp, event_addr, &event_nr))
319 tprintf(", event_nr=%" PRIu32, event_nr);
334 tprints_comment("misplaced struct dm_name_list");
338 dm_decode_dm_target_versions(struct tcb *const tcp, const kernel_ulong_t addr,
339 const struct dm_ioctl *const ioc)
341 static const uint32_t target_vers_name_offs =
342 offsetof(struct dm_target_versions, name);
343 struct dm_target_versions s;
344 uint32_t offset = ioc->data_start;
345 uint32_t offset_end = 0;
348 if (ioc->data_start == ioc->data_size)
356 for (count = 0;; count++) {
359 if (count && offset <= offset_end)
362 offset_end = offset + target_vers_name_offs;
364 if (offset_end <= offset || offset_end > ioc->data_size)
367 if (count >= max_strlen) {
372 if (umove_or_printaddr(tcp, addr + offset, &s))
376 printstr_ex(tcp, addr + offset_end, ioc->data_size - offset_end,
378 tprintf(", version=%" PRIu32 ".%" PRIu32 ".%" PRIu32 "}",
379 s.version[0], s.version[1], s.version[2]);
391 tprints_comment("misplaced struct dm_target_versions");
395 dm_decode_dm_target_msg(struct tcb *const tcp, const kernel_ulong_t addr,
396 const struct dm_ioctl *const ioc)
398 if (ioc->data_start == ioc->data_size)
408 static const uint32_t target_msg_message_offs =
409 offsetof(struct dm_target_msg, message);
410 uint32_t offset = ioc->data_start;
411 uint32_t offset_end = offset + target_msg_message_offs;
413 if (offset_end > offset && offset_end <= ioc->data_size) {
414 struct dm_target_msg s;
416 if (umove_or_printaddr(tcp, addr + offset, &s))
419 PRINT_FIELD_U("{", s, sector);
420 tprints(", message=");
421 printstr_ex(tcp, addr + offset_end, ioc->data_size - offset_end,
426 tprints_comment("misplaced struct dm_target_msg");
431 dm_decode_string(struct tcb *const tcp, const kernel_ulong_t addr,
432 const struct dm_ioctl *const ioc)
441 uint32_t offset = ioc->data_start;
443 if (offset <= ioc->data_size) {
445 printstr_ex(tcp, addr + offset, ioc->data_size - offset,
449 tprints_comment("misplaced string");
454 dm_ioctl_has_params(const unsigned int code)
464 case DM_DEV_ARM_POLL:
472 dm_known_ioctl(struct tcb *const tcp, const unsigned int code,
473 const kernel_ulong_t arg)
475 struct dm_ioctl *ioc = NULL;
476 struct dm_ioctl *entering_ioc = NULL;
477 bool ioc_changed = false;
480 ioc = malloc(sizeof(*ioc));
484 ioc = alloca(sizeof(*ioc));
487 if ((umoven(tcp, arg, offsetof(struct dm_ioctl, data), ioc) < 0) ||
488 (ioc->data_size < offsetof(struct dm_ioctl, data_size))) {
494 set_tcb_priv_data(tcp, ioc, free);
496 entering_ioc = get_tcb_priv_data(tcp);
499 * retrieve_status, __dev_status called only in case of success,
500 * so it looks like there's no need to check open_count,
501 * event_nr, target_count, dev fields for change (they are
502 * printed only in case of absence of errors).
505 (ioc->version[0] != entering_ioc->version[0]) ||
506 (ioc->version[1] != entering_ioc->version[1]) ||
507 (ioc->version[2] != entering_ioc->version[2]) ||
508 (ioc->data_size != entering_ioc->data_size) ||
509 (ioc->data_start != entering_ioc->data_start) ||
510 (ioc->flags != entering_ioc->flags))
514 if (exiting(tcp) && syserror(tcp) && !ioc_changed)
515 return RVAL_IOCTL_DECODED;
518 * device mapper code uses %d in some places and %u in another, but
519 * fields themselves are declared as __u32.
521 tprintf("%s{version=%u.%u.%u", entering(tcp) ? ", " : " => ",
522 ioc->version[0], ioc->version[1], ioc->version[2]);
524 * if we use a different version of ABI, do not attempt to decode
527 if (ioc->version[0] != DM_VERSION_MAJOR) {
528 tprints_comment("unsupported device mapper ABI version");
532 PRINT_FIELD_U(", ", *ioc, data_size);
534 if (ioc->data_size < offsetof(struct dm_ioctl, data)) {
535 tprints_comment("data_size too small");
539 if (dm_ioctl_has_params(code))
540 PRINT_FIELD_U(", ", *ioc, data_start);
542 dm_decode_device(code, ioc);
543 dm_decode_values(tcp, code, ioc);
544 dm_decode_flags(ioc);
548 case DM_TABLE_STATUS:
549 if (entering(tcp) || syserror(tcp))
551 dm_decode_dm_target_spec(tcp, arg, ioc);
556 dm_decode_dm_target_spec(tcp, arg, ioc);
559 if (entering(tcp) || syserror(tcp))
561 dm_decode_dm_target_deps(tcp, arg, ioc);
563 case DM_LIST_DEVICES:
564 if (entering(tcp) || syserror(tcp))
566 dm_decode_dm_name_list(tcp, arg, ioc);
568 case DM_LIST_VERSIONS:
569 if (entering(tcp) || syserror(tcp))
571 dm_decode_dm_target_versions(tcp, arg, ioc);
575 dm_decode_dm_target_msg(tcp, arg, ioc);
576 else if (!syserror(tcp) && ioc->flags & DM_DATA_OUT_FLAG)
577 dm_decode_string(tcp, arg, ioc);
580 case DM_DEV_SET_GEOMETRY:
583 dm_decode_string(tcp, arg, ioc);
589 return entering(tcp) ? 0 : RVAL_IOCTL_DECODED;
593 dm_ioctl(struct tcb *const tcp, const unsigned int code, const kernel_ulong_t arg)
598 case DM_LIST_DEVICES:
608 case DM_TABLE_STATUS:
609 case DM_LIST_VERSIONS:
611 case DM_DEV_SET_GEOMETRY:
612 case DM_DEV_ARM_POLL:
613 return dm_known_ioctl(tcp, code, arg);
619 # else /* !(DM_VERSION_MAJOR == 4) */
622 dm_ioctl(struct tcb *const tcp, const unsigned int code, const kernel_ulong_t arg)
627 # endif /* DM_VERSION_MAJOR == 4 */
628 #endif /* HAVE_LINUX_DM_IOCTL_H */