blob: db51e6f6819103c72b1253c7e72117fc67a467e5 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110024#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110025#include "raid0.h"
Trela, Maciej9af204c2010-03-08 16:02:44 +110026#include "raid5.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
NeilBrown26be34d2006-10-03 01:15:53 -070028static int raid0_congested(void *data, int bits)
29{
NeilBrownfd01b882011-10-11 16:47:53 +110030 struct mddev *mddev = data;
NeilBrown070ec552009-06-16 16:54:21 +100031 raid0_conf_t *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +110032 struct md_rdev **devlist = conf->devlist;
NeilBrown84707f32010-03-16 17:23:35 +110033 int raid_disks = conf->strip_zone[0].nb_dev;
NeilBrown26be34d2006-10-03 01:15:53 -070034 int i, ret = 0;
35
NeilBrown3fa841d2009-09-23 18:10:29 +100036 if (mddev_congested(mddev, bits))
37 return 1;
38
NeilBrown84707f32010-03-16 17:23:35 +110039 for (i = 0; i < raid_disks && !ret ; i++) {
Jens Axboe165125e2007-07-24 09:28:11 +020040 struct request_queue *q = bdev_get_queue(devlist[i]->bdev);
NeilBrown26be34d2006-10-03 01:15:53 -070041
42 ret |= bdi_congested(&q->backing_dev_info, bits);
43 }
44 return ret;
45}
46
raz ben yehuda46994192009-06-16 17:00:54 +100047/*
48 * inform the user of the raid configuration
49*/
NeilBrownfd01b882011-10-11 16:47:53 +110050static void dump_zones(struct mddev *mddev)
raz ben yehuda46994192009-06-16 17:00:54 +100051{
NeilBrown50de8df2011-10-07 14:23:22 +110052 int j, k;
raz ben yehuda46994192009-06-16 17:00:54 +100053 sector_t zone_size = 0;
54 sector_t zone_start = 0;
55 char b[BDEVNAME_SIZE];
56 raid0_conf_t *conf = mddev->private;
NeilBrown84707f32010-03-16 17:23:35 +110057 int raid_disks = conf->strip_zone[0].nb_dev;
NeilBrown50de8df2011-10-07 14:23:22 +110058 printk(KERN_INFO "md: RAID0 configuration for %s - %d zone%s\n",
59 mdname(mddev),
60 conf->nr_strip_zones, conf->nr_strip_zones==1?"":"s");
raz ben yehuda46994192009-06-16 17:00:54 +100061 for (j = 0; j < conf->nr_strip_zones; j++) {
NeilBrown50de8df2011-10-07 14:23:22 +110062 printk(KERN_INFO "md: zone%d=[", j);
raz ben yehuda46994192009-06-16 17:00:54 +100063 for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
NeilBrown50de8df2011-10-07 14:23:22 +110064 printk(KERN_CONT "%s%s", k?"/":"",
NeilBrown84707f32010-03-16 17:23:35 +110065 bdevname(conf->devlist[j*raid_disks
raz ben yehuda46994192009-06-16 17:00:54 +100066 + k]->bdev, b));
NeilBrownb5a20962010-05-03 15:06:27 +100067 printk(KERN_CONT "]\n");
raz ben yehuda46994192009-06-16 17:00:54 +100068
69 zone_size = conf->strip_zone[j].zone_end - zone_start;
NeilBrown50de8df2011-10-07 14:23:22 +110070 printk(KERN_INFO " zone-offset=%10lluKB, "
71 "device-offset=%10lluKB, size=%10lluKB\n",
raz ben yehuda46994192009-06-16 17:00:54 +100072 (unsigned long long)zone_start>>1,
73 (unsigned long long)conf->strip_zone[j].dev_start>>1,
74 (unsigned long long)zone_size>>1);
75 zone_start = conf->strip_zone[j].zone_end;
76 }
NeilBrown50de8df2011-10-07 14:23:22 +110077 printk(KERN_INFO "\n");
raz ben yehuda46994192009-06-16 17:00:54 +100078}
79
NeilBrownfd01b882011-10-11 16:47:53 +110080static int create_strip_zones(struct mddev *mddev, raid0_conf_t **private_conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
NeilBrowna9f326e2009-09-23 18:06:41 +100082 int i, c, err;
NeilBrown49f357a22009-06-16 16:50:35 +100083 sector_t curr_zone_end, sectors;
NeilBrown3cb03002011-10-11 16:45:26 +110084 struct md_rdev *smallest, *rdev1, *rdev2, *rdev, **dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 struct strip_zone *zone;
86 int cnt;
87 char b[BDEVNAME_SIZE];
NeilBrown50de8df2011-10-07 14:23:22 +110088 char b2[BDEVNAME_SIZE];
Andre Nolled7b0032009-06-16 16:47:36 +100089 raid0_conf_t *conf = kzalloc(sizeof(*conf), GFP_KERNEL);
90
91 if (!conf)
92 return -ENOMEM;
Cheng Renquan159ec1f2009-01-09 08:31:08 +110093 list_for_each_entry(rdev1, &mddev->disks, same_set) {
NeilBrown50de8df2011-10-07 14:23:22 +110094 pr_debug("md/raid0:%s: looking at %s\n",
95 mdname(mddev),
96 bdevname(rdev1->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 c = 0;
NeilBrown13f26822009-06-18 08:48:55 +100098
99 /* round size to chunk_size */
100 sectors = rdev1->sectors;
101 sector_div(sectors, mddev->chunk_sectors);
102 rdev1->sectors = sectors * mddev->chunk_sectors;
103
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100104 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown50de8df2011-10-07 14:23:22 +1100105 pr_debug("md/raid0:%s: comparing %s(%llu)"
106 " with %s(%llu)\n",
107 mdname(mddev),
108 bdevname(rdev1->bdev,b),
109 (unsigned long long)rdev1->sectors,
110 bdevname(rdev2->bdev,b2),
111 (unsigned long long)rdev2->sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 if (rdev2 == rdev1) {
NeilBrown50de8df2011-10-07 14:23:22 +1100113 pr_debug("md/raid0:%s: END\n",
114 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 break;
116 }
Andre Nolldd8ac332009-03-31 14:33:13 +1100117 if (rdev2->sectors == rdev1->sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 /*
119 * Not unique, don't count it as a new
120 * group
121 */
NeilBrown50de8df2011-10-07 14:23:22 +1100122 pr_debug("md/raid0:%s: EQUAL\n",
123 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 c = 1;
125 break;
126 }
NeilBrown50de8df2011-10-07 14:23:22 +1100127 pr_debug("md/raid0:%s: NOT EQUAL\n",
128 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 }
130 if (!c) {
NeilBrown50de8df2011-10-07 14:23:22 +1100131 pr_debug("md/raid0:%s: ==> UNIQUE\n",
132 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 conf->nr_strip_zones++;
NeilBrown50de8df2011-10-07 14:23:22 +1100134 pr_debug("md/raid0:%s: %d zones\n",
135 mdname(mddev), conf->nr_strip_zones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
137 }
NeilBrown50de8df2011-10-07 14:23:22 +1100138 pr_debug("md/raid0:%s: FINAL %d zones\n",
139 mdname(mddev), conf->nr_strip_zones);
Andre Nolled7b0032009-06-16 16:47:36 +1000140 err = -ENOMEM;
NeilBrown9ffae0c2006-01-06 00:20:32 -0800141 conf->strip_zone = kzalloc(sizeof(struct strip_zone)*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 conf->nr_strip_zones, GFP_KERNEL);
143 if (!conf->strip_zone)
Andre Nolled7b0032009-06-16 16:47:36 +1000144 goto abort;
NeilBrown3cb03002011-10-11 16:45:26 +1100145 conf->devlist = kzalloc(sizeof(struct md_rdev*)*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 conf->nr_strip_zones*mddev->raid_disks,
147 GFP_KERNEL);
148 if (!conf->devlist)
Andre Nolled7b0032009-06-16 16:47:36 +1000149 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 /* The first zone must contain all devices, so here we check that
152 * there is a proper alignment of slots to devices and find them all
153 */
154 zone = &conf->strip_zone[0];
155 cnt = 0;
156 smallest = NULL;
NeilBrownb4145792009-06-16 16:50:52 +1000157 dev = conf->devlist;
Andre Nolled7b0032009-06-16 16:47:36 +1000158 err = -EINVAL;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100159 list_for_each_entry(rdev1, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 int j = rdev1->raid_disk;
161
NeilBrowne93f68a2010-06-15 09:36:03 +0100162 if (mddev->level == 10) {
Trela, Maciej9af204c2010-03-08 16:02:44 +1100163 /* taking over a raid10-n2 array */
164 j /= 2;
NeilBrowne93f68a2010-06-15 09:36:03 +0100165 rdev1->new_raid_disk = j;
166 }
Trela, Maciej9af204c2010-03-08 16:02:44 +1100167
Krzysztof Wojcikfc3a08b2011-01-31 13:47:13 +1100168 if (mddev->level == 1) {
169 /* taiking over a raid1 array-
170 * we have only one active disk
171 */
172 j = 0;
173 rdev1->new_raid_disk = j;
174 }
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (j < 0 || j >= mddev->raid_disks) {
NeilBrownb5a20962010-05-03 15:06:27 +1000177 printk(KERN_ERR "md/raid0:%s: bad disk number %d - "
178 "aborting!\n", mdname(mddev), j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 goto abort;
180 }
NeilBrownb4145792009-06-16 16:50:52 +1000181 if (dev[j]) {
NeilBrownb5a20962010-05-03 15:06:27 +1000182 printk(KERN_ERR "md/raid0:%s: multiple devices for %d - "
183 "aborting!\n", mdname(mddev), j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 goto abort;
185 }
NeilBrownb4145792009-06-16 16:50:52 +1000186 dev[j] = rdev1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +1000188 disk_stack_limits(mddev->gendisk, rdev1->bdev,
189 rdev1->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 /* as we don't honour merge_bvec_fn, we must never risk
NeilBrown627a2d32010-03-08 16:44:38 +1100191 * violating it, so limit ->max_segments to 1, lying within
192 * a single page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 */
194
NeilBrown627a2d32010-03-08 16:44:38 +1100195 if (rdev1->bdev->bd_disk->queue->merge_bvec_fn) {
196 blk_queue_max_segments(mddev->queue, 1);
197 blk_queue_segment_boundary(mddev->queue,
198 PAGE_CACHE_SIZE - 1);
199 }
Andre Nolldd8ac332009-03-31 14:33:13 +1100200 if (!smallest || (rdev1->sectors < smallest->sectors))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 smallest = rdev1;
202 cnt++;
203 }
204 if (cnt != mddev->raid_disks) {
NeilBrownb5a20962010-05-03 15:06:27 +1000205 printk(KERN_ERR "md/raid0:%s: too few disks (%d of %d) - "
206 "aborting!\n", mdname(mddev), cnt, mddev->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 goto abort;
208 }
209 zone->nb_dev = cnt;
NeilBrown49f357a22009-06-16 16:50:35 +1000210 zone->zone_end = smallest->sectors * cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
NeilBrown49f357a22009-06-16 16:50:35 +1000212 curr_zone_end = zone->zone_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 /* now do the other zones */
215 for (i = 1; i < conf->nr_strip_zones; i++)
216 {
NeilBrowna9f326e2009-09-23 18:06:41 +1000217 int j;
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 zone = conf->strip_zone + i;
NeilBrownb4145792009-06-16 16:50:52 +1000220 dev = conf->devlist + i * mddev->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
NeilBrown50de8df2011-10-07 14:23:22 +1100222 pr_debug("md/raid0:%s: zone %d\n", mdname(mddev), i);
NeilBrownd27a43ab2009-06-16 16:46:46 +1000223 zone->dev_start = smallest->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 smallest = NULL;
225 c = 0;
226
227 for (j=0; j<cnt; j++) {
NeilBrownb4145792009-06-16 16:50:52 +1000228 rdev = conf->devlist[j];
NeilBrownd27a43ab2009-06-16 16:46:46 +1000229 if (rdev->sectors <= zone->dev_start) {
NeilBrown50de8df2011-10-07 14:23:22 +1100230 pr_debug("md/raid0:%s: checking %s ... nope\n",
231 mdname(mddev),
232 bdevname(rdev->bdev, b));
Andre Nolldd8ac332009-03-31 14:33:13 +1100233 continue;
234 }
NeilBrown50de8df2011-10-07 14:23:22 +1100235 pr_debug("md/raid0:%s: checking %s ..."
236 " contained as device %d\n",
237 mdname(mddev),
238 bdevname(rdev->bdev, b), c);
NeilBrownb4145792009-06-16 16:50:52 +1000239 dev[c] = rdev;
Andre Nolldd8ac332009-03-31 14:33:13 +1100240 c++;
241 if (!smallest || rdev->sectors < smallest->sectors) {
242 smallest = rdev;
NeilBrown50de8df2011-10-07 14:23:22 +1100243 pr_debug("md/raid0:%s: (%llu) is smallest!.\n",
244 mdname(mddev),
245 (unsigned long long)rdev->sectors);
Andre Nolldd8ac332009-03-31 14:33:13 +1100246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248
249 zone->nb_dev = c;
NeilBrown49f357a22009-06-16 16:50:35 +1000250 sectors = (smallest->sectors - zone->dev_start) * c;
NeilBrown50de8df2011-10-07 14:23:22 +1100251 pr_debug("md/raid0:%s: zone->nb_dev: %d, sectors: %llu\n",
252 mdname(mddev),
253 zone->nb_dev, (unsigned long long)sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
NeilBrown49f357a22009-06-16 16:50:35 +1000255 curr_zone_end += sectors;
NeilBrownd27a43ab2009-06-16 16:46:46 +1000256 zone->zone_end = curr_zone_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
NeilBrown50de8df2011-10-07 14:23:22 +1100258 pr_debug("md/raid0:%s: current zone start: %llu\n",
259 mdname(mddev),
260 (unsigned long long)smallest->sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
NeilBrown26be34d2006-10-03 01:15:53 -0700262 mddev->queue->backing_dev_info.congested_fn = raid0_congested;
263 mddev->queue->backing_dev_info.congested_data = mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
raz ben yehuda92e59b62009-06-16 17:00:57 +1000265 /*
266 * now since we have the hard sector sizes, we can make sure
267 * chunk size is a multiple of that sector size
268 */
Andre Noll9d8f0362009-06-18 08:45:01 +1000269 if ((mddev->chunk_sectors << 9) % queue_logical_block_size(mddev->queue)) {
NeilBrownb5a20962010-05-03 15:06:27 +1000270 printk(KERN_ERR "md/raid0:%s: chunk_size of %d not valid\n",
raz ben yehuda92e59b62009-06-16 17:00:57 +1000271 mdname(mddev),
Andre Noll9d8f0362009-06-18 08:45:01 +1000272 mddev->chunk_sectors << 9);
raz ben yehuda92e59b62009-06-16 17:00:57 +1000273 goto abort;
274 }
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +1000275
276 blk_queue_io_min(mddev->queue, mddev->chunk_sectors << 9);
277 blk_queue_io_opt(mddev->queue,
278 (mddev->chunk_sectors << 9) * mddev->raid_disks);
279
NeilBrown50de8df2011-10-07 14:23:22 +1100280 pr_debug("md/raid0:%s: done.\n", mdname(mddev));
Trela, Maciej9af204c2010-03-08 16:02:44 +1100281 *private_conf = conf;
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return 0;
Andre Noll5568a602009-06-16 16:47:21 +1000284abort:
Andre Nolled7b0032009-06-16 16:47:36 +1000285 kfree(conf->strip_zone);
286 kfree(conf->devlist);
287 kfree(conf);
Trela, Maciej9af204c2010-03-08 16:02:44 +1100288 *private_conf = NULL;
Andre Nolled7b0032009-06-16 16:47:36 +1000289 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
292/**
293 * raid0_mergeable_bvec -- tell bio layer if a two requests can be merged
294 * @q: request queue
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200295 * @bvm: properties of new bio
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 * @biovec: the request that could be merged to it.
297 *
298 * Return amount of bytes we can accept at this offset
299 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200300static int raid0_mergeable_bvec(struct request_queue *q,
301 struct bvec_merge_data *bvm,
302 struct bio_vec *biovec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
NeilBrownfd01b882011-10-11 16:47:53 +1100304 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200305 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +1000307 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200308 unsigned int bio_sectors = bvm->bi_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
NeilBrownd6e412e2009-06-18 08:47:00 +1000310 if (is_power_of_2(chunk_sectors))
raz ben yehudafbb704e2009-06-16 17:02:05 +1000311 max = (chunk_sectors - ((sector & (chunk_sectors-1))
312 + bio_sectors)) << 9;
313 else
314 max = (chunk_sectors - (sector_div(sector, chunk_sectors)
315 + bio_sectors)) << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
317 if (max <= biovec->bv_len && bio_sectors == 0)
318 return biovec->bv_len;
319 else
320 return max;
321}
322
NeilBrownfd01b882011-10-11 16:47:53 +1100323static sector_t raid0_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -0700324{
325 sector_t array_sectors = 0;
NeilBrown3cb03002011-10-11 16:45:26 +1100326 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -0700327
328 WARN_ONCE(sectors || raid_disks,
329 "%s does not support generic reshape\n", __func__);
330
331 list_for_each_entry(rdev, &mddev->disks, same_set)
332 array_sectors += rdev->sectors;
333
334 return array_sectors;
335}
336
NeilBrownfd01b882011-10-11 16:47:53 +1100337static int raid0_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Trela, Maciej9af204c2010-03-08 16:02:44 +1100339 raid0_conf_t *conf;
Andre Noll5568a602009-06-16 16:47:21 +1000340 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Andre Noll9d8f0362009-06-18 08:45:01 +1000342 if (mddev->chunk_sectors == 0) {
NeilBrownb5a20962010-05-03 15:06:27 +1000343 printk(KERN_ERR "md/raid0:%s: chunk size must be set.\n",
344 mdname(mddev));
NeilBrown2604b702006-01-06 00:20:36 -0800345 return -EINVAL;
346 }
Andre Noll0894cc32009-06-18 08:49:23 +1000347 if (md_check_no_bitmap(mddev))
348 return -EINVAL;
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500349 blk_queue_max_hw_sectors(mddev->queue, mddev->chunk_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Trela, Maciej9af204c2010-03-08 16:02:44 +1100351 /* if private is not null, we are here after takeover */
352 if (mddev->private == NULL) {
353 ret = create_strip_zones(mddev, &conf);
354 if (ret < 0)
355 return ret;
356 mddev->private = conf;
357 }
358 conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 /* calculate array device size */
Dan Williams1f403622009-03-31 14:59:03 +1100361 md_set_array_sectors(mddev, raid0_size(mddev, 0, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
NeilBrownb5a20962010-05-03 15:06:27 +1000363 printk(KERN_INFO "md/raid0:%s: md_size is %llu sectors.\n",
364 mdname(mddev),
365 (unsigned long long)mddev->array_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 /* calculate the max read-ahead size.
367 * For read-ahead of large files to be effective, we need to
368 * readahead at least twice a whole stripe. i.e. number of devices
369 * multiplied by chunk size times 2.
370 * If an individual device has an ra_pages greater than the
371 * chunk size, then we will not drive that device as hard as it
372 * wants. We consider this a configuration error: a larger
373 * chunksize should be used in that case.
374 */
375 {
Andre Noll9d8f0362009-06-18 08:45:01 +1000376 int stripe = mddev->raid_disks *
377 (mddev->chunk_sectors << 9) / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
379 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
380 }
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 blk_queue_merge_bvec(mddev->queue, raid0_mergeable_bvec);
raz ben yehuda46994192009-06-16 17:00:54 +1000383 dump_zones(mddev);
Martin K. Petersena91a2782011-03-17 11:11:05 +0100384 return md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
NeilBrownfd01b882011-10-11 16:47:53 +1100387static int raid0_stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
NeilBrown070ec552009-06-16 16:54:21 +1000389 raid0_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700392 kfree(conf->strip_zone);
Andre Nollfb5ab4b2009-06-16 16:48:19 +1000393 kfree(conf->devlist);
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700394 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 mddev->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return 0;
397}
398
NeilBrown49f357a22009-06-16 16:50:35 +1000399/* Find the zone which holds a particular offset
400 * Update *sectorp to be an offset in that zone
401 */
Andre Nolldc582662009-06-16 16:18:43 +1000402static struct strip_zone *find_zone(struct raid0_private_data *conf,
NeilBrown49f357a22009-06-16 16:50:35 +1000403 sector_t *sectorp)
Andre Nolldc582662009-06-16 16:18:43 +1000404{
405 int i;
406 struct strip_zone *z = conf->strip_zone;
NeilBrown49f357a22009-06-16 16:50:35 +1000407 sector_t sector = *sectorp;
Andre Nolldc582662009-06-16 16:18:43 +1000408
409 for (i = 0; i < conf->nr_strip_zones; i++)
NeilBrown49f357a22009-06-16 16:50:35 +1000410 if (sector < z[i].zone_end) {
411 if (i)
412 *sectorp = sector - z[i-1].zone_end;
Andre Nolldc582662009-06-16 16:18:43 +1000413 return z + i;
NeilBrown49f357a22009-06-16 16:50:35 +1000414 }
Andre Nolldc582662009-06-16 16:18:43 +1000415 BUG();
416}
417
raz ben yehudafbb704e2009-06-16 17:02:05 +1000418/*
419 * remaps the bio to the target device. we separate two flows.
420 * power 2 flow and a general flow for the sake of perfromance
421*/
NeilBrownfd01b882011-10-11 16:47:53 +1100422static struct md_rdev *map_sector(struct mddev *mddev, struct strip_zone *zone,
raz ben yehudafbb704e2009-06-16 17:02:05 +1000423 sector_t sector, sector_t *sector_offset)
424{
425 unsigned int sect_in_chunk;
426 sector_t chunk;
427 raid0_conf_t *conf = mddev->private;
NeilBrown84707f32010-03-16 17:23:35 +1100428 int raid_disks = conf->strip_zone[0].nb_dev;
Andre Noll9d8f0362009-06-18 08:45:01 +1000429 unsigned int chunk_sects = mddev->chunk_sectors;
raz ben yehudafbb704e2009-06-16 17:02:05 +1000430
NeilBrownd6e412e2009-06-18 08:47:00 +1000431 if (is_power_of_2(chunk_sects)) {
raz ben yehudafbb704e2009-06-16 17:02:05 +1000432 int chunksect_bits = ffz(~chunk_sects);
433 /* find the sector offset inside the chunk */
434 sect_in_chunk = sector & (chunk_sects - 1);
435 sector >>= chunksect_bits;
436 /* chunk in zone */
437 chunk = *sector_offset;
438 /* quotient is the chunk in real device*/
439 sector_div(chunk, zone->nb_dev << chunksect_bits);
440 } else{
441 sect_in_chunk = sector_div(sector, chunk_sects);
442 chunk = *sector_offset;
443 sector_div(chunk, chunk_sects * zone->nb_dev);
444 }
445 /*
446 * position the bio over the real device
447 * real sector = chunk in device + starting of zone
448 * + the position in the chunk
449 */
450 *sector_offset = (chunk * chunk_sects) + sect_in_chunk;
NeilBrown84707f32010-03-16 17:23:35 +1100451 return conf->devlist[(zone - conf->strip_zone)*raid_disks
raz ben yehudafbb704e2009-06-16 17:02:05 +1000452 + sector_div(sector, zone->nb_dev)];
453}
454
455/*
456 * Is io distribute over 1 or more chunks ?
457*/
NeilBrownfd01b882011-10-11 16:47:53 +1100458static inline int is_io_in_chunk_boundary(struct mddev *mddev,
raz ben yehudafbb704e2009-06-16 17:02:05 +1000459 unsigned int chunk_sects, struct bio *bio)
460{
NeilBrownd6e412e2009-06-18 08:47:00 +1000461 if (likely(is_power_of_2(chunk_sects))) {
raz ben yehudafbb704e2009-06-16 17:02:05 +1000462 return chunk_sects >= ((bio->bi_sector & (chunk_sects-1))
463 + (bio->bi_size >> 9));
464 } else{
465 sector_t sector = bio->bi_sector;
466 return chunk_sects >= (sector_div(sector, chunk_sects)
467 + (bio->bi_size >> 9));
468 }
469}
470
NeilBrownfd01b882011-10-11 16:47:53 +1100471static int raid0_make_request(struct mddev *mddev, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
raz ben yehudafbb704e2009-06-16 17:02:05 +1000473 unsigned int chunk_sects;
474 sector_t sector_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 struct strip_zone *zone;
NeilBrown3cb03002011-10-11 16:45:26 +1100476 struct md_rdev *tmp_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Tejun Heoe9c74692010-09-03 11:56:18 +0200478 if (unlikely(bio->bi_rw & REQ_FLUSH)) {
479 md_flush_request(mddev, bio);
NeilBrowne5dcdd82005-09-09 16:23:41 -0700480 return 0;
481 }
482
Andre Noll9d8f0362009-06-18 08:45:01 +1000483 chunk_sects = mddev->chunk_sectors;
raz ben yehudafbb704e2009-06-16 17:02:05 +1000484 if (unlikely(!is_io_in_chunk_boundary(mddev, chunk_sects, bio))) {
485 sector_t sector = bio->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 struct bio_pair *bp;
487 /* Sanity check -- queue functions should prevent this happening */
488 if (bio->bi_vcnt != 1 ||
489 bio->bi_idx != 0)
490 goto bad_map;
491 /* This is a one page bio that upper layers
492 * refuse to split for us, so we need to split it.
493 */
NeilBrownd6e412e2009-06-18 08:47:00 +1000494 if (likely(is_power_of_2(chunk_sects)))
raz ben yehudafbb704e2009-06-16 17:02:05 +1000495 bp = bio_split(bio, chunk_sects - (sector &
496 (chunk_sects-1)));
497 else
498 bp = bio_split(bio, chunk_sects -
499 sector_div(sector, chunk_sects));
NeilBrown21a52c62010-04-01 15:02:13 +1100500 if (raid0_make_request(mddev, &bp->bio1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 generic_make_request(&bp->bio1);
NeilBrown21a52c62010-04-01 15:02:13 +1100502 if (raid0_make_request(mddev, &bp->bio2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 generic_make_request(&bp->bio2);
504
505 bio_pair_release(bp);
506 return 0;
507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
raz ben yehudafbb704e2009-06-16 17:02:05 +1000509 sector_offset = bio->bi_sector;
510 zone = find_zone(mddev->private, &sector_offset);
511 tmp_dev = map_sector(mddev, zone, bio->bi_sector,
512 &sector_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 bio->bi_bdev = tmp_dev->bdev;
raz ben yehudafbb704e2009-06-16 17:02:05 +1000514 bio->bi_sector = sector_offset + zone->dev_start +
515 tmp_dev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 /*
517 * Let the main block layer submit the IO and resolve recursion:
518 */
519 return 1;
520
521bad_map:
NeilBrownb5a20962010-05-03 15:06:27 +1000522 printk("md/raid0:%s: make_request bug: can't convert block across chunks"
523 " or bigger than %dk %llu %d\n",
524 mdname(mddev), chunk_sects / 2,
525 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
NeilBrown6712ecf2007-09-27 12:47:43 +0200527 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return 0;
529}
NeilBrown8299d7f2007-10-16 23:30:53 -0700530
NeilBrownfd01b882011-10-11 16:47:53 +1100531static void raid0_status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Andre Noll9d8f0362009-06-18 08:45:01 +1000533 seq_printf(seq, " %dk chunks", mddev->chunk_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return;
535}
536
NeilBrownfd01b882011-10-11 16:47:53 +1100537static void *raid0_takeover_raid45(struct mddev *mddev)
Trela, Maciej9af204c2010-03-08 16:02:44 +1100538{
NeilBrown3cb03002011-10-11 16:45:26 +1100539 struct md_rdev *rdev;
Trela, Maciej9af204c2010-03-08 16:02:44 +1100540 raid0_conf_t *priv_conf;
541
542 if (mddev->degraded != 1) {
NeilBrownb5a20962010-05-03 15:06:27 +1000543 printk(KERN_ERR "md/raid0:%s: raid5 must be degraded! Degraded disks: %d\n",
544 mdname(mddev),
Trela, Maciej9af204c2010-03-08 16:02:44 +1100545 mddev->degraded);
546 return ERR_PTR(-EINVAL);
547 }
548
549 list_for_each_entry(rdev, &mddev->disks, same_set) {
550 /* check slot number for a disk */
551 if (rdev->raid_disk == mddev->raid_disks-1) {
NeilBrownb5a20962010-05-03 15:06:27 +1000552 printk(KERN_ERR "md/raid0:%s: raid5 must have missing parity disk!\n",
553 mdname(mddev));
Trela, Maciej9af204c2010-03-08 16:02:44 +1100554 return ERR_PTR(-EINVAL);
555 }
556 }
557
558 /* Set new parameters */
559 mddev->new_level = 0;
Maciej Trela001048a2010-06-16 11:55:14 +0100560 mddev->new_layout = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +1100561 mddev->new_chunk_sectors = mddev->chunk_sectors;
562 mddev->raid_disks--;
563 mddev->delta_disks = -1;
564 /* make sure it will be not marked as dirty */
565 mddev->recovery_cp = MaxSector;
566
567 create_strip_zones(mddev, &priv_conf);
568 return priv_conf;
569}
570
NeilBrownfd01b882011-10-11 16:47:53 +1100571static void *raid0_takeover_raid10(struct mddev *mddev)
Trela, Maciej9af204c2010-03-08 16:02:44 +1100572{
573 raid0_conf_t *priv_conf;
574
575 /* Check layout:
576 * - far_copies must be 1
577 * - near_copies must be 2
578 * - disks number must be even
579 * - all mirrors must be already degraded
580 */
581 if (mddev->layout != ((1 << 8) + 2)) {
NeilBrownb5a20962010-05-03 15:06:27 +1000582 printk(KERN_ERR "md/raid0:%s:: Raid0 cannot takover layout: 0x%x\n",
583 mdname(mddev),
Trela, Maciej9af204c2010-03-08 16:02:44 +1100584 mddev->layout);
585 return ERR_PTR(-EINVAL);
586 }
587 if (mddev->raid_disks & 1) {
NeilBrownb5a20962010-05-03 15:06:27 +1000588 printk(KERN_ERR "md/raid0:%s: Raid0 cannot takover Raid10 with odd disk number.\n",
589 mdname(mddev));
Trela, Maciej9af204c2010-03-08 16:02:44 +1100590 return ERR_PTR(-EINVAL);
591 }
592 if (mddev->degraded != (mddev->raid_disks>>1)) {
NeilBrownb5a20962010-05-03 15:06:27 +1000593 printk(KERN_ERR "md/raid0:%s: All mirrors must be already degraded!\n",
594 mdname(mddev));
Trela, Maciej9af204c2010-03-08 16:02:44 +1100595 return ERR_PTR(-EINVAL);
596 }
597
598 /* Set new parameters */
599 mddev->new_level = 0;
Maciej Trela001048a2010-06-16 11:55:14 +0100600 mddev->new_layout = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +1100601 mddev->new_chunk_sectors = mddev->chunk_sectors;
602 mddev->delta_disks = - mddev->raid_disks / 2;
603 mddev->raid_disks += mddev->delta_disks;
604 mddev->degraded = 0;
605 /* make sure it will be not marked as dirty */
606 mddev->recovery_cp = MaxSector;
607
608 create_strip_zones(mddev, &priv_conf);
Trela, Maciej9af204c2010-03-08 16:02:44 +1100609 return priv_conf;
610}
611
NeilBrownfd01b882011-10-11 16:47:53 +1100612static void *raid0_takeover_raid1(struct mddev *mddev)
Krzysztof Wojcikfc3a08b2011-01-31 13:47:13 +1100613{
614 raid0_conf_t *priv_conf;
615
616 /* Check layout:
617 * - (N - 1) mirror drives must be already faulty
618 */
619 if ((mddev->raid_disks - 1) != mddev->degraded) {
620 printk(KERN_ERR "md/raid0:%s: (N - 1) mirrors drives must be already faulty!\n",
621 mdname(mddev));
622 return ERR_PTR(-EINVAL);
623 }
624
625 /* Set new parameters */
626 mddev->new_level = 0;
627 mddev->new_layout = 0;
628 mddev->new_chunk_sectors = 128; /* by default set chunk size to 64k */
629 mddev->delta_disks = 1 - mddev->raid_disks;
Krzysztof Wojcikf7bee802011-02-14 10:01:41 +1100630 mddev->raid_disks = 1;
Krzysztof Wojcikfc3a08b2011-01-31 13:47:13 +1100631 /* make sure it will be not marked as dirty */
632 mddev->recovery_cp = MaxSector;
633
634 create_strip_zones(mddev, &priv_conf);
635 return priv_conf;
636}
637
NeilBrownfd01b882011-10-11 16:47:53 +1100638static void *raid0_takeover(struct mddev *mddev)
Trela, Maciej9af204c2010-03-08 16:02:44 +1100639{
640 /* raid0 can take over:
Maciej Trela049d6c12010-06-16 11:56:12 +0100641 * raid4 - if all data disks are active.
Trela, Maciej9af204c2010-03-08 16:02:44 +1100642 * raid5 - providing it is Raid4 layout and one disk is faulty
643 * raid10 - assuming we have all necessary active disks
Krzysztof Wojcikfc3a08b2011-01-31 13:47:13 +1100644 * raid1 - with (N -1) mirror drives faulty
Trela, Maciej9af204c2010-03-08 16:02:44 +1100645 */
Maciej Trela049d6c12010-06-16 11:56:12 +0100646 if (mddev->level == 4)
647 return raid0_takeover_raid45(mddev);
648
Trela, Maciej9af204c2010-03-08 16:02:44 +1100649 if (mddev->level == 5) {
650 if (mddev->layout == ALGORITHM_PARITY_N)
Maciej Trela049d6c12010-06-16 11:56:12 +0100651 return raid0_takeover_raid45(mddev);
Trela, Maciej9af204c2010-03-08 16:02:44 +1100652
NeilBrownb5a20962010-05-03 15:06:27 +1000653 printk(KERN_ERR "md/raid0:%s: Raid can only takeover Raid5 with layout: %d\n",
654 mdname(mddev), ALGORITHM_PARITY_N);
Trela, Maciej9af204c2010-03-08 16:02:44 +1100655 }
656
657 if (mddev->level == 10)
658 return raid0_takeover_raid10(mddev);
659
Krzysztof Wojcikfc3a08b2011-01-31 13:47:13 +1100660 if (mddev->level == 1)
661 return raid0_takeover_raid1(mddev);
662
663 printk(KERN_ERR "Takeover from raid%i to raid0 not supported\n",
664 mddev->level);
665
Trela, Maciej9af204c2010-03-08 16:02:44 +1100666 return ERR_PTR(-EINVAL);
667}
668
NeilBrownfd01b882011-10-11 16:47:53 +1100669static void raid0_quiesce(struct mddev *mddev, int state)
Trela, Maciej9af204c2010-03-08 16:02:44 +1100670{
671}
672
NeilBrown2604b702006-01-06 00:20:36 -0800673static struct mdk_personality raid0_personality=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
675 .name = "raid0",
NeilBrown2604b702006-01-06 00:20:36 -0800676 .level = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 .owner = THIS_MODULE,
678 .make_request = raid0_make_request,
679 .run = raid0_run,
680 .stop = raid0_stop,
681 .status = raid0_status,
Dan Williams80c3a6c2009-03-17 18:10:40 -0700682 .size = raid0_size,
Trela, Maciej9af204c2010-03-08 16:02:44 +1100683 .takeover = raid0_takeover,
684 .quiesce = raid0_quiesce,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685};
686
687static int __init raid0_init (void)
688{
NeilBrown2604b702006-01-06 00:20:36 -0800689 return register_md_personality (&raid0_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
692static void raid0_exit (void)
693{
NeilBrown2604b702006-01-06 00:20:36 -0800694 unregister_md_personality (&raid0_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
697module_init(raid0_init);
698module_exit(raid0_exit);
699MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +1100700MODULE_DESCRIPTION("RAID0 (striping) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701MODULE_ALIAS("md-personality-2"); /* RAID0 */
NeilBrownd9d166c2006-01-06 00:20:51 -0800702MODULE_ALIAS("md-raid0");
NeilBrown2604b702006-01-06 00:20:36 -0800703MODULE_ALIAS("md-level-0");