Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions drivers/mtd/apple-sftl-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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[] = {
Expand Down Expand Up @@ -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;

Expand All @@ -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) &&
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Loading
Loading