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