From e33fd978a8a56228677c14612fce99b3b3588125 Mon Sep 17 00:00:00 2001 From: Peter Rajnoha Date: Wed, 11 Jan 2012 12:46:19 +0000 Subject: Support different device name types on output of dmsetup deps, ls and info -c command. Add 'blkdevname' and 'blkdevs_used' field to dmsetup info -c -o. Add 'blkdevname' option to dmsetup ls --tree to see block device names. Add '-o options' to dmsetup deps and ls to select device name type on output. --- tools/dmsetup.c | 182 ++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 158 insertions(+), 24 deletions(-) (limited to 'tools/dmsetup.c') diff --git a/tools/dmsetup.c b/tools/dmsetup.c index 7c01bad4..dbdf481b 100644 --- a/tools/dmsetup.c +++ b/tools/dmsetup.c @@ -168,6 +168,12 @@ typedef enum { DR_NAME = 16 } report_type_t; +typedef enum { + DN_DEVNO, /* Major and minor number pair */ + DN_BLK, /* Block device name (e.g. dm-0) */ + DN_MAP /* Map name (for dm devices only, equal to DN_BLK otherwise) */ +} dev_name_t; + static int _switches[NUM_SWITCHES]; static int _int_args[NUM_SWITCHES]; static char *_string_args[NUM_SWITCHES]; @@ -182,6 +188,7 @@ static int _udev_only; static struct dm_tree *_dtree; static struct dm_report *_report; static report_type_t _report_type; +static dev_name_t _dev_name_type; /* * Commands @@ -1767,6 +1774,8 @@ static int _deps(CMD_ARGS) struct dm_task *dmt; struct dm_info info; char *name = NULL; + char dev_name[PATH_MAX]; + int major, minor; if (names) name = names->name; @@ -1813,10 +1822,17 @@ static int _deps(CMD_ARGS) printf("%s: ", name); printf("%d dependencies\t:", deps->count); - for (i = 0; i < deps->count; i++) - printf(" (%d, %d)", - (int) MAJOR(deps->device[i]), - (int) MINOR(deps->device[i])); + for (i = 0; i < deps->count; i++) { + major = (int) MAJOR(deps->device[i]); + minor = (int) MINOR(deps->device[i]); + + if ((_dev_name_type == DN_BLK || _dev_name_type == DN_MAP) && + dm_device_get_name(major, minor, _dev_name_type == DN_BLK, + dev_name, PATH_MAX)) + printf(" (%s)", dev_name); + else + printf(" (%d, %d)", major, minor); + } printf("\n"); if (multiple_devices && _switches[VERBOSE_ARG]) @@ -1831,8 +1847,16 @@ static int _deps(CMD_ARGS) static int _display_name(CMD_ARGS) { - printf("%s\t(%d, %d)\n", names->name, - (int) MAJOR(names->dev), (int) MINOR(names->dev)); + char dev_name[PATH_MAX]; + + if ((_dev_name_type == DN_BLK || _dev_name_type == DN_MAP) && + dm_device_get_name((int) MAJOR(names->dev), (int) MINOR(names->dev), + _dev_name_type == DN_BLK, dev_name, PATH_MAX)) + printf("%s\t(%s)\n", names->name, dev_name); + else + printf("%s\t(%d:%d)\n", names->name, + (int) MAJOR(names->dev), + (int) MINOR(names->dev)); return 1; } @@ -1843,6 +1867,7 @@ static int _display_name(CMD_ARGS) enum { TR_DEVICE=0, /* display device major:minor number */ + TR_BLKDEVNAME, /* display device kernel name */ TR_TABLE, TR_STATUS, TR_ACTIVE, @@ -2062,6 +2087,7 @@ static void _display_tree_node(struct dm_tree_node *node, unsigned depth, const char *name; const struct dm_info *info; int first_on_line = 0; + char dev_name[PATH_MAX]; /* Sub-tree for targets has 2 more depth */ if (depth + 2 > MAX_DEPTH) @@ -2069,7 +2095,8 @@ static void _display_tree_node(struct dm_tree_node *node, unsigned depth, name = dm_tree_node_get_name(node); - if ((!name || !*name) && !_tree_switches[TR_DEVICE]) + if ((!name || !*name) && + (!_tree_switches[TR_DEVICE] && !_tree_switches[TR_BLKDEVNAME])) return; /* Indicate whether there are more nodes at this depth */ @@ -2094,6 +2121,13 @@ static void _display_tree_node(struct dm_tree_node *node, unsigned depth, info = dm_tree_node_get_info(node); + if (_tree_switches[TR_BLKDEVNAME] && + dm_device_get_name(info->major, info->minor, 1, dev_name, PATH_MAX)) { + _out_string(name ? " <" : "<"); + _out_string(dev_name); + _out_char('>'); + } + if (_tree_switches[TR_DEVICE]) { _out_string(name ? " (" : "("); (void) _out_int(info->major); @@ -2244,6 +2278,24 @@ static int _dm_read_ahead_disp(struct dm_report *rh, return dm_report_field_uint32(rh, field, &value); } +static int _dm_blk_name_disp(struct dm_report *rh, + struct dm_pool *mem __attribute__((unused)), + struct dm_report_field *field, const void *data, + void *private __attribute__((unused))) +{ + char dev_name[PATH_MAX]; + const char *s = dev_name; + const struct dm_info *info = data; + + if (!dm_device_get_name(info->major, info->minor, 1, dev_name, PATH_MAX)) { + log_error("Could not resolve block device name for %d:%d.", + info->major, info->minor); + return 0; + } + + return dm_report_field_string(rh, field, &s); +} + static int _dm_info_status_disp(struct dm_report *rh, struct dm_pool *mem __attribute__((unused)), struct dm_report_field *field, const void *data, @@ -2323,7 +2375,7 @@ static int _dm_info_devno_disp(struct dm_report *rh, struct dm_pool *mem, struct dm_report_field *field, const void *data, void *private) { - char buf[DM_MAX_TYPE_NAME], *repstr; + char buf[PATH_MAX], *repstr; const struct dm_info *info = data; if (!dm_pool_begin_object(mem, 8)) { @@ -2331,10 +2383,17 @@ static int _dm_info_devno_disp(struct dm_report *rh, struct dm_pool *mem, return 0; } - if (dm_snprintf(buf, sizeof(buf), "%d:%d", - info->major, info->minor) < 0) { - log_error("dm_pool_alloc failed"); - goto out_abandon; + if (private) { + if (!dm_device_get_name(info->major, info->minor, + 1, buf, PATH_MAX)) + goto out_abandon; + } + else { + if (dm_snprintf(buf, sizeof(buf), "%d:%d", + info->major, info->minor) < 0) { + log_error("dm_pool_alloc failed"); + goto out_abandon; + } } if (!dm_pool_grow_object(mem, buf, strlen(buf) + 1)) { @@ -2475,13 +2534,14 @@ static int _dm_tree_parents_count_disp(struct dm_report *rh, return dm_report_field_int(rh, field, &num_parent); } -static int _dm_deps_disp(struct dm_report *rh, struct dm_pool *mem, - struct dm_report_field *field, const void *data, - void *private) +static int _dm_deps_disp_common(struct dm_report *rh, struct dm_pool*mem, + struct dm_report_field *field, const void *data, + void *private, int disp_blk_dev_names) { const struct dm_deps *deps = data; + char buf[PATH_MAX], *repstr; + int major, minor; unsigned i; - char buf[DM_MAX_TYPE_NAME], *repstr; if (!dm_pool_begin_object(mem, 16)) { log_error("dm_pool_begin_object failed"); @@ -2489,16 +2549,27 @@ static int _dm_deps_disp(struct dm_report *rh, struct dm_pool *mem, } for (i = 0; i < deps->count; i++) { - if (dm_snprintf(buf, sizeof(buf), "%d:%d", - (int) MAJOR(deps->device[i]), - (int) MINOR(deps->device[i])) < 0) { + major = (int) MAJOR(deps->device[i]); + minor = (int) MINOR(deps->device[i]); + + if (disp_blk_dev_names) { + if (!dm_device_get_name(major, minor, 1, buf, PATH_MAX)) { + log_error("Could not resolve block device " + "name for %d:%d.", major, minor); + goto out_abandon; + } + } + else if (dm_snprintf(buf, sizeof(buf), "%d:%d", + major, minor) < 0) { log_error("dm_snprintf failed"); goto out_abandon; } + if (!dm_pool_grow_object(mem, buf, 0)) { log_error("dm_pool_grow_object failed"); goto out_abandon; } + if (i + 1 < deps->count && !dm_pool_grow_object(mem, ",", 1)) { log_error("dm_pool_grow_object failed"); goto out_abandon; @@ -2519,6 +2590,20 @@ static int _dm_deps_disp(struct dm_report *rh, struct dm_pool *mem, return 0; } +static int _dm_deps_disp(struct dm_report *rh, struct dm_pool *mem, + struct dm_report_field *field, const void *data, + void *private) +{ + return _dm_deps_disp_common(rh, mem, field, data, private, 0); +} + +static int _dm_deps_blk_names_disp(struct dm_report *rh, struct dm_pool *mem, + struct dm_report_field *field, + const void *data, void *private) +{ + return _dm_deps_disp_common(rh, mem, field, data, private, 1); +} + static int _dm_subsystem_disp(struct dm_report *rh, struct dm_pool *mem __attribute__((unused)), struct dm_report_field *field, const void *data, @@ -2603,6 +2688,7 @@ FIELD_F(TASK, STR, "UUID", 32, dm_uuid, "uuid", "Unique (optional) identifier fo /* FIXME Next one should be INFO */ FIELD_F(TASK, NUM, "RAhead", 6, dm_read_ahead, "read_ahead", "Read ahead in sectors.") +FIELD_F(INFO, STR, "BlkDevName", 16, dm_blk_name, "blkdevname", "Name of block device.") FIELD_F(INFO, STR, "Stat", 4, dm_info_status, "attr", "(L)ive, (I)nactive, (s)uspended, (r)ead-only, read-(w)rite.") FIELD_F(INFO, STR, "Tables", 6, dm_info_table_loaded, "tables_loaded", "Which of the live and inactive table slots are filled.") FIELD_F(INFO, STR, "Suspended", 9, dm_info_suspended, "suspended", "Whether the device is suspended.") @@ -2617,6 +2703,7 @@ FIELD_O(INFO, dm_info, NUM, "Event", event_nr, 6, uint32, "events", "Number of m FIELD_O(DEPS, dm_deps, NUM, "#Devs", count, 5, int32, "device_count", "Number of devices used by this one.") FIELD_F(TREE, STR, "DevNames", 8, dm_deps_names, "devs_used", "List of names of mapped devices used by this one.") FIELD_F(DEPS, STR, "DevNos", 6, dm_deps, "devnos_used", "List of device numbers of devices used by this one.") +FIELD_F(DEPS, STR, "BlkDevNames", 16, dm_deps_blk_names, "blkdevs_used", "List of names of block devices used by this one.") FIELD_F(TREE, NUM, "#Refs", 5, dm_tree_parents_count, "device_ref_count", "Number of mapped devices referencing this one.") FIELD_F(TREE, STR, "RefNames", 8, dm_tree_parents_names, "names_using_dev", "List of names of mapped devices using this one.") @@ -2785,9 +2872,9 @@ static struct command _commands[] = { {"reload", " []", 0, 2, 0, _load}, {"rename", " [--setuuid] ", 1, 2, 0, _rename}, {"message", " ", 2, -1, 0, _message}, - {"ls", "[--target ] [--exec ] [--tree [-o options]]", 0, 0, 0, _ls}, + {"ls", "[--target ] [--exec ] [-o options] [--tree]", 0, 0, 0, _ls}, {"info", "[]", 0, -1, 1, _info}, - {"deps", "[]", 0, -1, 1, _deps}, + {"deps", "[-o options] []", 0, -1, 1, _deps}, {"status", "[] [--target ]", 0, -1, 1, _status}, {"table", "[] [--target ] [--showkeys]", 0, -1, 1, _status}, {"wait", " []", 0, 2, 0, _wait}, @@ -2823,8 +2910,9 @@ static void _usage(FILE *out) "-j -m \n"); fprintf(out, " are comma-separated. Use 'help -c' for list.\n"); fprintf(out, "Table_file contents may be supplied on stdin.\n"); - fprintf(out, "Tree options are: ascii, utf, vt100; compact, inverted, notrunc;\n" - " [no]device, active, open, rw and uuid.\n"); + fprintf(out, "Options are: devno, devname, blkdevname.\n"); + fprintf(out, "Tree specific options are: ascii, utf, vt100; compact, inverted, notrunc;\n" + " blkdevname, [no]device, active, open, rw and uuid.\n"); fprintf(out, "\n"); } @@ -2888,6 +2976,8 @@ static int _process_tree_options(const char *options) ; if (!strncmp(s, "device", len)) _tree_switches[TR_DEVICE] = 1; + else if (!strncmp(s, "blkdevname", len)) + _tree_switches[TR_BLKDEVNAME] = 1; else if (!strncmp(s, "nodevice", len)) _tree_switches[TR_DEVICE] = 0; else if (!strncmp(s, "status", len)) @@ -3157,6 +3247,50 @@ static int _process_losetup_switches(const char *base, int *argc, char ***argv, return 1; } +static int _process_options(const char *options) +{ + const char *s, *end; + size_t len; + + /* Tree options are processed separately. */ + if (_switches[TREE_ARG]) + return _process_tree_options(_string_args[OPTIONS_ARG]); + + /* Column options are processed separately by _report_init (called later). */ + if (_switches[COLS_ARG]) + return 1; + + /* No options specified. */ + if (!_switches[OPTIONS_ARG]) + return 1; + + /* Set defaults. */ + _dev_name_type = DN_DEVNO; + + /* Parse. */ + for (s = options; s && *s; s++) { + len = 0; + for (end = s; *end && *end != ','; end++, len++) + ; + if (!strncmp(s, "devno", len)) + _dev_name_type = DN_DEVNO; + else if (!strncmp(s, "blkdevname", len)) + _dev_name_type = DN_BLK; + else if (!strncmp(s, "devname", len)) + _dev_name_type = DN_MAP; + else { + fprintf(stderr, "Option not recognised: %s\n", s); + return 0; + } + + if (!*end) + break; + s = end; + } + + return 1; +} + static int _process_switches(int *argc, char ***argv, const char *dev_dir) { char *base, *namebase, *s; @@ -3402,7 +3536,7 @@ static int _process_switches(int *argc, char ***argv, const char *dev_dir) return 0; } - if (_switches[TREE_ARG] && !_process_tree_options(_string_args[OPTIONS_ARG])) + if (!_process_options(_string_args[OPTIONS_ARG])) return 0; if (_switches[TABLE_ARG] && _switches[NOTABLE_ARG]) { -- cgit