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