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