e602a06
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
b256068
From: Michael Chang <mchang@suse.com>
b256068
Date: Mon, 13 Dec 2021 14:25:49 +0800
b256068
Subject: [PATCH] fs/btrfs: Use full btrfs bootloader area
b256068
b256068
Up to now GRUB can only embed to the first 64 KiB before primary
b256068
superblock of btrfs, effectively limiting the GRUB core size. That
b256068
could consequently pose restrictions to feature enablement like
b256068
advanced zstd compression.
b256068
b256068
This patch attempts to utilize full unused area reserved by btrfs for
b256068
the bootloader outlined in the document [1]:
b256068
b256068
  The first 1MiB on each device is unused with the exception of primary
b256068
  superblock that is on the offset 64KiB and spans 4KiB.
b256068
b256068
Apart from that, adjacent sectors to superblock and first block group
b256068
are not used for embedding in case of overflow and logged access to
b256068
adjacent sectors could be useful for tracing it up.
b256068
b256068
This patch has been tested to provide out of the box support for btrfs
b256068
zstd compression with which GRUB has been installed to the partition.
b256068
b256068
[1] https://btrfs.wiki.kernel.org/index.php/Manpage/btrfs(5)#BOOTLOADER_SUPPORT
b256068
b256068
Signed-off-by: Michael Chang <mchang@suse.com>
b256068
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
b256068
(cherry picked from commit b0f06a81c6f31b6fa20be67a96b6683bba8210c9)
b256068
---
e602a06
 grub-core/fs/btrfs.c | 90 ++++++++++++++++++++++++++++++++++++++++++++--------
e602a06
 include/grub/disk.h  |  2 ++
b256068
 2 files changed, 79 insertions(+), 13 deletions(-)
b256068
b256068
diff --git a/grub-core/fs/btrfs.c b/grub-core/fs/btrfs.c
e622855
index 4cc86e9b79..07c0ff874b 100644
b256068
--- a/grub-core/fs/btrfs.c
b256068
+++ b/grub-core/fs/btrfs.c
b256068
@@ -2476,6 +2476,33 @@ grub_btrfs_label (grub_device_t device, char **label)
b256068
 }
b256068
 
b256068
 #ifdef GRUB_UTIL
b256068
+
b256068
+struct embed_region {
b256068
+  unsigned int start;
b256068
+  unsigned int secs;
b256068
+};
b256068
+
b256068
+/*
b256068
+ * https://btrfs.wiki.kernel.org/index.php/Manpage/btrfs(5)#BOOTLOADER_SUPPORT
b256068
+ * The first 1 MiB on each device is unused with the exception of primary
b256068
+ * superblock that is on the offset 64 KiB and spans 4 KiB.
b256068
+ */
b256068
+
b256068
+static const struct {
b256068
+  struct embed_region available;
b256068
+  struct embed_region used[6];
b256068
+} btrfs_head = {
b256068
+  .available = {0, GRUB_DISK_KiB_TO_SECTORS (1024)}, /* The first 1 MiB. */
b256068
+  .used = {
b256068
+    {0, 1},                                                        /* boot.S. */
b256068
+    {GRUB_DISK_KiB_TO_SECTORS (64) - 1, 1},                        /* Overflow guard. */
b256068
+    {GRUB_DISK_KiB_TO_SECTORS (64), GRUB_DISK_KiB_TO_SECTORS (4)}, /* 4 KiB superblock. */
b256068
+    {GRUB_DISK_KiB_TO_SECTORS (68), 1},                            /* Overflow guard. */
b256068
+    {GRUB_DISK_KiB_TO_SECTORS (1024) - 1, 1},                      /* Overflow guard. */
b256068
+    {0, 0}                                                         /* Array terminator. */
b256068
+  }
b256068
+};
b256068
+
b256068
 static grub_err_t
b256068
 grub_btrfs_embed (grub_device_t device __attribute__ ((unused)),
b256068
 		  unsigned int *nsectors,
b256068
@@ -2483,25 +2510,62 @@ grub_btrfs_embed (grub_device_t device __attribute__ ((unused)),
b256068
 		  grub_embed_type_t embed_type,
b256068
 		  grub_disk_addr_t **sectors)
b256068
 {
b256068
-  unsigned i;
b256068
+  unsigned int i, j, n = 0;
b256068
+  const struct embed_region *u;
b256068
+  grub_disk_addr_t *map;
b256068
 
b256068
   if (embed_type != GRUB_EMBED_PCBIOS)
b256068
     return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
b256068
 		       "BtrFS currently supports only PC-BIOS embedding");
b256068
 
b256068
-  if (64 * 2 - 1 < *nsectors)
b256068
-    return grub_error (GRUB_ERR_OUT_OF_RANGE,
b256068
-		       N_("your core.img is unusually large.  "
b256068
-			  "It won't fit in the embedding area"));
b256068
-
b256068
-  *nsectors = 64 * 2 - 1;
b256068
-  if (*nsectors > max_nsectors)
b256068
-    *nsectors = max_nsectors;
b256068
-  *sectors = grub_calloc (*nsectors, sizeof (**sectors));
b256068
-  if (!*sectors)
b256068
+  map = grub_calloc (btrfs_head.available.secs, sizeof (*map));
b256068
+  if (map == NULL)
b256068
     return grub_errno;
b256068
-  for (i = 0; i < *nsectors; i++)
b256068
-    (*sectors)[i] = i + 1;
b256068
+
b256068
+  /*
b256068
+   * Populating the map array so that it can be used to index if a disk
b256068
+   * address is available to embed:
b256068
+   *   - 0: available,
b256068
+   *   - 1: unavailable.
b256068
+   */
b256068
+  for (u = btrfs_head.used; u->secs; ++u)
b256068
+    {
b256068
+      unsigned int end = u->start + u->secs;
b256068
+
b256068
+      if (end > btrfs_head.available.secs)
b256068
+        end = btrfs_head.available.secs;
b256068
+      for (i = u->start; i < end; ++i)
b256068
+        map[i] = 1;
b256068
+    }
b256068
+
b256068
+  /* Adding up n until it matches total size of available embedding area. */
b256068
+  for (i = 0; i < btrfs_head.available.secs; ++i)
b256068
+    if (map[i] == 0)
b256068
+      n++;
b256068
+
b256068
+  if (n < *nsectors)
b256068
+    {
b256068
+      grub_free (map);
b256068
+      return grub_error (GRUB_ERR_OUT_OF_RANGE,
b256068
+		         N_("your core.img is unusually large.  "
b256068
+			    "It won't fit in the embedding area"));
b256068
+    }
b256068
+
b256068
+  if (n > max_nsectors)
b256068
+    n = max_nsectors;
b256068
+
b256068
+  /*
b256068
+   * Populating the array so that it can used to index disk block address for
b256068
+   * an image file's offset to be embedded on disk (the unit is in sectors):
b256068
+   *   - i: The disk block address relative to btrfs_head.available.start,
b256068
+   *   - j: The offset in image file.
b256068
+   */
b256068
+  for (i = 0, j = 0; i < btrfs_head.available.secs && j < n; ++i)
b256068
+    if (map[i] == 0)
b256068
+      map[j++] = btrfs_head.available.start + i;
b256068
+
b256068
+  *nsectors = n;
b256068
+  *sectors = map;
b256068
 
b256068
   return GRUB_ERR_NONE;
b256068
 }
b256068
diff --git a/include/grub/disk.h b/include/grub/disk.h
e622855
index f95aca929a..06210a7049 100644
b256068
--- a/include/grub/disk.h
b256068
+++ b/include/grub/disk.h
b256068
@@ -182,6 +182,8 @@ typedef struct grub_disk_memberlist *grub_disk_memberlist_t;
b256068
 /* Return value of grub_disk_native_sectors() in case disk size is unknown. */
b256068
 #define GRUB_DISK_SIZE_UNKNOWN	 0xffffffffffffffffULL
b256068
 
b256068
+#define GRUB_DISK_KiB_TO_SECTORS(x) ((x) << (10 - GRUB_DISK_SECTOR_BITS))
b256068
+
b256068
 /* Convert sector number from one sector size to another. */
b256068
 static inline grub_disk_addr_t
b256068
 grub_convert_sector (grub_disk_addr_t sector,