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) |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 474 | && !test_bit(STRIPE_EXPANDING, &sh->state) |
| 475 | && !test_bit(STRIPE_ON_UNPLUG_LIST, &sh->state)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | } else { |
| 477 | if (!test_bit(STRIPE_HANDLE, &sh->state)) |
| 478 | atomic_inc(&conf->active_stripes); |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 479 | if (list_empty(&sh->lru) && |
| 480 | !test_bit(STRIPE_EXPANDING, &sh->state)) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 481 | BUG(); |
| 482 | list_del_init(&sh->lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | } while (sh == NULL); |
| 486 | |
| 487 | if (sh) |
| 488 | atomic_inc(&sh->count); |
| 489 | |
| 490 | spin_unlock_irq(&conf->device_lock); |
| 491 | return sh; |
| 492 | } |
| 493 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 494 | /* Determine if 'data_offset' or 'new_data_offset' should be used |
| 495 | * in this stripe_head. |
| 496 | */ |
| 497 | static int use_new_offset(struct r5conf *conf, struct stripe_head *sh) |
| 498 | { |
| 499 | sector_t progress = conf->reshape_progress; |
| 500 | /* Need a memory barrier to make sure we see the value |
| 501 | * of conf->generation, or ->data_offset that was set before |
| 502 | * reshape_progress was updated. |
| 503 | */ |
| 504 | smp_rmb(); |
| 505 | if (progress == MaxSector) |
| 506 | return 0; |
| 507 | if (sh->generation == conf->generation - 1) |
| 508 | return 0; |
| 509 | /* We are in a reshape, and this is a new-generation stripe, |
| 510 | * so use new_data_offset. |
| 511 | */ |
| 512 | return 1; |
| 513 | } |
| 514 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 515 | static void |
| 516 | raid5_end_read_request(struct bio *bi, int error); |
| 517 | static void |
| 518 | raid5_end_write_request(struct bio *bi, int error); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 519 | |
Dan Williams | c4e5ac0 | 2008-06-28 08:31:53 +1000 | [diff] [blame] | 520 | 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] | 521 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 522 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 523 | int i, disks = sh->disks; |
| 524 | |
| 525 | might_sleep(); |
| 526 | |
| 527 | for (i = disks; i--; ) { |
| 528 | int rw; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 529 | int replace_only = 0; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 530 | struct bio *bi, *rbi; |
| 531 | struct md_rdev *rdev, *rrdev = NULL; |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 532 | if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) { |
| 533 | if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags)) |
| 534 | rw = WRITE_FUA; |
| 535 | else |
| 536 | rw = WRITE; |
| 537 | } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 538 | rw = READ; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 539 | else if (test_and_clear_bit(R5_WantReplace, |
| 540 | &sh->dev[i].flags)) { |
| 541 | rw = WRITE; |
| 542 | replace_only = 1; |
| 543 | } else |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 544 | continue; |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 545 | if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags)) |
| 546 | rw |= REQ_SYNC; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 547 | |
| 548 | bi = &sh->dev[i].req; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 549 | rbi = &sh->dev[i].rreq; /* For writing to replacement */ |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 550 | |
| 551 | bi->bi_rw = rw; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 552 | rbi->bi_rw = rw; |
| 553 | if (rw & WRITE) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 554 | bi->bi_end_io = raid5_end_write_request; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 555 | rbi->bi_end_io = raid5_end_write_request; |
| 556 | } else |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 557 | bi->bi_end_io = raid5_end_read_request; |
| 558 | |
| 559 | rcu_read_lock(); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 560 | rrdev = rcu_dereference(conf->disks[i].replacement); |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 561 | smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */ |
| 562 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 563 | if (!rdev) { |
| 564 | rdev = rrdev; |
| 565 | rrdev = NULL; |
| 566 | } |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 567 | if (rw & WRITE) { |
| 568 | if (replace_only) |
| 569 | rdev = NULL; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 570 | if (rdev == rrdev) |
| 571 | /* We raced and saw duplicates */ |
| 572 | rrdev = NULL; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 573 | } else { |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 574 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 575 | rdev = rrdev; |
| 576 | rrdev = NULL; |
| 577 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 578 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 579 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 580 | rdev = NULL; |
| 581 | if (rdev) |
| 582 | atomic_inc(&rdev->nr_pending); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 583 | if (rrdev && test_bit(Faulty, &rrdev->flags)) |
| 584 | rrdev = NULL; |
| 585 | if (rrdev) |
| 586 | atomic_inc(&rrdev->nr_pending); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 587 | rcu_read_unlock(); |
| 588 | |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 589 | /* We have already checked bad blocks for reads. Now |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 590 | * need to check for writes. We never accept write errors |
| 591 | * on the replacement, so we don't to check rrdev. |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 592 | */ |
| 593 | while ((rw & WRITE) && rdev && |
| 594 | test_bit(WriteErrorSeen, &rdev->flags)) { |
| 595 | sector_t first_bad; |
| 596 | int bad_sectors; |
| 597 | int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 598 | &first_bad, &bad_sectors); |
| 599 | if (!bad) |
| 600 | break; |
| 601 | |
| 602 | if (bad < 0) { |
| 603 | set_bit(BlockedBadBlocks, &rdev->flags); |
| 604 | if (!conf->mddev->external && |
| 605 | conf->mddev->flags) { |
| 606 | /* It is very unlikely, but we might |
| 607 | * still need to write out the |
| 608 | * bad block log - better give it |
| 609 | * a chance*/ |
| 610 | md_check_recovery(conf->mddev); |
| 611 | } |
majianpeng | 1850753 | 2012-07-03 12:11:54 +1000 | [diff] [blame] | 612 | /* |
| 613 | * Because md_wait_for_blocked_rdev |
| 614 | * will dec nr_pending, we must |
| 615 | * increment it first. |
| 616 | */ |
| 617 | atomic_inc(&rdev->nr_pending); |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 618 | md_wait_for_blocked_rdev(rdev, conf->mddev); |
| 619 | } else { |
| 620 | /* Acknowledged bad block - skip the write */ |
| 621 | rdev_dec_pending(rdev, conf->mddev); |
| 622 | rdev = NULL; |
| 623 | } |
| 624 | } |
| 625 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 626 | if (rdev) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 627 | if (s->syncing || s->expanding || s->expanded |
| 628 | || s->replacing) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 629 | md_sync_acct(rdev->bdev, STRIPE_SECTORS); |
| 630 | |
Dan Williams | 2b7497f | 2008-06-28 08:31:52 +1000 | [diff] [blame] | 631 | set_bit(STRIPE_IO_STARTED, &sh->state); |
| 632 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 633 | bi->bi_bdev = rdev->bdev; |
| 634 | pr_debug("%s: for %llu schedule op %ld on disc %d\n", |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 635 | __func__, (unsigned long long)sh->sector, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 636 | bi->bi_rw, i); |
| 637 | atomic_inc(&sh->count); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 638 | if (use_new_offset(conf, sh)) |
| 639 | bi->bi_sector = (sh->sector |
| 640 | + rdev->new_data_offset); |
| 641 | else |
| 642 | bi->bi_sector = (sh->sector |
| 643 | + rdev->data_offset); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 644 | bi->bi_flags = 1 << BIO_UPTODATE; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 645 | bi->bi_idx = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 646 | bi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
| 647 | bi->bi_io_vec[0].bv_offset = 0; |
| 648 | bi->bi_size = STRIPE_SIZE; |
| 649 | bi->bi_next = NULL; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 650 | if (rrdev) |
| 651 | set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 652 | generic_make_request(bi); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 653 | } |
| 654 | if (rrdev) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 655 | if (s->syncing || s->expanding || s->expanded |
| 656 | || s->replacing) |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 657 | md_sync_acct(rrdev->bdev, STRIPE_SECTORS); |
| 658 | |
| 659 | set_bit(STRIPE_IO_STARTED, &sh->state); |
| 660 | |
| 661 | rbi->bi_bdev = rrdev->bdev; |
| 662 | pr_debug("%s: for %llu schedule op %ld on " |
| 663 | "replacement disc %d\n", |
| 664 | __func__, (unsigned long long)sh->sector, |
| 665 | rbi->bi_rw, i); |
| 666 | atomic_inc(&sh->count); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 667 | if (use_new_offset(conf, sh)) |
| 668 | rbi->bi_sector = (sh->sector |
| 669 | + rrdev->new_data_offset); |
| 670 | else |
| 671 | rbi->bi_sector = (sh->sector |
| 672 | + rrdev->data_offset); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 673 | rbi->bi_flags = 1 << BIO_UPTODATE; |
| 674 | rbi->bi_idx = 0; |
| 675 | rbi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
| 676 | rbi->bi_io_vec[0].bv_offset = 0; |
| 677 | rbi->bi_size = STRIPE_SIZE; |
| 678 | rbi->bi_next = NULL; |
| 679 | generic_make_request(rbi); |
| 680 | } |
| 681 | if (!rdev && !rrdev) { |
Namhyung Kim | b062962 | 2011-06-14 14:20:19 +1000 | [diff] [blame] | 682 | if (rw & WRITE) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 683 | set_bit(STRIPE_DEGRADED, &sh->state); |
| 684 | pr_debug("skip op %ld on disc %d for sector %llu\n", |
| 685 | bi->bi_rw, i, (unsigned long long)sh->sector); |
| 686 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 687 | set_bit(STRIPE_HANDLE, &sh->state); |
| 688 | } |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | static struct dma_async_tx_descriptor * |
| 693 | async_copy_data(int frombio, struct bio *bio, struct page *page, |
| 694 | sector_t sector, struct dma_async_tx_descriptor *tx) |
| 695 | { |
| 696 | struct bio_vec *bvl; |
| 697 | struct page *bio_page; |
| 698 | int i; |
| 699 | int page_offset; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 700 | struct async_submit_ctl submit; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 701 | enum async_tx_flags flags = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 702 | |
| 703 | if (bio->bi_sector >= sector) |
| 704 | page_offset = (signed)(bio->bi_sector - sector) * 512; |
| 705 | else |
| 706 | page_offset = (signed)(sector - bio->bi_sector) * -512; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 707 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 708 | if (frombio) |
| 709 | flags |= ASYNC_TX_FENCE; |
| 710 | init_async_submit(&submit, flags, tx, NULL, NULL, NULL); |
| 711 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 712 | bio_for_each_segment(bvl, bio, i) { |
Namhyung Kim | fcde907 | 2011-06-14 14:23:57 +1000 | [diff] [blame] | 713 | int len = bvl->bv_len; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 714 | int clen; |
| 715 | int b_offset = 0; |
| 716 | |
| 717 | if (page_offset < 0) { |
| 718 | b_offset = -page_offset; |
| 719 | page_offset += b_offset; |
| 720 | len -= b_offset; |
| 721 | } |
| 722 | |
| 723 | if (len > 0 && page_offset + len > STRIPE_SIZE) |
| 724 | clen = STRIPE_SIZE - page_offset; |
| 725 | else |
| 726 | clen = len; |
| 727 | |
| 728 | if (clen > 0) { |
Namhyung Kim | fcde907 | 2011-06-14 14:23:57 +1000 | [diff] [blame] | 729 | b_offset += bvl->bv_offset; |
| 730 | bio_page = bvl->bv_page; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 731 | if (frombio) |
| 732 | tx = async_memcpy(page, bio_page, page_offset, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 733 | b_offset, clen, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 734 | else |
| 735 | tx = async_memcpy(bio_page, page, b_offset, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 736 | page_offset, clen, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 737 | } |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 738 | /* chain the operations */ |
| 739 | submit.depend_tx = tx; |
| 740 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 741 | if (clen < len) /* hit end of page */ |
| 742 | break; |
| 743 | page_offset += len; |
| 744 | } |
| 745 | |
| 746 | return tx; |
| 747 | } |
| 748 | |
| 749 | static void ops_complete_biofill(void *stripe_head_ref) |
| 750 | { |
| 751 | struct stripe_head *sh = stripe_head_ref; |
| 752 | struct bio *return_bi = NULL; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 753 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 754 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 755 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 756 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 757 | (unsigned long long)sh->sector); |
| 758 | |
| 759 | /* clear completed biofills */ |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 760 | spin_lock_irq(&conf->device_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 761 | for (i = sh->disks; i--; ) { |
| 762 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 763 | |
| 764 | /* acknowledge completion of a biofill operation */ |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 765 | /* and check if we need to reply to a read request, |
| 766 | * new R5_Wantfill requests are held off until |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 767 | * !STRIPE_BIOFILL_RUN |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 768 | */ |
| 769 | if (test_and_clear_bit(R5_Wantfill, &dev->flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 770 | struct bio *rbi, *rbi2; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 771 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 772 | BUG_ON(!dev->read); |
| 773 | rbi = dev->read; |
| 774 | dev->read = NULL; |
| 775 | while (rbi && rbi->bi_sector < |
| 776 | dev->sector + STRIPE_SECTORS) { |
| 777 | rbi2 = r5_next_bio(rbi, dev->sector); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 778 | if (!raid5_dec_bi_phys_segments(rbi)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 779 | rbi->bi_next = return_bi; |
| 780 | return_bi = rbi; |
| 781 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 782 | rbi = rbi2; |
| 783 | } |
| 784 | } |
| 785 | } |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 786 | spin_unlock_irq(&conf->device_lock); |
| 787 | clear_bit(STRIPE_BIOFILL_RUN, &sh->state); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 788 | |
| 789 | return_io(return_bi); |
| 790 | |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 791 | set_bit(STRIPE_HANDLE, &sh->state); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 792 | release_stripe(sh); |
| 793 | } |
| 794 | |
| 795 | static void ops_run_biofill(struct stripe_head *sh) |
| 796 | { |
| 797 | struct dma_async_tx_descriptor *tx = NULL; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 798 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 799 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 800 | int i; |
| 801 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 802 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 803 | (unsigned long long)sh->sector); |
| 804 | |
| 805 | for (i = sh->disks; i--; ) { |
| 806 | struct r5dev *dev = &sh->dev[i]; |
| 807 | if (test_bit(R5_Wantfill, &dev->flags)) { |
| 808 | struct bio *rbi; |
| 809 | spin_lock_irq(&conf->device_lock); |
| 810 | dev->read = rbi = dev->toread; |
| 811 | dev->toread = NULL; |
| 812 | spin_unlock_irq(&conf->device_lock); |
| 813 | while (rbi && rbi->bi_sector < |
| 814 | dev->sector + STRIPE_SECTORS) { |
| 815 | tx = async_copy_data(0, rbi, dev->page, |
| 816 | dev->sector, tx); |
| 817 | rbi = r5_next_bio(rbi, dev->sector); |
| 818 | } |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 823 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL); |
| 824 | async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 825 | } |
| 826 | |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 827 | static void mark_target_uptodate(struct stripe_head *sh, int target) |
| 828 | { |
| 829 | struct r5dev *tgt; |
| 830 | |
| 831 | if (target < 0) |
| 832 | return; |
| 833 | |
| 834 | tgt = &sh->dev[target]; |
| 835 | set_bit(R5_UPTODATE, &tgt->flags); |
| 836 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 837 | clear_bit(R5_Wantcompute, &tgt->flags); |
| 838 | } |
| 839 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 840 | static void ops_complete_compute(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 841 | { |
| 842 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 843 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 844 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 845 | (unsigned long long)sh->sector); |
| 846 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 847 | /* mark the computed target(s) as uptodate */ |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 848 | mark_target_uptodate(sh, sh->ops.target); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 849 | mark_target_uptodate(sh, sh->ops.target2); |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 850 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 851 | clear_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 852 | if (sh->check_state == check_state_compute_run) |
| 853 | sh->check_state = check_state_compute_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 854 | set_bit(STRIPE_HANDLE, &sh->state); |
| 855 | release_stripe(sh); |
| 856 | } |
| 857 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 858 | /* return a pointer to the address conversion region of the scribble buffer */ |
| 859 | static addr_conv_t *to_addr_conv(struct stripe_head *sh, |
| 860 | struct raid5_percpu *percpu) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 861 | { |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 862 | return percpu->scribble + sizeof(struct page *) * (sh->disks + 2); |
| 863 | } |
| 864 | |
| 865 | static struct dma_async_tx_descriptor * |
| 866 | ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 867 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 868 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 869 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 870 | int target = sh->ops.target; |
| 871 | struct r5dev *tgt = &sh->dev[target]; |
| 872 | struct page *xor_dest = tgt->page; |
| 873 | int count = 0; |
| 874 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 875 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 876 | int i; |
| 877 | |
| 878 | pr_debug("%s: stripe %llu block: %d\n", |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 879 | __func__, (unsigned long long)sh->sector, target); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 880 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 881 | |
| 882 | for (i = disks; i--; ) |
| 883 | if (i != target) |
| 884 | xor_srcs[count++] = sh->dev[i].page; |
| 885 | |
| 886 | atomic_inc(&sh->count); |
| 887 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 888 | 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] | 889 | ops_complete_compute, sh, to_addr_conv(sh, percpu)); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 890 | if (unlikely(count == 1)) |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 891 | 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] | 892 | else |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 893 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 894 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 895 | return tx; |
| 896 | } |
| 897 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 898 | /* set_syndrome_sources - populate source buffers for gen_syndrome |
| 899 | * @srcs - (struct page *) array of size sh->disks |
| 900 | * @sh - stripe_head to parse |
| 901 | * |
| 902 | * Populates srcs in proper layout order for the stripe and returns the |
| 903 | * 'count' of sources to be used in a call to async_gen_syndrome. The P |
| 904 | * destination buffer is recorded in srcs[count] and the Q destination |
| 905 | * is recorded in srcs[count+1]]. |
| 906 | */ |
| 907 | static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh) |
| 908 | { |
| 909 | int disks = sh->disks; |
| 910 | int syndrome_disks = sh->ddf_layout ? disks : (disks - 2); |
| 911 | int d0_idx = raid6_d0(sh); |
| 912 | int count; |
| 913 | int i; |
| 914 | |
| 915 | for (i = 0; i < disks; i++) |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 916 | srcs[i] = NULL; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 917 | |
| 918 | count = 0; |
| 919 | i = d0_idx; |
| 920 | do { |
| 921 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 922 | |
| 923 | srcs[slot] = sh->dev[i].page; |
| 924 | i = raid6_next_disk(i, disks); |
| 925 | } while (i != d0_idx); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 926 | |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 927 | return syndrome_disks; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | static struct dma_async_tx_descriptor * |
| 931 | ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 932 | { |
| 933 | int disks = sh->disks; |
| 934 | struct page **blocks = percpu->scribble; |
| 935 | int target; |
| 936 | int qd_idx = sh->qd_idx; |
| 937 | struct dma_async_tx_descriptor *tx; |
| 938 | struct async_submit_ctl submit; |
| 939 | struct r5dev *tgt; |
| 940 | struct page *dest; |
| 941 | int i; |
| 942 | int count; |
| 943 | |
| 944 | if (sh->ops.target < 0) |
| 945 | target = sh->ops.target2; |
| 946 | else if (sh->ops.target2 < 0) |
| 947 | target = sh->ops.target; |
| 948 | else |
| 949 | /* we should only have one valid target */ |
| 950 | BUG(); |
| 951 | BUG_ON(target < 0); |
| 952 | pr_debug("%s: stripe %llu block: %d\n", |
| 953 | __func__, (unsigned long long)sh->sector, target); |
| 954 | |
| 955 | tgt = &sh->dev[target]; |
| 956 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 957 | dest = tgt->page; |
| 958 | |
| 959 | atomic_inc(&sh->count); |
| 960 | |
| 961 | if (target == qd_idx) { |
| 962 | count = set_syndrome_sources(blocks, sh); |
| 963 | blocks[count] = NULL; /* regenerating p is not necessary */ |
| 964 | BUG_ON(blocks[count+1] != dest); /* q should already be set */ |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 965 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 966 | ops_complete_compute, sh, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 967 | to_addr_conv(sh, percpu)); |
| 968 | tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
| 969 | } else { |
| 970 | /* Compute any data- or p-drive using XOR */ |
| 971 | count = 0; |
| 972 | for (i = disks; i-- ; ) { |
| 973 | if (i == target || i == qd_idx) |
| 974 | continue; |
| 975 | blocks[count++] = sh->dev[i].page; |
| 976 | } |
| 977 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 978 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
| 979 | NULL, ops_complete_compute, sh, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 980 | to_addr_conv(sh, percpu)); |
| 981 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit); |
| 982 | } |
| 983 | |
| 984 | return tx; |
| 985 | } |
| 986 | |
| 987 | static struct dma_async_tx_descriptor * |
| 988 | ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 989 | { |
| 990 | int i, count, disks = sh->disks; |
| 991 | int syndrome_disks = sh->ddf_layout ? disks : disks-2; |
| 992 | int d0_idx = raid6_d0(sh); |
| 993 | int faila = -1, failb = -1; |
| 994 | int target = sh->ops.target; |
| 995 | int target2 = sh->ops.target2; |
| 996 | struct r5dev *tgt = &sh->dev[target]; |
| 997 | struct r5dev *tgt2 = &sh->dev[target2]; |
| 998 | struct dma_async_tx_descriptor *tx; |
| 999 | struct page **blocks = percpu->scribble; |
| 1000 | struct async_submit_ctl submit; |
| 1001 | |
| 1002 | pr_debug("%s: stripe %llu block1: %d block2: %d\n", |
| 1003 | __func__, (unsigned long long)sh->sector, target, target2); |
| 1004 | BUG_ON(target < 0 || target2 < 0); |
| 1005 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 1006 | BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags)); |
| 1007 | |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1008 | /* we need to open-code set_syndrome_sources to handle the |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1009 | * slot number conversion for 'faila' and 'failb' |
| 1010 | */ |
| 1011 | for (i = 0; i < disks ; i++) |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 1012 | blocks[i] = NULL; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1013 | count = 0; |
| 1014 | i = d0_idx; |
| 1015 | do { |
| 1016 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 1017 | |
| 1018 | blocks[slot] = sh->dev[i].page; |
| 1019 | |
| 1020 | if (i == target) |
| 1021 | faila = slot; |
| 1022 | if (i == target2) |
| 1023 | failb = slot; |
| 1024 | i = raid6_next_disk(i, disks); |
| 1025 | } while (i != d0_idx); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1026 | |
| 1027 | BUG_ON(faila == failb); |
| 1028 | if (failb < faila) |
| 1029 | swap(faila, failb); |
| 1030 | pr_debug("%s: stripe: %llu faila: %d failb: %d\n", |
| 1031 | __func__, (unsigned long long)sh->sector, faila, failb); |
| 1032 | |
| 1033 | atomic_inc(&sh->count); |
| 1034 | |
| 1035 | if (failb == syndrome_disks+1) { |
| 1036 | /* Q disk is one of the missing disks */ |
| 1037 | if (faila == syndrome_disks) { |
| 1038 | /* Missing P+Q, just recompute */ |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1039 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 1040 | ops_complete_compute, sh, |
| 1041 | to_addr_conv(sh, percpu)); |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 1042 | return async_gen_syndrome(blocks, 0, syndrome_disks+2, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1043 | STRIPE_SIZE, &submit); |
| 1044 | } else { |
| 1045 | struct page *dest; |
| 1046 | int data_target; |
| 1047 | int qd_idx = sh->qd_idx; |
| 1048 | |
| 1049 | /* Missing D+Q: recompute D from P, then recompute Q */ |
| 1050 | if (target == qd_idx) |
| 1051 | data_target = target2; |
| 1052 | else |
| 1053 | data_target = target; |
| 1054 | |
| 1055 | count = 0; |
| 1056 | for (i = disks; i-- ; ) { |
| 1057 | if (i == data_target || i == qd_idx) |
| 1058 | continue; |
| 1059 | blocks[count++] = sh->dev[i].page; |
| 1060 | } |
| 1061 | dest = sh->dev[data_target].page; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1062 | init_async_submit(&submit, |
| 1063 | ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
| 1064 | NULL, NULL, NULL, |
| 1065 | to_addr_conv(sh, percpu)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1066 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, |
| 1067 | &submit); |
| 1068 | |
| 1069 | count = set_syndrome_sources(blocks, sh); |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1070 | init_async_submit(&submit, ASYNC_TX_FENCE, tx, |
| 1071 | ops_complete_compute, sh, |
| 1072 | to_addr_conv(sh, percpu)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1073 | return async_gen_syndrome(blocks, 0, count+2, |
| 1074 | STRIPE_SIZE, &submit); |
| 1075 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1076 | } else { |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1077 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 1078 | ops_complete_compute, sh, |
| 1079 | to_addr_conv(sh, percpu)); |
| 1080 | if (failb == syndrome_disks) { |
| 1081 | /* We're missing D+P. */ |
| 1082 | return async_raid6_datap_recov(syndrome_disks+2, |
| 1083 | STRIPE_SIZE, faila, |
| 1084 | blocks, &submit); |
| 1085 | } else { |
| 1086 | /* We're missing D+D. */ |
| 1087 | return async_raid6_2data_recov(syndrome_disks+2, |
| 1088 | STRIPE_SIZE, faila, failb, |
| 1089 | blocks, &submit); |
| 1090 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1095 | static void ops_complete_prexor(void *stripe_head_ref) |
| 1096 | { |
| 1097 | struct stripe_head *sh = stripe_head_ref; |
| 1098 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1099 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1100 | (unsigned long long)sh->sector); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | static struct dma_async_tx_descriptor * |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1104 | ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1105 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1106 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1107 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1108 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1109 | int count = 0, pd_idx = sh->pd_idx, i; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1110 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1111 | |
| 1112 | /* existing parity data subtracted */ |
| 1113 | struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 1114 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1115 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1116 | (unsigned long long)sh->sector); |
| 1117 | |
| 1118 | for (i = disks; i--; ) { |
| 1119 | struct r5dev *dev = &sh->dev[i]; |
| 1120 | /* Only process blocks that are known to be uptodate */ |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1121 | if (test_bit(R5_Wantdrain, &dev->flags)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1122 | xor_srcs[count++] = dev->page; |
| 1123 | } |
| 1124 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1125 | 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] | 1126 | ops_complete_prexor, sh, to_addr_conv(sh, percpu)); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1127 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1128 | |
| 1129 | return tx; |
| 1130 | } |
| 1131 | |
| 1132 | static struct dma_async_tx_descriptor * |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1133 | 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] | 1134 | { |
| 1135 | int disks = sh->disks; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1136 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1137 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1138 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1139 | (unsigned long long)sh->sector); |
| 1140 | |
| 1141 | for (i = disks; i--; ) { |
| 1142 | struct r5dev *dev = &sh->dev[i]; |
| 1143 | struct bio *chosen; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1144 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1145 | if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1146 | struct bio *wbi; |
| 1147 | |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 1148 | spin_lock_irq(&sh->raid_conf->device_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1149 | chosen = dev->towrite; |
| 1150 | dev->towrite = NULL; |
| 1151 | BUG_ON(dev->written); |
| 1152 | wbi = dev->written = chosen; |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 1153 | spin_unlock_irq(&sh->raid_conf->device_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1154 | |
| 1155 | while (wbi && wbi->bi_sector < |
| 1156 | dev->sector + STRIPE_SECTORS) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1157 | if (wbi->bi_rw & REQ_FUA) |
| 1158 | set_bit(R5_WantFUA, &dev->flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1159 | if (wbi->bi_rw & REQ_SYNC) |
| 1160 | set_bit(R5_SyncIO, &dev->flags); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1161 | tx = async_copy_data(1, wbi, dev->page, |
| 1162 | dev->sector, tx); |
| 1163 | wbi = r5_next_bio(wbi, dev->sector); |
| 1164 | } |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | return tx; |
| 1169 | } |
| 1170 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1171 | static void ops_complete_reconstruct(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1172 | { |
| 1173 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1174 | int disks = sh->disks; |
| 1175 | int pd_idx = sh->pd_idx; |
| 1176 | int qd_idx = sh->qd_idx; |
| 1177 | int i; |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1178 | bool fua = false, sync = false; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1179 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1180 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1181 | (unsigned long long)sh->sector); |
| 1182 | |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1183 | for (i = disks; i--; ) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1184 | fua |= test_bit(R5_WantFUA, &sh->dev[i].flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1185 | sync |= test_bit(R5_SyncIO, &sh->dev[i].flags); |
| 1186 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1187 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1188 | for (i = disks; i--; ) { |
| 1189 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1190 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1191 | if (dev->written || i == pd_idx || i == qd_idx) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1192 | set_bit(R5_UPTODATE, &dev->flags); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1193 | if (fua) |
| 1194 | set_bit(R5_WantFUA, &dev->flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1195 | if (sync) |
| 1196 | set_bit(R5_SyncIO, &dev->flags); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1197 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1198 | } |
| 1199 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1200 | if (sh->reconstruct_state == reconstruct_state_drain_run) |
| 1201 | sh->reconstruct_state = reconstruct_state_drain_result; |
| 1202 | else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) |
| 1203 | sh->reconstruct_state = reconstruct_state_prexor_drain_result; |
| 1204 | else { |
| 1205 | BUG_ON(sh->reconstruct_state != reconstruct_state_run); |
| 1206 | sh->reconstruct_state = reconstruct_state_result; |
| 1207 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1208 | |
| 1209 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1210 | release_stripe(sh); |
| 1211 | } |
| 1212 | |
| 1213 | static void |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1214 | ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1215 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1216 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1217 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1218 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1219 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1220 | int count = 0, pd_idx = sh->pd_idx, i; |
| 1221 | struct page *xor_dest; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1222 | int prexor = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1223 | unsigned long flags; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1224 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1225 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1226 | (unsigned long long)sh->sector); |
| 1227 | |
| 1228 | /* check if prexor is active which means only process blocks |
| 1229 | * that are part of a read-modify-write (written) |
| 1230 | */ |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1231 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) { |
| 1232 | prexor = 1; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1233 | xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 1234 | for (i = disks; i--; ) { |
| 1235 | struct r5dev *dev = &sh->dev[i]; |
| 1236 | if (dev->written) |
| 1237 | xor_srcs[count++] = dev->page; |
| 1238 | } |
| 1239 | } else { |
| 1240 | xor_dest = sh->dev[pd_idx].page; |
| 1241 | for (i = disks; i--; ) { |
| 1242 | struct r5dev *dev = &sh->dev[i]; |
| 1243 | if (i != pd_idx) |
| 1244 | xor_srcs[count++] = dev->page; |
| 1245 | } |
| 1246 | } |
| 1247 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1248 | /* 1/ if we prexor'd then the dest is reused as a source |
| 1249 | * 2/ if we did not prexor then we are redoing the parity |
| 1250 | * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST |
| 1251 | * for the synchronous xor case |
| 1252 | */ |
Dan Williams | 88ba2aa | 2009-04-09 16:16:18 -0700 | [diff] [blame] | 1253 | flags = ASYNC_TX_ACK | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1254 | (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST); |
| 1255 | |
| 1256 | atomic_inc(&sh->count); |
| 1257 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1258 | init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh, |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1259 | to_addr_conv(sh, percpu)); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1260 | if (unlikely(count == 1)) |
| 1261 | tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit); |
| 1262 | else |
| 1263 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1264 | } |
| 1265 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1266 | static void |
| 1267 | ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1268 | struct dma_async_tx_descriptor *tx) |
| 1269 | { |
| 1270 | struct async_submit_ctl submit; |
| 1271 | struct page **blocks = percpu->scribble; |
| 1272 | int count; |
| 1273 | |
| 1274 | pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector); |
| 1275 | |
| 1276 | count = set_syndrome_sources(blocks, sh); |
| 1277 | |
| 1278 | atomic_inc(&sh->count); |
| 1279 | |
| 1280 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct, |
| 1281 | sh, to_addr_conv(sh, percpu)); |
| 1282 | async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | static void ops_complete_check(void *stripe_head_ref) |
| 1286 | { |
| 1287 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1288 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1289 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1290 | (unsigned long long)sh->sector); |
| 1291 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 1292 | sh->check_state = check_state_check_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1293 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1294 | release_stripe(sh); |
| 1295 | } |
| 1296 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1297 | 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] | 1298 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1299 | int disks = sh->disks; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1300 | int pd_idx = sh->pd_idx; |
| 1301 | int qd_idx = sh->qd_idx; |
| 1302 | struct page *xor_dest; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1303 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1304 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1305 | struct async_submit_ctl submit; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1306 | int count; |
| 1307 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1308 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1309 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1310 | (unsigned long long)sh->sector); |
| 1311 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1312 | count = 0; |
| 1313 | xor_dest = sh->dev[pd_idx].page; |
| 1314 | xor_srcs[count++] = xor_dest; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1315 | for (i = disks; i--; ) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1316 | if (i == pd_idx || i == qd_idx) |
| 1317 | continue; |
| 1318 | xor_srcs[count++] = sh->dev[i].page; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1319 | } |
| 1320 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1321 | init_async_submit(&submit, 0, NULL, NULL, NULL, |
| 1322 | to_addr_conv(sh, percpu)); |
Dan Williams | 099f53c | 2009-04-08 14:28:37 -0700 | [diff] [blame] | 1323 | tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1324 | &sh->ops.zero_sum_result, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1325 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1326 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1327 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL); |
| 1328 | tx = async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1329 | } |
| 1330 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1331 | static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp) |
| 1332 | { |
| 1333 | struct page **srcs = percpu->scribble; |
| 1334 | struct async_submit_ctl submit; |
| 1335 | int count; |
| 1336 | |
| 1337 | pr_debug("%s: stripe %llu checkp: %d\n", __func__, |
| 1338 | (unsigned long long)sh->sector, checkp); |
| 1339 | |
| 1340 | count = set_syndrome_sources(srcs, sh); |
| 1341 | if (!checkp) |
| 1342 | srcs[count] = NULL; |
| 1343 | |
| 1344 | atomic_inc(&sh->count); |
| 1345 | init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check, |
| 1346 | sh, to_addr_conv(sh, percpu)); |
| 1347 | async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE, |
| 1348 | &sh->ops.zero_sum_result, percpu->spare_page, &submit); |
| 1349 | } |
| 1350 | |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 1351 | 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] | 1352 | { |
| 1353 | int overlap_clear = 0, i, disks = sh->disks; |
| 1354 | struct dma_async_tx_descriptor *tx = NULL; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1355 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1356 | int level = conf->level; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1357 | struct raid5_percpu *percpu; |
| 1358 | unsigned long cpu; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1359 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1360 | cpu = get_cpu(); |
| 1361 | percpu = per_cpu_ptr(conf->percpu, cpu); |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 1362 | if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1363 | ops_run_biofill(sh); |
| 1364 | overlap_clear++; |
| 1365 | } |
| 1366 | |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 1367 | if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1368 | if (level < 6) |
| 1369 | tx = ops_run_compute5(sh, percpu); |
| 1370 | else { |
| 1371 | if (sh->ops.target2 < 0 || sh->ops.target < 0) |
| 1372 | tx = ops_run_compute6_1(sh, percpu); |
| 1373 | else |
| 1374 | tx = ops_run_compute6_2(sh, percpu); |
| 1375 | } |
| 1376 | /* terminate the chain if reconstruct is not set to be run */ |
| 1377 | if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 1378 | async_tx_ack(tx); |
| 1379 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1380 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1381 | if (test_bit(STRIPE_OP_PREXOR, &ops_request)) |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1382 | tx = ops_run_prexor(sh, percpu, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1383 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1384 | if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) { |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1385 | tx = ops_run_biodrain(sh, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1386 | overlap_clear++; |
| 1387 | } |
| 1388 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1389 | if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) { |
| 1390 | if (level < 6) |
| 1391 | ops_run_reconstruct5(sh, percpu, tx); |
| 1392 | else |
| 1393 | ops_run_reconstruct6(sh, percpu, tx); |
| 1394 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1395 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1396 | if (test_bit(STRIPE_OP_CHECK, &ops_request)) { |
| 1397 | if (sh->check_state == check_state_run) |
| 1398 | ops_run_check_p(sh, percpu); |
| 1399 | else if (sh->check_state == check_state_run_q) |
| 1400 | ops_run_check_pq(sh, percpu, 0); |
| 1401 | else if (sh->check_state == check_state_run_pq) |
| 1402 | ops_run_check_pq(sh, percpu, 1); |
| 1403 | else |
| 1404 | BUG(); |
| 1405 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1406 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1407 | if (overlap_clear) |
| 1408 | for (i = disks; i--; ) { |
| 1409 | struct r5dev *dev = &sh->dev[i]; |
| 1410 | if (test_and_clear_bit(R5_Overlap, &dev->flags)) |
| 1411 | wake_up(&sh->raid_conf->wait_for_overlap); |
| 1412 | } |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1413 | put_cpu(); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1414 | } |
| 1415 | |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 1416 | #ifdef CONFIG_MULTICORE_RAID456 |
| 1417 | static void async_run_ops(void *param, async_cookie_t cookie) |
| 1418 | { |
| 1419 | struct stripe_head *sh = param; |
| 1420 | unsigned long ops_request = sh->ops.request; |
| 1421 | |
| 1422 | clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state); |
| 1423 | wake_up(&sh->ops.wait_for_ops); |
| 1424 | |
| 1425 | __raid_run_ops(sh, ops_request); |
| 1426 | release_stripe(sh); |
| 1427 | } |
| 1428 | |
| 1429 | static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request) |
| 1430 | { |
| 1431 | /* since handle_stripe can be called outside of raid5d context |
| 1432 | * we need to ensure sh->ops.request is de-staged before another |
| 1433 | * request arrives |
| 1434 | */ |
| 1435 | wait_event(sh->ops.wait_for_ops, |
| 1436 | !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state)); |
| 1437 | sh->ops.request = ops_request; |
| 1438 | |
| 1439 | atomic_inc(&sh->count); |
| 1440 | async_schedule(async_run_ops, sh); |
| 1441 | } |
| 1442 | #else |
| 1443 | #define raid_run_ops __raid_run_ops |
| 1444 | #endif |
| 1445 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1446 | static int grow_one_stripe(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | { |
| 1448 | struct stripe_head *sh; |
Namhyung Kim | 6ce3284 | 2011-07-18 17:38:50 +1000 | [diff] [blame] | 1449 | sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1450 | if (!sh) |
| 1451 | return 0; |
Namhyung Kim | 6ce3284 | 2011-07-18 17:38:50 +1000 | [diff] [blame] | 1452 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1453 | sh->raid_conf = conf; |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 1454 | #ifdef CONFIG_MULTICORE_RAID456 |
| 1455 | init_waitqueue_head(&sh->ops.wait_for_ops); |
| 1456 | #endif |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1457 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 1458 | if (grow_buffers(sh)) { |
| 1459 | shrink_buffers(sh); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1460 | kmem_cache_free(conf->slab_cache, sh); |
| 1461 | return 0; |
| 1462 | } |
| 1463 | /* we just created an active stripe so... */ |
| 1464 | atomic_set(&sh->count, 1); |
| 1465 | atomic_inc(&conf->active_stripes); |
| 1466 | INIT_LIST_HEAD(&sh->lru); |
| 1467 | release_stripe(sh); |
| 1468 | return 1; |
| 1469 | } |
| 1470 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1471 | static int grow_stripes(struct r5conf *conf, int num) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1472 | { |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 1473 | struct kmem_cache *sc; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 1474 | int devs = max(conf->raid_disks, conf->previous_raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | |
NeilBrown | f4be6b4 | 2010-06-01 19:37:25 +1000 | [diff] [blame] | 1476 | if (conf->mddev->gendisk) |
| 1477 | sprintf(conf->cache_name[0], |
| 1478 | "raid%d-%s", conf->level, mdname(conf->mddev)); |
| 1479 | else |
| 1480 | sprintf(conf->cache_name[0], |
| 1481 | "raid%d-%p", conf->level, conf->mddev); |
| 1482 | sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]); |
| 1483 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1484 | conf->active_name = 0; |
| 1485 | sc = kmem_cache_create(conf->cache_name[conf->active_name], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1487 | 0, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | if (!sc) |
| 1489 | return 1; |
| 1490 | conf->slab_cache = sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1491 | conf->pool_size = devs; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1492 | while (num--) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1493 | if (!grow_one_stripe(conf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | return 0; |
| 1496 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 1497 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1498 | /** |
| 1499 | * scribble_len - return the required size of the scribble region |
| 1500 | * @num - total number of disks in the array |
| 1501 | * |
| 1502 | * The size must be enough to contain: |
| 1503 | * 1/ a struct page pointer for each device in the array +2 |
| 1504 | * 2/ room to convert each entry in (1) to its corresponding dma |
| 1505 | * (dma_map_page()) or page (page_address()) address. |
| 1506 | * |
| 1507 | * Note: the +2 is for the destination buffers of the ddf/raid6 case where we |
| 1508 | * calculate over all devices (not just the data blocks), using zeros in place |
| 1509 | * of the P and Q blocks. |
| 1510 | */ |
| 1511 | static size_t scribble_len(int num) |
| 1512 | { |
| 1513 | size_t len; |
| 1514 | |
| 1515 | len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2); |
| 1516 | |
| 1517 | return len; |
| 1518 | } |
| 1519 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1520 | static int resize_stripes(struct r5conf *conf, int newsize) |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1521 | { |
| 1522 | /* Make all the stripes able to hold 'newsize' devices. |
| 1523 | * New slots in each stripe get 'page' set to a new page. |
| 1524 | * |
| 1525 | * This happens in stages: |
| 1526 | * 1/ create a new kmem_cache and allocate the required number of |
| 1527 | * stripe_heads. |
| 1528 | * 2/ gather all the old stripe_heads and tranfer the pages across |
| 1529 | * to the new stripe_heads. This will have the side effect of |
| 1530 | * freezing the array as once all stripe_heads have been collected, |
| 1531 | * no IO will be possible. Old stripe heads are freed once their |
| 1532 | * pages have been transferred over, and the old kmem_cache is |
| 1533 | * freed when all stripes are done. |
| 1534 | * 3/ reallocate conf->disks to be suitable bigger. If this fails, |
| 1535 | * we simple return a failre status - no need to clean anything up. |
| 1536 | * 4/ allocate new pages for the new slots in the new stripe_heads. |
| 1537 | * If this fails, we don't bother trying the shrink the |
| 1538 | * stripe_heads down again, we just leave them as they are. |
| 1539 | * As each stripe_head is processed the new one is released into |
| 1540 | * active service. |
| 1541 | * |
| 1542 | * Once step2 is started, we cannot afford to wait for a write, |
| 1543 | * so we use GFP_NOIO allocations. |
| 1544 | */ |
| 1545 | struct stripe_head *osh, *nsh; |
| 1546 | LIST_HEAD(newstripes); |
| 1547 | struct disk_info *ndisks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1548 | unsigned long cpu; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 1549 | int err; |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 1550 | struct kmem_cache *sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1551 | int i; |
| 1552 | |
| 1553 | if (newsize <= conf->pool_size) |
| 1554 | return 0; /* never bother to shrink */ |
| 1555 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 1556 | err = md_allow_write(conf->mddev); |
| 1557 | if (err) |
| 1558 | return err; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 1559 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1560 | /* Step 1 */ |
| 1561 | sc = kmem_cache_create(conf->cache_name[1-conf->active_name], |
| 1562 | sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1563 | 0, 0, NULL); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1564 | if (!sc) |
| 1565 | return -ENOMEM; |
| 1566 | |
| 1567 | for (i = conf->max_nr_stripes; i; i--) { |
Namhyung Kim | 6ce3284 | 2011-07-18 17:38:50 +1000 | [diff] [blame] | 1568 | nsh = kmem_cache_zalloc(sc, GFP_KERNEL); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1569 | if (!nsh) |
| 1570 | break; |
| 1571 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1572 | nsh->raid_conf = conf; |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 1573 | #ifdef CONFIG_MULTICORE_RAID456 |
| 1574 | init_waitqueue_head(&nsh->ops.wait_for_ops); |
| 1575 | #endif |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1576 | |
| 1577 | list_add(&nsh->lru, &newstripes); |
| 1578 | } |
| 1579 | if (i) { |
| 1580 | /* didn't get enough, give up */ |
| 1581 | while (!list_empty(&newstripes)) { |
| 1582 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 1583 | list_del(&nsh->lru); |
| 1584 | kmem_cache_free(sc, nsh); |
| 1585 | } |
| 1586 | kmem_cache_destroy(sc); |
| 1587 | return -ENOMEM; |
| 1588 | } |
| 1589 | /* Step 2 - Must use GFP_NOIO now. |
| 1590 | * OK, we have enough stripes, start collecting inactive |
| 1591 | * stripes and copying them over |
| 1592 | */ |
| 1593 | list_for_each_entry(nsh, &newstripes, lru) { |
| 1594 | spin_lock_irq(&conf->device_lock); |
| 1595 | wait_event_lock_irq(conf->wait_for_stripe, |
| 1596 | !list_empty(&conf->inactive_list), |
| 1597 | conf->device_lock, |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 1598 | ); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1599 | osh = get_free_stripe(conf); |
| 1600 | spin_unlock_irq(&conf->device_lock); |
| 1601 | atomic_set(&nsh->count, 1); |
| 1602 | for(i=0; i<conf->pool_size; i++) |
| 1603 | nsh->dev[i].page = osh->dev[i].page; |
| 1604 | for( ; i<newsize; i++) |
| 1605 | nsh->dev[i].page = NULL; |
| 1606 | kmem_cache_free(conf->slab_cache, osh); |
| 1607 | } |
| 1608 | kmem_cache_destroy(conf->slab_cache); |
| 1609 | |
| 1610 | /* Step 3. |
| 1611 | * At this point, we are holding all the stripes so the array |
| 1612 | * is completely stalled, so now is a good time to resize |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1613 | * conf->disks and the scribble region |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1614 | */ |
| 1615 | ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO); |
| 1616 | if (ndisks) { |
| 1617 | for (i=0; i<conf->raid_disks; i++) |
| 1618 | ndisks[i] = conf->disks[i]; |
| 1619 | kfree(conf->disks); |
| 1620 | conf->disks = ndisks; |
| 1621 | } else |
| 1622 | err = -ENOMEM; |
| 1623 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1624 | get_online_cpus(); |
| 1625 | conf->scribble_len = scribble_len(newsize); |
| 1626 | for_each_present_cpu(cpu) { |
| 1627 | struct raid5_percpu *percpu; |
| 1628 | void *scribble; |
| 1629 | |
| 1630 | percpu = per_cpu_ptr(conf->percpu, cpu); |
| 1631 | scribble = kmalloc(conf->scribble_len, GFP_NOIO); |
| 1632 | |
| 1633 | if (scribble) { |
| 1634 | kfree(percpu->scribble); |
| 1635 | percpu->scribble = scribble; |
| 1636 | } else { |
| 1637 | err = -ENOMEM; |
| 1638 | break; |
| 1639 | } |
| 1640 | } |
| 1641 | put_online_cpus(); |
| 1642 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1643 | /* Step 4, return new stripes to service */ |
| 1644 | while(!list_empty(&newstripes)) { |
| 1645 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 1646 | list_del_init(&nsh->lru); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1647 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1648 | for (i=conf->raid_disks; i < newsize; i++) |
| 1649 | if (nsh->dev[i].page == NULL) { |
| 1650 | struct page *p = alloc_page(GFP_NOIO); |
| 1651 | nsh->dev[i].page = p; |
| 1652 | if (!p) |
| 1653 | err = -ENOMEM; |
| 1654 | } |
| 1655 | release_stripe(nsh); |
| 1656 | } |
| 1657 | /* critical section pass, GFP_NOIO no longer needed */ |
| 1658 | |
| 1659 | conf->slab_cache = sc; |
| 1660 | conf->active_name = 1-conf->active_name; |
| 1661 | conf->pool_size = newsize; |
| 1662 | return err; |
| 1663 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1665 | static int drop_one_stripe(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | { |
| 1667 | struct stripe_head *sh; |
| 1668 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1669 | spin_lock_irq(&conf->device_lock); |
| 1670 | sh = get_free_stripe(conf); |
| 1671 | spin_unlock_irq(&conf->device_lock); |
| 1672 | if (!sh) |
| 1673 | return 0; |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 1674 | BUG_ON(atomic_read(&sh->count)); |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 1675 | shrink_buffers(sh); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1676 | kmem_cache_free(conf->slab_cache, sh); |
| 1677 | atomic_dec(&conf->active_stripes); |
| 1678 | return 1; |
| 1679 | } |
| 1680 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1681 | static void shrink_stripes(struct r5conf *conf) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1682 | { |
| 1683 | while (drop_one_stripe(conf)) |
| 1684 | ; |
| 1685 | |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1686 | if (conf->slab_cache) |
| 1687 | kmem_cache_destroy(conf->slab_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | conf->slab_cache = NULL; |
| 1689 | } |
| 1690 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1691 | static void raid5_end_read_request(struct bio * bi, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1693 | struct stripe_head *sh = bi->bi_private; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1694 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 1695 | int disks = sh->disks, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1696 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1697 | char b[BDEVNAME_SIZE]; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1698 | struct md_rdev *rdev = NULL; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1699 | sector_t s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | |
| 1701 | for (i=0 ; i<disks; i++) |
| 1702 | if (bi == &sh->dev[i].req) |
| 1703 | break; |
| 1704 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1705 | pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n", |
| 1706 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | uptodate); |
| 1708 | if (i == disks) { |
| 1709 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1710 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | } |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1712 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1713 | /* If replacement finished while this request was outstanding, |
| 1714 | * 'replacement' might be NULL already. |
| 1715 | * In that case it moved down to 'rdev'. |
| 1716 | * rdev is not removed until all requests are finished. |
| 1717 | */ |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1718 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1719 | if (!rdev) |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1720 | rdev = conf->disks[i].rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1722 | if (use_new_offset(conf, sh)) |
| 1723 | s = sh->sector + rdev->new_data_offset; |
| 1724 | else |
| 1725 | s = sh->sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | if (uptodate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | set_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1728 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1729 | /* Note that this cannot happen on a |
| 1730 | * replacement device. We just fail those on |
| 1731 | * any error |
| 1732 | */ |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1733 | printk_ratelimited( |
| 1734 | KERN_INFO |
| 1735 | "md/raid:%s: read error corrected" |
| 1736 | " (%lu sectors at %llu on %s)\n", |
| 1737 | mdname(conf->mddev), STRIPE_SECTORS, |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1738 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1739 | bdevname(rdev->bdev, b)); |
Namhyung Kim | ddd5115 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1740 | atomic_add(STRIPE_SECTORS, &rdev->corrected_errors); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1741 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 1742 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
| 1743 | } |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1744 | if (atomic_read(&rdev->read_errors)) |
| 1745 | atomic_set(&rdev->read_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | } else { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1747 | const char *bdn = bdevname(rdev->bdev, b); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1748 | int retry = 0; |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1749 | int set_bad = 0; |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1750 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | clear_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1752 | atomic_inc(&rdev->read_errors); |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1753 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) |
| 1754 | printk_ratelimited( |
| 1755 | KERN_WARNING |
| 1756 | "md/raid:%s: read error on replacement device " |
| 1757 | "(sector %llu on %s).\n", |
| 1758 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1759 | (unsigned long long)s, |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1760 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1761 | else if (conf->mddev->degraded >= conf->max_degraded) { |
| 1762 | set_bad = 1; |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1763 | printk_ratelimited( |
| 1764 | KERN_WARNING |
| 1765 | "md/raid:%s: read error not correctable " |
| 1766 | "(sector %llu on %s).\n", |
| 1767 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1768 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1769 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1770 | } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) { |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1771 | /* Oh, no!!! */ |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1772 | set_bad = 1; |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1773 | printk_ratelimited( |
| 1774 | KERN_WARNING |
| 1775 | "md/raid:%s: read error NOT corrected!! " |
| 1776 | "(sector %llu on %s).\n", |
| 1777 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1778 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1779 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1780 | } else if (atomic_read(&rdev->read_errors) |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1781 | > conf->max_nr_stripes) |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 1782 | printk(KERN_WARNING |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 1783 | "md/raid:%s: Too many read errors, failing device %s.\n", |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1784 | mdname(conf->mddev), bdn); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1785 | else |
| 1786 | retry = 1; |
| 1787 | if (retry) |
| 1788 | set_bit(R5_ReadError, &sh->dev[i].flags); |
| 1789 | else { |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1790 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 1791 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1792 | if (!(set_bad |
| 1793 | && test_bit(In_sync, &rdev->flags) |
| 1794 | && rdev_set_badblocks( |
| 1795 | rdev, sh->sector, STRIPE_SECTORS, 0))) |
| 1796 | md_error(conf->mddev, rdev); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1797 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | } |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1799 | rdev_dec_pending(rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 1801 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1802 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | } |
| 1804 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 1805 | static void raid5_end_write_request(struct bio *bi, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1806 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1807 | struct stripe_head *sh = bi->bi_private; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1808 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 1809 | int disks = sh->disks, i; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1810 | struct md_rdev *uninitialized_var(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 1812 | sector_t first_bad; |
| 1813 | int bad_sectors; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1814 | int replacement = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1816 | for (i = 0 ; i < disks; i++) { |
| 1817 | if (bi == &sh->dev[i].req) { |
| 1818 | rdev = conf->disks[i].rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | break; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1820 | } |
| 1821 | if (bi == &sh->dev[i].rreq) { |
| 1822 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1823 | if (rdev) |
| 1824 | replacement = 1; |
| 1825 | else |
| 1826 | /* rdev was removed and 'replacement' |
| 1827 | * replaced it. rdev is not removed |
| 1828 | * until all requests are finished. |
| 1829 | */ |
| 1830 | rdev = conf->disks[i].rdev; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1831 | break; |
| 1832 | } |
| 1833 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1834 | pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1835 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
| 1836 | uptodate); |
| 1837 | if (i == disks) { |
| 1838 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1839 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | } |
| 1841 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1842 | if (replacement) { |
| 1843 | if (!uptodate) |
| 1844 | md_error(conf->mddev, rdev); |
| 1845 | else if (is_badblock(rdev, sh->sector, |
| 1846 | STRIPE_SECTORS, |
| 1847 | &first_bad, &bad_sectors)) |
| 1848 | set_bit(R5_MadeGoodRepl, &sh->dev[i].flags); |
| 1849 | } else { |
| 1850 | if (!uptodate) { |
| 1851 | set_bit(WriteErrorSeen, &rdev->flags); |
| 1852 | set_bit(R5_WriteError, &sh->dev[i].flags); |
NeilBrown | 3a6de29 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 1853 | if (!test_and_set_bit(WantReplacement, &rdev->flags)) |
| 1854 | set_bit(MD_RECOVERY_NEEDED, |
| 1855 | &rdev->mddev->recovery); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1856 | } else if (is_badblock(rdev, sh->sector, |
| 1857 | STRIPE_SECTORS, |
| 1858 | &first_bad, &bad_sectors)) |
| 1859 | set_bit(R5_MadeGood, &sh->dev[i].flags); |
| 1860 | } |
| 1861 | rdev_dec_pending(rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1863 | if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags)) |
| 1864 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | set_bit(STRIPE_HANDLE, &sh->state); |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1866 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1867 | } |
| 1868 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1869 | 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] | 1870 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1871 | 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] | 1872 | { |
| 1873 | struct r5dev *dev = &sh->dev[i]; |
| 1874 | |
| 1875 | bio_init(&dev->req); |
| 1876 | dev->req.bi_io_vec = &dev->vec; |
| 1877 | dev->req.bi_vcnt++; |
| 1878 | dev->req.bi_max_vecs++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1879 | dev->req.bi_private = sh; |
NeilBrown | 995c427 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1880 | dev->vec.bv_page = dev->page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1881 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1882 | bio_init(&dev->rreq); |
| 1883 | dev->rreq.bi_io_vec = &dev->rvec; |
| 1884 | dev->rreq.bi_vcnt++; |
| 1885 | dev->rreq.bi_max_vecs++; |
| 1886 | dev->rreq.bi_private = sh; |
| 1887 | dev->rvec.bv_page = dev->page; |
| 1888 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | dev->flags = 0; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1890 | dev->sector = compute_blocknr(sh, i, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1891 | } |
| 1892 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1893 | static void error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | { |
| 1895 | char b[BDEVNAME_SIZE]; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1896 | struct r5conf *conf = mddev->private; |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 1897 | unsigned long flags; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 1898 | pr_debug("raid456: error called\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 1900 | spin_lock_irqsave(&conf->device_lock, flags); |
| 1901 | clear_bit(In_sync, &rdev->flags); |
| 1902 | mddev->degraded = calc_degraded(conf); |
| 1903 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 1904 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 1905 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 1906 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 6f8d0c7 | 2011-05-11 14:38:44 +1000 | [diff] [blame] | 1907 | set_bit(Faulty, &rdev->flags); |
| 1908 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 1909 | printk(KERN_ALERT |
| 1910 | "md/raid:%s: Disk failure on %s, disabling device.\n" |
| 1911 | "md/raid:%s: Operation continuing on %d devices.\n", |
| 1912 | mdname(mddev), |
| 1913 | bdevname(rdev->bdev, b), |
| 1914 | mdname(mddev), |
| 1915 | conf->raid_disks - mddev->degraded); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1916 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | |
| 1918 | /* |
| 1919 | * Input: a 'big' sector number, |
| 1920 | * Output: index of the data and parity disk, and the sector # in them. |
| 1921 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1922 | static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1923 | int previous, int *dd_idx, |
| 1924 | struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1925 | { |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1926 | sector_t stripe, stripe2; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 1927 | sector_t chunk_number; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1928 | unsigned int chunk_offset; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1929 | int pd_idx, qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1930 | int ddf_layout = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | sector_t new_sector; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1932 | int algorithm = previous ? conf->prev_algo |
| 1933 | : conf->algorithm; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 1934 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
| 1935 | : conf->chunk_sectors; |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1936 | int raid_disks = previous ? conf->previous_raid_disks |
| 1937 | : conf->raid_disks; |
| 1938 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | |
| 1940 | /* First compute the information on this sector */ |
| 1941 | |
| 1942 | /* |
| 1943 | * Compute the chunk number and the sector offset inside the chunk |
| 1944 | */ |
| 1945 | chunk_offset = sector_div(r_sector, sectors_per_chunk); |
| 1946 | chunk_number = r_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | |
| 1948 | /* |
| 1949 | * Compute the stripe number |
| 1950 | */ |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 1951 | stripe = chunk_number; |
| 1952 | *dd_idx = sector_div(stripe, data_disks); |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1953 | stripe2 = stripe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1954 | /* |
| 1955 | * Select the parity disk based on the user selected algorithm. |
| 1956 | */ |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1957 | pd_idx = qd_idx = -1; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1958 | switch(conf->level) { |
| 1959 | case 4: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1960 | pd_idx = data_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1961 | break; |
| 1962 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1963 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1964 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1965 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1966 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1967 | (*dd_idx)++; |
| 1968 | break; |
| 1969 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1970 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1971 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1972 | (*dd_idx)++; |
| 1973 | break; |
| 1974 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1975 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1976 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | break; |
| 1978 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1979 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1980 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1982 | case ALGORITHM_PARITY_0: |
| 1983 | pd_idx = 0; |
| 1984 | (*dd_idx)++; |
| 1985 | break; |
| 1986 | case ALGORITHM_PARITY_N: |
| 1987 | pd_idx = data_disks; |
| 1988 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1990 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1991 | } |
| 1992 | break; |
| 1993 | case 6: |
| 1994 | |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1995 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1996 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1997 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1998 | qd_idx = pd_idx + 1; |
| 1999 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2000 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2001 | qd_idx = 0; |
| 2002 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2003 | (*dd_idx) += 2; /* D D P Q D */ |
| 2004 | break; |
| 2005 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2006 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2007 | qd_idx = pd_idx + 1; |
| 2008 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2009 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2010 | qd_idx = 0; |
| 2011 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2012 | (*dd_idx) += 2; /* D D P Q D */ |
| 2013 | break; |
| 2014 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2015 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2016 | qd_idx = (pd_idx + 1) % raid_disks; |
| 2017 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2018 | break; |
| 2019 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2020 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2021 | qd_idx = (pd_idx + 1) % raid_disks; |
| 2022 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2023 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2024 | |
| 2025 | case ALGORITHM_PARITY_0: |
| 2026 | pd_idx = 0; |
| 2027 | qd_idx = 1; |
| 2028 | (*dd_idx) += 2; |
| 2029 | break; |
| 2030 | case ALGORITHM_PARITY_N: |
| 2031 | pd_idx = data_disks; |
| 2032 | qd_idx = data_disks + 1; |
| 2033 | break; |
| 2034 | |
| 2035 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 2036 | /* Exactly the same as RIGHT_ASYMMETRIC, but or |
| 2037 | * of blocks for computing Q is different. |
| 2038 | */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2039 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2040 | qd_idx = pd_idx + 1; |
| 2041 | if (pd_idx == raid_disks-1) { |
| 2042 | (*dd_idx)++; /* Q D D D P */ |
| 2043 | qd_idx = 0; |
| 2044 | } else if (*dd_idx >= pd_idx) |
| 2045 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2046 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2047 | break; |
| 2048 | |
| 2049 | case ALGORITHM_ROTATING_N_RESTART: |
| 2050 | /* Same a left_asymmetric, by first stripe is |
| 2051 | * D D D P Q rather than |
| 2052 | * Q D D D P |
| 2053 | */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2054 | stripe2 += 1; |
| 2055 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2056 | qd_idx = pd_idx + 1; |
| 2057 | if (pd_idx == raid_disks-1) { |
| 2058 | (*dd_idx)++; /* Q D D D P */ |
| 2059 | qd_idx = 0; |
| 2060 | } else if (*dd_idx >= pd_idx) |
| 2061 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2062 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2063 | break; |
| 2064 | |
| 2065 | case ALGORITHM_ROTATING_N_CONTINUE: |
| 2066 | /* Same as left_symmetric but Q is before P */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2067 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2068 | qd_idx = (pd_idx + raid_disks - 1) % raid_disks; |
| 2069 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2070 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2071 | break; |
| 2072 | |
| 2073 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 2074 | /* RAID5 left_asymmetric, with Q on last device */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2075 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2076 | if (*dd_idx >= pd_idx) |
| 2077 | (*dd_idx)++; |
| 2078 | qd_idx = raid_disks - 1; |
| 2079 | break; |
| 2080 | |
| 2081 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2082 | pd_idx = sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2083 | if (*dd_idx >= pd_idx) |
| 2084 | (*dd_idx)++; |
| 2085 | qd_idx = raid_disks - 1; |
| 2086 | break; |
| 2087 | |
| 2088 | case ALGORITHM_LEFT_SYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2089 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2090 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 2091 | qd_idx = raid_disks - 1; |
| 2092 | break; |
| 2093 | |
| 2094 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2095 | pd_idx = sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2096 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 2097 | qd_idx = raid_disks - 1; |
| 2098 | break; |
| 2099 | |
| 2100 | case ALGORITHM_PARITY_0_6: |
| 2101 | pd_idx = 0; |
| 2102 | (*dd_idx)++; |
| 2103 | qd_idx = raid_disks - 1; |
| 2104 | break; |
| 2105 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2106 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2107 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2108 | } |
| 2109 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | } |
| 2111 | |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2112 | if (sh) { |
| 2113 | sh->pd_idx = pd_idx; |
| 2114 | sh->qd_idx = qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2115 | sh->ddf_layout = ddf_layout; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2116 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | /* |
| 2118 | * Finally, compute the new sector number |
| 2119 | */ |
| 2120 | new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset; |
| 2121 | return new_sector; |
| 2122 | } |
| 2123 | |
| 2124 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2125 | 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] | 2126 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2127 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 2128 | int raid_disks = sh->disks; |
| 2129 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2130 | sector_t new_sector = sh->sector, check; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 2131 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
| 2132 | : conf->chunk_sectors; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2133 | int algorithm = previous ? conf->prev_algo |
| 2134 | : conf->algorithm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2135 | sector_t stripe; |
| 2136 | int chunk_offset; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2137 | sector_t chunk_number; |
| 2138 | int dummy1, dd_idx = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2139 | sector_t r_sector; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2140 | struct stripe_head sh2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2141 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | chunk_offset = sector_div(new_sector, sectors_per_chunk); |
| 2144 | stripe = new_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2145 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2146 | if (i == sh->pd_idx) |
| 2147 | return 0; |
| 2148 | switch(conf->level) { |
| 2149 | case 4: break; |
| 2150 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2151 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2152 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 2153 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 2154 | if (i > sh->pd_idx) |
| 2155 | i--; |
| 2156 | break; |
| 2157 | case ALGORITHM_LEFT_SYMMETRIC: |
| 2158 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 2159 | if (i < sh->pd_idx) |
| 2160 | i += raid_disks; |
| 2161 | i -= (sh->pd_idx + 1); |
| 2162 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2163 | case ALGORITHM_PARITY_0: |
| 2164 | i -= 1; |
| 2165 | break; |
| 2166 | case ALGORITHM_PARITY_N: |
| 2167 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2169 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2170 | } |
| 2171 | break; |
| 2172 | case 6: |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2173 | if (i == sh->qd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2174 | return 0; /* It is the Q disk */ |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2175 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2176 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 2177 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2178 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 2179 | case ALGORITHM_ROTATING_N_RESTART: |
| 2180 | if (sh->pd_idx == raid_disks-1) |
| 2181 | i--; /* Q D D D P */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2182 | else if (i > sh->pd_idx) |
| 2183 | i -= 2; /* D D P Q D */ |
| 2184 | break; |
| 2185 | case ALGORITHM_LEFT_SYMMETRIC: |
| 2186 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 2187 | if (sh->pd_idx == raid_disks-1) |
| 2188 | i--; /* Q D D D P */ |
| 2189 | else { |
| 2190 | /* D D P Q D */ |
| 2191 | if (i < sh->pd_idx) |
| 2192 | i += raid_disks; |
| 2193 | i -= (sh->pd_idx + 2); |
| 2194 | } |
| 2195 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2196 | case ALGORITHM_PARITY_0: |
| 2197 | i -= 2; |
| 2198 | break; |
| 2199 | case ALGORITHM_PARITY_N: |
| 2200 | break; |
| 2201 | case ALGORITHM_ROTATING_N_CONTINUE: |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 2202 | /* Like left_symmetric, but P is before Q */ |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2203 | if (sh->pd_idx == 0) |
| 2204 | i--; /* P D D D Q */ |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 2205 | else { |
| 2206 | /* D D Q P D */ |
| 2207 | if (i < sh->pd_idx) |
| 2208 | i += raid_disks; |
| 2209 | i -= (sh->pd_idx + 1); |
| 2210 | } |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2211 | break; |
| 2212 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 2213 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 2214 | if (i > sh->pd_idx) |
| 2215 | i--; |
| 2216 | break; |
| 2217 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 2218 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 2219 | if (i < sh->pd_idx) |
| 2220 | i += data_disks + 1; |
| 2221 | i -= (sh->pd_idx + 1); |
| 2222 | break; |
| 2223 | case ALGORITHM_PARITY_0_6: |
| 2224 | i -= 1; |
| 2225 | break; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2226 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2227 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2228 | } |
| 2229 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2230 | } |
| 2231 | |
| 2232 | chunk_number = stripe * data_disks + i; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2233 | r_sector = chunk_number * sectors_per_chunk + chunk_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2234 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2235 | check = raid5_compute_sector(conf, r_sector, |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2236 | previous, &dummy1, &sh2); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2237 | if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx |
| 2238 | || sh2.qd_idx != sh->qd_idx) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 2239 | printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n", |
| 2240 | mdname(conf->mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | return 0; |
| 2242 | } |
| 2243 | return r_sector; |
| 2244 | } |
| 2245 | |
| 2246 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2247 | static void |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2248 | schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2249 | int rcw, int expand) |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2250 | { |
| 2251 | int i, pd_idx = sh->pd_idx, disks = sh->disks; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2252 | struct r5conf *conf = sh->raid_conf; |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2253 | int level = conf->level; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2254 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2255 | if (rcw) { |
| 2256 | /* if we are not expanding this is a proper write request, and |
| 2257 | * there will be bios with new data to be drained into the |
| 2258 | * stripe cache |
| 2259 | */ |
| 2260 | if (!expand) { |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2261 | sh->reconstruct_state = reconstruct_state_drain_run; |
| 2262 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
| 2263 | } else |
| 2264 | sh->reconstruct_state = reconstruct_state_run; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2265 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2266 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2267 | |
| 2268 | for (i = disks; i--; ) { |
| 2269 | struct r5dev *dev = &sh->dev[i]; |
| 2270 | |
| 2271 | if (dev->towrite) { |
| 2272 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2273 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2274 | if (!expand) |
| 2275 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2276 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2277 | } |
| 2278 | } |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2279 | if (s->locked + conf->max_degraded == disks) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2280 | if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state)) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2281 | atomic_inc(&conf->pending_full_writes); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2282 | } else { |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2283 | BUG_ON(level == 6); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2284 | BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) || |
| 2285 | test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags))); |
| 2286 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2287 | sh->reconstruct_state = reconstruct_state_prexor_drain_run; |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2288 | set_bit(STRIPE_OP_PREXOR, &s->ops_request); |
| 2289 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2290 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2291 | |
| 2292 | for (i = disks; i--; ) { |
| 2293 | struct r5dev *dev = &sh->dev[i]; |
| 2294 | if (i == pd_idx) |
| 2295 | continue; |
| 2296 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2297 | if (dev->towrite && |
| 2298 | (test_bit(R5_UPTODATE, &dev->flags) || |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2299 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2300 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2301 | set_bit(R5_LOCKED, &dev->flags); |
| 2302 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2303 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2304 | } |
| 2305 | } |
| 2306 | } |
| 2307 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2308 | /* keep the parity disk(s) locked while asynchronous operations |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2309 | * are in flight |
| 2310 | */ |
| 2311 | set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); |
| 2312 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2313 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2314 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2315 | if (level == 6) { |
| 2316 | int qd_idx = sh->qd_idx; |
| 2317 | struct r5dev *dev = &sh->dev[qd_idx]; |
| 2318 | |
| 2319 | set_bit(R5_LOCKED, &dev->flags); |
| 2320 | clear_bit(R5_UPTODATE, &dev->flags); |
| 2321 | s->locked++; |
| 2322 | } |
| 2323 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2324 | pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n", |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 2325 | __func__, (unsigned long long)sh->sector, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2326 | s->locked, s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2327 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2328 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2329 | /* |
| 2330 | * Each stripe/dev can have one or more bion attached. |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2331 | * toread/towrite point to the first in a chain. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2332 | * The bi_next chain must be in order. |
| 2333 | */ |
| 2334 | static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite) |
| 2335 | { |
| 2336 | struct bio **bip; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2337 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2338 | int firstwrite=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2339 | |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 2340 | pr_debug("adding bi b#%llu to stripe s#%llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2341 | (unsigned long long)bi->bi_sector, |
| 2342 | (unsigned long long)sh->sector); |
| 2343 | |
| 2344 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2345 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2346 | if (forwrite) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2347 | bip = &sh->dev[dd_idx].towrite; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2348 | if (*bip == NULL && sh->dev[dd_idx].written == NULL) |
| 2349 | firstwrite = 1; |
| 2350 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2351 | bip = &sh->dev[dd_idx].toread; |
| 2352 | while (*bip && (*bip)->bi_sector < bi->bi_sector) { |
| 2353 | if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector) |
| 2354 | goto overlap; |
| 2355 | bip = & (*bip)->bi_next; |
| 2356 | } |
| 2357 | if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9)) |
| 2358 | goto overlap; |
| 2359 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 2360 | BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2361 | if (*bip) |
| 2362 | bi->bi_next = *bip; |
| 2363 | *bip = bi; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2364 | bi->bi_phys_segments++; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2366 | if (forwrite) { |
| 2367 | /* check if page is covered */ |
| 2368 | sector_t sector = sh->dev[dd_idx].sector; |
| 2369 | for (bi=sh->dev[dd_idx].towrite; |
| 2370 | sector < sh->dev[dd_idx].sector + STRIPE_SECTORS && |
| 2371 | bi && bi->bi_sector <= sector; |
| 2372 | bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) { |
| 2373 | if (bi->bi_sector + (bi->bi_size>>9) >= sector) |
| 2374 | sector = bi->bi_sector + (bi->bi_size>>9); |
| 2375 | } |
| 2376 | if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS) |
| 2377 | set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags); |
| 2378 | } |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 2379 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 2380 | |
| 2381 | pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n", |
| 2382 | (unsigned long long)(*bip)->bi_sector, |
| 2383 | (unsigned long long)sh->sector, dd_idx); |
| 2384 | |
| 2385 | if (conf->mddev->bitmap && firstwrite) { |
| 2386 | bitmap_startwrite(conf->mddev->bitmap, sh->sector, |
| 2387 | STRIPE_SECTORS, 0); |
| 2388 | sh->bm_seq = conf->seq_flush+1; |
| 2389 | set_bit(STRIPE_BIT_DELAY, &sh->state); |
| 2390 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2391 | return 1; |
| 2392 | |
| 2393 | overlap: |
| 2394 | set_bit(R5_Overlap, &sh->dev[dd_idx].flags); |
| 2395 | spin_unlock_irq(&conf->device_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2396 | return 0; |
| 2397 | } |
| 2398 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2399 | static void end_reshape(struct r5conf *conf); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 2400 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2401 | static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2402 | struct stripe_head *sh) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2403 | { |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2404 | int sectors_per_chunk = |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 2405 | previous ? conf->prev_chunk_sectors : conf->chunk_sectors; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2406 | int dd_idx; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 2407 | int chunk_offset = sector_div(stripe, sectors_per_chunk); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2408 | int disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 2409 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2410 | raid5_compute_sector(conf, |
| 2411 | stripe * (disks - conf->max_degraded) |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 2412 | *sectors_per_chunk + chunk_offset, |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2413 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2414 | &dd_idx, sh); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2415 | } |
| 2416 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2417 | static void |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2418 | handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2419 | struct stripe_head_state *s, int disks, |
| 2420 | struct bio **return_bi) |
| 2421 | { |
| 2422 | int i; |
| 2423 | for (i = disks; i--; ) { |
| 2424 | struct bio *bi; |
| 2425 | int bitmap_end = 0; |
| 2426 | |
| 2427 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2428 | struct md_rdev *rdev; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2429 | rcu_read_lock(); |
| 2430 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 2431 | if (rdev && test_bit(In_sync, &rdev->flags)) |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2432 | atomic_inc(&rdev->nr_pending); |
| 2433 | else |
| 2434 | rdev = NULL; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2435 | rcu_read_unlock(); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2436 | if (rdev) { |
| 2437 | if (!rdev_set_badblocks( |
| 2438 | rdev, |
| 2439 | sh->sector, |
| 2440 | STRIPE_SECTORS, 0)) |
| 2441 | md_error(conf->mddev, rdev); |
| 2442 | rdev_dec_pending(rdev, conf->mddev); |
| 2443 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2444 | } |
| 2445 | spin_lock_irq(&conf->device_lock); |
| 2446 | /* fail all writes first */ |
| 2447 | bi = sh->dev[i].towrite; |
| 2448 | sh->dev[i].towrite = NULL; |
| 2449 | if (bi) { |
| 2450 | s->to_write--; |
| 2451 | bitmap_end = 1; |
| 2452 | } |
| 2453 | |
| 2454 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 2455 | wake_up(&conf->wait_for_overlap); |
| 2456 | |
| 2457 | while (bi && bi->bi_sector < |
| 2458 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2459 | struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); |
| 2460 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2461 | if (!raid5_dec_bi_phys_segments(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2462 | md_write_end(conf->mddev); |
| 2463 | bi->bi_next = *return_bi; |
| 2464 | *return_bi = bi; |
| 2465 | } |
| 2466 | bi = nextbi; |
| 2467 | } |
| 2468 | /* and fail all 'written' */ |
| 2469 | bi = sh->dev[i].written; |
| 2470 | sh->dev[i].written = NULL; |
| 2471 | if (bi) bitmap_end = 1; |
| 2472 | while (bi && bi->bi_sector < |
| 2473 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2474 | struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector); |
| 2475 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2476 | if (!raid5_dec_bi_phys_segments(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2477 | md_write_end(conf->mddev); |
| 2478 | bi->bi_next = *return_bi; |
| 2479 | *return_bi = bi; |
| 2480 | } |
| 2481 | bi = bi2; |
| 2482 | } |
| 2483 | |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2484 | /* fail any reads if this device is non-operational and |
| 2485 | * the data has not reached the cache yet. |
| 2486 | */ |
| 2487 | if (!test_bit(R5_Wantfill, &sh->dev[i].flags) && |
| 2488 | (!test_bit(R5_Insync, &sh->dev[i].flags) || |
| 2489 | test_bit(R5_ReadError, &sh->dev[i].flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2490 | bi = sh->dev[i].toread; |
| 2491 | sh->dev[i].toread = NULL; |
| 2492 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 2493 | wake_up(&conf->wait_for_overlap); |
| 2494 | if (bi) s->to_read--; |
| 2495 | while (bi && bi->bi_sector < |
| 2496 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2497 | struct bio *nextbi = |
| 2498 | r5_next_bio(bi, sh->dev[i].sector); |
| 2499 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2500 | if (!raid5_dec_bi_phys_segments(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2501 | bi->bi_next = *return_bi; |
| 2502 | *return_bi = bi; |
| 2503 | } |
| 2504 | bi = nextbi; |
| 2505 | } |
| 2506 | } |
| 2507 | spin_unlock_irq(&conf->device_lock); |
| 2508 | if (bitmap_end) |
| 2509 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 2510 | STRIPE_SECTORS, 0, 0); |
NeilBrown | 8cfa7b0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2511 | /* If we were in the middle of a write the parity block might |
| 2512 | * still be locked - so just clear all R5_LOCKED flags |
| 2513 | */ |
| 2514 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2515 | } |
| 2516 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2517 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 2518 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 2519 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2520 | } |
| 2521 | |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2522 | static void |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2523 | handle_failed_sync(struct r5conf *conf, struct stripe_head *sh, |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2524 | struct stripe_head_state *s) |
| 2525 | { |
| 2526 | int abort = 0; |
| 2527 | int i; |
| 2528 | |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2529 | clear_bit(STRIPE_SYNCING, &sh->state); |
| 2530 | s->syncing = 0; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2531 | s->replacing = 0; |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2532 | /* There is nothing more to do for sync/check/repair. |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 2533 | * Don't even need to abort as that is handled elsewhere |
| 2534 | * if needed, and not always wanted e.g. if there is a known |
| 2535 | * bad block here. |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2536 | * For recover/replace we need to record a bad block on all |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2537 | * non-sync devices, or abort the recovery |
| 2538 | */ |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 2539 | if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) { |
| 2540 | /* During recovery devices cannot be removed, so |
| 2541 | * locking and refcounting of rdevs is not needed |
| 2542 | */ |
| 2543 | for (i = 0; i < conf->raid_disks; i++) { |
| 2544 | struct md_rdev *rdev = conf->disks[i].rdev; |
| 2545 | if (rdev |
| 2546 | && !test_bit(Faulty, &rdev->flags) |
| 2547 | && !test_bit(In_sync, &rdev->flags) |
| 2548 | && !rdev_set_badblocks(rdev, sh->sector, |
| 2549 | STRIPE_SECTORS, 0)) |
| 2550 | abort = 1; |
| 2551 | rdev = conf->disks[i].replacement; |
| 2552 | if (rdev |
| 2553 | && !test_bit(Faulty, &rdev->flags) |
| 2554 | && !test_bit(In_sync, &rdev->flags) |
| 2555 | && !rdev_set_badblocks(rdev, sh->sector, |
| 2556 | STRIPE_SECTORS, 0)) |
| 2557 | abort = 1; |
| 2558 | } |
| 2559 | if (abort) |
| 2560 | conf->recovery_disabled = |
| 2561 | conf->mddev->recovery_disabled; |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2562 | } |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 2563 | md_done_sync(conf->mddev, STRIPE_SECTORS, !abort); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2564 | } |
| 2565 | |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2566 | static int want_replace(struct stripe_head *sh, int disk_idx) |
| 2567 | { |
| 2568 | struct md_rdev *rdev; |
| 2569 | int rv = 0; |
| 2570 | /* Doing recovery so rcu locking not required */ |
| 2571 | rdev = sh->raid_conf->disks[disk_idx].replacement; |
| 2572 | if (rdev |
| 2573 | && !test_bit(Faulty, &rdev->flags) |
| 2574 | && !test_bit(In_sync, &rdev->flags) |
| 2575 | && (rdev->recovery_offset <= sh->sector |
| 2576 | || rdev->mddev->recovery_cp <= sh->sector)) |
| 2577 | rv = 1; |
| 2578 | |
| 2579 | return rv; |
| 2580 | } |
| 2581 | |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2582 | /* 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] | 2583 | * to be read or computed to satisfy a request. |
| 2584 | * |
| 2585 | * Returns 1 when no more member devices need to be checked, otherwise returns |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2586 | * 0 to tell the loop in handle_stripe_fill to continue |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2587 | */ |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2588 | static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s, |
| 2589 | int disk_idx, int disks) |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2590 | { |
| 2591 | struct r5dev *dev = &sh->dev[disk_idx]; |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2592 | struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]], |
| 2593 | &sh->dev[s->failed_num[1]] }; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2594 | |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2595 | /* is the data in this block needed, and can we get it? */ |
| 2596 | if (!test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2597 | !test_bit(R5_UPTODATE, &dev->flags) && |
| 2598 | (dev->toread || |
| 2599 | (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) || |
| 2600 | s->syncing || s->expanding || |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2601 | (s->replacing && want_replace(sh, disk_idx)) || |
NeilBrown | 5d35e09 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2602 | (s->failed >= 1 && fdev[0]->toread) || |
| 2603 | (s->failed >= 2 && fdev[1]->toread) || |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2604 | (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite && |
| 2605 | !test_bit(R5_OVERWRITE, &fdev[0]->flags)) || |
| 2606 | (sh->raid_conf->level == 6 && s->failed && s->to_write))) { |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2607 | /* we would like to get this block, possibly by computing it, |
| 2608 | * otherwise read it if the backing disk is insync |
| 2609 | */ |
| 2610 | BUG_ON(test_bit(R5_Wantcompute, &dev->flags)); |
| 2611 | BUG_ON(test_bit(R5_Wantread, &dev->flags)); |
| 2612 | if ((s->uptodate == disks - 1) && |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2613 | (s->failed && (disk_idx == s->failed_num[0] || |
| 2614 | disk_idx == s->failed_num[1]))) { |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2615 | /* have disk failed, and we're requested to fetch it; |
| 2616 | * do compute it |
| 2617 | */ |
| 2618 | pr_debug("Computing stripe %llu block %d\n", |
| 2619 | (unsigned long long)sh->sector, disk_idx); |
| 2620 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2621 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2622 | set_bit(R5_Wantcompute, &dev->flags); |
| 2623 | sh->ops.target = disk_idx; |
| 2624 | sh->ops.target2 = -1; /* no 2nd target */ |
| 2625 | s->req_compute = 1; |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2626 | /* Careful: from this point on 'uptodate' is in the eye |
| 2627 | * of raid_run_ops which services 'compute' operations |
| 2628 | * before writes. R5_Wantcompute flags a block that will |
| 2629 | * be R5_UPTODATE by the time it is needed for a |
| 2630 | * subsequent operation. |
| 2631 | */ |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2632 | s->uptodate++; |
| 2633 | return 1; |
| 2634 | } else if (s->uptodate == disks-2 && s->failed >= 2) { |
| 2635 | /* Computing 2-failure is *very* expensive; only |
| 2636 | * do it if failed >= 2 |
| 2637 | */ |
| 2638 | int other; |
| 2639 | for (other = disks; other--; ) { |
| 2640 | if (other == disk_idx) |
| 2641 | continue; |
| 2642 | if (!test_bit(R5_UPTODATE, |
| 2643 | &sh->dev[other].flags)) |
| 2644 | break; |
| 2645 | } |
| 2646 | BUG_ON(other < 0); |
| 2647 | pr_debug("Computing stripe %llu blocks %d,%d\n", |
| 2648 | (unsigned long long)sh->sector, |
| 2649 | disk_idx, other); |
| 2650 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2651 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2652 | set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags); |
| 2653 | set_bit(R5_Wantcompute, &sh->dev[other].flags); |
| 2654 | sh->ops.target = disk_idx; |
| 2655 | sh->ops.target2 = other; |
| 2656 | s->uptodate += 2; |
| 2657 | s->req_compute = 1; |
| 2658 | return 1; |
| 2659 | } else if (test_bit(R5_Insync, &dev->flags)) { |
| 2660 | set_bit(R5_LOCKED, &dev->flags); |
| 2661 | set_bit(R5_Wantread, &dev->flags); |
| 2662 | s->locked++; |
| 2663 | pr_debug("Reading block %d (sync=%d)\n", |
| 2664 | disk_idx, s->syncing); |
| 2665 | } |
| 2666 | } |
| 2667 | |
| 2668 | return 0; |
| 2669 | } |
| 2670 | |
| 2671 | /** |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2672 | * handle_stripe_fill - read or compute data to satisfy pending requests. |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2673 | */ |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2674 | static void handle_stripe_fill(struct stripe_head *sh, |
| 2675 | struct stripe_head_state *s, |
| 2676 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2677 | { |
| 2678 | int i; |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2679 | |
| 2680 | /* look for blocks to read/compute, skip this if a compute |
| 2681 | * is already in flight, or if the stripe contents are in the |
| 2682 | * midst of changing due to a write |
| 2683 | */ |
| 2684 | if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state && |
| 2685 | !sh->reconstruct_state) |
| 2686 | for (i = disks; i--; ) |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2687 | if (fetch_block(sh, s, i, disks)) |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2688 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2689 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2690 | } |
| 2691 | |
| 2692 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2693 | /* handle_stripe_clean_event |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2694 | * any written block on an uptodate or failed drive can be returned. |
| 2695 | * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but |
| 2696 | * never LOCKED, so we don't need to test 'failed' directly. |
| 2697 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2698 | static void handle_stripe_clean_event(struct r5conf *conf, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2699 | struct stripe_head *sh, int disks, struct bio **return_bi) |
| 2700 | { |
| 2701 | int i; |
| 2702 | struct r5dev *dev; |
| 2703 | |
| 2704 | for (i = disks; i--; ) |
| 2705 | if (sh->dev[i].written) { |
| 2706 | dev = &sh->dev[i]; |
| 2707 | if (!test_bit(R5_LOCKED, &dev->flags) && |
| 2708 | test_bit(R5_UPTODATE, &dev->flags)) { |
| 2709 | /* We can return any write requests */ |
| 2710 | struct bio *wbi, *wbi2; |
| 2711 | int bitmap_end = 0; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2712 | pr_debug("Return write for disc %d\n", i); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2713 | spin_lock_irq(&conf->device_lock); |
| 2714 | wbi = dev->written; |
| 2715 | dev->written = NULL; |
| 2716 | while (wbi && wbi->bi_sector < |
| 2717 | dev->sector + STRIPE_SECTORS) { |
| 2718 | wbi2 = r5_next_bio(wbi, dev->sector); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2719 | if (!raid5_dec_bi_phys_segments(wbi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2720 | md_write_end(conf->mddev); |
| 2721 | wbi->bi_next = *return_bi; |
| 2722 | *return_bi = wbi; |
| 2723 | } |
| 2724 | wbi = wbi2; |
| 2725 | } |
| 2726 | if (dev->towrite == NULL) |
| 2727 | bitmap_end = 1; |
| 2728 | spin_unlock_irq(&conf->device_lock); |
| 2729 | if (bitmap_end) |
| 2730 | bitmap_endwrite(conf->mddev->bitmap, |
| 2731 | sh->sector, |
| 2732 | STRIPE_SECTORS, |
| 2733 | !test_bit(STRIPE_DEGRADED, &sh->state), |
| 2734 | 0); |
| 2735 | } |
| 2736 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2737 | |
| 2738 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 2739 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 2740 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2741 | } |
| 2742 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2743 | static void handle_stripe_dirtying(struct r5conf *conf, |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2744 | struct stripe_head *sh, |
| 2745 | struct stripe_head_state *s, |
| 2746 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2747 | { |
| 2748 | int rmw = 0, rcw = 0, i; |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2749 | if (conf->max_degraded == 2) { |
| 2750 | /* RAID6 requires 'rcw' in current implementation |
| 2751 | * Calculate the real rcw later - for now fake it |
| 2752 | * look like rcw is cheaper |
| 2753 | */ |
| 2754 | rcw = 1; rmw = 2; |
| 2755 | } else for (i = disks; i--; ) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2756 | /* would I have to read this buffer for read_modify_write */ |
| 2757 | struct r5dev *dev = &sh->dev[i]; |
| 2758 | if ((dev->towrite || i == sh->pd_idx) && |
| 2759 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2760 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2761 | test_bit(R5_Wantcompute, &dev->flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2762 | if (test_bit(R5_Insync, &dev->flags)) |
| 2763 | rmw++; |
| 2764 | else |
| 2765 | rmw += 2*disks; /* cannot read it */ |
| 2766 | } |
| 2767 | /* Would I have to read this buffer for reconstruct_write */ |
| 2768 | if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx && |
| 2769 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2770 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2771 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2772 | if (test_bit(R5_Insync, &dev->flags)) rcw++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2773 | else |
| 2774 | rcw += 2*disks; |
| 2775 | } |
| 2776 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2777 | pr_debug("for sector %llu, rmw=%d rcw=%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2778 | (unsigned long long)sh->sector, rmw, rcw); |
| 2779 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2780 | if (rmw < rcw && rmw > 0) |
| 2781 | /* prefer read-modify-write, but need to get some data */ |
| 2782 | for (i = disks; i--; ) { |
| 2783 | struct r5dev *dev = &sh->dev[i]; |
| 2784 | if ((dev->towrite || i == sh->pd_idx) && |
| 2785 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2786 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2787 | test_bit(R5_Wantcompute, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2788 | test_bit(R5_Insync, &dev->flags)) { |
| 2789 | if ( |
| 2790 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2791 | pr_debug("Read_old block " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2792 | "%d for r-m-w\n", i); |
| 2793 | set_bit(R5_LOCKED, &dev->flags); |
| 2794 | set_bit(R5_Wantread, &dev->flags); |
| 2795 | s->locked++; |
| 2796 | } else { |
| 2797 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2798 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2799 | } |
| 2800 | } |
| 2801 | } |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2802 | if (rcw <= rmw && rcw > 0) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2803 | /* want reconstruct write, but need to get some data */ |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2804 | rcw = 0; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2805 | for (i = disks; i--; ) { |
| 2806 | struct r5dev *dev = &sh->dev[i]; |
| 2807 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2808 | i != sh->pd_idx && i != sh->qd_idx && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2809 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2810 | !(test_bit(R5_UPTODATE, &dev->flags) || |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2811 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2812 | rcw++; |
| 2813 | if (!test_bit(R5_Insync, &dev->flags)) |
| 2814 | continue; /* it's a failed drive */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2815 | if ( |
| 2816 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2817 | pr_debug("Read_old block " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2818 | "%d for Reconstruct\n", i); |
| 2819 | set_bit(R5_LOCKED, &dev->flags); |
| 2820 | set_bit(R5_Wantread, &dev->flags); |
| 2821 | s->locked++; |
| 2822 | } else { |
| 2823 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2824 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2825 | } |
| 2826 | } |
| 2827 | } |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2828 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2829 | /* now if nothing is locked, and if we have enough data, |
| 2830 | * we can start a write request |
| 2831 | */ |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2832 | /* since handle_stripe can be called at any time we need to handle the |
| 2833 | * case where a compute block operation has been submitted and then a |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2834 | * subsequent call wants to start a write request. raid_run_ops only |
| 2835 | * handles the case where compute block and reconstruct are requested |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2836 | * simultaneously. If this is not the case then new writes need to be |
| 2837 | * held off until the compute completes. |
| 2838 | */ |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2839 | if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) && |
| 2840 | (s->locked == 0 && (rcw == 0 || rmw == 0) && |
| 2841 | !test_bit(STRIPE_BIT_DELAY, &sh->state))) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2842 | schedule_reconstruction(sh, s, rcw == 0, 0); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2843 | } |
| 2844 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2845 | static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2846 | struct stripe_head_state *s, int disks) |
| 2847 | { |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2848 | struct r5dev *dev = NULL; |
Dan Williams | e89f896 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2849 | |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2850 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2851 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2852 | switch (sh->check_state) { |
| 2853 | case check_state_idle: |
| 2854 | /* start a new check operation if there are no failures */ |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2855 | if (s->failed == 0) { |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2856 | BUG_ON(s->uptodate != disks); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2857 | sh->check_state = check_state_run; |
| 2858 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2859 | clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2860 | s->uptodate--; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2861 | break; |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2862 | } |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2863 | dev = &sh->dev[s->failed_num[0]]; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2864 | /* fall through */ |
| 2865 | case check_state_compute_result: |
| 2866 | sh->check_state = check_state_idle; |
| 2867 | if (!dev) |
| 2868 | dev = &sh->dev[sh->pd_idx]; |
| 2869 | |
| 2870 | /* check that a write has not made the stripe insync */ |
| 2871 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 2872 | break; |
| 2873 | |
| 2874 | /* either failed parity check, or recovery is happening */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2875 | BUG_ON(!test_bit(R5_UPTODATE, &dev->flags)); |
| 2876 | BUG_ON(s->uptodate != disks); |
| 2877 | |
| 2878 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2879 | s->locked++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2880 | set_bit(R5_Wantwrite, &dev->flags); |
Dan Williams | 830ea01 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2881 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2882 | clear_bit(STRIPE_DEGRADED, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2883 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2884 | break; |
| 2885 | case check_state_run: |
| 2886 | break; /* we will be called again upon completion */ |
| 2887 | case check_state_check_result: |
| 2888 | sh->check_state = check_state_idle; |
| 2889 | |
| 2890 | /* if a failure occurred during the check operation, leave |
| 2891 | * STRIPE_INSYNC not set and let the stripe be handled again |
| 2892 | */ |
| 2893 | if (s->failed) |
| 2894 | break; |
| 2895 | |
| 2896 | /* handle a successful check operation, if parity is correct |
| 2897 | * we are done. Otherwise update the mismatch count and repair |
| 2898 | * parity if !MD_RECOVERY_CHECK |
| 2899 | */ |
Dan Williams | ad283ea | 2009-08-29 19:09:26 -0700 | [diff] [blame] | 2900 | if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0) |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2901 | /* parity is correct (on disc, |
| 2902 | * not in buffer any more) |
| 2903 | */ |
| 2904 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2905 | else { |
| 2906 | conf->mddev->resync_mismatches += STRIPE_SECTORS; |
| 2907 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 2908 | /* don't try to repair!! */ |
| 2909 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2910 | else { |
| 2911 | sh->check_state = check_state_compute_run; |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2912 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2913 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2914 | set_bit(R5_Wantcompute, |
| 2915 | &sh->dev[sh->pd_idx].flags); |
| 2916 | sh->ops.target = sh->pd_idx; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2917 | sh->ops.target2 = -1; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2918 | s->uptodate++; |
| 2919 | } |
| 2920 | } |
| 2921 | break; |
| 2922 | case check_state_compute_run: |
| 2923 | break; |
| 2924 | default: |
| 2925 | printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n", |
| 2926 | __func__, sh->check_state, |
| 2927 | (unsigned long long) sh->sector); |
| 2928 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2929 | } |
| 2930 | } |
| 2931 | |
| 2932 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2933 | static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2934 | struct stripe_head_state *s, |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2935 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2936 | { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2937 | int pd_idx = sh->pd_idx; |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 2938 | int qd_idx = sh->qd_idx; |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2939 | struct r5dev *dev; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2940 | |
| 2941 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2942 | |
| 2943 | BUG_ON(s->failed > 2); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2944 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2945 | /* Want to check and possibly repair P and Q. |
| 2946 | * However there could be one 'failed' device, in which |
| 2947 | * case we can only check one of them, possibly using the |
| 2948 | * other to generate missing data |
| 2949 | */ |
| 2950 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2951 | switch (sh->check_state) { |
| 2952 | case check_state_idle: |
| 2953 | /* start a new check operation if there are < 2 failures */ |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2954 | if (s->failed == s->q_failed) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2955 | /* The only possible failed device holds Q, so it |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2956 | * makes sense to check P (If anything else were failed, |
| 2957 | * we would have used P to recreate it). |
| 2958 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2959 | sh->check_state = check_state_run; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2960 | } |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2961 | if (!s->q_failed && s->failed < 2) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2962 | /* Q is not failed, and we didn't use it to generate |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2963 | * anything, so it makes sense to check it |
| 2964 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2965 | if (sh->check_state == check_state_run) |
| 2966 | sh->check_state = check_state_run_pq; |
| 2967 | else |
| 2968 | sh->check_state = check_state_run_q; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2969 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2970 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2971 | /* discard potentially stale zero_sum_result */ |
| 2972 | sh->ops.zero_sum_result = 0; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2973 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2974 | if (sh->check_state == check_state_run) { |
| 2975 | /* async_xor_zero_sum destroys the contents of P */ |
| 2976 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
| 2977 | s->uptodate--; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2978 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2979 | if (sh->check_state >= check_state_run && |
| 2980 | sh->check_state <= check_state_run_pq) { |
| 2981 | /* async_syndrome_zero_sum preserves P and Q, so |
| 2982 | * no need to mark them !uptodate here |
| 2983 | */ |
| 2984 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
| 2985 | break; |
| 2986 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2987 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2988 | /* we have 2-disk failure */ |
| 2989 | BUG_ON(s->failed != 2); |
| 2990 | /* fall through */ |
| 2991 | case check_state_compute_result: |
| 2992 | sh->check_state = check_state_idle; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2993 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2994 | /* check that a write has not made the stripe insync */ |
| 2995 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 2996 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2997 | |
| 2998 | /* now write out any block on a failed drive, |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2999 | * or P or Q if they were recomputed |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3000 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3001 | 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] | 3002 | if (s->failed == 2) { |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3003 | dev = &sh->dev[s->failed_num[1]]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3004 | s->locked++; |
| 3005 | set_bit(R5_LOCKED, &dev->flags); |
| 3006 | set_bit(R5_Wantwrite, &dev->flags); |
| 3007 | } |
| 3008 | if (s->failed >= 1) { |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3009 | dev = &sh->dev[s->failed_num[0]]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3010 | s->locked++; |
| 3011 | set_bit(R5_LOCKED, &dev->flags); |
| 3012 | set_bit(R5_Wantwrite, &dev->flags); |
| 3013 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3014 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3015 | dev = &sh->dev[pd_idx]; |
| 3016 | s->locked++; |
| 3017 | set_bit(R5_LOCKED, &dev->flags); |
| 3018 | set_bit(R5_Wantwrite, &dev->flags); |
| 3019 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3020 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3021 | dev = &sh->dev[qd_idx]; |
| 3022 | s->locked++; |
| 3023 | set_bit(R5_LOCKED, &dev->flags); |
| 3024 | set_bit(R5_Wantwrite, &dev->flags); |
| 3025 | } |
| 3026 | clear_bit(STRIPE_DEGRADED, &sh->state); |
| 3027 | |
| 3028 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3029 | break; |
| 3030 | case check_state_run: |
| 3031 | case check_state_run_q: |
| 3032 | case check_state_run_pq: |
| 3033 | break; /* we will be called again upon completion */ |
| 3034 | case check_state_check_result: |
| 3035 | sh->check_state = check_state_idle; |
| 3036 | |
| 3037 | /* handle a successful check operation, if parity is correct |
| 3038 | * we are done. Otherwise update the mismatch count and repair |
| 3039 | * parity if !MD_RECOVERY_CHECK |
| 3040 | */ |
| 3041 | if (sh->ops.zero_sum_result == 0) { |
| 3042 | /* both parities are correct */ |
| 3043 | if (!s->failed) |
| 3044 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3045 | else { |
| 3046 | /* in contrast to the raid5 case we can validate |
| 3047 | * parity, but still have a failure to write |
| 3048 | * back |
| 3049 | */ |
| 3050 | sh->check_state = check_state_compute_result; |
| 3051 | /* Returning at this point means that we may go |
| 3052 | * off and bring p and/or q uptodate again so |
| 3053 | * we make sure to check zero_sum_result again |
| 3054 | * to verify if p or q need writeback |
| 3055 | */ |
| 3056 | } |
| 3057 | } else { |
| 3058 | conf->mddev->resync_mismatches += STRIPE_SECTORS; |
| 3059 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 3060 | /* don't try to repair!! */ |
| 3061 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3062 | else { |
| 3063 | int *target = &sh->ops.target; |
| 3064 | |
| 3065 | sh->ops.target = -1; |
| 3066 | sh->ops.target2 = -1; |
| 3067 | sh->check_state = check_state_compute_run; |
| 3068 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 3069 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 3070 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
| 3071 | set_bit(R5_Wantcompute, |
| 3072 | &sh->dev[pd_idx].flags); |
| 3073 | *target = pd_idx; |
| 3074 | target = &sh->ops.target2; |
| 3075 | s->uptodate++; |
| 3076 | } |
| 3077 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
| 3078 | set_bit(R5_Wantcompute, |
| 3079 | &sh->dev[qd_idx].flags); |
| 3080 | *target = qd_idx; |
| 3081 | s->uptodate++; |
| 3082 | } |
| 3083 | } |
| 3084 | } |
| 3085 | break; |
| 3086 | case check_state_compute_run: |
| 3087 | break; |
| 3088 | default: |
| 3089 | printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n", |
| 3090 | __func__, sh->check_state, |
| 3091 | (unsigned long long) sh->sector); |
| 3092 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3093 | } |
| 3094 | } |
| 3095 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3096 | static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3097 | { |
| 3098 | int i; |
| 3099 | |
| 3100 | /* We have read all the blocks in this stripe and now we need to |
| 3101 | * copy some of them into a target stripe for expand. |
| 3102 | */ |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3103 | struct dma_async_tx_descriptor *tx = NULL; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3104 | clear_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 3105 | for (i = 0; i < sh->disks; i++) |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 3106 | if (i != sh->pd_idx && i != sh->qd_idx) { |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3107 | int dd_idx, j; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3108 | struct stripe_head *sh2; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 3109 | struct async_submit_ctl submit; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3110 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 3111 | sector_t bn = compute_blocknr(sh, i, 1); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3112 | sector_t s = raid5_compute_sector(conf, bn, 0, |
| 3113 | &dd_idx, NULL); |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 3114 | sh2 = get_active_stripe(conf, s, 0, 1, 1); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3115 | if (sh2 == NULL) |
| 3116 | /* so far only the early blocks of this stripe |
| 3117 | * have been requested. When later blocks |
| 3118 | * get requested, we will try again |
| 3119 | */ |
| 3120 | continue; |
| 3121 | if (!test_bit(STRIPE_EXPANDING, &sh2->state) || |
| 3122 | test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) { |
| 3123 | /* must have already done this block */ |
| 3124 | release_stripe(sh2); |
| 3125 | continue; |
| 3126 | } |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3127 | |
| 3128 | /* place all the copies on one channel */ |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 3129 | init_async_submit(&submit, 0, tx, NULL, NULL, NULL); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3130 | tx = async_memcpy(sh2->dev[dd_idx].page, |
Dan Williams | 88ba2aa | 2009-04-09 16:16:18 -0700 | [diff] [blame] | 3131 | sh->dev[i].page, 0, 0, STRIPE_SIZE, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 3132 | &submit); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3133 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3134 | set_bit(R5_Expanded, &sh2->dev[dd_idx].flags); |
| 3135 | set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags); |
| 3136 | for (j = 0; j < conf->raid_disks; j++) |
| 3137 | if (j != sh2->pd_idx && |
NeilBrown | 86c374b | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3138 | j != sh2->qd_idx && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3139 | !test_bit(R5_Expanded, &sh2->dev[j].flags)) |
| 3140 | break; |
| 3141 | if (j == conf->raid_disks) { |
| 3142 | set_bit(STRIPE_EXPAND_READY, &sh2->state); |
| 3143 | set_bit(STRIPE_HANDLE, &sh2->state); |
| 3144 | } |
| 3145 | release_stripe(sh2); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3146 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3147 | } |
NeilBrown | a2e0855 | 2007-09-11 15:23:36 -0700 | [diff] [blame] | 3148 | /* done submitting copies, wait for them to complete */ |
| 3149 | if (tx) { |
| 3150 | async_tx_ack(tx); |
| 3151 | dma_wait_for_async_tx(tx); |
| 3152 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3153 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3154 | |
| 3155 | /* |
| 3156 | * handle_stripe - do things to a stripe. |
| 3157 | * |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3158 | * We lock the stripe by setting STRIPE_ACTIVE and then examine the |
| 3159 | * state of various bits to see what needs to be done. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3160 | * Possible results: |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3161 | * return some read requests which now have data |
| 3162 | * return some write requests which are safely on storage |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3163 | * schedule a read on some buffers |
| 3164 | * schedule a write of some buffers |
| 3165 | * return confirmation of parity correctness |
| 3166 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3167 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3168 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3169 | static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3170 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3171 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3172 | int disks = sh->disks; |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3173 | struct r5dev *dev; |
| 3174 | int i; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3175 | int do_recovery = 0; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3176 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3177 | memset(s, 0, sizeof(*s)); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3178 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3179 | s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 3180 | s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state); |
| 3181 | s->failed_num[0] = -1; |
| 3182 | s->failed_num[1] = -1; |
| 3183 | |
| 3184 | /* Now to look around and see what can be done */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3185 | rcu_read_lock(); |
NeilBrown | c4c1663 | 2011-07-26 11:34:20 +1000 | [diff] [blame] | 3186 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3187 | for (i=disks; i--; ) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3188 | struct md_rdev *rdev; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3189 | sector_t first_bad; |
| 3190 | int bad_sectors; |
| 3191 | int is_bad = 0; |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3192 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3193 | dev = &sh->dev[i]; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3194 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3195 | 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] | 3196 | i, dev->flags, |
| 3197 | dev->toread, dev->towrite, dev->written); |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3198 | /* maybe we can reply to a read |
| 3199 | * |
| 3200 | * new wantfill requests are only permitted while |
| 3201 | * ops_complete_biofill is guaranteed to be inactive |
| 3202 | */ |
| 3203 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread && |
| 3204 | !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) |
| 3205 | set_bit(R5_Wantfill, &dev->flags); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3206 | |
| 3207 | /* now count some things */ |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3208 | if (test_bit(R5_LOCKED, &dev->flags)) |
| 3209 | s->locked++; |
| 3210 | if (test_bit(R5_UPTODATE, &dev->flags)) |
| 3211 | s->uptodate++; |
Dan Williams | 2d6e4ec | 2009-09-16 12:11:54 -0700 | [diff] [blame] | 3212 | if (test_bit(R5_Wantcompute, &dev->flags)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3213 | s->compute++; |
| 3214 | BUG_ON(s->compute > 2); |
Dan Williams | 2d6e4ec | 2009-09-16 12:11:54 -0700 | [diff] [blame] | 3215 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3216 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3217 | if (test_bit(R5_Wantfill, &dev->flags)) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3218 | s->to_fill++; |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3219 | else if (dev->toread) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3220 | s->to_read++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3221 | if (dev->towrite) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3222 | s->to_write++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3223 | if (!test_bit(R5_OVERWRITE, &dev->flags)) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3224 | s->non_overwrite++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3225 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3226 | if (dev->written) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3227 | s->written++; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3228 | /* Prefer to use the replacement for reads, but only |
| 3229 | * if it is recovered enough and has no bad blocks. |
| 3230 | */ |
| 3231 | rdev = rcu_dereference(conf->disks[i].replacement); |
| 3232 | if (rdev && !test_bit(Faulty, &rdev->flags) && |
| 3233 | rdev->recovery_offset >= sh->sector + STRIPE_SECTORS && |
| 3234 | !is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 3235 | &first_bad, &bad_sectors)) |
| 3236 | set_bit(R5_ReadRepl, &dev->flags); |
| 3237 | else { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3238 | if (rdev) |
| 3239 | set_bit(R5_NeedReplace, &dev->flags); |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3240 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 3241 | clear_bit(R5_ReadRepl, &dev->flags); |
| 3242 | } |
NeilBrown | 9283d8c | 2011-12-08 16:27:57 +1100 | [diff] [blame] | 3243 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 3244 | rdev = NULL; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3245 | if (rdev) { |
| 3246 | is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 3247 | &first_bad, &bad_sectors); |
| 3248 | if (s->blocked_rdev == NULL |
| 3249 | && (test_bit(Blocked, &rdev->flags) |
| 3250 | || is_bad < 0)) { |
| 3251 | if (is_bad < 0) |
| 3252 | set_bit(BlockedBadBlocks, |
| 3253 | &rdev->flags); |
| 3254 | s->blocked_rdev = rdev; |
| 3255 | atomic_inc(&rdev->nr_pending); |
| 3256 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3257 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3258 | clear_bit(R5_Insync, &dev->flags); |
| 3259 | if (!rdev) |
| 3260 | /* Not in-sync */; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3261 | else if (is_bad) { |
| 3262 | /* also not in-sync */ |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3263 | if (!test_bit(WriteErrorSeen, &rdev->flags) && |
| 3264 | test_bit(R5_UPTODATE, &dev->flags)) { |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3265 | /* treat as in-sync, but with a read error |
| 3266 | * which we can now try to correct |
| 3267 | */ |
| 3268 | set_bit(R5_Insync, &dev->flags); |
| 3269 | set_bit(R5_ReadError, &dev->flags); |
| 3270 | } |
| 3271 | } else if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3272 | set_bit(R5_Insync, &dev->flags); |
NeilBrown | 30d7a48 | 2011-12-23 09:57:00 +1100 | [diff] [blame] | 3273 | else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset) |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3274 | /* in sync if before recovery_offset */ |
NeilBrown | 30d7a48 | 2011-12-23 09:57:00 +1100 | [diff] [blame] | 3275 | set_bit(R5_Insync, &dev->flags); |
| 3276 | else if (test_bit(R5_UPTODATE, &dev->flags) && |
| 3277 | test_bit(R5_Expanded, &dev->flags)) |
| 3278 | /* If we've reshaped into here, we assume it is Insync. |
| 3279 | * We will shortly update recovery_offset to make |
| 3280 | * it official. |
| 3281 | */ |
| 3282 | set_bit(R5_Insync, &dev->flags); |
| 3283 | |
Adam Kwolek | 5d8c71f | 2011-12-09 14:26:11 +1100 | [diff] [blame] | 3284 | if (rdev && test_bit(R5_WriteError, &dev->flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3285 | /* This flag does not apply to '.replacement' |
| 3286 | * only to .rdev, so make sure to check that*/ |
| 3287 | struct md_rdev *rdev2 = rcu_dereference( |
| 3288 | conf->disks[i].rdev); |
| 3289 | if (rdev2 == rdev) |
| 3290 | clear_bit(R5_Insync, &dev->flags); |
| 3291 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3292 | s->handle_bad_blocks = 1; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3293 | atomic_inc(&rdev2->nr_pending); |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3294 | } else |
| 3295 | clear_bit(R5_WriteError, &dev->flags); |
| 3296 | } |
Adam Kwolek | 5d8c71f | 2011-12-09 14:26:11 +1100 | [diff] [blame] | 3297 | if (rdev && test_bit(R5_MadeGood, &dev->flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3298 | /* This flag does not apply to '.replacement' |
| 3299 | * only to .rdev, so make sure to check that*/ |
| 3300 | struct md_rdev *rdev2 = rcu_dereference( |
| 3301 | conf->disks[i].rdev); |
| 3302 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 3303 | s->handle_bad_blocks = 1; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3304 | atomic_inc(&rdev2->nr_pending); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 3305 | } else |
| 3306 | clear_bit(R5_MadeGood, &dev->flags); |
| 3307 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3308 | if (test_bit(R5_MadeGoodRepl, &dev->flags)) { |
| 3309 | struct md_rdev *rdev2 = rcu_dereference( |
| 3310 | conf->disks[i].replacement); |
| 3311 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
| 3312 | s->handle_bad_blocks = 1; |
| 3313 | atomic_inc(&rdev2->nr_pending); |
| 3314 | } else |
| 3315 | clear_bit(R5_MadeGoodRepl, &dev->flags); |
| 3316 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3317 | if (!test_bit(R5_Insync, &dev->flags)) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3318 | /* The ReadError flag will just be confusing now */ |
| 3319 | clear_bit(R5_ReadError, &dev->flags); |
| 3320 | clear_bit(R5_ReWrite, &dev->flags); |
| 3321 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3322 | if (test_bit(R5_ReadError, &dev->flags)) |
| 3323 | clear_bit(R5_Insync, &dev->flags); |
| 3324 | if (!test_bit(R5_Insync, &dev->flags)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3325 | if (s->failed < 2) |
| 3326 | s->failed_num[s->failed] = i; |
| 3327 | s->failed++; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3328 | if (rdev && !test_bit(Faulty, &rdev->flags)) |
| 3329 | do_recovery = 1; |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3330 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3331 | } |
NeilBrown | c4c1663 | 2011-07-26 11:34:20 +1000 | [diff] [blame] | 3332 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3333 | if (test_bit(STRIPE_SYNCING, &sh->state)) { |
| 3334 | /* If there is a failed device being replaced, |
| 3335 | * we must be recovering. |
| 3336 | * else if we are after recovery_cp, we must be syncing |
majianpeng | c6d2e08 | 2012-04-02 01:16:59 +1000 | [diff] [blame] | 3337 | * else if MD_RECOVERY_REQUESTED is set, we also are syncing. |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3338 | * else we can only be replacing |
| 3339 | * sync and recovery both need to read all devices, and so |
| 3340 | * use the same flag. |
| 3341 | */ |
| 3342 | if (do_recovery || |
majianpeng | c6d2e08 | 2012-04-02 01:16:59 +1000 | [diff] [blame] | 3343 | sh->sector >= conf->mddev->recovery_cp || |
| 3344 | test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery))) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3345 | s->syncing = 1; |
| 3346 | else |
| 3347 | s->replacing = 1; |
| 3348 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3349 | rcu_read_unlock(); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3350 | } |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3351 | |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3352 | static void handle_stripe(struct stripe_head *sh) |
| 3353 | { |
| 3354 | struct stripe_head_state s; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3355 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3356 | int i; |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3357 | int prexor; |
| 3358 | int disks = sh->disks; |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3359 | struct r5dev *pdev, *qdev; |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3360 | |
| 3361 | clear_bit(STRIPE_HANDLE, &sh->state); |
Dan Williams | 257a4b4 | 2011-11-08 16:22:06 +1100 | [diff] [blame] | 3362 | if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3363 | /* already being handled, ensure it gets handled |
| 3364 | * again when current action finishes */ |
| 3365 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3366 | return; |
| 3367 | } |
| 3368 | |
| 3369 | if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) { |
| 3370 | set_bit(STRIPE_SYNCING, &sh->state); |
| 3371 | clear_bit(STRIPE_INSYNC, &sh->state); |
| 3372 | } |
| 3373 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 3374 | |
| 3375 | pr_debug("handling stripe %llu, state=%#lx cnt=%d, " |
| 3376 | "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n", |
| 3377 | (unsigned long long)sh->sector, sh->state, |
| 3378 | atomic_read(&sh->count), sh->pd_idx, sh->qd_idx, |
| 3379 | sh->check_state, sh->reconstruct_state); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3380 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3381 | analyse_stripe(sh, &s); |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3382 | |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3383 | if (s.handle_bad_blocks) { |
| 3384 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3385 | goto finish; |
| 3386 | } |
| 3387 | |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3388 | if (unlikely(s.blocked_rdev)) { |
| 3389 | if (s.syncing || s.expanding || s.expanded || |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3390 | s.replacing || s.to_write || s.written) { |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3391 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3392 | goto finish; |
| 3393 | } |
| 3394 | /* There is nothing for the blocked_rdev to block */ |
| 3395 | rdev_dec_pending(s.blocked_rdev, conf->mddev); |
| 3396 | s.blocked_rdev = NULL; |
| 3397 | } |
| 3398 | |
| 3399 | if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) { |
| 3400 | set_bit(STRIPE_OP_BIOFILL, &s.ops_request); |
| 3401 | set_bit(STRIPE_BIOFILL_RUN, &sh->state); |
| 3402 | } |
| 3403 | |
| 3404 | pr_debug("locked=%d uptodate=%d to_read=%d" |
| 3405 | " to_write=%d failed=%d failed_num=%d,%d\n", |
| 3406 | s.locked, s.uptodate, s.to_read, s.to_write, s.failed, |
| 3407 | s.failed_num[0], s.failed_num[1]); |
| 3408 | /* check if the array has lost more than max_degraded devices and, |
| 3409 | * if so, some requests might need to be failed. |
| 3410 | */ |
NeilBrown | 9a3f530 | 2011-11-08 16:22:01 +1100 | [diff] [blame] | 3411 | if (s.failed > conf->max_degraded) { |
| 3412 | sh->check_state = 0; |
| 3413 | sh->reconstruct_state = 0; |
| 3414 | if (s.to_read+s.to_write+s.written) |
| 3415 | handle_failed_stripe(conf, sh, &s, disks, &s.return_bi); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3416 | if (s.syncing + s.replacing) |
NeilBrown | 9a3f530 | 2011-11-08 16:22:01 +1100 | [diff] [blame] | 3417 | handle_failed_sync(conf, sh, &s); |
| 3418 | } |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3419 | |
| 3420 | /* |
| 3421 | * might be able to return some write requests if the parity blocks |
| 3422 | * are safe, or on a failed drive |
| 3423 | */ |
| 3424 | pdev = &sh->dev[sh->pd_idx]; |
| 3425 | s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx) |
| 3426 | || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx); |
| 3427 | qdev = &sh->dev[sh->qd_idx]; |
| 3428 | s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx) |
| 3429 | || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx) |
| 3430 | || conf->level < 6; |
| 3431 | |
| 3432 | if (s.written && |
| 3433 | (s.p_failed || ((test_bit(R5_Insync, &pdev->flags) |
| 3434 | && !test_bit(R5_LOCKED, &pdev->flags) |
| 3435 | && test_bit(R5_UPTODATE, &pdev->flags)))) && |
| 3436 | (s.q_failed || ((test_bit(R5_Insync, &qdev->flags) |
| 3437 | && !test_bit(R5_LOCKED, &qdev->flags) |
| 3438 | && test_bit(R5_UPTODATE, &qdev->flags))))) |
| 3439 | handle_stripe_clean_event(conf, sh, disks, &s.return_bi); |
| 3440 | |
| 3441 | /* Now we might consider reading some blocks, either to check/generate |
| 3442 | * parity, or to satisfy requests |
| 3443 | * or to load a block that is being partially written. |
| 3444 | */ |
| 3445 | if (s.to_read || s.non_overwrite |
| 3446 | || (conf->level == 6 && s.to_write && s.failed) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3447 | || (s.syncing && (s.uptodate + s.compute < disks)) |
| 3448 | || s.replacing |
| 3449 | || s.expanding) |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3450 | handle_stripe_fill(sh, &s, disks); |
| 3451 | |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3452 | /* Now we check to see if any write operations have recently |
| 3453 | * completed |
| 3454 | */ |
| 3455 | prexor = 0; |
| 3456 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_result) |
| 3457 | prexor = 1; |
| 3458 | if (sh->reconstruct_state == reconstruct_state_drain_result || |
| 3459 | sh->reconstruct_state == reconstruct_state_prexor_drain_result) { |
| 3460 | sh->reconstruct_state = reconstruct_state_idle; |
| 3461 | |
| 3462 | /* All the 'written' buffers and the parity block are ready to |
| 3463 | * be written back to disk |
| 3464 | */ |
| 3465 | BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags)); |
| 3466 | BUG_ON(sh->qd_idx >= 0 && |
| 3467 | !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags)); |
| 3468 | for (i = disks; i--; ) { |
| 3469 | struct r5dev *dev = &sh->dev[i]; |
| 3470 | if (test_bit(R5_LOCKED, &dev->flags) && |
| 3471 | (i == sh->pd_idx || i == sh->qd_idx || |
| 3472 | dev->written)) { |
| 3473 | pr_debug("Writing block %d\n", i); |
| 3474 | set_bit(R5_Wantwrite, &dev->flags); |
| 3475 | if (prexor) |
| 3476 | continue; |
| 3477 | if (!test_bit(R5_Insync, &dev->flags) || |
| 3478 | ((i == sh->pd_idx || i == sh->qd_idx) && |
| 3479 | s.failed == 0)) |
| 3480 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3481 | } |
| 3482 | } |
| 3483 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 3484 | s.dec_preread_active = 1; |
| 3485 | } |
| 3486 | |
| 3487 | /* Now to consider new write requests and what else, if anything |
| 3488 | * should be read. We do not handle new writes when: |
| 3489 | * 1/ A 'write' operation (copy+xor) is already in flight. |
| 3490 | * 2/ A 'check' operation is in flight, as it may clobber the parity |
| 3491 | * block. |
| 3492 | */ |
| 3493 | if (s.to_write && !sh->reconstruct_state && !sh->check_state) |
| 3494 | handle_stripe_dirtying(conf, sh, &s, disks); |
| 3495 | |
| 3496 | /* maybe we need to check and possibly fix the parity for this stripe |
| 3497 | * Any reads will already have been scheduled, so we just see if enough |
| 3498 | * data is available. The parity check is held off while parity |
| 3499 | * dependent operations are in flight. |
| 3500 | */ |
| 3501 | if (sh->check_state || |
| 3502 | (s.syncing && s.locked == 0 && |
| 3503 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && |
| 3504 | !test_bit(STRIPE_INSYNC, &sh->state))) { |
| 3505 | if (conf->level == 6) |
| 3506 | handle_parity_checks6(conf, sh, &s, disks); |
| 3507 | else |
| 3508 | handle_parity_checks5(conf, sh, &s, disks); |
| 3509 | } |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3510 | |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3511 | if (s.replacing && s.locked == 0 |
| 3512 | && !test_bit(STRIPE_INSYNC, &sh->state)) { |
| 3513 | /* Write out to replacement devices where possible */ |
| 3514 | for (i = 0; i < conf->raid_disks; i++) |
| 3515 | if (test_bit(R5_UPTODATE, &sh->dev[i].flags) && |
| 3516 | test_bit(R5_NeedReplace, &sh->dev[i].flags)) { |
| 3517 | set_bit(R5_WantReplace, &sh->dev[i].flags); |
| 3518 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 3519 | s.locked++; |
| 3520 | } |
| 3521 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3522 | } |
| 3523 | if ((s.syncing || s.replacing) && s.locked == 0 && |
| 3524 | test_bit(STRIPE_INSYNC, &sh->state)) { |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3525 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 3526 | clear_bit(STRIPE_SYNCING, &sh->state); |
| 3527 | } |
| 3528 | |
| 3529 | /* If the failed drives are just a ReadError, then we might need |
| 3530 | * to progress the repair/check process |
| 3531 | */ |
| 3532 | if (s.failed <= conf->max_degraded && !conf->mddev->ro) |
| 3533 | for (i = 0; i < s.failed; i++) { |
| 3534 | struct r5dev *dev = &sh->dev[s.failed_num[i]]; |
| 3535 | if (test_bit(R5_ReadError, &dev->flags) |
| 3536 | && !test_bit(R5_LOCKED, &dev->flags) |
| 3537 | && test_bit(R5_UPTODATE, &dev->flags) |
| 3538 | ) { |
| 3539 | if (!test_bit(R5_ReWrite, &dev->flags)) { |
| 3540 | set_bit(R5_Wantwrite, &dev->flags); |
| 3541 | set_bit(R5_ReWrite, &dev->flags); |
| 3542 | set_bit(R5_LOCKED, &dev->flags); |
| 3543 | s.locked++; |
| 3544 | } else { |
| 3545 | /* let's read it back */ |
| 3546 | set_bit(R5_Wantread, &dev->flags); |
| 3547 | set_bit(R5_LOCKED, &dev->flags); |
| 3548 | s.locked++; |
| 3549 | } |
| 3550 | } |
| 3551 | } |
| 3552 | |
| 3553 | |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3554 | /* Finish reconstruct operations initiated by the expansion process */ |
| 3555 | if (sh->reconstruct_state == reconstruct_state_result) { |
| 3556 | struct stripe_head *sh_src |
| 3557 | = get_active_stripe(conf, sh->sector, 1, 1, 1); |
| 3558 | if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) { |
| 3559 | /* sh cannot be written until sh_src has been read. |
| 3560 | * so arrange for sh to be delayed a little |
| 3561 | */ |
| 3562 | set_bit(STRIPE_DELAYED, &sh->state); |
| 3563 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3564 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, |
| 3565 | &sh_src->state)) |
| 3566 | atomic_inc(&conf->preread_active_stripes); |
| 3567 | release_stripe(sh_src); |
| 3568 | goto finish; |
| 3569 | } |
| 3570 | if (sh_src) |
| 3571 | release_stripe(sh_src); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3572 | |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3573 | sh->reconstruct_state = reconstruct_state_idle; |
| 3574 | clear_bit(STRIPE_EXPANDING, &sh->state); |
| 3575 | for (i = conf->raid_disks; i--; ) { |
| 3576 | set_bit(R5_Wantwrite, &sh->dev[i].flags); |
| 3577 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 3578 | s.locked++; |
| 3579 | } |
| 3580 | } |
| 3581 | |
| 3582 | if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) && |
| 3583 | !sh->reconstruct_state) { |
| 3584 | /* Need to write out all blocks after computing parity */ |
| 3585 | sh->disks = conf->raid_disks; |
| 3586 | stripe_set_idx(sh->sector, conf, 0, sh); |
| 3587 | schedule_reconstruction(sh, &s, 1, 1); |
| 3588 | } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) { |
| 3589 | clear_bit(STRIPE_EXPAND_READY, &sh->state); |
| 3590 | atomic_dec(&conf->reshape_stripes); |
| 3591 | wake_up(&conf->wait_for_overlap); |
| 3592 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 3593 | } |
| 3594 | |
| 3595 | if (s.expanding && s.locked == 0 && |
| 3596 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) |
| 3597 | handle_stripe_expansion(conf, sh); |
| 3598 | |
| 3599 | finish: |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3600 | /* wait for this device to become unblocked */ |
NeilBrown | 5f066c6 | 2012-07-03 12:13:29 +1000 | [diff] [blame] | 3601 | if (unlikely(s.blocked_rdev)) { |
| 3602 | if (conf->mddev->external) |
| 3603 | md_wait_for_blocked_rdev(s.blocked_rdev, |
| 3604 | conf->mddev); |
| 3605 | else |
| 3606 | /* Internal metadata will immediately |
| 3607 | * be written by raid5d, so we don't |
| 3608 | * need to wait here. |
| 3609 | */ |
| 3610 | rdev_dec_pending(s.blocked_rdev, |
| 3611 | conf->mddev); |
| 3612 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3613 | |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3614 | if (s.handle_bad_blocks) |
| 3615 | for (i = disks; i--; ) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3616 | struct md_rdev *rdev; |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3617 | struct r5dev *dev = &sh->dev[i]; |
| 3618 | if (test_and_clear_bit(R5_WriteError, &dev->flags)) { |
| 3619 | /* We own a safe reference to the rdev */ |
| 3620 | rdev = conf->disks[i].rdev; |
| 3621 | if (!rdev_set_badblocks(rdev, sh->sector, |
| 3622 | STRIPE_SECTORS, 0)) |
| 3623 | md_error(conf->mddev, rdev); |
| 3624 | rdev_dec_pending(rdev, conf->mddev); |
| 3625 | } |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 3626 | if (test_and_clear_bit(R5_MadeGood, &dev->flags)) { |
| 3627 | rdev = conf->disks[i].rdev; |
| 3628 | rdev_clear_badblocks(rdev, sh->sector, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3629 | STRIPE_SECTORS, 0); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 3630 | rdev_dec_pending(rdev, conf->mddev); |
| 3631 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3632 | if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) { |
| 3633 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3634 | if (!rdev) |
| 3635 | /* rdev have been moved down */ |
| 3636 | rdev = conf->disks[i].rdev; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3637 | rdev_clear_badblocks(rdev, sh->sector, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3638 | STRIPE_SECTORS, 0); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3639 | rdev_dec_pending(rdev, conf->mddev); |
| 3640 | } |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3641 | } |
| 3642 | |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3643 | if (s.ops_request) |
| 3644 | raid_run_ops(sh, s.ops_request); |
| 3645 | |
Dan Williams | f0e43bc | 2008-06-28 08:31:55 +1000 | [diff] [blame] | 3646 | ops_run_io(sh, &s); |
| 3647 | |
NeilBrown | c5709ef | 2011-07-26 11:35:20 +1000 | [diff] [blame] | 3648 | if (s.dec_preread_active) { |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 3649 | /* 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] | 3650 | * is waiting on a flush, it won't continue until the writes |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 3651 | * have actually been submitted. |
| 3652 | */ |
| 3653 | atomic_dec(&conf->preread_active_stripes); |
| 3654 | if (atomic_read(&conf->preread_active_stripes) < |
| 3655 | IO_THRESHOLD) |
| 3656 | md_wakeup_thread(conf->mddev->thread); |
| 3657 | } |
| 3658 | |
NeilBrown | c5709ef | 2011-07-26 11:35:20 +1000 | [diff] [blame] | 3659 | return_io(s.return_bi); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3660 | |
Dan Williams | 257a4b4 | 2011-11-08 16:22:06 +1100 | [diff] [blame] | 3661 | clear_bit_unlock(STRIPE_ACTIVE, &sh->state); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3662 | } |
| 3663 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3664 | static void raid5_activate_delayed(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3665 | { |
| 3666 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) { |
| 3667 | while (!list_empty(&conf->delayed_list)) { |
| 3668 | struct list_head *l = conf->delayed_list.next; |
| 3669 | struct stripe_head *sh; |
| 3670 | sh = list_entry(l, struct stripe_head, lru); |
| 3671 | list_del_init(l); |
| 3672 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 3673 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 3674 | atomic_inc(&conf->preread_active_stripes); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3675 | list_add_tail(&sh->lru, &conf->hold_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3676 | } |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 3677 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3678 | } |
| 3679 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3680 | static void activate_bit_delay(struct r5conf *conf) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3681 | { |
| 3682 | /* device_lock is held */ |
| 3683 | struct list_head head; |
| 3684 | list_add(&head, &conf->bitmap_list); |
| 3685 | list_del_init(&conf->bitmap_list); |
| 3686 | while (!list_empty(&head)) { |
| 3687 | struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru); |
| 3688 | list_del_init(&sh->lru); |
| 3689 | atomic_inc(&sh->count); |
| 3690 | __release_stripe(conf, sh); |
| 3691 | } |
| 3692 | } |
| 3693 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3694 | int md_raid5_congested(struct mddev *mddev, int bits) |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3695 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3696 | struct r5conf *conf = mddev->private; |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3697 | |
| 3698 | /* No difference between reads and writes. Just check |
| 3699 | * how busy the stripe_cache is |
| 3700 | */ |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 3701 | |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3702 | if (conf->inactive_blocked) |
| 3703 | return 1; |
| 3704 | if (conf->quiesce) |
| 3705 | return 1; |
| 3706 | if (list_empty_careful(&conf->inactive_list)) |
| 3707 | return 1; |
| 3708 | |
| 3709 | return 0; |
| 3710 | } |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 3711 | EXPORT_SYMBOL_GPL(md_raid5_congested); |
| 3712 | |
| 3713 | static int raid5_congested(void *data, int bits) |
| 3714 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3715 | struct mddev *mddev = data; |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 3716 | |
| 3717 | return mddev_congested(mddev, bits) || |
| 3718 | md_raid5_congested(mddev, bits); |
| 3719 | } |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3720 | |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3721 | /* We want read requests to align with chunks where possible, |
| 3722 | * but write requests don't need to. |
| 3723 | */ |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3724 | static int raid5_mergeable_bvec(struct request_queue *q, |
| 3725 | struct bvec_merge_data *bvm, |
| 3726 | struct bio_vec *biovec) |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3727 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3728 | struct mddev *mddev = q->queuedata; |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3729 | 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] | 3730 | int max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3731 | unsigned int chunk_sectors = mddev->chunk_sectors; |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3732 | unsigned int bio_sectors = bvm->bi_size >> 9; |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3733 | |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3734 | if ((bvm->bi_rw & 1) == WRITE) |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3735 | return biovec->bv_len; /* always allow writes to be mergeable */ |
| 3736 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3737 | if (mddev->new_chunk_sectors < mddev->chunk_sectors) |
| 3738 | chunk_sectors = mddev->new_chunk_sectors; |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3739 | max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9; |
| 3740 | if (max < 0) max = 0; |
| 3741 | if (max <= biovec->bv_len && bio_sectors == 0) |
| 3742 | return biovec->bv_len; |
| 3743 | else |
| 3744 | return max; |
| 3745 | } |
| 3746 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3747 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3748 | 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] | 3749 | { |
| 3750 | sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3751 | unsigned int chunk_sectors = mddev->chunk_sectors; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3752 | unsigned int bio_sectors = bio->bi_size >> 9; |
| 3753 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3754 | if (mddev->new_chunk_sectors < mddev->chunk_sectors) |
| 3755 | chunk_sectors = mddev->new_chunk_sectors; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3756 | return chunk_sectors >= |
| 3757 | ((sector & (chunk_sectors - 1)) + bio_sectors); |
| 3758 | } |
| 3759 | |
| 3760 | /* |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3761 | * add bio to the retry LIFO ( in O(1) ... we are in interrupt ) |
| 3762 | * later sampled by raid5d. |
| 3763 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3764 | 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] | 3765 | { |
| 3766 | unsigned long flags; |
| 3767 | |
| 3768 | spin_lock_irqsave(&conf->device_lock, flags); |
| 3769 | |
| 3770 | bi->bi_next = conf->retry_read_aligned_list; |
| 3771 | conf->retry_read_aligned_list = bi; |
| 3772 | |
| 3773 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 3774 | md_wakeup_thread(conf->mddev->thread); |
| 3775 | } |
| 3776 | |
| 3777 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3778 | static struct bio *remove_bio_from_retry(struct r5conf *conf) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3779 | { |
| 3780 | struct bio *bi; |
| 3781 | |
| 3782 | bi = conf->retry_read_aligned; |
| 3783 | if (bi) { |
| 3784 | conf->retry_read_aligned = NULL; |
| 3785 | return bi; |
| 3786 | } |
| 3787 | bi = conf->retry_read_aligned_list; |
| 3788 | if(bi) { |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3789 | conf->retry_read_aligned_list = bi->bi_next; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3790 | bi->bi_next = NULL; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 3791 | /* |
| 3792 | * this sets the active strip count to 1 and the processed |
| 3793 | * strip count to zero (upper 8 bits) |
| 3794 | */ |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3795 | bi->bi_phys_segments = 1; /* biased count of active stripes */ |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3796 | } |
| 3797 | |
| 3798 | return bi; |
| 3799 | } |
| 3800 | |
| 3801 | |
| 3802 | /* |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3803 | * The "raid5_align_endio" should check if the read succeeded and if it |
| 3804 | * did, call bio_endio on the original bio (having bio_put the new bio |
| 3805 | * first). |
| 3806 | * If the read failed.. |
| 3807 | */ |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3808 | static void raid5_align_endio(struct bio *bi, int error) |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3809 | { |
| 3810 | struct bio* raid_bi = bi->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3811 | struct mddev *mddev; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3812 | struct r5conf *conf; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3813 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3814 | struct md_rdev *rdev; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3815 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3816 | bio_put(bi); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3817 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3818 | rdev = (void*)raid_bi->bi_next; |
| 3819 | raid_bi->bi_next = NULL; |
NeilBrown | 2b7f222 | 2010-03-25 16:06:03 +1100 | [diff] [blame] | 3820 | mddev = rdev->mddev; |
| 3821 | conf = mddev->private; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3822 | |
| 3823 | rdev_dec_pending(rdev, conf->mddev); |
| 3824 | |
| 3825 | if (!error && uptodate) { |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3826 | bio_endio(raid_bi, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3827 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
| 3828 | wake_up(&conf->wait_for_stripe); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3829 | return; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3830 | } |
| 3831 | |
| 3832 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3833 | 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] | 3834 | |
| 3835 | add_bio_to_retry(raid_bi, conf); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3836 | } |
| 3837 | |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3838 | static int bio_fits_rdev(struct bio *bi) |
| 3839 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3840 | struct request_queue *q = bdev_get_queue(bi->bi_bdev); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3841 | |
Martin K. Petersen | ae03bf6 | 2009-05-22 17:17:50 -0400 | [diff] [blame] | 3842 | if ((bi->bi_size>>9) > queue_max_sectors(q)) |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3843 | return 0; |
| 3844 | blk_recount_segments(q, bi); |
Martin K. Petersen | 8a78362 | 2010-02-26 00:20:39 -0500 | [diff] [blame] | 3845 | if (bi->bi_phys_segments > queue_max_segments(q)) |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3846 | return 0; |
| 3847 | |
| 3848 | if (q->merge_bvec_fn) |
| 3849 | /* it's too hard to apply the merge_bvec_fn at this stage, |
| 3850 | * just just give up |
| 3851 | */ |
| 3852 | return 0; |
| 3853 | |
| 3854 | return 1; |
| 3855 | } |
| 3856 | |
| 3857 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3858 | 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] | 3859 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3860 | struct r5conf *conf = mddev->private; |
NeilBrown | 8553fe7ec | 2009-12-14 12:49:47 +1100 | [diff] [blame] | 3861 | int dd_idx; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3862 | struct bio* align_bi; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3863 | struct md_rdev *rdev; |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3864 | sector_t end_sector; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3865 | |
| 3866 | if (!in_chunk_boundary(mddev, raid_bio)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3867 | pr_debug("chunk_aligned_read : non aligned\n"); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3868 | return 0; |
| 3869 | } |
| 3870 | /* |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 3871 | * 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] | 3872 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 3873 | align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3874 | if (!align_bi) |
| 3875 | return 0; |
| 3876 | /* |
| 3877 | * set bi_end_io to a new function, and set bi_private to the |
| 3878 | * original bio. |
| 3879 | */ |
| 3880 | align_bi->bi_end_io = raid5_align_endio; |
| 3881 | align_bi->bi_private = raid_bio; |
| 3882 | /* |
| 3883 | * compute position |
| 3884 | */ |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3885 | align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector, |
| 3886 | 0, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3887 | &dd_idx, NULL); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3888 | |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3889 | 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] | 3890 | rcu_read_lock(); |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3891 | rdev = rcu_dereference(conf->disks[dd_idx].replacement); |
| 3892 | if (!rdev || test_bit(Faulty, &rdev->flags) || |
| 3893 | rdev->recovery_offset < end_sector) { |
| 3894 | rdev = rcu_dereference(conf->disks[dd_idx].rdev); |
| 3895 | if (rdev && |
| 3896 | (test_bit(Faulty, &rdev->flags) || |
| 3897 | !(test_bit(In_sync, &rdev->flags) || |
| 3898 | rdev->recovery_offset >= end_sector))) |
| 3899 | rdev = NULL; |
| 3900 | } |
| 3901 | if (rdev) { |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3902 | sector_t first_bad; |
| 3903 | int bad_sectors; |
| 3904 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3905 | atomic_inc(&rdev->nr_pending); |
| 3906 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3907 | raid_bio->bi_next = (void*)rdev; |
| 3908 | align_bi->bi_bdev = rdev->bdev; |
| 3909 | align_bi->bi_flags &= ~(1 << BIO_SEG_VALID); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3910 | |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3911 | if (!bio_fits_rdev(align_bi) || |
| 3912 | is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9, |
| 3913 | &first_bad, &bad_sectors)) { |
| 3914 | /* too big in some way, or has a known bad block */ |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3915 | bio_put(align_bi); |
| 3916 | rdev_dec_pending(rdev, mddev); |
| 3917 | return 0; |
| 3918 | } |
| 3919 | |
majianpeng | 6c0544e | 2012-06-12 08:31:10 +0800 | [diff] [blame] | 3920 | /* No reshape active, so we can trust rdev->data_offset */ |
| 3921 | align_bi->bi_sector += rdev->data_offset; |
| 3922 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3923 | spin_lock_irq(&conf->device_lock); |
| 3924 | wait_event_lock_irq(conf->wait_for_stripe, |
| 3925 | conf->quiesce == 0, |
| 3926 | conf->device_lock, /* nothing */); |
| 3927 | atomic_inc(&conf->active_aligned_reads); |
| 3928 | spin_unlock_irq(&conf->device_lock); |
| 3929 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3930 | generic_make_request(align_bi); |
| 3931 | return 1; |
| 3932 | } else { |
| 3933 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3934 | bio_put(align_bi); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3935 | return 0; |
| 3936 | } |
| 3937 | } |
| 3938 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3939 | /* __get_priority_stripe - get the next stripe to process |
| 3940 | * |
| 3941 | * Full stripe writes are allowed to pass preread active stripes up until |
| 3942 | * the bypass_threshold is exceeded. In general the bypass_count |
| 3943 | * increments when the handle_list is handled before the hold_list; however, it |
| 3944 | * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a |
| 3945 | * stripe with in flight i/o. The bypass_count will be reset when the |
| 3946 | * head of the hold_list has changed, i.e. the head was promoted to the |
| 3947 | * handle_list. |
| 3948 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3949 | static struct stripe_head *__get_priority_stripe(struct r5conf *conf) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3950 | { |
| 3951 | struct stripe_head *sh; |
| 3952 | |
| 3953 | pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n", |
| 3954 | __func__, |
| 3955 | list_empty(&conf->handle_list) ? "empty" : "busy", |
| 3956 | list_empty(&conf->hold_list) ? "empty" : "busy", |
| 3957 | atomic_read(&conf->pending_full_writes), conf->bypass_count); |
| 3958 | |
| 3959 | if (!list_empty(&conf->handle_list)) { |
| 3960 | sh = list_entry(conf->handle_list.next, typeof(*sh), lru); |
| 3961 | |
| 3962 | if (list_empty(&conf->hold_list)) |
| 3963 | conf->bypass_count = 0; |
| 3964 | else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) { |
| 3965 | if (conf->hold_list.next == conf->last_hold) |
| 3966 | conf->bypass_count++; |
| 3967 | else { |
| 3968 | conf->last_hold = conf->hold_list.next; |
| 3969 | conf->bypass_count -= conf->bypass_threshold; |
| 3970 | if (conf->bypass_count < 0) |
| 3971 | conf->bypass_count = 0; |
| 3972 | } |
| 3973 | } |
| 3974 | } else if (!list_empty(&conf->hold_list) && |
| 3975 | ((conf->bypass_threshold && |
| 3976 | conf->bypass_count > conf->bypass_threshold) || |
| 3977 | atomic_read(&conf->pending_full_writes) == 0)) { |
| 3978 | sh = list_entry(conf->hold_list.next, |
| 3979 | typeof(*sh), lru); |
| 3980 | conf->bypass_count -= conf->bypass_threshold; |
| 3981 | if (conf->bypass_count < 0) |
| 3982 | conf->bypass_count = 0; |
| 3983 | } else |
| 3984 | return NULL; |
| 3985 | |
| 3986 | list_del_init(&sh->lru); |
| 3987 | atomic_inc(&sh->count); |
| 3988 | BUG_ON(atomic_read(&sh->count) != 1); |
| 3989 | return sh; |
| 3990 | } |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3991 | |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 3992 | struct raid5_plug_cb { |
| 3993 | struct blk_plug_cb cb; |
| 3994 | struct list_head list; |
| 3995 | }; |
| 3996 | |
| 3997 | static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule) |
| 3998 | { |
| 3999 | struct raid5_plug_cb *cb = container_of( |
| 4000 | blk_cb, struct raid5_plug_cb, cb); |
| 4001 | struct stripe_head *sh; |
| 4002 | struct mddev *mddev = cb->cb.data; |
| 4003 | struct r5conf *conf = mddev->private; |
| 4004 | |
| 4005 | if (cb->list.next && !list_empty(&cb->list)) { |
| 4006 | spin_lock_irq(&conf->device_lock); |
| 4007 | while (!list_empty(&cb->list)) { |
| 4008 | sh = list_first_entry(&cb->list, struct stripe_head, lru); |
| 4009 | list_del_init(&sh->lru); |
| 4010 | /* |
| 4011 | * avoid race release_stripe_plug() sees |
| 4012 | * STRIPE_ON_UNPLUG_LIST clear but the stripe |
| 4013 | * is still in our list |
| 4014 | */ |
| 4015 | smp_mb__before_clear_bit(); |
| 4016 | clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state); |
| 4017 | __release_stripe(conf, sh); |
| 4018 | } |
| 4019 | spin_unlock_irq(&conf->device_lock); |
| 4020 | } |
| 4021 | kfree(cb); |
| 4022 | } |
| 4023 | |
| 4024 | static void release_stripe_plug(struct mddev *mddev, |
| 4025 | struct stripe_head *sh) |
| 4026 | { |
| 4027 | struct blk_plug_cb *blk_cb = blk_check_plugged( |
| 4028 | raid5_unplug, mddev, |
| 4029 | sizeof(struct raid5_plug_cb)); |
| 4030 | struct raid5_plug_cb *cb; |
| 4031 | |
| 4032 | if (!blk_cb) { |
| 4033 | release_stripe(sh); |
| 4034 | return; |
| 4035 | } |
| 4036 | |
| 4037 | cb = container_of(blk_cb, struct raid5_plug_cb, cb); |
| 4038 | |
| 4039 | if (cb->list.next == NULL) |
| 4040 | INIT_LIST_HEAD(&cb->list); |
| 4041 | |
| 4042 | if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state)) |
| 4043 | list_add_tail(&sh->lru, &cb->list); |
| 4044 | else |
| 4045 | release_stripe(sh); |
| 4046 | } |
| 4047 | |
Linus Torvalds | b4fdcb0 | 2011-11-04 17:06:58 -0700 | [diff] [blame] | 4048 | static void make_request(struct mddev *mddev, struct bio * bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4049 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4050 | struct r5conf *conf = mddev->private; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4051 | int dd_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4052 | sector_t new_sector; |
| 4053 | sector_t logical_sector, last_sector; |
| 4054 | struct stripe_head *sh; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 4055 | const int rw = bio_data_dir(bi); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 4056 | int remaining; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4057 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 4058 | if (unlikely(bi->bi_rw & REQ_FLUSH)) { |
| 4059 | md_flush_request(mddev, bi); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 4060 | return; |
NeilBrown | e5dcdd8 | 2005-09-09 16:23:41 -0700 | [diff] [blame] | 4061 | } |
| 4062 | |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 4063 | md_write_start(mddev, bi); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 4064 | |
NeilBrown | 802ba06 | 2006-12-13 00:34:13 -0800 | [diff] [blame] | 4065 | if (rw == READ && |
Raz Ben-Jehuda(caro) | 5248861 | 2006-12-10 02:20:48 -0800 | [diff] [blame] | 4066 | mddev->reshape_position == MaxSector && |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 4067 | chunk_aligned_read(mddev,bi)) |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 4068 | return; |
Raz Ben-Jehuda(caro) | 5248861 | 2006-12-10 02:20:48 -0800 | [diff] [blame] | 4069 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4070 | logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
| 4071 | last_sector = bi->bi_sector + (bi->bi_size>>9); |
| 4072 | bi->bi_next = NULL; |
| 4073 | bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 4074 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4075 | for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) { |
| 4076 | DEFINE_WAIT(w); |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4077 | int previous; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 4078 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4079 | retry: |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4080 | previous = 0; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 4081 | prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 4082 | if (unlikely(conf->reshape_progress != MaxSector)) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4083 | /* spinlock is needed as reshape_progress may be |
NeilBrown | df8e7f76 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 4084 | * 64bit on a 32bit platform, and so it might be |
| 4085 | * possible to see a half-updated value |
Jesper Juhl | aeb878b | 2011-04-10 18:06:17 +0200 | [diff] [blame] | 4086 | * Of course reshape_progress could change after |
NeilBrown | df8e7f76 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 4087 | * the lock is dropped, so once we get a reference |
| 4088 | * to the stripe that we think it is, we will have |
| 4089 | * to check again. |
| 4090 | */ |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4091 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4092 | if (mddev->reshape_backwards |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4093 | ? logical_sector < conf->reshape_progress |
| 4094 | : logical_sector >= conf->reshape_progress) { |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4095 | previous = 1; |
| 4096 | } else { |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4097 | if (mddev->reshape_backwards |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4098 | ? logical_sector < conf->reshape_safe |
| 4099 | : logical_sector >= conf->reshape_safe) { |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 4100 | spin_unlock_irq(&conf->device_lock); |
| 4101 | schedule(); |
| 4102 | goto retry; |
| 4103 | } |
| 4104 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4105 | spin_unlock_irq(&conf->device_lock); |
| 4106 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4107 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4108 | new_sector = raid5_compute_sector(conf, logical_sector, |
| 4109 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4110 | &dd_idx, NULL); |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4111 | pr_debug("raid456: make_request, sector %llu logical %llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4112 | (unsigned long long)new_sector, |
| 4113 | (unsigned long long)logical_sector); |
| 4114 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4115 | sh = get_active_stripe(conf, new_sector, previous, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4116 | (bi->bi_rw&RWA_MASK), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4117 | if (sh) { |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 4118 | if (unlikely(previous)) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4119 | /* expansion might have moved on while waiting for a |
NeilBrown | df8e7f76 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 4120 | * stripe, so we must do the range check again. |
| 4121 | * Expansion could still move past after this |
| 4122 | * test, but as we are holding a reference to |
| 4123 | * 'sh', we know that if that happens, |
| 4124 | * STRIPE_EXPANDING will get set and the expansion |
| 4125 | * won't proceed until we finish with the stripe. |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4126 | */ |
| 4127 | int must_retry = 0; |
| 4128 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4129 | if (mddev->reshape_backwards |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 4130 | ? logical_sector >= conf->reshape_progress |
| 4131 | : logical_sector < conf->reshape_progress) |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4132 | /* mismatch, need to try again */ |
| 4133 | must_retry = 1; |
| 4134 | spin_unlock_irq(&conf->device_lock); |
| 4135 | if (must_retry) { |
| 4136 | release_stripe(sh); |
Dan Williams | 7a3ab90 | 2009-06-16 16:00:33 -0700 | [diff] [blame] | 4137 | schedule(); |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4138 | goto retry; |
| 4139 | } |
| 4140 | } |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 4141 | |
Namhyung Kim | ffd96e3 | 2011-07-18 17:38:51 +1000 | [diff] [blame] | 4142 | if (rw == WRITE && |
NeilBrown | a5c308d | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 4143 | logical_sector >= mddev->suspend_lo && |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4144 | logical_sector < mddev->suspend_hi) { |
| 4145 | release_stripe(sh); |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 4146 | /* As the suspend_* range is controlled by |
| 4147 | * userspace, we want an interruptible |
| 4148 | * wait. |
| 4149 | */ |
| 4150 | flush_signals(current); |
| 4151 | prepare_to_wait(&conf->wait_for_overlap, |
| 4152 | &w, TASK_INTERRUPTIBLE); |
| 4153 | if (logical_sector >= mddev->suspend_lo && |
| 4154 | logical_sector < mddev->suspend_hi) |
| 4155 | schedule(); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4156 | goto retry; |
| 4157 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4158 | |
| 4159 | if (test_bit(STRIPE_EXPANDING, &sh->state) || |
Namhyung Kim | ffd96e3 | 2011-07-18 17:38:51 +1000 | [diff] [blame] | 4160 | !add_stripe_bio(sh, bi, dd_idx, rw)) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4161 | /* Stripe is busy expanding or |
| 4162 | * add failed due to overlap. Flush everything |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4163 | * and wait a while |
| 4164 | */ |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 4165 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4166 | release_stripe(sh); |
| 4167 | schedule(); |
| 4168 | goto retry; |
| 4169 | } |
| 4170 | finish_wait(&conf->wait_for_overlap, &w); |
NeilBrown | 6ed3003 | 2008-02-06 01:40:00 -0800 | [diff] [blame] | 4171 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4172 | clear_bit(STRIPE_DELAYED, &sh->state); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 4173 | if ((bi->bi_rw & REQ_SYNC) && |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 4174 | !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 4175 | atomic_inc(&conf->preread_active_stripes); |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 4176 | release_stripe_plug(mddev, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4177 | } else { |
| 4178 | /* cannot get stripe for read-ahead, just give-up */ |
| 4179 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
| 4180 | finish_wait(&conf->wait_for_overlap, &w); |
| 4181 | break; |
| 4182 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4183 | } |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 4184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4185 | spin_lock_irq(&conf->device_lock); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4186 | remaining = raid5_dec_bi_phys_segments(bi); |
NeilBrown | f634475 | 2006-03-27 01:18:17 -0800 | [diff] [blame] | 4187 | spin_unlock_irq(&conf->device_lock); |
| 4188 | if (remaining == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4189 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4190 | if ( rw == WRITE ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4191 | md_write_end(mddev); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 4192 | |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 4193 | bio_endio(bi, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4194 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4195 | } |
| 4196 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4197 | 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] | 4198 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4199 | 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] | 4200 | { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4201 | /* reshaping is quite different to recovery/resync so it is |
| 4202 | * handled quite separately ... here. |
| 4203 | * |
| 4204 | * On each call to sync_request, we gather one chunk worth of |
| 4205 | * destination stripes and flag them as expanding. |
| 4206 | * Then we find all the source stripes and request reads. |
| 4207 | * As the reads complete, handle_stripe will copy the data |
| 4208 | * into the destination stripe and release that stripe. |
| 4209 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4210 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4211 | struct stripe_head *sh; |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4212 | sector_t first_sector, last_sector; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4213 | int raid_disks = conf->previous_raid_disks; |
| 4214 | int data_disks = raid_disks - conf->max_degraded; |
| 4215 | int new_data_disks = conf->raid_disks - conf->max_degraded; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4216 | int i; |
| 4217 | int dd_idx; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4218 | sector_t writepos, readpos, safepos; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4219 | sector_t stripe_addr; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4220 | int reshape_sectors; |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4221 | struct list_head stripes; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4222 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4223 | if (sector_nr == 0) { |
| 4224 | /* If restarting in the middle, skip the initial sectors */ |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4225 | if (mddev->reshape_backwards && |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4226 | conf->reshape_progress < raid5_size(mddev, 0, 0)) { |
| 4227 | sector_nr = raid5_size(mddev, 0, 0) |
| 4228 | - conf->reshape_progress; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4229 | } else if (!mddev->reshape_backwards && |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4230 | conf->reshape_progress > 0) |
| 4231 | sector_nr = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4232 | sector_div(sector_nr, new_data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4233 | if (sector_nr) { |
NeilBrown | 8dee721 | 2009-11-06 14:59:29 +1100 | [diff] [blame] | 4234 | mddev->curr_resync_completed = sector_nr; |
| 4235 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4236 | *skipped = 1; |
| 4237 | return sector_nr; |
| 4238 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4239 | } |
| 4240 | |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4241 | /* We need to process a full chunk at a time. |
| 4242 | * If old and new chunk sizes differ, we need to process the |
| 4243 | * largest of these |
| 4244 | */ |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4245 | if (mddev->new_chunk_sectors > mddev->chunk_sectors) |
| 4246 | reshape_sectors = mddev->new_chunk_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4247 | else |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4248 | reshape_sectors = mddev->chunk_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4249 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 4250 | /* We update the metadata at least every 10 seconds, or when |
| 4251 | * the data about to be copied would over-write the source of |
| 4252 | * the data at the front of the range. i.e. one new_stripe |
| 4253 | * along from reshape_progress new_maps to after where |
| 4254 | * reshape_safe old_maps to |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4255 | */ |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4256 | writepos = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4257 | sector_div(writepos, new_data_disks); |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4258 | readpos = conf->reshape_progress; |
| 4259 | sector_div(readpos, data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4260 | safepos = conf->reshape_safe; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4261 | sector_div(safepos, data_disks); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4262 | if (mddev->reshape_backwards) { |
NeilBrown | ed37d83 | 2009-05-27 21:39:05 +1000 | [diff] [blame] | 4263 | writepos -= min_t(sector_t, reshape_sectors, writepos); |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4264 | readpos += reshape_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4265 | safepos += reshape_sectors; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4266 | } else { |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4267 | writepos += reshape_sectors; |
NeilBrown | ed37d83 | 2009-05-27 21:39:05 +1000 | [diff] [blame] | 4268 | readpos -= min_t(sector_t, reshape_sectors, readpos); |
| 4269 | safepos -= min_t(sector_t, reshape_sectors, safepos); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4270 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4271 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 4272 | /* Having calculated the 'writepos' possibly use it |
| 4273 | * to set 'stripe_addr' which is where we will write to. |
| 4274 | */ |
| 4275 | if (mddev->reshape_backwards) { |
| 4276 | BUG_ON(conf->reshape_progress == 0); |
| 4277 | stripe_addr = writepos; |
| 4278 | BUG_ON((mddev->dev_sectors & |
| 4279 | ~((sector_t)reshape_sectors - 1)) |
| 4280 | - reshape_sectors - stripe_addr |
| 4281 | != sector_nr); |
| 4282 | } else { |
| 4283 | BUG_ON(writepos != sector_nr + reshape_sectors); |
| 4284 | stripe_addr = sector_nr; |
| 4285 | } |
| 4286 | |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4287 | /* 'writepos' is the most advanced device address we might write. |
| 4288 | * 'readpos' is the least advanced device address we might read. |
| 4289 | * 'safepos' is the least address recorded in the metadata as having |
| 4290 | * been reshaped. |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 4291 | * If there is a min_offset_diff, these are adjusted either by |
| 4292 | * increasing the safepos/readpos if diff is negative, or |
| 4293 | * increasing writepos if diff is positive. |
| 4294 | * If 'readpos' is then behind 'writepos', there is no way that we can |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4295 | * ensure safety in the face of a crash - that must be done by userspace |
| 4296 | * making a backup of the data. So in that case there is no particular |
| 4297 | * rush to update metadata. |
| 4298 | * Otherwise if 'safepos' is behind 'writepos', then we really need to |
| 4299 | * update the metadata to advance 'safepos' to match 'readpos' so that |
| 4300 | * we can be safe in the event of a crash. |
| 4301 | * So we insist on updating metadata if safepos is behind writepos and |
| 4302 | * readpos is beyond writepos. |
| 4303 | * In any case, update the metadata every 10 seconds. |
| 4304 | * Maybe that number should be configurable, but I'm not sure it is |
| 4305 | * worth it.... maybe it could be a multiple of safemode_delay??? |
| 4306 | */ |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 4307 | if (conf->min_offset_diff < 0) { |
| 4308 | safepos += -conf->min_offset_diff; |
| 4309 | readpos += -conf->min_offset_diff; |
| 4310 | } else |
| 4311 | writepos += conf->min_offset_diff; |
| 4312 | |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4313 | if ((mddev->reshape_backwards |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4314 | ? (safepos > writepos && readpos < writepos) |
| 4315 | : (safepos < writepos && readpos > writepos)) || |
| 4316 | time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4317 | /* Cannot proceed until we've updated the superblock... */ |
| 4318 | wait_event(conf->wait_for_overlap, |
| 4319 | atomic_read(&conf->reshape_stripes)==0); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4320 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4321 | mddev->curr_resync_completed = sector_nr; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4322 | conf->reshape_checkpoint = jiffies; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4323 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4324 | md_wakeup_thread(mddev->thread); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4325 | wait_event(mddev->sb_wait, mddev->flags == 0 || |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4326 | kthread_should_stop()); |
| 4327 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4328 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4329 | spin_unlock_irq(&conf->device_lock); |
| 4330 | wake_up(&conf->wait_for_overlap); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4331 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4332 | } |
| 4333 | |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4334 | INIT_LIST_HEAD(&stripes); |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4335 | for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4336 | int j; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4337 | int skipped_disk = 0; |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4338 | sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4339 | set_bit(STRIPE_EXPANDING, &sh->state); |
| 4340 | atomic_inc(&conf->reshape_stripes); |
| 4341 | /* If any of this stripe is beyond the end of the old |
| 4342 | * array, then we need to zero those blocks |
| 4343 | */ |
| 4344 | for (j=sh->disks; j--;) { |
| 4345 | sector_t s; |
| 4346 | if (j == sh->pd_idx) |
| 4347 | continue; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4348 | if (conf->level == 6 && |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4349 | j == sh->qd_idx) |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4350 | continue; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 4351 | s = compute_blocknr(sh, j, 0); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4352 | if (s < raid5_size(mddev, 0, 0)) { |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4353 | skipped_disk = 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4354 | continue; |
| 4355 | } |
| 4356 | memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE); |
| 4357 | set_bit(R5_Expanded, &sh->dev[j].flags); |
| 4358 | set_bit(R5_UPTODATE, &sh->dev[j].flags); |
| 4359 | } |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4360 | if (!skipped_disk) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4361 | set_bit(STRIPE_EXPAND_READY, &sh->state); |
| 4362 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4363 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4364 | list_add(&sh->lru, &stripes); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4365 | } |
| 4366 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4367 | if (mddev->reshape_backwards) |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4368 | conf->reshape_progress -= reshape_sectors * new_data_disks; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4369 | else |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4370 | conf->reshape_progress += reshape_sectors * new_data_disks; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4371 | spin_unlock_irq(&conf->device_lock); |
| 4372 | /* Ok, those stripe are ready. We can start scheduling |
| 4373 | * reads on the source stripes. |
| 4374 | * The source stripes are determined by mapping the first and last |
| 4375 | * block on the destination stripes. |
| 4376 | */ |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4377 | first_sector = |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4378 | raid5_compute_sector(conf, stripe_addr*(new_data_disks), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4379 | 1, &dd_idx, NULL); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4380 | last_sector = |
NeilBrown | 0e6e027 | 2009-06-09 16:32:22 +1000 | [diff] [blame] | 4381 | raid5_compute_sector(conf, ((stripe_addr+reshape_sectors) |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 4382 | * new_data_disks - 1), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4383 | 1, &dd_idx, NULL); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4384 | if (last_sector >= mddev->dev_sectors) |
| 4385 | last_sector = mddev->dev_sectors - 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4386 | while (first_sector <= last_sector) { |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4387 | sh = get_active_stripe(conf, first_sector, 1, 0, 1); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4388 | set_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 4389 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4390 | release_stripe(sh); |
| 4391 | first_sector += STRIPE_SECTORS; |
| 4392 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4393 | /* Now that the sources are clearly marked, we can release |
| 4394 | * the destination stripes |
| 4395 | */ |
| 4396 | while (!list_empty(&stripes)) { |
| 4397 | sh = list_entry(stripes.next, struct stripe_head, lru); |
| 4398 | list_del_init(&sh->lru); |
| 4399 | release_stripe(sh); |
| 4400 | } |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4401 | /* If this takes us to the resync_max point where we have to pause, |
| 4402 | * then we need to write out the superblock. |
| 4403 | */ |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4404 | sector_nr += reshape_sectors; |
NeilBrown | c03f6a1 | 2009-04-17 11:06:30 +1000 | [diff] [blame] | 4405 | if ((sector_nr - mddev->curr_resync_completed) * 2 |
| 4406 | >= mddev->resync_max - mddev->curr_resync_completed) { |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4407 | /* Cannot proceed until we've updated the superblock... */ |
| 4408 | wait_event(conf->wait_for_overlap, |
| 4409 | atomic_read(&conf->reshape_stripes) == 0); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4410 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4411 | mddev->curr_resync_completed = sector_nr; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4412 | conf->reshape_checkpoint = jiffies; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4413 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 4414 | md_wakeup_thread(mddev->thread); |
| 4415 | wait_event(mddev->sb_wait, |
| 4416 | !test_bit(MD_CHANGE_DEVS, &mddev->flags) |
| 4417 | || kthread_should_stop()); |
| 4418 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4419 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4420 | spin_unlock_irq(&conf->device_lock); |
| 4421 | wake_up(&conf->wait_for_overlap); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4422 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4423 | } |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4424 | return reshape_sectors; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4425 | } |
| 4426 | |
| 4427 | /* FIXME go_faster isn't used */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4428 | 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] | 4429 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4430 | struct r5conf *conf = mddev->private; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4431 | struct stripe_head *sh; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4432 | sector_t max_sector = mddev->dev_sectors; |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 4433 | sector_t sync_blocks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4434 | int still_degraded = 0; |
| 4435 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4436 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4437 | if (sector_nr >= max_sector) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4438 | /* just being told to finish up .. nothing much to do */ |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 4439 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 4440 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { |
| 4441 | end_reshape(conf); |
| 4442 | return 0; |
| 4443 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4444 | |
| 4445 | if (mddev->curr_resync < max_sector) /* aborted */ |
| 4446 | bitmap_end_sync(mddev->bitmap, mddev->curr_resync, |
| 4447 | &sync_blocks, 1); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4448 | else /* completed sync */ |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4449 | conf->fullsync = 0; |
| 4450 | bitmap_close_sync(mddev->bitmap); |
| 4451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4452 | return 0; |
| 4453 | } |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4454 | |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4455 | /* Allow raid5_quiesce to complete */ |
| 4456 | wait_event(conf->wait_for_overlap, conf->quiesce != 2); |
| 4457 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4458 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 4459 | return reshape_request(mddev, sector_nr, skipped); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4460 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4461 | /* No need to check resync_max as we never do more than one |
| 4462 | * stripe, and as resync_max will always be on a chunk boundary, |
| 4463 | * if the check in md_do_sync didn't fire, there is no chance |
| 4464 | * of overstepping resync_max here |
| 4465 | */ |
| 4466 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4467 | /* if there is too many failed drives and we are trying |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4468 | * to resync, then assert that we are finished, because there is |
| 4469 | * nothing we can do. |
| 4470 | */ |
NeilBrown | 3285edf | 2006-06-26 00:27:55 -0700 | [diff] [blame] | 4471 | if (mddev->degraded >= conf->max_degraded && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4472 | test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4473 | sector_t rv = mddev->dev_sectors - sector_nr; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 4474 | *skipped = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4475 | return rv; |
| 4476 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4477 | if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) && |
NeilBrown | 3855ad9 | 2005-11-08 21:39:38 -0800 | [diff] [blame] | 4478 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4479 | !conf->fullsync && sync_blocks >= STRIPE_SECTORS) { |
| 4480 | /* we can skip this block, and probably more */ |
| 4481 | sync_blocks /= STRIPE_SECTORS; |
| 4482 | *skipped = 1; |
| 4483 | return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */ |
| 4484 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4485 | |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 4486 | bitmap_cond_end_sync(mddev->bitmap, sector_nr); |
| 4487 | |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4488 | sh = get_active_stripe(conf, sector_nr, 0, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4489 | if (sh == NULL) { |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4490 | sh = get_active_stripe(conf, sector_nr, 0, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4491 | /* make sure we don't swamp the stripe cache if someone else |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4492 | * is trying to get access |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4493 | */ |
Nishanth Aravamudan | 66c006a | 2005-11-07 01:01:17 -0800 | [diff] [blame] | 4494 | schedule_timeout_uninterruptible(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4495 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4496 | /* Need to check if array will still be degraded after recovery/resync |
| 4497 | * We don't need to check the 'failed' flag as when that gets set, |
| 4498 | * recovery aborts. |
| 4499 | */ |
NeilBrown | f001a70 | 2009-06-09 14:30:31 +1000 | [diff] [blame] | 4500 | for (i = 0; i < conf->raid_disks; i++) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4501 | if (conf->disks[i].rdev == NULL) |
| 4502 | still_degraded = 1; |
| 4503 | |
| 4504 | bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded); |
| 4505 | |
NeilBrown | 83206d6 | 2011-07-26 11:19:49 +1000 | [diff] [blame] | 4506 | set_bit(STRIPE_SYNC_REQUESTED, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4507 | |
NeilBrown | 1442577 | 2009-10-16 15:55:25 +1100 | [diff] [blame] | 4508 | handle_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4509 | release_stripe(sh); |
| 4510 | |
| 4511 | return STRIPE_SECTORS; |
| 4512 | } |
| 4513 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4514 | 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] | 4515 | { |
| 4516 | /* We may not be able to submit a whole bio at once as there |
| 4517 | * may not be enough stripe_heads available. |
| 4518 | * We cannot pre-allocate enough stripe_heads as we may need |
| 4519 | * more than exist in the cache (if we allow ever large chunks). |
| 4520 | * So we do one stripe head at a time and record in |
| 4521 | * ->bi_hw_segments how many have been done. |
| 4522 | * |
| 4523 | * We *know* that this entire raid_bio is in one chunk, so |
| 4524 | * it will be only one 'dd_idx' and only need one call to raid5_compute_sector. |
| 4525 | */ |
| 4526 | struct stripe_head *sh; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4527 | int dd_idx; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4528 | sector_t sector, logical_sector, last_sector; |
| 4529 | int scnt = 0; |
| 4530 | int remaining; |
| 4531 | int handled = 0; |
| 4532 | |
| 4533 | logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4534 | sector = raid5_compute_sector(conf, logical_sector, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4535 | 0, &dd_idx, NULL); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4536 | last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9); |
| 4537 | |
| 4538 | for (; logical_sector < last_sector; |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4539 | logical_sector += STRIPE_SECTORS, |
| 4540 | sector += STRIPE_SECTORS, |
| 4541 | scnt++) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4542 | |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4543 | if (scnt < raid5_bi_hw_segments(raid_bio)) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4544 | /* already done this stripe */ |
| 4545 | continue; |
| 4546 | |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4547 | sh = get_active_stripe(conf, sector, 0, 1, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4548 | |
| 4549 | if (!sh) { |
| 4550 | /* failed to get a stripe - must wait */ |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4551 | raid5_set_bi_hw_segments(raid_bio, scnt); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4552 | conf->retry_read_aligned = raid_bio; |
| 4553 | return handled; |
| 4554 | } |
| 4555 | |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4556 | if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) { |
| 4557 | release_stripe(sh); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4558 | raid5_set_bi_hw_segments(raid_bio, scnt); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4559 | conf->retry_read_aligned = raid_bio; |
| 4560 | return handled; |
| 4561 | } |
| 4562 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4563 | handle_stripe(sh); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4564 | release_stripe(sh); |
| 4565 | handled++; |
| 4566 | } |
| 4567 | spin_lock_irq(&conf->device_lock); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4568 | remaining = raid5_dec_bi_phys_segments(raid_bio); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4569 | spin_unlock_irq(&conf->device_lock); |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 4570 | if (remaining == 0) |
| 4571 | bio_endio(raid_bio, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4572 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
| 4573 | wake_up(&conf->wait_for_stripe); |
| 4574 | return handled; |
| 4575 | } |
| 4576 | |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame^] | 4577 | #define MAX_STRIPE_BATCH 8 |
| 4578 | static int handle_active_stripes(struct r5conf *conf) |
| 4579 | { |
| 4580 | struct stripe_head *batch[MAX_STRIPE_BATCH], *sh; |
| 4581 | int i, batch_size = 0; |
| 4582 | |
| 4583 | while (batch_size < MAX_STRIPE_BATCH && |
| 4584 | (sh = __get_priority_stripe(conf)) != NULL) |
| 4585 | batch[batch_size++] = sh; |
| 4586 | |
| 4587 | if (batch_size == 0) |
| 4588 | return batch_size; |
| 4589 | spin_unlock_irq(&conf->device_lock); |
| 4590 | |
| 4591 | for (i = 0; i < batch_size; i++) |
| 4592 | handle_stripe(batch[i]); |
| 4593 | |
| 4594 | cond_resched(); |
| 4595 | |
| 4596 | spin_lock_irq(&conf->device_lock); |
| 4597 | for (i = 0; i < batch_size; i++) |
| 4598 | __release_stripe(conf, batch[i]); |
| 4599 | return batch_size; |
| 4600 | } |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4601 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4602 | /* |
| 4603 | * This is our raid5 kernel thread. |
| 4604 | * |
| 4605 | * We scan the hash table for stripes which can be handled now. |
| 4606 | * During the scan, completed stripes are saved for us by the interrupt |
| 4607 | * handler, so that they will not have to wait for our next wakeup. |
| 4608 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4609 | static void raid5d(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4610 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4611 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4612 | int handled; |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 4613 | struct blk_plug plug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4614 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4615 | pr_debug("+++ raid5d active\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4616 | |
| 4617 | md_check_recovery(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4618 | |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 4619 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4620 | handled = 0; |
| 4621 | spin_lock_irq(&conf->device_lock); |
| 4622 | while (1) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4623 | struct bio *bio; |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame^] | 4624 | int batch_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4625 | |
NeilBrown | 0021b7b | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 4626 | if ( |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 4627 | !list_empty(&conf->bitmap_list)) { |
| 4628 | /* Now is a good time to flush some bitmap updates */ |
| 4629 | conf->seq_flush++; |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 4630 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4631 | bitmap_unplug(mddev->bitmap); |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 4632 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 4633 | conf->seq_write = conf->seq_flush; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4634 | activate_bit_delay(conf); |
| 4635 | } |
NeilBrown | 0021b7b | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 4636 | raid5_activate_delayed(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4637 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4638 | while ((bio = remove_bio_from_retry(conf))) { |
| 4639 | int ok; |
| 4640 | spin_unlock_irq(&conf->device_lock); |
| 4641 | ok = retry_aligned_read(conf, bio); |
| 4642 | spin_lock_irq(&conf->device_lock); |
| 4643 | if (!ok) |
| 4644 | break; |
| 4645 | handled++; |
| 4646 | } |
| 4647 | |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame^] | 4648 | batch_size = handle_active_stripes(conf); |
| 4649 | if (!batch_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4650 | break; |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame^] | 4651 | handled += batch_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4652 | |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame^] | 4653 | if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) { |
| 4654 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 4655 | md_check_recovery(mddev); |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame^] | 4656 | spin_lock_irq(&conf->device_lock); |
| 4657 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4658 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4659 | pr_debug("%d stripes handled\n", handled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4660 | |
| 4661 | spin_unlock_irq(&conf->device_lock); |
| 4662 | |
Dan Williams | c9f21aa | 2008-07-23 12:05:51 -0700 | [diff] [blame] | 4663 | async_tx_issue_pending_all(); |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 4664 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4665 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4666 | pr_debug("--- raid5d inactive\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4667 | } |
| 4668 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4669 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4670 | raid5_show_stripe_cache_size(struct mddev *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4671 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4672 | struct r5conf *conf = mddev->private; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4673 | if (conf) |
| 4674 | return sprintf(page, "%d\n", conf->max_nr_stripes); |
| 4675 | else |
| 4676 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4677 | } |
| 4678 | |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 4679 | int |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4680 | raid5_set_cache_size(struct mddev *mddev, int size) |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 4681 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4682 | struct r5conf *conf = mddev->private; |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 4683 | int err; |
| 4684 | |
| 4685 | if (size <= 16 || size > 32768) |
| 4686 | return -EINVAL; |
| 4687 | while (size < conf->max_nr_stripes) { |
| 4688 | if (drop_one_stripe(conf)) |
| 4689 | conf->max_nr_stripes--; |
| 4690 | else |
| 4691 | break; |
| 4692 | } |
| 4693 | err = md_allow_write(mddev); |
| 4694 | if (err) |
| 4695 | return err; |
| 4696 | while (size > conf->max_nr_stripes) { |
| 4697 | if (grow_one_stripe(conf)) |
| 4698 | conf->max_nr_stripes++; |
| 4699 | else break; |
| 4700 | } |
| 4701 | return 0; |
| 4702 | } |
| 4703 | EXPORT_SYMBOL(raid5_set_cache_size); |
| 4704 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4705 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4706 | 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] | 4707 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4708 | struct r5conf *conf = mddev->private; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4709 | unsigned long new; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4710 | int err; |
| 4711 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4712 | if (len >= PAGE_SIZE) |
| 4713 | return -EINVAL; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4714 | if (!conf) |
| 4715 | return -ENODEV; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4716 | |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4717 | if (strict_strtoul(page, 10, &new)) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4718 | return -EINVAL; |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 4719 | err = raid5_set_cache_size(mddev, new); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4720 | if (err) |
| 4721 | return err; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4722 | return len; |
| 4723 | } |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4724 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4725 | static struct md_sysfs_entry |
| 4726 | raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR, |
| 4727 | raid5_show_stripe_cache_size, |
| 4728 | raid5_store_stripe_cache_size); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4729 | |
| 4730 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4731 | raid5_show_preread_threshold(struct mddev *mddev, char *page) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4732 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4733 | struct r5conf *conf = mddev->private; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4734 | if (conf) |
| 4735 | return sprintf(page, "%d\n", conf->bypass_threshold); |
| 4736 | else |
| 4737 | return 0; |
| 4738 | } |
| 4739 | |
| 4740 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4741 | 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] | 4742 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4743 | struct r5conf *conf = mddev->private; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4744 | unsigned long new; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4745 | if (len >= PAGE_SIZE) |
| 4746 | return -EINVAL; |
| 4747 | if (!conf) |
| 4748 | return -ENODEV; |
| 4749 | |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4750 | if (strict_strtoul(page, 10, &new)) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4751 | return -EINVAL; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4752 | if (new > conf->max_nr_stripes) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4753 | return -EINVAL; |
| 4754 | conf->bypass_threshold = new; |
| 4755 | return len; |
| 4756 | } |
| 4757 | |
| 4758 | static struct md_sysfs_entry |
| 4759 | raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold, |
| 4760 | S_IRUGO | S_IWUSR, |
| 4761 | raid5_show_preread_threshold, |
| 4762 | raid5_store_preread_threshold); |
| 4763 | |
| 4764 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4765 | stripe_cache_active_show(struct mddev *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4766 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4767 | struct r5conf *conf = mddev->private; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4768 | if (conf) |
| 4769 | return sprintf(page, "%d\n", atomic_read(&conf->active_stripes)); |
| 4770 | else |
| 4771 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4772 | } |
| 4773 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4774 | static struct md_sysfs_entry |
| 4775 | raid5_stripecache_active = __ATTR_RO(stripe_cache_active); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4776 | |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4777 | static struct attribute *raid5_attrs[] = { |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4778 | &raid5_stripecache_size.attr, |
| 4779 | &raid5_stripecache_active.attr, |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4780 | &raid5_preread_bypass_threshold.attr, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4781 | NULL, |
| 4782 | }; |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4783 | static struct attribute_group raid5_attrs_group = { |
| 4784 | .name = NULL, |
| 4785 | .attrs = raid5_attrs, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4786 | }; |
| 4787 | |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4788 | static sector_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4789 | raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks) |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4790 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4791 | struct r5conf *conf = mddev->private; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4792 | |
| 4793 | if (!sectors) |
| 4794 | sectors = mddev->dev_sectors; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4795 | if (!raid_disks) |
NeilBrown | 7ec0547 | 2009-03-31 15:10:36 +1100 | [diff] [blame] | 4796 | /* size is defined by the smallest of previous and new size */ |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4797 | raid_disks = min(conf->raid_disks, conf->previous_raid_disks); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4798 | |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4799 | sectors &= ~((sector_t)mddev->chunk_sectors - 1); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4800 | sectors &= ~((sector_t)mddev->new_chunk_sectors - 1); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4801 | return sectors * (raid_disks - conf->max_degraded); |
| 4802 | } |
| 4803 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4804 | static void raid5_free_percpu(struct r5conf *conf) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4805 | { |
| 4806 | struct raid5_percpu *percpu; |
| 4807 | unsigned long cpu; |
| 4808 | |
| 4809 | if (!conf->percpu) |
| 4810 | return; |
| 4811 | |
| 4812 | get_online_cpus(); |
| 4813 | for_each_possible_cpu(cpu) { |
| 4814 | percpu = per_cpu_ptr(conf->percpu, cpu); |
| 4815 | safe_put_page(percpu->spare_page); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4816 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4817 | } |
| 4818 | #ifdef CONFIG_HOTPLUG_CPU |
| 4819 | unregister_cpu_notifier(&conf->cpu_notify); |
| 4820 | #endif |
| 4821 | put_online_cpus(); |
| 4822 | |
| 4823 | free_percpu(conf->percpu); |
| 4824 | } |
| 4825 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4826 | static void free_conf(struct r5conf *conf) |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 4827 | { |
| 4828 | shrink_stripes(conf); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4829 | raid5_free_percpu(conf); |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 4830 | kfree(conf->disks); |
| 4831 | kfree(conf->stripe_hashtbl); |
| 4832 | kfree(conf); |
| 4833 | } |
| 4834 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4835 | #ifdef CONFIG_HOTPLUG_CPU |
| 4836 | static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action, |
| 4837 | void *hcpu) |
| 4838 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4839 | struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4840 | long cpu = (long)hcpu; |
| 4841 | struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu); |
| 4842 | |
| 4843 | switch (action) { |
| 4844 | case CPU_UP_PREPARE: |
| 4845 | case CPU_UP_PREPARE_FROZEN: |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4846 | if (conf->level == 6 && !percpu->spare_page) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4847 | percpu->spare_page = alloc_page(GFP_KERNEL); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4848 | if (!percpu->scribble) |
| 4849 | percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL); |
| 4850 | |
| 4851 | if (!percpu->scribble || |
| 4852 | (conf->level == 6 && !percpu->spare_page)) { |
| 4853 | safe_put_page(percpu->spare_page); |
| 4854 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4855 | pr_err("%s: failed memory allocation for cpu%ld\n", |
| 4856 | __func__, cpu); |
Akinobu Mita | 55af6bb | 2010-05-26 14:43:35 -0700 | [diff] [blame] | 4857 | return notifier_from_errno(-ENOMEM); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4858 | } |
| 4859 | break; |
| 4860 | case CPU_DEAD: |
| 4861 | case CPU_DEAD_FROZEN: |
| 4862 | safe_put_page(percpu->spare_page); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4863 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4864 | percpu->spare_page = NULL; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4865 | percpu->scribble = NULL; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4866 | break; |
| 4867 | default: |
| 4868 | break; |
| 4869 | } |
| 4870 | return NOTIFY_OK; |
| 4871 | } |
| 4872 | #endif |
| 4873 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4874 | static int raid5_alloc_percpu(struct r5conf *conf) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4875 | { |
| 4876 | unsigned long cpu; |
| 4877 | struct page *spare_page; |
Tejun Heo | a29d8b8 | 2010-02-02 14:39:15 +0900 | [diff] [blame] | 4878 | struct raid5_percpu __percpu *allcpus; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4879 | void *scribble; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4880 | int err; |
| 4881 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4882 | allcpus = alloc_percpu(struct raid5_percpu); |
| 4883 | if (!allcpus) |
| 4884 | return -ENOMEM; |
| 4885 | conf->percpu = allcpus; |
| 4886 | |
| 4887 | get_online_cpus(); |
| 4888 | err = 0; |
| 4889 | for_each_present_cpu(cpu) { |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4890 | if (conf->level == 6) { |
| 4891 | spare_page = alloc_page(GFP_KERNEL); |
| 4892 | if (!spare_page) { |
| 4893 | err = -ENOMEM; |
| 4894 | break; |
| 4895 | } |
| 4896 | per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page; |
| 4897 | } |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4898 | scribble = kmalloc(conf->scribble_len, GFP_KERNEL); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4899 | if (!scribble) { |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4900 | err = -ENOMEM; |
| 4901 | break; |
| 4902 | } |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4903 | per_cpu_ptr(conf->percpu, cpu)->scribble = scribble; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4904 | } |
| 4905 | #ifdef CONFIG_HOTPLUG_CPU |
| 4906 | conf->cpu_notify.notifier_call = raid456_cpu_notify; |
| 4907 | conf->cpu_notify.priority = 0; |
| 4908 | if (err == 0) |
| 4909 | err = register_cpu_notifier(&conf->cpu_notify); |
| 4910 | #endif |
| 4911 | put_online_cpus(); |
| 4912 | |
| 4913 | return err; |
| 4914 | } |
| 4915 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4916 | static struct r5conf *setup_conf(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4917 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4918 | struct r5conf *conf; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4919 | int raid_disk, memory, max_disks; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4920 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4921 | struct disk_info *disk; |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 4922 | char pers_name[6]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4923 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4924 | if (mddev->new_level != 5 |
| 4925 | && mddev->new_level != 4 |
| 4926 | && mddev->new_level != 6) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4927 | 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] | 4928 | mdname(mddev), mddev->new_level); |
| 4929 | return ERR_PTR(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4930 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4931 | if ((mddev->new_level == 5 |
| 4932 | && !algorithm_valid_raid5(mddev->new_layout)) || |
| 4933 | (mddev->new_level == 6 |
| 4934 | && !algorithm_valid_raid6(mddev->new_layout))) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4935 | printk(KERN_ERR "md/raid:%s: layout %d not supported\n", |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4936 | mdname(mddev), mddev->new_layout); |
| 4937 | return ERR_PTR(-EIO); |
| 4938 | } |
| 4939 | if (mddev->new_level == 6 && mddev->raid_disks < 4) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4940 | 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] | 4941 | mdname(mddev), mddev->raid_disks); |
| 4942 | return ERR_PTR(-EINVAL); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4943 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4944 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4945 | if (!mddev->new_chunk_sectors || |
| 4946 | (mddev->new_chunk_sectors << 9) % PAGE_SIZE || |
| 4947 | !is_power_of_2(mddev->new_chunk_sectors)) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4948 | printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n", |
| 4949 | mdname(mddev), mddev->new_chunk_sectors << 9); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4950 | return ERR_PTR(-EINVAL); |
NeilBrown | 4bbf377 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4951 | } |
| 4952 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4953 | conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4954 | if (conf == NULL) |
| 4955 | goto abort; |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 4956 | spin_lock_init(&conf->device_lock); |
| 4957 | init_waitqueue_head(&conf->wait_for_stripe); |
| 4958 | init_waitqueue_head(&conf->wait_for_overlap); |
| 4959 | INIT_LIST_HEAD(&conf->handle_list); |
| 4960 | INIT_LIST_HEAD(&conf->hold_list); |
| 4961 | INIT_LIST_HEAD(&conf->delayed_list); |
| 4962 | INIT_LIST_HEAD(&conf->bitmap_list); |
| 4963 | INIT_LIST_HEAD(&conf->inactive_list); |
| 4964 | atomic_set(&conf->active_stripes, 0); |
| 4965 | atomic_set(&conf->preread_active_stripes, 0); |
| 4966 | atomic_set(&conf->active_aligned_reads, 0); |
| 4967 | conf->bypass_threshold = BYPASS_THRESHOLD; |
NeilBrown | d890fa2 | 2011-10-26 11:54:39 +1100 | [diff] [blame] | 4968 | conf->recovery_disabled = mddev->recovery_disabled - 1; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4969 | |
| 4970 | conf->raid_disks = mddev->raid_disks; |
| 4971 | if (mddev->reshape_position == MaxSector) |
| 4972 | conf->previous_raid_disks = mddev->raid_disks; |
| 4973 | else |
| 4974 | conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4975 | max_disks = max(conf->raid_disks, conf->previous_raid_disks); |
| 4976 | conf->scribble_len = scribble_len(max_disks); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4977 | |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4978 | conf->disks = kzalloc(max_disks * sizeof(struct disk_info), |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4979 | GFP_KERNEL); |
| 4980 | if (!conf->disks) |
| 4981 | goto abort; |
| 4982 | |
| 4983 | conf->mddev = mddev; |
| 4984 | |
| 4985 | if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL) |
| 4986 | goto abort; |
| 4987 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4988 | conf->level = mddev->new_level; |
| 4989 | if (raid5_alloc_percpu(conf) != 0) |
| 4990 | goto abort; |
| 4991 | |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4992 | pr_debug("raid456: run(%s) called.\n", mdname(mddev)); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4993 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 4994 | rdev_for_each(rdev, mddev) { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4995 | raid_disk = rdev->raid_disk; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4996 | if (raid_disk >= max_disks |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4997 | || raid_disk < 0) |
| 4998 | continue; |
| 4999 | disk = conf->disks + raid_disk; |
| 5000 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5001 | if (test_bit(Replacement, &rdev->flags)) { |
| 5002 | if (disk->replacement) |
| 5003 | goto abort; |
| 5004 | disk->replacement = rdev; |
| 5005 | } else { |
| 5006 | if (disk->rdev) |
| 5007 | goto abort; |
| 5008 | disk->rdev = rdev; |
| 5009 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5010 | |
| 5011 | if (test_bit(In_sync, &rdev->flags)) { |
| 5012 | char b[BDEVNAME_SIZE]; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5013 | printk(KERN_INFO "md/raid:%s: device %s operational as raid" |
| 5014 | " disk %d\n", |
| 5015 | mdname(mddev), bdevname(rdev->bdev, b), raid_disk); |
Jonathan Brassow | d6b212f | 2011-06-08 18:00:28 -0500 | [diff] [blame] | 5016 | } else if (rdev->saved_raid_disk != raid_disk) |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5017 | /* Cannot rely on bitmap to complete recovery */ |
| 5018 | conf->fullsync = 1; |
| 5019 | } |
| 5020 | |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5021 | conf->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5022 | conf->level = mddev->new_level; |
| 5023 | if (conf->level == 6) |
| 5024 | conf->max_degraded = 2; |
| 5025 | else |
| 5026 | conf->max_degraded = 1; |
| 5027 | conf->algorithm = mddev->new_layout; |
| 5028 | conf->max_nr_stripes = NR_STRIPES; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5029 | conf->reshape_progress = mddev->reshape_position; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 5030 | if (conf->reshape_progress != MaxSector) { |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5031 | conf->prev_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 5032 | conf->prev_algo = mddev->layout; |
| 5033 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5034 | |
| 5035 | memory = conf->max_nr_stripes * (sizeof(struct stripe_head) + |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 5036 | max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5037 | if (grow_stripes(conf, conf->max_nr_stripes)) { |
| 5038 | printk(KERN_ERR |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5039 | "md/raid:%s: couldn't allocate %dkB for buffers\n", |
| 5040 | mdname(mddev), memory); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5041 | goto abort; |
| 5042 | } else |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5043 | printk(KERN_INFO "md/raid:%s: allocated %dkB\n", |
| 5044 | mdname(mddev), memory); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5045 | |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 5046 | sprintf(pers_name, "raid%d", mddev->new_level); |
| 5047 | conf->thread = md_register_thread(raid5d, mddev, pers_name); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5048 | if (!conf->thread) { |
| 5049 | printk(KERN_ERR |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5050 | "md/raid:%s: couldn't allocate thread.\n", |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5051 | mdname(mddev)); |
| 5052 | goto abort; |
| 5053 | } |
| 5054 | |
| 5055 | return conf; |
| 5056 | |
| 5057 | abort: |
| 5058 | if (conf) { |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 5059 | free_conf(conf); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5060 | return ERR_PTR(-EIO); |
| 5061 | } else |
| 5062 | return ERR_PTR(-ENOMEM); |
| 5063 | } |
| 5064 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5065 | |
| 5066 | static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded) |
| 5067 | { |
| 5068 | switch (algo) { |
| 5069 | case ALGORITHM_PARITY_0: |
| 5070 | if (raid_disk < max_degraded) |
| 5071 | return 1; |
| 5072 | break; |
| 5073 | case ALGORITHM_PARITY_N: |
| 5074 | if (raid_disk >= raid_disks - max_degraded) |
| 5075 | return 1; |
| 5076 | break; |
| 5077 | case ALGORITHM_PARITY_0_6: |
| 5078 | if (raid_disk == 0 || |
| 5079 | raid_disk == raid_disks - 1) |
| 5080 | return 1; |
| 5081 | break; |
| 5082 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 5083 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 5084 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 5085 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 5086 | if (raid_disk == raid_disks - 1) |
| 5087 | return 1; |
| 5088 | } |
| 5089 | return 0; |
| 5090 | } |
| 5091 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5092 | static int run(struct mddev *mddev) |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5093 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5094 | struct r5conf *conf; |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 5095 | int working_disks = 0; |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5096 | int dirty_parity_disks = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5097 | struct md_rdev *rdev; |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5098 | sector_t reshape_offset = 0; |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5099 | int i; |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5100 | long long min_offset_diff = 0; |
| 5101 | int first = 1; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5102 | |
Andre Noll | 8c6ac868 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 5103 | if (mddev->recovery_cp != MaxSector) |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5104 | printk(KERN_NOTICE "md/raid:%s: not clean" |
Andre Noll | 8c6ac868 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 5105 | " -- starting background reconstruction\n", |
| 5106 | mdname(mddev)); |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5107 | |
| 5108 | rdev_for_each(rdev, mddev) { |
| 5109 | long long diff; |
| 5110 | if (rdev->raid_disk < 0) |
| 5111 | continue; |
| 5112 | diff = (rdev->new_data_offset - rdev->data_offset); |
| 5113 | if (first) { |
| 5114 | min_offset_diff = diff; |
| 5115 | first = 0; |
| 5116 | } else if (mddev->reshape_backwards && |
| 5117 | diff < min_offset_diff) |
| 5118 | min_offset_diff = diff; |
| 5119 | else if (!mddev->reshape_backwards && |
| 5120 | diff > min_offset_diff) |
| 5121 | min_offset_diff = diff; |
| 5122 | } |
| 5123 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5124 | if (mddev->reshape_position != MaxSector) { |
| 5125 | /* Check that we can continue the reshape. |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5126 | * Difficulties arise if the stripe we would write to |
| 5127 | * next is at or after the stripe we would read from next. |
| 5128 | * For a reshape that changes the number of devices, this |
| 5129 | * is only possible for a very short time, and mdadm makes |
| 5130 | * sure that time appears to have past before assembling |
| 5131 | * the array. So we fail if that time hasn't passed. |
| 5132 | * For a reshape that keeps the number of devices the same |
| 5133 | * mdadm must be monitoring the reshape can keeping the |
| 5134 | * critical areas read-only and backed up. It will start |
| 5135 | * the array in read-only mode, so we check for that. |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5136 | */ |
| 5137 | sector_t here_new, here_old; |
| 5138 | int old_disks; |
Andre Noll | 18b0033 | 2009-03-31 15:00:56 +1100 | [diff] [blame] | 5139 | int max_degraded = (mddev->level == 6 ? 2 : 1); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5140 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5141 | if (mddev->new_level != mddev->level) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5142 | printk(KERN_ERR "md/raid:%s: unsupported reshape " |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5143 | "required - aborting.\n", |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5144 | mdname(mddev)); |
| 5145 | return -EINVAL; |
| 5146 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5147 | old_disks = mddev->raid_disks - mddev->delta_disks; |
| 5148 | /* reshape_position must be on a new-stripe boundary, and one |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5149 | * further up in new geometry must map after here in old |
| 5150 | * geometry. |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5151 | */ |
| 5152 | here_new = mddev->reshape_position; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5153 | if (sector_div(here_new, mddev->new_chunk_sectors * |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5154 | (mddev->raid_disks - max_degraded))) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5155 | printk(KERN_ERR "md/raid:%s: reshape_position not " |
| 5156 | "on a stripe boundary\n", mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5157 | return -EINVAL; |
| 5158 | } |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5159 | reshape_offset = here_new * mddev->new_chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5160 | /* here_new is the stripe we will write to */ |
| 5161 | here_old = mddev->reshape_position; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5162 | sector_div(here_old, mddev->chunk_sectors * |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5163 | (old_disks-max_degraded)); |
| 5164 | /* here_old is the first stripe that we might need to read |
| 5165 | * from */ |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5166 | if (mddev->delta_disks == 0) { |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5167 | if ((here_new * mddev->new_chunk_sectors != |
| 5168 | here_old * mddev->chunk_sectors)) { |
| 5169 | printk(KERN_ERR "md/raid:%s: reshape position is" |
| 5170 | " confused - aborting\n", mdname(mddev)); |
| 5171 | return -EINVAL; |
| 5172 | } |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5173 | /* We cannot be sure it is safe to start an in-place |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5174 | * reshape. It is only safe if user-space is monitoring |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5175 | * and taking constant backups. |
| 5176 | * mdadm always starts a situation like this in |
| 5177 | * readonly mode so it can take control before |
| 5178 | * allowing any writes. So just check for that. |
| 5179 | */ |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5180 | if (abs(min_offset_diff) >= mddev->chunk_sectors && |
| 5181 | abs(min_offset_diff) >= mddev->new_chunk_sectors) |
| 5182 | /* not really in-place - so OK */; |
| 5183 | else if (mddev->ro == 0) { |
| 5184 | printk(KERN_ERR "md/raid:%s: in-place reshape " |
| 5185 | "must be started in read-only mode " |
| 5186 | "- aborting\n", |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5187 | mdname(mddev)); |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5188 | return -EINVAL; |
| 5189 | } |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5190 | } else if (mddev->reshape_backwards |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5191 | ? (here_new * mddev->new_chunk_sectors + min_offset_diff <= |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5192 | here_old * mddev->chunk_sectors) |
| 5193 | : (here_new * mddev->new_chunk_sectors >= |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5194 | here_old * mddev->chunk_sectors + (-min_offset_diff))) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5195 | /* Reading from the same stripe as writing to - bad */ |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5196 | printk(KERN_ERR "md/raid:%s: reshape_position too early for " |
| 5197 | "auto-recovery - aborting.\n", |
| 5198 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5199 | return -EINVAL; |
| 5200 | } |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5201 | printk(KERN_INFO "md/raid:%s: reshape will continue\n", |
| 5202 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5203 | /* OK, we should be able to continue; */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5204 | } else { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5205 | BUG_ON(mddev->level != mddev->new_level); |
| 5206 | BUG_ON(mddev->layout != mddev->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5207 | BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5208 | BUG_ON(mddev->delta_disks != 0); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5209 | } |
| 5210 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5211 | if (mddev->private == NULL) |
| 5212 | conf = setup_conf(mddev); |
| 5213 | else |
| 5214 | conf = mddev->private; |
| 5215 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5216 | if (IS_ERR(conf)) |
| 5217 | return PTR_ERR(conf); |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 5218 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5219 | conf->min_offset_diff = min_offset_diff; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5220 | mddev->thread = conf->thread; |
| 5221 | conf->thread = NULL; |
| 5222 | mddev->private = conf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5223 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5224 | for (i = 0; i < conf->raid_disks && conf->previous_raid_disks; |
| 5225 | i++) { |
| 5226 | rdev = conf->disks[i].rdev; |
| 5227 | if (!rdev && conf->disks[i].replacement) { |
| 5228 | /* The replacement is all we have yet */ |
| 5229 | rdev = conf->disks[i].replacement; |
| 5230 | conf->disks[i].replacement = NULL; |
| 5231 | clear_bit(Replacement, &rdev->flags); |
| 5232 | conf->disks[i].rdev = rdev; |
| 5233 | } |
| 5234 | if (!rdev) |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5235 | continue; |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5236 | if (conf->disks[i].replacement && |
| 5237 | conf->reshape_progress != MaxSector) { |
| 5238 | /* replacements and reshape simply do not mix. */ |
| 5239 | printk(KERN_ERR "md: cannot handle concurrent " |
| 5240 | "replacement and reshape.\n"); |
| 5241 | goto abort; |
| 5242 | } |
NeilBrown | 2f11588 | 2010-06-17 17:41:03 +1000 | [diff] [blame] | 5243 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5244 | working_disks++; |
NeilBrown | 2f11588 | 2010-06-17 17:41:03 +1000 | [diff] [blame] | 5245 | continue; |
| 5246 | } |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5247 | /* This disc is not fully in-sync. However if it |
| 5248 | * just stored parity (beyond the recovery_offset), |
| 5249 | * when we don't need to be concerned about the |
| 5250 | * array being dirty. |
| 5251 | * When reshape goes 'backwards', we never have |
| 5252 | * partially completed devices, so we only need |
| 5253 | * to worry about reshape going forwards. |
| 5254 | */ |
| 5255 | /* Hack because v0.91 doesn't store recovery_offset properly. */ |
| 5256 | if (mddev->major_version == 0 && |
| 5257 | mddev->minor_version > 90) |
| 5258 | rdev->recovery_offset = reshape_offset; |
| 5259 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5260 | if (rdev->recovery_offset < reshape_offset) { |
| 5261 | /* We need to check old and new layout */ |
| 5262 | if (!only_parity(rdev->raid_disk, |
| 5263 | conf->algorithm, |
| 5264 | conf->raid_disks, |
| 5265 | conf->max_degraded)) |
| 5266 | continue; |
| 5267 | } |
| 5268 | if (!only_parity(rdev->raid_disk, |
| 5269 | conf->prev_algo, |
| 5270 | conf->previous_raid_disks, |
| 5271 | conf->max_degraded)) |
| 5272 | continue; |
| 5273 | dirty_parity_disks++; |
| 5274 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5275 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5276 | /* |
| 5277 | * 0 for a fully functional array, 1 or 2 for a degraded array. |
| 5278 | */ |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 5279 | mddev->degraded = calc_degraded(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5280 | |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 5281 | if (has_failed(conf)) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5282 | printk(KERN_ERR "md/raid:%s: not enough operational devices" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5283 | " (%d/%d failed)\n", |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 5284 | mdname(mddev), mddev->degraded, conf->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5285 | goto abort; |
| 5286 | } |
| 5287 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5288 | /* device size must be a multiple of chunk size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5289 | mddev->dev_sectors &= ~(mddev->chunk_sectors - 1); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5290 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 5291 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5292 | if (mddev->degraded > dirty_parity_disks && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5293 | mddev->recovery_cp != MaxSector) { |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5294 | if (mddev->ok_start_degraded) |
| 5295 | printk(KERN_WARNING |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5296 | "md/raid:%s: starting dirty degraded array" |
| 5297 | " - data corruption possible.\n", |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5298 | mdname(mddev)); |
| 5299 | else { |
| 5300 | printk(KERN_ERR |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5301 | "md/raid:%s: cannot start dirty degraded array.\n", |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5302 | mdname(mddev)); |
| 5303 | goto abort; |
| 5304 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5305 | } |
| 5306 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5307 | if (mddev->degraded == 0) |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5308 | printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d" |
| 5309 | " devices, algorithm %d\n", mdname(mddev), conf->level, |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 5310 | mddev->raid_disks-mddev->degraded, mddev->raid_disks, |
| 5311 | mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5312 | else |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5313 | printk(KERN_ALERT "md/raid:%s: raid level %d active with %d" |
| 5314 | " out of %d devices, algorithm %d\n", |
| 5315 | mdname(mddev), conf->level, |
| 5316 | mddev->raid_disks - mddev->degraded, |
| 5317 | mddev->raid_disks, mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5318 | |
| 5319 | print_raid5_conf(conf); |
| 5320 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5321 | if (conf->reshape_progress != MaxSector) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5322 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5323 | atomic_set(&conf->reshape_stripes, 0); |
| 5324 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 5325 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 5326 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 5327 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 5328 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 5329 | "reshape"); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5330 | } |
| 5331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5332 | |
| 5333 | /* Ok, everything is just fine now */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 5334 | if (mddev->to_remove == &raid5_attrs_group) |
| 5335 | mddev->to_remove = NULL; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5336 | else if (mddev->kobj.sd && |
| 5337 | sysfs_create_group(&mddev->kobj, &raid5_attrs_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5338 | printk(KERN_WARNING |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 5339 | "raid5: failed to create sysfs attributes for %s\n", |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5340 | mdname(mddev)); |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 5341 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
| 5342 | |
| 5343 | if (mddev->queue) { |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 5344 | int chunk_size; |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 5345 | /* read-ahead size must cover two whole stripes, which |
| 5346 | * is 2 * (datadisks) * chunksize where 'n' is the |
| 5347 | * number of raid devices |
| 5348 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5349 | int data_disks = conf->previous_raid_disks - conf->max_degraded; |
| 5350 | int stripe = data_disks * |
| 5351 | ((mddev->chunk_sectors << 9) / PAGE_SIZE); |
| 5352 | if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 5353 | mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 5354 | |
| 5355 | blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec); |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 5356 | |
| 5357 | mddev->queue->backing_dev_info.congested_data = mddev; |
| 5358 | mddev->queue->backing_dev_info.congested_fn = raid5_congested; |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 5359 | |
| 5360 | chunk_size = mddev->chunk_sectors << 9; |
| 5361 | blk_queue_io_min(mddev->queue, chunk_size); |
| 5362 | blk_queue_io_opt(mddev->queue, chunk_size * |
| 5363 | (conf->raid_disks - conf->max_degraded)); |
| 5364 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5365 | rdev_for_each(rdev, mddev) { |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 5366 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 5367 | rdev->data_offset << 9); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5368 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 5369 | rdev->new_data_offset << 9); |
| 5370 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5371 | } |
| 5372 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5373 | return 0; |
| 5374 | abort: |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 5375 | md_unregister_thread(&mddev->thread); |
NeilBrown | e4f869d | 2011-10-07 14:22:49 +1100 | [diff] [blame] | 5376 | print_raid5_conf(conf); |
| 5377 | free_conf(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5378 | mddev->private = NULL; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5379 | 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] | 5380 | return -EIO; |
| 5381 | } |
| 5382 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5383 | static int stop(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5384 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5385 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5386 | |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 5387 | md_unregister_thread(&mddev->thread); |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 5388 | if (mddev->queue) |
| 5389 | mddev->queue->backing_dev_info.congested_fn = NULL; |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 5390 | free_conf(conf); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 5391 | mddev->private = NULL; |
| 5392 | mddev->to_remove = &raid5_attrs_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5393 | return 0; |
| 5394 | } |
| 5395 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5396 | static void status(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5397 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5398 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5399 | int i; |
| 5400 | |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5401 | seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level, |
| 5402 | mddev->chunk_sectors / 2, mddev->layout); |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 5403 | 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] | 5404 | for (i = 0; i < conf->raid_disks; i++) |
| 5405 | seq_printf (seq, "%s", |
| 5406 | conf->disks[i].rdev && |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5407 | test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5408 | seq_printf (seq, "]"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5409 | } |
| 5410 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5411 | static void print_raid5_conf (struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5412 | { |
| 5413 | int i; |
| 5414 | struct disk_info *tmp; |
| 5415 | |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5416 | printk(KERN_DEBUG "RAID conf printout:\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5417 | if (!conf) { |
| 5418 | printk("(conf==NULL)\n"); |
| 5419 | return; |
| 5420 | } |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5421 | printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level, |
| 5422 | conf->raid_disks, |
| 5423 | conf->raid_disks - conf->mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5424 | |
| 5425 | for (i = 0; i < conf->raid_disks; i++) { |
| 5426 | char b[BDEVNAME_SIZE]; |
| 5427 | tmp = conf->disks + i; |
| 5428 | if (tmp->rdev) |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5429 | printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n", |
| 5430 | i, !test_bit(Faulty, &tmp->rdev->flags), |
| 5431 | bdevname(tmp->rdev->bdev, b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5432 | } |
| 5433 | } |
| 5434 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5435 | static int raid5_spare_active(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5436 | { |
| 5437 | int i; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5438 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5439 | struct disk_info *tmp; |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5440 | int count = 0; |
| 5441 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5442 | |
| 5443 | for (i = 0; i < conf->raid_disks; i++) { |
| 5444 | tmp = conf->disks + i; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5445 | if (tmp->replacement |
| 5446 | && tmp->replacement->recovery_offset == MaxSector |
| 5447 | && !test_bit(Faulty, &tmp->replacement->flags) |
| 5448 | && !test_and_set_bit(In_sync, &tmp->replacement->flags)) { |
| 5449 | /* Replacement has just become active. */ |
| 5450 | if (!tmp->rdev |
| 5451 | || !test_and_clear_bit(In_sync, &tmp->rdev->flags)) |
| 5452 | count++; |
| 5453 | if (tmp->rdev) { |
| 5454 | /* Replaced device not technically faulty, |
| 5455 | * but we need to be sure it gets removed |
| 5456 | * and never re-added. |
| 5457 | */ |
| 5458 | set_bit(Faulty, &tmp->rdev->flags); |
| 5459 | sysfs_notify_dirent_safe( |
| 5460 | tmp->rdev->sysfs_state); |
| 5461 | } |
| 5462 | sysfs_notify_dirent_safe(tmp->replacement->sysfs_state); |
| 5463 | } else if (tmp->rdev |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 5464 | && tmp->rdev->recovery_offset == MaxSector |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5465 | && !test_bit(Faulty, &tmp->rdev->flags) |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 5466 | && !test_and_set_bit(In_sync, &tmp->rdev->flags)) { |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5467 | count++; |
Jonathan Brassow | 43c73ca | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5468 | sysfs_notify_dirent_safe(tmp->rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5469 | } |
| 5470 | } |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5471 | spin_lock_irqsave(&conf->device_lock, flags); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 5472 | mddev->degraded = calc_degraded(conf); |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5473 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5474 | print_raid5_conf(conf); |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5475 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5476 | } |
| 5477 | |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 5478 | static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5479 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5480 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5481 | int err = 0; |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 5482 | int number = rdev->raid_disk; |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5483 | struct md_rdev **rdevp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5484 | struct disk_info *p = conf->disks + number; |
| 5485 | |
| 5486 | print_raid5_conf(conf); |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5487 | if (rdev == p->rdev) |
| 5488 | rdevp = &p->rdev; |
| 5489 | else if (rdev == p->replacement) |
| 5490 | rdevp = &p->replacement; |
| 5491 | else |
| 5492 | return 0; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5493 | |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5494 | if (number >= conf->raid_disks && |
| 5495 | conf->reshape_progress == MaxSector) |
| 5496 | clear_bit(In_sync, &rdev->flags); |
| 5497 | |
| 5498 | if (test_bit(In_sync, &rdev->flags) || |
| 5499 | atomic_read(&rdev->nr_pending)) { |
| 5500 | err = -EBUSY; |
| 5501 | goto abort; |
| 5502 | } |
| 5503 | /* Only remove non-faulty devices if recovery |
| 5504 | * isn't possible. |
| 5505 | */ |
| 5506 | if (!test_bit(Faulty, &rdev->flags) && |
| 5507 | mddev->recovery_disabled != conf->recovery_disabled && |
| 5508 | !has_failed(conf) && |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5509 | (!p->replacement || p->replacement == rdev) && |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5510 | number < conf->raid_disks) { |
| 5511 | err = -EBUSY; |
| 5512 | goto abort; |
| 5513 | } |
| 5514 | *rdevp = NULL; |
| 5515 | synchronize_rcu(); |
| 5516 | if (atomic_read(&rdev->nr_pending)) { |
| 5517 | /* lost the race, try later */ |
| 5518 | err = -EBUSY; |
| 5519 | *rdevp = rdev; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5520 | } else if (p->replacement) { |
| 5521 | /* We must have just cleared 'rdev' */ |
| 5522 | p->rdev = p->replacement; |
| 5523 | clear_bit(Replacement, &p->replacement->flags); |
| 5524 | smp_mb(); /* Make sure other CPUs may see both as identical |
| 5525 | * but will never see neither - if they are careful |
| 5526 | */ |
| 5527 | p->replacement = NULL; |
| 5528 | clear_bit(WantReplacement, &rdev->flags); |
| 5529 | } else |
| 5530 | /* We might have just removed the Replacement as faulty- |
| 5531 | * clear the bit just in case |
| 5532 | */ |
| 5533 | clear_bit(WantReplacement, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5534 | abort: |
| 5535 | |
| 5536 | print_raid5_conf(conf); |
| 5537 | return err; |
| 5538 | } |
| 5539 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5540 | static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5541 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5542 | struct r5conf *conf = mddev->private; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5543 | int err = -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5544 | int disk; |
| 5545 | struct disk_info *p; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5546 | int first = 0; |
| 5547 | int last = conf->raid_disks - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5548 | |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 5549 | if (mddev->recovery_disabled == conf->recovery_disabled) |
| 5550 | return -EBUSY; |
| 5551 | |
NeilBrown | dc10c64 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 5552 | if (rdev->saved_raid_disk < 0 && has_failed(conf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5553 | /* no point adding a device */ |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5554 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5555 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5556 | if (rdev->raid_disk >= 0) |
| 5557 | first = last = rdev->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5558 | |
| 5559 | /* |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5560 | * find the disk ... but prefer rdev->saved_raid_disk |
| 5561 | * if possible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5562 | */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5563 | if (rdev->saved_raid_disk >= 0 && |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5564 | rdev->saved_raid_disk >= first && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5565 | conf->disks[rdev->saved_raid_disk].rdev == NULL) |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 5566 | first = rdev->saved_raid_disk; |
| 5567 | |
| 5568 | for (disk = first; disk <= last; disk++) { |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5569 | p = conf->disks + disk; |
| 5570 | if (p->rdev == NULL) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5571 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5572 | rdev->raid_disk = disk; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5573 | err = 0; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5574 | if (rdev->saved_raid_disk != disk) |
| 5575 | conf->fullsync = 1; |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 5576 | rcu_assign_pointer(p->rdev, rdev); |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 5577 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5578 | } |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 5579 | } |
| 5580 | for (disk = first; disk <= last; disk++) { |
| 5581 | p = conf->disks + disk; |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5582 | if (test_bit(WantReplacement, &p->rdev->flags) && |
| 5583 | p->replacement == NULL) { |
| 5584 | clear_bit(In_sync, &rdev->flags); |
| 5585 | set_bit(Replacement, &rdev->flags); |
| 5586 | rdev->raid_disk = disk; |
| 5587 | err = 0; |
| 5588 | conf->fullsync = 1; |
| 5589 | rcu_assign_pointer(p->replacement, rdev); |
| 5590 | break; |
| 5591 | } |
| 5592 | } |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 5593 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5594 | print_raid5_conf(conf); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5595 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5596 | } |
| 5597 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5598 | static int raid5_resize(struct mddev *mddev, sector_t sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5599 | { |
| 5600 | /* no resync is happening, and there is enough space |
| 5601 | * on all devices, so we can resize. |
| 5602 | * We need to make sure resync covers any new space. |
| 5603 | * If the array is shrinking we should possibly wait until |
| 5604 | * any io in the removed space completes, but it hardly seems |
| 5605 | * worth it. |
| 5606 | */ |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 5607 | sector_t newsize; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5608 | sectors &= ~((sector_t)mddev->chunk_sectors - 1); |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 5609 | newsize = raid5_size(mddev, sectors, mddev->raid_disks); |
| 5610 | if (mddev->external_size && |
| 5611 | mddev->array_sectors > newsize) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5612 | return -EINVAL; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 5613 | if (mddev->bitmap) { |
| 5614 | int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0); |
| 5615 | if (ret) |
| 5616 | return ret; |
| 5617 | } |
| 5618 | md_set_array_sectors(mddev, newsize); |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 5619 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5620 | revalidate_disk(mddev->gendisk); |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 5621 | if (sectors > mddev->dev_sectors && |
| 5622 | mddev->recovery_cp > mddev->dev_sectors) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5623 | mddev->recovery_cp = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5624 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5625 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5626 | mddev->dev_sectors = sectors; |
NeilBrown | 4b5c7ae | 2005-07-27 11:43:28 -0700 | [diff] [blame] | 5627 | mddev->resync_max_sectors = sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5628 | return 0; |
| 5629 | } |
| 5630 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5631 | static int check_stripe_cache(struct mddev *mddev) |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5632 | { |
| 5633 | /* Can only proceed if there are plenty of stripe_heads. |
| 5634 | * We need a minimum of one full stripe,, and for sensible progress |
| 5635 | * it is best to have about 4 times that. |
| 5636 | * If we require 4 times, then the default 256 4K stripe_heads will |
| 5637 | * allow for chunk sizes up to 256K, which is probably OK. |
| 5638 | * If the chunk size is greater, user-space should request more |
| 5639 | * stripe_heads first. |
| 5640 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5641 | struct r5conf *conf = mddev->private; |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5642 | if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4 |
| 5643 | > conf->max_nr_stripes || |
| 5644 | ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4 |
| 5645 | > conf->max_nr_stripes) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5646 | printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n", |
| 5647 | mdname(mddev), |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5648 | ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9) |
| 5649 | / STRIPE_SIZE)*4); |
| 5650 | return 0; |
| 5651 | } |
| 5652 | return 1; |
| 5653 | } |
| 5654 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5655 | static int check_reshape(struct mddev *mddev) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5656 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5657 | struct r5conf *conf = mddev->private; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5658 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5659 | if (mddev->delta_disks == 0 && |
| 5660 | mddev->new_layout == mddev->layout && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5661 | mddev->new_chunk_sectors == mddev->chunk_sectors) |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5662 | return 0; /* nothing to do */ |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 5663 | if (has_failed(conf)) |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5664 | return -EINVAL; |
| 5665 | if (mddev->delta_disks < 0) { |
| 5666 | /* We might be able to shrink, but the devices must |
| 5667 | * be made bigger first. |
| 5668 | * For raid6, 4 is the minimum size. |
| 5669 | * Otherwise 2 is the minimum |
| 5670 | */ |
| 5671 | int min = 2; |
| 5672 | if (mddev->level == 6) |
| 5673 | min = 4; |
| 5674 | if (mddev->raid_disks + mddev->delta_disks < min) |
| 5675 | return -EINVAL; |
| 5676 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5677 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5678 | if (!check_stripe_cache(mddev)) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5679 | return -ENOSPC; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5680 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5681 | return resize_stripes(conf, conf->raid_disks + mddev->delta_disks); |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5682 | } |
| 5683 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5684 | static int raid5_start_reshape(struct mddev *mddev) |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5685 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5686 | struct r5conf *conf = mddev->private; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5687 | struct md_rdev *rdev; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5688 | int spares = 0; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 5689 | unsigned long flags; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5690 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5691 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5692 | return -EBUSY; |
| 5693 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5694 | if (!check_stripe_cache(mddev)) |
| 5695 | return -ENOSPC; |
| 5696 | |
NeilBrown | 30b6764 | 2012-05-22 13:55:28 +1000 | [diff] [blame] | 5697 | if (has_failed(conf)) |
| 5698 | return -EINVAL; |
| 5699 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5700 | rdev_for_each(rdev, mddev) { |
NeilBrown | 469518a | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5701 | if (!test_bit(In_sync, &rdev->flags) |
| 5702 | && !test_bit(Faulty, &rdev->flags)) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5703 | spares++; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5704 | } |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5705 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5706 | if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5707 | /* Not enough devices even to make a degraded array |
| 5708 | * of that size |
| 5709 | */ |
| 5710 | return -EINVAL; |
| 5711 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5712 | /* Refuse to reduce size of the array. Any reductions in |
| 5713 | * array size must be through explicit setting of array_size |
| 5714 | * attribute. |
| 5715 | */ |
| 5716 | if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks) |
| 5717 | < mddev->array_sectors) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5718 | printk(KERN_ERR "md/raid:%s: array size must be reduced " |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5719 | "before number of disks\n", mdname(mddev)); |
| 5720 | return -EINVAL; |
| 5721 | } |
| 5722 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5723 | atomic_set(&conf->reshape_stripes, 0); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5724 | spin_lock_irq(&conf->device_lock); |
| 5725 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5726 | conf->raid_disks += mddev->delta_disks; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5727 | conf->prev_chunk_sectors = conf->chunk_sectors; |
| 5728 | conf->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5729 | conf->prev_algo = conf->algorithm; |
| 5730 | conf->algorithm = mddev->new_layout; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5731 | conf->generation++; |
| 5732 | /* Code that selects data_offset needs to see the generation update |
| 5733 | * if reshape_progress has been set - so a memory barrier needed. |
| 5734 | */ |
| 5735 | smp_mb(); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5736 | if (mddev->reshape_backwards) |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5737 | conf->reshape_progress = raid5_size(mddev, 0, 0); |
| 5738 | else |
| 5739 | conf->reshape_progress = 0; |
| 5740 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5741 | spin_unlock_irq(&conf->device_lock); |
| 5742 | |
| 5743 | /* Add some new drives, as many as will fit. |
| 5744 | * We know there are enough to make the newly sized array work. |
NeilBrown | 3424bf6 | 2010-06-17 17:48:26 +1000 | [diff] [blame] | 5745 | * Don't add devices if we are reducing the number of |
| 5746 | * devices in the array. This is because it is not possible |
| 5747 | * to correctly record the "partially reconstructed" state of |
| 5748 | * such devices during the reshape and confusion could result. |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5749 | */ |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5750 | if (mddev->delta_disks >= 0) { |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5751 | rdev_for_each(rdev, mddev) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5752 | if (rdev->raid_disk < 0 && |
| 5753 | !test_bit(Faulty, &rdev->flags)) { |
| 5754 | if (raid5_add_disk(mddev, rdev) == 0) { |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5755 | if (rdev->raid_disk |
NeilBrown | 9d4c7d8 | 2012-03-13 11:21:21 +1100 | [diff] [blame] | 5756 | >= conf->previous_raid_disks) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5757 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 9d4c7d8 | 2012-03-13 11:21:21 +1100 | [diff] [blame] | 5758 | else |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5759 | rdev->recovery_offset = 0; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5760 | |
| 5761 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5762 | /* Failure here is OK */; |
NeilBrown | 50da084 | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5763 | } |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5764 | } else if (rdev->raid_disk >= conf->previous_raid_disks |
| 5765 | && !test_bit(Faulty, &rdev->flags)) { |
| 5766 | /* This is a spare that was manually added */ |
| 5767 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5768 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5769 | |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5770 | /* When a reshape changes the number of devices, |
| 5771 | * ->degraded is measured against the larger of the |
| 5772 | * pre and post number of devices. |
| 5773 | */ |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5774 | spin_lock_irqsave(&conf->device_lock, flags); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 5775 | mddev->degraded = calc_degraded(conf); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5776 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 5777 | } |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5778 | mddev->raid_disks = conf->raid_disks; |
NeilBrown | e516402 | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 5779 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5780 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5781 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5782 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 5783 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 5784 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 5785 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 5786 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 5787 | "reshape"); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5788 | if (!mddev->sync_thread) { |
| 5789 | mddev->recovery = 0; |
| 5790 | spin_lock_irq(&conf->device_lock); |
| 5791 | mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5792 | rdev_for_each(rdev, mddev) |
| 5793 | rdev->new_data_offset = rdev->data_offset; |
| 5794 | smp_wmb(); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5795 | conf->reshape_progress = MaxSector; |
NeilBrown | 1e3fa9b | 2012-03-13 11:21:18 +1100 | [diff] [blame] | 5796 | mddev->reshape_position = MaxSector; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5797 | spin_unlock_irq(&conf->device_lock); |
| 5798 | return -EAGAIN; |
| 5799 | } |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5800 | conf->reshape_checkpoint = jiffies; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5801 | md_wakeup_thread(mddev->sync_thread); |
| 5802 | md_new_event(mddev); |
| 5803 | return 0; |
| 5804 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5805 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5806 | /* This is called from the reshape thread and should make any |
| 5807 | * changes needed in 'conf' |
| 5808 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5809 | static void end_reshape(struct r5conf *conf) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5810 | { |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5811 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5812 | if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) { |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5813 | struct md_rdev *rdev; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 5814 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5815 | spin_lock_irq(&conf->device_lock); |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5816 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5817 | rdev_for_each(rdev, conf->mddev) |
| 5818 | rdev->data_offset = rdev->new_data_offset; |
| 5819 | smp_wmb(); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5820 | conf->reshape_progress = MaxSector; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5821 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 5822 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5823 | |
| 5824 | /* read-ahead size must cover two whole stripes, which is |
| 5825 | * 2 * (datadisks) * chunksize where 'n' is the number of raid devices |
| 5826 | */ |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 5827 | if (conf->mddev->queue) { |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5828 | int data_disks = conf->raid_disks - conf->max_degraded; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5829 | int stripe = data_disks * ((conf->chunk_sectors << 9) |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5830 | / PAGE_SIZE); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5831 | if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 5832 | conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
| 5833 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5834 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5835 | } |
| 5836 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5837 | /* This is called from the raid5d thread with mddev_lock held. |
| 5838 | * It makes config changes to the device. |
| 5839 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5840 | static void raid5_finish_reshape(struct mddev *mddev) |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5841 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5842 | struct r5conf *conf = mddev->private; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5843 | |
| 5844 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 5845 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5846 | if (mddev->delta_disks > 0) { |
| 5847 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
| 5848 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5849 | revalidate_disk(mddev->gendisk); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5850 | } else { |
| 5851 | int d; |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 5852 | spin_lock_irq(&conf->device_lock); |
| 5853 | mddev->degraded = calc_degraded(conf); |
| 5854 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5855 | for (d = conf->raid_disks ; |
| 5856 | d < conf->raid_disks - mddev->delta_disks; |
NeilBrown | 1a67dde | 2009-08-13 10:41:49 +1000 | [diff] [blame] | 5857 | d++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5858 | struct md_rdev *rdev = conf->disks[d].rdev; |
NeilBrown | da7613b | 2012-05-22 13:55:33 +1000 | [diff] [blame] | 5859 | if (rdev) |
| 5860 | clear_bit(In_sync, &rdev->flags); |
| 5861 | rdev = conf->disks[d].replacement; |
| 5862 | if (rdev) |
| 5863 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 1a67dde | 2009-08-13 10:41:49 +1000 | [diff] [blame] | 5864 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5865 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5866 | mddev->layout = conf->algorithm; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5867 | mddev->chunk_sectors = conf->chunk_sectors; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5868 | mddev->reshape_position = MaxSector; |
| 5869 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5870 | mddev->reshape_backwards = 0; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5871 | } |
| 5872 | } |
| 5873 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5874 | static void raid5_quiesce(struct mddev *mddev, int state) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5875 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5876 | struct r5conf *conf = mddev->private; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5877 | |
| 5878 | switch(state) { |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5879 | case 2: /* resume for a suspend */ |
| 5880 | wake_up(&conf->wait_for_overlap); |
| 5881 | break; |
| 5882 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5883 | case 1: /* stop all writes */ |
| 5884 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5885 | /* '2' tells resync/reshape to pause so that all |
| 5886 | * active stripes can drain |
| 5887 | */ |
| 5888 | conf->quiesce = 2; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5889 | wait_event_lock_irq(conf->wait_for_stripe, |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5890 | atomic_read(&conf->active_stripes) == 0 && |
| 5891 | atomic_read(&conf->active_aligned_reads) == 0, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5892 | conf->device_lock, /* nothing */); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5893 | conf->quiesce = 1; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5894 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5895 | /* allow reshape to continue */ |
| 5896 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5897 | break; |
| 5898 | |
| 5899 | case 0: /* re-enable writes */ |
| 5900 | spin_lock_irq(&conf->device_lock); |
| 5901 | conf->quiesce = 0; |
| 5902 | wake_up(&conf->wait_for_stripe); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5903 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5904 | spin_unlock_irq(&conf->device_lock); |
| 5905 | break; |
| 5906 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5907 | } |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5908 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5909 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5910 | static void *raid45_takeover_raid0(struct mddev *mddev, int level) |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 5911 | { |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 5912 | struct r0conf *raid0_conf = mddev->private; |
Randy Dunlap | d76c842 | 2011-04-21 09:07:26 -0700 | [diff] [blame] | 5913 | sector_t sectors; |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 5914 | |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5915 | /* for raid0 takeover only one zone is supported */ |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 5916 | if (raid0_conf->nr_strip_zones > 1) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5917 | printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n", |
| 5918 | mdname(mddev)); |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5919 | return ERR_PTR(-EINVAL); |
| 5920 | } |
| 5921 | |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 5922 | sectors = raid0_conf->strip_zone[0].zone_end; |
| 5923 | sector_div(sectors, raid0_conf->strip_zone[0].nb_dev); |
NeilBrown | 3b71bd9 | 2011-04-20 15:38:18 +1000 | [diff] [blame] | 5924 | mddev->dev_sectors = sectors; |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5925 | mddev->new_level = level; |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 5926 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 5927 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
| 5928 | mddev->raid_disks += 1; |
| 5929 | mddev->delta_disks = 1; |
| 5930 | /* make sure it will be not marked as dirty */ |
| 5931 | mddev->recovery_cp = MaxSector; |
| 5932 | |
| 5933 | return setup_conf(mddev); |
| 5934 | } |
| 5935 | |
| 5936 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5937 | static void *raid5_takeover_raid1(struct mddev *mddev) |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5938 | { |
| 5939 | int chunksect; |
| 5940 | |
| 5941 | if (mddev->raid_disks != 2 || |
| 5942 | mddev->degraded > 1) |
| 5943 | return ERR_PTR(-EINVAL); |
| 5944 | |
| 5945 | /* Should check if there are write-behind devices? */ |
| 5946 | |
| 5947 | chunksect = 64*2; /* 64K by default */ |
| 5948 | |
| 5949 | /* The array must be an exact multiple of chunksize */ |
| 5950 | while (chunksect && (mddev->array_sectors & (chunksect-1))) |
| 5951 | chunksect >>= 1; |
| 5952 | |
| 5953 | if ((chunksect<<9) < STRIPE_SIZE) |
| 5954 | /* array size does not allow a suitable chunk size */ |
| 5955 | return ERR_PTR(-EINVAL); |
| 5956 | |
| 5957 | mddev->new_level = 5; |
| 5958 | mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5959 | mddev->new_chunk_sectors = chunksect; |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5960 | |
| 5961 | return setup_conf(mddev); |
| 5962 | } |
| 5963 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5964 | static void *raid5_takeover_raid6(struct mddev *mddev) |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 5965 | { |
| 5966 | int new_layout; |
| 5967 | |
| 5968 | switch (mddev->layout) { |
| 5969 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 5970 | new_layout = ALGORITHM_LEFT_ASYMMETRIC; |
| 5971 | break; |
| 5972 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 5973 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC; |
| 5974 | break; |
| 5975 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 5976 | new_layout = ALGORITHM_LEFT_SYMMETRIC; |
| 5977 | break; |
| 5978 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 5979 | new_layout = ALGORITHM_RIGHT_SYMMETRIC; |
| 5980 | break; |
| 5981 | case ALGORITHM_PARITY_0_6: |
| 5982 | new_layout = ALGORITHM_PARITY_0; |
| 5983 | break; |
| 5984 | case ALGORITHM_PARITY_N: |
| 5985 | new_layout = ALGORITHM_PARITY_N; |
| 5986 | break; |
| 5987 | default: |
| 5988 | return ERR_PTR(-EINVAL); |
| 5989 | } |
| 5990 | mddev->new_level = 5; |
| 5991 | mddev->new_layout = new_layout; |
| 5992 | mddev->delta_disks = -1; |
| 5993 | mddev->raid_disks -= 1; |
| 5994 | return setup_conf(mddev); |
| 5995 | } |
| 5996 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5997 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5998 | static int raid5_check_reshape(struct mddev *mddev) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5999 | { |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6000 | /* For a 2-drive array, the layout and chunk size can be changed |
| 6001 | * immediately as not restriping is needed. |
| 6002 | * For larger arrays we record the new value - after validation |
| 6003 | * to be used by a reshape pass. |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6004 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6005 | struct r5conf *conf = mddev->private; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6006 | int new_chunk = mddev->new_chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6007 | |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6008 | if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6009 | return -EINVAL; |
| 6010 | if (new_chunk > 0) { |
Andre Noll | 0ba459d | 2009-06-18 08:46:10 +1000 | [diff] [blame] | 6011 | if (!is_power_of_2(new_chunk)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6012 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6013 | if (new_chunk < (PAGE_SIZE>>9)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6014 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6015 | if (mddev->array_sectors & (new_chunk-1)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6016 | /* not factor of array size */ |
| 6017 | return -EINVAL; |
| 6018 | } |
| 6019 | |
| 6020 | /* They look valid */ |
| 6021 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6022 | if (mddev->raid_disks == 2) { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6023 | /* can make the change immediately */ |
| 6024 | if (mddev->new_layout >= 0) { |
| 6025 | conf->algorithm = mddev->new_layout; |
| 6026 | mddev->layout = mddev->new_layout; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6027 | } |
| 6028 | if (new_chunk > 0) { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6029 | conf->chunk_sectors = new_chunk ; |
| 6030 | mddev->chunk_sectors = new_chunk; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6031 | } |
| 6032 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 6033 | md_wakeup_thread(mddev->thread); |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6034 | } |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6035 | return check_reshape(mddev); |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6036 | } |
| 6037 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6038 | static int raid6_check_reshape(struct mddev *mddev) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6039 | { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6040 | int new_chunk = mddev->new_chunk_sectors; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6041 | |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6042 | if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6043 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6044 | if (new_chunk > 0) { |
Andre Noll | 0ba459d | 2009-06-18 08:46:10 +1000 | [diff] [blame] | 6045 | if (!is_power_of_2(new_chunk)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6046 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6047 | if (new_chunk < (PAGE_SIZE >> 9)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6048 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6049 | if (mddev->array_sectors & (new_chunk-1)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6050 | /* not factor of array size */ |
| 6051 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6052 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6053 | |
| 6054 | /* They look valid */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6055 | return check_reshape(mddev); |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6056 | } |
| 6057 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6058 | static void *raid5_takeover(struct mddev *mddev) |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6059 | { |
| 6060 | /* raid5 can take over: |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6061 | * raid0 - if there is only one strip zone - make it a raid4 layout |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6062 | * raid1 - if there are two drives. We need to know the chunk size |
| 6063 | * raid4 - trivial - just use a raid4 layout. |
| 6064 | * raid6 - Providing it is a *_6 layout |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6065 | */ |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6066 | if (mddev->level == 0) |
| 6067 | return raid45_takeover_raid0(mddev, 5); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6068 | if (mddev->level == 1) |
| 6069 | return raid5_takeover_raid1(mddev); |
NeilBrown | e9d4758 | 2009-03-31 14:57:09 +1100 | [diff] [blame] | 6070 | if (mddev->level == 4) { |
| 6071 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 6072 | mddev->new_level = 5; |
| 6073 | return setup_conf(mddev); |
| 6074 | } |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 6075 | if (mddev->level == 6) |
| 6076 | return raid5_takeover_raid6(mddev); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6077 | |
| 6078 | return ERR_PTR(-EINVAL); |
| 6079 | } |
| 6080 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6081 | static void *raid4_takeover(struct mddev *mddev) |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 6082 | { |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6083 | /* raid4 can take over: |
| 6084 | * raid0 - if there is only one strip zone |
| 6085 | * raid5 - if layout is right |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 6086 | */ |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6087 | if (mddev->level == 0) |
| 6088 | return raid45_takeover_raid0(mddev, 4); |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 6089 | if (mddev->level == 5 && |
| 6090 | mddev->layout == ALGORITHM_PARITY_N) { |
| 6091 | mddev->new_layout = 0; |
| 6092 | mddev->new_level = 4; |
| 6093 | return setup_conf(mddev); |
| 6094 | } |
| 6095 | return ERR_PTR(-EINVAL); |
| 6096 | } |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6097 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6098 | static struct md_personality raid5_personality; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6099 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6100 | static void *raid6_takeover(struct mddev *mddev) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6101 | { |
| 6102 | /* Currently can only take over a raid5. We map the |
| 6103 | * personality to an equivalent raid6 personality |
| 6104 | * with the Q block at the end. |
| 6105 | */ |
| 6106 | int new_layout; |
| 6107 | |
| 6108 | if (mddev->pers != &raid5_personality) |
| 6109 | return ERR_PTR(-EINVAL); |
| 6110 | if (mddev->degraded > 1) |
| 6111 | return ERR_PTR(-EINVAL); |
| 6112 | if (mddev->raid_disks > 253) |
| 6113 | return ERR_PTR(-EINVAL); |
| 6114 | if (mddev->raid_disks < 3) |
| 6115 | return ERR_PTR(-EINVAL); |
| 6116 | |
| 6117 | switch (mddev->layout) { |
| 6118 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 6119 | new_layout = ALGORITHM_LEFT_ASYMMETRIC_6; |
| 6120 | break; |
| 6121 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 6122 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6; |
| 6123 | break; |
| 6124 | case ALGORITHM_LEFT_SYMMETRIC: |
| 6125 | new_layout = ALGORITHM_LEFT_SYMMETRIC_6; |
| 6126 | break; |
| 6127 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 6128 | new_layout = ALGORITHM_RIGHT_SYMMETRIC_6; |
| 6129 | break; |
| 6130 | case ALGORITHM_PARITY_0: |
| 6131 | new_layout = ALGORITHM_PARITY_0_6; |
| 6132 | break; |
| 6133 | case ALGORITHM_PARITY_N: |
| 6134 | new_layout = ALGORITHM_PARITY_N; |
| 6135 | break; |
| 6136 | default: |
| 6137 | return ERR_PTR(-EINVAL); |
| 6138 | } |
| 6139 | mddev->new_level = 6; |
| 6140 | mddev->new_layout = new_layout; |
| 6141 | mddev->delta_disks = 1; |
| 6142 | mddev->raid_disks += 1; |
| 6143 | return setup_conf(mddev); |
| 6144 | } |
| 6145 | |
| 6146 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6147 | static struct md_personality raid6_personality = |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6148 | { |
| 6149 | .name = "raid6", |
| 6150 | .level = 6, |
| 6151 | .owner = THIS_MODULE, |
| 6152 | .make_request = make_request, |
| 6153 | .run = run, |
| 6154 | .stop = stop, |
| 6155 | .status = status, |
| 6156 | .error_handler = error, |
| 6157 | .hot_add_disk = raid5_add_disk, |
| 6158 | .hot_remove_disk= raid5_remove_disk, |
| 6159 | .spare_active = raid5_spare_active, |
| 6160 | .sync_request = sync_request, |
| 6161 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6162 | .size = raid5_size, |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6163 | .check_reshape = raid6_check_reshape, |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 6164 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6165 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6166 | .quiesce = raid5_quiesce, |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6167 | .takeover = raid6_takeover, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6168 | }; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6169 | static struct md_personality raid5_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6170 | { |
| 6171 | .name = "raid5", |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6172 | .level = 5, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6173 | .owner = THIS_MODULE, |
| 6174 | .make_request = make_request, |
| 6175 | .run = run, |
| 6176 | .stop = stop, |
| 6177 | .status = status, |
| 6178 | .error_handler = error, |
| 6179 | .hot_add_disk = raid5_add_disk, |
| 6180 | .hot_remove_disk= raid5_remove_disk, |
| 6181 | .spare_active = raid5_spare_active, |
| 6182 | .sync_request = sync_request, |
| 6183 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6184 | .size = raid5_size, |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6185 | .check_reshape = raid5_check_reshape, |
| 6186 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6187 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6188 | .quiesce = raid5_quiesce, |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6189 | .takeover = raid5_takeover, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6190 | }; |
| 6191 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6192 | static struct md_personality raid4_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6193 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6194 | .name = "raid4", |
| 6195 | .level = 4, |
| 6196 | .owner = THIS_MODULE, |
| 6197 | .make_request = make_request, |
| 6198 | .run = run, |
| 6199 | .stop = stop, |
| 6200 | .status = status, |
| 6201 | .error_handler = error, |
| 6202 | .hot_add_disk = raid5_add_disk, |
| 6203 | .hot_remove_disk= raid5_remove_disk, |
| 6204 | .spare_active = raid5_spare_active, |
| 6205 | .sync_request = sync_request, |
| 6206 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6207 | .size = raid5_size, |
NeilBrown | 3d37890 | 2007-03-26 21:32:13 -0800 | [diff] [blame] | 6208 | .check_reshape = raid5_check_reshape, |
| 6209 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6210 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6211 | .quiesce = raid5_quiesce, |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 6212 | .takeover = raid4_takeover, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6213 | }; |
| 6214 | |
| 6215 | static int __init raid5_init(void) |
| 6216 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6217 | register_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6218 | register_md_personality(&raid5_personality); |
| 6219 | register_md_personality(&raid4_personality); |
| 6220 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6221 | } |
| 6222 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6223 | static void raid5_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6224 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6225 | unregister_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6226 | unregister_md_personality(&raid5_personality); |
| 6227 | unregister_md_personality(&raid4_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6228 | } |
| 6229 | |
| 6230 | module_init(raid5_init); |
| 6231 | module_exit(raid5_exit); |
| 6232 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 6233 | MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6234 | MODULE_ALIAS("md-personality-4"); /* RAID5 */ |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 6235 | MODULE_ALIAS("md-raid5"); |
| 6236 | MODULE_ALIAS("md-raid4"); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6237 | MODULE_ALIAS("md-level-5"); |
| 6238 | MODULE_ALIAS("md-level-4"); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6239 | MODULE_ALIAS("md-personality-8"); /* RAID6 */ |
| 6240 | MODULE_ALIAS("md-raid6"); |
| 6241 | MODULE_ALIAS("md-level-6"); |
| 6242 | |
| 6243 | /* This used to be two separate modules, they were: */ |
| 6244 | MODULE_ALIAS("raid5"); |
| 6245 | MODULE_ALIAS("raid6"); |