nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0064-Grub-not-working-correctly-with-btrfs-snapshots-bsc-.patch

8e15ce
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
8e15ce
From: Michael Chang <mchang@suse.com>
8e15ce
Date: Thu, 11 May 2017 08:56:57 +0000
8e15ce
Subject: [PATCH] Grub not working correctly with btrfs snapshots (bsc#1026511)
8e15ce
8e15ce
---
8e15ce
 grub-core/fs/btrfs.c | 238 +++++++++++++++++++++++++++++++++++++++++++++++++++
8e15ce
 1 file changed, 238 insertions(+)
8e15ce
8e15ce
diff --git a/grub-core/fs/btrfs.c b/grub-core/fs/btrfs.c
8e15ce
index 673ded03522..2b21cbaa67e 100644
8e15ce
--- a/grub-core/fs/btrfs.c
8e15ce
+++ b/grub-core/fs/btrfs.c
8e15ce
@@ -2887,6 +2887,238 @@ out:
8e15ce
   return 0;
8e15ce
 }
8e15ce
 
8e15ce
+static grub_err_t
8e15ce
+grub_btrfs_get_parent_subvol_path (struct grub_btrfs_data *data,
8e15ce
+		grub_uint64_t child_id,
8e15ce
+		const char *child_path,
8e15ce
+		grub_uint64_t *parent_id,
8e15ce
+		char **path_out)
8e15ce
+{
8e15ce
+  grub_uint64_t fs_root = 0;
8e15ce
+  struct grub_btrfs_key key_in = {
8e15ce
+    .object_id = child_id,
8e15ce
+    .type = GRUB_BTRFS_ITEM_TYPE_ROOT_BACKREF,
8e15ce
+    .offset = 0,
8e15ce
+  }, key_out;
8e15ce
+  struct grub_btrfs_root_ref *ref;
8e15ce
+  char *buf;
8e15ce
+  struct grub_btrfs_leaf_descriptor desc;
8e15ce
+  grub_size_t elemsize;
8e15ce
+  grub_disk_addr_t elemaddr;
8e15ce
+  grub_err_t err;
8e15ce
+  char *parent_path;
8e15ce
+
8e15ce
+  *parent_id = 0;
8e15ce
+  *path_out = 0;
8e15ce
+
8e15ce
+  err = lower_bound(data, &key_in, &key_out, data->sblock.root_tree,
8e15ce
+                    &elemaddr, &elemsize, &desc, 0);
8e15ce
+  if (err)
8e15ce
+    return err;
8e15ce
+
8e15ce
+  if (key_out.type != GRUB_BTRFS_ITEM_TYPE_ROOT_BACKREF || elemaddr == 0)
8e15ce
+    next(data, &desc, &elemaddr, &elemsize, &key_out);
8e15ce
+
8e15ce
+  if (key_out.type != GRUB_BTRFS_ITEM_TYPE_ROOT_BACKREF)
8e15ce
+    {
8e15ce
+      free_iterator(&desc);
8e15ce
+      return grub_error(GRUB_ERR_FILE_NOT_FOUND, N_("can't find root backrefs"));
8e15ce
+    }
8e15ce
+
8e15ce
+  buf = grub_malloc(elemsize + 1);
8e15ce
+  if (!buf)
8e15ce
+    {
8e15ce
+      free_iterator(&desc);
8e15ce
+      return grub_errno;
8e15ce
+    }
8e15ce
+
8e15ce
+  err = grub_btrfs_read_logical(data, elemaddr, buf, elemsize, 0);
8e15ce
+  if (err)
8e15ce
+    {
8e15ce
+      grub_free(buf);
8e15ce
+      free_iterator(&desc);
8e15ce
+      return err;
8e15ce
+    }
8e15ce
+
8e15ce
+  buf[elemsize] = 0;
8e15ce
+  ref = (struct grub_btrfs_root_ref *)buf;
8e15ce
+
8e15ce
+  err = get_fs_root(data, data->sblock.root_tree, grub_le_to_cpu64 (key_out.offset),
8e15ce
+                    0, &fs_root);
8e15ce
+  if (err)
8e15ce
+    {
8e15ce
+      grub_free(buf);
8e15ce
+      free_iterator(&desc);
8e15ce
+      return err;
8e15ce
+    }
8e15ce
+
8e15ce
+  find_pathname(data, grub_le_to_cpu64 (ref->dirid), fs_root, ref->name, &parent_path);
8e15ce
+
8e15ce
+  if (child_path)
8e15ce
+    {
8e15ce
+      *path_out = grub_xasprintf ("%s/%s", parent_path, child_path);
8e15ce
+      grub_free (parent_path);
8e15ce
+    }
8e15ce
+  else
8e15ce
+    *path_out = parent_path;
8e15ce
+
8e15ce
+  *parent_id = grub_le_to_cpu64 (key_out.offset);
8e15ce
+
8e15ce
+  grub_free(buf);
8e15ce
+  free_iterator(&desc);
8e15ce
+  return GRUB_ERR_NONE;
8e15ce
+}
8e15ce
+
8e15ce
+static grub_err_t
8e15ce
+grub_btrfs_get_default_subvolume_id (struct grub_btrfs_data *data, grub_uint64_t *id)
8e15ce
+{
8e15ce
+  grub_err_t err;
8e15ce
+  grub_disk_addr_t elemaddr;
8e15ce
+  grub_size_t elemsize;
8e15ce
+  struct grub_btrfs_key key, key_out;
8e15ce
+  struct grub_btrfs_dir_item *direl = NULL;
8e15ce
+  const char *ctoken = "default";
8e15ce
+  grub_size_t ctokenlen = sizeof ("default") - 1;
8e15ce
+
8e15ce
+  *id = 0;
8e15ce
+  key.object_id = data->sblock.root_dir_objectid;
8e15ce
+  key.type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
8e15ce
+  key.offset = grub_cpu_to_le64 (~grub_getcrc32c (1, ctoken, ctokenlen));
8e15ce
+  err = lower_bound (data, &key, &key_out, data->sblock.root_tree, &elemaddr, &elemsize,
8e15ce
+			 NULL, 0);
8e15ce
+  if (err)
8e15ce
+    return err;
8e15ce
+
8e15ce
+  if (key_cmp (&key, &key_out) != 0)
8e15ce
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("file not found"));
8e15ce
+
8e15ce
+  struct grub_btrfs_dir_item *cdirel;
8e15ce
+  direl = grub_malloc (elemsize + 1);
8e15ce
+  err = grub_btrfs_read_logical (data, elemaddr, direl, elemsize, 0);
8e15ce
+  if (err)
8e15ce
+    {
8e15ce
+      grub_free (direl);
8e15ce
+      return err;
8e15ce
+    }
8e15ce
+  for (cdirel = direl;
8e15ce
+       (grub_uint8_t *) cdirel - (grub_uint8_t *) direl
8e15ce
+       < (grub_ssize_t) elemsize;
8e15ce
+       cdirel = (void *) ((grub_uint8_t *) (direl + 1)
8e15ce
+       + grub_le_to_cpu16 (cdirel->n)
8e15ce
+       + grub_le_to_cpu16 (cdirel->m)))
8e15ce
+    {
8e15ce
+      if (ctokenlen == grub_le_to_cpu16 (cdirel->n)
8e15ce
+        && grub_memcmp (cdirel->name, ctoken, ctokenlen) == 0)
8e15ce
+      break;
8e15ce
+    }
8e15ce
+  if ((grub_uint8_t *) cdirel - (grub_uint8_t *) direl
8e15ce
+      >= (grub_ssize_t) elemsize)
8e15ce
+    {
8e15ce
+      grub_free (direl);
8e15ce
+      err = grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("file not found"));
8e15ce
+      return err;
8e15ce
+    }
8e15ce
+
8e15ce
+  if (cdirel->key.type != GRUB_BTRFS_ITEM_TYPE_ROOT_ITEM)
8e15ce
+    {
8e15ce
+      grub_free (direl);
8e15ce
+      err = grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("file not found"));
8e15ce
+      return err;
8e15ce
+    }
8e15ce
+
8e15ce
+  *id = grub_le_to_cpu64 (cdirel->key.object_id);
8e15ce
+  return GRUB_ERR_NONE;
8e15ce
+}
8e15ce
+
8e15ce
+static grub_err_t
8e15ce
+grub_cmd_btrfs_get_default_subvol (struct grub_extcmd_context *ctxt,
8e15ce
+			     int argc, char **argv)
8e15ce
+{
8e15ce
+  char *devname;
8e15ce
+  grub_device_t dev;
8e15ce
+  struct grub_btrfs_data *data;
8e15ce
+  grub_err_t err;
8e15ce
+  grub_uint64_t id;
8e15ce
+  char *subvol = NULL;
8e15ce
+  grub_uint64_t subvolid = 0;
8e15ce
+  char *varname = NULL;
8e15ce
+  char *output = NULL;
8e15ce
+  int path_only = ctxt->state[1].set;
8e15ce
+  int num_only = ctxt->state[2].set;
8e15ce
+
8e15ce
+  if (ctxt->state[0].set)
8e15ce
+    varname = ctxt->state[0].arg;
8e15ce
+
8e15ce
+  if (argc < 1)
8e15ce
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "device name required");
8e15ce
+
8e15ce
+  devname = grub_file_get_device_name(argv[0]);
8e15ce
+  if (!devname)
8e15ce
+    return grub_errno;
8e15ce
+
8e15ce
+  dev = grub_device_open (devname);
8e15ce
+  grub_free (devname);
8e15ce
+  if (!dev)
8e15ce
+    return grub_errno;
8e15ce
+
8e15ce
+  data = grub_btrfs_mount(dev);
8e15ce
+  if (!data)
8e15ce
+    {
8e15ce
+      grub_device_close (dev);
8e15ce
+      grub_dprintf ("btrfs", "failed to open fs\n");
8e15ce
+      grub_errno = GRUB_ERR_NONE;
8e15ce
+      return 0;
8e15ce
+    }
8e15ce
+
8e15ce
+  err = grub_btrfs_get_default_subvolume_id (data, &subvolid);
8e15ce
+  if (err)
8e15ce
+    {
8e15ce
+      grub_btrfs_unmount (data);
8e15ce
+      grub_device_close (dev);
8e15ce
+      return err;
8e15ce
+    }
8e15ce
+
8e15ce
+  id = subvolid;
8e15ce
+  while (id != GRUB_BTRFS_ROOT_VOL_OBJECTID)
8e15ce
+    {
8e15ce
+      grub_uint64_t parent_id;
8e15ce
+      char *path_out;
8e15ce
+
8e15ce
+      err = grub_btrfs_get_parent_subvol_path (data, grub_cpu_to_le64 (id), subvol, &parent_id, &path_out);
8e15ce
+      if (err)
8e15ce
+	{
8e15ce
+	  grub_btrfs_unmount (data);
8e15ce
+	  grub_device_close (dev);
8e15ce
+	  return err;
8e15ce
+	}
8e15ce
+
8e15ce
+      if (subvol)
8e15ce
+        grub_free (subvol);
8e15ce
+      subvol = path_out;
8e15ce
+      id = parent_id;
8e15ce
+    }
8e15ce
+
8e15ce
+  if (num_only && path_only)
8e15ce
+      output = grub_xasprintf ("%"PRIuGRUB_UINT64_T" /%s", subvolid, subvol);
8e15ce
+  else if (num_only)
8e15ce
+      output = grub_xasprintf ("%"PRIuGRUB_UINT64_T, subvolid);
8e15ce
+  else
8e15ce
+      output = grub_xasprintf ("/%s", subvol);
8e15ce
+
8e15ce
+  if (varname)
8e15ce
+    grub_env_set(varname, output);
8e15ce
+  else
8e15ce
+    grub_printf ("%s\n", output);
8e15ce
+
8e15ce
+  grub_free (output);
8e15ce
+  grub_free (subvol);
8e15ce
+
8e15ce
+  grub_btrfs_unmount (data);
8e15ce
+  grub_device_close (dev);
8e15ce
+
8e15ce
+  return GRUB_ERR_NONE;
8e15ce
+}
8e15ce
+
8e15ce
 static struct grub_fs grub_btrfs_fs = {
8e15ce
   .name = "btrfs",
8e15ce
   .fs_dir = grub_btrfs_dir,
8e15ce
@@ -2905,6 +3137,7 @@ static struct grub_fs grub_btrfs_fs = {
8e15ce
 static grub_command_t cmd_info;
8e15ce
 static grub_command_t cmd_mount_subvol;
8e15ce
 static grub_extcmd_t cmd_list_subvols;
8e15ce
+static grub_extcmd_t cmd_get_default_subvol;
8e15ce
 
8e15ce
 static char *
8e15ce
 subvolid_set_env (struct grub_env_var *var __attribute__ ((unused)),
8e15ce
@@ -2975,6 +3208,11 @@ GRUB_MOD_INIT (btrfs)
8e15ce
 					 "[-p|-n] [-o var] DEVICE",
8e15ce
 					 "Print list of BtrFS subvolumes on "
8e15ce
 					 "DEVICE.", options);
8e15ce
+  cmd_get_default_subvol = grub_register_extcmd("btrfs-get-default-subvol",
8e15ce
+					 grub_cmd_btrfs_get_default_subvol, 0,
8e15ce
+					 "[-p|-n] [-o var] DEVICE",
8e15ce
+					 "Print default BtrFS subvolume on "
8e15ce
+					 "DEVICE.", options);
8e15ce
   grub_register_variable_hook ("btrfs_subvol", subvol_get_env,
8e15ce
                                subvol_set_env);
8e15ce
   grub_register_variable_hook ("btrfs_subvolid", subvolid_get_env,