From: Damien Le Moal Date: Thu, 26 Mar 2020 10:27:50 +0000 (+0100) Subject: blkzone: ioctl related code refactoring X-Git-Tag: v2.36-rc1~165 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=46cf662532ef4e924a3478f2e474b619caf141df;p=thirdparty%2Futil-linux.git blkzone: ioctl related code refactoring [kzak@redhat.com: - generate patch from Damien's v3 - make commands[] initialization more robust] Signed-off-by: Aravind Ramesh Signed-off-by: Damien Le Moal Signed-off-by: Karel Zak --- diff --git a/sys-utils/blkzone.c b/sys-utils/blkzone.c index b6ddaf8abd..8d9de2e1bb 100644 --- a/sys-utils/blkzone.c +++ b/sys-utils/blkzone.c @@ -65,6 +65,8 @@ static int blkzone_action(struct blkzone_control *ctl); struct blkzone_command { const char *name; int (*handler)(struct blkzone_control *); + unsigned long ioctl_cmd; + const char *ioctl_name; const char *help; }; @@ -83,29 +85,37 @@ struct blkzone_control { }; static const struct blkzone_command commands[] = { - { "report", blkzone_report, N_("Report zone information about the given device") }, - { "reset", blkzone_action, N_("Reset a range of zones.") }, - { "open", blkzone_action, N_("Open a range of zones.") }, - { "close", blkzone_action, N_("Close a range of zones.") }, - { "finish", blkzone_action, N_("Set a range of zones to Full.") } -}; - -/* - * The action values must match the command index in the command array. - */ -enum blkzone_action { - BLK_ZONE_NO_ACTION = 0, - BLK_ZONE_RESET, - BLK_ZONE_OPEN, - BLK_ZONE_CLOSE, - BLK_ZONE_FINISH, + { + .name = "report", + .handler = blkzone_report, + .help = N_("Report zone information about the given device") + },{ + .name = "reset", + .handler = blkzone_action, + .ioctl_cmd = BLKRESETZONE, + .ioctl_name = "BLKRESETZONE", + .help = N_("Reset a range of zones.") + },{ + .name = "open", + .handler = blkzone_action, + .ioctl_cmd = BLKOPENZONE, + .ioctl_name = "BLKOPENZONE", + .help = N_("Open a range of zones.") + },{ + .name = "close", + .handler = blkzone_action, + .ioctl_cmd = BLKCLOSEZONE, + .ioctl_name = "BLKCLOSEZONE", + .help = N_("Close a range of zones.") + },{ + .name = "finish", + .handler = blkzone_action, + .ioctl_cmd = BLKFINISHZONE, + .ioctl_name = "BLKFINISHZONE", + .help = N_("Set a range of zones to Full.") + } }; -static enum blkzone_action command_action(const struct blkzone_command *command) -{ - return command - &commands[0]; -} - static const struct blkzone_command *name_to_command(const char *name) { size_t i; @@ -258,7 +268,7 @@ static int blkzone_report(struct blkzone_control *ctl) } printf(_(" start: 0x%09"PRIx64", len 0x%06"PRIx64", wptr 0x%06"PRIx64 - " reset:%u non-seq:%u, zcond:%2u(%s) [type: %u(%s)]\n"), + " reset:%u non-seq:%u, zcond:%2u(%s) [type: %u(%s)]\n"), start, len, (type == 0x1) ? 0 : wp - start, entry->reset, entry->non_seq, cond, condition_str[cond & (ARRAY_SIZE(condition_str) - 1)], @@ -280,39 +290,13 @@ static int blkzone_report(struct blkzone_control *ctl) /* * blkzone reset, open, close, and finish. */ - static int blkzone_action(struct blkzone_control *ctl) { struct blk_zone_range za = { .sector = 0 }; unsigned long zonesize; - unsigned long ioctl_cmd; - const char *ioctl_name; uint64_t zlen; int fd; - switch (command_action(ctl->command)) { - case BLK_ZONE_RESET: - ioctl_cmd = BLKRESETZONE; - ioctl_name = "BLKRESETZONE"; - break; - case BLK_ZONE_OPEN: - ioctl_cmd = BLKOPENZONE; - ioctl_name = "BLKOPENZONE"; - break; - case BLK_ZONE_CLOSE: - ioctl_cmd = BLKCLOSEZONE; - ioctl_name = "BLKCLOSEZONE"; - break; - case BLK_ZONE_FINISH: - ioctl_cmd = BLKFINISHZONE; - ioctl_name = "BLKFINISHZONE"; - break; - case BLK_ZONE_NO_ACTION: - /* fallthrough */ - default: - errx(EXIT_FAILURE, _("Invalid zone action")); - } - zonesize = blkdev_chunk_sectors(ctl->devname); if (!zonesize) errx(EXIT_FAILURE, _("%s: unable to determine zone size"), ctl->devname); @@ -346,9 +330,9 @@ static int blkzone_action(struct blkzone_control *ctl) za.sector = ctl->offset; za.nr_sectors = zlen; - if (ioctl(fd, ioctl_cmd, &za) == -1) + if (ioctl(fd, ctl->command->ioctl_cmd, &za) == -1) err(EXIT_FAILURE, _("%s: %s ioctl failed"), - ctl->devname, ioctl_name); + ctl->devname, ctl->command->ioctl_name); else if (ctl->verbose) printf(_("%s: successfull %s of zones in range from %" PRIu64 ", to %" PRIu64), ctl->devname,