Blob Blame History Raw
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Diego Domingos <diegodo@br.ibm.com>
Date: Mon, 17 May 2021 16:59:04 +0200
Subject: [PATCH] ieee1275/ofdisk: retry on open failure

This patch aims to make grub more robust when booting from SAN/Multipath disks.
If a path is failing intermittently so grub will retry the OPEN and READ the
disk (grub_ieee1275_open and grub_ieee1275_read) until the total amount of times
specified in MAX_RETRIES.

Signed-off-by: Diego Domingos <diegodo@br.ibm.com>
---
 grub-core/disk/ieee1275/ofdisk.c | 32 ++++++++++++++++++++++++--------
 include/grub/ieee1275/ofdisk.h   |  8 ++++++++
 2 files changed, 32 insertions(+), 8 deletions(-)

diff --git a/grub-core/disk/ieee1275/ofdisk.c b/grub-core/disk/ieee1275/ofdisk.c
index 5fabe365ec..578d283327 100644
--- a/grub-core/disk/ieee1275/ofdisk.c
+++ b/grub-core/disk/ieee1275/ofdisk.c
@@ -220,7 +220,9 @@ dev_iterate (const struct grub_ieee1275_devalias *alias)
       char *buf, *bufptr;
       unsigned i;
 
-      if (grub_ieee1275_open (alias->path, &ihandle))
+
+      RETRY_IEEE1275_OFDISK_OPEN(alias->path, ihandle)
+      if (! ihandle)
 	return;
     
       INIT_IEEE1275_COMMON (&args.common, "call-method", 2, 3);
@@ -408,7 +410,8 @@ grub_ofdisk_open (const char *name, grub_disk_t disk)
     last_ihandle = 0;
     last_devpath = NULL;
 
-    grub_ieee1275_open (op->open_path, &last_ihandle);
+    RETRY_IEEE1275_OFDISK_OPEN(op->open_path, last_ihandle)
+
     if (! last_ihandle)
       return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't open device");
     last_devpath = op->open_path;
@@ -481,7 +484,7 @@ grub_ofdisk_prepare (grub_disk_t disk, grub_disk_addr_t sector)
       last_ihandle = 0;
       last_devpath = NULL;
 
-      grub_ieee1275_open (disk->data, &last_ihandle);
+      RETRY_IEEE1275_OFDISK_OPEN(disk->data, last_ihandle);
       if (! last_ihandle)
 	return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't open device");
       last_devpath = disk->data;      
@@ -508,11 +511,24 @@ grub_ofdisk_read (grub_disk_t disk, grub_disk_addr_t sector,
     return err;
   grub_ieee1275_read (last_ihandle, buf, size  << disk->log_sector_size,
 		      &actual);
-  if (actual != (grub_ssize_t) (size  << disk->log_sector_size))
-    return grub_error (GRUB_ERR_READ_ERROR, N_("failure reading sector 0x%llx "
-					       "from `%s'"),
-		       (unsigned long long) sector,
-		       disk->name);
+
+  int i = 0;
+  while(actual != (grub_ssize_t) (size  << disk->log_sector_size)){
+      if (i > MAX_RETRIES){
+       return grub_error (GRUB_ERR_READ_ERROR, N_("failure reading sector 0x%llx "
+                                               "from `%s'"),
+                       (unsigned long long) sector,
+                       disk->name);
+      }
+      grub_dprintf("ofdisk","Read failed. Retrying...\n");
+      last_devpath = NULL;
+      err = grub_ofdisk_prepare (disk, sector);
+      if (err)
+        return err;
+      grub_ieee1275_read (last_ihandle, buf, size  << disk->log_sector_size,
+                      &actual);
+      i++;
+   }
 
   return 0;
 }
diff --git a/include/grub/ieee1275/ofdisk.h b/include/grub/ieee1275/ofdisk.h
index 3f58317874..7446b67021 100644
--- a/include/grub/ieee1275/ofdisk.h
+++ b/include/grub/ieee1275/ofdisk.h
@@ -25,4 +25,12 @@ extern void grub_ofdisk_fini (void);
 extern grub_err_t grub_ofdisk_get_block_size (const char *device,
                                               grub_uint32_t *block_size);
 
+#define MAX_RETRIES 20
+
+
+#define RETRY_IEEE1275_OFDISK_OPEN(device, last_ihandle) unsigned retry_i=0;for(retry_i=0; retry_i < MAX_RETRIES; retry_i++){ \
+						if(!grub_ieee1275_open(device, & last_ihandle)) \
+						break; \
+						grub_dprintf("ofdisk","Opening disk %s failed. Retrying...\n",device); }
+
 #endif /* ! GRUB_INIT_HEADER */