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