15a207
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
15a207
From: Michael Chang <mchang@suse.com>
15a207
Date: Thu, 21 Aug 2014 03:39:11 +0000
15a207
Subject: [PATCH] grub2-btrfs-03-follow_default
15a207
15a207
---
15a207
 grub-core/fs/btrfs.c | 107 ++++++++++++++++++++++++++++++++++++---------------
15a207
 1 file changed, 76 insertions(+), 31 deletions(-)
15a207
15a207
diff --git a/grub-core/fs/btrfs.c b/grub-core/fs/btrfs.c
e15314
index 2d099b18ea1..2db89f71ea5 100644
15a207
--- a/grub-core/fs/btrfs.c
15a207
+++ b/grub-core/fs/btrfs.c
e15314
@@ -1236,6 +1236,7 @@ grub_btrfs_mount (grub_device_t dev)
15a207
 {
15a207
   struct grub_btrfs_data *data;
15a207
   grub_err_t err;
15a207
+  const char *relpath = grub_env_get ("btrfs_relative_path");
15a207
 
15a207
   if (!dev->disk)
15a207
     {
e15314
@@ -1266,11 +1267,14 @@ grub_btrfs_mount (grub_device_t dev)
15a207
   data->devices_attached[0].dev = dev;
15a207
   data->devices_attached[0].id = data->sblock.this_device.device_id;
15a207
 
15a207
-  err = btrfs_handle_subvol (data);
15a207
-  if (err)
15a207
+  if (relpath && (relpath[0] == '1' || relpath[0] == 'y'))
15a207
     {
15a207
-      grub_free (data);
15a207
-      return NULL;
15a207
+      err = btrfs_handle_subvol (data);
15a207
+      if (err)
15a207
+      {
15a207
+        grub_free (data);
15a207
+        return NULL;
15a207
+      }
15a207
     }
15a207
 
15a207
   return data;
e15314
@@ -1835,24 +1839,39 @@ find_path (struct grub_btrfs_data *data,
15a207
   grub_size_t allocated = 0;
15a207
   struct grub_btrfs_dir_item *direl = NULL;
15a207
   struct grub_btrfs_key key_out;
15a207
+  int follow_default;
15a207
   const char *ctoken;
15a207
   grub_size_t ctokenlen;
15a207
   char *path_alloc = NULL;
15a207
   char *origpath = NULL;
15a207
   unsigned symlinks_max = 32;
15a207
+  const char *relpath = grub_env_get ("btrfs_relative_path");
15a207
 
15a207
+  follow_default = 0;
15a207
   origpath = grub_strdup (path);
15a207
   if (!origpath)
15a207
     return grub_errno;
15a207
 
15a207
-  if (data->fs_tree)
15a207
+  if (relpath && (relpath[0] == '1' || relpath[0] == 'y'))
15a207
     {
15a207
-      *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
15a207
-      *tree = data->fs_tree;
15a207
-      /* This is a tree root, so everything starts at objectid 256 */
15a207
-      key->object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
15a207
-      key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
15a207
-      key->offset = 0;
15a207
+      if (data->fs_tree)
15a207
+        {
15a207
+          *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
15a207
+          *tree = data->fs_tree;
15a207
+          /* This is a tree root, so everything starts at objectid 256 */
15a207
+          key->object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
15a207
+          key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
15a207
+          key->offset = 0;
15a207
+        }
15a207
+      else
15a207
+        {
15a207
+          *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
15a207
+          *tree = data->sblock.root_tree;
15a207
+          key->object_id = data->sblock.root_dir_objectid;
15a207
+          key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
15a207
+          key->offset = 0;
15a207
+          follow_default = 1;
15a207
+        }
15a207
     }
15a207
   else
15a207
     {
e15314
@@ -1863,15 +1882,23 @@ find_path (struct grub_btrfs_data *data,
15a207
 
15a207
   while (1)
15a207
     {
15a207
-      while (path[0] == '/')
15a207
-	path++;
15a207
-      if (!path[0])
15a207
-	break;
15a207
-      slash = grub_strchr (path, '/');
15a207
-      if (!slash)
15a207
-	slash = path + grub_strlen (path);
15a207
-      ctoken = path;
15a207
-      ctokenlen = slash - path;
15a207
+      if (!follow_default)
15a207
+	{
15a207
+	  while (path[0] == '/')
15a207
+	    path++;
15a207
+	  if (!path[0])
15a207
+	    break;
15a207
+	  slash = grub_strchr (path, '/');
15a207
+	  if (!slash)
15a207
+	    slash = path + grub_strlen (path);
15a207
+	  ctoken = path;
15a207
+	  ctokenlen = slash - path;
15a207
+	}
15a207
+      else
15a207
+	{
15a207
+	  ctoken = "default";
15a207
+	  ctokenlen = sizeof ("default") - 1;
15a207
+	}
15a207
 
15a207
       if (*type != GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY)
15a207
 	{
e15314
@@ -1882,7 +1909,9 @@ find_path (struct grub_btrfs_data *data,
15a207
 
15a207
       if (ctokenlen == 1 && ctoken[0] == '.')
15a207
 	{
15a207
-	  path = slash;
15a207
+	  if (!follow_default)
15a207
+	    path = slash;
15a207
+	  follow_default = 0;
15a207
 	  continue;
15a207
 	}
15a207
       if (ctokenlen == 2 && ctoken[0] == '.' && ctoken[1] == '.')
e15314
@@ -1913,8 +1942,9 @@ find_path (struct grub_btrfs_data *data,
15a207
 	  *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
15a207
 	  key->object_id = key_out.offset;
15a207
 
15a207
-	  path = slash;
15a207
-
15a207
+	  if (!follow_default)
15a207
+	    path = slash;
15a207
+	  follow_default = 0;
15a207
 	  continue;
15a207
 	}
15a207
 
e15314
@@ -1983,7 +2013,9 @@ find_path (struct grub_btrfs_data *data,
15a207
 	  return err;
15a207
 	}
15a207
 
15a207
-      path = slash;
15a207
+      if (!follow_default)
15a207
+	path = slash;
15a207
+      follow_default = 0;
15a207
       if (cdirel->type == GRUB_BTRFS_DIR_ITEM_TYPE_SYMLINK)
15a207
 	{
15a207
 	  struct grub_btrfs_inode inode;
e15314
@@ -2033,14 +2065,26 @@ find_path (struct grub_btrfs_data *data,
15a207
 	  path = path_alloc = tmp;
15a207
 	  if (path[0] == '/')
15a207
 	    {
15a207
-	      if (data->fs_tree)
15a207
+              if (relpath && (relpath[0] == '1' || relpath[0] == 'y'))
15a207
 		{
15a207
-		  *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
15a207
-		  *tree = data->fs_tree;
15a207
-		  /* This is a tree root, so everything starts at objectid 256 */
15a207
-		  key->object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
15a207
-		  key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
15a207
-		  key->offset = 0;
15a207
+	          if (data->fs_tree)
15a207
+		    {
15a207
+		      *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
15a207
+		      *tree = data->fs_tree;
15a207
+		      /* This is a tree root, so everything starts at objectid 256 */
15a207
+		      key->object_id = grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK);
15a207
+		      key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
15a207
+		      key->offset = 0;
15a207
+		    }
15a207
+		  else
15a207
+		    {
15a207
+	              *type = GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY;
15a207
+	              *tree = data->sblock.root_tree;
15a207
+	              key->object_id = data->sblock.root_dir_objectid;
15a207
+	              key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
15a207
+	              key->offset = 0;
15a207
+	              follow_default = 1;
15a207
+		    }
15a207
 		}
15a207
 	      else
15a207
 		{
e15314
@@ -2696,6 +2740,7 @@ GRUB_MOD_INIT (btrfs)
15a207
                                subvolid_set_env);
15a207
   grub_env_export ("btrfs_subvol");
15a207
   grub_env_export ("btrfs_subvolid");
15a207
+  grub_env_export ("btrfs_relative_path");
15a207
 }
15a207
 
15a207
 GRUB_MOD_FINI (btrfs)