dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

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

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