d9d99f
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
d9d99f
From: Michael Chang <mchang@suse.com>
d9d99f
Date: Thu, 21 Aug 2014 03:39:11 +0000
d9d99f
Subject: [PATCH] grub2-btrfs-03-follow_default
d9d99f
d9d99f
---
d9d99f
 grub-core/fs/btrfs.c | 107 ++++++++++++++++++++++++++++++++++++---------------
d9d99f
 1 file changed, 76 insertions(+), 31 deletions(-)
d9d99f
d9d99f
diff --git a/grub-core/fs/btrfs.c b/grub-core/fs/btrfs.c
b71686
index 88d727d16..a47d29756 100644
d9d99f
--- a/grub-core/fs/btrfs.c
d9d99f
+++ b/grub-core/fs/btrfs.c
d9d99f
@@ -920,6 +920,7 @@ grub_btrfs_mount (grub_device_t dev)
d9d99f
 {
d9d99f
   struct grub_btrfs_data *data;
d9d99f
   grub_err_t err;
d9d99f
+  const char *relpath = grub_env_get ("btrfs_relative_path");
d9d99f
 
d9d99f
   if (!dev->disk)
d9d99f
     {
d9d99f
@@ -950,11 +951,14 @@ grub_btrfs_mount (grub_device_t dev)
d9d99f
   data->devices_attached[0].dev = dev;
d9d99f
   data->devices_attached[0].id = data->sblock.this_device.device_id;
d9d99f
 
d9d99f
-  err = btrfs_handle_subvol (data);
d9d99f
-  if (err)
d9d99f
+  if (relpath && (relpath[0] == '1' || relpath[0] == 'y'))
d9d99f
     {
d9d99f
-      grub_free (data);
d9d99f
-      return NULL;
d9d99f
+      err = btrfs_handle_subvol (data);
d9d99f
+      if (err)
d9d99f
+      {
d9d99f
+        grub_free (data);
d9d99f
+        return NULL;
d9d99f
+      }
d9d99f
     }
d9d99f
 
d9d99f
   return data;
d9d99f
@@ -1414,24 +1418,39 @@ find_path (struct grub_btrfs_data *data,
d9d99f
   grub_size_t allocated = 0;
d9d99f
   struct grub_btrfs_dir_item *direl = NULL;
d9d99f
   struct grub_btrfs_key key_out;
d9d99f
+  int follow_default;
d9d99f
   const char *ctoken;
d9d99f
   grub_size_t ctokenlen;
d9d99f
   char *path_alloc = NULL;
d9d99f
   char *origpath = NULL;
d9d99f
   unsigned symlinks_max = 32;
d9d99f
+  const char *relpath = grub_env_get ("btrfs_relative_path");
d9d99f
 
d9d99f
+  follow_default = 0;
d9d99f
   origpath = grub_strdup (path);
d9d99f
   if (!origpath)
d9d99f
     return grub_errno;
d9d99f
 
d9d99f
-  if (data->fs_tree)
d9d99f
+  if (relpath && (relpath[0] == '1' || relpath[0] == 'y'))
d9d99f
     {
d9d99f
-      *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
d9d99f
-      *tree = data->fs_tree;
d9d99f
-      /* This is a tree root, so everything starts at objectid 256 */
d9d99f
-      key->object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
d9d99f
-      key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
d9d99f
-      key->offset = 0;
d9d99f
+      if (data->fs_tree)
d9d99f
+        {
d9d99f
+          *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
d9d99f
+          *tree = data->fs_tree;
d9d99f
+          /* This is a tree root, so everything starts at objectid 256 */
d9d99f
+          key->object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
d9d99f
+          key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
d9d99f
+          key->offset = 0;
d9d99f
+        }
d9d99f
+      else
d9d99f
+        {
d9d99f
+          *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
d9d99f
+          *tree = data->sblock.root_tree;
d9d99f
+          key->object_id = data->sblock.root_dir_objectid;
d9d99f
+          key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
d9d99f
+          key->offset = 0;
d9d99f
+          follow_default = 1;
d9d99f
+        }
d9d99f
     }
d9d99f
   else
d9d99f
     {
d9d99f
@@ -1442,15 +1461,23 @@ find_path (struct grub_btrfs_data *data,
d9d99f
 
d9d99f
   while (1)
d9d99f
     {
d9d99f
-      while (path[0] == '/')
d9d99f
-	path++;
d9d99f
-      if (!path[0])
d9d99f
-	break;
d9d99f
-      slash = grub_strchr (path, '/');
d9d99f
-      if (!slash)
d9d99f
-	slash = path + grub_strlen (path);
d9d99f
-      ctoken = path;
d9d99f
-      ctokenlen = slash - path;
d9d99f
+      if (!follow_default)
d9d99f
+	{
d9d99f
+	  while (path[0] == '/')
d9d99f
+	    path++;
d9d99f
+	  if (!path[0])
d9d99f
+	    break;
d9d99f
+	  slash = grub_strchr (path, '/');
d9d99f
+	  if (!slash)
d9d99f
+	    slash = path + grub_strlen (path);
d9d99f
+	  ctoken = path;
d9d99f
+	  ctokenlen = slash - path;
d9d99f
+	}
d9d99f
+      else
d9d99f
+	{
d9d99f
+	  ctoken = "default";
d9d99f
+	  ctokenlen = sizeof ("default") - 1;
d9d99f
+	}
d9d99f
 
d9d99f
       if (*type != GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY)
d9d99f
 	{
d9d99f
@@ -1461,7 +1488,9 @@ find_path (struct grub_btrfs_data *data,
d9d99f
 
d9d99f
       if (ctokenlen == 1 && ctoken[0] == '.')
d9d99f
 	{
d9d99f
-	  path = slash;
d9d99f
+	  if (!follow_default)
d9d99f
+	    path = slash;
d9d99f
+	  follow_default = 0;
d9d99f
 	  continue;
d9d99f
 	}
d9d99f
       if (ctokenlen == 2 && ctoken[0] == '.' && ctoken[1] == '.')
d9d99f
@@ -1492,8 +1521,9 @@ find_path (struct grub_btrfs_data *data,
d9d99f
 	  *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
d9d99f
 	  key->object_id = key_out.offset;
d9d99f
 
d9d99f
-	  path = slash;
d9d99f
-
d9d99f
+	  if (!follow_default)
d9d99f
+	    path = slash;
d9d99f
+	  follow_default = 0;
d9d99f
 	  continue;
d9d99f
 	}
d9d99f
 
d9d99f
@@ -1562,7 +1592,9 @@ find_path (struct grub_btrfs_data *data,
d9d99f
 	  return err;
d9d99f
 	}
d9d99f
 
d9d99f
-      path = slash;
d9d99f
+      if (!follow_default)
d9d99f
+	path = slash;
d9d99f
+      follow_default = 0;
d9d99f
       if (cdirel->type == GRUB_BTRFS_DIR_ITEM_TYPE_SYMLINK)
d9d99f
 	{
d9d99f
 	  struct grub_btrfs_inode inode;
d9d99f
@@ -1612,14 +1644,26 @@ find_path (struct grub_btrfs_data *data,
d9d99f
 	  path = path_alloc = tmp;
d9d99f
 	  if (path[0] == '/')
d9d99f
 	    {
d9d99f
-	      if (data->fs_tree)
d9d99f
+              if (relpath && (relpath[0] == '1' || relpath[0] == 'y'))
d9d99f
 		{
d9d99f
-		  *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
d9d99f
-		  *tree = data->fs_tree;
d9d99f
-		  /* This is a tree root, so everything starts at objectid 256 */
d9d99f
-		  key->object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
d9d99f
-		  key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
d9d99f
-		  key->offset = 0;
d9d99f
+	          if (data->fs_tree)
d9d99f
+		    {
d9d99f
+		      *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
d9d99f
+		      *tree = data->fs_tree;
d9d99f
+		      /* This is a tree root, so everything starts at objectid 256 */
d9d99f
+		      key->object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
d9d99f
+		      key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
d9d99f
+		      key->offset = 0;
d9d99f
+		    }
d9d99f
+		  else
d9d99f
+		    {
d9d99f
+	              *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
d9d99f
+	              *tree = data->sblock.root_tree;
d9d99f
+	              key->object_id = data->sblock.root_dir_objectid;
d9d99f
+	              key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
d9d99f
+	              key->offset = 0;
d9d99f
+	              follow_default = 1;
d9d99f
+		    }
d9d99f
 		}
d9d99f
 	      else
d9d99f
 		{
d9d99f
@@ -2275,6 +2319,7 @@ GRUB_MOD_INIT (btrfs)
d9d99f
                                subvolid_set_env);
d9d99f
   grub_env_export ("btrfs_subvol");
d9d99f
   grub_env_export ("btrfs_subvolid");
d9d99f
+  grub_env_export ("btrfs_relative_path");
d9d99f
 }
d9d99f
 
d9d99f
 GRUB_MOD_FINI (btrfs)