nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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