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, 0xd, struct dm_ioctl)
49 # ifndef DM_TARGET_MSG
50 # define DM_TARGET_MSG _IOWR(DM_IOCTL, 0xe, struct dm_ioctl)
52 # ifndef DM_DEV_SET_GEOMETRY
53 # define DM_DEV_SET_GEOMETRY _IOWR(DM_IOCTL, 0xf, struct dm_ioctl)
58 dm_decode_device(const unsigned int code, const struct dm_ioctl *ioc)
63 case DM_LIST_VERSIONS:
67 PRINT_FIELD_DEV(", ", *ioc, dev);
70 PRINT_FIELD_CSTRING(", ", *ioc, name);
73 PRINT_FIELD_CSTRING(", ", *ioc, uuid);
80 dm_decode_values(struct tcb *tcp, const unsigned int code,
81 const struct dm_ioctl *ioc)
86 PRINT_FIELD_U(", ", *ioc, target_count);
89 if (ioc->flags & DM_SUSPEND_FLAG)
95 PRINT_FIELD_U(", ", *ioc, event_nr);
98 } else if (!syserror(tcp)) {
108 case DM_TABLE_STATUS:
110 PRINT_FIELD_U(", ", *ioc, target_count);
111 PRINT_FIELD_U(", ", *ioc, open_count);
112 PRINT_FIELD_U(", ", *ioc, event_nr);
118 #include "xlat/dm_flags.h"
121 dm_decode_flags(const struct dm_ioctl *ioc)
123 PRINT_FIELD_FLAGS(", ", *ioc, flags, dm_flags, "DM_???");
127 dm_decode_dm_target_spec(struct tcb *const tcp, const kernel_ulong_t addr,
128 const struct dm_ioctl *const ioc)
130 static const uint32_t target_spec_size =
131 sizeof(struct dm_target_spec);
133 uint32_t offset = ioc->data_start;
134 uint32_t offset_end = 0;
137 if (ioc->target_count)
143 for (i = 0; i < ioc->target_count; i++) {
146 if (i && offset <= offset_end)
149 offset_end = offset + target_spec_size;
151 if (offset_end <= offset || offset_end > ioc->data_size)
154 if (i >= max_strlen) {
159 struct dm_target_spec s;
161 if (umove_or_printaddr(tcp, addr + offset, &s))
164 PRINT_FIELD_U("{", s, sector_start);
165 PRINT_FIELD_U(", ", s, length);
168 PRINT_FIELD_D(", ", s, status);
170 PRINT_FIELD_CSTRING(", ", s, target_type);
172 tprints(", string=");
173 printstr_ex(tcp, addr + offset_end, ioc->data_size - offset_end,
180 offset = ioc->data_start + s.next;
187 tprints_comment("misplaced struct dm_target_spec");
191 dm_print_dev(struct tcb *tcp, void *dev_ptr, size_t dev_size, void *dummy)
193 uint64_t *dev = (uint64_t *) dev_ptr;
201 dm_decode_dm_target_deps(struct tcb *const tcp, const kernel_ulong_t addr,
202 const struct dm_ioctl *const ioc)
204 if (ioc->data_start == ioc->data_size)
214 static const uint32_t target_deps_dev_offs =
215 offsetof(struct dm_target_deps, dev);
217 struct dm_target_deps s;
218 uint32_t offset = ioc->data_start;
219 uint32_t offset_end = offset + target_deps_dev_offs;
222 if (offset_end <= offset || offset_end > ioc->data_size)
225 if (umove_or_printaddr(tcp, addr + offset, &s))
228 space = (ioc->data_size - offset_end) / sizeof(dev_buf);
233 PRINT_FIELD_U("{", s, count);
236 print_array(tcp, addr + offset_end, s.count, &dev_buf, sizeof(dev_buf),
237 umoven_or_printaddr, dm_print_dev, NULL);
245 tprints_comment("misplaced struct dm_target_deps");
249 dm_decode_dm_name_list(struct tcb *const tcp, const kernel_ulong_t addr,
250 const struct dm_ioctl *const ioc)
252 static const uint32_t name_list_name_offs =
253 offsetof(struct dm_name_list, name);
254 struct dm_name_list s;
255 uint32_t offset = ioc->data_start;
256 uint32_t offset_end = 0;
259 if (ioc->data_start == ioc->data_size)
267 for (count = 0;; count++) {
270 if (count && offset <= offset_end)
273 offset_end = offset + name_list_name_offs;
275 if (offset_end <= offset || offset_end > ioc->data_size)
278 if (count >= max_strlen) {
283 if (umove_or_printaddr(tcp, addr + offset, &s))
286 PRINT_FIELD_DEV("{", s, dev);
288 printstr_ex(tcp, addr + offset_end, ioc->data_size - offset_end,
302 tprints_comment("misplaced struct dm_name_list");
306 dm_decode_dm_target_versions(struct tcb *const tcp, const kernel_ulong_t addr,
307 const struct dm_ioctl *const ioc)
309 static const uint32_t target_vers_name_offs =
310 offsetof(struct dm_target_versions, name);
311 struct dm_target_versions s;
312 uint32_t offset = ioc->data_start;
313 uint32_t offset_end = 0;
316 if (ioc->data_start == ioc->data_size)
324 for (count = 0;; count++) {
327 if (count && offset <= offset_end)
330 offset_end = offset + target_vers_name_offs;
332 if (offset_end <= offset || offset_end > ioc->data_size)
335 if (count >= max_strlen) {
340 if (umove_or_printaddr(tcp, addr + offset, &s))
344 printstr_ex(tcp, addr + offset_end, ioc->data_size - offset_end,
346 tprintf(", version=%" PRIu32 ".%" PRIu32 ".%" PRIu32 "}",
347 s.version[0], s.version[1], s.version[2]);
359 tprints_comment("misplaced struct dm_target_versions");
363 dm_decode_dm_target_msg(struct tcb *const tcp, const kernel_ulong_t addr,
364 const struct dm_ioctl *const ioc)
366 if (ioc->data_start == ioc->data_size)
376 static const uint32_t target_msg_message_offs =
377 offsetof(struct dm_target_msg, message);
378 uint32_t offset = ioc->data_start;
379 uint32_t offset_end = offset + target_msg_message_offs;
381 if (offset_end > offset && offset_end <= ioc->data_size) {
382 struct dm_target_msg s;
384 if (umove_or_printaddr(tcp, addr + offset, &s))
387 PRINT_FIELD_U("{", s, sector);
388 tprints(", message=");
389 printstr_ex(tcp, addr + offset_end, ioc->data_size - offset_end,
394 tprints_comment("misplaced struct dm_target_msg");
399 dm_decode_string(struct tcb *const tcp, const kernel_ulong_t addr,
400 const struct dm_ioctl *const ioc)
409 uint32_t offset = ioc->data_start;
411 if (offset <= ioc->data_size) {
413 printstr_ex(tcp, addr + offset, ioc->data_size - offset,
417 tprints_comment("misplaced string");
422 dm_ioctl_has_params(const unsigned int code)
439 dm_known_ioctl(struct tcb *const tcp, const unsigned int code,
440 const kernel_ulong_t arg)
442 struct dm_ioctl *ioc = NULL;
443 struct dm_ioctl *entering_ioc = NULL;
444 bool ioc_changed = false;
447 ioc = malloc(sizeof(*ioc));
451 ioc = alloca(sizeof(*ioc));
454 if ((umoven(tcp, arg, offsetof(struct dm_ioctl, data), ioc) < 0) ||
455 (ioc->data_size < offsetof(struct dm_ioctl, data_size))) {
461 set_tcb_priv_data(tcp, ioc, free);
463 entering_ioc = get_tcb_priv_data(tcp);
466 * retrieve_status, __dev_status called only in case of success,
467 * so it looks like there's no need to check open_count,
468 * event_nr, target_count, dev fields for change (they are
469 * printed only in case of absence of errors).
472 (ioc->version[0] != entering_ioc->version[0]) ||
473 (ioc->version[1] != entering_ioc->version[1]) ||
474 (ioc->version[2] != entering_ioc->version[2]) ||
475 (ioc->data_size != entering_ioc->data_size) ||
476 (ioc->data_start != entering_ioc->data_start) ||
477 (ioc->flags != entering_ioc->flags))
481 if (exiting(tcp) && syserror(tcp) && !ioc_changed)
485 * device mapper code uses %d in some places and %u in another, but
486 * fields themselves are declared as __u32.
488 tprintf("%s{version=%u.%u.%u", entering(tcp) ? ", " : " => ",
489 ioc->version[0], ioc->version[1], ioc->version[2]);
491 * if we use a different version of ABI, do not attempt to decode
494 if (ioc->version[0] != DM_VERSION_MAJOR) {
495 tprints_comment("unsupported device mapper ABI version");
499 PRINT_FIELD_U(", ", *ioc, data_size);
501 if (ioc->data_size < offsetof(struct dm_ioctl, data)) {
502 tprints_comment("data_size too small");
506 if (dm_ioctl_has_params(code))
507 PRINT_FIELD_U(", ", *ioc, data_start);
509 dm_decode_device(code, ioc);
510 dm_decode_values(tcp, code, ioc);
511 dm_decode_flags(ioc);
515 case DM_TABLE_STATUS:
516 if (entering(tcp) || syserror(tcp))
518 dm_decode_dm_target_spec(tcp, arg, ioc);
523 dm_decode_dm_target_spec(tcp, arg, ioc);
526 if (entering(tcp) || syserror(tcp))
528 dm_decode_dm_target_deps(tcp, arg, ioc);
530 case DM_LIST_DEVICES:
531 if (entering(tcp) || syserror(tcp))
533 dm_decode_dm_name_list(tcp, arg, ioc);
535 case DM_LIST_VERSIONS:
536 if (entering(tcp) || syserror(tcp))
538 dm_decode_dm_target_versions(tcp, arg, ioc);
542 dm_decode_dm_target_msg(tcp, arg, ioc);
543 else if (!syserror(tcp) && ioc->flags & DM_DATA_OUT_FLAG)
544 dm_decode_string(tcp, arg, ioc);
547 case DM_DEV_SET_GEOMETRY:
550 dm_decode_string(tcp, arg, ioc);
560 dm_ioctl(struct tcb *const tcp, const unsigned int code, const kernel_ulong_t arg)
565 case DM_LIST_DEVICES:
575 case DM_TABLE_STATUS:
576 case DM_LIST_VERSIONS:
578 case DM_DEV_SET_GEOMETRY:
579 return dm_known_ioctl(tcp, code, arg);
585 # else /* !(DM_VERSION_MAJOR == 4) */
588 dm_ioctl(struct tcb *const tcp, const unsigned int code, const kernel_ulong_t arg)
593 # endif /* DM_VERSION_MAJOR == 4 */
594 #endif /* HAVE_LINUX_DM_IOCTL_H */