Linux 4.16.11
[linux/fpc-iii.git] / drivers / md / dm-unstripe.c
blob65f838fa2e99d2727bba31dd26fd5c715fa3f0c2
1 /*
2 * Copyright (C) 2017 Intel Corporation.
4 * This file is released under the GPL.
5 */
7 #include "dm.h"
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/blkdev.h>
12 #include <linux/bio.h>
13 #include <linux/slab.h>
14 #include <linux/bitops.h>
15 #include <linux/device-mapper.h>
17 struct unstripe_c {
18 struct dm_dev *dev;
19 sector_t physical_start;
21 uint32_t stripes;
23 uint32_t unstripe;
24 sector_t unstripe_width;
25 sector_t unstripe_offset;
27 uint32_t chunk_size;
28 u8 chunk_shift;
31 #define DM_MSG_PREFIX "unstriped"
33 static void cleanup_unstripe(struct unstripe_c *uc, struct dm_target *ti)
35 if (uc->dev)
36 dm_put_device(ti, uc->dev);
37 kfree(uc);
41 * Contruct an unstriped mapping.
42 * <number of stripes> <chunk size> <stripe #> <dev_path> <offset>
44 static int unstripe_ctr(struct dm_target *ti, unsigned int argc, char **argv)
46 struct unstripe_c *uc;
47 sector_t tmp_len;
48 unsigned long long start;
49 char dummy;
51 if (argc != 5) {
52 ti->error = "Invalid number of arguments";
53 return -EINVAL;
56 uc = kzalloc(sizeof(*uc), GFP_KERNEL);
57 if (!uc) {
58 ti->error = "Memory allocation for unstriped context failed";
59 return -ENOMEM;
62 if (kstrtouint(argv[0], 10, &uc->stripes) || !uc->stripes) {
63 ti->error = "Invalid stripe count";
64 goto err;
67 if (kstrtouint(argv[1], 10, &uc->chunk_size) || !uc->chunk_size) {
68 ti->error = "Invalid chunk_size";
69 goto err;
72 // FIXME: must support non power of 2 chunk_size, dm-stripe.c does
73 if (!is_power_of_2(uc->chunk_size)) {
74 ti->error = "Non power of 2 chunk_size is not supported yet";
75 goto err;
78 if (kstrtouint(argv[2], 10, &uc->unstripe)) {
79 ti->error = "Invalid stripe number";
80 goto err;
83 if (uc->unstripe > uc->stripes && uc->stripes > 1) {
84 ti->error = "Please provide stripe between [0, # of stripes]";
85 goto err;
88 if (dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &uc->dev)) {
89 ti->error = "Couldn't get striped device";
90 goto err;
93 if (sscanf(argv[4], "%llu%c", &start, &dummy) != 1) {
94 ti->error = "Invalid striped device offset";
95 goto err;
97 uc->physical_start = start;
99 uc->unstripe_offset = uc->unstripe * uc->chunk_size;
100 uc->unstripe_width = (uc->stripes - 1) * uc->chunk_size;
101 uc->chunk_shift = fls(uc->chunk_size) - 1;
103 tmp_len = ti->len;
104 if (sector_div(tmp_len, uc->chunk_size)) {
105 ti->error = "Target length not divisible by chunk size";
106 goto err;
109 if (dm_set_target_max_io_len(ti, uc->chunk_size)) {
110 ti->error = "Failed to set max io len";
111 goto err;
114 ti->private = uc;
115 return 0;
116 err:
117 cleanup_unstripe(uc, ti);
118 return -EINVAL;
121 static void unstripe_dtr(struct dm_target *ti)
123 struct unstripe_c *uc = ti->private;
125 cleanup_unstripe(uc, ti);
128 static sector_t map_to_core(struct dm_target *ti, struct bio *bio)
130 struct unstripe_c *uc = ti->private;
131 sector_t sector = bio->bi_iter.bi_sector;
133 /* Shift us up to the right "row" on the stripe */
134 sector += uc->unstripe_width * (sector >> uc->chunk_shift);
136 /* Account for what stripe we're operating on */
137 sector += uc->unstripe_offset;
139 return sector;
142 static int unstripe_map(struct dm_target *ti, struct bio *bio)
144 struct unstripe_c *uc = ti->private;
146 bio_set_dev(bio, uc->dev->bdev);
147 bio->bi_iter.bi_sector = map_to_core(ti, bio) + uc->physical_start;
149 return DM_MAPIO_REMAPPED;
152 static void unstripe_status(struct dm_target *ti, status_type_t type,
153 unsigned int status_flags, char *result, unsigned int maxlen)
155 struct unstripe_c *uc = ti->private;
156 unsigned int sz = 0;
158 switch (type) {
159 case STATUSTYPE_INFO:
160 break;
162 case STATUSTYPE_TABLE:
163 DMEMIT("%d %llu %d %s %llu",
164 uc->stripes, (unsigned long long)uc->chunk_size, uc->unstripe,
165 uc->dev->name, (unsigned long long)uc->physical_start);
166 break;
170 static int unstripe_iterate_devices(struct dm_target *ti,
171 iterate_devices_callout_fn fn, void *data)
173 struct unstripe_c *uc = ti->private;
175 return fn(ti, uc->dev, uc->physical_start, ti->len, data);
178 static void unstripe_io_hints(struct dm_target *ti,
179 struct queue_limits *limits)
181 struct unstripe_c *uc = ti->private;
183 limits->chunk_sectors = uc->chunk_size;
186 static struct target_type unstripe_target = {
187 .name = "unstriped",
188 .version = {1, 0, 0},
189 .module = THIS_MODULE,
190 .ctr = unstripe_ctr,
191 .dtr = unstripe_dtr,
192 .map = unstripe_map,
193 .status = unstripe_status,
194 .iterate_devices = unstripe_iterate_devices,
195 .io_hints = unstripe_io_hints,
198 static int __init dm_unstripe_init(void)
200 int r;
202 r = dm_register_target(&unstripe_target);
203 if (r < 0)
204 DMERR("target registration failed");
206 return r;
209 static void __exit dm_unstripe_exit(void)
211 dm_unregister_target(&unstripe_target);
214 module_init(dm_unstripe_init);
215 module_exit(dm_unstripe_exit);
217 MODULE_DESCRIPTION(DM_NAME " unstriped target");
218 MODULE_AUTHOR("Scott Bauer <scott.bauer@intel.com>");
219 MODULE_LICENSE("GPL");