18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Copyright (C) 2017 Intel Corporation. 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * This file is released under the GPL. 58c2ecf20Sopenharmony_ci */ 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci#include "dm.h" 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include <linux/module.h> 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_cistruct unstripe_c { 128c2ecf20Sopenharmony_ci struct dm_dev *dev; 138c2ecf20Sopenharmony_ci sector_t physical_start; 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci uint32_t stripes; 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci uint32_t unstripe; 188c2ecf20Sopenharmony_ci sector_t unstripe_width; 198c2ecf20Sopenharmony_ci sector_t unstripe_offset; 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci uint32_t chunk_size; 228c2ecf20Sopenharmony_ci u8 chunk_shift; 238c2ecf20Sopenharmony_ci}; 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci#define DM_MSG_PREFIX "unstriped" 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_cistatic void cleanup_unstripe(struct unstripe_c *uc, struct dm_target *ti) 288c2ecf20Sopenharmony_ci{ 298c2ecf20Sopenharmony_ci if (uc->dev) 308c2ecf20Sopenharmony_ci dm_put_device(ti, uc->dev); 318c2ecf20Sopenharmony_ci kfree(uc); 328c2ecf20Sopenharmony_ci} 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci/* 358c2ecf20Sopenharmony_ci * Contruct an unstriped mapping. 368c2ecf20Sopenharmony_ci * <number of stripes> <chunk size> <stripe #> <dev_path> <offset> 378c2ecf20Sopenharmony_ci */ 388c2ecf20Sopenharmony_cistatic int unstripe_ctr(struct dm_target *ti, unsigned int argc, char **argv) 398c2ecf20Sopenharmony_ci{ 408c2ecf20Sopenharmony_ci struct unstripe_c *uc; 418c2ecf20Sopenharmony_ci sector_t tmp_len; 428c2ecf20Sopenharmony_ci unsigned long long start; 438c2ecf20Sopenharmony_ci char dummy; 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci if (argc != 5) { 468c2ecf20Sopenharmony_ci ti->error = "Invalid number of arguments"; 478c2ecf20Sopenharmony_ci return -EINVAL; 488c2ecf20Sopenharmony_ci } 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci uc = kzalloc(sizeof(*uc), GFP_KERNEL); 518c2ecf20Sopenharmony_ci if (!uc) { 528c2ecf20Sopenharmony_ci ti->error = "Memory allocation for unstriped context failed"; 538c2ecf20Sopenharmony_ci return -ENOMEM; 548c2ecf20Sopenharmony_ci } 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci if (kstrtouint(argv[0], 10, &uc->stripes) || !uc->stripes) { 578c2ecf20Sopenharmony_ci ti->error = "Invalid stripe count"; 588c2ecf20Sopenharmony_ci goto err; 598c2ecf20Sopenharmony_ci } 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci if (kstrtouint(argv[1], 10, &uc->chunk_size) || !uc->chunk_size) { 628c2ecf20Sopenharmony_ci ti->error = "Invalid chunk_size"; 638c2ecf20Sopenharmony_ci goto err; 648c2ecf20Sopenharmony_ci } 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci if (kstrtouint(argv[2], 10, &uc->unstripe)) { 678c2ecf20Sopenharmony_ci ti->error = "Invalid stripe number"; 688c2ecf20Sopenharmony_ci goto err; 698c2ecf20Sopenharmony_ci } 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci if (uc->unstripe > uc->stripes && uc->stripes > 1) { 728c2ecf20Sopenharmony_ci ti->error = "Please provide stripe between [0, # of stripes]"; 738c2ecf20Sopenharmony_ci goto err; 748c2ecf20Sopenharmony_ci } 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci if (dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &uc->dev)) { 778c2ecf20Sopenharmony_ci ti->error = "Couldn't get striped device"; 788c2ecf20Sopenharmony_ci goto err; 798c2ecf20Sopenharmony_ci } 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci if (sscanf(argv[4], "%llu%c", &start, &dummy) != 1 || start != (sector_t)start) { 828c2ecf20Sopenharmony_ci ti->error = "Invalid striped device offset"; 838c2ecf20Sopenharmony_ci goto err; 848c2ecf20Sopenharmony_ci } 858c2ecf20Sopenharmony_ci uc->physical_start = start; 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci uc->unstripe_offset = uc->unstripe * uc->chunk_size; 888c2ecf20Sopenharmony_ci uc->unstripe_width = (uc->stripes - 1) * uc->chunk_size; 898c2ecf20Sopenharmony_ci uc->chunk_shift = is_power_of_2(uc->chunk_size) ? fls(uc->chunk_size) - 1 : 0; 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci tmp_len = ti->len; 928c2ecf20Sopenharmony_ci if (sector_div(tmp_len, uc->chunk_size)) { 938c2ecf20Sopenharmony_ci ti->error = "Target length not divisible by chunk size"; 948c2ecf20Sopenharmony_ci goto err; 958c2ecf20Sopenharmony_ci } 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci if (dm_set_target_max_io_len(ti, uc->chunk_size)) { 988c2ecf20Sopenharmony_ci ti->error = "Failed to set max io len"; 998c2ecf20Sopenharmony_ci goto err; 1008c2ecf20Sopenharmony_ci } 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci ti->private = uc; 1038c2ecf20Sopenharmony_ci return 0; 1048c2ecf20Sopenharmony_cierr: 1058c2ecf20Sopenharmony_ci cleanup_unstripe(uc, ti); 1068c2ecf20Sopenharmony_ci return -EINVAL; 1078c2ecf20Sopenharmony_ci} 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_cistatic void unstripe_dtr(struct dm_target *ti) 1108c2ecf20Sopenharmony_ci{ 1118c2ecf20Sopenharmony_ci struct unstripe_c *uc = ti->private; 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci cleanup_unstripe(uc, ti); 1148c2ecf20Sopenharmony_ci} 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_cistatic sector_t map_to_core(struct dm_target *ti, struct bio *bio) 1178c2ecf20Sopenharmony_ci{ 1188c2ecf20Sopenharmony_ci struct unstripe_c *uc = ti->private; 1198c2ecf20Sopenharmony_ci sector_t sector = bio->bi_iter.bi_sector; 1208c2ecf20Sopenharmony_ci sector_t tmp_sector = sector; 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci /* Shift us up to the right "row" on the stripe */ 1238c2ecf20Sopenharmony_ci if (uc->chunk_shift) 1248c2ecf20Sopenharmony_ci tmp_sector >>= uc->chunk_shift; 1258c2ecf20Sopenharmony_ci else 1268c2ecf20Sopenharmony_ci sector_div(tmp_sector, uc->chunk_size); 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci sector += uc->unstripe_width * tmp_sector; 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci /* Account for what stripe we're operating on */ 1318c2ecf20Sopenharmony_ci return sector + uc->unstripe_offset; 1328c2ecf20Sopenharmony_ci} 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_cistatic int unstripe_map(struct dm_target *ti, struct bio *bio) 1358c2ecf20Sopenharmony_ci{ 1368c2ecf20Sopenharmony_ci struct unstripe_c *uc = ti->private; 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci bio_set_dev(bio, uc->dev->bdev); 1398c2ecf20Sopenharmony_ci bio->bi_iter.bi_sector = map_to_core(ti, bio) + uc->physical_start; 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci return DM_MAPIO_REMAPPED; 1428c2ecf20Sopenharmony_ci} 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_cistatic void unstripe_status(struct dm_target *ti, status_type_t type, 1458c2ecf20Sopenharmony_ci unsigned int status_flags, char *result, unsigned int maxlen) 1468c2ecf20Sopenharmony_ci{ 1478c2ecf20Sopenharmony_ci struct unstripe_c *uc = ti->private; 1488c2ecf20Sopenharmony_ci unsigned int sz = 0; 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci switch (type) { 1518c2ecf20Sopenharmony_ci case STATUSTYPE_INFO: 1528c2ecf20Sopenharmony_ci break; 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci case STATUSTYPE_TABLE: 1558c2ecf20Sopenharmony_ci DMEMIT("%d %llu %d %s %llu", 1568c2ecf20Sopenharmony_ci uc->stripes, (unsigned long long)uc->chunk_size, uc->unstripe, 1578c2ecf20Sopenharmony_ci uc->dev->name, (unsigned long long)uc->physical_start); 1588c2ecf20Sopenharmony_ci break; 1598c2ecf20Sopenharmony_ci } 1608c2ecf20Sopenharmony_ci} 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_cistatic int unstripe_iterate_devices(struct dm_target *ti, 1638c2ecf20Sopenharmony_ci iterate_devices_callout_fn fn, void *data) 1648c2ecf20Sopenharmony_ci{ 1658c2ecf20Sopenharmony_ci struct unstripe_c *uc = ti->private; 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci return fn(ti, uc->dev, uc->physical_start, ti->len, data); 1688c2ecf20Sopenharmony_ci} 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_cistatic void unstripe_io_hints(struct dm_target *ti, 1718c2ecf20Sopenharmony_ci struct queue_limits *limits) 1728c2ecf20Sopenharmony_ci{ 1738c2ecf20Sopenharmony_ci struct unstripe_c *uc = ti->private; 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci limits->chunk_sectors = uc->chunk_size; 1768c2ecf20Sopenharmony_ci} 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_cistatic struct target_type unstripe_target = { 1798c2ecf20Sopenharmony_ci .name = "unstriped", 1808c2ecf20Sopenharmony_ci .version = {1, 1, 0}, 1818c2ecf20Sopenharmony_ci .module = THIS_MODULE, 1828c2ecf20Sopenharmony_ci .ctr = unstripe_ctr, 1838c2ecf20Sopenharmony_ci .dtr = unstripe_dtr, 1848c2ecf20Sopenharmony_ci .map = unstripe_map, 1858c2ecf20Sopenharmony_ci .status = unstripe_status, 1868c2ecf20Sopenharmony_ci .iterate_devices = unstripe_iterate_devices, 1878c2ecf20Sopenharmony_ci .io_hints = unstripe_io_hints, 1888c2ecf20Sopenharmony_ci}; 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_cistatic int __init dm_unstripe_init(void) 1918c2ecf20Sopenharmony_ci{ 1928c2ecf20Sopenharmony_ci return dm_register_target(&unstripe_target); 1938c2ecf20Sopenharmony_ci} 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_cistatic void __exit dm_unstripe_exit(void) 1968c2ecf20Sopenharmony_ci{ 1978c2ecf20Sopenharmony_ci dm_unregister_target(&unstripe_target); 1988c2ecf20Sopenharmony_ci} 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_cimodule_init(dm_unstripe_init); 2018c2ecf20Sopenharmony_cimodule_exit(dm_unstripe_exit); 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_ciMODULE_DESCRIPTION(DM_NAME " unstriped target"); 2048c2ecf20Sopenharmony_ciMODULE_ALIAS("dm-unstriped"); 2058c2ecf20Sopenharmony_ciMODULE_AUTHOR("Scott Bauer <scott.bauer@intel.com>"); 2068c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 207