2023-01-25 20:00:44 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2001-2003 Sistina Software (UK) Limited.
|
|
|
|
*
|
|
|
|
* This file is released under the GPL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "dm.h"
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/blkdev.h>
|
|
|
|
#include <linux/bio.h>
|
2017-04-12 20:37:44 +00:00
|
|
|
#include <linux/dax.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/slab.h>
|
2008-10-21 16:44:59 +00:00
|
|
|
#include <linux/device-mapper.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-06-26 07:27:35 +00:00
|
|
|
#define DM_MSG_PREFIX "linear"
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Linear: maps a linear range of a device.
|
|
|
|
*/
|
|
|
|
struct linear_c {
|
|
|
|
struct dm_dev *dev;
|
|
|
|
sector_t start;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Construct a linear mapping: <dev_path> <offset>
|
|
|
|
*/
|
|
|
|
static int linear_ctr(struct dm_target *ti, unsigned int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct linear_c *lc;
|
2006-03-27 09:17:48 +00:00
|
|
|
unsigned long long tmp;
|
dm: reject trailing characters in sccanf input
Device mapper uses sscanf to convert arguments to numbers. The problem is that
the way we use it ignores additional unmatched characters in the scanned string.
For example, this `if (sscanf(string, "%d", &number) == 1)' will match a number,
but also it will match number with some garbage appended, like "123abc".
As a result, device mapper accepts garbage after some numbers. For example
the command `dmsetup create vg1-new --table "0 16384 linear 254:1bla 34816bla"'
will pass without an error.
This patch fixes all sscanf uses in device mapper. It appends "%c" with
a pointer to a dummy character variable to every sscanf statement.
The construct `if (sscanf(string, "%d%c", &number, &dummy) == 1)' succeeds
only if string is a null-terminated number (optionally preceded by some
whitespace characters). If there is some character appended after the number,
sscanf matches "%c", writes the character to the dummy variable and returns 2.
We check the return value for 1 and consequently reject numbers with some
garbage appended.
Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
Acked-by: Mike Snitzer <snitzer@redhat.com>
Signed-off-by: Alasdair G Kergon <agk@redhat.com>
2012-03-28 17:41:26 +00:00
|
|
|
char dummy;
|
2015-07-31 13:20:36 +00:00
|
|
|
int ret;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (argc != 2) {
|
2006-06-26 07:27:35 +00:00
|
|
|
ti->error = "Invalid argument count";
|
2005-04-16 22:20:36 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
lc = kmalloc(sizeof(*lc), GFP_KERNEL);
|
|
|
|
if (lc == NULL) {
|
2015-10-27 19:38:58 +00:00
|
|
|
ti->error = "Cannot allocate linear context";
|
2005-04-16 22:20:36 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2015-07-31 13:20:36 +00:00
|
|
|
ret = -EINVAL;
|
2018-11-07 21:24:55 +00:00
|
|
|
if (sscanf(argv[1], "%llu%c", &tmp, &dummy) != 1 || tmp != (sector_t)tmp) {
|
2015-10-27 19:38:58 +00:00
|
|
|
ti->error = "Invalid device sector";
|
2005-04-16 22:20:36 +00:00
|
|
|
goto bad;
|
|
|
|
}
|
2006-03-27 09:17:48 +00:00
|
|
|
lc->start = tmp;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2015-07-31 13:20:36 +00:00
|
|
|
ret = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &lc->dev);
|
|
|
|
if (ret) {
|
2015-10-27 19:38:58 +00:00
|
|
|
ti->error = "Device lookup failed";
|
2005-04-16 22:20:36 +00:00
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
2013-03-01 22:45:47 +00:00
|
|
|
ti->num_flush_bios = 1;
|
|
|
|
ti->num_discard_bios = 1;
|
2018-03-13 09:23:45 +00:00
|
|
|
ti->num_secure_erase_bios = 1;
|
2017-04-05 17:21:05 +00:00
|
|
|
ti->num_write_zeroes_bios = 1;
|
2024-05-28 11:32:34 +00:00
|
|
|
ti->flush_bypasses_map = true;
|
2005-04-16 22:20:36 +00:00
|
|
|
ti->private = lc;
|
|
|
|
return 0;
|
|
|
|
|
2023-02-07 20:02:51 +00:00
|
|
|
bad:
|
2005-04-16 22:20:36 +00:00
|
|
|
kfree(lc);
|
2015-07-31 13:20:36 +00:00
|
|
|
return ret;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void linear_dtr(struct dm_target *ti)
|
|
|
|
{
|
2023-03-17 01:35:54 +00:00
|
|
|
struct linear_c *lc = ti->private;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
dm_put_device(ti, lc->dev);
|
|
|
|
kfree(lc);
|
|
|
|
}
|
|
|
|
|
2008-07-21 11:00:38 +00:00
|
|
|
static sector_t linear_map_sector(struct dm_target *ti, sector_t bi_sector)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-07-21 11:00:38 +00:00
|
|
|
struct linear_c *lc = ti->private;
|
|
|
|
|
2010-08-12 03:14:11 +00:00
|
|
|
return lc->start + dm_target_offset(ti, bi_sector);
|
2008-07-21 11:00:38 +00:00
|
|
|
}
|
|
|
|
|
2023-09-18 15:33:29 +00:00
|
|
|
int linear_map(struct dm_target *ti, struct bio *bio)
|
2008-07-21 11:00:38 +00:00
|
|
|
{
|
|
|
|
struct linear_c *lc = ti->private;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2017-08-23 17:10:32 +00:00
|
|
|
bio_set_dev(bio, lc->dev->bdev);
|
2022-03-30 17:52:10 +00:00
|
|
|
bio->bi_iter.bi_sector = linear_map_sector(ti, bio->bi_iter.bi_sector);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-12-08 10:41:06 +00:00
|
|
|
return DM_MAPIO_REMAPPED;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2013-03-01 22:45:44 +00:00
|
|
|
static void linear_status(struct dm_target *ti, status_type_t type,
|
2023-01-25 20:14:58 +00:00
|
|
|
unsigned int status_flags, char *result, unsigned int maxlen)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2023-03-17 01:35:54 +00:00
|
|
|
struct linear_c *lc = ti->private;
|
2021-07-13 00:49:03 +00:00
|
|
|
size_t sz = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case STATUSTYPE_INFO:
|
|
|
|
result[0] = '\0';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATUSTYPE_TABLE:
|
2021-07-13 00:49:03 +00:00
|
|
|
DMEMIT("%s %llu", lc->dev->name, (unsigned long long)lc->start);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATUSTYPE_IMA:
|
|
|
|
DMEMIT_TARGET_NAME_VERSION(ti->type);
|
|
|
|
DMEMIT(",device_name=%s,start=%llu;", lc->dev->name,
|
|
|
|
(unsigned long long)lc->start);
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-03 20:54:10 +00:00
|
|
|
static int linear_prepare_ioctl(struct dm_target *ti, struct block_device **bdev)
|
2006-10-03 08:15:18 +00:00
|
|
|
{
|
2023-03-17 01:35:54 +00:00
|
|
|
struct linear_c *lc = ti->private;
|
2012-01-12 15:01:29 +00:00
|
|
|
struct dm_dev *dev = lc->dev;
|
2015-10-15 12:10:50 +00:00
|
|
|
|
|
|
|
*bdev = dev->bdev;
|
2012-01-12 15:01:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Only pass ioctls through if the device sizes match exactly.
|
|
|
|
*/
|
2021-10-18 10:11:05 +00:00
|
|
|
if (lc->start || ti->len != bdev_nr_sectors(dev->bdev))
|
2015-10-15 12:10:50 +00:00
|
|
|
return 1;
|
|
|
|
return 0;
|
2006-10-03 08:15:18 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 10:08:49 +00:00
|
|
|
#ifdef CONFIG_BLK_DEV_ZONED
|
2019-11-11 02:39:30 +00:00
|
|
|
static int linear_report_zones(struct dm_target *ti,
|
|
|
|
struct dm_report_zones_args *args, unsigned int nr_zones)
|
2018-10-12 10:08:49 +00:00
|
|
|
{
|
2019-11-11 02:39:30 +00:00
|
|
|
struct linear_c *lc = ti->private;
|
2018-10-12 10:08:49 +00:00
|
|
|
|
2021-05-25 21:24:57 +00:00
|
|
|
return dm_report_zones(lc->dev->bdev, lc->start,
|
|
|
|
linear_map_sector(ti, args->next_sector),
|
|
|
|
args, nr_zones);
|
2018-10-12 10:08:49 +00:00
|
|
|
}
|
2021-02-10 22:38:30 +00:00
|
|
|
#else
|
|
|
|
#define linear_report_zones NULL
|
2018-10-12 10:08:49 +00:00
|
|
|
#endif
|
|
|
|
|
2009-06-22 09:12:33 +00:00
|
|
|
static int linear_iterate_devices(struct dm_target *ti,
|
|
|
|
iterate_devices_callout_fn fn, void *data)
|
|
|
|
{
|
|
|
|
struct linear_c *lc = ti->private;
|
|
|
|
|
2009-07-23 19:30:42 +00:00
|
|
|
return fn(ti, lc->dev, lc->start, ti->len, data);
|
2009-06-22 09:12:33 +00:00
|
|
|
}
|
|
|
|
|
2021-11-29 10:21:36 +00:00
|
|
|
#if IS_ENABLED(CONFIG_FS_DAX)
|
2021-11-29 10:21:43 +00:00
|
|
|
static struct dax_device *linear_dax_pgoff(struct dm_target *ti, pgoff_t *pgoff)
|
|
|
|
{
|
|
|
|
struct linear_c *lc = ti->private;
|
|
|
|
sector_t sector = linear_map_sector(ti, *pgoff << PAGE_SECTORS_SHIFT);
|
|
|
|
|
|
|
|
*pgoff = (get_start_sect(lc->dev->bdev) + sector) >> PAGE_SECTORS_SHIFT;
|
|
|
|
return lc->dev->dax_dev;
|
|
|
|
}
|
|
|
|
|
2017-04-12 20:37:44 +00:00
|
|
|
static long linear_dax_direct_access(struct dm_target *ti, pgoff_t pgoff,
|
2022-05-13 22:10:58 +00:00
|
|
|
long nr_pages, enum dax_access_mode mode, void **kaddr,
|
|
|
|
pfn_t *pfn)
|
2016-06-22 23:54:54 +00:00
|
|
|
{
|
2021-11-29 10:21:43 +00:00
|
|
|
struct dax_device *dax_dev = linear_dax_pgoff(ti, &pgoff);
|
|
|
|
|
2022-05-13 22:10:58 +00:00
|
|
|
return dax_direct_access(dax_dev, pgoff, nr_pages, mode, kaddr, pfn);
|
2016-06-22 23:54:54 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 16:34:54 +00:00
|
|
|
static int linear_dax_zero_page_range(struct dm_target *ti, pgoff_t pgoff,
|
|
|
|
size_t nr_pages)
|
|
|
|
{
|
2021-11-29 10:21:43 +00:00
|
|
|
struct dax_device *dax_dev = linear_dax_pgoff(ti, &pgoff);
|
|
|
|
|
2020-02-28 16:34:54 +00:00
|
|
|
return dax_zero_page_range(dax_dev, pgoff, nr_pages);
|
|
|
|
}
|
|
|
|
|
2022-04-22 22:45:06 +00:00
|
|
|
static size_t linear_dax_recovery_write(struct dm_target *ti, pgoff_t pgoff,
|
|
|
|
void *addr, size_t bytes, struct iov_iter *i)
|
|
|
|
{
|
|
|
|
struct dax_device *dax_dev = linear_dax_pgoff(ti, &pgoff);
|
|
|
|
|
|
|
|
return dax_recovery_write(dax_dev, pgoff, addr, bytes, i);
|
|
|
|
}
|
|
|
|
|
2018-03-30 00:22:13 +00:00
|
|
|
#else
|
|
|
|
#define linear_dax_direct_access NULL
|
2020-02-28 16:34:54 +00:00
|
|
|
#define linear_dax_zero_page_range NULL
|
2022-04-22 22:45:06 +00:00
|
|
|
#define linear_dax_recovery_write NULL
|
2018-03-30 00:22:13 +00:00
|
|
|
#endif
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
static struct target_type linear_target = {
|
|
|
|
.name = "linear",
|
2017-05-08 23:40:50 +00:00
|
|
|
.version = {1, 4, 0},
|
2020-09-23 20:06:52 +00:00
|
|
|
.features = DM_TARGET_PASSES_INTEGRITY | DM_TARGET_NOWAIT |
|
2021-02-01 05:10:19 +00:00
|
|
|
DM_TARGET_ZONED_HM | DM_TARGET_PASSES_CRYPTO,
|
2018-10-12 10:08:49 +00:00
|
|
|
.report_zones = linear_report_zones,
|
2005-04-16 22:20:36 +00:00
|
|
|
.module = THIS_MODULE,
|
|
|
|
.ctr = linear_ctr,
|
|
|
|
.dtr = linear_dtr,
|
|
|
|
.map = linear_map,
|
|
|
|
.status = linear_status,
|
2015-10-15 12:10:50 +00:00
|
|
|
.prepare_ioctl = linear_prepare_ioctl,
|
2009-06-22 09:12:33 +00:00
|
|
|
.iterate_devices = linear_iterate_devices,
|
2017-04-12 20:37:44 +00:00
|
|
|
.direct_access = linear_dax_direct_access,
|
2020-02-28 16:34:54 +00:00
|
|
|
.dax_zero_page_range = linear_dax_zero_page_range,
|
2022-04-22 22:45:06 +00:00
|
|
|
.dax_recovery_write = linear_dax_recovery_write,
|
2005-04-16 22:20:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
int __init dm_linear_init(void)
|
|
|
|
{
|
|
|
|
int r = dm_register_target(&linear_target);
|
|
|
|
|
|
|
|
if (r < 0)
|
2006-06-26 07:27:35 +00:00
|
|
|
DMERR("register failed %d", r);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dm_linear_exit(void)
|
|
|
|
{
|
2009-01-06 03:04:58 +00:00
|
|
|
dm_unregister_target(&linear_target);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|