blob: 64e4c77a15689637a86af9c7e53d016f79209427 [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
NeilBrownbff61972009-03-31 14:33:13 +110021#include <linux/blkdev.h>
22#include <linux/raid/md_k.h>
23#include <linux/seq_file.h>
Christoph Hellwigef740c32009-03-31 14:27:03 +110024#include "raid0.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Jens Axboe165125e2007-07-24 09:28:11 +020026static void raid0_unplug(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
28 mddev_t *mddev = q->queuedata;
29 raid0_conf_t *conf = mddev_to_conf(mddev);
30 mdk_rdev_t **devlist = conf->strip_zone[0].dev;
31 int i;
32
33 for (i=0; i<mddev->raid_disks; i++) {
Jens Axboe165125e2007-07-24 09:28:11 +020034 struct request_queue *r_queue = bdev_get_queue(devlist[i]->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -050036 blk_unplug(r_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 }
38}
39
NeilBrown26be34d2006-10-03 01:15:53 -070040static int raid0_congested(void *data, int bits)
41{
42 mddev_t *mddev = data;
43 raid0_conf_t *conf = mddev_to_conf(mddev);
44 mdk_rdev_t **devlist = conf->strip_zone[0].dev;
45 int i, ret = 0;
46
47 for (i = 0; i < mddev->raid_disks && !ret ; i++) {
Jens Axboe165125e2007-07-24 09:28:11 +020048 struct request_queue *q = bdev_get_queue(devlist[i]->bdev);
NeilBrown26be34d2006-10-03 01:15:53 -070049
50 ret |= bdi_congested(&q->backing_dev_info, bits);
51 }
52 return ret;
53}
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56static int create_strip_zones (mddev_t *mddev)
57{
58 int i, c, j;
Andre Noll6b8796c2009-01-09 08:31:07 +110059 sector_t current_start, curr_zone_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 sector_t min_spacing;
61 raid0_conf_t *conf = mddev_to_conf(mddev);
62 mdk_rdev_t *smallest, *rdev1, *rdev2, *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 struct strip_zone *zone;
64 int cnt;
65 char b[BDEVNAME_SIZE];
66
67 /*
68 * The number of 'same size groups'
69 */
70 conf->nr_strip_zones = 0;
71
Cheng Renquan159ec1f2009-01-09 08:31:08 +110072 list_for_each_entry(rdev1, &mddev->disks, same_set) {
Andre Noll0825b87a2009-01-09 08:31:07 +110073 printk(KERN_INFO "raid0: looking at %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 bdevname(rdev1->bdev,b));
75 c = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +110076 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Andre Noll0825b87a2009-01-09 08:31:07 +110077 printk(KERN_INFO "raid0: comparing %s(%llu)",
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 bdevname(rdev1->bdev,b),
79 (unsigned long long)rdev1->size);
Andre Noll0825b87a2009-01-09 08:31:07 +110080 printk(KERN_INFO " with %s(%llu)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 bdevname(rdev2->bdev,b),
82 (unsigned long long)rdev2->size);
83 if (rdev2 == rdev1) {
Andre Noll0825b87a2009-01-09 08:31:07 +110084 printk(KERN_INFO "raid0: END\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 break;
86 }
87 if (rdev2->size == rdev1->size)
88 {
89 /*
90 * Not unique, don't count it as a new
91 * group
92 */
Andre Noll0825b87a2009-01-09 08:31:07 +110093 printk(KERN_INFO "raid0: EQUAL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 c = 1;
95 break;
96 }
Andre Noll0825b87a2009-01-09 08:31:07 +110097 printk(KERN_INFO "raid0: NOT EQUAL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
99 if (!c) {
Andre Noll0825b87a2009-01-09 08:31:07 +1100100 printk(KERN_INFO "raid0: ==> UNIQUE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 conf->nr_strip_zones++;
Andre Noll0825b87a2009-01-09 08:31:07 +1100102 printk(KERN_INFO "raid0: %d zones\n",
103 conf->nr_strip_zones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
105 }
Andre Noll0825b87a2009-01-09 08:31:07 +1100106 printk(KERN_INFO "raid0: FINAL %d zones\n", conf->nr_strip_zones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
NeilBrown9ffae0c2006-01-06 00:20:32 -0800108 conf->strip_zone = kzalloc(sizeof(struct strip_zone)*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 conf->nr_strip_zones, GFP_KERNEL);
110 if (!conf->strip_zone)
111 return 1;
NeilBrown9ffae0c2006-01-06 00:20:32 -0800112 conf->devlist = kzalloc(sizeof(mdk_rdev_t*)*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 conf->nr_strip_zones*mddev->raid_disks,
114 GFP_KERNEL);
115 if (!conf->devlist)
116 return 1;
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 /* The first zone must contain all devices, so here we check that
119 * there is a proper alignment of slots to devices and find them all
120 */
121 zone = &conf->strip_zone[0];
122 cnt = 0;
123 smallest = NULL;
124 zone->dev = conf->devlist;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100125 list_for_each_entry(rdev1, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 int j = rdev1->raid_disk;
127
128 if (j < 0 || j >= mddev->raid_disks) {
Andre Noll0825b87a2009-01-09 08:31:07 +1100129 printk(KERN_ERR "raid0: bad disk number %d - "
130 "aborting!\n", j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 goto abort;
132 }
133 if (zone->dev[j]) {
Andre Noll0825b87a2009-01-09 08:31:07 +1100134 printk(KERN_ERR "raid0: multiple devices for %d - "
135 "aborting!\n", j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 goto abort;
137 }
138 zone->dev[j] = rdev1;
139
140 blk_queue_stack_limits(mddev->queue,
141 rdev1->bdev->bd_disk->queue);
142 /* as we don't honour merge_bvec_fn, we must never risk
143 * violating it, so limit ->max_sector to one PAGE, as
144 * a one page request is never in violation.
145 */
146
147 if (rdev1->bdev->bd_disk->queue->merge_bvec_fn &&
148 mddev->queue->max_sectors > (PAGE_SIZE>>9))
149 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
150
151 if (!smallest || (rdev1->size <smallest->size))
152 smallest = rdev1;
153 cnt++;
154 }
155 if (cnt != mddev->raid_disks) {
Andre Noll0825b87a2009-01-09 08:31:07 +1100156 printk(KERN_ERR "raid0: too few disks (%d of %d) - "
157 "aborting!\n", cnt, mddev->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 goto abort;
159 }
160 zone->nb_dev = cnt;
Andre Noll83838ed2009-01-09 08:31:07 +1100161 zone->sectors = smallest->size * cnt * 2;
Andre Noll6199d3d2009-01-09 08:31:07 +1100162 zone->zone_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Andre Noll6b8796c2009-01-09 08:31:07 +1100164 current_start = smallest->size * 2;
Andre Noll83838ed2009-01-09 08:31:07 +1100165 curr_zone_start = zone->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 /* now do the other zones */
168 for (i = 1; i < conf->nr_strip_zones; i++)
169 {
170 zone = conf->strip_zone + i;
171 zone->dev = conf->strip_zone[i-1].dev + mddev->raid_disks;
172
Andre Noll0825b87a2009-01-09 08:31:07 +1100173 printk(KERN_INFO "raid0: zone %d\n", i);
Andre Noll6b8796c2009-01-09 08:31:07 +1100174 zone->dev_start = current_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 smallest = NULL;
176 c = 0;
177
178 for (j=0; j<cnt; j++) {
179 char b[BDEVNAME_SIZE];
180 rdev = conf->strip_zone[0].dev[j];
Andre Noll0825b87a2009-01-09 08:31:07 +1100181 printk(KERN_INFO "raid0: checking %s ...",
182 bdevname(rdev->bdev, b));
Andre Noll6b8796c2009-01-09 08:31:07 +1100183 if (rdev->size > current_start / 2) {
Andre Noll0825b87a2009-01-09 08:31:07 +1100184 printk(KERN_INFO " contained as device %d\n",
185 c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 zone->dev[c] = rdev;
187 c++;
188 if (!smallest || (rdev->size <smallest->size)) {
189 smallest = rdev;
Andre Noll0825b87a2009-01-09 08:31:07 +1100190 printk(KERN_INFO " (%llu) is smallest!.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 (unsigned long long)rdev->size);
192 }
193 } else
Andre Noll0825b87a2009-01-09 08:31:07 +1100194 printk(KERN_INFO " nope.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
196
197 zone->nb_dev = c;
Andre Noll83838ed2009-01-09 08:31:07 +1100198 zone->sectors = (smallest->size * 2 - current_start) * c;
199 printk(KERN_INFO "raid0: zone->nb_dev: %d, sectors: %llu\n",
200 zone->nb_dev, (unsigned long long)zone->sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Andre Noll6b8796c2009-01-09 08:31:07 +1100202 zone->zone_start = curr_zone_start;
Andre Noll83838ed2009-01-09 08:31:07 +1100203 curr_zone_start += zone->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Andre Noll6b8796c2009-01-09 08:31:07 +1100205 current_start = smallest->size * 2;
206 printk(KERN_INFO "raid0: current zone start: %llu\n",
207 (unsigned long long)current_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
209
210 /* Now find appropriate hash spacing.
211 * We want a number which causes most hash entries to cover
212 * at most two strips, but the hash table must be at most
213 * 1 PAGE. We choose the smallest strip, or contiguous collection
214 * of strips, that has big enough size. We never consider the last
215 * strip though as it's size has no bearing on the efficacy of the hash
216 * table.
217 */
Andre Nollccacc7d2009-01-09 08:31:08 +1100218 conf->spacing = curr_zone_start;
219 min_spacing = curr_zone_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 sector_div(min_spacing, PAGE_SIZE/sizeof(struct strip_zone*));
221 for (i=0; i < conf->nr_strip_zones-1; i++) {
Andre Nollccacc7d2009-01-09 08:31:08 +1100222 sector_t s = 0;
223 for (j = i; j < conf->nr_strip_zones - 1 &&
224 s < min_spacing; j++)
225 s += conf->strip_zone[j].sectors;
226 if (s >= min_spacing && s < conf->spacing)
227 conf->spacing = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
229
230 mddev->queue->unplug_fn = raid0_unplug;
231
NeilBrown26be34d2006-10-03 01:15:53 -0700232 mddev->queue->backing_dev_info.congested_fn = raid0_congested;
233 mddev->queue->backing_dev_info.congested_data = mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Andre Noll0825b87a2009-01-09 08:31:07 +1100235 printk(KERN_INFO "raid0: done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return 0;
237 abort:
238 return 1;
239}
240
241/**
242 * raid0_mergeable_bvec -- tell bio layer if a two requests can be merged
243 * @q: request queue
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200244 * @bvm: properties of new bio
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 * @biovec: the request that could be merged to it.
246 *
247 * Return amount of bytes we can accept at this offset
248 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200249static int raid0_mergeable_bvec(struct request_queue *q,
250 struct bvec_merge_data *bvm,
251 struct bio_vec *biovec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 mddev_t *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200254 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 int max;
256 unsigned int chunk_sectors = mddev->chunk_size >> 9;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200257 unsigned int bio_sectors = bvm->bi_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
260 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
261 if (max <= biovec->bv_len && bio_sectors == 0)
262 return biovec->bv_len;
263 else
264 return max;
265}
266
267static int raid0_run (mddev_t *mddev)
268{
269 unsigned cur=0, i=0, nb_zone;
Andre Nollccacc7d2009-01-09 08:31:08 +1100270 s64 sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 raid0_conf_t *conf;
272 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
NeilBrown2604b702006-01-06 00:20:36 -0800274 if (mddev->chunk_size == 0) {
275 printk(KERN_ERR "md/raid0: non-zero chunk size required.\n");
276 return -EINVAL;
277 }
278 printk(KERN_INFO "%s: setting max_sectors to %d, segment boundary to %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 mdname(mddev),
280 mddev->chunk_size >> 9,
281 (mddev->chunk_size>>1)-1);
282 blk_queue_max_sectors(mddev->queue, mddev->chunk_size >> 9);
283 blk_queue_segment_boundary(mddev->queue, (mddev->chunk_size>>1) - 1);
Neil Browne7e72bf2008-05-14 16:05:54 -0700284 mddev->queue->queue_lock = &mddev->queue->__queue_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 conf = kmalloc(sizeof (raid0_conf_t), GFP_KERNEL);
287 if (!conf)
288 goto out;
289 mddev->private = (void *)conf;
290
291 conf->strip_zone = NULL;
292 conf->devlist = NULL;
293 if (create_strip_zones (mddev))
294 goto out_free_conf;
295
296 /* calculate array device size */
Andre Nollf233ea52008-07-21 17:05:22 +1000297 mddev->array_sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100298 list_for_each_entry(rdev, &mddev->disks, same_set)
Andre Nollf233ea52008-07-21 17:05:22 +1000299 mddev->array_sectors += rdev->size * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Andre Nollccacc7d2009-01-09 08:31:08 +1100301 printk(KERN_INFO "raid0 : md_size is %llu sectors.\n",
302 (unsigned long long)mddev->array_sectors);
303 printk(KERN_INFO "raid0 : conf->spacing is %llu sectors.\n",
304 (unsigned long long)conf->spacing);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 {
Andre Nollccacc7d2009-01-09 08:31:08 +1100306 sector_t s = mddev->array_sectors;
307 sector_t space = conf->spacing;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 int round;
Andre Nollccacc7d2009-01-09 08:31:08 +1100309 conf->sector_shift = 0;
Neil Brown1eb29122005-07-15 03:56:27 -0700310 if (sizeof(sector_t) > sizeof(u32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 /*shift down space and s so that sector_div will work */
Neil Brown1eb29122005-07-15 03:56:27 -0700312 while (space > (sector_t) (~(u32)0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 s >>= 1;
314 space >>= 1;
315 s += 1; /* force round-up */
Andre Nollccacc7d2009-01-09 08:31:08 +1100316 conf->sector_shift++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
318 }
Neil Brown1eb29122005-07-15 03:56:27 -0700319 round = sector_div(s, (u32)space) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 nb_zone = s + round;
321 }
Andre Nollccacc7d2009-01-09 08:31:08 +1100322 printk(KERN_INFO "raid0 : nb_zone is %d.\n", nb_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Andre Nollccacc7d2009-01-09 08:31:08 +1100324 printk(KERN_INFO "raid0 : Allocating %zu bytes for hash.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 nb_zone*sizeof(struct strip_zone*));
326 conf->hash_table = kmalloc (sizeof (struct strip_zone *)*nb_zone, GFP_KERNEL);
327 if (!conf->hash_table)
328 goto out_free_conf;
Andre Nollccacc7d2009-01-09 08:31:08 +1100329 sectors = conf->strip_zone[cur].sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
NeilBrown5c4c3332006-05-22 22:35:26 -0700331 conf->hash_table[0] = conf->strip_zone + cur;
332 for (i=1; i< nb_zone; i++) {
Andre Nollccacc7d2009-01-09 08:31:08 +1100333 while (sectors <= conf->spacing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 cur++;
Andre Nollccacc7d2009-01-09 08:31:08 +1100335 sectors += conf->strip_zone[cur].sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
Andre Nollccacc7d2009-01-09 08:31:08 +1100337 sectors -= conf->spacing;
NeilBrown5c4c3332006-05-22 22:35:26 -0700338 conf->hash_table[i] = conf->strip_zone + cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
Andre Nollccacc7d2009-01-09 08:31:08 +1100340 if (conf->sector_shift) {
341 conf->spacing >>= conf->sector_shift;
342 /* round spacing up so when we divide by it, we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 * err on the side of too-low, which is safest
344 */
Andre Nollccacc7d2009-01-09 08:31:08 +1100345 conf->spacing++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347
348 /* calculate the max read-ahead size.
349 * For read-ahead of large files to be effective, we need to
350 * readahead at least twice a whole stripe. i.e. number of devices
351 * multiplied by chunk size times 2.
352 * If an individual device has an ra_pages greater than the
353 * chunk size, then we will not drive that device as hard as it
354 * wants. We consider this a configuration error: a larger
355 * chunksize should be used in that case.
356 */
357 {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800358 int stripe = mddev->raid_disks * mddev->chunk_size / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
360 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
361 }
362
363
364 blk_queue_merge_bvec(mddev->queue, raid0_mergeable_bvec);
365 return 0;
366
367out_free_conf:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700368 kfree(conf->strip_zone);
369 kfree(conf->devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 kfree(conf);
371 mddev->private = NULL;
372out:
NeilBrown29fc7e32006-02-03 03:03:41 -0800373 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
376static int raid0_stop (mddev_t *mddev)
377{
378 raid0_conf_t *conf = mddev_to_conf(mddev);
379
380 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700381 kfree(conf->hash_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 conf->hash_table = NULL;
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700383 kfree(conf->strip_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 conf->strip_zone = NULL;
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700385 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 mddev->private = NULL;
387
388 return 0;
389}
390
Jens Axboe165125e2007-07-24 09:28:11 +0200391static int raid0_make_request (struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 mddev_t *mddev = q->queuedata;
Andre Nolla4712002009-01-09 08:31:06 +1100394 unsigned int sect_in_chunk, chunksect_bits, chunk_sects;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 raid0_conf_t *conf = mddev_to_conf(mddev);
396 struct strip_zone *zone;
397 mdk_rdev_t *tmp_dev;
NeilBrown787f17f2007-05-23 13:58:09 -0700398 sector_t chunk;
Andre Nolle0f06862009-01-09 08:31:06 +1100399 sector_t sector, rsect;
Jens Axboea3623572005-11-01 09:26:16 +0100400 const int rw = bio_data_dir(bio);
Tejun Heoc9959052008-08-25 19:47:21 +0900401 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
NeilBrowne5dcdd82005-09-09 16:23:41 -0700403 if (unlikely(bio_barrier(bio))) {
NeilBrown6712ecf2007-09-27 12:47:43 +0200404 bio_endio(bio, -EOPNOTSUPP);
NeilBrowne5dcdd82005-09-09 16:23:41 -0700405 return 0;
406 }
407
Tejun Heo074a7ac2008-08-25 19:56:14 +0900408 cpu = part_stat_lock();
409 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
410 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
411 bio_sectors(bio));
412 part_stat_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 chunk_sects = mddev->chunk_size >> 9;
Andre Noll1b7fdf82009-01-09 08:31:06 +1100415 chunksect_bits = ffz(~chunk_sects);
Andre Nolle0f06862009-01-09 08:31:06 +1100416 sector = bio->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 if (unlikely(chunk_sects < (bio->bi_sector & (chunk_sects - 1)) + (bio->bi_size >> 9))) {
419 struct bio_pair *bp;
420 /* Sanity check -- queue functions should prevent this happening */
421 if (bio->bi_vcnt != 1 ||
422 bio->bi_idx != 0)
423 goto bad_map;
424 /* This is a one page bio that upper layers
425 * refuse to split for us, so we need to split it.
426 */
Denis ChengRq6feef532008-10-09 08:57:05 +0200427 bp = bio_split(bio, chunk_sects - (bio->bi_sector & (chunk_sects - 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (raid0_make_request(q, &bp->bio1))
429 generic_make_request(&bp->bio1);
430 if (raid0_make_request(q, &bp->bio2))
431 generic_make_request(&bp->bio2);
432
433 bio_pair_release(bp);
434 return 0;
435 }
436
437
438 {
Andre Nollccacc7d2009-01-09 08:31:08 +1100439 sector_t x = sector >> conf->sector_shift;
440 sector_div(x, (u32)conf->spacing);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 zone = conf->hash_table[x];
442 }
Andre Noll83838ed2009-01-09 08:31:07 +1100443
444 while (sector >= zone->zone_start + zone->sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 zone++;
Andre Noll83838ed2009-01-09 08:31:07 +1100446
Andre Nolla4712002009-01-09 08:31:06 +1100447 sect_in_chunk = bio->bi_sector & (chunk_sects - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449
450 {
Andre Noll6199d3d2009-01-09 08:31:07 +1100451 sector_t x = (sector - zone->zone_start) >> chunksect_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 sector_div(x, zone->nb_dev);
454 chunk = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Andre Nolle0f06862009-01-09 08:31:06 +1100456 x = sector >> chunksect_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 tmp_dev = zone->dev[sector_div(x, zone->nb_dev)];
458 }
Andre Noll019c4e22009-01-09 08:31:06 +1100459 rsect = (chunk << chunksect_bits) + zone->dev_start + sect_in_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 bio->bi_bdev = tmp_dev->bdev;
462 bio->bi_sector = rsect + tmp_dev->data_offset;
463
464 /*
465 * Let the main block layer submit the IO and resolve recursion:
466 */
467 return 1;
468
469bad_map:
470 printk("raid0_make_request bug: can't convert block across chunks"
Andre Nolla4712002009-01-09 08:31:06 +1100471 " or bigger than %dk %llu %d\n", chunk_sects / 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
473
NeilBrown6712ecf2007-09-27 12:47:43 +0200474 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return 0;
476}
NeilBrown8299d7f2007-10-16 23:30:53 -0700477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478static void raid0_status (struct seq_file *seq, mddev_t *mddev)
479{
480#undef MD_DEBUG
481#ifdef MD_DEBUG
482 int j, k, h;
483 char b[BDEVNAME_SIZE];
484 raid0_conf_t *conf = mddev_to_conf(mddev);
NeilBrown8299d7f2007-10-16 23:30:53 -0700485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 h = 0;
487 for (j = 0; j < conf->nr_strip_zones; j++) {
488 seq_printf(seq, " z%d", j);
489 if (conf->hash_table[h] == conf->strip_zone+j)
NeilBrown8299d7f2007-10-16 23:30:53 -0700490 seq_printf(seq, "(h%d)", h++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 seq_printf(seq, "=[");
492 for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
NeilBrown8299d7f2007-10-16 23:30:53 -0700493 seq_printf(seq, "%s/", bdevname(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 conf->strip_zone[j].dev[k]->bdev,b));
495
Andre Noll6199d3d2009-01-09 08:31:07 +1100496 seq_printf(seq, "] zs=%d ds=%d s=%d\n",
497 conf->strip_zone[j].zone_start,
Andre Noll019c4e22009-01-09 08:31:06 +1100498 conf->strip_zone[j].dev_start,
Andre Noll83838ed2009-01-09 08:31:07 +1100499 conf->strip_zone[j].sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501#endif
502 seq_printf(seq, " %dk chunks", mddev->chunk_size/1024);
503 return;
504}
505
NeilBrown2604b702006-01-06 00:20:36 -0800506static struct mdk_personality raid0_personality=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508 .name = "raid0",
NeilBrown2604b702006-01-06 00:20:36 -0800509 .level = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 .owner = THIS_MODULE,
511 .make_request = raid0_make_request,
512 .run = raid0_run,
513 .stop = raid0_stop,
514 .status = raid0_status,
515};
516
517static int __init raid0_init (void)
518{
NeilBrown2604b702006-01-06 00:20:36 -0800519 return register_md_personality (&raid0_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
522static void raid0_exit (void)
523{
NeilBrown2604b702006-01-06 00:20:36 -0800524 unregister_md_personality (&raid0_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
527module_init(raid0_init);
528module_exit(raid0_exit);
529MODULE_LICENSE("GPL");
530MODULE_ALIAS("md-personality-2"); /* RAID0 */
NeilBrownd9d166c2006-01-06 00:20:51 -0800531MODULE_ALIAS("md-raid0");
NeilBrown2604b702006-01-06 00:20:36 -0800532MODULE_ALIAS("md-level-0");