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