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

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