2 * Copyright (C) 2001-2003 Sistina Software (UK) Limited.
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/log2.h>
16 #define DM_MSG_PREFIX "striped"
20 sector_t physical_start
;
26 /* The size of this target / num. stripes */
27 sector_t stripe_width
;
29 /* stripe chunk size */
33 struct stripe stripe
[0];
36 static inline struct stripe_c
*alloc_context(unsigned int stripes
)
40 if (array_too_big(sizeof(struct stripe_c
), sizeof(struct stripe
),
44 len
= sizeof(struct stripe_c
) + (sizeof(struct stripe
) * stripes
);
46 return kmalloc(len
, GFP_KERNEL
);
50 * Parse a single <dev> <sector> pair
52 static int get_stripe(struct dm_target
*ti
, struct stripe_c
*sc
,
53 unsigned int stripe
, char **argv
)
55 unsigned long long start
;
57 if (sscanf(argv
[1], "%llu", &start
) != 1)
60 if (dm_get_device(ti
, argv
[0], start
, sc
->stripe_width
,
61 dm_table_get_mode(ti
->table
),
62 &sc
->stripe
[stripe
].dev
))
65 sc
->stripe
[stripe
].physical_start
= start
;
70 * Construct a striped mapping.
71 * <number of stripes> <chunk size (2^^n)> [<dev_path> <offset>]+
73 static int stripe_ctr(struct dm_target
*ti
, unsigned int argc
, char **argv
)
84 ti
->error
= "Not enough arguments";
88 stripes
= simple_strtoul(argv
[0], &end
, 10);
90 ti
->error
= "Invalid stripe count";
94 chunk_size
= simple_strtoul(argv
[1], &end
, 10);
96 ti
->error
= "Invalid chunk_size";
101 * chunk_size is a power of two
103 if (!is_power_of_2(chunk_size
) ||
104 (chunk_size
< (PAGE_SIZE
>> SECTOR_SHIFT
))) {
105 ti
->error
= "Invalid chunk size";
109 if (ti
->len
& (chunk_size
- 1)) {
110 ti
->error
= "Target length not divisible by "
116 if (sector_div(width
, stripes
)) {
117 ti
->error
= "Target length not divisible by "
123 * Do we have enough arguments for that many stripes ?
125 if (argc
!= (2 + 2 * stripes
)) {
126 ti
->error
= "Not enough destinations "
131 sc
= alloc_context(stripes
);
133 ti
->error
= "Memory allocation for striped context "
138 sc
->stripes
= stripes
;
139 sc
->stripe_width
= width
;
140 ti
->split_io
= chunk_size
;
142 sc
->chunk_mask
= ((sector_t
) chunk_size
) - 1;
143 for (sc
->chunk_shift
= 0; chunk_size
; sc
->chunk_shift
++)
148 * Get the stripe destinations.
150 for (i
= 0; i
< stripes
; i
++) {
153 r
= get_stripe(ti
, sc
, i
, argv
);
155 ti
->error
= "Couldn't parse stripe destination";
157 dm_put_device(ti
, sc
->stripe
[i
].dev
);
167 static void stripe_dtr(struct dm_target
*ti
)
170 struct stripe_c
*sc
= (struct stripe_c
*) ti
->private;
172 for (i
= 0; i
< sc
->stripes
; i
++)
173 dm_put_device(ti
, sc
->stripe
[i
].dev
);
178 static int stripe_map(struct dm_target
*ti
, struct bio
*bio
,
179 union map_info
*map_context
)
181 struct stripe_c
*sc
= (struct stripe_c
*) ti
->private;
183 sector_t offset
= bio
->bi_sector
- ti
->begin
;
184 sector_t chunk
= offset
>> sc
->chunk_shift
;
185 uint32_t stripe
= sector_div(chunk
, sc
->stripes
);
187 bio
->bi_bdev
= sc
->stripe
[stripe
].dev
->bdev
;
188 bio
->bi_sector
= sc
->stripe
[stripe
].physical_start
+
189 (chunk
<< sc
->chunk_shift
) + (offset
& sc
->chunk_mask
);
190 return DM_MAPIO_REMAPPED
;
193 static int stripe_status(struct dm_target
*ti
,
194 status_type_t type
, char *result
, unsigned int maxlen
)
196 struct stripe_c
*sc
= (struct stripe_c
*) ti
->private;
201 case STATUSTYPE_INFO
:
205 case STATUSTYPE_TABLE
:
206 DMEMIT("%d %llu", sc
->stripes
,
207 (unsigned long long)sc
->chunk_mask
+ 1);
208 for (i
= 0; i
< sc
->stripes
; i
++)
209 DMEMIT(" %s %llu", sc
->stripe
[i
].dev
->name
,
210 (unsigned long long)sc
->stripe
[i
].physical_start
);
216 static struct target_type stripe_target
= {
219 .module
= THIS_MODULE
,
223 .status
= stripe_status
,
226 int __init
dm_stripe_init(void)
230 r
= dm_register_target(&stripe_target
);
232 DMWARN("target registration failed");
237 void dm_stripe_exit(void)
239 if (dm_unregister_target(&stripe_target
))
240 DMWARN("target unregistration failed");