46968b6
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
46968b6
From: Diego Domingos <diegodo@br.ibm.com>
46968b6
Date: Mon, 14 Dec 2020 17:42:45 +0100
46968b6
Subject: [PATCH] ieee1275: Avoiding many unecessary open/close
46968b6
46968b6
Signed-off-by: Diego Domingos <diegodo@br.ibm.com>
46968b6
---
46968b6
 grub-core/disk/ieee1275/ofdisk.c | 64 ++++++++++++++++++++++------------------
46968b6
 1 file changed, 35 insertions(+), 29 deletions(-)
46968b6
46968b6
diff --git a/grub-core/disk/ieee1275/ofdisk.c b/grub-core/disk/ieee1275/ofdisk.c
46968b6
index 03674cb477e..ea7f78ac7d8 100644
46968b6
--- a/grub-core/disk/ieee1275/ofdisk.c
46968b6
+++ b/grub-core/disk/ieee1275/ofdisk.c
46968b6
@@ -44,7 +44,7 @@ struct ofdisk_hash_ent
46968b6
 };
46968b6
 
46968b6
 static grub_err_t
46968b6
-grub_ofdisk_get_block_size (const char *device, grub_uint32_t *block_size,
46968b6
+grub_ofdisk_get_block_size (grub_uint32_t *block_size,
46968b6
 			    struct ofdisk_hash_ent *op);
46968b6
 
46968b6
 #define OFDISK_HASH_SZ	8
46968b6
@@ -461,6 +461,7 @@ grub_ofdisk_open (const char *name, grub_disk_t disk)
46968b6
   grub_ssize_t actual;
46968b6
   grub_uint32_t block_size = 0;
46968b6
   grub_err_t err;
46968b6
+  struct ofdisk_hash_ent *op;
46968b6
 
46968b6
   if (grub_strncmp (name, "ieee1275/", sizeof ("ieee1275/") - 1) != 0)
46968b6
       return grub_error (GRUB_ERR_UNKNOWN_DEVICE,
46968b6
@@ -471,6 +472,35 @@ grub_ofdisk_open (const char *name, grub_disk_t disk)
46968b6
 
46968b6
   grub_dprintf ("disk", "Opening `%s'.\n", devpath);
46968b6
 
46968b6
+  op = ofdisk_hash_find (devpath);
46968b6
+  if (!op)
46968b6
+    op = ofdisk_hash_add (devpath, NULL);
46968b6
+  if (!op)
46968b6
+    {
46968b6
+      grub_free (devpath);
46968b6
+      return grub_errno;
46968b6
+    }
46968b6
+
46968b6
+  /* Check if the call to open is the same to the last disk already opened */
46968b6
+  if (last_devpath && !grub_strcmp(op->open_path,last_devpath))
46968b6
+  {
46968b6
+      goto finish;
46968b6
+  }
46968b6
+
46968b6
+ /* If not, we need to close the previous disk and open the new one */
46968b6
+  else {
46968b6
+    if (last_ihandle){
46968b6
+        grub_ieee1275_close (last_ihandle);
46968b6
+    }
46968b6
+    last_ihandle = 0;
46968b6
+    last_devpath = NULL;
46968b6
+
46968b6
+    grub_ieee1275_open (op->open_path, &last_ihandle);
46968b6
+    if (! last_ihandle)
46968b6
+      return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't open device");
46968b6
+    last_devpath = op->open_path;
46968b6
+  }
46968b6
+
46968b6
   if (grub_ieee1275_finddevice (devpath, &dev))
46968b6
     {
46968b6
       grub_free (devpath);
46968b6
@@ -491,25 +521,18 @@ grub_ofdisk_open (const char *name, grub_disk_t disk)
46968b6
       return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "not a block device");
46968b6
     }
46968b6
 
46968b6
+
46968b6
+  finish:
46968b6
   /* XXX: There is no property to read the number of blocks.  There
46968b6
      should be a property `#blocks', but it is not there.  Perhaps it
46968b6
      is possible to use seek for this.  */
46968b6
   disk->total_sectors = GRUB_DISK_SIZE_UNKNOWN;
46968b6
 
46968b6
   {
46968b6
-    struct ofdisk_hash_ent *op;
46968b6
-    op = ofdisk_hash_find (devpath);
46968b6
-    if (!op)
46968b6
-      op = ofdisk_hash_add (devpath, NULL);
46968b6
-    if (!op)
46968b6
-      {
46968b6
-        grub_free (devpath);
46968b6
-        return grub_errno;
46968b6
-      }
46968b6
     disk->id = (unsigned long) op;
46968b6
     disk->data = op->open_path;
46968b6
 
46968b6
-    err = grub_ofdisk_get_block_size (devpath, &block_size, op);
46968b6
+    err = grub_ofdisk_get_block_size (&block_size, op);
46968b6
     if (err)
46968b6
       {
46968b6
         grub_free (devpath);
46968b6
@@ -532,13 +555,6 @@ grub_ofdisk_open (const char *name, grub_disk_t disk)
46968b6
 static void
46968b6
 grub_ofdisk_close (grub_disk_t disk)
46968b6
 {
46968b6
-  if (disk->data == last_devpath)
46968b6
-    {
46968b6
-      if (last_ihandle)
46968b6
-	grub_ieee1275_close (last_ihandle);
46968b6
-      last_ihandle = 0;
46968b6
-      last_devpath = NULL;
46968b6
-    }
46968b6
   disk->data = 0;
46968b6
 }
46968b6
 
46968b6
@@ -685,7 +701,7 @@ grub_ofdisk_init (void)
46968b6
 }
46968b6
 
46968b6
 static grub_err_t
46968b6
-grub_ofdisk_get_block_size (const char *device, grub_uint32_t *block_size,
46968b6
+grub_ofdisk_get_block_size (grub_uint32_t *block_size,
46968b6
 			    struct ofdisk_hash_ent *op)
46968b6
 {
46968b6
   struct size_args_ieee1275
46968b6
@@ -698,16 +714,6 @@ grub_ofdisk_get_block_size (const char *device, grub_uint32_t *block_size,
46968b6
       grub_ieee1275_cell_t size2;
46968b6
     } args_ieee1275;
46968b6
 
46968b6
-  if (last_ihandle)
46968b6
-    grub_ieee1275_close (last_ihandle);
46968b6
-
46968b6
-  last_ihandle = 0;
46968b6
-  last_devpath = NULL;
46968b6
-
46968b6
-  grub_ieee1275_open (device, &last_ihandle);
46968b6
-  if (! last_ihandle)
46968b6
-    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't open device");
46968b6
-
46968b6
   *block_size = 0;
46968b6
 
46968b6
   if (op->block_size_fails >= 2)