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 | |
NeilBrown | ae3c20c | 2006-07-10 04:44:17 -0700 | [diff] [blame] | 21 | /* |
| 22 | * BITMAP UNPLUGGING: |
| 23 | * |
| 24 | * The sequencing for updating the bitmap reliably is a little |
| 25 | * subtle (and I got it wrong the first time) so it deserves some |
| 26 | * explanation. |
| 27 | * |
| 28 | * We group bitmap updates into batches. Each batch has a number. |
| 29 | * We may write out several batches at once, but that isn't very important. |
| 30 | * conf->bm_write is the number of the last batch successfully written. |
| 31 | * conf->bm_flush is the number of the last batch that was closed to |
| 32 | * new additions. |
| 33 | * When we discover that we will need to write to any block in a stripe |
| 34 | * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq |
| 35 | * the number of the batch it will be in. This is bm_flush+1. |
| 36 | * When we are ready to do a write, if that batch hasn't been written yet, |
| 37 | * we plug the array and queue the stripe for later. |
| 38 | * When an unplug happens, we increment bm_flush, thus closing the current |
| 39 | * batch. |
| 40 | * When we notice that bm_flush > bm_write, we write out all pending updates |
| 41 | * to the bitmap, and advance bm_write to where bm_flush was. |
| 42 | * This may occasionally write a bit out twice, but is sure never to |
| 43 | * miss any bits. |
| 44 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include <linux/module.h> |
| 47 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <linux/highmem.h> |
| 49 | #include <linux/bitops.h> |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 50 | #include <linux/kthread.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include <asm/atomic.h> |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 52 | #include "raid6.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 54 | #include <linux/raid/bitmap.h> |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 55 | #include <linux/async_tx.h> |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | /* |
| 58 | * Stripe cache |
| 59 | */ |
| 60 | |
| 61 | #define NR_STRIPES 256 |
| 62 | #define STRIPE_SIZE PAGE_SIZE |
| 63 | #define STRIPE_SHIFT (PAGE_SHIFT - 9) |
| 64 | #define STRIPE_SECTORS (STRIPE_SIZE>>9) |
| 65 | #define IO_THRESHOLD 1 |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 66 | #define BYPASS_THRESHOLD 1 |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 67 | #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | #define HASH_MASK (NR_HASH - 1) |
| 69 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 70 | #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] | 71 | |
| 72 | /* bio's attached to a stripe+device for I/O are linked together in bi_sector |
| 73 | * order without overlap. There may be several bio's per stripe+device, and |
| 74 | * a bio could span several devices. |
| 75 | * When walking this list for a particular stripe+device, we must never proceed |
| 76 | * beyond a bio that extends past this device, as the next bio might no longer |
| 77 | * be valid. |
| 78 | * This macro is used to determine the 'next' bio in the list, given the sector |
| 79 | * of the current stripe+device |
| 80 | */ |
| 81 | #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL) |
| 82 | /* |
| 83 | * The following can be used to debug the driver |
| 84 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | #define RAID5_PARANOIA 1 |
| 86 | #if RAID5_PARANOIA && defined(CONFIG_SMP) |
| 87 | # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock) |
| 88 | #else |
| 89 | # define CHECK_DEVLOCK() |
| 90 | #endif |
| 91 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 92 | #ifdef DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | #define inline |
| 94 | #define __inline__ |
| 95 | #endif |
| 96 | |
Bernd Schubert | 6be9d49 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 97 | #define printk_rl(args...) ((void) (printk_ratelimit() && printk(args))) |
| 98 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 99 | #if !RAID6_USE_EMPTY_ZERO_PAGE |
| 100 | /* In .bss so it's zeroed */ |
| 101 | const char raid6_empty_zero_page[PAGE_SIZE] __attribute__((aligned(256))); |
| 102 | #endif |
| 103 | |
| 104 | static inline int raid6_next_disk(int disk, int raid_disks) |
| 105 | { |
| 106 | disk++; |
| 107 | return (disk < raid_disks) ? disk : 0; |
| 108 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 109 | |
| 110 | static void return_io(struct bio *return_bi) |
| 111 | { |
| 112 | struct bio *bi = return_bi; |
| 113 | while (bi) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 114 | |
| 115 | return_bi = bi->bi_next; |
| 116 | bi->bi_next = NULL; |
| 117 | bi->bi_size = 0; |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 118 | bio_endio(bi, 0); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 119 | bi = return_bi; |
| 120 | } |
| 121 | } |
| 122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | static void print_raid5_conf (raid5_conf_t *conf); |
| 124 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 125 | static int stripe_operations_active(struct stripe_head *sh) |
| 126 | { |
| 127 | return sh->check_state || sh->reconstruct_state || |
| 128 | test_bit(STRIPE_BIOFILL_RUN, &sh->state) || |
| 129 | test_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 130 | } |
| 131 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 132 | static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | { |
| 134 | if (atomic_dec_and_test(&sh->count)) { |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 135 | BUG_ON(!list_empty(&sh->lru)); |
| 136 | BUG_ON(atomic_read(&conf->active_stripes)==0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | if (test_bit(STRIPE_HANDLE, &sh->state)) { |
NeilBrown | 7c785b7 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 138 | if (test_bit(STRIPE_DELAYED, &sh->state)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | list_add_tail(&sh->lru, &conf->delayed_list); |
NeilBrown | 7c785b7 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 140 | blk_plug_device(conf->mddev->queue); |
| 141 | } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) && |
NeilBrown | ae3c20c | 2006-07-10 04:44:17 -0700 | [diff] [blame] | 142 | sh->bm_seq - conf->seq_write > 0) { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 143 | list_add_tail(&sh->lru, &conf->bitmap_list); |
NeilBrown | 7c785b7 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 144 | blk_plug_device(conf->mddev->queue); |
| 145 | } else { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 146 | clear_bit(STRIPE_BIT_DELAY, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | list_add_tail(&sh->lru, &conf->handle_list); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 148 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | md_wakeup_thread(conf->mddev->thread); |
| 150 | } else { |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 151 | BUG_ON(stripe_operations_active(sh)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
| 153 | atomic_dec(&conf->preread_active_stripes); |
| 154 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) |
| 155 | md_wakeup_thread(conf->mddev->thread); |
| 156 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | atomic_dec(&conf->active_stripes); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 158 | if (!test_bit(STRIPE_EXPANDING, &sh->state)) { |
| 159 | list_add_tail(&sh->lru, &conf->inactive_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | wake_up(&conf->wait_for_stripe); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 161 | if (conf->retry_read_aligned) |
| 162 | md_wakeup_thread(conf->mddev->thread); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 163 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | } |
| 167 | static void release_stripe(struct stripe_head *sh) |
| 168 | { |
| 169 | raid5_conf_t *conf = sh->raid_conf; |
| 170 | unsigned long flags; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 171 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | spin_lock_irqsave(&conf->device_lock, flags); |
| 173 | __release_stripe(conf, sh); |
| 174 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 175 | } |
| 176 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 177 | static inline void remove_hash(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 179 | pr_debug("remove_hash(), stripe %llu\n", |
| 180 | (unsigned long long)sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 182 | hlist_del_init(&sh->hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | } |
| 184 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 185 | 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] | 186 | { |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 187 | struct hlist_head *hp = stripe_hash(conf, sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 189 | pr_debug("insert_hash(), stripe %llu\n", |
| 190 | (unsigned long long)sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
| 192 | CHECK_DEVLOCK(); |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 193 | hlist_add_head(&sh->hash, hp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | |
| 197 | /* find an idle stripe, make sure it is unhashed, and return it. */ |
| 198 | static struct stripe_head *get_free_stripe(raid5_conf_t *conf) |
| 199 | { |
| 200 | struct stripe_head *sh = NULL; |
| 201 | struct list_head *first; |
| 202 | |
| 203 | CHECK_DEVLOCK(); |
| 204 | if (list_empty(&conf->inactive_list)) |
| 205 | goto out; |
| 206 | first = conf->inactive_list.next; |
| 207 | sh = list_entry(first, struct stripe_head, lru); |
| 208 | list_del_init(first); |
| 209 | remove_hash(sh); |
| 210 | atomic_inc(&conf->active_stripes); |
| 211 | out: |
| 212 | return sh; |
| 213 | } |
| 214 | |
| 215 | static void shrink_buffers(struct stripe_head *sh, int num) |
| 216 | { |
| 217 | struct page *p; |
| 218 | int i; |
| 219 | |
| 220 | for (i=0; i<num ; i++) { |
| 221 | p = sh->dev[i].page; |
| 222 | if (!p) |
| 223 | continue; |
| 224 | sh->dev[i].page = NULL; |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 225 | put_page(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
| 229 | static int grow_buffers(struct stripe_head *sh, int num) |
| 230 | { |
| 231 | int i; |
| 232 | |
| 233 | for (i=0; i<num; i++) { |
| 234 | struct page *page; |
| 235 | |
| 236 | if (!(page = alloc_page(GFP_KERNEL))) { |
| 237 | return 1; |
| 238 | } |
| 239 | sh->dev[i].page = page; |
| 240 | } |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | static void raid5_build_block (struct stripe_head *sh, int i); |
| 245 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 246 | 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] | 247 | { |
| 248 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 249 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 251 | BUG_ON(atomic_read(&sh->count) != 0); |
| 252 | BUG_ON(test_bit(STRIPE_HANDLE, &sh->state)); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 253 | BUG_ON(stripe_operations_active(sh)); |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 254 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | CHECK_DEVLOCK(); |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 256 | pr_debug("init_stripe called, stripe %llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | (unsigned long long)sh->sector); |
| 258 | |
| 259 | remove_hash(sh); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 260 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | sh->sector = sector; |
| 262 | sh->pd_idx = pd_idx; |
| 263 | sh->state = 0; |
| 264 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 265 | sh->disks = disks; |
| 266 | |
| 267 | for (i = sh->disks; i--; ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | struct r5dev *dev = &sh->dev[i]; |
| 269 | |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 270 | if (dev->toread || dev->read || dev->towrite || dev->written || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | test_bit(R5_LOCKED, &dev->flags)) { |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 272 | printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | (unsigned long long)sh->sector, i, dev->toread, |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 274 | dev->read, dev->towrite, dev->written, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | test_bit(R5_LOCKED, &dev->flags)); |
| 276 | BUG(); |
| 277 | } |
| 278 | dev->flags = 0; |
| 279 | raid5_build_block(sh, i); |
| 280 | } |
| 281 | insert_hash(conf, sh); |
| 282 | } |
| 283 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 284 | 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] | 285 | { |
| 286 | struct stripe_head *sh; |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 287 | struct hlist_node *hn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | |
| 289 | CHECK_DEVLOCK(); |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 290 | pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector); |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 291 | hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash) |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 292 | if (sh->sector == sector && sh->disks == disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | return sh; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 294 | pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | return NULL; |
| 296 | } |
| 297 | |
| 298 | static void unplug_slaves(mddev_t *mddev); |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 299 | static void raid5_unplug_device(struct request_queue *q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 301 | static struct stripe_head *get_active_stripe(raid5_conf_t *conf, sector_t sector, int disks, |
| 302 | int pd_idx, int noblock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | { |
| 304 | struct stripe_head *sh; |
| 305 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 306 | pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | |
| 308 | spin_lock_irq(&conf->device_lock); |
| 309 | |
| 310 | do { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 311 | wait_event_lock_irq(conf->wait_for_stripe, |
| 312 | conf->quiesce == 0, |
| 313 | conf->device_lock, /* nothing */); |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 314 | sh = __find_stripe(conf, sector, disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | if (!sh) { |
| 316 | if (!conf->inactive_blocked) |
| 317 | sh = get_free_stripe(conf); |
| 318 | if (noblock && sh == NULL) |
| 319 | break; |
| 320 | if (!sh) { |
| 321 | conf->inactive_blocked = 1; |
| 322 | wait_event_lock_irq(conf->wait_for_stripe, |
| 323 | !list_empty(&conf->inactive_list) && |
NeilBrown | 5036805 | 2005-12-12 02:39:17 -0800 | [diff] [blame] | 324 | (atomic_read(&conf->active_stripes) |
| 325 | < (conf->max_nr_stripes *3/4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | || !conf->inactive_blocked), |
| 327 | conf->device_lock, |
NeilBrown | f437078 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 328 | raid5_unplug_device(conf->mddev->queue) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | ); |
| 330 | conf->inactive_blocked = 0; |
| 331 | } else |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 332 | init_stripe(sh, sector, pd_idx, disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | } else { |
| 334 | if (atomic_read(&sh->count)) { |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 335 | BUG_ON(!list_empty(&sh->lru)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | } else { |
| 337 | if (!test_bit(STRIPE_HANDLE, &sh->state)) |
| 338 | atomic_inc(&conf->active_stripes); |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 339 | if (list_empty(&sh->lru) && |
| 340 | !test_bit(STRIPE_EXPANDING, &sh->state)) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 341 | BUG(); |
| 342 | list_del_init(&sh->lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | } while (sh == NULL); |
| 346 | |
| 347 | if (sh) |
| 348 | atomic_inc(&sh->count); |
| 349 | |
| 350 | spin_unlock_irq(&conf->device_lock); |
| 351 | return sh; |
| 352 | } |
| 353 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 354 | static void |
| 355 | raid5_end_read_request(struct bio *bi, int error); |
| 356 | static void |
| 357 | raid5_end_write_request(struct bio *bi, int error); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 358 | |
Dan Williams | c4e5ac0 | 2008-06-28 08:31:53 +1000 | [diff] [blame] | 359 | static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 360 | { |
| 361 | raid5_conf_t *conf = sh->raid_conf; |
| 362 | int i, disks = sh->disks; |
| 363 | |
| 364 | might_sleep(); |
| 365 | |
| 366 | for (i = disks; i--; ) { |
| 367 | int rw; |
| 368 | struct bio *bi; |
| 369 | mdk_rdev_t *rdev; |
| 370 | if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) |
| 371 | rw = WRITE; |
| 372 | else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags)) |
| 373 | rw = READ; |
| 374 | else |
| 375 | continue; |
| 376 | |
| 377 | bi = &sh->dev[i].req; |
| 378 | |
| 379 | bi->bi_rw = rw; |
| 380 | if (rw == WRITE) |
| 381 | bi->bi_end_io = raid5_end_write_request; |
| 382 | else |
| 383 | bi->bi_end_io = raid5_end_read_request; |
| 384 | |
| 385 | rcu_read_lock(); |
| 386 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 387 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 388 | rdev = NULL; |
| 389 | if (rdev) |
| 390 | atomic_inc(&rdev->nr_pending); |
| 391 | rcu_read_unlock(); |
| 392 | |
| 393 | if (rdev) { |
Dan Williams | c4e5ac0 | 2008-06-28 08:31:53 +1000 | [diff] [blame] | 394 | if (s->syncing || s->expanding || s->expanded) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 395 | md_sync_acct(rdev->bdev, STRIPE_SECTORS); |
| 396 | |
Dan Williams | 2b7497f | 2008-06-28 08:31:52 +1000 | [diff] [blame] | 397 | set_bit(STRIPE_IO_STARTED, &sh->state); |
| 398 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 399 | bi->bi_bdev = rdev->bdev; |
| 400 | pr_debug("%s: for %llu schedule op %ld on disc %d\n", |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 401 | __func__, (unsigned long long)sh->sector, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 402 | bi->bi_rw, i); |
| 403 | atomic_inc(&sh->count); |
| 404 | bi->bi_sector = sh->sector + rdev->data_offset; |
| 405 | bi->bi_flags = 1 << BIO_UPTODATE; |
| 406 | bi->bi_vcnt = 1; |
| 407 | bi->bi_max_vecs = 1; |
| 408 | bi->bi_idx = 0; |
| 409 | bi->bi_io_vec = &sh->dev[i].vec; |
| 410 | bi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
| 411 | bi->bi_io_vec[0].bv_offset = 0; |
| 412 | bi->bi_size = STRIPE_SIZE; |
| 413 | bi->bi_next = NULL; |
| 414 | if (rw == WRITE && |
| 415 | test_bit(R5_ReWrite, &sh->dev[i].flags)) |
| 416 | atomic_add(STRIPE_SECTORS, |
| 417 | &rdev->corrected_errors); |
| 418 | generic_make_request(bi); |
| 419 | } else { |
| 420 | if (rw == WRITE) |
| 421 | set_bit(STRIPE_DEGRADED, &sh->state); |
| 422 | pr_debug("skip op %ld on disc %d for sector %llu\n", |
| 423 | bi->bi_rw, i, (unsigned long long)sh->sector); |
| 424 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 425 | set_bit(STRIPE_HANDLE, &sh->state); |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | static struct dma_async_tx_descriptor * |
| 431 | async_copy_data(int frombio, struct bio *bio, struct page *page, |
| 432 | sector_t sector, struct dma_async_tx_descriptor *tx) |
| 433 | { |
| 434 | struct bio_vec *bvl; |
| 435 | struct page *bio_page; |
| 436 | int i; |
| 437 | int page_offset; |
| 438 | |
| 439 | if (bio->bi_sector >= sector) |
| 440 | page_offset = (signed)(bio->bi_sector - sector) * 512; |
| 441 | else |
| 442 | page_offset = (signed)(sector - bio->bi_sector) * -512; |
| 443 | bio_for_each_segment(bvl, bio, i) { |
| 444 | int len = bio_iovec_idx(bio, i)->bv_len; |
| 445 | int clen; |
| 446 | int b_offset = 0; |
| 447 | |
| 448 | if (page_offset < 0) { |
| 449 | b_offset = -page_offset; |
| 450 | page_offset += b_offset; |
| 451 | len -= b_offset; |
| 452 | } |
| 453 | |
| 454 | if (len > 0 && page_offset + len > STRIPE_SIZE) |
| 455 | clen = STRIPE_SIZE - page_offset; |
| 456 | else |
| 457 | clen = len; |
| 458 | |
| 459 | if (clen > 0) { |
| 460 | b_offset += bio_iovec_idx(bio, i)->bv_offset; |
| 461 | bio_page = bio_iovec_idx(bio, i)->bv_page; |
| 462 | if (frombio) |
| 463 | tx = async_memcpy(page, bio_page, page_offset, |
| 464 | b_offset, clen, |
Dan Williams | eb0645a | 2007-07-20 00:31:46 -0700 | [diff] [blame] | 465 | ASYNC_TX_DEP_ACK, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 466 | tx, NULL, NULL); |
| 467 | else |
| 468 | tx = async_memcpy(bio_page, page, b_offset, |
| 469 | page_offset, clen, |
Dan Williams | eb0645a | 2007-07-20 00:31:46 -0700 | [diff] [blame] | 470 | ASYNC_TX_DEP_ACK, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 471 | tx, NULL, NULL); |
| 472 | } |
| 473 | if (clen < len) /* hit end of page */ |
| 474 | break; |
| 475 | page_offset += len; |
| 476 | } |
| 477 | |
| 478 | return tx; |
| 479 | } |
| 480 | |
| 481 | static void ops_complete_biofill(void *stripe_head_ref) |
| 482 | { |
| 483 | struct stripe_head *sh = stripe_head_ref; |
| 484 | struct bio *return_bi = NULL; |
| 485 | raid5_conf_t *conf = sh->raid_conf; |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 486 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 487 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 488 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 489 | (unsigned long long)sh->sector); |
| 490 | |
| 491 | /* clear completed biofills */ |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 492 | spin_lock_irq(&conf->device_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 493 | for (i = sh->disks; i--; ) { |
| 494 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 495 | |
| 496 | /* acknowledge completion of a biofill operation */ |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 497 | /* and check if we need to reply to a read request, |
| 498 | * new R5_Wantfill requests are held off until |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 499 | * !STRIPE_BIOFILL_RUN |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 500 | */ |
| 501 | if (test_and_clear_bit(R5_Wantfill, &dev->flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 502 | struct bio *rbi, *rbi2; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 503 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 504 | BUG_ON(!dev->read); |
| 505 | rbi = dev->read; |
| 506 | dev->read = NULL; |
| 507 | while (rbi && rbi->bi_sector < |
| 508 | dev->sector + STRIPE_SECTORS) { |
| 509 | rbi2 = r5_next_bio(rbi, dev->sector); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 510 | if (--rbi->bi_phys_segments == 0) { |
| 511 | rbi->bi_next = return_bi; |
| 512 | return_bi = rbi; |
| 513 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 514 | rbi = rbi2; |
| 515 | } |
| 516 | } |
| 517 | } |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 518 | spin_unlock_irq(&conf->device_lock); |
| 519 | clear_bit(STRIPE_BIOFILL_RUN, &sh->state); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 520 | |
| 521 | return_io(return_bi); |
| 522 | |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 523 | set_bit(STRIPE_HANDLE, &sh->state); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 524 | release_stripe(sh); |
| 525 | } |
| 526 | |
| 527 | static void ops_run_biofill(struct stripe_head *sh) |
| 528 | { |
| 529 | struct dma_async_tx_descriptor *tx = NULL; |
| 530 | raid5_conf_t *conf = sh->raid_conf; |
| 531 | int i; |
| 532 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 533 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 534 | (unsigned long long)sh->sector); |
| 535 | |
| 536 | for (i = sh->disks; i--; ) { |
| 537 | struct r5dev *dev = &sh->dev[i]; |
| 538 | if (test_bit(R5_Wantfill, &dev->flags)) { |
| 539 | struct bio *rbi; |
| 540 | spin_lock_irq(&conf->device_lock); |
| 541 | dev->read = rbi = dev->toread; |
| 542 | dev->toread = NULL; |
| 543 | spin_unlock_irq(&conf->device_lock); |
| 544 | while (rbi && rbi->bi_sector < |
| 545 | dev->sector + STRIPE_SECTORS) { |
| 546 | tx = async_copy_data(0, rbi, dev->page, |
| 547 | dev->sector, tx); |
| 548 | rbi = r5_next_bio(rbi, dev->sector); |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | atomic_inc(&sh->count); |
| 554 | async_trigger_callback(ASYNC_TX_DEP_ACK | ASYNC_TX_ACK, tx, |
| 555 | ops_complete_biofill, sh); |
| 556 | } |
| 557 | |
| 558 | static void ops_complete_compute5(void *stripe_head_ref) |
| 559 | { |
| 560 | struct stripe_head *sh = stripe_head_ref; |
| 561 | int target = sh->ops.target; |
| 562 | struct r5dev *tgt = &sh->dev[target]; |
| 563 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 564 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 565 | (unsigned long long)sh->sector); |
| 566 | |
| 567 | set_bit(R5_UPTODATE, &tgt->flags); |
| 568 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 569 | clear_bit(R5_Wantcompute, &tgt->flags); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 570 | clear_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 571 | if (sh->check_state == check_state_compute_run) |
| 572 | sh->check_state = check_state_compute_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 573 | set_bit(STRIPE_HANDLE, &sh->state); |
| 574 | release_stripe(sh); |
| 575 | } |
| 576 | |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 577 | static struct dma_async_tx_descriptor *ops_run_compute5(struct stripe_head *sh) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 578 | { |
| 579 | /* kernel stack size limits the total number of disks */ |
| 580 | int disks = sh->disks; |
| 581 | struct page *xor_srcs[disks]; |
| 582 | int target = sh->ops.target; |
| 583 | struct r5dev *tgt = &sh->dev[target]; |
| 584 | struct page *xor_dest = tgt->page; |
| 585 | int count = 0; |
| 586 | struct dma_async_tx_descriptor *tx; |
| 587 | int i; |
| 588 | |
| 589 | pr_debug("%s: stripe %llu block: %d\n", |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 590 | __func__, (unsigned long long)sh->sector, target); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 591 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 592 | |
| 593 | for (i = disks; i--; ) |
| 594 | if (i != target) |
| 595 | xor_srcs[count++] = sh->dev[i].page; |
| 596 | |
| 597 | atomic_inc(&sh->count); |
| 598 | |
| 599 | if (unlikely(count == 1)) |
| 600 | tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, |
| 601 | 0, NULL, ops_complete_compute5, sh); |
| 602 | else |
| 603 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, |
| 604 | ASYNC_TX_XOR_ZERO_DST, NULL, |
| 605 | ops_complete_compute5, sh); |
| 606 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 607 | return tx; |
| 608 | } |
| 609 | |
| 610 | static void ops_complete_prexor(void *stripe_head_ref) |
| 611 | { |
| 612 | struct stripe_head *sh = stripe_head_ref; |
| 613 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 614 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 615 | (unsigned long long)sh->sector); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | static struct dma_async_tx_descriptor * |
| 619 | ops_run_prexor(struct stripe_head *sh, struct dma_async_tx_descriptor *tx) |
| 620 | { |
| 621 | /* kernel stack size limits the total number of disks */ |
| 622 | int disks = sh->disks; |
| 623 | struct page *xor_srcs[disks]; |
| 624 | int count = 0, pd_idx = sh->pd_idx, i; |
| 625 | |
| 626 | /* existing parity data subtracted */ |
| 627 | struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 628 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 629 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 630 | (unsigned long long)sh->sector); |
| 631 | |
| 632 | for (i = disks; i--; ) { |
| 633 | struct r5dev *dev = &sh->dev[i]; |
| 634 | /* Only process blocks that are known to be uptodate */ |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 635 | if (test_bit(R5_Wantdrain, &dev->flags)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 636 | xor_srcs[count++] = dev->page; |
| 637 | } |
| 638 | |
| 639 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, |
| 640 | ASYNC_TX_DEP_ACK | ASYNC_TX_XOR_DROP_DST, tx, |
| 641 | ops_complete_prexor, sh); |
| 642 | |
| 643 | return tx; |
| 644 | } |
| 645 | |
| 646 | static struct dma_async_tx_descriptor * |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 647 | ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 648 | { |
| 649 | int disks = sh->disks; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 650 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 651 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 652 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 653 | (unsigned long long)sh->sector); |
| 654 | |
| 655 | for (i = disks; i--; ) { |
| 656 | struct r5dev *dev = &sh->dev[i]; |
| 657 | struct bio *chosen; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 658 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 659 | if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 660 | struct bio *wbi; |
| 661 | |
| 662 | spin_lock(&sh->lock); |
| 663 | chosen = dev->towrite; |
| 664 | dev->towrite = NULL; |
| 665 | BUG_ON(dev->written); |
| 666 | wbi = dev->written = chosen; |
| 667 | spin_unlock(&sh->lock); |
| 668 | |
| 669 | while (wbi && wbi->bi_sector < |
| 670 | dev->sector + STRIPE_SECTORS) { |
| 671 | tx = async_copy_data(1, wbi, dev->page, |
| 672 | dev->sector, tx); |
| 673 | wbi = r5_next_bio(wbi, dev->sector); |
| 674 | } |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | return tx; |
| 679 | } |
| 680 | |
| 681 | static void ops_complete_postxor(void *stripe_head_ref) |
| 682 | { |
| 683 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 684 | int disks = sh->disks, i, pd_idx = sh->pd_idx; |
| 685 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 686 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 687 | (unsigned long long)sh->sector); |
| 688 | |
| 689 | for (i = disks; i--; ) { |
| 690 | struct r5dev *dev = &sh->dev[i]; |
| 691 | if (dev->written || i == pd_idx) |
| 692 | set_bit(R5_UPTODATE, &dev->flags); |
| 693 | } |
| 694 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 695 | if (sh->reconstruct_state == reconstruct_state_drain_run) |
| 696 | sh->reconstruct_state = reconstruct_state_drain_result; |
| 697 | else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) |
| 698 | sh->reconstruct_state = reconstruct_state_prexor_drain_result; |
| 699 | else { |
| 700 | BUG_ON(sh->reconstruct_state != reconstruct_state_run); |
| 701 | sh->reconstruct_state = reconstruct_state_result; |
| 702 | } |
| 703 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 704 | set_bit(STRIPE_HANDLE, &sh->state); |
| 705 | release_stripe(sh); |
| 706 | } |
| 707 | |
| 708 | static void |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 709 | ops_run_postxor(struct stripe_head *sh, struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 710 | { |
| 711 | /* kernel stack size limits the total number of disks */ |
| 712 | int disks = sh->disks; |
| 713 | struct page *xor_srcs[disks]; |
| 714 | |
| 715 | int count = 0, pd_idx = sh->pd_idx, i; |
| 716 | struct page *xor_dest; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 717 | int prexor = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 718 | unsigned long flags; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 719 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 720 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 721 | (unsigned long long)sh->sector); |
| 722 | |
| 723 | /* check if prexor is active which means only process blocks |
| 724 | * that are part of a read-modify-write (written) |
| 725 | */ |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 726 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) { |
| 727 | prexor = 1; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 728 | xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 729 | for (i = disks; i--; ) { |
| 730 | struct r5dev *dev = &sh->dev[i]; |
| 731 | if (dev->written) |
| 732 | xor_srcs[count++] = dev->page; |
| 733 | } |
| 734 | } else { |
| 735 | xor_dest = sh->dev[pd_idx].page; |
| 736 | for (i = disks; i--; ) { |
| 737 | struct r5dev *dev = &sh->dev[i]; |
| 738 | if (i != pd_idx) |
| 739 | xor_srcs[count++] = dev->page; |
| 740 | } |
| 741 | } |
| 742 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 743 | /* 1/ if we prexor'd then the dest is reused as a source |
| 744 | * 2/ if we did not prexor then we are redoing the parity |
| 745 | * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST |
| 746 | * for the synchronous xor case |
| 747 | */ |
| 748 | flags = ASYNC_TX_DEP_ACK | ASYNC_TX_ACK | |
| 749 | (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST); |
| 750 | |
| 751 | atomic_inc(&sh->count); |
| 752 | |
| 753 | if (unlikely(count == 1)) { |
| 754 | flags &= ~(ASYNC_TX_XOR_DROP_DST | ASYNC_TX_XOR_ZERO_DST); |
| 755 | tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 756 | flags, tx, ops_complete_postxor, sh); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 757 | } else |
| 758 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 759 | flags, tx, ops_complete_postxor, sh); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | static void ops_complete_check(void *stripe_head_ref) |
| 763 | { |
| 764 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 765 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 766 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 767 | (unsigned long long)sh->sector); |
| 768 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 769 | sh->check_state = check_state_check_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 770 | set_bit(STRIPE_HANDLE, &sh->state); |
| 771 | release_stripe(sh); |
| 772 | } |
| 773 | |
| 774 | static void ops_run_check(struct stripe_head *sh) |
| 775 | { |
| 776 | /* kernel stack size limits the total number of disks */ |
| 777 | int disks = sh->disks; |
| 778 | struct page *xor_srcs[disks]; |
| 779 | struct dma_async_tx_descriptor *tx; |
| 780 | |
| 781 | int count = 0, pd_idx = sh->pd_idx, i; |
| 782 | struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 783 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 784 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 785 | (unsigned long long)sh->sector); |
| 786 | |
| 787 | for (i = disks; i--; ) { |
| 788 | struct r5dev *dev = &sh->dev[i]; |
| 789 | if (i != pd_idx) |
| 790 | xor_srcs[count++] = dev->page; |
| 791 | } |
| 792 | |
| 793 | tx = async_xor_zero_sum(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, |
| 794 | &sh->ops.zero_sum_result, 0, NULL, NULL, NULL); |
| 795 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 796 | atomic_inc(&sh->count); |
| 797 | tx = async_trigger_callback(ASYNC_TX_DEP_ACK | ASYNC_TX_ACK, tx, |
| 798 | ops_complete_check, sh); |
| 799 | } |
| 800 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 801 | static void raid5_run_ops(struct stripe_head *sh, unsigned long ops_request) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 802 | { |
| 803 | int overlap_clear = 0, i, disks = sh->disks; |
| 804 | struct dma_async_tx_descriptor *tx = NULL; |
| 805 | |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 806 | if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 807 | ops_run_biofill(sh); |
| 808 | overlap_clear++; |
| 809 | } |
| 810 | |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 811 | if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) { |
| 812 | tx = ops_run_compute5(sh); |
| 813 | /* terminate the chain if postxor is not set to be run */ |
| 814 | if (tx && !test_bit(STRIPE_OP_POSTXOR, &ops_request)) |
| 815 | async_tx_ack(tx); |
| 816 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 817 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 818 | if (test_bit(STRIPE_OP_PREXOR, &ops_request)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 819 | tx = ops_run_prexor(sh, tx); |
| 820 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 821 | if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) { |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 822 | tx = ops_run_biodrain(sh, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 823 | overlap_clear++; |
| 824 | } |
| 825 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 826 | if (test_bit(STRIPE_OP_POSTXOR, &ops_request)) |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 827 | ops_run_postxor(sh, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 828 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 829 | if (test_bit(STRIPE_OP_CHECK, &ops_request)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 830 | ops_run_check(sh); |
| 831 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 832 | if (overlap_clear) |
| 833 | for (i = disks; i--; ) { |
| 834 | struct r5dev *dev = &sh->dev[i]; |
| 835 | if (test_and_clear_bit(R5_Overlap, &dev->flags)) |
| 836 | wake_up(&sh->raid_conf->wait_for_overlap); |
| 837 | } |
| 838 | } |
| 839 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 840 | static int grow_one_stripe(raid5_conf_t *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | { |
| 842 | struct stripe_head *sh; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 843 | sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL); |
| 844 | if (!sh) |
| 845 | return 0; |
| 846 | memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev)); |
| 847 | sh->raid_conf = conf; |
| 848 | spin_lock_init(&sh->lock); |
| 849 | |
| 850 | if (grow_buffers(sh, conf->raid_disks)) { |
| 851 | shrink_buffers(sh, conf->raid_disks); |
| 852 | kmem_cache_free(conf->slab_cache, sh); |
| 853 | return 0; |
| 854 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 855 | sh->disks = conf->raid_disks; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 856 | /* we just created an active stripe so... */ |
| 857 | atomic_set(&sh->count, 1); |
| 858 | atomic_inc(&conf->active_stripes); |
| 859 | INIT_LIST_HEAD(&sh->lru); |
| 860 | release_stripe(sh); |
| 861 | return 1; |
| 862 | } |
| 863 | |
| 864 | static int grow_stripes(raid5_conf_t *conf, int num) |
| 865 | { |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 866 | struct kmem_cache *sc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | int devs = conf->raid_disks; |
| 868 | |
NeilBrown | 42b9beb | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 869 | sprintf(conf->cache_name[0], "raid5-%s", mdname(conf->mddev)); |
| 870 | sprintf(conf->cache_name[1], "raid5-%s-alt", mdname(conf->mddev)); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 871 | conf->active_name = 0; |
| 872 | sc = kmem_cache_create(conf->cache_name[conf->active_name], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 874 | 0, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | if (!sc) |
| 876 | return 1; |
| 877 | conf->slab_cache = sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 878 | conf->pool_size = devs; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 879 | while (num--) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 880 | if (!grow_one_stripe(conf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | return 0; |
| 883 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 884 | |
| 885 | #ifdef CONFIG_MD_RAID5_RESHAPE |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 886 | static int resize_stripes(raid5_conf_t *conf, int newsize) |
| 887 | { |
| 888 | /* Make all the stripes able to hold 'newsize' devices. |
| 889 | * New slots in each stripe get 'page' set to a new page. |
| 890 | * |
| 891 | * This happens in stages: |
| 892 | * 1/ create a new kmem_cache and allocate the required number of |
| 893 | * stripe_heads. |
| 894 | * 2/ gather all the old stripe_heads and tranfer the pages across |
| 895 | * to the new stripe_heads. This will have the side effect of |
| 896 | * freezing the array as once all stripe_heads have been collected, |
| 897 | * no IO will be possible. Old stripe heads are freed once their |
| 898 | * pages have been transferred over, and the old kmem_cache is |
| 899 | * freed when all stripes are done. |
| 900 | * 3/ reallocate conf->disks to be suitable bigger. If this fails, |
| 901 | * we simple return a failre status - no need to clean anything up. |
| 902 | * 4/ allocate new pages for the new slots in the new stripe_heads. |
| 903 | * If this fails, we don't bother trying the shrink the |
| 904 | * stripe_heads down again, we just leave them as they are. |
| 905 | * As each stripe_head is processed the new one is released into |
| 906 | * active service. |
| 907 | * |
| 908 | * Once step2 is started, we cannot afford to wait for a write, |
| 909 | * so we use GFP_NOIO allocations. |
| 910 | */ |
| 911 | struct stripe_head *osh, *nsh; |
| 912 | LIST_HEAD(newstripes); |
| 913 | struct disk_info *ndisks; |
| 914 | int err = 0; |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 915 | struct kmem_cache *sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 916 | int i; |
| 917 | |
| 918 | if (newsize <= conf->pool_size) |
| 919 | return 0; /* never bother to shrink */ |
| 920 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 921 | md_allow_write(conf->mddev); |
| 922 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 923 | /* Step 1 */ |
| 924 | sc = kmem_cache_create(conf->cache_name[1-conf->active_name], |
| 925 | sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 926 | 0, 0, NULL); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 927 | if (!sc) |
| 928 | return -ENOMEM; |
| 929 | |
| 930 | for (i = conf->max_nr_stripes; i; i--) { |
| 931 | nsh = kmem_cache_alloc(sc, GFP_KERNEL); |
| 932 | if (!nsh) |
| 933 | break; |
| 934 | |
| 935 | memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev)); |
| 936 | |
| 937 | nsh->raid_conf = conf; |
| 938 | spin_lock_init(&nsh->lock); |
| 939 | |
| 940 | list_add(&nsh->lru, &newstripes); |
| 941 | } |
| 942 | if (i) { |
| 943 | /* didn't get enough, give up */ |
| 944 | while (!list_empty(&newstripes)) { |
| 945 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 946 | list_del(&nsh->lru); |
| 947 | kmem_cache_free(sc, nsh); |
| 948 | } |
| 949 | kmem_cache_destroy(sc); |
| 950 | return -ENOMEM; |
| 951 | } |
| 952 | /* Step 2 - Must use GFP_NOIO now. |
| 953 | * OK, we have enough stripes, start collecting inactive |
| 954 | * stripes and copying them over |
| 955 | */ |
| 956 | list_for_each_entry(nsh, &newstripes, lru) { |
| 957 | spin_lock_irq(&conf->device_lock); |
| 958 | wait_event_lock_irq(conf->wait_for_stripe, |
| 959 | !list_empty(&conf->inactive_list), |
| 960 | conf->device_lock, |
NeilBrown | b3b46be | 2006-03-27 01:18:16 -0800 | [diff] [blame] | 961 | unplug_slaves(conf->mddev) |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 962 | ); |
| 963 | osh = get_free_stripe(conf); |
| 964 | spin_unlock_irq(&conf->device_lock); |
| 965 | atomic_set(&nsh->count, 1); |
| 966 | for(i=0; i<conf->pool_size; i++) |
| 967 | nsh->dev[i].page = osh->dev[i].page; |
| 968 | for( ; i<newsize; i++) |
| 969 | nsh->dev[i].page = NULL; |
| 970 | kmem_cache_free(conf->slab_cache, osh); |
| 971 | } |
| 972 | kmem_cache_destroy(conf->slab_cache); |
| 973 | |
| 974 | /* Step 3. |
| 975 | * At this point, we are holding all the stripes so the array |
| 976 | * is completely stalled, so now is a good time to resize |
| 977 | * conf->disks. |
| 978 | */ |
| 979 | ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO); |
| 980 | if (ndisks) { |
| 981 | for (i=0; i<conf->raid_disks; i++) |
| 982 | ndisks[i] = conf->disks[i]; |
| 983 | kfree(conf->disks); |
| 984 | conf->disks = ndisks; |
| 985 | } else |
| 986 | err = -ENOMEM; |
| 987 | |
| 988 | /* Step 4, return new stripes to service */ |
| 989 | while(!list_empty(&newstripes)) { |
| 990 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 991 | list_del_init(&nsh->lru); |
| 992 | for (i=conf->raid_disks; i < newsize; i++) |
| 993 | if (nsh->dev[i].page == NULL) { |
| 994 | struct page *p = alloc_page(GFP_NOIO); |
| 995 | nsh->dev[i].page = p; |
| 996 | if (!p) |
| 997 | err = -ENOMEM; |
| 998 | } |
| 999 | release_stripe(nsh); |
| 1000 | } |
| 1001 | /* critical section pass, GFP_NOIO no longer needed */ |
| 1002 | |
| 1003 | conf->slab_cache = sc; |
| 1004 | conf->active_name = 1-conf->active_name; |
| 1005 | conf->pool_size = newsize; |
| 1006 | return err; |
| 1007 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 1008 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1010 | static int drop_one_stripe(raid5_conf_t *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | { |
| 1012 | struct stripe_head *sh; |
| 1013 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1014 | spin_lock_irq(&conf->device_lock); |
| 1015 | sh = get_free_stripe(conf); |
| 1016 | spin_unlock_irq(&conf->device_lock); |
| 1017 | if (!sh) |
| 1018 | return 0; |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 1019 | BUG_ON(atomic_read(&sh->count)); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1020 | shrink_buffers(sh, conf->pool_size); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1021 | kmem_cache_free(conf->slab_cache, sh); |
| 1022 | atomic_dec(&conf->active_stripes); |
| 1023 | return 1; |
| 1024 | } |
| 1025 | |
| 1026 | static void shrink_stripes(raid5_conf_t *conf) |
| 1027 | { |
| 1028 | while (drop_one_stripe(conf)) |
| 1029 | ; |
| 1030 | |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1031 | if (conf->slab_cache) |
| 1032 | kmem_cache_destroy(conf->slab_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | conf->slab_cache = NULL; |
| 1034 | } |
| 1035 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1036 | static void raid5_end_read_request(struct bio * bi, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | { |
| 1038 | struct stripe_head *sh = bi->bi_private; |
| 1039 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 1040 | int disks = sh->disks, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1042 | char b[BDEVNAME_SIZE]; |
| 1043 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | |
| 1046 | for (i=0 ; i<disks; i++) |
| 1047 | if (bi == &sh->dev[i].req) |
| 1048 | break; |
| 1049 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1050 | pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n", |
| 1051 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | uptodate); |
| 1053 | if (i == disks) { |
| 1054 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1055 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | if (uptodate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | set_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1060 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1061 | rdev = conf->disks[i].rdev; |
Bernd Schubert | 6be9d49 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 1062 | printk_rl(KERN_INFO "raid5:%s: read error corrected" |
| 1063 | " (%lu sectors at %llu on %s)\n", |
| 1064 | mdname(conf->mddev), STRIPE_SECTORS, |
| 1065 | (unsigned long long)(sh->sector |
| 1066 | + rdev->data_offset), |
| 1067 | bdevname(rdev->bdev, b)); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1068 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 1069 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
| 1070 | } |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1071 | if (atomic_read(&conf->disks[i].rdev->read_errors)) |
| 1072 | atomic_set(&conf->disks[i].rdev->read_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | } else { |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1074 | const char *bdn = bdevname(conf->disks[i].rdev->bdev, b); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1075 | int retry = 0; |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1076 | rdev = conf->disks[i].rdev; |
| 1077 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | clear_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1079 | atomic_inc(&rdev->read_errors); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1080 | if (conf->mddev->degraded) |
Bernd Schubert | 6be9d49 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 1081 | printk_rl(KERN_WARNING |
| 1082 | "raid5:%s: read error not correctable " |
| 1083 | "(sector %llu on %s).\n", |
| 1084 | mdname(conf->mddev), |
| 1085 | (unsigned long long)(sh->sector |
| 1086 | + rdev->data_offset), |
| 1087 | bdn); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1088 | else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1089 | /* Oh, no!!! */ |
Bernd Schubert | 6be9d49 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 1090 | printk_rl(KERN_WARNING |
| 1091 | "raid5:%s: read error NOT corrected!! " |
| 1092 | "(sector %llu on %s).\n", |
| 1093 | mdname(conf->mddev), |
| 1094 | (unsigned long long)(sh->sector |
| 1095 | + rdev->data_offset), |
| 1096 | bdn); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1097 | else if (atomic_read(&rdev->read_errors) |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1098 | > conf->max_nr_stripes) |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 1099 | printk(KERN_WARNING |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1100 | "raid5:%s: Too many read errors, failing device %s.\n", |
| 1101 | mdname(conf->mddev), bdn); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1102 | else |
| 1103 | retry = 1; |
| 1104 | if (retry) |
| 1105 | set_bit(R5_ReadError, &sh->dev[i].flags); |
| 1106 | else { |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1107 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 1108 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1109 | md_error(conf->mddev, rdev); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1110 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | } |
| 1112 | rdev_dec_pending(conf->disks[i].rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 1114 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1115 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | } |
| 1117 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1118 | static void raid5_end_write_request (struct bio *bi, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | { |
| 1120 | struct stripe_head *sh = bi->bi_private; |
| 1121 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 1122 | int disks = sh->disks, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
| 1124 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | for (i=0 ; i<disks; i++) |
| 1126 | if (bi == &sh->dev[i].req) |
| 1127 | break; |
| 1128 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1129 | pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
| 1131 | uptodate); |
| 1132 | if (i == disks) { |
| 1133 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1134 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | } |
| 1136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | if (!uptodate) |
| 1138 | md_error(conf->mddev, conf->disks[i].rdev); |
| 1139 | |
| 1140 | rdev_dec_pending(conf->disks[i].rdev, conf->mddev); |
| 1141 | |
| 1142 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 1143 | set_bit(STRIPE_HANDLE, &sh->state); |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1144 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | |
| 1148 | static sector_t compute_blocknr(struct stripe_head *sh, int i); |
| 1149 | |
| 1150 | static void raid5_build_block (struct stripe_head *sh, int i) |
| 1151 | { |
| 1152 | struct r5dev *dev = &sh->dev[i]; |
| 1153 | |
| 1154 | bio_init(&dev->req); |
| 1155 | dev->req.bi_io_vec = &dev->vec; |
| 1156 | dev->req.bi_vcnt++; |
| 1157 | dev->req.bi_max_vecs++; |
| 1158 | dev->vec.bv_page = dev->page; |
| 1159 | dev->vec.bv_len = STRIPE_SIZE; |
| 1160 | dev->vec.bv_offset = 0; |
| 1161 | |
| 1162 | dev->req.bi_sector = sh->sector; |
| 1163 | dev->req.bi_private = sh; |
| 1164 | |
| 1165 | dev->flags = 0; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1166 | dev->sector = compute_blocknr(sh, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | static void error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1170 | { |
| 1171 | char b[BDEVNAME_SIZE]; |
| 1172 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1173 | pr_debug("raid5: error called\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1175 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1176 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1177 | if (test_and_clear_bit(In_sync, &rdev->flags)) { |
| 1178 | unsigned long flags; |
| 1179 | spin_lock_irqsave(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | mddev->degraded++; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1181 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | /* |
| 1183 | * if recovery was running, make sure it aborts. |
| 1184 | */ |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 1185 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1187 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | printk (KERN_ALERT |
Nick Andrew | d7a420c | 2008-04-28 02:15:55 -0700 | [diff] [blame] | 1189 | "raid5: Disk failure on %s, disabling device.\n" |
| 1190 | "raid5: Operation continuing on %d devices.\n", |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 1191 | bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1193 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | |
| 1195 | /* |
| 1196 | * Input: a 'big' sector number, |
| 1197 | * Output: index of the data and parity disk, and the sector # in them. |
| 1198 | */ |
| 1199 | static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks, |
| 1200 | unsigned int data_disks, unsigned int * dd_idx, |
| 1201 | unsigned int * pd_idx, raid5_conf_t *conf) |
| 1202 | { |
| 1203 | long stripe; |
| 1204 | unsigned long chunk_number; |
| 1205 | unsigned int chunk_offset; |
| 1206 | sector_t new_sector; |
| 1207 | int sectors_per_chunk = conf->chunk_size >> 9; |
| 1208 | |
| 1209 | /* First compute the information on this sector */ |
| 1210 | |
| 1211 | /* |
| 1212 | * Compute the chunk number and the sector offset inside the chunk |
| 1213 | */ |
| 1214 | chunk_offset = sector_div(r_sector, sectors_per_chunk); |
| 1215 | chunk_number = r_sector; |
| 1216 | BUG_ON(r_sector != chunk_number); |
| 1217 | |
| 1218 | /* |
| 1219 | * Compute the stripe number |
| 1220 | */ |
| 1221 | stripe = chunk_number / data_disks; |
| 1222 | |
| 1223 | /* |
| 1224 | * Compute the data disk and parity disk indexes inside the stripe |
| 1225 | */ |
| 1226 | *dd_idx = chunk_number % data_disks; |
| 1227 | |
| 1228 | /* |
| 1229 | * Select the parity disk based on the user selected algorithm. |
| 1230 | */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1231 | switch(conf->level) { |
| 1232 | case 4: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | *pd_idx = data_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1234 | break; |
| 1235 | case 5: |
| 1236 | switch (conf->algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 1238 | *pd_idx = data_disks - stripe % raid_disks; |
| 1239 | if (*dd_idx >= *pd_idx) |
| 1240 | (*dd_idx)++; |
| 1241 | break; |
| 1242 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 1243 | *pd_idx = stripe % raid_disks; |
| 1244 | if (*dd_idx >= *pd_idx) |
| 1245 | (*dd_idx)++; |
| 1246 | break; |
| 1247 | case ALGORITHM_LEFT_SYMMETRIC: |
| 1248 | *pd_idx = data_disks - stripe % raid_disks; |
| 1249 | *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks; |
| 1250 | break; |
| 1251 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 1252 | *pd_idx = stripe % raid_disks; |
| 1253 | *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks; |
| 1254 | break; |
| 1255 | default: |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 1256 | printk(KERN_ERR "raid5: unsupported algorithm %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | conf->algorithm); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1258 | } |
| 1259 | break; |
| 1260 | case 6: |
| 1261 | |
| 1262 | /**** FIX THIS ****/ |
| 1263 | switch (conf->algorithm) { |
| 1264 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 1265 | *pd_idx = raid_disks - 1 - (stripe % raid_disks); |
| 1266 | if (*pd_idx == raid_disks-1) |
| 1267 | (*dd_idx)++; /* Q D D D P */ |
| 1268 | else if (*dd_idx >= *pd_idx) |
| 1269 | (*dd_idx) += 2; /* D D P Q D */ |
| 1270 | break; |
| 1271 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 1272 | *pd_idx = stripe % raid_disks; |
| 1273 | if (*pd_idx == raid_disks-1) |
| 1274 | (*dd_idx)++; /* Q D D D P */ |
| 1275 | else if (*dd_idx >= *pd_idx) |
| 1276 | (*dd_idx) += 2; /* D D P Q D */ |
| 1277 | break; |
| 1278 | case ALGORITHM_LEFT_SYMMETRIC: |
| 1279 | *pd_idx = raid_disks - 1 - (stripe % raid_disks); |
| 1280 | *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks; |
| 1281 | break; |
| 1282 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 1283 | *pd_idx = stripe % raid_disks; |
| 1284 | *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks; |
| 1285 | break; |
| 1286 | default: |
| 1287 | printk (KERN_CRIT "raid6: unsupported algorithm %d\n", |
| 1288 | conf->algorithm); |
| 1289 | } |
| 1290 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | /* |
| 1294 | * Finally, compute the new sector number |
| 1295 | */ |
| 1296 | new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset; |
| 1297 | return new_sector; |
| 1298 | } |
| 1299 | |
| 1300 | |
| 1301 | static sector_t compute_blocknr(struct stripe_head *sh, int i) |
| 1302 | { |
| 1303 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 1304 | int raid_disks = sh->disks; |
| 1305 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | sector_t new_sector = sh->sector, check; |
| 1307 | int sectors_per_chunk = conf->chunk_size >> 9; |
| 1308 | sector_t stripe; |
| 1309 | int chunk_offset; |
| 1310 | int chunk_number, dummy1, dummy2, dd_idx = i; |
| 1311 | sector_t r_sector; |
| 1312 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1313 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | chunk_offset = sector_div(new_sector, sectors_per_chunk); |
| 1315 | stripe = new_sector; |
| 1316 | BUG_ON(new_sector != stripe); |
| 1317 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1318 | if (i == sh->pd_idx) |
| 1319 | return 0; |
| 1320 | switch(conf->level) { |
| 1321 | case 4: break; |
| 1322 | case 5: |
| 1323 | switch (conf->algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 1325 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 1326 | if (i > sh->pd_idx) |
| 1327 | i--; |
| 1328 | break; |
| 1329 | case ALGORITHM_LEFT_SYMMETRIC: |
| 1330 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 1331 | if (i < sh->pd_idx) |
| 1332 | i += raid_disks; |
| 1333 | i -= (sh->pd_idx + 1); |
| 1334 | break; |
| 1335 | default: |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 1336 | printk(KERN_ERR "raid5: unsupported algorithm %d\n", |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1337 | conf->algorithm); |
| 1338 | } |
| 1339 | break; |
| 1340 | case 6: |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1341 | if (i == raid6_next_disk(sh->pd_idx, raid_disks)) |
| 1342 | return 0; /* It is the Q disk */ |
| 1343 | switch (conf->algorithm) { |
| 1344 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 1345 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 1346 | if (sh->pd_idx == raid_disks-1) |
| 1347 | i--; /* Q D D D P */ |
| 1348 | else if (i > sh->pd_idx) |
| 1349 | i -= 2; /* D D P Q D */ |
| 1350 | break; |
| 1351 | case ALGORITHM_LEFT_SYMMETRIC: |
| 1352 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 1353 | if (sh->pd_idx == raid_disks-1) |
| 1354 | i--; /* Q D D D P */ |
| 1355 | else { |
| 1356 | /* D D P Q D */ |
| 1357 | if (i < sh->pd_idx) |
| 1358 | i += raid_disks; |
| 1359 | i -= (sh->pd_idx + 2); |
| 1360 | } |
| 1361 | break; |
| 1362 | default: |
| 1363 | printk (KERN_CRIT "raid6: unsupported algorithm %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | conf->algorithm); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1365 | } |
| 1366 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | chunk_number = stripe * data_disks + i; |
| 1370 | r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset; |
| 1371 | |
| 1372 | check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf); |
| 1373 | if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) { |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 1374 | printk(KERN_ERR "compute_blocknr: map not correct\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | return 0; |
| 1376 | } |
| 1377 | return r_sector; |
| 1378 | } |
| 1379 | |
| 1380 | |
| 1381 | |
| 1382 | /* |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1383 | * Copy data between a page in the stripe cache, and one or more bion |
| 1384 | * The page could align with the middle of the bio, or there could be |
| 1385 | * several bion, each with several bio_vecs, which cover part of the page |
| 1386 | * Multiple bion are linked together on bi_next. There may be extras |
| 1387 | * at the end of this list. We ignore them. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | */ |
| 1389 | static void copy_data(int frombio, struct bio *bio, |
| 1390 | struct page *page, |
| 1391 | sector_t sector) |
| 1392 | { |
| 1393 | char *pa = page_address(page); |
| 1394 | struct bio_vec *bvl; |
| 1395 | int i; |
| 1396 | int page_offset; |
| 1397 | |
| 1398 | if (bio->bi_sector >= sector) |
| 1399 | page_offset = (signed)(bio->bi_sector - sector) * 512; |
| 1400 | else |
| 1401 | page_offset = (signed)(sector - bio->bi_sector) * -512; |
| 1402 | bio_for_each_segment(bvl, bio, i) { |
| 1403 | int len = bio_iovec_idx(bio,i)->bv_len; |
| 1404 | int clen; |
| 1405 | int b_offset = 0; |
| 1406 | |
| 1407 | if (page_offset < 0) { |
| 1408 | b_offset = -page_offset; |
| 1409 | page_offset += b_offset; |
| 1410 | len -= b_offset; |
| 1411 | } |
| 1412 | |
| 1413 | if (len > 0 && page_offset + len > STRIPE_SIZE) |
| 1414 | clen = STRIPE_SIZE - page_offset; |
| 1415 | else clen = len; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1416 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | if (clen > 0) { |
| 1418 | char *ba = __bio_kmap_atomic(bio, i, KM_USER0); |
| 1419 | if (frombio) |
| 1420 | memcpy(pa+page_offset, ba+b_offset, clen); |
| 1421 | else |
| 1422 | memcpy(ba+b_offset, pa+page_offset, clen); |
| 1423 | __bio_kunmap_atomic(ba, KM_USER0); |
| 1424 | } |
| 1425 | if (clen < len) /* hit end of page */ |
| 1426 | break; |
| 1427 | page_offset += len; |
| 1428 | } |
| 1429 | } |
| 1430 | |
Dan Williams | 9bc89cd | 2007-01-02 11:10:44 -0700 | [diff] [blame] | 1431 | #define check_xor() do { \ |
| 1432 | if (count == MAX_XOR_BLOCKS) { \ |
| 1433 | xor_blocks(count, STRIPE_SIZE, dest, ptr);\ |
| 1434 | count = 0; \ |
| 1435 | } \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | } while(0) |
| 1437 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1438 | static void compute_parity6(struct stripe_head *sh, int method) |
| 1439 | { |
| 1440 | raid6_conf_t *conf = sh->raid_conf; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 1441 | int i, pd_idx = sh->pd_idx, qd_idx, d0_idx, disks = sh->disks, count; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1442 | struct bio *chosen; |
| 1443 | /**** FIX THIS: This could be very bad if disks is close to 256 ****/ |
| 1444 | void *ptrs[disks]; |
| 1445 | |
| 1446 | qd_idx = raid6_next_disk(pd_idx, disks); |
| 1447 | d0_idx = raid6_next_disk(qd_idx, disks); |
| 1448 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1449 | pr_debug("compute_parity, stripe %llu, method %d\n", |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1450 | (unsigned long long)sh->sector, method); |
| 1451 | |
| 1452 | switch(method) { |
| 1453 | case READ_MODIFY_WRITE: |
| 1454 | BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */ |
| 1455 | case RECONSTRUCT_WRITE: |
| 1456 | for (i= disks; i-- ;) |
| 1457 | if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) { |
| 1458 | chosen = sh->dev[i].towrite; |
| 1459 | sh->dev[i].towrite = NULL; |
| 1460 | |
| 1461 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 1462 | wake_up(&conf->wait_for_overlap); |
| 1463 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 1464 | BUG_ON(sh->dev[i].written); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1465 | sh->dev[i].written = chosen; |
| 1466 | } |
| 1467 | break; |
| 1468 | case CHECK_PARITY: |
| 1469 | BUG(); /* Not implemented yet */ |
| 1470 | } |
| 1471 | |
| 1472 | for (i = disks; i--;) |
| 1473 | if (sh->dev[i].written) { |
| 1474 | sector_t sector = sh->dev[i].sector; |
| 1475 | struct bio *wbi = sh->dev[i].written; |
| 1476 | while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) { |
| 1477 | copy_data(1, wbi, sh->dev[i].page, sector); |
| 1478 | wbi = r5_next_bio(wbi, sector); |
| 1479 | } |
| 1480 | |
| 1481 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 1482 | set_bit(R5_UPTODATE, &sh->dev[i].flags); |
| 1483 | } |
| 1484 | |
| 1485 | // switch(method) { |
| 1486 | // case RECONSTRUCT_WRITE: |
| 1487 | // case CHECK_PARITY: |
| 1488 | // case UPDATE_PARITY: |
| 1489 | /* Note that unlike RAID-5, the ordering of the disks matters greatly. */ |
| 1490 | /* FIX: Is this ordering of drives even remotely optimal? */ |
| 1491 | count = 0; |
| 1492 | i = d0_idx; |
| 1493 | do { |
| 1494 | ptrs[count++] = page_address(sh->dev[i].page); |
| 1495 | if (count <= disks-2 && !test_bit(R5_UPTODATE, &sh->dev[i].flags)) |
| 1496 | printk("block %d/%d not uptodate on parity calc\n", i,count); |
| 1497 | i = raid6_next_disk(i, disks); |
| 1498 | } while ( i != d0_idx ); |
| 1499 | // break; |
| 1500 | // } |
| 1501 | |
| 1502 | raid6_call.gen_syndrome(disks, STRIPE_SIZE, ptrs); |
| 1503 | |
| 1504 | switch(method) { |
| 1505 | case RECONSTRUCT_WRITE: |
| 1506 | set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
| 1507 | set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags); |
| 1508 | set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); |
| 1509 | set_bit(R5_LOCKED, &sh->dev[qd_idx].flags); |
| 1510 | break; |
| 1511 | case UPDATE_PARITY: |
| 1512 | set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
| 1513 | set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags); |
| 1514 | break; |
| 1515 | } |
| 1516 | } |
| 1517 | |
| 1518 | |
| 1519 | /* Compute one missing block */ |
| 1520 | static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero) |
| 1521 | { |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 1522 | int i, count, disks = sh->disks; |
Dan Williams | 9bc89cd | 2007-01-02 11:10:44 -0700 | [diff] [blame] | 1523 | void *ptr[MAX_XOR_BLOCKS], *dest, *p; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1524 | int pd_idx = sh->pd_idx; |
| 1525 | int qd_idx = raid6_next_disk(pd_idx, disks); |
| 1526 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1527 | pr_debug("compute_block_1, stripe %llu, idx %d\n", |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1528 | (unsigned long long)sh->sector, dd_idx); |
| 1529 | |
| 1530 | if ( dd_idx == qd_idx ) { |
| 1531 | /* We're actually computing the Q drive */ |
| 1532 | compute_parity6(sh, UPDATE_PARITY); |
| 1533 | } else { |
Dan Williams | 9bc89cd | 2007-01-02 11:10:44 -0700 | [diff] [blame] | 1534 | dest = page_address(sh->dev[dd_idx].page); |
| 1535 | if (!nozero) memset(dest, 0, STRIPE_SIZE); |
| 1536 | count = 0; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1537 | for (i = disks ; i--; ) { |
| 1538 | if (i == dd_idx || i == qd_idx) |
| 1539 | continue; |
| 1540 | p = page_address(sh->dev[i].page); |
| 1541 | if (test_bit(R5_UPTODATE, &sh->dev[i].flags)) |
| 1542 | ptr[count++] = p; |
| 1543 | else |
| 1544 | printk("compute_block() %d, stripe %llu, %d" |
| 1545 | " not present\n", dd_idx, |
| 1546 | (unsigned long long)sh->sector, i); |
| 1547 | |
| 1548 | check_xor(); |
| 1549 | } |
Dan Williams | 9bc89cd | 2007-01-02 11:10:44 -0700 | [diff] [blame] | 1550 | if (count) |
| 1551 | xor_blocks(count, STRIPE_SIZE, dest, ptr); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1552 | if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags); |
| 1553 | else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags); |
| 1554 | } |
| 1555 | } |
| 1556 | |
| 1557 | /* Compute two missing blocks */ |
| 1558 | static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2) |
| 1559 | { |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 1560 | int i, count, disks = sh->disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1561 | int pd_idx = sh->pd_idx; |
| 1562 | int qd_idx = raid6_next_disk(pd_idx, disks); |
| 1563 | int d0_idx = raid6_next_disk(qd_idx, disks); |
| 1564 | int faila, failb; |
| 1565 | |
| 1566 | /* faila and failb are disk numbers relative to d0_idx */ |
| 1567 | /* pd_idx become disks-2 and qd_idx become disks-1 */ |
| 1568 | faila = (dd_idx1 < d0_idx) ? dd_idx1+(disks-d0_idx) : dd_idx1-d0_idx; |
| 1569 | failb = (dd_idx2 < d0_idx) ? dd_idx2+(disks-d0_idx) : dd_idx2-d0_idx; |
| 1570 | |
| 1571 | BUG_ON(faila == failb); |
| 1572 | if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; } |
| 1573 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1574 | pr_debug("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n", |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1575 | (unsigned long long)sh->sector, dd_idx1, dd_idx2, faila, failb); |
| 1576 | |
| 1577 | if ( failb == disks-1 ) { |
| 1578 | /* Q disk is one of the missing disks */ |
| 1579 | if ( faila == disks-2 ) { |
| 1580 | /* Missing P+Q, just recompute */ |
| 1581 | compute_parity6(sh, UPDATE_PARITY); |
| 1582 | return; |
| 1583 | } else { |
| 1584 | /* We're missing D+Q; recompute D from P */ |
| 1585 | compute_block_1(sh, (dd_idx1 == qd_idx) ? dd_idx2 : dd_idx1, 0); |
| 1586 | compute_parity6(sh, UPDATE_PARITY); /* Is this necessary? */ |
| 1587 | return; |
| 1588 | } |
| 1589 | } |
| 1590 | |
| 1591 | /* We're missing D+P or D+D; build pointer table */ |
| 1592 | { |
| 1593 | /**** FIX THIS: This could be very bad if disks is close to 256 ****/ |
| 1594 | void *ptrs[disks]; |
| 1595 | |
| 1596 | count = 0; |
| 1597 | i = d0_idx; |
| 1598 | do { |
| 1599 | ptrs[count++] = page_address(sh->dev[i].page); |
| 1600 | i = raid6_next_disk(i, disks); |
| 1601 | if (i != dd_idx1 && i != dd_idx2 && |
| 1602 | !test_bit(R5_UPTODATE, &sh->dev[i].flags)) |
| 1603 | printk("compute_2 with missing block %d/%d\n", count, i); |
| 1604 | } while ( i != d0_idx ); |
| 1605 | |
| 1606 | if ( failb == disks-2 ) { |
| 1607 | /* We're missing D+P. */ |
| 1608 | raid6_datap_recov(disks, STRIPE_SIZE, faila, ptrs); |
| 1609 | } else { |
| 1610 | /* We're missing D+D. */ |
| 1611 | raid6_2data_recov(disks, STRIPE_SIZE, faila, failb, ptrs); |
| 1612 | } |
| 1613 | |
| 1614 | /* Both the above update both missing blocks */ |
| 1615 | set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags); |
| 1616 | set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags); |
| 1617 | } |
| 1618 | } |
| 1619 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1620 | static void |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame^] | 1621 | schedule_reconstruction5(struct stripe_head *sh, struct stripe_head_state *s, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1622 | int rcw, int expand) |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1623 | { |
| 1624 | int i, pd_idx = sh->pd_idx, disks = sh->disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1625 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1626 | if (rcw) { |
| 1627 | /* if we are not expanding this is a proper write request, and |
| 1628 | * there will be bios with new data to be drained into the |
| 1629 | * stripe cache |
| 1630 | */ |
| 1631 | if (!expand) { |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1632 | sh->reconstruct_state = reconstruct_state_drain_run; |
| 1633 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
| 1634 | } else |
| 1635 | sh->reconstruct_state = reconstruct_state_run; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1636 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1637 | set_bit(STRIPE_OP_POSTXOR, &s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1638 | |
| 1639 | for (i = disks; i--; ) { |
| 1640 | struct r5dev *dev = &sh->dev[i]; |
| 1641 | |
| 1642 | if (dev->towrite) { |
| 1643 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1644 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1645 | if (!expand) |
| 1646 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1647 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1648 | } |
| 1649 | } |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1650 | if (s->locked + 1 == disks) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 1651 | if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 1652 | atomic_inc(&sh->raid_conf->pending_full_writes); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1653 | } else { |
| 1654 | BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) || |
| 1655 | test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags))); |
| 1656 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1657 | sh->reconstruct_state = reconstruct_state_prexor_drain_run; |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1658 | set_bit(STRIPE_OP_PREXOR, &s->ops_request); |
| 1659 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
| 1660 | set_bit(STRIPE_OP_POSTXOR, &s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1661 | |
| 1662 | for (i = disks; i--; ) { |
| 1663 | struct r5dev *dev = &sh->dev[i]; |
| 1664 | if (i == pd_idx) |
| 1665 | continue; |
| 1666 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1667 | if (dev->towrite && |
| 1668 | (test_bit(R5_UPTODATE, &dev->flags) || |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1669 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 1670 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1671 | set_bit(R5_LOCKED, &dev->flags); |
| 1672 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1673 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1674 | } |
| 1675 | } |
| 1676 | } |
| 1677 | |
| 1678 | /* keep the parity disk locked while asynchronous operations |
| 1679 | * are in flight |
| 1680 | */ |
| 1681 | set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); |
| 1682 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1683 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1684 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1685 | pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n", |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1686 | __func__, (unsigned long long)sh->sector, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1687 | s->locked, s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1688 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1689 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | /* |
| 1691 | * Each stripe/dev can have one or more bion attached. |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1692 | * toread/towrite point to the first in a chain. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | * The bi_next chain must be in order. |
| 1694 | */ |
| 1695 | static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite) |
| 1696 | { |
| 1697 | struct bio **bip; |
| 1698 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1699 | int firstwrite=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1701 | pr_debug("adding bh b#%llu to stripe s#%llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | (unsigned long long)bi->bi_sector, |
| 1703 | (unsigned long long)sh->sector); |
| 1704 | |
| 1705 | |
| 1706 | spin_lock(&sh->lock); |
| 1707 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1708 | if (forwrite) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | bip = &sh->dev[dd_idx].towrite; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1710 | if (*bip == NULL && sh->dev[dd_idx].written == NULL) |
| 1711 | firstwrite = 1; |
| 1712 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | bip = &sh->dev[dd_idx].toread; |
| 1714 | while (*bip && (*bip)->bi_sector < bi->bi_sector) { |
| 1715 | if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector) |
| 1716 | goto overlap; |
| 1717 | bip = & (*bip)->bi_next; |
| 1718 | } |
| 1719 | if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9)) |
| 1720 | goto overlap; |
| 1721 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 1722 | BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | if (*bip) |
| 1724 | bi->bi_next = *bip; |
| 1725 | *bip = bi; |
| 1726 | bi->bi_phys_segments ++; |
| 1727 | spin_unlock_irq(&conf->device_lock); |
| 1728 | spin_unlock(&sh->lock); |
| 1729 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1730 | pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | (unsigned long long)bi->bi_sector, |
| 1732 | (unsigned long long)sh->sector, dd_idx); |
| 1733 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1734 | if (conf->mddev->bitmap && firstwrite) { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1735 | bitmap_startwrite(conf->mddev->bitmap, sh->sector, |
| 1736 | STRIPE_SECTORS, 0); |
NeilBrown | ae3c20c | 2006-07-10 04:44:17 -0700 | [diff] [blame] | 1737 | sh->bm_seq = conf->seq_flush+1; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1738 | set_bit(STRIPE_BIT_DELAY, &sh->state); |
| 1739 | } |
| 1740 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | if (forwrite) { |
| 1742 | /* check if page is covered */ |
| 1743 | sector_t sector = sh->dev[dd_idx].sector; |
| 1744 | for (bi=sh->dev[dd_idx].towrite; |
| 1745 | sector < sh->dev[dd_idx].sector + STRIPE_SECTORS && |
| 1746 | bi && bi->bi_sector <= sector; |
| 1747 | bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) { |
| 1748 | if (bi->bi_sector + (bi->bi_size>>9) >= sector) |
| 1749 | sector = bi->bi_sector + (bi->bi_size>>9); |
| 1750 | } |
| 1751 | if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS) |
| 1752 | set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags); |
| 1753 | } |
| 1754 | return 1; |
| 1755 | |
| 1756 | overlap: |
| 1757 | set_bit(R5_Overlap, &sh->dev[dd_idx].flags); |
| 1758 | spin_unlock_irq(&conf->device_lock); |
| 1759 | spin_unlock(&sh->lock); |
| 1760 | return 0; |
| 1761 | } |
| 1762 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 1763 | static void end_reshape(raid5_conf_t *conf); |
| 1764 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1765 | static int page_is_zero(struct page *p) |
| 1766 | { |
| 1767 | char *a = page_address(p); |
| 1768 | return ((*(u32*)a) == 0 && |
| 1769 | memcmp(a, a+4, STRIPE_SIZE-4)==0); |
| 1770 | } |
| 1771 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 1772 | static int stripe_to_pdidx(sector_t stripe, raid5_conf_t *conf, int disks) |
| 1773 | { |
| 1774 | int sectors_per_chunk = conf->chunk_size >> 9; |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 1775 | int pd_idx, dd_idx; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 1776 | int chunk_offset = sector_div(stripe, sectors_per_chunk); |
| 1777 | |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 1778 | raid5_compute_sector(stripe * (disks - conf->max_degraded) |
| 1779 | *sectors_per_chunk + chunk_offset, |
| 1780 | disks, disks - conf->max_degraded, |
| 1781 | &dd_idx, &pd_idx, conf); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 1782 | return pd_idx; |
| 1783 | } |
| 1784 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1785 | static void |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame^] | 1786 | handle_failed_stripe(raid5_conf_t *conf, struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1787 | struct stripe_head_state *s, int disks, |
| 1788 | struct bio **return_bi) |
| 1789 | { |
| 1790 | int i; |
| 1791 | for (i = disks; i--; ) { |
| 1792 | struct bio *bi; |
| 1793 | int bitmap_end = 0; |
| 1794 | |
| 1795 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
| 1796 | mdk_rdev_t *rdev; |
| 1797 | rcu_read_lock(); |
| 1798 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 1799 | if (rdev && test_bit(In_sync, &rdev->flags)) |
| 1800 | /* multiple read failures in one stripe */ |
| 1801 | md_error(conf->mddev, rdev); |
| 1802 | rcu_read_unlock(); |
| 1803 | } |
| 1804 | spin_lock_irq(&conf->device_lock); |
| 1805 | /* fail all writes first */ |
| 1806 | bi = sh->dev[i].towrite; |
| 1807 | sh->dev[i].towrite = NULL; |
| 1808 | if (bi) { |
| 1809 | s->to_write--; |
| 1810 | bitmap_end = 1; |
| 1811 | } |
| 1812 | |
| 1813 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 1814 | wake_up(&conf->wait_for_overlap); |
| 1815 | |
| 1816 | while (bi && bi->bi_sector < |
| 1817 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 1818 | struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); |
| 1819 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
| 1820 | if (--bi->bi_phys_segments == 0) { |
| 1821 | md_write_end(conf->mddev); |
| 1822 | bi->bi_next = *return_bi; |
| 1823 | *return_bi = bi; |
| 1824 | } |
| 1825 | bi = nextbi; |
| 1826 | } |
| 1827 | /* and fail all 'written' */ |
| 1828 | bi = sh->dev[i].written; |
| 1829 | sh->dev[i].written = NULL; |
| 1830 | if (bi) bitmap_end = 1; |
| 1831 | while (bi && bi->bi_sector < |
| 1832 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 1833 | struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector); |
| 1834 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
| 1835 | if (--bi->bi_phys_segments == 0) { |
| 1836 | md_write_end(conf->mddev); |
| 1837 | bi->bi_next = *return_bi; |
| 1838 | *return_bi = bi; |
| 1839 | } |
| 1840 | bi = bi2; |
| 1841 | } |
| 1842 | |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 1843 | /* fail any reads if this device is non-operational and |
| 1844 | * the data has not reached the cache yet. |
| 1845 | */ |
| 1846 | if (!test_bit(R5_Wantfill, &sh->dev[i].flags) && |
| 1847 | (!test_bit(R5_Insync, &sh->dev[i].flags) || |
| 1848 | test_bit(R5_ReadError, &sh->dev[i].flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1849 | bi = sh->dev[i].toread; |
| 1850 | sh->dev[i].toread = NULL; |
| 1851 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 1852 | wake_up(&conf->wait_for_overlap); |
| 1853 | if (bi) s->to_read--; |
| 1854 | while (bi && bi->bi_sector < |
| 1855 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 1856 | struct bio *nextbi = |
| 1857 | r5_next_bio(bi, sh->dev[i].sector); |
| 1858 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
| 1859 | if (--bi->bi_phys_segments == 0) { |
| 1860 | bi->bi_next = *return_bi; |
| 1861 | *return_bi = bi; |
| 1862 | } |
| 1863 | bi = nextbi; |
| 1864 | } |
| 1865 | } |
| 1866 | spin_unlock_irq(&conf->device_lock); |
| 1867 | if (bitmap_end) |
| 1868 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 1869 | STRIPE_SECTORS, 0, 0); |
| 1870 | } |
| 1871 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 1872 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 1873 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 1874 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1875 | } |
| 1876 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame^] | 1877 | /* fetch_block5 - checks the given member device to see if its data needs |
| 1878 | * to be read or computed to satisfy a request. |
| 1879 | * |
| 1880 | * Returns 1 when no more member devices need to be checked, otherwise returns |
| 1881 | * 0 to tell the loop in handle_stripe_fill5 to continue |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1882 | */ |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame^] | 1883 | static int fetch_block5(struct stripe_head *sh, struct stripe_head_state *s, |
| 1884 | int disk_idx, int disks) |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1885 | { |
| 1886 | struct r5dev *dev = &sh->dev[disk_idx]; |
| 1887 | struct r5dev *failed_dev = &sh->dev[s->failed_num]; |
| 1888 | |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1889 | /* is the data in this block needed, and can we get it? */ |
| 1890 | if (!test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame^] | 1891 | !test_bit(R5_UPTODATE, &dev->flags) && |
| 1892 | (dev->toread || |
| 1893 | (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) || |
| 1894 | s->syncing || s->expanding || |
| 1895 | (s->failed && |
| 1896 | (failed_dev->toread || |
| 1897 | (failed_dev->towrite && |
| 1898 | !test_bit(R5_OVERWRITE, &failed_dev->flags)))))) { |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 1899 | /* We would like to get this block, possibly by computing it, |
| 1900 | * otherwise read it if the backing disk is insync |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1901 | */ |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 1902 | if ((s->uptodate == disks - 1) && |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 1903 | (s->failed && disk_idx == s->failed_num)) { |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 1904 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 1905 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1906 | set_bit(R5_Wantcompute, &dev->flags); |
| 1907 | sh->ops.target = disk_idx; |
| 1908 | s->req_compute = 1; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1909 | /* Careful: from this point on 'uptodate' is in the eye |
| 1910 | * of raid5_run_ops which services 'compute' operations |
| 1911 | * before writes. R5_Wantcompute flags a block that will |
| 1912 | * be R5_UPTODATE by the time it is needed for a |
| 1913 | * subsequent operation. |
| 1914 | */ |
| 1915 | s->uptodate++; |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame^] | 1916 | return 1; /* uptodate + compute == disks */ |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 1917 | } else if (test_bit(R5_Insync, &dev->flags)) { |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1918 | set_bit(R5_LOCKED, &dev->flags); |
| 1919 | set_bit(R5_Wantread, &dev->flags); |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1920 | s->locked++; |
| 1921 | pr_debug("Reading block %d (sync=%d)\n", disk_idx, |
| 1922 | s->syncing); |
| 1923 | } |
| 1924 | } |
| 1925 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame^] | 1926 | return 0; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1927 | } |
| 1928 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame^] | 1929 | /** |
| 1930 | * handle_stripe_fill5 - read or compute data to satisfy pending requests. |
| 1931 | */ |
| 1932 | static void handle_stripe_fill5(struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1933 | struct stripe_head_state *s, int disks) |
| 1934 | { |
| 1935 | int i; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1936 | |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1937 | /* look for blocks to read/compute, skip this if a compute |
| 1938 | * is already in flight, or if the stripe contents are in the |
| 1939 | * midst of changing due to a write |
| 1940 | */ |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 1941 | if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state && |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame^] | 1942 | !sh->reconstruct_state) |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1943 | for (i = disks; i--; ) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame^] | 1944 | if (fetch_block5(sh, s, i, disks)) |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1945 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1946 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1947 | } |
| 1948 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame^] | 1949 | static void handle_stripe_fill6(struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1950 | struct stripe_head_state *s, struct r6_state *r6s, |
| 1951 | int disks) |
| 1952 | { |
| 1953 | int i; |
| 1954 | for (i = disks; i--; ) { |
| 1955 | struct r5dev *dev = &sh->dev[i]; |
| 1956 | if (!test_bit(R5_LOCKED, &dev->flags) && |
| 1957 | !test_bit(R5_UPTODATE, &dev->flags) && |
| 1958 | (dev->toread || (dev->towrite && |
| 1959 | !test_bit(R5_OVERWRITE, &dev->flags)) || |
| 1960 | s->syncing || s->expanding || |
| 1961 | (s->failed >= 1 && |
| 1962 | (sh->dev[r6s->failed_num[0]].toread || |
| 1963 | s->to_write)) || |
| 1964 | (s->failed >= 2 && |
| 1965 | (sh->dev[r6s->failed_num[1]].toread || |
| 1966 | s->to_write)))) { |
| 1967 | /* we would like to get this block, possibly |
| 1968 | * by computing it, but we might not be able to |
| 1969 | */ |
Dan Williams | c337869 | 2008-06-05 22:45:54 -0700 | [diff] [blame] | 1970 | if ((s->uptodate == disks - 1) && |
| 1971 | (s->failed && (i == r6s->failed_num[0] || |
| 1972 | i == r6s->failed_num[1]))) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1973 | pr_debug("Computing stripe %llu block %d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1974 | (unsigned long long)sh->sector, i); |
| 1975 | compute_block_1(sh, i, 0); |
| 1976 | s->uptodate++; |
| 1977 | } else if ( s->uptodate == disks-2 && s->failed >= 2 ) { |
| 1978 | /* Computing 2-failure is *very* expensive; only |
| 1979 | * do it if failed >= 2 |
| 1980 | */ |
| 1981 | int other; |
| 1982 | for (other = disks; other--; ) { |
| 1983 | if (other == i) |
| 1984 | continue; |
| 1985 | if (!test_bit(R5_UPTODATE, |
| 1986 | &sh->dev[other].flags)) |
| 1987 | break; |
| 1988 | } |
| 1989 | BUG_ON(other < 0); |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1990 | pr_debug("Computing stripe %llu blocks %d,%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1991 | (unsigned long long)sh->sector, |
| 1992 | i, other); |
| 1993 | compute_block_2(sh, i, other); |
| 1994 | s->uptodate += 2; |
| 1995 | } else if (test_bit(R5_Insync, &dev->flags)) { |
| 1996 | set_bit(R5_LOCKED, &dev->flags); |
| 1997 | set_bit(R5_Wantread, &dev->flags); |
| 1998 | s->locked++; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1999 | pr_debug("Reading block %d (sync=%d)\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2000 | i, s->syncing); |
| 2001 | } |
| 2002 | } |
| 2003 | } |
| 2004 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2005 | } |
| 2006 | |
| 2007 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame^] | 2008 | /* handle_stripe_clean_event |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2009 | * any written block on an uptodate or failed drive can be returned. |
| 2010 | * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but |
| 2011 | * never LOCKED, so we don't need to test 'failed' directly. |
| 2012 | */ |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame^] | 2013 | static void handle_stripe_clean_event(raid5_conf_t *conf, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2014 | struct stripe_head *sh, int disks, struct bio **return_bi) |
| 2015 | { |
| 2016 | int i; |
| 2017 | struct r5dev *dev; |
| 2018 | |
| 2019 | for (i = disks; i--; ) |
| 2020 | if (sh->dev[i].written) { |
| 2021 | dev = &sh->dev[i]; |
| 2022 | if (!test_bit(R5_LOCKED, &dev->flags) && |
| 2023 | test_bit(R5_UPTODATE, &dev->flags)) { |
| 2024 | /* We can return any write requests */ |
| 2025 | struct bio *wbi, *wbi2; |
| 2026 | int bitmap_end = 0; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2027 | pr_debug("Return write for disc %d\n", i); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2028 | spin_lock_irq(&conf->device_lock); |
| 2029 | wbi = dev->written; |
| 2030 | dev->written = NULL; |
| 2031 | while (wbi && wbi->bi_sector < |
| 2032 | dev->sector + STRIPE_SECTORS) { |
| 2033 | wbi2 = r5_next_bio(wbi, dev->sector); |
| 2034 | if (--wbi->bi_phys_segments == 0) { |
| 2035 | md_write_end(conf->mddev); |
| 2036 | wbi->bi_next = *return_bi; |
| 2037 | *return_bi = wbi; |
| 2038 | } |
| 2039 | wbi = wbi2; |
| 2040 | } |
| 2041 | if (dev->towrite == NULL) |
| 2042 | bitmap_end = 1; |
| 2043 | spin_unlock_irq(&conf->device_lock); |
| 2044 | if (bitmap_end) |
| 2045 | bitmap_endwrite(conf->mddev->bitmap, |
| 2046 | sh->sector, |
| 2047 | STRIPE_SECTORS, |
| 2048 | !test_bit(STRIPE_DEGRADED, &sh->state), |
| 2049 | 0); |
| 2050 | } |
| 2051 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2052 | |
| 2053 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 2054 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 2055 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2056 | } |
| 2057 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame^] | 2058 | static void handle_stripe_dirtying5(raid5_conf_t *conf, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2059 | struct stripe_head *sh, struct stripe_head_state *s, int disks) |
| 2060 | { |
| 2061 | int rmw = 0, rcw = 0, i; |
| 2062 | for (i = disks; i--; ) { |
| 2063 | /* would I have to read this buffer for read_modify_write */ |
| 2064 | struct r5dev *dev = &sh->dev[i]; |
| 2065 | if ((dev->towrite || i == sh->pd_idx) && |
| 2066 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2067 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2068 | test_bit(R5_Wantcompute, &dev->flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2069 | if (test_bit(R5_Insync, &dev->flags)) |
| 2070 | rmw++; |
| 2071 | else |
| 2072 | rmw += 2*disks; /* cannot read it */ |
| 2073 | } |
| 2074 | /* Would I have to read this buffer for reconstruct_write */ |
| 2075 | if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx && |
| 2076 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2077 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2078 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2079 | if (test_bit(R5_Insync, &dev->flags)) rcw++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2080 | else |
| 2081 | rcw += 2*disks; |
| 2082 | } |
| 2083 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2084 | pr_debug("for sector %llu, rmw=%d rcw=%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2085 | (unsigned long long)sh->sector, rmw, rcw); |
| 2086 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2087 | if (rmw < rcw && rmw > 0) |
| 2088 | /* prefer read-modify-write, but need to get some data */ |
| 2089 | for (i = disks; i--; ) { |
| 2090 | struct r5dev *dev = &sh->dev[i]; |
| 2091 | if ((dev->towrite || i == sh->pd_idx) && |
| 2092 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2093 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2094 | test_bit(R5_Wantcompute, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2095 | test_bit(R5_Insync, &dev->flags)) { |
| 2096 | if ( |
| 2097 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2098 | pr_debug("Read_old block " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2099 | "%d for r-m-w\n", i); |
| 2100 | set_bit(R5_LOCKED, &dev->flags); |
| 2101 | set_bit(R5_Wantread, &dev->flags); |
| 2102 | s->locked++; |
| 2103 | } else { |
| 2104 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2105 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2106 | } |
| 2107 | } |
| 2108 | } |
| 2109 | if (rcw <= rmw && rcw > 0) |
| 2110 | /* want reconstruct write, but need to get some data */ |
| 2111 | for (i = disks; i--; ) { |
| 2112 | struct r5dev *dev = &sh->dev[i]; |
| 2113 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
| 2114 | i != sh->pd_idx && |
| 2115 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2116 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2117 | test_bit(R5_Wantcompute, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2118 | test_bit(R5_Insync, &dev->flags)) { |
| 2119 | if ( |
| 2120 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2121 | pr_debug("Read_old block " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2122 | "%d for Reconstruct\n", i); |
| 2123 | set_bit(R5_LOCKED, &dev->flags); |
| 2124 | set_bit(R5_Wantread, &dev->flags); |
| 2125 | s->locked++; |
| 2126 | } else { |
| 2127 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2128 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2129 | } |
| 2130 | } |
| 2131 | } |
| 2132 | /* now if nothing is locked, and if we have enough data, |
| 2133 | * we can start a write request |
| 2134 | */ |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2135 | /* since handle_stripe can be called at any time we need to handle the |
| 2136 | * case where a compute block operation has been submitted and then a |
| 2137 | * subsequent call wants to start a write request. raid5_run_ops only |
| 2138 | * handles the case where compute block and postxor are requested |
| 2139 | * simultaneously. If this is not the case then new writes need to be |
| 2140 | * held off until the compute completes. |
| 2141 | */ |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2142 | if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) && |
| 2143 | (s->locked == 0 && (rcw == 0 || rmw == 0) && |
| 2144 | !test_bit(STRIPE_BIT_DELAY, &sh->state))) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame^] | 2145 | schedule_reconstruction5(sh, s, rcw == 0, 0); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2146 | } |
| 2147 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame^] | 2148 | static void handle_stripe_dirtying6(raid5_conf_t *conf, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2149 | struct stripe_head *sh, struct stripe_head_state *s, |
| 2150 | struct r6_state *r6s, int disks) |
| 2151 | { |
| 2152 | int rcw = 0, must_compute = 0, pd_idx = sh->pd_idx, i; |
| 2153 | int qd_idx = r6s->qd_idx; |
| 2154 | for (i = disks; i--; ) { |
| 2155 | struct r5dev *dev = &sh->dev[i]; |
| 2156 | /* Would I have to read this buffer for reconstruct_write */ |
| 2157 | if (!test_bit(R5_OVERWRITE, &dev->flags) |
| 2158 | && i != pd_idx && i != qd_idx |
| 2159 | && (!test_bit(R5_LOCKED, &dev->flags) |
| 2160 | ) && |
| 2161 | !test_bit(R5_UPTODATE, &dev->flags)) { |
| 2162 | if (test_bit(R5_Insync, &dev->flags)) rcw++; |
| 2163 | else { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2164 | pr_debug("raid6: must_compute: " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2165 | "disk %d flags=%#lx\n", i, dev->flags); |
| 2166 | must_compute++; |
| 2167 | } |
| 2168 | } |
| 2169 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2170 | pr_debug("for sector %llu, rcw=%d, must_compute=%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2171 | (unsigned long long)sh->sector, rcw, must_compute); |
| 2172 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2173 | |
| 2174 | if (rcw > 0) |
| 2175 | /* want reconstruct write, but need to get some data */ |
| 2176 | for (i = disks; i--; ) { |
| 2177 | struct r5dev *dev = &sh->dev[i]; |
| 2178 | if (!test_bit(R5_OVERWRITE, &dev->flags) |
| 2179 | && !(s->failed == 0 && (i == pd_idx || i == qd_idx)) |
| 2180 | && !test_bit(R5_LOCKED, &dev->flags) && |
| 2181 | !test_bit(R5_UPTODATE, &dev->flags) && |
| 2182 | test_bit(R5_Insync, &dev->flags)) { |
| 2183 | if ( |
| 2184 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2185 | pr_debug("Read_old stripe %llu " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2186 | "block %d for Reconstruct\n", |
| 2187 | (unsigned long long)sh->sector, i); |
| 2188 | set_bit(R5_LOCKED, &dev->flags); |
| 2189 | set_bit(R5_Wantread, &dev->flags); |
| 2190 | s->locked++; |
| 2191 | } else { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2192 | pr_debug("Request delayed stripe %llu " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2193 | "block %d for Reconstruct\n", |
| 2194 | (unsigned long long)sh->sector, i); |
| 2195 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2196 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2197 | } |
| 2198 | } |
| 2199 | } |
| 2200 | /* now if nothing is locked, and if we have enough data, we can start a |
| 2201 | * write request |
| 2202 | */ |
| 2203 | if (s->locked == 0 && rcw == 0 && |
| 2204 | !test_bit(STRIPE_BIT_DELAY, &sh->state)) { |
| 2205 | if (must_compute > 0) { |
| 2206 | /* We have failed blocks and need to compute them */ |
| 2207 | switch (s->failed) { |
| 2208 | case 0: |
| 2209 | BUG(); |
| 2210 | case 1: |
| 2211 | compute_block_1(sh, r6s->failed_num[0], 0); |
| 2212 | break; |
| 2213 | case 2: |
| 2214 | compute_block_2(sh, r6s->failed_num[0], |
| 2215 | r6s->failed_num[1]); |
| 2216 | break; |
| 2217 | default: /* This request should have been failed? */ |
| 2218 | BUG(); |
| 2219 | } |
| 2220 | } |
| 2221 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2222 | pr_debug("Computing parity for stripe %llu\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2223 | (unsigned long long)sh->sector); |
| 2224 | compute_parity6(sh, RECONSTRUCT_WRITE); |
| 2225 | /* now every locked buffer is ready to be written */ |
| 2226 | for (i = disks; i--; ) |
| 2227 | if (test_bit(R5_LOCKED, &sh->dev[i].flags)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2228 | pr_debug("Writing stripe %llu block %d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2229 | (unsigned long long)sh->sector, i); |
| 2230 | s->locked++; |
| 2231 | set_bit(R5_Wantwrite, &sh->dev[i].flags); |
| 2232 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2233 | if (s->locked == disks) |
| 2234 | if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 2235 | atomic_inc(&conf->pending_full_writes); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2236 | /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */ |
| 2237 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2238 | |
| 2239 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
| 2240 | atomic_dec(&conf->preread_active_stripes); |
| 2241 | if (atomic_read(&conf->preread_active_stripes) < |
| 2242 | IO_THRESHOLD) |
| 2243 | md_wakeup_thread(conf->mddev->thread); |
| 2244 | } |
| 2245 | } |
| 2246 | } |
| 2247 | |
| 2248 | static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh, |
| 2249 | struct stripe_head_state *s, int disks) |
| 2250 | { |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2251 | struct r5dev *dev = NULL; |
Dan Williams | e89f896 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2252 | |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2253 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2254 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2255 | switch (sh->check_state) { |
| 2256 | case check_state_idle: |
| 2257 | /* start a new check operation if there are no failures */ |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2258 | if (s->failed == 0) { |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2259 | BUG_ON(s->uptodate != disks); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2260 | sh->check_state = check_state_run; |
| 2261 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2262 | clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2263 | s->uptodate--; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2264 | break; |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2265 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2266 | dev = &sh->dev[s->failed_num]; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2267 | /* fall through */ |
| 2268 | case check_state_compute_result: |
| 2269 | sh->check_state = check_state_idle; |
| 2270 | if (!dev) |
| 2271 | dev = &sh->dev[sh->pd_idx]; |
| 2272 | |
| 2273 | /* check that a write has not made the stripe insync */ |
| 2274 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 2275 | break; |
| 2276 | |
| 2277 | /* either failed parity check, or recovery is happening */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2278 | BUG_ON(!test_bit(R5_UPTODATE, &dev->flags)); |
| 2279 | BUG_ON(s->uptodate != disks); |
| 2280 | |
| 2281 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2282 | s->locked++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2283 | set_bit(R5_Wantwrite, &dev->flags); |
Dan Williams | 830ea01 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2284 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2285 | clear_bit(STRIPE_DEGRADED, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2286 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2287 | break; |
| 2288 | case check_state_run: |
| 2289 | break; /* we will be called again upon completion */ |
| 2290 | case check_state_check_result: |
| 2291 | sh->check_state = check_state_idle; |
| 2292 | |
| 2293 | /* if a failure occurred during the check operation, leave |
| 2294 | * STRIPE_INSYNC not set and let the stripe be handled again |
| 2295 | */ |
| 2296 | if (s->failed) |
| 2297 | break; |
| 2298 | |
| 2299 | /* handle a successful check operation, if parity is correct |
| 2300 | * we are done. Otherwise update the mismatch count and repair |
| 2301 | * parity if !MD_RECOVERY_CHECK |
| 2302 | */ |
| 2303 | if (sh->ops.zero_sum_result == 0) |
| 2304 | /* parity is correct (on disc, |
| 2305 | * not in buffer any more) |
| 2306 | */ |
| 2307 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2308 | else { |
| 2309 | conf->mddev->resync_mismatches += STRIPE_SECTORS; |
| 2310 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 2311 | /* don't try to repair!! */ |
| 2312 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2313 | else { |
| 2314 | sh->check_state = check_state_compute_run; |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2315 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2316 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2317 | set_bit(R5_Wantcompute, |
| 2318 | &sh->dev[sh->pd_idx].flags); |
| 2319 | sh->ops.target = sh->pd_idx; |
| 2320 | s->uptodate++; |
| 2321 | } |
| 2322 | } |
| 2323 | break; |
| 2324 | case check_state_compute_run: |
| 2325 | break; |
| 2326 | default: |
| 2327 | printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n", |
| 2328 | __func__, sh->check_state, |
| 2329 | (unsigned long long) sh->sector); |
| 2330 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2331 | } |
| 2332 | } |
| 2333 | |
| 2334 | |
| 2335 | static void handle_parity_checks6(raid5_conf_t *conf, struct stripe_head *sh, |
| 2336 | struct stripe_head_state *s, |
| 2337 | struct r6_state *r6s, struct page *tmp_page, |
| 2338 | int disks) |
| 2339 | { |
| 2340 | int update_p = 0, update_q = 0; |
| 2341 | struct r5dev *dev; |
| 2342 | int pd_idx = sh->pd_idx; |
| 2343 | int qd_idx = r6s->qd_idx; |
| 2344 | |
| 2345 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2346 | |
| 2347 | BUG_ON(s->failed > 2); |
| 2348 | BUG_ON(s->uptodate < disks); |
| 2349 | /* Want to check and possibly repair P and Q. |
| 2350 | * However there could be one 'failed' device, in which |
| 2351 | * case we can only check one of them, possibly using the |
| 2352 | * other to generate missing data |
| 2353 | */ |
| 2354 | |
| 2355 | /* If !tmp_page, we cannot do the calculations, |
| 2356 | * but as we have set STRIPE_HANDLE, we will soon be called |
| 2357 | * by stripe_handle with a tmp_page - just wait until then. |
| 2358 | */ |
| 2359 | if (tmp_page) { |
| 2360 | if (s->failed == r6s->q_failed) { |
| 2361 | /* The only possible failed device holds 'Q', so it |
| 2362 | * makes sense to check P (If anything else were failed, |
| 2363 | * we would have used P to recreate it). |
| 2364 | */ |
| 2365 | compute_block_1(sh, pd_idx, 1); |
| 2366 | if (!page_is_zero(sh->dev[pd_idx].page)) { |
| 2367 | compute_block_1(sh, pd_idx, 0); |
| 2368 | update_p = 1; |
| 2369 | } |
| 2370 | } |
| 2371 | if (!r6s->q_failed && s->failed < 2) { |
| 2372 | /* q is not failed, and we didn't use it to generate |
| 2373 | * anything, so it makes sense to check it |
| 2374 | */ |
| 2375 | memcpy(page_address(tmp_page), |
| 2376 | page_address(sh->dev[qd_idx].page), |
| 2377 | STRIPE_SIZE); |
| 2378 | compute_parity6(sh, UPDATE_PARITY); |
| 2379 | if (memcmp(page_address(tmp_page), |
| 2380 | page_address(sh->dev[qd_idx].page), |
| 2381 | STRIPE_SIZE) != 0) { |
| 2382 | clear_bit(STRIPE_INSYNC, &sh->state); |
| 2383 | update_q = 1; |
| 2384 | } |
| 2385 | } |
| 2386 | if (update_p || update_q) { |
| 2387 | conf->mddev->resync_mismatches += STRIPE_SECTORS; |
| 2388 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 2389 | /* don't try to repair!! */ |
| 2390 | update_p = update_q = 0; |
| 2391 | } |
| 2392 | |
| 2393 | /* now write out any block on a failed drive, |
| 2394 | * or P or Q if they need it |
| 2395 | */ |
| 2396 | |
| 2397 | if (s->failed == 2) { |
| 2398 | dev = &sh->dev[r6s->failed_num[1]]; |
| 2399 | s->locked++; |
| 2400 | set_bit(R5_LOCKED, &dev->flags); |
| 2401 | set_bit(R5_Wantwrite, &dev->flags); |
| 2402 | } |
| 2403 | if (s->failed >= 1) { |
| 2404 | dev = &sh->dev[r6s->failed_num[0]]; |
| 2405 | s->locked++; |
| 2406 | set_bit(R5_LOCKED, &dev->flags); |
| 2407 | set_bit(R5_Wantwrite, &dev->flags); |
| 2408 | } |
| 2409 | |
| 2410 | if (update_p) { |
| 2411 | dev = &sh->dev[pd_idx]; |
| 2412 | s->locked++; |
| 2413 | set_bit(R5_LOCKED, &dev->flags); |
| 2414 | set_bit(R5_Wantwrite, &dev->flags); |
| 2415 | } |
| 2416 | if (update_q) { |
| 2417 | dev = &sh->dev[qd_idx]; |
| 2418 | s->locked++; |
| 2419 | set_bit(R5_LOCKED, &dev->flags); |
| 2420 | set_bit(R5_Wantwrite, &dev->flags); |
| 2421 | } |
| 2422 | clear_bit(STRIPE_DEGRADED, &sh->state); |
| 2423 | |
| 2424 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2425 | } |
| 2426 | } |
| 2427 | |
| 2428 | static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh, |
| 2429 | struct r6_state *r6s) |
| 2430 | { |
| 2431 | int i; |
| 2432 | |
| 2433 | /* We have read all the blocks in this stripe and now we need to |
| 2434 | * copy some of them into a target stripe for expand. |
| 2435 | */ |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2436 | struct dma_async_tx_descriptor *tx = NULL; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2437 | clear_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 2438 | for (i = 0; i < sh->disks; i++) |
NeilBrown | a2e0855 | 2007-09-11 15:23:36 -0700 | [diff] [blame] | 2439 | if (i != sh->pd_idx && (!r6s || i != r6s->qd_idx)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2440 | int dd_idx, pd_idx, j; |
| 2441 | struct stripe_head *sh2; |
| 2442 | |
| 2443 | sector_t bn = compute_blocknr(sh, i); |
| 2444 | sector_t s = raid5_compute_sector(bn, conf->raid_disks, |
| 2445 | conf->raid_disks - |
| 2446 | conf->max_degraded, &dd_idx, |
| 2447 | &pd_idx, conf); |
| 2448 | sh2 = get_active_stripe(conf, s, conf->raid_disks, |
| 2449 | pd_idx, 1); |
| 2450 | if (sh2 == NULL) |
| 2451 | /* so far only the early blocks of this stripe |
| 2452 | * have been requested. When later blocks |
| 2453 | * get requested, we will try again |
| 2454 | */ |
| 2455 | continue; |
| 2456 | if (!test_bit(STRIPE_EXPANDING, &sh2->state) || |
| 2457 | test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) { |
| 2458 | /* must have already done this block */ |
| 2459 | release_stripe(sh2); |
| 2460 | continue; |
| 2461 | } |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2462 | |
| 2463 | /* place all the copies on one channel */ |
| 2464 | tx = async_memcpy(sh2->dev[dd_idx].page, |
| 2465 | sh->dev[i].page, 0, 0, STRIPE_SIZE, |
| 2466 | ASYNC_TX_DEP_ACK, tx, NULL, NULL); |
| 2467 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2468 | set_bit(R5_Expanded, &sh2->dev[dd_idx].flags); |
| 2469 | set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags); |
| 2470 | for (j = 0; j < conf->raid_disks; j++) |
| 2471 | if (j != sh2->pd_idx && |
NeilBrown | a2e0855 | 2007-09-11 15:23:36 -0700 | [diff] [blame] | 2472 | (!r6s || j != raid6_next_disk(sh2->pd_idx, |
| 2473 | sh2->disks)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2474 | !test_bit(R5_Expanded, &sh2->dev[j].flags)) |
| 2475 | break; |
| 2476 | if (j == conf->raid_disks) { |
| 2477 | set_bit(STRIPE_EXPAND_READY, &sh2->state); |
| 2478 | set_bit(STRIPE_HANDLE, &sh2->state); |
| 2479 | } |
| 2480 | release_stripe(sh2); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2481 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2482 | } |
NeilBrown | a2e0855 | 2007-09-11 15:23:36 -0700 | [diff] [blame] | 2483 | /* done submitting copies, wait for them to complete */ |
| 2484 | if (tx) { |
| 2485 | async_tx_ack(tx); |
| 2486 | dma_wait_for_async_tx(tx); |
| 2487 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2488 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2489 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2490 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2491 | /* |
| 2492 | * handle_stripe - do things to a stripe. |
| 2493 | * |
| 2494 | * We lock the stripe and then examine the state of various bits |
| 2495 | * to see what needs to be done. |
| 2496 | * Possible results: |
| 2497 | * return some read request which now have data |
| 2498 | * return some write requests which are safely on disc |
| 2499 | * schedule a read on some buffers |
| 2500 | * schedule a write of some buffers |
| 2501 | * return confirmation of parity correctness |
| 2502 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2503 | * buffers are taken off read_list or write_list, and bh_cache buffers |
| 2504 | * get BH_Lock set before the stripe lock is released. |
| 2505 | * |
| 2506 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2507 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2508 | static void handle_stripe5(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2509 | { |
| 2510 | raid5_conf_t *conf = sh->raid_conf; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2511 | int disks = sh->disks, i; |
| 2512 | struct bio *return_bi = NULL; |
| 2513 | struct stripe_head_state s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2514 | struct r5dev *dev; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2515 | mdk_rdev_t *blocked_rdev = NULL; |
Dan Williams | e0a115e | 2008-06-05 22:45:52 -0700 | [diff] [blame] | 2516 | int prexor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2517 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2518 | memset(&s, 0, sizeof(s)); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2519 | pr_debug("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d check:%d " |
| 2520 | "reconstruct:%d\n", (unsigned long long)sh->sector, sh->state, |
| 2521 | atomic_read(&sh->count), sh->pd_idx, sh->check_state, |
| 2522 | sh->reconstruct_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2523 | |
| 2524 | spin_lock(&sh->lock); |
| 2525 | clear_bit(STRIPE_HANDLE, &sh->state); |
| 2526 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 2527 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2528 | s.syncing = test_bit(STRIPE_SYNCING, &sh->state); |
| 2529 | s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 2530 | s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state); |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 2531 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2532 | /* Now to look around and see what can be done */ |
NeilBrown | 9910f16 | 2006-01-06 00:20:24 -0800 | [diff] [blame] | 2533 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2534 | for (i=disks; i--; ) { |
| 2535 | mdk_rdev_t *rdev; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2536 | struct r5dev *dev = &sh->dev[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2537 | clear_bit(R5_Insync, &dev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2538 | |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2539 | pr_debug("check %d: state 0x%lx toread %p read %p write %p " |
| 2540 | "written %p\n", i, dev->flags, dev->toread, dev->read, |
| 2541 | dev->towrite, dev->written); |
| 2542 | |
| 2543 | /* maybe we can request a biofill operation |
| 2544 | * |
| 2545 | * new wantfill requests are only permitted while |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 2546 | * ops_complete_biofill is guaranteed to be inactive |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2547 | */ |
| 2548 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread && |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 2549 | !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2550 | set_bit(R5_Wantfill, &dev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2551 | |
| 2552 | /* now count some things */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2553 | if (test_bit(R5_LOCKED, &dev->flags)) s.locked++; |
| 2554 | if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2555 | if (test_bit(R5_Wantcompute, &dev->flags)) s.compute++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2556 | |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2557 | if (test_bit(R5_Wantfill, &dev->flags)) |
| 2558 | s.to_fill++; |
| 2559 | else if (dev->toread) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2560 | s.to_read++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2561 | if (dev->towrite) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2562 | s.to_write++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2563 | if (!test_bit(R5_OVERWRITE, &dev->flags)) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2564 | s.non_overwrite++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2565 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2566 | if (dev->written) |
| 2567 | s.written++; |
NeilBrown | 9910f16 | 2006-01-06 00:20:24 -0800 | [diff] [blame] | 2568 | rdev = rcu_dereference(conf->disks[i].rdev); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2569 | if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) { |
| 2570 | blocked_rdev = rdev; |
| 2571 | atomic_inc(&rdev->nr_pending); |
| 2572 | break; |
| 2573 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2574 | if (!rdev || !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 2575 | /* The ReadError flag will just be confusing now */ |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2576 | clear_bit(R5_ReadError, &dev->flags); |
| 2577 | clear_bit(R5_ReWrite, &dev->flags); |
| 2578 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2579 | if (!rdev || !test_bit(In_sync, &rdev->flags) |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2580 | || test_bit(R5_ReadError, &dev->flags)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2581 | s.failed++; |
| 2582 | s.failed_num = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2583 | } else |
| 2584 | set_bit(R5_Insync, &dev->flags); |
| 2585 | } |
NeilBrown | 9910f16 | 2006-01-06 00:20:24 -0800 | [diff] [blame] | 2586 | rcu_read_unlock(); |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2587 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2588 | if (unlikely(blocked_rdev)) { |
| 2589 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2590 | goto unlock; |
| 2591 | } |
| 2592 | |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 2593 | if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) { |
| 2594 | set_bit(STRIPE_OP_BIOFILL, &s.ops_request); |
| 2595 | set_bit(STRIPE_BIOFILL_RUN, &sh->state); |
| 2596 | } |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2597 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2598 | pr_debug("locked=%d uptodate=%d to_read=%d" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2599 | " to_write=%d failed=%d failed_num=%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2600 | s.locked, s.uptodate, s.to_read, s.to_write, |
| 2601 | s.failed, s.failed_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2602 | /* check if the array has lost two devices and, if so, some requests might |
| 2603 | * need to be failed |
| 2604 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2605 | if (s.failed > 1 && s.to_read+s.to_write+s.written) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame^] | 2606 | handle_failed_stripe(conf, sh, &s, disks, &return_bi); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2607 | if (s.failed > 1 && s.syncing) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2608 | md_done_sync(conf->mddev, STRIPE_SECTORS,0); |
| 2609 | clear_bit(STRIPE_SYNCING, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2610 | s.syncing = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2611 | } |
| 2612 | |
| 2613 | /* might be able to return some write requests if the parity block |
| 2614 | * is safe, or on a failed drive |
| 2615 | */ |
| 2616 | dev = &sh->dev[sh->pd_idx]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2617 | if ( s.written && |
| 2618 | ((test_bit(R5_Insync, &dev->flags) && |
| 2619 | !test_bit(R5_LOCKED, &dev->flags) && |
| 2620 | test_bit(R5_UPTODATE, &dev->flags)) || |
| 2621 | (s.failed == 1 && s.failed_num == sh->pd_idx))) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame^] | 2622 | handle_stripe_clean_event(conf, sh, disks, &return_bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2623 | |
| 2624 | /* Now we might consider reading some blocks, either to check/generate |
| 2625 | * parity, or to satisfy requests |
| 2626 | * or to load a block that is being partially written. |
| 2627 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2628 | if (s.to_read || s.non_overwrite || |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2629 | (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame^] | 2630 | handle_stripe_fill5(sh, &s, disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2631 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2632 | /* Now we check to see if any write operations have recently |
| 2633 | * completed |
| 2634 | */ |
Dan Williams | e0a115e | 2008-06-05 22:45:52 -0700 | [diff] [blame] | 2635 | prexor = 0; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2636 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_result) |
| 2637 | prexor = 1; |
| 2638 | if (sh->reconstruct_state == reconstruct_state_drain_result || |
| 2639 | sh->reconstruct_state == reconstruct_state_prexor_drain_result) { |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2640 | sh->reconstruct_state = reconstruct_state_idle; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2641 | |
| 2642 | /* All the 'written' buffers and the parity block are ready to |
| 2643 | * be written back to disk |
| 2644 | */ |
| 2645 | BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags)); |
| 2646 | for (i = disks; i--; ) { |
| 2647 | dev = &sh->dev[i]; |
| 2648 | if (test_bit(R5_LOCKED, &dev->flags) && |
| 2649 | (i == sh->pd_idx || dev->written)) { |
| 2650 | pr_debug("Writing block %d\n", i); |
| 2651 | set_bit(R5_Wantwrite, &dev->flags); |
Dan Williams | e0a115e | 2008-06-05 22:45:52 -0700 | [diff] [blame] | 2652 | if (prexor) |
| 2653 | continue; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2654 | if (!test_bit(R5_Insync, &dev->flags) || |
| 2655 | (i == sh->pd_idx && s.failed == 0)) |
| 2656 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2657 | } |
| 2658 | } |
| 2659 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
| 2660 | atomic_dec(&conf->preread_active_stripes); |
| 2661 | if (atomic_read(&conf->preread_active_stripes) < |
| 2662 | IO_THRESHOLD) |
| 2663 | md_wakeup_thread(conf->mddev->thread); |
| 2664 | } |
| 2665 | } |
| 2666 | |
| 2667 | /* Now to consider new write requests and what else, if anything |
| 2668 | * should be read. We do not handle new writes when: |
| 2669 | * 1/ A 'write' operation (copy+xor) is already in flight. |
| 2670 | * 2/ A 'check' operation is in flight, as it may clobber the parity |
| 2671 | * block. |
| 2672 | */ |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2673 | if (s.to_write && !sh->reconstruct_state && !sh->check_state) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame^] | 2674 | handle_stripe_dirtying5(conf, sh, &s, disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2675 | |
| 2676 | /* maybe we need to check and possibly fix the parity for this stripe |
Dan Williams | e89f896 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2677 | * Any reads will already have been scheduled, so we just see if enough |
| 2678 | * data is available. The parity check is held off while parity |
| 2679 | * dependent operations are in flight. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2680 | */ |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2681 | if (sh->check_state || |
| 2682 | (s.syncing && s.locked == 0 && |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2683 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2684 | !test_bit(STRIPE_INSYNC, &sh->state))) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2685 | handle_parity_checks5(conf, sh, &s, disks); |
Dan Williams | e89f896 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2686 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2687 | if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2688 | md_done_sync(conf->mddev, STRIPE_SECTORS,1); |
| 2689 | clear_bit(STRIPE_SYNCING, &sh->state); |
| 2690 | } |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2691 | |
| 2692 | /* If the failed drive is just a ReadError, then we might need to progress |
| 2693 | * the repair/check process |
| 2694 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2695 | if (s.failed == 1 && !conf->mddev->ro && |
| 2696 | test_bit(R5_ReadError, &sh->dev[s.failed_num].flags) |
| 2697 | && !test_bit(R5_LOCKED, &sh->dev[s.failed_num].flags) |
| 2698 | && test_bit(R5_UPTODATE, &sh->dev[s.failed_num].flags) |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2699 | ) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2700 | dev = &sh->dev[s.failed_num]; |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2701 | if (!test_bit(R5_ReWrite, &dev->flags)) { |
| 2702 | set_bit(R5_Wantwrite, &dev->flags); |
| 2703 | set_bit(R5_ReWrite, &dev->flags); |
| 2704 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2705 | s.locked++; |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2706 | } else { |
| 2707 | /* let's read it back */ |
| 2708 | set_bit(R5_Wantread, &dev->flags); |
| 2709 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2710 | s.locked++; |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2711 | } |
| 2712 | } |
| 2713 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2714 | /* Finish reconstruct operations initiated by the expansion process */ |
| 2715 | if (sh->reconstruct_state == reconstruct_state_result) { |
| 2716 | sh->reconstruct_state = reconstruct_state_idle; |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2717 | clear_bit(STRIPE_EXPANDING, &sh->state); |
Dan Williams | 2b7497f | 2008-06-28 08:31:52 +1000 | [diff] [blame] | 2718 | for (i = conf->raid_disks; i--; ) |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2719 | set_bit(R5_Wantwrite, &sh->dev[i].flags); |
Neil Brown | efe3114 | 2008-06-28 08:31:14 +1000 | [diff] [blame] | 2720 | set_bit(R5_LOCKED, &dev->flags); |
| 2721 | s.locked++; |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2722 | } |
| 2723 | |
| 2724 | if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) && |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2725 | !sh->reconstruct_state) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2726 | /* Need to write out all blocks after computing parity */ |
| 2727 | sh->disks = conf->raid_disks; |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2728 | sh->pd_idx = stripe_to_pdidx(sh->sector, conf, |
| 2729 | conf->raid_disks); |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame^] | 2730 | schedule_reconstruction5(sh, &s, 1, 1); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2731 | } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2732 | clear_bit(STRIPE_EXPAND_READY, &sh->state); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 2733 | atomic_dec(&conf->reshape_stripes); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2734 | wake_up(&conf->wait_for_overlap); |
| 2735 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 2736 | } |
| 2737 | |
Dan Williams | 0f94e87 | 2008-01-08 15:32:53 -0800 | [diff] [blame] | 2738 | if (s.expanding && s.locked == 0 && |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2739 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2740 | handle_stripe_expansion(conf, sh, NULL); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2741 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2742 | unlock: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2743 | spin_unlock(&sh->lock); |
| 2744 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2745 | /* wait for this device to become unblocked */ |
| 2746 | if (unlikely(blocked_rdev)) |
| 2747 | md_wait_for_blocked_rdev(blocked_rdev, conf->mddev); |
| 2748 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2749 | if (s.ops_request) |
| 2750 | raid5_run_ops(sh, s.ops_request); |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2751 | |
Dan Williams | c4e5ac0 | 2008-06-28 08:31:53 +1000 | [diff] [blame] | 2752 | ops_run_io(sh, &s); |
Dan Williams | 2b7497f | 2008-06-28 08:31:52 +1000 | [diff] [blame] | 2753 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2754 | return_io(return_bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2755 | } |
| 2756 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2757 | static void handle_stripe6(struct stripe_head *sh, struct page *tmp_page) |
| 2758 | { |
| 2759 | raid6_conf_t *conf = sh->raid_conf; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 2760 | int disks = sh->disks; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2761 | struct bio *return_bi = NULL; |
| 2762 | int i, pd_idx = sh->pd_idx; |
| 2763 | struct stripe_head_state s; |
| 2764 | struct r6_state r6s; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2765 | struct r5dev *dev, *pdev, *qdev; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2766 | mdk_rdev_t *blocked_rdev = NULL; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2767 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2768 | r6s.qd_idx = raid6_next_disk(pd_idx, disks); |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2769 | pr_debug("handling stripe %llu, state=%#lx cnt=%d, " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2770 | "pd_idx=%d, qd_idx=%d\n", |
| 2771 | (unsigned long long)sh->sector, sh->state, |
| 2772 | atomic_read(&sh->count), pd_idx, r6s.qd_idx); |
| 2773 | memset(&s, 0, sizeof(s)); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2774 | |
| 2775 | spin_lock(&sh->lock); |
| 2776 | clear_bit(STRIPE_HANDLE, &sh->state); |
| 2777 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 2778 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2779 | s.syncing = test_bit(STRIPE_SYNCING, &sh->state); |
| 2780 | s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 2781 | s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2782 | /* Now to look around and see what can be done */ |
| 2783 | |
| 2784 | rcu_read_lock(); |
| 2785 | for (i=disks; i--; ) { |
| 2786 | mdk_rdev_t *rdev; |
| 2787 | dev = &sh->dev[i]; |
| 2788 | clear_bit(R5_Insync, &dev->flags); |
| 2789 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2790 | pr_debug("check %d: state 0x%lx read %p write %p written %p\n", |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2791 | i, dev->flags, dev->toread, dev->towrite, dev->written); |
| 2792 | /* maybe we can reply to a read */ |
| 2793 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) { |
| 2794 | struct bio *rbi, *rbi2; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2795 | pr_debug("Return read for disc %d\n", i); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2796 | spin_lock_irq(&conf->device_lock); |
| 2797 | rbi = dev->toread; |
| 2798 | dev->toread = NULL; |
| 2799 | if (test_and_clear_bit(R5_Overlap, &dev->flags)) |
| 2800 | wake_up(&conf->wait_for_overlap); |
| 2801 | spin_unlock_irq(&conf->device_lock); |
| 2802 | while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) { |
| 2803 | copy_data(0, rbi, dev->page, dev->sector); |
| 2804 | rbi2 = r5_next_bio(rbi, dev->sector); |
| 2805 | spin_lock_irq(&conf->device_lock); |
| 2806 | if (--rbi->bi_phys_segments == 0) { |
| 2807 | rbi->bi_next = return_bi; |
| 2808 | return_bi = rbi; |
| 2809 | } |
| 2810 | spin_unlock_irq(&conf->device_lock); |
| 2811 | rbi = rbi2; |
| 2812 | } |
| 2813 | } |
| 2814 | |
| 2815 | /* now count some things */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2816 | if (test_bit(R5_LOCKED, &dev->flags)) s.locked++; |
| 2817 | if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2818 | |
| 2819 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2820 | if (dev->toread) |
| 2821 | s.to_read++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2822 | if (dev->towrite) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2823 | s.to_write++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2824 | if (!test_bit(R5_OVERWRITE, &dev->flags)) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2825 | s.non_overwrite++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2826 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2827 | if (dev->written) |
| 2828 | s.written++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2829 | rdev = rcu_dereference(conf->disks[i].rdev); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2830 | if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) { |
| 2831 | blocked_rdev = rdev; |
| 2832 | atomic_inc(&rdev->nr_pending); |
| 2833 | break; |
| 2834 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2835 | if (!rdev || !test_bit(In_sync, &rdev->flags)) { |
| 2836 | /* The ReadError flag will just be confusing now */ |
| 2837 | clear_bit(R5_ReadError, &dev->flags); |
| 2838 | clear_bit(R5_ReWrite, &dev->flags); |
| 2839 | } |
| 2840 | if (!rdev || !test_bit(In_sync, &rdev->flags) |
| 2841 | || test_bit(R5_ReadError, &dev->flags)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2842 | if (s.failed < 2) |
| 2843 | r6s.failed_num[s.failed] = i; |
| 2844 | s.failed++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2845 | } else |
| 2846 | set_bit(R5_Insync, &dev->flags); |
| 2847 | } |
| 2848 | rcu_read_unlock(); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2849 | |
| 2850 | if (unlikely(blocked_rdev)) { |
| 2851 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2852 | goto unlock; |
| 2853 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2854 | pr_debug("locked=%d uptodate=%d to_read=%d" |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2855 | " to_write=%d failed=%d failed_num=%d,%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2856 | s.locked, s.uptodate, s.to_read, s.to_write, s.failed, |
| 2857 | r6s.failed_num[0], r6s.failed_num[1]); |
| 2858 | /* check if the array has lost >2 devices and, if so, some requests |
| 2859 | * might need to be failed |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2860 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2861 | if (s.failed > 2 && s.to_read+s.to_write+s.written) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame^] | 2862 | handle_failed_stripe(conf, sh, &s, disks, &return_bi); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2863 | if (s.failed > 2 && s.syncing) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2864 | md_done_sync(conf->mddev, STRIPE_SECTORS,0); |
| 2865 | clear_bit(STRIPE_SYNCING, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2866 | s.syncing = 0; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2867 | } |
| 2868 | |
| 2869 | /* |
| 2870 | * might be able to return some write requests if the parity blocks |
| 2871 | * are safe, or on a failed drive |
| 2872 | */ |
| 2873 | pdev = &sh->dev[pd_idx]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2874 | r6s.p_failed = (s.failed >= 1 && r6s.failed_num[0] == pd_idx) |
| 2875 | || (s.failed >= 2 && r6s.failed_num[1] == pd_idx); |
| 2876 | qdev = &sh->dev[r6s.qd_idx]; |
| 2877 | r6s.q_failed = (s.failed >= 1 && r6s.failed_num[0] == r6s.qd_idx) |
| 2878 | || (s.failed >= 2 && r6s.failed_num[1] == r6s.qd_idx); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2879 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2880 | if ( s.written && |
| 2881 | ( r6s.p_failed || ((test_bit(R5_Insync, &pdev->flags) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2882 | && !test_bit(R5_LOCKED, &pdev->flags) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2883 | && test_bit(R5_UPTODATE, &pdev->flags)))) && |
| 2884 | ( r6s.q_failed || ((test_bit(R5_Insync, &qdev->flags) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2885 | && !test_bit(R5_LOCKED, &qdev->flags) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2886 | && test_bit(R5_UPTODATE, &qdev->flags))))) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame^] | 2887 | handle_stripe_clean_event(conf, sh, disks, &return_bi); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2888 | |
| 2889 | /* Now we might consider reading some blocks, either to check/generate |
| 2890 | * parity, or to satisfy requests |
| 2891 | * or to load a block that is being partially written. |
| 2892 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2893 | if (s.to_read || s.non_overwrite || (s.to_write && s.failed) || |
| 2894 | (s.syncing && (s.uptodate < disks)) || s.expanding) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame^] | 2895 | handle_stripe_fill6(sh, &s, &r6s, disks); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2896 | |
| 2897 | /* now to consider writing and what else, if anything should be read */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2898 | if (s.to_write) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame^] | 2899 | handle_stripe_dirtying6(conf, sh, &s, &r6s, disks); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2900 | |
| 2901 | /* maybe we need to check and possibly fix the parity for this stripe |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2902 | * Any reads will already have been scheduled, so we just see if enough |
| 2903 | * data is available |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2904 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2905 | if (s.syncing && s.locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state)) |
| 2906 | handle_parity_checks6(conf, sh, &s, &r6s, tmp_page, disks); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2907 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2908 | if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2909 | md_done_sync(conf->mddev, STRIPE_SECTORS,1); |
| 2910 | clear_bit(STRIPE_SYNCING, &sh->state); |
| 2911 | } |
| 2912 | |
| 2913 | /* If the failed drives are just a ReadError, then we might need |
| 2914 | * to progress the repair/check process |
| 2915 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2916 | if (s.failed <= 2 && !conf->mddev->ro) |
| 2917 | for (i = 0; i < s.failed; i++) { |
| 2918 | dev = &sh->dev[r6s.failed_num[i]]; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2919 | if (test_bit(R5_ReadError, &dev->flags) |
| 2920 | && !test_bit(R5_LOCKED, &dev->flags) |
| 2921 | && test_bit(R5_UPTODATE, &dev->flags) |
| 2922 | ) { |
| 2923 | if (!test_bit(R5_ReWrite, &dev->flags)) { |
| 2924 | set_bit(R5_Wantwrite, &dev->flags); |
| 2925 | set_bit(R5_ReWrite, &dev->flags); |
| 2926 | set_bit(R5_LOCKED, &dev->flags); |
| 2927 | } else { |
| 2928 | /* let's read it back */ |
| 2929 | set_bit(R5_Wantread, &dev->flags); |
| 2930 | set_bit(R5_LOCKED, &dev->flags); |
| 2931 | } |
| 2932 | } |
| 2933 | } |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 2934 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2935 | if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state)) { |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 2936 | /* Need to write out all blocks after computing P&Q */ |
| 2937 | sh->disks = conf->raid_disks; |
| 2938 | sh->pd_idx = stripe_to_pdidx(sh->sector, conf, |
| 2939 | conf->raid_disks); |
| 2940 | compute_parity6(sh, RECONSTRUCT_WRITE); |
| 2941 | for (i = conf->raid_disks ; i-- ; ) { |
| 2942 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2943 | s.locked++; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 2944 | set_bit(R5_Wantwrite, &sh->dev[i].flags); |
| 2945 | } |
| 2946 | clear_bit(STRIPE_EXPANDING, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2947 | } else if (s.expanded) { |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 2948 | clear_bit(STRIPE_EXPAND_READY, &sh->state); |
| 2949 | atomic_dec(&conf->reshape_stripes); |
| 2950 | wake_up(&conf->wait_for_overlap); |
| 2951 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 2952 | } |
| 2953 | |
Dan Williams | 0f94e87 | 2008-01-08 15:32:53 -0800 | [diff] [blame] | 2954 | if (s.expanding && s.locked == 0 && |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2955 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2956 | handle_stripe_expansion(conf, sh, &r6s); |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 2957 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2958 | unlock: |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2959 | spin_unlock(&sh->lock); |
| 2960 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2961 | /* wait for this device to become unblocked */ |
| 2962 | if (unlikely(blocked_rdev)) |
| 2963 | md_wait_for_blocked_rdev(blocked_rdev, conf->mddev); |
| 2964 | |
Dan Williams | f0e43bc | 2008-06-28 08:31:55 +1000 | [diff] [blame] | 2965 | ops_run_io(sh, &s); |
| 2966 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2967 | return_io(return_bi); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2968 | } |
| 2969 | |
| 2970 | static void handle_stripe(struct stripe_head *sh, struct page *tmp_page) |
| 2971 | { |
| 2972 | if (sh->raid_conf->level == 6) |
| 2973 | handle_stripe6(sh, tmp_page); |
| 2974 | else |
| 2975 | handle_stripe5(sh); |
| 2976 | } |
| 2977 | |
| 2978 | |
| 2979 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 2980 | static void raid5_activate_delayed(raid5_conf_t *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2981 | { |
| 2982 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) { |
| 2983 | while (!list_empty(&conf->delayed_list)) { |
| 2984 | struct list_head *l = conf->delayed_list.next; |
| 2985 | struct stripe_head *sh; |
| 2986 | sh = list_entry(l, struct stripe_head, lru); |
| 2987 | list_del_init(l); |
| 2988 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 2989 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 2990 | atomic_inc(&conf->preread_active_stripes); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2991 | list_add_tail(&sh->lru, &conf->hold_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2992 | } |
NeilBrown | 6ed3003 | 2008-02-06 01:40:00 -0800 | [diff] [blame] | 2993 | } else |
| 2994 | blk_plug_device(conf->mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2995 | } |
| 2996 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 2997 | static void activate_bit_delay(raid5_conf_t *conf) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2998 | { |
| 2999 | /* device_lock is held */ |
| 3000 | struct list_head head; |
| 3001 | list_add(&head, &conf->bitmap_list); |
| 3002 | list_del_init(&conf->bitmap_list); |
| 3003 | while (!list_empty(&head)) { |
| 3004 | struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru); |
| 3005 | list_del_init(&sh->lru); |
| 3006 | atomic_inc(&sh->count); |
| 3007 | __release_stripe(conf, sh); |
| 3008 | } |
| 3009 | } |
| 3010 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3011 | static void unplug_slaves(mddev_t *mddev) |
| 3012 | { |
| 3013 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 3014 | int i; |
| 3015 | |
| 3016 | rcu_read_lock(); |
| 3017 | for (i=0; i<mddev->raid_disks; i++) { |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 3018 | mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3019 | if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3020 | struct request_queue *r_queue = bdev_get_queue(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3021 | |
| 3022 | atomic_inc(&rdev->nr_pending); |
| 3023 | rcu_read_unlock(); |
| 3024 | |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 3025 | blk_unplug(r_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3026 | |
| 3027 | rdev_dec_pending(rdev, mddev); |
| 3028 | rcu_read_lock(); |
| 3029 | } |
| 3030 | } |
| 3031 | rcu_read_unlock(); |
| 3032 | } |
| 3033 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3034 | static void raid5_unplug_device(struct request_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3035 | { |
| 3036 | mddev_t *mddev = q->queuedata; |
| 3037 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 3038 | unsigned long flags; |
| 3039 | |
| 3040 | spin_lock_irqsave(&conf->device_lock, flags); |
| 3041 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3042 | if (blk_remove_plug(q)) { |
| 3043 | conf->seq_flush++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3044 | raid5_activate_delayed(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3045 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3046 | md_wakeup_thread(mddev->thread); |
| 3047 | |
| 3048 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 3049 | |
| 3050 | unplug_slaves(mddev); |
| 3051 | } |
| 3052 | |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3053 | static int raid5_congested(void *data, int bits) |
| 3054 | { |
| 3055 | mddev_t *mddev = data; |
| 3056 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 3057 | |
| 3058 | /* No difference between reads and writes. Just check |
| 3059 | * how busy the stripe_cache is |
| 3060 | */ |
| 3061 | if (conf->inactive_blocked) |
| 3062 | return 1; |
| 3063 | if (conf->quiesce) |
| 3064 | return 1; |
| 3065 | if (list_empty_careful(&conf->inactive_list)) |
| 3066 | return 1; |
| 3067 | |
| 3068 | return 0; |
| 3069 | } |
| 3070 | |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3071 | /* We want read requests to align with chunks where possible, |
| 3072 | * but write requests don't need to. |
| 3073 | */ |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3074 | static int raid5_mergeable_bvec(struct request_queue *q, struct bio *bio, struct bio_vec *biovec) |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3075 | { |
| 3076 | mddev_t *mddev = q->queuedata; |
| 3077 | sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev); |
| 3078 | int max; |
| 3079 | unsigned int chunk_sectors = mddev->chunk_size >> 9; |
| 3080 | unsigned int bio_sectors = bio->bi_size >> 9; |
| 3081 | |
NeilBrown | 802ba06 | 2006-12-13 00:34:13 -0800 | [diff] [blame] | 3082 | if (bio_data_dir(bio) == WRITE) |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3083 | return biovec->bv_len; /* always allow writes to be mergeable */ |
| 3084 | |
| 3085 | max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9; |
| 3086 | if (max < 0) max = 0; |
| 3087 | if (max <= biovec->bv_len && bio_sectors == 0) |
| 3088 | return biovec->bv_len; |
| 3089 | else |
| 3090 | return max; |
| 3091 | } |
| 3092 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3093 | |
| 3094 | static int in_chunk_boundary(mddev_t *mddev, struct bio *bio) |
| 3095 | { |
| 3096 | sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev); |
| 3097 | unsigned int chunk_sectors = mddev->chunk_size >> 9; |
| 3098 | unsigned int bio_sectors = bio->bi_size >> 9; |
| 3099 | |
| 3100 | return chunk_sectors >= |
| 3101 | ((sector & (chunk_sectors - 1)) + bio_sectors); |
| 3102 | } |
| 3103 | |
| 3104 | /* |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3105 | * add bio to the retry LIFO ( in O(1) ... we are in interrupt ) |
| 3106 | * later sampled by raid5d. |
| 3107 | */ |
| 3108 | static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf) |
| 3109 | { |
| 3110 | unsigned long flags; |
| 3111 | |
| 3112 | spin_lock_irqsave(&conf->device_lock, flags); |
| 3113 | |
| 3114 | bi->bi_next = conf->retry_read_aligned_list; |
| 3115 | conf->retry_read_aligned_list = bi; |
| 3116 | |
| 3117 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 3118 | md_wakeup_thread(conf->mddev->thread); |
| 3119 | } |
| 3120 | |
| 3121 | |
| 3122 | static struct bio *remove_bio_from_retry(raid5_conf_t *conf) |
| 3123 | { |
| 3124 | struct bio *bi; |
| 3125 | |
| 3126 | bi = conf->retry_read_aligned; |
| 3127 | if (bi) { |
| 3128 | conf->retry_read_aligned = NULL; |
| 3129 | return bi; |
| 3130 | } |
| 3131 | bi = conf->retry_read_aligned_list; |
| 3132 | if(bi) { |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3133 | conf->retry_read_aligned_list = bi->bi_next; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3134 | bi->bi_next = NULL; |
| 3135 | bi->bi_phys_segments = 1; /* biased count of active stripes */ |
| 3136 | bi->bi_hw_segments = 0; /* count of processed stripes */ |
| 3137 | } |
| 3138 | |
| 3139 | return bi; |
| 3140 | } |
| 3141 | |
| 3142 | |
| 3143 | /* |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3144 | * The "raid5_align_endio" should check if the read succeeded and if it |
| 3145 | * did, call bio_endio on the original bio (having bio_put the new bio |
| 3146 | * first). |
| 3147 | * If the read failed.. |
| 3148 | */ |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3149 | static void raid5_align_endio(struct bio *bi, int error) |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3150 | { |
| 3151 | struct bio* raid_bi = bi->bi_private; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3152 | mddev_t *mddev; |
| 3153 | raid5_conf_t *conf; |
| 3154 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
| 3155 | mdk_rdev_t *rdev; |
| 3156 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3157 | bio_put(bi); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3158 | |
| 3159 | mddev = raid_bi->bi_bdev->bd_disk->queue->queuedata; |
| 3160 | conf = mddev_to_conf(mddev); |
| 3161 | rdev = (void*)raid_bi->bi_next; |
| 3162 | raid_bi->bi_next = NULL; |
| 3163 | |
| 3164 | rdev_dec_pending(rdev, conf->mddev); |
| 3165 | |
| 3166 | if (!error && uptodate) { |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3167 | bio_endio(raid_bi, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3168 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
| 3169 | wake_up(&conf->wait_for_stripe); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3170 | return; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3171 | } |
| 3172 | |
| 3173 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3174 | pr_debug("raid5_align_endio : io error...handing IO for a retry\n"); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3175 | |
| 3176 | add_bio_to_retry(raid_bi, conf); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3177 | } |
| 3178 | |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3179 | static int bio_fits_rdev(struct bio *bi) |
| 3180 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3181 | struct request_queue *q = bdev_get_queue(bi->bi_bdev); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3182 | |
| 3183 | if ((bi->bi_size>>9) > q->max_sectors) |
| 3184 | return 0; |
| 3185 | blk_recount_segments(q, bi); |
| 3186 | if (bi->bi_phys_segments > q->max_phys_segments || |
| 3187 | bi->bi_hw_segments > q->max_hw_segments) |
| 3188 | return 0; |
| 3189 | |
| 3190 | if (q->merge_bvec_fn) |
| 3191 | /* it's too hard to apply the merge_bvec_fn at this stage, |
| 3192 | * just just give up |
| 3193 | */ |
| 3194 | return 0; |
| 3195 | |
| 3196 | return 1; |
| 3197 | } |
| 3198 | |
| 3199 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3200 | static int chunk_aligned_read(struct request_queue *q, struct bio * raid_bio) |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3201 | { |
| 3202 | mddev_t *mddev = q->queuedata; |
| 3203 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 3204 | const unsigned int raid_disks = conf->raid_disks; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3205 | const unsigned int data_disks = raid_disks - conf->max_degraded; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3206 | unsigned int dd_idx, pd_idx; |
| 3207 | struct bio* align_bi; |
| 3208 | mdk_rdev_t *rdev; |
| 3209 | |
| 3210 | if (!in_chunk_boundary(mddev, raid_bio)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3211 | pr_debug("chunk_aligned_read : non aligned\n"); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3212 | return 0; |
| 3213 | } |
| 3214 | /* |
| 3215 | * use bio_clone to make a copy of the bio |
| 3216 | */ |
| 3217 | align_bi = bio_clone(raid_bio, GFP_NOIO); |
| 3218 | if (!align_bi) |
| 3219 | return 0; |
| 3220 | /* |
| 3221 | * set bi_end_io to a new function, and set bi_private to the |
| 3222 | * original bio. |
| 3223 | */ |
| 3224 | align_bi->bi_end_io = raid5_align_endio; |
| 3225 | align_bi->bi_private = raid_bio; |
| 3226 | /* |
| 3227 | * compute position |
| 3228 | */ |
| 3229 | align_bi->bi_sector = raid5_compute_sector(raid_bio->bi_sector, |
| 3230 | raid_disks, |
| 3231 | data_disks, |
| 3232 | &dd_idx, |
| 3233 | &pd_idx, |
| 3234 | conf); |
| 3235 | |
| 3236 | rcu_read_lock(); |
| 3237 | rdev = rcu_dereference(conf->disks[dd_idx].rdev); |
| 3238 | if (rdev && test_bit(In_sync, &rdev->flags)) { |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3239 | atomic_inc(&rdev->nr_pending); |
| 3240 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3241 | raid_bio->bi_next = (void*)rdev; |
| 3242 | align_bi->bi_bdev = rdev->bdev; |
| 3243 | align_bi->bi_flags &= ~(1 << BIO_SEG_VALID); |
| 3244 | align_bi->bi_sector += rdev->data_offset; |
| 3245 | |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3246 | if (!bio_fits_rdev(align_bi)) { |
| 3247 | /* too big in some way */ |
| 3248 | bio_put(align_bi); |
| 3249 | rdev_dec_pending(rdev, mddev); |
| 3250 | return 0; |
| 3251 | } |
| 3252 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3253 | spin_lock_irq(&conf->device_lock); |
| 3254 | wait_event_lock_irq(conf->wait_for_stripe, |
| 3255 | conf->quiesce == 0, |
| 3256 | conf->device_lock, /* nothing */); |
| 3257 | atomic_inc(&conf->active_aligned_reads); |
| 3258 | spin_unlock_irq(&conf->device_lock); |
| 3259 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3260 | generic_make_request(align_bi); |
| 3261 | return 1; |
| 3262 | } else { |
| 3263 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3264 | bio_put(align_bi); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3265 | return 0; |
| 3266 | } |
| 3267 | } |
| 3268 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3269 | /* __get_priority_stripe - get the next stripe to process |
| 3270 | * |
| 3271 | * Full stripe writes are allowed to pass preread active stripes up until |
| 3272 | * the bypass_threshold is exceeded. In general the bypass_count |
| 3273 | * increments when the handle_list is handled before the hold_list; however, it |
| 3274 | * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a |
| 3275 | * stripe with in flight i/o. The bypass_count will be reset when the |
| 3276 | * head of the hold_list has changed, i.e. the head was promoted to the |
| 3277 | * handle_list. |
| 3278 | */ |
| 3279 | static struct stripe_head *__get_priority_stripe(raid5_conf_t *conf) |
| 3280 | { |
| 3281 | struct stripe_head *sh; |
| 3282 | |
| 3283 | pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n", |
| 3284 | __func__, |
| 3285 | list_empty(&conf->handle_list) ? "empty" : "busy", |
| 3286 | list_empty(&conf->hold_list) ? "empty" : "busy", |
| 3287 | atomic_read(&conf->pending_full_writes), conf->bypass_count); |
| 3288 | |
| 3289 | if (!list_empty(&conf->handle_list)) { |
| 3290 | sh = list_entry(conf->handle_list.next, typeof(*sh), lru); |
| 3291 | |
| 3292 | if (list_empty(&conf->hold_list)) |
| 3293 | conf->bypass_count = 0; |
| 3294 | else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) { |
| 3295 | if (conf->hold_list.next == conf->last_hold) |
| 3296 | conf->bypass_count++; |
| 3297 | else { |
| 3298 | conf->last_hold = conf->hold_list.next; |
| 3299 | conf->bypass_count -= conf->bypass_threshold; |
| 3300 | if (conf->bypass_count < 0) |
| 3301 | conf->bypass_count = 0; |
| 3302 | } |
| 3303 | } |
| 3304 | } else if (!list_empty(&conf->hold_list) && |
| 3305 | ((conf->bypass_threshold && |
| 3306 | conf->bypass_count > conf->bypass_threshold) || |
| 3307 | atomic_read(&conf->pending_full_writes) == 0)) { |
| 3308 | sh = list_entry(conf->hold_list.next, |
| 3309 | typeof(*sh), lru); |
| 3310 | conf->bypass_count -= conf->bypass_threshold; |
| 3311 | if (conf->bypass_count < 0) |
| 3312 | conf->bypass_count = 0; |
| 3313 | } else |
| 3314 | return NULL; |
| 3315 | |
| 3316 | list_del_init(&sh->lru); |
| 3317 | atomic_inc(&sh->count); |
| 3318 | BUG_ON(atomic_read(&sh->count) != 1); |
| 3319 | return sh; |
| 3320 | } |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3321 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3322 | static int make_request(struct request_queue *q, struct bio * bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3323 | { |
| 3324 | mddev_t *mddev = q->queuedata; |
| 3325 | raid5_conf_t *conf = mddev_to_conf(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3326 | unsigned int dd_idx, pd_idx; |
| 3327 | sector_t new_sector; |
| 3328 | sector_t logical_sector, last_sector; |
| 3329 | struct stripe_head *sh; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 3330 | const int rw = bio_data_dir(bi); |
NeilBrown | f634475 | 2006-03-27 01:18:17 -0800 | [diff] [blame] | 3331 | int remaining; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3332 | |
NeilBrown | e5dcdd8 | 2005-09-09 16:23:41 -0700 | [diff] [blame] | 3333 | if (unlikely(bio_barrier(bi))) { |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3334 | bio_endio(bi, -EOPNOTSUPP); |
NeilBrown | e5dcdd8 | 2005-09-09 16:23:41 -0700 | [diff] [blame] | 3335 | return 0; |
| 3336 | } |
| 3337 | |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 3338 | md_write_start(mddev, bi); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 3339 | |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 3340 | disk_stat_inc(mddev->gendisk, ios[rw]); |
| 3341 | disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3342 | |
NeilBrown | 802ba06 | 2006-12-13 00:34:13 -0800 | [diff] [blame] | 3343 | if (rw == READ && |
Raz Ben-Jehuda(caro) | 5248861 | 2006-12-10 02:20:48 -0800 | [diff] [blame] | 3344 | mddev->reshape_position == MaxSector && |
| 3345 | chunk_aligned_read(q,bi)) |
| 3346 | return 0; |
| 3347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3348 | logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
| 3349 | last_sector = bi->bi_sector + (bi->bi_size>>9); |
| 3350 | bi->bi_next = NULL; |
| 3351 | bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 3352 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3353 | for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) { |
| 3354 | DEFINE_WAIT(w); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3355 | int disks, data_disks; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 3356 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3357 | retry: |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 3358 | prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE); |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3359 | if (likely(conf->expand_progress == MaxSector)) |
| 3360 | disks = conf->raid_disks; |
| 3361 | else { |
NeilBrown | df8e7f7 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 3362 | /* spinlock is needed as expand_progress may be |
| 3363 | * 64bit on a 32bit platform, and so it might be |
| 3364 | * possible to see a half-updated value |
| 3365 | * Ofcourse expand_progress could change after |
| 3366 | * the lock is dropped, so once we get a reference |
| 3367 | * to the stripe that we think it is, we will have |
| 3368 | * to check again. |
| 3369 | */ |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3370 | spin_lock_irq(&conf->device_lock); |
| 3371 | disks = conf->raid_disks; |
| 3372 | if (logical_sector >= conf->expand_progress) |
| 3373 | disks = conf->previous_raid_disks; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 3374 | else { |
| 3375 | if (logical_sector >= conf->expand_lo) { |
| 3376 | spin_unlock_irq(&conf->device_lock); |
| 3377 | schedule(); |
| 3378 | goto retry; |
| 3379 | } |
| 3380 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3381 | spin_unlock_irq(&conf->device_lock); |
| 3382 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3383 | data_disks = disks - conf->max_degraded; |
| 3384 | |
| 3385 | new_sector = raid5_compute_sector(logical_sector, disks, data_disks, |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3386 | &dd_idx, &pd_idx, conf); |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3387 | pr_debug("raid5: make_request, sector %llu logical %llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3388 | (unsigned long long)new_sector, |
| 3389 | (unsigned long long)logical_sector); |
| 3390 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3391 | 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] | 3392 | if (sh) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3393 | if (unlikely(conf->expand_progress != MaxSector)) { |
| 3394 | /* expansion might have moved on while waiting for a |
NeilBrown | df8e7f7 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 3395 | * stripe, so we must do the range check again. |
| 3396 | * Expansion could still move past after this |
| 3397 | * test, but as we are holding a reference to |
| 3398 | * 'sh', we know that if that happens, |
| 3399 | * STRIPE_EXPANDING will get set and the expansion |
| 3400 | * won't proceed until we finish with the stripe. |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3401 | */ |
| 3402 | int must_retry = 0; |
| 3403 | spin_lock_irq(&conf->device_lock); |
| 3404 | if (logical_sector < conf->expand_progress && |
| 3405 | disks == conf->previous_raid_disks) |
| 3406 | /* mismatch, need to try again */ |
| 3407 | must_retry = 1; |
| 3408 | spin_unlock_irq(&conf->device_lock); |
| 3409 | if (must_retry) { |
| 3410 | release_stripe(sh); |
| 3411 | goto retry; |
| 3412 | } |
| 3413 | } |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3414 | /* FIXME what if we get a false positive because these |
| 3415 | * are being updated. |
| 3416 | */ |
| 3417 | if (logical_sector >= mddev->suspend_lo && |
| 3418 | logical_sector < mddev->suspend_hi) { |
| 3419 | release_stripe(sh); |
| 3420 | schedule(); |
| 3421 | goto retry; |
| 3422 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3423 | |
| 3424 | if (test_bit(STRIPE_EXPANDING, &sh->state) || |
| 3425 | !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) { |
| 3426 | /* Stripe is busy expanding or |
| 3427 | * add failed due to overlap. Flush everything |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3428 | * and wait a while |
| 3429 | */ |
| 3430 | raid5_unplug_device(mddev->queue); |
| 3431 | release_stripe(sh); |
| 3432 | schedule(); |
| 3433 | goto retry; |
| 3434 | } |
| 3435 | finish_wait(&conf->wait_for_overlap, &w); |
NeilBrown | 6ed3003 | 2008-02-06 01:40:00 -0800 | [diff] [blame] | 3436 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3437 | clear_bit(STRIPE_DELAYED, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3438 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3439 | } else { |
| 3440 | /* cannot get stripe for read-ahead, just give-up */ |
| 3441 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
| 3442 | finish_wait(&conf->wait_for_overlap, &w); |
| 3443 | break; |
| 3444 | } |
| 3445 | |
| 3446 | } |
| 3447 | spin_lock_irq(&conf->device_lock); |
NeilBrown | f634475 | 2006-03-27 01:18:17 -0800 | [diff] [blame] | 3448 | remaining = --bi->bi_phys_segments; |
| 3449 | spin_unlock_irq(&conf->device_lock); |
| 3450 | if (remaining == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3451 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3452 | if ( rw == WRITE ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3453 | md_write_end(mddev); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3454 | |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 3455 | bio_endio(bi, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3456 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3457 | return 0; |
| 3458 | } |
| 3459 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3460 | 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] | 3461 | { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3462 | /* reshaping is quite different to recovery/resync so it is |
| 3463 | * handled quite separately ... here. |
| 3464 | * |
| 3465 | * On each call to sync_request, we gather one chunk worth of |
| 3466 | * destination stripes and flag them as expanding. |
| 3467 | * Then we find all the source stripes and request reads. |
| 3468 | * As the reads complete, handle_stripe will copy the data |
| 3469 | * into the destination stripe and release that stripe. |
| 3470 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3471 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; |
| 3472 | struct stripe_head *sh; |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3473 | int pd_idx; |
| 3474 | sector_t first_sector, last_sector; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3475 | int raid_disks = conf->previous_raid_disks; |
| 3476 | int data_disks = raid_disks - conf->max_degraded; |
| 3477 | int new_data_disks = conf->raid_disks - conf->max_degraded; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3478 | int i; |
| 3479 | int dd_idx; |
| 3480 | sector_t writepos, safepos, gap; |
| 3481 | |
| 3482 | if (sector_nr == 0 && |
| 3483 | conf->expand_progress != 0) { |
| 3484 | /* restarting in the middle, skip the initial sectors */ |
| 3485 | sector_nr = conf->expand_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3486 | sector_div(sector_nr, new_data_disks); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3487 | *skipped = 1; |
| 3488 | return sector_nr; |
| 3489 | } |
| 3490 | |
| 3491 | /* we update the metadata when there is more than 3Meg |
| 3492 | * in the block range (that is rather arbitrary, should |
| 3493 | * probably be time based) or when the data about to be |
| 3494 | * copied would over-write the source of the data at |
| 3495 | * the front of the range. |
| 3496 | * i.e. one new_stripe forward from expand_progress new_maps |
| 3497 | * to after where expand_lo old_maps to |
| 3498 | */ |
| 3499 | writepos = conf->expand_progress + |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3500 | conf->chunk_size/512*(new_data_disks); |
| 3501 | sector_div(writepos, new_data_disks); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3502 | safepos = conf->expand_lo; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3503 | sector_div(safepos, data_disks); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3504 | gap = conf->expand_progress - conf->expand_lo; |
| 3505 | |
| 3506 | if (writepos >= safepos || |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3507 | gap > (new_data_disks)*3000*2 /*3Meg*/) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3508 | /* Cannot proceed until we've updated the superblock... */ |
| 3509 | wait_event(conf->wait_for_overlap, |
| 3510 | atomic_read(&conf->reshape_stripes)==0); |
| 3511 | mddev->reshape_position = conf->expand_progress; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3512 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3513 | md_wakeup_thread(mddev->thread); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3514 | wait_event(mddev->sb_wait, mddev->flags == 0 || |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3515 | kthread_should_stop()); |
| 3516 | spin_lock_irq(&conf->device_lock); |
| 3517 | conf->expand_lo = mddev->reshape_position; |
| 3518 | spin_unlock_irq(&conf->device_lock); |
| 3519 | wake_up(&conf->wait_for_overlap); |
| 3520 | } |
| 3521 | |
| 3522 | for (i=0; i < conf->chunk_size/512; i+= STRIPE_SECTORS) { |
| 3523 | int j; |
| 3524 | int skipped = 0; |
| 3525 | pd_idx = stripe_to_pdidx(sector_nr+i, conf, conf->raid_disks); |
| 3526 | sh = get_active_stripe(conf, sector_nr+i, |
| 3527 | conf->raid_disks, pd_idx, 0); |
| 3528 | set_bit(STRIPE_EXPANDING, &sh->state); |
| 3529 | atomic_inc(&conf->reshape_stripes); |
| 3530 | /* If any of this stripe is beyond the end of the old |
| 3531 | * array, then we need to zero those blocks |
| 3532 | */ |
| 3533 | for (j=sh->disks; j--;) { |
| 3534 | sector_t s; |
| 3535 | if (j == sh->pd_idx) |
| 3536 | continue; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3537 | if (conf->level == 6 && |
| 3538 | j == raid6_next_disk(sh->pd_idx, sh->disks)) |
| 3539 | continue; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3540 | s = compute_blocknr(sh, j); |
| 3541 | if (s < (mddev->array_size<<1)) { |
| 3542 | skipped = 1; |
| 3543 | continue; |
| 3544 | } |
| 3545 | memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE); |
| 3546 | set_bit(R5_Expanded, &sh->dev[j].flags); |
| 3547 | set_bit(R5_UPTODATE, &sh->dev[j].flags); |
| 3548 | } |
| 3549 | if (!skipped) { |
| 3550 | set_bit(STRIPE_EXPAND_READY, &sh->state); |
| 3551 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3552 | } |
| 3553 | release_stripe(sh); |
| 3554 | } |
| 3555 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 6d3baf2 | 2007-03-05 00:30:44 -0800 | [diff] [blame] | 3556 | conf->expand_progress = (sector_nr + i) * new_data_disks; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3557 | spin_unlock_irq(&conf->device_lock); |
| 3558 | /* Ok, those stripe are ready. We can start scheduling |
| 3559 | * reads on the source stripes. |
| 3560 | * The source stripes are determined by mapping the first and last |
| 3561 | * block on the destination stripes. |
| 3562 | */ |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3563 | first_sector = |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3564 | raid5_compute_sector(sector_nr*(new_data_disks), |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3565 | raid_disks, data_disks, |
| 3566 | &dd_idx, &pd_idx, conf); |
| 3567 | last_sector = |
| 3568 | raid5_compute_sector((sector_nr+conf->chunk_size/512) |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3569 | *(new_data_disks) -1, |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3570 | raid_disks, data_disks, |
| 3571 | &dd_idx, &pd_idx, conf); |
| 3572 | if (last_sector >= (mddev->size<<1)) |
| 3573 | last_sector = (mddev->size<<1)-1; |
| 3574 | while (first_sector <= last_sector) { |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3575 | pd_idx = stripe_to_pdidx(first_sector, conf, |
| 3576 | conf->previous_raid_disks); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3577 | sh = get_active_stripe(conf, first_sector, |
| 3578 | conf->previous_raid_disks, pd_idx, 0); |
| 3579 | set_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 3580 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3581 | release_stripe(sh); |
| 3582 | first_sector += STRIPE_SECTORS; |
| 3583 | } |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3584 | /* If this takes us to the resync_max point where we have to pause, |
| 3585 | * then we need to write out the superblock. |
| 3586 | */ |
| 3587 | sector_nr += conf->chunk_size>>9; |
| 3588 | if (sector_nr >= mddev->resync_max) { |
| 3589 | /* Cannot proceed until we've updated the superblock... */ |
| 3590 | wait_event(conf->wait_for_overlap, |
| 3591 | atomic_read(&conf->reshape_stripes) == 0); |
| 3592 | mddev->reshape_position = conf->expand_progress; |
| 3593 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 3594 | md_wakeup_thread(mddev->thread); |
| 3595 | wait_event(mddev->sb_wait, |
| 3596 | !test_bit(MD_CHANGE_DEVS, &mddev->flags) |
| 3597 | || kthread_should_stop()); |
| 3598 | spin_lock_irq(&conf->device_lock); |
| 3599 | conf->expand_lo = mddev->reshape_position; |
| 3600 | spin_unlock_irq(&conf->device_lock); |
| 3601 | wake_up(&conf->wait_for_overlap); |
| 3602 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3603 | return conf->chunk_size>>9; |
| 3604 | } |
| 3605 | |
| 3606 | /* FIXME go_faster isn't used */ |
| 3607 | static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster) |
| 3608 | { |
| 3609 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; |
| 3610 | struct stripe_head *sh; |
| 3611 | int pd_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3612 | int raid_disks = conf->raid_disks; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3613 | sector_t max_sector = mddev->size << 1; |
| 3614 | int sync_blocks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3615 | int still_degraded = 0; |
| 3616 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3617 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3618 | if (sector_nr >= max_sector) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3619 | /* just being told to finish up .. nothing much to do */ |
| 3620 | unplug_slaves(mddev); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 3621 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { |
| 3622 | end_reshape(conf); |
| 3623 | return 0; |
| 3624 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3625 | |
| 3626 | if (mddev->curr_resync < max_sector) /* aborted */ |
| 3627 | bitmap_end_sync(mddev->bitmap, mddev->curr_resync, |
| 3628 | &sync_blocks, 1); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3629 | else /* completed sync */ |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3630 | conf->fullsync = 0; |
| 3631 | bitmap_close_sync(mddev->bitmap); |
| 3632 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3633 | return 0; |
| 3634 | } |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3635 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3636 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 3637 | return reshape_request(mddev, sector_nr, skipped); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3638 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3639 | /* No need to check resync_max as we never do more than one |
| 3640 | * stripe, and as resync_max will always be on a chunk boundary, |
| 3641 | * if the check in md_do_sync didn't fire, there is no chance |
| 3642 | * of overstepping resync_max here |
| 3643 | */ |
| 3644 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3645 | /* if there is too many failed drives and we are trying |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3646 | * to resync, then assert that we are finished, because there is |
| 3647 | * nothing we can do. |
| 3648 | */ |
NeilBrown | 3285edf | 2006-06-26 00:27:55 -0700 | [diff] [blame] | 3649 | if (mddev->degraded >= conf->max_degraded && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3650 | test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 3651 | sector_t rv = (mddev->size << 1) - sector_nr; |
| 3652 | *skipped = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3653 | return rv; |
| 3654 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3655 | if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) && |
NeilBrown | 3855ad9 | 2005-11-08 21:39:38 -0800 | [diff] [blame] | 3656 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3657 | !conf->fullsync && sync_blocks >= STRIPE_SECTORS) { |
| 3658 | /* we can skip this block, and probably more */ |
| 3659 | sync_blocks /= STRIPE_SECTORS; |
| 3660 | *skipped = 1; |
| 3661 | return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */ |
| 3662 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3663 | |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 3664 | |
| 3665 | bitmap_cond_end_sync(mddev->bitmap, sector_nr); |
| 3666 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3667 | pd_idx = stripe_to_pdidx(sector_nr, conf, raid_disks); |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3668 | sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3669 | if (sh == NULL) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3670 | sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3671 | /* make sure we don't swamp the stripe cache if someone else |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3672 | * is trying to get access |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3673 | */ |
Nishanth Aravamudan | 66c006a | 2005-11-07 01:01:17 -0800 | [diff] [blame] | 3674 | schedule_timeout_uninterruptible(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3675 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3676 | /* Need to check if array will still be degraded after recovery/resync |
| 3677 | * We don't need to check the 'failed' flag as when that gets set, |
| 3678 | * recovery aborts. |
| 3679 | */ |
| 3680 | for (i=0; i<mddev->raid_disks; i++) |
| 3681 | if (conf->disks[i].rdev == NULL) |
| 3682 | still_degraded = 1; |
| 3683 | |
| 3684 | bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded); |
| 3685 | |
| 3686 | spin_lock(&sh->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3687 | set_bit(STRIPE_SYNCING, &sh->state); |
| 3688 | clear_bit(STRIPE_INSYNC, &sh->state); |
| 3689 | spin_unlock(&sh->lock); |
| 3690 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3691 | handle_stripe(sh, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3692 | release_stripe(sh); |
| 3693 | |
| 3694 | return STRIPE_SECTORS; |
| 3695 | } |
| 3696 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3697 | static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio) |
| 3698 | { |
| 3699 | /* We may not be able to submit a whole bio at once as there |
| 3700 | * may not be enough stripe_heads available. |
| 3701 | * We cannot pre-allocate enough stripe_heads as we may need |
| 3702 | * more than exist in the cache (if we allow ever large chunks). |
| 3703 | * So we do one stripe head at a time and record in |
| 3704 | * ->bi_hw_segments how many have been done. |
| 3705 | * |
| 3706 | * We *know* that this entire raid_bio is in one chunk, so |
| 3707 | * it will be only one 'dd_idx' and only need one call to raid5_compute_sector. |
| 3708 | */ |
| 3709 | struct stripe_head *sh; |
| 3710 | int dd_idx, pd_idx; |
| 3711 | sector_t sector, logical_sector, last_sector; |
| 3712 | int scnt = 0; |
| 3713 | int remaining; |
| 3714 | int handled = 0; |
| 3715 | |
| 3716 | logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
| 3717 | sector = raid5_compute_sector( logical_sector, |
| 3718 | conf->raid_disks, |
| 3719 | conf->raid_disks - conf->max_degraded, |
| 3720 | &dd_idx, |
| 3721 | &pd_idx, |
| 3722 | conf); |
| 3723 | last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9); |
| 3724 | |
| 3725 | for (; logical_sector < last_sector; |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3726 | logical_sector += STRIPE_SECTORS, |
| 3727 | sector += STRIPE_SECTORS, |
| 3728 | scnt++) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3729 | |
| 3730 | if (scnt < raid_bio->bi_hw_segments) |
| 3731 | /* already done this stripe */ |
| 3732 | continue; |
| 3733 | |
| 3734 | sh = get_active_stripe(conf, sector, conf->raid_disks, pd_idx, 1); |
| 3735 | |
| 3736 | if (!sh) { |
| 3737 | /* failed to get a stripe - must wait */ |
| 3738 | raid_bio->bi_hw_segments = scnt; |
| 3739 | conf->retry_read_aligned = raid_bio; |
| 3740 | return handled; |
| 3741 | } |
| 3742 | |
| 3743 | set_bit(R5_ReadError, &sh->dev[dd_idx].flags); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3744 | if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) { |
| 3745 | release_stripe(sh); |
| 3746 | raid_bio->bi_hw_segments = scnt; |
| 3747 | conf->retry_read_aligned = raid_bio; |
| 3748 | return handled; |
| 3749 | } |
| 3750 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3751 | handle_stripe(sh, NULL); |
| 3752 | release_stripe(sh); |
| 3753 | handled++; |
| 3754 | } |
| 3755 | spin_lock_irq(&conf->device_lock); |
| 3756 | remaining = --raid_bio->bi_phys_segments; |
| 3757 | spin_unlock_irq(&conf->device_lock); |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 3758 | if (remaining == 0) |
| 3759 | bio_endio(raid_bio, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3760 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
| 3761 | wake_up(&conf->wait_for_stripe); |
| 3762 | return handled; |
| 3763 | } |
| 3764 | |
| 3765 | |
| 3766 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3767 | /* |
| 3768 | * This is our raid5 kernel thread. |
| 3769 | * |
| 3770 | * We scan the hash table for stripes which can be handled now. |
| 3771 | * During the scan, completed stripes are saved for us by the interrupt |
| 3772 | * handler, so that they will not have to wait for our next wakeup. |
| 3773 | */ |
NeilBrown | 6ed3003 | 2008-02-06 01:40:00 -0800 | [diff] [blame] | 3774 | static void raid5d(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3775 | { |
| 3776 | struct stripe_head *sh; |
| 3777 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 3778 | int handled; |
| 3779 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3780 | pr_debug("+++ raid5d active\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3781 | |
| 3782 | md_check_recovery(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3783 | |
| 3784 | handled = 0; |
| 3785 | spin_lock_irq(&conf->device_lock); |
| 3786 | while (1) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3787 | struct bio *bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3788 | |
NeilBrown | ae3c20c | 2006-07-10 04:44:17 -0700 | [diff] [blame] | 3789 | if (conf->seq_flush != conf->seq_write) { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3790 | int seq = conf->seq_flush; |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 3791 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3792 | bitmap_unplug(mddev->bitmap); |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 3793 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3794 | conf->seq_write = seq; |
| 3795 | activate_bit_delay(conf); |
| 3796 | } |
| 3797 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3798 | while ((bio = remove_bio_from_retry(conf))) { |
| 3799 | int ok; |
| 3800 | spin_unlock_irq(&conf->device_lock); |
| 3801 | ok = retry_aligned_read(conf, bio); |
| 3802 | spin_lock_irq(&conf->device_lock); |
| 3803 | if (!ok) |
| 3804 | break; |
| 3805 | handled++; |
| 3806 | } |
| 3807 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3808 | sh = __get_priority_stripe(conf); |
| 3809 | |
| 3810 | if (!sh) { |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3811 | async_tx_issue_pending_all(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3812 | break; |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3813 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3814 | spin_unlock_irq(&conf->device_lock); |
| 3815 | |
| 3816 | handled++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3817 | handle_stripe(sh, conf->spare_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3818 | release_stripe(sh); |
| 3819 | |
| 3820 | spin_lock_irq(&conf->device_lock); |
| 3821 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3822 | pr_debug("%d stripes handled\n", handled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3823 | |
| 3824 | spin_unlock_irq(&conf->device_lock); |
| 3825 | |
| 3826 | unplug_slaves(mddev); |
| 3827 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3828 | pr_debug("--- raid5d inactive\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3829 | } |
| 3830 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 3831 | static ssize_t |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 3832 | raid5_show_stripe_cache_size(mddev_t *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 3833 | { |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 3834 | raid5_conf_t *conf = mddev_to_conf(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3835 | if (conf) |
| 3836 | return sprintf(page, "%d\n", conf->max_nr_stripes); |
| 3837 | else |
| 3838 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 3839 | } |
| 3840 | |
| 3841 | static ssize_t |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 3842 | 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] | 3843 | { |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 3844 | raid5_conf_t *conf = mddev_to_conf(mddev); |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 3845 | unsigned long new; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 3846 | if (len >= PAGE_SIZE) |
| 3847 | return -EINVAL; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3848 | if (!conf) |
| 3849 | return -ENODEV; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 3850 | |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 3851 | if (strict_strtoul(page, 10, &new)) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 3852 | return -EINVAL; |
| 3853 | if (new <= 16 || new > 32768) |
| 3854 | return -EINVAL; |
| 3855 | while (new < conf->max_nr_stripes) { |
| 3856 | if (drop_one_stripe(conf)) |
| 3857 | conf->max_nr_stripes--; |
| 3858 | else |
| 3859 | break; |
| 3860 | } |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 3861 | md_allow_write(mddev); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 3862 | while (new > conf->max_nr_stripes) { |
| 3863 | if (grow_one_stripe(conf)) |
| 3864 | conf->max_nr_stripes++; |
| 3865 | else break; |
| 3866 | } |
| 3867 | return len; |
| 3868 | } |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 3869 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3870 | static struct md_sysfs_entry |
| 3871 | raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR, |
| 3872 | raid5_show_stripe_cache_size, |
| 3873 | raid5_store_stripe_cache_size); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 3874 | |
| 3875 | static ssize_t |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3876 | raid5_show_preread_threshold(mddev_t *mddev, char *page) |
| 3877 | { |
| 3878 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 3879 | if (conf) |
| 3880 | return sprintf(page, "%d\n", conf->bypass_threshold); |
| 3881 | else |
| 3882 | return 0; |
| 3883 | } |
| 3884 | |
| 3885 | static ssize_t |
| 3886 | raid5_store_preread_threshold(mddev_t *mddev, const char *page, size_t len) |
| 3887 | { |
| 3888 | raid5_conf_t *conf = mddev_to_conf(mddev); |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 3889 | unsigned long new; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3890 | if (len >= PAGE_SIZE) |
| 3891 | return -EINVAL; |
| 3892 | if (!conf) |
| 3893 | return -ENODEV; |
| 3894 | |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 3895 | if (strict_strtoul(page, 10, &new)) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3896 | return -EINVAL; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 3897 | if (new > conf->max_nr_stripes) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3898 | return -EINVAL; |
| 3899 | conf->bypass_threshold = new; |
| 3900 | return len; |
| 3901 | } |
| 3902 | |
| 3903 | static struct md_sysfs_entry |
| 3904 | raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold, |
| 3905 | S_IRUGO | S_IWUSR, |
| 3906 | raid5_show_preread_threshold, |
| 3907 | raid5_store_preread_threshold); |
| 3908 | |
| 3909 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3910 | stripe_cache_active_show(mddev_t *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 3911 | { |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 3912 | raid5_conf_t *conf = mddev_to_conf(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3913 | if (conf) |
| 3914 | return sprintf(page, "%d\n", atomic_read(&conf->active_stripes)); |
| 3915 | else |
| 3916 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 3917 | } |
| 3918 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3919 | static struct md_sysfs_entry |
| 3920 | raid5_stripecache_active = __ATTR_RO(stripe_cache_active); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 3921 | |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 3922 | static struct attribute *raid5_attrs[] = { |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 3923 | &raid5_stripecache_size.attr, |
| 3924 | &raid5_stripecache_active.attr, |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3925 | &raid5_preread_bypass_threshold.attr, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 3926 | NULL, |
| 3927 | }; |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 3928 | static struct attribute_group raid5_attrs_group = { |
| 3929 | .name = NULL, |
| 3930 | .attrs = raid5_attrs, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 3931 | }; |
| 3932 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3933 | static int run(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3934 | { |
| 3935 | raid5_conf_t *conf; |
| 3936 | int raid_disk, memory; |
| 3937 | mdk_rdev_t *rdev; |
| 3938 | struct disk_info *disk; |
| 3939 | struct list_head *tmp; |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 3940 | int working_disks = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3941 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3942 | if (mddev->level != 5 && mddev->level != 4 && mddev->level != 6) { |
| 3943 | 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] | 3944 | mdname(mddev), mddev->level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3945 | return -EIO; |
| 3946 | } |
| 3947 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3948 | if (mddev->reshape_position != MaxSector) { |
| 3949 | /* Check that we can continue the reshape. |
| 3950 | * Currently only disks can change, it must |
| 3951 | * increase, and we must be past the point where |
| 3952 | * a stripe over-writes itself |
| 3953 | */ |
| 3954 | sector_t here_new, here_old; |
| 3955 | int old_disks; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3956 | int max_degraded = (mddev->level == 5 ? 1 : 2); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3957 | |
| 3958 | if (mddev->new_level != mddev->level || |
| 3959 | mddev->new_layout != mddev->layout || |
| 3960 | mddev->new_chunk != mddev->chunk_size) { |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3961 | printk(KERN_ERR "raid5: %s: unsupported reshape " |
| 3962 | "required - aborting.\n", |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3963 | mdname(mddev)); |
| 3964 | return -EINVAL; |
| 3965 | } |
| 3966 | if (mddev->delta_disks <= 0) { |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3967 | printk(KERN_ERR "raid5: %s: unsupported reshape " |
| 3968 | "(reduce disks) required - aborting.\n", |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3969 | mdname(mddev)); |
| 3970 | return -EINVAL; |
| 3971 | } |
| 3972 | old_disks = mddev->raid_disks - mddev->delta_disks; |
| 3973 | /* reshape_position must be on a new-stripe boundary, and one |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3974 | * further up in new geometry must map after here in old |
| 3975 | * geometry. |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3976 | */ |
| 3977 | here_new = mddev->reshape_position; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3978 | if (sector_div(here_new, (mddev->chunk_size>>9)* |
| 3979 | (mddev->raid_disks - max_degraded))) { |
| 3980 | printk(KERN_ERR "raid5: reshape_position not " |
| 3981 | "on a stripe boundary\n"); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3982 | return -EINVAL; |
| 3983 | } |
| 3984 | /* here_new is the stripe we will write to */ |
| 3985 | here_old = mddev->reshape_position; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3986 | sector_div(here_old, (mddev->chunk_size>>9)* |
| 3987 | (old_disks-max_degraded)); |
| 3988 | /* here_old is the first stripe that we might need to read |
| 3989 | * from */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3990 | if (here_new >= here_old) { |
| 3991 | /* Reading from the same stripe as writing to - bad */ |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3992 | printk(KERN_ERR "raid5: reshape_position too early for " |
| 3993 | "auto-recovery - aborting.\n"); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3994 | return -EINVAL; |
| 3995 | } |
| 3996 | printk(KERN_INFO "raid5: reshape will continue\n"); |
| 3997 | /* OK, we should be able to continue; */ |
| 3998 | } |
| 3999 | |
| 4000 | |
NeilBrown | b55e6bf | 2006-03-27 01:18:06 -0800 | [diff] [blame] | 4001 | mddev->private = kzalloc(sizeof (raid5_conf_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4002 | if ((conf = mddev->private) == NULL) |
| 4003 | goto abort; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4004 | if (mddev->reshape_position == MaxSector) { |
| 4005 | conf->previous_raid_disks = conf->raid_disks = mddev->raid_disks; |
| 4006 | } else { |
| 4007 | conf->raid_disks = mddev->raid_disks; |
| 4008 | conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks; |
| 4009 | } |
| 4010 | |
| 4011 | conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info), |
NeilBrown | b55e6bf | 2006-03-27 01:18:06 -0800 | [diff] [blame] | 4012 | GFP_KERNEL); |
| 4013 | if (!conf->disks) |
| 4014 | goto abort; |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 4015 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4016 | conf->mddev = mddev; |
| 4017 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 4018 | if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4019 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4020 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4021 | if (mddev->level == 6) { |
| 4022 | conf->spare_page = alloc_page(GFP_KERNEL); |
| 4023 | if (!conf->spare_page) |
| 4024 | goto abort; |
| 4025 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4026 | spin_lock_init(&conf->device_lock); |
Neil Brown | e7e72bf | 2008-05-14 16:05:54 -0700 | [diff] [blame] | 4027 | mddev->queue->queue_lock = &conf->device_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4028 | init_waitqueue_head(&conf->wait_for_stripe); |
| 4029 | init_waitqueue_head(&conf->wait_for_overlap); |
| 4030 | INIT_LIST_HEAD(&conf->handle_list); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4031 | INIT_LIST_HEAD(&conf->hold_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4032 | INIT_LIST_HEAD(&conf->delayed_list); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4033 | INIT_LIST_HEAD(&conf->bitmap_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4034 | INIT_LIST_HEAD(&conf->inactive_list); |
| 4035 | atomic_set(&conf->active_stripes, 0); |
| 4036 | atomic_set(&conf->preread_active_stripes, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4037 | atomic_set(&conf->active_aligned_reads, 0); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4038 | conf->bypass_threshold = BYPASS_THRESHOLD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4039 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4040 | pr_debug("raid5: run(%s) called.\n", mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4041 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 4042 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4043 | raid_disk = rdev->raid_disk; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4044 | if (raid_disk >= conf->raid_disks |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4045 | || raid_disk < 0) |
| 4046 | continue; |
| 4047 | disk = conf->disks + raid_disk; |
| 4048 | |
| 4049 | disk->rdev = rdev; |
| 4050 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4051 | if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4052 | char b[BDEVNAME_SIZE]; |
| 4053 | printk(KERN_INFO "raid5: device %s operational as raid" |
| 4054 | " disk %d\n", bdevname(rdev->bdev,b), |
| 4055 | raid_disk); |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 4056 | working_disks++; |
Neil Brown | 8c2e870 | 2008-06-28 08:30:52 +1000 | [diff] [blame] | 4057 | } else |
| 4058 | /* Cannot rely on bitmap to complete recovery */ |
| 4059 | conf->fullsync = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4060 | } |
| 4061 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4062 | /* |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4063 | * 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] | 4064 | */ |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 4065 | mddev->degraded = conf->raid_disks - working_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4066 | conf->mddev = mddev; |
| 4067 | conf->chunk_size = mddev->chunk_size; |
| 4068 | conf->level = mddev->level; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4069 | if (conf->level == 6) |
| 4070 | conf->max_degraded = 2; |
| 4071 | else |
| 4072 | conf->max_degraded = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4073 | conf->algorithm = mddev->layout; |
| 4074 | conf->max_nr_stripes = NR_STRIPES; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4075 | conf->expand_progress = mddev->reshape_position; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4076 | |
| 4077 | /* device size must be a multiple of chunk size */ |
| 4078 | mddev->size &= ~(mddev->chunk_size/1024 -1); |
NeilBrown | b158156 | 2005-07-31 22:34:50 -0700 | [diff] [blame] | 4079 | mddev->resync_max_sectors = mddev->size << 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4080 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4081 | if (conf->level == 6 && conf->raid_disks < 4) { |
| 4082 | printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n", |
| 4083 | mdname(mddev), conf->raid_disks); |
| 4084 | goto abort; |
| 4085 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4086 | if (!conf->chunk_size || conf->chunk_size % 4) { |
| 4087 | printk(KERN_ERR "raid5: invalid chunk size %d for %s\n", |
| 4088 | conf->chunk_size, mdname(mddev)); |
| 4089 | goto abort; |
| 4090 | } |
| 4091 | if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) { |
| 4092 | printk(KERN_ERR |
| 4093 | "raid5: unsupported parity algorithm %d for %s\n", |
| 4094 | conf->algorithm, mdname(mddev)); |
| 4095 | goto abort; |
| 4096 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4097 | if (mddev->degraded > conf->max_degraded) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4098 | printk(KERN_ERR "raid5: not enough operational devices for %s" |
| 4099 | " (%d/%d failed)\n", |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 4100 | mdname(mddev), mddev->degraded, conf->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4101 | goto abort; |
| 4102 | } |
| 4103 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4104 | if (mddev->degraded > 0 && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4105 | mddev->recovery_cp != MaxSector) { |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4106 | if (mddev->ok_start_degraded) |
| 4107 | printk(KERN_WARNING |
| 4108 | "raid5: starting dirty degraded array: %s" |
| 4109 | "- data corruption possible.\n", |
| 4110 | mdname(mddev)); |
| 4111 | else { |
| 4112 | printk(KERN_ERR |
| 4113 | "raid5: cannot start dirty degraded array for %s\n", |
| 4114 | mdname(mddev)); |
| 4115 | goto abort; |
| 4116 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4117 | } |
| 4118 | |
| 4119 | { |
| 4120 | mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5"); |
| 4121 | if (!mddev->thread) { |
| 4122 | printk(KERN_ERR |
| 4123 | "raid5: couldn't allocate thread for %s\n", |
| 4124 | mdname(mddev)); |
| 4125 | goto abort; |
| 4126 | } |
| 4127 | } |
NeilBrown | 5036805 | 2005-12-12 02:39:17 -0800 | [diff] [blame] | 4128 | memory = conf->max_nr_stripes * (sizeof(struct stripe_head) + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4129 | conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024; |
| 4130 | if (grow_stripes(conf, conf->max_nr_stripes)) { |
| 4131 | printk(KERN_ERR |
| 4132 | "raid5: couldn't allocate %dkB for buffers\n", memory); |
| 4133 | shrink_stripes(conf); |
| 4134 | md_unregister_thread(mddev->thread); |
| 4135 | goto abort; |
| 4136 | } else |
| 4137 | printk(KERN_INFO "raid5: allocated %dkB for %s\n", |
| 4138 | memory, mdname(mddev)); |
| 4139 | |
| 4140 | if (mddev->degraded == 0) |
| 4141 | printk("raid5: raid level %d set %s active with %d out of %d" |
| 4142 | " devices, algorithm %d\n", conf->level, mdname(mddev), |
| 4143 | mddev->raid_disks-mddev->degraded, mddev->raid_disks, |
| 4144 | conf->algorithm); |
| 4145 | else |
| 4146 | printk(KERN_ALERT "raid5: raid level %d set %s active with %d" |
| 4147 | " out of %d devices, algorithm %d\n", conf->level, |
| 4148 | mdname(mddev), mddev->raid_disks - mddev->degraded, |
| 4149 | mddev->raid_disks, conf->algorithm); |
| 4150 | |
| 4151 | print_raid5_conf(conf); |
| 4152 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4153 | if (conf->expand_progress != MaxSector) { |
| 4154 | printk("...ok start reshape thread\n"); |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 4155 | conf->expand_lo = conf->expand_progress; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4156 | atomic_set(&conf->reshape_stripes, 0); |
| 4157 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 4158 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 4159 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 4160 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 4161 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
| 4162 | "%s_reshape"); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4163 | } |
| 4164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4165 | /* read-ahead size must cover two whole stripes, which is |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4166 | * 2 * (datadisks) * chunksize where 'n' is the number of raid devices |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4167 | */ |
| 4168 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4169 | int data_disks = conf->previous_raid_disks - conf->max_degraded; |
| 4170 | int stripe = data_disks * |
NeilBrown | 8932c2e | 2006-06-26 00:27:36 -0700 | [diff] [blame] | 4171 | (mddev->chunk_size / PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4172 | if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 4173 | mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
| 4174 | } |
| 4175 | |
| 4176 | /* Ok, everything is just fine now */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4177 | if (sysfs_create_group(&mddev->kobj, &raid5_attrs_group)) |
| 4178 | printk(KERN_WARNING |
| 4179 | "raid5: failed to create sysfs attributes for %s\n", |
| 4180 | mdname(mddev)); |
NeilBrown | 7a5febe | 2005-05-16 21:53:16 -0700 | [diff] [blame] | 4181 | |
| 4182 | mddev->queue->unplug_fn = raid5_unplug_device; |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 4183 | mddev->queue->backing_dev_info.congested_data = mddev; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4184 | mddev->queue->backing_dev_info.congested_fn = raid5_congested; |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 4185 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4186 | mddev->array_size = mddev->size * (conf->previous_raid_disks - |
| 4187 | conf->max_degraded); |
NeilBrown | 7a5febe | 2005-05-16 21:53:16 -0700 | [diff] [blame] | 4188 | |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 4189 | blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec); |
| 4190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4191 | return 0; |
| 4192 | abort: |
| 4193 | if (conf) { |
| 4194 | print_raid5_conf(conf); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4195 | safe_put_page(conf->spare_page); |
NeilBrown | b55e6bf | 2006-03-27 01:18:06 -0800 | [diff] [blame] | 4196 | kfree(conf->disks); |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 4197 | kfree(conf->stripe_hashtbl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4198 | kfree(conf); |
| 4199 | } |
| 4200 | mddev->private = NULL; |
| 4201 | printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev)); |
| 4202 | return -EIO; |
| 4203 | } |
| 4204 | |
| 4205 | |
| 4206 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4207 | static int stop(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4208 | { |
| 4209 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; |
| 4210 | |
| 4211 | md_unregister_thread(mddev->thread); |
| 4212 | mddev->thread = NULL; |
| 4213 | shrink_stripes(conf); |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 4214 | kfree(conf->stripe_hashtbl); |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4215 | mddev->queue->backing_dev_info.congested_fn = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4216 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4217 | sysfs_remove_group(&mddev->kobj, &raid5_attrs_group); |
NeilBrown | b55e6bf | 2006-03-27 01:18:06 -0800 | [diff] [blame] | 4218 | kfree(conf->disks); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4219 | kfree(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4220 | mddev->private = NULL; |
| 4221 | return 0; |
| 4222 | } |
| 4223 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4224 | #ifdef DEBUG |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4225 | static void print_sh (struct seq_file *seq, struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4226 | { |
| 4227 | int i; |
| 4228 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4229 | seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n", |
| 4230 | (unsigned long long)sh->sector, sh->pd_idx, sh->state); |
| 4231 | seq_printf(seq, "sh %llu, count %d.\n", |
| 4232 | (unsigned long long)sh->sector, atomic_read(&sh->count)); |
| 4233 | seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector); |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4234 | for (i = 0; i < sh->disks; i++) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4235 | seq_printf(seq, "(cache%d: %p %ld) ", |
| 4236 | i, sh->dev[i].page, sh->dev[i].flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4237 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4238 | seq_printf(seq, "\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4239 | } |
| 4240 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4241 | static void printall (struct seq_file *seq, raid5_conf_t *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4242 | { |
| 4243 | struct stripe_head *sh; |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 4244 | struct hlist_node *hn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4245 | int i; |
| 4246 | |
| 4247 | spin_lock_irq(&conf->device_lock); |
| 4248 | for (i = 0; i < NR_HASH; i++) { |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 4249 | hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4250 | if (sh->raid_conf != conf) |
| 4251 | continue; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4252 | print_sh(seq, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4253 | } |
| 4254 | } |
| 4255 | spin_unlock_irq(&conf->device_lock); |
| 4256 | } |
| 4257 | #endif |
| 4258 | |
| 4259 | static void status (struct seq_file *seq, mddev_t *mddev) |
| 4260 | { |
| 4261 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; |
| 4262 | int i; |
| 4263 | |
| 4264 | seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout); |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 4265 | seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4266 | for (i = 0; i < conf->raid_disks; i++) |
| 4267 | seq_printf (seq, "%s", |
| 4268 | conf->disks[i].rdev && |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4269 | test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4270 | seq_printf (seq, "]"); |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4271 | #ifdef DEBUG |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4272 | seq_printf (seq, "\n"); |
| 4273 | printall(seq, conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4274 | #endif |
| 4275 | } |
| 4276 | |
| 4277 | static void print_raid5_conf (raid5_conf_t *conf) |
| 4278 | { |
| 4279 | int i; |
| 4280 | struct disk_info *tmp; |
| 4281 | |
| 4282 | printk("RAID5 conf printout:\n"); |
| 4283 | if (!conf) { |
| 4284 | printk("(conf==NULL)\n"); |
| 4285 | return; |
| 4286 | } |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 4287 | printk(" --- rd:%d wd:%d\n", conf->raid_disks, |
| 4288 | conf->raid_disks - conf->mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4289 | |
| 4290 | for (i = 0; i < conf->raid_disks; i++) { |
| 4291 | char b[BDEVNAME_SIZE]; |
| 4292 | tmp = conf->disks + i; |
| 4293 | if (tmp->rdev) |
| 4294 | printk(" disk %d, o:%d, dev:%s\n", |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4295 | i, !test_bit(Faulty, &tmp->rdev->flags), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4296 | bdevname(tmp->rdev->bdev,b)); |
| 4297 | } |
| 4298 | } |
| 4299 | |
| 4300 | static int raid5_spare_active(mddev_t *mddev) |
| 4301 | { |
| 4302 | int i; |
| 4303 | raid5_conf_t *conf = mddev->private; |
| 4304 | struct disk_info *tmp; |
| 4305 | |
| 4306 | for (i = 0; i < conf->raid_disks; i++) { |
| 4307 | tmp = conf->disks + i; |
| 4308 | if (tmp->rdev |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4309 | && !test_bit(Faulty, &tmp->rdev->flags) |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 4310 | && !test_and_set_bit(In_sync, &tmp->rdev->flags)) { |
| 4311 | unsigned long flags; |
| 4312 | spin_lock_irqsave(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4313 | mddev->degraded--; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 4314 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4315 | } |
| 4316 | } |
| 4317 | print_raid5_conf(conf); |
| 4318 | return 0; |
| 4319 | } |
| 4320 | |
| 4321 | static int raid5_remove_disk(mddev_t *mddev, int number) |
| 4322 | { |
| 4323 | raid5_conf_t *conf = mddev->private; |
| 4324 | int err = 0; |
| 4325 | mdk_rdev_t *rdev; |
| 4326 | struct disk_info *p = conf->disks + number; |
| 4327 | |
| 4328 | print_raid5_conf(conf); |
| 4329 | rdev = p->rdev; |
| 4330 | if (rdev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4331 | if (test_bit(In_sync, &rdev->flags) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4332 | atomic_read(&rdev->nr_pending)) { |
| 4333 | err = -EBUSY; |
| 4334 | goto abort; |
| 4335 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 4336 | /* Only remove non-faulty devices if recovery |
| 4337 | * isn't possible. |
| 4338 | */ |
| 4339 | if (!test_bit(Faulty, &rdev->flags) && |
| 4340 | mddev->degraded <= conf->max_degraded) { |
| 4341 | err = -EBUSY; |
| 4342 | goto abort; |
| 4343 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4344 | p->rdev = NULL; |
Paul E. McKenney | fbd568a3e | 2005-05-01 08:59:04 -0700 | [diff] [blame] | 4345 | synchronize_rcu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4346 | if (atomic_read(&rdev->nr_pending)) { |
| 4347 | /* lost the race, try later */ |
| 4348 | err = -EBUSY; |
| 4349 | p->rdev = rdev; |
| 4350 | } |
| 4351 | } |
| 4352 | abort: |
| 4353 | |
| 4354 | print_raid5_conf(conf); |
| 4355 | return err; |
| 4356 | } |
| 4357 | |
| 4358 | static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev) |
| 4359 | { |
| 4360 | raid5_conf_t *conf = mddev->private; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 4361 | int err = -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4362 | int disk; |
| 4363 | struct disk_info *p; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 4364 | int first = 0; |
| 4365 | int last = conf->raid_disks - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4366 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4367 | if (mddev->degraded > conf->max_degraded) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4368 | /* no point adding a device */ |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 4369 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4370 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 4371 | if (rdev->raid_disk >= 0) |
| 4372 | first = last = rdev->raid_disk; |
| 4373 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4374 | /* |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4375 | * find the disk ... but prefer rdev->saved_raid_disk |
| 4376 | * if possible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4377 | */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4378 | if (rdev->saved_raid_disk >= 0 && |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 4379 | rdev->saved_raid_disk >= first && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4380 | conf->disks[rdev->saved_raid_disk].rdev == NULL) |
| 4381 | disk = rdev->saved_raid_disk; |
| 4382 | else |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 4383 | disk = first; |
| 4384 | for ( ; disk <= last ; disk++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4385 | if ((p=conf->disks + disk)->rdev == NULL) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4386 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4387 | rdev->raid_disk = disk; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 4388 | err = 0; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4389 | if (rdev->saved_raid_disk != disk) |
| 4390 | conf->fullsync = 1; |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 4391 | rcu_assign_pointer(p->rdev, rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4392 | break; |
| 4393 | } |
| 4394 | print_raid5_conf(conf); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 4395 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4396 | } |
| 4397 | |
| 4398 | static int raid5_resize(mddev_t *mddev, sector_t sectors) |
| 4399 | { |
| 4400 | /* no resync is happening, and there is enough space |
| 4401 | * on all devices, so we can resize. |
| 4402 | * We need to make sure resync covers any new space. |
| 4403 | * If the array is shrinking we should possibly wait until |
| 4404 | * any io in the removed space completes, but it hardly seems |
| 4405 | * worth it. |
| 4406 | */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4407 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 4408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4409 | sectors &= ~((sector_t)mddev->chunk_size/512 - 1); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4410 | mddev->array_size = (sectors * (mddev->raid_disks-conf->max_degraded))>>1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4411 | set_capacity(mddev->gendisk, mddev->array_size << 1); |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4412 | mddev->changed = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4413 | if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) { |
| 4414 | mddev->recovery_cp = mddev->size << 1; |
| 4415 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4416 | } |
| 4417 | mddev->size = sectors /2; |
NeilBrown | 4b5c7ae | 2005-07-27 11:43:28 -0700 | [diff] [blame] | 4418 | mddev->resync_max_sectors = sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4419 | return 0; |
| 4420 | } |
| 4421 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 4422 | #ifdef CONFIG_MD_RAID5_RESHAPE |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4423 | static int raid5_check_reshape(mddev_t *mddev) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 4424 | { |
| 4425 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 4426 | int err; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 4427 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4428 | if (mddev->delta_disks < 0 || |
| 4429 | mddev->new_level != mddev->level) |
| 4430 | return -EINVAL; /* Cannot shrink array or change level yet */ |
| 4431 | if (mddev->delta_disks == 0) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 4432 | return 0; /* nothing to do */ |
| 4433 | |
| 4434 | /* Can only proceed if there are plenty of stripe_heads. |
| 4435 | * We need a minimum of one full stripe,, and for sensible progress |
| 4436 | * it is best to have about 4 times that. |
| 4437 | * If we require 4 times, then the default 256 4K stripe_heads will |
| 4438 | * allow for chunk sizes up to 256K, which is probably OK. |
| 4439 | * If the chunk size is greater, user-space should request more |
| 4440 | * stripe_heads first. |
| 4441 | */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4442 | if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes || |
| 4443 | (mddev->new_chunk / STRIPE_SIZE) * 4 > conf->max_nr_stripes) { |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 4444 | printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n", |
| 4445 | (mddev->chunk_size / STRIPE_SIZE)*4); |
| 4446 | return -ENOSPC; |
| 4447 | } |
| 4448 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4449 | err = resize_stripes(conf, conf->raid_disks + mddev->delta_disks); |
| 4450 | if (err) |
| 4451 | return err; |
| 4452 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 4453 | if (mddev->degraded > conf->max_degraded) |
| 4454 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4455 | /* looks like we might be able to manage this */ |
| 4456 | return 0; |
| 4457 | } |
| 4458 | |
| 4459 | static int raid5_start_reshape(mddev_t *mddev) |
| 4460 | { |
| 4461 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 4462 | mdk_rdev_t *rdev; |
| 4463 | struct list_head *rtmp; |
| 4464 | int spares = 0; |
| 4465 | int added_devices = 0; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 4466 | unsigned long flags; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4467 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4468 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4469 | return -EBUSY; |
| 4470 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 4471 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 4472 | if (rdev->raid_disk < 0 && |
| 4473 | !test_bit(Faulty, &rdev->flags)) |
| 4474 | spares++; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4475 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4476 | if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 4477 | /* Not enough devices even to make a degraded array |
| 4478 | * of that size |
| 4479 | */ |
| 4480 | return -EINVAL; |
| 4481 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4482 | atomic_set(&conf->reshape_stripes, 0); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 4483 | spin_lock_irq(&conf->device_lock); |
| 4484 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4485 | conf->raid_disks += mddev->delta_disks; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 4486 | conf->expand_progress = 0; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 4487 | conf->expand_lo = 0; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 4488 | spin_unlock_irq(&conf->device_lock); |
| 4489 | |
| 4490 | /* Add some new drives, as many as will fit. |
| 4491 | * We know there are enough to make the newly sized array work. |
| 4492 | */ |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 4493 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 4494 | if (rdev->raid_disk < 0 && |
| 4495 | !test_bit(Faulty, &rdev->flags)) { |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 4496 | if (raid5_add_disk(mddev, rdev) == 0) { |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 4497 | char nm[20]; |
| 4498 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 4499 | added_devices++; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4500 | rdev->recovery_offset = 0; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 4501 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4502 | if (sysfs_create_link(&mddev->kobj, |
| 4503 | &rdev->kobj, nm)) |
| 4504 | printk(KERN_WARNING |
| 4505 | "raid5: failed to create " |
| 4506 | " link %s for %s\n", |
| 4507 | nm, mdname(mddev)); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 4508 | } else |
| 4509 | break; |
| 4510 | } |
| 4511 | |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 4512 | spin_lock_irqsave(&conf->device_lock, flags); |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4513 | mddev->degraded = (conf->raid_disks - conf->previous_raid_disks) - added_devices; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 4514 | spin_unlock_irqrestore(&conf->device_lock, flags); |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4515 | mddev->raid_disks = conf->raid_disks; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4516 | mddev->reshape_position = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4517 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4518 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 4519 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 4520 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 4521 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 4522 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 4523 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
| 4524 | "%s_reshape"); |
| 4525 | if (!mddev->sync_thread) { |
| 4526 | mddev->recovery = 0; |
| 4527 | spin_lock_irq(&conf->device_lock); |
| 4528 | mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks; |
| 4529 | conf->expand_progress = MaxSector; |
| 4530 | spin_unlock_irq(&conf->device_lock); |
| 4531 | return -EAGAIN; |
| 4532 | } |
| 4533 | md_wakeup_thread(mddev->sync_thread); |
| 4534 | md_new_event(mddev); |
| 4535 | return 0; |
| 4536 | } |
| 4537 | #endif |
| 4538 | |
| 4539 | static void end_reshape(raid5_conf_t *conf) |
| 4540 | { |
| 4541 | struct block_device *bdev; |
| 4542 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4543 | if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) { |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4544 | conf->mddev->array_size = conf->mddev->size * |
| 4545 | (conf->raid_disks - conf->max_degraded); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4546 | set_capacity(conf->mddev->gendisk, conf->mddev->array_size << 1); |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4547 | conf->mddev->changed = 1; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 4548 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4549 | bdev = bdget_disk(conf->mddev->gendisk, 0); |
| 4550 | if (bdev) { |
| 4551 | mutex_lock(&bdev->bd_inode->i_mutex); |
NeilBrown | 0692c6b | 2006-11-08 17:44:48 -0800 | [diff] [blame] | 4552 | i_size_write(bdev->bd_inode, (loff_t)conf->mddev->array_size << 10); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4553 | mutex_unlock(&bdev->bd_inode->i_mutex); |
| 4554 | bdput(bdev); |
| 4555 | } |
| 4556 | spin_lock_irq(&conf->device_lock); |
| 4557 | conf->expand_progress = MaxSector; |
| 4558 | spin_unlock_irq(&conf->device_lock); |
| 4559 | conf->mddev->reshape_position = MaxSector; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4560 | |
| 4561 | /* read-ahead size must cover two whole stripes, which is |
| 4562 | * 2 * (datadisks) * chunksize where 'n' is the number of raid devices |
| 4563 | */ |
| 4564 | { |
| 4565 | int data_disks = conf->previous_raid_disks - conf->max_degraded; |
| 4566 | int stripe = data_disks * |
| 4567 | (conf->mddev->chunk_size / PAGE_SIZE); |
| 4568 | if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 4569 | conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
| 4570 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 4571 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 4572 | } |
| 4573 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4574 | static void raid5_quiesce(mddev_t *mddev, int state) |
| 4575 | { |
| 4576 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 4577 | |
| 4578 | switch(state) { |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4579 | case 2: /* resume for a suspend */ |
| 4580 | wake_up(&conf->wait_for_overlap); |
| 4581 | break; |
| 4582 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4583 | case 1: /* stop all writes */ |
| 4584 | spin_lock_irq(&conf->device_lock); |
| 4585 | conf->quiesce = 1; |
| 4586 | wait_event_lock_irq(conf->wait_for_stripe, |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4587 | atomic_read(&conf->active_stripes) == 0 && |
| 4588 | atomic_read(&conf->active_aligned_reads) == 0, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4589 | conf->device_lock, /* nothing */); |
| 4590 | spin_unlock_irq(&conf->device_lock); |
| 4591 | break; |
| 4592 | |
| 4593 | case 0: /* re-enable writes */ |
| 4594 | spin_lock_irq(&conf->device_lock); |
| 4595 | conf->quiesce = 0; |
| 4596 | wake_up(&conf->wait_for_stripe); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4597 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4598 | spin_unlock_irq(&conf->device_lock); |
| 4599 | break; |
| 4600 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4601 | } |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4602 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4603 | static struct mdk_personality raid6_personality = |
| 4604 | { |
| 4605 | .name = "raid6", |
| 4606 | .level = 6, |
| 4607 | .owner = THIS_MODULE, |
| 4608 | .make_request = make_request, |
| 4609 | .run = run, |
| 4610 | .stop = stop, |
| 4611 | .status = status, |
| 4612 | .error_handler = error, |
| 4613 | .hot_add_disk = raid5_add_disk, |
| 4614 | .hot_remove_disk= raid5_remove_disk, |
| 4615 | .spare_active = raid5_spare_active, |
| 4616 | .sync_request = sync_request, |
| 4617 | .resize = raid5_resize, |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4618 | #ifdef CONFIG_MD_RAID5_RESHAPE |
| 4619 | .check_reshape = raid5_check_reshape, |
| 4620 | .start_reshape = raid5_start_reshape, |
| 4621 | #endif |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4622 | .quiesce = raid5_quiesce, |
| 4623 | }; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4624 | static struct mdk_personality raid5_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4625 | { |
| 4626 | .name = "raid5", |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4627 | .level = 5, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4628 | .owner = THIS_MODULE, |
| 4629 | .make_request = make_request, |
| 4630 | .run = run, |
| 4631 | .stop = stop, |
| 4632 | .status = status, |
| 4633 | .error_handler = error, |
| 4634 | .hot_add_disk = raid5_add_disk, |
| 4635 | .hot_remove_disk= raid5_remove_disk, |
| 4636 | .spare_active = raid5_spare_active, |
| 4637 | .sync_request = sync_request, |
| 4638 | .resize = raid5_resize, |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 4639 | #ifdef CONFIG_MD_RAID5_RESHAPE |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4640 | .check_reshape = raid5_check_reshape, |
| 4641 | .start_reshape = raid5_start_reshape, |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 4642 | #endif |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4643 | .quiesce = raid5_quiesce, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4644 | }; |
| 4645 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4646 | static struct mdk_personality raid4_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4647 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4648 | .name = "raid4", |
| 4649 | .level = 4, |
| 4650 | .owner = THIS_MODULE, |
| 4651 | .make_request = make_request, |
| 4652 | .run = run, |
| 4653 | .stop = stop, |
| 4654 | .status = status, |
| 4655 | .error_handler = error, |
| 4656 | .hot_add_disk = raid5_add_disk, |
| 4657 | .hot_remove_disk= raid5_remove_disk, |
| 4658 | .spare_active = raid5_spare_active, |
| 4659 | .sync_request = sync_request, |
| 4660 | .resize = raid5_resize, |
NeilBrown | 3d37890 | 2007-03-26 21:32:13 -0800 | [diff] [blame] | 4661 | #ifdef CONFIG_MD_RAID5_RESHAPE |
| 4662 | .check_reshape = raid5_check_reshape, |
| 4663 | .start_reshape = raid5_start_reshape, |
| 4664 | #endif |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4665 | .quiesce = raid5_quiesce, |
| 4666 | }; |
| 4667 | |
| 4668 | static int __init raid5_init(void) |
| 4669 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4670 | int e; |
| 4671 | |
| 4672 | e = raid6_select_algo(); |
| 4673 | if ( e ) |
| 4674 | return e; |
| 4675 | register_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4676 | register_md_personality(&raid5_personality); |
| 4677 | register_md_personality(&raid4_personality); |
| 4678 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4679 | } |
| 4680 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4681 | static void raid5_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4682 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4683 | unregister_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4684 | unregister_md_personality(&raid5_personality); |
| 4685 | unregister_md_personality(&raid4_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4686 | } |
| 4687 | |
| 4688 | module_init(raid5_init); |
| 4689 | module_exit(raid5_exit); |
| 4690 | MODULE_LICENSE("GPL"); |
| 4691 | MODULE_ALIAS("md-personality-4"); /* RAID5 */ |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4692 | MODULE_ALIAS("md-raid5"); |
| 4693 | MODULE_ALIAS("md-raid4"); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4694 | MODULE_ALIAS("md-level-5"); |
| 4695 | MODULE_ALIAS("md-level-4"); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4696 | MODULE_ALIAS("md-personality-8"); /* RAID6 */ |
| 4697 | MODULE_ALIAS("md-raid6"); |
| 4698 | MODULE_ALIAS("md-level-6"); |
| 4699 | |
| 4700 | /* This used to be two separate modules, they were: */ |
| 4701 | MODULE_ALIAS("raid5"); |
| 4702 | MODULE_ALIAS("raid6"); |