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. |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 30 | * conf->seq_write is the number of the last batch successfully written. |
| 31 | * conf->seq_flush is the number of the last batch that was closed to |
NeilBrown | ae3c20c | 2006-07-10 04:44:17 -0700 | [diff] [blame] | 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 |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 35 | * the number of the batch it will be in. This is seq_flush+1. |
NeilBrown | ae3c20c | 2006-07-10 04:44:17 -0700 | [diff] [blame] | 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 | f701d58 | 2009-03-31 15:09:39 +1100 | [diff] [blame] | 48 | #include <linux/raid/pq.h> |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 49 | #include <linux/async_tx.h> |
Paul Gortmaker | 056075c | 2011-07-03 13:58:33 -0400 | [diff] [blame] | 50 | #include <linux/module.h> |
Dan Williams | 07a3b41 | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 51 | #include <linux/async.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 52 | #include <linux/seq_file.h> |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 53 | #include <linux/cpu.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 54 | #include <linux/slab.h> |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 55 | #include <linux/ratelimit.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 56 | #include "md.h" |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 57 | #include "raid5.h" |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 58 | #include "raid0.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 59 | #include "bitmap.h" |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 60 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | /* |
| 62 | * Stripe cache |
| 63 | */ |
| 64 | |
| 65 | #define NR_STRIPES 256 |
| 66 | #define STRIPE_SIZE PAGE_SIZE |
| 67 | #define STRIPE_SHIFT (PAGE_SHIFT - 9) |
| 68 | #define STRIPE_SECTORS (STRIPE_SIZE>>9) |
| 69 | #define IO_THRESHOLD 1 |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 70 | #define BYPASS_THRESHOLD 1 |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 71 | #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | #define HASH_MASK (NR_HASH - 1) |
| 73 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 74 | static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect) |
NeilBrown | db298e1 | 2011-10-07 14:23:00 +1100 | [diff] [blame] | 75 | { |
| 76 | int hash = (sect >> STRIPE_SHIFT) & HASH_MASK; |
| 77 | return &conf->stripe_hashtbl[hash]; |
| 78 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
| 80 | /* bio's attached to a stripe+device for I/O are linked together in bi_sector |
| 81 | * order without overlap. There may be several bio's per stripe+device, and |
| 82 | * a bio could span several devices. |
| 83 | * When walking this list for a particular stripe+device, we must never proceed |
| 84 | * beyond a bio that extends past this device, as the next bio might no longer |
| 85 | * be valid. |
NeilBrown | db298e1 | 2011-10-07 14:23:00 +1100 | [diff] [blame] | 86 | * This function is used to determine the 'next' bio in the list, given the sector |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | * of the current stripe+device |
| 88 | */ |
NeilBrown | db298e1 | 2011-10-07 14:23:00 +1100 | [diff] [blame] | 89 | static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector) |
| 90 | { |
| 91 | int sectors = bio->bi_size >> 9; |
| 92 | if (bio->bi_sector + sectors < sector + STRIPE_SECTORS) |
| 93 | return bio->bi_next; |
| 94 | else |
| 95 | return NULL; |
| 96 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 98 | /* |
Jens Axboe | 5b99c2f | 2008-08-15 10:56:11 +0200 | [diff] [blame] | 99 | * We maintain a biased count of active stripes in the bottom 16 bits of |
| 100 | * 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] | 101 | */ |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame^] | 102 | static inline int raid5_bi_processed_stripes(struct bio *bio) |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 103 | { |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame^] | 104 | atomic_t *segments = (atomic_t *)&bio->bi_phys_segments; |
| 105 | return (atomic_read(segments) >> 16) & 0xffff; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 106 | } |
| 107 | |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame^] | 108 | static inline int raid5_dec_bi_active_stripes(struct bio *bio) |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 109 | { |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame^] | 110 | atomic_t *segments = (atomic_t *)&bio->bi_phys_segments; |
| 111 | return atomic_sub_return(1, segments) & 0xffff; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 112 | } |
| 113 | |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame^] | 114 | static inline void raid5_inc_bi_active_stripes(struct bio *bio) |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 115 | { |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame^] | 116 | atomic_t *segments = (atomic_t *)&bio->bi_phys_segments; |
| 117 | atomic_inc(segments); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 118 | } |
| 119 | |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame^] | 120 | static inline void raid5_set_bi_processed_stripes(struct bio *bio, |
| 121 | unsigned int cnt) |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 122 | { |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame^] | 123 | atomic_t *segments = (atomic_t *)&bio->bi_phys_segments; |
| 124 | int old, new; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 125 | |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame^] | 126 | do { |
| 127 | old = atomic_read(segments); |
| 128 | new = (old & 0xffff) | (cnt << 16); |
| 129 | } while (atomic_cmpxchg(segments, old, new) != old); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame^] | 132 | static inline void raid5_set_bi_stripes(struct bio *bio, unsigned int cnt) |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 133 | { |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame^] | 134 | atomic_t *segments = (atomic_t *)&bio->bi_phys_segments; |
| 135 | atomic_set(segments, cnt); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 136 | } |
| 137 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 138 | /* Find first data disk in a raid6 stripe */ |
| 139 | static inline int raid6_d0(struct stripe_head *sh) |
| 140 | { |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 141 | if (sh->ddf_layout) |
| 142 | /* ddf always start from first device */ |
| 143 | return 0; |
| 144 | /* md starts just after Q block */ |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 145 | if (sh->qd_idx == sh->disks - 1) |
| 146 | return 0; |
| 147 | else |
| 148 | return sh->qd_idx + 1; |
| 149 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 150 | static inline int raid6_next_disk(int disk, int raid_disks) |
| 151 | { |
| 152 | disk++; |
| 153 | return (disk < raid_disks) ? disk : 0; |
| 154 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 155 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 156 | /* When walking through the disks in a raid5, starting at raid6_d0, |
| 157 | * We need to map each disk to a 'slot', where the data disks are slot |
| 158 | * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk |
| 159 | * is raid_disks-1. This help does that mapping. |
| 160 | */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 161 | static int raid6_idx_to_slot(int idx, struct stripe_head *sh, |
| 162 | int *count, int syndrome_disks) |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 163 | { |
Dan Williams | 6629542 | 2009-10-19 18:09:32 -0700 | [diff] [blame] | 164 | int slot = *count; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 165 | |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 166 | if (sh->ddf_layout) |
Dan Williams | 6629542 | 2009-10-19 18:09:32 -0700 | [diff] [blame] | 167 | (*count)++; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 168 | if (idx == sh->pd_idx) |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 169 | return syndrome_disks; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 170 | if (idx == sh->qd_idx) |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 171 | return syndrome_disks + 1; |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 172 | if (!sh->ddf_layout) |
Dan Williams | 6629542 | 2009-10-19 18:09:32 -0700 | [diff] [blame] | 173 | (*count)++; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 174 | return slot; |
| 175 | } |
| 176 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 177 | static void return_io(struct bio *return_bi) |
| 178 | { |
| 179 | struct bio *bi = return_bi; |
| 180 | while (bi) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 181 | |
| 182 | return_bi = bi->bi_next; |
| 183 | bi->bi_next = NULL; |
| 184 | bi->bi_size = 0; |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 185 | bio_endio(bi, 0); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 186 | bi = return_bi; |
| 187 | } |
| 188 | } |
| 189 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 190 | static void print_raid5_conf (struct r5conf *conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 192 | static int stripe_operations_active(struct stripe_head *sh) |
| 193 | { |
| 194 | return sh->check_state || sh->reconstruct_state || |
| 195 | test_bit(STRIPE_BIOFILL_RUN, &sh->state) || |
| 196 | test_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 197 | } |
| 198 | |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 199 | static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | { |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 201 | BUG_ON(!list_empty(&sh->lru)); |
| 202 | BUG_ON(atomic_read(&conf->active_stripes)==0); |
| 203 | if (test_bit(STRIPE_HANDLE, &sh->state)) { |
| 204 | if (test_bit(STRIPE_DELAYED, &sh->state) && |
| 205 | !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 206 | list_add_tail(&sh->lru, &conf->delayed_list); |
| 207 | else if (test_bit(STRIPE_BIT_DELAY, &sh->state) && |
| 208 | sh->bm_seq - conf->seq_write > 0) |
| 209 | list_add_tail(&sh->lru, &conf->bitmap_list); |
| 210 | else { |
| 211 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 212 | clear_bit(STRIPE_BIT_DELAY, &sh->state); |
| 213 | list_add_tail(&sh->lru, &conf->handle_list); |
| 214 | } |
| 215 | md_wakeup_thread(conf->mddev->thread); |
| 216 | } else { |
| 217 | BUG_ON(stripe_operations_active(sh)); |
| 218 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 219 | if (atomic_dec_return(&conf->preread_active_stripes) |
| 220 | < IO_THRESHOLD) |
| 221 | md_wakeup_thread(conf->mddev->thread); |
| 222 | atomic_dec(&conf->active_stripes); |
| 223 | if (!test_bit(STRIPE_EXPANDING, &sh->state)) { |
| 224 | list_add_tail(&sh->lru, &conf->inactive_list); |
| 225 | wake_up(&conf->wait_for_stripe); |
| 226 | if (conf->retry_read_aligned) |
| 227 | md_wakeup_thread(conf->mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | } |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 231 | |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 232 | static void __release_stripe(struct r5conf *conf, struct stripe_head *sh) |
| 233 | { |
| 234 | if (atomic_dec_and_test(&sh->count)) |
| 235 | do_release_stripe(conf, sh); |
| 236 | } |
| 237 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | static void release_stripe(struct stripe_head *sh) |
| 239 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 240 | struct r5conf *conf = sh->raid_conf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | unsigned long flags; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 242 | |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 243 | local_irq_save(flags); |
| 244 | if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) { |
| 245 | do_release_stripe(conf, sh); |
| 246 | spin_unlock(&conf->device_lock); |
| 247 | } |
| 248 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | } |
| 250 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 251 | static inline void remove_hash(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 253 | pr_debug("remove_hash(), stripe %llu\n", |
| 254 | (unsigned long long)sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 256 | hlist_del_init(&sh->hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | } |
| 258 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 259 | static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | { |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 261 | struct hlist_head *hp = stripe_hash(conf, sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 263 | pr_debug("insert_hash(), stripe %llu\n", |
| 264 | (unsigned long long)sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 266 | hlist_add_head(&sh->hash, hp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | |
| 270 | /* find an idle stripe, make sure it is unhashed, and return it. */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 271 | static struct stripe_head *get_free_stripe(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | { |
| 273 | struct stripe_head *sh = NULL; |
| 274 | struct list_head *first; |
| 275 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | if (list_empty(&conf->inactive_list)) |
| 277 | goto out; |
| 278 | first = conf->inactive_list.next; |
| 279 | sh = list_entry(first, struct stripe_head, lru); |
| 280 | list_del_init(first); |
| 281 | remove_hash(sh); |
| 282 | atomic_inc(&conf->active_stripes); |
| 283 | out: |
| 284 | return sh; |
| 285 | } |
| 286 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 287 | static void shrink_buffers(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | { |
| 289 | struct page *p; |
| 290 | int i; |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 291 | int num = sh->raid_conf->pool_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 293 | for (i = 0; i < num ; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | p = sh->dev[i].page; |
| 295 | if (!p) |
| 296 | continue; |
| 297 | sh->dev[i].page = NULL; |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 298 | put_page(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 302 | static int grow_buffers(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | { |
| 304 | int i; |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 305 | int num = sh->raid_conf->pool_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 307 | for (i = 0; i < num; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | struct page *page; |
| 309 | |
| 310 | if (!(page = alloc_page(GFP_KERNEL))) { |
| 311 | return 1; |
| 312 | } |
| 313 | sh->dev[i].page = page; |
| 314 | } |
| 315 | return 0; |
| 316 | } |
| 317 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 318 | static void raid5_build_block(struct stripe_head *sh, int i, int previous); |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 319 | static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 320 | struct stripe_head *sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 322 | 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] | 323 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 324 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 325 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 327 | BUG_ON(atomic_read(&sh->count) != 0); |
| 328 | BUG_ON(test_bit(STRIPE_HANDLE, &sh->state)); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 329 | BUG_ON(stripe_operations_active(sh)); |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 330 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 331 | pr_debug("init_stripe called, stripe %llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | (unsigned long long)sh->sector); |
| 333 | |
| 334 | remove_hash(sh); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 335 | |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 336 | sh->generation = conf->generation - previous; |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 337 | sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | sh->sector = sector; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 339 | stripe_set_idx(sector, conf, previous, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | sh->state = 0; |
| 341 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 342 | |
| 343 | for (i = sh->disks; i--; ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | struct r5dev *dev = &sh->dev[i]; |
| 345 | |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 346 | if (dev->toread || dev->read || dev->towrite || dev->written || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | test_bit(R5_LOCKED, &dev->flags)) { |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 348 | 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] | 349 | (unsigned long long)sh->sector, i, dev->toread, |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 350 | dev->read, dev->towrite, dev->written, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | test_bit(R5_LOCKED, &dev->flags)); |
NeilBrown | 8cfa7b0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 352 | WARN_ON(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | } |
| 354 | dev->flags = 0; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 355 | raid5_build_block(sh, i, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | } |
| 357 | insert_hash(conf, sh); |
| 358 | } |
| 359 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 360 | static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector, |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 361 | short generation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | { |
| 363 | struct stripe_head *sh; |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 364 | struct hlist_node *hn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 366 | pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector); |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 367 | hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash) |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 368 | if (sh->sector == sector && sh->generation == generation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | return sh; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 370 | pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | return NULL; |
| 372 | } |
| 373 | |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 374 | /* |
| 375 | * Need to check if array has failed when deciding whether to: |
| 376 | * - start an array |
| 377 | * - remove non-faulty devices |
| 378 | * - add a spare |
| 379 | * - allow a reshape |
| 380 | * This determination is simple when no reshape is happening. |
| 381 | * However if there is a reshape, we need to carefully check |
| 382 | * both the before and after sections. |
| 383 | * This is because some failed devices may only affect one |
| 384 | * of the two sections, and some non-in_sync devices may |
| 385 | * be insync in the section most affected by failed devices. |
| 386 | */ |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 387 | static int calc_degraded(struct r5conf *conf) |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 388 | { |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 389 | int degraded, degraded2; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 390 | int i; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 391 | |
| 392 | rcu_read_lock(); |
| 393 | degraded = 0; |
| 394 | for (i = 0; i < conf->previous_raid_disks; i++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 395 | struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 396 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
| 397 | degraded++; |
| 398 | else if (test_bit(In_sync, &rdev->flags)) |
| 399 | ; |
| 400 | else |
| 401 | /* not in-sync or faulty. |
| 402 | * If the reshape increases the number of devices, |
| 403 | * this is being recovered by the reshape, so |
| 404 | * this 'previous' section is not in_sync. |
| 405 | * If the number of devices is being reduced however, |
| 406 | * the device can only be part of the array if |
| 407 | * we are reverting a reshape, so this section will |
| 408 | * be in-sync. |
| 409 | */ |
| 410 | if (conf->raid_disks >= conf->previous_raid_disks) |
| 411 | degraded++; |
| 412 | } |
| 413 | rcu_read_unlock(); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 414 | if (conf->raid_disks == conf->previous_raid_disks) |
| 415 | return degraded; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 416 | rcu_read_lock(); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 417 | degraded2 = 0; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 418 | for (i = 0; i < conf->raid_disks; i++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 419 | struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 420 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 421 | degraded2++; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 422 | else if (test_bit(In_sync, &rdev->flags)) |
| 423 | ; |
| 424 | else |
| 425 | /* not in-sync or faulty. |
| 426 | * If reshape increases the number of devices, this |
| 427 | * section has already been recovered, else it |
| 428 | * almost certainly hasn't. |
| 429 | */ |
| 430 | if (conf->raid_disks <= conf->previous_raid_disks) |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 431 | degraded2++; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 432 | } |
| 433 | rcu_read_unlock(); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 434 | if (degraded2 > degraded) |
| 435 | return degraded2; |
| 436 | return degraded; |
| 437 | } |
| 438 | |
| 439 | static int has_failed(struct r5conf *conf) |
| 440 | { |
| 441 | int degraded; |
| 442 | |
| 443 | if (conf->mddev->reshape_position == MaxSector) |
| 444 | return conf->mddev->degraded > conf->max_degraded; |
| 445 | |
| 446 | degraded = calc_degraded(conf); |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 447 | if (degraded > conf->max_degraded) |
| 448 | return 1; |
| 449 | return 0; |
| 450 | } |
| 451 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 452 | static struct stripe_head * |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 453 | get_active_stripe(struct r5conf *conf, sector_t sector, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 454 | int previous, int noblock, int noquiesce) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | { |
| 456 | struct stripe_head *sh; |
| 457 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 458 | pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | |
| 460 | spin_lock_irq(&conf->device_lock); |
| 461 | |
| 462 | do { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 463 | wait_event_lock_irq(conf->wait_for_stripe, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 464 | conf->quiesce == 0 || noquiesce, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 465 | conf->device_lock, /* nothing */); |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 466 | sh = __find_stripe(conf, sector, conf->generation - previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | if (!sh) { |
| 468 | if (!conf->inactive_blocked) |
| 469 | sh = get_free_stripe(conf); |
| 470 | if (noblock && sh == NULL) |
| 471 | break; |
| 472 | if (!sh) { |
| 473 | conf->inactive_blocked = 1; |
| 474 | wait_event_lock_irq(conf->wait_for_stripe, |
| 475 | !list_empty(&conf->inactive_list) && |
NeilBrown | 5036805 | 2005-12-12 02:39:17 -0800 | [diff] [blame] | 476 | (atomic_read(&conf->active_stripes) |
| 477 | < (conf->max_nr_stripes *3/4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | || !conf->inactive_blocked), |
| 479 | conf->device_lock, |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 480 | ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | conf->inactive_blocked = 0; |
| 482 | } else |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 483 | init_stripe(sh, sector, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | } else { |
| 485 | if (atomic_read(&sh->count)) { |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 486 | BUG_ON(!list_empty(&sh->lru) |
| 487 | && !test_bit(STRIPE_EXPANDING, &sh->state)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | } else { |
| 489 | if (!test_bit(STRIPE_HANDLE, &sh->state)) |
| 490 | atomic_inc(&conf->active_stripes); |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 491 | if (list_empty(&sh->lru) && |
| 492 | !test_bit(STRIPE_EXPANDING, &sh->state)) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 493 | BUG(); |
| 494 | list_del_init(&sh->lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | } while (sh == NULL); |
| 498 | |
| 499 | if (sh) |
| 500 | atomic_inc(&sh->count); |
| 501 | |
| 502 | spin_unlock_irq(&conf->device_lock); |
| 503 | return sh; |
| 504 | } |
| 505 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 506 | /* Determine if 'data_offset' or 'new_data_offset' should be used |
| 507 | * in this stripe_head. |
| 508 | */ |
| 509 | static int use_new_offset(struct r5conf *conf, struct stripe_head *sh) |
| 510 | { |
| 511 | sector_t progress = conf->reshape_progress; |
| 512 | /* Need a memory barrier to make sure we see the value |
| 513 | * of conf->generation, or ->data_offset that was set before |
| 514 | * reshape_progress was updated. |
| 515 | */ |
| 516 | smp_rmb(); |
| 517 | if (progress == MaxSector) |
| 518 | return 0; |
| 519 | if (sh->generation == conf->generation - 1) |
| 520 | return 0; |
| 521 | /* We are in a reshape, and this is a new-generation stripe, |
| 522 | * so use new_data_offset. |
| 523 | */ |
| 524 | return 1; |
| 525 | } |
| 526 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 527 | static void |
| 528 | raid5_end_read_request(struct bio *bi, int error); |
| 529 | static void |
| 530 | raid5_end_write_request(struct bio *bi, int error); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 531 | |
Dan Williams | c4e5ac0 | 2008-06-28 08:31:53 +1000 | [diff] [blame] | 532 | 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] | 533 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 534 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 535 | int i, disks = sh->disks; |
| 536 | |
| 537 | might_sleep(); |
| 538 | |
| 539 | for (i = disks; i--; ) { |
| 540 | int rw; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 541 | int replace_only = 0; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 542 | struct bio *bi, *rbi; |
| 543 | struct md_rdev *rdev, *rrdev = NULL; |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 544 | if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) { |
| 545 | if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags)) |
| 546 | rw = WRITE_FUA; |
| 547 | else |
| 548 | rw = WRITE; |
| 549 | } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 550 | rw = READ; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 551 | else if (test_and_clear_bit(R5_WantReplace, |
| 552 | &sh->dev[i].flags)) { |
| 553 | rw = WRITE; |
| 554 | replace_only = 1; |
| 555 | } else |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 556 | continue; |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 557 | if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags)) |
| 558 | rw |= REQ_SYNC; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 559 | |
| 560 | bi = &sh->dev[i].req; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 561 | rbi = &sh->dev[i].rreq; /* For writing to replacement */ |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 562 | |
| 563 | bi->bi_rw = rw; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 564 | rbi->bi_rw = rw; |
| 565 | if (rw & WRITE) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 566 | bi->bi_end_io = raid5_end_write_request; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 567 | rbi->bi_end_io = raid5_end_write_request; |
| 568 | } else |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 569 | bi->bi_end_io = raid5_end_read_request; |
| 570 | |
| 571 | rcu_read_lock(); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 572 | rrdev = rcu_dereference(conf->disks[i].replacement); |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 573 | smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */ |
| 574 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 575 | if (!rdev) { |
| 576 | rdev = rrdev; |
| 577 | rrdev = NULL; |
| 578 | } |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 579 | if (rw & WRITE) { |
| 580 | if (replace_only) |
| 581 | rdev = NULL; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 582 | if (rdev == rrdev) |
| 583 | /* We raced and saw duplicates */ |
| 584 | rrdev = NULL; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 585 | } else { |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 586 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 587 | rdev = rrdev; |
| 588 | rrdev = NULL; |
| 589 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 590 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 591 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 592 | rdev = NULL; |
| 593 | if (rdev) |
| 594 | atomic_inc(&rdev->nr_pending); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 595 | if (rrdev && test_bit(Faulty, &rrdev->flags)) |
| 596 | rrdev = NULL; |
| 597 | if (rrdev) |
| 598 | atomic_inc(&rrdev->nr_pending); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 599 | rcu_read_unlock(); |
| 600 | |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 601 | /* We have already checked bad blocks for reads. Now |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 602 | * need to check for writes. We never accept write errors |
| 603 | * on the replacement, so we don't to check rrdev. |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 604 | */ |
| 605 | while ((rw & WRITE) && rdev && |
| 606 | test_bit(WriteErrorSeen, &rdev->flags)) { |
| 607 | sector_t first_bad; |
| 608 | int bad_sectors; |
| 609 | int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 610 | &first_bad, &bad_sectors); |
| 611 | if (!bad) |
| 612 | break; |
| 613 | |
| 614 | if (bad < 0) { |
| 615 | set_bit(BlockedBadBlocks, &rdev->flags); |
| 616 | if (!conf->mddev->external && |
| 617 | conf->mddev->flags) { |
| 618 | /* It is very unlikely, but we might |
| 619 | * still need to write out the |
| 620 | * bad block log - better give it |
| 621 | * a chance*/ |
| 622 | md_check_recovery(conf->mddev); |
| 623 | } |
majianpeng | 1850753 | 2012-07-03 12:11:54 +1000 | [diff] [blame] | 624 | /* |
| 625 | * Because md_wait_for_blocked_rdev |
| 626 | * will dec nr_pending, we must |
| 627 | * increment it first. |
| 628 | */ |
| 629 | atomic_inc(&rdev->nr_pending); |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 630 | md_wait_for_blocked_rdev(rdev, conf->mddev); |
| 631 | } else { |
| 632 | /* Acknowledged bad block - skip the write */ |
| 633 | rdev_dec_pending(rdev, conf->mddev); |
| 634 | rdev = NULL; |
| 635 | } |
| 636 | } |
| 637 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 638 | if (rdev) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 639 | if (s->syncing || s->expanding || s->expanded |
| 640 | || s->replacing) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 641 | md_sync_acct(rdev->bdev, STRIPE_SECTORS); |
| 642 | |
Dan Williams | 2b7497f | 2008-06-28 08:31:52 +1000 | [diff] [blame] | 643 | set_bit(STRIPE_IO_STARTED, &sh->state); |
| 644 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 645 | bi->bi_bdev = rdev->bdev; |
| 646 | pr_debug("%s: for %llu schedule op %ld on disc %d\n", |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 647 | __func__, (unsigned long long)sh->sector, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 648 | bi->bi_rw, i); |
| 649 | atomic_inc(&sh->count); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 650 | if (use_new_offset(conf, sh)) |
| 651 | bi->bi_sector = (sh->sector |
| 652 | + rdev->new_data_offset); |
| 653 | else |
| 654 | bi->bi_sector = (sh->sector |
| 655 | + rdev->data_offset); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 656 | bi->bi_flags = 1 << BIO_UPTODATE; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 657 | bi->bi_idx = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 658 | bi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
| 659 | bi->bi_io_vec[0].bv_offset = 0; |
| 660 | bi->bi_size = STRIPE_SIZE; |
| 661 | bi->bi_next = NULL; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 662 | if (rrdev) |
| 663 | set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 664 | generic_make_request(bi); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 665 | } |
| 666 | if (rrdev) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 667 | if (s->syncing || s->expanding || s->expanded |
| 668 | || s->replacing) |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 669 | md_sync_acct(rrdev->bdev, STRIPE_SECTORS); |
| 670 | |
| 671 | set_bit(STRIPE_IO_STARTED, &sh->state); |
| 672 | |
| 673 | rbi->bi_bdev = rrdev->bdev; |
| 674 | pr_debug("%s: for %llu schedule op %ld on " |
| 675 | "replacement disc %d\n", |
| 676 | __func__, (unsigned long long)sh->sector, |
| 677 | rbi->bi_rw, i); |
| 678 | atomic_inc(&sh->count); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 679 | if (use_new_offset(conf, sh)) |
| 680 | rbi->bi_sector = (sh->sector |
| 681 | + rrdev->new_data_offset); |
| 682 | else |
| 683 | rbi->bi_sector = (sh->sector |
| 684 | + rrdev->data_offset); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 685 | rbi->bi_flags = 1 << BIO_UPTODATE; |
| 686 | rbi->bi_idx = 0; |
| 687 | rbi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
| 688 | rbi->bi_io_vec[0].bv_offset = 0; |
| 689 | rbi->bi_size = STRIPE_SIZE; |
| 690 | rbi->bi_next = NULL; |
| 691 | generic_make_request(rbi); |
| 692 | } |
| 693 | if (!rdev && !rrdev) { |
Namhyung Kim | b062962 | 2011-06-14 14:20:19 +1000 | [diff] [blame] | 694 | if (rw & WRITE) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 695 | set_bit(STRIPE_DEGRADED, &sh->state); |
| 696 | pr_debug("skip op %ld on disc %d for sector %llu\n", |
| 697 | bi->bi_rw, i, (unsigned long long)sh->sector); |
| 698 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 699 | set_bit(STRIPE_HANDLE, &sh->state); |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | static struct dma_async_tx_descriptor * |
| 705 | async_copy_data(int frombio, struct bio *bio, struct page *page, |
| 706 | sector_t sector, struct dma_async_tx_descriptor *tx) |
| 707 | { |
| 708 | struct bio_vec *bvl; |
| 709 | struct page *bio_page; |
| 710 | int i; |
| 711 | int page_offset; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 712 | struct async_submit_ctl submit; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 713 | enum async_tx_flags flags = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 714 | |
| 715 | if (bio->bi_sector >= sector) |
| 716 | page_offset = (signed)(bio->bi_sector - sector) * 512; |
| 717 | else |
| 718 | page_offset = (signed)(sector - bio->bi_sector) * -512; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 719 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 720 | if (frombio) |
| 721 | flags |= ASYNC_TX_FENCE; |
| 722 | init_async_submit(&submit, flags, tx, NULL, NULL, NULL); |
| 723 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 724 | bio_for_each_segment(bvl, bio, i) { |
Namhyung Kim | fcde907 | 2011-06-14 14:23:57 +1000 | [diff] [blame] | 725 | int len = bvl->bv_len; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 726 | int clen; |
| 727 | int b_offset = 0; |
| 728 | |
| 729 | if (page_offset < 0) { |
| 730 | b_offset = -page_offset; |
| 731 | page_offset += b_offset; |
| 732 | len -= b_offset; |
| 733 | } |
| 734 | |
| 735 | if (len > 0 && page_offset + len > STRIPE_SIZE) |
| 736 | clen = STRIPE_SIZE - page_offset; |
| 737 | else |
| 738 | clen = len; |
| 739 | |
| 740 | if (clen > 0) { |
Namhyung Kim | fcde907 | 2011-06-14 14:23:57 +1000 | [diff] [blame] | 741 | b_offset += bvl->bv_offset; |
| 742 | bio_page = bvl->bv_page; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 743 | if (frombio) |
| 744 | tx = async_memcpy(page, bio_page, page_offset, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 745 | b_offset, clen, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 746 | else |
| 747 | tx = async_memcpy(bio_page, page, b_offset, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 748 | page_offset, clen, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 749 | } |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 750 | /* chain the operations */ |
| 751 | submit.depend_tx = tx; |
| 752 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 753 | if (clen < len) /* hit end of page */ |
| 754 | break; |
| 755 | page_offset += len; |
| 756 | } |
| 757 | |
| 758 | return tx; |
| 759 | } |
| 760 | |
| 761 | static void ops_complete_biofill(void *stripe_head_ref) |
| 762 | { |
| 763 | struct stripe_head *sh = stripe_head_ref; |
| 764 | struct bio *return_bi = NULL; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 765 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 766 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 767 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 768 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 769 | (unsigned long long)sh->sector); |
| 770 | |
| 771 | /* clear completed biofills */ |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 772 | spin_lock_irq(&conf->device_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 773 | for (i = sh->disks; i--; ) { |
| 774 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 775 | |
| 776 | /* acknowledge completion of a biofill operation */ |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 777 | /* and check if we need to reply to a read request, |
| 778 | * new R5_Wantfill requests are held off until |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 779 | * !STRIPE_BIOFILL_RUN |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 780 | */ |
| 781 | if (test_and_clear_bit(R5_Wantfill, &dev->flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 782 | struct bio *rbi, *rbi2; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 783 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 784 | BUG_ON(!dev->read); |
| 785 | rbi = dev->read; |
| 786 | dev->read = NULL; |
| 787 | while (rbi && rbi->bi_sector < |
| 788 | dev->sector + STRIPE_SECTORS) { |
| 789 | rbi2 = r5_next_bio(rbi, dev->sector); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame^] | 790 | if (!raid5_dec_bi_active_stripes(rbi)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 791 | rbi->bi_next = return_bi; |
| 792 | return_bi = rbi; |
| 793 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 794 | rbi = rbi2; |
| 795 | } |
| 796 | } |
| 797 | } |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 798 | spin_unlock_irq(&conf->device_lock); |
| 799 | clear_bit(STRIPE_BIOFILL_RUN, &sh->state); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 800 | |
| 801 | return_io(return_bi); |
| 802 | |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 803 | set_bit(STRIPE_HANDLE, &sh->state); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 804 | release_stripe(sh); |
| 805 | } |
| 806 | |
| 807 | static void ops_run_biofill(struct stripe_head *sh) |
| 808 | { |
| 809 | struct dma_async_tx_descriptor *tx = NULL; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 810 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 811 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 812 | int i; |
| 813 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 814 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 815 | (unsigned long long)sh->sector); |
| 816 | |
| 817 | for (i = sh->disks; i--; ) { |
| 818 | struct r5dev *dev = &sh->dev[i]; |
| 819 | if (test_bit(R5_Wantfill, &dev->flags)) { |
| 820 | struct bio *rbi; |
| 821 | spin_lock_irq(&conf->device_lock); |
| 822 | dev->read = rbi = dev->toread; |
| 823 | dev->toread = NULL; |
| 824 | spin_unlock_irq(&conf->device_lock); |
| 825 | while (rbi && rbi->bi_sector < |
| 826 | dev->sector + STRIPE_SECTORS) { |
| 827 | tx = async_copy_data(0, rbi, dev->page, |
| 828 | dev->sector, tx); |
| 829 | rbi = r5_next_bio(rbi, dev->sector); |
| 830 | } |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 835 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL); |
| 836 | async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 837 | } |
| 838 | |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 839 | static void mark_target_uptodate(struct stripe_head *sh, int target) |
| 840 | { |
| 841 | struct r5dev *tgt; |
| 842 | |
| 843 | if (target < 0) |
| 844 | return; |
| 845 | |
| 846 | tgt = &sh->dev[target]; |
| 847 | set_bit(R5_UPTODATE, &tgt->flags); |
| 848 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 849 | clear_bit(R5_Wantcompute, &tgt->flags); |
| 850 | } |
| 851 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 852 | static void ops_complete_compute(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 853 | { |
| 854 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 855 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 856 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 857 | (unsigned long long)sh->sector); |
| 858 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 859 | /* mark the computed target(s) as uptodate */ |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 860 | mark_target_uptodate(sh, sh->ops.target); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 861 | mark_target_uptodate(sh, sh->ops.target2); |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 862 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 863 | clear_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 864 | if (sh->check_state == check_state_compute_run) |
| 865 | sh->check_state = check_state_compute_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 866 | set_bit(STRIPE_HANDLE, &sh->state); |
| 867 | release_stripe(sh); |
| 868 | } |
| 869 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 870 | /* return a pointer to the address conversion region of the scribble buffer */ |
| 871 | static addr_conv_t *to_addr_conv(struct stripe_head *sh, |
| 872 | struct raid5_percpu *percpu) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 873 | { |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 874 | return percpu->scribble + sizeof(struct page *) * (sh->disks + 2); |
| 875 | } |
| 876 | |
| 877 | static struct dma_async_tx_descriptor * |
| 878 | ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 879 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 880 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 881 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 882 | int target = sh->ops.target; |
| 883 | struct r5dev *tgt = &sh->dev[target]; |
| 884 | struct page *xor_dest = tgt->page; |
| 885 | int count = 0; |
| 886 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 887 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 888 | int i; |
| 889 | |
| 890 | pr_debug("%s: stripe %llu block: %d\n", |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 891 | __func__, (unsigned long long)sh->sector, target); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 892 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 893 | |
| 894 | for (i = disks; i--; ) |
| 895 | if (i != target) |
| 896 | xor_srcs[count++] = sh->dev[i].page; |
| 897 | |
| 898 | atomic_inc(&sh->count); |
| 899 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 900 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 901 | ops_complete_compute, sh, to_addr_conv(sh, percpu)); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 902 | if (unlikely(count == 1)) |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 903 | tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 904 | else |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 905 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 906 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 907 | return tx; |
| 908 | } |
| 909 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 910 | /* set_syndrome_sources - populate source buffers for gen_syndrome |
| 911 | * @srcs - (struct page *) array of size sh->disks |
| 912 | * @sh - stripe_head to parse |
| 913 | * |
| 914 | * Populates srcs in proper layout order for the stripe and returns the |
| 915 | * 'count' of sources to be used in a call to async_gen_syndrome. The P |
| 916 | * destination buffer is recorded in srcs[count] and the Q destination |
| 917 | * is recorded in srcs[count+1]]. |
| 918 | */ |
| 919 | static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh) |
| 920 | { |
| 921 | int disks = sh->disks; |
| 922 | int syndrome_disks = sh->ddf_layout ? disks : (disks - 2); |
| 923 | int d0_idx = raid6_d0(sh); |
| 924 | int count; |
| 925 | int i; |
| 926 | |
| 927 | for (i = 0; i < disks; i++) |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 928 | srcs[i] = NULL; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 929 | |
| 930 | count = 0; |
| 931 | i = d0_idx; |
| 932 | do { |
| 933 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 934 | |
| 935 | srcs[slot] = sh->dev[i].page; |
| 936 | i = raid6_next_disk(i, disks); |
| 937 | } while (i != d0_idx); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 938 | |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 939 | return syndrome_disks; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | static struct dma_async_tx_descriptor * |
| 943 | ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 944 | { |
| 945 | int disks = sh->disks; |
| 946 | struct page **blocks = percpu->scribble; |
| 947 | int target; |
| 948 | int qd_idx = sh->qd_idx; |
| 949 | struct dma_async_tx_descriptor *tx; |
| 950 | struct async_submit_ctl submit; |
| 951 | struct r5dev *tgt; |
| 952 | struct page *dest; |
| 953 | int i; |
| 954 | int count; |
| 955 | |
| 956 | if (sh->ops.target < 0) |
| 957 | target = sh->ops.target2; |
| 958 | else if (sh->ops.target2 < 0) |
| 959 | target = sh->ops.target; |
| 960 | else |
| 961 | /* we should only have one valid target */ |
| 962 | BUG(); |
| 963 | BUG_ON(target < 0); |
| 964 | pr_debug("%s: stripe %llu block: %d\n", |
| 965 | __func__, (unsigned long long)sh->sector, target); |
| 966 | |
| 967 | tgt = &sh->dev[target]; |
| 968 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 969 | dest = tgt->page; |
| 970 | |
| 971 | atomic_inc(&sh->count); |
| 972 | |
| 973 | if (target == qd_idx) { |
| 974 | count = set_syndrome_sources(blocks, sh); |
| 975 | blocks[count] = NULL; /* regenerating p is not necessary */ |
| 976 | BUG_ON(blocks[count+1] != dest); /* q should already be set */ |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 977 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 978 | ops_complete_compute, sh, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 979 | to_addr_conv(sh, percpu)); |
| 980 | tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
| 981 | } else { |
| 982 | /* Compute any data- or p-drive using XOR */ |
| 983 | count = 0; |
| 984 | for (i = disks; i-- ; ) { |
| 985 | if (i == target || i == qd_idx) |
| 986 | continue; |
| 987 | blocks[count++] = sh->dev[i].page; |
| 988 | } |
| 989 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 990 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
| 991 | NULL, ops_complete_compute, sh, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 992 | to_addr_conv(sh, percpu)); |
| 993 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit); |
| 994 | } |
| 995 | |
| 996 | return tx; |
| 997 | } |
| 998 | |
| 999 | static struct dma_async_tx_descriptor * |
| 1000 | ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 1001 | { |
| 1002 | int i, count, disks = sh->disks; |
| 1003 | int syndrome_disks = sh->ddf_layout ? disks : disks-2; |
| 1004 | int d0_idx = raid6_d0(sh); |
| 1005 | int faila = -1, failb = -1; |
| 1006 | int target = sh->ops.target; |
| 1007 | int target2 = sh->ops.target2; |
| 1008 | struct r5dev *tgt = &sh->dev[target]; |
| 1009 | struct r5dev *tgt2 = &sh->dev[target2]; |
| 1010 | struct dma_async_tx_descriptor *tx; |
| 1011 | struct page **blocks = percpu->scribble; |
| 1012 | struct async_submit_ctl submit; |
| 1013 | |
| 1014 | pr_debug("%s: stripe %llu block1: %d block2: %d\n", |
| 1015 | __func__, (unsigned long long)sh->sector, target, target2); |
| 1016 | BUG_ON(target < 0 || target2 < 0); |
| 1017 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 1018 | BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags)); |
| 1019 | |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1020 | /* we need to open-code set_syndrome_sources to handle the |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1021 | * slot number conversion for 'faila' and 'failb' |
| 1022 | */ |
| 1023 | for (i = 0; i < disks ; i++) |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 1024 | blocks[i] = NULL; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1025 | count = 0; |
| 1026 | i = d0_idx; |
| 1027 | do { |
| 1028 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 1029 | |
| 1030 | blocks[slot] = sh->dev[i].page; |
| 1031 | |
| 1032 | if (i == target) |
| 1033 | faila = slot; |
| 1034 | if (i == target2) |
| 1035 | failb = slot; |
| 1036 | i = raid6_next_disk(i, disks); |
| 1037 | } while (i != d0_idx); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1038 | |
| 1039 | BUG_ON(faila == failb); |
| 1040 | if (failb < faila) |
| 1041 | swap(faila, failb); |
| 1042 | pr_debug("%s: stripe: %llu faila: %d failb: %d\n", |
| 1043 | __func__, (unsigned long long)sh->sector, faila, failb); |
| 1044 | |
| 1045 | atomic_inc(&sh->count); |
| 1046 | |
| 1047 | if (failb == syndrome_disks+1) { |
| 1048 | /* Q disk is one of the missing disks */ |
| 1049 | if (faila == syndrome_disks) { |
| 1050 | /* Missing P+Q, just recompute */ |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1051 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 1052 | ops_complete_compute, sh, |
| 1053 | to_addr_conv(sh, percpu)); |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 1054 | return async_gen_syndrome(blocks, 0, syndrome_disks+2, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1055 | STRIPE_SIZE, &submit); |
| 1056 | } else { |
| 1057 | struct page *dest; |
| 1058 | int data_target; |
| 1059 | int qd_idx = sh->qd_idx; |
| 1060 | |
| 1061 | /* Missing D+Q: recompute D from P, then recompute Q */ |
| 1062 | if (target == qd_idx) |
| 1063 | data_target = target2; |
| 1064 | else |
| 1065 | data_target = target; |
| 1066 | |
| 1067 | count = 0; |
| 1068 | for (i = disks; i-- ; ) { |
| 1069 | if (i == data_target || i == qd_idx) |
| 1070 | continue; |
| 1071 | blocks[count++] = sh->dev[i].page; |
| 1072 | } |
| 1073 | dest = sh->dev[data_target].page; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1074 | init_async_submit(&submit, |
| 1075 | ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
| 1076 | NULL, NULL, NULL, |
| 1077 | to_addr_conv(sh, percpu)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1078 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, |
| 1079 | &submit); |
| 1080 | |
| 1081 | count = set_syndrome_sources(blocks, sh); |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1082 | init_async_submit(&submit, ASYNC_TX_FENCE, tx, |
| 1083 | ops_complete_compute, sh, |
| 1084 | to_addr_conv(sh, percpu)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1085 | return async_gen_syndrome(blocks, 0, count+2, |
| 1086 | STRIPE_SIZE, &submit); |
| 1087 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1088 | } else { |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1089 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 1090 | ops_complete_compute, sh, |
| 1091 | to_addr_conv(sh, percpu)); |
| 1092 | if (failb == syndrome_disks) { |
| 1093 | /* We're missing D+P. */ |
| 1094 | return async_raid6_datap_recov(syndrome_disks+2, |
| 1095 | STRIPE_SIZE, faila, |
| 1096 | blocks, &submit); |
| 1097 | } else { |
| 1098 | /* We're missing D+D. */ |
| 1099 | return async_raid6_2data_recov(syndrome_disks+2, |
| 1100 | STRIPE_SIZE, faila, failb, |
| 1101 | blocks, &submit); |
| 1102 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1107 | static void ops_complete_prexor(void *stripe_head_ref) |
| 1108 | { |
| 1109 | struct stripe_head *sh = stripe_head_ref; |
| 1110 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1111 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1112 | (unsigned long long)sh->sector); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | static struct dma_async_tx_descriptor * |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1116 | ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1117 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1118 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1119 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1120 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1121 | int count = 0, pd_idx = sh->pd_idx, i; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1122 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1123 | |
| 1124 | /* existing parity data subtracted */ |
| 1125 | struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 1126 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1127 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1128 | (unsigned long long)sh->sector); |
| 1129 | |
| 1130 | for (i = disks; i--; ) { |
| 1131 | struct r5dev *dev = &sh->dev[i]; |
| 1132 | /* Only process blocks that are known to be uptodate */ |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1133 | if (test_bit(R5_Wantdrain, &dev->flags)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1134 | xor_srcs[count++] = dev->page; |
| 1135 | } |
| 1136 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1137 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx, |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1138 | ops_complete_prexor, sh, to_addr_conv(sh, percpu)); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1139 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1140 | |
| 1141 | return tx; |
| 1142 | } |
| 1143 | |
| 1144 | static struct dma_async_tx_descriptor * |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1145 | 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] | 1146 | { |
| 1147 | int disks = sh->disks; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1148 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1149 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1150 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1151 | (unsigned long long)sh->sector); |
| 1152 | |
| 1153 | for (i = disks; i--; ) { |
| 1154 | struct r5dev *dev = &sh->dev[i]; |
| 1155 | struct bio *chosen; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1156 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1157 | if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1158 | struct bio *wbi; |
| 1159 | |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 1160 | spin_lock_irq(&sh->raid_conf->device_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1161 | chosen = dev->towrite; |
| 1162 | dev->towrite = NULL; |
| 1163 | BUG_ON(dev->written); |
| 1164 | wbi = dev->written = chosen; |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 1165 | spin_unlock_irq(&sh->raid_conf->device_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1166 | |
| 1167 | while (wbi && wbi->bi_sector < |
| 1168 | dev->sector + STRIPE_SECTORS) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1169 | if (wbi->bi_rw & REQ_FUA) |
| 1170 | set_bit(R5_WantFUA, &dev->flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1171 | if (wbi->bi_rw & REQ_SYNC) |
| 1172 | set_bit(R5_SyncIO, &dev->flags); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1173 | tx = async_copy_data(1, wbi, dev->page, |
| 1174 | dev->sector, tx); |
| 1175 | wbi = r5_next_bio(wbi, dev->sector); |
| 1176 | } |
| 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | return tx; |
| 1181 | } |
| 1182 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1183 | static void ops_complete_reconstruct(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1184 | { |
| 1185 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1186 | int disks = sh->disks; |
| 1187 | int pd_idx = sh->pd_idx; |
| 1188 | int qd_idx = sh->qd_idx; |
| 1189 | int i; |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1190 | bool fua = false, sync = false; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1191 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1192 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1193 | (unsigned long long)sh->sector); |
| 1194 | |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1195 | for (i = disks; i--; ) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1196 | fua |= test_bit(R5_WantFUA, &sh->dev[i].flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1197 | sync |= test_bit(R5_SyncIO, &sh->dev[i].flags); |
| 1198 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1199 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1200 | for (i = disks; i--; ) { |
| 1201 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1202 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1203 | if (dev->written || i == pd_idx || i == qd_idx) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1204 | set_bit(R5_UPTODATE, &dev->flags); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1205 | if (fua) |
| 1206 | set_bit(R5_WantFUA, &dev->flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1207 | if (sync) |
| 1208 | set_bit(R5_SyncIO, &dev->flags); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1209 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1210 | } |
| 1211 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1212 | if (sh->reconstruct_state == reconstruct_state_drain_run) |
| 1213 | sh->reconstruct_state = reconstruct_state_drain_result; |
| 1214 | else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) |
| 1215 | sh->reconstruct_state = reconstruct_state_prexor_drain_result; |
| 1216 | else { |
| 1217 | BUG_ON(sh->reconstruct_state != reconstruct_state_run); |
| 1218 | sh->reconstruct_state = reconstruct_state_result; |
| 1219 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1220 | |
| 1221 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1222 | release_stripe(sh); |
| 1223 | } |
| 1224 | |
| 1225 | static void |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1226 | ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1227 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1228 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1229 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1230 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1231 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1232 | int count = 0, pd_idx = sh->pd_idx, i; |
| 1233 | struct page *xor_dest; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1234 | int prexor = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1235 | unsigned long flags; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1236 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1237 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1238 | (unsigned long long)sh->sector); |
| 1239 | |
| 1240 | /* check if prexor is active which means only process blocks |
| 1241 | * that are part of a read-modify-write (written) |
| 1242 | */ |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1243 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) { |
| 1244 | prexor = 1; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1245 | xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 1246 | for (i = disks; i--; ) { |
| 1247 | struct r5dev *dev = &sh->dev[i]; |
| 1248 | if (dev->written) |
| 1249 | xor_srcs[count++] = dev->page; |
| 1250 | } |
| 1251 | } else { |
| 1252 | xor_dest = sh->dev[pd_idx].page; |
| 1253 | for (i = disks; i--; ) { |
| 1254 | struct r5dev *dev = &sh->dev[i]; |
| 1255 | if (i != pd_idx) |
| 1256 | xor_srcs[count++] = dev->page; |
| 1257 | } |
| 1258 | } |
| 1259 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1260 | /* 1/ if we prexor'd then the dest is reused as a source |
| 1261 | * 2/ if we did not prexor then we are redoing the parity |
| 1262 | * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST |
| 1263 | * for the synchronous xor case |
| 1264 | */ |
Dan Williams | 88ba2aa | 2009-04-09 16:16:18 -0700 | [diff] [blame] | 1265 | flags = ASYNC_TX_ACK | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1266 | (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST); |
| 1267 | |
| 1268 | atomic_inc(&sh->count); |
| 1269 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1270 | init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh, |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1271 | to_addr_conv(sh, percpu)); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1272 | if (unlikely(count == 1)) |
| 1273 | tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit); |
| 1274 | else |
| 1275 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1276 | } |
| 1277 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1278 | static void |
| 1279 | ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1280 | struct dma_async_tx_descriptor *tx) |
| 1281 | { |
| 1282 | struct async_submit_ctl submit; |
| 1283 | struct page **blocks = percpu->scribble; |
| 1284 | int count; |
| 1285 | |
| 1286 | pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector); |
| 1287 | |
| 1288 | count = set_syndrome_sources(blocks, sh); |
| 1289 | |
| 1290 | atomic_inc(&sh->count); |
| 1291 | |
| 1292 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct, |
| 1293 | sh, to_addr_conv(sh, percpu)); |
| 1294 | async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | static void ops_complete_check(void *stripe_head_ref) |
| 1298 | { |
| 1299 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1300 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1301 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1302 | (unsigned long long)sh->sector); |
| 1303 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 1304 | sh->check_state = check_state_check_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1305 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1306 | release_stripe(sh); |
| 1307 | } |
| 1308 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1309 | static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1310 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1311 | int disks = sh->disks; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1312 | int pd_idx = sh->pd_idx; |
| 1313 | int qd_idx = sh->qd_idx; |
| 1314 | struct page *xor_dest; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1315 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1316 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1317 | struct async_submit_ctl submit; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1318 | int count; |
| 1319 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1320 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1321 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1322 | (unsigned long long)sh->sector); |
| 1323 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1324 | count = 0; |
| 1325 | xor_dest = sh->dev[pd_idx].page; |
| 1326 | xor_srcs[count++] = xor_dest; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1327 | for (i = disks; i--; ) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1328 | if (i == pd_idx || i == qd_idx) |
| 1329 | continue; |
| 1330 | xor_srcs[count++] = sh->dev[i].page; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1331 | } |
| 1332 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1333 | init_async_submit(&submit, 0, NULL, NULL, NULL, |
| 1334 | to_addr_conv(sh, percpu)); |
Dan Williams | 099f53c | 2009-04-08 14:28:37 -0700 | [diff] [blame] | 1335 | tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1336 | &sh->ops.zero_sum_result, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1337 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1338 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1339 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL); |
| 1340 | tx = async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1341 | } |
| 1342 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1343 | static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp) |
| 1344 | { |
| 1345 | struct page **srcs = percpu->scribble; |
| 1346 | struct async_submit_ctl submit; |
| 1347 | int count; |
| 1348 | |
| 1349 | pr_debug("%s: stripe %llu checkp: %d\n", __func__, |
| 1350 | (unsigned long long)sh->sector, checkp); |
| 1351 | |
| 1352 | count = set_syndrome_sources(srcs, sh); |
| 1353 | if (!checkp) |
| 1354 | srcs[count] = NULL; |
| 1355 | |
| 1356 | atomic_inc(&sh->count); |
| 1357 | init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check, |
| 1358 | sh, to_addr_conv(sh, percpu)); |
| 1359 | async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE, |
| 1360 | &sh->ops.zero_sum_result, percpu->spare_page, &submit); |
| 1361 | } |
| 1362 | |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 1363 | static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1364 | { |
| 1365 | int overlap_clear = 0, i, disks = sh->disks; |
| 1366 | struct dma_async_tx_descriptor *tx = NULL; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1367 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1368 | int level = conf->level; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1369 | struct raid5_percpu *percpu; |
| 1370 | unsigned long cpu; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1371 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1372 | cpu = get_cpu(); |
| 1373 | percpu = per_cpu_ptr(conf->percpu, cpu); |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 1374 | if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1375 | ops_run_biofill(sh); |
| 1376 | overlap_clear++; |
| 1377 | } |
| 1378 | |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 1379 | if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1380 | if (level < 6) |
| 1381 | tx = ops_run_compute5(sh, percpu); |
| 1382 | else { |
| 1383 | if (sh->ops.target2 < 0 || sh->ops.target < 0) |
| 1384 | tx = ops_run_compute6_1(sh, percpu); |
| 1385 | else |
| 1386 | tx = ops_run_compute6_2(sh, percpu); |
| 1387 | } |
| 1388 | /* terminate the chain if reconstruct is not set to be run */ |
| 1389 | if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 1390 | async_tx_ack(tx); |
| 1391 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1392 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1393 | if (test_bit(STRIPE_OP_PREXOR, &ops_request)) |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1394 | tx = ops_run_prexor(sh, percpu, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1395 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1396 | if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) { |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1397 | tx = ops_run_biodrain(sh, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1398 | overlap_clear++; |
| 1399 | } |
| 1400 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1401 | if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) { |
| 1402 | if (level < 6) |
| 1403 | ops_run_reconstruct5(sh, percpu, tx); |
| 1404 | else |
| 1405 | ops_run_reconstruct6(sh, percpu, tx); |
| 1406 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1407 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1408 | if (test_bit(STRIPE_OP_CHECK, &ops_request)) { |
| 1409 | if (sh->check_state == check_state_run) |
| 1410 | ops_run_check_p(sh, percpu); |
| 1411 | else if (sh->check_state == check_state_run_q) |
| 1412 | ops_run_check_pq(sh, percpu, 0); |
| 1413 | else if (sh->check_state == check_state_run_pq) |
| 1414 | ops_run_check_pq(sh, percpu, 1); |
| 1415 | else |
| 1416 | BUG(); |
| 1417 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1418 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1419 | if (overlap_clear) |
| 1420 | for (i = disks; i--; ) { |
| 1421 | struct r5dev *dev = &sh->dev[i]; |
| 1422 | if (test_and_clear_bit(R5_Overlap, &dev->flags)) |
| 1423 | wake_up(&sh->raid_conf->wait_for_overlap); |
| 1424 | } |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1425 | put_cpu(); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1426 | } |
| 1427 | |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 1428 | #ifdef CONFIG_MULTICORE_RAID456 |
| 1429 | static void async_run_ops(void *param, async_cookie_t cookie) |
| 1430 | { |
| 1431 | struct stripe_head *sh = param; |
| 1432 | unsigned long ops_request = sh->ops.request; |
| 1433 | |
| 1434 | clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state); |
| 1435 | wake_up(&sh->ops.wait_for_ops); |
| 1436 | |
| 1437 | __raid_run_ops(sh, ops_request); |
| 1438 | release_stripe(sh); |
| 1439 | } |
| 1440 | |
| 1441 | static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request) |
| 1442 | { |
| 1443 | /* since handle_stripe can be called outside of raid5d context |
| 1444 | * we need to ensure sh->ops.request is de-staged before another |
| 1445 | * request arrives |
| 1446 | */ |
| 1447 | wait_event(sh->ops.wait_for_ops, |
| 1448 | !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state)); |
| 1449 | sh->ops.request = ops_request; |
| 1450 | |
| 1451 | atomic_inc(&sh->count); |
| 1452 | async_schedule(async_run_ops, sh); |
| 1453 | } |
| 1454 | #else |
| 1455 | #define raid_run_ops __raid_run_ops |
| 1456 | #endif |
| 1457 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1458 | static int grow_one_stripe(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | { |
| 1460 | struct stripe_head *sh; |
Namhyung Kim | 6ce3284 | 2011-07-18 17:38:50 +1000 | [diff] [blame] | 1461 | sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1462 | if (!sh) |
| 1463 | return 0; |
Namhyung Kim | 6ce3284 | 2011-07-18 17:38:50 +1000 | [diff] [blame] | 1464 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1465 | sh->raid_conf = conf; |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 1466 | #ifdef CONFIG_MULTICORE_RAID456 |
| 1467 | init_waitqueue_head(&sh->ops.wait_for_ops); |
| 1468 | #endif |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1469 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 1470 | if (grow_buffers(sh)) { |
| 1471 | shrink_buffers(sh); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1472 | kmem_cache_free(conf->slab_cache, sh); |
| 1473 | return 0; |
| 1474 | } |
| 1475 | /* we just created an active stripe so... */ |
| 1476 | atomic_set(&sh->count, 1); |
| 1477 | atomic_inc(&conf->active_stripes); |
| 1478 | INIT_LIST_HEAD(&sh->lru); |
| 1479 | release_stripe(sh); |
| 1480 | return 1; |
| 1481 | } |
| 1482 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1483 | static int grow_stripes(struct r5conf *conf, int num) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1484 | { |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 1485 | struct kmem_cache *sc; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 1486 | int devs = max(conf->raid_disks, conf->previous_raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | |
NeilBrown | f4be6b4 | 2010-06-01 19:37:25 +1000 | [diff] [blame] | 1488 | if (conf->mddev->gendisk) |
| 1489 | sprintf(conf->cache_name[0], |
| 1490 | "raid%d-%s", conf->level, mdname(conf->mddev)); |
| 1491 | else |
| 1492 | sprintf(conf->cache_name[0], |
| 1493 | "raid%d-%p", conf->level, conf->mddev); |
| 1494 | sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]); |
| 1495 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1496 | conf->active_name = 0; |
| 1497 | sc = kmem_cache_create(conf->cache_name[conf->active_name], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1499 | 0, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 | if (!sc) |
| 1501 | return 1; |
| 1502 | conf->slab_cache = sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1503 | conf->pool_size = devs; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1504 | while (num--) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1505 | if (!grow_one_stripe(conf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | return 0; |
| 1508 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 1509 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1510 | /** |
| 1511 | * scribble_len - return the required size of the scribble region |
| 1512 | * @num - total number of disks in the array |
| 1513 | * |
| 1514 | * The size must be enough to contain: |
| 1515 | * 1/ a struct page pointer for each device in the array +2 |
| 1516 | * 2/ room to convert each entry in (1) to its corresponding dma |
| 1517 | * (dma_map_page()) or page (page_address()) address. |
| 1518 | * |
| 1519 | * Note: the +2 is for the destination buffers of the ddf/raid6 case where we |
| 1520 | * calculate over all devices (not just the data blocks), using zeros in place |
| 1521 | * of the P and Q blocks. |
| 1522 | */ |
| 1523 | static size_t scribble_len(int num) |
| 1524 | { |
| 1525 | size_t len; |
| 1526 | |
| 1527 | len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2); |
| 1528 | |
| 1529 | return len; |
| 1530 | } |
| 1531 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1532 | static int resize_stripes(struct r5conf *conf, int newsize) |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1533 | { |
| 1534 | /* Make all the stripes able to hold 'newsize' devices. |
| 1535 | * New slots in each stripe get 'page' set to a new page. |
| 1536 | * |
| 1537 | * This happens in stages: |
| 1538 | * 1/ create a new kmem_cache and allocate the required number of |
| 1539 | * stripe_heads. |
| 1540 | * 2/ gather all the old stripe_heads and tranfer the pages across |
| 1541 | * to the new stripe_heads. This will have the side effect of |
| 1542 | * freezing the array as once all stripe_heads have been collected, |
| 1543 | * no IO will be possible. Old stripe heads are freed once their |
| 1544 | * pages have been transferred over, and the old kmem_cache is |
| 1545 | * freed when all stripes are done. |
| 1546 | * 3/ reallocate conf->disks to be suitable bigger. If this fails, |
| 1547 | * we simple return a failre status - no need to clean anything up. |
| 1548 | * 4/ allocate new pages for the new slots in the new stripe_heads. |
| 1549 | * If this fails, we don't bother trying the shrink the |
| 1550 | * stripe_heads down again, we just leave them as they are. |
| 1551 | * As each stripe_head is processed the new one is released into |
| 1552 | * active service. |
| 1553 | * |
| 1554 | * Once step2 is started, we cannot afford to wait for a write, |
| 1555 | * so we use GFP_NOIO allocations. |
| 1556 | */ |
| 1557 | struct stripe_head *osh, *nsh; |
| 1558 | LIST_HEAD(newstripes); |
| 1559 | struct disk_info *ndisks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1560 | unsigned long cpu; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 1561 | int err; |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 1562 | struct kmem_cache *sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1563 | int i; |
| 1564 | |
| 1565 | if (newsize <= conf->pool_size) |
| 1566 | return 0; /* never bother to shrink */ |
| 1567 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 1568 | err = md_allow_write(conf->mddev); |
| 1569 | if (err) |
| 1570 | return err; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 1571 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1572 | /* Step 1 */ |
| 1573 | sc = kmem_cache_create(conf->cache_name[1-conf->active_name], |
| 1574 | sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1575 | 0, 0, NULL); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1576 | if (!sc) |
| 1577 | return -ENOMEM; |
| 1578 | |
| 1579 | for (i = conf->max_nr_stripes; i; i--) { |
Namhyung Kim | 6ce3284 | 2011-07-18 17:38:50 +1000 | [diff] [blame] | 1580 | nsh = kmem_cache_zalloc(sc, GFP_KERNEL); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1581 | if (!nsh) |
| 1582 | break; |
| 1583 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1584 | nsh->raid_conf = conf; |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 1585 | #ifdef CONFIG_MULTICORE_RAID456 |
| 1586 | init_waitqueue_head(&nsh->ops.wait_for_ops); |
| 1587 | #endif |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1588 | |
| 1589 | list_add(&nsh->lru, &newstripes); |
| 1590 | } |
| 1591 | if (i) { |
| 1592 | /* didn't get enough, give up */ |
| 1593 | while (!list_empty(&newstripes)) { |
| 1594 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 1595 | list_del(&nsh->lru); |
| 1596 | kmem_cache_free(sc, nsh); |
| 1597 | } |
| 1598 | kmem_cache_destroy(sc); |
| 1599 | return -ENOMEM; |
| 1600 | } |
| 1601 | /* Step 2 - Must use GFP_NOIO now. |
| 1602 | * OK, we have enough stripes, start collecting inactive |
| 1603 | * stripes and copying them over |
| 1604 | */ |
| 1605 | list_for_each_entry(nsh, &newstripes, lru) { |
| 1606 | spin_lock_irq(&conf->device_lock); |
| 1607 | wait_event_lock_irq(conf->wait_for_stripe, |
| 1608 | !list_empty(&conf->inactive_list), |
| 1609 | conf->device_lock, |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 1610 | ); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1611 | osh = get_free_stripe(conf); |
| 1612 | spin_unlock_irq(&conf->device_lock); |
| 1613 | atomic_set(&nsh->count, 1); |
| 1614 | for(i=0; i<conf->pool_size; i++) |
| 1615 | nsh->dev[i].page = osh->dev[i].page; |
| 1616 | for( ; i<newsize; i++) |
| 1617 | nsh->dev[i].page = NULL; |
| 1618 | kmem_cache_free(conf->slab_cache, osh); |
| 1619 | } |
| 1620 | kmem_cache_destroy(conf->slab_cache); |
| 1621 | |
| 1622 | /* Step 3. |
| 1623 | * At this point, we are holding all the stripes so the array |
| 1624 | * is completely stalled, so now is a good time to resize |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1625 | * conf->disks and the scribble region |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1626 | */ |
| 1627 | ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO); |
| 1628 | if (ndisks) { |
| 1629 | for (i=0; i<conf->raid_disks; i++) |
| 1630 | ndisks[i] = conf->disks[i]; |
| 1631 | kfree(conf->disks); |
| 1632 | conf->disks = ndisks; |
| 1633 | } else |
| 1634 | err = -ENOMEM; |
| 1635 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1636 | get_online_cpus(); |
| 1637 | conf->scribble_len = scribble_len(newsize); |
| 1638 | for_each_present_cpu(cpu) { |
| 1639 | struct raid5_percpu *percpu; |
| 1640 | void *scribble; |
| 1641 | |
| 1642 | percpu = per_cpu_ptr(conf->percpu, cpu); |
| 1643 | scribble = kmalloc(conf->scribble_len, GFP_NOIO); |
| 1644 | |
| 1645 | if (scribble) { |
| 1646 | kfree(percpu->scribble); |
| 1647 | percpu->scribble = scribble; |
| 1648 | } else { |
| 1649 | err = -ENOMEM; |
| 1650 | break; |
| 1651 | } |
| 1652 | } |
| 1653 | put_online_cpus(); |
| 1654 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1655 | /* Step 4, return new stripes to service */ |
| 1656 | while(!list_empty(&newstripes)) { |
| 1657 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 1658 | list_del_init(&nsh->lru); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1659 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1660 | for (i=conf->raid_disks; i < newsize; i++) |
| 1661 | if (nsh->dev[i].page == NULL) { |
| 1662 | struct page *p = alloc_page(GFP_NOIO); |
| 1663 | nsh->dev[i].page = p; |
| 1664 | if (!p) |
| 1665 | err = -ENOMEM; |
| 1666 | } |
| 1667 | release_stripe(nsh); |
| 1668 | } |
| 1669 | /* critical section pass, GFP_NOIO no longer needed */ |
| 1670 | |
| 1671 | conf->slab_cache = sc; |
| 1672 | conf->active_name = 1-conf->active_name; |
| 1673 | conf->pool_size = newsize; |
| 1674 | return err; |
| 1675 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1677 | static int drop_one_stripe(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | { |
| 1679 | struct stripe_head *sh; |
| 1680 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1681 | spin_lock_irq(&conf->device_lock); |
| 1682 | sh = get_free_stripe(conf); |
| 1683 | spin_unlock_irq(&conf->device_lock); |
| 1684 | if (!sh) |
| 1685 | return 0; |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 1686 | BUG_ON(atomic_read(&sh->count)); |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 1687 | shrink_buffers(sh); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1688 | kmem_cache_free(conf->slab_cache, sh); |
| 1689 | atomic_dec(&conf->active_stripes); |
| 1690 | return 1; |
| 1691 | } |
| 1692 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1693 | static void shrink_stripes(struct r5conf *conf) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1694 | { |
| 1695 | while (drop_one_stripe(conf)) |
| 1696 | ; |
| 1697 | |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1698 | if (conf->slab_cache) |
| 1699 | kmem_cache_destroy(conf->slab_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | conf->slab_cache = NULL; |
| 1701 | } |
| 1702 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1703 | static void raid5_end_read_request(struct bio * bi, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1705 | struct stripe_head *sh = bi->bi_private; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1706 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 1707 | int disks = sh->disks, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1709 | char b[BDEVNAME_SIZE]; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1710 | struct md_rdev *rdev = NULL; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1711 | sector_t s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | |
| 1713 | for (i=0 ; i<disks; i++) |
| 1714 | if (bi == &sh->dev[i].req) |
| 1715 | break; |
| 1716 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1717 | pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n", |
| 1718 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | uptodate); |
| 1720 | if (i == disks) { |
| 1721 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1722 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | } |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1724 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1725 | /* If replacement finished while this request was outstanding, |
| 1726 | * 'replacement' might be NULL already. |
| 1727 | * In that case it moved down to 'rdev'. |
| 1728 | * rdev is not removed until all requests are finished. |
| 1729 | */ |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1730 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1731 | if (!rdev) |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1732 | rdev = conf->disks[i].rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1734 | if (use_new_offset(conf, sh)) |
| 1735 | s = sh->sector + rdev->new_data_offset; |
| 1736 | else |
| 1737 | s = sh->sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | if (uptodate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | set_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1740 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1741 | /* Note that this cannot happen on a |
| 1742 | * replacement device. We just fail those on |
| 1743 | * any error |
| 1744 | */ |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1745 | printk_ratelimited( |
| 1746 | KERN_INFO |
| 1747 | "md/raid:%s: read error corrected" |
| 1748 | " (%lu sectors at %llu on %s)\n", |
| 1749 | mdname(conf->mddev), STRIPE_SECTORS, |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1750 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1751 | bdevname(rdev->bdev, b)); |
Namhyung Kim | ddd5115 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1752 | atomic_add(STRIPE_SECTORS, &rdev->corrected_errors); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1753 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 1754 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
| 1755 | } |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1756 | if (atomic_read(&rdev->read_errors)) |
| 1757 | atomic_set(&rdev->read_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | } else { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1759 | const char *bdn = bdevname(rdev->bdev, b); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1760 | int retry = 0; |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1761 | int set_bad = 0; |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1762 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | clear_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1764 | atomic_inc(&rdev->read_errors); |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1765 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) |
| 1766 | printk_ratelimited( |
| 1767 | KERN_WARNING |
| 1768 | "md/raid:%s: read error on replacement device " |
| 1769 | "(sector %llu on %s).\n", |
| 1770 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1771 | (unsigned long long)s, |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1772 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1773 | else if (conf->mddev->degraded >= conf->max_degraded) { |
| 1774 | set_bad = 1; |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1775 | printk_ratelimited( |
| 1776 | KERN_WARNING |
| 1777 | "md/raid:%s: read error not correctable " |
| 1778 | "(sector %llu on %s).\n", |
| 1779 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1780 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1781 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1782 | } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) { |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1783 | /* Oh, no!!! */ |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1784 | set_bad = 1; |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1785 | printk_ratelimited( |
| 1786 | KERN_WARNING |
| 1787 | "md/raid:%s: read error NOT corrected!! " |
| 1788 | "(sector %llu on %s).\n", |
| 1789 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1790 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1791 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1792 | } else if (atomic_read(&rdev->read_errors) |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1793 | > conf->max_nr_stripes) |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 1794 | printk(KERN_WARNING |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 1795 | "md/raid:%s: Too many read errors, failing device %s.\n", |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1796 | mdname(conf->mddev), bdn); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1797 | else |
| 1798 | retry = 1; |
| 1799 | if (retry) |
| 1800 | set_bit(R5_ReadError, &sh->dev[i].flags); |
| 1801 | else { |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1802 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 1803 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1804 | if (!(set_bad |
| 1805 | && test_bit(In_sync, &rdev->flags) |
| 1806 | && rdev_set_badblocks( |
| 1807 | rdev, sh->sector, STRIPE_SECTORS, 0))) |
| 1808 | md_error(conf->mddev, rdev); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1809 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | } |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1811 | rdev_dec_pending(rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 1813 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1814 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | } |
| 1816 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 1817 | static void raid5_end_write_request(struct bio *bi, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1819 | struct stripe_head *sh = bi->bi_private; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1820 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 1821 | int disks = sh->disks, i; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1822 | struct md_rdev *uninitialized_var(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 1824 | sector_t first_bad; |
| 1825 | int bad_sectors; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1826 | int replacement = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1828 | for (i = 0 ; i < disks; i++) { |
| 1829 | if (bi == &sh->dev[i].req) { |
| 1830 | rdev = conf->disks[i].rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | break; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1832 | } |
| 1833 | if (bi == &sh->dev[i].rreq) { |
| 1834 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1835 | if (rdev) |
| 1836 | replacement = 1; |
| 1837 | else |
| 1838 | /* rdev was removed and 'replacement' |
| 1839 | * replaced it. rdev is not removed |
| 1840 | * until all requests are finished. |
| 1841 | */ |
| 1842 | rdev = conf->disks[i].rdev; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1843 | break; |
| 1844 | } |
| 1845 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1846 | pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
| 1848 | uptodate); |
| 1849 | if (i == disks) { |
| 1850 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1851 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | } |
| 1853 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1854 | if (replacement) { |
| 1855 | if (!uptodate) |
| 1856 | md_error(conf->mddev, rdev); |
| 1857 | else if (is_badblock(rdev, sh->sector, |
| 1858 | STRIPE_SECTORS, |
| 1859 | &first_bad, &bad_sectors)) |
| 1860 | set_bit(R5_MadeGoodRepl, &sh->dev[i].flags); |
| 1861 | } else { |
| 1862 | if (!uptodate) { |
| 1863 | set_bit(WriteErrorSeen, &rdev->flags); |
| 1864 | set_bit(R5_WriteError, &sh->dev[i].flags); |
NeilBrown | 3a6de29 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 1865 | if (!test_and_set_bit(WantReplacement, &rdev->flags)) |
| 1866 | set_bit(MD_RECOVERY_NEEDED, |
| 1867 | &rdev->mddev->recovery); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1868 | } else if (is_badblock(rdev, sh->sector, |
| 1869 | STRIPE_SECTORS, |
| 1870 | &first_bad, &bad_sectors)) |
| 1871 | set_bit(R5_MadeGood, &sh->dev[i].flags); |
| 1872 | } |
| 1873 | rdev_dec_pending(rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1874 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1875 | if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags)) |
| 1876 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1877 | set_bit(STRIPE_HANDLE, &sh->state); |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1878 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1879 | } |
| 1880 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1881 | static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1883 | static void raid5_build_block(struct stripe_head *sh, int i, int previous) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | { |
| 1885 | struct r5dev *dev = &sh->dev[i]; |
| 1886 | |
| 1887 | bio_init(&dev->req); |
| 1888 | dev->req.bi_io_vec = &dev->vec; |
| 1889 | dev->req.bi_vcnt++; |
| 1890 | dev->req.bi_max_vecs++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1891 | dev->req.bi_private = sh; |
NeilBrown | 995c427 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1892 | dev->vec.bv_page = dev->page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1894 | bio_init(&dev->rreq); |
| 1895 | dev->rreq.bi_io_vec = &dev->rvec; |
| 1896 | dev->rreq.bi_vcnt++; |
| 1897 | dev->rreq.bi_max_vecs++; |
| 1898 | dev->rreq.bi_private = sh; |
| 1899 | dev->rvec.bv_page = dev->page; |
| 1900 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | dev->flags = 0; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1902 | dev->sector = compute_blocknr(sh, i, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1903 | } |
| 1904 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1905 | static void error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1906 | { |
| 1907 | char b[BDEVNAME_SIZE]; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1908 | struct r5conf *conf = mddev->private; |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 1909 | unsigned long flags; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 1910 | pr_debug("raid456: error called\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 1912 | spin_lock_irqsave(&conf->device_lock, flags); |
| 1913 | clear_bit(In_sync, &rdev->flags); |
| 1914 | mddev->degraded = calc_degraded(conf); |
| 1915 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 1916 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 1917 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 1918 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 6f8d0c7 | 2011-05-11 14:38:44 +1000 | [diff] [blame] | 1919 | set_bit(Faulty, &rdev->flags); |
| 1920 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 1921 | printk(KERN_ALERT |
| 1922 | "md/raid:%s: Disk failure on %s, disabling device.\n" |
| 1923 | "md/raid:%s: Operation continuing on %d devices.\n", |
| 1924 | mdname(mddev), |
| 1925 | bdevname(rdev->bdev, b), |
| 1926 | mdname(mddev), |
| 1927 | conf->raid_disks - mddev->degraded); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1928 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1929 | |
| 1930 | /* |
| 1931 | * Input: a 'big' sector number, |
| 1932 | * Output: index of the data and parity disk, and the sector # in them. |
| 1933 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1934 | static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1935 | int previous, int *dd_idx, |
| 1936 | struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1937 | { |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1938 | sector_t stripe, stripe2; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 1939 | sector_t chunk_number; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1940 | unsigned int chunk_offset; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1941 | int pd_idx, qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1942 | int ddf_layout = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1943 | sector_t new_sector; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1944 | int algorithm = previous ? conf->prev_algo |
| 1945 | : conf->algorithm; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 1946 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
| 1947 | : conf->chunk_sectors; |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1948 | int raid_disks = previous ? conf->previous_raid_disks |
| 1949 | : conf->raid_disks; |
| 1950 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1951 | |
| 1952 | /* First compute the information on this sector */ |
| 1953 | |
| 1954 | /* |
| 1955 | * Compute the chunk number and the sector offset inside the chunk |
| 1956 | */ |
| 1957 | chunk_offset = sector_div(r_sector, sectors_per_chunk); |
| 1958 | chunk_number = r_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1959 | |
| 1960 | /* |
| 1961 | * Compute the stripe number |
| 1962 | */ |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 1963 | stripe = chunk_number; |
| 1964 | *dd_idx = sector_div(stripe, data_disks); |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1965 | stripe2 = stripe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1966 | /* |
| 1967 | * Select the parity disk based on the user selected algorithm. |
| 1968 | */ |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1969 | pd_idx = qd_idx = -1; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1970 | switch(conf->level) { |
| 1971 | case 4: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1972 | pd_idx = data_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1973 | break; |
| 1974 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1975 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1977 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1978 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | (*dd_idx)++; |
| 1980 | break; |
| 1981 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1982 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1983 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | (*dd_idx)++; |
| 1985 | break; |
| 1986 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1987 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1988 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | break; |
| 1990 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1991 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1992 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1993 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1994 | case ALGORITHM_PARITY_0: |
| 1995 | pd_idx = 0; |
| 1996 | (*dd_idx)++; |
| 1997 | break; |
| 1998 | case ALGORITHM_PARITY_N: |
| 1999 | pd_idx = data_disks; |
| 2000 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2002 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2003 | } |
| 2004 | break; |
| 2005 | case 6: |
| 2006 | |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2007 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2008 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2009 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2010 | qd_idx = pd_idx + 1; |
| 2011 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2012 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2013 | qd_idx = 0; |
| 2014 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2015 | (*dd_idx) += 2; /* D D P Q D */ |
| 2016 | break; |
| 2017 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2018 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2019 | qd_idx = pd_idx + 1; |
| 2020 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2021 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2022 | qd_idx = 0; |
| 2023 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2024 | (*dd_idx) += 2; /* D D P Q D */ |
| 2025 | break; |
| 2026 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2027 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2028 | qd_idx = (pd_idx + 1) % raid_disks; |
| 2029 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2030 | break; |
| 2031 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2032 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2033 | qd_idx = (pd_idx + 1) % raid_disks; |
| 2034 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2035 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2036 | |
| 2037 | case ALGORITHM_PARITY_0: |
| 2038 | pd_idx = 0; |
| 2039 | qd_idx = 1; |
| 2040 | (*dd_idx) += 2; |
| 2041 | break; |
| 2042 | case ALGORITHM_PARITY_N: |
| 2043 | pd_idx = data_disks; |
| 2044 | qd_idx = data_disks + 1; |
| 2045 | break; |
| 2046 | |
| 2047 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 2048 | /* Exactly the same as RIGHT_ASYMMETRIC, but or |
| 2049 | * of blocks for computing Q is different. |
| 2050 | */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2051 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2052 | qd_idx = pd_idx + 1; |
| 2053 | if (pd_idx == raid_disks-1) { |
| 2054 | (*dd_idx)++; /* Q D D D P */ |
| 2055 | qd_idx = 0; |
| 2056 | } else if (*dd_idx >= pd_idx) |
| 2057 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2058 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2059 | break; |
| 2060 | |
| 2061 | case ALGORITHM_ROTATING_N_RESTART: |
| 2062 | /* Same a left_asymmetric, by first stripe is |
| 2063 | * D D D P Q rather than |
| 2064 | * Q D D D P |
| 2065 | */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2066 | stripe2 += 1; |
| 2067 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2068 | qd_idx = pd_idx + 1; |
| 2069 | if (pd_idx == raid_disks-1) { |
| 2070 | (*dd_idx)++; /* Q D D D P */ |
| 2071 | qd_idx = 0; |
| 2072 | } else if (*dd_idx >= pd_idx) |
| 2073 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2074 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2075 | break; |
| 2076 | |
| 2077 | case ALGORITHM_ROTATING_N_CONTINUE: |
| 2078 | /* Same as left_symmetric but Q is before P */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2079 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2080 | qd_idx = (pd_idx + raid_disks - 1) % raid_disks; |
| 2081 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2082 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2083 | break; |
| 2084 | |
| 2085 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 2086 | /* RAID5 left_asymmetric, with Q on last device */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2087 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2088 | if (*dd_idx >= pd_idx) |
| 2089 | (*dd_idx)++; |
| 2090 | qd_idx = raid_disks - 1; |
| 2091 | break; |
| 2092 | |
| 2093 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2094 | pd_idx = sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2095 | if (*dd_idx >= pd_idx) |
| 2096 | (*dd_idx)++; |
| 2097 | qd_idx = raid_disks - 1; |
| 2098 | break; |
| 2099 | |
| 2100 | case ALGORITHM_LEFT_SYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2101 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2102 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 2103 | qd_idx = raid_disks - 1; |
| 2104 | break; |
| 2105 | |
| 2106 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2107 | pd_idx = sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2108 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 2109 | qd_idx = raid_disks - 1; |
| 2110 | break; |
| 2111 | |
| 2112 | case ALGORITHM_PARITY_0_6: |
| 2113 | pd_idx = 0; |
| 2114 | (*dd_idx)++; |
| 2115 | qd_idx = raid_disks - 1; |
| 2116 | break; |
| 2117 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2118 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2119 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2120 | } |
| 2121 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | } |
| 2123 | |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2124 | if (sh) { |
| 2125 | sh->pd_idx = pd_idx; |
| 2126 | sh->qd_idx = qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2127 | sh->ddf_layout = ddf_layout; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2128 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | /* |
| 2130 | * Finally, compute the new sector number |
| 2131 | */ |
| 2132 | new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset; |
| 2133 | return new_sector; |
| 2134 | } |
| 2135 | |
| 2136 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2137 | static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2138 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2139 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 2140 | int raid_disks = sh->disks; |
| 2141 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | sector_t new_sector = sh->sector, check; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 2143 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
| 2144 | : conf->chunk_sectors; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2145 | int algorithm = previous ? conf->prev_algo |
| 2146 | : conf->algorithm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | sector_t stripe; |
| 2148 | int chunk_offset; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2149 | sector_t chunk_number; |
| 2150 | int dummy1, dd_idx = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2151 | sector_t r_sector; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2152 | struct stripe_head sh2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2153 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2154 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2155 | chunk_offset = sector_div(new_sector, sectors_per_chunk); |
| 2156 | stripe = new_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2158 | if (i == sh->pd_idx) |
| 2159 | return 0; |
| 2160 | switch(conf->level) { |
| 2161 | case 4: break; |
| 2162 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2163 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 2165 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 2166 | if (i > sh->pd_idx) |
| 2167 | i--; |
| 2168 | break; |
| 2169 | case ALGORITHM_LEFT_SYMMETRIC: |
| 2170 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 2171 | if (i < sh->pd_idx) |
| 2172 | i += raid_disks; |
| 2173 | i -= (sh->pd_idx + 1); |
| 2174 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2175 | case ALGORITHM_PARITY_0: |
| 2176 | i -= 1; |
| 2177 | break; |
| 2178 | case ALGORITHM_PARITY_N: |
| 2179 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2180 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2181 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2182 | } |
| 2183 | break; |
| 2184 | case 6: |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2185 | if (i == sh->qd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2186 | return 0; /* It is the Q disk */ |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2187 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2188 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 2189 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2190 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 2191 | case ALGORITHM_ROTATING_N_RESTART: |
| 2192 | if (sh->pd_idx == raid_disks-1) |
| 2193 | i--; /* Q D D D P */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2194 | else if (i > sh->pd_idx) |
| 2195 | i -= 2; /* D D P Q D */ |
| 2196 | break; |
| 2197 | case ALGORITHM_LEFT_SYMMETRIC: |
| 2198 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 2199 | if (sh->pd_idx == raid_disks-1) |
| 2200 | i--; /* Q D D D P */ |
| 2201 | else { |
| 2202 | /* D D P Q D */ |
| 2203 | if (i < sh->pd_idx) |
| 2204 | i += raid_disks; |
| 2205 | i -= (sh->pd_idx + 2); |
| 2206 | } |
| 2207 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2208 | case ALGORITHM_PARITY_0: |
| 2209 | i -= 2; |
| 2210 | break; |
| 2211 | case ALGORITHM_PARITY_N: |
| 2212 | break; |
| 2213 | case ALGORITHM_ROTATING_N_CONTINUE: |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 2214 | /* Like left_symmetric, but P is before Q */ |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2215 | if (sh->pd_idx == 0) |
| 2216 | i--; /* P D D D Q */ |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 2217 | else { |
| 2218 | /* D D Q P D */ |
| 2219 | if (i < sh->pd_idx) |
| 2220 | i += raid_disks; |
| 2221 | i -= (sh->pd_idx + 1); |
| 2222 | } |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2223 | break; |
| 2224 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 2225 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 2226 | if (i > sh->pd_idx) |
| 2227 | i--; |
| 2228 | break; |
| 2229 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 2230 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 2231 | if (i < sh->pd_idx) |
| 2232 | i += data_disks + 1; |
| 2233 | i -= (sh->pd_idx + 1); |
| 2234 | break; |
| 2235 | case ALGORITHM_PARITY_0_6: |
| 2236 | i -= 1; |
| 2237 | break; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2238 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2239 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2240 | } |
| 2241 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2242 | } |
| 2243 | |
| 2244 | chunk_number = stripe * data_disks + i; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2245 | r_sector = chunk_number * sectors_per_chunk + chunk_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2247 | check = raid5_compute_sector(conf, r_sector, |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2248 | previous, &dummy1, &sh2); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2249 | if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx |
| 2250 | || sh2.qd_idx != sh->qd_idx) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 2251 | printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n", |
| 2252 | mdname(conf->mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2253 | return 0; |
| 2254 | } |
| 2255 | return r_sector; |
| 2256 | } |
| 2257 | |
| 2258 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2259 | static void |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2260 | schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2261 | int rcw, int expand) |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2262 | { |
| 2263 | int i, pd_idx = sh->pd_idx, disks = sh->disks; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2264 | struct r5conf *conf = sh->raid_conf; |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2265 | int level = conf->level; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2266 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2267 | if (rcw) { |
| 2268 | /* if we are not expanding this is a proper write request, and |
| 2269 | * there will be bios with new data to be drained into the |
| 2270 | * stripe cache |
| 2271 | */ |
| 2272 | if (!expand) { |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2273 | sh->reconstruct_state = reconstruct_state_drain_run; |
| 2274 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
| 2275 | } else |
| 2276 | sh->reconstruct_state = reconstruct_state_run; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2277 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2278 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2279 | |
| 2280 | for (i = disks; i--; ) { |
| 2281 | struct r5dev *dev = &sh->dev[i]; |
| 2282 | |
| 2283 | if (dev->towrite) { |
| 2284 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2285 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2286 | if (!expand) |
| 2287 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2288 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2289 | } |
| 2290 | } |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2291 | if (s->locked + conf->max_degraded == disks) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2292 | if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state)) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2293 | atomic_inc(&conf->pending_full_writes); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2294 | } else { |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2295 | BUG_ON(level == 6); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2296 | BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) || |
| 2297 | test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags))); |
| 2298 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2299 | sh->reconstruct_state = reconstruct_state_prexor_drain_run; |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2300 | set_bit(STRIPE_OP_PREXOR, &s->ops_request); |
| 2301 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2302 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2303 | |
| 2304 | for (i = disks; i--; ) { |
| 2305 | struct r5dev *dev = &sh->dev[i]; |
| 2306 | if (i == pd_idx) |
| 2307 | continue; |
| 2308 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2309 | if (dev->towrite && |
| 2310 | (test_bit(R5_UPTODATE, &dev->flags) || |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2311 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2312 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2313 | set_bit(R5_LOCKED, &dev->flags); |
| 2314 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2315 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2316 | } |
| 2317 | } |
| 2318 | } |
| 2319 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2320 | /* keep the parity disk(s) locked while asynchronous operations |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2321 | * are in flight |
| 2322 | */ |
| 2323 | set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); |
| 2324 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2325 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2326 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2327 | if (level == 6) { |
| 2328 | int qd_idx = sh->qd_idx; |
| 2329 | struct r5dev *dev = &sh->dev[qd_idx]; |
| 2330 | |
| 2331 | set_bit(R5_LOCKED, &dev->flags); |
| 2332 | clear_bit(R5_UPTODATE, &dev->flags); |
| 2333 | s->locked++; |
| 2334 | } |
| 2335 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2336 | pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n", |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 2337 | __func__, (unsigned long long)sh->sector, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2338 | s->locked, s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2339 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2340 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2341 | /* |
| 2342 | * Each stripe/dev can have one or more bion attached. |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2343 | * toread/towrite point to the first in a chain. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2344 | * The bi_next chain must be in order. |
| 2345 | */ |
| 2346 | static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite) |
| 2347 | { |
| 2348 | struct bio **bip; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2349 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2350 | int firstwrite=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2351 | |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 2352 | pr_debug("adding bi b#%llu to stripe s#%llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2353 | (unsigned long long)bi->bi_sector, |
| 2354 | (unsigned long long)sh->sector); |
| 2355 | |
| 2356 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2357 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2358 | if (forwrite) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2359 | bip = &sh->dev[dd_idx].towrite; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2360 | if (*bip == NULL && sh->dev[dd_idx].written == NULL) |
| 2361 | firstwrite = 1; |
| 2362 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | bip = &sh->dev[dd_idx].toread; |
| 2364 | while (*bip && (*bip)->bi_sector < bi->bi_sector) { |
| 2365 | if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector) |
| 2366 | goto overlap; |
| 2367 | bip = & (*bip)->bi_next; |
| 2368 | } |
| 2369 | if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9)) |
| 2370 | goto overlap; |
| 2371 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 2372 | BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2373 | if (*bip) |
| 2374 | bi->bi_next = *bip; |
| 2375 | *bip = bi; |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame^] | 2376 | raid5_inc_bi_active_stripes(bi); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2377 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2378 | if (forwrite) { |
| 2379 | /* check if page is covered */ |
| 2380 | sector_t sector = sh->dev[dd_idx].sector; |
| 2381 | for (bi=sh->dev[dd_idx].towrite; |
| 2382 | sector < sh->dev[dd_idx].sector + STRIPE_SECTORS && |
| 2383 | bi && bi->bi_sector <= sector; |
| 2384 | bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) { |
| 2385 | if (bi->bi_sector + (bi->bi_size>>9) >= sector) |
| 2386 | sector = bi->bi_sector + (bi->bi_size>>9); |
| 2387 | } |
| 2388 | if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS) |
| 2389 | set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags); |
| 2390 | } |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 2391 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 2392 | |
| 2393 | pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n", |
| 2394 | (unsigned long long)(*bip)->bi_sector, |
| 2395 | (unsigned long long)sh->sector, dd_idx); |
| 2396 | |
| 2397 | if (conf->mddev->bitmap && firstwrite) { |
| 2398 | bitmap_startwrite(conf->mddev->bitmap, sh->sector, |
| 2399 | STRIPE_SECTORS, 0); |
| 2400 | sh->bm_seq = conf->seq_flush+1; |
| 2401 | set_bit(STRIPE_BIT_DELAY, &sh->state); |
| 2402 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2403 | return 1; |
| 2404 | |
| 2405 | overlap: |
| 2406 | set_bit(R5_Overlap, &sh->dev[dd_idx].flags); |
| 2407 | spin_unlock_irq(&conf->device_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2408 | return 0; |
| 2409 | } |
| 2410 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2411 | static void end_reshape(struct r5conf *conf); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 2412 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2413 | static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2414 | struct stripe_head *sh) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2415 | { |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2416 | int sectors_per_chunk = |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 2417 | previous ? conf->prev_chunk_sectors : conf->chunk_sectors; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2418 | int dd_idx; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 2419 | int chunk_offset = sector_div(stripe, sectors_per_chunk); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2420 | int disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 2421 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2422 | raid5_compute_sector(conf, |
| 2423 | stripe * (disks - conf->max_degraded) |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 2424 | *sectors_per_chunk + chunk_offset, |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2425 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2426 | &dd_idx, sh); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2427 | } |
| 2428 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2429 | static void |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2430 | handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2431 | struct stripe_head_state *s, int disks, |
| 2432 | struct bio **return_bi) |
| 2433 | { |
| 2434 | int i; |
| 2435 | for (i = disks; i--; ) { |
| 2436 | struct bio *bi; |
| 2437 | int bitmap_end = 0; |
| 2438 | |
| 2439 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2440 | struct md_rdev *rdev; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2441 | rcu_read_lock(); |
| 2442 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 2443 | if (rdev && test_bit(In_sync, &rdev->flags)) |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2444 | atomic_inc(&rdev->nr_pending); |
| 2445 | else |
| 2446 | rdev = NULL; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2447 | rcu_read_unlock(); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2448 | if (rdev) { |
| 2449 | if (!rdev_set_badblocks( |
| 2450 | rdev, |
| 2451 | sh->sector, |
| 2452 | STRIPE_SECTORS, 0)) |
| 2453 | md_error(conf->mddev, rdev); |
| 2454 | rdev_dec_pending(rdev, conf->mddev); |
| 2455 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2456 | } |
| 2457 | spin_lock_irq(&conf->device_lock); |
| 2458 | /* fail all writes first */ |
| 2459 | bi = sh->dev[i].towrite; |
| 2460 | sh->dev[i].towrite = NULL; |
| 2461 | if (bi) { |
| 2462 | s->to_write--; |
| 2463 | bitmap_end = 1; |
| 2464 | } |
| 2465 | |
| 2466 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 2467 | wake_up(&conf->wait_for_overlap); |
| 2468 | |
| 2469 | while (bi && bi->bi_sector < |
| 2470 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2471 | struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); |
| 2472 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame^] | 2473 | if (!raid5_dec_bi_active_stripes(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2474 | md_write_end(conf->mddev); |
| 2475 | bi->bi_next = *return_bi; |
| 2476 | *return_bi = bi; |
| 2477 | } |
| 2478 | bi = nextbi; |
| 2479 | } |
| 2480 | /* and fail all 'written' */ |
| 2481 | bi = sh->dev[i].written; |
| 2482 | sh->dev[i].written = NULL; |
| 2483 | if (bi) bitmap_end = 1; |
| 2484 | while (bi && bi->bi_sector < |
| 2485 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2486 | struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector); |
| 2487 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame^] | 2488 | if (!raid5_dec_bi_active_stripes(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2489 | md_write_end(conf->mddev); |
| 2490 | bi->bi_next = *return_bi; |
| 2491 | *return_bi = bi; |
| 2492 | } |
| 2493 | bi = bi2; |
| 2494 | } |
| 2495 | |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2496 | /* fail any reads if this device is non-operational and |
| 2497 | * the data has not reached the cache yet. |
| 2498 | */ |
| 2499 | if (!test_bit(R5_Wantfill, &sh->dev[i].flags) && |
| 2500 | (!test_bit(R5_Insync, &sh->dev[i].flags) || |
| 2501 | test_bit(R5_ReadError, &sh->dev[i].flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2502 | bi = sh->dev[i].toread; |
| 2503 | sh->dev[i].toread = NULL; |
| 2504 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 2505 | wake_up(&conf->wait_for_overlap); |
| 2506 | if (bi) s->to_read--; |
| 2507 | while (bi && bi->bi_sector < |
| 2508 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2509 | struct bio *nextbi = |
| 2510 | r5_next_bio(bi, sh->dev[i].sector); |
| 2511 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame^] | 2512 | if (!raid5_dec_bi_active_stripes(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2513 | bi->bi_next = *return_bi; |
| 2514 | *return_bi = bi; |
| 2515 | } |
| 2516 | bi = nextbi; |
| 2517 | } |
| 2518 | } |
| 2519 | spin_unlock_irq(&conf->device_lock); |
| 2520 | if (bitmap_end) |
| 2521 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 2522 | STRIPE_SECTORS, 0, 0); |
NeilBrown | 8cfa7b0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2523 | /* If we were in the middle of a write the parity block might |
| 2524 | * still be locked - so just clear all R5_LOCKED flags |
| 2525 | */ |
| 2526 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2527 | } |
| 2528 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2529 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 2530 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 2531 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2532 | } |
| 2533 | |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2534 | static void |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2535 | handle_failed_sync(struct r5conf *conf, struct stripe_head *sh, |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2536 | struct stripe_head_state *s) |
| 2537 | { |
| 2538 | int abort = 0; |
| 2539 | int i; |
| 2540 | |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2541 | clear_bit(STRIPE_SYNCING, &sh->state); |
| 2542 | s->syncing = 0; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2543 | s->replacing = 0; |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2544 | /* There is nothing more to do for sync/check/repair. |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 2545 | * Don't even need to abort as that is handled elsewhere |
| 2546 | * if needed, and not always wanted e.g. if there is a known |
| 2547 | * bad block here. |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2548 | * For recover/replace we need to record a bad block on all |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2549 | * non-sync devices, or abort the recovery |
| 2550 | */ |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 2551 | if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) { |
| 2552 | /* During recovery devices cannot be removed, so |
| 2553 | * locking and refcounting of rdevs is not needed |
| 2554 | */ |
| 2555 | for (i = 0; i < conf->raid_disks; i++) { |
| 2556 | struct md_rdev *rdev = conf->disks[i].rdev; |
| 2557 | if (rdev |
| 2558 | && !test_bit(Faulty, &rdev->flags) |
| 2559 | && !test_bit(In_sync, &rdev->flags) |
| 2560 | && !rdev_set_badblocks(rdev, sh->sector, |
| 2561 | STRIPE_SECTORS, 0)) |
| 2562 | abort = 1; |
| 2563 | rdev = conf->disks[i].replacement; |
| 2564 | if (rdev |
| 2565 | && !test_bit(Faulty, &rdev->flags) |
| 2566 | && !test_bit(In_sync, &rdev->flags) |
| 2567 | && !rdev_set_badblocks(rdev, sh->sector, |
| 2568 | STRIPE_SECTORS, 0)) |
| 2569 | abort = 1; |
| 2570 | } |
| 2571 | if (abort) |
| 2572 | conf->recovery_disabled = |
| 2573 | conf->mddev->recovery_disabled; |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2574 | } |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 2575 | md_done_sync(conf->mddev, STRIPE_SECTORS, !abort); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2576 | } |
| 2577 | |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2578 | static int want_replace(struct stripe_head *sh, int disk_idx) |
| 2579 | { |
| 2580 | struct md_rdev *rdev; |
| 2581 | int rv = 0; |
| 2582 | /* Doing recovery so rcu locking not required */ |
| 2583 | rdev = sh->raid_conf->disks[disk_idx].replacement; |
| 2584 | if (rdev |
| 2585 | && !test_bit(Faulty, &rdev->flags) |
| 2586 | && !test_bit(In_sync, &rdev->flags) |
| 2587 | && (rdev->recovery_offset <= sh->sector |
| 2588 | || rdev->mddev->recovery_cp <= sh->sector)) |
| 2589 | rv = 1; |
| 2590 | |
| 2591 | return rv; |
| 2592 | } |
| 2593 | |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2594 | /* fetch_block - checks the given member device to see if its data needs |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2595 | * to be read or computed to satisfy a request. |
| 2596 | * |
| 2597 | * Returns 1 when no more member devices need to be checked, otherwise returns |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2598 | * 0 to tell the loop in handle_stripe_fill to continue |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2599 | */ |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2600 | static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s, |
| 2601 | int disk_idx, int disks) |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2602 | { |
| 2603 | struct r5dev *dev = &sh->dev[disk_idx]; |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2604 | struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]], |
| 2605 | &sh->dev[s->failed_num[1]] }; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2606 | |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2607 | /* is the data in this block needed, and can we get it? */ |
| 2608 | if (!test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2609 | !test_bit(R5_UPTODATE, &dev->flags) && |
| 2610 | (dev->toread || |
| 2611 | (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) || |
| 2612 | s->syncing || s->expanding || |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2613 | (s->replacing && want_replace(sh, disk_idx)) || |
NeilBrown | 5d35e09 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2614 | (s->failed >= 1 && fdev[0]->toread) || |
| 2615 | (s->failed >= 2 && fdev[1]->toread) || |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2616 | (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite && |
| 2617 | !test_bit(R5_OVERWRITE, &fdev[0]->flags)) || |
| 2618 | (sh->raid_conf->level == 6 && s->failed && s->to_write))) { |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2619 | /* we would like to get this block, possibly by computing it, |
| 2620 | * otherwise read it if the backing disk is insync |
| 2621 | */ |
| 2622 | BUG_ON(test_bit(R5_Wantcompute, &dev->flags)); |
| 2623 | BUG_ON(test_bit(R5_Wantread, &dev->flags)); |
| 2624 | if ((s->uptodate == disks - 1) && |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2625 | (s->failed && (disk_idx == s->failed_num[0] || |
| 2626 | disk_idx == s->failed_num[1]))) { |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2627 | /* have disk failed, and we're requested to fetch it; |
| 2628 | * do compute it |
| 2629 | */ |
| 2630 | pr_debug("Computing stripe %llu block %d\n", |
| 2631 | (unsigned long long)sh->sector, disk_idx); |
| 2632 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2633 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2634 | set_bit(R5_Wantcompute, &dev->flags); |
| 2635 | sh->ops.target = disk_idx; |
| 2636 | sh->ops.target2 = -1; /* no 2nd target */ |
| 2637 | s->req_compute = 1; |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2638 | /* Careful: from this point on 'uptodate' is in the eye |
| 2639 | * of raid_run_ops which services 'compute' operations |
| 2640 | * before writes. R5_Wantcompute flags a block that will |
| 2641 | * be R5_UPTODATE by the time it is needed for a |
| 2642 | * subsequent operation. |
| 2643 | */ |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2644 | s->uptodate++; |
| 2645 | return 1; |
| 2646 | } else if (s->uptodate == disks-2 && s->failed >= 2) { |
| 2647 | /* Computing 2-failure is *very* expensive; only |
| 2648 | * do it if failed >= 2 |
| 2649 | */ |
| 2650 | int other; |
| 2651 | for (other = disks; other--; ) { |
| 2652 | if (other == disk_idx) |
| 2653 | continue; |
| 2654 | if (!test_bit(R5_UPTODATE, |
| 2655 | &sh->dev[other].flags)) |
| 2656 | break; |
| 2657 | } |
| 2658 | BUG_ON(other < 0); |
| 2659 | pr_debug("Computing stripe %llu blocks %d,%d\n", |
| 2660 | (unsigned long long)sh->sector, |
| 2661 | disk_idx, other); |
| 2662 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2663 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2664 | set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags); |
| 2665 | set_bit(R5_Wantcompute, &sh->dev[other].flags); |
| 2666 | sh->ops.target = disk_idx; |
| 2667 | sh->ops.target2 = other; |
| 2668 | s->uptodate += 2; |
| 2669 | s->req_compute = 1; |
| 2670 | return 1; |
| 2671 | } else if (test_bit(R5_Insync, &dev->flags)) { |
| 2672 | set_bit(R5_LOCKED, &dev->flags); |
| 2673 | set_bit(R5_Wantread, &dev->flags); |
| 2674 | s->locked++; |
| 2675 | pr_debug("Reading block %d (sync=%d)\n", |
| 2676 | disk_idx, s->syncing); |
| 2677 | } |
| 2678 | } |
| 2679 | |
| 2680 | return 0; |
| 2681 | } |
| 2682 | |
| 2683 | /** |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2684 | * handle_stripe_fill - read or compute data to satisfy pending requests. |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2685 | */ |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2686 | static void handle_stripe_fill(struct stripe_head *sh, |
| 2687 | struct stripe_head_state *s, |
| 2688 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2689 | { |
| 2690 | int i; |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2691 | |
| 2692 | /* look for blocks to read/compute, skip this if a compute |
| 2693 | * is already in flight, or if the stripe contents are in the |
| 2694 | * midst of changing due to a write |
| 2695 | */ |
| 2696 | if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state && |
| 2697 | !sh->reconstruct_state) |
| 2698 | for (i = disks; i--; ) |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2699 | if (fetch_block(sh, s, i, disks)) |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2700 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2701 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2702 | } |
| 2703 | |
| 2704 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2705 | /* handle_stripe_clean_event |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2706 | * any written block on an uptodate or failed drive can be returned. |
| 2707 | * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but |
| 2708 | * never LOCKED, so we don't need to test 'failed' directly. |
| 2709 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2710 | static void handle_stripe_clean_event(struct r5conf *conf, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2711 | struct stripe_head *sh, int disks, struct bio **return_bi) |
| 2712 | { |
| 2713 | int i; |
| 2714 | struct r5dev *dev; |
| 2715 | |
| 2716 | for (i = disks; i--; ) |
| 2717 | if (sh->dev[i].written) { |
| 2718 | dev = &sh->dev[i]; |
| 2719 | if (!test_bit(R5_LOCKED, &dev->flags) && |
| 2720 | test_bit(R5_UPTODATE, &dev->flags)) { |
| 2721 | /* We can return any write requests */ |
| 2722 | struct bio *wbi, *wbi2; |
| 2723 | int bitmap_end = 0; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2724 | pr_debug("Return write for disc %d\n", i); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2725 | spin_lock_irq(&conf->device_lock); |
| 2726 | wbi = dev->written; |
| 2727 | dev->written = NULL; |
| 2728 | while (wbi && wbi->bi_sector < |
| 2729 | dev->sector + STRIPE_SECTORS) { |
| 2730 | wbi2 = r5_next_bio(wbi, dev->sector); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame^] | 2731 | if (!raid5_dec_bi_active_stripes(wbi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2732 | md_write_end(conf->mddev); |
| 2733 | wbi->bi_next = *return_bi; |
| 2734 | *return_bi = wbi; |
| 2735 | } |
| 2736 | wbi = wbi2; |
| 2737 | } |
| 2738 | if (dev->towrite == NULL) |
| 2739 | bitmap_end = 1; |
| 2740 | spin_unlock_irq(&conf->device_lock); |
| 2741 | if (bitmap_end) |
| 2742 | bitmap_endwrite(conf->mddev->bitmap, |
| 2743 | sh->sector, |
| 2744 | STRIPE_SECTORS, |
| 2745 | !test_bit(STRIPE_DEGRADED, &sh->state), |
| 2746 | 0); |
| 2747 | } |
| 2748 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2749 | |
| 2750 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 2751 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 2752 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2753 | } |
| 2754 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2755 | static void handle_stripe_dirtying(struct r5conf *conf, |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2756 | struct stripe_head *sh, |
| 2757 | struct stripe_head_state *s, |
| 2758 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2759 | { |
| 2760 | int rmw = 0, rcw = 0, i; |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2761 | if (conf->max_degraded == 2) { |
| 2762 | /* RAID6 requires 'rcw' in current implementation |
| 2763 | * Calculate the real rcw later - for now fake it |
| 2764 | * look like rcw is cheaper |
| 2765 | */ |
| 2766 | rcw = 1; rmw = 2; |
| 2767 | } else for (i = disks; i--; ) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2768 | /* would I have to read this buffer for read_modify_write */ |
| 2769 | struct r5dev *dev = &sh->dev[i]; |
| 2770 | if ((dev->towrite || i == sh->pd_idx) && |
| 2771 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2772 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2773 | test_bit(R5_Wantcompute, &dev->flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2774 | if (test_bit(R5_Insync, &dev->flags)) |
| 2775 | rmw++; |
| 2776 | else |
| 2777 | rmw += 2*disks; /* cannot read it */ |
| 2778 | } |
| 2779 | /* Would I have to read this buffer for reconstruct_write */ |
| 2780 | if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx && |
| 2781 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2782 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2783 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2784 | if (test_bit(R5_Insync, &dev->flags)) rcw++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2785 | else |
| 2786 | rcw += 2*disks; |
| 2787 | } |
| 2788 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2789 | pr_debug("for sector %llu, rmw=%d rcw=%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2790 | (unsigned long long)sh->sector, rmw, rcw); |
| 2791 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2792 | if (rmw < rcw && rmw > 0) |
| 2793 | /* prefer read-modify-write, but need to get some data */ |
| 2794 | for (i = disks; i--; ) { |
| 2795 | struct r5dev *dev = &sh->dev[i]; |
| 2796 | if ((dev->towrite || i == sh->pd_idx) && |
| 2797 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2798 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2799 | test_bit(R5_Wantcompute, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2800 | test_bit(R5_Insync, &dev->flags)) { |
| 2801 | if ( |
| 2802 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2803 | pr_debug("Read_old block " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2804 | "%d for r-m-w\n", i); |
| 2805 | set_bit(R5_LOCKED, &dev->flags); |
| 2806 | set_bit(R5_Wantread, &dev->flags); |
| 2807 | s->locked++; |
| 2808 | } else { |
| 2809 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2810 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2811 | } |
| 2812 | } |
| 2813 | } |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2814 | if (rcw <= rmw && rcw > 0) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2815 | /* want reconstruct write, but need to get some data */ |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2816 | rcw = 0; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2817 | for (i = disks; i--; ) { |
| 2818 | struct r5dev *dev = &sh->dev[i]; |
| 2819 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2820 | i != sh->pd_idx && i != sh->qd_idx && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2821 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2822 | !(test_bit(R5_UPTODATE, &dev->flags) || |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2823 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2824 | rcw++; |
| 2825 | if (!test_bit(R5_Insync, &dev->flags)) |
| 2826 | continue; /* it's a failed drive */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2827 | if ( |
| 2828 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2829 | pr_debug("Read_old block " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2830 | "%d for Reconstruct\n", i); |
| 2831 | set_bit(R5_LOCKED, &dev->flags); |
| 2832 | set_bit(R5_Wantread, &dev->flags); |
| 2833 | s->locked++; |
| 2834 | } else { |
| 2835 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2836 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2837 | } |
| 2838 | } |
| 2839 | } |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2840 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2841 | /* now if nothing is locked, and if we have enough data, |
| 2842 | * we can start a write request |
| 2843 | */ |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2844 | /* since handle_stripe can be called at any time we need to handle the |
| 2845 | * case where a compute block operation has been submitted and then a |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2846 | * subsequent call wants to start a write request. raid_run_ops only |
| 2847 | * handles the case where compute block and reconstruct are requested |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2848 | * simultaneously. If this is not the case then new writes need to be |
| 2849 | * held off until the compute completes. |
| 2850 | */ |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2851 | if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) && |
| 2852 | (s->locked == 0 && (rcw == 0 || rmw == 0) && |
| 2853 | !test_bit(STRIPE_BIT_DELAY, &sh->state))) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2854 | schedule_reconstruction(sh, s, rcw == 0, 0); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2855 | } |
| 2856 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2857 | static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2858 | struct stripe_head_state *s, int disks) |
| 2859 | { |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2860 | struct r5dev *dev = NULL; |
Dan Williams | e89f896 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2861 | |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2862 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2863 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2864 | switch (sh->check_state) { |
| 2865 | case check_state_idle: |
| 2866 | /* start a new check operation if there are no failures */ |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2867 | if (s->failed == 0) { |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2868 | BUG_ON(s->uptodate != disks); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2869 | sh->check_state = check_state_run; |
| 2870 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2871 | clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2872 | s->uptodate--; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2873 | break; |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2874 | } |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2875 | dev = &sh->dev[s->failed_num[0]]; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2876 | /* fall through */ |
| 2877 | case check_state_compute_result: |
| 2878 | sh->check_state = check_state_idle; |
| 2879 | if (!dev) |
| 2880 | dev = &sh->dev[sh->pd_idx]; |
| 2881 | |
| 2882 | /* check that a write has not made the stripe insync */ |
| 2883 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 2884 | break; |
| 2885 | |
| 2886 | /* either failed parity check, or recovery is happening */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2887 | BUG_ON(!test_bit(R5_UPTODATE, &dev->flags)); |
| 2888 | BUG_ON(s->uptodate != disks); |
| 2889 | |
| 2890 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2891 | s->locked++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2892 | set_bit(R5_Wantwrite, &dev->flags); |
Dan Williams | 830ea01 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2893 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2894 | clear_bit(STRIPE_DEGRADED, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2895 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2896 | break; |
| 2897 | case check_state_run: |
| 2898 | break; /* we will be called again upon completion */ |
| 2899 | case check_state_check_result: |
| 2900 | sh->check_state = check_state_idle; |
| 2901 | |
| 2902 | /* if a failure occurred during the check operation, leave |
| 2903 | * STRIPE_INSYNC not set and let the stripe be handled again |
| 2904 | */ |
| 2905 | if (s->failed) |
| 2906 | break; |
| 2907 | |
| 2908 | /* handle a successful check operation, if parity is correct |
| 2909 | * we are done. Otherwise update the mismatch count and repair |
| 2910 | * parity if !MD_RECOVERY_CHECK |
| 2911 | */ |
Dan Williams | ad283ea | 2009-08-29 19:09:26 -0700 | [diff] [blame] | 2912 | if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0) |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2913 | /* parity is correct (on disc, |
| 2914 | * not in buffer any more) |
| 2915 | */ |
| 2916 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2917 | else { |
| 2918 | conf->mddev->resync_mismatches += STRIPE_SECTORS; |
| 2919 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 2920 | /* don't try to repair!! */ |
| 2921 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2922 | else { |
| 2923 | sh->check_state = check_state_compute_run; |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2924 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2925 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2926 | set_bit(R5_Wantcompute, |
| 2927 | &sh->dev[sh->pd_idx].flags); |
| 2928 | sh->ops.target = sh->pd_idx; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2929 | sh->ops.target2 = -1; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2930 | s->uptodate++; |
| 2931 | } |
| 2932 | } |
| 2933 | break; |
| 2934 | case check_state_compute_run: |
| 2935 | break; |
| 2936 | default: |
| 2937 | printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n", |
| 2938 | __func__, sh->check_state, |
| 2939 | (unsigned long long) sh->sector); |
| 2940 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2941 | } |
| 2942 | } |
| 2943 | |
| 2944 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2945 | static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2946 | struct stripe_head_state *s, |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2947 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2948 | { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2949 | int pd_idx = sh->pd_idx; |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 2950 | int qd_idx = sh->qd_idx; |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2951 | struct r5dev *dev; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2952 | |
| 2953 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2954 | |
| 2955 | BUG_ON(s->failed > 2); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2956 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2957 | /* Want to check and possibly repair P and Q. |
| 2958 | * However there could be one 'failed' device, in which |
| 2959 | * case we can only check one of them, possibly using the |
| 2960 | * other to generate missing data |
| 2961 | */ |
| 2962 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2963 | switch (sh->check_state) { |
| 2964 | case check_state_idle: |
| 2965 | /* start a new check operation if there are < 2 failures */ |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2966 | if (s->failed == s->q_failed) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2967 | /* The only possible failed device holds Q, so it |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2968 | * makes sense to check P (If anything else were failed, |
| 2969 | * we would have used P to recreate it). |
| 2970 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2971 | sh->check_state = check_state_run; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2972 | } |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2973 | if (!s->q_failed && s->failed < 2) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2974 | /* Q is not failed, and we didn't use it to generate |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2975 | * anything, so it makes sense to check it |
| 2976 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2977 | if (sh->check_state == check_state_run) |
| 2978 | sh->check_state = check_state_run_pq; |
| 2979 | else |
| 2980 | sh->check_state = check_state_run_q; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2981 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2982 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2983 | /* discard potentially stale zero_sum_result */ |
| 2984 | sh->ops.zero_sum_result = 0; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2985 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2986 | if (sh->check_state == check_state_run) { |
| 2987 | /* async_xor_zero_sum destroys the contents of P */ |
| 2988 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
| 2989 | s->uptodate--; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2990 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2991 | if (sh->check_state >= check_state_run && |
| 2992 | sh->check_state <= check_state_run_pq) { |
| 2993 | /* async_syndrome_zero_sum preserves P and Q, so |
| 2994 | * no need to mark them !uptodate here |
| 2995 | */ |
| 2996 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
| 2997 | break; |
| 2998 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2999 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3000 | /* we have 2-disk failure */ |
| 3001 | BUG_ON(s->failed != 2); |
| 3002 | /* fall through */ |
| 3003 | case check_state_compute_result: |
| 3004 | sh->check_state = check_state_idle; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3005 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3006 | /* check that a write has not made the stripe insync */ |
| 3007 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 3008 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3009 | |
| 3010 | /* now write out any block on a failed drive, |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3011 | * or P or Q if they were recomputed |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3012 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3013 | BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3014 | if (s->failed == 2) { |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3015 | dev = &sh->dev[s->failed_num[1]]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3016 | s->locked++; |
| 3017 | set_bit(R5_LOCKED, &dev->flags); |
| 3018 | set_bit(R5_Wantwrite, &dev->flags); |
| 3019 | } |
| 3020 | if (s->failed >= 1) { |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3021 | dev = &sh->dev[s->failed_num[0]]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3022 | s->locked++; |
| 3023 | set_bit(R5_LOCKED, &dev->flags); |
| 3024 | set_bit(R5_Wantwrite, &dev->flags); |
| 3025 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3026 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3027 | dev = &sh->dev[pd_idx]; |
| 3028 | s->locked++; |
| 3029 | set_bit(R5_LOCKED, &dev->flags); |
| 3030 | set_bit(R5_Wantwrite, &dev->flags); |
| 3031 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3032 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3033 | dev = &sh->dev[qd_idx]; |
| 3034 | s->locked++; |
| 3035 | set_bit(R5_LOCKED, &dev->flags); |
| 3036 | set_bit(R5_Wantwrite, &dev->flags); |
| 3037 | } |
| 3038 | clear_bit(STRIPE_DEGRADED, &sh->state); |
| 3039 | |
| 3040 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3041 | break; |
| 3042 | case check_state_run: |
| 3043 | case check_state_run_q: |
| 3044 | case check_state_run_pq: |
| 3045 | break; /* we will be called again upon completion */ |
| 3046 | case check_state_check_result: |
| 3047 | sh->check_state = check_state_idle; |
| 3048 | |
| 3049 | /* handle a successful check operation, if parity is correct |
| 3050 | * we are done. Otherwise update the mismatch count and repair |
| 3051 | * parity if !MD_RECOVERY_CHECK |
| 3052 | */ |
| 3053 | if (sh->ops.zero_sum_result == 0) { |
| 3054 | /* both parities are correct */ |
| 3055 | if (!s->failed) |
| 3056 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3057 | else { |
| 3058 | /* in contrast to the raid5 case we can validate |
| 3059 | * parity, but still have a failure to write |
| 3060 | * back |
| 3061 | */ |
| 3062 | sh->check_state = check_state_compute_result; |
| 3063 | /* Returning at this point means that we may go |
| 3064 | * off and bring p and/or q uptodate again so |
| 3065 | * we make sure to check zero_sum_result again |
| 3066 | * to verify if p or q need writeback |
| 3067 | */ |
| 3068 | } |
| 3069 | } else { |
| 3070 | conf->mddev->resync_mismatches += STRIPE_SECTORS; |
| 3071 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 3072 | /* don't try to repair!! */ |
| 3073 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3074 | else { |
| 3075 | int *target = &sh->ops.target; |
| 3076 | |
| 3077 | sh->ops.target = -1; |
| 3078 | sh->ops.target2 = -1; |
| 3079 | sh->check_state = check_state_compute_run; |
| 3080 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 3081 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 3082 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
| 3083 | set_bit(R5_Wantcompute, |
| 3084 | &sh->dev[pd_idx].flags); |
| 3085 | *target = pd_idx; |
| 3086 | target = &sh->ops.target2; |
| 3087 | s->uptodate++; |
| 3088 | } |
| 3089 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
| 3090 | set_bit(R5_Wantcompute, |
| 3091 | &sh->dev[qd_idx].flags); |
| 3092 | *target = qd_idx; |
| 3093 | s->uptodate++; |
| 3094 | } |
| 3095 | } |
| 3096 | } |
| 3097 | break; |
| 3098 | case check_state_compute_run: |
| 3099 | break; |
| 3100 | default: |
| 3101 | printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n", |
| 3102 | __func__, sh->check_state, |
| 3103 | (unsigned long long) sh->sector); |
| 3104 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3105 | } |
| 3106 | } |
| 3107 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3108 | static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3109 | { |
| 3110 | int i; |
| 3111 | |
| 3112 | /* We have read all the blocks in this stripe and now we need to |
| 3113 | * copy some of them into a target stripe for expand. |
| 3114 | */ |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3115 | struct dma_async_tx_descriptor *tx = NULL; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3116 | clear_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 3117 | for (i = 0; i < sh->disks; i++) |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 3118 | if (i != sh->pd_idx && i != sh->qd_idx) { |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3119 | int dd_idx, j; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3120 | struct stripe_head *sh2; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 3121 | struct async_submit_ctl submit; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3122 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 3123 | sector_t bn = compute_blocknr(sh, i, 1); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3124 | sector_t s = raid5_compute_sector(conf, bn, 0, |
| 3125 | &dd_idx, NULL); |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 3126 | sh2 = get_active_stripe(conf, s, 0, 1, 1); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3127 | if (sh2 == NULL) |
| 3128 | /* so far only the early blocks of this stripe |
| 3129 | * have been requested. When later blocks |
| 3130 | * get requested, we will try again |
| 3131 | */ |
| 3132 | continue; |
| 3133 | if (!test_bit(STRIPE_EXPANDING, &sh2->state) || |
| 3134 | test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) { |
| 3135 | /* must have already done this block */ |
| 3136 | release_stripe(sh2); |
| 3137 | continue; |
| 3138 | } |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3139 | |
| 3140 | /* place all the copies on one channel */ |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 3141 | init_async_submit(&submit, 0, tx, NULL, NULL, NULL); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3142 | tx = async_memcpy(sh2->dev[dd_idx].page, |
Dan Williams | 88ba2aa | 2009-04-09 16:16:18 -0700 | [diff] [blame] | 3143 | sh->dev[i].page, 0, 0, STRIPE_SIZE, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 3144 | &submit); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3145 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3146 | set_bit(R5_Expanded, &sh2->dev[dd_idx].flags); |
| 3147 | set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags); |
| 3148 | for (j = 0; j < conf->raid_disks; j++) |
| 3149 | if (j != sh2->pd_idx && |
NeilBrown | 86c374b | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3150 | j != sh2->qd_idx && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3151 | !test_bit(R5_Expanded, &sh2->dev[j].flags)) |
| 3152 | break; |
| 3153 | if (j == conf->raid_disks) { |
| 3154 | set_bit(STRIPE_EXPAND_READY, &sh2->state); |
| 3155 | set_bit(STRIPE_HANDLE, &sh2->state); |
| 3156 | } |
| 3157 | release_stripe(sh2); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3158 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3159 | } |
NeilBrown | a2e0855 | 2007-09-11 15:23:36 -0700 | [diff] [blame] | 3160 | /* done submitting copies, wait for them to complete */ |
| 3161 | if (tx) { |
| 3162 | async_tx_ack(tx); |
| 3163 | dma_wait_for_async_tx(tx); |
| 3164 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3165 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3166 | |
| 3167 | /* |
| 3168 | * handle_stripe - do things to a stripe. |
| 3169 | * |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3170 | * We lock the stripe by setting STRIPE_ACTIVE and then examine the |
| 3171 | * state of various bits to see what needs to be done. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3172 | * Possible results: |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3173 | * return some read requests which now have data |
| 3174 | * return some write requests which are safely on storage |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3175 | * schedule a read on some buffers |
| 3176 | * schedule a write of some buffers |
| 3177 | * return confirmation of parity correctness |
| 3178 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3179 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3180 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3181 | static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3182 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3183 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3184 | int disks = sh->disks; |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3185 | struct r5dev *dev; |
| 3186 | int i; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3187 | int do_recovery = 0; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3188 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3189 | memset(s, 0, sizeof(*s)); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3190 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3191 | s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 3192 | s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state); |
| 3193 | s->failed_num[0] = -1; |
| 3194 | s->failed_num[1] = -1; |
| 3195 | |
| 3196 | /* Now to look around and see what can be done */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3197 | rcu_read_lock(); |
NeilBrown | c4c1663 | 2011-07-26 11:34:20 +1000 | [diff] [blame] | 3198 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3199 | for (i=disks; i--; ) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3200 | struct md_rdev *rdev; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3201 | sector_t first_bad; |
| 3202 | int bad_sectors; |
| 3203 | int is_bad = 0; |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3204 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3205 | dev = &sh->dev[i]; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3206 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3207 | pr_debug("check %d: state 0x%lx read %p write %p written %p\n", |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3208 | i, dev->flags, |
| 3209 | dev->toread, dev->towrite, dev->written); |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3210 | /* maybe we can reply to a read |
| 3211 | * |
| 3212 | * new wantfill requests are only permitted while |
| 3213 | * ops_complete_biofill is guaranteed to be inactive |
| 3214 | */ |
| 3215 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread && |
| 3216 | !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) |
| 3217 | set_bit(R5_Wantfill, &dev->flags); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3218 | |
| 3219 | /* now count some things */ |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3220 | if (test_bit(R5_LOCKED, &dev->flags)) |
| 3221 | s->locked++; |
| 3222 | if (test_bit(R5_UPTODATE, &dev->flags)) |
| 3223 | s->uptodate++; |
Dan Williams | 2d6e4ec | 2009-09-16 12:11:54 -0700 | [diff] [blame] | 3224 | if (test_bit(R5_Wantcompute, &dev->flags)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3225 | s->compute++; |
| 3226 | BUG_ON(s->compute > 2); |
Dan Williams | 2d6e4ec | 2009-09-16 12:11:54 -0700 | [diff] [blame] | 3227 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3228 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3229 | if (test_bit(R5_Wantfill, &dev->flags)) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3230 | s->to_fill++; |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3231 | else if (dev->toread) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3232 | s->to_read++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3233 | if (dev->towrite) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3234 | s->to_write++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3235 | if (!test_bit(R5_OVERWRITE, &dev->flags)) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3236 | s->non_overwrite++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3237 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3238 | if (dev->written) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3239 | s->written++; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3240 | /* Prefer to use the replacement for reads, but only |
| 3241 | * if it is recovered enough and has no bad blocks. |
| 3242 | */ |
| 3243 | rdev = rcu_dereference(conf->disks[i].replacement); |
| 3244 | if (rdev && !test_bit(Faulty, &rdev->flags) && |
| 3245 | rdev->recovery_offset >= sh->sector + STRIPE_SECTORS && |
| 3246 | !is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 3247 | &first_bad, &bad_sectors)) |
| 3248 | set_bit(R5_ReadRepl, &dev->flags); |
| 3249 | else { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3250 | if (rdev) |
| 3251 | set_bit(R5_NeedReplace, &dev->flags); |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3252 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 3253 | clear_bit(R5_ReadRepl, &dev->flags); |
| 3254 | } |
NeilBrown | 9283d8c | 2011-12-08 16:27:57 +1100 | [diff] [blame] | 3255 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 3256 | rdev = NULL; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3257 | if (rdev) { |
| 3258 | is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 3259 | &first_bad, &bad_sectors); |
| 3260 | if (s->blocked_rdev == NULL |
| 3261 | && (test_bit(Blocked, &rdev->flags) |
| 3262 | || is_bad < 0)) { |
| 3263 | if (is_bad < 0) |
| 3264 | set_bit(BlockedBadBlocks, |
| 3265 | &rdev->flags); |
| 3266 | s->blocked_rdev = rdev; |
| 3267 | atomic_inc(&rdev->nr_pending); |
| 3268 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3269 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3270 | clear_bit(R5_Insync, &dev->flags); |
| 3271 | if (!rdev) |
| 3272 | /* Not in-sync */; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3273 | else if (is_bad) { |
| 3274 | /* also not in-sync */ |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3275 | if (!test_bit(WriteErrorSeen, &rdev->flags) && |
| 3276 | test_bit(R5_UPTODATE, &dev->flags)) { |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3277 | /* treat as in-sync, but with a read error |
| 3278 | * which we can now try to correct |
| 3279 | */ |
| 3280 | set_bit(R5_Insync, &dev->flags); |
| 3281 | set_bit(R5_ReadError, &dev->flags); |
| 3282 | } |
| 3283 | } else if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3284 | set_bit(R5_Insync, &dev->flags); |
NeilBrown | 30d7a48 | 2011-12-23 09:57:00 +1100 | [diff] [blame] | 3285 | else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset) |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3286 | /* in sync if before recovery_offset */ |
NeilBrown | 30d7a48 | 2011-12-23 09:57:00 +1100 | [diff] [blame] | 3287 | set_bit(R5_Insync, &dev->flags); |
| 3288 | else if (test_bit(R5_UPTODATE, &dev->flags) && |
| 3289 | test_bit(R5_Expanded, &dev->flags)) |
| 3290 | /* If we've reshaped into here, we assume it is Insync. |
| 3291 | * We will shortly update recovery_offset to make |
| 3292 | * it official. |
| 3293 | */ |
| 3294 | set_bit(R5_Insync, &dev->flags); |
| 3295 | |
Adam Kwolek | 5d8c71f | 2011-12-09 14:26:11 +1100 | [diff] [blame] | 3296 | if (rdev && test_bit(R5_WriteError, &dev->flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3297 | /* This flag does not apply to '.replacement' |
| 3298 | * only to .rdev, so make sure to check that*/ |
| 3299 | struct md_rdev *rdev2 = rcu_dereference( |
| 3300 | conf->disks[i].rdev); |
| 3301 | if (rdev2 == rdev) |
| 3302 | clear_bit(R5_Insync, &dev->flags); |
| 3303 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3304 | s->handle_bad_blocks = 1; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3305 | atomic_inc(&rdev2->nr_pending); |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3306 | } else |
| 3307 | clear_bit(R5_WriteError, &dev->flags); |
| 3308 | } |
Adam Kwolek | 5d8c71f | 2011-12-09 14:26:11 +1100 | [diff] [blame] | 3309 | if (rdev && test_bit(R5_MadeGood, &dev->flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3310 | /* This flag does not apply to '.replacement' |
| 3311 | * only to .rdev, so make sure to check that*/ |
| 3312 | struct md_rdev *rdev2 = rcu_dereference( |
| 3313 | conf->disks[i].rdev); |
| 3314 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 3315 | s->handle_bad_blocks = 1; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3316 | atomic_inc(&rdev2->nr_pending); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 3317 | } else |
| 3318 | clear_bit(R5_MadeGood, &dev->flags); |
| 3319 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3320 | if (test_bit(R5_MadeGoodRepl, &dev->flags)) { |
| 3321 | struct md_rdev *rdev2 = rcu_dereference( |
| 3322 | conf->disks[i].replacement); |
| 3323 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
| 3324 | s->handle_bad_blocks = 1; |
| 3325 | atomic_inc(&rdev2->nr_pending); |
| 3326 | } else |
| 3327 | clear_bit(R5_MadeGoodRepl, &dev->flags); |
| 3328 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3329 | if (!test_bit(R5_Insync, &dev->flags)) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3330 | /* The ReadError flag will just be confusing now */ |
| 3331 | clear_bit(R5_ReadError, &dev->flags); |
| 3332 | clear_bit(R5_ReWrite, &dev->flags); |
| 3333 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3334 | if (test_bit(R5_ReadError, &dev->flags)) |
| 3335 | clear_bit(R5_Insync, &dev->flags); |
| 3336 | if (!test_bit(R5_Insync, &dev->flags)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3337 | if (s->failed < 2) |
| 3338 | s->failed_num[s->failed] = i; |
| 3339 | s->failed++; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3340 | if (rdev && !test_bit(Faulty, &rdev->flags)) |
| 3341 | do_recovery = 1; |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3342 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3343 | } |
NeilBrown | c4c1663 | 2011-07-26 11:34:20 +1000 | [diff] [blame] | 3344 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3345 | if (test_bit(STRIPE_SYNCING, &sh->state)) { |
| 3346 | /* If there is a failed device being replaced, |
| 3347 | * we must be recovering. |
| 3348 | * else if we are after recovery_cp, we must be syncing |
majianpeng | c6d2e08 | 2012-04-02 01:16:59 +1000 | [diff] [blame] | 3349 | * else if MD_RECOVERY_REQUESTED is set, we also are syncing. |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3350 | * else we can only be replacing |
| 3351 | * sync and recovery both need to read all devices, and so |
| 3352 | * use the same flag. |
| 3353 | */ |
| 3354 | if (do_recovery || |
majianpeng | c6d2e08 | 2012-04-02 01:16:59 +1000 | [diff] [blame] | 3355 | sh->sector >= conf->mddev->recovery_cp || |
| 3356 | test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery))) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3357 | s->syncing = 1; |
| 3358 | else |
| 3359 | s->replacing = 1; |
| 3360 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3361 | rcu_read_unlock(); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3362 | } |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3363 | |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3364 | static void handle_stripe(struct stripe_head *sh) |
| 3365 | { |
| 3366 | struct stripe_head_state s; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3367 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3368 | int i; |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3369 | int prexor; |
| 3370 | int disks = sh->disks; |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3371 | struct r5dev *pdev, *qdev; |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3372 | |
| 3373 | clear_bit(STRIPE_HANDLE, &sh->state); |
Dan Williams | 257a4b4 | 2011-11-08 16:22:06 +1100 | [diff] [blame] | 3374 | if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3375 | /* already being handled, ensure it gets handled |
| 3376 | * again when current action finishes */ |
| 3377 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3378 | return; |
| 3379 | } |
| 3380 | |
| 3381 | if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) { |
| 3382 | set_bit(STRIPE_SYNCING, &sh->state); |
| 3383 | clear_bit(STRIPE_INSYNC, &sh->state); |
| 3384 | } |
| 3385 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 3386 | |
| 3387 | pr_debug("handling stripe %llu, state=%#lx cnt=%d, " |
| 3388 | "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n", |
| 3389 | (unsigned long long)sh->sector, sh->state, |
| 3390 | atomic_read(&sh->count), sh->pd_idx, sh->qd_idx, |
| 3391 | sh->check_state, sh->reconstruct_state); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3392 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3393 | analyse_stripe(sh, &s); |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3394 | |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3395 | if (s.handle_bad_blocks) { |
| 3396 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3397 | goto finish; |
| 3398 | } |
| 3399 | |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3400 | if (unlikely(s.blocked_rdev)) { |
| 3401 | if (s.syncing || s.expanding || s.expanded || |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3402 | s.replacing || s.to_write || s.written) { |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3403 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3404 | goto finish; |
| 3405 | } |
| 3406 | /* There is nothing for the blocked_rdev to block */ |
| 3407 | rdev_dec_pending(s.blocked_rdev, conf->mddev); |
| 3408 | s.blocked_rdev = NULL; |
| 3409 | } |
| 3410 | |
| 3411 | if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) { |
| 3412 | set_bit(STRIPE_OP_BIOFILL, &s.ops_request); |
| 3413 | set_bit(STRIPE_BIOFILL_RUN, &sh->state); |
| 3414 | } |
| 3415 | |
| 3416 | pr_debug("locked=%d uptodate=%d to_read=%d" |
| 3417 | " to_write=%d failed=%d failed_num=%d,%d\n", |
| 3418 | s.locked, s.uptodate, s.to_read, s.to_write, s.failed, |
| 3419 | s.failed_num[0], s.failed_num[1]); |
| 3420 | /* check if the array has lost more than max_degraded devices and, |
| 3421 | * if so, some requests might need to be failed. |
| 3422 | */ |
NeilBrown | 9a3f530 | 2011-11-08 16:22:01 +1100 | [diff] [blame] | 3423 | if (s.failed > conf->max_degraded) { |
| 3424 | sh->check_state = 0; |
| 3425 | sh->reconstruct_state = 0; |
| 3426 | if (s.to_read+s.to_write+s.written) |
| 3427 | handle_failed_stripe(conf, sh, &s, disks, &s.return_bi); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3428 | if (s.syncing + s.replacing) |
NeilBrown | 9a3f530 | 2011-11-08 16:22:01 +1100 | [diff] [blame] | 3429 | handle_failed_sync(conf, sh, &s); |
| 3430 | } |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3431 | |
| 3432 | /* |
| 3433 | * might be able to return some write requests if the parity blocks |
| 3434 | * are safe, or on a failed drive |
| 3435 | */ |
| 3436 | pdev = &sh->dev[sh->pd_idx]; |
| 3437 | s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx) |
| 3438 | || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx); |
| 3439 | qdev = &sh->dev[sh->qd_idx]; |
| 3440 | s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx) |
| 3441 | || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx) |
| 3442 | || conf->level < 6; |
| 3443 | |
| 3444 | if (s.written && |
| 3445 | (s.p_failed || ((test_bit(R5_Insync, &pdev->flags) |
| 3446 | && !test_bit(R5_LOCKED, &pdev->flags) |
| 3447 | && test_bit(R5_UPTODATE, &pdev->flags)))) && |
| 3448 | (s.q_failed || ((test_bit(R5_Insync, &qdev->flags) |
| 3449 | && !test_bit(R5_LOCKED, &qdev->flags) |
| 3450 | && test_bit(R5_UPTODATE, &qdev->flags))))) |
| 3451 | handle_stripe_clean_event(conf, sh, disks, &s.return_bi); |
| 3452 | |
| 3453 | /* Now we might consider reading some blocks, either to check/generate |
| 3454 | * parity, or to satisfy requests |
| 3455 | * or to load a block that is being partially written. |
| 3456 | */ |
| 3457 | if (s.to_read || s.non_overwrite |
| 3458 | || (conf->level == 6 && s.to_write && s.failed) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3459 | || (s.syncing && (s.uptodate + s.compute < disks)) |
| 3460 | || s.replacing |
| 3461 | || s.expanding) |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3462 | handle_stripe_fill(sh, &s, disks); |
| 3463 | |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3464 | /* Now we check to see if any write operations have recently |
| 3465 | * completed |
| 3466 | */ |
| 3467 | prexor = 0; |
| 3468 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_result) |
| 3469 | prexor = 1; |
| 3470 | if (sh->reconstruct_state == reconstruct_state_drain_result || |
| 3471 | sh->reconstruct_state == reconstruct_state_prexor_drain_result) { |
| 3472 | sh->reconstruct_state = reconstruct_state_idle; |
| 3473 | |
| 3474 | /* All the 'written' buffers and the parity block are ready to |
| 3475 | * be written back to disk |
| 3476 | */ |
| 3477 | BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags)); |
| 3478 | BUG_ON(sh->qd_idx >= 0 && |
| 3479 | !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags)); |
| 3480 | for (i = disks; i--; ) { |
| 3481 | struct r5dev *dev = &sh->dev[i]; |
| 3482 | if (test_bit(R5_LOCKED, &dev->flags) && |
| 3483 | (i == sh->pd_idx || i == sh->qd_idx || |
| 3484 | dev->written)) { |
| 3485 | pr_debug("Writing block %d\n", i); |
| 3486 | set_bit(R5_Wantwrite, &dev->flags); |
| 3487 | if (prexor) |
| 3488 | continue; |
| 3489 | if (!test_bit(R5_Insync, &dev->flags) || |
| 3490 | ((i == sh->pd_idx || i == sh->qd_idx) && |
| 3491 | s.failed == 0)) |
| 3492 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3493 | } |
| 3494 | } |
| 3495 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 3496 | s.dec_preread_active = 1; |
| 3497 | } |
| 3498 | |
| 3499 | /* Now to consider new write requests and what else, if anything |
| 3500 | * should be read. We do not handle new writes when: |
| 3501 | * 1/ A 'write' operation (copy+xor) is already in flight. |
| 3502 | * 2/ A 'check' operation is in flight, as it may clobber the parity |
| 3503 | * block. |
| 3504 | */ |
| 3505 | if (s.to_write && !sh->reconstruct_state && !sh->check_state) |
| 3506 | handle_stripe_dirtying(conf, sh, &s, disks); |
| 3507 | |
| 3508 | /* maybe we need to check and possibly fix the parity for this stripe |
| 3509 | * Any reads will already have been scheduled, so we just see if enough |
| 3510 | * data is available. The parity check is held off while parity |
| 3511 | * dependent operations are in flight. |
| 3512 | */ |
| 3513 | if (sh->check_state || |
| 3514 | (s.syncing && s.locked == 0 && |
| 3515 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && |
| 3516 | !test_bit(STRIPE_INSYNC, &sh->state))) { |
| 3517 | if (conf->level == 6) |
| 3518 | handle_parity_checks6(conf, sh, &s, disks); |
| 3519 | else |
| 3520 | handle_parity_checks5(conf, sh, &s, disks); |
| 3521 | } |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3522 | |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3523 | if (s.replacing && s.locked == 0 |
| 3524 | && !test_bit(STRIPE_INSYNC, &sh->state)) { |
| 3525 | /* Write out to replacement devices where possible */ |
| 3526 | for (i = 0; i < conf->raid_disks; i++) |
| 3527 | if (test_bit(R5_UPTODATE, &sh->dev[i].flags) && |
| 3528 | test_bit(R5_NeedReplace, &sh->dev[i].flags)) { |
| 3529 | set_bit(R5_WantReplace, &sh->dev[i].flags); |
| 3530 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 3531 | s.locked++; |
| 3532 | } |
| 3533 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3534 | } |
| 3535 | if ((s.syncing || s.replacing) && s.locked == 0 && |
| 3536 | test_bit(STRIPE_INSYNC, &sh->state)) { |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3537 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 3538 | clear_bit(STRIPE_SYNCING, &sh->state); |
| 3539 | } |
| 3540 | |
| 3541 | /* If the failed drives are just a ReadError, then we might need |
| 3542 | * to progress the repair/check process |
| 3543 | */ |
| 3544 | if (s.failed <= conf->max_degraded && !conf->mddev->ro) |
| 3545 | for (i = 0; i < s.failed; i++) { |
| 3546 | struct r5dev *dev = &sh->dev[s.failed_num[i]]; |
| 3547 | if (test_bit(R5_ReadError, &dev->flags) |
| 3548 | && !test_bit(R5_LOCKED, &dev->flags) |
| 3549 | && test_bit(R5_UPTODATE, &dev->flags) |
| 3550 | ) { |
| 3551 | if (!test_bit(R5_ReWrite, &dev->flags)) { |
| 3552 | set_bit(R5_Wantwrite, &dev->flags); |
| 3553 | set_bit(R5_ReWrite, &dev->flags); |
| 3554 | set_bit(R5_LOCKED, &dev->flags); |
| 3555 | s.locked++; |
| 3556 | } else { |
| 3557 | /* let's read it back */ |
| 3558 | set_bit(R5_Wantread, &dev->flags); |
| 3559 | set_bit(R5_LOCKED, &dev->flags); |
| 3560 | s.locked++; |
| 3561 | } |
| 3562 | } |
| 3563 | } |
| 3564 | |
| 3565 | |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3566 | /* Finish reconstruct operations initiated by the expansion process */ |
| 3567 | if (sh->reconstruct_state == reconstruct_state_result) { |
| 3568 | struct stripe_head *sh_src |
| 3569 | = get_active_stripe(conf, sh->sector, 1, 1, 1); |
| 3570 | if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) { |
| 3571 | /* sh cannot be written until sh_src has been read. |
| 3572 | * so arrange for sh to be delayed a little |
| 3573 | */ |
| 3574 | set_bit(STRIPE_DELAYED, &sh->state); |
| 3575 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3576 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, |
| 3577 | &sh_src->state)) |
| 3578 | atomic_inc(&conf->preread_active_stripes); |
| 3579 | release_stripe(sh_src); |
| 3580 | goto finish; |
| 3581 | } |
| 3582 | if (sh_src) |
| 3583 | release_stripe(sh_src); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3584 | |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3585 | sh->reconstruct_state = reconstruct_state_idle; |
| 3586 | clear_bit(STRIPE_EXPANDING, &sh->state); |
| 3587 | for (i = conf->raid_disks; i--; ) { |
| 3588 | set_bit(R5_Wantwrite, &sh->dev[i].flags); |
| 3589 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 3590 | s.locked++; |
| 3591 | } |
| 3592 | } |
| 3593 | |
| 3594 | if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) && |
| 3595 | !sh->reconstruct_state) { |
| 3596 | /* Need to write out all blocks after computing parity */ |
| 3597 | sh->disks = conf->raid_disks; |
| 3598 | stripe_set_idx(sh->sector, conf, 0, sh); |
| 3599 | schedule_reconstruction(sh, &s, 1, 1); |
| 3600 | } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) { |
| 3601 | clear_bit(STRIPE_EXPAND_READY, &sh->state); |
| 3602 | atomic_dec(&conf->reshape_stripes); |
| 3603 | wake_up(&conf->wait_for_overlap); |
| 3604 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 3605 | } |
| 3606 | |
| 3607 | if (s.expanding && s.locked == 0 && |
| 3608 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) |
| 3609 | handle_stripe_expansion(conf, sh); |
| 3610 | |
| 3611 | finish: |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3612 | /* wait for this device to become unblocked */ |
NeilBrown | 5f066c6 | 2012-07-03 12:13:29 +1000 | [diff] [blame] | 3613 | if (unlikely(s.blocked_rdev)) { |
| 3614 | if (conf->mddev->external) |
| 3615 | md_wait_for_blocked_rdev(s.blocked_rdev, |
| 3616 | conf->mddev); |
| 3617 | else |
| 3618 | /* Internal metadata will immediately |
| 3619 | * be written by raid5d, so we don't |
| 3620 | * need to wait here. |
| 3621 | */ |
| 3622 | rdev_dec_pending(s.blocked_rdev, |
| 3623 | conf->mddev); |
| 3624 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3625 | |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3626 | if (s.handle_bad_blocks) |
| 3627 | for (i = disks; i--; ) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3628 | struct md_rdev *rdev; |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3629 | struct r5dev *dev = &sh->dev[i]; |
| 3630 | if (test_and_clear_bit(R5_WriteError, &dev->flags)) { |
| 3631 | /* We own a safe reference to the rdev */ |
| 3632 | rdev = conf->disks[i].rdev; |
| 3633 | if (!rdev_set_badblocks(rdev, sh->sector, |
| 3634 | STRIPE_SECTORS, 0)) |
| 3635 | md_error(conf->mddev, rdev); |
| 3636 | rdev_dec_pending(rdev, conf->mddev); |
| 3637 | } |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 3638 | if (test_and_clear_bit(R5_MadeGood, &dev->flags)) { |
| 3639 | rdev = conf->disks[i].rdev; |
| 3640 | rdev_clear_badblocks(rdev, sh->sector, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3641 | STRIPE_SECTORS, 0); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 3642 | rdev_dec_pending(rdev, conf->mddev); |
| 3643 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3644 | if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) { |
| 3645 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3646 | if (!rdev) |
| 3647 | /* rdev have been moved down */ |
| 3648 | rdev = conf->disks[i].rdev; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3649 | rdev_clear_badblocks(rdev, sh->sector, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3650 | STRIPE_SECTORS, 0); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3651 | rdev_dec_pending(rdev, conf->mddev); |
| 3652 | } |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3653 | } |
| 3654 | |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3655 | if (s.ops_request) |
| 3656 | raid_run_ops(sh, s.ops_request); |
| 3657 | |
Dan Williams | f0e43bc | 2008-06-28 08:31:55 +1000 | [diff] [blame] | 3658 | ops_run_io(sh, &s); |
| 3659 | |
NeilBrown | c5709ef | 2011-07-26 11:35:20 +1000 | [diff] [blame] | 3660 | if (s.dec_preread_active) { |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 3661 | /* We delay this until after ops_run_io so that if make_request |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 3662 | * is waiting on a flush, it won't continue until the writes |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 3663 | * have actually been submitted. |
| 3664 | */ |
| 3665 | atomic_dec(&conf->preread_active_stripes); |
| 3666 | if (atomic_read(&conf->preread_active_stripes) < |
| 3667 | IO_THRESHOLD) |
| 3668 | md_wakeup_thread(conf->mddev->thread); |
| 3669 | } |
| 3670 | |
NeilBrown | c5709ef | 2011-07-26 11:35:20 +1000 | [diff] [blame] | 3671 | return_io(s.return_bi); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3672 | |
Dan Williams | 257a4b4 | 2011-11-08 16:22:06 +1100 | [diff] [blame] | 3673 | clear_bit_unlock(STRIPE_ACTIVE, &sh->state); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3674 | } |
| 3675 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3676 | static void raid5_activate_delayed(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3677 | { |
| 3678 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) { |
| 3679 | while (!list_empty(&conf->delayed_list)) { |
| 3680 | struct list_head *l = conf->delayed_list.next; |
| 3681 | struct stripe_head *sh; |
| 3682 | sh = list_entry(l, struct stripe_head, lru); |
| 3683 | list_del_init(l); |
| 3684 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 3685 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 3686 | atomic_inc(&conf->preread_active_stripes); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3687 | list_add_tail(&sh->lru, &conf->hold_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3688 | } |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 3689 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3690 | } |
| 3691 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3692 | static void activate_bit_delay(struct r5conf *conf) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3693 | { |
| 3694 | /* device_lock is held */ |
| 3695 | struct list_head head; |
| 3696 | list_add(&head, &conf->bitmap_list); |
| 3697 | list_del_init(&conf->bitmap_list); |
| 3698 | while (!list_empty(&head)) { |
| 3699 | struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru); |
| 3700 | list_del_init(&sh->lru); |
| 3701 | atomic_inc(&sh->count); |
| 3702 | __release_stripe(conf, sh); |
| 3703 | } |
| 3704 | } |
| 3705 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3706 | int md_raid5_congested(struct mddev *mddev, int bits) |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3707 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3708 | struct r5conf *conf = mddev->private; |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3709 | |
| 3710 | /* No difference between reads and writes. Just check |
| 3711 | * how busy the stripe_cache is |
| 3712 | */ |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 3713 | |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3714 | if (conf->inactive_blocked) |
| 3715 | return 1; |
| 3716 | if (conf->quiesce) |
| 3717 | return 1; |
| 3718 | if (list_empty_careful(&conf->inactive_list)) |
| 3719 | return 1; |
| 3720 | |
| 3721 | return 0; |
| 3722 | } |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 3723 | EXPORT_SYMBOL_GPL(md_raid5_congested); |
| 3724 | |
| 3725 | static int raid5_congested(void *data, int bits) |
| 3726 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3727 | struct mddev *mddev = data; |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 3728 | |
| 3729 | return mddev_congested(mddev, bits) || |
| 3730 | md_raid5_congested(mddev, bits); |
| 3731 | } |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3732 | |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3733 | /* We want read requests to align with chunks where possible, |
| 3734 | * but write requests don't need to. |
| 3735 | */ |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3736 | static int raid5_mergeable_bvec(struct request_queue *q, |
| 3737 | struct bvec_merge_data *bvm, |
| 3738 | struct bio_vec *biovec) |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3739 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3740 | struct mddev *mddev = q->queuedata; |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3741 | 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] | 3742 | int max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3743 | unsigned int chunk_sectors = mddev->chunk_sectors; |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3744 | unsigned int bio_sectors = bvm->bi_size >> 9; |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3745 | |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3746 | if ((bvm->bi_rw & 1) == WRITE) |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3747 | return biovec->bv_len; /* always allow writes to be mergeable */ |
| 3748 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3749 | if (mddev->new_chunk_sectors < mddev->chunk_sectors) |
| 3750 | chunk_sectors = mddev->new_chunk_sectors; |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3751 | max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9; |
| 3752 | if (max < 0) max = 0; |
| 3753 | if (max <= biovec->bv_len && bio_sectors == 0) |
| 3754 | return biovec->bv_len; |
| 3755 | else |
| 3756 | return max; |
| 3757 | } |
| 3758 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3759 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3760 | static int in_chunk_boundary(struct mddev *mddev, struct bio *bio) |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3761 | { |
| 3762 | sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3763 | unsigned int chunk_sectors = mddev->chunk_sectors; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3764 | unsigned int bio_sectors = bio->bi_size >> 9; |
| 3765 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3766 | if (mddev->new_chunk_sectors < mddev->chunk_sectors) |
| 3767 | chunk_sectors = mddev->new_chunk_sectors; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3768 | return chunk_sectors >= |
| 3769 | ((sector & (chunk_sectors - 1)) + bio_sectors); |
| 3770 | } |
| 3771 | |
| 3772 | /* |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3773 | * add bio to the retry LIFO ( in O(1) ... we are in interrupt ) |
| 3774 | * later sampled by raid5d. |
| 3775 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3776 | static void add_bio_to_retry(struct bio *bi,struct r5conf *conf) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3777 | { |
| 3778 | unsigned long flags; |
| 3779 | |
| 3780 | spin_lock_irqsave(&conf->device_lock, flags); |
| 3781 | |
| 3782 | bi->bi_next = conf->retry_read_aligned_list; |
| 3783 | conf->retry_read_aligned_list = bi; |
| 3784 | |
| 3785 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 3786 | md_wakeup_thread(conf->mddev->thread); |
| 3787 | } |
| 3788 | |
| 3789 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3790 | static struct bio *remove_bio_from_retry(struct r5conf *conf) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3791 | { |
| 3792 | struct bio *bi; |
| 3793 | |
| 3794 | bi = conf->retry_read_aligned; |
| 3795 | if (bi) { |
| 3796 | conf->retry_read_aligned = NULL; |
| 3797 | return bi; |
| 3798 | } |
| 3799 | bi = conf->retry_read_aligned_list; |
| 3800 | if(bi) { |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3801 | conf->retry_read_aligned_list = bi->bi_next; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3802 | bi->bi_next = NULL; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 3803 | /* |
| 3804 | * this sets the active strip count to 1 and the processed |
| 3805 | * strip count to zero (upper 8 bits) |
| 3806 | */ |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame^] | 3807 | raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */ |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3808 | } |
| 3809 | |
| 3810 | return bi; |
| 3811 | } |
| 3812 | |
| 3813 | |
| 3814 | /* |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3815 | * The "raid5_align_endio" should check if the read succeeded and if it |
| 3816 | * did, call bio_endio on the original bio (having bio_put the new bio |
| 3817 | * first). |
| 3818 | * If the read failed.. |
| 3819 | */ |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3820 | static void raid5_align_endio(struct bio *bi, int error) |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3821 | { |
| 3822 | struct bio* raid_bi = bi->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3823 | struct mddev *mddev; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3824 | struct r5conf *conf; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3825 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3826 | struct md_rdev *rdev; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3827 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3828 | bio_put(bi); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3829 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3830 | rdev = (void*)raid_bi->bi_next; |
| 3831 | raid_bi->bi_next = NULL; |
NeilBrown | 2b7f222 | 2010-03-25 16:06:03 +1100 | [diff] [blame] | 3832 | mddev = rdev->mddev; |
| 3833 | conf = mddev->private; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3834 | |
| 3835 | rdev_dec_pending(rdev, conf->mddev); |
| 3836 | |
| 3837 | if (!error && uptodate) { |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3838 | bio_endio(raid_bi, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3839 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
| 3840 | wake_up(&conf->wait_for_stripe); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3841 | return; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3842 | } |
| 3843 | |
| 3844 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3845 | 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] | 3846 | |
| 3847 | add_bio_to_retry(raid_bi, conf); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3848 | } |
| 3849 | |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3850 | static int bio_fits_rdev(struct bio *bi) |
| 3851 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3852 | struct request_queue *q = bdev_get_queue(bi->bi_bdev); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3853 | |
Martin K. Petersen | ae03bf6 | 2009-05-22 17:17:50 -0400 | [diff] [blame] | 3854 | if ((bi->bi_size>>9) > queue_max_sectors(q)) |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3855 | return 0; |
| 3856 | blk_recount_segments(q, bi); |
Martin K. Petersen | 8a78362 | 2010-02-26 00:20:39 -0500 | [diff] [blame] | 3857 | if (bi->bi_phys_segments > queue_max_segments(q)) |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3858 | return 0; |
| 3859 | |
| 3860 | if (q->merge_bvec_fn) |
| 3861 | /* it's too hard to apply the merge_bvec_fn at this stage, |
| 3862 | * just just give up |
| 3863 | */ |
| 3864 | return 0; |
| 3865 | |
| 3866 | return 1; |
| 3867 | } |
| 3868 | |
| 3869 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3870 | static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio) |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3871 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3872 | struct r5conf *conf = mddev->private; |
NeilBrown | 8553fe7ec | 2009-12-14 12:49:47 +1100 | [diff] [blame] | 3873 | int dd_idx; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3874 | struct bio* align_bi; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3875 | struct md_rdev *rdev; |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3876 | sector_t end_sector; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3877 | |
| 3878 | if (!in_chunk_boundary(mddev, raid_bio)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3879 | pr_debug("chunk_aligned_read : non aligned\n"); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3880 | return 0; |
| 3881 | } |
| 3882 | /* |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 3883 | * use bio_clone_mddev to make a copy of the bio |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3884 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 3885 | align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3886 | if (!align_bi) |
| 3887 | return 0; |
| 3888 | /* |
| 3889 | * set bi_end_io to a new function, and set bi_private to the |
| 3890 | * original bio. |
| 3891 | */ |
| 3892 | align_bi->bi_end_io = raid5_align_endio; |
| 3893 | align_bi->bi_private = raid_bio; |
| 3894 | /* |
| 3895 | * compute position |
| 3896 | */ |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3897 | align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector, |
| 3898 | 0, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3899 | &dd_idx, NULL); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3900 | |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3901 | end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3902 | rcu_read_lock(); |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3903 | rdev = rcu_dereference(conf->disks[dd_idx].replacement); |
| 3904 | if (!rdev || test_bit(Faulty, &rdev->flags) || |
| 3905 | rdev->recovery_offset < end_sector) { |
| 3906 | rdev = rcu_dereference(conf->disks[dd_idx].rdev); |
| 3907 | if (rdev && |
| 3908 | (test_bit(Faulty, &rdev->flags) || |
| 3909 | !(test_bit(In_sync, &rdev->flags) || |
| 3910 | rdev->recovery_offset >= end_sector))) |
| 3911 | rdev = NULL; |
| 3912 | } |
| 3913 | if (rdev) { |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3914 | sector_t first_bad; |
| 3915 | int bad_sectors; |
| 3916 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3917 | atomic_inc(&rdev->nr_pending); |
| 3918 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3919 | raid_bio->bi_next = (void*)rdev; |
| 3920 | align_bi->bi_bdev = rdev->bdev; |
| 3921 | align_bi->bi_flags &= ~(1 << BIO_SEG_VALID); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3922 | |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3923 | if (!bio_fits_rdev(align_bi) || |
| 3924 | is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9, |
| 3925 | &first_bad, &bad_sectors)) { |
| 3926 | /* too big in some way, or has a known bad block */ |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3927 | bio_put(align_bi); |
| 3928 | rdev_dec_pending(rdev, mddev); |
| 3929 | return 0; |
| 3930 | } |
| 3931 | |
majianpeng | 6c0544e | 2012-06-12 08:31:10 +0800 | [diff] [blame] | 3932 | /* No reshape active, so we can trust rdev->data_offset */ |
| 3933 | align_bi->bi_sector += rdev->data_offset; |
| 3934 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3935 | spin_lock_irq(&conf->device_lock); |
| 3936 | wait_event_lock_irq(conf->wait_for_stripe, |
| 3937 | conf->quiesce == 0, |
| 3938 | conf->device_lock, /* nothing */); |
| 3939 | atomic_inc(&conf->active_aligned_reads); |
| 3940 | spin_unlock_irq(&conf->device_lock); |
| 3941 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3942 | generic_make_request(align_bi); |
| 3943 | return 1; |
| 3944 | } else { |
| 3945 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3946 | bio_put(align_bi); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3947 | return 0; |
| 3948 | } |
| 3949 | } |
| 3950 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3951 | /* __get_priority_stripe - get the next stripe to process |
| 3952 | * |
| 3953 | * Full stripe writes are allowed to pass preread active stripes up until |
| 3954 | * the bypass_threshold is exceeded. In general the bypass_count |
| 3955 | * increments when the handle_list is handled before the hold_list; however, it |
| 3956 | * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a |
| 3957 | * stripe with in flight i/o. The bypass_count will be reset when the |
| 3958 | * head of the hold_list has changed, i.e. the head was promoted to the |
| 3959 | * handle_list. |
| 3960 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3961 | static struct stripe_head *__get_priority_stripe(struct r5conf *conf) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3962 | { |
| 3963 | struct stripe_head *sh; |
| 3964 | |
| 3965 | pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n", |
| 3966 | __func__, |
| 3967 | list_empty(&conf->handle_list) ? "empty" : "busy", |
| 3968 | list_empty(&conf->hold_list) ? "empty" : "busy", |
| 3969 | atomic_read(&conf->pending_full_writes), conf->bypass_count); |
| 3970 | |
| 3971 | if (!list_empty(&conf->handle_list)) { |
| 3972 | sh = list_entry(conf->handle_list.next, typeof(*sh), lru); |
| 3973 | |
| 3974 | if (list_empty(&conf->hold_list)) |
| 3975 | conf->bypass_count = 0; |
| 3976 | else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) { |
| 3977 | if (conf->hold_list.next == conf->last_hold) |
| 3978 | conf->bypass_count++; |
| 3979 | else { |
| 3980 | conf->last_hold = conf->hold_list.next; |
| 3981 | conf->bypass_count -= conf->bypass_threshold; |
| 3982 | if (conf->bypass_count < 0) |
| 3983 | conf->bypass_count = 0; |
| 3984 | } |
| 3985 | } |
| 3986 | } else if (!list_empty(&conf->hold_list) && |
| 3987 | ((conf->bypass_threshold && |
| 3988 | conf->bypass_count > conf->bypass_threshold) || |
| 3989 | atomic_read(&conf->pending_full_writes) == 0)) { |
| 3990 | sh = list_entry(conf->hold_list.next, |
| 3991 | typeof(*sh), lru); |
| 3992 | conf->bypass_count -= conf->bypass_threshold; |
| 3993 | if (conf->bypass_count < 0) |
| 3994 | conf->bypass_count = 0; |
| 3995 | } else |
| 3996 | return NULL; |
| 3997 | |
| 3998 | list_del_init(&sh->lru); |
| 3999 | atomic_inc(&sh->count); |
| 4000 | BUG_ON(atomic_read(&sh->count) != 1); |
| 4001 | return sh; |
| 4002 | } |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4003 | |
Linus Torvalds | b4fdcb0 | 2011-11-04 17:06:58 -0700 | [diff] [blame] | 4004 | static void make_request(struct mddev *mddev, struct bio * bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4005 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4006 | struct r5conf *conf = mddev->private; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4007 | int dd_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4008 | sector_t new_sector; |
| 4009 | sector_t logical_sector, last_sector; |
| 4010 | struct stripe_head *sh; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 4011 | const int rw = bio_data_dir(bi); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 4012 | int remaining; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4013 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 4014 | if (unlikely(bi->bi_rw & REQ_FLUSH)) { |
| 4015 | md_flush_request(mddev, bi); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 4016 | return; |
NeilBrown | e5dcdd8 | 2005-09-09 16:23:41 -0700 | [diff] [blame] | 4017 | } |
| 4018 | |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 4019 | md_write_start(mddev, bi); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 4020 | |
NeilBrown | 802ba06 | 2006-12-13 00:34:13 -0800 | [diff] [blame] | 4021 | if (rw == READ && |
Raz Ben-Jehuda(caro) | 5248861 | 2006-12-10 02:20:48 -0800 | [diff] [blame] | 4022 | mddev->reshape_position == MaxSector && |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 4023 | chunk_aligned_read(mddev,bi)) |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 4024 | return; |
Raz Ben-Jehuda(caro) | 5248861 | 2006-12-10 02:20:48 -0800 | [diff] [blame] | 4025 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4026 | logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
| 4027 | last_sector = bi->bi_sector + (bi->bi_size>>9); |
| 4028 | bi->bi_next = NULL; |
| 4029 | bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 4030 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4031 | for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) { |
| 4032 | DEFINE_WAIT(w); |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4033 | int previous; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 4034 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4035 | retry: |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4036 | previous = 0; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 4037 | prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 4038 | if (unlikely(conf->reshape_progress != MaxSector)) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4039 | /* spinlock is needed as reshape_progress may be |
NeilBrown | df8e7f76 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 4040 | * 64bit on a 32bit platform, and so it might be |
| 4041 | * possible to see a half-updated value |
Jesper Juhl | aeb878b | 2011-04-10 18:06:17 +0200 | [diff] [blame] | 4042 | * Of course reshape_progress could change after |
NeilBrown | df8e7f76 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 4043 | * the lock is dropped, so once we get a reference |
| 4044 | * to the stripe that we think it is, we will have |
| 4045 | * to check again. |
| 4046 | */ |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4047 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4048 | if (mddev->reshape_backwards |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4049 | ? logical_sector < conf->reshape_progress |
| 4050 | : logical_sector >= conf->reshape_progress) { |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4051 | previous = 1; |
| 4052 | } else { |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4053 | if (mddev->reshape_backwards |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4054 | ? logical_sector < conf->reshape_safe |
| 4055 | : logical_sector >= conf->reshape_safe) { |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 4056 | spin_unlock_irq(&conf->device_lock); |
| 4057 | schedule(); |
| 4058 | goto retry; |
| 4059 | } |
| 4060 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4061 | spin_unlock_irq(&conf->device_lock); |
| 4062 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4063 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4064 | new_sector = raid5_compute_sector(conf, logical_sector, |
| 4065 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4066 | &dd_idx, NULL); |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4067 | pr_debug("raid456: make_request, sector %llu logical %llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4068 | (unsigned long long)new_sector, |
| 4069 | (unsigned long long)logical_sector); |
| 4070 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4071 | sh = get_active_stripe(conf, new_sector, previous, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4072 | (bi->bi_rw&RWA_MASK), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4073 | if (sh) { |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 4074 | if (unlikely(previous)) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4075 | /* expansion might have moved on while waiting for a |
NeilBrown | df8e7f76 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 4076 | * stripe, so we must do the range check again. |
| 4077 | * Expansion could still move past after this |
| 4078 | * test, but as we are holding a reference to |
| 4079 | * 'sh', we know that if that happens, |
| 4080 | * STRIPE_EXPANDING will get set and the expansion |
| 4081 | * won't proceed until we finish with the stripe. |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4082 | */ |
| 4083 | int must_retry = 0; |
| 4084 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4085 | if (mddev->reshape_backwards |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 4086 | ? logical_sector >= conf->reshape_progress |
| 4087 | : logical_sector < conf->reshape_progress) |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4088 | /* mismatch, need to try again */ |
| 4089 | must_retry = 1; |
| 4090 | spin_unlock_irq(&conf->device_lock); |
| 4091 | if (must_retry) { |
| 4092 | release_stripe(sh); |
Dan Williams | 7a3ab90 | 2009-06-16 16:00:33 -0700 | [diff] [blame] | 4093 | schedule(); |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4094 | goto retry; |
| 4095 | } |
| 4096 | } |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 4097 | |
Namhyung Kim | ffd96e3 | 2011-07-18 17:38:51 +1000 | [diff] [blame] | 4098 | if (rw == WRITE && |
NeilBrown | a5c308d | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 4099 | logical_sector >= mddev->suspend_lo && |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4100 | logical_sector < mddev->suspend_hi) { |
| 4101 | release_stripe(sh); |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 4102 | /* As the suspend_* range is controlled by |
| 4103 | * userspace, we want an interruptible |
| 4104 | * wait. |
| 4105 | */ |
| 4106 | flush_signals(current); |
| 4107 | prepare_to_wait(&conf->wait_for_overlap, |
| 4108 | &w, TASK_INTERRUPTIBLE); |
| 4109 | if (logical_sector >= mddev->suspend_lo && |
| 4110 | logical_sector < mddev->suspend_hi) |
| 4111 | schedule(); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4112 | goto retry; |
| 4113 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4114 | |
| 4115 | if (test_bit(STRIPE_EXPANDING, &sh->state) || |
Namhyung Kim | ffd96e3 | 2011-07-18 17:38:51 +1000 | [diff] [blame] | 4116 | !add_stripe_bio(sh, bi, dd_idx, rw)) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4117 | /* Stripe is busy expanding or |
| 4118 | * add failed due to overlap. Flush everything |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4119 | * and wait a while |
| 4120 | */ |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 4121 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4122 | release_stripe(sh); |
| 4123 | schedule(); |
| 4124 | goto retry; |
| 4125 | } |
| 4126 | finish_wait(&conf->wait_for_overlap, &w); |
NeilBrown | 6ed3003 | 2008-02-06 01:40:00 -0800 | [diff] [blame] | 4127 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4128 | clear_bit(STRIPE_DELAYED, &sh->state); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 4129 | if ((bi->bi_rw & REQ_SYNC) && |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 4130 | !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 4131 | atomic_inc(&conf->preread_active_stripes); |
NeilBrown | b357f04 | 2012-07-03 17:45:31 +1000 | [diff] [blame] | 4132 | mddev_check_plugged(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4133 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4134 | } else { |
| 4135 | /* cannot get stripe for read-ahead, just give-up */ |
| 4136 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
| 4137 | finish_wait(&conf->wait_for_overlap, &w); |
| 4138 | break; |
| 4139 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4140 | } |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 4141 | |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame^] | 4142 | remaining = raid5_dec_bi_active_stripes(bi); |
NeilBrown | f634475 | 2006-03-27 01:18:17 -0800 | [diff] [blame] | 4143 | if (remaining == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4144 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4145 | if ( rw == WRITE ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4146 | md_write_end(mddev); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 4147 | |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 4148 | bio_endio(bi, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4149 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4150 | } |
| 4151 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4152 | static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4153 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4154 | static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4155 | { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4156 | /* reshaping is quite different to recovery/resync so it is |
| 4157 | * handled quite separately ... here. |
| 4158 | * |
| 4159 | * On each call to sync_request, we gather one chunk worth of |
| 4160 | * destination stripes and flag them as expanding. |
| 4161 | * Then we find all the source stripes and request reads. |
| 4162 | * As the reads complete, handle_stripe will copy the data |
| 4163 | * into the destination stripe and release that stripe. |
| 4164 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4165 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4166 | struct stripe_head *sh; |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4167 | sector_t first_sector, last_sector; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4168 | int raid_disks = conf->previous_raid_disks; |
| 4169 | int data_disks = raid_disks - conf->max_degraded; |
| 4170 | int new_data_disks = conf->raid_disks - conf->max_degraded; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4171 | int i; |
| 4172 | int dd_idx; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4173 | sector_t writepos, readpos, safepos; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4174 | sector_t stripe_addr; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4175 | int reshape_sectors; |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4176 | struct list_head stripes; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4177 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4178 | if (sector_nr == 0) { |
| 4179 | /* If restarting in the middle, skip the initial sectors */ |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4180 | if (mddev->reshape_backwards && |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4181 | conf->reshape_progress < raid5_size(mddev, 0, 0)) { |
| 4182 | sector_nr = raid5_size(mddev, 0, 0) |
| 4183 | - conf->reshape_progress; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4184 | } else if (!mddev->reshape_backwards && |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4185 | conf->reshape_progress > 0) |
| 4186 | sector_nr = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4187 | sector_div(sector_nr, new_data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4188 | if (sector_nr) { |
NeilBrown | 8dee721 | 2009-11-06 14:59:29 +1100 | [diff] [blame] | 4189 | mddev->curr_resync_completed = sector_nr; |
| 4190 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4191 | *skipped = 1; |
| 4192 | return sector_nr; |
| 4193 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4194 | } |
| 4195 | |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4196 | /* We need to process a full chunk at a time. |
| 4197 | * If old and new chunk sizes differ, we need to process the |
| 4198 | * largest of these |
| 4199 | */ |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4200 | if (mddev->new_chunk_sectors > mddev->chunk_sectors) |
| 4201 | reshape_sectors = mddev->new_chunk_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4202 | else |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4203 | reshape_sectors = mddev->chunk_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4204 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 4205 | /* We update the metadata at least every 10 seconds, or when |
| 4206 | * the data about to be copied would over-write the source of |
| 4207 | * the data at the front of the range. i.e. one new_stripe |
| 4208 | * along from reshape_progress new_maps to after where |
| 4209 | * reshape_safe old_maps to |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4210 | */ |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4211 | writepos = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4212 | sector_div(writepos, new_data_disks); |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4213 | readpos = conf->reshape_progress; |
| 4214 | sector_div(readpos, data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4215 | safepos = conf->reshape_safe; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4216 | sector_div(safepos, data_disks); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4217 | if (mddev->reshape_backwards) { |
NeilBrown | ed37d83 | 2009-05-27 21:39:05 +1000 | [diff] [blame] | 4218 | writepos -= min_t(sector_t, reshape_sectors, writepos); |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4219 | readpos += reshape_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4220 | safepos += reshape_sectors; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4221 | } else { |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4222 | writepos += reshape_sectors; |
NeilBrown | ed37d83 | 2009-05-27 21:39:05 +1000 | [diff] [blame] | 4223 | readpos -= min_t(sector_t, reshape_sectors, readpos); |
| 4224 | safepos -= min_t(sector_t, reshape_sectors, safepos); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4225 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4226 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 4227 | /* Having calculated the 'writepos' possibly use it |
| 4228 | * to set 'stripe_addr' which is where we will write to. |
| 4229 | */ |
| 4230 | if (mddev->reshape_backwards) { |
| 4231 | BUG_ON(conf->reshape_progress == 0); |
| 4232 | stripe_addr = writepos; |
| 4233 | BUG_ON((mddev->dev_sectors & |
| 4234 | ~((sector_t)reshape_sectors - 1)) |
| 4235 | - reshape_sectors - stripe_addr |
| 4236 | != sector_nr); |
| 4237 | } else { |
| 4238 | BUG_ON(writepos != sector_nr + reshape_sectors); |
| 4239 | stripe_addr = sector_nr; |
| 4240 | } |
| 4241 | |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4242 | /* 'writepos' is the most advanced device address we might write. |
| 4243 | * 'readpos' is the least advanced device address we might read. |
| 4244 | * 'safepos' is the least address recorded in the metadata as having |
| 4245 | * been reshaped. |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 4246 | * If there is a min_offset_diff, these are adjusted either by |
| 4247 | * increasing the safepos/readpos if diff is negative, or |
| 4248 | * increasing writepos if diff is positive. |
| 4249 | * If 'readpos' is then behind 'writepos', there is no way that we can |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4250 | * ensure safety in the face of a crash - that must be done by userspace |
| 4251 | * making a backup of the data. So in that case there is no particular |
| 4252 | * rush to update metadata. |
| 4253 | * Otherwise if 'safepos' is behind 'writepos', then we really need to |
| 4254 | * update the metadata to advance 'safepos' to match 'readpos' so that |
| 4255 | * we can be safe in the event of a crash. |
| 4256 | * So we insist on updating metadata if safepos is behind writepos and |
| 4257 | * readpos is beyond writepos. |
| 4258 | * In any case, update the metadata every 10 seconds. |
| 4259 | * Maybe that number should be configurable, but I'm not sure it is |
| 4260 | * worth it.... maybe it could be a multiple of safemode_delay??? |
| 4261 | */ |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 4262 | if (conf->min_offset_diff < 0) { |
| 4263 | safepos += -conf->min_offset_diff; |
| 4264 | readpos += -conf->min_offset_diff; |
| 4265 | } else |
| 4266 | writepos += conf->min_offset_diff; |
| 4267 | |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4268 | if ((mddev->reshape_backwards |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4269 | ? (safepos > writepos && readpos < writepos) |
| 4270 | : (safepos < writepos && readpos > writepos)) || |
| 4271 | time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4272 | /* Cannot proceed until we've updated the superblock... */ |
| 4273 | wait_event(conf->wait_for_overlap, |
| 4274 | atomic_read(&conf->reshape_stripes)==0); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4275 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4276 | mddev->curr_resync_completed = sector_nr; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4277 | conf->reshape_checkpoint = jiffies; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4278 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4279 | md_wakeup_thread(mddev->thread); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4280 | wait_event(mddev->sb_wait, mddev->flags == 0 || |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4281 | kthread_should_stop()); |
| 4282 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4283 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4284 | spin_unlock_irq(&conf->device_lock); |
| 4285 | wake_up(&conf->wait_for_overlap); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4286 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4287 | } |
| 4288 | |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4289 | INIT_LIST_HEAD(&stripes); |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4290 | for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4291 | int j; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4292 | int skipped_disk = 0; |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4293 | sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4294 | set_bit(STRIPE_EXPANDING, &sh->state); |
| 4295 | atomic_inc(&conf->reshape_stripes); |
| 4296 | /* If any of this stripe is beyond the end of the old |
| 4297 | * array, then we need to zero those blocks |
| 4298 | */ |
| 4299 | for (j=sh->disks; j--;) { |
| 4300 | sector_t s; |
| 4301 | if (j == sh->pd_idx) |
| 4302 | continue; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4303 | if (conf->level == 6 && |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4304 | j == sh->qd_idx) |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4305 | continue; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 4306 | s = compute_blocknr(sh, j, 0); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4307 | if (s < raid5_size(mddev, 0, 0)) { |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4308 | skipped_disk = 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4309 | continue; |
| 4310 | } |
| 4311 | memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE); |
| 4312 | set_bit(R5_Expanded, &sh->dev[j].flags); |
| 4313 | set_bit(R5_UPTODATE, &sh->dev[j].flags); |
| 4314 | } |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4315 | if (!skipped_disk) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4316 | set_bit(STRIPE_EXPAND_READY, &sh->state); |
| 4317 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4318 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4319 | list_add(&sh->lru, &stripes); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4320 | } |
| 4321 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4322 | if (mddev->reshape_backwards) |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4323 | conf->reshape_progress -= reshape_sectors * new_data_disks; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4324 | else |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4325 | conf->reshape_progress += reshape_sectors * new_data_disks; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4326 | spin_unlock_irq(&conf->device_lock); |
| 4327 | /* Ok, those stripe are ready. We can start scheduling |
| 4328 | * reads on the source stripes. |
| 4329 | * The source stripes are determined by mapping the first and last |
| 4330 | * block on the destination stripes. |
| 4331 | */ |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4332 | first_sector = |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4333 | raid5_compute_sector(conf, stripe_addr*(new_data_disks), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4334 | 1, &dd_idx, NULL); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4335 | last_sector = |
NeilBrown | 0e6e027 | 2009-06-09 16:32:22 +1000 | [diff] [blame] | 4336 | raid5_compute_sector(conf, ((stripe_addr+reshape_sectors) |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 4337 | * new_data_disks - 1), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4338 | 1, &dd_idx, NULL); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4339 | if (last_sector >= mddev->dev_sectors) |
| 4340 | last_sector = mddev->dev_sectors - 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4341 | while (first_sector <= last_sector) { |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4342 | sh = get_active_stripe(conf, first_sector, 1, 0, 1); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4343 | set_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 4344 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4345 | release_stripe(sh); |
| 4346 | first_sector += STRIPE_SECTORS; |
| 4347 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4348 | /* Now that the sources are clearly marked, we can release |
| 4349 | * the destination stripes |
| 4350 | */ |
| 4351 | while (!list_empty(&stripes)) { |
| 4352 | sh = list_entry(stripes.next, struct stripe_head, lru); |
| 4353 | list_del_init(&sh->lru); |
| 4354 | release_stripe(sh); |
| 4355 | } |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4356 | /* If this takes us to the resync_max point where we have to pause, |
| 4357 | * then we need to write out the superblock. |
| 4358 | */ |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4359 | sector_nr += reshape_sectors; |
NeilBrown | c03f6a1 | 2009-04-17 11:06:30 +1000 | [diff] [blame] | 4360 | if ((sector_nr - mddev->curr_resync_completed) * 2 |
| 4361 | >= mddev->resync_max - mddev->curr_resync_completed) { |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4362 | /* Cannot proceed until we've updated the superblock... */ |
| 4363 | wait_event(conf->wait_for_overlap, |
| 4364 | atomic_read(&conf->reshape_stripes) == 0); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4365 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4366 | mddev->curr_resync_completed = sector_nr; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4367 | conf->reshape_checkpoint = jiffies; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4368 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 4369 | md_wakeup_thread(mddev->thread); |
| 4370 | wait_event(mddev->sb_wait, |
| 4371 | !test_bit(MD_CHANGE_DEVS, &mddev->flags) |
| 4372 | || kthread_should_stop()); |
| 4373 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4374 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4375 | spin_unlock_irq(&conf->device_lock); |
| 4376 | wake_up(&conf->wait_for_overlap); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4377 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4378 | } |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4379 | return reshape_sectors; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4380 | } |
| 4381 | |
| 4382 | /* FIXME go_faster isn't used */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4383 | static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster) |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4384 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4385 | struct r5conf *conf = mddev->private; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4386 | struct stripe_head *sh; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4387 | sector_t max_sector = mddev->dev_sectors; |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 4388 | sector_t sync_blocks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4389 | int still_degraded = 0; |
| 4390 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4391 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4392 | if (sector_nr >= max_sector) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4393 | /* just being told to finish up .. nothing much to do */ |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 4394 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 4395 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { |
| 4396 | end_reshape(conf); |
| 4397 | return 0; |
| 4398 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4399 | |
| 4400 | if (mddev->curr_resync < max_sector) /* aborted */ |
| 4401 | bitmap_end_sync(mddev->bitmap, mddev->curr_resync, |
| 4402 | &sync_blocks, 1); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4403 | else /* completed sync */ |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4404 | conf->fullsync = 0; |
| 4405 | bitmap_close_sync(mddev->bitmap); |
| 4406 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4407 | return 0; |
| 4408 | } |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4409 | |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4410 | /* Allow raid5_quiesce to complete */ |
| 4411 | wait_event(conf->wait_for_overlap, conf->quiesce != 2); |
| 4412 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4413 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 4414 | return reshape_request(mddev, sector_nr, skipped); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4415 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4416 | /* No need to check resync_max as we never do more than one |
| 4417 | * stripe, and as resync_max will always be on a chunk boundary, |
| 4418 | * if the check in md_do_sync didn't fire, there is no chance |
| 4419 | * of overstepping resync_max here |
| 4420 | */ |
| 4421 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4422 | /* if there is too many failed drives and we are trying |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4423 | * to resync, then assert that we are finished, because there is |
| 4424 | * nothing we can do. |
| 4425 | */ |
NeilBrown | 3285edf | 2006-06-26 00:27:55 -0700 | [diff] [blame] | 4426 | if (mddev->degraded >= conf->max_degraded && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4427 | test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4428 | sector_t rv = mddev->dev_sectors - sector_nr; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 4429 | *skipped = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4430 | return rv; |
| 4431 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4432 | if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) && |
NeilBrown | 3855ad9 | 2005-11-08 21:39:38 -0800 | [diff] [blame] | 4433 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4434 | !conf->fullsync && sync_blocks >= STRIPE_SECTORS) { |
| 4435 | /* we can skip this block, and probably more */ |
| 4436 | sync_blocks /= STRIPE_SECTORS; |
| 4437 | *skipped = 1; |
| 4438 | return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */ |
| 4439 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4440 | |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 4441 | bitmap_cond_end_sync(mddev->bitmap, sector_nr); |
| 4442 | |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4443 | sh = get_active_stripe(conf, sector_nr, 0, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4444 | if (sh == NULL) { |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4445 | sh = get_active_stripe(conf, sector_nr, 0, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4446 | /* make sure we don't swamp the stripe cache if someone else |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4447 | * is trying to get access |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4448 | */ |
Nishanth Aravamudan | 66c006a | 2005-11-07 01:01:17 -0800 | [diff] [blame] | 4449 | schedule_timeout_uninterruptible(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4450 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4451 | /* Need to check if array will still be degraded after recovery/resync |
| 4452 | * We don't need to check the 'failed' flag as when that gets set, |
| 4453 | * recovery aborts. |
| 4454 | */ |
NeilBrown | f001a70 | 2009-06-09 14:30:31 +1000 | [diff] [blame] | 4455 | for (i = 0; i < conf->raid_disks; i++) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4456 | if (conf->disks[i].rdev == NULL) |
| 4457 | still_degraded = 1; |
| 4458 | |
| 4459 | bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded); |
| 4460 | |
NeilBrown | 83206d6 | 2011-07-26 11:19:49 +1000 | [diff] [blame] | 4461 | set_bit(STRIPE_SYNC_REQUESTED, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4462 | |
NeilBrown | 1442577 | 2009-10-16 15:55:25 +1100 | [diff] [blame] | 4463 | handle_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4464 | release_stripe(sh); |
| 4465 | |
| 4466 | return STRIPE_SECTORS; |
| 4467 | } |
| 4468 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4469 | static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4470 | { |
| 4471 | /* We may not be able to submit a whole bio at once as there |
| 4472 | * may not be enough stripe_heads available. |
| 4473 | * We cannot pre-allocate enough stripe_heads as we may need |
| 4474 | * more than exist in the cache (if we allow ever large chunks). |
| 4475 | * So we do one stripe head at a time and record in |
| 4476 | * ->bi_hw_segments how many have been done. |
| 4477 | * |
| 4478 | * We *know* that this entire raid_bio is in one chunk, so |
| 4479 | * it will be only one 'dd_idx' and only need one call to raid5_compute_sector. |
| 4480 | */ |
| 4481 | struct stripe_head *sh; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4482 | int dd_idx; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4483 | sector_t sector, logical_sector, last_sector; |
| 4484 | int scnt = 0; |
| 4485 | int remaining; |
| 4486 | int handled = 0; |
| 4487 | |
| 4488 | logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4489 | sector = raid5_compute_sector(conf, logical_sector, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4490 | 0, &dd_idx, NULL); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4491 | last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9); |
| 4492 | |
| 4493 | for (; logical_sector < last_sector; |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4494 | logical_sector += STRIPE_SECTORS, |
| 4495 | sector += STRIPE_SECTORS, |
| 4496 | scnt++) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4497 | |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame^] | 4498 | if (scnt < raid5_bi_processed_stripes(raid_bio)) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4499 | /* already done this stripe */ |
| 4500 | continue; |
| 4501 | |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4502 | sh = get_active_stripe(conf, sector, 0, 1, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4503 | |
| 4504 | if (!sh) { |
| 4505 | /* failed to get a stripe - must wait */ |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame^] | 4506 | raid5_set_bi_processed_stripes(raid_bio, scnt); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4507 | conf->retry_read_aligned = raid_bio; |
| 4508 | return handled; |
| 4509 | } |
| 4510 | |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4511 | if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) { |
| 4512 | release_stripe(sh); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame^] | 4513 | raid5_set_bi_processed_stripes(raid_bio, scnt); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4514 | conf->retry_read_aligned = raid_bio; |
| 4515 | return handled; |
| 4516 | } |
| 4517 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4518 | handle_stripe(sh); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4519 | release_stripe(sh); |
| 4520 | handled++; |
| 4521 | } |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame^] | 4522 | remaining = raid5_dec_bi_active_stripes(raid_bio); |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 4523 | if (remaining == 0) |
| 4524 | bio_endio(raid_bio, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4525 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
| 4526 | wake_up(&conf->wait_for_stripe); |
| 4527 | return handled; |
| 4528 | } |
| 4529 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4530 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4531 | /* |
| 4532 | * This is our raid5 kernel thread. |
| 4533 | * |
| 4534 | * We scan the hash table for stripes which can be handled now. |
| 4535 | * During the scan, completed stripes are saved for us by the interrupt |
| 4536 | * handler, so that they will not have to wait for our next wakeup. |
| 4537 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4538 | static void raid5d(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4539 | { |
| 4540 | struct stripe_head *sh; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4541 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4542 | int handled; |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 4543 | struct blk_plug plug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4544 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4545 | pr_debug("+++ raid5d active\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4546 | |
| 4547 | md_check_recovery(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4548 | |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 4549 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4550 | handled = 0; |
| 4551 | spin_lock_irq(&conf->device_lock); |
| 4552 | while (1) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4553 | struct bio *bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4554 | |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 4555 | if (atomic_read(&mddev->plug_cnt) == 0 && |
| 4556 | !list_empty(&conf->bitmap_list)) { |
| 4557 | /* Now is a good time to flush some bitmap updates */ |
| 4558 | conf->seq_flush++; |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 4559 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4560 | bitmap_unplug(mddev->bitmap); |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 4561 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 4562 | conf->seq_write = conf->seq_flush; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4563 | activate_bit_delay(conf); |
| 4564 | } |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 4565 | if (atomic_read(&mddev->plug_cnt) == 0) |
| 4566 | raid5_activate_delayed(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4567 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4568 | while ((bio = remove_bio_from_retry(conf))) { |
| 4569 | int ok; |
| 4570 | spin_unlock_irq(&conf->device_lock); |
| 4571 | ok = retry_aligned_read(conf, bio); |
| 4572 | spin_lock_irq(&conf->device_lock); |
| 4573 | if (!ok) |
| 4574 | break; |
| 4575 | handled++; |
| 4576 | } |
| 4577 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4578 | sh = __get_priority_stripe(conf); |
| 4579 | |
Dan Williams | c9f21aa | 2008-07-23 12:05:51 -0700 | [diff] [blame] | 4580 | if (!sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4581 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4582 | spin_unlock_irq(&conf->device_lock); |
| 4583 | |
| 4584 | handled++; |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 4585 | handle_stripe(sh); |
| 4586 | release_stripe(sh); |
| 4587 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4588 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 4589 | if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) |
| 4590 | md_check_recovery(mddev); |
| 4591 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4592 | spin_lock_irq(&conf->device_lock); |
| 4593 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4594 | pr_debug("%d stripes handled\n", handled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4595 | |
| 4596 | spin_unlock_irq(&conf->device_lock); |
| 4597 | |
Dan Williams | c9f21aa | 2008-07-23 12:05:51 -0700 | [diff] [blame] | 4598 | async_tx_issue_pending_all(); |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 4599 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4600 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4601 | pr_debug("--- raid5d inactive\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4602 | } |
| 4603 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4604 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4605 | raid5_show_stripe_cache_size(struct mddev *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4606 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4607 | struct r5conf *conf = mddev->private; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4608 | if (conf) |
| 4609 | return sprintf(page, "%d\n", conf->max_nr_stripes); |
| 4610 | else |
| 4611 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4612 | } |
| 4613 | |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 4614 | int |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4615 | raid5_set_cache_size(struct mddev *mddev, int size) |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 4616 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4617 | struct r5conf *conf = mddev->private; |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 4618 | int err; |
| 4619 | |
| 4620 | if (size <= 16 || size > 32768) |
| 4621 | return -EINVAL; |
| 4622 | while (size < conf->max_nr_stripes) { |
| 4623 | if (drop_one_stripe(conf)) |
| 4624 | conf->max_nr_stripes--; |
| 4625 | else |
| 4626 | break; |
| 4627 | } |
| 4628 | err = md_allow_write(mddev); |
| 4629 | if (err) |
| 4630 | return err; |
| 4631 | while (size > conf->max_nr_stripes) { |
| 4632 | if (grow_one_stripe(conf)) |
| 4633 | conf->max_nr_stripes++; |
| 4634 | else break; |
| 4635 | } |
| 4636 | return 0; |
| 4637 | } |
| 4638 | EXPORT_SYMBOL(raid5_set_cache_size); |
| 4639 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4640 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4641 | raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4642 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4643 | struct r5conf *conf = mddev->private; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4644 | unsigned long new; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4645 | int err; |
| 4646 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4647 | if (len >= PAGE_SIZE) |
| 4648 | return -EINVAL; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4649 | if (!conf) |
| 4650 | return -ENODEV; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4651 | |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4652 | if (strict_strtoul(page, 10, &new)) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4653 | return -EINVAL; |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 4654 | err = raid5_set_cache_size(mddev, new); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4655 | if (err) |
| 4656 | return err; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4657 | return len; |
| 4658 | } |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4659 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4660 | static struct md_sysfs_entry |
| 4661 | raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR, |
| 4662 | raid5_show_stripe_cache_size, |
| 4663 | raid5_store_stripe_cache_size); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4664 | |
| 4665 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4666 | raid5_show_preread_threshold(struct mddev *mddev, char *page) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4667 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4668 | struct r5conf *conf = mddev->private; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4669 | if (conf) |
| 4670 | return sprintf(page, "%d\n", conf->bypass_threshold); |
| 4671 | else |
| 4672 | return 0; |
| 4673 | } |
| 4674 | |
| 4675 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4676 | raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4677 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4678 | struct r5conf *conf = mddev->private; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4679 | unsigned long new; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4680 | if (len >= PAGE_SIZE) |
| 4681 | return -EINVAL; |
| 4682 | if (!conf) |
| 4683 | return -ENODEV; |
| 4684 | |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4685 | if (strict_strtoul(page, 10, &new)) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4686 | return -EINVAL; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4687 | if (new > conf->max_nr_stripes) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4688 | return -EINVAL; |
| 4689 | conf->bypass_threshold = new; |
| 4690 | return len; |
| 4691 | } |
| 4692 | |
| 4693 | static struct md_sysfs_entry |
| 4694 | raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold, |
| 4695 | S_IRUGO | S_IWUSR, |
| 4696 | raid5_show_preread_threshold, |
| 4697 | raid5_store_preread_threshold); |
| 4698 | |
| 4699 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4700 | stripe_cache_active_show(struct mddev *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4701 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4702 | struct r5conf *conf = mddev->private; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4703 | if (conf) |
| 4704 | return sprintf(page, "%d\n", atomic_read(&conf->active_stripes)); |
| 4705 | else |
| 4706 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4707 | } |
| 4708 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4709 | static struct md_sysfs_entry |
| 4710 | raid5_stripecache_active = __ATTR_RO(stripe_cache_active); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4711 | |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4712 | static struct attribute *raid5_attrs[] = { |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4713 | &raid5_stripecache_size.attr, |
| 4714 | &raid5_stripecache_active.attr, |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4715 | &raid5_preread_bypass_threshold.attr, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4716 | NULL, |
| 4717 | }; |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4718 | static struct attribute_group raid5_attrs_group = { |
| 4719 | .name = NULL, |
| 4720 | .attrs = raid5_attrs, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4721 | }; |
| 4722 | |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4723 | static sector_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4724 | raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks) |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4725 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4726 | struct r5conf *conf = mddev->private; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4727 | |
| 4728 | if (!sectors) |
| 4729 | sectors = mddev->dev_sectors; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4730 | if (!raid_disks) |
NeilBrown | 7ec0547 | 2009-03-31 15:10:36 +1100 | [diff] [blame] | 4731 | /* size is defined by the smallest of previous and new size */ |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4732 | raid_disks = min(conf->raid_disks, conf->previous_raid_disks); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4733 | |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4734 | sectors &= ~((sector_t)mddev->chunk_sectors - 1); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4735 | sectors &= ~((sector_t)mddev->new_chunk_sectors - 1); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4736 | return sectors * (raid_disks - conf->max_degraded); |
| 4737 | } |
| 4738 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4739 | static void raid5_free_percpu(struct r5conf *conf) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4740 | { |
| 4741 | struct raid5_percpu *percpu; |
| 4742 | unsigned long cpu; |
| 4743 | |
| 4744 | if (!conf->percpu) |
| 4745 | return; |
| 4746 | |
| 4747 | get_online_cpus(); |
| 4748 | for_each_possible_cpu(cpu) { |
| 4749 | percpu = per_cpu_ptr(conf->percpu, cpu); |
| 4750 | safe_put_page(percpu->spare_page); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4751 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4752 | } |
| 4753 | #ifdef CONFIG_HOTPLUG_CPU |
| 4754 | unregister_cpu_notifier(&conf->cpu_notify); |
| 4755 | #endif |
| 4756 | put_online_cpus(); |
| 4757 | |
| 4758 | free_percpu(conf->percpu); |
| 4759 | } |
| 4760 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4761 | static void free_conf(struct r5conf *conf) |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 4762 | { |
| 4763 | shrink_stripes(conf); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4764 | raid5_free_percpu(conf); |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 4765 | kfree(conf->disks); |
| 4766 | kfree(conf->stripe_hashtbl); |
| 4767 | kfree(conf); |
| 4768 | } |
| 4769 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4770 | #ifdef CONFIG_HOTPLUG_CPU |
| 4771 | static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action, |
| 4772 | void *hcpu) |
| 4773 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4774 | struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4775 | long cpu = (long)hcpu; |
| 4776 | struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu); |
| 4777 | |
| 4778 | switch (action) { |
| 4779 | case CPU_UP_PREPARE: |
| 4780 | case CPU_UP_PREPARE_FROZEN: |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4781 | if (conf->level == 6 && !percpu->spare_page) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4782 | percpu->spare_page = alloc_page(GFP_KERNEL); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4783 | if (!percpu->scribble) |
| 4784 | percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL); |
| 4785 | |
| 4786 | if (!percpu->scribble || |
| 4787 | (conf->level == 6 && !percpu->spare_page)) { |
| 4788 | safe_put_page(percpu->spare_page); |
| 4789 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4790 | pr_err("%s: failed memory allocation for cpu%ld\n", |
| 4791 | __func__, cpu); |
Akinobu Mita | 55af6bb | 2010-05-26 14:43:35 -0700 | [diff] [blame] | 4792 | return notifier_from_errno(-ENOMEM); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4793 | } |
| 4794 | break; |
| 4795 | case CPU_DEAD: |
| 4796 | case CPU_DEAD_FROZEN: |
| 4797 | safe_put_page(percpu->spare_page); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4798 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4799 | percpu->spare_page = NULL; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4800 | percpu->scribble = NULL; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4801 | break; |
| 4802 | default: |
| 4803 | break; |
| 4804 | } |
| 4805 | return NOTIFY_OK; |
| 4806 | } |
| 4807 | #endif |
| 4808 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4809 | static int raid5_alloc_percpu(struct r5conf *conf) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4810 | { |
| 4811 | unsigned long cpu; |
| 4812 | struct page *spare_page; |
Tejun Heo | a29d8b8 | 2010-02-02 14:39:15 +0900 | [diff] [blame] | 4813 | struct raid5_percpu __percpu *allcpus; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4814 | void *scribble; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4815 | int err; |
| 4816 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4817 | allcpus = alloc_percpu(struct raid5_percpu); |
| 4818 | if (!allcpus) |
| 4819 | return -ENOMEM; |
| 4820 | conf->percpu = allcpus; |
| 4821 | |
| 4822 | get_online_cpus(); |
| 4823 | err = 0; |
| 4824 | for_each_present_cpu(cpu) { |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4825 | if (conf->level == 6) { |
| 4826 | spare_page = alloc_page(GFP_KERNEL); |
| 4827 | if (!spare_page) { |
| 4828 | err = -ENOMEM; |
| 4829 | break; |
| 4830 | } |
| 4831 | per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page; |
| 4832 | } |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4833 | scribble = kmalloc(conf->scribble_len, GFP_KERNEL); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4834 | if (!scribble) { |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4835 | err = -ENOMEM; |
| 4836 | break; |
| 4837 | } |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4838 | per_cpu_ptr(conf->percpu, cpu)->scribble = scribble; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4839 | } |
| 4840 | #ifdef CONFIG_HOTPLUG_CPU |
| 4841 | conf->cpu_notify.notifier_call = raid456_cpu_notify; |
| 4842 | conf->cpu_notify.priority = 0; |
| 4843 | if (err == 0) |
| 4844 | err = register_cpu_notifier(&conf->cpu_notify); |
| 4845 | #endif |
| 4846 | put_online_cpus(); |
| 4847 | |
| 4848 | return err; |
| 4849 | } |
| 4850 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4851 | static struct r5conf *setup_conf(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4852 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4853 | struct r5conf *conf; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4854 | int raid_disk, memory, max_disks; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4855 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4856 | struct disk_info *disk; |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 4857 | char pers_name[6]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4858 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4859 | if (mddev->new_level != 5 |
| 4860 | && mddev->new_level != 4 |
| 4861 | && mddev->new_level != 6) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4862 | printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n", |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4863 | mdname(mddev), mddev->new_level); |
| 4864 | return ERR_PTR(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4865 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4866 | if ((mddev->new_level == 5 |
| 4867 | && !algorithm_valid_raid5(mddev->new_layout)) || |
| 4868 | (mddev->new_level == 6 |
| 4869 | && !algorithm_valid_raid6(mddev->new_layout))) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4870 | printk(KERN_ERR "md/raid:%s: layout %d not supported\n", |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4871 | mdname(mddev), mddev->new_layout); |
| 4872 | return ERR_PTR(-EIO); |
| 4873 | } |
| 4874 | if (mddev->new_level == 6 && mddev->raid_disks < 4) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4875 | printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n", |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4876 | mdname(mddev), mddev->raid_disks); |
| 4877 | return ERR_PTR(-EINVAL); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4878 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4879 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4880 | if (!mddev->new_chunk_sectors || |
| 4881 | (mddev->new_chunk_sectors << 9) % PAGE_SIZE || |
| 4882 | !is_power_of_2(mddev->new_chunk_sectors)) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4883 | printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n", |
| 4884 | mdname(mddev), mddev->new_chunk_sectors << 9); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4885 | return ERR_PTR(-EINVAL); |
NeilBrown | 4bbf377 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4886 | } |
| 4887 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4888 | conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4889 | if (conf == NULL) |
| 4890 | goto abort; |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 4891 | spin_lock_init(&conf->device_lock); |
| 4892 | init_waitqueue_head(&conf->wait_for_stripe); |
| 4893 | init_waitqueue_head(&conf->wait_for_overlap); |
| 4894 | INIT_LIST_HEAD(&conf->handle_list); |
| 4895 | INIT_LIST_HEAD(&conf->hold_list); |
| 4896 | INIT_LIST_HEAD(&conf->delayed_list); |
| 4897 | INIT_LIST_HEAD(&conf->bitmap_list); |
| 4898 | INIT_LIST_HEAD(&conf->inactive_list); |
| 4899 | atomic_set(&conf->active_stripes, 0); |
| 4900 | atomic_set(&conf->preread_active_stripes, 0); |
| 4901 | atomic_set(&conf->active_aligned_reads, 0); |
| 4902 | conf->bypass_threshold = BYPASS_THRESHOLD; |
NeilBrown | d890fa2 | 2011-10-26 11:54:39 +1100 | [diff] [blame] | 4903 | conf->recovery_disabled = mddev->recovery_disabled - 1; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4904 | |
| 4905 | conf->raid_disks = mddev->raid_disks; |
| 4906 | if (mddev->reshape_position == MaxSector) |
| 4907 | conf->previous_raid_disks = mddev->raid_disks; |
| 4908 | else |
| 4909 | conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4910 | max_disks = max(conf->raid_disks, conf->previous_raid_disks); |
| 4911 | conf->scribble_len = scribble_len(max_disks); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4912 | |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4913 | conf->disks = kzalloc(max_disks * sizeof(struct disk_info), |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4914 | GFP_KERNEL); |
| 4915 | if (!conf->disks) |
| 4916 | goto abort; |
| 4917 | |
| 4918 | conf->mddev = mddev; |
| 4919 | |
| 4920 | if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL) |
| 4921 | goto abort; |
| 4922 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4923 | conf->level = mddev->new_level; |
| 4924 | if (raid5_alloc_percpu(conf) != 0) |
| 4925 | goto abort; |
| 4926 | |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4927 | pr_debug("raid456: run(%s) called.\n", mdname(mddev)); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4928 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 4929 | rdev_for_each(rdev, mddev) { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4930 | raid_disk = rdev->raid_disk; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4931 | if (raid_disk >= max_disks |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4932 | || raid_disk < 0) |
| 4933 | continue; |
| 4934 | disk = conf->disks + raid_disk; |
| 4935 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4936 | if (test_bit(Replacement, &rdev->flags)) { |
| 4937 | if (disk->replacement) |
| 4938 | goto abort; |
| 4939 | disk->replacement = rdev; |
| 4940 | } else { |
| 4941 | if (disk->rdev) |
| 4942 | goto abort; |
| 4943 | disk->rdev = rdev; |
| 4944 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4945 | |
| 4946 | if (test_bit(In_sync, &rdev->flags)) { |
| 4947 | char b[BDEVNAME_SIZE]; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4948 | printk(KERN_INFO "md/raid:%s: device %s operational as raid" |
| 4949 | " disk %d\n", |
| 4950 | mdname(mddev), bdevname(rdev->bdev, b), raid_disk); |
Jonathan Brassow | d6b212f | 2011-06-08 18:00:28 -0500 | [diff] [blame] | 4951 | } else if (rdev->saved_raid_disk != raid_disk) |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4952 | /* Cannot rely on bitmap to complete recovery */ |
| 4953 | conf->fullsync = 1; |
| 4954 | } |
| 4955 | |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 4956 | conf->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4957 | conf->level = mddev->new_level; |
| 4958 | if (conf->level == 6) |
| 4959 | conf->max_degraded = 2; |
| 4960 | else |
| 4961 | conf->max_degraded = 1; |
| 4962 | conf->algorithm = mddev->new_layout; |
| 4963 | conf->max_nr_stripes = NR_STRIPES; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4964 | conf->reshape_progress = mddev->reshape_position; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 4965 | if (conf->reshape_progress != MaxSector) { |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 4966 | conf->prev_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 4967 | conf->prev_algo = mddev->layout; |
| 4968 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4969 | |
| 4970 | memory = conf->max_nr_stripes * (sizeof(struct stripe_head) + |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4971 | max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4972 | if (grow_stripes(conf, conf->max_nr_stripes)) { |
| 4973 | printk(KERN_ERR |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4974 | "md/raid:%s: couldn't allocate %dkB for buffers\n", |
| 4975 | mdname(mddev), memory); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4976 | goto abort; |
| 4977 | } else |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4978 | printk(KERN_INFO "md/raid:%s: allocated %dkB\n", |
| 4979 | mdname(mddev), memory); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4980 | |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 4981 | sprintf(pers_name, "raid%d", mddev->new_level); |
| 4982 | conf->thread = md_register_thread(raid5d, mddev, pers_name); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4983 | if (!conf->thread) { |
| 4984 | printk(KERN_ERR |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4985 | "md/raid:%s: couldn't allocate thread.\n", |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4986 | mdname(mddev)); |
| 4987 | goto abort; |
| 4988 | } |
| 4989 | |
| 4990 | return conf; |
| 4991 | |
| 4992 | abort: |
| 4993 | if (conf) { |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 4994 | free_conf(conf); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4995 | return ERR_PTR(-EIO); |
| 4996 | } else |
| 4997 | return ERR_PTR(-ENOMEM); |
| 4998 | } |
| 4999 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5000 | |
| 5001 | static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded) |
| 5002 | { |
| 5003 | switch (algo) { |
| 5004 | case ALGORITHM_PARITY_0: |
| 5005 | if (raid_disk < max_degraded) |
| 5006 | return 1; |
| 5007 | break; |
| 5008 | case ALGORITHM_PARITY_N: |
| 5009 | if (raid_disk >= raid_disks - max_degraded) |
| 5010 | return 1; |
| 5011 | break; |
| 5012 | case ALGORITHM_PARITY_0_6: |
| 5013 | if (raid_disk == 0 || |
| 5014 | raid_disk == raid_disks - 1) |
| 5015 | return 1; |
| 5016 | break; |
| 5017 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 5018 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 5019 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 5020 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 5021 | if (raid_disk == raid_disks - 1) |
| 5022 | return 1; |
| 5023 | } |
| 5024 | return 0; |
| 5025 | } |
| 5026 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5027 | static int run(struct mddev *mddev) |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5028 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5029 | struct r5conf *conf; |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 5030 | int working_disks = 0; |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5031 | int dirty_parity_disks = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5032 | struct md_rdev *rdev; |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5033 | sector_t reshape_offset = 0; |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5034 | int i; |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5035 | long long min_offset_diff = 0; |
| 5036 | int first = 1; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5037 | |
Andre Noll | 8c6ac868 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 5038 | if (mddev->recovery_cp != MaxSector) |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5039 | printk(KERN_NOTICE "md/raid:%s: not clean" |
Andre Noll | 8c6ac868 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 5040 | " -- starting background reconstruction\n", |
| 5041 | mdname(mddev)); |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5042 | |
| 5043 | rdev_for_each(rdev, mddev) { |
| 5044 | long long diff; |
| 5045 | if (rdev->raid_disk < 0) |
| 5046 | continue; |
| 5047 | diff = (rdev->new_data_offset - rdev->data_offset); |
| 5048 | if (first) { |
| 5049 | min_offset_diff = diff; |
| 5050 | first = 0; |
| 5051 | } else if (mddev->reshape_backwards && |
| 5052 | diff < min_offset_diff) |
| 5053 | min_offset_diff = diff; |
| 5054 | else if (!mddev->reshape_backwards && |
| 5055 | diff > min_offset_diff) |
| 5056 | min_offset_diff = diff; |
| 5057 | } |
| 5058 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5059 | if (mddev->reshape_position != MaxSector) { |
| 5060 | /* Check that we can continue the reshape. |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5061 | * Difficulties arise if the stripe we would write to |
| 5062 | * next is at or after the stripe we would read from next. |
| 5063 | * For a reshape that changes the number of devices, this |
| 5064 | * is only possible for a very short time, and mdadm makes |
| 5065 | * sure that time appears to have past before assembling |
| 5066 | * the array. So we fail if that time hasn't passed. |
| 5067 | * For a reshape that keeps the number of devices the same |
| 5068 | * mdadm must be monitoring the reshape can keeping the |
| 5069 | * critical areas read-only and backed up. It will start |
| 5070 | * the array in read-only mode, so we check for that. |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5071 | */ |
| 5072 | sector_t here_new, here_old; |
| 5073 | int old_disks; |
Andre Noll | 18b0033 | 2009-03-31 15:00:56 +1100 | [diff] [blame] | 5074 | int max_degraded = (mddev->level == 6 ? 2 : 1); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5075 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5076 | if (mddev->new_level != mddev->level) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5077 | printk(KERN_ERR "md/raid:%s: unsupported reshape " |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5078 | "required - aborting.\n", |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5079 | mdname(mddev)); |
| 5080 | return -EINVAL; |
| 5081 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5082 | old_disks = mddev->raid_disks - mddev->delta_disks; |
| 5083 | /* reshape_position must be on a new-stripe boundary, and one |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5084 | * further up in new geometry must map after here in old |
| 5085 | * geometry. |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5086 | */ |
| 5087 | here_new = mddev->reshape_position; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5088 | if (sector_div(here_new, mddev->new_chunk_sectors * |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5089 | (mddev->raid_disks - max_degraded))) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5090 | printk(KERN_ERR "md/raid:%s: reshape_position not " |
| 5091 | "on a stripe boundary\n", mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5092 | return -EINVAL; |
| 5093 | } |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5094 | reshape_offset = here_new * mddev->new_chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5095 | /* here_new is the stripe we will write to */ |
| 5096 | here_old = mddev->reshape_position; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5097 | sector_div(here_old, mddev->chunk_sectors * |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5098 | (old_disks-max_degraded)); |
| 5099 | /* here_old is the first stripe that we might need to read |
| 5100 | * from */ |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5101 | if (mddev->delta_disks == 0) { |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5102 | if ((here_new * mddev->new_chunk_sectors != |
| 5103 | here_old * mddev->chunk_sectors)) { |
| 5104 | printk(KERN_ERR "md/raid:%s: reshape position is" |
| 5105 | " confused - aborting\n", mdname(mddev)); |
| 5106 | return -EINVAL; |
| 5107 | } |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5108 | /* We cannot be sure it is safe to start an in-place |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5109 | * reshape. It is only safe if user-space is monitoring |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5110 | * and taking constant backups. |
| 5111 | * mdadm always starts a situation like this in |
| 5112 | * readonly mode so it can take control before |
| 5113 | * allowing any writes. So just check for that. |
| 5114 | */ |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5115 | if (abs(min_offset_diff) >= mddev->chunk_sectors && |
| 5116 | abs(min_offset_diff) >= mddev->new_chunk_sectors) |
| 5117 | /* not really in-place - so OK */; |
| 5118 | else if (mddev->ro == 0) { |
| 5119 | printk(KERN_ERR "md/raid:%s: in-place reshape " |
| 5120 | "must be started in read-only mode " |
| 5121 | "- aborting\n", |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5122 | mdname(mddev)); |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5123 | return -EINVAL; |
| 5124 | } |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5125 | } else if (mddev->reshape_backwards |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5126 | ? (here_new * mddev->new_chunk_sectors + min_offset_diff <= |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5127 | here_old * mddev->chunk_sectors) |
| 5128 | : (here_new * mddev->new_chunk_sectors >= |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5129 | here_old * mddev->chunk_sectors + (-min_offset_diff))) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5130 | /* Reading from the same stripe as writing to - bad */ |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5131 | printk(KERN_ERR "md/raid:%s: reshape_position too early for " |
| 5132 | "auto-recovery - aborting.\n", |
| 5133 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5134 | return -EINVAL; |
| 5135 | } |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5136 | printk(KERN_INFO "md/raid:%s: reshape will continue\n", |
| 5137 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5138 | /* OK, we should be able to continue; */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5139 | } else { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5140 | BUG_ON(mddev->level != mddev->new_level); |
| 5141 | BUG_ON(mddev->layout != mddev->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5142 | BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5143 | BUG_ON(mddev->delta_disks != 0); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5144 | } |
| 5145 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5146 | if (mddev->private == NULL) |
| 5147 | conf = setup_conf(mddev); |
| 5148 | else |
| 5149 | conf = mddev->private; |
| 5150 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5151 | if (IS_ERR(conf)) |
| 5152 | return PTR_ERR(conf); |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 5153 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5154 | conf->min_offset_diff = min_offset_diff; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5155 | mddev->thread = conf->thread; |
| 5156 | conf->thread = NULL; |
| 5157 | mddev->private = conf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5158 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5159 | for (i = 0; i < conf->raid_disks && conf->previous_raid_disks; |
| 5160 | i++) { |
| 5161 | rdev = conf->disks[i].rdev; |
| 5162 | if (!rdev && conf->disks[i].replacement) { |
| 5163 | /* The replacement is all we have yet */ |
| 5164 | rdev = conf->disks[i].replacement; |
| 5165 | conf->disks[i].replacement = NULL; |
| 5166 | clear_bit(Replacement, &rdev->flags); |
| 5167 | conf->disks[i].rdev = rdev; |
| 5168 | } |
| 5169 | if (!rdev) |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5170 | continue; |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5171 | if (conf->disks[i].replacement && |
| 5172 | conf->reshape_progress != MaxSector) { |
| 5173 | /* replacements and reshape simply do not mix. */ |
| 5174 | printk(KERN_ERR "md: cannot handle concurrent " |
| 5175 | "replacement and reshape.\n"); |
| 5176 | goto abort; |
| 5177 | } |
NeilBrown | 2f11588 | 2010-06-17 17:41:03 +1000 | [diff] [blame] | 5178 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5179 | working_disks++; |
NeilBrown | 2f11588 | 2010-06-17 17:41:03 +1000 | [diff] [blame] | 5180 | continue; |
| 5181 | } |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5182 | /* This disc is not fully in-sync. However if it |
| 5183 | * just stored parity (beyond the recovery_offset), |
| 5184 | * when we don't need to be concerned about the |
| 5185 | * array being dirty. |
| 5186 | * When reshape goes 'backwards', we never have |
| 5187 | * partially completed devices, so we only need |
| 5188 | * to worry about reshape going forwards. |
| 5189 | */ |
| 5190 | /* Hack because v0.91 doesn't store recovery_offset properly. */ |
| 5191 | if (mddev->major_version == 0 && |
| 5192 | mddev->minor_version > 90) |
| 5193 | rdev->recovery_offset = reshape_offset; |
| 5194 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5195 | if (rdev->recovery_offset < reshape_offset) { |
| 5196 | /* We need to check old and new layout */ |
| 5197 | if (!only_parity(rdev->raid_disk, |
| 5198 | conf->algorithm, |
| 5199 | conf->raid_disks, |
| 5200 | conf->max_degraded)) |
| 5201 | continue; |
| 5202 | } |
| 5203 | if (!only_parity(rdev->raid_disk, |
| 5204 | conf->prev_algo, |
| 5205 | conf->previous_raid_disks, |
| 5206 | conf->max_degraded)) |
| 5207 | continue; |
| 5208 | dirty_parity_disks++; |
| 5209 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5210 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5211 | /* |
| 5212 | * 0 for a fully functional array, 1 or 2 for a degraded array. |
| 5213 | */ |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 5214 | mddev->degraded = calc_degraded(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5215 | |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 5216 | if (has_failed(conf)) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5217 | printk(KERN_ERR "md/raid:%s: not enough operational devices" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5218 | " (%d/%d failed)\n", |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 5219 | mdname(mddev), mddev->degraded, conf->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5220 | goto abort; |
| 5221 | } |
| 5222 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5223 | /* device size must be a multiple of chunk size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5224 | mddev->dev_sectors &= ~(mddev->chunk_sectors - 1); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5225 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 5226 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5227 | if (mddev->degraded > dirty_parity_disks && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5228 | mddev->recovery_cp != MaxSector) { |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5229 | if (mddev->ok_start_degraded) |
| 5230 | printk(KERN_WARNING |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5231 | "md/raid:%s: starting dirty degraded array" |
| 5232 | " - data corruption possible.\n", |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5233 | mdname(mddev)); |
| 5234 | else { |
| 5235 | printk(KERN_ERR |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5236 | "md/raid:%s: cannot start dirty degraded array.\n", |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5237 | mdname(mddev)); |
| 5238 | goto abort; |
| 5239 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5240 | } |
| 5241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5242 | if (mddev->degraded == 0) |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5243 | printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d" |
| 5244 | " devices, algorithm %d\n", mdname(mddev), conf->level, |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 5245 | mddev->raid_disks-mddev->degraded, mddev->raid_disks, |
| 5246 | mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5247 | else |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5248 | printk(KERN_ALERT "md/raid:%s: raid level %d active with %d" |
| 5249 | " out of %d devices, algorithm %d\n", |
| 5250 | mdname(mddev), conf->level, |
| 5251 | mddev->raid_disks - mddev->degraded, |
| 5252 | mddev->raid_disks, mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5253 | |
| 5254 | print_raid5_conf(conf); |
| 5255 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5256 | if (conf->reshape_progress != MaxSector) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5257 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5258 | atomic_set(&conf->reshape_stripes, 0); |
| 5259 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 5260 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 5261 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 5262 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 5263 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 5264 | "reshape"); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5265 | } |
| 5266 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5267 | |
| 5268 | /* Ok, everything is just fine now */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 5269 | if (mddev->to_remove == &raid5_attrs_group) |
| 5270 | mddev->to_remove = NULL; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5271 | else if (mddev->kobj.sd && |
| 5272 | sysfs_create_group(&mddev->kobj, &raid5_attrs_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5273 | printk(KERN_WARNING |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 5274 | "raid5: failed to create sysfs attributes for %s\n", |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5275 | mdname(mddev)); |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 5276 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
| 5277 | |
| 5278 | if (mddev->queue) { |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 5279 | int chunk_size; |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 5280 | /* read-ahead size must cover two whole stripes, which |
| 5281 | * is 2 * (datadisks) * chunksize where 'n' is the |
| 5282 | * number of raid devices |
| 5283 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5284 | int data_disks = conf->previous_raid_disks - conf->max_degraded; |
| 5285 | int stripe = data_disks * |
| 5286 | ((mddev->chunk_sectors << 9) / PAGE_SIZE); |
| 5287 | if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 5288 | mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 5289 | |
| 5290 | blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec); |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 5291 | |
| 5292 | mddev->queue->backing_dev_info.congested_data = mddev; |
| 5293 | mddev->queue->backing_dev_info.congested_fn = raid5_congested; |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 5294 | |
| 5295 | chunk_size = mddev->chunk_sectors << 9; |
| 5296 | blk_queue_io_min(mddev->queue, chunk_size); |
| 5297 | blk_queue_io_opt(mddev->queue, chunk_size * |
| 5298 | (conf->raid_disks - conf->max_degraded)); |
| 5299 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5300 | rdev_for_each(rdev, mddev) { |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 5301 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 5302 | rdev->data_offset << 9); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5303 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 5304 | rdev->new_data_offset << 9); |
| 5305 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5306 | } |
| 5307 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5308 | return 0; |
| 5309 | abort: |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 5310 | md_unregister_thread(&mddev->thread); |
NeilBrown | e4f869d | 2011-10-07 14:22:49 +1100 | [diff] [blame] | 5311 | print_raid5_conf(conf); |
| 5312 | free_conf(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5313 | mddev->private = NULL; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5314 | printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5315 | return -EIO; |
| 5316 | } |
| 5317 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5318 | static int stop(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5319 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5320 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5321 | |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 5322 | md_unregister_thread(&mddev->thread); |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 5323 | if (mddev->queue) |
| 5324 | mddev->queue->backing_dev_info.congested_fn = NULL; |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 5325 | free_conf(conf); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 5326 | mddev->private = NULL; |
| 5327 | mddev->to_remove = &raid5_attrs_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5328 | return 0; |
| 5329 | } |
| 5330 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5331 | static void status(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5332 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5333 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5334 | int i; |
| 5335 | |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5336 | seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level, |
| 5337 | mddev->chunk_sectors / 2, mddev->layout); |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 5338 | 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] | 5339 | for (i = 0; i < conf->raid_disks; i++) |
| 5340 | seq_printf (seq, "%s", |
| 5341 | conf->disks[i].rdev && |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5342 | test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5343 | seq_printf (seq, "]"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5344 | } |
| 5345 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5346 | static void print_raid5_conf (struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5347 | { |
| 5348 | int i; |
| 5349 | struct disk_info *tmp; |
| 5350 | |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5351 | printk(KERN_DEBUG "RAID conf printout:\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5352 | if (!conf) { |
| 5353 | printk("(conf==NULL)\n"); |
| 5354 | return; |
| 5355 | } |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5356 | printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level, |
| 5357 | conf->raid_disks, |
| 5358 | conf->raid_disks - conf->mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5359 | |
| 5360 | for (i = 0; i < conf->raid_disks; i++) { |
| 5361 | char b[BDEVNAME_SIZE]; |
| 5362 | tmp = conf->disks + i; |
| 5363 | if (tmp->rdev) |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5364 | printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n", |
| 5365 | i, !test_bit(Faulty, &tmp->rdev->flags), |
| 5366 | bdevname(tmp->rdev->bdev, b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5367 | } |
| 5368 | } |
| 5369 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5370 | static int raid5_spare_active(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5371 | { |
| 5372 | int i; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5373 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5374 | struct disk_info *tmp; |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5375 | int count = 0; |
| 5376 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5377 | |
| 5378 | for (i = 0; i < conf->raid_disks; i++) { |
| 5379 | tmp = conf->disks + i; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5380 | if (tmp->replacement |
| 5381 | && tmp->replacement->recovery_offset == MaxSector |
| 5382 | && !test_bit(Faulty, &tmp->replacement->flags) |
| 5383 | && !test_and_set_bit(In_sync, &tmp->replacement->flags)) { |
| 5384 | /* Replacement has just become active. */ |
| 5385 | if (!tmp->rdev |
| 5386 | || !test_and_clear_bit(In_sync, &tmp->rdev->flags)) |
| 5387 | count++; |
| 5388 | if (tmp->rdev) { |
| 5389 | /* Replaced device not technically faulty, |
| 5390 | * but we need to be sure it gets removed |
| 5391 | * and never re-added. |
| 5392 | */ |
| 5393 | set_bit(Faulty, &tmp->rdev->flags); |
| 5394 | sysfs_notify_dirent_safe( |
| 5395 | tmp->rdev->sysfs_state); |
| 5396 | } |
| 5397 | sysfs_notify_dirent_safe(tmp->replacement->sysfs_state); |
| 5398 | } else if (tmp->rdev |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 5399 | && tmp->rdev->recovery_offset == MaxSector |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5400 | && !test_bit(Faulty, &tmp->rdev->flags) |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 5401 | && !test_and_set_bit(In_sync, &tmp->rdev->flags)) { |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5402 | count++; |
Jonathan Brassow | 43c73ca | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5403 | sysfs_notify_dirent_safe(tmp->rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5404 | } |
| 5405 | } |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5406 | spin_lock_irqsave(&conf->device_lock, flags); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 5407 | mddev->degraded = calc_degraded(conf); |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5408 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5409 | print_raid5_conf(conf); |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5410 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5411 | } |
| 5412 | |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 5413 | static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5414 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5415 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5416 | int err = 0; |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 5417 | int number = rdev->raid_disk; |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5418 | struct md_rdev **rdevp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5419 | struct disk_info *p = conf->disks + number; |
| 5420 | |
| 5421 | print_raid5_conf(conf); |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5422 | if (rdev == p->rdev) |
| 5423 | rdevp = &p->rdev; |
| 5424 | else if (rdev == p->replacement) |
| 5425 | rdevp = &p->replacement; |
| 5426 | else |
| 5427 | return 0; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5428 | |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5429 | if (number >= conf->raid_disks && |
| 5430 | conf->reshape_progress == MaxSector) |
| 5431 | clear_bit(In_sync, &rdev->flags); |
| 5432 | |
| 5433 | if (test_bit(In_sync, &rdev->flags) || |
| 5434 | atomic_read(&rdev->nr_pending)) { |
| 5435 | err = -EBUSY; |
| 5436 | goto abort; |
| 5437 | } |
| 5438 | /* Only remove non-faulty devices if recovery |
| 5439 | * isn't possible. |
| 5440 | */ |
| 5441 | if (!test_bit(Faulty, &rdev->flags) && |
| 5442 | mddev->recovery_disabled != conf->recovery_disabled && |
| 5443 | !has_failed(conf) && |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5444 | (!p->replacement || p->replacement == rdev) && |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5445 | number < conf->raid_disks) { |
| 5446 | err = -EBUSY; |
| 5447 | goto abort; |
| 5448 | } |
| 5449 | *rdevp = NULL; |
| 5450 | synchronize_rcu(); |
| 5451 | if (atomic_read(&rdev->nr_pending)) { |
| 5452 | /* lost the race, try later */ |
| 5453 | err = -EBUSY; |
| 5454 | *rdevp = rdev; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5455 | } else if (p->replacement) { |
| 5456 | /* We must have just cleared 'rdev' */ |
| 5457 | p->rdev = p->replacement; |
| 5458 | clear_bit(Replacement, &p->replacement->flags); |
| 5459 | smp_mb(); /* Make sure other CPUs may see both as identical |
| 5460 | * but will never see neither - if they are careful |
| 5461 | */ |
| 5462 | p->replacement = NULL; |
| 5463 | clear_bit(WantReplacement, &rdev->flags); |
| 5464 | } else |
| 5465 | /* We might have just removed the Replacement as faulty- |
| 5466 | * clear the bit just in case |
| 5467 | */ |
| 5468 | clear_bit(WantReplacement, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5469 | abort: |
| 5470 | |
| 5471 | print_raid5_conf(conf); |
| 5472 | return err; |
| 5473 | } |
| 5474 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5475 | static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5476 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5477 | struct r5conf *conf = mddev->private; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5478 | int err = -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5479 | int disk; |
| 5480 | struct disk_info *p; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5481 | int first = 0; |
| 5482 | int last = conf->raid_disks - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5483 | |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 5484 | if (mddev->recovery_disabled == conf->recovery_disabled) |
| 5485 | return -EBUSY; |
| 5486 | |
NeilBrown | dc10c64 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 5487 | if (rdev->saved_raid_disk < 0 && has_failed(conf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5488 | /* no point adding a device */ |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5489 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5490 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5491 | if (rdev->raid_disk >= 0) |
| 5492 | first = last = rdev->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5493 | |
| 5494 | /* |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5495 | * find the disk ... but prefer rdev->saved_raid_disk |
| 5496 | * if possible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5497 | */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5498 | if (rdev->saved_raid_disk >= 0 && |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5499 | rdev->saved_raid_disk >= first && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5500 | conf->disks[rdev->saved_raid_disk].rdev == NULL) |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 5501 | first = rdev->saved_raid_disk; |
| 5502 | |
| 5503 | for (disk = first; disk <= last; disk++) { |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5504 | p = conf->disks + disk; |
| 5505 | if (p->rdev == NULL) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5506 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5507 | rdev->raid_disk = disk; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5508 | err = 0; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5509 | if (rdev->saved_raid_disk != disk) |
| 5510 | conf->fullsync = 1; |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 5511 | rcu_assign_pointer(p->rdev, rdev); |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 5512 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5513 | } |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 5514 | } |
| 5515 | for (disk = first; disk <= last; disk++) { |
| 5516 | p = conf->disks + disk; |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5517 | if (test_bit(WantReplacement, &p->rdev->flags) && |
| 5518 | p->replacement == NULL) { |
| 5519 | clear_bit(In_sync, &rdev->flags); |
| 5520 | set_bit(Replacement, &rdev->flags); |
| 5521 | rdev->raid_disk = disk; |
| 5522 | err = 0; |
| 5523 | conf->fullsync = 1; |
| 5524 | rcu_assign_pointer(p->replacement, rdev); |
| 5525 | break; |
| 5526 | } |
| 5527 | } |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 5528 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5529 | print_raid5_conf(conf); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5530 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5531 | } |
| 5532 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5533 | static int raid5_resize(struct mddev *mddev, sector_t sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5534 | { |
| 5535 | /* no resync is happening, and there is enough space |
| 5536 | * on all devices, so we can resize. |
| 5537 | * We need to make sure resync covers any new space. |
| 5538 | * If the array is shrinking we should possibly wait until |
| 5539 | * any io in the removed space completes, but it hardly seems |
| 5540 | * worth it. |
| 5541 | */ |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 5542 | sector_t newsize; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5543 | sectors &= ~((sector_t)mddev->chunk_sectors - 1); |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 5544 | newsize = raid5_size(mddev, sectors, mddev->raid_disks); |
| 5545 | if (mddev->external_size && |
| 5546 | mddev->array_sectors > newsize) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5547 | return -EINVAL; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 5548 | if (mddev->bitmap) { |
| 5549 | int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0); |
| 5550 | if (ret) |
| 5551 | return ret; |
| 5552 | } |
| 5553 | md_set_array_sectors(mddev, newsize); |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 5554 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5555 | revalidate_disk(mddev->gendisk); |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 5556 | if (sectors > mddev->dev_sectors && |
| 5557 | mddev->recovery_cp > mddev->dev_sectors) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5558 | mddev->recovery_cp = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5559 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5560 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5561 | mddev->dev_sectors = sectors; |
NeilBrown | 4b5c7ae | 2005-07-27 11:43:28 -0700 | [diff] [blame] | 5562 | mddev->resync_max_sectors = sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5563 | return 0; |
| 5564 | } |
| 5565 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5566 | static int check_stripe_cache(struct mddev *mddev) |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5567 | { |
| 5568 | /* Can only proceed if there are plenty of stripe_heads. |
| 5569 | * We need a minimum of one full stripe,, and for sensible progress |
| 5570 | * it is best to have about 4 times that. |
| 5571 | * If we require 4 times, then the default 256 4K stripe_heads will |
| 5572 | * allow for chunk sizes up to 256K, which is probably OK. |
| 5573 | * If the chunk size is greater, user-space should request more |
| 5574 | * stripe_heads first. |
| 5575 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5576 | struct r5conf *conf = mddev->private; |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5577 | if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4 |
| 5578 | > conf->max_nr_stripes || |
| 5579 | ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4 |
| 5580 | > conf->max_nr_stripes) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5581 | printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n", |
| 5582 | mdname(mddev), |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5583 | ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9) |
| 5584 | / STRIPE_SIZE)*4); |
| 5585 | return 0; |
| 5586 | } |
| 5587 | return 1; |
| 5588 | } |
| 5589 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5590 | static int check_reshape(struct mddev *mddev) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5591 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5592 | struct r5conf *conf = mddev->private; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5593 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5594 | if (mddev->delta_disks == 0 && |
| 5595 | mddev->new_layout == mddev->layout && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5596 | mddev->new_chunk_sectors == mddev->chunk_sectors) |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5597 | return 0; /* nothing to do */ |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 5598 | if (has_failed(conf)) |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5599 | return -EINVAL; |
| 5600 | if (mddev->delta_disks < 0) { |
| 5601 | /* We might be able to shrink, but the devices must |
| 5602 | * be made bigger first. |
| 5603 | * For raid6, 4 is the minimum size. |
| 5604 | * Otherwise 2 is the minimum |
| 5605 | */ |
| 5606 | int min = 2; |
| 5607 | if (mddev->level == 6) |
| 5608 | min = 4; |
| 5609 | if (mddev->raid_disks + mddev->delta_disks < min) |
| 5610 | return -EINVAL; |
| 5611 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5612 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5613 | if (!check_stripe_cache(mddev)) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5614 | return -ENOSPC; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5615 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5616 | return resize_stripes(conf, conf->raid_disks + mddev->delta_disks); |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5617 | } |
| 5618 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5619 | static int raid5_start_reshape(struct mddev *mddev) |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5620 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5621 | struct r5conf *conf = mddev->private; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5622 | struct md_rdev *rdev; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5623 | int spares = 0; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 5624 | unsigned long flags; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5625 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5626 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5627 | return -EBUSY; |
| 5628 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5629 | if (!check_stripe_cache(mddev)) |
| 5630 | return -ENOSPC; |
| 5631 | |
NeilBrown | 30b6764 | 2012-05-22 13:55:28 +1000 | [diff] [blame] | 5632 | if (has_failed(conf)) |
| 5633 | return -EINVAL; |
| 5634 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5635 | rdev_for_each(rdev, mddev) { |
NeilBrown | 469518a | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5636 | if (!test_bit(In_sync, &rdev->flags) |
| 5637 | && !test_bit(Faulty, &rdev->flags)) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5638 | spares++; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5639 | } |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5640 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5641 | if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5642 | /* Not enough devices even to make a degraded array |
| 5643 | * of that size |
| 5644 | */ |
| 5645 | return -EINVAL; |
| 5646 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5647 | /* Refuse to reduce size of the array. Any reductions in |
| 5648 | * array size must be through explicit setting of array_size |
| 5649 | * attribute. |
| 5650 | */ |
| 5651 | if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks) |
| 5652 | < mddev->array_sectors) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5653 | printk(KERN_ERR "md/raid:%s: array size must be reduced " |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5654 | "before number of disks\n", mdname(mddev)); |
| 5655 | return -EINVAL; |
| 5656 | } |
| 5657 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5658 | atomic_set(&conf->reshape_stripes, 0); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5659 | spin_lock_irq(&conf->device_lock); |
| 5660 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5661 | conf->raid_disks += mddev->delta_disks; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5662 | conf->prev_chunk_sectors = conf->chunk_sectors; |
| 5663 | conf->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5664 | conf->prev_algo = conf->algorithm; |
| 5665 | conf->algorithm = mddev->new_layout; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5666 | conf->generation++; |
| 5667 | /* Code that selects data_offset needs to see the generation update |
| 5668 | * if reshape_progress has been set - so a memory barrier needed. |
| 5669 | */ |
| 5670 | smp_mb(); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5671 | if (mddev->reshape_backwards) |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5672 | conf->reshape_progress = raid5_size(mddev, 0, 0); |
| 5673 | else |
| 5674 | conf->reshape_progress = 0; |
| 5675 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5676 | spin_unlock_irq(&conf->device_lock); |
| 5677 | |
| 5678 | /* Add some new drives, as many as will fit. |
| 5679 | * We know there are enough to make the newly sized array work. |
NeilBrown | 3424bf6 | 2010-06-17 17:48:26 +1000 | [diff] [blame] | 5680 | * Don't add devices if we are reducing the number of |
| 5681 | * devices in the array. This is because it is not possible |
| 5682 | * to correctly record the "partially reconstructed" state of |
| 5683 | * such devices during the reshape and confusion could result. |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5684 | */ |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5685 | if (mddev->delta_disks >= 0) { |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5686 | rdev_for_each(rdev, mddev) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5687 | if (rdev->raid_disk < 0 && |
| 5688 | !test_bit(Faulty, &rdev->flags)) { |
| 5689 | if (raid5_add_disk(mddev, rdev) == 0) { |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5690 | if (rdev->raid_disk |
NeilBrown | 9d4c7d8 | 2012-03-13 11:21:21 +1100 | [diff] [blame] | 5691 | >= conf->previous_raid_disks) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5692 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 9d4c7d8 | 2012-03-13 11:21:21 +1100 | [diff] [blame] | 5693 | else |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5694 | rdev->recovery_offset = 0; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5695 | |
| 5696 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5697 | /* Failure here is OK */; |
NeilBrown | 50da084 | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5698 | } |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5699 | } else if (rdev->raid_disk >= conf->previous_raid_disks |
| 5700 | && !test_bit(Faulty, &rdev->flags)) { |
| 5701 | /* This is a spare that was manually added */ |
| 5702 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5703 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5704 | |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5705 | /* When a reshape changes the number of devices, |
| 5706 | * ->degraded is measured against the larger of the |
| 5707 | * pre and post number of devices. |
| 5708 | */ |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5709 | spin_lock_irqsave(&conf->device_lock, flags); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 5710 | mddev->degraded = calc_degraded(conf); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5711 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 5712 | } |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5713 | mddev->raid_disks = conf->raid_disks; |
NeilBrown | e516402 | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 5714 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5715 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5716 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5717 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 5718 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 5719 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 5720 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 5721 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 5722 | "reshape"); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5723 | if (!mddev->sync_thread) { |
| 5724 | mddev->recovery = 0; |
| 5725 | spin_lock_irq(&conf->device_lock); |
| 5726 | mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5727 | rdev_for_each(rdev, mddev) |
| 5728 | rdev->new_data_offset = rdev->data_offset; |
| 5729 | smp_wmb(); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5730 | conf->reshape_progress = MaxSector; |
NeilBrown | 1e3fa9b | 2012-03-13 11:21:18 +1100 | [diff] [blame] | 5731 | mddev->reshape_position = MaxSector; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5732 | spin_unlock_irq(&conf->device_lock); |
| 5733 | return -EAGAIN; |
| 5734 | } |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5735 | conf->reshape_checkpoint = jiffies; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5736 | md_wakeup_thread(mddev->sync_thread); |
| 5737 | md_new_event(mddev); |
| 5738 | return 0; |
| 5739 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5740 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5741 | /* This is called from the reshape thread and should make any |
| 5742 | * changes needed in 'conf' |
| 5743 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5744 | static void end_reshape(struct r5conf *conf) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5745 | { |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5746 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5747 | if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) { |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5748 | struct md_rdev *rdev; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 5749 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5750 | spin_lock_irq(&conf->device_lock); |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5751 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5752 | rdev_for_each(rdev, conf->mddev) |
| 5753 | rdev->data_offset = rdev->new_data_offset; |
| 5754 | smp_wmb(); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5755 | conf->reshape_progress = MaxSector; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5756 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 5757 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5758 | |
| 5759 | /* read-ahead size must cover two whole stripes, which is |
| 5760 | * 2 * (datadisks) * chunksize where 'n' is the number of raid devices |
| 5761 | */ |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 5762 | if (conf->mddev->queue) { |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5763 | int data_disks = conf->raid_disks - conf->max_degraded; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5764 | int stripe = data_disks * ((conf->chunk_sectors << 9) |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5765 | / PAGE_SIZE); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5766 | if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 5767 | conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
| 5768 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5769 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5770 | } |
| 5771 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5772 | /* This is called from the raid5d thread with mddev_lock held. |
| 5773 | * It makes config changes to the device. |
| 5774 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5775 | static void raid5_finish_reshape(struct mddev *mddev) |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5776 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5777 | struct r5conf *conf = mddev->private; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5778 | |
| 5779 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 5780 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5781 | if (mddev->delta_disks > 0) { |
| 5782 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
| 5783 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5784 | revalidate_disk(mddev->gendisk); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5785 | } else { |
| 5786 | int d; |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 5787 | spin_lock_irq(&conf->device_lock); |
| 5788 | mddev->degraded = calc_degraded(conf); |
| 5789 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5790 | for (d = conf->raid_disks ; |
| 5791 | d < conf->raid_disks - mddev->delta_disks; |
NeilBrown | 1a67dde | 2009-08-13 10:41:49 +1000 | [diff] [blame] | 5792 | d++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5793 | struct md_rdev *rdev = conf->disks[d].rdev; |
NeilBrown | da7613b | 2012-05-22 13:55:33 +1000 | [diff] [blame] | 5794 | if (rdev) |
| 5795 | clear_bit(In_sync, &rdev->flags); |
| 5796 | rdev = conf->disks[d].replacement; |
| 5797 | if (rdev) |
| 5798 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 1a67dde | 2009-08-13 10:41:49 +1000 | [diff] [blame] | 5799 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5800 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5801 | mddev->layout = conf->algorithm; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5802 | mddev->chunk_sectors = conf->chunk_sectors; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5803 | mddev->reshape_position = MaxSector; |
| 5804 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5805 | mddev->reshape_backwards = 0; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5806 | } |
| 5807 | } |
| 5808 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5809 | static void raid5_quiesce(struct mddev *mddev, int state) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5810 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5811 | struct r5conf *conf = mddev->private; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5812 | |
| 5813 | switch(state) { |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5814 | case 2: /* resume for a suspend */ |
| 5815 | wake_up(&conf->wait_for_overlap); |
| 5816 | break; |
| 5817 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5818 | case 1: /* stop all writes */ |
| 5819 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5820 | /* '2' tells resync/reshape to pause so that all |
| 5821 | * active stripes can drain |
| 5822 | */ |
| 5823 | conf->quiesce = 2; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5824 | wait_event_lock_irq(conf->wait_for_stripe, |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5825 | atomic_read(&conf->active_stripes) == 0 && |
| 5826 | atomic_read(&conf->active_aligned_reads) == 0, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5827 | conf->device_lock, /* nothing */); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5828 | conf->quiesce = 1; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5829 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5830 | /* allow reshape to continue */ |
| 5831 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5832 | break; |
| 5833 | |
| 5834 | case 0: /* re-enable writes */ |
| 5835 | spin_lock_irq(&conf->device_lock); |
| 5836 | conf->quiesce = 0; |
| 5837 | wake_up(&conf->wait_for_stripe); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5838 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5839 | spin_unlock_irq(&conf->device_lock); |
| 5840 | break; |
| 5841 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5842 | } |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5843 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5844 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5845 | static void *raid45_takeover_raid0(struct mddev *mddev, int level) |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 5846 | { |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 5847 | struct r0conf *raid0_conf = mddev->private; |
Randy Dunlap | d76c842 | 2011-04-21 09:07:26 -0700 | [diff] [blame] | 5848 | sector_t sectors; |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 5849 | |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5850 | /* for raid0 takeover only one zone is supported */ |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 5851 | if (raid0_conf->nr_strip_zones > 1) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5852 | printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n", |
| 5853 | mdname(mddev)); |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5854 | return ERR_PTR(-EINVAL); |
| 5855 | } |
| 5856 | |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 5857 | sectors = raid0_conf->strip_zone[0].zone_end; |
| 5858 | sector_div(sectors, raid0_conf->strip_zone[0].nb_dev); |
NeilBrown | 3b71bd9 | 2011-04-20 15:38:18 +1000 | [diff] [blame] | 5859 | mddev->dev_sectors = sectors; |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5860 | mddev->new_level = level; |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 5861 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 5862 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
| 5863 | mddev->raid_disks += 1; |
| 5864 | mddev->delta_disks = 1; |
| 5865 | /* make sure it will be not marked as dirty */ |
| 5866 | mddev->recovery_cp = MaxSector; |
| 5867 | |
| 5868 | return setup_conf(mddev); |
| 5869 | } |
| 5870 | |
| 5871 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5872 | static void *raid5_takeover_raid1(struct mddev *mddev) |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5873 | { |
| 5874 | int chunksect; |
| 5875 | |
| 5876 | if (mddev->raid_disks != 2 || |
| 5877 | mddev->degraded > 1) |
| 5878 | return ERR_PTR(-EINVAL); |
| 5879 | |
| 5880 | /* Should check if there are write-behind devices? */ |
| 5881 | |
| 5882 | chunksect = 64*2; /* 64K by default */ |
| 5883 | |
| 5884 | /* The array must be an exact multiple of chunksize */ |
| 5885 | while (chunksect && (mddev->array_sectors & (chunksect-1))) |
| 5886 | chunksect >>= 1; |
| 5887 | |
| 5888 | if ((chunksect<<9) < STRIPE_SIZE) |
| 5889 | /* array size does not allow a suitable chunk size */ |
| 5890 | return ERR_PTR(-EINVAL); |
| 5891 | |
| 5892 | mddev->new_level = 5; |
| 5893 | mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5894 | mddev->new_chunk_sectors = chunksect; |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5895 | |
| 5896 | return setup_conf(mddev); |
| 5897 | } |
| 5898 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5899 | static void *raid5_takeover_raid6(struct mddev *mddev) |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 5900 | { |
| 5901 | int new_layout; |
| 5902 | |
| 5903 | switch (mddev->layout) { |
| 5904 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 5905 | new_layout = ALGORITHM_LEFT_ASYMMETRIC; |
| 5906 | break; |
| 5907 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 5908 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC; |
| 5909 | break; |
| 5910 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 5911 | new_layout = ALGORITHM_LEFT_SYMMETRIC; |
| 5912 | break; |
| 5913 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 5914 | new_layout = ALGORITHM_RIGHT_SYMMETRIC; |
| 5915 | break; |
| 5916 | case ALGORITHM_PARITY_0_6: |
| 5917 | new_layout = ALGORITHM_PARITY_0; |
| 5918 | break; |
| 5919 | case ALGORITHM_PARITY_N: |
| 5920 | new_layout = ALGORITHM_PARITY_N; |
| 5921 | break; |
| 5922 | default: |
| 5923 | return ERR_PTR(-EINVAL); |
| 5924 | } |
| 5925 | mddev->new_level = 5; |
| 5926 | mddev->new_layout = new_layout; |
| 5927 | mddev->delta_disks = -1; |
| 5928 | mddev->raid_disks -= 1; |
| 5929 | return setup_conf(mddev); |
| 5930 | } |
| 5931 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5932 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5933 | static int raid5_check_reshape(struct mddev *mddev) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5934 | { |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5935 | /* For a 2-drive array, the layout and chunk size can be changed |
| 5936 | * immediately as not restriping is needed. |
| 5937 | * For larger arrays we record the new value - after validation |
| 5938 | * to be used by a reshape pass. |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5939 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5940 | struct r5conf *conf = mddev->private; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5941 | int new_chunk = mddev->new_chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5942 | |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5943 | if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5944 | return -EINVAL; |
| 5945 | if (new_chunk > 0) { |
Andre Noll | 0ba459d | 2009-06-18 08:46:10 +1000 | [diff] [blame] | 5946 | if (!is_power_of_2(new_chunk)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5947 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5948 | if (new_chunk < (PAGE_SIZE>>9)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5949 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5950 | if (mddev->array_sectors & (new_chunk-1)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5951 | /* not factor of array size */ |
| 5952 | return -EINVAL; |
| 5953 | } |
| 5954 | |
| 5955 | /* They look valid */ |
| 5956 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5957 | if (mddev->raid_disks == 2) { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5958 | /* can make the change immediately */ |
| 5959 | if (mddev->new_layout >= 0) { |
| 5960 | conf->algorithm = mddev->new_layout; |
| 5961 | mddev->layout = mddev->new_layout; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5962 | } |
| 5963 | if (new_chunk > 0) { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5964 | conf->chunk_sectors = new_chunk ; |
| 5965 | mddev->chunk_sectors = new_chunk; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5966 | } |
| 5967 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 5968 | md_wakeup_thread(mddev->thread); |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5969 | } |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5970 | return check_reshape(mddev); |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5971 | } |
| 5972 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5973 | static int raid6_check_reshape(struct mddev *mddev) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5974 | { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5975 | int new_chunk = mddev->new_chunk_sectors; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5976 | |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5977 | if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5978 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5979 | if (new_chunk > 0) { |
Andre Noll | 0ba459d | 2009-06-18 08:46:10 +1000 | [diff] [blame] | 5980 | if (!is_power_of_2(new_chunk)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5981 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5982 | if (new_chunk < (PAGE_SIZE >> 9)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5983 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5984 | if (mddev->array_sectors & (new_chunk-1)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5985 | /* not factor of array size */ |
| 5986 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5987 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5988 | |
| 5989 | /* They look valid */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5990 | return check_reshape(mddev); |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5991 | } |
| 5992 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5993 | static void *raid5_takeover(struct mddev *mddev) |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5994 | { |
| 5995 | /* raid5 can take over: |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5996 | * raid0 - if there is only one strip zone - make it a raid4 layout |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5997 | * raid1 - if there are two drives. We need to know the chunk size |
| 5998 | * raid4 - trivial - just use a raid4 layout. |
| 5999 | * raid6 - Providing it is a *_6 layout |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6000 | */ |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6001 | if (mddev->level == 0) |
| 6002 | return raid45_takeover_raid0(mddev, 5); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6003 | if (mddev->level == 1) |
| 6004 | return raid5_takeover_raid1(mddev); |
NeilBrown | e9d4758 | 2009-03-31 14:57:09 +1100 | [diff] [blame] | 6005 | if (mddev->level == 4) { |
| 6006 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 6007 | mddev->new_level = 5; |
| 6008 | return setup_conf(mddev); |
| 6009 | } |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 6010 | if (mddev->level == 6) |
| 6011 | return raid5_takeover_raid6(mddev); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6012 | |
| 6013 | return ERR_PTR(-EINVAL); |
| 6014 | } |
| 6015 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6016 | static void *raid4_takeover(struct mddev *mddev) |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 6017 | { |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6018 | /* raid4 can take over: |
| 6019 | * raid0 - if there is only one strip zone |
| 6020 | * raid5 - if layout is right |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 6021 | */ |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6022 | if (mddev->level == 0) |
| 6023 | return raid45_takeover_raid0(mddev, 4); |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 6024 | if (mddev->level == 5 && |
| 6025 | mddev->layout == ALGORITHM_PARITY_N) { |
| 6026 | mddev->new_layout = 0; |
| 6027 | mddev->new_level = 4; |
| 6028 | return setup_conf(mddev); |
| 6029 | } |
| 6030 | return ERR_PTR(-EINVAL); |
| 6031 | } |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6032 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6033 | static struct md_personality raid5_personality; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6034 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6035 | static void *raid6_takeover(struct mddev *mddev) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6036 | { |
| 6037 | /* Currently can only take over a raid5. We map the |
| 6038 | * personality to an equivalent raid6 personality |
| 6039 | * with the Q block at the end. |
| 6040 | */ |
| 6041 | int new_layout; |
| 6042 | |
| 6043 | if (mddev->pers != &raid5_personality) |
| 6044 | return ERR_PTR(-EINVAL); |
| 6045 | if (mddev->degraded > 1) |
| 6046 | return ERR_PTR(-EINVAL); |
| 6047 | if (mddev->raid_disks > 253) |
| 6048 | return ERR_PTR(-EINVAL); |
| 6049 | if (mddev->raid_disks < 3) |
| 6050 | return ERR_PTR(-EINVAL); |
| 6051 | |
| 6052 | switch (mddev->layout) { |
| 6053 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 6054 | new_layout = ALGORITHM_LEFT_ASYMMETRIC_6; |
| 6055 | break; |
| 6056 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 6057 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6; |
| 6058 | break; |
| 6059 | case ALGORITHM_LEFT_SYMMETRIC: |
| 6060 | new_layout = ALGORITHM_LEFT_SYMMETRIC_6; |
| 6061 | break; |
| 6062 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 6063 | new_layout = ALGORITHM_RIGHT_SYMMETRIC_6; |
| 6064 | break; |
| 6065 | case ALGORITHM_PARITY_0: |
| 6066 | new_layout = ALGORITHM_PARITY_0_6; |
| 6067 | break; |
| 6068 | case ALGORITHM_PARITY_N: |
| 6069 | new_layout = ALGORITHM_PARITY_N; |
| 6070 | break; |
| 6071 | default: |
| 6072 | return ERR_PTR(-EINVAL); |
| 6073 | } |
| 6074 | mddev->new_level = 6; |
| 6075 | mddev->new_layout = new_layout; |
| 6076 | mddev->delta_disks = 1; |
| 6077 | mddev->raid_disks += 1; |
| 6078 | return setup_conf(mddev); |
| 6079 | } |
| 6080 | |
| 6081 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6082 | static struct md_personality raid6_personality = |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6083 | { |
| 6084 | .name = "raid6", |
| 6085 | .level = 6, |
| 6086 | .owner = THIS_MODULE, |
| 6087 | .make_request = make_request, |
| 6088 | .run = run, |
| 6089 | .stop = stop, |
| 6090 | .status = status, |
| 6091 | .error_handler = error, |
| 6092 | .hot_add_disk = raid5_add_disk, |
| 6093 | .hot_remove_disk= raid5_remove_disk, |
| 6094 | .spare_active = raid5_spare_active, |
| 6095 | .sync_request = sync_request, |
| 6096 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6097 | .size = raid5_size, |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6098 | .check_reshape = raid6_check_reshape, |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 6099 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6100 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6101 | .quiesce = raid5_quiesce, |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6102 | .takeover = raid6_takeover, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6103 | }; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6104 | static struct md_personality raid5_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6105 | { |
| 6106 | .name = "raid5", |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6107 | .level = 5, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6108 | .owner = THIS_MODULE, |
| 6109 | .make_request = make_request, |
| 6110 | .run = run, |
| 6111 | .stop = stop, |
| 6112 | .status = status, |
| 6113 | .error_handler = error, |
| 6114 | .hot_add_disk = raid5_add_disk, |
| 6115 | .hot_remove_disk= raid5_remove_disk, |
| 6116 | .spare_active = raid5_spare_active, |
| 6117 | .sync_request = sync_request, |
| 6118 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6119 | .size = raid5_size, |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6120 | .check_reshape = raid5_check_reshape, |
| 6121 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6122 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6123 | .quiesce = raid5_quiesce, |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6124 | .takeover = raid5_takeover, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6125 | }; |
| 6126 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6127 | static struct md_personality raid4_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6128 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6129 | .name = "raid4", |
| 6130 | .level = 4, |
| 6131 | .owner = THIS_MODULE, |
| 6132 | .make_request = make_request, |
| 6133 | .run = run, |
| 6134 | .stop = stop, |
| 6135 | .status = status, |
| 6136 | .error_handler = error, |
| 6137 | .hot_add_disk = raid5_add_disk, |
| 6138 | .hot_remove_disk= raid5_remove_disk, |
| 6139 | .spare_active = raid5_spare_active, |
| 6140 | .sync_request = sync_request, |
| 6141 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6142 | .size = raid5_size, |
NeilBrown | 3d37890 | 2007-03-26 21:32:13 -0800 | [diff] [blame] | 6143 | .check_reshape = raid5_check_reshape, |
| 6144 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6145 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6146 | .quiesce = raid5_quiesce, |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 6147 | .takeover = raid4_takeover, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6148 | }; |
| 6149 | |
| 6150 | static int __init raid5_init(void) |
| 6151 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6152 | register_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6153 | register_md_personality(&raid5_personality); |
| 6154 | register_md_personality(&raid4_personality); |
| 6155 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6156 | } |
| 6157 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6158 | static void raid5_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6159 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6160 | unregister_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6161 | unregister_md_personality(&raid5_personality); |
| 6162 | unregister_md_personality(&raid4_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6163 | } |
| 6164 | |
| 6165 | module_init(raid5_init); |
| 6166 | module_exit(raid5_exit); |
| 6167 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 6168 | MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6169 | MODULE_ALIAS("md-personality-4"); /* RAID5 */ |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 6170 | MODULE_ALIAS("md-raid5"); |
| 6171 | MODULE_ALIAS("md-raid4"); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6172 | MODULE_ALIAS("md-level-5"); |
| 6173 | MODULE_ALIAS("md-level-4"); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6174 | MODULE_ALIAS("md-personality-8"); /* RAID6 */ |
| 6175 | MODULE_ALIAS("md-raid6"); |
| 6176 | MODULE_ALIAS("md-level-6"); |
| 6177 | |
| 6178 | /* This used to be two separate modules, they were: */ |
| 6179 | MODULE_ALIAS("raid5"); |
| 6180 | MODULE_ALIAS("raid6"); |