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
12 changes: 12 additions & 0 deletions Documentation/driver-api/mtd/apple-sftl.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ profile 3,906,266 and 3,906,250 entries, or 16,000,000,000 bytes. The disk has
is ``applesftlN``. LwVM and filesystem interpretation happen above this
logical provider. Encrypted partition contents remain ciphertext.

The LwVM partition parser adds a partition to ``applesftlN`` only when its
chunks are stored in order and back to back. iOS often scatters a partition
over the disk. For those, the frontend adds a second, read-only disk,
``applesftlNlwvm``, of the same size: its first page is a copy of the LwVM
header that lists only the scattered partitions, each stored in order from
chunk 1, and every other chunk of it is read through the chunk map from the
main disk or as zeroes. The partition parser then adds them as
``applesftlNlwvmP`` with their LwVM number, PARTUUID and name, so
``root=PARTUUID=`` finds them as it finds any other partition. Partitions
whose map has a hole, a doubled or an out-of-range chunk are reported and
left out. The view has no write path.

Failure and lifetime rules
--------------------------

Expand Down
7 changes: 7 additions & 0 deletions drivers/mtd/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ config MTD_PARTITIONED_MASTER
config MTD_APPLE_SFTL
tristate "Apple PPN VFL/SwissPPNFTL translation"
depends on BLOCK
select CRC32
help
Recover the logical block medium stored in Apple VFL/SwissPPNFTL
metadata on the measured 64 GB and 16 GB Hynix PPN layouts. The
Expand All @@ -224,6 +225,11 @@ config MTD_APPLE_SFTL
validates. Providers without a qualified erase operation return
ENOSPC when their supply of blank blocks runs out.

The LwVM partition parser adds only partitions stored in order on
applesftlN. Those that iOS stored in scattered chunks are added
read-only as partitions of a second disk, applesftlNlwvm, with their
LwVM number, PARTUUID and name.

config MTD_APPLE_SFTL_KUNIT_TEST
tristate "KUnit tests for Apple VFL/SwissPPNFTL" if !KUNIT_ALL_TESTS
depends on KUNIT && MTD_APPLE_SFTL
Expand All @@ -235,6 +241,7 @@ config MTD_APPLE_SFTL_KUNIT_TEST
readback and recovery using synthetic flash.
Exercise native accounting, checkpoint page cuts, interrupted lane
retirement, both replicated VFL banks and fresh-loader replay.
Decode LwVM chunk maps and check the view of scattered partitions.
All test media and provider operations are synthetic.

source "drivers/mtd/chips/Kconfig"
Expand Down
2 changes: 2 additions & 0 deletions drivers/mtd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ obj-$(CONFIG_MTD_HYPERBUS) += hyperbus/

obj-$(CONFIG_MTD_APPLE_SFTL) += apple-sftl.o
apple-sftl-y := apple-sftl-core.o apple-sftl-block.o apple-sftl-reclaim.o apple-sftl-account.o apple-sftl-provider.o apple-sftl-vfl.o
apple-sftl-y += apple-sftl-lwvm.o
obj-$(CONFIG_MTD_APPLE_SFTL_KUNIT_TEST) += apple-sftl-test.o
obj-$(CONFIG_MTD_APPLE_SFTL_KUNIT_TEST) += apple-sftl-reclaim-test.o
obj-$(CONFIG_MTD_APPLE_SFTL_KUNIT_TEST) += apple-sftl-account-test.o apple-sftl-provider-test.o apple-sftl-vfl-test.o
obj-$(CONFIG_MTD_APPLE_SFTL_KUNIT_TEST) += apple-sftl-lwvm-test.o
229 changes: 218 additions & 11 deletions drivers/mtd/apple-sftl-block.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
#include <linux/slab.h>

#include "apple-sftl.h"
#include "apple-sftl-lwvm.h"

#define SFTL_BLOCK_NAME "apple-sftl"
#define SFTL_DISK_MINORS 16
#define SFTL_LOGICAL_BYTES 4096U

struct sftl_view;

struct sftl_disk {
struct apple_sftl *ftl;
struct blk_mq_tag_set tags;
Expand All @@ -27,13 +30,32 @@ struct sftl_disk {
unsigned int id;
bool dead;
bool writable;
struct sftl_view *view;
};

/*
* The LwVM partitions that iOS stored in scattered chunks, which the
* partition parser cannot add to the main disk. The view disk reads the
* parent's pages through the chunk map, under the parent's lock and with
* its buffer. There is no write path.
*/
struct sftl_view {
struct sftl_disk *parent;
struct blk_mq_tag_set tags;
struct gendisk *disk;
struct apple_sftl_lwvm_view map;
};

static DEFINE_IDA(sftl_disk_ids);
static int sftl_major;

/* The blocking queue and this lock keep the preallocated buffer exclusive. */
static int sftl_read_request(struct sftl_disk *sftl, struct request *request)
/*
* The blocking queue and this lock keep the preallocated buffer exclusive.
* The view passes its map: each 4 KiB page is looked up in it, so a request
* can cross chunks in any order.
*/
static int sftl_read_request(struct sftl_disk *sftl, struct request *request,
const struct apple_sftl_lwvm_view *view)
{
struct req_iterator iterator;
struct bio_vec bvec;
Expand All @@ -44,22 +66,37 @@ static int sftl_read_request(struct sftl_disk *sftl, struct request *request)
unsigned int done = 0;

while (done < bvec.bv_len) {
u32 logical = position >> 12;
enum apple_sftl_lwvm_page page = APPLE_SFTL_LWVM_PAGE_DISK;
unsigned int offset = position & (SFTL_LOGICAL_BYTES - 1);
unsigned int length = min(bvec.bv_len - done,
SFTL_LOGICAL_BYTES - offset);
u64 source = position;
const u8 *from;
void *mapped;
u32 logical;
int ret;

if (logical != cached) {
ret = apple_sftl_read(sftl->ftl, logical, sftl->buffer);
if (ret)
return ret;
cached = logical;
if (view)
page = apple_sftl_lwvm_view_map(view, position, &source);
if (page == APPLE_SFTL_LWVM_PAGE_HEADER) {
from = view->header + offset;
} else if (page == APPLE_SFTL_LWVM_PAGE_ZERO) {
from = NULL;
} else {
logical = source >> 12;
if (logical != cached) {
ret = apple_sftl_read(sftl->ftl, logical, sftl->buffer);
if (ret)
return ret;
cached = logical;
}
from = sftl->buffer + offset;
}
mapped = kmap_local_page(bvec.bv_page);
memcpy(mapped + bvec.bv_offset + done,
sftl->buffer + offset, length);
if (from)
memcpy(mapped + bvec.bv_offset + done, from, length);
else
memset(mapped + bvec.bv_offset + done, 0, length);
kunmap_local(mapped);
done += length;
position += length;
Expand Down Expand Up @@ -157,7 +194,7 @@ static blk_status_t sftl_queue_rq(struct blk_mq_hw_ctx *hctx,
} else if (req_op(request) == REQ_OP_WRITE) {
ret = sftl->writable ? sftl_write_request(sftl, request) : -EROFS;
} else {
ret = sftl_read_request(sftl, request);
ret = sftl_read_request(sftl, request, NULL);
}
memalloc_noio_restore(noio_flags);
mutex_unlock(&sftl->io_lock);
Expand All @@ -166,14 +203,182 @@ static blk_status_t sftl_queue_rq(struct blk_mq_hw_ctx *hctx,
return BLK_STS_OK;
}

/* Reads only: anything else ends here and never reaches the FTL. */
static blk_status_t sftl_view_queue_rq(struct blk_mq_hw_ctx *hctx,
const struct blk_mq_queue_data *data)
{
struct request *request = data->rq;
struct sftl_view *view = request->q->queuedata;
struct sftl_disk *sftl = view->parent;
sector_t capacity = get_capacity(view->disk);
unsigned int noio_flags;
int ret;

if (req_op(request) != REQ_OP_READ) {
blk_mq_start_request(request);
ret = req_op(request) == REQ_OP_WRITE ? -EROFS : -EOPNOTSUPP;
blk_mq_end_request(request, errno_to_blk_status(ret));
return BLK_STS_OK;
}
mutex_lock(&sftl->io_lock);
noio_flags = memalloc_noio_save();
blk_mq_start_request(request);
if (sftl->dead)
ret = -ENODEV;
else if (blk_rq_pos(request) > capacity ||
blk_rq_sectors(request) > capacity - blk_rq_pos(request))
ret = -EIO;
else
ret = sftl_read_request(sftl, request, &view->map);
memalloc_noio_restore(noio_flags);
mutex_unlock(&sftl->io_lock);
blk_mq_end_request(request, errno_to_blk_status(ret));
return BLK_STS_OK;
}

static const struct blk_mq_ops sftl_mq_ops = {
.queue_rq = sftl_queue_rq,
};

static const struct blk_mq_ops sftl_view_mq_ops = {
.queue_rq = sftl_view_queue_rq,
};

static const struct block_device_operations sftl_block_ops = {
.owner = THIS_MODULE,
};

/*
* Read the LwVM header (or its copy in the last page of the first chunk if
* it fails its CRC, as the partition parser does) and build the view of
* the partitions the parser skips because they are scattered. NULL when
* there are none or the header is not usable; the parser reports that.
*/
static struct sftl_view *sftl_view_alloc(struct platform_device *pdev,
struct sftl_disk *sftl, u64 media)
{
struct apple_sftl_lwvm *lwvm;
struct sftl_view *view;
unsigned int noio_flags, i;
u8 *header;
int ret = -ENOMEM;

view = kzalloc(sizeof(*view), GFP_KERNEL);
lwvm = kzalloc(sizeof(*lwvm), GFP_KERNEL);
header = kmalloc(SFTL_LOGICAL_BYTES, GFP_KERNEL);
if (!view || !lwvm || !header)
goto out;
mutex_lock(&sftl->io_lock);
noio_flags = memalloc_noio_save();
ret = apple_sftl_read(sftl->ftl, 0, header);
if (!ret)
ret = apple_sftl_lwvm_decode(header, media, lwvm);
if (ret == -EBADMSG) {
u32 copy = (1U << (apple_sftl_lwvm_chunk_shift(media) - 12)) - 1;

ret = apple_sftl_read(sftl->ftl, copy, header);
if (!ret)
ret = apple_sftl_lwvm_decode(header, media, lwvm);
}
memalloc_noio_restore(noio_flags);
mutex_unlock(&sftl->io_lock);
if (ret)
goto out;
/* Empty or oversized entries are skipped silently, as by the parser. */
for (i = 0; i < lwvm->nr_parts; i++)
if (lwvm->parts[i].error && lwvm->parts[i].error != -EINVAL &&
lwvm->parts[i].error != -EFBIG)
dev_warn(&pdev->dev, "LwVM partition %u \"%s\" has a broken chunk map (%d), not added\n",
i + 1, lwvm->parts[i].name, lwvm->parts[i].error);
if (apple_sftl_lwvm_view(header, lwvm, &view->map))
goto out;
ret = -ENOENT;
out:
if (ret && ret != -ENOENT)
dev_warn(&pdev->dev, "LwVM header not read (%d), no scattered partitions\n", ret);
kfree(header);
kfree(lwvm);
if (ret) {
kfree(view);
return NULL;
}
return view;
}

/*
* Give the scattered LwVM partitions a disk of their own, applesftlNlwvm,
* whose partitions keep their LwVM number, PARTUUID and name. It is
* read-only on every medium. On a read-write medium a write through the
* main disk into one of its chunks leaves its page cache stale, as a write
* to a whole disk does for a partition. Every failure here leaves the main
* disk as it is.
*/
static void sftl_add_view(struct platform_device *pdev, struct sftl_disk *sftl,
const struct apple_sftl_info *info)
{
struct queue_limits limits = {
.logical_block_size = SECTOR_SIZE,
.physical_block_size = SFTL_LOGICAL_BYTES,
.max_hw_sectors = 128,
};
u64 media = (u64)info->disk_pages * SFTL_LOGICAL_BYTES;
struct sftl_view *view;
struct gendisk *disk;
int ret;

view = sftl_view_alloc(pdev, sftl, media);
if (!view)
return;
view->parent = sftl;
ret = blk_mq_alloc_sq_tag_set(&view->tags, &sftl_view_mq_ops, 8,
BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_BLOCKING);
if (ret)
goto free_view;
/* Its reads can wait behind a write of the main disk. */
view->tags.timeout = sftl->tags.timeout;
disk = blk_mq_alloc_disk(&view->tags, &limits, view);
if (IS_ERR(disk)) {
ret = PTR_ERR(disk);
goto free_tags;
}
view->disk = disk;
/* No major and no minors: dynamic device numbers for it and its partitions. */
disk->fops = &sftl_block_ops;
disk->private_data = view;
snprintf(disk->disk_name, DISK_NAME_LEN, "applesftl%ulwvm", sftl->id);
set_capacity(disk, media >> SECTOR_SHIFT);
set_disk_ro(disk, true);
ret = device_add_disk(&pdev->dev, disk, NULL);
if (ret)
goto put_disk;
sftl->view = view;
dev_info(&pdev->dev, "%s: %u scattered LwVM partitions, read-only\n",
disk->disk_name, view->map.nr_parts);
return;

put_disk:
put_disk(disk);
free_tags:
blk_mq_free_tag_set(&view->tags);
free_view:
kfree(view);
dev_warn(&pdev->dev, "scattered LwVM partitions not added: %d\n", ret);
}

/* Before the parent: its reads take the parent's lock, buffer and FTL. */
static void sftl_remove_view(struct sftl_disk *sftl)
{
struct sftl_view *view = sftl->view;

if (!view)
return;
del_gendisk(view->disk);
put_disk(view->disk);
blk_mq_free_tag_set(&view->tags);
kfree(view);
sftl->view = NULL;
}

static int sftl_disk_probe(struct platform_device *pdev)
{
struct queue_limits limits = {
Expand Down Expand Up @@ -262,6 +467,7 @@ static int sftl_disk_probe(struct platform_device *pdev)
dev_info(&pdev->dev, "%s: %s, %u logical pages, sequence %llu\n",
disk->disk_name, sftl->writable ? "read-write" : "read-only",
info.disk_pages, info.sequence);
sftl_add_view(pdev, sftl, &info);
return 0;

put_disk:
Expand All @@ -287,6 +493,7 @@ static void sftl_disk_remove(struct platform_device *pdev)
mutex_lock(&sftl->io_lock);
sftl->dead = true;
mutex_unlock(&sftl->io_lock);
sftl_remove_view(sftl);
/* del_gendisk freezes and drains the queue before the provider is freed. */
del_gendisk(sftl->disk);
put_disk(sftl->disk);
Expand Down
Loading
Loading