2 * Copyright (C) 2017 Intel Corporation.
4 * This file is released under the GPL.
9 #include <linux/module.h>
13 sector_t physical_start
;
18 sector_t unstripe_width
;
19 sector_t unstripe_offset
;
25 #define DM_MSG_PREFIX "unstriped"
27 static void cleanup_unstripe(struct unstripe_c
*uc
, struct dm_target
*ti
)
30 dm_put_device(ti
, uc
->dev
);
35 * Contruct an unstriped mapping.
36 * <number of stripes> <chunk size> <stripe #> <dev_path> <offset>
38 static int unstripe_ctr(struct dm_target
*ti
, unsigned int argc
, char **argv
)
40 struct unstripe_c
*uc
;
42 unsigned long long start
;
46 ti
->error
= "Invalid number of arguments";
50 uc
= kzalloc(sizeof(*uc
), GFP_KERNEL
);
52 ti
->error
= "Memory allocation for unstriped context failed";
56 if (kstrtouint(argv
[0], 10, &uc
->stripes
) || !uc
->stripes
) {
57 ti
->error
= "Invalid stripe count";
61 if (kstrtouint(argv
[1], 10, &uc
->chunk_size
) || !uc
->chunk_size
) {
62 ti
->error
= "Invalid chunk_size";
66 if (kstrtouint(argv
[2], 10, &uc
->unstripe
)) {
67 ti
->error
= "Invalid stripe number";
71 if (uc
->unstripe
> uc
->stripes
&& uc
->stripes
> 1) {
72 ti
->error
= "Please provide stripe between [0, # of stripes]";
76 if (dm_get_device(ti
, argv
[3], dm_table_get_mode(ti
->table
), &uc
->dev
)) {
77 ti
->error
= "Couldn't get striped device";
81 if (sscanf(argv
[4], "%llu%c", &start
, &dummy
) != 1 || start
!= (sector_t
)start
) {
82 ti
->error
= "Invalid striped device offset";
85 uc
->physical_start
= start
;
87 uc
->unstripe_offset
= uc
->unstripe
* uc
->chunk_size
;
88 uc
->unstripe_width
= (uc
->stripes
- 1) * uc
->chunk_size
;
89 uc
->chunk_shift
= is_power_of_2(uc
->chunk_size
) ? fls(uc
->chunk_size
) - 1 : 0;
92 if (sector_div(tmp_len
, uc
->chunk_size
)) {
93 ti
->error
= "Target length not divisible by chunk size";
97 if (dm_set_target_max_io_len(ti
, uc
->chunk_size
)) {
98 ti
->error
= "Failed to set max io len";
105 cleanup_unstripe(uc
, ti
);
109 static void unstripe_dtr(struct dm_target
*ti
)
111 struct unstripe_c
*uc
= ti
->private;
113 cleanup_unstripe(uc
, ti
);
116 static sector_t
map_to_core(struct dm_target
*ti
, struct bio
*bio
)
118 struct unstripe_c
*uc
= ti
->private;
119 sector_t sector
= bio
->bi_iter
.bi_sector
;
120 sector_t tmp_sector
= sector
;
122 /* Shift us up to the right "row" on the stripe */
124 tmp_sector
>>= uc
->chunk_shift
;
126 sector_div(tmp_sector
, uc
->chunk_size
);
128 sector
+= uc
->unstripe_width
* tmp_sector
;
130 /* Account for what stripe we're operating on */
131 return sector
+ uc
->unstripe_offset
;
134 static int unstripe_map(struct dm_target
*ti
, struct bio
*bio
)
136 struct unstripe_c
*uc
= ti
->private;
138 bio_set_dev(bio
, uc
->dev
->bdev
);
139 bio
->bi_iter
.bi_sector
= map_to_core(ti
, bio
) + uc
->physical_start
;
141 return DM_MAPIO_REMAPPED
;
144 static void unstripe_status(struct dm_target
*ti
, status_type_t type
,
145 unsigned int status_flags
, char *result
, unsigned int maxlen
)
147 struct unstripe_c
*uc
= ti
->private;
151 case STATUSTYPE_INFO
:
154 case STATUSTYPE_TABLE
:
155 DMEMIT("%d %llu %d %s %llu",
156 uc
->stripes
, (unsigned long long)uc
->chunk_size
, uc
->unstripe
,
157 uc
->dev
->name
, (unsigned long long)uc
->physical_start
);
162 static int unstripe_iterate_devices(struct dm_target
*ti
,
163 iterate_devices_callout_fn fn
, void *data
)
165 struct unstripe_c
*uc
= ti
->private;
167 return fn(ti
, uc
->dev
, uc
->physical_start
, ti
->len
, data
);
170 static void unstripe_io_hints(struct dm_target
*ti
,
171 struct queue_limits
*limits
)
173 struct unstripe_c
*uc
= ti
->private;
175 limits
->chunk_sectors
= uc
->chunk_size
;
178 static struct target_type unstripe_target
= {
180 .version
= {1, 1, 0},
181 .module
= THIS_MODULE
,
185 .status
= unstripe_status
,
186 .iterate_devices
= unstripe_iterate_devices
,
187 .io_hints
= unstripe_io_hints
,
190 static int __init
dm_unstripe_init(void)
192 return dm_register_target(&unstripe_target
);
195 static void __exit
dm_unstripe_exit(void)
197 dm_unregister_target(&unstripe_target
);
200 module_init(dm_unstripe_init
);
201 module_exit(dm_unstripe_exit
);
203 MODULE_DESCRIPTION(DM_NAME
" unstriped target");
204 MODULE_ALIAS("dm-unstriped");
205 MODULE_AUTHOR("Scott Bauer <scott.bauer@intel.com>");
206 MODULE_LICENSE("GPL");