blob: d535f9be39f4da9fbe331dedfaf6da1adf8114fb [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"
Trela, Maciej9af204c2010-03-08 16:02:44 +110025#include "raid5.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Jens Axboe165125e2007-07-24 09:28:11 +020027static void raid0_unplug(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
29 mddev_t *mddev = q->queuedata;
NeilBrown070ec552009-06-16 16:54:21 +100030 raid0_conf_t *conf = mddev->private;
NeilBrownb4145792009-06-16 16:50:52 +100031 mdk_rdev_t **devlist = conf->devlist;
NeilBrown84707f32010-03-16 17:23:35 +110032 int raid_disks = conf->strip_zone[0].nb_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 int i;
34
NeilBrown84707f32010-03-16 17:23:35 +110035 for (i=0; i < raid_disks; i++) {
Jens Axboe165125e2007-07-24 09:28:11 +020036 struct request_queue *r_queue = bdev_get_queue(devlist[i]->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -050038 blk_unplug(r_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 }
40}
41
NeilBrown26be34d2006-10-03 01:15:53 -070042static int raid0_congested(void *data, int bits)
43{
44 mddev_t *mddev = data;
NeilBrown070ec552009-06-16 16:54:21 +100045 raid0_conf_t *conf = mddev->private;
NeilBrownb4145792009-06-16 16:50:52 +100046 mdk_rdev_t **devlist = conf->devlist;
NeilBrown84707f32010-03-16 17:23:35 +110047 int raid_disks = conf->strip_zone[0].nb_dev;
NeilBrown26be34d2006-10-03 01:15:53 -070048 int i, ret = 0;
49
NeilBrown3fa841d2009-09-23 18:10:29 +100050 if (mddev_congested(mddev, bits))
51 return 1;
52
NeilBrown84707f32010-03-16 17:23:35 +110053 for (i = 0; i < raid_disks && !ret ; i++) {
Jens Axboe165125e2007-07-24 09:28:11 +020054 struct request_queue *q = bdev_get_queue(devlist[i]->bdev);
NeilBrown26be34d2006-10-03 01:15:53 -070055
56 ret |= bdi_congested(&q->backing_dev_info, bits);
57 }
58 return ret;
59}
60
raz ben yehuda46994192009-06-16 17:00:54 +100061/*
62 * inform the user of the raid configuration
63*/
64static void dump_zones(mddev_t *mddev)
65{
66 int j, k, h;
67 sector_t zone_size = 0;
68 sector_t zone_start = 0;
69 char b[BDEVNAME_SIZE];
70 raid0_conf_t *conf = mddev->private;
NeilBrown84707f32010-03-16 17:23:35 +110071 int raid_disks = conf->strip_zone[0].nb_dev;
raz ben yehuda46994192009-06-16 17:00:54 +100072 printk(KERN_INFO "******* %s configuration *********\n",
73 mdname(mddev));
74 h = 0;
75 for (j = 0; j < conf->nr_strip_zones; j++) {
76 printk(KERN_INFO "zone%d=[", j);
77 for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
78 printk("%s/",
NeilBrown84707f32010-03-16 17:23:35 +110079 bdevname(conf->devlist[j*raid_disks
raz ben yehuda46994192009-06-16 17:00:54 +100080 + k]->bdev, b));
81 printk("]\n");
82
83 zone_size = conf->strip_zone[j].zone_end - zone_start;
84 printk(KERN_INFO " zone offset=%llukb "
85 "device offset=%llukb size=%llukb\n",
86 (unsigned long long)zone_start>>1,
87 (unsigned long long)conf->strip_zone[j].dev_start>>1,
88 (unsigned long long)zone_size>>1);
89 zone_start = conf->strip_zone[j].zone_end;
90 }
91 printk(KERN_INFO "**********************************\n\n");
92}
93
Trela, Maciej9af204c2010-03-08 16:02:44 +110094static int create_strip_zones(mddev_t *mddev, raid0_conf_t **private_conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
NeilBrowna9f326e2009-09-23 18:06:41 +100096 int i, c, err;
NeilBrown49f357a22009-06-16 16:50:35 +100097 sector_t curr_zone_end, sectors;
NeilBrownb4145792009-06-16 16:50:52 +100098 mdk_rdev_t *smallest, *rdev1, *rdev2, *rdev, **dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 struct strip_zone *zone;
100 int cnt;
101 char b[BDEVNAME_SIZE];
Andre Nolled7b0032009-06-16 16:47:36 +1000102 raid0_conf_t *conf = kzalloc(sizeof(*conf), GFP_KERNEL);
103
104 if (!conf)
105 return -ENOMEM;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100106 list_for_each_entry(rdev1, &mddev->disks, same_set) {
Andre Noll0825b872009-01-09 08:31:07 +1100107 printk(KERN_INFO "raid0: looking at %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 bdevname(rdev1->bdev,b));
109 c = 0;
NeilBrown13f26822009-06-18 08:48:55 +1000110
111 /* round size to chunk_size */
112 sectors = rdev1->sectors;
113 sector_div(sectors, mddev->chunk_sectors);
114 rdev1->sectors = sectors * mddev->chunk_sectors;
115
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100116 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Andre Noll0825b872009-01-09 08:31:07 +1100117 printk(KERN_INFO "raid0: comparing %s(%llu)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 bdevname(rdev1->bdev,b),
Andre Nolldd8ac332009-03-31 14:33:13 +1100119 (unsigned long long)rdev1->sectors);
Andre Noll0825b872009-01-09 08:31:07 +1100120 printk(KERN_INFO " with %s(%llu)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 bdevname(rdev2->bdev,b),
Andre Nolldd8ac332009-03-31 14:33:13 +1100122 (unsigned long long)rdev2->sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (rdev2 == rdev1) {
Andre Noll0825b872009-01-09 08:31:07 +1100124 printk(KERN_INFO "raid0: END\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 break;
126 }
Andre Nolldd8ac332009-03-31 14:33:13 +1100127 if (rdev2->sectors == rdev1->sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 /*
129 * Not unique, don't count it as a new
130 * group
131 */
Andre Noll0825b872009-01-09 08:31:07 +1100132 printk(KERN_INFO "raid0: EQUAL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 c = 1;
134 break;
135 }
Andre Noll0825b872009-01-09 08:31:07 +1100136 printk(KERN_INFO "raid0: NOT EQUAL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
138 if (!c) {
Andre Noll0825b872009-01-09 08:31:07 +1100139 printk(KERN_INFO "raid0: ==> UNIQUE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 conf->nr_strip_zones++;
Andre Noll0825b872009-01-09 08:31:07 +1100141 printk(KERN_INFO "raid0: %d zones\n",
142 conf->nr_strip_zones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
144 }
Andre Noll0825b872009-01-09 08:31:07 +1100145 printk(KERN_INFO "raid0: FINAL %d zones\n", conf->nr_strip_zones);
Andre Nolled7b0032009-06-16 16:47:36 +1000146 err = -ENOMEM;
NeilBrown9ffae0c2006-01-06 00:20:32 -0800147 conf->strip_zone = kzalloc(sizeof(struct strip_zone)*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 conf->nr_strip_zones, GFP_KERNEL);
149 if (!conf->strip_zone)
Andre Nolled7b0032009-06-16 16:47:36 +1000150 goto abort;
NeilBrown9ffae0c2006-01-06 00:20:32 -0800151 conf->devlist = kzalloc(sizeof(mdk_rdev_t*)*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 conf->nr_strip_zones*mddev->raid_disks,
153 GFP_KERNEL);
154 if (!conf->devlist)
Andre Nolled7b0032009-06-16 16:47:36 +1000155 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 /* The first zone must contain all devices, so here we check that
158 * there is a proper alignment of slots to devices and find them all
159 */
160 zone = &conf->strip_zone[0];
161 cnt = 0;
162 smallest = NULL;
NeilBrownb4145792009-06-16 16:50:52 +1000163 dev = conf->devlist;
Andre Nolled7b0032009-06-16 16:47:36 +1000164 err = -EINVAL;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100165 list_for_each_entry(rdev1, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 int j = rdev1->raid_disk;
167
Trela, Maciej9af204c2010-03-08 16:02:44 +1100168 if (mddev->level == 10)
169 /* taking over a raid10-n2 array */
170 j /= 2;
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (j < 0 || j >= mddev->raid_disks) {
Andre Noll0825b872009-01-09 08:31:07 +1100173 printk(KERN_ERR "raid0: bad disk number %d - "
174 "aborting!\n", j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 goto abort;
176 }
NeilBrownb4145792009-06-16 16:50:52 +1000177 if (dev[j]) {
Andre Noll0825b872009-01-09 08:31:07 +1100178 printk(KERN_ERR "raid0: multiple devices for %d - "
179 "aborting!\n", j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 goto abort;
181 }
NeilBrownb4145792009-06-16 16:50:52 +1000182 dev[j] = rdev1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +1000184 disk_stack_limits(mddev->gendisk, rdev1->bdev,
185 rdev1->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 /* as we don't honour merge_bvec_fn, we must never risk
NeilBrown627a2d32010-03-08 16:44:38 +1100187 * violating it, so limit ->max_segments to 1, lying within
188 * a single page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 */
190
NeilBrown627a2d32010-03-08 16:44:38 +1100191 if (rdev1->bdev->bd_disk->queue->merge_bvec_fn) {
192 blk_queue_max_segments(mddev->queue, 1);
193 blk_queue_segment_boundary(mddev->queue,
194 PAGE_CACHE_SIZE - 1);
195 }
Andre Nolldd8ac332009-03-31 14:33:13 +1100196 if (!smallest || (rdev1->sectors < smallest->sectors))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 smallest = rdev1;
198 cnt++;
199 }
200 if (cnt != mddev->raid_disks) {
Andre Noll0825b872009-01-09 08:31:07 +1100201 printk(KERN_ERR "raid0: too few disks (%d of %d) - "
202 "aborting!\n", cnt, mddev->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 goto abort;
204 }
205 zone->nb_dev = cnt;
NeilBrown49f357a22009-06-16 16:50:35 +1000206 zone->zone_end = smallest->sectors * cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
NeilBrown49f357a22009-06-16 16:50:35 +1000208 curr_zone_end = zone->zone_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 /* now do the other zones */
211 for (i = 1; i < conf->nr_strip_zones; i++)
212 {
NeilBrowna9f326e2009-09-23 18:06:41 +1000213 int j;
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 zone = conf->strip_zone + i;
NeilBrownb4145792009-06-16 16:50:52 +1000216 dev = conf->devlist + i * mddev->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Andre Noll0825b872009-01-09 08:31:07 +1100218 printk(KERN_INFO "raid0: zone %d\n", i);
NeilBrownd27a43a2009-06-16 16:46:46 +1000219 zone->dev_start = smallest->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 smallest = NULL;
221 c = 0;
222
223 for (j=0; j<cnt; j++) {
NeilBrownb4145792009-06-16 16:50:52 +1000224 rdev = conf->devlist[j];
Andre Noll0825b872009-01-09 08:31:07 +1100225 printk(KERN_INFO "raid0: checking %s ...",
226 bdevname(rdev->bdev, b));
NeilBrownd27a43a2009-06-16 16:46:46 +1000227 if (rdev->sectors <= zone->dev_start) {
Andre Noll0825b872009-01-09 08:31:07 +1100228 printk(KERN_INFO " nope.\n");
Andre Nolldd8ac332009-03-31 14:33:13 +1100229 continue;
230 }
231 printk(KERN_INFO " contained as device %d\n", c);
NeilBrownb4145792009-06-16 16:50:52 +1000232 dev[c] = rdev;
Andre Nolldd8ac332009-03-31 14:33:13 +1100233 c++;
234 if (!smallest || rdev->sectors < smallest->sectors) {
235 smallest = rdev;
236 printk(KERN_INFO " (%llu) is smallest!.\n",
237 (unsigned long long)rdev->sectors);
238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240
241 zone->nb_dev = c;
NeilBrown49f357a22009-06-16 16:50:35 +1000242 sectors = (smallest->sectors - zone->dev_start) * c;
Andre Noll83838ed2009-01-09 08:31:07 +1100243 printk(KERN_INFO "raid0: zone->nb_dev: %d, sectors: %llu\n",
NeilBrown49f357a22009-06-16 16:50:35 +1000244 zone->nb_dev, (unsigned long long)sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
NeilBrown49f357a22009-06-16 16:50:35 +1000246 curr_zone_end += sectors;
NeilBrownd27a43a2009-06-16 16:46:46 +1000247 zone->zone_end = curr_zone_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Andre Noll6b8796c2009-01-09 08:31:07 +1100249 printk(KERN_INFO "raid0: current zone start: %llu\n",
NeilBrownd27a43a2009-06-16 16:46:46 +1000250 (unsigned long long)smallest->sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 mddev->queue->unplug_fn = raid0_unplug;
NeilBrown26be34d2006-10-03 01:15:53 -0700253 mddev->queue->backing_dev_info.congested_fn = raid0_congested;
254 mddev->queue->backing_dev_info.congested_data = mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
raz ben yehuda92e59b62009-06-16 17:00:57 +1000256 /*
257 * now since we have the hard sector sizes, we can make sure
258 * chunk size is a multiple of that sector size
259 */
Andre Noll9d8f0362009-06-18 08:45:01 +1000260 if ((mddev->chunk_sectors << 9) % queue_logical_block_size(mddev->queue)) {
raz ben yehuda92e59b62009-06-16 17:00:57 +1000261 printk(KERN_ERR "%s chunk_size of %d not valid\n",
262 mdname(mddev),
Andre Noll9d8f0362009-06-18 08:45:01 +1000263 mddev->chunk_sectors << 9);
raz ben yehuda92e59b62009-06-16 17:00:57 +1000264 goto abort;
265 }
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +1000266
267 blk_queue_io_min(mddev->queue, mddev->chunk_sectors << 9);
268 blk_queue_io_opt(mddev->queue,
269 (mddev->chunk_sectors << 9) * mddev->raid_disks);
270
Andre Noll0825b872009-01-09 08:31:07 +1100271 printk(KERN_INFO "raid0: done.\n");
Trela, Maciej9af204c2010-03-08 16:02:44 +1100272 *private_conf = conf;
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return 0;
Andre Noll5568a602009-06-16 16:47:21 +1000275abort:
Andre Nolled7b0032009-06-16 16:47:36 +1000276 kfree(conf->strip_zone);
277 kfree(conf->devlist);
278 kfree(conf);
Trela, Maciej9af204c2010-03-08 16:02:44 +1100279 *private_conf = NULL;
Andre Nolled7b0032009-06-16 16:47:36 +1000280 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
283/**
284 * raid0_mergeable_bvec -- tell bio layer if a two requests can be merged
285 * @q: request queue
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200286 * @bvm: properties of new bio
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 * @biovec: the request that could be merged to it.
288 *
289 * Return amount of bytes we can accept at this offset
290 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200291static int raid0_mergeable_bvec(struct request_queue *q,
292 struct bvec_merge_data *bvm,
293 struct bio_vec *biovec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 mddev_t *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200296 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +1000298 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200299 unsigned int bio_sectors = bvm->bi_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
NeilBrownd6e412e2009-06-18 08:47:00 +1000301 if (is_power_of_2(chunk_sectors))
raz ben yehudafbb704e2009-06-16 17:02:05 +1000302 max = (chunk_sectors - ((sector & (chunk_sectors-1))
303 + bio_sectors)) << 9;
304 else
305 max = (chunk_sectors - (sector_div(sector, chunk_sectors)
306 + bio_sectors)) << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
308 if (max <= biovec->bv_len && bio_sectors == 0)
309 return biovec->bv_len;
310 else
311 return max;
312}
313
Dan Williams80c3a6c2009-03-17 18:10:40 -0700314static sector_t raid0_size(mddev_t *mddev, sector_t sectors, int raid_disks)
315{
316 sector_t array_sectors = 0;
317 mdk_rdev_t *rdev;
318
319 WARN_ONCE(sectors || raid_disks,
320 "%s does not support generic reshape\n", __func__);
321
322 list_for_each_entry(rdev, &mddev->disks, same_set)
323 array_sectors += rdev->sectors;
324
325 return array_sectors;
326}
327
Andre Noll8f79cfc2009-06-16 16:47:10 +1000328static int raid0_run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Trela, Maciej9af204c2010-03-08 16:02:44 +1100330 raid0_conf_t *conf;
Andre Noll5568a602009-06-16 16:47:21 +1000331 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Andre Noll9d8f0362009-06-18 08:45:01 +1000333 if (mddev->chunk_sectors == 0) {
raz ben yehudafbb704e2009-06-16 17:02:05 +1000334 printk(KERN_ERR "md/raid0: chunk size must be set.\n");
NeilBrown2604b702006-01-06 00:20:36 -0800335 return -EINVAL;
336 }
Andre Noll0894cc32009-06-18 08:49:23 +1000337 if (md_check_no_bitmap(mddev))
338 return -EINVAL;
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500339 blk_queue_max_hw_sectors(mddev->queue, mddev->chunk_sectors);
Neil Browne7e72bf2008-05-14 16:05:54 -0700340 mddev->queue->queue_lock = &mddev->queue->__queue_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Trela, Maciej9af204c2010-03-08 16:02:44 +1100342 /* if private is not null, we are here after takeover */
343 if (mddev->private == NULL) {
344 ret = create_strip_zones(mddev, &conf);
345 if (ret < 0)
346 return ret;
347 mddev->private = conf;
348 }
349 conf = mddev->private;
350 if (conf->scale_raid_disks) {
351 int i;
352 for (i=0; i < conf->strip_zone[0].nb_dev; i++)
353 conf->devlist[i]->raid_disk /= conf->scale_raid_disks;
354 /* FIXME update sysfs rd links */
355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 /* calculate array device size */
Dan Williams1f403622009-03-31 14:59:03 +1100358 md_set_array_sectors(mddev, raid0_size(mddev, 0, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Andre Nollccacc7d2009-01-09 08:31:08 +1100360 printk(KERN_INFO "raid0 : md_size is %llu sectors.\n",
361 (unsigned long long)mddev->array_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 /* calculate the max read-ahead size.
363 * For read-ahead of large files to be effective, we need to
364 * readahead at least twice a whole stripe. i.e. number of devices
365 * multiplied by chunk size times 2.
366 * If an individual device has an ra_pages greater than the
367 * chunk size, then we will not drive that device as hard as it
368 * wants. We consider this a configuration error: a larger
369 * chunksize should be used in that case.
370 */
371 {
Andre Noll9d8f0362009-06-18 08:45:01 +1000372 int stripe = mddev->raid_disks *
373 (mddev->chunk_sectors << 9) / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
375 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
376 }
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 blk_queue_merge_bvec(mddev->queue, raid0_mergeable_bvec);
raz ben yehuda46994192009-06-16 17:00:54 +1000379 dump_zones(mddev);
Andre Nollac5e7112009-08-03 10:59:47 +1000380 md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
Andre Nollfb5ab4b2009-06-16 16:48:19 +1000384static int raid0_stop(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
NeilBrown070ec552009-06-16 16:54:21 +1000386 raid0_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700389 kfree(conf->strip_zone);
Andre Nollfb5ab4b2009-06-16 16:48:19 +1000390 kfree(conf->devlist);
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700391 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 mddev->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return 0;
394}
395
NeilBrown49f357a22009-06-16 16:50:35 +1000396/* Find the zone which holds a particular offset
397 * Update *sectorp to be an offset in that zone
398 */
Andre Nolldc582662009-06-16 16:18:43 +1000399static struct strip_zone *find_zone(struct raid0_private_data *conf,
NeilBrown49f357a22009-06-16 16:50:35 +1000400 sector_t *sectorp)
Andre Nolldc582662009-06-16 16:18:43 +1000401{
402 int i;
403 struct strip_zone *z = conf->strip_zone;
NeilBrown49f357a22009-06-16 16:50:35 +1000404 sector_t sector = *sectorp;
Andre Nolldc582662009-06-16 16:18:43 +1000405
406 for (i = 0; i < conf->nr_strip_zones; i++)
NeilBrown49f357a22009-06-16 16:50:35 +1000407 if (sector < z[i].zone_end) {
408 if (i)
409 *sectorp = sector - z[i-1].zone_end;
Andre Nolldc582662009-06-16 16:18:43 +1000410 return z + i;
NeilBrown49f357a22009-06-16 16:50:35 +1000411 }
Andre Nolldc582662009-06-16 16:18:43 +1000412 BUG();
413}
414
raz ben yehudafbb704e2009-06-16 17:02:05 +1000415/*
416 * remaps the bio to the target device. we separate two flows.
417 * power 2 flow and a general flow for the sake of perfromance
418*/
419static mdk_rdev_t *map_sector(mddev_t *mddev, struct strip_zone *zone,
420 sector_t sector, sector_t *sector_offset)
421{
422 unsigned int sect_in_chunk;
423 sector_t chunk;
424 raid0_conf_t *conf = mddev->private;
NeilBrown84707f32010-03-16 17:23:35 +1100425 int raid_disks = conf->strip_zone[0].nb_dev;
Andre Noll9d8f0362009-06-18 08:45:01 +1000426 unsigned int chunk_sects = mddev->chunk_sectors;
raz ben yehudafbb704e2009-06-16 17:02:05 +1000427
NeilBrownd6e412e2009-06-18 08:47:00 +1000428 if (is_power_of_2(chunk_sects)) {
raz ben yehudafbb704e2009-06-16 17:02:05 +1000429 int chunksect_bits = ffz(~chunk_sects);
430 /* find the sector offset inside the chunk */
431 sect_in_chunk = sector & (chunk_sects - 1);
432 sector >>= chunksect_bits;
433 /* chunk in zone */
434 chunk = *sector_offset;
435 /* quotient is the chunk in real device*/
436 sector_div(chunk, zone->nb_dev << chunksect_bits);
437 } else{
438 sect_in_chunk = sector_div(sector, chunk_sects);
439 chunk = *sector_offset;
440 sector_div(chunk, chunk_sects * zone->nb_dev);
441 }
442 /*
443 * position the bio over the real device
444 * real sector = chunk in device + starting of zone
445 * + the position in the chunk
446 */
447 *sector_offset = (chunk * chunk_sects) + sect_in_chunk;
NeilBrown84707f32010-03-16 17:23:35 +1100448 return conf->devlist[(zone - conf->strip_zone)*raid_disks
raz ben yehudafbb704e2009-06-16 17:02:05 +1000449 + sector_div(sector, zone->nb_dev)];
450}
451
452/*
453 * Is io distribute over 1 or more chunks ?
454*/
455static inline int is_io_in_chunk_boundary(mddev_t *mddev,
456 unsigned int chunk_sects, struct bio *bio)
457{
NeilBrownd6e412e2009-06-18 08:47:00 +1000458 if (likely(is_power_of_2(chunk_sects))) {
raz ben yehudafbb704e2009-06-16 17:02:05 +1000459 return chunk_sects >= ((bio->bi_sector & (chunk_sects-1))
460 + (bio->bi_size >> 9));
461 } else{
462 sector_t sector = bio->bi_sector;
463 return chunk_sects >= (sector_div(sector, chunk_sects)
464 + (bio->bi_size >> 9));
465 }
466}
467
468static int raid0_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 mddev_t *mddev = q->queuedata;
raz ben yehudafbb704e2009-06-16 17:02:05 +1000471 unsigned int chunk_sects;
472 sector_t sector_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 struct strip_zone *zone;
474 mdk_rdev_t *tmp_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Jens Axboe1f98a132009-09-11 14:32:04 +0200476 if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) {
NeilBrowna2826aa2009-12-14 12:49:49 +1100477 md_barrier_request(mddev, bio);
NeilBrowne5dcdd82005-09-09 16:23:41 -0700478 return 0;
479 }
480
Andre Noll9d8f0362009-06-18 08:45:01 +1000481 chunk_sects = mddev->chunk_sectors;
raz ben yehudafbb704e2009-06-16 17:02:05 +1000482 if (unlikely(!is_io_in_chunk_boundary(mddev, chunk_sects, bio))) {
483 sector_t sector = bio->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 struct bio_pair *bp;
485 /* Sanity check -- queue functions should prevent this happening */
486 if (bio->bi_vcnt != 1 ||
487 bio->bi_idx != 0)
488 goto bad_map;
489 /* This is a one page bio that upper layers
490 * refuse to split for us, so we need to split it.
491 */
NeilBrownd6e412e2009-06-18 08:47:00 +1000492 if (likely(is_power_of_2(chunk_sects)))
raz ben yehudafbb704e2009-06-16 17:02:05 +1000493 bp = bio_split(bio, chunk_sects - (sector &
494 (chunk_sects-1)));
495 else
496 bp = bio_split(bio, chunk_sects -
497 sector_div(sector, chunk_sects));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (raid0_make_request(q, &bp->bio1))
499 generic_make_request(&bp->bio1);
500 if (raid0_make_request(q, &bp->bio2))
501 generic_make_request(&bp->bio2);
502
503 bio_pair_release(bp);
504 return 0;
505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
raz ben yehudafbb704e2009-06-16 17:02:05 +1000507 sector_offset = bio->bi_sector;
508 zone = find_zone(mddev->private, &sector_offset);
509 tmp_dev = map_sector(mddev, zone, bio->bi_sector,
510 &sector_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 bio->bi_bdev = tmp_dev->bdev;
raz ben yehudafbb704e2009-06-16 17:02:05 +1000512 bio->bi_sector = sector_offset + zone->dev_start +
513 tmp_dev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 /*
515 * Let the main block layer submit the IO and resolve recursion:
516 */
517 return 1;
518
519bad_map:
520 printk("raid0_make_request bug: can't convert block across chunks"
Andre Nolla4712002009-01-09 08:31:06 +1100521 " or bigger than %dk %llu %d\n", chunk_sects / 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
523
NeilBrown6712ecf2007-09-27 12:47:43 +0200524 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return 0;
526}
NeilBrown8299d7f2007-10-16 23:30:53 -0700527
raz ben yehuda1b961422009-06-16 16:57:40 +1000528static void raid0_status(struct seq_file *seq, mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
530#undef MD_DEBUG
531#ifdef MD_DEBUG
532 int j, k, h;
533 char b[BDEVNAME_SIZE];
NeilBrown070ec552009-06-16 16:54:21 +1000534 raid0_conf_t *conf = mddev->private;
NeilBrown84707f32010-03-16 17:23:35 +1100535 int raid_disks = conf->strip_zone[0].nb_dev;
NeilBrown8299d7f2007-10-16 23:30:53 -0700536
raz ben yehuda1b961422009-06-16 16:57:40 +1000537 sector_t zone_size;
538 sector_t zone_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 h = 0;
raz ben yehuda1b961422009-06-16 16:57:40 +1000540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 for (j = 0; j < conf->nr_strip_zones; j++) {
542 seq_printf(seq, " z%d", j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 seq_printf(seq, "=[");
544 for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
NeilBrown8299d7f2007-10-16 23:30:53 -0700545 seq_printf(seq, "%s/", bdevname(
NeilBrown84707f32010-03-16 17:23:35 +1100546 conf->devlist[j*raid_disks + k]
raz ben yehuda1b961422009-06-16 16:57:40 +1000547 ->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
raz ben yehuda1b961422009-06-16 16:57:40 +1000549 zone_size = conf->strip_zone[j].zone_end - zone_start;
550 seq_printf(seq, "] ze=%lld ds=%lld s=%lld\n",
551 (unsigned long long)zone_start>>1,
552 (unsigned long long)conf->strip_zone[j].dev_start>>1,
553 (unsigned long long)zone_size>>1);
554 zone_start = conf->strip_zone[j].zone_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556#endif
Andre Noll9d8f0362009-06-18 08:45:01 +1000557 seq_printf(seq, " %dk chunks", mddev->chunk_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return;
559}
560
Trela, Maciej9af204c2010-03-08 16:02:44 +1100561static void *raid0_takeover_raid5(mddev_t *mddev)
562{
563 mdk_rdev_t *rdev;
564 raid0_conf_t *priv_conf;
565
566 if (mddev->degraded != 1) {
567 printk(KERN_ERR "md: raid5 must be degraded! Degraded disks: %d\n",
568 mddev->degraded);
569 return ERR_PTR(-EINVAL);
570 }
571
572 list_for_each_entry(rdev, &mddev->disks, same_set) {
573 /* check slot number for a disk */
574 if (rdev->raid_disk == mddev->raid_disks-1) {
575 printk(KERN_ERR "md: raid5 must have missing parity disk!\n");
576 return ERR_PTR(-EINVAL);
577 }
578 }
579
580 /* Set new parameters */
581 mddev->new_level = 0;
582 mddev->new_chunk_sectors = mddev->chunk_sectors;
583 mddev->raid_disks--;
584 mddev->delta_disks = -1;
585 /* make sure it will be not marked as dirty */
586 mddev->recovery_cp = MaxSector;
587
588 create_strip_zones(mddev, &priv_conf);
589 return priv_conf;
590}
591
592static void *raid0_takeover_raid10(mddev_t *mddev)
593{
594 raid0_conf_t *priv_conf;
595
596 /* Check layout:
597 * - far_copies must be 1
598 * - near_copies must be 2
599 * - disks number must be even
600 * - all mirrors must be already degraded
601 */
602 if (mddev->layout != ((1 << 8) + 2)) {
603 printk(KERN_ERR "md: Raid0 cannot takover layout: %x\n",
604 mddev->layout);
605 return ERR_PTR(-EINVAL);
606 }
607 if (mddev->raid_disks & 1) {
608 printk(KERN_ERR "md: Raid0 cannot takover Raid10 with odd disk number.\n");
609 return ERR_PTR(-EINVAL);
610 }
611 if (mddev->degraded != (mddev->raid_disks>>1)) {
612 printk(KERN_ERR "md: All mirrors must be already degraded!\n");
613 return ERR_PTR(-EINVAL);
614 }
615
616 /* Set new parameters */
617 mddev->new_level = 0;
618 mddev->new_chunk_sectors = mddev->chunk_sectors;
619 mddev->delta_disks = - mddev->raid_disks / 2;
620 mddev->raid_disks += mddev->delta_disks;
621 mddev->degraded = 0;
622 /* make sure it will be not marked as dirty */
623 mddev->recovery_cp = MaxSector;
624
625 create_strip_zones(mddev, &priv_conf);
626 priv_conf->scale_raid_disks = 2;
627 return priv_conf;
628}
629
630static void *raid0_takeover(mddev_t *mddev)
631{
632 /* raid0 can take over:
633 * raid5 - providing it is Raid4 layout and one disk is faulty
634 * raid10 - assuming we have all necessary active disks
635 */
636 if (mddev->level == 5) {
637 if (mddev->layout == ALGORITHM_PARITY_N)
638 return raid0_takeover_raid5(mddev);
639
640 printk(KERN_ERR "md: Raid can only takeover Raid5 with layout: %d\n",
641 ALGORITHM_PARITY_N);
642 }
643
644 if (mddev->level == 10)
645 return raid0_takeover_raid10(mddev);
646
647 return ERR_PTR(-EINVAL);
648}
649
650static void raid0_quiesce(mddev_t *mddev, int state)
651{
652}
653
NeilBrown2604b702006-01-06 00:20:36 -0800654static struct mdk_personality raid0_personality=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
656 .name = "raid0",
NeilBrown2604b702006-01-06 00:20:36 -0800657 .level = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 .owner = THIS_MODULE,
659 .make_request = raid0_make_request,
660 .run = raid0_run,
661 .stop = raid0_stop,
662 .status = raid0_status,
Dan Williams80c3a6c2009-03-17 18:10:40 -0700663 .size = raid0_size,
Trela, Maciej9af204c2010-03-08 16:02:44 +1100664 .takeover = raid0_takeover,
665 .quiesce = raid0_quiesce,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666};
667
668static int __init raid0_init (void)
669{
NeilBrown2604b702006-01-06 00:20:36 -0800670 return register_md_personality (&raid0_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
673static void raid0_exit (void)
674{
NeilBrown2604b702006-01-06 00:20:36 -0800675 unregister_md_personality (&raid0_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
678module_init(raid0_init);
679module_exit(raid0_exit);
680MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +1100681MODULE_DESCRIPTION("RAID0 (striping) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682MODULE_ALIAS("md-personality-2"); /* RAID0 */
NeilBrownd9d166c2006-01-06 00:20:51 -0800683MODULE_ALIAS("md-raid0");
NeilBrown2604b702006-01-06 00:20:36 -0800684MODULE_ALIAS("md-level-0");