From 62cec9b2258a1d3f363b35ee132d56d0db4d655b Mon Sep 17 00:00:00 2001 From: Alasdair Kergon Date: Tue, 8 Jun 2004 20:34:40 +0000 Subject: [PATCH] Support new target message ioctl. --- WHATS_NEW_DM | 4 +++ libdm/.exported_symbols | 2 ++ libdm/ioctl/libdm-compat.h | 1 + libdm/ioctl/libdm-iface.c | 59 ++++++++++++++++++++++++++++++++++--- libdm/ioctl/libdm-targets.h | 2 ++ libdm/libdevmapper.h | 6 +++- tools/dmsetup.c | 52 ++++++++++++++++++++++++++++++-- 7 files changed, 119 insertions(+), 7 deletions(-) diff --git a/WHATS_NEW_DM b/WHATS_NEW_DM index ae78594f9..2e3526370 100644 --- a/WHATS_NEW_DM +++ b/WHATS_NEW_DM @@ -1,3 +1,7 @@ +Version 1.00.18 +============================= + Add target message-passing ioctl. + Version 1.00.17 - 17 Apr 2004 ============================= configure --with-owner= --with-group= to avoid -o and -g args to 'install' diff --git a/libdm/.exported_symbols b/libdm/.exported_symbols index 1b7ab160d..01fe677e9 100644 --- a/libdm/.exported_symbols +++ b/libdm/.exported_symbols @@ -17,6 +17,8 @@ dm_task_set_newname dm_task_set_event_nr dm_task_set_major dm_task_set_minor +dm_task_set_sector +dm_task_set_message dm_task_add_target dm_get_next_target dm_task_run diff --git a/libdm/ioctl/libdm-compat.h b/libdm/ioctl/libdm-compat.h index 4f8e4b49d..ed6e85f29 100644 --- a/libdm/ioctl/libdm-compat.h +++ b/libdm/ioctl/libdm-compat.h @@ -115,6 +115,7 @@ static struct cmd_data _cmd_data_v1[] = { { "clear", 0, {4, 0, 0} }, { "mknodes", 0, {4, 0, 0} }, { "versions", 0, {4, 1, 0} }, + { "message", 0, {4, 2, 0} }, }; /* *INDENT-ON* */ diff --git a/libdm/ioctl/libdm-iface.c b/libdm/ioctl/libdm-iface.c index 555bfe756..3c72e9d5a 100644 --- a/libdm/ioctl/libdm-iface.c +++ b/libdm/ioctl/libdm-iface.c @@ -93,6 +93,9 @@ static struct cmd_data _cmd_data_v4[] = { #ifdef DM_LIST_VERSIONS {"versions", DM_LIST_VERSIONS, {4, 1, 0}}, #endif +#ifdef DM_TARGET_MSG + {"message", DM_TARGET_MSG, {4, 2, 0}}, +#endif }; /* *INDENT-ON* */ @@ -146,6 +149,9 @@ void dm_task_destroy(struct dm_task *dmt) if (dmt->newname) free(dmt->newname); + if (dmt->message) + free(dmt->message); + if (dmt->dmi.v4) free(dmt->dmi.v4); @@ -190,8 +196,9 @@ static int _unmarshal_status_v1(struct dm_task *dmt, struct dm_ioctl_v1 *dmi) if (!dm_task_add_target(dmt, spec->sector_start, (uint64_t) spec->length, spec->target_type, - outptr + sizeof(*spec))) + outptr + sizeof(*spec))) { return 0; + } outptr = outbuf + spec->next; } @@ -652,8 +659,9 @@ static int _unmarshal_status(struct dm_task *dmt, struct dm_ioctl *dmi) if (!dm_task_add_target(dmt, spec->sector_start, spec->length, spec->target_type, - outptr + sizeof(*spec))) + outptr + sizeof(*spec))) { return 0; + } outptr = outbuf + spec->next; } @@ -775,6 +783,23 @@ int dm_task_set_newname(struct dm_task *dmt, const char *newname) return 1; } +int dm_task_set_message(struct dm_task *dmt, const char *message) +{ + if (!(dmt->message = strdup(message))) { + log_error("dm_task_set_message: strdup(%s) failed", message); + return 0; + } + + return 1; +} + +int dm_task_set_sector(struct dm_task *dmt, uint64_t sector) +{ + dmt->sector = sector; + + return 1; +} + int dm_task_set_event_nr(struct dm_task *dmt, uint32_t event_nr) { dmt->event_nr = event_nr; @@ -861,6 +886,7 @@ static struct dm_ioctl *_flatten(struct dm_task *dmt) struct dm_ioctl *dmi; struct target *t; + struct dm_target_msg *tmsg; size_t len = sizeof(struct dm_ioctl); void *b, *e; int count = 0; @@ -871,14 +897,32 @@ static struct dm_ioctl *_flatten(struct dm_task *dmt) count++; } + if (count && (dmt->sector || dmt->message)) { + log_error("targets and message are incompatible"); + return NULL; + } + if (count && dmt->newname) { log_error("targets and newname are incompatible"); return NULL; } + if (dmt->newname && (dmt->sector || dmt->message)) { + log_error("message and newname are incompatible"); + return NULL; + } + + if (dmt->sector && !dmt->message) { + log_error("message is required with sector"); + return NULL; + } + if (dmt->newname) len += strlen(dmt->newname) + 1; + if (dmt->message) + len += sizeof(struct dm_target_msg) + strlen(dmt->message) + 1; + /* * Give len a minimum size so that we have space to store * dependencies or status information. @@ -933,6 +977,12 @@ static struct dm_ioctl *_flatten(struct dm_task *dmt) if (dmt->newname) strcpy(b, dmt->newname); + if (dmt->message) { + tmsg = (struct dm_target_msg *) b; + tmsg->sector = dmt->sector; + strcpy(tmsg->message, dmt->message); + } + return dmi; bad: @@ -1118,8 +1168,9 @@ int dm_task_run(struct dm_task *dmt) dmi->flags |= DM_STATUS_TABLE_FLAG; dmi->flags |= DM_EXISTS_FLAG; /* FIXME */ - log_debug("dm %s %s %s %s", _cmd_data_v4[dmt->type].name, dmi->name, - dmi->uuid, dmt->newname ? dmt->newname : ""); + log_debug("dm %s %s %s %s%.0llu %s", _cmd_data_v4[dmt->type].name, + dmi->name, dmi->uuid, dmt->newname ? dmt->newname : "", + dmt->sector, dmt->message ? dmt->message : ""); if (ioctl(_control_fd, command, dmi) < 0) { if (errno == ENXIO && ((dmt->type == DM_DEVICE_INFO) || (dmt->type == DM_DEVICE_MKNODES))) { diff --git a/libdm/ioctl/libdm-targets.h b/libdm/ioctl/libdm-targets.h index 498b9618f..97fa8661d 100644 --- a/libdm/ioctl/libdm-targets.h +++ b/libdm/ioctl/libdm-targets.h @@ -45,6 +45,8 @@ struct dm_task { struct dm_ioctl_v1 *v1; } dmi; char *newname; + char *message; + uint64_t sector; char *uuid; }; diff --git a/libdm/libdevmapper.h b/libdm/libdevmapper.h index 7d838ef2d..f0aeccde3 100644 --- a/libdm/libdevmapper.h +++ b/libdm/libdevmapper.h @@ -68,7 +68,9 @@ enum { DM_DEVICE_MKNODES, - DM_DEVICE_LIST_VERSIONS + DM_DEVICE_LIST_VERSIONS, + + DM_DEVICE_TARGET_MSG }; struct dm_task; @@ -130,6 +132,8 @@ int dm_task_set_newname(struct dm_task *dmt, const char *newname); int dm_task_set_minor(struct dm_task *dmt, int minor); int dm_task_set_major(struct dm_task *dmt, int major); int dm_task_set_event_nr(struct dm_task *dmt, uint32_t event_nr); +int dm_task_set_message(struct dm_task *dmt, const char *message); +int dm_task_set_sector(struct dm_task *dmt, uint64_t sector); /* * Use these to prepare for a create or reload. diff --git a/tools/dmsetup.c b/tools/dmsetup.c index f7d0b5e79..555f413bc 100644 --- a/tools/dmsetup.c +++ b/tools/dmsetup.c @@ -248,6 +248,51 @@ static int _rename(int argc, char **argv, void *data) return r; } +static int _message(int argc, char **argv, void *data) +{ + int r = 0, sz = 1, i; + struct dm_task *dmt; + char *str; + + if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG))) + return 0; + + if (!dm_task_set_name(dmt, argv[1])) + goto out; + + if (!dm_task_set_sector(dmt, atoll(argv[2]))) + goto out; + + argc -= 3; + argv += 3; + + for (i = 0; i < argc; i++) + sz += strlen(argv[i]) + 1; + + str = malloc(sz); + + for (i = 0; i < argc; i++) { + if (i) + strcat(str, " "); + strcat(str, argv[i]); + } + + if (!dm_task_set_message(dmt, str)) + goto out; + + free(str); + + if (!dm_task_run(dmt)) + goto out; + + r = 1; + + out: + dm_task_destroy(dmt); + + return r; +} + static int _version(int argc, char **argv, void *data) { int r = 0; @@ -264,8 +309,9 @@ static int _version(int argc, char **argv, void *data) goto out; if (!dm_task_get_driver_version(dmt, (char *) &version, - sizeof(version))) + sizeof(version))) { goto out; + } printf("Driver version: %s\n", version); @@ -623,6 +669,7 @@ static struct command _commands[] = { {"clear", "", 1, 1, _clear}, {"reload", " []", 1, 2, _reload}, {"rename", " ", 2, 2, _rename}, + {"message", " ", 3, -1, _message}, {"ls", "", 0, 0, _ls}, {"info", "[]", 0, 1, _info}, {"deps", "[]", 0, 1, _deps}, @@ -744,7 +791,8 @@ int main(int argc, char **argv) exit(1); } - if (argc < c->min_args + 1 || argc > c->max_args + 1) { + if (argc < c->min_args + 1 || + (c->max_args >= 0 && argc > c->max_args + 1)) { fprintf(stderr, "Incorrect number of arguments\n"); _usage(stderr); exit(1);