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