Blame SOURCES/0325-ieee1275-Avoiding-many-unecessary-open-close.patch

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