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 <linux/dm-ioctl.h>
39 # include <linux/ioctl.h>
41 # if DM_VERSION_MAJOR == 4
43 /* Definitions for command which have been added later */
45 # ifndef DM_LIST_VERSIONS
46 # define DM_LIST_VERSIONS _IOWR(DM_IOCTL, 0xd, struct dm_ioctl)
48 # ifndef DM_TARGET_MSG
49 # define DM_TARGET_MSG _IOWR(DM_IOCTL, 0xe, struct dm_ioctl)
51 # ifndef DM_DEV_SET_GEOMETRY
52 # define DM_DEV_SET_GEOMETRY _IOWR(DM_IOCTL, 0xf, struct dm_ioctl)
57 dm_decode_device(const unsigned int code, const struct dm_ioctl *ioc)
62 case DM_LIST_VERSIONS:
67 print_dev_t(ioc->dev);
71 print_quoted_string(ioc->name, DM_NAME_LEN,
76 print_quoted_string(ioc->uuid, DM_UUID_LEN,
84 dm_decode_values(struct tcb *tcp, const unsigned int code,
85 const struct dm_ioctl *ioc)
90 tprintf(", target_count=%" PRIu32,
94 if (ioc->flags & DM_SUSPEND_FLAG)
100 tprintf(", event_nr=%" PRIu32,
104 } else if (!syserror(tcp)) {
114 case DM_TABLE_STATUS:
116 tprintf(", target_count=%" PRIu32,
118 tprintf(", open_count=%" PRIu32,
120 tprintf(", event_nr=%" PRIu32,
127 #include "xlat/dm_flags.h"
130 dm_decode_flags(const struct dm_ioctl *ioc)
133 printflags(dm_flags, ioc->flags, "DM_???");
137 dm_decode_dm_target_spec(struct tcb *const tcp, const kernel_ulong_t addr,
138 const struct dm_ioctl *const ioc)
140 static const uint32_t target_spec_size =
141 sizeof(struct dm_target_spec);
143 uint32_t offset = ioc->data_start;
144 uint32_t offset_end = 0;
147 if (ioc->target_count)
153 for (i = 0; i < ioc->target_count; i++) {
156 if (i && offset <= offset_end)
159 offset_end = offset + target_spec_size;
161 if (offset_end <= offset || offset_end > ioc->data_size)
164 if (i >= max_strlen) {
169 struct dm_target_spec s;
171 if (umove_or_printaddr(tcp, addr + offset, &s))
174 tprintf("{sector_start=%" PRI__u64 ", length=%" PRI__u64,
175 s.sector_start, s.length);
178 tprintf(", status=%" PRId32, s.status);
180 tprints(", target_type=");
181 print_quoted_string(s.target_type, DM_MAX_TYPE_NAME,
184 tprints(", string=");
185 printstr_ex(tcp, addr + offset_end, ioc->data_size - offset_end,
192 offset = ioc->data_start + s.next;
199 tprints_comment("misplaced struct dm_target_spec");
203 dm_print_dev(struct tcb *tcp, void *dev_ptr, size_t dev_size, void *dummy)
205 uint64_t *dev = (uint64_t *) dev_ptr;
213 dm_decode_dm_target_deps(struct tcb *const tcp, const kernel_ulong_t addr,
214 const struct dm_ioctl *const ioc)
216 if (ioc->data_start == ioc->data_size)
226 static const uint32_t target_deps_dev_offs =
227 offsetof(struct dm_target_deps, dev);
229 struct dm_target_deps s;
230 uint32_t offset = ioc->data_start;
231 uint32_t offset_end = offset + target_deps_dev_offs;
234 if (offset_end <= offset || offset_end > ioc->data_size)
237 if (umove_or_printaddr(tcp, addr + offset, &s))
240 space = (ioc->data_size - offset_end) / sizeof(dev_buf);
245 tprintf("{count=%u, deps=", s.count);
247 print_array(tcp, addr + offset_end, s.count, &dev_buf, sizeof(dev_buf),
248 umoven_or_printaddr, dm_print_dev, NULL);
256 tprints_comment("misplaced struct dm_target_deps");
260 dm_decode_dm_name_list(struct tcb *const tcp, const kernel_ulong_t addr,
261 const struct dm_ioctl *const ioc)
263 static const uint32_t name_list_name_offs =
264 offsetof(struct dm_name_list, name);
265 struct dm_name_list s;
266 uint32_t offset = ioc->data_start;
267 uint32_t offset_end = 0;
270 if (ioc->data_start == ioc->data_size)
278 for (count = 0;; count++) {
281 if (count && offset <= offset_end)
284 offset_end = offset + name_list_name_offs;
286 if (offset_end <= offset || offset_end > ioc->data_size)
289 if (count >= max_strlen) {
294 if (umove_or_printaddr(tcp, addr + offset, &s))
301 printstr_ex(tcp, addr + offset_end, ioc->data_size - offset_end,
315 tprints_comment("misplaced struct dm_name_list");
319 dm_decode_dm_target_versions(struct tcb *const tcp, const kernel_ulong_t addr,
320 const struct dm_ioctl *const ioc)
322 static const uint32_t target_vers_name_offs =
323 offsetof(struct dm_target_versions, name);
324 struct dm_target_versions s;
325 uint32_t offset = ioc->data_start;
326 uint32_t offset_end = 0;
329 if (ioc->data_start == ioc->data_size)
337 for (count = 0;; count++) {
340 if (count && offset <= offset_end)
343 offset_end = offset + target_vers_name_offs;
345 if (offset_end <= offset || offset_end > ioc->data_size)
348 if (count >= max_strlen) {
353 if (umove_or_printaddr(tcp, addr + offset, &s))
357 printstr_ex(tcp, addr + offset_end, ioc->data_size - offset_end,
359 tprintf(", version=%" PRIu32 ".%" PRIu32 ".%" PRIu32 "}",
360 s.version[0], s.version[1], s.version[2]);
372 tprints_comment("misplaced struct dm_target_versions");
376 dm_decode_dm_target_msg(struct tcb *const tcp, const kernel_ulong_t addr,
377 const struct dm_ioctl *const ioc)
379 if (ioc->data_start == ioc->data_size)
389 static const uint32_t target_msg_message_offs =
390 offsetof(struct dm_target_msg, message);
391 uint32_t offset = ioc->data_start;
392 uint32_t offset_end = offset + target_msg_message_offs;
394 if (offset_end > offset && offset_end <= ioc->data_size) {
395 struct dm_target_msg s;
397 if (umove_or_printaddr(tcp, addr + offset, &s))
400 tprintf("{sector=%" PRI__u64 ", message=", s.sector);
401 printstr_ex(tcp, addr + offset_end, ioc->data_size - offset_end,
406 tprints_comment("misplaced struct dm_target_msg");
411 dm_decode_string(struct tcb *const tcp, const kernel_ulong_t addr,
412 const struct dm_ioctl *const ioc)
421 uint32_t offset = ioc->data_start;
423 if (offset <= ioc->data_size) {
425 printstr_ex(tcp, addr + offset, ioc->data_size - offset,
429 tprints_comment("misplaced string");
434 dm_ioctl_has_params(const unsigned int code)
451 dm_known_ioctl(struct tcb *const tcp, const unsigned int code,
452 const kernel_ulong_t arg)
454 struct dm_ioctl *ioc = NULL;
455 struct dm_ioctl *entering_ioc = NULL;
456 bool ioc_changed = false;
459 ioc = malloc(sizeof(*ioc));
463 ioc = alloca(sizeof(*ioc));
466 if ((umoven(tcp, arg, offsetof(struct dm_ioctl, data), ioc) < 0) ||
467 (ioc->data_size < offsetof(struct dm_ioctl, data_size))) {
473 set_tcb_priv_data(tcp, ioc, free);
475 entering_ioc = get_tcb_priv_data(tcp);
478 * retrieve_status, __dev_status called only in case of success,
479 * so it looks like there's no need to check open_count,
480 * event_nr, target_count, dev fields for change (they are
481 * printed only in case of absence of errors).
484 (ioc->version[0] != entering_ioc->version[0]) ||
485 (ioc->version[1] != entering_ioc->version[1]) ||
486 (ioc->version[2] != entering_ioc->version[2]) ||
487 (ioc->data_size != entering_ioc->data_size) ||
488 (ioc->data_start != entering_ioc->data_start) ||
489 (ioc->flags != entering_ioc->flags))
493 if (exiting(tcp) && syserror(tcp) && !ioc_changed)
497 * device mapper code uses %d in some places and %u in another, but
498 * fields themselves are declared as __u32.
500 tprintf("%s{version=%u.%u.%u", entering(tcp) ? ", " : " => ",
501 ioc->version[0], ioc->version[1], ioc->version[2]);
503 * if we use a different version of ABI, do not attempt to decode
506 if (ioc->version[0] != DM_VERSION_MAJOR) {
507 tprints_comment("unsupported device mapper ABI version");
511 tprintf(", data_size=%u", ioc->data_size);
513 if (ioc->data_size < offsetof(struct dm_ioctl, data)) {
514 tprints_comment("data_size too small");
518 if (dm_ioctl_has_params(code))
519 tprintf(", data_start=%u", ioc->data_start);
521 dm_decode_device(code, ioc);
522 dm_decode_values(tcp, code, ioc);
523 dm_decode_flags(ioc);
527 case DM_TABLE_STATUS:
528 if (entering(tcp) || syserror(tcp))
530 dm_decode_dm_target_spec(tcp, arg, ioc);
535 dm_decode_dm_target_spec(tcp, arg, ioc);
538 if (entering(tcp) || syserror(tcp))
540 dm_decode_dm_target_deps(tcp, arg, ioc);
542 case DM_LIST_DEVICES:
543 if (entering(tcp) || syserror(tcp))
545 dm_decode_dm_name_list(tcp, arg, ioc);
547 case DM_LIST_VERSIONS:
548 if (entering(tcp) || syserror(tcp))
550 dm_decode_dm_target_versions(tcp, arg, ioc);
554 dm_decode_dm_target_msg(tcp, arg, ioc);
555 else if (!syserror(tcp) && ioc->flags & DM_DATA_OUT_FLAG)
556 dm_decode_string(tcp, arg, ioc);
559 case DM_DEV_SET_GEOMETRY:
562 dm_decode_string(tcp, arg, ioc);
572 dm_ioctl(struct tcb *const tcp, const unsigned int code, const kernel_ulong_t arg)
577 case DM_LIST_DEVICES:
587 case DM_TABLE_STATUS:
588 case DM_LIST_VERSIONS:
590 case DM_DEV_SET_GEOMETRY:
591 return dm_known_ioctl(tcp, code, arg);
597 # else /* !(DM_VERSION_MAJOR == 4) */
600 dm_ioctl(struct tcb *const tcp, const unsigned int code, const kernel_ulong_t arg)
605 # endif /* DM_VERSION_MAJOR == 4 */
606 #endif /* HAVE_LINUX_DM_IOCTL_H */