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

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