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>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #ifdef HAVE_LINUX_DM_IOCTL_H
37 # include <linux/dm-ioctl.h>
38 # include <linux/ioctl.h>
40 # if DM_VERSION_MAJOR == 4
42 /* Definitions for command which have been added later */
44 # ifndef DM_LIST_VERSIONS
45 # define DM_LIST_VERSIONS _IOWR(DM_IOCTL, 0xd, struct dm_ioctl)
47 # ifndef DM_TARGET_MSG
48 # define DM_TARGET_MSG _IOWR(DM_IOCTL, 0xe, struct dm_ioctl)
50 # ifndef DM_DEV_SET_GEOMETRY
51 # define DM_DEV_SET_GEOMETRY _IOWR(DM_IOCTL, 0xf, struct dm_ioctl)
56 dm_decode_device(const unsigned int code, const struct dm_ioctl *ioc)
61 case DM_LIST_VERSIONS:
66 print_dev_t(ioc->dev);
70 print_quoted_string(ioc->name, DM_NAME_LEN,
75 print_quoted_string(ioc->uuid, DM_UUID_LEN,
83 dm_decode_values(struct tcb *tcp, const unsigned int code,
84 const struct dm_ioctl *ioc)
89 tprintf(", target_count=%" PRIu32,
93 if (ioc->flags & DM_SUSPEND_FLAG)
99 tprintf(", event_nr=%" PRIu32,
103 } else if (!syserror(tcp)) {
113 case DM_TABLE_STATUS:
115 tprintf(", target_count=%" PRIu32,
117 tprintf(", open_count=%" PRIu32,
119 tprintf(", event_nr=%" PRIu32,
126 #include "xlat/dm_flags.h"
129 dm_decode_flags(const struct dm_ioctl *ioc)
132 printflags(dm_flags, ioc->flags, "DM_???");
136 dm_decode_dm_target_spec(struct tcb *const tcp, const kernel_ulong_t addr,
137 const struct dm_ioctl *const ioc)
139 static const uint32_t target_spec_size =
140 sizeof(struct dm_target_spec);
142 uint32_t offset = ioc->data_start;
146 if (ioc->target_count)
152 for (i = 0; i < ioc->target_count; i++) {
153 struct dm_target_spec s;
155 offset_end = offset + target_spec_size;
157 if (offset_end <= offset || offset_end > ioc->data_size)
162 if (i >= max_strlen) {
167 if (umove_or_printaddr(tcp, addr + offset, &s))
170 tprintf("{sector_start=%" PRI__u64 ", length=%" PRI__u64,
171 s.sector_start, s.length);
174 tprintf(", status=%" PRId32, s.status);
176 tprints(", target_type=");
177 print_quoted_string(s.target_type, DM_MAX_TYPE_NAME,
180 tprints(", string=");
181 printstr_ex(tcp, addr + offset_end, ioc->data_size - offset_end,
188 offset = ioc->data_start + s.next;
190 if (offset <= offset_end)
197 tprints(", /* misplaced struct dm_target_spec */ ...");
201 dm_print_dev(struct tcb *tcp, void *dev_ptr, size_t dev_size, void *dummy)
203 uint64_t *dev = (uint64_t *) dev_ptr;
211 dm_decode_dm_target_deps(struct tcb *const tcp, const kernel_ulong_t addr,
212 const struct dm_ioctl *const ioc)
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;
229 if (offset_end <= offset || offset_end > ioc->data_size)
232 if (umove_or_printaddr(tcp, addr + offset, &s))
235 space = (ioc->data_size - offset_end) / sizeof(dev_buf);
240 tprintf("{count=%u, deps=", s.count);
242 print_array(tcp, addr + offset_end, s.count, &dev_buf, sizeof(dev_buf),
243 umoven_or_printaddr, dm_print_dev, NULL);
250 tprints("/* misplaced struct dm_target_deps */ ...");
254 dm_decode_dm_name_list(struct tcb *const tcp, const kernel_ulong_t addr,
255 const struct dm_ioctl *const ioc)
257 static const uint32_t name_list_name_offs =
258 offsetof(struct dm_name_list, name);
259 struct dm_name_list s;
260 uint32_t offset = ioc->data_start;
269 for (count = 0;; count++) {
270 offset_end = offset + name_list_name_offs;
272 if (offset_end <= offset || offset_end > ioc->data_size)
277 if (count >= max_strlen) {
282 if (umove_or_printaddr(tcp, addr + offset, &s))
284 if (!count && !s.dev) {
285 tprints("/* no devices present */");
293 printstr_ex(tcp, addr + offset_end, ioc->data_size - offset_end,
301 if (offset <= offset_end)
308 tprints(", /* misplaced struct dm_name_list */ ...");
312 dm_decode_dm_target_versions(struct tcb *const tcp, const kernel_ulong_t addr,
313 const struct dm_ioctl *const ioc)
315 static const uint32_t target_vers_name_offs =
316 offsetof(struct dm_target_versions, name);
317 struct dm_target_versions s;
318 uint32_t offset = ioc->data_start;
327 for (count = 0;; count++) {
328 offset_end = offset + target_vers_name_offs;
330 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]);
353 if (offset <= offset_end)
360 tprints(", /* misplaced struct dm_target_versions */ ...");
364 dm_decode_dm_target_msg(struct tcb *const tcp, const kernel_ulong_t addr,
365 const struct dm_ioctl *const ioc)
367 static const uint32_t target_msg_message_offs =
368 offsetof(struct dm_target_msg, message);
369 uint32_t offset = ioc->data_start;
370 uint32_t offset_end = offset + target_msg_message_offs;
377 if (offset_end > offset && offset_end <= ioc->data_size) {
378 struct dm_target_msg s;
382 if (umove_or_printaddr(tcp, addr + offset, &s))
385 tprintf("{sector=%" PRI__u64 ", message=", s.sector);
386 printstr_ex(tcp, addr + offset_end, ioc->data_size - offset_end,
390 tprints(", /* misplaced struct dm_target_msg */");
395 dm_decode_string(struct tcb *const tcp, const kernel_ulong_t addr,
396 const struct dm_ioctl *const ioc)
398 uint32_t offset = ioc->data_start;
405 if (offset < ioc->data_size) {
406 tprints(", string=");
407 printstr_ex(tcp, addr + offset, ioc->data_size - offset,
410 tprints(", /* misplaced string */");
415 dm_ioctl_has_params(const unsigned int code)
432 dm_known_ioctl(struct tcb *const tcp, const unsigned int code,
433 const kernel_ulong_t arg)
435 struct dm_ioctl *ioc = NULL;
436 struct dm_ioctl *entering_ioc = NULL;
437 bool ioc_changed = false;
440 ioc = malloc(sizeof(*ioc));
444 ioc = alloca(sizeof(*ioc));
447 if ((umoven(tcp, arg, offsetof(struct dm_ioctl, data), ioc) < 0) ||
448 (ioc->data_size < offsetof(struct dm_ioctl, data_size))) {
454 set_tcb_priv_data(tcp, ioc, free);
456 entering_ioc = get_tcb_priv_data(tcp);
459 * retrieve_status, __dev_status called only in case of success,
460 * so it looks like there's no need to check open_count,
461 * event_nr, target_count, dev fields for change (they are
462 * printed only in case of absence of errors).
465 (ioc->version[0] != entering_ioc->version[0]) ||
466 (ioc->version[1] != entering_ioc->version[1]) ||
467 (ioc->version[2] != entering_ioc->version[2]) ||
468 (ioc->data_size != entering_ioc->data_size) ||
469 (ioc->data_start != entering_ioc->data_start) ||
470 (ioc->flags != entering_ioc->flags))
474 if (exiting(tcp) && syserror(tcp) && !ioc_changed)
478 * device mapper code uses %d in some places and %u in another, but
479 * fields themselves are declared as __u32.
481 tprintf("%s{version=%u.%u.%u", entering(tcp) ? ", " : " => ",
482 ioc->version[0], ioc->version[1], ioc->version[2]);
484 * if we use a different version of ABI, do not attempt to decode
487 if (ioc->version[0] != DM_VERSION_MAJOR) {
488 tprints(", /* Unsupported device mapper ABI version */ ...");
492 tprintf(", data_size=%u", ioc->data_size);
494 if (dm_ioctl_has_params(code))
495 tprintf(", data_start=%u", ioc->data_start);
497 if (ioc->data_size < offsetof(struct dm_ioctl, data)) {
498 tprints(", /* Incorrect data_size */ ...");
502 dm_decode_device(code, ioc);
503 dm_decode_values(tcp, code, ioc);
504 dm_decode_flags(ioc);
508 case DM_TABLE_STATUS:
509 if (entering(tcp) || syserror(tcp))
511 dm_decode_dm_target_spec(tcp, arg, ioc);
516 dm_decode_dm_target_spec(tcp, arg, ioc);
519 if (entering(tcp) || syserror(tcp))
521 dm_decode_dm_target_deps(tcp, arg, ioc);
523 case DM_LIST_DEVICES:
524 if (entering(tcp) || syserror(tcp))
526 dm_decode_dm_name_list(tcp, arg, ioc);
528 case DM_LIST_VERSIONS:
529 if (entering(tcp) || syserror(tcp))
531 dm_decode_dm_target_versions(tcp, arg, ioc);
535 dm_decode_dm_target_msg(tcp, arg, ioc);
536 else if (!syserror(tcp) && ioc->flags & DM_DATA_OUT_FLAG)
537 dm_decode_string(tcp, arg, ioc);
540 case DM_DEV_SET_GEOMETRY:
543 dm_decode_string(tcp, arg, ioc);
553 dm_ioctl(struct tcb *const tcp, const unsigned int code, const kernel_ulong_t arg)
558 case DM_LIST_DEVICES:
568 case DM_TABLE_STATUS:
569 case DM_LIST_VERSIONS:
571 case DM_DEV_SET_GEOMETRY:
572 return dm_known_ioctl(tcp, code, arg);
578 # else /* !(DM_VERSION_MAJOR == 4) */
581 dm_ioctl(struct tcb *const tcp, const unsigned int code, const kernel_ulong_t arg)
586 # endif /* DM_VERSION_MAJOR == 4 */
587 #endif /* HAVE_LINUX_DM_IOCTL_H */