2 * Copyright (C) 2017 Intel Corporation.
4 * This file is released under the GPL.
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>
19 sector_t physical_start
;
24 sector_t unstripe_width
;
25 sector_t unstripe_offset
;
31 #define DM_MSG_PREFIX "unstriped"
33 static void cleanup_unstripe(struct unstripe_c
*uc
, struct dm_target
*ti
)
36 dm_put_device(ti
, uc
->dev
);
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
;
48 unsigned long long start
;
52 ti
->error
= "Invalid number of arguments";
56 uc
= kzalloc(sizeof(*uc
), GFP_KERNEL
);
58 ti
->error
= "Memory allocation for unstriped context failed";
62 if (kstrtouint(argv
[0], 10, &uc
->stripes
) || !uc
->stripes
) {
63 ti
->error
= "Invalid stripe count";
67 if (kstrtouint(argv
[1], 10, &uc
->chunk_size
) || !uc
->chunk_size
) {
68 ti
->error
= "Invalid chunk_size";
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";
78 if (kstrtouint(argv
[2], 10, &uc
->unstripe
)) {
79 ti
->error
= "Invalid stripe number";
83 if (uc
->unstripe
> uc
->stripes
&& uc
->stripes
> 1) {
84 ti
->error
= "Please provide stripe between [0, # of stripes]";
88 if (dm_get_device(ti
, argv
[3], dm_table_get_mode(ti
->table
), &uc
->dev
)) {
89 ti
->error
= "Couldn't get striped device";
93 if (sscanf(argv
[4], "%llu%c", &start
, &dummy
) != 1) {
94 ti
->error
= "Invalid striped device offset";
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;
104 if (sector_div(tmp_len
, uc
->chunk_size
)) {
105 ti
->error
= "Target length not divisible by chunk size";
109 if (dm_set_target_max_io_len(ti
, uc
->chunk_size
)) {
110 ti
->error
= "Failed to set max io len";
117 cleanup_unstripe(uc
, ti
);
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
;
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;
159 case STATUSTYPE_INFO
:
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
);
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
= {
188 .version
= {1, 0, 0},
189 .module
= THIS_MODULE
,
193 .status
= unstripe_status
,
194 .iterate_devices
= unstripe_iterate_devices
,
195 .io_hints
= unstripe_io_hints
,
198 static int __init
dm_unstripe_init(void)
202 r
= dm_register_target(&unstripe_target
);
204 DMERR("target registration failed");
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");