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 | */ |
| 102 | static inline int raid5_bi_phys_segments(struct bio *bio) |
| 103 | { |
Jens Axboe | 5b99c2f | 2008-08-15 10:56:11 +0200 | [diff] [blame] | 104 | return bio->bi_phys_segments & 0xffff; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static inline int raid5_bi_hw_segments(struct bio *bio) |
| 108 | { |
Jens Axboe | 5b99c2f | 2008-08-15 10:56:11 +0200 | [diff] [blame] | 109 | return (bio->bi_phys_segments >> 16) & 0xffff; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static inline int raid5_dec_bi_phys_segments(struct bio *bio) |
| 113 | { |
| 114 | --bio->bi_phys_segments; |
| 115 | return raid5_bi_phys_segments(bio); |
| 116 | } |
| 117 | |
| 118 | static inline int raid5_dec_bi_hw_segments(struct bio *bio) |
| 119 | { |
| 120 | unsigned short val = raid5_bi_hw_segments(bio); |
| 121 | |
| 122 | --val; |
Jens Axboe | 5b99c2f | 2008-08-15 10:56:11 +0200 | [diff] [blame] | 123 | bio->bi_phys_segments = (val << 16) | raid5_bi_phys_segments(bio); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 124 | return val; |
| 125 | } |
| 126 | |
| 127 | static inline void raid5_set_bi_hw_segments(struct bio *bio, unsigned int cnt) |
| 128 | { |
Namhyung Kim | 9b2dc8b | 2011-06-13 14:48:22 +0900 | [diff] [blame] | 129 | bio->bi_phys_segments = raid5_bi_phys_segments(bio) | (cnt << 16); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 130 | } |
| 131 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 132 | /* Find first data disk in a raid6 stripe */ |
| 133 | static inline int raid6_d0(struct stripe_head *sh) |
| 134 | { |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 135 | if (sh->ddf_layout) |
| 136 | /* ddf always start from first device */ |
| 137 | return 0; |
| 138 | /* md starts just after Q block */ |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 139 | if (sh->qd_idx == sh->disks - 1) |
| 140 | return 0; |
| 141 | else |
| 142 | return sh->qd_idx + 1; |
| 143 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 144 | static inline int raid6_next_disk(int disk, int raid_disks) |
| 145 | { |
| 146 | disk++; |
| 147 | return (disk < raid_disks) ? disk : 0; |
| 148 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 149 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 150 | /* When walking through the disks in a raid5, starting at raid6_d0, |
| 151 | * We need to map each disk to a 'slot', where the data disks are slot |
| 152 | * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk |
| 153 | * is raid_disks-1. This help does that mapping. |
| 154 | */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 155 | static int raid6_idx_to_slot(int idx, struct stripe_head *sh, |
| 156 | int *count, int syndrome_disks) |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 157 | { |
Dan Williams | 6629542 | 2009-10-19 18:09:32 -0700 | [diff] [blame] | 158 | int slot = *count; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 159 | |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 160 | if (sh->ddf_layout) |
Dan Williams | 6629542 | 2009-10-19 18:09:32 -0700 | [diff] [blame] | 161 | (*count)++; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 162 | if (idx == sh->pd_idx) |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 163 | return syndrome_disks; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 164 | if (idx == sh->qd_idx) |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 165 | return syndrome_disks + 1; |
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 | return slot; |
| 169 | } |
| 170 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 171 | static void return_io(struct bio *return_bi) |
| 172 | { |
| 173 | struct bio *bi = return_bi; |
| 174 | while (bi) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 175 | |
| 176 | return_bi = bi->bi_next; |
| 177 | bi->bi_next = NULL; |
| 178 | bi->bi_size = 0; |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 179 | bio_endio(bi, 0); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 180 | bi = return_bi; |
| 181 | } |
| 182 | } |
| 183 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 184 | static void print_raid5_conf (struct r5conf *conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 186 | static int stripe_operations_active(struct stripe_head *sh) |
| 187 | { |
| 188 | return sh->check_state || sh->reconstruct_state || |
| 189 | test_bit(STRIPE_BIOFILL_RUN, &sh->state) || |
| 190 | test_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 191 | } |
| 192 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 193 | static void __release_stripe(struct r5conf *conf, struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | { |
| 195 | if (atomic_dec_and_test(&sh->count)) { |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 196 | BUG_ON(!list_empty(&sh->lru)); |
| 197 | BUG_ON(atomic_read(&conf->active_stripes)==0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | if (test_bit(STRIPE_HANDLE, &sh->state)) { |
Shaohua Li | fab363b | 2012-07-03 15:57:19 +1000 | [diff] [blame] | 199 | if (test_bit(STRIPE_DELAYED, &sh->state) && |
| 200 | !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | list_add_tail(&sh->lru, &conf->delayed_list); |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 202 | else if (test_bit(STRIPE_BIT_DELAY, &sh->state) && |
| 203 | sh->bm_seq - conf->seq_write > 0) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 204 | list_add_tail(&sh->lru, &conf->bitmap_list); |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 205 | else { |
Shaohua Li | fab363b | 2012-07-03 15:57:19 +1000 | [diff] [blame] | 206 | clear_bit(STRIPE_DELAYED, &sh->state); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 207 | clear_bit(STRIPE_BIT_DELAY, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | list_add_tail(&sh->lru, &conf->handle_list); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 209 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | md_wakeup_thread(conf->mddev->thread); |
| 211 | } else { |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 212 | BUG_ON(stripe_operations_active(sh)); |
majianpeng | 41fe75f | 2012-03-13 11:21:25 +1100 | [diff] [blame] | 213 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 214 | if (atomic_dec_return(&conf->preread_active_stripes) |
| 215 | < IO_THRESHOLD) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | md_wakeup_thread(conf->mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | atomic_dec(&conf->active_stripes); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 218 | if (!test_bit(STRIPE_EXPANDING, &sh->state)) { |
| 219 | list_add_tail(&sh->lru, &conf->inactive_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | wake_up(&conf->wait_for_stripe); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 221 | if (conf->retry_read_aligned) |
| 222 | md_wakeup_thread(conf->mddev->thread); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 223 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | } |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | static void release_stripe(struct stripe_head *sh) |
| 229 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 230 | struct r5conf *conf = sh->raid_conf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | unsigned long flags; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | spin_lock_irqsave(&conf->device_lock, flags); |
| 234 | __release_stripe(conf, sh); |
| 235 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 236 | } |
| 237 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 238 | static inline void remove_hash(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 240 | pr_debug("remove_hash(), stripe %llu\n", |
| 241 | (unsigned long long)sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 243 | hlist_del_init(&sh->hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | } |
| 245 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 246 | static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | { |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 248 | struct hlist_head *hp = stripe_hash(conf, sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 250 | pr_debug("insert_hash(), stripe %llu\n", |
| 251 | (unsigned long long)sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 253 | hlist_add_head(&sh->hash, hp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | |
| 257 | /* find an idle stripe, make sure it is unhashed, and return it. */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 258 | static struct stripe_head *get_free_stripe(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | { |
| 260 | struct stripe_head *sh = NULL; |
| 261 | struct list_head *first; |
| 262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | if (list_empty(&conf->inactive_list)) |
| 264 | goto out; |
| 265 | first = conf->inactive_list.next; |
| 266 | sh = list_entry(first, struct stripe_head, lru); |
| 267 | list_del_init(first); |
| 268 | remove_hash(sh); |
| 269 | atomic_inc(&conf->active_stripes); |
| 270 | out: |
| 271 | return sh; |
| 272 | } |
| 273 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 274 | static void shrink_buffers(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | { |
| 276 | struct page *p; |
| 277 | int i; |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 278 | int num = sh->raid_conf->pool_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 280 | for (i = 0; i < num ; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | p = sh->dev[i].page; |
| 282 | if (!p) |
| 283 | continue; |
| 284 | sh->dev[i].page = NULL; |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 285 | put_page(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 289 | static int grow_buffers(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | { |
| 291 | int i; |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 292 | int num = sh->raid_conf->pool_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 294 | for (i = 0; i < num; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | struct page *page; |
| 296 | |
| 297 | if (!(page = alloc_page(GFP_KERNEL))) { |
| 298 | return 1; |
| 299 | } |
| 300 | sh->dev[i].page = page; |
| 301 | } |
| 302 | return 0; |
| 303 | } |
| 304 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 305 | static void raid5_build_block(struct stripe_head *sh, int i, int previous); |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 306 | static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 307 | struct stripe_head *sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 309 | 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] | 310 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 311 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 312 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 314 | BUG_ON(atomic_read(&sh->count) != 0); |
| 315 | BUG_ON(test_bit(STRIPE_HANDLE, &sh->state)); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 316 | BUG_ON(stripe_operations_active(sh)); |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 317 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 318 | pr_debug("init_stripe called, stripe %llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | (unsigned long long)sh->sector); |
| 320 | |
| 321 | remove_hash(sh); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 322 | |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 323 | sh->generation = conf->generation - previous; |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 324 | sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | sh->sector = sector; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 326 | stripe_set_idx(sector, conf, previous, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | sh->state = 0; |
| 328 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 329 | |
| 330 | for (i = sh->disks; i--; ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | struct r5dev *dev = &sh->dev[i]; |
| 332 | |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 333 | if (dev->toread || dev->read || dev->towrite || dev->written || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | test_bit(R5_LOCKED, &dev->flags)) { |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 335 | 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] | 336 | (unsigned long long)sh->sector, i, dev->toread, |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 337 | dev->read, dev->towrite, dev->written, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | test_bit(R5_LOCKED, &dev->flags)); |
NeilBrown | 8cfa7b0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 339 | WARN_ON(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | } |
| 341 | dev->flags = 0; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 342 | raid5_build_block(sh, i, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | } |
| 344 | insert_hash(conf, sh); |
| 345 | } |
| 346 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 347 | static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector, |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 348 | short generation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | { |
| 350 | struct stripe_head *sh; |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 351 | struct hlist_node *hn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 353 | pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector); |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 354 | hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash) |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 355 | if (sh->sector == sector && sh->generation == generation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | return sh; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 357 | pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | return NULL; |
| 359 | } |
| 360 | |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 361 | /* |
| 362 | * Need to check if array has failed when deciding whether to: |
| 363 | * - start an array |
| 364 | * - remove non-faulty devices |
| 365 | * - add a spare |
| 366 | * - allow a reshape |
| 367 | * This determination is simple when no reshape is happening. |
| 368 | * However if there is a reshape, we need to carefully check |
| 369 | * both the before and after sections. |
| 370 | * This is because some failed devices may only affect one |
| 371 | * of the two sections, and some non-in_sync devices may |
| 372 | * be insync in the section most affected by failed devices. |
| 373 | */ |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 374 | static int calc_degraded(struct r5conf *conf) |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 375 | { |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 376 | int degraded, degraded2; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 377 | int i; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 378 | |
| 379 | rcu_read_lock(); |
| 380 | degraded = 0; |
| 381 | for (i = 0; i < conf->previous_raid_disks; i++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 382 | struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 383 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
| 384 | degraded++; |
| 385 | else if (test_bit(In_sync, &rdev->flags)) |
| 386 | ; |
| 387 | else |
| 388 | /* not in-sync or faulty. |
| 389 | * If the reshape increases the number of devices, |
| 390 | * this is being recovered by the reshape, so |
| 391 | * this 'previous' section is not in_sync. |
| 392 | * If the number of devices is being reduced however, |
| 393 | * the device can only be part of the array if |
| 394 | * we are reverting a reshape, so this section will |
| 395 | * be in-sync. |
| 396 | */ |
| 397 | if (conf->raid_disks >= conf->previous_raid_disks) |
| 398 | degraded++; |
| 399 | } |
| 400 | rcu_read_unlock(); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 401 | if (conf->raid_disks == conf->previous_raid_disks) |
| 402 | return degraded; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 403 | rcu_read_lock(); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 404 | degraded2 = 0; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 405 | for (i = 0; i < conf->raid_disks; i++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 406 | struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 407 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 408 | degraded2++; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 409 | else if (test_bit(In_sync, &rdev->flags)) |
| 410 | ; |
| 411 | else |
| 412 | /* not in-sync or faulty. |
| 413 | * If reshape increases the number of devices, this |
| 414 | * section has already been recovered, else it |
| 415 | * almost certainly hasn't. |
| 416 | */ |
| 417 | if (conf->raid_disks <= conf->previous_raid_disks) |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 418 | degraded2++; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 419 | } |
| 420 | rcu_read_unlock(); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 421 | if (degraded2 > degraded) |
| 422 | return degraded2; |
| 423 | return degraded; |
| 424 | } |
| 425 | |
| 426 | static int has_failed(struct r5conf *conf) |
| 427 | { |
| 428 | int degraded; |
| 429 | |
| 430 | if (conf->mddev->reshape_position == MaxSector) |
| 431 | return conf->mddev->degraded > conf->max_degraded; |
| 432 | |
| 433 | degraded = calc_degraded(conf); |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 434 | if (degraded > conf->max_degraded) |
| 435 | return 1; |
| 436 | return 0; |
| 437 | } |
| 438 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 439 | static struct stripe_head * |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 440 | get_active_stripe(struct r5conf *conf, sector_t sector, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 441 | int previous, int noblock, int noquiesce) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | { |
| 443 | struct stripe_head *sh; |
| 444 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 445 | pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | |
| 447 | spin_lock_irq(&conf->device_lock); |
| 448 | |
| 449 | do { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 450 | wait_event_lock_irq(conf->wait_for_stripe, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 451 | conf->quiesce == 0 || noquiesce, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 452 | conf->device_lock, /* nothing */); |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 453 | sh = __find_stripe(conf, sector, conf->generation - previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | if (!sh) { |
| 455 | if (!conf->inactive_blocked) |
| 456 | sh = get_free_stripe(conf); |
| 457 | if (noblock && sh == NULL) |
| 458 | break; |
| 459 | if (!sh) { |
| 460 | conf->inactive_blocked = 1; |
| 461 | wait_event_lock_irq(conf->wait_for_stripe, |
| 462 | !list_empty(&conf->inactive_list) && |
NeilBrown | 5036805 | 2005-12-12 02:39:17 -0800 | [diff] [blame] | 463 | (atomic_read(&conf->active_stripes) |
| 464 | < (conf->max_nr_stripes *3/4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | || !conf->inactive_blocked), |
| 466 | conf->device_lock, |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 467 | ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | conf->inactive_blocked = 0; |
| 469 | } else |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 470 | init_stripe(sh, sector, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | } else { |
| 472 | if (atomic_read(&sh->count)) { |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 473 | BUG_ON(!list_empty(&sh->lru) |
| 474 | && !test_bit(STRIPE_EXPANDING, &sh->state)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | } else { |
| 476 | if (!test_bit(STRIPE_HANDLE, &sh->state)) |
| 477 | atomic_inc(&conf->active_stripes); |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 478 | if (list_empty(&sh->lru) && |
| 479 | !test_bit(STRIPE_EXPANDING, &sh->state)) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 480 | BUG(); |
| 481 | list_del_init(&sh->lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | } while (sh == NULL); |
| 485 | |
| 486 | if (sh) |
| 487 | atomic_inc(&sh->count); |
| 488 | |
| 489 | spin_unlock_irq(&conf->device_lock); |
| 490 | return sh; |
| 491 | } |
| 492 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 493 | /* Determine if 'data_offset' or 'new_data_offset' should be used |
| 494 | * in this stripe_head. |
| 495 | */ |
| 496 | static int use_new_offset(struct r5conf *conf, struct stripe_head *sh) |
| 497 | { |
| 498 | sector_t progress = conf->reshape_progress; |
| 499 | /* Need a memory barrier to make sure we see the value |
| 500 | * of conf->generation, or ->data_offset that was set before |
| 501 | * reshape_progress was updated. |
| 502 | */ |
| 503 | smp_rmb(); |
| 504 | if (progress == MaxSector) |
| 505 | return 0; |
| 506 | if (sh->generation == conf->generation - 1) |
| 507 | return 0; |
| 508 | /* We are in a reshape, and this is a new-generation stripe, |
| 509 | * so use new_data_offset. |
| 510 | */ |
| 511 | return 1; |
| 512 | } |
| 513 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 514 | static void |
| 515 | raid5_end_read_request(struct bio *bi, int error); |
| 516 | static void |
| 517 | raid5_end_write_request(struct bio *bi, int error); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 518 | |
Dan Williams | c4e5ac0 | 2008-06-28 08:31:53 +1000 | [diff] [blame] | 519 | 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] | 520 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 521 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 522 | int i, disks = sh->disks; |
| 523 | |
| 524 | might_sleep(); |
| 525 | |
| 526 | for (i = disks; i--; ) { |
| 527 | int rw; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 528 | int replace_only = 0; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 529 | struct bio *bi, *rbi; |
| 530 | struct md_rdev *rdev, *rrdev = NULL; |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 531 | if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) { |
| 532 | if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags)) |
| 533 | rw = WRITE_FUA; |
| 534 | else |
| 535 | rw = WRITE; |
| 536 | } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 537 | rw = READ; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 538 | else if (test_and_clear_bit(R5_WantReplace, |
| 539 | &sh->dev[i].flags)) { |
| 540 | rw = WRITE; |
| 541 | replace_only = 1; |
| 542 | } else |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 543 | continue; |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 544 | if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags)) |
| 545 | rw |= REQ_SYNC; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 546 | |
| 547 | bi = &sh->dev[i].req; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 548 | rbi = &sh->dev[i].rreq; /* For writing to replacement */ |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 549 | |
| 550 | bi->bi_rw = rw; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 551 | rbi->bi_rw = rw; |
| 552 | if (rw & WRITE) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 553 | bi->bi_end_io = raid5_end_write_request; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 554 | rbi->bi_end_io = raid5_end_write_request; |
| 555 | } else |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 556 | bi->bi_end_io = raid5_end_read_request; |
| 557 | |
| 558 | rcu_read_lock(); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 559 | rrdev = rcu_dereference(conf->disks[i].replacement); |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 560 | smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */ |
| 561 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 562 | if (!rdev) { |
| 563 | rdev = rrdev; |
| 564 | rrdev = NULL; |
| 565 | } |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 566 | if (rw & WRITE) { |
| 567 | if (replace_only) |
| 568 | rdev = NULL; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 569 | if (rdev == rrdev) |
| 570 | /* We raced and saw duplicates */ |
| 571 | rrdev = NULL; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 572 | } else { |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 573 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 574 | rdev = rrdev; |
| 575 | rrdev = NULL; |
| 576 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 577 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 578 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 579 | rdev = NULL; |
| 580 | if (rdev) |
| 581 | atomic_inc(&rdev->nr_pending); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 582 | if (rrdev && test_bit(Faulty, &rrdev->flags)) |
| 583 | rrdev = NULL; |
| 584 | if (rrdev) |
| 585 | atomic_inc(&rrdev->nr_pending); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 586 | rcu_read_unlock(); |
| 587 | |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 588 | /* We have already checked bad blocks for reads. Now |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 589 | * need to check for writes. We never accept write errors |
| 590 | * on the replacement, so we don't to check rrdev. |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 591 | */ |
| 592 | while ((rw & WRITE) && rdev && |
| 593 | test_bit(WriteErrorSeen, &rdev->flags)) { |
| 594 | sector_t first_bad; |
| 595 | int bad_sectors; |
| 596 | int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 597 | &first_bad, &bad_sectors); |
| 598 | if (!bad) |
| 599 | break; |
| 600 | |
| 601 | if (bad < 0) { |
| 602 | set_bit(BlockedBadBlocks, &rdev->flags); |
| 603 | if (!conf->mddev->external && |
| 604 | conf->mddev->flags) { |
| 605 | /* It is very unlikely, but we might |
| 606 | * still need to write out the |
| 607 | * bad block log - better give it |
| 608 | * a chance*/ |
| 609 | md_check_recovery(conf->mddev); |
| 610 | } |
majianpeng | 1850753 | 2012-07-03 12:11:54 +1000 | [diff] [blame] | 611 | /* |
| 612 | * Because md_wait_for_blocked_rdev |
| 613 | * will dec nr_pending, we must |
| 614 | * increment it first. |
| 615 | */ |
| 616 | atomic_inc(&rdev->nr_pending); |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 617 | md_wait_for_blocked_rdev(rdev, conf->mddev); |
| 618 | } else { |
| 619 | /* Acknowledged bad block - skip the write */ |
| 620 | rdev_dec_pending(rdev, conf->mddev); |
| 621 | rdev = NULL; |
| 622 | } |
| 623 | } |
| 624 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 625 | if (rdev) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 626 | if (s->syncing || s->expanding || s->expanded |
| 627 | || s->replacing) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 628 | md_sync_acct(rdev->bdev, STRIPE_SECTORS); |
| 629 | |
Dan Williams | 2b7497f | 2008-06-28 08:31:52 +1000 | [diff] [blame] | 630 | set_bit(STRIPE_IO_STARTED, &sh->state); |
| 631 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 632 | bi->bi_bdev = rdev->bdev; |
| 633 | pr_debug("%s: for %llu schedule op %ld on disc %d\n", |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 634 | __func__, (unsigned long long)sh->sector, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 635 | bi->bi_rw, i); |
| 636 | atomic_inc(&sh->count); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 637 | if (use_new_offset(conf, sh)) |
| 638 | bi->bi_sector = (sh->sector |
| 639 | + rdev->new_data_offset); |
| 640 | else |
| 641 | bi->bi_sector = (sh->sector |
| 642 | + rdev->data_offset); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 643 | bi->bi_flags = 1 << BIO_UPTODATE; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 644 | bi->bi_idx = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 645 | bi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
| 646 | bi->bi_io_vec[0].bv_offset = 0; |
| 647 | bi->bi_size = STRIPE_SIZE; |
| 648 | bi->bi_next = NULL; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 649 | if (rrdev) |
| 650 | set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 651 | generic_make_request(bi); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 652 | } |
| 653 | if (rrdev) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 654 | if (s->syncing || s->expanding || s->expanded |
| 655 | || s->replacing) |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 656 | md_sync_acct(rrdev->bdev, STRIPE_SECTORS); |
| 657 | |
| 658 | set_bit(STRIPE_IO_STARTED, &sh->state); |
| 659 | |
| 660 | rbi->bi_bdev = rrdev->bdev; |
| 661 | pr_debug("%s: for %llu schedule op %ld on " |
| 662 | "replacement disc %d\n", |
| 663 | __func__, (unsigned long long)sh->sector, |
| 664 | rbi->bi_rw, i); |
| 665 | atomic_inc(&sh->count); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 666 | if (use_new_offset(conf, sh)) |
| 667 | rbi->bi_sector = (sh->sector |
| 668 | + rrdev->new_data_offset); |
| 669 | else |
| 670 | rbi->bi_sector = (sh->sector |
| 671 | + rrdev->data_offset); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 672 | rbi->bi_flags = 1 << BIO_UPTODATE; |
| 673 | rbi->bi_idx = 0; |
| 674 | rbi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
| 675 | rbi->bi_io_vec[0].bv_offset = 0; |
| 676 | rbi->bi_size = STRIPE_SIZE; |
| 677 | rbi->bi_next = NULL; |
| 678 | generic_make_request(rbi); |
| 679 | } |
| 680 | if (!rdev && !rrdev) { |
Namhyung Kim | b062962 | 2011-06-14 14:20:19 +1000 | [diff] [blame] | 681 | if (rw & WRITE) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 682 | set_bit(STRIPE_DEGRADED, &sh->state); |
| 683 | pr_debug("skip op %ld on disc %d for sector %llu\n", |
| 684 | bi->bi_rw, i, (unsigned long long)sh->sector); |
| 685 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 686 | set_bit(STRIPE_HANDLE, &sh->state); |
| 687 | } |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | static struct dma_async_tx_descriptor * |
| 692 | async_copy_data(int frombio, struct bio *bio, struct page *page, |
| 693 | sector_t sector, struct dma_async_tx_descriptor *tx) |
| 694 | { |
| 695 | struct bio_vec *bvl; |
| 696 | struct page *bio_page; |
| 697 | int i; |
| 698 | int page_offset; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 699 | struct async_submit_ctl submit; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 700 | enum async_tx_flags flags = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 701 | |
| 702 | if (bio->bi_sector >= sector) |
| 703 | page_offset = (signed)(bio->bi_sector - sector) * 512; |
| 704 | else |
| 705 | page_offset = (signed)(sector - bio->bi_sector) * -512; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 706 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 707 | if (frombio) |
| 708 | flags |= ASYNC_TX_FENCE; |
| 709 | init_async_submit(&submit, flags, tx, NULL, NULL, NULL); |
| 710 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 711 | bio_for_each_segment(bvl, bio, i) { |
Namhyung Kim | fcde907 | 2011-06-14 14:23:57 +1000 | [diff] [blame] | 712 | int len = bvl->bv_len; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 713 | int clen; |
| 714 | int b_offset = 0; |
| 715 | |
| 716 | if (page_offset < 0) { |
| 717 | b_offset = -page_offset; |
| 718 | page_offset += b_offset; |
| 719 | len -= b_offset; |
| 720 | } |
| 721 | |
| 722 | if (len > 0 && page_offset + len > STRIPE_SIZE) |
| 723 | clen = STRIPE_SIZE - page_offset; |
| 724 | else |
| 725 | clen = len; |
| 726 | |
| 727 | if (clen > 0) { |
Namhyung Kim | fcde907 | 2011-06-14 14:23:57 +1000 | [diff] [blame] | 728 | b_offset += bvl->bv_offset; |
| 729 | bio_page = bvl->bv_page; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 730 | if (frombio) |
| 731 | tx = async_memcpy(page, bio_page, page_offset, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 732 | b_offset, clen, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 733 | else |
| 734 | tx = async_memcpy(bio_page, page, b_offset, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 735 | page_offset, clen, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 736 | } |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 737 | /* chain the operations */ |
| 738 | submit.depend_tx = tx; |
| 739 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 740 | if (clen < len) /* hit end of page */ |
| 741 | break; |
| 742 | page_offset += len; |
| 743 | } |
| 744 | |
| 745 | return tx; |
| 746 | } |
| 747 | |
| 748 | static void ops_complete_biofill(void *stripe_head_ref) |
| 749 | { |
| 750 | struct stripe_head *sh = stripe_head_ref; |
| 751 | struct bio *return_bi = NULL; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 752 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 753 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 754 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 755 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 756 | (unsigned long long)sh->sector); |
| 757 | |
| 758 | /* clear completed biofills */ |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 759 | spin_lock_irq(&conf->device_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 760 | for (i = sh->disks; i--; ) { |
| 761 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 762 | |
| 763 | /* acknowledge completion of a biofill operation */ |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 764 | /* and check if we need to reply to a read request, |
| 765 | * new R5_Wantfill requests are held off until |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 766 | * !STRIPE_BIOFILL_RUN |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 767 | */ |
| 768 | if (test_and_clear_bit(R5_Wantfill, &dev->flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 769 | struct bio *rbi, *rbi2; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 770 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 771 | BUG_ON(!dev->read); |
| 772 | rbi = dev->read; |
| 773 | dev->read = NULL; |
| 774 | while (rbi && rbi->bi_sector < |
| 775 | dev->sector + STRIPE_SECTORS) { |
| 776 | rbi2 = r5_next_bio(rbi, dev->sector); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 777 | if (!raid5_dec_bi_phys_segments(rbi)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 778 | rbi->bi_next = return_bi; |
| 779 | return_bi = rbi; |
| 780 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 781 | rbi = rbi2; |
| 782 | } |
| 783 | } |
| 784 | } |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 785 | spin_unlock_irq(&conf->device_lock); |
| 786 | clear_bit(STRIPE_BIOFILL_RUN, &sh->state); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 787 | |
| 788 | return_io(return_bi); |
| 789 | |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 790 | set_bit(STRIPE_HANDLE, &sh->state); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 791 | release_stripe(sh); |
| 792 | } |
| 793 | |
| 794 | static void ops_run_biofill(struct stripe_head *sh) |
| 795 | { |
| 796 | struct dma_async_tx_descriptor *tx = NULL; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 797 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 798 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 799 | int i; |
| 800 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 801 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 802 | (unsigned long long)sh->sector); |
| 803 | |
| 804 | for (i = sh->disks; i--; ) { |
| 805 | struct r5dev *dev = &sh->dev[i]; |
| 806 | if (test_bit(R5_Wantfill, &dev->flags)) { |
| 807 | struct bio *rbi; |
| 808 | spin_lock_irq(&conf->device_lock); |
| 809 | dev->read = rbi = dev->toread; |
| 810 | dev->toread = NULL; |
| 811 | spin_unlock_irq(&conf->device_lock); |
| 812 | while (rbi && rbi->bi_sector < |
| 813 | dev->sector + STRIPE_SECTORS) { |
| 814 | tx = async_copy_data(0, rbi, dev->page, |
| 815 | dev->sector, tx); |
| 816 | rbi = r5_next_bio(rbi, dev->sector); |
| 817 | } |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 822 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL); |
| 823 | async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 824 | } |
| 825 | |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 826 | static void mark_target_uptodate(struct stripe_head *sh, int target) |
| 827 | { |
| 828 | struct r5dev *tgt; |
| 829 | |
| 830 | if (target < 0) |
| 831 | return; |
| 832 | |
| 833 | tgt = &sh->dev[target]; |
| 834 | set_bit(R5_UPTODATE, &tgt->flags); |
| 835 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 836 | clear_bit(R5_Wantcompute, &tgt->flags); |
| 837 | } |
| 838 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 839 | static void ops_complete_compute(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 840 | { |
| 841 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 842 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 843 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 844 | (unsigned long long)sh->sector); |
| 845 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 846 | /* mark the computed target(s) as uptodate */ |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 847 | mark_target_uptodate(sh, sh->ops.target); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 848 | mark_target_uptodate(sh, sh->ops.target2); |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 849 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 850 | clear_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 851 | if (sh->check_state == check_state_compute_run) |
| 852 | sh->check_state = check_state_compute_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 853 | set_bit(STRIPE_HANDLE, &sh->state); |
| 854 | release_stripe(sh); |
| 855 | } |
| 856 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 857 | /* return a pointer to the address conversion region of the scribble buffer */ |
| 858 | static addr_conv_t *to_addr_conv(struct stripe_head *sh, |
| 859 | struct raid5_percpu *percpu) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 860 | { |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 861 | return percpu->scribble + sizeof(struct page *) * (sh->disks + 2); |
| 862 | } |
| 863 | |
| 864 | static struct dma_async_tx_descriptor * |
| 865 | ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 866 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 867 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 868 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 869 | int target = sh->ops.target; |
| 870 | struct r5dev *tgt = &sh->dev[target]; |
| 871 | struct page *xor_dest = tgt->page; |
| 872 | int count = 0; |
| 873 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 874 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 875 | int i; |
| 876 | |
| 877 | pr_debug("%s: stripe %llu block: %d\n", |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 878 | __func__, (unsigned long long)sh->sector, target); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 879 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 880 | |
| 881 | for (i = disks; i--; ) |
| 882 | if (i != target) |
| 883 | xor_srcs[count++] = sh->dev[i].page; |
| 884 | |
| 885 | atomic_inc(&sh->count); |
| 886 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 887 | 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] | 888 | ops_complete_compute, sh, to_addr_conv(sh, percpu)); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 889 | if (unlikely(count == 1)) |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 890 | 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] | 891 | else |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 892 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 893 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 894 | return tx; |
| 895 | } |
| 896 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 897 | /* set_syndrome_sources - populate source buffers for gen_syndrome |
| 898 | * @srcs - (struct page *) array of size sh->disks |
| 899 | * @sh - stripe_head to parse |
| 900 | * |
| 901 | * Populates srcs in proper layout order for the stripe and returns the |
| 902 | * 'count' of sources to be used in a call to async_gen_syndrome. The P |
| 903 | * destination buffer is recorded in srcs[count] and the Q destination |
| 904 | * is recorded in srcs[count+1]]. |
| 905 | */ |
| 906 | static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh) |
| 907 | { |
| 908 | int disks = sh->disks; |
| 909 | int syndrome_disks = sh->ddf_layout ? disks : (disks - 2); |
| 910 | int d0_idx = raid6_d0(sh); |
| 911 | int count; |
| 912 | int i; |
| 913 | |
| 914 | for (i = 0; i < disks; i++) |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 915 | srcs[i] = NULL; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 916 | |
| 917 | count = 0; |
| 918 | i = d0_idx; |
| 919 | do { |
| 920 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 921 | |
| 922 | srcs[slot] = sh->dev[i].page; |
| 923 | i = raid6_next_disk(i, disks); |
| 924 | } while (i != d0_idx); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 925 | |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 926 | return syndrome_disks; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | static struct dma_async_tx_descriptor * |
| 930 | ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 931 | { |
| 932 | int disks = sh->disks; |
| 933 | struct page **blocks = percpu->scribble; |
| 934 | int target; |
| 935 | int qd_idx = sh->qd_idx; |
| 936 | struct dma_async_tx_descriptor *tx; |
| 937 | struct async_submit_ctl submit; |
| 938 | struct r5dev *tgt; |
| 939 | struct page *dest; |
| 940 | int i; |
| 941 | int count; |
| 942 | |
| 943 | if (sh->ops.target < 0) |
| 944 | target = sh->ops.target2; |
| 945 | else if (sh->ops.target2 < 0) |
| 946 | target = sh->ops.target; |
| 947 | else |
| 948 | /* we should only have one valid target */ |
| 949 | BUG(); |
| 950 | BUG_ON(target < 0); |
| 951 | pr_debug("%s: stripe %llu block: %d\n", |
| 952 | __func__, (unsigned long long)sh->sector, target); |
| 953 | |
| 954 | tgt = &sh->dev[target]; |
| 955 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 956 | dest = tgt->page; |
| 957 | |
| 958 | atomic_inc(&sh->count); |
| 959 | |
| 960 | if (target == qd_idx) { |
| 961 | count = set_syndrome_sources(blocks, sh); |
| 962 | blocks[count] = NULL; /* regenerating p is not necessary */ |
| 963 | BUG_ON(blocks[count+1] != dest); /* q should already be set */ |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 964 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 965 | ops_complete_compute, sh, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 966 | to_addr_conv(sh, percpu)); |
| 967 | tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
| 968 | } else { |
| 969 | /* Compute any data- or p-drive using XOR */ |
| 970 | count = 0; |
| 971 | for (i = disks; i-- ; ) { |
| 972 | if (i == target || i == qd_idx) |
| 973 | continue; |
| 974 | blocks[count++] = sh->dev[i].page; |
| 975 | } |
| 976 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 977 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
| 978 | NULL, ops_complete_compute, sh, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 979 | to_addr_conv(sh, percpu)); |
| 980 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit); |
| 981 | } |
| 982 | |
| 983 | return tx; |
| 984 | } |
| 985 | |
| 986 | static struct dma_async_tx_descriptor * |
| 987 | ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 988 | { |
| 989 | int i, count, disks = sh->disks; |
| 990 | int syndrome_disks = sh->ddf_layout ? disks : disks-2; |
| 991 | int d0_idx = raid6_d0(sh); |
| 992 | int faila = -1, failb = -1; |
| 993 | int target = sh->ops.target; |
| 994 | int target2 = sh->ops.target2; |
| 995 | struct r5dev *tgt = &sh->dev[target]; |
| 996 | struct r5dev *tgt2 = &sh->dev[target2]; |
| 997 | struct dma_async_tx_descriptor *tx; |
| 998 | struct page **blocks = percpu->scribble; |
| 999 | struct async_submit_ctl submit; |
| 1000 | |
| 1001 | pr_debug("%s: stripe %llu block1: %d block2: %d\n", |
| 1002 | __func__, (unsigned long long)sh->sector, target, target2); |
| 1003 | BUG_ON(target < 0 || target2 < 0); |
| 1004 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 1005 | BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags)); |
| 1006 | |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1007 | /* we need to open-code set_syndrome_sources to handle the |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1008 | * slot number conversion for 'faila' and 'failb' |
| 1009 | */ |
| 1010 | for (i = 0; i < disks ; i++) |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 1011 | blocks[i] = NULL; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1012 | count = 0; |
| 1013 | i = d0_idx; |
| 1014 | do { |
| 1015 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 1016 | |
| 1017 | blocks[slot] = sh->dev[i].page; |
| 1018 | |
| 1019 | if (i == target) |
| 1020 | faila = slot; |
| 1021 | if (i == target2) |
| 1022 | failb = slot; |
| 1023 | i = raid6_next_disk(i, disks); |
| 1024 | } while (i != d0_idx); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1025 | |
| 1026 | BUG_ON(faila == failb); |
| 1027 | if (failb < faila) |
| 1028 | swap(faila, failb); |
| 1029 | pr_debug("%s: stripe: %llu faila: %d failb: %d\n", |
| 1030 | __func__, (unsigned long long)sh->sector, faila, failb); |
| 1031 | |
| 1032 | atomic_inc(&sh->count); |
| 1033 | |
| 1034 | if (failb == syndrome_disks+1) { |
| 1035 | /* Q disk is one of the missing disks */ |
| 1036 | if (faila == syndrome_disks) { |
| 1037 | /* Missing P+Q, just recompute */ |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1038 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 1039 | ops_complete_compute, sh, |
| 1040 | to_addr_conv(sh, percpu)); |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 1041 | return async_gen_syndrome(blocks, 0, syndrome_disks+2, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1042 | STRIPE_SIZE, &submit); |
| 1043 | } else { |
| 1044 | struct page *dest; |
| 1045 | int data_target; |
| 1046 | int qd_idx = sh->qd_idx; |
| 1047 | |
| 1048 | /* Missing D+Q: recompute D from P, then recompute Q */ |
| 1049 | if (target == qd_idx) |
| 1050 | data_target = target2; |
| 1051 | else |
| 1052 | data_target = target; |
| 1053 | |
| 1054 | count = 0; |
| 1055 | for (i = disks; i-- ; ) { |
| 1056 | if (i == data_target || i == qd_idx) |
| 1057 | continue; |
| 1058 | blocks[count++] = sh->dev[i].page; |
| 1059 | } |
| 1060 | dest = sh->dev[data_target].page; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1061 | init_async_submit(&submit, |
| 1062 | ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
| 1063 | NULL, NULL, NULL, |
| 1064 | to_addr_conv(sh, percpu)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1065 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, |
| 1066 | &submit); |
| 1067 | |
| 1068 | count = set_syndrome_sources(blocks, sh); |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1069 | init_async_submit(&submit, ASYNC_TX_FENCE, tx, |
| 1070 | ops_complete_compute, sh, |
| 1071 | to_addr_conv(sh, percpu)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1072 | return async_gen_syndrome(blocks, 0, count+2, |
| 1073 | STRIPE_SIZE, &submit); |
| 1074 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1075 | } else { |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1076 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 1077 | ops_complete_compute, sh, |
| 1078 | to_addr_conv(sh, percpu)); |
| 1079 | if (failb == syndrome_disks) { |
| 1080 | /* We're missing D+P. */ |
| 1081 | return async_raid6_datap_recov(syndrome_disks+2, |
| 1082 | STRIPE_SIZE, faila, |
| 1083 | blocks, &submit); |
| 1084 | } else { |
| 1085 | /* We're missing D+D. */ |
| 1086 | return async_raid6_2data_recov(syndrome_disks+2, |
| 1087 | STRIPE_SIZE, faila, failb, |
| 1088 | blocks, &submit); |
| 1089 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1090 | } |
| 1091 | } |
| 1092 | |
| 1093 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1094 | static void ops_complete_prexor(void *stripe_head_ref) |
| 1095 | { |
| 1096 | struct stripe_head *sh = stripe_head_ref; |
| 1097 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1098 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1099 | (unsigned long long)sh->sector); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | static struct dma_async_tx_descriptor * |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1103 | ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1104 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1105 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1106 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1107 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1108 | int count = 0, pd_idx = sh->pd_idx, i; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1109 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1110 | |
| 1111 | /* existing parity data subtracted */ |
| 1112 | struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 1113 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1114 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1115 | (unsigned long long)sh->sector); |
| 1116 | |
| 1117 | for (i = disks; i--; ) { |
| 1118 | struct r5dev *dev = &sh->dev[i]; |
| 1119 | /* Only process blocks that are known to be uptodate */ |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1120 | if (test_bit(R5_Wantdrain, &dev->flags)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1121 | xor_srcs[count++] = dev->page; |
| 1122 | } |
| 1123 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1124 | 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] | 1125 | ops_complete_prexor, sh, to_addr_conv(sh, percpu)); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1126 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1127 | |
| 1128 | return tx; |
| 1129 | } |
| 1130 | |
| 1131 | static struct dma_async_tx_descriptor * |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1132 | 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] | 1133 | { |
| 1134 | int disks = sh->disks; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1135 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1136 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1137 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1138 | (unsigned long long)sh->sector); |
| 1139 | |
| 1140 | for (i = disks; i--; ) { |
| 1141 | struct r5dev *dev = &sh->dev[i]; |
| 1142 | struct bio *chosen; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1143 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1144 | if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1145 | struct bio *wbi; |
| 1146 | |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 1147 | spin_lock_irq(&sh->raid_conf->device_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1148 | chosen = dev->towrite; |
| 1149 | dev->towrite = NULL; |
| 1150 | BUG_ON(dev->written); |
| 1151 | wbi = dev->written = chosen; |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 1152 | spin_unlock_irq(&sh->raid_conf->device_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1153 | |
| 1154 | while (wbi && wbi->bi_sector < |
| 1155 | dev->sector + STRIPE_SECTORS) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1156 | if (wbi->bi_rw & REQ_FUA) |
| 1157 | set_bit(R5_WantFUA, &dev->flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1158 | if (wbi->bi_rw & REQ_SYNC) |
| 1159 | set_bit(R5_SyncIO, &dev->flags); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1160 | tx = async_copy_data(1, wbi, dev->page, |
| 1161 | dev->sector, tx); |
| 1162 | wbi = r5_next_bio(wbi, dev->sector); |
| 1163 | } |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | return tx; |
| 1168 | } |
| 1169 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1170 | static void ops_complete_reconstruct(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1171 | { |
| 1172 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1173 | int disks = sh->disks; |
| 1174 | int pd_idx = sh->pd_idx; |
| 1175 | int qd_idx = sh->qd_idx; |
| 1176 | int i; |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1177 | bool fua = false, sync = false; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1178 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1179 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1180 | (unsigned long long)sh->sector); |
| 1181 | |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1182 | for (i = disks; i--; ) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1183 | fua |= test_bit(R5_WantFUA, &sh->dev[i].flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1184 | sync |= test_bit(R5_SyncIO, &sh->dev[i].flags); |
| 1185 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1186 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1187 | for (i = disks; i--; ) { |
| 1188 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1189 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1190 | if (dev->written || i == pd_idx || i == qd_idx) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1191 | set_bit(R5_UPTODATE, &dev->flags); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1192 | if (fua) |
| 1193 | set_bit(R5_WantFUA, &dev->flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1194 | if (sync) |
| 1195 | set_bit(R5_SyncIO, &dev->flags); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1196 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1197 | } |
| 1198 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1199 | if (sh->reconstruct_state == reconstruct_state_drain_run) |
| 1200 | sh->reconstruct_state = reconstruct_state_drain_result; |
| 1201 | else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) |
| 1202 | sh->reconstruct_state = reconstruct_state_prexor_drain_result; |
| 1203 | else { |
| 1204 | BUG_ON(sh->reconstruct_state != reconstruct_state_run); |
| 1205 | sh->reconstruct_state = reconstruct_state_result; |
| 1206 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1207 | |
| 1208 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1209 | release_stripe(sh); |
| 1210 | } |
| 1211 | |
| 1212 | static void |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1213 | ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1214 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1215 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1216 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1217 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1218 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1219 | int count = 0, pd_idx = sh->pd_idx, i; |
| 1220 | struct page *xor_dest; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1221 | int prexor = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1222 | unsigned long flags; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1223 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1224 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1225 | (unsigned long long)sh->sector); |
| 1226 | |
| 1227 | /* check if prexor is active which means only process blocks |
| 1228 | * that are part of a read-modify-write (written) |
| 1229 | */ |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1230 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) { |
| 1231 | prexor = 1; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1232 | xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 1233 | for (i = disks; i--; ) { |
| 1234 | struct r5dev *dev = &sh->dev[i]; |
| 1235 | if (dev->written) |
| 1236 | xor_srcs[count++] = dev->page; |
| 1237 | } |
| 1238 | } else { |
| 1239 | xor_dest = sh->dev[pd_idx].page; |
| 1240 | for (i = disks; i--; ) { |
| 1241 | struct r5dev *dev = &sh->dev[i]; |
| 1242 | if (i != pd_idx) |
| 1243 | xor_srcs[count++] = dev->page; |
| 1244 | } |
| 1245 | } |
| 1246 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1247 | /* 1/ if we prexor'd then the dest is reused as a source |
| 1248 | * 2/ if we did not prexor then we are redoing the parity |
| 1249 | * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST |
| 1250 | * for the synchronous xor case |
| 1251 | */ |
Dan Williams | 88ba2aa | 2009-04-09 16:16:18 -0700 | [diff] [blame] | 1252 | flags = ASYNC_TX_ACK | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1253 | (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST); |
| 1254 | |
| 1255 | atomic_inc(&sh->count); |
| 1256 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1257 | init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh, |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1258 | to_addr_conv(sh, percpu)); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1259 | if (unlikely(count == 1)) |
| 1260 | tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit); |
| 1261 | else |
| 1262 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1263 | } |
| 1264 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1265 | static void |
| 1266 | ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1267 | struct dma_async_tx_descriptor *tx) |
| 1268 | { |
| 1269 | struct async_submit_ctl submit; |
| 1270 | struct page **blocks = percpu->scribble; |
| 1271 | int count; |
| 1272 | |
| 1273 | pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector); |
| 1274 | |
| 1275 | count = set_syndrome_sources(blocks, sh); |
| 1276 | |
| 1277 | atomic_inc(&sh->count); |
| 1278 | |
| 1279 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct, |
| 1280 | sh, to_addr_conv(sh, percpu)); |
| 1281 | async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | static void ops_complete_check(void *stripe_head_ref) |
| 1285 | { |
| 1286 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1287 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1288 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1289 | (unsigned long long)sh->sector); |
| 1290 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 1291 | sh->check_state = check_state_check_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1292 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1293 | release_stripe(sh); |
| 1294 | } |
| 1295 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1296 | 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] | 1297 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1298 | int disks = sh->disks; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1299 | int pd_idx = sh->pd_idx; |
| 1300 | int qd_idx = sh->qd_idx; |
| 1301 | struct page *xor_dest; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1302 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1303 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1304 | struct async_submit_ctl submit; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1305 | int count; |
| 1306 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1307 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1308 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1309 | (unsigned long long)sh->sector); |
| 1310 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1311 | count = 0; |
| 1312 | xor_dest = sh->dev[pd_idx].page; |
| 1313 | xor_srcs[count++] = xor_dest; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1314 | for (i = disks; i--; ) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1315 | if (i == pd_idx || i == qd_idx) |
| 1316 | continue; |
| 1317 | xor_srcs[count++] = sh->dev[i].page; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1318 | } |
| 1319 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1320 | init_async_submit(&submit, 0, NULL, NULL, NULL, |
| 1321 | to_addr_conv(sh, percpu)); |
Dan Williams | 099f53c | 2009-04-08 14:28:37 -0700 | [diff] [blame] | 1322 | tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1323 | &sh->ops.zero_sum_result, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1324 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1325 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1326 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL); |
| 1327 | tx = async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1328 | } |
| 1329 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1330 | static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp) |
| 1331 | { |
| 1332 | struct page **srcs = percpu->scribble; |
| 1333 | struct async_submit_ctl submit; |
| 1334 | int count; |
| 1335 | |
| 1336 | pr_debug("%s: stripe %llu checkp: %d\n", __func__, |
| 1337 | (unsigned long long)sh->sector, checkp); |
| 1338 | |
| 1339 | count = set_syndrome_sources(srcs, sh); |
| 1340 | if (!checkp) |
| 1341 | srcs[count] = NULL; |
| 1342 | |
| 1343 | atomic_inc(&sh->count); |
| 1344 | init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check, |
| 1345 | sh, to_addr_conv(sh, percpu)); |
| 1346 | async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE, |
| 1347 | &sh->ops.zero_sum_result, percpu->spare_page, &submit); |
| 1348 | } |
| 1349 | |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 1350 | 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] | 1351 | { |
| 1352 | int overlap_clear = 0, i, disks = sh->disks; |
| 1353 | struct dma_async_tx_descriptor *tx = NULL; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1354 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1355 | int level = conf->level; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1356 | struct raid5_percpu *percpu; |
| 1357 | unsigned long cpu; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1358 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1359 | cpu = get_cpu(); |
| 1360 | percpu = per_cpu_ptr(conf->percpu, cpu); |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 1361 | if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1362 | ops_run_biofill(sh); |
| 1363 | overlap_clear++; |
| 1364 | } |
| 1365 | |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 1366 | if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1367 | if (level < 6) |
| 1368 | tx = ops_run_compute5(sh, percpu); |
| 1369 | else { |
| 1370 | if (sh->ops.target2 < 0 || sh->ops.target < 0) |
| 1371 | tx = ops_run_compute6_1(sh, percpu); |
| 1372 | else |
| 1373 | tx = ops_run_compute6_2(sh, percpu); |
| 1374 | } |
| 1375 | /* terminate the chain if reconstruct is not set to be run */ |
| 1376 | if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 1377 | async_tx_ack(tx); |
| 1378 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1379 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1380 | if (test_bit(STRIPE_OP_PREXOR, &ops_request)) |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1381 | tx = ops_run_prexor(sh, percpu, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1382 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1383 | if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) { |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1384 | tx = ops_run_biodrain(sh, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1385 | overlap_clear++; |
| 1386 | } |
| 1387 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1388 | if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) { |
| 1389 | if (level < 6) |
| 1390 | ops_run_reconstruct5(sh, percpu, tx); |
| 1391 | else |
| 1392 | ops_run_reconstruct6(sh, percpu, tx); |
| 1393 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1394 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1395 | if (test_bit(STRIPE_OP_CHECK, &ops_request)) { |
| 1396 | if (sh->check_state == check_state_run) |
| 1397 | ops_run_check_p(sh, percpu); |
| 1398 | else if (sh->check_state == check_state_run_q) |
| 1399 | ops_run_check_pq(sh, percpu, 0); |
| 1400 | else if (sh->check_state == check_state_run_pq) |
| 1401 | ops_run_check_pq(sh, percpu, 1); |
| 1402 | else |
| 1403 | BUG(); |
| 1404 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1405 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1406 | if (overlap_clear) |
| 1407 | for (i = disks; i--; ) { |
| 1408 | struct r5dev *dev = &sh->dev[i]; |
| 1409 | if (test_and_clear_bit(R5_Overlap, &dev->flags)) |
| 1410 | wake_up(&sh->raid_conf->wait_for_overlap); |
| 1411 | } |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1412 | put_cpu(); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1413 | } |
| 1414 | |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 1415 | #ifdef CONFIG_MULTICORE_RAID456 |
| 1416 | static void async_run_ops(void *param, async_cookie_t cookie) |
| 1417 | { |
| 1418 | struct stripe_head *sh = param; |
| 1419 | unsigned long ops_request = sh->ops.request; |
| 1420 | |
| 1421 | clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state); |
| 1422 | wake_up(&sh->ops.wait_for_ops); |
| 1423 | |
| 1424 | __raid_run_ops(sh, ops_request); |
| 1425 | release_stripe(sh); |
| 1426 | } |
| 1427 | |
| 1428 | static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request) |
| 1429 | { |
| 1430 | /* since handle_stripe can be called outside of raid5d context |
| 1431 | * we need to ensure sh->ops.request is de-staged before another |
| 1432 | * request arrives |
| 1433 | */ |
| 1434 | wait_event(sh->ops.wait_for_ops, |
| 1435 | !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state)); |
| 1436 | sh->ops.request = ops_request; |
| 1437 | |
| 1438 | atomic_inc(&sh->count); |
| 1439 | async_schedule(async_run_ops, sh); |
| 1440 | } |
| 1441 | #else |
| 1442 | #define raid_run_ops __raid_run_ops |
| 1443 | #endif |
| 1444 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1445 | static int grow_one_stripe(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | { |
| 1447 | struct stripe_head *sh; |
Namhyung Kim | 6ce3284 | 2011-07-18 17:38:50 +1000 | [diff] [blame] | 1448 | sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1449 | if (!sh) |
| 1450 | return 0; |
Namhyung Kim | 6ce3284 | 2011-07-18 17:38:50 +1000 | [diff] [blame] | 1451 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1452 | sh->raid_conf = conf; |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 1453 | #ifdef CONFIG_MULTICORE_RAID456 |
| 1454 | init_waitqueue_head(&sh->ops.wait_for_ops); |
| 1455 | #endif |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1456 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 1457 | if (grow_buffers(sh)) { |
| 1458 | shrink_buffers(sh); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1459 | kmem_cache_free(conf->slab_cache, sh); |
| 1460 | return 0; |
| 1461 | } |
| 1462 | /* we just created an active stripe so... */ |
| 1463 | atomic_set(&sh->count, 1); |
| 1464 | atomic_inc(&conf->active_stripes); |
| 1465 | INIT_LIST_HEAD(&sh->lru); |
| 1466 | release_stripe(sh); |
| 1467 | return 1; |
| 1468 | } |
| 1469 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1470 | static int grow_stripes(struct r5conf *conf, int num) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1471 | { |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 1472 | struct kmem_cache *sc; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 1473 | int devs = max(conf->raid_disks, conf->previous_raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | |
NeilBrown | f4be6b4 | 2010-06-01 19:37:25 +1000 | [diff] [blame] | 1475 | if (conf->mddev->gendisk) |
| 1476 | sprintf(conf->cache_name[0], |
| 1477 | "raid%d-%s", conf->level, mdname(conf->mddev)); |
| 1478 | else |
| 1479 | sprintf(conf->cache_name[0], |
| 1480 | "raid%d-%p", conf->level, conf->mddev); |
| 1481 | sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]); |
| 1482 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1483 | conf->active_name = 0; |
| 1484 | sc = kmem_cache_create(conf->cache_name[conf->active_name], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1486 | 0, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | if (!sc) |
| 1488 | return 1; |
| 1489 | conf->slab_cache = sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1490 | conf->pool_size = devs; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1491 | while (num--) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1492 | if (!grow_one_stripe(conf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | return 0; |
| 1495 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 1496 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1497 | /** |
| 1498 | * scribble_len - return the required size of the scribble region |
| 1499 | * @num - total number of disks in the array |
| 1500 | * |
| 1501 | * The size must be enough to contain: |
| 1502 | * 1/ a struct page pointer for each device in the array +2 |
| 1503 | * 2/ room to convert each entry in (1) to its corresponding dma |
| 1504 | * (dma_map_page()) or page (page_address()) address. |
| 1505 | * |
| 1506 | * Note: the +2 is for the destination buffers of the ddf/raid6 case where we |
| 1507 | * calculate over all devices (not just the data blocks), using zeros in place |
| 1508 | * of the P and Q blocks. |
| 1509 | */ |
| 1510 | static size_t scribble_len(int num) |
| 1511 | { |
| 1512 | size_t len; |
| 1513 | |
| 1514 | len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2); |
| 1515 | |
| 1516 | return len; |
| 1517 | } |
| 1518 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1519 | static int resize_stripes(struct r5conf *conf, int newsize) |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1520 | { |
| 1521 | /* Make all the stripes able to hold 'newsize' devices. |
| 1522 | * New slots in each stripe get 'page' set to a new page. |
| 1523 | * |
| 1524 | * This happens in stages: |
| 1525 | * 1/ create a new kmem_cache and allocate the required number of |
| 1526 | * stripe_heads. |
| 1527 | * 2/ gather all the old stripe_heads and tranfer the pages across |
| 1528 | * to the new stripe_heads. This will have the side effect of |
| 1529 | * freezing the array as once all stripe_heads have been collected, |
| 1530 | * no IO will be possible. Old stripe heads are freed once their |
| 1531 | * pages have been transferred over, and the old kmem_cache is |
| 1532 | * freed when all stripes are done. |
| 1533 | * 3/ reallocate conf->disks to be suitable bigger. If this fails, |
| 1534 | * we simple return a failre status - no need to clean anything up. |
| 1535 | * 4/ allocate new pages for the new slots in the new stripe_heads. |
| 1536 | * If this fails, we don't bother trying the shrink the |
| 1537 | * stripe_heads down again, we just leave them as they are. |
| 1538 | * As each stripe_head is processed the new one is released into |
| 1539 | * active service. |
| 1540 | * |
| 1541 | * Once step2 is started, we cannot afford to wait for a write, |
| 1542 | * so we use GFP_NOIO allocations. |
| 1543 | */ |
| 1544 | struct stripe_head *osh, *nsh; |
| 1545 | LIST_HEAD(newstripes); |
| 1546 | struct disk_info *ndisks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1547 | unsigned long cpu; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 1548 | int err; |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 1549 | struct kmem_cache *sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1550 | int i; |
| 1551 | |
| 1552 | if (newsize <= conf->pool_size) |
| 1553 | return 0; /* never bother to shrink */ |
| 1554 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 1555 | err = md_allow_write(conf->mddev); |
| 1556 | if (err) |
| 1557 | return err; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 1558 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1559 | /* Step 1 */ |
| 1560 | sc = kmem_cache_create(conf->cache_name[1-conf->active_name], |
| 1561 | sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1562 | 0, 0, NULL); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1563 | if (!sc) |
| 1564 | return -ENOMEM; |
| 1565 | |
| 1566 | for (i = conf->max_nr_stripes; i; i--) { |
Namhyung Kim | 6ce3284 | 2011-07-18 17:38:50 +1000 | [diff] [blame] | 1567 | nsh = kmem_cache_zalloc(sc, GFP_KERNEL); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1568 | if (!nsh) |
| 1569 | break; |
| 1570 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1571 | nsh->raid_conf = conf; |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 1572 | #ifdef CONFIG_MULTICORE_RAID456 |
| 1573 | init_waitqueue_head(&nsh->ops.wait_for_ops); |
| 1574 | #endif |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1575 | |
| 1576 | list_add(&nsh->lru, &newstripes); |
| 1577 | } |
| 1578 | if (i) { |
| 1579 | /* didn't get enough, give up */ |
| 1580 | while (!list_empty(&newstripes)) { |
| 1581 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 1582 | list_del(&nsh->lru); |
| 1583 | kmem_cache_free(sc, nsh); |
| 1584 | } |
| 1585 | kmem_cache_destroy(sc); |
| 1586 | return -ENOMEM; |
| 1587 | } |
| 1588 | /* Step 2 - Must use GFP_NOIO now. |
| 1589 | * OK, we have enough stripes, start collecting inactive |
| 1590 | * stripes and copying them over |
| 1591 | */ |
| 1592 | list_for_each_entry(nsh, &newstripes, lru) { |
| 1593 | spin_lock_irq(&conf->device_lock); |
| 1594 | wait_event_lock_irq(conf->wait_for_stripe, |
| 1595 | !list_empty(&conf->inactive_list), |
| 1596 | conf->device_lock, |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 1597 | ); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1598 | osh = get_free_stripe(conf); |
| 1599 | spin_unlock_irq(&conf->device_lock); |
| 1600 | atomic_set(&nsh->count, 1); |
| 1601 | for(i=0; i<conf->pool_size; i++) |
| 1602 | nsh->dev[i].page = osh->dev[i].page; |
| 1603 | for( ; i<newsize; i++) |
| 1604 | nsh->dev[i].page = NULL; |
| 1605 | kmem_cache_free(conf->slab_cache, osh); |
| 1606 | } |
| 1607 | kmem_cache_destroy(conf->slab_cache); |
| 1608 | |
| 1609 | /* Step 3. |
| 1610 | * At this point, we are holding all the stripes so the array |
| 1611 | * is completely stalled, so now is a good time to resize |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1612 | * conf->disks and the scribble region |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1613 | */ |
| 1614 | ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO); |
| 1615 | if (ndisks) { |
| 1616 | for (i=0; i<conf->raid_disks; i++) |
| 1617 | ndisks[i] = conf->disks[i]; |
| 1618 | kfree(conf->disks); |
| 1619 | conf->disks = ndisks; |
| 1620 | } else |
| 1621 | err = -ENOMEM; |
| 1622 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1623 | get_online_cpus(); |
| 1624 | conf->scribble_len = scribble_len(newsize); |
| 1625 | for_each_present_cpu(cpu) { |
| 1626 | struct raid5_percpu *percpu; |
| 1627 | void *scribble; |
| 1628 | |
| 1629 | percpu = per_cpu_ptr(conf->percpu, cpu); |
| 1630 | scribble = kmalloc(conf->scribble_len, GFP_NOIO); |
| 1631 | |
| 1632 | if (scribble) { |
| 1633 | kfree(percpu->scribble); |
| 1634 | percpu->scribble = scribble; |
| 1635 | } else { |
| 1636 | err = -ENOMEM; |
| 1637 | break; |
| 1638 | } |
| 1639 | } |
| 1640 | put_online_cpus(); |
| 1641 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1642 | /* Step 4, return new stripes to service */ |
| 1643 | while(!list_empty(&newstripes)) { |
| 1644 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 1645 | list_del_init(&nsh->lru); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1646 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1647 | for (i=conf->raid_disks; i < newsize; i++) |
| 1648 | if (nsh->dev[i].page == NULL) { |
| 1649 | struct page *p = alloc_page(GFP_NOIO); |
| 1650 | nsh->dev[i].page = p; |
| 1651 | if (!p) |
| 1652 | err = -ENOMEM; |
| 1653 | } |
| 1654 | release_stripe(nsh); |
| 1655 | } |
| 1656 | /* critical section pass, GFP_NOIO no longer needed */ |
| 1657 | |
| 1658 | conf->slab_cache = sc; |
| 1659 | conf->active_name = 1-conf->active_name; |
| 1660 | conf->pool_size = newsize; |
| 1661 | return err; |
| 1662 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1664 | static int drop_one_stripe(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | { |
| 1666 | struct stripe_head *sh; |
| 1667 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1668 | spin_lock_irq(&conf->device_lock); |
| 1669 | sh = get_free_stripe(conf); |
| 1670 | spin_unlock_irq(&conf->device_lock); |
| 1671 | if (!sh) |
| 1672 | return 0; |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 1673 | BUG_ON(atomic_read(&sh->count)); |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 1674 | shrink_buffers(sh); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1675 | kmem_cache_free(conf->slab_cache, sh); |
| 1676 | atomic_dec(&conf->active_stripes); |
| 1677 | return 1; |
| 1678 | } |
| 1679 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1680 | static void shrink_stripes(struct r5conf *conf) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1681 | { |
| 1682 | while (drop_one_stripe(conf)) |
| 1683 | ; |
| 1684 | |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1685 | if (conf->slab_cache) |
| 1686 | kmem_cache_destroy(conf->slab_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | conf->slab_cache = NULL; |
| 1688 | } |
| 1689 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1690 | static void raid5_end_read_request(struct bio * bi, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1692 | struct stripe_head *sh = bi->bi_private; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1693 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 1694 | int disks = sh->disks, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1696 | char b[BDEVNAME_SIZE]; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1697 | struct md_rdev *rdev = NULL; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1698 | sector_t s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1699 | |
| 1700 | for (i=0 ; i<disks; i++) |
| 1701 | if (bi == &sh->dev[i].req) |
| 1702 | break; |
| 1703 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1704 | pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n", |
| 1705 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1706 | uptodate); |
| 1707 | if (i == disks) { |
| 1708 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1709 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | } |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1711 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1712 | /* If replacement finished while this request was outstanding, |
| 1713 | * 'replacement' might be NULL already. |
| 1714 | * In that case it moved down to 'rdev'. |
| 1715 | * rdev is not removed until all requests are finished. |
| 1716 | */ |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1717 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1718 | if (!rdev) |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1719 | rdev = conf->disks[i].rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1721 | if (use_new_offset(conf, sh)) |
| 1722 | s = sh->sector + rdev->new_data_offset; |
| 1723 | else |
| 1724 | s = sh->sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | if (uptodate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | set_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1727 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1728 | /* Note that this cannot happen on a |
| 1729 | * replacement device. We just fail those on |
| 1730 | * any error |
| 1731 | */ |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1732 | printk_ratelimited( |
| 1733 | KERN_INFO |
| 1734 | "md/raid:%s: read error corrected" |
| 1735 | " (%lu sectors at %llu on %s)\n", |
| 1736 | mdname(conf->mddev), STRIPE_SECTORS, |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1737 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1738 | bdevname(rdev->bdev, b)); |
Namhyung Kim | ddd5115 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1739 | atomic_add(STRIPE_SECTORS, &rdev->corrected_errors); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1740 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 1741 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
| 1742 | } |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1743 | if (atomic_read(&rdev->read_errors)) |
| 1744 | atomic_set(&rdev->read_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | } else { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1746 | const char *bdn = bdevname(rdev->bdev, b); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1747 | int retry = 0; |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1748 | int set_bad = 0; |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1749 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | clear_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1751 | atomic_inc(&rdev->read_errors); |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1752 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) |
| 1753 | printk_ratelimited( |
| 1754 | KERN_WARNING |
| 1755 | "md/raid:%s: read error on replacement device " |
| 1756 | "(sector %llu on %s).\n", |
| 1757 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1758 | (unsigned long long)s, |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1759 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1760 | else if (conf->mddev->degraded >= conf->max_degraded) { |
| 1761 | set_bad = 1; |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1762 | printk_ratelimited( |
| 1763 | KERN_WARNING |
| 1764 | "md/raid:%s: read error not correctable " |
| 1765 | "(sector %llu on %s).\n", |
| 1766 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1767 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1768 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1769 | } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) { |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1770 | /* Oh, no!!! */ |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1771 | set_bad = 1; |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1772 | printk_ratelimited( |
| 1773 | KERN_WARNING |
| 1774 | "md/raid:%s: read error NOT corrected!! " |
| 1775 | "(sector %llu on %s).\n", |
| 1776 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1777 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1778 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1779 | } else if (atomic_read(&rdev->read_errors) |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1780 | > conf->max_nr_stripes) |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 1781 | printk(KERN_WARNING |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 1782 | "md/raid:%s: Too many read errors, failing device %s.\n", |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1783 | mdname(conf->mddev), bdn); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1784 | else |
| 1785 | retry = 1; |
| 1786 | if (retry) |
| 1787 | set_bit(R5_ReadError, &sh->dev[i].flags); |
| 1788 | else { |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1789 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 1790 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1791 | if (!(set_bad |
| 1792 | && test_bit(In_sync, &rdev->flags) |
| 1793 | && rdev_set_badblocks( |
| 1794 | rdev, sh->sector, STRIPE_SECTORS, 0))) |
| 1795 | md_error(conf->mddev, rdev); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1796 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | } |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1798 | rdev_dec_pending(rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 1800 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1801 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1802 | } |
| 1803 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 1804 | static void raid5_end_write_request(struct bio *bi, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1806 | struct stripe_head *sh = bi->bi_private; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1807 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 1808 | int disks = sh->disks, i; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1809 | struct md_rdev *uninitialized_var(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 1811 | sector_t first_bad; |
| 1812 | int bad_sectors; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1813 | int replacement = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1815 | for (i = 0 ; i < disks; i++) { |
| 1816 | if (bi == &sh->dev[i].req) { |
| 1817 | rdev = conf->disks[i].rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | break; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1819 | } |
| 1820 | if (bi == &sh->dev[i].rreq) { |
| 1821 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1822 | if (rdev) |
| 1823 | replacement = 1; |
| 1824 | else |
| 1825 | /* rdev was removed and 'replacement' |
| 1826 | * replaced it. rdev is not removed |
| 1827 | * until all requests are finished. |
| 1828 | */ |
| 1829 | rdev = conf->disks[i].rdev; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1830 | break; |
| 1831 | } |
| 1832 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1833 | pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
| 1835 | uptodate); |
| 1836 | if (i == disks) { |
| 1837 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1838 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1839 | } |
| 1840 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1841 | if (replacement) { |
| 1842 | if (!uptodate) |
| 1843 | md_error(conf->mddev, rdev); |
| 1844 | else if (is_badblock(rdev, sh->sector, |
| 1845 | STRIPE_SECTORS, |
| 1846 | &first_bad, &bad_sectors)) |
| 1847 | set_bit(R5_MadeGoodRepl, &sh->dev[i].flags); |
| 1848 | } else { |
| 1849 | if (!uptodate) { |
| 1850 | set_bit(WriteErrorSeen, &rdev->flags); |
| 1851 | set_bit(R5_WriteError, &sh->dev[i].flags); |
NeilBrown | 3a6de29 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 1852 | if (!test_and_set_bit(WantReplacement, &rdev->flags)) |
| 1853 | set_bit(MD_RECOVERY_NEEDED, |
| 1854 | &rdev->mddev->recovery); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1855 | } else if (is_badblock(rdev, sh->sector, |
| 1856 | STRIPE_SECTORS, |
| 1857 | &first_bad, &bad_sectors)) |
| 1858 | set_bit(R5_MadeGood, &sh->dev[i].flags); |
| 1859 | } |
| 1860 | rdev_dec_pending(rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1862 | if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags)) |
| 1863 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | set_bit(STRIPE_HANDLE, &sh->state); |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1865 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1866 | } |
| 1867 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1868 | 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] | 1869 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1870 | 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] | 1871 | { |
| 1872 | struct r5dev *dev = &sh->dev[i]; |
| 1873 | |
| 1874 | bio_init(&dev->req); |
| 1875 | dev->req.bi_io_vec = &dev->vec; |
| 1876 | dev->req.bi_vcnt++; |
| 1877 | dev->req.bi_max_vecs++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | dev->req.bi_private = sh; |
NeilBrown | 995c427 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1879 | dev->vec.bv_page = dev->page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1881 | bio_init(&dev->rreq); |
| 1882 | dev->rreq.bi_io_vec = &dev->rvec; |
| 1883 | dev->rreq.bi_vcnt++; |
| 1884 | dev->rreq.bi_max_vecs++; |
| 1885 | dev->rreq.bi_private = sh; |
| 1886 | dev->rvec.bv_page = dev->page; |
| 1887 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | dev->flags = 0; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1889 | dev->sector = compute_blocknr(sh, i, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1890 | } |
| 1891 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1892 | static void error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | { |
| 1894 | char b[BDEVNAME_SIZE]; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1895 | struct r5conf *conf = mddev->private; |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 1896 | unsigned long flags; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 1897 | pr_debug("raid456: error called\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1898 | |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 1899 | spin_lock_irqsave(&conf->device_lock, flags); |
| 1900 | clear_bit(In_sync, &rdev->flags); |
| 1901 | mddev->degraded = calc_degraded(conf); |
| 1902 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 1903 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 1904 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 1905 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 6f8d0c7 | 2011-05-11 14:38:44 +1000 | [diff] [blame] | 1906 | set_bit(Faulty, &rdev->flags); |
| 1907 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 1908 | printk(KERN_ALERT |
| 1909 | "md/raid:%s: Disk failure on %s, disabling device.\n" |
| 1910 | "md/raid:%s: Operation continuing on %d devices.\n", |
| 1911 | mdname(mddev), |
| 1912 | bdevname(rdev->bdev, b), |
| 1913 | mdname(mddev), |
| 1914 | conf->raid_disks - mddev->degraded); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1915 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1916 | |
| 1917 | /* |
| 1918 | * Input: a 'big' sector number, |
| 1919 | * Output: index of the data and parity disk, and the sector # in them. |
| 1920 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1921 | static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1922 | int previous, int *dd_idx, |
| 1923 | struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1924 | { |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1925 | sector_t stripe, stripe2; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 1926 | sector_t chunk_number; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | unsigned int chunk_offset; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1928 | int pd_idx, qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1929 | int ddf_layout = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1930 | sector_t new_sector; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1931 | int algorithm = previous ? conf->prev_algo |
| 1932 | : conf->algorithm; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 1933 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
| 1934 | : conf->chunk_sectors; |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1935 | int raid_disks = previous ? conf->previous_raid_disks |
| 1936 | : conf->raid_disks; |
| 1937 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1938 | |
| 1939 | /* First compute the information on this sector */ |
| 1940 | |
| 1941 | /* |
| 1942 | * Compute the chunk number and the sector offset inside the chunk |
| 1943 | */ |
| 1944 | chunk_offset = sector_div(r_sector, sectors_per_chunk); |
| 1945 | chunk_number = r_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1946 | |
| 1947 | /* |
| 1948 | * Compute the stripe number |
| 1949 | */ |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 1950 | stripe = chunk_number; |
| 1951 | *dd_idx = sector_div(stripe, data_disks); |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1952 | stripe2 = stripe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1953 | /* |
| 1954 | * Select the parity disk based on the user selected algorithm. |
| 1955 | */ |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1956 | pd_idx = qd_idx = -1; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1957 | switch(conf->level) { |
| 1958 | case 4: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1959 | pd_idx = data_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1960 | break; |
| 1961 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1962 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1964 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1965 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1966 | (*dd_idx)++; |
| 1967 | break; |
| 1968 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1969 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1970 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1971 | (*dd_idx)++; |
| 1972 | break; |
| 1973 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1974 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1975 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | break; |
| 1977 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1978 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1979 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1981 | case ALGORITHM_PARITY_0: |
| 1982 | pd_idx = 0; |
| 1983 | (*dd_idx)++; |
| 1984 | break; |
| 1985 | case ALGORITHM_PARITY_N: |
| 1986 | pd_idx = data_disks; |
| 1987 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1988 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1989 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1990 | } |
| 1991 | break; |
| 1992 | case 6: |
| 1993 | |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1994 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1995 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1996 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1997 | qd_idx = pd_idx + 1; |
| 1998 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1999 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2000 | qd_idx = 0; |
| 2001 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2002 | (*dd_idx) += 2; /* D D P Q D */ |
| 2003 | break; |
| 2004 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2005 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2006 | qd_idx = pd_idx + 1; |
| 2007 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2008 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2009 | qd_idx = 0; |
| 2010 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2011 | (*dd_idx) += 2; /* D D P Q D */ |
| 2012 | break; |
| 2013 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2014 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2015 | qd_idx = (pd_idx + 1) % raid_disks; |
| 2016 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2017 | break; |
| 2018 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2019 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2020 | qd_idx = (pd_idx + 1) % raid_disks; |
| 2021 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2022 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2023 | |
| 2024 | case ALGORITHM_PARITY_0: |
| 2025 | pd_idx = 0; |
| 2026 | qd_idx = 1; |
| 2027 | (*dd_idx) += 2; |
| 2028 | break; |
| 2029 | case ALGORITHM_PARITY_N: |
| 2030 | pd_idx = data_disks; |
| 2031 | qd_idx = data_disks + 1; |
| 2032 | break; |
| 2033 | |
| 2034 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 2035 | /* Exactly the same as RIGHT_ASYMMETRIC, but or |
| 2036 | * of blocks for computing Q is different. |
| 2037 | */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2038 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2039 | qd_idx = pd_idx + 1; |
| 2040 | if (pd_idx == raid_disks-1) { |
| 2041 | (*dd_idx)++; /* Q D D D P */ |
| 2042 | qd_idx = 0; |
| 2043 | } else if (*dd_idx >= pd_idx) |
| 2044 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2045 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2046 | break; |
| 2047 | |
| 2048 | case ALGORITHM_ROTATING_N_RESTART: |
| 2049 | /* Same a left_asymmetric, by first stripe is |
| 2050 | * D D D P Q rather than |
| 2051 | * Q D D D P |
| 2052 | */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2053 | stripe2 += 1; |
| 2054 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2055 | qd_idx = pd_idx + 1; |
| 2056 | if (pd_idx == raid_disks-1) { |
| 2057 | (*dd_idx)++; /* Q D D D P */ |
| 2058 | qd_idx = 0; |
| 2059 | } else if (*dd_idx >= pd_idx) |
| 2060 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2061 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2062 | break; |
| 2063 | |
| 2064 | case ALGORITHM_ROTATING_N_CONTINUE: |
| 2065 | /* Same as left_symmetric but Q is before P */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2066 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2067 | qd_idx = (pd_idx + raid_disks - 1) % raid_disks; |
| 2068 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2069 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2070 | break; |
| 2071 | |
| 2072 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 2073 | /* RAID5 left_asymmetric, with Q on last device */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2074 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2075 | if (*dd_idx >= pd_idx) |
| 2076 | (*dd_idx)++; |
| 2077 | qd_idx = raid_disks - 1; |
| 2078 | break; |
| 2079 | |
| 2080 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2081 | pd_idx = sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2082 | if (*dd_idx >= pd_idx) |
| 2083 | (*dd_idx)++; |
| 2084 | qd_idx = raid_disks - 1; |
| 2085 | break; |
| 2086 | |
| 2087 | case ALGORITHM_LEFT_SYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2088 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2089 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 2090 | qd_idx = raid_disks - 1; |
| 2091 | break; |
| 2092 | |
| 2093 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2094 | pd_idx = sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2095 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 2096 | qd_idx = raid_disks - 1; |
| 2097 | break; |
| 2098 | |
| 2099 | case ALGORITHM_PARITY_0_6: |
| 2100 | pd_idx = 0; |
| 2101 | (*dd_idx)++; |
| 2102 | qd_idx = raid_disks - 1; |
| 2103 | break; |
| 2104 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2105 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2106 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2107 | } |
| 2108 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2109 | } |
| 2110 | |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2111 | if (sh) { |
| 2112 | sh->pd_idx = pd_idx; |
| 2113 | sh->qd_idx = qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2114 | sh->ddf_layout = ddf_layout; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2115 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 | /* |
| 2117 | * Finally, compute the new sector number |
| 2118 | */ |
| 2119 | new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset; |
| 2120 | return new_sector; |
| 2121 | } |
| 2122 | |
| 2123 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2124 | 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] | 2125 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2126 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 2127 | int raid_disks = sh->disks; |
| 2128 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | sector_t new_sector = sh->sector, check; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 2130 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
| 2131 | : conf->chunk_sectors; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2132 | int algorithm = previous ? conf->prev_algo |
| 2133 | : conf->algorithm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2134 | sector_t stripe; |
| 2135 | int chunk_offset; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2136 | sector_t chunk_number; |
| 2137 | int dummy1, dd_idx = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2138 | sector_t r_sector; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2139 | struct stripe_head sh2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | chunk_offset = sector_div(new_sector, sectors_per_chunk); |
| 2143 | stripe = new_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2145 | if (i == sh->pd_idx) |
| 2146 | return 0; |
| 2147 | switch(conf->level) { |
| 2148 | case 4: break; |
| 2149 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2150 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2151 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 2152 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 2153 | if (i > sh->pd_idx) |
| 2154 | i--; |
| 2155 | break; |
| 2156 | case ALGORITHM_LEFT_SYMMETRIC: |
| 2157 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 2158 | if (i < sh->pd_idx) |
| 2159 | i += raid_disks; |
| 2160 | i -= (sh->pd_idx + 1); |
| 2161 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2162 | case ALGORITHM_PARITY_0: |
| 2163 | i -= 1; |
| 2164 | break; |
| 2165 | case ALGORITHM_PARITY_N: |
| 2166 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2167 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2168 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2169 | } |
| 2170 | break; |
| 2171 | case 6: |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2172 | if (i == sh->qd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2173 | return 0; /* It is the Q disk */ |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2174 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2175 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 2176 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2177 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 2178 | case ALGORITHM_ROTATING_N_RESTART: |
| 2179 | if (sh->pd_idx == raid_disks-1) |
| 2180 | i--; /* Q D D D P */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2181 | else if (i > sh->pd_idx) |
| 2182 | i -= 2; /* D D P Q D */ |
| 2183 | break; |
| 2184 | case ALGORITHM_LEFT_SYMMETRIC: |
| 2185 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 2186 | if (sh->pd_idx == raid_disks-1) |
| 2187 | i--; /* Q D D D P */ |
| 2188 | else { |
| 2189 | /* D D P Q D */ |
| 2190 | if (i < sh->pd_idx) |
| 2191 | i += raid_disks; |
| 2192 | i -= (sh->pd_idx + 2); |
| 2193 | } |
| 2194 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2195 | case ALGORITHM_PARITY_0: |
| 2196 | i -= 2; |
| 2197 | break; |
| 2198 | case ALGORITHM_PARITY_N: |
| 2199 | break; |
| 2200 | case ALGORITHM_ROTATING_N_CONTINUE: |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 2201 | /* Like left_symmetric, but P is before Q */ |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2202 | if (sh->pd_idx == 0) |
| 2203 | i--; /* P D D D Q */ |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 2204 | else { |
| 2205 | /* D D Q P D */ |
| 2206 | if (i < sh->pd_idx) |
| 2207 | i += raid_disks; |
| 2208 | i -= (sh->pd_idx + 1); |
| 2209 | } |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2210 | break; |
| 2211 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 2212 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 2213 | if (i > sh->pd_idx) |
| 2214 | i--; |
| 2215 | break; |
| 2216 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 2217 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 2218 | if (i < sh->pd_idx) |
| 2219 | i += data_disks + 1; |
| 2220 | i -= (sh->pd_idx + 1); |
| 2221 | break; |
| 2222 | case ALGORITHM_PARITY_0_6: |
| 2223 | i -= 1; |
| 2224 | break; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2225 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2226 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2227 | } |
| 2228 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2229 | } |
| 2230 | |
| 2231 | chunk_number = stripe * data_disks + i; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2232 | r_sector = chunk_number * sectors_per_chunk + chunk_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2233 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2234 | check = raid5_compute_sector(conf, r_sector, |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2235 | previous, &dummy1, &sh2); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2236 | if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx |
| 2237 | || sh2.qd_idx != sh->qd_idx) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 2238 | printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n", |
| 2239 | mdname(conf->mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2240 | return 0; |
| 2241 | } |
| 2242 | return r_sector; |
| 2243 | } |
| 2244 | |
| 2245 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2246 | static void |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2247 | schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2248 | int rcw, int expand) |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2249 | { |
| 2250 | int i, pd_idx = sh->pd_idx, disks = sh->disks; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2251 | struct r5conf *conf = sh->raid_conf; |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2252 | int level = conf->level; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2253 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2254 | if (rcw) { |
| 2255 | /* if we are not expanding this is a proper write request, and |
| 2256 | * there will be bios with new data to be drained into the |
| 2257 | * stripe cache |
| 2258 | */ |
| 2259 | if (!expand) { |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2260 | sh->reconstruct_state = reconstruct_state_drain_run; |
| 2261 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
| 2262 | } else |
| 2263 | sh->reconstruct_state = reconstruct_state_run; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2264 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2265 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2266 | |
| 2267 | for (i = disks; i--; ) { |
| 2268 | struct r5dev *dev = &sh->dev[i]; |
| 2269 | |
| 2270 | if (dev->towrite) { |
| 2271 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2272 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2273 | if (!expand) |
| 2274 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2275 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2276 | } |
| 2277 | } |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2278 | if (s->locked + conf->max_degraded == disks) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2279 | if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state)) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2280 | atomic_inc(&conf->pending_full_writes); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2281 | } else { |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2282 | BUG_ON(level == 6); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2283 | BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) || |
| 2284 | test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags))); |
| 2285 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2286 | sh->reconstruct_state = reconstruct_state_prexor_drain_run; |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2287 | set_bit(STRIPE_OP_PREXOR, &s->ops_request); |
| 2288 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2289 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2290 | |
| 2291 | for (i = disks; i--; ) { |
| 2292 | struct r5dev *dev = &sh->dev[i]; |
| 2293 | if (i == pd_idx) |
| 2294 | continue; |
| 2295 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2296 | if (dev->towrite && |
| 2297 | (test_bit(R5_UPTODATE, &dev->flags) || |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2298 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2299 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2300 | set_bit(R5_LOCKED, &dev->flags); |
| 2301 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2302 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2303 | } |
| 2304 | } |
| 2305 | } |
| 2306 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2307 | /* keep the parity disk(s) locked while asynchronous operations |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2308 | * are in flight |
| 2309 | */ |
| 2310 | set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); |
| 2311 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2312 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2313 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2314 | if (level == 6) { |
| 2315 | int qd_idx = sh->qd_idx; |
| 2316 | struct r5dev *dev = &sh->dev[qd_idx]; |
| 2317 | |
| 2318 | set_bit(R5_LOCKED, &dev->flags); |
| 2319 | clear_bit(R5_UPTODATE, &dev->flags); |
| 2320 | s->locked++; |
| 2321 | } |
| 2322 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2323 | pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n", |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 2324 | __func__, (unsigned long long)sh->sector, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2325 | s->locked, s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2326 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2328 | /* |
| 2329 | * Each stripe/dev can have one or more bion attached. |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2330 | * toread/towrite point to the first in a chain. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2331 | * The bi_next chain must be in order. |
| 2332 | */ |
| 2333 | static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite) |
| 2334 | { |
| 2335 | struct bio **bip; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2336 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2337 | int firstwrite=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2338 | |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 2339 | pr_debug("adding bi b#%llu to stripe s#%llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2340 | (unsigned long long)bi->bi_sector, |
| 2341 | (unsigned long long)sh->sector); |
| 2342 | |
| 2343 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2344 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2345 | if (forwrite) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2346 | bip = &sh->dev[dd_idx].towrite; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2347 | if (*bip == NULL && sh->dev[dd_idx].written == NULL) |
| 2348 | firstwrite = 1; |
| 2349 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2350 | bip = &sh->dev[dd_idx].toread; |
| 2351 | while (*bip && (*bip)->bi_sector < bi->bi_sector) { |
| 2352 | if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector) |
| 2353 | goto overlap; |
| 2354 | bip = & (*bip)->bi_next; |
| 2355 | } |
| 2356 | if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9)) |
| 2357 | goto overlap; |
| 2358 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 2359 | BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2360 | if (*bip) |
| 2361 | bi->bi_next = *bip; |
| 2362 | *bip = bi; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2363 | bi->bi_phys_segments++; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2364 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2365 | if (forwrite) { |
| 2366 | /* check if page is covered */ |
| 2367 | sector_t sector = sh->dev[dd_idx].sector; |
| 2368 | for (bi=sh->dev[dd_idx].towrite; |
| 2369 | sector < sh->dev[dd_idx].sector + STRIPE_SECTORS && |
| 2370 | bi && bi->bi_sector <= sector; |
| 2371 | bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) { |
| 2372 | if (bi->bi_sector + (bi->bi_size>>9) >= sector) |
| 2373 | sector = bi->bi_sector + (bi->bi_size>>9); |
| 2374 | } |
| 2375 | if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS) |
| 2376 | set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags); |
| 2377 | } |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 2378 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 2379 | |
| 2380 | pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n", |
| 2381 | (unsigned long long)(*bip)->bi_sector, |
| 2382 | (unsigned long long)sh->sector, dd_idx); |
| 2383 | |
| 2384 | if (conf->mddev->bitmap && firstwrite) { |
| 2385 | bitmap_startwrite(conf->mddev->bitmap, sh->sector, |
| 2386 | STRIPE_SECTORS, 0); |
| 2387 | sh->bm_seq = conf->seq_flush+1; |
| 2388 | set_bit(STRIPE_BIT_DELAY, &sh->state); |
| 2389 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2390 | return 1; |
| 2391 | |
| 2392 | overlap: |
| 2393 | set_bit(R5_Overlap, &sh->dev[dd_idx].flags); |
| 2394 | spin_unlock_irq(&conf->device_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2395 | return 0; |
| 2396 | } |
| 2397 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2398 | static void end_reshape(struct r5conf *conf); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 2399 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2400 | static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2401 | struct stripe_head *sh) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2402 | { |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2403 | int sectors_per_chunk = |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 2404 | previous ? conf->prev_chunk_sectors : conf->chunk_sectors; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2405 | int dd_idx; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 2406 | int chunk_offset = sector_div(stripe, sectors_per_chunk); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2407 | int disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 2408 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2409 | raid5_compute_sector(conf, |
| 2410 | stripe * (disks - conf->max_degraded) |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 2411 | *sectors_per_chunk + chunk_offset, |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2412 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2413 | &dd_idx, sh); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2414 | } |
| 2415 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2416 | static void |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2417 | handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2418 | struct stripe_head_state *s, int disks, |
| 2419 | struct bio **return_bi) |
| 2420 | { |
| 2421 | int i; |
| 2422 | for (i = disks; i--; ) { |
| 2423 | struct bio *bi; |
| 2424 | int bitmap_end = 0; |
| 2425 | |
| 2426 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2427 | struct md_rdev *rdev; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2428 | rcu_read_lock(); |
| 2429 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 2430 | if (rdev && test_bit(In_sync, &rdev->flags)) |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2431 | atomic_inc(&rdev->nr_pending); |
| 2432 | else |
| 2433 | rdev = NULL; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2434 | rcu_read_unlock(); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2435 | if (rdev) { |
| 2436 | if (!rdev_set_badblocks( |
| 2437 | rdev, |
| 2438 | sh->sector, |
| 2439 | STRIPE_SECTORS, 0)) |
| 2440 | md_error(conf->mddev, rdev); |
| 2441 | rdev_dec_pending(rdev, conf->mddev); |
| 2442 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2443 | } |
| 2444 | spin_lock_irq(&conf->device_lock); |
| 2445 | /* fail all writes first */ |
| 2446 | bi = sh->dev[i].towrite; |
| 2447 | sh->dev[i].towrite = NULL; |
| 2448 | if (bi) { |
| 2449 | s->to_write--; |
| 2450 | bitmap_end = 1; |
| 2451 | } |
| 2452 | |
| 2453 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 2454 | wake_up(&conf->wait_for_overlap); |
| 2455 | |
| 2456 | while (bi && bi->bi_sector < |
| 2457 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2458 | struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); |
| 2459 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2460 | if (!raid5_dec_bi_phys_segments(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2461 | md_write_end(conf->mddev); |
| 2462 | bi->bi_next = *return_bi; |
| 2463 | *return_bi = bi; |
| 2464 | } |
| 2465 | bi = nextbi; |
| 2466 | } |
| 2467 | /* and fail all 'written' */ |
| 2468 | bi = sh->dev[i].written; |
| 2469 | sh->dev[i].written = NULL; |
| 2470 | if (bi) bitmap_end = 1; |
| 2471 | while (bi && bi->bi_sector < |
| 2472 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2473 | struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector); |
| 2474 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2475 | if (!raid5_dec_bi_phys_segments(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2476 | md_write_end(conf->mddev); |
| 2477 | bi->bi_next = *return_bi; |
| 2478 | *return_bi = bi; |
| 2479 | } |
| 2480 | bi = bi2; |
| 2481 | } |
| 2482 | |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2483 | /* fail any reads if this device is non-operational and |
| 2484 | * the data has not reached the cache yet. |
| 2485 | */ |
| 2486 | if (!test_bit(R5_Wantfill, &sh->dev[i].flags) && |
| 2487 | (!test_bit(R5_Insync, &sh->dev[i].flags) || |
| 2488 | test_bit(R5_ReadError, &sh->dev[i].flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2489 | bi = sh->dev[i].toread; |
| 2490 | sh->dev[i].toread = NULL; |
| 2491 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 2492 | wake_up(&conf->wait_for_overlap); |
| 2493 | if (bi) s->to_read--; |
| 2494 | while (bi && bi->bi_sector < |
| 2495 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2496 | struct bio *nextbi = |
| 2497 | r5_next_bio(bi, sh->dev[i].sector); |
| 2498 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2499 | if (!raid5_dec_bi_phys_segments(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2500 | bi->bi_next = *return_bi; |
| 2501 | *return_bi = bi; |
| 2502 | } |
| 2503 | bi = nextbi; |
| 2504 | } |
| 2505 | } |
| 2506 | spin_unlock_irq(&conf->device_lock); |
| 2507 | if (bitmap_end) |
| 2508 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 2509 | STRIPE_SECTORS, 0, 0); |
NeilBrown | 8cfa7b0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2510 | /* If we were in the middle of a write the parity block might |
| 2511 | * still be locked - so just clear all R5_LOCKED flags |
| 2512 | */ |
| 2513 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2514 | } |
| 2515 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2516 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 2517 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 2518 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2519 | } |
| 2520 | |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2521 | static void |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2522 | handle_failed_sync(struct r5conf *conf, struct stripe_head *sh, |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2523 | struct stripe_head_state *s) |
| 2524 | { |
| 2525 | int abort = 0; |
| 2526 | int i; |
| 2527 | |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2528 | clear_bit(STRIPE_SYNCING, &sh->state); |
| 2529 | s->syncing = 0; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2530 | s->replacing = 0; |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2531 | /* There is nothing more to do for sync/check/repair. |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 2532 | * Don't even need to abort as that is handled elsewhere |
| 2533 | * if needed, and not always wanted e.g. if there is a known |
| 2534 | * bad block here. |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2535 | * For recover/replace we need to record a bad block on all |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2536 | * non-sync devices, or abort the recovery |
| 2537 | */ |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 2538 | if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) { |
| 2539 | /* During recovery devices cannot be removed, so |
| 2540 | * locking and refcounting of rdevs is not needed |
| 2541 | */ |
| 2542 | for (i = 0; i < conf->raid_disks; i++) { |
| 2543 | struct md_rdev *rdev = conf->disks[i].rdev; |
| 2544 | if (rdev |
| 2545 | && !test_bit(Faulty, &rdev->flags) |
| 2546 | && !test_bit(In_sync, &rdev->flags) |
| 2547 | && !rdev_set_badblocks(rdev, sh->sector, |
| 2548 | STRIPE_SECTORS, 0)) |
| 2549 | abort = 1; |
| 2550 | rdev = conf->disks[i].replacement; |
| 2551 | if (rdev |
| 2552 | && !test_bit(Faulty, &rdev->flags) |
| 2553 | && !test_bit(In_sync, &rdev->flags) |
| 2554 | && !rdev_set_badblocks(rdev, sh->sector, |
| 2555 | STRIPE_SECTORS, 0)) |
| 2556 | abort = 1; |
| 2557 | } |
| 2558 | if (abort) |
| 2559 | conf->recovery_disabled = |
| 2560 | conf->mddev->recovery_disabled; |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2561 | } |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 2562 | md_done_sync(conf->mddev, STRIPE_SECTORS, !abort); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2563 | } |
| 2564 | |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2565 | static int want_replace(struct stripe_head *sh, int disk_idx) |
| 2566 | { |
| 2567 | struct md_rdev *rdev; |
| 2568 | int rv = 0; |
| 2569 | /* Doing recovery so rcu locking not required */ |
| 2570 | rdev = sh->raid_conf->disks[disk_idx].replacement; |
| 2571 | if (rdev |
| 2572 | && !test_bit(Faulty, &rdev->flags) |
| 2573 | && !test_bit(In_sync, &rdev->flags) |
| 2574 | && (rdev->recovery_offset <= sh->sector |
| 2575 | || rdev->mddev->recovery_cp <= sh->sector)) |
| 2576 | rv = 1; |
| 2577 | |
| 2578 | return rv; |
| 2579 | } |
| 2580 | |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2581 | /* 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] | 2582 | * to be read or computed to satisfy a request. |
| 2583 | * |
| 2584 | * Returns 1 when no more member devices need to be checked, otherwise returns |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2585 | * 0 to tell the loop in handle_stripe_fill to continue |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2586 | */ |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2587 | static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s, |
| 2588 | int disk_idx, int disks) |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2589 | { |
| 2590 | struct r5dev *dev = &sh->dev[disk_idx]; |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2591 | struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]], |
| 2592 | &sh->dev[s->failed_num[1]] }; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2593 | |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2594 | /* is the data in this block needed, and can we get it? */ |
| 2595 | if (!test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2596 | !test_bit(R5_UPTODATE, &dev->flags) && |
| 2597 | (dev->toread || |
| 2598 | (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) || |
| 2599 | s->syncing || s->expanding || |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2600 | (s->replacing && want_replace(sh, disk_idx)) || |
NeilBrown | 5d35e09 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2601 | (s->failed >= 1 && fdev[0]->toread) || |
| 2602 | (s->failed >= 2 && fdev[1]->toread) || |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2603 | (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite && |
| 2604 | !test_bit(R5_OVERWRITE, &fdev[0]->flags)) || |
| 2605 | (sh->raid_conf->level == 6 && s->failed && s->to_write))) { |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2606 | /* we would like to get this block, possibly by computing it, |
| 2607 | * otherwise read it if the backing disk is insync |
| 2608 | */ |
| 2609 | BUG_ON(test_bit(R5_Wantcompute, &dev->flags)); |
| 2610 | BUG_ON(test_bit(R5_Wantread, &dev->flags)); |
| 2611 | if ((s->uptodate == disks - 1) && |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2612 | (s->failed && (disk_idx == s->failed_num[0] || |
| 2613 | disk_idx == s->failed_num[1]))) { |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2614 | /* have disk failed, and we're requested to fetch it; |
| 2615 | * do compute it |
| 2616 | */ |
| 2617 | pr_debug("Computing stripe %llu block %d\n", |
| 2618 | (unsigned long long)sh->sector, disk_idx); |
| 2619 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2620 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2621 | set_bit(R5_Wantcompute, &dev->flags); |
| 2622 | sh->ops.target = disk_idx; |
| 2623 | sh->ops.target2 = -1; /* no 2nd target */ |
| 2624 | s->req_compute = 1; |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2625 | /* Careful: from this point on 'uptodate' is in the eye |
| 2626 | * of raid_run_ops which services 'compute' operations |
| 2627 | * before writes. R5_Wantcompute flags a block that will |
| 2628 | * be R5_UPTODATE by the time it is needed for a |
| 2629 | * subsequent operation. |
| 2630 | */ |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2631 | s->uptodate++; |
| 2632 | return 1; |
| 2633 | } else if (s->uptodate == disks-2 && s->failed >= 2) { |
| 2634 | /* Computing 2-failure is *very* expensive; only |
| 2635 | * do it if failed >= 2 |
| 2636 | */ |
| 2637 | int other; |
| 2638 | for (other = disks; other--; ) { |
| 2639 | if (other == disk_idx) |
| 2640 | continue; |
| 2641 | if (!test_bit(R5_UPTODATE, |
| 2642 | &sh->dev[other].flags)) |
| 2643 | break; |
| 2644 | } |
| 2645 | BUG_ON(other < 0); |
| 2646 | pr_debug("Computing stripe %llu blocks %d,%d\n", |
| 2647 | (unsigned long long)sh->sector, |
| 2648 | disk_idx, other); |
| 2649 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2650 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2651 | set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags); |
| 2652 | set_bit(R5_Wantcompute, &sh->dev[other].flags); |
| 2653 | sh->ops.target = disk_idx; |
| 2654 | sh->ops.target2 = other; |
| 2655 | s->uptodate += 2; |
| 2656 | s->req_compute = 1; |
| 2657 | return 1; |
| 2658 | } else if (test_bit(R5_Insync, &dev->flags)) { |
| 2659 | set_bit(R5_LOCKED, &dev->flags); |
| 2660 | set_bit(R5_Wantread, &dev->flags); |
| 2661 | s->locked++; |
| 2662 | pr_debug("Reading block %d (sync=%d)\n", |
| 2663 | disk_idx, s->syncing); |
| 2664 | } |
| 2665 | } |
| 2666 | |
| 2667 | return 0; |
| 2668 | } |
| 2669 | |
| 2670 | /** |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2671 | * handle_stripe_fill - read or compute data to satisfy pending requests. |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2672 | */ |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2673 | static void handle_stripe_fill(struct stripe_head *sh, |
| 2674 | struct stripe_head_state *s, |
| 2675 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2676 | { |
| 2677 | int i; |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2678 | |
| 2679 | /* look for blocks to read/compute, skip this if a compute |
| 2680 | * is already in flight, or if the stripe contents are in the |
| 2681 | * midst of changing due to a write |
| 2682 | */ |
| 2683 | if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state && |
| 2684 | !sh->reconstruct_state) |
| 2685 | for (i = disks; i--; ) |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2686 | if (fetch_block(sh, s, i, disks)) |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2687 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2688 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2689 | } |
| 2690 | |
| 2691 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2692 | /* handle_stripe_clean_event |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2693 | * any written block on an uptodate or failed drive can be returned. |
| 2694 | * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but |
| 2695 | * never LOCKED, so we don't need to test 'failed' directly. |
| 2696 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2697 | static void handle_stripe_clean_event(struct r5conf *conf, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2698 | struct stripe_head *sh, int disks, struct bio **return_bi) |
| 2699 | { |
| 2700 | int i; |
| 2701 | struct r5dev *dev; |
| 2702 | |
| 2703 | for (i = disks; i--; ) |
| 2704 | if (sh->dev[i].written) { |
| 2705 | dev = &sh->dev[i]; |
| 2706 | if (!test_bit(R5_LOCKED, &dev->flags) && |
| 2707 | test_bit(R5_UPTODATE, &dev->flags)) { |
| 2708 | /* We can return any write requests */ |
| 2709 | struct bio *wbi, *wbi2; |
| 2710 | int bitmap_end = 0; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2711 | pr_debug("Return write for disc %d\n", i); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2712 | spin_lock_irq(&conf->device_lock); |
| 2713 | wbi = dev->written; |
| 2714 | dev->written = NULL; |
| 2715 | while (wbi && wbi->bi_sector < |
| 2716 | dev->sector + STRIPE_SECTORS) { |
| 2717 | wbi2 = r5_next_bio(wbi, dev->sector); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2718 | if (!raid5_dec_bi_phys_segments(wbi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2719 | md_write_end(conf->mddev); |
| 2720 | wbi->bi_next = *return_bi; |
| 2721 | *return_bi = wbi; |
| 2722 | } |
| 2723 | wbi = wbi2; |
| 2724 | } |
| 2725 | if (dev->towrite == NULL) |
| 2726 | bitmap_end = 1; |
| 2727 | spin_unlock_irq(&conf->device_lock); |
| 2728 | if (bitmap_end) |
| 2729 | bitmap_endwrite(conf->mddev->bitmap, |
| 2730 | sh->sector, |
| 2731 | STRIPE_SECTORS, |
| 2732 | !test_bit(STRIPE_DEGRADED, &sh->state), |
| 2733 | 0); |
| 2734 | } |
| 2735 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2736 | |
| 2737 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 2738 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 2739 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2740 | } |
| 2741 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2742 | static void handle_stripe_dirtying(struct r5conf *conf, |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2743 | struct stripe_head *sh, |
| 2744 | struct stripe_head_state *s, |
| 2745 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2746 | { |
| 2747 | int rmw = 0, rcw = 0, i; |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2748 | if (conf->max_degraded == 2) { |
| 2749 | /* RAID6 requires 'rcw' in current implementation |
| 2750 | * Calculate the real rcw later - for now fake it |
| 2751 | * look like rcw is cheaper |
| 2752 | */ |
| 2753 | rcw = 1; rmw = 2; |
| 2754 | } else for (i = disks; i--; ) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2755 | /* would I have to read this buffer for read_modify_write */ |
| 2756 | struct r5dev *dev = &sh->dev[i]; |
| 2757 | if ((dev->towrite || i == sh->pd_idx) && |
| 2758 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2759 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2760 | test_bit(R5_Wantcompute, &dev->flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2761 | if (test_bit(R5_Insync, &dev->flags)) |
| 2762 | rmw++; |
| 2763 | else |
| 2764 | rmw += 2*disks; /* cannot read it */ |
| 2765 | } |
| 2766 | /* Would I have to read this buffer for reconstruct_write */ |
| 2767 | if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx && |
| 2768 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2769 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2770 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2771 | if (test_bit(R5_Insync, &dev->flags)) rcw++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2772 | else |
| 2773 | rcw += 2*disks; |
| 2774 | } |
| 2775 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2776 | pr_debug("for sector %llu, rmw=%d rcw=%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2777 | (unsigned long long)sh->sector, rmw, rcw); |
| 2778 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2779 | if (rmw < rcw && rmw > 0) |
| 2780 | /* prefer read-modify-write, but need to get some data */ |
| 2781 | for (i = disks; i--; ) { |
| 2782 | struct r5dev *dev = &sh->dev[i]; |
| 2783 | if ((dev->towrite || i == sh->pd_idx) && |
| 2784 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2785 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2786 | test_bit(R5_Wantcompute, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2787 | test_bit(R5_Insync, &dev->flags)) { |
| 2788 | if ( |
| 2789 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2790 | pr_debug("Read_old block " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2791 | "%d for r-m-w\n", i); |
| 2792 | set_bit(R5_LOCKED, &dev->flags); |
| 2793 | set_bit(R5_Wantread, &dev->flags); |
| 2794 | s->locked++; |
| 2795 | } else { |
| 2796 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2797 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2798 | } |
| 2799 | } |
| 2800 | } |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2801 | if (rcw <= rmw && rcw > 0) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2802 | /* want reconstruct write, but need to get some data */ |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2803 | rcw = 0; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2804 | for (i = disks; i--; ) { |
| 2805 | struct r5dev *dev = &sh->dev[i]; |
| 2806 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2807 | i != sh->pd_idx && i != sh->qd_idx && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2808 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2809 | !(test_bit(R5_UPTODATE, &dev->flags) || |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2810 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2811 | rcw++; |
| 2812 | if (!test_bit(R5_Insync, &dev->flags)) |
| 2813 | continue; /* it's a failed drive */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2814 | if ( |
| 2815 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2816 | pr_debug("Read_old block " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2817 | "%d for Reconstruct\n", i); |
| 2818 | set_bit(R5_LOCKED, &dev->flags); |
| 2819 | set_bit(R5_Wantread, &dev->flags); |
| 2820 | s->locked++; |
| 2821 | } else { |
| 2822 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2823 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2824 | } |
| 2825 | } |
| 2826 | } |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2827 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2828 | /* now if nothing is locked, and if we have enough data, |
| 2829 | * we can start a write request |
| 2830 | */ |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2831 | /* since handle_stripe can be called at any time we need to handle the |
| 2832 | * case where a compute block operation has been submitted and then a |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2833 | * subsequent call wants to start a write request. raid_run_ops only |
| 2834 | * handles the case where compute block and reconstruct are requested |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2835 | * simultaneously. If this is not the case then new writes need to be |
| 2836 | * held off until the compute completes. |
| 2837 | */ |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2838 | if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) && |
| 2839 | (s->locked == 0 && (rcw == 0 || rmw == 0) && |
| 2840 | !test_bit(STRIPE_BIT_DELAY, &sh->state))) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2841 | schedule_reconstruction(sh, s, rcw == 0, 0); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2842 | } |
| 2843 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2844 | static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2845 | struct stripe_head_state *s, int disks) |
| 2846 | { |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2847 | struct r5dev *dev = NULL; |
Dan Williams | e89f896 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2848 | |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2849 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2850 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2851 | switch (sh->check_state) { |
| 2852 | case check_state_idle: |
| 2853 | /* start a new check operation if there are no failures */ |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2854 | if (s->failed == 0) { |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2855 | BUG_ON(s->uptodate != disks); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2856 | sh->check_state = check_state_run; |
| 2857 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2858 | clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2859 | s->uptodate--; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2860 | break; |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2861 | } |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2862 | dev = &sh->dev[s->failed_num[0]]; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2863 | /* fall through */ |
| 2864 | case check_state_compute_result: |
| 2865 | sh->check_state = check_state_idle; |
| 2866 | if (!dev) |
| 2867 | dev = &sh->dev[sh->pd_idx]; |
| 2868 | |
| 2869 | /* check that a write has not made the stripe insync */ |
| 2870 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 2871 | break; |
| 2872 | |
| 2873 | /* either failed parity check, or recovery is happening */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2874 | BUG_ON(!test_bit(R5_UPTODATE, &dev->flags)); |
| 2875 | BUG_ON(s->uptodate != disks); |
| 2876 | |
| 2877 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2878 | s->locked++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2879 | set_bit(R5_Wantwrite, &dev->flags); |
Dan Williams | 830ea01 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2880 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2881 | clear_bit(STRIPE_DEGRADED, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2882 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2883 | break; |
| 2884 | case check_state_run: |
| 2885 | break; /* we will be called again upon completion */ |
| 2886 | case check_state_check_result: |
| 2887 | sh->check_state = check_state_idle; |
| 2888 | |
| 2889 | /* if a failure occurred during the check operation, leave |
| 2890 | * STRIPE_INSYNC not set and let the stripe be handled again |
| 2891 | */ |
| 2892 | if (s->failed) |
| 2893 | break; |
| 2894 | |
| 2895 | /* handle a successful check operation, if parity is correct |
| 2896 | * we are done. Otherwise update the mismatch count and repair |
| 2897 | * parity if !MD_RECOVERY_CHECK |
| 2898 | */ |
Dan Williams | ad283ea | 2009-08-29 19:09:26 -0700 | [diff] [blame] | 2899 | if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0) |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2900 | /* parity is correct (on disc, |
| 2901 | * not in buffer any more) |
| 2902 | */ |
| 2903 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2904 | else { |
| 2905 | conf->mddev->resync_mismatches += STRIPE_SECTORS; |
| 2906 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 2907 | /* don't try to repair!! */ |
| 2908 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2909 | else { |
| 2910 | sh->check_state = check_state_compute_run; |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2911 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2912 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2913 | set_bit(R5_Wantcompute, |
| 2914 | &sh->dev[sh->pd_idx].flags); |
| 2915 | sh->ops.target = sh->pd_idx; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2916 | sh->ops.target2 = -1; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2917 | s->uptodate++; |
| 2918 | } |
| 2919 | } |
| 2920 | break; |
| 2921 | case check_state_compute_run: |
| 2922 | break; |
| 2923 | default: |
| 2924 | printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n", |
| 2925 | __func__, sh->check_state, |
| 2926 | (unsigned long long) sh->sector); |
| 2927 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2928 | } |
| 2929 | } |
| 2930 | |
| 2931 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2932 | static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2933 | struct stripe_head_state *s, |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2934 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2935 | { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2936 | int pd_idx = sh->pd_idx; |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 2937 | int qd_idx = sh->qd_idx; |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2938 | struct r5dev *dev; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2939 | |
| 2940 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2941 | |
| 2942 | BUG_ON(s->failed > 2); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2943 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2944 | /* Want to check and possibly repair P and Q. |
| 2945 | * However there could be one 'failed' device, in which |
| 2946 | * case we can only check one of them, possibly using the |
| 2947 | * other to generate missing data |
| 2948 | */ |
| 2949 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2950 | switch (sh->check_state) { |
| 2951 | case check_state_idle: |
| 2952 | /* start a new check operation if there are < 2 failures */ |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2953 | if (s->failed == s->q_failed) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2954 | /* The only possible failed device holds Q, so it |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2955 | * makes sense to check P (If anything else were failed, |
| 2956 | * we would have used P to recreate it). |
| 2957 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2958 | sh->check_state = check_state_run; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2959 | } |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2960 | if (!s->q_failed && s->failed < 2) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2961 | /* Q is not failed, and we didn't use it to generate |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2962 | * anything, so it makes sense to check it |
| 2963 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2964 | if (sh->check_state == check_state_run) |
| 2965 | sh->check_state = check_state_run_pq; |
| 2966 | else |
| 2967 | sh->check_state = check_state_run_q; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2968 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2969 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2970 | /* discard potentially stale zero_sum_result */ |
| 2971 | sh->ops.zero_sum_result = 0; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2972 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2973 | if (sh->check_state == check_state_run) { |
| 2974 | /* async_xor_zero_sum destroys the contents of P */ |
| 2975 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
| 2976 | s->uptodate--; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2977 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2978 | if (sh->check_state >= check_state_run && |
| 2979 | sh->check_state <= check_state_run_pq) { |
| 2980 | /* async_syndrome_zero_sum preserves P and Q, so |
| 2981 | * no need to mark them !uptodate here |
| 2982 | */ |
| 2983 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
| 2984 | break; |
| 2985 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2986 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2987 | /* we have 2-disk failure */ |
| 2988 | BUG_ON(s->failed != 2); |
| 2989 | /* fall through */ |
| 2990 | case check_state_compute_result: |
| 2991 | sh->check_state = check_state_idle; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2992 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2993 | /* check that a write has not made the stripe insync */ |
| 2994 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 2995 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2996 | |
| 2997 | /* now write out any block on a failed drive, |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2998 | * or P or Q if they were recomputed |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2999 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3000 | 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] | 3001 | if (s->failed == 2) { |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3002 | dev = &sh->dev[s->failed_num[1]]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3003 | s->locked++; |
| 3004 | set_bit(R5_LOCKED, &dev->flags); |
| 3005 | set_bit(R5_Wantwrite, &dev->flags); |
| 3006 | } |
| 3007 | if (s->failed >= 1) { |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3008 | dev = &sh->dev[s->failed_num[0]]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3009 | s->locked++; |
| 3010 | set_bit(R5_LOCKED, &dev->flags); |
| 3011 | set_bit(R5_Wantwrite, &dev->flags); |
| 3012 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3013 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3014 | dev = &sh->dev[pd_idx]; |
| 3015 | s->locked++; |
| 3016 | set_bit(R5_LOCKED, &dev->flags); |
| 3017 | set_bit(R5_Wantwrite, &dev->flags); |
| 3018 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3019 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3020 | dev = &sh->dev[qd_idx]; |
| 3021 | s->locked++; |
| 3022 | set_bit(R5_LOCKED, &dev->flags); |
| 3023 | set_bit(R5_Wantwrite, &dev->flags); |
| 3024 | } |
| 3025 | clear_bit(STRIPE_DEGRADED, &sh->state); |
| 3026 | |
| 3027 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3028 | break; |
| 3029 | case check_state_run: |
| 3030 | case check_state_run_q: |
| 3031 | case check_state_run_pq: |
| 3032 | break; /* we will be called again upon completion */ |
| 3033 | case check_state_check_result: |
| 3034 | sh->check_state = check_state_idle; |
| 3035 | |
| 3036 | /* handle a successful check operation, if parity is correct |
| 3037 | * we are done. Otherwise update the mismatch count and repair |
| 3038 | * parity if !MD_RECOVERY_CHECK |
| 3039 | */ |
| 3040 | if (sh->ops.zero_sum_result == 0) { |
| 3041 | /* both parities are correct */ |
| 3042 | if (!s->failed) |
| 3043 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3044 | else { |
| 3045 | /* in contrast to the raid5 case we can validate |
| 3046 | * parity, but still have a failure to write |
| 3047 | * back |
| 3048 | */ |
| 3049 | sh->check_state = check_state_compute_result; |
| 3050 | /* Returning at this point means that we may go |
| 3051 | * off and bring p and/or q uptodate again so |
| 3052 | * we make sure to check zero_sum_result again |
| 3053 | * to verify if p or q need writeback |
| 3054 | */ |
| 3055 | } |
| 3056 | } else { |
| 3057 | conf->mddev->resync_mismatches += STRIPE_SECTORS; |
| 3058 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 3059 | /* don't try to repair!! */ |
| 3060 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3061 | else { |
| 3062 | int *target = &sh->ops.target; |
| 3063 | |
| 3064 | sh->ops.target = -1; |
| 3065 | sh->ops.target2 = -1; |
| 3066 | sh->check_state = check_state_compute_run; |
| 3067 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 3068 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 3069 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
| 3070 | set_bit(R5_Wantcompute, |
| 3071 | &sh->dev[pd_idx].flags); |
| 3072 | *target = pd_idx; |
| 3073 | target = &sh->ops.target2; |
| 3074 | s->uptodate++; |
| 3075 | } |
| 3076 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
| 3077 | set_bit(R5_Wantcompute, |
| 3078 | &sh->dev[qd_idx].flags); |
| 3079 | *target = qd_idx; |
| 3080 | s->uptodate++; |
| 3081 | } |
| 3082 | } |
| 3083 | } |
| 3084 | break; |
| 3085 | case check_state_compute_run: |
| 3086 | break; |
| 3087 | default: |
| 3088 | printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n", |
| 3089 | __func__, sh->check_state, |
| 3090 | (unsigned long long) sh->sector); |
| 3091 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3092 | } |
| 3093 | } |
| 3094 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3095 | static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3096 | { |
| 3097 | int i; |
| 3098 | |
| 3099 | /* We have read all the blocks in this stripe and now we need to |
| 3100 | * copy some of them into a target stripe for expand. |
| 3101 | */ |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3102 | struct dma_async_tx_descriptor *tx = NULL; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3103 | clear_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 3104 | for (i = 0; i < sh->disks; i++) |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 3105 | if (i != sh->pd_idx && i != sh->qd_idx) { |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3106 | int dd_idx, j; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3107 | struct stripe_head *sh2; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 3108 | struct async_submit_ctl submit; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3109 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 3110 | sector_t bn = compute_blocknr(sh, i, 1); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3111 | sector_t s = raid5_compute_sector(conf, bn, 0, |
| 3112 | &dd_idx, NULL); |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 3113 | sh2 = get_active_stripe(conf, s, 0, 1, 1); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3114 | if (sh2 == NULL) |
| 3115 | /* so far only the early blocks of this stripe |
| 3116 | * have been requested. When later blocks |
| 3117 | * get requested, we will try again |
| 3118 | */ |
| 3119 | continue; |
| 3120 | if (!test_bit(STRIPE_EXPANDING, &sh2->state) || |
| 3121 | test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) { |
| 3122 | /* must have already done this block */ |
| 3123 | release_stripe(sh2); |
| 3124 | continue; |
| 3125 | } |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3126 | |
| 3127 | /* place all the copies on one channel */ |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 3128 | init_async_submit(&submit, 0, tx, NULL, NULL, NULL); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3129 | tx = async_memcpy(sh2->dev[dd_idx].page, |
Dan Williams | 88ba2aa | 2009-04-09 16:16:18 -0700 | [diff] [blame] | 3130 | sh->dev[i].page, 0, 0, STRIPE_SIZE, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 3131 | &submit); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3132 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3133 | set_bit(R5_Expanded, &sh2->dev[dd_idx].flags); |
| 3134 | set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags); |
| 3135 | for (j = 0; j < conf->raid_disks; j++) |
| 3136 | if (j != sh2->pd_idx && |
NeilBrown | 86c374b | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3137 | j != sh2->qd_idx && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3138 | !test_bit(R5_Expanded, &sh2->dev[j].flags)) |
| 3139 | break; |
| 3140 | if (j == conf->raid_disks) { |
| 3141 | set_bit(STRIPE_EXPAND_READY, &sh2->state); |
| 3142 | set_bit(STRIPE_HANDLE, &sh2->state); |
| 3143 | } |
| 3144 | release_stripe(sh2); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3145 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3146 | } |
NeilBrown | a2e0855 | 2007-09-11 15:23:36 -0700 | [diff] [blame] | 3147 | /* done submitting copies, wait for them to complete */ |
| 3148 | if (tx) { |
| 3149 | async_tx_ack(tx); |
| 3150 | dma_wait_for_async_tx(tx); |
| 3151 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3152 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3153 | |
| 3154 | /* |
| 3155 | * handle_stripe - do things to a stripe. |
| 3156 | * |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3157 | * We lock the stripe by setting STRIPE_ACTIVE and then examine the |
| 3158 | * state of various bits to see what needs to be done. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3159 | * Possible results: |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3160 | * return some read requests which now have data |
| 3161 | * return some write requests which are safely on storage |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3162 | * schedule a read on some buffers |
| 3163 | * schedule a write of some buffers |
| 3164 | * return confirmation of parity correctness |
| 3165 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3166 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3167 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3168 | static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3169 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3170 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3171 | int disks = sh->disks; |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3172 | struct r5dev *dev; |
| 3173 | int i; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3174 | int do_recovery = 0; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3175 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3176 | memset(s, 0, sizeof(*s)); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3177 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3178 | s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 3179 | s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state); |
| 3180 | s->failed_num[0] = -1; |
| 3181 | s->failed_num[1] = -1; |
| 3182 | |
| 3183 | /* Now to look around and see what can be done */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3184 | rcu_read_lock(); |
NeilBrown | c4c1663 | 2011-07-26 11:34:20 +1000 | [diff] [blame] | 3185 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3186 | for (i=disks; i--; ) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3187 | struct md_rdev *rdev; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3188 | sector_t first_bad; |
| 3189 | int bad_sectors; |
| 3190 | int is_bad = 0; |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3191 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3192 | dev = &sh->dev[i]; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3193 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3194 | 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] | 3195 | i, dev->flags, |
| 3196 | dev->toread, dev->towrite, dev->written); |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3197 | /* maybe we can reply to a read |
| 3198 | * |
| 3199 | * new wantfill requests are only permitted while |
| 3200 | * ops_complete_biofill is guaranteed to be inactive |
| 3201 | */ |
| 3202 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread && |
| 3203 | !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) |
| 3204 | set_bit(R5_Wantfill, &dev->flags); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3205 | |
| 3206 | /* now count some things */ |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3207 | if (test_bit(R5_LOCKED, &dev->flags)) |
| 3208 | s->locked++; |
| 3209 | if (test_bit(R5_UPTODATE, &dev->flags)) |
| 3210 | s->uptodate++; |
Dan Williams | 2d6e4ec | 2009-09-16 12:11:54 -0700 | [diff] [blame] | 3211 | if (test_bit(R5_Wantcompute, &dev->flags)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3212 | s->compute++; |
| 3213 | BUG_ON(s->compute > 2); |
Dan Williams | 2d6e4ec | 2009-09-16 12:11:54 -0700 | [diff] [blame] | 3214 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3215 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3216 | if (test_bit(R5_Wantfill, &dev->flags)) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3217 | s->to_fill++; |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3218 | else if (dev->toread) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3219 | s->to_read++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3220 | if (dev->towrite) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3221 | s->to_write++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3222 | if (!test_bit(R5_OVERWRITE, &dev->flags)) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3223 | s->non_overwrite++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3224 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3225 | if (dev->written) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3226 | s->written++; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3227 | /* Prefer to use the replacement for reads, but only |
| 3228 | * if it is recovered enough and has no bad blocks. |
| 3229 | */ |
| 3230 | rdev = rcu_dereference(conf->disks[i].replacement); |
| 3231 | if (rdev && !test_bit(Faulty, &rdev->flags) && |
| 3232 | rdev->recovery_offset >= sh->sector + STRIPE_SECTORS && |
| 3233 | !is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 3234 | &first_bad, &bad_sectors)) |
| 3235 | set_bit(R5_ReadRepl, &dev->flags); |
| 3236 | else { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3237 | if (rdev) |
| 3238 | set_bit(R5_NeedReplace, &dev->flags); |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3239 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 3240 | clear_bit(R5_ReadRepl, &dev->flags); |
| 3241 | } |
NeilBrown | 9283d8c | 2011-12-08 16:27:57 +1100 | [diff] [blame] | 3242 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 3243 | rdev = NULL; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3244 | if (rdev) { |
| 3245 | is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 3246 | &first_bad, &bad_sectors); |
| 3247 | if (s->blocked_rdev == NULL |
| 3248 | && (test_bit(Blocked, &rdev->flags) |
| 3249 | || is_bad < 0)) { |
| 3250 | if (is_bad < 0) |
| 3251 | set_bit(BlockedBadBlocks, |
| 3252 | &rdev->flags); |
| 3253 | s->blocked_rdev = rdev; |
| 3254 | atomic_inc(&rdev->nr_pending); |
| 3255 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3256 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3257 | clear_bit(R5_Insync, &dev->flags); |
| 3258 | if (!rdev) |
| 3259 | /* Not in-sync */; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3260 | else if (is_bad) { |
| 3261 | /* also not in-sync */ |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3262 | if (!test_bit(WriteErrorSeen, &rdev->flags) && |
| 3263 | test_bit(R5_UPTODATE, &dev->flags)) { |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3264 | /* treat as in-sync, but with a read error |
| 3265 | * which we can now try to correct |
| 3266 | */ |
| 3267 | set_bit(R5_Insync, &dev->flags); |
| 3268 | set_bit(R5_ReadError, &dev->flags); |
| 3269 | } |
| 3270 | } else if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3271 | set_bit(R5_Insync, &dev->flags); |
NeilBrown | 30d7a48 | 2011-12-23 09:57:00 +1100 | [diff] [blame] | 3272 | else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset) |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3273 | /* in sync if before recovery_offset */ |
NeilBrown | 30d7a48 | 2011-12-23 09:57:00 +1100 | [diff] [blame] | 3274 | set_bit(R5_Insync, &dev->flags); |
| 3275 | else if (test_bit(R5_UPTODATE, &dev->flags) && |
| 3276 | test_bit(R5_Expanded, &dev->flags)) |
| 3277 | /* If we've reshaped into here, we assume it is Insync. |
| 3278 | * We will shortly update recovery_offset to make |
| 3279 | * it official. |
| 3280 | */ |
| 3281 | set_bit(R5_Insync, &dev->flags); |
| 3282 | |
Adam Kwolek | 5d8c71f | 2011-12-09 14:26:11 +1100 | [diff] [blame] | 3283 | if (rdev && test_bit(R5_WriteError, &dev->flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3284 | /* This flag does not apply to '.replacement' |
| 3285 | * only to .rdev, so make sure to check that*/ |
| 3286 | struct md_rdev *rdev2 = rcu_dereference( |
| 3287 | conf->disks[i].rdev); |
| 3288 | if (rdev2 == rdev) |
| 3289 | clear_bit(R5_Insync, &dev->flags); |
| 3290 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3291 | s->handle_bad_blocks = 1; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3292 | atomic_inc(&rdev2->nr_pending); |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3293 | } else |
| 3294 | clear_bit(R5_WriteError, &dev->flags); |
| 3295 | } |
Adam Kwolek | 5d8c71f | 2011-12-09 14:26:11 +1100 | [diff] [blame] | 3296 | if (rdev && test_bit(R5_MadeGood, &dev->flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3297 | /* This flag does not apply to '.replacement' |
| 3298 | * only to .rdev, so make sure to check that*/ |
| 3299 | struct md_rdev *rdev2 = rcu_dereference( |
| 3300 | conf->disks[i].rdev); |
| 3301 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 3302 | s->handle_bad_blocks = 1; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3303 | atomic_inc(&rdev2->nr_pending); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 3304 | } else |
| 3305 | clear_bit(R5_MadeGood, &dev->flags); |
| 3306 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3307 | if (test_bit(R5_MadeGoodRepl, &dev->flags)) { |
| 3308 | struct md_rdev *rdev2 = rcu_dereference( |
| 3309 | conf->disks[i].replacement); |
| 3310 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
| 3311 | s->handle_bad_blocks = 1; |
| 3312 | atomic_inc(&rdev2->nr_pending); |
| 3313 | } else |
| 3314 | clear_bit(R5_MadeGoodRepl, &dev->flags); |
| 3315 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3316 | if (!test_bit(R5_Insync, &dev->flags)) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3317 | /* The ReadError flag will just be confusing now */ |
| 3318 | clear_bit(R5_ReadError, &dev->flags); |
| 3319 | clear_bit(R5_ReWrite, &dev->flags); |
| 3320 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3321 | if (test_bit(R5_ReadError, &dev->flags)) |
| 3322 | clear_bit(R5_Insync, &dev->flags); |
| 3323 | if (!test_bit(R5_Insync, &dev->flags)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3324 | if (s->failed < 2) |
| 3325 | s->failed_num[s->failed] = i; |
| 3326 | s->failed++; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3327 | if (rdev && !test_bit(Faulty, &rdev->flags)) |
| 3328 | do_recovery = 1; |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3329 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3330 | } |
NeilBrown | c4c1663 | 2011-07-26 11:34:20 +1000 | [diff] [blame] | 3331 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3332 | if (test_bit(STRIPE_SYNCING, &sh->state)) { |
| 3333 | /* If there is a failed device being replaced, |
| 3334 | * we must be recovering. |
| 3335 | * else if we are after recovery_cp, we must be syncing |
majianpeng | c6d2e08 | 2012-04-02 01:16:59 +1000 | [diff] [blame] | 3336 | * else if MD_RECOVERY_REQUESTED is set, we also are syncing. |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3337 | * else we can only be replacing |
| 3338 | * sync and recovery both need to read all devices, and so |
| 3339 | * use the same flag. |
| 3340 | */ |
| 3341 | if (do_recovery || |
majianpeng | c6d2e08 | 2012-04-02 01:16:59 +1000 | [diff] [blame] | 3342 | sh->sector >= conf->mddev->recovery_cp || |
| 3343 | test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery))) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3344 | s->syncing = 1; |
| 3345 | else |
| 3346 | s->replacing = 1; |
| 3347 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3348 | rcu_read_unlock(); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3349 | } |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3350 | |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3351 | static void handle_stripe(struct stripe_head *sh) |
| 3352 | { |
| 3353 | struct stripe_head_state s; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3354 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3355 | int i; |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3356 | int prexor; |
| 3357 | int disks = sh->disks; |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3358 | struct r5dev *pdev, *qdev; |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3359 | |
| 3360 | clear_bit(STRIPE_HANDLE, &sh->state); |
Dan Williams | 257a4b4 | 2011-11-08 16:22:06 +1100 | [diff] [blame] | 3361 | if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3362 | /* already being handled, ensure it gets handled |
| 3363 | * again when current action finishes */ |
| 3364 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3365 | return; |
| 3366 | } |
| 3367 | |
| 3368 | if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) { |
| 3369 | set_bit(STRIPE_SYNCING, &sh->state); |
| 3370 | clear_bit(STRIPE_INSYNC, &sh->state); |
| 3371 | } |
| 3372 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 3373 | |
| 3374 | pr_debug("handling stripe %llu, state=%#lx cnt=%d, " |
| 3375 | "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n", |
| 3376 | (unsigned long long)sh->sector, sh->state, |
| 3377 | atomic_read(&sh->count), sh->pd_idx, sh->qd_idx, |
| 3378 | sh->check_state, sh->reconstruct_state); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3379 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3380 | analyse_stripe(sh, &s); |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3381 | |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3382 | if (s.handle_bad_blocks) { |
| 3383 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3384 | goto finish; |
| 3385 | } |
| 3386 | |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3387 | if (unlikely(s.blocked_rdev)) { |
| 3388 | if (s.syncing || s.expanding || s.expanded || |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3389 | s.replacing || s.to_write || s.written) { |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3390 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3391 | goto finish; |
| 3392 | } |
| 3393 | /* There is nothing for the blocked_rdev to block */ |
| 3394 | rdev_dec_pending(s.blocked_rdev, conf->mddev); |
| 3395 | s.blocked_rdev = NULL; |
| 3396 | } |
| 3397 | |
| 3398 | if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) { |
| 3399 | set_bit(STRIPE_OP_BIOFILL, &s.ops_request); |
| 3400 | set_bit(STRIPE_BIOFILL_RUN, &sh->state); |
| 3401 | } |
| 3402 | |
| 3403 | pr_debug("locked=%d uptodate=%d to_read=%d" |
| 3404 | " to_write=%d failed=%d failed_num=%d,%d\n", |
| 3405 | s.locked, s.uptodate, s.to_read, s.to_write, s.failed, |
| 3406 | s.failed_num[0], s.failed_num[1]); |
| 3407 | /* check if the array has lost more than max_degraded devices and, |
| 3408 | * if so, some requests might need to be failed. |
| 3409 | */ |
NeilBrown | 9a3f530 | 2011-11-08 16:22:01 +1100 | [diff] [blame] | 3410 | if (s.failed > conf->max_degraded) { |
| 3411 | sh->check_state = 0; |
| 3412 | sh->reconstruct_state = 0; |
| 3413 | if (s.to_read+s.to_write+s.written) |
| 3414 | handle_failed_stripe(conf, sh, &s, disks, &s.return_bi); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3415 | if (s.syncing + s.replacing) |
NeilBrown | 9a3f530 | 2011-11-08 16:22:01 +1100 | [diff] [blame] | 3416 | handle_failed_sync(conf, sh, &s); |
| 3417 | } |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3418 | |
| 3419 | /* |
| 3420 | * might be able to return some write requests if the parity blocks |
| 3421 | * are safe, or on a failed drive |
| 3422 | */ |
| 3423 | pdev = &sh->dev[sh->pd_idx]; |
| 3424 | s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx) |
| 3425 | || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx); |
| 3426 | qdev = &sh->dev[sh->qd_idx]; |
| 3427 | s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx) |
| 3428 | || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx) |
| 3429 | || conf->level < 6; |
| 3430 | |
| 3431 | if (s.written && |
| 3432 | (s.p_failed || ((test_bit(R5_Insync, &pdev->flags) |
| 3433 | && !test_bit(R5_LOCKED, &pdev->flags) |
| 3434 | && test_bit(R5_UPTODATE, &pdev->flags)))) && |
| 3435 | (s.q_failed || ((test_bit(R5_Insync, &qdev->flags) |
| 3436 | && !test_bit(R5_LOCKED, &qdev->flags) |
| 3437 | && test_bit(R5_UPTODATE, &qdev->flags))))) |
| 3438 | handle_stripe_clean_event(conf, sh, disks, &s.return_bi); |
| 3439 | |
| 3440 | /* Now we might consider reading some blocks, either to check/generate |
| 3441 | * parity, or to satisfy requests |
| 3442 | * or to load a block that is being partially written. |
| 3443 | */ |
| 3444 | if (s.to_read || s.non_overwrite |
| 3445 | || (conf->level == 6 && s.to_write && s.failed) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3446 | || (s.syncing && (s.uptodate + s.compute < disks)) |
| 3447 | || s.replacing |
| 3448 | || s.expanding) |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3449 | handle_stripe_fill(sh, &s, disks); |
| 3450 | |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3451 | /* Now we check to see if any write operations have recently |
| 3452 | * completed |
| 3453 | */ |
| 3454 | prexor = 0; |
| 3455 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_result) |
| 3456 | prexor = 1; |
| 3457 | if (sh->reconstruct_state == reconstruct_state_drain_result || |
| 3458 | sh->reconstruct_state == reconstruct_state_prexor_drain_result) { |
| 3459 | sh->reconstruct_state = reconstruct_state_idle; |
| 3460 | |
| 3461 | /* All the 'written' buffers and the parity block are ready to |
| 3462 | * be written back to disk |
| 3463 | */ |
| 3464 | BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags)); |
| 3465 | BUG_ON(sh->qd_idx >= 0 && |
| 3466 | !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags)); |
| 3467 | for (i = disks; i--; ) { |
| 3468 | struct r5dev *dev = &sh->dev[i]; |
| 3469 | if (test_bit(R5_LOCKED, &dev->flags) && |
| 3470 | (i == sh->pd_idx || i == sh->qd_idx || |
| 3471 | dev->written)) { |
| 3472 | pr_debug("Writing block %d\n", i); |
| 3473 | set_bit(R5_Wantwrite, &dev->flags); |
| 3474 | if (prexor) |
| 3475 | continue; |
| 3476 | if (!test_bit(R5_Insync, &dev->flags) || |
| 3477 | ((i == sh->pd_idx || i == sh->qd_idx) && |
| 3478 | s.failed == 0)) |
| 3479 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3480 | } |
| 3481 | } |
| 3482 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 3483 | s.dec_preread_active = 1; |
| 3484 | } |
| 3485 | |
| 3486 | /* Now to consider new write requests and what else, if anything |
| 3487 | * should be read. We do not handle new writes when: |
| 3488 | * 1/ A 'write' operation (copy+xor) is already in flight. |
| 3489 | * 2/ A 'check' operation is in flight, as it may clobber the parity |
| 3490 | * block. |
| 3491 | */ |
| 3492 | if (s.to_write && !sh->reconstruct_state && !sh->check_state) |
| 3493 | handle_stripe_dirtying(conf, sh, &s, disks); |
| 3494 | |
| 3495 | /* maybe we need to check and possibly fix the parity for this stripe |
| 3496 | * Any reads will already have been scheduled, so we just see if enough |
| 3497 | * data is available. The parity check is held off while parity |
| 3498 | * dependent operations are in flight. |
| 3499 | */ |
| 3500 | if (sh->check_state || |
| 3501 | (s.syncing && s.locked == 0 && |
| 3502 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && |
| 3503 | !test_bit(STRIPE_INSYNC, &sh->state))) { |
| 3504 | if (conf->level == 6) |
| 3505 | handle_parity_checks6(conf, sh, &s, disks); |
| 3506 | else |
| 3507 | handle_parity_checks5(conf, sh, &s, disks); |
| 3508 | } |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3509 | |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3510 | if (s.replacing && s.locked == 0 |
| 3511 | && !test_bit(STRIPE_INSYNC, &sh->state)) { |
| 3512 | /* Write out to replacement devices where possible */ |
| 3513 | for (i = 0; i < conf->raid_disks; i++) |
| 3514 | if (test_bit(R5_UPTODATE, &sh->dev[i].flags) && |
| 3515 | test_bit(R5_NeedReplace, &sh->dev[i].flags)) { |
| 3516 | set_bit(R5_WantReplace, &sh->dev[i].flags); |
| 3517 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 3518 | s.locked++; |
| 3519 | } |
| 3520 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3521 | } |
| 3522 | if ((s.syncing || s.replacing) && s.locked == 0 && |
| 3523 | test_bit(STRIPE_INSYNC, &sh->state)) { |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3524 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 3525 | clear_bit(STRIPE_SYNCING, &sh->state); |
| 3526 | } |
| 3527 | |
| 3528 | /* If the failed drives are just a ReadError, then we might need |
| 3529 | * to progress the repair/check process |
| 3530 | */ |
| 3531 | if (s.failed <= conf->max_degraded && !conf->mddev->ro) |
| 3532 | for (i = 0; i < s.failed; i++) { |
| 3533 | struct r5dev *dev = &sh->dev[s.failed_num[i]]; |
| 3534 | if (test_bit(R5_ReadError, &dev->flags) |
| 3535 | && !test_bit(R5_LOCKED, &dev->flags) |
| 3536 | && test_bit(R5_UPTODATE, &dev->flags) |
| 3537 | ) { |
| 3538 | if (!test_bit(R5_ReWrite, &dev->flags)) { |
| 3539 | set_bit(R5_Wantwrite, &dev->flags); |
| 3540 | set_bit(R5_ReWrite, &dev->flags); |
| 3541 | set_bit(R5_LOCKED, &dev->flags); |
| 3542 | s.locked++; |
| 3543 | } else { |
| 3544 | /* let's read it back */ |
| 3545 | set_bit(R5_Wantread, &dev->flags); |
| 3546 | set_bit(R5_LOCKED, &dev->flags); |
| 3547 | s.locked++; |
| 3548 | } |
| 3549 | } |
| 3550 | } |
| 3551 | |
| 3552 | |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3553 | /* Finish reconstruct operations initiated by the expansion process */ |
| 3554 | if (sh->reconstruct_state == reconstruct_state_result) { |
| 3555 | struct stripe_head *sh_src |
| 3556 | = get_active_stripe(conf, sh->sector, 1, 1, 1); |
| 3557 | if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) { |
| 3558 | /* sh cannot be written until sh_src has been read. |
| 3559 | * so arrange for sh to be delayed a little |
| 3560 | */ |
| 3561 | set_bit(STRIPE_DELAYED, &sh->state); |
| 3562 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3563 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, |
| 3564 | &sh_src->state)) |
| 3565 | atomic_inc(&conf->preread_active_stripes); |
| 3566 | release_stripe(sh_src); |
| 3567 | goto finish; |
| 3568 | } |
| 3569 | if (sh_src) |
| 3570 | release_stripe(sh_src); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3571 | |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3572 | sh->reconstruct_state = reconstruct_state_idle; |
| 3573 | clear_bit(STRIPE_EXPANDING, &sh->state); |
| 3574 | for (i = conf->raid_disks; i--; ) { |
| 3575 | set_bit(R5_Wantwrite, &sh->dev[i].flags); |
| 3576 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 3577 | s.locked++; |
| 3578 | } |
| 3579 | } |
| 3580 | |
| 3581 | if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) && |
| 3582 | !sh->reconstruct_state) { |
| 3583 | /* Need to write out all blocks after computing parity */ |
| 3584 | sh->disks = conf->raid_disks; |
| 3585 | stripe_set_idx(sh->sector, conf, 0, sh); |
| 3586 | schedule_reconstruction(sh, &s, 1, 1); |
| 3587 | } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) { |
| 3588 | clear_bit(STRIPE_EXPAND_READY, &sh->state); |
| 3589 | atomic_dec(&conf->reshape_stripes); |
| 3590 | wake_up(&conf->wait_for_overlap); |
| 3591 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 3592 | } |
| 3593 | |
| 3594 | if (s.expanding && s.locked == 0 && |
| 3595 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) |
| 3596 | handle_stripe_expansion(conf, sh); |
| 3597 | |
| 3598 | finish: |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3599 | /* wait for this device to become unblocked */ |
NeilBrown | 5f066c6 | 2012-07-03 12:13:29 +1000 | [diff] [blame] | 3600 | if (unlikely(s.blocked_rdev)) { |
| 3601 | if (conf->mddev->external) |
| 3602 | md_wait_for_blocked_rdev(s.blocked_rdev, |
| 3603 | conf->mddev); |
| 3604 | else |
| 3605 | /* Internal metadata will immediately |
| 3606 | * be written by raid5d, so we don't |
| 3607 | * need to wait here. |
| 3608 | */ |
| 3609 | rdev_dec_pending(s.blocked_rdev, |
| 3610 | conf->mddev); |
| 3611 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3612 | |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3613 | if (s.handle_bad_blocks) |
| 3614 | for (i = disks; i--; ) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3615 | struct md_rdev *rdev; |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3616 | struct r5dev *dev = &sh->dev[i]; |
| 3617 | if (test_and_clear_bit(R5_WriteError, &dev->flags)) { |
| 3618 | /* We own a safe reference to the rdev */ |
| 3619 | rdev = conf->disks[i].rdev; |
| 3620 | if (!rdev_set_badblocks(rdev, sh->sector, |
| 3621 | STRIPE_SECTORS, 0)) |
| 3622 | md_error(conf->mddev, rdev); |
| 3623 | rdev_dec_pending(rdev, conf->mddev); |
| 3624 | } |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 3625 | if (test_and_clear_bit(R5_MadeGood, &dev->flags)) { |
| 3626 | rdev = conf->disks[i].rdev; |
| 3627 | rdev_clear_badblocks(rdev, sh->sector, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3628 | STRIPE_SECTORS, 0); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 3629 | rdev_dec_pending(rdev, conf->mddev); |
| 3630 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3631 | if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) { |
| 3632 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3633 | if (!rdev) |
| 3634 | /* rdev have been moved down */ |
| 3635 | rdev = conf->disks[i].rdev; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3636 | rdev_clear_badblocks(rdev, sh->sector, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3637 | STRIPE_SECTORS, 0); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3638 | rdev_dec_pending(rdev, conf->mddev); |
| 3639 | } |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3640 | } |
| 3641 | |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3642 | if (s.ops_request) |
| 3643 | raid_run_ops(sh, s.ops_request); |
| 3644 | |
Dan Williams | f0e43bc | 2008-06-28 08:31:55 +1000 | [diff] [blame] | 3645 | ops_run_io(sh, &s); |
| 3646 | |
NeilBrown | c5709ef | 2011-07-26 11:35:20 +1000 | [diff] [blame] | 3647 | if (s.dec_preread_active) { |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 3648 | /* 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] | 3649 | * is waiting on a flush, it won't continue until the writes |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 3650 | * have actually been submitted. |
| 3651 | */ |
| 3652 | atomic_dec(&conf->preread_active_stripes); |
| 3653 | if (atomic_read(&conf->preread_active_stripes) < |
| 3654 | IO_THRESHOLD) |
| 3655 | md_wakeup_thread(conf->mddev->thread); |
| 3656 | } |
| 3657 | |
NeilBrown | c5709ef | 2011-07-26 11:35:20 +1000 | [diff] [blame] | 3658 | return_io(s.return_bi); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3659 | |
Dan Williams | 257a4b4 | 2011-11-08 16:22:06 +1100 | [diff] [blame] | 3660 | clear_bit_unlock(STRIPE_ACTIVE, &sh->state); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3661 | } |
| 3662 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3663 | static void raid5_activate_delayed(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3664 | { |
| 3665 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) { |
| 3666 | while (!list_empty(&conf->delayed_list)) { |
| 3667 | struct list_head *l = conf->delayed_list.next; |
| 3668 | struct stripe_head *sh; |
| 3669 | sh = list_entry(l, struct stripe_head, lru); |
| 3670 | list_del_init(l); |
| 3671 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 3672 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 3673 | atomic_inc(&conf->preread_active_stripes); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3674 | list_add_tail(&sh->lru, &conf->hold_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3675 | } |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 3676 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3677 | } |
| 3678 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3679 | static void activate_bit_delay(struct r5conf *conf) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3680 | { |
| 3681 | /* device_lock is held */ |
| 3682 | struct list_head head; |
| 3683 | list_add(&head, &conf->bitmap_list); |
| 3684 | list_del_init(&conf->bitmap_list); |
| 3685 | while (!list_empty(&head)) { |
| 3686 | struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru); |
| 3687 | list_del_init(&sh->lru); |
| 3688 | atomic_inc(&sh->count); |
| 3689 | __release_stripe(conf, sh); |
| 3690 | } |
| 3691 | } |
| 3692 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3693 | int md_raid5_congested(struct mddev *mddev, int bits) |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3694 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3695 | struct r5conf *conf = mddev->private; |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3696 | |
| 3697 | /* No difference between reads and writes. Just check |
| 3698 | * how busy the stripe_cache is |
| 3699 | */ |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 3700 | |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3701 | if (conf->inactive_blocked) |
| 3702 | return 1; |
| 3703 | if (conf->quiesce) |
| 3704 | return 1; |
| 3705 | if (list_empty_careful(&conf->inactive_list)) |
| 3706 | return 1; |
| 3707 | |
| 3708 | return 0; |
| 3709 | } |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 3710 | EXPORT_SYMBOL_GPL(md_raid5_congested); |
| 3711 | |
| 3712 | static int raid5_congested(void *data, int bits) |
| 3713 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3714 | struct mddev *mddev = data; |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 3715 | |
| 3716 | return mddev_congested(mddev, bits) || |
| 3717 | md_raid5_congested(mddev, bits); |
| 3718 | } |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3719 | |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3720 | /* We want read requests to align with chunks where possible, |
| 3721 | * but write requests don't need to. |
| 3722 | */ |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3723 | static int raid5_mergeable_bvec(struct request_queue *q, |
| 3724 | struct bvec_merge_data *bvm, |
| 3725 | struct bio_vec *biovec) |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3726 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3727 | struct mddev *mddev = q->queuedata; |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3728 | 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] | 3729 | int max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3730 | unsigned int chunk_sectors = mddev->chunk_sectors; |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3731 | unsigned int bio_sectors = bvm->bi_size >> 9; |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3732 | |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3733 | if ((bvm->bi_rw & 1) == WRITE) |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3734 | return biovec->bv_len; /* always allow writes to be mergeable */ |
| 3735 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3736 | if (mddev->new_chunk_sectors < mddev->chunk_sectors) |
| 3737 | chunk_sectors = mddev->new_chunk_sectors; |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3738 | max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9; |
| 3739 | if (max < 0) max = 0; |
| 3740 | if (max <= biovec->bv_len && bio_sectors == 0) |
| 3741 | return biovec->bv_len; |
| 3742 | else |
| 3743 | return max; |
| 3744 | } |
| 3745 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3746 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3747 | 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] | 3748 | { |
| 3749 | sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3750 | unsigned int chunk_sectors = mddev->chunk_sectors; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3751 | unsigned int bio_sectors = bio->bi_size >> 9; |
| 3752 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3753 | if (mddev->new_chunk_sectors < mddev->chunk_sectors) |
| 3754 | chunk_sectors = mddev->new_chunk_sectors; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3755 | return chunk_sectors >= |
| 3756 | ((sector & (chunk_sectors - 1)) + bio_sectors); |
| 3757 | } |
| 3758 | |
| 3759 | /* |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3760 | * add bio to the retry LIFO ( in O(1) ... we are in interrupt ) |
| 3761 | * later sampled by raid5d. |
| 3762 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3763 | 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] | 3764 | { |
| 3765 | unsigned long flags; |
| 3766 | |
| 3767 | spin_lock_irqsave(&conf->device_lock, flags); |
| 3768 | |
| 3769 | bi->bi_next = conf->retry_read_aligned_list; |
| 3770 | conf->retry_read_aligned_list = bi; |
| 3771 | |
| 3772 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 3773 | md_wakeup_thread(conf->mddev->thread); |
| 3774 | } |
| 3775 | |
| 3776 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3777 | static struct bio *remove_bio_from_retry(struct r5conf *conf) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3778 | { |
| 3779 | struct bio *bi; |
| 3780 | |
| 3781 | bi = conf->retry_read_aligned; |
| 3782 | if (bi) { |
| 3783 | conf->retry_read_aligned = NULL; |
| 3784 | return bi; |
| 3785 | } |
| 3786 | bi = conf->retry_read_aligned_list; |
| 3787 | if(bi) { |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3788 | conf->retry_read_aligned_list = bi->bi_next; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3789 | bi->bi_next = NULL; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 3790 | /* |
| 3791 | * this sets the active strip count to 1 and the processed |
| 3792 | * strip count to zero (upper 8 bits) |
| 3793 | */ |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3794 | bi->bi_phys_segments = 1; /* biased count of active stripes */ |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3795 | } |
| 3796 | |
| 3797 | return bi; |
| 3798 | } |
| 3799 | |
| 3800 | |
| 3801 | /* |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3802 | * The "raid5_align_endio" should check if the read succeeded and if it |
| 3803 | * did, call bio_endio on the original bio (having bio_put the new bio |
| 3804 | * first). |
| 3805 | * If the read failed.. |
| 3806 | */ |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3807 | static void raid5_align_endio(struct bio *bi, int error) |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3808 | { |
| 3809 | struct bio* raid_bi = bi->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3810 | struct mddev *mddev; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3811 | struct r5conf *conf; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3812 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3813 | struct md_rdev *rdev; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3814 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3815 | bio_put(bi); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3816 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3817 | rdev = (void*)raid_bi->bi_next; |
| 3818 | raid_bi->bi_next = NULL; |
NeilBrown | 2b7f222 | 2010-03-25 16:06:03 +1100 | [diff] [blame] | 3819 | mddev = rdev->mddev; |
| 3820 | conf = mddev->private; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3821 | |
| 3822 | rdev_dec_pending(rdev, conf->mddev); |
| 3823 | |
| 3824 | if (!error && uptodate) { |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3825 | bio_endio(raid_bi, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3826 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
| 3827 | wake_up(&conf->wait_for_stripe); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3828 | return; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3829 | } |
| 3830 | |
| 3831 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3832 | 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] | 3833 | |
| 3834 | add_bio_to_retry(raid_bi, conf); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3835 | } |
| 3836 | |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3837 | static int bio_fits_rdev(struct bio *bi) |
| 3838 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3839 | struct request_queue *q = bdev_get_queue(bi->bi_bdev); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3840 | |
Martin K. Petersen | ae03bf6 | 2009-05-22 17:17:50 -0400 | [diff] [blame] | 3841 | if ((bi->bi_size>>9) > queue_max_sectors(q)) |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3842 | return 0; |
| 3843 | blk_recount_segments(q, bi); |
Martin K. Petersen | 8a78362 | 2010-02-26 00:20:39 -0500 | [diff] [blame] | 3844 | if (bi->bi_phys_segments > queue_max_segments(q)) |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3845 | return 0; |
| 3846 | |
| 3847 | if (q->merge_bvec_fn) |
| 3848 | /* it's too hard to apply the merge_bvec_fn at this stage, |
| 3849 | * just just give up |
| 3850 | */ |
| 3851 | return 0; |
| 3852 | |
| 3853 | return 1; |
| 3854 | } |
| 3855 | |
| 3856 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3857 | 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] | 3858 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3859 | struct r5conf *conf = mddev->private; |
NeilBrown | 8553fe7ec | 2009-12-14 12:49:47 +1100 | [diff] [blame] | 3860 | int dd_idx; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3861 | struct bio* align_bi; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3862 | struct md_rdev *rdev; |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3863 | sector_t end_sector; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3864 | |
| 3865 | if (!in_chunk_boundary(mddev, raid_bio)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3866 | pr_debug("chunk_aligned_read : non aligned\n"); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3867 | return 0; |
| 3868 | } |
| 3869 | /* |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 3870 | * 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] | 3871 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 3872 | align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3873 | if (!align_bi) |
| 3874 | return 0; |
| 3875 | /* |
| 3876 | * set bi_end_io to a new function, and set bi_private to the |
| 3877 | * original bio. |
| 3878 | */ |
| 3879 | align_bi->bi_end_io = raid5_align_endio; |
| 3880 | align_bi->bi_private = raid_bio; |
| 3881 | /* |
| 3882 | * compute position |
| 3883 | */ |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3884 | align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector, |
| 3885 | 0, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3886 | &dd_idx, NULL); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3887 | |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3888 | 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] | 3889 | rcu_read_lock(); |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3890 | rdev = rcu_dereference(conf->disks[dd_idx].replacement); |
| 3891 | if (!rdev || test_bit(Faulty, &rdev->flags) || |
| 3892 | rdev->recovery_offset < end_sector) { |
| 3893 | rdev = rcu_dereference(conf->disks[dd_idx].rdev); |
| 3894 | if (rdev && |
| 3895 | (test_bit(Faulty, &rdev->flags) || |
| 3896 | !(test_bit(In_sync, &rdev->flags) || |
| 3897 | rdev->recovery_offset >= end_sector))) |
| 3898 | rdev = NULL; |
| 3899 | } |
| 3900 | if (rdev) { |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3901 | sector_t first_bad; |
| 3902 | int bad_sectors; |
| 3903 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3904 | atomic_inc(&rdev->nr_pending); |
| 3905 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3906 | raid_bio->bi_next = (void*)rdev; |
| 3907 | align_bi->bi_bdev = rdev->bdev; |
| 3908 | align_bi->bi_flags &= ~(1 << BIO_SEG_VALID); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3909 | |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3910 | if (!bio_fits_rdev(align_bi) || |
| 3911 | is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9, |
| 3912 | &first_bad, &bad_sectors)) { |
| 3913 | /* too big in some way, or has a known bad block */ |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3914 | bio_put(align_bi); |
| 3915 | rdev_dec_pending(rdev, mddev); |
| 3916 | return 0; |
| 3917 | } |
| 3918 | |
majianpeng | 6c0544e | 2012-06-12 08:31:10 +0800 | [diff] [blame] | 3919 | /* No reshape active, so we can trust rdev->data_offset */ |
| 3920 | align_bi->bi_sector += rdev->data_offset; |
| 3921 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3922 | spin_lock_irq(&conf->device_lock); |
| 3923 | wait_event_lock_irq(conf->wait_for_stripe, |
| 3924 | conf->quiesce == 0, |
| 3925 | conf->device_lock, /* nothing */); |
| 3926 | atomic_inc(&conf->active_aligned_reads); |
| 3927 | spin_unlock_irq(&conf->device_lock); |
| 3928 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3929 | generic_make_request(align_bi); |
| 3930 | return 1; |
| 3931 | } else { |
| 3932 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3933 | bio_put(align_bi); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3934 | return 0; |
| 3935 | } |
| 3936 | } |
| 3937 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3938 | /* __get_priority_stripe - get the next stripe to process |
| 3939 | * |
| 3940 | * Full stripe writes are allowed to pass preread active stripes up until |
| 3941 | * the bypass_threshold is exceeded. In general the bypass_count |
| 3942 | * increments when the handle_list is handled before the hold_list; however, it |
| 3943 | * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a |
| 3944 | * stripe with in flight i/o. The bypass_count will be reset when the |
| 3945 | * head of the hold_list has changed, i.e. the head was promoted to the |
| 3946 | * handle_list. |
| 3947 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3948 | static struct stripe_head *__get_priority_stripe(struct r5conf *conf) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3949 | { |
| 3950 | struct stripe_head *sh; |
| 3951 | |
| 3952 | pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n", |
| 3953 | __func__, |
| 3954 | list_empty(&conf->handle_list) ? "empty" : "busy", |
| 3955 | list_empty(&conf->hold_list) ? "empty" : "busy", |
| 3956 | atomic_read(&conf->pending_full_writes), conf->bypass_count); |
| 3957 | |
| 3958 | if (!list_empty(&conf->handle_list)) { |
| 3959 | sh = list_entry(conf->handle_list.next, typeof(*sh), lru); |
| 3960 | |
| 3961 | if (list_empty(&conf->hold_list)) |
| 3962 | conf->bypass_count = 0; |
| 3963 | else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) { |
| 3964 | if (conf->hold_list.next == conf->last_hold) |
| 3965 | conf->bypass_count++; |
| 3966 | else { |
| 3967 | conf->last_hold = conf->hold_list.next; |
| 3968 | conf->bypass_count -= conf->bypass_threshold; |
| 3969 | if (conf->bypass_count < 0) |
| 3970 | conf->bypass_count = 0; |
| 3971 | } |
| 3972 | } |
| 3973 | } else if (!list_empty(&conf->hold_list) && |
| 3974 | ((conf->bypass_threshold && |
| 3975 | conf->bypass_count > conf->bypass_threshold) || |
| 3976 | atomic_read(&conf->pending_full_writes) == 0)) { |
| 3977 | sh = list_entry(conf->hold_list.next, |
| 3978 | typeof(*sh), lru); |
| 3979 | conf->bypass_count -= conf->bypass_threshold; |
| 3980 | if (conf->bypass_count < 0) |
| 3981 | conf->bypass_count = 0; |
| 3982 | } else |
| 3983 | return NULL; |
| 3984 | |
| 3985 | list_del_init(&sh->lru); |
| 3986 | atomic_inc(&sh->count); |
| 3987 | BUG_ON(atomic_read(&sh->count) != 1); |
| 3988 | return sh; |
| 3989 | } |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3990 | |
Linus Torvalds | b4fdcb0 | 2011-11-04 17:06:58 -0700 | [diff] [blame] | 3991 | static void make_request(struct mddev *mddev, struct bio * bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3992 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3993 | struct r5conf *conf = mddev->private; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3994 | int dd_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3995 | sector_t new_sector; |
| 3996 | sector_t logical_sector, last_sector; |
| 3997 | struct stripe_head *sh; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 3998 | const int rw = bio_data_dir(bi); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 3999 | int remaining; |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 4000 | int plugged; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4001 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 4002 | if (unlikely(bi->bi_rw & REQ_FLUSH)) { |
| 4003 | md_flush_request(mddev, bi); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 4004 | return; |
NeilBrown | e5dcdd8 | 2005-09-09 16:23:41 -0700 | [diff] [blame] | 4005 | } |
| 4006 | |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 4007 | md_write_start(mddev, bi); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 4008 | |
NeilBrown | 802ba06 | 2006-12-13 00:34:13 -0800 | [diff] [blame] | 4009 | if (rw == READ && |
Raz Ben-Jehuda(caro) | 5248861 | 2006-12-10 02:20:48 -0800 | [diff] [blame] | 4010 | mddev->reshape_position == MaxSector && |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 4011 | chunk_aligned_read(mddev,bi)) |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 4012 | return; |
Raz Ben-Jehuda(caro) | 5248861 | 2006-12-10 02:20:48 -0800 | [diff] [blame] | 4013 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4014 | logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
| 4015 | last_sector = bi->bi_sector + (bi->bi_size>>9); |
| 4016 | bi->bi_next = NULL; |
| 4017 | bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 4018 | |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 4019 | plugged = mddev_check_plugged(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4020 | for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) { |
| 4021 | DEFINE_WAIT(w); |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4022 | int previous; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 4023 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4024 | retry: |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4025 | previous = 0; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 4026 | prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 4027 | if (unlikely(conf->reshape_progress != MaxSector)) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4028 | /* spinlock is needed as reshape_progress may be |
NeilBrown | df8e7f7 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 4029 | * 64bit on a 32bit platform, and so it might be |
| 4030 | * possible to see a half-updated value |
Jesper Juhl | aeb878b | 2011-04-10 18:06:17 +0200 | [diff] [blame] | 4031 | * Of course reshape_progress could change after |
NeilBrown | df8e7f7 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 4032 | * the lock is dropped, so once we get a reference |
| 4033 | * to the stripe that we think it is, we will have |
| 4034 | * to check again. |
| 4035 | */ |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4036 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4037 | if (mddev->reshape_backwards |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4038 | ? logical_sector < conf->reshape_progress |
| 4039 | : logical_sector >= conf->reshape_progress) { |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4040 | previous = 1; |
| 4041 | } else { |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4042 | if (mddev->reshape_backwards |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4043 | ? logical_sector < conf->reshape_safe |
| 4044 | : logical_sector >= conf->reshape_safe) { |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 4045 | spin_unlock_irq(&conf->device_lock); |
| 4046 | schedule(); |
| 4047 | goto retry; |
| 4048 | } |
| 4049 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4050 | spin_unlock_irq(&conf->device_lock); |
| 4051 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4052 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4053 | new_sector = raid5_compute_sector(conf, logical_sector, |
| 4054 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4055 | &dd_idx, NULL); |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4056 | pr_debug("raid456: make_request, sector %llu logical %llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4057 | (unsigned long long)new_sector, |
| 4058 | (unsigned long long)logical_sector); |
| 4059 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4060 | sh = get_active_stripe(conf, new_sector, previous, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4061 | (bi->bi_rw&RWA_MASK), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4062 | if (sh) { |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 4063 | if (unlikely(previous)) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4064 | /* expansion might have moved on while waiting for a |
NeilBrown | df8e7f7 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 4065 | * stripe, so we must do the range check again. |
| 4066 | * Expansion could still move past after this |
| 4067 | * test, but as we are holding a reference to |
| 4068 | * 'sh', we know that if that happens, |
| 4069 | * STRIPE_EXPANDING will get set and the expansion |
| 4070 | * won't proceed until we finish with the stripe. |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4071 | */ |
| 4072 | int must_retry = 0; |
| 4073 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4074 | if (mddev->reshape_backwards |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 4075 | ? logical_sector >= conf->reshape_progress |
| 4076 | : logical_sector < conf->reshape_progress) |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4077 | /* mismatch, need to try again */ |
| 4078 | must_retry = 1; |
| 4079 | spin_unlock_irq(&conf->device_lock); |
| 4080 | if (must_retry) { |
| 4081 | release_stripe(sh); |
Dan Williams | 7a3ab90 | 2009-06-16 16:00:33 -0700 | [diff] [blame] | 4082 | schedule(); |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4083 | goto retry; |
| 4084 | } |
| 4085 | } |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 4086 | |
Namhyung Kim | ffd96e3 | 2011-07-18 17:38:51 +1000 | [diff] [blame] | 4087 | if (rw == WRITE && |
NeilBrown | a5c308d | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 4088 | logical_sector >= mddev->suspend_lo && |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4089 | logical_sector < mddev->suspend_hi) { |
| 4090 | release_stripe(sh); |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 4091 | /* As the suspend_* range is controlled by |
| 4092 | * userspace, we want an interruptible |
| 4093 | * wait. |
| 4094 | */ |
| 4095 | flush_signals(current); |
| 4096 | prepare_to_wait(&conf->wait_for_overlap, |
| 4097 | &w, TASK_INTERRUPTIBLE); |
| 4098 | if (logical_sector >= mddev->suspend_lo && |
| 4099 | logical_sector < mddev->suspend_hi) |
| 4100 | schedule(); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4101 | goto retry; |
| 4102 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4103 | |
| 4104 | if (test_bit(STRIPE_EXPANDING, &sh->state) || |
Namhyung Kim | ffd96e3 | 2011-07-18 17:38:51 +1000 | [diff] [blame] | 4105 | !add_stripe_bio(sh, bi, dd_idx, rw)) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4106 | /* Stripe is busy expanding or |
| 4107 | * add failed due to overlap. Flush everything |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4108 | * and wait a while |
| 4109 | */ |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 4110 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4111 | release_stripe(sh); |
| 4112 | schedule(); |
| 4113 | goto retry; |
| 4114 | } |
| 4115 | finish_wait(&conf->wait_for_overlap, &w); |
NeilBrown | 6ed3003 | 2008-02-06 01:40:00 -0800 | [diff] [blame] | 4116 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4117 | clear_bit(STRIPE_DELAYED, &sh->state); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 4118 | if ((bi->bi_rw & REQ_SYNC) && |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 4119 | !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 4120 | atomic_inc(&conf->preread_active_stripes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4121 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4122 | } else { |
| 4123 | /* cannot get stripe for read-ahead, just give-up */ |
| 4124 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
| 4125 | finish_wait(&conf->wait_for_overlap, &w); |
| 4126 | break; |
| 4127 | } |
| 4128 | |
| 4129 | } |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 4130 | if (!plugged) |
| 4131 | md_wakeup_thread(mddev->thread); |
| 4132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4133 | spin_lock_irq(&conf->device_lock); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4134 | remaining = raid5_dec_bi_phys_segments(bi); |
NeilBrown | f634475 | 2006-03-27 01:18:17 -0800 | [diff] [blame] | 4135 | spin_unlock_irq(&conf->device_lock); |
| 4136 | if (remaining == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4137 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4138 | if ( rw == WRITE ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4139 | md_write_end(mddev); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 4140 | |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 4141 | bio_endio(bi, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4142 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4143 | } |
| 4144 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4145 | 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] | 4146 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4147 | 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] | 4148 | { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4149 | /* reshaping is quite different to recovery/resync so it is |
| 4150 | * handled quite separately ... here. |
| 4151 | * |
| 4152 | * On each call to sync_request, we gather one chunk worth of |
| 4153 | * destination stripes and flag them as expanding. |
| 4154 | * Then we find all the source stripes and request reads. |
| 4155 | * As the reads complete, handle_stripe will copy the data |
| 4156 | * into the destination stripe and release that stripe. |
| 4157 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4158 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4159 | struct stripe_head *sh; |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4160 | sector_t first_sector, last_sector; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4161 | int raid_disks = conf->previous_raid_disks; |
| 4162 | int data_disks = raid_disks - conf->max_degraded; |
| 4163 | int new_data_disks = conf->raid_disks - conf->max_degraded; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4164 | int i; |
| 4165 | int dd_idx; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4166 | sector_t writepos, readpos, safepos; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4167 | sector_t stripe_addr; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4168 | int reshape_sectors; |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4169 | struct list_head stripes; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4170 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4171 | if (sector_nr == 0) { |
| 4172 | /* If restarting in the middle, skip the initial sectors */ |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4173 | if (mddev->reshape_backwards && |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4174 | conf->reshape_progress < raid5_size(mddev, 0, 0)) { |
| 4175 | sector_nr = raid5_size(mddev, 0, 0) |
| 4176 | - conf->reshape_progress; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4177 | } else if (!mddev->reshape_backwards && |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4178 | conf->reshape_progress > 0) |
| 4179 | sector_nr = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4180 | sector_div(sector_nr, new_data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4181 | if (sector_nr) { |
NeilBrown | 8dee721 | 2009-11-06 14:59:29 +1100 | [diff] [blame] | 4182 | mddev->curr_resync_completed = sector_nr; |
| 4183 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4184 | *skipped = 1; |
| 4185 | return sector_nr; |
| 4186 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4187 | } |
| 4188 | |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4189 | /* We need to process a full chunk at a time. |
| 4190 | * If old and new chunk sizes differ, we need to process the |
| 4191 | * largest of these |
| 4192 | */ |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4193 | if (mddev->new_chunk_sectors > mddev->chunk_sectors) |
| 4194 | reshape_sectors = mddev->new_chunk_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4195 | else |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4196 | reshape_sectors = mddev->chunk_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4197 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 4198 | /* We update the metadata at least every 10 seconds, or when |
| 4199 | * the data about to be copied would over-write the source of |
| 4200 | * the data at the front of the range. i.e. one new_stripe |
| 4201 | * along from reshape_progress new_maps to after where |
| 4202 | * reshape_safe old_maps to |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4203 | */ |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4204 | writepos = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4205 | sector_div(writepos, new_data_disks); |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4206 | readpos = conf->reshape_progress; |
| 4207 | sector_div(readpos, data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4208 | safepos = conf->reshape_safe; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4209 | sector_div(safepos, data_disks); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4210 | if (mddev->reshape_backwards) { |
NeilBrown | ed37d83 | 2009-05-27 21:39:05 +1000 | [diff] [blame] | 4211 | writepos -= min_t(sector_t, reshape_sectors, writepos); |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4212 | readpos += reshape_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4213 | safepos += reshape_sectors; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4214 | } else { |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4215 | writepos += reshape_sectors; |
NeilBrown | ed37d83 | 2009-05-27 21:39:05 +1000 | [diff] [blame] | 4216 | readpos -= min_t(sector_t, reshape_sectors, readpos); |
| 4217 | safepos -= min_t(sector_t, reshape_sectors, safepos); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4218 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4219 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 4220 | /* Having calculated the 'writepos' possibly use it |
| 4221 | * to set 'stripe_addr' which is where we will write to. |
| 4222 | */ |
| 4223 | if (mddev->reshape_backwards) { |
| 4224 | BUG_ON(conf->reshape_progress == 0); |
| 4225 | stripe_addr = writepos; |
| 4226 | BUG_ON((mddev->dev_sectors & |
| 4227 | ~((sector_t)reshape_sectors - 1)) |
| 4228 | - reshape_sectors - stripe_addr |
| 4229 | != sector_nr); |
| 4230 | } else { |
| 4231 | BUG_ON(writepos != sector_nr + reshape_sectors); |
| 4232 | stripe_addr = sector_nr; |
| 4233 | } |
| 4234 | |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4235 | /* 'writepos' is the most advanced device address we might write. |
| 4236 | * 'readpos' is the least advanced device address we might read. |
| 4237 | * 'safepos' is the least address recorded in the metadata as having |
| 4238 | * been reshaped. |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 4239 | * If there is a min_offset_diff, these are adjusted either by |
| 4240 | * increasing the safepos/readpos if diff is negative, or |
| 4241 | * increasing writepos if diff is positive. |
| 4242 | * If 'readpos' is then behind 'writepos', there is no way that we can |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4243 | * ensure safety in the face of a crash - that must be done by userspace |
| 4244 | * making a backup of the data. So in that case there is no particular |
| 4245 | * rush to update metadata. |
| 4246 | * Otherwise if 'safepos' is behind 'writepos', then we really need to |
| 4247 | * update the metadata to advance 'safepos' to match 'readpos' so that |
| 4248 | * we can be safe in the event of a crash. |
| 4249 | * So we insist on updating metadata if safepos is behind writepos and |
| 4250 | * readpos is beyond writepos. |
| 4251 | * In any case, update the metadata every 10 seconds. |
| 4252 | * Maybe that number should be configurable, but I'm not sure it is |
| 4253 | * worth it.... maybe it could be a multiple of safemode_delay??? |
| 4254 | */ |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 4255 | if (conf->min_offset_diff < 0) { |
| 4256 | safepos += -conf->min_offset_diff; |
| 4257 | readpos += -conf->min_offset_diff; |
| 4258 | } else |
| 4259 | writepos += conf->min_offset_diff; |
| 4260 | |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4261 | if ((mddev->reshape_backwards |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4262 | ? (safepos > writepos && readpos < writepos) |
| 4263 | : (safepos < writepos && readpos > writepos)) || |
| 4264 | time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4265 | /* Cannot proceed until we've updated the superblock... */ |
| 4266 | wait_event(conf->wait_for_overlap, |
| 4267 | atomic_read(&conf->reshape_stripes)==0); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4268 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4269 | mddev->curr_resync_completed = sector_nr; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4270 | conf->reshape_checkpoint = jiffies; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4271 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4272 | md_wakeup_thread(mddev->thread); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4273 | wait_event(mddev->sb_wait, mddev->flags == 0 || |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4274 | kthread_should_stop()); |
| 4275 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4276 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4277 | spin_unlock_irq(&conf->device_lock); |
| 4278 | wake_up(&conf->wait_for_overlap); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4279 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4280 | } |
| 4281 | |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4282 | INIT_LIST_HEAD(&stripes); |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4283 | for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4284 | int j; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4285 | int skipped_disk = 0; |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4286 | sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4287 | set_bit(STRIPE_EXPANDING, &sh->state); |
| 4288 | atomic_inc(&conf->reshape_stripes); |
| 4289 | /* If any of this stripe is beyond the end of the old |
| 4290 | * array, then we need to zero those blocks |
| 4291 | */ |
| 4292 | for (j=sh->disks; j--;) { |
| 4293 | sector_t s; |
| 4294 | if (j == sh->pd_idx) |
| 4295 | continue; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4296 | if (conf->level == 6 && |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4297 | j == sh->qd_idx) |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4298 | continue; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 4299 | s = compute_blocknr(sh, j, 0); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4300 | if (s < raid5_size(mddev, 0, 0)) { |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4301 | skipped_disk = 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4302 | continue; |
| 4303 | } |
| 4304 | memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE); |
| 4305 | set_bit(R5_Expanded, &sh->dev[j].flags); |
| 4306 | set_bit(R5_UPTODATE, &sh->dev[j].flags); |
| 4307 | } |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4308 | if (!skipped_disk) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4309 | set_bit(STRIPE_EXPAND_READY, &sh->state); |
| 4310 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4311 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4312 | list_add(&sh->lru, &stripes); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4313 | } |
| 4314 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4315 | if (mddev->reshape_backwards) |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4316 | conf->reshape_progress -= reshape_sectors * new_data_disks; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4317 | else |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4318 | conf->reshape_progress += reshape_sectors * new_data_disks; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4319 | spin_unlock_irq(&conf->device_lock); |
| 4320 | /* Ok, those stripe are ready. We can start scheduling |
| 4321 | * reads on the source stripes. |
| 4322 | * The source stripes are determined by mapping the first and last |
| 4323 | * block on the destination stripes. |
| 4324 | */ |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4325 | first_sector = |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4326 | raid5_compute_sector(conf, stripe_addr*(new_data_disks), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4327 | 1, &dd_idx, NULL); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4328 | last_sector = |
NeilBrown | 0e6e027 | 2009-06-09 16:32:22 +1000 | [diff] [blame] | 4329 | raid5_compute_sector(conf, ((stripe_addr+reshape_sectors) |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 4330 | * new_data_disks - 1), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4331 | 1, &dd_idx, NULL); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4332 | if (last_sector >= mddev->dev_sectors) |
| 4333 | last_sector = mddev->dev_sectors - 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4334 | while (first_sector <= last_sector) { |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4335 | sh = get_active_stripe(conf, first_sector, 1, 0, 1); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4336 | set_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 4337 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4338 | release_stripe(sh); |
| 4339 | first_sector += STRIPE_SECTORS; |
| 4340 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4341 | /* Now that the sources are clearly marked, we can release |
| 4342 | * the destination stripes |
| 4343 | */ |
| 4344 | while (!list_empty(&stripes)) { |
| 4345 | sh = list_entry(stripes.next, struct stripe_head, lru); |
| 4346 | list_del_init(&sh->lru); |
| 4347 | release_stripe(sh); |
| 4348 | } |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4349 | /* If this takes us to the resync_max point where we have to pause, |
| 4350 | * then we need to write out the superblock. |
| 4351 | */ |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4352 | sector_nr += reshape_sectors; |
NeilBrown | c03f6a1 | 2009-04-17 11:06:30 +1000 | [diff] [blame] | 4353 | if ((sector_nr - mddev->curr_resync_completed) * 2 |
| 4354 | >= mddev->resync_max - mddev->curr_resync_completed) { |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4355 | /* Cannot proceed until we've updated the superblock... */ |
| 4356 | wait_event(conf->wait_for_overlap, |
| 4357 | atomic_read(&conf->reshape_stripes) == 0); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4358 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4359 | mddev->curr_resync_completed = sector_nr; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4360 | conf->reshape_checkpoint = jiffies; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4361 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 4362 | md_wakeup_thread(mddev->thread); |
| 4363 | wait_event(mddev->sb_wait, |
| 4364 | !test_bit(MD_CHANGE_DEVS, &mddev->flags) |
| 4365 | || kthread_should_stop()); |
| 4366 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4367 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4368 | spin_unlock_irq(&conf->device_lock); |
| 4369 | wake_up(&conf->wait_for_overlap); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4370 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4371 | } |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4372 | return reshape_sectors; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4373 | } |
| 4374 | |
| 4375 | /* FIXME go_faster isn't used */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4376 | 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] | 4377 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4378 | struct r5conf *conf = mddev->private; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4379 | struct stripe_head *sh; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4380 | sector_t max_sector = mddev->dev_sectors; |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 4381 | sector_t sync_blocks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4382 | int still_degraded = 0; |
| 4383 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4384 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4385 | if (sector_nr >= max_sector) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4386 | /* just being told to finish up .. nothing much to do */ |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 4387 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 4388 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { |
| 4389 | end_reshape(conf); |
| 4390 | return 0; |
| 4391 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4392 | |
| 4393 | if (mddev->curr_resync < max_sector) /* aborted */ |
| 4394 | bitmap_end_sync(mddev->bitmap, mddev->curr_resync, |
| 4395 | &sync_blocks, 1); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4396 | else /* completed sync */ |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4397 | conf->fullsync = 0; |
| 4398 | bitmap_close_sync(mddev->bitmap); |
| 4399 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4400 | return 0; |
| 4401 | } |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4402 | |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4403 | /* Allow raid5_quiesce to complete */ |
| 4404 | wait_event(conf->wait_for_overlap, conf->quiesce != 2); |
| 4405 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4406 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 4407 | return reshape_request(mddev, sector_nr, skipped); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4408 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4409 | /* No need to check resync_max as we never do more than one |
| 4410 | * stripe, and as resync_max will always be on a chunk boundary, |
| 4411 | * if the check in md_do_sync didn't fire, there is no chance |
| 4412 | * of overstepping resync_max here |
| 4413 | */ |
| 4414 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4415 | /* if there is too many failed drives and we are trying |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4416 | * to resync, then assert that we are finished, because there is |
| 4417 | * nothing we can do. |
| 4418 | */ |
NeilBrown | 3285edf | 2006-06-26 00:27:55 -0700 | [diff] [blame] | 4419 | if (mddev->degraded >= conf->max_degraded && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4420 | test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4421 | sector_t rv = mddev->dev_sectors - sector_nr; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 4422 | *skipped = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4423 | return rv; |
| 4424 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4425 | if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) && |
NeilBrown | 3855ad9 | 2005-11-08 21:39:38 -0800 | [diff] [blame] | 4426 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4427 | !conf->fullsync && sync_blocks >= STRIPE_SECTORS) { |
| 4428 | /* we can skip this block, and probably more */ |
| 4429 | sync_blocks /= STRIPE_SECTORS; |
| 4430 | *skipped = 1; |
| 4431 | return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */ |
| 4432 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4433 | |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 4434 | bitmap_cond_end_sync(mddev->bitmap, sector_nr); |
| 4435 | |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4436 | sh = get_active_stripe(conf, sector_nr, 0, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4437 | if (sh == NULL) { |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4438 | sh = get_active_stripe(conf, sector_nr, 0, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4439 | /* make sure we don't swamp the stripe cache if someone else |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4440 | * is trying to get access |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4441 | */ |
Nishanth Aravamudan | 66c006a | 2005-11-07 01:01:17 -0800 | [diff] [blame] | 4442 | schedule_timeout_uninterruptible(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4443 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4444 | /* Need to check if array will still be degraded after recovery/resync |
| 4445 | * We don't need to check the 'failed' flag as when that gets set, |
| 4446 | * recovery aborts. |
| 4447 | */ |
NeilBrown | f001a70 | 2009-06-09 14:30:31 +1000 | [diff] [blame] | 4448 | for (i = 0; i < conf->raid_disks; i++) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4449 | if (conf->disks[i].rdev == NULL) |
| 4450 | still_degraded = 1; |
| 4451 | |
| 4452 | bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded); |
| 4453 | |
NeilBrown | 83206d6 | 2011-07-26 11:19:49 +1000 | [diff] [blame] | 4454 | set_bit(STRIPE_SYNC_REQUESTED, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4455 | |
NeilBrown | 1442577 | 2009-10-16 15:55:25 +1100 | [diff] [blame] | 4456 | handle_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4457 | release_stripe(sh); |
| 4458 | |
| 4459 | return STRIPE_SECTORS; |
| 4460 | } |
| 4461 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4462 | 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] | 4463 | { |
| 4464 | /* We may not be able to submit a whole bio at once as there |
| 4465 | * may not be enough stripe_heads available. |
| 4466 | * We cannot pre-allocate enough stripe_heads as we may need |
| 4467 | * more than exist in the cache (if we allow ever large chunks). |
| 4468 | * So we do one stripe head at a time and record in |
| 4469 | * ->bi_hw_segments how many have been done. |
| 4470 | * |
| 4471 | * We *know* that this entire raid_bio is in one chunk, so |
| 4472 | * it will be only one 'dd_idx' and only need one call to raid5_compute_sector. |
| 4473 | */ |
| 4474 | struct stripe_head *sh; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4475 | int dd_idx; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4476 | sector_t sector, logical_sector, last_sector; |
| 4477 | int scnt = 0; |
| 4478 | int remaining; |
| 4479 | int handled = 0; |
| 4480 | |
| 4481 | logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4482 | sector = raid5_compute_sector(conf, logical_sector, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4483 | 0, &dd_idx, NULL); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4484 | last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9); |
| 4485 | |
| 4486 | for (; logical_sector < last_sector; |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4487 | logical_sector += STRIPE_SECTORS, |
| 4488 | sector += STRIPE_SECTORS, |
| 4489 | scnt++) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4490 | |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4491 | if (scnt < raid5_bi_hw_segments(raid_bio)) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4492 | /* already done this stripe */ |
| 4493 | continue; |
| 4494 | |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4495 | sh = get_active_stripe(conf, sector, 0, 1, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4496 | |
| 4497 | if (!sh) { |
| 4498 | /* failed to get a stripe - must wait */ |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4499 | raid5_set_bi_hw_segments(raid_bio, scnt); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4500 | conf->retry_read_aligned = raid_bio; |
| 4501 | return handled; |
| 4502 | } |
| 4503 | |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4504 | if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) { |
| 4505 | release_stripe(sh); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4506 | raid5_set_bi_hw_segments(raid_bio, scnt); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4507 | conf->retry_read_aligned = raid_bio; |
| 4508 | return handled; |
| 4509 | } |
| 4510 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4511 | handle_stripe(sh); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4512 | release_stripe(sh); |
| 4513 | handled++; |
| 4514 | } |
| 4515 | spin_lock_irq(&conf->device_lock); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4516 | remaining = raid5_dec_bi_phys_segments(raid_bio); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4517 | spin_unlock_irq(&conf->device_lock); |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 4518 | if (remaining == 0) |
| 4519 | bio_endio(raid_bio, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4520 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
| 4521 | wake_up(&conf->wait_for_stripe); |
| 4522 | return handled; |
| 4523 | } |
| 4524 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4526 | /* |
| 4527 | * This is our raid5 kernel thread. |
| 4528 | * |
| 4529 | * We scan the hash table for stripes which can be handled now. |
| 4530 | * During the scan, completed stripes are saved for us by the interrupt |
| 4531 | * handler, so that they will not have to wait for our next wakeup. |
| 4532 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4533 | static void raid5d(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4534 | { |
| 4535 | struct stripe_head *sh; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4536 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4537 | int handled; |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 4538 | struct blk_plug plug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4539 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4540 | pr_debug("+++ raid5d active\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4541 | |
| 4542 | md_check_recovery(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4543 | |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 4544 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4545 | handled = 0; |
| 4546 | spin_lock_irq(&conf->device_lock); |
| 4547 | while (1) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4548 | struct bio *bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4549 | |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 4550 | if (atomic_read(&mddev->plug_cnt) == 0 && |
| 4551 | !list_empty(&conf->bitmap_list)) { |
| 4552 | /* Now is a good time to flush some bitmap updates */ |
| 4553 | conf->seq_flush++; |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 4554 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4555 | bitmap_unplug(mddev->bitmap); |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 4556 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 4557 | conf->seq_write = conf->seq_flush; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4558 | activate_bit_delay(conf); |
| 4559 | } |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 4560 | if (atomic_read(&mddev->plug_cnt) == 0) |
| 4561 | raid5_activate_delayed(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4562 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4563 | while ((bio = remove_bio_from_retry(conf))) { |
| 4564 | int ok; |
| 4565 | spin_unlock_irq(&conf->device_lock); |
| 4566 | ok = retry_aligned_read(conf, bio); |
| 4567 | spin_lock_irq(&conf->device_lock); |
| 4568 | if (!ok) |
| 4569 | break; |
| 4570 | handled++; |
| 4571 | } |
| 4572 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4573 | sh = __get_priority_stripe(conf); |
| 4574 | |
Dan Williams | c9f21aa | 2008-07-23 12:05:51 -0700 | [diff] [blame] | 4575 | if (!sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4576 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4577 | spin_unlock_irq(&conf->device_lock); |
| 4578 | |
| 4579 | handled++; |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 4580 | handle_stripe(sh); |
| 4581 | release_stripe(sh); |
| 4582 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4583 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 4584 | if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) |
| 4585 | md_check_recovery(mddev); |
| 4586 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4587 | spin_lock_irq(&conf->device_lock); |
| 4588 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4589 | pr_debug("%d stripes handled\n", handled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4590 | |
| 4591 | spin_unlock_irq(&conf->device_lock); |
| 4592 | |
Dan Williams | c9f21aa | 2008-07-23 12:05:51 -0700 | [diff] [blame] | 4593 | async_tx_issue_pending_all(); |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 4594 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4595 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4596 | pr_debug("--- raid5d inactive\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4597 | } |
| 4598 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4599 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4600 | raid5_show_stripe_cache_size(struct mddev *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4601 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4602 | struct r5conf *conf = mddev->private; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4603 | if (conf) |
| 4604 | return sprintf(page, "%d\n", conf->max_nr_stripes); |
| 4605 | else |
| 4606 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4607 | } |
| 4608 | |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 4609 | int |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4610 | raid5_set_cache_size(struct mddev *mddev, int size) |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 4611 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4612 | struct r5conf *conf = mddev->private; |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 4613 | int err; |
| 4614 | |
| 4615 | if (size <= 16 || size > 32768) |
| 4616 | return -EINVAL; |
| 4617 | while (size < conf->max_nr_stripes) { |
| 4618 | if (drop_one_stripe(conf)) |
| 4619 | conf->max_nr_stripes--; |
| 4620 | else |
| 4621 | break; |
| 4622 | } |
| 4623 | err = md_allow_write(mddev); |
| 4624 | if (err) |
| 4625 | return err; |
| 4626 | while (size > conf->max_nr_stripes) { |
| 4627 | if (grow_one_stripe(conf)) |
| 4628 | conf->max_nr_stripes++; |
| 4629 | else break; |
| 4630 | } |
| 4631 | return 0; |
| 4632 | } |
| 4633 | EXPORT_SYMBOL(raid5_set_cache_size); |
| 4634 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4635 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4636 | 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] | 4637 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4638 | struct r5conf *conf = mddev->private; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4639 | unsigned long new; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4640 | int err; |
| 4641 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4642 | if (len >= PAGE_SIZE) |
| 4643 | return -EINVAL; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4644 | if (!conf) |
| 4645 | return -ENODEV; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4646 | |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4647 | if (strict_strtoul(page, 10, &new)) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4648 | return -EINVAL; |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 4649 | err = raid5_set_cache_size(mddev, new); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4650 | if (err) |
| 4651 | return err; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4652 | return len; |
| 4653 | } |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4654 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4655 | static struct md_sysfs_entry |
| 4656 | raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR, |
| 4657 | raid5_show_stripe_cache_size, |
| 4658 | raid5_store_stripe_cache_size); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4659 | |
| 4660 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4661 | raid5_show_preread_threshold(struct mddev *mddev, char *page) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4662 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4663 | struct r5conf *conf = mddev->private; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4664 | if (conf) |
| 4665 | return sprintf(page, "%d\n", conf->bypass_threshold); |
| 4666 | else |
| 4667 | return 0; |
| 4668 | } |
| 4669 | |
| 4670 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4671 | 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] | 4672 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4673 | struct r5conf *conf = mddev->private; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4674 | unsigned long new; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4675 | if (len >= PAGE_SIZE) |
| 4676 | return -EINVAL; |
| 4677 | if (!conf) |
| 4678 | return -ENODEV; |
| 4679 | |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4680 | if (strict_strtoul(page, 10, &new)) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4681 | return -EINVAL; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4682 | if (new > conf->max_nr_stripes) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4683 | return -EINVAL; |
| 4684 | conf->bypass_threshold = new; |
| 4685 | return len; |
| 4686 | } |
| 4687 | |
| 4688 | static struct md_sysfs_entry |
| 4689 | raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold, |
| 4690 | S_IRUGO | S_IWUSR, |
| 4691 | raid5_show_preread_threshold, |
| 4692 | raid5_store_preread_threshold); |
| 4693 | |
| 4694 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4695 | stripe_cache_active_show(struct mddev *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4696 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4697 | struct r5conf *conf = mddev->private; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4698 | if (conf) |
| 4699 | return sprintf(page, "%d\n", atomic_read(&conf->active_stripes)); |
| 4700 | else |
| 4701 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4702 | } |
| 4703 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4704 | static struct md_sysfs_entry |
| 4705 | raid5_stripecache_active = __ATTR_RO(stripe_cache_active); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4706 | |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4707 | static struct attribute *raid5_attrs[] = { |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4708 | &raid5_stripecache_size.attr, |
| 4709 | &raid5_stripecache_active.attr, |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4710 | &raid5_preread_bypass_threshold.attr, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4711 | NULL, |
| 4712 | }; |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4713 | static struct attribute_group raid5_attrs_group = { |
| 4714 | .name = NULL, |
| 4715 | .attrs = raid5_attrs, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4716 | }; |
| 4717 | |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4718 | static sector_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4719 | raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks) |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4720 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4721 | struct r5conf *conf = mddev->private; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4722 | |
| 4723 | if (!sectors) |
| 4724 | sectors = mddev->dev_sectors; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4725 | if (!raid_disks) |
NeilBrown | 7ec0547 | 2009-03-31 15:10:36 +1100 | [diff] [blame] | 4726 | /* size is defined by the smallest of previous and new size */ |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4727 | raid_disks = min(conf->raid_disks, conf->previous_raid_disks); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4728 | |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4729 | sectors &= ~((sector_t)mddev->chunk_sectors - 1); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4730 | sectors &= ~((sector_t)mddev->new_chunk_sectors - 1); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4731 | return sectors * (raid_disks - conf->max_degraded); |
| 4732 | } |
| 4733 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4734 | static void raid5_free_percpu(struct r5conf *conf) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4735 | { |
| 4736 | struct raid5_percpu *percpu; |
| 4737 | unsigned long cpu; |
| 4738 | |
| 4739 | if (!conf->percpu) |
| 4740 | return; |
| 4741 | |
| 4742 | get_online_cpus(); |
| 4743 | for_each_possible_cpu(cpu) { |
| 4744 | percpu = per_cpu_ptr(conf->percpu, cpu); |
| 4745 | safe_put_page(percpu->spare_page); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4746 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4747 | } |
| 4748 | #ifdef CONFIG_HOTPLUG_CPU |
| 4749 | unregister_cpu_notifier(&conf->cpu_notify); |
| 4750 | #endif |
| 4751 | put_online_cpus(); |
| 4752 | |
| 4753 | free_percpu(conf->percpu); |
| 4754 | } |
| 4755 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4756 | static void free_conf(struct r5conf *conf) |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 4757 | { |
| 4758 | shrink_stripes(conf); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4759 | raid5_free_percpu(conf); |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 4760 | kfree(conf->disks); |
| 4761 | kfree(conf->stripe_hashtbl); |
| 4762 | kfree(conf); |
| 4763 | } |
| 4764 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4765 | #ifdef CONFIG_HOTPLUG_CPU |
| 4766 | static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action, |
| 4767 | void *hcpu) |
| 4768 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4769 | struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4770 | long cpu = (long)hcpu; |
| 4771 | struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu); |
| 4772 | |
| 4773 | switch (action) { |
| 4774 | case CPU_UP_PREPARE: |
| 4775 | case CPU_UP_PREPARE_FROZEN: |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4776 | if (conf->level == 6 && !percpu->spare_page) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4777 | percpu->spare_page = alloc_page(GFP_KERNEL); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4778 | if (!percpu->scribble) |
| 4779 | percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL); |
| 4780 | |
| 4781 | if (!percpu->scribble || |
| 4782 | (conf->level == 6 && !percpu->spare_page)) { |
| 4783 | safe_put_page(percpu->spare_page); |
| 4784 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4785 | pr_err("%s: failed memory allocation for cpu%ld\n", |
| 4786 | __func__, cpu); |
Akinobu Mita | 55af6bb | 2010-05-26 14:43:35 -0700 | [diff] [blame] | 4787 | return notifier_from_errno(-ENOMEM); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4788 | } |
| 4789 | break; |
| 4790 | case CPU_DEAD: |
| 4791 | case CPU_DEAD_FROZEN: |
| 4792 | safe_put_page(percpu->spare_page); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4793 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4794 | percpu->spare_page = NULL; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4795 | percpu->scribble = NULL; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4796 | break; |
| 4797 | default: |
| 4798 | break; |
| 4799 | } |
| 4800 | return NOTIFY_OK; |
| 4801 | } |
| 4802 | #endif |
| 4803 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4804 | static int raid5_alloc_percpu(struct r5conf *conf) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4805 | { |
| 4806 | unsigned long cpu; |
| 4807 | struct page *spare_page; |
Tejun Heo | a29d8b8 | 2010-02-02 14:39:15 +0900 | [diff] [blame] | 4808 | struct raid5_percpu __percpu *allcpus; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4809 | void *scribble; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4810 | int err; |
| 4811 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4812 | allcpus = alloc_percpu(struct raid5_percpu); |
| 4813 | if (!allcpus) |
| 4814 | return -ENOMEM; |
| 4815 | conf->percpu = allcpus; |
| 4816 | |
| 4817 | get_online_cpus(); |
| 4818 | err = 0; |
| 4819 | for_each_present_cpu(cpu) { |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4820 | if (conf->level == 6) { |
| 4821 | spare_page = alloc_page(GFP_KERNEL); |
| 4822 | if (!spare_page) { |
| 4823 | err = -ENOMEM; |
| 4824 | break; |
| 4825 | } |
| 4826 | per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page; |
| 4827 | } |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4828 | scribble = kmalloc(conf->scribble_len, GFP_KERNEL); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4829 | if (!scribble) { |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4830 | err = -ENOMEM; |
| 4831 | break; |
| 4832 | } |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4833 | per_cpu_ptr(conf->percpu, cpu)->scribble = scribble; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4834 | } |
| 4835 | #ifdef CONFIG_HOTPLUG_CPU |
| 4836 | conf->cpu_notify.notifier_call = raid456_cpu_notify; |
| 4837 | conf->cpu_notify.priority = 0; |
| 4838 | if (err == 0) |
| 4839 | err = register_cpu_notifier(&conf->cpu_notify); |
| 4840 | #endif |
| 4841 | put_online_cpus(); |
| 4842 | |
| 4843 | return err; |
| 4844 | } |
| 4845 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4846 | static struct r5conf *setup_conf(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4847 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4848 | struct r5conf *conf; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4849 | int raid_disk, memory, max_disks; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4850 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4851 | struct disk_info *disk; |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 4852 | char pers_name[6]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4853 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4854 | if (mddev->new_level != 5 |
| 4855 | && mddev->new_level != 4 |
| 4856 | && mddev->new_level != 6) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4857 | 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] | 4858 | mdname(mddev), mddev->new_level); |
| 4859 | return ERR_PTR(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4860 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4861 | if ((mddev->new_level == 5 |
| 4862 | && !algorithm_valid_raid5(mddev->new_layout)) || |
| 4863 | (mddev->new_level == 6 |
| 4864 | && !algorithm_valid_raid6(mddev->new_layout))) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4865 | printk(KERN_ERR "md/raid:%s: layout %d not supported\n", |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4866 | mdname(mddev), mddev->new_layout); |
| 4867 | return ERR_PTR(-EIO); |
| 4868 | } |
| 4869 | if (mddev->new_level == 6 && mddev->raid_disks < 4) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4870 | 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] | 4871 | mdname(mddev), mddev->raid_disks); |
| 4872 | return ERR_PTR(-EINVAL); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4873 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4874 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4875 | if (!mddev->new_chunk_sectors || |
| 4876 | (mddev->new_chunk_sectors << 9) % PAGE_SIZE || |
| 4877 | !is_power_of_2(mddev->new_chunk_sectors)) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4878 | printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n", |
| 4879 | mdname(mddev), mddev->new_chunk_sectors << 9); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4880 | return ERR_PTR(-EINVAL); |
NeilBrown | 4bbf377 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4881 | } |
| 4882 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4883 | conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4884 | if (conf == NULL) |
| 4885 | goto abort; |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 4886 | spin_lock_init(&conf->device_lock); |
| 4887 | init_waitqueue_head(&conf->wait_for_stripe); |
| 4888 | init_waitqueue_head(&conf->wait_for_overlap); |
| 4889 | INIT_LIST_HEAD(&conf->handle_list); |
| 4890 | INIT_LIST_HEAD(&conf->hold_list); |
| 4891 | INIT_LIST_HEAD(&conf->delayed_list); |
| 4892 | INIT_LIST_HEAD(&conf->bitmap_list); |
| 4893 | INIT_LIST_HEAD(&conf->inactive_list); |
| 4894 | atomic_set(&conf->active_stripes, 0); |
| 4895 | atomic_set(&conf->preread_active_stripes, 0); |
| 4896 | atomic_set(&conf->active_aligned_reads, 0); |
| 4897 | conf->bypass_threshold = BYPASS_THRESHOLD; |
NeilBrown | d890fa2 | 2011-10-26 11:54:39 +1100 | [diff] [blame] | 4898 | conf->recovery_disabled = mddev->recovery_disabled - 1; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4899 | |
| 4900 | conf->raid_disks = mddev->raid_disks; |
| 4901 | if (mddev->reshape_position == MaxSector) |
| 4902 | conf->previous_raid_disks = mddev->raid_disks; |
| 4903 | else |
| 4904 | conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4905 | max_disks = max(conf->raid_disks, conf->previous_raid_disks); |
| 4906 | conf->scribble_len = scribble_len(max_disks); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4907 | |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4908 | conf->disks = kzalloc(max_disks * sizeof(struct disk_info), |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4909 | GFP_KERNEL); |
| 4910 | if (!conf->disks) |
| 4911 | goto abort; |
| 4912 | |
| 4913 | conf->mddev = mddev; |
| 4914 | |
| 4915 | if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL) |
| 4916 | goto abort; |
| 4917 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4918 | conf->level = mddev->new_level; |
| 4919 | if (raid5_alloc_percpu(conf) != 0) |
| 4920 | goto abort; |
| 4921 | |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4922 | pr_debug("raid456: run(%s) called.\n", mdname(mddev)); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4923 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 4924 | rdev_for_each(rdev, mddev) { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4925 | raid_disk = rdev->raid_disk; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4926 | if (raid_disk >= max_disks |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4927 | || raid_disk < 0) |
| 4928 | continue; |
| 4929 | disk = conf->disks + raid_disk; |
| 4930 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4931 | if (test_bit(Replacement, &rdev->flags)) { |
| 4932 | if (disk->replacement) |
| 4933 | goto abort; |
| 4934 | disk->replacement = rdev; |
| 4935 | } else { |
| 4936 | if (disk->rdev) |
| 4937 | goto abort; |
| 4938 | disk->rdev = rdev; |
| 4939 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4940 | |
| 4941 | if (test_bit(In_sync, &rdev->flags)) { |
| 4942 | char b[BDEVNAME_SIZE]; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4943 | printk(KERN_INFO "md/raid:%s: device %s operational as raid" |
| 4944 | " disk %d\n", |
| 4945 | mdname(mddev), bdevname(rdev->bdev, b), raid_disk); |
Jonathan Brassow | d6b212f | 2011-06-08 18:00:28 -0500 | [diff] [blame] | 4946 | } else if (rdev->saved_raid_disk != raid_disk) |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4947 | /* Cannot rely on bitmap to complete recovery */ |
| 4948 | conf->fullsync = 1; |
| 4949 | } |
| 4950 | |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 4951 | conf->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4952 | conf->level = mddev->new_level; |
| 4953 | if (conf->level == 6) |
| 4954 | conf->max_degraded = 2; |
| 4955 | else |
| 4956 | conf->max_degraded = 1; |
| 4957 | conf->algorithm = mddev->new_layout; |
| 4958 | conf->max_nr_stripes = NR_STRIPES; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4959 | conf->reshape_progress = mddev->reshape_position; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 4960 | if (conf->reshape_progress != MaxSector) { |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 4961 | conf->prev_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 4962 | conf->prev_algo = mddev->layout; |
| 4963 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4964 | |
| 4965 | memory = conf->max_nr_stripes * (sizeof(struct stripe_head) + |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4966 | max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4967 | if (grow_stripes(conf, conf->max_nr_stripes)) { |
| 4968 | printk(KERN_ERR |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4969 | "md/raid:%s: couldn't allocate %dkB for buffers\n", |
| 4970 | mdname(mddev), memory); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4971 | goto abort; |
| 4972 | } else |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4973 | printk(KERN_INFO "md/raid:%s: allocated %dkB\n", |
| 4974 | mdname(mddev), memory); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4975 | |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 4976 | sprintf(pers_name, "raid%d", mddev->new_level); |
| 4977 | conf->thread = md_register_thread(raid5d, mddev, pers_name); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4978 | if (!conf->thread) { |
| 4979 | printk(KERN_ERR |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4980 | "md/raid:%s: couldn't allocate thread.\n", |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4981 | mdname(mddev)); |
| 4982 | goto abort; |
| 4983 | } |
| 4984 | |
| 4985 | return conf; |
| 4986 | |
| 4987 | abort: |
| 4988 | if (conf) { |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 4989 | free_conf(conf); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4990 | return ERR_PTR(-EIO); |
| 4991 | } else |
| 4992 | return ERR_PTR(-ENOMEM); |
| 4993 | } |
| 4994 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 4995 | |
| 4996 | static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded) |
| 4997 | { |
| 4998 | switch (algo) { |
| 4999 | case ALGORITHM_PARITY_0: |
| 5000 | if (raid_disk < max_degraded) |
| 5001 | return 1; |
| 5002 | break; |
| 5003 | case ALGORITHM_PARITY_N: |
| 5004 | if (raid_disk >= raid_disks - max_degraded) |
| 5005 | return 1; |
| 5006 | break; |
| 5007 | case ALGORITHM_PARITY_0_6: |
| 5008 | if (raid_disk == 0 || |
| 5009 | raid_disk == raid_disks - 1) |
| 5010 | return 1; |
| 5011 | break; |
| 5012 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 5013 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 5014 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 5015 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 5016 | if (raid_disk == raid_disks - 1) |
| 5017 | return 1; |
| 5018 | } |
| 5019 | return 0; |
| 5020 | } |
| 5021 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5022 | static int run(struct mddev *mddev) |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5023 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5024 | struct r5conf *conf; |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 5025 | int working_disks = 0; |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5026 | int dirty_parity_disks = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5027 | struct md_rdev *rdev; |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5028 | sector_t reshape_offset = 0; |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5029 | int i; |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5030 | long long min_offset_diff = 0; |
| 5031 | int first = 1; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5032 | |
Andre Noll | 8c6ac86 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 5033 | if (mddev->recovery_cp != MaxSector) |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5034 | printk(KERN_NOTICE "md/raid:%s: not clean" |
Andre Noll | 8c6ac86 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 5035 | " -- starting background reconstruction\n", |
| 5036 | mdname(mddev)); |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5037 | |
| 5038 | rdev_for_each(rdev, mddev) { |
| 5039 | long long diff; |
| 5040 | if (rdev->raid_disk < 0) |
| 5041 | continue; |
| 5042 | diff = (rdev->new_data_offset - rdev->data_offset); |
| 5043 | if (first) { |
| 5044 | min_offset_diff = diff; |
| 5045 | first = 0; |
| 5046 | } else if (mddev->reshape_backwards && |
| 5047 | diff < min_offset_diff) |
| 5048 | min_offset_diff = diff; |
| 5049 | else if (!mddev->reshape_backwards && |
| 5050 | diff > min_offset_diff) |
| 5051 | min_offset_diff = diff; |
| 5052 | } |
| 5053 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5054 | if (mddev->reshape_position != MaxSector) { |
| 5055 | /* Check that we can continue the reshape. |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5056 | * Difficulties arise if the stripe we would write to |
| 5057 | * next is at or after the stripe we would read from next. |
| 5058 | * For a reshape that changes the number of devices, this |
| 5059 | * is only possible for a very short time, and mdadm makes |
| 5060 | * sure that time appears to have past before assembling |
| 5061 | * the array. So we fail if that time hasn't passed. |
| 5062 | * For a reshape that keeps the number of devices the same |
| 5063 | * mdadm must be monitoring the reshape can keeping the |
| 5064 | * critical areas read-only and backed up. It will start |
| 5065 | * the array in read-only mode, so we check for that. |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5066 | */ |
| 5067 | sector_t here_new, here_old; |
| 5068 | int old_disks; |
Andre Noll | 18b0033 | 2009-03-31 15:00:56 +1100 | [diff] [blame] | 5069 | int max_degraded = (mddev->level == 6 ? 2 : 1); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5070 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5071 | if (mddev->new_level != mddev->level) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5072 | printk(KERN_ERR "md/raid:%s: unsupported reshape " |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5073 | "required - aborting.\n", |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5074 | mdname(mddev)); |
| 5075 | return -EINVAL; |
| 5076 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5077 | old_disks = mddev->raid_disks - mddev->delta_disks; |
| 5078 | /* reshape_position must be on a new-stripe boundary, and one |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5079 | * further up in new geometry must map after here in old |
| 5080 | * geometry. |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5081 | */ |
| 5082 | here_new = mddev->reshape_position; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5083 | if (sector_div(here_new, mddev->new_chunk_sectors * |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5084 | (mddev->raid_disks - max_degraded))) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5085 | printk(KERN_ERR "md/raid:%s: reshape_position not " |
| 5086 | "on a stripe boundary\n", mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5087 | return -EINVAL; |
| 5088 | } |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5089 | reshape_offset = here_new * mddev->new_chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5090 | /* here_new is the stripe we will write to */ |
| 5091 | here_old = mddev->reshape_position; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5092 | sector_div(here_old, mddev->chunk_sectors * |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5093 | (old_disks-max_degraded)); |
| 5094 | /* here_old is the first stripe that we might need to read |
| 5095 | * from */ |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5096 | if (mddev->delta_disks == 0) { |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5097 | if ((here_new * mddev->new_chunk_sectors != |
| 5098 | here_old * mddev->chunk_sectors)) { |
| 5099 | printk(KERN_ERR "md/raid:%s: reshape position is" |
| 5100 | " confused - aborting\n", mdname(mddev)); |
| 5101 | return -EINVAL; |
| 5102 | } |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5103 | /* We cannot be sure it is safe to start an in-place |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5104 | * reshape. It is only safe if user-space is monitoring |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5105 | * and taking constant backups. |
| 5106 | * mdadm always starts a situation like this in |
| 5107 | * readonly mode so it can take control before |
| 5108 | * allowing any writes. So just check for that. |
| 5109 | */ |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5110 | if (abs(min_offset_diff) >= mddev->chunk_sectors && |
| 5111 | abs(min_offset_diff) >= mddev->new_chunk_sectors) |
| 5112 | /* not really in-place - so OK */; |
| 5113 | else if (mddev->ro == 0) { |
| 5114 | printk(KERN_ERR "md/raid:%s: in-place reshape " |
| 5115 | "must be started in read-only mode " |
| 5116 | "- aborting\n", |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5117 | mdname(mddev)); |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5118 | return -EINVAL; |
| 5119 | } |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5120 | } else if (mddev->reshape_backwards |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5121 | ? (here_new * mddev->new_chunk_sectors + min_offset_diff <= |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5122 | here_old * mddev->chunk_sectors) |
| 5123 | : (here_new * mddev->new_chunk_sectors >= |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5124 | here_old * mddev->chunk_sectors + (-min_offset_diff))) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5125 | /* Reading from the same stripe as writing to - bad */ |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5126 | printk(KERN_ERR "md/raid:%s: reshape_position too early for " |
| 5127 | "auto-recovery - aborting.\n", |
| 5128 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5129 | return -EINVAL; |
| 5130 | } |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5131 | printk(KERN_INFO "md/raid:%s: reshape will continue\n", |
| 5132 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5133 | /* OK, we should be able to continue; */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5134 | } else { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5135 | BUG_ON(mddev->level != mddev->new_level); |
| 5136 | BUG_ON(mddev->layout != mddev->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5137 | BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5138 | BUG_ON(mddev->delta_disks != 0); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5139 | } |
| 5140 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5141 | if (mddev->private == NULL) |
| 5142 | conf = setup_conf(mddev); |
| 5143 | else |
| 5144 | conf = mddev->private; |
| 5145 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5146 | if (IS_ERR(conf)) |
| 5147 | return PTR_ERR(conf); |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 5148 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5149 | conf->min_offset_diff = min_offset_diff; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5150 | mddev->thread = conf->thread; |
| 5151 | conf->thread = NULL; |
| 5152 | mddev->private = conf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5153 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5154 | for (i = 0; i < conf->raid_disks && conf->previous_raid_disks; |
| 5155 | i++) { |
| 5156 | rdev = conf->disks[i].rdev; |
| 5157 | if (!rdev && conf->disks[i].replacement) { |
| 5158 | /* The replacement is all we have yet */ |
| 5159 | rdev = conf->disks[i].replacement; |
| 5160 | conf->disks[i].replacement = NULL; |
| 5161 | clear_bit(Replacement, &rdev->flags); |
| 5162 | conf->disks[i].rdev = rdev; |
| 5163 | } |
| 5164 | if (!rdev) |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5165 | continue; |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5166 | if (conf->disks[i].replacement && |
| 5167 | conf->reshape_progress != MaxSector) { |
| 5168 | /* replacements and reshape simply do not mix. */ |
| 5169 | printk(KERN_ERR "md: cannot handle concurrent " |
| 5170 | "replacement and reshape.\n"); |
| 5171 | goto abort; |
| 5172 | } |
NeilBrown | 2f11588 | 2010-06-17 17:41:03 +1000 | [diff] [blame] | 5173 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5174 | working_disks++; |
NeilBrown | 2f11588 | 2010-06-17 17:41:03 +1000 | [diff] [blame] | 5175 | continue; |
| 5176 | } |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5177 | /* This disc is not fully in-sync. However if it |
| 5178 | * just stored parity (beyond the recovery_offset), |
| 5179 | * when we don't need to be concerned about the |
| 5180 | * array being dirty. |
| 5181 | * When reshape goes 'backwards', we never have |
| 5182 | * partially completed devices, so we only need |
| 5183 | * to worry about reshape going forwards. |
| 5184 | */ |
| 5185 | /* Hack because v0.91 doesn't store recovery_offset properly. */ |
| 5186 | if (mddev->major_version == 0 && |
| 5187 | mddev->minor_version > 90) |
| 5188 | rdev->recovery_offset = reshape_offset; |
| 5189 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5190 | if (rdev->recovery_offset < reshape_offset) { |
| 5191 | /* We need to check old and new layout */ |
| 5192 | if (!only_parity(rdev->raid_disk, |
| 5193 | conf->algorithm, |
| 5194 | conf->raid_disks, |
| 5195 | conf->max_degraded)) |
| 5196 | continue; |
| 5197 | } |
| 5198 | if (!only_parity(rdev->raid_disk, |
| 5199 | conf->prev_algo, |
| 5200 | conf->previous_raid_disks, |
| 5201 | conf->max_degraded)) |
| 5202 | continue; |
| 5203 | dirty_parity_disks++; |
| 5204 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5205 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5206 | /* |
| 5207 | * 0 for a fully functional array, 1 or 2 for a degraded array. |
| 5208 | */ |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 5209 | mddev->degraded = calc_degraded(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5210 | |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 5211 | if (has_failed(conf)) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5212 | printk(KERN_ERR "md/raid:%s: not enough operational devices" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5213 | " (%d/%d failed)\n", |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 5214 | mdname(mddev), mddev->degraded, conf->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5215 | goto abort; |
| 5216 | } |
| 5217 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5218 | /* device size must be a multiple of chunk size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5219 | mddev->dev_sectors &= ~(mddev->chunk_sectors - 1); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5220 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 5221 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5222 | if (mddev->degraded > dirty_parity_disks && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5223 | mddev->recovery_cp != MaxSector) { |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5224 | if (mddev->ok_start_degraded) |
| 5225 | printk(KERN_WARNING |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5226 | "md/raid:%s: starting dirty degraded array" |
| 5227 | " - data corruption possible.\n", |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5228 | mdname(mddev)); |
| 5229 | else { |
| 5230 | printk(KERN_ERR |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5231 | "md/raid:%s: cannot start dirty degraded array.\n", |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5232 | mdname(mddev)); |
| 5233 | goto abort; |
| 5234 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5235 | } |
| 5236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5237 | if (mddev->degraded == 0) |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5238 | printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d" |
| 5239 | " devices, algorithm %d\n", mdname(mddev), conf->level, |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 5240 | mddev->raid_disks-mddev->degraded, mddev->raid_disks, |
| 5241 | mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5242 | else |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5243 | printk(KERN_ALERT "md/raid:%s: raid level %d active with %d" |
| 5244 | " out of %d devices, algorithm %d\n", |
| 5245 | mdname(mddev), conf->level, |
| 5246 | mddev->raid_disks - mddev->degraded, |
| 5247 | mddev->raid_disks, mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5248 | |
| 5249 | print_raid5_conf(conf); |
| 5250 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5251 | if (conf->reshape_progress != MaxSector) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5252 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5253 | atomic_set(&conf->reshape_stripes, 0); |
| 5254 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 5255 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 5256 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 5257 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 5258 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 5259 | "reshape"); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5260 | } |
| 5261 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5262 | |
| 5263 | /* Ok, everything is just fine now */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 5264 | if (mddev->to_remove == &raid5_attrs_group) |
| 5265 | mddev->to_remove = NULL; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5266 | else if (mddev->kobj.sd && |
| 5267 | sysfs_create_group(&mddev->kobj, &raid5_attrs_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5268 | printk(KERN_WARNING |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 5269 | "raid5: failed to create sysfs attributes for %s\n", |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5270 | mdname(mddev)); |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 5271 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
| 5272 | |
| 5273 | if (mddev->queue) { |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 5274 | int chunk_size; |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 5275 | /* read-ahead size must cover two whole stripes, which |
| 5276 | * is 2 * (datadisks) * chunksize where 'n' is the |
| 5277 | * number of raid devices |
| 5278 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5279 | int data_disks = conf->previous_raid_disks - conf->max_degraded; |
| 5280 | int stripe = data_disks * |
| 5281 | ((mddev->chunk_sectors << 9) / PAGE_SIZE); |
| 5282 | if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 5283 | mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 5284 | |
| 5285 | blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec); |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 5286 | |
| 5287 | mddev->queue->backing_dev_info.congested_data = mddev; |
| 5288 | mddev->queue->backing_dev_info.congested_fn = raid5_congested; |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 5289 | |
| 5290 | chunk_size = mddev->chunk_sectors << 9; |
| 5291 | blk_queue_io_min(mddev->queue, chunk_size); |
| 5292 | blk_queue_io_opt(mddev->queue, chunk_size * |
| 5293 | (conf->raid_disks - conf->max_degraded)); |
| 5294 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5295 | rdev_for_each(rdev, mddev) { |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 5296 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 5297 | rdev->data_offset << 9); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5298 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 5299 | rdev->new_data_offset << 9); |
| 5300 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5301 | } |
| 5302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5303 | return 0; |
| 5304 | abort: |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 5305 | md_unregister_thread(&mddev->thread); |
NeilBrown | e4f869d | 2011-10-07 14:22:49 +1100 | [diff] [blame] | 5306 | print_raid5_conf(conf); |
| 5307 | free_conf(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5308 | mddev->private = NULL; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5309 | 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] | 5310 | return -EIO; |
| 5311 | } |
| 5312 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5313 | static int stop(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5314 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5315 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5316 | |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 5317 | md_unregister_thread(&mddev->thread); |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 5318 | if (mddev->queue) |
| 5319 | mddev->queue->backing_dev_info.congested_fn = NULL; |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 5320 | free_conf(conf); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 5321 | mddev->private = NULL; |
| 5322 | mddev->to_remove = &raid5_attrs_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5323 | return 0; |
| 5324 | } |
| 5325 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5326 | static void status(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5327 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5328 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5329 | int i; |
| 5330 | |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5331 | seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level, |
| 5332 | mddev->chunk_sectors / 2, mddev->layout); |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 5333 | 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] | 5334 | for (i = 0; i < conf->raid_disks; i++) |
| 5335 | seq_printf (seq, "%s", |
| 5336 | conf->disks[i].rdev && |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5337 | test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5338 | seq_printf (seq, "]"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5339 | } |
| 5340 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5341 | static void print_raid5_conf (struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5342 | { |
| 5343 | int i; |
| 5344 | struct disk_info *tmp; |
| 5345 | |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5346 | printk(KERN_DEBUG "RAID conf printout:\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5347 | if (!conf) { |
| 5348 | printk("(conf==NULL)\n"); |
| 5349 | return; |
| 5350 | } |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5351 | printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level, |
| 5352 | conf->raid_disks, |
| 5353 | conf->raid_disks - conf->mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5354 | |
| 5355 | for (i = 0; i < conf->raid_disks; i++) { |
| 5356 | char b[BDEVNAME_SIZE]; |
| 5357 | tmp = conf->disks + i; |
| 5358 | if (tmp->rdev) |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5359 | printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n", |
| 5360 | i, !test_bit(Faulty, &tmp->rdev->flags), |
| 5361 | bdevname(tmp->rdev->bdev, b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5362 | } |
| 5363 | } |
| 5364 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5365 | static int raid5_spare_active(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5366 | { |
| 5367 | int i; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5368 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5369 | struct disk_info *tmp; |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5370 | int count = 0; |
| 5371 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5372 | |
| 5373 | for (i = 0; i < conf->raid_disks; i++) { |
| 5374 | tmp = conf->disks + i; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5375 | if (tmp->replacement |
| 5376 | && tmp->replacement->recovery_offset == MaxSector |
| 5377 | && !test_bit(Faulty, &tmp->replacement->flags) |
| 5378 | && !test_and_set_bit(In_sync, &tmp->replacement->flags)) { |
| 5379 | /* Replacement has just become active. */ |
| 5380 | if (!tmp->rdev |
| 5381 | || !test_and_clear_bit(In_sync, &tmp->rdev->flags)) |
| 5382 | count++; |
| 5383 | if (tmp->rdev) { |
| 5384 | /* Replaced device not technically faulty, |
| 5385 | * but we need to be sure it gets removed |
| 5386 | * and never re-added. |
| 5387 | */ |
| 5388 | set_bit(Faulty, &tmp->rdev->flags); |
| 5389 | sysfs_notify_dirent_safe( |
| 5390 | tmp->rdev->sysfs_state); |
| 5391 | } |
| 5392 | sysfs_notify_dirent_safe(tmp->replacement->sysfs_state); |
| 5393 | } else if (tmp->rdev |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 5394 | && tmp->rdev->recovery_offset == MaxSector |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5395 | && !test_bit(Faulty, &tmp->rdev->flags) |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 5396 | && !test_and_set_bit(In_sync, &tmp->rdev->flags)) { |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5397 | count++; |
Jonathan Brassow | 43c73ca | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5398 | sysfs_notify_dirent_safe(tmp->rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5399 | } |
| 5400 | } |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5401 | spin_lock_irqsave(&conf->device_lock, flags); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 5402 | mddev->degraded = calc_degraded(conf); |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5403 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5404 | print_raid5_conf(conf); |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5405 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5406 | } |
| 5407 | |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 5408 | static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5409 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5410 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5411 | int err = 0; |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 5412 | int number = rdev->raid_disk; |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5413 | struct md_rdev **rdevp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5414 | struct disk_info *p = conf->disks + number; |
| 5415 | |
| 5416 | print_raid5_conf(conf); |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5417 | if (rdev == p->rdev) |
| 5418 | rdevp = &p->rdev; |
| 5419 | else if (rdev == p->replacement) |
| 5420 | rdevp = &p->replacement; |
| 5421 | else |
| 5422 | return 0; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5423 | |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5424 | if (number >= conf->raid_disks && |
| 5425 | conf->reshape_progress == MaxSector) |
| 5426 | clear_bit(In_sync, &rdev->flags); |
| 5427 | |
| 5428 | if (test_bit(In_sync, &rdev->flags) || |
| 5429 | atomic_read(&rdev->nr_pending)) { |
| 5430 | err = -EBUSY; |
| 5431 | goto abort; |
| 5432 | } |
| 5433 | /* Only remove non-faulty devices if recovery |
| 5434 | * isn't possible. |
| 5435 | */ |
| 5436 | if (!test_bit(Faulty, &rdev->flags) && |
| 5437 | mddev->recovery_disabled != conf->recovery_disabled && |
| 5438 | !has_failed(conf) && |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5439 | (!p->replacement || p->replacement == rdev) && |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5440 | number < conf->raid_disks) { |
| 5441 | err = -EBUSY; |
| 5442 | goto abort; |
| 5443 | } |
| 5444 | *rdevp = NULL; |
| 5445 | synchronize_rcu(); |
| 5446 | if (atomic_read(&rdev->nr_pending)) { |
| 5447 | /* lost the race, try later */ |
| 5448 | err = -EBUSY; |
| 5449 | *rdevp = rdev; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5450 | } else if (p->replacement) { |
| 5451 | /* We must have just cleared 'rdev' */ |
| 5452 | p->rdev = p->replacement; |
| 5453 | clear_bit(Replacement, &p->replacement->flags); |
| 5454 | smp_mb(); /* Make sure other CPUs may see both as identical |
| 5455 | * but will never see neither - if they are careful |
| 5456 | */ |
| 5457 | p->replacement = NULL; |
| 5458 | clear_bit(WantReplacement, &rdev->flags); |
| 5459 | } else |
| 5460 | /* We might have just removed the Replacement as faulty- |
| 5461 | * clear the bit just in case |
| 5462 | */ |
| 5463 | clear_bit(WantReplacement, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5464 | abort: |
| 5465 | |
| 5466 | print_raid5_conf(conf); |
| 5467 | return err; |
| 5468 | } |
| 5469 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5470 | static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5471 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5472 | struct r5conf *conf = mddev->private; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5473 | int err = -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5474 | int disk; |
| 5475 | struct disk_info *p; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5476 | int first = 0; |
| 5477 | int last = conf->raid_disks - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5478 | |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 5479 | if (mddev->recovery_disabled == conf->recovery_disabled) |
| 5480 | return -EBUSY; |
| 5481 | |
NeilBrown | dc10c64 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 5482 | if (rdev->saved_raid_disk < 0 && has_failed(conf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5483 | /* no point adding a device */ |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5484 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5485 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5486 | if (rdev->raid_disk >= 0) |
| 5487 | first = last = rdev->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5488 | |
| 5489 | /* |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5490 | * find the disk ... but prefer rdev->saved_raid_disk |
| 5491 | * if possible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5492 | */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5493 | if (rdev->saved_raid_disk >= 0 && |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5494 | rdev->saved_raid_disk >= first && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5495 | conf->disks[rdev->saved_raid_disk].rdev == NULL) |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 5496 | first = rdev->saved_raid_disk; |
| 5497 | |
| 5498 | for (disk = first; disk <= last; disk++) { |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5499 | p = conf->disks + disk; |
| 5500 | if (p->rdev == NULL) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5501 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5502 | rdev->raid_disk = disk; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5503 | err = 0; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5504 | if (rdev->saved_raid_disk != disk) |
| 5505 | conf->fullsync = 1; |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 5506 | rcu_assign_pointer(p->rdev, rdev); |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 5507 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5508 | } |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 5509 | } |
| 5510 | for (disk = first; disk <= last; disk++) { |
| 5511 | p = conf->disks + disk; |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5512 | if (test_bit(WantReplacement, &p->rdev->flags) && |
| 5513 | p->replacement == NULL) { |
| 5514 | clear_bit(In_sync, &rdev->flags); |
| 5515 | set_bit(Replacement, &rdev->flags); |
| 5516 | rdev->raid_disk = disk; |
| 5517 | err = 0; |
| 5518 | conf->fullsync = 1; |
| 5519 | rcu_assign_pointer(p->replacement, rdev); |
| 5520 | break; |
| 5521 | } |
| 5522 | } |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 5523 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5524 | print_raid5_conf(conf); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5525 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5526 | } |
| 5527 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5528 | static int raid5_resize(struct mddev *mddev, sector_t sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5529 | { |
| 5530 | /* no resync is happening, and there is enough space |
| 5531 | * on all devices, so we can resize. |
| 5532 | * We need to make sure resync covers any new space. |
| 5533 | * If the array is shrinking we should possibly wait until |
| 5534 | * any io in the removed space completes, but it hardly seems |
| 5535 | * worth it. |
| 5536 | */ |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 5537 | sector_t newsize; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5538 | sectors &= ~((sector_t)mddev->chunk_sectors - 1); |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 5539 | newsize = raid5_size(mddev, sectors, mddev->raid_disks); |
| 5540 | if (mddev->external_size && |
| 5541 | mddev->array_sectors > newsize) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5542 | return -EINVAL; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 5543 | if (mddev->bitmap) { |
| 5544 | int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0); |
| 5545 | if (ret) |
| 5546 | return ret; |
| 5547 | } |
| 5548 | md_set_array_sectors(mddev, newsize); |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 5549 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5550 | revalidate_disk(mddev->gendisk); |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 5551 | if (sectors > mddev->dev_sectors && |
| 5552 | mddev->recovery_cp > mddev->dev_sectors) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5553 | mddev->recovery_cp = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5554 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5555 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5556 | mddev->dev_sectors = sectors; |
NeilBrown | 4b5c7ae | 2005-07-27 11:43:28 -0700 | [diff] [blame] | 5557 | mddev->resync_max_sectors = sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5558 | return 0; |
| 5559 | } |
| 5560 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5561 | static int check_stripe_cache(struct mddev *mddev) |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5562 | { |
| 5563 | /* Can only proceed if there are plenty of stripe_heads. |
| 5564 | * We need a minimum of one full stripe,, and for sensible progress |
| 5565 | * it is best to have about 4 times that. |
| 5566 | * If we require 4 times, then the default 256 4K stripe_heads will |
| 5567 | * allow for chunk sizes up to 256K, which is probably OK. |
| 5568 | * If the chunk size is greater, user-space should request more |
| 5569 | * stripe_heads first. |
| 5570 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5571 | struct r5conf *conf = mddev->private; |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5572 | if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4 |
| 5573 | > conf->max_nr_stripes || |
| 5574 | ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4 |
| 5575 | > conf->max_nr_stripes) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5576 | printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n", |
| 5577 | mdname(mddev), |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5578 | ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9) |
| 5579 | / STRIPE_SIZE)*4); |
| 5580 | return 0; |
| 5581 | } |
| 5582 | return 1; |
| 5583 | } |
| 5584 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5585 | static int check_reshape(struct mddev *mddev) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5586 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5587 | struct r5conf *conf = mddev->private; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5588 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5589 | if (mddev->delta_disks == 0 && |
| 5590 | mddev->new_layout == mddev->layout && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5591 | mddev->new_chunk_sectors == mddev->chunk_sectors) |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5592 | return 0; /* nothing to do */ |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 5593 | if (has_failed(conf)) |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5594 | return -EINVAL; |
| 5595 | if (mddev->delta_disks < 0) { |
| 5596 | /* We might be able to shrink, but the devices must |
| 5597 | * be made bigger first. |
| 5598 | * For raid6, 4 is the minimum size. |
| 5599 | * Otherwise 2 is the minimum |
| 5600 | */ |
| 5601 | int min = 2; |
| 5602 | if (mddev->level == 6) |
| 5603 | min = 4; |
| 5604 | if (mddev->raid_disks + mddev->delta_disks < min) |
| 5605 | return -EINVAL; |
| 5606 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5607 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5608 | if (!check_stripe_cache(mddev)) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5609 | return -ENOSPC; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5610 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5611 | return resize_stripes(conf, conf->raid_disks + mddev->delta_disks); |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5612 | } |
| 5613 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5614 | static int raid5_start_reshape(struct mddev *mddev) |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5615 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5616 | struct r5conf *conf = mddev->private; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5617 | struct md_rdev *rdev; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5618 | int spares = 0; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 5619 | unsigned long flags; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5620 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5621 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5622 | return -EBUSY; |
| 5623 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5624 | if (!check_stripe_cache(mddev)) |
| 5625 | return -ENOSPC; |
| 5626 | |
NeilBrown | 30b6764 | 2012-05-22 13:55:28 +1000 | [diff] [blame] | 5627 | if (has_failed(conf)) |
| 5628 | return -EINVAL; |
| 5629 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5630 | rdev_for_each(rdev, mddev) { |
NeilBrown | 469518a | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5631 | if (!test_bit(In_sync, &rdev->flags) |
| 5632 | && !test_bit(Faulty, &rdev->flags)) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5633 | spares++; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5634 | } |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5635 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5636 | if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5637 | /* Not enough devices even to make a degraded array |
| 5638 | * of that size |
| 5639 | */ |
| 5640 | return -EINVAL; |
| 5641 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5642 | /* Refuse to reduce size of the array. Any reductions in |
| 5643 | * array size must be through explicit setting of array_size |
| 5644 | * attribute. |
| 5645 | */ |
| 5646 | if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks) |
| 5647 | < mddev->array_sectors) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5648 | printk(KERN_ERR "md/raid:%s: array size must be reduced " |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5649 | "before number of disks\n", mdname(mddev)); |
| 5650 | return -EINVAL; |
| 5651 | } |
| 5652 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5653 | atomic_set(&conf->reshape_stripes, 0); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5654 | spin_lock_irq(&conf->device_lock); |
| 5655 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5656 | conf->raid_disks += mddev->delta_disks; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5657 | conf->prev_chunk_sectors = conf->chunk_sectors; |
| 5658 | conf->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5659 | conf->prev_algo = conf->algorithm; |
| 5660 | conf->algorithm = mddev->new_layout; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5661 | conf->generation++; |
| 5662 | /* Code that selects data_offset needs to see the generation update |
| 5663 | * if reshape_progress has been set - so a memory barrier needed. |
| 5664 | */ |
| 5665 | smp_mb(); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5666 | if (mddev->reshape_backwards) |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5667 | conf->reshape_progress = raid5_size(mddev, 0, 0); |
| 5668 | else |
| 5669 | conf->reshape_progress = 0; |
| 5670 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5671 | spin_unlock_irq(&conf->device_lock); |
| 5672 | |
| 5673 | /* Add some new drives, as many as will fit. |
| 5674 | * We know there are enough to make the newly sized array work. |
NeilBrown | 3424bf6 | 2010-06-17 17:48:26 +1000 | [diff] [blame] | 5675 | * Don't add devices if we are reducing the number of |
| 5676 | * devices in the array. This is because it is not possible |
| 5677 | * to correctly record the "partially reconstructed" state of |
| 5678 | * such devices during the reshape and confusion could result. |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5679 | */ |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5680 | if (mddev->delta_disks >= 0) { |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5681 | rdev_for_each(rdev, mddev) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5682 | if (rdev->raid_disk < 0 && |
| 5683 | !test_bit(Faulty, &rdev->flags)) { |
| 5684 | if (raid5_add_disk(mddev, rdev) == 0) { |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5685 | if (rdev->raid_disk |
NeilBrown | 9d4c7d8 | 2012-03-13 11:21:21 +1100 | [diff] [blame] | 5686 | >= conf->previous_raid_disks) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5687 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 9d4c7d8 | 2012-03-13 11:21:21 +1100 | [diff] [blame] | 5688 | else |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5689 | rdev->recovery_offset = 0; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5690 | |
| 5691 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5692 | /* Failure here is OK */; |
NeilBrown | 50da084 | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5693 | } |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5694 | } else if (rdev->raid_disk >= conf->previous_raid_disks |
| 5695 | && !test_bit(Faulty, &rdev->flags)) { |
| 5696 | /* This is a spare that was manually added */ |
| 5697 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5698 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5699 | |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5700 | /* When a reshape changes the number of devices, |
| 5701 | * ->degraded is measured against the larger of the |
| 5702 | * pre and post number of devices. |
| 5703 | */ |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5704 | spin_lock_irqsave(&conf->device_lock, flags); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 5705 | mddev->degraded = calc_degraded(conf); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5706 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 5707 | } |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5708 | mddev->raid_disks = conf->raid_disks; |
NeilBrown | e516402 | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 5709 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5710 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5711 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5712 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 5713 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 5714 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 5715 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 5716 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 5717 | "reshape"); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5718 | if (!mddev->sync_thread) { |
| 5719 | mddev->recovery = 0; |
| 5720 | spin_lock_irq(&conf->device_lock); |
| 5721 | mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5722 | rdev_for_each(rdev, mddev) |
| 5723 | rdev->new_data_offset = rdev->data_offset; |
| 5724 | smp_wmb(); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5725 | conf->reshape_progress = MaxSector; |
NeilBrown | 1e3fa9b | 2012-03-13 11:21:18 +1100 | [diff] [blame] | 5726 | mddev->reshape_position = MaxSector; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5727 | spin_unlock_irq(&conf->device_lock); |
| 5728 | return -EAGAIN; |
| 5729 | } |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5730 | conf->reshape_checkpoint = jiffies; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5731 | md_wakeup_thread(mddev->sync_thread); |
| 5732 | md_new_event(mddev); |
| 5733 | return 0; |
| 5734 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5735 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5736 | /* This is called from the reshape thread and should make any |
| 5737 | * changes needed in 'conf' |
| 5738 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5739 | static void end_reshape(struct r5conf *conf) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5740 | { |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5741 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5742 | if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) { |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5743 | struct md_rdev *rdev; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 5744 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5745 | spin_lock_irq(&conf->device_lock); |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5746 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5747 | rdev_for_each(rdev, conf->mddev) |
| 5748 | rdev->data_offset = rdev->new_data_offset; |
| 5749 | smp_wmb(); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5750 | conf->reshape_progress = MaxSector; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5751 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 5752 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5753 | |
| 5754 | /* read-ahead size must cover two whole stripes, which is |
| 5755 | * 2 * (datadisks) * chunksize where 'n' is the number of raid devices |
| 5756 | */ |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 5757 | if (conf->mddev->queue) { |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5758 | int data_disks = conf->raid_disks - conf->max_degraded; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5759 | int stripe = data_disks * ((conf->chunk_sectors << 9) |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5760 | / PAGE_SIZE); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5761 | if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 5762 | conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
| 5763 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5764 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5765 | } |
| 5766 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5767 | /* This is called from the raid5d thread with mddev_lock held. |
| 5768 | * It makes config changes to the device. |
| 5769 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5770 | static void raid5_finish_reshape(struct mddev *mddev) |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5771 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5772 | struct r5conf *conf = mddev->private; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5773 | |
| 5774 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 5775 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5776 | if (mddev->delta_disks > 0) { |
| 5777 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
| 5778 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5779 | revalidate_disk(mddev->gendisk); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5780 | } else { |
| 5781 | int d; |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 5782 | spin_lock_irq(&conf->device_lock); |
| 5783 | mddev->degraded = calc_degraded(conf); |
| 5784 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5785 | for (d = conf->raid_disks ; |
| 5786 | d < conf->raid_disks - mddev->delta_disks; |
NeilBrown | 1a67dde | 2009-08-13 10:41:49 +1000 | [diff] [blame] | 5787 | d++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5788 | struct md_rdev *rdev = conf->disks[d].rdev; |
NeilBrown | da7613b | 2012-05-22 13:55:33 +1000 | [diff] [blame] | 5789 | if (rdev) |
| 5790 | clear_bit(In_sync, &rdev->flags); |
| 5791 | rdev = conf->disks[d].replacement; |
| 5792 | if (rdev) |
| 5793 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 1a67dde | 2009-08-13 10:41:49 +1000 | [diff] [blame] | 5794 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5795 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5796 | mddev->layout = conf->algorithm; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5797 | mddev->chunk_sectors = conf->chunk_sectors; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5798 | mddev->reshape_position = MaxSector; |
| 5799 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5800 | mddev->reshape_backwards = 0; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5801 | } |
| 5802 | } |
| 5803 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5804 | static void raid5_quiesce(struct mddev *mddev, int state) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5805 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5806 | struct r5conf *conf = mddev->private; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5807 | |
| 5808 | switch(state) { |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5809 | case 2: /* resume for a suspend */ |
| 5810 | wake_up(&conf->wait_for_overlap); |
| 5811 | break; |
| 5812 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5813 | case 1: /* stop all writes */ |
| 5814 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5815 | /* '2' tells resync/reshape to pause so that all |
| 5816 | * active stripes can drain |
| 5817 | */ |
| 5818 | conf->quiesce = 2; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5819 | wait_event_lock_irq(conf->wait_for_stripe, |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5820 | atomic_read(&conf->active_stripes) == 0 && |
| 5821 | atomic_read(&conf->active_aligned_reads) == 0, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5822 | conf->device_lock, /* nothing */); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5823 | conf->quiesce = 1; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5824 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5825 | /* allow reshape to continue */ |
| 5826 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5827 | break; |
| 5828 | |
| 5829 | case 0: /* re-enable writes */ |
| 5830 | spin_lock_irq(&conf->device_lock); |
| 5831 | conf->quiesce = 0; |
| 5832 | wake_up(&conf->wait_for_stripe); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5833 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5834 | spin_unlock_irq(&conf->device_lock); |
| 5835 | break; |
| 5836 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5837 | } |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5838 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5839 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5840 | static void *raid45_takeover_raid0(struct mddev *mddev, int level) |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 5841 | { |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 5842 | struct r0conf *raid0_conf = mddev->private; |
Randy Dunlap | d76c842 | 2011-04-21 09:07:26 -0700 | [diff] [blame] | 5843 | sector_t sectors; |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 5844 | |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5845 | /* for raid0 takeover only one zone is supported */ |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 5846 | if (raid0_conf->nr_strip_zones > 1) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5847 | printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n", |
| 5848 | mdname(mddev)); |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5849 | return ERR_PTR(-EINVAL); |
| 5850 | } |
| 5851 | |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 5852 | sectors = raid0_conf->strip_zone[0].zone_end; |
| 5853 | sector_div(sectors, raid0_conf->strip_zone[0].nb_dev); |
NeilBrown | 3b71bd9 | 2011-04-20 15:38:18 +1000 | [diff] [blame] | 5854 | mddev->dev_sectors = sectors; |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5855 | mddev->new_level = level; |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 5856 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 5857 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
| 5858 | mddev->raid_disks += 1; |
| 5859 | mddev->delta_disks = 1; |
| 5860 | /* make sure it will be not marked as dirty */ |
| 5861 | mddev->recovery_cp = MaxSector; |
| 5862 | |
| 5863 | return setup_conf(mddev); |
| 5864 | } |
| 5865 | |
| 5866 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5867 | static void *raid5_takeover_raid1(struct mddev *mddev) |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5868 | { |
| 5869 | int chunksect; |
| 5870 | |
| 5871 | if (mddev->raid_disks != 2 || |
| 5872 | mddev->degraded > 1) |
| 5873 | return ERR_PTR(-EINVAL); |
| 5874 | |
| 5875 | /* Should check if there are write-behind devices? */ |
| 5876 | |
| 5877 | chunksect = 64*2; /* 64K by default */ |
| 5878 | |
| 5879 | /* The array must be an exact multiple of chunksize */ |
| 5880 | while (chunksect && (mddev->array_sectors & (chunksect-1))) |
| 5881 | chunksect >>= 1; |
| 5882 | |
| 5883 | if ((chunksect<<9) < STRIPE_SIZE) |
| 5884 | /* array size does not allow a suitable chunk size */ |
| 5885 | return ERR_PTR(-EINVAL); |
| 5886 | |
| 5887 | mddev->new_level = 5; |
| 5888 | mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5889 | mddev->new_chunk_sectors = chunksect; |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5890 | |
| 5891 | return setup_conf(mddev); |
| 5892 | } |
| 5893 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5894 | static void *raid5_takeover_raid6(struct mddev *mddev) |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 5895 | { |
| 5896 | int new_layout; |
| 5897 | |
| 5898 | switch (mddev->layout) { |
| 5899 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 5900 | new_layout = ALGORITHM_LEFT_ASYMMETRIC; |
| 5901 | break; |
| 5902 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 5903 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC; |
| 5904 | break; |
| 5905 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 5906 | new_layout = ALGORITHM_LEFT_SYMMETRIC; |
| 5907 | break; |
| 5908 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 5909 | new_layout = ALGORITHM_RIGHT_SYMMETRIC; |
| 5910 | break; |
| 5911 | case ALGORITHM_PARITY_0_6: |
| 5912 | new_layout = ALGORITHM_PARITY_0; |
| 5913 | break; |
| 5914 | case ALGORITHM_PARITY_N: |
| 5915 | new_layout = ALGORITHM_PARITY_N; |
| 5916 | break; |
| 5917 | default: |
| 5918 | return ERR_PTR(-EINVAL); |
| 5919 | } |
| 5920 | mddev->new_level = 5; |
| 5921 | mddev->new_layout = new_layout; |
| 5922 | mddev->delta_disks = -1; |
| 5923 | mddev->raid_disks -= 1; |
| 5924 | return setup_conf(mddev); |
| 5925 | } |
| 5926 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5927 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5928 | static int raid5_check_reshape(struct mddev *mddev) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5929 | { |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5930 | /* For a 2-drive array, the layout and chunk size can be changed |
| 5931 | * immediately as not restriping is needed. |
| 5932 | * For larger arrays we record the new value - after validation |
| 5933 | * to be used by a reshape pass. |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5934 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5935 | struct r5conf *conf = mddev->private; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5936 | int new_chunk = mddev->new_chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5937 | |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5938 | if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5939 | return -EINVAL; |
| 5940 | if (new_chunk > 0) { |
Andre Noll | 0ba459d | 2009-06-18 08:46:10 +1000 | [diff] [blame] | 5941 | if (!is_power_of_2(new_chunk)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5942 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5943 | if (new_chunk < (PAGE_SIZE>>9)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5944 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5945 | if (mddev->array_sectors & (new_chunk-1)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5946 | /* not factor of array size */ |
| 5947 | return -EINVAL; |
| 5948 | } |
| 5949 | |
| 5950 | /* They look valid */ |
| 5951 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5952 | if (mddev->raid_disks == 2) { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5953 | /* can make the change immediately */ |
| 5954 | if (mddev->new_layout >= 0) { |
| 5955 | conf->algorithm = mddev->new_layout; |
| 5956 | mddev->layout = mddev->new_layout; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5957 | } |
| 5958 | if (new_chunk > 0) { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5959 | conf->chunk_sectors = new_chunk ; |
| 5960 | mddev->chunk_sectors = new_chunk; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5961 | } |
| 5962 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 5963 | md_wakeup_thread(mddev->thread); |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5964 | } |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5965 | return check_reshape(mddev); |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5966 | } |
| 5967 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5968 | static int raid6_check_reshape(struct mddev *mddev) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5969 | { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5970 | int new_chunk = mddev->new_chunk_sectors; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5971 | |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5972 | if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5973 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5974 | if (new_chunk > 0) { |
Andre Noll | 0ba459d | 2009-06-18 08:46:10 +1000 | [diff] [blame] | 5975 | if (!is_power_of_2(new_chunk)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5976 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5977 | if (new_chunk < (PAGE_SIZE >> 9)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5978 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5979 | if (mddev->array_sectors & (new_chunk-1)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5980 | /* not factor of array size */ |
| 5981 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5982 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5983 | |
| 5984 | /* They look valid */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5985 | return check_reshape(mddev); |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5986 | } |
| 5987 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5988 | static void *raid5_takeover(struct mddev *mddev) |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5989 | { |
| 5990 | /* raid5 can take over: |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5991 | * raid0 - if there is only one strip zone - make it a raid4 layout |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5992 | * raid1 - if there are two drives. We need to know the chunk size |
| 5993 | * raid4 - trivial - just use a raid4 layout. |
| 5994 | * raid6 - Providing it is a *_6 layout |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5995 | */ |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5996 | if (mddev->level == 0) |
| 5997 | return raid45_takeover_raid0(mddev, 5); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5998 | if (mddev->level == 1) |
| 5999 | return raid5_takeover_raid1(mddev); |
NeilBrown | e9d4758 | 2009-03-31 14:57:09 +1100 | [diff] [blame] | 6000 | if (mddev->level == 4) { |
| 6001 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 6002 | mddev->new_level = 5; |
| 6003 | return setup_conf(mddev); |
| 6004 | } |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 6005 | if (mddev->level == 6) |
| 6006 | return raid5_takeover_raid6(mddev); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6007 | |
| 6008 | return ERR_PTR(-EINVAL); |
| 6009 | } |
| 6010 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6011 | static void *raid4_takeover(struct mddev *mddev) |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 6012 | { |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6013 | /* raid4 can take over: |
| 6014 | * raid0 - if there is only one strip zone |
| 6015 | * raid5 - if layout is right |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 6016 | */ |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6017 | if (mddev->level == 0) |
| 6018 | return raid45_takeover_raid0(mddev, 4); |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 6019 | if (mddev->level == 5 && |
| 6020 | mddev->layout == ALGORITHM_PARITY_N) { |
| 6021 | mddev->new_layout = 0; |
| 6022 | mddev->new_level = 4; |
| 6023 | return setup_conf(mddev); |
| 6024 | } |
| 6025 | return ERR_PTR(-EINVAL); |
| 6026 | } |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6027 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6028 | static struct md_personality raid5_personality; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6029 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6030 | static void *raid6_takeover(struct mddev *mddev) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6031 | { |
| 6032 | /* Currently can only take over a raid5. We map the |
| 6033 | * personality to an equivalent raid6 personality |
| 6034 | * with the Q block at the end. |
| 6035 | */ |
| 6036 | int new_layout; |
| 6037 | |
| 6038 | if (mddev->pers != &raid5_personality) |
| 6039 | return ERR_PTR(-EINVAL); |
| 6040 | if (mddev->degraded > 1) |
| 6041 | return ERR_PTR(-EINVAL); |
| 6042 | if (mddev->raid_disks > 253) |
| 6043 | return ERR_PTR(-EINVAL); |
| 6044 | if (mddev->raid_disks < 3) |
| 6045 | return ERR_PTR(-EINVAL); |
| 6046 | |
| 6047 | switch (mddev->layout) { |
| 6048 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 6049 | new_layout = ALGORITHM_LEFT_ASYMMETRIC_6; |
| 6050 | break; |
| 6051 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 6052 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6; |
| 6053 | break; |
| 6054 | case ALGORITHM_LEFT_SYMMETRIC: |
| 6055 | new_layout = ALGORITHM_LEFT_SYMMETRIC_6; |
| 6056 | break; |
| 6057 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 6058 | new_layout = ALGORITHM_RIGHT_SYMMETRIC_6; |
| 6059 | break; |
| 6060 | case ALGORITHM_PARITY_0: |
| 6061 | new_layout = ALGORITHM_PARITY_0_6; |
| 6062 | break; |
| 6063 | case ALGORITHM_PARITY_N: |
| 6064 | new_layout = ALGORITHM_PARITY_N; |
| 6065 | break; |
| 6066 | default: |
| 6067 | return ERR_PTR(-EINVAL); |
| 6068 | } |
| 6069 | mddev->new_level = 6; |
| 6070 | mddev->new_layout = new_layout; |
| 6071 | mddev->delta_disks = 1; |
| 6072 | mddev->raid_disks += 1; |
| 6073 | return setup_conf(mddev); |
| 6074 | } |
| 6075 | |
| 6076 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6077 | static struct md_personality raid6_personality = |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6078 | { |
| 6079 | .name = "raid6", |
| 6080 | .level = 6, |
| 6081 | .owner = THIS_MODULE, |
| 6082 | .make_request = make_request, |
| 6083 | .run = run, |
| 6084 | .stop = stop, |
| 6085 | .status = status, |
| 6086 | .error_handler = error, |
| 6087 | .hot_add_disk = raid5_add_disk, |
| 6088 | .hot_remove_disk= raid5_remove_disk, |
| 6089 | .spare_active = raid5_spare_active, |
| 6090 | .sync_request = sync_request, |
| 6091 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6092 | .size = raid5_size, |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6093 | .check_reshape = raid6_check_reshape, |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 6094 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6095 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6096 | .quiesce = raid5_quiesce, |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6097 | .takeover = raid6_takeover, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6098 | }; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6099 | static struct md_personality raid5_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6100 | { |
| 6101 | .name = "raid5", |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6102 | .level = 5, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6103 | .owner = THIS_MODULE, |
| 6104 | .make_request = make_request, |
| 6105 | .run = run, |
| 6106 | .stop = stop, |
| 6107 | .status = status, |
| 6108 | .error_handler = error, |
| 6109 | .hot_add_disk = raid5_add_disk, |
| 6110 | .hot_remove_disk= raid5_remove_disk, |
| 6111 | .spare_active = raid5_spare_active, |
| 6112 | .sync_request = sync_request, |
| 6113 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6114 | .size = raid5_size, |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6115 | .check_reshape = raid5_check_reshape, |
| 6116 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6117 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6118 | .quiesce = raid5_quiesce, |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6119 | .takeover = raid5_takeover, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6120 | }; |
| 6121 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6122 | static struct md_personality raid4_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6123 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6124 | .name = "raid4", |
| 6125 | .level = 4, |
| 6126 | .owner = THIS_MODULE, |
| 6127 | .make_request = make_request, |
| 6128 | .run = run, |
| 6129 | .stop = stop, |
| 6130 | .status = status, |
| 6131 | .error_handler = error, |
| 6132 | .hot_add_disk = raid5_add_disk, |
| 6133 | .hot_remove_disk= raid5_remove_disk, |
| 6134 | .spare_active = raid5_spare_active, |
| 6135 | .sync_request = sync_request, |
| 6136 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6137 | .size = raid5_size, |
NeilBrown | 3d37890 | 2007-03-26 21:32:13 -0800 | [diff] [blame] | 6138 | .check_reshape = raid5_check_reshape, |
| 6139 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6140 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6141 | .quiesce = raid5_quiesce, |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 6142 | .takeover = raid4_takeover, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6143 | }; |
| 6144 | |
| 6145 | static int __init raid5_init(void) |
| 6146 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6147 | register_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6148 | register_md_personality(&raid5_personality); |
| 6149 | register_md_personality(&raid4_personality); |
| 6150 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6151 | } |
| 6152 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6153 | static void raid5_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6154 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6155 | unregister_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6156 | unregister_md_personality(&raid5_personality); |
| 6157 | unregister_md_personality(&raid4_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6158 | } |
| 6159 | |
| 6160 | module_init(raid5_init); |
| 6161 | module_exit(raid5_exit); |
| 6162 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 6163 | MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6164 | MODULE_ALIAS("md-personality-4"); /* RAID5 */ |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 6165 | MODULE_ALIAS("md-raid5"); |
| 6166 | MODULE_ALIAS("md-raid4"); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6167 | MODULE_ALIAS("md-level-5"); |
| 6168 | MODULE_ALIAS("md-level-4"); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6169 | MODULE_ALIAS("md-personality-8"); /* RAID6 */ |
| 6170 | MODULE_ALIAS("md-raid6"); |
| 6171 | MODULE_ALIAS("md-level-6"); |
| 6172 | |
| 6173 | /* This used to be two separate modules, they were: */ |
| 6174 | MODULE_ALIAS("raid5"); |
| 6175 | MODULE_ALIAS("raid6"); |