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