Skip to contentSkip to conversation

mkfs: check sizes of important structs at build time

PATCH In review Hitoshi Mitake 3 participants 8 messages Jan 4, 2014 – Jan 5, 2014 6 messages not in the archive NILFS2 filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mkfs: check sizes of important structs at build time

Current nilfs_check_ondisk_sizes() checks sizes of important structs at run time. The checking should be done at build time. This patch adds a new macro, BUILD_BUG_ON(), for this purpose. It is similar to static_assert() of C++11. If an argument is true, the macro causes a bulid error.

Below is an example of BUILD_BUG_ON(). When the checked conditions are true like below:

/* intentional change for testing BUILD_BUG_ON() */

static __attribute__((used)) void nilfs_check_ondisk_sizes(void)

{
	BUILD_BUG_ON(sizeof(struct nilfs_inode) > NILFS_MIN_BLOCKSIZE);

...

build process of mkfs.o causes errors like this:

gcc -std=gnu99 -DHAVE_CONFIG_H -I. -I../.. -I../../include -Wall -g -O2 -MT mkfs.o -MD -MP -MF .deps/mkfs.Tpo -c -o mkfs.o mkfs.c mkfs.c: In function 'nilfs_check_ondisk_sizes':
mkfs.c:429:2: error: negative width in bit-field '<anonymous>'
mkfs.c:430:2: error: negative width in bit-field '<anonymous>'
mkfs.c:431:2: error: negative width in bit-field '<anonymous>'
mkfs.c:432:2: error: negative width in bit-field '<anonymous>'
mkfs.c:433:2: error: negative width in bit-field '<anonymous>'
mkfs.c:434:2: error: negative width in bit-field '<anonymous>'
mkfs.c:435:2: error: negative width in bit-field '<anonymous>'

---
 sbin/mkfs/mkfs.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
sbin/mkfs/mkfs.c +11 -11
index 4e153ce..8eb00bf 100644
@@ -85,6 +85,9 @@
 typedef __u64  blocknr_t;
 
 #define BUG_ON(x)	   assert(!(x))
+/* Force a compilation error if the condition is true */
+#define BUILD_BUG_ON(condition) ((void)sizeof(struct { int: -!!(condition); }))
+
 #define ROUNDUP_DIV(n, m)	(((n) - 1) / (m) + 1)
 #define max_t(type, x, y) \
 	({ type __x = (x); type __y = (y); __x > __y ? __x : __y; })
@@ -417,16 +420,15 @@ static unsigned count_dat_blocks(unsigned nr_dat_entries)
 	return nblocks;
 }
 
-static void nilfs_check_ondisk_sizes(void)
+static __attribute__((used)) void nilfs_check_ondisk_sizes(void)
 {
-	if (sizeof(struct nilfs_inode) > NILFS_MIN_BLOCKSIZE ||
-	    sizeof(struct nilfs_sufile_header) > NILFS_MIN_BLOCKSIZE ||
-	    sizeof(struct nilfs_segment_usage) > NILFS_MIN_BLOCKSIZE ||
-	    sizeof(struct nilfs_cpfile_header) > NILFS_MIN_BLOCKSIZE ||
-	    sizeof(struct nilfs_checkpoint) > NILFS_MIN_BLOCKSIZE ||
-	    sizeof(struct nilfs_dat_entry) > NILFS_MIN_BLOCKSIZE ||
-	    sizeof(struct nilfs_super_root) > NILFS_MIN_BLOCKSIZE)
-		perr("Internal error: too large on-disk structure");
+	BUILD_BUG_ON(sizeof(struct nilfs_inode) > NILFS_MIN_BLOCKSIZE);
+	BUILD_BUG_ON(sizeof(struct nilfs_sufile_header) > NILFS_MIN_BLOCKSIZE);
+	BUILD_BUG_ON(sizeof(struct nilfs_segment_usage) > NILFS_MIN_BLOCKSIZE);
+	BUILD_BUG_ON(sizeof(struct nilfs_cpfile_header) > NILFS_MIN_BLOCKSIZE);
+	BUILD_BUG_ON(sizeof(struct nilfs_checkpoint) > NILFS_MIN_BLOCKSIZE);
+	BUILD_BUG_ON(sizeof(struct nilfs_dat_entry) > NILFS_MIN_BLOCKSIZE);
+	BUILD_BUG_ON(sizeof(struct nilfs_super_root) > NILFS_MIN_BLOCKSIZE);
 }
 
 static unsigned long
@@ -523,8 +525,6 @@ static void init_disk_layout(struct nilfs_disk_info *di, int fd,
 		     "or shorten segments with -B option.", dev_size,
 		     (unsigned long long)segment_size * min_nsegments);
 	di->nseginfo = 0;
-
-	nilfs_check_ondisk_sizes();
 }
 
 static struct nilfs_segment_info *new_segment(struct nilfs_disk_info *di)
Signature · 6 lines
1.8.1.2

--
To unsubscribe from this list: send the line "unsubscribe linux-nilfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#4 lore raw
In reply to a message not in the archive
Quoted 12 lines · Vyacheslav Dubeyko
Quoted 9 lines · Hitoshi Mitake

Current nilfs_check_ondisk_sizes() checks sizes of important structs at run time. The checking should be done at build time. This patch adds a new macro, BUILD_BUG_ON(), for this purpose. It is similar to static_assert() of C++11. If an argument is true, the macro causes a bulid error.

Below is an example of BUILD_BUG_ON(). When the checked conditions are true like below:

Quoted 8 lines
Quoted 5 lines

/* intentional change for testing BUILD_BUG_ON() */

static __attribute__((used)) void nilfs_check_ondisk_sizes(void)

{
      BUILD_BUG_ON(sizeof(struct nilfs_inode) > NILFS_MIN_BLOCKSIZE);

So, why do we need to have function for the case of checking on compilation phase?

Just for excluding the checking from other part of code and improve readability.

Quoted 5 lines

I suppose that we need to have some run-time check anyway. Your approach is correct for the current state of the code. But I feel a necessity in run-time check anyway. Maybe it looks like a paranoia. :) Maybe it needs to extend checking in this place.

Do you mean both of the build time check and the run time check? If so, I agree with your opinion. I'll send v2 based on this policy.

Thanks,
Hitoshi
--
To unsubscribe from this list: send the line "unsubscribe linux-nilfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#2 lore raw
In reply to a message not in the archive
Quoted 6 lines · Hitoshi Mitake

Current nilfs_check_ondisk_sizes() checks sizes of important structs at run time. The checking should be done at build time. This patch adds a new macro, BUILD_BUG_ON(), for this purpose. It is similar to static_assert() of C++11. If an argument is true, the macro causes a bulid error.

Quoted 8 lines

Below is an example of BUILD_BUG_ON(). When the checked conditions are true like below:

/* intentional change for testing BUILD_BUG_ON() */

static __attribute__((used)) void nilfs_check_ondisk_sizes(void)

{
	BUILD_BUG_ON(sizeof(struct nilfs_inode) > NILFS_MIN_BLOCKSIZE);

So, why do we need to have function for the case of checking on compilation phase?

I suppose that we need to have some run-time check anyway. Your approach is correct for the current state of the code. But I feel a necessity in run-time check anyway. Maybe it looks like a paranoia. :) Maybe it needs to extend checking in this place.

Thanks,
Vyacheslav Dubeyko.

--
To unsubscribe from this list: send the line "unsubscribe linux-nilfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#6 lore raw
In reply to a message not in the archive
Quoted 15 lines · Hitoshi Mitake
Quoted 14 lines · Vyacheslav Dubeyko
Quoted 11 lines · Hitoshi Mitake

Current nilfs_check_ondisk_sizes() checks sizes of important structs at run time. The checking should be done at build time. This patch adds a new macro, BUILD_BUG_ON(), for this purpose. It is similar to static_assert() of C++11. If an argument is true, the macro causes a bulid error.

Below is an example of BUILD_BUG_ON(). When the checked conditions are true like below:

/* intentional change for testing BUILD_BUG_ON() */

Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

static __attribute__((used)) void nilfs_check_ondisk_sizes(void)

{
     BUILD_BUG_ON(sizeof(struct nilfs_inode) > NILFS_MIN_BLOCKSIZE);

So, why do we need to have function for the case of checking on compilation phase?

Just for excluding the checking from other part of code and improve readability.

I think that we can have only macro instead of the function nilfs_check_ondisk_sizes(). And this macros can be placed in the begin of main() call. I think that it will be enough for the compilation phase check.

Quoted 9 lines
Quoted 5 lines

I suppose that we need to have some run-time check anyway. Your approach is correct for the current state of the code. But I feel a necessity in run-time check anyway. Maybe it looks like a paranoia. :) Maybe it needs to extend checking in this place.

Do you mean both of the build time check and the run time check? If so, I agree with your opinion. I'll send v2 based on this policy.

I mean that block size can be different during volume creation and maybe it makes sense to extend a block size related checking for run-time phase. That's all. But right now I haven't any concrete suggestions.

Thanks,
Vyacheslav Dubeyko.

--
To unsubscribe from this list: send the line "unsubscribe linux-nilfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#9 lore raw
In reply to a message not in the archive
Quoted 28 lines · Hitoshi Mitake

Current nilfs_check_ondisk_sizes() checks sizes of important structs at run time. The checking should be done at build time. This patch adds a new macro, BUILD_BUG_ON(), for this purpose. It is similar to static_assert() of C++11. If an argument is true, the macro causes a bulid error.

Below is an example of BUILD_BUG_ON(). When the checked conditions are true like below:

/* intentional change for testing BUILD_BUG_ON() */

static __attribute__((used)) void nilfs_check_ondisk_sizes(void)

{
	BUILD_BUG_ON(sizeof(struct nilfs_inode) > NILFS_MIN_BLOCKSIZE);

...

build process of mkfs.o causes errors like this:

gcc -std=gnu99 -DHAVE_CONFIG_H -I. -I../.. -I../../include -Wall -g -O2 -MT mkfs.o -MD -MP -MF .deps/mkfs.Tpo -c -o mkfs.o mkfs.c mkfs.c: In function 'nilfs_check_ondisk_sizes':
mkfs.c:429:2: error: negative width in bit-field '<anonymous>'
mkfs.c:430:2: error: negative width in bit-field '<anonymous>'
mkfs.c:431:2: error: negative width in bit-field '<anonymous>'
mkfs.c:432:2: error: negative width in bit-field '<anonymous>'
mkfs.c:433:2: error: negative width in bit-field '<anonymous>'
mkfs.c:434:2: error: negative width in bit-field '<anonymous>'
mkfs.c:435:2: error: negative width in bit-field '<anonymous>'

Quoted 1 line

This is an interesting patch.

I am inclined to apply this since the every test in the nilfs_check_ondisk_sizes function is static.

If we will add a new check that depends on block size in a future, we need to add a separate runtime check function as Vyacheslav wrote, but I think you are doing right thing.

One my question is why you used bit operator. The BUILD_BUG_ON marcro of kernel is implemented with negative array index.
Is there any reason for this ?

Thanks,
Ryusuke Konishi
--
To unsubscribe from this list: send the line "unsubscribe linux-nilfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#8 lore raw
In reply to a message not in the archive

At Sat, 4 Jan 2014 18:39:58 +0300,

Quoted 26 lines · Vyacheslav Dubeyko
Quoted 22 lines · Hitoshi Mitake
Quoted 20 lines · Vyacheslav Dubeyko
Quoted 6 lines · Hitoshi Mitake

Current nilfs_check_ondisk_sizes() checks sizes of important structs at run time. The checking should be done at build time. This patch adds a new macro, BUILD_BUG_ON(), for this purpose. It is similar to static_assert() of C++11. If an argument is true, the macro causes a bulid error.

Quoted 8 lines

Below is an example of BUILD_BUG_ON(). When the checked conditions are true like below:

/* intentional change for testing BUILD_BUG_ON() */

static __attribute__((used)) void nilfs_check_ondisk_sizes(void)

{
     BUILD_BUG_ON(sizeof(struct nilfs_inode) > NILFS_MIN_BLOCKSIZE);

So, why do we need to have function for the case of checking on compilation phase?

Quoted 6 lines
Quoted 2 lines

Just for excluding the checking from other part of code and improve readability.

I think that we can have only macro instead of the function nilfs_check_ondisk_sizes(). And this macros can be placed in the begin of main() call. I think that it will be enough for the compilation phase check.

Ah, I see.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

I suppose that we need to have some run-time check anyway. Your approach is correct for the current state of the code. But I feel a necessity in run-time check anyway. Maybe it looks like a paranoia. :) Maybe it needs to extend checking in this place.

Quoted 7 lines
Quoted 3 lines

Do you mean both of the build time check and the run time check? If so, I agree with your opinion. I'll send v2 based on this policy.

I mean that block size can be different during volume creation and maybe it makes sense to extend a block size related checking for run-time phase. That's all. But right now I haven't any concrete suggestions.

Currently, the check is comparison between sizes of important structs and the minimal block size. So we don't need a function for it. I think adding the function for runtime checking when nilfs requires it would be enough.

Thanks,
Hitoshi

--
To unsubscribe from this list: send the line "unsubscribe linux-nilfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#11 lore raw
In reply to a message not in the archive

At Sun, 05 Jan 2014 01:08:43 +0900 (JST),

Quoted 37 lines · Ryusuke Konishi
Quoted 28 lines · Hitoshi Mitake

Current nilfs_check_ondisk_sizes() checks sizes of important structs at run time. The checking should be done at build time. This patch adds a new macro, BUILD_BUG_ON(), for this purpose. It is similar to static_assert() of C++11. If an argument is true, the macro causes a bulid error.

Below is an example of BUILD_BUG_ON(). When the checked conditions are true like below:

/* intentional change for testing BUILD_BUG_ON() */

static __attribute__((used)) void nilfs_check_ondisk_sizes(void)

{
	BUILD_BUG_ON(sizeof(struct nilfs_inode) > NILFS_MIN_BLOCKSIZE);

...

build process of mkfs.o causes errors like this:

gcc -std=gnu99 -DHAVE_CONFIG_H -I. -I../.. -I../../include -Wall -g -O2 -MT mkfs.o -MD -MP -MF .deps/mkfs.Tpo -c -o mkfs.o mkfs.c mkfs.c: In function 'nilfs_check_ondisk_sizes':
mkfs.c:429:2: error: negative width in bit-field '<anonymous>'
mkfs.c:430:2: error: negative width in bit-field '<anonymous>'
mkfs.c:431:2: error: negative width in bit-field '<anonymous>'
mkfs.c:432:2: error: negative width in bit-field '<anonymous>'
mkfs.c:433:2: error: negative width in bit-field '<anonymous>'
mkfs.c:434:2: error: negative width in bit-field '<anonymous>'
mkfs.c:435:2: error: negative width in bit-field '<anonymous>'

Quoted 1 line

This is an interesting patch.

I am inclined to apply this since the every test in the nilfs_check_ondisk_sizes function is static.

Quoted 7 lines

If we will add a new check that depends on block size in a future, we need to add a separate runtime check function as Vyacheslav wrote, but I think you are doing right thing.

One my question is why you used bit operator. The BUILD_BUG_ON marcro of kernel is implemented with negative array index.
Is there any reason for this ?

If I remember correctly, I found in the BUILD_BUG_ON() in the code of Xen. I don't have any opinion about how we implement the check. If you like the way of array with negative length, I will employ it in v2.

BTW, there is another approach of the implementation.

#define static_assert(a, b) do { switch (0) case 0: case (a): ; } while (0)

# from xv6: http://pdos.csail.mit.edu/6.828/2012/xv6.html

This duplicated case of switch statement can be used to implement BUILD_BUG_ON() (the b can be used as an error message).

Which one do you like?

Thanks,
Hitoshi
--
To unsubscribe from this list: send the line "unsubscribe linux-nilfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#13 lore raw
In reply to a message not in the archive
Quoted 53 lines · Hitoshi Mitake

At Sun, 05 Jan 2014 01:08:43 +0900 (JST),

Quoted 37 lines · Ryusuke Konishi
Quoted 28 lines · Hitoshi Mitake

Current nilfs_check_ondisk_sizes() checks sizes of important structs at run time. The checking should be done at build time. This patch adds a new macro, BUILD_BUG_ON(), for this purpose. It is similar to static_assert() of C++11. If an argument is true, the macro causes a bulid error.

Below is an example of BUILD_BUG_ON(). When the checked conditions are true like below:

/* intentional change for testing BUILD_BUG_ON() */

static __attribute__((used)) void nilfs_check_ondisk_sizes(void)

{
	BUILD_BUG_ON(sizeof(struct nilfs_inode) > NILFS_MIN_BLOCKSIZE);

...

build process of mkfs.o causes errors like this:

gcc -std=gnu99 -DHAVE_CONFIG_H -I. -I../.. -I../../include -Wall -g -O2 -MT mkfs.o -MD -MP -MF .deps/mkfs.Tpo -c -o mkfs.o mkfs.c mkfs.c: In function 'nilfs_check_ondisk_sizes':
mkfs.c:429:2: error: negative width in bit-field '<anonymous>'
mkfs.c:430:2: error: negative width in bit-field '<anonymous>'
mkfs.c:431:2: error: negative width in bit-field '<anonymous>'
mkfs.c:432:2: error: negative width in bit-field '<anonymous>'
mkfs.c:433:2: error: negative width in bit-field '<anonymous>'
mkfs.c:434:2: error: negative width in bit-field '<anonymous>'
mkfs.c:435:2: error: negative width in bit-field '<anonymous>'

Quoted 1 line

This is an interesting patch.

I am inclined to apply this since the every test in the nilfs_check_ondisk_sizes function is static.

Quoted 7 lines

If we will add a new check that depends on block size in a future, we need to add a separate runtime check function as Vyacheslav wrote, but I think you are doing right thing.

One my question is why you used bit operator. The BUILD_BUG_ON marcro of kernel is implemented with negative array index.
Is there any reason for this ?

If I remember correctly, I found in the BUILD_BUG_ON() in the code of Xen. I don't have any opinion about how we implement the check. If you like the way of array with negative length, I will employ it in v2.

BTW, there is another approach of the implementation.

Quoted 7 lines

#define static_assert(a, b) do { switch (0) case 0: case (a): ; } while (0)

# from xv6: http://pdos.csail.mit.edu/6.828/2012/xv6.html

This duplicated case of switch statement can be used to implement BUILD_BUG_ON() (the b can be used as an error message).

Which one do you like?

Thanks for letting me know.

If there is no known differences in those implementations, I don't dwell on them, all seems ok.

I applied the first patch this time.

Thank you.

Ryusuke Konishi
--
To unsubscribe from this list: send the line "unsubscribe linux-nilfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html