Blame SOURCES/0062-grub2-btrfs-06-subvol-mount.patch

8e15ce
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
8e15ce
From: Michael Chang <mchang@suse.com>
8e15ce
Date: Tue, 9 Jul 2019 13:56:16 +0200
8e15ce
Subject: [PATCH] grub2-btrfs-06-subvol-mount
8e15ce
8e15ce
---
8e15ce
 grub-core/fs/btrfs.c            | 195 +++++++++++++++++++++++++++++++++++++++-
8e15ce
 grub-core/osdep/linux/getroot.c | 148 +++++++++++++++++++++++++++++-
8e15ce
 util/grub-install.c             |  49 ++++++++++
8e15ce
 include/grub/emu/getroot.h      |   5 ++
8e15ce
 4 files changed, 392 insertions(+), 5 deletions(-)
8e15ce
8e15ce
diff --git a/grub-core/fs/btrfs.c b/grub-core/fs/btrfs.c
8e15ce
index 113c1f746c9..d323746ecfa 100644
8e15ce
--- a/grub-core/fs/btrfs.c
8e15ce
+++ b/grub-core/fs/btrfs.c
8e15ce
@@ -41,6 +41,7 @@
8e15ce
 #include <grub/command.h>
8e15ce
 #include <grub/env.h>
8e15ce
 #include <grub/extcmd.h>
8e15ce
+#include <grub/list.h>
8e15ce
 #include <grub/crypto.h>
8e15ce
 #include <grub/diskfilter.h>
8e15ce
 #include <grub/safemath.h>
8e15ce
@@ -266,6 +267,12 @@ static grub_err_t
8e15ce
 grub_btrfs_read_logical (struct grub_btrfs_data *data,
8e15ce
 			 grub_disk_addr_t addr, void *buf, grub_size_t size,
8e15ce
 			 int recursion_depth);
8e15ce
+static grub_err_t
8e15ce
+get_root (struct grub_btrfs_data *data, struct grub_btrfs_key *key,
8e15ce
+	  grub_uint64_t *tree, grub_uint8_t *type);
8e15ce
+
8e15ce
+grub_uint64_t
8e15ce
+find_mtab_subvol_tree (const char *path, char **path_in_subvol);
8e15ce
 
8e15ce
 static grub_err_t
8e15ce
 read_sblock (grub_disk_t disk, struct grub_btrfs_superblock *sb)
8e15ce
@@ -1223,9 +1230,26 @@ lookup_root_by_name(struct grub_btrfs_data *data, const char *path)
8e15ce
   grub_err_t err;
8e15ce
   grub_uint64_t tree = 0;
8e15ce
   grub_uint8_t type;
8e15ce
+  grub_uint64_t saved_tree;
8e15ce
   struct grub_btrfs_key key;
8e15ce
 
8e15ce
+  if (path[0] == '\0')
8e15ce
+    {
8e15ce
+      data->fs_tree = 0;
8e15ce
+      return GRUB_ERR_NONE;
8e15ce
+    }
8e15ce
+
8e15ce
+  err = get_root (data, &key, &tree, &type);
8e15ce
+  if (err)
8e15ce
+    return err;
8e15ce
+
8e15ce
+  saved_tree = data->fs_tree;
8e15ce
+  data->fs_tree = tree;
8e15ce
+
8e15ce
   err = find_path (data, path, &key, &tree, &type);
8e15ce
+
8e15ce
+  data->fs_tree = saved_tree;
8e15ce
+
8e15ce
   if (err)
8e15ce
       return grub_error(GRUB_ERR_FILE_NOT_FOUND, "couldn't locate %s\n", path);
8e15ce
 
8e15ce
@@ -2199,11 +2223,20 @@ grub_btrfs_dir (grub_device_t device, const char *path,
8e15ce
   int r = 0;
8e15ce
   grub_uint64_t tree;
8e15ce
   grub_uint8_t type;
8e15ce
+  char *new_path = NULL;
8e15ce
 
8e15ce
   if (!data)
8e15ce
     return grub_errno;
8e15ce
 
8e15ce
-  err = find_path (data, path, &key_in, &tree, &type);
8e15ce
+  tree = find_mtab_subvol_tree (path, &new_path);
8e15ce
+
8e15ce
+  if (tree)
8e15ce
+    data->fs_tree = tree;
8e15ce
+
8e15ce
+  err = find_path (data, new_path ? new_path : path, &key_in, &tree, &type);
8e15ce
+  if (new_path)
8e15ce
+    grub_free (new_path);
8e15ce
+
8e15ce
   if (err)
8e15ce
     {
8e15ce
       grub_btrfs_unmount (data);
8e15ce
@@ -2305,11 +2338,21 @@ grub_btrfs_open (struct grub_file *file, const char *name)
8e15ce
   struct grub_btrfs_inode inode;
8e15ce
   grub_uint8_t type;
8e15ce
   struct grub_btrfs_key key_in;
8e15ce
+  grub_uint64_t tree;
8e15ce
+  char *new_path = NULL;
8e15ce
 
8e15ce
   if (!data)
8e15ce
     return grub_errno;
8e15ce
 
8e15ce
-  err = find_path (data, name, &key_in, &data->tree, &type);
8e15ce
+  tree = find_mtab_subvol_tree (name, &new_path);
8e15ce
+
8e15ce
+  if (tree)
8e15ce
+    data->fs_tree = tree;
8e15ce
+
8e15ce
+  err = find_path (data, new_path ? new_path : name, &key_in, &data->tree, &type);
8e15ce
+  if (new_path)
8e15ce
+    grub_free (new_path);
8e15ce
+
8e15ce
   if (err)
8e15ce
     {
8e15ce
       grub_btrfs_unmount (data);
8e15ce
@@ -2480,6 +2523,150 @@ grub_cmd_btrfs_info (grub_command_t cmd __attribute__ ((unused)), int argc,
8e15ce
   return 0;
8e15ce
 }
8e15ce
 
8e15ce
+struct grub_btrfs_mtab
8e15ce
+{
8e15ce
+  struct grub_btrfs_mtab *next;
8e15ce
+  struct grub_btrfs_mtab **prev;
8e15ce
+  char *path;
8e15ce
+  char *subvol;
8e15ce
+  grub_uint64_t tree;
8e15ce
+};
8e15ce
+
8e15ce
+typedef struct grub_btrfs_mtab* grub_btrfs_mtab_t;
8e15ce
+
8e15ce
+static struct grub_btrfs_mtab *btrfs_mtab;
8e15ce
+
8e15ce
+#define FOR_GRUB_MTAB(var) FOR_LIST_ELEMENTS (var, btrfs_mtab)
8e15ce
+#define FOR_GRUB_MTAB_SAFE(var, next) FOR_LIST_ELEMENTS_SAFE((var), (next), btrfs_mtab)
8e15ce
+
8e15ce
+static void
8e15ce
+add_mountpoint (const char *path, const char *subvol, grub_uint64_t tree)
8e15ce
+{
8e15ce
+  grub_btrfs_mtab_t m = grub_malloc (sizeof (*m));
8e15ce
+
8e15ce
+  m->path = grub_strdup (path);
8e15ce
+  m->subvol = grub_strdup (subvol);
8e15ce
+  m->tree = tree;
8e15ce
+  grub_list_push (GRUB_AS_LIST_P (&btrfs_mtab), GRUB_AS_LIST (m));
8e15ce
+}
8e15ce
+
8e15ce
+static grub_err_t
8e15ce
+grub_cmd_btrfs_mount_subvol (grub_command_t cmd __attribute__ ((unused)), int argc,
8e15ce
+		     char **argv)
8e15ce
+{
8e15ce
+  char *devname, *dirname, *subvol;
8e15ce
+  struct grub_btrfs_key key_in;
8e15ce
+  grub_uint8_t type;
8e15ce
+  grub_uint64_t tree;
8e15ce
+  grub_uint64_t saved_tree;
8e15ce
+  grub_err_t err;
8e15ce
+  struct grub_btrfs_data *data = NULL;
8e15ce
+  grub_device_t dev = NULL;
8e15ce
+
8e15ce
+  if (argc < 3)
8e15ce
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "required <dev> <dir> and <subvol>");
8e15ce
+
8e15ce
+  devname = grub_file_get_device_name(argv[0]);
8e15ce
+  dev = grub_device_open (devname);
8e15ce
+  grub_free (devname);
8e15ce
+
8e15ce
+  if (!dev)
8e15ce
+    {
8e15ce
+      err = grub_errno;
8e15ce
+      goto err_out;
8e15ce
+    }
8e15ce
+
8e15ce
+  dirname = argv[1];
8e15ce
+  subvol = argv[2];
8e15ce
+
8e15ce
+  data = grub_btrfs_mount (dev);
8e15ce
+  if (!data)
8e15ce
+    {
8e15ce
+      err = grub_errno;
8e15ce
+      goto err_out;
8e15ce
+    }
8e15ce
+
8e15ce
+  err = find_path (data, dirname, &key_in, &tree, &type);
8e15ce
+  if (err)
8e15ce
+    goto err_out;
8e15ce
+
8e15ce
+  if (type !=  GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY)
8e15ce
+    {
8e15ce
+      err = grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a directory"));
8e15ce
+      goto err_out;
8e15ce
+    }
8e15ce
+
8e15ce
+  err = get_root (data, &key_in, &tree, &type);
8e15ce
+
8e15ce
+  if (err)
8e15ce
+    goto err_out;
8e15ce
+
8e15ce
+  saved_tree = data->fs_tree;
8e15ce
+  data->fs_tree = tree;
8e15ce
+  err = find_path (data, subvol, &key_in, &tree, &type);
8e15ce
+  data->fs_tree = saved_tree;
8e15ce
+
8e15ce
+  if (err)
8e15ce
+    goto err_out;
8e15ce
+
8e15ce
+  if (key_in.object_id != grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK) || tree == 0)
8e15ce
+    {
8e15ce
+      err = grub_error (GRUB_ERR_BAD_FILE_TYPE, "%s: not a subvolume\n", subvol);
8e15ce
+      goto err_out;
8e15ce
+    }
8e15ce
+
8e15ce
+  grub_btrfs_unmount (data);
8e15ce
+  grub_device_close (dev);
8e15ce
+  add_mountpoint (dirname, subvol, tree);
8e15ce
+
8e15ce
+  return GRUB_ERR_NONE;
8e15ce
+
8e15ce
+err_out:
8e15ce
+
8e15ce
+  if (data)
8e15ce
+    grub_btrfs_unmount (data);
8e15ce
+
8e15ce
+  if (dev)
8e15ce
+    grub_device_close (dev);
8e15ce
+
8e15ce
+  return err;
8e15ce
+}
8e15ce
+
8e15ce
+grub_uint64_t
8e15ce
+find_mtab_subvol_tree (const char *path, char **path_in_subvol)
8e15ce
+{
8e15ce
+  grub_btrfs_mtab_t m, cm;
8e15ce
+  grub_uint64_t tree;
8e15ce
+
8e15ce
+  if (!path || !path_in_subvol)
8e15ce
+    return 0;
8e15ce
+
8e15ce
+  *path_in_subvol = NULL;
8e15ce
+  tree = 0;
8e15ce
+  cm = NULL;
8e15ce
+
8e15ce
+  FOR_GRUB_MTAB (m)
8e15ce
+    {
8e15ce
+      if (grub_strncmp (path, m->path, grub_strlen (m->path)) == 0)
8e15ce
+	{
8e15ce
+	  if (!cm)
8e15ce
+	    cm = m;
8e15ce
+	  else
8e15ce
+	    if (grub_strcmp (m->path, cm->path) > 0)
8e15ce
+	      cm = m;
8e15ce
+	}
8e15ce
+    }
8e15ce
+
8e15ce
+  if (cm)
8e15ce
+    {
8e15ce
+      const char *s = path + grub_strlen (cm->path);
8e15ce
+      *path_in_subvol = (s[0] == '\0') ? grub_strdup ("/") : grub_strdup (s);
8e15ce
+      tree = cm->tree;
8e15ce
+    }
8e15ce
+
8e15ce
+  return tree;
8e15ce
+}
8e15ce
+
8e15ce
 static grub_err_t
8e15ce
 get_fs_root(struct grub_btrfs_data *data, grub_uint64_t tree,
8e15ce
             grub_uint64_t objectid, grub_uint64_t offset,
8e15ce
@@ -2686,6 +2873,7 @@ static struct grub_fs grub_btrfs_fs = {
8e15ce
 };
8e15ce
 
8e15ce
 static grub_command_t cmd_info;
8e15ce
+static grub_command_t cmd_mount_subvol;
8e15ce
 static grub_extcmd_t cmd_list_subvols;
8e15ce
 
8e15ce
 static char *
8e15ce
@@ -2749,6 +2937,9 @@ GRUB_MOD_INIT (btrfs)
8e15ce
   cmd_info = grub_register_command("btrfs-info", grub_cmd_btrfs_info,
8e15ce
 				   "DEVICE",
8e15ce
 				   "Print BtrFS info about DEVICE.");
8e15ce
+  cmd_mount_subvol = grub_register_command("btrfs-mount-subvol", grub_cmd_btrfs_mount_subvol,
8e15ce
+				   "DEVICE DIRECTORY SUBVOL",
8e15ce
+				   "Set btrfs DEVICE the DIRECTORY a mountpoint of SUBVOL.");
8e15ce
   cmd_list_subvols = grub_register_extcmd("btrfs-list-subvols",
8e15ce
 					 grub_cmd_btrfs_list_subvols, 0,
8e15ce
 					 "[-p|-n] [-o var] DEVICE",
8e15ce
diff --git a/grub-core/osdep/linux/getroot.c b/grub-core/osdep/linux/getroot.c
8e15ce
index caf9b1ccd3f..28790307e00 100644
8e15ce
--- a/grub-core/osdep/linux/getroot.c
8e15ce
+++ b/grub-core/osdep/linux/getroot.c
8e15ce
@@ -107,6 +107,14 @@ struct btrfs_ioctl_search_key
8e15ce
   grub_uint32_t unused[9];
8e15ce
 };
8e15ce
 
8e15ce
+struct btrfs_ioctl_search_header {
8e15ce
+  grub_uint64_t transid;
8e15ce
+  grub_uint64_t objectid;
8e15ce
+  grub_uint64_t offset;
8e15ce
+  grub_uint32_t type;
8e15ce
+  grub_uint32_t len;
8e15ce
+};
8e15ce
+
8e15ce
 struct btrfs_ioctl_search_args {
8e15ce
   struct btrfs_ioctl_search_key key;
8e15ce
   grub_uint64_t buf[(4096 - sizeof(struct btrfs_ioctl_search_key))
8e15ce
@@ -378,6 +386,109 @@ get_btrfs_fs_prefix (const char *mount_path)
8e15ce
 
8e15ce
 int use_relative_path_on_btrfs = 0;
8e15ce
 
8e15ce
+static char *
8e15ce
+get_btrfs_subvol (const char *path)
8e15ce
+{
8e15ce
+  struct btrfs_ioctl_ino_lookup_args args;
8e15ce
+  grub_uint64_t tree_id;
8e15ce
+  int fd = -1;
8e15ce
+  char *ret = NULL;
8e15ce
+
8e15ce
+  fd = open (path, O_RDONLY);
8e15ce
+
8e15ce
+  if (fd < 0)
8e15ce
+    return NULL;
8e15ce
+
8e15ce
+  memset (&args, 0, sizeof(args));
8e15ce
+  args.objectid = GRUB_BTRFS_TREE_ROOT_OBJECTID;
8e15ce
+
8e15ce
+  if (ioctl (fd, BTRFS_IOC_INO_LOOKUP, &args) < 0)
8e15ce
+    goto error;
8e15ce
+
8e15ce
+  tree_id = args.treeid;
8e15ce
+
8e15ce
+  while (tree_id != GRUB_BTRFS_ROOT_VOL_OBJECTID)
8e15ce
+    {
8e15ce
+      struct btrfs_ioctl_search_args sargs;
8e15ce
+      struct grub_btrfs_root_backref *br;
8e15ce
+      struct btrfs_ioctl_search_header *search_header;
8e15ce
+      char *old;
8e15ce
+      grub_uint16_t len;
8e15ce
+      grub_uint64_t inode_id;
8e15ce
+
8e15ce
+      memset (&sargs, 0, sizeof(sargs));
8e15ce
+
8e15ce
+      sargs.key.tree_id = 1;
8e15ce
+      sargs.key.min_objectid = tree_id;
8e15ce
+      sargs.key.max_objectid = tree_id;
8e15ce
+
8e15ce
+      sargs.key.min_offset = 0;
8e15ce
+      sargs.key.max_offset = ~0ULL;
8e15ce
+      sargs.key.min_transid = 0;
8e15ce
+      sargs.key.max_transid = ~0ULL;
8e15ce
+      sargs.key.min_type = GRUB_BTRFS_ITEM_TYPE_ROOT_BACKREF;
8e15ce
+      sargs.key.max_type = GRUB_BTRFS_ITEM_TYPE_ROOT_BACKREF;
8e15ce
+
8e15ce
+      sargs.key.nr_items = 1;
8e15ce
+
8e15ce
+      if (ioctl (fd, BTRFS_IOC_TREE_SEARCH, &sargs) < 0)
8e15ce
+	goto error;
8e15ce
+
8e15ce
+      if (sargs.key.nr_items == 0)
8e15ce
+	goto error;
8e15ce
+
8e15ce
+      search_header = (struct btrfs_ioctl_search_header *)sargs.buf;
8e15ce
+      br = (struct grub_btrfs_root_backref *) (search_header + 1);
8e15ce
+
8e15ce
+      len = grub_le_to_cpu16 (br->n);
8e15ce
+      inode_id = grub_le_to_cpu64 (br->inode_id);
8e15ce
+      tree_id = search_header->offset;
8e15ce
+
8e15ce
+      old = ret;
8e15ce
+      ret = malloc (len + 1);
8e15ce
+      memcpy (ret, br->name, len);
8e15ce
+      ret[len] = '\0';
8e15ce
+
8e15ce
+      if (inode_id != GRUB_BTRFS_TREE_ROOT_OBJECTID)
8e15ce
+	{
8e15ce
+	  char *s;
8e15ce
+
8e15ce
+	  memset(&args, 0, sizeof(args));
8e15ce
+	  args.treeid = search_header->offset;
8e15ce
+	  args.objectid = inode_id;
8e15ce
+
8e15ce
+	  if (ioctl (fd, BTRFS_IOC_INO_LOOKUP, &args) < 0)
8e15ce
+	    goto error;
8e15ce
+
8e15ce
+	  s = xasprintf ("%s%s", args.name, ret);
8e15ce
+	  free (ret);
8e15ce
+	  ret = s;
8e15ce
+	}
8e15ce
+
8e15ce
+      if (old)
8e15ce
+	{
8e15ce
+	  char *s = xasprintf ("%s/%s", ret, old);
8e15ce
+	  free (ret);
8e15ce
+	  free (old);
8e15ce
+	  ret = s;
8e15ce
+	}
8e15ce
+    }
8e15ce
+
8e15ce
+  close (fd);
8e15ce
+  return ret;
8e15ce
+
8e15ce
+error:
8e15ce
+
8e15ce
+  if (fd >= 0)
8e15ce
+    close (fd);
8e15ce
+  if (ret)
8e15ce
+    free (ret);
8e15ce
+
8e15ce
+  return NULL;
8e15ce
+}
8e15ce
+
8e15ce
+void (*grub_find_root_btrfs_mount_path_hook)(const char *mount_path);
8e15ce
+
8e15ce
 char **
8e15ce
 grub_find_root_devices_from_mountinfo (const char *dir, char **relroot)
8e15ce
 {
8e15ce
@@ -519,12 +630,15 @@ again:
8e15ce
       else if (grub_strcmp (entries[i].fstype, "btrfs") == 0)
8e15ce
 	{
8e15ce
 	  ret = grub_find_root_devices_from_btrfs (dir);
8e15ce
-	  fs_prefix = get_btrfs_fs_prefix (entries[i].enc_path);
8e15ce
 	  if (use_relative_path_on_btrfs)
8e15ce
 	    {
8e15ce
-	      if (fs_prefix)
8e15ce
-	        free (fs_prefix);
8e15ce
 	      fs_prefix = xstrdup ("/");
8e15ce
+	      if (grub_find_root_btrfs_mount_path_hook)
8e15ce
+		grub_find_root_btrfs_mount_path_hook (entries[i].enc_path);
8e15ce
+	    }
8e15ce
+	  else
8e15ce
+	    {
8e15ce
+	      fs_prefix = get_btrfs_fs_prefix (entries[i].enc_path);
8e15ce
 	    }
8e15ce
 	}
8e15ce
       else if (!retry && grub_strcmp (entries[i].fstype, "autofs") == 0)
8e15ce
@@ -1150,6 +1264,34 @@ grub_util_get_grub_dev_os (const char *os_dev)
8e15ce
   return grub_dev;
8e15ce
 }
8e15ce
 
8e15ce
+
8e15ce
+char *
8e15ce
+grub_util_get_btrfs_subvol (const char *path, char **mount_path)
8e15ce
+{
8e15ce
+  char *mp = NULL;
8e15ce
+
8e15ce
+  if (mount_path)
8e15ce
+    *mount_path = NULL;
8e15ce
+
8e15ce
+  auto void
8e15ce
+  mount_path_hook (const char *m)
8e15ce
+  {
8e15ce
+    mp = strdup (m);
8e15ce
+  }
8e15ce
+
8e15ce
+  grub_find_root_btrfs_mount_path_hook = mount_path_hook;
8e15ce
+  grub_free (grub_find_root_devices_from_mountinfo (path, NULL));
8e15ce
+  grub_find_root_btrfs_mount_path_hook = NULL;
8e15ce
+
8e15ce
+  if (!mp)
8e15ce
+    return NULL;
8e15ce
+
8e15ce
+  if (mount_path)
8e15ce
+    *mount_path = mp;
8e15ce
+
8e15ce
+  return get_btrfs_subvol (mp);
8e15ce
+}
8e15ce
+
8e15ce
 char *
8e15ce
 grub_make_system_path_relative_to_its_root_os (const char *path)
8e15ce
 {
8e15ce
diff --git a/util/grub-install.c b/util/grub-install.c
8e15ce
index 0f66f36d23a..84ed6e88ecb 100644
8e15ce
--- a/util/grub-install.c
8e15ce
+++ b/util/grub-install.c
8e15ce
@@ -1569,6 +1569,55 @@ main (int argc, char *argv[])
8e15ce
       prefix_drive = xasprintf ("(%s)", grub_drives[0]);
8e15ce
     }
8e15ce
 
8e15ce
+#ifdef __linux__
8e15ce
+
8e15ce
+  if (config.is_suse_btrfs_snapshot_enabled
8e15ce
+      && grub_strncmp(grub_fs->name, "btrfs", sizeof ("btrfs") - 1) == 0)
8e15ce
+    {
8e15ce
+      char *subvol = NULL;
8e15ce
+      char *mount_path = NULL;
8e15ce
+      char **rootdir_devices = NULL;
8e15ce
+      char *rootdir_path = grub_util_path_concat (2, "/", rootdir);
8e15ce
+
8e15ce
+      if (grub_util_is_directory (rootdir_path))
8e15ce
+	rootdir_devices = grub_guess_root_devices (rootdir_path);
8e15ce
+
8e15ce
+      free (rootdir_path);
8e15ce
+
8e15ce
+      if (rootdir_devices && rootdir_devices[0])
8e15ce
+	if (grub_strcmp (rootdir_devices[0], grub_devices[0]) == 0)
8e15ce
+	  subvol = grub_util_get_btrfs_subvol (platdir, &mount_path);
8e15ce
+
8e15ce
+      if (subvol && mount_path)
8e15ce
+	{
8e15ce
+	  char *def_subvol;
8e15ce
+
8e15ce
+	  def_subvol = grub_util_get_btrfs_subvol ("/", NULL);
8e15ce
+
8e15ce
+	  if (def_subvol)
8e15ce
+	    {
8e15ce
+	      if (!load_cfg_f)
8e15ce
+		load_cfg_f = grub_util_fopen (load_cfg, "wb");
8e15ce
+	      have_load_cfg = 1;
8e15ce
+
8e15ce
+	      if (grub_strcmp (subvol, def_subvol) != 0)
8e15ce
+		fprintf (load_cfg_f, "btrfs-mount-subvol ($root) %s %s\n", mount_path, subvol);
8e15ce
+	      free (def_subvol);
8e15ce
+	    }
8e15ce
+	}
8e15ce
+
8e15ce
+      for (curdev = rootdir_devices; *curdev; curdev++)
8e15ce
+	free (*curdev);
8e15ce
+      if (rootdir_devices)
8e15ce
+	free (rootdir_devices);
8e15ce
+      if (subvol)
8e15ce
+	free (subvol);
8e15ce
+      if (mount_path)
8e15ce
+	free (mount_path);
8e15ce
+    }
8e15ce
+
8e15ce
+#endif
8e15ce
+
8e15ce
   char mkimage_target[200];
8e15ce
   const char *core_name = NULL;
8e15ce
 
8e15ce
diff --git a/include/grub/emu/getroot.h b/include/grub/emu/getroot.h
8e15ce
index 73fa2d34abb..9c642ae3fe3 100644
8e15ce
--- a/include/grub/emu/getroot.h
8e15ce
+++ b/include/grub/emu/getroot.h
8e15ce
@@ -53,6 +53,11 @@ char **
8e15ce
 grub_find_root_devices_from_mountinfo (const char *dir, char **relroot);
8e15ce
 #endif
8e15ce
 
8e15ce
+#ifdef __linux__
8e15ce
+char *
8e15ce
+grub_util_get_btrfs_subvol (const char *path, char **mount_path);
8e15ce
+#endif
8e15ce
+
8e15ce
 /* Devmapper functions provided by getroot_devmapper.c.  */
8e15ce
 void
8e15ce
 grub_util_pull_devmapper (const char *os_dev);