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> |
Dan Williams | 07a3b41 | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 50 | #include <linux/async.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 51 | #include <linux/seq_file.h> |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 52 | #include <linux/cpu.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 53 | #include <linux/slab.h> |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 54 | #include <linux/ratelimit.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 55 | #include "md.h" |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 56 | #include "raid5.h" |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 57 | #include "raid0.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 58 | #include "bitmap.h" |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | /* |
| 61 | * Stripe cache |
| 62 | */ |
| 63 | |
| 64 | #define NR_STRIPES 256 |
| 65 | #define STRIPE_SIZE PAGE_SIZE |
| 66 | #define STRIPE_SHIFT (PAGE_SHIFT - 9) |
| 67 | #define STRIPE_SECTORS (STRIPE_SIZE>>9) |
| 68 | #define IO_THRESHOLD 1 |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 69 | #define BYPASS_THRESHOLD 1 |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 70 | #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | #define HASH_MASK (NR_HASH - 1) |
| 72 | |
NeilBrown | db298e1 | 2011-10-07 14:23:00 +1100 | [diff] [blame^] | 73 | static inline struct hlist_head *stripe_hash(raid5_conf_t *conf, sector_t sect) |
| 74 | { |
| 75 | int hash = (sect >> STRIPE_SHIFT) & HASH_MASK; |
| 76 | return &conf->stripe_hashtbl[hash]; |
| 77 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | /* bio's attached to a stripe+device for I/O are linked together in bi_sector |
| 80 | * order without overlap. There may be several bio's per stripe+device, and |
| 81 | * a bio could span several devices. |
| 82 | * When walking this list for a particular stripe+device, we must never proceed |
| 83 | * beyond a bio that extends past this device, as the next bio might no longer |
| 84 | * be valid. |
NeilBrown | db298e1 | 2011-10-07 14:23:00 +1100 | [diff] [blame^] | 85 | * 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] | 86 | * of the current stripe+device |
| 87 | */ |
NeilBrown | db298e1 | 2011-10-07 14:23:00 +1100 | [diff] [blame^] | 88 | static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector) |
| 89 | { |
| 90 | int sectors = bio->bi_size >> 9; |
| 91 | if (bio->bi_sector + sectors < sector + STRIPE_SECTORS) |
| 92 | return bio->bi_next; |
| 93 | else |
| 94 | return NULL; |
| 95 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | /* |
| 97 | * The following can be used to debug the driver |
| 98 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | #define RAID5_PARANOIA 1 |
| 100 | #if RAID5_PARANOIA && defined(CONFIG_SMP) |
| 101 | # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock) |
| 102 | #else |
| 103 | # define CHECK_DEVLOCK() |
| 104 | #endif |
| 105 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 106 | #ifdef DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | #define inline |
| 108 | #define __inline__ |
| 109 | #endif |
| 110 | |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 111 | /* |
Jens Axboe | 5b99c2f | 2008-08-15 10:56:11 +0200 | [diff] [blame] | 112 | * We maintain a biased count of active stripes in the bottom 16 bits of |
| 113 | * 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] | 114 | */ |
| 115 | static inline int raid5_bi_phys_segments(struct bio *bio) |
| 116 | { |
Jens Axboe | 5b99c2f | 2008-08-15 10:56:11 +0200 | [diff] [blame] | 117 | return bio->bi_phys_segments & 0xffff; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static inline int raid5_bi_hw_segments(struct bio *bio) |
| 121 | { |
Jens Axboe | 5b99c2f | 2008-08-15 10:56:11 +0200 | [diff] [blame] | 122 | return (bio->bi_phys_segments >> 16) & 0xffff; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static inline int raid5_dec_bi_phys_segments(struct bio *bio) |
| 126 | { |
| 127 | --bio->bi_phys_segments; |
| 128 | return raid5_bi_phys_segments(bio); |
| 129 | } |
| 130 | |
| 131 | static inline int raid5_dec_bi_hw_segments(struct bio *bio) |
| 132 | { |
| 133 | unsigned short val = raid5_bi_hw_segments(bio); |
| 134 | |
| 135 | --val; |
Jens Axboe | 5b99c2f | 2008-08-15 10:56:11 +0200 | [diff] [blame] | 136 | bio->bi_phys_segments = (val << 16) | raid5_bi_phys_segments(bio); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 137 | return val; |
| 138 | } |
| 139 | |
| 140 | static inline void raid5_set_bi_hw_segments(struct bio *bio, unsigned int cnt) |
| 141 | { |
Namhyung Kim | 9b2dc8b | 2011-06-13 14:48:22 +0900 | [diff] [blame] | 142 | bio->bi_phys_segments = raid5_bi_phys_segments(bio) | (cnt << 16); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 143 | } |
| 144 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 145 | /* Find first data disk in a raid6 stripe */ |
| 146 | static inline int raid6_d0(struct stripe_head *sh) |
| 147 | { |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 148 | if (sh->ddf_layout) |
| 149 | /* ddf always start from first device */ |
| 150 | return 0; |
| 151 | /* md starts just after Q block */ |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 152 | if (sh->qd_idx == sh->disks - 1) |
| 153 | return 0; |
| 154 | else |
| 155 | return sh->qd_idx + 1; |
| 156 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 157 | static inline int raid6_next_disk(int disk, int raid_disks) |
| 158 | { |
| 159 | disk++; |
| 160 | return (disk < raid_disks) ? disk : 0; |
| 161 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 162 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 163 | /* When walking through the disks in a raid5, starting at raid6_d0, |
| 164 | * We need to map each disk to a 'slot', where the data disks are slot |
| 165 | * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk |
| 166 | * is raid_disks-1. This help does that mapping. |
| 167 | */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 168 | static int raid6_idx_to_slot(int idx, struct stripe_head *sh, |
| 169 | int *count, int syndrome_disks) |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 170 | { |
Dan Williams | 6629542 | 2009-10-19 18:09:32 -0700 | [diff] [blame] | 171 | int slot = *count; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 172 | |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 173 | if (sh->ddf_layout) |
Dan Williams | 6629542 | 2009-10-19 18:09:32 -0700 | [diff] [blame] | 174 | (*count)++; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 175 | if (idx == sh->pd_idx) |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 176 | return syndrome_disks; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 177 | if (idx == sh->qd_idx) |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 178 | return syndrome_disks + 1; |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 179 | if (!sh->ddf_layout) |
Dan Williams | 6629542 | 2009-10-19 18:09:32 -0700 | [diff] [blame] | 180 | (*count)++; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 181 | return slot; |
| 182 | } |
| 183 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 184 | static void return_io(struct bio *return_bi) |
| 185 | { |
| 186 | struct bio *bi = return_bi; |
| 187 | while (bi) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 188 | |
| 189 | return_bi = bi->bi_next; |
| 190 | bi->bi_next = NULL; |
| 191 | bi->bi_size = 0; |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 192 | bio_endio(bi, 0); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 193 | bi = return_bi; |
| 194 | } |
| 195 | } |
| 196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | static void print_raid5_conf (raid5_conf_t *conf); |
| 198 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 199 | static int stripe_operations_active(struct stripe_head *sh) |
| 200 | { |
| 201 | return sh->check_state || sh->reconstruct_state || |
| 202 | test_bit(STRIPE_BIOFILL_RUN, &sh->state) || |
| 203 | test_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 204 | } |
| 205 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 206 | static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | { |
| 208 | if (atomic_dec_and_test(&sh->count)) { |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 209 | BUG_ON(!list_empty(&sh->lru)); |
| 210 | BUG_ON(atomic_read(&conf->active_stripes)==0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | if (test_bit(STRIPE_HANDLE, &sh->state)) { |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 212 | if (test_bit(STRIPE_DELAYED, &sh->state)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | list_add_tail(&sh->lru, &conf->delayed_list); |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 214 | else if (test_bit(STRIPE_BIT_DELAY, &sh->state) && |
| 215 | sh->bm_seq - conf->seq_write > 0) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 216 | list_add_tail(&sh->lru, &conf->bitmap_list); |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 217 | else { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 218 | clear_bit(STRIPE_BIT_DELAY, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | list_add_tail(&sh->lru, &conf->handle_list); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 220 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | md_wakeup_thread(conf->mddev->thread); |
| 222 | } else { |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 223 | BUG_ON(stripe_operations_active(sh)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
| 225 | atomic_dec(&conf->preread_active_stripes); |
| 226 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) |
| 227 | md_wakeup_thread(conf->mddev->thread); |
| 228 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | atomic_dec(&conf->active_stripes); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 230 | if (!test_bit(STRIPE_EXPANDING, &sh->state)) { |
| 231 | list_add_tail(&sh->lru, &conf->inactive_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | wake_up(&conf->wait_for_stripe); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 233 | if (conf->retry_read_aligned) |
| 234 | md_wakeup_thread(conf->mddev->thread); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 235 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | } |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 239 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | static void release_stripe(struct stripe_head *sh) |
| 241 | { |
| 242 | raid5_conf_t *conf = sh->raid_conf; |
| 243 | unsigned long flags; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | spin_lock_irqsave(&conf->device_lock, flags); |
| 246 | __release_stripe(conf, sh); |
| 247 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 248 | } |
| 249 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 250 | static inline void remove_hash(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 252 | pr_debug("remove_hash(), stripe %llu\n", |
| 253 | (unsigned long long)sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 255 | hlist_del_init(&sh->hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | } |
| 257 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 258 | static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | { |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 260 | struct hlist_head *hp = stripe_hash(conf, sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 262 | pr_debug("insert_hash(), stripe %llu\n", |
| 263 | (unsigned long long)sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | |
| 265 | CHECK_DEVLOCK(); |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 266 | hlist_add_head(&sh->hash, hp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | |
| 270 | /* find an idle stripe, make sure it is unhashed, and return it. */ |
| 271 | static struct stripe_head *get_free_stripe(raid5_conf_t *conf) |
| 272 | { |
| 273 | struct stripe_head *sh = NULL; |
| 274 | struct list_head *first; |
| 275 | |
| 276 | CHECK_DEVLOCK(); |
| 277 | if (list_empty(&conf->inactive_list)) |
| 278 | goto out; |
| 279 | first = conf->inactive_list.next; |
| 280 | sh = list_entry(first, struct stripe_head, lru); |
| 281 | list_del_init(first); |
| 282 | remove_hash(sh); |
| 283 | atomic_inc(&conf->active_stripes); |
| 284 | out: |
| 285 | return sh; |
| 286 | } |
| 287 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 288 | static void shrink_buffers(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | { |
| 290 | struct page *p; |
| 291 | int i; |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 292 | int num = sh->raid_conf->pool_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 294 | for (i = 0; i < num ; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | p = sh->dev[i].page; |
| 296 | if (!p) |
| 297 | continue; |
| 298 | sh->dev[i].page = NULL; |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 299 | put_page(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 303 | static int grow_buffers(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | { |
| 305 | int i; |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 306 | int num = sh->raid_conf->pool_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 308 | for (i = 0; i < num; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | struct page *page; |
| 310 | |
| 311 | if (!(page = alloc_page(GFP_KERNEL))) { |
| 312 | return 1; |
| 313 | } |
| 314 | sh->dev[i].page = page; |
| 315 | } |
| 316 | return 0; |
| 317 | } |
| 318 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 319 | static void raid5_build_block(struct stripe_head *sh, int i, int previous); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 320 | static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous, |
| 321 | struct stripe_head *sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 323 | 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] | 324 | { |
| 325 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 326 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 328 | BUG_ON(atomic_read(&sh->count) != 0); |
| 329 | BUG_ON(test_bit(STRIPE_HANDLE, &sh->state)); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 330 | BUG_ON(stripe_operations_active(sh)); |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | CHECK_DEVLOCK(); |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 333 | pr_debug("init_stripe called, stripe %llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | (unsigned long long)sh->sector); |
| 335 | |
| 336 | remove_hash(sh); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 337 | |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 338 | sh->generation = conf->generation - previous; |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 339 | sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | sh->sector = sector; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 341 | stripe_set_idx(sector, conf, previous, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | sh->state = 0; |
| 343 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 344 | |
| 345 | for (i = sh->disks; i--; ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | struct r5dev *dev = &sh->dev[i]; |
| 347 | |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 348 | if (dev->toread || dev->read || dev->towrite || dev->written || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | test_bit(R5_LOCKED, &dev->flags)) { |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 350 | 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] | 351 | (unsigned long long)sh->sector, i, dev->toread, |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 352 | dev->read, dev->towrite, dev->written, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | test_bit(R5_LOCKED, &dev->flags)); |
NeilBrown | 8cfa7b0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 354 | WARN_ON(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | } |
| 356 | dev->flags = 0; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 357 | raid5_build_block(sh, i, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | } |
| 359 | insert_hash(conf, sh); |
| 360 | } |
| 361 | |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 362 | static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector, |
| 363 | short generation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | { |
| 365 | struct stripe_head *sh; |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 366 | struct hlist_node *hn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | |
| 368 | CHECK_DEVLOCK(); |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 369 | pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector); |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 370 | hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash) |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 371 | if (sh->sector == sector && sh->generation == generation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | return sh; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 373 | pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | return NULL; |
| 375 | } |
| 376 | |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 377 | /* |
| 378 | * Need to check if array has failed when deciding whether to: |
| 379 | * - start an array |
| 380 | * - remove non-faulty devices |
| 381 | * - add a spare |
| 382 | * - allow a reshape |
| 383 | * This determination is simple when no reshape is happening. |
| 384 | * However if there is a reshape, we need to carefully check |
| 385 | * both the before and after sections. |
| 386 | * This is because some failed devices may only affect one |
| 387 | * of the two sections, and some non-in_sync devices may |
| 388 | * be insync in the section most affected by failed devices. |
| 389 | */ |
| 390 | static int has_failed(raid5_conf_t *conf) |
| 391 | { |
| 392 | int degraded; |
| 393 | int i; |
| 394 | if (conf->mddev->reshape_position == MaxSector) |
| 395 | return conf->mddev->degraded > conf->max_degraded; |
| 396 | |
| 397 | rcu_read_lock(); |
| 398 | degraded = 0; |
| 399 | for (i = 0; i < conf->previous_raid_disks; i++) { |
| 400 | mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev); |
| 401 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
| 402 | degraded++; |
| 403 | else if (test_bit(In_sync, &rdev->flags)) |
| 404 | ; |
| 405 | else |
| 406 | /* not in-sync or faulty. |
| 407 | * If the reshape increases the number of devices, |
| 408 | * this is being recovered by the reshape, so |
| 409 | * this 'previous' section is not in_sync. |
| 410 | * If the number of devices is being reduced however, |
| 411 | * the device can only be part of the array if |
| 412 | * we are reverting a reshape, so this section will |
| 413 | * be in-sync. |
| 414 | */ |
| 415 | if (conf->raid_disks >= conf->previous_raid_disks) |
| 416 | degraded++; |
| 417 | } |
| 418 | rcu_read_unlock(); |
| 419 | if (degraded > conf->max_degraded) |
| 420 | return 1; |
| 421 | rcu_read_lock(); |
| 422 | degraded = 0; |
| 423 | for (i = 0; i < conf->raid_disks; i++) { |
| 424 | mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev); |
| 425 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
| 426 | degraded++; |
| 427 | else if (test_bit(In_sync, &rdev->flags)) |
| 428 | ; |
| 429 | else |
| 430 | /* not in-sync or faulty. |
| 431 | * If reshape increases the number of devices, this |
| 432 | * section has already been recovered, else it |
| 433 | * almost certainly hasn't. |
| 434 | */ |
| 435 | if (conf->raid_disks <= conf->previous_raid_disks) |
| 436 | degraded++; |
| 437 | } |
| 438 | rcu_read_unlock(); |
| 439 | if (degraded > conf->max_degraded) |
| 440 | return 1; |
| 441 | return 0; |
| 442 | } |
| 443 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 444 | static struct stripe_head * |
| 445 | get_active_stripe(raid5_conf_t *conf, sector_t sector, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 446 | int previous, int noblock, int noquiesce) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | { |
| 448 | struct stripe_head *sh; |
| 449 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 450 | pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
| 452 | spin_lock_irq(&conf->device_lock); |
| 453 | |
| 454 | do { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 455 | wait_event_lock_irq(conf->wait_for_stripe, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 456 | conf->quiesce == 0 || noquiesce, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 457 | conf->device_lock, /* nothing */); |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 458 | sh = __find_stripe(conf, sector, conf->generation - previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | if (!sh) { |
| 460 | if (!conf->inactive_blocked) |
| 461 | sh = get_free_stripe(conf); |
| 462 | if (noblock && sh == NULL) |
| 463 | break; |
| 464 | if (!sh) { |
| 465 | conf->inactive_blocked = 1; |
| 466 | wait_event_lock_irq(conf->wait_for_stripe, |
| 467 | !list_empty(&conf->inactive_list) && |
NeilBrown | 5036805 | 2005-12-12 02:39:17 -0800 | [diff] [blame] | 468 | (atomic_read(&conf->active_stripes) |
| 469 | < (conf->max_nr_stripes *3/4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | || !conf->inactive_blocked), |
| 471 | conf->device_lock, |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 472 | ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | conf->inactive_blocked = 0; |
| 474 | } else |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 475 | init_stripe(sh, sector, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | } else { |
| 477 | if (atomic_read(&sh->count)) { |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 478 | BUG_ON(!list_empty(&sh->lru) |
| 479 | && !test_bit(STRIPE_EXPANDING, &sh->state)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | } else { |
| 481 | if (!test_bit(STRIPE_HANDLE, &sh->state)) |
| 482 | atomic_inc(&conf->active_stripes); |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 483 | if (list_empty(&sh->lru) && |
| 484 | !test_bit(STRIPE_EXPANDING, &sh->state)) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 485 | BUG(); |
| 486 | list_del_init(&sh->lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | } while (sh == NULL); |
| 490 | |
| 491 | if (sh) |
| 492 | atomic_inc(&sh->count); |
| 493 | |
| 494 | spin_unlock_irq(&conf->device_lock); |
| 495 | return sh; |
| 496 | } |
| 497 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 498 | static void |
| 499 | raid5_end_read_request(struct bio *bi, int error); |
| 500 | static void |
| 501 | raid5_end_write_request(struct bio *bi, int error); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 502 | |
Dan Williams | c4e5ac0 | 2008-06-28 08:31:53 +1000 | [diff] [blame] | 503 | 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] | 504 | { |
| 505 | raid5_conf_t *conf = sh->raid_conf; |
| 506 | int i, disks = sh->disks; |
| 507 | |
| 508 | might_sleep(); |
| 509 | |
| 510 | for (i = disks; i--; ) { |
| 511 | int rw; |
| 512 | struct bio *bi; |
| 513 | mdk_rdev_t *rdev; |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 514 | if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) { |
| 515 | if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags)) |
| 516 | rw = WRITE_FUA; |
| 517 | else |
| 518 | rw = WRITE; |
| 519 | } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 520 | rw = READ; |
| 521 | else |
| 522 | continue; |
| 523 | |
| 524 | bi = &sh->dev[i].req; |
| 525 | |
| 526 | bi->bi_rw = rw; |
Namhyung Kim | b062962 | 2011-06-14 14:20:19 +1000 | [diff] [blame] | 527 | if (rw & WRITE) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 528 | bi->bi_end_io = raid5_end_write_request; |
| 529 | else |
| 530 | bi->bi_end_io = raid5_end_read_request; |
| 531 | |
| 532 | rcu_read_lock(); |
| 533 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 534 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 535 | rdev = NULL; |
| 536 | if (rdev) |
| 537 | atomic_inc(&rdev->nr_pending); |
| 538 | rcu_read_unlock(); |
| 539 | |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 540 | /* We have already checked bad blocks for reads. Now |
| 541 | * need to check for writes. |
| 542 | */ |
| 543 | while ((rw & WRITE) && rdev && |
| 544 | test_bit(WriteErrorSeen, &rdev->flags)) { |
| 545 | sector_t first_bad; |
| 546 | int bad_sectors; |
| 547 | int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 548 | &first_bad, &bad_sectors); |
| 549 | if (!bad) |
| 550 | break; |
| 551 | |
| 552 | if (bad < 0) { |
| 553 | set_bit(BlockedBadBlocks, &rdev->flags); |
| 554 | if (!conf->mddev->external && |
| 555 | conf->mddev->flags) { |
| 556 | /* It is very unlikely, but we might |
| 557 | * still need to write out the |
| 558 | * bad block log - better give it |
| 559 | * a chance*/ |
| 560 | md_check_recovery(conf->mddev); |
| 561 | } |
| 562 | md_wait_for_blocked_rdev(rdev, conf->mddev); |
| 563 | } else { |
| 564 | /* Acknowledged bad block - skip the write */ |
| 565 | rdev_dec_pending(rdev, conf->mddev); |
| 566 | rdev = NULL; |
| 567 | } |
| 568 | } |
| 569 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 570 | if (rdev) { |
Dan Williams | c4e5ac0 | 2008-06-28 08:31:53 +1000 | [diff] [blame] | 571 | if (s->syncing || s->expanding || s->expanded) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 572 | md_sync_acct(rdev->bdev, STRIPE_SECTORS); |
| 573 | |
Dan Williams | 2b7497f | 2008-06-28 08:31:52 +1000 | [diff] [blame] | 574 | set_bit(STRIPE_IO_STARTED, &sh->state); |
| 575 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 576 | bi->bi_bdev = rdev->bdev; |
| 577 | pr_debug("%s: for %llu schedule op %ld on disc %d\n", |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 578 | __func__, (unsigned long long)sh->sector, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 579 | bi->bi_rw, i); |
| 580 | atomic_inc(&sh->count); |
| 581 | bi->bi_sector = sh->sector + rdev->data_offset; |
| 582 | bi->bi_flags = 1 << BIO_UPTODATE; |
| 583 | bi->bi_vcnt = 1; |
| 584 | bi->bi_max_vecs = 1; |
| 585 | bi->bi_idx = 0; |
| 586 | bi->bi_io_vec = &sh->dev[i].vec; |
| 587 | bi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
| 588 | bi->bi_io_vec[0].bv_offset = 0; |
| 589 | bi->bi_size = STRIPE_SIZE; |
| 590 | bi->bi_next = NULL; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 591 | generic_make_request(bi); |
| 592 | } else { |
Namhyung Kim | b062962 | 2011-06-14 14:20:19 +1000 | [diff] [blame] | 593 | if (rw & WRITE) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 594 | set_bit(STRIPE_DEGRADED, &sh->state); |
| 595 | pr_debug("skip op %ld on disc %d for sector %llu\n", |
| 596 | bi->bi_rw, i, (unsigned long long)sh->sector); |
| 597 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 598 | set_bit(STRIPE_HANDLE, &sh->state); |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | static struct dma_async_tx_descriptor * |
| 604 | async_copy_data(int frombio, struct bio *bio, struct page *page, |
| 605 | sector_t sector, struct dma_async_tx_descriptor *tx) |
| 606 | { |
| 607 | struct bio_vec *bvl; |
| 608 | struct page *bio_page; |
| 609 | int i; |
| 610 | int page_offset; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 611 | struct async_submit_ctl submit; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 612 | enum async_tx_flags flags = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 613 | |
| 614 | if (bio->bi_sector >= sector) |
| 615 | page_offset = (signed)(bio->bi_sector - sector) * 512; |
| 616 | else |
| 617 | page_offset = (signed)(sector - bio->bi_sector) * -512; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 618 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 619 | if (frombio) |
| 620 | flags |= ASYNC_TX_FENCE; |
| 621 | init_async_submit(&submit, flags, tx, NULL, NULL, NULL); |
| 622 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 623 | bio_for_each_segment(bvl, bio, i) { |
Namhyung Kim | fcde907 | 2011-06-14 14:23:57 +1000 | [diff] [blame] | 624 | int len = bvl->bv_len; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 625 | int clen; |
| 626 | int b_offset = 0; |
| 627 | |
| 628 | if (page_offset < 0) { |
| 629 | b_offset = -page_offset; |
| 630 | page_offset += b_offset; |
| 631 | len -= b_offset; |
| 632 | } |
| 633 | |
| 634 | if (len > 0 && page_offset + len > STRIPE_SIZE) |
| 635 | clen = STRIPE_SIZE - page_offset; |
| 636 | else |
| 637 | clen = len; |
| 638 | |
| 639 | if (clen > 0) { |
Namhyung Kim | fcde907 | 2011-06-14 14:23:57 +1000 | [diff] [blame] | 640 | b_offset += bvl->bv_offset; |
| 641 | bio_page = bvl->bv_page; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 642 | if (frombio) |
| 643 | tx = async_memcpy(page, bio_page, page_offset, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 644 | b_offset, clen, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 645 | else |
| 646 | tx = async_memcpy(bio_page, page, b_offset, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 647 | page_offset, clen, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 648 | } |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 649 | /* chain the operations */ |
| 650 | submit.depend_tx = tx; |
| 651 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 652 | if (clen < len) /* hit end of page */ |
| 653 | break; |
| 654 | page_offset += len; |
| 655 | } |
| 656 | |
| 657 | return tx; |
| 658 | } |
| 659 | |
| 660 | static void ops_complete_biofill(void *stripe_head_ref) |
| 661 | { |
| 662 | struct stripe_head *sh = stripe_head_ref; |
| 663 | struct bio *return_bi = NULL; |
| 664 | raid5_conf_t *conf = sh->raid_conf; |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 665 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 666 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 667 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 668 | (unsigned long long)sh->sector); |
| 669 | |
| 670 | /* clear completed biofills */ |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 671 | spin_lock_irq(&conf->device_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 672 | for (i = sh->disks; i--; ) { |
| 673 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 674 | |
| 675 | /* acknowledge completion of a biofill operation */ |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 676 | /* and check if we need to reply to a read request, |
| 677 | * new R5_Wantfill requests are held off until |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 678 | * !STRIPE_BIOFILL_RUN |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 679 | */ |
| 680 | if (test_and_clear_bit(R5_Wantfill, &dev->flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 681 | struct bio *rbi, *rbi2; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 682 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 683 | BUG_ON(!dev->read); |
| 684 | rbi = dev->read; |
| 685 | dev->read = NULL; |
| 686 | while (rbi && rbi->bi_sector < |
| 687 | dev->sector + STRIPE_SECTORS) { |
| 688 | rbi2 = r5_next_bio(rbi, dev->sector); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 689 | if (!raid5_dec_bi_phys_segments(rbi)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 690 | rbi->bi_next = return_bi; |
| 691 | return_bi = rbi; |
| 692 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 693 | rbi = rbi2; |
| 694 | } |
| 695 | } |
| 696 | } |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 697 | spin_unlock_irq(&conf->device_lock); |
| 698 | clear_bit(STRIPE_BIOFILL_RUN, &sh->state); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 699 | |
| 700 | return_io(return_bi); |
| 701 | |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 702 | set_bit(STRIPE_HANDLE, &sh->state); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 703 | release_stripe(sh); |
| 704 | } |
| 705 | |
| 706 | static void ops_run_biofill(struct stripe_head *sh) |
| 707 | { |
| 708 | struct dma_async_tx_descriptor *tx = NULL; |
| 709 | raid5_conf_t *conf = sh->raid_conf; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 710 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 711 | int i; |
| 712 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 713 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 714 | (unsigned long long)sh->sector); |
| 715 | |
| 716 | for (i = sh->disks; i--; ) { |
| 717 | struct r5dev *dev = &sh->dev[i]; |
| 718 | if (test_bit(R5_Wantfill, &dev->flags)) { |
| 719 | struct bio *rbi; |
| 720 | spin_lock_irq(&conf->device_lock); |
| 721 | dev->read = rbi = dev->toread; |
| 722 | dev->toread = NULL; |
| 723 | spin_unlock_irq(&conf->device_lock); |
| 724 | while (rbi && rbi->bi_sector < |
| 725 | dev->sector + STRIPE_SECTORS) { |
| 726 | tx = async_copy_data(0, rbi, dev->page, |
| 727 | dev->sector, tx); |
| 728 | rbi = r5_next_bio(rbi, dev->sector); |
| 729 | } |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 734 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL); |
| 735 | async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 736 | } |
| 737 | |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 738 | static void mark_target_uptodate(struct stripe_head *sh, int target) |
| 739 | { |
| 740 | struct r5dev *tgt; |
| 741 | |
| 742 | if (target < 0) |
| 743 | return; |
| 744 | |
| 745 | tgt = &sh->dev[target]; |
| 746 | set_bit(R5_UPTODATE, &tgt->flags); |
| 747 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 748 | clear_bit(R5_Wantcompute, &tgt->flags); |
| 749 | } |
| 750 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 751 | static void ops_complete_compute(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 752 | { |
| 753 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 754 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 755 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 756 | (unsigned long long)sh->sector); |
| 757 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 758 | /* mark the computed target(s) as uptodate */ |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 759 | mark_target_uptodate(sh, sh->ops.target); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 760 | mark_target_uptodate(sh, sh->ops.target2); |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 761 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 762 | clear_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 763 | if (sh->check_state == check_state_compute_run) |
| 764 | sh->check_state = check_state_compute_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 765 | set_bit(STRIPE_HANDLE, &sh->state); |
| 766 | release_stripe(sh); |
| 767 | } |
| 768 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 769 | /* return a pointer to the address conversion region of the scribble buffer */ |
| 770 | static addr_conv_t *to_addr_conv(struct stripe_head *sh, |
| 771 | struct raid5_percpu *percpu) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 772 | { |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 773 | return percpu->scribble + sizeof(struct page *) * (sh->disks + 2); |
| 774 | } |
| 775 | |
| 776 | static struct dma_async_tx_descriptor * |
| 777 | ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 778 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 779 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 780 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 781 | int target = sh->ops.target; |
| 782 | struct r5dev *tgt = &sh->dev[target]; |
| 783 | struct page *xor_dest = tgt->page; |
| 784 | int count = 0; |
| 785 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 786 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 787 | int i; |
| 788 | |
| 789 | pr_debug("%s: stripe %llu block: %d\n", |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 790 | __func__, (unsigned long long)sh->sector, target); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 791 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 792 | |
| 793 | for (i = disks; i--; ) |
| 794 | if (i != target) |
| 795 | xor_srcs[count++] = sh->dev[i].page; |
| 796 | |
| 797 | atomic_inc(&sh->count); |
| 798 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 799 | 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] | 800 | ops_complete_compute, sh, to_addr_conv(sh, percpu)); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 801 | if (unlikely(count == 1)) |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 802 | 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] | 803 | else |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 804 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 805 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 806 | return tx; |
| 807 | } |
| 808 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 809 | /* set_syndrome_sources - populate source buffers for gen_syndrome |
| 810 | * @srcs - (struct page *) array of size sh->disks |
| 811 | * @sh - stripe_head to parse |
| 812 | * |
| 813 | * Populates srcs in proper layout order for the stripe and returns the |
| 814 | * 'count' of sources to be used in a call to async_gen_syndrome. The P |
| 815 | * destination buffer is recorded in srcs[count] and the Q destination |
| 816 | * is recorded in srcs[count+1]]. |
| 817 | */ |
| 818 | static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh) |
| 819 | { |
| 820 | int disks = sh->disks; |
| 821 | int syndrome_disks = sh->ddf_layout ? disks : (disks - 2); |
| 822 | int d0_idx = raid6_d0(sh); |
| 823 | int count; |
| 824 | int i; |
| 825 | |
| 826 | for (i = 0; i < disks; i++) |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 827 | srcs[i] = NULL; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 828 | |
| 829 | count = 0; |
| 830 | i = d0_idx; |
| 831 | do { |
| 832 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 833 | |
| 834 | srcs[slot] = sh->dev[i].page; |
| 835 | i = raid6_next_disk(i, disks); |
| 836 | } while (i != d0_idx); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 837 | |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 838 | return syndrome_disks; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | static struct dma_async_tx_descriptor * |
| 842 | ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 843 | { |
| 844 | int disks = sh->disks; |
| 845 | struct page **blocks = percpu->scribble; |
| 846 | int target; |
| 847 | int qd_idx = sh->qd_idx; |
| 848 | struct dma_async_tx_descriptor *tx; |
| 849 | struct async_submit_ctl submit; |
| 850 | struct r5dev *tgt; |
| 851 | struct page *dest; |
| 852 | int i; |
| 853 | int count; |
| 854 | |
| 855 | if (sh->ops.target < 0) |
| 856 | target = sh->ops.target2; |
| 857 | else if (sh->ops.target2 < 0) |
| 858 | target = sh->ops.target; |
| 859 | else |
| 860 | /* we should only have one valid target */ |
| 861 | BUG(); |
| 862 | BUG_ON(target < 0); |
| 863 | pr_debug("%s: stripe %llu block: %d\n", |
| 864 | __func__, (unsigned long long)sh->sector, target); |
| 865 | |
| 866 | tgt = &sh->dev[target]; |
| 867 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 868 | dest = tgt->page; |
| 869 | |
| 870 | atomic_inc(&sh->count); |
| 871 | |
| 872 | if (target == qd_idx) { |
| 873 | count = set_syndrome_sources(blocks, sh); |
| 874 | blocks[count] = NULL; /* regenerating p is not necessary */ |
| 875 | BUG_ON(blocks[count+1] != dest); /* q should already be set */ |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 876 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 877 | ops_complete_compute, sh, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 878 | to_addr_conv(sh, percpu)); |
| 879 | tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
| 880 | } else { |
| 881 | /* Compute any data- or p-drive using XOR */ |
| 882 | count = 0; |
| 883 | for (i = disks; i-- ; ) { |
| 884 | if (i == target || i == qd_idx) |
| 885 | continue; |
| 886 | blocks[count++] = sh->dev[i].page; |
| 887 | } |
| 888 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 889 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
| 890 | NULL, ops_complete_compute, sh, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 891 | to_addr_conv(sh, percpu)); |
| 892 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit); |
| 893 | } |
| 894 | |
| 895 | return tx; |
| 896 | } |
| 897 | |
| 898 | static struct dma_async_tx_descriptor * |
| 899 | ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 900 | { |
| 901 | int i, count, disks = sh->disks; |
| 902 | int syndrome_disks = sh->ddf_layout ? disks : disks-2; |
| 903 | int d0_idx = raid6_d0(sh); |
| 904 | int faila = -1, failb = -1; |
| 905 | int target = sh->ops.target; |
| 906 | int target2 = sh->ops.target2; |
| 907 | struct r5dev *tgt = &sh->dev[target]; |
| 908 | struct r5dev *tgt2 = &sh->dev[target2]; |
| 909 | struct dma_async_tx_descriptor *tx; |
| 910 | struct page **blocks = percpu->scribble; |
| 911 | struct async_submit_ctl submit; |
| 912 | |
| 913 | pr_debug("%s: stripe %llu block1: %d block2: %d\n", |
| 914 | __func__, (unsigned long long)sh->sector, target, target2); |
| 915 | BUG_ON(target < 0 || target2 < 0); |
| 916 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 917 | BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags)); |
| 918 | |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 919 | /* we need to open-code set_syndrome_sources to handle the |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 920 | * slot number conversion for 'faila' and 'failb' |
| 921 | */ |
| 922 | for (i = 0; i < disks ; i++) |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 923 | blocks[i] = NULL; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 924 | count = 0; |
| 925 | i = d0_idx; |
| 926 | do { |
| 927 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 928 | |
| 929 | blocks[slot] = sh->dev[i].page; |
| 930 | |
| 931 | if (i == target) |
| 932 | faila = slot; |
| 933 | if (i == target2) |
| 934 | failb = slot; |
| 935 | i = raid6_next_disk(i, disks); |
| 936 | } while (i != d0_idx); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 937 | |
| 938 | BUG_ON(faila == failb); |
| 939 | if (failb < faila) |
| 940 | swap(faila, failb); |
| 941 | pr_debug("%s: stripe: %llu faila: %d failb: %d\n", |
| 942 | __func__, (unsigned long long)sh->sector, faila, failb); |
| 943 | |
| 944 | atomic_inc(&sh->count); |
| 945 | |
| 946 | if (failb == syndrome_disks+1) { |
| 947 | /* Q disk is one of the missing disks */ |
| 948 | if (faila == syndrome_disks) { |
| 949 | /* Missing P+Q, just recompute */ |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 950 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 951 | ops_complete_compute, sh, |
| 952 | to_addr_conv(sh, percpu)); |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 953 | return async_gen_syndrome(blocks, 0, syndrome_disks+2, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 954 | STRIPE_SIZE, &submit); |
| 955 | } else { |
| 956 | struct page *dest; |
| 957 | int data_target; |
| 958 | int qd_idx = sh->qd_idx; |
| 959 | |
| 960 | /* Missing D+Q: recompute D from P, then recompute Q */ |
| 961 | if (target == qd_idx) |
| 962 | data_target = target2; |
| 963 | else |
| 964 | data_target = target; |
| 965 | |
| 966 | count = 0; |
| 967 | for (i = disks; i-- ; ) { |
| 968 | if (i == data_target || i == qd_idx) |
| 969 | continue; |
| 970 | blocks[count++] = sh->dev[i].page; |
| 971 | } |
| 972 | dest = sh->dev[data_target].page; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 973 | init_async_submit(&submit, |
| 974 | ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
| 975 | NULL, NULL, NULL, |
| 976 | to_addr_conv(sh, percpu)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 977 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, |
| 978 | &submit); |
| 979 | |
| 980 | count = set_syndrome_sources(blocks, sh); |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 981 | init_async_submit(&submit, ASYNC_TX_FENCE, tx, |
| 982 | ops_complete_compute, sh, |
| 983 | to_addr_conv(sh, percpu)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 984 | return async_gen_syndrome(blocks, 0, count+2, |
| 985 | STRIPE_SIZE, &submit); |
| 986 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 987 | } else { |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 988 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 989 | ops_complete_compute, sh, |
| 990 | to_addr_conv(sh, percpu)); |
| 991 | if (failb == syndrome_disks) { |
| 992 | /* We're missing D+P. */ |
| 993 | return async_raid6_datap_recov(syndrome_disks+2, |
| 994 | STRIPE_SIZE, faila, |
| 995 | blocks, &submit); |
| 996 | } else { |
| 997 | /* We're missing D+D. */ |
| 998 | return async_raid6_2data_recov(syndrome_disks+2, |
| 999 | STRIPE_SIZE, faila, failb, |
| 1000 | blocks, &submit); |
| 1001 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1006 | static void ops_complete_prexor(void *stripe_head_ref) |
| 1007 | { |
| 1008 | struct stripe_head *sh = stripe_head_ref; |
| 1009 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1010 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1011 | (unsigned long long)sh->sector); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | static struct dma_async_tx_descriptor * |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1015 | ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1016 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1017 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1018 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1019 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1020 | int count = 0, pd_idx = sh->pd_idx, i; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1021 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1022 | |
| 1023 | /* existing parity data subtracted */ |
| 1024 | struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 1025 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1026 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1027 | (unsigned long long)sh->sector); |
| 1028 | |
| 1029 | for (i = disks; i--; ) { |
| 1030 | struct r5dev *dev = &sh->dev[i]; |
| 1031 | /* Only process blocks that are known to be uptodate */ |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1032 | if (test_bit(R5_Wantdrain, &dev->flags)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1033 | xor_srcs[count++] = dev->page; |
| 1034 | } |
| 1035 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1036 | 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] | 1037 | ops_complete_prexor, sh, to_addr_conv(sh, percpu)); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1038 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1039 | |
| 1040 | return tx; |
| 1041 | } |
| 1042 | |
| 1043 | static struct dma_async_tx_descriptor * |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1044 | 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] | 1045 | { |
| 1046 | int disks = sh->disks; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1047 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1048 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1049 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1050 | (unsigned long long)sh->sector); |
| 1051 | |
| 1052 | for (i = disks; i--; ) { |
| 1053 | struct r5dev *dev = &sh->dev[i]; |
| 1054 | struct bio *chosen; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1055 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1056 | if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1057 | struct bio *wbi; |
| 1058 | |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 1059 | spin_lock_irq(&sh->raid_conf->device_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1060 | chosen = dev->towrite; |
| 1061 | dev->towrite = NULL; |
| 1062 | BUG_ON(dev->written); |
| 1063 | wbi = dev->written = chosen; |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 1064 | spin_unlock_irq(&sh->raid_conf->device_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1065 | |
| 1066 | while (wbi && wbi->bi_sector < |
| 1067 | dev->sector + STRIPE_SECTORS) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1068 | if (wbi->bi_rw & REQ_FUA) |
| 1069 | set_bit(R5_WantFUA, &dev->flags); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1070 | tx = async_copy_data(1, wbi, dev->page, |
| 1071 | dev->sector, tx); |
| 1072 | wbi = r5_next_bio(wbi, dev->sector); |
| 1073 | } |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | return tx; |
| 1078 | } |
| 1079 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1080 | static void ops_complete_reconstruct(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1081 | { |
| 1082 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1083 | int disks = sh->disks; |
| 1084 | int pd_idx = sh->pd_idx; |
| 1085 | int qd_idx = sh->qd_idx; |
| 1086 | int i; |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1087 | bool fua = false; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1088 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1089 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1090 | (unsigned long long)sh->sector); |
| 1091 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1092 | for (i = disks; i--; ) |
| 1093 | fua |= test_bit(R5_WantFUA, &sh->dev[i].flags); |
| 1094 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1095 | for (i = disks; i--; ) { |
| 1096 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1097 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1098 | if (dev->written || i == pd_idx || i == qd_idx) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1099 | set_bit(R5_UPTODATE, &dev->flags); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1100 | if (fua) |
| 1101 | set_bit(R5_WantFUA, &dev->flags); |
| 1102 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1105 | if (sh->reconstruct_state == reconstruct_state_drain_run) |
| 1106 | sh->reconstruct_state = reconstruct_state_drain_result; |
| 1107 | else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) |
| 1108 | sh->reconstruct_state = reconstruct_state_prexor_drain_result; |
| 1109 | else { |
| 1110 | BUG_ON(sh->reconstruct_state != reconstruct_state_run); |
| 1111 | sh->reconstruct_state = reconstruct_state_result; |
| 1112 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1113 | |
| 1114 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1115 | release_stripe(sh); |
| 1116 | } |
| 1117 | |
| 1118 | static void |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1119 | ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1120 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1121 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1122 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1123 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1124 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1125 | int count = 0, pd_idx = sh->pd_idx, i; |
| 1126 | struct page *xor_dest; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1127 | int prexor = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1128 | unsigned long flags; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1129 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1130 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1131 | (unsigned long long)sh->sector); |
| 1132 | |
| 1133 | /* check if prexor is active which means only process blocks |
| 1134 | * that are part of a read-modify-write (written) |
| 1135 | */ |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1136 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) { |
| 1137 | prexor = 1; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1138 | xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 1139 | for (i = disks; i--; ) { |
| 1140 | struct r5dev *dev = &sh->dev[i]; |
| 1141 | if (dev->written) |
| 1142 | xor_srcs[count++] = dev->page; |
| 1143 | } |
| 1144 | } else { |
| 1145 | xor_dest = sh->dev[pd_idx].page; |
| 1146 | for (i = disks; i--; ) { |
| 1147 | struct r5dev *dev = &sh->dev[i]; |
| 1148 | if (i != pd_idx) |
| 1149 | xor_srcs[count++] = dev->page; |
| 1150 | } |
| 1151 | } |
| 1152 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1153 | /* 1/ if we prexor'd then the dest is reused as a source |
| 1154 | * 2/ if we did not prexor then we are redoing the parity |
| 1155 | * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST |
| 1156 | * for the synchronous xor case |
| 1157 | */ |
Dan Williams | 88ba2aa | 2009-04-09 16:16:18 -0700 | [diff] [blame] | 1158 | flags = ASYNC_TX_ACK | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1159 | (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST); |
| 1160 | |
| 1161 | atomic_inc(&sh->count); |
| 1162 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1163 | init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh, |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1164 | to_addr_conv(sh, percpu)); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1165 | if (unlikely(count == 1)) |
| 1166 | tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit); |
| 1167 | else |
| 1168 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1169 | } |
| 1170 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1171 | static void |
| 1172 | ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1173 | struct dma_async_tx_descriptor *tx) |
| 1174 | { |
| 1175 | struct async_submit_ctl submit; |
| 1176 | struct page **blocks = percpu->scribble; |
| 1177 | int count; |
| 1178 | |
| 1179 | pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector); |
| 1180 | |
| 1181 | count = set_syndrome_sources(blocks, sh); |
| 1182 | |
| 1183 | atomic_inc(&sh->count); |
| 1184 | |
| 1185 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct, |
| 1186 | sh, to_addr_conv(sh, percpu)); |
| 1187 | async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | static void ops_complete_check(void *stripe_head_ref) |
| 1191 | { |
| 1192 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1193 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1194 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1195 | (unsigned long long)sh->sector); |
| 1196 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 1197 | sh->check_state = check_state_check_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1198 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1199 | release_stripe(sh); |
| 1200 | } |
| 1201 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1202 | 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] | 1203 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1204 | int disks = sh->disks; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1205 | int pd_idx = sh->pd_idx; |
| 1206 | int qd_idx = sh->qd_idx; |
| 1207 | struct page *xor_dest; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1208 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1209 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1210 | struct async_submit_ctl submit; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1211 | int count; |
| 1212 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1213 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1214 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1215 | (unsigned long long)sh->sector); |
| 1216 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1217 | count = 0; |
| 1218 | xor_dest = sh->dev[pd_idx].page; |
| 1219 | xor_srcs[count++] = xor_dest; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1220 | for (i = disks; i--; ) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1221 | if (i == pd_idx || i == qd_idx) |
| 1222 | continue; |
| 1223 | xor_srcs[count++] = sh->dev[i].page; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1224 | } |
| 1225 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1226 | init_async_submit(&submit, 0, NULL, NULL, NULL, |
| 1227 | to_addr_conv(sh, percpu)); |
Dan Williams | 099f53c | 2009-04-08 14:28:37 -0700 | [diff] [blame] | 1228 | tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1229 | &sh->ops.zero_sum_result, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1230 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1231 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1232 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL); |
| 1233 | tx = async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1234 | } |
| 1235 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1236 | static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp) |
| 1237 | { |
| 1238 | struct page **srcs = percpu->scribble; |
| 1239 | struct async_submit_ctl submit; |
| 1240 | int count; |
| 1241 | |
| 1242 | pr_debug("%s: stripe %llu checkp: %d\n", __func__, |
| 1243 | (unsigned long long)sh->sector, checkp); |
| 1244 | |
| 1245 | count = set_syndrome_sources(srcs, sh); |
| 1246 | if (!checkp) |
| 1247 | srcs[count] = NULL; |
| 1248 | |
| 1249 | atomic_inc(&sh->count); |
| 1250 | init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check, |
| 1251 | sh, to_addr_conv(sh, percpu)); |
| 1252 | async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE, |
| 1253 | &sh->ops.zero_sum_result, percpu->spare_page, &submit); |
| 1254 | } |
| 1255 | |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 1256 | 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] | 1257 | { |
| 1258 | int overlap_clear = 0, i, disks = sh->disks; |
| 1259 | struct dma_async_tx_descriptor *tx = NULL; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1260 | raid5_conf_t *conf = sh->raid_conf; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1261 | int level = conf->level; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1262 | struct raid5_percpu *percpu; |
| 1263 | unsigned long cpu; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1264 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1265 | cpu = get_cpu(); |
| 1266 | percpu = per_cpu_ptr(conf->percpu, cpu); |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 1267 | if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1268 | ops_run_biofill(sh); |
| 1269 | overlap_clear++; |
| 1270 | } |
| 1271 | |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 1272 | if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1273 | if (level < 6) |
| 1274 | tx = ops_run_compute5(sh, percpu); |
| 1275 | else { |
| 1276 | if (sh->ops.target2 < 0 || sh->ops.target < 0) |
| 1277 | tx = ops_run_compute6_1(sh, percpu); |
| 1278 | else |
| 1279 | tx = ops_run_compute6_2(sh, percpu); |
| 1280 | } |
| 1281 | /* terminate the chain if reconstruct is not set to be run */ |
| 1282 | if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 1283 | async_tx_ack(tx); |
| 1284 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1285 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1286 | if (test_bit(STRIPE_OP_PREXOR, &ops_request)) |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1287 | tx = ops_run_prexor(sh, percpu, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1288 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1289 | if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) { |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1290 | tx = ops_run_biodrain(sh, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1291 | overlap_clear++; |
| 1292 | } |
| 1293 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1294 | if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) { |
| 1295 | if (level < 6) |
| 1296 | ops_run_reconstruct5(sh, percpu, tx); |
| 1297 | else |
| 1298 | ops_run_reconstruct6(sh, percpu, tx); |
| 1299 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1300 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1301 | if (test_bit(STRIPE_OP_CHECK, &ops_request)) { |
| 1302 | if (sh->check_state == check_state_run) |
| 1303 | ops_run_check_p(sh, percpu); |
| 1304 | else if (sh->check_state == check_state_run_q) |
| 1305 | ops_run_check_pq(sh, percpu, 0); |
| 1306 | else if (sh->check_state == check_state_run_pq) |
| 1307 | ops_run_check_pq(sh, percpu, 1); |
| 1308 | else |
| 1309 | BUG(); |
| 1310 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1311 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1312 | if (overlap_clear) |
| 1313 | for (i = disks; i--; ) { |
| 1314 | struct r5dev *dev = &sh->dev[i]; |
| 1315 | if (test_and_clear_bit(R5_Overlap, &dev->flags)) |
| 1316 | wake_up(&sh->raid_conf->wait_for_overlap); |
| 1317 | } |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1318 | put_cpu(); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1319 | } |
| 1320 | |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 1321 | #ifdef CONFIG_MULTICORE_RAID456 |
| 1322 | static void async_run_ops(void *param, async_cookie_t cookie) |
| 1323 | { |
| 1324 | struct stripe_head *sh = param; |
| 1325 | unsigned long ops_request = sh->ops.request; |
| 1326 | |
| 1327 | clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state); |
| 1328 | wake_up(&sh->ops.wait_for_ops); |
| 1329 | |
| 1330 | __raid_run_ops(sh, ops_request); |
| 1331 | release_stripe(sh); |
| 1332 | } |
| 1333 | |
| 1334 | static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request) |
| 1335 | { |
| 1336 | /* since handle_stripe can be called outside of raid5d context |
| 1337 | * we need to ensure sh->ops.request is de-staged before another |
| 1338 | * request arrives |
| 1339 | */ |
| 1340 | wait_event(sh->ops.wait_for_ops, |
| 1341 | !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state)); |
| 1342 | sh->ops.request = ops_request; |
| 1343 | |
| 1344 | atomic_inc(&sh->count); |
| 1345 | async_schedule(async_run_ops, sh); |
| 1346 | } |
| 1347 | #else |
| 1348 | #define raid_run_ops __raid_run_ops |
| 1349 | #endif |
| 1350 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1351 | static int grow_one_stripe(raid5_conf_t *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | { |
| 1353 | struct stripe_head *sh; |
Namhyung Kim | 6ce3284 | 2011-07-18 17:38:50 +1000 | [diff] [blame] | 1354 | sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1355 | if (!sh) |
| 1356 | return 0; |
Namhyung Kim | 6ce3284 | 2011-07-18 17:38:50 +1000 | [diff] [blame] | 1357 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1358 | sh->raid_conf = conf; |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 1359 | #ifdef CONFIG_MULTICORE_RAID456 |
| 1360 | init_waitqueue_head(&sh->ops.wait_for_ops); |
| 1361 | #endif |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1362 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 1363 | if (grow_buffers(sh)) { |
| 1364 | shrink_buffers(sh); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1365 | kmem_cache_free(conf->slab_cache, sh); |
| 1366 | return 0; |
| 1367 | } |
| 1368 | /* we just created an active stripe so... */ |
| 1369 | atomic_set(&sh->count, 1); |
| 1370 | atomic_inc(&conf->active_stripes); |
| 1371 | INIT_LIST_HEAD(&sh->lru); |
| 1372 | release_stripe(sh); |
| 1373 | return 1; |
| 1374 | } |
| 1375 | |
| 1376 | static int grow_stripes(raid5_conf_t *conf, int num) |
| 1377 | { |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 1378 | struct kmem_cache *sc; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 1379 | int devs = max(conf->raid_disks, conf->previous_raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | |
NeilBrown | f4be6b4 | 2010-06-01 19:37:25 +1000 | [diff] [blame] | 1381 | if (conf->mddev->gendisk) |
| 1382 | sprintf(conf->cache_name[0], |
| 1383 | "raid%d-%s", conf->level, mdname(conf->mddev)); |
| 1384 | else |
| 1385 | sprintf(conf->cache_name[0], |
| 1386 | "raid%d-%p", conf->level, conf->mddev); |
| 1387 | sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]); |
| 1388 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1389 | conf->active_name = 0; |
| 1390 | sc = kmem_cache_create(conf->cache_name[conf->active_name], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1392 | 0, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | if (!sc) |
| 1394 | return 1; |
| 1395 | conf->slab_cache = sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1396 | conf->pool_size = devs; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1397 | while (num--) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1398 | if (!grow_one_stripe(conf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | return 0; |
| 1401 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 1402 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1403 | /** |
| 1404 | * scribble_len - return the required size of the scribble region |
| 1405 | * @num - total number of disks in the array |
| 1406 | * |
| 1407 | * The size must be enough to contain: |
| 1408 | * 1/ a struct page pointer for each device in the array +2 |
| 1409 | * 2/ room to convert each entry in (1) to its corresponding dma |
| 1410 | * (dma_map_page()) or page (page_address()) address. |
| 1411 | * |
| 1412 | * Note: the +2 is for the destination buffers of the ddf/raid6 case where we |
| 1413 | * calculate over all devices (not just the data blocks), using zeros in place |
| 1414 | * of the P and Q blocks. |
| 1415 | */ |
| 1416 | static size_t scribble_len(int num) |
| 1417 | { |
| 1418 | size_t len; |
| 1419 | |
| 1420 | len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2); |
| 1421 | |
| 1422 | return len; |
| 1423 | } |
| 1424 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1425 | static int resize_stripes(raid5_conf_t *conf, int newsize) |
| 1426 | { |
| 1427 | /* Make all the stripes able to hold 'newsize' devices. |
| 1428 | * New slots in each stripe get 'page' set to a new page. |
| 1429 | * |
| 1430 | * This happens in stages: |
| 1431 | * 1/ create a new kmem_cache and allocate the required number of |
| 1432 | * stripe_heads. |
| 1433 | * 2/ gather all the old stripe_heads and tranfer the pages across |
| 1434 | * to the new stripe_heads. This will have the side effect of |
| 1435 | * freezing the array as once all stripe_heads have been collected, |
| 1436 | * no IO will be possible. Old stripe heads are freed once their |
| 1437 | * pages have been transferred over, and the old kmem_cache is |
| 1438 | * freed when all stripes are done. |
| 1439 | * 3/ reallocate conf->disks to be suitable bigger. If this fails, |
| 1440 | * we simple return a failre status - no need to clean anything up. |
| 1441 | * 4/ allocate new pages for the new slots in the new stripe_heads. |
| 1442 | * If this fails, we don't bother trying the shrink the |
| 1443 | * stripe_heads down again, we just leave them as they are. |
| 1444 | * As each stripe_head is processed the new one is released into |
| 1445 | * active service. |
| 1446 | * |
| 1447 | * Once step2 is started, we cannot afford to wait for a write, |
| 1448 | * so we use GFP_NOIO allocations. |
| 1449 | */ |
| 1450 | struct stripe_head *osh, *nsh; |
| 1451 | LIST_HEAD(newstripes); |
| 1452 | struct disk_info *ndisks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1453 | unsigned long cpu; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 1454 | int err; |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 1455 | struct kmem_cache *sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1456 | int i; |
| 1457 | |
| 1458 | if (newsize <= conf->pool_size) |
| 1459 | return 0; /* never bother to shrink */ |
| 1460 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 1461 | err = md_allow_write(conf->mddev); |
| 1462 | if (err) |
| 1463 | return err; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 1464 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1465 | /* Step 1 */ |
| 1466 | sc = kmem_cache_create(conf->cache_name[1-conf->active_name], |
| 1467 | sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1468 | 0, 0, NULL); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1469 | if (!sc) |
| 1470 | return -ENOMEM; |
| 1471 | |
| 1472 | for (i = conf->max_nr_stripes; i; i--) { |
Namhyung Kim | 6ce3284 | 2011-07-18 17:38:50 +1000 | [diff] [blame] | 1473 | nsh = kmem_cache_zalloc(sc, GFP_KERNEL); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1474 | if (!nsh) |
| 1475 | break; |
| 1476 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1477 | nsh->raid_conf = conf; |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 1478 | #ifdef CONFIG_MULTICORE_RAID456 |
| 1479 | init_waitqueue_head(&nsh->ops.wait_for_ops); |
| 1480 | #endif |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1481 | |
| 1482 | list_add(&nsh->lru, &newstripes); |
| 1483 | } |
| 1484 | if (i) { |
| 1485 | /* didn't get enough, give up */ |
| 1486 | while (!list_empty(&newstripes)) { |
| 1487 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 1488 | list_del(&nsh->lru); |
| 1489 | kmem_cache_free(sc, nsh); |
| 1490 | } |
| 1491 | kmem_cache_destroy(sc); |
| 1492 | return -ENOMEM; |
| 1493 | } |
| 1494 | /* Step 2 - Must use GFP_NOIO now. |
| 1495 | * OK, we have enough stripes, start collecting inactive |
| 1496 | * stripes and copying them over |
| 1497 | */ |
| 1498 | list_for_each_entry(nsh, &newstripes, lru) { |
| 1499 | spin_lock_irq(&conf->device_lock); |
| 1500 | wait_event_lock_irq(conf->wait_for_stripe, |
| 1501 | !list_empty(&conf->inactive_list), |
| 1502 | conf->device_lock, |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 1503 | ); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1504 | osh = get_free_stripe(conf); |
| 1505 | spin_unlock_irq(&conf->device_lock); |
| 1506 | atomic_set(&nsh->count, 1); |
| 1507 | for(i=0; i<conf->pool_size; i++) |
| 1508 | nsh->dev[i].page = osh->dev[i].page; |
| 1509 | for( ; i<newsize; i++) |
| 1510 | nsh->dev[i].page = NULL; |
| 1511 | kmem_cache_free(conf->slab_cache, osh); |
| 1512 | } |
| 1513 | kmem_cache_destroy(conf->slab_cache); |
| 1514 | |
| 1515 | /* Step 3. |
| 1516 | * At this point, we are holding all the stripes so the array |
| 1517 | * is completely stalled, so now is a good time to resize |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1518 | * conf->disks and the scribble region |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1519 | */ |
| 1520 | ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO); |
| 1521 | if (ndisks) { |
| 1522 | for (i=0; i<conf->raid_disks; i++) |
| 1523 | ndisks[i] = conf->disks[i]; |
| 1524 | kfree(conf->disks); |
| 1525 | conf->disks = ndisks; |
| 1526 | } else |
| 1527 | err = -ENOMEM; |
| 1528 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1529 | get_online_cpus(); |
| 1530 | conf->scribble_len = scribble_len(newsize); |
| 1531 | for_each_present_cpu(cpu) { |
| 1532 | struct raid5_percpu *percpu; |
| 1533 | void *scribble; |
| 1534 | |
| 1535 | percpu = per_cpu_ptr(conf->percpu, cpu); |
| 1536 | scribble = kmalloc(conf->scribble_len, GFP_NOIO); |
| 1537 | |
| 1538 | if (scribble) { |
| 1539 | kfree(percpu->scribble); |
| 1540 | percpu->scribble = scribble; |
| 1541 | } else { |
| 1542 | err = -ENOMEM; |
| 1543 | break; |
| 1544 | } |
| 1545 | } |
| 1546 | put_online_cpus(); |
| 1547 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1548 | /* Step 4, return new stripes to service */ |
| 1549 | while(!list_empty(&newstripes)) { |
| 1550 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 1551 | list_del_init(&nsh->lru); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1552 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1553 | for (i=conf->raid_disks; i < newsize; i++) |
| 1554 | if (nsh->dev[i].page == NULL) { |
| 1555 | struct page *p = alloc_page(GFP_NOIO); |
| 1556 | nsh->dev[i].page = p; |
| 1557 | if (!p) |
| 1558 | err = -ENOMEM; |
| 1559 | } |
| 1560 | release_stripe(nsh); |
| 1561 | } |
| 1562 | /* critical section pass, GFP_NOIO no longer needed */ |
| 1563 | |
| 1564 | conf->slab_cache = sc; |
| 1565 | conf->active_name = 1-conf->active_name; |
| 1566 | conf->pool_size = newsize; |
| 1567 | return err; |
| 1568 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1570 | static int drop_one_stripe(raid5_conf_t *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | { |
| 1572 | struct stripe_head *sh; |
| 1573 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1574 | spin_lock_irq(&conf->device_lock); |
| 1575 | sh = get_free_stripe(conf); |
| 1576 | spin_unlock_irq(&conf->device_lock); |
| 1577 | if (!sh) |
| 1578 | return 0; |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 1579 | BUG_ON(atomic_read(&sh->count)); |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 1580 | shrink_buffers(sh); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1581 | kmem_cache_free(conf->slab_cache, sh); |
| 1582 | atomic_dec(&conf->active_stripes); |
| 1583 | return 1; |
| 1584 | } |
| 1585 | |
| 1586 | static void shrink_stripes(raid5_conf_t *conf) |
| 1587 | { |
| 1588 | while (drop_one_stripe(conf)) |
| 1589 | ; |
| 1590 | |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1591 | if (conf->slab_cache) |
| 1592 | kmem_cache_destroy(conf->slab_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | conf->slab_cache = NULL; |
| 1594 | } |
| 1595 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1596 | static void raid5_end_read_request(struct bio * bi, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1598 | struct stripe_head *sh = bi->bi_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 1600 | int disks = sh->disks, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1602 | char b[BDEVNAME_SIZE]; |
| 1603 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | |
| 1606 | for (i=0 ; i<disks; i++) |
| 1607 | if (bi == &sh->dev[i].req) |
| 1608 | break; |
| 1609 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1610 | pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n", |
| 1611 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | uptodate); |
| 1613 | if (i == disks) { |
| 1614 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1615 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | if (uptodate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | set_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1620 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1621 | rdev = conf->disks[i].rdev; |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1622 | printk_ratelimited( |
| 1623 | KERN_INFO |
| 1624 | "md/raid:%s: read error corrected" |
| 1625 | " (%lu sectors at %llu on %s)\n", |
| 1626 | mdname(conf->mddev), STRIPE_SECTORS, |
| 1627 | (unsigned long long)(sh->sector |
| 1628 | + rdev->data_offset), |
| 1629 | bdevname(rdev->bdev, b)); |
Namhyung Kim | ddd5115 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1630 | atomic_add(STRIPE_SECTORS, &rdev->corrected_errors); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1631 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 1632 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
| 1633 | } |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1634 | if (atomic_read(&conf->disks[i].rdev->read_errors)) |
| 1635 | atomic_set(&conf->disks[i].rdev->read_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | } else { |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1637 | const char *bdn = bdevname(conf->disks[i].rdev->bdev, b); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1638 | int retry = 0; |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1639 | rdev = conf->disks[i].rdev; |
| 1640 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | clear_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1642 | atomic_inc(&rdev->read_errors); |
Gabriele A. Trombetti | 7b0bb53 | 2010-04-28 11:51:17 +1000 | [diff] [blame] | 1643 | if (conf->mddev->degraded >= conf->max_degraded) |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1644 | printk_ratelimited( |
| 1645 | KERN_WARNING |
| 1646 | "md/raid:%s: read error not correctable " |
| 1647 | "(sector %llu on %s).\n", |
| 1648 | mdname(conf->mddev), |
| 1649 | (unsigned long long)(sh->sector |
| 1650 | + rdev->data_offset), |
| 1651 | bdn); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1652 | else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1653 | /* Oh, no!!! */ |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1654 | printk_ratelimited( |
| 1655 | KERN_WARNING |
| 1656 | "md/raid:%s: read error NOT corrected!! " |
| 1657 | "(sector %llu on %s).\n", |
| 1658 | mdname(conf->mddev), |
| 1659 | (unsigned long long)(sh->sector |
| 1660 | + rdev->data_offset), |
| 1661 | bdn); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1662 | else if (atomic_read(&rdev->read_errors) |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1663 | > conf->max_nr_stripes) |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 1664 | printk(KERN_WARNING |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 1665 | "md/raid:%s: Too many read errors, failing device %s.\n", |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1666 | mdname(conf->mddev), bdn); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1667 | else |
| 1668 | retry = 1; |
| 1669 | if (retry) |
| 1670 | set_bit(R5_ReadError, &sh->dev[i].flags); |
| 1671 | else { |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1672 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 1673 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1674 | md_error(conf->mddev, rdev); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1675 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | } |
| 1677 | rdev_dec_pending(conf->disks[i].rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 1679 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1680 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | } |
| 1682 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 1683 | static void raid5_end_write_request(struct bio *bi, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1685 | struct stripe_head *sh = bi->bi_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 1687 | int disks = sh->disks, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 1689 | sector_t first_bad; |
| 1690 | int bad_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | for (i=0 ; i<disks; i++) |
| 1693 | if (bi == &sh->dev[i].req) |
| 1694 | break; |
| 1695 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1696 | pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
| 1698 | uptodate); |
| 1699 | if (i == disks) { |
| 1700 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1701 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | } |
| 1703 | |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 1704 | if (!uptodate) { |
| 1705 | set_bit(WriteErrorSeen, &conf->disks[i].rdev->flags); |
| 1706 | set_bit(R5_WriteError, &sh->dev[i].flags); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 1707 | } else if (is_badblock(conf->disks[i].rdev, sh->sector, STRIPE_SECTORS, |
| 1708 | &first_bad, &bad_sectors)) |
| 1709 | set_bit(R5_MadeGood, &sh->dev[i].flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | |
| 1711 | rdev_dec_pending(conf->disks[i].rdev, conf->mddev); |
| 1712 | |
| 1713 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 1714 | set_bit(STRIPE_HANDLE, &sh->state); |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1715 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1719 | 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] | 1720 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1721 | 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] | 1722 | { |
| 1723 | struct r5dev *dev = &sh->dev[i]; |
| 1724 | |
| 1725 | bio_init(&dev->req); |
| 1726 | dev->req.bi_io_vec = &dev->vec; |
| 1727 | dev->req.bi_vcnt++; |
| 1728 | dev->req.bi_max_vecs++; |
| 1729 | dev->vec.bv_page = dev->page; |
| 1730 | dev->vec.bv_len = STRIPE_SIZE; |
| 1731 | dev->vec.bv_offset = 0; |
| 1732 | |
| 1733 | dev->req.bi_sector = sh->sector; |
| 1734 | dev->req.bi_private = sh; |
| 1735 | |
| 1736 | dev->flags = 0; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1737 | dev->sector = compute_blocknr(sh, i, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | } |
| 1739 | |
| 1740 | static void error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1741 | { |
| 1742 | char b[BDEVNAME_SIZE]; |
H Hartley Sweeten | 7b92813 | 2010-03-08 16:02:40 +1100 | [diff] [blame] | 1743 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 1744 | pr_debug("raid456: error called\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | |
NeilBrown | 6f8d0c7 | 2011-05-11 14:38:44 +1000 | [diff] [blame] | 1746 | if (test_and_clear_bit(In_sync, &rdev->flags)) { |
| 1747 | unsigned long flags; |
| 1748 | spin_lock_irqsave(&conf->device_lock, flags); |
| 1749 | mddev->degraded++; |
| 1750 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 1751 | /* |
| 1752 | * if recovery was running, make sure it aborts. |
| 1753 | */ |
| 1754 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1755 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 1756 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 6f8d0c7 | 2011-05-11 14:38:44 +1000 | [diff] [blame] | 1757 | set_bit(Faulty, &rdev->flags); |
| 1758 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 1759 | printk(KERN_ALERT |
| 1760 | "md/raid:%s: Disk failure on %s, disabling device.\n" |
| 1761 | "md/raid:%s: Operation continuing on %d devices.\n", |
| 1762 | mdname(mddev), |
| 1763 | bdevname(rdev->bdev, b), |
| 1764 | mdname(mddev), |
| 1765 | conf->raid_disks - mddev->degraded); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1766 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | |
| 1768 | /* |
| 1769 | * Input: a 'big' sector number, |
| 1770 | * Output: index of the data and parity disk, and the sector # in them. |
| 1771 | */ |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1772 | static sector_t raid5_compute_sector(raid5_conf_t *conf, sector_t r_sector, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1773 | int previous, int *dd_idx, |
| 1774 | struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | { |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1776 | sector_t stripe, stripe2; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 1777 | sector_t chunk_number; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | unsigned int chunk_offset; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1779 | int pd_idx, qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1780 | int ddf_layout = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1781 | sector_t new_sector; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1782 | int algorithm = previous ? conf->prev_algo |
| 1783 | : conf->algorithm; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 1784 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
| 1785 | : conf->chunk_sectors; |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1786 | int raid_disks = previous ? conf->previous_raid_disks |
| 1787 | : conf->raid_disks; |
| 1788 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | |
| 1790 | /* First compute the information on this sector */ |
| 1791 | |
| 1792 | /* |
| 1793 | * Compute the chunk number and the sector offset inside the chunk |
| 1794 | */ |
| 1795 | chunk_offset = sector_div(r_sector, sectors_per_chunk); |
| 1796 | chunk_number = r_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | |
| 1798 | /* |
| 1799 | * Compute the stripe number |
| 1800 | */ |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 1801 | stripe = chunk_number; |
| 1802 | *dd_idx = sector_div(stripe, data_disks); |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1803 | stripe2 = stripe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | /* |
| 1805 | * Select the parity disk based on the user selected algorithm. |
| 1806 | */ |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1807 | pd_idx = qd_idx = -1; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1808 | switch(conf->level) { |
| 1809 | case 4: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1810 | pd_idx = data_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1811 | break; |
| 1812 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1813 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1815 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1816 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | (*dd_idx)++; |
| 1818 | break; |
| 1819 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1820 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1821 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | (*dd_idx)++; |
| 1823 | break; |
| 1824 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1825 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1826 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | break; |
| 1828 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1829 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1830 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1832 | case ALGORITHM_PARITY_0: |
| 1833 | pd_idx = 0; |
| 1834 | (*dd_idx)++; |
| 1835 | break; |
| 1836 | case ALGORITHM_PARITY_N: |
| 1837 | pd_idx = data_disks; |
| 1838 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1839 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1840 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1841 | } |
| 1842 | break; |
| 1843 | case 6: |
| 1844 | |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1845 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1846 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1847 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1848 | qd_idx = pd_idx + 1; |
| 1849 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1850 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1851 | qd_idx = 0; |
| 1852 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1853 | (*dd_idx) += 2; /* D D P Q D */ |
| 1854 | break; |
| 1855 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1856 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1857 | qd_idx = pd_idx + 1; |
| 1858 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1859 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1860 | qd_idx = 0; |
| 1861 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1862 | (*dd_idx) += 2; /* D D P Q D */ |
| 1863 | break; |
| 1864 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1865 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1866 | qd_idx = (pd_idx + 1) % raid_disks; |
| 1867 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1868 | break; |
| 1869 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1870 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1871 | qd_idx = (pd_idx + 1) % raid_disks; |
| 1872 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1873 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1874 | |
| 1875 | case ALGORITHM_PARITY_0: |
| 1876 | pd_idx = 0; |
| 1877 | qd_idx = 1; |
| 1878 | (*dd_idx) += 2; |
| 1879 | break; |
| 1880 | case ALGORITHM_PARITY_N: |
| 1881 | pd_idx = data_disks; |
| 1882 | qd_idx = data_disks + 1; |
| 1883 | break; |
| 1884 | |
| 1885 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 1886 | /* Exactly the same as RIGHT_ASYMMETRIC, but or |
| 1887 | * of blocks for computing Q is different. |
| 1888 | */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1889 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1890 | qd_idx = pd_idx + 1; |
| 1891 | if (pd_idx == raid_disks-1) { |
| 1892 | (*dd_idx)++; /* Q D D D P */ |
| 1893 | qd_idx = 0; |
| 1894 | } else if (*dd_idx >= pd_idx) |
| 1895 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1896 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1897 | break; |
| 1898 | |
| 1899 | case ALGORITHM_ROTATING_N_RESTART: |
| 1900 | /* Same a left_asymmetric, by first stripe is |
| 1901 | * D D D P Q rather than |
| 1902 | * Q D D D P |
| 1903 | */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1904 | stripe2 += 1; |
| 1905 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1906 | qd_idx = pd_idx + 1; |
| 1907 | if (pd_idx == raid_disks-1) { |
| 1908 | (*dd_idx)++; /* Q D D D P */ |
| 1909 | qd_idx = 0; |
| 1910 | } else if (*dd_idx >= pd_idx) |
| 1911 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1912 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1913 | break; |
| 1914 | |
| 1915 | case ALGORITHM_ROTATING_N_CONTINUE: |
| 1916 | /* Same as left_symmetric but Q is before P */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1917 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1918 | qd_idx = (pd_idx + raid_disks - 1) % raid_disks; |
| 1919 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1920 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1921 | break; |
| 1922 | |
| 1923 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 1924 | /* RAID5 left_asymmetric, with Q on last device */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1925 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1926 | if (*dd_idx >= pd_idx) |
| 1927 | (*dd_idx)++; |
| 1928 | qd_idx = raid_disks - 1; |
| 1929 | break; |
| 1930 | |
| 1931 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1932 | pd_idx = sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1933 | if (*dd_idx >= pd_idx) |
| 1934 | (*dd_idx)++; |
| 1935 | qd_idx = raid_disks - 1; |
| 1936 | break; |
| 1937 | |
| 1938 | case ALGORITHM_LEFT_SYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1939 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1940 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 1941 | qd_idx = raid_disks - 1; |
| 1942 | break; |
| 1943 | |
| 1944 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1945 | pd_idx = sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1946 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 1947 | qd_idx = raid_disks - 1; |
| 1948 | break; |
| 1949 | |
| 1950 | case ALGORITHM_PARITY_0_6: |
| 1951 | pd_idx = 0; |
| 1952 | (*dd_idx)++; |
| 1953 | qd_idx = raid_disks - 1; |
| 1954 | break; |
| 1955 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1956 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1957 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1958 | } |
| 1959 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | } |
| 1961 | |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1962 | if (sh) { |
| 1963 | sh->pd_idx = pd_idx; |
| 1964 | sh->qd_idx = qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1965 | sh->ddf_layout = ddf_layout; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1966 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1967 | /* |
| 1968 | * Finally, compute the new sector number |
| 1969 | */ |
| 1970 | new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset; |
| 1971 | return new_sector; |
| 1972 | } |
| 1973 | |
| 1974 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1975 | 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] | 1976 | { |
| 1977 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 1978 | int raid_disks = sh->disks; |
| 1979 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | sector_t new_sector = sh->sector, check; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 1981 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
| 1982 | : conf->chunk_sectors; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1983 | int algorithm = previous ? conf->prev_algo |
| 1984 | : conf->algorithm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1985 | sector_t stripe; |
| 1986 | int chunk_offset; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 1987 | sector_t chunk_number; |
| 1988 | int dummy1, dd_idx = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | sector_t r_sector; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1990 | struct stripe_head sh2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1991 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1992 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1993 | chunk_offset = sector_div(new_sector, sectors_per_chunk); |
| 1994 | stripe = new_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1995 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1996 | if (i == sh->pd_idx) |
| 1997 | return 0; |
| 1998 | switch(conf->level) { |
| 1999 | case 4: break; |
| 2000 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2001 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2002 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 2003 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 2004 | if (i > sh->pd_idx) |
| 2005 | i--; |
| 2006 | break; |
| 2007 | case ALGORITHM_LEFT_SYMMETRIC: |
| 2008 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 2009 | if (i < sh->pd_idx) |
| 2010 | i += raid_disks; |
| 2011 | i -= (sh->pd_idx + 1); |
| 2012 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2013 | case ALGORITHM_PARITY_0: |
| 2014 | i -= 1; |
| 2015 | break; |
| 2016 | case ALGORITHM_PARITY_N: |
| 2017 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2019 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2020 | } |
| 2021 | break; |
| 2022 | case 6: |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2023 | if (i == sh->qd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2024 | return 0; /* It is the Q disk */ |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2025 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2026 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 2027 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2028 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 2029 | case ALGORITHM_ROTATING_N_RESTART: |
| 2030 | if (sh->pd_idx == raid_disks-1) |
| 2031 | i--; /* Q D D D P */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2032 | else if (i > sh->pd_idx) |
| 2033 | i -= 2; /* D D P Q D */ |
| 2034 | break; |
| 2035 | case ALGORITHM_LEFT_SYMMETRIC: |
| 2036 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 2037 | if (sh->pd_idx == raid_disks-1) |
| 2038 | i--; /* Q D D D P */ |
| 2039 | else { |
| 2040 | /* D D P Q D */ |
| 2041 | if (i < sh->pd_idx) |
| 2042 | i += raid_disks; |
| 2043 | i -= (sh->pd_idx + 2); |
| 2044 | } |
| 2045 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2046 | case ALGORITHM_PARITY_0: |
| 2047 | i -= 2; |
| 2048 | break; |
| 2049 | case ALGORITHM_PARITY_N: |
| 2050 | break; |
| 2051 | case ALGORITHM_ROTATING_N_CONTINUE: |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 2052 | /* Like left_symmetric, but P is before Q */ |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2053 | if (sh->pd_idx == 0) |
| 2054 | i--; /* P D D D Q */ |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 2055 | else { |
| 2056 | /* D D Q P D */ |
| 2057 | if (i < sh->pd_idx) |
| 2058 | i += raid_disks; |
| 2059 | i -= (sh->pd_idx + 1); |
| 2060 | } |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2061 | break; |
| 2062 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 2063 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 2064 | if (i > sh->pd_idx) |
| 2065 | i--; |
| 2066 | break; |
| 2067 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 2068 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 2069 | if (i < sh->pd_idx) |
| 2070 | i += data_disks + 1; |
| 2071 | i -= (sh->pd_idx + 1); |
| 2072 | break; |
| 2073 | case ALGORITHM_PARITY_0_6: |
| 2074 | i -= 1; |
| 2075 | break; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2076 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2077 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2078 | } |
| 2079 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2080 | } |
| 2081 | |
| 2082 | chunk_number = stripe * data_disks + i; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2083 | r_sector = chunk_number * sectors_per_chunk + chunk_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2084 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2085 | check = raid5_compute_sector(conf, r_sector, |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2086 | previous, &dummy1, &sh2); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2087 | if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx |
| 2088 | || sh2.qd_idx != sh->qd_idx) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 2089 | printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n", |
| 2090 | mdname(conf->mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2091 | return 0; |
| 2092 | } |
| 2093 | return r_sector; |
| 2094 | } |
| 2095 | |
| 2096 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2097 | static void |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2098 | schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2099 | int rcw, int expand) |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2100 | { |
| 2101 | int i, pd_idx = sh->pd_idx, disks = sh->disks; |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2102 | raid5_conf_t *conf = sh->raid_conf; |
| 2103 | int level = conf->level; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2104 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2105 | if (rcw) { |
| 2106 | /* if we are not expanding this is a proper write request, and |
| 2107 | * there will be bios with new data to be drained into the |
| 2108 | * stripe cache |
| 2109 | */ |
| 2110 | if (!expand) { |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2111 | sh->reconstruct_state = reconstruct_state_drain_run; |
| 2112 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
| 2113 | } else |
| 2114 | sh->reconstruct_state = reconstruct_state_run; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2115 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2116 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2117 | |
| 2118 | for (i = disks; i--; ) { |
| 2119 | struct r5dev *dev = &sh->dev[i]; |
| 2120 | |
| 2121 | if (dev->towrite) { |
| 2122 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2123 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2124 | if (!expand) |
| 2125 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2126 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2127 | } |
| 2128 | } |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2129 | if (s->locked + conf->max_degraded == disks) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2130 | if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state)) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2131 | atomic_inc(&conf->pending_full_writes); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2132 | } else { |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2133 | BUG_ON(level == 6); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2134 | BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) || |
| 2135 | test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags))); |
| 2136 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2137 | sh->reconstruct_state = reconstruct_state_prexor_drain_run; |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2138 | set_bit(STRIPE_OP_PREXOR, &s->ops_request); |
| 2139 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2140 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2141 | |
| 2142 | for (i = disks; i--; ) { |
| 2143 | struct r5dev *dev = &sh->dev[i]; |
| 2144 | if (i == pd_idx) |
| 2145 | continue; |
| 2146 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2147 | if (dev->towrite && |
| 2148 | (test_bit(R5_UPTODATE, &dev->flags) || |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2149 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2150 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2151 | set_bit(R5_LOCKED, &dev->flags); |
| 2152 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2153 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2154 | } |
| 2155 | } |
| 2156 | } |
| 2157 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2158 | /* keep the parity disk(s) locked while asynchronous operations |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2159 | * are in flight |
| 2160 | */ |
| 2161 | set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); |
| 2162 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2163 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2164 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2165 | if (level == 6) { |
| 2166 | int qd_idx = sh->qd_idx; |
| 2167 | struct r5dev *dev = &sh->dev[qd_idx]; |
| 2168 | |
| 2169 | set_bit(R5_LOCKED, &dev->flags); |
| 2170 | clear_bit(R5_UPTODATE, &dev->flags); |
| 2171 | s->locked++; |
| 2172 | } |
| 2173 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2174 | pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n", |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 2175 | __func__, (unsigned long long)sh->sector, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2176 | s->locked, s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2177 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2178 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | /* |
| 2180 | * Each stripe/dev can have one or more bion attached. |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2181 | * toread/towrite point to the first in a chain. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | * The bi_next chain must be in order. |
| 2183 | */ |
| 2184 | static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite) |
| 2185 | { |
| 2186 | struct bio **bip; |
| 2187 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2188 | int firstwrite=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 2190 | pr_debug("adding bi b#%llu to stripe s#%llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2191 | (unsigned long long)bi->bi_sector, |
| 2192 | (unsigned long long)sh->sector); |
| 2193 | |
| 2194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2195 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2196 | if (forwrite) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | bip = &sh->dev[dd_idx].towrite; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2198 | if (*bip == NULL && sh->dev[dd_idx].written == NULL) |
| 2199 | firstwrite = 1; |
| 2200 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2201 | bip = &sh->dev[dd_idx].toread; |
| 2202 | while (*bip && (*bip)->bi_sector < bi->bi_sector) { |
| 2203 | if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector) |
| 2204 | goto overlap; |
| 2205 | bip = & (*bip)->bi_next; |
| 2206 | } |
| 2207 | if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9)) |
| 2208 | goto overlap; |
| 2209 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 2210 | BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2211 | if (*bip) |
| 2212 | bi->bi_next = *bip; |
| 2213 | *bip = bi; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2214 | bi->bi_phys_segments++; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2216 | if (forwrite) { |
| 2217 | /* check if page is covered */ |
| 2218 | sector_t sector = sh->dev[dd_idx].sector; |
| 2219 | for (bi=sh->dev[dd_idx].towrite; |
| 2220 | sector < sh->dev[dd_idx].sector + STRIPE_SECTORS && |
| 2221 | bi && bi->bi_sector <= sector; |
| 2222 | bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) { |
| 2223 | if (bi->bi_sector + (bi->bi_size>>9) >= sector) |
| 2224 | sector = bi->bi_sector + (bi->bi_size>>9); |
| 2225 | } |
| 2226 | if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS) |
| 2227 | set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags); |
| 2228 | } |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 2229 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 2230 | |
| 2231 | pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n", |
| 2232 | (unsigned long long)(*bip)->bi_sector, |
| 2233 | (unsigned long long)sh->sector, dd_idx); |
| 2234 | |
| 2235 | if (conf->mddev->bitmap && firstwrite) { |
| 2236 | bitmap_startwrite(conf->mddev->bitmap, sh->sector, |
| 2237 | STRIPE_SECTORS, 0); |
| 2238 | sh->bm_seq = conf->seq_flush+1; |
| 2239 | set_bit(STRIPE_BIT_DELAY, &sh->state); |
| 2240 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | return 1; |
| 2242 | |
| 2243 | overlap: |
| 2244 | set_bit(R5_Overlap, &sh->dev[dd_idx].flags); |
| 2245 | spin_unlock_irq(&conf->device_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | return 0; |
| 2247 | } |
| 2248 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 2249 | static void end_reshape(raid5_conf_t *conf); |
| 2250 | |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2251 | static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous, |
| 2252 | struct stripe_head *sh) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2253 | { |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2254 | int sectors_per_chunk = |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 2255 | previous ? conf->prev_chunk_sectors : conf->chunk_sectors; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2256 | int dd_idx; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 2257 | int chunk_offset = sector_div(stripe, sectors_per_chunk); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2258 | int disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 2259 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2260 | raid5_compute_sector(conf, |
| 2261 | stripe * (disks - conf->max_degraded) |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 2262 | *sectors_per_chunk + chunk_offset, |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2263 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2264 | &dd_idx, sh); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2265 | } |
| 2266 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2267 | static void |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2268 | handle_failed_stripe(raid5_conf_t *conf, struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2269 | struct stripe_head_state *s, int disks, |
| 2270 | struct bio **return_bi) |
| 2271 | { |
| 2272 | int i; |
| 2273 | for (i = disks; i--; ) { |
| 2274 | struct bio *bi; |
| 2275 | int bitmap_end = 0; |
| 2276 | |
| 2277 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
| 2278 | mdk_rdev_t *rdev; |
| 2279 | rcu_read_lock(); |
| 2280 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 2281 | if (rdev && test_bit(In_sync, &rdev->flags)) |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2282 | atomic_inc(&rdev->nr_pending); |
| 2283 | else |
| 2284 | rdev = NULL; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2285 | rcu_read_unlock(); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2286 | if (rdev) { |
| 2287 | if (!rdev_set_badblocks( |
| 2288 | rdev, |
| 2289 | sh->sector, |
| 2290 | STRIPE_SECTORS, 0)) |
| 2291 | md_error(conf->mddev, rdev); |
| 2292 | rdev_dec_pending(rdev, conf->mddev); |
| 2293 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2294 | } |
| 2295 | spin_lock_irq(&conf->device_lock); |
| 2296 | /* fail all writes first */ |
| 2297 | bi = sh->dev[i].towrite; |
| 2298 | sh->dev[i].towrite = NULL; |
| 2299 | if (bi) { |
| 2300 | s->to_write--; |
| 2301 | bitmap_end = 1; |
| 2302 | } |
| 2303 | |
| 2304 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 2305 | wake_up(&conf->wait_for_overlap); |
| 2306 | |
| 2307 | while (bi && bi->bi_sector < |
| 2308 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2309 | struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); |
| 2310 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2311 | if (!raid5_dec_bi_phys_segments(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2312 | md_write_end(conf->mddev); |
| 2313 | bi->bi_next = *return_bi; |
| 2314 | *return_bi = bi; |
| 2315 | } |
| 2316 | bi = nextbi; |
| 2317 | } |
| 2318 | /* and fail all 'written' */ |
| 2319 | bi = sh->dev[i].written; |
| 2320 | sh->dev[i].written = NULL; |
| 2321 | if (bi) bitmap_end = 1; |
| 2322 | while (bi && bi->bi_sector < |
| 2323 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2324 | struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector); |
| 2325 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2326 | if (!raid5_dec_bi_phys_segments(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2327 | md_write_end(conf->mddev); |
| 2328 | bi->bi_next = *return_bi; |
| 2329 | *return_bi = bi; |
| 2330 | } |
| 2331 | bi = bi2; |
| 2332 | } |
| 2333 | |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2334 | /* fail any reads if this device is non-operational and |
| 2335 | * the data has not reached the cache yet. |
| 2336 | */ |
| 2337 | if (!test_bit(R5_Wantfill, &sh->dev[i].flags) && |
| 2338 | (!test_bit(R5_Insync, &sh->dev[i].flags) || |
| 2339 | test_bit(R5_ReadError, &sh->dev[i].flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2340 | bi = sh->dev[i].toread; |
| 2341 | sh->dev[i].toread = NULL; |
| 2342 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 2343 | wake_up(&conf->wait_for_overlap); |
| 2344 | if (bi) s->to_read--; |
| 2345 | while (bi && bi->bi_sector < |
| 2346 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2347 | struct bio *nextbi = |
| 2348 | r5_next_bio(bi, sh->dev[i].sector); |
| 2349 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2350 | if (!raid5_dec_bi_phys_segments(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2351 | bi->bi_next = *return_bi; |
| 2352 | *return_bi = bi; |
| 2353 | } |
| 2354 | bi = nextbi; |
| 2355 | } |
| 2356 | } |
| 2357 | spin_unlock_irq(&conf->device_lock); |
| 2358 | if (bitmap_end) |
| 2359 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 2360 | STRIPE_SECTORS, 0, 0); |
NeilBrown | 8cfa7b0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2361 | /* If we were in the middle of a write the parity block might |
| 2362 | * still be locked - so just clear all R5_LOCKED flags |
| 2363 | */ |
| 2364 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2365 | } |
| 2366 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2367 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 2368 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 2369 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2370 | } |
| 2371 | |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2372 | static void |
| 2373 | handle_failed_sync(raid5_conf_t *conf, struct stripe_head *sh, |
| 2374 | struct stripe_head_state *s) |
| 2375 | { |
| 2376 | int abort = 0; |
| 2377 | int i; |
| 2378 | |
| 2379 | md_done_sync(conf->mddev, STRIPE_SECTORS, 0); |
| 2380 | clear_bit(STRIPE_SYNCING, &sh->state); |
| 2381 | s->syncing = 0; |
| 2382 | /* There is nothing more to do for sync/check/repair. |
| 2383 | * For recover we need to record a bad block on all |
| 2384 | * non-sync devices, or abort the recovery |
| 2385 | */ |
| 2386 | if (!test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) |
| 2387 | return; |
| 2388 | /* During recovery devices cannot be removed, so locking and |
| 2389 | * refcounting of rdevs is not needed |
| 2390 | */ |
| 2391 | for (i = 0; i < conf->raid_disks; i++) { |
| 2392 | mdk_rdev_t *rdev = conf->disks[i].rdev; |
| 2393 | if (!rdev |
| 2394 | || test_bit(Faulty, &rdev->flags) |
| 2395 | || test_bit(In_sync, &rdev->flags)) |
| 2396 | continue; |
| 2397 | if (!rdev_set_badblocks(rdev, sh->sector, |
| 2398 | STRIPE_SECTORS, 0)) |
| 2399 | abort = 1; |
| 2400 | } |
| 2401 | if (abort) { |
| 2402 | conf->recovery_disabled = conf->mddev->recovery_disabled; |
| 2403 | set_bit(MD_RECOVERY_INTR, &conf->mddev->recovery); |
| 2404 | } |
| 2405 | } |
| 2406 | |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2407 | /* 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] | 2408 | * to be read or computed to satisfy a request. |
| 2409 | * |
| 2410 | * Returns 1 when no more member devices need to be checked, otherwise returns |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2411 | * 0 to tell the loop in handle_stripe_fill to continue |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2412 | */ |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2413 | static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s, |
| 2414 | int disk_idx, int disks) |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2415 | { |
| 2416 | struct r5dev *dev = &sh->dev[disk_idx]; |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2417 | struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]], |
| 2418 | &sh->dev[s->failed_num[1]] }; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2419 | |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2420 | /* is the data in this block needed, and can we get it? */ |
| 2421 | if (!test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2422 | !test_bit(R5_UPTODATE, &dev->flags) && |
| 2423 | (dev->toread || |
| 2424 | (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) || |
| 2425 | s->syncing || s->expanding || |
NeilBrown | 5d35e09 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2426 | (s->failed >= 1 && fdev[0]->toread) || |
| 2427 | (s->failed >= 2 && fdev[1]->toread) || |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2428 | (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite && |
| 2429 | !test_bit(R5_OVERWRITE, &fdev[0]->flags)) || |
| 2430 | (sh->raid_conf->level == 6 && s->failed && s->to_write))) { |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2431 | /* we would like to get this block, possibly by computing it, |
| 2432 | * otherwise read it if the backing disk is insync |
| 2433 | */ |
| 2434 | BUG_ON(test_bit(R5_Wantcompute, &dev->flags)); |
| 2435 | BUG_ON(test_bit(R5_Wantread, &dev->flags)); |
| 2436 | if ((s->uptodate == disks - 1) && |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2437 | (s->failed && (disk_idx == s->failed_num[0] || |
| 2438 | disk_idx == s->failed_num[1]))) { |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2439 | /* have disk failed, and we're requested to fetch it; |
| 2440 | * do compute it |
| 2441 | */ |
| 2442 | pr_debug("Computing stripe %llu block %d\n", |
| 2443 | (unsigned long long)sh->sector, disk_idx); |
| 2444 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2445 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2446 | set_bit(R5_Wantcompute, &dev->flags); |
| 2447 | sh->ops.target = disk_idx; |
| 2448 | sh->ops.target2 = -1; /* no 2nd target */ |
| 2449 | s->req_compute = 1; |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2450 | /* Careful: from this point on 'uptodate' is in the eye |
| 2451 | * of raid_run_ops which services 'compute' operations |
| 2452 | * before writes. R5_Wantcompute flags a block that will |
| 2453 | * be R5_UPTODATE by the time it is needed for a |
| 2454 | * subsequent operation. |
| 2455 | */ |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2456 | s->uptodate++; |
| 2457 | return 1; |
| 2458 | } else if (s->uptodate == disks-2 && s->failed >= 2) { |
| 2459 | /* Computing 2-failure is *very* expensive; only |
| 2460 | * do it if failed >= 2 |
| 2461 | */ |
| 2462 | int other; |
| 2463 | for (other = disks; other--; ) { |
| 2464 | if (other == disk_idx) |
| 2465 | continue; |
| 2466 | if (!test_bit(R5_UPTODATE, |
| 2467 | &sh->dev[other].flags)) |
| 2468 | break; |
| 2469 | } |
| 2470 | BUG_ON(other < 0); |
| 2471 | pr_debug("Computing stripe %llu blocks %d,%d\n", |
| 2472 | (unsigned long long)sh->sector, |
| 2473 | disk_idx, other); |
| 2474 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2475 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2476 | set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags); |
| 2477 | set_bit(R5_Wantcompute, &sh->dev[other].flags); |
| 2478 | sh->ops.target = disk_idx; |
| 2479 | sh->ops.target2 = other; |
| 2480 | s->uptodate += 2; |
| 2481 | s->req_compute = 1; |
| 2482 | return 1; |
| 2483 | } else if (test_bit(R5_Insync, &dev->flags)) { |
| 2484 | set_bit(R5_LOCKED, &dev->flags); |
| 2485 | set_bit(R5_Wantread, &dev->flags); |
| 2486 | s->locked++; |
| 2487 | pr_debug("Reading block %d (sync=%d)\n", |
| 2488 | disk_idx, s->syncing); |
| 2489 | } |
| 2490 | } |
| 2491 | |
| 2492 | return 0; |
| 2493 | } |
| 2494 | |
| 2495 | /** |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2496 | * handle_stripe_fill - read or compute data to satisfy pending requests. |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2497 | */ |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2498 | static void handle_stripe_fill(struct stripe_head *sh, |
| 2499 | struct stripe_head_state *s, |
| 2500 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2501 | { |
| 2502 | int i; |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2503 | |
| 2504 | /* look for blocks to read/compute, skip this if a compute |
| 2505 | * is already in flight, or if the stripe contents are in the |
| 2506 | * midst of changing due to a write |
| 2507 | */ |
| 2508 | if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state && |
| 2509 | !sh->reconstruct_state) |
| 2510 | for (i = disks; i--; ) |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2511 | if (fetch_block(sh, s, i, disks)) |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2512 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2513 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2514 | } |
| 2515 | |
| 2516 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2517 | /* handle_stripe_clean_event |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2518 | * any written block on an uptodate or failed drive can be returned. |
| 2519 | * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but |
| 2520 | * never LOCKED, so we don't need to test 'failed' directly. |
| 2521 | */ |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2522 | static void handle_stripe_clean_event(raid5_conf_t *conf, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2523 | struct stripe_head *sh, int disks, struct bio **return_bi) |
| 2524 | { |
| 2525 | int i; |
| 2526 | struct r5dev *dev; |
| 2527 | |
| 2528 | for (i = disks; i--; ) |
| 2529 | if (sh->dev[i].written) { |
| 2530 | dev = &sh->dev[i]; |
| 2531 | if (!test_bit(R5_LOCKED, &dev->flags) && |
| 2532 | test_bit(R5_UPTODATE, &dev->flags)) { |
| 2533 | /* We can return any write requests */ |
| 2534 | struct bio *wbi, *wbi2; |
| 2535 | int bitmap_end = 0; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2536 | pr_debug("Return write for disc %d\n", i); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2537 | spin_lock_irq(&conf->device_lock); |
| 2538 | wbi = dev->written; |
| 2539 | dev->written = NULL; |
| 2540 | while (wbi && wbi->bi_sector < |
| 2541 | dev->sector + STRIPE_SECTORS) { |
| 2542 | wbi2 = r5_next_bio(wbi, dev->sector); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2543 | if (!raid5_dec_bi_phys_segments(wbi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2544 | md_write_end(conf->mddev); |
| 2545 | wbi->bi_next = *return_bi; |
| 2546 | *return_bi = wbi; |
| 2547 | } |
| 2548 | wbi = wbi2; |
| 2549 | } |
| 2550 | if (dev->towrite == NULL) |
| 2551 | bitmap_end = 1; |
| 2552 | spin_unlock_irq(&conf->device_lock); |
| 2553 | if (bitmap_end) |
| 2554 | bitmap_endwrite(conf->mddev->bitmap, |
| 2555 | sh->sector, |
| 2556 | STRIPE_SECTORS, |
| 2557 | !test_bit(STRIPE_DEGRADED, &sh->state), |
| 2558 | 0); |
| 2559 | } |
| 2560 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2561 | |
| 2562 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 2563 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 2564 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2565 | } |
| 2566 | |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2567 | static void handle_stripe_dirtying(raid5_conf_t *conf, |
| 2568 | struct stripe_head *sh, |
| 2569 | struct stripe_head_state *s, |
| 2570 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2571 | { |
| 2572 | int rmw = 0, rcw = 0, i; |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2573 | if (conf->max_degraded == 2) { |
| 2574 | /* RAID6 requires 'rcw' in current implementation |
| 2575 | * Calculate the real rcw later - for now fake it |
| 2576 | * look like rcw is cheaper |
| 2577 | */ |
| 2578 | rcw = 1; rmw = 2; |
| 2579 | } else for (i = disks; i--; ) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2580 | /* would I have to read this buffer for read_modify_write */ |
| 2581 | struct r5dev *dev = &sh->dev[i]; |
| 2582 | if ((dev->towrite || i == sh->pd_idx) && |
| 2583 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2584 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2585 | test_bit(R5_Wantcompute, &dev->flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2586 | if (test_bit(R5_Insync, &dev->flags)) |
| 2587 | rmw++; |
| 2588 | else |
| 2589 | rmw += 2*disks; /* cannot read it */ |
| 2590 | } |
| 2591 | /* Would I have to read this buffer for reconstruct_write */ |
| 2592 | if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx && |
| 2593 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2594 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2595 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2596 | if (test_bit(R5_Insync, &dev->flags)) rcw++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2597 | else |
| 2598 | rcw += 2*disks; |
| 2599 | } |
| 2600 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2601 | pr_debug("for sector %llu, rmw=%d rcw=%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2602 | (unsigned long long)sh->sector, rmw, rcw); |
| 2603 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2604 | if (rmw < rcw && rmw > 0) |
| 2605 | /* prefer read-modify-write, but need to get some data */ |
| 2606 | for (i = disks; i--; ) { |
| 2607 | struct r5dev *dev = &sh->dev[i]; |
| 2608 | if ((dev->towrite || i == sh->pd_idx) && |
| 2609 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2610 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2611 | test_bit(R5_Wantcompute, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2612 | test_bit(R5_Insync, &dev->flags)) { |
| 2613 | if ( |
| 2614 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2615 | pr_debug("Read_old block " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2616 | "%d for r-m-w\n", i); |
| 2617 | set_bit(R5_LOCKED, &dev->flags); |
| 2618 | set_bit(R5_Wantread, &dev->flags); |
| 2619 | s->locked++; |
| 2620 | } else { |
| 2621 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2622 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2623 | } |
| 2624 | } |
| 2625 | } |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2626 | if (rcw <= rmw && rcw > 0) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2627 | /* want reconstruct write, but need to get some data */ |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2628 | rcw = 0; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2629 | for (i = disks; i--; ) { |
| 2630 | struct r5dev *dev = &sh->dev[i]; |
| 2631 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2632 | i != sh->pd_idx && i != sh->qd_idx && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2633 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2634 | !(test_bit(R5_UPTODATE, &dev->flags) || |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2635 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2636 | rcw++; |
| 2637 | if (!test_bit(R5_Insync, &dev->flags)) |
| 2638 | continue; /* it's a failed drive */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2639 | if ( |
| 2640 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2641 | pr_debug("Read_old block " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2642 | "%d for Reconstruct\n", i); |
| 2643 | set_bit(R5_LOCKED, &dev->flags); |
| 2644 | set_bit(R5_Wantread, &dev->flags); |
| 2645 | s->locked++; |
| 2646 | } else { |
| 2647 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2648 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2649 | } |
| 2650 | } |
| 2651 | } |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2652 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2653 | /* now if nothing is locked, and if we have enough data, |
| 2654 | * we can start a write request |
| 2655 | */ |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2656 | /* since handle_stripe can be called at any time we need to handle the |
| 2657 | * case where a compute block operation has been submitted and then a |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2658 | * subsequent call wants to start a write request. raid_run_ops only |
| 2659 | * handles the case where compute block and reconstruct are requested |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2660 | * simultaneously. If this is not the case then new writes need to be |
| 2661 | * held off until the compute completes. |
| 2662 | */ |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2663 | if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) && |
| 2664 | (s->locked == 0 && (rcw == 0 || rmw == 0) && |
| 2665 | !test_bit(STRIPE_BIT_DELAY, &sh->state))) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2666 | schedule_reconstruction(sh, s, rcw == 0, 0); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2667 | } |
| 2668 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2669 | static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh, |
| 2670 | struct stripe_head_state *s, int disks) |
| 2671 | { |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2672 | struct r5dev *dev = NULL; |
Dan Williams | e89f896 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2673 | |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2674 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2675 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2676 | switch (sh->check_state) { |
| 2677 | case check_state_idle: |
| 2678 | /* start a new check operation if there are no failures */ |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2679 | if (s->failed == 0) { |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2680 | BUG_ON(s->uptodate != disks); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2681 | sh->check_state = check_state_run; |
| 2682 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2683 | clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2684 | s->uptodate--; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2685 | break; |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2686 | } |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2687 | dev = &sh->dev[s->failed_num[0]]; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2688 | /* fall through */ |
| 2689 | case check_state_compute_result: |
| 2690 | sh->check_state = check_state_idle; |
| 2691 | if (!dev) |
| 2692 | dev = &sh->dev[sh->pd_idx]; |
| 2693 | |
| 2694 | /* check that a write has not made the stripe insync */ |
| 2695 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 2696 | break; |
| 2697 | |
| 2698 | /* either failed parity check, or recovery is happening */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2699 | BUG_ON(!test_bit(R5_UPTODATE, &dev->flags)); |
| 2700 | BUG_ON(s->uptodate != disks); |
| 2701 | |
| 2702 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2703 | s->locked++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2704 | set_bit(R5_Wantwrite, &dev->flags); |
Dan Williams | 830ea01 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2705 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2706 | clear_bit(STRIPE_DEGRADED, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2707 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2708 | break; |
| 2709 | case check_state_run: |
| 2710 | break; /* we will be called again upon completion */ |
| 2711 | case check_state_check_result: |
| 2712 | sh->check_state = check_state_idle; |
| 2713 | |
| 2714 | /* if a failure occurred during the check operation, leave |
| 2715 | * STRIPE_INSYNC not set and let the stripe be handled again |
| 2716 | */ |
| 2717 | if (s->failed) |
| 2718 | break; |
| 2719 | |
| 2720 | /* handle a successful check operation, if parity is correct |
| 2721 | * we are done. Otherwise update the mismatch count and repair |
| 2722 | * parity if !MD_RECOVERY_CHECK |
| 2723 | */ |
Dan Williams | ad283ea | 2009-08-29 19:09:26 -0700 | [diff] [blame] | 2724 | if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0) |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2725 | /* parity is correct (on disc, |
| 2726 | * not in buffer any more) |
| 2727 | */ |
| 2728 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2729 | else { |
| 2730 | conf->mddev->resync_mismatches += STRIPE_SECTORS; |
| 2731 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 2732 | /* don't try to repair!! */ |
| 2733 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2734 | else { |
| 2735 | sh->check_state = check_state_compute_run; |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2736 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2737 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2738 | set_bit(R5_Wantcompute, |
| 2739 | &sh->dev[sh->pd_idx].flags); |
| 2740 | sh->ops.target = sh->pd_idx; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2741 | sh->ops.target2 = -1; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2742 | s->uptodate++; |
| 2743 | } |
| 2744 | } |
| 2745 | break; |
| 2746 | case check_state_compute_run: |
| 2747 | break; |
| 2748 | default: |
| 2749 | printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n", |
| 2750 | __func__, sh->check_state, |
| 2751 | (unsigned long long) sh->sector); |
| 2752 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2753 | } |
| 2754 | } |
| 2755 | |
| 2756 | |
| 2757 | static void handle_parity_checks6(raid5_conf_t *conf, struct stripe_head *sh, |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2758 | struct stripe_head_state *s, |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2759 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2760 | { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2761 | int pd_idx = sh->pd_idx; |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 2762 | int qd_idx = sh->qd_idx; |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2763 | struct r5dev *dev; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2764 | |
| 2765 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2766 | |
| 2767 | BUG_ON(s->failed > 2); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2768 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2769 | /* Want to check and possibly repair P and Q. |
| 2770 | * However there could be one 'failed' device, in which |
| 2771 | * case we can only check one of them, possibly using the |
| 2772 | * other to generate missing data |
| 2773 | */ |
| 2774 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2775 | switch (sh->check_state) { |
| 2776 | case check_state_idle: |
| 2777 | /* start a new check operation if there are < 2 failures */ |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2778 | if (s->failed == s->q_failed) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2779 | /* The only possible failed device holds Q, so it |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2780 | * makes sense to check P (If anything else were failed, |
| 2781 | * we would have used P to recreate it). |
| 2782 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2783 | sh->check_state = check_state_run; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2784 | } |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2785 | if (!s->q_failed && s->failed < 2) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2786 | /* Q is not failed, and we didn't use it to generate |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2787 | * anything, so it makes sense to check it |
| 2788 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2789 | if (sh->check_state == check_state_run) |
| 2790 | sh->check_state = check_state_run_pq; |
| 2791 | else |
| 2792 | sh->check_state = check_state_run_q; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2793 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2794 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2795 | /* discard potentially stale zero_sum_result */ |
| 2796 | sh->ops.zero_sum_result = 0; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2797 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2798 | if (sh->check_state == check_state_run) { |
| 2799 | /* async_xor_zero_sum destroys the contents of P */ |
| 2800 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
| 2801 | s->uptodate--; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2802 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2803 | if (sh->check_state >= check_state_run && |
| 2804 | sh->check_state <= check_state_run_pq) { |
| 2805 | /* async_syndrome_zero_sum preserves P and Q, so |
| 2806 | * no need to mark them !uptodate here |
| 2807 | */ |
| 2808 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
| 2809 | break; |
| 2810 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2811 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2812 | /* we have 2-disk failure */ |
| 2813 | BUG_ON(s->failed != 2); |
| 2814 | /* fall through */ |
| 2815 | case check_state_compute_result: |
| 2816 | sh->check_state = check_state_idle; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2817 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2818 | /* check that a write has not made the stripe insync */ |
| 2819 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 2820 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2821 | |
| 2822 | /* now write out any block on a failed drive, |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2823 | * or P or Q if they were recomputed |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2824 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2825 | 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] | 2826 | if (s->failed == 2) { |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2827 | dev = &sh->dev[s->failed_num[1]]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2828 | s->locked++; |
| 2829 | set_bit(R5_LOCKED, &dev->flags); |
| 2830 | set_bit(R5_Wantwrite, &dev->flags); |
| 2831 | } |
| 2832 | if (s->failed >= 1) { |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2833 | dev = &sh->dev[s->failed_num[0]]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2834 | s->locked++; |
| 2835 | set_bit(R5_LOCKED, &dev->flags); |
| 2836 | set_bit(R5_Wantwrite, &dev->flags); |
| 2837 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2838 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2839 | dev = &sh->dev[pd_idx]; |
| 2840 | s->locked++; |
| 2841 | set_bit(R5_LOCKED, &dev->flags); |
| 2842 | set_bit(R5_Wantwrite, &dev->flags); |
| 2843 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2844 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2845 | dev = &sh->dev[qd_idx]; |
| 2846 | s->locked++; |
| 2847 | set_bit(R5_LOCKED, &dev->flags); |
| 2848 | set_bit(R5_Wantwrite, &dev->flags); |
| 2849 | } |
| 2850 | clear_bit(STRIPE_DEGRADED, &sh->state); |
| 2851 | |
| 2852 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2853 | break; |
| 2854 | case check_state_run: |
| 2855 | case check_state_run_q: |
| 2856 | case check_state_run_pq: |
| 2857 | break; /* we will be called again upon completion */ |
| 2858 | case check_state_check_result: |
| 2859 | sh->check_state = check_state_idle; |
| 2860 | |
| 2861 | /* handle a successful check operation, if parity is correct |
| 2862 | * we are done. Otherwise update the mismatch count and repair |
| 2863 | * parity if !MD_RECOVERY_CHECK |
| 2864 | */ |
| 2865 | if (sh->ops.zero_sum_result == 0) { |
| 2866 | /* both parities are correct */ |
| 2867 | if (!s->failed) |
| 2868 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2869 | else { |
| 2870 | /* in contrast to the raid5 case we can validate |
| 2871 | * parity, but still have a failure to write |
| 2872 | * back |
| 2873 | */ |
| 2874 | sh->check_state = check_state_compute_result; |
| 2875 | /* Returning at this point means that we may go |
| 2876 | * off and bring p and/or q uptodate again so |
| 2877 | * we make sure to check zero_sum_result again |
| 2878 | * to verify if p or q need writeback |
| 2879 | */ |
| 2880 | } |
| 2881 | } else { |
| 2882 | conf->mddev->resync_mismatches += STRIPE_SECTORS; |
| 2883 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 2884 | /* don't try to repair!! */ |
| 2885 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2886 | else { |
| 2887 | int *target = &sh->ops.target; |
| 2888 | |
| 2889 | sh->ops.target = -1; |
| 2890 | sh->ops.target2 = -1; |
| 2891 | sh->check_state = check_state_compute_run; |
| 2892 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2893 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2894 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
| 2895 | set_bit(R5_Wantcompute, |
| 2896 | &sh->dev[pd_idx].flags); |
| 2897 | *target = pd_idx; |
| 2898 | target = &sh->ops.target2; |
| 2899 | s->uptodate++; |
| 2900 | } |
| 2901 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
| 2902 | set_bit(R5_Wantcompute, |
| 2903 | &sh->dev[qd_idx].flags); |
| 2904 | *target = qd_idx; |
| 2905 | s->uptodate++; |
| 2906 | } |
| 2907 | } |
| 2908 | } |
| 2909 | break; |
| 2910 | case check_state_compute_run: |
| 2911 | break; |
| 2912 | default: |
| 2913 | printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n", |
| 2914 | __func__, sh->check_state, |
| 2915 | (unsigned long long) sh->sector); |
| 2916 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2917 | } |
| 2918 | } |
| 2919 | |
NeilBrown | 86c374b | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2920 | static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2921 | { |
| 2922 | int i; |
| 2923 | |
| 2924 | /* We have read all the blocks in this stripe and now we need to |
| 2925 | * copy some of them into a target stripe for expand. |
| 2926 | */ |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2927 | struct dma_async_tx_descriptor *tx = NULL; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2928 | clear_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 2929 | for (i = 0; i < sh->disks; i++) |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 2930 | if (i != sh->pd_idx && i != sh->qd_idx) { |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2931 | int dd_idx, j; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2932 | struct stripe_head *sh2; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 2933 | struct async_submit_ctl submit; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2934 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2935 | sector_t bn = compute_blocknr(sh, i, 1); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2936 | sector_t s = raid5_compute_sector(conf, bn, 0, |
| 2937 | &dd_idx, NULL); |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 2938 | sh2 = get_active_stripe(conf, s, 0, 1, 1); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2939 | if (sh2 == NULL) |
| 2940 | /* so far only the early blocks of this stripe |
| 2941 | * have been requested. When later blocks |
| 2942 | * get requested, we will try again |
| 2943 | */ |
| 2944 | continue; |
| 2945 | if (!test_bit(STRIPE_EXPANDING, &sh2->state) || |
| 2946 | test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) { |
| 2947 | /* must have already done this block */ |
| 2948 | release_stripe(sh2); |
| 2949 | continue; |
| 2950 | } |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2951 | |
| 2952 | /* place all the copies on one channel */ |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 2953 | init_async_submit(&submit, 0, tx, NULL, NULL, NULL); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2954 | tx = async_memcpy(sh2->dev[dd_idx].page, |
Dan Williams | 88ba2aa | 2009-04-09 16:16:18 -0700 | [diff] [blame] | 2955 | sh->dev[i].page, 0, 0, STRIPE_SIZE, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 2956 | &submit); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2957 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2958 | set_bit(R5_Expanded, &sh2->dev[dd_idx].flags); |
| 2959 | set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags); |
| 2960 | for (j = 0; j < conf->raid_disks; j++) |
| 2961 | if (j != sh2->pd_idx && |
NeilBrown | 86c374b | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2962 | j != sh2->qd_idx && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2963 | !test_bit(R5_Expanded, &sh2->dev[j].flags)) |
| 2964 | break; |
| 2965 | if (j == conf->raid_disks) { |
| 2966 | set_bit(STRIPE_EXPAND_READY, &sh2->state); |
| 2967 | set_bit(STRIPE_HANDLE, &sh2->state); |
| 2968 | } |
| 2969 | release_stripe(sh2); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2970 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2971 | } |
NeilBrown | a2e0855 | 2007-09-11 15:23:36 -0700 | [diff] [blame] | 2972 | /* done submitting copies, wait for them to complete */ |
| 2973 | if (tx) { |
| 2974 | async_tx_ack(tx); |
| 2975 | dma_wait_for_async_tx(tx); |
| 2976 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2977 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2978 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2979 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2980 | /* |
| 2981 | * handle_stripe - do things to a stripe. |
| 2982 | * |
| 2983 | * We lock the stripe and then examine the state of various bits |
| 2984 | * to see what needs to be done. |
| 2985 | * Possible results: |
| 2986 | * return some read request which now have data |
| 2987 | * return some write requests which are safely on disc |
| 2988 | * schedule a read on some buffers |
| 2989 | * schedule a write of some buffers |
| 2990 | * return confirmation of parity correctness |
| 2991 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2992 | * buffers are taken off read_list or write_list, and bh_cache buffers |
| 2993 | * get BH_Lock set before the stripe lock is released. |
| 2994 | * |
| 2995 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2996 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2997 | static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2998 | { |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2999 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3000 | int disks = sh->disks; |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3001 | struct r5dev *dev; |
| 3002 | int i; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3003 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3004 | memset(s, 0, sizeof(*s)); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3005 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3006 | s->syncing = test_bit(STRIPE_SYNCING, &sh->state); |
| 3007 | s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 3008 | s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state); |
| 3009 | s->failed_num[0] = -1; |
| 3010 | s->failed_num[1] = -1; |
| 3011 | |
| 3012 | /* Now to look around and see what can be done */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3013 | rcu_read_lock(); |
NeilBrown | c4c1663 | 2011-07-26 11:34:20 +1000 | [diff] [blame] | 3014 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3015 | for (i=disks; i--; ) { |
| 3016 | mdk_rdev_t *rdev; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3017 | sector_t first_bad; |
| 3018 | int bad_sectors; |
| 3019 | int is_bad = 0; |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3020 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3021 | dev = &sh->dev[i]; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3022 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3023 | pr_debug("check %d: state 0x%lx read %p write %p written %p\n", |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3024 | i, dev->flags, dev->toread, dev->towrite, dev->written); |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3025 | /* maybe we can reply to a read |
| 3026 | * |
| 3027 | * new wantfill requests are only permitted while |
| 3028 | * ops_complete_biofill is guaranteed to be inactive |
| 3029 | */ |
| 3030 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread && |
| 3031 | !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) |
| 3032 | set_bit(R5_Wantfill, &dev->flags); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3033 | |
| 3034 | /* now count some things */ |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3035 | if (test_bit(R5_LOCKED, &dev->flags)) |
| 3036 | s->locked++; |
| 3037 | if (test_bit(R5_UPTODATE, &dev->flags)) |
| 3038 | s->uptodate++; |
Dan Williams | 2d6e4ec | 2009-09-16 12:11:54 -0700 | [diff] [blame] | 3039 | if (test_bit(R5_Wantcompute, &dev->flags)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3040 | s->compute++; |
| 3041 | BUG_ON(s->compute > 2); |
Dan Williams | 2d6e4ec | 2009-09-16 12:11:54 -0700 | [diff] [blame] | 3042 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3043 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3044 | if (test_bit(R5_Wantfill, &dev->flags)) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3045 | s->to_fill++; |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3046 | else if (dev->toread) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3047 | s->to_read++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3048 | if (dev->towrite) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3049 | s->to_write++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3050 | if (!test_bit(R5_OVERWRITE, &dev->flags)) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3051 | s->non_overwrite++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3052 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3053 | if (dev->written) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3054 | s->written++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3055 | rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3056 | if (rdev) { |
| 3057 | is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 3058 | &first_bad, &bad_sectors); |
| 3059 | if (s->blocked_rdev == NULL |
| 3060 | && (test_bit(Blocked, &rdev->flags) |
| 3061 | || is_bad < 0)) { |
| 3062 | if (is_bad < 0) |
| 3063 | set_bit(BlockedBadBlocks, |
| 3064 | &rdev->flags); |
| 3065 | s->blocked_rdev = rdev; |
| 3066 | atomic_inc(&rdev->nr_pending); |
| 3067 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3068 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3069 | clear_bit(R5_Insync, &dev->flags); |
| 3070 | if (!rdev) |
| 3071 | /* Not in-sync */; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3072 | else if (is_bad) { |
| 3073 | /* also not in-sync */ |
| 3074 | if (!test_bit(WriteErrorSeen, &rdev->flags)) { |
| 3075 | /* treat as in-sync, but with a read error |
| 3076 | * which we can now try to correct |
| 3077 | */ |
| 3078 | set_bit(R5_Insync, &dev->flags); |
| 3079 | set_bit(R5_ReadError, &dev->flags); |
| 3080 | } |
| 3081 | } else if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3082 | set_bit(R5_Insync, &dev->flags); |
| 3083 | else { |
| 3084 | /* in sync if before recovery_offset */ |
| 3085 | if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset) |
| 3086 | set_bit(R5_Insync, &dev->flags); |
| 3087 | } |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3088 | if (test_bit(R5_WriteError, &dev->flags)) { |
| 3089 | clear_bit(R5_Insync, &dev->flags); |
| 3090 | if (!test_bit(Faulty, &rdev->flags)) { |
| 3091 | s->handle_bad_blocks = 1; |
| 3092 | atomic_inc(&rdev->nr_pending); |
| 3093 | } else |
| 3094 | clear_bit(R5_WriteError, &dev->flags); |
| 3095 | } |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 3096 | if (test_bit(R5_MadeGood, &dev->flags)) { |
| 3097 | if (!test_bit(Faulty, &rdev->flags)) { |
| 3098 | s->handle_bad_blocks = 1; |
| 3099 | atomic_inc(&rdev->nr_pending); |
| 3100 | } else |
| 3101 | clear_bit(R5_MadeGood, &dev->flags); |
| 3102 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3103 | if (!test_bit(R5_Insync, &dev->flags)) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3104 | /* The ReadError flag will just be confusing now */ |
| 3105 | clear_bit(R5_ReadError, &dev->flags); |
| 3106 | clear_bit(R5_ReWrite, &dev->flags); |
| 3107 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3108 | if (test_bit(R5_ReadError, &dev->flags)) |
| 3109 | clear_bit(R5_Insync, &dev->flags); |
| 3110 | if (!test_bit(R5_Insync, &dev->flags)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3111 | if (s->failed < 2) |
| 3112 | s->failed_num[s->failed] = i; |
| 3113 | s->failed++; |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3114 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3115 | } |
NeilBrown | c4c1663 | 2011-07-26 11:34:20 +1000 | [diff] [blame] | 3116 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3117 | rcu_read_unlock(); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3118 | } |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3119 | |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3120 | static void handle_stripe(struct stripe_head *sh) |
| 3121 | { |
| 3122 | struct stripe_head_state s; |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3123 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3124 | int i; |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3125 | int prexor; |
| 3126 | int disks = sh->disks; |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3127 | struct r5dev *pdev, *qdev; |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3128 | |
| 3129 | clear_bit(STRIPE_HANDLE, &sh->state); |
| 3130 | if (test_and_set_bit(STRIPE_ACTIVE, &sh->state)) { |
| 3131 | /* already being handled, ensure it gets handled |
| 3132 | * again when current action finishes */ |
| 3133 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3134 | return; |
| 3135 | } |
| 3136 | |
| 3137 | if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) { |
| 3138 | set_bit(STRIPE_SYNCING, &sh->state); |
| 3139 | clear_bit(STRIPE_INSYNC, &sh->state); |
| 3140 | } |
| 3141 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 3142 | |
| 3143 | pr_debug("handling stripe %llu, state=%#lx cnt=%d, " |
| 3144 | "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n", |
| 3145 | (unsigned long long)sh->sector, sh->state, |
| 3146 | atomic_read(&sh->count), sh->pd_idx, sh->qd_idx, |
| 3147 | sh->check_state, sh->reconstruct_state); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3148 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3149 | analyse_stripe(sh, &s); |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3150 | |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3151 | if (s.handle_bad_blocks) { |
| 3152 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3153 | goto finish; |
| 3154 | } |
| 3155 | |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3156 | if (unlikely(s.blocked_rdev)) { |
| 3157 | if (s.syncing || s.expanding || s.expanded || |
| 3158 | s.to_write || s.written) { |
| 3159 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3160 | goto finish; |
| 3161 | } |
| 3162 | /* There is nothing for the blocked_rdev to block */ |
| 3163 | rdev_dec_pending(s.blocked_rdev, conf->mddev); |
| 3164 | s.blocked_rdev = NULL; |
| 3165 | } |
| 3166 | |
| 3167 | if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) { |
| 3168 | set_bit(STRIPE_OP_BIOFILL, &s.ops_request); |
| 3169 | set_bit(STRIPE_BIOFILL_RUN, &sh->state); |
| 3170 | } |
| 3171 | |
| 3172 | pr_debug("locked=%d uptodate=%d to_read=%d" |
| 3173 | " to_write=%d failed=%d failed_num=%d,%d\n", |
| 3174 | s.locked, s.uptodate, s.to_read, s.to_write, s.failed, |
| 3175 | s.failed_num[0], s.failed_num[1]); |
| 3176 | /* check if the array has lost more than max_degraded devices and, |
| 3177 | * if so, some requests might need to be failed. |
| 3178 | */ |
| 3179 | if (s.failed > conf->max_degraded && s.to_read+s.to_write+s.written) |
| 3180 | handle_failed_stripe(conf, sh, &s, disks, &s.return_bi); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3181 | if (s.failed > conf->max_degraded && s.syncing) |
| 3182 | handle_failed_sync(conf, sh, &s); |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3183 | |
| 3184 | /* |
| 3185 | * might be able to return some write requests if the parity blocks |
| 3186 | * are safe, or on a failed drive |
| 3187 | */ |
| 3188 | pdev = &sh->dev[sh->pd_idx]; |
| 3189 | s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx) |
| 3190 | || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx); |
| 3191 | qdev = &sh->dev[sh->qd_idx]; |
| 3192 | s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx) |
| 3193 | || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx) |
| 3194 | || conf->level < 6; |
| 3195 | |
| 3196 | if (s.written && |
| 3197 | (s.p_failed || ((test_bit(R5_Insync, &pdev->flags) |
| 3198 | && !test_bit(R5_LOCKED, &pdev->flags) |
| 3199 | && test_bit(R5_UPTODATE, &pdev->flags)))) && |
| 3200 | (s.q_failed || ((test_bit(R5_Insync, &qdev->flags) |
| 3201 | && !test_bit(R5_LOCKED, &qdev->flags) |
| 3202 | && test_bit(R5_UPTODATE, &qdev->flags))))) |
| 3203 | handle_stripe_clean_event(conf, sh, disks, &s.return_bi); |
| 3204 | |
| 3205 | /* Now we might consider reading some blocks, either to check/generate |
| 3206 | * parity, or to satisfy requests |
| 3207 | * or to load a block that is being partially written. |
| 3208 | */ |
| 3209 | if (s.to_read || s.non_overwrite |
| 3210 | || (conf->level == 6 && s.to_write && s.failed) |
| 3211 | || (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding) |
| 3212 | handle_stripe_fill(sh, &s, disks); |
| 3213 | |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3214 | /* Now we check to see if any write operations have recently |
| 3215 | * completed |
| 3216 | */ |
| 3217 | prexor = 0; |
| 3218 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_result) |
| 3219 | prexor = 1; |
| 3220 | if (sh->reconstruct_state == reconstruct_state_drain_result || |
| 3221 | sh->reconstruct_state == reconstruct_state_prexor_drain_result) { |
| 3222 | sh->reconstruct_state = reconstruct_state_idle; |
| 3223 | |
| 3224 | /* All the 'written' buffers and the parity block are ready to |
| 3225 | * be written back to disk |
| 3226 | */ |
| 3227 | BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags)); |
| 3228 | BUG_ON(sh->qd_idx >= 0 && |
| 3229 | !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags)); |
| 3230 | for (i = disks; i--; ) { |
| 3231 | struct r5dev *dev = &sh->dev[i]; |
| 3232 | if (test_bit(R5_LOCKED, &dev->flags) && |
| 3233 | (i == sh->pd_idx || i == sh->qd_idx || |
| 3234 | dev->written)) { |
| 3235 | pr_debug("Writing block %d\n", i); |
| 3236 | set_bit(R5_Wantwrite, &dev->flags); |
| 3237 | if (prexor) |
| 3238 | continue; |
| 3239 | if (!test_bit(R5_Insync, &dev->flags) || |
| 3240 | ((i == sh->pd_idx || i == sh->qd_idx) && |
| 3241 | s.failed == 0)) |
| 3242 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3243 | } |
| 3244 | } |
| 3245 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 3246 | s.dec_preread_active = 1; |
| 3247 | } |
| 3248 | |
| 3249 | /* Now to consider new write requests and what else, if anything |
| 3250 | * should be read. We do not handle new writes when: |
| 3251 | * 1/ A 'write' operation (copy+xor) is already in flight. |
| 3252 | * 2/ A 'check' operation is in flight, as it may clobber the parity |
| 3253 | * block. |
| 3254 | */ |
| 3255 | if (s.to_write && !sh->reconstruct_state && !sh->check_state) |
| 3256 | handle_stripe_dirtying(conf, sh, &s, disks); |
| 3257 | |
| 3258 | /* maybe we need to check and possibly fix the parity for this stripe |
| 3259 | * Any reads will already have been scheduled, so we just see if enough |
| 3260 | * data is available. The parity check is held off while parity |
| 3261 | * dependent operations are in flight. |
| 3262 | */ |
| 3263 | if (sh->check_state || |
| 3264 | (s.syncing && s.locked == 0 && |
| 3265 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && |
| 3266 | !test_bit(STRIPE_INSYNC, &sh->state))) { |
| 3267 | if (conf->level == 6) |
| 3268 | handle_parity_checks6(conf, sh, &s, disks); |
| 3269 | else |
| 3270 | handle_parity_checks5(conf, sh, &s, disks); |
| 3271 | } |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3272 | |
| 3273 | if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) { |
| 3274 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 3275 | clear_bit(STRIPE_SYNCING, &sh->state); |
| 3276 | } |
| 3277 | |
| 3278 | /* If the failed drives are just a ReadError, then we might need |
| 3279 | * to progress the repair/check process |
| 3280 | */ |
| 3281 | if (s.failed <= conf->max_degraded && !conf->mddev->ro) |
| 3282 | for (i = 0; i < s.failed; i++) { |
| 3283 | struct r5dev *dev = &sh->dev[s.failed_num[i]]; |
| 3284 | if (test_bit(R5_ReadError, &dev->flags) |
| 3285 | && !test_bit(R5_LOCKED, &dev->flags) |
| 3286 | && test_bit(R5_UPTODATE, &dev->flags) |
| 3287 | ) { |
| 3288 | if (!test_bit(R5_ReWrite, &dev->flags)) { |
| 3289 | set_bit(R5_Wantwrite, &dev->flags); |
| 3290 | set_bit(R5_ReWrite, &dev->flags); |
| 3291 | set_bit(R5_LOCKED, &dev->flags); |
| 3292 | s.locked++; |
| 3293 | } else { |
| 3294 | /* let's read it back */ |
| 3295 | set_bit(R5_Wantread, &dev->flags); |
| 3296 | set_bit(R5_LOCKED, &dev->flags); |
| 3297 | s.locked++; |
| 3298 | } |
| 3299 | } |
| 3300 | } |
| 3301 | |
| 3302 | |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3303 | /* Finish reconstruct operations initiated by the expansion process */ |
| 3304 | if (sh->reconstruct_state == reconstruct_state_result) { |
| 3305 | struct stripe_head *sh_src |
| 3306 | = get_active_stripe(conf, sh->sector, 1, 1, 1); |
| 3307 | if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) { |
| 3308 | /* sh cannot be written until sh_src has been read. |
| 3309 | * so arrange for sh to be delayed a little |
| 3310 | */ |
| 3311 | set_bit(STRIPE_DELAYED, &sh->state); |
| 3312 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3313 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, |
| 3314 | &sh_src->state)) |
| 3315 | atomic_inc(&conf->preread_active_stripes); |
| 3316 | release_stripe(sh_src); |
| 3317 | goto finish; |
| 3318 | } |
| 3319 | if (sh_src) |
| 3320 | release_stripe(sh_src); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3321 | |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3322 | sh->reconstruct_state = reconstruct_state_idle; |
| 3323 | clear_bit(STRIPE_EXPANDING, &sh->state); |
| 3324 | for (i = conf->raid_disks; i--; ) { |
| 3325 | set_bit(R5_Wantwrite, &sh->dev[i].flags); |
| 3326 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 3327 | s.locked++; |
| 3328 | } |
| 3329 | } |
| 3330 | |
| 3331 | if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) && |
| 3332 | !sh->reconstruct_state) { |
| 3333 | /* Need to write out all blocks after computing parity */ |
| 3334 | sh->disks = conf->raid_disks; |
| 3335 | stripe_set_idx(sh->sector, conf, 0, sh); |
| 3336 | schedule_reconstruction(sh, &s, 1, 1); |
| 3337 | } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) { |
| 3338 | clear_bit(STRIPE_EXPAND_READY, &sh->state); |
| 3339 | atomic_dec(&conf->reshape_stripes); |
| 3340 | wake_up(&conf->wait_for_overlap); |
| 3341 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 3342 | } |
| 3343 | |
| 3344 | if (s.expanding && s.locked == 0 && |
| 3345 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) |
| 3346 | handle_stripe_expansion(conf, sh); |
| 3347 | |
| 3348 | finish: |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3349 | /* wait for this device to become unblocked */ |
NeilBrown | 43220aa | 2011-08-31 12:49:14 +1000 | [diff] [blame] | 3350 | if (conf->mddev->external && unlikely(s.blocked_rdev)) |
NeilBrown | c5709ef | 2011-07-26 11:35:20 +1000 | [diff] [blame] | 3351 | md_wait_for_blocked_rdev(s.blocked_rdev, conf->mddev); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3352 | |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3353 | if (s.handle_bad_blocks) |
| 3354 | for (i = disks; i--; ) { |
| 3355 | mdk_rdev_t *rdev; |
| 3356 | struct r5dev *dev = &sh->dev[i]; |
| 3357 | if (test_and_clear_bit(R5_WriteError, &dev->flags)) { |
| 3358 | /* We own a safe reference to the rdev */ |
| 3359 | rdev = conf->disks[i].rdev; |
| 3360 | if (!rdev_set_badblocks(rdev, sh->sector, |
| 3361 | STRIPE_SECTORS, 0)) |
| 3362 | md_error(conf->mddev, rdev); |
| 3363 | rdev_dec_pending(rdev, conf->mddev); |
| 3364 | } |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 3365 | if (test_and_clear_bit(R5_MadeGood, &dev->flags)) { |
| 3366 | rdev = conf->disks[i].rdev; |
| 3367 | rdev_clear_badblocks(rdev, sh->sector, |
| 3368 | STRIPE_SECTORS); |
| 3369 | rdev_dec_pending(rdev, conf->mddev); |
| 3370 | } |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3371 | } |
| 3372 | |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3373 | if (s.ops_request) |
| 3374 | raid_run_ops(sh, s.ops_request); |
| 3375 | |
Dan Williams | f0e43bc | 2008-06-28 08:31:55 +1000 | [diff] [blame] | 3376 | ops_run_io(sh, &s); |
| 3377 | |
NeilBrown | c5709ef | 2011-07-26 11:35:20 +1000 | [diff] [blame] | 3378 | if (s.dec_preread_active) { |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 3379 | /* 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] | 3380 | * is waiting on a flush, it won't continue until the writes |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 3381 | * have actually been submitted. |
| 3382 | */ |
| 3383 | atomic_dec(&conf->preread_active_stripes); |
| 3384 | if (atomic_read(&conf->preread_active_stripes) < |
| 3385 | IO_THRESHOLD) |
| 3386 | md_wakeup_thread(conf->mddev->thread); |
| 3387 | } |
| 3388 | |
NeilBrown | c5709ef | 2011-07-26 11:35:20 +1000 | [diff] [blame] | 3389 | return_io(s.return_bi); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3390 | |
NeilBrown | c4c1663 | 2011-07-26 11:34:20 +1000 | [diff] [blame] | 3391 | clear_bit(STRIPE_ACTIVE, &sh->state); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3392 | } |
| 3393 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 3394 | static void raid5_activate_delayed(raid5_conf_t *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3395 | { |
| 3396 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) { |
| 3397 | while (!list_empty(&conf->delayed_list)) { |
| 3398 | struct list_head *l = conf->delayed_list.next; |
| 3399 | struct stripe_head *sh; |
| 3400 | sh = list_entry(l, struct stripe_head, lru); |
| 3401 | list_del_init(l); |
| 3402 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 3403 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 3404 | atomic_inc(&conf->preread_active_stripes); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3405 | list_add_tail(&sh->lru, &conf->hold_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3406 | } |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 3407 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3408 | } |
| 3409 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 3410 | static void activate_bit_delay(raid5_conf_t *conf) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3411 | { |
| 3412 | /* device_lock is held */ |
| 3413 | struct list_head head; |
| 3414 | list_add(&head, &conf->bitmap_list); |
| 3415 | list_del_init(&conf->bitmap_list); |
| 3416 | while (!list_empty(&head)) { |
| 3417 | struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru); |
| 3418 | list_del_init(&sh->lru); |
| 3419 | atomic_inc(&sh->count); |
| 3420 | __release_stripe(conf, sh); |
| 3421 | } |
| 3422 | } |
| 3423 | |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 3424 | int md_raid5_congested(mddev_t *mddev, int bits) |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3425 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 3426 | raid5_conf_t *conf = mddev->private; |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3427 | |
| 3428 | /* No difference between reads and writes. Just check |
| 3429 | * how busy the stripe_cache is |
| 3430 | */ |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 3431 | |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3432 | if (conf->inactive_blocked) |
| 3433 | return 1; |
| 3434 | if (conf->quiesce) |
| 3435 | return 1; |
| 3436 | if (list_empty_careful(&conf->inactive_list)) |
| 3437 | return 1; |
| 3438 | |
| 3439 | return 0; |
| 3440 | } |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 3441 | EXPORT_SYMBOL_GPL(md_raid5_congested); |
| 3442 | |
| 3443 | static int raid5_congested(void *data, int bits) |
| 3444 | { |
| 3445 | mddev_t *mddev = data; |
| 3446 | |
| 3447 | return mddev_congested(mddev, bits) || |
| 3448 | md_raid5_congested(mddev, bits); |
| 3449 | } |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3450 | |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3451 | /* We want read requests to align with chunks where possible, |
| 3452 | * but write requests don't need to. |
| 3453 | */ |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3454 | static int raid5_mergeable_bvec(struct request_queue *q, |
| 3455 | struct bvec_merge_data *bvm, |
| 3456 | struct bio_vec *biovec) |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3457 | { |
| 3458 | mddev_t *mddev = q->queuedata; |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3459 | 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] | 3460 | int max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3461 | unsigned int chunk_sectors = mddev->chunk_sectors; |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3462 | unsigned int bio_sectors = bvm->bi_size >> 9; |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3463 | |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3464 | if ((bvm->bi_rw & 1) == WRITE) |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3465 | return biovec->bv_len; /* always allow writes to be mergeable */ |
| 3466 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3467 | if (mddev->new_chunk_sectors < mddev->chunk_sectors) |
| 3468 | chunk_sectors = mddev->new_chunk_sectors; |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3469 | max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9; |
| 3470 | if (max < 0) max = 0; |
| 3471 | if (max <= biovec->bv_len && bio_sectors == 0) |
| 3472 | return biovec->bv_len; |
| 3473 | else |
| 3474 | return max; |
| 3475 | } |
| 3476 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3477 | |
| 3478 | static int in_chunk_boundary(mddev_t *mddev, struct bio *bio) |
| 3479 | { |
| 3480 | sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3481 | unsigned int chunk_sectors = mddev->chunk_sectors; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3482 | unsigned int bio_sectors = bio->bi_size >> 9; |
| 3483 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3484 | if (mddev->new_chunk_sectors < mddev->chunk_sectors) |
| 3485 | chunk_sectors = mddev->new_chunk_sectors; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3486 | return chunk_sectors >= |
| 3487 | ((sector & (chunk_sectors - 1)) + bio_sectors); |
| 3488 | } |
| 3489 | |
| 3490 | /* |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3491 | * add bio to the retry LIFO ( in O(1) ... we are in interrupt ) |
| 3492 | * later sampled by raid5d. |
| 3493 | */ |
| 3494 | static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf) |
| 3495 | { |
| 3496 | unsigned long flags; |
| 3497 | |
| 3498 | spin_lock_irqsave(&conf->device_lock, flags); |
| 3499 | |
| 3500 | bi->bi_next = conf->retry_read_aligned_list; |
| 3501 | conf->retry_read_aligned_list = bi; |
| 3502 | |
| 3503 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 3504 | md_wakeup_thread(conf->mddev->thread); |
| 3505 | } |
| 3506 | |
| 3507 | |
| 3508 | static struct bio *remove_bio_from_retry(raid5_conf_t *conf) |
| 3509 | { |
| 3510 | struct bio *bi; |
| 3511 | |
| 3512 | bi = conf->retry_read_aligned; |
| 3513 | if (bi) { |
| 3514 | conf->retry_read_aligned = NULL; |
| 3515 | return bi; |
| 3516 | } |
| 3517 | bi = conf->retry_read_aligned_list; |
| 3518 | if(bi) { |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3519 | conf->retry_read_aligned_list = bi->bi_next; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3520 | bi->bi_next = NULL; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 3521 | /* |
| 3522 | * this sets the active strip count to 1 and the processed |
| 3523 | * strip count to zero (upper 8 bits) |
| 3524 | */ |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3525 | bi->bi_phys_segments = 1; /* biased count of active stripes */ |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3526 | } |
| 3527 | |
| 3528 | return bi; |
| 3529 | } |
| 3530 | |
| 3531 | |
| 3532 | /* |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3533 | * The "raid5_align_endio" should check if the read succeeded and if it |
| 3534 | * did, call bio_endio on the original bio (having bio_put the new bio |
| 3535 | * first). |
| 3536 | * If the read failed.. |
| 3537 | */ |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3538 | static void raid5_align_endio(struct bio *bi, int error) |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3539 | { |
| 3540 | struct bio* raid_bi = bi->bi_private; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3541 | mddev_t *mddev; |
| 3542 | raid5_conf_t *conf; |
| 3543 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
| 3544 | mdk_rdev_t *rdev; |
| 3545 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3546 | bio_put(bi); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3547 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3548 | rdev = (void*)raid_bi->bi_next; |
| 3549 | raid_bi->bi_next = NULL; |
NeilBrown | 2b7f222 | 2010-03-25 16:06:03 +1100 | [diff] [blame] | 3550 | mddev = rdev->mddev; |
| 3551 | conf = mddev->private; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3552 | |
| 3553 | rdev_dec_pending(rdev, conf->mddev); |
| 3554 | |
| 3555 | if (!error && uptodate) { |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3556 | bio_endio(raid_bi, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3557 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
| 3558 | wake_up(&conf->wait_for_stripe); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3559 | return; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3560 | } |
| 3561 | |
| 3562 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3563 | 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] | 3564 | |
| 3565 | add_bio_to_retry(raid_bi, conf); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3566 | } |
| 3567 | |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3568 | static int bio_fits_rdev(struct bio *bi) |
| 3569 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3570 | struct request_queue *q = bdev_get_queue(bi->bi_bdev); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3571 | |
Martin K. Petersen | ae03bf6 | 2009-05-22 17:17:50 -0400 | [diff] [blame] | 3572 | if ((bi->bi_size>>9) > queue_max_sectors(q)) |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3573 | return 0; |
| 3574 | blk_recount_segments(q, bi); |
Martin K. Petersen | 8a78362 | 2010-02-26 00:20:39 -0500 | [diff] [blame] | 3575 | if (bi->bi_phys_segments > queue_max_segments(q)) |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3576 | return 0; |
| 3577 | |
| 3578 | if (q->merge_bvec_fn) |
| 3579 | /* it's too hard to apply the merge_bvec_fn at this stage, |
| 3580 | * just just give up |
| 3581 | */ |
| 3582 | return 0; |
| 3583 | |
| 3584 | return 1; |
| 3585 | } |
| 3586 | |
| 3587 | |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 3588 | static int chunk_aligned_read(mddev_t *mddev, struct bio * raid_bio) |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3589 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 3590 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 8553fe7ec | 2009-12-14 12:49:47 +1100 | [diff] [blame] | 3591 | int dd_idx; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3592 | struct bio* align_bi; |
| 3593 | mdk_rdev_t *rdev; |
| 3594 | |
| 3595 | if (!in_chunk_boundary(mddev, raid_bio)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3596 | pr_debug("chunk_aligned_read : non aligned\n"); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3597 | return 0; |
| 3598 | } |
| 3599 | /* |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 3600 | * 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] | 3601 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 3602 | align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3603 | if (!align_bi) |
| 3604 | return 0; |
| 3605 | /* |
| 3606 | * set bi_end_io to a new function, and set bi_private to the |
| 3607 | * original bio. |
| 3608 | */ |
| 3609 | align_bi->bi_end_io = raid5_align_endio; |
| 3610 | align_bi->bi_private = raid_bio; |
| 3611 | /* |
| 3612 | * compute position |
| 3613 | */ |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3614 | align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector, |
| 3615 | 0, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3616 | &dd_idx, NULL); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3617 | |
| 3618 | rcu_read_lock(); |
| 3619 | rdev = rcu_dereference(conf->disks[dd_idx].rdev); |
| 3620 | if (rdev && test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3621 | sector_t first_bad; |
| 3622 | int bad_sectors; |
| 3623 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3624 | atomic_inc(&rdev->nr_pending); |
| 3625 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3626 | raid_bio->bi_next = (void*)rdev; |
| 3627 | align_bi->bi_bdev = rdev->bdev; |
| 3628 | align_bi->bi_flags &= ~(1 << BIO_SEG_VALID); |
| 3629 | align_bi->bi_sector += rdev->data_offset; |
| 3630 | |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3631 | if (!bio_fits_rdev(align_bi) || |
| 3632 | is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9, |
| 3633 | &first_bad, &bad_sectors)) { |
| 3634 | /* too big in some way, or has a known bad block */ |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3635 | bio_put(align_bi); |
| 3636 | rdev_dec_pending(rdev, mddev); |
| 3637 | return 0; |
| 3638 | } |
| 3639 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3640 | spin_lock_irq(&conf->device_lock); |
| 3641 | wait_event_lock_irq(conf->wait_for_stripe, |
| 3642 | conf->quiesce == 0, |
| 3643 | conf->device_lock, /* nothing */); |
| 3644 | atomic_inc(&conf->active_aligned_reads); |
| 3645 | spin_unlock_irq(&conf->device_lock); |
| 3646 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3647 | generic_make_request(align_bi); |
| 3648 | return 1; |
| 3649 | } else { |
| 3650 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3651 | bio_put(align_bi); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3652 | return 0; |
| 3653 | } |
| 3654 | } |
| 3655 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3656 | /* __get_priority_stripe - get the next stripe to process |
| 3657 | * |
| 3658 | * Full stripe writes are allowed to pass preread active stripes up until |
| 3659 | * the bypass_threshold is exceeded. In general the bypass_count |
| 3660 | * increments when the handle_list is handled before the hold_list; however, it |
| 3661 | * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a |
| 3662 | * stripe with in flight i/o. The bypass_count will be reset when the |
| 3663 | * head of the hold_list has changed, i.e. the head was promoted to the |
| 3664 | * handle_list. |
| 3665 | */ |
| 3666 | static struct stripe_head *__get_priority_stripe(raid5_conf_t *conf) |
| 3667 | { |
| 3668 | struct stripe_head *sh; |
| 3669 | |
| 3670 | pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n", |
| 3671 | __func__, |
| 3672 | list_empty(&conf->handle_list) ? "empty" : "busy", |
| 3673 | list_empty(&conf->hold_list) ? "empty" : "busy", |
| 3674 | atomic_read(&conf->pending_full_writes), conf->bypass_count); |
| 3675 | |
| 3676 | if (!list_empty(&conf->handle_list)) { |
| 3677 | sh = list_entry(conf->handle_list.next, typeof(*sh), lru); |
| 3678 | |
| 3679 | if (list_empty(&conf->hold_list)) |
| 3680 | conf->bypass_count = 0; |
| 3681 | else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) { |
| 3682 | if (conf->hold_list.next == conf->last_hold) |
| 3683 | conf->bypass_count++; |
| 3684 | else { |
| 3685 | conf->last_hold = conf->hold_list.next; |
| 3686 | conf->bypass_count -= conf->bypass_threshold; |
| 3687 | if (conf->bypass_count < 0) |
| 3688 | conf->bypass_count = 0; |
| 3689 | } |
| 3690 | } |
| 3691 | } else if (!list_empty(&conf->hold_list) && |
| 3692 | ((conf->bypass_threshold && |
| 3693 | conf->bypass_count > conf->bypass_threshold) || |
| 3694 | atomic_read(&conf->pending_full_writes) == 0)) { |
| 3695 | sh = list_entry(conf->hold_list.next, |
| 3696 | typeof(*sh), lru); |
| 3697 | conf->bypass_count -= conf->bypass_threshold; |
| 3698 | if (conf->bypass_count < 0) |
| 3699 | conf->bypass_count = 0; |
| 3700 | } else |
| 3701 | return NULL; |
| 3702 | |
| 3703 | list_del_init(&sh->lru); |
| 3704 | atomic_inc(&sh->count); |
| 3705 | BUG_ON(atomic_read(&sh->count) != 1); |
| 3706 | return sh; |
| 3707 | } |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3708 | |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 3709 | static int make_request(mddev_t *mddev, struct bio * bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3710 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 3711 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3712 | int dd_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3713 | sector_t new_sector; |
| 3714 | sector_t logical_sector, last_sector; |
| 3715 | struct stripe_head *sh; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 3716 | const int rw = bio_data_dir(bi); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 3717 | int remaining; |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 3718 | int plugged; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3719 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 3720 | if (unlikely(bi->bi_rw & REQ_FLUSH)) { |
| 3721 | md_flush_request(mddev, bi); |
NeilBrown | e5dcdd8 | 2005-09-09 16:23:41 -0700 | [diff] [blame] | 3722 | return 0; |
| 3723 | } |
| 3724 | |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 3725 | md_write_start(mddev, bi); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 3726 | |
NeilBrown | 802ba06 | 2006-12-13 00:34:13 -0800 | [diff] [blame] | 3727 | if (rw == READ && |
Raz Ben-Jehuda(caro) | 5248861 | 2006-12-10 02:20:48 -0800 | [diff] [blame] | 3728 | mddev->reshape_position == MaxSector && |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 3729 | chunk_aligned_read(mddev,bi)) |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3730 | return 0; |
Raz Ben-Jehuda(caro) | 5248861 | 2006-12-10 02:20:48 -0800 | [diff] [blame] | 3731 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3732 | logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
| 3733 | last_sector = bi->bi_sector + (bi->bi_size>>9); |
| 3734 | bi->bi_next = NULL; |
| 3735 | bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 3736 | |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 3737 | plugged = mddev_check_plugged(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3738 | for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) { |
| 3739 | DEFINE_WAIT(w); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3740 | int disks, data_disks; |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3741 | int previous; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 3742 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3743 | retry: |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3744 | previous = 0; |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 3745 | disks = conf->raid_disks; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 3746 | prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 3747 | if (unlikely(conf->reshape_progress != MaxSector)) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3748 | /* spinlock is needed as reshape_progress may be |
NeilBrown | df8e7f76 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 3749 | * 64bit on a 32bit platform, and so it might be |
| 3750 | * possible to see a half-updated value |
Jesper Juhl | aeb878b | 2011-04-10 18:06:17 +0200 | [diff] [blame] | 3751 | * Of course reshape_progress could change after |
NeilBrown | df8e7f76 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 3752 | * the lock is dropped, so once we get a reference |
| 3753 | * to the stripe that we think it is, we will have |
| 3754 | * to check again. |
| 3755 | */ |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3756 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3757 | if (mddev->delta_disks < 0 |
| 3758 | ? logical_sector < conf->reshape_progress |
| 3759 | : logical_sector >= conf->reshape_progress) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3760 | disks = conf->previous_raid_disks; |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3761 | previous = 1; |
| 3762 | } else { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3763 | if (mddev->delta_disks < 0 |
| 3764 | ? logical_sector < conf->reshape_safe |
| 3765 | : logical_sector >= conf->reshape_safe) { |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 3766 | spin_unlock_irq(&conf->device_lock); |
| 3767 | schedule(); |
| 3768 | goto retry; |
| 3769 | } |
| 3770 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3771 | spin_unlock_irq(&conf->device_lock); |
| 3772 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3773 | data_disks = disks - conf->max_degraded; |
| 3774 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3775 | new_sector = raid5_compute_sector(conf, logical_sector, |
| 3776 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3777 | &dd_idx, NULL); |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 3778 | pr_debug("raid456: make_request, sector %llu logical %llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3779 | (unsigned long long)new_sector, |
| 3780 | (unsigned long long)logical_sector); |
| 3781 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3782 | sh = get_active_stripe(conf, new_sector, previous, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 3783 | (bi->bi_rw&RWA_MASK), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3784 | if (sh) { |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 3785 | if (unlikely(previous)) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3786 | /* expansion might have moved on while waiting for a |
NeilBrown | df8e7f76 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 3787 | * stripe, so we must do the range check again. |
| 3788 | * Expansion could still move past after this |
| 3789 | * test, but as we are holding a reference to |
| 3790 | * 'sh', we know that if that happens, |
| 3791 | * STRIPE_EXPANDING will get set and the expansion |
| 3792 | * won't proceed until we finish with the stripe. |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3793 | */ |
| 3794 | int must_retry = 0; |
| 3795 | spin_lock_irq(&conf->device_lock); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 3796 | if (mddev->delta_disks < 0 |
| 3797 | ? logical_sector >= conf->reshape_progress |
| 3798 | : logical_sector < conf->reshape_progress) |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3799 | /* mismatch, need to try again */ |
| 3800 | must_retry = 1; |
| 3801 | spin_unlock_irq(&conf->device_lock); |
| 3802 | if (must_retry) { |
| 3803 | release_stripe(sh); |
Dan Williams | 7a3ab90 | 2009-06-16 16:00:33 -0700 | [diff] [blame] | 3804 | schedule(); |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3805 | goto retry; |
| 3806 | } |
| 3807 | } |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 3808 | |
Namhyung Kim | ffd96e3 | 2011-07-18 17:38:51 +1000 | [diff] [blame] | 3809 | if (rw == WRITE && |
NeilBrown | a5c308d | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 3810 | logical_sector >= mddev->suspend_lo && |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3811 | logical_sector < mddev->suspend_hi) { |
| 3812 | release_stripe(sh); |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 3813 | /* As the suspend_* range is controlled by |
| 3814 | * userspace, we want an interruptible |
| 3815 | * wait. |
| 3816 | */ |
| 3817 | flush_signals(current); |
| 3818 | prepare_to_wait(&conf->wait_for_overlap, |
| 3819 | &w, TASK_INTERRUPTIBLE); |
| 3820 | if (logical_sector >= mddev->suspend_lo && |
| 3821 | logical_sector < mddev->suspend_hi) |
| 3822 | schedule(); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3823 | goto retry; |
| 3824 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3825 | |
| 3826 | if (test_bit(STRIPE_EXPANDING, &sh->state) || |
Namhyung Kim | ffd96e3 | 2011-07-18 17:38:51 +1000 | [diff] [blame] | 3827 | !add_stripe_bio(sh, bi, dd_idx, rw)) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3828 | /* Stripe is busy expanding or |
| 3829 | * add failed due to overlap. Flush everything |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3830 | * and wait a while |
| 3831 | */ |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 3832 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3833 | release_stripe(sh); |
| 3834 | schedule(); |
| 3835 | goto retry; |
| 3836 | } |
| 3837 | finish_wait(&conf->wait_for_overlap, &w); |
NeilBrown | 6ed3003 | 2008-02-06 01:40:00 -0800 | [diff] [blame] | 3838 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3839 | clear_bit(STRIPE_DELAYED, &sh->state); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 3840 | if ((bi->bi_rw & REQ_SYNC) && |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 3841 | !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 3842 | atomic_inc(&conf->preread_active_stripes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3843 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3844 | } else { |
| 3845 | /* cannot get stripe for read-ahead, just give-up */ |
| 3846 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
| 3847 | finish_wait(&conf->wait_for_overlap, &w); |
| 3848 | break; |
| 3849 | } |
| 3850 | |
| 3851 | } |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 3852 | if (!plugged) |
| 3853 | md_wakeup_thread(mddev->thread); |
| 3854 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3855 | spin_lock_irq(&conf->device_lock); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 3856 | remaining = raid5_dec_bi_phys_segments(bi); |
NeilBrown | f634475 | 2006-03-27 01:18:17 -0800 | [diff] [blame] | 3857 | spin_unlock_irq(&conf->device_lock); |
| 3858 | if (remaining == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3859 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3860 | if ( rw == WRITE ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3861 | md_write_end(mddev); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3862 | |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 3863 | bio_endio(bi, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3864 | } |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 3865 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3866 | return 0; |
| 3867 | } |
| 3868 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3869 | static sector_t raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks); |
| 3870 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3871 | static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3872 | { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3873 | /* reshaping is quite different to recovery/resync so it is |
| 3874 | * handled quite separately ... here. |
| 3875 | * |
| 3876 | * On each call to sync_request, we gather one chunk worth of |
| 3877 | * destination stripes and flag them as expanding. |
| 3878 | * Then we find all the source stripes and request reads. |
| 3879 | * As the reads complete, handle_stripe will copy the data |
| 3880 | * into the destination stripe and release that stripe. |
| 3881 | */ |
H Hartley Sweeten | 7b92813 | 2010-03-08 16:02:40 +1100 | [diff] [blame] | 3882 | raid5_conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3883 | struct stripe_head *sh; |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3884 | sector_t first_sector, last_sector; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3885 | int raid_disks = conf->previous_raid_disks; |
| 3886 | int data_disks = raid_disks - conf->max_degraded; |
| 3887 | int new_data_disks = conf->raid_disks - conf->max_degraded; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3888 | int i; |
| 3889 | int dd_idx; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 3890 | sector_t writepos, readpos, safepos; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 3891 | sector_t stripe_addr; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 3892 | int reshape_sectors; |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 3893 | struct list_head stripes; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3894 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3895 | if (sector_nr == 0) { |
| 3896 | /* If restarting in the middle, skip the initial sectors */ |
| 3897 | if (mddev->delta_disks < 0 && |
| 3898 | conf->reshape_progress < raid5_size(mddev, 0, 0)) { |
| 3899 | sector_nr = raid5_size(mddev, 0, 0) |
| 3900 | - conf->reshape_progress; |
NeilBrown | a639755 | 2009-08-13 10:13:00 +1000 | [diff] [blame] | 3901 | } else if (mddev->delta_disks >= 0 && |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3902 | conf->reshape_progress > 0) |
| 3903 | sector_nr = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3904 | sector_div(sector_nr, new_data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3905 | if (sector_nr) { |
NeilBrown | 8dee721 | 2009-11-06 14:59:29 +1100 | [diff] [blame] | 3906 | mddev->curr_resync_completed = sector_nr; |
| 3907 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3908 | *skipped = 1; |
| 3909 | return sector_nr; |
| 3910 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3911 | } |
| 3912 | |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 3913 | /* We need to process a full chunk at a time. |
| 3914 | * If old and new chunk sizes differ, we need to process the |
| 3915 | * largest of these |
| 3916 | */ |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3917 | if (mddev->new_chunk_sectors > mddev->chunk_sectors) |
| 3918 | reshape_sectors = mddev->new_chunk_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 3919 | else |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3920 | reshape_sectors = mddev->chunk_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 3921 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3922 | /* we update the metadata when there is more than 3Meg |
| 3923 | * in the block range (that is rather arbitrary, should |
| 3924 | * probably be time based) or when the data about to be |
| 3925 | * copied would over-write the source of the data at |
| 3926 | * the front of the range. |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3927 | * i.e. one new_stripe along from reshape_progress new_maps |
| 3928 | * to after where reshape_safe old_maps to |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3929 | */ |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3930 | writepos = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3931 | sector_div(writepos, new_data_disks); |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 3932 | readpos = conf->reshape_progress; |
| 3933 | sector_div(readpos, data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3934 | safepos = conf->reshape_safe; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3935 | sector_div(safepos, data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3936 | if (mddev->delta_disks < 0) { |
NeilBrown | ed37d83 | 2009-05-27 21:39:05 +1000 | [diff] [blame] | 3937 | writepos -= min_t(sector_t, reshape_sectors, writepos); |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 3938 | readpos += reshape_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 3939 | safepos += reshape_sectors; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3940 | } else { |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 3941 | writepos += reshape_sectors; |
NeilBrown | ed37d83 | 2009-05-27 21:39:05 +1000 | [diff] [blame] | 3942 | readpos -= min_t(sector_t, reshape_sectors, readpos); |
| 3943 | safepos -= min_t(sector_t, reshape_sectors, safepos); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3944 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3945 | |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 3946 | /* 'writepos' is the most advanced device address we might write. |
| 3947 | * 'readpos' is the least advanced device address we might read. |
| 3948 | * 'safepos' is the least address recorded in the metadata as having |
| 3949 | * been reshaped. |
| 3950 | * If 'readpos' is behind 'writepos', then there is no way that we can |
| 3951 | * ensure safety in the face of a crash - that must be done by userspace |
| 3952 | * making a backup of the data. So in that case there is no particular |
| 3953 | * rush to update metadata. |
| 3954 | * Otherwise if 'safepos' is behind 'writepos', then we really need to |
| 3955 | * update the metadata to advance 'safepos' to match 'readpos' so that |
| 3956 | * we can be safe in the event of a crash. |
| 3957 | * So we insist on updating metadata if safepos is behind writepos and |
| 3958 | * readpos is beyond writepos. |
| 3959 | * In any case, update the metadata every 10 seconds. |
| 3960 | * Maybe that number should be configurable, but I'm not sure it is |
| 3961 | * worth it.... maybe it could be a multiple of safemode_delay??? |
| 3962 | */ |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3963 | if ((mddev->delta_disks < 0 |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 3964 | ? (safepos > writepos && readpos < writepos) |
| 3965 | : (safepos < writepos && readpos > writepos)) || |
| 3966 | time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3967 | /* Cannot proceed until we've updated the superblock... */ |
| 3968 | wait_event(conf->wait_for_overlap, |
| 3969 | atomic_read(&conf->reshape_stripes)==0); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3970 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3971 | mddev->curr_resync_completed = sector_nr; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 3972 | conf->reshape_checkpoint = jiffies; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3973 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3974 | md_wakeup_thread(mddev->thread); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3975 | wait_event(mddev->sb_wait, mddev->flags == 0 || |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3976 | kthread_should_stop()); |
| 3977 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3978 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3979 | spin_unlock_irq(&conf->device_lock); |
| 3980 | wake_up(&conf->wait_for_overlap); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3981 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3982 | } |
| 3983 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 3984 | if (mddev->delta_disks < 0) { |
| 3985 | BUG_ON(conf->reshape_progress == 0); |
| 3986 | stripe_addr = writepos; |
| 3987 | BUG_ON((mddev->dev_sectors & |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 3988 | ~((sector_t)reshape_sectors - 1)) |
| 3989 | - reshape_sectors - stripe_addr |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 3990 | != sector_nr); |
| 3991 | } else { |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 3992 | BUG_ON(writepos != sector_nr + reshape_sectors); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 3993 | stripe_addr = sector_nr; |
| 3994 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 3995 | INIT_LIST_HEAD(&stripes); |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 3996 | for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3997 | int j; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 3998 | int skipped_disk = 0; |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 3999 | sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4000 | set_bit(STRIPE_EXPANDING, &sh->state); |
| 4001 | atomic_inc(&conf->reshape_stripes); |
| 4002 | /* If any of this stripe is beyond the end of the old |
| 4003 | * array, then we need to zero those blocks |
| 4004 | */ |
| 4005 | for (j=sh->disks; j--;) { |
| 4006 | sector_t s; |
| 4007 | if (j == sh->pd_idx) |
| 4008 | continue; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4009 | if (conf->level == 6 && |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4010 | j == sh->qd_idx) |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4011 | continue; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 4012 | s = compute_blocknr(sh, j, 0); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4013 | if (s < raid5_size(mddev, 0, 0)) { |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4014 | skipped_disk = 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4015 | continue; |
| 4016 | } |
| 4017 | memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE); |
| 4018 | set_bit(R5_Expanded, &sh->dev[j].flags); |
| 4019 | set_bit(R5_UPTODATE, &sh->dev[j].flags); |
| 4020 | } |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4021 | if (!skipped_disk) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4022 | set_bit(STRIPE_EXPAND_READY, &sh->state); |
| 4023 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4024 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4025 | list_add(&sh->lru, &stripes); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4026 | } |
| 4027 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4028 | if (mddev->delta_disks < 0) |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4029 | conf->reshape_progress -= reshape_sectors * new_data_disks; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4030 | else |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4031 | conf->reshape_progress += reshape_sectors * new_data_disks; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4032 | spin_unlock_irq(&conf->device_lock); |
| 4033 | /* Ok, those stripe are ready. We can start scheduling |
| 4034 | * reads on the source stripes. |
| 4035 | * The source stripes are determined by mapping the first and last |
| 4036 | * block on the destination stripes. |
| 4037 | */ |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4038 | first_sector = |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4039 | raid5_compute_sector(conf, stripe_addr*(new_data_disks), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4040 | 1, &dd_idx, NULL); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4041 | last_sector = |
NeilBrown | 0e6e027 | 2009-06-09 16:32:22 +1000 | [diff] [blame] | 4042 | raid5_compute_sector(conf, ((stripe_addr+reshape_sectors) |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 4043 | * new_data_disks - 1), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4044 | 1, &dd_idx, NULL); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4045 | if (last_sector >= mddev->dev_sectors) |
| 4046 | last_sector = mddev->dev_sectors - 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4047 | while (first_sector <= last_sector) { |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4048 | sh = get_active_stripe(conf, first_sector, 1, 0, 1); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4049 | set_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 4050 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4051 | release_stripe(sh); |
| 4052 | first_sector += STRIPE_SECTORS; |
| 4053 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4054 | /* Now that the sources are clearly marked, we can release |
| 4055 | * the destination stripes |
| 4056 | */ |
| 4057 | while (!list_empty(&stripes)) { |
| 4058 | sh = list_entry(stripes.next, struct stripe_head, lru); |
| 4059 | list_del_init(&sh->lru); |
| 4060 | release_stripe(sh); |
| 4061 | } |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4062 | /* If this takes us to the resync_max point where we have to pause, |
| 4063 | * then we need to write out the superblock. |
| 4064 | */ |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4065 | sector_nr += reshape_sectors; |
NeilBrown | c03f6a1 | 2009-04-17 11:06:30 +1000 | [diff] [blame] | 4066 | if ((sector_nr - mddev->curr_resync_completed) * 2 |
| 4067 | >= mddev->resync_max - mddev->curr_resync_completed) { |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4068 | /* Cannot proceed until we've updated the superblock... */ |
| 4069 | wait_event(conf->wait_for_overlap, |
| 4070 | atomic_read(&conf->reshape_stripes) == 0); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4071 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4072 | mddev->curr_resync_completed = sector_nr; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4073 | conf->reshape_checkpoint = jiffies; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4074 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 4075 | md_wakeup_thread(mddev->thread); |
| 4076 | wait_event(mddev->sb_wait, |
| 4077 | !test_bit(MD_CHANGE_DEVS, &mddev->flags) |
| 4078 | || kthread_should_stop()); |
| 4079 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4080 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4081 | spin_unlock_irq(&conf->device_lock); |
| 4082 | wake_up(&conf->wait_for_overlap); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4083 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4084 | } |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4085 | return reshape_sectors; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4086 | } |
| 4087 | |
| 4088 | /* FIXME go_faster isn't used */ |
| 4089 | static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster) |
| 4090 | { |
H Hartley Sweeten | 7b92813 | 2010-03-08 16:02:40 +1100 | [diff] [blame] | 4091 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4092 | struct stripe_head *sh; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4093 | sector_t max_sector = mddev->dev_sectors; |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 4094 | sector_t sync_blocks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4095 | int still_degraded = 0; |
| 4096 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4097 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4098 | if (sector_nr >= max_sector) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4099 | /* just being told to finish up .. nothing much to do */ |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 4100 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 4101 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { |
| 4102 | end_reshape(conf); |
| 4103 | return 0; |
| 4104 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4105 | |
| 4106 | if (mddev->curr_resync < max_sector) /* aborted */ |
| 4107 | bitmap_end_sync(mddev->bitmap, mddev->curr_resync, |
| 4108 | &sync_blocks, 1); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4109 | else /* completed sync */ |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4110 | conf->fullsync = 0; |
| 4111 | bitmap_close_sync(mddev->bitmap); |
| 4112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4113 | return 0; |
| 4114 | } |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4115 | |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4116 | /* Allow raid5_quiesce to complete */ |
| 4117 | wait_event(conf->wait_for_overlap, conf->quiesce != 2); |
| 4118 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4119 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 4120 | return reshape_request(mddev, sector_nr, skipped); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4121 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4122 | /* No need to check resync_max as we never do more than one |
| 4123 | * stripe, and as resync_max will always be on a chunk boundary, |
| 4124 | * if the check in md_do_sync didn't fire, there is no chance |
| 4125 | * of overstepping resync_max here |
| 4126 | */ |
| 4127 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4128 | /* if there is too many failed drives and we are trying |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4129 | * to resync, then assert that we are finished, because there is |
| 4130 | * nothing we can do. |
| 4131 | */ |
NeilBrown | 3285edf | 2006-06-26 00:27:55 -0700 | [diff] [blame] | 4132 | if (mddev->degraded >= conf->max_degraded && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4133 | test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4134 | sector_t rv = mddev->dev_sectors - sector_nr; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 4135 | *skipped = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4136 | return rv; |
| 4137 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4138 | if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) && |
NeilBrown | 3855ad9 | 2005-11-08 21:39:38 -0800 | [diff] [blame] | 4139 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4140 | !conf->fullsync && sync_blocks >= STRIPE_SECTORS) { |
| 4141 | /* we can skip this block, and probably more */ |
| 4142 | sync_blocks /= STRIPE_SECTORS; |
| 4143 | *skipped = 1; |
| 4144 | return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */ |
| 4145 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4146 | |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 4147 | |
| 4148 | bitmap_cond_end_sync(mddev->bitmap, sector_nr); |
| 4149 | |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4150 | sh = get_active_stripe(conf, sector_nr, 0, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4151 | if (sh == NULL) { |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4152 | sh = get_active_stripe(conf, sector_nr, 0, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4153 | /* make sure we don't swamp the stripe cache if someone else |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4154 | * is trying to get access |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4155 | */ |
Nishanth Aravamudan | 66c006a | 2005-11-07 01:01:17 -0800 | [diff] [blame] | 4156 | schedule_timeout_uninterruptible(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4157 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4158 | /* Need to check if array will still be degraded after recovery/resync |
| 4159 | * We don't need to check the 'failed' flag as when that gets set, |
| 4160 | * recovery aborts. |
| 4161 | */ |
NeilBrown | f001a70 | 2009-06-09 14:30:31 +1000 | [diff] [blame] | 4162 | for (i = 0; i < conf->raid_disks; i++) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4163 | if (conf->disks[i].rdev == NULL) |
| 4164 | still_degraded = 1; |
| 4165 | |
| 4166 | bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded); |
| 4167 | |
NeilBrown | 83206d6 | 2011-07-26 11:19:49 +1000 | [diff] [blame] | 4168 | set_bit(STRIPE_SYNC_REQUESTED, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4169 | |
NeilBrown | 1442577 | 2009-10-16 15:55:25 +1100 | [diff] [blame] | 4170 | handle_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4171 | release_stripe(sh); |
| 4172 | |
| 4173 | return STRIPE_SECTORS; |
| 4174 | } |
| 4175 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4176 | static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio) |
| 4177 | { |
| 4178 | /* We may not be able to submit a whole bio at once as there |
| 4179 | * may not be enough stripe_heads available. |
| 4180 | * We cannot pre-allocate enough stripe_heads as we may need |
| 4181 | * more than exist in the cache (if we allow ever large chunks). |
| 4182 | * So we do one stripe head at a time and record in |
| 4183 | * ->bi_hw_segments how many have been done. |
| 4184 | * |
| 4185 | * We *know* that this entire raid_bio is in one chunk, so |
| 4186 | * it will be only one 'dd_idx' and only need one call to raid5_compute_sector. |
| 4187 | */ |
| 4188 | struct stripe_head *sh; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4189 | int dd_idx; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4190 | sector_t sector, logical_sector, last_sector; |
| 4191 | int scnt = 0; |
| 4192 | int remaining; |
| 4193 | int handled = 0; |
| 4194 | |
| 4195 | logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4196 | sector = raid5_compute_sector(conf, logical_sector, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4197 | 0, &dd_idx, NULL); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4198 | last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9); |
| 4199 | |
| 4200 | for (; logical_sector < last_sector; |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4201 | logical_sector += STRIPE_SECTORS, |
| 4202 | sector += STRIPE_SECTORS, |
| 4203 | scnt++) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4204 | |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4205 | if (scnt < raid5_bi_hw_segments(raid_bio)) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4206 | /* already done this stripe */ |
| 4207 | continue; |
| 4208 | |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4209 | sh = get_active_stripe(conf, sector, 0, 1, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4210 | |
| 4211 | if (!sh) { |
| 4212 | /* failed to get a stripe - must wait */ |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4213 | raid5_set_bi_hw_segments(raid_bio, scnt); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4214 | conf->retry_read_aligned = raid_bio; |
| 4215 | return handled; |
| 4216 | } |
| 4217 | |
| 4218 | set_bit(R5_ReadError, &sh->dev[dd_idx].flags); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4219 | if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) { |
| 4220 | release_stripe(sh); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4221 | raid5_set_bi_hw_segments(raid_bio, scnt); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4222 | conf->retry_read_aligned = raid_bio; |
| 4223 | return handled; |
| 4224 | } |
| 4225 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4226 | handle_stripe(sh); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4227 | release_stripe(sh); |
| 4228 | handled++; |
| 4229 | } |
| 4230 | spin_lock_irq(&conf->device_lock); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4231 | remaining = raid5_dec_bi_phys_segments(raid_bio); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4232 | spin_unlock_irq(&conf->device_lock); |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 4233 | if (remaining == 0) |
| 4234 | bio_endio(raid_bio, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4235 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
| 4236 | wake_up(&conf->wait_for_stripe); |
| 4237 | return handled; |
| 4238 | } |
| 4239 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4240 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4241 | /* |
| 4242 | * This is our raid5 kernel thread. |
| 4243 | * |
| 4244 | * We scan the hash table for stripes which can be handled now. |
| 4245 | * During the scan, completed stripes are saved for us by the interrupt |
| 4246 | * handler, so that they will not have to wait for our next wakeup. |
| 4247 | */ |
NeilBrown | 6ed3003 | 2008-02-06 01:40:00 -0800 | [diff] [blame] | 4248 | static void raid5d(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4249 | { |
| 4250 | struct stripe_head *sh; |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 4251 | raid5_conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4252 | int handled; |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 4253 | struct blk_plug plug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4254 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4255 | pr_debug("+++ raid5d active\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4256 | |
| 4257 | md_check_recovery(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4258 | |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 4259 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4260 | handled = 0; |
| 4261 | spin_lock_irq(&conf->device_lock); |
| 4262 | while (1) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4263 | struct bio *bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4264 | |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 4265 | if (atomic_read(&mddev->plug_cnt) == 0 && |
| 4266 | !list_empty(&conf->bitmap_list)) { |
| 4267 | /* Now is a good time to flush some bitmap updates */ |
| 4268 | conf->seq_flush++; |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 4269 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4270 | bitmap_unplug(mddev->bitmap); |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 4271 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 4272 | conf->seq_write = conf->seq_flush; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4273 | activate_bit_delay(conf); |
| 4274 | } |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 4275 | if (atomic_read(&mddev->plug_cnt) == 0) |
| 4276 | raid5_activate_delayed(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4277 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4278 | while ((bio = remove_bio_from_retry(conf))) { |
| 4279 | int ok; |
| 4280 | spin_unlock_irq(&conf->device_lock); |
| 4281 | ok = retry_aligned_read(conf, bio); |
| 4282 | spin_lock_irq(&conf->device_lock); |
| 4283 | if (!ok) |
| 4284 | break; |
| 4285 | handled++; |
| 4286 | } |
| 4287 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4288 | sh = __get_priority_stripe(conf); |
| 4289 | |
Dan Williams | c9f21aa | 2008-07-23 12:05:51 -0700 | [diff] [blame] | 4290 | if (!sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4291 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4292 | spin_unlock_irq(&conf->device_lock); |
| 4293 | |
| 4294 | handled++; |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 4295 | handle_stripe(sh); |
| 4296 | release_stripe(sh); |
| 4297 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4298 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 4299 | if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) |
| 4300 | md_check_recovery(mddev); |
| 4301 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4302 | spin_lock_irq(&conf->device_lock); |
| 4303 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4304 | pr_debug("%d stripes handled\n", handled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4305 | |
| 4306 | spin_unlock_irq(&conf->device_lock); |
| 4307 | |
Dan Williams | c9f21aa | 2008-07-23 12:05:51 -0700 | [diff] [blame] | 4308 | async_tx_issue_pending_all(); |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 4309 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4310 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4311 | pr_debug("--- raid5d inactive\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4312 | } |
| 4313 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4314 | static ssize_t |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4315 | raid5_show_stripe_cache_size(mddev_t *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4316 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 4317 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4318 | if (conf) |
| 4319 | return sprintf(page, "%d\n", conf->max_nr_stripes); |
| 4320 | else |
| 4321 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4322 | } |
| 4323 | |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 4324 | int |
| 4325 | raid5_set_cache_size(mddev_t *mddev, int size) |
| 4326 | { |
| 4327 | raid5_conf_t *conf = mddev->private; |
| 4328 | int err; |
| 4329 | |
| 4330 | if (size <= 16 || size > 32768) |
| 4331 | return -EINVAL; |
| 4332 | while (size < conf->max_nr_stripes) { |
| 4333 | if (drop_one_stripe(conf)) |
| 4334 | conf->max_nr_stripes--; |
| 4335 | else |
| 4336 | break; |
| 4337 | } |
| 4338 | err = md_allow_write(mddev); |
| 4339 | if (err) |
| 4340 | return err; |
| 4341 | while (size > conf->max_nr_stripes) { |
| 4342 | if (grow_one_stripe(conf)) |
| 4343 | conf->max_nr_stripes++; |
| 4344 | else break; |
| 4345 | } |
| 4346 | return 0; |
| 4347 | } |
| 4348 | EXPORT_SYMBOL(raid5_set_cache_size); |
| 4349 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4350 | static ssize_t |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4351 | raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4352 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 4353 | raid5_conf_t *conf = mddev->private; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4354 | unsigned long new; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4355 | int err; |
| 4356 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4357 | if (len >= PAGE_SIZE) |
| 4358 | return -EINVAL; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4359 | if (!conf) |
| 4360 | return -ENODEV; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4361 | |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4362 | if (strict_strtoul(page, 10, &new)) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4363 | return -EINVAL; |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 4364 | err = raid5_set_cache_size(mddev, new); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4365 | if (err) |
| 4366 | return err; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4367 | return len; |
| 4368 | } |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4369 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4370 | static struct md_sysfs_entry |
| 4371 | raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR, |
| 4372 | raid5_show_stripe_cache_size, |
| 4373 | raid5_store_stripe_cache_size); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4374 | |
| 4375 | static ssize_t |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4376 | raid5_show_preread_threshold(mddev_t *mddev, char *page) |
| 4377 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 4378 | raid5_conf_t *conf = mddev->private; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4379 | if (conf) |
| 4380 | return sprintf(page, "%d\n", conf->bypass_threshold); |
| 4381 | else |
| 4382 | return 0; |
| 4383 | } |
| 4384 | |
| 4385 | static ssize_t |
| 4386 | raid5_store_preread_threshold(mddev_t *mddev, const char *page, size_t len) |
| 4387 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 4388 | raid5_conf_t *conf = mddev->private; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4389 | unsigned long new; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4390 | if (len >= PAGE_SIZE) |
| 4391 | return -EINVAL; |
| 4392 | if (!conf) |
| 4393 | return -ENODEV; |
| 4394 | |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4395 | if (strict_strtoul(page, 10, &new)) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4396 | return -EINVAL; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4397 | if (new > conf->max_nr_stripes) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4398 | return -EINVAL; |
| 4399 | conf->bypass_threshold = new; |
| 4400 | return len; |
| 4401 | } |
| 4402 | |
| 4403 | static struct md_sysfs_entry |
| 4404 | raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold, |
| 4405 | S_IRUGO | S_IWUSR, |
| 4406 | raid5_show_preread_threshold, |
| 4407 | raid5_store_preread_threshold); |
| 4408 | |
| 4409 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4410 | stripe_cache_active_show(mddev_t *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4411 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 4412 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4413 | if (conf) |
| 4414 | return sprintf(page, "%d\n", atomic_read(&conf->active_stripes)); |
| 4415 | else |
| 4416 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4417 | } |
| 4418 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4419 | static struct md_sysfs_entry |
| 4420 | raid5_stripecache_active = __ATTR_RO(stripe_cache_active); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4421 | |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4422 | static struct attribute *raid5_attrs[] = { |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4423 | &raid5_stripecache_size.attr, |
| 4424 | &raid5_stripecache_active.attr, |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4425 | &raid5_preread_bypass_threshold.attr, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4426 | NULL, |
| 4427 | }; |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4428 | static struct attribute_group raid5_attrs_group = { |
| 4429 | .name = NULL, |
| 4430 | .attrs = raid5_attrs, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4431 | }; |
| 4432 | |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4433 | static sector_t |
| 4434 | raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks) |
| 4435 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 4436 | raid5_conf_t *conf = mddev->private; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4437 | |
| 4438 | if (!sectors) |
| 4439 | sectors = mddev->dev_sectors; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4440 | if (!raid_disks) |
NeilBrown | 7ec0547 | 2009-03-31 15:10:36 +1100 | [diff] [blame] | 4441 | /* size is defined by the smallest of previous and new size */ |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4442 | raid_disks = min(conf->raid_disks, conf->previous_raid_disks); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4443 | |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4444 | sectors &= ~((sector_t)mddev->chunk_sectors - 1); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4445 | sectors &= ~((sector_t)mddev->new_chunk_sectors - 1); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4446 | return sectors * (raid_disks - conf->max_degraded); |
| 4447 | } |
| 4448 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4449 | static void raid5_free_percpu(raid5_conf_t *conf) |
| 4450 | { |
| 4451 | struct raid5_percpu *percpu; |
| 4452 | unsigned long cpu; |
| 4453 | |
| 4454 | if (!conf->percpu) |
| 4455 | return; |
| 4456 | |
| 4457 | get_online_cpus(); |
| 4458 | for_each_possible_cpu(cpu) { |
| 4459 | percpu = per_cpu_ptr(conf->percpu, cpu); |
| 4460 | safe_put_page(percpu->spare_page); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4461 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4462 | } |
| 4463 | #ifdef CONFIG_HOTPLUG_CPU |
| 4464 | unregister_cpu_notifier(&conf->cpu_notify); |
| 4465 | #endif |
| 4466 | put_online_cpus(); |
| 4467 | |
| 4468 | free_percpu(conf->percpu); |
| 4469 | } |
| 4470 | |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 4471 | static void free_conf(raid5_conf_t *conf) |
| 4472 | { |
| 4473 | shrink_stripes(conf); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4474 | raid5_free_percpu(conf); |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 4475 | kfree(conf->disks); |
| 4476 | kfree(conf->stripe_hashtbl); |
| 4477 | kfree(conf); |
| 4478 | } |
| 4479 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4480 | #ifdef CONFIG_HOTPLUG_CPU |
| 4481 | static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action, |
| 4482 | void *hcpu) |
| 4483 | { |
| 4484 | raid5_conf_t *conf = container_of(nfb, raid5_conf_t, cpu_notify); |
| 4485 | long cpu = (long)hcpu; |
| 4486 | struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu); |
| 4487 | |
| 4488 | switch (action) { |
| 4489 | case CPU_UP_PREPARE: |
| 4490 | case CPU_UP_PREPARE_FROZEN: |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4491 | if (conf->level == 6 && !percpu->spare_page) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4492 | percpu->spare_page = alloc_page(GFP_KERNEL); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4493 | if (!percpu->scribble) |
| 4494 | percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL); |
| 4495 | |
| 4496 | if (!percpu->scribble || |
| 4497 | (conf->level == 6 && !percpu->spare_page)) { |
| 4498 | safe_put_page(percpu->spare_page); |
| 4499 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4500 | pr_err("%s: failed memory allocation for cpu%ld\n", |
| 4501 | __func__, cpu); |
Akinobu Mita | 55af6bb | 2010-05-26 14:43:35 -0700 | [diff] [blame] | 4502 | return notifier_from_errno(-ENOMEM); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4503 | } |
| 4504 | break; |
| 4505 | case CPU_DEAD: |
| 4506 | case CPU_DEAD_FROZEN: |
| 4507 | safe_put_page(percpu->spare_page); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4508 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4509 | percpu->spare_page = NULL; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4510 | percpu->scribble = NULL; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4511 | break; |
| 4512 | default: |
| 4513 | break; |
| 4514 | } |
| 4515 | return NOTIFY_OK; |
| 4516 | } |
| 4517 | #endif |
| 4518 | |
| 4519 | static int raid5_alloc_percpu(raid5_conf_t *conf) |
| 4520 | { |
| 4521 | unsigned long cpu; |
| 4522 | struct page *spare_page; |
Tejun Heo | a29d8b8 | 2010-02-02 14:39:15 +0900 | [diff] [blame] | 4523 | struct raid5_percpu __percpu *allcpus; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4524 | void *scribble; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4525 | int err; |
| 4526 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4527 | allcpus = alloc_percpu(struct raid5_percpu); |
| 4528 | if (!allcpus) |
| 4529 | return -ENOMEM; |
| 4530 | conf->percpu = allcpus; |
| 4531 | |
| 4532 | get_online_cpus(); |
| 4533 | err = 0; |
| 4534 | for_each_present_cpu(cpu) { |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4535 | if (conf->level == 6) { |
| 4536 | spare_page = alloc_page(GFP_KERNEL); |
| 4537 | if (!spare_page) { |
| 4538 | err = -ENOMEM; |
| 4539 | break; |
| 4540 | } |
| 4541 | per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page; |
| 4542 | } |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4543 | scribble = kmalloc(conf->scribble_len, GFP_KERNEL); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4544 | if (!scribble) { |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4545 | err = -ENOMEM; |
| 4546 | break; |
| 4547 | } |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4548 | per_cpu_ptr(conf->percpu, cpu)->scribble = scribble; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4549 | } |
| 4550 | #ifdef CONFIG_HOTPLUG_CPU |
| 4551 | conf->cpu_notify.notifier_call = raid456_cpu_notify; |
| 4552 | conf->cpu_notify.priority = 0; |
| 4553 | if (err == 0) |
| 4554 | err = register_cpu_notifier(&conf->cpu_notify); |
| 4555 | #endif |
| 4556 | put_online_cpus(); |
| 4557 | |
| 4558 | return err; |
| 4559 | } |
| 4560 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4561 | static raid5_conf_t *setup_conf(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4562 | { |
| 4563 | raid5_conf_t *conf; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4564 | int raid_disk, memory, max_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4565 | mdk_rdev_t *rdev; |
| 4566 | struct disk_info *disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4567 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4568 | if (mddev->new_level != 5 |
| 4569 | && mddev->new_level != 4 |
| 4570 | && mddev->new_level != 6) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4571 | 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] | 4572 | mdname(mddev), mddev->new_level); |
| 4573 | return ERR_PTR(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4574 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4575 | if ((mddev->new_level == 5 |
| 4576 | && !algorithm_valid_raid5(mddev->new_layout)) || |
| 4577 | (mddev->new_level == 6 |
| 4578 | && !algorithm_valid_raid6(mddev->new_layout))) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4579 | printk(KERN_ERR "md/raid:%s: layout %d not supported\n", |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4580 | mdname(mddev), mddev->new_layout); |
| 4581 | return ERR_PTR(-EIO); |
| 4582 | } |
| 4583 | if (mddev->new_level == 6 && mddev->raid_disks < 4) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4584 | 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] | 4585 | mdname(mddev), mddev->raid_disks); |
| 4586 | return ERR_PTR(-EINVAL); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4587 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4588 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4589 | if (!mddev->new_chunk_sectors || |
| 4590 | (mddev->new_chunk_sectors << 9) % PAGE_SIZE || |
| 4591 | !is_power_of_2(mddev->new_chunk_sectors)) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4592 | printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n", |
| 4593 | mdname(mddev), mddev->new_chunk_sectors << 9); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4594 | return ERR_PTR(-EINVAL); |
NeilBrown | 4bbf377 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4595 | } |
| 4596 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4597 | conf = kzalloc(sizeof(raid5_conf_t), GFP_KERNEL); |
| 4598 | if (conf == NULL) |
| 4599 | goto abort; |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 4600 | spin_lock_init(&conf->device_lock); |
| 4601 | init_waitqueue_head(&conf->wait_for_stripe); |
| 4602 | init_waitqueue_head(&conf->wait_for_overlap); |
| 4603 | INIT_LIST_HEAD(&conf->handle_list); |
| 4604 | INIT_LIST_HEAD(&conf->hold_list); |
| 4605 | INIT_LIST_HEAD(&conf->delayed_list); |
| 4606 | INIT_LIST_HEAD(&conf->bitmap_list); |
| 4607 | INIT_LIST_HEAD(&conf->inactive_list); |
| 4608 | atomic_set(&conf->active_stripes, 0); |
| 4609 | atomic_set(&conf->preread_active_stripes, 0); |
| 4610 | atomic_set(&conf->active_aligned_reads, 0); |
| 4611 | conf->bypass_threshold = BYPASS_THRESHOLD; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4612 | |
| 4613 | conf->raid_disks = mddev->raid_disks; |
| 4614 | if (mddev->reshape_position == MaxSector) |
| 4615 | conf->previous_raid_disks = mddev->raid_disks; |
| 4616 | else |
| 4617 | conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4618 | max_disks = max(conf->raid_disks, conf->previous_raid_disks); |
| 4619 | conf->scribble_len = scribble_len(max_disks); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4620 | |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4621 | conf->disks = kzalloc(max_disks * sizeof(struct disk_info), |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4622 | GFP_KERNEL); |
| 4623 | if (!conf->disks) |
| 4624 | goto abort; |
| 4625 | |
| 4626 | conf->mddev = mddev; |
| 4627 | |
| 4628 | if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL) |
| 4629 | goto abort; |
| 4630 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4631 | conf->level = mddev->new_level; |
| 4632 | if (raid5_alloc_percpu(conf) != 0) |
| 4633 | goto abort; |
| 4634 | |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4635 | pr_debug("raid456: run(%s) called.\n", mdname(mddev)); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4636 | |
| 4637 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 4638 | raid_disk = rdev->raid_disk; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4639 | if (raid_disk >= max_disks |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4640 | || raid_disk < 0) |
| 4641 | continue; |
| 4642 | disk = conf->disks + raid_disk; |
| 4643 | |
| 4644 | disk->rdev = rdev; |
| 4645 | |
| 4646 | if (test_bit(In_sync, &rdev->flags)) { |
| 4647 | char b[BDEVNAME_SIZE]; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4648 | printk(KERN_INFO "md/raid:%s: device %s operational as raid" |
| 4649 | " disk %d\n", |
| 4650 | mdname(mddev), bdevname(rdev->bdev, b), raid_disk); |
Jonathan Brassow | d6b212f | 2011-06-08 18:00:28 -0500 | [diff] [blame] | 4651 | } else if (rdev->saved_raid_disk != raid_disk) |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4652 | /* Cannot rely on bitmap to complete recovery */ |
| 4653 | conf->fullsync = 1; |
| 4654 | } |
| 4655 | |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 4656 | conf->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4657 | conf->level = mddev->new_level; |
| 4658 | if (conf->level == 6) |
| 4659 | conf->max_degraded = 2; |
| 4660 | else |
| 4661 | conf->max_degraded = 1; |
| 4662 | conf->algorithm = mddev->new_layout; |
| 4663 | conf->max_nr_stripes = NR_STRIPES; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4664 | conf->reshape_progress = mddev->reshape_position; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 4665 | if (conf->reshape_progress != MaxSector) { |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 4666 | conf->prev_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 4667 | conf->prev_algo = mddev->layout; |
| 4668 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4669 | |
| 4670 | memory = conf->max_nr_stripes * (sizeof(struct stripe_head) + |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4671 | max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4672 | if (grow_stripes(conf, conf->max_nr_stripes)) { |
| 4673 | printk(KERN_ERR |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4674 | "md/raid:%s: couldn't allocate %dkB for buffers\n", |
| 4675 | mdname(mddev), memory); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4676 | goto abort; |
| 4677 | } else |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4678 | printk(KERN_INFO "md/raid:%s: allocated %dkB\n", |
| 4679 | mdname(mddev), memory); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4680 | |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 4681 | conf->thread = md_register_thread(raid5d, mddev, NULL); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4682 | if (!conf->thread) { |
| 4683 | printk(KERN_ERR |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4684 | "md/raid:%s: couldn't allocate thread.\n", |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4685 | mdname(mddev)); |
| 4686 | goto abort; |
| 4687 | } |
| 4688 | |
| 4689 | return conf; |
| 4690 | |
| 4691 | abort: |
| 4692 | if (conf) { |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 4693 | free_conf(conf); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4694 | return ERR_PTR(-EIO); |
| 4695 | } else |
| 4696 | return ERR_PTR(-ENOMEM); |
| 4697 | } |
| 4698 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 4699 | |
| 4700 | static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded) |
| 4701 | { |
| 4702 | switch (algo) { |
| 4703 | case ALGORITHM_PARITY_0: |
| 4704 | if (raid_disk < max_degraded) |
| 4705 | return 1; |
| 4706 | break; |
| 4707 | case ALGORITHM_PARITY_N: |
| 4708 | if (raid_disk >= raid_disks - max_degraded) |
| 4709 | return 1; |
| 4710 | break; |
| 4711 | case ALGORITHM_PARITY_0_6: |
| 4712 | if (raid_disk == 0 || |
| 4713 | raid_disk == raid_disks - 1) |
| 4714 | return 1; |
| 4715 | break; |
| 4716 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 4717 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 4718 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 4719 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 4720 | if (raid_disk == raid_disks - 1) |
| 4721 | return 1; |
| 4722 | } |
| 4723 | return 0; |
| 4724 | } |
| 4725 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4726 | static int run(mddev_t *mddev) |
| 4727 | { |
| 4728 | raid5_conf_t *conf; |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 4729 | int working_disks = 0; |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 4730 | int dirty_parity_disks = 0; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4731 | mdk_rdev_t *rdev; |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 4732 | sector_t reshape_offset = 0; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4733 | |
Andre Noll | 8c6ac86 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 4734 | if (mddev->recovery_cp != MaxSector) |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4735 | printk(KERN_NOTICE "md/raid:%s: not clean" |
Andre Noll | 8c6ac86 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 4736 | " -- starting background reconstruction\n", |
| 4737 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4738 | if (mddev->reshape_position != MaxSector) { |
| 4739 | /* Check that we can continue the reshape. |
| 4740 | * Currently only disks can change, it must |
| 4741 | * increase, and we must be past the point where |
| 4742 | * a stripe over-writes itself |
| 4743 | */ |
| 4744 | sector_t here_new, here_old; |
| 4745 | int old_disks; |
Andre Noll | 18b0033 | 2009-03-31 15:00:56 +1100 | [diff] [blame] | 4746 | int max_degraded = (mddev->level == 6 ? 2 : 1); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4747 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 4748 | if (mddev->new_level != mddev->level) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4749 | printk(KERN_ERR "md/raid:%s: unsupported reshape " |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4750 | "required - aborting.\n", |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4751 | mdname(mddev)); |
| 4752 | return -EINVAL; |
| 4753 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4754 | old_disks = mddev->raid_disks - mddev->delta_disks; |
| 4755 | /* reshape_position must be on a new-stripe boundary, and one |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4756 | * further up in new geometry must map after here in old |
| 4757 | * geometry. |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4758 | */ |
| 4759 | here_new = mddev->reshape_position; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4760 | if (sector_div(here_new, mddev->new_chunk_sectors * |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4761 | (mddev->raid_disks - max_degraded))) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4762 | printk(KERN_ERR "md/raid:%s: reshape_position not " |
| 4763 | "on a stripe boundary\n", mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4764 | return -EINVAL; |
| 4765 | } |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 4766 | reshape_offset = here_new * mddev->new_chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4767 | /* here_new is the stripe we will write to */ |
| 4768 | here_old = mddev->reshape_position; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4769 | sector_div(here_old, mddev->chunk_sectors * |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4770 | (old_disks-max_degraded)); |
| 4771 | /* here_old is the first stripe that we might need to read |
| 4772 | * from */ |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 4773 | if (mddev->delta_disks == 0) { |
| 4774 | /* We cannot be sure it is safe to start an in-place |
| 4775 | * reshape. It is only safe if user-space if monitoring |
| 4776 | * and taking constant backups. |
| 4777 | * mdadm always starts a situation like this in |
| 4778 | * readonly mode so it can take control before |
| 4779 | * allowing any writes. So just check for that. |
| 4780 | */ |
| 4781 | if ((here_new * mddev->new_chunk_sectors != |
| 4782 | here_old * mddev->chunk_sectors) || |
| 4783 | mddev->ro == 0) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4784 | printk(KERN_ERR "md/raid:%s: in-place reshape must be started" |
| 4785 | " in read-only mode - aborting\n", |
| 4786 | mdname(mddev)); |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 4787 | return -EINVAL; |
| 4788 | } |
| 4789 | } else if (mddev->delta_disks < 0 |
| 4790 | ? (here_new * mddev->new_chunk_sectors <= |
| 4791 | here_old * mddev->chunk_sectors) |
| 4792 | : (here_new * mddev->new_chunk_sectors >= |
| 4793 | here_old * mddev->chunk_sectors)) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4794 | /* Reading from the same stripe as writing to - bad */ |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4795 | printk(KERN_ERR "md/raid:%s: reshape_position too early for " |
| 4796 | "auto-recovery - aborting.\n", |
| 4797 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4798 | return -EINVAL; |
| 4799 | } |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4800 | printk(KERN_INFO "md/raid:%s: reshape will continue\n", |
| 4801 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4802 | /* OK, we should be able to continue; */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4803 | } else { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4804 | BUG_ON(mddev->level != mddev->new_level); |
| 4805 | BUG_ON(mddev->layout != mddev->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4806 | BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4807 | BUG_ON(mddev->delta_disks != 0); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4808 | } |
| 4809 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4810 | if (mddev->private == NULL) |
| 4811 | conf = setup_conf(mddev); |
| 4812 | else |
| 4813 | conf = mddev->private; |
| 4814 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4815 | if (IS_ERR(conf)) |
| 4816 | return PTR_ERR(conf); |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 4817 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4818 | mddev->thread = conf->thread; |
| 4819 | conf->thread = NULL; |
| 4820 | mddev->private = conf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4821 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4822 | /* |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4823 | * 0 for a fully functional array, 1 or 2 for a degraded array. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4824 | */ |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 4825 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 4826 | if (rdev->raid_disk < 0) |
| 4827 | continue; |
NeilBrown | 2f11588 | 2010-06-17 17:41:03 +1000 | [diff] [blame] | 4828 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4829 | working_disks++; |
NeilBrown | 2f11588 | 2010-06-17 17:41:03 +1000 | [diff] [blame] | 4830 | continue; |
| 4831 | } |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 4832 | /* This disc is not fully in-sync. However if it |
| 4833 | * just stored parity (beyond the recovery_offset), |
| 4834 | * when we don't need to be concerned about the |
| 4835 | * array being dirty. |
| 4836 | * When reshape goes 'backwards', we never have |
| 4837 | * partially completed devices, so we only need |
| 4838 | * to worry about reshape going forwards. |
| 4839 | */ |
| 4840 | /* Hack because v0.91 doesn't store recovery_offset properly. */ |
| 4841 | if (mddev->major_version == 0 && |
| 4842 | mddev->minor_version > 90) |
| 4843 | rdev->recovery_offset = reshape_offset; |
| 4844 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 4845 | if (rdev->recovery_offset < reshape_offset) { |
| 4846 | /* We need to check old and new layout */ |
| 4847 | if (!only_parity(rdev->raid_disk, |
| 4848 | conf->algorithm, |
| 4849 | conf->raid_disks, |
| 4850 | conf->max_degraded)) |
| 4851 | continue; |
| 4852 | } |
| 4853 | if (!only_parity(rdev->raid_disk, |
| 4854 | conf->prev_algo, |
| 4855 | conf->previous_raid_disks, |
| 4856 | conf->max_degraded)) |
| 4857 | continue; |
| 4858 | dirty_parity_disks++; |
| 4859 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4860 | |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4861 | mddev->degraded = (max(conf->raid_disks, conf->previous_raid_disks) |
| 4862 | - working_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4863 | |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 4864 | if (has_failed(conf)) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4865 | printk(KERN_ERR "md/raid:%s: not enough operational devices" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4866 | " (%d/%d failed)\n", |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 4867 | mdname(mddev), mddev->degraded, conf->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4868 | goto abort; |
| 4869 | } |
| 4870 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4871 | /* device size must be a multiple of chunk size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4872 | mddev->dev_sectors &= ~(mddev->chunk_sectors - 1); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4873 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4874 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 4875 | if (mddev->degraded > dirty_parity_disks && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4876 | mddev->recovery_cp != MaxSector) { |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4877 | if (mddev->ok_start_degraded) |
| 4878 | printk(KERN_WARNING |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4879 | "md/raid:%s: starting dirty degraded array" |
| 4880 | " - data corruption possible.\n", |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4881 | mdname(mddev)); |
| 4882 | else { |
| 4883 | printk(KERN_ERR |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4884 | "md/raid:%s: cannot start dirty degraded array.\n", |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4885 | mdname(mddev)); |
| 4886 | goto abort; |
| 4887 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4888 | } |
| 4889 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4890 | if (mddev->degraded == 0) |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4891 | printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d" |
| 4892 | " devices, algorithm %d\n", mdname(mddev), conf->level, |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 4893 | mddev->raid_disks-mddev->degraded, mddev->raid_disks, |
| 4894 | mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4895 | else |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4896 | printk(KERN_ALERT "md/raid:%s: raid level %d active with %d" |
| 4897 | " out of %d devices, algorithm %d\n", |
| 4898 | mdname(mddev), conf->level, |
| 4899 | mddev->raid_disks - mddev->degraded, |
| 4900 | mddev->raid_disks, mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4901 | |
| 4902 | print_raid5_conf(conf); |
| 4903 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4904 | if (conf->reshape_progress != MaxSector) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4905 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4906 | atomic_set(&conf->reshape_stripes, 0); |
| 4907 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 4908 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 4909 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 4910 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 4911 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 4912 | "reshape"); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4913 | } |
| 4914 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4915 | |
| 4916 | /* Ok, everything is just fine now */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 4917 | if (mddev->to_remove == &raid5_attrs_group) |
| 4918 | mddev->to_remove = NULL; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4919 | else if (mddev->kobj.sd && |
| 4920 | sysfs_create_group(&mddev->kobj, &raid5_attrs_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4921 | printk(KERN_WARNING |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 4922 | "raid5: failed to create sysfs attributes for %s\n", |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4923 | mdname(mddev)); |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 4924 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
| 4925 | |
| 4926 | if (mddev->queue) { |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 4927 | int chunk_size; |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 4928 | /* read-ahead size must cover two whole stripes, which |
| 4929 | * is 2 * (datadisks) * chunksize where 'n' is the |
| 4930 | * number of raid devices |
| 4931 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4932 | int data_disks = conf->previous_raid_disks - conf->max_degraded; |
| 4933 | int stripe = data_disks * |
| 4934 | ((mddev->chunk_sectors << 9) / PAGE_SIZE); |
| 4935 | if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 4936 | mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 4937 | |
| 4938 | blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec); |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 4939 | |
| 4940 | mddev->queue->backing_dev_info.congested_data = mddev; |
| 4941 | mddev->queue->backing_dev_info.congested_fn = raid5_congested; |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 4942 | |
| 4943 | chunk_size = mddev->chunk_sectors << 9; |
| 4944 | blk_queue_io_min(mddev->queue, chunk_size); |
| 4945 | blk_queue_io_opt(mddev->queue, chunk_size * |
| 4946 | (conf->raid_disks - conf->max_degraded)); |
| 4947 | |
| 4948 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4949 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 4950 | rdev->data_offset << 9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4951 | } |
| 4952 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4953 | return 0; |
| 4954 | abort: |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 4955 | md_unregister_thread(&mddev->thread); |
NeilBrown | e4f869d | 2011-10-07 14:22:49 +1100 | [diff] [blame] | 4956 | print_raid5_conf(conf); |
| 4957 | free_conf(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4958 | mddev->private = NULL; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4959 | 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] | 4960 | return -EIO; |
| 4961 | } |
| 4962 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4963 | static int stop(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4964 | { |
H Hartley Sweeten | 7b92813 | 2010-03-08 16:02:40 +1100 | [diff] [blame] | 4965 | raid5_conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4966 | |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 4967 | md_unregister_thread(&mddev->thread); |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 4968 | if (mddev->queue) |
| 4969 | mddev->queue->backing_dev_info.congested_fn = NULL; |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 4970 | free_conf(conf); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 4971 | mddev->private = NULL; |
| 4972 | mddev->to_remove = &raid5_attrs_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4973 | return 0; |
| 4974 | } |
| 4975 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4976 | #ifdef DEBUG |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4977 | static void print_sh(struct seq_file *seq, struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4978 | { |
| 4979 | int i; |
| 4980 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4981 | seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n", |
| 4982 | (unsigned long long)sh->sector, sh->pd_idx, sh->state); |
| 4983 | seq_printf(seq, "sh %llu, count %d.\n", |
| 4984 | (unsigned long long)sh->sector, atomic_read(&sh->count)); |
| 4985 | seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector); |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4986 | for (i = 0; i < sh->disks; i++) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4987 | seq_printf(seq, "(cache%d: %p %ld) ", |
| 4988 | i, sh->dev[i].page, sh->dev[i].flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4989 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4990 | seq_printf(seq, "\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4991 | } |
| 4992 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4993 | static void printall(struct seq_file *seq, raid5_conf_t *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4994 | { |
| 4995 | struct stripe_head *sh; |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 4996 | struct hlist_node *hn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4997 | int i; |
| 4998 | |
| 4999 | spin_lock_irq(&conf->device_lock); |
| 5000 | for (i = 0; i < NR_HASH; i++) { |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 5001 | hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5002 | if (sh->raid_conf != conf) |
| 5003 | continue; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5004 | print_sh(seq, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5005 | } |
| 5006 | } |
| 5007 | spin_unlock_irq(&conf->device_lock); |
| 5008 | } |
| 5009 | #endif |
| 5010 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5011 | static void status(struct seq_file *seq, mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5012 | { |
H Hartley Sweeten | 7b92813 | 2010-03-08 16:02:40 +1100 | [diff] [blame] | 5013 | raid5_conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5014 | int i; |
| 5015 | |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5016 | seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level, |
| 5017 | mddev->chunk_sectors / 2, mddev->layout); |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 5018 | 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] | 5019 | for (i = 0; i < conf->raid_disks; i++) |
| 5020 | seq_printf (seq, "%s", |
| 5021 | conf->disks[i].rdev && |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5022 | test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5023 | seq_printf (seq, "]"); |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 5024 | #ifdef DEBUG |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5025 | seq_printf (seq, "\n"); |
| 5026 | printall(seq, conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5027 | #endif |
| 5028 | } |
| 5029 | |
| 5030 | static void print_raid5_conf (raid5_conf_t *conf) |
| 5031 | { |
| 5032 | int i; |
| 5033 | struct disk_info *tmp; |
| 5034 | |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5035 | printk(KERN_DEBUG "RAID conf printout:\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5036 | if (!conf) { |
| 5037 | printk("(conf==NULL)\n"); |
| 5038 | return; |
| 5039 | } |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5040 | printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level, |
| 5041 | conf->raid_disks, |
| 5042 | conf->raid_disks - conf->mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5043 | |
| 5044 | for (i = 0; i < conf->raid_disks; i++) { |
| 5045 | char b[BDEVNAME_SIZE]; |
| 5046 | tmp = conf->disks + i; |
| 5047 | if (tmp->rdev) |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5048 | printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n", |
| 5049 | i, !test_bit(Faulty, &tmp->rdev->flags), |
| 5050 | bdevname(tmp->rdev->bdev, b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5051 | } |
| 5052 | } |
| 5053 | |
| 5054 | static int raid5_spare_active(mddev_t *mddev) |
| 5055 | { |
| 5056 | int i; |
| 5057 | raid5_conf_t *conf = mddev->private; |
| 5058 | struct disk_info *tmp; |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5059 | int count = 0; |
| 5060 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5061 | |
| 5062 | for (i = 0; i < conf->raid_disks; i++) { |
| 5063 | tmp = conf->disks + i; |
| 5064 | if (tmp->rdev |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 5065 | && tmp->rdev->recovery_offset == MaxSector |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5066 | && !test_bit(Faulty, &tmp->rdev->flags) |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 5067 | && !test_and_set_bit(In_sync, &tmp->rdev->flags)) { |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5068 | count++; |
Jonathan Brassow | 43c73ca | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5069 | sysfs_notify_dirent_safe(tmp->rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5070 | } |
| 5071 | } |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5072 | spin_lock_irqsave(&conf->device_lock, flags); |
| 5073 | mddev->degraded -= count; |
| 5074 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5075 | print_raid5_conf(conf); |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5076 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5077 | } |
| 5078 | |
| 5079 | static int raid5_remove_disk(mddev_t *mddev, int number) |
| 5080 | { |
| 5081 | raid5_conf_t *conf = mddev->private; |
| 5082 | int err = 0; |
| 5083 | mdk_rdev_t *rdev; |
| 5084 | struct disk_info *p = conf->disks + number; |
| 5085 | |
| 5086 | print_raid5_conf(conf); |
| 5087 | rdev = p->rdev; |
| 5088 | if (rdev) { |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5089 | if (number >= conf->raid_disks && |
| 5090 | conf->reshape_progress == MaxSector) |
| 5091 | clear_bit(In_sync, &rdev->flags); |
| 5092 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5093 | if (test_bit(In_sync, &rdev->flags) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5094 | atomic_read(&rdev->nr_pending)) { |
| 5095 | err = -EBUSY; |
| 5096 | goto abort; |
| 5097 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5098 | /* Only remove non-faulty devices if recovery |
| 5099 | * isn't possible. |
| 5100 | */ |
| 5101 | if (!test_bit(Faulty, &rdev->flags) && |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 5102 | mddev->recovery_disabled != conf->recovery_disabled && |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 5103 | !has_failed(conf) && |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5104 | number < conf->raid_disks) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5105 | err = -EBUSY; |
| 5106 | goto abort; |
| 5107 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5108 | p->rdev = NULL; |
Paul E. McKenney | fbd568a3e | 2005-05-01 08:59:04 -0700 | [diff] [blame] | 5109 | synchronize_rcu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5110 | if (atomic_read(&rdev->nr_pending)) { |
| 5111 | /* lost the race, try later */ |
| 5112 | err = -EBUSY; |
| 5113 | p->rdev = rdev; |
| 5114 | } |
| 5115 | } |
| 5116 | abort: |
| 5117 | |
| 5118 | print_raid5_conf(conf); |
| 5119 | return err; |
| 5120 | } |
| 5121 | |
| 5122 | static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev) |
| 5123 | { |
| 5124 | raid5_conf_t *conf = mddev->private; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5125 | int err = -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5126 | int disk; |
| 5127 | struct disk_info *p; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5128 | int first = 0; |
| 5129 | int last = conf->raid_disks - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5130 | |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 5131 | if (mddev->recovery_disabled == conf->recovery_disabled) |
| 5132 | return -EBUSY; |
| 5133 | |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 5134 | if (has_failed(conf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5135 | /* no point adding a device */ |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5136 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5137 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5138 | if (rdev->raid_disk >= 0) |
| 5139 | first = last = rdev->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5140 | |
| 5141 | /* |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5142 | * find the disk ... but prefer rdev->saved_raid_disk |
| 5143 | * if possible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5144 | */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5145 | if (rdev->saved_raid_disk >= 0 && |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5146 | rdev->saved_raid_disk >= first && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5147 | conf->disks[rdev->saved_raid_disk].rdev == NULL) |
| 5148 | disk = rdev->saved_raid_disk; |
| 5149 | else |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5150 | disk = first; |
| 5151 | for ( ; disk <= last ; disk++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5152 | if ((p=conf->disks + disk)->rdev == NULL) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5153 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5154 | rdev->raid_disk = disk; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5155 | err = 0; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5156 | if (rdev->saved_raid_disk != disk) |
| 5157 | conf->fullsync = 1; |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 5158 | rcu_assign_pointer(p->rdev, rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5159 | break; |
| 5160 | } |
| 5161 | print_raid5_conf(conf); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5162 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5163 | } |
| 5164 | |
| 5165 | static int raid5_resize(mddev_t *mddev, sector_t sectors) |
| 5166 | { |
| 5167 | /* no resync is happening, and there is enough space |
| 5168 | * on all devices, so we can resize. |
| 5169 | * We need to make sure resync covers any new space. |
| 5170 | * If the array is shrinking we should possibly wait until |
| 5171 | * any io in the removed space completes, but it hardly seems |
| 5172 | * worth it. |
| 5173 | */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5174 | sectors &= ~((sector_t)mddev->chunk_sectors - 1); |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5175 | md_set_array_sectors(mddev, raid5_size(mddev, sectors, |
| 5176 | mddev->raid_disks)); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5177 | if (mddev->array_sectors > |
| 5178 | raid5_size(mddev, sectors, mddev->raid_disks)) |
| 5179 | return -EINVAL; |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 5180 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5181 | revalidate_disk(mddev->gendisk); |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 5182 | if (sectors > mddev->dev_sectors && |
| 5183 | mddev->recovery_cp > mddev->dev_sectors) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5184 | mddev->recovery_cp = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5185 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5186 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5187 | mddev->dev_sectors = sectors; |
NeilBrown | 4b5c7ae | 2005-07-27 11:43:28 -0700 | [diff] [blame] | 5188 | mddev->resync_max_sectors = sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5189 | return 0; |
| 5190 | } |
| 5191 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5192 | static int check_stripe_cache(mddev_t *mddev) |
| 5193 | { |
| 5194 | /* Can only proceed if there are plenty of stripe_heads. |
| 5195 | * We need a minimum of one full stripe,, and for sensible progress |
| 5196 | * it is best to have about 4 times that. |
| 5197 | * If we require 4 times, then the default 256 4K stripe_heads will |
| 5198 | * allow for chunk sizes up to 256K, which is probably OK. |
| 5199 | * If the chunk size is greater, user-space should request more |
| 5200 | * stripe_heads first. |
| 5201 | */ |
| 5202 | raid5_conf_t *conf = mddev->private; |
| 5203 | if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4 |
| 5204 | > conf->max_nr_stripes || |
| 5205 | ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4 |
| 5206 | > conf->max_nr_stripes) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5207 | printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n", |
| 5208 | mdname(mddev), |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5209 | ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9) |
| 5210 | / STRIPE_SIZE)*4); |
| 5211 | return 0; |
| 5212 | } |
| 5213 | return 1; |
| 5214 | } |
| 5215 | |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5216 | static int check_reshape(mddev_t *mddev) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5217 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 5218 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5219 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5220 | if (mddev->delta_disks == 0 && |
| 5221 | mddev->new_layout == mddev->layout && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5222 | mddev->new_chunk_sectors == mddev->chunk_sectors) |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5223 | return 0; /* nothing to do */ |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5224 | if (mddev->bitmap) |
| 5225 | /* Cannot grow a bitmap yet */ |
| 5226 | return -EBUSY; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 5227 | if (has_failed(conf)) |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5228 | return -EINVAL; |
| 5229 | if (mddev->delta_disks < 0) { |
| 5230 | /* We might be able to shrink, but the devices must |
| 5231 | * be made bigger first. |
| 5232 | * For raid6, 4 is the minimum size. |
| 5233 | * Otherwise 2 is the minimum |
| 5234 | */ |
| 5235 | int min = 2; |
| 5236 | if (mddev->level == 6) |
| 5237 | min = 4; |
| 5238 | if (mddev->raid_disks + mddev->delta_disks < min) |
| 5239 | return -EINVAL; |
| 5240 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5241 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5242 | if (!check_stripe_cache(mddev)) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5243 | return -ENOSPC; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5244 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5245 | return resize_stripes(conf, conf->raid_disks + mddev->delta_disks); |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5246 | } |
| 5247 | |
| 5248 | static int raid5_start_reshape(mddev_t *mddev) |
| 5249 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 5250 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5251 | mdk_rdev_t *rdev; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5252 | int spares = 0; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 5253 | unsigned long flags; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5254 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5255 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5256 | return -EBUSY; |
| 5257 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5258 | if (!check_stripe_cache(mddev)) |
| 5259 | return -ENOSPC; |
| 5260 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5261 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 469518a | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5262 | if (!test_bit(In_sync, &rdev->flags) |
| 5263 | && !test_bit(Faulty, &rdev->flags)) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5264 | spares++; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5265 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5266 | if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5267 | /* Not enough devices even to make a degraded array |
| 5268 | * of that size |
| 5269 | */ |
| 5270 | return -EINVAL; |
| 5271 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5272 | /* Refuse to reduce size of the array. Any reductions in |
| 5273 | * array size must be through explicit setting of array_size |
| 5274 | * attribute. |
| 5275 | */ |
| 5276 | if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks) |
| 5277 | < mddev->array_sectors) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5278 | printk(KERN_ERR "md/raid:%s: array size must be reduced " |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5279 | "before number of disks\n", mdname(mddev)); |
| 5280 | return -EINVAL; |
| 5281 | } |
| 5282 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5283 | atomic_set(&conf->reshape_stripes, 0); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5284 | spin_lock_irq(&conf->device_lock); |
| 5285 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5286 | conf->raid_disks += mddev->delta_disks; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5287 | conf->prev_chunk_sectors = conf->chunk_sectors; |
| 5288 | conf->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5289 | conf->prev_algo = conf->algorithm; |
| 5290 | conf->algorithm = mddev->new_layout; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5291 | if (mddev->delta_disks < 0) |
| 5292 | conf->reshape_progress = raid5_size(mddev, 0, 0); |
| 5293 | else |
| 5294 | conf->reshape_progress = 0; |
| 5295 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 5296 | conf->generation++; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5297 | spin_unlock_irq(&conf->device_lock); |
| 5298 | |
| 5299 | /* Add some new drives, as many as will fit. |
| 5300 | * We know there are enough to make the newly sized array work. |
NeilBrown | 3424bf6 | 2010-06-17 17:48:26 +1000 | [diff] [blame] | 5301 | * Don't add devices if we are reducing the number of |
| 5302 | * devices in the array. This is because it is not possible |
| 5303 | * to correctly record the "partially reconstructed" state of |
| 5304 | * such devices during the reshape and confusion could result. |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5305 | */ |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5306 | if (mddev->delta_disks >= 0) { |
| 5307 | int added_devices = 0; |
| 5308 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 5309 | if (rdev->raid_disk < 0 && |
| 5310 | !test_bit(Faulty, &rdev->flags)) { |
| 5311 | if (raid5_add_disk(mddev, rdev) == 0) { |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5312 | if (rdev->raid_disk |
| 5313 | >= conf->previous_raid_disks) { |
| 5314 | set_bit(In_sync, &rdev->flags); |
| 5315 | added_devices++; |
| 5316 | } else |
| 5317 | rdev->recovery_offset = 0; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5318 | |
| 5319 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5320 | /* Failure here is OK */; |
NeilBrown | 50da084 | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5321 | } |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5322 | } else if (rdev->raid_disk >= conf->previous_raid_disks |
| 5323 | && !test_bit(Faulty, &rdev->flags)) { |
| 5324 | /* This is a spare that was manually added */ |
| 5325 | set_bit(In_sync, &rdev->flags); |
| 5326 | added_devices++; |
| 5327 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5328 | |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5329 | /* When a reshape changes the number of devices, |
| 5330 | * ->degraded is measured against the larger of the |
| 5331 | * pre and post number of devices. |
| 5332 | */ |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5333 | spin_lock_irqsave(&conf->device_lock, flags); |
NeilBrown | 9eb07c2 | 2010-02-09 12:31:47 +1100 | [diff] [blame] | 5334 | mddev->degraded += (conf->raid_disks - conf->previous_raid_disks) |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5335 | - added_devices; |
| 5336 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 5337 | } |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5338 | mddev->raid_disks = conf->raid_disks; |
NeilBrown | e516402 | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 5339 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5340 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5341 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5342 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 5343 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 5344 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 5345 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 5346 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 5347 | "reshape"); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5348 | if (!mddev->sync_thread) { |
| 5349 | mddev->recovery = 0; |
| 5350 | spin_lock_irq(&conf->device_lock); |
| 5351 | mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5352 | conf->reshape_progress = MaxSector; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5353 | spin_unlock_irq(&conf->device_lock); |
| 5354 | return -EAGAIN; |
| 5355 | } |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5356 | conf->reshape_checkpoint = jiffies; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5357 | md_wakeup_thread(mddev->sync_thread); |
| 5358 | md_new_event(mddev); |
| 5359 | return 0; |
| 5360 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5361 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5362 | /* This is called from the reshape thread and should make any |
| 5363 | * changes needed in 'conf' |
| 5364 | */ |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5365 | static void end_reshape(raid5_conf_t *conf) |
| 5366 | { |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5367 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5368 | if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) { |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 5369 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5370 | spin_lock_irq(&conf->device_lock); |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5371 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5372 | conf->reshape_progress = MaxSector; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5373 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 5374 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5375 | |
| 5376 | /* read-ahead size must cover two whole stripes, which is |
| 5377 | * 2 * (datadisks) * chunksize where 'n' is the number of raid devices |
| 5378 | */ |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 5379 | if (conf->mddev->queue) { |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5380 | int data_disks = conf->raid_disks - conf->max_degraded; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5381 | int stripe = data_disks * ((conf->chunk_sectors << 9) |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5382 | / PAGE_SIZE); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5383 | if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 5384 | conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
| 5385 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5386 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5387 | } |
| 5388 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5389 | /* This is called from the raid5d thread with mddev_lock held. |
| 5390 | * It makes config changes to the device. |
| 5391 | */ |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5392 | static void raid5_finish_reshape(mddev_t *mddev) |
| 5393 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 5394 | raid5_conf_t *conf = mddev->private; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5395 | |
| 5396 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 5397 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5398 | if (mddev->delta_disks > 0) { |
| 5399 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
| 5400 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5401 | revalidate_disk(mddev->gendisk); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5402 | } else { |
| 5403 | int d; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5404 | mddev->degraded = conf->raid_disks; |
| 5405 | for (d = 0; d < conf->raid_disks ; d++) |
| 5406 | if (conf->disks[d].rdev && |
| 5407 | test_bit(In_sync, |
| 5408 | &conf->disks[d].rdev->flags)) |
| 5409 | mddev->degraded--; |
| 5410 | for (d = conf->raid_disks ; |
| 5411 | d < conf->raid_disks - mddev->delta_disks; |
NeilBrown | 1a67dde | 2009-08-13 10:41:49 +1000 | [diff] [blame] | 5412 | d++) { |
| 5413 | mdk_rdev_t *rdev = conf->disks[d].rdev; |
| 5414 | if (rdev && raid5_remove_disk(mddev, d) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5415 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | 1a67dde | 2009-08-13 10:41:49 +1000 | [diff] [blame] | 5416 | rdev->raid_disk = -1; |
| 5417 | } |
| 5418 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5419 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5420 | mddev->layout = conf->algorithm; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5421 | mddev->chunk_sectors = conf->chunk_sectors; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5422 | mddev->reshape_position = MaxSector; |
| 5423 | mddev->delta_disks = 0; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5424 | } |
| 5425 | } |
| 5426 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5427 | static void raid5_quiesce(mddev_t *mddev, int state) |
| 5428 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 5429 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5430 | |
| 5431 | switch(state) { |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5432 | case 2: /* resume for a suspend */ |
| 5433 | wake_up(&conf->wait_for_overlap); |
| 5434 | break; |
| 5435 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5436 | case 1: /* stop all writes */ |
| 5437 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5438 | /* '2' tells resync/reshape to pause so that all |
| 5439 | * active stripes can drain |
| 5440 | */ |
| 5441 | conf->quiesce = 2; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5442 | wait_event_lock_irq(conf->wait_for_stripe, |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5443 | atomic_read(&conf->active_stripes) == 0 && |
| 5444 | atomic_read(&conf->active_aligned_reads) == 0, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5445 | conf->device_lock, /* nothing */); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5446 | conf->quiesce = 1; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5447 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5448 | /* allow reshape to continue */ |
| 5449 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5450 | break; |
| 5451 | |
| 5452 | case 0: /* re-enable writes */ |
| 5453 | spin_lock_irq(&conf->device_lock); |
| 5454 | conf->quiesce = 0; |
| 5455 | wake_up(&conf->wait_for_stripe); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5456 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5457 | spin_unlock_irq(&conf->device_lock); |
| 5458 | break; |
| 5459 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5460 | } |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5461 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5462 | |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5463 | static void *raid45_takeover_raid0(mddev_t *mddev, int level) |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 5464 | { |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5465 | struct raid0_private_data *raid0_priv = mddev->private; |
Randy Dunlap | d76c842 | 2011-04-21 09:07:26 -0700 | [diff] [blame] | 5466 | sector_t sectors; |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 5467 | |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5468 | /* for raid0 takeover only one zone is supported */ |
| 5469 | if (raid0_priv->nr_strip_zones > 1) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5470 | printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n", |
| 5471 | mdname(mddev)); |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5472 | return ERR_PTR(-EINVAL); |
| 5473 | } |
| 5474 | |
NeilBrown | 3b71bd9 | 2011-04-20 15:38:18 +1000 | [diff] [blame] | 5475 | sectors = raid0_priv->strip_zone[0].zone_end; |
| 5476 | sector_div(sectors, raid0_priv->strip_zone[0].nb_dev); |
| 5477 | mddev->dev_sectors = sectors; |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5478 | mddev->new_level = level; |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 5479 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 5480 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
| 5481 | mddev->raid_disks += 1; |
| 5482 | mddev->delta_disks = 1; |
| 5483 | /* make sure it will be not marked as dirty */ |
| 5484 | mddev->recovery_cp = MaxSector; |
| 5485 | |
| 5486 | return setup_conf(mddev); |
| 5487 | } |
| 5488 | |
| 5489 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5490 | static void *raid5_takeover_raid1(mddev_t *mddev) |
| 5491 | { |
| 5492 | int chunksect; |
| 5493 | |
| 5494 | if (mddev->raid_disks != 2 || |
| 5495 | mddev->degraded > 1) |
| 5496 | return ERR_PTR(-EINVAL); |
| 5497 | |
| 5498 | /* Should check if there are write-behind devices? */ |
| 5499 | |
| 5500 | chunksect = 64*2; /* 64K by default */ |
| 5501 | |
| 5502 | /* The array must be an exact multiple of chunksize */ |
| 5503 | while (chunksect && (mddev->array_sectors & (chunksect-1))) |
| 5504 | chunksect >>= 1; |
| 5505 | |
| 5506 | if ((chunksect<<9) < STRIPE_SIZE) |
| 5507 | /* array size does not allow a suitable chunk size */ |
| 5508 | return ERR_PTR(-EINVAL); |
| 5509 | |
| 5510 | mddev->new_level = 5; |
| 5511 | mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5512 | mddev->new_chunk_sectors = chunksect; |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5513 | |
| 5514 | return setup_conf(mddev); |
| 5515 | } |
| 5516 | |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 5517 | static void *raid5_takeover_raid6(mddev_t *mddev) |
| 5518 | { |
| 5519 | int new_layout; |
| 5520 | |
| 5521 | switch (mddev->layout) { |
| 5522 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 5523 | new_layout = ALGORITHM_LEFT_ASYMMETRIC; |
| 5524 | break; |
| 5525 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 5526 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC; |
| 5527 | break; |
| 5528 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 5529 | new_layout = ALGORITHM_LEFT_SYMMETRIC; |
| 5530 | break; |
| 5531 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 5532 | new_layout = ALGORITHM_RIGHT_SYMMETRIC; |
| 5533 | break; |
| 5534 | case ALGORITHM_PARITY_0_6: |
| 5535 | new_layout = ALGORITHM_PARITY_0; |
| 5536 | break; |
| 5537 | case ALGORITHM_PARITY_N: |
| 5538 | new_layout = ALGORITHM_PARITY_N; |
| 5539 | break; |
| 5540 | default: |
| 5541 | return ERR_PTR(-EINVAL); |
| 5542 | } |
| 5543 | mddev->new_level = 5; |
| 5544 | mddev->new_layout = new_layout; |
| 5545 | mddev->delta_disks = -1; |
| 5546 | mddev->raid_disks -= 1; |
| 5547 | return setup_conf(mddev); |
| 5548 | } |
| 5549 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5550 | |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5551 | static int raid5_check_reshape(mddev_t *mddev) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5552 | { |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5553 | /* For a 2-drive array, the layout and chunk size can be changed |
| 5554 | * immediately as not restriping is needed. |
| 5555 | * For larger arrays we record the new value - after validation |
| 5556 | * to be used by a reshape pass. |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5557 | */ |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 5558 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5559 | int new_chunk = mddev->new_chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5560 | |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5561 | if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5562 | return -EINVAL; |
| 5563 | if (new_chunk > 0) { |
Andre Noll | 0ba459d | 2009-06-18 08:46:10 +1000 | [diff] [blame] | 5564 | if (!is_power_of_2(new_chunk)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5565 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5566 | if (new_chunk < (PAGE_SIZE>>9)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5567 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5568 | if (mddev->array_sectors & (new_chunk-1)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5569 | /* not factor of array size */ |
| 5570 | return -EINVAL; |
| 5571 | } |
| 5572 | |
| 5573 | /* They look valid */ |
| 5574 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5575 | if (mddev->raid_disks == 2) { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5576 | /* can make the change immediately */ |
| 5577 | if (mddev->new_layout >= 0) { |
| 5578 | conf->algorithm = mddev->new_layout; |
| 5579 | mddev->layout = mddev->new_layout; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5580 | } |
| 5581 | if (new_chunk > 0) { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5582 | conf->chunk_sectors = new_chunk ; |
| 5583 | mddev->chunk_sectors = new_chunk; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5584 | } |
| 5585 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 5586 | md_wakeup_thread(mddev->thread); |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5587 | } |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5588 | return check_reshape(mddev); |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5589 | } |
| 5590 | |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5591 | static int raid6_check_reshape(mddev_t *mddev) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5592 | { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5593 | int new_chunk = mddev->new_chunk_sectors; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5594 | |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5595 | if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5596 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5597 | if (new_chunk > 0) { |
Andre Noll | 0ba459d | 2009-06-18 08:46:10 +1000 | [diff] [blame] | 5598 | if (!is_power_of_2(new_chunk)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5599 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5600 | if (new_chunk < (PAGE_SIZE >> 9)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5601 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5602 | if (mddev->array_sectors & (new_chunk-1)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5603 | /* not factor of array size */ |
| 5604 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5605 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5606 | |
| 5607 | /* They look valid */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5608 | return check_reshape(mddev); |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5609 | } |
| 5610 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5611 | static void *raid5_takeover(mddev_t *mddev) |
| 5612 | { |
| 5613 | /* raid5 can take over: |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5614 | * raid0 - if there is only one strip zone - make it a raid4 layout |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5615 | * raid1 - if there are two drives. We need to know the chunk size |
| 5616 | * raid4 - trivial - just use a raid4 layout. |
| 5617 | * raid6 - Providing it is a *_6 layout |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5618 | */ |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5619 | if (mddev->level == 0) |
| 5620 | return raid45_takeover_raid0(mddev, 5); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5621 | if (mddev->level == 1) |
| 5622 | return raid5_takeover_raid1(mddev); |
NeilBrown | e9d4758 | 2009-03-31 14:57:09 +1100 | [diff] [blame] | 5623 | if (mddev->level == 4) { |
| 5624 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 5625 | mddev->new_level = 5; |
| 5626 | return setup_conf(mddev); |
| 5627 | } |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 5628 | if (mddev->level == 6) |
| 5629 | return raid5_takeover_raid6(mddev); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5630 | |
| 5631 | return ERR_PTR(-EINVAL); |
| 5632 | } |
| 5633 | |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 5634 | static void *raid4_takeover(mddev_t *mddev) |
| 5635 | { |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5636 | /* raid4 can take over: |
| 5637 | * raid0 - if there is only one strip zone |
| 5638 | * raid5 - if layout is right |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 5639 | */ |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5640 | if (mddev->level == 0) |
| 5641 | return raid45_takeover_raid0(mddev, 4); |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 5642 | if (mddev->level == 5 && |
| 5643 | mddev->layout == ALGORITHM_PARITY_N) { |
| 5644 | mddev->new_layout = 0; |
| 5645 | mddev->new_level = 4; |
| 5646 | return setup_conf(mddev); |
| 5647 | } |
| 5648 | return ERR_PTR(-EINVAL); |
| 5649 | } |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5650 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5651 | static struct mdk_personality raid5_personality; |
| 5652 | |
| 5653 | static void *raid6_takeover(mddev_t *mddev) |
| 5654 | { |
| 5655 | /* Currently can only take over a raid5. We map the |
| 5656 | * personality to an equivalent raid6 personality |
| 5657 | * with the Q block at the end. |
| 5658 | */ |
| 5659 | int new_layout; |
| 5660 | |
| 5661 | if (mddev->pers != &raid5_personality) |
| 5662 | return ERR_PTR(-EINVAL); |
| 5663 | if (mddev->degraded > 1) |
| 5664 | return ERR_PTR(-EINVAL); |
| 5665 | if (mddev->raid_disks > 253) |
| 5666 | return ERR_PTR(-EINVAL); |
| 5667 | if (mddev->raid_disks < 3) |
| 5668 | return ERR_PTR(-EINVAL); |
| 5669 | |
| 5670 | switch (mddev->layout) { |
| 5671 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 5672 | new_layout = ALGORITHM_LEFT_ASYMMETRIC_6; |
| 5673 | break; |
| 5674 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 5675 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6; |
| 5676 | break; |
| 5677 | case ALGORITHM_LEFT_SYMMETRIC: |
| 5678 | new_layout = ALGORITHM_LEFT_SYMMETRIC_6; |
| 5679 | break; |
| 5680 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 5681 | new_layout = ALGORITHM_RIGHT_SYMMETRIC_6; |
| 5682 | break; |
| 5683 | case ALGORITHM_PARITY_0: |
| 5684 | new_layout = ALGORITHM_PARITY_0_6; |
| 5685 | break; |
| 5686 | case ALGORITHM_PARITY_N: |
| 5687 | new_layout = ALGORITHM_PARITY_N; |
| 5688 | break; |
| 5689 | default: |
| 5690 | return ERR_PTR(-EINVAL); |
| 5691 | } |
| 5692 | mddev->new_level = 6; |
| 5693 | mddev->new_layout = new_layout; |
| 5694 | mddev->delta_disks = 1; |
| 5695 | mddev->raid_disks += 1; |
| 5696 | return setup_conf(mddev); |
| 5697 | } |
| 5698 | |
| 5699 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5700 | static struct mdk_personality raid6_personality = |
| 5701 | { |
| 5702 | .name = "raid6", |
| 5703 | .level = 6, |
| 5704 | .owner = THIS_MODULE, |
| 5705 | .make_request = make_request, |
| 5706 | .run = run, |
| 5707 | .stop = stop, |
| 5708 | .status = status, |
| 5709 | .error_handler = error, |
| 5710 | .hot_add_disk = raid5_add_disk, |
| 5711 | .hot_remove_disk= raid5_remove_disk, |
| 5712 | .spare_active = raid5_spare_active, |
| 5713 | .sync_request = sync_request, |
| 5714 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 5715 | .size = raid5_size, |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5716 | .check_reshape = raid6_check_reshape, |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5717 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5718 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5719 | .quiesce = raid5_quiesce, |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5720 | .takeover = raid6_takeover, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5721 | }; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5722 | static struct mdk_personality raid5_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5723 | { |
| 5724 | .name = "raid5", |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5725 | .level = 5, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5726 | .owner = THIS_MODULE, |
| 5727 | .make_request = make_request, |
| 5728 | .run = run, |
| 5729 | .stop = stop, |
| 5730 | .status = status, |
| 5731 | .error_handler = error, |
| 5732 | .hot_add_disk = raid5_add_disk, |
| 5733 | .hot_remove_disk= raid5_remove_disk, |
| 5734 | .spare_active = raid5_spare_active, |
| 5735 | .sync_request = sync_request, |
| 5736 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 5737 | .size = raid5_size, |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5738 | .check_reshape = raid5_check_reshape, |
| 5739 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5740 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5741 | .quiesce = raid5_quiesce, |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5742 | .takeover = raid5_takeover, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5743 | }; |
| 5744 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5745 | static struct mdk_personality raid4_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5746 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5747 | .name = "raid4", |
| 5748 | .level = 4, |
| 5749 | .owner = THIS_MODULE, |
| 5750 | .make_request = make_request, |
| 5751 | .run = run, |
| 5752 | .stop = stop, |
| 5753 | .status = status, |
| 5754 | .error_handler = error, |
| 5755 | .hot_add_disk = raid5_add_disk, |
| 5756 | .hot_remove_disk= raid5_remove_disk, |
| 5757 | .spare_active = raid5_spare_active, |
| 5758 | .sync_request = sync_request, |
| 5759 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 5760 | .size = raid5_size, |
NeilBrown | 3d37890 | 2007-03-26 21:32:13 -0800 | [diff] [blame] | 5761 | .check_reshape = raid5_check_reshape, |
| 5762 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5763 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5764 | .quiesce = raid5_quiesce, |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 5765 | .takeover = raid4_takeover, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5766 | }; |
| 5767 | |
| 5768 | static int __init raid5_init(void) |
| 5769 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5770 | register_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5771 | register_md_personality(&raid5_personality); |
| 5772 | register_md_personality(&raid4_personality); |
| 5773 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5774 | } |
| 5775 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5776 | static void raid5_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5777 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5778 | unregister_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5779 | unregister_md_personality(&raid5_personality); |
| 5780 | unregister_md_personality(&raid4_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5781 | } |
| 5782 | |
| 5783 | module_init(raid5_init); |
| 5784 | module_exit(raid5_exit); |
| 5785 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 5786 | MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5787 | MODULE_ALIAS("md-personality-4"); /* RAID5 */ |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5788 | MODULE_ALIAS("md-raid5"); |
| 5789 | MODULE_ALIAS("md-raid4"); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5790 | MODULE_ALIAS("md-level-5"); |
| 5791 | MODULE_ALIAS("md-level-4"); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5792 | MODULE_ALIAS("md-personality-8"); /* RAID6 */ |
| 5793 | MODULE_ALIAS("md-raid6"); |
| 5794 | MODULE_ALIAS("md-level-6"); |
| 5795 | |
| 5796 | /* This used to be two separate modules, they were: */ |
| 5797 | MODULE_ALIAS("raid5"); |
| 5798 | MODULE_ALIAS("raid6"); |