blob: 68a4d129206ee2a89e1fe32852d531a694ff5d40 [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;
NeilBrown070ec552009-06-16 16:54:21 +100029 raid0_conf_t *conf = mddev->private;
NeilBrownb4145792009-06-16 16:50:52 +100030 mdk_rdev_t **devlist = conf->devlist;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 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;
NeilBrown070ec552009-06-16 16:54:21 +100043 raid0_conf_t *conf = mddev->private;
NeilBrownb4145792009-06-16 16:50:52 +100044 mdk_rdev_t **devlist = conf->devlist;
NeilBrown26be34d2006-10-03 01:15:53 -070045 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
raz ben yehuda46994192009-06-16 17:00:54 +100055/*
56 * inform the user of the raid configuration
57*/
58static void dump_zones(mddev_t *mddev)
59{
60 int j, k, h;
61 sector_t zone_size = 0;
62 sector_t zone_start = 0;
63 char b[BDEVNAME_SIZE];
64 raid0_conf_t *conf = mddev->private;
65 printk(KERN_INFO "******* %s configuration *********\n",
66 mdname(mddev));
67 h = 0;
68 for (j = 0; j < conf->nr_strip_zones; j++) {
69 printk(KERN_INFO "zone%d=[", j);
70 for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
71 printk("%s/",
72 bdevname(conf->devlist[j*mddev->raid_disks
73 + k]->bdev, b));
74 printk("]\n");
75
76 zone_size = conf->strip_zone[j].zone_end - zone_start;
77 printk(KERN_INFO " zone offset=%llukb "
78 "device offset=%llukb size=%llukb\n",
79 (unsigned long long)zone_start>>1,
80 (unsigned long long)conf->strip_zone[j].dev_start>>1,
81 (unsigned long long)zone_size>>1);
82 zone_start = conf->strip_zone[j].zone_end;
83 }
84 printk(KERN_INFO "**********************************\n\n");
85}
86
Andre Nolled7b0032009-06-16 16:47:36 +100087static int create_strip_zones(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
NeilBrowna9f326e2009-09-23 18:06:41 +100089 int i, c, err;
NeilBrown49f357a22009-06-16 16:50:35 +100090 sector_t curr_zone_end, sectors;
NeilBrownb4145792009-06-16 16:50:52 +100091 mdk_rdev_t *smallest, *rdev1, *rdev2, *rdev, **dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 struct strip_zone *zone;
93 int cnt;
94 char b[BDEVNAME_SIZE];
Andre Nolled7b0032009-06-16 16:47:36 +100095 raid0_conf_t *conf = kzalloc(sizeof(*conf), GFP_KERNEL);
96
97 if (!conf)
98 return -ENOMEM;
Cheng Renquan159ec1f2009-01-09 08:31:08 +110099 list_for_each_entry(rdev1, &mddev->disks, same_set) {
Andre Noll0825b87a2009-01-09 08:31:07 +1100100 printk(KERN_INFO "raid0: looking at %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 bdevname(rdev1->bdev,b));
102 c = 0;
NeilBrown13f26822009-06-18 08:48:55 +1000103
104 /* round size to chunk_size */
105 sectors = rdev1->sectors;
106 sector_div(sectors, mddev->chunk_sectors);
107 rdev1->sectors = sectors * mddev->chunk_sectors;
108
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100109 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Andre Noll0825b87a2009-01-09 08:31:07 +1100110 printk(KERN_INFO "raid0: comparing %s(%llu)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 bdevname(rdev1->bdev,b),
Andre Nolldd8ac332009-03-31 14:33:13 +1100112 (unsigned long long)rdev1->sectors);
Andre Noll0825b87a2009-01-09 08:31:07 +1100113 printk(KERN_INFO " with %s(%llu)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 bdevname(rdev2->bdev,b),
Andre Nolldd8ac332009-03-31 14:33:13 +1100115 (unsigned long long)rdev2->sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (rdev2 == rdev1) {
Andre Noll0825b87a2009-01-09 08:31:07 +1100117 printk(KERN_INFO "raid0: END\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 break;
119 }
Andre Nolldd8ac332009-03-31 14:33:13 +1100120 if (rdev2->sectors == rdev1->sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 /*
122 * Not unique, don't count it as a new
123 * group
124 */
Andre Noll0825b87a2009-01-09 08:31:07 +1100125 printk(KERN_INFO "raid0: EQUAL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 c = 1;
127 break;
128 }
Andre Noll0825b87a2009-01-09 08:31:07 +1100129 printk(KERN_INFO "raid0: NOT EQUAL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
131 if (!c) {
Andre Noll0825b87a2009-01-09 08:31:07 +1100132 printk(KERN_INFO "raid0: ==> UNIQUE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 conf->nr_strip_zones++;
Andre Noll0825b87a2009-01-09 08:31:07 +1100134 printk(KERN_INFO "raid0: %d zones\n",
135 conf->nr_strip_zones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
137 }
Andre Noll0825b87a2009-01-09 08:31:07 +1100138 printk(KERN_INFO "raid0: FINAL %d zones\n", conf->nr_strip_zones);
Andre Nolled7b0032009-06-16 16:47:36 +1000139 err = -ENOMEM;
NeilBrown9ffae0c2006-01-06 00:20:32 -0800140 conf->strip_zone = kzalloc(sizeof(struct strip_zone)*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 conf->nr_strip_zones, GFP_KERNEL);
142 if (!conf->strip_zone)
Andre Nolled7b0032009-06-16 16:47:36 +1000143 goto abort;
NeilBrown9ffae0c2006-01-06 00:20:32 -0800144 conf->devlist = kzalloc(sizeof(mdk_rdev_t*)*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 conf->nr_strip_zones*mddev->raid_disks,
146 GFP_KERNEL);
147 if (!conf->devlist)
Andre Nolled7b0032009-06-16 16:47:36 +1000148 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 /* The first zone must contain all devices, so here we check that
151 * there is a proper alignment of slots to devices and find them all
152 */
153 zone = &conf->strip_zone[0];
154 cnt = 0;
155 smallest = NULL;
NeilBrownb4145792009-06-16 16:50:52 +1000156 dev = conf->devlist;
Andre Nolled7b0032009-06-16 16:47:36 +1000157 err = -EINVAL;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100158 list_for_each_entry(rdev1, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 int j = rdev1->raid_disk;
160
161 if (j < 0 || j >= mddev->raid_disks) {
Andre Noll0825b87a2009-01-09 08:31:07 +1100162 printk(KERN_ERR "raid0: bad disk number %d - "
163 "aborting!\n", j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 goto abort;
165 }
NeilBrownb4145792009-06-16 16:50:52 +1000166 if (dev[j]) {
Andre Noll0825b87a2009-01-09 08:31:07 +1100167 printk(KERN_ERR "raid0: multiple devices for %d - "
168 "aborting!\n", j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 goto abort;
170 }
NeilBrownb4145792009-06-16 16:50:52 +1000171 dev[j] = rdev1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +1000173 disk_stack_limits(mddev->gendisk, rdev1->bdev,
174 rdev1->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 /* as we don't honour merge_bvec_fn, we must never risk
176 * violating it, so limit ->max_sector to one PAGE, as
177 * a one page request is never in violation.
178 */
179
180 if (rdev1->bdev->bd_disk->queue->merge_bvec_fn &&
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400181 queue_max_sectors(mddev->queue) > (PAGE_SIZE>>9))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
183
Andre Nolldd8ac332009-03-31 14:33:13 +1100184 if (!smallest || (rdev1->sectors < smallest->sectors))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 smallest = rdev1;
186 cnt++;
187 }
188 if (cnt != mddev->raid_disks) {
Andre Noll0825b87a2009-01-09 08:31:07 +1100189 printk(KERN_ERR "raid0: too few disks (%d of %d) - "
190 "aborting!\n", cnt, mddev->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 goto abort;
192 }
193 zone->nb_dev = cnt;
NeilBrown49f357a22009-06-16 16:50:35 +1000194 zone->zone_end = smallest->sectors * cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
NeilBrown49f357a22009-06-16 16:50:35 +1000196 curr_zone_end = zone->zone_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 /* now do the other zones */
199 for (i = 1; i < conf->nr_strip_zones; i++)
200 {
NeilBrowna9f326e2009-09-23 18:06:41 +1000201 int j;
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 zone = conf->strip_zone + i;
NeilBrownb4145792009-06-16 16:50:52 +1000204 dev = conf->devlist + i * mddev->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Andre Noll0825b87a2009-01-09 08:31:07 +1100206 printk(KERN_INFO "raid0: zone %d\n", i);
NeilBrownd27a43ab2009-06-16 16:46:46 +1000207 zone->dev_start = smallest->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 smallest = NULL;
209 c = 0;
210
211 for (j=0; j<cnt; j++) {
NeilBrownb4145792009-06-16 16:50:52 +1000212 rdev = conf->devlist[j];
Andre Noll0825b87a2009-01-09 08:31:07 +1100213 printk(KERN_INFO "raid0: checking %s ...",
214 bdevname(rdev->bdev, b));
NeilBrownd27a43ab2009-06-16 16:46:46 +1000215 if (rdev->sectors <= zone->dev_start) {
Andre Noll0825b87a2009-01-09 08:31:07 +1100216 printk(KERN_INFO " nope.\n");
Andre Nolldd8ac332009-03-31 14:33:13 +1100217 continue;
218 }
219 printk(KERN_INFO " contained as device %d\n", c);
NeilBrownb4145792009-06-16 16:50:52 +1000220 dev[c] = rdev;
Andre Nolldd8ac332009-03-31 14:33:13 +1100221 c++;
222 if (!smallest || rdev->sectors < smallest->sectors) {
223 smallest = rdev;
224 printk(KERN_INFO " (%llu) is smallest!.\n",
225 (unsigned long long)rdev->sectors);
226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
228
229 zone->nb_dev = c;
NeilBrown49f357a22009-06-16 16:50:35 +1000230 sectors = (smallest->sectors - zone->dev_start) * c;
Andre Noll83838ed2009-01-09 08:31:07 +1100231 printk(KERN_INFO "raid0: zone->nb_dev: %d, sectors: %llu\n",
NeilBrown49f357a22009-06-16 16:50:35 +1000232 zone->nb_dev, (unsigned long long)sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
NeilBrown49f357a22009-06-16 16:50:35 +1000234 curr_zone_end += sectors;
NeilBrownd27a43ab2009-06-16 16:46:46 +1000235 zone->zone_end = curr_zone_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Andre Noll6b8796c2009-01-09 08:31:07 +1100237 printk(KERN_INFO "raid0: current zone start: %llu\n",
NeilBrownd27a43ab2009-06-16 16:46:46 +1000238 (unsigned long long)smallest->sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 mddev->queue->unplug_fn = raid0_unplug;
NeilBrown26be34d2006-10-03 01:15:53 -0700241 mddev->queue->backing_dev_info.congested_fn = raid0_congested;
242 mddev->queue->backing_dev_info.congested_data = mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
raz ben yehuda92e59b62009-06-16 17:00:57 +1000244 /*
245 * now since we have the hard sector sizes, we can make sure
246 * chunk size is a multiple of that sector size
247 */
Andre Noll9d8f0362009-06-18 08:45:01 +1000248 if ((mddev->chunk_sectors << 9) % queue_logical_block_size(mddev->queue)) {
raz ben yehuda92e59b62009-06-16 17:00:57 +1000249 printk(KERN_ERR "%s chunk_size of %d not valid\n",
250 mdname(mddev),
Andre Noll9d8f0362009-06-18 08:45:01 +1000251 mddev->chunk_sectors << 9);
raz ben yehuda92e59b62009-06-16 17:00:57 +1000252 goto abort;
253 }
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +1000254
255 blk_queue_io_min(mddev->queue, mddev->chunk_sectors << 9);
256 blk_queue_io_opt(mddev->queue,
257 (mddev->chunk_sectors << 9) * mddev->raid_disks);
258
Andre Noll0825b87a2009-01-09 08:31:07 +1100259 printk(KERN_INFO "raid0: done.\n");
Andre Nolled7b0032009-06-16 16:47:36 +1000260 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return 0;
Andre Noll5568a602009-06-16 16:47:21 +1000262abort:
Andre Nolled7b0032009-06-16 16:47:36 +1000263 kfree(conf->strip_zone);
264 kfree(conf->devlist);
265 kfree(conf);
266 mddev->private = NULL;
267 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
270/**
271 * raid0_mergeable_bvec -- tell bio layer if a two requests can be merged
272 * @q: request queue
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200273 * @bvm: properties of new bio
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 * @biovec: the request that could be merged to it.
275 *
276 * Return amount of bytes we can accept at this offset
277 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200278static int raid0_mergeable_bvec(struct request_queue *q,
279 struct bvec_merge_data *bvm,
280 struct bio_vec *biovec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 mddev_t *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200283 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +1000285 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200286 unsigned int bio_sectors = bvm->bi_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
NeilBrownd6e412e2009-06-18 08:47:00 +1000288 if (is_power_of_2(chunk_sectors))
raz ben yehudafbb704e2009-06-16 17:02:05 +1000289 max = (chunk_sectors - ((sector & (chunk_sectors-1))
290 + bio_sectors)) << 9;
291 else
292 max = (chunk_sectors - (sector_div(sector, chunk_sectors)
293 + bio_sectors)) << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
295 if (max <= biovec->bv_len && bio_sectors == 0)
296 return biovec->bv_len;
297 else
298 return max;
299}
300
Dan Williams80c3a6c2009-03-17 18:10:40 -0700301static sector_t raid0_size(mddev_t *mddev, sector_t sectors, int raid_disks)
302{
303 sector_t array_sectors = 0;
304 mdk_rdev_t *rdev;
305
306 WARN_ONCE(sectors || raid_disks,
307 "%s does not support generic reshape\n", __func__);
308
309 list_for_each_entry(rdev, &mddev->disks, same_set)
310 array_sectors += rdev->sectors;
311
312 return array_sectors;
313}
314
Andre Noll8f79cfc2009-06-16 16:47:10 +1000315static int raid0_run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Andre Noll5568a602009-06-16 16:47:21 +1000317 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Andre Noll9d8f0362009-06-18 08:45:01 +1000319 if (mddev->chunk_sectors == 0) {
raz ben yehudafbb704e2009-06-16 17:02:05 +1000320 printk(KERN_ERR "md/raid0: chunk size must be set.\n");
NeilBrown2604b702006-01-06 00:20:36 -0800321 return -EINVAL;
322 }
Andre Noll0894cc32009-06-18 08:49:23 +1000323 if (md_check_no_bitmap(mddev))
324 return -EINVAL;
Andre Noll9d8f0362009-06-18 08:45:01 +1000325 blk_queue_max_sectors(mddev->queue, mddev->chunk_sectors);
Neil Browne7e72bf2008-05-14 16:05:54 -0700326 mddev->queue->queue_lock = &mddev->queue->__queue_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Andre Noll5568a602009-06-16 16:47:21 +1000328 ret = create_strip_zones(mddev);
329 if (ret < 0)
Andre Nolled7b0032009-06-16 16:47:36 +1000330 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 /* calculate array device size */
Dan Williams1f403622009-03-31 14:59:03 +1100333 md_set_array_sectors(mddev, raid0_size(mddev, 0, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Andre Nollccacc7d2009-01-09 08:31:08 +1100335 printk(KERN_INFO "raid0 : md_size is %llu sectors.\n",
336 (unsigned long long)mddev->array_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 /* calculate the max read-ahead size.
338 * For read-ahead of large files to be effective, we need to
339 * readahead at least twice a whole stripe. i.e. number of devices
340 * multiplied by chunk size times 2.
341 * If an individual device has an ra_pages greater than the
342 * chunk size, then we will not drive that device as hard as it
343 * wants. We consider this a configuration error: a larger
344 * chunksize should be used in that case.
345 */
346 {
Andre Noll9d8f0362009-06-18 08:45:01 +1000347 int stripe = mddev->raid_disks *
348 (mddev->chunk_sectors << 9) / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
350 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
351 }
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 blk_queue_merge_bvec(mddev->queue, raid0_mergeable_bvec);
raz ben yehuda46994192009-06-16 17:00:54 +1000354 dump_zones(mddev);
Andre Nollac5e7112009-08-03 10:59:47 +1000355 md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
Andre Nollfb5ab4b2009-06-16 16:48:19 +1000359static int raid0_stop(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
NeilBrown070ec552009-06-16 16:54:21 +1000361 raid0_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700364 kfree(conf->strip_zone);
Andre Nollfb5ab4b2009-06-16 16:48:19 +1000365 kfree(conf->devlist);
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700366 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 mddev->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return 0;
369}
370
NeilBrown49f357a22009-06-16 16:50:35 +1000371/* Find the zone which holds a particular offset
372 * Update *sectorp to be an offset in that zone
373 */
Andre Nolldc582662009-06-16 16:18:43 +1000374static struct strip_zone *find_zone(struct raid0_private_data *conf,
NeilBrown49f357a22009-06-16 16:50:35 +1000375 sector_t *sectorp)
Andre Nolldc582662009-06-16 16:18:43 +1000376{
377 int i;
378 struct strip_zone *z = conf->strip_zone;
NeilBrown49f357a22009-06-16 16:50:35 +1000379 sector_t sector = *sectorp;
Andre Nolldc582662009-06-16 16:18:43 +1000380
381 for (i = 0; i < conf->nr_strip_zones; i++)
NeilBrown49f357a22009-06-16 16:50:35 +1000382 if (sector < z[i].zone_end) {
383 if (i)
384 *sectorp = sector - z[i-1].zone_end;
Andre Nolldc582662009-06-16 16:18:43 +1000385 return z + i;
NeilBrown49f357a22009-06-16 16:50:35 +1000386 }
Andre Nolldc582662009-06-16 16:18:43 +1000387 BUG();
388}
389
raz ben yehudafbb704e2009-06-16 17:02:05 +1000390/*
391 * remaps the bio to the target device. we separate two flows.
392 * power 2 flow and a general flow for the sake of perfromance
393*/
394static mdk_rdev_t *map_sector(mddev_t *mddev, struct strip_zone *zone,
395 sector_t sector, sector_t *sector_offset)
396{
397 unsigned int sect_in_chunk;
398 sector_t chunk;
399 raid0_conf_t *conf = mddev->private;
Andre Noll9d8f0362009-06-18 08:45:01 +1000400 unsigned int chunk_sects = mddev->chunk_sectors;
raz ben yehudafbb704e2009-06-16 17:02:05 +1000401
NeilBrownd6e412e2009-06-18 08:47:00 +1000402 if (is_power_of_2(chunk_sects)) {
raz ben yehudafbb704e2009-06-16 17:02:05 +1000403 int chunksect_bits = ffz(~chunk_sects);
404 /* find the sector offset inside the chunk */
405 sect_in_chunk = sector & (chunk_sects - 1);
406 sector >>= chunksect_bits;
407 /* chunk in zone */
408 chunk = *sector_offset;
409 /* quotient is the chunk in real device*/
410 sector_div(chunk, zone->nb_dev << chunksect_bits);
411 } else{
412 sect_in_chunk = sector_div(sector, chunk_sects);
413 chunk = *sector_offset;
414 sector_div(chunk, chunk_sects * zone->nb_dev);
415 }
416 /*
417 * position the bio over the real device
418 * real sector = chunk in device + starting of zone
419 * + the position in the chunk
420 */
421 *sector_offset = (chunk * chunk_sects) + sect_in_chunk;
422 return conf->devlist[(zone - conf->strip_zone)*mddev->raid_disks
423 + sector_div(sector, zone->nb_dev)];
424}
425
426/*
427 * Is io distribute over 1 or more chunks ?
428*/
429static inline int is_io_in_chunk_boundary(mddev_t *mddev,
430 unsigned int chunk_sects, struct bio *bio)
431{
NeilBrownd6e412e2009-06-18 08:47:00 +1000432 if (likely(is_power_of_2(chunk_sects))) {
raz ben yehudafbb704e2009-06-16 17:02:05 +1000433 return chunk_sects >= ((bio->bi_sector & (chunk_sects-1))
434 + (bio->bi_size >> 9));
435 } else{
436 sector_t sector = bio->bi_sector;
437 return chunk_sects >= (sector_div(sector, chunk_sects)
438 + (bio->bi_size >> 9));
439 }
440}
441
442static int raid0_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444 mddev_t *mddev = q->queuedata;
raz ben yehudafbb704e2009-06-16 17:02:05 +1000445 unsigned int chunk_sects;
446 sector_t sector_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 struct strip_zone *zone;
448 mdk_rdev_t *tmp_dev;
Jens Axboea3623572005-11-01 09:26:16 +0100449 const int rw = bio_data_dir(bio);
Tejun Heoc9959052008-08-25 19:47:21 +0900450 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Jens Axboe1f98a132009-09-11 14:32:04 +0200452 if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) {
NeilBrown6712ecf2007-09-27 12:47:43 +0200453 bio_endio(bio, -EOPNOTSUPP);
NeilBrowne5dcdd82005-09-09 16:23:41 -0700454 return 0;
455 }
456
Tejun Heo074a7ac2008-08-25 19:56:14 +0900457 cpu = part_stat_lock();
458 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
459 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
460 bio_sectors(bio));
461 part_stat_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Andre Noll9d8f0362009-06-18 08:45:01 +1000463 chunk_sects = mddev->chunk_sectors;
raz ben yehudafbb704e2009-06-16 17:02:05 +1000464 if (unlikely(!is_io_in_chunk_boundary(mddev, chunk_sects, bio))) {
465 sector_t sector = bio->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 struct bio_pair *bp;
467 /* Sanity check -- queue functions should prevent this happening */
468 if (bio->bi_vcnt != 1 ||
469 bio->bi_idx != 0)
470 goto bad_map;
471 /* This is a one page bio that upper layers
472 * refuse to split for us, so we need to split it.
473 */
NeilBrownd6e412e2009-06-18 08:47:00 +1000474 if (likely(is_power_of_2(chunk_sects)))
raz ben yehudafbb704e2009-06-16 17:02:05 +1000475 bp = bio_split(bio, chunk_sects - (sector &
476 (chunk_sects-1)));
477 else
478 bp = bio_split(bio, chunk_sects -
479 sector_div(sector, chunk_sects));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (raid0_make_request(q, &bp->bio1))
481 generic_make_request(&bp->bio1);
482 if (raid0_make_request(q, &bp->bio2))
483 generic_make_request(&bp->bio2);
484
485 bio_pair_release(bp);
486 return 0;
487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
raz ben yehudafbb704e2009-06-16 17:02:05 +1000489 sector_offset = bio->bi_sector;
490 zone = find_zone(mddev->private, &sector_offset);
491 tmp_dev = map_sector(mddev, zone, bio->bi_sector,
492 &sector_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 bio->bi_bdev = tmp_dev->bdev;
raz ben yehudafbb704e2009-06-16 17:02:05 +1000494 bio->bi_sector = sector_offset + zone->dev_start +
495 tmp_dev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 /*
497 * Let the main block layer submit the IO and resolve recursion:
498 */
499 return 1;
500
501bad_map:
502 printk("raid0_make_request bug: can't convert block across chunks"
Andre Nolla4712002009-01-09 08:31:06 +1100503 " or bigger than %dk %llu %d\n", chunk_sects / 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
505
NeilBrown6712ecf2007-09-27 12:47:43 +0200506 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return 0;
508}
NeilBrown8299d7f2007-10-16 23:30:53 -0700509
raz ben yehuda1b9614292009-06-16 16:57:40 +1000510static void raid0_status(struct seq_file *seq, mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
512#undef MD_DEBUG
513#ifdef MD_DEBUG
514 int j, k, h;
515 char b[BDEVNAME_SIZE];
NeilBrown070ec552009-06-16 16:54:21 +1000516 raid0_conf_t *conf = mddev->private;
NeilBrown8299d7f2007-10-16 23:30:53 -0700517
raz ben yehuda1b9614292009-06-16 16:57:40 +1000518 sector_t zone_size;
519 sector_t zone_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 h = 0;
raz ben yehuda1b9614292009-06-16 16:57:40 +1000521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 for (j = 0; j < conf->nr_strip_zones; j++) {
523 seq_printf(seq, " z%d", j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 seq_printf(seq, "=[");
525 for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
NeilBrown8299d7f2007-10-16 23:30:53 -0700526 seq_printf(seq, "%s/", bdevname(
raz ben yehuda1b9614292009-06-16 16:57:40 +1000527 conf->devlist[j*mddev->raid_disks + k]
528 ->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
raz ben yehuda1b9614292009-06-16 16:57:40 +1000530 zone_size = conf->strip_zone[j].zone_end - zone_start;
531 seq_printf(seq, "] ze=%lld ds=%lld s=%lld\n",
532 (unsigned long long)zone_start>>1,
533 (unsigned long long)conf->strip_zone[j].dev_start>>1,
534 (unsigned long long)zone_size>>1);
535 zone_start = conf->strip_zone[j].zone_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
537#endif
Andre Noll9d8f0362009-06-18 08:45:01 +1000538 seq_printf(seq, " %dk chunks", mddev->chunk_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return;
540}
541
NeilBrown2604b702006-01-06 00:20:36 -0800542static struct mdk_personality raid0_personality=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 .name = "raid0",
NeilBrown2604b702006-01-06 00:20:36 -0800545 .level = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 .owner = THIS_MODULE,
547 .make_request = raid0_make_request,
548 .run = raid0_run,
549 .stop = raid0_stop,
550 .status = raid0_status,
Dan Williams80c3a6c2009-03-17 18:10:40 -0700551 .size = raid0_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552};
553
554static int __init raid0_init (void)
555{
NeilBrown2604b702006-01-06 00:20:36 -0800556 return register_md_personality (&raid0_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
559static void raid0_exit (void)
560{
NeilBrown2604b702006-01-06 00:20:36 -0800561 unregister_md_personality (&raid0_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
564module_init(raid0_init);
565module_exit(raid0_exit);
566MODULE_LICENSE("GPL");
567MODULE_ALIAS("md-personality-2"); /* RAID0 */
NeilBrownd9d166c2006-01-06 00:20:51 -0800568MODULE_ALIAS("md-raid0");
NeilBrown2604b702006-01-06 00:20:36 -0800569MODULE_ALIAS("md-level-0");