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)) { |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 199 | if (test_bit(STRIPE_DELAYED, &sh->state)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | list_add_tail(&sh->lru, &conf->delayed_list); |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 201 | else if (test_bit(STRIPE_BIT_DELAY, &sh->state) && |
| 202 | sh->bm_seq - conf->seq_write > 0) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 203 | list_add_tail(&sh->lru, &conf->bitmap_list); |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 204 | else { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 205 | clear_bit(STRIPE_BIT_DELAY, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | list_add_tail(&sh->lru, &conf->handle_list); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 207 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | md_wakeup_thread(conf->mddev->thread); |
| 209 | } else { |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 210 | BUG_ON(stripe_operations_active(sh)); |
majianpeng | 41fe75f | 2012-03-13 11:21:25 +1100 | [diff] [blame] | 211 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 212 | if (atomic_dec_return(&conf->preread_active_stripes) |
| 213 | < IO_THRESHOLD) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | md_wakeup_thread(conf->mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | atomic_dec(&conf->active_stripes); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 216 | if (!test_bit(STRIPE_EXPANDING, &sh->state)) { |
| 217 | list_add_tail(&sh->lru, &conf->inactive_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | wake_up(&conf->wait_for_stripe); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 219 | if (conf->retry_read_aligned) |
| 220 | md_wakeup_thread(conf->mddev->thread); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 221 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | } |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 225 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | static void release_stripe(struct stripe_head *sh) |
| 227 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 228 | struct r5conf *conf = sh->raid_conf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | unsigned long flags; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | spin_lock_irqsave(&conf->device_lock, flags); |
| 232 | __release_stripe(conf, sh); |
| 233 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 234 | } |
| 235 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 236 | static inline void remove_hash(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 238 | pr_debug("remove_hash(), stripe %llu\n", |
| 239 | (unsigned long long)sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 241 | hlist_del_init(&sh->hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | } |
| 243 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 244 | static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | { |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 246 | struct hlist_head *hp = stripe_hash(conf, sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 248 | pr_debug("insert_hash(), stripe %llu\n", |
| 249 | (unsigned long long)sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 251 | hlist_add_head(&sh->hash, hp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | |
| 255 | /* find an idle stripe, make sure it is unhashed, and return it. */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 256 | static struct stripe_head *get_free_stripe(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | { |
| 258 | struct stripe_head *sh = NULL; |
| 259 | struct list_head *first; |
| 260 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | if (list_empty(&conf->inactive_list)) |
| 262 | goto out; |
| 263 | first = conf->inactive_list.next; |
| 264 | sh = list_entry(first, struct stripe_head, lru); |
| 265 | list_del_init(first); |
| 266 | remove_hash(sh); |
| 267 | atomic_inc(&conf->active_stripes); |
| 268 | out: |
| 269 | return sh; |
| 270 | } |
| 271 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 272 | static void shrink_buffers(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | { |
| 274 | struct page *p; |
| 275 | int i; |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 276 | int num = sh->raid_conf->pool_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 278 | for (i = 0; i < num ; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | p = sh->dev[i].page; |
| 280 | if (!p) |
| 281 | continue; |
| 282 | sh->dev[i].page = NULL; |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 283 | put_page(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 287 | static int grow_buffers(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | { |
| 289 | int i; |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 290 | int num = sh->raid_conf->pool_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 292 | for (i = 0; i < num; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | struct page *page; |
| 294 | |
| 295 | if (!(page = alloc_page(GFP_KERNEL))) { |
| 296 | return 1; |
| 297 | } |
| 298 | sh->dev[i].page = page; |
| 299 | } |
| 300 | return 0; |
| 301 | } |
| 302 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 303 | static void raid5_build_block(struct stripe_head *sh, int i, int previous); |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 304 | static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 305 | struct stripe_head *sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 307 | 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] | 308 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 309 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 310 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 312 | BUG_ON(atomic_read(&sh->count) != 0); |
| 313 | BUG_ON(test_bit(STRIPE_HANDLE, &sh->state)); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 314 | BUG_ON(stripe_operations_active(sh)); |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 315 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 316 | pr_debug("init_stripe called, stripe %llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | (unsigned long long)sh->sector); |
| 318 | |
| 319 | remove_hash(sh); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 320 | |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 321 | sh->generation = conf->generation - previous; |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 322 | sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | sh->sector = sector; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 324 | stripe_set_idx(sector, conf, previous, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | sh->state = 0; |
| 326 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 327 | |
| 328 | for (i = sh->disks; i--; ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | struct r5dev *dev = &sh->dev[i]; |
| 330 | |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 331 | if (dev->toread || dev->read || dev->towrite || dev->written || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | test_bit(R5_LOCKED, &dev->flags)) { |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 333 | 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] | 334 | (unsigned long long)sh->sector, i, dev->toread, |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 335 | dev->read, dev->towrite, dev->written, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | test_bit(R5_LOCKED, &dev->flags)); |
NeilBrown | 8cfa7b0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 337 | WARN_ON(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | } |
| 339 | dev->flags = 0; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 340 | raid5_build_block(sh, i, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | } |
| 342 | insert_hash(conf, sh); |
| 343 | } |
| 344 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 345 | static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector, |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 346 | short generation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | { |
| 348 | struct stripe_head *sh; |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 349 | struct hlist_node *hn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 351 | pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector); |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 352 | hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash) |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 353 | if (sh->sector == sector && sh->generation == generation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | return sh; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 355 | pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | return NULL; |
| 357 | } |
| 358 | |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 359 | /* |
| 360 | * Need to check if array has failed when deciding whether to: |
| 361 | * - start an array |
| 362 | * - remove non-faulty devices |
| 363 | * - add a spare |
| 364 | * - allow a reshape |
| 365 | * This determination is simple when no reshape is happening. |
| 366 | * However if there is a reshape, we need to carefully check |
| 367 | * both the before and after sections. |
| 368 | * This is because some failed devices may only affect one |
| 369 | * of the two sections, and some non-in_sync devices may |
| 370 | * be insync in the section most affected by failed devices. |
| 371 | */ |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 372 | static int calc_degraded(struct r5conf *conf) |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 373 | { |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 374 | int degraded, degraded2; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 375 | int i; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 376 | |
| 377 | rcu_read_lock(); |
| 378 | degraded = 0; |
| 379 | for (i = 0; i < conf->previous_raid_disks; i++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 380 | struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 381 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
| 382 | degraded++; |
| 383 | else if (test_bit(In_sync, &rdev->flags)) |
| 384 | ; |
| 385 | else |
| 386 | /* not in-sync or faulty. |
| 387 | * If the reshape increases the number of devices, |
| 388 | * this is being recovered by the reshape, so |
| 389 | * this 'previous' section is not in_sync. |
| 390 | * If the number of devices is being reduced however, |
| 391 | * the device can only be part of the array if |
| 392 | * we are reverting a reshape, so this section will |
| 393 | * be in-sync. |
| 394 | */ |
| 395 | if (conf->raid_disks >= conf->previous_raid_disks) |
| 396 | degraded++; |
| 397 | } |
| 398 | rcu_read_unlock(); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 399 | if (conf->raid_disks == conf->previous_raid_disks) |
| 400 | return degraded; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 401 | rcu_read_lock(); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 402 | degraded2 = 0; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 403 | for (i = 0; i < conf->raid_disks; i++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 404 | struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 405 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 406 | degraded2++; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 407 | else if (test_bit(In_sync, &rdev->flags)) |
| 408 | ; |
| 409 | else |
| 410 | /* not in-sync or faulty. |
| 411 | * If reshape increases the number of devices, this |
| 412 | * section has already been recovered, else it |
| 413 | * almost certainly hasn't. |
| 414 | */ |
| 415 | if (conf->raid_disks <= conf->previous_raid_disks) |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 416 | degraded2++; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 417 | } |
| 418 | rcu_read_unlock(); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 419 | if (degraded2 > degraded) |
| 420 | return degraded2; |
| 421 | return degraded; |
| 422 | } |
| 423 | |
| 424 | static int has_failed(struct r5conf *conf) |
| 425 | { |
| 426 | int degraded; |
| 427 | |
| 428 | if (conf->mddev->reshape_position == MaxSector) |
| 429 | return conf->mddev->degraded > conf->max_degraded; |
| 430 | |
| 431 | degraded = calc_degraded(conf); |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 432 | if (degraded > conf->max_degraded) |
| 433 | return 1; |
| 434 | return 0; |
| 435 | } |
| 436 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 437 | static struct stripe_head * |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 438 | get_active_stripe(struct r5conf *conf, sector_t sector, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 439 | int previous, int noblock, int noquiesce) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | { |
| 441 | struct stripe_head *sh; |
| 442 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 443 | pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | |
| 445 | spin_lock_irq(&conf->device_lock); |
| 446 | |
| 447 | do { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 448 | wait_event_lock_irq(conf->wait_for_stripe, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 449 | conf->quiesce == 0 || noquiesce, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 450 | conf->device_lock, /* nothing */); |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 451 | sh = __find_stripe(conf, sector, conf->generation - previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | if (!sh) { |
| 453 | if (!conf->inactive_blocked) |
| 454 | sh = get_free_stripe(conf); |
| 455 | if (noblock && sh == NULL) |
| 456 | break; |
| 457 | if (!sh) { |
| 458 | conf->inactive_blocked = 1; |
| 459 | wait_event_lock_irq(conf->wait_for_stripe, |
| 460 | !list_empty(&conf->inactive_list) && |
NeilBrown | 5036805 | 2005-12-12 02:39:17 -0800 | [diff] [blame] | 461 | (atomic_read(&conf->active_stripes) |
| 462 | < (conf->max_nr_stripes *3/4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | || !conf->inactive_blocked), |
| 464 | conf->device_lock, |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 465 | ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | conf->inactive_blocked = 0; |
| 467 | } else |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 468 | init_stripe(sh, sector, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | } else { |
| 470 | if (atomic_read(&sh->count)) { |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 471 | BUG_ON(!list_empty(&sh->lru) |
| 472 | && !test_bit(STRIPE_EXPANDING, &sh->state)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | } else { |
| 474 | if (!test_bit(STRIPE_HANDLE, &sh->state)) |
| 475 | atomic_inc(&conf->active_stripes); |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 476 | if (list_empty(&sh->lru) && |
| 477 | !test_bit(STRIPE_EXPANDING, &sh->state)) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 478 | BUG(); |
| 479 | list_del_init(&sh->lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | } while (sh == NULL); |
| 483 | |
| 484 | if (sh) |
| 485 | atomic_inc(&sh->count); |
| 486 | |
| 487 | spin_unlock_irq(&conf->device_lock); |
| 488 | return sh; |
| 489 | } |
| 490 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 491 | /* Determine if 'data_offset' or 'new_data_offset' should be used |
| 492 | * in this stripe_head. |
| 493 | */ |
| 494 | static int use_new_offset(struct r5conf *conf, struct stripe_head *sh) |
| 495 | { |
| 496 | sector_t progress = conf->reshape_progress; |
| 497 | /* Need a memory barrier to make sure we see the value |
| 498 | * of conf->generation, or ->data_offset that was set before |
| 499 | * reshape_progress was updated. |
| 500 | */ |
| 501 | smp_rmb(); |
| 502 | if (progress == MaxSector) |
| 503 | return 0; |
| 504 | if (sh->generation == conf->generation - 1) |
| 505 | return 0; |
| 506 | /* We are in a reshape, and this is a new-generation stripe, |
| 507 | * so use new_data_offset. |
| 508 | */ |
| 509 | return 1; |
| 510 | } |
| 511 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 512 | static void |
| 513 | raid5_end_read_request(struct bio *bi, int error); |
| 514 | static void |
| 515 | raid5_end_write_request(struct bio *bi, int error); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 516 | |
Dan Williams | c4e5ac0 | 2008-06-28 08:31:53 +1000 | [diff] [blame] | 517 | 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] | 518 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 519 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 520 | int i, disks = sh->disks; |
| 521 | |
| 522 | might_sleep(); |
| 523 | |
| 524 | for (i = disks; i--; ) { |
| 525 | int rw; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 526 | int replace_only = 0; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 527 | struct bio *bi, *rbi; |
| 528 | struct md_rdev *rdev, *rrdev = NULL; |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 529 | if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) { |
| 530 | if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags)) |
| 531 | rw = WRITE_FUA; |
| 532 | else |
| 533 | rw = WRITE; |
| 534 | } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 535 | rw = READ; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 536 | else if (test_and_clear_bit(R5_WantReplace, |
| 537 | &sh->dev[i].flags)) { |
| 538 | rw = WRITE; |
| 539 | replace_only = 1; |
| 540 | } else |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 541 | continue; |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 542 | if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags)) |
| 543 | rw |= REQ_SYNC; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 544 | |
| 545 | bi = &sh->dev[i].req; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 546 | rbi = &sh->dev[i].rreq; /* For writing to replacement */ |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 547 | |
| 548 | bi->bi_rw = rw; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 549 | rbi->bi_rw = rw; |
| 550 | if (rw & WRITE) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 551 | bi->bi_end_io = raid5_end_write_request; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 552 | rbi->bi_end_io = raid5_end_write_request; |
| 553 | } else |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 554 | bi->bi_end_io = raid5_end_read_request; |
| 555 | |
| 556 | rcu_read_lock(); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 557 | rrdev = rcu_dereference(conf->disks[i].replacement); |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 558 | smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */ |
| 559 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 560 | if (!rdev) { |
| 561 | rdev = rrdev; |
| 562 | rrdev = NULL; |
| 563 | } |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 564 | if (rw & WRITE) { |
| 565 | if (replace_only) |
| 566 | rdev = NULL; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 567 | if (rdev == rrdev) |
| 568 | /* We raced and saw duplicates */ |
| 569 | rrdev = NULL; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 570 | } else { |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 571 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 572 | rdev = rrdev; |
| 573 | rrdev = NULL; |
| 574 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 575 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 576 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 577 | rdev = NULL; |
| 578 | if (rdev) |
| 579 | atomic_inc(&rdev->nr_pending); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 580 | if (rrdev && test_bit(Faulty, &rrdev->flags)) |
| 581 | rrdev = NULL; |
| 582 | if (rrdev) |
| 583 | atomic_inc(&rrdev->nr_pending); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 584 | rcu_read_unlock(); |
| 585 | |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 586 | /* We have already checked bad blocks for reads. Now |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 587 | * need to check for writes. We never accept write errors |
| 588 | * on the replacement, so we don't to check rrdev. |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 589 | */ |
| 590 | while ((rw & WRITE) && rdev && |
| 591 | test_bit(WriteErrorSeen, &rdev->flags)) { |
| 592 | sector_t first_bad; |
| 593 | int bad_sectors; |
| 594 | int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 595 | &first_bad, &bad_sectors); |
| 596 | if (!bad) |
| 597 | break; |
| 598 | |
| 599 | if (bad < 0) { |
| 600 | set_bit(BlockedBadBlocks, &rdev->flags); |
| 601 | if (!conf->mddev->external && |
| 602 | conf->mddev->flags) { |
| 603 | /* It is very unlikely, but we might |
| 604 | * still need to write out the |
| 605 | * bad block log - better give it |
| 606 | * a chance*/ |
| 607 | md_check_recovery(conf->mddev); |
| 608 | } |
majianpeng | 1850753 | 2012-07-03 12:11:54 +1000 | [diff] [blame^] | 609 | /* |
| 610 | * Because md_wait_for_blocked_rdev |
| 611 | * will dec nr_pending, we must |
| 612 | * increment it first. |
| 613 | */ |
| 614 | atomic_inc(&rdev->nr_pending); |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 615 | md_wait_for_blocked_rdev(rdev, conf->mddev); |
| 616 | } else { |
| 617 | /* Acknowledged bad block - skip the write */ |
| 618 | rdev_dec_pending(rdev, conf->mddev); |
| 619 | rdev = NULL; |
| 620 | } |
| 621 | } |
| 622 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 623 | if (rdev) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 624 | if (s->syncing || s->expanding || s->expanded |
| 625 | || s->replacing) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 626 | md_sync_acct(rdev->bdev, STRIPE_SECTORS); |
| 627 | |
Dan Williams | 2b7497f | 2008-06-28 08:31:52 +1000 | [diff] [blame] | 628 | set_bit(STRIPE_IO_STARTED, &sh->state); |
| 629 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 630 | bi->bi_bdev = rdev->bdev; |
| 631 | pr_debug("%s: for %llu schedule op %ld on disc %d\n", |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 632 | __func__, (unsigned long long)sh->sector, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 633 | bi->bi_rw, i); |
| 634 | atomic_inc(&sh->count); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 635 | if (use_new_offset(conf, sh)) |
| 636 | bi->bi_sector = (sh->sector |
| 637 | + rdev->new_data_offset); |
| 638 | else |
| 639 | bi->bi_sector = (sh->sector |
| 640 | + rdev->data_offset); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 641 | bi->bi_flags = 1 << BIO_UPTODATE; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 642 | bi->bi_idx = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 643 | bi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
| 644 | bi->bi_io_vec[0].bv_offset = 0; |
| 645 | bi->bi_size = STRIPE_SIZE; |
| 646 | bi->bi_next = NULL; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 647 | if (rrdev) |
| 648 | set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 649 | generic_make_request(bi); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 650 | } |
| 651 | if (rrdev) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 652 | if (s->syncing || s->expanding || s->expanded |
| 653 | || s->replacing) |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 654 | md_sync_acct(rrdev->bdev, STRIPE_SECTORS); |
| 655 | |
| 656 | set_bit(STRIPE_IO_STARTED, &sh->state); |
| 657 | |
| 658 | rbi->bi_bdev = rrdev->bdev; |
| 659 | pr_debug("%s: for %llu schedule op %ld on " |
| 660 | "replacement disc %d\n", |
| 661 | __func__, (unsigned long long)sh->sector, |
| 662 | rbi->bi_rw, i); |
| 663 | atomic_inc(&sh->count); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 664 | if (use_new_offset(conf, sh)) |
| 665 | rbi->bi_sector = (sh->sector |
| 666 | + rrdev->new_data_offset); |
| 667 | else |
| 668 | rbi->bi_sector = (sh->sector |
| 669 | + rrdev->data_offset); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 670 | rbi->bi_flags = 1 << BIO_UPTODATE; |
| 671 | rbi->bi_idx = 0; |
| 672 | rbi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
| 673 | rbi->bi_io_vec[0].bv_offset = 0; |
| 674 | rbi->bi_size = STRIPE_SIZE; |
| 675 | rbi->bi_next = NULL; |
| 676 | generic_make_request(rbi); |
| 677 | } |
| 678 | if (!rdev && !rrdev) { |
Namhyung Kim | b062962 | 2011-06-14 14:20:19 +1000 | [diff] [blame] | 679 | if (rw & WRITE) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 680 | set_bit(STRIPE_DEGRADED, &sh->state); |
| 681 | pr_debug("skip op %ld on disc %d for sector %llu\n", |
| 682 | bi->bi_rw, i, (unsigned long long)sh->sector); |
| 683 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 684 | set_bit(STRIPE_HANDLE, &sh->state); |
| 685 | } |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | static struct dma_async_tx_descriptor * |
| 690 | async_copy_data(int frombio, struct bio *bio, struct page *page, |
| 691 | sector_t sector, struct dma_async_tx_descriptor *tx) |
| 692 | { |
| 693 | struct bio_vec *bvl; |
| 694 | struct page *bio_page; |
| 695 | int i; |
| 696 | int page_offset; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 697 | struct async_submit_ctl submit; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 698 | enum async_tx_flags flags = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 699 | |
| 700 | if (bio->bi_sector >= sector) |
| 701 | page_offset = (signed)(bio->bi_sector - sector) * 512; |
| 702 | else |
| 703 | page_offset = (signed)(sector - bio->bi_sector) * -512; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 704 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 705 | if (frombio) |
| 706 | flags |= ASYNC_TX_FENCE; |
| 707 | init_async_submit(&submit, flags, tx, NULL, NULL, NULL); |
| 708 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 709 | bio_for_each_segment(bvl, bio, i) { |
Namhyung Kim | fcde907 | 2011-06-14 14:23:57 +1000 | [diff] [blame] | 710 | int len = bvl->bv_len; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 711 | int clen; |
| 712 | int b_offset = 0; |
| 713 | |
| 714 | if (page_offset < 0) { |
| 715 | b_offset = -page_offset; |
| 716 | page_offset += b_offset; |
| 717 | len -= b_offset; |
| 718 | } |
| 719 | |
| 720 | if (len > 0 && page_offset + len > STRIPE_SIZE) |
| 721 | clen = STRIPE_SIZE - page_offset; |
| 722 | else |
| 723 | clen = len; |
| 724 | |
| 725 | if (clen > 0) { |
Namhyung Kim | fcde907 | 2011-06-14 14:23:57 +1000 | [diff] [blame] | 726 | b_offset += bvl->bv_offset; |
| 727 | bio_page = bvl->bv_page; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 728 | if (frombio) |
| 729 | tx = async_memcpy(page, bio_page, page_offset, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 730 | b_offset, clen, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 731 | else |
| 732 | tx = async_memcpy(bio_page, page, b_offset, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 733 | page_offset, clen, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 734 | } |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 735 | /* chain the operations */ |
| 736 | submit.depend_tx = tx; |
| 737 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 738 | if (clen < len) /* hit end of page */ |
| 739 | break; |
| 740 | page_offset += len; |
| 741 | } |
| 742 | |
| 743 | return tx; |
| 744 | } |
| 745 | |
| 746 | static void ops_complete_biofill(void *stripe_head_ref) |
| 747 | { |
| 748 | struct stripe_head *sh = stripe_head_ref; |
| 749 | struct bio *return_bi = NULL; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 750 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 751 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 752 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 753 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 754 | (unsigned long long)sh->sector); |
| 755 | |
| 756 | /* clear completed biofills */ |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 757 | spin_lock_irq(&conf->device_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 758 | for (i = sh->disks; i--; ) { |
| 759 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 760 | |
| 761 | /* acknowledge completion of a biofill operation */ |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 762 | /* and check if we need to reply to a read request, |
| 763 | * new R5_Wantfill requests are held off until |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 764 | * !STRIPE_BIOFILL_RUN |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 765 | */ |
| 766 | if (test_and_clear_bit(R5_Wantfill, &dev->flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 767 | struct bio *rbi, *rbi2; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 768 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 769 | BUG_ON(!dev->read); |
| 770 | rbi = dev->read; |
| 771 | dev->read = NULL; |
| 772 | while (rbi && rbi->bi_sector < |
| 773 | dev->sector + STRIPE_SECTORS) { |
| 774 | rbi2 = r5_next_bio(rbi, dev->sector); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 775 | if (!raid5_dec_bi_phys_segments(rbi)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 776 | rbi->bi_next = return_bi; |
| 777 | return_bi = rbi; |
| 778 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 779 | rbi = rbi2; |
| 780 | } |
| 781 | } |
| 782 | } |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 783 | spin_unlock_irq(&conf->device_lock); |
| 784 | clear_bit(STRIPE_BIOFILL_RUN, &sh->state); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 785 | |
| 786 | return_io(return_bi); |
| 787 | |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 788 | set_bit(STRIPE_HANDLE, &sh->state); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 789 | release_stripe(sh); |
| 790 | } |
| 791 | |
| 792 | static void ops_run_biofill(struct stripe_head *sh) |
| 793 | { |
| 794 | struct dma_async_tx_descriptor *tx = NULL; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 795 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 796 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 797 | int i; |
| 798 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 799 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 800 | (unsigned long long)sh->sector); |
| 801 | |
| 802 | for (i = sh->disks; i--; ) { |
| 803 | struct r5dev *dev = &sh->dev[i]; |
| 804 | if (test_bit(R5_Wantfill, &dev->flags)) { |
| 805 | struct bio *rbi; |
| 806 | spin_lock_irq(&conf->device_lock); |
| 807 | dev->read = rbi = dev->toread; |
| 808 | dev->toread = NULL; |
| 809 | spin_unlock_irq(&conf->device_lock); |
| 810 | while (rbi && rbi->bi_sector < |
| 811 | dev->sector + STRIPE_SECTORS) { |
| 812 | tx = async_copy_data(0, rbi, dev->page, |
| 813 | dev->sector, tx); |
| 814 | rbi = r5_next_bio(rbi, dev->sector); |
| 815 | } |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 820 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL); |
| 821 | async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 822 | } |
| 823 | |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 824 | static void mark_target_uptodate(struct stripe_head *sh, int target) |
| 825 | { |
| 826 | struct r5dev *tgt; |
| 827 | |
| 828 | if (target < 0) |
| 829 | return; |
| 830 | |
| 831 | tgt = &sh->dev[target]; |
| 832 | set_bit(R5_UPTODATE, &tgt->flags); |
| 833 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 834 | clear_bit(R5_Wantcompute, &tgt->flags); |
| 835 | } |
| 836 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 837 | static void ops_complete_compute(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 838 | { |
| 839 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 840 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 841 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 842 | (unsigned long long)sh->sector); |
| 843 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 844 | /* mark the computed target(s) as uptodate */ |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 845 | mark_target_uptodate(sh, sh->ops.target); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 846 | mark_target_uptodate(sh, sh->ops.target2); |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 847 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 848 | clear_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 849 | if (sh->check_state == check_state_compute_run) |
| 850 | sh->check_state = check_state_compute_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 851 | set_bit(STRIPE_HANDLE, &sh->state); |
| 852 | release_stripe(sh); |
| 853 | } |
| 854 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 855 | /* return a pointer to the address conversion region of the scribble buffer */ |
| 856 | static addr_conv_t *to_addr_conv(struct stripe_head *sh, |
| 857 | struct raid5_percpu *percpu) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 858 | { |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 859 | return percpu->scribble + sizeof(struct page *) * (sh->disks + 2); |
| 860 | } |
| 861 | |
| 862 | static struct dma_async_tx_descriptor * |
| 863 | ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 864 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 865 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 866 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 867 | int target = sh->ops.target; |
| 868 | struct r5dev *tgt = &sh->dev[target]; |
| 869 | struct page *xor_dest = tgt->page; |
| 870 | int count = 0; |
| 871 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 872 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 873 | int i; |
| 874 | |
| 875 | pr_debug("%s: stripe %llu block: %d\n", |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 876 | __func__, (unsigned long long)sh->sector, target); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 877 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 878 | |
| 879 | for (i = disks; i--; ) |
| 880 | if (i != target) |
| 881 | xor_srcs[count++] = sh->dev[i].page; |
| 882 | |
| 883 | atomic_inc(&sh->count); |
| 884 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 885 | 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] | 886 | ops_complete_compute, sh, to_addr_conv(sh, percpu)); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 887 | if (unlikely(count == 1)) |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 888 | 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] | 889 | else |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 890 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 891 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 892 | return tx; |
| 893 | } |
| 894 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 895 | /* set_syndrome_sources - populate source buffers for gen_syndrome |
| 896 | * @srcs - (struct page *) array of size sh->disks |
| 897 | * @sh - stripe_head to parse |
| 898 | * |
| 899 | * Populates srcs in proper layout order for the stripe and returns the |
| 900 | * 'count' of sources to be used in a call to async_gen_syndrome. The P |
| 901 | * destination buffer is recorded in srcs[count] and the Q destination |
| 902 | * is recorded in srcs[count+1]]. |
| 903 | */ |
| 904 | static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh) |
| 905 | { |
| 906 | int disks = sh->disks; |
| 907 | int syndrome_disks = sh->ddf_layout ? disks : (disks - 2); |
| 908 | int d0_idx = raid6_d0(sh); |
| 909 | int count; |
| 910 | int i; |
| 911 | |
| 912 | for (i = 0; i < disks; i++) |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 913 | srcs[i] = NULL; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 914 | |
| 915 | count = 0; |
| 916 | i = d0_idx; |
| 917 | do { |
| 918 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 919 | |
| 920 | srcs[slot] = sh->dev[i].page; |
| 921 | i = raid6_next_disk(i, disks); |
| 922 | } while (i != d0_idx); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 923 | |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 924 | return syndrome_disks; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | static struct dma_async_tx_descriptor * |
| 928 | ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 929 | { |
| 930 | int disks = sh->disks; |
| 931 | struct page **blocks = percpu->scribble; |
| 932 | int target; |
| 933 | int qd_idx = sh->qd_idx; |
| 934 | struct dma_async_tx_descriptor *tx; |
| 935 | struct async_submit_ctl submit; |
| 936 | struct r5dev *tgt; |
| 937 | struct page *dest; |
| 938 | int i; |
| 939 | int count; |
| 940 | |
| 941 | if (sh->ops.target < 0) |
| 942 | target = sh->ops.target2; |
| 943 | else if (sh->ops.target2 < 0) |
| 944 | target = sh->ops.target; |
| 945 | else |
| 946 | /* we should only have one valid target */ |
| 947 | BUG(); |
| 948 | BUG_ON(target < 0); |
| 949 | pr_debug("%s: stripe %llu block: %d\n", |
| 950 | __func__, (unsigned long long)sh->sector, target); |
| 951 | |
| 952 | tgt = &sh->dev[target]; |
| 953 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 954 | dest = tgt->page; |
| 955 | |
| 956 | atomic_inc(&sh->count); |
| 957 | |
| 958 | if (target == qd_idx) { |
| 959 | count = set_syndrome_sources(blocks, sh); |
| 960 | blocks[count] = NULL; /* regenerating p is not necessary */ |
| 961 | BUG_ON(blocks[count+1] != dest); /* q should already be set */ |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 962 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 963 | ops_complete_compute, sh, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 964 | to_addr_conv(sh, percpu)); |
| 965 | tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
| 966 | } else { |
| 967 | /* Compute any data- or p-drive using XOR */ |
| 968 | count = 0; |
| 969 | for (i = disks; i-- ; ) { |
| 970 | if (i == target || i == qd_idx) |
| 971 | continue; |
| 972 | blocks[count++] = sh->dev[i].page; |
| 973 | } |
| 974 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 975 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
| 976 | NULL, ops_complete_compute, sh, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 977 | to_addr_conv(sh, percpu)); |
| 978 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit); |
| 979 | } |
| 980 | |
| 981 | return tx; |
| 982 | } |
| 983 | |
| 984 | static struct dma_async_tx_descriptor * |
| 985 | ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 986 | { |
| 987 | int i, count, disks = sh->disks; |
| 988 | int syndrome_disks = sh->ddf_layout ? disks : disks-2; |
| 989 | int d0_idx = raid6_d0(sh); |
| 990 | int faila = -1, failb = -1; |
| 991 | int target = sh->ops.target; |
| 992 | int target2 = sh->ops.target2; |
| 993 | struct r5dev *tgt = &sh->dev[target]; |
| 994 | struct r5dev *tgt2 = &sh->dev[target2]; |
| 995 | struct dma_async_tx_descriptor *tx; |
| 996 | struct page **blocks = percpu->scribble; |
| 997 | struct async_submit_ctl submit; |
| 998 | |
| 999 | pr_debug("%s: stripe %llu block1: %d block2: %d\n", |
| 1000 | __func__, (unsigned long long)sh->sector, target, target2); |
| 1001 | BUG_ON(target < 0 || target2 < 0); |
| 1002 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 1003 | BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags)); |
| 1004 | |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1005 | /* we need to open-code set_syndrome_sources to handle the |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1006 | * slot number conversion for 'faila' and 'failb' |
| 1007 | */ |
| 1008 | for (i = 0; i < disks ; i++) |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 1009 | blocks[i] = NULL; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1010 | count = 0; |
| 1011 | i = d0_idx; |
| 1012 | do { |
| 1013 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 1014 | |
| 1015 | blocks[slot] = sh->dev[i].page; |
| 1016 | |
| 1017 | if (i == target) |
| 1018 | faila = slot; |
| 1019 | if (i == target2) |
| 1020 | failb = slot; |
| 1021 | i = raid6_next_disk(i, disks); |
| 1022 | } while (i != d0_idx); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1023 | |
| 1024 | BUG_ON(faila == failb); |
| 1025 | if (failb < faila) |
| 1026 | swap(faila, failb); |
| 1027 | pr_debug("%s: stripe: %llu faila: %d failb: %d\n", |
| 1028 | __func__, (unsigned long long)sh->sector, faila, failb); |
| 1029 | |
| 1030 | atomic_inc(&sh->count); |
| 1031 | |
| 1032 | if (failb == syndrome_disks+1) { |
| 1033 | /* Q disk is one of the missing disks */ |
| 1034 | if (faila == syndrome_disks) { |
| 1035 | /* Missing P+Q, just recompute */ |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1036 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 1037 | ops_complete_compute, sh, |
| 1038 | to_addr_conv(sh, percpu)); |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 1039 | return async_gen_syndrome(blocks, 0, syndrome_disks+2, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1040 | STRIPE_SIZE, &submit); |
| 1041 | } else { |
| 1042 | struct page *dest; |
| 1043 | int data_target; |
| 1044 | int qd_idx = sh->qd_idx; |
| 1045 | |
| 1046 | /* Missing D+Q: recompute D from P, then recompute Q */ |
| 1047 | if (target == qd_idx) |
| 1048 | data_target = target2; |
| 1049 | else |
| 1050 | data_target = target; |
| 1051 | |
| 1052 | count = 0; |
| 1053 | for (i = disks; i-- ; ) { |
| 1054 | if (i == data_target || i == qd_idx) |
| 1055 | continue; |
| 1056 | blocks[count++] = sh->dev[i].page; |
| 1057 | } |
| 1058 | dest = sh->dev[data_target].page; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1059 | init_async_submit(&submit, |
| 1060 | ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
| 1061 | NULL, NULL, NULL, |
| 1062 | to_addr_conv(sh, percpu)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1063 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, |
| 1064 | &submit); |
| 1065 | |
| 1066 | count = set_syndrome_sources(blocks, sh); |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1067 | init_async_submit(&submit, ASYNC_TX_FENCE, tx, |
| 1068 | ops_complete_compute, sh, |
| 1069 | to_addr_conv(sh, percpu)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1070 | return async_gen_syndrome(blocks, 0, count+2, |
| 1071 | STRIPE_SIZE, &submit); |
| 1072 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1073 | } else { |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1074 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 1075 | ops_complete_compute, sh, |
| 1076 | to_addr_conv(sh, percpu)); |
| 1077 | if (failb == syndrome_disks) { |
| 1078 | /* We're missing D+P. */ |
| 1079 | return async_raid6_datap_recov(syndrome_disks+2, |
| 1080 | STRIPE_SIZE, faila, |
| 1081 | blocks, &submit); |
| 1082 | } else { |
| 1083 | /* We're missing D+D. */ |
| 1084 | return async_raid6_2data_recov(syndrome_disks+2, |
| 1085 | STRIPE_SIZE, faila, failb, |
| 1086 | blocks, &submit); |
| 1087 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1092 | static void ops_complete_prexor(void *stripe_head_ref) |
| 1093 | { |
| 1094 | struct stripe_head *sh = stripe_head_ref; |
| 1095 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1096 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1097 | (unsigned long long)sh->sector); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | static struct dma_async_tx_descriptor * |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1101 | ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1102 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1103 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1104 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1105 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1106 | int count = 0, pd_idx = sh->pd_idx, i; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1107 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1108 | |
| 1109 | /* existing parity data subtracted */ |
| 1110 | struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 1111 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1112 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1113 | (unsigned long long)sh->sector); |
| 1114 | |
| 1115 | for (i = disks; i--; ) { |
| 1116 | struct r5dev *dev = &sh->dev[i]; |
| 1117 | /* Only process blocks that are known to be uptodate */ |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1118 | if (test_bit(R5_Wantdrain, &dev->flags)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1119 | xor_srcs[count++] = dev->page; |
| 1120 | } |
| 1121 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1122 | 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] | 1123 | ops_complete_prexor, sh, to_addr_conv(sh, percpu)); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1124 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1125 | |
| 1126 | return tx; |
| 1127 | } |
| 1128 | |
| 1129 | static struct dma_async_tx_descriptor * |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1130 | 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] | 1131 | { |
| 1132 | int disks = sh->disks; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1133 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1134 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1135 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1136 | (unsigned long long)sh->sector); |
| 1137 | |
| 1138 | for (i = disks; i--; ) { |
| 1139 | struct r5dev *dev = &sh->dev[i]; |
| 1140 | struct bio *chosen; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1141 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1142 | if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1143 | struct bio *wbi; |
| 1144 | |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 1145 | spin_lock_irq(&sh->raid_conf->device_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1146 | chosen = dev->towrite; |
| 1147 | dev->towrite = NULL; |
| 1148 | BUG_ON(dev->written); |
| 1149 | wbi = dev->written = chosen; |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 1150 | spin_unlock_irq(&sh->raid_conf->device_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1151 | |
| 1152 | while (wbi && wbi->bi_sector < |
| 1153 | dev->sector + STRIPE_SECTORS) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1154 | if (wbi->bi_rw & REQ_FUA) |
| 1155 | set_bit(R5_WantFUA, &dev->flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1156 | if (wbi->bi_rw & REQ_SYNC) |
| 1157 | set_bit(R5_SyncIO, &dev->flags); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1158 | tx = async_copy_data(1, wbi, dev->page, |
| 1159 | dev->sector, tx); |
| 1160 | wbi = r5_next_bio(wbi, dev->sector); |
| 1161 | } |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | return tx; |
| 1166 | } |
| 1167 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1168 | static void ops_complete_reconstruct(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1169 | { |
| 1170 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1171 | int disks = sh->disks; |
| 1172 | int pd_idx = sh->pd_idx; |
| 1173 | int qd_idx = sh->qd_idx; |
| 1174 | int i; |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1175 | bool fua = false, sync = false; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1176 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1177 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1178 | (unsigned long long)sh->sector); |
| 1179 | |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1180 | for (i = disks; i--; ) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1181 | fua |= test_bit(R5_WantFUA, &sh->dev[i].flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1182 | sync |= test_bit(R5_SyncIO, &sh->dev[i].flags); |
| 1183 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1184 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1185 | for (i = disks; i--; ) { |
| 1186 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1187 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1188 | if (dev->written || i == pd_idx || i == qd_idx) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1189 | set_bit(R5_UPTODATE, &dev->flags); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1190 | if (fua) |
| 1191 | set_bit(R5_WantFUA, &dev->flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1192 | if (sync) |
| 1193 | set_bit(R5_SyncIO, &dev->flags); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1194 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1195 | } |
| 1196 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1197 | if (sh->reconstruct_state == reconstruct_state_drain_run) |
| 1198 | sh->reconstruct_state = reconstruct_state_drain_result; |
| 1199 | else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) |
| 1200 | sh->reconstruct_state = reconstruct_state_prexor_drain_result; |
| 1201 | else { |
| 1202 | BUG_ON(sh->reconstruct_state != reconstruct_state_run); |
| 1203 | sh->reconstruct_state = reconstruct_state_result; |
| 1204 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1205 | |
| 1206 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1207 | release_stripe(sh); |
| 1208 | } |
| 1209 | |
| 1210 | static void |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1211 | ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1212 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1213 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1214 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1215 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1216 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1217 | int count = 0, pd_idx = sh->pd_idx, i; |
| 1218 | struct page *xor_dest; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1219 | int prexor = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1220 | unsigned long flags; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1221 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1222 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1223 | (unsigned long long)sh->sector); |
| 1224 | |
| 1225 | /* check if prexor is active which means only process blocks |
| 1226 | * that are part of a read-modify-write (written) |
| 1227 | */ |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1228 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) { |
| 1229 | prexor = 1; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1230 | xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 1231 | for (i = disks; i--; ) { |
| 1232 | struct r5dev *dev = &sh->dev[i]; |
| 1233 | if (dev->written) |
| 1234 | xor_srcs[count++] = dev->page; |
| 1235 | } |
| 1236 | } else { |
| 1237 | xor_dest = sh->dev[pd_idx].page; |
| 1238 | for (i = disks; i--; ) { |
| 1239 | struct r5dev *dev = &sh->dev[i]; |
| 1240 | if (i != pd_idx) |
| 1241 | xor_srcs[count++] = dev->page; |
| 1242 | } |
| 1243 | } |
| 1244 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1245 | /* 1/ if we prexor'd then the dest is reused as a source |
| 1246 | * 2/ if we did not prexor then we are redoing the parity |
| 1247 | * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST |
| 1248 | * for the synchronous xor case |
| 1249 | */ |
Dan Williams | 88ba2aa | 2009-04-09 16:16:18 -0700 | [diff] [blame] | 1250 | flags = ASYNC_TX_ACK | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1251 | (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST); |
| 1252 | |
| 1253 | atomic_inc(&sh->count); |
| 1254 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1255 | init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh, |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1256 | to_addr_conv(sh, percpu)); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1257 | if (unlikely(count == 1)) |
| 1258 | tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit); |
| 1259 | else |
| 1260 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1261 | } |
| 1262 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1263 | static void |
| 1264 | ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1265 | struct dma_async_tx_descriptor *tx) |
| 1266 | { |
| 1267 | struct async_submit_ctl submit; |
| 1268 | struct page **blocks = percpu->scribble; |
| 1269 | int count; |
| 1270 | |
| 1271 | pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector); |
| 1272 | |
| 1273 | count = set_syndrome_sources(blocks, sh); |
| 1274 | |
| 1275 | atomic_inc(&sh->count); |
| 1276 | |
| 1277 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct, |
| 1278 | sh, to_addr_conv(sh, percpu)); |
| 1279 | async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | static void ops_complete_check(void *stripe_head_ref) |
| 1283 | { |
| 1284 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1285 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1286 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1287 | (unsigned long long)sh->sector); |
| 1288 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 1289 | sh->check_state = check_state_check_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1290 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1291 | release_stripe(sh); |
| 1292 | } |
| 1293 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1294 | 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] | 1295 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1296 | int disks = sh->disks; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1297 | int pd_idx = sh->pd_idx; |
| 1298 | int qd_idx = sh->qd_idx; |
| 1299 | struct page *xor_dest; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1300 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1301 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1302 | struct async_submit_ctl submit; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1303 | int count; |
| 1304 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1305 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1306 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1307 | (unsigned long long)sh->sector); |
| 1308 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1309 | count = 0; |
| 1310 | xor_dest = sh->dev[pd_idx].page; |
| 1311 | xor_srcs[count++] = xor_dest; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1312 | for (i = disks; i--; ) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1313 | if (i == pd_idx || i == qd_idx) |
| 1314 | continue; |
| 1315 | xor_srcs[count++] = sh->dev[i].page; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1316 | } |
| 1317 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1318 | init_async_submit(&submit, 0, NULL, NULL, NULL, |
| 1319 | to_addr_conv(sh, percpu)); |
Dan Williams | 099f53c | 2009-04-08 14:28:37 -0700 | [diff] [blame] | 1320 | tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1321 | &sh->ops.zero_sum_result, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1322 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1323 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1324 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL); |
| 1325 | tx = async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1326 | } |
| 1327 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1328 | static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp) |
| 1329 | { |
| 1330 | struct page **srcs = percpu->scribble; |
| 1331 | struct async_submit_ctl submit; |
| 1332 | int count; |
| 1333 | |
| 1334 | pr_debug("%s: stripe %llu checkp: %d\n", __func__, |
| 1335 | (unsigned long long)sh->sector, checkp); |
| 1336 | |
| 1337 | count = set_syndrome_sources(srcs, sh); |
| 1338 | if (!checkp) |
| 1339 | srcs[count] = NULL; |
| 1340 | |
| 1341 | atomic_inc(&sh->count); |
| 1342 | init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check, |
| 1343 | sh, to_addr_conv(sh, percpu)); |
| 1344 | async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE, |
| 1345 | &sh->ops.zero_sum_result, percpu->spare_page, &submit); |
| 1346 | } |
| 1347 | |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 1348 | 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] | 1349 | { |
| 1350 | int overlap_clear = 0, i, disks = sh->disks; |
| 1351 | struct dma_async_tx_descriptor *tx = NULL; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1352 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1353 | int level = conf->level; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1354 | struct raid5_percpu *percpu; |
| 1355 | unsigned long cpu; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1356 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1357 | cpu = get_cpu(); |
| 1358 | percpu = per_cpu_ptr(conf->percpu, cpu); |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 1359 | if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1360 | ops_run_biofill(sh); |
| 1361 | overlap_clear++; |
| 1362 | } |
| 1363 | |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 1364 | if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1365 | if (level < 6) |
| 1366 | tx = ops_run_compute5(sh, percpu); |
| 1367 | else { |
| 1368 | if (sh->ops.target2 < 0 || sh->ops.target < 0) |
| 1369 | tx = ops_run_compute6_1(sh, percpu); |
| 1370 | else |
| 1371 | tx = ops_run_compute6_2(sh, percpu); |
| 1372 | } |
| 1373 | /* terminate the chain if reconstruct is not set to be run */ |
| 1374 | if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 1375 | async_tx_ack(tx); |
| 1376 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1377 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1378 | if (test_bit(STRIPE_OP_PREXOR, &ops_request)) |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1379 | tx = ops_run_prexor(sh, percpu, tx); |
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_BIODRAIN, &ops_request)) { |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1382 | tx = ops_run_biodrain(sh, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1383 | overlap_clear++; |
| 1384 | } |
| 1385 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1386 | if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) { |
| 1387 | if (level < 6) |
| 1388 | ops_run_reconstruct5(sh, percpu, tx); |
| 1389 | else |
| 1390 | ops_run_reconstruct6(sh, percpu, tx); |
| 1391 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1392 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1393 | if (test_bit(STRIPE_OP_CHECK, &ops_request)) { |
| 1394 | if (sh->check_state == check_state_run) |
| 1395 | ops_run_check_p(sh, percpu); |
| 1396 | else if (sh->check_state == check_state_run_q) |
| 1397 | ops_run_check_pq(sh, percpu, 0); |
| 1398 | else if (sh->check_state == check_state_run_pq) |
| 1399 | ops_run_check_pq(sh, percpu, 1); |
| 1400 | else |
| 1401 | BUG(); |
| 1402 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1403 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1404 | if (overlap_clear) |
| 1405 | for (i = disks; i--; ) { |
| 1406 | struct r5dev *dev = &sh->dev[i]; |
| 1407 | if (test_and_clear_bit(R5_Overlap, &dev->flags)) |
| 1408 | wake_up(&sh->raid_conf->wait_for_overlap); |
| 1409 | } |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1410 | put_cpu(); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1411 | } |
| 1412 | |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 1413 | #ifdef CONFIG_MULTICORE_RAID456 |
| 1414 | static void async_run_ops(void *param, async_cookie_t cookie) |
| 1415 | { |
| 1416 | struct stripe_head *sh = param; |
| 1417 | unsigned long ops_request = sh->ops.request; |
| 1418 | |
| 1419 | clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state); |
| 1420 | wake_up(&sh->ops.wait_for_ops); |
| 1421 | |
| 1422 | __raid_run_ops(sh, ops_request); |
| 1423 | release_stripe(sh); |
| 1424 | } |
| 1425 | |
| 1426 | static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request) |
| 1427 | { |
| 1428 | /* since handle_stripe can be called outside of raid5d context |
| 1429 | * we need to ensure sh->ops.request is de-staged before another |
| 1430 | * request arrives |
| 1431 | */ |
| 1432 | wait_event(sh->ops.wait_for_ops, |
| 1433 | !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state)); |
| 1434 | sh->ops.request = ops_request; |
| 1435 | |
| 1436 | atomic_inc(&sh->count); |
| 1437 | async_schedule(async_run_ops, sh); |
| 1438 | } |
| 1439 | #else |
| 1440 | #define raid_run_ops __raid_run_ops |
| 1441 | #endif |
| 1442 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1443 | static int grow_one_stripe(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | { |
| 1445 | struct stripe_head *sh; |
Namhyung Kim | 6ce3284 | 2011-07-18 17:38:50 +1000 | [diff] [blame] | 1446 | sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1447 | if (!sh) |
| 1448 | return 0; |
Namhyung Kim | 6ce3284 | 2011-07-18 17:38:50 +1000 | [diff] [blame] | 1449 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1450 | sh->raid_conf = conf; |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 1451 | #ifdef CONFIG_MULTICORE_RAID456 |
| 1452 | init_waitqueue_head(&sh->ops.wait_for_ops); |
| 1453 | #endif |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1454 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 1455 | if (grow_buffers(sh)) { |
| 1456 | shrink_buffers(sh); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1457 | kmem_cache_free(conf->slab_cache, sh); |
| 1458 | return 0; |
| 1459 | } |
| 1460 | /* we just created an active stripe so... */ |
| 1461 | atomic_set(&sh->count, 1); |
| 1462 | atomic_inc(&conf->active_stripes); |
| 1463 | INIT_LIST_HEAD(&sh->lru); |
| 1464 | release_stripe(sh); |
| 1465 | return 1; |
| 1466 | } |
| 1467 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1468 | static int grow_stripes(struct r5conf *conf, int num) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1469 | { |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 1470 | struct kmem_cache *sc; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 1471 | int devs = max(conf->raid_disks, conf->previous_raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | |
NeilBrown | f4be6b4 | 2010-06-01 19:37:25 +1000 | [diff] [blame] | 1473 | if (conf->mddev->gendisk) |
| 1474 | sprintf(conf->cache_name[0], |
| 1475 | "raid%d-%s", conf->level, mdname(conf->mddev)); |
| 1476 | else |
| 1477 | sprintf(conf->cache_name[0], |
| 1478 | "raid%d-%p", conf->level, conf->mddev); |
| 1479 | sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]); |
| 1480 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1481 | conf->active_name = 0; |
| 1482 | sc = kmem_cache_create(conf->cache_name[conf->active_name], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1484 | 0, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | if (!sc) |
| 1486 | return 1; |
| 1487 | conf->slab_cache = sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1488 | conf->pool_size = devs; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1489 | while (num--) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1490 | if (!grow_one_stripe(conf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | return 0; |
| 1493 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 1494 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1495 | /** |
| 1496 | * scribble_len - return the required size of the scribble region |
| 1497 | * @num - total number of disks in the array |
| 1498 | * |
| 1499 | * The size must be enough to contain: |
| 1500 | * 1/ a struct page pointer for each device in the array +2 |
| 1501 | * 2/ room to convert each entry in (1) to its corresponding dma |
| 1502 | * (dma_map_page()) or page (page_address()) address. |
| 1503 | * |
| 1504 | * Note: the +2 is for the destination buffers of the ddf/raid6 case where we |
| 1505 | * calculate over all devices (not just the data blocks), using zeros in place |
| 1506 | * of the P and Q blocks. |
| 1507 | */ |
| 1508 | static size_t scribble_len(int num) |
| 1509 | { |
| 1510 | size_t len; |
| 1511 | |
| 1512 | len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2); |
| 1513 | |
| 1514 | return len; |
| 1515 | } |
| 1516 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1517 | static int resize_stripes(struct r5conf *conf, int newsize) |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1518 | { |
| 1519 | /* Make all the stripes able to hold 'newsize' devices. |
| 1520 | * New slots in each stripe get 'page' set to a new page. |
| 1521 | * |
| 1522 | * This happens in stages: |
| 1523 | * 1/ create a new kmem_cache and allocate the required number of |
| 1524 | * stripe_heads. |
| 1525 | * 2/ gather all the old stripe_heads and tranfer the pages across |
| 1526 | * to the new stripe_heads. This will have the side effect of |
| 1527 | * freezing the array as once all stripe_heads have been collected, |
| 1528 | * no IO will be possible. Old stripe heads are freed once their |
| 1529 | * pages have been transferred over, and the old kmem_cache is |
| 1530 | * freed when all stripes are done. |
| 1531 | * 3/ reallocate conf->disks to be suitable bigger. If this fails, |
| 1532 | * we simple return a failre status - no need to clean anything up. |
| 1533 | * 4/ allocate new pages for the new slots in the new stripe_heads. |
| 1534 | * If this fails, we don't bother trying the shrink the |
| 1535 | * stripe_heads down again, we just leave them as they are. |
| 1536 | * As each stripe_head is processed the new one is released into |
| 1537 | * active service. |
| 1538 | * |
| 1539 | * Once step2 is started, we cannot afford to wait for a write, |
| 1540 | * so we use GFP_NOIO allocations. |
| 1541 | */ |
| 1542 | struct stripe_head *osh, *nsh; |
| 1543 | LIST_HEAD(newstripes); |
| 1544 | struct disk_info *ndisks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1545 | unsigned long cpu; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 1546 | int err; |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 1547 | struct kmem_cache *sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1548 | int i; |
| 1549 | |
| 1550 | if (newsize <= conf->pool_size) |
| 1551 | return 0; /* never bother to shrink */ |
| 1552 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 1553 | err = md_allow_write(conf->mddev); |
| 1554 | if (err) |
| 1555 | return err; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 1556 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1557 | /* Step 1 */ |
| 1558 | sc = kmem_cache_create(conf->cache_name[1-conf->active_name], |
| 1559 | sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1560 | 0, 0, NULL); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1561 | if (!sc) |
| 1562 | return -ENOMEM; |
| 1563 | |
| 1564 | for (i = conf->max_nr_stripes; i; i--) { |
Namhyung Kim | 6ce3284 | 2011-07-18 17:38:50 +1000 | [diff] [blame] | 1565 | nsh = kmem_cache_zalloc(sc, GFP_KERNEL); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1566 | if (!nsh) |
| 1567 | break; |
| 1568 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1569 | nsh->raid_conf = conf; |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 1570 | #ifdef CONFIG_MULTICORE_RAID456 |
| 1571 | init_waitqueue_head(&nsh->ops.wait_for_ops); |
| 1572 | #endif |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1573 | |
| 1574 | list_add(&nsh->lru, &newstripes); |
| 1575 | } |
| 1576 | if (i) { |
| 1577 | /* didn't get enough, give up */ |
| 1578 | while (!list_empty(&newstripes)) { |
| 1579 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 1580 | list_del(&nsh->lru); |
| 1581 | kmem_cache_free(sc, nsh); |
| 1582 | } |
| 1583 | kmem_cache_destroy(sc); |
| 1584 | return -ENOMEM; |
| 1585 | } |
| 1586 | /* Step 2 - Must use GFP_NOIO now. |
| 1587 | * OK, we have enough stripes, start collecting inactive |
| 1588 | * stripes and copying them over |
| 1589 | */ |
| 1590 | list_for_each_entry(nsh, &newstripes, lru) { |
| 1591 | spin_lock_irq(&conf->device_lock); |
| 1592 | wait_event_lock_irq(conf->wait_for_stripe, |
| 1593 | !list_empty(&conf->inactive_list), |
| 1594 | conf->device_lock, |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 1595 | ); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1596 | osh = get_free_stripe(conf); |
| 1597 | spin_unlock_irq(&conf->device_lock); |
| 1598 | atomic_set(&nsh->count, 1); |
| 1599 | for(i=0; i<conf->pool_size; i++) |
| 1600 | nsh->dev[i].page = osh->dev[i].page; |
| 1601 | for( ; i<newsize; i++) |
| 1602 | nsh->dev[i].page = NULL; |
| 1603 | kmem_cache_free(conf->slab_cache, osh); |
| 1604 | } |
| 1605 | kmem_cache_destroy(conf->slab_cache); |
| 1606 | |
| 1607 | /* Step 3. |
| 1608 | * At this point, we are holding all the stripes so the array |
| 1609 | * is completely stalled, so now is a good time to resize |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1610 | * conf->disks and the scribble region |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1611 | */ |
| 1612 | ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO); |
| 1613 | if (ndisks) { |
| 1614 | for (i=0; i<conf->raid_disks; i++) |
| 1615 | ndisks[i] = conf->disks[i]; |
| 1616 | kfree(conf->disks); |
| 1617 | conf->disks = ndisks; |
| 1618 | } else |
| 1619 | err = -ENOMEM; |
| 1620 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1621 | get_online_cpus(); |
| 1622 | conf->scribble_len = scribble_len(newsize); |
| 1623 | for_each_present_cpu(cpu) { |
| 1624 | struct raid5_percpu *percpu; |
| 1625 | void *scribble; |
| 1626 | |
| 1627 | percpu = per_cpu_ptr(conf->percpu, cpu); |
| 1628 | scribble = kmalloc(conf->scribble_len, GFP_NOIO); |
| 1629 | |
| 1630 | if (scribble) { |
| 1631 | kfree(percpu->scribble); |
| 1632 | percpu->scribble = scribble; |
| 1633 | } else { |
| 1634 | err = -ENOMEM; |
| 1635 | break; |
| 1636 | } |
| 1637 | } |
| 1638 | put_online_cpus(); |
| 1639 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1640 | /* Step 4, return new stripes to service */ |
| 1641 | while(!list_empty(&newstripes)) { |
| 1642 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 1643 | list_del_init(&nsh->lru); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1644 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1645 | for (i=conf->raid_disks; i < newsize; i++) |
| 1646 | if (nsh->dev[i].page == NULL) { |
| 1647 | struct page *p = alloc_page(GFP_NOIO); |
| 1648 | nsh->dev[i].page = p; |
| 1649 | if (!p) |
| 1650 | err = -ENOMEM; |
| 1651 | } |
| 1652 | release_stripe(nsh); |
| 1653 | } |
| 1654 | /* critical section pass, GFP_NOIO no longer needed */ |
| 1655 | |
| 1656 | conf->slab_cache = sc; |
| 1657 | conf->active_name = 1-conf->active_name; |
| 1658 | conf->pool_size = newsize; |
| 1659 | return err; |
| 1660 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1662 | static int drop_one_stripe(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | { |
| 1664 | struct stripe_head *sh; |
| 1665 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1666 | spin_lock_irq(&conf->device_lock); |
| 1667 | sh = get_free_stripe(conf); |
| 1668 | spin_unlock_irq(&conf->device_lock); |
| 1669 | if (!sh) |
| 1670 | return 0; |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 1671 | BUG_ON(atomic_read(&sh->count)); |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 1672 | shrink_buffers(sh); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1673 | kmem_cache_free(conf->slab_cache, sh); |
| 1674 | atomic_dec(&conf->active_stripes); |
| 1675 | return 1; |
| 1676 | } |
| 1677 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1678 | static void shrink_stripes(struct r5conf *conf) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1679 | { |
| 1680 | while (drop_one_stripe(conf)) |
| 1681 | ; |
| 1682 | |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1683 | if (conf->slab_cache) |
| 1684 | kmem_cache_destroy(conf->slab_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | conf->slab_cache = NULL; |
| 1686 | } |
| 1687 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1688 | static void raid5_end_read_request(struct bio * bi, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1690 | struct stripe_head *sh = bi->bi_private; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1691 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 1692 | int disks = sh->disks, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1694 | char b[BDEVNAME_SIZE]; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1695 | struct md_rdev *rdev = NULL; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1696 | sector_t s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | |
| 1698 | for (i=0 ; i<disks; i++) |
| 1699 | if (bi == &sh->dev[i].req) |
| 1700 | break; |
| 1701 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1702 | pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n", |
| 1703 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | uptodate); |
| 1705 | if (i == disks) { |
| 1706 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1707 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | } |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1709 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1710 | /* If replacement finished while this request was outstanding, |
| 1711 | * 'replacement' might be NULL already. |
| 1712 | * In that case it moved down to 'rdev'. |
| 1713 | * rdev is not removed until all requests are finished. |
| 1714 | */ |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1715 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1716 | if (!rdev) |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1717 | rdev = conf->disks[i].rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1719 | if (use_new_offset(conf, sh)) |
| 1720 | s = sh->sector + rdev->new_data_offset; |
| 1721 | else |
| 1722 | s = sh->sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | if (uptodate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | set_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1725 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1726 | /* Note that this cannot happen on a |
| 1727 | * replacement device. We just fail those on |
| 1728 | * any error |
| 1729 | */ |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1730 | printk_ratelimited( |
| 1731 | KERN_INFO |
| 1732 | "md/raid:%s: read error corrected" |
| 1733 | " (%lu sectors at %llu on %s)\n", |
| 1734 | mdname(conf->mddev), STRIPE_SECTORS, |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1735 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1736 | bdevname(rdev->bdev, b)); |
Namhyung Kim | ddd5115 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1737 | atomic_add(STRIPE_SECTORS, &rdev->corrected_errors); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1738 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 1739 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
| 1740 | } |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1741 | if (atomic_read(&rdev->read_errors)) |
| 1742 | atomic_set(&rdev->read_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | } else { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1744 | const char *bdn = bdevname(rdev->bdev, b); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1745 | int retry = 0; |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1746 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | clear_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1748 | atomic_inc(&rdev->read_errors); |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1749 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) |
| 1750 | printk_ratelimited( |
| 1751 | KERN_WARNING |
| 1752 | "md/raid:%s: read error on replacement device " |
| 1753 | "(sector %llu on %s).\n", |
| 1754 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1755 | (unsigned long long)s, |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1756 | bdn); |
| 1757 | else if (conf->mddev->degraded >= conf->max_degraded) |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1758 | printk_ratelimited( |
| 1759 | KERN_WARNING |
| 1760 | "md/raid:%s: read error not correctable " |
| 1761 | "(sector %llu on %s).\n", |
| 1762 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1763 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1764 | bdn); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1765 | else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1766 | /* Oh, no!!! */ |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1767 | printk_ratelimited( |
| 1768 | KERN_WARNING |
| 1769 | "md/raid:%s: read error NOT corrected!! " |
| 1770 | "(sector %llu on %s).\n", |
| 1771 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1772 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1773 | bdn); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1774 | else if (atomic_read(&rdev->read_errors) |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1775 | > conf->max_nr_stripes) |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 1776 | printk(KERN_WARNING |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 1777 | "md/raid:%s: Too many read errors, failing device %s.\n", |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1778 | mdname(conf->mddev), bdn); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1779 | else |
| 1780 | retry = 1; |
| 1781 | if (retry) |
| 1782 | set_bit(R5_ReadError, &sh->dev[i].flags); |
| 1783 | else { |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1784 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 1785 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1786 | md_error(conf->mddev, rdev); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1787 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | } |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1789 | rdev_dec_pending(rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1790 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 1791 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1792 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | } |
| 1794 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 1795 | static void raid5_end_write_request(struct bio *bi, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1797 | struct stripe_head *sh = bi->bi_private; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1798 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 1799 | int disks = sh->disks, i; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1800 | struct md_rdev *uninitialized_var(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1801 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 1802 | sector_t first_bad; |
| 1803 | int bad_sectors; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1804 | int replacement = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1806 | for (i = 0 ; i < disks; i++) { |
| 1807 | if (bi == &sh->dev[i].req) { |
| 1808 | rdev = conf->disks[i].rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1809 | break; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1810 | } |
| 1811 | if (bi == &sh->dev[i].rreq) { |
| 1812 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1813 | if (rdev) |
| 1814 | replacement = 1; |
| 1815 | else |
| 1816 | /* rdev was removed and 'replacement' |
| 1817 | * replaced it. rdev is not removed |
| 1818 | * until all requests are finished. |
| 1819 | */ |
| 1820 | rdev = conf->disks[i].rdev; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1821 | break; |
| 1822 | } |
| 1823 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1824 | pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
| 1826 | uptodate); |
| 1827 | if (i == disks) { |
| 1828 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1829 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | } |
| 1831 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1832 | if (replacement) { |
| 1833 | if (!uptodate) |
| 1834 | md_error(conf->mddev, rdev); |
| 1835 | else if (is_badblock(rdev, sh->sector, |
| 1836 | STRIPE_SECTORS, |
| 1837 | &first_bad, &bad_sectors)) |
| 1838 | set_bit(R5_MadeGoodRepl, &sh->dev[i].flags); |
| 1839 | } else { |
| 1840 | if (!uptodate) { |
| 1841 | set_bit(WriteErrorSeen, &rdev->flags); |
| 1842 | set_bit(R5_WriteError, &sh->dev[i].flags); |
NeilBrown | 3a6de29 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 1843 | if (!test_and_set_bit(WantReplacement, &rdev->flags)) |
| 1844 | set_bit(MD_RECOVERY_NEEDED, |
| 1845 | &rdev->mddev->recovery); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1846 | } else if (is_badblock(rdev, sh->sector, |
| 1847 | STRIPE_SECTORS, |
| 1848 | &first_bad, &bad_sectors)) |
| 1849 | set_bit(R5_MadeGood, &sh->dev[i].flags); |
| 1850 | } |
| 1851 | rdev_dec_pending(rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1853 | if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags)) |
| 1854 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1855 | set_bit(STRIPE_HANDLE, &sh->state); |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1856 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | } |
| 1858 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1859 | 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] | 1860 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1861 | 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] | 1862 | { |
| 1863 | struct r5dev *dev = &sh->dev[i]; |
| 1864 | |
| 1865 | bio_init(&dev->req); |
| 1866 | dev->req.bi_io_vec = &dev->vec; |
| 1867 | dev->req.bi_vcnt++; |
| 1868 | dev->req.bi_max_vecs++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | dev->req.bi_private = sh; |
NeilBrown | 995c427 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1870 | dev->vec.bv_page = dev->page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1872 | bio_init(&dev->rreq); |
| 1873 | dev->rreq.bi_io_vec = &dev->rvec; |
| 1874 | dev->rreq.bi_vcnt++; |
| 1875 | dev->rreq.bi_max_vecs++; |
| 1876 | dev->rreq.bi_private = sh; |
| 1877 | dev->rvec.bv_page = dev->page; |
| 1878 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1879 | dev->flags = 0; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1880 | dev->sector = compute_blocknr(sh, i, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1881 | } |
| 1882 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1883 | static void error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | { |
| 1885 | char b[BDEVNAME_SIZE]; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1886 | struct r5conf *conf = mddev->private; |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 1887 | unsigned long flags; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 1888 | pr_debug("raid456: error called\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 1890 | spin_lock_irqsave(&conf->device_lock, flags); |
| 1891 | clear_bit(In_sync, &rdev->flags); |
| 1892 | mddev->degraded = calc_degraded(conf); |
| 1893 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 1894 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 1895 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 1896 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 6f8d0c7 | 2011-05-11 14:38:44 +1000 | [diff] [blame] | 1897 | set_bit(Faulty, &rdev->flags); |
| 1898 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 1899 | printk(KERN_ALERT |
| 1900 | "md/raid:%s: Disk failure on %s, disabling device.\n" |
| 1901 | "md/raid:%s: Operation continuing on %d devices.\n", |
| 1902 | mdname(mddev), |
| 1903 | bdevname(rdev->bdev, b), |
| 1904 | mdname(mddev), |
| 1905 | conf->raid_disks - mddev->degraded); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1906 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1907 | |
| 1908 | /* |
| 1909 | * Input: a 'big' sector number, |
| 1910 | * Output: index of the data and parity disk, and the sector # in them. |
| 1911 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1912 | static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1913 | int previous, int *dd_idx, |
| 1914 | struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | { |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1916 | sector_t stripe, stripe2; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 1917 | sector_t chunk_number; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1918 | unsigned int chunk_offset; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1919 | int pd_idx, qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1920 | int ddf_layout = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | sector_t new_sector; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1922 | int algorithm = previous ? conf->prev_algo |
| 1923 | : conf->algorithm; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 1924 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
| 1925 | : conf->chunk_sectors; |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1926 | int raid_disks = previous ? conf->previous_raid_disks |
| 1927 | : conf->raid_disks; |
| 1928 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1929 | |
| 1930 | /* First compute the information on this sector */ |
| 1931 | |
| 1932 | /* |
| 1933 | * Compute the chunk number and the sector offset inside the chunk |
| 1934 | */ |
| 1935 | chunk_offset = sector_div(r_sector, sectors_per_chunk); |
| 1936 | chunk_number = r_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1937 | |
| 1938 | /* |
| 1939 | * Compute the stripe number |
| 1940 | */ |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 1941 | stripe = chunk_number; |
| 1942 | *dd_idx = sector_div(stripe, data_disks); |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1943 | stripe2 = stripe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | /* |
| 1945 | * Select the parity disk based on the user selected algorithm. |
| 1946 | */ |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1947 | pd_idx = qd_idx = -1; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1948 | switch(conf->level) { |
| 1949 | case 4: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1950 | pd_idx = data_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1951 | break; |
| 1952 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1953 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1954 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1955 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1956 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1957 | (*dd_idx)++; |
| 1958 | break; |
| 1959 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1960 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1961 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1962 | (*dd_idx)++; |
| 1963 | break; |
| 1964 | case ALGORITHM_LEFT_SYMMETRIC: |
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 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1967 | break; |
| 1968 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1969 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1970 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1971 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1972 | case ALGORITHM_PARITY_0: |
| 1973 | pd_idx = 0; |
| 1974 | (*dd_idx)++; |
| 1975 | break; |
| 1976 | case ALGORITHM_PARITY_N: |
| 1977 | pd_idx = data_disks; |
| 1978 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1980 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1981 | } |
| 1982 | break; |
| 1983 | case 6: |
| 1984 | |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1985 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1986 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1987 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1988 | qd_idx = pd_idx + 1; |
| 1989 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1990 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1991 | qd_idx = 0; |
| 1992 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1993 | (*dd_idx) += 2; /* D D P Q D */ |
| 1994 | break; |
| 1995 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1996 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1997 | qd_idx = pd_idx + 1; |
| 1998 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1999 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2000 | qd_idx = 0; |
| 2001 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2002 | (*dd_idx) += 2; /* D D P Q D */ |
| 2003 | break; |
| 2004 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2005 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2006 | qd_idx = (pd_idx + 1) % raid_disks; |
| 2007 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2008 | break; |
| 2009 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2010 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2011 | qd_idx = (pd_idx + 1) % raid_disks; |
| 2012 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2013 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2014 | |
| 2015 | case ALGORITHM_PARITY_0: |
| 2016 | pd_idx = 0; |
| 2017 | qd_idx = 1; |
| 2018 | (*dd_idx) += 2; |
| 2019 | break; |
| 2020 | case ALGORITHM_PARITY_N: |
| 2021 | pd_idx = data_disks; |
| 2022 | qd_idx = data_disks + 1; |
| 2023 | break; |
| 2024 | |
| 2025 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 2026 | /* Exactly the same as RIGHT_ASYMMETRIC, but or |
| 2027 | * of blocks for computing Q is different. |
| 2028 | */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2029 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2030 | qd_idx = pd_idx + 1; |
| 2031 | if (pd_idx == raid_disks-1) { |
| 2032 | (*dd_idx)++; /* Q D D D P */ |
| 2033 | qd_idx = 0; |
| 2034 | } else if (*dd_idx >= pd_idx) |
| 2035 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2036 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2037 | break; |
| 2038 | |
| 2039 | case ALGORITHM_ROTATING_N_RESTART: |
| 2040 | /* Same a left_asymmetric, by first stripe is |
| 2041 | * D D D P Q rather than |
| 2042 | * Q D D D P |
| 2043 | */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2044 | stripe2 += 1; |
| 2045 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2046 | qd_idx = pd_idx + 1; |
| 2047 | if (pd_idx == raid_disks-1) { |
| 2048 | (*dd_idx)++; /* Q D D D P */ |
| 2049 | qd_idx = 0; |
| 2050 | } else if (*dd_idx >= pd_idx) |
| 2051 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2052 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2053 | break; |
| 2054 | |
| 2055 | case ALGORITHM_ROTATING_N_CONTINUE: |
| 2056 | /* Same as left_symmetric but Q is before P */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2057 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2058 | qd_idx = (pd_idx + raid_disks - 1) % raid_disks; |
| 2059 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2060 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2061 | break; |
| 2062 | |
| 2063 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 2064 | /* RAID5 left_asymmetric, with Q on last device */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2065 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2066 | if (*dd_idx >= pd_idx) |
| 2067 | (*dd_idx)++; |
| 2068 | qd_idx = raid_disks - 1; |
| 2069 | break; |
| 2070 | |
| 2071 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2072 | pd_idx = sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2073 | if (*dd_idx >= pd_idx) |
| 2074 | (*dd_idx)++; |
| 2075 | qd_idx = raid_disks - 1; |
| 2076 | break; |
| 2077 | |
| 2078 | case ALGORITHM_LEFT_SYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2079 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2080 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 2081 | qd_idx = raid_disks - 1; |
| 2082 | break; |
| 2083 | |
| 2084 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2085 | pd_idx = sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2086 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 2087 | qd_idx = raid_disks - 1; |
| 2088 | break; |
| 2089 | |
| 2090 | case ALGORITHM_PARITY_0_6: |
| 2091 | pd_idx = 0; |
| 2092 | (*dd_idx)++; |
| 2093 | qd_idx = raid_disks - 1; |
| 2094 | break; |
| 2095 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2096 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2097 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2098 | } |
| 2099 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | } |
| 2101 | |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2102 | if (sh) { |
| 2103 | sh->pd_idx = pd_idx; |
| 2104 | sh->qd_idx = qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2105 | sh->ddf_layout = ddf_layout; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2106 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2107 | /* |
| 2108 | * Finally, compute the new sector number |
| 2109 | */ |
| 2110 | new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset; |
| 2111 | return new_sector; |
| 2112 | } |
| 2113 | |
| 2114 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2115 | 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] | 2116 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2117 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 2118 | int raid_disks = sh->disks; |
| 2119 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2120 | sector_t new_sector = sh->sector, check; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 2121 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
| 2122 | : conf->chunk_sectors; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2123 | int algorithm = previous ? conf->prev_algo |
| 2124 | : conf->algorithm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2125 | sector_t stripe; |
| 2126 | int chunk_offset; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2127 | sector_t chunk_number; |
| 2128 | int dummy1, dd_idx = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | sector_t r_sector; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2130 | struct stripe_head sh2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2131 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2133 | chunk_offset = sector_div(new_sector, sectors_per_chunk); |
| 2134 | stripe = new_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2135 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2136 | if (i == sh->pd_idx) |
| 2137 | return 0; |
| 2138 | switch(conf->level) { |
| 2139 | case 4: break; |
| 2140 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2141 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 2143 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 2144 | if (i > sh->pd_idx) |
| 2145 | i--; |
| 2146 | break; |
| 2147 | case ALGORITHM_LEFT_SYMMETRIC: |
| 2148 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 2149 | if (i < sh->pd_idx) |
| 2150 | i += raid_disks; |
| 2151 | i -= (sh->pd_idx + 1); |
| 2152 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2153 | case ALGORITHM_PARITY_0: |
| 2154 | i -= 1; |
| 2155 | break; |
| 2156 | case ALGORITHM_PARITY_N: |
| 2157 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2159 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2160 | } |
| 2161 | break; |
| 2162 | case 6: |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2163 | if (i == sh->qd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2164 | return 0; /* It is the Q disk */ |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2165 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2166 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 2167 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2168 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 2169 | case ALGORITHM_ROTATING_N_RESTART: |
| 2170 | if (sh->pd_idx == raid_disks-1) |
| 2171 | i--; /* Q D D D P */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2172 | else if (i > sh->pd_idx) |
| 2173 | i -= 2; /* D D P Q D */ |
| 2174 | break; |
| 2175 | case ALGORITHM_LEFT_SYMMETRIC: |
| 2176 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 2177 | if (sh->pd_idx == raid_disks-1) |
| 2178 | i--; /* Q D D D P */ |
| 2179 | else { |
| 2180 | /* D D P Q D */ |
| 2181 | if (i < sh->pd_idx) |
| 2182 | i += raid_disks; |
| 2183 | i -= (sh->pd_idx + 2); |
| 2184 | } |
| 2185 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2186 | case ALGORITHM_PARITY_0: |
| 2187 | i -= 2; |
| 2188 | break; |
| 2189 | case ALGORITHM_PARITY_N: |
| 2190 | break; |
| 2191 | case ALGORITHM_ROTATING_N_CONTINUE: |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 2192 | /* Like left_symmetric, but P is before Q */ |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2193 | if (sh->pd_idx == 0) |
| 2194 | i--; /* P D D D Q */ |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 2195 | else { |
| 2196 | /* D D Q P D */ |
| 2197 | if (i < sh->pd_idx) |
| 2198 | i += raid_disks; |
| 2199 | i -= (sh->pd_idx + 1); |
| 2200 | } |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2201 | break; |
| 2202 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 2203 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 2204 | if (i > sh->pd_idx) |
| 2205 | i--; |
| 2206 | break; |
| 2207 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 2208 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 2209 | if (i < sh->pd_idx) |
| 2210 | i += data_disks + 1; |
| 2211 | i -= (sh->pd_idx + 1); |
| 2212 | break; |
| 2213 | case ALGORITHM_PARITY_0_6: |
| 2214 | i -= 1; |
| 2215 | break; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2216 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2217 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2218 | } |
| 2219 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2220 | } |
| 2221 | |
| 2222 | chunk_number = stripe * data_disks + i; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2223 | r_sector = chunk_number * sectors_per_chunk + chunk_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2224 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2225 | check = raid5_compute_sector(conf, r_sector, |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2226 | previous, &dummy1, &sh2); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2227 | if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx |
| 2228 | || sh2.qd_idx != sh->qd_idx) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 2229 | printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n", |
| 2230 | mdname(conf->mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2231 | return 0; |
| 2232 | } |
| 2233 | return r_sector; |
| 2234 | } |
| 2235 | |
| 2236 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2237 | static void |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2238 | schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2239 | int rcw, int expand) |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2240 | { |
| 2241 | int i, pd_idx = sh->pd_idx, disks = sh->disks; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2242 | struct r5conf *conf = sh->raid_conf; |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2243 | int level = conf->level; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2244 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2245 | if (rcw) { |
| 2246 | /* if we are not expanding this is a proper write request, and |
| 2247 | * there will be bios with new data to be drained into the |
| 2248 | * stripe cache |
| 2249 | */ |
| 2250 | if (!expand) { |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2251 | sh->reconstruct_state = reconstruct_state_drain_run; |
| 2252 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
| 2253 | } else |
| 2254 | sh->reconstruct_state = reconstruct_state_run; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2255 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2256 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2257 | |
| 2258 | for (i = disks; i--; ) { |
| 2259 | struct r5dev *dev = &sh->dev[i]; |
| 2260 | |
| 2261 | if (dev->towrite) { |
| 2262 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2263 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2264 | if (!expand) |
| 2265 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2266 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2267 | } |
| 2268 | } |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2269 | if (s->locked + conf->max_degraded == disks) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2270 | if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state)) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2271 | atomic_inc(&conf->pending_full_writes); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2272 | } else { |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2273 | BUG_ON(level == 6); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2274 | BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) || |
| 2275 | test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags))); |
| 2276 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2277 | sh->reconstruct_state = reconstruct_state_prexor_drain_run; |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2278 | set_bit(STRIPE_OP_PREXOR, &s->ops_request); |
| 2279 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2280 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2281 | |
| 2282 | for (i = disks; i--; ) { |
| 2283 | struct r5dev *dev = &sh->dev[i]; |
| 2284 | if (i == pd_idx) |
| 2285 | continue; |
| 2286 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2287 | if (dev->towrite && |
| 2288 | (test_bit(R5_UPTODATE, &dev->flags) || |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2289 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2290 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2291 | set_bit(R5_LOCKED, &dev->flags); |
| 2292 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2293 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2294 | } |
| 2295 | } |
| 2296 | } |
| 2297 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2298 | /* keep the parity disk(s) locked while asynchronous operations |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2299 | * are in flight |
| 2300 | */ |
| 2301 | set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); |
| 2302 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].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 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2305 | if (level == 6) { |
| 2306 | int qd_idx = sh->qd_idx; |
| 2307 | struct r5dev *dev = &sh->dev[qd_idx]; |
| 2308 | |
| 2309 | set_bit(R5_LOCKED, &dev->flags); |
| 2310 | clear_bit(R5_UPTODATE, &dev->flags); |
| 2311 | s->locked++; |
| 2312 | } |
| 2313 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2314 | pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n", |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 2315 | __func__, (unsigned long long)sh->sector, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2316 | s->locked, s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2317 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2318 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2319 | /* |
| 2320 | * Each stripe/dev can have one or more bion attached. |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2321 | * toread/towrite point to the first in a chain. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2322 | * The bi_next chain must be in order. |
| 2323 | */ |
| 2324 | static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite) |
| 2325 | { |
| 2326 | struct bio **bip; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2327 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2328 | int firstwrite=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2329 | |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 2330 | pr_debug("adding bi b#%llu to stripe s#%llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2331 | (unsigned long long)bi->bi_sector, |
| 2332 | (unsigned long long)sh->sector); |
| 2333 | |
| 2334 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2335 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2336 | if (forwrite) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2337 | bip = &sh->dev[dd_idx].towrite; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2338 | if (*bip == NULL && sh->dev[dd_idx].written == NULL) |
| 2339 | firstwrite = 1; |
| 2340 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2341 | bip = &sh->dev[dd_idx].toread; |
| 2342 | while (*bip && (*bip)->bi_sector < bi->bi_sector) { |
| 2343 | if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector) |
| 2344 | goto overlap; |
| 2345 | bip = & (*bip)->bi_next; |
| 2346 | } |
| 2347 | if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9)) |
| 2348 | goto overlap; |
| 2349 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 2350 | BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2351 | if (*bip) |
| 2352 | bi->bi_next = *bip; |
| 2353 | *bip = bi; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2354 | bi->bi_phys_segments++; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2356 | if (forwrite) { |
| 2357 | /* check if page is covered */ |
| 2358 | sector_t sector = sh->dev[dd_idx].sector; |
| 2359 | for (bi=sh->dev[dd_idx].towrite; |
| 2360 | sector < sh->dev[dd_idx].sector + STRIPE_SECTORS && |
| 2361 | bi && bi->bi_sector <= sector; |
| 2362 | bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) { |
| 2363 | if (bi->bi_sector + (bi->bi_size>>9) >= sector) |
| 2364 | sector = bi->bi_sector + (bi->bi_size>>9); |
| 2365 | } |
| 2366 | if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS) |
| 2367 | set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags); |
| 2368 | } |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 2369 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 2370 | |
| 2371 | pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n", |
| 2372 | (unsigned long long)(*bip)->bi_sector, |
| 2373 | (unsigned long long)sh->sector, dd_idx); |
| 2374 | |
| 2375 | if (conf->mddev->bitmap && firstwrite) { |
| 2376 | bitmap_startwrite(conf->mddev->bitmap, sh->sector, |
| 2377 | STRIPE_SECTORS, 0); |
| 2378 | sh->bm_seq = conf->seq_flush+1; |
| 2379 | set_bit(STRIPE_BIT_DELAY, &sh->state); |
| 2380 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2381 | return 1; |
| 2382 | |
| 2383 | overlap: |
| 2384 | set_bit(R5_Overlap, &sh->dev[dd_idx].flags); |
| 2385 | spin_unlock_irq(&conf->device_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2386 | return 0; |
| 2387 | } |
| 2388 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2389 | static void end_reshape(struct r5conf *conf); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 2390 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2391 | static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2392 | struct stripe_head *sh) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2393 | { |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2394 | int sectors_per_chunk = |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 2395 | previous ? conf->prev_chunk_sectors : conf->chunk_sectors; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2396 | int dd_idx; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 2397 | int chunk_offset = sector_div(stripe, sectors_per_chunk); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2398 | int disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 2399 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2400 | raid5_compute_sector(conf, |
| 2401 | stripe * (disks - conf->max_degraded) |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 2402 | *sectors_per_chunk + chunk_offset, |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2403 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2404 | &dd_idx, sh); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2405 | } |
| 2406 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2407 | static void |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2408 | handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2409 | struct stripe_head_state *s, int disks, |
| 2410 | struct bio **return_bi) |
| 2411 | { |
| 2412 | int i; |
| 2413 | for (i = disks; i--; ) { |
| 2414 | struct bio *bi; |
| 2415 | int bitmap_end = 0; |
| 2416 | |
| 2417 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2418 | struct md_rdev *rdev; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2419 | rcu_read_lock(); |
| 2420 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 2421 | if (rdev && test_bit(In_sync, &rdev->flags)) |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2422 | atomic_inc(&rdev->nr_pending); |
| 2423 | else |
| 2424 | rdev = NULL; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2425 | rcu_read_unlock(); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2426 | if (rdev) { |
| 2427 | if (!rdev_set_badblocks( |
| 2428 | rdev, |
| 2429 | sh->sector, |
| 2430 | STRIPE_SECTORS, 0)) |
| 2431 | md_error(conf->mddev, rdev); |
| 2432 | rdev_dec_pending(rdev, conf->mddev); |
| 2433 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2434 | } |
| 2435 | spin_lock_irq(&conf->device_lock); |
| 2436 | /* fail all writes first */ |
| 2437 | bi = sh->dev[i].towrite; |
| 2438 | sh->dev[i].towrite = NULL; |
| 2439 | if (bi) { |
| 2440 | s->to_write--; |
| 2441 | bitmap_end = 1; |
| 2442 | } |
| 2443 | |
| 2444 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 2445 | wake_up(&conf->wait_for_overlap); |
| 2446 | |
| 2447 | while (bi && bi->bi_sector < |
| 2448 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2449 | struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); |
| 2450 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2451 | if (!raid5_dec_bi_phys_segments(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2452 | md_write_end(conf->mddev); |
| 2453 | bi->bi_next = *return_bi; |
| 2454 | *return_bi = bi; |
| 2455 | } |
| 2456 | bi = nextbi; |
| 2457 | } |
| 2458 | /* and fail all 'written' */ |
| 2459 | bi = sh->dev[i].written; |
| 2460 | sh->dev[i].written = NULL; |
| 2461 | if (bi) bitmap_end = 1; |
| 2462 | while (bi && bi->bi_sector < |
| 2463 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2464 | struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector); |
| 2465 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2466 | if (!raid5_dec_bi_phys_segments(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2467 | md_write_end(conf->mddev); |
| 2468 | bi->bi_next = *return_bi; |
| 2469 | *return_bi = bi; |
| 2470 | } |
| 2471 | bi = bi2; |
| 2472 | } |
| 2473 | |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2474 | /* fail any reads if this device is non-operational and |
| 2475 | * the data has not reached the cache yet. |
| 2476 | */ |
| 2477 | if (!test_bit(R5_Wantfill, &sh->dev[i].flags) && |
| 2478 | (!test_bit(R5_Insync, &sh->dev[i].flags) || |
| 2479 | test_bit(R5_ReadError, &sh->dev[i].flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2480 | bi = sh->dev[i].toread; |
| 2481 | sh->dev[i].toread = NULL; |
| 2482 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 2483 | wake_up(&conf->wait_for_overlap); |
| 2484 | if (bi) s->to_read--; |
| 2485 | while (bi && bi->bi_sector < |
| 2486 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2487 | struct bio *nextbi = |
| 2488 | r5_next_bio(bi, sh->dev[i].sector); |
| 2489 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2490 | if (!raid5_dec_bi_phys_segments(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2491 | bi->bi_next = *return_bi; |
| 2492 | *return_bi = bi; |
| 2493 | } |
| 2494 | bi = nextbi; |
| 2495 | } |
| 2496 | } |
| 2497 | spin_unlock_irq(&conf->device_lock); |
| 2498 | if (bitmap_end) |
| 2499 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 2500 | STRIPE_SECTORS, 0, 0); |
NeilBrown | 8cfa7b0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2501 | /* If we were in the middle of a write the parity block might |
| 2502 | * still be locked - so just clear all R5_LOCKED flags |
| 2503 | */ |
| 2504 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2505 | } |
| 2506 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2507 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 2508 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 2509 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2510 | } |
| 2511 | |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2512 | static void |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2513 | handle_failed_sync(struct r5conf *conf, struct stripe_head *sh, |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2514 | struct stripe_head_state *s) |
| 2515 | { |
| 2516 | int abort = 0; |
| 2517 | int i; |
| 2518 | |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2519 | clear_bit(STRIPE_SYNCING, &sh->state); |
| 2520 | s->syncing = 0; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2521 | s->replacing = 0; |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2522 | /* There is nothing more to do for sync/check/repair. |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 2523 | * Don't even need to abort as that is handled elsewhere |
| 2524 | * if needed, and not always wanted e.g. if there is a known |
| 2525 | * bad block here. |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2526 | * For recover/replace we need to record a bad block on all |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2527 | * non-sync devices, or abort the recovery |
| 2528 | */ |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 2529 | if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) { |
| 2530 | /* During recovery devices cannot be removed, so |
| 2531 | * locking and refcounting of rdevs is not needed |
| 2532 | */ |
| 2533 | for (i = 0; i < conf->raid_disks; i++) { |
| 2534 | struct md_rdev *rdev = conf->disks[i].rdev; |
| 2535 | if (rdev |
| 2536 | && !test_bit(Faulty, &rdev->flags) |
| 2537 | && !test_bit(In_sync, &rdev->flags) |
| 2538 | && !rdev_set_badblocks(rdev, sh->sector, |
| 2539 | STRIPE_SECTORS, 0)) |
| 2540 | abort = 1; |
| 2541 | rdev = conf->disks[i].replacement; |
| 2542 | if (rdev |
| 2543 | && !test_bit(Faulty, &rdev->flags) |
| 2544 | && !test_bit(In_sync, &rdev->flags) |
| 2545 | && !rdev_set_badblocks(rdev, sh->sector, |
| 2546 | STRIPE_SECTORS, 0)) |
| 2547 | abort = 1; |
| 2548 | } |
| 2549 | if (abort) |
| 2550 | conf->recovery_disabled = |
| 2551 | conf->mddev->recovery_disabled; |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2552 | } |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 2553 | md_done_sync(conf->mddev, STRIPE_SECTORS, !abort); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2554 | } |
| 2555 | |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2556 | static int want_replace(struct stripe_head *sh, int disk_idx) |
| 2557 | { |
| 2558 | struct md_rdev *rdev; |
| 2559 | int rv = 0; |
| 2560 | /* Doing recovery so rcu locking not required */ |
| 2561 | rdev = sh->raid_conf->disks[disk_idx].replacement; |
| 2562 | if (rdev |
| 2563 | && !test_bit(Faulty, &rdev->flags) |
| 2564 | && !test_bit(In_sync, &rdev->flags) |
| 2565 | && (rdev->recovery_offset <= sh->sector |
| 2566 | || rdev->mddev->recovery_cp <= sh->sector)) |
| 2567 | rv = 1; |
| 2568 | |
| 2569 | return rv; |
| 2570 | } |
| 2571 | |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2572 | /* 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] | 2573 | * to be read or computed to satisfy a request. |
| 2574 | * |
| 2575 | * Returns 1 when no more member devices need to be checked, otherwise returns |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2576 | * 0 to tell the loop in handle_stripe_fill to continue |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2577 | */ |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2578 | static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s, |
| 2579 | int disk_idx, int disks) |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2580 | { |
| 2581 | struct r5dev *dev = &sh->dev[disk_idx]; |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2582 | struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]], |
| 2583 | &sh->dev[s->failed_num[1]] }; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2584 | |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2585 | /* is the data in this block needed, and can we get it? */ |
| 2586 | if (!test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2587 | !test_bit(R5_UPTODATE, &dev->flags) && |
| 2588 | (dev->toread || |
| 2589 | (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) || |
| 2590 | s->syncing || s->expanding || |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2591 | (s->replacing && want_replace(sh, disk_idx)) || |
NeilBrown | 5d35e09 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2592 | (s->failed >= 1 && fdev[0]->toread) || |
| 2593 | (s->failed >= 2 && fdev[1]->toread) || |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2594 | (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite && |
| 2595 | !test_bit(R5_OVERWRITE, &fdev[0]->flags)) || |
| 2596 | (sh->raid_conf->level == 6 && s->failed && s->to_write))) { |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2597 | /* we would like to get this block, possibly by computing it, |
| 2598 | * otherwise read it if the backing disk is insync |
| 2599 | */ |
| 2600 | BUG_ON(test_bit(R5_Wantcompute, &dev->flags)); |
| 2601 | BUG_ON(test_bit(R5_Wantread, &dev->flags)); |
| 2602 | if ((s->uptodate == disks - 1) && |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2603 | (s->failed && (disk_idx == s->failed_num[0] || |
| 2604 | disk_idx == s->failed_num[1]))) { |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2605 | /* have disk failed, and we're requested to fetch it; |
| 2606 | * do compute it |
| 2607 | */ |
| 2608 | pr_debug("Computing stripe %llu block %d\n", |
| 2609 | (unsigned long long)sh->sector, disk_idx); |
| 2610 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2611 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2612 | set_bit(R5_Wantcompute, &dev->flags); |
| 2613 | sh->ops.target = disk_idx; |
| 2614 | sh->ops.target2 = -1; /* no 2nd target */ |
| 2615 | s->req_compute = 1; |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2616 | /* Careful: from this point on 'uptodate' is in the eye |
| 2617 | * of raid_run_ops which services 'compute' operations |
| 2618 | * before writes. R5_Wantcompute flags a block that will |
| 2619 | * be R5_UPTODATE by the time it is needed for a |
| 2620 | * subsequent operation. |
| 2621 | */ |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2622 | s->uptodate++; |
| 2623 | return 1; |
| 2624 | } else if (s->uptodate == disks-2 && s->failed >= 2) { |
| 2625 | /* Computing 2-failure is *very* expensive; only |
| 2626 | * do it if failed >= 2 |
| 2627 | */ |
| 2628 | int other; |
| 2629 | for (other = disks; other--; ) { |
| 2630 | if (other == disk_idx) |
| 2631 | continue; |
| 2632 | if (!test_bit(R5_UPTODATE, |
| 2633 | &sh->dev[other].flags)) |
| 2634 | break; |
| 2635 | } |
| 2636 | BUG_ON(other < 0); |
| 2637 | pr_debug("Computing stripe %llu blocks %d,%d\n", |
| 2638 | (unsigned long long)sh->sector, |
| 2639 | disk_idx, other); |
| 2640 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2641 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2642 | set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags); |
| 2643 | set_bit(R5_Wantcompute, &sh->dev[other].flags); |
| 2644 | sh->ops.target = disk_idx; |
| 2645 | sh->ops.target2 = other; |
| 2646 | s->uptodate += 2; |
| 2647 | s->req_compute = 1; |
| 2648 | return 1; |
| 2649 | } else if (test_bit(R5_Insync, &dev->flags)) { |
| 2650 | set_bit(R5_LOCKED, &dev->flags); |
| 2651 | set_bit(R5_Wantread, &dev->flags); |
| 2652 | s->locked++; |
| 2653 | pr_debug("Reading block %d (sync=%d)\n", |
| 2654 | disk_idx, s->syncing); |
| 2655 | } |
| 2656 | } |
| 2657 | |
| 2658 | return 0; |
| 2659 | } |
| 2660 | |
| 2661 | /** |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2662 | * handle_stripe_fill - read or compute data to satisfy pending requests. |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2663 | */ |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2664 | static void handle_stripe_fill(struct stripe_head *sh, |
| 2665 | struct stripe_head_state *s, |
| 2666 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2667 | { |
| 2668 | int i; |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2669 | |
| 2670 | /* look for blocks to read/compute, skip this if a compute |
| 2671 | * is already in flight, or if the stripe contents are in the |
| 2672 | * midst of changing due to a write |
| 2673 | */ |
| 2674 | if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state && |
| 2675 | !sh->reconstruct_state) |
| 2676 | for (i = disks; i--; ) |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2677 | if (fetch_block(sh, s, i, disks)) |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2678 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2679 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2680 | } |
| 2681 | |
| 2682 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2683 | /* handle_stripe_clean_event |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2684 | * any written block on an uptodate or failed drive can be returned. |
| 2685 | * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but |
| 2686 | * never LOCKED, so we don't need to test 'failed' directly. |
| 2687 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2688 | static void handle_stripe_clean_event(struct r5conf *conf, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2689 | struct stripe_head *sh, int disks, struct bio **return_bi) |
| 2690 | { |
| 2691 | int i; |
| 2692 | struct r5dev *dev; |
| 2693 | |
| 2694 | for (i = disks; i--; ) |
| 2695 | if (sh->dev[i].written) { |
| 2696 | dev = &sh->dev[i]; |
| 2697 | if (!test_bit(R5_LOCKED, &dev->flags) && |
| 2698 | test_bit(R5_UPTODATE, &dev->flags)) { |
| 2699 | /* We can return any write requests */ |
| 2700 | struct bio *wbi, *wbi2; |
| 2701 | int bitmap_end = 0; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2702 | pr_debug("Return write for disc %d\n", i); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2703 | spin_lock_irq(&conf->device_lock); |
| 2704 | wbi = dev->written; |
| 2705 | dev->written = NULL; |
| 2706 | while (wbi && wbi->bi_sector < |
| 2707 | dev->sector + STRIPE_SECTORS) { |
| 2708 | wbi2 = r5_next_bio(wbi, dev->sector); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2709 | if (!raid5_dec_bi_phys_segments(wbi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2710 | md_write_end(conf->mddev); |
| 2711 | wbi->bi_next = *return_bi; |
| 2712 | *return_bi = wbi; |
| 2713 | } |
| 2714 | wbi = wbi2; |
| 2715 | } |
| 2716 | if (dev->towrite == NULL) |
| 2717 | bitmap_end = 1; |
| 2718 | spin_unlock_irq(&conf->device_lock); |
| 2719 | if (bitmap_end) |
| 2720 | bitmap_endwrite(conf->mddev->bitmap, |
| 2721 | sh->sector, |
| 2722 | STRIPE_SECTORS, |
| 2723 | !test_bit(STRIPE_DEGRADED, &sh->state), |
| 2724 | 0); |
| 2725 | } |
| 2726 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2727 | |
| 2728 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 2729 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 2730 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2731 | } |
| 2732 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2733 | static void handle_stripe_dirtying(struct r5conf *conf, |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2734 | struct stripe_head *sh, |
| 2735 | struct stripe_head_state *s, |
| 2736 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2737 | { |
| 2738 | int rmw = 0, rcw = 0, i; |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2739 | if (conf->max_degraded == 2) { |
| 2740 | /* RAID6 requires 'rcw' in current implementation |
| 2741 | * Calculate the real rcw later - for now fake it |
| 2742 | * look like rcw is cheaper |
| 2743 | */ |
| 2744 | rcw = 1; rmw = 2; |
| 2745 | } else for (i = disks; i--; ) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2746 | /* would I have to read this buffer for read_modify_write */ |
| 2747 | struct r5dev *dev = &sh->dev[i]; |
| 2748 | if ((dev->towrite || i == sh->pd_idx) && |
| 2749 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2750 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2751 | test_bit(R5_Wantcompute, &dev->flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2752 | if (test_bit(R5_Insync, &dev->flags)) |
| 2753 | rmw++; |
| 2754 | else |
| 2755 | rmw += 2*disks; /* cannot read it */ |
| 2756 | } |
| 2757 | /* Would I have to read this buffer for reconstruct_write */ |
| 2758 | if (!test_bit(R5_OVERWRITE, &dev->flags) && 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))) { |
| 2762 | if (test_bit(R5_Insync, &dev->flags)) rcw++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2763 | else |
| 2764 | rcw += 2*disks; |
| 2765 | } |
| 2766 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2767 | pr_debug("for sector %llu, rmw=%d rcw=%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2768 | (unsigned long long)sh->sector, rmw, rcw); |
| 2769 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2770 | if (rmw < rcw && rmw > 0) |
| 2771 | /* prefer read-modify-write, but need to get some data */ |
| 2772 | for (i = disks; i--; ) { |
| 2773 | struct r5dev *dev = &sh->dev[i]; |
| 2774 | if ((dev->towrite || i == sh->pd_idx) && |
| 2775 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2776 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2777 | test_bit(R5_Wantcompute, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2778 | test_bit(R5_Insync, &dev->flags)) { |
| 2779 | if ( |
| 2780 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2781 | pr_debug("Read_old block " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2782 | "%d for r-m-w\n", i); |
| 2783 | set_bit(R5_LOCKED, &dev->flags); |
| 2784 | set_bit(R5_Wantread, &dev->flags); |
| 2785 | s->locked++; |
| 2786 | } else { |
| 2787 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2788 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2789 | } |
| 2790 | } |
| 2791 | } |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2792 | if (rcw <= rmw && rcw > 0) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2793 | /* want reconstruct write, but need to get some data */ |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2794 | rcw = 0; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2795 | for (i = disks; i--; ) { |
| 2796 | struct r5dev *dev = &sh->dev[i]; |
| 2797 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2798 | i != sh->pd_idx && i != sh->qd_idx && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2799 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2800 | !(test_bit(R5_UPTODATE, &dev->flags) || |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2801 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2802 | rcw++; |
| 2803 | if (!test_bit(R5_Insync, &dev->flags)) |
| 2804 | continue; /* it's a failed drive */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2805 | if ( |
| 2806 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2807 | pr_debug("Read_old block " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2808 | "%d for Reconstruct\n", i); |
| 2809 | set_bit(R5_LOCKED, &dev->flags); |
| 2810 | set_bit(R5_Wantread, &dev->flags); |
| 2811 | s->locked++; |
| 2812 | } else { |
| 2813 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2814 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2815 | } |
| 2816 | } |
| 2817 | } |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2818 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2819 | /* now if nothing is locked, and if we have enough data, |
| 2820 | * we can start a write request |
| 2821 | */ |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2822 | /* since handle_stripe can be called at any time we need to handle the |
| 2823 | * case where a compute block operation has been submitted and then a |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2824 | * subsequent call wants to start a write request. raid_run_ops only |
| 2825 | * handles the case where compute block and reconstruct are requested |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2826 | * simultaneously. If this is not the case then new writes need to be |
| 2827 | * held off until the compute completes. |
| 2828 | */ |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2829 | if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) && |
| 2830 | (s->locked == 0 && (rcw == 0 || rmw == 0) && |
| 2831 | !test_bit(STRIPE_BIT_DELAY, &sh->state))) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2832 | schedule_reconstruction(sh, s, rcw == 0, 0); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2833 | } |
| 2834 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2835 | static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2836 | struct stripe_head_state *s, int disks) |
| 2837 | { |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2838 | struct r5dev *dev = NULL; |
Dan Williams | e89f896 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2839 | |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2840 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2841 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2842 | switch (sh->check_state) { |
| 2843 | case check_state_idle: |
| 2844 | /* start a new check operation if there are no failures */ |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2845 | if (s->failed == 0) { |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2846 | BUG_ON(s->uptodate != disks); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2847 | sh->check_state = check_state_run; |
| 2848 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2849 | clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2850 | s->uptodate--; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2851 | break; |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2852 | } |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2853 | dev = &sh->dev[s->failed_num[0]]; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2854 | /* fall through */ |
| 2855 | case check_state_compute_result: |
| 2856 | sh->check_state = check_state_idle; |
| 2857 | if (!dev) |
| 2858 | dev = &sh->dev[sh->pd_idx]; |
| 2859 | |
| 2860 | /* check that a write has not made the stripe insync */ |
| 2861 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 2862 | break; |
| 2863 | |
| 2864 | /* either failed parity check, or recovery is happening */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2865 | BUG_ON(!test_bit(R5_UPTODATE, &dev->flags)); |
| 2866 | BUG_ON(s->uptodate != disks); |
| 2867 | |
| 2868 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2869 | s->locked++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2870 | set_bit(R5_Wantwrite, &dev->flags); |
Dan Williams | 830ea01 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2871 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2872 | clear_bit(STRIPE_DEGRADED, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2873 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2874 | break; |
| 2875 | case check_state_run: |
| 2876 | break; /* we will be called again upon completion */ |
| 2877 | case check_state_check_result: |
| 2878 | sh->check_state = check_state_idle; |
| 2879 | |
| 2880 | /* if a failure occurred during the check operation, leave |
| 2881 | * STRIPE_INSYNC not set and let the stripe be handled again |
| 2882 | */ |
| 2883 | if (s->failed) |
| 2884 | break; |
| 2885 | |
| 2886 | /* handle a successful check operation, if parity is correct |
| 2887 | * we are done. Otherwise update the mismatch count and repair |
| 2888 | * parity if !MD_RECOVERY_CHECK |
| 2889 | */ |
Dan Williams | ad283ea | 2009-08-29 19:09:26 -0700 | [diff] [blame] | 2890 | if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0) |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2891 | /* parity is correct (on disc, |
| 2892 | * not in buffer any more) |
| 2893 | */ |
| 2894 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2895 | else { |
| 2896 | conf->mddev->resync_mismatches += STRIPE_SECTORS; |
| 2897 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 2898 | /* don't try to repair!! */ |
| 2899 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2900 | else { |
| 2901 | sh->check_state = check_state_compute_run; |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2902 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2903 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2904 | set_bit(R5_Wantcompute, |
| 2905 | &sh->dev[sh->pd_idx].flags); |
| 2906 | sh->ops.target = sh->pd_idx; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2907 | sh->ops.target2 = -1; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2908 | s->uptodate++; |
| 2909 | } |
| 2910 | } |
| 2911 | break; |
| 2912 | case check_state_compute_run: |
| 2913 | break; |
| 2914 | default: |
| 2915 | printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n", |
| 2916 | __func__, sh->check_state, |
| 2917 | (unsigned long long) sh->sector); |
| 2918 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2919 | } |
| 2920 | } |
| 2921 | |
| 2922 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2923 | static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2924 | struct stripe_head_state *s, |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2925 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2926 | { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2927 | int pd_idx = sh->pd_idx; |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 2928 | int qd_idx = sh->qd_idx; |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2929 | struct r5dev *dev; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2930 | |
| 2931 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2932 | |
| 2933 | BUG_ON(s->failed > 2); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2934 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2935 | /* Want to check and possibly repair P and Q. |
| 2936 | * However there could be one 'failed' device, in which |
| 2937 | * case we can only check one of them, possibly using the |
| 2938 | * other to generate missing data |
| 2939 | */ |
| 2940 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2941 | switch (sh->check_state) { |
| 2942 | case check_state_idle: |
| 2943 | /* start a new check operation if there are < 2 failures */ |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2944 | if (s->failed == s->q_failed) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2945 | /* The only possible failed device holds Q, so it |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2946 | * makes sense to check P (If anything else were failed, |
| 2947 | * we would have used P to recreate it). |
| 2948 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2949 | sh->check_state = check_state_run; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2950 | } |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2951 | if (!s->q_failed && s->failed < 2) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2952 | /* Q is not failed, and we didn't use it to generate |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2953 | * anything, so it makes sense to check it |
| 2954 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2955 | if (sh->check_state == check_state_run) |
| 2956 | sh->check_state = check_state_run_pq; |
| 2957 | else |
| 2958 | sh->check_state = check_state_run_q; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2959 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2960 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2961 | /* discard potentially stale zero_sum_result */ |
| 2962 | sh->ops.zero_sum_result = 0; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2963 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2964 | if (sh->check_state == check_state_run) { |
| 2965 | /* async_xor_zero_sum destroys the contents of P */ |
| 2966 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
| 2967 | s->uptodate--; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2968 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2969 | if (sh->check_state >= check_state_run && |
| 2970 | sh->check_state <= check_state_run_pq) { |
| 2971 | /* async_syndrome_zero_sum preserves P and Q, so |
| 2972 | * no need to mark them !uptodate here |
| 2973 | */ |
| 2974 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
| 2975 | break; |
| 2976 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2977 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2978 | /* we have 2-disk failure */ |
| 2979 | BUG_ON(s->failed != 2); |
| 2980 | /* fall through */ |
| 2981 | case check_state_compute_result: |
| 2982 | sh->check_state = check_state_idle; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2983 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2984 | /* check that a write has not made the stripe insync */ |
| 2985 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 2986 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2987 | |
| 2988 | /* now write out any block on a failed drive, |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2989 | * or P or Q if they were recomputed |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2990 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2991 | 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] | 2992 | if (s->failed == 2) { |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2993 | dev = &sh->dev[s->failed_num[1]]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2994 | s->locked++; |
| 2995 | set_bit(R5_LOCKED, &dev->flags); |
| 2996 | set_bit(R5_Wantwrite, &dev->flags); |
| 2997 | } |
| 2998 | if (s->failed >= 1) { |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2999 | dev = &sh->dev[s->failed_num[0]]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3000 | s->locked++; |
| 3001 | set_bit(R5_LOCKED, &dev->flags); |
| 3002 | set_bit(R5_Wantwrite, &dev->flags); |
| 3003 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3004 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3005 | dev = &sh->dev[pd_idx]; |
| 3006 | s->locked++; |
| 3007 | set_bit(R5_LOCKED, &dev->flags); |
| 3008 | set_bit(R5_Wantwrite, &dev->flags); |
| 3009 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3010 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3011 | dev = &sh->dev[qd_idx]; |
| 3012 | s->locked++; |
| 3013 | set_bit(R5_LOCKED, &dev->flags); |
| 3014 | set_bit(R5_Wantwrite, &dev->flags); |
| 3015 | } |
| 3016 | clear_bit(STRIPE_DEGRADED, &sh->state); |
| 3017 | |
| 3018 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3019 | break; |
| 3020 | case check_state_run: |
| 3021 | case check_state_run_q: |
| 3022 | case check_state_run_pq: |
| 3023 | break; /* we will be called again upon completion */ |
| 3024 | case check_state_check_result: |
| 3025 | sh->check_state = check_state_idle; |
| 3026 | |
| 3027 | /* handle a successful check operation, if parity is correct |
| 3028 | * we are done. Otherwise update the mismatch count and repair |
| 3029 | * parity if !MD_RECOVERY_CHECK |
| 3030 | */ |
| 3031 | if (sh->ops.zero_sum_result == 0) { |
| 3032 | /* both parities are correct */ |
| 3033 | if (!s->failed) |
| 3034 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3035 | else { |
| 3036 | /* in contrast to the raid5 case we can validate |
| 3037 | * parity, but still have a failure to write |
| 3038 | * back |
| 3039 | */ |
| 3040 | sh->check_state = check_state_compute_result; |
| 3041 | /* Returning at this point means that we may go |
| 3042 | * off and bring p and/or q uptodate again so |
| 3043 | * we make sure to check zero_sum_result again |
| 3044 | * to verify if p or q need writeback |
| 3045 | */ |
| 3046 | } |
| 3047 | } else { |
| 3048 | conf->mddev->resync_mismatches += STRIPE_SECTORS; |
| 3049 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 3050 | /* don't try to repair!! */ |
| 3051 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3052 | else { |
| 3053 | int *target = &sh->ops.target; |
| 3054 | |
| 3055 | sh->ops.target = -1; |
| 3056 | sh->ops.target2 = -1; |
| 3057 | sh->check_state = check_state_compute_run; |
| 3058 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 3059 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 3060 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
| 3061 | set_bit(R5_Wantcompute, |
| 3062 | &sh->dev[pd_idx].flags); |
| 3063 | *target = pd_idx; |
| 3064 | target = &sh->ops.target2; |
| 3065 | s->uptodate++; |
| 3066 | } |
| 3067 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
| 3068 | set_bit(R5_Wantcompute, |
| 3069 | &sh->dev[qd_idx].flags); |
| 3070 | *target = qd_idx; |
| 3071 | s->uptodate++; |
| 3072 | } |
| 3073 | } |
| 3074 | } |
| 3075 | break; |
| 3076 | case check_state_compute_run: |
| 3077 | break; |
| 3078 | default: |
| 3079 | printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n", |
| 3080 | __func__, sh->check_state, |
| 3081 | (unsigned long long) sh->sector); |
| 3082 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3083 | } |
| 3084 | } |
| 3085 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3086 | static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3087 | { |
| 3088 | int i; |
| 3089 | |
| 3090 | /* We have read all the blocks in this stripe and now we need to |
| 3091 | * copy some of them into a target stripe for expand. |
| 3092 | */ |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3093 | struct dma_async_tx_descriptor *tx = NULL; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3094 | clear_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 3095 | for (i = 0; i < sh->disks; i++) |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 3096 | if (i != sh->pd_idx && i != sh->qd_idx) { |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3097 | int dd_idx, j; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3098 | struct stripe_head *sh2; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 3099 | struct async_submit_ctl submit; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3100 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 3101 | sector_t bn = compute_blocknr(sh, i, 1); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3102 | sector_t s = raid5_compute_sector(conf, bn, 0, |
| 3103 | &dd_idx, NULL); |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 3104 | sh2 = get_active_stripe(conf, s, 0, 1, 1); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3105 | if (sh2 == NULL) |
| 3106 | /* so far only the early blocks of this stripe |
| 3107 | * have been requested. When later blocks |
| 3108 | * get requested, we will try again |
| 3109 | */ |
| 3110 | continue; |
| 3111 | if (!test_bit(STRIPE_EXPANDING, &sh2->state) || |
| 3112 | test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) { |
| 3113 | /* must have already done this block */ |
| 3114 | release_stripe(sh2); |
| 3115 | continue; |
| 3116 | } |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3117 | |
| 3118 | /* place all the copies on one channel */ |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 3119 | init_async_submit(&submit, 0, tx, NULL, NULL, NULL); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3120 | tx = async_memcpy(sh2->dev[dd_idx].page, |
Dan Williams | 88ba2aa | 2009-04-09 16:16:18 -0700 | [diff] [blame] | 3121 | sh->dev[i].page, 0, 0, STRIPE_SIZE, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 3122 | &submit); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3123 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3124 | set_bit(R5_Expanded, &sh2->dev[dd_idx].flags); |
| 3125 | set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags); |
| 3126 | for (j = 0; j < conf->raid_disks; j++) |
| 3127 | if (j != sh2->pd_idx && |
NeilBrown | 86c374b | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3128 | j != sh2->qd_idx && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3129 | !test_bit(R5_Expanded, &sh2->dev[j].flags)) |
| 3130 | break; |
| 3131 | if (j == conf->raid_disks) { |
| 3132 | set_bit(STRIPE_EXPAND_READY, &sh2->state); |
| 3133 | set_bit(STRIPE_HANDLE, &sh2->state); |
| 3134 | } |
| 3135 | release_stripe(sh2); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3136 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3137 | } |
NeilBrown | a2e0855 | 2007-09-11 15:23:36 -0700 | [diff] [blame] | 3138 | /* done submitting copies, wait for them to complete */ |
| 3139 | if (tx) { |
| 3140 | async_tx_ack(tx); |
| 3141 | dma_wait_for_async_tx(tx); |
| 3142 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3143 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3144 | |
| 3145 | /* |
| 3146 | * handle_stripe - do things to a stripe. |
| 3147 | * |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3148 | * We lock the stripe by setting STRIPE_ACTIVE and then examine the |
| 3149 | * state of various bits to see what needs to be done. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3150 | * Possible results: |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3151 | * return some read requests which now have data |
| 3152 | * return some write requests which are safely on storage |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3153 | * schedule a read on some buffers |
| 3154 | * schedule a write of some buffers |
| 3155 | * return confirmation of parity correctness |
| 3156 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3157 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3158 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3159 | static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3160 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3161 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3162 | int disks = sh->disks; |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3163 | struct r5dev *dev; |
| 3164 | int i; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3165 | int do_recovery = 0; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3166 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3167 | memset(s, 0, sizeof(*s)); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3168 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3169 | s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 3170 | s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state); |
| 3171 | s->failed_num[0] = -1; |
| 3172 | s->failed_num[1] = -1; |
| 3173 | |
| 3174 | /* Now to look around and see what can be done */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3175 | rcu_read_lock(); |
NeilBrown | c4c1663 | 2011-07-26 11:34:20 +1000 | [diff] [blame] | 3176 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3177 | for (i=disks; i--; ) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3178 | struct md_rdev *rdev; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3179 | sector_t first_bad; |
| 3180 | int bad_sectors; |
| 3181 | int is_bad = 0; |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3182 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3183 | dev = &sh->dev[i]; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3184 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3185 | 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] | 3186 | i, dev->flags, |
| 3187 | dev->toread, dev->towrite, dev->written); |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3188 | /* maybe we can reply to a read |
| 3189 | * |
| 3190 | * new wantfill requests are only permitted while |
| 3191 | * ops_complete_biofill is guaranteed to be inactive |
| 3192 | */ |
| 3193 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread && |
| 3194 | !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) |
| 3195 | set_bit(R5_Wantfill, &dev->flags); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3196 | |
| 3197 | /* now count some things */ |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3198 | if (test_bit(R5_LOCKED, &dev->flags)) |
| 3199 | s->locked++; |
| 3200 | if (test_bit(R5_UPTODATE, &dev->flags)) |
| 3201 | s->uptodate++; |
Dan Williams | 2d6e4ec | 2009-09-16 12:11:54 -0700 | [diff] [blame] | 3202 | if (test_bit(R5_Wantcompute, &dev->flags)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3203 | s->compute++; |
| 3204 | BUG_ON(s->compute > 2); |
Dan Williams | 2d6e4ec | 2009-09-16 12:11:54 -0700 | [diff] [blame] | 3205 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3206 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3207 | if (test_bit(R5_Wantfill, &dev->flags)) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3208 | s->to_fill++; |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3209 | else if (dev->toread) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3210 | s->to_read++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3211 | if (dev->towrite) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3212 | s->to_write++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3213 | if (!test_bit(R5_OVERWRITE, &dev->flags)) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3214 | s->non_overwrite++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3215 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3216 | if (dev->written) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3217 | s->written++; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3218 | /* Prefer to use the replacement for reads, but only |
| 3219 | * if it is recovered enough and has no bad blocks. |
| 3220 | */ |
| 3221 | rdev = rcu_dereference(conf->disks[i].replacement); |
| 3222 | if (rdev && !test_bit(Faulty, &rdev->flags) && |
| 3223 | rdev->recovery_offset >= sh->sector + STRIPE_SECTORS && |
| 3224 | !is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 3225 | &first_bad, &bad_sectors)) |
| 3226 | set_bit(R5_ReadRepl, &dev->flags); |
| 3227 | else { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3228 | if (rdev) |
| 3229 | set_bit(R5_NeedReplace, &dev->flags); |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3230 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 3231 | clear_bit(R5_ReadRepl, &dev->flags); |
| 3232 | } |
NeilBrown | 9283d8c | 2011-12-08 16:27:57 +1100 | [diff] [blame] | 3233 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 3234 | rdev = NULL; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3235 | if (rdev) { |
| 3236 | is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 3237 | &first_bad, &bad_sectors); |
| 3238 | if (s->blocked_rdev == NULL |
| 3239 | && (test_bit(Blocked, &rdev->flags) |
| 3240 | || is_bad < 0)) { |
| 3241 | if (is_bad < 0) |
| 3242 | set_bit(BlockedBadBlocks, |
| 3243 | &rdev->flags); |
| 3244 | s->blocked_rdev = rdev; |
| 3245 | atomic_inc(&rdev->nr_pending); |
| 3246 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3247 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3248 | clear_bit(R5_Insync, &dev->flags); |
| 3249 | if (!rdev) |
| 3250 | /* Not in-sync */; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3251 | else if (is_bad) { |
| 3252 | /* also not in-sync */ |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3253 | if (!test_bit(WriteErrorSeen, &rdev->flags) && |
| 3254 | test_bit(R5_UPTODATE, &dev->flags)) { |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3255 | /* treat as in-sync, but with a read error |
| 3256 | * which we can now try to correct |
| 3257 | */ |
| 3258 | set_bit(R5_Insync, &dev->flags); |
| 3259 | set_bit(R5_ReadError, &dev->flags); |
| 3260 | } |
| 3261 | } else if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3262 | set_bit(R5_Insync, &dev->flags); |
NeilBrown | 30d7a48 | 2011-12-23 09:57:00 +1100 | [diff] [blame] | 3263 | else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset) |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3264 | /* in sync if before recovery_offset */ |
NeilBrown | 30d7a48 | 2011-12-23 09:57:00 +1100 | [diff] [blame] | 3265 | set_bit(R5_Insync, &dev->flags); |
| 3266 | else if (test_bit(R5_UPTODATE, &dev->flags) && |
| 3267 | test_bit(R5_Expanded, &dev->flags)) |
| 3268 | /* If we've reshaped into here, we assume it is Insync. |
| 3269 | * We will shortly update recovery_offset to make |
| 3270 | * it official. |
| 3271 | */ |
| 3272 | set_bit(R5_Insync, &dev->flags); |
| 3273 | |
Adam Kwolek | 5d8c71f | 2011-12-09 14:26:11 +1100 | [diff] [blame] | 3274 | if (rdev && test_bit(R5_WriteError, &dev->flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3275 | /* This flag does not apply to '.replacement' |
| 3276 | * only to .rdev, so make sure to check that*/ |
| 3277 | struct md_rdev *rdev2 = rcu_dereference( |
| 3278 | conf->disks[i].rdev); |
| 3279 | if (rdev2 == rdev) |
| 3280 | clear_bit(R5_Insync, &dev->flags); |
| 3281 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3282 | s->handle_bad_blocks = 1; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3283 | atomic_inc(&rdev2->nr_pending); |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3284 | } else |
| 3285 | clear_bit(R5_WriteError, &dev->flags); |
| 3286 | } |
Adam Kwolek | 5d8c71f | 2011-12-09 14:26:11 +1100 | [diff] [blame] | 3287 | if (rdev && test_bit(R5_MadeGood, &dev->flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3288 | /* This flag does not apply to '.replacement' |
| 3289 | * only to .rdev, so make sure to check that*/ |
| 3290 | struct md_rdev *rdev2 = rcu_dereference( |
| 3291 | conf->disks[i].rdev); |
| 3292 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 3293 | s->handle_bad_blocks = 1; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3294 | atomic_inc(&rdev2->nr_pending); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 3295 | } else |
| 3296 | clear_bit(R5_MadeGood, &dev->flags); |
| 3297 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3298 | if (test_bit(R5_MadeGoodRepl, &dev->flags)) { |
| 3299 | struct md_rdev *rdev2 = rcu_dereference( |
| 3300 | conf->disks[i].replacement); |
| 3301 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
| 3302 | s->handle_bad_blocks = 1; |
| 3303 | atomic_inc(&rdev2->nr_pending); |
| 3304 | } else |
| 3305 | clear_bit(R5_MadeGoodRepl, &dev->flags); |
| 3306 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3307 | if (!test_bit(R5_Insync, &dev->flags)) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3308 | /* The ReadError flag will just be confusing now */ |
| 3309 | clear_bit(R5_ReadError, &dev->flags); |
| 3310 | clear_bit(R5_ReWrite, &dev->flags); |
| 3311 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3312 | if (test_bit(R5_ReadError, &dev->flags)) |
| 3313 | clear_bit(R5_Insync, &dev->flags); |
| 3314 | if (!test_bit(R5_Insync, &dev->flags)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3315 | if (s->failed < 2) |
| 3316 | s->failed_num[s->failed] = i; |
| 3317 | s->failed++; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3318 | if (rdev && !test_bit(Faulty, &rdev->flags)) |
| 3319 | do_recovery = 1; |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3320 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3321 | } |
NeilBrown | c4c1663 | 2011-07-26 11:34:20 +1000 | [diff] [blame] | 3322 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3323 | if (test_bit(STRIPE_SYNCING, &sh->state)) { |
| 3324 | /* If there is a failed device being replaced, |
| 3325 | * we must be recovering. |
| 3326 | * else if we are after recovery_cp, we must be syncing |
majianpeng | c6d2e08 | 2012-04-02 01:16:59 +1000 | [diff] [blame] | 3327 | * else if MD_RECOVERY_REQUESTED is set, we also are syncing. |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3328 | * else we can only be replacing |
| 3329 | * sync and recovery both need to read all devices, and so |
| 3330 | * use the same flag. |
| 3331 | */ |
| 3332 | if (do_recovery || |
majianpeng | c6d2e08 | 2012-04-02 01:16:59 +1000 | [diff] [blame] | 3333 | sh->sector >= conf->mddev->recovery_cp || |
| 3334 | test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery))) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3335 | s->syncing = 1; |
| 3336 | else |
| 3337 | s->replacing = 1; |
| 3338 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3339 | rcu_read_unlock(); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3340 | } |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3341 | |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3342 | static void handle_stripe(struct stripe_head *sh) |
| 3343 | { |
| 3344 | struct stripe_head_state s; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3345 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3346 | int i; |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3347 | int prexor; |
| 3348 | int disks = sh->disks; |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3349 | struct r5dev *pdev, *qdev; |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3350 | |
| 3351 | clear_bit(STRIPE_HANDLE, &sh->state); |
Dan Williams | 257a4b4 | 2011-11-08 16:22:06 +1100 | [diff] [blame] | 3352 | if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3353 | /* already being handled, ensure it gets handled |
| 3354 | * again when current action finishes */ |
| 3355 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3356 | return; |
| 3357 | } |
| 3358 | |
| 3359 | if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) { |
| 3360 | set_bit(STRIPE_SYNCING, &sh->state); |
| 3361 | clear_bit(STRIPE_INSYNC, &sh->state); |
| 3362 | } |
| 3363 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 3364 | |
| 3365 | pr_debug("handling stripe %llu, state=%#lx cnt=%d, " |
| 3366 | "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n", |
| 3367 | (unsigned long long)sh->sector, sh->state, |
| 3368 | atomic_read(&sh->count), sh->pd_idx, sh->qd_idx, |
| 3369 | sh->check_state, sh->reconstruct_state); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3370 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3371 | analyse_stripe(sh, &s); |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3372 | |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3373 | if (s.handle_bad_blocks) { |
| 3374 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3375 | goto finish; |
| 3376 | } |
| 3377 | |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3378 | if (unlikely(s.blocked_rdev)) { |
| 3379 | if (s.syncing || s.expanding || s.expanded || |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3380 | s.replacing || s.to_write || s.written) { |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3381 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3382 | goto finish; |
| 3383 | } |
| 3384 | /* There is nothing for the blocked_rdev to block */ |
| 3385 | rdev_dec_pending(s.blocked_rdev, conf->mddev); |
| 3386 | s.blocked_rdev = NULL; |
| 3387 | } |
| 3388 | |
| 3389 | if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) { |
| 3390 | set_bit(STRIPE_OP_BIOFILL, &s.ops_request); |
| 3391 | set_bit(STRIPE_BIOFILL_RUN, &sh->state); |
| 3392 | } |
| 3393 | |
| 3394 | pr_debug("locked=%d uptodate=%d to_read=%d" |
| 3395 | " to_write=%d failed=%d failed_num=%d,%d\n", |
| 3396 | s.locked, s.uptodate, s.to_read, s.to_write, s.failed, |
| 3397 | s.failed_num[0], s.failed_num[1]); |
| 3398 | /* check if the array has lost more than max_degraded devices and, |
| 3399 | * if so, some requests might need to be failed. |
| 3400 | */ |
NeilBrown | 9a3f530 | 2011-11-08 16:22:01 +1100 | [diff] [blame] | 3401 | if (s.failed > conf->max_degraded) { |
| 3402 | sh->check_state = 0; |
| 3403 | sh->reconstruct_state = 0; |
| 3404 | if (s.to_read+s.to_write+s.written) |
| 3405 | handle_failed_stripe(conf, sh, &s, disks, &s.return_bi); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3406 | if (s.syncing + s.replacing) |
NeilBrown | 9a3f530 | 2011-11-08 16:22:01 +1100 | [diff] [blame] | 3407 | handle_failed_sync(conf, sh, &s); |
| 3408 | } |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3409 | |
| 3410 | /* |
| 3411 | * might be able to return some write requests if the parity blocks |
| 3412 | * are safe, or on a failed drive |
| 3413 | */ |
| 3414 | pdev = &sh->dev[sh->pd_idx]; |
| 3415 | s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx) |
| 3416 | || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx); |
| 3417 | qdev = &sh->dev[sh->qd_idx]; |
| 3418 | s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx) |
| 3419 | || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx) |
| 3420 | || conf->level < 6; |
| 3421 | |
| 3422 | if (s.written && |
| 3423 | (s.p_failed || ((test_bit(R5_Insync, &pdev->flags) |
| 3424 | && !test_bit(R5_LOCKED, &pdev->flags) |
| 3425 | && test_bit(R5_UPTODATE, &pdev->flags)))) && |
| 3426 | (s.q_failed || ((test_bit(R5_Insync, &qdev->flags) |
| 3427 | && !test_bit(R5_LOCKED, &qdev->flags) |
| 3428 | && test_bit(R5_UPTODATE, &qdev->flags))))) |
| 3429 | handle_stripe_clean_event(conf, sh, disks, &s.return_bi); |
| 3430 | |
| 3431 | /* Now we might consider reading some blocks, either to check/generate |
| 3432 | * parity, or to satisfy requests |
| 3433 | * or to load a block that is being partially written. |
| 3434 | */ |
| 3435 | if (s.to_read || s.non_overwrite |
| 3436 | || (conf->level == 6 && s.to_write && s.failed) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3437 | || (s.syncing && (s.uptodate + s.compute < disks)) |
| 3438 | || s.replacing |
| 3439 | || s.expanding) |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3440 | handle_stripe_fill(sh, &s, disks); |
| 3441 | |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3442 | /* Now we check to see if any write operations have recently |
| 3443 | * completed |
| 3444 | */ |
| 3445 | prexor = 0; |
| 3446 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_result) |
| 3447 | prexor = 1; |
| 3448 | if (sh->reconstruct_state == reconstruct_state_drain_result || |
| 3449 | sh->reconstruct_state == reconstruct_state_prexor_drain_result) { |
| 3450 | sh->reconstruct_state = reconstruct_state_idle; |
| 3451 | |
| 3452 | /* All the 'written' buffers and the parity block are ready to |
| 3453 | * be written back to disk |
| 3454 | */ |
| 3455 | BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags)); |
| 3456 | BUG_ON(sh->qd_idx >= 0 && |
| 3457 | !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags)); |
| 3458 | for (i = disks; i--; ) { |
| 3459 | struct r5dev *dev = &sh->dev[i]; |
| 3460 | if (test_bit(R5_LOCKED, &dev->flags) && |
| 3461 | (i == sh->pd_idx || i == sh->qd_idx || |
| 3462 | dev->written)) { |
| 3463 | pr_debug("Writing block %d\n", i); |
| 3464 | set_bit(R5_Wantwrite, &dev->flags); |
| 3465 | if (prexor) |
| 3466 | continue; |
| 3467 | if (!test_bit(R5_Insync, &dev->flags) || |
| 3468 | ((i == sh->pd_idx || i == sh->qd_idx) && |
| 3469 | s.failed == 0)) |
| 3470 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3471 | } |
| 3472 | } |
| 3473 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 3474 | s.dec_preread_active = 1; |
| 3475 | } |
| 3476 | |
| 3477 | /* Now to consider new write requests and what else, if anything |
| 3478 | * should be read. We do not handle new writes when: |
| 3479 | * 1/ A 'write' operation (copy+xor) is already in flight. |
| 3480 | * 2/ A 'check' operation is in flight, as it may clobber the parity |
| 3481 | * block. |
| 3482 | */ |
| 3483 | if (s.to_write && !sh->reconstruct_state && !sh->check_state) |
| 3484 | handle_stripe_dirtying(conf, sh, &s, disks); |
| 3485 | |
| 3486 | /* maybe we need to check and possibly fix the parity for this stripe |
| 3487 | * Any reads will already have been scheduled, so we just see if enough |
| 3488 | * data is available. The parity check is held off while parity |
| 3489 | * dependent operations are in flight. |
| 3490 | */ |
| 3491 | if (sh->check_state || |
| 3492 | (s.syncing && s.locked == 0 && |
| 3493 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && |
| 3494 | !test_bit(STRIPE_INSYNC, &sh->state))) { |
| 3495 | if (conf->level == 6) |
| 3496 | handle_parity_checks6(conf, sh, &s, disks); |
| 3497 | else |
| 3498 | handle_parity_checks5(conf, sh, &s, disks); |
| 3499 | } |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3500 | |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3501 | if (s.replacing && s.locked == 0 |
| 3502 | && !test_bit(STRIPE_INSYNC, &sh->state)) { |
| 3503 | /* Write out to replacement devices where possible */ |
| 3504 | for (i = 0; i < conf->raid_disks; i++) |
| 3505 | if (test_bit(R5_UPTODATE, &sh->dev[i].flags) && |
| 3506 | test_bit(R5_NeedReplace, &sh->dev[i].flags)) { |
| 3507 | set_bit(R5_WantReplace, &sh->dev[i].flags); |
| 3508 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 3509 | s.locked++; |
| 3510 | } |
| 3511 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3512 | } |
| 3513 | if ((s.syncing || s.replacing) && s.locked == 0 && |
| 3514 | test_bit(STRIPE_INSYNC, &sh->state)) { |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3515 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 3516 | clear_bit(STRIPE_SYNCING, &sh->state); |
| 3517 | } |
| 3518 | |
| 3519 | /* If the failed drives are just a ReadError, then we might need |
| 3520 | * to progress the repair/check process |
| 3521 | */ |
| 3522 | if (s.failed <= conf->max_degraded && !conf->mddev->ro) |
| 3523 | for (i = 0; i < s.failed; i++) { |
| 3524 | struct r5dev *dev = &sh->dev[s.failed_num[i]]; |
| 3525 | if (test_bit(R5_ReadError, &dev->flags) |
| 3526 | && !test_bit(R5_LOCKED, &dev->flags) |
| 3527 | && test_bit(R5_UPTODATE, &dev->flags) |
| 3528 | ) { |
| 3529 | if (!test_bit(R5_ReWrite, &dev->flags)) { |
| 3530 | set_bit(R5_Wantwrite, &dev->flags); |
| 3531 | set_bit(R5_ReWrite, &dev->flags); |
| 3532 | set_bit(R5_LOCKED, &dev->flags); |
| 3533 | s.locked++; |
| 3534 | } else { |
| 3535 | /* let's read it back */ |
| 3536 | set_bit(R5_Wantread, &dev->flags); |
| 3537 | set_bit(R5_LOCKED, &dev->flags); |
| 3538 | s.locked++; |
| 3539 | } |
| 3540 | } |
| 3541 | } |
| 3542 | |
| 3543 | |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3544 | /* Finish reconstruct operations initiated by the expansion process */ |
| 3545 | if (sh->reconstruct_state == reconstruct_state_result) { |
| 3546 | struct stripe_head *sh_src |
| 3547 | = get_active_stripe(conf, sh->sector, 1, 1, 1); |
| 3548 | if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) { |
| 3549 | /* sh cannot be written until sh_src has been read. |
| 3550 | * so arrange for sh to be delayed a little |
| 3551 | */ |
| 3552 | set_bit(STRIPE_DELAYED, &sh->state); |
| 3553 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3554 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, |
| 3555 | &sh_src->state)) |
| 3556 | atomic_inc(&conf->preread_active_stripes); |
| 3557 | release_stripe(sh_src); |
| 3558 | goto finish; |
| 3559 | } |
| 3560 | if (sh_src) |
| 3561 | release_stripe(sh_src); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3562 | |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3563 | sh->reconstruct_state = reconstruct_state_idle; |
| 3564 | clear_bit(STRIPE_EXPANDING, &sh->state); |
| 3565 | for (i = conf->raid_disks; i--; ) { |
| 3566 | set_bit(R5_Wantwrite, &sh->dev[i].flags); |
| 3567 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 3568 | s.locked++; |
| 3569 | } |
| 3570 | } |
| 3571 | |
| 3572 | if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) && |
| 3573 | !sh->reconstruct_state) { |
| 3574 | /* Need to write out all blocks after computing parity */ |
| 3575 | sh->disks = conf->raid_disks; |
| 3576 | stripe_set_idx(sh->sector, conf, 0, sh); |
| 3577 | schedule_reconstruction(sh, &s, 1, 1); |
| 3578 | } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) { |
| 3579 | clear_bit(STRIPE_EXPAND_READY, &sh->state); |
| 3580 | atomic_dec(&conf->reshape_stripes); |
| 3581 | wake_up(&conf->wait_for_overlap); |
| 3582 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 3583 | } |
| 3584 | |
| 3585 | if (s.expanding && s.locked == 0 && |
| 3586 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) |
| 3587 | handle_stripe_expansion(conf, sh); |
| 3588 | |
| 3589 | finish: |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3590 | /* wait for this device to become unblocked */ |
NeilBrown | 43220aa | 2011-08-31 12:49:14 +1000 | [diff] [blame] | 3591 | if (conf->mddev->external && unlikely(s.blocked_rdev)) |
NeilBrown | c5709ef | 2011-07-26 11:35:20 +1000 | [diff] [blame] | 3592 | md_wait_for_blocked_rdev(s.blocked_rdev, conf->mddev); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3593 | |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3594 | if (s.handle_bad_blocks) |
| 3595 | for (i = disks; i--; ) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3596 | struct md_rdev *rdev; |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3597 | struct r5dev *dev = &sh->dev[i]; |
| 3598 | if (test_and_clear_bit(R5_WriteError, &dev->flags)) { |
| 3599 | /* We own a safe reference to the rdev */ |
| 3600 | rdev = conf->disks[i].rdev; |
| 3601 | if (!rdev_set_badblocks(rdev, sh->sector, |
| 3602 | STRIPE_SECTORS, 0)) |
| 3603 | md_error(conf->mddev, rdev); |
| 3604 | rdev_dec_pending(rdev, conf->mddev); |
| 3605 | } |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 3606 | if (test_and_clear_bit(R5_MadeGood, &dev->flags)) { |
| 3607 | rdev = conf->disks[i].rdev; |
| 3608 | rdev_clear_badblocks(rdev, sh->sector, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3609 | STRIPE_SECTORS, 0); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 3610 | rdev_dec_pending(rdev, conf->mddev); |
| 3611 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3612 | if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) { |
| 3613 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3614 | if (!rdev) |
| 3615 | /* rdev have been moved down */ |
| 3616 | rdev = conf->disks[i].rdev; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3617 | rdev_clear_badblocks(rdev, sh->sector, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3618 | STRIPE_SECTORS, 0); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3619 | rdev_dec_pending(rdev, conf->mddev); |
| 3620 | } |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3621 | } |
| 3622 | |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3623 | if (s.ops_request) |
| 3624 | raid_run_ops(sh, s.ops_request); |
| 3625 | |
Dan Williams | f0e43bc | 2008-06-28 08:31:55 +1000 | [diff] [blame] | 3626 | ops_run_io(sh, &s); |
| 3627 | |
NeilBrown | c5709ef | 2011-07-26 11:35:20 +1000 | [diff] [blame] | 3628 | if (s.dec_preread_active) { |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 3629 | /* 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] | 3630 | * is waiting on a flush, it won't continue until the writes |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 3631 | * have actually been submitted. |
| 3632 | */ |
| 3633 | atomic_dec(&conf->preread_active_stripes); |
| 3634 | if (atomic_read(&conf->preread_active_stripes) < |
| 3635 | IO_THRESHOLD) |
| 3636 | md_wakeup_thread(conf->mddev->thread); |
| 3637 | } |
| 3638 | |
NeilBrown | c5709ef | 2011-07-26 11:35:20 +1000 | [diff] [blame] | 3639 | return_io(s.return_bi); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3640 | |
Dan Williams | 257a4b4 | 2011-11-08 16:22:06 +1100 | [diff] [blame] | 3641 | clear_bit_unlock(STRIPE_ACTIVE, &sh->state); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3642 | } |
| 3643 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3644 | static void raid5_activate_delayed(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3645 | { |
| 3646 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) { |
| 3647 | while (!list_empty(&conf->delayed_list)) { |
| 3648 | struct list_head *l = conf->delayed_list.next; |
| 3649 | struct stripe_head *sh; |
| 3650 | sh = list_entry(l, struct stripe_head, lru); |
| 3651 | list_del_init(l); |
| 3652 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 3653 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 3654 | atomic_inc(&conf->preread_active_stripes); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3655 | list_add_tail(&sh->lru, &conf->hold_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3656 | } |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 3657 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3658 | } |
| 3659 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3660 | static void activate_bit_delay(struct r5conf *conf) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3661 | { |
| 3662 | /* device_lock is held */ |
| 3663 | struct list_head head; |
| 3664 | list_add(&head, &conf->bitmap_list); |
| 3665 | list_del_init(&conf->bitmap_list); |
| 3666 | while (!list_empty(&head)) { |
| 3667 | struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru); |
| 3668 | list_del_init(&sh->lru); |
| 3669 | atomic_inc(&sh->count); |
| 3670 | __release_stripe(conf, sh); |
| 3671 | } |
| 3672 | } |
| 3673 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3674 | int md_raid5_congested(struct mddev *mddev, int bits) |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3675 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3676 | struct r5conf *conf = mddev->private; |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3677 | |
| 3678 | /* No difference between reads and writes. Just check |
| 3679 | * how busy the stripe_cache is |
| 3680 | */ |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 3681 | |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3682 | if (conf->inactive_blocked) |
| 3683 | return 1; |
| 3684 | if (conf->quiesce) |
| 3685 | return 1; |
| 3686 | if (list_empty_careful(&conf->inactive_list)) |
| 3687 | return 1; |
| 3688 | |
| 3689 | return 0; |
| 3690 | } |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 3691 | EXPORT_SYMBOL_GPL(md_raid5_congested); |
| 3692 | |
| 3693 | static int raid5_congested(void *data, int bits) |
| 3694 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3695 | struct mddev *mddev = data; |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 3696 | |
| 3697 | return mddev_congested(mddev, bits) || |
| 3698 | md_raid5_congested(mddev, bits); |
| 3699 | } |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3700 | |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3701 | /* We want read requests to align with chunks where possible, |
| 3702 | * but write requests don't need to. |
| 3703 | */ |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3704 | static int raid5_mergeable_bvec(struct request_queue *q, |
| 3705 | struct bvec_merge_data *bvm, |
| 3706 | struct bio_vec *biovec) |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3707 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3708 | struct mddev *mddev = q->queuedata; |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3709 | 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] | 3710 | int max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3711 | unsigned int chunk_sectors = mddev->chunk_sectors; |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3712 | unsigned int bio_sectors = bvm->bi_size >> 9; |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3713 | |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3714 | if ((bvm->bi_rw & 1) == WRITE) |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3715 | return biovec->bv_len; /* always allow writes to be mergeable */ |
| 3716 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3717 | if (mddev->new_chunk_sectors < mddev->chunk_sectors) |
| 3718 | chunk_sectors = mddev->new_chunk_sectors; |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3719 | max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9; |
| 3720 | if (max < 0) max = 0; |
| 3721 | if (max <= biovec->bv_len && bio_sectors == 0) |
| 3722 | return biovec->bv_len; |
| 3723 | else |
| 3724 | return max; |
| 3725 | } |
| 3726 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3727 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3728 | 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] | 3729 | { |
| 3730 | sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3731 | unsigned int chunk_sectors = mddev->chunk_sectors; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3732 | unsigned int bio_sectors = bio->bi_size >> 9; |
| 3733 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3734 | if (mddev->new_chunk_sectors < mddev->chunk_sectors) |
| 3735 | chunk_sectors = mddev->new_chunk_sectors; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3736 | return chunk_sectors >= |
| 3737 | ((sector & (chunk_sectors - 1)) + bio_sectors); |
| 3738 | } |
| 3739 | |
| 3740 | /* |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3741 | * add bio to the retry LIFO ( in O(1) ... we are in interrupt ) |
| 3742 | * later sampled by raid5d. |
| 3743 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3744 | 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] | 3745 | { |
| 3746 | unsigned long flags; |
| 3747 | |
| 3748 | spin_lock_irqsave(&conf->device_lock, flags); |
| 3749 | |
| 3750 | bi->bi_next = conf->retry_read_aligned_list; |
| 3751 | conf->retry_read_aligned_list = bi; |
| 3752 | |
| 3753 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 3754 | md_wakeup_thread(conf->mddev->thread); |
| 3755 | } |
| 3756 | |
| 3757 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3758 | static struct bio *remove_bio_from_retry(struct r5conf *conf) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3759 | { |
| 3760 | struct bio *bi; |
| 3761 | |
| 3762 | bi = conf->retry_read_aligned; |
| 3763 | if (bi) { |
| 3764 | conf->retry_read_aligned = NULL; |
| 3765 | return bi; |
| 3766 | } |
| 3767 | bi = conf->retry_read_aligned_list; |
| 3768 | if(bi) { |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3769 | conf->retry_read_aligned_list = bi->bi_next; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3770 | bi->bi_next = NULL; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 3771 | /* |
| 3772 | * this sets the active strip count to 1 and the processed |
| 3773 | * strip count to zero (upper 8 bits) |
| 3774 | */ |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3775 | bi->bi_phys_segments = 1; /* biased count of active stripes */ |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3776 | } |
| 3777 | |
| 3778 | return bi; |
| 3779 | } |
| 3780 | |
| 3781 | |
| 3782 | /* |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3783 | * The "raid5_align_endio" should check if the read succeeded and if it |
| 3784 | * did, call bio_endio on the original bio (having bio_put the new bio |
| 3785 | * first). |
| 3786 | * If the read failed.. |
| 3787 | */ |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3788 | static void raid5_align_endio(struct bio *bi, int error) |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3789 | { |
| 3790 | struct bio* raid_bi = bi->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3791 | struct mddev *mddev; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3792 | struct r5conf *conf; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3793 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3794 | struct md_rdev *rdev; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3795 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3796 | bio_put(bi); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3797 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3798 | rdev = (void*)raid_bi->bi_next; |
| 3799 | raid_bi->bi_next = NULL; |
NeilBrown | 2b7f222 | 2010-03-25 16:06:03 +1100 | [diff] [blame] | 3800 | mddev = rdev->mddev; |
| 3801 | conf = mddev->private; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3802 | |
| 3803 | rdev_dec_pending(rdev, conf->mddev); |
| 3804 | |
| 3805 | if (!error && uptodate) { |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3806 | bio_endio(raid_bi, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3807 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
| 3808 | wake_up(&conf->wait_for_stripe); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3809 | return; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3810 | } |
| 3811 | |
| 3812 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3813 | 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] | 3814 | |
| 3815 | add_bio_to_retry(raid_bi, conf); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3816 | } |
| 3817 | |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3818 | static int bio_fits_rdev(struct bio *bi) |
| 3819 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3820 | struct request_queue *q = bdev_get_queue(bi->bi_bdev); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3821 | |
Martin K. Petersen | ae03bf6 | 2009-05-22 17:17:50 -0400 | [diff] [blame] | 3822 | if ((bi->bi_size>>9) > queue_max_sectors(q)) |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3823 | return 0; |
| 3824 | blk_recount_segments(q, bi); |
Martin K. Petersen | 8a78362 | 2010-02-26 00:20:39 -0500 | [diff] [blame] | 3825 | if (bi->bi_phys_segments > queue_max_segments(q)) |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3826 | return 0; |
| 3827 | |
| 3828 | if (q->merge_bvec_fn) |
| 3829 | /* it's too hard to apply the merge_bvec_fn at this stage, |
| 3830 | * just just give up |
| 3831 | */ |
| 3832 | return 0; |
| 3833 | |
| 3834 | return 1; |
| 3835 | } |
| 3836 | |
| 3837 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3838 | 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] | 3839 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3840 | struct r5conf *conf = mddev->private; |
NeilBrown | 8553fe7ec | 2009-12-14 12:49:47 +1100 | [diff] [blame] | 3841 | int dd_idx; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3842 | struct bio* align_bi; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3843 | struct md_rdev *rdev; |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3844 | sector_t end_sector; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3845 | |
| 3846 | if (!in_chunk_boundary(mddev, raid_bio)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3847 | pr_debug("chunk_aligned_read : non aligned\n"); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3848 | return 0; |
| 3849 | } |
| 3850 | /* |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 3851 | * 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] | 3852 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 3853 | align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3854 | if (!align_bi) |
| 3855 | return 0; |
| 3856 | /* |
| 3857 | * set bi_end_io to a new function, and set bi_private to the |
| 3858 | * original bio. |
| 3859 | */ |
| 3860 | align_bi->bi_end_io = raid5_align_endio; |
| 3861 | align_bi->bi_private = raid_bio; |
| 3862 | /* |
| 3863 | * compute position |
| 3864 | */ |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3865 | align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector, |
| 3866 | 0, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3867 | &dd_idx, NULL); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3868 | |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3869 | 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] | 3870 | rcu_read_lock(); |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3871 | rdev = rcu_dereference(conf->disks[dd_idx].replacement); |
| 3872 | if (!rdev || test_bit(Faulty, &rdev->flags) || |
| 3873 | rdev->recovery_offset < end_sector) { |
| 3874 | rdev = rcu_dereference(conf->disks[dd_idx].rdev); |
| 3875 | if (rdev && |
| 3876 | (test_bit(Faulty, &rdev->flags) || |
| 3877 | !(test_bit(In_sync, &rdev->flags) || |
| 3878 | rdev->recovery_offset >= end_sector))) |
| 3879 | rdev = NULL; |
| 3880 | } |
| 3881 | if (rdev) { |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3882 | sector_t first_bad; |
| 3883 | int bad_sectors; |
| 3884 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3885 | atomic_inc(&rdev->nr_pending); |
| 3886 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3887 | raid_bio->bi_next = (void*)rdev; |
| 3888 | align_bi->bi_bdev = rdev->bdev; |
| 3889 | align_bi->bi_flags &= ~(1 << BIO_SEG_VALID); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3890 | |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3891 | if (!bio_fits_rdev(align_bi) || |
| 3892 | is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9, |
| 3893 | &first_bad, &bad_sectors)) { |
| 3894 | /* too big in some way, or has a known bad block */ |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3895 | bio_put(align_bi); |
| 3896 | rdev_dec_pending(rdev, mddev); |
| 3897 | return 0; |
| 3898 | } |
| 3899 | |
majianpeng | 6c0544e | 2012-06-12 08:31:10 +0800 | [diff] [blame] | 3900 | /* No reshape active, so we can trust rdev->data_offset */ |
| 3901 | align_bi->bi_sector += rdev->data_offset; |
| 3902 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3903 | spin_lock_irq(&conf->device_lock); |
| 3904 | wait_event_lock_irq(conf->wait_for_stripe, |
| 3905 | conf->quiesce == 0, |
| 3906 | conf->device_lock, /* nothing */); |
| 3907 | atomic_inc(&conf->active_aligned_reads); |
| 3908 | spin_unlock_irq(&conf->device_lock); |
| 3909 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3910 | generic_make_request(align_bi); |
| 3911 | return 1; |
| 3912 | } else { |
| 3913 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3914 | bio_put(align_bi); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3915 | return 0; |
| 3916 | } |
| 3917 | } |
| 3918 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3919 | /* __get_priority_stripe - get the next stripe to process |
| 3920 | * |
| 3921 | * Full stripe writes are allowed to pass preread active stripes up until |
| 3922 | * the bypass_threshold is exceeded. In general the bypass_count |
| 3923 | * increments when the handle_list is handled before the hold_list; however, it |
| 3924 | * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a |
| 3925 | * stripe with in flight i/o. The bypass_count will be reset when the |
| 3926 | * head of the hold_list has changed, i.e. the head was promoted to the |
| 3927 | * handle_list. |
| 3928 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3929 | static struct stripe_head *__get_priority_stripe(struct r5conf *conf) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3930 | { |
| 3931 | struct stripe_head *sh; |
| 3932 | |
| 3933 | pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n", |
| 3934 | __func__, |
| 3935 | list_empty(&conf->handle_list) ? "empty" : "busy", |
| 3936 | list_empty(&conf->hold_list) ? "empty" : "busy", |
| 3937 | atomic_read(&conf->pending_full_writes), conf->bypass_count); |
| 3938 | |
| 3939 | if (!list_empty(&conf->handle_list)) { |
| 3940 | sh = list_entry(conf->handle_list.next, typeof(*sh), lru); |
| 3941 | |
| 3942 | if (list_empty(&conf->hold_list)) |
| 3943 | conf->bypass_count = 0; |
| 3944 | else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) { |
| 3945 | if (conf->hold_list.next == conf->last_hold) |
| 3946 | conf->bypass_count++; |
| 3947 | else { |
| 3948 | conf->last_hold = conf->hold_list.next; |
| 3949 | conf->bypass_count -= conf->bypass_threshold; |
| 3950 | if (conf->bypass_count < 0) |
| 3951 | conf->bypass_count = 0; |
| 3952 | } |
| 3953 | } |
| 3954 | } else if (!list_empty(&conf->hold_list) && |
| 3955 | ((conf->bypass_threshold && |
| 3956 | conf->bypass_count > conf->bypass_threshold) || |
| 3957 | atomic_read(&conf->pending_full_writes) == 0)) { |
| 3958 | sh = list_entry(conf->hold_list.next, |
| 3959 | typeof(*sh), lru); |
| 3960 | conf->bypass_count -= conf->bypass_threshold; |
| 3961 | if (conf->bypass_count < 0) |
| 3962 | conf->bypass_count = 0; |
| 3963 | } else |
| 3964 | return NULL; |
| 3965 | |
| 3966 | list_del_init(&sh->lru); |
| 3967 | atomic_inc(&sh->count); |
| 3968 | BUG_ON(atomic_read(&sh->count) != 1); |
| 3969 | return sh; |
| 3970 | } |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3971 | |
Linus Torvalds | b4fdcb0 | 2011-11-04 17:06:58 -0700 | [diff] [blame] | 3972 | static void make_request(struct mddev *mddev, struct bio * bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3973 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3974 | struct r5conf *conf = mddev->private; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3975 | int dd_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3976 | sector_t new_sector; |
| 3977 | sector_t logical_sector, last_sector; |
| 3978 | struct stripe_head *sh; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 3979 | const int rw = bio_data_dir(bi); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 3980 | int remaining; |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 3981 | int plugged; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3982 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 3983 | if (unlikely(bi->bi_rw & REQ_FLUSH)) { |
| 3984 | md_flush_request(mddev, bi); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 3985 | return; |
NeilBrown | e5dcdd8 | 2005-09-09 16:23:41 -0700 | [diff] [blame] | 3986 | } |
| 3987 | |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 3988 | md_write_start(mddev, bi); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 3989 | |
NeilBrown | 802ba06 | 2006-12-13 00:34:13 -0800 | [diff] [blame] | 3990 | if (rw == READ && |
Raz Ben-Jehuda(caro) | 5248861 | 2006-12-10 02:20:48 -0800 | [diff] [blame] | 3991 | mddev->reshape_position == MaxSector && |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 3992 | chunk_aligned_read(mddev,bi)) |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 3993 | return; |
Raz Ben-Jehuda(caro) | 5248861 | 2006-12-10 02:20:48 -0800 | [diff] [blame] | 3994 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3995 | logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
| 3996 | last_sector = bi->bi_sector + (bi->bi_size>>9); |
| 3997 | bi->bi_next = NULL; |
| 3998 | bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 3999 | |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 4000 | plugged = mddev_check_plugged(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4001 | for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) { |
| 4002 | DEFINE_WAIT(w); |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4003 | int previous; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 4004 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4005 | retry: |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4006 | previous = 0; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 4007 | prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 4008 | if (unlikely(conf->reshape_progress != MaxSector)) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4009 | /* spinlock is needed as reshape_progress may be |
NeilBrown | df8e7f76 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 4010 | * 64bit on a 32bit platform, and so it might be |
| 4011 | * possible to see a half-updated value |
Jesper Juhl | aeb878b | 2011-04-10 18:06:17 +0200 | [diff] [blame] | 4012 | * Of course reshape_progress could change after |
NeilBrown | df8e7f76 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 4013 | * the lock is dropped, so once we get a reference |
| 4014 | * to the stripe that we think it is, we will have |
| 4015 | * to check again. |
| 4016 | */ |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4017 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4018 | if (mddev->reshape_backwards |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4019 | ? logical_sector < conf->reshape_progress |
| 4020 | : logical_sector >= conf->reshape_progress) { |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4021 | previous = 1; |
| 4022 | } else { |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4023 | if (mddev->reshape_backwards |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4024 | ? logical_sector < conf->reshape_safe |
| 4025 | : logical_sector >= conf->reshape_safe) { |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 4026 | spin_unlock_irq(&conf->device_lock); |
| 4027 | schedule(); |
| 4028 | goto retry; |
| 4029 | } |
| 4030 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4031 | spin_unlock_irq(&conf->device_lock); |
| 4032 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4033 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4034 | new_sector = raid5_compute_sector(conf, logical_sector, |
| 4035 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4036 | &dd_idx, NULL); |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4037 | pr_debug("raid456: make_request, sector %llu logical %llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4038 | (unsigned long long)new_sector, |
| 4039 | (unsigned long long)logical_sector); |
| 4040 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4041 | sh = get_active_stripe(conf, new_sector, previous, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4042 | (bi->bi_rw&RWA_MASK), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4043 | if (sh) { |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 4044 | if (unlikely(previous)) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4045 | /* expansion might have moved on while waiting for a |
NeilBrown | df8e7f76 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 4046 | * stripe, so we must do the range check again. |
| 4047 | * Expansion could still move past after this |
| 4048 | * test, but as we are holding a reference to |
| 4049 | * 'sh', we know that if that happens, |
| 4050 | * STRIPE_EXPANDING will get set and the expansion |
| 4051 | * won't proceed until we finish with the stripe. |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4052 | */ |
| 4053 | int must_retry = 0; |
| 4054 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4055 | if (mddev->reshape_backwards |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 4056 | ? logical_sector >= conf->reshape_progress |
| 4057 | : logical_sector < conf->reshape_progress) |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4058 | /* mismatch, need to try again */ |
| 4059 | must_retry = 1; |
| 4060 | spin_unlock_irq(&conf->device_lock); |
| 4061 | if (must_retry) { |
| 4062 | release_stripe(sh); |
Dan Williams | 7a3ab90 | 2009-06-16 16:00:33 -0700 | [diff] [blame] | 4063 | schedule(); |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4064 | goto retry; |
| 4065 | } |
| 4066 | } |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 4067 | |
Namhyung Kim | ffd96e3 | 2011-07-18 17:38:51 +1000 | [diff] [blame] | 4068 | if (rw == WRITE && |
NeilBrown | a5c308d | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 4069 | logical_sector >= mddev->suspend_lo && |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4070 | logical_sector < mddev->suspend_hi) { |
| 4071 | release_stripe(sh); |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 4072 | /* As the suspend_* range is controlled by |
| 4073 | * userspace, we want an interruptible |
| 4074 | * wait. |
| 4075 | */ |
| 4076 | flush_signals(current); |
| 4077 | prepare_to_wait(&conf->wait_for_overlap, |
| 4078 | &w, TASK_INTERRUPTIBLE); |
| 4079 | if (logical_sector >= mddev->suspend_lo && |
| 4080 | logical_sector < mddev->suspend_hi) |
| 4081 | schedule(); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4082 | goto retry; |
| 4083 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4084 | |
| 4085 | if (test_bit(STRIPE_EXPANDING, &sh->state) || |
Namhyung Kim | ffd96e3 | 2011-07-18 17:38:51 +1000 | [diff] [blame] | 4086 | !add_stripe_bio(sh, bi, dd_idx, rw)) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4087 | /* Stripe is busy expanding or |
| 4088 | * add failed due to overlap. Flush everything |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4089 | * and wait a while |
| 4090 | */ |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 4091 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4092 | release_stripe(sh); |
| 4093 | schedule(); |
| 4094 | goto retry; |
| 4095 | } |
| 4096 | finish_wait(&conf->wait_for_overlap, &w); |
NeilBrown | 6ed3003 | 2008-02-06 01:40:00 -0800 | [diff] [blame] | 4097 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4098 | clear_bit(STRIPE_DELAYED, &sh->state); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 4099 | if ((bi->bi_rw & REQ_SYNC) && |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 4100 | !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 4101 | atomic_inc(&conf->preread_active_stripes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4102 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4103 | } else { |
| 4104 | /* cannot get stripe for read-ahead, just give-up */ |
| 4105 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
| 4106 | finish_wait(&conf->wait_for_overlap, &w); |
| 4107 | break; |
| 4108 | } |
| 4109 | |
| 4110 | } |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 4111 | if (!plugged) |
| 4112 | md_wakeup_thread(mddev->thread); |
| 4113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4114 | spin_lock_irq(&conf->device_lock); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4115 | remaining = raid5_dec_bi_phys_segments(bi); |
NeilBrown | f634475 | 2006-03-27 01:18:17 -0800 | [diff] [blame] | 4116 | spin_unlock_irq(&conf->device_lock); |
| 4117 | if (remaining == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4118 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4119 | if ( rw == WRITE ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4120 | md_write_end(mddev); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 4121 | |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 4122 | bio_endio(bi, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4123 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4124 | } |
| 4125 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4126 | 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] | 4127 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4128 | 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] | 4129 | { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4130 | /* reshaping is quite different to recovery/resync so it is |
| 4131 | * handled quite separately ... here. |
| 4132 | * |
| 4133 | * On each call to sync_request, we gather one chunk worth of |
| 4134 | * destination stripes and flag them as expanding. |
| 4135 | * Then we find all the source stripes and request reads. |
| 4136 | * As the reads complete, handle_stripe will copy the data |
| 4137 | * into the destination stripe and release that stripe. |
| 4138 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4139 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4140 | struct stripe_head *sh; |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4141 | sector_t first_sector, last_sector; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4142 | int raid_disks = conf->previous_raid_disks; |
| 4143 | int data_disks = raid_disks - conf->max_degraded; |
| 4144 | int new_data_disks = conf->raid_disks - conf->max_degraded; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4145 | int i; |
| 4146 | int dd_idx; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4147 | sector_t writepos, readpos, safepos; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4148 | sector_t stripe_addr; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4149 | int reshape_sectors; |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4150 | struct list_head stripes; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4151 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4152 | if (sector_nr == 0) { |
| 4153 | /* If restarting in the middle, skip the initial sectors */ |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4154 | if (mddev->reshape_backwards && |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4155 | conf->reshape_progress < raid5_size(mddev, 0, 0)) { |
| 4156 | sector_nr = raid5_size(mddev, 0, 0) |
| 4157 | - conf->reshape_progress; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4158 | } else if (!mddev->reshape_backwards && |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4159 | conf->reshape_progress > 0) |
| 4160 | sector_nr = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4161 | sector_div(sector_nr, new_data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4162 | if (sector_nr) { |
NeilBrown | 8dee721 | 2009-11-06 14:59:29 +1100 | [diff] [blame] | 4163 | mddev->curr_resync_completed = sector_nr; |
| 4164 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4165 | *skipped = 1; |
| 4166 | return sector_nr; |
| 4167 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4168 | } |
| 4169 | |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4170 | /* We need to process a full chunk at a time. |
| 4171 | * If old and new chunk sizes differ, we need to process the |
| 4172 | * largest of these |
| 4173 | */ |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4174 | if (mddev->new_chunk_sectors > mddev->chunk_sectors) |
| 4175 | reshape_sectors = mddev->new_chunk_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4176 | else |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4177 | reshape_sectors = mddev->chunk_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4178 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 4179 | /* We update the metadata at least every 10 seconds, or when |
| 4180 | * the data about to be copied would over-write the source of |
| 4181 | * the data at the front of the range. i.e. one new_stripe |
| 4182 | * along from reshape_progress new_maps to after where |
| 4183 | * reshape_safe old_maps to |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4184 | */ |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4185 | writepos = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4186 | sector_div(writepos, new_data_disks); |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4187 | readpos = conf->reshape_progress; |
| 4188 | sector_div(readpos, data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4189 | safepos = conf->reshape_safe; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4190 | sector_div(safepos, data_disks); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4191 | if (mddev->reshape_backwards) { |
NeilBrown | ed37d83 | 2009-05-27 21:39:05 +1000 | [diff] [blame] | 4192 | writepos -= min_t(sector_t, reshape_sectors, writepos); |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4193 | readpos += reshape_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4194 | safepos += reshape_sectors; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4195 | } else { |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4196 | writepos += reshape_sectors; |
NeilBrown | ed37d83 | 2009-05-27 21:39:05 +1000 | [diff] [blame] | 4197 | readpos -= min_t(sector_t, reshape_sectors, readpos); |
| 4198 | safepos -= min_t(sector_t, reshape_sectors, safepos); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4199 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4200 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 4201 | /* Having calculated the 'writepos' possibly use it |
| 4202 | * to set 'stripe_addr' which is where we will write to. |
| 4203 | */ |
| 4204 | if (mddev->reshape_backwards) { |
| 4205 | BUG_ON(conf->reshape_progress == 0); |
| 4206 | stripe_addr = writepos; |
| 4207 | BUG_ON((mddev->dev_sectors & |
| 4208 | ~((sector_t)reshape_sectors - 1)) |
| 4209 | - reshape_sectors - stripe_addr |
| 4210 | != sector_nr); |
| 4211 | } else { |
| 4212 | BUG_ON(writepos != sector_nr + reshape_sectors); |
| 4213 | stripe_addr = sector_nr; |
| 4214 | } |
| 4215 | |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4216 | /* 'writepos' is the most advanced device address we might write. |
| 4217 | * 'readpos' is the least advanced device address we might read. |
| 4218 | * 'safepos' is the least address recorded in the metadata as having |
| 4219 | * been reshaped. |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 4220 | * If there is a min_offset_diff, these are adjusted either by |
| 4221 | * increasing the safepos/readpos if diff is negative, or |
| 4222 | * increasing writepos if diff is positive. |
| 4223 | * If 'readpos' is then behind 'writepos', there is no way that we can |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4224 | * ensure safety in the face of a crash - that must be done by userspace |
| 4225 | * making a backup of the data. So in that case there is no particular |
| 4226 | * rush to update metadata. |
| 4227 | * Otherwise if 'safepos' is behind 'writepos', then we really need to |
| 4228 | * update the metadata to advance 'safepos' to match 'readpos' so that |
| 4229 | * we can be safe in the event of a crash. |
| 4230 | * So we insist on updating metadata if safepos is behind writepos and |
| 4231 | * readpos is beyond writepos. |
| 4232 | * In any case, update the metadata every 10 seconds. |
| 4233 | * Maybe that number should be configurable, but I'm not sure it is |
| 4234 | * worth it.... maybe it could be a multiple of safemode_delay??? |
| 4235 | */ |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 4236 | if (conf->min_offset_diff < 0) { |
| 4237 | safepos += -conf->min_offset_diff; |
| 4238 | readpos += -conf->min_offset_diff; |
| 4239 | } else |
| 4240 | writepos += conf->min_offset_diff; |
| 4241 | |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4242 | if ((mddev->reshape_backwards |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4243 | ? (safepos > writepos && readpos < writepos) |
| 4244 | : (safepos < writepos && readpos > writepos)) || |
| 4245 | time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4246 | /* Cannot proceed until we've updated the superblock... */ |
| 4247 | wait_event(conf->wait_for_overlap, |
| 4248 | atomic_read(&conf->reshape_stripes)==0); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4249 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4250 | mddev->curr_resync_completed = sector_nr; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4251 | conf->reshape_checkpoint = jiffies; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4252 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4253 | md_wakeup_thread(mddev->thread); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4254 | wait_event(mddev->sb_wait, mddev->flags == 0 || |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4255 | kthread_should_stop()); |
| 4256 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4257 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4258 | spin_unlock_irq(&conf->device_lock); |
| 4259 | wake_up(&conf->wait_for_overlap); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4260 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4261 | } |
| 4262 | |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4263 | INIT_LIST_HEAD(&stripes); |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4264 | for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4265 | int j; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4266 | int skipped_disk = 0; |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4267 | sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4268 | set_bit(STRIPE_EXPANDING, &sh->state); |
| 4269 | atomic_inc(&conf->reshape_stripes); |
| 4270 | /* If any of this stripe is beyond the end of the old |
| 4271 | * array, then we need to zero those blocks |
| 4272 | */ |
| 4273 | for (j=sh->disks; j--;) { |
| 4274 | sector_t s; |
| 4275 | if (j == sh->pd_idx) |
| 4276 | continue; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4277 | if (conf->level == 6 && |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4278 | j == sh->qd_idx) |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4279 | continue; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 4280 | s = compute_blocknr(sh, j, 0); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4281 | if (s < raid5_size(mddev, 0, 0)) { |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4282 | skipped_disk = 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4283 | continue; |
| 4284 | } |
| 4285 | memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE); |
| 4286 | set_bit(R5_Expanded, &sh->dev[j].flags); |
| 4287 | set_bit(R5_UPTODATE, &sh->dev[j].flags); |
| 4288 | } |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4289 | if (!skipped_disk) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4290 | set_bit(STRIPE_EXPAND_READY, &sh->state); |
| 4291 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4292 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4293 | list_add(&sh->lru, &stripes); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4294 | } |
| 4295 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4296 | if (mddev->reshape_backwards) |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4297 | conf->reshape_progress -= reshape_sectors * new_data_disks; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4298 | else |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4299 | conf->reshape_progress += reshape_sectors * new_data_disks; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4300 | spin_unlock_irq(&conf->device_lock); |
| 4301 | /* Ok, those stripe are ready. We can start scheduling |
| 4302 | * reads on the source stripes. |
| 4303 | * The source stripes are determined by mapping the first and last |
| 4304 | * block on the destination stripes. |
| 4305 | */ |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4306 | first_sector = |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4307 | raid5_compute_sector(conf, stripe_addr*(new_data_disks), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4308 | 1, &dd_idx, NULL); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4309 | last_sector = |
NeilBrown | 0e6e027 | 2009-06-09 16:32:22 +1000 | [diff] [blame] | 4310 | raid5_compute_sector(conf, ((stripe_addr+reshape_sectors) |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 4311 | * new_data_disks - 1), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4312 | 1, &dd_idx, NULL); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4313 | if (last_sector >= mddev->dev_sectors) |
| 4314 | last_sector = mddev->dev_sectors - 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4315 | while (first_sector <= last_sector) { |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4316 | sh = get_active_stripe(conf, first_sector, 1, 0, 1); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4317 | set_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 4318 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4319 | release_stripe(sh); |
| 4320 | first_sector += STRIPE_SECTORS; |
| 4321 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4322 | /* Now that the sources are clearly marked, we can release |
| 4323 | * the destination stripes |
| 4324 | */ |
| 4325 | while (!list_empty(&stripes)) { |
| 4326 | sh = list_entry(stripes.next, struct stripe_head, lru); |
| 4327 | list_del_init(&sh->lru); |
| 4328 | release_stripe(sh); |
| 4329 | } |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4330 | /* If this takes us to the resync_max point where we have to pause, |
| 4331 | * then we need to write out the superblock. |
| 4332 | */ |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4333 | sector_nr += reshape_sectors; |
NeilBrown | c03f6a1 | 2009-04-17 11:06:30 +1000 | [diff] [blame] | 4334 | if ((sector_nr - mddev->curr_resync_completed) * 2 |
| 4335 | >= mddev->resync_max - mddev->curr_resync_completed) { |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4336 | /* Cannot proceed until we've updated the superblock... */ |
| 4337 | wait_event(conf->wait_for_overlap, |
| 4338 | atomic_read(&conf->reshape_stripes) == 0); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4339 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4340 | mddev->curr_resync_completed = sector_nr; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4341 | conf->reshape_checkpoint = jiffies; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4342 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 4343 | md_wakeup_thread(mddev->thread); |
| 4344 | wait_event(mddev->sb_wait, |
| 4345 | !test_bit(MD_CHANGE_DEVS, &mddev->flags) |
| 4346 | || kthread_should_stop()); |
| 4347 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4348 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4349 | spin_unlock_irq(&conf->device_lock); |
| 4350 | wake_up(&conf->wait_for_overlap); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4351 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4352 | } |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4353 | return reshape_sectors; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4354 | } |
| 4355 | |
| 4356 | /* FIXME go_faster isn't used */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4357 | 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] | 4358 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4359 | struct r5conf *conf = mddev->private; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4360 | struct stripe_head *sh; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4361 | sector_t max_sector = mddev->dev_sectors; |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 4362 | sector_t sync_blocks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4363 | int still_degraded = 0; |
| 4364 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4365 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4366 | if (sector_nr >= max_sector) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4367 | /* just being told to finish up .. nothing much to do */ |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 4368 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 4369 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { |
| 4370 | end_reshape(conf); |
| 4371 | return 0; |
| 4372 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4373 | |
| 4374 | if (mddev->curr_resync < max_sector) /* aborted */ |
| 4375 | bitmap_end_sync(mddev->bitmap, mddev->curr_resync, |
| 4376 | &sync_blocks, 1); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4377 | else /* completed sync */ |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4378 | conf->fullsync = 0; |
| 4379 | bitmap_close_sync(mddev->bitmap); |
| 4380 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4381 | return 0; |
| 4382 | } |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4383 | |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4384 | /* Allow raid5_quiesce to complete */ |
| 4385 | wait_event(conf->wait_for_overlap, conf->quiesce != 2); |
| 4386 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4387 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 4388 | return reshape_request(mddev, sector_nr, skipped); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4389 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4390 | /* No need to check resync_max as we never do more than one |
| 4391 | * stripe, and as resync_max will always be on a chunk boundary, |
| 4392 | * if the check in md_do_sync didn't fire, there is no chance |
| 4393 | * of overstepping resync_max here |
| 4394 | */ |
| 4395 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4396 | /* if there is too many failed drives and we are trying |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4397 | * to resync, then assert that we are finished, because there is |
| 4398 | * nothing we can do. |
| 4399 | */ |
NeilBrown | 3285edf | 2006-06-26 00:27:55 -0700 | [diff] [blame] | 4400 | if (mddev->degraded >= conf->max_degraded && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4401 | test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4402 | sector_t rv = mddev->dev_sectors - sector_nr; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 4403 | *skipped = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4404 | return rv; |
| 4405 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4406 | if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) && |
NeilBrown | 3855ad9 | 2005-11-08 21:39:38 -0800 | [diff] [blame] | 4407 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4408 | !conf->fullsync && sync_blocks >= STRIPE_SECTORS) { |
| 4409 | /* we can skip this block, and probably more */ |
| 4410 | sync_blocks /= STRIPE_SECTORS; |
| 4411 | *skipped = 1; |
| 4412 | return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */ |
| 4413 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4414 | |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 4415 | bitmap_cond_end_sync(mddev->bitmap, sector_nr); |
| 4416 | |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4417 | sh = get_active_stripe(conf, sector_nr, 0, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4418 | if (sh == NULL) { |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4419 | sh = get_active_stripe(conf, sector_nr, 0, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4420 | /* make sure we don't swamp the stripe cache if someone else |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4421 | * is trying to get access |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4422 | */ |
Nishanth Aravamudan | 66c006a | 2005-11-07 01:01:17 -0800 | [diff] [blame] | 4423 | schedule_timeout_uninterruptible(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4424 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4425 | /* Need to check if array will still be degraded after recovery/resync |
| 4426 | * We don't need to check the 'failed' flag as when that gets set, |
| 4427 | * recovery aborts. |
| 4428 | */ |
NeilBrown | f001a70 | 2009-06-09 14:30:31 +1000 | [diff] [blame] | 4429 | for (i = 0; i < conf->raid_disks; i++) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4430 | if (conf->disks[i].rdev == NULL) |
| 4431 | still_degraded = 1; |
| 4432 | |
| 4433 | bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded); |
| 4434 | |
NeilBrown | 83206d6 | 2011-07-26 11:19:49 +1000 | [diff] [blame] | 4435 | set_bit(STRIPE_SYNC_REQUESTED, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4436 | |
NeilBrown | 1442577 | 2009-10-16 15:55:25 +1100 | [diff] [blame] | 4437 | handle_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4438 | release_stripe(sh); |
| 4439 | |
| 4440 | return STRIPE_SECTORS; |
| 4441 | } |
| 4442 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4443 | 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] | 4444 | { |
| 4445 | /* We may not be able to submit a whole bio at once as there |
| 4446 | * may not be enough stripe_heads available. |
| 4447 | * We cannot pre-allocate enough stripe_heads as we may need |
| 4448 | * more than exist in the cache (if we allow ever large chunks). |
| 4449 | * So we do one stripe head at a time and record in |
| 4450 | * ->bi_hw_segments how many have been done. |
| 4451 | * |
| 4452 | * We *know* that this entire raid_bio is in one chunk, so |
| 4453 | * it will be only one 'dd_idx' and only need one call to raid5_compute_sector. |
| 4454 | */ |
| 4455 | struct stripe_head *sh; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4456 | int dd_idx; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4457 | sector_t sector, logical_sector, last_sector; |
| 4458 | int scnt = 0; |
| 4459 | int remaining; |
| 4460 | int handled = 0; |
| 4461 | |
| 4462 | logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4463 | sector = raid5_compute_sector(conf, logical_sector, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4464 | 0, &dd_idx, NULL); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4465 | last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9); |
| 4466 | |
| 4467 | for (; logical_sector < last_sector; |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4468 | logical_sector += STRIPE_SECTORS, |
| 4469 | sector += STRIPE_SECTORS, |
| 4470 | scnt++) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4471 | |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4472 | if (scnt < raid5_bi_hw_segments(raid_bio)) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4473 | /* already done this stripe */ |
| 4474 | continue; |
| 4475 | |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4476 | sh = get_active_stripe(conf, sector, 0, 1, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4477 | |
| 4478 | if (!sh) { |
| 4479 | /* failed to get a stripe - must wait */ |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4480 | raid5_set_bi_hw_segments(raid_bio, scnt); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4481 | conf->retry_read_aligned = raid_bio; |
| 4482 | return handled; |
| 4483 | } |
| 4484 | |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4485 | if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) { |
| 4486 | release_stripe(sh); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4487 | raid5_set_bi_hw_segments(raid_bio, scnt); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4488 | conf->retry_read_aligned = raid_bio; |
| 4489 | return handled; |
| 4490 | } |
| 4491 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4492 | handle_stripe(sh); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4493 | release_stripe(sh); |
| 4494 | handled++; |
| 4495 | } |
| 4496 | spin_lock_irq(&conf->device_lock); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4497 | remaining = raid5_dec_bi_phys_segments(raid_bio); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4498 | spin_unlock_irq(&conf->device_lock); |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 4499 | if (remaining == 0) |
| 4500 | bio_endio(raid_bio, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4501 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
| 4502 | wake_up(&conf->wait_for_stripe); |
| 4503 | return handled; |
| 4504 | } |
| 4505 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4506 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4507 | /* |
| 4508 | * This is our raid5 kernel thread. |
| 4509 | * |
| 4510 | * We scan the hash table for stripes which can be handled now. |
| 4511 | * During the scan, completed stripes are saved for us by the interrupt |
| 4512 | * handler, so that they will not have to wait for our next wakeup. |
| 4513 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4514 | static void raid5d(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4515 | { |
| 4516 | struct stripe_head *sh; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4517 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4518 | int handled; |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 4519 | struct blk_plug plug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4520 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4521 | pr_debug("+++ raid5d active\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4522 | |
| 4523 | md_check_recovery(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4524 | |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 4525 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4526 | handled = 0; |
| 4527 | spin_lock_irq(&conf->device_lock); |
| 4528 | while (1) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4529 | struct bio *bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4530 | |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 4531 | if (atomic_read(&mddev->plug_cnt) == 0 && |
| 4532 | !list_empty(&conf->bitmap_list)) { |
| 4533 | /* Now is a good time to flush some bitmap updates */ |
| 4534 | conf->seq_flush++; |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 4535 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4536 | bitmap_unplug(mddev->bitmap); |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 4537 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 4538 | conf->seq_write = conf->seq_flush; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4539 | activate_bit_delay(conf); |
| 4540 | } |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 4541 | if (atomic_read(&mddev->plug_cnt) == 0) |
| 4542 | raid5_activate_delayed(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4543 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4544 | while ((bio = remove_bio_from_retry(conf))) { |
| 4545 | int ok; |
| 4546 | spin_unlock_irq(&conf->device_lock); |
| 4547 | ok = retry_aligned_read(conf, bio); |
| 4548 | spin_lock_irq(&conf->device_lock); |
| 4549 | if (!ok) |
| 4550 | break; |
| 4551 | handled++; |
| 4552 | } |
| 4553 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4554 | sh = __get_priority_stripe(conf); |
| 4555 | |
Dan Williams | c9f21aa | 2008-07-23 12:05:51 -0700 | [diff] [blame] | 4556 | if (!sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4557 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4558 | spin_unlock_irq(&conf->device_lock); |
| 4559 | |
| 4560 | handled++; |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 4561 | handle_stripe(sh); |
| 4562 | release_stripe(sh); |
| 4563 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4564 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 4565 | if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) |
| 4566 | md_check_recovery(mddev); |
| 4567 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4568 | spin_lock_irq(&conf->device_lock); |
| 4569 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4570 | pr_debug("%d stripes handled\n", handled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4571 | |
| 4572 | spin_unlock_irq(&conf->device_lock); |
| 4573 | |
Dan Williams | c9f21aa | 2008-07-23 12:05:51 -0700 | [diff] [blame] | 4574 | async_tx_issue_pending_all(); |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 4575 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4576 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4577 | pr_debug("--- raid5d inactive\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4578 | } |
| 4579 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4580 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4581 | raid5_show_stripe_cache_size(struct mddev *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4582 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4583 | struct r5conf *conf = mddev->private; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4584 | if (conf) |
| 4585 | return sprintf(page, "%d\n", conf->max_nr_stripes); |
| 4586 | else |
| 4587 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4588 | } |
| 4589 | |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 4590 | int |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4591 | raid5_set_cache_size(struct mddev *mddev, int size) |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 4592 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4593 | struct r5conf *conf = mddev->private; |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 4594 | int err; |
| 4595 | |
| 4596 | if (size <= 16 || size > 32768) |
| 4597 | return -EINVAL; |
| 4598 | while (size < conf->max_nr_stripes) { |
| 4599 | if (drop_one_stripe(conf)) |
| 4600 | conf->max_nr_stripes--; |
| 4601 | else |
| 4602 | break; |
| 4603 | } |
| 4604 | err = md_allow_write(mddev); |
| 4605 | if (err) |
| 4606 | return err; |
| 4607 | while (size > conf->max_nr_stripes) { |
| 4608 | if (grow_one_stripe(conf)) |
| 4609 | conf->max_nr_stripes++; |
| 4610 | else break; |
| 4611 | } |
| 4612 | return 0; |
| 4613 | } |
| 4614 | EXPORT_SYMBOL(raid5_set_cache_size); |
| 4615 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4616 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4617 | 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] | 4618 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4619 | struct r5conf *conf = mddev->private; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4620 | unsigned long new; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4621 | int err; |
| 4622 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4623 | if (len >= PAGE_SIZE) |
| 4624 | return -EINVAL; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4625 | if (!conf) |
| 4626 | return -ENODEV; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4627 | |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4628 | if (strict_strtoul(page, 10, &new)) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4629 | return -EINVAL; |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 4630 | err = raid5_set_cache_size(mddev, new); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4631 | if (err) |
| 4632 | return err; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4633 | return len; |
| 4634 | } |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4635 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4636 | static struct md_sysfs_entry |
| 4637 | raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR, |
| 4638 | raid5_show_stripe_cache_size, |
| 4639 | raid5_store_stripe_cache_size); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4640 | |
| 4641 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4642 | raid5_show_preread_threshold(struct mddev *mddev, char *page) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4643 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4644 | struct r5conf *conf = mddev->private; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4645 | if (conf) |
| 4646 | return sprintf(page, "%d\n", conf->bypass_threshold); |
| 4647 | else |
| 4648 | return 0; |
| 4649 | } |
| 4650 | |
| 4651 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4652 | 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] | 4653 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4654 | struct r5conf *conf = mddev->private; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4655 | unsigned long new; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4656 | if (len >= PAGE_SIZE) |
| 4657 | return -EINVAL; |
| 4658 | if (!conf) |
| 4659 | return -ENODEV; |
| 4660 | |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4661 | if (strict_strtoul(page, 10, &new)) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4662 | return -EINVAL; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4663 | if (new > conf->max_nr_stripes) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4664 | return -EINVAL; |
| 4665 | conf->bypass_threshold = new; |
| 4666 | return len; |
| 4667 | } |
| 4668 | |
| 4669 | static struct md_sysfs_entry |
| 4670 | raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold, |
| 4671 | S_IRUGO | S_IWUSR, |
| 4672 | raid5_show_preread_threshold, |
| 4673 | raid5_store_preread_threshold); |
| 4674 | |
| 4675 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4676 | stripe_cache_active_show(struct mddev *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4677 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4678 | struct r5conf *conf = mddev->private; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4679 | if (conf) |
| 4680 | return sprintf(page, "%d\n", atomic_read(&conf->active_stripes)); |
| 4681 | else |
| 4682 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4683 | } |
| 4684 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4685 | static struct md_sysfs_entry |
| 4686 | raid5_stripecache_active = __ATTR_RO(stripe_cache_active); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4687 | |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4688 | static struct attribute *raid5_attrs[] = { |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4689 | &raid5_stripecache_size.attr, |
| 4690 | &raid5_stripecache_active.attr, |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4691 | &raid5_preread_bypass_threshold.attr, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4692 | NULL, |
| 4693 | }; |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4694 | static struct attribute_group raid5_attrs_group = { |
| 4695 | .name = NULL, |
| 4696 | .attrs = raid5_attrs, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4697 | }; |
| 4698 | |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4699 | static sector_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4700 | raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks) |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4701 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4702 | struct r5conf *conf = mddev->private; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4703 | |
| 4704 | if (!sectors) |
| 4705 | sectors = mddev->dev_sectors; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4706 | if (!raid_disks) |
NeilBrown | 7ec0547 | 2009-03-31 15:10:36 +1100 | [diff] [blame] | 4707 | /* size is defined by the smallest of previous and new size */ |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4708 | raid_disks = min(conf->raid_disks, conf->previous_raid_disks); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4709 | |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4710 | sectors &= ~((sector_t)mddev->chunk_sectors - 1); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4711 | sectors &= ~((sector_t)mddev->new_chunk_sectors - 1); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4712 | return sectors * (raid_disks - conf->max_degraded); |
| 4713 | } |
| 4714 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4715 | static void raid5_free_percpu(struct r5conf *conf) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4716 | { |
| 4717 | struct raid5_percpu *percpu; |
| 4718 | unsigned long cpu; |
| 4719 | |
| 4720 | if (!conf->percpu) |
| 4721 | return; |
| 4722 | |
| 4723 | get_online_cpus(); |
| 4724 | for_each_possible_cpu(cpu) { |
| 4725 | percpu = per_cpu_ptr(conf->percpu, cpu); |
| 4726 | safe_put_page(percpu->spare_page); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4727 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4728 | } |
| 4729 | #ifdef CONFIG_HOTPLUG_CPU |
| 4730 | unregister_cpu_notifier(&conf->cpu_notify); |
| 4731 | #endif |
| 4732 | put_online_cpus(); |
| 4733 | |
| 4734 | free_percpu(conf->percpu); |
| 4735 | } |
| 4736 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4737 | static void free_conf(struct r5conf *conf) |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 4738 | { |
| 4739 | shrink_stripes(conf); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4740 | raid5_free_percpu(conf); |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 4741 | kfree(conf->disks); |
| 4742 | kfree(conf->stripe_hashtbl); |
| 4743 | kfree(conf); |
| 4744 | } |
| 4745 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4746 | #ifdef CONFIG_HOTPLUG_CPU |
| 4747 | static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action, |
| 4748 | void *hcpu) |
| 4749 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4750 | struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4751 | long cpu = (long)hcpu; |
| 4752 | struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu); |
| 4753 | |
| 4754 | switch (action) { |
| 4755 | case CPU_UP_PREPARE: |
| 4756 | case CPU_UP_PREPARE_FROZEN: |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4757 | if (conf->level == 6 && !percpu->spare_page) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4758 | percpu->spare_page = alloc_page(GFP_KERNEL); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4759 | if (!percpu->scribble) |
| 4760 | percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL); |
| 4761 | |
| 4762 | if (!percpu->scribble || |
| 4763 | (conf->level == 6 && !percpu->spare_page)) { |
| 4764 | safe_put_page(percpu->spare_page); |
| 4765 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4766 | pr_err("%s: failed memory allocation for cpu%ld\n", |
| 4767 | __func__, cpu); |
Akinobu Mita | 55af6bb | 2010-05-26 14:43:35 -0700 | [diff] [blame] | 4768 | return notifier_from_errno(-ENOMEM); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4769 | } |
| 4770 | break; |
| 4771 | case CPU_DEAD: |
| 4772 | case CPU_DEAD_FROZEN: |
| 4773 | safe_put_page(percpu->spare_page); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4774 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4775 | percpu->spare_page = NULL; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4776 | percpu->scribble = NULL; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4777 | break; |
| 4778 | default: |
| 4779 | break; |
| 4780 | } |
| 4781 | return NOTIFY_OK; |
| 4782 | } |
| 4783 | #endif |
| 4784 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4785 | static int raid5_alloc_percpu(struct r5conf *conf) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4786 | { |
| 4787 | unsigned long cpu; |
| 4788 | struct page *spare_page; |
Tejun Heo | a29d8b8 | 2010-02-02 14:39:15 +0900 | [diff] [blame] | 4789 | struct raid5_percpu __percpu *allcpus; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4790 | void *scribble; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4791 | int err; |
| 4792 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4793 | allcpus = alloc_percpu(struct raid5_percpu); |
| 4794 | if (!allcpus) |
| 4795 | return -ENOMEM; |
| 4796 | conf->percpu = allcpus; |
| 4797 | |
| 4798 | get_online_cpus(); |
| 4799 | err = 0; |
| 4800 | for_each_present_cpu(cpu) { |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4801 | if (conf->level == 6) { |
| 4802 | spare_page = alloc_page(GFP_KERNEL); |
| 4803 | if (!spare_page) { |
| 4804 | err = -ENOMEM; |
| 4805 | break; |
| 4806 | } |
| 4807 | per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page; |
| 4808 | } |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4809 | scribble = kmalloc(conf->scribble_len, GFP_KERNEL); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4810 | if (!scribble) { |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4811 | err = -ENOMEM; |
| 4812 | break; |
| 4813 | } |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4814 | per_cpu_ptr(conf->percpu, cpu)->scribble = scribble; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4815 | } |
| 4816 | #ifdef CONFIG_HOTPLUG_CPU |
| 4817 | conf->cpu_notify.notifier_call = raid456_cpu_notify; |
| 4818 | conf->cpu_notify.priority = 0; |
| 4819 | if (err == 0) |
| 4820 | err = register_cpu_notifier(&conf->cpu_notify); |
| 4821 | #endif |
| 4822 | put_online_cpus(); |
| 4823 | |
| 4824 | return err; |
| 4825 | } |
| 4826 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4827 | static struct r5conf *setup_conf(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4828 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4829 | struct r5conf *conf; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4830 | int raid_disk, memory, max_disks; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4831 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4832 | struct disk_info *disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4833 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4834 | if (mddev->new_level != 5 |
| 4835 | && mddev->new_level != 4 |
| 4836 | && mddev->new_level != 6) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4837 | 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] | 4838 | mdname(mddev), mddev->new_level); |
| 4839 | return ERR_PTR(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4840 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4841 | if ((mddev->new_level == 5 |
| 4842 | && !algorithm_valid_raid5(mddev->new_layout)) || |
| 4843 | (mddev->new_level == 6 |
| 4844 | && !algorithm_valid_raid6(mddev->new_layout))) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4845 | printk(KERN_ERR "md/raid:%s: layout %d not supported\n", |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4846 | mdname(mddev), mddev->new_layout); |
| 4847 | return ERR_PTR(-EIO); |
| 4848 | } |
| 4849 | if (mddev->new_level == 6 && mddev->raid_disks < 4) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4850 | 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] | 4851 | mdname(mddev), mddev->raid_disks); |
| 4852 | return ERR_PTR(-EINVAL); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4853 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4854 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4855 | if (!mddev->new_chunk_sectors || |
| 4856 | (mddev->new_chunk_sectors << 9) % PAGE_SIZE || |
| 4857 | !is_power_of_2(mddev->new_chunk_sectors)) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4858 | printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n", |
| 4859 | mdname(mddev), mddev->new_chunk_sectors << 9); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4860 | return ERR_PTR(-EINVAL); |
NeilBrown | 4bbf377 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4861 | } |
| 4862 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4863 | conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4864 | if (conf == NULL) |
| 4865 | goto abort; |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 4866 | spin_lock_init(&conf->device_lock); |
| 4867 | init_waitqueue_head(&conf->wait_for_stripe); |
| 4868 | init_waitqueue_head(&conf->wait_for_overlap); |
| 4869 | INIT_LIST_HEAD(&conf->handle_list); |
| 4870 | INIT_LIST_HEAD(&conf->hold_list); |
| 4871 | INIT_LIST_HEAD(&conf->delayed_list); |
| 4872 | INIT_LIST_HEAD(&conf->bitmap_list); |
| 4873 | INIT_LIST_HEAD(&conf->inactive_list); |
| 4874 | atomic_set(&conf->active_stripes, 0); |
| 4875 | atomic_set(&conf->preread_active_stripes, 0); |
| 4876 | atomic_set(&conf->active_aligned_reads, 0); |
| 4877 | conf->bypass_threshold = BYPASS_THRESHOLD; |
NeilBrown | d890fa2 | 2011-10-26 11:54:39 +1100 | [diff] [blame] | 4878 | conf->recovery_disabled = mddev->recovery_disabled - 1; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4879 | |
| 4880 | conf->raid_disks = mddev->raid_disks; |
| 4881 | if (mddev->reshape_position == MaxSector) |
| 4882 | conf->previous_raid_disks = mddev->raid_disks; |
| 4883 | else |
| 4884 | conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4885 | max_disks = max(conf->raid_disks, conf->previous_raid_disks); |
| 4886 | conf->scribble_len = scribble_len(max_disks); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4887 | |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4888 | conf->disks = kzalloc(max_disks * sizeof(struct disk_info), |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4889 | GFP_KERNEL); |
| 4890 | if (!conf->disks) |
| 4891 | goto abort; |
| 4892 | |
| 4893 | conf->mddev = mddev; |
| 4894 | |
| 4895 | if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL) |
| 4896 | goto abort; |
| 4897 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4898 | conf->level = mddev->new_level; |
| 4899 | if (raid5_alloc_percpu(conf) != 0) |
| 4900 | goto abort; |
| 4901 | |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4902 | pr_debug("raid456: run(%s) called.\n", mdname(mddev)); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4903 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 4904 | rdev_for_each(rdev, mddev) { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4905 | raid_disk = rdev->raid_disk; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4906 | if (raid_disk >= max_disks |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4907 | || raid_disk < 0) |
| 4908 | continue; |
| 4909 | disk = conf->disks + raid_disk; |
| 4910 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4911 | if (test_bit(Replacement, &rdev->flags)) { |
| 4912 | if (disk->replacement) |
| 4913 | goto abort; |
| 4914 | disk->replacement = rdev; |
| 4915 | } else { |
| 4916 | if (disk->rdev) |
| 4917 | goto abort; |
| 4918 | disk->rdev = rdev; |
| 4919 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4920 | |
| 4921 | if (test_bit(In_sync, &rdev->flags)) { |
| 4922 | char b[BDEVNAME_SIZE]; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4923 | printk(KERN_INFO "md/raid:%s: device %s operational as raid" |
| 4924 | " disk %d\n", |
| 4925 | mdname(mddev), bdevname(rdev->bdev, b), raid_disk); |
Jonathan Brassow | d6b212f | 2011-06-08 18:00:28 -0500 | [diff] [blame] | 4926 | } else if (rdev->saved_raid_disk != raid_disk) |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4927 | /* Cannot rely on bitmap to complete recovery */ |
| 4928 | conf->fullsync = 1; |
| 4929 | } |
| 4930 | |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 4931 | conf->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4932 | conf->level = mddev->new_level; |
| 4933 | if (conf->level == 6) |
| 4934 | conf->max_degraded = 2; |
| 4935 | else |
| 4936 | conf->max_degraded = 1; |
| 4937 | conf->algorithm = mddev->new_layout; |
| 4938 | conf->max_nr_stripes = NR_STRIPES; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4939 | conf->reshape_progress = mddev->reshape_position; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 4940 | if (conf->reshape_progress != MaxSector) { |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 4941 | conf->prev_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 4942 | conf->prev_algo = mddev->layout; |
| 4943 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4944 | |
| 4945 | memory = conf->max_nr_stripes * (sizeof(struct stripe_head) + |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4946 | max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4947 | if (grow_stripes(conf, conf->max_nr_stripes)) { |
| 4948 | printk(KERN_ERR |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4949 | "md/raid:%s: couldn't allocate %dkB for buffers\n", |
| 4950 | mdname(mddev), memory); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4951 | goto abort; |
| 4952 | } else |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4953 | printk(KERN_INFO "md/raid:%s: allocated %dkB\n", |
| 4954 | mdname(mddev), memory); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4955 | |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 4956 | conf->thread = md_register_thread(raid5d, mddev, NULL); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4957 | if (!conf->thread) { |
| 4958 | printk(KERN_ERR |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4959 | "md/raid:%s: couldn't allocate thread.\n", |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4960 | mdname(mddev)); |
| 4961 | goto abort; |
| 4962 | } |
| 4963 | |
| 4964 | return conf; |
| 4965 | |
| 4966 | abort: |
| 4967 | if (conf) { |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 4968 | free_conf(conf); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4969 | return ERR_PTR(-EIO); |
| 4970 | } else |
| 4971 | return ERR_PTR(-ENOMEM); |
| 4972 | } |
| 4973 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 4974 | |
| 4975 | static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded) |
| 4976 | { |
| 4977 | switch (algo) { |
| 4978 | case ALGORITHM_PARITY_0: |
| 4979 | if (raid_disk < max_degraded) |
| 4980 | return 1; |
| 4981 | break; |
| 4982 | case ALGORITHM_PARITY_N: |
| 4983 | if (raid_disk >= raid_disks - max_degraded) |
| 4984 | return 1; |
| 4985 | break; |
| 4986 | case ALGORITHM_PARITY_0_6: |
| 4987 | if (raid_disk == 0 || |
| 4988 | raid_disk == raid_disks - 1) |
| 4989 | return 1; |
| 4990 | break; |
| 4991 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 4992 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 4993 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 4994 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 4995 | if (raid_disk == raid_disks - 1) |
| 4996 | return 1; |
| 4997 | } |
| 4998 | return 0; |
| 4999 | } |
| 5000 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5001 | static int run(struct mddev *mddev) |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5002 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5003 | struct r5conf *conf; |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 5004 | int working_disks = 0; |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5005 | int dirty_parity_disks = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5006 | struct md_rdev *rdev; |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5007 | sector_t reshape_offset = 0; |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5008 | int i; |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5009 | long long min_offset_diff = 0; |
| 5010 | int first = 1; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5011 | |
Andre Noll | 8c6ac868 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 5012 | if (mddev->recovery_cp != MaxSector) |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5013 | printk(KERN_NOTICE "md/raid:%s: not clean" |
Andre Noll | 8c6ac868 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 5014 | " -- starting background reconstruction\n", |
| 5015 | mdname(mddev)); |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5016 | |
| 5017 | rdev_for_each(rdev, mddev) { |
| 5018 | long long diff; |
| 5019 | if (rdev->raid_disk < 0) |
| 5020 | continue; |
| 5021 | diff = (rdev->new_data_offset - rdev->data_offset); |
| 5022 | if (first) { |
| 5023 | min_offset_diff = diff; |
| 5024 | first = 0; |
| 5025 | } else if (mddev->reshape_backwards && |
| 5026 | diff < min_offset_diff) |
| 5027 | min_offset_diff = diff; |
| 5028 | else if (!mddev->reshape_backwards && |
| 5029 | diff > min_offset_diff) |
| 5030 | min_offset_diff = diff; |
| 5031 | } |
| 5032 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5033 | if (mddev->reshape_position != MaxSector) { |
| 5034 | /* Check that we can continue the reshape. |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5035 | * Difficulties arise if the stripe we would write to |
| 5036 | * next is at or after the stripe we would read from next. |
| 5037 | * For a reshape that changes the number of devices, this |
| 5038 | * is only possible for a very short time, and mdadm makes |
| 5039 | * sure that time appears to have past before assembling |
| 5040 | * the array. So we fail if that time hasn't passed. |
| 5041 | * For a reshape that keeps the number of devices the same |
| 5042 | * mdadm must be monitoring the reshape can keeping the |
| 5043 | * critical areas read-only and backed up. It will start |
| 5044 | * the array in read-only mode, so we check for that. |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5045 | */ |
| 5046 | sector_t here_new, here_old; |
| 5047 | int old_disks; |
Andre Noll | 18b0033 | 2009-03-31 15:00:56 +1100 | [diff] [blame] | 5048 | int max_degraded = (mddev->level == 6 ? 2 : 1); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5049 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5050 | if (mddev->new_level != mddev->level) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5051 | printk(KERN_ERR "md/raid:%s: unsupported reshape " |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5052 | "required - aborting.\n", |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5053 | mdname(mddev)); |
| 5054 | return -EINVAL; |
| 5055 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5056 | old_disks = mddev->raid_disks - mddev->delta_disks; |
| 5057 | /* reshape_position must be on a new-stripe boundary, and one |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5058 | * further up in new geometry must map after here in old |
| 5059 | * geometry. |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5060 | */ |
| 5061 | here_new = mddev->reshape_position; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5062 | if (sector_div(here_new, mddev->new_chunk_sectors * |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5063 | (mddev->raid_disks - max_degraded))) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5064 | printk(KERN_ERR "md/raid:%s: reshape_position not " |
| 5065 | "on a stripe boundary\n", mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5066 | return -EINVAL; |
| 5067 | } |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5068 | reshape_offset = here_new * mddev->new_chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5069 | /* here_new is the stripe we will write to */ |
| 5070 | here_old = mddev->reshape_position; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5071 | sector_div(here_old, mddev->chunk_sectors * |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5072 | (old_disks-max_degraded)); |
| 5073 | /* here_old is the first stripe that we might need to read |
| 5074 | * from */ |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5075 | if (mddev->delta_disks == 0) { |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5076 | if ((here_new * mddev->new_chunk_sectors != |
| 5077 | here_old * mddev->chunk_sectors)) { |
| 5078 | printk(KERN_ERR "md/raid:%s: reshape position is" |
| 5079 | " confused - aborting\n", mdname(mddev)); |
| 5080 | return -EINVAL; |
| 5081 | } |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5082 | /* We cannot be sure it is safe to start an in-place |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5083 | * reshape. It is only safe if user-space is monitoring |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5084 | * and taking constant backups. |
| 5085 | * mdadm always starts a situation like this in |
| 5086 | * readonly mode so it can take control before |
| 5087 | * allowing any writes. So just check for that. |
| 5088 | */ |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5089 | if (abs(min_offset_diff) >= mddev->chunk_sectors && |
| 5090 | abs(min_offset_diff) >= mddev->new_chunk_sectors) |
| 5091 | /* not really in-place - so OK */; |
| 5092 | else if (mddev->ro == 0) { |
| 5093 | printk(KERN_ERR "md/raid:%s: in-place reshape " |
| 5094 | "must be started in read-only mode " |
| 5095 | "- aborting\n", |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5096 | mdname(mddev)); |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5097 | return -EINVAL; |
| 5098 | } |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5099 | } else if (mddev->reshape_backwards |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5100 | ? (here_new * mddev->new_chunk_sectors + min_offset_diff <= |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5101 | here_old * mddev->chunk_sectors) |
| 5102 | : (here_new * mddev->new_chunk_sectors >= |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5103 | here_old * mddev->chunk_sectors + (-min_offset_diff))) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5104 | /* Reading from the same stripe as writing to - bad */ |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5105 | printk(KERN_ERR "md/raid:%s: reshape_position too early for " |
| 5106 | "auto-recovery - aborting.\n", |
| 5107 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5108 | return -EINVAL; |
| 5109 | } |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5110 | printk(KERN_INFO "md/raid:%s: reshape will continue\n", |
| 5111 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5112 | /* OK, we should be able to continue; */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5113 | } else { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5114 | BUG_ON(mddev->level != mddev->new_level); |
| 5115 | BUG_ON(mddev->layout != mddev->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5116 | BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5117 | BUG_ON(mddev->delta_disks != 0); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5118 | } |
| 5119 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5120 | if (mddev->private == NULL) |
| 5121 | conf = setup_conf(mddev); |
| 5122 | else |
| 5123 | conf = mddev->private; |
| 5124 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5125 | if (IS_ERR(conf)) |
| 5126 | return PTR_ERR(conf); |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 5127 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5128 | conf->min_offset_diff = min_offset_diff; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5129 | mddev->thread = conf->thread; |
| 5130 | conf->thread = NULL; |
| 5131 | mddev->private = conf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5132 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5133 | for (i = 0; i < conf->raid_disks && conf->previous_raid_disks; |
| 5134 | i++) { |
| 5135 | rdev = conf->disks[i].rdev; |
| 5136 | if (!rdev && conf->disks[i].replacement) { |
| 5137 | /* The replacement is all we have yet */ |
| 5138 | rdev = conf->disks[i].replacement; |
| 5139 | conf->disks[i].replacement = NULL; |
| 5140 | clear_bit(Replacement, &rdev->flags); |
| 5141 | conf->disks[i].rdev = rdev; |
| 5142 | } |
| 5143 | if (!rdev) |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5144 | continue; |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5145 | if (conf->disks[i].replacement && |
| 5146 | conf->reshape_progress != MaxSector) { |
| 5147 | /* replacements and reshape simply do not mix. */ |
| 5148 | printk(KERN_ERR "md: cannot handle concurrent " |
| 5149 | "replacement and reshape.\n"); |
| 5150 | goto abort; |
| 5151 | } |
NeilBrown | 2f11588 | 2010-06-17 17:41:03 +1000 | [diff] [blame] | 5152 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5153 | working_disks++; |
NeilBrown | 2f11588 | 2010-06-17 17:41:03 +1000 | [diff] [blame] | 5154 | continue; |
| 5155 | } |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5156 | /* This disc is not fully in-sync. However if it |
| 5157 | * just stored parity (beyond the recovery_offset), |
| 5158 | * when we don't need to be concerned about the |
| 5159 | * array being dirty. |
| 5160 | * When reshape goes 'backwards', we never have |
| 5161 | * partially completed devices, so we only need |
| 5162 | * to worry about reshape going forwards. |
| 5163 | */ |
| 5164 | /* Hack because v0.91 doesn't store recovery_offset properly. */ |
| 5165 | if (mddev->major_version == 0 && |
| 5166 | mddev->minor_version > 90) |
| 5167 | rdev->recovery_offset = reshape_offset; |
| 5168 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5169 | if (rdev->recovery_offset < reshape_offset) { |
| 5170 | /* We need to check old and new layout */ |
| 5171 | if (!only_parity(rdev->raid_disk, |
| 5172 | conf->algorithm, |
| 5173 | conf->raid_disks, |
| 5174 | conf->max_degraded)) |
| 5175 | continue; |
| 5176 | } |
| 5177 | if (!only_parity(rdev->raid_disk, |
| 5178 | conf->prev_algo, |
| 5179 | conf->previous_raid_disks, |
| 5180 | conf->max_degraded)) |
| 5181 | continue; |
| 5182 | dirty_parity_disks++; |
| 5183 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5184 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5185 | /* |
| 5186 | * 0 for a fully functional array, 1 or 2 for a degraded array. |
| 5187 | */ |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 5188 | mddev->degraded = calc_degraded(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5189 | |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 5190 | if (has_failed(conf)) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5191 | printk(KERN_ERR "md/raid:%s: not enough operational devices" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5192 | " (%d/%d failed)\n", |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 5193 | mdname(mddev), mddev->degraded, conf->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5194 | goto abort; |
| 5195 | } |
| 5196 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5197 | /* device size must be a multiple of chunk size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5198 | mddev->dev_sectors &= ~(mddev->chunk_sectors - 1); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5199 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 5200 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5201 | if (mddev->degraded > dirty_parity_disks && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5202 | mddev->recovery_cp != MaxSector) { |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5203 | if (mddev->ok_start_degraded) |
| 5204 | printk(KERN_WARNING |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5205 | "md/raid:%s: starting dirty degraded array" |
| 5206 | " - data corruption possible.\n", |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5207 | mdname(mddev)); |
| 5208 | else { |
| 5209 | printk(KERN_ERR |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5210 | "md/raid:%s: cannot start dirty degraded array.\n", |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5211 | mdname(mddev)); |
| 5212 | goto abort; |
| 5213 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5214 | } |
| 5215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5216 | if (mddev->degraded == 0) |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5217 | printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d" |
| 5218 | " devices, algorithm %d\n", mdname(mddev), conf->level, |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 5219 | mddev->raid_disks-mddev->degraded, mddev->raid_disks, |
| 5220 | mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5221 | else |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5222 | printk(KERN_ALERT "md/raid:%s: raid level %d active with %d" |
| 5223 | " out of %d devices, algorithm %d\n", |
| 5224 | mdname(mddev), conf->level, |
| 5225 | mddev->raid_disks - mddev->degraded, |
| 5226 | mddev->raid_disks, mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5227 | |
| 5228 | print_raid5_conf(conf); |
| 5229 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5230 | if (conf->reshape_progress != MaxSector) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5231 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5232 | atomic_set(&conf->reshape_stripes, 0); |
| 5233 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 5234 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 5235 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 5236 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 5237 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 5238 | "reshape"); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5239 | } |
| 5240 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5241 | |
| 5242 | /* Ok, everything is just fine now */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 5243 | if (mddev->to_remove == &raid5_attrs_group) |
| 5244 | mddev->to_remove = NULL; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5245 | else if (mddev->kobj.sd && |
| 5246 | sysfs_create_group(&mddev->kobj, &raid5_attrs_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5247 | printk(KERN_WARNING |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 5248 | "raid5: failed to create sysfs attributes for %s\n", |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5249 | mdname(mddev)); |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 5250 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
| 5251 | |
| 5252 | if (mddev->queue) { |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 5253 | int chunk_size; |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 5254 | /* read-ahead size must cover two whole stripes, which |
| 5255 | * is 2 * (datadisks) * chunksize where 'n' is the |
| 5256 | * number of raid devices |
| 5257 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5258 | int data_disks = conf->previous_raid_disks - conf->max_degraded; |
| 5259 | int stripe = data_disks * |
| 5260 | ((mddev->chunk_sectors << 9) / PAGE_SIZE); |
| 5261 | if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 5262 | mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 5263 | |
| 5264 | blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec); |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 5265 | |
| 5266 | mddev->queue->backing_dev_info.congested_data = mddev; |
| 5267 | mddev->queue->backing_dev_info.congested_fn = raid5_congested; |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 5268 | |
| 5269 | chunk_size = mddev->chunk_sectors << 9; |
| 5270 | blk_queue_io_min(mddev->queue, chunk_size); |
| 5271 | blk_queue_io_opt(mddev->queue, chunk_size * |
| 5272 | (conf->raid_disks - conf->max_degraded)); |
| 5273 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5274 | rdev_for_each(rdev, mddev) { |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 5275 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 5276 | rdev->data_offset << 9); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5277 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 5278 | rdev->new_data_offset << 9); |
| 5279 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5280 | } |
| 5281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5282 | return 0; |
| 5283 | abort: |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 5284 | md_unregister_thread(&mddev->thread); |
NeilBrown | e4f869d | 2011-10-07 14:22:49 +1100 | [diff] [blame] | 5285 | print_raid5_conf(conf); |
| 5286 | free_conf(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5287 | mddev->private = NULL; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5288 | 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] | 5289 | return -EIO; |
| 5290 | } |
| 5291 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5292 | static int stop(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5293 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5294 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5295 | |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 5296 | md_unregister_thread(&mddev->thread); |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 5297 | if (mddev->queue) |
| 5298 | mddev->queue->backing_dev_info.congested_fn = NULL; |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 5299 | free_conf(conf); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 5300 | mddev->private = NULL; |
| 5301 | mddev->to_remove = &raid5_attrs_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5302 | return 0; |
| 5303 | } |
| 5304 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5305 | static void status(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5306 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5307 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5308 | int i; |
| 5309 | |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5310 | seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level, |
| 5311 | mddev->chunk_sectors / 2, mddev->layout); |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 5312 | 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] | 5313 | for (i = 0; i < conf->raid_disks; i++) |
| 5314 | seq_printf (seq, "%s", |
| 5315 | conf->disks[i].rdev && |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5316 | test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5317 | seq_printf (seq, "]"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5318 | } |
| 5319 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5320 | static void print_raid5_conf (struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5321 | { |
| 5322 | int i; |
| 5323 | struct disk_info *tmp; |
| 5324 | |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5325 | printk(KERN_DEBUG "RAID conf printout:\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5326 | if (!conf) { |
| 5327 | printk("(conf==NULL)\n"); |
| 5328 | return; |
| 5329 | } |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5330 | printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level, |
| 5331 | conf->raid_disks, |
| 5332 | conf->raid_disks - conf->mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5333 | |
| 5334 | for (i = 0; i < conf->raid_disks; i++) { |
| 5335 | char b[BDEVNAME_SIZE]; |
| 5336 | tmp = conf->disks + i; |
| 5337 | if (tmp->rdev) |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5338 | printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n", |
| 5339 | i, !test_bit(Faulty, &tmp->rdev->flags), |
| 5340 | bdevname(tmp->rdev->bdev, b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5341 | } |
| 5342 | } |
| 5343 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5344 | static int raid5_spare_active(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5345 | { |
| 5346 | int i; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5347 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5348 | struct disk_info *tmp; |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5349 | int count = 0; |
| 5350 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5351 | |
| 5352 | for (i = 0; i < conf->raid_disks; i++) { |
| 5353 | tmp = conf->disks + i; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5354 | if (tmp->replacement |
| 5355 | && tmp->replacement->recovery_offset == MaxSector |
| 5356 | && !test_bit(Faulty, &tmp->replacement->flags) |
| 5357 | && !test_and_set_bit(In_sync, &tmp->replacement->flags)) { |
| 5358 | /* Replacement has just become active. */ |
| 5359 | if (!tmp->rdev |
| 5360 | || !test_and_clear_bit(In_sync, &tmp->rdev->flags)) |
| 5361 | count++; |
| 5362 | if (tmp->rdev) { |
| 5363 | /* Replaced device not technically faulty, |
| 5364 | * but we need to be sure it gets removed |
| 5365 | * and never re-added. |
| 5366 | */ |
| 5367 | set_bit(Faulty, &tmp->rdev->flags); |
| 5368 | sysfs_notify_dirent_safe( |
| 5369 | tmp->rdev->sysfs_state); |
| 5370 | } |
| 5371 | sysfs_notify_dirent_safe(tmp->replacement->sysfs_state); |
| 5372 | } else if (tmp->rdev |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 5373 | && tmp->rdev->recovery_offset == MaxSector |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5374 | && !test_bit(Faulty, &tmp->rdev->flags) |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 5375 | && !test_and_set_bit(In_sync, &tmp->rdev->flags)) { |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5376 | count++; |
Jonathan Brassow | 43c73ca | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5377 | sysfs_notify_dirent_safe(tmp->rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5378 | } |
| 5379 | } |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5380 | spin_lock_irqsave(&conf->device_lock, flags); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 5381 | mddev->degraded = calc_degraded(conf); |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5382 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5383 | print_raid5_conf(conf); |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5384 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5385 | } |
| 5386 | |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 5387 | static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5388 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5389 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5390 | int err = 0; |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 5391 | int number = rdev->raid_disk; |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5392 | struct md_rdev **rdevp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5393 | struct disk_info *p = conf->disks + number; |
| 5394 | |
| 5395 | print_raid5_conf(conf); |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5396 | if (rdev == p->rdev) |
| 5397 | rdevp = &p->rdev; |
| 5398 | else if (rdev == p->replacement) |
| 5399 | rdevp = &p->replacement; |
| 5400 | else |
| 5401 | return 0; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5402 | |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5403 | if (number >= conf->raid_disks && |
| 5404 | conf->reshape_progress == MaxSector) |
| 5405 | clear_bit(In_sync, &rdev->flags); |
| 5406 | |
| 5407 | if (test_bit(In_sync, &rdev->flags) || |
| 5408 | atomic_read(&rdev->nr_pending)) { |
| 5409 | err = -EBUSY; |
| 5410 | goto abort; |
| 5411 | } |
| 5412 | /* Only remove non-faulty devices if recovery |
| 5413 | * isn't possible. |
| 5414 | */ |
| 5415 | if (!test_bit(Faulty, &rdev->flags) && |
| 5416 | mddev->recovery_disabled != conf->recovery_disabled && |
| 5417 | !has_failed(conf) && |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5418 | (!p->replacement || p->replacement == rdev) && |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5419 | number < conf->raid_disks) { |
| 5420 | err = -EBUSY; |
| 5421 | goto abort; |
| 5422 | } |
| 5423 | *rdevp = NULL; |
| 5424 | synchronize_rcu(); |
| 5425 | if (atomic_read(&rdev->nr_pending)) { |
| 5426 | /* lost the race, try later */ |
| 5427 | err = -EBUSY; |
| 5428 | *rdevp = rdev; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5429 | } else if (p->replacement) { |
| 5430 | /* We must have just cleared 'rdev' */ |
| 5431 | p->rdev = p->replacement; |
| 5432 | clear_bit(Replacement, &p->replacement->flags); |
| 5433 | smp_mb(); /* Make sure other CPUs may see both as identical |
| 5434 | * but will never see neither - if they are careful |
| 5435 | */ |
| 5436 | p->replacement = NULL; |
| 5437 | clear_bit(WantReplacement, &rdev->flags); |
| 5438 | } else |
| 5439 | /* We might have just removed the Replacement as faulty- |
| 5440 | * clear the bit just in case |
| 5441 | */ |
| 5442 | clear_bit(WantReplacement, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5443 | abort: |
| 5444 | |
| 5445 | print_raid5_conf(conf); |
| 5446 | return err; |
| 5447 | } |
| 5448 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5449 | static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5450 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5451 | struct r5conf *conf = mddev->private; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5452 | int err = -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5453 | int disk; |
| 5454 | struct disk_info *p; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5455 | int first = 0; |
| 5456 | int last = conf->raid_disks - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5457 | |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 5458 | if (mddev->recovery_disabled == conf->recovery_disabled) |
| 5459 | return -EBUSY; |
| 5460 | |
NeilBrown | dc10c64 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 5461 | if (rdev->saved_raid_disk < 0 && has_failed(conf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5462 | /* no point adding a device */ |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5463 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5464 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5465 | if (rdev->raid_disk >= 0) |
| 5466 | first = last = rdev->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5467 | |
| 5468 | /* |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5469 | * find the disk ... but prefer rdev->saved_raid_disk |
| 5470 | * if possible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5471 | */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5472 | if (rdev->saved_raid_disk >= 0 && |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5473 | rdev->saved_raid_disk >= first && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5474 | conf->disks[rdev->saved_raid_disk].rdev == NULL) |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 5475 | first = rdev->saved_raid_disk; |
| 5476 | |
| 5477 | for (disk = first; disk <= last; disk++) { |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5478 | p = conf->disks + disk; |
| 5479 | if (p->rdev == NULL) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5480 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5481 | rdev->raid_disk = disk; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5482 | err = 0; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5483 | if (rdev->saved_raid_disk != disk) |
| 5484 | conf->fullsync = 1; |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 5485 | rcu_assign_pointer(p->rdev, rdev); |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 5486 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5487 | } |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 5488 | } |
| 5489 | for (disk = first; disk <= last; disk++) { |
| 5490 | p = conf->disks + disk; |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5491 | if (test_bit(WantReplacement, &p->rdev->flags) && |
| 5492 | p->replacement == NULL) { |
| 5493 | clear_bit(In_sync, &rdev->flags); |
| 5494 | set_bit(Replacement, &rdev->flags); |
| 5495 | rdev->raid_disk = disk; |
| 5496 | err = 0; |
| 5497 | conf->fullsync = 1; |
| 5498 | rcu_assign_pointer(p->replacement, rdev); |
| 5499 | break; |
| 5500 | } |
| 5501 | } |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 5502 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5503 | print_raid5_conf(conf); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5504 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5505 | } |
| 5506 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5507 | static int raid5_resize(struct mddev *mddev, sector_t sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5508 | { |
| 5509 | /* no resync is happening, and there is enough space |
| 5510 | * on all devices, so we can resize. |
| 5511 | * We need to make sure resync covers any new space. |
| 5512 | * If the array is shrinking we should possibly wait until |
| 5513 | * any io in the removed space completes, but it hardly seems |
| 5514 | * worth it. |
| 5515 | */ |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 5516 | sector_t newsize; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5517 | sectors &= ~((sector_t)mddev->chunk_sectors - 1); |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 5518 | newsize = raid5_size(mddev, sectors, mddev->raid_disks); |
| 5519 | if (mddev->external_size && |
| 5520 | mddev->array_sectors > newsize) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5521 | return -EINVAL; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 5522 | if (mddev->bitmap) { |
| 5523 | int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0); |
| 5524 | if (ret) |
| 5525 | return ret; |
| 5526 | } |
| 5527 | md_set_array_sectors(mddev, newsize); |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 5528 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5529 | revalidate_disk(mddev->gendisk); |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 5530 | if (sectors > mddev->dev_sectors && |
| 5531 | mddev->recovery_cp > mddev->dev_sectors) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5532 | mddev->recovery_cp = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5533 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5534 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5535 | mddev->dev_sectors = sectors; |
NeilBrown | 4b5c7ae | 2005-07-27 11:43:28 -0700 | [diff] [blame] | 5536 | mddev->resync_max_sectors = sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5537 | return 0; |
| 5538 | } |
| 5539 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5540 | static int check_stripe_cache(struct mddev *mddev) |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5541 | { |
| 5542 | /* Can only proceed if there are plenty of stripe_heads. |
| 5543 | * We need a minimum of one full stripe,, and for sensible progress |
| 5544 | * it is best to have about 4 times that. |
| 5545 | * If we require 4 times, then the default 256 4K stripe_heads will |
| 5546 | * allow for chunk sizes up to 256K, which is probably OK. |
| 5547 | * If the chunk size is greater, user-space should request more |
| 5548 | * stripe_heads first. |
| 5549 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5550 | struct r5conf *conf = mddev->private; |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5551 | if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4 |
| 5552 | > conf->max_nr_stripes || |
| 5553 | ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4 |
| 5554 | > conf->max_nr_stripes) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5555 | printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n", |
| 5556 | mdname(mddev), |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5557 | ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9) |
| 5558 | / STRIPE_SIZE)*4); |
| 5559 | return 0; |
| 5560 | } |
| 5561 | return 1; |
| 5562 | } |
| 5563 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5564 | static int check_reshape(struct mddev *mddev) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5565 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5566 | struct r5conf *conf = mddev->private; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5567 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5568 | if (mddev->delta_disks == 0 && |
| 5569 | mddev->new_layout == mddev->layout && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5570 | mddev->new_chunk_sectors == mddev->chunk_sectors) |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5571 | return 0; /* nothing to do */ |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 5572 | if (has_failed(conf)) |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5573 | return -EINVAL; |
| 5574 | if (mddev->delta_disks < 0) { |
| 5575 | /* We might be able to shrink, but the devices must |
| 5576 | * be made bigger first. |
| 5577 | * For raid6, 4 is the minimum size. |
| 5578 | * Otherwise 2 is the minimum |
| 5579 | */ |
| 5580 | int min = 2; |
| 5581 | if (mddev->level == 6) |
| 5582 | min = 4; |
| 5583 | if (mddev->raid_disks + mddev->delta_disks < min) |
| 5584 | return -EINVAL; |
| 5585 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5586 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5587 | if (!check_stripe_cache(mddev)) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5588 | return -ENOSPC; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5589 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5590 | return resize_stripes(conf, conf->raid_disks + mddev->delta_disks); |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5591 | } |
| 5592 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5593 | static int raid5_start_reshape(struct mddev *mddev) |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5594 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5595 | struct r5conf *conf = mddev->private; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5596 | struct md_rdev *rdev; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5597 | int spares = 0; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 5598 | unsigned long flags; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5599 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5600 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5601 | return -EBUSY; |
| 5602 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5603 | if (!check_stripe_cache(mddev)) |
| 5604 | return -ENOSPC; |
| 5605 | |
NeilBrown | 30b6764 | 2012-05-22 13:55:28 +1000 | [diff] [blame] | 5606 | if (has_failed(conf)) |
| 5607 | return -EINVAL; |
| 5608 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5609 | rdev_for_each(rdev, mddev) { |
NeilBrown | 469518a | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5610 | if (!test_bit(In_sync, &rdev->flags) |
| 5611 | && !test_bit(Faulty, &rdev->flags)) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5612 | spares++; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5613 | } |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5614 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5615 | if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5616 | /* Not enough devices even to make a degraded array |
| 5617 | * of that size |
| 5618 | */ |
| 5619 | return -EINVAL; |
| 5620 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5621 | /* Refuse to reduce size of the array. Any reductions in |
| 5622 | * array size must be through explicit setting of array_size |
| 5623 | * attribute. |
| 5624 | */ |
| 5625 | if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks) |
| 5626 | < mddev->array_sectors) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5627 | printk(KERN_ERR "md/raid:%s: array size must be reduced " |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5628 | "before number of disks\n", mdname(mddev)); |
| 5629 | return -EINVAL; |
| 5630 | } |
| 5631 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5632 | atomic_set(&conf->reshape_stripes, 0); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5633 | spin_lock_irq(&conf->device_lock); |
| 5634 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5635 | conf->raid_disks += mddev->delta_disks; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5636 | conf->prev_chunk_sectors = conf->chunk_sectors; |
| 5637 | conf->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5638 | conf->prev_algo = conf->algorithm; |
| 5639 | conf->algorithm = mddev->new_layout; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5640 | conf->generation++; |
| 5641 | /* Code that selects data_offset needs to see the generation update |
| 5642 | * if reshape_progress has been set - so a memory barrier needed. |
| 5643 | */ |
| 5644 | smp_mb(); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5645 | if (mddev->reshape_backwards) |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5646 | conf->reshape_progress = raid5_size(mddev, 0, 0); |
| 5647 | else |
| 5648 | conf->reshape_progress = 0; |
| 5649 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5650 | spin_unlock_irq(&conf->device_lock); |
| 5651 | |
| 5652 | /* Add some new drives, as many as will fit. |
| 5653 | * We know there are enough to make the newly sized array work. |
NeilBrown | 3424bf6 | 2010-06-17 17:48:26 +1000 | [diff] [blame] | 5654 | * Don't add devices if we are reducing the number of |
| 5655 | * devices in the array. This is because it is not possible |
| 5656 | * to correctly record the "partially reconstructed" state of |
| 5657 | * such devices during the reshape and confusion could result. |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5658 | */ |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5659 | if (mddev->delta_disks >= 0) { |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5660 | rdev_for_each(rdev, mddev) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5661 | if (rdev->raid_disk < 0 && |
| 5662 | !test_bit(Faulty, &rdev->flags)) { |
| 5663 | if (raid5_add_disk(mddev, rdev) == 0) { |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5664 | if (rdev->raid_disk |
NeilBrown | 9d4c7d8 | 2012-03-13 11:21:21 +1100 | [diff] [blame] | 5665 | >= conf->previous_raid_disks) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5666 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 9d4c7d8 | 2012-03-13 11:21:21 +1100 | [diff] [blame] | 5667 | else |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5668 | rdev->recovery_offset = 0; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5669 | |
| 5670 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5671 | /* Failure here is OK */; |
NeilBrown | 50da084 | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5672 | } |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5673 | } else if (rdev->raid_disk >= conf->previous_raid_disks |
| 5674 | && !test_bit(Faulty, &rdev->flags)) { |
| 5675 | /* This is a spare that was manually added */ |
| 5676 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5677 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5678 | |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5679 | /* When a reshape changes the number of devices, |
| 5680 | * ->degraded is measured against the larger of the |
| 5681 | * pre and post number of devices. |
| 5682 | */ |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5683 | spin_lock_irqsave(&conf->device_lock, flags); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 5684 | mddev->degraded = calc_degraded(conf); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5685 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 5686 | } |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5687 | mddev->raid_disks = conf->raid_disks; |
NeilBrown | e516402 | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 5688 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5689 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5690 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5691 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 5692 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 5693 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 5694 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 5695 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 5696 | "reshape"); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5697 | if (!mddev->sync_thread) { |
| 5698 | mddev->recovery = 0; |
| 5699 | spin_lock_irq(&conf->device_lock); |
| 5700 | mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5701 | rdev_for_each(rdev, mddev) |
| 5702 | rdev->new_data_offset = rdev->data_offset; |
| 5703 | smp_wmb(); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5704 | conf->reshape_progress = MaxSector; |
NeilBrown | 1e3fa9b | 2012-03-13 11:21:18 +1100 | [diff] [blame] | 5705 | mddev->reshape_position = MaxSector; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5706 | spin_unlock_irq(&conf->device_lock); |
| 5707 | return -EAGAIN; |
| 5708 | } |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5709 | conf->reshape_checkpoint = jiffies; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5710 | md_wakeup_thread(mddev->sync_thread); |
| 5711 | md_new_event(mddev); |
| 5712 | return 0; |
| 5713 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5714 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5715 | /* This is called from the reshape thread and should make any |
| 5716 | * changes needed in 'conf' |
| 5717 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5718 | static void end_reshape(struct r5conf *conf) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5719 | { |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5720 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5721 | if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) { |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5722 | struct md_rdev *rdev; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 5723 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5724 | spin_lock_irq(&conf->device_lock); |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5725 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5726 | rdev_for_each(rdev, conf->mddev) |
| 5727 | rdev->data_offset = rdev->new_data_offset; |
| 5728 | smp_wmb(); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5729 | conf->reshape_progress = MaxSector; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5730 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 5731 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5732 | |
| 5733 | /* read-ahead size must cover two whole stripes, which is |
| 5734 | * 2 * (datadisks) * chunksize where 'n' is the number of raid devices |
| 5735 | */ |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 5736 | if (conf->mddev->queue) { |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5737 | int data_disks = conf->raid_disks - conf->max_degraded; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5738 | int stripe = data_disks * ((conf->chunk_sectors << 9) |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5739 | / PAGE_SIZE); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5740 | if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 5741 | conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
| 5742 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5743 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5744 | } |
| 5745 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5746 | /* This is called from the raid5d thread with mddev_lock held. |
| 5747 | * It makes config changes to the device. |
| 5748 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5749 | static void raid5_finish_reshape(struct mddev *mddev) |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5750 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5751 | struct r5conf *conf = mddev->private; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5752 | |
| 5753 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 5754 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5755 | if (mddev->delta_disks > 0) { |
| 5756 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
| 5757 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5758 | revalidate_disk(mddev->gendisk); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5759 | } else { |
| 5760 | int d; |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 5761 | spin_lock_irq(&conf->device_lock); |
| 5762 | mddev->degraded = calc_degraded(conf); |
| 5763 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5764 | for (d = conf->raid_disks ; |
| 5765 | d < conf->raid_disks - mddev->delta_disks; |
NeilBrown | 1a67dde | 2009-08-13 10:41:49 +1000 | [diff] [blame] | 5766 | d++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5767 | struct md_rdev *rdev = conf->disks[d].rdev; |
NeilBrown | da7613b | 2012-05-22 13:55:33 +1000 | [diff] [blame] | 5768 | if (rdev) |
| 5769 | clear_bit(In_sync, &rdev->flags); |
| 5770 | rdev = conf->disks[d].replacement; |
| 5771 | if (rdev) |
| 5772 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 1a67dde | 2009-08-13 10:41:49 +1000 | [diff] [blame] | 5773 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5774 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5775 | mddev->layout = conf->algorithm; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5776 | mddev->chunk_sectors = conf->chunk_sectors; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5777 | mddev->reshape_position = MaxSector; |
| 5778 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5779 | mddev->reshape_backwards = 0; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5780 | } |
| 5781 | } |
| 5782 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5783 | static void raid5_quiesce(struct mddev *mddev, int state) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5784 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5785 | struct r5conf *conf = mddev->private; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5786 | |
| 5787 | switch(state) { |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5788 | case 2: /* resume for a suspend */ |
| 5789 | wake_up(&conf->wait_for_overlap); |
| 5790 | break; |
| 5791 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5792 | case 1: /* stop all writes */ |
| 5793 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5794 | /* '2' tells resync/reshape to pause so that all |
| 5795 | * active stripes can drain |
| 5796 | */ |
| 5797 | conf->quiesce = 2; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5798 | wait_event_lock_irq(conf->wait_for_stripe, |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5799 | atomic_read(&conf->active_stripes) == 0 && |
| 5800 | atomic_read(&conf->active_aligned_reads) == 0, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5801 | conf->device_lock, /* nothing */); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5802 | conf->quiesce = 1; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5803 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5804 | /* allow reshape to continue */ |
| 5805 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5806 | break; |
| 5807 | |
| 5808 | case 0: /* re-enable writes */ |
| 5809 | spin_lock_irq(&conf->device_lock); |
| 5810 | conf->quiesce = 0; |
| 5811 | wake_up(&conf->wait_for_stripe); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5812 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5813 | spin_unlock_irq(&conf->device_lock); |
| 5814 | break; |
| 5815 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5816 | } |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5817 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5818 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5819 | static void *raid45_takeover_raid0(struct mddev *mddev, int level) |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 5820 | { |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 5821 | struct r0conf *raid0_conf = mddev->private; |
Randy Dunlap | d76c842 | 2011-04-21 09:07:26 -0700 | [diff] [blame] | 5822 | sector_t sectors; |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 5823 | |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5824 | /* for raid0 takeover only one zone is supported */ |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 5825 | if (raid0_conf->nr_strip_zones > 1) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5826 | printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n", |
| 5827 | mdname(mddev)); |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5828 | return ERR_PTR(-EINVAL); |
| 5829 | } |
| 5830 | |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 5831 | sectors = raid0_conf->strip_zone[0].zone_end; |
| 5832 | sector_div(sectors, raid0_conf->strip_zone[0].nb_dev); |
NeilBrown | 3b71bd9 | 2011-04-20 15:38:18 +1000 | [diff] [blame] | 5833 | mddev->dev_sectors = sectors; |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5834 | mddev->new_level = level; |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 5835 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 5836 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
| 5837 | mddev->raid_disks += 1; |
| 5838 | mddev->delta_disks = 1; |
| 5839 | /* make sure it will be not marked as dirty */ |
| 5840 | mddev->recovery_cp = MaxSector; |
| 5841 | |
| 5842 | return setup_conf(mddev); |
| 5843 | } |
| 5844 | |
| 5845 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5846 | static void *raid5_takeover_raid1(struct mddev *mddev) |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5847 | { |
| 5848 | int chunksect; |
| 5849 | |
| 5850 | if (mddev->raid_disks != 2 || |
| 5851 | mddev->degraded > 1) |
| 5852 | return ERR_PTR(-EINVAL); |
| 5853 | |
| 5854 | /* Should check if there are write-behind devices? */ |
| 5855 | |
| 5856 | chunksect = 64*2; /* 64K by default */ |
| 5857 | |
| 5858 | /* The array must be an exact multiple of chunksize */ |
| 5859 | while (chunksect && (mddev->array_sectors & (chunksect-1))) |
| 5860 | chunksect >>= 1; |
| 5861 | |
| 5862 | if ((chunksect<<9) < STRIPE_SIZE) |
| 5863 | /* array size does not allow a suitable chunk size */ |
| 5864 | return ERR_PTR(-EINVAL); |
| 5865 | |
| 5866 | mddev->new_level = 5; |
| 5867 | mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5868 | mddev->new_chunk_sectors = chunksect; |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5869 | |
| 5870 | return setup_conf(mddev); |
| 5871 | } |
| 5872 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5873 | static void *raid5_takeover_raid6(struct mddev *mddev) |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 5874 | { |
| 5875 | int new_layout; |
| 5876 | |
| 5877 | switch (mddev->layout) { |
| 5878 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 5879 | new_layout = ALGORITHM_LEFT_ASYMMETRIC; |
| 5880 | break; |
| 5881 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 5882 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC; |
| 5883 | break; |
| 5884 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 5885 | new_layout = ALGORITHM_LEFT_SYMMETRIC; |
| 5886 | break; |
| 5887 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 5888 | new_layout = ALGORITHM_RIGHT_SYMMETRIC; |
| 5889 | break; |
| 5890 | case ALGORITHM_PARITY_0_6: |
| 5891 | new_layout = ALGORITHM_PARITY_0; |
| 5892 | break; |
| 5893 | case ALGORITHM_PARITY_N: |
| 5894 | new_layout = ALGORITHM_PARITY_N; |
| 5895 | break; |
| 5896 | default: |
| 5897 | return ERR_PTR(-EINVAL); |
| 5898 | } |
| 5899 | mddev->new_level = 5; |
| 5900 | mddev->new_layout = new_layout; |
| 5901 | mddev->delta_disks = -1; |
| 5902 | mddev->raid_disks -= 1; |
| 5903 | return setup_conf(mddev); |
| 5904 | } |
| 5905 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5906 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5907 | static int raid5_check_reshape(struct mddev *mddev) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5908 | { |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5909 | /* For a 2-drive array, the layout and chunk size can be changed |
| 5910 | * immediately as not restriping is needed. |
| 5911 | * For larger arrays we record the new value - after validation |
| 5912 | * to be used by a reshape pass. |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5913 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5914 | struct r5conf *conf = mddev->private; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5915 | int new_chunk = mddev->new_chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5916 | |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5917 | if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5918 | return -EINVAL; |
| 5919 | if (new_chunk > 0) { |
Andre Noll | 0ba459d | 2009-06-18 08:46:10 +1000 | [diff] [blame] | 5920 | if (!is_power_of_2(new_chunk)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5921 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5922 | if (new_chunk < (PAGE_SIZE>>9)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5923 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5924 | if (mddev->array_sectors & (new_chunk-1)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5925 | /* not factor of array size */ |
| 5926 | return -EINVAL; |
| 5927 | } |
| 5928 | |
| 5929 | /* They look valid */ |
| 5930 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5931 | if (mddev->raid_disks == 2) { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5932 | /* can make the change immediately */ |
| 5933 | if (mddev->new_layout >= 0) { |
| 5934 | conf->algorithm = mddev->new_layout; |
| 5935 | mddev->layout = mddev->new_layout; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5936 | } |
| 5937 | if (new_chunk > 0) { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5938 | conf->chunk_sectors = new_chunk ; |
| 5939 | mddev->chunk_sectors = new_chunk; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5940 | } |
| 5941 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 5942 | md_wakeup_thread(mddev->thread); |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5943 | } |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5944 | return check_reshape(mddev); |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5945 | } |
| 5946 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5947 | static int raid6_check_reshape(struct mddev *mddev) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5948 | { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5949 | int new_chunk = mddev->new_chunk_sectors; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5950 | |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5951 | if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5952 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5953 | if (new_chunk > 0) { |
Andre Noll | 0ba459d | 2009-06-18 08:46:10 +1000 | [diff] [blame] | 5954 | if (!is_power_of_2(new_chunk)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5955 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5956 | if (new_chunk < (PAGE_SIZE >> 9)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5957 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5958 | if (mddev->array_sectors & (new_chunk-1)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5959 | /* not factor of array size */ |
| 5960 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5961 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5962 | |
| 5963 | /* They look valid */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5964 | return check_reshape(mddev); |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5965 | } |
| 5966 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5967 | static void *raid5_takeover(struct mddev *mddev) |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5968 | { |
| 5969 | /* raid5 can take over: |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5970 | * raid0 - if there is only one strip zone - make it a raid4 layout |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5971 | * raid1 - if there are two drives. We need to know the chunk size |
| 5972 | * raid4 - trivial - just use a raid4 layout. |
| 5973 | * raid6 - Providing it is a *_6 layout |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5974 | */ |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5975 | if (mddev->level == 0) |
| 5976 | return raid45_takeover_raid0(mddev, 5); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5977 | if (mddev->level == 1) |
| 5978 | return raid5_takeover_raid1(mddev); |
NeilBrown | e9d4758 | 2009-03-31 14:57:09 +1100 | [diff] [blame] | 5979 | if (mddev->level == 4) { |
| 5980 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 5981 | mddev->new_level = 5; |
| 5982 | return setup_conf(mddev); |
| 5983 | } |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 5984 | if (mddev->level == 6) |
| 5985 | return raid5_takeover_raid6(mddev); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5986 | |
| 5987 | return ERR_PTR(-EINVAL); |
| 5988 | } |
| 5989 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5990 | static void *raid4_takeover(struct mddev *mddev) |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 5991 | { |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5992 | /* raid4 can take over: |
| 5993 | * raid0 - if there is only one strip zone |
| 5994 | * raid5 - if layout is right |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 5995 | */ |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5996 | if (mddev->level == 0) |
| 5997 | return raid45_takeover_raid0(mddev, 4); |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 5998 | if (mddev->level == 5 && |
| 5999 | mddev->layout == ALGORITHM_PARITY_N) { |
| 6000 | mddev->new_layout = 0; |
| 6001 | mddev->new_level = 4; |
| 6002 | return setup_conf(mddev); |
| 6003 | } |
| 6004 | return ERR_PTR(-EINVAL); |
| 6005 | } |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6006 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6007 | static struct md_personality raid5_personality; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6008 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6009 | static void *raid6_takeover(struct mddev *mddev) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6010 | { |
| 6011 | /* Currently can only take over a raid5. We map the |
| 6012 | * personality to an equivalent raid6 personality |
| 6013 | * with the Q block at the end. |
| 6014 | */ |
| 6015 | int new_layout; |
| 6016 | |
| 6017 | if (mddev->pers != &raid5_personality) |
| 6018 | return ERR_PTR(-EINVAL); |
| 6019 | if (mddev->degraded > 1) |
| 6020 | return ERR_PTR(-EINVAL); |
| 6021 | if (mddev->raid_disks > 253) |
| 6022 | return ERR_PTR(-EINVAL); |
| 6023 | if (mddev->raid_disks < 3) |
| 6024 | return ERR_PTR(-EINVAL); |
| 6025 | |
| 6026 | switch (mddev->layout) { |
| 6027 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 6028 | new_layout = ALGORITHM_LEFT_ASYMMETRIC_6; |
| 6029 | break; |
| 6030 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 6031 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6; |
| 6032 | break; |
| 6033 | case ALGORITHM_LEFT_SYMMETRIC: |
| 6034 | new_layout = ALGORITHM_LEFT_SYMMETRIC_6; |
| 6035 | break; |
| 6036 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 6037 | new_layout = ALGORITHM_RIGHT_SYMMETRIC_6; |
| 6038 | break; |
| 6039 | case ALGORITHM_PARITY_0: |
| 6040 | new_layout = ALGORITHM_PARITY_0_6; |
| 6041 | break; |
| 6042 | case ALGORITHM_PARITY_N: |
| 6043 | new_layout = ALGORITHM_PARITY_N; |
| 6044 | break; |
| 6045 | default: |
| 6046 | return ERR_PTR(-EINVAL); |
| 6047 | } |
| 6048 | mddev->new_level = 6; |
| 6049 | mddev->new_layout = new_layout; |
| 6050 | mddev->delta_disks = 1; |
| 6051 | mddev->raid_disks += 1; |
| 6052 | return setup_conf(mddev); |
| 6053 | } |
| 6054 | |
| 6055 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6056 | static struct md_personality raid6_personality = |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6057 | { |
| 6058 | .name = "raid6", |
| 6059 | .level = 6, |
| 6060 | .owner = THIS_MODULE, |
| 6061 | .make_request = make_request, |
| 6062 | .run = run, |
| 6063 | .stop = stop, |
| 6064 | .status = status, |
| 6065 | .error_handler = error, |
| 6066 | .hot_add_disk = raid5_add_disk, |
| 6067 | .hot_remove_disk= raid5_remove_disk, |
| 6068 | .spare_active = raid5_spare_active, |
| 6069 | .sync_request = sync_request, |
| 6070 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6071 | .size = raid5_size, |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6072 | .check_reshape = raid6_check_reshape, |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 6073 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6074 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6075 | .quiesce = raid5_quiesce, |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6076 | .takeover = raid6_takeover, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6077 | }; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6078 | static struct md_personality raid5_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6079 | { |
| 6080 | .name = "raid5", |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6081 | .level = 5, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6082 | .owner = THIS_MODULE, |
| 6083 | .make_request = make_request, |
| 6084 | .run = run, |
| 6085 | .stop = stop, |
| 6086 | .status = status, |
| 6087 | .error_handler = error, |
| 6088 | .hot_add_disk = raid5_add_disk, |
| 6089 | .hot_remove_disk= raid5_remove_disk, |
| 6090 | .spare_active = raid5_spare_active, |
| 6091 | .sync_request = sync_request, |
| 6092 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6093 | .size = raid5_size, |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6094 | .check_reshape = raid5_check_reshape, |
| 6095 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6096 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6097 | .quiesce = raid5_quiesce, |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6098 | .takeover = raid5_takeover, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6099 | }; |
| 6100 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6101 | static struct md_personality raid4_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6102 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6103 | .name = "raid4", |
| 6104 | .level = 4, |
| 6105 | .owner = THIS_MODULE, |
| 6106 | .make_request = make_request, |
| 6107 | .run = run, |
| 6108 | .stop = stop, |
| 6109 | .status = status, |
| 6110 | .error_handler = error, |
| 6111 | .hot_add_disk = raid5_add_disk, |
| 6112 | .hot_remove_disk= raid5_remove_disk, |
| 6113 | .spare_active = raid5_spare_active, |
| 6114 | .sync_request = sync_request, |
| 6115 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6116 | .size = raid5_size, |
NeilBrown | 3d37890 | 2007-03-26 21:32:13 -0800 | [diff] [blame] | 6117 | .check_reshape = raid5_check_reshape, |
| 6118 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6119 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6120 | .quiesce = raid5_quiesce, |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 6121 | .takeover = raid4_takeover, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6122 | }; |
| 6123 | |
| 6124 | static int __init raid5_init(void) |
| 6125 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6126 | register_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6127 | register_md_personality(&raid5_personality); |
| 6128 | register_md_personality(&raid4_personality); |
| 6129 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6130 | } |
| 6131 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6132 | static void raid5_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6133 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6134 | unregister_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6135 | unregister_md_personality(&raid5_personality); |
| 6136 | unregister_md_personality(&raid4_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6137 | } |
| 6138 | |
| 6139 | module_init(raid5_init); |
| 6140 | module_exit(raid5_exit); |
| 6141 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 6142 | MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6143 | MODULE_ALIAS("md-personality-4"); /* RAID5 */ |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 6144 | MODULE_ALIAS("md-raid5"); |
| 6145 | MODULE_ALIAS("md-raid4"); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6146 | MODULE_ALIAS("md-level-5"); |
| 6147 | MODULE_ALIAS("md-level-4"); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6148 | MODULE_ALIAS("md-personality-8"); /* RAID6 */ |
| 6149 | MODULE_ALIAS("md-raid6"); |
| 6150 | MODULE_ALIAS("md-level-6"); |
| 6151 | |
| 6152 | /* This used to be two separate modules, they were: */ |
| 6153 | MODULE_ALIAS("raid5"); |
| 6154 | MODULE_ALIAS("raid6"); |