Skip to contentSkip to conversation

Btrfs-progs: use atoll() for mkfs.btrfs size option

PATCH In review Stefan Behrens 2 participants 5 messages Oct 15, 2012 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Btrfs-progs: use atoll() for mkfs.btrfs size option

On 32 bit systems, a numerical parameter of 2147483648 or above to the mkfs.btrfs -b option does not work. The parameter is stored in an u64 but is read using atol() and thus not read correctly. This patch changes it to use atoll().
Specifying a multiplier (k, m or g) like "100g" in the mkfs.btrfs parameter list would also be a working workaround.

---
The patch is based on the master branch of
git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs-progs.git

 mkfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mkfs.c +1 -1
index 47f0c9c..e62f0e4 100644
@@ -80,7 +80,7 @@ static u64 parse_size(char *s)
 		}
 		s[len - 1] = '\0';
 	}
-	ret = atol(s) * mult;
+	ret = atoll(s) * mult;
 	free(s);
 	return ret;
 }
Signature · 1 line
1.7.12.3
#1 lore raw

Hi Stefan,

when I checked your patch I found further problems to the function parse_size():
- this function is declared two time: both in mkfs.c and in cmd-filesystem.c; this is a good chance to move it in the utils.c file.
- your suggestion was to use atoll, which is signed. strtoull (which is unsigned) would be a better choice.
- this function doesn't check for overflow [minor]
- if a number like 123MB is passed, it is evaluated as 123 instead of 123*1024*1024 [bug]

This suggested me to make the following patch.
Hoping to not offende anybody :-)

BR
G.Baroncelli

You can pull the patch from
http://cassiopea.homelinux.net/git/btrfs-progs-unstable.git
branch
parse_size

#2 lore raw

PATCH 1/1 Move parse_size() in utils.c

From: Goffredo Baroncelli <[email protected]>

Move parse_size() in utils.c, because it is used both from cmds-filesystem.c and mkfs.c.
Moreover added check about overflow, support to further units ( t=tera, e=exa, z=zetta, y=yotta).
Correct a bug which happens if a value like 123MB is passed: before the function returned 123 instead of 123*1024*1024

---
 cmds-filesystem.c |   26 -----------------------
 mkfs.c            |   31 ---------------------------
 utils.c           |   61 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 utils.h           |    1 +
 4 files changed, 62 insertions(+), 57 deletions(-)
cmds-filesystem.c +0 -26
index 9c43d35..507239a 100644
@@ -311,32 +311,6 @@ static int cmd_sync(int argc, char **argv)
 	return 0;
 }
 
-static u64 parse_size(char *s)
-{
-	int len = strlen(s);
-	char c;
-	u64 mult = 1;
-
-	if (!isdigit(s[len - 1])) {
-		c = tolower(s[len - 1]);
-		switch (c) {
-		case 'g':
-			mult *= 1024;
-		case 'm':
-			mult *= 1024;
-		case 'k':
-			mult *= 1024;
-		case 'b':
-			break;
-		default:
-			fprintf(stderr, "Unknown size descriptor %c\n", c);
-			exit(1);
-		}
-		s[len - 1] = '\0';
-	}
-	return atoll(s) * mult;
-}
-
 static int parse_compress_type(char *s)
 {
 	if (strcmp(optarg, "zlib") == 0)
mkfs.c +0 -31
index 47f0c9c..ca850d9 100644
@@ -54,37 +54,6 @@ struct directory_name_entry {
 	struct list_head list;
 };
 
-static u64 parse_size(char *s)
-{
-	int len = strlen(s);
-	char c;
-	u64 mult = 1;
-	u64 ret;
-
-	s = strdup(s);
-
-	if (len && !isdigit(s[len - 1])) {
-		c = tolower(s[len - 1]);
-		switch (c) {
-		case 'g':
-			mult *= 1024;
-		case 'm':
-			mult *= 1024;
-		case 'k':
-			mult *= 1024;
-		case 'b':
-			break;
-		default:
-			fprintf(stderr, "Unknown size descriptor %c\n", c);
-			exit(1);
-		}
-		s[len - 1] = '\0';
-	}
-	ret = atol(s) * mult;
-	free(s);
-	return ret;
-}
-
 static int make_root_dir(struct btrfs_root *root, int mixed)
 {
 	struct btrfs_trans_handle *trans;
utils.c +61 -0

64 lines not on this page · raw message

utils.h +1 -0
index 3a0368b..180b3f9 100644
@@ -46,4 +46,5 @@ int check_label(char *input);
 int get_mountpt(char *dev, char *mntpt, size_t size);
 
 int btrfs_scan_block_devices(int run_ioctl);
+u64 parse_size(char *s);
 #endif
Signature · 1 line
1.7.10.4
#3 lore raw

Re: [PATCH] Move parse_size() in utils.c

Hi Goffredo

Quoted 4 lines · Goffredo Baroncelli

From: Goffredo Baroncelli <[email protected]>

Move parse_size() in utils.c, because it is used both from cmds-filesystem.c and mkfs.c.

Makes sense.
(Jan also sent such a patch on 1 Feb 2011 <http://permalink.gmane.org/gmane.comp.file-systems.btrfs/9016>, as part of the patch for speed profiles and dedicated log devices. But the whole patch series was not integrated.)

Quoted 2 lines

Moreover added check about overflow, support to further units ( t=tera, e=exa, z=zetta, y=yotta).

Don't make it too complicated, please. Btrfs cannot handle more than 2^64 bytes.

Quoted 2 lines

Correct a bug which happens if a value like 123MB is passed: before the function returned 123 instead of 123*1024*1024

Yes, that should be fixed. 'B' is taken as the size multiplier 'byte' (times one) and 'M' is silently ignored.

Quoted 95 lines
---
  cmds-filesystem.c |   26 -----------------------
  mkfs.c            |   31 ---------------------------
  utils.c           |   61 +++++++++++++++++++++++++++++++++++++++++++++++++++++
  utils.h           |    1 +
  4 files changed, 62 insertions(+), 57 deletions(-)
cmds-filesystem.c +0 -26
index 9c43d35..507239a 100644
@@ -311,32 +311,6 @@ static int cmd_sync(int argc, char **argv)
  	return 0;
  }
 
-static u64 parse_size(char *s)
-{
-	int len = strlen(s);
-	char c;
-	u64 mult = 1;
-
-	if (!isdigit(s[len - 1])) {
-		c = tolower(s[len - 1]);
-		switch (c) {
-		case 'g':
-			mult *= 1024;
-		case 'm':
-			mult *= 1024;
-		case 'k':
-			mult *= 1024;
-		case 'b':
-			break;
-		default:
-			fprintf(stderr, "Unknown size descriptor %c\n", c);
-			exit(1);
-		}
-		s[len - 1] = '\0';
-	}
-	return atoll(s) * mult;
-}
-
  static int parse_compress_type(char *s)
  {
  	if (strcmp(optarg, "zlib") == 0)
mkfs.c +0 -31
index 47f0c9c..ca850d9 100644
@@ -54,37 +54,6 @@ struct directory_name_entry {
  	struct list_head list;
  };
 
-static u64 parse_size(char *s)
-{
-	int len = strlen(s);
-	char c;
-	u64 mult = 1;
-	u64 ret;
-
-	s = strdup(s);
-
-	if (len && !isdigit(s[len - 1])) {
-		c = tolower(s[len - 1]);
-		switch (c) {
-		case 'g':
-			mult *= 1024;
-		case 'm':
-			mult *= 1024;
-		case 'k':
-			mult *= 1024;
-		case 'b':
-			break;
-		default:
-			fprintf(stderr, "Unknown size descriptor %c\n", c);
-			exit(1);
-		}
-		s[len - 1] = '\0';
-	}
-	ret = atol(s) * mult;
-	free(s);
-	return ret;
-}
-
  static int make_root_dir(struct btrfs_root *root, int mixed)
  {
  	struct btrfs_trans_handle *trans;
utils.c +1 -0
index 205e667..b1bd669 100644
@@ -1220,3 +1220,64 @@ scan_again:
  	return 0;
  }
 
+/*

utils.c scan_again: · line 1233

Quoted 10 lines
utils.c +10 -0
+ *  see http://gurmeet.net/puzzles/fast-bit-counting-routines/ for
+ *  further algorithms, I used the only one which I understood :-)
+ */
+static int bitcount(u64 v)
+{
+	int	n;
+	for(n=0; v ; n++, v >>= 1) ;
+
+	return n;
+}

IMO, something like bitcount() is not needed, much too complicated for such a minor issue.

utils.c scan_again: · line 1241

Quoted 8 lines
utils.c +8 -0
+
+u64 parse_size(char *s)
+{
+	int shift = 0;
+	u64 ret;
+	int n, i;
+
+	s = strdup(s);

Either don't call strdup() or free() the memory at the end.

utils.c scan_again: · line 1246

Quoted 5 lines
utils.c +5 -0
+
+	for( i = 0 ; s[i] && isdigit(s[i]) ; i++ ) ;
+	switch (tolower(s[i])) {
+		/* note: the yobibyte and the zebibyte don't fit
+		   in a u64, they need an u128 !!! */

If the comment is correct, please remove yobi, zebi and the comment :)

Quoted 7 lines
utils.c +7 -0
+		case 'y':	/* yobibyte */
+			shift += 10;
+		case 'z':	/* zebibyte */
+			shift += 10;
+		case 'e':	/* exbibyte */
+			shift += 10;
+		case 'p':	/* pebibyte */

utils.c scan_again: · line 1263

Quoted 10 lines
utils.c +10 -0
+			shift += 10;
+		case 't':	/* tetibyte */
+			shift += 10;
+		case 'g':	/* gibibyte */
+			shift += 10;
+		case 'm':	/* mebibyte */
+			shift += 10;
+		case 'k':	/* kibibyte */
+			shift += 10;
+		case 0:

This should be "case 'b'" at the end in order to maintain backward compatibility.

Quoted 5 lines
utils.c +5 -0
+			break;
+		default:
+			fprintf(stderr, "ERROR: Unknown size descriptor %c\n",
+				s[i]);
+			exit(1);

utils.c scan_again: · line 1278

Quoted 10 lines
utils.c +10 -0
+	}
+	ret = strtoull(s, 0, 0);
+	n = bitcount(ret);
+
+	if( ( n + shift ) > ( sizeof(u64) * CHAR_BIT )) {
+		fprintf(stderr, "ERROR: Overflow, the value '%s' is too big\n",
+			s);
+		fprintf(stderr, "ERROR: Abort\n");
+		exit(1);
+	}

IMO, bitcount() and the check afterwards should be removed.

Quoted 16 lines
utils.c +5 -0
+
+	return ret << shift;
+}
+
+
utils.h +1 -0
index 3a0368b..180b3f9 100644
@@ -46,4 +46,5 @@ int check_label(char *input);
  int get_mountpt(char *dev, char *mntpt, size_t size);
 
  int btrfs_scan_block_devices(int run_ioctl);
+u64 parse_size(char *s);
  #endif
#4 lore raw

Re: [PATCH] Move parse_size() in utils.c

Hi Stefan,

Quoted 8 lines · Stefan Behrens

Hi Goffredo

Quoted 4 lines · Goffredo Baroncelli

From: Goffredo Baroncelli <[email protected]>

Move parse_size() in utils.c, because it is used both from cmds-filesystem.c and mkfs.c.

Quoted 7 lines

Makes sense.
(Jan also sent such a patch on 1 Feb 2011 <http://permalink.gmane.org/gmane.comp.file-systems.btrfs/9016>, as part of the patch for speed profiles and dedicated log devices. But the whole patch series was not integrated.)

Quoted 2 lines

Moreover added check about overflow, support to further units ( t=tera, e=exa, z=zetta, y=yotta).

Don't make it too complicated, please. Btrfs cannot handle more than 2^64 bytes.

yeaa I didn't resisted :-)

Quoted 9 lines
Quoted 3 lines

Correct a bug which happens if a value like 123MB is passed: before the function returned 123 instead of 123*1024*1024

Yes, that should be fixed. 'B' is taken as the size multiplier 'byte' (times one) and 'M' is silently ignored.

[....]

Quoted 1 line
Quoted 13 lines

utils.c scan_again: · line 1233

Quoted 11 lines
utils.c +11 -0
+/*
+ *  see http://gurmeet.net/puzzles/fast-bit-counting-routines/ for
+ *  further algorithms, I used the only one which I understood :-)
+ */
+static int bitcount(u64 v)
+{
+    int    n;
+    for(n=0; v ; n++, v >>= 1) ;
+
+    return n;
+}

IMO, something like bitcount() is not needed, much too complicated for such a minor issue.

I fear that if somebody pass something like 16E, it got un-predicible results. Let me to wait a day to see if anyone has a different opinion on it. If nobody tell otherwise I will remove this check.

Quoted 11 lines

utils.c scan_again: · line 1241

Quoted 9 lines
utils.c +8 -0
+
+u64 parse_size(char *s)
+{
+    int shift = 0;
+    u64 ret;
+    int n, i;
+
+    s = strdup(s);

Either don't call strdup() or free() the memory at the end.

This was a my BUG, I removed the needing of strdup(), but I don't remove the strdup() itself :-) Thanks to catch it.

Quoted 8 lines

utils.c scan_again: · line 1246

Quoted 6 lines
utils.c +5 -0
+
+    for( i = 0 ; s[i] && isdigit(s[i]) ; i++ ) ;
+    switch (tolower(s[i])) {
+        /* note: the yobibyte and the zebibyte don't fit
+           in a u64, they need an u128 !!! */

If the comment is correct, please remove yobi, zebi and the comment :)

ok... (but zfs supports zettabyte filesystem.... )

Quoted 19 lines
Quoted 8 lines
utils.c +7 -0
+        case 'y':    /* yobibyte */
+            shift += 10;
+        case 'z':    /* zebibyte */
+            shift += 10;
+        case 'e':    /* exbibyte */
+            shift += 10;
+        case 'p':    /* pebibyte */

utils.c scan_again: · line 1263

Quoted 10 lines
utils.c +10 -0
+            shift += 10;
+        case 't':    /* tetibyte */
+            shift += 10;
+        case 'g':    /* gibibyte */
+            shift += 10;
+        case 'm':    /* mebibyte */
+            shift += 10;
+        case 'k':    /* kibibyte */
+            shift += 10;
+        case 0:
Quoted 1 line

This should be "case 'b'" at the end in order to maintain backward compatibility.

Ok, this make sense

Quoted 31 lines
Quoted 6 lines
utils.c +5 -0
+            break;
+        default:
+            fprintf(stderr, "ERROR: Unknown size descriptor %c\n",
+                s[i]);
+            exit(1);

utils.c scan_again: · line 1278

Quoted 10 lines
utils.c +10 -0
+    }
+    ret = strtoull(s, 0, 0);
+    n = bitcount(ret);
+
+    if( ( n + shift ) > ( sizeof(u64) * CHAR_BIT )) {
+        fprintf(stderr, "ERROR: Overflow, the value '%s' is too big\n",
+            s);
+        fprintf(stderr, "ERROR: Abort\n");
+        exit(1);
+    }

IMO, bitcount() and the check afterwards should be removed.

Quoted 12 lines
utils.c +5 -0
+
+    return ret << shift;
+}
+
+
utils.h +0 -0
index 3a0368b..180b3f9 100644
@@ -46,4 +46,5 @@ int check_label(char *input);
  int get_mountpt(char *dev, char *mntpt, size_t size);
 
Quoted 6 lines

utils.h int check_label(char *input); · line 50

Quoted 4 lines
utils.h +1 -0
  int btrfs_scan_block_devices(int run_ioctl);
+u64 parse_size(char *s);
  #endif

I updated the patch on my repo (see first email), if someone want to make some tests. As explained above I will wait a day, then I will re-issue the patch.

BR
G.Baroncelli