blob: c2e0d1d2810234a8b38221869742b9ce881267d5 [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;
NeilBrown84707f32010-03-16 17:23:35 +110031 int raid_disks = conf->strip_zone[0].nb_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 int i;
33
NeilBrown84707f32010-03-16 17:23:35 +110034 for (i=0; i < raid_disks; i++) {
Jens Axboe165125e2007-07-24 09:28:11 +020035 struct request_queue *r_queue = bdev_get_queue(devlist[i]->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -050037 blk_unplug(r_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 }
39}
40
NeilBrown26be34d2006-10-03 01:15:53 -070041static int raid0_congested(void *data, int bits)
42{
43 mddev_t *mddev = data;
NeilBrown070ec552009-06-16 16:54:21 +100044 raid0_conf_t *conf = mddev->private;
NeilBrownb4145792009-06-16 16:50:52 +100045 mdk_rdev_t **devlist = conf->devlist;
NeilBrown84707f32010-03-16 17:23:35 +110046 int raid_disks = conf->strip_zone[0].nb_dev;
NeilBrown26be34d2006-10-03 01:15:53 -070047 int i, ret = 0;
48
NeilBrown3fa841d2009-09-23 18:10:29 +100049 if (mddev_congested(mddev, bits))
50 return 1;
51
NeilBrown84707f32010-03-16 17:23:35 +110052 for (i = 0; i < raid_disks && !ret ; i++) {
Jens Axboe165125e2007-07-24 09:28:11 +020053 struct request_queue *q = bdev_get_queue(devlist[i]->bdev);
NeilBrown26be34d2006-10-03 01:15:53 -070054
55 ret |= bdi_congested(&q->backing_dev_info, bits);
56 }
57 return ret;
58}
59
raz ben yehuda46994192009-06-16 17:00:54 +100060/*
61 * inform the user of the raid configuration
62*/
63static void dump_zones(mddev_t *mddev)
64{
65 int j, k, h;
66 sector_t zone_size = 0;
67 sector_t zone_start = 0;
68 char b[BDEVNAME_SIZE];
69 raid0_conf_t *conf = mddev->private;
NeilBrown84707f32010-03-16 17:23:35 +110070 int raid_disks = conf->strip_zone[0].nb_dev;
raz ben yehuda46994192009-06-16 17:00:54 +100071 printk(KERN_INFO "******* %s configuration *********\n",
72 mdname(mddev));
73 h = 0;
74 for (j = 0; j < conf->nr_strip_zones; j++) {
75 printk(KERN_INFO "zone%d=[", j);
76 for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
77 printk("%s/",
NeilBrown84707f32010-03-16 17:23:35 +110078 bdevname(conf->devlist[j*raid_disks
raz ben yehuda46994192009-06-16 17:00:54 +100079 + k]->bdev, b));
80 printk("]\n");
81
82 zone_size = conf->strip_zone[j].zone_end - zone_start;
83 printk(KERN_INFO " zone offset=%llukb "
84 "device offset=%llukb size=%llukb\n",
85 (unsigned long long)zone_start>>1,
86 (unsigned long long)conf->strip_zone[j].dev_start>>1,
87 (unsigned long long)zone_size>>1);
88 zone_start = conf->strip_zone[j].zone_end;
89 }
90 printk(KERN_INFO "**********************************\n\n");
91}
92
Andre Nolled7b0032009-06-16 16:47:36 +100093static int create_strip_zones(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
NeilBrowna9f326e2009-09-23 18:06:41 +100095 int i, c, err;
NeilBrown49f357a22009-06-16 16:50:35 +100096 sector_t curr_zone_end, sectors;
NeilBrownb4145792009-06-16 16:50:52 +100097 mdk_rdev_t *smallest, *rdev1, *rdev2, *rdev, **dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 struct strip_zone *zone;
99 int cnt;
100 char b[BDEVNAME_SIZE];
Andre Nolled7b0032009-06-16 16:47:36 +1000101 raid0_conf_t *conf = kzalloc(sizeof(*conf), GFP_KERNEL);
102
103 if (!conf)
104 return -ENOMEM;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100105 list_for_each_entry(rdev1, &mddev->disks, same_set) {
Andre Noll0825b872009-01-09 08:31:07 +1100106 printk(KERN_INFO "raid0: looking at %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 bdevname(rdev1->bdev,b));
108 c = 0;
NeilBrown13f26822009-06-18 08:48:55 +1000109
110 /* round size to chunk_size */
111 sectors = rdev1->sectors;
112 sector_div(sectors, mddev->chunk_sectors);
113 rdev1->sectors = sectors * mddev->chunk_sectors;
114
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100115 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Andre Noll0825b872009-01-09 08:31:07 +1100116 printk(KERN_INFO "raid0: comparing %s(%llu)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 bdevname(rdev1->bdev,b),
Andre Nolldd8ac332009-03-31 14:33:13 +1100118 (unsigned long long)rdev1->sectors);
Andre Noll0825b872009-01-09 08:31:07 +1100119 printk(KERN_INFO " with %s(%llu)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 bdevname(rdev2->bdev,b),
Andre Nolldd8ac332009-03-31 14:33:13 +1100121 (unsigned long long)rdev2->sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 if (rdev2 == rdev1) {
Andre Noll0825b872009-01-09 08:31:07 +1100123 printk(KERN_INFO "raid0: END\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 break;
125 }
Andre Nolldd8ac332009-03-31 14:33:13 +1100126 if (rdev2->sectors == rdev1->sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 /*
128 * Not unique, don't count it as a new
129 * group
130 */
Andre Noll0825b872009-01-09 08:31:07 +1100131 printk(KERN_INFO "raid0: EQUAL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 c = 1;
133 break;
134 }
Andre Noll0825b872009-01-09 08:31:07 +1100135 printk(KERN_INFO "raid0: NOT EQUAL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
137 if (!c) {
Andre Noll0825b872009-01-09 08:31:07 +1100138 printk(KERN_INFO "raid0: ==> UNIQUE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 conf->nr_strip_zones++;
Andre Noll0825b872009-01-09 08:31:07 +1100140 printk(KERN_INFO "raid0: %d zones\n",
141 conf->nr_strip_zones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
143 }
Andre Noll0825b872009-01-09 08:31:07 +1100144 printk(KERN_INFO "raid0: FINAL %d zones\n", conf->nr_strip_zones);
Andre Nolled7b0032009-06-16 16:47:36 +1000145 err = -ENOMEM;
NeilBrown9ffae0c2006-01-06 00:20:32 -0800146 conf->strip_zone = kzalloc(sizeof(struct strip_zone)*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 conf->nr_strip_zones, GFP_KERNEL);
148 if (!conf->strip_zone)
Andre Nolled7b0032009-06-16 16:47:36 +1000149 goto abort;
NeilBrown9ffae0c2006-01-06 00:20:32 -0800150 conf->devlist = kzalloc(sizeof(mdk_rdev_t*)*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 conf->nr_strip_zones*mddev->raid_disks,
152 GFP_KERNEL);
153 if (!conf->devlist)
Andre Nolled7b0032009-06-16 16:47:36 +1000154 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 /* The first zone must contain all devices, so here we check that
157 * there is a proper alignment of slots to devices and find them all
158 */
159 zone = &conf->strip_zone[0];
160 cnt = 0;
161 smallest = NULL;
NeilBrownb4145792009-06-16 16:50:52 +1000162 dev = conf->devlist;
Andre Nolled7b0032009-06-16 16:47:36 +1000163 err = -EINVAL;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100164 list_for_each_entry(rdev1, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 int j = rdev1->raid_disk;
166
167 if (j < 0 || j >= mddev->raid_disks) {
Andre Noll0825b872009-01-09 08:31:07 +1100168 printk(KERN_ERR "raid0: bad disk number %d - "
169 "aborting!\n", j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 goto abort;
171 }
NeilBrownb4145792009-06-16 16:50:52 +1000172 if (dev[j]) {
Andre Noll0825b872009-01-09 08:31:07 +1100173 printk(KERN_ERR "raid0: multiple devices for %d - "
174 "aborting!\n", j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 goto abort;
176 }
NeilBrownb4145792009-06-16 16:50:52 +1000177 dev[j] = rdev1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +1000179 disk_stack_limits(mddev->gendisk, rdev1->bdev,
180 rdev1->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 /* as we don't honour merge_bvec_fn, we must never risk
NeilBrown627a2d32010-03-08 16:44:38 +1100182 * violating it, so limit ->max_segments to 1, lying within
183 * a single page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 */
185
NeilBrown627a2d32010-03-08 16:44:38 +1100186 if (rdev1->bdev->bd_disk->queue->merge_bvec_fn) {
187 blk_queue_max_segments(mddev->queue, 1);
188 blk_queue_segment_boundary(mddev->queue,
189 PAGE_CACHE_SIZE - 1);
190 }
Andre Nolldd8ac332009-03-31 14:33:13 +1100191 if (!smallest || (rdev1->sectors < smallest->sectors))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 smallest = rdev1;
193 cnt++;
194 }
195 if (cnt != mddev->raid_disks) {
Andre Noll0825b872009-01-09 08:31:07 +1100196 printk(KERN_ERR "raid0: too few disks (%d of %d) - "
197 "aborting!\n", cnt, mddev->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 goto abort;
199 }
200 zone->nb_dev = cnt;
NeilBrown49f357a22009-06-16 16:50:35 +1000201 zone->zone_end = smallest->sectors * cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
NeilBrown49f357a22009-06-16 16:50:35 +1000203 curr_zone_end = zone->zone_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 /* now do the other zones */
206 for (i = 1; i < conf->nr_strip_zones; i++)
207 {
NeilBrowna9f326e2009-09-23 18:06:41 +1000208 int j;
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 zone = conf->strip_zone + i;
NeilBrownb4145792009-06-16 16:50:52 +1000211 dev = conf->devlist + i * mddev->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Andre Noll0825b872009-01-09 08:31:07 +1100213 printk(KERN_INFO "raid0: zone %d\n", i);
NeilBrownd27a43a2009-06-16 16:46:46 +1000214 zone->dev_start = smallest->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 smallest = NULL;
216 c = 0;
217
218 for (j=0; j<cnt; j++) {
NeilBrownb4145792009-06-16 16:50:52 +1000219 rdev = conf->devlist[j];
Andre Noll0825b872009-01-09 08:31:07 +1100220 printk(KERN_INFO "raid0: checking %s ...",
221 bdevname(rdev->bdev, b));
NeilBrownd27a43a2009-06-16 16:46:46 +1000222 if (rdev->sectors <= zone->dev_start) {
Andre Noll0825b872009-01-09 08:31:07 +1100223 printk(KERN_INFO " nope.\n");
Andre Nolldd8ac332009-03-31 14:33:13 +1100224 continue;
225 }
226 printk(KERN_INFO " contained as device %d\n", c);
NeilBrownb4145792009-06-16 16:50:52 +1000227 dev[c] = rdev;
Andre Nolldd8ac332009-03-31 14:33:13 +1100228 c++;
229 if (!smallest || rdev->sectors < smallest->sectors) {
230 smallest = rdev;
231 printk(KERN_INFO " (%llu) is smallest!.\n",
232 (unsigned long long)rdev->sectors);
233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
235
236 zone->nb_dev = c;
NeilBrown49f357a22009-06-16 16:50:35 +1000237 sectors = (smallest->sectors - zone->dev_start) * c;
Andre Noll83838ed2009-01-09 08:31:07 +1100238 printk(KERN_INFO "raid0: zone->nb_dev: %d, sectors: %llu\n",
NeilBrown49f357a22009-06-16 16:50:35 +1000239 zone->nb_dev, (unsigned long long)sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
NeilBrown49f357a22009-06-16 16:50:35 +1000241 curr_zone_end += sectors;
NeilBrownd27a43a2009-06-16 16:46:46 +1000242 zone->zone_end = curr_zone_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Andre Noll6b8796c2009-01-09 08:31:07 +1100244 printk(KERN_INFO "raid0: current zone start: %llu\n",
NeilBrownd27a43a2009-06-16 16:46:46 +1000245 (unsigned long long)smallest->sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 mddev->queue->unplug_fn = raid0_unplug;
NeilBrown26be34d2006-10-03 01:15:53 -0700248 mddev->queue->backing_dev_info.congested_fn = raid0_congested;
249 mddev->queue->backing_dev_info.congested_data = mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
raz ben yehuda92e59b62009-06-16 17:00:57 +1000251 /*
252 * now since we have the hard sector sizes, we can make sure
253 * chunk size is a multiple of that sector size
254 */
Andre Noll9d8f0362009-06-18 08:45:01 +1000255 if ((mddev->chunk_sectors << 9) % queue_logical_block_size(mddev->queue)) {
raz ben yehuda92e59b62009-06-16 17:00:57 +1000256 printk(KERN_ERR "%s chunk_size of %d not valid\n",
257 mdname(mddev),
Andre Noll9d8f0362009-06-18 08:45:01 +1000258 mddev->chunk_sectors << 9);
raz ben yehuda92e59b62009-06-16 17:00:57 +1000259 goto abort;
260 }
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +1000261
262 blk_queue_io_min(mddev->queue, mddev->chunk_sectors << 9);
263 blk_queue_io_opt(mddev->queue,
264 (mddev->chunk_sectors << 9) * mddev->raid_disks);
265
Andre Noll0825b872009-01-09 08:31:07 +1100266 printk(KERN_INFO "raid0: done.\n");
Andre Nolled7b0032009-06-16 16:47:36 +1000267 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return 0;
Andre Noll5568a602009-06-16 16:47:21 +1000269abort:
Andre Nolled7b0032009-06-16 16:47:36 +1000270 kfree(conf->strip_zone);
271 kfree(conf->devlist);
272 kfree(conf);
273 mddev->private = NULL;
274 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
277/**
278 * raid0_mergeable_bvec -- tell bio layer if a two requests can be merged
279 * @q: request queue
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200280 * @bvm: properties of new bio
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 * @biovec: the request that could be merged to it.
282 *
283 * Return amount of bytes we can accept at this offset
284 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200285static int raid0_mergeable_bvec(struct request_queue *q,
286 struct bvec_merge_data *bvm,
287 struct bio_vec *biovec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 mddev_t *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200290 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +1000292 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200293 unsigned int bio_sectors = bvm->bi_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
NeilBrownd6e412e2009-06-18 08:47:00 +1000295 if (is_power_of_2(chunk_sectors))
raz ben yehudafbb704e2009-06-16 17:02:05 +1000296 max = (chunk_sectors - ((sector & (chunk_sectors-1))
297 + bio_sectors)) << 9;
298 else
299 max = (chunk_sectors - (sector_div(sector, chunk_sectors)
300 + bio_sectors)) << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
302 if (max <= biovec->bv_len && bio_sectors == 0)
303 return biovec->bv_len;
304 else
305 return max;
306}
307
Dan Williams80c3a6c2009-03-17 18:10:40 -0700308static sector_t raid0_size(mddev_t *mddev, sector_t sectors, int raid_disks)
309{
310 sector_t array_sectors = 0;
311 mdk_rdev_t *rdev;
312
313 WARN_ONCE(sectors || raid_disks,
314 "%s does not support generic reshape\n", __func__);
315
316 list_for_each_entry(rdev, &mddev->disks, same_set)
317 array_sectors += rdev->sectors;
318
319 return array_sectors;
320}
321
Andre Noll8f79cfc2009-06-16 16:47:10 +1000322static int raid0_run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Andre Noll5568a602009-06-16 16:47:21 +1000324 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Andre Noll9d8f0362009-06-18 08:45:01 +1000326 if (mddev->chunk_sectors == 0) {
raz ben yehudafbb704e2009-06-16 17:02:05 +1000327 printk(KERN_ERR "md/raid0: chunk size must be set.\n");
NeilBrown2604b702006-01-06 00:20:36 -0800328 return -EINVAL;
329 }
Andre Noll0894cc32009-06-18 08:49:23 +1000330 if (md_check_no_bitmap(mddev))
331 return -EINVAL;
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500332 blk_queue_max_hw_sectors(mddev->queue, mddev->chunk_sectors);
Neil Browne7e72bf2008-05-14 16:05:54 -0700333 mddev->queue->queue_lock = &mddev->queue->__queue_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Andre Noll5568a602009-06-16 16:47:21 +1000335 ret = create_strip_zones(mddev);
336 if (ret < 0)
Andre Nolled7b0032009-06-16 16:47:36 +1000337 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 /* calculate array device size */
Dan Williams1f403622009-03-31 14:59:03 +1100340 md_set_array_sectors(mddev, raid0_size(mddev, 0, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Andre Nollccacc7d2009-01-09 08:31:08 +1100342 printk(KERN_INFO "raid0 : md_size is %llu sectors.\n",
343 (unsigned long long)mddev->array_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 /* calculate the max read-ahead size.
345 * For read-ahead of large files to be effective, we need to
346 * readahead at least twice a whole stripe. i.e. number of devices
347 * multiplied by chunk size times 2.
348 * If an individual device has an ra_pages greater than the
349 * chunk size, then we will not drive that device as hard as it
350 * wants. We consider this a configuration error: a larger
351 * chunksize should be used in that case.
352 */
353 {
Andre Noll9d8f0362009-06-18 08:45:01 +1000354 int stripe = mddev->raid_disks *
355 (mddev->chunk_sectors << 9) / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
357 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
358 }
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 blk_queue_merge_bvec(mddev->queue, raid0_mergeable_bvec);
raz ben yehuda46994192009-06-16 17:00:54 +1000361 dump_zones(mddev);
Andre Nollac5e7112009-08-03 10:59:47 +1000362 md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
Andre Nollfb5ab4b2009-06-16 16:48:19 +1000366static int raid0_stop(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
NeilBrown070ec552009-06-16 16:54:21 +1000368 raid0_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700371 kfree(conf->strip_zone);
Andre Nollfb5ab4b2009-06-16 16:48:19 +1000372 kfree(conf->devlist);
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700373 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 mddev->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return 0;
376}
377
NeilBrown49f357a22009-06-16 16:50:35 +1000378/* Find the zone which holds a particular offset
379 * Update *sectorp to be an offset in that zone
380 */
Andre Nolldc582662009-06-16 16:18:43 +1000381static struct strip_zone *find_zone(struct raid0_private_data *conf,
NeilBrown49f357a22009-06-16 16:50:35 +1000382 sector_t *sectorp)
Andre Nolldc582662009-06-16 16:18:43 +1000383{
384 int i;
385 struct strip_zone *z = conf->strip_zone;
NeilBrown49f357a22009-06-16 16:50:35 +1000386 sector_t sector = *sectorp;
Andre Nolldc582662009-06-16 16:18:43 +1000387
388 for (i = 0; i < conf->nr_strip_zones; i++)
NeilBrown49f357a22009-06-16 16:50:35 +1000389 if (sector < z[i].zone_end) {
390 if (i)
391 *sectorp = sector - z[i-1].zone_end;
Andre Nolldc582662009-06-16 16:18:43 +1000392 return z + i;
NeilBrown49f357a22009-06-16 16:50:35 +1000393 }
Andre Nolldc582662009-06-16 16:18:43 +1000394 BUG();
395}
396
raz ben yehudafbb704e2009-06-16 17:02:05 +1000397/*
398 * remaps the bio to the target device. we separate two flows.
399 * power 2 flow and a general flow for the sake of perfromance
400*/
401static mdk_rdev_t *map_sector(mddev_t *mddev, struct strip_zone *zone,
402 sector_t sector, sector_t *sector_offset)
403{
404 unsigned int sect_in_chunk;
405 sector_t chunk;
406 raid0_conf_t *conf = mddev->private;
NeilBrown84707f32010-03-16 17:23:35 +1100407 int raid_disks = conf->strip_zone[0].nb_dev;
Andre Noll9d8f0362009-06-18 08:45:01 +1000408 unsigned int chunk_sects = mddev->chunk_sectors;
raz ben yehudafbb704e2009-06-16 17:02:05 +1000409
NeilBrownd6e412e2009-06-18 08:47:00 +1000410 if (is_power_of_2(chunk_sects)) {
raz ben yehudafbb704e2009-06-16 17:02:05 +1000411 int chunksect_bits = ffz(~chunk_sects);
412 /* find the sector offset inside the chunk */
413 sect_in_chunk = sector & (chunk_sects - 1);
414 sector >>= chunksect_bits;
415 /* chunk in zone */
416 chunk = *sector_offset;
417 /* quotient is the chunk in real device*/
418 sector_div(chunk, zone->nb_dev << chunksect_bits);
419 } else{
420 sect_in_chunk = sector_div(sector, chunk_sects);
421 chunk = *sector_offset;
422 sector_div(chunk, chunk_sects * zone->nb_dev);
423 }
424 /*
425 * position the bio over the real device
426 * real sector = chunk in device + starting of zone
427 * + the position in the chunk
428 */
429 *sector_offset = (chunk * chunk_sects) + sect_in_chunk;
NeilBrown84707f32010-03-16 17:23:35 +1100430 return conf->devlist[(zone - conf->strip_zone)*raid_disks
raz ben yehudafbb704e2009-06-16 17:02:05 +1000431 + sector_div(sector, zone->nb_dev)];
432}
433
434/*
435 * Is io distribute over 1 or more chunks ?
436*/
437static inline int is_io_in_chunk_boundary(mddev_t *mddev,
438 unsigned int chunk_sects, struct bio *bio)
439{
NeilBrownd6e412e2009-06-18 08:47:00 +1000440 if (likely(is_power_of_2(chunk_sects))) {
raz ben yehudafbb704e2009-06-16 17:02:05 +1000441 return chunk_sects >= ((bio->bi_sector & (chunk_sects-1))
442 + (bio->bi_size >> 9));
443 } else{
444 sector_t sector = bio->bi_sector;
445 return chunk_sects >= (sector_div(sector, chunk_sects)
446 + (bio->bi_size >> 9));
447 }
448}
449
450static int raid0_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 mddev_t *mddev = q->queuedata;
raz ben yehudafbb704e2009-06-16 17:02:05 +1000453 unsigned int chunk_sects;
454 sector_t sector_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 struct strip_zone *zone;
456 mdk_rdev_t *tmp_dev;
Jens Axboea3623572005-11-01 09:26:16 +0100457 const int rw = bio_data_dir(bio);
Tejun Heoc9959052008-08-25 19:47:21 +0900458 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Jens Axboe1f98a132009-09-11 14:32:04 +0200460 if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) {
NeilBrowna2826aa2009-12-14 12:49:49 +1100461 md_barrier_request(mddev, bio);
NeilBrowne5dcdd82005-09-09 16:23:41 -0700462 return 0;
463 }
464
Tejun Heo074a7ac2008-08-25 19:56:14 +0900465 cpu = part_stat_lock();
466 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
467 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
468 bio_sectors(bio));
469 part_stat_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Andre Noll9d8f0362009-06-18 08:45:01 +1000471 chunk_sects = mddev->chunk_sectors;
raz ben yehudafbb704e2009-06-16 17:02:05 +1000472 if (unlikely(!is_io_in_chunk_boundary(mddev, chunk_sects, bio))) {
473 sector_t sector = bio->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 struct bio_pair *bp;
475 /* Sanity check -- queue functions should prevent this happening */
476 if (bio->bi_vcnt != 1 ||
477 bio->bi_idx != 0)
478 goto bad_map;
479 /* This is a one page bio that upper layers
480 * refuse to split for us, so we need to split it.
481 */
NeilBrownd6e412e2009-06-18 08:47:00 +1000482 if (likely(is_power_of_2(chunk_sects)))
raz ben yehudafbb704e2009-06-16 17:02:05 +1000483 bp = bio_split(bio, chunk_sects - (sector &
484 (chunk_sects-1)));
485 else
486 bp = bio_split(bio, chunk_sects -
487 sector_div(sector, chunk_sects));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (raid0_make_request(q, &bp->bio1))
489 generic_make_request(&bp->bio1);
490 if (raid0_make_request(q, &bp->bio2))
491 generic_make_request(&bp->bio2);
492
493 bio_pair_release(bp);
494 return 0;
495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
raz ben yehudafbb704e2009-06-16 17:02:05 +1000497 sector_offset = bio->bi_sector;
498 zone = find_zone(mddev->private, &sector_offset);
499 tmp_dev = map_sector(mddev, zone, bio->bi_sector,
500 &sector_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 bio->bi_bdev = tmp_dev->bdev;
raz ben yehudafbb704e2009-06-16 17:02:05 +1000502 bio->bi_sector = sector_offset + zone->dev_start +
503 tmp_dev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 /*
505 * Let the main block layer submit the IO and resolve recursion:
506 */
507 return 1;
508
509bad_map:
510 printk("raid0_make_request bug: can't convert block across chunks"
Andre Nolla4712002009-01-09 08:31:06 +1100511 " or bigger than %dk %llu %d\n", chunk_sects / 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
513
NeilBrown6712ecf2007-09-27 12:47:43 +0200514 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return 0;
516}
NeilBrown8299d7f2007-10-16 23:30:53 -0700517
raz ben yehuda1b961422009-06-16 16:57:40 +1000518static void raid0_status(struct seq_file *seq, mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520#undef MD_DEBUG
521#ifdef MD_DEBUG
522 int j, k, h;
523 char b[BDEVNAME_SIZE];
NeilBrown070ec552009-06-16 16:54:21 +1000524 raid0_conf_t *conf = mddev->private;
NeilBrown84707f32010-03-16 17:23:35 +1100525 int raid_disks = conf->strip_zone[0].nb_dev;
NeilBrown8299d7f2007-10-16 23:30:53 -0700526
raz ben yehuda1b961422009-06-16 16:57:40 +1000527 sector_t zone_size;
528 sector_t zone_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 h = 0;
raz ben yehuda1b961422009-06-16 16:57:40 +1000530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 for (j = 0; j < conf->nr_strip_zones; j++) {
532 seq_printf(seq, " z%d", j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 seq_printf(seq, "=[");
534 for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
NeilBrown8299d7f2007-10-16 23:30:53 -0700535 seq_printf(seq, "%s/", bdevname(
NeilBrown84707f32010-03-16 17:23:35 +1100536 conf->devlist[j*raid_disks + k]
raz ben yehuda1b961422009-06-16 16:57:40 +1000537 ->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
raz ben yehuda1b961422009-06-16 16:57:40 +1000539 zone_size = conf->strip_zone[j].zone_end - zone_start;
540 seq_printf(seq, "] ze=%lld ds=%lld s=%lld\n",
541 (unsigned long long)zone_start>>1,
542 (unsigned long long)conf->strip_zone[j].dev_start>>1,
543 (unsigned long long)zone_size>>1);
544 zone_start = conf->strip_zone[j].zone_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
546#endif
Andre Noll9d8f0362009-06-18 08:45:01 +1000547 seq_printf(seq, " %dk chunks", mddev->chunk_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return;
549}
550
NeilBrown2604b702006-01-06 00:20:36 -0800551static struct mdk_personality raid0_personality=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 .name = "raid0",
NeilBrown2604b702006-01-06 00:20:36 -0800554 .level = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 .owner = THIS_MODULE,
556 .make_request = raid0_make_request,
557 .run = raid0_run,
558 .stop = raid0_stop,
559 .status = raid0_status,
Dan Williams80c3a6c2009-03-17 18:10:40 -0700560 .size = raid0_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561};
562
563static int __init raid0_init (void)
564{
NeilBrown2604b702006-01-06 00:20:36 -0800565 return register_md_personality (&raid0_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
568static void raid0_exit (void)
569{
NeilBrown2604b702006-01-06 00:20:36 -0800570 unregister_md_personality (&raid0_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
573module_init(raid0_init);
574module_exit(raid0_exit);
575MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +1100576MODULE_DESCRIPTION("RAID0 (striping) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577MODULE_ALIAS("md-personality-2"); /* RAID0 */
NeilBrownd9d166c2006-01-06 00:20:51 -0800578MODULE_ALIAS("md-raid0");
NeilBrown2604b702006-01-06 00:20:36 -0800579MODULE_ALIAS("md-level-0");