blob: c08d7559be5531fb01bedb55e4663a0e40092607 [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>
NeilBrownbff61972009-03-31 14:33:13 +110022#include <linux/seq_file.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110023#include "md.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),
Andre Nolldd8ac332009-03-31 14:33:13 +110079 (unsigned long long)rdev1->sectors);
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),
Andre Nolldd8ac332009-03-31 14:33:13 +110082 (unsigned long long)rdev2->sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 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 }
Andre Nolldd8ac332009-03-31 14:33:13 +110087 if (rdev2->sectors == rdev1->sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 /*
89 * Not unique, don't count it as a new
90 * group
91 */
Andre Noll0825b87a2009-01-09 08:31:07 +110092 printk(KERN_INFO "raid0: EQUAL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 c = 1;
94 break;
95 }
Andre Noll0825b87a2009-01-09 08:31:07 +110096 printk(KERN_INFO "raid0: NOT EQUAL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 }
98 if (!c) {
Andre Noll0825b87a2009-01-09 08:31:07 +110099 printk(KERN_INFO "raid0: ==> UNIQUE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 conf->nr_strip_zones++;
Andre Noll0825b87a2009-01-09 08:31:07 +1100101 printk(KERN_INFO "raid0: %d zones\n",
102 conf->nr_strip_zones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
104 }
Andre Noll0825b87a2009-01-09 08:31:07 +1100105 printk(KERN_INFO "raid0: FINAL %d zones\n", conf->nr_strip_zones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
NeilBrown9ffae0c2006-01-06 00:20:32 -0800107 conf->strip_zone = kzalloc(sizeof(struct strip_zone)*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 conf->nr_strip_zones, GFP_KERNEL);
109 if (!conf->strip_zone)
110 return 1;
NeilBrown9ffae0c2006-01-06 00:20:32 -0800111 conf->devlist = kzalloc(sizeof(mdk_rdev_t*)*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 conf->nr_strip_zones*mddev->raid_disks,
113 GFP_KERNEL);
114 if (!conf->devlist)
115 return 1;
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 /* The first zone must contain all devices, so here we check that
118 * there is a proper alignment of slots to devices and find them all
119 */
120 zone = &conf->strip_zone[0];
121 cnt = 0;
122 smallest = NULL;
123 zone->dev = conf->devlist;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100124 list_for_each_entry(rdev1, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 int j = rdev1->raid_disk;
126
127 if (j < 0 || j >= mddev->raid_disks) {
Andre Noll0825b87a2009-01-09 08:31:07 +1100128 printk(KERN_ERR "raid0: bad disk number %d - "
129 "aborting!\n", j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 goto abort;
131 }
132 if (zone->dev[j]) {
Andre Noll0825b87a2009-01-09 08:31:07 +1100133 printk(KERN_ERR "raid0: multiple devices for %d - "
134 "aborting!\n", j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 goto abort;
136 }
137 zone->dev[j] = rdev1;
138
139 blk_queue_stack_limits(mddev->queue,
140 rdev1->bdev->bd_disk->queue);
141 /* as we don't honour merge_bvec_fn, we must never risk
142 * violating it, so limit ->max_sector to one PAGE, as
143 * a one page request is never in violation.
144 */
145
146 if (rdev1->bdev->bd_disk->queue->merge_bvec_fn &&
147 mddev->queue->max_sectors > (PAGE_SIZE>>9))
148 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
149
Andre Nolldd8ac332009-03-31 14:33:13 +1100150 if (!smallest || (rdev1->sectors < smallest->sectors))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 smallest = rdev1;
152 cnt++;
153 }
154 if (cnt != mddev->raid_disks) {
Andre Noll0825b87a2009-01-09 08:31:07 +1100155 printk(KERN_ERR "raid0: too few disks (%d of %d) - "
156 "aborting!\n", cnt, mddev->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 goto abort;
158 }
159 zone->nb_dev = cnt;
Andre Nolldd8ac332009-03-31 14:33:13 +1100160 zone->sectors = smallest->sectors * cnt;
Andre Noll6199d3d2009-01-09 08:31:07 +1100161 zone->zone_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Andre Nolldd8ac332009-03-31 14:33:13 +1100163 current_start = smallest->sectors;
Andre Noll83838ed2009-01-09 08:31:07 +1100164 curr_zone_start = zone->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 /* now do the other zones */
167 for (i = 1; i < conf->nr_strip_zones; i++)
168 {
169 zone = conf->strip_zone + i;
170 zone->dev = conf->strip_zone[i-1].dev + mddev->raid_disks;
171
Andre Noll0825b87a2009-01-09 08:31:07 +1100172 printk(KERN_INFO "raid0: zone %d\n", i);
Andre Noll6b8796c2009-01-09 08:31:07 +1100173 zone->dev_start = current_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 smallest = NULL;
175 c = 0;
176
177 for (j=0; j<cnt; j++) {
178 char b[BDEVNAME_SIZE];
179 rdev = conf->strip_zone[0].dev[j];
Andre Noll0825b87a2009-01-09 08:31:07 +1100180 printk(KERN_INFO "raid0: checking %s ...",
181 bdevname(rdev->bdev, b));
Andre Nolldd8ac332009-03-31 14:33:13 +1100182 if (rdev->sectors <= current_start) {
Andre Noll0825b87a2009-01-09 08:31:07 +1100183 printk(KERN_INFO " nope.\n");
Andre Nolldd8ac332009-03-31 14:33:13 +1100184 continue;
185 }
186 printk(KERN_INFO " contained as device %d\n", c);
187 zone->dev[c] = rdev;
188 c++;
189 if (!smallest || rdev->sectors < smallest->sectors) {
190 smallest = rdev;
191 printk(KERN_INFO " (%llu) is smallest!.\n",
192 (unsigned long long)rdev->sectors);
193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
195
196 zone->nb_dev = c;
Andre Nolldd8ac332009-03-31 14:33:13 +1100197 zone->sectors = (smallest->sectors - current_start) * c;
Andre Noll83838ed2009-01-09 08:31:07 +1100198 printk(KERN_INFO "raid0: zone->nb_dev: %d, sectors: %llu\n",
199 zone->nb_dev, (unsigned long long)zone->sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Andre Noll6b8796c2009-01-09 08:31:07 +1100201 zone->zone_start = curr_zone_start;
Andre Noll83838ed2009-01-09 08:31:07 +1100202 curr_zone_start += zone->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Andre Nolldd8ac332009-03-31 14:33:13 +1100204 current_start = smallest->sectors;
Andre Noll6b8796c2009-01-09 08:31:07 +1100205 printk(KERN_INFO "raid0: current zone start: %llu\n",
206 (unsigned long long)current_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208
209 /* Now find appropriate hash spacing.
210 * We want a number which causes most hash entries to cover
211 * at most two strips, but the hash table must be at most
212 * 1 PAGE. We choose the smallest strip, or contiguous collection
213 * of strips, that has big enough size. We never consider the last
214 * strip though as it's size has no bearing on the efficacy of the hash
215 * table.
216 */
Andre Nollccacc7d2009-01-09 08:31:08 +1100217 conf->spacing = curr_zone_start;
218 min_spacing = curr_zone_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 sector_div(min_spacing, PAGE_SIZE/sizeof(struct strip_zone*));
220 for (i=0; i < conf->nr_strip_zones-1; i++) {
Andre Nollccacc7d2009-01-09 08:31:08 +1100221 sector_t s = 0;
222 for (j = i; j < conf->nr_strip_zones - 1 &&
223 s < min_spacing; j++)
224 s += conf->strip_zone[j].sectors;
225 if (s >= min_spacing && s < conf->spacing)
226 conf->spacing = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
228
229 mddev->queue->unplug_fn = raid0_unplug;
230
NeilBrown26be34d2006-10-03 01:15:53 -0700231 mddev->queue->backing_dev_info.congested_fn = raid0_congested;
232 mddev->queue->backing_dev_info.congested_data = mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Andre Noll0825b87a2009-01-09 08:31:07 +1100234 printk(KERN_INFO "raid0: done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return 0;
236 abort:
237 return 1;
238}
239
240/**
241 * raid0_mergeable_bvec -- tell bio layer if a two requests can be merged
242 * @q: request queue
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200243 * @bvm: properties of new bio
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 * @biovec: the request that could be merged to it.
245 *
246 * Return amount of bytes we can accept at this offset
247 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200248static int raid0_mergeable_bvec(struct request_queue *q,
249 struct bvec_merge_data *bvm,
250 struct bio_vec *biovec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 mddev_t *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200253 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 int max;
255 unsigned int chunk_sectors = mddev->chunk_size >> 9;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200256 unsigned int bio_sectors = bvm->bi_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
259 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
260 if (max <= biovec->bv_len && bio_sectors == 0)
261 return biovec->bv_len;
262 else
263 return max;
264}
265
Dan Williams80c3a6c2009-03-17 18:10:40 -0700266static sector_t raid0_size(mddev_t *mddev, sector_t sectors, int raid_disks)
267{
268 sector_t array_sectors = 0;
269 mdk_rdev_t *rdev;
270
271 WARN_ONCE(sectors || raid_disks,
272 "%s does not support generic reshape\n", __func__);
273
274 list_for_each_entry(rdev, &mddev->disks, same_set)
275 array_sectors += rdev->sectors;
276
277 return array_sectors;
278}
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280static int raid0_run (mddev_t *mddev)
281{
282 unsigned cur=0, i=0, nb_zone;
Andre Nollccacc7d2009-01-09 08:31:08 +1100283 s64 sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 raid0_conf_t *conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
NeilBrown2604b702006-01-06 00:20:36 -0800286 if (mddev->chunk_size == 0) {
287 printk(KERN_ERR "md/raid0: non-zero chunk size required.\n");
288 return -EINVAL;
289 }
290 printk(KERN_INFO "%s: setting max_sectors to %d, segment boundary to %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 mdname(mddev),
292 mddev->chunk_size >> 9,
293 (mddev->chunk_size>>1)-1);
294 blk_queue_max_sectors(mddev->queue, mddev->chunk_size >> 9);
295 blk_queue_segment_boundary(mddev->queue, (mddev->chunk_size>>1) - 1);
Neil Browne7e72bf2008-05-14 16:05:54 -0700296 mddev->queue->queue_lock = &mddev->queue->__queue_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 conf = kmalloc(sizeof (raid0_conf_t), GFP_KERNEL);
299 if (!conf)
300 goto out;
301 mddev->private = (void *)conf;
302
303 conf->strip_zone = NULL;
304 conf->devlist = NULL;
305 if (create_strip_zones (mddev))
306 goto out_free_conf;
307
308 /* calculate array device size */
Dan Williams1f403622009-03-31 14:59:03 +1100309 md_set_array_sectors(mddev, raid0_size(mddev, 0, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Andre Nollccacc7d2009-01-09 08:31:08 +1100311 printk(KERN_INFO "raid0 : md_size is %llu sectors.\n",
312 (unsigned long long)mddev->array_sectors);
313 printk(KERN_INFO "raid0 : conf->spacing is %llu sectors.\n",
314 (unsigned long long)conf->spacing);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 {
Dan Williamsb522adc2009-03-31 15:00:31 +1100316 sector_t s = raid0_size(mddev, 0, 0);
Andre Nollccacc7d2009-01-09 08:31:08 +1100317 sector_t space = conf->spacing;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 int round;
Andre Nollccacc7d2009-01-09 08:31:08 +1100319 conf->sector_shift = 0;
Neil Brown1eb29122005-07-15 03:56:27 -0700320 if (sizeof(sector_t) > sizeof(u32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 /*shift down space and s so that sector_div will work */
Neil Brown1eb29122005-07-15 03:56:27 -0700322 while (space > (sector_t) (~(u32)0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 s >>= 1;
324 space >>= 1;
325 s += 1; /* force round-up */
Andre Nollccacc7d2009-01-09 08:31:08 +1100326 conf->sector_shift++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
328 }
Neil Brown1eb29122005-07-15 03:56:27 -0700329 round = sector_div(s, (u32)space) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 nb_zone = s + round;
331 }
Andre Nollccacc7d2009-01-09 08:31:08 +1100332 printk(KERN_INFO "raid0 : nb_zone is %d.\n", nb_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Andre Nollccacc7d2009-01-09 08:31:08 +1100334 printk(KERN_INFO "raid0 : Allocating %zu bytes for hash.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 nb_zone*sizeof(struct strip_zone*));
336 conf->hash_table = kmalloc (sizeof (struct strip_zone *)*nb_zone, GFP_KERNEL);
337 if (!conf->hash_table)
338 goto out_free_conf;
Andre Nollccacc7d2009-01-09 08:31:08 +1100339 sectors = conf->strip_zone[cur].sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
NeilBrown5c4c3332006-05-22 22:35:26 -0700341 conf->hash_table[0] = conf->strip_zone + cur;
342 for (i=1; i< nb_zone; i++) {
Andre Nollccacc7d2009-01-09 08:31:08 +1100343 while (sectors <= conf->spacing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 cur++;
Andre Nollccacc7d2009-01-09 08:31:08 +1100345 sectors += conf->strip_zone[cur].sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
Andre Nollccacc7d2009-01-09 08:31:08 +1100347 sectors -= conf->spacing;
NeilBrown5c4c3332006-05-22 22:35:26 -0700348 conf->hash_table[i] = conf->strip_zone + cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
Andre Nollccacc7d2009-01-09 08:31:08 +1100350 if (conf->sector_shift) {
351 conf->spacing >>= conf->sector_shift;
352 /* round spacing up so when we divide by it, we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 * err on the side of too-low, which is safest
354 */
Andre Nollccacc7d2009-01-09 08:31:08 +1100355 conf->spacing++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
357
358 /* calculate the max read-ahead size.
359 * For read-ahead of large files to be effective, we need to
360 * readahead at least twice a whole stripe. i.e. number of devices
361 * multiplied by chunk size times 2.
362 * If an individual device has an ra_pages greater than the
363 * chunk size, then we will not drive that device as hard as it
364 * wants. We consider this a configuration error: a larger
365 * chunksize should be used in that case.
366 */
367 {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800368 int stripe = mddev->raid_disks * mddev->chunk_size / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
370 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
371 }
372
373
374 blk_queue_merge_bvec(mddev->queue, raid0_mergeable_bvec);
375 return 0;
376
377out_free_conf:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700378 kfree(conf->strip_zone);
379 kfree(conf->devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 kfree(conf);
381 mddev->private = NULL;
382out:
NeilBrown29fc7e32006-02-03 03:03:41 -0800383 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
386static int raid0_stop (mddev_t *mddev)
387{
388 raid0_conf_t *conf = mddev_to_conf(mddev);
389
390 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700391 kfree(conf->hash_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 conf->hash_table = NULL;
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700393 kfree(conf->strip_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 conf->strip_zone = NULL;
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700395 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 mddev->private = NULL;
397
398 return 0;
399}
400
Jens Axboe165125e2007-07-24 09:28:11 +0200401static int raid0_make_request (struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
403 mddev_t *mddev = q->queuedata;
Andre Nolla4712002009-01-09 08:31:06 +1100404 unsigned int sect_in_chunk, chunksect_bits, chunk_sects;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 raid0_conf_t *conf = mddev_to_conf(mddev);
406 struct strip_zone *zone;
407 mdk_rdev_t *tmp_dev;
NeilBrown787f17f2007-05-23 13:58:09 -0700408 sector_t chunk;
Andre Nolle0f06862009-01-09 08:31:06 +1100409 sector_t sector, rsect;
Jens Axboea3623572005-11-01 09:26:16 +0100410 const int rw = bio_data_dir(bio);
Tejun Heoc9959052008-08-25 19:47:21 +0900411 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
NeilBrowne5dcdd82005-09-09 16:23:41 -0700413 if (unlikely(bio_barrier(bio))) {
NeilBrown6712ecf2007-09-27 12:47:43 +0200414 bio_endio(bio, -EOPNOTSUPP);
NeilBrowne5dcdd82005-09-09 16:23:41 -0700415 return 0;
416 }
417
Tejun Heo074a7ac2008-08-25 19:56:14 +0900418 cpu = part_stat_lock();
419 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
420 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
421 bio_sectors(bio));
422 part_stat_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 chunk_sects = mddev->chunk_size >> 9;
Andre Noll1b7fdf82009-01-09 08:31:06 +1100425 chunksect_bits = ffz(~chunk_sects);
Andre Nolle0f06862009-01-09 08:31:06 +1100426 sector = bio->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 if (unlikely(chunk_sects < (bio->bi_sector & (chunk_sects - 1)) + (bio->bi_size >> 9))) {
429 struct bio_pair *bp;
430 /* Sanity check -- queue functions should prevent this happening */
431 if (bio->bi_vcnt != 1 ||
432 bio->bi_idx != 0)
433 goto bad_map;
434 /* This is a one page bio that upper layers
435 * refuse to split for us, so we need to split it.
436 */
Denis ChengRq6feef532008-10-09 08:57:05 +0200437 bp = bio_split(bio, chunk_sects - (bio->bi_sector & (chunk_sects - 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (raid0_make_request(q, &bp->bio1))
439 generic_make_request(&bp->bio1);
440 if (raid0_make_request(q, &bp->bio2))
441 generic_make_request(&bp->bio2);
442
443 bio_pair_release(bp);
444 return 0;
445 }
446
447
448 {
Andre Nollccacc7d2009-01-09 08:31:08 +1100449 sector_t x = sector >> conf->sector_shift;
450 sector_div(x, (u32)conf->spacing);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 zone = conf->hash_table[x];
452 }
Andre Noll83838ed2009-01-09 08:31:07 +1100453
454 while (sector >= zone->zone_start + zone->sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 zone++;
Andre Noll83838ed2009-01-09 08:31:07 +1100456
Andre Nolla4712002009-01-09 08:31:06 +1100457 sect_in_chunk = bio->bi_sector & (chunk_sects - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459
460 {
Andre Noll6199d3d2009-01-09 08:31:07 +1100461 sector_t x = (sector - zone->zone_start) >> chunksect_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 sector_div(x, zone->nb_dev);
464 chunk = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Andre Nolle0f06862009-01-09 08:31:06 +1100466 x = sector >> chunksect_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 tmp_dev = zone->dev[sector_div(x, zone->nb_dev)];
468 }
Andre Noll019c4e22009-01-09 08:31:06 +1100469 rsect = (chunk << chunksect_bits) + zone->dev_start + sect_in_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 bio->bi_bdev = tmp_dev->bdev;
472 bio->bi_sector = rsect + tmp_dev->data_offset;
473
474 /*
475 * Let the main block layer submit the IO and resolve recursion:
476 */
477 return 1;
478
479bad_map:
480 printk("raid0_make_request bug: can't convert block across chunks"
Andre Nolla4712002009-01-09 08:31:06 +1100481 " or bigger than %dk %llu %d\n", chunk_sects / 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
483
NeilBrown6712ecf2007-09-27 12:47:43 +0200484 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return 0;
486}
NeilBrown8299d7f2007-10-16 23:30:53 -0700487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488static void raid0_status (struct seq_file *seq, mddev_t *mddev)
489{
490#undef MD_DEBUG
491#ifdef MD_DEBUG
492 int j, k, h;
493 char b[BDEVNAME_SIZE];
494 raid0_conf_t *conf = mddev_to_conf(mddev);
NeilBrown8299d7f2007-10-16 23:30:53 -0700495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 h = 0;
497 for (j = 0; j < conf->nr_strip_zones; j++) {
498 seq_printf(seq, " z%d", j);
499 if (conf->hash_table[h] == conf->strip_zone+j)
NeilBrown8299d7f2007-10-16 23:30:53 -0700500 seq_printf(seq, "(h%d)", h++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 seq_printf(seq, "=[");
502 for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
NeilBrown8299d7f2007-10-16 23:30:53 -0700503 seq_printf(seq, "%s/", bdevname(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 conf->strip_zone[j].dev[k]->bdev,b));
505
Andre Noll6199d3d2009-01-09 08:31:07 +1100506 seq_printf(seq, "] zs=%d ds=%d s=%d\n",
507 conf->strip_zone[j].zone_start,
Andre Noll019c4e22009-01-09 08:31:06 +1100508 conf->strip_zone[j].dev_start,
Andre Noll83838ed2009-01-09 08:31:07 +1100509 conf->strip_zone[j].sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
511#endif
512 seq_printf(seq, " %dk chunks", mddev->chunk_size/1024);
513 return;
514}
515
NeilBrown2604b702006-01-06 00:20:36 -0800516static struct mdk_personality raid0_personality=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 .name = "raid0",
NeilBrown2604b702006-01-06 00:20:36 -0800519 .level = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 .owner = THIS_MODULE,
521 .make_request = raid0_make_request,
522 .run = raid0_run,
523 .stop = raid0_stop,
524 .status = raid0_status,
Dan Williams80c3a6c2009-03-17 18:10:40 -0700525 .size = raid0_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526};
527
528static int __init raid0_init (void)
529{
NeilBrown2604b702006-01-06 00:20:36 -0800530 return register_md_personality (&raid0_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
533static void raid0_exit (void)
534{
NeilBrown2604b702006-01-06 00:20:36 -0800535 unregister_md_personality (&raid0_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
538module_init(raid0_init);
539module_exit(raid0_exit);
540MODULE_LICENSE("GPL");
541MODULE_ALIAS("md-personality-2"); /* RAID0 */
NeilBrownd9d166c2006-01-06 00:20:51 -0800542MODULE_ALIAS("md-raid0");
NeilBrown2604b702006-01-06 00:20:36 -0800543MODULE_ALIAS("md-level-0");