blob: 90f5b24f6e6cf6e2d314409ca89a9f0a44a6b194 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 raid0.c : Multiple Devices driver for Linux
3 Copyright (C) 1994-96 Marc ZYNGIER
4 <zyngier@ufr-info-p7.ibp.fr> or
5 <maz@gloups.fdn.fr>
6 Copyright (C) 1999, 2000 Ingo Molnar, Red Hat
7
8
9 RAID-0 management functions.
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
14 any later version.
15
16 You should have received a copy of the GNU General Public License
17 (for example /usr/src/linux/COPYING); if not, write to the Free
18 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/raid/raid0.h>
22
Jens Axboe165125e2007-07-24 09:28:11 +020023static void raid0_unplug(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
25 mddev_t *mddev = q->queuedata;
26 raid0_conf_t *conf = mddev_to_conf(mddev);
27 mdk_rdev_t **devlist = conf->strip_zone[0].dev;
28 int i;
29
30 for (i=0; i<mddev->raid_disks; i++) {
Jens Axboe165125e2007-07-24 09:28:11 +020031 struct request_queue *r_queue = bdev_get_queue(devlist[i]->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -050033 blk_unplug(r_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 }
35}
36
NeilBrown26be34d2006-10-03 01:15:53 -070037static int raid0_congested(void *data, int bits)
38{
39 mddev_t *mddev = data;
40 raid0_conf_t *conf = mddev_to_conf(mddev);
41 mdk_rdev_t **devlist = conf->strip_zone[0].dev;
42 int i, ret = 0;
43
44 for (i = 0; i < mddev->raid_disks && !ret ; i++) {
Jens Axboe165125e2007-07-24 09:28:11 +020045 struct request_queue *q = bdev_get_queue(devlist[i]->bdev);
NeilBrown26be34d2006-10-03 01:15:53 -070046
47 ret |= bdi_congested(&q->backing_dev_info, bits);
48 }
49 return ret;
50}
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53static int create_strip_zones (mddev_t *mddev)
54{
55 int i, c, j;
Andre Noll6b8796c2009-01-09 08:31:07 +110056 sector_t current_start, curr_zone_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 sector_t min_spacing;
58 raid0_conf_t *conf = mddev_to_conf(mddev);
59 mdk_rdev_t *smallest, *rdev1, *rdev2, *rdev;
60 struct list_head *tmp1, *tmp2;
61 struct strip_zone *zone;
62 int cnt;
63 char b[BDEVNAME_SIZE];
64
65 /*
66 * The number of 'same size groups'
67 */
68 conf->nr_strip_zones = 0;
69
NeilBrownd089c6a2008-02-06 01:39:59 -080070 rdev_for_each(rdev1, tmp1, mddev) {
Andre Noll0825b87a2009-01-09 08:31:07 +110071 printk(KERN_INFO "raid0: looking at %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 bdevname(rdev1->bdev,b));
73 c = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -080074 rdev_for_each(rdev2, tmp2, mddev) {
Andre Noll0825b87a2009-01-09 08:31:07 +110075 printk(KERN_INFO "raid0: comparing %s(%llu)",
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 bdevname(rdev1->bdev,b),
77 (unsigned long long)rdev1->size);
Andre Noll0825b87a2009-01-09 08:31:07 +110078 printk(KERN_INFO " with %s(%llu)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 bdevname(rdev2->bdev,b),
80 (unsigned long long)rdev2->size);
81 if (rdev2 == rdev1) {
Andre Noll0825b87a2009-01-09 08:31:07 +110082 printk(KERN_INFO "raid0: END\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 break;
84 }
85 if (rdev2->size == rdev1->size)
86 {
87 /*
88 * Not unique, don't count it as a new
89 * group
90 */
Andre Noll0825b87a2009-01-09 08:31:07 +110091 printk(KERN_INFO "raid0: EQUAL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 c = 1;
93 break;
94 }
Andre Noll0825b87a2009-01-09 08:31:07 +110095 printk(KERN_INFO "raid0: NOT EQUAL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
97 if (!c) {
Andre Noll0825b87a2009-01-09 08:31:07 +110098 printk(KERN_INFO "raid0: ==> UNIQUE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 conf->nr_strip_zones++;
Andre Noll0825b87a2009-01-09 08:31:07 +1100100 printk(KERN_INFO "raid0: %d zones\n",
101 conf->nr_strip_zones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
103 }
Andre Noll0825b87a2009-01-09 08:31:07 +1100104 printk(KERN_INFO "raid0: FINAL %d zones\n", conf->nr_strip_zones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
NeilBrown9ffae0c2006-01-06 00:20:32 -0800106 conf->strip_zone = kzalloc(sizeof(struct strip_zone)*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 conf->nr_strip_zones, GFP_KERNEL);
108 if (!conf->strip_zone)
109 return 1;
NeilBrown9ffae0c2006-01-06 00:20:32 -0800110 conf->devlist = kzalloc(sizeof(mdk_rdev_t*)*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 conf->nr_strip_zones*mddev->raid_disks,
112 GFP_KERNEL);
113 if (!conf->devlist)
114 return 1;
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 /* The first zone must contain all devices, so here we check that
117 * there is a proper alignment of slots to devices and find them all
118 */
119 zone = &conf->strip_zone[0];
120 cnt = 0;
121 smallest = NULL;
122 zone->dev = conf->devlist;
NeilBrownd089c6a2008-02-06 01:39:59 -0800123 rdev_for_each(rdev1, tmp1, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 int j = rdev1->raid_disk;
125
126 if (j < 0 || j >= mddev->raid_disks) {
Andre Noll0825b87a2009-01-09 08:31:07 +1100127 printk(KERN_ERR "raid0: bad disk number %d - "
128 "aborting!\n", j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 goto abort;
130 }
131 if (zone->dev[j]) {
Andre Noll0825b87a2009-01-09 08:31:07 +1100132 printk(KERN_ERR "raid0: multiple devices for %d - "
133 "aborting!\n", j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 goto abort;
135 }
136 zone->dev[j] = rdev1;
137
138 blk_queue_stack_limits(mddev->queue,
139 rdev1->bdev->bd_disk->queue);
140 /* as we don't honour merge_bvec_fn, we must never risk
141 * violating it, so limit ->max_sector to one PAGE, as
142 * a one page request is never in violation.
143 */
144
145 if (rdev1->bdev->bd_disk->queue->merge_bvec_fn &&
146 mddev->queue->max_sectors > (PAGE_SIZE>>9))
147 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
148
149 if (!smallest || (rdev1->size <smallest->size))
150 smallest = rdev1;
151 cnt++;
152 }
153 if (cnt != mddev->raid_disks) {
Andre Noll0825b87a2009-01-09 08:31:07 +1100154 printk(KERN_ERR "raid0: too few disks (%d of %d) - "
155 "aborting!\n", cnt, mddev->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 goto abort;
157 }
158 zone->nb_dev = cnt;
Andre Noll83838ed2009-01-09 08:31:07 +1100159 zone->sectors = smallest->size * cnt * 2;
Andre Noll6199d3d2009-01-09 08:31:07 +1100160 zone->zone_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Andre Noll6b8796c2009-01-09 08:31:07 +1100162 current_start = smallest->size * 2;
Andre Noll83838ed2009-01-09 08:31:07 +1100163 curr_zone_start = zone->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 /* now do the other zones */
166 for (i = 1; i < conf->nr_strip_zones; i++)
167 {
168 zone = conf->strip_zone + i;
169 zone->dev = conf->strip_zone[i-1].dev + mddev->raid_disks;
170
Andre Noll0825b87a2009-01-09 08:31:07 +1100171 printk(KERN_INFO "raid0: zone %d\n", i);
Andre Noll6b8796c2009-01-09 08:31:07 +1100172 zone->dev_start = current_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 smallest = NULL;
174 c = 0;
175
176 for (j=0; j<cnt; j++) {
177 char b[BDEVNAME_SIZE];
178 rdev = conf->strip_zone[0].dev[j];
Andre Noll0825b87a2009-01-09 08:31:07 +1100179 printk(KERN_INFO "raid0: checking %s ...",
180 bdevname(rdev->bdev, b));
Andre Noll6b8796c2009-01-09 08:31:07 +1100181 if (rdev->size > current_start / 2) {
Andre Noll0825b87a2009-01-09 08:31:07 +1100182 printk(KERN_INFO " contained as device %d\n",
183 c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 zone->dev[c] = rdev;
185 c++;
186 if (!smallest || (rdev->size <smallest->size)) {
187 smallest = rdev;
Andre Noll0825b87a2009-01-09 08:31:07 +1100188 printk(KERN_INFO " (%llu) is smallest!.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 (unsigned long long)rdev->size);
190 }
191 } else
Andre Noll0825b87a2009-01-09 08:31:07 +1100192 printk(KERN_INFO " nope.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
194
195 zone->nb_dev = c;
Andre Noll83838ed2009-01-09 08:31:07 +1100196 zone->sectors = (smallest->size * 2 - current_start) * c;
197 printk(KERN_INFO "raid0: zone->nb_dev: %d, sectors: %llu\n",
198 zone->nb_dev, (unsigned long long)zone->sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Andre Noll6b8796c2009-01-09 08:31:07 +1100200 zone->zone_start = curr_zone_start;
Andre Noll83838ed2009-01-09 08:31:07 +1100201 curr_zone_start += zone->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Andre Noll6b8796c2009-01-09 08:31:07 +1100203 current_start = smallest->size * 2;
204 printk(KERN_INFO "raid0: current zone start: %llu\n",
205 (unsigned long long)current_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
207
208 /* Now find appropriate hash spacing.
209 * We want a number which causes most hash entries to cover
210 * at most two strips, but the hash table must be at most
211 * 1 PAGE. We choose the smallest strip, or contiguous collection
212 * of strips, that has big enough size. We never consider the last
213 * strip though as it's size has no bearing on the efficacy of the hash
214 * table.
215 */
Andre Nollccacc7d2009-01-09 08:31:08 +1100216 conf->spacing = curr_zone_start;
217 min_spacing = curr_zone_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 sector_div(min_spacing, PAGE_SIZE/sizeof(struct strip_zone*));
219 for (i=0; i < conf->nr_strip_zones-1; i++) {
Andre Nollccacc7d2009-01-09 08:31:08 +1100220 sector_t s = 0;
221 for (j = i; j < conf->nr_strip_zones - 1 &&
222 s < min_spacing; j++)
223 s += conf->strip_zone[j].sectors;
224 if (s >= min_spacing && s < conf->spacing)
225 conf->spacing = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
227
228 mddev->queue->unplug_fn = raid0_unplug;
229
NeilBrown26be34d2006-10-03 01:15:53 -0700230 mddev->queue->backing_dev_info.congested_fn = raid0_congested;
231 mddev->queue->backing_dev_info.congested_data = mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Andre Noll0825b87a2009-01-09 08:31:07 +1100233 printk(KERN_INFO "raid0: done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return 0;
235 abort:
236 return 1;
237}
238
239/**
240 * raid0_mergeable_bvec -- tell bio layer if a two requests can be merged
241 * @q: request queue
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200242 * @bvm: properties of new bio
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 * @biovec: the request that could be merged to it.
244 *
245 * Return amount of bytes we can accept at this offset
246 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200247static int raid0_mergeable_bvec(struct request_queue *q,
248 struct bvec_merge_data *bvm,
249 struct bio_vec *biovec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 mddev_t *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200252 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 int max;
254 unsigned int chunk_sectors = mddev->chunk_size >> 9;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200255 unsigned int bio_sectors = bvm->bi_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
258 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
259 if (max <= biovec->bv_len && bio_sectors == 0)
260 return biovec->bv_len;
261 else
262 return max;
263}
264
265static int raid0_run (mddev_t *mddev)
266{
267 unsigned cur=0, i=0, nb_zone;
Andre Nollccacc7d2009-01-09 08:31:08 +1100268 s64 sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 raid0_conf_t *conf;
270 mdk_rdev_t *rdev;
271 struct list_head *tmp;
272
NeilBrown2604b702006-01-06 00:20:36 -0800273 if (mddev->chunk_size == 0) {
274 printk(KERN_ERR "md/raid0: non-zero chunk size required.\n");
275 return -EINVAL;
276 }
277 printk(KERN_INFO "%s: setting max_sectors to %d, segment boundary to %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 mdname(mddev),
279 mddev->chunk_size >> 9,
280 (mddev->chunk_size>>1)-1);
281 blk_queue_max_sectors(mddev->queue, mddev->chunk_size >> 9);
282 blk_queue_segment_boundary(mddev->queue, (mddev->chunk_size>>1) - 1);
Neil Browne7e72bf2008-05-14 16:05:54 -0700283 mddev->queue->queue_lock = &mddev->queue->__queue_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 conf = kmalloc(sizeof (raid0_conf_t), GFP_KERNEL);
286 if (!conf)
287 goto out;
288 mddev->private = (void *)conf;
289
290 conf->strip_zone = NULL;
291 conf->devlist = NULL;
292 if (create_strip_zones (mddev))
293 goto out_free_conf;
294
295 /* calculate array device size */
Andre Nollf233ea52008-07-21 17:05:22 +1000296 mddev->array_sectors = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -0800297 rdev_for_each(rdev, tmp, mddev)
Andre Nollf233ea52008-07-21 17:05:22 +1000298 mddev->array_sectors += rdev->size * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Andre Nollccacc7d2009-01-09 08:31:08 +1100300 printk(KERN_INFO "raid0 : md_size is %llu sectors.\n",
301 (unsigned long long)mddev->array_sectors);
302 printk(KERN_INFO "raid0 : conf->spacing is %llu sectors.\n",
303 (unsigned long long)conf->spacing);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 {
Andre Nollccacc7d2009-01-09 08:31:08 +1100305 sector_t s = mddev->array_sectors;
306 sector_t space = conf->spacing;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 int round;
Andre Nollccacc7d2009-01-09 08:31:08 +1100308 conf->sector_shift = 0;
Neil Brown1eb29122005-07-15 03:56:27 -0700309 if (sizeof(sector_t) > sizeof(u32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 /*shift down space and s so that sector_div will work */
Neil Brown1eb29122005-07-15 03:56:27 -0700311 while (space > (sector_t) (~(u32)0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 s >>= 1;
313 space >>= 1;
314 s += 1; /* force round-up */
Andre Nollccacc7d2009-01-09 08:31:08 +1100315 conf->sector_shift++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
317 }
Neil Brown1eb29122005-07-15 03:56:27 -0700318 round = sector_div(s, (u32)space) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 nb_zone = s + round;
320 }
Andre Nollccacc7d2009-01-09 08:31:08 +1100321 printk(KERN_INFO "raid0 : nb_zone is %d.\n", nb_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Andre Nollccacc7d2009-01-09 08:31:08 +1100323 printk(KERN_INFO "raid0 : Allocating %zu bytes for hash.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 nb_zone*sizeof(struct strip_zone*));
325 conf->hash_table = kmalloc (sizeof (struct strip_zone *)*nb_zone, GFP_KERNEL);
326 if (!conf->hash_table)
327 goto out_free_conf;
Andre Nollccacc7d2009-01-09 08:31:08 +1100328 sectors = conf->strip_zone[cur].sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
NeilBrown5c4c3332006-05-22 22:35:26 -0700330 conf->hash_table[0] = conf->strip_zone + cur;
331 for (i=1; i< nb_zone; i++) {
Andre Nollccacc7d2009-01-09 08:31:08 +1100332 while (sectors <= conf->spacing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 cur++;
Andre Nollccacc7d2009-01-09 08:31:08 +1100334 sectors += conf->strip_zone[cur].sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
Andre Nollccacc7d2009-01-09 08:31:08 +1100336 sectors -= conf->spacing;
NeilBrown5c4c3332006-05-22 22:35:26 -0700337 conf->hash_table[i] = conf->strip_zone + cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
Andre Nollccacc7d2009-01-09 08:31:08 +1100339 if (conf->sector_shift) {
340 conf->spacing >>= conf->sector_shift;
341 /* round spacing up so when we divide by it, we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 * err on the side of too-low, which is safest
343 */
Andre Nollccacc7d2009-01-09 08:31:08 +1100344 conf->spacing++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
346
347 /* calculate the max read-ahead size.
348 * For read-ahead of large files to be effective, we need to
349 * readahead at least twice a whole stripe. i.e. number of devices
350 * multiplied by chunk size times 2.
351 * If an individual device has an ra_pages greater than the
352 * chunk size, then we will not drive that device as hard as it
353 * wants. We consider this a configuration error: a larger
354 * chunksize should be used in that case.
355 */
356 {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800357 int stripe = mddev->raid_disks * mddev->chunk_size / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
359 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
360 }
361
362
363 blk_queue_merge_bvec(mddev->queue, raid0_mergeable_bvec);
364 return 0;
365
366out_free_conf:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700367 kfree(conf->strip_zone);
368 kfree(conf->devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 kfree(conf);
370 mddev->private = NULL;
371out:
NeilBrown29fc7e32006-02-03 03:03:41 -0800372 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
375static int raid0_stop (mddev_t *mddev)
376{
377 raid0_conf_t *conf = mddev_to_conf(mddev);
378
379 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700380 kfree(conf->hash_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 conf->hash_table = NULL;
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700382 kfree(conf->strip_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 conf->strip_zone = NULL;
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700384 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 mddev->private = NULL;
386
387 return 0;
388}
389
Jens Axboe165125e2007-07-24 09:28:11 +0200390static int raid0_make_request (struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 mddev_t *mddev = q->queuedata;
Andre Nolla4712002009-01-09 08:31:06 +1100393 unsigned int sect_in_chunk, chunksect_bits, chunk_sects;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 raid0_conf_t *conf = mddev_to_conf(mddev);
395 struct strip_zone *zone;
396 mdk_rdev_t *tmp_dev;
NeilBrown787f17f2007-05-23 13:58:09 -0700397 sector_t chunk;
Andre Nolle0f06862009-01-09 08:31:06 +1100398 sector_t sector, rsect;
Jens Axboea3623572005-11-01 09:26:16 +0100399 const int rw = bio_data_dir(bio);
Tejun Heoc9959052008-08-25 19:47:21 +0900400 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
NeilBrowne5dcdd82005-09-09 16:23:41 -0700402 if (unlikely(bio_barrier(bio))) {
NeilBrown6712ecf2007-09-27 12:47:43 +0200403 bio_endio(bio, -EOPNOTSUPP);
NeilBrowne5dcdd82005-09-09 16:23:41 -0700404 return 0;
405 }
406
Tejun Heo074a7ac2008-08-25 19:56:14 +0900407 cpu = part_stat_lock();
408 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
409 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
410 bio_sectors(bio));
411 part_stat_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 chunk_sects = mddev->chunk_size >> 9;
Andre Noll1b7fdf82009-01-09 08:31:06 +1100414 chunksect_bits = ffz(~chunk_sects);
Andre Nolle0f06862009-01-09 08:31:06 +1100415 sector = bio->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 if (unlikely(chunk_sects < (bio->bi_sector & (chunk_sects - 1)) + (bio->bi_size >> 9))) {
418 struct bio_pair *bp;
419 /* Sanity check -- queue functions should prevent this happening */
420 if (bio->bi_vcnt != 1 ||
421 bio->bi_idx != 0)
422 goto bad_map;
423 /* This is a one page bio that upper layers
424 * refuse to split for us, so we need to split it.
425 */
Denis ChengRq6feef532008-10-09 08:57:05 +0200426 bp = bio_split(bio, chunk_sects - (bio->bi_sector & (chunk_sects - 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (raid0_make_request(q, &bp->bio1))
428 generic_make_request(&bp->bio1);
429 if (raid0_make_request(q, &bp->bio2))
430 generic_make_request(&bp->bio2);
431
432 bio_pair_release(bp);
433 return 0;
434 }
435
436
437 {
Andre Nollccacc7d2009-01-09 08:31:08 +1100438 sector_t x = sector >> conf->sector_shift;
439 sector_div(x, (u32)conf->spacing);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 zone = conf->hash_table[x];
441 }
Andre Noll83838ed2009-01-09 08:31:07 +1100442
443 while (sector >= zone->zone_start + zone->sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 zone++;
Andre Noll83838ed2009-01-09 08:31:07 +1100445
Andre Nolla4712002009-01-09 08:31:06 +1100446 sect_in_chunk = bio->bi_sector & (chunk_sects - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448
449 {
Andre Noll6199d3d2009-01-09 08:31:07 +1100450 sector_t x = (sector - zone->zone_start) >> chunksect_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 sector_div(x, zone->nb_dev);
453 chunk = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Andre Nolle0f06862009-01-09 08:31:06 +1100455 x = sector >> chunksect_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 tmp_dev = zone->dev[sector_div(x, zone->nb_dev)];
457 }
Andre Noll019c4e22009-01-09 08:31:06 +1100458 rsect = (chunk << chunksect_bits) + zone->dev_start + sect_in_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 bio->bi_bdev = tmp_dev->bdev;
461 bio->bi_sector = rsect + tmp_dev->data_offset;
462
463 /*
464 * Let the main block layer submit the IO and resolve recursion:
465 */
466 return 1;
467
468bad_map:
469 printk("raid0_make_request bug: can't convert block across chunks"
Andre Nolla4712002009-01-09 08:31:06 +1100470 " or bigger than %dk %llu %d\n", chunk_sects / 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
472
NeilBrown6712ecf2007-09-27 12:47:43 +0200473 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return 0;
475}
NeilBrown8299d7f2007-10-16 23:30:53 -0700476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477static void raid0_status (struct seq_file *seq, mddev_t *mddev)
478{
479#undef MD_DEBUG
480#ifdef MD_DEBUG
481 int j, k, h;
482 char b[BDEVNAME_SIZE];
483 raid0_conf_t *conf = mddev_to_conf(mddev);
NeilBrown8299d7f2007-10-16 23:30:53 -0700484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 h = 0;
486 for (j = 0; j < conf->nr_strip_zones; j++) {
487 seq_printf(seq, " z%d", j);
488 if (conf->hash_table[h] == conf->strip_zone+j)
NeilBrown8299d7f2007-10-16 23:30:53 -0700489 seq_printf(seq, "(h%d)", h++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 seq_printf(seq, "=[");
491 for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
NeilBrown8299d7f2007-10-16 23:30:53 -0700492 seq_printf(seq, "%s/", bdevname(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 conf->strip_zone[j].dev[k]->bdev,b));
494
Andre Noll6199d3d2009-01-09 08:31:07 +1100495 seq_printf(seq, "] zs=%d ds=%d s=%d\n",
496 conf->strip_zone[j].zone_start,
Andre Noll019c4e22009-01-09 08:31:06 +1100497 conf->strip_zone[j].dev_start,
Andre Noll83838ed2009-01-09 08:31:07 +1100498 conf->strip_zone[j].sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
500#endif
501 seq_printf(seq, " %dk chunks", mddev->chunk_size/1024);
502 return;
503}
504
NeilBrown2604b702006-01-06 00:20:36 -0800505static struct mdk_personality raid0_personality=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 .name = "raid0",
NeilBrown2604b702006-01-06 00:20:36 -0800508 .level = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 .owner = THIS_MODULE,
510 .make_request = raid0_make_request,
511 .run = raid0_run,
512 .stop = raid0_stop,
513 .status = raid0_status,
514};
515
516static int __init raid0_init (void)
517{
NeilBrown2604b702006-01-06 00:20:36 -0800518 return register_md_personality (&raid0_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
521static void raid0_exit (void)
522{
NeilBrown2604b702006-01-06 00:20:36 -0800523 unregister_md_personality (&raid0_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
526module_init(raid0_init);
527module_exit(raid0_exit);
528MODULE_LICENSE("GPL");
529MODULE_ALIAS("md-personality-2"); /* RAID0 */
NeilBrownd9d166c2006-01-06 00:20:51 -0800530MODULE_ALIAS("md-raid0");
NeilBrown2604b702006-01-06 00:20:36 -0800531MODULE_ALIAS("md-level-0");