diff --git a/drivers/mtd/apple-sftl-core.c b/drivers/mtd/apple-sftl-core.c index 4a810908a1c057..3ef448ef9b66f6 100644 --- a/drivers/mtd/apple-sftl-core.c +++ b/drivers/mtd/apple-sftl-core.c @@ -78,6 +78,11 @@ struct sftl_geometry { bool summary_sequences; /* a summary may consume one sequence per chunk */ bool degrade_remnants; /* a context block with an unknown base only blocks writes */ bool hidden_tail_type2; /* iOS 9 writes the hidden tail as type 2, flag 1 */ + bool older_epoch_subtype1; /* type 1, flag 1 after the checkpoint may keep an + * older user epoch. 16 GB only until confirmed on + * the device: it is read-only, while a 64 GB + * checkpoint would persist a wrong acceptance. + */ }; /* 64 GB Hynix PPN with 13G36 (iOS 9.3.5) on the media. */ @@ -105,6 +110,7 @@ static const struct sftl_geometry sftl_16gb = { .map_batch = 16, .disk_pages = 3906250, .lpages = 3906266, .summary_sequences = true, .degrade_remnants = true, .hidden_tail_type2 = true, + .older_epoch_subtype1 = true, }; static const struct sftl_geometry *const sftl_geometries[] = { @@ -1395,9 +1401,11 @@ static bool sftl_journal_meta(const struct apple_sftl *sftl, const u8 *meta) u64 sequence = sftl_sequence(meta); /* Native subtype-1 data covered by the checkpoint can retain an - * earlier user epoch. It occupies the page but is never replayed. + * earlier user epoch. On 16 GB media allow the same shape in replay: + * a move has a new sequence but can retain the original user epoch. */ - if (meta[0] == 1 && meta[1] == 1 && sequence <= sftl->checkpoint_sequence && + if (meta[0] == 1 && meta[1] == 1 && + (sequence <= sftl->checkpoint_sequence || sftl->geo->older_epoch_subtype1) && get_unaligned_le32(meta + 8) < sftl->geo->disk_pages) return get_unaligned_le32(meta + 12) <= sequence >> 16; @@ -1410,6 +1418,17 @@ static bool sftl_journal_meta(const struct apple_sftl *sftl, const u8 *meta) get_unaligned_le32(meta + 8) < sftl->geo->lpages) return sftl_user_epoch_valid(sftl, meta, sequence); + /* iOS 9 also writes a TRIM record as type 2 with flag 1 (a 16 GB P105 + * showed the tag three times, each 79 or 95 sequences below its + * checkpoint and followed by ordinary records in the same open block). + * The epoch must equal the sequence's, as for every type-2 record. + * If replay encounters one above the checkpoint, it consumes the + * sequence without applying the unverified payload. + */ + if (sftl->geo->hidden_tail_type2 && meta[0] == 2 && meta[1] == 1 && + get_unaligned_le32(meta + 8) == SFTL_TRIM) + return get_unaligned_le32(meta + 12) == sequence >> 16; + return sftl_erased(meta, SFTL_META) || ((meta[0] == 1 || meta[0] == 2) && sftl_user_epoch_valid(sftl, meta, sequence) && @@ -1557,7 +1576,8 @@ static int sftl_footer(struct apple_sftl *sftl, const u8 *head, u32 block, if (ret) goto out; memcpy(meta, sftl->oob + chunk * SFTL_META, sizeof(meta)); - if (meta[0] != 2 || meta[1] || sftl_sequence(meta) != sequence || + /* sftl_add_journal() checks the flag and epoch. */ + if (meta[0] != 2 || sftl_sequence(meta) != sequence || get_unaligned_le32(meta + 8) != SFTL_TRIM) { ret = -EBADMSG; goto out; @@ -1781,6 +1801,12 @@ static int sftl_journal(struct apple_sftl *sftl, const u8 *heads, struct sftl_re const u8 *payload; u32 count, offset; + /* Flag 1 may mark a relocated TRIM. Applying its + * unverified payload could discard newer data; a + * missed TRIM leaves data in filesystem-free LBAs. + */ + if (sftl_flags(record) == 1) + continue; ret = sftl_read_address(sftl, record->address, &chunk); if (ret) goto out; diff --git a/drivers/mtd/apple-sftl-test.c b/drivers/mtd/apple-sftl-test.c index 95637f39e05292..a266329fe5a417 100644 --- a/drivers/mtd/apple-sftl-test.c +++ b/drivers/mtd/apple-sftl-test.c @@ -42,6 +42,8 @@ enum sftl_fault { SFTL_UNKNOWN_STATUS, SFTL_TORN_FOOTER, SFTL_RUNTIME_FAILURE, + SFTL_CLOSED_TRIM_FLAG0, + SFTL_CLOSED_TRIM_FLAG1, }; enum sftl_test_commit { @@ -281,6 +283,19 @@ static void sftl_test_closed(struct sftl_fixture *f, unsigned int position, memset(data + i * 4096, (slot + 1) & 0xff, 4096); sftl_test_meta(oob + i * 16, 1, 0, TEST_CLOSED_FIRST + f->sequence_bias + slot, slot + 1); + if (slot == TEST_CLOSED_CHUNKS - 1 && + (f->fault == SFTL_CLOSED_TRIM_FLAG0 || + f->fault == SFTL_CLOSED_TRIM_FLAG1)) { + /* The last record is a TRIM of LBA 1 (the summary says so). */ + memset(data + i * 4096, 0, 4096); + put_unaligned_le32(1, data + i * 4096); + put_unaligned_le32(TEST_CLOSED_FIRST >> 16, data + i * 4096 + 4); + put_unaligned_le32(1, data + i * 4096 + 8); + sftl_test_meta(oob + i * 16, 2, + f->fault == SFTL_CLOSED_TRIM_FLAG1, + TEST_CLOSED_FIRST + f->sequence_bias + slot, + 0xffff0001); + } } } else { u32 span = TEST_CLOSED_CHUNKS; @@ -290,6 +305,13 @@ static void sftl_test_closed(struct sftl_fixture *f, unsigned int position, put_unaligned_le32((TEST_CLOSED_FIRST + f->sequence_bias) >> 16, data + 4); put_unaligned_le32(1, data + 8); put_unaligned_le32(TEST_CLOSED_CHUNKS, data + 12); + if (f->fault == SFTL_CLOSED_TRIM_FLAG0 || f->fault == SFTL_CLOSED_TRIM_FLAG1) { + put_unaligned_le32(TEST_CLOSED_CHUNKS - 1, data + 12); + put_unaligned_le32(TEST_CLOSED_CHUNKS - 1, data + 16); + put_unaligned_le32((TEST_CLOSED_FIRST + f->sequence_bias) >> 16, data + 20); + put_unaligned_le32(0xffff0001, data + 24); + put_unaligned_le32(1, data + 28); + } if (f->fault == SFTL_ALLOCATION_FLOOR) span += 8; sftl_test_meta(oob, 0x1c, 8, TEST_CLOSED_FIRST + f->sequence_bias, span); @@ -1448,6 +1470,54 @@ static void sftl_test_hidden_tail_is_torn_on_64gb(struct kunit *test) KUNIT_EXPECT_PTR_EQ(test, sftl, NULL); } +static void sftl_test_trim_flag1_is_torn_on_64gb(struct kunit *test) +{ + struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); + struct sftl_test_page *page = kunit_kzalloc(test, sizeof(*page), GFP_KERNEL); + struct apple_sftl *sftl = NULL; + unsigned int i; + + KUNIT_ASSERT_NOT_NULL(test, f); + KUNIT_ASSERT_NOT_NULL(test, page); + put_unaligned_le32(1, page->data); + put_unaligned_le32(2, page->data + 8); + for (i = 0; i < 4; i++) + sftl_test_meta(page->oob + i * APPLE_SFTL_META, 2, 0, + TEST_APPEND_FIRST + i, 0xffff0001); + KUNIT_ASSERT_EQ(test, sftl_test_program_page(f, 0, 0, 5, 0, page->data, page->oob), 0); + memset(page->data, 'n', sizeof(page->data)); + for (i = 0; i < 4; i++) + sftl_test_meta(page->oob + i * APPLE_SFTL_META, 1, 0, + TEST_APPEND_FIRST + 4 + i, TEST_APPEND_BASE + i); + KUNIT_ASSERT_EQ(test, sftl_test_program_page(f, 0, 0, 5, 1, page->data, page->oob), 0); + /* With flag 0 the same medium, including the later data page, loads. */ + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + apple_sftl_free(sftl); + + page = sftl_test_written(f, 0, 0, 0); + KUNIT_ASSERT_NOT_NULL(test, page); + for (i = 0; i < 4; i++) + page->oob[i * APPLE_SFTL_META + 1] = 1; + KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); + KUNIT_EXPECT_PTR_EQ(test, sftl, NULL); + apple_sftl_free(sftl); +} + +static void sftl_test_closed_trim_flag1_is_torn_on_64gb(struct kunit *test) +{ + struct sftl_fixture *f = sftl_test_fixture(test, SFTL_CLOSED_TRIM_FLAG0); + struct apple_sftl *sftl = NULL; + + KUNIT_ASSERT_NOT_NULL(test, f); + /* A flag-0 TRIM in the closed block's summary loads. */ + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + apple_sftl_free(sftl); + sftl = NULL; + f->fault = SFTL_CLOSED_TRIM_FLAG1; + KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); + KUNIT_EXPECT_PTR_EQ(test, sftl, NULL); +} + static void sftl_test_append_scans_entire_candidate(struct kunit *test) { struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); @@ -3464,9 +3534,23 @@ struct t16_fixture { bool remnant_reused; /* erased and rewritten with the journal's next records */ bool remnant_mixed; /* ... but its last record is an old one */ bool remnant_context; /* its second chunk is a context record newer than the checkpoint */ + bool remnant_moved; /* reused for flag-1 moves, whose user epoch is older */ u32 hidden_key; /* two pages of type 2, flag 1 records, then data, after the new block */ u32 hidden_epoch_skew; /* added to the hidden records' user epoch */ bool hidden_last; /* only one such page, every record with hidden_key */ + u32 moved_pre; /* flag-1 slots the checkpoint covers, before the moves */ + u32 moved_slots; /* type 1, flag 1 slots at the head of the next block */ + u32 moved_key; /* first moved key; its source is the old block */ + bool moved_future; /* the user epoch is one ahead of the sequence's */ + bool moved_last; /* omit ordinary records after the moved slots */ + u32 trim_pre; /* type 2, flag 1 TRIM slots the checkpoint covers, at the next block's head */ + u32 trim_slots; /* the same after the checkpoint, targeting LBAs 0 and 1 */ + int trim_epoch_skew; /* added to the covered or closed TRIM's user epoch */ + u32 trim_key; /* key of the covered records when not SFTL_TRIM */ + u8 trim_flag; /* flags of the covered records when not 1 */ + bool trim_bad_count; /* the first TRIM above the checkpoint claims too many ranges */ + bool trim_last; /* omit ordinary records after the TRIM slots */ + bool closed_trim; /* one TRIM after the data run in the new closed block */ }; /* Fixed records before the ranges: the native base, statistics and tables. */ @@ -3642,6 +3726,7 @@ static void t16_slot(struct t16_fixture *f, u32 block, u32 slot, u8 *payload, u8 } if (block == T16_NEW && f->new_block) { u64 base = T16_CONTEXT_BASE + t16_records(f); + u32 pad_start = T16_NEW_RUN + f->closed_trim; if (slot >= t16_new_slots()) { u32 index = slot - t16_new_slots(); @@ -3652,15 +3737,23 @@ static void t16_slot(struct t16_fixture *f, u32 block, u32 slot, u8 *payload, u8 put_unaligned_le16(0xffff, meta + 14); memset(payload, 0, 4096); if (!index) { - /* One data run, then padding up to the summary. */ + /* One data run, an optional TRIM, then padding. */ put_unaligned_le32(0, payload); put_unaligned_le32(base >> 16, payload + 4); put_unaligned_le32(T16_NEW_LBA, payload + 8); put_unaligned_le32(T16_NEW_RUN, payload + 12); - put_unaligned_le32(T16_NEW_RUN, payload + 16); - put_unaligned_le32(base >> 16, payload + 20); - put_unaligned_le32(0xffff0000, payload + 24); - put_unaligned_le32(t16_new_slots() - T16_NEW_RUN, payload + 28); + payload += 16; + if (f->closed_trim) { + put_unaligned_le32(T16_NEW_RUN, payload); + put_unaligned_le32(base >> 16, payload + 4); + put_unaligned_le32(0xffff0001, payload + 8); + put_unaligned_le32(1, payload + 12); + payload += 16; + } + put_unaligned_le32(pad_start, payload); + put_unaligned_le32(base >> 16, payload + 4); + put_unaligned_le32(0xffff0000, payload + 8); + put_unaligned_le32(t16_new_slots() - pad_start, payload + 12); } return; } @@ -3668,19 +3761,30 @@ static void t16_slot(struct t16_fixture *f, u32 block, u32 slot, u8 *payload, u8 memset(payload, 'n', 4096); put_unaligned_le32(T16_NEW_LBA + slot, payload); t16_meta(meta, 1, 0, base + slot, T16_NEW_LBA + slot, (base + slot) >> 16); + } else if (slot < pad_start) { + memset(payload, 0, 4096); + put_unaligned_le32(1, payload); + put_unaligned_le32(2, payload + 8); + t16_meta(meta, 2, f->trim_flag ?: 1, base + slot, 0xffff0001, + ((base + slot) >> 16) + f->trim_epoch_skew); } else { memset(payload, 0, 4096); t16_meta(meta, 1, 0, base + slot, 0xffff0000, (base + slot) >> 16); } return; } - if (block == T16_REMNANT && f->remnant && f->remnant_reused && slot < 4 && - !(f->remnant_mixed && slot == 3)) { - u64 sequence = T16_CONTEXT_BASE + t16_records(f) + t16_new_slots() + slot; + if (block == T16_REMNANT && f->remnant && f->remnant_reused && + slot < 4 && !(f->remnant_mixed && slot == 3)) { + u32 index = slot; + u64 sequence = T16_CONTEXT_BASE + t16_records(f) + t16_new_slots() + index; + /* A move keeps its source's user epoch; its higher sequence wins. + * The 'x' payload marker is test-only: a real move copies its source. + */ memset(payload, 'x', 4096); - put_unaligned_le32(T16_NEXT_LBA + slot, payload); - t16_meta(meta, 1, 0, sequence, T16_NEXT_LBA + slot, sequence >> 16); + put_unaligned_le32(T16_NEXT_LBA + index, payload); + t16_meta(meta, 1, f->remnant_moved, sequence, T16_NEXT_LBA + index, + f->remnant_moved ? 0 : sequence >> 16); return; } if (block == T16_REMNANT && f->remnant && f->remnant_context && slot == 1) { @@ -3715,6 +3819,67 @@ static void t16_slot(struct t16_fixture *f, u32 block, u32 slot, u8 *payload, u8 } return; } + if (block == T16_NEXT && (f->trim_pre || f->trim_slots)) { + u64 base = T16_CONTEXT_BASE + t16_records(f) + t16_new_slots(); + u32 above = slot - f->trim_pre; + + /* iOS 9 writes a TRIM as type 2, flag 1: the key is the TRIM tag. */ + if (slot < f->trim_pre) { + u64 sequence = T16_CONTEXT_BASE - f->trim_pre + slot; + + memset(payload, 0, 4096); + t16_meta(meta, 2, f->trim_flag ?: 1, sequence, f->trim_key ?: 0xffff0001, + (sequence >> 16) + f->trim_epoch_skew); + } else if (above < f->trim_slots) { + u64 sequence = base + above; + + memset(payload, 0, 4096); + t16_meta(meta, 2, 1, sequence, 0xffff0001, sequence >> 16); + if (!above) { + put_unaligned_le32(f->trim_bad_count ? 4096 : 1, payload); + put_unaligned_le32(0, payload + 4); + put_unaligned_le32(2, payload + 8); + } + } else if (!f->trim_last && above < f->trim_slots + 4) { + u64 sequence = base + above; + + memset(payload, 'x', 4096); + put_unaligned_le32(T16_NEXT_LBA + above - f->trim_slots, payload); + t16_meta(meta, 1, 0, sequence, T16_NEXT_LBA + above - f->trim_slots, + sequence >> 16); + } + return; + } + if (block == T16_NEXT && f->moved_slots) { + u64 sequence = T16_CONTEXT_BASE + t16_records(f) + t16_new_slots() + + slot - f->moved_pre; + u32 key; + + if (slot < f->moved_pre) { + /* Flag-1 data the checkpoint already covers. */ + key = T16_NEXT_LBA + 8 + slot; + memset(payload, 's', 4096); + t16_meta(meta, 1, 1, T16_CONTEXT_BASE - f->moved_pre + slot, key, 0); + } else if (slot < f->moved_pre + f->moved_slots) { + slot -= f->moved_pre; + /* A move keeps its source's user epoch; its higher sequence wins. + * The 'g' marker is test-only: a real move copies its source. + */ + key = f->moved_key + slot; + memset(payload, 'g', 4096); + t16_meta(meta, 1, 1, sequence, key, + f->moved_future ? (sequence >> 16) + 1 : 0); + } else if (!f->moved_last && slot < f->moved_pre + f->moved_slots + 4) { + slot -= f->moved_pre + f->moved_slots; + key = T16_NEXT_LBA + slot; + memset(payload, 'x', 4096); + t16_meta(meta, 1, 0, sequence, key, sequence >> 16); + } else { + return; + } + put_unaligned_le32(key, payload); + return; + } if (block == T16_NEXT && f->next_block && slot < T16_SUMMARY) { u64 sequence = T16_CONTEXT_BASE + t16_records(f) + t16_new_slots() + slot; @@ -3916,6 +4081,194 @@ static void sftl_test_16gb_reused_context_block(struct kunit *test) KUNIT_EXPECT_PTR_EQ(test, sftl, NULL); } +static void sftl_test_16gb_moved_context_spare(struct kunit *test) +{ + struct t16_fixture *f = t16_fixture(test); + struct apple_sftl *sftl = NULL; + struct apple_sftl_info info; + u32 i; + + KUNIT_ASSERT_NOT_NULL(test, f); + f->remnant = true; + f->remnant_reused = true; + f->remnant_moved = true; + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + apple_sftl_get_info(sftl, &info); + KUNIT_EXPECT_EQ(test, info.journal_sequence, t16_last(f)); + for (i = 0; i < 4; i++) + t16_expect_page(test, sftl, T16_NEXT_LBA + i, 'x', T16_NEXT_LBA + i); + apple_sftl_free(sftl); +} + +/* One open block holds flag-1 pages the checkpoint covers, then moves above + * it with older user epochs. The block loads and the moves are replayed. + */ +static void sftl_test_16gb_moved_straddle(struct kunit *test) +{ + struct t16_fixture *f = t16_fixture(test); + struct apple_sftl *sftl = NULL; + struct apple_sftl_info info; + u32 i; + + KUNIT_ASSERT_NOT_NULL(test, f); + f->moved_pre = 2; + f->moved_slots = 4; + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + apple_sftl_get_info(sftl, &info); + KUNIT_EXPECT_EQ(test, info.journal_sequence, t16_last(f) + 4); + for (i = 0; i < 4; i++) { + t16_expect_page(test, sftl, i, 'g', i); + t16_expect_page(test, sftl, T16_NEXT_LBA + i, 'x', T16_NEXT_LBA + i); + } + apple_sftl_free(sftl); +} + +static void sftl_test_16gb_moved_after_checkpoint(struct kunit *test) +{ + struct t16_fixture *f = t16_fixture(test); + struct apple_sftl *sftl = NULL; + struct apple_sftl_info info; + u32 i; + + KUNIT_ASSERT_NOT_NULL(test, f); + f->moved_slots = 8; + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + apple_sftl_get_info(sftl, &info); + KUNIT_EXPECT_TRUE(test, info.read_only); + KUNIT_EXPECT_EQ(test, info.journal_sequence, t16_last(f) + 8); + /* The newer move wins over the summary even with an older user epoch. */ + for (i = 0; i < 8; i++) + t16_expect_page(test, sftl, i, 'g', i); + for (i = 0; i < 4; i++) + t16_expect_page(test, sftl, T16_NEXT_LBA + i, 'x', T16_NEXT_LBA + i); + apple_sftl_free(sftl); + + /* Valid moved records still cannot bridge missing sequences. */ + f->new_block = false; + KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); + f->new_block = true; + + /* Future epochs and old-epoch keys outside the disk are not moves. */ + f->moved_future = true; + KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); + f->moved_future = false; + f->moved_key = T16_DISK; + KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); + + /* A single bad page at the end is still torn; its data is not mapped. */ + f->moved_key = 0; + f->moved_future = true; + f->moved_slots = 4; + f->moved_last = true; + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + t16_expect_page(test, sftl, 0, 'o', 0); + t16_expect_page(test, sftl, T16_NEW_LBA, 'n', T16_NEW_LBA); + apple_sftl_free(sftl); +} + +/* iOS 9 writes a TRIM as type 2, flag 1, and keeps writing after it in the + * same open block. Seen on a 16 GB P105: such a record 79 or 95 sequences + * below the checkpoint, then ordinary records above it. It is not a torn page. + */ +static void sftl_test_16gb_trim_flag1(struct kunit *test) +{ + struct t16_fixture *f = t16_fixture(test); + struct apple_sftl *sftl = NULL; + struct apple_sftl_info info; + u64 base; + u32 i; + + KUNIT_ASSERT_NOT_NULL(test, f); + base = T16_CONTEXT_BASE + t16_records(f) + t16_new_slots(); + + /* Covered TRIM page, then ordinary records: loads, nothing is unmapped. */ + f->trim_pre = 4; + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + apple_sftl_get_info(sftl, &info); + KUNIT_EXPECT_EQ(test, info.journal_sequence, base + 4 - 1); + t16_expect_page(test, sftl, 0, 'o', 0); + for (i = 0; i < 4; i++) + t16_expect_page(test, sftl, T16_NEXT_LBA + i, 'x', T16_NEXT_LBA + i); + apple_sftl_free(sftl); + + /* Above the checkpoint the sequence is consumed, preserving both LBAs. */ + f->trim_pre = 0; + f->trim_slots = 4; + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + apple_sftl_get_info(sftl, &info); + KUNIT_EXPECT_EQ(test, info.journal_sequence, base + 8 - 1); + t16_expect_page(test, sftl, 0, 'o', 0); + t16_expect_page(test, sftl, 1, 'o' + 1, 1); + t16_expect_page(test, sftl, 2, 'o' + 2, 2); + for (i = 0; i < 4; i++) + t16_expect_page(test, sftl, T16_NEXT_LBA + i, 'x', T16_NEXT_LBA + i); + apple_sftl_free(sftl); + + /* Another tag with a hidden key, or a TRIM with a bad epoch, is a torn + * page; valid pages after it make the medium inconsistent. + */ + f->trim_slots = 0; + f->trim_pre = 4; + f->trim_epoch_skew = 1; + KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); + f->trim_epoch_skew = -1; + KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); + f->trim_epoch_skew = 0; + f->trim_key = 0xffff0002; + KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); + f->trim_key = 0xffff0000; + KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); + f->trim_key = 0; + f->trim_flag = 2; + KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); + f->trim_flag = 0; + + /* The same page as the last one of the block is a torn tail. */ + f->trim_last = true; + f->trim_key = 0xffff0002; + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + t16_expect_page(test, sftl, 0, 'o', 0); + apple_sftl_free(sftl); + f->trim_key = 0; + f->trim_epoch_skew = 1; + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + apple_sftl_free(sftl); + + /* The payload is ignored, even if it cannot be a flag-0 payload. */ + f->trim_epoch_skew = 0; + f->trim_last = false; + f->trim_pre = 0; + f->trim_slots = 4; + f->trim_bad_count = true; + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + t16_expect_page(test, sftl, 0, 'o', 0); + apple_sftl_free(sftl); +} + +static void sftl_test_16gb_closed_trim_flag1(struct kunit *test) +{ + struct t16_fixture *f = t16_fixture(test); + struct apple_sftl *sftl = NULL; + struct apple_sftl_info info; + + KUNIT_ASSERT_NOT_NULL(test, f); + f->closed_trim = true; + f->next_block = true; + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + apple_sftl_get_info(sftl, &info); + KUNIT_EXPECT_EQ(test, info.journal_sequence, t16_last(f)); + t16_expect_page(test, sftl, 0, 'o', 0); + t16_expect_page(test, sftl, 1, 'o' + 1, 1); + t16_expect_page(test, sftl, T16_NEXT_LBA, 'x', T16_NEXT_LBA); + apple_sftl_free(sftl); + + f->trim_flag = 2; + KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); + f->trim_flag = 0; + f->trim_epoch_skew = 1; + KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); +} + /* iOS 9 writes the hidden tail, past the exported disk, as type 2 with * flag 1. Several such pages before data in an open block are not torn * pages: the hidden pages are mapped and the exported disk is unaffected. @@ -4065,6 +4418,8 @@ static struct kunit_case apple_sftl_cases[] = { KUNIT_CASE(sftl_test_checkpointed_open_copies), KUNIT_CASE(sftl_test_torn_page_not_last), KUNIT_CASE(sftl_test_hidden_tail_is_torn_on_64gb), + KUNIT_CASE(sftl_test_trim_flag1_is_torn_on_64gb), + KUNIT_CASE(sftl_test_closed_trim_flag1_is_torn_on_64gb), KUNIT_CASE(sftl_test_append_scans_entire_candidate), KUNIT_CASE(sftl_test_append_reserves_vfl_members), KUNIT_CASE(sftl_test_append_closes_at_sequence_limit), @@ -4093,6 +4448,11 @@ static struct kunit_case apple_sftl_cases[] = { KUNIT_CASE(sftl_test_16gb_native_summary), KUNIT_CASE(sftl_test_16gb_context_remnant), KUNIT_CASE(sftl_test_16gb_reused_context_block), + KUNIT_CASE(sftl_test_16gb_moved_context_spare), + KUNIT_CASE(sftl_test_16gb_moved_after_checkpoint), + KUNIT_CASE(sftl_test_16gb_moved_straddle), + KUNIT_CASE(sftl_test_16gb_trim_flag1), + KUNIT_CASE(sftl_test_16gb_closed_trim_flag1), KUNIT_CASE(sftl_test_16gb_hidden_tail), {} };