Blame SOURCES/0057-grub2-btrfs-Add-ability-to-boot-from-subvolumes.patch

5593c8
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
5593c8
From: Jeff Mahoney <jeffm@suse.com>
5593c8
Date: Tue, 9 Jul 2019 13:39:45 +0200
5593c8
Subject: [PATCH] grub2/btrfs: Add ability to boot from subvolumes
5593c8
5593c8
This patch adds the ability to specify a different root on a btrfs
5593c8
filesystem too boot from other than the default one.
5593c8
5593c8
btrfs-list-snapshots <dev> will list the subvolumes available on the
5593c8
filesystem.
5593c8
5593c8
set btrfs_subvol=<path> and set btrfs_subvolid=<subvolid> will specify
5593c8
which subvolume to use and any pathnames provided with either of those
5593c8
variables set will start using that root. If the subvolume or subvolume id
5593c8
doesn't exist, then an error case will result.
5593c8
5593c8
It is possible to boot into a separate GRUB instance by exporting the
5593c8
variable and loading the config file from the subvolume.
5593c8
5593c8
Signed-off-by: Jeff Mahoney <jeffm@suse.com>
5593c8
---
5593c8
 grub-core/fs/btrfs.c | 552 +++++++++++++++++++++++++++++++++++++++++++++++++--
5593c8
 include/grub/btrfs.h |   1 +
5593c8
 2 files changed, 533 insertions(+), 20 deletions(-)
5593c8
5593c8
diff --git a/grub-core/fs/btrfs.c b/grub-core/fs/btrfs.c
d3c3ab
index 63203034dfc..f1fff7385b5 100644
5593c8
--- a/grub-core/fs/btrfs.c
5593c8
+++ b/grub-core/fs/btrfs.c
5593c8
@@ -38,6 +38,9 @@
5593c8
 #include <zstd.h>
5593c8
 #include <grub/i18n.h>
5593c8
 #include <grub/btrfs.h>
5593c8
+#include <grub/command.h>
5593c8
+#include <grub/env.h>
5593c8
+#include <grub/extcmd.h>
5593c8
 #include <grub/crypto.h>
5593c8
 #include <grub/diskfilter.h>
5593c8
 #include <grub/safemath.h>
5593c8
@@ -79,9 +82,11 @@ struct grub_btrfs_superblock
5593c8
   grub_uint64_t generation;
5593c8
   grub_uint64_t root_tree;
5593c8
   grub_uint64_t chunk_tree;
5593c8
-  grub_uint8_t dummy2[0x20];
5593c8
+  grub_uint8_t dummy2[0x18];
5593c8
+  grub_uint64_t bytes_used;
5593c8
   grub_uint64_t root_dir_objectid;
5593c8
-  grub_uint8_t dummy3[0x41];
5593c8
+  grub_uint64_t num_devices;
5593c8
+  grub_uint8_t dummy3[0x39];
5593c8
   struct grub_btrfs_device this_device;
5593c8
   char label[0x100];
5593c8
   grub_uint8_t dummy4[0x100];
5593c8
@@ -121,6 +126,7 @@ struct grub_btrfs_data
5593c8
   grub_uint64_t exttree;
5593c8
   grub_size_t extsize;
5593c8
   struct grub_btrfs_extent_data *extent;
5593c8
+  grub_uint64_t fs_tree;
5593c8
 };
5593c8
 
5593c8
 struct grub_btrfs_chunk_item
5593c8
@@ -191,6 +197,14 @@ struct grub_btrfs_leaf_descriptor
5593c8
   } *data;
5593c8
 };
5593c8
 
5593c8
+struct grub_btrfs_root_ref
5593c8
+{
5593c8
+  grub_uint64_t dirid;
5593c8
+  grub_uint64_t sequence;
5593c8
+  grub_uint16_t name_len;
5593c8
+  const char name[0];
5593c8
+} __attribute__ ((packed));
5593c8
+
5593c8
 struct grub_btrfs_time
5593c8
 {
5593c8
   grub_int64_t sec;
5593c8
@@ -236,6 +250,14 @@ struct grub_btrfs_extent_data
5593c8
 
5593c8
 #define GRUB_BTRFS_OBJECT_ID_CHUNK 0x100
5593c8
 
5593c8
+#define GRUB_BTRFS_ROOT_TREE_OBJECTID 1ULL
5593c8
+#define GRUB_BTRFS_FS_TREE_OBJECTID 5ULL
5593c8
+#define GRUB_BTRFS_ROOT_REF_KEY     156
5593c8
+#define GRUB_BTRFS_ROOT_ITEM_KEY     132
5593c8
+
5593c8
+static grub_uint64_t btrfs_default_subvolid = 0;
5593c8
+static char *btrfs_default_subvol = NULL;
5593c8
+
5593c8
 static grub_disk_addr_t superblock_sectors[] = { 64 * 2, 64 * 1024 * 2,
5593c8
   256 * 1048576 * 2, 1048576ULL * 1048576ULL * 2
5593c8
 };
5593c8
@@ -1173,6 +1195,62 @@ grub_btrfs_read_logical (struct grub_btrfs_data *data, grub_disk_addr_t addr,
5593c8
   return GRUB_ERR_NONE;
5593c8
 }
5593c8
 
5593c8
+static grub_err_t
5593c8
+get_fs_root(struct grub_btrfs_data *data, grub_uint64_t tree,
5593c8
+            grub_uint64_t objectid, grub_uint64_t offset,
5593c8
+            grub_uint64_t *fs_root);
5593c8
+
5593c8
+static grub_err_t
5593c8
+lookup_root_by_id(struct grub_btrfs_data *data, grub_uint64_t id)
5593c8
+{
5593c8
+  grub_err_t err;
5593c8
+  grub_uint64_t tree;
5593c8
+
5593c8
+  err = get_fs_root(data, data->sblock.root_tree, id, -1, &tree);
5593c8
+  if (!err)
5593c8
+    data->fs_tree = tree;
5593c8
+  return err;
5593c8
+}
5593c8
+
5593c8
+static grub_err_t
5593c8
+find_path (struct grub_btrfs_data *data,
5593c8
+	   const char *path, struct grub_btrfs_key *key,
5593c8
+	   grub_uint64_t *tree, grub_uint8_t *type);
5593c8
+
5593c8
+static grub_err_t
5593c8
+lookup_root_by_name(struct grub_btrfs_data *data, const char *path)
5593c8
+{
5593c8
+  grub_err_t err;
5593c8
+  grub_uint64_t tree = 0;
5593c8
+  grub_uint8_t type;
5593c8
+  struct grub_btrfs_key key;
5593c8
+
5593c8
+  err = find_path (data, path, &key, &tree, &type);
5593c8
+  if (err)
5593c8
+      return grub_error(GRUB_ERR_FILE_NOT_FOUND, "couldn't locate %s\n", path);
5593c8
+
5593c8
+  if (key.object_id != grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK) || tree == 0)
5593c8
+    return grub_error(GRUB_ERR_BAD_FILE_TYPE, "%s: not a subvolume\n", path);
5593c8
+
5593c8
+  data->fs_tree = tree;
5593c8
+  return GRUB_ERR_NONE;
5593c8
+}
5593c8
+
5593c8
+static grub_err_t
5593c8
+btrfs_handle_subvol(struct grub_btrfs_data *data __attribute__ ((unused)))
5593c8
+{
5593c8
+  if (btrfs_default_subvol)
5593c8
+    return lookup_root_by_name(data, btrfs_default_subvol);
5593c8
+
5593c8
+  if (btrfs_default_subvolid)
5593c8
+    return lookup_root_by_id(data, btrfs_default_subvolid);
5593c8
+
5593c8
+  data->fs_tree = 0;
5593c8
+
5593c8
+  return GRUB_ERR_NONE;
5593c8
+}
5593c8
+
5593c8
+
5593c8
 static struct grub_btrfs_data *
5593c8
 grub_btrfs_mount (grub_device_t dev)
5593c8
 {
5593c8
@@ -1208,6 +1286,13 @@ grub_btrfs_mount (grub_device_t dev)
5593c8
   data->devices_attached[0].dev = dev;
5593c8
   data->devices_attached[0].id = data->sblock.this_device.device_id;
5593c8
 
5593c8
+  err = btrfs_handle_subvol (data);
5593c8
+  if (err)
5593c8
+    {
5593c8
+      grub_free (data);
5593c8
+      return NULL;
5593c8
+    }
5593c8
+
5593c8
   return data;
5593c8
 }
5593c8
 
5593c8
@@ -1673,6 +1758,91 @@ get_root (struct grub_btrfs_data *data, struct grub_btrfs_key *key,
5593c8
   return GRUB_ERR_NONE;
5593c8
 }
5593c8
 
5593c8
+static grub_err_t
5593c8
+find_pathname(struct grub_btrfs_data *data, grub_uint64_t objectid,
5593c8
+              grub_uint64_t fs_root, const char *name, char **pathname)
5593c8
+{
5593c8
+  grub_err_t err;
5593c8
+  struct grub_btrfs_key key = {
5593c8
+    .object_id = objectid,
5593c8
+    .type = GRUB_BTRFS_ITEM_TYPE_INODE_REF,
5593c8
+    .offset = 0,
5593c8
+  };
5593c8
+  struct grub_btrfs_key key_out;
5593c8
+  struct grub_btrfs_leaf_descriptor desc;
5593c8
+  char *p = grub_strdup (name);
5593c8
+  grub_disk_addr_t elemaddr;
5593c8
+  grub_size_t elemsize;
5593c8
+  grub_size_t alloc = grub_strlen(name) + 1;
5593c8
+
5593c8
+  err = lower_bound(data, &key, &key_out, fs_root,
5593c8
+                    &elemaddr, &elemsize, &desc, 0);
5593c8
+  if (err)
5593c8
+    return grub_error(err, "lower_bound caught %d\n", err);
5593c8
+
5593c8
+  if (key_out.type != GRUB_BTRFS_ITEM_TYPE_INODE_REF)
5593c8
+    next(data, &desc, &elemaddr, &elemsize, &key_out);
5593c8
+
5593c8
+  if (key_out.type != GRUB_BTRFS_ITEM_TYPE_INODE_REF)
5593c8
+    {
5593c8
+      return grub_error(GRUB_ERR_FILE_NOT_FOUND,
5593c8
+                        "Can't find inode ref for {%"PRIuGRUB_UINT64_T
5593c8
+                        ", %u, %"PRIuGRUB_UINT64_T"} %"PRIuGRUB_UINT64_T
5593c8
+                        "/%"PRIuGRUB_SIZE"\n",
5593c8
+                        key_out.object_id, key_out.type,
5593c8
+                        key_out.offset, elemaddr, elemsize);
5593c8
+    }
5593c8
+
5593c8
+
5593c8
+  while (key_out.type == GRUB_BTRFS_ITEM_TYPE_INODE_REF &&
5593c8
+         key_out.object_id != key_out.offset) {
5593c8
+    struct grub_btrfs_inode_ref *inode_ref;
5593c8
+    char *new;
5593c8
+
5593c8
+    inode_ref = grub_malloc(elemsize + 1);
5593c8
+    if (!inode_ref)
5593c8
+      return grub_error(GRUB_ERR_OUT_OF_MEMORY,
5593c8
+                        "couldn't allocate memory for inode_ref (%"PRIuGRUB_SIZE")\n", elemsize);
5593c8
+
5593c8
+    err = grub_btrfs_read_logical(data, elemaddr, inode_ref, elemsize, 0);
5593c8
+    if (err)
5593c8
+      return grub_error(err, "read_logical caught %d\n", err);
5593c8
+
5593c8
+    alloc += grub_le_to_cpu16 (inode_ref->n) + 2;
5593c8
+    new = grub_malloc(alloc);
5593c8
+    if (!new)
5593c8
+      return grub_error(GRUB_ERR_OUT_OF_MEMORY,
5593c8
+                        "couldn't allocate memory for name (%"PRIuGRUB_SIZE")\n", alloc);
5593c8
+
5593c8
+    grub_memcpy(new, inode_ref->name, grub_le_to_cpu16 (inode_ref->n));
5593c8
+    if (p)
5593c8
+      {
5593c8
+        new[grub_le_to_cpu16 (inode_ref->n)] = '/';
5593c8
+        grub_strcpy (new + grub_le_to_cpu16 (inode_ref->n) + 1, p);
5593c8
+        grub_free(p);
5593c8
+      }
5593c8
+    else
5593c8
+      new[grub_le_to_cpu16 (inode_ref->n)] = 0;
5593c8
+    grub_free(inode_ref);
5593c8
+
5593c8
+    p = new;
5593c8
+
5593c8
+    key.object_id = key_out.offset;
5593c8
+
5593c8
+    err = lower_bound(data, &key, &key_out, fs_root, &elemaddr,
5593c8
+                      &elemsize, &desc, 0);
5593c8
+    if (err)
5593c8
+      return grub_error(err, "lower_bound caught %d\n", err);
5593c8
+
5593c8
+    if (key_out.type != GRUB_BTRFS_ITEM_TYPE_INODE_REF)
5593c8
+      next(data, &desc, &elemaddr, &elemsize, &key_out);
5593c8
+
5593c8
+  }
5593c8
+
5593c8
+  *pathname = p;
5593c8
+  return 0;
5593c8
+}
5593c8
+
5593c8
 static grub_err_t
5593c8
 find_path (struct grub_btrfs_data *data,
5593c8
 	   const char *path, struct grub_btrfs_key *key,
5593c8
@@ -1691,14 +1861,26 @@ find_path (struct grub_btrfs_data *data,
5593c8
   char *origpath = NULL;
5593c8
   unsigned symlinks_max = 32;
5593c8
 
5593c8
-  err = get_root (data, key, tree, type);
5593c8
-  if (err)
5593c8
-    return err;
5593c8
-
5593c8
   origpath = grub_strdup (path);
5593c8
   if (!origpath)
5593c8
     return grub_errno;
5593c8
 
5593c8
+  if (data->fs_tree)
5593c8
+    {
5593c8
+      *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
5593c8
+      *tree = data->fs_tree;
5593c8
+      /* This is a tree root, so everything starts at objectid 256 */
5593c8
+      key->object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
5593c8
+      key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
5593c8
+      key->offset = 0;
5593c8
+    }
5593c8
+  else
5593c8
+    {
5593c8
+      err = get_root (data, key, tree, type);
5593c8
+      if (err)
5593c8
+	return err;
5593c8
+    }
5593c8
+
5593c8
   while (1)
5593c8
     {
5593c8
       while (path[0] == '/')
5593c8
@@ -1871,9 +2053,21 @@ find_path (struct grub_btrfs_data *data,
5593c8
 	  path = path_alloc = tmp;
5593c8
 	  if (path[0] == '/')
5593c8
 	    {
5593c8
-	      err = get_root (data, key, tree, type);
5593c8
-	      if (err)
5593c8
-		return err;
5593c8
+	      if (data->fs_tree)
5593c8
+		{
5593c8
+		  *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
5593c8
+		  *tree = data->fs_tree;
5593c8
+		  /* This is a tree root, so everything starts at objectid 256 */
5593c8
+		  key->object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
5593c8
+		  key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
5593c8
+		  key->offset = 0;
5593c8
+		}
5593c8
+	      else
5593c8
+		{
5593c8
+		  err = get_root (data, key, tree, type);
5593c8
+		  if (err)
5593c8
+		    return err;
5593c8
+		}
5593c8
 	    }
5593c8
 	  continue;
5593c8
 	}
5593c8
@@ -2114,18 +2308,10 @@ grub_btrfs_read (grub_file_t file, char *buf, grub_size_t len)
5593c8
 				 data->tree, file->offset, buf, len);
5593c8
 }
5593c8
 
5593c8
-static grub_err_t
5593c8
-grub_btrfs_uuid (grub_device_t device, char **uuid)
5593c8
+static char *
5593c8
+btrfs_unparse_uuid(struct grub_btrfs_data *data)
5593c8
 {
5593c8
-  struct grub_btrfs_data *data;
5593c8
-
5593c8
-  *uuid = NULL;
5593c8
-
5593c8
-  data = grub_btrfs_mount (device);
5593c8
-  if (!data)
5593c8
-    return grub_errno;
5593c8
-
5593c8
-  *uuid = grub_xasprintf ("%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
5593c8
+  return  grub_xasprintf ("%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
5593c8
 			  grub_be_to_cpu16 (data->sblock.uuid[0]),
5593c8
 			  grub_be_to_cpu16 (data->sblock.uuid[1]),
5593c8
 			  grub_be_to_cpu16 (data->sblock.uuid[2]),
5593c8
@@ -2134,6 +2320,20 @@ grub_btrfs_uuid (grub_device_t device, char **uuid)
5593c8
 			  grub_be_to_cpu16 (data->sblock.uuid[5]),
5593c8
 			  grub_be_to_cpu16 (data->sblock.uuid[6]),
5593c8
 			  grub_be_to_cpu16 (data->sblock.uuid[7]));
5593c8
+}
5593c8
+
5593c8
+static grub_err_t
5593c8
+grub_btrfs_uuid (grub_device_t device, char **uuid)
5593c8
+{
5593c8
+  struct grub_btrfs_data *data;
5593c8
+
5593c8
+  *uuid = NULL;
5593c8
+
5593c8
+  data = grub_btrfs_mount (device);
5593c8
+  if (!data)
5593c8
+    return grub_errno;
5593c8
+
5593c8
+  *uuid = btrfs_unparse_uuid(data);
5593c8
 
5593c8
   grub_btrfs_unmount (data);
5593c8
 
5593c8
@@ -2190,6 +2390,242 @@ grub_btrfs_embed (grub_device_t device __attribute__ ((unused)),
5593c8
 }
5593c8
 #endif
5593c8
 
5593c8
+static grub_err_t
5593c8
+grub_cmd_btrfs_info (grub_command_t cmd __attribute__ ((unused)), int argc,
5593c8
+		     char **argv)
5593c8
+{
5593c8
+  grub_device_t dev;
5593c8
+  char *devname;
5593c8
+  struct grub_btrfs_data *data;
5593c8
+  char *uuid;
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
+
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
+      return grub_error (GRUB_ERR_BAD_ARGUMENT, "failed to open fs");
5593c8
+    }
5593c8
+
5593c8
+  if (data->sblock.label)
5593c8
+    grub_printf("Label: '%s' ", data->sblock.label);
5593c8
+  else
5593c8
+    grub_printf("Label: none ");
5593c8
+
5593c8
+  uuid = btrfs_unparse_uuid(data);
5593c8
+
5593c8
+  grub_printf(" uuid: %s\n\tTotal devices %" PRIuGRUB_UINT64_T
5593c8
+              " FS bytes used %" PRIuGRUB_UINT64_T "\n",
5593c8
+	      uuid, grub_cpu_to_le64(data->sblock.num_devices),
5593c8
+	      grub_cpu_to_le64(data->sblock.bytes_used));
5593c8
+
5593c8
+  grub_btrfs_unmount (data);
5593c8
+
5593c8
+  return 0;
5593c8
+}
5593c8
+
5593c8
+static grub_err_t
5593c8
+get_fs_root(struct grub_btrfs_data *data, grub_uint64_t tree,
5593c8
+            grub_uint64_t objectid, grub_uint64_t offset,
5593c8
+            grub_uint64_t *fs_root)
5593c8
+{
5593c8
+  grub_err_t err;
5593c8
+  struct grub_btrfs_key key_in = {
5593c8
+    .object_id = objectid,
5593c8
+    .type = GRUB_BTRFS_ROOT_ITEM_KEY,
5593c8
+    .offset = offset,
5593c8
+  }, key_out;
5593c8
+  struct grub_btrfs_leaf_descriptor desc;
5593c8
+  grub_disk_addr_t elemaddr;
5593c8
+  grub_size_t elemsize;
5593c8
+  struct grub_btrfs_root_item ri;
5593c8
+
5593c8
+  err = lower_bound(data, &key_in, &key_out, tree,
5593c8
+                    &elemaddr, &elemsize, &desc, 0);
5593c8
+
5593c8
+  if (err)
5593c8
+    return err;
5593c8
+
5593c8
+  if (key_out.type != GRUB_BTRFS_ITEM_TYPE_ROOT_ITEM || elemaddr == 0)
5593c8
+    return grub_error(GRUB_ERR_FILE_NOT_FOUND,
5593c8
+                    N_("can't find fs root for subvol %"PRIuGRUB_UINT64_T"\n"),
5593c8
+                    key_in.object_id);
5593c8
+
5593c8
+  err = grub_btrfs_read_logical (data, elemaddr, &ri, sizeof (ri), 0);
5593c8
+  if (err)
5593c8
+    return err;
5593c8
+
5593c8
+  *fs_root = ri.tree;
5593c8
+
5593c8
+  return GRUB_ERR_NONE;
5593c8
+}
5593c8
+
5593c8
+static const struct grub_arg_option options[] = {
5593c8
+  {"output", 'o', 0, N_("Output to a variable instead of the console."),
5593c8
+   N_("VARNAME"), ARG_TYPE_STRING},
5593c8
+  {"path-only", 'p', 0, N_("Show only the path of the subvolume."), 0, 0},
5593c8
+  {"id-only", 'i', 0, N_("Show only the id of the subvolume."), 0, 0},
5593c8
+  {0, 0, 0, 0, 0, 0}
5593c8
+};
5593c8
+
5593c8
+static grub_err_t
5593c8
+grub_cmd_btrfs_list_subvols (struct grub_extcmd_context *ctxt,
5593c8
+			     int argc, char **argv)
5593c8
+{
5593c8
+  struct grub_btrfs_data *data;
5593c8
+  grub_device_t dev;
5593c8
+  char *devname;
5593c8
+  grub_uint64_t tree;
5593c8
+  struct grub_btrfs_key key_in = {
5593c8
+    .object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_FS_TREE_OBJECTID),
5593c8
+    .type = GRUB_BTRFS_ROOT_REF_KEY,
5593c8
+    .offset = 0,
5593c8
+  }, key_out;
5593c8
+  struct grub_btrfs_leaf_descriptor desc;
5593c8
+  grub_disk_addr_t elemaddr;
5593c8
+  grub_uint64_t fs_root = 0;
5593c8
+  grub_size_t elemsize;
5593c8
+  grub_size_t allocated = 0;
5593c8
+  int r = 0;
5593c8
+  grub_err_t err;
5593c8
+  char *buf = NULL;
5593c8
+  int print = 1;
5593c8
+  int path_only = ctxt->state[1].set;
5593c8
+  int num_only = ctxt->state[2].set;
5593c8
+  char *varname = NULL;
5593c8
+  char *output = NULL;
5593c8
+
5593c8
+  if (ctxt->state[0].set) {
5593c8
+    varname = ctxt->state[0].arg;
5593c8
+    print = 0;
5593c8
+  }
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
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "could not open device");
5593c8
+
5593c8
+  tree = data->sblock.root_tree;
5593c8
+  err = get_fs_root(data, tree, grub_cpu_to_le64_compile_time (GRUB_BTRFS_FS_TREE_OBJECTID),
5593c8
+                    0, &fs_root);
5593c8
+  if (err)
5593c8
+    goto out;
5593c8
+
5593c8
+  err = lower_bound(data, &key_in, &key_out, tree,
5593c8
+                    &elemaddr, &elemsize, &desc, 0);
5593c8
+
5593c8
+  if (err)
5593c8
+    {
5593c8
+      grub_btrfs_unmount(data);
5593c8
+      return err;
5593c8
+    }
5593c8
+
5593c8
+  if (key_out.type != GRUB_BTRFS_ITEM_TYPE_ROOT_REF || elemaddr == 0)
5593c8
+    {
5593c8
+      r = next(data, &desc, &elemaddr, &elemsize, &key_out);
5593c8
+    }
5593c8
+
5593c8
+  if (key_out.type != GRUB_BTRFS_ITEM_TYPE_ROOT_REF) {
5593c8
+    err = GRUB_ERR_FILE_NOT_FOUND;
5593c8
+    grub_error(GRUB_ERR_FILE_NOT_FOUND, N_("can't find root refs"));
5593c8
+    goto out;
5593c8
+  }
5593c8
+
5593c8
+  do
5593c8
+    {
5593c8
+      struct grub_btrfs_root_ref *ref;
5593c8
+      char *p = NULL;
5593c8
+
5593c8
+      if (key_out.type != GRUB_BTRFS_ITEM_TYPE_ROOT_REF)
5593c8
+        {
5593c8
+          r = 0;
5593c8
+          break;
5593c8
+        }
5593c8
+
5593c8
+      if (elemsize > allocated)
5593c8
+        {
5593c8
+          grub_free(buf);
5593c8
+          allocated = 2 * elemsize;
5593c8
+          buf = grub_malloc(allocated + 1);
5593c8
+          if (!buf)
5593c8
+            {
5593c8
+              r = -grub_errno;
5593c8
+              break;
5593c8
+            }
5593c8
+        }
5593c8
+      ref = (struct grub_btrfs_root_ref *)buf;
5593c8
+
5593c8
+      err = grub_btrfs_read_logical(data, elemaddr, buf, elemsize, 0);
5593c8
+      if (err)
5593c8
+        {
5593c8
+          r = -err;
5593c8
+          break;
5593c8
+        }
5593c8
+        buf[elemsize] = 0;
5593c8
+
5593c8
+      find_pathname(data, ref->dirid, fs_root, ref->name, &p);
5593c8
+
5593c8
+      if (print)
5593c8
+        {
5593c8
+          if (num_only)
5593c8
+            grub_printf("ID %"PRIuGRUB_UINT64_T"\n", key_out.offset);
5593c8
+          else if (path_only)
5593c8
+            grub_printf("%s\n", p);
5593c8
+          else
5593c8
+            grub_printf("ID %"PRIuGRUB_UINT64_T" path %s\n", key_out.offset, p);
5593c8
+        } else {
5593c8
+          char *old = output;
5593c8
+          if (num_only)
5593c8
+            output = grub_xasprintf("%s%"PRIuGRUB_UINT64_T"\n",
5593c8
+                                    old ?: "", key_out.offset);
5593c8
+          else if (path_only)
5593c8
+            output = grub_xasprintf("%s%s\n", old ?: "", p);
5593c8
+          else
5593c8
+            output = grub_xasprintf("%sID %"PRIuGRUB_UINT64_T" path %s\n",
5593c8
+                                    old ?: "", key_out.offset, p);
5593c8
+
5593c8
+          if (old)
5593c8
+            grub_free(old);
5593c8
+        }
5593c8
+
5593c8
+      r = next(data, &desc, &elemaddr, &elemsize, &key_out);
5593c8
+  } while(r > 0);
5593c8
+
5593c8
+  if (output)
5593c8
+    grub_env_set(varname, output);
5593c8
+
5593c8
+out:
5593c8
+  free_iterator(&desc);
5593c8
+  grub_btrfs_unmount(data);
5593c8
+
5593c8
+  grub_device_close (dev);
5593c8
+
5593c8
+  return 0;
5593c8
+}
5593c8
+
5593c8
 static struct grub_fs grub_btrfs_fs = {
5593c8
   .name = "btrfs",
5593c8
   .fs_dir = grub_btrfs_dir,
5593c8
@@ -2205,12 +2641,88 @@ static struct grub_fs grub_btrfs_fs = {
5593c8
 #endif
5593c8
 };
5593c8
 
5593c8
+static grub_command_t cmd_info;
5593c8
+static grub_extcmd_t cmd_list_subvols;
5593c8
+
5593c8
+static char *
5593c8
+subvolid_set_env (struct grub_env_var *var __attribute__ ((unused)),
5593c8
+                  const char *val)
5593c8
+{
5593c8
+  unsigned long long result = 0;
5593c8
+
5593c8
+  grub_errno = GRUB_ERR_NONE;
5593c8
+  if (*val)
5593c8
+    {
5593c8
+      result = grub_strtoull(val, NULL, 10);
5593c8
+      if (grub_errno)
5593c8
+        return NULL;
5593c8
+    }
5593c8
+
5593c8
+  grub_free (btrfs_default_subvol);
5593c8
+  btrfs_default_subvol = NULL;
5593c8
+  btrfs_default_subvolid = result;
5593c8
+  return grub_strdup(val);
5593c8
+}
5593c8
+
5593c8
+static const char *
5593c8
+subvolid_get_env (struct grub_env_var *var __attribute__ ((unused)),
5593c8
+                  const char *val __attribute__ ((unused)))
5593c8
+{
5593c8
+  if (btrfs_default_subvol)
5593c8
+    return grub_xasprintf("subvol:%s", btrfs_default_subvol);
5593c8
+  else if (btrfs_default_subvolid)
5593c8
+    return grub_xasprintf("%"PRIuGRUB_UINT64_T, btrfs_default_subvolid);
5593c8
+  else
5593c8
+    return "";
5593c8
+}
5593c8
+
5593c8
+static char *
5593c8
+subvol_set_env (struct grub_env_var *var __attribute__ ((unused)),
5593c8
+                const char *val)
5593c8
+{
5593c8
+  grub_free (btrfs_default_subvol);
5593c8
+  btrfs_default_subvol = grub_strdup (val);
5593c8
+  btrfs_default_subvolid = 0;
5593c8
+  return grub_strdup(val);
5593c8
+}
5593c8
+
5593c8
+static const char *
5593c8
+subvol_get_env (struct grub_env_var *var __attribute__ ((unused)),
5593c8
+                const char *val __attribute__ ((unused)))
5593c8
+{
5593c8
+  if (btrfs_default_subvol)
5593c8
+    return btrfs_default_subvol;
5593c8
+  else if (btrfs_default_subvolid)
5593c8
+    return grub_xasprintf("subvolid:%" PRIuGRUB_UINT64_T,
5593c8
+                          btrfs_default_subvolid);
5593c8
+  else
5593c8
+    return "";
5593c8
+}
5593c8
+
5593c8
 GRUB_MOD_INIT (btrfs)
5593c8
 {
5593c8
   grub_fs_register (&grub_btrfs_fs);
5593c8
+  cmd_info = grub_register_command("btrfs-info", grub_cmd_btrfs_info,
5593c8
+				   "DEVICE",
5593c8
+				   "Print BtrFS info about DEVICE.");
5593c8
+  cmd_list_subvols = grub_register_extcmd("btrfs-list-subvols",
5593c8
+					 grub_cmd_btrfs_list_subvols, 0,
5593c8
+					 "[-p|-n] [-o var] DEVICE",
5593c8
+					 "Print list of BtrFS subvolumes 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,
5593c8
+                               subvolid_set_env);
5593c8
 }
5593c8
 
5593c8
 GRUB_MOD_FINI (btrfs)
5593c8
 {
5593c8
+  grub_register_variable_hook ("btrfs_subvol", NULL, NULL);
5593c8
+  grub_register_variable_hook ("btrfs_subvolid", NULL, NULL);
5593c8
+  grub_unregister_command (cmd_info);
5593c8
+  grub_unregister_extcmd (cmd_list_subvols);
5593c8
   grub_fs_unregister (&grub_btrfs_fs);
5593c8
 }
5593c8
+
5593c8
+// vim: si et sw=2:
5593c8
diff --git a/include/grub/btrfs.h b/include/grub/btrfs.h
d3c3ab
index 9d93fb6c182..234ad976771 100644
5593c8
--- a/include/grub/btrfs.h
5593c8
+++ b/include/grub/btrfs.h
5593c8
@@ -29,6 +29,7 @@ enum
5593c8
     GRUB_BTRFS_ITEM_TYPE_ROOT_ITEM = 0x84,
5593c8
     GRUB_BTRFS_ITEM_TYPE_ROOT_BACKREF = 0x90,
5593c8
     GRUB_BTRFS_ITEM_TYPE_DEVICE = 0xd8,
5593c8
+    GRUB_BTRFS_ITEM_TYPE_ROOT_REF = 0x9c,
5593c8
     GRUB_BTRFS_ITEM_TYPE_CHUNK = 0xe4
5593c8
   };
5593c8