mptutil, mfitil, expand_number

Eitan Adler lists at eitanadler.com
Mon Nov 11 02:16:42 UTC 2013


On Sun, Nov 10, 2013 at 1:25 AM, Bruce Evans <brde at optusnet.com.au> wrote:
> On Sat, 9 Nov 2013, Eitan Adler wrote:

Lets see if I understand everything you wanted me to do.  I am not
intending to fix any bugs in expand_number at the moment if any.

In case the patch below is mangled the following should be identical:
http://people.freebsd.org/~eadler/files/expand_number.diff

commit 55c7ba2c76c9719617f0e76cb781fad0e26eba74
Author: Eitan Adler <lists at eitanadler.com>
Date:   Sat Nov 9 22:09:17 2013 -0500

    Convert from a hand-rolled function to expand_number.

diff --git a/usr.sbin/mptutil/mpt_config.c b/usr.sbin/mptutil/mpt_config.c
index 17b9945..0d34d16 100644
--- a/usr.sbin/mptutil/mpt_config.c
+++ b/usr.sbin/mptutil/mpt_config.c
@@ -50,35 +50,6 @@ __RCSID("$FreeBSD$");
 static void dump_config(CONFIG_PAGE_RAID_VOL_0 *vol);
 #endif

-static long
-dehumanize(const char *value)
-{
-        char    *vtp;
-        long    iv;
-
-        if (value == NULL)
-                return (0);
-        iv = strtoq(value, &vtp, 0);
-        if (vtp == value || (vtp[0] != '\0' && vtp[1] != '\0')) {
-                return (0);
-        }
-        switch (vtp[0]) {
-        case 't': case 'T':
-                iv *= 1024;
-        case 'g': case 'G':
-                iv *= 1024;
-        case 'm': case 'M':
-                iv *= 1024;
-        case 'k': case 'K':
-                iv *= 1024;
-        case '\0':
-                break;
-        default:
-                return (0);
-        }
-        return (iv);
-}
-
 /*
  * Lock the volume by opening its /dev device read/write.  This will
  * only work if nothing else has it opened (including mounts).  We
@@ -615,7 +586,7 @@ create_volume(int ac, char **av)
  CONFIG_PAGE_RAID_VOL_0 *vol;
  struct config_id_state state;
  struct volume_info *info;
- long stripe_size;
+ uint64_t stripe_size;
  int ch, error, fd, i, quick, raid_type, verbose;
 #ifdef DEBUG
  int dump;
@@ -666,7 +637,11 @@ create_volume(int ac, char **av)
  quick = 1;
  break;
  case 's':
- stripe_size = dehumanize(optarg);
+ error = expand_number(optarg, &stripe_size);
+ if (error == -1) {
+ warnx("Invalid stripe size %s", optarg);
+ return (errno);
+ }
  if ((stripe_size < 512) || (!powerof2(stripe_size))) {
  warnx("Invalid stripe size %s", optarg);
  return (EINVAL);

commit fdca0b483f9184f0a6de420086a81cfa34652a5f
Author: Eitan Adler <lists at eitanadler.com>
Date:   Sat Nov 9 22:09:17 2013 -0500

    Convert from a hand-rolled function to expand_number.

    Reviewed by: sbruno

diff --git a/usr.sbin/mfiutil/mfi_config.c b/usr.sbin/mfiutil/mfi_config.c
index a919214..abecb7d 100644
--- a/usr.sbin/mfiutil/mfi_config.c
+++ b/usr.sbin/mfiutil/mfi_config.c
@@ -47,35 +47,6 @@
 static int add_spare(int ac, char **av);
 static int remove_spare(int ac, char **av);

-static long
-dehumanize(const char *value)
-{
-        char    *vtp;
-        long    iv;
-
-        if (value == NULL)
-                return (0);
-        iv = strtoq(value, &vtp, 0);
-        if (vtp == value || (vtp[0] != '\0' && vtp[1] != '\0')) {
-                return (0);
-        }
-        switch (vtp[0]) {
-        case 't': case 'T':
-                iv *= 1024;
-        case 'g': case 'G':
-                iv *= 1024;
-        case 'm': case 'M':
-                iv *= 1024;
-        case 'k': case 'K':
-                iv *= 1024;
-        case '\0':
-                break;
-        default:
-                return (0);
-        }
-        return (iv);
-}
-
 int
 mfi_config_read(int fd, struct mfi_config_data **configp)
 {
@@ -629,7 +600,11 @@ create_volume(int ac, char **av)
  break;
 #endif
  case 's':
- stripe_size = dehumanize(optarg);
+ error = expand_number(optarg, &stripe_size);
+ if (error == -1) {
+ error = errno;
+ goto error;
+ }
  if ((stripe_size < 512) || (!powerof2(stripe_size)))
  stripe_size = 64 * 1024;
  break;

commit 20ad9d0a2bd9aea4aaf7c5ae9253978ae0aed75c
Author: Eitan Adler <lists at eitanadler.com>
Date:   Sat Nov 9 22:19:41 2013 -0500

    Indicate that expand_number is case-insensitive.

diff --git a/lib/libutil/expand_number.3 b/lib/libutil/expand_number.3
index f78223b..2f5871f 100644
--- a/lib/libutil/expand_number.3
+++ b/lib/libutil/expand_number.3
@@ -51,12 +51,13 @@ argument.
 The
 .Fn expand_number
 function
+is case-insensitive and
 follows the SI power of two convention.
 .Pp
 The prefixes are:
 .Bl -column "Prefix" "Description" "1000000000000000000" -offset indent
 .It Sy "Prefix" Ta Sy "Description" Ta Sy "Multiplier"
-.It Li k Ta No kilo Ta 1024
+.It Li K Ta No kilo Ta 1024
 .It Li M Ta No mega Ta 1048576
 .It Li G Ta No giga Ta 1073741824
 .It Li T Ta No tera Ta 1099511627776



-- 
Eitan Adler


More information about the freebsd-scsi mailing list