d18179
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
70a9d4
From: Diego Domingos <diegodo@br.ibm.com>
70a9d4
Date: Mon, 17 May 2021 16:59:04 +0200
70a9d4
Subject: [PATCH] ieee1275/ofdisk: retry on open failure
70a9d4
70a9d4
This patch aims to make grub more robust when booting from SAN/Multipath disks.
70a9d4
If a path is failing intermittently so grub will retry the OPEN and READ the
70a9d4
disk (grub_ieee1275_open and grub_ieee1275_read) until the total amount of times
70a9d4
specified in MAX_RETRIES.
70a9d4
70a9d4
Signed-off-by: Diego Domingos <diegodo@br.ibm.com>
70a9d4
---
70a9d4
 grub-core/disk/ieee1275/ofdisk.c | 32 ++++++++++++++++++++++++--------
70a9d4
 include/grub/ieee1275/ofdisk.h   |  8 ++++++++
70a9d4
 2 files changed, 32 insertions(+), 8 deletions(-)
70a9d4
70a9d4
diff --git a/grub-core/disk/ieee1275/ofdisk.c b/grub-core/disk/ieee1275/ofdisk.c
d18179
index 5fabe365ec..578d283327 100644
70a9d4
--- a/grub-core/disk/ieee1275/ofdisk.c
70a9d4
+++ b/grub-core/disk/ieee1275/ofdisk.c
70a9d4
@@ -220,7 +220,9 @@ dev_iterate (const struct grub_ieee1275_devalias *alias)
70a9d4
       char *buf, *bufptr;
70a9d4
       unsigned i;
70a9d4
 
70a9d4
-      if (grub_ieee1275_open (alias->path, &ihandle))
70a9d4
+
70a9d4
+      RETRY_IEEE1275_OFDISK_OPEN(alias->path, ihandle)
70a9d4
+      if (! ihandle)
70a9d4
 	return;
70a9d4
     
70a9d4
       INIT_IEEE1275_COMMON (&args.common, "call-method", 2, 3);
70a9d4
@@ -408,7 +410,8 @@ grub_ofdisk_open (const char *name, grub_disk_t disk)
70a9d4
     last_ihandle = 0;
70a9d4
     last_devpath = NULL;
70a9d4
 
70a9d4
-    grub_ieee1275_open (op->open_path, &last_ihandle);
70a9d4
+    RETRY_IEEE1275_OFDISK_OPEN(op->open_path, last_ihandle)
70a9d4
+
70a9d4
     if (! last_ihandle)
70a9d4
       return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't open device");
70a9d4
     last_devpath = op->open_path;
70a9d4
@@ -481,7 +484,7 @@ grub_ofdisk_prepare (grub_disk_t disk, grub_disk_addr_t sector)
70a9d4
       last_ihandle = 0;
70a9d4
       last_devpath = NULL;
70a9d4
 
70a9d4
-      grub_ieee1275_open (disk->data, &last_ihandle);
70a9d4
+      RETRY_IEEE1275_OFDISK_OPEN(disk->data, last_ihandle);
70a9d4
       if (! last_ihandle)
70a9d4
 	return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't open device");
70a9d4
       last_devpath = disk->data;      
70a9d4
@@ -508,11 +511,24 @@ grub_ofdisk_read (grub_disk_t disk, grub_disk_addr_t sector,
70a9d4
     return err;
70a9d4
   grub_ieee1275_read (last_ihandle, buf, size  << disk->log_sector_size,
70a9d4
 		      &actual);
70a9d4
-  if (actual != (grub_ssize_t) (size  << disk->log_sector_size))
70a9d4
-    return grub_error (GRUB_ERR_READ_ERROR, N_("failure reading sector 0x%llx "
70a9d4
-					       "from `%s'"),
70a9d4
-		       (unsigned long long) sector,
70a9d4
-		       disk->name);
70a9d4
+
70a9d4
+  int i = 0;
70a9d4
+  while(actual != (grub_ssize_t) (size  << disk->log_sector_size)){
70a9d4
+      if (i > MAX_RETRIES){
70a9d4
+       return grub_error (GRUB_ERR_READ_ERROR, N_("failure reading sector 0x%llx "
70a9d4
+                                               "from `%s'"),
70a9d4
+                       (unsigned long long) sector,
70a9d4
+                       disk->name);
70a9d4
+      }
70a9d4
+      grub_dprintf("ofdisk","Read failed. Retrying...\n");
70a9d4
+      last_devpath = NULL;
70a9d4
+      err = grub_ofdisk_prepare (disk, sector);
70a9d4
+      if (err)
70a9d4
+        return err;
70a9d4
+      grub_ieee1275_read (last_ihandle, buf, size  << disk->log_sector_size,
70a9d4
+                      &actual);
70a9d4
+      i++;
70a9d4
+   }
70a9d4
 
70a9d4
   return 0;
70a9d4
 }
70a9d4
diff --git a/include/grub/ieee1275/ofdisk.h b/include/grub/ieee1275/ofdisk.h
d18179
index 3f58317874..7446b67021 100644
70a9d4
--- a/include/grub/ieee1275/ofdisk.h
70a9d4
+++ b/include/grub/ieee1275/ofdisk.h
70a9d4
@@ -25,4 +25,12 @@ extern void grub_ofdisk_fini (void);
70a9d4
 extern grub_err_t grub_ofdisk_get_block_size (const char *device,
70a9d4
                                               grub_uint32_t *block_size);
70a9d4
 
70a9d4
+#define MAX_RETRIES 20
70a9d4
+
70a9d4
+
70a9d4
+#define RETRY_IEEE1275_OFDISK_OPEN(device, last_ihandle) unsigned retry_i=0;for(retry_i=0; retry_i < MAX_RETRIES; retry_i++){ \
70a9d4
+						if(!grub_ieee1275_open(device, & last_ihandle)) \
70a9d4
+						break; \
70a9d4
+						grub_dprintf("ofdisk","Opening disk %s failed. Retrying...\n",device); }
70a9d4
+
70a9d4
 #endif /* ! GRUB_INIT_HEADER */