blob: 39936a217f95526edb70315d89e2ced1033af199 [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{
Andre Nolled7b0032009-06-16 16:47:36 +100089 int i, c, j, 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;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100103 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Andre Noll0825b87a2009-01-09 08:31:07 +1100104 printk(KERN_INFO "raid0: comparing %s(%llu)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 bdevname(rdev1->bdev,b),
Andre Nolldd8ac332009-03-31 14:33:13 +1100106 (unsigned long long)rdev1->sectors);
Andre Noll0825b87a2009-01-09 08:31:07 +1100107 printk(KERN_INFO " with %s(%llu)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 bdevname(rdev2->bdev,b),
Andre Nolldd8ac332009-03-31 14:33:13 +1100109 (unsigned long long)rdev2->sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 if (rdev2 == rdev1) {
Andre Noll0825b87a2009-01-09 08:31:07 +1100111 printk(KERN_INFO "raid0: END\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 break;
113 }
Andre Nolldd8ac332009-03-31 14:33:13 +1100114 if (rdev2->sectors == rdev1->sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 /*
116 * Not unique, don't count it as a new
117 * group
118 */
Andre Noll0825b87a2009-01-09 08:31:07 +1100119 printk(KERN_INFO "raid0: EQUAL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 c = 1;
121 break;
122 }
Andre Noll0825b87a2009-01-09 08:31:07 +1100123 printk(KERN_INFO "raid0: NOT EQUAL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
125 if (!c) {
Andre Noll0825b87a2009-01-09 08:31:07 +1100126 printk(KERN_INFO "raid0: ==> UNIQUE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 conf->nr_strip_zones++;
Andre Noll0825b87a2009-01-09 08:31:07 +1100128 printk(KERN_INFO "raid0: %d zones\n",
129 conf->nr_strip_zones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
131 }
Andre Noll0825b87a2009-01-09 08:31:07 +1100132 printk(KERN_INFO "raid0: FINAL %d zones\n", conf->nr_strip_zones);
Andre Nolled7b0032009-06-16 16:47:36 +1000133 err = -ENOMEM;
NeilBrown9ffae0c2006-01-06 00:20:32 -0800134 conf->strip_zone = kzalloc(sizeof(struct strip_zone)*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 conf->nr_strip_zones, GFP_KERNEL);
136 if (!conf->strip_zone)
Andre Nolled7b0032009-06-16 16:47:36 +1000137 goto abort;
NeilBrown9ffae0c2006-01-06 00:20:32 -0800138 conf->devlist = kzalloc(sizeof(mdk_rdev_t*)*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 conf->nr_strip_zones*mddev->raid_disks,
140 GFP_KERNEL);
141 if (!conf->devlist)
Andre Nolled7b0032009-06-16 16:47:36 +1000142 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 /* The first zone must contain all devices, so here we check that
145 * there is a proper alignment of slots to devices and find them all
146 */
147 zone = &conf->strip_zone[0];
148 cnt = 0;
149 smallest = NULL;
NeilBrownb4145792009-06-16 16:50:52 +1000150 dev = conf->devlist;
Andre Nolled7b0032009-06-16 16:47:36 +1000151 err = -EINVAL;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100152 list_for_each_entry(rdev1, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 int j = rdev1->raid_disk;
154
155 if (j < 0 || j >= mddev->raid_disks) {
Andre Noll0825b87a2009-01-09 08:31:07 +1100156 printk(KERN_ERR "raid0: bad disk number %d - "
157 "aborting!\n", j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 goto abort;
159 }
NeilBrownb4145792009-06-16 16:50:52 +1000160 if (dev[j]) {
Andre Noll0825b87a2009-01-09 08:31:07 +1100161 printk(KERN_ERR "raid0: multiple devices for %d - "
162 "aborting!\n", j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 goto abort;
164 }
NeilBrownb4145792009-06-16 16:50:52 +1000165 dev[j] = rdev1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 blk_queue_stack_limits(mddev->queue,
168 rdev1->bdev->bd_disk->queue);
169 /* as we don't honour merge_bvec_fn, we must never risk
170 * violating it, so limit ->max_sector to one PAGE, as
171 * a one page request is never in violation.
172 */
173
174 if (rdev1->bdev->bd_disk->queue->merge_bvec_fn &&
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400175 queue_max_sectors(mddev->queue) > (PAGE_SIZE>>9))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
177
Andre Nolldd8ac332009-03-31 14:33:13 +1100178 if (!smallest || (rdev1->sectors < smallest->sectors))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 smallest = rdev1;
180 cnt++;
181 }
182 if (cnt != mddev->raid_disks) {
Andre Noll0825b87a2009-01-09 08:31:07 +1100183 printk(KERN_ERR "raid0: too few disks (%d of %d) - "
184 "aborting!\n", cnt, mddev->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 goto abort;
186 }
187 zone->nb_dev = cnt;
NeilBrown49f357a22009-06-16 16:50:35 +1000188 zone->zone_end = smallest->sectors * cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
NeilBrown49f357a22009-06-16 16:50:35 +1000190 curr_zone_end = zone->zone_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 /* now do the other zones */
193 for (i = 1; i < conf->nr_strip_zones; i++)
194 {
195 zone = conf->strip_zone + i;
NeilBrownb4145792009-06-16 16:50:52 +1000196 dev = conf->devlist + i * mddev->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Andre Noll0825b87a2009-01-09 08:31:07 +1100198 printk(KERN_INFO "raid0: zone %d\n", i);
NeilBrownd27a43ab2009-06-16 16:46:46 +1000199 zone->dev_start = smallest->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 smallest = NULL;
201 c = 0;
202
203 for (j=0; j<cnt; j++) {
204 char b[BDEVNAME_SIZE];
NeilBrownb4145792009-06-16 16:50:52 +1000205 rdev = conf->devlist[j];
Andre Noll0825b87a2009-01-09 08:31:07 +1100206 printk(KERN_INFO "raid0: checking %s ...",
207 bdevname(rdev->bdev, b));
NeilBrownd27a43ab2009-06-16 16:46:46 +1000208 if (rdev->sectors <= zone->dev_start) {
Andre Noll0825b87a2009-01-09 08:31:07 +1100209 printk(KERN_INFO " nope.\n");
Andre Nolldd8ac332009-03-31 14:33:13 +1100210 continue;
211 }
212 printk(KERN_INFO " contained as device %d\n", c);
NeilBrownb4145792009-06-16 16:50:52 +1000213 dev[c] = rdev;
Andre Nolldd8ac332009-03-31 14:33:13 +1100214 c++;
215 if (!smallest || rdev->sectors < smallest->sectors) {
216 smallest = rdev;
217 printk(KERN_INFO " (%llu) is smallest!.\n",
218 (unsigned long long)rdev->sectors);
219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
221
222 zone->nb_dev = c;
NeilBrown49f357a22009-06-16 16:50:35 +1000223 sectors = (smallest->sectors - zone->dev_start) * c;
Andre Noll83838ed2009-01-09 08:31:07 +1100224 printk(KERN_INFO "raid0: zone->nb_dev: %d, sectors: %llu\n",
NeilBrown49f357a22009-06-16 16:50:35 +1000225 zone->nb_dev, (unsigned long long)sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
NeilBrown49f357a22009-06-16 16:50:35 +1000227 curr_zone_end += sectors;
NeilBrownd27a43ab2009-06-16 16:46:46 +1000228 zone->zone_end = curr_zone_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Andre Noll6b8796c2009-01-09 08:31:07 +1100230 printk(KERN_INFO "raid0: current zone start: %llu\n",
NeilBrownd27a43ab2009-06-16 16:46:46 +1000231 (unsigned long long)smallest->sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 mddev->queue->unplug_fn = raid0_unplug;
NeilBrown26be34d2006-10-03 01:15:53 -0700234 mddev->queue->backing_dev_info.congested_fn = raid0_congested;
235 mddev->queue->backing_dev_info.congested_data = mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
raz ben yehuda92e59b62009-06-16 17:00:57 +1000237 /*
238 * now since we have the hard sector sizes, we can make sure
239 * chunk size is a multiple of that sector size
240 */
241 if (mddev->chunk_size % queue_logical_block_size(mddev->queue)) {
242 printk(KERN_ERR "%s chunk_size of %d not valid\n",
243 mdname(mddev),
244 mddev->chunk_size);
245 goto abort;
246 }
Andre Noll0825b87a2009-01-09 08:31:07 +1100247 printk(KERN_INFO "raid0: done.\n");
Andre Nolled7b0032009-06-16 16:47:36 +1000248 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return 0;
Andre Noll5568a602009-06-16 16:47:21 +1000250abort:
Andre Nolled7b0032009-06-16 16:47:36 +1000251 kfree(conf->strip_zone);
252 kfree(conf->devlist);
253 kfree(conf);
254 mddev->private = NULL;
255 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
258/**
259 * raid0_mergeable_bvec -- tell bio layer if a two requests can be merged
260 * @q: request queue
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200261 * @bvm: properties of new bio
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 * @biovec: the request that could be merged to it.
263 *
264 * Return amount of bytes we can accept at this offset
265 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200266static int raid0_mergeable_bvec(struct request_queue *q,
267 struct bvec_merge_data *bvm,
268 struct bio_vec *biovec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
270 mddev_t *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200271 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 int max;
273 unsigned int chunk_sectors = mddev->chunk_size >> 9;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200274 unsigned int bio_sectors = bvm->bi_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
277 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
278 if (max <= biovec->bv_len && bio_sectors == 0)
279 return biovec->bv_len;
280 else
281 return max;
282}
283
Dan Williams80c3a6c2009-03-17 18:10:40 -0700284static sector_t raid0_size(mddev_t *mddev, sector_t sectors, int raid_disks)
285{
286 sector_t array_sectors = 0;
287 mdk_rdev_t *rdev;
288
289 WARN_ONCE(sectors || raid_disks,
290 "%s does not support generic reshape\n", __func__);
291
292 list_for_each_entry(rdev, &mddev->disks, same_set)
293 array_sectors += rdev->sectors;
294
295 return array_sectors;
296}
297
Andre Noll8f79cfc2009-06-16 16:47:10 +1000298static int raid0_run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Andre Noll5568a602009-06-16 16:47:21 +1000300 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
raz ben yehuda92e59b62009-06-16 17:00:57 +1000302 if (mddev->chunk_size == 0 ||
303 !is_power_of_2(mddev->chunk_size)) {
304 printk(KERN_ERR "md/raid0: chunk size must be a power of 2.\n");
NeilBrown2604b702006-01-06 00:20:36 -0800305 return -EINVAL;
306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 blk_queue_max_sectors(mddev->queue, mddev->chunk_size >> 9);
Neil Browne7e72bf2008-05-14 16:05:54 -0700308 mddev->queue->queue_lock = &mddev->queue->__queue_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Andre Noll5568a602009-06-16 16:47:21 +1000310 ret = create_strip_zones(mddev);
311 if (ret < 0)
Andre Nolled7b0032009-06-16 16:47:36 +1000312 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 /* calculate array device size */
Dan Williams1f403622009-03-31 14:59:03 +1100315 md_set_array_sectors(mddev, raid0_size(mddev, 0, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Andre Nollccacc7d2009-01-09 08:31:08 +1100317 printk(KERN_INFO "raid0 : md_size is %llu sectors.\n",
318 (unsigned long long)mddev->array_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /* calculate the max read-ahead size.
320 * For read-ahead of large files to be effective, we need to
321 * readahead at least twice a whole stripe. i.e. number of devices
322 * multiplied by chunk size times 2.
323 * If an individual device has an ra_pages greater than the
324 * chunk size, then we will not drive that device as hard as it
325 * wants. We consider this a configuration error: a larger
326 * chunksize should be used in that case.
327 */
328 {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800329 int stripe = mddev->raid_disks * mddev->chunk_size / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
331 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
332 }
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 blk_queue_merge_bvec(mddev->queue, raid0_mergeable_bvec);
raz ben yehuda46994192009-06-16 17:00:54 +1000335 dump_zones(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
Andre Nollfb5ab4b2009-06-16 16:48:19 +1000339static int raid0_stop(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
NeilBrown070ec552009-06-16 16:54:21 +1000341 raid0_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700344 kfree(conf->strip_zone);
Andre Nollfb5ab4b2009-06-16 16:48:19 +1000345 kfree(conf->devlist);
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700346 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 mddev->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return 0;
349}
350
NeilBrown49f357a22009-06-16 16:50:35 +1000351/* Find the zone which holds a particular offset
352 * Update *sectorp to be an offset in that zone
353 */
Andre Nolldc582662009-06-16 16:18:43 +1000354static struct strip_zone *find_zone(struct raid0_private_data *conf,
NeilBrown49f357a22009-06-16 16:50:35 +1000355 sector_t *sectorp)
Andre Nolldc582662009-06-16 16:18:43 +1000356{
357 int i;
358 struct strip_zone *z = conf->strip_zone;
NeilBrown49f357a22009-06-16 16:50:35 +1000359 sector_t sector = *sectorp;
Andre Nolldc582662009-06-16 16:18:43 +1000360
361 for (i = 0; i < conf->nr_strip_zones; i++)
NeilBrown49f357a22009-06-16 16:50:35 +1000362 if (sector < z[i].zone_end) {
363 if (i)
364 *sectorp = sector - z[i-1].zone_end;
Andre Nolldc582662009-06-16 16:18:43 +1000365 return z + i;
NeilBrown49f357a22009-06-16 16:50:35 +1000366 }
Andre Nolldc582662009-06-16 16:18:43 +1000367 BUG();
368}
369
Jens Axboe165125e2007-07-24 09:28:11 +0200370static int raid0_make_request (struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 mddev_t *mddev = q->queuedata;
Andre Nolla4712002009-01-09 08:31:06 +1100373 unsigned int sect_in_chunk, chunksect_bits, chunk_sects;
NeilBrown070ec552009-06-16 16:54:21 +1000374 raid0_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 struct strip_zone *zone;
376 mdk_rdev_t *tmp_dev;
NeilBrown787f17f2007-05-23 13:58:09 -0700377 sector_t chunk;
NeilBrown49f357a22009-06-16 16:50:35 +1000378 sector_t sector, rsect, sector_offset;
Jens Axboea3623572005-11-01 09:26:16 +0100379 const int rw = bio_data_dir(bio);
Tejun Heoc9959052008-08-25 19:47:21 +0900380 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
NeilBrowne5dcdd82005-09-09 16:23:41 -0700382 if (unlikely(bio_barrier(bio))) {
NeilBrown6712ecf2007-09-27 12:47:43 +0200383 bio_endio(bio, -EOPNOTSUPP);
NeilBrowne5dcdd82005-09-09 16:23:41 -0700384 return 0;
385 }
386
Tejun Heo074a7ac2008-08-25 19:56:14 +0900387 cpu = part_stat_lock();
388 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
389 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
390 bio_sectors(bio));
391 part_stat_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 chunk_sects = mddev->chunk_size >> 9;
Andre Noll1b7fdf82009-01-09 08:31:06 +1100394 chunksect_bits = ffz(~chunk_sects);
Andre Nolle0f06862009-01-09 08:31:06 +1100395 sector = bio->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 if (unlikely(chunk_sects < (bio->bi_sector & (chunk_sects - 1)) + (bio->bi_size >> 9))) {
398 struct bio_pair *bp;
399 /* Sanity check -- queue functions should prevent this happening */
400 if (bio->bi_vcnt != 1 ||
401 bio->bi_idx != 0)
402 goto bad_map;
403 /* This is a one page bio that upper layers
404 * refuse to split for us, so we need to split it.
405 */
Denis ChengRq6feef532008-10-09 08:57:05 +0200406 bp = bio_split(bio, chunk_sects - (bio->bi_sector & (chunk_sects - 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (raid0_make_request(q, &bp->bio1))
408 generic_make_request(&bp->bio1);
409 if (raid0_make_request(q, &bp->bio2))
410 generic_make_request(&bp->bio2);
411
412 bio_pair_release(bp);
413 return 0;
414 }
NeilBrown49f357a22009-06-16 16:50:35 +1000415 sector_offset = sector;
416 zone = find_zone(conf, &sector_offset);
Andre Nolla4712002009-01-09 08:31:06 +1100417 sect_in_chunk = bio->bi_sector & (chunk_sects - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 {
NeilBrown49f357a22009-06-16 16:50:35 +1000419 sector_t x = sector_offset >> chunksect_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 sector_div(x, zone->nb_dev);
422 chunk = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Andre Nolle0f06862009-01-09 08:31:06 +1100424 x = sector >> chunksect_bits;
NeilBrownb4145792009-06-16 16:50:52 +1000425 tmp_dev = conf->devlist[(zone - conf->strip_zone)*mddev->raid_disks
426 + sector_div(x, zone->nb_dev)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
Andre Noll019c4e22009-01-09 08:31:06 +1100428 rsect = (chunk << chunksect_bits) + zone->dev_start + sect_in_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 bio->bi_bdev = tmp_dev->bdev;
431 bio->bi_sector = rsect + tmp_dev->data_offset;
432
433 /*
434 * Let the main block layer submit the IO and resolve recursion:
435 */
436 return 1;
437
438bad_map:
439 printk("raid0_make_request bug: can't convert block across chunks"
Andre Nolla4712002009-01-09 08:31:06 +1100440 " or bigger than %dk %llu %d\n", chunk_sects / 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
442
NeilBrown6712ecf2007-09-27 12:47:43 +0200443 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return 0;
445}
NeilBrown8299d7f2007-10-16 23:30:53 -0700446
raz ben yehuda1b961422009-06-16 16:57:40 +1000447static void raid0_status(struct seq_file *seq, mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449#undef MD_DEBUG
450#ifdef MD_DEBUG
451 int j, k, h;
452 char b[BDEVNAME_SIZE];
NeilBrown070ec552009-06-16 16:54:21 +1000453 raid0_conf_t *conf = mddev->private;
NeilBrown8299d7f2007-10-16 23:30:53 -0700454
raz ben yehuda1b961422009-06-16 16:57:40 +1000455 sector_t zone_size;
456 sector_t zone_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 h = 0;
raz ben yehuda1b961422009-06-16 16:57:40 +1000458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 for (j = 0; j < conf->nr_strip_zones; j++) {
460 seq_printf(seq, " z%d", j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 seq_printf(seq, "=[");
462 for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
NeilBrown8299d7f2007-10-16 23:30:53 -0700463 seq_printf(seq, "%s/", bdevname(
raz ben yehuda1b961422009-06-16 16:57:40 +1000464 conf->devlist[j*mddev->raid_disks + k]
465 ->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
raz ben yehuda1b961422009-06-16 16:57:40 +1000467 zone_size = conf->strip_zone[j].zone_end - zone_start;
468 seq_printf(seq, "] ze=%lld ds=%lld s=%lld\n",
469 (unsigned long long)zone_start>>1,
470 (unsigned long long)conf->strip_zone[j].dev_start>>1,
471 (unsigned long long)zone_size>>1);
472 zone_start = conf->strip_zone[j].zone_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474#endif
475 seq_printf(seq, " %dk chunks", mddev->chunk_size/1024);
476 return;
477}
478
NeilBrown2604b702006-01-06 00:20:36 -0800479static struct mdk_personality raid0_personality=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 .name = "raid0",
NeilBrown2604b702006-01-06 00:20:36 -0800482 .level = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 .owner = THIS_MODULE,
484 .make_request = raid0_make_request,
485 .run = raid0_run,
486 .stop = raid0_stop,
487 .status = raid0_status,
Dan Williams80c3a6c2009-03-17 18:10:40 -0700488 .size = raid0_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489};
490
491static int __init raid0_init (void)
492{
NeilBrown2604b702006-01-06 00:20:36 -0800493 return register_md_personality (&raid0_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
496static void raid0_exit (void)
497{
NeilBrown2604b702006-01-06 00:20:36 -0800498 unregister_md_personality (&raid0_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
501module_init(raid0_init);
502module_exit(raid0_exit);
503MODULE_LICENSE("GPL");
504MODULE_ALIAS("md-personality-2"); /* RAID0 */
NeilBrownd9d166c2006-01-06 00:20:51 -0800505MODULE_ALIAS("md-raid0");
NeilBrown2604b702006-01-06 00:20:36 -0800506MODULE_ALIAS("md-level-0");