Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 21 | #include <linux/blkdev.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 22 | #include <linux/seq_file.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 23 | #include "md.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 24 | #include "raid0.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 26 | static void raid0_unplug(struct request_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | { |
| 28 | mddev_t *mddev = q->queuedata; |
| 29 | raid0_conf_t *conf = mddev_to_conf(mddev); |
| 30 | mdk_rdev_t **devlist = conf->strip_zone[0].dev; |
| 31 | int i; |
| 32 | |
| 33 | for (i=0; i<mddev->raid_disks; i++) { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 34 | struct request_queue *r_queue = bdev_get_queue(devlist[i]->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 36 | blk_unplug(r_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | |
NeilBrown | 26be34d | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 40 | static int raid0_congested(void *data, int bits) |
| 41 | { |
| 42 | mddev_t *mddev = data; |
| 43 | raid0_conf_t *conf = mddev_to_conf(mddev); |
| 44 | mdk_rdev_t **devlist = conf->strip_zone[0].dev; |
| 45 | int i, ret = 0; |
| 46 | |
| 47 | for (i = 0; i < mddev->raid_disks && !ret ; i++) { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 48 | struct request_queue *q = bdev_get_queue(devlist[i]->bdev); |
NeilBrown | 26be34d | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 49 | |
| 50 | ret |= bdi_congested(&q->backing_dev_info, bits); |
| 51 | } |
| 52 | return ret; |
| 53 | } |
| 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
| 56 | static int create_strip_zones (mddev_t *mddev) |
| 57 | { |
| 58 | int i, c, j; |
Andre Noll | 6b8796c | 2009-01-09 08:31:07 +1100 | [diff] [blame] | 59 | sector_t current_start, curr_zone_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | sector_t min_spacing; |
| 61 | raid0_conf_t *conf = mddev_to_conf(mddev); |
| 62 | mdk_rdev_t *smallest, *rdev1, *rdev2, *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | struct strip_zone *zone; |
| 64 | int cnt; |
| 65 | char b[BDEVNAME_SIZE]; |
| 66 | |
| 67 | /* |
| 68 | * The number of 'same size groups' |
| 69 | */ |
| 70 | conf->nr_strip_zones = 0; |
| 71 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 72 | list_for_each_entry(rdev1, &mddev->disks, same_set) { |
Andre Noll | 0825b87a | 2009-01-09 08:31:07 +1100 | [diff] [blame] | 73 | printk(KERN_INFO "raid0: looking at %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | bdevname(rdev1->bdev,b)); |
| 75 | c = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 76 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Andre Noll | 0825b87a | 2009-01-09 08:31:07 +1100 | [diff] [blame] | 77 | printk(KERN_INFO "raid0: comparing %s(%llu)", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | bdevname(rdev1->bdev,b), |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 79 | (unsigned long long)rdev1->sectors); |
Andre Noll | 0825b87a | 2009-01-09 08:31:07 +1100 | [diff] [blame] | 80 | printk(KERN_INFO " with %s(%llu)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | bdevname(rdev2->bdev,b), |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 82 | (unsigned long long)rdev2->sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | if (rdev2 == rdev1) { |
Andre Noll | 0825b87a | 2009-01-09 08:31:07 +1100 | [diff] [blame] | 84 | printk(KERN_INFO "raid0: END\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | break; |
| 86 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 87 | if (rdev2->sectors == rdev1->sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | /* |
| 89 | * Not unique, don't count it as a new |
| 90 | * group |
| 91 | */ |
Andre Noll | 0825b87a | 2009-01-09 08:31:07 +1100 | [diff] [blame] | 92 | printk(KERN_INFO "raid0: EQUAL\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | c = 1; |
| 94 | break; |
| 95 | } |
Andre Noll | 0825b87a | 2009-01-09 08:31:07 +1100 | [diff] [blame] | 96 | printk(KERN_INFO "raid0: NOT EQUAL\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } |
| 98 | if (!c) { |
Andre Noll | 0825b87a | 2009-01-09 08:31:07 +1100 | [diff] [blame] | 99 | printk(KERN_INFO "raid0: ==> UNIQUE\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | conf->nr_strip_zones++; |
Andre Noll | 0825b87a | 2009-01-09 08:31:07 +1100 | [diff] [blame] | 101 | printk(KERN_INFO "raid0: %d zones\n", |
| 102 | conf->nr_strip_zones); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
Andre Noll | 0825b87a | 2009-01-09 08:31:07 +1100 | [diff] [blame] | 105 | printk(KERN_INFO "raid0: FINAL %d zones\n", conf->nr_strip_zones); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 107 | conf->strip_zone = kzalloc(sizeof(struct strip_zone)* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | conf->nr_strip_zones, GFP_KERNEL); |
| 109 | if (!conf->strip_zone) |
| 110 | return 1; |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 111 | conf->devlist = kzalloc(sizeof(mdk_rdev_t*)* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | conf->nr_strip_zones*mddev->raid_disks, |
| 113 | GFP_KERNEL); |
| 114 | if (!conf->devlist) |
| 115 | return 1; |
| 116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | /* The first zone must contain all devices, so here we check that |
| 118 | * there is a proper alignment of slots to devices and find them all |
| 119 | */ |
| 120 | zone = &conf->strip_zone[0]; |
| 121 | cnt = 0; |
| 122 | smallest = NULL; |
| 123 | zone->dev = conf->devlist; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 124 | list_for_each_entry(rdev1, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | int j = rdev1->raid_disk; |
| 126 | |
| 127 | if (j < 0 || j >= mddev->raid_disks) { |
Andre Noll | 0825b87a | 2009-01-09 08:31:07 +1100 | [diff] [blame] | 128 | printk(KERN_ERR "raid0: bad disk number %d - " |
| 129 | "aborting!\n", j); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | goto abort; |
| 131 | } |
| 132 | if (zone->dev[j]) { |
Andre Noll | 0825b87a | 2009-01-09 08:31:07 +1100 | [diff] [blame] | 133 | printk(KERN_ERR "raid0: multiple devices for %d - " |
| 134 | "aborting!\n", j); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | goto abort; |
| 136 | } |
| 137 | zone->dev[j] = rdev1; |
| 138 | |
| 139 | blk_queue_stack_limits(mddev->queue, |
| 140 | rdev1->bdev->bd_disk->queue); |
| 141 | /* as we don't honour merge_bvec_fn, we must never risk |
| 142 | * violating it, so limit ->max_sector to one PAGE, as |
| 143 | * a one page request is never in violation. |
| 144 | */ |
| 145 | |
| 146 | if (rdev1->bdev->bd_disk->queue->merge_bvec_fn && |
| 147 | mddev->queue->max_sectors > (PAGE_SIZE>>9)) |
| 148 | blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9); |
| 149 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 150 | if (!smallest || (rdev1->sectors < smallest->sectors)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | smallest = rdev1; |
| 152 | cnt++; |
| 153 | } |
| 154 | if (cnt != mddev->raid_disks) { |
Andre Noll | 0825b87a | 2009-01-09 08:31:07 +1100 | [diff] [blame] | 155 | printk(KERN_ERR "raid0: too few disks (%d of %d) - " |
| 156 | "aborting!\n", cnt, mddev->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | goto abort; |
| 158 | } |
| 159 | zone->nb_dev = cnt; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 160 | zone->sectors = smallest->sectors * cnt; |
Andre Noll | 6199d3d | 2009-01-09 08:31:07 +1100 | [diff] [blame] | 161 | zone->zone_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 163 | current_start = smallest->sectors; |
Andre Noll | 83838ed | 2009-01-09 08:31:07 +1100 | [diff] [blame] | 164 | curr_zone_start = zone->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
| 166 | /* now do the other zones */ |
| 167 | for (i = 1; i < conf->nr_strip_zones; i++) |
| 168 | { |
| 169 | zone = conf->strip_zone + i; |
| 170 | zone->dev = conf->strip_zone[i-1].dev + mddev->raid_disks; |
| 171 | |
Andre Noll | 0825b87a | 2009-01-09 08:31:07 +1100 | [diff] [blame] | 172 | printk(KERN_INFO "raid0: zone %d\n", i); |
Andre Noll | 6b8796c | 2009-01-09 08:31:07 +1100 | [diff] [blame] | 173 | zone->dev_start = current_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | smallest = NULL; |
| 175 | c = 0; |
| 176 | |
| 177 | for (j=0; j<cnt; j++) { |
| 178 | char b[BDEVNAME_SIZE]; |
| 179 | rdev = conf->strip_zone[0].dev[j]; |
Andre Noll | 0825b87a | 2009-01-09 08:31:07 +1100 | [diff] [blame] | 180 | printk(KERN_INFO "raid0: checking %s ...", |
| 181 | bdevname(rdev->bdev, b)); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 182 | if (rdev->sectors <= current_start) { |
Andre Noll | 0825b87a | 2009-01-09 08:31:07 +1100 | [diff] [blame] | 183 | printk(KERN_INFO " nope.\n"); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 184 | continue; |
| 185 | } |
| 186 | printk(KERN_INFO " contained as device %d\n", c); |
| 187 | zone->dev[c] = rdev; |
| 188 | c++; |
| 189 | if (!smallest || rdev->sectors < smallest->sectors) { |
| 190 | smallest = rdev; |
| 191 | printk(KERN_INFO " (%llu) is smallest!.\n", |
| 192 | (unsigned long long)rdev->sectors); |
| 193 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | zone->nb_dev = c; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 197 | zone->sectors = (smallest->sectors - current_start) * c; |
Andre Noll | 83838ed | 2009-01-09 08:31:07 +1100 | [diff] [blame] | 198 | printk(KERN_INFO "raid0: zone->nb_dev: %d, sectors: %llu\n", |
| 199 | zone->nb_dev, (unsigned long long)zone->sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
Andre Noll | 6b8796c | 2009-01-09 08:31:07 +1100 | [diff] [blame] | 201 | zone->zone_start = curr_zone_start; |
Andre Noll | 83838ed | 2009-01-09 08:31:07 +1100 | [diff] [blame] | 202 | curr_zone_start += zone->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 204 | current_start = smallest->sectors; |
Andre Noll | 6b8796c | 2009-01-09 08:31:07 +1100 | [diff] [blame] | 205 | printk(KERN_INFO "raid0: current zone start: %llu\n", |
| 206 | (unsigned long long)current_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /* Now find appropriate hash spacing. |
| 210 | * We want a number which causes most hash entries to cover |
| 211 | * at most two strips, but the hash table must be at most |
| 212 | * 1 PAGE. We choose the smallest strip, or contiguous collection |
| 213 | * of strips, that has big enough size. We never consider the last |
| 214 | * strip though as it's size has no bearing on the efficacy of the hash |
| 215 | * table. |
| 216 | */ |
Andre Noll | ccacc7d | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 217 | conf->spacing = curr_zone_start; |
| 218 | min_spacing = curr_zone_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | sector_div(min_spacing, PAGE_SIZE/sizeof(struct strip_zone*)); |
| 220 | for (i=0; i < conf->nr_strip_zones-1; i++) { |
Andre Noll | ccacc7d | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 221 | sector_t s = 0; |
| 222 | for (j = i; j < conf->nr_strip_zones - 1 && |
| 223 | s < min_spacing; j++) |
| 224 | s += conf->strip_zone[j].sectors; |
| 225 | if (s >= min_spacing && s < conf->spacing) |
| 226 | conf->spacing = s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | mddev->queue->unplug_fn = raid0_unplug; |
| 230 | |
NeilBrown | 26be34d | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 231 | mddev->queue->backing_dev_info.congested_fn = raid0_congested; |
| 232 | mddev->queue->backing_dev_info.congested_data = mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
Andre Noll | 0825b87a | 2009-01-09 08:31:07 +1100 | [diff] [blame] | 234 | printk(KERN_INFO "raid0: done.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | return 0; |
| 236 | abort: |
| 237 | return 1; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * raid0_mergeable_bvec -- tell bio layer if a two requests can be merged |
| 242 | * @q: request queue |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 243 | * @bvm: properties of new bio |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | * @biovec: the request that could be merged to it. |
| 245 | * |
| 246 | * Return amount of bytes we can accept at this offset |
| 247 | */ |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 248 | static int raid0_mergeable_bvec(struct request_queue *q, |
| 249 | struct bvec_merge_data *bvm, |
| 250 | struct bio_vec *biovec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | { |
| 252 | mddev_t *mddev = q->queuedata; |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 253 | sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | int max; |
| 255 | unsigned int chunk_sectors = mddev->chunk_size >> 9; |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 256 | unsigned int bio_sectors = bvm->bi_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
| 258 | max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9; |
| 259 | if (max < 0) max = 0; /* bio_add cannot handle a negative return */ |
| 260 | if (max <= biovec->bv_len && bio_sectors == 0) |
| 261 | return biovec->bv_len; |
| 262 | else |
| 263 | return max; |
| 264 | } |
| 265 | |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 266 | static sector_t raid0_size(mddev_t *mddev, sector_t sectors, int raid_disks) |
| 267 | { |
| 268 | sector_t array_sectors = 0; |
| 269 | mdk_rdev_t *rdev; |
| 270 | |
| 271 | WARN_ONCE(sectors || raid_disks, |
| 272 | "%s does not support generic reshape\n", __func__); |
| 273 | |
| 274 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 275 | array_sectors += rdev->sectors; |
| 276 | |
| 277 | return array_sectors; |
| 278 | } |
| 279 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | static int raid0_run (mddev_t *mddev) |
| 281 | { |
| 282 | unsigned cur=0, i=0, nb_zone; |
Andre Noll | ccacc7d | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 283 | s64 sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | raid0_conf_t *conf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 286 | if (mddev->chunk_size == 0) { |
| 287 | printk(KERN_ERR "md/raid0: non-zero chunk size required.\n"); |
| 288 | return -EINVAL; |
| 289 | } |
| 290 | printk(KERN_INFO "%s: setting max_sectors to %d, segment boundary to %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | mdname(mddev), |
| 292 | mddev->chunk_size >> 9, |
| 293 | (mddev->chunk_size>>1)-1); |
| 294 | blk_queue_max_sectors(mddev->queue, mddev->chunk_size >> 9); |
| 295 | blk_queue_segment_boundary(mddev->queue, (mddev->chunk_size>>1) - 1); |
Neil Brown | e7e72bf | 2008-05-14 16:05:54 -0700 | [diff] [blame] | 296 | mddev->queue->queue_lock = &mddev->queue->__queue_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
| 298 | conf = kmalloc(sizeof (raid0_conf_t), GFP_KERNEL); |
| 299 | if (!conf) |
| 300 | goto out; |
| 301 | mddev->private = (void *)conf; |
| 302 | |
| 303 | conf->strip_zone = NULL; |
| 304 | conf->devlist = NULL; |
| 305 | if (create_strip_zones (mddev)) |
| 306 | goto out_free_conf; |
| 307 | |
| 308 | /* calculate array device size */ |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 309 | md_set_array_sectors(mddev, raid0_size(mddev, 0, 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | |
Andre Noll | ccacc7d | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 311 | printk(KERN_INFO "raid0 : md_size is %llu sectors.\n", |
| 312 | (unsigned long long)mddev->array_sectors); |
| 313 | printk(KERN_INFO "raid0 : conf->spacing is %llu sectors.\n", |
| 314 | (unsigned long long)conf->spacing); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame^] | 316 | sector_t s = raid0_size(mddev, 0, 0); |
Andre Noll | ccacc7d | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 317 | sector_t space = conf->spacing; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | int round; |
Andre Noll | ccacc7d | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 319 | conf->sector_shift = 0; |
Neil Brown | 1eb2912 | 2005-07-15 03:56:27 -0700 | [diff] [blame] | 320 | if (sizeof(sector_t) > sizeof(u32)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | /*shift down space and s so that sector_div will work */ |
Neil Brown | 1eb2912 | 2005-07-15 03:56:27 -0700 | [diff] [blame] | 322 | while (space > (sector_t) (~(u32)0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | s >>= 1; |
| 324 | space >>= 1; |
| 325 | s += 1; /* force round-up */ |
Andre Noll | ccacc7d | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 326 | conf->sector_shift++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | } |
| 328 | } |
Neil Brown | 1eb2912 | 2005-07-15 03:56:27 -0700 | [diff] [blame] | 329 | round = sector_div(s, (u32)space) ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | nb_zone = s + round; |
| 331 | } |
Andre Noll | ccacc7d | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 332 | printk(KERN_INFO "raid0 : nb_zone is %d.\n", nb_zone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
Andre Noll | ccacc7d | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 334 | printk(KERN_INFO "raid0 : Allocating %zu bytes for hash.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | nb_zone*sizeof(struct strip_zone*)); |
| 336 | conf->hash_table = kmalloc (sizeof (struct strip_zone *)*nb_zone, GFP_KERNEL); |
| 337 | if (!conf->hash_table) |
| 338 | goto out_free_conf; |
Andre Noll | ccacc7d | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 339 | sectors = conf->strip_zone[cur].sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | |
NeilBrown | 5c4c333 | 2006-05-22 22:35:26 -0700 | [diff] [blame] | 341 | conf->hash_table[0] = conf->strip_zone + cur; |
| 342 | for (i=1; i< nb_zone; i++) { |
Andre Noll | ccacc7d | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 343 | while (sectors <= conf->spacing) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | cur++; |
Andre Noll | ccacc7d | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 345 | sectors += conf->strip_zone[cur].sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | } |
Andre Noll | ccacc7d | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 347 | sectors -= conf->spacing; |
NeilBrown | 5c4c333 | 2006-05-22 22:35:26 -0700 | [diff] [blame] | 348 | conf->hash_table[i] = conf->strip_zone + cur; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | } |
Andre Noll | ccacc7d | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 350 | if (conf->sector_shift) { |
| 351 | conf->spacing >>= conf->sector_shift; |
| 352 | /* round spacing up so when we divide by it, we |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | * err on the side of too-low, which is safest |
| 354 | */ |
Andre Noll | ccacc7d | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 355 | conf->spacing++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /* calculate the max read-ahead size. |
| 359 | * For read-ahead of large files to be effective, we need to |
| 360 | * readahead at least twice a whole stripe. i.e. number of devices |
| 361 | * multiplied by chunk size times 2. |
| 362 | * If an individual device has an ra_pages greater than the |
| 363 | * chunk size, then we will not drive that device as hard as it |
| 364 | * wants. We consider this a configuration error: a larger |
| 365 | * chunksize should be used in that case. |
| 366 | */ |
| 367 | { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 368 | int stripe = mddev->raid_disks * mddev->chunk_size / PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | if (mddev->queue->backing_dev_info.ra_pages < 2* stripe) |
| 370 | mddev->queue->backing_dev_info.ra_pages = 2* stripe; |
| 371 | } |
| 372 | |
| 373 | |
| 374 | blk_queue_merge_bvec(mddev->queue, raid0_mergeable_bvec); |
| 375 | return 0; |
| 376 | |
| 377 | out_free_conf: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 378 | kfree(conf->strip_zone); |
| 379 | kfree(conf->devlist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | kfree(conf); |
| 381 | mddev->private = NULL; |
| 382 | out: |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 383 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | static int raid0_stop (mddev_t *mddev) |
| 387 | { |
| 388 | raid0_conf_t *conf = mddev_to_conf(mddev); |
| 389 | |
| 390 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 391 | kfree(conf->hash_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | conf->hash_table = NULL; |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 393 | kfree(conf->strip_zone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | conf->strip_zone = NULL; |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 395 | kfree(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | mddev->private = NULL; |
| 397 | |
| 398 | return 0; |
| 399 | } |
| 400 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 401 | static int raid0_make_request (struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | { |
| 403 | mddev_t *mddev = q->queuedata; |
Andre Noll | a471200 | 2009-01-09 08:31:06 +1100 | [diff] [blame] | 404 | unsigned int sect_in_chunk, chunksect_bits, chunk_sects; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | raid0_conf_t *conf = mddev_to_conf(mddev); |
| 406 | struct strip_zone *zone; |
| 407 | mdk_rdev_t *tmp_dev; |
NeilBrown | 787f17f | 2007-05-23 13:58:09 -0700 | [diff] [blame] | 408 | sector_t chunk; |
Andre Noll | e0f0686 | 2009-01-09 08:31:06 +1100 | [diff] [blame] | 409 | sector_t sector, rsect; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 410 | const int rw = bio_data_dir(bio); |
Tejun Heo | c995905 | 2008-08-25 19:47:21 +0900 | [diff] [blame] | 411 | int cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
NeilBrown | e5dcdd8 | 2005-09-09 16:23:41 -0700 | [diff] [blame] | 413 | if (unlikely(bio_barrier(bio))) { |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 414 | bio_endio(bio, -EOPNOTSUPP); |
NeilBrown | e5dcdd8 | 2005-09-09 16:23:41 -0700 | [diff] [blame] | 415 | return 0; |
| 416 | } |
| 417 | |
Tejun Heo | 074a7ac | 2008-08-25 19:56:14 +0900 | [diff] [blame] | 418 | cpu = part_stat_lock(); |
| 419 | part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]); |
| 420 | part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], |
| 421 | bio_sectors(bio)); |
| 422 | part_stat_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | chunk_sects = mddev->chunk_size >> 9; |
Andre Noll | 1b7fdf8 | 2009-01-09 08:31:06 +1100 | [diff] [blame] | 425 | chunksect_bits = ffz(~chunk_sects); |
Andre Noll | e0f0686 | 2009-01-09 08:31:06 +1100 | [diff] [blame] | 426 | sector = bio->bi_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | |
| 428 | if (unlikely(chunk_sects < (bio->bi_sector & (chunk_sects - 1)) + (bio->bi_size >> 9))) { |
| 429 | struct bio_pair *bp; |
| 430 | /* Sanity check -- queue functions should prevent this happening */ |
| 431 | if (bio->bi_vcnt != 1 || |
| 432 | bio->bi_idx != 0) |
| 433 | goto bad_map; |
| 434 | /* This is a one page bio that upper layers |
| 435 | * refuse to split for us, so we need to split it. |
| 436 | */ |
Denis ChengRq | 6feef53 | 2008-10-09 08:57:05 +0200 | [diff] [blame] | 437 | bp = bio_split(bio, chunk_sects - (bio->bi_sector & (chunk_sects - 1))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | if (raid0_make_request(q, &bp->bio1)) |
| 439 | generic_make_request(&bp->bio1); |
| 440 | if (raid0_make_request(q, &bp->bio2)) |
| 441 | generic_make_request(&bp->bio2); |
| 442 | |
| 443 | bio_pair_release(bp); |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | |
| 448 | { |
Andre Noll | ccacc7d | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 449 | sector_t x = sector >> conf->sector_shift; |
| 450 | sector_div(x, (u32)conf->spacing); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | zone = conf->hash_table[x]; |
| 452 | } |
Andre Noll | 83838ed | 2009-01-09 08:31:07 +1100 | [diff] [blame] | 453 | |
| 454 | while (sector >= zone->zone_start + zone->sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | zone++; |
Andre Noll | 83838ed | 2009-01-09 08:31:07 +1100 | [diff] [blame] | 456 | |
Andre Noll | a471200 | 2009-01-09 08:31:06 +1100 | [diff] [blame] | 457 | sect_in_chunk = bio->bi_sector & (chunk_sects - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | |
| 459 | |
| 460 | { |
Andre Noll | 6199d3d | 2009-01-09 08:31:07 +1100 | [diff] [blame] | 461 | sector_t x = (sector - zone->zone_start) >> chunksect_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | |
| 463 | sector_div(x, zone->nb_dev); |
| 464 | chunk = x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | |
Andre Noll | e0f0686 | 2009-01-09 08:31:06 +1100 | [diff] [blame] | 466 | x = sector >> chunksect_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | tmp_dev = zone->dev[sector_div(x, zone->nb_dev)]; |
| 468 | } |
Andre Noll | 019c4e2 | 2009-01-09 08:31:06 +1100 | [diff] [blame] | 469 | rsect = (chunk << chunksect_bits) + zone->dev_start + sect_in_chunk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
| 471 | bio->bi_bdev = tmp_dev->bdev; |
| 472 | bio->bi_sector = rsect + tmp_dev->data_offset; |
| 473 | |
| 474 | /* |
| 475 | * Let the main block layer submit the IO and resolve recursion: |
| 476 | */ |
| 477 | return 1; |
| 478 | |
| 479 | bad_map: |
| 480 | printk("raid0_make_request bug: can't convert block across chunks" |
Andre Noll | a471200 | 2009-01-09 08:31:06 +1100 | [diff] [blame] | 481 | " or bigger than %dk %llu %d\n", chunk_sects / 2, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | (unsigned long long)bio->bi_sector, bio->bi_size >> 10); |
| 483 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 484 | bio_io_error(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | return 0; |
| 486 | } |
NeilBrown | 8299d7f | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 487 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | static void raid0_status (struct seq_file *seq, mddev_t *mddev) |
| 489 | { |
| 490 | #undef MD_DEBUG |
| 491 | #ifdef MD_DEBUG |
| 492 | int j, k, h; |
| 493 | char b[BDEVNAME_SIZE]; |
| 494 | raid0_conf_t *conf = mddev_to_conf(mddev); |
NeilBrown | 8299d7f | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | h = 0; |
| 497 | for (j = 0; j < conf->nr_strip_zones; j++) { |
| 498 | seq_printf(seq, " z%d", j); |
| 499 | if (conf->hash_table[h] == conf->strip_zone+j) |
NeilBrown | 8299d7f | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 500 | seq_printf(seq, "(h%d)", h++); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | seq_printf(seq, "=["); |
| 502 | for (k = 0; k < conf->strip_zone[j].nb_dev; k++) |
NeilBrown | 8299d7f | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 503 | seq_printf(seq, "%s/", bdevname( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | conf->strip_zone[j].dev[k]->bdev,b)); |
| 505 | |
Andre Noll | 6199d3d | 2009-01-09 08:31:07 +1100 | [diff] [blame] | 506 | seq_printf(seq, "] zs=%d ds=%d s=%d\n", |
| 507 | conf->strip_zone[j].zone_start, |
Andre Noll | 019c4e2 | 2009-01-09 08:31:06 +1100 | [diff] [blame] | 508 | conf->strip_zone[j].dev_start, |
Andre Noll | 83838ed | 2009-01-09 08:31:07 +1100 | [diff] [blame] | 509 | conf->strip_zone[j].sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | } |
| 511 | #endif |
| 512 | seq_printf(seq, " %dk chunks", mddev->chunk_size/1024); |
| 513 | return; |
| 514 | } |
| 515 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 516 | static struct mdk_personality raid0_personality= |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | { |
| 518 | .name = "raid0", |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 519 | .level = 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | .owner = THIS_MODULE, |
| 521 | .make_request = raid0_make_request, |
| 522 | .run = raid0_run, |
| 523 | .stop = raid0_stop, |
| 524 | .status = raid0_status, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 525 | .size = raid0_size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | }; |
| 527 | |
| 528 | static int __init raid0_init (void) |
| 529 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 530 | return register_md_personality (&raid0_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | static void raid0_exit (void) |
| 534 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 535 | unregister_md_personality (&raid0_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | module_init(raid0_init); |
| 539 | module_exit(raid0_exit); |
| 540 | MODULE_LICENSE("GPL"); |
| 541 | MODULE_ALIAS("md-personality-2"); /* RAID0 */ |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 542 | MODULE_ALIAS("md-raid0"); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 543 | MODULE_ALIAS("md-level-0"); |