Skip to contentSkip to conversation

simplify grub_raid_array

PATCH In review Robert Millan 2 participants 6 messages Feb 6, 2008 – Feb 8, 2008

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 simplify grub_raid_array

Unless I missed something, it seems that grub_raid_array contains redundant information (name' is already present via disk->name'). I propose to simplify it this way.

Signature · 5 lines
Robert Millan

<GPLv2> I know my rights; I want my phone call!
<DRM> What use is a phone call… if you are unable to speak?
(as seen on /.)
disk/raid.c renamed from grub2/disk/raid.c +10 -14
diff -x configure -x config.h.in -x CVS -x '*~' -x '*.mk' -urp ../grub2/disk/raid.c ./disk/raid.c
@@ -146,7 +146,7 @@ grub_raid_read (grub_disk_t disk, grub_d
 	  {
 	    grub_uint32_t i;
 
-	    err = grub_disk_read (array->device[disknr].disk, read_sector, 0,
+	    err = grub_disk_read (array->device[disknr], read_sector, 0,
 				  read_size << GRUB_DISK_SECTOR_BITS, buf);
 	    if (err)
 	      break;
@@ -189,9 +189,9 @@ grub_raid_read (grub_disk_t disk, grub_d
 	
 	for (i = 0; i < array->total_devs; i++)
 	  {
-	    if (array->device[i].disk)
+	    if (array->device[i])
 	      {
-		err = grub_disk_read (array->device[i].disk, sector, 0,
+		err = grub_disk_read (array->device[i], sector, 0,
 				      size << GRUB_DISK_SECTOR_BITS, buf);
 
 		if (!err)
@@ -234,8 +234,8 @@ grub_raid_read (grub_disk_t disk, grub_d
 	  {
 	    grub_uint32_t i;
 
-	    if (array->device[disknr].disk)
-	      err = grub_disk_read (array->device[disknr].disk, read_sector, 0,
+	    if (array->device[disknr])
+	      err = grub_disk_read (array->device[disknr], read_sector, 0,
 				    read_size << GRUB_DISK_SECTOR_BITS, buf);
 
 	    /* If an error occurs when we already have an degraded
@@ -243,7 +243,7 @@ grub_raid_read (grub_disk_t disk, grub_d
 	    if (err && ((array->total_devs - 1) == array->nr_devs))
 	      break;
 	    
-	    if (err || ! array->device[disknr].disk)
+	    if (err || ! array->device[disknr])
 	      {
 		/* Either an error occured or the disk is not
 		   available.  We have to compute this block from the
@@ -260,7 +260,7 @@ grub_raid_read (grub_disk_t disk, grub_d
 
 		    if (j != (unsigned int) disknr)
 		      {
-			err = grub_disk_read (array->device[j].disk, read_sector,
+			err = grub_disk_read (array->device[j], read_sector,
 					      0, buf_size, buf2);
 			if (err)
 			  return err;
@@ -410,7 +410,7 @@ grub_raid_scan_device (const char *name)
 	  return 0;
 	}
   
-      if (array->device[sb.this_disk.number].name != 0)
+      if (array->device[sb.this_disk.number]->name != 0)
 	{
 	  /* We found multiple devices with the same number. Again,
 	     this shouldn't happen.*/
@@ -491,14 +491,10 @@ grub_raid_scan_device (const char *name)
     }
 
   /* Add the device to the array. */
-  array->device[sb.this_disk.number].name = grub_strdup (name);
-  array->device[sb.this_disk.number].disk = grub_disk_open (name);
+  array->device[sb.this_disk.number] = grub_disk_open (name);
   
-  if (! array->device[sb.this_disk.number].name
-      || ! array->device[sb.this_disk.number].disk)
+  if (! array->device[sb.this_disk.number])
     {
-      grub_free (array->device[sb.this_disk.number].name);
-
       /* Remove array from the list if we have just added it. */
       if (array->nr_devs == 0)
 	{
include/grub/raid.h renamed from grub2/include/grub/raid.h +1 -5
diff -x configure -x config.h.in -x CVS -x '*~' -x '*.mk' -urp ../grub2/include/grub/raid.h ./include/grub/raid.h
@@ -37,11 +37,7 @@ struct grub_raid_array
   char *name;              /* That will be "md<number>". */
   grub_uint64_t disk_size; /* Size of an individual disk, in 512 byte
 			      sectors. */
-  struct
-  {
-    char *name;            /* Name of the device */
-    grub_disk_t disk;      /* The device itself. */
-  } device[32];            /* Array of total_devs devices. */          
+  grub_disk_t device[32];  /* Array of total_devs devices. */          
   struct grub_raid_array *next;
 };
 
#1 lore raw

At Wed, 6 Feb 2008 17:45:07 +0100,

Quoted 3 lines · Robert Millan

Unless I missed something, it seems that grub_raid_array contains redundant information (name' is already present via disk->name'). I propose to simplify it this way.

No idea why, I don't have the time to look at the actual code, but

disk/raid.c grub_raid_scan_device (const char *name) · line 416

Quoted 9 lines
disk/raid.c renamed from grub2/disk/raid.c +1 -1
@@ -410,7 +410,7 @@ grub_raid_scan_device (const char *name)
 	  return 0;
 	}
 
-      if (array->device[sb.this_disk.number].name != 0)
+      if (array->device[sb.this_disk.number]->name != 0)
 	{
 	  /* We found multiple devices with the same number. Again,
 	     this shouldn't happen.*/

looks suspicious to me. Is that really doing what it is meant to do?

Regards,

Jeroen Dekkers

#2 lore raw
Quoted 12 lines · Jeroen Dekkers

At Wed, 6 Feb 2008 17:45:07 +0100,

Quoted 3 lines · Robert Millan

Unless I missed something, it seems that grub_raid_array contains redundant information (name' is already present via disk->name'). I propose to simplify it this way.

No idea why, I don't have the time to look at the actual code, but

Quoted 4 lines
disk/raid.c renamed from grub2/disk/raid.c +0 -0
@@ -410,7 +410,7 @@ grub_raid_scan_device (const char *name)
 	  return 0;
 	}
 
Quoted 7 lines

disk/raid.c grub_raid_scan_device (const char *name) · line 416

Quoted 5 lines
disk/raid.c renamed from grub2/disk/raid.c +1 -1
-      if (array->device[sb.this_disk.number].name != 0)
+      if (array->device[sb.this_disk.number]->name != 0)
 	{
 	  /* We found multiple devices with the same number. Again,
 	     this shouldn't happen.*/

looks suspicious to me. Is that really doing what it is meant to do?

Yes. `array->device[sb.this_disk.number]' used to be:

struct { char *name; grub_disk_t disk; }

and it becomes grub_disk_t, which is:

struct { char *name; other stuff; } *

this is tested in runtime. My concern was more about whether both `name' instances could have different meaning or so.

Signature · 5 lines
Robert Millan

<GPLv2> I know my rights; I want my phone call!
<DRM> What use is a phone call… if you are unable to speak?
(as seen on /.)
#3 lore raw

At Wed, 6 Feb 2008 23:59:54 +0100,

Quoted 22 lines · Robert Millan
Quoted 12 lines · Jeroen Dekkers

At Wed, 6 Feb 2008 17:45:07 +0100,

Quoted 3 lines · Robert Millan

Unless I missed something, it seems that grub_raid_array contains redundant information (name' is already present via disk->name'). I propose to simplify it this way.

No idea why, I don't have the time to look at the actual code, but

Quoted 4 lines
disk/raid.c renamed from grub2/disk/raid.c +0 -0
@@ -410,7 +410,7 @@ grub_raid_scan_device (const char *name)
 	  return 0;
 	}
 
Quoted 7 lines

disk/raid.c grub_raid_scan_device (const char *name) · line 416

Quoted 5 lines
disk/raid.c renamed from grub2/disk/raid.c +1 -1
-      if (array->device[sb.this_disk.number].name != 0)
+      if (array->device[sb.this_disk.number]->name != 0)
 	{
 	  /* We found multiple devices with the same number. Again,
 	     this shouldn't happen.*/

looks suspicious to me. Is that really doing what it is meant to do?

Quoted 1 line

Yes.

No, it doesn't. The meaning of the check .name != 0 is whether a device with that number already exists in the array. If .name is 0, it doesn't exist yet, because else it would've been assigned something. What you're now doing is wrong because if there isn't a device in the array then array->device[sb.this_disk.number] is 0. I guess the only reason this actually works is because the first page of memory (the one at address 0) happens to be zero by luck.

Jeroen Dekkers

#4 lore raw
Quoted 26 lines · Jeroen Dekkers

At Wed, 6 Feb 2008 23:59:54 +0100,

Quoted 22 lines · Robert Millan
Quoted 12 lines · Jeroen Dekkers

At Wed, 6 Feb 2008 17:45:07 +0100,

Quoted 3 lines · Robert Millan

Unless I missed something, it seems that grub_raid_array contains redundant information (name' is already present via disk->name'). I propose to simplify it this way.

No idea why, I don't have the time to look at the actual code, but

Quoted 4 lines
disk/raid.c renamed from grub2/disk/raid.c +0 -0
@@ -410,7 +410,7 @@ grub_raid_scan_device (const char *name)
 	  return 0;
 	}
 
Quoted 7 lines

disk/raid.c grub_raid_scan_device (const char *name) · line 416

Quoted 5 lines
disk/raid.c renamed from grub2/disk/raid.c +1 -1
-      if (array->device[sb.this_disk.number].name != 0)
+      if (array->device[sb.this_disk.number]->name != 0)
 	{
 	  /* We found multiple devices with the same number. Again,
 	     this shouldn't happen.*/

looks suspicious to me. Is that really doing what it is meant to do?

Quoted 1 line

Yes.

Quoted 7 lines

No, it doesn't. The meaning of the check .name != 0 is whether a device with that number already exists in the array. If .name is 0, it doesn't exist yet, because else it would've been assigned something. What you're now doing is wrong because if there isn't a device in the array then array->device[sb.this_disk.number] is 0. I guess the only reason this actually works is because the first page of memory (the one at address 0) happens to be zero by luck.

Yep. I just fixed this a while ago (Sam caught that in a segfault).

Does it look good now?

Signature · 5 lines
Robert Millan

<GPLv2> I know my rights; I want my phone call!
<DRM> What use is a phone call… if you are unable to speak?
(as seen on /.)
#5 lore raw
In reply to Robert Millan
Quoted 4 lines · Robert Millan

Unless I missed something, it seems that grub_raid_array contains redundant information (name' is already present via disk->name'). I propose to simplify it this way.

Committed.

Signature · 5 lines
Robert Millan

<GPLv2> I know my rights; I want my phone call!
<DRM> What use is a phone call… if you are unable to speak?
(as seen on /.)