Blame SOURCES/0202-ieee1275-ofdisk-retry-on-open-failure.patch

5593c8
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
5593c8
From: Diego Domingos <diegodo@br.ibm.com>
5593c8
Date: Wed, 10 Mar 2021 14:17:52 -0500
5593c8
Subject: [PATCH] ieee1275/ofdisk: retry on open failure
5593c8
5593c8
This patch aims to make grub more robust when booting from SAN/Multipath disks.
5593c8
5593c8
If a path is failing intermittently so grub will retry the OPEN and READ the
5593c8
disk (grub_ieee1275_open and grub_ieee1275_read) until the total amount of times
5593c8
specified in MAX_RETRIES.
5593c8
5593c8
Signed-off-by: Diego Domingos <diegodo@br.ibm.com>
5593c8
---
5593c8
 grub-core/disk/ieee1275/ofdisk.c | 25 ++++++++++++++++++++-----
5593c8
 include/grub/ieee1275/ofdisk.h   |  8 ++++++++
5593c8
 2 files changed, 28 insertions(+), 5 deletions(-)
5593c8
5593c8
diff --git a/grub-core/disk/ieee1275/ofdisk.c b/grub-core/disk/ieee1275/ofdisk.c
1c6ba0
index ea7f78ac7d..55346849d3 100644
5593c8
--- a/grub-core/disk/ieee1275/ofdisk.c
5593c8
+++ b/grub-core/disk/ieee1275/ofdisk.c
5593c8
@@ -225,7 +225,9 @@ dev_iterate (const struct grub_ieee1275_devalias *alias)
5593c8
       char *buf, *bufptr;
5593c8
       unsigned i;
5593c8
 
5593c8
-      if (grub_ieee1275_open (alias->path, &ihandle))
5593c8
+
5593c8
+      RETRY_IEEE1275_OFDISK_OPEN(alias->path, &ihandle)
5593c8
+      if (! ihandle)
5593c8
 	return;
5593c8
 
5593c8
       /* This method doesn't need memory allocation for the table. Open
5593c8
@@ -305,7 +307,9 @@ dev_iterate (const struct grub_ieee1275_devalias *alias)
5593c8
           return;
5593c8
         }
5593c8
 
5593c8
-      if (grub_ieee1275_open (alias->path, &ihandle))
5593c8
+      RETRY_IEEE1275_OFDISK_OPEN(alias->path, &ihandle);
5593c8
+
5593c8
+      if (! ihandle)
5593c8
         {
5593c8
           grub_free (buf);
5593c8
           grub_free (table);
5593c8
@@ -495,7 +499,7 @@ grub_ofdisk_open (const char *name, grub_disk_t disk)
5593c8
     last_ihandle = 0;
5593c8
     last_devpath = NULL;
5593c8
 
5593c8
-    grub_ieee1275_open (op->open_path, &last_ihandle);
5593c8
+    RETRY_IEEE1275_OFDISK_OPEN(op->open_path, &last_ihandle);
5593c8
     if (! last_ihandle)
5593c8
       return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't open device");
5593c8
     last_devpath = op->open_path;
5593c8
@@ -571,7 +575,7 @@ grub_ofdisk_prepare (grub_disk_t disk, grub_disk_addr_t sector)
5593c8
       last_ihandle = 0;
5593c8
       last_devpath = NULL;
5593c8
 
5593c8
-      grub_ieee1275_open (disk->data, &last_ihandle);
5593c8
+      RETRY_IEEE1275_OFDISK_OPEN(disk->data, &last_ihandle);
5593c8
       if (! last_ihandle)
5593c8
 	return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't open device");
5593c8
       last_devpath = disk->data;      
5593c8
@@ -598,12 +602,23 @@ grub_ofdisk_read (grub_disk_t disk, grub_disk_addr_t sector,
5593c8
     return err;
5593c8
   grub_ieee1275_read (last_ihandle, buf, size  << disk->log_sector_size,
5593c8
 		      &actual);
5593c8
-  if (actual != (grub_ssize_t) (size  << disk->log_sector_size))
5593c8
+  int i = 0;
5593c8
+  while(actual != (grub_ssize_t) (size  << disk->log_sector_size)){
5593c8
+    if (i>MAX_RETRIES){
5593c8
     return grub_error (GRUB_ERR_READ_ERROR, N_("failure reading sector 0x%llx "
5593c8
 					       "from `%s'"),
5593c8
 		       (unsigned long long) sector,
5593c8
 		       disk->name);
5593c8
+    }
5593c8
+    last_devpath = NULL;
5593c8
+    err = grub_ofdisk_prepare (disk, sector);
5593c8
+    if (err)
5593c8
+      return err;
5593c8
 
5593c8
+    grub_ieee1275_read (last_ihandle, buf, size  << disk->log_sector_size,
5593c8
+                      &actual);
5593c8
+    i++;
5593c8
+  }
5593c8
   return 0;
5593c8
 }
5593c8
 
5593c8
diff --git a/include/grub/ieee1275/ofdisk.h b/include/grub/ieee1275/ofdisk.h
1c6ba0
index 2f69e3f191..7d2d540930 100644
5593c8
--- a/include/grub/ieee1275/ofdisk.h
5593c8
+++ b/include/grub/ieee1275/ofdisk.h
5593c8
@@ -22,4 +22,12 @@
5593c8
 extern void grub_ofdisk_init (void);
5593c8
 extern void grub_ofdisk_fini (void);
5593c8
 
5593c8
+#define MAX_RETRIES 20
5593c8
+
5593c8
+
5593c8
+#define RETRY_IEEE1275_OFDISK_OPEN(device, last_ihandle) unsigned retry_i=0;for(retry_i=0; retry_i < MAX_RETRIES; retry_i++){ \
5593c8
+						if(!grub_ieee1275_open(device, last_ihandle)) \
5593c8
+						break; \
5593c8
+						grub_dprintf("ofdisk","Opening disk %s failed. Retrying...\n",device); }
5593c8
+
5593c8
 #endif /* ! GRUB_INIT_HEADER */