1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2017 Intel Corporation.
5 * This file is released under the GPL.
10 #include <linux/module.h>
14 sector_t physical_start
;
19 sector_t unstripe_width
;
20 sector_t unstripe_offset
;
26 #define DM_MSG_PREFIX "unstriped"
28 static void cleanup_unstripe(struct unstripe_c
*uc
, struct dm_target
*ti
)
31 dm_put_device(ti
, uc
->dev
);
36 * Contruct an unstriped mapping.
37 * <number of stripes> <chunk size> <stripe #> <dev_path> <offset>
39 static int unstripe_ctr(struct dm_target
*ti
, unsigned int argc
, char **argv
)
41 struct unstripe_c
*uc
;
43 unsigned long long start
;
47 ti
->error
= "Invalid number of arguments";
51 uc
= kzalloc(sizeof(*uc
), GFP_KERNEL
);
53 ti
->error
= "Memory allocation for unstriped context failed";
57 if (kstrtouint(argv
[0], 10, &uc
->stripes
) || !uc
->stripes
) {
58 ti
->error
= "Invalid stripe count";
62 if (kstrtouint(argv
[1], 10, &uc
->chunk_size
) || !uc
->chunk_size
) {
63 ti
->error
= "Invalid chunk_size";
67 if (kstrtouint(argv
[2], 10, &uc
->unstripe
)) {
68 ti
->error
= "Invalid stripe number";
72 if (uc
->unstripe
> uc
->stripes
&& uc
->stripes
> 1) {
73 ti
->error
= "Please provide stripe between [0, # of stripes]";
77 if (dm_get_device(ti
, argv
[3], dm_table_get_mode(ti
->table
), &uc
->dev
)) {
78 ti
->error
= "Couldn't get striped device";
82 if (sscanf(argv
[4], "%llu%c", &start
, &dummy
) != 1 || start
!= (sector_t
)start
) {
83 ti
->error
= "Invalid striped device offset";
86 uc
->physical_start
= start
;
88 uc
->unstripe_offset
= (sector_t
)uc
->unstripe
* uc
->chunk_size
;
89 uc
->unstripe_width
= (sector_t
)(uc
->stripes
- 1) * uc
->chunk_size
;
90 uc
->chunk_shift
= is_power_of_2(uc
->chunk_size
) ? fls(uc
->chunk_size
) - 1 : 0;
93 if (sector_div(tmp_len
, uc
->chunk_size
)) {
94 ti
->error
= "Target length not divisible by chunk size";
98 if (dm_set_target_max_io_len(ti
, uc
->chunk_size
)) {
99 ti
->error
= "Failed to set max io len";
106 cleanup_unstripe(uc
, ti
);
110 static void unstripe_dtr(struct dm_target
*ti
)
112 struct unstripe_c
*uc
= ti
->private;
114 cleanup_unstripe(uc
, ti
);
117 static sector_t
map_to_core(struct dm_target
*ti
, struct bio
*bio
)
119 struct unstripe_c
*uc
= ti
->private;
120 sector_t sector
= bio
->bi_iter
.bi_sector
;
121 sector_t tmp_sector
= sector
;
123 /* Shift us up to the right "row" on the stripe */
125 tmp_sector
>>= uc
->chunk_shift
;
127 sector_div(tmp_sector
, uc
->chunk_size
);
129 sector
+= uc
->unstripe_width
* tmp_sector
;
131 /* Account for what stripe we're operating on */
132 return sector
+ uc
->unstripe_offset
;
135 static int unstripe_map(struct dm_target
*ti
, struct bio
*bio
)
137 struct unstripe_c
*uc
= ti
->private;
139 bio_set_dev(bio
, uc
->dev
->bdev
);
140 bio
->bi_iter
.bi_sector
= map_to_core(ti
, bio
) + uc
->physical_start
;
142 return DM_MAPIO_REMAPPED
;
145 static void unstripe_status(struct dm_target
*ti
, status_type_t type
,
146 unsigned int status_flags
, char *result
, unsigned int maxlen
)
148 struct unstripe_c
*uc
= ti
->private;
152 case STATUSTYPE_INFO
:
155 case STATUSTYPE_TABLE
:
156 DMEMIT("%d %llu %d %s %llu",
157 uc
->stripes
, (unsigned long long)uc
->chunk_size
, uc
->unstripe
,
158 uc
->dev
->name
, (unsigned long long)uc
->physical_start
);
167 static int unstripe_iterate_devices(struct dm_target
*ti
,
168 iterate_devices_callout_fn fn
, void *data
)
170 struct unstripe_c
*uc
= ti
->private;
172 return fn(ti
, uc
->dev
, uc
->physical_start
, ti
->len
, data
);
175 static void unstripe_io_hints(struct dm_target
*ti
,
176 struct queue_limits
*limits
)
178 struct unstripe_c
*uc
= ti
->private;
180 limits
->chunk_sectors
= uc
->chunk_size
;
183 static struct target_type unstripe_target
= {
185 .version
= {1, 1, 0},
186 .features
= DM_TARGET_NOWAIT
,
187 .module
= THIS_MODULE
,
191 .status
= unstripe_status
,
192 .iterate_devices
= unstripe_iterate_devices
,
193 .io_hints
= unstripe_io_hints
,
197 MODULE_DESCRIPTION(DM_NAME
" unstriped target");
198 MODULE_ALIAS("dm-unstriped");
199 MODULE_AUTHOR("Scott Bauer <scott.bauer@intel.com>");
200 MODULE_LICENSE("GPL");