Skip to contentSkip to conversation

Problem with the blocklist command

DISCUSSION Bean 2 participants 8 messages Jul 31, 2007 – Aug 2, 2007

lore
Share & tools
lore

Bean

author
#0 lore raw

Hi,

I notice that the output of blocklist command is quite strange. After some debuging, I think
the problem may be caused by the compiler.

static grub_err_t
grub_cmd_blocklist (struct grub_arg_list *state __attribute__ ((unused)),
int argc, char **args)
{
grub_file_t file;
char buf[GRUB_DISK_SECTOR_SIZE];
unsigned long start_sector = 0;
unsigned num_sectors = 0;
int num_entries = 0;
grub_disk_addr_t part_start = 0;
auto void read_blocklist (grub_disk_addr_t sector, unsigned offset,

			    unsigned length);
  auto void print_blocklist (grub_disk_addr_t sector, unsigned num,
			     unsigned offset, unsigned length);

void read_blocklist (grub_disk_addr_t sector, unsigned offset,

		       unsigned length)
    {
      grub_printf("2- %llu\n", part_start);
      if (num_sectors > 0)
	{
	  if (start_sector + num_sectors == sector
	      && offset == 0 && length == GRUB_DISK_SECTOR_SIZE)
	    {
	      num_sectors++;
	      return;
	    }
	
	  print_blocklist (start_sector, num_sectors, 0, 0);
	  num_sectors = 0;
	}

      if (offset == 0 && length == GRUB_DISK_SECTOR_SIZE)
	{
	  start_sector = sector;
	  num_sectors++;
	}
      else
	print_blocklist (sector, 0, offset, length);
    }

void print_blocklist (grub_disk_addr_t sector, unsigned num,

			unsigned offset, unsigned length)
    {
      grub_printf("3- %llu\n", part_start);
      if (num_entries++)
	grub_printf (",");

      grub_printf ("%llu", sector - part_start);
      if (num > 0)
	grub_printf ("+%u", num);
      if (offset != 0 || length != 0)
	grub_printf ("[%u-%u]", offset, offset + length);
    }
  if (argc < 1)
    return grub_error (GRUB_ERR_BAD_ARGUMENT, "no file specified");

  file = grub_file_open (args[0]);
  if (! file)
    return grub_errno;

if (! file->device->disk)
return grub_error (GRUB_ERR_BAD_DEVICE,
"this command is available only for disk devices.");

  if (file->device->disk->partition)
    part_start = grub_partition_get_start (file->device->disk->partition);
  grub_printf("1- %llu\n", part_start);

  file->read_hook = read_blocklist;

while (grub_file_read (file, buf, sizeof (buf)) > 0)

    ;
  if (num_sectors > 0)
    print_blocklist (start_sector, num_sectors, 0, 0);

  grub_file_close (file);

return grub_errno;
}

I blocklist a very small file, it shows
1- 32
2- 32
3- 17294103305076670291

If I define part_start as a global variable, the ouput is ok.

I don't know if this is a compiler related bug, my gcc version is

gcc version 4.1.2 20061028 (prerelease) (Debian 4.1.1-19)

btw, I'm curious to know how local variable part_start is passed to hook function
read_blocklist.

--
Bean

#1 lore raw

Bean <[email protected]> writes:

Hi Bean,

Quoted 3 lines

I notice that the output of blocklist command is quite strange. After some debuging, I think
the problem may be caused by the compiler.

[...]

Quoted 10 lines

I blocklist a very small file, it shows
1- 32
2- 32
3- 17294103305076670291

If I define part_start as a global variable, the ouput is ok.

I don't know if this is a compiler related bug, my gcc version is

gcc version 4.1.2 20061028 (prerelease) (Debian 4.1.1-19)

This is most likely the regparam bug. I wrote a macro to work around this bug (NESTED_FUNC_ATTR). See, for example, how this is used in fshelp.[ch]. Most likely the same has to be done for the read_hook. When you do this, the problem will disappear. I could fix this for you, if you do not exactly understand what I mean...

We should at some point check if other hooks have the same problem.

Quoted 3 lines

btw, I'm curious to know how local variable part_start is passed to hook function
read_blocklist.

It isn't. The hook function is a nested function, it can access all local variables of grub_cmd_blocklist.

--
Marco

author
#2 lore raw
Quoted 7 lines · Marco Gerards

This is most likely the regparam bug. I wrote a macro to work around this bug (NESTED_FUNC_ATTR). See, for example, how this is used in fshelp.[ch]. Most likely the same has to be done for the read_hook. When you do this, the problem will disappear. I could fix this for you, if you do not exactly understand what I mean...

We should at some point check if other hooks have the same problem.

Yes, you're right, after adding the NESTED_FUNC_ATTR macro, it finally get the correct result. But there is a lot of place where modification is needed, including disk.h and file.h.

Signature · 1 line
Bean
#3 lore raw
Quoted 7 lines · Bean
Quoted 6 lines · Marco Gerards

This is most likely the regparam bug. I wrote a macro to work around this bug (NESTED_FUNC_ATTR). See, for example, how this is used in fshelp.[ch]. Most likely the same has to be done for the read_hook. When you do this, the problem will disappear. I could fix this for you, if you do not exactly understand what I mean...

Quoted 5 lines
Quoted 1 line

We should at some point check if other hooks have the same problem.

Yes, you're right, after adding the NESTED_FUNC_ATTR macro, it finally get the correct result. But there is a lot of place where modification is needed, including disk.h and file.h.

Yes, I was afraid of that... Can you make a patch+changelog of this so it can be fixed in CVS?

--
Marco

author
#4 lore raw
Quoted 2 lines · Marco Gerards

Yes, I was afraid of that... Can you make a patch+changelog of this so it can be fixed in CVS?

Ok, i basicly change read_hook in grub_disk and grub_file, then fix the warning. see if it works.

Signature · 1 line
Bean

* disk.h (grub_disk): Add macro NESTED_FUNC_ATTR.

* file.h (grub_file): Likewise.

* fshelp.h (grub_fshelp_read_file): Likewise.

* util/i386/pc/grub-setup.c (setup): Likewise. (save_first_sector): Likewise. (save_blocklists): Likewise.

* fs/affs.c (grub_affs_read_file): Likewise.

* fs/ext2.c (grub_ext2_read_file): Likewise.

* fs/fat.c (grub_fat_read_data): Likewise.

* fs/fshelp.c (grub_fshelp_read_file): Likewise.

* fs/hfs.c (grub_hfs_read_file): Likewise.

* fs/hfsplus.c (grub_hfsplus_read_file): Likewise.

* fs/jfs.c (grub_jfs_read_file): Likewise.

* fs/minix.c (grub_minix_read_file): Likewise.

* fs/sfs.c (grub_sfs_read_file): Likewise.

* fs/ufs.c (grub_ufs_read_file): Likewise.

* fs/xfs.c (grub_xfs_read_file): Likewise.

* command/blocklist.c (read_blocklist): Likewise. (print_blocklist): Likewise.

Index: include/grub/disk.h
===================================================================
RCS file: /sources/grub/grub2/include/grub/disk.h,v retrieving revision 1.13

grub/disk.h +1 -1
diff -u -p -r1.13 disk.h
@@ -94,7 +94,7 @@ struct grub_disk
 
   /* Called when a sector was read. OFFSET is between 0 and
      the sector size minus 1, and LENGTH is between 0 and the sector size.  */
-  void (*read_hook) (grub_disk_addr_t sector,
+  void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector,
 		     unsigned offset, unsigned length);
 
   /* Device-specific data.  */

Index: include/grub/file.h
===================================================================
RCS file: /sources/grub/grub2/include/grub/file.h,v retrieving revision 1.5

grub/file.h +1 -1
diff -u -p -r1.5 file.h
@@ -43,7 +43,7 @@ struct grub_file
   void *data;
 
   /* This is called when a sector is read. Used only for a disk device.  */
-  void (*read_hook) (grub_disk_addr_t sector,
+  void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector,
 		     unsigned offset, unsigned length);
 };
 typedef struct grub_file *grub_file_t;

Index: include/grub/fshelp.h
===================================================================
RCS file: /sources/grub/grub2/include/grub/fshelp.h,v retrieving revision 1.4

grub/fshelp.h +1 -1
diff -u -p -r1.4 fshelp.h
@@ -63,7 +63,7 @@ EXPORT_FUNC(grub_fshelp_find_file) (cons
    blocks have a size of LOG2BLOCKSIZE (in log2).  */
 grub_ssize_t
 EXPORT_FUNC(grub_fshelp_read_file) (grub_disk_t disk, grub_fshelp_node_t node,
-				    void (*read_hook) (grub_disk_addr_t sector,
+				    void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector,
 						       unsigned offset,
 						       unsigned length),
 				    int pos, grub_size_t len, char *buf,

Index: util/i386/pc/grub-setup.c
===================================================================
RCS file: /sources/grub/grub2/util/i386/pc/grub-setup.c,v retrieving revision 1.27

i386/pc/grub-setup.c +4 -4
diff -u -p -r1.27 grub-setup.c
@@ -109,9 +109,9 @@ setup (const char *prefix, const char *d
   unsigned long first_start = ~0UL;
   int able_to_embed = 1;
   
-  auto void save_first_sector (grub_disk_addr_t sector, unsigned offset,
+  auto void NESTED_FUNC_ATTR save_first_sector (grub_disk_addr_t sector, unsigned offset,
 			       unsigned length);
-  auto void save_blocklists (grub_disk_addr_t sector, unsigned offset,
+  auto void NESTED_FUNC_ATTR save_blocklists (grub_disk_addr_t sector, unsigned offset,
 			     unsigned length);
 
   auto int find_first_partition_start (grub_disk_t disk,
@@ -130,7 +130,7 @@ setup (const char *prefix, const char *d
       return 0;
     }
   
-  void save_first_sector (grub_disk_addr_t sector, unsigned offset,
+  void NESTED_FUNC_ATTR save_first_sector (grub_disk_addr_t sector, unsigned offset,
 			  unsigned length)
     {
       grub_util_info ("the first sector is <%llu,%u,%u>",
@@ -142,7 +142,7 @@ setup (const char *prefix, const char *d
       first_sector = sector;
     }
 
-  void save_blocklists (grub_disk_addr_t sector, unsigned offset,
+  void NESTED_FUNC_ATTR save_blocklists (grub_disk_addr_t sector, unsigned offset,
 			unsigned length)
     {
       struct boot_blocklist *prev = block + 1;

Index: fs/affs.c
===================================================================
RCS file: /sources/grub/grub2/fs/affs.c,v
retrieving revision 1.5

affs.c +1 -1
diff -u -p -r1.5 affs.c
@@ -149,7 +149,7 @@ grub_affs_read_block (grub_fshelp_node_t
    POS.  Return the amount of read bytes in READ.  */
 static grub_ssize_t
 grub_affs_read_file (grub_fshelp_node_t node,
-		     void (*read_hook) (grub_disk_addr_t sector,
+		     void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector,
 					unsigned offset, unsigned length),
 		     int pos, grub_size_t len, char *buf)
 {

Index: fs/ext2.c
===================================================================
RCS file: /sources/grub/grub2/fs/ext2.c,v
retrieving revision 1.16

ext2.c +1 -1
diff -u -p -r1.16 ext2.c
@@ -267,7 +267,7 @@ grub_ext2_read_block (grub_fshelp_node_t
    POS.  Return the amount of read bytes in READ.  */
 static grub_ssize_t
 grub_ext2_read_file (grub_fshelp_node_t node,
-		     void (*read_hook) (grub_disk_addr_t sector,
+		     void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector,
 					unsigned offset, unsigned length),
 		     int pos, grub_size_t len, char *buf)
 {

Index: fs/fat.c
===================================================================
RCS file: /sources/grub/grub2/fs/fat.c,v
retrieving revision 1.17

fat.c +1 -1
diff -u -p -r1.17 fat.c
@@ -309,7 +309,7 @@ grub_fat_mount (grub_disk_t disk)
 
 static grub_ssize_t
 grub_fat_read_data (grub_disk_t disk, struct grub_fat_data *data,
-		    void (*read_hook) (grub_disk_addr_t sector,
+		    void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector,
 				       unsigned offset, unsigned length),
 		    grub_off_t offset, grub_size_t len, char *buf)
 {

Index: fs/fshelp.c
===================================================================
RCS file: /sources/grub/grub2/fs/fshelp.c,v
retrieving revision 1.8

fshelp.c +1 -1
diff -u -p -r1.8 fshelp.c
@@ -222,7 +222,7 @@ grub_fshelp_find_file (const char *path,
    blocks have a size of LOG2BLOCKSIZE (in log2).  */
 grub_ssize_t
 grub_fshelp_read_file (grub_disk_t disk, grub_fshelp_node_t node,
-		       void (*read_hook) (grub_disk_addr_t sector,
+		       void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector,
 					  unsigned offset, unsigned length),
 		       int pos, grub_size_t len, char *buf,
 		       int (*get_block) (grub_fshelp_node_t node, int block),

Index: fs/hfs.c
===================================================================
RCS file: /sources/grub/grub2/fs/hfs.c,v
retrieving revision 1.8

hfs.c +1 -1
diff -u -p -r1.8 hfs.c
@@ -228,7 +228,7 @@ grub_hfs_block (struct grub_hfs_data *da
    POS.  Return the amount of read bytes in READ.  */
 static grub_ssize_t
 grub_hfs_read_file (struct grub_hfs_data *data,
-		    void (*read_hook) (grub_disk_addr_t sector,
+		    void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector,
 				       unsigned offset, unsigned length),
 		     int pos, grub_size_t len, char *buf)
 {

Index: fs/hfsplus.c
===================================================================
RCS file: /sources/grub/grub2/fs/hfsplus.c,v
retrieving revision 1.9

hfsplus.c +1 -1
diff -u -p -r1.9 hfsplus.c
@@ -356,7 +356,7 @@ grub_hfsplus_read_block (grub_fshelp_nod
    POS.  Return the amount of read bytes in READ.  */
 static grub_ssize_t
 grub_hfsplus_read_file (grub_fshelp_node_t node,
-			void (*read_hook) (grub_disk_addr_t sector,
+			void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector,
 					   unsigned offset, unsigned length),
 			int pos, grub_size_t len, char *buf)
 {

Index: fs/jfs.c
===================================================================
RCS file: /sources/grub/grub2/fs/jfs.c,v
retrieving revision 1.7

jfs.c +1 -1
diff -u -p -r1.7 jfs.c
@@ -539,7 +539,7 @@ grub_jfs_getent (struct grub_jfs_diropen
    POS.  Return the amount of read bytes in READ.  */
 static grub_ssize_t
 grub_jfs_read_file (struct grub_jfs_data *data,
-		    void (*read_hook) (grub_disk_addr_t sector,
+		    void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector,
 				       unsigned offset, unsigned length),
 		    int pos, grub_size_t len, char *buf)
 {

Index: fs/minix.c
===================================================================
RCS file: /sources/grub/grub2/fs/minix.c,v
retrieving revision 1.6

minix.c +1 -1
diff -u -p -r1.6 minix.c
@@ -186,7 +186,7 @@ grub_minix_get_file_block (struct grub_m
    POS.  Return the amount of read bytes in READ.  */
 static grub_ssize_t
 grub_minix_read_file (struct grub_minix_data *data,
-		      void (*read_hook) (grub_disk_addr_t sector,
+		      void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector,
 					 unsigned offset, unsigned length),
 		      int pos, grub_disk_addr_t len, char *buf)
 {

Index: fs/sfs.c
===================================================================
RCS file: /sources/grub/grub2/fs/sfs.c,v
retrieving revision 1.6

sfs.c +1 -1
diff -u -p -r1.6 sfs.c
@@ -258,7 +258,7 @@ grub_sfs_read_block (grub_fshelp_node_t 
    POS.  Return the amount of read bytes in READ.  */
 static grub_ssize_t
 grub_sfs_read_file (grub_fshelp_node_t node,
-		    void (*read_hook) (grub_disk_addr_t sector,
+		    void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector,
 				       unsigned offset, unsigned length),
 		    int pos, grub_size_t len, char *buf)
 {

Index: fs/ufs.c
===================================================================
RCS file: /sources/grub/grub2/fs/ufs.c,v
retrieving revision 1.7

ufs.c +1 -1
diff -u -p -r1.7 ufs.c
@@ -240,7 +240,7 @@ grub_ufs_get_file_block (struct grub_ufs
    POS.  Return the amount of read bytes in READ.  */
 static grub_ssize_t
 grub_ufs_read_file (struct grub_ufs_data *data,
-		    void (*read_hook) (grub_disk_addr_t sector,
+		    void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector,
 				       unsigned offset, unsigned length),
 		    int pos, grub_size_t len, char *buf)
 {

Index: fs/xfs.c
===================================================================
RCS file: /sources/grub/grub2/fs/xfs.c,v
retrieving revision 1.9

xfs.c +1 -1
diff -u -p -r1.9 xfs.c
@@ -236,7 +236,7 @@ grub_xfs_read_block (grub_fshelp_node_t 
    POS.  Return the amount of read bytes in READ.  */
 static grub_ssize_t
 grub_xfs_read_file (grub_fshelp_node_t node,
-		     void (*read_hook) (grub_disk_addr_t sector,
+		     void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector,
 					unsigned offset, unsigned length),
 		     int pos, grub_size_t len, char *buf)
 {

Index: commands/blocklist.c
===================================================================
RCS file: /sources/grub/grub2/commands/blocklist.c,v retrieving revision 1.3

blocklist.c +4 -4
diff -u -p -r1.3 blocklist.c
@@ -36,12 +36,12 @@ grub_cmd_blocklist (struct grub_arg_list
   unsigned num_sectors = 0;
   int num_entries = 0;
   grub_disk_addr_t part_start = 0;
-  auto void read_blocklist (grub_disk_addr_t sector, unsigned offset,
+  auto void NESTED_FUNC_ATTR read_blocklist (grub_disk_addr_t sector, unsigned offset,
 			    unsigned length);
-  auto void print_blocklist (grub_disk_addr_t sector, unsigned num,
+  auto void NESTED_FUNC_ATTR print_blocklist (grub_disk_addr_t sector, unsigned num,
 			     unsigned offset, unsigned length);
   
-  void read_blocklist (grub_disk_addr_t sector, unsigned offset,
+  void NESTED_FUNC_ATTR read_blocklist (grub_disk_addr_t sector, unsigned offset,
 		       unsigned length)
     {
       if (num_sectors > 0)
@@ -66,7 +66,7 @@ grub_cmd_blocklist (struct grub_arg_list
 	print_blocklist (sector, 0, offset, length);
     }
   
-  void print_blocklist (grub_disk_addr_t sector, unsigned num,
+  void NESTED_FUNC_ATTR print_blocklist (grub_disk_addr_t sector, unsigned num,
 			unsigned offset, unsigned length)
     {
       if (num_entries++)
#5 lore raw
Quoted 6 lines · Bean
Quoted 2 lines · Marco Gerards

Yes, I was afraid of that... Can you make a patch+changelog of this so it can be fixed in CVS?

Ok, i basicly change read_hook in grub_disk and grub_file, then fix the warning. see if it works.

Do you mean that this patch was not tested? Can you please verify that this patch works before I commit this?

BTW, your changelog entry came without the header, but I can fix that before committing.

Thanks,
Marco

author
#6 lore raw
Quoted 12 lines · Marco Gerards
Quoted 6 lines · Bean
Quoted 2 lines · Marco Gerards

Yes, I was afraid of that... Can you make a patch+changelog of this so it can be fixed in CVS?

Ok, i basicly change read_hook in grub_disk and grub_file, then fix the warning. see if it works.

Do you mean that this patch was not tested? Can you please verify that this patch works before I commit this?

I have tested the blocklist command in fat, ntfs and ext2 partition, the result seems fine. I didn't try the other fs, but I guess they'll be ok.

Quoted 3 lines

BTW, your changelog entry came without the header, but I can fix that before committing.

Thanks.

Signature · 1 line
Bean
#7 lore raw
Quoted 10 lines · Bean
Quoted 9 lines · Marco Gerards
Quoted 6 lines · Bean
Quoted 2 lines · Marco Gerards

Yes, I was afraid of that... Can you make a patch+changelog of this so it can be fixed in CVS?

Ok, i basicly change read_hook in grub_disk and grub_file, then fix the warning. see if it works.

Quoted 7 lines
Quoted 3 lines

Do you mean that this patch was not tested? Can you please verify that this patch works before I commit this?

I have tested the blocklist command in fat, ntfs and ext2 partition, the result seems fine. I didn't try the other fs, but I guess they'll be ok.

Committed.

Thanks,
Marco