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) |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 487 | && !test_bit(STRIPE_EXPANDING, &sh->state) |
| 488 | && !test_bit(STRIPE_ON_UNPLUG_LIST, &sh->state)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | } else { |
| 490 | if (!test_bit(STRIPE_HANDLE, &sh->state)) |
| 491 | atomic_inc(&conf->active_stripes); |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 492 | if (list_empty(&sh->lru) && |
| 493 | !test_bit(STRIPE_EXPANDING, &sh->state)) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 494 | BUG(); |
| 495 | list_del_init(&sh->lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | } while (sh == NULL); |
| 499 | |
| 500 | if (sh) |
| 501 | atomic_inc(&sh->count); |
| 502 | |
| 503 | spin_unlock_irq(&conf->device_lock); |
| 504 | return sh; |
| 505 | } |
| 506 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 507 | /* Determine if 'data_offset' or 'new_data_offset' should be used |
| 508 | * in this stripe_head. |
| 509 | */ |
| 510 | static int use_new_offset(struct r5conf *conf, struct stripe_head *sh) |
| 511 | { |
| 512 | sector_t progress = conf->reshape_progress; |
| 513 | /* Need a memory barrier to make sure we see the value |
| 514 | * of conf->generation, or ->data_offset that was set before |
| 515 | * reshape_progress was updated. |
| 516 | */ |
| 517 | smp_rmb(); |
| 518 | if (progress == MaxSector) |
| 519 | return 0; |
| 520 | if (sh->generation == conf->generation - 1) |
| 521 | return 0; |
| 522 | /* We are in a reshape, and this is a new-generation stripe, |
| 523 | * so use new_data_offset. |
| 524 | */ |
| 525 | return 1; |
| 526 | } |
| 527 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 528 | static void |
| 529 | raid5_end_read_request(struct bio *bi, int error); |
| 530 | static void |
| 531 | raid5_end_write_request(struct bio *bi, int error); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 532 | |
Dan Williams | c4e5ac0 | 2008-06-28 08:31:53 +1000 | [diff] [blame] | 533 | 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] | 534 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 535 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 536 | int i, disks = sh->disks; |
| 537 | |
| 538 | might_sleep(); |
| 539 | |
| 540 | for (i = disks; i--; ) { |
| 541 | int rw; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 542 | int replace_only = 0; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 543 | struct bio *bi, *rbi; |
| 544 | struct md_rdev *rdev, *rrdev = NULL; |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 545 | if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) { |
| 546 | if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags)) |
| 547 | rw = WRITE_FUA; |
| 548 | else |
| 549 | rw = WRITE; |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 550 | if (test_bit(R5_Discard, &sh->dev[i].flags)) |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 551 | rw |= REQ_DISCARD; |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 552 | } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 553 | rw = READ; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 554 | else if (test_and_clear_bit(R5_WantReplace, |
| 555 | &sh->dev[i].flags)) { |
| 556 | rw = WRITE; |
| 557 | replace_only = 1; |
| 558 | } else |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 559 | continue; |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 560 | if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags)) |
| 561 | rw |= REQ_SYNC; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 562 | |
| 563 | bi = &sh->dev[i].req; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 564 | rbi = &sh->dev[i].rreq; /* For writing to replacement */ |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 565 | |
| 566 | bi->bi_rw = rw; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 567 | rbi->bi_rw = rw; |
| 568 | if (rw & WRITE) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 569 | bi->bi_end_io = raid5_end_write_request; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 570 | rbi->bi_end_io = raid5_end_write_request; |
| 571 | } else |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 572 | bi->bi_end_io = raid5_end_read_request; |
| 573 | |
| 574 | rcu_read_lock(); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 575 | rrdev = rcu_dereference(conf->disks[i].replacement); |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 576 | smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */ |
| 577 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 578 | if (!rdev) { |
| 579 | rdev = rrdev; |
| 580 | rrdev = NULL; |
| 581 | } |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 582 | if (rw & WRITE) { |
| 583 | if (replace_only) |
| 584 | rdev = NULL; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 585 | if (rdev == rrdev) |
| 586 | /* We raced and saw duplicates */ |
| 587 | rrdev = NULL; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 588 | } else { |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 589 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 590 | rdev = rrdev; |
| 591 | rrdev = NULL; |
| 592 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 593 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 594 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 595 | rdev = NULL; |
| 596 | if (rdev) |
| 597 | atomic_inc(&rdev->nr_pending); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 598 | if (rrdev && test_bit(Faulty, &rrdev->flags)) |
| 599 | rrdev = NULL; |
| 600 | if (rrdev) |
| 601 | atomic_inc(&rrdev->nr_pending); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 602 | rcu_read_unlock(); |
| 603 | |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 604 | /* We have already checked bad blocks for reads. Now |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 605 | * need to check for writes. We never accept write errors |
| 606 | * on the replacement, so we don't to check rrdev. |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 607 | */ |
| 608 | while ((rw & WRITE) && rdev && |
| 609 | test_bit(WriteErrorSeen, &rdev->flags)) { |
| 610 | sector_t first_bad; |
| 611 | int bad_sectors; |
| 612 | int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 613 | &first_bad, &bad_sectors); |
| 614 | if (!bad) |
| 615 | break; |
| 616 | |
| 617 | if (bad < 0) { |
| 618 | set_bit(BlockedBadBlocks, &rdev->flags); |
| 619 | if (!conf->mddev->external && |
| 620 | conf->mddev->flags) { |
| 621 | /* It is very unlikely, but we might |
| 622 | * still need to write out the |
| 623 | * bad block log - better give it |
| 624 | * a chance*/ |
| 625 | md_check_recovery(conf->mddev); |
| 626 | } |
majianpeng | 1850753 | 2012-07-03 12:11:54 +1000 | [diff] [blame] | 627 | /* |
| 628 | * Because md_wait_for_blocked_rdev |
| 629 | * will dec nr_pending, we must |
| 630 | * increment it first. |
| 631 | */ |
| 632 | atomic_inc(&rdev->nr_pending); |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 633 | md_wait_for_blocked_rdev(rdev, conf->mddev); |
| 634 | } else { |
| 635 | /* Acknowledged bad block - skip the write */ |
| 636 | rdev_dec_pending(rdev, conf->mddev); |
| 637 | rdev = NULL; |
| 638 | } |
| 639 | } |
| 640 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 641 | if (rdev) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 642 | if (s->syncing || s->expanding || s->expanded |
| 643 | || s->replacing) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 644 | md_sync_acct(rdev->bdev, STRIPE_SECTORS); |
| 645 | |
Dan Williams | 2b7497f | 2008-06-28 08:31:52 +1000 | [diff] [blame] | 646 | set_bit(STRIPE_IO_STARTED, &sh->state); |
| 647 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 648 | bi->bi_bdev = rdev->bdev; |
| 649 | pr_debug("%s: for %llu schedule op %ld on disc %d\n", |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 650 | __func__, (unsigned long long)sh->sector, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 651 | bi->bi_rw, i); |
| 652 | atomic_inc(&sh->count); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 653 | if (use_new_offset(conf, sh)) |
| 654 | bi->bi_sector = (sh->sector |
| 655 | + rdev->new_data_offset); |
| 656 | else |
| 657 | bi->bi_sector = (sh->sector |
| 658 | + rdev->data_offset); |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 659 | if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) |
| 660 | bi->bi_rw |= REQ_FLUSH; |
| 661 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 662 | bi->bi_flags = 1 << BIO_UPTODATE; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 663 | bi->bi_idx = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 664 | bi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
| 665 | bi->bi_io_vec[0].bv_offset = 0; |
| 666 | bi->bi_size = STRIPE_SIZE; |
| 667 | bi->bi_next = NULL; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 668 | if (rrdev) |
| 669 | set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 670 | generic_make_request(bi); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 671 | } |
| 672 | if (rrdev) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 673 | if (s->syncing || s->expanding || s->expanded |
| 674 | || s->replacing) |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 675 | md_sync_acct(rrdev->bdev, STRIPE_SECTORS); |
| 676 | |
| 677 | set_bit(STRIPE_IO_STARTED, &sh->state); |
| 678 | |
| 679 | rbi->bi_bdev = rrdev->bdev; |
| 680 | pr_debug("%s: for %llu schedule op %ld on " |
| 681 | "replacement disc %d\n", |
| 682 | __func__, (unsigned long long)sh->sector, |
| 683 | rbi->bi_rw, i); |
| 684 | atomic_inc(&sh->count); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 685 | if (use_new_offset(conf, sh)) |
| 686 | rbi->bi_sector = (sh->sector |
| 687 | + rrdev->new_data_offset); |
| 688 | else |
| 689 | rbi->bi_sector = (sh->sector |
| 690 | + rrdev->data_offset); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 691 | rbi->bi_flags = 1 << BIO_UPTODATE; |
| 692 | rbi->bi_idx = 0; |
| 693 | rbi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
| 694 | rbi->bi_io_vec[0].bv_offset = 0; |
| 695 | rbi->bi_size = STRIPE_SIZE; |
| 696 | rbi->bi_next = NULL; |
| 697 | generic_make_request(rbi); |
| 698 | } |
| 699 | if (!rdev && !rrdev) { |
Namhyung Kim | b062962 | 2011-06-14 14:20:19 +1000 | [diff] [blame] | 700 | if (rw & WRITE) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 701 | set_bit(STRIPE_DEGRADED, &sh->state); |
| 702 | pr_debug("skip op %ld on disc %d for sector %llu\n", |
| 703 | bi->bi_rw, i, (unsigned long long)sh->sector); |
| 704 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 705 | set_bit(STRIPE_HANDLE, &sh->state); |
| 706 | } |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | static struct dma_async_tx_descriptor * |
| 711 | async_copy_data(int frombio, struct bio *bio, struct page *page, |
| 712 | sector_t sector, struct dma_async_tx_descriptor *tx) |
| 713 | { |
| 714 | struct bio_vec *bvl; |
| 715 | struct page *bio_page; |
| 716 | int i; |
| 717 | int page_offset; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 718 | struct async_submit_ctl submit; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 719 | enum async_tx_flags flags = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 720 | |
| 721 | if (bio->bi_sector >= sector) |
| 722 | page_offset = (signed)(bio->bi_sector - sector) * 512; |
| 723 | else |
| 724 | page_offset = (signed)(sector - bio->bi_sector) * -512; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 725 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 726 | if (frombio) |
| 727 | flags |= ASYNC_TX_FENCE; |
| 728 | init_async_submit(&submit, flags, tx, NULL, NULL, NULL); |
| 729 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 730 | bio_for_each_segment(bvl, bio, i) { |
Namhyung Kim | fcde907 | 2011-06-14 14:23:57 +1000 | [diff] [blame] | 731 | int len = bvl->bv_len; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 732 | int clen; |
| 733 | int b_offset = 0; |
| 734 | |
| 735 | if (page_offset < 0) { |
| 736 | b_offset = -page_offset; |
| 737 | page_offset += b_offset; |
| 738 | len -= b_offset; |
| 739 | } |
| 740 | |
| 741 | if (len > 0 && page_offset + len > STRIPE_SIZE) |
| 742 | clen = STRIPE_SIZE - page_offset; |
| 743 | else |
| 744 | clen = len; |
| 745 | |
| 746 | if (clen > 0) { |
Namhyung Kim | fcde907 | 2011-06-14 14:23:57 +1000 | [diff] [blame] | 747 | b_offset += bvl->bv_offset; |
| 748 | bio_page = bvl->bv_page; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 749 | if (frombio) |
| 750 | tx = async_memcpy(page, bio_page, page_offset, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 751 | b_offset, clen, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 752 | else |
| 753 | tx = async_memcpy(bio_page, page, b_offset, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 754 | page_offset, clen, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 755 | } |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 756 | /* chain the operations */ |
| 757 | submit.depend_tx = tx; |
| 758 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 759 | if (clen < len) /* hit end of page */ |
| 760 | break; |
| 761 | page_offset += len; |
| 762 | } |
| 763 | |
| 764 | return tx; |
| 765 | } |
| 766 | |
| 767 | static void ops_complete_biofill(void *stripe_head_ref) |
| 768 | { |
| 769 | struct stripe_head *sh = stripe_head_ref; |
| 770 | struct bio *return_bi = NULL; |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 771 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 772 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 773 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 774 | (unsigned long long)sh->sector); |
| 775 | |
| 776 | /* clear completed biofills */ |
| 777 | for (i = sh->disks; i--; ) { |
| 778 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 779 | |
| 780 | /* acknowledge completion of a biofill operation */ |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 781 | /* and check if we need to reply to a read request, |
| 782 | * new R5_Wantfill requests are held off until |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 783 | * !STRIPE_BIOFILL_RUN |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 784 | */ |
| 785 | if (test_and_clear_bit(R5_Wantfill, &dev->flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 786 | struct bio *rbi, *rbi2; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 787 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 788 | BUG_ON(!dev->read); |
| 789 | rbi = dev->read; |
| 790 | dev->read = NULL; |
| 791 | while (rbi && rbi->bi_sector < |
| 792 | dev->sector + STRIPE_SECTORS) { |
| 793 | rbi2 = r5_next_bio(rbi, dev->sector); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 794 | if (!raid5_dec_bi_active_stripes(rbi)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 795 | rbi->bi_next = return_bi; |
| 796 | return_bi = rbi; |
| 797 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 798 | rbi = rbi2; |
| 799 | } |
| 800 | } |
| 801 | } |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 802 | clear_bit(STRIPE_BIOFILL_RUN, &sh->state); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 803 | |
| 804 | return_io(return_bi); |
| 805 | |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 806 | set_bit(STRIPE_HANDLE, &sh->state); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 807 | release_stripe(sh); |
| 808 | } |
| 809 | |
| 810 | static void ops_run_biofill(struct stripe_head *sh) |
| 811 | { |
| 812 | struct dma_async_tx_descriptor *tx = NULL; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 813 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 814 | int i; |
| 815 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 816 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 817 | (unsigned long long)sh->sector); |
| 818 | |
| 819 | for (i = sh->disks; i--; ) { |
| 820 | struct r5dev *dev = &sh->dev[i]; |
| 821 | if (test_bit(R5_Wantfill, &dev->flags)) { |
| 822 | struct bio *rbi; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 823 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 824 | dev->read = rbi = dev->toread; |
| 825 | dev->toread = NULL; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 826 | spin_unlock_irq(&sh->stripe_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 827 | while (rbi && rbi->bi_sector < |
| 828 | dev->sector + STRIPE_SECTORS) { |
| 829 | tx = async_copy_data(0, rbi, dev->page, |
| 830 | dev->sector, tx); |
| 831 | rbi = r5_next_bio(rbi, dev->sector); |
| 832 | } |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 837 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL); |
| 838 | async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 839 | } |
| 840 | |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 841 | static void mark_target_uptodate(struct stripe_head *sh, int target) |
| 842 | { |
| 843 | struct r5dev *tgt; |
| 844 | |
| 845 | if (target < 0) |
| 846 | return; |
| 847 | |
| 848 | tgt = &sh->dev[target]; |
| 849 | set_bit(R5_UPTODATE, &tgt->flags); |
| 850 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 851 | clear_bit(R5_Wantcompute, &tgt->flags); |
| 852 | } |
| 853 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 854 | static void ops_complete_compute(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 855 | { |
| 856 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 857 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 858 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 859 | (unsigned long long)sh->sector); |
| 860 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 861 | /* mark the computed target(s) as uptodate */ |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 862 | mark_target_uptodate(sh, sh->ops.target); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 863 | mark_target_uptodate(sh, sh->ops.target2); |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 864 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 865 | clear_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 866 | if (sh->check_state == check_state_compute_run) |
| 867 | sh->check_state = check_state_compute_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 868 | set_bit(STRIPE_HANDLE, &sh->state); |
| 869 | release_stripe(sh); |
| 870 | } |
| 871 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 872 | /* return a pointer to the address conversion region of the scribble buffer */ |
| 873 | static addr_conv_t *to_addr_conv(struct stripe_head *sh, |
| 874 | struct raid5_percpu *percpu) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 875 | { |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 876 | return percpu->scribble + sizeof(struct page *) * (sh->disks + 2); |
| 877 | } |
| 878 | |
| 879 | static struct dma_async_tx_descriptor * |
| 880 | ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 881 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 882 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 883 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 884 | int target = sh->ops.target; |
| 885 | struct r5dev *tgt = &sh->dev[target]; |
| 886 | struct page *xor_dest = tgt->page; |
| 887 | int count = 0; |
| 888 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 889 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 890 | int i; |
| 891 | |
| 892 | pr_debug("%s: stripe %llu block: %d\n", |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 893 | __func__, (unsigned long long)sh->sector, target); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 894 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 895 | |
| 896 | for (i = disks; i--; ) |
| 897 | if (i != target) |
| 898 | xor_srcs[count++] = sh->dev[i].page; |
| 899 | |
| 900 | atomic_inc(&sh->count); |
| 901 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 902 | 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] | 903 | ops_complete_compute, sh, to_addr_conv(sh, percpu)); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 904 | if (unlikely(count == 1)) |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 905 | 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] | 906 | else |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 907 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 908 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 909 | return tx; |
| 910 | } |
| 911 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 912 | /* set_syndrome_sources - populate source buffers for gen_syndrome |
| 913 | * @srcs - (struct page *) array of size sh->disks |
| 914 | * @sh - stripe_head to parse |
| 915 | * |
| 916 | * Populates srcs in proper layout order for the stripe and returns the |
| 917 | * 'count' of sources to be used in a call to async_gen_syndrome. The P |
| 918 | * destination buffer is recorded in srcs[count] and the Q destination |
| 919 | * is recorded in srcs[count+1]]. |
| 920 | */ |
| 921 | static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh) |
| 922 | { |
| 923 | int disks = sh->disks; |
| 924 | int syndrome_disks = sh->ddf_layout ? disks : (disks - 2); |
| 925 | int d0_idx = raid6_d0(sh); |
| 926 | int count; |
| 927 | int i; |
| 928 | |
| 929 | for (i = 0; i < disks; i++) |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 930 | srcs[i] = NULL; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 931 | |
| 932 | count = 0; |
| 933 | i = d0_idx; |
| 934 | do { |
| 935 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 936 | |
| 937 | srcs[slot] = sh->dev[i].page; |
| 938 | i = raid6_next_disk(i, disks); |
| 939 | } while (i != d0_idx); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 940 | |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 941 | return syndrome_disks; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | static struct dma_async_tx_descriptor * |
| 945 | ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 946 | { |
| 947 | int disks = sh->disks; |
| 948 | struct page **blocks = percpu->scribble; |
| 949 | int target; |
| 950 | int qd_idx = sh->qd_idx; |
| 951 | struct dma_async_tx_descriptor *tx; |
| 952 | struct async_submit_ctl submit; |
| 953 | struct r5dev *tgt; |
| 954 | struct page *dest; |
| 955 | int i; |
| 956 | int count; |
| 957 | |
| 958 | if (sh->ops.target < 0) |
| 959 | target = sh->ops.target2; |
| 960 | else if (sh->ops.target2 < 0) |
| 961 | target = sh->ops.target; |
| 962 | else |
| 963 | /* we should only have one valid target */ |
| 964 | BUG(); |
| 965 | BUG_ON(target < 0); |
| 966 | pr_debug("%s: stripe %llu block: %d\n", |
| 967 | __func__, (unsigned long long)sh->sector, target); |
| 968 | |
| 969 | tgt = &sh->dev[target]; |
| 970 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 971 | dest = tgt->page; |
| 972 | |
| 973 | atomic_inc(&sh->count); |
| 974 | |
| 975 | if (target == qd_idx) { |
| 976 | count = set_syndrome_sources(blocks, sh); |
| 977 | blocks[count] = NULL; /* regenerating p is not necessary */ |
| 978 | BUG_ON(blocks[count+1] != dest); /* q should already be set */ |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 979 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 980 | ops_complete_compute, sh, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 981 | to_addr_conv(sh, percpu)); |
| 982 | tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
| 983 | } else { |
| 984 | /* Compute any data- or p-drive using XOR */ |
| 985 | count = 0; |
| 986 | for (i = disks; i-- ; ) { |
| 987 | if (i == target || i == qd_idx) |
| 988 | continue; |
| 989 | blocks[count++] = sh->dev[i].page; |
| 990 | } |
| 991 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 992 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
| 993 | NULL, ops_complete_compute, sh, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 994 | to_addr_conv(sh, percpu)); |
| 995 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit); |
| 996 | } |
| 997 | |
| 998 | return tx; |
| 999 | } |
| 1000 | |
| 1001 | static struct dma_async_tx_descriptor * |
| 1002 | ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 1003 | { |
| 1004 | int i, count, disks = sh->disks; |
| 1005 | int syndrome_disks = sh->ddf_layout ? disks : disks-2; |
| 1006 | int d0_idx = raid6_d0(sh); |
| 1007 | int faila = -1, failb = -1; |
| 1008 | int target = sh->ops.target; |
| 1009 | int target2 = sh->ops.target2; |
| 1010 | struct r5dev *tgt = &sh->dev[target]; |
| 1011 | struct r5dev *tgt2 = &sh->dev[target2]; |
| 1012 | struct dma_async_tx_descriptor *tx; |
| 1013 | struct page **blocks = percpu->scribble; |
| 1014 | struct async_submit_ctl submit; |
| 1015 | |
| 1016 | pr_debug("%s: stripe %llu block1: %d block2: %d\n", |
| 1017 | __func__, (unsigned long long)sh->sector, target, target2); |
| 1018 | BUG_ON(target < 0 || target2 < 0); |
| 1019 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 1020 | BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags)); |
| 1021 | |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1022 | /* we need to open-code set_syndrome_sources to handle the |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1023 | * slot number conversion for 'faila' and 'failb' |
| 1024 | */ |
| 1025 | for (i = 0; i < disks ; i++) |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 1026 | blocks[i] = NULL; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1027 | count = 0; |
| 1028 | i = d0_idx; |
| 1029 | do { |
| 1030 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 1031 | |
| 1032 | blocks[slot] = sh->dev[i].page; |
| 1033 | |
| 1034 | if (i == target) |
| 1035 | faila = slot; |
| 1036 | if (i == target2) |
| 1037 | failb = slot; |
| 1038 | i = raid6_next_disk(i, disks); |
| 1039 | } while (i != d0_idx); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1040 | |
| 1041 | BUG_ON(faila == failb); |
| 1042 | if (failb < faila) |
| 1043 | swap(faila, failb); |
| 1044 | pr_debug("%s: stripe: %llu faila: %d failb: %d\n", |
| 1045 | __func__, (unsigned long long)sh->sector, faila, failb); |
| 1046 | |
| 1047 | atomic_inc(&sh->count); |
| 1048 | |
| 1049 | if (failb == syndrome_disks+1) { |
| 1050 | /* Q disk is one of the missing disks */ |
| 1051 | if (faila == syndrome_disks) { |
| 1052 | /* Missing P+Q, just recompute */ |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1053 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 1054 | ops_complete_compute, sh, |
| 1055 | to_addr_conv(sh, percpu)); |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 1056 | return async_gen_syndrome(blocks, 0, syndrome_disks+2, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1057 | STRIPE_SIZE, &submit); |
| 1058 | } else { |
| 1059 | struct page *dest; |
| 1060 | int data_target; |
| 1061 | int qd_idx = sh->qd_idx; |
| 1062 | |
| 1063 | /* Missing D+Q: recompute D from P, then recompute Q */ |
| 1064 | if (target == qd_idx) |
| 1065 | data_target = target2; |
| 1066 | else |
| 1067 | data_target = target; |
| 1068 | |
| 1069 | count = 0; |
| 1070 | for (i = disks; i-- ; ) { |
| 1071 | if (i == data_target || i == qd_idx) |
| 1072 | continue; |
| 1073 | blocks[count++] = sh->dev[i].page; |
| 1074 | } |
| 1075 | dest = sh->dev[data_target].page; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1076 | init_async_submit(&submit, |
| 1077 | ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
| 1078 | NULL, NULL, NULL, |
| 1079 | to_addr_conv(sh, percpu)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1080 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, |
| 1081 | &submit); |
| 1082 | |
| 1083 | count = set_syndrome_sources(blocks, sh); |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1084 | init_async_submit(&submit, ASYNC_TX_FENCE, tx, |
| 1085 | ops_complete_compute, sh, |
| 1086 | to_addr_conv(sh, percpu)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1087 | return async_gen_syndrome(blocks, 0, count+2, |
| 1088 | STRIPE_SIZE, &submit); |
| 1089 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1090 | } else { |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1091 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 1092 | ops_complete_compute, sh, |
| 1093 | to_addr_conv(sh, percpu)); |
| 1094 | if (failb == syndrome_disks) { |
| 1095 | /* We're missing D+P. */ |
| 1096 | return async_raid6_datap_recov(syndrome_disks+2, |
| 1097 | STRIPE_SIZE, faila, |
| 1098 | blocks, &submit); |
| 1099 | } else { |
| 1100 | /* We're missing D+D. */ |
| 1101 | return async_raid6_2data_recov(syndrome_disks+2, |
| 1102 | STRIPE_SIZE, faila, failb, |
| 1103 | blocks, &submit); |
| 1104 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1109 | static void ops_complete_prexor(void *stripe_head_ref) |
| 1110 | { |
| 1111 | struct stripe_head *sh = stripe_head_ref; |
| 1112 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1113 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1114 | (unsigned long long)sh->sector); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | static struct dma_async_tx_descriptor * |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1118 | ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1119 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1120 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1121 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1122 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1123 | int count = 0, pd_idx = sh->pd_idx, i; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1124 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1125 | |
| 1126 | /* existing parity data subtracted */ |
| 1127 | struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 1128 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1129 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1130 | (unsigned long long)sh->sector); |
| 1131 | |
| 1132 | for (i = disks; i--; ) { |
| 1133 | struct r5dev *dev = &sh->dev[i]; |
| 1134 | /* Only process blocks that are known to be uptodate */ |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1135 | if (test_bit(R5_Wantdrain, &dev->flags)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1136 | xor_srcs[count++] = dev->page; |
| 1137 | } |
| 1138 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1139 | 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] | 1140 | ops_complete_prexor, sh, to_addr_conv(sh, percpu)); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1141 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1142 | |
| 1143 | return tx; |
| 1144 | } |
| 1145 | |
| 1146 | static struct dma_async_tx_descriptor * |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1147 | 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] | 1148 | { |
| 1149 | int disks = sh->disks; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1150 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1151 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1152 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1153 | (unsigned long long)sh->sector); |
| 1154 | |
| 1155 | for (i = disks; i--; ) { |
| 1156 | struct r5dev *dev = &sh->dev[i]; |
| 1157 | struct bio *chosen; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1158 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1159 | if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1160 | struct bio *wbi; |
| 1161 | |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1162 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1163 | chosen = dev->towrite; |
| 1164 | dev->towrite = NULL; |
| 1165 | BUG_ON(dev->written); |
| 1166 | wbi = dev->written = chosen; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1167 | spin_unlock_irq(&sh->stripe_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1168 | |
| 1169 | while (wbi && wbi->bi_sector < |
| 1170 | dev->sector + STRIPE_SECTORS) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1171 | if (wbi->bi_rw & REQ_FUA) |
| 1172 | set_bit(R5_WantFUA, &dev->flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1173 | if (wbi->bi_rw & REQ_SYNC) |
| 1174 | set_bit(R5_SyncIO, &dev->flags); |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1175 | if (wbi->bi_rw & REQ_DISCARD) |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1176 | set_bit(R5_Discard, &dev->flags); |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1177 | else |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1178 | tx = async_copy_data(1, wbi, dev->page, |
| 1179 | dev->sector, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1180 | wbi = r5_next_bio(wbi, dev->sector); |
| 1181 | } |
| 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | return tx; |
| 1186 | } |
| 1187 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1188 | static void ops_complete_reconstruct(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1189 | { |
| 1190 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1191 | int disks = sh->disks; |
| 1192 | int pd_idx = sh->pd_idx; |
| 1193 | int qd_idx = sh->qd_idx; |
| 1194 | int i; |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1195 | bool fua = false, sync = false, discard = false; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1196 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1197 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1198 | (unsigned long long)sh->sector); |
| 1199 | |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1200 | for (i = disks; i--; ) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1201 | fua |= test_bit(R5_WantFUA, &sh->dev[i].flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1202 | sync |= test_bit(R5_SyncIO, &sh->dev[i].flags); |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1203 | discard |= test_bit(R5_Discard, &sh->dev[i].flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1204 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1205 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1206 | for (i = disks; i--; ) { |
| 1207 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1208 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1209 | if (dev->written || i == pd_idx || i == qd_idx) { |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1210 | if (!discard) |
| 1211 | set_bit(R5_UPTODATE, &dev->flags); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1212 | if (fua) |
| 1213 | set_bit(R5_WantFUA, &dev->flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1214 | if (sync) |
| 1215 | set_bit(R5_SyncIO, &dev->flags); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1216 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1217 | } |
| 1218 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1219 | if (sh->reconstruct_state == reconstruct_state_drain_run) |
| 1220 | sh->reconstruct_state = reconstruct_state_drain_result; |
| 1221 | else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) |
| 1222 | sh->reconstruct_state = reconstruct_state_prexor_drain_result; |
| 1223 | else { |
| 1224 | BUG_ON(sh->reconstruct_state != reconstruct_state_run); |
| 1225 | sh->reconstruct_state = reconstruct_state_result; |
| 1226 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1227 | |
| 1228 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1229 | release_stripe(sh); |
| 1230 | } |
| 1231 | |
| 1232 | static void |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1233 | ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1234 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1235 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1236 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1237 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1238 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1239 | int count = 0, pd_idx = sh->pd_idx, i; |
| 1240 | struct page *xor_dest; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1241 | int prexor = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1242 | unsigned long flags; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1243 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1244 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1245 | (unsigned long long)sh->sector); |
| 1246 | |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1247 | for (i = 0; i < sh->disks; i++) { |
| 1248 | if (pd_idx == i) |
| 1249 | continue; |
| 1250 | if (!test_bit(R5_Discard, &sh->dev[i].flags)) |
| 1251 | break; |
| 1252 | } |
| 1253 | if (i >= sh->disks) { |
| 1254 | atomic_inc(&sh->count); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1255 | set_bit(R5_Discard, &sh->dev[pd_idx].flags); |
| 1256 | ops_complete_reconstruct(sh); |
| 1257 | return; |
| 1258 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1259 | /* check if prexor is active which means only process blocks |
| 1260 | * that are part of a read-modify-write (written) |
| 1261 | */ |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1262 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) { |
| 1263 | prexor = 1; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1264 | xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 1265 | for (i = disks; i--; ) { |
| 1266 | struct r5dev *dev = &sh->dev[i]; |
| 1267 | if (dev->written) |
| 1268 | xor_srcs[count++] = dev->page; |
| 1269 | } |
| 1270 | } else { |
| 1271 | xor_dest = sh->dev[pd_idx].page; |
| 1272 | for (i = disks; i--; ) { |
| 1273 | struct r5dev *dev = &sh->dev[i]; |
| 1274 | if (i != pd_idx) |
| 1275 | xor_srcs[count++] = dev->page; |
| 1276 | } |
| 1277 | } |
| 1278 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1279 | /* 1/ if we prexor'd then the dest is reused as a source |
| 1280 | * 2/ if we did not prexor then we are redoing the parity |
| 1281 | * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST |
| 1282 | * for the synchronous xor case |
| 1283 | */ |
Dan Williams | 88ba2aa | 2009-04-09 16:16:18 -0700 | [diff] [blame] | 1284 | flags = ASYNC_TX_ACK | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1285 | (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST); |
| 1286 | |
| 1287 | atomic_inc(&sh->count); |
| 1288 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1289 | init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh, |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1290 | to_addr_conv(sh, percpu)); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1291 | if (unlikely(count == 1)) |
| 1292 | tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit); |
| 1293 | else |
| 1294 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1295 | } |
| 1296 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1297 | static void |
| 1298 | ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1299 | struct dma_async_tx_descriptor *tx) |
| 1300 | { |
| 1301 | struct async_submit_ctl submit; |
| 1302 | struct page **blocks = percpu->scribble; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1303 | int count, i; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1304 | |
| 1305 | pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector); |
| 1306 | |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1307 | for (i = 0; i < sh->disks; i++) { |
| 1308 | if (sh->pd_idx == i || sh->qd_idx == i) |
| 1309 | continue; |
| 1310 | if (!test_bit(R5_Discard, &sh->dev[i].flags)) |
| 1311 | break; |
| 1312 | } |
| 1313 | if (i >= sh->disks) { |
| 1314 | atomic_inc(&sh->count); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1315 | set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags); |
| 1316 | set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags); |
| 1317 | ops_complete_reconstruct(sh); |
| 1318 | return; |
| 1319 | } |
| 1320 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1321 | count = set_syndrome_sources(blocks, sh); |
| 1322 | |
| 1323 | atomic_inc(&sh->count); |
| 1324 | |
| 1325 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct, |
| 1326 | sh, to_addr_conv(sh, percpu)); |
| 1327 | async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | static void ops_complete_check(void *stripe_head_ref) |
| 1331 | { |
| 1332 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1333 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1334 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1335 | (unsigned long long)sh->sector); |
| 1336 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 1337 | sh->check_state = check_state_check_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1338 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1339 | release_stripe(sh); |
| 1340 | } |
| 1341 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1342 | 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] | 1343 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1344 | int disks = sh->disks; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1345 | int pd_idx = sh->pd_idx; |
| 1346 | int qd_idx = sh->qd_idx; |
| 1347 | struct page *xor_dest; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1348 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1349 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1350 | struct async_submit_ctl submit; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1351 | int count; |
| 1352 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1353 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1354 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1355 | (unsigned long long)sh->sector); |
| 1356 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1357 | count = 0; |
| 1358 | xor_dest = sh->dev[pd_idx].page; |
| 1359 | xor_srcs[count++] = xor_dest; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1360 | for (i = disks; i--; ) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1361 | if (i == pd_idx || i == qd_idx) |
| 1362 | continue; |
| 1363 | xor_srcs[count++] = sh->dev[i].page; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1364 | } |
| 1365 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1366 | init_async_submit(&submit, 0, NULL, NULL, NULL, |
| 1367 | to_addr_conv(sh, percpu)); |
Dan Williams | 099f53c | 2009-04-08 14:28:37 -0700 | [diff] [blame] | 1368 | tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1369 | &sh->ops.zero_sum_result, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1370 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1371 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1372 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL); |
| 1373 | tx = async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1374 | } |
| 1375 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1376 | static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp) |
| 1377 | { |
| 1378 | struct page **srcs = percpu->scribble; |
| 1379 | struct async_submit_ctl submit; |
| 1380 | int count; |
| 1381 | |
| 1382 | pr_debug("%s: stripe %llu checkp: %d\n", __func__, |
| 1383 | (unsigned long long)sh->sector, checkp); |
| 1384 | |
| 1385 | count = set_syndrome_sources(srcs, sh); |
| 1386 | if (!checkp) |
| 1387 | srcs[count] = NULL; |
| 1388 | |
| 1389 | atomic_inc(&sh->count); |
| 1390 | init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check, |
| 1391 | sh, to_addr_conv(sh, percpu)); |
| 1392 | async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE, |
| 1393 | &sh->ops.zero_sum_result, percpu->spare_page, &submit); |
| 1394 | } |
| 1395 | |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 1396 | 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] | 1397 | { |
| 1398 | int overlap_clear = 0, i, disks = sh->disks; |
| 1399 | struct dma_async_tx_descriptor *tx = NULL; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1400 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1401 | int level = conf->level; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1402 | struct raid5_percpu *percpu; |
| 1403 | unsigned long cpu; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1404 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1405 | cpu = get_cpu(); |
| 1406 | percpu = per_cpu_ptr(conf->percpu, cpu); |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 1407 | if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1408 | ops_run_biofill(sh); |
| 1409 | overlap_clear++; |
| 1410 | } |
| 1411 | |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 1412 | if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1413 | if (level < 6) |
| 1414 | tx = ops_run_compute5(sh, percpu); |
| 1415 | else { |
| 1416 | if (sh->ops.target2 < 0 || sh->ops.target < 0) |
| 1417 | tx = ops_run_compute6_1(sh, percpu); |
| 1418 | else |
| 1419 | tx = ops_run_compute6_2(sh, percpu); |
| 1420 | } |
| 1421 | /* terminate the chain if reconstruct is not set to be run */ |
| 1422 | if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 1423 | async_tx_ack(tx); |
| 1424 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1425 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1426 | if (test_bit(STRIPE_OP_PREXOR, &ops_request)) |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1427 | tx = ops_run_prexor(sh, percpu, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1428 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1429 | if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) { |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1430 | tx = ops_run_biodrain(sh, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1431 | overlap_clear++; |
| 1432 | } |
| 1433 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1434 | if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) { |
| 1435 | if (level < 6) |
| 1436 | ops_run_reconstruct5(sh, percpu, tx); |
| 1437 | else |
| 1438 | ops_run_reconstruct6(sh, percpu, tx); |
| 1439 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1440 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1441 | if (test_bit(STRIPE_OP_CHECK, &ops_request)) { |
| 1442 | if (sh->check_state == check_state_run) |
| 1443 | ops_run_check_p(sh, percpu); |
| 1444 | else if (sh->check_state == check_state_run_q) |
| 1445 | ops_run_check_pq(sh, percpu, 0); |
| 1446 | else if (sh->check_state == check_state_run_pq) |
| 1447 | ops_run_check_pq(sh, percpu, 1); |
| 1448 | else |
| 1449 | BUG(); |
| 1450 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1451 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1452 | if (overlap_clear) |
| 1453 | for (i = disks; i--; ) { |
| 1454 | struct r5dev *dev = &sh->dev[i]; |
| 1455 | if (test_and_clear_bit(R5_Overlap, &dev->flags)) |
| 1456 | wake_up(&sh->raid_conf->wait_for_overlap); |
| 1457 | } |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1458 | put_cpu(); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1459 | } |
| 1460 | |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 1461 | #ifdef CONFIG_MULTICORE_RAID456 |
| 1462 | static void async_run_ops(void *param, async_cookie_t cookie) |
| 1463 | { |
| 1464 | struct stripe_head *sh = param; |
| 1465 | unsigned long ops_request = sh->ops.request; |
| 1466 | |
| 1467 | clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state); |
| 1468 | wake_up(&sh->ops.wait_for_ops); |
| 1469 | |
| 1470 | __raid_run_ops(sh, ops_request); |
| 1471 | release_stripe(sh); |
| 1472 | } |
| 1473 | |
| 1474 | static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request) |
| 1475 | { |
| 1476 | /* since handle_stripe can be called outside of raid5d context |
| 1477 | * we need to ensure sh->ops.request is de-staged before another |
| 1478 | * request arrives |
| 1479 | */ |
| 1480 | wait_event(sh->ops.wait_for_ops, |
| 1481 | !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state)); |
| 1482 | sh->ops.request = ops_request; |
| 1483 | |
| 1484 | atomic_inc(&sh->count); |
| 1485 | async_schedule(async_run_ops, sh); |
| 1486 | } |
| 1487 | #else |
| 1488 | #define raid_run_ops __raid_run_ops |
| 1489 | #endif |
| 1490 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1491 | static int grow_one_stripe(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | { |
| 1493 | struct stripe_head *sh; |
Namhyung Kim | 6ce3284 | 2011-07-18 17:38:50 +1000 | [diff] [blame] | 1494 | sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1495 | if (!sh) |
| 1496 | return 0; |
Namhyung Kim | 6ce3284 | 2011-07-18 17:38:50 +1000 | [diff] [blame] | 1497 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1498 | sh->raid_conf = conf; |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 1499 | #ifdef CONFIG_MULTICORE_RAID456 |
| 1500 | init_waitqueue_head(&sh->ops.wait_for_ops); |
| 1501 | #endif |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1502 | |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1503 | spin_lock_init(&sh->stripe_lock); |
| 1504 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 1505 | if (grow_buffers(sh)) { |
| 1506 | shrink_buffers(sh); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1507 | kmem_cache_free(conf->slab_cache, sh); |
| 1508 | return 0; |
| 1509 | } |
| 1510 | /* we just created an active stripe so... */ |
| 1511 | atomic_set(&sh->count, 1); |
| 1512 | atomic_inc(&conf->active_stripes); |
| 1513 | INIT_LIST_HEAD(&sh->lru); |
| 1514 | release_stripe(sh); |
| 1515 | return 1; |
| 1516 | } |
| 1517 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1518 | static int grow_stripes(struct r5conf *conf, int num) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1519 | { |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 1520 | struct kmem_cache *sc; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 1521 | int devs = max(conf->raid_disks, conf->previous_raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | |
NeilBrown | f4be6b4 | 2010-06-01 19:37:25 +1000 | [diff] [blame] | 1523 | if (conf->mddev->gendisk) |
| 1524 | sprintf(conf->cache_name[0], |
| 1525 | "raid%d-%s", conf->level, mdname(conf->mddev)); |
| 1526 | else |
| 1527 | sprintf(conf->cache_name[0], |
| 1528 | "raid%d-%p", conf->level, conf->mddev); |
| 1529 | sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]); |
| 1530 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1531 | conf->active_name = 0; |
| 1532 | sc = kmem_cache_create(conf->cache_name[conf->active_name], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1534 | 0, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | if (!sc) |
| 1536 | return 1; |
| 1537 | conf->slab_cache = sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1538 | conf->pool_size = devs; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1539 | while (num--) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1540 | if (!grow_one_stripe(conf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | return 0; |
| 1543 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 1544 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1545 | /** |
| 1546 | * scribble_len - return the required size of the scribble region |
| 1547 | * @num - total number of disks in the array |
| 1548 | * |
| 1549 | * The size must be enough to contain: |
| 1550 | * 1/ a struct page pointer for each device in the array +2 |
| 1551 | * 2/ room to convert each entry in (1) to its corresponding dma |
| 1552 | * (dma_map_page()) or page (page_address()) address. |
| 1553 | * |
| 1554 | * Note: the +2 is for the destination buffers of the ddf/raid6 case where we |
| 1555 | * calculate over all devices (not just the data blocks), using zeros in place |
| 1556 | * of the P and Q blocks. |
| 1557 | */ |
| 1558 | static size_t scribble_len(int num) |
| 1559 | { |
| 1560 | size_t len; |
| 1561 | |
| 1562 | len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2); |
| 1563 | |
| 1564 | return len; |
| 1565 | } |
| 1566 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1567 | static int resize_stripes(struct r5conf *conf, int newsize) |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1568 | { |
| 1569 | /* Make all the stripes able to hold 'newsize' devices. |
| 1570 | * New slots in each stripe get 'page' set to a new page. |
| 1571 | * |
| 1572 | * This happens in stages: |
| 1573 | * 1/ create a new kmem_cache and allocate the required number of |
| 1574 | * stripe_heads. |
| 1575 | * 2/ gather all the old stripe_heads and tranfer the pages across |
| 1576 | * to the new stripe_heads. This will have the side effect of |
| 1577 | * freezing the array as once all stripe_heads have been collected, |
| 1578 | * no IO will be possible. Old stripe heads are freed once their |
| 1579 | * pages have been transferred over, and the old kmem_cache is |
| 1580 | * freed when all stripes are done. |
| 1581 | * 3/ reallocate conf->disks to be suitable bigger. If this fails, |
| 1582 | * we simple return a failre status - no need to clean anything up. |
| 1583 | * 4/ allocate new pages for the new slots in the new stripe_heads. |
| 1584 | * If this fails, we don't bother trying the shrink the |
| 1585 | * stripe_heads down again, we just leave them as they are. |
| 1586 | * As each stripe_head is processed the new one is released into |
| 1587 | * active service. |
| 1588 | * |
| 1589 | * Once step2 is started, we cannot afford to wait for a write, |
| 1590 | * so we use GFP_NOIO allocations. |
| 1591 | */ |
| 1592 | struct stripe_head *osh, *nsh; |
| 1593 | LIST_HEAD(newstripes); |
| 1594 | struct disk_info *ndisks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1595 | unsigned long cpu; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 1596 | int err; |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 1597 | struct kmem_cache *sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1598 | int i; |
| 1599 | |
| 1600 | if (newsize <= conf->pool_size) |
| 1601 | return 0; /* never bother to shrink */ |
| 1602 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 1603 | err = md_allow_write(conf->mddev); |
| 1604 | if (err) |
| 1605 | return err; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 1606 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1607 | /* Step 1 */ |
| 1608 | sc = kmem_cache_create(conf->cache_name[1-conf->active_name], |
| 1609 | sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1610 | 0, 0, NULL); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1611 | if (!sc) |
| 1612 | return -ENOMEM; |
| 1613 | |
| 1614 | for (i = conf->max_nr_stripes; i; i--) { |
Namhyung Kim | 6ce3284 | 2011-07-18 17:38:50 +1000 | [diff] [blame] | 1615 | nsh = kmem_cache_zalloc(sc, GFP_KERNEL); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1616 | if (!nsh) |
| 1617 | break; |
| 1618 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1619 | nsh->raid_conf = conf; |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 1620 | #ifdef CONFIG_MULTICORE_RAID456 |
| 1621 | init_waitqueue_head(&nsh->ops.wait_for_ops); |
| 1622 | #endif |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1623 | |
| 1624 | list_add(&nsh->lru, &newstripes); |
| 1625 | } |
| 1626 | if (i) { |
| 1627 | /* didn't get enough, give up */ |
| 1628 | while (!list_empty(&newstripes)) { |
| 1629 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 1630 | list_del(&nsh->lru); |
| 1631 | kmem_cache_free(sc, nsh); |
| 1632 | } |
| 1633 | kmem_cache_destroy(sc); |
| 1634 | return -ENOMEM; |
| 1635 | } |
| 1636 | /* Step 2 - Must use GFP_NOIO now. |
| 1637 | * OK, we have enough stripes, start collecting inactive |
| 1638 | * stripes and copying them over |
| 1639 | */ |
| 1640 | list_for_each_entry(nsh, &newstripes, lru) { |
| 1641 | spin_lock_irq(&conf->device_lock); |
| 1642 | wait_event_lock_irq(conf->wait_for_stripe, |
| 1643 | !list_empty(&conf->inactive_list), |
| 1644 | conf->device_lock, |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 1645 | ); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1646 | osh = get_free_stripe(conf); |
| 1647 | spin_unlock_irq(&conf->device_lock); |
| 1648 | atomic_set(&nsh->count, 1); |
| 1649 | for(i=0; i<conf->pool_size; i++) |
| 1650 | nsh->dev[i].page = osh->dev[i].page; |
| 1651 | for( ; i<newsize; i++) |
| 1652 | nsh->dev[i].page = NULL; |
| 1653 | kmem_cache_free(conf->slab_cache, osh); |
| 1654 | } |
| 1655 | kmem_cache_destroy(conf->slab_cache); |
| 1656 | |
| 1657 | /* Step 3. |
| 1658 | * At this point, we are holding all the stripes so the array |
| 1659 | * is completely stalled, so now is a good time to resize |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1660 | * conf->disks and the scribble region |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1661 | */ |
| 1662 | ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO); |
| 1663 | if (ndisks) { |
| 1664 | for (i=0; i<conf->raid_disks; i++) |
| 1665 | ndisks[i] = conf->disks[i]; |
| 1666 | kfree(conf->disks); |
| 1667 | conf->disks = ndisks; |
| 1668 | } else |
| 1669 | err = -ENOMEM; |
| 1670 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1671 | get_online_cpus(); |
| 1672 | conf->scribble_len = scribble_len(newsize); |
| 1673 | for_each_present_cpu(cpu) { |
| 1674 | struct raid5_percpu *percpu; |
| 1675 | void *scribble; |
| 1676 | |
| 1677 | percpu = per_cpu_ptr(conf->percpu, cpu); |
| 1678 | scribble = kmalloc(conf->scribble_len, GFP_NOIO); |
| 1679 | |
| 1680 | if (scribble) { |
| 1681 | kfree(percpu->scribble); |
| 1682 | percpu->scribble = scribble; |
| 1683 | } else { |
| 1684 | err = -ENOMEM; |
| 1685 | break; |
| 1686 | } |
| 1687 | } |
| 1688 | put_online_cpus(); |
| 1689 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1690 | /* Step 4, return new stripes to service */ |
| 1691 | while(!list_empty(&newstripes)) { |
| 1692 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 1693 | list_del_init(&nsh->lru); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1694 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1695 | for (i=conf->raid_disks; i < newsize; i++) |
| 1696 | if (nsh->dev[i].page == NULL) { |
| 1697 | struct page *p = alloc_page(GFP_NOIO); |
| 1698 | nsh->dev[i].page = p; |
| 1699 | if (!p) |
| 1700 | err = -ENOMEM; |
| 1701 | } |
| 1702 | release_stripe(nsh); |
| 1703 | } |
| 1704 | /* critical section pass, GFP_NOIO no longer needed */ |
| 1705 | |
| 1706 | conf->slab_cache = sc; |
| 1707 | conf->active_name = 1-conf->active_name; |
| 1708 | conf->pool_size = newsize; |
| 1709 | return err; |
| 1710 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1712 | static int drop_one_stripe(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | { |
| 1714 | struct stripe_head *sh; |
| 1715 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1716 | spin_lock_irq(&conf->device_lock); |
| 1717 | sh = get_free_stripe(conf); |
| 1718 | spin_unlock_irq(&conf->device_lock); |
| 1719 | if (!sh) |
| 1720 | return 0; |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 1721 | BUG_ON(atomic_read(&sh->count)); |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 1722 | shrink_buffers(sh); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1723 | kmem_cache_free(conf->slab_cache, sh); |
| 1724 | atomic_dec(&conf->active_stripes); |
| 1725 | return 1; |
| 1726 | } |
| 1727 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1728 | static void shrink_stripes(struct r5conf *conf) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1729 | { |
| 1730 | while (drop_one_stripe(conf)) |
| 1731 | ; |
| 1732 | |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1733 | if (conf->slab_cache) |
| 1734 | kmem_cache_destroy(conf->slab_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | conf->slab_cache = NULL; |
| 1736 | } |
| 1737 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1738 | static void raid5_end_read_request(struct bio * bi, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1740 | struct stripe_head *sh = bi->bi_private; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1741 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 1742 | int disks = sh->disks, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1744 | char b[BDEVNAME_SIZE]; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1745 | struct md_rdev *rdev = NULL; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1746 | sector_t s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | |
| 1748 | for (i=0 ; i<disks; i++) |
| 1749 | if (bi == &sh->dev[i].req) |
| 1750 | break; |
| 1751 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1752 | pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n", |
| 1753 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | uptodate); |
| 1755 | if (i == disks) { |
| 1756 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1757 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | } |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1759 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1760 | /* If replacement finished while this request was outstanding, |
| 1761 | * 'replacement' might be NULL already. |
| 1762 | * In that case it moved down to 'rdev'. |
| 1763 | * rdev is not removed until all requests are finished. |
| 1764 | */ |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1765 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1766 | if (!rdev) |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1767 | rdev = conf->disks[i].rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1768 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1769 | if (use_new_offset(conf, sh)) |
| 1770 | s = sh->sector + rdev->new_data_offset; |
| 1771 | else |
| 1772 | s = sh->sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | if (uptodate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | set_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1775 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1776 | /* Note that this cannot happen on a |
| 1777 | * replacement device. We just fail those on |
| 1778 | * any error |
| 1779 | */ |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1780 | printk_ratelimited( |
| 1781 | KERN_INFO |
| 1782 | "md/raid:%s: read error corrected" |
| 1783 | " (%lu sectors at %llu on %s)\n", |
| 1784 | mdname(conf->mddev), STRIPE_SECTORS, |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1785 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1786 | bdevname(rdev->bdev, b)); |
Namhyung Kim | ddd5115 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1787 | atomic_add(STRIPE_SECTORS, &rdev->corrected_errors); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1788 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 1789 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 1790 | } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) |
| 1791 | clear_bit(R5_ReadNoMerge, &sh->dev[i].flags); |
| 1792 | |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1793 | if (atomic_read(&rdev->read_errors)) |
| 1794 | atomic_set(&rdev->read_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1795 | } else { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1796 | const char *bdn = bdevname(rdev->bdev, b); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1797 | int retry = 0; |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1798 | int set_bad = 0; |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1799 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | clear_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1801 | atomic_inc(&rdev->read_errors); |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1802 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) |
| 1803 | printk_ratelimited( |
| 1804 | KERN_WARNING |
| 1805 | "md/raid:%s: read error on replacement device " |
| 1806 | "(sector %llu on %s).\n", |
| 1807 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1808 | (unsigned long long)s, |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1809 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1810 | else if (conf->mddev->degraded >= conf->max_degraded) { |
| 1811 | set_bad = 1; |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1812 | printk_ratelimited( |
| 1813 | KERN_WARNING |
| 1814 | "md/raid:%s: read error not correctable " |
| 1815 | "(sector %llu on %s).\n", |
| 1816 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1817 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1818 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1819 | } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) { |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1820 | /* Oh, no!!! */ |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1821 | set_bad = 1; |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1822 | printk_ratelimited( |
| 1823 | KERN_WARNING |
| 1824 | "md/raid:%s: read error NOT corrected!! " |
| 1825 | "(sector %llu on %s).\n", |
| 1826 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1827 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1828 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1829 | } else if (atomic_read(&rdev->read_errors) |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1830 | > conf->max_nr_stripes) |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 1831 | printk(KERN_WARNING |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 1832 | "md/raid:%s: Too many read errors, failing device %s.\n", |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1833 | mdname(conf->mddev), bdn); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1834 | else |
| 1835 | retry = 1; |
| 1836 | if (retry) |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 1837 | if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) { |
| 1838 | set_bit(R5_ReadError, &sh->dev[i].flags); |
| 1839 | clear_bit(R5_ReadNoMerge, &sh->dev[i].flags); |
| 1840 | } else |
| 1841 | set_bit(R5_ReadNoMerge, &sh->dev[i].flags); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1842 | else { |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1843 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 1844 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1845 | if (!(set_bad |
| 1846 | && test_bit(In_sync, &rdev->flags) |
| 1847 | && rdev_set_badblocks( |
| 1848 | rdev, sh->sector, STRIPE_SECTORS, 0))) |
| 1849 | md_error(conf->mddev, rdev); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1850 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1851 | } |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1852 | rdev_dec_pending(rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 1854 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1855 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | } |
| 1857 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 1858 | static void raid5_end_write_request(struct bio *bi, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1860 | struct stripe_head *sh = bi->bi_private; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1861 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 1862 | int disks = sh->disks, i; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1863 | struct md_rdev *uninitialized_var(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 1865 | sector_t first_bad; |
| 1866 | int bad_sectors; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1867 | int replacement = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1869 | for (i = 0 ; i < disks; i++) { |
| 1870 | if (bi == &sh->dev[i].req) { |
| 1871 | rdev = conf->disks[i].rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | break; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1873 | } |
| 1874 | if (bi == &sh->dev[i].rreq) { |
| 1875 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1876 | if (rdev) |
| 1877 | replacement = 1; |
| 1878 | else |
| 1879 | /* rdev was removed and 'replacement' |
| 1880 | * replaced it. rdev is not removed |
| 1881 | * until all requests are finished. |
| 1882 | */ |
| 1883 | rdev = conf->disks[i].rdev; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1884 | break; |
| 1885 | } |
| 1886 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1887 | pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
| 1889 | uptodate); |
| 1890 | if (i == disks) { |
| 1891 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1892 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | } |
| 1894 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1895 | if (replacement) { |
| 1896 | if (!uptodate) |
| 1897 | md_error(conf->mddev, rdev); |
| 1898 | else if (is_badblock(rdev, sh->sector, |
| 1899 | STRIPE_SECTORS, |
| 1900 | &first_bad, &bad_sectors)) |
| 1901 | set_bit(R5_MadeGoodRepl, &sh->dev[i].flags); |
| 1902 | } else { |
| 1903 | if (!uptodate) { |
| 1904 | set_bit(WriteErrorSeen, &rdev->flags); |
| 1905 | set_bit(R5_WriteError, &sh->dev[i].flags); |
NeilBrown | 3a6de29 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 1906 | if (!test_and_set_bit(WantReplacement, &rdev->flags)) |
| 1907 | set_bit(MD_RECOVERY_NEEDED, |
| 1908 | &rdev->mddev->recovery); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1909 | } else if (is_badblock(rdev, sh->sector, |
| 1910 | STRIPE_SECTORS, |
| 1911 | &first_bad, &bad_sectors)) |
| 1912 | set_bit(R5_MadeGood, &sh->dev[i].flags); |
| 1913 | } |
| 1914 | rdev_dec_pending(rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1916 | if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags)) |
| 1917 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1918 | set_bit(STRIPE_HANDLE, &sh->state); |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1919 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | } |
| 1921 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1922 | 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] | 1923 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1924 | 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] | 1925 | { |
| 1926 | struct r5dev *dev = &sh->dev[i]; |
| 1927 | |
| 1928 | bio_init(&dev->req); |
| 1929 | dev->req.bi_io_vec = &dev->vec; |
| 1930 | dev->req.bi_vcnt++; |
| 1931 | dev->req.bi_max_vecs++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1932 | dev->req.bi_private = sh; |
NeilBrown | 995c427 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1933 | dev->vec.bv_page = dev->page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1934 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1935 | bio_init(&dev->rreq); |
| 1936 | dev->rreq.bi_io_vec = &dev->rvec; |
| 1937 | dev->rreq.bi_vcnt++; |
| 1938 | dev->rreq.bi_max_vecs++; |
| 1939 | dev->rreq.bi_private = sh; |
| 1940 | dev->rvec.bv_page = dev->page; |
| 1941 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | dev->flags = 0; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1943 | dev->sector = compute_blocknr(sh, i, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | } |
| 1945 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1946 | static void error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | { |
| 1948 | char b[BDEVNAME_SIZE]; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1949 | struct r5conf *conf = mddev->private; |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 1950 | unsigned long flags; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 1951 | pr_debug("raid456: error called\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 1953 | spin_lock_irqsave(&conf->device_lock, flags); |
| 1954 | clear_bit(In_sync, &rdev->flags); |
| 1955 | mddev->degraded = calc_degraded(conf); |
| 1956 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 1957 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 1958 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 1959 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 6f8d0c7 | 2011-05-11 14:38:44 +1000 | [diff] [blame] | 1960 | set_bit(Faulty, &rdev->flags); |
| 1961 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 1962 | printk(KERN_ALERT |
| 1963 | "md/raid:%s: Disk failure on %s, disabling device.\n" |
| 1964 | "md/raid:%s: Operation continuing on %d devices.\n", |
| 1965 | mdname(mddev), |
| 1966 | bdevname(rdev->bdev, b), |
| 1967 | mdname(mddev), |
| 1968 | conf->raid_disks - mddev->degraded); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1969 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1970 | |
| 1971 | /* |
| 1972 | * Input: a 'big' sector number, |
| 1973 | * Output: index of the data and parity disk, and the sector # in them. |
| 1974 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1975 | static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1976 | int previous, int *dd_idx, |
| 1977 | struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1978 | { |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1979 | sector_t stripe, stripe2; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 1980 | sector_t chunk_number; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | unsigned int chunk_offset; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1982 | int pd_idx, qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1983 | int ddf_layout = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | sector_t new_sector; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1985 | int algorithm = previous ? conf->prev_algo |
| 1986 | : conf->algorithm; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 1987 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
| 1988 | : conf->chunk_sectors; |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1989 | int raid_disks = previous ? conf->previous_raid_disks |
| 1990 | : conf->raid_disks; |
| 1991 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | |
| 1993 | /* First compute the information on this sector */ |
| 1994 | |
| 1995 | /* |
| 1996 | * Compute the chunk number and the sector offset inside the chunk |
| 1997 | */ |
| 1998 | chunk_offset = sector_div(r_sector, sectors_per_chunk); |
| 1999 | chunk_number = r_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2000 | |
| 2001 | /* |
| 2002 | * Compute the stripe number |
| 2003 | */ |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2004 | stripe = chunk_number; |
| 2005 | *dd_idx = sector_div(stripe, data_disks); |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2006 | stripe2 = stripe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2007 | /* |
| 2008 | * Select the parity disk based on the user selected algorithm. |
| 2009 | */ |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2010 | pd_idx = qd_idx = -1; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2011 | switch(conf->level) { |
| 2012 | case 4: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2013 | pd_idx = data_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2014 | break; |
| 2015 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2016 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2017 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2018 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2019 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | (*dd_idx)++; |
| 2021 | break; |
| 2022 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2023 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2024 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2025 | (*dd_idx)++; |
| 2026 | break; |
| 2027 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2028 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2029 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -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 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2034 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2035 | case ALGORITHM_PARITY_0: |
| 2036 | pd_idx = 0; |
| 2037 | (*dd_idx)++; |
| 2038 | break; |
| 2039 | case ALGORITHM_PARITY_N: |
| 2040 | pd_idx = data_disks; |
| 2041 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2042 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2043 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2044 | } |
| 2045 | break; |
| 2046 | case 6: |
| 2047 | |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2048 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2049 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2050 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2051 | qd_idx = pd_idx + 1; |
| 2052 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2053 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2054 | qd_idx = 0; |
| 2055 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2056 | (*dd_idx) += 2; /* D D P Q D */ |
| 2057 | break; |
| 2058 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2059 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2060 | qd_idx = pd_idx + 1; |
| 2061 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2062 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2063 | qd_idx = 0; |
| 2064 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2065 | (*dd_idx) += 2; /* D D P Q D */ |
| 2066 | break; |
| 2067 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2068 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2069 | qd_idx = (pd_idx + 1) % raid_disks; |
| 2070 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2071 | break; |
| 2072 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2073 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2074 | qd_idx = (pd_idx + 1) % raid_disks; |
| 2075 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2076 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2077 | |
| 2078 | case ALGORITHM_PARITY_0: |
| 2079 | pd_idx = 0; |
| 2080 | qd_idx = 1; |
| 2081 | (*dd_idx) += 2; |
| 2082 | break; |
| 2083 | case ALGORITHM_PARITY_N: |
| 2084 | pd_idx = data_disks; |
| 2085 | qd_idx = data_disks + 1; |
| 2086 | break; |
| 2087 | |
| 2088 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 2089 | /* Exactly the same as RIGHT_ASYMMETRIC, but or |
| 2090 | * of blocks for computing Q is different. |
| 2091 | */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2092 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2093 | qd_idx = pd_idx + 1; |
| 2094 | if (pd_idx == raid_disks-1) { |
| 2095 | (*dd_idx)++; /* Q D D D P */ |
| 2096 | qd_idx = 0; |
| 2097 | } else if (*dd_idx >= pd_idx) |
| 2098 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2099 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2100 | break; |
| 2101 | |
| 2102 | case ALGORITHM_ROTATING_N_RESTART: |
| 2103 | /* Same a left_asymmetric, by first stripe is |
| 2104 | * D D D P Q rather than |
| 2105 | * Q D D D P |
| 2106 | */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2107 | stripe2 += 1; |
| 2108 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2109 | qd_idx = pd_idx + 1; |
| 2110 | if (pd_idx == raid_disks-1) { |
| 2111 | (*dd_idx)++; /* Q D D D P */ |
| 2112 | qd_idx = 0; |
| 2113 | } else if (*dd_idx >= pd_idx) |
| 2114 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2115 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2116 | break; |
| 2117 | |
| 2118 | case ALGORITHM_ROTATING_N_CONTINUE: |
| 2119 | /* Same as left_symmetric but Q is before P */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2120 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2121 | qd_idx = (pd_idx + raid_disks - 1) % raid_disks; |
| 2122 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2123 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2124 | break; |
| 2125 | |
| 2126 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 2127 | /* RAID5 left_asymmetric, with Q on last device */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2128 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2129 | if (*dd_idx >= pd_idx) |
| 2130 | (*dd_idx)++; |
| 2131 | qd_idx = raid_disks - 1; |
| 2132 | break; |
| 2133 | |
| 2134 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2135 | pd_idx = sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2136 | if (*dd_idx >= pd_idx) |
| 2137 | (*dd_idx)++; |
| 2138 | qd_idx = raid_disks - 1; |
| 2139 | break; |
| 2140 | |
| 2141 | case ALGORITHM_LEFT_SYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2142 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2143 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 2144 | qd_idx = raid_disks - 1; |
| 2145 | break; |
| 2146 | |
| 2147 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2148 | pd_idx = sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2149 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 2150 | qd_idx = raid_disks - 1; |
| 2151 | break; |
| 2152 | |
| 2153 | case ALGORITHM_PARITY_0_6: |
| 2154 | pd_idx = 0; |
| 2155 | (*dd_idx)++; |
| 2156 | qd_idx = raid_disks - 1; |
| 2157 | break; |
| 2158 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2159 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2160 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2161 | } |
| 2162 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | } |
| 2164 | |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2165 | if (sh) { |
| 2166 | sh->pd_idx = pd_idx; |
| 2167 | sh->qd_idx = qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2168 | sh->ddf_layout = ddf_layout; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2169 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2170 | /* |
| 2171 | * Finally, compute the new sector number |
| 2172 | */ |
| 2173 | new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset; |
| 2174 | return new_sector; |
| 2175 | } |
| 2176 | |
| 2177 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2178 | 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] | 2179 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2180 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 2181 | int raid_disks = sh->disks; |
| 2182 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | sector_t new_sector = sh->sector, check; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 2184 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
| 2185 | : conf->chunk_sectors; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2186 | int algorithm = previous ? conf->prev_algo |
| 2187 | : conf->algorithm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2188 | sector_t stripe; |
| 2189 | int chunk_offset; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2190 | sector_t chunk_number; |
| 2191 | int dummy1, dd_idx = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | sector_t r_sector; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2193 | struct stripe_head sh2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2194 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2196 | chunk_offset = sector_div(new_sector, sectors_per_chunk); |
| 2197 | stripe = new_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2198 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2199 | if (i == sh->pd_idx) |
| 2200 | return 0; |
| 2201 | switch(conf->level) { |
| 2202 | case 4: break; |
| 2203 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2204 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2205 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 2206 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 2207 | if (i > sh->pd_idx) |
| 2208 | i--; |
| 2209 | break; |
| 2210 | case ALGORITHM_LEFT_SYMMETRIC: |
| 2211 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 2212 | if (i < sh->pd_idx) |
| 2213 | i += raid_disks; |
| 2214 | i -= (sh->pd_idx + 1); |
| 2215 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2216 | case ALGORITHM_PARITY_0: |
| 2217 | i -= 1; |
| 2218 | break; |
| 2219 | case ALGORITHM_PARITY_N: |
| 2220 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2221 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2222 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2223 | } |
| 2224 | break; |
| 2225 | case 6: |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2226 | if (i == sh->qd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2227 | return 0; /* It is the Q disk */ |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2228 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2229 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 2230 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2231 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 2232 | case ALGORITHM_ROTATING_N_RESTART: |
| 2233 | if (sh->pd_idx == raid_disks-1) |
| 2234 | i--; /* Q D D D P */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2235 | else if (i > sh->pd_idx) |
| 2236 | i -= 2; /* D D P Q D */ |
| 2237 | break; |
| 2238 | case ALGORITHM_LEFT_SYMMETRIC: |
| 2239 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 2240 | if (sh->pd_idx == raid_disks-1) |
| 2241 | i--; /* Q D D D P */ |
| 2242 | else { |
| 2243 | /* D D P Q D */ |
| 2244 | if (i < sh->pd_idx) |
| 2245 | i += raid_disks; |
| 2246 | i -= (sh->pd_idx + 2); |
| 2247 | } |
| 2248 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2249 | case ALGORITHM_PARITY_0: |
| 2250 | i -= 2; |
| 2251 | break; |
| 2252 | case ALGORITHM_PARITY_N: |
| 2253 | break; |
| 2254 | case ALGORITHM_ROTATING_N_CONTINUE: |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 2255 | /* Like left_symmetric, but P is before Q */ |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2256 | if (sh->pd_idx == 0) |
| 2257 | i--; /* P D D D Q */ |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 2258 | else { |
| 2259 | /* D D Q P D */ |
| 2260 | if (i < sh->pd_idx) |
| 2261 | i += raid_disks; |
| 2262 | i -= (sh->pd_idx + 1); |
| 2263 | } |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2264 | break; |
| 2265 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 2266 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 2267 | if (i > sh->pd_idx) |
| 2268 | i--; |
| 2269 | break; |
| 2270 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 2271 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 2272 | if (i < sh->pd_idx) |
| 2273 | i += data_disks + 1; |
| 2274 | i -= (sh->pd_idx + 1); |
| 2275 | break; |
| 2276 | case ALGORITHM_PARITY_0_6: |
| 2277 | i -= 1; |
| 2278 | break; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2279 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2280 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2281 | } |
| 2282 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2283 | } |
| 2284 | |
| 2285 | chunk_number = stripe * data_disks + i; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2286 | r_sector = chunk_number * sectors_per_chunk + chunk_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2287 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2288 | check = raid5_compute_sector(conf, r_sector, |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2289 | previous, &dummy1, &sh2); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2290 | if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx |
| 2291 | || sh2.qd_idx != sh->qd_idx) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 2292 | printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n", |
| 2293 | mdname(conf->mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2294 | return 0; |
| 2295 | } |
| 2296 | return r_sector; |
| 2297 | } |
| 2298 | |
| 2299 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2300 | static void |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2301 | schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2302 | int rcw, int expand) |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2303 | { |
| 2304 | int i, pd_idx = sh->pd_idx, disks = sh->disks; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2305 | struct r5conf *conf = sh->raid_conf; |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2306 | int level = conf->level; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2307 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2308 | if (rcw) { |
| 2309 | /* if we are not expanding this is a proper write request, and |
| 2310 | * there will be bios with new data to be drained into the |
| 2311 | * stripe cache |
| 2312 | */ |
| 2313 | if (!expand) { |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2314 | sh->reconstruct_state = reconstruct_state_drain_run; |
| 2315 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
| 2316 | } else |
| 2317 | sh->reconstruct_state = reconstruct_state_run; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2318 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2319 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2320 | |
| 2321 | for (i = disks; i--; ) { |
| 2322 | struct r5dev *dev = &sh->dev[i]; |
| 2323 | |
| 2324 | if (dev->towrite) { |
| 2325 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2326 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2327 | if (!expand) |
| 2328 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2329 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2330 | } |
| 2331 | } |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2332 | if (s->locked + conf->max_degraded == disks) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2333 | if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state)) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2334 | atomic_inc(&conf->pending_full_writes); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2335 | } else { |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2336 | BUG_ON(level == 6); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2337 | BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) || |
| 2338 | test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags))); |
| 2339 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2340 | sh->reconstruct_state = reconstruct_state_prexor_drain_run; |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2341 | set_bit(STRIPE_OP_PREXOR, &s->ops_request); |
| 2342 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2343 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2344 | |
| 2345 | for (i = disks; i--; ) { |
| 2346 | struct r5dev *dev = &sh->dev[i]; |
| 2347 | if (i == pd_idx) |
| 2348 | continue; |
| 2349 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2350 | if (dev->towrite && |
| 2351 | (test_bit(R5_UPTODATE, &dev->flags) || |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2352 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2353 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2354 | set_bit(R5_LOCKED, &dev->flags); |
| 2355 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2356 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2357 | } |
| 2358 | } |
| 2359 | } |
| 2360 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2361 | /* keep the parity disk(s) locked while asynchronous operations |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2362 | * are in flight |
| 2363 | */ |
| 2364 | set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); |
| 2365 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2366 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2367 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2368 | if (level == 6) { |
| 2369 | int qd_idx = sh->qd_idx; |
| 2370 | struct r5dev *dev = &sh->dev[qd_idx]; |
| 2371 | |
| 2372 | set_bit(R5_LOCKED, &dev->flags); |
| 2373 | clear_bit(R5_UPTODATE, &dev->flags); |
| 2374 | s->locked++; |
| 2375 | } |
| 2376 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2377 | pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n", |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 2378 | __func__, (unsigned long long)sh->sector, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2379 | s->locked, s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2380 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2381 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2382 | /* |
| 2383 | * Each stripe/dev can have one or more bion attached. |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2384 | * toread/towrite point to the first in a chain. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2385 | * The bi_next chain must be in order. |
| 2386 | */ |
| 2387 | static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite) |
| 2388 | { |
| 2389 | struct bio **bip; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2390 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2391 | int firstwrite=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2392 | |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 2393 | pr_debug("adding bi b#%llu to stripe s#%llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2394 | (unsigned long long)bi->bi_sector, |
| 2395 | (unsigned long long)sh->sector); |
| 2396 | |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2397 | /* |
| 2398 | * If several bio share a stripe. The bio bi_phys_segments acts as a |
| 2399 | * reference count to avoid race. The reference count should already be |
| 2400 | * increased before this function is called (for example, in |
| 2401 | * make_request()), so other bio sharing this stripe will not free the |
| 2402 | * stripe. If a stripe is owned by one stripe, the stripe lock will |
| 2403 | * protect it. |
| 2404 | */ |
| 2405 | spin_lock_irq(&sh->stripe_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2406 | if (forwrite) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2407 | bip = &sh->dev[dd_idx].towrite; |
Shaohua Li | 7eaf7e8 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2408 | if (*bip == NULL) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2409 | firstwrite = 1; |
| 2410 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2411 | bip = &sh->dev[dd_idx].toread; |
| 2412 | while (*bip && (*bip)->bi_sector < bi->bi_sector) { |
| 2413 | if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector) |
| 2414 | goto overlap; |
| 2415 | bip = & (*bip)->bi_next; |
| 2416 | } |
| 2417 | if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9)) |
| 2418 | goto overlap; |
| 2419 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 2420 | BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2421 | if (*bip) |
| 2422 | bi->bi_next = *bip; |
| 2423 | *bip = bi; |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2424 | raid5_inc_bi_active_stripes(bi); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2425 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2426 | if (forwrite) { |
| 2427 | /* check if page is covered */ |
| 2428 | sector_t sector = sh->dev[dd_idx].sector; |
| 2429 | for (bi=sh->dev[dd_idx].towrite; |
| 2430 | sector < sh->dev[dd_idx].sector + STRIPE_SECTORS && |
| 2431 | bi && bi->bi_sector <= sector; |
| 2432 | bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) { |
| 2433 | if (bi->bi_sector + (bi->bi_size>>9) >= sector) |
| 2434 | sector = bi->bi_sector + (bi->bi_size>>9); |
| 2435 | } |
| 2436 | if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS) |
| 2437 | set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags); |
| 2438 | } |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2439 | spin_unlock_irq(&sh->stripe_lock); |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 2440 | |
| 2441 | pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n", |
| 2442 | (unsigned long long)(*bip)->bi_sector, |
| 2443 | (unsigned long long)sh->sector, dd_idx); |
| 2444 | |
| 2445 | if (conf->mddev->bitmap && firstwrite) { |
| 2446 | bitmap_startwrite(conf->mddev->bitmap, sh->sector, |
| 2447 | STRIPE_SECTORS, 0); |
| 2448 | sh->bm_seq = conf->seq_flush+1; |
| 2449 | set_bit(STRIPE_BIT_DELAY, &sh->state); |
| 2450 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2451 | return 1; |
| 2452 | |
| 2453 | overlap: |
| 2454 | set_bit(R5_Overlap, &sh->dev[dd_idx].flags); |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2455 | spin_unlock_irq(&sh->stripe_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2456 | return 0; |
| 2457 | } |
| 2458 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2459 | static void end_reshape(struct r5conf *conf); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 2460 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2461 | static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2462 | struct stripe_head *sh) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2463 | { |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2464 | int sectors_per_chunk = |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 2465 | previous ? conf->prev_chunk_sectors : conf->chunk_sectors; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2466 | int dd_idx; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 2467 | int chunk_offset = sector_div(stripe, sectors_per_chunk); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2468 | int disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 2469 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2470 | raid5_compute_sector(conf, |
| 2471 | stripe * (disks - conf->max_degraded) |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 2472 | *sectors_per_chunk + chunk_offset, |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2473 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2474 | &dd_idx, sh); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2475 | } |
| 2476 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2477 | static void |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2478 | handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2479 | struct stripe_head_state *s, int disks, |
| 2480 | struct bio **return_bi) |
| 2481 | { |
| 2482 | int i; |
| 2483 | for (i = disks; i--; ) { |
| 2484 | struct bio *bi; |
| 2485 | int bitmap_end = 0; |
| 2486 | |
| 2487 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2488 | struct md_rdev *rdev; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2489 | rcu_read_lock(); |
| 2490 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 2491 | if (rdev && test_bit(In_sync, &rdev->flags)) |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2492 | atomic_inc(&rdev->nr_pending); |
| 2493 | else |
| 2494 | rdev = NULL; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2495 | rcu_read_unlock(); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2496 | if (rdev) { |
| 2497 | if (!rdev_set_badblocks( |
| 2498 | rdev, |
| 2499 | sh->sector, |
| 2500 | STRIPE_SECTORS, 0)) |
| 2501 | md_error(conf->mddev, rdev); |
| 2502 | rdev_dec_pending(rdev, conf->mddev); |
| 2503 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2504 | } |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2505 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2506 | /* fail all writes first */ |
| 2507 | bi = sh->dev[i].towrite; |
| 2508 | sh->dev[i].towrite = NULL; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2509 | spin_unlock_irq(&sh->stripe_lock); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2510 | if (bi) { |
| 2511 | s->to_write--; |
| 2512 | bitmap_end = 1; |
| 2513 | } |
| 2514 | |
| 2515 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 2516 | wake_up(&conf->wait_for_overlap); |
| 2517 | |
| 2518 | while (bi && bi->bi_sector < |
| 2519 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2520 | struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); |
| 2521 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2522 | if (!raid5_dec_bi_active_stripes(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2523 | md_write_end(conf->mddev); |
| 2524 | bi->bi_next = *return_bi; |
| 2525 | *return_bi = bi; |
| 2526 | } |
| 2527 | bi = nextbi; |
| 2528 | } |
Shaohua Li | 7eaf7e8 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2529 | if (bitmap_end) |
| 2530 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 2531 | STRIPE_SECTORS, 0, 0); |
| 2532 | bitmap_end = 0; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2533 | /* and fail all 'written' */ |
| 2534 | bi = sh->dev[i].written; |
| 2535 | sh->dev[i].written = NULL; |
| 2536 | if (bi) bitmap_end = 1; |
| 2537 | while (bi && bi->bi_sector < |
| 2538 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2539 | struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector); |
| 2540 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2541 | if (!raid5_dec_bi_active_stripes(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2542 | md_write_end(conf->mddev); |
| 2543 | bi->bi_next = *return_bi; |
| 2544 | *return_bi = bi; |
| 2545 | } |
| 2546 | bi = bi2; |
| 2547 | } |
| 2548 | |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2549 | /* fail any reads if this device is non-operational and |
| 2550 | * the data has not reached the cache yet. |
| 2551 | */ |
| 2552 | if (!test_bit(R5_Wantfill, &sh->dev[i].flags) && |
| 2553 | (!test_bit(R5_Insync, &sh->dev[i].flags) || |
| 2554 | test_bit(R5_ReadError, &sh->dev[i].flags))) { |
NeilBrown | 143c4d0 | 2012-10-11 13:50:12 +1100 | [diff] [blame^] | 2555 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2556 | bi = sh->dev[i].toread; |
| 2557 | sh->dev[i].toread = NULL; |
NeilBrown | 143c4d0 | 2012-10-11 13:50:12 +1100 | [diff] [blame^] | 2558 | spin_unlock_irq(&sh->stripe_lock); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2559 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 2560 | wake_up(&conf->wait_for_overlap); |
| 2561 | if (bi) s->to_read--; |
| 2562 | while (bi && bi->bi_sector < |
| 2563 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2564 | struct bio *nextbi = |
| 2565 | r5_next_bio(bi, sh->dev[i].sector); |
| 2566 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2567 | if (!raid5_dec_bi_active_stripes(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2568 | bi->bi_next = *return_bi; |
| 2569 | *return_bi = bi; |
| 2570 | } |
| 2571 | bi = nextbi; |
| 2572 | } |
| 2573 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2574 | if (bitmap_end) |
| 2575 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 2576 | STRIPE_SECTORS, 0, 0); |
NeilBrown | 8cfa7b0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2577 | /* If we were in the middle of a write the parity block might |
| 2578 | * still be locked - so just clear all R5_LOCKED flags |
| 2579 | */ |
| 2580 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2581 | } |
| 2582 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2583 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 2584 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 2585 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2586 | } |
| 2587 | |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2588 | static void |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2589 | handle_failed_sync(struct r5conf *conf, struct stripe_head *sh, |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2590 | struct stripe_head_state *s) |
| 2591 | { |
| 2592 | int abort = 0; |
| 2593 | int i; |
| 2594 | |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2595 | clear_bit(STRIPE_SYNCING, &sh->state); |
| 2596 | s->syncing = 0; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2597 | s->replacing = 0; |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2598 | /* There is nothing more to do for sync/check/repair. |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 2599 | * Don't even need to abort as that is handled elsewhere |
| 2600 | * if needed, and not always wanted e.g. if there is a known |
| 2601 | * bad block here. |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2602 | * For recover/replace we need to record a bad block on all |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2603 | * non-sync devices, or abort the recovery |
| 2604 | */ |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 2605 | if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) { |
| 2606 | /* During recovery devices cannot be removed, so |
| 2607 | * locking and refcounting of rdevs is not needed |
| 2608 | */ |
| 2609 | for (i = 0; i < conf->raid_disks; i++) { |
| 2610 | struct md_rdev *rdev = conf->disks[i].rdev; |
| 2611 | if (rdev |
| 2612 | && !test_bit(Faulty, &rdev->flags) |
| 2613 | && !test_bit(In_sync, &rdev->flags) |
| 2614 | && !rdev_set_badblocks(rdev, sh->sector, |
| 2615 | STRIPE_SECTORS, 0)) |
| 2616 | abort = 1; |
| 2617 | rdev = conf->disks[i].replacement; |
| 2618 | if (rdev |
| 2619 | && !test_bit(Faulty, &rdev->flags) |
| 2620 | && !test_bit(In_sync, &rdev->flags) |
| 2621 | && !rdev_set_badblocks(rdev, sh->sector, |
| 2622 | STRIPE_SECTORS, 0)) |
| 2623 | abort = 1; |
| 2624 | } |
| 2625 | if (abort) |
| 2626 | conf->recovery_disabled = |
| 2627 | conf->mddev->recovery_disabled; |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2628 | } |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 2629 | md_done_sync(conf->mddev, STRIPE_SECTORS, !abort); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2630 | } |
| 2631 | |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2632 | static int want_replace(struct stripe_head *sh, int disk_idx) |
| 2633 | { |
| 2634 | struct md_rdev *rdev; |
| 2635 | int rv = 0; |
| 2636 | /* Doing recovery so rcu locking not required */ |
| 2637 | rdev = sh->raid_conf->disks[disk_idx].replacement; |
| 2638 | if (rdev |
| 2639 | && !test_bit(Faulty, &rdev->flags) |
| 2640 | && !test_bit(In_sync, &rdev->flags) |
| 2641 | && (rdev->recovery_offset <= sh->sector |
| 2642 | || rdev->mddev->recovery_cp <= sh->sector)) |
| 2643 | rv = 1; |
| 2644 | |
| 2645 | return rv; |
| 2646 | } |
| 2647 | |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2648 | /* 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] | 2649 | * to be read or computed to satisfy a request. |
| 2650 | * |
| 2651 | * Returns 1 when no more member devices need to be checked, otherwise returns |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2652 | * 0 to tell the loop in handle_stripe_fill to continue |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2653 | */ |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2654 | static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s, |
| 2655 | int disk_idx, int disks) |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2656 | { |
| 2657 | struct r5dev *dev = &sh->dev[disk_idx]; |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2658 | struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]], |
| 2659 | &sh->dev[s->failed_num[1]] }; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2660 | |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2661 | /* is the data in this block needed, and can we get it? */ |
| 2662 | if (!test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2663 | !test_bit(R5_UPTODATE, &dev->flags) && |
| 2664 | (dev->toread || |
| 2665 | (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) || |
| 2666 | s->syncing || s->expanding || |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2667 | (s->replacing && want_replace(sh, disk_idx)) || |
NeilBrown | 5d35e09 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2668 | (s->failed >= 1 && fdev[0]->toread) || |
| 2669 | (s->failed >= 2 && fdev[1]->toread) || |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2670 | (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite && |
| 2671 | !test_bit(R5_OVERWRITE, &fdev[0]->flags)) || |
| 2672 | (sh->raid_conf->level == 6 && s->failed && s->to_write))) { |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2673 | /* we would like to get this block, possibly by computing it, |
| 2674 | * otherwise read it if the backing disk is insync |
| 2675 | */ |
| 2676 | BUG_ON(test_bit(R5_Wantcompute, &dev->flags)); |
| 2677 | BUG_ON(test_bit(R5_Wantread, &dev->flags)); |
| 2678 | if ((s->uptodate == disks - 1) && |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2679 | (s->failed && (disk_idx == s->failed_num[0] || |
| 2680 | disk_idx == s->failed_num[1]))) { |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2681 | /* have disk failed, and we're requested to fetch it; |
| 2682 | * do compute it |
| 2683 | */ |
| 2684 | pr_debug("Computing stripe %llu block %d\n", |
| 2685 | (unsigned long long)sh->sector, disk_idx); |
| 2686 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2687 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2688 | set_bit(R5_Wantcompute, &dev->flags); |
| 2689 | sh->ops.target = disk_idx; |
| 2690 | sh->ops.target2 = -1; /* no 2nd target */ |
| 2691 | s->req_compute = 1; |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2692 | /* Careful: from this point on 'uptodate' is in the eye |
| 2693 | * of raid_run_ops which services 'compute' operations |
| 2694 | * before writes. R5_Wantcompute flags a block that will |
| 2695 | * be R5_UPTODATE by the time it is needed for a |
| 2696 | * subsequent operation. |
| 2697 | */ |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2698 | s->uptodate++; |
| 2699 | return 1; |
| 2700 | } else if (s->uptodate == disks-2 && s->failed >= 2) { |
| 2701 | /* Computing 2-failure is *very* expensive; only |
| 2702 | * do it if failed >= 2 |
| 2703 | */ |
| 2704 | int other; |
| 2705 | for (other = disks; other--; ) { |
| 2706 | if (other == disk_idx) |
| 2707 | continue; |
| 2708 | if (!test_bit(R5_UPTODATE, |
| 2709 | &sh->dev[other].flags)) |
| 2710 | break; |
| 2711 | } |
| 2712 | BUG_ON(other < 0); |
| 2713 | pr_debug("Computing stripe %llu blocks %d,%d\n", |
| 2714 | (unsigned long long)sh->sector, |
| 2715 | disk_idx, other); |
| 2716 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2717 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2718 | set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags); |
| 2719 | set_bit(R5_Wantcompute, &sh->dev[other].flags); |
| 2720 | sh->ops.target = disk_idx; |
| 2721 | sh->ops.target2 = other; |
| 2722 | s->uptodate += 2; |
| 2723 | s->req_compute = 1; |
| 2724 | return 1; |
| 2725 | } else if (test_bit(R5_Insync, &dev->flags)) { |
| 2726 | set_bit(R5_LOCKED, &dev->flags); |
| 2727 | set_bit(R5_Wantread, &dev->flags); |
| 2728 | s->locked++; |
| 2729 | pr_debug("Reading block %d (sync=%d)\n", |
| 2730 | disk_idx, s->syncing); |
| 2731 | } |
| 2732 | } |
| 2733 | |
| 2734 | return 0; |
| 2735 | } |
| 2736 | |
| 2737 | /** |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2738 | * handle_stripe_fill - read or compute data to satisfy pending requests. |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2739 | */ |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2740 | static void handle_stripe_fill(struct stripe_head *sh, |
| 2741 | struct stripe_head_state *s, |
| 2742 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2743 | { |
| 2744 | int i; |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2745 | |
| 2746 | /* look for blocks to read/compute, skip this if a compute |
| 2747 | * is already in flight, or if the stripe contents are in the |
| 2748 | * midst of changing due to a write |
| 2749 | */ |
| 2750 | if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state && |
| 2751 | !sh->reconstruct_state) |
| 2752 | for (i = disks; i--; ) |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2753 | if (fetch_block(sh, s, i, disks)) |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2754 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2755 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2756 | } |
| 2757 | |
| 2758 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2759 | /* handle_stripe_clean_event |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2760 | * any written block on an uptodate or failed drive can be returned. |
| 2761 | * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but |
| 2762 | * never LOCKED, so we don't need to test 'failed' directly. |
| 2763 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2764 | static void handle_stripe_clean_event(struct r5conf *conf, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2765 | struct stripe_head *sh, int disks, struct bio **return_bi) |
| 2766 | { |
| 2767 | int i; |
| 2768 | struct r5dev *dev; |
| 2769 | |
| 2770 | for (i = disks; i--; ) |
| 2771 | if (sh->dev[i].written) { |
| 2772 | dev = &sh->dev[i]; |
| 2773 | if (!test_bit(R5_LOCKED, &dev->flags) && |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 2774 | (test_bit(R5_UPTODATE, &dev->flags) || |
| 2775 | test_and_clear_bit(R5_Discard, &dev->flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2776 | /* We can return any write requests */ |
| 2777 | struct bio *wbi, *wbi2; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2778 | pr_debug("Return write for disc %d\n", i); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2779 | wbi = dev->written; |
| 2780 | dev->written = NULL; |
| 2781 | while (wbi && wbi->bi_sector < |
| 2782 | dev->sector + STRIPE_SECTORS) { |
| 2783 | wbi2 = r5_next_bio(wbi, dev->sector); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2784 | if (!raid5_dec_bi_active_stripes(wbi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2785 | md_write_end(conf->mddev); |
| 2786 | wbi->bi_next = *return_bi; |
| 2787 | *return_bi = wbi; |
| 2788 | } |
| 2789 | wbi = wbi2; |
| 2790 | } |
Shaohua Li | 7eaf7e8 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2791 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 2792 | STRIPE_SECTORS, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2793 | !test_bit(STRIPE_DEGRADED, &sh->state), |
Shaohua Li | 7eaf7e8 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2794 | 0); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2795 | } |
| 2796 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2797 | |
| 2798 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 2799 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 2800 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2801 | } |
| 2802 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2803 | static void handle_stripe_dirtying(struct r5conf *conf, |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2804 | struct stripe_head *sh, |
| 2805 | struct stripe_head_state *s, |
| 2806 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2807 | { |
| 2808 | int rmw = 0, rcw = 0, i; |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2809 | if (conf->max_degraded == 2) { |
| 2810 | /* RAID6 requires 'rcw' in current implementation |
| 2811 | * Calculate the real rcw later - for now fake it |
| 2812 | * look like rcw is cheaper |
| 2813 | */ |
| 2814 | rcw = 1; rmw = 2; |
| 2815 | } else for (i = disks; i--; ) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2816 | /* would I have to read this buffer for read_modify_write */ |
| 2817 | struct r5dev *dev = &sh->dev[i]; |
| 2818 | if ((dev->towrite || i == sh->pd_idx) && |
| 2819 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2820 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2821 | test_bit(R5_Wantcompute, &dev->flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2822 | if (test_bit(R5_Insync, &dev->flags)) |
| 2823 | rmw++; |
| 2824 | else |
| 2825 | rmw += 2*disks; /* cannot read it */ |
| 2826 | } |
| 2827 | /* Would I have to read this buffer for reconstruct_write */ |
| 2828 | if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx && |
| 2829 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2830 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2831 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2832 | if (test_bit(R5_Insync, &dev->flags)) rcw++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2833 | else |
| 2834 | rcw += 2*disks; |
| 2835 | } |
| 2836 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2837 | pr_debug("for sector %llu, rmw=%d rcw=%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2838 | (unsigned long long)sh->sector, rmw, rcw); |
| 2839 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2840 | if (rmw < rcw && rmw > 0) |
| 2841 | /* prefer read-modify-write, but need to get some data */ |
| 2842 | for (i = disks; i--; ) { |
| 2843 | struct r5dev *dev = &sh->dev[i]; |
| 2844 | if ((dev->towrite || i == sh->pd_idx) && |
| 2845 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2846 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2847 | test_bit(R5_Wantcompute, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2848 | test_bit(R5_Insync, &dev->flags)) { |
| 2849 | if ( |
| 2850 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2851 | pr_debug("Read_old block " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2852 | "%d for r-m-w\n", i); |
| 2853 | set_bit(R5_LOCKED, &dev->flags); |
| 2854 | set_bit(R5_Wantread, &dev->flags); |
| 2855 | s->locked++; |
| 2856 | } else { |
| 2857 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2858 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2859 | } |
| 2860 | } |
| 2861 | } |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2862 | if (rcw <= rmw && rcw > 0) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2863 | /* want reconstruct write, but need to get some data */ |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2864 | rcw = 0; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2865 | for (i = disks; i--; ) { |
| 2866 | struct r5dev *dev = &sh->dev[i]; |
| 2867 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2868 | i != sh->pd_idx && i != sh->qd_idx && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2869 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2870 | !(test_bit(R5_UPTODATE, &dev->flags) || |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2871 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2872 | rcw++; |
| 2873 | if (!test_bit(R5_Insync, &dev->flags)) |
| 2874 | continue; /* it's a failed drive */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2875 | if ( |
| 2876 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2877 | pr_debug("Read_old block " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2878 | "%d for Reconstruct\n", i); |
| 2879 | set_bit(R5_LOCKED, &dev->flags); |
| 2880 | set_bit(R5_Wantread, &dev->flags); |
| 2881 | s->locked++; |
| 2882 | } else { |
| 2883 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2884 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2885 | } |
| 2886 | } |
| 2887 | } |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2888 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2889 | /* now if nothing is locked, and if we have enough data, |
| 2890 | * we can start a write request |
| 2891 | */ |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2892 | /* since handle_stripe can be called at any time we need to handle the |
| 2893 | * case where a compute block operation has been submitted and then a |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2894 | * subsequent call wants to start a write request. raid_run_ops only |
| 2895 | * handles the case where compute block and reconstruct are requested |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2896 | * simultaneously. If this is not the case then new writes need to be |
| 2897 | * held off until the compute completes. |
| 2898 | */ |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2899 | if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) && |
| 2900 | (s->locked == 0 && (rcw == 0 || rmw == 0) && |
| 2901 | !test_bit(STRIPE_BIT_DELAY, &sh->state))) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2902 | schedule_reconstruction(sh, s, rcw == 0, 0); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2903 | } |
| 2904 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2905 | static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2906 | struct stripe_head_state *s, int disks) |
| 2907 | { |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2908 | struct r5dev *dev = NULL; |
Dan Williams | e89f896 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2909 | |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2910 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2911 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2912 | switch (sh->check_state) { |
| 2913 | case check_state_idle: |
| 2914 | /* start a new check operation if there are no failures */ |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2915 | if (s->failed == 0) { |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2916 | BUG_ON(s->uptodate != disks); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2917 | sh->check_state = check_state_run; |
| 2918 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2919 | clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2920 | s->uptodate--; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2921 | break; |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2922 | } |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2923 | dev = &sh->dev[s->failed_num[0]]; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2924 | /* fall through */ |
| 2925 | case check_state_compute_result: |
| 2926 | sh->check_state = check_state_idle; |
| 2927 | if (!dev) |
| 2928 | dev = &sh->dev[sh->pd_idx]; |
| 2929 | |
| 2930 | /* check that a write has not made the stripe insync */ |
| 2931 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 2932 | break; |
| 2933 | |
| 2934 | /* either failed parity check, or recovery is happening */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2935 | BUG_ON(!test_bit(R5_UPTODATE, &dev->flags)); |
| 2936 | BUG_ON(s->uptodate != disks); |
| 2937 | |
| 2938 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2939 | s->locked++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2940 | set_bit(R5_Wantwrite, &dev->flags); |
Dan Williams | 830ea01 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2941 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2942 | clear_bit(STRIPE_DEGRADED, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2943 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2944 | break; |
| 2945 | case check_state_run: |
| 2946 | break; /* we will be called again upon completion */ |
| 2947 | case check_state_check_result: |
| 2948 | sh->check_state = check_state_idle; |
| 2949 | |
| 2950 | /* if a failure occurred during the check operation, leave |
| 2951 | * STRIPE_INSYNC not set and let the stripe be handled again |
| 2952 | */ |
| 2953 | if (s->failed) |
| 2954 | break; |
| 2955 | |
| 2956 | /* handle a successful check operation, if parity is correct |
| 2957 | * we are done. Otherwise update the mismatch count and repair |
| 2958 | * parity if !MD_RECOVERY_CHECK |
| 2959 | */ |
Dan Williams | ad283ea | 2009-08-29 19:09:26 -0700 | [diff] [blame] | 2960 | if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0) |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2961 | /* parity is correct (on disc, |
| 2962 | * not in buffer any more) |
| 2963 | */ |
| 2964 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2965 | else { |
| 2966 | conf->mddev->resync_mismatches += STRIPE_SECTORS; |
| 2967 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 2968 | /* don't try to repair!! */ |
| 2969 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2970 | else { |
| 2971 | sh->check_state = check_state_compute_run; |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2972 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2973 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2974 | set_bit(R5_Wantcompute, |
| 2975 | &sh->dev[sh->pd_idx].flags); |
| 2976 | sh->ops.target = sh->pd_idx; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2977 | sh->ops.target2 = -1; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2978 | s->uptodate++; |
| 2979 | } |
| 2980 | } |
| 2981 | break; |
| 2982 | case check_state_compute_run: |
| 2983 | break; |
| 2984 | default: |
| 2985 | printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n", |
| 2986 | __func__, sh->check_state, |
| 2987 | (unsigned long long) sh->sector); |
| 2988 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2989 | } |
| 2990 | } |
| 2991 | |
| 2992 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2993 | static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2994 | struct stripe_head_state *s, |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2995 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2996 | { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2997 | int pd_idx = sh->pd_idx; |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 2998 | int qd_idx = sh->qd_idx; |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2999 | struct r5dev *dev; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3000 | |
| 3001 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3002 | |
| 3003 | BUG_ON(s->failed > 2); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3004 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3005 | /* Want to check and possibly repair P and Q. |
| 3006 | * However there could be one 'failed' device, in which |
| 3007 | * case we can only check one of them, possibly using the |
| 3008 | * other to generate missing data |
| 3009 | */ |
| 3010 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3011 | switch (sh->check_state) { |
| 3012 | case check_state_idle: |
| 3013 | /* start a new check operation if there are < 2 failures */ |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3014 | if (s->failed == s->q_failed) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3015 | /* The only possible failed device holds Q, so it |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3016 | * makes sense to check P (If anything else were failed, |
| 3017 | * we would have used P to recreate it). |
| 3018 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3019 | sh->check_state = check_state_run; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3020 | } |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3021 | if (!s->q_failed && s->failed < 2) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3022 | /* Q is not failed, and we didn't use it to generate |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3023 | * anything, so it makes sense to check it |
| 3024 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3025 | if (sh->check_state == check_state_run) |
| 3026 | sh->check_state = check_state_run_pq; |
| 3027 | else |
| 3028 | sh->check_state = check_state_run_q; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3029 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3030 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3031 | /* discard potentially stale zero_sum_result */ |
| 3032 | sh->ops.zero_sum_result = 0; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3033 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3034 | if (sh->check_state == check_state_run) { |
| 3035 | /* async_xor_zero_sum destroys the contents of P */ |
| 3036 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
| 3037 | s->uptodate--; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3038 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3039 | if (sh->check_state >= check_state_run && |
| 3040 | sh->check_state <= check_state_run_pq) { |
| 3041 | /* async_syndrome_zero_sum preserves P and Q, so |
| 3042 | * no need to mark them !uptodate here |
| 3043 | */ |
| 3044 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
| 3045 | break; |
| 3046 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3047 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3048 | /* we have 2-disk failure */ |
| 3049 | BUG_ON(s->failed != 2); |
| 3050 | /* fall through */ |
| 3051 | case check_state_compute_result: |
| 3052 | sh->check_state = check_state_idle; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3053 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3054 | /* check that a write has not made the stripe insync */ |
| 3055 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 3056 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3057 | |
| 3058 | /* now write out any block on a failed drive, |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3059 | * or P or Q if they were recomputed |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3060 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3061 | 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] | 3062 | if (s->failed == 2) { |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3063 | dev = &sh->dev[s->failed_num[1]]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3064 | s->locked++; |
| 3065 | set_bit(R5_LOCKED, &dev->flags); |
| 3066 | set_bit(R5_Wantwrite, &dev->flags); |
| 3067 | } |
| 3068 | if (s->failed >= 1) { |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3069 | dev = &sh->dev[s->failed_num[0]]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3070 | s->locked++; |
| 3071 | set_bit(R5_LOCKED, &dev->flags); |
| 3072 | set_bit(R5_Wantwrite, &dev->flags); |
| 3073 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3074 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3075 | dev = &sh->dev[pd_idx]; |
| 3076 | s->locked++; |
| 3077 | set_bit(R5_LOCKED, &dev->flags); |
| 3078 | set_bit(R5_Wantwrite, &dev->flags); |
| 3079 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3080 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3081 | dev = &sh->dev[qd_idx]; |
| 3082 | s->locked++; |
| 3083 | set_bit(R5_LOCKED, &dev->flags); |
| 3084 | set_bit(R5_Wantwrite, &dev->flags); |
| 3085 | } |
| 3086 | clear_bit(STRIPE_DEGRADED, &sh->state); |
| 3087 | |
| 3088 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3089 | break; |
| 3090 | case check_state_run: |
| 3091 | case check_state_run_q: |
| 3092 | case check_state_run_pq: |
| 3093 | break; /* we will be called again upon completion */ |
| 3094 | case check_state_check_result: |
| 3095 | sh->check_state = check_state_idle; |
| 3096 | |
| 3097 | /* handle a successful check operation, if parity is correct |
| 3098 | * we are done. Otherwise update the mismatch count and repair |
| 3099 | * parity if !MD_RECOVERY_CHECK |
| 3100 | */ |
| 3101 | if (sh->ops.zero_sum_result == 0) { |
| 3102 | /* both parities are correct */ |
| 3103 | if (!s->failed) |
| 3104 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3105 | else { |
| 3106 | /* in contrast to the raid5 case we can validate |
| 3107 | * parity, but still have a failure to write |
| 3108 | * back |
| 3109 | */ |
| 3110 | sh->check_state = check_state_compute_result; |
| 3111 | /* Returning at this point means that we may go |
| 3112 | * off and bring p and/or q uptodate again so |
| 3113 | * we make sure to check zero_sum_result again |
| 3114 | * to verify if p or q need writeback |
| 3115 | */ |
| 3116 | } |
| 3117 | } else { |
| 3118 | conf->mddev->resync_mismatches += STRIPE_SECTORS; |
| 3119 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 3120 | /* don't try to repair!! */ |
| 3121 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3122 | else { |
| 3123 | int *target = &sh->ops.target; |
| 3124 | |
| 3125 | sh->ops.target = -1; |
| 3126 | sh->ops.target2 = -1; |
| 3127 | sh->check_state = check_state_compute_run; |
| 3128 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 3129 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 3130 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
| 3131 | set_bit(R5_Wantcompute, |
| 3132 | &sh->dev[pd_idx].flags); |
| 3133 | *target = pd_idx; |
| 3134 | target = &sh->ops.target2; |
| 3135 | s->uptodate++; |
| 3136 | } |
| 3137 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
| 3138 | set_bit(R5_Wantcompute, |
| 3139 | &sh->dev[qd_idx].flags); |
| 3140 | *target = qd_idx; |
| 3141 | s->uptodate++; |
| 3142 | } |
| 3143 | } |
| 3144 | } |
| 3145 | break; |
| 3146 | case check_state_compute_run: |
| 3147 | break; |
| 3148 | default: |
| 3149 | printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n", |
| 3150 | __func__, sh->check_state, |
| 3151 | (unsigned long long) sh->sector); |
| 3152 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3153 | } |
| 3154 | } |
| 3155 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3156 | static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3157 | { |
| 3158 | int i; |
| 3159 | |
| 3160 | /* We have read all the blocks in this stripe and now we need to |
| 3161 | * copy some of them into a target stripe for expand. |
| 3162 | */ |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3163 | struct dma_async_tx_descriptor *tx = NULL; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3164 | clear_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 3165 | for (i = 0; i < sh->disks; i++) |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 3166 | if (i != sh->pd_idx && i != sh->qd_idx) { |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3167 | int dd_idx, j; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3168 | struct stripe_head *sh2; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 3169 | struct async_submit_ctl submit; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3170 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 3171 | sector_t bn = compute_blocknr(sh, i, 1); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3172 | sector_t s = raid5_compute_sector(conf, bn, 0, |
| 3173 | &dd_idx, NULL); |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 3174 | sh2 = get_active_stripe(conf, s, 0, 1, 1); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3175 | if (sh2 == NULL) |
| 3176 | /* so far only the early blocks of this stripe |
| 3177 | * have been requested. When later blocks |
| 3178 | * get requested, we will try again |
| 3179 | */ |
| 3180 | continue; |
| 3181 | if (!test_bit(STRIPE_EXPANDING, &sh2->state) || |
| 3182 | test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) { |
| 3183 | /* must have already done this block */ |
| 3184 | release_stripe(sh2); |
| 3185 | continue; |
| 3186 | } |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3187 | |
| 3188 | /* place all the copies on one channel */ |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 3189 | init_async_submit(&submit, 0, tx, NULL, NULL, NULL); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3190 | tx = async_memcpy(sh2->dev[dd_idx].page, |
Dan Williams | 88ba2aa | 2009-04-09 16:16:18 -0700 | [diff] [blame] | 3191 | sh->dev[i].page, 0, 0, STRIPE_SIZE, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 3192 | &submit); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3193 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3194 | set_bit(R5_Expanded, &sh2->dev[dd_idx].flags); |
| 3195 | set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags); |
| 3196 | for (j = 0; j < conf->raid_disks; j++) |
| 3197 | if (j != sh2->pd_idx && |
NeilBrown | 86c374b | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3198 | j != sh2->qd_idx && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3199 | !test_bit(R5_Expanded, &sh2->dev[j].flags)) |
| 3200 | break; |
| 3201 | if (j == conf->raid_disks) { |
| 3202 | set_bit(STRIPE_EXPAND_READY, &sh2->state); |
| 3203 | set_bit(STRIPE_HANDLE, &sh2->state); |
| 3204 | } |
| 3205 | release_stripe(sh2); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3206 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3207 | } |
NeilBrown | a2e0855 | 2007-09-11 15:23:36 -0700 | [diff] [blame] | 3208 | /* done submitting copies, wait for them to complete */ |
| 3209 | if (tx) { |
| 3210 | async_tx_ack(tx); |
| 3211 | dma_wait_for_async_tx(tx); |
| 3212 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3213 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3214 | |
| 3215 | /* |
| 3216 | * handle_stripe - do things to a stripe. |
| 3217 | * |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3218 | * We lock the stripe by setting STRIPE_ACTIVE and then examine the |
| 3219 | * state of various bits to see what needs to be done. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3220 | * Possible results: |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3221 | * return some read requests which now have data |
| 3222 | * return some write requests which are safely on storage |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3223 | * schedule a read on some buffers |
| 3224 | * schedule a write of some buffers |
| 3225 | * return confirmation of parity correctness |
| 3226 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3227 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3228 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3229 | static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3230 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3231 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3232 | int disks = sh->disks; |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3233 | struct r5dev *dev; |
| 3234 | int i; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3235 | int do_recovery = 0; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3236 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3237 | memset(s, 0, sizeof(*s)); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3238 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3239 | s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 3240 | s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state); |
| 3241 | s->failed_num[0] = -1; |
| 3242 | s->failed_num[1] = -1; |
| 3243 | |
| 3244 | /* Now to look around and see what can be done */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3245 | rcu_read_lock(); |
| 3246 | for (i=disks; i--; ) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3247 | struct md_rdev *rdev; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3248 | sector_t first_bad; |
| 3249 | int bad_sectors; |
| 3250 | int is_bad = 0; |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3251 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3252 | dev = &sh->dev[i]; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3253 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3254 | 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] | 3255 | i, dev->flags, |
| 3256 | dev->toread, dev->towrite, dev->written); |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3257 | /* maybe we can reply to a read |
| 3258 | * |
| 3259 | * new wantfill requests are only permitted while |
| 3260 | * ops_complete_biofill is guaranteed to be inactive |
| 3261 | */ |
| 3262 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread && |
| 3263 | !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) |
| 3264 | set_bit(R5_Wantfill, &dev->flags); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3265 | |
| 3266 | /* now count some things */ |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3267 | if (test_bit(R5_LOCKED, &dev->flags)) |
| 3268 | s->locked++; |
| 3269 | if (test_bit(R5_UPTODATE, &dev->flags)) |
| 3270 | s->uptodate++; |
Dan Williams | 2d6e4ec | 2009-09-16 12:11:54 -0700 | [diff] [blame] | 3271 | if (test_bit(R5_Wantcompute, &dev->flags)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3272 | s->compute++; |
| 3273 | BUG_ON(s->compute > 2); |
Dan Williams | 2d6e4ec | 2009-09-16 12:11:54 -0700 | [diff] [blame] | 3274 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3275 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3276 | if (test_bit(R5_Wantfill, &dev->flags)) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3277 | s->to_fill++; |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3278 | else if (dev->toread) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3279 | s->to_read++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3280 | if (dev->towrite) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3281 | s->to_write++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3282 | if (!test_bit(R5_OVERWRITE, &dev->flags)) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3283 | s->non_overwrite++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3284 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3285 | if (dev->written) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3286 | s->written++; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3287 | /* Prefer to use the replacement for reads, but only |
| 3288 | * if it is recovered enough and has no bad blocks. |
| 3289 | */ |
| 3290 | rdev = rcu_dereference(conf->disks[i].replacement); |
| 3291 | if (rdev && !test_bit(Faulty, &rdev->flags) && |
| 3292 | rdev->recovery_offset >= sh->sector + STRIPE_SECTORS && |
| 3293 | !is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 3294 | &first_bad, &bad_sectors)) |
| 3295 | set_bit(R5_ReadRepl, &dev->flags); |
| 3296 | else { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3297 | if (rdev) |
| 3298 | set_bit(R5_NeedReplace, &dev->flags); |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3299 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 3300 | clear_bit(R5_ReadRepl, &dev->flags); |
| 3301 | } |
NeilBrown | 9283d8c | 2011-12-08 16:27:57 +1100 | [diff] [blame] | 3302 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 3303 | rdev = NULL; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3304 | if (rdev) { |
| 3305 | is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 3306 | &first_bad, &bad_sectors); |
| 3307 | if (s->blocked_rdev == NULL |
| 3308 | && (test_bit(Blocked, &rdev->flags) |
| 3309 | || is_bad < 0)) { |
| 3310 | if (is_bad < 0) |
| 3311 | set_bit(BlockedBadBlocks, |
| 3312 | &rdev->flags); |
| 3313 | s->blocked_rdev = rdev; |
| 3314 | atomic_inc(&rdev->nr_pending); |
| 3315 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3316 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3317 | clear_bit(R5_Insync, &dev->flags); |
| 3318 | if (!rdev) |
| 3319 | /* Not in-sync */; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3320 | else if (is_bad) { |
| 3321 | /* also not in-sync */ |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3322 | if (!test_bit(WriteErrorSeen, &rdev->flags) && |
| 3323 | test_bit(R5_UPTODATE, &dev->flags)) { |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3324 | /* treat as in-sync, but with a read error |
| 3325 | * which we can now try to correct |
| 3326 | */ |
| 3327 | set_bit(R5_Insync, &dev->flags); |
| 3328 | set_bit(R5_ReadError, &dev->flags); |
| 3329 | } |
| 3330 | } else if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3331 | set_bit(R5_Insync, &dev->flags); |
NeilBrown | 30d7a48 | 2011-12-23 09:57:00 +1100 | [diff] [blame] | 3332 | else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset) |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3333 | /* in sync if before recovery_offset */ |
NeilBrown | 30d7a48 | 2011-12-23 09:57:00 +1100 | [diff] [blame] | 3334 | set_bit(R5_Insync, &dev->flags); |
| 3335 | else if (test_bit(R5_UPTODATE, &dev->flags) && |
| 3336 | test_bit(R5_Expanded, &dev->flags)) |
| 3337 | /* If we've reshaped into here, we assume it is Insync. |
| 3338 | * We will shortly update recovery_offset to make |
| 3339 | * it official. |
| 3340 | */ |
| 3341 | set_bit(R5_Insync, &dev->flags); |
| 3342 | |
Adam Kwolek | 5d8c71f | 2011-12-09 14:26:11 +1100 | [diff] [blame] | 3343 | if (rdev && test_bit(R5_WriteError, &dev->flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3344 | /* This flag does not apply to '.replacement' |
| 3345 | * only to .rdev, so make sure to check that*/ |
| 3346 | struct md_rdev *rdev2 = rcu_dereference( |
| 3347 | conf->disks[i].rdev); |
| 3348 | if (rdev2 == rdev) |
| 3349 | clear_bit(R5_Insync, &dev->flags); |
| 3350 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3351 | s->handle_bad_blocks = 1; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3352 | atomic_inc(&rdev2->nr_pending); |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3353 | } else |
| 3354 | clear_bit(R5_WriteError, &dev->flags); |
| 3355 | } |
Adam Kwolek | 5d8c71f | 2011-12-09 14:26:11 +1100 | [diff] [blame] | 3356 | if (rdev && test_bit(R5_MadeGood, &dev->flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3357 | /* This flag does not apply to '.replacement' |
| 3358 | * only to .rdev, so make sure to check that*/ |
| 3359 | struct md_rdev *rdev2 = rcu_dereference( |
| 3360 | conf->disks[i].rdev); |
| 3361 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 3362 | s->handle_bad_blocks = 1; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3363 | atomic_inc(&rdev2->nr_pending); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 3364 | } else |
| 3365 | clear_bit(R5_MadeGood, &dev->flags); |
| 3366 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3367 | if (test_bit(R5_MadeGoodRepl, &dev->flags)) { |
| 3368 | struct md_rdev *rdev2 = rcu_dereference( |
| 3369 | conf->disks[i].replacement); |
| 3370 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
| 3371 | s->handle_bad_blocks = 1; |
| 3372 | atomic_inc(&rdev2->nr_pending); |
| 3373 | } else |
| 3374 | clear_bit(R5_MadeGoodRepl, &dev->flags); |
| 3375 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3376 | if (!test_bit(R5_Insync, &dev->flags)) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3377 | /* The ReadError flag will just be confusing now */ |
| 3378 | clear_bit(R5_ReadError, &dev->flags); |
| 3379 | clear_bit(R5_ReWrite, &dev->flags); |
| 3380 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3381 | if (test_bit(R5_ReadError, &dev->flags)) |
| 3382 | clear_bit(R5_Insync, &dev->flags); |
| 3383 | if (!test_bit(R5_Insync, &dev->flags)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3384 | if (s->failed < 2) |
| 3385 | s->failed_num[s->failed] = i; |
| 3386 | s->failed++; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3387 | if (rdev && !test_bit(Faulty, &rdev->flags)) |
| 3388 | do_recovery = 1; |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3389 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3390 | } |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3391 | if (test_bit(STRIPE_SYNCING, &sh->state)) { |
| 3392 | /* If there is a failed device being replaced, |
| 3393 | * we must be recovering. |
| 3394 | * else if we are after recovery_cp, we must be syncing |
majianpeng | c6d2e08 | 2012-04-02 01:16:59 +1000 | [diff] [blame] | 3395 | * else if MD_RECOVERY_REQUESTED is set, we also are syncing. |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3396 | * else we can only be replacing |
| 3397 | * sync and recovery both need to read all devices, and so |
| 3398 | * use the same flag. |
| 3399 | */ |
| 3400 | if (do_recovery || |
majianpeng | c6d2e08 | 2012-04-02 01:16:59 +1000 | [diff] [blame] | 3401 | sh->sector >= conf->mddev->recovery_cp || |
| 3402 | test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery))) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3403 | s->syncing = 1; |
| 3404 | else |
| 3405 | s->replacing = 1; |
| 3406 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3407 | rcu_read_unlock(); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3408 | } |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3409 | |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3410 | static void handle_stripe(struct stripe_head *sh) |
| 3411 | { |
| 3412 | struct stripe_head_state s; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3413 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3414 | int i; |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3415 | int prexor; |
| 3416 | int disks = sh->disks; |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3417 | struct r5dev *pdev, *qdev; |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3418 | |
| 3419 | clear_bit(STRIPE_HANDLE, &sh->state); |
Dan Williams | 257a4b4 | 2011-11-08 16:22:06 +1100 | [diff] [blame] | 3420 | if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3421 | /* already being handled, ensure it gets handled |
| 3422 | * again when current action finishes */ |
| 3423 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3424 | return; |
| 3425 | } |
| 3426 | |
| 3427 | if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) { |
| 3428 | set_bit(STRIPE_SYNCING, &sh->state); |
| 3429 | clear_bit(STRIPE_INSYNC, &sh->state); |
| 3430 | } |
| 3431 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 3432 | |
| 3433 | pr_debug("handling stripe %llu, state=%#lx cnt=%d, " |
| 3434 | "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n", |
| 3435 | (unsigned long long)sh->sector, sh->state, |
| 3436 | atomic_read(&sh->count), sh->pd_idx, sh->qd_idx, |
| 3437 | sh->check_state, sh->reconstruct_state); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3438 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3439 | analyse_stripe(sh, &s); |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3440 | |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3441 | if (s.handle_bad_blocks) { |
| 3442 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3443 | goto finish; |
| 3444 | } |
| 3445 | |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3446 | if (unlikely(s.blocked_rdev)) { |
| 3447 | if (s.syncing || s.expanding || s.expanded || |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3448 | s.replacing || s.to_write || s.written) { |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3449 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3450 | goto finish; |
| 3451 | } |
| 3452 | /* There is nothing for the blocked_rdev to block */ |
| 3453 | rdev_dec_pending(s.blocked_rdev, conf->mddev); |
| 3454 | s.blocked_rdev = NULL; |
| 3455 | } |
| 3456 | |
| 3457 | if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) { |
| 3458 | set_bit(STRIPE_OP_BIOFILL, &s.ops_request); |
| 3459 | set_bit(STRIPE_BIOFILL_RUN, &sh->state); |
| 3460 | } |
| 3461 | |
| 3462 | pr_debug("locked=%d uptodate=%d to_read=%d" |
| 3463 | " to_write=%d failed=%d failed_num=%d,%d\n", |
| 3464 | s.locked, s.uptodate, s.to_read, s.to_write, s.failed, |
| 3465 | s.failed_num[0], s.failed_num[1]); |
| 3466 | /* check if the array has lost more than max_degraded devices and, |
| 3467 | * if so, some requests might need to be failed. |
| 3468 | */ |
NeilBrown | 9a3f530 | 2011-11-08 16:22:01 +1100 | [diff] [blame] | 3469 | if (s.failed > conf->max_degraded) { |
| 3470 | sh->check_state = 0; |
| 3471 | sh->reconstruct_state = 0; |
| 3472 | if (s.to_read+s.to_write+s.written) |
| 3473 | handle_failed_stripe(conf, sh, &s, disks, &s.return_bi); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3474 | if (s.syncing + s.replacing) |
NeilBrown | 9a3f530 | 2011-11-08 16:22:01 +1100 | [diff] [blame] | 3475 | handle_failed_sync(conf, sh, &s); |
| 3476 | } |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3477 | |
| 3478 | /* |
| 3479 | * might be able to return some write requests if the parity blocks |
| 3480 | * are safe, or on a failed drive |
| 3481 | */ |
| 3482 | pdev = &sh->dev[sh->pd_idx]; |
| 3483 | s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx) |
| 3484 | || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx); |
| 3485 | qdev = &sh->dev[sh->qd_idx]; |
| 3486 | s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx) |
| 3487 | || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx) |
| 3488 | || conf->level < 6; |
| 3489 | |
| 3490 | if (s.written && |
| 3491 | (s.p_failed || ((test_bit(R5_Insync, &pdev->flags) |
| 3492 | && !test_bit(R5_LOCKED, &pdev->flags) |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 3493 | && (test_bit(R5_UPTODATE, &pdev->flags) || |
| 3494 | test_bit(R5_Discard, &pdev->flags))))) && |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3495 | (s.q_failed || ((test_bit(R5_Insync, &qdev->flags) |
| 3496 | && !test_bit(R5_LOCKED, &qdev->flags) |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 3497 | && (test_bit(R5_UPTODATE, &qdev->flags) || |
| 3498 | test_bit(R5_Discard, &qdev->flags)))))) |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3499 | handle_stripe_clean_event(conf, sh, disks, &s.return_bi); |
| 3500 | |
| 3501 | /* Now we might consider reading some blocks, either to check/generate |
| 3502 | * parity, or to satisfy requests |
| 3503 | * or to load a block that is being partially written. |
| 3504 | */ |
| 3505 | if (s.to_read || s.non_overwrite |
| 3506 | || (conf->level == 6 && s.to_write && s.failed) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3507 | || (s.syncing && (s.uptodate + s.compute < disks)) |
| 3508 | || s.replacing |
| 3509 | || s.expanding) |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3510 | handle_stripe_fill(sh, &s, disks); |
| 3511 | |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3512 | /* Now we check to see if any write operations have recently |
| 3513 | * completed |
| 3514 | */ |
| 3515 | prexor = 0; |
| 3516 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_result) |
| 3517 | prexor = 1; |
| 3518 | if (sh->reconstruct_state == reconstruct_state_drain_result || |
| 3519 | sh->reconstruct_state == reconstruct_state_prexor_drain_result) { |
| 3520 | sh->reconstruct_state = reconstruct_state_idle; |
| 3521 | |
| 3522 | /* All the 'written' buffers and the parity block are ready to |
| 3523 | * be written back to disk |
| 3524 | */ |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 3525 | BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) && |
| 3526 | !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)); |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3527 | BUG_ON(sh->qd_idx >= 0 && |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 3528 | !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) && |
| 3529 | !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags)); |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3530 | for (i = disks; i--; ) { |
| 3531 | struct r5dev *dev = &sh->dev[i]; |
| 3532 | if (test_bit(R5_LOCKED, &dev->flags) && |
| 3533 | (i == sh->pd_idx || i == sh->qd_idx || |
| 3534 | dev->written)) { |
| 3535 | pr_debug("Writing block %d\n", i); |
| 3536 | set_bit(R5_Wantwrite, &dev->flags); |
| 3537 | if (prexor) |
| 3538 | continue; |
| 3539 | if (!test_bit(R5_Insync, &dev->flags) || |
| 3540 | ((i == sh->pd_idx || i == sh->qd_idx) && |
| 3541 | s.failed == 0)) |
| 3542 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3543 | } |
| 3544 | } |
| 3545 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 3546 | s.dec_preread_active = 1; |
| 3547 | } |
| 3548 | |
| 3549 | /* Now to consider new write requests and what else, if anything |
| 3550 | * should be read. We do not handle new writes when: |
| 3551 | * 1/ A 'write' operation (copy+xor) is already in flight. |
| 3552 | * 2/ A 'check' operation is in flight, as it may clobber the parity |
| 3553 | * block. |
| 3554 | */ |
| 3555 | if (s.to_write && !sh->reconstruct_state && !sh->check_state) |
| 3556 | handle_stripe_dirtying(conf, sh, &s, disks); |
| 3557 | |
| 3558 | /* maybe we need to check and possibly fix the parity for this stripe |
| 3559 | * Any reads will already have been scheduled, so we just see if enough |
| 3560 | * data is available. The parity check is held off while parity |
| 3561 | * dependent operations are in flight. |
| 3562 | */ |
| 3563 | if (sh->check_state || |
| 3564 | (s.syncing && s.locked == 0 && |
| 3565 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && |
| 3566 | !test_bit(STRIPE_INSYNC, &sh->state))) { |
| 3567 | if (conf->level == 6) |
| 3568 | handle_parity_checks6(conf, sh, &s, disks); |
| 3569 | else |
| 3570 | handle_parity_checks5(conf, sh, &s, disks); |
| 3571 | } |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3572 | |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3573 | if (s.replacing && s.locked == 0 |
| 3574 | && !test_bit(STRIPE_INSYNC, &sh->state)) { |
| 3575 | /* Write out to replacement devices where possible */ |
| 3576 | for (i = 0; i < conf->raid_disks; i++) |
| 3577 | if (test_bit(R5_UPTODATE, &sh->dev[i].flags) && |
| 3578 | test_bit(R5_NeedReplace, &sh->dev[i].flags)) { |
| 3579 | set_bit(R5_WantReplace, &sh->dev[i].flags); |
| 3580 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 3581 | s.locked++; |
| 3582 | } |
| 3583 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3584 | } |
| 3585 | if ((s.syncing || s.replacing) && s.locked == 0 && |
| 3586 | test_bit(STRIPE_INSYNC, &sh->state)) { |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3587 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 3588 | clear_bit(STRIPE_SYNCING, &sh->state); |
| 3589 | } |
| 3590 | |
| 3591 | /* If the failed drives are just a ReadError, then we might need |
| 3592 | * to progress the repair/check process |
| 3593 | */ |
| 3594 | if (s.failed <= conf->max_degraded && !conf->mddev->ro) |
| 3595 | for (i = 0; i < s.failed; i++) { |
| 3596 | struct r5dev *dev = &sh->dev[s.failed_num[i]]; |
| 3597 | if (test_bit(R5_ReadError, &dev->flags) |
| 3598 | && !test_bit(R5_LOCKED, &dev->flags) |
| 3599 | && test_bit(R5_UPTODATE, &dev->flags) |
| 3600 | ) { |
| 3601 | if (!test_bit(R5_ReWrite, &dev->flags)) { |
| 3602 | set_bit(R5_Wantwrite, &dev->flags); |
| 3603 | set_bit(R5_ReWrite, &dev->flags); |
| 3604 | set_bit(R5_LOCKED, &dev->flags); |
| 3605 | s.locked++; |
| 3606 | } else { |
| 3607 | /* let's read it back */ |
| 3608 | set_bit(R5_Wantread, &dev->flags); |
| 3609 | set_bit(R5_LOCKED, &dev->flags); |
| 3610 | s.locked++; |
| 3611 | } |
| 3612 | } |
| 3613 | } |
| 3614 | |
| 3615 | |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3616 | /* Finish reconstruct operations initiated by the expansion process */ |
| 3617 | if (sh->reconstruct_state == reconstruct_state_result) { |
| 3618 | struct stripe_head *sh_src |
| 3619 | = get_active_stripe(conf, sh->sector, 1, 1, 1); |
| 3620 | if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) { |
| 3621 | /* sh cannot be written until sh_src has been read. |
| 3622 | * so arrange for sh to be delayed a little |
| 3623 | */ |
| 3624 | set_bit(STRIPE_DELAYED, &sh->state); |
| 3625 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3626 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, |
| 3627 | &sh_src->state)) |
| 3628 | atomic_inc(&conf->preread_active_stripes); |
| 3629 | release_stripe(sh_src); |
| 3630 | goto finish; |
| 3631 | } |
| 3632 | if (sh_src) |
| 3633 | release_stripe(sh_src); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3634 | |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3635 | sh->reconstruct_state = reconstruct_state_idle; |
| 3636 | clear_bit(STRIPE_EXPANDING, &sh->state); |
| 3637 | for (i = conf->raid_disks; i--; ) { |
| 3638 | set_bit(R5_Wantwrite, &sh->dev[i].flags); |
| 3639 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 3640 | s.locked++; |
| 3641 | } |
| 3642 | } |
| 3643 | |
| 3644 | if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) && |
| 3645 | !sh->reconstruct_state) { |
| 3646 | /* Need to write out all blocks after computing parity */ |
| 3647 | sh->disks = conf->raid_disks; |
| 3648 | stripe_set_idx(sh->sector, conf, 0, sh); |
| 3649 | schedule_reconstruction(sh, &s, 1, 1); |
| 3650 | } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) { |
| 3651 | clear_bit(STRIPE_EXPAND_READY, &sh->state); |
| 3652 | atomic_dec(&conf->reshape_stripes); |
| 3653 | wake_up(&conf->wait_for_overlap); |
| 3654 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 3655 | } |
| 3656 | |
| 3657 | if (s.expanding && s.locked == 0 && |
| 3658 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) |
| 3659 | handle_stripe_expansion(conf, sh); |
| 3660 | |
| 3661 | finish: |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3662 | /* wait for this device to become unblocked */ |
NeilBrown | 5f066c6 | 2012-07-03 12:13:29 +1000 | [diff] [blame] | 3663 | if (unlikely(s.blocked_rdev)) { |
| 3664 | if (conf->mddev->external) |
| 3665 | md_wait_for_blocked_rdev(s.blocked_rdev, |
| 3666 | conf->mddev); |
| 3667 | else |
| 3668 | /* Internal metadata will immediately |
| 3669 | * be written by raid5d, so we don't |
| 3670 | * need to wait here. |
| 3671 | */ |
| 3672 | rdev_dec_pending(s.blocked_rdev, |
| 3673 | conf->mddev); |
| 3674 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3675 | |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3676 | if (s.handle_bad_blocks) |
| 3677 | for (i = disks; i--; ) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3678 | struct md_rdev *rdev; |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3679 | struct r5dev *dev = &sh->dev[i]; |
| 3680 | if (test_and_clear_bit(R5_WriteError, &dev->flags)) { |
| 3681 | /* We own a safe reference to the rdev */ |
| 3682 | rdev = conf->disks[i].rdev; |
| 3683 | if (!rdev_set_badblocks(rdev, sh->sector, |
| 3684 | STRIPE_SECTORS, 0)) |
| 3685 | md_error(conf->mddev, rdev); |
| 3686 | rdev_dec_pending(rdev, conf->mddev); |
| 3687 | } |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 3688 | if (test_and_clear_bit(R5_MadeGood, &dev->flags)) { |
| 3689 | rdev = conf->disks[i].rdev; |
| 3690 | rdev_clear_badblocks(rdev, sh->sector, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3691 | STRIPE_SECTORS, 0); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 3692 | rdev_dec_pending(rdev, conf->mddev); |
| 3693 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3694 | if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) { |
| 3695 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3696 | if (!rdev) |
| 3697 | /* rdev have been moved down */ |
| 3698 | rdev = conf->disks[i].rdev; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3699 | rdev_clear_badblocks(rdev, sh->sector, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3700 | STRIPE_SECTORS, 0); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3701 | rdev_dec_pending(rdev, conf->mddev); |
| 3702 | } |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3703 | } |
| 3704 | |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3705 | if (s.ops_request) |
| 3706 | raid_run_ops(sh, s.ops_request); |
| 3707 | |
Dan Williams | f0e43bc | 2008-06-28 08:31:55 +1000 | [diff] [blame] | 3708 | ops_run_io(sh, &s); |
| 3709 | |
NeilBrown | c5709ef | 2011-07-26 11:35:20 +1000 | [diff] [blame] | 3710 | if (s.dec_preread_active) { |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 3711 | /* 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] | 3712 | * is waiting on a flush, it won't continue until the writes |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 3713 | * have actually been submitted. |
| 3714 | */ |
| 3715 | atomic_dec(&conf->preread_active_stripes); |
| 3716 | if (atomic_read(&conf->preread_active_stripes) < |
| 3717 | IO_THRESHOLD) |
| 3718 | md_wakeup_thread(conf->mddev->thread); |
| 3719 | } |
| 3720 | |
NeilBrown | c5709ef | 2011-07-26 11:35:20 +1000 | [diff] [blame] | 3721 | return_io(s.return_bi); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3722 | |
Dan Williams | 257a4b4 | 2011-11-08 16:22:06 +1100 | [diff] [blame] | 3723 | clear_bit_unlock(STRIPE_ACTIVE, &sh->state); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3724 | } |
| 3725 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3726 | static void raid5_activate_delayed(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3727 | { |
| 3728 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) { |
| 3729 | while (!list_empty(&conf->delayed_list)) { |
| 3730 | struct list_head *l = conf->delayed_list.next; |
| 3731 | struct stripe_head *sh; |
| 3732 | sh = list_entry(l, struct stripe_head, lru); |
| 3733 | list_del_init(l); |
| 3734 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 3735 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 3736 | atomic_inc(&conf->preread_active_stripes); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3737 | list_add_tail(&sh->lru, &conf->hold_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3738 | } |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 3739 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3740 | } |
| 3741 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3742 | static void activate_bit_delay(struct r5conf *conf) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3743 | { |
| 3744 | /* device_lock is held */ |
| 3745 | struct list_head head; |
| 3746 | list_add(&head, &conf->bitmap_list); |
| 3747 | list_del_init(&conf->bitmap_list); |
| 3748 | while (!list_empty(&head)) { |
| 3749 | struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru); |
| 3750 | list_del_init(&sh->lru); |
| 3751 | atomic_inc(&sh->count); |
| 3752 | __release_stripe(conf, sh); |
| 3753 | } |
| 3754 | } |
| 3755 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3756 | int md_raid5_congested(struct mddev *mddev, int bits) |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3757 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3758 | struct r5conf *conf = mddev->private; |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3759 | |
| 3760 | /* No difference between reads and writes. Just check |
| 3761 | * how busy the stripe_cache is |
| 3762 | */ |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 3763 | |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3764 | if (conf->inactive_blocked) |
| 3765 | return 1; |
| 3766 | if (conf->quiesce) |
| 3767 | return 1; |
| 3768 | if (list_empty_careful(&conf->inactive_list)) |
| 3769 | return 1; |
| 3770 | |
| 3771 | return 0; |
| 3772 | } |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 3773 | EXPORT_SYMBOL_GPL(md_raid5_congested); |
| 3774 | |
| 3775 | static int raid5_congested(void *data, int bits) |
| 3776 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3777 | struct mddev *mddev = data; |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 3778 | |
| 3779 | return mddev_congested(mddev, bits) || |
| 3780 | md_raid5_congested(mddev, bits); |
| 3781 | } |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3782 | |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3783 | /* We want read requests to align with chunks where possible, |
| 3784 | * but write requests don't need to. |
| 3785 | */ |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3786 | static int raid5_mergeable_bvec(struct request_queue *q, |
| 3787 | struct bvec_merge_data *bvm, |
| 3788 | struct bio_vec *biovec) |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3789 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3790 | struct mddev *mddev = q->queuedata; |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3791 | 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] | 3792 | int max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3793 | unsigned int chunk_sectors = mddev->chunk_sectors; |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3794 | unsigned int bio_sectors = bvm->bi_size >> 9; |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3795 | |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3796 | if ((bvm->bi_rw & 1) == WRITE) |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3797 | return biovec->bv_len; /* always allow writes to be mergeable */ |
| 3798 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3799 | if (mddev->new_chunk_sectors < mddev->chunk_sectors) |
| 3800 | chunk_sectors = mddev->new_chunk_sectors; |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3801 | max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9; |
| 3802 | if (max < 0) max = 0; |
| 3803 | if (max <= biovec->bv_len && bio_sectors == 0) |
| 3804 | return biovec->bv_len; |
| 3805 | else |
| 3806 | return max; |
| 3807 | } |
| 3808 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3809 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3810 | 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] | 3811 | { |
| 3812 | sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3813 | unsigned int chunk_sectors = mddev->chunk_sectors; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3814 | unsigned int bio_sectors = bio->bi_size >> 9; |
| 3815 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3816 | if (mddev->new_chunk_sectors < mddev->chunk_sectors) |
| 3817 | chunk_sectors = mddev->new_chunk_sectors; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3818 | return chunk_sectors >= |
| 3819 | ((sector & (chunk_sectors - 1)) + bio_sectors); |
| 3820 | } |
| 3821 | |
| 3822 | /* |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3823 | * add bio to the retry LIFO ( in O(1) ... we are in interrupt ) |
| 3824 | * later sampled by raid5d. |
| 3825 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3826 | 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] | 3827 | { |
| 3828 | unsigned long flags; |
| 3829 | |
| 3830 | spin_lock_irqsave(&conf->device_lock, flags); |
| 3831 | |
| 3832 | bi->bi_next = conf->retry_read_aligned_list; |
| 3833 | conf->retry_read_aligned_list = bi; |
| 3834 | |
| 3835 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 3836 | md_wakeup_thread(conf->mddev->thread); |
| 3837 | } |
| 3838 | |
| 3839 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3840 | static struct bio *remove_bio_from_retry(struct r5conf *conf) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3841 | { |
| 3842 | struct bio *bi; |
| 3843 | |
| 3844 | bi = conf->retry_read_aligned; |
| 3845 | if (bi) { |
| 3846 | conf->retry_read_aligned = NULL; |
| 3847 | return bi; |
| 3848 | } |
| 3849 | bi = conf->retry_read_aligned_list; |
| 3850 | if(bi) { |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3851 | conf->retry_read_aligned_list = bi->bi_next; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3852 | bi->bi_next = NULL; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 3853 | /* |
| 3854 | * this sets the active strip count to 1 and the processed |
| 3855 | * strip count to zero (upper 8 bits) |
| 3856 | */ |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3857 | 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] | 3858 | } |
| 3859 | |
| 3860 | return bi; |
| 3861 | } |
| 3862 | |
| 3863 | |
| 3864 | /* |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3865 | * The "raid5_align_endio" should check if the read succeeded and if it |
| 3866 | * did, call bio_endio on the original bio (having bio_put the new bio |
| 3867 | * first). |
| 3868 | * If the read failed.. |
| 3869 | */ |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3870 | static void raid5_align_endio(struct bio *bi, int error) |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3871 | { |
| 3872 | struct bio* raid_bi = bi->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3873 | struct mddev *mddev; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3874 | struct r5conf *conf; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3875 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3876 | struct md_rdev *rdev; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3877 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3878 | bio_put(bi); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3879 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3880 | rdev = (void*)raid_bi->bi_next; |
| 3881 | raid_bi->bi_next = NULL; |
NeilBrown | 2b7f222 | 2010-03-25 16:06:03 +1100 | [diff] [blame] | 3882 | mddev = rdev->mddev; |
| 3883 | conf = mddev->private; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3884 | |
| 3885 | rdev_dec_pending(rdev, conf->mddev); |
| 3886 | |
| 3887 | if (!error && uptodate) { |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3888 | bio_endio(raid_bi, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3889 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
| 3890 | wake_up(&conf->wait_for_stripe); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3891 | return; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3892 | } |
| 3893 | |
| 3894 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3895 | 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] | 3896 | |
| 3897 | add_bio_to_retry(raid_bi, conf); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3898 | } |
| 3899 | |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3900 | static int bio_fits_rdev(struct bio *bi) |
| 3901 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3902 | struct request_queue *q = bdev_get_queue(bi->bi_bdev); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3903 | |
Martin K. Petersen | ae03bf6 | 2009-05-22 17:17:50 -0400 | [diff] [blame] | 3904 | if ((bi->bi_size>>9) > queue_max_sectors(q)) |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3905 | return 0; |
| 3906 | blk_recount_segments(q, bi); |
Martin K. Petersen | 8a78362 | 2010-02-26 00:20:39 -0500 | [diff] [blame] | 3907 | if (bi->bi_phys_segments > queue_max_segments(q)) |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3908 | return 0; |
| 3909 | |
| 3910 | if (q->merge_bvec_fn) |
| 3911 | /* it's too hard to apply the merge_bvec_fn at this stage, |
| 3912 | * just just give up |
| 3913 | */ |
| 3914 | return 0; |
| 3915 | |
| 3916 | return 1; |
| 3917 | } |
| 3918 | |
| 3919 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3920 | 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] | 3921 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3922 | struct r5conf *conf = mddev->private; |
NeilBrown | 8553fe7ec | 2009-12-14 12:49:47 +1100 | [diff] [blame] | 3923 | int dd_idx; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3924 | struct bio* align_bi; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3925 | struct md_rdev *rdev; |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3926 | sector_t end_sector; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3927 | |
| 3928 | if (!in_chunk_boundary(mddev, raid_bio)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3929 | pr_debug("chunk_aligned_read : non aligned\n"); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3930 | return 0; |
| 3931 | } |
| 3932 | /* |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 3933 | * 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] | 3934 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 3935 | align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3936 | if (!align_bi) |
| 3937 | return 0; |
| 3938 | /* |
| 3939 | * set bi_end_io to a new function, and set bi_private to the |
| 3940 | * original bio. |
| 3941 | */ |
| 3942 | align_bi->bi_end_io = raid5_align_endio; |
| 3943 | align_bi->bi_private = raid_bio; |
| 3944 | /* |
| 3945 | * compute position |
| 3946 | */ |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3947 | align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector, |
| 3948 | 0, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3949 | &dd_idx, NULL); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3950 | |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3951 | 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] | 3952 | rcu_read_lock(); |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3953 | rdev = rcu_dereference(conf->disks[dd_idx].replacement); |
| 3954 | if (!rdev || test_bit(Faulty, &rdev->flags) || |
| 3955 | rdev->recovery_offset < end_sector) { |
| 3956 | rdev = rcu_dereference(conf->disks[dd_idx].rdev); |
| 3957 | if (rdev && |
| 3958 | (test_bit(Faulty, &rdev->flags) || |
| 3959 | !(test_bit(In_sync, &rdev->flags) || |
| 3960 | rdev->recovery_offset >= end_sector))) |
| 3961 | rdev = NULL; |
| 3962 | } |
| 3963 | if (rdev) { |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3964 | sector_t first_bad; |
| 3965 | int bad_sectors; |
| 3966 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3967 | atomic_inc(&rdev->nr_pending); |
| 3968 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3969 | raid_bio->bi_next = (void*)rdev; |
| 3970 | align_bi->bi_bdev = rdev->bdev; |
| 3971 | align_bi->bi_flags &= ~(1 << BIO_SEG_VALID); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3972 | |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3973 | if (!bio_fits_rdev(align_bi) || |
| 3974 | is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9, |
| 3975 | &first_bad, &bad_sectors)) { |
| 3976 | /* too big in some way, or has a known bad block */ |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3977 | bio_put(align_bi); |
| 3978 | rdev_dec_pending(rdev, mddev); |
| 3979 | return 0; |
| 3980 | } |
| 3981 | |
majianpeng | 6c0544e | 2012-06-12 08:31:10 +0800 | [diff] [blame] | 3982 | /* No reshape active, so we can trust rdev->data_offset */ |
| 3983 | align_bi->bi_sector += rdev->data_offset; |
| 3984 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3985 | spin_lock_irq(&conf->device_lock); |
| 3986 | wait_event_lock_irq(conf->wait_for_stripe, |
| 3987 | conf->quiesce == 0, |
| 3988 | conf->device_lock, /* nothing */); |
| 3989 | atomic_inc(&conf->active_aligned_reads); |
| 3990 | spin_unlock_irq(&conf->device_lock); |
| 3991 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3992 | generic_make_request(align_bi); |
| 3993 | return 1; |
| 3994 | } else { |
| 3995 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3996 | bio_put(align_bi); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3997 | return 0; |
| 3998 | } |
| 3999 | } |
| 4000 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4001 | /* __get_priority_stripe - get the next stripe to process |
| 4002 | * |
| 4003 | * Full stripe writes are allowed to pass preread active stripes up until |
| 4004 | * the bypass_threshold is exceeded. In general the bypass_count |
| 4005 | * increments when the handle_list is handled before the hold_list; however, it |
| 4006 | * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a |
| 4007 | * stripe with in flight i/o. The bypass_count will be reset when the |
| 4008 | * head of the hold_list has changed, i.e. the head was promoted to the |
| 4009 | * handle_list. |
| 4010 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4011 | static struct stripe_head *__get_priority_stripe(struct r5conf *conf) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4012 | { |
| 4013 | struct stripe_head *sh; |
| 4014 | |
| 4015 | pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n", |
| 4016 | __func__, |
| 4017 | list_empty(&conf->handle_list) ? "empty" : "busy", |
| 4018 | list_empty(&conf->hold_list) ? "empty" : "busy", |
| 4019 | atomic_read(&conf->pending_full_writes), conf->bypass_count); |
| 4020 | |
| 4021 | if (!list_empty(&conf->handle_list)) { |
| 4022 | sh = list_entry(conf->handle_list.next, typeof(*sh), lru); |
| 4023 | |
| 4024 | if (list_empty(&conf->hold_list)) |
| 4025 | conf->bypass_count = 0; |
| 4026 | else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) { |
| 4027 | if (conf->hold_list.next == conf->last_hold) |
| 4028 | conf->bypass_count++; |
| 4029 | else { |
| 4030 | conf->last_hold = conf->hold_list.next; |
| 4031 | conf->bypass_count -= conf->bypass_threshold; |
| 4032 | if (conf->bypass_count < 0) |
| 4033 | conf->bypass_count = 0; |
| 4034 | } |
| 4035 | } |
| 4036 | } else if (!list_empty(&conf->hold_list) && |
| 4037 | ((conf->bypass_threshold && |
| 4038 | conf->bypass_count > conf->bypass_threshold) || |
| 4039 | atomic_read(&conf->pending_full_writes) == 0)) { |
| 4040 | sh = list_entry(conf->hold_list.next, |
| 4041 | typeof(*sh), lru); |
| 4042 | conf->bypass_count -= conf->bypass_threshold; |
| 4043 | if (conf->bypass_count < 0) |
| 4044 | conf->bypass_count = 0; |
| 4045 | } else |
| 4046 | return NULL; |
| 4047 | |
| 4048 | list_del_init(&sh->lru); |
| 4049 | atomic_inc(&sh->count); |
| 4050 | BUG_ON(atomic_read(&sh->count) != 1); |
| 4051 | return sh; |
| 4052 | } |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4053 | |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 4054 | struct raid5_plug_cb { |
| 4055 | struct blk_plug_cb cb; |
| 4056 | struct list_head list; |
| 4057 | }; |
| 4058 | |
| 4059 | static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule) |
| 4060 | { |
| 4061 | struct raid5_plug_cb *cb = container_of( |
| 4062 | blk_cb, struct raid5_plug_cb, cb); |
| 4063 | struct stripe_head *sh; |
| 4064 | struct mddev *mddev = cb->cb.data; |
| 4065 | struct r5conf *conf = mddev->private; |
| 4066 | |
| 4067 | if (cb->list.next && !list_empty(&cb->list)) { |
| 4068 | spin_lock_irq(&conf->device_lock); |
| 4069 | while (!list_empty(&cb->list)) { |
| 4070 | sh = list_first_entry(&cb->list, struct stripe_head, lru); |
| 4071 | list_del_init(&sh->lru); |
| 4072 | /* |
| 4073 | * avoid race release_stripe_plug() sees |
| 4074 | * STRIPE_ON_UNPLUG_LIST clear but the stripe |
| 4075 | * is still in our list |
| 4076 | */ |
| 4077 | smp_mb__before_clear_bit(); |
| 4078 | clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state); |
| 4079 | __release_stripe(conf, sh); |
| 4080 | } |
| 4081 | spin_unlock_irq(&conf->device_lock); |
| 4082 | } |
| 4083 | kfree(cb); |
| 4084 | } |
| 4085 | |
| 4086 | static void release_stripe_plug(struct mddev *mddev, |
| 4087 | struct stripe_head *sh) |
| 4088 | { |
| 4089 | struct blk_plug_cb *blk_cb = blk_check_plugged( |
| 4090 | raid5_unplug, mddev, |
| 4091 | sizeof(struct raid5_plug_cb)); |
| 4092 | struct raid5_plug_cb *cb; |
| 4093 | |
| 4094 | if (!blk_cb) { |
| 4095 | release_stripe(sh); |
| 4096 | return; |
| 4097 | } |
| 4098 | |
| 4099 | cb = container_of(blk_cb, struct raid5_plug_cb, cb); |
| 4100 | |
| 4101 | if (cb->list.next == NULL) |
| 4102 | INIT_LIST_HEAD(&cb->list); |
| 4103 | |
| 4104 | if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state)) |
| 4105 | list_add_tail(&sh->lru, &cb->list); |
| 4106 | else |
| 4107 | release_stripe(sh); |
| 4108 | } |
| 4109 | |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 4110 | static void make_discard_request(struct mddev *mddev, struct bio *bi) |
| 4111 | { |
| 4112 | struct r5conf *conf = mddev->private; |
| 4113 | sector_t logical_sector, last_sector; |
| 4114 | struct stripe_head *sh; |
| 4115 | int remaining; |
| 4116 | int stripe_sectors; |
| 4117 | |
| 4118 | if (mddev->reshape_position != MaxSector) |
| 4119 | /* Skip discard while reshape is happening */ |
| 4120 | return; |
| 4121 | |
| 4122 | logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
| 4123 | last_sector = bi->bi_sector + (bi->bi_size>>9); |
| 4124 | |
| 4125 | bi->bi_next = NULL; |
| 4126 | bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ |
| 4127 | |
| 4128 | stripe_sectors = conf->chunk_sectors * |
| 4129 | (conf->raid_disks - conf->max_degraded); |
| 4130 | logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector, |
| 4131 | stripe_sectors); |
| 4132 | sector_div(last_sector, stripe_sectors); |
| 4133 | |
| 4134 | logical_sector *= conf->chunk_sectors; |
| 4135 | last_sector *= conf->chunk_sectors; |
| 4136 | |
| 4137 | for (; logical_sector < last_sector; |
| 4138 | logical_sector += STRIPE_SECTORS) { |
| 4139 | DEFINE_WAIT(w); |
| 4140 | int d; |
| 4141 | again: |
| 4142 | sh = get_active_stripe(conf, logical_sector, 0, 0, 0); |
| 4143 | prepare_to_wait(&conf->wait_for_overlap, &w, |
| 4144 | TASK_UNINTERRUPTIBLE); |
| 4145 | spin_lock_irq(&sh->stripe_lock); |
| 4146 | for (d = 0; d < conf->raid_disks; d++) { |
| 4147 | if (d == sh->pd_idx || d == sh->qd_idx) |
| 4148 | continue; |
| 4149 | if (sh->dev[d].towrite || sh->dev[d].toread) { |
| 4150 | set_bit(R5_Overlap, &sh->dev[d].flags); |
| 4151 | spin_unlock_irq(&sh->stripe_lock); |
| 4152 | release_stripe(sh); |
| 4153 | schedule(); |
| 4154 | goto again; |
| 4155 | } |
| 4156 | } |
| 4157 | finish_wait(&conf->wait_for_overlap, &w); |
| 4158 | for (d = 0; d < conf->raid_disks; d++) { |
| 4159 | if (d == sh->pd_idx || d == sh->qd_idx) |
| 4160 | continue; |
| 4161 | sh->dev[d].towrite = bi; |
| 4162 | set_bit(R5_OVERWRITE, &sh->dev[d].flags); |
| 4163 | raid5_inc_bi_active_stripes(bi); |
| 4164 | } |
| 4165 | spin_unlock_irq(&sh->stripe_lock); |
| 4166 | if (conf->mddev->bitmap) { |
| 4167 | for (d = 0; |
| 4168 | d < conf->raid_disks - conf->max_degraded; |
| 4169 | d++) |
| 4170 | bitmap_startwrite(mddev->bitmap, |
| 4171 | sh->sector, |
| 4172 | STRIPE_SECTORS, |
| 4173 | 0); |
| 4174 | sh->bm_seq = conf->seq_flush + 1; |
| 4175 | set_bit(STRIPE_BIT_DELAY, &sh->state); |
| 4176 | } |
| 4177 | |
| 4178 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4179 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 4180 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 4181 | atomic_inc(&conf->preread_active_stripes); |
| 4182 | release_stripe_plug(mddev, sh); |
| 4183 | } |
| 4184 | |
| 4185 | remaining = raid5_dec_bi_active_stripes(bi); |
| 4186 | if (remaining == 0) { |
| 4187 | md_write_end(mddev); |
| 4188 | bio_endio(bi, 0); |
| 4189 | } |
| 4190 | } |
| 4191 | |
Linus Torvalds | b4fdcb0 | 2011-11-04 17:06:58 -0700 | [diff] [blame] | 4192 | static void make_request(struct mddev *mddev, struct bio * bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4193 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4194 | struct r5conf *conf = mddev->private; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4195 | int dd_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4196 | sector_t new_sector; |
| 4197 | sector_t logical_sector, last_sector; |
| 4198 | struct stripe_head *sh; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 4199 | const int rw = bio_data_dir(bi); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 4200 | int remaining; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4201 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 4202 | if (unlikely(bi->bi_rw & REQ_FLUSH)) { |
| 4203 | md_flush_request(mddev, bi); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 4204 | return; |
NeilBrown | e5dcdd8 | 2005-09-09 16:23:41 -0700 | [diff] [blame] | 4205 | } |
| 4206 | |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 4207 | md_write_start(mddev, bi); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 4208 | |
NeilBrown | 802ba06 | 2006-12-13 00:34:13 -0800 | [diff] [blame] | 4209 | if (rw == READ && |
Raz Ben-Jehuda(caro) | 5248861 | 2006-12-10 02:20:48 -0800 | [diff] [blame] | 4210 | mddev->reshape_position == MaxSector && |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 4211 | chunk_aligned_read(mddev,bi)) |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 4212 | return; |
Raz Ben-Jehuda(caro) | 5248861 | 2006-12-10 02:20:48 -0800 | [diff] [blame] | 4213 | |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 4214 | if (unlikely(bi->bi_rw & REQ_DISCARD)) { |
| 4215 | make_discard_request(mddev, bi); |
| 4216 | return; |
| 4217 | } |
| 4218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4219 | logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
| 4220 | last_sector = bi->bi_sector + (bi->bi_size>>9); |
| 4221 | bi->bi_next = NULL; |
| 4222 | bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 4223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4224 | for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) { |
| 4225 | DEFINE_WAIT(w); |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4226 | int previous; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 4227 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4228 | retry: |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4229 | previous = 0; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 4230 | prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 4231 | if (unlikely(conf->reshape_progress != MaxSector)) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4232 | /* spinlock is needed as reshape_progress may be |
NeilBrown | df8e7f76 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 4233 | * 64bit on a 32bit platform, and so it might be |
| 4234 | * possible to see a half-updated value |
Jesper Juhl | aeb878b | 2011-04-10 18:06:17 +0200 | [diff] [blame] | 4235 | * Of course reshape_progress could change after |
NeilBrown | df8e7f76 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 4236 | * the lock is dropped, so once we get a reference |
| 4237 | * to the stripe that we think it is, we will have |
| 4238 | * to check again. |
| 4239 | */ |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4240 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4241 | if (mddev->reshape_backwards |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4242 | ? logical_sector < conf->reshape_progress |
| 4243 | : logical_sector >= conf->reshape_progress) { |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4244 | previous = 1; |
| 4245 | } else { |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4246 | if (mddev->reshape_backwards |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4247 | ? logical_sector < conf->reshape_safe |
| 4248 | : logical_sector >= conf->reshape_safe) { |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 4249 | spin_unlock_irq(&conf->device_lock); |
| 4250 | schedule(); |
| 4251 | goto retry; |
| 4252 | } |
| 4253 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4254 | spin_unlock_irq(&conf->device_lock); |
| 4255 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4256 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4257 | new_sector = raid5_compute_sector(conf, logical_sector, |
| 4258 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4259 | &dd_idx, NULL); |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4260 | pr_debug("raid456: make_request, sector %llu logical %llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4261 | (unsigned long long)new_sector, |
| 4262 | (unsigned long long)logical_sector); |
| 4263 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4264 | sh = get_active_stripe(conf, new_sector, previous, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4265 | (bi->bi_rw&RWA_MASK), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4266 | if (sh) { |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 4267 | if (unlikely(previous)) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4268 | /* expansion might have moved on while waiting for a |
NeilBrown | df8e7f76 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 4269 | * stripe, so we must do the range check again. |
| 4270 | * Expansion could still move past after this |
| 4271 | * test, but as we are holding a reference to |
| 4272 | * 'sh', we know that if that happens, |
| 4273 | * STRIPE_EXPANDING will get set and the expansion |
| 4274 | * won't proceed until we finish with the stripe. |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4275 | */ |
| 4276 | int must_retry = 0; |
| 4277 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4278 | if (mddev->reshape_backwards |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 4279 | ? logical_sector >= conf->reshape_progress |
| 4280 | : logical_sector < conf->reshape_progress) |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4281 | /* mismatch, need to try again */ |
| 4282 | must_retry = 1; |
| 4283 | spin_unlock_irq(&conf->device_lock); |
| 4284 | if (must_retry) { |
| 4285 | release_stripe(sh); |
Dan Williams | 7a3ab90 | 2009-06-16 16:00:33 -0700 | [diff] [blame] | 4286 | schedule(); |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4287 | goto retry; |
| 4288 | } |
| 4289 | } |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 4290 | |
Namhyung Kim | ffd96e3 | 2011-07-18 17:38:51 +1000 | [diff] [blame] | 4291 | if (rw == WRITE && |
NeilBrown | a5c308d | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 4292 | logical_sector >= mddev->suspend_lo && |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4293 | logical_sector < mddev->suspend_hi) { |
| 4294 | release_stripe(sh); |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 4295 | /* As the suspend_* range is controlled by |
| 4296 | * userspace, we want an interruptible |
| 4297 | * wait. |
| 4298 | */ |
| 4299 | flush_signals(current); |
| 4300 | prepare_to_wait(&conf->wait_for_overlap, |
| 4301 | &w, TASK_INTERRUPTIBLE); |
| 4302 | if (logical_sector >= mddev->suspend_lo && |
| 4303 | logical_sector < mddev->suspend_hi) |
| 4304 | schedule(); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4305 | goto retry; |
| 4306 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4307 | |
| 4308 | if (test_bit(STRIPE_EXPANDING, &sh->state) || |
Namhyung Kim | ffd96e3 | 2011-07-18 17:38:51 +1000 | [diff] [blame] | 4309 | !add_stripe_bio(sh, bi, dd_idx, rw)) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4310 | /* Stripe is busy expanding or |
| 4311 | * add failed due to overlap. Flush everything |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4312 | * and wait a while |
| 4313 | */ |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 4314 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4315 | release_stripe(sh); |
| 4316 | schedule(); |
| 4317 | goto retry; |
| 4318 | } |
| 4319 | finish_wait(&conf->wait_for_overlap, &w); |
NeilBrown | 6ed3003 | 2008-02-06 01:40:00 -0800 | [diff] [blame] | 4320 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4321 | clear_bit(STRIPE_DELAYED, &sh->state); |
majianpeng | 895e3c5 | 2012-07-31 10:05:44 +1000 | [diff] [blame] | 4322 | if ((bi->bi_rw & REQ_NOIDLE) && |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 4323 | !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 4324 | atomic_inc(&conf->preread_active_stripes); |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 4325 | release_stripe_plug(mddev, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4326 | } else { |
| 4327 | /* cannot get stripe for read-ahead, just give-up */ |
| 4328 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
| 4329 | finish_wait(&conf->wait_for_overlap, &w); |
| 4330 | break; |
| 4331 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4332 | } |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 4333 | |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 4334 | remaining = raid5_dec_bi_active_stripes(bi); |
NeilBrown | f634475 | 2006-03-27 01:18:17 -0800 | [diff] [blame] | 4335 | if (remaining == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4336 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4337 | if ( rw == WRITE ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4338 | md_write_end(mddev); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 4339 | |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 4340 | bio_endio(bi, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4341 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4342 | } |
| 4343 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4344 | 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] | 4345 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4346 | 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] | 4347 | { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4348 | /* reshaping is quite different to recovery/resync so it is |
| 4349 | * handled quite separately ... here. |
| 4350 | * |
| 4351 | * On each call to sync_request, we gather one chunk worth of |
| 4352 | * destination stripes and flag them as expanding. |
| 4353 | * Then we find all the source stripes and request reads. |
| 4354 | * As the reads complete, handle_stripe will copy the data |
| 4355 | * into the destination stripe and release that stripe. |
| 4356 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4357 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4358 | struct stripe_head *sh; |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4359 | sector_t first_sector, last_sector; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4360 | int raid_disks = conf->previous_raid_disks; |
| 4361 | int data_disks = raid_disks - conf->max_degraded; |
| 4362 | int new_data_disks = conf->raid_disks - conf->max_degraded; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4363 | int i; |
| 4364 | int dd_idx; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4365 | sector_t writepos, readpos, safepos; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4366 | sector_t stripe_addr; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4367 | int reshape_sectors; |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4368 | struct list_head stripes; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4369 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4370 | if (sector_nr == 0) { |
| 4371 | /* If restarting in the middle, skip the initial sectors */ |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4372 | if (mddev->reshape_backwards && |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4373 | conf->reshape_progress < raid5_size(mddev, 0, 0)) { |
| 4374 | sector_nr = raid5_size(mddev, 0, 0) |
| 4375 | - conf->reshape_progress; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4376 | } else if (!mddev->reshape_backwards && |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4377 | conf->reshape_progress > 0) |
| 4378 | sector_nr = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4379 | sector_div(sector_nr, new_data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4380 | if (sector_nr) { |
NeilBrown | 8dee721 | 2009-11-06 14:59:29 +1100 | [diff] [blame] | 4381 | mddev->curr_resync_completed = sector_nr; |
| 4382 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4383 | *skipped = 1; |
| 4384 | return sector_nr; |
| 4385 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4386 | } |
| 4387 | |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4388 | /* We need to process a full chunk at a time. |
| 4389 | * If old and new chunk sizes differ, we need to process the |
| 4390 | * largest of these |
| 4391 | */ |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4392 | if (mddev->new_chunk_sectors > mddev->chunk_sectors) |
| 4393 | reshape_sectors = mddev->new_chunk_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4394 | else |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4395 | reshape_sectors = mddev->chunk_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4396 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 4397 | /* We update the metadata at least every 10 seconds, or when |
| 4398 | * the data about to be copied would over-write the source of |
| 4399 | * the data at the front of the range. i.e. one new_stripe |
| 4400 | * along from reshape_progress new_maps to after where |
| 4401 | * reshape_safe old_maps to |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4402 | */ |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4403 | writepos = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4404 | sector_div(writepos, new_data_disks); |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4405 | readpos = conf->reshape_progress; |
| 4406 | sector_div(readpos, data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4407 | safepos = conf->reshape_safe; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4408 | sector_div(safepos, data_disks); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4409 | if (mddev->reshape_backwards) { |
NeilBrown | ed37d83 | 2009-05-27 21:39:05 +1000 | [diff] [blame] | 4410 | writepos -= min_t(sector_t, reshape_sectors, writepos); |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4411 | readpos += reshape_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4412 | safepos += reshape_sectors; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4413 | } else { |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4414 | writepos += reshape_sectors; |
NeilBrown | ed37d83 | 2009-05-27 21:39:05 +1000 | [diff] [blame] | 4415 | readpos -= min_t(sector_t, reshape_sectors, readpos); |
| 4416 | safepos -= min_t(sector_t, reshape_sectors, safepos); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4417 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4418 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 4419 | /* Having calculated the 'writepos' possibly use it |
| 4420 | * to set 'stripe_addr' which is where we will write to. |
| 4421 | */ |
| 4422 | if (mddev->reshape_backwards) { |
| 4423 | BUG_ON(conf->reshape_progress == 0); |
| 4424 | stripe_addr = writepos; |
| 4425 | BUG_ON((mddev->dev_sectors & |
| 4426 | ~((sector_t)reshape_sectors - 1)) |
| 4427 | - reshape_sectors - stripe_addr |
| 4428 | != sector_nr); |
| 4429 | } else { |
| 4430 | BUG_ON(writepos != sector_nr + reshape_sectors); |
| 4431 | stripe_addr = sector_nr; |
| 4432 | } |
| 4433 | |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4434 | /* 'writepos' is the most advanced device address we might write. |
| 4435 | * 'readpos' is the least advanced device address we might read. |
| 4436 | * 'safepos' is the least address recorded in the metadata as having |
| 4437 | * been reshaped. |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 4438 | * If there is a min_offset_diff, these are adjusted either by |
| 4439 | * increasing the safepos/readpos if diff is negative, or |
| 4440 | * increasing writepos if diff is positive. |
| 4441 | * If 'readpos' is then behind 'writepos', there is no way that we can |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4442 | * ensure safety in the face of a crash - that must be done by userspace |
| 4443 | * making a backup of the data. So in that case there is no particular |
| 4444 | * rush to update metadata. |
| 4445 | * Otherwise if 'safepos' is behind 'writepos', then we really need to |
| 4446 | * update the metadata to advance 'safepos' to match 'readpos' so that |
| 4447 | * we can be safe in the event of a crash. |
| 4448 | * So we insist on updating metadata if safepos is behind writepos and |
| 4449 | * readpos is beyond writepos. |
| 4450 | * In any case, update the metadata every 10 seconds. |
| 4451 | * Maybe that number should be configurable, but I'm not sure it is |
| 4452 | * worth it.... maybe it could be a multiple of safemode_delay??? |
| 4453 | */ |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 4454 | if (conf->min_offset_diff < 0) { |
| 4455 | safepos += -conf->min_offset_diff; |
| 4456 | readpos += -conf->min_offset_diff; |
| 4457 | } else |
| 4458 | writepos += conf->min_offset_diff; |
| 4459 | |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4460 | if ((mddev->reshape_backwards |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4461 | ? (safepos > writepos && readpos < writepos) |
| 4462 | : (safepos < writepos && readpos > writepos)) || |
| 4463 | time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4464 | /* Cannot proceed until we've updated the superblock... */ |
| 4465 | wait_event(conf->wait_for_overlap, |
| 4466 | atomic_read(&conf->reshape_stripes)==0); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4467 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4468 | mddev->curr_resync_completed = sector_nr; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4469 | conf->reshape_checkpoint = jiffies; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4470 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4471 | md_wakeup_thread(mddev->thread); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4472 | wait_event(mddev->sb_wait, mddev->flags == 0 || |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4473 | kthread_should_stop()); |
| 4474 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4475 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4476 | spin_unlock_irq(&conf->device_lock); |
| 4477 | wake_up(&conf->wait_for_overlap); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4478 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4479 | } |
| 4480 | |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4481 | INIT_LIST_HEAD(&stripes); |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4482 | for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4483 | int j; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4484 | int skipped_disk = 0; |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4485 | sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4486 | set_bit(STRIPE_EXPANDING, &sh->state); |
| 4487 | atomic_inc(&conf->reshape_stripes); |
| 4488 | /* If any of this stripe is beyond the end of the old |
| 4489 | * array, then we need to zero those blocks |
| 4490 | */ |
| 4491 | for (j=sh->disks; j--;) { |
| 4492 | sector_t s; |
| 4493 | if (j == sh->pd_idx) |
| 4494 | continue; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4495 | if (conf->level == 6 && |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4496 | j == sh->qd_idx) |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4497 | continue; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 4498 | s = compute_blocknr(sh, j, 0); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4499 | if (s < raid5_size(mddev, 0, 0)) { |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4500 | skipped_disk = 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4501 | continue; |
| 4502 | } |
| 4503 | memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE); |
| 4504 | set_bit(R5_Expanded, &sh->dev[j].flags); |
| 4505 | set_bit(R5_UPTODATE, &sh->dev[j].flags); |
| 4506 | } |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4507 | if (!skipped_disk) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4508 | set_bit(STRIPE_EXPAND_READY, &sh->state); |
| 4509 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4510 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4511 | list_add(&sh->lru, &stripes); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4512 | } |
| 4513 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4514 | if (mddev->reshape_backwards) |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4515 | conf->reshape_progress -= reshape_sectors * new_data_disks; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4516 | else |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4517 | conf->reshape_progress += reshape_sectors * new_data_disks; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4518 | spin_unlock_irq(&conf->device_lock); |
| 4519 | /* Ok, those stripe are ready. We can start scheduling |
| 4520 | * reads on the source stripes. |
| 4521 | * The source stripes are determined by mapping the first and last |
| 4522 | * block on the destination stripes. |
| 4523 | */ |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4524 | first_sector = |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4525 | raid5_compute_sector(conf, stripe_addr*(new_data_disks), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4526 | 1, &dd_idx, NULL); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4527 | last_sector = |
NeilBrown | 0e6e027 | 2009-06-09 16:32:22 +1000 | [diff] [blame] | 4528 | raid5_compute_sector(conf, ((stripe_addr+reshape_sectors) |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 4529 | * new_data_disks - 1), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4530 | 1, &dd_idx, NULL); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4531 | if (last_sector >= mddev->dev_sectors) |
| 4532 | last_sector = mddev->dev_sectors - 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4533 | while (first_sector <= last_sector) { |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4534 | sh = get_active_stripe(conf, first_sector, 1, 0, 1); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4535 | set_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 4536 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4537 | release_stripe(sh); |
| 4538 | first_sector += STRIPE_SECTORS; |
| 4539 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4540 | /* Now that the sources are clearly marked, we can release |
| 4541 | * the destination stripes |
| 4542 | */ |
| 4543 | while (!list_empty(&stripes)) { |
| 4544 | sh = list_entry(stripes.next, struct stripe_head, lru); |
| 4545 | list_del_init(&sh->lru); |
| 4546 | release_stripe(sh); |
| 4547 | } |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4548 | /* If this takes us to the resync_max point where we have to pause, |
| 4549 | * then we need to write out the superblock. |
| 4550 | */ |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4551 | sector_nr += reshape_sectors; |
NeilBrown | c03f6a1 | 2009-04-17 11:06:30 +1000 | [diff] [blame] | 4552 | if ((sector_nr - mddev->curr_resync_completed) * 2 |
| 4553 | >= mddev->resync_max - mddev->curr_resync_completed) { |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4554 | /* Cannot proceed until we've updated the superblock... */ |
| 4555 | wait_event(conf->wait_for_overlap, |
| 4556 | atomic_read(&conf->reshape_stripes) == 0); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4557 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4558 | mddev->curr_resync_completed = sector_nr; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4559 | conf->reshape_checkpoint = jiffies; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4560 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 4561 | md_wakeup_thread(mddev->thread); |
| 4562 | wait_event(mddev->sb_wait, |
| 4563 | !test_bit(MD_CHANGE_DEVS, &mddev->flags) |
| 4564 | || kthread_should_stop()); |
| 4565 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4566 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4567 | spin_unlock_irq(&conf->device_lock); |
| 4568 | wake_up(&conf->wait_for_overlap); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4569 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4570 | } |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4571 | return reshape_sectors; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4572 | } |
| 4573 | |
| 4574 | /* FIXME go_faster isn't used */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4575 | 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] | 4576 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4577 | struct r5conf *conf = mddev->private; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4578 | struct stripe_head *sh; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4579 | sector_t max_sector = mddev->dev_sectors; |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 4580 | sector_t sync_blocks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4581 | int still_degraded = 0; |
| 4582 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4583 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4584 | if (sector_nr >= max_sector) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4585 | /* just being told to finish up .. nothing much to do */ |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 4586 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 4587 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { |
| 4588 | end_reshape(conf); |
| 4589 | return 0; |
| 4590 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4591 | |
| 4592 | if (mddev->curr_resync < max_sector) /* aborted */ |
| 4593 | bitmap_end_sync(mddev->bitmap, mddev->curr_resync, |
| 4594 | &sync_blocks, 1); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4595 | else /* completed sync */ |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4596 | conf->fullsync = 0; |
| 4597 | bitmap_close_sync(mddev->bitmap); |
| 4598 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4599 | return 0; |
| 4600 | } |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4601 | |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4602 | /* Allow raid5_quiesce to complete */ |
| 4603 | wait_event(conf->wait_for_overlap, conf->quiesce != 2); |
| 4604 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4605 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 4606 | return reshape_request(mddev, sector_nr, skipped); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4607 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4608 | /* No need to check resync_max as we never do more than one |
| 4609 | * stripe, and as resync_max will always be on a chunk boundary, |
| 4610 | * if the check in md_do_sync didn't fire, there is no chance |
| 4611 | * of overstepping resync_max here |
| 4612 | */ |
| 4613 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4614 | /* if there is too many failed drives and we are trying |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4615 | * to resync, then assert that we are finished, because there is |
| 4616 | * nothing we can do. |
| 4617 | */ |
NeilBrown | 3285edf | 2006-06-26 00:27:55 -0700 | [diff] [blame] | 4618 | if (mddev->degraded >= conf->max_degraded && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4619 | test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4620 | sector_t rv = mddev->dev_sectors - sector_nr; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 4621 | *skipped = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4622 | return rv; |
| 4623 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4624 | if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) && |
NeilBrown | 3855ad9 | 2005-11-08 21:39:38 -0800 | [diff] [blame] | 4625 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4626 | !conf->fullsync && sync_blocks >= STRIPE_SECTORS) { |
| 4627 | /* we can skip this block, and probably more */ |
| 4628 | sync_blocks /= STRIPE_SECTORS; |
| 4629 | *skipped = 1; |
| 4630 | return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */ |
| 4631 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4632 | |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 4633 | bitmap_cond_end_sync(mddev->bitmap, sector_nr); |
| 4634 | |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4635 | sh = get_active_stripe(conf, sector_nr, 0, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4636 | if (sh == NULL) { |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4637 | sh = get_active_stripe(conf, sector_nr, 0, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4638 | /* make sure we don't swamp the stripe cache if someone else |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4639 | * is trying to get access |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4640 | */ |
Nishanth Aravamudan | 66c006a | 2005-11-07 01:01:17 -0800 | [diff] [blame] | 4641 | schedule_timeout_uninterruptible(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4642 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4643 | /* Need to check if array will still be degraded after recovery/resync |
| 4644 | * We don't need to check the 'failed' flag as when that gets set, |
| 4645 | * recovery aborts. |
| 4646 | */ |
NeilBrown | f001a70 | 2009-06-09 14:30:31 +1000 | [diff] [blame] | 4647 | for (i = 0; i < conf->raid_disks; i++) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4648 | if (conf->disks[i].rdev == NULL) |
| 4649 | still_degraded = 1; |
| 4650 | |
| 4651 | bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded); |
| 4652 | |
NeilBrown | 83206d6 | 2011-07-26 11:19:49 +1000 | [diff] [blame] | 4653 | set_bit(STRIPE_SYNC_REQUESTED, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4654 | |
NeilBrown | 1442577 | 2009-10-16 15:55:25 +1100 | [diff] [blame] | 4655 | handle_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4656 | release_stripe(sh); |
| 4657 | |
| 4658 | return STRIPE_SECTORS; |
| 4659 | } |
| 4660 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4661 | 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] | 4662 | { |
| 4663 | /* We may not be able to submit a whole bio at once as there |
| 4664 | * may not be enough stripe_heads available. |
| 4665 | * We cannot pre-allocate enough stripe_heads as we may need |
| 4666 | * more than exist in the cache (if we allow ever large chunks). |
| 4667 | * So we do one stripe head at a time and record in |
| 4668 | * ->bi_hw_segments how many have been done. |
| 4669 | * |
| 4670 | * We *know* that this entire raid_bio is in one chunk, so |
| 4671 | * it will be only one 'dd_idx' and only need one call to raid5_compute_sector. |
| 4672 | */ |
| 4673 | struct stripe_head *sh; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4674 | int dd_idx; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4675 | sector_t sector, logical_sector, last_sector; |
| 4676 | int scnt = 0; |
| 4677 | int remaining; |
| 4678 | int handled = 0; |
| 4679 | |
| 4680 | logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4681 | sector = raid5_compute_sector(conf, logical_sector, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4682 | 0, &dd_idx, NULL); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4683 | last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9); |
| 4684 | |
| 4685 | for (; logical_sector < last_sector; |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4686 | logical_sector += STRIPE_SECTORS, |
| 4687 | sector += STRIPE_SECTORS, |
| 4688 | scnt++) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4689 | |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 4690 | if (scnt < raid5_bi_processed_stripes(raid_bio)) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4691 | /* already done this stripe */ |
| 4692 | continue; |
| 4693 | |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4694 | sh = get_active_stripe(conf, sector, 0, 1, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4695 | |
| 4696 | if (!sh) { |
| 4697 | /* failed to get a stripe - must wait */ |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 4698 | raid5_set_bi_processed_stripes(raid_bio, scnt); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4699 | conf->retry_read_aligned = raid_bio; |
| 4700 | return handled; |
| 4701 | } |
| 4702 | |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4703 | if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) { |
| 4704 | release_stripe(sh); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 4705 | raid5_set_bi_processed_stripes(raid_bio, scnt); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4706 | conf->retry_read_aligned = raid_bio; |
| 4707 | return handled; |
| 4708 | } |
| 4709 | |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 4710 | set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4711 | handle_stripe(sh); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4712 | release_stripe(sh); |
| 4713 | handled++; |
| 4714 | } |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 4715 | remaining = raid5_dec_bi_active_stripes(raid_bio); |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 4716 | if (remaining == 0) |
| 4717 | bio_endio(raid_bio, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4718 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
| 4719 | wake_up(&conf->wait_for_stripe); |
| 4720 | return handled; |
| 4721 | } |
| 4722 | |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 4723 | #define MAX_STRIPE_BATCH 8 |
| 4724 | static int handle_active_stripes(struct r5conf *conf) |
| 4725 | { |
| 4726 | struct stripe_head *batch[MAX_STRIPE_BATCH], *sh; |
| 4727 | int i, batch_size = 0; |
| 4728 | |
| 4729 | while (batch_size < MAX_STRIPE_BATCH && |
| 4730 | (sh = __get_priority_stripe(conf)) != NULL) |
| 4731 | batch[batch_size++] = sh; |
| 4732 | |
| 4733 | if (batch_size == 0) |
| 4734 | return batch_size; |
| 4735 | spin_unlock_irq(&conf->device_lock); |
| 4736 | |
| 4737 | for (i = 0; i < batch_size; i++) |
| 4738 | handle_stripe(batch[i]); |
| 4739 | |
| 4740 | cond_resched(); |
| 4741 | |
| 4742 | spin_lock_irq(&conf->device_lock); |
| 4743 | for (i = 0; i < batch_size; i++) |
| 4744 | __release_stripe(conf, batch[i]); |
| 4745 | return batch_size; |
| 4746 | } |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4747 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4748 | /* |
| 4749 | * This is our raid5 kernel thread. |
| 4750 | * |
| 4751 | * We scan the hash table for stripes which can be handled now. |
| 4752 | * During the scan, completed stripes are saved for us by the interrupt |
| 4753 | * handler, so that they will not have to wait for our next wakeup. |
| 4754 | */ |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 4755 | static void raid5d(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4756 | { |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 4757 | struct mddev *mddev = thread->mddev; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4758 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4759 | int handled; |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 4760 | struct blk_plug plug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4761 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4762 | pr_debug("+++ raid5d active\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4763 | |
| 4764 | md_check_recovery(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4765 | |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 4766 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4767 | handled = 0; |
| 4768 | spin_lock_irq(&conf->device_lock); |
| 4769 | while (1) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4770 | struct bio *bio; |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 4771 | int batch_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4772 | |
NeilBrown | 0021b7b | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 4773 | if ( |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 4774 | !list_empty(&conf->bitmap_list)) { |
| 4775 | /* Now is a good time to flush some bitmap updates */ |
| 4776 | conf->seq_flush++; |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 4777 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4778 | bitmap_unplug(mddev->bitmap); |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 4779 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 4780 | conf->seq_write = conf->seq_flush; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4781 | activate_bit_delay(conf); |
| 4782 | } |
NeilBrown | 0021b7b | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 4783 | raid5_activate_delayed(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4784 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4785 | while ((bio = remove_bio_from_retry(conf))) { |
| 4786 | int ok; |
| 4787 | spin_unlock_irq(&conf->device_lock); |
| 4788 | ok = retry_aligned_read(conf, bio); |
| 4789 | spin_lock_irq(&conf->device_lock); |
| 4790 | if (!ok) |
| 4791 | break; |
| 4792 | handled++; |
| 4793 | } |
| 4794 | |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 4795 | batch_size = handle_active_stripes(conf); |
| 4796 | if (!batch_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4797 | break; |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 4798 | handled += batch_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4799 | |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 4800 | if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) { |
| 4801 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 4802 | md_check_recovery(mddev); |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 4803 | spin_lock_irq(&conf->device_lock); |
| 4804 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4805 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4806 | pr_debug("%d stripes handled\n", handled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4807 | |
| 4808 | spin_unlock_irq(&conf->device_lock); |
| 4809 | |
Dan Williams | c9f21aa | 2008-07-23 12:05:51 -0700 | [diff] [blame] | 4810 | async_tx_issue_pending_all(); |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 4811 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4812 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4813 | pr_debug("--- raid5d inactive\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4814 | } |
| 4815 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4816 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4817 | raid5_show_stripe_cache_size(struct mddev *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4818 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4819 | struct r5conf *conf = mddev->private; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4820 | if (conf) |
| 4821 | return sprintf(page, "%d\n", conf->max_nr_stripes); |
| 4822 | else |
| 4823 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4824 | } |
| 4825 | |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 4826 | int |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4827 | raid5_set_cache_size(struct mddev *mddev, int size) |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 4828 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4829 | struct r5conf *conf = mddev->private; |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 4830 | int err; |
| 4831 | |
| 4832 | if (size <= 16 || size > 32768) |
| 4833 | return -EINVAL; |
| 4834 | while (size < conf->max_nr_stripes) { |
| 4835 | if (drop_one_stripe(conf)) |
| 4836 | conf->max_nr_stripes--; |
| 4837 | else |
| 4838 | break; |
| 4839 | } |
| 4840 | err = md_allow_write(mddev); |
| 4841 | if (err) |
| 4842 | return err; |
| 4843 | while (size > conf->max_nr_stripes) { |
| 4844 | if (grow_one_stripe(conf)) |
| 4845 | conf->max_nr_stripes++; |
| 4846 | else break; |
| 4847 | } |
| 4848 | return 0; |
| 4849 | } |
| 4850 | EXPORT_SYMBOL(raid5_set_cache_size); |
| 4851 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4852 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4853 | 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] | 4854 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4855 | struct r5conf *conf = mddev->private; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4856 | unsigned long new; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4857 | int err; |
| 4858 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4859 | if (len >= PAGE_SIZE) |
| 4860 | return -EINVAL; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4861 | if (!conf) |
| 4862 | return -ENODEV; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4863 | |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4864 | if (strict_strtoul(page, 10, &new)) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4865 | return -EINVAL; |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 4866 | err = raid5_set_cache_size(mddev, new); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4867 | if (err) |
| 4868 | return err; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4869 | return len; |
| 4870 | } |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4871 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4872 | static struct md_sysfs_entry |
| 4873 | raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR, |
| 4874 | raid5_show_stripe_cache_size, |
| 4875 | raid5_store_stripe_cache_size); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4876 | |
| 4877 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4878 | raid5_show_preread_threshold(struct mddev *mddev, char *page) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4879 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4880 | struct r5conf *conf = mddev->private; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4881 | if (conf) |
| 4882 | return sprintf(page, "%d\n", conf->bypass_threshold); |
| 4883 | else |
| 4884 | return 0; |
| 4885 | } |
| 4886 | |
| 4887 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4888 | 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] | 4889 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4890 | struct r5conf *conf = mddev->private; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4891 | unsigned long new; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4892 | if (len >= PAGE_SIZE) |
| 4893 | return -EINVAL; |
| 4894 | if (!conf) |
| 4895 | return -ENODEV; |
| 4896 | |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4897 | if (strict_strtoul(page, 10, &new)) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4898 | return -EINVAL; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4899 | if (new > conf->max_nr_stripes) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4900 | return -EINVAL; |
| 4901 | conf->bypass_threshold = new; |
| 4902 | return len; |
| 4903 | } |
| 4904 | |
| 4905 | static struct md_sysfs_entry |
| 4906 | raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold, |
| 4907 | S_IRUGO | S_IWUSR, |
| 4908 | raid5_show_preread_threshold, |
| 4909 | raid5_store_preread_threshold); |
| 4910 | |
| 4911 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4912 | stripe_cache_active_show(struct mddev *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4913 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4914 | struct r5conf *conf = mddev->private; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4915 | if (conf) |
| 4916 | return sprintf(page, "%d\n", atomic_read(&conf->active_stripes)); |
| 4917 | else |
| 4918 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4919 | } |
| 4920 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4921 | static struct md_sysfs_entry |
| 4922 | raid5_stripecache_active = __ATTR_RO(stripe_cache_active); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4923 | |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4924 | static struct attribute *raid5_attrs[] = { |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4925 | &raid5_stripecache_size.attr, |
| 4926 | &raid5_stripecache_active.attr, |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4927 | &raid5_preread_bypass_threshold.attr, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4928 | NULL, |
| 4929 | }; |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4930 | static struct attribute_group raid5_attrs_group = { |
| 4931 | .name = NULL, |
| 4932 | .attrs = raid5_attrs, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4933 | }; |
| 4934 | |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4935 | static sector_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4936 | raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks) |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4937 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4938 | struct r5conf *conf = mddev->private; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4939 | |
| 4940 | if (!sectors) |
| 4941 | sectors = mddev->dev_sectors; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4942 | if (!raid_disks) |
NeilBrown | 7ec0547 | 2009-03-31 15:10:36 +1100 | [diff] [blame] | 4943 | /* size is defined by the smallest of previous and new size */ |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4944 | raid_disks = min(conf->raid_disks, conf->previous_raid_disks); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4945 | |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4946 | sectors &= ~((sector_t)mddev->chunk_sectors - 1); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4947 | sectors &= ~((sector_t)mddev->new_chunk_sectors - 1); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4948 | return sectors * (raid_disks - conf->max_degraded); |
| 4949 | } |
| 4950 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4951 | static void raid5_free_percpu(struct r5conf *conf) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4952 | { |
| 4953 | struct raid5_percpu *percpu; |
| 4954 | unsigned long cpu; |
| 4955 | |
| 4956 | if (!conf->percpu) |
| 4957 | return; |
| 4958 | |
| 4959 | get_online_cpus(); |
| 4960 | for_each_possible_cpu(cpu) { |
| 4961 | percpu = per_cpu_ptr(conf->percpu, cpu); |
| 4962 | safe_put_page(percpu->spare_page); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4963 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4964 | } |
| 4965 | #ifdef CONFIG_HOTPLUG_CPU |
| 4966 | unregister_cpu_notifier(&conf->cpu_notify); |
| 4967 | #endif |
| 4968 | put_online_cpus(); |
| 4969 | |
| 4970 | free_percpu(conf->percpu); |
| 4971 | } |
| 4972 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4973 | static void free_conf(struct r5conf *conf) |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 4974 | { |
| 4975 | shrink_stripes(conf); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4976 | raid5_free_percpu(conf); |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 4977 | kfree(conf->disks); |
| 4978 | kfree(conf->stripe_hashtbl); |
| 4979 | kfree(conf); |
| 4980 | } |
| 4981 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4982 | #ifdef CONFIG_HOTPLUG_CPU |
| 4983 | static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action, |
| 4984 | void *hcpu) |
| 4985 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4986 | struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4987 | long cpu = (long)hcpu; |
| 4988 | struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu); |
| 4989 | |
| 4990 | switch (action) { |
| 4991 | case CPU_UP_PREPARE: |
| 4992 | case CPU_UP_PREPARE_FROZEN: |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4993 | if (conf->level == 6 && !percpu->spare_page) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4994 | percpu->spare_page = alloc_page(GFP_KERNEL); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4995 | if (!percpu->scribble) |
| 4996 | percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL); |
| 4997 | |
| 4998 | if (!percpu->scribble || |
| 4999 | (conf->level == 6 && !percpu->spare_page)) { |
| 5000 | safe_put_page(percpu->spare_page); |
| 5001 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5002 | pr_err("%s: failed memory allocation for cpu%ld\n", |
| 5003 | __func__, cpu); |
Akinobu Mita | 55af6bb | 2010-05-26 14:43:35 -0700 | [diff] [blame] | 5004 | return notifier_from_errno(-ENOMEM); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5005 | } |
| 5006 | break; |
| 5007 | case CPU_DEAD: |
| 5008 | case CPU_DEAD_FROZEN: |
| 5009 | safe_put_page(percpu->spare_page); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 5010 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5011 | percpu->spare_page = NULL; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 5012 | percpu->scribble = NULL; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5013 | break; |
| 5014 | default: |
| 5015 | break; |
| 5016 | } |
| 5017 | return NOTIFY_OK; |
| 5018 | } |
| 5019 | #endif |
| 5020 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5021 | static int raid5_alloc_percpu(struct r5conf *conf) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5022 | { |
| 5023 | unsigned long cpu; |
| 5024 | struct page *spare_page; |
Tejun Heo | a29d8b8 | 2010-02-02 14:39:15 +0900 | [diff] [blame] | 5025 | struct raid5_percpu __percpu *allcpus; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 5026 | void *scribble; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5027 | int err; |
| 5028 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5029 | allcpus = alloc_percpu(struct raid5_percpu); |
| 5030 | if (!allcpus) |
| 5031 | return -ENOMEM; |
| 5032 | conf->percpu = allcpus; |
| 5033 | |
| 5034 | get_online_cpus(); |
| 5035 | err = 0; |
| 5036 | for_each_present_cpu(cpu) { |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 5037 | if (conf->level == 6) { |
| 5038 | spare_page = alloc_page(GFP_KERNEL); |
| 5039 | if (!spare_page) { |
| 5040 | err = -ENOMEM; |
| 5041 | break; |
| 5042 | } |
| 5043 | per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page; |
| 5044 | } |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 5045 | scribble = kmalloc(conf->scribble_len, GFP_KERNEL); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 5046 | if (!scribble) { |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5047 | err = -ENOMEM; |
| 5048 | break; |
| 5049 | } |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 5050 | per_cpu_ptr(conf->percpu, cpu)->scribble = scribble; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5051 | } |
| 5052 | #ifdef CONFIG_HOTPLUG_CPU |
| 5053 | conf->cpu_notify.notifier_call = raid456_cpu_notify; |
| 5054 | conf->cpu_notify.priority = 0; |
| 5055 | if (err == 0) |
| 5056 | err = register_cpu_notifier(&conf->cpu_notify); |
| 5057 | #endif |
| 5058 | put_online_cpus(); |
| 5059 | |
| 5060 | return err; |
| 5061 | } |
| 5062 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5063 | static struct r5conf *setup_conf(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5064 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5065 | struct r5conf *conf; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 5066 | int raid_disk, memory, max_disks; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5067 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5068 | struct disk_info *disk; |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 5069 | char pers_name[6]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5070 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5071 | if (mddev->new_level != 5 |
| 5072 | && mddev->new_level != 4 |
| 5073 | && mddev->new_level != 6) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5074 | 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] | 5075 | mdname(mddev), mddev->new_level); |
| 5076 | return ERR_PTR(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5077 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5078 | if ((mddev->new_level == 5 |
| 5079 | && !algorithm_valid_raid5(mddev->new_layout)) || |
| 5080 | (mddev->new_level == 6 |
| 5081 | && !algorithm_valid_raid6(mddev->new_layout))) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5082 | printk(KERN_ERR "md/raid:%s: layout %d not supported\n", |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5083 | mdname(mddev), mddev->new_layout); |
| 5084 | return ERR_PTR(-EIO); |
| 5085 | } |
| 5086 | if (mddev->new_level == 6 && mddev->raid_disks < 4) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5087 | 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] | 5088 | mdname(mddev), mddev->raid_disks); |
| 5089 | return ERR_PTR(-EINVAL); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5090 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5091 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5092 | if (!mddev->new_chunk_sectors || |
| 5093 | (mddev->new_chunk_sectors << 9) % PAGE_SIZE || |
| 5094 | !is_power_of_2(mddev->new_chunk_sectors)) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5095 | printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n", |
| 5096 | mdname(mddev), mddev->new_chunk_sectors << 9); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5097 | return ERR_PTR(-EINVAL); |
NeilBrown | 4bbf377 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5098 | } |
| 5099 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5100 | conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5101 | if (conf == NULL) |
| 5102 | goto abort; |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 5103 | spin_lock_init(&conf->device_lock); |
| 5104 | init_waitqueue_head(&conf->wait_for_stripe); |
| 5105 | init_waitqueue_head(&conf->wait_for_overlap); |
| 5106 | INIT_LIST_HEAD(&conf->handle_list); |
| 5107 | INIT_LIST_HEAD(&conf->hold_list); |
| 5108 | INIT_LIST_HEAD(&conf->delayed_list); |
| 5109 | INIT_LIST_HEAD(&conf->bitmap_list); |
| 5110 | INIT_LIST_HEAD(&conf->inactive_list); |
| 5111 | atomic_set(&conf->active_stripes, 0); |
| 5112 | atomic_set(&conf->preread_active_stripes, 0); |
| 5113 | atomic_set(&conf->active_aligned_reads, 0); |
| 5114 | conf->bypass_threshold = BYPASS_THRESHOLD; |
NeilBrown | d890fa2 | 2011-10-26 11:54:39 +1100 | [diff] [blame] | 5115 | conf->recovery_disabled = mddev->recovery_disabled - 1; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5116 | |
| 5117 | conf->raid_disks = mddev->raid_disks; |
| 5118 | if (mddev->reshape_position == MaxSector) |
| 5119 | conf->previous_raid_disks = mddev->raid_disks; |
| 5120 | else |
| 5121 | conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 5122 | max_disks = max(conf->raid_disks, conf->previous_raid_disks); |
| 5123 | conf->scribble_len = scribble_len(max_disks); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5124 | |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 5125 | conf->disks = kzalloc(max_disks * sizeof(struct disk_info), |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5126 | GFP_KERNEL); |
| 5127 | if (!conf->disks) |
| 5128 | goto abort; |
| 5129 | |
| 5130 | conf->mddev = mddev; |
| 5131 | |
| 5132 | if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL) |
| 5133 | goto abort; |
| 5134 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5135 | conf->level = mddev->new_level; |
| 5136 | if (raid5_alloc_percpu(conf) != 0) |
| 5137 | goto abort; |
| 5138 | |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5139 | pr_debug("raid456: run(%s) called.\n", mdname(mddev)); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5140 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5141 | rdev_for_each(rdev, mddev) { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5142 | raid_disk = rdev->raid_disk; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 5143 | if (raid_disk >= max_disks |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5144 | || raid_disk < 0) |
| 5145 | continue; |
| 5146 | disk = conf->disks + raid_disk; |
| 5147 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5148 | if (test_bit(Replacement, &rdev->flags)) { |
| 5149 | if (disk->replacement) |
| 5150 | goto abort; |
| 5151 | disk->replacement = rdev; |
| 5152 | } else { |
| 5153 | if (disk->rdev) |
| 5154 | goto abort; |
| 5155 | disk->rdev = rdev; |
| 5156 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5157 | |
| 5158 | if (test_bit(In_sync, &rdev->flags)) { |
| 5159 | char b[BDEVNAME_SIZE]; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5160 | printk(KERN_INFO "md/raid:%s: device %s operational as raid" |
| 5161 | " disk %d\n", |
| 5162 | mdname(mddev), bdevname(rdev->bdev, b), raid_disk); |
Jonathan Brassow | d6b212f | 2011-06-08 18:00:28 -0500 | [diff] [blame] | 5163 | } else if (rdev->saved_raid_disk != raid_disk) |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5164 | /* Cannot rely on bitmap to complete recovery */ |
| 5165 | conf->fullsync = 1; |
| 5166 | } |
| 5167 | |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5168 | conf->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5169 | conf->level = mddev->new_level; |
| 5170 | if (conf->level == 6) |
| 5171 | conf->max_degraded = 2; |
| 5172 | else |
| 5173 | conf->max_degraded = 1; |
| 5174 | conf->algorithm = mddev->new_layout; |
| 5175 | conf->max_nr_stripes = NR_STRIPES; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5176 | conf->reshape_progress = mddev->reshape_position; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 5177 | if (conf->reshape_progress != MaxSector) { |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5178 | conf->prev_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 5179 | conf->prev_algo = mddev->layout; |
| 5180 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5181 | |
| 5182 | memory = conf->max_nr_stripes * (sizeof(struct stripe_head) + |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 5183 | max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5184 | if (grow_stripes(conf, conf->max_nr_stripes)) { |
| 5185 | printk(KERN_ERR |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5186 | "md/raid:%s: couldn't allocate %dkB for buffers\n", |
| 5187 | mdname(mddev), memory); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5188 | goto abort; |
| 5189 | } else |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5190 | printk(KERN_INFO "md/raid:%s: allocated %dkB\n", |
| 5191 | mdname(mddev), memory); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5192 | |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 5193 | sprintf(pers_name, "raid%d", mddev->new_level); |
| 5194 | conf->thread = md_register_thread(raid5d, mddev, pers_name); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5195 | if (!conf->thread) { |
| 5196 | printk(KERN_ERR |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5197 | "md/raid:%s: couldn't allocate thread.\n", |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5198 | mdname(mddev)); |
| 5199 | goto abort; |
| 5200 | } |
| 5201 | |
| 5202 | return conf; |
| 5203 | |
| 5204 | abort: |
| 5205 | if (conf) { |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 5206 | free_conf(conf); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5207 | return ERR_PTR(-EIO); |
| 5208 | } else |
| 5209 | return ERR_PTR(-ENOMEM); |
| 5210 | } |
| 5211 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5212 | |
| 5213 | static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded) |
| 5214 | { |
| 5215 | switch (algo) { |
| 5216 | case ALGORITHM_PARITY_0: |
| 5217 | if (raid_disk < max_degraded) |
| 5218 | return 1; |
| 5219 | break; |
| 5220 | case ALGORITHM_PARITY_N: |
| 5221 | if (raid_disk >= raid_disks - max_degraded) |
| 5222 | return 1; |
| 5223 | break; |
| 5224 | case ALGORITHM_PARITY_0_6: |
| 5225 | if (raid_disk == 0 || |
| 5226 | raid_disk == raid_disks - 1) |
| 5227 | return 1; |
| 5228 | break; |
| 5229 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 5230 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 5231 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 5232 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 5233 | if (raid_disk == raid_disks - 1) |
| 5234 | return 1; |
| 5235 | } |
| 5236 | return 0; |
| 5237 | } |
| 5238 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5239 | static int run(struct mddev *mddev) |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5240 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5241 | struct r5conf *conf; |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 5242 | int working_disks = 0; |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5243 | int dirty_parity_disks = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5244 | struct md_rdev *rdev; |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5245 | sector_t reshape_offset = 0; |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5246 | int i; |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5247 | long long min_offset_diff = 0; |
| 5248 | int first = 1; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5249 | |
Andre Noll | 8c6ac868 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 5250 | if (mddev->recovery_cp != MaxSector) |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5251 | printk(KERN_NOTICE "md/raid:%s: not clean" |
Andre Noll | 8c6ac868 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 5252 | " -- starting background reconstruction\n", |
| 5253 | mdname(mddev)); |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5254 | |
| 5255 | rdev_for_each(rdev, mddev) { |
| 5256 | long long diff; |
| 5257 | if (rdev->raid_disk < 0) |
| 5258 | continue; |
| 5259 | diff = (rdev->new_data_offset - rdev->data_offset); |
| 5260 | if (first) { |
| 5261 | min_offset_diff = diff; |
| 5262 | first = 0; |
| 5263 | } else if (mddev->reshape_backwards && |
| 5264 | diff < min_offset_diff) |
| 5265 | min_offset_diff = diff; |
| 5266 | else if (!mddev->reshape_backwards && |
| 5267 | diff > min_offset_diff) |
| 5268 | min_offset_diff = diff; |
| 5269 | } |
| 5270 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5271 | if (mddev->reshape_position != MaxSector) { |
| 5272 | /* Check that we can continue the reshape. |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5273 | * Difficulties arise if the stripe we would write to |
| 5274 | * next is at or after the stripe we would read from next. |
| 5275 | * For a reshape that changes the number of devices, this |
| 5276 | * is only possible for a very short time, and mdadm makes |
| 5277 | * sure that time appears to have past before assembling |
| 5278 | * the array. So we fail if that time hasn't passed. |
| 5279 | * For a reshape that keeps the number of devices the same |
| 5280 | * mdadm must be monitoring the reshape can keeping the |
| 5281 | * critical areas read-only and backed up. It will start |
| 5282 | * the array in read-only mode, so we check for that. |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5283 | */ |
| 5284 | sector_t here_new, here_old; |
| 5285 | int old_disks; |
Andre Noll | 18b0033 | 2009-03-31 15:00:56 +1100 | [diff] [blame] | 5286 | int max_degraded = (mddev->level == 6 ? 2 : 1); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5287 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5288 | if (mddev->new_level != mddev->level) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5289 | printk(KERN_ERR "md/raid:%s: unsupported reshape " |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5290 | "required - aborting.\n", |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5291 | mdname(mddev)); |
| 5292 | return -EINVAL; |
| 5293 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5294 | old_disks = mddev->raid_disks - mddev->delta_disks; |
| 5295 | /* reshape_position must be on a new-stripe boundary, and one |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5296 | * further up in new geometry must map after here in old |
| 5297 | * geometry. |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5298 | */ |
| 5299 | here_new = mddev->reshape_position; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5300 | if (sector_div(here_new, mddev->new_chunk_sectors * |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5301 | (mddev->raid_disks - max_degraded))) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5302 | printk(KERN_ERR "md/raid:%s: reshape_position not " |
| 5303 | "on a stripe boundary\n", mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5304 | return -EINVAL; |
| 5305 | } |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5306 | reshape_offset = here_new * mddev->new_chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5307 | /* here_new is the stripe we will write to */ |
| 5308 | here_old = mddev->reshape_position; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5309 | sector_div(here_old, mddev->chunk_sectors * |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5310 | (old_disks-max_degraded)); |
| 5311 | /* here_old is the first stripe that we might need to read |
| 5312 | * from */ |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5313 | if (mddev->delta_disks == 0) { |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5314 | if ((here_new * mddev->new_chunk_sectors != |
| 5315 | here_old * mddev->chunk_sectors)) { |
| 5316 | printk(KERN_ERR "md/raid:%s: reshape position is" |
| 5317 | " confused - aborting\n", mdname(mddev)); |
| 5318 | return -EINVAL; |
| 5319 | } |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5320 | /* We cannot be sure it is safe to start an in-place |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5321 | * reshape. It is only safe if user-space is monitoring |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5322 | * and taking constant backups. |
| 5323 | * mdadm always starts a situation like this in |
| 5324 | * readonly mode so it can take control before |
| 5325 | * allowing any writes. So just check for that. |
| 5326 | */ |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5327 | if (abs(min_offset_diff) >= mddev->chunk_sectors && |
| 5328 | abs(min_offset_diff) >= mddev->new_chunk_sectors) |
| 5329 | /* not really in-place - so OK */; |
| 5330 | else if (mddev->ro == 0) { |
| 5331 | printk(KERN_ERR "md/raid:%s: in-place reshape " |
| 5332 | "must be started in read-only mode " |
| 5333 | "- aborting\n", |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5334 | mdname(mddev)); |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5335 | return -EINVAL; |
| 5336 | } |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5337 | } else if (mddev->reshape_backwards |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5338 | ? (here_new * mddev->new_chunk_sectors + min_offset_diff <= |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5339 | here_old * mddev->chunk_sectors) |
| 5340 | : (here_new * mddev->new_chunk_sectors >= |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5341 | here_old * mddev->chunk_sectors + (-min_offset_diff))) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5342 | /* Reading from the same stripe as writing to - bad */ |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5343 | printk(KERN_ERR "md/raid:%s: reshape_position too early for " |
| 5344 | "auto-recovery - aborting.\n", |
| 5345 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5346 | return -EINVAL; |
| 5347 | } |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5348 | printk(KERN_INFO "md/raid:%s: reshape will continue\n", |
| 5349 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5350 | /* OK, we should be able to continue; */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5351 | } else { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5352 | BUG_ON(mddev->level != mddev->new_level); |
| 5353 | BUG_ON(mddev->layout != mddev->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5354 | BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5355 | BUG_ON(mddev->delta_disks != 0); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5356 | } |
| 5357 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5358 | if (mddev->private == NULL) |
| 5359 | conf = setup_conf(mddev); |
| 5360 | else |
| 5361 | conf = mddev->private; |
| 5362 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5363 | if (IS_ERR(conf)) |
| 5364 | return PTR_ERR(conf); |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 5365 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5366 | conf->min_offset_diff = min_offset_diff; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5367 | mddev->thread = conf->thread; |
| 5368 | conf->thread = NULL; |
| 5369 | mddev->private = conf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5370 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5371 | for (i = 0; i < conf->raid_disks && conf->previous_raid_disks; |
| 5372 | i++) { |
| 5373 | rdev = conf->disks[i].rdev; |
| 5374 | if (!rdev && conf->disks[i].replacement) { |
| 5375 | /* The replacement is all we have yet */ |
| 5376 | rdev = conf->disks[i].replacement; |
| 5377 | conf->disks[i].replacement = NULL; |
| 5378 | clear_bit(Replacement, &rdev->flags); |
| 5379 | conf->disks[i].rdev = rdev; |
| 5380 | } |
| 5381 | if (!rdev) |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5382 | continue; |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5383 | if (conf->disks[i].replacement && |
| 5384 | conf->reshape_progress != MaxSector) { |
| 5385 | /* replacements and reshape simply do not mix. */ |
| 5386 | printk(KERN_ERR "md: cannot handle concurrent " |
| 5387 | "replacement and reshape.\n"); |
| 5388 | goto abort; |
| 5389 | } |
NeilBrown | 2f11588 | 2010-06-17 17:41:03 +1000 | [diff] [blame] | 5390 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5391 | working_disks++; |
NeilBrown | 2f11588 | 2010-06-17 17:41:03 +1000 | [diff] [blame] | 5392 | continue; |
| 5393 | } |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5394 | /* This disc is not fully in-sync. However if it |
| 5395 | * just stored parity (beyond the recovery_offset), |
| 5396 | * when we don't need to be concerned about the |
| 5397 | * array being dirty. |
| 5398 | * When reshape goes 'backwards', we never have |
| 5399 | * partially completed devices, so we only need |
| 5400 | * to worry about reshape going forwards. |
| 5401 | */ |
| 5402 | /* Hack because v0.91 doesn't store recovery_offset properly. */ |
| 5403 | if (mddev->major_version == 0 && |
| 5404 | mddev->minor_version > 90) |
| 5405 | rdev->recovery_offset = reshape_offset; |
| 5406 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5407 | if (rdev->recovery_offset < reshape_offset) { |
| 5408 | /* We need to check old and new layout */ |
| 5409 | if (!only_parity(rdev->raid_disk, |
| 5410 | conf->algorithm, |
| 5411 | conf->raid_disks, |
| 5412 | conf->max_degraded)) |
| 5413 | continue; |
| 5414 | } |
| 5415 | if (!only_parity(rdev->raid_disk, |
| 5416 | conf->prev_algo, |
| 5417 | conf->previous_raid_disks, |
| 5418 | conf->max_degraded)) |
| 5419 | continue; |
| 5420 | dirty_parity_disks++; |
| 5421 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5422 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5423 | /* |
| 5424 | * 0 for a fully functional array, 1 or 2 for a degraded array. |
| 5425 | */ |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 5426 | mddev->degraded = calc_degraded(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5427 | |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 5428 | if (has_failed(conf)) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5429 | printk(KERN_ERR "md/raid:%s: not enough operational devices" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5430 | " (%d/%d failed)\n", |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 5431 | mdname(mddev), mddev->degraded, conf->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5432 | goto abort; |
| 5433 | } |
| 5434 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5435 | /* device size must be a multiple of chunk size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5436 | mddev->dev_sectors &= ~(mddev->chunk_sectors - 1); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5437 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 5438 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5439 | if (mddev->degraded > dirty_parity_disks && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5440 | mddev->recovery_cp != MaxSector) { |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5441 | if (mddev->ok_start_degraded) |
| 5442 | printk(KERN_WARNING |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5443 | "md/raid:%s: starting dirty degraded array" |
| 5444 | " - data corruption possible.\n", |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5445 | mdname(mddev)); |
| 5446 | else { |
| 5447 | printk(KERN_ERR |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5448 | "md/raid:%s: cannot start dirty degraded array.\n", |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5449 | mdname(mddev)); |
| 5450 | goto abort; |
| 5451 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5452 | } |
| 5453 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5454 | if (mddev->degraded == 0) |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5455 | printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d" |
| 5456 | " devices, algorithm %d\n", mdname(mddev), conf->level, |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 5457 | mddev->raid_disks-mddev->degraded, mddev->raid_disks, |
| 5458 | mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5459 | else |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5460 | printk(KERN_ALERT "md/raid:%s: raid level %d active with %d" |
| 5461 | " out of %d devices, algorithm %d\n", |
| 5462 | mdname(mddev), conf->level, |
| 5463 | mddev->raid_disks - mddev->degraded, |
| 5464 | mddev->raid_disks, mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5465 | |
| 5466 | print_raid5_conf(conf); |
| 5467 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5468 | if (conf->reshape_progress != MaxSector) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5469 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5470 | atomic_set(&conf->reshape_stripes, 0); |
| 5471 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 5472 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 5473 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 5474 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 5475 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 5476 | "reshape"); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5477 | } |
| 5478 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5479 | |
| 5480 | /* Ok, everything is just fine now */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 5481 | if (mddev->to_remove == &raid5_attrs_group) |
| 5482 | mddev->to_remove = NULL; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5483 | else if (mddev->kobj.sd && |
| 5484 | sysfs_create_group(&mddev->kobj, &raid5_attrs_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5485 | printk(KERN_WARNING |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 5486 | "raid5: failed to create sysfs attributes for %s\n", |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5487 | mdname(mddev)); |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 5488 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
| 5489 | |
| 5490 | if (mddev->queue) { |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 5491 | int chunk_size; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5492 | bool discard_supported = true; |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 5493 | /* read-ahead size must cover two whole stripes, which |
| 5494 | * is 2 * (datadisks) * chunksize where 'n' is the |
| 5495 | * number of raid devices |
| 5496 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5497 | int data_disks = conf->previous_raid_disks - conf->max_degraded; |
| 5498 | int stripe = data_disks * |
| 5499 | ((mddev->chunk_sectors << 9) / PAGE_SIZE); |
| 5500 | if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 5501 | mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 5502 | |
| 5503 | blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec); |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 5504 | |
| 5505 | mddev->queue->backing_dev_info.congested_data = mddev; |
| 5506 | mddev->queue->backing_dev_info.congested_fn = raid5_congested; |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 5507 | |
| 5508 | chunk_size = mddev->chunk_sectors << 9; |
| 5509 | blk_queue_io_min(mddev->queue, chunk_size); |
| 5510 | blk_queue_io_opt(mddev->queue, chunk_size * |
| 5511 | (conf->raid_disks - conf->max_degraded)); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5512 | /* |
| 5513 | * We can only discard a whole stripe. It doesn't make sense to |
| 5514 | * discard data disk but write parity disk |
| 5515 | */ |
| 5516 | stripe = stripe * PAGE_SIZE; |
| 5517 | mddev->queue->limits.discard_alignment = stripe; |
| 5518 | mddev->queue->limits.discard_granularity = stripe; |
| 5519 | /* |
| 5520 | * unaligned part of discard request will be ignored, so can't |
| 5521 | * guarantee discard_zerors_data |
| 5522 | */ |
| 5523 | mddev->queue->limits.discard_zeroes_data = 0; |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 5524 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5525 | rdev_for_each(rdev, mddev) { |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 5526 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 5527 | rdev->data_offset << 9); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5528 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 5529 | rdev->new_data_offset << 9); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5530 | /* |
| 5531 | * discard_zeroes_data is required, otherwise data |
| 5532 | * could be lost. Consider a scenario: discard a stripe |
| 5533 | * (the stripe could be inconsistent if |
| 5534 | * discard_zeroes_data is 0); write one disk of the |
| 5535 | * stripe (the stripe could be inconsistent again |
| 5536 | * depending on which disks are used to calculate |
| 5537 | * parity); the disk is broken; The stripe data of this |
| 5538 | * disk is lost. |
| 5539 | */ |
| 5540 | if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) || |
| 5541 | !bdev_get_queue(rdev->bdev)-> |
| 5542 | limits.discard_zeroes_data) |
| 5543 | discard_supported = false; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5544 | } |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5545 | |
| 5546 | if (discard_supported && |
| 5547 | mddev->queue->limits.max_discard_sectors >= stripe && |
| 5548 | mddev->queue->limits.discard_granularity >= stripe) |
| 5549 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, |
| 5550 | mddev->queue); |
| 5551 | else |
| 5552 | queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, |
| 5553 | mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5554 | } |
| 5555 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5556 | return 0; |
| 5557 | abort: |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 5558 | md_unregister_thread(&mddev->thread); |
NeilBrown | e4f869d | 2011-10-07 14:22:49 +1100 | [diff] [blame] | 5559 | print_raid5_conf(conf); |
| 5560 | free_conf(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5561 | mddev->private = NULL; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5562 | 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] | 5563 | return -EIO; |
| 5564 | } |
| 5565 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5566 | static int stop(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5567 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5568 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5569 | |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 5570 | md_unregister_thread(&mddev->thread); |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 5571 | if (mddev->queue) |
| 5572 | mddev->queue->backing_dev_info.congested_fn = NULL; |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 5573 | free_conf(conf); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 5574 | mddev->private = NULL; |
| 5575 | mddev->to_remove = &raid5_attrs_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5576 | return 0; |
| 5577 | } |
| 5578 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5579 | static void status(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5580 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5581 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5582 | int i; |
| 5583 | |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5584 | seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level, |
| 5585 | mddev->chunk_sectors / 2, mddev->layout); |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 5586 | 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] | 5587 | for (i = 0; i < conf->raid_disks; i++) |
| 5588 | seq_printf (seq, "%s", |
| 5589 | conf->disks[i].rdev && |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5590 | test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5591 | seq_printf (seq, "]"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5592 | } |
| 5593 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5594 | static void print_raid5_conf (struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5595 | { |
| 5596 | int i; |
| 5597 | struct disk_info *tmp; |
| 5598 | |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5599 | printk(KERN_DEBUG "RAID conf printout:\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5600 | if (!conf) { |
| 5601 | printk("(conf==NULL)\n"); |
| 5602 | return; |
| 5603 | } |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5604 | printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level, |
| 5605 | conf->raid_disks, |
| 5606 | conf->raid_disks - conf->mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5607 | |
| 5608 | for (i = 0; i < conf->raid_disks; i++) { |
| 5609 | char b[BDEVNAME_SIZE]; |
| 5610 | tmp = conf->disks + i; |
| 5611 | if (tmp->rdev) |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5612 | printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n", |
| 5613 | i, !test_bit(Faulty, &tmp->rdev->flags), |
| 5614 | bdevname(tmp->rdev->bdev, b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5615 | } |
| 5616 | } |
| 5617 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5618 | static int raid5_spare_active(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5619 | { |
| 5620 | int i; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5621 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5622 | struct disk_info *tmp; |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5623 | int count = 0; |
| 5624 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5625 | |
| 5626 | for (i = 0; i < conf->raid_disks; i++) { |
| 5627 | tmp = conf->disks + i; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5628 | if (tmp->replacement |
| 5629 | && tmp->replacement->recovery_offset == MaxSector |
| 5630 | && !test_bit(Faulty, &tmp->replacement->flags) |
| 5631 | && !test_and_set_bit(In_sync, &tmp->replacement->flags)) { |
| 5632 | /* Replacement has just become active. */ |
| 5633 | if (!tmp->rdev |
| 5634 | || !test_and_clear_bit(In_sync, &tmp->rdev->flags)) |
| 5635 | count++; |
| 5636 | if (tmp->rdev) { |
| 5637 | /* Replaced device not technically faulty, |
| 5638 | * but we need to be sure it gets removed |
| 5639 | * and never re-added. |
| 5640 | */ |
| 5641 | set_bit(Faulty, &tmp->rdev->flags); |
| 5642 | sysfs_notify_dirent_safe( |
| 5643 | tmp->rdev->sysfs_state); |
| 5644 | } |
| 5645 | sysfs_notify_dirent_safe(tmp->replacement->sysfs_state); |
| 5646 | } else if (tmp->rdev |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 5647 | && tmp->rdev->recovery_offset == MaxSector |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5648 | && !test_bit(Faulty, &tmp->rdev->flags) |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 5649 | && !test_and_set_bit(In_sync, &tmp->rdev->flags)) { |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5650 | count++; |
Jonathan Brassow | 43c73ca | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5651 | sysfs_notify_dirent_safe(tmp->rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5652 | } |
| 5653 | } |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5654 | spin_lock_irqsave(&conf->device_lock, flags); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 5655 | mddev->degraded = calc_degraded(conf); |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5656 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5657 | print_raid5_conf(conf); |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5658 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5659 | } |
| 5660 | |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 5661 | static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5662 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5663 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5664 | int err = 0; |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 5665 | int number = rdev->raid_disk; |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5666 | struct md_rdev **rdevp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5667 | struct disk_info *p = conf->disks + number; |
| 5668 | |
| 5669 | print_raid5_conf(conf); |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5670 | if (rdev == p->rdev) |
| 5671 | rdevp = &p->rdev; |
| 5672 | else if (rdev == p->replacement) |
| 5673 | rdevp = &p->replacement; |
| 5674 | else |
| 5675 | return 0; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5676 | |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5677 | if (number >= conf->raid_disks && |
| 5678 | conf->reshape_progress == MaxSector) |
| 5679 | clear_bit(In_sync, &rdev->flags); |
| 5680 | |
| 5681 | if (test_bit(In_sync, &rdev->flags) || |
| 5682 | atomic_read(&rdev->nr_pending)) { |
| 5683 | err = -EBUSY; |
| 5684 | goto abort; |
| 5685 | } |
| 5686 | /* Only remove non-faulty devices if recovery |
| 5687 | * isn't possible. |
| 5688 | */ |
| 5689 | if (!test_bit(Faulty, &rdev->flags) && |
| 5690 | mddev->recovery_disabled != conf->recovery_disabled && |
| 5691 | !has_failed(conf) && |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5692 | (!p->replacement || p->replacement == rdev) && |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5693 | number < conf->raid_disks) { |
| 5694 | err = -EBUSY; |
| 5695 | goto abort; |
| 5696 | } |
| 5697 | *rdevp = NULL; |
| 5698 | synchronize_rcu(); |
| 5699 | if (atomic_read(&rdev->nr_pending)) { |
| 5700 | /* lost the race, try later */ |
| 5701 | err = -EBUSY; |
| 5702 | *rdevp = rdev; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5703 | } else if (p->replacement) { |
| 5704 | /* We must have just cleared 'rdev' */ |
| 5705 | p->rdev = p->replacement; |
| 5706 | clear_bit(Replacement, &p->replacement->flags); |
| 5707 | smp_mb(); /* Make sure other CPUs may see both as identical |
| 5708 | * but will never see neither - if they are careful |
| 5709 | */ |
| 5710 | p->replacement = NULL; |
| 5711 | clear_bit(WantReplacement, &rdev->flags); |
| 5712 | } else |
| 5713 | /* We might have just removed the Replacement as faulty- |
| 5714 | * clear the bit just in case |
| 5715 | */ |
| 5716 | clear_bit(WantReplacement, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5717 | abort: |
| 5718 | |
| 5719 | print_raid5_conf(conf); |
| 5720 | return err; |
| 5721 | } |
| 5722 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5723 | static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5724 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5725 | struct r5conf *conf = mddev->private; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5726 | int err = -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5727 | int disk; |
| 5728 | struct disk_info *p; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5729 | int first = 0; |
| 5730 | int last = conf->raid_disks - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5731 | |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 5732 | if (mddev->recovery_disabled == conf->recovery_disabled) |
| 5733 | return -EBUSY; |
| 5734 | |
NeilBrown | dc10c64 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 5735 | if (rdev->saved_raid_disk < 0 && has_failed(conf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5736 | /* no point adding a device */ |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5737 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5738 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5739 | if (rdev->raid_disk >= 0) |
| 5740 | first = last = rdev->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5741 | |
| 5742 | /* |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5743 | * find the disk ... but prefer rdev->saved_raid_disk |
| 5744 | * if possible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5745 | */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5746 | if (rdev->saved_raid_disk >= 0 && |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5747 | rdev->saved_raid_disk >= first && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5748 | conf->disks[rdev->saved_raid_disk].rdev == NULL) |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 5749 | first = rdev->saved_raid_disk; |
| 5750 | |
| 5751 | for (disk = first; disk <= last; disk++) { |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5752 | p = conf->disks + disk; |
| 5753 | if (p->rdev == NULL) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5754 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5755 | rdev->raid_disk = disk; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5756 | err = 0; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5757 | if (rdev->saved_raid_disk != disk) |
| 5758 | conf->fullsync = 1; |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 5759 | rcu_assign_pointer(p->rdev, rdev); |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 5760 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5761 | } |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 5762 | } |
| 5763 | for (disk = first; disk <= last; disk++) { |
| 5764 | p = conf->disks + disk; |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5765 | if (test_bit(WantReplacement, &p->rdev->flags) && |
| 5766 | p->replacement == NULL) { |
| 5767 | clear_bit(In_sync, &rdev->flags); |
| 5768 | set_bit(Replacement, &rdev->flags); |
| 5769 | rdev->raid_disk = disk; |
| 5770 | err = 0; |
| 5771 | conf->fullsync = 1; |
| 5772 | rcu_assign_pointer(p->replacement, rdev); |
| 5773 | break; |
| 5774 | } |
| 5775 | } |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 5776 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5777 | print_raid5_conf(conf); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5778 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5779 | } |
| 5780 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5781 | static int raid5_resize(struct mddev *mddev, sector_t sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5782 | { |
| 5783 | /* no resync is happening, and there is enough space |
| 5784 | * on all devices, so we can resize. |
| 5785 | * We need to make sure resync covers any new space. |
| 5786 | * If the array is shrinking we should possibly wait until |
| 5787 | * any io in the removed space completes, but it hardly seems |
| 5788 | * worth it. |
| 5789 | */ |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 5790 | sector_t newsize; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5791 | sectors &= ~((sector_t)mddev->chunk_sectors - 1); |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 5792 | newsize = raid5_size(mddev, sectors, mddev->raid_disks); |
| 5793 | if (mddev->external_size && |
| 5794 | mddev->array_sectors > newsize) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5795 | return -EINVAL; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 5796 | if (mddev->bitmap) { |
| 5797 | int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0); |
| 5798 | if (ret) |
| 5799 | return ret; |
| 5800 | } |
| 5801 | md_set_array_sectors(mddev, newsize); |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 5802 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5803 | revalidate_disk(mddev->gendisk); |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 5804 | if (sectors > mddev->dev_sectors && |
| 5805 | mddev->recovery_cp > mddev->dev_sectors) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5806 | mddev->recovery_cp = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5807 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5808 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5809 | mddev->dev_sectors = sectors; |
NeilBrown | 4b5c7ae | 2005-07-27 11:43:28 -0700 | [diff] [blame] | 5810 | mddev->resync_max_sectors = sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5811 | return 0; |
| 5812 | } |
| 5813 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5814 | static int check_stripe_cache(struct mddev *mddev) |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5815 | { |
| 5816 | /* Can only proceed if there are plenty of stripe_heads. |
| 5817 | * We need a minimum of one full stripe,, and for sensible progress |
| 5818 | * it is best to have about 4 times that. |
| 5819 | * If we require 4 times, then the default 256 4K stripe_heads will |
| 5820 | * allow for chunk sizes up to 256K, which is probably OK. |
| 5821 | * If the chunk size is greater, user-space should request more |
| 5822 | * stripe_heads first. |
| 5823 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5824 | struct r5conf *conf = mddev->private; |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5825 | if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4 |
| 5826 | > conf->max_nr_stripes || |
| 5827 | ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4 |
| 5828 | > conf->max_nr_stripes) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5829 | printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n", |
| 5830 | mdname(mddev), |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5831 | ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9) |
| 5832 | / STRIPE_SIZE)*4); |
| 5833 | return 0; |
| 5834 | } |
| 5835 | return 1; |
| 5836 | } |
| 5837 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5838 | static int check_reshape(struct mddev *mddev) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5839 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5840 | struct r5conf *conf = mddev->private; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5841 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5842 | if (mddev->delta_disks == 0 && |
| 5843 | mddev->new_layout == mddev->layout && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5844 | mddev->new_chunk_sectors == mddev->chunk_sectors) |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5845 | return 0; /* nothing to do */ |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 5846 | if (has_failed(conf)) |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5847 | return -EINVAL; |
| 5848 | if (mddev->delta_disks < 0) { |
| 5849 | /* We might be able to shrink, but the devices must |
| 5850 | * be made bigger first. |
| 5851 | * For raid6, 4 is the minimum size. |
| 5852 | * Otherwise 2 is the minimum |
| 5853 | */ |
| 5854 | int min = 2; |
| 5855 | if (mddev->level == 6) |
| 5856 | min = 4; |
| 5857 | if (mddev->raid_disks + mddev->delta_disks < min) |
| 5858 | return -EINVAL; |
| 5859 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5860 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5861 | if (!check_stripe_cache(mddev)) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5862 | return -ENOSPC; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5863 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5864 | return resize_stripes(conf, conf->raid_disks + mddev->delta_disks); |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5865 | } |
| 5866 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5867 | static int raid5_start_reshape(struct mddev *mddev) |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5868 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5869 | struct r5conf *conf = mddev->private; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5870 | struct md_rdev *rdev; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5871 | int spares = 0; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 5872 | unsigned long flags; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5873 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5874 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5875 | return -EBUSY; |
| 5876 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5877 | if (!check_stripe_cache(mddev)) |
| 5878 | return -ENOSPC; |
| 5879 | |
NeilBrown | 30b6764 | 2012-05-22 13:55:28 +1000 | [diff] [blame] | 5880 | if (has_failed(conf)) |
| 5881 | return -EINVAL; |
| 5882 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5883 | rdev_for_each(rdev, mddev) { |
NeilBrown | 469518a | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5884 | if (!test_bit(In_sync, &rdev->flags) |
| 5885 | && !test_bit(Faulty, &rdev->flags)) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5886 | spares++; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5887 | } |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5888 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5889 | if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5890 | /* Not enough devices even to make a degraded array |
| 5891 | * of that size |
| 5892 | */ |
| 5893 | return -EINVAL; |
| 5894 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5895 | /* Refuse to reduce size of the array. Any reductions in |
| 5896 | * array size must be through explicit setting of array_size |
| 5897 | * attribute. |
| 5898 | */ |
| 5899 | if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks) |
| 5900 | < mddev->array_sectors) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5901 | printk(KERN_ERR "md/raid:%s: array size must be reduced " |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5902 | "before number of disks\n", mdname(mddev)); |
| 5903 | return -EINVAL; |
| 5904 | } |
| 5905 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5906 | atomic_set(&conf->reshape_stripes, 0); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5907 | spin_lock_irq(&conf->device_lock); |
| 5908 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5909 | conf->raid_disks += mddev->delta_disks; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5910 | conf->prev_chunk_sectors = conf->chunk_sectors; |
| 5911 | conf->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5912 | conf->prev_algo = conf->algorithm; |
| 5913 | conf->algorithm = mddev->new_layout; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5914 | conf->generation++; |
| 5915 | /* Code that selects data_offset needs to see the generation update |
| 5916 | * if reshape_progress has been set - so a memory barrier needed. |
| 5917 | */ |
| 5918 | smp_mb(); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5919 | if (mddev->reshape_backwards) |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5920 | conf->reshape_progress = raid5_size(mddev, 0, 0); |
| 5921 | else |
| 5922 | conf->reshape_progress = 0; |
| 5923 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5924 | spin_unlock_irq(&conf->device_lock); |
| 5925 | |
| 5926 | /* Add some new drives, as many as will fit. |
| 5927 | * We know there are enough to make the newly sized array work. |
NeilBrown | 3424bf6 | 2010-06-17 17:48:26 +1000 | [diff] [blame] | 5928 | * Don't add devices if we are reducing the number of |
| 5929 | * devices in the array. This is because it is not possible |
| 5930 | * to correctly record the "partially reconstructed" state of |
| 5931 | * such devices during the reshape and confusion could result. |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5932 | */ |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5933 | if (mddev->delta_disks >= 0) { |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5934 | rdev_for_each(rdev, mddev) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5935 | if (rdev->raid_disk < 0 && |
| 5936 | !test_bit(Faulty, &rdev->flags)) { |
| 5937 | if (raid5_add_disk(mddev, rdev) == 0) { |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5938 | if (rdev->raid_disk |
NeilBrown | 9d4c7d8 | 2012-03-13 11:21:21 +1100 | [diff] [blame] | 5939 | >= conf->previous_raid_disks) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5940 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 9d4c7d8 | 2012-03-13 11:21:21 +1100 | [diff] [blame] | 5941 | else |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5942 | rdev->recovery_offset = 0; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5943 | |
| 5944 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5945 | /* Failure here is OK */; |
NeilBrown | 50da084 | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5946 | } |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5947 | } else if (rdev->raid_disk >= conf->previous_raid_disks |
| 5948 | && !test_bit(Faulty, &rdev->flags)) { |
| 5949 | /* This is a spare that was manually added */ |
| 5950 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5951 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5952 | |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5953 | /* When a reshape changes the number of devices, |
| 5954 | * ->degraded is measured against the larger of the |
| 5955 | * pre and post number of devices. |
| 5956 | */ |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5957 | spin_lock_irqsave(&conf->device_lock, flags); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 5958 | mddev->degraded = calc_degraded(conf); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5959 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 5960 | } |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5961 | mddev->raid_disks = conf->raid_disks; |
NeilBrown | e516402 | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 5962 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5963 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5964 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5965 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 5966 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 5967 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 5968 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 5969 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 5970 | "reshape"); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5971 | if (!mddev->sync_thread) { |
| 5972 | mddev->recovery = 0; |
| 5973 | spin_lock_irq(&conf->device_lock); |
| 5974 | mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5975 | rdev_for_each(rdev, mddev) |
| 5976 | rdev->new_data_offset = rdev->data_offset; |
| 5977 | smp_wmb(); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5978 | conf->reshape_progress = MaxSector; |
NeilBrown | 1e3fa9b | 2012-03-13 11:21:18 +1100 | [diff] [blame] | 5979 | mddev->reshape_position = MaxSector; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5980 | spin_unlock_irq(&conf->device_lock); |
| 5981 | return -EAGAIN; |
| 5982 | } |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5983 | conf->reshape_checkpoint = jiffies; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5984 | md_wakeup_thread(mddev->sync_thread); |
| 5985 | md_new_event(mddev); |
| 5986 | return 0; |
| 5987 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5988 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5989 | /* This is called from the reshape thread and should make any |
| 5990 | * changes needed in 'conf' |
| 5991 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5992 | static void end_reshape(struct r5conf *conf) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5993 | { |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5994 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5995 | if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) { |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5996 | struct md_rdev *rdev; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 5997 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5998 | spin_lock_irq(&conf->device_lock); |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5999 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6000 | rdev_for_each(rdev, conf->mddev) |
| 6001 | rdev->data_offset = rdev->new_data_offset; |
| 6002 | smp_wmb(); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 6003 | conf->reshape_progress = MaxSector; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6004 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 6005 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6006 | |
| 6007 | /* read-ahead size must cover two whole stripes, which is |
| 6008 | * 2 * (datadisks) * chunksize where 'n' is the number of raid devices |
| 6009 | */ |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 6010 | if (conf->mddev->queue) { |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6011 | int data_disks = conf->raid_disks - conf->max_degraded; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 6012 | int stripe = data_disks * ((conf->chunk_sectors << 9) |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6013 | / PAGE_SIZE); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6014 | if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 6015 | conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
| 6016 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6017 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6018 | } |
| 6019 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 6020 | /* This is called from the raid5d thread with mddev_lock held. |
| 6021 | * It makes config changes to the device. |
| 6022 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6023 | static void raid5_finish_reshape(struct mddev *mddev) |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6024 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6025 | struct r5conf *conf = mddev->private; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6026 | |
| 6027 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6028 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 6029 | if (mddev->delta_disks > 0) { |
| 6030 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
| 6031 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6032 | revalidate_disk(mddev->gendisk); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 6033 | } else { |
| 6034 | int d; |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 6035 | spin_lock_irq(&conf->device_lock); |
| 6036 | mddev->degraded = calc_degraded(conf); |
| 6037 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 6038 | for (d = conf->raid_disks ; |
| 6039 | d < conf->raid_disks - mddev->delta_disks; |
NeilBrown | 1a67dde | 2009-08-13 10:41:49 +1000 | [diff] [blame] | 6040 | d++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6041 | struct md_rdev *rdev = conf->disks[d].rdev; |
NeilBrown | da7613b | 2012-05-22 13:55:33 +1000 | [diff] [blame] | 6042 | if (rdev) |
| 6043 | clear_bit(In_sync, &rdev->flags); |
| 6044 | rdev = conf->disks[d].replacement; |
| 6045 | if (rdev) |
| 6046 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 1a67dde | 2009-08-13 10:41:49 +1000 | [diff] [blame] | 6047 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6048 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6049 | mddev->layout = conf->algorithm; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 6050 | mddev->chunk_sectors = conf->chunk_sectors; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 6051 | mddev->reshape_position = MaxSector; |
| 6052 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6053 | mddev->reshape_backwards = 0; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6054 | } |
| 6055 | } |
| 6056 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6057 | static void raid5_quiesce(struct mddev *mddev, int state) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6058 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6059 | struct r5conf *conf = mddev->private; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6060 | |
| 6061 | switch(state) { |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 6062 | case 2: /* resume for a suspend */ |
| 6063 | wake_up(&conf->wait_for_overlap); |
| 6064 | break; |
| 6065 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6066 | case 1: /* stop all writes */ |
| 6067 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6068 | /* '2' tells resync/reshape to pause so that all |
| 6069 | * active stripes can drain |
| 6070 | */ |
| 6071 | conf->quiesce = 2; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6072 | wait_event_lock_irq(conf->wait_for_stripe, |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6073 | atomic_read(&conf->active_stripes) == 0 && |
| 6074 | atomic_read(&conf->active_aligned_reads) == 0, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6075 | conf->device_lock, /* nothing */); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6076 | conf->quiesce = 1; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6077 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6078 | /* allow reshape to continue */ |
| 6079 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6080 | break; |
| 6081 | |
| 6082 | case 0: /* re-enable writes */ |
| 6083 | spin_lock_irq(&conf->device_lock); |
| 6084 | conf->quiesce = 0; |
| 6085 | wake_up(&conf->wait_for_stripe); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 6086 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6087 | spin_unlock_irq(&conf->device_lock); |
| 6088 | break; |
| 6089 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6090 | } |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 6091 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6092 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6093 | static void *raid45_takeover_raid0(struct mddev *mddev, int level) |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 6094 | { |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 6095 | struct r0conf *raid0_conf = mddev->private; |
Randy Dunlap | d76c842 | 2011-04-21 09:07:26 -0700 | [diff] [blame] | 6096 | sector_t sectors; |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 6097 | |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6098 | /* for raid0 takeover only one zone is supported */ |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 6099 | if (raid0_conf->nr_strip_zones > 1) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 6100 | printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n", |
| 6101 | mdname(mddev)); |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6102 | return ERR_PTR(-EINVAL); |
| 6103 | } |
| 6104 | |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 6105 | sectors = raid0_conf->strip_zone[0].zone_end; |
| 6106 | sector_div(sectors, raid0_conf->strip_zone[0].nb_dev); |
NeilBrown | 3b71bd9 | 2011-04-20 15:38:18 +1000 | [diff] [blame] | 6107 | mddev->dev_sectors = sectors; |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6108 | mddev->new_level = level; |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 6109 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 6110 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
| 6111 | mddev->raid_disks += 1; |
| 6112 | mddev->delta_disks = 1; |
| 6113 | /* make sure it will be not marked as dirty */ |
| 6114 | mddev->recovery_cp = MaxSector; |
| 6115 | |
| 6116 | return setup_conf(mddev); |
| 6117 | } |
| 6118 | |
| 6119 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6120 | static void *raid5_takeover_raid1(struct mddev *mddev) |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6121 | { |
| 6122 | int chunksect; |
| 6123 | |
| 6124 | if (mddev->raid_disks != 2 || |
| 6125 | mddev->degraded > 1) |
| 6126 | return ERR_PTR(-EINVAL); |
| 6127 | |
| 6128 | /* Should check if there are write-behind devices? */ |
| 6129 | |
| 6130 | chunksect = 64*2; /* 64K by default */ |
| 6131 | |
| 6132 | /* The array must be an exact multiple of chunksize */ |
| 6133 | while (chunksect && (mddev->array_sectors & (chunksect-1))) |
| 6134 | chunksect >>= 1; |
| 6135 | |
| 6136 | if ((chunksect<<9) < STRIPE_SIZE) |
| 6137 | /* array size does not allow a suitable chunk size */ |
| 6138 | return ERR_PTR(-EINVAL); |
| 6139 | |
| 6140 | mddev->new_level = 5; |
| 6141 | mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 6142 | mddev->new_chunk_sectors = chunksect; |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6143 | |
| 6144 | return setup_conf(mddev); |
| 6145 | } |
| 6146 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6147 | static void *raid5_takeover_raid6(struct mddev *mddev) |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 6148 | { |
| 6149 | int new_layout; |
| 6150 | |
| 6151 | switch (mddev->layout) { |
| 6152 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 6153 | new_layout = ALGORITHM_LEFT_ASYMMETRIC; |
| 6154 | break; |
| 6155 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 6156 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC; |
| 6157 | break; |
| 6158 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 6159 | new_layout = ALGORITHM_LEFT_SYMMETRIC; |
| 6160 | break; |
| 6161 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 6162 | new_layout = ALGORITHM_RIGHT_SYMMETRIC; |
| 6163 | break; |
| 6164 | case ALGORITHM_PARITY_0_6: |
| 6165 | new_layout = ALGORITHM_PARITY_0; |
| 6166 | break; |
| 6167 | case ALGORITHM_PARITY_N: |
| 6168 | new_layout = ALGORITHM_PARITY_N; |
| 6169 | break; |
| 6170 | default: |
| 6171 | return ERR_PTR(-EINVAL); |
| 6172 | } |
| 6173 | mddev->new_level = 5; |
| 6174 | mddev->new_layout = new_layout; |
| 6175 | mddev->delta_disks = -1; |
| 6176 | mddev->raid_disks -= 1; |
| 6177 | return setup_conf(mddev); |
| 6178 | } |
| 6179 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6180 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6181 | static int raid5_check_reshape(struct mddev *mddev) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6182 | { |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6183 | /* For a 2-drive array, the layout and chunk size can be changed |
| 6184 | * immediately as not restriping is needed. |
| 6185 | * For larger arrays we record the new value - after validation |
| 6186 | * to be used by a reshape pass. |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6187 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6188 | struct r5conf *conf = mddev->private; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6189 | int new_chunk = mddev->new_chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6190 | |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6191 | if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6192 | return -EINVAL; |
| 6193 | if (new_chunk > 0) { |
Andre Noll | 0ba459d | 2009-06-18 08:46:10 +1000 | [diff] [blame] | 6194 | if (!is_power_of_2(new_chunk)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6195 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6196 | if (new_chunk < (PAGE_SIZE>>9)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6197 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6198 | if (mddev->array_sectors & (new_chunk-1)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6199 | /* not factor of array size */ |
| 6200 | return -EINVAL; |
| 6201 | } |
| 6202 | |
| 6203 | /* They look valid */ |
| 6204 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6205 | if (mddev->raid_disks == 2) { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6206 | /* can make the change immediately */ |
| 6207 | if (mddev->new_layout >= 0) { |
| 6208 | conf->algorithm = mddev->new_layout; |
| 6209 | mddev->layout = mddev->new_layout; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6210 | } |
| 6211 | if (new_chunk > 0) { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6212 | conf->chunk_sectors = new_chunk ; |
| 6213 | mddev->chunk_sectors = new_chunk; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6214 | } |
| 6215 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 6216 | md_wakeup_thread(mddev->thread); |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6217 | } |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6218 | return check_reshape(mddev); |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6219 | } |
| 6220 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6221 | static int raid6_check_reshape(struct mddev *mddev) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6222 | { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6223 | int new_chunk = mddev->new_chunk_sectors; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6224 | |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6225 | if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6226 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6227 | if (new_chunk > 0) { |
Andre Noll | 0ba459d | 2009-06-18 08:46:10 +1000 | [diff] [blame] | 6228 | if (!is_power_of_2(new_chunk)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6229 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6230 | if (new_chunk < (PAGE_SIZE >> 9)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6231 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6232 | if (mddev->array_sectors & (new_chunk-1)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6233 | /* not factor of array size */ |
| 6234 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6235 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6236 | |
| 6237 | /* They look valid */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6238 | return check_reshape(mddev); |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6239 | } |
| 6240 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6241 | static void *raid5_takeover(struct mddev *mddev) |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6242 | { |
| 6243 | /* raid5 can take over: |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6244 | * raid0 - if there is only one strip zone - make it a raid4 layout |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6245 | * raid1 - if there are two drives. We need to know the chunk size |
| 6246 | * raid4 - trivial - just use a raid4 layout. |
| 6247 | * raid6 - Providing it is a *_6 layout |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6248 | */ |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6249 | if (mddev->level == 0) |
| 6250 | return raid45_takeover_raid0(mddev, 5); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6251 | if (mddev->level == 1) |
| 6252 | return raid5_takeover_raid1(mddev); |
NeilBrown | e9d4758 | 2009-03-31 14:57:09 +1100 | [diff] [blame] | 6253 | if (mddev->level == 4) { |
| 6254 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 6255 | mddev->new_level = 5; |
| 6256 | return setup_conf(mddev); |
| 6257 | } |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 6258 | if (mddev->level == 6) |
| 6259 | return raid5_takeover_raid6(mddev); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6260 | |
| 6261 | return ERR_PTR(-EINVAL); |
| 6262 | } |
| 6263 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6264 | static void *raid4_takeover(struct mddev *mddev) |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 6265 | { |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6266 | /* raid4 can take over: |
| 6267 | * raid0 - if there is only one strip zone |
| 6268 | * raid5 - if layout is right |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 6269 | */ |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6270 | if (mddev->level == 0) |
| 6271 | return raid45_takeover_raid0(mddev, 4); |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 6272 | if (mddev->level == 5 && |
| 6273 | mddev->layout == ALGORITHM_PARITY_N) { |
| 6274 | mddev->new_layout = 0; |
| 6275 | mddev->new_level = 4; |
| 6276 | return setup_conf(mddev); |
| 6277 | } |
| 6278 | return ERR_PTR(-EINVAL); |
| 6279 | } |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6280 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6281 | static struct md_personality raid5_personality; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6282 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6283 | static void *raid6_takeover(struct mddev *mddev) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6284 | { |
| 6285 | /* Currently can only take over a raid5. We map the |
| 6286 | * personality to an equivalent raid6 personality |
| 6287 | * with the Q block at the end. |
| 6288 | */ |
| 6289 | int new_layout; |
| 6290 | |
| 6291 | if (mddev->pers != &raid5_personality) |
| 6292 | return ERR_PTR(-EINVAL); |
| 6293 | if (mddev->degraded > 1) |
| 6294 | return ERR_PTR(-EINVAL); |
| 6295 | if (mddev->raid_disks > 253) |
| 6296 | return ERR_PTR(-EINVAL); |
| 6297 | if (mddev->raid_disks < 3) |
| 6298 | return ERR_PTR(-EINVAL); |
| 6299 | |
| 6300 | switch (mddev->layout) { |
| 6301 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 6302 | new_layout = ALGORITHM_LEFT_ASYMMETRIC_6; |
| 6303 | break; |
| 6304 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 6305 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6; |
| 6306 | break; |
| 6307 | case ALGORITHM_LEFT_SYMMETRIC: |
| 6308 | new_layout = ALGORITHM_LEFT_SYMMETRIC_6; |
| 6309 | break; |
| 6310 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 6311 | new_layout = ALGORITHM_RIGHT_SYMMETRIC_6; |
| 6312 | break; |
| 6313 | case ALGORITHM_PARITY_0: |
| 6314 | new_layout = ALGORITHM_PARITY_0_6; |
| 6315 | break; |
| 6316 | case ALGORITHM_PARITY_N: |
| 6317 | new_layout = ALGORITHM_PARITY_N; |
| 6318 | break; |
| 6319 | default: |
| 6320 | return ERR_PTR(-EINVAL); |
| 6321 | } |
| 6322 | mddev->new_level = 6; |
| 6323 | mddev->new_layout = new_layout; |
| 6324 | mddev->delta_disks = 1; |
| 6325 | mddev->raid_disks += 1; |
| 6326 | return setup_conf(mddev); |
| 6327 | } |
| 6328 | |
| 6329 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6330 | static struct md_personality raid6_personality = |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6331 | { |
| 6332 | .name = "raid6", |
| 6333 | .level = 6, |
| 6334 | .owner = THIS_MODULE, |
| 6335 | .make_request = make_request, |
| 6336 | .run = run, |
| 6337 | .stop = stop, |
| 6338 | .status = status, |
| 6339 | .error_handler = error, |
| 6340 | .hot_add_disk = raid5_add_disk, |
| 6341 | .hot_remove_disk= raid5_remove_disk, |
| 6342 | .spare_active = raid5_spare_active, |
| 6343 | .sync_request = sync_request, |
| 6344 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6345 | .size = raid5_size, |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6346 | .check_reshape = raid6_check_reshape, |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 6347 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6348 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6349 | .quiesce = raid5_quiesce, |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6350 | .takeover = raid6_takeover, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6351 | }; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6352 | static struct md_personality raid5_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6353 | { |
| 6354 | .name = "raid5", |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6355 | .level = 5, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6356 | .owner = THIS_MODULE, |
| 6357 | .make_request = make_request, |
| 6358 | .run = run, |
| 6359 | .stop = stop, |
| 6360 | .status = status, |
| 6361 | .error_handler = error, |
| 6362 | .hot_add_disk = raid5_add_disk, |
| 6363 | .hot_remove_disk= raid5_remove_disk, |
| 6364 | .spare_active = raid5_spare_active, |
| 6365 | .sync_request = sync_request, |
| 6366 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6367 | .size = raid5_size, |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6368 | .check_reshape = raid5_check_reshape, |
| 6369 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6370 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6371 | .quiesce = raid5_quiesce, |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6372 | .takeover = raid5_takeover, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6373 | }; |
| 6374 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6375 | static struct md_personality raid4_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6376 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6377 | .name = "raid4", |
| 6378 | .level = 4, |
| 6379 | .owner = THIS_MODULE, |
| 6380 | .make_request = make_request, |
| 6381 | .run = run, |
| 6382 | .stop = stop, |
| 6383 | .status = status, |
| 6384 | .error_handler = error, |
| 6385 | .hot_add_disk = raid5_add_disk, |
| 6386 | .hot_remove_disk= raid5_remove_disk, |
| 6387 | .spare_active = raid5_spare_active, |
| 6388 | .sync_request = sync_request, |
| 6389 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6390 | .size = raid5_size, |
NeilBrown | 3d37890 | 2007-03-26 21:32:13 -0800 | [diff] [blame] | 6391 | .check_reshape = raid5_check_reshape, |
| 6392 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6393 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6394 | .quiesce = raid5_quiesce, |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 6395 | .takeover = raid4_takeover, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6396 | }; |
| 6397 | |
| 6398 | static int __init raid5_init(void) |
| 6399 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6400 | register_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6401 | register_md_personality(&raid5_personality); |
| 6402 | register_md_personality(&raid4_personality); |
| 6403 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6404 | } |
| 6405 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6406 | static void raid5_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6407 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6408 | unregister_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6409 | unregister_md_personality(&raid5_personality); |
| 6410 | unregister_md_personality(&raid4_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6411 | } |
| 6412 | |
| 6413 | module_init(raid5_init); |
| 6414 | module_exit(raid5_exit); |
| 6415 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 6416 | MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6417 | MODULE_ALIAS("md-personality-4"); /* RAID5 */ |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 6418 | MODULE_ALIAS("md-raid5"); |
| 6419 | MODULE_ALIAS("md-raid4"); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6420 | MODULE_ALIAS("md-level-5"); |
| 6421 | MODULE_ALIAS("md-level-4"); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6422 | MODULE_ALIAS("md-personality-8"); /* RAID6 */ |
| 6423 | MODULE_ALIAS("md-raid6"); |
| 6424 | MODULE_ALIAS("md-level-6"); |
| 6425 | |
| 6426 | /* This used to be two separate modules, they were: */ |
| 6427 | MODULE_ALIAS("raid5"); |
| 6428 | MODULE_ALIAS("raid6"); |