0013-fs-jfs-Use-full-40-bits-offset-and-address-for-a-dat.patch 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. From 978c4c79935a375cb16d94e8114d96fee013c288 Mon Sep 17 00:00:00 2001
  2. From: Lidong Chen <lidong.chen@oracle.com>
  3. Date: Mon, 16 Dec 2024 20:22:39 +0000
  4. Subject: [PATCH] fs/jfs: Use full 40 bits offset and address for a data extent
  5. An extent's logical offset and address are represented as a 40-bit value
  6. split into two parts: the most significant 8 bits and the least
  7. significant 32 bits. Currently the JFS code uses only the least
  8. significant 32 bits value for offsets and addresses assuming the data
  9. size will never exceed the 32-bit range. This approach ignores the most
  10. significant 8 bits potentially leading to incorrect offsets and
  11. addresses for larger values. The patch fixes it by incorporating the
  12. most significant 8 bits into the calculation to get the full 40-bits
  13. value for offsets and addresses.
  14. https://jfs.sourceforge.net/project/pub/jfslayout.pdf
  15. "off1,off2 is a 40-bit field, containing the logical offset of the first
  16. block in the extent.
  17. ...
  18. addr1,addr2 is a 40-bit field, containing the address of the extent."
  19. Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
  20. Reviewed-by: Alec Brown <alec.r.brown@oracle.com>
  21. Reviewed-by: Ross Philipson <ross.philipson@oracle.com>
  22. Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
  23. Upstream: bd999310fe67f35a66de3bfa2836da91589d04ef
  24. Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
  25. ---
  26. grub-core/fs/jfs.c | 41 +++++++++++++++++++++++++++++------------
  27. 1 file changed, 29 insertions(+), 12 deletions(-)
  28. diff --git a/grub-core/fs/jfs.c b/grub-core/fs/jfs.c
  29. index 88fb884df..2bde48d45 100644
  30. --- a/grub-core/fs/jfs.c
  31. +++ b/grub-core/fs/jfs.c
  32. @@ -265,6 +265,20 @@ static grub_dl_t my_mod;
  33. static grub_err_t grub_jfs_lookup_symlink (struct grub_jfs_data *data, grub_uint32_t ino);
  34. +/*
  35. + * An extent's offset, physical and logical, is represented as a 40-bit value.
  36. + * This 40-bit value is split into two parts:
  37. + * - offset1: the most signficant 8 bits of the offset,
  38. + * - offset2: the least significant 32 bits of the offset.
  39. + *
  40. + * This function calculates and returns the 64-bit offset of an extent.
  41. + */
  42. +static grub_uint64_t
  43. +get_ext_offset (grub_uint8_t offset1, grub_uint32_t offset2)
  44. +{
  45. + return (((grub_uint64_t) offset1 << 32) | grub_le_to_cpu32 (offset2));
  46. +}
  47. +
  48. static grub_int64_t
  49. getblk (struct grub_jfs_treehead *treehead,
  50. struct grub_jfs_tree_extent *extents,
  51. @@ -274,22 +288,25 @@ getblk (struct grub_jfs_treehead *treehead,
  52. {
  53. int found = -1;
  54. int i;
  55. + grub_uint64_t ext_offset, ext_blk;
  56. for (i = 0; i < grub_le_to_cpu16 (treehead->count) - 2 &&
  57. i < max_extents; i++)
  58. {
  59. + ext_offset = get_ext_offset (extents[i].offset1, extents[i].offset2);
  60. + ext_blk = get_ext_offset (extents[i].extent.blk1, extents[i].extent.blk2);
  61. +
  62. if (treehead->flags & GRUB_JFS_TREE_LEAF)
  63. {
  64. /* Read the leafnode. */
  65. - if (grub_le_to_cpu32 (extents[i].offset2) <= blk
  66. + if (ext_offset <= blk
  67. && ((grub_le_to_cpu16 (extents[i].extent.length))
  68. + (extents[i].extent.length2 << 16)
  69. - + grub_le_to_cpu32 (extents[i].offset2)) > blk)
  70. - return (blk - grub_le_to_cpu32 (extents[i].offset2)
  71. - + grub_le_to_cpu32 (extents[i].extent.blk2));
  72. + + ext_offset) > blk)
  73. + return (blk - ext_offset + ext_blk);
  74. }
  75. else
  76. - if (blk >= grub_le_to_cpu32 (extents[i].offset2))
  77. + if (blk >= ext_offset)
  78. found = i;
  79. }
  80. @@ -307,10 +324,9 @@ getblk (struct grub_jfs_treehead *treehead,
  81. return -1;
  82. if (!grub_disk_read (data->disk,
  83. - ((grub_disk_addr_t) grub_le_to_cpu32 (extents[found].extent.blk2))
  84. - << (grub_le_to_cpu16 (data->sblock.log2_blksz)
  85. - - GRUB_DISK_SECTOR_BITS), 0,
  86. - sizeof (*tree), (char *) tree))
  87. + (grub_disk_addr_t) ext_blk
  88. + << (grub_le_to_cpu16 (data->sblock.log2_blksz) - GRUB_DISK_SECTOR_BITS),
  89. + 0, sizeof (*tree), (char *) tree))
  90. {
  91. if (grub_memcmp (&tree->treehead, treehead, sizeof (struct grub_jfs_treehead)) ||
  92. grub_memcmp (&tree->extents, extents, 254 * sizeof (struct grub_jfs_tree_extent)))
  93. @@ -361,7 +377,7 @@ grub_jfs_read_inode (struct grub_jfs_data *data, grub_uint32_t ino,
  94. sizeof (iag_inodes), &iag_inodes))
  95. return grub_errno;
  96. - inoblk = grub_le_to_cpu32 (iag_inodes[inoext].blk2);
  97. + inoblk = get_ext_offset (iag_inodes[inoext].blk1, iag_inodes[inoext].blk2);
  98. inoblk <<= (grub_le_to_cpu16 (data->sblock.log2_blksz)
  99. - GRUB_DISK_SECTOR_BITS);
  100. inoblk += inonum;
  101. @@ -490,7 +506,8 @@ grub_jfs_opendir (struct grub_jfs_data *data, struct grub_jfs_inode *inode)
  102. return 0;
  103. }
  104. - blk = grub_le_to_cpu32 (de[inode->dir.header.sorted[0]].ex.blk2);
  105. + blk = get_ext_offset (de[inode->dir.header.sorted[0]].ex.blk1,
  106. + de[inode->dir.header.sorted[0]].ex.blk2);
  107. blk <<= (grub_le_to_cpu16 (data->sblock.log2_blksz) - GRUB_DISK_SECTOR_BITS);
  108. /* Read in the nodes until we are on the leaf node level. */
  109. @@ -508,7 +525,7 @@ grub_jfs_opendir (struct grub_jfs_data *data, struct grub_jfs_inode *inode)
  110. de = (struct grub_jfs_internal_dirent *) diro->dirpage->dirent;
  111. index = diro->dirpage->sorted[diro->dirpage->header.sindex * 32];
  112. - blk = (grub_le_to_cpu32 (de[index].ex.blk2)
  113. + blk = (get_ext_offset (de[index].ex.blk1, de[index].ex.blk2)
  114. << (grub_le_to_cpu16 (data->sblock.log2_blksz)
  115. - GRUB_DISK_SECTOR_BITS));
  116. } while (!(diro->dirpage->header.flags & GRUB_JFS_TREE_LEAF));
  117. --
  118. 2.50.1