[PATCH v4] hfsplus: reorder complete combining runs for dentry keys
From: Shawn Shao
Date: Wed Dec 02 2026 - 02:56:43 EST
Dentry hashing and comparison reorder decomposed names in fixed-size
chunks. When a combining run exceeds the chunk size, marks with lower
combining classes in a later chunk cannot move ahead of earlier marks.
The resulting dentry key can differ from the full canonical key produced
by hfsplus_asc2uni().
Keep the 32-unit buffer for the common case. For longer runs, remember the
combining classes present and rescan the run in ascending class order. This
preserves source order for marks of equal class without allocating memory
or growing the iterator state with the run length. Apply the HFS+ legacy
sequence fixups to the streamed key so hashing and comparison agree with
catalog conversion.
Add a regression test with a combining run longer than the fast path.
Fixes: bbe5ebcbc006 ("hfsplus: canonicalize decomposed catalog names like fsck_hfs expects")
Signed-off-by: Shawn Shao <shawnq1@xxxxxxx>
---
Changes in v4:
- Keep a 32-unit fast path and rescan longer runs by combining class using
fixed-size state.
- Apply HFS+ legacy sequence corrections while streaming dentry keys.
- Test equal hash, comparison, and stored catalog key for a 33-mark run.
Testing:
- x86-64 disassembly shows hfsplus_compare_dentry reserves 456 bytes for
its stack frame. The bzImage build passed with CONFIG_HFSPLUS_FS=y,
CONFIG_FRAME_WARN=1280, and KCFLAGS=-Wframe-larger-than=1280; no frame
size warning was emitted.
- hfsplus_unicode KUnit suite: 36 tests passed.
- Fixed-kernel QEMU test: the alternate 33-mark spelling resolved before
remount; the controls passed (POC_EXIT=0).
- git apply --check, git diff --check, and checkpatch --strict passed.
Reproducer, configuration, KUnit output, and console log:
https://gist.github.com/shaos26/73b61181ce48283492d2a10f69bb11eb
fs/hfsplus/hfsplus_unicode_test.c | 83 ++++++++++++
fs/hfsplus/tables.c | 70 +++++------
fs/hfsplus/unicode.c | 258 ++++++++++++++++++++++++++++----------
fs/hfsplus/unicode.h | 1 +
4 files changed, 311 insertions(+), 101 deletions(-)
diff --git a/fs/hfsplus/hfsplus_unicode_test.c b/fs/hfsplus/hfsplus_unicode_test.c
index cb237a40c0c3e..e8f066447e89f 100644
--- a/fs/hfsplus/hfsplus_unicode_test.c
+++ b/fs/hfsplus/hfsplus_unicode_test.c
@@ -1533,6 +1533,88 @@ static void hfsplus_hash_dentry_combining_reorder_test(struct kunit *test)
free_mock_sb(mock_sb);
}
+static void hfsplus_dentry_combining_reorder_long_test(struct kunit *test)
+{
+ static const char acute[] = "\xcc\x81";
+ static const char dot_below[] = "\xcc\x96";
+ enum { NR_ACUTE = 32, NAME_LEN = 1 + (NR_ACUTE + 1) * 2 + 1 };
+ char acute_first[NAME_LEN], dot_first[NAME_LEN];
+ struct hfsplus_unistr *stored1, *stored2;
+ struct test_mock_sb *mock_sb;
+ struct qstr str1, str2;
+ char *p;
+ int i, result;
+
+ mock_sb = setup_mock_sb();
+ KUNIT_ASSERT_NOT_NULL(test, mock_sb);
+
+ stored1 = kunit_kzalloc(test, sizeof(*stored1), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, stored1);
+ stored2 = kunit_kzalloc(test, sizeof(*stored2), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, stored2);
+
+ setup_mock_dentry(&mock_sb->sb);
+ mock_sb->nls.char2uni = test_char2uni_utf8;
+
+ p = acute_first;
+ *p++ = 'e';
+ for (i = 0; i < NR_ACUTE; i++) {
+ memcpy(p, acute, sizeof(acute) - 1);
+ p += sizeof(acute) - 1;
+ }
+ memcpy(p, dot_below, sizeof(dot_below) - 1);
+ p += sizeof(dot_below) - 1;
+ *p = '\0';
+
+ p = dot_first;
+ *p++ = 'e';
+ memcpy(p, dot_below, sizeof(dot_below) - 1);
+ p += sizeof(dot_below) - 1;
+ for (i = 0; i < NR_ACUTE; i++) {
+ memcpy(p, acute, sizeof(acute) - 1);
+ p += sizeof(acute) - 1;
+ }
+ *p = '\0';
+
+ create_qstr(&str1, acute_first);
+ create_qstr(&str2, dot_first);
+ result = hfsplus_hash_dentry(&test_dentry, &str1);
+ KUNIT_ASSERT_EQ(test, 0, result);
+ result = hfsplus_hash_dentry(&test_dentry, &str2);
+ KUNIT_ASSERT_EQ(test, 0, result);
+ KUNIT_EXPECT_EQ(test, str1.hash, str2.hash);
+ KUNIT_EXPECT_EQ(test,
+ hfsplus_compare_dentry(&test_dentry, str1.len,
+ str1.name, &str2), 0);
+
+ result = hfsplus_asc2uni(&mock_sb->sb, stored1, HFSPLUS_MAX_STRLEN,
+ str1.name, str1.len, HFS_REGULAR_NAME);
+ KUNIT_ASSERT_EQ(test, 0, result);
+ result = hfsplus_asc2uni(&mock_sb->sb, stored2, HFSPLUS_MAX_STRLEN,
+ str2.name, str2.len, HFS_REGULAR_NAME);
+ KUNIT_ASSERT_EQ(test, 0, result);
+ KUNIT_EXPECT_EQ(test, be16_to_cpu(stored1->length),
+ be16_to_cpu(stored2->length));
+ KUNIT_EXPECT_EQ(test, 34, be16_to_cpu(stored1->length));
+ KUNIT_EXPECT_EQ(test,
+ memcmp(stored1->unicode, stored2->unicode,
+ be16_to_cpu(stored1->length) *
+ sizeof(stored1->unicode[0])), 0);
+
+ create_qstr(&str1, "e\xcc\x86\xcc\x87");
+ create_qstr(&str2, "e\xcc\x90");
+ result = hfsplus_hash_dentry(&test_dentry, &str1);
+ KUNIT_ASSERT_EQ(test, 0, result);
+ result = hfsplus_hash_dentry(&test_dentry, &str2);
+ KUNIT_ASSERT_EQ(test, 0, result);
+ KUNIT_EXPECT_EQ(test, str1.hash, str2.hash);
+ KUNIT_EXPECT_EQ(test,
+ hfsplus_compare_dentry(&test_dentry, str1.len,
+ str1.name, &str2), 0);
+
+ free_mock_sb(mock_sb);
+}
+
/* Test hfsplus_compare_dentry basic functionality */
static void hfsplus_compare_dentry_basic_test(struct kunit *test)
{
@@ -1935,6 +2017,7 @@ static struct kunit_case hfsplus_unicode_test_cases[] = {
KUNIT_CASE(hfsplus_hash_dentry_consistency_test),
KUNIT_CASE(hfsplus_hash_dentry_edge_cases_test),
KUNIT_CASE(hfsplus_hash_dentry_combining_reorder_test),
+ KUNIT_CASE(hfsplus_dentry_combining_reorder_long_test),
KUNIT_CASE(hfsplus_compare_dentry_basic_test),
KUNIT_CASE(hfsplus_compare_dentry_casefold_test),
KUNIT_CASE(hfsplus_compare_dentry_special_chars_test),
diff --git a/fs/hfsplus/tables.c b/fs/hfsplus/tables.c
index 2101201ab973f..022b655f9ccf1 100644
--- a/fs/hfsplus/tables.c
+++ b/fs/hfsplus/tables.c
@@ -3693,50 +3693,50 @@ const u16 *hfsplus_legacy_decompose(u16 uc, int *size)
return NULL;
}
+/* Apply one legacy correction at the start of a decomposed sequence. */
+bool hfsplus_fixup_legacy_sequence(u16 *buf, int *len)
+{
+ unsigned int j;
+
+ for (j = 0; j < ARRAY_SIZE(hfsplus_legacy_seq_fixups); j++) {
+ struct hfsplus_legacy_seq_fixup *f =
+ &hfsplus_legacy_seq_fixups[j];
+ size_t copy_len;
+ int k;
+
+ if (f->match_len > *len)
+ continue;
+ for (k = 0; k < f->match_len; k++)
+ if (buf[k] != f->match[k])
+ break;
+ if (k != f->match_len)
+ continue;
+
+ copy_len = (*len - f->match_len) * sizeof(*buf);
+ memmove(buf + f->repl_len, buf + f->match_len, copy_len);
+ memcpy(buf, f->repl, f->repl_len * sizeof(*buf));
+ *len += f->repl_len - f->match_len;
+ return true;
+ }
+
+ return false;
+}
+
/*
* Scan an already decomposed and canonically-reordered code unit buffer
- * for any of the short legacy sequences above and replace them in place
- * with their corrected form. @len is updated to the buffer's new length.
+ * for corrected legacy sequences. @len is updated to the buffer's new
+ * length.
*/
void hfsplus_fixup_legacy_sequences(u16 *buf, int *len)
{
int i = 0;
while (i < *len) {
- unsigned int j;
- bool matched = false;
+ int remaining = *len - i;
- for (j = 0; j < ARRAY_SIZE(hfsplus_legacy_seq_fixups); j++) {
- struct hfsplus_legacy_seq_fixup *f =
- &hfsplus_legacy_seq_fixups[j];
- u16 *src, *dst;
- size_t copy_len;
- int k;
-
- if (i + f->match_len > *len)
- continue;
- for (k = 0; k < f->match_len; k++)
- if (buf[i + k] != f->match[k])
- break;
- if (k != f->match_len)
- continue;
-
- dst = &buf[i + f->repl_len];
- src = &buf[i + f->match_len];
- copy_len = (*len - i - f->match_len) * sizeof(*buf);
- memmove(dst, src, copy_len);
-
- dst = &buf[i];
- src = f->repl;
- copy_len = f->repl_len * sizeof(*buf);
- memcpy(dst, src, copy_len);
-
- *len += f->repl_len - f->match_len;
- matched = true;
- break;
- }
-
- if (!matched)
+ if (hfsplus_fixup_legacy_sequence(buf + i, &remaining))
+ *len = i + remaining;
+ else
i++;
}
}
diff --git a/fs/hfsplus/unicode.c b/fs/hfsplus/unicode.c
index 59b8788a69a9e..740107f799bad 100644
--- a/fs/hfsplus/unicode.c
+++ b/fs/hfsplus/unicode.c
@@ -576,37 +576,47 @@ int hfsplus_asc2uni(struct super_block *sb,
}
EXPORT_SYMBOL_IF_KUNIT(hfsplus_asc2uni);
-/*
- * Maximum length of a single maximal run of nonzero-combining-class code
- * units that hfsplus_decompose_iter_next() below will canonically
- * reorder. Every code unit with combining class 0 starts a new run, so
- * this only bounds how many *consecutive* combining marks between two
- * base characters get sorted - real text, and even deliberately
- * adversarial "Zalgo" text, essentially never approaches this. Keeping
- * it small means hfsplus_hash_dentry() and hfsplus_compare_dentry() only
- * ever need a tiny amount of lookahead state, rather than buffering an
- * entire (up to 255-unit) name.
- */
-#define HFSPLUS_CCC_RUN_MAX 32
+#define HFSPLUS_HANGUL_MAX_JAMO (3) /* L + V + optional T */
+#define HFSPLUS_CCC_RUN_MAX (32) /* fast-path run size */
+
+static_assert(HFSPLUS_HANGUL_MAX_JAMO >= HFSPLUS_LEGACY_DECOMP_MAX_LEN,
+ "decomposition buffer must fit the longest legacy sequence");
+
+/* State for reading decomposed UTF-16 code units from an input name. */
+struct hfsplus_decompose_state {
+ const char *astr;
+ int len;
+ u16 units[HFSPLUS_HANGUL_MAX_JAMO];
+ u8 unit_pos;
+ u8 unit_len;
+};
/*
- * Iterator that produces the canonically-ordered, decomposed code units
- * of a Linux name one at a time, without materializing the whole name.
- * hfsplus_hash_dentry() and hfsplus_compare_dentry() use this so that
- * hashing and comparison always agree with what hfsplus_asc2uni() would
- * actually store, while still comparing lazily (stopping at the first
- * difference) the way this code did before canonical reordering existed.
+ * The dentry iterator keeps short runs in a fixed-size fast path and
+ * rescans longer runs by combining class. Its storage does not grow with
+ * the input run.
*/
struct hfsplus_decompose_iter {
struct super_block *sb;
- const char *astr;
- int len;
int name_type;
int decompose;
- u16 run[HFSPLUS_CCC_RUN_MAX];
- int run_len;
- int run_pos;
+ struct hfsplus_decompose_state raw;
+ struct hfsplus_decompose_state run_start;
+ struct hfsplus_decompose_state scan;
+ u16 fast_run[HFSPLUS_CCC_RUN_MAX];
+ u8 classes[U8_MAX / 8 + 1];
+ unsigned int fast_len;
+ unsigned int fast_pos;
+ unsigned int run_len;
+ unsigned int run_left;
+ unsigned int run_class;
+ bool fast_active;
+ bool run_active;
+
+ u16 fixup[HFSPLUS_LEGACY_SEQ_MAX_MATCH];
+ int fixup_len;
+ bool raw_done;
};
static void hfsplus_decompose_iter_init(struct hfsplus_decompose_iter *it,
@@ -615,68 +625,184 @@ static void hfsplus_decompose_iter_init(struct hfsplus_decompose_iter *it,
int name_type)
{
it->sb = sb;
- it->astr = astr;
- it->len = len;
it->name_type = name_type;
it->decompose = !test_bit(HFSPLUS_SB_NODECOMPOSE, &HFSPLUS_SB(sb)->flags);
+ it->raw.astr = astr;
+ it->raw.len = len;
+ memset(it->raw.units, 0, sizeof(it->raw.units));
+ it->raw.unit_pos = 0;
+ it->raw.unit_len = 0;
+ it->fast_len = 0;
+ it->fast_pos = 0;
it->run_len = 0;
- it->run_pos = 0;
+ it->run_left = 0;
+ it->run_class = 0;
+ it->fast_active = false;
+ it->run_active = false;
+ it->fixup_len = 0;
+ it->raw_done = false;
}
-/*
- * Gather the next maximal run of nonzero-combining-class code units
- * (starting with whatever character comes next, base or not) and
- * canonically reorder just that run. A character is only included once
- * we know the class of the first unit it produces, so decoding it is
- * speculative until that's decided.
- */
-static bool hfsplus_decompose_iter_refill(struct hfsplus_decompose_iter *it)
+static int hfsplus_decompose_iter_raw_next(struct hfsplus_decompose_iter *it,
+ struct hfsplus_decompose_state *state)
{
- it->run_pos = 0;
- it->run_len = 0;
+ u16 dhangul[HFSPLUS_HANGUL_MAX_JAMO];
+ const u16 *dstr;
+ int dsize, size;
+ wchar_t c;
+
+ if (state->unit_pos < state->unit_len)
+ return state->units[state->unit_pos++];
+ if (!state->len)
+ return -1;
- while (it->len > 0) {
- u16 *dstr;
- u16 dhangul[HFSPLUS_HANGUL_MAX_JAMO];
- int dsize, size;
- wchar_t c;
+ size = asc2unichar(it->sb, state->astr, state->len, &c,
+ it->name_type);
+ dstr = it->decompose ? decompose_unichar(c, &dsize, dhangul) : NULL;
+ if (!dstr) {
+ dhangul[0] = c;
+ dstr = dhangul;
+ dsize = 1;
+ }
- size = asc2unichar(it->sb, it->astr, it->len, &c,
- it->name_type);
+ memcpy(state->units, dstr, dsize * sizeof(*dstr));
+ state->unit_len = dsize;
+ state->unit_pos = 1;
+ state->astr += size;
+ state->len -= size;
+ return state->units[0];
+}
+
+static void hfsplus_decompose_iter_set_class(struct hfsplus_decompose_iter *it,
+ u8 cls)
+{
+ it->classes[cls / 8] |= 1U << (cls % 8);
+}
+
+static bool hfsplus_decompose_iter_has_class(struct hfsplus_decompose_iter *it,
+ unsigned int cls)
+{
+ return it->classes[cls / 8] & (1U << (cls % 8));
+}
- dstr = it->decompose ?
- decompose_unichar(c, &dsize, dhangul) : NULL;
- if (!dstr) {
- dhangul[0] = c;
- dstr = dhangul;
- dsize = 1;
+/* Return the next canonically ordered code unit without buffering a run. */
+static int hfsplus_decompose_iter_next_reordered(struct hfsplus_decompose_iter *it)
+{
+ while (1) {
+ if (it->fast_active) {
+ if (it->fast_pos < it->fast_len)
+ return it->fast_run[it->fast_pos++];
+ it->fast_active = false;
}
- if (it->run_len > 0 && !hfsplus_combining_class(dstr[0]))
- break;
+ if (it->run_active) {
+ while (it->run_left) {
+ int c = hfsplus_decompose_iter_raw_next(it, &it->scan);
- if (it->run_len + dsize > HFSPLUS_CCC_RUN_MAX)
- break;
+ if (c < 0) {
+ it->run_active = false;
+ return -1;
+ }
+ it->run_left--;
+ if (hfsplus_combining_class(c) == it->run_class)
+ return c;
+ }
- it->astr += size;
- it->len -= size;
+ while (++it->run_class <= U8_MAX) {
+ if (hfsplus_decompose_iter_has_class(it,
+ it->run_class)) {
+ it->scan = it->run_start;
+ it->run_left = it->run_len;
+ break;
+ }
+ }
+ if (it->run_class <= U8_MAX)
+ continue;
+ it->run_active = false;
+ }
- do {
- it->run[it->run_len++] = *dstr++;
- } while (--dsize > 0);
- }
+ {
+ struct hfsplus_decompose_state scan;
+ int c;
+ u8 cls;
+ bool overflow = false;
+
+ it->run_start = it->raw;
+ c = hfsplus_decompose_iter_raw_next(it, &it->raw);
+ if (c < 0)
+ return -1;
+ cls = hfsplus_combining_class(c);
+ if (!cls)
+ return c;
+
+ memset(it->classes, 0, sizeof(it->classes));
+ it->fast_len = 1;
+ it->fast_run[0] = c;
+ it->run_len = 1;
+ hfsplus_decompose_iter_set_class(it, cls);
+ scan = it->raw;
+ while (1) {
+ struct hfsplus_decompose_state next = scan;
+
+ c = hfsplus_decompose_iter_raw_next(it, &next);
+ if (c < 0 || !hfsplus_combining_class(c))
+ break;
+ scan = next;
+ it->raw = next;
+ if (it->fast_len < HFSPLUS_CCC_RUN_MAX)
+ it->fast_run[it->fast_len++] = c;
+ else
+ overflow = true;
+ it->run_len++;
+ hfsplus_decompose_iter_set_class(it,
+ hfsplus_combining_class(c));
+ }
+
+ if (!overflow) {
+ hfsplus_canonical_reorder(it->fast_run,
+ it->fast_len);
+ it->fast_pos = 0;
+ it->fast_active = true;
+ continue;
+ }
- hfsplus_canonical_reorder(it->run, it->run_len);
- hfsplus_fixup_legacy_sequences(it->run, &it->run_len);
- return it->run_len > 0;
+ it->run_class = 0;
+ it->scan = it->run_start;
+ it->run_left = it->run_len;
+ it->run_active = true;
+ }
+ }
}
-/* Returns the next code unit, or a negative value once @it is exhausted. */
+/* Apply legacy replacements with a three-code-unit lookahead. */
static int hfsplus_decompose_iter_next(struct hfsplus_decompose_iter *it)
{
- if (it->run_pos >= it->run_len && !hfsplus_decompose_iter_refill(it))
- return -1;
- return it->run[it->run_pos++];
+ while (1) {
+ while (it->fixup_len < HFSPLUS_LEGACY_SEQ_MAX_MATCH &&
+ !it->raw_done) {
+ int c = hfsplus_decompose_iter_next_reordered(it);
+
+ if (c < 0)
+ it->raw_done = true;
+ else
+ it->fixup[it->fixup_len++] = c;
+ }
+
+ if (!it->fixup_len)
+ return -1;
+
+ if (hfsplus_fixup_legacy_sequence(it->fixup, &it->fixup_len))
+ continue;
+
+ {
+ int c = it->fixup[0];
+
+ it->fixup_len--;
+ memmove(it->fixup, it->fixup + 1,
+ it->fixup_len * sizeof(*it->fixup));
+ return c;
+ }
+ }
}
/*
diff --git a/fs/hfsplus/unicode.h b/fs/hfsplus/unicode.h
index 31cc7691c9162..db16e1d5dd1bb 100644
--- a/fs/hfsplus/unicode.h
+++ b/fs/hfsplus/unicode.h
@@ -58,6 +58,7 @@ extern struct hfsplus_legacy_seq_fixup hfsplus_legacy_seq_fixups[];
u8 hfsplus_combining_class(u16 c);
const u16 *hfsplus_legacy_decompose(u16 uc, int *size);
+bool hfsplus_fixup_legacy_sequence(u16 *buf, int *len);
void hfsplus_fixup_legacy_sequences(u16 *buf, int *len);
#endif /* _LINUX_HFSPLUS_UNICODE_H */