blob: 6c29fcecd8921d46503d51d23bf1f8be619c7ead [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2001-2003 Sistina Software (UK) Limited.
3 *
4 * This file is released under the GPL.
5 */
6
7#include "dm.h"
8
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
Alasdair G Kergon72d94862006-06-26 00:27:35 -070015#define DM_MSG_PREFIX "striped"
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017struct stripe {
18 struct dm_dev *dev;
19 sector_t physical_start;
20};
21
22struct stripe_c {
23 uint32_t stripes;
24
25 /* The size of this target / num. stripes */
26 sector_t stripe_width;
27
28 /* stripe chunk size */
29 uint32_t chunk_shift;
30 sector_t chunk_mask;
31
32 struct stripe stripe[0];
33};
34
35static inline struct stripe_c *alloc_context(unsigned int stripes)
36{
37 size_t len;
38
39 if (array_too_big(sizeof(struct stripe_c), sizeof(struct stripe),
40 stripes))
41 return NULL;
42
43 len = sizeof(struct stripe_c) + (sizeof(struct stripe) * stripes);
44
45 return kmalloc(len, GFP_KERNEL);
46}
47
48/*
49 * Parse a single <dev> <sector> pair
50 */
51static int get_stripe(struct dm_target *ti, struct stripe_c *sc,
52 unsigned int stripe, char **argv)
53{
Andrew Morton4ee218c2006-03-27 01:17:48 -080054 unsigned long long start;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Andrew Morton4ee218c2006-03-27 01:17:48 -080056 if (sscanf(argv[1], "%llu", &start) != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 return -EINVAL;
58
59 if (dm_get_device(ti, argv[0], start, sc->stripe_width,
60 dm_table_get_mode(ti->table),
61 &sc->stripe[stripe].dev))
62 return -ENXIO;
63
64 sc->stripe[stripe].physical_start = start;
65 return 0;
66}
67
68/*
69 * Construct a striped mapping.
70 * <number of stripes> <chunk size (2^^n)> [<dev_path> <offset>]+
71 */
72static int stripe_ctr(struct dm_target *ti, unsigned int argc, char **argv)
73{
74 struct stripe_c *sc;
75 sector_t width;
76 uint32_t stripes;
77 uint32_t chunk_size;
78 char *end;
79 int r;
80 unsigned int i;
81
82 if (argc < 2) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -070083 ti->error = "Not enough arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return -EINVAL;
85 }
86
87 stripes = simple_strtoul(argv[0], &end, 10);
88 if (*end) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -070089 ti->error = "Invalid stripe count";
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return -EINVAL;
91 }
92
93 chunk_size = simple_strtoul(argv[1], &end, 10);
94 if (*end) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -070095 ti->error = "Invalid chunk_size";
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return -EINVAL;
97 }
98
99 /*
100 * chunk_size is a power of two
101 */
102 if (!chunk_size || (chunk_size & (chunk_size - 1)) ||
103 (chunk_size < (PAGE_SIZE >> SECTOR_SHIFT))) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700104 ti->error = "Invalid chunk size";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return -EINVAL;
106 }
107
Kevin Corrya22c96c2006-03-27 01:18:01 -0800108 if (ti->len & (chunk_size - 1)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700109 ti->error = "Target length not divisible by "
Kevin Corry8ba32fd2006-03-16 23:04:03 -0800110 "chunk size";
111 return -EINVAL;
112 }
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 width = ti->len;
115 if (sector_div(width, stripes)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700116 ti->error = "Target length not divisible by "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 "number of stripes";
118 return -EINVAL;
119 }
120
121 /*
122 * Do we have enough arguments for that many stripes ?
123 */
124 if (argc != (2 + 2 * stripes)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700125 ti->error = "Not enough destinations "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 "specified";
127 return -EINVAL;
128 }
129
130 sc = alloc_context(stripes);
131 if (!sc) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700132 ti->error = "Memory allocation for striped context "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 "failed";
134 return -ENOMEM;
135 }
136
137 sc->stripes = stripes;
138 sc->stripe_width = width;
139 ti->split_io = chunk_size;
140
141 sc->chunk_mask = ((sector_t) chunk_size) - 1;
142 for (sc->chunk_shift = 0; chunk_size; sc->chunk_shift++)
143 chunk_size >>= 1;
144 sc->chunk_shift--;
145
146 /*
147 * Get the stripe destinations.
148 */
149 for (i = 0; i < stripes; i++) {
150 argv += 2;
151
152 r = get_stripe(ti, sc, i, argv);
153 if (r < 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700154 ti->error = "Couldn't parse stripe destination";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 while (i--)
156 dm_put_device(ti, sc->stripe[i].dev);
157 kfree(sc);
158 return r;
159 }
160 }
161
162 ti->private = sc;
163 return 0;
164}
165
166static void stripe_dtr(struct dm_target *ti)
167{
168 unsigned int i;
169 struct stripe_c *sc = (struct stripe_c *) ti->private;
170
171 for (i = 0; i < sc->stripes; i++)
172 dm_put_device(ti, sc->stripe[i].dev);
173
174 kfree(sc);
175}
176
177static int stripe_map(struct dm_target *ti, struct bio *bio,
178 union map_info *map_context)
179{
180 struct stripe_c *sc = (struct stripe_c *) ti->private;
181
182 sector_t offset = bio->bi_sector - ti->begin;
183 sector_t chunk = offset >> sc->chunk_shift;
184 uint32_t stripe = sector_div(chunk, sc->stripes);
185
186 bio->bi_bdev = sc->stripe[stripe].dev->bdev;
187 bio->bi_sector = sc->stripe[stripe].physical_start +
188 (chunk << sc->chunk_shift) + (offset & sc->chunk_mask);
189 return 1;
190}
191
192static int stripe_status(struct dm_target *ti,
193 status_type_t type, char *result, unsigned int maxlen)
194{
195 struct stripe_c *sc = (struct stripe_c *) ti->private;
196 unsigned int sz = 0;
197 unsigned int i;
198
199 switch (type) {
200 case STATUSTYPE_INFO:
201 result[0] = '\0';
202 break;
203
204 case STATUSTYPE_TABLE:
Andrew Morton4ee218c2006-03-27 01:17:48 -0800205 DMEMIT("%d %llu", sc->stripes,
206 (unsigned long long)sc->chunk_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 for (i = 0; i < sc->stripes; i++)
Andrew Morton4ee218c2006-03-27 01:17:48 -0800208 DMEMIT(" %s %llu", sc->stripe[i].dev->name,
209 (unsigned long long)sc->stripe[i].physical_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 break;
211 }
212 return 0;
213}
214
215static struct target_type stripe_target = {
216 .name = "striped",
217 .version= {1, 0, 2},
218 .module = THIS_MODULE,
219 .ctr = stripe_ctr,
220 .dtr = stripe_dtr,
221 .map = stripe_map,
222 .status = stripe_status,
223};
224
225int __init dm_stripe_init(void)
226{
227 int r;
228
229 r = dm_register_target(&stripe_target);
230 if (r < 0)
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700231 DMWARN("target registration failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 return r;
234}
235
236void dm_stripe_exit(void)
237{
238 if (dm_unregister_target(&stripe_target))
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700239 DMWARN("target unregistration failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 return;
242}