Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * raid5.c : Multiple Devices driver for Linux |
| 3 | * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman |
| 4 | * Copyright (C) 1999, 2000 Ingo Molnar |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5 | * Copyright (C) 2002, 2003 H. Peter Anvin |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 7 | * RAID-4/5/6 management functions. |
| 8 | * Thanks to Penguin Computing for making the RAID-6 development possible |
| 9 | * by donating a test server! |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/module.h> |
| 23 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/highmem.h> |
| 25 | #include <linux/bitops.h> |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 26 | #include <linux/kthread.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <asm/atomic.h> |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 28 | #include "raid6.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 30 | #include <linux/raid/bitmap.h> |
| 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | /* |
| 33 | * Stripe cache |
| 34 | */ |
| 35 | |
| 36 | #define NR_STRIPES 256 |
| 37 | #define STRIPE_SIZE PAGE_SIZE |
| 38 | #define STRIPE_SHIFT (PAGE_SHIFT - 9) |
| 39 | #define STRIPE_SECTORS (STRIPE_SIZE>>9) |
| 40 | #define IO_THRESHOLD 1 |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 41 | #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #define HASH_MASK (NR_HASH - 1) |
| 43 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 44 | #define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK])) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | /* bio's attached to a stripe+device for I/O are linked together in bi_sector |
| 47 | * order without overlap. There may be several bio's per stripe+device, and |
| 48 | * a bio could span several devices. |
| 49 | * When walking this list for a particular stripe+device, we must never proceed |
| 50 | * beyond a bio that extends past this device, as the next bio might no longer |
| 51 | * be valid. |
| 52 | * This macro is used to determine the 'next' bio in the list, given the sector |
| 53 | * of the current stripe+device |
| 54 | */ |
| 55 | #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL) |
| 56 | /* |
| 57 | * The following can be used to debug the driver |
| 58 | */ |
| 59 | #define RAID5_DEBUG 0 |
| 60 | #define RAID5_PARANOIA 1 |
| 61 | #if RAID5_PARANOIA && defined(CONFIG_SMP) |
| 62 | # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock) |
| 63 | #else |
| 64 | # define CHECK_DEVLOCK() |
| 65 | #endif |
| 66 | |
| 67 | #define PRINTK(x...) ((void)(RAID5_DEBUG && printk(x))) |
| 68 | #if RAID5_DEBUG |
| 69 | #define inline |
| 70 | #define __inline__ |
| 71 | #endif |
| 72 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 73 | #if !RAID6_USE_EMPTY_ZERO_PAGE |
| 74 | /* In .bss so it's zeroed */ |
| 75 | const char raid6_empty_zero_page[PAGE_SIZE] __attribute__((aligned(256))); |
| 76 | #endif |
| 77 | |
| 78 | static inline int raid6_next_disk(int disk, int raid_disks) |
| 79 | { |
| 80 | disk++; |
| 81 | return (disk < raid_disks) ? disk : 0; |
| 82 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | static void print_raid5_conf (raid5_conf_t *conf); |
| 84 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 85 | static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | { |
| 87 | if (atomic_dec_and_test(&sh->count)) { |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 88 | BUG_ON(!list_empty(&sh->lru)); |
| 89 | BUG_ON(atomic_read(&conf->active_stripes)==0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | if (test_bit(STRIPE_HANDLE, &sh->state)) { |
NeilBrown | 7c785b7 | 2006-07-10 04:44:16 -0700 | [diff] [blame^] | 91 | if (test_bit(STRIPE_DELAYED, &sh->state)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | list_add_tail(&sh->lru, &conf->delayed_list); |
NeilBrown | 7c785b7 | 2006-07-10 04:44:16 -0700 | [diff] [blame^] | 93 | blk_plug_device(conf->mddev->queue); |
| 94 | } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) && |
| 95 | conf->seq_write == sh->bm_seq) { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 96 | list_add_tail(&sh->lru, &conf->bitmap_list); |
NeilBrown | 7c785b7 | 2006-07-10 04:44:16 -0700 | [diff] [blame^] | 97 | blk_plug_device(conf->mddev->queue); |
| 98 | } else { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 99 | clear_bit(STRIPE_BIT_DELAY, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | list_add_tail(&sh->lru, &conf->handle_list); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 101 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | md_wakeup_thread(conf->mddev->thread); |
| 103 | } else { |
| 104 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
| 105 | atomic_dec(&conf->preread_active_stripes); |
| 106 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) |
| 107 | md_wakeup_thread(conf->mddev->thread); |
| 108 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | atomic_dec(&conf->active_stripes); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 110 | if (!test_bit(STRIPE_EXPANDING, &sh->state)) { |
| 111 | list_add_tail(&sh->lru, &conf->inactive_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | wake_up(&conf->wait_for_stripe); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 113 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | } |
| 117 | static void release_stripe(struct stripe_head *sh) |
| 118 | { |
| 119 | raid5_conf_t *conf = sh->raid_conf; |
| 120 | unsigned long flags; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | spin_lock_irqsave(&conf->device_lock, flags); |
| 123 | __release_stripe(conf, sh); |
| 124 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 125 | } |
| 126 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 127 | static inline void remove_hash(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | { |
| 129 | PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh->sector); |
| 130 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 131 | hlist_del_init(&sh->hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | } |
| 133 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 134 | static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | { |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 136 | struct hlist_head *hp = stripe_hash(conf, sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
| 138 | PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh->sector); |
| 139 | |
| 140 | CHECK_DEVLOCK(); |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 141 | hlist_add_head(&sh->hash, hp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | |
| 145 | /* find an idle stripe, make sure it is unhashed, and return it. */ |
| 146 | static struct stripe_head *get_free_stripe(raid5_conf_t *conf) |
| 147 | { |
| 148 | struct stripe_head *sh = NULL; |
| 149 | struct list_head *first; |
| 150 | |
| 151 | CHECK_DEVLOCK(); |
| 152 | if (list_empty(&conf->inactive_list)) |
| 153 | goto out; |
| 154 | first = conf->inactive_list.next; |
| 155 | sh = list_entry(first, struct stripe_head, lru); |
| 156 | list_del_init(first); |
| 157 | remove_hash(sh); |
| 158 | atomic_inc(&conf->active_stripes); |
| 159 | out: |
| 160 | return sh; |
| 161 | } |
| 162 | |
| 163 | static void shrink_buffers(struct stripe_head *sh, int num) |
| 164 | { |
| 165 | struct page *p; |
| 166 | int i; |
| 167 | |
| 168 | for (i=0; i<num ; i++) { |
| 169 | p = sh->dev[i].page; |
| 170 | if (!p) |
| 171 | continue; |
| 172 | sh->dev[i].page = NULL; |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 173 | put_page(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
| 177 | static int grow_buffers(struct stripe_head *sh, int num) |
| 178 | { |
| 179 | int i; |
| 180 | |
| 181 | for (i=0; i<num; i++) { |
| 182 | struct page *page; |
| 183 | |
| 184 | if (!(page = alloc_page(GFP_KERNEL))) { |
| 185 | return 1; |
| 186 | } |
| 187 | sh->dev[i].page = page; |
| 188 | } |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | static void raid5_build_block (struct stripe_head *sh, int i); |
| 193 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 194 | static void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx, int disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | { |
| 196 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 197 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 199 | BUG_ON(atomic_read(&sh->count) != 0); |
| 200 | BUG_ON(test_bit(STRIPE_HANDLE, &sh->state)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
| 202 | CHECK_DEVLOCK(); |
| 203 | PRINTK("init_stripe called, stripe %llu\n", |
| 204 | (unsigned long long)sh->sector); |
| 205 | |
| 206 | remove_hash(sh); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 207 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | sh->sector = sector; |
| 209 | sh->pd_idx = pd_idx; |
| 210 | sh->state = 0; |
| 211 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 212 | sh->disks = disks; |
| 213 | |
| 214 | for (i = sh->disks; i--; ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | struct r5dev *dev = &sh->dev[i]; |
| 216 | |
| 217 | if (dev->toread || dev->towrite || dev->written || |
| 218 | test_bit(R5_LOCKED, &dev->flags)) { |
| 219 | printk("sector=%llx i=%d %p %p %p %d\n", |
| 220 | (unsigned long long)sh->sector, i, dev->toread, |
| 221 | dev->towrite, dev->written, |
| 222 | test_bit(R5_LOCKED, &dev->flags)); |
| 223 | BUG(); |
| 224 | } |
| 225 | dev->flags = 0; |
| 226 | raid5_build_block(sh, i); |
| 227 | } |
| 228 | insert_hash(conf, sh); |
| 229 | } |
| 230 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 231 | static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector, int disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | { |
| 233 | struct stripe_head *sh; |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 234 | struct hlist_node *hn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | |
| 236 | CHECK_DEVLOCK(); |
| 237 | PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector); |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 238 | hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash) |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 239 | if (sh->sector == sector && sh->disks == disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | return sh; |
| 241 | PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector); |
| 242 | return NULL; |
| 243 | } |
| 244 | |
| 245 | static void unplug_slaves(mddev_t *mddev); |
| 246 | static void raid5_unplug_device(request_queue_t *q); |
| 247 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 248 | static struct stripe_head *get_active_stripe(raid5_conf_t *conf, sector_t sector, int disks, |
| 249 | int pd_idx, int noblock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | { |
| 251 | struct stripe_head *sh; |
| 252 | |
| 253 | PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector); |
| 254 | |
| 255 | spin_lock_irq(&conf->device_lock); |
| 256 | |
| 257 | do { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 258 | wait_event_lock_irq(conf->wait_for_stripe, |
| 259 | conf->quiesce == 0, |
| 260 | conf->device_lock, /* nothing */); |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 261 | sh = __find_stripe(conf, sector, disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | if (!sh) { |
| 263 | if (!conf->inactive_blocked) |
| 264 | sh = get_free_stripe(conf); |
| 265 | if (noblock && sh == NULL) |
| 266 | break; |
| 267 | if (!sh) { |
| 268 | conf->inactive_blocked = 1; |
| 269 | wait_event_lock_irq(conf->wait_for_stripe, |
| 270 | !list_empty(&conf->inactive_list) && |
NeilBrown | 5036805 | 2005-12-12 02:39:17 -0800 | [diff] [blame] | 271 | (atomic_read(&conf->active_stripes) |
| 272 | < (conf->max_nr_stripes *3/4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | || !conf->inactive_blocked), |
| 274 | conf->device_lock, |
NeilBrown | f437078 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 275 | raid5_unplug_device(conf->mddev->queue) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | ); |
| 277 | conf->inactive_blocked = 0; |
| 278 | } else |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 279 | init_stripe(sh, sector, pd_idx, disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | } else { |
| 281 | if (atomic_read(&sh->count)) { |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 282 | BUG_ON(!list_empty(&sh->lru)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | } else { |
| 284 | if (!test_bit(STRIPE_HANDLE, &sh->state)) |
| 285 | atomic_inc(&conf->active_stripes); |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 286 | if (list_empty(&sh->lru) && |
| 287 | !test_bit(STRIPE_EXPANDING, &sh->state)) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 288 | BUG(); |
| 289 | list_del_init(&sh->lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | } while (sh == NULL); |
| 293 | |
| 294 | if (sh) |
| 295 | atomic_inc(&sh->count); |
| 296 | |
| 297 | spin_unlock_irq(&conf->device_lock); |
| 298 | return sh; |
| 299 | } |
| 300 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 301 | static int grow_one_stripe(raid5_conf_t *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | { |
| 303 | struct stripe_head *sh; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 304 | sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL); |
| 305 | if (!sh) |
| 306 | return 0; |
| 307 | memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev)); |
| 308 | sh->raid_conf = conf; |
| 309 | spin_lock_init(&sh->lock); |
| 310 | |
| 311 | if (grow_buffers(sh, conf->raid_disks)) { |
| 312 | shrink_buffers(sh, conf->raid_disks); |
| 313 | kmem_cache_free(conf->slab_cache, sh); |
| 314 | return 0; |
| 315 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 316 | sh->disks = conf->raid_disks; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 317 | /* we just created an active stripe so... */ |
| 318 | atomic_set(&sh->count, 1); |
| 319 | atomic_inc(&conf->active_stripes); |
| 320 | INIT_LIST_HEAD(&sh->lru); |
| 321 | release_stripe(sh); |
| 322 | return 1; |
| 323 | } |
| 324 | |
| 325 | static int grow_stripes(raid5_conf_t *conf, int num) |
| 326 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | kmem_cache_t *sc; |
| 328 | int devs = conf->raid_disks; |
| 329 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 330 | sprintf(conf->cache_name[0], "raid5/%s", mdname(conf->mddev)); |
| 331 | sprintf(conf->cache_name[1], "raid5/%s-alt", mdname(conf->mddev)); |
| 332 | conf->active_name = 0; |
| 333 | sc = kmem_cache_create(conf->cache_name[conf->active_name], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev), |
| 335 | 0, 0, NULL, NULL); |
| 336 | if (!sc) |
| 337 | return 1; |
| 338 | conf->slab_cache = sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 339 | conf->pool_size = devs; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 340 | while (num--) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 341 | if (!grow_one_stripe(conf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | return 0; |
| 344 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 345 | |
| 346 | #ifdef CONFIG_MD_RAID5_RESHAPE |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 347 | static int resize_stripes(raid5_conf_t *conf, int newsize) |
| 348 | { |
| 349 | /* Make all the stripes able to hold 'newsize' devices. |
| 350 | * New slots in each stripe get 'page' set to a new page. |
| 351 | * |
| 352 | * This happens in stages: |
| 353 | * 1/ create a new kmem_cache and allocate the required number of |
| 354 | * stripe_heads. |
| 355 | * 2/ gather all the old stripe_heads and tranfer the pages across |
| 356 | * to the new stripe_heads. This will have the side effect of |
| 357 | * freezing the array as once all stripe_heads have been collected, |
| 358 | * no IO will be possible. Old stripe heads are freed once their |
| 359 | * pages have been transferred over, and the old kmem_cache is |
| 360 | * freed when all stripes are done. |
| 361 | * 3/ reallocate conf->disks to be suitable bigger. If this fails, |
| 362 | * we simple return a failre status - no need to clean anything up. |
| 363 | * 4/ allocate new pages for the new slots in the new stripe_heads. |
| 364 | * If this fails, we don't bother trying the shrink the |
| 365 | * stripe_heads down again, we just leave them as they are. |
| 366 | * As each stripe_head is processed the new one is released into |
| 367 | * active service. |
| 368 | * |
| 369 | * Once step2 is started, we cannot afford to wait for a write, |
| 370 | * so we use GFP_NOIO allocations. |
| 371 | */ |
| 372 | struct stripe_head *osh, *nsh; |
| 373 | LIST_HEAD(newstripes); |
| 374 | struct disk_info *ndisks; |
| 375 | int err = 0; |
| 376 | kmem_cache_t *sc; |
| 377 | int i; |
| 378 | |
| 379 | if (newsize <= conf->pool_size) |
| 380 | return 0; /* never bother to shrink */ |
| 381 | |
| 382 | /* Step 1 */ |
| 383 | sc = kmem_cache_create(conf->cache_name[1-conf->active_name], |
| 384 | sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev), |
| 385 | 0, 0, NULL, NULL); |
| 386 | if (!sc) |
| 387 | return -ENOMEM; |
| 388 | |
| 389 | for (i = conf->max_nr_stripes; i; i--) { |
| 390 | nsh = kmem_cache_alloc(sc, GFP_KERNEL); |
| 391 | if (!nsh) |
| 392 | break; |
| 393 | |
| 394 | memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev)); |
| 395 | |
| 396 | nsh->raid_conf = conf; |
| 397 | spin_lock_init(&nsh->lock); |
| 398 | |
| 399 | list_add(&nsh->lru, &newstripes); |
| 400 | } |
| 401 | if (i) { |
| 402 | /* didn't get enough, give up */ |
| 403 | while (!list_empty(&newstripes)) { |
| 404 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 405 | list_del(&nsh->lru); |
| 406 | kmem_cache_free(sc, nsh); |
| 407 | } |
| 408 | kmem_cache_destroy(sc); |
| 409 | return -ENOMEM; |
| 410 | } |
| 411 | /* Step 2 - Must use GFP_NOIO now. |
| 412 | * OK, we have enough stripes, start collecting inactive |
| 413 | * stripes and copying them over |
| 414 | */ |
| 415 | list_for_each_entry(nsh, &newstripes, lru) { |
| 416 | spin_lock_irq(&conf->device_lock); |
| 417 | wait_event_lock_irq(conf->wait_for_stripe, |
| 418 | !list_empty(&conf->inactive_list), |
| 419 | conf->device_lock, |
NeilBrown | b3b46be | 2006-03-27 01:18:16 -0800 | [diff] [blame] | 420 | unplug_slaves(conf->mddev) |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 421 | ); |
| 422 | osh = get_free_stripe(conf); |
| 423 | spin_unlock_irq(&conf->device_lock); |
| 424 | atomic_set(&nsh->count, 1); |
| 425 | for(i=0; i<conf->pool_size; i++) |
| 426 | nsh->dev[i].page = osh->dev[i].page; |
| 427 | for( ; i<newsize; i++) |
| 428 | nsh->dev[i].page = NULL; |
| 429 | kmem_cache_free(conf->slab_cache, osh); |
| 430 | } |
| 431 | kmem_cache_destroy(conf->slab_cache); |
| 432 | |
| 433 | /* Step 3. |
| 434 | * At this point, we are holding all the stripes so the array |
| 435 | * is completely stalled, so now is a good time to resize |
| 436 | * conf->disks. |
| 437 | */ |
| 438 | ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO); |
| 439 | if (ndisks) { |
| 440 | for (i=0; i<conf->raid_disks; i++) |
| 441 | ndisks[i] = conf->disks[i]; |
| 442 | kfree(conf->disks); |
| 443 | conf->disks = ndisks; |
| 444 | } else |
| 445 | err = -ENOMEM; |
| 446 | |
| 447 | /* Step 4, return new stripes to service */ |
| 448 | while(!list_empty(&newstripes)) { |
| 449 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 450 | list_del_init(&nsh->lru); |
| 451 | for (i=conf->raid_disks; i < newsize; i++) |
| 452 | if (nsh->dev[i].page == NULL) { |
| 453 | struct page *p = alloc_page(GFP_NOIO); |
| 454 | nsh->dev[i].page = p; |
| 455 | if (!p) |
| 456 | err = -ENOMEM; |
| 457 | } |
| 458 | release_stripe(nsh); |
| 459 | } |
| 460 | /* critical section pass, GFP_NOIO no longer needed */ |
| 461 | |
| 462 | conf->slab_cache = sc; |
| 463 | conf->active_name = 1-conf->active_name; |
| 464 | conf->pool_size = newsize; |
| 465 | return err; |
| 466 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 467 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 469 | static int drop_one_stripe(raid5_conf_t *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | { |
| 471 | struct stripe_head *sh; |
| 472 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 473 | spin_lock_irq(&conf->device_lock); |
| 474 | sh = get_free_stripe(conf); |
| 475 | spin_unlock_irq(&conf->device_lock); |
| 476 | if (!sh) |
| 477 | return 0; |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 478 | BUG_ON(atomic_read(&sh->count)); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 479 | shrink_buffers(sh, conf->pool_size); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 480 | kmem_cache_free(conf->slab_cache, sh); |
| 481 | atomic_dec(&conf->active_stripes); |
| 482 | return 1; |
| 483 | } |
| 484 | |
| 485 | static void shrink_stripes(raid5_conf_t *conf) |
| 486 | { |
| 487 | while (drop_one_stripe(conf)) |
| 488 | ; |
| 489 | |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 490 | if (conf->slab_cache) |
| 491 | kmem_cache_destroy(conf->slab_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | conf->slab_cache = NULL; |
| 493 | } |
| 494 | |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 495 | static int raid5_end_read_request(struct bio * bi, unsigned int bytes_done, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | int error) |
| 497 | { |
| 498 | struct stripe_head *sh = bi->bi_private; |
| 499 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 500 | int disks = sh->disks, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
| 502 | |
| 503 | if (bi->bi_size) |
| 504 | return 1; |
| 505 | |
| 506 | for (i=0 ; i<disks; i++) |
| 507 | if (bi == &sh->dev[i].req) |
| 508 | break; |
| 509 | |
| 510 | PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n", |
| 511 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
| 512 | uptodate); |
| 513 | if (i == disks) { |
| 514 | BUG(); |
| 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | if (uptodate) { |
| 519 | #if 0 |
| 520 | struct bio *bio; |
| 521 | unsigned long flags; |
| 522 | spin_lock_irqsave(&conf->device_lock, flags); |
| 523 | /* we can return a buffer if we bypassed the cache or |
| 524 | * if the top buffer is not in highmem. If there are |
| 525 | * multiple buffers, leave the extra work to |
| 526 | * handle_stripe |
| 527 | */ |
| 528 | buffer = sh->bh_read[i]; |
| 529 | if (buffer && |
| 530 | (!PageHighMem(buffer->b_page) |
| 531 | || buffer->b_page == bh->b_page ) |
| 532 | ) { |
| 533 | sh->bh_read[i] = buffer->b_reqnext; |
| 534 | buffer->b_reqnext = NULL; |
| 535 | } else |
| 536 | buffer = NULL; |
| 537 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 538 | if (sh->bh_page[i]==bh->b_page) |
| 539 | set_buffer_uptodate(bh); |
| 540 | if (buffer) { |
| 541 | if (buffer->b_page != bh->b_page) |
| 542 | memcpy(buffer->b_data, bh->b_data, bh->b_size); |
| 543 | buffer->b_end_io(buffer, 1); |
| 544 | } |
| 545 | #else |
| 546 | set_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 547 | #endif |
| 548 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 549 | printk(KERN_INFO "raid5: read error corrected!!\n"); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 550 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 551 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
| 552 | } |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 553 | if (atomic_read(&conf->disks[i].rdev->read_errors)) |
| 554 | atomic_set(&conf->disks[i].rdev->read_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | } else { |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 556 | int retry = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | clear_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 558 | atomic_inc(&conf->disks[i].rdev->read_errors); |
| 559 | if (conf->mddev->degraded) |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 560 | printk(KERN_WARNING "raid5: read error not correctable.\n"); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 561 | else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 562 | /* Oh, no!!! */ |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 563 | printk(KERN_WARNING "raid5: read error NOT corrected!!\n"); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 564 | else if (atomic_read(&conf->disks[i].rdev->read_errors) |
| 565 | > conf->max_nr_stripes) |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 566 | printk(KERN_WARNING |
| 567 | "raid5: Too many read errors, failing device.\n"); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 568 | else |
| 569 | retry = 1; |
| 570 | if (retry) |
| 571 | set_bit(R5_ReadError, &sh->dev[i].flags); |
| 572 | else { |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 573 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 574 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
| 575 | md_error(conf->mddev, conf->disks[i].rdev); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 576 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | } |
| 578 | rdev_dec_pending(conf->disks[i].rdev, conf->mddev); |
| 579 | #if 0 |
| 580 | /* must restore b_page before unlocking buffer... */ |
| 581 | if (sh->bh_page[i] != bh->b_page) { |
| 582 | bh->b_page = sh->bh_page[i]; |
| 583 | bh->b_data = page_address(bh->b_page); |
| 584 | clear_buffer_uptodate(bh); |
| 585 | } |
| 586 | #endif |
| 587 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 588 | set_bit(STRIPE_HANDLE, &sh->state); |
| 589 | release_stripe(sh); |
| 590 | return 0; |
| 591 | } |
| 592 | |
| 593 | static int raid5_end_write_request (struct bio *bi, unsigned int bytes_done, |
| 594 | int error) |
| 595 | { |
| 596 | struct stripe_head *sh = bi->bi_private; |
| 597 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 598 | int disks = sh->disks, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | unsigned long flags; |
| 600 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
| 601 | |
| 602 | if (bi->bi_size) |
| 603 | return 1; |
| 604 | |
| 605 | for (i=0 ; i<disks; i++) |
| 606 | if (bi == &sh->dev[i].req) |
| 607 | break; |
| 608 | |
| 609 | PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n", |
| 610 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
| 611 | uptodate); |
| 612 | if (i == disks) { |
| 613 | BUG(); |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | spin_lock_irqsave(&conf->device_lock, flags); |
| 618 | if (!uptodate) |
| 619 | md_error(conf->mddev, conf->disks[i].rdev); |
| 620 | |
| 621 | rdev_dec_pending(conf->disks[i].rdev, conf->mddev); |
| 622 | |
| 623 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 624 | set_bit(STRIPE_HANDLE, &sh->state); |
| 625 | __release_stripe(conf, sh); |
| 626 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 627 | return 0; |
| 628 | } |
| 629 | |
| 630 | |
| 631 | static sector_t compute_blocknr(struct stripe_head *sh, int i); |
| 632 | |
| 633 | static void raid5_build_block (struct stripe_head *sh, int i) |
| 634 | { |
| 635 | struct r5dev *dev = &sh->dev[i]; |
| 636 | |
| 637 | bio_init(&dev->req); |
| 638 | dev->req.bi_io_vec = &dev->vec; |
| 639 | dev->req.bi_vcnt++; |
| 640 | dev->req.bi_max_vecs++; |
| 641 | dev->vec.bv_page = dev->page; |
| 642 | dev->vec.bv_len = STRIPE_SIZE; |
| 643 | dev->vec.bv_offset = 0; |
| 644 | |
| 645 | dev->req.bi_sector = sh->sector; |
| 646 | dev->req.bi_private = sh; |
| 647 | |
| 648 | dev->flags = 0; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 649 | dev->sector = compute_blocknr(sh, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | static void error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 653 | { |
| 654 | char b[BDEVNAME_SIZE]; |
| 655 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; |
| 656 | PRINTK("raid5: error called\n"); |
| 657 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 658 | if (!test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | mddev->sb_dirty = 1; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 660 | if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | conf->working_disks--; |
| 662 | mddev->degraded++; |
| 663 | conf->failed_disks++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 664 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | /* |
| 666 | * if recovery was running, make sure it aborts. |
| 667 | */ |
| 668 | set_bit(MD_RECOVERY_ERR, &mddev->recovery); |
| 669 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 670 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | printk (KERN_ALERT |
| 672 | "raid5: Disk failure on %s, disabling device." |
| 673 | " Operation continuing on %d devices\n", |
| 674 | bdevname(rdev->bdev,b), conf->working_disks); |
| 675 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 676 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | |
| 678 | /* |
| 679 | * Input: a 'big' sector number, |
| 680 | * Output: index of the data and parity disk, and the sector # in them. |
| 681 | */ |
| 682 | static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks, |
| 683 | unsigned int data_disks, unsigned int * dd_idx, |
| 684 | unsigned int * pd_idx, raid5_conf_t *conf) |
| 685 | { |
| 686 | long stripe; |
| 687 | unsigned long chunk_number; |
| 688 | unsigned int chunk_offset; |
| 689 | sector_t new_sector; |
| 690 | int sectors_per_chunk = conf->chunk_size >> 9; |
| 691 | |
| 692 | /* First compute the information on this sector */ |
| 693 | |
| 694 | /* |
| 695 | * Compute the chunk number and the sector offset inside the chunk |
| 696 | */ |
| 697 | chunk_offset = sector_div(r_sector, sectors_per_chunk); |
| 698 | chunk_number = r_sector; |
| 699 | BUG_ON(r_sector != chunk_number); |
| 700 | |
| 701 | /* |
| 702 | * Compute the stripe number |
| 703 | */ |
| 704 | stripe = chunk_number / data_disks; |
| 705 | |
| 706 | /* |
| 707 | * Compute the data disk and parity disk indexes inside the stripe |
| 708 | */ |
| 709 | *dd_idx = chunk_number % data_disks; |
| 710 | |
| 711 | /* |
| 712 | * Select the parity disk based on the user selected algorithm. |
| 713 | */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 714 | switch(conf->level) { |
| 715 | case 4: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | *pd_idx = data_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 717 | break; |
| 718 | case 5: |
| 719 | switch (conf->algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 721 | *pd_idx = data_disks - stripe % raid_disks; |
| 722 | if (*dd_idx >= *pd_idx) |
| 723 | (*dd_idx)++; |
| 724 | break; |
| 725 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 726 | *pd_idx = stripe % raid_disks; |
| 727 | if (*dd_idx >= *pd_idx) |
| 728 | (*dd_idx)++; |
| 729 | break; |
| 730 | case ALGORITHM_LEFT_SYMMETRIC: |
| 731 | *pd_idx = data_disks - stripe % raid_disks; |
| 732 | *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks; |
| 733 | break; |
| 734 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 735 | *pd_idx = stripe % raid_disks; |
| 736 | *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks; |
| 737 | break; |
| 738 | default: |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 739 | printk(KERN_ERR "raid5: unsupported algorithm %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | conf->algorithm); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 741 | } |
| 742 | break; |
| 743 | case 6: |
| 744 | |
| 745 | /**** FIX THIS ****/ |
| 746 | switch (conf->algorithm) { |
| 747 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 748 | *pd_idx = raid_disks - 1 - (stripe % raid_disks); |
| 749 | if (*pd_idx == raid_disks-1) |
| 750 | (*dd_idx)++; /* Q D D D P */ |
| 751 | else if (*dd_idx >= *pd_idx) |
| 752 | (*dd_idx) += 2; /* D D P Q D */ |
| 753 | break; |
| 754 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 755 | *pd_idx = stripe % raid_disks; |
| 756 | if (*pd_idx == raid_disks-1) |
| 757 | (*dd_idx)++; /* Q D D D P */ |
| 758 | else if (*dd_idx >= *pd_idx) |
| 759 | (*dd_idx) += 2; /* D D P Q D */ |
| 760 | break; |
| 761 | case ALGORITHM_LEFT_SYMMETRIC: |
| 762 | *pd_idx = raid_disks - 1 - (stripe % raid_disks); |
| 763 | *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks; |
| 764 | break; |
| 765 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 766 | *pd_idx = stripe % raid_disks; |
| 767 | *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks; |
| 768 | break; |
| 769 | default: |
| 770 | printk (KERN_CRIT "raid6: unsupported algorithm %d\n", |
| 771 | conf->algorithm); |
| 772 | } |
| 773 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | /* |
| 777 | * Finally, compute the new sector number |
| 778 | */ |
| 779 | new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset; |
| 780 | return new_sector; |
| 781 | } |
| 782 | |
| 783 | |
| 784 | static sector_t compute_blocknr(struct stripe_head *sh, int i) |
| 785 | { |
| 786 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 787 | int raid_disks = sh->disks, data_disks = raid_disks - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | sector_t new_sector = sh->sector, check; |
| 789 | int sectors_per_chunk = conf->chunk_size >> 9; |
| 790 | sector_t stripe; |
| 791 | int chunk_offset; |
| 792 | int chunk_number, dummy1, dummy2, dd_idx = i; |
| 793 | sector_t r_sector; |
| 794 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 795 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | chunk_offset = sector_div(new_sector, sectors_per_chunk); |
| 797 | stripe = new_sector; |
| 798 | BUG_ON(new_sector != stripe); |
| 799 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 800 | if (i == sh->pd_idx) |
| 801 | return 0; |
| 802 | switch(conf->level) { |
| 803 | case 4: break; |
| 804 | case 5: |
| 805 | switch (conf->algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 807 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 808 | if (i > sh->pd_idx) |
| 809 | i--; |
| 810 | break; |
| 811 | case ALGORITHM_LEFT_SYMMETRIC: |
| 812 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 813 | if (i < sh->pd_idx) |
| 814 | i += raid_disks; |
| 815 | i -= (sh->pd_idx + 1); |
| 816 | break; |
| 817 | default: |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 818 | printk(KERN_ERR "raid5: unsupported algorithm %d\n", |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 819 | conf->algorithm); |
| 820 | } |
| 821 | break; |
| 822 | case 6: |
| 823 | data_disks = raid_disks - 2; |
| 824 | if (i == raid6_next_disk(sh->pd_idx, raid_disks)) |
| 825 | return 0; /* It is the Q disk */ |
| 826 | switch (conf->algorithm) { |
| 827 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 828 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 829 | if (sh->pd_idx == raid_disks-1) |
| 830 | i--; /* Q D D D P */ |
| 831 | else if (i > sh->pd_idx) |
| 832 | i -= 2; /* D D P Q D */ |
| 833 | break; |
| 834 | case ALGORITHM_LEFT_SYMMETRIC: |
| 835 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 836 | if (sh->pd_idx == raid_disks-1) |
| 837 | i--; /* Q D D D P */ |
| 838 | else { |
| 839 | /* D D P Q D */ |
| 840 | if (i < sh->pd_idx) |
| 841 | i += raid_disks; |
| 842 | i -= (sh->pd_idx + 2); |
| 843 | } |
| 844 | break; |
| 845 | default: |
| 846 | printk (KERN_CRIT "raid6: unsupported algorithm %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | conf->algorithm); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 848 | } |
| 849 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | chunk_number = stripe * data_disks + i; |
| 853 | r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset; |
| 854 | |
| 855 | check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf); |
| 856 | if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) { |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 857 | printk(KERN_ERR "compute_blocknr: map not correct\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | return 0; |
| 859 | } |
| 860 | return r_sector; |
| 861 | } |
| 862 | |
| 863 | |
| 864 | |
| 865 | /* |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 866 | * Copy data between a page in the stripe cache, and one or more bion |
| 867 | * The page could align with the middle of the bio, or there could be |
| 868 | * several bion, each with several bio_vecs, which cover part of the page |
| 869 | * Multiple bion are linked together on bi_next. There may be extras |
| 870 | * at the end of this list. We ignore them. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | */ |
| 872 | static void copy_data(int frombio, struct bio *bio, |
| 873 | struct page *page, |
| 874 | sector_t sector) |
| 875 | { |
| 876 | char *pa = page_address(page); |
| 877 | struct bio_vec *bvl; |
| 878 | int i; |
| 879 | int page_offset; |
| 880 | |
| 881 | if (bio->bi_sector >= sector) |
| 882 | page_offset = (signed)(bio->bi_sector - sector) * 512; |
| 883 | else |
| 884 | page_offset = (signed)(sector - bio->bi_sector) * -512; |
| 885 | bio_for_each_segment(bvl, bio, i) { |
| 886 | int len = bio_iovec_idx(bio,i)->bv_len; |
| 887 | int clen; |
| 888 | int b_offset = 0; |
| 889 | |
| 890 | if (page_offset < 0) { |
| 891 | b_offset = -page_offset; |
| 892 | page_offset += b_offset; |
| 893 | len -= b_offset; |
| 894 | } |
| 895 | |
| 896 | if (len > 0 && page_offset + len > STRIPE_SIZE) |
| 897 | clen = STRIPE_SIZE - page_offset; |
| 898 | else clen = len; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 899 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | if (clen > 0) { |
| 901 | char *ba = __bio_kmap_atomic(bio, i, KM_USER0); |
| 902 | if (frombio) |
| 903 | memcpy(pa+page_offset, ba+b_offset, clen); |
| 904 | else |
| 905 | memcpy(ba+b_offset, pa+page_offset, clen); |
| 906 | __bio_kunmap_atomic(ba, KM_USER0); |
| 907 | } |
| 908 | if (clen < len) /* hit end of page */ |
| 909 | break; |
| 910 | page_offset += len; |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | #define check_xor() do { \ |
| 915 | if (count == MAX_XOR_BLOCKS) { \ |
| 916 | xor_block(count, STRIPE_SIZE, ptr); \ |
| 917 | count = 1; \ |
| 918 | } \ |
| 919 | } while(0) |
| 920 | |
| 921 | |
| 922 | static void compute_block(struct stripe_head *sh, int dd_idx) |
| 923 | { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 924 | int i, count, disks = sh->disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | void *ptr[MAX_XOR_BLOCKS], *p; |
| 926 | |
| 927 | PRINTK("compute_block, stripe %llu, idx %d\n", |
| 928 | (unsigned long long)sh->sector, dd_idx); |
| 929 | |
| 930 | ptr[0] = page_address(sh->dev[dd_idx].page); |
| 931 | memset(ptr[0], 0, STRIPE_SIZE); |
| 932 | count = 1; |
| 933 | for (i = disks ; i--; ) { |
| 934 | if (i == dd_idx) |
| 935 | continue; |
| 936 | p = page_address(sh->dev[i].page); |
| 937 | if (test_bit(R5_UPTODATE, &sh->dev[i].flags)) |
| 938 | ptr[count++] = p; |
| 939 | else |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 940 | printk(KERN_ERR "compute_block() %d, stripe %llu, %d" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | " not present\n", dd_idx, |
| 942 | (unsigned long long)sh->sector, i); |
| 943 | |
| 944 | check_xor(); |
| 945 | } |
| 946 | if (count != 1) |
| 947 | xor_block(count, STRIPE_SIZE, ptr); |
| 948 | set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags); |
| 949 | } |
| 950 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 951 | static void compute_parity5(struct stripe_head *sh, int method) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | { |
| 953 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 954 | int i, pd_idx = sh->pd_idx, disks = sh->disks, count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | void *ptr[MAX_XOR_BLOCKS]; |
| 956 | struct bio *chosen; |
| 957 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 958 | PRINTK("compute_parity5, stripe %llu, method %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | (unsigned long long)sh->sector, method); |
| 960 | |
| 961 | count = 1; |
| 962 | ptr[0] = page_address(sh->dev[pd_idx].page); |
| 963 | switch(method) { |
| 964 | case READ_MODIFY_WRITE: |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 965 | BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | for (i=disks ; i-- ;) { |
| 967 | if (i==pd_idx) |
| 968 | continue; |
| 969 | if (sh->dev[i].towrite && |
| 970 | test_bit(R5_UPTODATE, &sh->dev[i].flags)) { |
| 971 | ptr[count++] = page_address(sh->dev[i].page); |
| 972 | chosen = sh->dev[i].towrite; |
| 973 | sh->dev[i].towrite = NULL; |
| 974 | |
| 975 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 976 | wake_up(&conf->wait_for_overlap); |
| 977 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 978 | BUG_ON(sh->dev[i].written); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | sh->dev[i].written = chosen; |
| 980 | check_xor(); |
| 981 | } |
| 982 | } |
| 983 | break; |
| 984 | case RECONSTRUCT_WRITE: |
| 985 | memset(ptr[0], 0, STRIPE_SIZE); |
| 986 | for (i= disks; i-- ;) |
| 987 | if (i!=pd_idx && sh->dev[i].towrite) { |
| 988 | chosen = sh->dev[i].towrite; |
| 989 | sh->dev[i].towrite = NULL; |
| 990 | |
| 991 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 992 | wake_up(&conf->wait_for_overlap); |
| 993 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 994 | BUG_ON(sh->dev[i].written); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | sh->dev[i].written = chosen; |
| 996 | } |
| 997 | break; |
| 998 | case CHECK_PARITY: |
| 999 | break; |
| 1000 | } |
| 1001 | if (count>1) { |
| 1002 | xor_block(count, STRIPE_SIZE, ptr); |
| 1003 | count = 1; |
| 1004 | } |
| 1005 | |
| 1006 | for (i = disks; i--;) |
| 1007 | if (sh->dev[i].written) { |
| 1008 | sector_t sector = sh->dev[i].sector; |
| 1009 | struct bio *wbi = sh->dev[i].written; |
| 1010 | while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) { |
| 1011 | copy_data(1, wbi, sh->dev[i].page, sector); |
| 1012 | wbi = r5_next_bio(wbi, sector); |
| 1013 | } |
| 1014 | |
| 1015 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 1016 | set_bit(R5_UPTODATE, &sh->dev[i].flags); |
| 1017 | } |
| 1018 | |
| 1019 | switch(method) { |
| 1020 | case RECONSTRUCT_WRITE: |
| 1021 | case CHECK_PARITY: |
| 1022 | for (i=disks; i--;) |
| 1023 | if (i != pd_idx) { |
| 1024 | ptr[count++] = page_address(sh->dev[i].page); |
| 1025 | check_xor(); |
| 1026 | } |
| 1027 | break; |
| 1028 | case READ_MODIFY_WRITE: |
| 1029 | for (i = disks; i--;) |
| 1030 | if (sh->dev[i].written) { |
| 1031 | ptr[count++] = page_address(sh->dev[i].page); |
| 1032 | check_xor(); |
| 1033 | } |
| 1034 | } |
| 1035 | if (count != 1) |
| 1036 | xor_block(count, STRIPE_SIZE, ptr); |
| 1037 | |
| 1038 | if (method != CHECK_PARITY) { |
| 1039 | set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
| 1040 | set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); |
| 1041 | } else |
| 1042 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
| 1043 | } |
| 1044 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1045 | static void compute_parity6(struct stripe_head *sh, int method) |
| 1046 | { |
| 1047 | raid6_conf_t *conf = sh->raid_conf; |
| 1048 | int i, pd_idx = sh->pd_idx, qd_idx, d0_idx, disks = conf->raid_disks, count; |
| 1049 | struct bio *chosen; |
| 1050 | /**** FIX THIS: This could be very bad if disks is close to 256 ****/ |
| 1051 | void *ptrs[disks]; |
| 1052 | |
| 1053 | qd_idx = raid6_next_disk(pd_idx, disks); |
| 1054 | d0_idx = raid6_next_disk(qd_idx, disks); |
| 1055 | |
| 1056 | PRINTK("compute_parity, stripe %llu, method %d\n", |
| 1057 | (unsigned long long)sh->sector, method); |
| 1058 | |
| 1059 | switch(method) { |
| 1060 | case READ_MODIFY_WRITE: |
| 1061 | BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */ |
| 1062 | case RECONSTRUCT_WRITE: |
| 1063 | for (i= disks; i-- ;) |
| 1064 | if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) { |
| 1065 | chosen = sh->dev[i].towrite; |
| 1066 | sh->dev[i].towrite = NULL; |
| 1067 | |
| 1068 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 1069 | wake_up(&conf->wait_for_overlap); |
| 1070 | |
| 1071 | if (sh->dev[i].written) BUG(); |
| 1072 | sh->dev[i].written = chosen; |
| 1073 | } |
| 1074 | break; |
| 1075 | case CHECK_PARITY: |
| 1076 | BUG(); /* Not implemented yet */ |
| 1077 | } |
| 1078 | |
| 1079 | for (i = disks; i--;) |
| 1080 | if (sh->dev[i].written) { |
| 1081 | sector_t sector = sh->dev[i].sector; |
| 1082 | struct bio *wbi = sh->dev[i].written; |
| 1083 | while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) { |
| 1084 | copy_data(1, wbi, sh->dev[i].page, sector); |
| 1085 | wbi = r5_next_bio(wbi, sector); |
| 1086 | } |
| 1087 | |
| 1088 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 1089 | set_bit(R5_UPTODATE, &sh->dev[i].flags); |
| 1090 | } |
| 1091 | |
| 1092 | // switch(method) { |
| 1093 | // case RECONSTRUCT_WRITE: |
| 1094 | // case CHECK_PARITY: |
| 1095 | // case UPDATE_PARITY: |
| 1096 | /* Note that unlike RAID-5, the ordering of the disks matters greatly. */ |
| 1097 | /* FIX: Is this ordering of drives even remotely optimal? */ |
| 1098 | count = 0; |
| 1099 | i = d0_idx; |
| 1100 | do { |
| 1101 | ptrs[count++] = page_address(sh->dev[i].page); |
| 1102 | if (count <= disks-2 && !test_bit(R5_UPTODATE, &sh->dev[i].flags)) |
| 1103 | printk("block %d/%d not uptodate on parity calc\n", i,count); |
| 1104 | i = raid6_next_disk(i, disks); |
| 1105 | } while ( i != d0_idx ); |
| 1106 | // break; |
| 1107 | // } |
| 1108 | |
| 1109 | raid6_call.gen_syndrome(disks, STRIPE_SIZE, ptrs); |
| 1110 | |
| 1111 | switch(method) { |
| 1112 | case RECONSTRUCT_WRITE: |
| 1113 | set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
| 1114 | set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags); |
| 1115 | set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); |
| 1116 | set_bit(R5_LOCKED, &sh->dev[qd_idx].flags); |
| 1117 | break; |
| 1118 | case UPDATE_PARITY: |
| 1119 | set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
| 1120 | set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags); |
| 1121 | break; |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | |
| 1126 | /* Compute one missing block */ |
| 1127 | static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero) |
| 1128 | { |
| 1129 | raid6_conf_t *conf = sh->raid_conf; |
| 1130 | int i, count, disks = conf->raid_disks; |
| 1131 | void *ptr[MAX_XOR_BLOCKS], *p; |
| 1132 | int pd_idx = sh->pd_idx; |
| 1133 | int qd_idx = raid6_next_disk(pd_idx, disks); |
| 1134 | |
| 1135 | PRINTK("compute_block_1, stripe %llu, idx %d\n", |
| 1136 | (unsigned long long)sh->sector, dd_idx); |
| 1137 | |
| 1138 | if ( dd_idx == qd_idx ) { |
| 1139 | /* We're actually computing the Q drive */ |
| 1140 | compute_parity6(sh, UPDATE_PARITY); |
| 1141 | } else { |
| 1142 | ptr[0] = page_address(sh->dev[dd_idx].page); |
| 1143 | if (!nozero) memset(ptr[0], 0, STRIPE_SIZE); |
| 1144 | count = 1; |
| 1145 | for (i = disks ; i--; ) { |
| 1146 | if (i == dd_idx || i == qd_idx) |
| 1147 | continue; |
| 1148 | p = page_address(sh->dev[i].page); |
| 1149 | if (test_bit(R5_UPTODATE, &sh->dev[i].flags)) |
| 1150 | ptr[count++] = p; |
| 1151 | else |
| 1152 | printk("compute_block() %d, stripe %llu, %d" |
| 1153 | " not present\n", dd_idx, |
| 1154 | (unsigned long long)sh->sector, i); |
| 1155 | |
| 1156 | check_xor(); |
| 1157 | } |
| 1158 | if (count != 1) |
| 1159 | xor_block(count, STRIPE_SIZE, ptr); |
| 1160 | if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags); |
| 1161 | else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags); |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | /* Compute two missing blocks */ |
| 1166 | static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2) |
| 1167 | { |
| 1168 | raid6_conf_t *conf = sh->raid_conf; |
| 1169 | int i, count, disks = conf->raid_disks; |
| 1170 | int pd_idx = sh->pd_idx; |
| 1171 | int qd_idx = raid6_next_disk(pd_idx, disks); |
| 1172 | int d0_idx = raid6_next_disk(qd_idx, disks); |
| 1173 | int faila, failb; |
| 1174 | |
| 1175 | /* faila and failb are disk numbers relative to d0_idx */ |
| 1176 | /* pd_idx become disks-2 and qd_idx become disks-1 */ |
| 1177 | faila = (dd_idx1 < d0_idx) ? dd_idx1+(disks-d0_idx) : dd_idx1-d0_idx; |
| 1178 | failb = (dd_idx2 < d0_idx) ? dd_idx2+(disks-d0_idx) : dd_idx2-d0_idx; |
| 1179 | |
| 1180 | BUG_ON(faila == failb); |
| 1181 | if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; } |
| 1182 | |
| 1183 | PRINTK("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n", |
| 1184 | (unsigned long long)sh->sector, dd_idx1, dd_idx2, faila, failb); |
| 1185 | |
| 1186 | if ( failb == disks-1 ) { |
| 1187 | /* Q disk is one of the missing disks */ |
| 1188 | if ( faila == disks-2 ) { |
| 1189 | /* Missing P+Q, just recompute */ |
| 1190 | compute_parity6(sh, UPDATE_PARITY); |
| 1191 | return; |
| 1192 | } else { |
| 1193 | /* We're missing D+Q; recompute D from P */ |
| 1194 | compute_block_1(sh, (dd_idx1 == qd_idx) ? dd_idx2 : dd_idx1, 0); |
| 1195 | compute_parity6(sh, UPDATE_PARITY); /* Is this necessary? */ |
| 1196 | return; |
| 1197 | } |
| 1198 | } |
| 1199 | |
| 1200 | /* We're missing D+P or D+D; build pointer table */ |
| 1201 | { |
| 1202 | /**** FIX THIS: This could be very bad if disks is close to 256 ****/ |
| 1203 | void *ptrs[disks]; |
| 1204 | |
| 1205 | count = 0; |
| 1206 | i = d0_idx; |
| 1207 | do { |
| 1208 | ptrs[count++] = page_address(sh->dev[i].page); |
| 1209 | i = raid6_next_disk(i, disks); |
| 1210 | if (i != dd_idx1 && i != dd_idx2 && |
| 1211 | !test_bit(R5_UPTODATE, &sh->dev[i].flags)) |
| 1212 | printk("compute_2 with missing block %d/%d\n", count, i); |
| 1213 | } while ( i != d0_idx ); |
| 1214 | |
| 1215 | if ( failb == disks-2 ) { |
| 1216 | /* We're missing D+P. */ |
| 1217 | raid6_datap_recov(disks, STRIPE_SIZE, faila, ptrs); |
| 1218 | } else { |
| 1219 | /* We're missing D+D. */ |
| 1220 | raid6_2data_recov(disks, STRIPE_SIZE, faila, failb, ptrs); |
| 1221 | } |
| 1222 | |
| 1223 | /* Both the above update both missing blocks */ |
| 1224 | set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags); |
| 1225 | set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags); |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | |
| 1230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | /* |
| 1232 | * Each stripe/dev can have one or more bion attached. |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1233 | * toread/towrite point to the first in a chain. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | * The bi_next chain must be in order. |
| 1235 | */ |
| 1236 | static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite) |
| 1237 | { |
| 1238 | struct bio **bip; |
| 1239 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1240 | int firstwrite=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | |
| 1242 | PRINTK("adding bh b#%llu to stripe s#%llu\n", |
| 1243 | (unsigned long long)bi->bi_sector, |
| 1244 | (unsigned long long)sh->sector); |
| 1245 | |
| 1246 | |
| 1247 | spin_lock(&sh->lock); |
| 1248 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1249 | if (forwrite) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | bip = &sh->dev[dd_idx].towrite; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1251 | if (*bip == NULL && sh->dev[dd_idx].written == NULL) |
| 1252 | firstwrite = 1; |
| 1253 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | bip = &sh->dev[dd_idx].toread; |
| 1255 | while (*bip && (*bip)->bi_sector < bi->bi_sector) { |
| 1256 | if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector) |
| 1257 | goto overlap; |
| 1258 | bip = & (*bip)->bi_next; |
| 1259 | } |
| 1260 | if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9)) |
| 1261 | goto overlap; |
| 1262 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 1263 | BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | if (*bip) |
| 1265 | bi->bi_next = *bip; |
| 1266 | *bip = bi; |
| 1267 | bi->bi_phys_segments ++; |
| 1268 | spin_unlock_irq(&conf->device_lock); |
| 1269 | spin_unlock(&sh->lock); |
| 1270 | |
| 1271 | PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n", |
| 1272 | (unsigned long long)bi->bi_sector, |
| 1273 | (unsigned long long)sh->sector, dd_idx); |
| 1274 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1275 | if (conf->mddev->bitmap && firstwrite) { |
| 1276 | sh->bm_seq = conf->seq_write; |
| 1277 | bitmap_startwrite(conf->mddev->bitmap, sh->sector, |
| 1278 | STRIPE_SECTORS, 0); |
| 1279 | set_bit(STRIPE_BIT_DELAY, &sh->state); |
| 1280 | } |
| 1281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | if (forwrite) { |
| 1283 | /* check if page is covered */ |
| 1284 | sector_t sector = sh->dev[dd_idx].sector; |
| 1285 | for (bi=sh->dev[dd_idx].towrite; |
| 1286 | sector < sh->dev[dd_idx].sector + STRIPE_SECTORS && |
| 1287 | bi && bi->bi_sector <= sector; |
| 1288 | bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) { |
| 1289 | if (bi->bi_sector + (bi->bi_size>>9) >= sector) |
| 1290 | sector = bi->bi_sector + (bi->bi_size>>9); |
| 1291 | } |
| 1292 | if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS) |
| 1293 | set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags); |
| 1294 | } |
| 1295 | return 1; |
| 1296 | |
| 1297 | overlap: |
| 1298 | set_bit(R5_Overlap, &sh->dev[dd_idx].flags); |
| 1299 | spin_unlock_irq(&conf->device_lock); |
| 1300 | spin_unlock(&sh->lock); |
| 1301 | return 0; |
| 1302 | } |
| 1303 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 1304 | static void end_reshape(raid5_conf_t *conf); |
| 1305 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1306 | static int page_is_zero(struct page *p) |
| 1307 | { |
| 1308 | char *a = page_address(p); |
| 1309 | return ((*(u32*)a) == 0 && |
| 1310 | memcmp(a, a+4, STRIPE_SIZE-4)==0); |
| 1311 | } |
| 1312 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 1313 | static int stripe_to_pdidx(sector_t stripe, raid5_conf_t *conf, int disks) |
| 1314 | { |
| 1315 | int sectors_per_chunk = conf->chunk_size >> 9; |
| 1316 | sector_t x = stripe; |
| 1317 | int pd_idx, dd_idx; |
| 1318 | int chunk_offset = sector_div(x, sectors_per_chunk); |
| 1319 | stripe = x; |
| 1320 | raid5_compute_sector(stripe*(disks-1)*sectors_per_chunk |
| 1321 | + chunk_offset, disks, disks-1, &dd_idx, &pd_idx, conf); |
| 1322 | return pd_idx; |
| 1323 | } |
| 1324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | |
| 1326 | /* |
| 1327 | * handle_stripe - do things to a stripe. |
| 1328 | * |
| 1329 | * We lock the stripe and then examine the state of various bits |
| 1330 | * to see what needs to be done. |
| 1331 | * Possible results: |
| 1332 | * return some read request which now have data |
| 1333 | * return some write requests which are safely on disc |
| 1334 | * schedule a read on some buffers |
| 1335 | * schedule a write of some buffers |
| 1336 | * return confirmation of parity correctness |
| 1337 | * |
| 1338 | * Parity calculations are done inside the stripe lock |
| 1339 | * buffers are taken off read_list or write_list, and bh_cache buffers |
| 1340 | * get BH_Lock set before the stripe lock is released. |
| 1341 | * |
| 1342 | */ |
| 1343 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1344 | static void handle_stripe5(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | { |
| 1346 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 1347 | int disks = sh->disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | struct bio *return_bi= NULL; |
| 1349 | struct bio *bi; |
| 1350 | int i; |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 1351 | int syncing, expanding, expanded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0; |
| 1353 | int non_overwrite = 0; |
| 1354 | int failed_num=0; |
| 1355 | struct r5dev *dev; |
| 1356 | |
| 1357 | PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n", |
| 1358 | (unsigned long long)sh->sector, atomic_read(&sh->count), |
| 1359 | sh->pd_idx); |
| 1360 | |
| 1361 | spin_lock(&sh->lock); |
| 1362 | clear_bit(STRIPE_HANDLE, &sh->state); |
| 1363 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 1364 | |
| 1365 | syncing = test_bit(STRIPE_SYNCING, &sh->state); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 1366 | expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 1367 | expanded = test_bit(STRIPE_EXPAND_READY, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | /* Now to look around and see what can be done */ |
| 1369 | |
NeilBrown | 9910f16 | 2006-01-06 00:20:24 -0800 | [diff] [blame] | 1370 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | for (i=disks; i--; ) { |
| 1372 | mdk_rdev_t *rdev; |
| 1373 | dev = &sh->dev[i]; |
| 1374 | clear_bit(R5_Insync, &dev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | |
| 1376 | PRINTK("check %d: state 0x%lx read %p write %p written %p\n", |
| 1377 | i, dev->flags, dev->toread, dev->towrite, dev->written); |
| 1378 | /* maybe we can reply to a read */ |
| 1379 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) { |
| 1380 | struct bio *rbi, *rbi2; |
| 1381 | PRINTK("Return read for disc %d\n", i); |
| 1382 | spin_lock_irq(&conf->device_lock); |
| 1383 | rbi = dev->toread; |
| 1384 | dev->toread = NULL; |
| 1385 | if (test_and_clear_bit(R5_Overlap, &dev->flags)) |
| 1386 | wake_up(&conf->wait_for_overlap); |
| 1387 | spin_unlock_irq(&conf->device_lock); |
| 1388 | while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) { |
| 1389 | copy_data(0, rbi, dev->page, dev->sector); |
| 1390 | rbi2 = r5_next_bio(rbi, dev->sector); |
| 1391 | spin_lock_irq(&conf->device_lock); |
| 1392 | if (--rbi->bi_phys_segments == 0) { |
| 1393 | rbi->bi_next = return_bi; |
| 1394 | return_bi = rbi; |
| 1395 | } |
| 1396 | spin_unlock_irq(&conf->device_lock); |
| 1397 | rbi = rbi2; |
| 1398 | } |
| 1399 | } |
| 1400 | |
| 1401 | /* now count some things */ |
| 1402 | if (test_bit(R5_LOCKED, &dev->flags)) locked++; |
| 1403 | if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++; |
| 1404 | |
| 1405 | |
| 1406 | if (dev->toread) to_read++; |
| 1407 | if (dev->towrite) { |
| 1408 | to_write++; |
| 1409 | if (!test_bit(R5_OVERWRITE, &dev->flags)) |
| 1410 | non_overwrite++; |
| 1411 | } |
| 1412 | if (dev->written) written++; |
NeilBrown | 9910f16 | 2006-01-06 00:20:24 -0800 | [diff] [blame] | 1413 | rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1414 | if (!rdev || !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 1415 | /* The ReadError flag will just be confusing now */ |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1416 | clear_bit(R5_ReadError, &dev->flags); |
| 1417 | clear_bit(R5_ReWrite, &dev->flags); |
| 1418 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1419 | if (!rdev || !test_bit(In_sync, &rdev->flags) |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1420 | || test_bit(R5_ReadError, &dev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | failed++; |
| 1422 | failed_num = i; |
| 1423 | } else |
| 1424 | set_bit(R5_Insync, &dev->flags); |
| 1425 | } |
NeilBrown | 9910f16 | 2006-01-06 00:20:24 -0800 | [diff] [blame] | 1426 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | PRINTK("locked=%d uptodate=%d to_read=%d" |
| 1428 | " to_write=%d failed=%d failed_num=%d\n", |
| 1429 | locked, uptodate, to_read, to_write, failed, failed_num); |
| 1430 | /* check if the array has lost two devices and, if so, some requests might |
| 1431 | * need to be failed |
| 1432 | */ |
| 1433 | if (failed > 1 && to_read+to_write+written) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | for (i=disks; i--; ) { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1435 | int bitmap_end = 0; |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1436 | |
| 1437 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | 9910f16 | 2006-01-06 00:20:24 -0800 | [diff] [blame] | 1438 | mdk_rdev_t *rdev; |
| 1439 | rcu_read_lock(); |
| 1440 | rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1441 | if (rdev && test_bit(In_sync, &rdev->flags)) |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1442 | /* multiple read failures in one stripe */ |
| 1443 | md_error(conf->mddev, rdev); |
NeilBrown | 9910f16 | 2006-01-06 00:20:24 -0800 | [diff] [blame] | 1444 | rcu_read_unlock(); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1445 | } |
| 1446 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1447 | spin_lock_irq(&conf->device_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | /* fail all writes first */ |
| 1449 | bi = sh->dev[i].towrite; |
| 1450 | sh->dev[i].towrite = NULL; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1451 | if (bi) { to_write--; bitmap_end = 1; } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | |
| 1453 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 1454 | wake_up(&conf->wait_for_overlap); |
| 1455 | |
| 1456 | while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){ |
| 1457 | struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); |
| 1458 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
| 1459 | if (--bi->bi_phys_segments == 0) { |
| 1460 | md_write_end(conf->mddev); |
| 1461 | bi->bi_next = return_bi; |
| 1462 | return_bi = bi; |
| 1463 | } |
| 1464 | bi = nextbi; |
| 1465 | } |
| 1466 | /* and fail all 'written' */ |
| 1467 | bi = sh->dev[i].written; |
| 1468 | sh->dev[i].written = NULL; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1469 | if (bi) bitmap_end = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) { |
| 1471 | struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector); |
| 1472 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
| 1473 | if (--bi->bi_phys_segments == 0) { |
| 1474 | md_write_end(conf->mddev); |
| 1475 | bi->bi_next = return_bi; |
| 1476 | return_bi = bi; |
| 1477 | } |
| 1478 | bi = bi2; |
| 1479 | } |
| 1480 | |
| 1481 | /* fail any reads if this device is non-operational */ |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1482 | if (!test_bit(R5_Insync, &sh->dev[i].flags) || |
| 1483 | test_bit(R5_ReadError, &sh->dev[i].flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | bi = sh->dev[i].toread; |
| 1485 | sh->dev[i].toread = NULL; |
| 1486 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 1487 | wake_up(&conf->wait_for_overlap); |
| 1488 | if (bi) to_read--; |
| 1489 | while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){ |
| 1490 | struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); |
| 1491 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
| 1492 | if (--bi->bi_phys_segments == 0) { |
| 1493 | bi->bi_next = return_bi; |
| 1494 | return_bi = bi; |
| 1495 | } |
| 1496 | bi = nextbi; |
| 1497 | } |
| 1498 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1499 | spin_unlock_irq(&conf->device_lock); |
| 1500 | if (bitmap_end) |
| 1501 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 1502 | STRIPE_SECTORS, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | } |
| 1505 | if (failed > 1 && syncing) { |
| 1506 | md_done_sync(conf->mddev, STRIPE_SECTORS,0); |
| 1507 | clear_bit(STRIPE_SYNCING, &sh->state); |
| 1508 | syncing = 0; |
| 1509 | } |
| 1510 | |
| 1511 | /* might be able to return some write requests if the parity block |
| 1512 | * is safe, or on a failed drive |
| 1513 | */ |
| 1514 | dev = &sh->dev[sh->pd_idx]; |
| 1515 | if ( written && |
| 1516 | ( (test_bit(R5_Insync, &dev->flags) && !test_bit(R5_LOCKED, &dev->flags) && |
| 1517 | test_bit(R5_UPTODATE, &dev->flags)) |
| 1518 | || (failed == 1 && failed_num == sh->pd_idx)) |
| 1519 | ) { |
| 1520 | /* any written block on an uptodate or failed drive can be returned. |
| 1521 | * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but |
| 1522 | * never LOCKED, so we don't need to test 'failed' directly. |
| 1523 | */ |
| 1524 | for (i=disks; i--; ) |
| 1525 | if (sh->dev[i].written) { |
| 1526 | dev = &sh->dev[i]; |
| 1527 | if (!test_bit(R5_LOCKED, &dev->flags) && |
| 1528 | test_bit(R5_UPTODATE, &dev->flags) ) { |
| 1529 | /* We can return any write requests */ |
| 1530 | struct bio *wbi, *wbi2; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1531 | int bitmap_end = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | PRINTK("Return write for disc %d\n", i); |
| 1533 | spin_lock_irq(&conf->device_lock); |
| 1534 | wbi = dev->written; |
| 1535 | dev->written = NULL; |
| 1536 | while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) { |
| 1537 | wbi2 = r5_next_bio(wbi, dev->sector); |
| 1538 | if (--wbi->bi_phys_segments == 0) { |
| 1539 | md_write_end(conf->mddev); |
| 1540 | wbi->bi_next = return_bi; |
| 1541 | return_bi = wbi; |
| 1542 | } |
| 1543 | wbi = wbi2; |
| 1544 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1545 | if (dev->towrite == NULL) |
| 1546 | bitmap_end = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1548 | if (bitmap_end) |
| 1549 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 1550 | STRIPE_SECTORS, |
| 1551 | !test_bit(STRIPE_DEGRADED, &sh->state), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | } |
| 1553 | } |
| 1554 | } |
| 1555 | |
| 1556 | /* Now we might consider reading some blocks, either to check/generate |
| 1557 | * parity, or to satisfy requests |
| 1558 | * or to load a block that is being partially written. |
| 1559 | */ |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 1560 | if (to_read || non_overwrite || (syncing && (uptodate < disks)) || expanding) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1561 | for (i=disks; i--;) { |
| 1562 | dev = &sh->dev[i]; |
| 1563 | if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) && |
| 1564 | (dev->toread || |
| 1565 | (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) || |
| 1566 | syncing || |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 1567 | expanding || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | (failed && (sh->dev[failed_num].toread || |
| 1569 | (sh->dev[failed_num].towrite && !test_bit(R5_OVERWRITE, &sh->dev[failed_num].flags)))) |
| 1570 | ) |
| 1571 | ) { |
| 1572 | /* we would like to get this block, possibly |
| 1573 | * by computing it, but we might not be able to |
| 1574 | */ |
| 1575 | if (uptodate == disks-1) { |
| 1576 | PRINTK("Computing block %d\n", i); |
| 1577 | compute_block(sh, i); |
| 1578 | uptodate++; |
| 1579 | } else if (test_bit(R5_Insync, &dev->flags)) { |
| 1580 | set_bit(R5_LOCKED, &dev->flags); |
| 1581 | set_bit(R5_Wantread, &dev->flags); |
| 1582 | #if 0 |
| 1583 | /* if I am just reading this block and we don't have |
| 1584 | a failed drive, or any pending writes then sidestep the cache */ |
| 1585 | if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext && |
| 1586 | ! syncing && !failed && !to_write) { |
| 1587 | sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page; |
| 1588 | sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data; |
| 1589 | } |
| 1590 | #endif |
| 1591 | locked++; |
| 1592 | PRINTK("Reading block %d (sync=%d)\n", |
| 1593 | i, syncing); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | } |
| 1595 | } |
| 1596 | } |
| 1597 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1598 | } |
| 1599 | |
| 1600 | /* now to consider writing and what else, if anything should be read */ |
| 1601 | if (to_write) { |
| 1602 | int rmw=0, rcw=0; |
| 1603 | for (i=disks ; i--;) { |
| 1604 | /* would I have to read this buffer for read_modify_write */ |
| 1605 | dev = &sh->dev[i]; |
| 1606 | if ((dev->towrite || i == sh->pd_idx) && |
| 1607 | (!test_bit(R5_LOCKED, &dev->flags) |
| 1608 | #if 0 |
| 1609 | || sh->bh_page[i]!=bh->b_page |
| 1610 | #endif |
| 1611 | ) && |
| 1612 | !test_bit(R5_UPTODATE, &dev->flags)) { |
| 1613 | if (test_bit(R5_Insync, &dev->flags) |
| 1614 | /* && !(!mddev->insync && i == sh->pd_idx) */ |
| 1615 | ) |
| 1616 | rmw++; |
| 1617 | else rmw += 2*disks; /* cannot read it */ |
| 1618 | } |
| 1619 | /* Would I have to read this buffer for reconstruct_write */ |
| 1620 | if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx && |
| 1621 | (!test_bit(R5_LOCKED, &dev->flags) |
| 1622 | #if 0 |
| 1623 | || sh->bh_page[i] != bh->b_page |
| 1624 | #endif |
| 1625 | ) && |
| 1626 | !test_bit(R5_UPTODATE, &dev->flags)) { |
| 1627 | if (test_bit(R5_Insync, &dev->flags)) rcw++; |
| 1628 | else rcw += 2*disks; |
| 1629 | } |
| 1630 | } |
| 1631 | PRINTK("for sector %llu, rmw=%d rcw=%d\n", |
| 1632 | (unsigned long long)sh->sector, rmw, rcw); |
| 1633 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1634 | if (rmw < rcw && rmw > 0) |
| 1635 | /* prefer read-modify-write, but need to get some data */ |
| 1636 | for (i=disks; i--;) { |
| 1637 | dev = &sh->dev[i]; |
| 1638 | if ((dev->towrite || i == sh->pd_idx) && |
| 1639 | !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) && |
| 1640 | test_bit(R5_Insync, &dev->flags)) { |
| 1641 | if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 1642 | { |
| 1643 | PRINTK("Read_old block %d for r-m-w\n", i); |
| 1644 | set_bit(R5_LOCKED, &dev->flags); |
| 1645 | set_bit(R5_Wantread, &dev->flags); |
| 1646 | locked++; |
| 1647 | } else { |
| 1648 | set_bit(STRIPE_DELAYED, &sh->state); |
| 1649 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1650 | } |
| 1651 | } |
| 1652 | } |
| 1653 | if (rcw <= rmw && rcw > 0) |
| 1654 | /* want reconstruct write, but need to get some data */ |
| 1655 | for (i=disks; i--;) { |
| 1656 | dev = &sh->dev[i]; |
| 1657 | if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx && |
| 1658 | !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) && |
| 1659 | test_bit(R5_Insync, &dev->flags)) { |
| 1660 | if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 1661 | { |
| 1662 | PRINTK("Read_old block %d for Reconstruct\n", i); |
| 1663 | set_bit(R5_LOCKED, &dev->flags); |
| 1664 | set_bit(R5_Wantread, &dev->flags); |
| 1665 | locked++; |
| 1666 | } else { |
| 1667 | set_bit(STRIPE_DELAYED, &sh->state); |
| 1668 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1669 | } |
| 1670 | } |
| 1671 | } |
| 1672 | /* now if nothing is locked, and if we have enough data, we can start a write request */ |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1673 | if (locked == 0 && (rcw == 0 ||rmw == 0) && |
| 1674 | !test_bit(STRIPE_BIT_DELAY, &sh->state)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | PRINTK("Computing parity...\n"); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1676 | compute_parity5(sh, rcw==0 ? RECONSTRUCT_WRITE : READ_MODIFY_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | /* now every locked buffer is ready to be written */ |
| 1678 | for (i=disks; i--;) |
| 1679 | if (test_bit(R5_LOCKED, &sh->dev[i].flags)) { |
| 1680 | PRINTK("Writing block %d\n", i); |
| 1681 | locked++; |
| 1682 | set_bit(R5_Wantwrite, &sh->dev[i].flags); |
| 1683 | if (!test_bit(R5_Insync, &sh->dev[i].flags) |
| 1684 | || (i==sh->pd_idx && failed == 0)) |
| 1685 | set_bit(STRIPE_INSYNC, &sh->state); |
| 1686 | } |
| 1687 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
| 1688 | atomic_dec(&conf->preread_active_stripes); |
| 1689 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) |
| 1690 | md_wakeup_thread(conf->mddev->thread); |
| 1691 | } |
| 1692 | } |
| 1693 | } |
| 1694 | |
| 1695 | /* maybe we need to check and possibly fix the parity for this stripe |
| 1696 | * Any reads will already have been scheduled, so we just see if enough data |
| 1697 | * is available |
| 1698 | */ |
| 1699 | if (syncing && locked == 0 && |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 1700 | !test_bit(STRIPE_INSYNC, &sh->state)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1702 | if (failed == 0) { |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 1703 | BUG_ON(uptodate != disks); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1704 | compute_parity5(sh, CHECK_PARITY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | uptodate--; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1706 | if (page_is_zero(sh->dev[sh->pd_idx].page)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | /* parity is correct (on disc, not in buffer any more) */ |
| 1708 | set_bit(STRIPE_INSYNC, &sh->state); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 1709 | } else { |
| 1710 | conf->mddev->resync_mismatches += STRIPE_SECTORS; |
| 1711 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 1712 | /* don't try to repair!! */ |
| 1713 | set_bit(STRIPE_INSYNC, &sh->state); |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 1714 | else { |
| 1715 | compute_block(sh, sh->pd_idx); |
| 1716 | uptodate++; |
| 1717 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | } |
| 1719 | } |
| 1720 | if (!test_bit(STRIPE_INSYNC, &sh->state)) { |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 1721 | /* either failed parity check, or recovery is happening */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 | if (failed==0) |
| 1723 | failed_num = sh->pd_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | dev = &sh->dev[failed_num]; |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 1725 | BUG_ON(!test_bit(R5_UPTODATE, &dev->flags)); |
| 1726 | BUG_ON(uptodate != disks); |
| 1727 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1728 | set_bit(R5_LOCKED, &dev->flags); |
| 1729 | set_bit(R5_Wantwrite, &dev->flags); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1730 | clear_bit(STRIPE_DEGRADED, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | locked++; |
| 1732 | set_bit(STRIPE_INSYNC, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | } |
| 1734 | } |
| 1735 | if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) { |
| 1736 | md_done_sync(conf->mddev, STRIPE_SECTORS,1); |
| 1737 | clear_bit(STRIPE_SYNCING, &sh->state); |
| 1738 | } |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1739 | |
| 1740 | /* If the failed drive is just a ReadError, then we might need to progress |
| 1741 | * the repair/check process |
| 1742 | */ |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1743 | if (failed == 1 && ! conf->mddev->ro && |
| 1744 | test_bit(R5_ReadError, &sh->dev[failed_num].flags) |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1745 | && !test_bit(R5_LOCKED, &sh->dev[failed_num].flags) |
| 1746 | && test_bit(R5_UPTODATE, &sh->dev[failed_num].flags) |
| 1747 | ) { |
| 1748 | dev = &sh->dev[failed_num]; |
| 1749 | if (!test_bit(R5_ReWrite, &dev->flags)) { |
| 1750 | set_bit(R5_Wantwrite, &dev->flags); |
| 1751 | set_bit(R5_ReWrite, &dev->flags); |
| 1752 | set_bit(R5_LOCKED, &dev->flags); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 1753 | locked++; |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1754 | } else { |
| 1755 | /* let's read it back */ |
| 1756 | set_bit(R5_Wantread, &dev->flags); |
| 1757 | set_bit(R5_LOCKED, &dev->flags); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 1758 | locked++; |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1759 | } |
| 1760 | } |
| 1761 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 1762 | if (expanded && test_bit(STRIPE_EXPANDING, &sh->state)) { |
| 1763 | /* Need to write out all blocks after computing parity */ |
| 1764 | sh->disks = conf->raid_disks; |
| 1765 | sh->pd_idx = stripe_to_pdidx(sh->sector, conf, conf->raid_disks); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1766 | compute_parity5(sh, RECONSTRUCT_WRITE); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 1767 | for (i= conf->raid_disks; i--;) { |
| 1768 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 1769 | locked++; |
| 1770 | set_bit(R5_Wantwrite, &sh->dev[i].flags); |
| 1771 | } |
| 1772 | clear_bit(STRIPE_EXPANDING, &sh->state); |
| 1773 | } else if (expanded) { |
| 1774 | clear_bit(STRIPE_EXPAND_READY, &sh->state); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1775 | atomic_dec(&conf->reshape_stripes); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 1776 | wake_up(&conf->wait_for_overlap); |
| 1777 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 1778 | } |
| 1779 | |
| 1780 | if (expanding && locked == 0) { |
| 1781 | /* We have read all the blocks in this stripe and now we need to |
| 1782 | * copy some of them into a target stripe for expand. |
| 1783 | */ |
| 1784 | clear_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 1785 | for (i=0; i< sh->disks; i++) |
| 1786 | if (i != sh->pd_idx) { |
| 1787 | int dd_idx, pd_idx, j; |
| 1788 | struct stripe_head *sh2; |
| 1789 | |
| 1790 | sector_t bn = compute_blocknr(sh, i); |
| 1791 | sector_t s = raid5_compute_sector(bn, conf->raid_disks, |
| 1792 | conf->raid_disks-1, |
| 1793 | &dd_idx, &pd_idx, conf); |
| 1794 | sh2 = get_active_stripe(conf, s, conf->raid_disks, pd_idx, 1); |
| 1795 | if (sh2 == NULL) |
| 1796 | /* so far only the early blocks of this stripe |
| 1797 | * have been requested. When later blocks |
| 1798 | * get requested, we will try again |
| 1799 | */ |
| 1800 | continue; |
| 1801 | if(!test_bit(STRIPE_EXPANDING, &sh2->state) || |
| 1802 | test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) { |
| 1803 | /* must have already done this block */ |
| 1804 | release_stripe(sh2); |
| 1805 | continue; |
| 1806 | } |
| 1807 | memcpy(page_address(sh2->dev[dd_idx].page), |
| 1808 | page_address(sh->dev[i].page), |
| 1809 | STRIPE_SIZE); |
| 1810 | set_bit(R5_Expanded, &sh2->dev[dd_idx].flags); |
| 1811 | set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags); |
| 1812 | for (j=0; j<conf->raid_disks; j++) |
| 1813 | if (j != sh2->pd_idx && |
| 1814 | !test_bit(R5_Expanded, &sh2->dev[j].flags)) |
| 1815 | break; |
| 1816 | if (j == conf->raid_disks) { |
| 1817 | set_bit(STRIPE_EXPAND_READY, &sh2->state); |
| 1818 | set_bit(STRIPE_HANDLE, &sh2->state); |
| 1819 | } |
| 1820 | release_stripe(sh2); |
| 1821 | } |
| 1822 | } |
| 1823 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | spin_unlock(&sh->lock); |
| 1825 | |
| 1826 | while ((bi=return_bi)) { |
| 1827 | int bytes = bi->bi_size; |
| 1828 | |
| 1829 | return_bi = bi->bi_next; |
| 1830 | bi->bi_next = NULL; |
| 1831 | bi->bi_size = 0; |
| 1832 | bi->bi_end_io(bi, bytes, 0); |
| 1833 | } |
| 1834 | for (i=disks; i-- ;) { |
| 1835 | int rw; |
| 1836 | struct bio *bi; |
| 1837 | mdk_rdev_t *rdev; |
| 1838 | if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) |
| 1839 | rw = 1; |
| 1840 | else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags)) |
| 1841 | rw = 0; |
| 1842 | else |
| 1843 | continue; |
| 1844 | |
| 1845 | bi = &sh->dev[i].req; |
| 1846 | |
| 1847 | bi->bi_rw = rw; |
| 1848 | if (rw) |
| 1849 | bi->bi_end_io = raid5_end_write_request; |
| 1850 | else |
| 1851 | bi->bi_end_io = raid5_end_read_request; |
| 1852 | |
| 1853 | rcu_read_lock(); |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 1854 | rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1855 | if (rdev && test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | rdev = NULL; |
| 1857 | if (rdev) |
| 1858 | atomic_inc(&rdev->nr_pending); |
| 1859 | rcu_read_unlock(); |
| 1860 | |
| 1861 | if (rdev) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 1862 | if (syncing || expanding || expanded) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1863 | md_sync_acct(rdev->bdev, STRIPE_SECTORS); |
| 1864 | |
| 1865 | bi->bi_bdev = rdev->bdev; |
| 1866 | PRINTK("for %llu schedule op %ld on disc %d\n", |
| 1867 | (unsigned long long)sh->sector, bi->bi_rw, i); |
| 1868 | atomic_inc(&sh->count); |
| 1869 | bi->bi_sector = sh->sector + rdev->data_offset; |
| 1870 | bi->bi_flags = 1 << BIO_UPTODATE; |
| 1871 | bi->bi_vcnt = 1; |
| 1872 | bi->bi_max_vecs = 1; |
| 1873 | bi->bi_idx = 0; |
| 1874 | bi->bi_io_vec = &sh->dev[i].vec; |
| 1875 | bi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
| 1876 | bi->bi_io_vec[0].bv_offset = 0; |
| 1877 | bi->bi_size = STRIPE_SIZE; |
| 1878 | bi->bi_next = NULL; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1879 | if (rw == WRITE && |
| 1880 | test_bit(R5_ReWrite, &sh->dev[i].flags)) |
| 1881 | atomic_add(STRIPE_SECTORS, &rdev->corrected_errors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 | generic_make_request(bi); |
| 1883 | } else { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1884 | if (rw == 1) |
| 1885 | set_bit(STRIPE_DEGRADED, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1886 | PRINTK("skip op %ld on disc %d for sector %llu\n", |
| 1887 | bi->bi_rw, i, (unsigned long long)sh->sector); |
| 1888 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 1889 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1890 | } |
| 1891 | } |
| 1892 | } |
| 1893 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1894 | static void handle_stripe6(struct stripe_head *sh, struct page *tmp_page) |
| 1895 | { |
| 1896 | raid6_conf_t *conf = sh->raid_conf; |
| 1897 | int disks = conf->raid_disks; |
| 1898 | struct bio *return_bi= NULL; |
| 1899 | struct bio *bi; |
| 1900 | int i; |
| 1901 | int syncing; |
| 1902 | int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0; |
| 1903 | int non_overwrite = 0; |
| 1904 | int failed_num[2] = {0, 0}; |
| 1905 | struct r5dev *dev, *pdev, *qdev; |
| 1906 | int pd_idx = sh->pd_idx; |
| 1907 | int qd_idx = raid6_next_disk(pd_idx, disks); |
| 1908 | int p_failed, q_failed; |
| 1909 | |
| 1910 | PRINTK("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d, qd_idx=%d\n", |
| 1911 | (unsigned long long)sh->sector, sh->state, atomic_read(&sh->count), |
| 1912 | pd_idx, qd_idx); |
| 1913 | |
| 1914 | spin_lock(&sh->lock); |
| 1915 | clear_bit(STRIPE_HANDLE, &sh->state); |
| 1916 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 1917 | |
| 1918 | syncing = test_bit(STRIPE_SYNCING, &sh->state); |
| 1919 | /* Now to look around and see what can be done */ |
| 1920 | |
| 1921 | rcu_read_lock(); |
| 1922 | for (i=disks; i--; ) { |
| 1923 | mdk_rdev_t *rdev; |
| 1924 | dev = &sh->dev[i]; |
| 1925 | clear_bit(R5_Insync, &dev->flags); |
| 1926 | |
| 1927 | PRINTK("check %d: state 0x%lx read %p write %p written %p\n", |
| 1928 | i, dev->flags, dev->toread, dev->towrite, dev->written); |
| 1929 | /* maybe we can reply to a read */ |
| 1930 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) { |
| 1931 | struct bio *rbi, *rbi2; |
| 1932 | PRINTK("Return read for disc %d\n", i); |
| 1933 | spin_lock_irq(&conf->device_lock); |
| 1934 | rbi = dev->toread; |
| 1935 | dev->toread = NULL; |
| 1936 | if (test_and_clear_bit(R5_Overlap, &dev->flags)) |
| 1937 | wake_up(&conf->wait_for_overlap); |
| 1938 | spin_unlock_irq(&conf->device_lock); |
| 1939 | while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) { |
| 1940 | copy_data(0, rbi, dev->page, dev->sector); |
| 1941 | rbi2 = r5_next_bio(rbi, dev->sector); |
| 1942 | spin_lock_irq(&conf->device_lock); |
| 1943 | if (--rbi->bi_phys_segments == 0) { |
| 1944 | rbi->bi_next = return_bi; |
| 1945 | return_bi = rbi; |
| 1946 | } |
| 1947 | spin_unlock_irq(&conf->device_lock); |
| 1948 | rbi = rbi2; |
| 1949 | } |
| 1950 | } |
| 1951 | |
| 1952 | /* now count some things */ |
| 1953 | if (test_bit(R5_LOCKED, &dev->flags)) locked++; |
| 1954 | if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++; |
| 1955 | |
| 1956 | |
| 1957 | if (dev->toread) to_read++; |
| 1958 | if (dev->towrite) { |
| 1959 | to_write++; |
| 1960 | if (!test_bit(R5_OVERWRITE, &dev->flags)) |
| 1961 | non_overwrite++; |
| 1962 | } |
| 1963 | if (dev->written) written++; |
| 1964 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 1965 | if (!rdev || !test_bit(In_sync, &rdev->flags)) { |
| 1966 | /* The ReadError flag will just be confusing now */ |
| 1967 | clear_bit(R5_ReadError, &dev->flags); |
| 1968 | clear_bit(R5_ReWrite, &dev->flags); |
| 1969 | } |
| 1970 | if (!rdev || !test_bit(In_sync, &rdev->flags) |
| 1971 | || test_bit(R5_ReadError, &dev->flags)) { |
| 1972 | if ( failed < 2 ) |
| 1973 | failed_num[failed] = i; |
| 1974 | failed++; |
| 1975 | } else |
| 1976 | set_bit(R5_Insync, &dev->flags); |
| 1977 | } |
| 1978 | rcu_read_unlock(); |
| 1979 | PRINTK("locked=%d uptodate=%d to_read=%d" |
| 1980 | " to_write=%d failed=%d failed_num=%d,%d\n", |
| 1981 | locked, uptodate, to_read, to_write, failed, |
| 1982 | failed_num[0], failed_num[1]); |
| 1983 | /* check if the array has lost >2 devices and, if so, some requests might |
| 1984 | * need to be failed |
| 1985 | */ |
| 1986 | if (failed > 2 && to_read+to_write+written) { |
| 1987 | for (i=disks; i--; ) { |
| 1988 | int bitmap_end = 0; |
| 1989 | |
| 1990 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
| 1991 | mdk_rdev_t *rdev; |
| 1992 | rcu_read_lock(); |
| 1993 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 1994 | if (rdev && test_bit(In_sync, &rdev->flags)) |
| 1995 | /* multiple read failures in one stripe */ |
| 1996 | md_error(conf->mddev, rdev); |
| 1997 | rcu_read_unlock(); |
| 1998 | } |
| 1999 | |
| 2000 | spin_lock_irq(&conf->device_lock); |
| 2001 | /* fail all writes first */ |
| 2002 | bi = sh->dev[i].towrite; |
| 2003 | sh->dev[i].towrite = NULL; |
| 2004 | if (bi) { to_write--; bitmap_end = 1; } |
| 2005 | |
| 2006 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 2007 | wake_up(&conf->wait_for_overlap); |
| 2008 | |
| 2009 | while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){ |
| 2010 | struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); |
| 2011 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
| 2012 | if (--bi->bi_phys_segments == 0) { |
| 2013 | md_write_end(conf->mddev); |
| 2014 | bi->bi_next = return_bi; |
| 2015 | return_bi = bi; |
| 2016 | } |
| 2017 | bi = nextbi; |
| 2018 | } |
| 2019 | /* and fail all 'written' */ |
| 2020 | bi = sh->dev[i].written; |
| 2021 | sh->dev[i].written = NULL; |
| 2022 | if (bi) bitmap_end = 1; |
| 2023 | while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) { |
| 2024 | struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector); |
| 2025 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
| 2026 | if (--bi->bi_phys_segments == 0) { |
| 2027 | md_write_end(conf->mddev); |
| 2028 | bi->bi_next = return_bi; |
| 2029 | return_bi = bi; |
| 2030 | } |
| 2031 | bi = bi2; |
| 2032 | } |
| 2033 | |
| 2034 | /* fail any reads if this device is non-operational */ |
| 2035 | if (!test_bit(R5_Insync, &sh->dev[i].flags) || |
| 2036 | test_bit(R5_ReadError, &sh->dev[i].flags)) { |
| 2037 | bi = sh->dev[i].toread; |
| 2038 | sh->dev[i].toread = NULL; |
| 2039 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 2040 | wake_up(&conf->wait_for_overlap); |
| 2041 | if (bi) to_read--; |
| 2042 | while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){ |
| 2043 | struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); |
| 2044 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
| 2045 | if (--bi->bi_phys_segments == 0) { |
| 2046 | bi->bi_next = return_bi; |
| 2047 | return_bi = bi; |
| 2048 | } |
| 2049 | bi = nextbi; |
| 2050 | } |
| 2051 | } |
| 2052 | spin_unlock_irq(&conf->device_lock); |
| 2053 | if (bitmap_end) |
| 2054 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 2055 | STRIPE_SECTORS, 0, 0); |
| 2056 | } |
| 2057 | } |
| 2058 | if (failed > 2 && syncing) { |
| 2059 | md_done_sync(conf->mddev, STRIPE_SECTORS,0); |
| 2060 | clear_bit(STRIPE_SYNCING, &sh->state); |
| 2061 | syncing = 0; |
| 2062 | } |
| 2063 | |
| 2064 | /* |
| 2065 | * might be able to return some write requests if the parity blocks |
| 2066 | * are safe, or on a failed drive |
| 2067 | */ |
| 2068 | pdev = &sh->dev[pd_idx]; |
| 2069 | p_failed = (failed >= 1 && failed_num[0] == pd_idx) |
| 2070 | || (failed >= 2 && failed_num[1] == pd_idx); |
| 2071 | qdev = &sh->dev[qd_idx]; |
| 2072 | q_failed = (failed >= 1 && failed_num[0] == qd_idx) |
| 2073 | || (failed >= 2 && failed_num[1] == qd_idx); |
| 2074 | |
| 2075 | if ( written && |
| 2076 | ( p_failed || ((test_bit(R5_Insync, &pdev->flags) |
| 2077 | && !test_bit(R5_LOCKED, &pdev->flags) |
| 2078 | && test_bit(R5_UPTODATE, &pdev->flags))) ) && |
| 2079 | ( q_failed || ((test_bit(R5_Insync, &qdev->flags) |
| 2080 | && !test_bit(R5_LOCKED, &qdev->flags) |
| 2081 | && test_bit(R5_UPTODATE, &qdev->flags))) ) ) { |
| 2082 | /* any written block on an uptodate or failed drive can be |
| 2083 | * returned. Note that if we 'wrote' to a failed drive, |
| 2084 | * it will be UPTODATE, but never LOCKED, so we don't need |
| 2085 | * to test 'failed' directly. |
| 2086 | */ |
| 2087 | for (i=disks; i--; ) |
| 2088 | if (sh->dev[i].written) { |
| 2089 | dev = &sh->dev[i]; |
| 2090 | if (!test_bit(R5_LOCKED, &dev->flags) && |
| 2091 | test_bit(R5_UPTODATE, &dev->flags) ) { |
| 2092 | /* We can return any write requests */ |
| 2093 | int bitmap_end = 0; |
| 2094 | struct bio *wbi, *wbi2; |
| 2095 | PRINTK("Return write for stripe %llu disc %d\n", |
| 2096 | (unsigned long long)sh->sector, i); |
| 2097 | spin_lock_irq(&conf->device_lock); |
| 2098 | wbi = dev->written; |
| 2099 | dev->written = NULL; |
| 2100 | while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) { |
| 2101 | wbi2 = r5_next_bio(wbi, dev->sector); |
| 2102 | if (--wbi->bi_phys_segments == 0) { |
| 2103 | md_write_end(conf->mddev); |
| 2104 | wbi->bi_next = return_bi; |
| 2105 | return_bi = wbi; |
| 2106 | } |
| 2107 | wbi = wbi2; |
| 2108 | } |
| 2109 | if (dev->towrite == NULL) |
| 2110 | bitmap_end = 1; |
| 2111 | spin_unlock_irq(&conf->device_lock); |
| 2112 | if (bitmap_end) |
| 2113 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 2114 | STRIPE_SECTORS, |
| 2115 | !test_bit(STRIPE_DEGRADED, &sh->state), 0); |
| 2116 | } |
| 2117 | } |
| 2118 | } |
| 2119 | |
| 2120 | /* Now we might consider reading some blocks, either to check/generate |
| 2121 | * parity, or to satisfy requests |
| 2122 | * or to load a block that is being partially written. |
| 2123 | */ |
| 2124 | if (to_read || non_overwrite || (to_write && failed) || (syncing && (uptodate < disks))) { |
| 2125 | for (i=disks; i--;) { |
| 2126 | dev = &sh->dev[i]; |
| 2127 | if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) && |
| 2128 | (dev->toread || |
| 2129 | (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) || |
| 2130 | syncing || |
| 2131 | (failed >= 1 && (sh->dev[failed_num[0]].toread || to_write)) || |
| 2132 | (failed >= 2 && (sh->dev[failed_num[1]].toread || to_write)) |
| 2133 | ) |
| 2134 | ) { |
| 2135 | /* we would like to get this block, possibly |
| 2136 | * by computing it, but we might not be able to |
| 2137 | */ |
| 2138 | if (uptodate == disks-1) { |
| 2139 | PRINTK("Computing stripe %llu block %d\n", |
| 2140 | (unsigned long long)sh->sector, i); |
| 2141 | compute_block_1(sh, i, 0); |
| 2142 | uptodate++; |
| 2143 | } else if ( uptodate == disks-2 && failed >= 2 ) { |
| 2144 | /* Computing 2-failure is *very* expensive; only do it if failed >= 2 */ |
| 2145 | int other; |
| 2146 | for (other=disks; other--;) { |
| 2147 | if ( other == i ) |
| 2148 | continue; |
| 2149 | if ( !test_bit(R5_UPTODATE, &sh->dev[other].flags) ) |
| 2150 | break; |
| 2151 | } |
| 2152 | BUG_ON(other < 0); |
| 2153 | PRINTK("Computing stripe %llu blocks %d,%d\n", |
| 2154 | (unsigned long long)sh->sector, i, other); |
| 2155 | compute_block_2(sh, i, other); |
| 2156 | uptodate += 2; |
| 2157 | } else if (test_bit(R5_Insync, &dev->flags)) { |
| 2158 | set_bit(R5_LOCKED, &dev->flags); |
| 2159 | set_bit(R5_Wantread, &dev->flags); |
| 2160 | #if 0 |
| 2161 | /* if I am just reading this block and we don't have |
| 2162 | a failed drive, or any pending writes then sidestep the cache */ |
| 2163 | if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext && |
| 2164 | ! syncing && !failed && !to_write) { |
| 2165 | sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page; |
| 2166 | sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data; |
| 2167 | } |
| 2168 | #endif |
| 2169 | locked++; |
| 2170 | PRINTK("Reading block %d (sync=%d)\n", |
| 2171 | i, syncing); |
| 2172 | } |
| 2173 | } |
| 2174 | } |
| 2175 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2176 | } |
| 2177 | |
| 2178 | /* now to consider writing and what else, if anything should be read */ |
| 2179 | if (to_write) { |
| 2180 | int rcw=0, must_compute=0; |
| 2181 | for (i=disks ; i--;) { |
| 2182 | dev = &sh->dev[i]; |
| 2183 | /* Would I have to read this buffer for reconstruct_write */ |
| 2184 | if (!test_bit(R5_OVERWRITE, &dev->flags) |
| 2185 | && i != pd_idx && i != qd_idx |
| 2186 | && (!test_bit(R5_LOCKED, &dev->flags) |
| 2187 | #if 0 |
| 2188 | || sh->bh_page[i] != bh->b_page |
| 2189 | #endif |
| 2190 | ) && |
| 2191 | !test_bit(R5_UPTODATE, &dev->flags)) { |
| 2192 | if (test_bit(R5_Insync, &dev->flags)) rcw++; |
| 2193 | else { |
| 2194 | PRINTK("raid6: must_compute: disk %d flags=%#lx\n", i, dev->flags); |
| 2195 | must_compute++; |
| 2196 | } |
| 2197 | } |
| 2198 | } |
| 2199 | PRINTK("for sector %llu, rcw=%d, must_compute=%d\n", |
| 2200 | (unsigned long long)sh->sector, rcw, must_compute); |
| 2201 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2202 | |
| 2203 | if (rcw > 0) |
| 2204 | /* want reconstruct write, but need to get some data */ |
| 2205 | for (i=disks; i--;) { |
| 2206 | dev = &sh->dev[i]; |
| 2207 | if (!test_bit(R5_OVERWRITE, &dev->flags) |
| 2208 | && !(failed == 0 && (i == pd_idx || i == qd_idx)) |
| 2209 | && !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) && |
| 2210 | test_bit(R5_Insync, &dev->flags)) { |
| 2211 | if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 2212 | { |
| 2213 | PRINTK("Read_old stripe %llu block %d for Reconstruct\n", |
| 2214 | (unsigned long long)sh->sector, i); |
| 2215 | set_bit(R5_LOCKED, &dev->flags); |
| 2216 | set_bit(R5_Wantread, &dev->flags); |
| 2217 | locked++; |
| 2218 | } else { |
| 2219 | PRINTK("Request delayed stripe %llu block %d for Reconstruct\n", |
| 2220 | (unsigned long long)sh->sector, i); |
| 2221 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2222 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2223 | } |
| 2224 | } |
| 2225 | } |
| 2226 | /* now if nothing is locked, and if we have enough data, we can start a write request */ |
| 2227 | if (locked == 0 && rcw == 0 && |
| 2228 | !test_bit(STRIPE_BIT_DELAY, &sh->state)) { |
| 2229 | if ( must_compute > 0 ) { |
| 2230 | /* We have failed blocks and need to compute them */ |
| 2231 | switch ( failed ) { |
| 2232 | case 0: BUG(); |
| 2233 | case 1: compute_block_1(sh, failed_num[0], 0); break; |
| 2234 | case 2: compute_block_2(sh, failed_num[0], failed_num[1]); break; |
| 2235 | default: BUG(); /* This request should have been failed? */ |
| 2236 | } |
| 2237 | } |
| 2238 | |
| 2239 | PRINTK("Computing parity for stripe %llu\n", (unsigned long long)sh->sector); |
| 2240 | compute_parity6(sh, RECONSTRUCT_WRITE); |
| 2241 | /* now every locked buffer is ready to be written */ |
| 2242 | for (i=disks; i--;) |
| 2243 | if (test_bit(R5_LOCKED, &sh->dev[i].flags)) { |
| 2244 | PRINTK("Writing stripe %llu block %d\n", |
| 2245 | (unsigned long long)sh->sector, i); |
| 2246 | locked++; |
| 2247 | set_bit(R5_Wantwrite, &sh->dev[i].flags); |
| 2248 | } |
| 2249 | /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */ |
| 2250 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2251 | |
| 2252 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
| 2253 | atomic_dec(&conf->preread_active_stripes); |
| 2254 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) |
| 2255 | md_wakeup_thread(conf->mddev->thread); |
| 2256 | } |
| 2257 | } |
| 2258 | } |
| 2259 | |
| 2260 | /* maybe we need to check and possibly fix the parity for this stripe |
| 2261 | * Any reads will already have been scheduled, so we just see if enough data |
| 2262 | * is available |
| 2263 | */ |
| 2264 | if (syncing && locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state)) { |
| 2265 | int update_p = 0, update_q = 0; |
| 2266 | struct r5dev *dev; |
| 2267 | |
| 2268 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2269 | |
| 2270 | BUG_ON(failed>2); |
| 2271 | BUG_ON(uptodate < disks); |
| 2272 | /* Want to check and possibly repair P and Q. |
| 2273 | * However there could be one 'failed' device, in which |
| 2274 | * case we can only check one of them, possibly using the |
| 2275 | * other to generate missing data |
| 2276 | */ |
| 2277 | |
| 2278 | /* If !tmp_page, we cannot do the calculations, |
| 2279 | * but as we have set STRIPE_HANDLE, we will soon be called |
| 2280 | * by stripe_handle with a tmp_page - just wait until then. |
| 2281 | */ |
| 2282 | if (tmp_page) { |
| 2283 | if (failed == q_failed) { |
| 2284 | /* The only possible failed device holds 'Q', so it makes |
| 2285 | * sense to check P (If anything else were failed, we would |
| 2286 | * have used P to recreate it). |
| 2287 | */ |
| 2288 | compute_block_1(sh, pd_idx, 1); |
| 2289 | if (!page_is_zero(sh->dev[pd_idx].page)) { |
| 2290 | compute_block_1(sh,pd_idx,0); |
| 2291 | update_p = 1; |
| 2292 | } |
| 2293 | } |
| 2294 | if (!q_failed && failed < 2) { |
| 2295 | /* q is not failed, and we didn't use it to generate |
| 2296 | * anything, so it makes sense to check it |
| 2297 | */ |
| 2298 | memcpy(page_address(tmp_page), |
| 2299 | page_address(sh->dev[qd_idx].page), |
| 2300 | STRIPE_SIZE); |
| 2301 | compute_parity6(sh, UPDATE_PARITY); |
| 2302 | if (memcmp(page_address(tmp_page), |
| 2303 | page_address(sh->dev[qd_idx].page), |
| 2304 | STRIPE_SIZE)!= 0) { |
| 2305 | clear_bit(STRIPE_INSYNC, &sh->state); |
| 2306 | update_q = 1; |
| 2307 | } |
| 2308 | } |
| 2309 | if (update_p || update_q) { |
| 2310 | conf->mddev->resync_mismatches += STRIPE_SECTORS; |
| 2311 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 2312 | /* don't try to repair!! */ |
| 2313 | update_p = update_q = 0; |
| 2314 | } |
| 2315 | |
| 2316 | /* now write out any block on a failed drive, |
| 2317 | * or P or Q if they need it |
| 2318 | */ |
| 2319 | |
| 2320 | if (failed == 2) { |
| 2321 | dev = &sh->dev[failed_num[1]]; |
| 2322 | locked++; |
| 2323 | set_bit(R5_LOCKED, &dev->flags); |
| 2324 | set_bit(R5_Wantwrite, &dev->flags); |
| 2325 | } |
| 2326 | if (failed >= 1) { |
| 2327 | dev = &sh->dev[failed_num[0]]; |
| 2328 | locked++; |
| 2329 | set_bit(R5_LOCKED, &dev->flags); |
| 2330 | set_bit(R5_Wantwrite, &dev->flags); |
| 2331 | } |
| 2332 | |
| 2333 | if (update_p) { |
| 2334 | dev = &sh->dev[pd_idx]; |
| 2335 | locked ++; |
| 2336 | set_bit(R5_LOCKED, &dev->flags); |
| 2337 | set_bit(R5_Wantwrite, &dev->flags); |
| 2338 | } |
| 2339 | if (update_q) { |
| 2340 | dev = &sh->dev[qd_idx]; |
| 2341 | locked++; |
| 2342 | set_bit(R5_LOCKED, &dev->flags); |
| 2343 | set_bit(R5_Wantwrite, &dev->flags); |
| 2344 | } |
| 2345 | clear_bit(STRIPE_DEGRADED, &sh->state); |
| 2346 | |
| 2347 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2348 | } |
| 2349 | } |
| 2350 | |
| 2351 | if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) { |
| 2352 | md_done_sync(conf->mddev, STRIPE_SECTORS,1); |
| 2353 | clear_bit(STRIPE_SYNCING, &sh->state); |
| 2354 | } |
| 2355 | |
| 2356 | /* If the failed drives are just a ReadError, then we might need |
| 2357 | * to progress the repair/check process |
| 2358 | */ |
| 2359 | if (failed <= 2 && ! conf->mddev->ro) |
| 2360 | for (i=0; i<failed;i++) { |
| 2361 | dev = &sh->dev[failed_num[i]]; |
| 2362 | if (test_bit(R5_ReadError, &dev->flags) |
| 2363 | && !test_bit(R5_LOCKED, &dev->flags) |
| 2364 | && test_bit(R5_UPTODATE, &dev->flags) |
| 2365 | ) { |
| 2366 | if (!test_bit(R5_ReWrite, &dev->flags)) { |
| 2367 | set_bit(R5_Wantwrite, &dev->flags); |
| 2368 | set_bit(R5_ReWrite, &dev->flags); |
| 2369 | set_bit(R5_LOCKED, &dev->flags); |
| 2370 | } else { |
| 2371 | /* let's read it back */ |
| 2372 | set_bit(R5_Wantread, &dev->flags); |
| 2373 | set_bit(R5_LOCKED, &dev->flags); |
| 2374 | } |
| 2375 | } |
| 2376 | } |
| 2377 | spin_unlock(&sh->lock); |
| 2378 | |
| 2379 | while ((bi=return_bi)) { |
| 2380 | int bytes = bi->bi_size; |
| 2381 | |
| 2382 | return_bi = bi->bi_next; |
| 2383 | bi->bi_next = NULL; |
| 2384 | bi->bi_size = 0; |
| 2385 | bi->bi_end_io(bi, bytes, 0); |
| 2386 | } |
| 2387 | for (i=disks; i-- ;) { |
| 2388 | int rw; |
| 2389 | struct bio *bi; |
| 2390 | mdk_rdev_t *rdev; |
| 2391 | if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) |
| 2392 | rw = 1; |
| 2393 | else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags)) |
| 2394 | rw = 0; |
| 2395 | else |
| 2396 | continue; |
| 2397 | |
| 2398 | bi = &sh->dev[i].req; |
| 2399 | |
| 2400 | bi->bi_rw = rw; |
| 2401 | if (rw) |
| 2402 | bi->bi_end_io = raid5_end_write_request; |
| 2403 | else |
| 2404 | bi->bi_end_io = raid5_end_read_request; |
| 2405 | |
| 2406 | rcu_read_lock(); |
| 2407 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 2408 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 2409 | rdev = NULL; |
| 2410 | if (rdev) |
| 2411 | atomic_inc(&rdev->nr_pending); |
| 2412 | rcu_read_unlock(); |
| 2413 | |
| 2414 | if (rdev) { |
| 2415 | if (syncing) |
| 2416 | md_sync_acct(rdev->bdev, STRIPE_SECTORS); |
| 2417 | |
| 2418 | bi->bi_bdev = rdev->bdev; |
| 2419 | PRINTK("for %llu schedule op %ld on disc %d\n", |
| 2420 | (unsigned long long)sh->sector, bi->bi_rw, i); |
| 2421 | atomic_inc(&sh->count); |
| 2422 | bi->bi_sector = sh->sector + rdev->data_offset; |
| 2423 | bi->bi_flags = 1 << BIO_UPTODATE; |
| 2424 | bi->bi_vcnt = 1; |
| 2425 | bi->bi_max_vecs = 1; |
| 2426 | bi->bi_idx = 0; |
| 2427 | bi->bi_io_vec = &sh->dev[i].vec; |
| 2428 | bi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
| 2429 | bi->bi_io_vec[0].bv_offset = 0; |
| 2430 | bi->bi_size = STRIPE_SIZE; |
| 2431 | bi->bi_next = NULL; |
| 2432 | if (rw == WRITE && |
| 2433 | test_bit(R5_ReWrite, &sh->dev[i].flags)) |
| 2434 | atomic_add(STRIPE_SECTORS, &rdev->corrected_errors); |
| 2435 | generic_make_request(bi); |
| 2436 | } else { |
| 2437 | if (rw == 1) |
| 2438 | set_bit(STRIPE_DEGRADED, &sh->state); |
| 2439 | PRINTK("skip op %ld on disc %d for sector %llu\n", |
| 2440 | bi->bi_rw, i, (unsigned long long)sh->sector); |
| 2441 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 2442 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2443 | } |
| 2444 | } |
| 2445 | } |
| 2446 | |
| 2447 | static void handle_stripe(struct stripe_head *sh, struct page *tmp_page) |
| 2448 | { |
| 2449 | if (sh->raid_conf->level == 6) |
| 2450 | handle_stripe6(sh, tmp_page); |
| 2451 | else |
| 2452 | handle_stripe5(sh); |
| 2453 | } |
| 2454 | |
| 2455 | |
| 2456 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 2457 | static void raid5_activate_delayed(raid5_conf_t *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2458 | { |
| 2459 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) { |
| 2460 | while (!list_empty(&conf->delayed_list)) { |
| 2461 | struct list_head *l = conf->delayed_list.next; |
| 2462 | struct stripe_head *sh; |
| 2463 | sh = list_entry(l, struct stripe_head, lru); |
| 2464 | list_del_init(l); |
| 2465 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 2466 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 2467 | atomic_inc(&conf->preread_active_stripes); |
| 2468 | list_add_tail(&sh->lru, &conf->handle_list); |
| 2469 | } |
| 2470 | } |
| 2471 | } |
| 2472 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 2473 | static void activate_bit_delay(raid5_conf_t *conf) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2474 | { |
| 2475 | /* device_lock is held */ |
| 2476 | struct list_head head; |
| 2477 | list_add(&head, &conf->bitmap_list); |
| 2478 | list_del_init(&conf->bitmap_list); |
| 2479 | while (!list_empty(&head)) { |
| 2480 | struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru); |
| 2481 | list_del_init(&sh->lru); |
| 2482 | atomic_inc(&sh->count); |
| 2483 | __release_stripe(conf, sh); |
| 2484 | } |
| 2485 | } |
| 2486 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2487 | static void unplug_slaves(mddev_t *mddev) |
| 2488 | { |
| 2489 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 2490 | int i; |
| 2491 | |
| 2492 | rcu_read_lock(); |
| 2493 | for (i=0; i<mddev->raid_disks; i++) { |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 2494 | mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2495 | if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2496 | request_queue_t *r_queue = bdev_get_queue(rdev->bdev); |
| 2497 | |
| 2498 | atomic_inc(&rdev->nr_pending); |
| 2499 | rcu_read_unlock(); |
| 2500 | |
| 2501 | if (r_queue->unplug_fn) |
| 2502 | r_queue->unplug_fn(r_queue); |
| 2503 | |
| 2504 | rdev_dec_pending(rdev, mddev); |
| 2505 | rcu_read_lock(); |
| 2506 | } |
| 2507 | } |
| 2508 | rcu_read_unlock(); |
| 2509 | } |
| 2510 | |
| 2511 | static void raid5_unplug_device(request_queue_t *q) |
| 2512 | { |
| 2513 | mddev_t *mddev = q->queuedata; |
| 2514 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 2515 | unsigned long flags; |
| 2516 | |
| 2517 | spin_lock_irqsave(&conf->device_lock, flags); |
| 2518 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2519 | if (blk_remove_plug(q)) { |
| 2520 | conf->seq_flush++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2521 | raid5_activate_delayed(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2522 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2523 | md_wakeup_thread(mddev->thread); |
| 2524 | |
| 2525 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 2526 | |
| 2527 | unplug_slaves(mddev); |
| 2528 | } |
| 2529 | |
| 2530 | static int raid5_issue_flush(request_queue_t *q, struct gendisk *disk, |
| 2531 | sector_t *error_sector) |
| 2532 | { |
| 2533 | mddev_t *mddev = q->queuedata; |
| 2534 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 2535 | int i, ret = 0; |
| 2536 | |
| 2537 | rcu_read_lock(); |
| 2538 | for (i=0; i<mddev->raid_disks && ret == 0; i++) { |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 2539 | mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2540 | if (rdev && !test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2541 | struct block_device *bdev = rdev->bdev; |
| 2542 | request_queue_t *r_queue = bdev_get_queue(bdev); |
| 2543 | |
| 2544 | if (!r_queue->issue_flush_fn) |
| 2545 | ret = -EOPNOTSUPP; |
| 2546 | else { |
| 2547 | atomic_inc(&rdev->nr_pending); |
| 2548 | rcu_read_unlock(); |
| 2549 | ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk, |
| 2550 | error_sector); |
| 2551 | rdev_dec_pending(rdev, mddev); |
| 2552 | rcu_read_lock(); |
| 2553 | } |
| 2554 | } |
| 2555 | } |
| 2556 | rcu_read_unlock(); |
| 2557 | return ret; |
| 2558 | } |
| 2559 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 2560 | static int make_request(request_queue_t *q, struct bio * bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2561 | { |
| 2562 | mddev_t *mddev = q->queuedata; |
| 2563 | raid5_conf_t *conf = mddev_to_conf(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2564 | unsigned int dd_idx, pd_idx; |
| 2565 | sector_t new_sector; |
| 2566 | sector_t logical_sector, last_sector; |
| 2567 | struct stripe_head *sh; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 2568 | const int rw = bio_data_dir(bi); |
NeilBrown | f634475 | 2006-03-27 01:18:17 -0800 | [diff] [blame] | 2569 | int remaining; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2570 | |
NeilBrown | e5dcdd8 | 2005-09-09 16:23:41 -0700 | [diff] [blame] | 2571 | if (unlikely(bio_barrier(bi))) { |
| 2572 | bio_endio(bi, bi->bi_size, -EOPNOTSUPP); |
| 2573 | return 0; |
| 2574 | } |
| 2575 | |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2576 | md_write_start(mddev, bi); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2577 | |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 2578 | disk_stat_inc(mddev->gendisk, ios[rw]); |
| 2579 | disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2580 | |
| 2581 | logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
| 2582 | last_sector = bi->bi_sector + (bi->bi_size>>9); |
| 2583 | bi->bi_next = NULL; |
| 2584 | bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2585 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2586 | for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) { |
| 2587 | DEFINE_WAIT(w); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2588 | int disks, data_disks; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 2589 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 2590 | retry: |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 2591 | prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE); |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 2592 | if (likely(conf->expand_progress == MaxSector)) |
| 2593 | disks = conf->raid_disks; |
| 2594 | else { |
NeilBrown | df8e7f7 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 2595 | /* spinlock is needed as expand_progress may be |
| 2596 | * 64bit on a 32bit platform, and so it might be |
| 2597 | * possible to see a half-updated value |
| 2598 | * Ofcourse expand_progress could change after |
| 2599 | * the lock is dropped, so once we get a reference |
| 2600 | * to the stripe that we think it is, we will have |
| 2601 | * to check again. |
| 2602 | */ |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 2603 | spin_lock_irq(&conf->device_lock); |
| 2604 | disks = conf->raid_disks; |
| 2605 | if (logical_sector >= conf->expand_progress) |
| 2606 | disks = conf->previous_raid_disks; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 2607 | else { |
| 2608 | if (logical_sector >= conf->expand_lo) { |
| 2609 | spin_unlock_irq(&conf->device_lock); |
| 2610 | schedule(); |
| 2611 | goto retry; |
| 2612 | } |
| 2613 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 2614 | spin_unlock_irq(&conf->device_lock); |
| 2615 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2616 | data_disks = disks - conf->max_degraded; |
| 2617 | |
| 2618 | new_sector = raid5_compute_sector(logical_sector, disks, data_disks, |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 2619 | &dd_idx, &pd_idx, conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2620 | PRINTK("raid5: make_request, sector %llu logical %llu\n", |
| 2621 | (unsigned long long)new_sector, |
| 2622 | (unsigned long long)logical_sector); |
| 2623 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 2624 | sh = get_active_stripe(conf, new_sector, disks, pd_idx, (bi->bi_rw&RWA_MASK)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2625 | if (sh) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 2626 | if (unlikely(conf->expand_progress != MaxSector)) { |
| 2627 | /* expansion might have moved on while waiting for a |
NeilBrown | df8e7f7 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 2628 | * stripe, so we must do the range check again. |
| 2629 | * Expansion could still move past after this |
| 2630 | * test, but as we are holding a reference to |
| 2631 | * 'sh', we know that if that happens, |
| 2632 | * STRIPE_EXPANDING will get set and the expansion |
| 2633 | * won't proceed until we finish with the stripe. |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 2634 | */ |
| 2635 | int must_retry = 0; |
| 2636 | spin_lock_irq(&conf->device_lock); |
| 2637 | if (logical_sector < conf->expand_progress && |
| 2638 | disks == conf->previous_raid_disks) |
| 2639 | /* mismatch, need to try again */ |
| 2640 | must_retry = 1; |
| 2641 | spin_unlock_irq(&conf->device_lock); |
| 2642 | if (must_retry) { |
| 2643 | release_stripe(sh); |
| 2644 | goto retry; |
| 2645 | } |
| 2646 | } |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 2647 | /* FIXME what if we get a false positive because these |
| 2648 | * are being updated. |
| 2649 | */ |
| 2650 | if (logical_sector >= mddev->suspend_lo && |
| 2651 | logical_sector < mddev->suspend_hi) { |
| 2652 | release_stripe(sh); |
| 2653 | schedule(); |
| 2654 | goto retry; |
| 2655 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 2656 | |
| 2657 | if (test_bit(STRIPE_EXPANDING, &sh->state) || |
| 2658 | !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) { |
| 2659 | /* Stripe is busy expanding or |
| 2660 | * add failed due to overlap. Flush everything |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2661 | * and wait a while |
| 2662 | */ |
| 2663 | raid5_unplug_device(mddev->queue); |
| 2664 | release_stripe(sh); |
| 2665 | schedule(); |
| 2666 | goto retry; |
| 2667 | } |
| 2668 | finish_wait(&conf->wait_for_overlap, &w); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2669 | handle_stripe(sh, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2670 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2671 | } else { |
| 2672 | /* cannot get stripe for read-ahead, just give-up */ |
| 2673 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
| 2674 | finish_wait(&conf->wait_for_overlap, &w); |
| 2675 | break; |
| 2676 | } |
| 2677 | |
| 2678 | } |
| 2679 | spin_lock_irq(&conf->device_lock); |
NeilBrown | f634475 | 2006-03-27 01:18:17 -0800 | [diff] [blame] | 2680 | remaining = --bi->bi_phys_segments; |
| 2681 | spin_unlock_irq(&conf->device_lock); |
| 2682 | if (remaining == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2683 | int bytes = bi->bi_size; |
| 2684 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2685 | if ( rw == WRITE ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2686 | md_write_end(mddev); |
| 2687 | bi->bi_size = 0; |
| 2688 | bi->bi_end_io(bi, bytes, 0); |
| 2689 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2690 | return 0; |
| 2691 | } |
| 2692 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 2693 | static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2694 | { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 2695 | /* reshaping is quite different to recovery/resync so it is |
| 2696 | * handled quite separately ... here. |
| 2697 | * |
| 2698 | * On each call to sync_request, we gather one chunk worth of |
| 2699 | * destination stripes and flag them as expanding. |
| 2700 | * Then we find all the source stripes and request reads. |
| 2701 | * As the reads complete, handle_stripe will copy the data |
| 2702 | * into the destination stripe and release that stripe. |
| 2703 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2704 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; |
| 2705 | struct stripe_head *sh; |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2706 | int pd_idx; |
| 2707 | sector_t first_sector, last_sector; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 2708 | int raid_disks; |
| 2709 | int data_disks; |
| 2710 | int i; |
| 2711 | int dd_idx; |
| 2712 | sector_t writepos, safepos, gap; |
| 2713 | |
| 2714 | if (sector_nr == 0 && |
| 2715 | conf->expand_progress != 0) { |
| 2716 | /* restarting in the middle, skip the initial sectors */ |
| 2717 | sector_nr = conf->expand_progress; |
| 2718 | sector_div(sector_nr, conf->raid_disks-1); |
| 2719 | *skipped = 1; |
| 2720 | return sector_nr; |
| 2721 | } |
| 2722 | |
| 2723 | /* we update the metadata when there is more than 3Meg |
| 2724 | * in the block range (that is rather arbitrary, should |
| 2725 | * probably be time based) or when the data about to be |
| 2726 | * copied would over-write the source of the data at |
| 2727 | * the front of the range. |
| 2728 | * i.e. one new_stripe forward from expand_progress new_maps |
| 2729 | * to after where expand_lo old_maps to |
| 2730 | */ |
| 2731 | writepos = conf->expand_progress + |
| 2732 | conf->chunk_size/512*(conf->raid_disks-1); |
| 2733 | sector_div(writepos, conf->raid_disks-1); |
| 2734 | safepos = conf->expand_lo; |
| 2735 | sector_div(safepos, conf->previous_raid_disks-1); |
| 2736 | gap = conf->expand_progress - conf->expand_lo; |
| 2737 | |
| 2738 | if (writepos >= safepos || |
| 2739 | gap > (conf->raid_disks-1)*3000*2 /*3Meg*/) { |
| 2740 | /* Cannot proceed until we've updated the superblock... */ |
| 2741 | wait_event(conf->wait_for_overlap, |
| 2742 | atomic_read(&conf->reshape_stripes)==0); |
| 2743 | mddev->reshape_position = conf->expand_progress; |
| 2744 | mddev->sb_dirty = 1; |
| 2745 | md_wakeup_thread(mddev->thread); |
| 2746 | wait_event(mddev->sb_wait, mddev->sb_dirty == 0 || |
| 2747 | kthread_should_stop()); |
| 2748 | spin_lock_irq(&conf->device_lock); |
| 2749 | conf->expand_lo = mddev->reshape_position; |
| 2750 | spin_unlock_irq(&conf->device_lock); |
| 2751 | wake_up(&conf->wait_for_overlap); |
| 2752 | } |
| 2753 | |
| 2754 | for (i=0; i < conf->chunk_size/512; i+= STRIPE_SECTORS) { |
| 2755 | int j; |
| 2756 | int skipped = 0; |
| 2757 | pd_idx = stripe_to_pdidx(sector_nr+i, conf, conf->raid_disks); |
| 2758 | sh = get_active_stripe(conf, sector_nr+i, |
| 2759 | conf->raid_disks, pd_idx, 0); |
| 2760 | set_bit(STRIPE_EXPANDING, &sh->state); |
| 2761 | atomic_inc(&conf->reshape_stripes); |
| 2762 | /* If any of this stripe is beyond the end of the old |
| 2763 | * array, then we need to zero those blocks |
| 2764 | */ |
| 2765 | for (j=sh->disks; j--;) { |
| 2766 | sector_t s; |
| 2767 | if (j == sh->pd_idx) |
| 2768 | continue; |
| 2769 | s = compute_blocknr(sh, j); |
| 2770 | if (s < (mddev->array_size<<1)) { |
| 2771 | skipped = 1; |
| 2772 | continue; |
| 2773 | } |
| 2774 | memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE); |
| 2775 | set_bit(R5_Expanded, &sh->dev[j].flags); |
| 2776 | set_bit(R5_UPTODATE, &sh->dev[j].flags); |
| 2777 | } |
| 2778 | if (!skipped) { |
| 2779 | set_bit(STRIPE_EXPAND_READY, &sh->state); |
| 2780 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2781 | } |
| 2782 | release_stripe(sh); |
| 2783 | } |
| 2784 | spin_lock_irq(&conf->device_lock); |
| 2785 | conf->expand_progress = (sector_nr + i)*(conf->raid_disks-1); |
| 2786 | spin_unlock_irq(&conf->device_lock); |
| 2787 | /* Ok, those stripe are ready. We can start scheduling |
| 2788 | * reads on the source stripes. |
| 2789 | * The source stripes are determined by mapping the first and last |
| 2790 | * block on the destination stripes. |
| 2791 | */ |
| 2792 | raid_disks = conf->previous_raid_disks; |
| 2793 | data_disks = raid_disks - 1; |
| 2794 | first_sector = |
| 2795 | raid5_compute_sector(sector_nr*(conf->raid_disks-1), |
| 2796 | raid_disks, data_disks, |
| 2797 | &dd_idx, &pd_idx, conf); |
| 2798 | last_sector = |
| 2799 | raid5_compute_sector((sector_nr+conf->chunk_size/512) |
| 2800 | *(conf->raid_disks-1) -1, |
| 2801 | raid_disks, data_disks, |
| 2802 | &dd_idx, &pd_idx, conf); |
| 2803 | if (last_sector >= (mddev->size<<1)) |
| 2804 | last_sector = (mddev->size<<1)-1; |
| 2805 | while (first_sector <= last_sector) { |
| 2806 | pd_idx = stripe_to_pdidx(first_sector, conf, conf->previous_raid_disks); |
| 2807 | sh = get_active_stripe(conf, first_sector, |
| 2808 | conf->previous_raid_disks, pd_idx, 0); |
| 2809 | set_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 2810 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2811 | release_stripe(sh); |
| 2812 | first_sector += STRIPE_SECTORS; |
| 2813 | } |
| 2814 | return conf->chunk_size>>9; |
| 2815 | } |
| 2816 | |
| 2817 | /* FIXME go_faster isn't used */ |
| 2818 | static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster) |
| 2819 | { |
| 2820 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; |
| 2821 | struct stripe_head *sh; |
| 2822 | int pd_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2823 | int raid_disks = conf->raid_disks; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2824 | sector_t max_sector = mddev->size << 1; |
| 2825 | int sync_blocks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2826 | int still_degraded = 0; |
| 2827 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2828 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2829 | if (sector_nr >= max_sector) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2830 | /* just being told to finish up .. nothing much to do */ |
| 2831 | unplug_slaves(mddev); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 2832 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { |
| 2833 | end_reshape(conf); |
| 2834 | return 0; |
| 2835 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2836 | |
| 2837 | if (mddev->curr_resync < max_sector) /* aborted */ |
| 2838 | bitmap_end_sync(mddev->bitmap, mddev->curr_resync, |
| 2839 | &sync_blocks, 1); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2840 | else /* completed sync */ |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2841 | conf->fullsync = 0; |
| 2842 | bitmap_close_sync(mddev->bitmap); |
| 2843 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2844 | return 0; |
| 2845 | } |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2846 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 2847 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 2848 | return reshape_request(mddev, sector_nr, skipped); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 2849 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2850 | /* if there is too many failed drives and we are trying |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2851 | * to resync, then assert that we are finished, because there is |
| 2852 | * nothing we can do. |
| 2853 | */ |
NeilBrown | 3285edf | 2006-06-26 00:27:55 -0700 | [diff] [blame] | 2854 | if (mddev->degraded >= conf->max_degraded && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2855 | test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 2856 | sector_t rv = (mddev->size << 1) - sector_nr; |
| 2857 | *skipped = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2858 | return rv; |
| 2859 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2860 | if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) && |
NeilBrown | 3855ad9 | 2005-11-08 21:39:38 -0800 | [diff] [blame] | 2861 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2862 | !conf->fullsync && sync_blocks >= STRIPE_SECTORS) { |
| 2863 | /* we can skip this block, and probably more */ |
| 2864 | sync_blocks /= STRIPE_SECTORS; |
| 2865 | *skipped = 1; |
| 2866 | return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */ |
| 2867 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2868 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2869 | pd_idx = stripe_to_pdidx(sector_nr, conf, raid_disks); |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 2870 | sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2871 | if (sh == NULL) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 2872 | sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2873 | /* make sure we don't swamp the stripe cache if someone else |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2874 | * is trying to get access |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2875 | */ |
Nishanth Aravamudan | 66c006a | 2005-11-07 01:01:17 -0800 | [diff] [blame] | 2876 | schedule_timeout_uninterruptible(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2877 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2878 | /* Need to check if array will still be degraded after recovery/resync |
| 2879 | * We don't need to check the 'failed' flag as when that gets set, |
| 2880 | * recovery aborts. |
| 2881 | */ |
| 2882 | for (i=0; i<mddev->raid_disks; i++) |
| 2883 | if (conf->disks[i].rdev == NULL) |
| 2884 | still_degraded = 1; |
| 2885 | |
| 2886 | bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded); |
| 2887 | |
| 2888 | spin_lock(&sh->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2889 | set_bit(STRIPE_SYNCING, &sh->state); |
| 2890 | clear_bit(STRIPE_INSYNC, &sh->state); |
| 2891 | spin_unlock(&sh->lock); |
| 2892 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2893 | handle_stripe(sh, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2894 | release_stripe(sh); |
| 2895 | |
| 2896 | return STRIPE_SECTORS; |
| 2897 | } |
| 2898 | |
| 2899 | /* |
| 2900 | * This is our raid5 kernel thread. |
| 2901 | * |
| 2902 | * We scan the hash table for stripes which can be handled now. |
| 2903 | * During the scan, completed stripes are saved for us by the interrupt |
| 2904 | * handler, so that they will not have to wait for our next wakeup. |
| 2905 | */ |
| 2906 | static void raid5d (mddev_t *mddev) |
| 2907 | { |
| 2908 | struct stripe_head *sh; |
| 2909 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 2910 | int handled; |
| 2911 | |
| 2912 | PRINTK("+++ raid5d active\n"); |
| 2913 | |
| 2914 | md_check_recovery(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2915 | |
| 2916 | handled = 0; |
| 2917 | spin_lock_irq(&conf->device_lock); |
| 2918 | while (1) { |
| 2919 | struct list_head *first; |
| 2920 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2921 | if (conf->seq_flush - conf->seq_write > 0) { |
| 2922 | int seq = conf->seq_flush; |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 2923 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2924 | bitmap_unplug(mddev->bitmap); |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 2925 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2926 | conf->seq_write = seq; |
| 2927 | activate_bit_delay(conf); |
| 2928 | } |
| 2929 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2930 | if (list_empty(&conf->handle_list) && |
| 2931 | atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD && |
| 2932 | !blk_queue_plugged(mddev->queue) && |
| 2933 | !list_empty(&conf->delayed_list)) |
| 2934 | raid5_activate_delayed(conf); |
| 2935 | |
| 2936 | if (list_empty(&conf->handle_list)) |
| 2937 | break; |
| 2938 | |
| 2939 | first = conf->handle_list.next; |
| 2940 | sh = list_entry(first, struct stripe_head, lru); |
| 2941 | |
| 2942 | list_del_init(first); |
| 2943 | atomic_inc(&sh->count); |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 2944 | BUG_ON(atomic_read(&sh->count)!= 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2945 | spin_unlock_irq(&conf->device_lock); |
| 2946 | |
| 2947 | handled++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2948 | handle_stripe(sh, conf->spare_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2949 | release_stripe(sh); |
| 2950 | |
| 2951 | spin_lock_irq(&conf->device_lock); |
| 2952 | } |
| 2953 | PRINTK("%d stripes handled\n", handled); |
| 2954 | |
| 2955 | spin_unlock_irq(&conf->device_lock); |
| 2956 | |
| 2957 | unplug_slaves(mddev); |
| 2958 | |
| 2959 | PRINTK("--- raid5d inactive\n"); |
| 2960 | } |
| 2961 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2962 | static ssize_t |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 2963 | raid5_show_stripe_cache_size(mddev_t *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2964 | { |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 2965 | raid5_conf_t *conf = mddev_to_conf(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2966 | if (conf) |
| 2967 | return sprintf(page, "%d\n", conf->max_nr_stripes); |
| 2968 | else |
| 2969 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2970 | } |
| 2971 | |
| 2972 | static ssize_t |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 2973 | raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2974 | { |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 2975 | raid5_conf_t *conf = mddev_to_conf(mddev); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2976 | char *end; |
| 2977 | int new; |
| 2978 | if (len >= PAGE_SIZE) |
| 2979 | return -EINVAL; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2980 | if (!conf) |
| 2981 | return -ENODEV; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2982 | |
| 2983 | new = simple_strtoul(page, &end, 10); |
| 2984 | if (!*page || (*end && *end != '\n') ) |
| 2985 | return -EINVAL; |
| 2986 | if (new <= 16 || new > 32768) |
| 2987 | return -EINVAL; |
| 2988 | while (new < conf->max_nr_stripes) { |
| 2989 | if (drop_one_stripe(conf)) |
| 2990 | conf->max_nr_stripes--; |
| 2991 | else |
| 2992 | break; |
| 2993 | } |
| 2994 | while (new > conf->max_nr_stripes) { |
| 2995 | if (grow_one_stripe(conf)) |
| 2996 | conf->max_nr_stripes++; |
| 2997 | else break; |
| 2998 | } |
| 2999 | return len; |
| 3000 | } |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 3001 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3002 | static struct md_sysfs_entry |
| 3003 | raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR, |
| 3004 | raid5_show_stripe_cache_size, |
| 3005 | raid5_store_stripe_cache_size); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 3006 | |
| 3007 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3008 | stripe_cache_active_show(mddev_t *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 3009 | { |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 3010 | raid5_conf_t *conf = mddev_to_conf(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3011 | if (conf) |
| 3012 | return sprintf(page, "%d\n", atomic_read(&conf->active_stripes)); |
| 3013 | else |
| 3014 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 3015 | } |
| 3016 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3017 | static struct md_sysfs_entry |
| 3018 | raid5_stripecache_active = __ATTR_RO(stripe_cache_active); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 3019 | |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 3020 | static struct attribute *raid5_attrs[] = { |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 3021 | &raid5_stripecache_size.attr, |
| 3022 | &raid5_stripecache_active.attr, |
| 3023 | NULL, |
| 3024 | }; |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 3025 | static struct attribute_group raid5_attrs_group = { |
| 3026 | .name = NULL, |
| 3027 | .attrs = raid5_attrs, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 3028 | }; |
| 3029 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3030 | static int run(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3031 | { |
| 3032 | raid5_conf_t *conf; |
| 3033 | int raid_disk, memory; |
| 3034 | mdk_rdev_t *rdev; |
| 3035 | struct disk_info *disk; |
| 3036 | struct list_head *tmp; |
| 3037 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3038 | if (mddev->level != 5 && mddev->level != 4 && mddev->level != 6) { |
| 3039 | printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n", |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 3040 | mdname(mddev), mddev->level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3041 | return -EIO; |
| 3042 | } |
| 3043 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3044 | if (mddev->reshape_position != MaxSector) { |
| 3045 | /* Check that we can continue the reshape. |
| 3046 | * Currently only disks can change, it must |
| 3047 | * increase, and we must be past the point where |
| 3048 | * a stripe over-writes itself |
| 3049 | */ |
| 3050 | sector_t here_new, here_old; |
| 3051 | int old_disks; |
| 3052 | |
| 3053 | if (mddev->new_level != mddev->level || |
| 3054 | mddev->new_layout != mddev->layout || |
| 3055 | mddev->new_chunk != mddev->chunk_size) { |
| 3056 | printk(KERN_ERR "raid5: %s: unsupported reshape required - aborting.\n", |
| 3057 | mdname(mddev)); |
| 3058 | return -EINVAL; |
| 3059 | } |
| 3060 | if (mddev->delta_disks <= 0) { |
| 3061 | printk(KERN_ERR "raid5: %s: unsupported reshape (reduce disks) required - aborting.\n", |
| 3062 | mdname(mddev)); |
| 3063 | return -EINVAL; |
| 3064 | } |
| 3065 | old_disks = mddev->raid_disks - mddev->delta_disks; |
| 3066 | /* reshape_position must be on a new-stripe boundary, and one |
| 3067 | * further up in new geometry must map after here in old geometry. |
| 3068 | */ |
| 3069 | here_new = mddev->reshape_position; |
| 3070 | if (sector_div(here_new, (mddev->chunk_size>>9)*(mddev->raid_disks-1))) { |
| 3071 | printk(KERN_ERR "raid5: reshape_position not on a stripe boundary\n"); |
| 3072 | return -EINVAL; |
| 3073 | } |
| 3074 | /* here_new is the stripe we will write to */ |
| 3075 | here_old = mddev->reshape_position; |
| 3076 | sector_div(here_old, (mddev->chunk_size>>9)*(old_disks-1)); |
| 3077 | /* here_old is the first stripe that we might need to read from */ |
| 3078 | if (here_new >= here_old) { |
| 3079 | /* Reading from the same stripe as writing to - bad */ |
| 3080 | printk(KERN_ERR "raid5: reshape_position too early for auto-recovery - aborting.\n"); |
| 3081 | return -EINVAL; |
| 3082 | } |
| 3083 | printk(KERN_INFO "raid5: reshape will continue\n"); |
| 3084 | /* OK, we should be able to continue; */ |
| 3085 | } |
| 3086 | |
| 3087 | |
NeilBrown | b55e6bf | 2006-03-27 01:18:06 -0800 | [diff] [blame] | 3088 | mddev->private = kzalloc(sizeof (raid5_conf_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3089 | if ((conf = mddev->private) == NULL) |
| 3090 | goto abort; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3091 | if (mddev->reshape_position == MaxSector) { |
| 3092 | conf->previous_raid_disks = conf->raid_disks = mddev->raid_disks; |
| 3093 | } else { |
| 3094 | conf->raid_disks = mddev->raid_disks; |
| 3095 | conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks; |
| 3096 | } |
| 3097 | |
| 3098 | conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info), |
NeilBrown | b55e6bf | 2006-03-27 01:18:06 -0800 | [diff] [blame] | 3099 | GFP_KERNEL); |
| 3100 | if (!conf->disks) |
| 3101 | goto abort; |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 3102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3103 | conf->mddev = mddev; |
| 3104 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 3105 | if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3106 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3107 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3108 | if (mddev->level == 6) { |
| 3109 | conf->spare_page = alloc_page(GFP_KERNEL); |
| 3110 | if (!conf->spare_page) |
| 3111 | goto abort; |
| 3112 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3113 | spin_lock_init(&conf->device_lock); |
| 3114 | init_waitqueue_head(&conf->wait_for_stripe); |
| 3115 | init_waitqueue_head(&conf->wait_for_overlap); |
| 3116 | INIT_LIST_HEAD(&conf->handle_list); |
| 3117 | INIT_LIST_HEAD(&conf->delayed_list); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3118 | INIT_LIST_HEAD(&conf->bitmap_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3119 | INIT_LIST_HEAD(&conf->inactive_list); |
| 3120 | atomic_set(&conf->active_stripes, 0); |
| 3121 | atomic_set(&conf->preread_active_stripes, 0); |
| 3122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3123 | PRINTK("raid5: run(%s) called.\n", mdname(mddev)); |
| 3124 | |
| 3125 | ITERATE_RDEV(mddev,rdev,tmp) { |
| 3126 | raid_disk = rdev->raid_disk; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3127 | if (raid_disk >= conf->raid_disks |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3128 | || raid_disk < 0) |
| 3129 | continue; |
| 3130 | disk = conf->disks + raid_disk; |
| 3131 | |
| 3132 | disk->rdev = rdev; |
| 3133 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3134 | if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3135 | char b[BDEVNAME_SIZE]; |
| 3136 | printk(KERN_INFO "raid5: device %s operational as raid" |
| 3137 | " disk %d\n", bdevname(rdev->bdev,b), |
| 3138 | raid_disk); |
| 3139 | conf->working_disks++; |
| 3140 | } |
| 3141 | } |
| 3142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3143 | /* |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3144 | * 0 for a fully functional array, 1 or 2 for a degraded array. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3145 | */ |
| 3146 | mddev->degraded = conf->failed_disks = conf->raid_disks - conf->working_disks; |
| 3147 | conf->mddev = mddev; |
| 3148 | conf->chunk_size = mddev->chunk_size; |
| 3149 | conf->level = mddev->level; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3150 | if (conf->level == 6) |
| 3151 | conf->max_degraded = 2; |
| 3152 | else |
| 3153 | conf->max_degraded = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3154 | conf->algorithm = mddev->layout; |
| 3155 | conf->max_nr_stripes = NR_STRIPES; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3156 | conf->expand_progress = mddev->reshape_position; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3157 | |
| 3158 | /* device size must be a multiple of chunk size */ |
| 3159 | mddev->size &= ~(mddev->chunk_size/1024 -1); |
NeilBrown | b158156 | 2005-07-31 22:34:50 -0700 | [diff] [blame] | 3160 | mddev->resync_max_sectors = mddev->size << 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3161 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3162 | if (conf->level == 6 && conf->raid_disks < 4) { |
| 3163 | printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n", |
| 3164 | mdname(mddev), conf->raid_disks); |
| 3165 | goto abort; |
| 3166 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3167 | if (!conf->chunk_size || conf->chunk_size % 4) { |
| 3168 | printk(KERN_ERR "raid5: invalid chunk size %d for %s\n", |
| 3169 | conf->chunk_size, mdname(mddev)); |
| 3170 | goto abort; |
| 3171 | } |
| 3172 | if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) { |
| 3173 | printk(KERN_ERR |
| 3174 | "raid5: unsupported parity algorithm %d for %s\n", |
| 3175 | conf->algorithm, mdname(mddev)); |
| 3176 | goto abort; |
| 3177 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3178 | if (mddev->degraded > conf->max_degraded) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3179 | printk(KERN_ERR "raid5: not enough operational devices for %s" |
| 3180 | " (%d/%d failed)\n", |
| 3181 | mdname(mddev), conf->failed_disks, conf->raid_disks); |
| 3182 | goto abort; |
| 3183 | } |
| 3184 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3185 | if (mddev->degraded > 0 && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3186 | mddev->recovery_cp != MaxSector) { |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 3187 | if (mddev->ok_start_degraded) |
| 3188 | printk(KERN_WARNING |
| 3189 | "raid5: starting dirty degraded array: %s" |
| 3190 | "- data corruption possible.\n", |
| 3191 | mdname(mddev)); |
| 3192 | else { |
| 3193 | printk(KERN_ERR |
| 3194 | "raid5: cannot start dirty degraded array for %s\n", |
| 3195 | mdname(mddev)); |
| 3196 | goto abort; |
| 3197 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3198 | } |
| 3199 | |
| 3200 | { |
| 3201 | mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5"); |
| 3202 | if (!mddev->thread) { |
| 3203 | printk(KERN_ERR |
| 3204 | "raid5: couldn't allocate thread for %s\n", |
| 3205 | mdname(mddev)); |
| 3206 | goto abort; |
| 3207 | } |
| 3208 | } |
NeilBrown | 5036805 | 2005-12-12 02:39:17 -0800 | [diff] [blame] | 3209 | memory = conf->max_nr_stripes * (sizeof(struct stripe_head) + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3210 | conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024; |
| 3211 | if (grow_stripes(conf, conf->max_nr_stripes)) { |
| 3212 | printk(KERN_ERR |
| 3213 | "raid5: couldn't allocate %dkB for buffers\n", memory); |
| 3214 | shrink_stripes(conf); |
| 3215 | md_unregister_thread(mddev->thread); |
| 3216 | goto abort; |
| 3217 | } else |
| 3218 | printk(KERN_INFO "raid5: allocated %dkB for %s\n", |
| 3219 | memory, mdname(mddev)); |
| 3220 | |
| 3221 | if (mddev->degraded == 0) |
| 3222 | printk("raid5: raid level %d set %s active with %d out of %d" |
| 3223 | " devices, algorithm %d\n", conf->level, mdname(mddev), |
| 3224 | mddev->raid_disks-mddev->degraded, mddev->raid_disks, |
| 3225 | conf->algorithm); |
| 3226 | else |
| 3227 | printk(KERN_ALERT "raid5: raid level %d set %s active with %d" |
| 3228 | " out of %d devices, algorithm %d\n", conf->level, |
| 3229 | mdname(mddev), mddev->raid_disks - mddev->degraded, |
| 3230 | mddev->raid_disks, conf->algorithm); |
| 3231 | |
| 3232 | print_raid5_conf(conf); |
| 3233 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3234 | if (conf->expand_progress != MaxSector) { |
| 3235 | printk("...ok start reshape thread\n"); |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 3236 | conf->expand_lo = conf->expand_progress; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3237 | atomic_set(&conf->reshape_stripes, 0); |
| 3238 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 3239 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 3240 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 3241 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 3242 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
| 3243 | "%s_reshape"); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3244 | } |
| 3245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3246 | /* read-ahead size must cover two whole stripes, which is |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3247 | * 2 * (datadisks) * chunksize where 'n' is the number of raid devices |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3248 | */ |
| 3249 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3250 | int data_disks = conf->previous_raid_disks - conf->max_degraded; |
| 3251 | int stripe = data_disks * |
NeilBrown | 8932c2e | 2006-06-26 00:27:36 -0700 | [diff] [blame] | 3252 | (mddev->chunk_size / PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3253 | if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 3254 | mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
| 3255 | } |
| 3256 | |
| 3257 | /* Ok, everything is just fine now */ |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 3258 | sysfs_create_group(&mddev->kobj, &raid5_attrs_group); |
NeilBrown | 7a5febe | 2005-05-16 21:53:16 -0700 | [diff] [blame] | 3259 | |
| 3260 | mddev->queue->unplug_fn = raid5_unplug_device; |
| 3261 | mddev->queue->issue_flush_fn = raid5_issue_flush; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3262 | mddev->array_size = mddev->size * (conf->previous_raid_disks - |
| 3263 | conf->max_degraded); |
NeilBrown | 7a5febe | 2005-05-16 21:53:16 -0700 | [diff] [blame] | 3264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3265 | return 0; |
| 3266 | abort: |
| 3267 | if (conf) { |
| 3268 | print_raid5_conf(conf); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3269 | safe_put_page(conf->spare_page); |
NeilBrown | b55e6bf | 2006-03-27 01:18:06 -0800 | [diff] [blame] | 3270 | kfree(conf->disks); |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 3271 | kfree(conf->stripe_hashtbl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3272 | kfree(conf); |
| 3273 | } |
| 3274 | mddev->private = NULL; |
| 3275 | printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev)); |
| 3276 | return -EIO; |
| 3277 | } |
| 3278 | |
| 3279 | |
| 3280 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 3281 | static int stop(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3282 | { |
| 3283 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; |
| 3284 | |
| 3285 | md_unregister_thread(mddev->thread); |
| 3286 | mddev->thread = NULL; |
| 3287 | shrink_stripes(conf); |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 3288 | kfree(conf->stripe_hashtbl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3289 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 3290 | sysfs_remove_group(&mddev->kobj, &raid5_attrs_group); |
NeilBrown | b55e6bf | 2006-03-27 01:18:06 -0800 | [diff] [blame] | 3291 | kfree(conf->disks); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3292 | kfree(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3293 | mddev->private = NULL; |
| 3294 | return 0; |
| 3295 | } |
| 3296 | |
| 3297 | #if RAID5_DEBUG |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3298 | static void print_sh (struct seq_file *seq, struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3299 | { |
| 3300 | int i; |
| 3301 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3302 | seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n", |
| 3303 | (unsigned long long)sh->sector, sh->pd_idx, sh->state); |
| 3304 | seq_printf(seq, "sh %llu, count %d.\n", |
| 3305 | (unsigned long long)sh->sector, atomic_read(&sh->count)); |
| 3306 | seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector); |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3307 | for (i = 0; i < sh->disks; i++) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3308 | seq_printf(seq, "(cache%d: %p %ld) ", |
| 3309 | i, sh->dev[i].page, sh->dev[i].flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3310 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3311 | seq_printf(seq, "\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3312 | } |
| 3313 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3314 | static void printall (struct seq_file *seq, raid5_conf_t *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3315 | { |
| 3316 | struct stripe_head *sh; |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 3317 | struct hlist_node *hn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3318 | int i; |
| 3319 | |
| 3320 | spin_lock_irq(&conf->device_lock); |
| 3321 | for (i = 0; i < NR_HASH; i++) { |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 3322 | hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3323 | if (sh->raid_conf != conf) |
| 3324 | continue; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3325 | print_sh(seq, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3326 | } |
| 3327 | } |
| 3328 | spin_unlock_irq(&conf->device_lock); |
| 3329 | } |
| 3330 | #endif |
| 3331 | |
| 3332 | static void status (struct seq_file *seq, mddev_t *mddev) |
| 3333 | { |
| 3334 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; |
| 3335 | int i; |
| 3336 | |
| 3337 | seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout); |
| 3338 | seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->working_disks); |
| 3339 | for (i = 0; i < conf->raid_disks; i++) |
| 3340 | seq_printf (seq, "%s", |
| 3341 | conf->disks[i].rdev && |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3342 | test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3343 | seq_printf (seq, "]"); |
| 3344 | #if RAID5_DEBUG |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3345 | seq_printf (seq, "\n"); |
| 3346 | printall(seq, conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3347 | #endif |
| 3348 | } |
| 3349 | |
| 3350 | static void print_raid5_conf (raid5_conf_t *conf) |
| 3351 | { |
| 3352 | int i; |
| 3353 | struct disk_info *tmp; |
| 3354 | |
| 3355 | printk("RAID5 conf printout:\n"); |
| 3356 | if (!conf) { |
| 3357 | printk("(conf==NULL)\n"); |
| 3358 | return; |
| 3359 | } |
| 3360 | printk(" --- rd:%d wd:%d fd:%d\n", conf->raid_disks, |
| 3361 | conf->working_disks, conf->failed_disks); |
| 3362 | |
| 3363 | for (i = 0; i < conf->raid_disks; i++) { |
| 3364 | char b[BDEVNAME_SIZE]; |
| 3365 | tmp = conf->disks + i; |
| 3366 | if (tmp->rdev) |
| 3367 | printk(" disk %d, o:%d, dev:%s\n", |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3368 | i, !test_bit(Faulty, &tmp->rdev->flags), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3369 | bdevname(tmp->rdev->bdev,b)); |
| 3370 | } |
| 3371 | } |
| 3372 | |
| 3373 | static int raid5_spare_active(mddev_t *mddev) |
| 3374 | { |
| 3375 | int i; |
| 3376 | raid5_conf_t *conf = mddev->private; |
| 3377 | struct disk_info *tmp; |
| 3378 | |
| 3379 | for (i = 0; i < conf->raid_disks; i++) { |
| 3380 | tmp = conf->disks + i; |
| 3381 | if (tmp->rdev |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3382 | && !test_bit(Faulty, &tmp->rdev->flags) |
| 3383 | && !test_bit(In_sync, &tmp->rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3384 | mddev->degraded--; |
| 3385 | conf->failed_disks--; |
| 3386 | conf->working_disks++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3387 | set_bit(In_sync, &tmp->rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3388 | } |
| 3389 | } |
| 3390 | print_raid5_conf(conf); |
| 3391 | return 0; |
| 3392 | } |
| 3393 | |
| 3394 | static int raid5_remove_disk(mddev_t *mddev, int number) |
| 3395 | { |
| 3396 | raid5_conf_t *conf = mddev->private; |
| 3397 | int err = 0; |
| 3398 | mdk_rdev_t *rdev; |
| 3399 | struct disk_info *p = conf->disks + number; |
| 3400 | |
| 3401 | print_raid5_conf(conf); |
| 3402 | rdev = p->rdev; |
| 3403 | if (rdev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3404 | if (test_bit(In_sync, &rdev->flags) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3405 | atomic_read(&rdev->nr_pending)) { |
| 3406 | err = -EBUSY; |
| 3407 | goto abort; |
| 3408 | } |
| 3409 | p->rdev = NULL; |
Paul E. McKenney | fbd568a3e | 2005-05-01 08:59:04 -0700 | [diff] [blame] | 3410 | synchronize_rcu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3411 | if (atomic_read(&rdev->nr_pending)) { |
| 3412 | /* lost the race, try later */ |
| 3413 | err = -EBUSY; |
| 3414 | p->rdev = rdev; |
| 3415 | } |
| 3416 | } |
| 3417 | abort: |
| 3418 | |
| 3419 | print_raid5_conf(conf); |
| 3420 | return err; |
| 3421 | } |
| 3422 | |
| 3423 | static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev) |
| 3424 | { |
| 3425 | raid5_conf_t *conf = mddev->private; |
| 3426 | int found = 0; |
| 3427 | int disk; |
| 3428 | struct disk_info *p; |
| 3429 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3430 | if (mddev->degraded > conf->max_degraded) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3431 | /* no point adding a device */ |
| 3432 | return 0; |
| 3433 | |
| 3434 | /* |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3435 | * find the disk ... but prefer rdev->saved_raid_disk |
| 3436 | * if possible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3437 | */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3438 | if (rdev->saved_raid_disk >= 0 && |
| 3439 | conf->disks[rdev->saved_raid_disk].rdev == NULL) |
| 3440 | disk = rdev->saved_raid_disk; |
| 3441 | else |
| 3442 | disk = 0; |
| 3443 | for ( ; disk < conf->raid_disks; disk++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3444 | if ((p=conf->disks + disk)->rdev == NULL) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3445 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3446 | rdev->raid_disk = disk; |
| 3447 | found = 1; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3448 | if (rdev->saved_raid_disk != disk) |
| 3449 | conf->fullsync = 1; |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 3450 | rcu_assign_pointer(p->rdev, rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3451 | break; |
| 3452 | } |
| 3453 | print_raid5_conf(conf); |
| 3454 | return found; |
| 3455 | } |
| 3456 | |
| 3457 | static int raid5_resize(mddev_t *mddev, sector_t sectors) |
| 3458 | { |
| 3459 | /* no resync is happening, and there is enough space |
| 3460 | * on all devices, so we can resize. |
| 3461 | * We need to make sure resync covers any new space. |
| 3462 | * If the array is shrinking we should possibly wait until |
| 3463 | * any io in the removed space completes, but it hardly seems |
| 3464 | * worth it. |
| 3465 | */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3466 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 3467 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3468 | sectors &= ~((sector_t)mddev->chunk_size/512 - 1); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3469 | mddev->array_size = (sectors * (mddev->raid_disks-conf->max_degraded))>>1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3470 | set_capacity(mddev->gendisk, mddev->array_size << 1); |
| 3471 | mddev->changed = 1; |
| 3472 | if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) { |
| 3473 | mddev->recovery_cp = mddev->size << 1; |
| 3474 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3475 | } |
| 3476 | mddev->size = sectors /2; |
NeilBrown | 4b5c7ae | 2005-07-27 11:43:28 -0700 | [diff] [blame] | 3477 | mddev->resync_max_sectors = sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3478 | return 0; |
| 3479 | } |
| 3480 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 3481 | #ifdef CONFIG_MD_RAID5_RESHAPE |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3482 | static int raid5_check_reshape(mddev_t *mddev) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 3483 | { |
| 3484 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 3485 | int err; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 3486 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3487 | if (mddev->delta_disks < 0 || |
| 3488 | mddev->new_level != mddev->level) |
| 3489 | return -EINVAL; /* Cannot shrink array or change level yet */ |
| 3490 | if (mddev->delta_disks == 0) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 3491 | return 0; /* nothing to do */ |
| 3492 | |
| 3493 | /* Can only proceed if there are plenty of stripe_heads. |
| 3494 | * We need a minimum of one full stripe,, and for sensible progress |
| 3495 | * it is best to have about 4 times that. |
| 3496 | * If we require 4 times, then the default 256 4K stripe_heads will |
| 3497 | * allow for chunk sizes up to 256K, which is probably OK. |
| 3498 | * If the chunk size is greater, user-space should request more |
| 3499 | * stripe_heads first. |
| 3500 | */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3501 | if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes || |
| 3502 | (mddev->new_chunk / STRIPE_SIZE) * 4 > conf->max_nr_stripes) { |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 3503 | printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n", |
| 3504 | (mddev->chunk_size / STRIPE_SIZE)*4); |
| 3505 | return -ENOSPC; |
| 3506 | } |
| 3507 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3508 | err = resize_stripes(conf, conf->raid_disks + mddev->delta_disks); |
| 3509 | if (err) |
| 3510 | return err; |
| 3511 | |
| 3512 | /* looks like we might be able to manage this */ |
| 3513 | return 0; |
| 3514 | } |
| 3515 | |
| 3516 | static int raid5_start_reshape(mddev_t *mddev) |
| 3517 | { |
| 3518 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 3519 | mdk_rdev_t *rdev; |
| 3520 | struct list_head *rtmp; |
| 3521 | int spares = 0; |
| 3522 | int added_devices = 0; |
| 3523 | |
| 3524 | if (mddev->degraded || |
| 3525 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3526 | return -EBUSY; |
| 3527 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 3528 | ITERATE_RDEV(mddev, rdev, rtmp) |
| 3529 | if (rdev->raid_disk < 0 && |
| 3530 | !test_bit(Faulty, &rdev->flags)) |
| 3531 | spares++; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3532 | |
| 3533 | if (spares < mddev->delta_disks-1) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 3534 | /* Not enough devices even to make a degraded array |
| 3535 | * of that size |
| 3536 | */ |
| 3537 | return -EINVAL; |
| 3538 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3539 | atomic_set(&conf->reshape_stripes, 0); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 3540 | spin_lock_irq(&conf->device_lock); |
| 3541 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3542 | conf->raid_disks += mddev->delta_disks; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 3543 | conf->expand_progress = 0; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 3544 | conf->expand_lo = 0; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 3545 | spin_unlock_irq(&conf->device_lock); |
| 3546 | |
| 3547 | /* Add some new drives, as many as will fit. |
| 3548 | * We know there are enough to make the newly sized array work. |
| 3549 | */ |
| 3550 | ITERATE_RDEV(mddev, rdev, rtmp) |
| 3551 | if (rdev->raid_disk < 0 && |
| 3552 | !test_bit(Faulty, &rdev->flags)) { |
| 3553 | if (raid5_add_disk(mddev, rdev)) { |
| 3554 | char nm[20]; |
| 3555 | set_bit(In_sync, &rdev->flags); |
| 3556 | conf->working_disks++; |
| 3557 | added_devices++; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3558 | rdev->recovery_offset = 0; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 3559 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 3560 | sysfs_create_link(&mddev->kobj, &rdev->kobj, nm); |
| 3561 | } else |
| 3562 | break; |
| 3563 | } |
| 3564 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3565 | mddev->degraded = (conf->raid_disks - conf->previous_raid_disks) - added_devices; |
| 3566 | mddev->raid_disks = conf->raid_disks; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3567 | mddev->reshape_position = 0; |
| 3568 | mddev->sb_dirty = 1; |
| 3569 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 3570 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 3571 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 3572 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 3573 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 3574 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
| 3575 | "%s_reshape"); |
| 3576 | if (!mddev->sync_thread) { |
| 3577 | mddev->recovery = 0; |
| 3578 | spin_lock_irq(&conf->device_lock); |
| 3579 | mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks; |
| 3580 | conf->expand_progress = MaxSector; |
| 3581 | spin_unlock_irq(&conf->device_lock); |
| 3582 | return -EAGAIN; |
| 3583 | } |
| 3584 | md_wakeup_thread(mddev->sync_thread); |
| 3585 | md_new_event(mddev); |
| 3586 | return 0; |
| 3587 | } |
| 3588 | #endif |
| 3589 | |
| 3590 | static void end_reshape(raid5_conf_t *conf) |
| 3591 | { |
| 3592 | struct block_device *bdev; |
| 3593 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3594 | if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) { |
| 3595 | conf->mddev->array_size = conf->mddev->size * (conf->raid_disks-1); |
| 3596 | set_capacity(conf->mddev->gendisk, conf->mddev->array_size << 1); |
| 3597 | conf->mddev->changed = 1; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 3598 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3599 | bdev = bdget_disk(conf->mddev->gendisk, 0); |
| 3600 | if (bdev) { |
| 3601 | mutex_lock(&bdev->bd_inode->i_mutex); |
| 3602 | i_size_write(bdev->bd_inode, conf->mddev->array_size << 10); |
| 3603 | mutex_unlock(&bdev->bd_inode->i_mutex); |
| 3604 | bdput(bdev); |
| 3605 | } |
| 3606 | spin_lock_irq(&conf->device_lock); |
| 3607 | conf->expand_progress = MaxSector; |
| 3608 | spin_unlock_irq(&conf->device_lock); |
| 3609 | conf->mddev->reshape_position = MaxSector; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3610 | |
| 3611 | /* read-ahead size must cover two whole stripes, which is |
| 3612 | * 2 * (datadisks) * chunksize where 'n' is the number of raid devices |
| 3613 | */ |
| 3614 | { |
| 3615 | int data_disks = conf->previous_raid_disks - conf->max_degraded; |
| 3616 | int stripe = data_disks * |
| 3617 | (conf->mddev->chunk_size / PAGE_SIZE); |
| 3618 | if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 3619 | conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
| 3620 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 3621 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 3622 | } |
| 3623 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3624 | static void raid5_quiesce(mddev_t *mddev, int state) |
| 3625 | { |
| 3626 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 3627 | |
| 3628 | switch(state) { |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3629 | case 2: /* resume for a suspend */ |
| 3630 | wake_up(&conf->wait_for_overlap); |
| 3631 | break; |
| 3632 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3633 | case 1: /* stop all writes */ |
| 3634 | spin_lock_irq(&conf->device_lock); |
| 3635 | conf->quiesce = 1; |
| 3636 | wait_event_lock_irq(conf->wait_for_stripe, |
| 3637 | atomic_read(&conf->active_stripes) == 0, |
| 3638 | conf->device_lock, /* nothing */); |
| 3639 | spin_unlock_irq(&conf->device_lock); |
| 3640 | break; |
| 3641 | |
| 3642 | case 0: /* re-enable writes */ |
| 3643 | spin_lock_irq(&conf->device_lock); |
| 3644 | conf->quiesce = 0; |
| 3645 | wake_up(&conf->wait_for_stripe); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3646 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3647 | spin_unlock_irq(&conf->device_lock); |
| 3648 | break; |
| 3649 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3650 | } |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3651 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3652 | static struct mdk_personality raid6_personality = |
| 3653 | { |
| 3654 | .name = "raid6", |
| 3655 | .level = 6, |
| 3656 | .owner = THIS_MODULE, |
| 3657 | .make_request = make_request, |
| 3658 | .run = run, |
| 3659 | .stop = stop, |
| 3660 | .status = status, |
| 3661 | .error_handler = error, |
| 3662 | .hot_add_disk = raid5_add_disk, |
| 3663 | .hot_remove_disk= raid5_remove_disk, |
| 3664 | .spare_active = raid5_spare_active, |
| 3665 | .sync_request = sync_request, |
| 3666 | .resize = raid5_resize, |
| 3667 | .quiesce = raid5_quiesce, |
| 3668 | }; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3669 | static struct mdk_personality raid5_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3670 | { |
| 3671 | .name = "raid5", |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3672 | .level = 5, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3673 | .owner = THIS_MODULE, |
| 3674 | .make_request = make_request, |
| 3675 | .run = run, |
| 3676 | .stop = stop, |
| 3677 | .status = status, |
| 3678 | .error_handler = error, |
| 3679 | .hot_add_disk = raid5_add_disk, |
| 3680 | .hot_remove_disk= raid5_remove_disk, |
| 3681 | .spare_active = raid5_spare_active, |
| 3682 | .sync_request = sync_request, |
| 3683 | .resize = raid5_resize, |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 3684 | #ifdef CONFIG_MD_RAID5_RESHAPE |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3685 | .check_reshape = raid5_check_reshape, |
| 3686 | .start_reshape = raid5_start_reshape, |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 3687 | #endif |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3688 | .quiesce = raid5_quiesce, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3689 | }; |
| 3690 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3691 | static struct mdk_personality raid4_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3692 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3693 | .name = "raid4", |
| 3694 | .level = 4, |
| 3695 | .owner = THIS_MODULE, |
| 3696 | .make_request = make_request, |
| 3697 | .run = run, |
| 3698 | .stop = stop, |
| 3699 | .status = status, |
| 3700 | .error_handler = error, |
| 3701 | .hot_add_disk = raid5_add_disk, |
| 3702 | .hot_remove_disk= raid5_remove_disk, |
| 3703 | .spare_active = raid5_spare_active, |
| 3704 | .sync_request = sync_request, |
| 3705 | .resize = raid5_resize, |
| 3706 | .quiesce = raid5_quiesce, |
| 3707 | }; |
| 3708 | |
| 3709 | static int __init raid5_init(void) |
| 3710 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3711 | int e; |
| 3712 | |
| 3713 | e = raid6_select_algo(); |
| 3714 | if ( e ) |
| 3715 | return e; |
| 3716 | register_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3717 | register_md_personality(&raid5_personality); |
| 3718 | register_md_personality(&raid4_personality); |
| 3719 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3720 | } |
| 3721 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3722 | static void raid5_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3723 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3724 | unregister_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3725 | unregister_md_personality(&raid5_personality); |
| 3726 | unregister_md_personality(&raid4_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3727 | } |
| 3728 | |
| 3729 | module_init(raid5_init); |
| 3730 | module_exit(raid5_exit); |
| 3731 | MODULE_LICENSE("GPL"); |
| 3732 | MODULE_ALIAS("md-personality-4"); /* RAID5 */ |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3733 | MODULE_ALIAS("md-raid5"); |
| 3734 | MODULE_ALIAS("md-raid4"); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3735 | MODULE_ALIAS("md-level-5"); |
| 3736 | MODULE_ALIAS("md-level-4"); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3737 | MODULE_ALIAS("md-personality-8"); /* RAID6 */ |
| 3738 | MODULE_ALIAS("md-raid6"); |
| 3739 | MODULE_ALIAS("md-level-6"); |
| 3740 | |
| 3741 | /* This used to be two separate modules, they were: */ |
| 3742 | MODULE_ALIAS("raid5"); |
| 3743 | MODULE_ALIAS("raid6"); |