blob: bb245a6d16c8d6cf83a1e4f9a56eebd07ffbc812 [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;
Andre Noll6b8796c2009-01-09 08:31:07 +110058 sector_t current_start, curr_zone_start;
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 Noll0825b87a2009-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 Noll0825b87a2009-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 Noll0825b87a2009-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 Noll0825b87a2009-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 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;
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 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 &&
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 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 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
Andre Nolldd8ac332009-03-31 14:33:13 +1100162 current_start = smallest->sectors;
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 Nolldd8ac332009-03-31 14:33:13 +1100181 if (rdev->sectors <= current_start) {
Andre Noll0825b87a2009-01-09 08:31:07 +1100182 printk(KERN_INFO " nope.\n");
Andre Nolldd8ac332009-03-31 14:33:13 +1100183 continue;
184 }
185 printk(KERN_INFO " contained as device %d\n", c);
186 zone->dev[c] = rdev;
187 c++;
188 if (!smallest || rdev->sectors < smallest->sectors) {
189 smallest = rdev;
190 printk(KERN_INFO " (%llu) is smallest!.\n",
191 (unsigned long long)rdev->sectors);
192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
194
195 zone->nb_dev = c;
Andre Nolldd8ac332009-03-31 14:33:13 +1100196 zone->sectors = (smallest->sectors - current_start) * c;
Andre Noll83838ed2009-01-09 08:31:07 +1100197 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 Nolldc582662009-06-16 16:18:43 +1000200 zone->zone_end = curr_zone_start + zone->sectors;
Andre Noll83838ed2009-01-09 08:31:07 +1100201 curr_zone_start += zone->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Andre Nolldd8ac332009-03-31 14:33:13 +1100203 current_start = smallest->sectors;
Andre Noll6b8796c2009-01-09 08:31:07 +1100204 printk(KERN_INFO "raid0: current zone start: %llu\n",
205 (unsigned long long)current_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 /* Now find appropriate hash spacing.
208 * We want a number which causes most hash entries to cover
209 * at most two strips, but the hash table must be at most
210 * 1 PAGE. We choose the smallest strip, or contiguous collection
211 * of strips, that has big enough size. We never consider the last
212 * strip though as it's size has no bearing on the efficacy of the hash
213 * table.
214 */
Andre Nollccacc7d2009-01-09 08:31:08 +1100215 conf->spacing = curr_zone_start;
216 min_spacing = curr_zone_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 sector_div(min_spacing, PAGE_SIZE/sizeof(struct strip_zone*));
218 for (i=0; i < conf->nr_strip_zones-1; i++) {
Andre Nollccacc7d2009-01-09 08:31:08 +1100219 sector_t s = 0;
220 for (j = i; j < conf->nr_strip_zones - 1 &&
221 s < min_spacing; j++)
222 s += conf->strip_zone[j].sectors;
223 if (s >= min_spacing && s < conf->spacing)
224 conf->spacing = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
226
227 mddev->queue->unplug_fn = raid0_unplug;
228
NeilBrown26be34d2006-10-03 01:15:53 -0700229 mddev->queue->backing_dev_info.congested_fn = raid0_congested;
230 mddev->queue->backing_dev_info.congested_data = mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Andre Noll0825b87a2009-01-09 08:31:07 +1100232 printk(KERN_INFO "raid0: done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return 0;
234 abort:
235 return 1;
236}
237
238/**
239 * raid0_mergeable_bvec -- tell bio layer if a two requests can be merged
240 * @q: request queue
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200241 * @bvm: properties of new bio
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 * @biovec: the request that could be merged to it.
243 *
244 * Return amount of bytes we can accept at this offset
245 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200246static int raid0_mergeable_bvec(struct request_queue *q,
247 struct bvec_merge_data *bvm,
248 struct bio_vec *biovec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 mddev_t *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200251 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 int max;
253 unsigned int chunk_sectors = mddev->chunk_size >> 9;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200254 unsigned int bio_sectors = bvm->bi_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
257 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
258 if (max <= biovec->bv_len && bio_sectors == 0)
259 return biovec->bv_len;
260 else
261 return max;
262}
263
Dan Williams80c3a6c2009-03-17 18:10:40 -0700264static sector_t raid0_size(mddev_t *mddev, sector_t sectors, int raid_disks)
265{
266 sector_t array_sectors = 0;
267 mdk_rdev_t *rdev;
268
269 WARN_ONCE(sectors || raid_disks,
270 "%s does not support generic reshape\n", __func__);
271
272 list_for_each_entry(rdev, &mddev->disks, same_set)
273 array_sectors += rdev->sectors;
274
275 return array_sectors;
276}
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278static int raid0_run (mddev_t *mddev)
279{
280 unsigned cur=0, i=0, nb_zone;
Andre Nollccacc7d2009-01-09 08:31:08 +1100281 s64 sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 raid0_conf_t *conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
NeilBrown2604b702006-01-06 00:20:36 -0800284 if (mddev->chunk_size == 0) {
285 printk(KERN_ERR "md/raid0: non-zero chunk size required.\n");
286 return -EINVAL;
287 }
288 printk(KERN_INFO "%s: setting max_sectors to %d, segment boundary to %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 mdname(mddev),
290 mddev->chunk_size >> 9,
291 (mddev->chunk_size>>1)-1);
292 blk_queue_max_sectors(mddev->queue, mddev->chunk_size >> 9);
293 blk_queue_segment_boundary(mddev->queue, (mddev->chunk_size>>1) - 1);
Neil Browne7e72bf2008-05-14 16:05:54 -0700294 mddev->queue->queue_lock = &mddev->queue->__queue_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 conf = kmalloc(sizeof (raid0_conf_t), GFP_KERNEL);
297 if (!conf)
298 goto out;
299 mddev->private = (void *)conf;
300
301 conf->strip_zone = NULL;
302 conf->devlist = NULL;
303 if (create_strip_zones (mddev))
304 goto out_free_conf;
305
306 /* calculate array device size */
Dan Williams1f403622009-03-31 14:59:03 +1100307 md_set_array_sectors(mddev, raid0_size(mddev, 0, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Andre Nollccacc7d2009-01-09 08:31:08 +1100309 printk(KERN_INFO "raid0 : md_size is %llu sectors.\n",
310 (unsigned long long)mddev->array_sectors);
311 printk(KERN_INFO "raid0 : conf->spacing is %llu sectors.\n",
312 (unsigned long long)conf->spacing);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 {
Dan Williamsb522adc2009-03-31 15:00:31 +1100314 sector_t s = raid0_size(mddev, 0, 0);
Andre Nollccacc7d2009-01-09 08:31:08 +1100315 sector_t space = conf->spacing;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 int round;
Andre Nollccacc7d2009-01-09 08:31:08 +1100317 conf->sector_shift = 0;
Neil Brown1eb29122005-07-15 03:56:27 -0700318 if (sizeof(sector_t) > sizeof(u32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /*shift down space and s so that sector_div will work */
Neil Brown1eb29122005-07-15 03:56:27 -0700320 while (space > (sector_t) (~(u32)0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 s >>= 1;
322 space >>= 1;
323 s += 1; /* force round-up */
Andre Nollccacc7d2009-01-09 08:31:08 +1100324 conf->sector_shift++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
326 }
Neil Brown1eb29122005-07-15 03:56:27 -0700327 round = sector_div(s, (u32)space) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 nb_zone = s + round;
329 }
Andre Nollccacc7d2009-01-09 08:31:08 +1100330 printk(KERN_INFO "raid0 : nb_zone is %d.\n", nb_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Andre Nollccacc7d2009-01-09 08:31:08 +1100332 printk(KERN_INFO "raid0 : Allocating %zu bytes for hash.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 nb_zone*sizeof(struct strip_zone*));
334 conf->hash_table = kmalloc (sizeof (struct strip_zone *)*nb_zone, GFP_KERNEL);
335 if (!conf->hash_table)
336 goto out_free_conf;
Andre Nollccacc7d2009-01-09 08:31:08 +1100337 sectors = conf->strip_zone[cur].sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
NeilBrown5c4c3332006-05-22 22:35:26 -0700339 conf->hash_table[0] = conf->strip_zone + cur;
340 for (i=1; i< nb_zone; i++) {
Andre Nollccacc7d2009-01-09 08:31:08 +1100341 while (sectors <= conf->spacing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 cur++;
Andre Nollccacc7d2009-01-09 08:31:08 +1100343 sectors += conf->strip_zone[cur].sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
Andre Nollccacc7d2009-01-09 08:31:08 +1100345 sectors -= conf->spacing;
NeilBrown5c4c3332006-05-22 22:35:26 -0700346 conf->hash_table[i] = conf->strip_zone + cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
Andre Nollccacc7d2009-01-09 08:31:08 +1100348 if (conf->sector_shift) {
349 conf->spacing >>= conf->sector_shift;
350 /* round spacing up so when we divide by it, we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 * err on the side of too-low, which is safest
352 */
Andre Nollccacc7d2009-01-09 08:31:08 +1100353 conf->spacing++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
355
356 /* calculate the max read-ahead size.
357 * For read-ahead of large files to be effective, we need to
358 * readahead at least twice a whole stripe. i.e. number of devices
359 * multiplied by chunk size times 2.
360 * If an individual device has an ra_pages greater than the
361 * chunk size, then we will not drive that device as hard as it
362 * wants. We consider this a configuration error: a larger
363 * chunksize should be used in that case.
364 */
365 {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800366 int stripe = mddev->raid_disks * mddev->chunk_size / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
368 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
369 }
370
371
372 blk_queue_merge_bvec(mddev->queue, raid0_mergeable_bvec);
373 return 0;
374
375out_free_conf:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700376 kfree(conf->strip_zone);
377 kfree(conf->devlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 kfree(conf);
379 mddev->private = NULL;
380out:
NeilBrown29fc7e32006-02-03 03:03:41 -0800381 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
384static int raid0_stop (mddev_t *mddev)
385{
386 raid0_conf_t *conf = mddev_to_conf(mddev);
387
388 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700389 kfree(conf->hash_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 conf->hash_table = NULL;
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700391 kfree(conf->strip_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 conf->strip_zone = NULL;
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700393 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 mddev->private = NULL;
395
396 return 0;
397}
398
Andre Nolldc582662009-06-16 16:18:43 +1000399/* Find the zone which holds a particular offset */
400static struct strip_zone *find_zone(struct raid0_private_data *conf,
401 sector_t sector)
402{
403 int i;
404 struct strip_zone *z = conf->strip_zone;
405
406 for (i = 0; i < conf->nr_strip_zones; i++)
407 if (sector < z[i].zone_end)
408 return z + i;
409 BUG();
410}
411
Jens Axboe165125e2007-07-24 09:28:11 +0200412static int raid0_make_request (struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 mddev_t *mddev = q->queuedata;
Andre Nolla4712002009-01-09 08:31:06 +1100415 unsigned int sect_in_chunk, chunksect_bits, chunk_sects;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 raid0_conf_t *conf = mddev_to_conf(mddev);
417 struct strip_zone *zone;
418 mdk_rdev_t *tmp_dev;
NeilBrown787f17f2007-05-23 13:58:09 -0700419 sector_t chunk;
Andre Nolle0f06862009-01-09 08:31:06 +1100420 sector_t sector, rsect;
Jens Axboea3623572005-11-01 09:26:16 +0100421 const int rw = bio_data_dir(bio);
Tejun Heoc9959052008-08-25 19:47:21 +0900422 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
NeilBrowne5dcdd82005-09-09 16:23:41 -0700424 if (unlikely(bio_barrier(bio))) {
NeilBrown6712ecf2007-09-27 12:47:43 +0200425 bio_endio(bio, -EOPNOTSUPP);
NeilBrowne5dcdd82005-09-09 16:23:41 -0700426 return 0;
427 }
428
Tejun Heo074a7ac2008-08-25 19:56:14 +0900429 cpu = part_stat_lock();
430 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
431 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
432 bio_sectors(bio));
433 part_stat_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 chunk_sects = mddev->chunk_size >> 9;
Andre Noll1b7fdf82009-01-09 08:31:06 +1100436 chunksect_bits = ffz(~chunk_sects);
Andre Nolle0f06862009-01-09 08:31:06 +1100437 sector = bio->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 if (unlikely(chunk_sects < (bio->bi_sector & (chunk_sects - 1)) + (bio->bi_size >> 9))) {
440 struct bio_pair *bp;
441 /* Sanity check -- queue functions should prevent this happening */
442 if (bio->bi_vcnt != 1 ||
443 bio->bi_idx != 0)
444 goto bad_map;
445 /* This is a one page bio that upper layers
446 * refuse to split for us, so we need to split it.
447 */
Denis ChengRq6feef532008-10-09 08:57:05 +0200448 bp = bio_split(bio, chunk_sects - (bio->bi_sector & (chunk_sects - 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (raid0_make_request(q, &bp->bio1))
450 generic_make_request(&bp->bio1);
451 if (raid0_make_request(q, &bp->bio2))
452 generic_make_request(&bp->bio2);
453
454 bio_pair_release(bp);
455 return 0;
456 }
Andre Nolldc582662009-06-16 16:18:43 +1000457 zone = find_zone(conf, sector);
Andre Nolla4712002009-01-09 08:31:06 +1100458 sect_in_chunk = bio->bi_sector & (chunk_sects - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 {
Andre Nolldc582662009-06-16 16:18:43 +1000460 sector_t x = (zone->sectors + sector - zone->zone_end)
461 >> 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 Nolldc582662009-06-16 16:18:43 +1000506 seq_printf(seq, "] ze=%d ds=%d s=%d\n",
507 conf->strip_zone[j].zone_end,
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");