grub-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] grub-install: force journal draining to ensure data integrity


From: Michael Chang
Subject: [PATCH] grub-install: force journal draining to ensure data integrity
Date: Wed, 29 Apr 2020 18:47:24 +0800
User-agent: Mutt/1.10.1 (2018-07-13)

In XFS, the system would end up in unbootable state if an abrupt power
off after grub-install is occuring. It can be easily reproduced with.

  grub-install /dev/vda; reboot -f

The grub error would show many different kinds of corruption in
filesystem and the problem boils down to incompleted journal transaction
which would leave pending writes behind in the on-disk journal. It is
therefore necessary to recover the system via re-mounting the filesystem
from linux system that all pending journal log can be replayed.

On the other hand if journal draining can be enforced by grub-install
then it can bring more resilience to such abrupt power loss. The fsync
is not enough here for XFS, because that only writes in-memory log to
on-disk (ie makes sure broken state can be repaired). Unfortunately
there's no designated system call to serve solely for the journal
draining, so it can only be achieved via fsfreeze system call that the
journal draining can happen as a byproduct during the process.

This patch adds fsfreeze/unfreeze at the end of grub-install to induce
journal draining on journaled file system. However btrfs is excluded
from the list as it is using fsync to drain journal and also is not
desired as reportedly having negative side effect. With this patch
applied, the boot falilure can no longer be reproduced with above
procedure.

Signed-off-by: Michael Chang <address@hidden>
---
 Makefile.util.def                    |  1 +
 grub-core/osdep/basic/journaled_fs.c | 26 +++++++++++++++++++
 grub-core/osdep/journaled_fs.c       |  5 ++++
 grub-core/osdep/linux/journaled_fs.c | 48 ++++++++++++++++++++++++++++++++++++
 include/grub/util/install.h          |  2 ++
 util/grub-install.c                  | 20 +++++++++++++++
 6 files changed, 102 insertions(+)
 create mode 100644 grub-core/osdep/basic/journaled_fs.c
 create mode 100644 grub-core/osdep/journaled_fs.c
 create mode 100644 grub-core/osdep/linux/journaled_fs.c

diff --git a/Makefile.util.def b/Makefile.util.def
index d9e2bd84d..8461f7b20 100644
--- a/Makefile.util.def
+++ b/Makefile.util.def
@@ -652,6 +652,7 @@ program = {
   common = util/resolve.c;
   common = grub-core/kern/emu/argp_common.c;
   common = grub-core/osdep/init.c;
+  common = grub-core/osdep/journaled_fs.c;
 
   ldadd = '$(LIBLZMA)';
   ldadd = libgrubmods.a;
diff --git a/grub-core/osdep/basic/journaled_fs.c 
b/grub-core/osdep/basic/journaled_fs.c
new file mode 100644
index 000000000..9d8245704
--- /dev/null
+++ b/grub-core/osdep/basic/journaled_fs.c
@@ -0,0 +1,26 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2010,2011,2012,2013  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/util/install.h>
+
+int
+grub_install_sync_fs_journal (const char *path)
+{
+  return 1;
+}
+
diff --git a/grub-core/osdep/journaled_fs.c b/grub-core/osdep/journaled_fs.c
new file mode 100644
index 000000000..fa5d0a199
--- /dev/null
+++ b/grub-core/osdep/journaled_fs.c
@@ -0,0 +1,5 @@
+#ifdef __linux__
+#include "linux/journaled_fs.c"
+#else
+#include "basic/journaled_fs.c"
+#endif
diff --git a/grub-core/osdep/linux/journaled_fs.c 
b/grub-core/osdep/linux/journaled_fs.c
new file mode 100644
index 000000000..0fa8360b1
--- /dev/null
+++ b/grub-core/osdep/linux/journaled_fs.c
@@ -0,0 +1,48 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2010,2011,2012,2013  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <fcntl.h>
+#include <linux/fs.h>
+#include <sys/ioctl.h>
+#include <errno.h>
+#include <grub/util/install.h>
+
+int
+grub_install_sync_fs_journal (const char *path)
+{
+  int fd, ret;
+
+  fd = open (path, O_RDONLY);
+
+  if (fd == -1)
+    return 1;
+
+  if (ioctl (fd, FIFREEZE, 0) == 0)
+    {
+      ioctl(fd, FITHAW, 0);
+      ret = 1;
+    }
+  else if (errno == EOPNOTSUPP)
+    ret = 1;
+  else
+    ret = 0;
+
+  close (fd);
+  return ret;
+}
+
diff --git a/include/grub/util/install.h b/include/grub/util/install.h
index 2631b1074..775c7442b 100644
--- a/include/grub/util/install.h
+++ b/include/grub/util/install.h
@@ -265,4 +265,6 @@ grub_util_get_target_name (const struct 
grub_install_image_target_desc *t);
 extern char *grub_install_copy_buffer;
 #define GRUB_INSTALL_COPY_BUFFER_SIZE 1048576
 
+int
+grub_install_sync_fs_journal (const char *path);
 #endif
diff --git a/util/grub-install.c b/util/grub-install.c
index 8970b73aa..843aa9fd4 100644
--- a/util/grub-install.c
+++ b/util/grub-install.c
@@ -42,6 +42,7 @@
 #include <grub/emu/config.h>
 #include <grub/util/ofpath.h>
 #include <grub/hfsplus.h>
+#include <grub/time.h>
 
 #include <string.h>
 
@@ -1962,6 +1963,25 @@ main (int argc, char *argv[])
       break;
     }
 
+  {
+    const char *journaled_fs[] = {"xfs", "ext2", NULL};
+    int i;
+
+    for (i = 0; journaled_fs[i]; ++i)
+      if (grub_strcmp (grub_fs->name, journaled_fs[i]) == 0)
+       {
+         int retries = 10;
+
+         /* If the fs is already frozen at that point, we could generally
+          * expected that it will be soon unfrozen again (assuming some other
+          * process has frozen it for snapshotting or something), so we may
+          * as well retry a few (limited) times in a delay loop. */
+         while (retries-- && !grub_install_sync_fs_journal (grubdir))
+           grub_sleep (1);
+         break;
+       }
+  }
+
   fprintf (stderr, "%s\n", _("Installation finished. No error reported."));
 
   /* Free resources.  */
-- 
2.16.4




reply via email to

[Prev in Thread] Current Thread [Next in Thread]