blob: 1afdfd120bba12aa4d2ed57701aa0f14bc9f94dd [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 -070055static int create_strip_zones (mddev_t *mddev)
56{
57 int i, c, j;
NeilBrownd27a43ab2009-06-16 16:46:46 +100058 sector_t curr_zone_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 sector_t min_spacing;
60 raid0_conf_t *conf = mddev_to_conf(mddev);
61 mdk_rdev_t *smallest, *rdev1, *rdev2, *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 struct strip_zone *zone;
63 int cnt;
64 char b[BDEVNAME_SIZE];
65
66 /*
67 * The number of 'same size groups'
68 */
69 conf->nr_strip_zones = 0;
70
Cheng Renquan159ec1f2009-01-09 08:31:08 +110071 list_for_each_entry(rdev1, &mddev->disks, same_set) {
Andre Noll0825b872009-01-09 08:31:07 +110072 printk(KERN_INFO "raid0: looking at %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 bdevname(rdev1->bdev,b));
74 c = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +110075 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Andre Noll0825b872009-01-09 08:31:07 +110076 printk(KERN_INFO "raid0: comparing %s(%llu)",
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 bdevname(rdev1->bdev,b),
Andre Nolldd8ac332009-03-31 14:33:13 +110078 (unsigned long long)rdev1->sectors);
Andre Noll0825b872009-01-09 08:31:07 +110079 printk(KERN_INFO " with %s(%llu)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 bdevname(rdev2->bdev,b),
Andre Nolldd8ac332009-03-31 14:33:13 +110081 (unsigned long long)rdev2->sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 if (rdev2 == rdev1) {
Andre Noll0825b872009-01-09 08:31:07 +110083 printk(KERN_INFO "raid0: END\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 break;
85 }
Andre Nolldd8ac332009-03-31 14:33:13 +110086 if (rdev2->sectors == rdev1->sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 /*
88 * Not unique, don't count it as a new
89 * group
90 */
Andre Noll0825b872009-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 Noll0825b872009-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 Noll0825b872009-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 Noll0825b872009-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 Noll0825b872009-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;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100123 list_for_each_entry(rdev1, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 int j = rdev1->raid_disk;
125
126 if (j < 0 || j >= mddev->raid_disks) {
Andre Noll0825b872009-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 Noll0825b872009-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 &&
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400146 queue_max_sectors(mddev->queue) > (PAGE_SIZE>>9))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
148
Andre Nolldd8ac332009-03-31 14:33:13 +1100149 if (!smallest || (rdev1->sectors < smallest->sectors))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 smallest = rdev1;
151 cnt++;
152 }
153 if (cnt != mddev->raid_disks) {
Andre Noll0825b872009-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 Nolldd8ac332009-03-31 14:33:13 +1100159 zone->sectors = smallest->sectors * cnt;
Andre Nolldc582662009-06-16 16:18:43 +1000160 zone->zone_end = zone->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
NeilBrownd27a43ab2009-06-16 16:46:46 +1000162 curr_zone_end = zone->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 /* now do the other zones */
165 for (i = 1; i < conf->nr_strip_zones; i++)
166 {
167 zone = conf->strip_zone + i;
168 zone->dev = conf->strip_zone[i-1].dev + mddev->raid_disks;
169
Andre Noll0825b872009-01-09 08:31:07 +1100170 printk(KERN_INFO "raid0: zone %d\n", i);
NeilBrownd27a43ab2009-06-16 16:46:46 +1000171 zone->dev_start = smallest->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 smallest = NULL;
173 c = 0;
174
175 for (j=0; j<cnt; j++) {
176 char b[BDEVNAME_SIZE];
177 rdev = conf->strip_zone[0].dev[j];
Andre Noll0825b872009-01-09 08:31:07 +1100178 printk(KERN_INFO "raid0: checking %s ...",
179 bdevname(rdev->bdev, b));
NeilBrownd27a43ab2009-06-16 16:46:46 +1000180 if (rdev->sectors <= zone->dev_start) {
Andre Noll0825b872009-01-09 08:31:07 +1100181 printk(KERN_INFO " nope.\n");
Andre Nolldd8ac332009-03-31 14:33:13 +1100182 continue;
183 }
184 printk(KERN_INFO " contained as device %d\n", c);
185 zone->dev[c] = rdev;
186 c++;
187 if (!smallest || rdev->sectors < smallest->sectors) {
188 smallest = rdev;
189 printk(KERN_INFO " (%llu) is smallest!.\n",
190 (unsigned long long)rdev->sectors);
191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
193
194 zone->nb_dev = c;
NeilBrownd27a43ab2009-06-16 16:46:46 +1000195 zone->sectors = (smallest->sectors - zone->dev_start) * c;
Andre Noll83838ed2009-01-09 08:31:07 +1100196 printk(KERN_INFO "raid0: zone->nb_dev: %d, sectors: %llu\n",
197 zone->nb_dev, (unsigned long long)zone->sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
NeilBrownd27a43ab2009-06-16 16:46:46 +1000199 curr_zone_end += zone->sectors;
200 zone->zone_end = curr_zone_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Andre Noll6b8796c2009-01-09 08:31:07 +1100202 printk(KERN_INFO "raid0: current zone start: %llu\n",
NeilBrownd27a43ab2009-06-16 16:46:46 +1000203 (unsigned long long)smallest->sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 /* Now find appropriate hash spacing.
206 * We want a number which causes most hash entries to cover
207 * at most two strips, but the hash table must be at most
208 * 1 PAGE. We choose the smallest strip, or contiguous collection
209 * of strips, that has big enough size. We never consider the last
210 * strip though as it's size has no bearing on the efficacy of the hash
211 * table.
212 */
NeilBrownd27a43ab2009-06-16 16:46:46 +1000213 conf->spacing = curr_zone_end;
214 min_spacing = curr_zone_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 sector_div(min_spacing, PAGE_SIZE/sizeof(struct strip_zone*));
216 for (i=0; i < conf->nr_strip_zones-1; i++) {
Andre Nollccacc7d2009-01-09 08:31:08 +1100217 sector_t s = 0;
218 for (j = i; j < conf->nr_strip_zones - 1 &&
219 s < min_spacing; j++)
220 s += conf->strip_zone[j].sectors;
221 if (s >= min_spacing && s < conf->spacing)
222 conf->spacing = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
224
225 mddev->queue->unplug_fn = raid0_unplug;
226
NeilBrown26be34d2006-10-03 01:15:53 -0700227 mddev->queue->backing_dev_info.congested_fn = raid0_congested;
228 mddev->queue->backing_dev_info.congested_data = mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Andre Noll0825b872009-01-09 08:31:07 +1100230 printk(KERN_INFO "raid0: done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return 0;
232 abort:
233 return 1;
234}
235
236/**
237 * raid0_mergeable_bvec -- tell bio layer if a two requests can be merged
238 * @q: request queue
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200239 * @bvm: properties of new bio
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 * @biovec: the request that could be merged to it.
241 *
242 * Return amount of bytes we can accept at this offset
243 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200244static int raid0_mergeable_bvec(struct request_queue *q,
245 struct bvec_merge_data *bvm,
246 struct bio_vec *biovec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 mddev_t *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200249 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 int max;
251 unsigned int chunk_sectors = mddev->chunk_size >> 9;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200252 unsigned int bio_sectors = bvm->bi_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
255 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
256 if (max <= biovec->bv_len && bio_sectors == 0)
257 return biovec->bv_len;
258 else
259 return max;
260}
261
Dan Williams80c3a6c2009-03-17 18:10:40 -0700262static sector_t raid0_size(mddev_t *mddev, sector_t sectors, int raid_disks)
263{
264 sector_t array_sectors = 0;
265 mdk_rdev_t *rdev;
266
267 WARN_ONCE(sectors || raid_disks,
268 "%s does not support generic reshape\n", __func__);
269
270 list_for_each_entry(rdev, &mddev->disks, same_set)
271 array_sectors += rdev->sectors;
272
273 return array_sectors;
274}
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276static int raid0_run (mddev_t *mddev)
277{
278 unsigned cur=0, i=0, nb_zone;
Andre Nollccacc7d2009-01-09 08:31:08 +1100279 s64 sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 raid0_conf_t *conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
NeilBrown2604b702006-01-06 00:20:36 -0800282 if (mddev->chunk_size == 0) {
283 printk(KERN_ERR "md/raid0: non-zero chunk size required.\n");
284 return -EINVAL;
285 }
286 printk(KERN_INFO "%s: setting max_sectors to %d, segment boundary to %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 mdname(mddev),
288 mddev->chunk_size >> 9,
289 (mddev->chunk_size>>1)-1);
290 blk_queue_max_sectors(mddev->queue, mddev->chunk_size >> 9);
291 blk_queue_segment_boundary(mddev->queue, (mddev->chunk_size>>1) - 1);
Neil Browne7e72bf2008-05-14 16:05:54 -0700292 mddev->queue->queue_lock = &mddev->queue->__queue_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 conf = kmalloc(sizeof (raid0_conf_t), GFP_KERNEL);
295 if (!conf)
296 goto out;
297 mddev->private = (void *)conf;
298
299 conf->strip_zone = NULL;
300 conf->devlist = NULL;
301 if (create_strip_zones (mddev))
302 goto out_free_conf;
303
304 /* calculate array device size */
Dan Williams1f403622009-03-31 14:59:03 +1100305 md_set_array_sectors(mddev, raid0_size(mddev, 0, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Andre Nollccacc7d2009-01-09 08:31:08 +1100307 printk(KERN_INFO "raid0 : md_size is %llu sectors.\n",
308 (unsigned long long)mddev->array_sectors);
309 printk(KERN_INFO "raid0 : conf->spacing is %llu sectors.\n",
310 (unsigned long long)conf->spacing);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 {
Dan Williamsb522adc2009-03-31 15:00:31 +1100312 sector_t s = raid0_size(mddev, 0, 0);
Andre Nollccacc7d2009-01-09 08:31:08 +1100313 sector_t space = conf->spacing;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 int round;
Andre Nollccacc7d2009-01-09 08:31:08 +1100315 conf->sector_shift = 0;
Neil Brown1eb29122005-07-15 03:56:27 -0700316 if (sizeof(sector_t) > sizeof(u32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 /*shift down space and s so that sector_div will work */
Neil Brown1eb29122005-07-15 03:56:27 -0700318 while (space > (sector_t) (~(u32)0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 s >>= 1;
320 space >>= 1;
321 s += 1; /* force round-up */
Andre Nollccacc7d2009-01-09 08:31:08 +1100322 conf->sector_shift++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
324 }
Neil Brown1eb29122005-07-15 03:56:27 -0700325 round = sector_div(s, (u32)space) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 nb_zone = s + round;
327 }
Andre Nollccacc7d2009-01-09 08:31:08 +1100328 printk(KERN_INFO "raid0 : nb_zone is %d.\n", nb_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Andre Nollccacc7d2009-01-09 08:31:08 +1100330 printk(KERN_INFO "raid0 : Allocating %zu bytes for hash.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 nb_zone*sizeof(struct strip_zone*));
332 conf->hash_table = kmalloc (sizeof (struct strip_zone *)*nb_zone, GFP_KERNEL);
333 if (!conf->hash_table)
334 goto out_free_conf;
Andre Nollccacc7d2009-01-09 08:31:08 +1100335 sectors = conf->strip_zone[cur].sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
NeilBrown5c4c3332006-05-22 22:35:26 -0700337 conf->hash_table[0] = conf->strip_zone + cur;
338 for (i=1; i< nb_zone; i++) {
Andre Nollccacc7d2009-01-09 08:31:08 +1100339 while (sectors <= conf->spacing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 cur++;
Andre Nollccacc7d2009-01-09 08:31:08 +1100341 sectors += conf->strip_zone[cur].sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
Andre Nollccacc7d2009-01-09 08:31:08 +1100343 sectors -= conf->spacing;
NeilBrown5c4c3332006-05-22 22:35:26 -0700344 conf->hash_table[i] = conf->strip_zone + cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
Andre Nollccacc7d2009-01-09 08:31:08 +1100346 if (conf->sector_shift) {
347 conf->spacing >>= conf->sector_shift;
348 /* round spacing up so when we divide by it, we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 * err on the side of too-low, which is safest
350 */
Andre Nollccacc7d2009-01-09 08:31:08 +1100351 conf->spacing++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
353
354 /* calculate the max read-ahead size.
355 * For read-ahead of large files to be effective, we need to
356 * readahead at least twice a whole stripe. i.e. number of devices
357 * multiplied by chunk size times 2.
358 * If an individual device has an ra_pages greater than the
359 * chunk size, then we will not drive that device as hard as it
360 * wants. We consider this a configuration error: a larger
361 * chunksize should be used in that case.
362 */
363 {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800364 int stripe = mddev->raid_disks * mddev->chunk_size / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
366 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
367 }
368
369
370 blk_queue_merge_bvec(mddev->queue, raid0_mergeable_bvec);
371 return 0;
372
373out_free_conf:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700374 kfree(conf->strip_zone);
375 kfree(conf->devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 kfree(conf);
377 mddev->private = NULL;
378out:
NeilBrown29fc7e32006-02-03 03:03:41 -0800379 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
382static int raid0_stop (mddev_t *mddev)
383{
384 raid0_conf_t *conf = mddev_to_conf(mddev);
385
386 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700387 kfree(conf->hash_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 conf->hash_table = NULL;
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700389 kfree(conf->strip_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 conf->strip_zone = NULL;
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700391 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 mddev->private = NULL;
393
394 return 0;
395}
396
Andre Nolldc582662009-06-16 16:18:43 +1000397/* Find the zone which holds a particular offset */
398static struct strip_zone *find_zone(struct raid0_private_data *conf,
399 sector_t sector)
400{
401 int i;
402 struct strip_zone *z = conf->strip_zone;
403
404 for (i = 0; i < conf->nr_strip_zones; i++)
405 if (sector < z[i].zone_end)
406 return z + i;
407 BUG();
408}
409
Jens Axboe165125e2007-07-24 09:28:11 +0200410static int raid0_make_request (struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
412 mddev_t *mddev = q->queuedata;
Andre Nolla4712002009-01-09 08:31:06 +1100413 unsigned int sect_in_chunk, chunksect_bits, chunk_sects;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 raid0_conf_t *conf = mddev_to_conf(mddev);
415 struct strip_zone *zone;
416 mdk_rdev_t *tmp_dev;
NeilBrown787f17f2007-05-23 13:58:09 -0700417 sector_t chunk;
Andre Nolle0f06862009-01-09 08:31:06 +1100418 sector_t sector, rsect;
Jens Axboea3623572005-11-01 09:26:16 +0100419 const int rw = bio_data_dir(bio);
Tejun Heoc9959052008-08-25 19:47:21 +0900420 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
NeilBrowne5dcdd82005-09-09 16:23:41 -0700422 if (unlikely(bio_barrier(bio))) {
NeilBrown6712ecf2007-09-27 12:47:43 +0200423 bio_endio(bio, -EOPNOTSUPP);
NeilBrowne5dcdd82005-09-09 16:23:41 -0700424 return 0;
425 }
426
Tejun Heo074a7ac2008-08-25 19:56:14 +0900427 cpu = part_stat_lock();
428 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
429 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
430 bio_sectors(bio));
431 part_stat_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 chunk_sects = mddev->chunk_size >> 9;
Andre Noll1b7fdf82009-01-09 08:31:06 +1100434 chunksect_bits = ffz(~chunk_sects);
Andre Nolle0f06862009-01-09 08:31:06 +1100435 sector = bio->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 if (unlikely(chunk_sects < (bio->bi_sector & (chunk_sects - 1)) + (bio->bi_size >> 9))) {
438 struct bio_pair *bp;
439 /* Sanity check -- queue functions should prevent this happening */
440 if (bio->bi_vcnt != 1 ||
441 bio->bi_idx != 0)
442 goto bad_map;
443 /* This is a one page bio that upper layers
444 * refuse to split for us, so we need to split it.
445 */
Denis ChengRq6feef532008-10-09 08:57:05 +0200446 bp = bio_split(bio, chunk_sects - (bio->bi_sector & (chunk_sects - 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (raid0_make_request(q, &bp->bio1))
448 generic_make_request(&bp->bio1);
449 if (raid0_make_request(q, &bp->bio2))
450 generic_make_request(&bp->bio2);
451
452 bio_pair_release(bp);
453 return 0;
454 }
Andre Nolldc582662009-06-16 16:18:43 +1000455 zone = find_zone(conf, sector);
Andre Nolla4712002009-01-09 08:31:06 +1100456 sect_in_chunk = bio->bi_sector & (chunk_sects - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 {
Andre Nolldc582662009-06-16 16:18:43 +1000458 sector_t x = (zone->sectors + sector - zone->zone_end)
459 >> chunksect_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 sector_div(x, zone->nb_dev);
462 chunk = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Andre Nolle0f06862009-01-09 08:31:06 +1100464 x = sector >> chunksect_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 tmp_dev = zone->dev[sector_div(x, zone->nb_dev)];
466 }
Andre Noll019c4e22009-01-09 08:31:06 +1100467 rsect = (chunk << chunksect_bits) + zone->dev_start + sect_in_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 bio->bi_bdev = tmp_dev->bdev;
470 bio->bi_sector = rsect + tmp_dev->data_offset;
471
472 /*
473 * Let the main block layer submit the IO and resolve recursion:
474 */
475 return 1;
476
477bad_map:
478 printk("raid0_make_request bug: can't convert block across chunks"
Andre Nolla4712002009-01-09 08:31:06 +1100479 " or bigger than %dk %llu %d\n", chunk_sects / 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
481
NeilBrown6712ecf2007-09-27 12:47:43 +0200482 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return 0;
484}
NeilBrown8299d7f2007-10-16 23:30:53 -0700485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486static void raid0_status (struct seq_file *seq, mddev_t *mddev)
487{
488#undef MD_DEBUG
489#ifdef MD_DEBUG
490 int j, k, h;
491 char b[BDEVNAME_SIZE];
492 raid0_conf_t *conf = mddev_to_conf(mddev);
NeilBrown8299d7f2007-10-16 23:30:53 -0700493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 h = 0;
495 for (j = 0; j < conf->nr_strip_zones; j++) {
496 seq_printf(seq, " z%d", j);
497 if (conf->hash_table[h] == conf->strip_zone+j)
NeilBrown8299d7f2007-10-16 23:30:53 -0700498 seq_printf(seq, "(h%d)", h++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 seq_printf(seq, "=[");
500 for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
NeilBrown8299d7f2007-10-16 23:30:53 -0700501 seq_printf(seq, "%s/", bdevname(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 conf->strip_zone[j].dev[k]->bdev,b));
503
Andre Nolldc582662009-06-16 16:18:43 +1000504 seq_printf(seq, "] ze=%d ds=%d s=%d\n",
505 conf->strip_zone[j].zone_end,
Andre Noll019c4e22009-01-09 08:31:06 +1100506 conf->strip_zone[j].dev_start,
Andre Noll83838ed2009-01-09 08:31:07 +1100507 conf->strip_zone[j].sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509#endif
510 seq_printf(seq, " %dk chunks", mddev->chunk_size/1024);
511 return;
512}
513
NeilBrown2604b702006-01-06 00:20:36 -0800514static struct mdk_personality raid0_personality=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
516 .name = "raid0",
NeilBrown2604b702006-01-06 00:20:36 -0800517 .level = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 .owner = THIS_MODULE,
519 .make_request = raid0_make_request,
520 .run = raid0_run,
521 .stop = raid0_stop,
522 .status = raid0_status,
Dan Williams80c3a6c2009-03-17 18:10:40 -0700523 .size = raid0_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524};
525
526static int __init raid0_init (void)
527{
NeilBrown2604b702006-01-06 00:20:36 -0800528 return register_md_personality (&raid0_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
531static void raid0_exit (void)
532{
NeilBrown2604b702006-01-06 00:20:36 -0800533 unregister_md_personality (&raid0_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536module_init(raid0_init);
537module_exit(raid0_exit);
538MODULE_LICENSE("GPL");
539MODULE_ALIAS("md-personality-2"); /* RAID0 */
NeilBrownd9d166c2006-01-06 00:20:51 -0800540MODULE_ALIAS("md-raid0");
NeilBrown2604b702006-01-06 00:20:36 -0800541MODULE_ALIAS("md-level-0");