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. |
| 30 | * conf->bm_write is the number of the last batch successfully written. |
| 31 | * conf->bm_flush is the number of the last batch that was closed to |
| 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 |
| 35 | * the number of the batch it will be in. This is bm_flush+1. |
| 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> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 54 | #include "md.h" |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 55 | #include "raid5.h" |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 56 | #include "raid0.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 57 | #include "bitmap.h" |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | /* |
| 60 | * Stripe cache |
| 61 | */ |
| 62 | |
| 63 | #define NR_STRIPES 256 |
| 64 | #define STRIPE_SIZE PAGE_SIZE |
| 65 | #define STRIPE_SHIFT (PAGE_SHIFT - 9) |
| 66 | #define STRIPE_SECTORS (STRIPE_SIZE>>9) |
| 67 | #define IO_THRESHOLD 1 |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 68 | #define BYPASS_THRESHOLD 1 |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 69 | #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | #define HASH_MASK (NR_HASH - 1) |
| 71 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 72 | #define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK])) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 74 | /* bio's attached to a stripe+device for I/O are linked together in bi_sector |
| 75 | * order without overlap. There may be several bio's per stripe+device, and |
| 76 | * a bio could span several devices. |
| 77 | * When walking this list for a particular stripe+device, we must never proceed |
| 78 | * beyond a bio that extends past this device, as the next bio might no longer |
| 79 | * be valid. |
| 80 | * This macro is used to determine the 'next' bio in the list, given the sector |
| 81 | * of the current stripe+device |
| 82 | */ |
| 83 | #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL) |
| 84 | /* |
| 85 | * The following can be used to debug the driver |
| 86 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | #define RAID5_PARANOIA 1 |
| 88 | #if RAID5_PARANOIA && defined(CONFIG_SMP) |
| 89 | # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock) |
| 90 | #else |
| 91 | # define CHECK_DEVLOCK() |
| 92 | #endif |
| 93 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 94 | #ifdef DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | #define inline |
| 96 | #define __inline__ |
| 97 | #endif |
| 98 | |
Bernd Schubert | 6be9d49 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 99 | #define printk_rl(args...) ((void) (printk_ratelimit() && printk(args))) |
| 100 | |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 101 | /* |
Jens Axboe | 5b99c2f | 2008-08-15 10:56:11 +0200 | [diff] [blame] | 102 | * We maintain a biased count of active stripes in the bottom 16 bits of |
| 103 | * 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] | 104 | */ |
| 105 | static inline int raid5_bi_phys_segments(struct bio *bio) |
| 106 | { |
Jens Axboe | 5b99c2f | 2008-08-15 10:56:11 +0200 | [diff] [blame] | 107 | return bio->bi_phys_segments & 0xffff; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static inline int raid5_bi_hw_segments(struct bio *bio) |
| 111 | { |
Jens Axboe | 5b99c2f | 2008-08-15 10:56:11 +0200 | [diff] [blame] | 112 | return (bio->bi_phys_segments >> 16) & 0xffff; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static inline int raid5_dec_bi_phys_segments(struct bio *bio) |
| 116 | { |
| 117 | --bio->bi_phys_segments; |
| 118 | return raid5_bi_phys_segments(bio); |
| 119 | } |
| 120 | |
| 121 | static inline int raid5_dec_bi_hw_segments(struct bio *bio) |
| 122 | { |
| 123 | unsigned short val = raid5_bi_hw_segments(bio); |
| 124 | |
| 125 | --val; |
Jens Axboe | 5b99c2f | 2008-08-15 10:56:11 +0200 | [diff] [blame] | 126 | bio->bi_phys_segments = (val << 16) | raid5_bi_phys_segments(bio); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 127 | return val; |
| 128 | } |
| 129 | |
| 130 | static inline void raid5_set_bi_hw_segments(struct bio *bio, unsigned int cnt) |
| 131 | { |
Jens Axboe | 5b99c2f | 2008-08-15 10:56:11 +0200 | [diff] [blame] | 132 | bio->bi_phys_segments = raid5_bi_phys_segments(bio) || (cnt << 16); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 133 | } |
| 134 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 135 | /* Find first data disk in a raid6 stripe */ |
| 136 | static inline int raid6_d0(struct stripe_head *sh) |
| 137 | { |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 138 | if (sh->ddf_layout) |
| 139 | /* ddf always start from first device */ |
| 140 | return 0; |
| 141 | /* md starts just after Q block */ |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 142 | if (sh->qd_idx == sh->disks - 1) |
| 143 | return 0; |
| 144 | else |
| 145 | return sh->qd_idx + 1; |
| 146 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 147 | static inline int raid6_next_disk(int disk, int raid_disks) |
| 148 | { |
| 149 | disk++; |
| 150 | return (disk < raid_disks) ? disk : 0; |
| 151 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 152 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 153 | /* When walking through the disks in a raid5, starting at raid6_d0, |
| 154 | * We need to map each disk to a 'slot', where the data disks are slot |
| 155 | * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk |
| 156 | * is raid_disks-1. This help does that mapping. |
| 157 | */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 158 | static int raid6_idx_to_slot(int idx, struct stripe_head *sh, |
| 159 | int *count, int syndrome_disks) |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 160 | { |
Dan Williams | 6629542 | 2009-10-19 18:09:32 -0700 | [diff] [blame] | 161 | int slot = *count; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 162 | |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 163 | if (sh->ddf_layout) |
Dan Williams | 6629542 | 2009-10-19 18:09:32 -0700 | [diff] [blame] | 164 | (*count)++; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 165 | if (idx == sh->pd_idx) |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 166 | return syndrome_disks; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 167 | if (idx == sh->qd_idx) |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 168 | return syndrome_disks + 1; |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 169 | if (!sh->ddf_layout) |
Dan Williams | 6629542 | 2009-10-19 18:09:32 -0700 | [diff] [blame] | 170 | (*count)++; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 171 | return slot; |
| 172 | } |
| 173 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 174 | static void return_io(struct bio *return_bi) |
| 175 | { |
| 176 | struct bio *bi = return_bi; |
| 177 | while (bi) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 178 | |
| 179 | return_bi = bi->bi_next; |
| 180 | bi->bi_next = NULL; |
| 181 | bi->bi_size = 0; |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 182 | bio_endio(bi, 0); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 183 | bi = return_bi; |
| 184 | } |
| 185 | } |
| 186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | static void print_raid5_conf (raid5_conf_t *conf); |
| 188 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 189 | static int stripe_operations_active(struct stripe_head *sh) |
| 190 | { |
| 191 | return sh->check_state || sh->reconstruct_state || |
| 192 | test_bit(STRIPE_BIOFILL_RUN, &sh->state) || |
| 193 | test_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 194 | } |
| 195 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 196 | static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | { |
| 198 | if (atomic_dec_and_test(&sh->count)) { |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 199 | BUG_ON(!list_empty(&sh->lru)); |
| 200 | BUG_ON(atomic_read(&conf->active_stripes)==0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | if (test_bit(STRIPE_HANDLE, &sh->state)) { |
NeilBrown | 7c785b7 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 202 | if (test_bit(STRIPE_DELAYED, &sh->state)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | list_add_tail(&sh->lru, &conf->delayed_list); |
NeilBrown | 7c785b7 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 204 | blk_plug_device(conf->mddev->queue); |
| 205 | } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) && |
NeilBrown | ae3c20c | 2006-07-10 04:44:17 -0700 | [diff] [blame] | 206 | sh->bm_seq - conf->seq_write > 0) { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 207 | list_add_tail(&sh->lru, &conf->bitmap_list); |
NeilBrown | 7c785b7 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 208 | blk_plug_device(conf->mddev->queue); |
| 209 | } else { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 210 | clear_bit(STRIPE_BIT_DELAY, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | list_add_tail(&sh->lru, &conf->handle_list); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 212 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | md_wakeup_thread(conf->mddev->thread); |
| 214 | } else { |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 215 | BUG_ON(stripe_operations_active(sh)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
| 217 | atomic_dec(&conf->preread_active_stripes); |
| 218 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) |
| 219 | md_wakeup_thread(conf->mddev->thread); |
| 220 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | atomic_dec(&conf->active_stripes); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 222 | if (!test_bit(STRIPE_EXPANDING, &sh->state)) { |
| 223 | list_add_tail(&sh->lru, &conf->inactive_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | wake_up(&conf->wait_for_stripe); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 225 | if (conf->retry_read_aligned) |
| 226 | md_wakeup_thread(conf->mddev->thread); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 227 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | } |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | static void release_stripe(struct stripe_head *sh) |
| 233 | { |
| 234 | raid5_conf_t *conf = sh->raid_conf; |
| 235 | unsigned long flags; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | spin_lock_irqsave(&conf->device_lock, flags); |
| 238 | __release_stripe(conf, sh); |
| 239 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 240 | } |
| 241 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 242 | static inline void remove_hash(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 244 | pr_debug("remove_hash(), stripe %llu\n", |
| 245 | (unsigned long long)sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 247 | hlist_del_init(&sh->hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | } |
| 249 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 250 | 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] | 251 | { |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 252 | struct hlist_head *hp = stripe_hash(conf, sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 254 | pr_debug("insert_hash(), stripe %llu\n", |
| 255 | (unsigned long long)sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
| 257 | CHECK_DEVLOCK(); |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 258 | hlist_add_head(&sh->hash, hp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | |
| 262 | /* find an idle stripe, make sure it is unhashed, and return it. */ |
| 263 | static struct stripe_head *get_free_stripe(raid5_conf_t *conf) |
| 264 | { |
| 265 | struct stripe_head *sh = NULL; |
| 266 | struct list_head *first; |
| 267 | |
| 268 | CHECK_DEVLOCK(); |
| 269 | if (list_empty(&conf->inactive_list)) |
| 270 | goto out; |
| 271 | first = conf->inactive_list.next; |
| 272 | sh = list_entry(first, struct stripe_head, lru); |
| 273 | list_del_init(first); |
| 274 | remove_hash(sh); |
| 275 | atomic_inc(&conf->active_stripes); |
| 276 | out: |
| 277 | return sh; |
| 278 | } |
| 279 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 280 | static void shrink_buffers(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | { |
| 282 | struct page *p; |
| 283 | int i; |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 284 | int num = sh->raid_conf->pool_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 286 | for (i = 0; i < num ; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | p = sh->dev[i].page; |
| 288 | if (!p) |
| 289 | continue; |
| 290 | sh->dev[i].page = NULL; |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 291 | put_page(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 295 | static int grow_buffers(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | { |
| 297 | int i; |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 298 | int num = sh->raid_conf->pool_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 300 | for (i = 0; i < num; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | struct page *page; |
| 302 | |
| 303 | if (!(page = alloc_page(GFP_KERNEL))) { |
| 304 | return 1; |
| 305 | } |
| 306 | sh->dev[i].page = page; |
| 307 | } |
| 308 | return 0; |
| 309 | } |
| 310 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 311 | static void raid5_build_block(struct stripe_head *sh, int i, int previous); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 312 | static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous, |
| 313 | struct stripe_head *sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 315 | 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] | 316 | { |
| 317 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 318 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 320 | BUG_ON(atomic_read(&sh->count) != 0); |
| 321 | BUG_ON(test_bit(STRIPE_HANDLE, &sh->state)); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 322 | BUG_ON(stripe_operations_active(sh)); |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 323 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | CHECK_DEVLOCK(); |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 325 | pr_debug("init_stripe called, stripe %llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | (unsigned long long)sh->sector); |
| 327 | |
| 328 | remove_hash(sh); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 329 | |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 330 | sh->generation = conf->generation - previous; |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 331 | sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | sh->sector = sector; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 333 | stripe_set_idx(sector, conf, previous, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | sh->state = 0; |
| 335 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 336 | |
| 337 | for (i = sh->disks; i--; ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | struct r5dev *dev = &sh->dev[i]; |
| 339 | |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 340 | if (dev->toread || dev->read || dev->towrite || dev->written || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | test_bit(R5_LOCKED, &dev->flags)) { |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 342 | 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] | 343 | (unsigned long long)sh->sector, i, dev->toread, |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 344 | dev->read, dev->towrite, dev->written, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | test_bit(R5_LOCKED, &dev->flags)); |
| 346 | BUG(); |
| 347 | } |
| 348 | dev->flags = 0; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 349 | raid5_build_block(sh, i, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | } |
| 351 | insert_hash(conf, sh); |
| 352 | } |
| 353 | |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 354 | static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector, |
| 355 | short generation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | { |
| 357 | struct stripe_head *sh; |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 358 | struct hlist_node *hn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | |
| 360 | CHECK_DEVLOCK(); |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 361 | pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector); |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 362 | hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash) |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 363 | if (sh->sector == sector && sh->generation == generation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | return sh; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 365 | pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | return NULL; |
| 367 | } |
| 368 | |
| 369 | static void unplug_slaves(mddev_t *mddev); |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 370 | static void raid5_unplug_device(struct request_queue *q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 372 | static struct stripe_head * |
| 373 | get_active_stripe(raid5_conf_t *conf, sector_t sector, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 374 | int previous, int noblock, int noquiesce) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | { |
| 376 | struct stripe_head *sh; |
| 377 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 378 | pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | |
| 380 | spin_lock_irq(&conf->device_lock); |
| 381 | |
| 382 | do { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 383 | wait_event_lock_irq(conf->wait_for_stripe, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 384 | conf->quiesce == 0 || noquiesce, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 385 | conf->device_lock, /* nothing */); |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 386 | sh = __find_stripe(conf, sector, conf->generation - previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | if (!sh) { |
| 388 | if (!conf->inactive_blocked) |
| 389 | sh = get_free_stripe(conf); |
| 390 | if (noblock && sh == NULL) |
| 391 | break; |
| 392 | if (!sh) { |
| 393 | conf->inactive_blocked = 1; |
| 394 | wait_event_lock_irq(conf->wait_for_stripe, |
| 395 | !list_empty(&conf->inactive_list) && |
NeilBrown | 5036805 | 2005-12-12 02:39:17 -0800 | [diff] [blame] | 396 | (atomic_read(&conf->active_stripes) |
| 397 | < (conf->max_nr_stripes *3/4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | || !conf->inactive_blocked), |
| 399 | conf->device_lock, |
NeilBrown | f437078 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 400 | raid5_unplug_device(conf->mddev->queue) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | ); |
| 402 | conf->inactive_blocked = 0; |
| 403 | } else |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 404 | init_stripe(sh, sector, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | } else { |
| 406 | if (atomic_read(&sh->count)) { |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 407 | BUG_ON(!list_empty(&sh->lru) |
| 408 | && !test_bit(STRIPE_EXPANDING, &sh->state)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | } else { |
| 410 | if (!test_bit(STRIPE_HANDLE, &sh->state)) |
| 411 | atomic_inc(&conf->active_stripes); |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 412 | if (list_empty(&sh->lru) && |
| 413 | !test_bit(STRIPE_EXPANDING, &sh->state)) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 414 | BUG(); |
| 415 | list_del_init(&sh->lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | } while (sh == NULL); |
| 419 | |
| 420 | if (sh) |
| 421 | atomic_inc(&sh->count); |
| 422 | |
| 423 | spin_unlock_irq(&conf->device_lock); |
| 424 | return sh; |
| 425 | } |
| 426 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 427 | static void |
| 428 | raid5_end_read_request(struct bio *bi, int error); |
| 429 | static void |
| 430 | raid5_end_write_request(struct bio *bi, int error); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 431 | |
Dan Williams | c4e5ac0 | 2008-06-28 08:31:53 +1000 | [diff] [blame] | 432 | 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] | 433 | { |
| 434 | raid5_conf_t *conf = sh->raid_conf; |
| 435 | int i, disks = sh->disks; |
| 436 | |
| 437 | might_sleep(); |
| 438 | |
| 439 | for (i = disks; i--; ) { |
| 440 | int rw; |
| 441 | struct bio *bi; |
| 442 | mdk_rdev_t *rdev; |
| 443 | if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) |
| 444 | rw = WRITE; |
| 445 | else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags)) |
| 446 | rw = READ; |
| 447 | else |
| 448 | continue; |
| 449 | |
| 450 | bi = &sh->dev[i].req; |
| 451 | |
| 452 | bi->bi_rw = rw; |
| 453 | if (rw == WRITE) |
| 454 | bi->bi_end_io = raid5_end_write_request; |
| 455 | else |
| 456 | bi->bi_end_io = raid5_end_read_request; |
| 457 | |
| 458 | rcu_read_lock(); |
| 459 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 460 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 461 | rdev = NULL; |
| 462 | if (rdev) |
| 463 | atomic_inc(&rdev->nr_pending); |
| 464 | rcu_read_unlock(); |
| 465 | |
| 466 | if (rdev) { |
Dan Williams | c4e5ac0 | 2008-06-28 08:31:53 +1000 | [diff] [blame] | 467 | if (s->syncing || s->expanding || s->expanded) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 468 | md_sync_acct(rdev->bdev, STRIPE_SECTORS); |
| 469 | |
Dan Williams | 2b7497f | 2008-06-28 08:31:52 +1000 | [diff] [blame] | 470 | set_bit(STRIPE_IO_STARTED, &sh->state); |
| 471 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 472 | bi->bi_bdev = rdev->bdev; |
| 473 | pr_debug("%s: for %llu schedule op %ld on disc %d\n", |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 474 | __func__, (unsigned long long)sh->sector, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 475 | bi->bi_rw, i); |
| 476 | atomic_inc(&sh->count); |
| 477 | bi->bi_sector = sh->sector + rdev->data_offset; |
| 478 | bi->bi_flags = 1 << BIO_UPTODATE; |
| 479 | bi->bi_vcnt = 1; |
| 480 | bi->bi_max_vecs = 1; |
| 481 | bi->bi_idx = 0; |
| 482 | bi->bi_io_vec = &sh->dev[i].vec; |
| 483 | bi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
| 484 | bi->bi_io_vec[0].bv_offset = 0; |
| 485 | bi->bi_size = STRIPE_SIZE; |
| 486 | bi->bi_next = NULL; |
| 487 | if (rw == WRITE && |
| 488 | test_bit(R5_ReWrite, &sh->dev[i].flags)) |
| 489 | atomic_add(STRIPE_SECTORS, |
| 490 | &rdev->corrected_errors); |
| 491 | generic_make_request(bi); |
| 492 | } else { |
| 493 | if (rw == WRITE) |
| 494 | set_bit(STRIPE_DEGRADED, &sh->state); |
| 495 | pr_debug("skip op %ld on disc %d for sector %llu\n", |
| 496 | bi->bi_rw, i, (unsigned long long)sh->sector); |
| 497 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 498 | set_bit(STRIPE_HANDLE, &sh->state); |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | static struct dma_async_tx_descriptor * |
| 504 | async_copy_data(int frombio, struct bio *bio, struct page *page, |
| 505 | sector_t sector, struct dma_async_tx_descriptor *tx) |
| 506 | { |
| 507 | struct bio_vec *bvl; |
| 508 | struct page *bio_page; |
| 509 | int i; |
| 510 | int page_offset; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 511 | struct async_submit_ctl submit; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 512 | enum async_tx_flags flags = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 513 | |
| 514 | if (bio->bi_sector >= sector) |
| 515 | page_offset = (signed)(bio->bi_sector - sector) * 512; |
| 516 | else |
| 517 | page_offset = (signed)(sector - bio->bi_sector) * -512; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 518 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 519 | if (frombio) |
| 520 | flags |= ASYNC_TX_FENCE; |
| 521 | init_async_submit(&submit, flags, tx, NULL, NULL, NULL); |
| 522 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 523 | bio_for_each_segment(bvl, bio, i) { |
| 524 | int len = bio_iovec_idx(bio, i)->bv_len; |
| 525 | int clen; |
| 526 | int b_offset = 0; |
| 527 | |
| 528 | if (page_offset < 0) { |
| 529 | b_offset = -page_offset; |
| 530 | page_offset += b_offset; |
| 531 | len -= b_offset; |
| 532 | } |
| 533 | |
| 534 | if (len > 0 && page_offset + len > STRIPE_SIZE) |
| 535 | clen = STRIPE_SIZE - page_offset; |
| 536 | else |
| 537 | clen = len; |
| 538 | |
| 539 | if (clen > 0) { |
| 540 | b_offset += bio_iovec_idx(bio, i)->bv_offset; |
| 541 | bio_page = bio_iovec_idx(bio, i)->bv_page; |
| 542 | if (frombio) |
| 543 | tx = async_memcpy(page, bio_page, page_offset, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 544 | b_offset, clen, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 545 | else |
| 546 | tx = async_memcpy(bio_page, page, b_offset, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 547 | page_offset, clen, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 548 | } |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 549 | /* chain the operations */ |
| 550 | submit.depend_tx = tx; |
| 551 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 552 | if (clen < len) /* hit end of page */ |
| 553 | break; |
| 554 | page_offset += len; |
| 555 | } |
| 556 | |
| 557 | return tx; |
| 558 | } |
| 559 | |
| 560 | static void ops_complete_biofill(void *stripe_head_ref) |
| 561 | { |
| 562 | struct stripe_head *sh = stripe_head_ref; |
| 563 | struct bio *return_bi = NULL; |
| 564 | raid5_conf_t *conf = sh->raid_conf; |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 565 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 566 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 567 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 568 | (unsigned long long)sh->sector); |
| 569 | |
| 570 | /* clear completed biofills */ |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 571 | spin_lock_irq(&conf->device_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 572 | for (i = sh->disks; i--; ) { |
| 573 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 574 | |
| 575 | /* acknowledge completion of a biofill operation */ |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 576 | /* and check if we need to reply to a read request, |
| 577 | * new R5_Wantfill requests are held off until |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 578 | * !STRIPE_BIOFILL_RUN |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 579 | */ |
| 580 | if (test_and_clear_bit(R5_Wantfill, &dev->flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 581 | struct bio *rbi, *rbi2; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 582 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 583 | BUG_ON(!dev->read); |
| 584 | rbi = dev->read; |
| 585 | dev->read = NULL; |
| 586 | while (rbi && rbi->bi_sector < |
| 587 | dev->sector + STRIPE_SECTORS) { |
| 588 | rbi2 = r5_next_bio(rbi, dev->sector); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 589 | if (!raid5_dec_bi_phys_segments(rbi)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 590 | rbi->bi_next = return_bi; |
| 591 | return_bi = rbi; |
| 592 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 593 | rbi = rbi2; |
| 594 | } |
| 595 | } |
| 596 | } |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 597 | spin_unlock_irq(&conf->device_lock); |
| 598 | clear_bit(STRIPE_BIOFILL_RUN, &sh->state); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 599 | |
| 600 | return_io(return_bi); |
| 601 | |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 602 | set_bit(STRIPE_HANDLE, &sh->state); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 603 | release_stripe(sh); |
| 604 | } |
| 605 | |
| 606 | static void ops_run_biofill(struct stripe_head *sh) |
| 607 | { |
| 608 | struct dma_async_tx_descriptor *tx = NULL; |
| 609 | raid5_conf_t *conf = sh->raid_conf; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 610 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 611 | int i; |
| 612 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 613 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 614 | (unsigned long long)sh->sector); |
| 615 | |
| 616 | for (i = sh->disks; i--; ) { |
| 617 | struct r5dev *dev = &sh->dev[i]; |
| 618 | if (test_bit(R5_Wantfill, &dev->flags)) { |
| 619 | struct bio *rbi; |
| 620 | spin_lock_irq(&conf->device_lock); |
| 621 | dev->read = rbi = dev->toread; |
| 622 | dev->toread = NULL; |
| 623 | spin_unlock_irq(&conf->device_lock); |
| 624 | while (rbi && rbi->bi_sector < |
| 625 | dev->sector + STRIPE_SECTORS) { |
| 626 | tx = async_copy_data(0, rbi, dev->page, |
| 627 | dev->sector, tx); |
| 628 | rbi = r5_next_bio(rbi, dev->sector); |
| 629 | } |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 634 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL); |
| 635 | async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 636 | } |
| 637 | |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 638 | static void mark_target_uptodate(struct stripe_head *sh, int target) |
| 639 | { |
| 640 | struct r5dev *tgt; |
| 641 | |
| 642 | if (target < 0) |
| 643 | return; |
| 644 | |
| 645 | tgt = &sh->dev[target]; |
| 646 | set_bit(R5_UPTODATE, &tgt->flags); |
| 647 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 648 | clear_bit(R5_Wantcompute, &tgt->flags); |
| 649 | } |
| 650 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 651 | static void ops_complete_compute(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 652 | { |
| 653 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 654 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 655 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 656 | (unsigned long long)sh->sector); |
| 657 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 658 | /* mark the computed target(s) as uptodate */ |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 659 | mark_target_uptodate(sh, sh->ops.target); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 660 | mark_target_uptodate(sh, sh->ops.target2); |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 661 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 662 | clear_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 663 | if (sh->check_state == check_state_compute_run) |
| 664 | sh->check_state = check_state_compute_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 665 | set_bit(STRIPE_HANDLE, &sh->state); |
| 666 | release_stripe(sh); |
| 667 | } |
| 668 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 669 | /* return a pointer to the address conversion region of the scribble buffer */ |
| 670 | static addr_conv_t *to_addr_conv(struct stripe_head *sh, |
| 671 | struct raid5_percpu *percpu) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 672 | { |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 673 | return percpu->scribble + sizeof(struct page *) * (sh->disks + 2); |
| 674 | } |
| 675 | |
| 676 | static struct dma_async_tx_descriptor * |
| 677 | ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 678 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 679 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 680 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 681 | int target = sh->ops.target; |
| 682 | struct r5dev *tgt = &sh->dev[target]; |
| 683 | struct page *xor_dest = tgt->page; |
| 684 | int count = 0; |
| 685 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 686 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 687 | int i; |
| 688 | |
| 689 | pr_debug("%s: stripe %llu block: %d\n", |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 690 | __func__, (unsigned long long)sh->sector, target); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 691 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 692 | |
| 693 | for (i = disks; i--; ) |
| 694 | if (i != target) |
| 695 | xor_srcs[count++] = sh->dev[i].page; |
| 696 | |
| 697 | atomic_inc(&sh->count); |
| 698 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 699 | 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] | 700 | ops_complete_compute, sh, to_addr_conv(sh, percpu)); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 701 | if (unlikely(count == 1)) |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 702 | 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] | 703 | else |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 704 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 705 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 706 | return tx; |
| 707 | } |
| 708 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 709 | /* set_syndrome_sources - populate source buffers for gen_syndrome |
| 710 | * @srcs - (struct page *) array of size sh->disks |
| 711 | * @sh - stripe_head to parse |
| 712 | * |
| 713 | * Populates srcs in proper layout order for the stripe and returns the |
| 714 | * 'count' of sources to be used in a call to async_gen_syndrome. The P |
| 715 | * destination buffer is recorded in srcs[count] and the Q destination |
| 716 | * is recorded in srcs[count+1]]. |
| 717 | */ |
| 718 | static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh) |
| 719 | { |
| 720 | int disks = sh->disks; |
| 721 | int syndrome_disks = sh->ddf_layout ? disks : (disks - 2); |
| 722 | int d0_idx = raid6_d0(sh); |
| 723 | int count; |
| 724 | int i; |
| 725 | |
| 726 | for (i = 0; i < disks; i++) |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 727 | srcs[i] = NULL; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 728 | |
| 729 | count = 0; |
| 730 | i = d0_idx; |
| 731 | do { |
| 732 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 733 | |
| 734 | srcs[slot] = sh->dev[i].page; |
| 735 | i = raid6_next_disk(i, disks); |
| 736 | } while (i != d0_idx); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 737 | |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 738 | return syndrome_disks; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | static struct dma_async_tx_descriptor * |
| 742 | ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 743 | { |
| 744 | int disks = sh->disks; |
| 745 | struct page **blocks = percpu->scribble; |
| 746 | int target; |
| 747 | int qd_idx = sh->qd_idx; |
| 748 | struct dma_async_tx_descriptor *tx; |
| 749 | struct async_submit_ctl submit; |
| 750 | struct r5dev *tgt; |
| 751 | struct page *dest; |
| 752 | int i; |
| 753 | int count; |
| 754 | |
| 755 | if (sh->ops.target < 0) |
| 756 | target = sh->ops.target2; |
| 757 | else if (sh->ops.target2 < 0) |
| 758 | target = sh->ops.target; |
| 759 | else |
| 760 | /* we should only have one valid target */ |
| 761 | BUG(); |
| 762 | BUG_ON(target < 0); |
| 763 | pr_debug("%s: stripe %llu block: %d\n", |
| 764 | __func__, (unsigned long long)sh->sector, target); |
| 765 | |
| 766 | tgt = &sh->dev[target]; |
| 767 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 768 | dest = tgt->page; |
| 769 | |
| 770 | atomic_inc(&sh->count); |
| 771 | |
| 772 | if (target == qd_idx) { |
| 773 | count = set_syndrome_sources(blocks, sh); |
| 774 | blocks[count] = NULL; /* regenerating p is not necessary */ |
| 775 | BUG_ON(blocks[count+1] != dest); /* q should already be set */ |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 776 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 777 | ops_complete_compute, sh, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 778 | to_addr_conv(sh, percpu)); |
| 779 | tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
| 780 | } else { |
| 781 | /* Compute any data- or p-drive using XOR */ |
| 782 | count = 0; |
| 783 | for (i = disks; i-- ; ) { |
| 784 | if (i == target || i == qd_idx) |
| 785 | continue; |
| 786 | blocks[count++] = sh->dev[i].page; |
| 787 | } |
| 788 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 789 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
| 790 | NULL, ops_complete_compute, sh, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 791 | to_addr_conv(sh, percpu)); |
| 792 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit); |
| 793 | } |
| 794 | |
| 795 | return tx; |
| 796 | } |
| 797 | |
| 798 | static struct dma_async_tx_descriptor * |
| 799 | ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 800 | { |
| 801 | int i, count, disks = sh->disks; |
| 802 | int syndrome_disks = sh->ddf_layout ? disks : disks-2; |
| 803 | int d0_idx = raid6_d0(sh); |
| 804 | int faila = -1, failb = -1; |
| 805 | int target = sh->ops.target; |
| 806 | int target2 = sh->ops.target2; |
| 807 | struct r5dev *tgt = &sh->dev[target]; |
| 808 | struct r5dev *tgt2 = &sh->dev[target2]; |
| 809 | struct dma_async_tx_descriptor *tx; |
| 810 | struct page **blocks = percpu->scribble; |
| 811 | struct async_submit_ctl submit; |
| 812 | |
| 813 | pr_debug("%s: stripe %llu block1: %d block2: %d\n", |
| 814 | __func__, (unsigned long long)sh->sector, target, target2); |
| 815 | BUG_ON(target < 0 || target2 < 0); |
| 816 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 817 | BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags)); |
| 818 | |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 819 | /* we need to open-code set_syndrome_sources to handle the |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 820 | * slot number conversion for 'faila' and 'failb' |
| 821 | */ |
| 822 | for (i = 0; i < disks ; i++) |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 823 | blocks[i] = NULL; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 824 | count = 0; |
| 825 | i = d0_idx; |
| 826 | do { |
| 827 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 828 | |
| 829 | blocks[slot] = sh->dev[i].page; |
| 830 | |
| 831 | if (i == target) |
| 832 | faila = slot; |
| 833 | if (i == target2) |
| 834 | failb = slot; |
| 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 | |
| 838 | BUG_ON(faila == failb); |
| 839 | if (failb < faila) |
| 840 | swap(faila, failb); |
| 841 | pr_debug("%s: stripe: %llu faila: %d failb: %d\n", |
| 842 | __func__, (unsigned long long)sh->sector, faila, failb); |
| 843 | |
| 844 | atomic_inc(&sh->count); |
| 845 | |
| 846 | if (failb == syndrome_disks+1) { |
| 847 | /* Q disk is one of the missing disks */ |
| 848 | if (faila == syndrome_disks) { |
| 849 | /* Missing P+Q, just recompute */ |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 850 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 851 | ops_complete_compute, sh, |
| 852 | to_addr_conv(sh, percpu)); |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 853 | return async_gen_syndrome(blocks, 0, syndrome_disks+2, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 854 | STRIPE_SIZE, &submit); |
| 855 | } else { |
| 856 | struct page *dest; |
| 857 | int data_target; |
| 858 | int qd_idx = sh->qd_idx; |
| 859 | |
| 860 | /* Missing D+Q: recompute D from P, then recompute Q */ |
| 861 | if (target == qd_idx) |
| 862 | data_target = target2; |
| 863 | else |
| 864 | data_target = target; |
| 865 | |
| 866 | count = 0; |
| 867 | for (i = disks; i-- ; ) { |
| 868 | if (i == data_target || i == qd_idx) |
| 869 | continue; |
| 870 | blocks[count++] = sh->dev[i].page; |
| 871 | } |
| 872 | dest = sh->dev[data_target].page; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 873 | init_async_submit(&submit, |
| 874 | ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
| 875 | NULL, NULL, NULL, |
| 876 | to_addr_conv(sh, percpu)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 877 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, |
| 878 | &submit); |
| 879 | |
| 880 | count = set_syndrome_sources(blocks, sh); |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 881 | init_async_submit(&submit, ASYNC_TX_FENCE, tx, |
| 882 | ops_complete_compute, sh, |
| 883 | to_addr_conv(sh, percpu)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 884 | return async_gen_syndrome(blocks, 0, count+2, |
| 885 | STRIPE_SIZE, &submit); |
| 886 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 887 | } else { |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 888 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 889 | ops_complete_compute, sh, |
| 890 | to_addr_conv(sh, percpu)); |
| 891 | if (failb == syndrome_disks) { |
| 892 | /* We're missing D+P. */ |
| 893 | return async_raid6_datap_recov(syndrome_disks+2, |
| 894 | STRIPE_SIZE, faila, |
| 895 | blocks, &submit); |
| 896 | } else { |
| 897 | /* We're missing D+D. */ |
| 898 | return async_raid6_2data_recov(syndrome_disks+2, |
| 899 | STRIPE_SIZE, faila, failb, |
| 900 | blocks, &submit); |
| 901 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 902 | } |
| 903 | } |
| 904 | |
| 905 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 906 | static void ops_complete_prexor(void *stripe_head_ref) |
| 907 | { |
| 908 | struct stripe_head *sh = stripe_head_ref; |
| 909 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 910 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 911 | (unsigned long long)sh->sector); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | static struct dma_async_tx_descriptor * |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 915 | ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 916 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 917 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 918 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 919 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 920 | int count = 0, pd_idx = sh->pd_idx, i; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 921 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 922 | |
| 923 | /* existing parity data subtracted */ |
| 924 | struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 925 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 926 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 927 | (unsigned long long)sh->sector); |
| 928 | |
| 929 | for (i = disks; i--; ) { |
| 930 | struct r5dev *dev = &sh->dev[i]; |
| 931 | /* Only process blocks that are known to be uptodate */ |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 932 | if (test_bit(R5_Wantdrain, &dev->flags)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 933 | xor_srcs[count++] = dev->page; |
| 934 | } |
| 935 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 936 | 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] | 937 | ops_complete_prexor, sh, to_addr_conv(sh, percpu)); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 938 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 939 | |
| 940 | return tx; |
| 941 | } |
| 942 | |
| 943 | static struct dma_async_tx_descriptor * |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 944 | 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] | 945 | { |
| 946 | int disks = sh->disks; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 947 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 948 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 949 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 950 | (unsigned long long)sh->sector); |
| 951 | |
| 952 | for (i = disks; i--; ) { |
| 953 | struct r5dev *dev = &sh->dev[i]; |
| 954 | struct bio *chosen; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 955 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 956 | if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 957 | struct bio *wbi; |
| 958 | |
| 959 | spin_lock(&sh->lock); |
| 960 | chosen = dev->towrite; |
| 961 | dev->towrite = NULL; |
| 962 | BUG_ON(dev->written); |
| 963 | wbi = dev->written = chosen; |
| 964 | spin_unlock(&sh->lock); |
| 965 | |
| 966 | while (wbi && wbi->bi_sector < |
| 967 | dev->sector + STRIPE_SECTORS) { |
| 968 | tx = async_copy_data(1, wbi, dev->page, |
| 969 | dev->sector, tx); |
| 970 | wbi = r5_next_bio(wbi, dev->sector); |
| 971 | } |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | return tx; |
| 976 | } |
| 977 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 978 | static void ops_complete_reconstruct(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 979 | { |
| 980 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 981 | int disks = sh->disks; |
| 982 | int pd_idx = sh->pd_idx; |
| 983 | int qd_idx = sh->qd_idx; |
| 984 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 985 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 986 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 987 | (unsigned long long)sh->sector); |
| 988 | |
| 989 | for (i = disks; i--; ) { |
| 990 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 991 | |
| 992 | if (dev->written || i == pd_idx || i == qd_idx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 993 | set_bit(R5_UPTODATE, &dev->flags); |
| 994 | } |
| 995 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 996 | if (sh->reconstruct_state == reconstruct_state_drain_run) |
| 997 | sh->reconstruct_state = reconstruct_state_drain_result; |
| 998 | else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) |
| 999 | sh->reconstruct_state = reconstruct_state_prexor_drain_result; |
| 1000 | else { |
| 1001 | BUG_ON(sh->reconstruct_state != reconstruct_state_run); |
| 1002 | sh->reconstruct_state = reconstruct_state_result; |
| 1003 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1004 | |
| 1005 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1006 | release_stripe(sh); |
| 1007 | } |
| 1008 | |
| 1009 | static void |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1010 | ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1011 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1012 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1013 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1014 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1015 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1016 | int count = 0, pd_idx = sh->pd_idx, i; |
| 1017 | struct page *xor_dest; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1018 | int prexor = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1019 | unsigned long flags; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1020 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1021 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1022 | (unsigned long long)sh->sector); |
| 1023 | |
| 1024 | /* check if prexor is active which means only process blocks |
| 1025 | * that are part of a read-modify-write (written) |
| 1026 | */ |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1027 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) { |
| 1028 | prexor = 1; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1029 | xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 1030 | for (i = disks; i--; ) { |
| 1031 | struct r5dev *dev = &sh->dev[i]; |
| 1032 | if (dev->written) |
| 1033 | xor_srcs[count++] = dev->page; |
| 1034 | } |
| 1035 | } else { |
| 1036 | xor_dest = sh->dev[pd_idx].page; |
| 1037 | for (i = disks; i--; ) { |
| 1038 | struct r5dev *dev = &sh->dev[i]; |
| 1039 | if (i != pd_idx) |
| 1040 | xor_srcs[count++] = dev->page; |
| 1041 | } |
| 1042 | } |
| 1043 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1044 | /* 1/ if we prexor'd then the dest is reused as a source |
| 1045 | * 2/ if we did not prexor then we are redoing the parity |
| 1046 | * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST |
| 1047 | * for the synchronous xor case |
| 1048 | */ |
Dan Williams | 88ba2aa | 2009-04-09 16:16:18 -0700 | [diff] [blame] | 1049 | flags = ASYNC_TX_ACK | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1050 | (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST); |
| 1051 | |
| 1052 | atomic_inc(&sh->count); |
| 1053 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1054 | init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh, |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1055 | to_addr_conv(sh, percpu)); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1056 | if (unlikely(count == 1)) |
| 1057 | tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit); |
| 1058 | else |
| 1059 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1060 | } |
| 1061 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1062 | static void |
| 1063 | ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1064 | struct dma_async_tx_descriptor *tx) |
| 1065 | { |
| 1066 | struct async_submit_ctl submit; |
| 1067 | struct page **blocks = percpu->scribble; |
| 1068 | int count; |
| 1069 | |
| 1070 | pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector); |
| 1071 | |
| 1072 | count = set_syndrome_sources(blocks, sh); |
| 1073 | |
| 1074 | atomic_inc(&sh->count); |
| 1075 | |
| 1076 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct, |
| 1077 | sh, to_addr_conv(sh, percpu)); |
| 1078 | async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | static void ops_complete_check(void *stripe_head_ref) |
| 1082 | { |
| 1083 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1084 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1085 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1086 | (unsigned long long)sh->sector); |
| 1087 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 1088 | sh->check_state = check_state_check_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1089 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1090 | release_stripe(sh); |
| 1091 | } |
| 1092 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1093 | 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] | 1094 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1095 | int disks = sh->disks; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1096 | int pd_idx = sh->pd_idx; |
| 1097 | int qd_idx = sh->qd_idx; |
| 1098 | struct page *xor_dest; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1099 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1100 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1101 | struct async_submit_ctl submit; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1102 | int count; |
| 1103 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1104 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1105 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1106 | (unsigned long long)sh->sector); |
| 1107 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1108 | count = 0; |
| 1109 | xor_dest = sh->dev[pd_idx].page; |
| 1110 | xor_srcs[count++] = xor_dest; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1111 | for (i = disks; i--; ) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1112 | if (i == pd_idx || i == qd_idx) |
| 1113 | continue; |
| 1114 | xor_srcs[count++] = sh->dev[i].page; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1115 | } |
| 1116 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1117 | init_async_submit(&submit, 0, NULL, NULL, NULL, |
| 1118 | to_addr_conv(sh, percpu)); |
Dan Williams | 099f53c | 2009-04-08 14:28:37 -0700 | [diff] [blame] | 1119 | tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1120 | &sh->ops.zero_sum_result, &submit); |
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 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1123 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL); |
| 1124 | tx = async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1125 | } |
| 1126 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1127 | static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp) |
| 1128 | { |
| 1129 | struct page **srcs = percpu->scribble; |
| 1130 | struct async_submit_ctl submit; |
| 1131 | int count; |
| 1132 | |
| 1133 | pr_debug("%s: stripe %llu checkp: %d\n", __func__, |
| 1134 | (unsigned long long)sh->sector, checkp); |
| 1135 | |
| 1136 | count = set_syndrome_sources(srcs, sh); |
| 1137 | if (!checkp) |
| 1138 | srcs[count] = NULL; |
| 1139 | |
| 1140 | atomic_inc(&sh->count); |
| 1141 | init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check, |
| 1142 | sh, to_addr_conv(sh, percpu)); |
| 1143 | async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE, |
| 1144 | &sh->ops.zero_sum_result, percpu->spare_page, &submit); |
| 1145 | } |
| 1146 | |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 1147 | 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] | 1148 | { |
| 1149 | int overlap_clear = 0, i, disks = sh->disks; |
| 1150 | struct dma_async_tx_descriptor *tx = NULL; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1151 | raid5_conf_t *conf = sh->raid_conf; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1152 | int level = conf->level; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1153 | struct raid5_percpu *percpu; |
| 1154 | unsigned long cpu; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1155 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1156 | cpu = get_cpu(); |
| 1157 | percpu = per_cpu_ptr(conf->percpu, cpu); |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 1158 | if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1159 | ops_run_biofill(sh); |
| 1160 | overlap_clear++; |
| 1161 | } |
| 1162 | |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 1163 | if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1164 | if (level < 6) |
| 1165 | tx = ops_run_compute5(sh, percpu); |
| 1166 | else { |
| 1167 | if (sh->ops.target2 < 0 || sh->ops.target < 0) |
| 1168 | tx = ops_run_compute6_1(sh, percpu); |
| 1169 | else |
| 1170 | tx = ops_run_compute6_2(sh, percpu); |
| 1171 | } |
| 1172 | /* terminate the chain if reconstruct is not set to be run */ |
| 1173 | if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 1174 | async_tx_ack(tx); |
| 1175 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1176 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1177 | if (test_bit(STRIPE_OP_PREXOR, &ops_request)) |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1178 | tx = ops_run_prexor(sh, percpu, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1179 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1180 | if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) { |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1181 | tx = ops_run_biodrain(sh, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1182 | overlap_clear++; |
| 1183 | } |
| 1184 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1185 | if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) { |
| 1186 | if (level < 6) |
| 1187 | ops_run_reconstruct5(sh, percpu, tx); |
| 1188 | else |
| 1189 | ops_run_reconstruct6(sh, percpu, tx); |
| 1190 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1191 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1192 | if (test_bit(STRIPE_OP_CHECK, &ops_request)) { |
| 1193 | if (sh->check_state == check_state_run) |
| 1194 | ops_run_check_p(sh, percpu); |
| 1195 | else if (sh->check_state == check_state_run_q) |
| 1196 | ops_run_check_pq(sh, percpu, 0); |
| 1197 | else if (sh->check_state == check_state_run_pq) |
| 1198 | ops_run_check_pq(sh, percpu, 1); |
| 1199 | else |
| 1200 | BUG(); |
| 1201 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1202 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1203 | if (overlap_clear) |
| 1204 | for (i = disks; i--; ) { |
| 1205 | struct r5dev *dev = &sh->dev[i]; |
| 1206 | if (test_and_clear_bit(R5_Overlap, &dev->flags)) |
| 1207 | wake_up(&sh->raid_conf->wait_for_overlap); |
| 1208 | } |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1209 | put_cpu(); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1210 | } |
| 1211 | |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 1212 | #ifdef CONFIG_MULTICORE_RAID456 |
| 1213 | static void async_run_ops(void *param, async_cookie_t cookie) |
| 1214 | { |
| 1215 | struct stripe_head *sh = param; |
| 1216 | unsigned long ops_request = sh->ops.request; |
| 1217 | |
| 1218 | clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state); |
| 1219 | wake_up(&sh->ops.wait_for_ops); |
| 1220 | |
| 1221 | __raid_run_ops(sh, ops_request); |
| 1222 | release_stripe(sh); |
| 1223 | } |
| 1224 | |
| 1225 | static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request) |
| 1226 | { |
| 1227 | /* since handle_stripe can be called outside of raid5d context |
| 1228 | * we need to ensure sh->ops.request is de-staged before another |
| 1229 | * request arrives |
| 1230 | */ |
| 1231 | wait_event(sh->ops.wait_for_ops, |
| 1232 | !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state)); |
| 1233 | sh->ops.request = ops_request; |
| 1234 | |
| 1235 | atomic_inc(&sh->count); |
| 1236 | async_schedule(async_run_ops, sh); |
| 1237 | } |
| 1238 | #else |
| 1239 | #define raid_run_ops __raid_run_ops |
| 1240 | #endif |
| 1241 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1242 | static int grow_one_stripe(raid5_conf_t *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | { |
| 1244 | struct stripe_head *sh; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1245 | sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL); |
| 1246 | if (!sh) |
| 1247 | return 0; |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 1248 | memset(sh, 0, sizeof(*sh) + (conf->pool_size-1)*sizeof(struct r5dev)); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1249 | sh->raid_conf = conf; |
| 1250 | spin_lock_init(&sh->lock); |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 1251 | #ifdef CONFIG_MULTICORE_RAID456 |
| 1252 | init_waitqueue_head(&sh->ops.wait_for_ops); |
| 1253 | #endif |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1254 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 1255 | if (grow_buffers(sh)) { |
| 1256 | shrink_buffers(sh); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1257 | kmem_cache_free(conf->slab_cache, sh); |
| 1258 | return 0; |
| 1259 | } |
| 1260 | /* we just created an active stripe so... */ |
| 1261 | atomic_set(&sh->count, 1); |
| 1262 | atomic_inc(&conf->active_stripes); |
| 1263 | INIT_LIST_HEAD(&sh->lru); |
| 1264 | release_stripe(sh); |
| 1265 | return 1; |
| 1266 | } |
| 1267 | |
| 1268 | static int grow_stripes(raid5_conf_t *conf, int num) |
| 1269 | { |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 1270 | struct kmem_cache *sc; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 1271 | int devs = max(conf->raid_disks, conf->previous_raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 1273 | sprintf(conf->cache_name[0], |
| 1274 | "raid%d-%s", conf->level, mdname(conf->mddev)); |
| 1275 | sprintf(conf->cache_name[1], |
| 1276 | "raid%d-%s-alt", conf->level, mdname(conf->mddev)); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1277 | conf->active_name = 0; |
| 1278 | sc = kmem_cache_create(conf->cache_name[conf->active_name], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1280 | 0, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | if (!sc) |
| 1282 | return 1; |
| 1283 | conf->slab_cache = sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1284 | conf->pool_size = devs; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1285 | while (num--) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1286 | if (!grow_one_stripe(conf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | return 0; |
| 1289 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 1290 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1291 | /** |
| 1292 | * scribble_len - return the required size of the scribble region |
| 1293 | * @num - total number of disks in the array |
| 1294 | * |
| 1295 | * The size must be enough to contain: |
| 1296 | * 1/ a struct page pointer for each device in the array +2 |
| 1297 | * 2/ room to convert each entry in (1) to its corresponding dma |
| 1298 | * (dma_map_page()) or page (page_address()) address. |
| 1299 | * |
| 1300 | * Note: the +2 is for the destination buffers of the ddf/raid6 case where we |
| 1301 | * calculate over all devices (not just the data blocks), using zeros in place |
| 1302 | * of the P and Q blocks. |
| 1303 | */ |
| 1304 | static size_t scribble_len(int num) |
| 1305 | { |
| 1306 | size_t len; |
| 1307 | |
| 1308 | len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2); |
| 1309 | |
| 1310 | return len; |
| 1311 | } |
| 1312 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1313 | static int resize_stripes(raid5_conf_t *conf, int newsize) |
| 1314 | { |
| 1315 | /* Make all the stripes able to hold 'newsize' devices. |
| 1316 | * New slots in each stripe get 'page' set to a new page. |
| 1317 | * |
| 1318 | * This happens in stages: |
| 1319 | * 1/ create a new kmem_cache and allocate the required number of |
| 1320 | * stripe_heads. |
| 1321 | * 2/ gather all the old stripe_heads and tranfer the pages across |
| 1322 | * to the new stripe_heads. This will have the side effect of |
| 1323 | * freezing the array as once all stripe_heads have been collected, |
| 1324 | * no IO will be possible. Old stripe heads are freed once their |
| 1325 | * pages have been transferred over, and the old kmem_cache is |
| 1326 | * freed when all stripes are done. |
| 1327 | * 3/ reallocate conf->disks to be suitable bigger. If this fails, |
| 1328 | * we simple return a failre status - no need to clean anything up. |
| 1329 | * 4/ allocate new pages for the new slots in the new stripe_heads. |
| 1330 | * If this fails, we don't bother trying the shrink the |
| 1331 | * stripe_heads down again, we just leave them as they are. |
| 1332 | * As each stripe_head is processed the new one is released into |
| 1333 | * active service. |
| 1334 | * |
| 1335 | * Once step2 is started, we cannot afford to wait for a write, |
| 1336 | * so we use GFP_NOIO allocations. |
| 1337 | */ |
| 1338 | struct stripe_head *osh, *nsh; |
| 1339 | LIST_HEAD(newstripes); |
| 1340 | struct disk_info *ndisks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1341 | unsigned long cpu; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 1342 | int err; |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 1343 | struct kmem_cache *sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1344 | int i; |
| 1345 | |
| 1346 | if (newsize <= conf->pool_size) |
| 1347 | return 0; /* never bother to shrink */ |
| 1348 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 1349 | err = md_allow_write(conf->mddev); |
| 1350 | if (err) |
| 1351 | return err; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 1352 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1353 | /* Step 1 */ |
| 1354 | sc = kmem_cache_create(conf->cache_name[1-conf->active_name], |
| 1355 | sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1356 | 0, 0, NULL); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1357 | if (!sc) |
| 1358 | return -ENOMEM; |
| 1359 | |
| 1360 | for (i = conf->max_nr_stripes; i; i--) { |
| 1361 | nsh = kmem_cache_alloc(sc, GFP_KERNEL); |
| 1362 | if (!nsh) |
| 1363 | break; |
| 1364 | |
| 1365 | memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev)); |
| 1366 | |
| 1367 | nsh->raid_conf = conf; |
| 1368 | spin_lock_init(&nsh->lock); |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 1369 | #ifdef CONFIG_MULTICORE_RAID456 |
| 1370 | init_waitqueue_head(&nsh->ops.wait_for_ops); |
| 1371 | #endif |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1372 | |
| 1373 | list_add(&nsh->lru, &newstripes); |
| 1374 | } |
| 1375 | if (i) { |
| 1376 | /* didn't get enough, give up */ |
| 1377 | while (!list_empty(&newstripes)) { |
| 1378 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 1379 | list_del(&nsh->lru); |
| 1380 | kmem_cache_free(sc, nsh); |
| 1381 | } |
| 1382 | kmem_cache_destroy(sc); |
| 1383 | return -ENOMEM; |
| 1384 | } |
| 1385 | /* Step 2 - Must use GFP_NOIO now. |
| 1386 | * OK, we have enough stripes, start collecting inactive |
| 1387 | * stripes and copying them over |
| 1388 | */ |
| 1389 | list_for_each_entry(nsh, &newstripes, lru) { |
| 1390 | spin_lock_irq(&conf->device_lock); |
| 1391 | wait_event_lock_irq(conf->wait_for_stripe, |
| 1392 | !list_empty(&conf->inactive_list), |
| 1393 | conf->device_lock, |
NeilBrown | b3b46be | 2006-03-27 01:18:16 -0800 | [diff] [blame] | 1394 | unplug_slaves(conf->mddev) |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1395 | ); |
| 1396 | osh = get_free_stripe(conf); |
| 1397 | spin_unlock_irq(&conf->device_lock); |
| 1398 | atomic_set(&nsh->count, 1); |
| 1399 | for(i=0; i<conf->pool_size; i++) |
| 1400 | nsh->dev[i].page = osh->dev[i].page; |
| 1401 | for( ; i<newsize; i++) |
| 1402 | nsh->dev[i].page = NULL; |
| 1403 | kmem_cache_free(conf->slab_cache, osh); |
| 1404 | } |
| 1405 | kmem_cache_destroy(conf->slab_cache); |
| 1406 | |
| 1407 | /* Step 3. |
| 1408 | * At this point, we are holding all the stripes so the array |
| 1409 | * is completely stalled, so now is a good time to resize |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1410 | * conf->disks and the scribble region |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1411 | */ |
| 1412 | ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO); |
| 1413 | if (ndisks) { |
| 1414 | for (i=0; i<conf->raid_disks; i++) |
| 1415 | ndisks[i] = conf->disks[i]; |
| 1416 | kfree(conf->disks); |
| 1417 | conf->disks = ndisks; |
| 1418 | } else |
| 1419 | err = -ENOMEM; |
| 1420 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1421 | get_online_cpus(); |
| 1422 | conf->scribble_len = scribble_len(newsize); |
| 1423 | for_each_present_cpu(cpu) { |
| 1424 | struct raid5_percpu *percpu; |
| 1425 | void *scribble; |
| 1426 | |
| 1427 | percpu = per_cpu_ptr(conf->percpu, cpu); |
| 1428 | scribble = kmalloc(conf->scribble_len, GFP_NOIO); |
| 1429 | |
| 1430 | if (scribble) { |
| 1431 | kfree(percpu->scribble); |
| 1432 | percpu->scribble = scribble; |
| 1433 | } else { |
| 1434 | err = -ENOMEM; |
| 1435 | break; |
| 1436 | } |
| 1437 | } |
| 1438 | put_online_cpus(); |
| 1439 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1440 | /* Step 4, return new stripes to service */ |
| 1441 | while(!list_empty(&newstripes)) { |
| 1442 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 1443 | list_del_init(&nsh->lru); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1444 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1445 | for (i=conf->raid_disks; i < newsize; i++) |
| 1446 | if (nsh->dev[i].page == NULL) { |
| 1447 | struct page *p = alloc_page(GFP_NOIO); |
| 1448 | nsh->dev[i].page = p; |
| 1449 | if (!p) |
| 1450 | err = -ENOMEM; |
| 1451 | } |
| 1452 | release_stripe(nsh); |
| 1453 | } |
| 1454 | /* critical section pass, GFP_NOIO no longer needed */ |
| 1455 | |
| 1456 | conf->slab_cache = sc; |
| 1457 | conf->active_name = 1-conf->active_name; |
| 1458 | conf->pool_size = newsize; |
| 1459 | return err; |
| 1460 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1462 | static int drop_one_stripe(raid5_conf_t *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | { |
| 1464 | struct stripe_head *sh; |
| 1465 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1466 | spin_lock_irq(&conf->device_lock); |
| 1467 | sh = get_free_stripe(conf); |
| 1468 | spin_unlock_irq(&conf->device_lock); |
| 1469 | if (!sh) |
| 1470 | return 0; |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 1471 | BUG_ON(atomic_read(&sh->count)); |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 1472 | shrink_buffers(sh); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1473 | kmem_cache_free(conf->slab_cache, sh); |
| 1474 | atomic_dec(&conf->active_stripes); |
| 1475 | return 1; |
| 1476 | } |
| 1477 | |
| 1478 | static void shrink_stripes(raid5_conf_t *conf) |
| 1479 | { |
| 1480 | while (drop_one_stripe(conf)) |
| 1481 | ; |
| 1482 | |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1483 | if (conf->slab_cache) |
| 1484 | kmem_cache_destroy(conf->slab_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | conf->slab_cache = NULL; |
| 1486 | } |
| 1487 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1488 | static void raid5_end_read_request(struct bio * bi, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1490 | struct stripe_head *sh = bi->bi_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 1492 | int disks = sh->disks, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1494 | char b[BDEVNAME_SIZE]; |
| 1495 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | |
| 1498 | for (i=0 ; i<disks; i++) |
| 1499 | if (bi == &sh->dev[i].req) |
| 1500 | break; |
| 1501 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1502 | pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n", |
| 1503 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | uptodate); |
| 1505 | if (i == disks) { |
| 1506 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1507 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | } |
| 1509 | |
| 1510 | if (uptodate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | set_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1512 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1513 | rdev = conf->disks[i].rdev; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 1514 | printk_rl(KERN_INFO "md/raid:%s: read error corrected" |
Bernd Schubert | 6be9d49 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 1515 | " (%lu sectors at %llu on %s)\n", |
| 1516 | mdname(conf->mddev), STRIPE_SECTORS, |
| 1517 | (unsigned long long)(sh->sector |
| 1518 | + rdev->data_offset), |
| 1519 | bdevname(rdev->bdev, b)); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1520 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 1521 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
| 1522 | } |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1523 | if (atomic_read(&conf->disks[i].rdev->read_errors)) |
| 1524 | atomic_set(&conf->disks[i].rdev->read_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | } else { |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1526 | const char *bdn = bdevname(conf->disks[i].rdev->bdev, b); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1527 | int retry = 0; |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1528 | rdev = conf->disks[i].rdev; |
| 1529 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | clear_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1531 | atomic_inc(&rdev->read_errors); |
Gabriele A. Trombetti | 7b0bb53 | 2010-04-28 11:51:17 +1000 | [diff] [blame] | 1532 | if (conf->mddev->degraded >= conf->max_degraded) |
Bernd Schubert | 6be9d49 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 1533 | printk_rl(KERN_WARNING |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 1534 | "md/raid:%s: read error not correctable " |
Bernd Schubert | 6be9d49 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 1535 | "(sector %llu on %s).\n", |
| 1536 | mdname(conf->mddev), |
| 1537 | (unsigned long long)(sh->sector |
| 1538 | + rdev->data_offset), |
| 1539 | bdn); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1540 | else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1541 | /* Oh, no!!! */ |
Bernd Schubert | 6be9d49 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 1542 | printk_rl(KERN_WARNING |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 1543 | "md/raid:%s: read error NOT corrected!! " |
Bernd Schubert | 6be9d49 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 1544 | "(sector %llu on %s).\n", |
| 1545 | mdname(conf->mddev), |
| 1546 | (unsigned long long)(sh->sector |
| 1547 | + rdev->data_offset), |
| 1548 | bdn); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1549 | else if (atomic_read(&rdev->read_errors) |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1550 | > conf->max_nr_stripes) |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 1551 | printk(KERN_WARNING |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 1552 | "md/raid:%s: Too many read errors, failing device %s.\n", |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1553 | mdname(conf->mddev), bdn); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1554 | else |
| 1555 | retry = 1; |
| 1556 | if (retry) |
| 1557 | set_bit(R5_ReadError, &sh->dev[i].flags); |
| 1558 | else { |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1559 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 1560 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1561 | md_error(conf->mddev, rdev); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1562 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | } |
| 1564 | rdev_dec_pending(conf->disks[i].rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 1566 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1567 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | } |
| 1569 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 1570 | static void raid5_end_write_request(struct bio *bi, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1572 | struct stripe_head *sh = bi->bi_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 1574 | int disks = sh->disks, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
| 1576 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | for (i=0 ; i<disks; i++) |
| 1578 | if (bi == &sh->dev[i].req) |
| 1579 | break; |
| 1580 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1581 | pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
| 1583 | uptodate); |
| 1584 | if (i == disks) { |
| 1585 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1586 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | } |
| 1588 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | if (!uptodate) |
| 1590 | md_error(conf->mddev, conf->disks[i].rdev); |
| 1591 | |
| 1592 | rdev_dec_pending(conf->disks[i].rdev, conf->mddev); |
| 1593 | |
| 1594 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 1595 | set_bit(STRIPE_HANDLE, &sh->state); |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1596 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | } |
| 1598 | |
| 1599 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1600 | 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] | 1601 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1602 | 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] | 1603 | { |
| 1604 | struct r5dev *dev = &sh->dev[i]; |
| 1605 | |
| 1606 | bio_init(&dev->req); |
| 1607 | dev->req.bi_io_vec = &dev->vec; |
| 1608 | dev->req.bi_vcnt++; |
| 1609 | dev->req.bi_max_vecs++; |
| 1610 | dev->vec.bv_page = dev->page; |
| 1611 | dev->vec.bv_len = STRIPE_SIZE; |
| 1612 | dev->vec.bv_offset = 0; |
| 1613 | |
| 1614 | dev->req.bi_sector = sh->sector; |
| 1615 | dev->req.bi_private = sh; |
| 1616 | |
| 1617 | dev->flags = 0; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1618 | dev->sector = compute_blocknr(sh, i, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | } |
| 1620 | |
| 1621 | static void error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1622 | { |
| 1623 | char b[BDEVNAME_SIZE]; |
H Hartley Sweeten | 7b92813 | 2010-03-08 16:02:40 +1100 | [diff] [blame] | 1624 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 1625 | pr_debug("raid456: error called\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1627 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1628 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1629 | if (test_and_clear_bit(In_sync, &rdev->flags)) { |
| 1630 | unsigned long flags; |
| 1631 | spin_lock_irqsave(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | mddev->degraded++; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1633 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | /* |
| 1635 | * if recovery was running, make sure it aborts. |
| 1636 | */ |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 1637 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1639 | set_bit(Faulty, &rdev->flags); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 1640 | printk(KERN_ALERT |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 1641 | "md/raid:%s: Disk failure on %s, disabling device.\n" |
| 1642 | KERN_ALERT |
| 1643 | "md/raid:%s: Operation continuing on %d devices.\n", |
| 1644 | mdname(mddev), |
| 1645 | bdevname(rdev->bdev, b), |
| 1646 | mdname(mddev), |
| 1647 | conf->raid_disks - mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1649 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1650 | |
| 1651 | /* |
| 1652 | * Input: a 'big' sector number, |
| 1653 | * Output: index of the data and parity disk, and the sector # in them. |
| 1654 | */ |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1655 | 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] | 1656 | int previous, int *dd_idx, |
| 1657 | struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | { |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1659 | sector_t stripe, stripe2; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 1660 | sector_t chunk_number; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | unsigned int chunk_offset; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1662 | int pd_idx, qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1663 | int ddf_layout = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | sector_t new_sector; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1665 | int algorithm = previous ? conf->prev_algo |
| 1666 | : conf->algorithm; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 1667 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
| 1668 | : conf->chunk_sectors; |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1669 | int raid_disks = previous ? conf->previous_raid_disks |
| 1670 | : conf->raid_disks; |
| 1671 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | |
| 1673 | /* First compute the information on this sector */ |
| 1674 | |
| 1675 | /* |
| 1676 | * Compute the chunk number and the sector offset inside the chunk |
| 1677 | */ |
| 1678 | chunk_offset = sector_div(r_sector, sectors_per_chunk); |
| 1679 | chunk_number = r_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | |
| 1681 | /* |
| 1682 | * Compute the stripe number |
| 1683 | */ |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 1684 | stripe = chunk_number; |
| 1685 | *dd_idx = sector_div(stripe, data_disks); |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1686 | stripe2 = stripe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | /* |
| 1688 | * Select the parity disk based on the user selected algorithm. |
| 1689 | */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1690 | pd_idx = qd_idx = ~0; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1691 | switch(conf->level) { |
| 1692 | case 4: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1693 | pd_idx = data_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1694 | break; |
| 1695 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1696 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1698 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1699 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | (*dd_idx)++; |
| 1701 | break; |
| 1702 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1703 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1704 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | (*dd_idx)++; |
| 1706 | break; |
| 1707 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1708 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1709 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | break; |
| 1711 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1712 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1713 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1715 | case ALGORITHM_PARITY_0: |
| 1716 | pd_idx = 0; |
| 1717 | (*dd_idx)++; |
| 1718 | break; |
| 1719 | case ALGORITHM_PARITY_N: |
| 1720 | pd_idx = data_disks; |
| 1721 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1723 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1724 | } |
| 1725 | break; |
| 1726 | case 6: |
| 1727 | |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1728 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1729 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1730 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1731 | qd_idx = pd_idx + 1; |
| 1732 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1733 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1734 | qd_idx = 0; |
| 1735 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1736 | (*dd_idx) += 2; /* D D P Q D */ |
| 1737 | break; |
| 1738 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1739 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1740 | qd_idx = pd_idx + 1; |
| 1741 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1742 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1743 | qd_idx = 0; |
| 1744 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1745 | (*dd_idx) += 2; /* D D P Q D */ |
| 1746 | break; |
| 1747 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1748 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1749 | qd_idx = (pd_idx + 1) % raid_disks; |
| 1750 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1751 | break; |
| 1752 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1753 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1754 | qd_idx = (pd_idx + 1) % raid_disks; |
| 1755 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1756 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1757 | |
| 1758 | case ALGORITHM_PARITY_0: |
| 1759 | pd_idx = 0; |
| 1760 | qd_idx = 1; |
| 1761 | (*dd_idx) += 2; |
| 1762 | break; |
| 1763 | case ALGORITHM_PARITY_N: |
| 1764 | pd_idx = data_disks; |
| 1765 | qd_idx = data_disks + 1; |
| 1766 | break; |
| 1767 | |
| 1768 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 1769 | /* Exactly the same as RIGHT_ASYMMETRIC, but or |
| 1770 | * of blocks for computing Q is different. |
| 1771 | */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1772 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1773 | qd_idx = pd_idx + 1; |
| 1774 | if (pd_idx == raid_disks-1) { |
| 1775 | (*dd_idx)++; /* Q D D D P */ |
| 1776 | qd_idx = 0; |
| 1777 | } else if (*dd_idx >= pd_idx) |
| 1778 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1779 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1780 | break; |
| 1781 | |
| 1782 | case ALGORITHM_ROTATING_N_RESTART: |
| 1783 | /* Same a left_asymmetric, by first stripe is |
| 1784 | * D D D P Q rather than |
| 1785 | * Q D D D P |
| 1786 | */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1787 | stripe2 += 1; |
| 1788 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1789 | qd_idx = pd_idx + 1; |
| 1790 | if (pd_idx == raid_disks-1) { |
| 1791 | (*dd_idx)++; /* Q D D D P */ |
| 1792 | qd_idx = 0; |
| 1793 | } else if (*dd_idx >= pd_idx) |
| 1794 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1795 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1796 | break; |
| 1797 | |
| 1798 | case ALGORITHM_ROTATING_N_CONTINUE: |
| 1799 | /* Same as left_symmetric but Q is before P */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1800 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1801 | qd_idx = (pd_idx + raid_disks - 1) % raid_disks; |
| 1802 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1803 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1804 | break; |
| 1805 | |
| 1806 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 1807 | /* RAID5 left_asymmetric, with Q on last device */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1808 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1809 | if (*dd_idx >= pd_idx) |
| 1810 | (*dd_idx)++; |
| 1811 | qd_idx = raid_disks - 1; |
| 1812 | break; |
| 1813 | |
| 1814 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1815 | pd_idx = sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1816 | if (*dd_idx >= pd_idx) |
| 1817 | (*dd_idx)++; |
| 1818 | qd_idx = raid_disks - 1; |
| 1819 | break; |
| 1820 | |
| 1821 | case ALGORITHM_LEFT_SYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1822 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1823 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 1824 | qd_idx = raid_disks - 1; |
| 1825 | break; |
| 1826 | |
| 1827 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1828 | pd_idx = sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1829 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 1830 | qd_idx = raid_disks - 1; |
| 1831 | break; |
| 1832 | |
| 1833 | case ALGORITHM_PARITY_0_6: |
| 1834 | pd_idx = 0; |
| 1835 | (*dd_idx)++; |
| 1836 | qd_idx = raid_disks - 1; |
| 1837 | break; |
| 1838 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | } |
| 1844 | |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1845 | if (sh) { |
| 1846 | sh->pd_idx = pd_idx; |
| 1847 | sh->qd_idx = qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1848 | sh->ddf_layout = ddf_layout; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1849 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | /* |
| 1851 | * Finally, compute the new sector number |
| 1852 | */ |
| 1853 | new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset; |
| 1854 | return new_sector; |
| 1855 | } |
| 1856 | |
| 1857 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1858 | 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] | 1859 | { |
| 1860 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 1861 | int raid_disks = sh->disks; |
| 1862 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1863 | sector_t new_sector = sh->sector, check; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 1864 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
| 1865 | : conf->chunk_sectors; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1866 | int algorithm = previous ? conf->prev_algo |
| 1867 | : conf->algorithm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | sector_t stripe; |
| 1869 | int chunk_offset; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 1870 | sector_t chunk_number; |
| 1871 | int dummy1, dd_idx = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | sector_t r_sector; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1873 | struct stripe_head sh2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1874 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1875 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1876 | chunk_offset = sector_div(new_sector, sectors_per_chunk); |
| 1877 | stripe = new_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1879 | if (i == sh->pd_idx) |
| 1880 | return 0; |
| 1881 | switch(conf->level) { |
| 1882 | case 4: break; |
| 1883 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1884 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1885 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 1886 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 1887 | if (i > sh->pd_idx) |
| 1888 | i--; |
| 1889 | break; |
| 1890 | case ALGORITHM_LEFT_SYMMETRIC: |
| 1891 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 1892 | if (i < sh->pd_idx) |
| 1893 | i += raid_disks; |
| 1894 | i -= (sh->pd_idx + 1); |
| 1895 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1896 | case ALGORITHM_PARITY_0: |
| 1897 | i -= 1; |
| 1898 | break; |
| 1899 | case ALGORITHM_PARITY_N: |
| 1900 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1902 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1903 | } |
| 1904 | break; |
| 1905 | case 6: |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1906 | if (i == sh->qd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1907 | return 0; /* It is the Q disk */ |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1908 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1909 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 1910 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1911 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 1912 | case ALGORITHM_ROTATING_N_RESTART: |
| 1913 | if (sh->pd_idx == raid_disks-1) |
| 1914 | i--; /* Q D D D P */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1915 | else if (i > sh->pd_idx) |
| 1916 | i -= 2; /* D D P Q D */ |
| 1917 | break; |
| 1918 | case ALGORITHM_LEFT_SYMMETRIC: |
| 1919 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 1920 | if (sh->pd_idx == raid_disks-1) |
| 1921 | i--; /* Q D D D P */ |
| 1922 | else { |
| 1923 | /* D D P Q D */ |
| 1924 | if (i < sh->pd_idx) |
| 1925 | i += raid_disks; |
| 1926 | i -= (sh->pd_idx + 2); |
| 1927 | } |
| 1928 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1929 | case ALGORITHM_PARITY_0: |
| 1930 | i -= 2; |
| 1931 | break; |
| 1932 | case ALGORITHM_PARITY_N: |
| 1933 | break; |
| 1934 | case ALGORITHM_ROTATING_N_CONTINUE: |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 1935 | /* Like left_symmetric, but P is before Q */ |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1936 | if (sh->pd_idx == 0) |
| 1937 | i--; /* P D D D Q */ |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 1938 | else { |
| 1939 | /* D D Q P D */ |
| 1940 | if (i < sh->pd_idx) |
| 1941 | i += raid_disks; |
| 1942 | i -= (sh->pd_idx + 1); |
| 1943 | } |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1944 | break; |
| 1945 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 1946 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 1947 | if (i > sh->pd_idx) |
| 1948 | i--; |
| 1949 | break; |
| 1950 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 1951 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 1952 | if (i < sh->pd_idx) |
| 1953 | i += data_disks + 1; |
| 1954 | i -= (sh->pd_idx + 1); |
| 1955 | break; |
| 1956 | case ALGORITHM_PARITY_0_6: |
| 1957 | i -= 1; |
| 1958 | break; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1959 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1960 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1961 | } |
| 1962 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | } |
| 1964 | |
| 1965 | chunk_number = stripe * data_disks + i; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 1966 | r_sector = chunk_number * sectors_per_chunk + chunk_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1967 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1968 | check = raid5_compute_sector(conf, r_sector, |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1969 | previous, &dummy1, &sh2); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1970 | if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx |
| 1971 | || sh2.qd_idx != sh->qd_idx) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 1972 | printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n", |
| 1973 | mdname(conf->mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1974 | return 0; |
| 1975 | } |
| 1976 | return r_sector; |
| 1977 | } |
| 1978 | |
| 1979 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1980 | static void |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 1981 | schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1982 | int rcw, int expand) |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1983 | { |
| 1984 | int i, pd_idx = sh->pd_idx, disks = sh->disks; |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 1985 | raid5_conf_t *conf = sh->raid_conf; |
| 1986 | int level = conf->level; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1987 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1988 | if (rcw) { |
| 1989 | /* if we are not expanding this is a proper write request, and |
| 1990 | * there will be bios with new data to be drained into the |
| 1991 | * stripe cache |
| 1992 | */ |
| 1993 | if (!expand) { |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1994 | sh->reconstruct_state = reconstruct_state_drain_run; |
| 1995 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
| 1996 | } else |
| 1997 | sh->reconstruct_state = reconstruct_state_run; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1998 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1999 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2000 | |
| 2001 | for (i = disks; i--; ) { |
| 2002 | struct r5dev *dev = &sh->dev[i]; |
| 2003 | |
| 2004 | if (dev->towrite) { |
| 2005 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2006 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2007 | if (!expand) |
| 2008 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2009 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2010 | } |
| 2011 | } |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2012 | if (s->locked + conf->max_degraded == disks) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2013 | if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state)) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2014 | atomic_inc(&conf->pending_full_writes); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2015 | } else { |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2016 | BUG_ON(level == 6); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2017 | BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) || |
| 2018 | test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags))); |
| 2019 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2020 | sh->reconstruct_state = reconstruct_state_prexor_drain_run; |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2021 | set_bit(STRIPE_OP_PREXOR, &s->ops_request); |
| 2022 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2023 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2024 | |
| 2025 | for (i = disks; i--; ) { |
| 2026 | struct r5dev *dev = &sh->dev[i]; |
| 2027 | if (i == pd_idx) |
| 2028 | continue; |
| 2029 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2030 | if (dev->towrite && |
| 2031 | (test_bit(R5_UPTODATE, &dev->flags) || |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2032 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2033 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2034 | set_bit(R5_LOCKED, &dev->flags); |
| 2035 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2036 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2037 | } |
| 2038 | } |
| 2039 | } |
| 2040 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2041 | /* keep the parity disk(s) locked while asynchronous operations |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2042 | * are in flight |
| 2043 | */ |
| 2044 | set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); |
| 2045 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2046 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2047 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2048 | if (level == 6) { |
| 2049 | int qd_idx = sh->qd_idx; |
| 2050 | struct r5dev *dev = &sh->dev[qd_idx]; |
| 2051 | |
| 2052 | set_bit(R5_LOCKED, &dev->flags); |
| 2053 | clear_bit(R5_UPTODATE, &dev->flags); |
| 2054 | s->locked++; |
| 2055 | } |
| 2056 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2057 | pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n", |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 2058 | __func__, (unsigned long long)sh->sector, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2059 | s->locked, s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2060 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2061 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | /* |
| 2063 | * Each stripe/dev can have one or more bion attached. |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2064 | * toread/towrite point to the first in a chain. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | * The bi_next chain must be in order. |
| 2066 | */ |
| 2067 | static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite) |
| 2068 | { |
| 2069 | struct bio **bip; |
| 2070 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2071 | int firstwrite=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2073 | pr_debug("adding bh b#%llu to stripe s#%llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | (unsigned long long)bi->bi_sector, |
| 2075 | (unsigned long long)sh->sector); |
| 2076 | |
| 2077 | |
| 2078 | spin_lock(&sh->lock); |
| 2079 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2080 | if (forwrite) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2081 | bip = &sh->dev[dd_idx].towrite; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2082 | if (*bip == NULL && sh->dev[dd_idx].written == NULL) |
| 2083 | firstwrite = 1; |
| 2084 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | bip = &sh->dev[dd_idx].toread; |
| 2086 | while (*bip && (*bip)->bi_sector < bi->bi_sector) { |
| 2087 | if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector) |
| 2088 | goto overlap; |
| 2089 | bip = & (*bip)->bi_next; |
| 2090 | } |
| 2091 | if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9)) |
| 2092 | goto overlap; |
| 2093 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 2094 | BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2095 | if (*bip) |
| 2096 | bi->bi_next = *bip; |
| 2097 | *bip = bi; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2098 | bi->bi_phys_segments++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | spin_unlock_irq(&conf->device_lock); |
| 2100 | spin_unlock(&sh->lock); |
| 2101 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2102 | pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2103 | (unsigned long long)bi->bi_sector, |
| 2104 | (unsigned long long)sh->sector, dd_idx); |
| 2105 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2106 | if (conf->mddev->bitmap && firstwrite) { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2107 | bitmap_startwrite(conf->mddev->bitmap, sh->sector, |
| 2108 | STRIPE_SECTORS, 0); |
NeilBrown | ae3c20c | 2006-07-10 04:44:17 -0700 | [diff] [blame] | 2109 | sh->bm_seq = conf->seq_flush+1; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2110 | set_bit(STRIPE_BIT_DELAY, &sh->state); |
| 2111 | } |
| 2112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | if (forwrite) { |
| 2114 | /* check if page is covered */ |
| 2115 | sector_t sector = sh->dev[dd_idx].sector; |
| 2116 | for (bi=sh->dev[dd_idx].towrite; |
| 2117 | sector < sh->dev[dd_idx].sector + STRIPE_SECTORS && |
| 2118 | bi && bi->bi_sector <= sector; |
| 2119 | bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) { |
| 2120 | if (bi->bi_sector + (bi->bi_size>>9) >= sector) |
| 2121 | sector = bi->bi_sector + (bi->bi_size>>9); |
| 2122 | } |
| 2123 | if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS) |
| 2124 | set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags); |
| 2125 | } |
| 2126 | return 1; |
| 2127 | |
| 2128 | overlap: |
| 2129 | set_bit(R5_Overlap, &sh->dev[dd_idx].flags); |
| 2130 | spin_unlock_irq(&conf->device_lock); |
| 2131 | spin_unlock(&sh->lock); |
| 2132 | return 0; |
| 2133 | } |
| 2134 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 2135 | static void end_reshape(raid5_conf_t *conf); |
| 2136 | |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2137 | static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous, |
| 2138 | struct stripe_head *sh) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2139 | { |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2140 | int sectors_per_chunk = |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 2141 | previous ? conf->prev_chunk_sectors : conf->chunk_sectors; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2142 | int dd_idx; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 2143 | int chunk_offset = sector_div(stripe, sectors_per_chunk); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2144 | int disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 2145 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2146 | raid5_compute_sector(conf, |
| 2147 | stripe * (disks - conf->max_degraded) |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 2148 | *sectors_per_chunk + chunk_offset, |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2149 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2150 | &dd_idx, sh); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2151 | } |
| 2152 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2153 | static void |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2154 | handle_failed_stripe(raid5_conf_t *conf, struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2155 | struct stripe_head_state *s, int disks, |
| 2156 | struct bio **return_bi) |
| 2157 | { |
| 2158 | int i; |
| 2159 | for (i = disks; i--; ) { |
| 2160 | struct bio *bi; |
| 2161 | int bitmap_end = 0; |
| 2162 | |
| 2163 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
| 2164 | mdk_rdev_t *rdev; |
| 2165 | rcu_read_lock(); |
| 2166 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 2167 | if (rdev && test_bit(In_sync, &rdev->flags)) |
| 2168 | /* multiple read failures in one stripe */ |
| 2169 | md_error(conf->mddev, rdev); |
| 2170 | rcu_read_unlock(); |
| 2171 | } |
| 2172 | spin_lock_irq(&conf->device_lock); |
| 2173 | /* fail all writes first */ |
| 2174 | bi = sh->dev[i].towrite; |
| 2175 | sh->dev[i].towrite = NULL; |
| 2176 | if (bi) { |
| 2177 | s->to_write--; |
| 2178 | bitmap_end = 1; |
| 2179 | } |
| 2180 | |
| 2181 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 2182 | wake_up(&conf->wait_for_overlap); |
| 2183 | |
| 2184 | while (bi && bi->bi_sector < |
| 2185 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2186 | struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); |
| 2187 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2188 | if (!raid5_dec_bi_phys_segments(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2189 | md_write_end(conf->mddev); |
| 2190 | bi->bi_next = *return_bi; |
| 2191 | *return_bi = bi; |
| 2192 | } |
| 2193 | bi = nextbi; |
| 2194 | } |
| 2195 | /* and fail all 'written' */ |
| 2196 | bi = sh->dev[i].written; |
| 2197 | sh->dev[i].written = NULL; |
| 2198 | if (bi) bitmap_end = 1; |
| 2199 | while (bi && bi->bi_sector < |
| 2200 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2201 | struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector); |
| 2202 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2203 | if (!raid5_dec_bi_phys_segments(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2204 | md_write_end(conf->mddev); |
| 2205 | bi->bi_next = *return_bi; |
| 2206 | *return_bi = bi; |
| 2207 | } |
| 2208 | bi = bi2; |
| 2209 | } |
| 2210 | |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2211 | /* fail any reads if this device is non-operational and |
| 2212 | * the data has not reached the cache yet. |
| 2213 | */ |
| 2214 | if (!test_bit(R5_Wantfill, &sh->dev[i].flags) && |
| 2215 | (!test_bit(R5_Insync, &sh->dev[i].flags) || |
| 2216 | test_bit(R5_ReadError, &sh->dev[i].flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2217 | bi = sh->dev[i].toread; |
| 2218 | sh->dev[i].toread = NULL; |
| 2219 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 2220 | wake_up(&conf->wait_for_overlap); |
| 2221 | if (bi) s->to_read--; |
| 2222 | while (bi && bi->bi_sector < |
| 2223 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2224 | struct bio *nextbi = |
| 2225 | r5_next_bio(bi, sh->dev[i].sector); |
| 2226 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2227 | if (!raid5_dec_bi_phys_segments(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2228 | bi->bi_next = *return_bi; |
| 2229 | *return_bi = bi; |
| 2230 | } |
| 2231 | bi = nextbi; |
| 2232 | } |
| 2233 | } |
| 2234 | spin_unlock_irq(&conf->device_lock); |
| 2235 | if (bitmap_end) |
| 2236 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 2237 | STRIPE_SECTORS, 0, 0); |
| 2238 | } |
| 2239 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2240 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 2241 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 2242 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2243 | } |
| 2244 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2245 | /* fetch_block5 - checks the given member device to see if its data needs |
| 2246 | * to be read or computed to satisfy a request. |
| 2247 | * |
| 2248 | * Returns 1 when no more member devices need to be checked, otherwise returns |
| 2249 | * 0 to tell the loop in handle_stripe_fill5 to continue |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2250 | */ |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2251 | static int fetch_block5(struct stripe_head *sh, struct stripe_head_state *s, |
| 2252 | int disk_idx, int disks) |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2253 | { |
| 2254 | struct r5dev *dev = &sh->dev[disk_idx]; |
| 2255 | struct r5dev *failed_dev = &sh->dev[s->failed_num]; |
| 2256 | |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2257 | /* is the data in this block needed, and can we get it? */ |
| 2258 | if (!test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2259 | !test_bit(R5_UPTODATE, &dev->flags) && |
| 2260 | (dev->toread || |
| 2261 | (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) || |
| 2262 | s->syncing || s->expanding || |
| 2263 | (s->failed && |
| 2264 | (failed_dev->toread || |
| 2265 | (failed_dev->towrite && |
| 2266 | !test_bit(R5_OVERWRITE, &failed_dev->flags)))))) { |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2267 | /* We would like to get this block, possibly by computing it, |
| 2268 | * otherwise read it if the backing disk is insync |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2269 | */ |
| 2270 | if ((s->uptodate == disks - 1) && |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2271 | (s->failed && disk_idx == s->failed_num)) { |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2272 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2273 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2274 | set_bit(R5_Wantcompute, &dev->flags); |
| 2275 | sh->ops.target = disk_idx; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2276 | sh->ops.target2 = -1; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2277 | s->req_compute = 1; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2278 | /* Careful: from this point on 'uptodate' is in the eye |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2279 | * of raid_run_ops which services 'compute' operations |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2280 | * before writes. R5_Wantcompute flags a block that will |
| 2281 | * be R5_UPTODATE by the time it is needed for a |
| 2282 | * subsequent operation. |
| 2283 | */ |
| 2284 | s->uptodate++; |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2285 | return 1; /* uptodate + compute == disks */ |
Dan Williams | 7a1fc53 | 2008-07-10 04:54:57 -0700 | [diff] [blame] | 2286 | } else if (test_bit(R5_Insync, &dev->flags)) { |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2287 | set_bit(R5_LOCKED, &dev->flags); |
| 2288 | set_bit(R5_Wantread, &dev->flags); |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2289 | s->locked++; |
| 2290 | pr_debug("Reading block %d (sync=%d)\n", disk_idx, |
| 2291 | s->syncing); |
| 2292 | } |
| 2293 | } |
| 2294 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2295 | return 0; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2296 | } |
| 2297 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2298 | /** |
| 2299 | * handle_stripe_fill5 - read or compute data to satisfy pending requests. |
| 2300 | */ |
| 2301 | static void handle_stripe_fill5(struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2302 | struct stripe_head_state *s, int disks) |
| 2303 | { |
| 2304 | int i; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2305 | |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2306 | /* look for blocks to read/compute, skip this if a compute |
| 2307 | * is already in flight, or if the stripe contents are in the |
| 2308 | * midst of changing due to a write |
| 2309 | */ |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2310 | if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state && |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2311 | !sh->reconstruct_state) |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2312 | for (i = disks; i--; ) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2313 | if (fetch_block5(sh, s, i, disks)) |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2314 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2315 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2316 | } |
| 2317 | |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2318 | /* fetch_block6 - checks the given member device to see if its data needs |
| 2319 | * to be read or computed to satisfy a request. |
| 2320 | * |
| 2321 | * Returns 1 when no more member devices need to be checked, otherwise returns |
| 2322 | * 0 to tell the loop in handle_stripe_fill6 to continue |
| 2323 | */ |
| 2324 | static int fetch_block6(struct stripe_head *sh, struct stripe_head_state *s, |
| 2325 | struct r6_state *r6s, int disk_idx, int disks) |
| 2326 | { |
| 2327 | struct r5dev *dev = &sh->dev[disk_idx]; |
| 2328 | struct r5dev *fdev[2] = { &sh->dev[r6s->failed_num[0]], |
| 2329 | &sh->dev[r6s->failed_num[1]] }; |
| 2330 | |
| 2331 | if (!test_bit(R5_LOCKED, &dev->flags) && |
| 2332 | !test_bit(R5_UPTODATE, &dev->flags) && |
| 2333 | (dev->toread || |
| 2334 | (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) || |
| 2335 | s->syncing || s->expanding || |
| 2336 | (s->failed >= 1 && |
| 2337 | (fdev[0]->toread || s->to_write)) || |
| 2338 | (s->failed >= 2 && |
| 2339 | (fdev[1]->toread || s->to_write)))) { |
| 2340 | /* we would like to get this block, possibly by computing it, |
| 2341 | * otherwise read it if the backing disk is insync |
| 2342 | */ |
| 2343 | BUG_ON(test_bit(R5_Wantcompute, &dev->flags)); |
| 2344 | BUG_ON(test_bit(R5_Wantread, &dev->flags)); |
| 2345 | if ((s->uptodate == disks - 1) && |
| 2346 | (s->failed && (disk_idx == r6s->failed_num[0] || |
| 2347 | disk_idx == r6s->failed_num[1]))) { |
| 2348 | /* have disk failed, and we're requested to fetch it; |
| 2349 | * do compute it |
| 2350 | */ |
| 2351 | pr_debug("Computing stripe %llu block %d\n", |
| 2352 | (unsigned long long)sh->sector, disk_idx); |
| 2353 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2354 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2355 | set_bit(R5_Wantcompute, &dev->flags); |
| 2356 | sh->ops.target = disk_idx; |
| 2357 | sh->ops.target2 = -1; /* no 2nd target */ |
| 2358 | s->req_compute = 1; |
| 2359 | s->uptodate++; |
| 2360 | return 1; |
| 2361 | } else if (s->uptodate == disks-2 && s->failed >= 2) { |
| 2362 | /* Computing 2-failure is *very* expensive; only |
| 2363 | * do it if failed >= 2 |
| 2364 | */ |
| 2365 | int other; |
| 2366 | for (other = disks; other--; ) { |
| 2367 | if (other == disk_idx) |
| 2368 | continue; |
| 2369 | if (!test_bit(R5_UPTODATE, |
| 2370 | &sh->dev[other].flags)) |
| 2371 | break; |
| 2372 | } |
| 2373 | BUG_ON(other < 0); |
| 2374 | pr_debug("Computing stripe %llu blocks %d,%d\n", |
| 2375 | (unsigned long long)sh->sector, |
| 2376 | disk_idx, other); |
| 2377 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2378 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2379 | set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags); |
| 2380 | set_bit(R5_Wantcompute, &sh->dev[other].flags); |
| 2381 | sh->ops.target = disk_idx; |
| 2382 | sh->ops.target2 = other; |
| 2383 | s->uptodate += 2; |
| 2384 | s->req_compute = 1; |
| 2385 | return 1; |
| 2386 | } else if (test_bit(R5_Insync, &dev->flags)) { |
| 2387 | set_bit(R5_LOCKED, &dev->flags); |
| 2388 | set_bit(R5_Wantread, &dev->flags); |
| 2389 | s->locked++; |
| 2390 | pr_debug("Reading block %d (sync=%d)\n", |
| 2391 | disk_idx, s->syncing); |
| 2392 | } |
| 2393 | } |
| 2394 | |
| 2395 | return 0; |
| 2396 | } |
| 2397 | |
| 2398 | /** |
| 2399 | * handle_stripe_fill6 - read or compute data to satisfy pending requests. |
| 2400 | */ |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2401 | static void handle_stripe_fill6(struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2402 | struct stripe_head_state *s, struct r6_state *r6s, |
| 2403 | int disks) |
| 2404 | { |
| 2405 | int i; |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2406 | |
| 2407 | /* look for blocks to read/compute, skip this if a compute |
| 2408 | * is already in flight, or if the stripe contents are in the |
| 2409 | * midst of changing due to a write |
| 2410 | */ |
| 2411 | if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state && |
| 2412 | !sh->reconstruct_state) |
| 2413 | for (i = disks; i--; ) |
| 2414 | if (fetch_block6(sh, s, r6s, i, disks)) |
| 2415 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2416 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2417 | } |
| 2418 | |
| 2419 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2420 | /* handle_stripe_clean_event |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2421 | * any written block on an uptodate or failed drive can be returned. |
| 2422 | * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but |
| 2423 | * never LOCKED, so we don't need to test 'failed' directly. |
| 2424 | */ |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2425 | static void handle_stripe_clean_event(raid5_conf_t *conf, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2426 | struct stripe_head *sh, int disks, struct bio **return_bi) |
| 2427 | { |
| 2428 | int i; |
| 2429 | struct r5dev *dev; |
| 2430 | |
| 2431 | for (i = disks; i--; ) |
| 2432 | if (sh->dev[i].written) { |
| 2433 | dev = &sh->dev[i]; |
| 2434 | if (!test_bit(R5_LOCKED, &dev->flags) && |
| 2435 | test_bit(R5_UPTODATE, &dev->flags)) { |
| 2436 | /* We can return any write requests */ |
| 2437 | struct bio *wbi, *wbi2; |
| 2438 | int bitmap_end = 0; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2439 | pr_debug("Return write for disc %d\n", i); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2440 | spin_lock_irq(&conf->device_lock); |
| 2441 | wbi = dev->written; |
| 2442 | dev->written = NULL; |
| 2443 | while (wbi && wbi->bi_sector < |
| 2444 | dev->sector + STRIPE_SECTORS) { |
| 2445 | wbi2 = r5_next_bio(wbi, dev->sector); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2446 | if (!raid5_dec_bi_phys_segments(wbi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2447 | md_write_end(conf->mddev); |
| 2448 | wbi->bi_next = *return_bi; |
| 2449 | *return_bi = wbi; |
| 2450 | } |
| 2451 | wbi = wbi2; |
| 2452 | } |
| 2453 | if (dev->towrite == NULL) |
| 2454 | bitmap_end = 1; |
| 2455 | spin_unlock_irq(&conf->device_lock); |
| 2456 | if (bitmap_end) |
| 2457 | bitmap_endwrite(conf->mddev->bitmap, |
| 2458 | sh->sector, |
| 2459 | STRIPE_SECTORS, |
| 2460 | !test_bit(STRIPE_DEGRADED, &sh->state), |
| 2461 | 0); |
| 2462 | } |
| 2463 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2464 | |
| 2465 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 2466 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 2467 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2468 | } |
| 2469 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2470 | static void handle_stripe_dirtying5(raid5_conf_t *conf, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2471 | struct stripe_head *sh, struct stripe_head_state *s, int disks) |
| 2472 | { |
| 2473 | int rmw = 0, rcw = 0, i; |
| 2474 | for (i = disks; i--; ) { |
| 2475 | /* would I have to read this buffer for read_modify_write */ |
| 2476 | struct r5dev *dev = &sh->dev[i]; |
| 2477 | if ((dev->towrite || i == sh->pd_idx) && |
| 2478 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2479 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2480 | test_bit(R5_Wantcompute, &dev->flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2481 | if (test_bit(R5_Insync, &dev->flags)) |
| 2482 | rmw++; |
| 2483 | else |
| 2484 | rmw += 2*disks; /* cannot read it */ |
| 2485 | } |
| 2486 | /* Would I have to read this buffer for reconstruct_write */ |
| 2487 | if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx && |
| 2488 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2489 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2490 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2491 | if (test_bit(R5_Insync, &dev->flags)) rcw++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2492 | else |
| 2493 | rcw += 2*disks; |
| 2494 | } |
| 2495 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2496 | pr_debug("for sector %llu, rmw=%d rcw=%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2497 | (unsigned long long)sh->sector, rmw, rcw); |
| 2498 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2499 | if (rmw < rcw && rmw > 0) |
| 2500 | /* prefer read-modify-write, but need to get some data */ |
| 2501 | for (i = disks; i--; ) { |
| 2502 | struct r5dev *dev = &sh->dev[i]; |
| 2503 | if ((dev->towrite || i == sh->pd_idx) && |
| 2504 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2505 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2506 | test_bit(R5_Wantcompute, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2507 | test_bit(R5_Insync, &dev->flags)) { |
| 2508 | if ( |
| 2509 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2510 | pr_debug("Read_old block " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2511 | "%d for r-m-w\n", i); |
| 2512 | set_bit(R5_LOCKED, &dev->flags); |
| 2513 | set_bit(R5_Wantread, &dev->flags); |
| 2514 | s->locked++; |
| 2515 | } else { |
| 2516 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2517 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2518 | } |
| 2519 | } |
| 2520 | } |
| 2521 | if (rcw <= rmw && rcw > 0) |
| 2522 | /* want reconstruct write, but need to get some data */ |
| 2523 | for (i = disks; i--; ) { |
| 2524 | struct r5dev *dev = &sh->dev[i]; |
| 2525 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
| 2526 | i != sh->pd_idx && |
| 2527 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2528 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2529 | test_bit(R5_Wantcompute, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2530 | test_bit(R5_Insync, &dev->flags)) { |
| 2531 | if ( |
| 2532 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2533 | pr_debug("Read_old block " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2534 | "%d for Reconstruct\n", i); |
| 2535 | set_bit(R5_LOCKED, &dev->flags); |
| 2536 | set_bit(R5_Wantread, &dev->flags); |
| 2537 | s->locked++; |
| 2538 | } else { |
| 2539 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2540 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2541 | } |
| 2542 | } |
| 2543 | } |
| 2544 | /* now if nothing is locked, and if we have enough data, |
| 2545 | * we can start a write request |
| 2546 | */ |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2547 | /* since handle_stripe can be called at any time we need to handle the |
| 2548 | * case where a compute block operation has been submitted and then a |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2549 | * subsequent call wants to start a write request. raid_run_ops only |
| 2550 | * handles the case where compute block and reconstruct are requested |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2551 | * simultaneously. If this is not the case then new writes need to be |
| 2552 | * held off until the compute completes. |
| 2553 | */ |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2554 | if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) && |
| 2555 | (s->locked == 0 && (rcw == 0 || rmw == 0) && |
| 2556 | !test_bit(STRIPE_BIT_DELAY, &sh->state))) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2557 | schedule_reconstruction(sh, s, rcw == 0, 0); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2558 | } |
| 2559 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2560 | static void handle_stripe_dirtying6(raid5_conf_t *conf, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2561 | struct stripe_head *sh, struct stripe_head_state *s, |
| 2562 | struct r6_state *r6s, int disks) |
| 2563 | { |
Yuri Tikhonov | a9b39a7 | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2564 | int rcw = 0, pd_idx = sh->pd_idx, i; |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 2565 | int qd_idx = sh->qd_idx; |
Yuri Tikhonov | a9b39a7 | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2566 | |
| 2567 | set_bit(STRIPE_HANDLE, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2568 | for (i = disks; i--; ) { |
| 2569 | struct r5dev *dev = &sh->dev[i]; |
Yuri Tikhonov | a9b39a7 | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2570 | /* check if we haven't enough data */ |
| 2571 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
| 2572 | i != pd_idx && i != qd_idx && |
| 2573 | !test_bit(R5_LOCKED, &dev->flags) && |
| 2574 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2575 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2576 | rcw++; |
| 2577 | if (!test_bit(R5_Insync, &dev->flags)) |
| 2578 | continue; /* it's a failed drive */ |
| 2579 | |
| 2580 | if ( |
| 2581 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
| 2582 | pr_debug("Read_old stripe %llu " |
| 2583 | "block %d for Reconstruct\n", |
| 2584 | (unsigned long long)sh->sector, i); |
| 2585 | set_bit(R5_LOCKED, &dev->flags); |
| 2586 | set_bit(R5_Wantread, &dev->flags); |
| 2587 | s->locked++; |
| 2588 | } else { |
| 2589 | pr_debug("Request delayed stripe %llu " |
| 2590 | "block %d for Reconstruct\n", |
| 2591 | (unsigned long long)sh->sector, i); |
| 2592 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2593 | set_bit(STRIPE_HANDLE, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2594 | } |
| 2595 | } |
| 2596 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2597 | /* now if nothing is locked, and if we have enough data, we can start a |
| 2598 | * write request |
| 2599 | */ |
Yuri Tikhonov | a9b39a7 | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2600 | if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) && |
| 2601 | s->locked == 0 && rcw == 0 && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2602 | !test_bit(STRIPE_BIT_DELAY, &sh->state)) { |
Yuri Tikhonov | a9b39a7 | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2603 | schedule_reconstruction(sh, s, 1, 0); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2604 | } |
| 2605 | } |
| 2606 | |
| 2607 | static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh, |
| 2608 | struct stripe_head_state *s, int disks) |
| 2609 | { |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2610 | struct r5dev *dev = NULL; |
Dan Williams | e89f896 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2611 | |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2612 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2613 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2614 | switch (sh->check_state) { |
| 2615 | case check_state_idle: |
| 2616 | /* start a new check operation if there are no failures */ |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2617 | if (s->failed == 0) { |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2618 | BUG_ON(s->uptodate != disks); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2619 | sh->check_state = check_state_run; |
| 2620 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2621 | clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2622 | s->uptodate--; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2623 | break; |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2624 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2625 | dev = &sh->dev[s->failed_num]; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2626 | /* fall through */ |
| 2627 | case check_state_compute_result: |
| 2628 | sh->check_state = check_state_idle; |
| 2629 | if (!dev) |
| 2630 | dev = &sh->dev[sh->pd_idx]; |
| 2631 | |
| 2632 | /* check that a write has not made the stripe insync */ |
| 2633 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 2634 | break; |
| 2635 | |
| 2636 | /* either failed parity check, or recovery is happening */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2637 | BUG_ON(!test_bit(R5_UPTODATE, &dev->flags)); |
| 2638 | BUG_ON(s->uptodate != disks); |
| 2639 | |
| 2640 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2641 | s->locked++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2642 | set_bit(R5_Wantwrite, &dev->flags); |
Dan Williams | 830ea01 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2643 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2644 | clear_bit(STRIPE_DEGRADED, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2645 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2646 | break; |
| 2647 | case check_state_run: |
| 2648 | break; /* we will be called again upon completion */ |
| 2649 | case check_state_check_result: |
| 2650 | sh->check_state = check_state_idle; |
| 2651 | |
| 2652 | /* if a failure occurred during the check operation, leave |
| 2653 | * STRIPE_INSYNC not set and let the stripe be handled again |
| 2654 | */ |
| 2655 | if (s->failed) |
| 2656 | break; |
| 2657 | |
| 2658 | /* handle a successful check operation, if parity is correct |
| 2659 | * we are done. Otherwise update the mismatch count and repair |
| 2660 | * parity if !MD_RECOVERY_CHECK |
| 2661 | */ |
Dan Williams | ad283ea | 2009-08-29 19:09:26 -0700 | [diff] [blame] | 2662 | if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0) |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2663 | /* parity is correct (on disc, |
| 2664 | * not in buffer any more) |
| 2665 | */ |
| 2666 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2667 | else { |
| 2668 | conf->mddev->resync_mismatches += STRIPE_SECTORS; |
| 2669 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 2670 | /* don't try to repair!! */ |
| 2671 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2672 | else { |
| 2673 | sh->check_state = check_state_compute_run; |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2674 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2675 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2676 | set_bit(R5_Wantcompute, |
| 2677 | &sh->dev[sh->pd_idx].flags); |
| 2678 | sh->ops.target = sh->pd_idx; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2679 | sh->ops.target2 = -1; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2680 | s->uptodate++; |
| 2681 | } |
| 2682 | } |
| 2683 | break; |
| 2684 | case check_state_compute_run: |
| 2685 | break; |
| 2686 | default: |
| 2687 | printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n", |
| 2688 | __func__, sh->check_state, |
| 2689 | (unsigned long long) sh->sector); |
| 2690 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2691 | } |
| 2692 | } |
| 2693 | |
| 2694 | |
| 2695 | 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] | 2696 | struct stripe_head_state *s, |
| 2697 | struct r6_state *r6s, int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2698 | { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2699 | int pd_idx = sh->pd_idx; |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 2700 | int qd_idx = sh->qd_idx; |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2701 | struct r5dev *dev; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2702 | |
| 2703 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2704 | |
| 2705 | BUG_ON(s->failed > 2); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2706 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2707 | /* Want to check and possibly repair P and Q. |
| 2708 | * However there could be one 'failed' device, in which |
| 2709 | * case we can only check one of them, possibly using the |
| 2710 | * other to generate missing data |
| 2711 | */ |
| 2712 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2713 | switch (sh->check_state) { |
| 2714 | case check_state_idle: |
| 2715 | /* start a new check operation if there are < 2 failures */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2716 | if (s->failed == r6s->q_failed) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2717 | /* The only possible failed device holds Q, so it |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2718 | * makes sense to check P (If anything else were failed, |
| 2719 | * we would have used P to recreate it). |
| 2720 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2721 | sh->check_state = check_state_run; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2722 | } |
| 2723 | if (!r6s->q_failed && s->failed < 2) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2724 | /* Q is not failed, and we didn't use it to generate |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2725 | * anything, so it makes sense to check it |
| 2726 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2727 | if (sh->check_state == check_state_run) |
| 2728 | sh->check_state = check_state_run_pq; |
| 2729 | else |
| 2730 | sh->check_state = check_state_run_q; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2731 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2732 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2733 | /* discard potentially stale zero_sum_result */ |
| 2734 | sh->ops.zero_sum_result = 0; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2735 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2736 | if (sh->check_state == check_state_run) { |
| 2737 | /* async_xor_zero_sum destroys the contents of P */ |
| 2738 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
| 2739 | s->uptodate--; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2740 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2741 | if (sh->check_state >= check_state_run && |
| 2742 | sh->check_state <= check_state_run_pq) { |
| 2743 | /* async_syndrome_zero_sum preserves P and Q, so |
| 2744 | * no need to mark them !uptodate here |
| 2745 | */ |
| 2746 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
| 2747 | break; |
| 2748 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2749 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2750 | /* we have 2-disk failure */ |
| 2751 | BUG_ON(s->failed != 2); |
| 2752 | /* fall through */ |
| 2753 | case check_state_compute_result: |
| 2754 | sh->check_state = check_state_idle; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2755 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2756 | /* check that a write has not made the stripe insync */ |
| 2757 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 2758 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2759 | |
| 2760 | /* now write out any block on a failed drive, |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2761 | * or P or Q if they were recomputed |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2762 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2763 | 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] | 2764 | if (s->failed == 2) { |
| 2765 | dev = &sh->dev[r6s->failed_num[1]]; |
| 2766 | s->locked++; |
| 2767 | set_bit(R5_LOCKED, &dev->flags); |
| 2768 | set_bit(R5_Wantwrite, &dev->flags); |
| 2769 | } |
| 2770 | if (s->failed >= 1) { |
| 2771 | dev = &sh->dev[r6s->failed_num[0]]; |
| 2772 | s->locked++; |
| 2773 | set_bit(R5_LOCKED, &dev->flags); |
| 2774 | set_bit(R5_Wantwrite, &dev->flags); |
| 2775 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2776 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2777 | dev = &sh->dev[pd_idx]; |
| 2778 | s->locked++; |
| 2779 | set_bit(R5_LOCKED, &dev->flags); |
| 2780 | set_bit(R5_Wantwrite, &dev->flags); |
| 2781 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2782 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2783 | dev = &sh->dev[qd_idx]; |
| 2784 | s->locked++; |
| 2785 | set_bit(R5_LOCKED, &dev->flags); |
| 2786 | set_bit(R5_Wantwrite, &dev->flags); |
| 2787 | } |
| 2788 | clear_bit(STRIPE_DEGRADED, &sh->state); |
| 2789 | |
| 2790 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2791 | break; |
| 2792 | case check_state_run: |
| 2793 | case check_state_run_q: |
| 2794 | case check_state_run_pq: |
| 2795 | break; /* we will be called again upon completion */ |
| 2796 | case check_state_check_result: |
| 2797 | sh->check_state = check_state_idle; |
| 2798 | |
| 2799 | /* handle a successful check operation, if parity is correct |
| 2800 | * we are done. Otherwise update the mismatch count and repair |
| 2801 | * parity if !MD_RECOVERY_CHECK |
| 2802 | */ |
| 2803 | if (sh->ops.zero_sum_result == 0) { |
| 2804 | /* both parities are correct */ |
| 2805 | if (!s->failed) |
| 2806 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2807 | else { |
| 2808 | /* in contrast to the raid5 case we can validate |
| 2809 | * parity, but still have a failure to write |
| 2810 | * back |
| 2811 | */ |
| 2812 | sh->check_state = check_state_compute_result; |
| 2813 | /* Returning at this point means that we may go |
| 2814 | * off and bring p and/or q uptodate again so |
| 2815 | * we make sure to check zero_sum_result again |
| 2816 | * to verify if p or q need writeback |
| 2817 | */ |
| 2818 | } |
| 2819 | } else { |
| 2820 | conf->mddev->resync_mismatches += STRIPE_SECTORS; |
| 2821 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 2822 | /* don't try to repair!! */ |
| 2823 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2824 | else { |
| 2825 | int *target = &sh->ops.target; |
| 2826 | |
| 2827 | sh->ops.target = -1; |
| 2828 | sh->ops.target2 = -1; |
| 2829 | sh->check_state = check_state_compute_run; |
| 2830 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2831 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2832 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
| 2833 | set_bit(R5_Wantcompute, |
| 2834 | &sh->dev[pd_idx].flags); |
| 2835 | *target = pd_idx; |
| 2836 | target = &sh->ops.target2; |
| 2837 | s->uptodate++; |
| 2838 | } |
| 2839 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
| 2840 | set_bit(R5_Wantcompute, |
| 2841 | &sh->dev[qd_idx].flags); |
| 2842 | *target = qd_idx; |
| 2843 | s->uptodate++; |
| 2844 | } |
| 2845 | } |
| 2846 | } |
| 2847 | break; |
| 2848 | case check_state_compute_run: |
| 2849 | break; |
| 2850 | default: |
| 2851 | printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n", |
| 2852 | __func__, sh->check_state, |
| 2853 | (unsigned long long) sh->sector); |
| 2854 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2855 | } |
| 2856 | } |
| 2857 | |
| 2858 | static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh, |
| 2859 | struct r6_state *r6s) |
| 2860 | { |
| 2861 | int i; |
| 2862 | |
| 2863 | /* We have read all the blocks in this stripe and now we need to |
| 2864 | * copy some of them into a target stripe for expand. |
| 2865 | */ |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2866 | struct dma_async_tx_descriptor *tx = NULL; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2867 | clear_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 2868 | for (i = 0; i < sh->disks; i++) |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 2869 | if (i != sh->pd_idx && i != sh->qd_idx) { |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2870 | int dd_idx, j; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2871 | struct stripe_head *sh2; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 2872 | struct async_submit_ctl submit; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2873 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2874 | sector_t bn = compute_blocknr(sh, i, 1); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2875 | sector_t s = raid5_compute_sector(conf, bn, 0, |
| 2876 | &dd_idx, NULL); |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 2877 | sh2 = get_active_stripe(conf, s, 0, 1, 1); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2878 | if (sh2 == NULL) |
| 2879 | /* so far only the early blocks of this stripe |
| 2880 | * have been requested. When later blocks |
| 2881 | * get requested, we will try again |
| 2882 | */ |
| 2883 | continue; |
| 2884 | if (!test_bit(STRIPE_EXPANDING, &sh2->state) || |
| 2885 | test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) { |
| 2886 | /* must have already done this block */ |
| 2887 | release_stripe(sh2); |
| 2888 | continue; |
| 2889 | } |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2890 | |
| 2891 | /* place all the copies on one channel */ |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 2892 | init_async_submit(&submit, 0, tx, NULL, NULL, NULL); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2893 | tx = async_memcpy(sh2->dev[dd_idx].page, |
Dan Williams | 88ba2aa | 2009-04-09 16:16:18 -0700 | [diff] [blame] | 2894 | sh->dev[i].page, 0, 0, STRIPE_SIZE, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 2895 | &submit); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2896 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2897 | set_bit(R5_Expanded, &sh2->dev[dd_idx].flags); |
| 2898 | set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags); |
| 2899 | for (j = 0; j < conf->raid_disks; j++) |
| 2900 | if (j != sh2->pd_idx && |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2901 | (!r6s || j != sh2->qd_idx) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2902 | !test_bit(R5_Expanded, &sh2->dev[j].flags)) |
| 2903 | break; |
| 2904 | if (j == conf->raid_disks) { |
| 2905 | set_bit(STRIPE_EXPAND_READY, &sh2->state); |
| 2906 | set_bit(STRIPE_HANDLE, &sh2->state); |
| 2907 | } |
| 2908 | release_stripe(sh2); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2909 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2910 | } |
NeilBrown | a2e0855 | 2007-09-11 15:23:36 -0700 | [diff] [blame] | 2911 | /* done submitting copies, wait for them to complete */ |
| 2912 | if (tx) { |
| 2913 | async_tx_ack(tx); |
| 2914 | dma_wait_for_async_tx(tx); |
| 2915 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2916 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2917 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2918 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2919 | /* |
| 2920 | * handle_stripe - do things to a stripe. |
| 2921 | * |
| 2922 | * We lock the stripe and then examine the state of various bits |
| 2923 | * to see what needs to be done. |
| 2924 | * Possible results: |
| 2925 | * return some read request which now have data |
| 2926 | * return some write requests which are safely on disc |
| 2927 | * schedule a read on some buffers |
| 2928 | * schedule a write of some buffers |
| 2929 | * return confirmation of parity correctness |
| 2930 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2931 | * buffers are taken off read_list or write_list, and bh_cache buffers |
| 2932 | * get BH_Lock set before the stripe lock is released. |
| 2933 | * |
| 2934 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2935 | |
NeilBrown | 1442577 | 2009-10-16 15:55:25 +1100 | [diff] [blame] | 2936 | static void handle_stripe5(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2937 | { |
| 2938 | raid5_conf_t *conf = sh->raid_conf; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2939 | int disks = sh->disks, i; |
| 2940 | struct bio *return_bi = NULL; |
| 2941 | struct stripe_head_state s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2942 | struct r5dev *dev; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2943 | mdk_rdev_t *blocked_rdev = NULL; |
Dan Williams | e0a115e | 2008-06-05 22:45:52 -0700 | [diff] [blame] | 2944 | int prexor; |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 2945 | int dec_preread_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2946 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2947 | memset(&s, 0, sizeof(s)); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2948 | pr_debug("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d check:%d " |
| 2949 | "reconstruct:%d\n", (unsigned long long)sh->sector, sh->state, |
| 2950 | atomic_read(&sh->count), sh->pd_idx, sh->check_state, |
| 2951 | sh->reconstruct_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2952 | |
| 2953 | spin_lock(&sh->lock); |
| 2954 | clear_bit(STRIPE_HANDLE, &sh->state); |
| 2955 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 2956 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2957 | s.syncing = test_bit(STRIPE_SYNCING, &sh->state); |
| 2958 | s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 2959 | s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state); |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 2960 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2961 | /* Now to look around and see what can be done */ |
NeilBrown | 9910f16 | 2006-01-06 00:20:24 -0800 | [diff] [blame] | 2962 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2963 | for (i=disks; i--; ) { |
| 2964 | mdk_rdev_t *rdev; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 2965 | |
| 2966 | dev = &sh->dev[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2967 | clear_bit(R5_Insync, &dev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2968 | |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2969 | pr_debug("check %d: state 0x%lx toread %p read %p write %p " |
| 2970 | "written %p\n", i, dev->flags, dev->toread, dev->read, |
| 2971 | dev->towrite, dev->written); |
| 2972 | |
| 2973 | /* maybe we can request a biofill operation |
| 2974 | * |
| 2975 | * new wantfill requests are only permitted while |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 2976 | * ops_complete_biofill is guaranteed to be inactive |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2977 | */ |
| 2978 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread && |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 2979 | !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2980 | set_bit(R5_Wantfill, &dev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2981 | |
| 2982 | /* now count some things */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2983 | if (test_bit(R5_LOCKED, &dev->flags)) s.locked++; |
| 2984 | if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2985 | if (test_bit(R5_Wantcompute, &dev->flags)) s.compute++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2986 | |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2987 | if (test_bit(R5_Wantfill, &dev->flags)) |
| 2988 | s.to_fill++; |
| 2989 | else if (dev->toread) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2990 | s.to_read++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2991 | if (dev->towrite) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2992 | s.to_write++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2993 | if (!test_bit(R5_OVERWRITE, &dev->flags)) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2994 | s.non_overwrite++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2995 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2996 | if (dev->written) |
| 2997 | s.written++; |
NeilBrown | 9910f16 | 2006-01-06 00:20:24 -0800 | [diff] [blame] | 2998 | rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | ac4090d | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2999 | if (blocked_rdev == NULL && |
| 3000 | rdev && unlikely(test_bit(Blocked, &rdev->flags))) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3001 | blocked_rdev = rdev; |
| 3002 | atomic_inc(&rdev->nr_pending); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3003 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3004 | if (!rdev || !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 3005 | /* The ReadError flag will just be confusing now */ |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 3006 | clear_bit(R5_ReadError, &dev->flags); |
| 3007 | clear_bit(R5_ReWrite, &dev->flags); |
| 3008 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3009 | if (!rdev || !test_bit(In_sync, &rdev->flags) |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 3010 | || test_bit(R5_ReadError, &dev->flags)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3011 | s.failed++; |
| 3012 | s.failed_num = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3013 | } else |
| 3014 | set_bit(R5_Insync, &dev->flags); |
| 3015 | } |
NeilBrown | 9910f16 | 2006-01-06 00:20:24 -0800 | [diff] [blame] | 3016 | rcu_read_unlock(); |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3017 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3018 | if (unlikely(blocked_rdev)) { |
NeilBrown | ac4090d | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3019 | if (s.syncing || s.expanding || s.expanded || |
| 3020 | s.to_write || s.written) { |
| 3021 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3022 | goto unlock; |
| 3023 | } |
| 3024 | /* There is nothing for the blocked_rdev to block */ |
| 3025 | rdev_dec_pending(blocked_rdev, conf->mddev); |
| 3026 | blocked_rdev = NULL; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3027 | } |
| 3028 | |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 3029 | if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) { |
| 3030 | set_bit(STRIPE_OP_BIOFILL, &s.ops_request); |
| 3031 | set_bit(STRIPE_BIOFILL_RUN, &sh->state); |
| 3032 | } |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3033 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3034 | pr_debug("locked=%d uptodate=%d to_read=%d" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3035 | " to_write=%d failed=%d failed_num=%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3036 | s.locked, s.uptodate, s.to_read, s.to_write, |
| 3037 | s.failed, s.failed_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3038 | /* check if the array has lost two devices and, if so, some requests might |
| 3039 | * need to be failed |
| 3040 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3041 | if (s.failed > 1 && s.to_read+s.to_write+s.written) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3042 | handle_failed_stripe(conf, sh, &s, disks, &return_bi); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3043 | if (s.failed > 1 && s.syncing) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3044 | md_done_sync(conf->mddev, STRIPE_SECTORS,0); |
| 3045 | clear_bit(STRIPE_SYNCING, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3046 | s.syncing = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3047 | } |
| 3048 | |
| 3049 | /* might be able to return some write requests if the parity block |
| 3050 | * is safe, or on a failed drive |
| 3051 | */ |
| 3052 | dev = &sh->dev[sh->pd_idx]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3053 | if ( s.written && |
| 3054 | ((test_bit(R5_Insync, &dev->flags) && |
| 3055 | !test_bit(R5_LOCKED, &dev->flags) && |
| 3056 | test_bit(R5_UPTODATE, &dev->flags)) || |
| 3057 | (s.failed == 1 && s.failed_num == sh->pd_idx))) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3058 | handle_stripe_clean_event(conf, sh, disks, &return_bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3059 | |
| 3060 | /* Now we might consider reading some blocks, either to check/generate |
| 3061 | * parity, or to satisfy requests |
| 3062 | * or to load a block that is being partially written. |
| 3063 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3064 | if (s.to_read || s.non_overwrite || |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 3065 | (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3066 | handle_stripe_fill5(sh, &s, disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3067 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3068 | /* Now we check to see if any write operations have recently |
| 3069 | * completed |
| 3070 | */ |
Dan Williams | e0a115e | 2008-06-05 22:45:52 -0700 | [diff] [blame] | 3071 | prexor = 0; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 3072 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_result) |
Dan Williams | e0a115e | 2008-06-05 22:45:52 -0700 | [diff] [blame] | 3073 | prexor = 1; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 3074 | if (sh->reconstruct_state == reconstruct_state_drain_result || |
| 3075 | sh->reconstruct_state == reconstruct_state_prexor_drain_result) { |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3076 | sh->reconstruct_state = reconstruct_state_idle; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3077 | |
| 3078 | /* All the 'written' buffers and the parity block are ready to |
| 3079 | * be written back to disk |
| 3080 | */ |
| 3081 | BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags)); |
| 3082 | for (i = disks; i--; ) { |
| 3083 | dev = &sh->dev[i]; |
| 3084 | if (test_bit(R5_LOCKED, &dev->flags) && |
| 3085 | (i == sh->pd_idx || dev->written)) { |
| 3086 | pr_debug("Writing block %d\n", i); |
| 3087 | set_bit(R5_Wantwrite, &dev->flags); |
Dan Williams | e0a115e | 2008-06-05 22:45:52 -0700 | [diff] [blame] | 3088 | if (prexor) |
| 3089 | continue; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3090 | if (!test_bit(R5_Insync, &dev->flags) || |
| 3091 | (i == sh->pd_idx && s.failed == 0)) |
| 3092 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3093 | } |
| 3094 | } |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 3095 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 3096 | dec_preread_active = 1; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3097 | } |
| 3098 | |
| 3099 | /* Now to consider new write requests and what else, if anything |
| 3100 | * should be read. We do not handle new writes when: |
| 3101 | * 1/ A 'write' operation (copy+xor) is already in flight. |
| 3102 | * 2/ A 'check' operation is in flight, as it may clobber the parity |
| 3103 | * block. |
| 3104 | */ |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3105 | if (s.to_write && !sh->reconstruct_state && !sh->check_state) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3106 | handle_stripe_dirtying5(conf, sh, &s, disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3107 | |
| 3108 | /* maybe we need to check and possibly fix the parity for this stripe |
Dan Williams | e89f896 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3109 | * Any reads will already have been scheduled, so we just see if enough |
| 3110 | * data is available. The parity check is held off while parity |
| 3111 | * dependent operations are in flight. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3112 | */ |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3113 | if (sh->check_state || |
| 3114 | (s.syncing && s.locked == 0 && |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 3115 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3116 | !test_bit(STRIPE_INSYNC, &sh->state))) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3117 | handle_parity_checks5(conf, sh, &s, disks); |
Dan Williams | e89f896 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3118 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3119 | if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3120 | md_done_sync(conf->mddev, STRIPE_SECTORS,1); |
| 3121 | clear_bit(STRIPE_SYNCING, &sh->state); |
| 3122 | } |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 3123 | |
| 3124 | /* If the failed drive is just a ReadError, then we might need to progress |
| 3125 | * the repair/check process |
| 3126 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3127 | if (s.failed == 1 && !conf->mddev->ro && |
| 3128 | test_bit(R5_ReadError, &sh->dev[s.failed_num].flags) |
| 3129 | && !test_bit(R5_LOCKED, &sh->dev[s.failed_num].flags) |
| 3130 | && test_bit(R5_UPTODATE, &sh->dev[s.failed_num].flags) |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 3131 | ) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3132 | dev = &sh->dev[s.failed_num]; |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 3133 | if (!test_bit(R5_ReWrite, &dev->flags)) { |
| 3134 | set_bit(R5_Wantwrite, &dev->flags); |
| 3135 | set_bit(R5_ReWrite, &dev->flags); |
| 3136 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3137 | s.locked++; |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 3138 | } else { |
| 3139 | /* let's read it back */ |
| 3140 | set_bit(R5_Wantread, &dev->flags); |
| 3141 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3142 | s.locked++; |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 3143 | } |
| 3144 | } |
| 3145 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3146 | /* Finish reconstruct operations initiated by the expansion process */ |
| 3147 | if (sh->reconstruct_state == reconstruct_state_result) { |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 3148 | struct stripe_head *sh2 |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 3149 | = get_active_stripe(conf, sh->sector, 1, 1, 1); |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 3150 | if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) { |
| 3151 | /* sh cannot be written until sh2 has been read. |
| 3152 | * so arrange for sh to be delayed a little |
| 3153 | */ |
| 3154 | set_bit(STRIPE_DELAYED, &sh->state); |
| 3155 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3156 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, |
| 3157 | &sh2->state)) |
| 3158 | atomic_inc(&conf->preread_active_stripes); |
| 3159 | release_stripe(sh2); |
| 3160 | goto unlock; |
| 3161 | } |
| 3162 | if (sh2) |
| 3163 | release_stripe(sh2); |
| 3164 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3165 | sh->reconstruct_state = reconstruct_state_idle; |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3166 | clear_bit(STRIPE_EXPANDING, &sh->state); |
Dan Williams | 2339788 | 2008-07-23 20:05:34 -0700 | [diff] [blame] | 3167 | for (i = conf->raid_disks; i--; ) { |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3168 | set_bit(R5_Wantwrite, &sh->dev[i].flags); |
Dan Williams | 2339788 | 2008-07-23 20:05:34 -0700 | [diff] [blame] | 3169 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
Neil Brown | efe3114 | 2008-06-28 08:31:14 +1000 | [diff] [blame] | 3170 | s.locked++; |
Dan Williams | 2339788 | 2008-07-23 20:05:34 -0700 | [diff] [blame] | 3171 | } |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3172 | } |
| 3173 | |
| 3174 | if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) && |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3175 | !sh->reconstruct_state) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3176 | /* Need to write out all blocks after computing parity */ |
| 3177 | sh->disks = conf->raid_disks; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3178 | stripe_set_idx(sh->sector, conf, 0, sh); |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3179 | schedule_reconstruction(sh, &s, 1, 1); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3180 | } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3181 | clear_bit(STRIPE_EXPAND_READY, &sh->state); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3182 | atomic_dec(&conf->reshape_stripes); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3183 | wake_up(&conf->wait_for_overlap); |
| 3184 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 3185 | } |
| 3186 | |
Dan Williams | 0f94e87 | 2008-01-08 15:32:53 -0800 | [diff] [blame] | 3187 | if (s.expanding && s.locked == 0 && |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 3188 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3189 | handle_stripe_expansion(conf, sh, NULL); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3190 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3191 | unlock: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3192 | spin_unlock(&sh->lock); |
| 3193 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3194 | /* wait for this device to become unblocked */ |
| 3195 | if (unlikely(blocked_rdev)) |
| 3196 | md_wait_for_blocked_rdev(blocked_rdev, conf->mddev); |
| 3197 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3198 | if (s.ops_request) |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 3199 | raid_run_ops(sh, s.ops_request); |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3200 | |
Dan Williams | c4e5ac0 | 2008-06-28 08:31:53 +1000 | [diff] [blame] | 3201 | ops_run_io(sh, &s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3202 | |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 3203 | if (dec_preread_active) { |
| 3204 | /* We delay this until after ops_run_io so that if make_request |
| 3205 | * is waiting on a barrier, it won't continue until the writes |
| 3206 | * have actually been submitted. |
| 3207 | */ |
| 3208 | atomic_dec(&conf->preread_active_stripes); |
| 3209 | if (atomic_read(&conf->preread_active_stripes) < |
| 3210 | IO_THRESHOLD) |
| 3211 | md_wakeup_thread(conf->mddev->thread); |
| 3212 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3213 | return_io(return_bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3214 | } |
| 3215 | |
NeilBrown | 1442577 | 2009-10-16 15:55:25 +1100 | [diff] [blame] | 3216 | static void handle_stripe6(struct stripe_head *sh) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3217 | { |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3218 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3219 | int disks = sh->disks; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3220 | struct bio *return_bi = NULL; |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 3221 | int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3222 | struct stripe_head_state s; |
| 3223 | struct r6_state r6s; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3224 | struct r5dev *dev, *pdev, *qdev; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3225 | mdk_rdev_t *blocked_rdev = NULL; |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 3226 | int dec_preread_active = 0; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3227 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3228 | pr_debug("handling stripe %llu, state=%#lx cnt=%d, " |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3229 | "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3230 | (unsigned long long)sh->sector, sh->state, |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3231 | atomic_read(&sh->count), pd_idx, qd_idx, |
| 3232 | sh->check_state, sh->reconstruct_state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3233 | memset(&s, 0, sizeof(s)); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3234 | |
| 3235 | spin_lock(&sh->lock); |
| 3236 | clear_bit(STRIPE_HANDLE, &sh->state); |
| 3237 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 3238 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3239 | s.syncing = test_bit(STRIPE_SYNCING, &sh->state); |
| 3240 | s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 3241 | s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3242 | /* Now to look around and see what can be done */ |
| 3243 | |
| 3244 | rcu_read_lock(); |
| 3245 | for (i=disks; i--; ) { |
| 3246 | mdk_rdev_t *rdev; |
| 3247 | dev = &sh->dev[i]; |
| 3248 | clear_bit(R5_Insync, &dev->flags); |
| 3249 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3250 | 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] | 3251 | i, dev->flags, dev->toread, dev->towrite, dev->written); |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3252 | /* maybe we can reply to a read |
| 3253 | * |
| 3254 | * new wantfill requests are only permitted while |
| 3255 | * ops_complete_biofill is guaranteed to be inactive |
| 3256 | */ |
| 3257 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread && |
| 3258 | !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) |
| 3259 | set_bit(R5_Wantfill, &dev->flags); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3260 | |
| 3261 | /* now count some things */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3262 | if (test_bit(R5_LOCKED, &dev->flags)) s.locked++; |
| 3263 | if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++; |
Dan Williams | 2d6e4ec | 2009-09-16 12:11:54 -0700 | [diff] [blame] | 3264 | if (test_bit(R5_Wantcompute, &dev->flags)) { |
| 3265 | s.compute++; |
| 3266 | BUG_ON(s.compute > 2); |
| 3267 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3268 | |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3269 | if (test_bit(R5_Wantfill, &dev->flags)) { |
| 3270 | s.to_fill++; |
| 3271 | } else if (dev->toread) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3272 | s.to_read++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3273 | if (dev->towrite) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3274 | s.to_write++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3275 | if (!test_bit(R5_OVERWRITE, &dev->flags)) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3276 | s.non_overwrite++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3277 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3278 | if (dev->written) |
| 3279 | s.written++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3280 | rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | ac4090d | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3281 | if (blocked_rdev == NULL && |
| 3282 | rdev && unlikely(test_bit(Blocked, &rdev->flags))) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3283 | blocked_rdev = rdev; |
| 3284 | atomic_inc(&rdev->nr_pending); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3285 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3286 | if (!rdev || !test_bit(In_sync, &rdev->flags)) { |
| 3287 | /* The ReadError flag will just be confusing now */ |
| 3288 | clear_bit(R5_ReadError, &dev->flags); |
| 3289 | clear_bit(R5_ReWrite, &dev->flags); |
| 3290 | } |
| 3291 | if (!rdev || !test_bit(In_sync, &rdev->flags) |
| 3292 | || test_bit(R5_ReadError, &dev->flags)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3293 | if (s.failed < 2) |
| 3294 | r6s.failed_num[s.failed] = i; |
| 3295 | s.failed++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3296 | } else |
| 3297 | set_bit(R5_Insync, &dev->flags); |
| 3298 | } |
| 3299 | rcu_read_unlock(); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3300 | |
| 3301 | if (unlikely(blocked_rdev)) { |
NeilBrown | ac4090d | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3302 | if (s.syncing || s.expanding || s.expanded || |
| 3303 | s.to_write || s.written) { |
| 3304 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3305 | goto unlock; |
| 3306 | } |
| 3307 | /* There is nothing for the blocked_rdev to block */ |
| 3308 | rdev_dec_pending(blocked_rdev, conf->mddev); |
| 3309 | blocked_rdev = NULL; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3310 | } |
NeilBrown | ac4090d | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3311 | |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3312 | if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) { |
| 3313 | set_bit(STRIPE_OP_BIOFILL, &s.ops_request); |
| 3314 | set_bit(STRIPE_BIOFILL_RUN, &sh->state); |
| 3315 | } |
| 3316 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3317 | pr_debug("locked=%d uptodate=%d to_read=%d" |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3318 | " to_write=%d failed=%d failed_num=%d,%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3319 | s.locked, s.uptodate, s.to_read, s.to_write, s.failed, |
| 3320 | r6s.failed_num[0], r6s.failed_num[1]); |
| 3321 | /* check if the array has lost >2 devices and, if so, some requests |
| 3322 | * might need to be failed |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3323 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3324 | if (s.failed > 2 && s.to_read+s.to_write+s.written) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3325 | handle_failed_stripe(conf, sh, &s, disks, &return_bi); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3326 | if (s.failed > 2 && s.syncing) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3327 | md_done_sync(conf->mddev, STRIPE_SECTORS,0); |
| 3328 | clear_bit(STRIPE_SYNCING, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3329 | s.syncing = 0; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3330 | } |
| 3331 | |
| 3332 | /* |
| 3333 | * might be able to return some write requests if the parity blocks |
| 3334 | * are safe, or on a failed drive |
| 3335 | */ |
| 3336 | pdev = &sh->dev[pd_idx]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3337 | r6s.p_failed = (s.failed >= 1 && r6s.failed_num[0] == pd_idx) |
| 3338 | || (s.failed >= 2 && r6s.failed_num[1] == pd_idx); |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 3339 | qdev = &sh->dev[qd_idx]; |
| 3340 | r6s.q_failed = (s.failed >= 1 && r6s.failed_num[0] == qd_idx) |
| 3341 | || (s.failed >= 2 && r6s.failed_num[1] == qd_idx); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3342 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3343 | if ( s.written && |
| 3344 | ( r6s.p_failed || ((test_bit(R5_Insync, &pdev->flags) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3345 | && !test_bit(R5_LOCKED, &pdev->flags) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3346 | && test_bit(R5_UPTODATE, &pdev->flags)))) && |
| 3347 | ( r6s.q_failed || ((test_bit(R5_Insync, &qdev->flags) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3348 | && !test_bit(R5_LOCKED, &qdev->flags) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3349 | && test_bit(R5_UPTODATE, &qdev->flags))))) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3350 | handle_stripe_clean_event(conf, sh, disks, &return_bi); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3351 | |
| 3352 | /* Now we might consider reading some blocks, either to check/generate |
| 3353 | * parity, or to satisfy requests |
| 3354 | * or to load a block that is being partially written. |
| 3355 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3356 | if (s.to_read || s.non_overwrite || (s.to_write && s.failed) || |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3357 | (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3358 | handle_stripe_fill6(sh, &s, &r6s, disks); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3359 | |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3360 | /* Now we check to see if any write operations have recently |
| 3361 | * completed |
| 3362 | */ |
| 3363 | if (sh->reconstruct_state == reconstruct_state_drain_result) { |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3364 | |
| 3365 | sh->reconstruct_state = reconstruct_state_idle; |
| 3366 | /* All the 'written' buffers and the parity blocks are ready to |
| 3367 | * be written back to disk |
| 3368 | */ |
| 3369 | BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags)); |
| 3370 | BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags)); |
| 3371 | for (i = disks; i--; ) { |
| 3372 | dev = &sh->dev[i]; |
| 3373 | if (test_bit(R5_LOCKED, &dev->flags) && |
| 3374 | (i == sh->pd_idx || i == qd_idx || |
| 3375 | dev->written)) { |
| 3376 | pr_debug("Writing block %d\n", i); |
| 3377 | BUG_ON(!test_bit(R5_UPTODATE, &dev->flags)); |
| 3378 | set_bit(R5_Wantwrite, &dev->flags); |
| 3379 | if (!test_bit(R5_Insync, &dev->flags) || |
| 3380 | ((i == sh->pd_idx || i == qd_idx) && |
| 3381 | s.failed == 0)) |
| 3382 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3383 | } |
| 3384 | } |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 3385 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 3386 | dec_preread_active = 1; |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3387 | } |
| 3388 | |
Yuri Tikhonov | a9b39a7 | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3389 | /* Now to consider new write requests and what else, if anything |
| 3390 | * should be read. We do not handle new writes when: |
| 3391 | * 1/ A 'write' operation (copy+gen_syndrome) is already in flight. |
| 3392 | * 2/ A 'check' operation is in flight, as it may clobber the parity |
| 3393 | * block. |
| 3394 | */ |
| 3395 | if (s.to_write && !sh->reconstruct_state && !sh->check_state) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3396 | handle_stripe_dirtying6(conf, sh, &s, &r6s, disks); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3397 | |
| 3398 | /* maybe we need to check and possibly fix the parity for this stripe |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3399 | * Any reads will already have been scheduled, so we just see if enough |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3400 | * data is available. The parity check is held off while parity |
| 3401 | * dependent operations are in flight. |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3402 | */ |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3403 | if (sh->check_state || |
| 3404 | (s.syncing && s.locked == 0 && |
| 3405 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && |
| 3406 | !test_bit(STRIPE_INSYNC, &sh->state))) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3407 | handle_parity_checks6(conf, sh, &s, &r6s, disks); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3408 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3409 | if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3410 | md_done_sync(conf->mddev, STRIPE_SECTORS,1); |
| 3411 | clear_bit(STRIPE_SYNCING, &sh->state); |
| 3412 | } |
| 3413 | |
| 3414 | /* If the failed drives are just a ReadError, then we might need |
| 3415 | * to progress the repair/check process |
| 3416 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3417 | if (s.failed <= 2 && !conf->mddev->ro) |
| 3418 | for (i = 0; i < s.failed; i++) { |
| 3419 | dev = &sh->dev[r6s.failed_num[i]]; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3420 | if (test_bit(R5_ReadError, &dev->flags) |
| 3421 | && !test_bit(R5_LOCKED, &dev->flags) |
| 3422 | && test_bit(R5_UPTODATE, &dev->flags) |
| 3423 | ) { |
| 3424 | if (!test_bit(R5_ReWrite, &dev->flags)) { |
| 3425 | set_bit(R5_Wantwrite, &dev->flags); |
| 3426 | set_bit(R5_ReWrite, &dev->flags); |
| 3427 | set_bit(R5_LOCKED, &dev->flags); |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3428 | s.locked++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3429 | } else { |
| 3430 | /* let's read it back */ |
| 3431 | set_bit(R5_Wantread, &dev->flags); |
| 3432 | set_bit(R5_LOCKED, &dev->flags); |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3433 | s.locked++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3434 | } |
| 3435 | } |
| 3436 | } |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3437 | |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3438 | /* Finish reconstruct operations initiated by the expansion process */ |
| 3439 | if (sh->reconstruct_state == reconstruct_state_result) { |
| 3440 | sh->reconstruct_state = reconstruct_state_idle; |
| 3441 | clear_bit(STRIPE_EXPANDING, &sh->state); |
| 3442 | for (i = conf->raid_disks; i--; ) { |
| 3443 | set_bit(R5_Wantwrite, &sh->dev[i].flags); |
| 3444 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 3445 | s.locked++; |
| 3446 | } |
| 3447 | } |
| 3448 | |
| 3449 | if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) && |
| 3450 | !sh->reconstruct_state) { |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 3451 | struct stripe_head *sh2 |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 3452 | = get_active_stripe(conf, sh->sector, 1, 1, 1); |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 3453 | if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) { |
| 3454 | /* sh cannot be written until sh2 has been read. |
| 3455 | * so arrange for sh to be delayed a little |
| 3456 | */ |
| 3457 | set_bit(STRIPE_DELAYED, &sh->state); |
| 3458 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3459 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, |
| 3460 | &sh2->state)) |
| 3461 | atomic_inc(&conf->preread_active_stripes); |
| 3462 | release_stripe(sh2); |
| 3463 | goto unlock; |
| 3464 | } |
| 3465 | if (sh2) |
| 3466 | release_stripe(sh2); |
| 3467 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3468 | /* Need to write out all blocks after computing P&Q */ |
| 3469 | sh->disks = conf->raid_disks; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3470 | stripe_set_idx(sh->sector, conf, 0, sh); |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3471 | schedule_reconstruction(sh, &s, 1, 1); |
| 3472 | } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) { |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3473 | clear_bit(STRIPE_EXPAND_READY, &sh->state); |
| 3474 | atomic_dec(&conf->reshape_stripes); |
| 3475 | wake_up(&conf->wait_for_overlap); |
| 3476 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 3477 | } |
| 3478 | |
Dan Williams | 0f94e87 | 2008-01-08 15:32:53 -0800 | [diff] [blame] | 3479 | if (s.expanding && s.locked == 0 && |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 3480 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3481 | handle_stripe_expansion(conf, sh, &r6s); |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3482 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3483 | unlock: |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3484 | spin_unlock(&sh->lock); |
| 3485 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3486 | /* wait for this device to become unblocked */ |
| 3487 | if (unlikely(blocked_rdev)) |
| 3488 | md_wait_for_blocked_rdev(blocked_rdev, conf->mddev); |
| 3489 | |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3490 | if (s.ops_request) |
| 3491 | raid_run_ops(sh, s.ops_request); |
| 3492 | |
Dan Williams | f0e43bc | 2008-06-28 08:31:55 +1000 | [diff] [blame] | 3493 | ops_run_io(sh, &s); |
| 3494 | |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 3495 | |
| 3496 | if (dec_preread_active) { |
| 3497 | /* We delay this until after ops_run_io so that if make_request |
| 3498 | * is waiting on a barrier, it won't continue until the writes |
| 3499 | * have actually been submitted. |
| 3500 | */ |
| 3501 | atomic_dec(&conf->preread_active_stripes); |
| 3502 | if (atomic_read(&conf->preread_active_stripes) < |
| 3503 | IO_THRESHOLD) |
| 3504 | md_wakeup_thread(conf->mddev->thread); |
| 3505 | } |
| 3506 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3507 | return_io(return_bi); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3508 | } |
| 3509 | |
NeilBrown | 1442577 | 2009-10-16 15:55:25 +1100 | [diff] [blame] | 3510 | static void handle_stripe(struct stripe_head *sh) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3511 | { |
| 3512 | if (sh->raid_conf->level == 6) |
NeilBrown | 1442577 | 2009-10-16 15:55:25 +1100 | [diff] [blame] | 3513 | handle_stripe6(sh); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3514 | else |
NeilBrown | 1442577 | 2009-10-16 15:55:25 +1100 | [diff] [blame] | 3515 | handle_stripe5(sh); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3516 | } |
| 3517 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 3518 | static void raid5_activate_delayed(raid5_conf_t *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3519 | { |
| 3520 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) { |
| 3521 | while (!list_empty(&conf->delayed_list)) { |
| 3522 | struct list_head *l = conf->delayed_list.next; |
| 3523 | struct stripe_head *sh; |
| 3524 | sh = list_entry(l, struct stripe_head, lru); |
| 3525 | list_del_init(l); |
| 3526 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 3527 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 3528 | atomic_inc(&conf->preread_active_stripes); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3529 | list_add_tail(&sh->lru, &conf->hold_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3530 | } |
NeilBrown | 6ed3003 | 2008-02-06 01:40:00 -0800 | [diff] [blame] | 3531 | } else |
| 3532 | blk_plug_device(conf->mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3533 | } |
| 3534 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 3535 | static void activate_bit_delay(raid5_conf_t *conf) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3536 | { |
| 3537 | /* device_lock is held */ |
| 3538 | struct list_head head; |
| 3539 | list_add(&head, &conf->bitmap_list); |
| 3540 | list_del_init(&conf->bitmap_list); |
| 3541 | while (!list_empty(&head)) { |
| 3542 | struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru); |
| 3543 | list_del_init(&sh->lru); |
| 3544 | atomic_inc(&sh->count); |
| 3545 | __release_stripe(conf, sh); |
| 3546 | } |
| 3547 | } |
| 3548 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3549 | static void unplug_slaves(mddev_t *mddev) |
| 3550 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 3551 | raid5_conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3552 | int i; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 3553 | int devs = max(conf->raid_disks, conf->previous_raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3554 | |
| 3555 | rcu_read_lock(); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 3556 | for (i = 0; i < devs; i++) { |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 3557 | mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3558 | if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3559 | struct request_queue *r_queue = bdev_get_queue(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3560 | |
| 3561 | atomic_inc(&rdev->nr_pending); |
| 3562 | rcu_read_unlock(); |
| 3563 | |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 3564 | blk_unplug(r_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3565 | |
| 3566 | rdev_dec_pending(rdev, mddev); |
| 3567 | rcu_read_lock(); |
| 3568 | } |
| 3569 | } |
| 3570 | rcu_read_unlock(); |
| 3571 | } |
| 3572 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3573 | static void raid5_unplug_device(struct request_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3574 | { |
| 3575 | mddev_t *mddev = q->queuedata; |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 3576 | raid5_conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3577 | unsigned long flags; |
| 3578 | |
| 3579 | spin_lock_irqsave(&conf->device_lock, flags); |
| 3580 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3581 | if (blk_remove_plug(q)) { |
| 3582 | conf->seq_flush++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3583 | raid5_activate_delayed(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3584 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3585 | md_wakeup_thread(mddev->thread); |
| 3586 | |
| 3587 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 3588 | |
| 3589 | unplug_slaves(mddev); |
| 3590 | } |
| 3591 | |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3592 | static int raid5_congested(void *data, int bits) |
| 3593 | { |
| 3594 | mddev_t *mddev = data; |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 3595 | raid5_conf_t *conf = mddev->private; |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3596 | |
| 3597 | /* No difference between reads and writes. Just check |
| 3598 | * how busy the stripe_cache is |
| 3599 | */ |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 3600 | |
| 3601 | if (mddev_congested(mddev, bits)) |
| 3602 | return 1; |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3603 | if (conf->inactive_blocked) |
| 3604 | return 1; |
| 3605 | if (conf->quiesce) |
| 3606 | return 1; |
| 3607 | if (list_empty_careful(&conf->inactive_list)) |
| 3608 | return 1; |
| 3609 | |
| 3610 | return 0; |
| 3611 | } |
| 3612 | |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3613 | /* We want read requests to align with chunks where possible, |
| 3614 | * but write requests don't need to. |
| 3615 | */ |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3616 | static int raid5_mergeable_bvec(struct request_queue *q, |
| 3617 | struct bvec_merge_data *bvm, |
| 3618 | struct bio_vec *biovec) |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3619 | { |
| 3620 | mddev_t *mddev = q->queuedata; |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3621 | 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] | 3622 | int max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3623 | unsigned int chunk_sectors = mddev->chunk_sectors; |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3624 | unsigned int bio_sectors = bvm->bi_size >> 9; |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3625 | |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3626 | if ((bvm->bi_rw & 1) == WRITE) |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3627 | return biovec->bv_len; /* always allow writes to be mergeable */ |
| 3628 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3629 | if (mddev->new_chunk_sectors < mddev->chunk_sectors) |
| 3630 | chunk_sectors = mddev->new_chunk_sectors; |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3631 | max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9; |
| 3632 | if (max < 0) max = 0; |
| 3633 | if (max <= biovec->bv_len && bio_sectors == 0) |
| 3634 | return biovec->bv_len; |
| 3635 | else |
| 3636 | return max; |
| 3637 | } |
| 3638 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3639 | |
| 3640 | static int in_chunk_boundary(mddev_t *mddev, struct bio *bio) |
| 3641 | { |
| 3642 | sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3643 | unsigned int chunk_sectors = mddev->chunk_sectors; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3644 | unsigned int bio_sectors = bio->bi_size >> 9; |
| 3645 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3646 | if (mddev->new_chunk_sectors < mddev->chunk_sectors) |
| 3647 | chunk_sectors = mddev->new_chunk_sectors; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3648 | return chunk_sectors >= |
| 3649 | ((sector & (chunk_sectors - 1)) + bio_sectors); |
| 3650 | } |
| 3651 | |
| 3652 | /* |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3653 | * add bio to the retry LIFO ( in O(1) ... we are in interrupt ) |
| 3654 | * later sampled by raid5d. |
| 3655 | */ |
| 3656 | static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf) |
| 3657 | { |
| 3658 | unsigned long flags; |
| 3659 | |
| 3660 | spin_lock_irqsave(&conf->device_lock, flags); |
| 3661 | |
| 3662 | bi->bi_next = conf->retry_read_aligned_list; |
| 3663 | conf->retry_read_aligned_list = bi; |
| 3664 | |
| 3665 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 3666 | md_wakeup_thread(conf->mddev->thread); |
| 3667 | } |
| 3668 | |
| 3669 | |
| 3670 | static struct bio *remove_bio_from_retry(raid5_conf_t *conf) |
| 3671 | { |
| 3672 | struct bio *bi; |
| 3673 | |
| 3674 | bi = conf->retry_read_aligned; |
| 3675 | if (bi) { |
| 3676 | conf->retry_read_aligned = NULL; |
| 3677 | return bi; |
| 3678 | } |
| 3679 | bi = conf->retry_read_aligned_list; |
| 3680 | if(bi) { |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3681 | conf->retry_read_aligned_list = bi->bi_next; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3682 | bi->bi_next = NULL; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 3683 | /* |
| 3684 | * this sets the active strip count to 1 and the processed |
| 3685 | * strip count to zero (upper 8 bits) |
| 3686 | */ |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3687 | bi->bi_phys_segments = 1; /* biased count of active stripes */ |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3688 | } |
| 3689 | |
| 3690 | return bi; |
| 3691 | } |
| 3692 | |
| 3693 | |
| 3694 | /* |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3695 | * The "raid5_align_endio" should check if the read succeeded and if it |
| 3696 | * did, call bio_endio on the original bio (having bio_put the new bio |
| 3697 | * first). |
| 3698 | * If the read failed.. |
| 3699 | */ |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3700 | static void raid5_align_endio(struct bio *bi, int error) |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3701 | { |
| 3702 | struct bio* raid_bi = bi->bi_private; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3703 | mddev_t *mddev; |
| 3704 | raid5_conf_t *conf; |
| 3705 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
| 3706 | mdk_rdev_t *rdev; |
| 3707 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3708 | bio_put(bi); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3709 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3710 | rdev = (void*)raid_bi->bi_next; |
| 3711 | raid_bi->bi_next = NULL; |
NeilBrown | 2b7f222 | 2010-03-25 16:06:03 +1100 | [diff] [blame] | 3712 | mddev = rdev->mddev; |
| 3713 | conf = mddev->private; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3714 | |
| 3715 | rdev_dec_pending(rdev, conf->mddev); |
| 3716 | |
| 3717 | if (!error && uptodate) { |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3718 | bio_endio(raid_bi, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3719 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
| 3720 | wake_up(&conf->wait_for_stripe); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3721 | return; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3722 | } |
| 3723 | |
| 3724 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3725 | 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] | 3726 | |
| 3727 | add_bio_to_retry(raid_bi, conf); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3728 | } |
| 3729 | |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3730 | static int bio_fits_rdev(struct bio *bi) |
| 3731 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3732 | struct request_queue *q = bdev_get_queue(bi->bi_bdev); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3733 | |
Martin K. Petersen | ae03bf6 | 2009-05-22 17:17:50 -0400 | [diff] [blame] | 3734 | if ((bi->bi_size>>9) > queue_max_sectors(q)) |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3735 | return 0; |
| 3736 | blk_recount_segments(q, bi); |
Martin K. Petersen | 8a78362 | 2010-02-26 00:20:39 -0500 | [diff] [blame] | 3737 | if (bi->bi_phys_segments > queue_max_segments(q)) |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3738 | return 0; |
| 3739 | |
| 3740 | if (q->merge_bvec_fn) |
| 3741 | /* it's too hard to apply the merge_bvec_fn at this stage, |
| 3742 | * just just give up |
| 3743 | */ |
| 3744 | return 0; |
| 3745 | |
| 3746 | return 1; |
| 3747 | } |
| 3748 | |
| 3749 | |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 3750 | 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] | 3751 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 3752 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 8553fe7 | 2009-12-14 12:49:47 +1100 | [diff] [blame] | 3753 | int dd_idx; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3754 | struct bio* align_bi; |
| 3755 | mdk_rdev_t *rdev; |
| 3756 | |
| 3757 | if (!in_chunk_boundary(mddev, raid_bio)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3758 | pr_debug("chunk_aligned_read : non aligned\n"); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3759 | return 0; |
| 3760 | } |
| 3761 | /* |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3762 | * use bio_clone to make a copy of the bio |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3763 | */ |
| 3764 | align_bi = bio_clone(raid_bio, GFP_NOIO); |
| 3765 | if (!align_bi) |
| 3766 | return 0; |
| 3767 | /* |
| 3768 | * set bi_end_io to a new function, and set bi_private to the |
| 3769 | * original bio. |
| 3770 | */ |
| 3771 | align_bi->bi_end_io = raid5_align_endio; |
| 3772 | align_bi->bi_private = raid_bio; |
| 3773 | /* |
| 3774 | * compute position |
| 3775 | */ |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3776 | align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector, |
| 3777 | 0, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3778 | &dd_idx, NULL); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3779 | |
| 3780 | rcu_read_lock(); |
| 3781 | rdev = rcu_dereference(conf->disks[dd_idx].rdev); |
| 3782 | if (rdev && test_bit(In_sync, &rdev->flags)) { |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3783 | atomic_inc(&rdev->nr_pending); |
| 3784 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3785 | raid_bio->bi_next = (void*)rdev; |
| 3786 | align_bi->bi_bdev = rdev->bdev; |
| 3787 | align_bi->bi_flags &= ~(1 << BIO_SEG_VALID); |
| 3788 | align_bi->bi_sector += rdev->data_offset; |
| 3789 | |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3790 | if (!bio_fits_rdev(align_bi)) { |
| 3791 | /* too big in some way */ |
| 3792 | bio_put(align_bi); |
| 3793 | rdev_dec_pending(rdev, mddev); |
| 3794 | return 0; |
| 3795 | } |
| 3796 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3797 | spin_lock_irq(&conf->device_lock); |
| 3798 | wait_event_lock_irq(conf->wait_for_stripe, |
| 3799 | conf->quiesce == 0, |
| 3800 | conf->device_lock, /* nothing */); |
| 3801 | atomic_inc(&conf->active_aligned_reads); |
| 3802 | spin_unlock_irq(&conf->device_lock); |
| 3803 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3804 | generic_make_request(align_bi); |
| 3805 | return 1; |
| 3806 | } else { |
| 3807 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3808 | bio_put(align_bi); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3809 | return 0; |
| 3810 | } |
| 3811 | } |
| 3812 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3813 | /* __get_priority_stripe - get the next stripe to process |
| 3814 | * |
| 3815 | * Full stripe writes are allowed to pass preread active stripes up until |
| 3816 | * the bypass_threshold is exceeded. In general the bypass_count |
| 3817 | * increments when the handle_list is handled before the hold_list; however, it |
| 3818 | * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a |
| 3819 | * stripe with in flight i/o. The bypass_count will be reset when the |
| 3820 | * head of the hold_list has changed, i.e. the head was promoted to the |
| 3821 | * handle_list. |
| 3822 | */ |
| 3823 | static struct stripe_head *__get_priority_stripe(raid5_conf_t *conf) |
| 3824 | { |
| 3825 | struct stripe_head *sh; |
| 3826 | |
| 3827 | pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n", |
| 3828 | __func__, |
| 3829 | list_empty(&conf->handle_list) ? "empty" : "busy", |
| 3830 | list_empty(&conf->hold_list) ? "empty" : "busy", |
| 3831 | atomic_read(&conf->pending_full_writes), conf->bypass_count); |
| 3832 | |
| 3833 | if (!list_empty(&conf->handle_list)) { |
| 3834 | sh = list_entry(conf->handle_list.next, typeof(*sh), lru); |
| 3835 | |
| 3836 | if (list_empty(&conf->hold_list)) |
| 3837 | conf->bypass_count = 0; |
| 3838 | else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) { |
| 3839 | if (conf->hold_list.next == conf->last_hold) |
| 3840 | conf->bypass_count++; |
| 3841 | else { |
| 3842 | conf->last_hold = conf->hold_list.next; |
| 3843 | conf->bypass_count -= conf->bypass_threshold; |
| 3844 | if (conf->bypass_count < 0) |
| 3845 | conf->bypass_count = 0; |
| 3846 | } |
| 3847 | } |
| 3848 | } else if (!list_empty(&conf->hold_list) && |
| 3849 | ((conf->bypass_threshold && |
| 3850 | conf->bypass_count > conf->bypass_threshold) || |
| 3851 | atomic_read(&conf->pending_full_writes) == 0)) { |
| 3852 | sh = list_entry(conf->hold_list.next, |
| 3853 | typeof(*sh), lru); |
| 3854 | conf->bypass_count -= conf->bypass_threshold; |
| 3855 | if (conf->bypass_count < 0) |
| 3856 | conf->bypass_count = 0; |
| 3857 | } else |
| 3858 | return NULL; |
| 3859 | |
| 3860 | list_del_init(&sh->lru); |
| 3861 | atomic_inc(&sh->count); |
| 3862 | BUG_ON(atomic_read(&sh->count) != 1); |
| 3863 | return sh; |
| 3864 | } |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3865 | |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 3866 | static int make_request(mddev_t *mddev, struct bio * bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3867 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 3868 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3869 | int dd_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3870 | sector_t new_sector; |
| 3871 | sector_t logical_sector, last_sector; |
| 3872 | struct stripe_head *sh; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 3873 | const int rw = bio_data_dir(bi); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 3874 | int remaining; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3875 | |
Jens Axboe | 1f98a13 | 2009-09-11 14:32:04 +0200 | [diff] [blame] | 3876 | if (unlikely(bio_rw_flagged(bi, BIO_RW_BARRIER))) { |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 3877 | /* Drain all pending writes. We only really need |
| 3878 | * to ensure they have been submitted, but this is |
| 3879 | * easier. |
| 3880 | */ |
| 3881 | mddev->pers->quiesce(mddev, 1); |
| 3882 | mddev->pers->quiesce(mddev, 0); |
| 3883 | md_barrier_request(mddev, bi); |
NeilBrown | e5dcdd8 | 2005-09-09 16:23:41 -0700 | [diff] [blame] | 3884 | return 0; |
| 3885 | } |
| 3886 | |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 3887 | md_write_start(mddev, bi); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 3888 | |
NeilBrown | 802ba06 | 2006-12-13 00:34:13 -0800 | [diff] [blame] | 3889 | if (rw == READ && |
Raz Ben-Jehuda(caro) | 5248861 | 2006-12-10 02:20:48 -0800 | [diff] [blame] | 3890 | mddev->reshape_position == MaxSector && |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 3891 | chunk_aligned_read(mddev,bi)) |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3892 | return 0; |
Raz Ben-Jehuda(caro) | 5248861 | 2006-12-10 02:20:48 -0800 | [diff] [blame] | 3893 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3894 | logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
| 3895 | last_sector = bi->bi_sector + (bi->bi_size>>9); |
| 3896 | bi->bi_next = NULL; |
| 3897 | bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 3898 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3899 | for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) { |
| 3900 | DEFINE_WAIT(w); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3901 | int disks, data_disks; |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3902 | int previous; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 3903 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3904 | retry: |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3905 | previous = 0; |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 3906 | disks = conf->raid_disks; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 3907 | prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 3908 | if (unlikely(conf->reshape_progress != MaxSector)) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3909 | /* spinlock is needed as reshape_progress may be |
NeilBrown | df8e7f7 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 3910 | * 64bit on a 32bit platform, and so it might be |
| 3911 | * possible to see a half-updated value |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3912 | * Ofcourse reshape_progress could change after |
NeilBrown | df8e7f7 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 3913 | * the lock is dropped, so once we get a reference |
| 3914 | * to the stripe that we think it is, we will have |
| 3915 | * to check again. |
| 3916 | */ |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3917 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3918 | if (mddev->delta_disks < 0 |
| 3919 | ? logical_sector < conf->reshape_progress |
| 3920 | : logical_sector >= conf->reshape_progress) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3921 | disks = conf->previous_raid_disks; |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3922 | previous = 1; |
| 3923 | } else { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3924 | if (mddev->delta_disks < 0 |
| 3925 | ? logical_sector < conf->reshape_safe |
| 3926 | : logical_sector >= conf->reshape_safe) { |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 3927 | spin_unlock_irq(&conf->device_lock); |
| 3928 | schedule(); |
| 3929 | goto retry; |
| 3930 | } |
| 3931 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3932 | spin_unlock_irq(&conf->device_lock); |
| 3933 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3934 | data_disks = disks - conf->max_degraded; |
| 3935 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3936 | new_sector = raid5_compute_sector(conf, logical_sector, |
| 3937 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3938 | &dd_idx, NULL); |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 3939 | pr_debug("raid456: make_request, sector %llu logical %llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3940 | (unsigned long long)new_sector, |
| 3941 | (unsigned long long)logical_sector); |
| 3942 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3943 | sh = get_active_stripe(conf, new_sector, previous, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 3944 | (bi->bi_rw&RWA_MASK), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3945 | if (sh) { |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 3946 | if (unlikely(previous)) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3947 | /* expansion might have moved on while waiting for a |
NeilBrown | df8e7f7 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 3948 | * stripe, so we must do the range check again. |
| 3949 | * Expansion could still move past after this |
| 3950 | * test, but as we are holding a reference to |
| 3951 | * 'sh', we know that if that happens, |
| 3952 | * STRIPE_EXPANDING will get set and the expansion |
| 3953 | * won't proceed until we finish with the stripe. |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3954 | */ |
| 3955 | int must_retry = 0; |
| 3956 | spin_lock_irq(&conf->device_lock); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 3957 | if (mddev->delta_disks < 0 |
| 3958 | ? logical_sector >= conf->reshape_progress |
| 3959 | : logical_sector < conf->reshape_progress) |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3960 | /* mismatch, need to try again */ |
| 3961 | must_retry = 1; |
| 3962 | spin_unlock_irq(&conf->device_lock); |
| 3963 | if (must_retry) { |
| 3964 | release_stripe(sh); |
Dan Williams | 7a3ab90 | 2009-06-16 16:00:33 -0700 | [diff] [blame] | 3965 | schedule(); |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3966 | goto retry; |
| 3967 | } |
| 3968 | } |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 3969 | |
NeilBrown | a5c308d | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 3970 | if (bio_data_dir(bi) == WRITE && |
| 3971 | logical_sector >= mddev->suspend_lo && |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3972 | logical_sector < mddev->suspend_hi) { |
| 3973 | release_stripe(sh); |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 3974 | /* As the suspend_* range is controlled by |
| 3975 | * userspace, we want an interruptible |
| 3976 | * wait. |
| 3977 | */ |
| 3978 | flush_signals(current); |
| 3979 | prepare_to_wait(&conf->wait_for_overlap, |
| 3980 | &w, TASK_INTERRUPTIBLE); |
| 3981 | if (logical_sector >= mddev->suspend_lo && |
| 3982 | logical_sector < mddev->suspend_hi) |
| 3983 | schedule(); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3984 | goto retry; |
| 3985 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3986 | |
| 3987 | if (test_bit(STRIPE_EXPANDING, &sh->state) || |
| 3988 | !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) { |
| 3989 | /* Stripe is busy expanding or |
| 3990 | * add failed due to overlap. Flush everything |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3991 | * and wait a while |
| 3992 | */ |
| 3993 | raid5_unplug_device(mddev->queue); |
| 3994 | release_stripe(sh); |
| 3995 | schedule(); |
| 3996 | goto retry; |
| 3997 | } |
| 3998 | finish_wait(&conf->wait_for_overlap, &w); |
NeilBrown | 6ed3003 | 2008-02-06 01:40:00 -0800 | [diff] [blame] | 3999 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4000 | clear_bit(STRIPE_DELAYED, &sh->state); |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 4001 | if (mddev->barrier && |
| 4002 | !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 4003 | atomic_inc(&conf->preread_active_stripes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4004 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4005 | } else { |
| 4006 | /* cannot get stripe for read-ahead, just give-up */ |
| 4007 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
| 4008 | finish_wait(&conf->wait_for_overlap, &w); |
| 4009 | break; |
| 4010 | } |
| 4011 | |
| 4012 | } |
| 4013 | spin_lock_irq(&conf->device_lock); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4014 | remaining = raid5_dec_bi_phys_segments(bi); |
NeilBrown | f634475 | 2006-03-27 01:18:17 -0800 | [diff] [blame] | 4015 | spin_unlock_irq(&conf->device_lock); |
| 4016 | if (remaining == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4017 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4018 | if ( rw == WRITE ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4019 | md_write_end(mddev); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 4020 | |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 4021 | bio_endio(bi, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4022 | } |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 4023 | |
| 4024 | if (mddev->barrier) { |
| 4025 | /* We need to wait for the stripes to all be handled. |
| 4026 | * So: wait for preread_active_stripes to drop to 0. |
| 4027 | */ |
| 4028 | wait_event(mddev->thread->wqueue, |
| 4029 | atomic_read(&conf->preread_active_stripes) == 0); |
| 4030 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4031 | return 0; |
| 4032 | } |
| 4033 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4034 | static sector_t raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks); |
| 4035 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4036 | 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] | 4037 | { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4038 | /* reshaping is quite different to recovery/resync so it is |
| 4039 | * handled quite separately ... here. |
| 4040 | * |
| 4041 | * On each call to sync_request, we gather one chunk worth of |
| 4042 | * destination stripes and flag them as expanding. |
| 4043 | * Then we find all the source stripes and request reads. |
| 4044 | * As the reads complete, handle_stripe will copy the data |
| 4045 | * into the destination stripe and release that stripe. |
| 4046 | */ |
H Hartley Sweeten | 7b92813 | 2010-03-08 16:02:40 +1100 | [diff] [blame] | 4047 | raid5_conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4048 | struct stripe_head *sh; |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4049 | sector_t first_sector, last_sector; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4050 | int raid_disks = conf->previous_raid_disks; |
| 4051 | int data_disks = raid_disks - conf->max_degraded; |
| 4052 | int new_data_disks = conf->raid_disks - conf->max_degraded; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4053 | int i; |
| 4054 | int dd_idx; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4055 | sector_t writepos, readpos, safepos; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4056 | sector_t stripe_addr; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4057 | int reshape_sectors; |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4058 | struct list_head stripes; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4059 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4060 | if (sector_nr == 0) { |
| 4061 | /* If restarting in the middle, skip the initial sectors */ |
| 4062 | if (mddev->delta_disks < 0 && |
| 4063 | conf->reshape_progress < raid5_size(mddev, 0, 0)) { |
| 4064 | sector_nr = raid5_size(mddev, 0, 0) |
| 4065 | - conf->reshape_progress; |
NeilBrown | a639755 | 2009-08-13 10:13:00 +1000 | [diff] [blame] | 4066 | } else if (mddev->delta_disks >= 0 && |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4067 | conf->reshape_progress > 0) |
| 4068 | sector_nr = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4069 | sector_div(sector_nr, new_data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4070 | if (sector_nr) { |
NeilBrown | 8dee721 | 2009-11-06 14:59:29 +1100 | [diff] [blame] | 4071 | mddev->curr_resync_completed = sector_nr; |
| 4072 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4073 | *skipped = 1; |
| 4074 | return sector_nr; |
| 4075 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4076 | } |
| 4077 | |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4078 | /* We need to process a full chunk at a time. |
| 4079 | * If old and new chunk sizes differ, we need to process the |
| 4080 | * largest of these |
| 4081 | */ |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4082 | if (mddev->new_chunk_sectors > mddev->chunk_sectors) |
| 4083 | reshape_sectors = mddev->new_chunk_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4084 | else |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4085 | reshape_sectors = mddev->chunk_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4086 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4087 | /* we update the metadata when there is more than 3Meg |
| 4088 | * in the block range (that is rather arbitrary, should |
| 4089 | * probably be time based) or when the data about to be |
| 4090 | * copied would over-write the source of the data at |
| 4091 | * the front of the range. |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4092 | * i.e. one new_stripe along from reshape_progress new_maps |
| 4093 | * to after where reshape_safe old_maps to |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4094 | */ |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4095 | writepos = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4096 | sector_div(writepos, new_data_disks); |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4097 | readpos = conf->reshape_progress; |
| 4098 | sector_div(readpos, data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4099 | safepos = conf->reshape_safe; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4100 | sector_div(safepos, data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4101 | if (mddev->delta_disks < 0) { |
NeilBrown | ed37d83 | 2009-05-27 21:39:05 +1000 | [diff] [blame] | 4102 | writepos -= min_t(sector_t, reshape_sectors, writepos); |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4103 | readpos += reshape_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4104 | safepos += reshape_sectors; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4105 | } else { |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4106 | writepos += reshape_sectors; |
NeilBrown | ed37d83 | 2009-05-27 21:39:05 +1000 | [diff] [blame] | 4107 | readpos -= min_t(sector_t, reshape_sectors, readpos); |
| 4108 | safepos -= min_t(sector_t, reshape_sectors, safepos); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4109 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4110 | |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4111 | /* 'writepos' is the most advanced device address we might write. |
| 4112 | * 'readpos' is the least advanced device address we might read. |
| 4113 | * 'safepos' is the least address recorded in the metadata as having |
| 4114 | * been reshaped. |
| 4115 | * If 'readpos' is behind 'writepos', then there is no way that we can |
| 4116 | * ensure safety in the face of a crash - that must be done by userspace |
| 4117 | * making a backup of the data. So in that case there is no particular |
| 4118 | * rush to update metadata. |
| 4119 | * Otherwise if 'safepos' is behind 'writepos', then we really need to |
| 4120 | * update the metadata to advance 'safepos' to match 'readpos' so that |
| 4121 | * we can be safe in the event of a crash. |
| 4122 | * So we insist on updating metadata if safepos is behind writepos and |
| 4123 | * readpos is beyond writepos. |
| 4124 | * In any case, update the metadata every 10 seconds. |
| 4125 | * Maybe that number should be configurable, but I'm not sure it is |
| 4126 | * worth it.... maybe it could be a multiple of safemode_delay??? |
| 4127 | */ |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4128 | if ((mddev->delta_disks < 0 |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4129 | ? (safepos > writepos && readpos < writepos) |
| 4130 | : (safepos < writepos && readpos > writepos)) || |
| 4131 | time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4132 | /* Cannot proceed until we've updated the superblock... */ |
| 4133 | wait_event(conf->wait_for_overlap, |
| 4134 | atomic_read(&conf->reshape_stripes)==0); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4135 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4136 | mddev->curr_resync_completed = mddev->curr_resync; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4137 | conf->reshape_checkpoint = jiffies; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4138 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4139 | md_wakeup_thread(mddev->thread); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4140 | wait_event(mddev->sb_wait, mddev->flags == 0 || |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4141 | kthread_should_stop()); |
| 4142 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4143 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4144 | spin_unlock_irq(&conf->device_lock); |
| 4145 | wake_up(&conf->wait_for_overlap); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4146 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4147 | } |
| 4148 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4149 | if (mddev->delta_disks < 0) { |
| 4150 | BUG_ON(conf->reshape_progress == 0); |
| 4151 | stripe_addr = writepos; |
| 4152 | BUG_ON((mddev->dev_sectors & |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4153 | ~((sector_t)reshape_sectors - 1)) |
| 4154 | - reshape_sectors - stripe_addr |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4155 | != sector_nr); |
| 4156 | } else { |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4157 | BUG_ON(writepos != sector_nr + reshape_sectors); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4158 | stripe_addr = sector_nr; |
| 4159 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4160 | INIT_LIST_HEAD(&stripes); |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4161 | for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4162 | int j; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4163 | int skipped_disk = 0; |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4164 | sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4165 | set_bit(STRIPE_EXPANDING, &sh->state); |
| 4166 | atomic_inc(&conf->reshape_stripes); |
| 4167 | /* If any of this stripe is beyond the end of the old |
| 4168 | * array, then we need to zero those blocks |
| 4169 | */ |
| 4170 | for (j=sh->disks; j--;) { |
| 4171 | sector_t s; |
| 4172 | if (j == sh->pd_idx) |
| 4173 | continue; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4174 | if (conf->level == 6 && |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4175 | j == sh->qd_idx) |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4176 | continue; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 4177 | s = compute_blocknr(sh, j, 0); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4178 | if (s < raid5_size(mddev, 0, 0)) { |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4179 | skipped_disk = 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4180 | continue; |
| 4181 | } |
| 4182 | memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE); |
| 4183 | set_bit(R5_Expanded, &sh->dev[j].flags); |
| 4184 | set_bit(R5_UPTODATE, &sh->dev[j].flags); |
| 4185 | } |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4186 | if (!skipped_disk) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4187 | set_bit(STRIPE_EXPAND_READY, &sh->state); |
| 4188 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4189 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4190 | list_add(&sh->lru, &stripes); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4191 | } |
| 4192 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4193 | if (mddev->delta_disks < 0) |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4194 | conf->reshape_progress -= reshape_sectors * new_data_disks; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4195 | else |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4196 | conf->reshape_progress += reshape_sectors * new_data_disks; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4197 | spin_unlock_irq(&conf->device_lock); |
| 4198 | /* Ok, those stripe are ready. We can start scheduling |
| 4199 | * reads on the source stripes. |
| 4200 | * The source stripes are determined by mapping the first and last |
| 4201 | * block on the destination stripes. |
| 4202 | */ |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4203 | first_sector = |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4204 | raid5_compute_sector(conf, stripe_addr*(new_data_disks), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4205 | 1, &dd_idx, NULL); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4206 | last_sector = |
NeilBrown | 0e6e027 | 2009-06-09 16:32:22 +1000 | [diff] [blame] | 4207 | raid5_compute_sector(conf, ((stripe_addr+reshape_sectors) |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 4208 | * new_data_disks - 1), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4209 | 1, &dd_idx, NULL); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4210 | if (last_sector >= mddev->dev_sectors) |
| 4211 | last_sector = mddev->dev_sectors - 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4212 | while (first_sector <= last_sector) { |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4213 | sh = get_active_stripe(conf, first_sector, 1, 0, 1); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4214 | set_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 4215 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4216 | release_stripe(sh); |
| 4217 | first_sector += STRIPE_SECTORS; |
| 4218 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4219 | /* Now that the sources are clearly marked, we can release |
| 4220 | * the destination stripes |
| 4221 | */ |
| 4222 | while (!list_empty(&stripes)) { |
| 4223 | sh = list_entry(stripes.next, struct stripe_head, lru); |
| 4224 | list_del_init(&sh->lru); |
| 4225 | release_stripe(sh); |
| 4226 | } |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4227 | /* If this takes us to the resync_max point where we have to pause, |
| 4228 | * then we need to write out the superblock. |
| 4229 | */ |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4230 | sector_nr += reshape_sectors; |
NeilBrown | c03f6a1 | 2009-04-17 11:06:30 +1000 | [diff] [blame] | 4231 | if ((sector_nr - mddev->curr_resync_completed) * 2 |
| 4232 | >= mddev->resync_max - mddev->curr_resync_completed) { |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4233 | /* Cannot proceed until we've updated the superblock... */ |
| 4234 | wait_event(conf->wait_for_overlap, |
| 4235 | atomic_read(&conf->reshape_stripes) == 0); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4236 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 48606a9 | 2009-06-18 09:14:12 +1000 | [diff] [blame] | 4237 | mddev->curr_resync_completed = mddev->curr_resync + reshape_sectors; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4238 | conf->reshape_checkpoint = jiffies; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4239 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 4240 | md_wakeup_thread(mddev->thread); |
| 4241 | wait_event(mddev->sb_wait, |
| 4242 | !test_bit(MD_CHANGE_DEVS, &mddev->flags) |
| 4243 | || kthread_should_stop()); |
| 4244 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4245 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4246 | spin_unlock_irq(&conf->device_lock); |
| 4247 | wake_up(&conf->wait_for_overlap); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4248 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4249 | } |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4250 | return reshape_sectors; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4251 | } |
| 4252 | |
| 4253 | /* FIXME go_faster isn't used */ |
| 4254 | static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster) |
| 4255 | { |
H Hartley Sweeten | 7b92813 | 2010-03-08 16:02:40 +1100 | [diff] [blame] | 4256 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4257 | struct stripe_head *sh; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4258 | sector_t max_sector = mddev->dev_sectors; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4259 | int sync_blocks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4260 | int still_degraded = 0; |
| 4261 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4262 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4263 | if (sector_nr >= max_sector) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4264 | /* just being told to finish up .. nothing much to do */ |
| 4265 | unplug_slaves(mddev); |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 4266 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 4267 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { |
| 4268 | end_reshape(conf); |
| 4269 | return 0; |
| 4270 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4271 | |
| 4272 | if (mddev->curr_resync < max_sector) /* aborted */ |
| 4273 | bitmap_end_sync(mddev->bitmap, mddev->curr_resync, |
| 4274 | &sync_blocks, 1); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4275 | else /* completed sync */ |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4276 | conf->fullsync = 0; |
| 4277 | bitmap_close_sync(mddev->bitmap); |
| 4278 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4279 | return 0; |
| 4280 | } |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4281 | |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4282 | /* Allow raid5_quiesce to complete */ |
| 4283 | wait_event(conf->wait_for_overlap, conf->quiesce != 2); |
| 4284 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4285 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 4286 | return reshape_request(mddev, sector_nr, skipped); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4287 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4288 | /* No need to check resync_max as we never do more than one |
| 4289 | * stripe, and as resync_max will always be on a chunk boundary, |
| 4290 | * if the check in md_do_sync didn't fire, there is no chance |
| 4291 | * of overstepping resync_max here |
| 4292 | */ |
| 4293 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4294 | /* if there is too many failed drives and we are trying |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4295 | * to resync, then assert that we are finished, because there is |
| 4296 | * nothing we can do. |
| 4297 | */ |
NeilBrown | 3285edf | 2006-06-26 00:27:55 -0700 | [diff] [blame] | 4298 | if (mddev->degraded >= conf->max_degraded && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4299 | test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4300 | sector_t rv = mddev->dev_sectors - sector_nr; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 4301 | *skipped = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4302 | return rv; |
| 4303 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4304 | if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) && |
NeilBrown | 3855ad9 | 2005-11-08 21:39:38 -0800 | [diff] [blame] | 4305 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4306 | !conf->fullsync && sync_blocks >= STRIPE_SECTORS) { |
| 4307 | /* we can skip this block, and probably more */ |
| 4308 | sync_blocks /= STRIPE_SECTORS; |
| 4309 | *skipped = 1; |
| 4310 | return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */ |
| 4311 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4312 | |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 4313 | |
| 4314 | bitmap_cond_end_sync(mddev->bitmap, sector_nr); |
| 4315 | |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4316 | sh = get_active_stripe(conf, sector_nr, 0, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4317 | if (sh == NULL) { |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4318 | sh = get_active_stripe(conf, sector_nr, 0, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4319 | /* make sure we don't swamp the stripe cache if someone else |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4320 | * is trying to get access |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4321 | */ |
Nishanth Aravamudan | 66c006a | 2005-11-07 01:01:17 -0800 | [diff] [blame] | 4322 | schedule_timeout_uninterruptible(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4323 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4324 | /* Need to check if array will still be degraded after recovery/resync |
| 4325 | * We don't need to check the 'failed' flag as when that gets set, |
| 4326 | * recovery aborts. |
| 4327 | */ |
NeilBrown | f001a70 | 2009-06-09 14:30:31 +1000 | [diff] [blame] | 4328 | for (i = 0; i < conf->raid_disks; i++) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4329 | if (conf->disks[i].rdev == NULL) |
| 4330 | still_degraded = 1; |
| 4331 | |
| 4332 | bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded); |
| 4333 | |
| 4334 | spin_lock(&sh->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4335 | set_bit(STRIPE_SYNCING, &sh->state); |
| 4336 | clear_bit(STRIPE_INSYNC, &sh->state); |
| 4337 | spin_unlock(&sh->lock); |
| 4338 | |
NeilBrown | 1442577 | 2009-10-16 15:55:25 +1100 | [diff] [blame] | 4339 | handle_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4340 | release_stripe(sh); |
| 4341 | |
| 4342 | return STRIPE_SECTORS; |
| 4343 | } |
| 4344 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4345 | static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio) |
| 4346 | { |
| 4347 | /* We may not be able to submit a whole bio at once as there |
| 4348 | * may not be enough stripe_heads available. |
| 4349 | * We cannot pre-allocate enough stripe_heads as we may need |
| 4350 | * more than exist in the cache (if we allow ever large chunks). |
| 4351 | * So we do one stripe head at a time and record in |
| 4352 | * ->bi_hw_segments how many have been done. |
| 4353 | * |
| 4354 | * We *know* that this entire raid_bio is in one chunk, so |
| 4355 | * it will be only one 'dd_idx' and only need one call to raid5_compute_sector. |
| 4356 | */ |
| 4357 | struct stripe_head *sh; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4358 | int dd_idx; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4359 | sector_t sector, logical_sector, last_sector; |
| 4360 | int scnt = 0; |
| 4361 | int remaining; |
| 4362 | int handled = 0; |
| 4363 | |
| 4364 | logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4365 | sector = raid5_compute_sector(conf, logical_sector, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4366 | 0, &dd_idx, NULL); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4367 | last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9); |
| 4368 | |
| 4369 | for (; logical_sector < last_sector; |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4370 | logical_sector += STRIPE_SECTORS, |
| 4371 | sector += STRIPE_SECTORS, |
| 4372 | scnt++) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4373 | |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4374 | if (scnt < raid5_bi_hw_segments(raid_bio)) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4375 | /* already done this stripe */ |
| 4376 | continue; |
| 4377 | |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4378 | sh = get_active_stripe(conf, sector, 0, 1, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4379 | |
| 4380 | if (!sh) { |
| 4381 | /* failed to get a stripe - must wait */ |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4382 | raid5_set_bi_hw_segments(raid_bio, scnt); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4383 | conf->retry_read_aligned = raid_bio; |
| 4384 | return handled; |
| 4385 | } |
| 4386 | |
| 4387 | set_bit(R5_ReadError, &sh->dev[dd_idx].flags); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4388 | if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) { |
| 4389 | release_stripe(sh); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4390 | raid5_set_bi_hw_segments(raid_bio, scnt); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4391 | conf->retry_read_aligned = raid_bio; |
| 4392 | return handled; |
| 4393 | } |
| 4394 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4395 | handle_stripe(sh); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4396 | release_stripe(sh); |
| 4397 | handled++; |
| 4398 | } |
| 4399 | spin_lock_irq(&conf->device_lock); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4400 | remaining = raid5_dec_bi_phys_segments(raid_bio); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4401 | spin_unlock_irq(&conf->device_lock); |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 4402 | if (remaining == 0) |
| 4403 | bio_endio(raid_bio, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4404 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
| 4405 | wake_up(&conf->wait_for_stripe); |
| 4406 | return handled; |
| 4407 | } |
| 4408 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4409 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4410 | /* |
| 4411 | * This is our raid5 kernel thread. |
| 4412 | * |
| 4413 | * We scan the hash table for stripes which can be handled now. |
| 4414 | * During the scan, completed stripes are saved for us by the interrupt |
| 4415 | * handler, so that they will not have to wait for our next wakeup. |
| 4416 | */ |
NeilBrown | 6ed3003 | 2008-02-06 01:40:00 -0800 | [diff] [blame] | 4417 | static void raid5d(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4418 | { |
| 4419 | struct stripe_head *sh; |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 4420 | raid5_conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4421 | int handled; |
| 4422 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4423 | pr_debug("+++ raid5d active\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4424 | |
| 4425 | md_check_recovery(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4426 | |
| 4427 | handled = 0; |
| 4428 | spin_lock_irq(&conf->device_lock); |
| 4429 | while (1) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4430 | struct bio *bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4431 | |
NeilBrown | ae3c20c | 2006-07-10 04:44:17 -0700 | [diff] [blame] | 4432 | if (conf->seq_flush != conf->seq_write) { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4433 | int seq = conf->seq_flush; |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 4434 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4435 | bitmap_unplug(mddev->bitmap); |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 4436 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4437 | conf->seq_write = seq; |
| 4438 | activate_bit_delay(conf); |
| 4439 | } |
| 4440 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4441 | while ((bio = remove_bio_from_retry(conf))) { |
| 4442 | int ok; |
| 4443 | spin_unlock_irq(&conf->device_lock); |
| 4444 | ok = retry_aligned_read(conf, bio); |
| 4445 | spin_lock_irq(&conf->device_lock); |
| 4446 | if (!ok) |
| 4447 | break; |
| 4448 | handled++; |
| 4449 | } |
| 4450 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4451 | sh = __get_priority_stripe(conf); |
| 4452 | |
Dan Williams | c9f21aa | 2008-07-23 12:05:51 -0700 | [diff] [blame] | 4453 | if (!sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4454 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4455 | spin_unlock_irq(&conf->device_lock); |
| 4456 | |
| 4457 | handled++; |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 4458 | handle_stripe(sh); |
| 4459 | release_stripe(sh); |
| 4460 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4461 | |
| 4462 | spin_lock_irq(&conf->device_lock); |
| 4463 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4464 | pr_debug("%d stripes handled\n", handled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4465 | |
| 4466 | spin_unlock_irq(&conf->device_lock); |
| 4467 | |
Dan Williams | c9f21aa | 2008-07-23 12:05:51 -0700 | [diff] [blame] | 4468 | async_tx_issue_pending_all(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4469 | unplug_slaves(mddev); |
| 4470 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4471 | pr_debug("--- raid5d inactive\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4472 | } |
| 4473 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4474 | static ssize_t |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4475 | raid5_show_stripe_cache_size(mddev_t *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4476 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 4477 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4478 | if (conf) |
| 4479 | return sprintf(page, "%d\n", conf->max_nr_stripes); |
| 4480 | else |
| 4481 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4482 | } |
| 4483 | |
| 4484 | static ssize_t |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4485 | 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] | 4486 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 4487 | raid5_conf_t *conf = mddev->private; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4488 | unsigned long new; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4489 | int err; |
| 4490 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4491 | if (len >= PAGE_SIZE) |
| 4492 | return -EINVAL; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4493 | if (!conf) |
| 4494 | return -ENODEV; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4495 | |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4496 | if (strict_strtoul(page, 10, &new)) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4497 | return -EINVAL; |
| 4498 | if (new <= 16 || new > 32768) |
| 4499 | return -EINVAL; |
| 4500 | while (new < conf->max_nr_stripes) { |
| 4501 | if (drop_one_stripe(conf)) |
| 4502 | conf->max_nr_stripes--; |
| 4503 | else |
| 4504 | break; |
| 4505 | } |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4506 | err = md_allow_write(mddev); |
| 4507 | if (err) |
| 4508 | return err; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4509 | while (new > conf->max_nr_stripes) { |
| 4510 | if (grow_one_stripe(conf)) |
| 4511 | conf->max_nr_stripes++; |
| 4512 | else break; |
| 4513 | } |
| 4514 | return len; |
| 4515 | } |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4516 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4517 | static struct md_sysfs_entry |
| 4518 | raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR, |
| 4519 | raid5_show_stripe_cache_size, |
| 4520 | raid5_store_stripe_cache_size); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4521 | |
| 4522 | static ssize_t |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4523 | raid5_show_preread_threshold(mddev_t *mddev, char *page) |
| 4524 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 4525 | raid5_conf_t *conf = mddev->private; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4526 | if (conf) |
| 4527 | return sprintf(page, "%d\n", conf->bypass_threshold); |
| 4528 | else |
| 4529 | return 0; |
| 4530 | } |
| 4531 | |
| 4532 | static ssize_t |
| 4533 | raid5_store_preread_threshold(mddev_t *mddev, const char *page, size_t len) |
| 4534 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 4535 | raid5_conf_t *conf = mddev->private; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4536 | unsigned long new; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4537 | if (len >= PAGE_SIZE) |
| 4538 | return -EINVAL; |
| 4539 | if (!conf) |
| 4540 | return -ENODEV; |
| 4541 | |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4542 | if (strict_strtoul(page, 10, &new)) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4543 | return -EINVAL; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4544 | if (new > conf->max_nr_stripes) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4545 | return -EINVAL; |
| 4546 | conf->bypass_threshold = new; |
| 4547 | return len; |
| 4548 | } |
| 4549 | |
| 4550 | static struct md_sysfs_entry |
| 4551 | raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold, |
| 4552 | S_IRUGO | S_IWUSR, |
| 4553 | raid5_show_preread_threshold, |
| 4554 | raid5_store_preread_threshold); |
| 4555 | |
| 4556 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4557 | stripe_cache_active_show(mddev_t *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4558 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 4559 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4560 | if (conf) |
| 4561 | return sprintf(page, "%d\n", atomic_read(&conf->active_stripes)); |
| 4562 | else |
| 4563 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4564 | } |
| 4565 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4566 | static struct md_sysfs_entry |
| 4567 | raid5_stripecache_active = __ATTR_RO(stripe_cache_active); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4568 | |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4569 | static struct attribute *raid5_attrs[] = { |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4570 | &raid5_stripecache_size.attr, |
| 4571 | &raid5_stripecache_active.attr, |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4572 | &raid5_preread_bypass_threshold.attr, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4573 | NULL, |
| 4574 | }; |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4575 | static struct attribute_group raid5_attrs_group = { |
| 4576 | .name = NULL, |
| 4577 | .attrs = raid5_attrs, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4578 | }; |
| 4579 | |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4580 | static sector_t |
| 4581 | raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks) |
| 4582 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 4583 | raid5_conf_t *conf = mddev->private; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4584 | |
| 4585 | if (!sectors) |
| 4586 | sectors = mddev->dev_sectors; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4587 | if (!raid_disks) |
NeilBrown | 7ec0547 | 2009-03-31 15:10:36 +1100 | [diff] [blame] | 4588 | /* size is defined by the smallest of previous and new size */ |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4589 | raid_disks = min(conf->raid_disks, conf->previous_raid_disks); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4590 | |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4591 | sectors &= ~((sector_t)mddev->chunk_sectors - 1); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4592 | sectors &= ~((sector_t)mddev->new_chunk_sectors - 1); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4593 | return sectors * (raid_disks - conf->max_degraded); |
| 4594 | } |
| 4595 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4596 | static void raid5_free_percpu(raid5_conf_t *conf) |
| 4597 | { |
| 4598 | struct raid5_percpu *percpu; |
| 4599 | unsigned long cpu; |
| 4600 | |
| 4601 | if (!conf->percpu) |
| 4602 | return; |
| 4603 | |
| 4604 | get_online_cpus(); |
| 4605 | for_each_possible_cpu(cpu) { |
| 4606 | percpu = per_cpu_ptr(conf->percpu, cpu); |
| 4607 | safe_put_page(percpu->spare_page); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4608 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4609 | } |
| 4610 | #ifdef CONFIG_HOTPLUG_CPU |
| 4611 | unregister_cpu_notifier(&conf->cpu_notify); |
| 4612 | #endif |
| 4613 | put_online_cpus(); |
| 4614 | |
| 4615 | free_percpu(conf->percpu); |
| 4616 | } |
| 4617 | |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 4618 | static void free_conf(raid5_conf_t *conf) |
| 4619 | { |
| 4620 | shrink_stripes(conf); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4621 | raid5_free_percpu(conf); |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 4622 | kfree(conf->disks); |
| 4623 | kfree(conf->stripe_hashtbl); |
| 4624 | kfree(conf); |
| 4625 | } |
| 4626 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4627 | #ifdef CONFIG_HOTPLUG_CPU |
| 4628 | static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action, |
| 4629 | void *hcpu) |
| 4630 | { |
| 4631 | raid5_conf_t *conf = container_of(nfb, raid5_conf_t, cpu_notify); |
| 4632 | long cpu = (long)hcpu; |
| 4633 | struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu); |
| 4634 | |
| 4635 | switch (action) { |
| 4636 | case CPU_UP_PREPARE: |
| 4637 | case CPU_UP_PREPARE_FROZEN: |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4638 | if (conf->level == 6 && !percpu->spare_page) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4639 | percpu->spare_page = alloc_page(GFP_KERNEL); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4640 | if (!percpu->scribble) |
| 4641 | percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL); |
| 4642 | |
| 4643 | if (!percpu->scribble || |
| 4644 | (conf->level == 6 && !percpu->spare_page)) { |
| 4645 | safe_put_page(percpu->spare_page); |
| 4646 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4647 | pr_err("%s: failed memory allocation for cpu%ld\n", |
| 4648 | __func__, cpu); |
Akinobu Mita | 55af6bb | 2010-05-26 14:43:35 -0700 | [diff] [blame] | 4649 | return notifier_from_errno(-ENOMEM); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4650 | } |
| 4651 | break; |
| 4652 | case CPU_DEAD: |
| 4653 | case CPU_DEAD_FROZEN: |
| 4654 | safe_put_page(percpu->spare_page); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4655 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4656 | percpu->spare_page = NULL; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4657 | percpu->scribble = NULL; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4658 | break; |
| 4659 | default: |
| 4660 | break; |
| 4661 | } |
| 4662 | return NOTIFY_OK; |
| 4663 | } |
| 4664 | #endif |
| 4665 | |
| 4666 | static int raid5_alloc_percpu(raid5_conf_t *conf) |
| 4667 | { |
| 4668 | unsigned long cpu; |
| 4669 | struct page *spare_page; |
Tejun Heo | a29d8b8 | 2010-02-02 14:39:15 +0900 | [diff] [blame] | 4670 | struct raid5_percpu __percpu *allcpus; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4671 | void *scribble; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4672 | int err; |
| 4673 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4674 | allcpus = alloc_percpu(struct raid5_percpu); |
| 4675 | if (!allcpus) |
| 4676 | return -ENOMEM; |
| 4677 | conf->percpu = allcpus; |
| 4678 | |
| 4679 | get_online_cpus(); |
| 4680 | err = 0; |
| 4681 | for_each_present_cpu(cpu) { |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4682 | if (conf->level == 6) { |
| 4683 | spare_page = alloc_page(GFP_KERNEL); |
| 4684 | if (!spare_page) { |
| 4685 | err = -ENOMEM; |
| 4686 | break; |
| 4687 | } |
| 4688 | per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page; |
| 4689 | } |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4690 | scribble = kmalloc(conf->scribble_len, GFP_KERNEL); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4691 | if (!scribble) { |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4692 | err = -ENOMEM; |
| 4693 | break; |
| 4694 | } |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4695 | per_cpu_ptr(conf->percpu, cpu)->scribble = scribble; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4696 | } |
| 4697 | #ifdef CONFIG_HOTPLUG_CPU |
| 4698 | conf->cpu_notify.notifier_call = raid456_cpu_notify; |
| 4699 | conf->cpu_notify.priority = 0; |
| 4700 | if (err == 0) |
| 4701 | err = register_cpu_notifier(&conf->cpu_notify); |
| 4702 | #endif |
| 4703 | put_online_cpus(); |
| 4704 | |
| 4705 | return err; |
| 4706 | } |
| 4707 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4708 | static raid5_conf_t *setup_conf(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4709 | { |
| 4710 | raid5_conf_t *conf; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4711 | int raid_disk, memory, max_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4712 | mdk_rdev_t *rdev; |
| 4713 | struct disk_info *disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4714 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4715 | if (mddev->new_level != 5 |
| 4716 | && mddev->new_level != 4 |
| 4717 | && mddev->new_level != 6) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4718 | 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] | 4719 | mdname(mddev), mddev->new_level); |
| 4720 | return ERR_PTR(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4721 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4722 | if ((mddev->new_level == 5 |
| 4723 | && !algorithm_valid_raid5(mddev->new_layout)) || |
| 4724 | (mddev->new_level == 6 |
| 4725 | && !algorithm_valid_raid6(mddev->new_layout))) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4726 | printk(KERN_ERR "md/raid:%s: layout %d not supported\n", |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4727 | mdname(mddev), mddev->new_layout); |
| 4728 | return ERR_PTR(-EIO); |
| 4729 | } |
| 4730 | if (mddev->new_level == 6 && mddev->raid_disks < 4) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4731 | 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] | 4732 | mdname(mddev), mddev->raid_disks); |
| 4733 | return ERR_PTR(-EINVAL); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4734 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4735 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4736 | if (!mddev->new_chunk_sectors || |
| 4737 | (mddev->new_chunk_sectors << 9) % PAGE_SIZE || |
| 4738 | !is_power_of_2(mddev->new_chunk_sectors)) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4739 | printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n", |
| 4740 | mdname(mddev), mddev->new_chunk_sectors << 9); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4741 | return ERR_PTR(-EINVAL); |
NeilBrown | 4bbf377 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4742 | } |
| 4743 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4744 | conf = kzalloc(sizeof(raid5_conf_t), GFP_KERNEL); |
| 4745 | if (conf == NULL) |
| 4746 | goto abort; |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 4747 | spin_lock_init(&conf->device_lock); |
| 4748 | init_waitqueue_head(&conf->wait_for_stripe); |
| 4749 | init_waitqueue_head(&conf->wait_for_overlap); |
| 4750 | INIT_LIST_HEAD(&conf->handle_list); |
| 4751 | INIT_LIST_HEAD(&conf->hold_list); |
| 4752 | INIT_LIST_HEAD(&conf->delayed_list); |
| 4753 | INIT_LIST_HEAD(&conf->bitmap_list); |
| 4754 | INIT_LIST_HEAD(&conf->inactive_list); |
| 4755 | atomic_set(&conf->active_stripes, 0); |
| 4756 | atomic_set(&conf->preread_active_stripes, 0); |
| 4757 | atomic_set(&conf->active_aligned_reads, 0); |
| 4758 | conf->bypass_threshold = BYPASS_THRESHOLD; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4759 | |
| 4760 | conf->raid_disks = mddev->raid_disks; |
| 4761 | if (mddev->reshape_position == MaxSector) |
| 4762 | conf->previous_raid_disks = mddev->raid_disks; |
| 4763 | else |
| 4764 | conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4765 | max_disks = max(conf->raid_disks, conf->previous_raid_disks); |
| 4766 | conf->scribble_len = scribble_len(max_disks); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4767 | |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4768 | conf->disks = kzalloc(max_disks * sizeof(struct disk_info), |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4769 | GFP_KERNEL); |
| 4770 | if (!conf->disks) |
| 4771 | goto abort; |
| 4772 | |
| 4773 | conf->mddev = mddev; |
| 4774 | |
| 4775 | if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL) |
| 4776 | goto abort; |
| 4777 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4778 | conf->level = mddev->new_level; |
| 4779 | if (raid5_alloc_percpu(conf) != 0) |
| 4780 | goto abort; |
| 4781 | |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4782 | pr_debug("raid456: run(%s) called.\n", mdname(mddev)); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4783 | |
| 4784 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 4785 | raid_disk = rdev->raid_disk; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4786 | if (raid_disk >= max_disks |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4787 | || raid_disk < 0) |
| 4788 | continue; |
| 4789 | disk = conf->disks + raid_disk; |
| 4790 | |
| 4791 | disk->rdev = rdev; |
| 4792 | |
| 4793 | if (test_bit(In_sync, &rdev->flags)) { |
| 4794 | char b[BDEVNAME_SIZE]; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4795 | printk(KERN_INFO "md/raid:%s: device %s operational as raid" |
| 4796 | " disk %d\n", |
| 4797 | mdname(mddev), bdevname(rdev->bdev, b), raid_disk); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4798 | } else |
| 4799 | /* Cannot rely on bitmap to complete recovery */ |
| 4800 | conf->fullsync = 1; |
| 4801 | } |
| 4802 | |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 4803 | conf->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4804 | conf->level = mddev->new_level; |
| 4805 | if (conf->level == 6) |
| 4806 | conf->max_degraded = 2; |
| 4807 | else |
| 4808 | conf->max_degraded = 1; |
| 4809 | conf->algorithm = mddev->new_layout; |
| 4810 | conf->max_nr_stripes = NR_STRIPES; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4811 | conf->reshape_progress = mddev->reshape_position; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 4812 | if (conf->reshape_progress != MaxSector) { |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 4813 | conf->prev_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 4814 | conf->prev_algo = mddev->layout; |
| 4815 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4816 | |
| 4817 | memory = conf->max_nr_stripes * (sizeof(struct stripe_head) + |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4818 | max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4819 | if (grow_stripes(conf, conf->max_nr_stripes)) { |
| 4820 | printk(KERN_ERR |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4821 | "md/raid:%s: couldn't allocate %dkB for buffers\n", |
| 4822 | mdname(mddev), memory); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4823 | goto abort; |
| 4824 | } else |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4825 | printk(KERN_INFO "md/raid:%s: allocated %dkB\n", |
| 4826 | mdname(mddev), memory); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4827 | |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 4828 | conf->thread = md_register_thread(raid5d, mddev, NULL); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4829 | if (!conf->thread) { |
| 4830 | printk(KERN_ERR |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4831 | "md/raid:%s: couldn't allocate thread.\n", |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4832 | mdname(mddev)); |
| 4833 | goto abort; |
| 4834 | } |
| 4835 | |
| 4836 | return conf; |
| 4837 | |
| 4838 | abort: |
| 4839 | if (conf) { |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 4840 | free_conf(conf); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4841 | return ERR_PTR(-EIO); |
| 4842 | } else |
| 4843 | return ERR_PTR(-ENOMEM); |
| 4844 | } |
| 4845 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 4846 | |
| 4847 | static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded) |
| 4848 | { |
| 4849 | switch (algo) { |
| 4850 | case ALGORITHM_PARITY_0: |
| 4851 | if (raid_disk < max_degraded) |
| 4852 | return 1; |
| 4853 | break; |
| 4854 | case ALGORITHM_PARITY_N: |
| 4855 | if (raid_disk >= raid_disks - max_degraded) |
| 4856 | return 1; |
| 4857 | break; |
| 4858 | case ALGORITHM_PARITY_0_6: |
| 4859 | if (raid_disk == 0 || |
| 4860 | raid_disk == raid_disks - 1) |
| 4861 | return 1; |
| 4862 | break; |
| 4863 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 4864 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 4865 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 4866 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 4867 | if (raid_disk == raid_disks - 1) |
| 4868 | return 1; |
| 4869 | } |
| 4870 | return 0; |
| 4871 | } |
| 4872 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4873 | static int run(mddev_t *mddev) |
| 4874 | { |
| 4875 | raid5_conf_t *conf; |
Martin K. Petersen | 8f6c2e4 | 2009-07-01 11:13:45 +1000 | [diff] [blame] | 4876 | int working_disks = 0, chunk_size; |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 4877 | int dirty_parity_disks = 0; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4878 | mdk_rdev_t *rdev; |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 4879 | sector_t reshape_offset = 0; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4880 | |
Andre Noll | 8c6ac86 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 4881 | if (mddev->recovery_cp != MaxSector) |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4882 | printk(KERN_NOTICE "md/raid:%s: not clean" |
Andre Noll | 8c6ac86 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 4883 | " -- starting background reconstruction\n", |
| 4884 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4885 | if (mddev->reshape_position != MaxSector) { |
| 4886 | /* Check that we can continue the reshape. |
| 4887 | * Currently only disks can change, it must |
| 4888 | * increase, and we must be past the point where |
| 4889 | * a stripe over-writes itself |
| 4890 | */ |
| 4891 | sector_t here_new, here_old; |
| 4892 | int old_disks; |
Andre Noll | 18b0033 | 2009-03-31 15:00:56 +1100 | [diff] [blame] | 4893 | int max_degraded = (mddev->level == 6 ? 2 : 1); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4894 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 4895 | if (mddev->new_level != mddev->level) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4896 | printk(KERN_ERR "md/raid:%s: unsupported reshape " |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4897 | "required - aborting.\n", |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4898 | mdname(mddev)); |
| 4899 | return -EINVAL; |
| 4900 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4901 | old_disks = mddev->raid_disks - mddev->delta_disks; |
| 4902 | /* reshape_position must be on a new-stripe boundary, and one |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4903 | * further up in new geometry must map after here in old |
| 4904 | * geometry. |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4905 | */ |
| 4906 | here_new = mddev->reshape_position; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4907 | if (sector_div(here_new, mddev->new_chunk_sectors * |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4908 | (mddev->raid_disks - max_degraded))) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4909 | printk(KERN_ERR "md/raid:%s: reshape_position not " |
| 4910 | "on a stripe boundary\n", mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4911 | return -EINVAL; |
| 4912 | } |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 4913 | reshape_offset = here_new * mddev->new_chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4914 | /* here_new is the stripe we will write to */ |
| 4915 | here_old = mddev->reshape_position; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4916 | sector_div(here_old, mddev->chunk_sectors * |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4917 | (old_disks-max_degraded)); |
| 4918 | /* here_old is the first stripe that we might need to read |
| 4919 | * from */ |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 4920 | if (mddev->delta_disks == 0) { |
| 4921 | /* We cannot be sure it is safe to start an in-place |
| 4922 | * reshape. It is only safe if user-space if monitoring |
| 4923 | * and taking constant backups. |
| 4924 | * mdadm always starts a situation like this in |
| 4925 | * readonly mode so it can take control before |
| 4926 | * allowing any writes. So just check for that. |
| 4927 | */ |
| 4928 | if ((here_new * mddev->new_chunk_sectors != |
| 4929 | here_old * mddev->chunk_sectors) || |
| 4930 | mddev->ro == 0) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4931 | printk(KERN_ERR "md/raid:%s: in-place reshape must be started" |
| 4932 | " in read-only mode - aborting\n", |
| 4933 | mdname(mddev)); |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 4934 | return -EINVAL; |
| 4935 | } |
| 4936 | } else if (mddev->delta_disks < 0 |
| 4937 | ? (here_new * mddev->new_chunk_sectors <= |
| 4938 | here_old * mddev->chunk_sectors) |
| 4939 | : (here_new * mddev->new_chunk_sectors >= |
| 4940 | here_old * mddev->chunk_sectors)) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4941 | /* Reading from the same stripe as writing to - bad */ |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4942 | printk(KERN_ERR "md/raid:%s: reshape_position too early for " |
| 4943 | "auto-recovery - aborting.\n", |
| 4944 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4945 | return -EINVAL; |
| 4946 | } |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4947 | printk(KERN_INFO "md/raid:%s: reshape will continue\n", |
| 4948 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4949 | /* OK, we should be able to continue; */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4950 | } else { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4951 | BUG_ON(mddev->level != mddev->new_level); |
| 4952 | BUG_ON(mddev->layout != mddev->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4953 | BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4954 | BUG_ON(mddev->delta_disks != 0); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4955 | } |
| 4956 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4957 | if (mddev->private == NULL) |
| 4958 | conf = setup_conf(mddev); |
| 4959 | else |
| 4960 | conf = mddev->private; |
| 4961 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4962 | if (IS_ERR(conf)) |
| 4963 | return PTR_ERR(conf); |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 4964 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4965 | mddev->thread = conf->thread; |
| 4966 | conf->thread = NULL; |
| 4967 | mddev->private = conf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4968 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4969 | /* |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4970 | * 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] | 4971 | */ |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 4972 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 4973 | if (rdev->raid_disk < 0) |
| 4974 | continue; |
| 4975 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4976 | working_disks++; |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 4977 | /* This disc is not fully in-sync. However if it |
| 4978 | * just stored parity (beyond the recovery_offset), |
| 4979 | * when we don't need to be concerned about the |
| 4980 | * array being dirty. |
| 4981 | * When reshape goes 'backwards', we never have |
| 4982 | * partially completed devices, so we only need |
| 4983 | * to worry about reshape going forwards. |
| 4984 | */ |
| 4985 | /* Hack because v0.91 doesn't store recovery_offset properly. */ |
| 4986 | if (mddev->major_version == 0 && |
| 4987 | mddev->minor_version > 90) |
| 4988 | rdev->recovery_offset = reshape_offset; |
| 4989 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 4990 | if (rdev->recovery_offset < reshape_offset) { |
| 4991 | /* We need to check old and new layout */ |
| 4992 | if (!only_parity(rdev->raid_disk, |
| 4993 | conf->algorithm, |
| 4994 | conf->raid_disks, |
| 4995 | conf->max_degraded)) |
| 4996 | continue; |
| 4997 | } |
| 4998 | if (!only_parity(rdev->raid_disk, |
| 4999 | conf->prev_algo, |
| 5000 | conf->previous_raid_disks, |
| 5001 | conf->max_degraded)) |
| 5002 | continue; |
| 5003 | dirty_parity_disks++; |
| 5004 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5005 | |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 5006 | mddev->degraded = (max(conf->raid_disks, conf->previous_raid_disks) |
| 5007 | - working_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5008 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5009 | if (mddev->degraded > conf->max_degraded) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5010 | printk(KERN_ERR "md/raid:%s: not enough operational devices" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5011 | " (%d/%d failed)\n", |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 5012 | mdname(mddev), mddev->degraded, conf->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5013 | goto abort; |
| 5014 | } |
| 5015 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5016 | /* device size must be a multiple of chunk size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5017 | mddev->dev_sectors &= ~(mddev->chunk_sectors - 1); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5018 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 5019 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5020 | if (mddev->degraded > dirty_parity_disks && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5021 | mddev->recovery_cp != MaxSector) { |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5022 | if (mddev->ok_start_degraded) |
| 5023 | printk(KERN_WARNING |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5024 | "md/raid:%s: starting dirty degraded array" |
| 5025 | " - data corruption possible.\n", |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5026 | mdname(mddev)); |
| 5027 | else { |
| 5028 | printk(KERN_ERR |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5029 | "md/raid:%s: cannot start dirty degraded array.\n", |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5030 | mdname(mddev)); |
| 5031 | goto abort; |
| 5032 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5033 | } |
| 5034 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5035 | if (mddev->degraded == 0) |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5036 | printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d" |
| 5037 | " devices, algorithm %d\n", mdname(mddev), conf->level, |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 5038 | mddev->raid_disks-mddev->degraded, mddev->raid_disks, |
| 5039 | mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5040 | else |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5041 | printk(KERN_ALERT "md/raid:%s: raid level %d active with %d" |
| 5042 | " out of %d devices, algorithm %d\n", |
| 5043 | mdname(mddev), conf->level, |
| 5044 | mddev->raid_disks - mddev->degraded, |
| 5045 | mddev->raid_disks, mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5046 | |
| 5047 | print_raid5_conf(conf); |
| 5048 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5049 | if (conf->reshape_progress != MaxSector) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5050 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5051 | atomic_set(&conf->reshape_stripes, 0); |
| 5052 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 5053 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 5054 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 5055 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 5056 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 5057 | "reshape"); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5058 | } |
| 5059 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5060 | /* read-ahead size must cover two whole stripes, which is |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5061 | * 2 * (datadisks) * chunksize where 'n' is the number of raid devices |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5062 | */ |
| 5063 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5064 | int data_disks = conf->previous_raid_disks - conf->max_degraded; |
| 5065 | int stripe = data_disks * |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5066 | ((mddev->chunk_sectors << 9) / PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5067 | if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 5068 | mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
| 5069 | } |
| 5070 | |
| 5071 | /* Ok, everything is just fine now */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 5072 | if (mddev->to_remove == &raid5_attrs_group) |
| 5073 | mddev->to_remove = NULL; |
| 5074 | else if (sysfs_create_group(&mddev->kobj, &raid5_attrs_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5075 | printk(KERN_WARNING |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5076 | "md/raid:%s: failed to create sysfs attributes.\n", |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5077 | mdname(mddev)); |
NeilBrown | 7a5febe | 2005-05-16 21:53:16 -0700 | [diff] [blame] | 5078 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5079 | mddev->queue->queue_lock = &conf->device_lock; |
| 5080 | |
NeilBrown | 7a5febe | 2005-05-16 21:53:16 -0700 | [diff] [blame] | 5081 | mddev->queue->unplug_fn = raid5_unplug_device; |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 5082 | mddev->queue->backing_dev_info.congested_data = mddev; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5083 | mddev->queue->backing_dev_info.congested_fn = raid5_congested; |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 5084 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5085 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
NeilBrown | 7a5febe | 2005-05-16 21:53:16 -0700 | [diff] [blame] | 5086 | |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 5087 | blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec); |
Martin K. Petersen | 8f6c2e4 | 2009-07-01 11:13:45 +1000 | [diff] [blame] | 5088 | chunk_size = mddev->chunk_sectors << 9; |
| 5089 | blk_queue_io_min(mddev->queue, chunk_size); |
| 5090 | blk_queue_io_opt(mddev->queue, chunk_size * |
| 5091 | (conf->raid_disks - conf->max_degraded)); |
| 5092 | |
| 5093 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 5094 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 5095 | rdev->data_offset << 9); |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 5096 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5097 | return 0; |
| 5098 | abort: |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5099 | md_unregister_thread(mddev->thread); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5100 | mddev->thread = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5101 | if (conf) { |
| 5102 | print_raid5_conf(conf); |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 5103 | free_conf(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5104 | } |
| 5105 | mddev->private = NULL; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5106 | 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] | 5107 | return -EIO; |
| 5108 | } |
| 5109 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 5110 | static int stop(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5111 | { |
H Hartley Sweeten | 7b92813 | 2010-03-08 16:02:40 +1100 | [diff] [blame] | 5112 | raid5_conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5113 | |
| 5114 | md_unregister_thread(mddev->thread); |
| 5115 | mddev->thread = NULL; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5116 | mddev->queue->backing_dev_info.congested_fn = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5117 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 5118 | free_conf(conf); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 5119 | mddev->private = NULL; |
| 5120 | mddev->to_remove = &raid5_attrs_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5121 | return 0; |
| 5122 | } |
| 5123 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 5124 | #ifdef DEBUG |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5125 | static void print_sh(struct seq_file *seq, struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5126 | { |
| 5127 | int i; |
| 5128 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5129 | seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n", |
| 5130 | (unsigned long long)sh->sector, sh->pd_idx, sh->state); |
| 5131 | seq_printf(seq, "sh %llu, count %d.\n", |
| 5132 | (unsigned long long)sh->sector, atomic_read(&sh->count)); |
| 5133 | seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector); |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5134 | for (i = 0; i < sh->disks; i++) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5135 | seq_printf(seq, "(cache%d: %p %ld) ", |
| 5136 | i, sh->dev[i].page, sh->dev[i].flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5137 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5138 | seq_printf(seq, "\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5139 | } |
| 5140 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5141 | static void printall(struct seq_file *seq, raid5_conf_t *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5142 | { |
| 5143 | struct stripe_head *sh; |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 5144 | struct hlist_node *hn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5145 | int i; |
| 5146 | |
| 5147 | spin_lock_irq(&conf->device_lock); |
| 5148 | for (i = 0; i < NR_HASH; i++) { |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 5149 | hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5150 | if (sh->raid_conf != conf) |
| 5151 | continue; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5152 | print_sh(seq, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5153 | } |
| 5154 | } |
| 5155 | spin_unlock_irq(&conf->device_lock); |
| 5156 | } |
| 5157 | #endif |
| 5158 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5159 | static void status(struct seq_file *seq, mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5160 | { |
H Hartley Sweeten | 7b92813 | 2010-03-08 16:02:40 +1100 | [diff] [blame] | 5161 | raid5_conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5162 | int i; |
| 5163 | |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5164 | seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level, |
| 5165 | mddev->chunk_sectors / 2, mddev->layout); |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 5166 | 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] | 5167 | for (i = 0; i < conf->raid_disks; i++) |
| 5168 | seq_printf (seq, "%s", |
| 5169 | conf->disks[i].rdev && |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5170 | test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5171 | seq_printf (seq, "]"); |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 5172 | #ifdef DEBUG |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5173 | seq_printf (seq, "\n"); |
| 5174 | printall(seq, conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5175 | #endif |
| 5176 | } |
| 5177 | |
| 5178 | static void print_raid5_conf (raid5_conf_t *conf) |
| 5179 | { |
| 5180 | int i; |
| 5181 | struct disk_info *tmp; |
| 5182 | |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5183 | printk(KERN_DEBUG "RAID conf printout:\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5184 | if (!conf) { |
| 5185 | printk("(conf==NULL)\n"); |
| 5186 | return; |
| 5187 | } |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5188 | printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level, |
| 5189 | conf->raid_disks, |
| 5190 | conf->raid_disks - conf->mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5191 | |
| 5192 | for (i = 0; i < conf->raid_disks; i++) { |
| 5193 | char b[BDEVNAME_SIZE]; |
| 5194 | tmp = conf->disks + i; |
| 5195 | if (tmp->rdev) |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5196 | printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n", |
| 5197 | i, !test_bit(Faulty, &tmp->rdev->flags), |
| 5198 | bdevname(tmp->rdev->bdev, b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5199 | } |
| 5200 | } |
| 5201 | |
| 5202 | static int raid5_spare_active(mddev_t *mddev) |
| 5203 | { |
| 5204 | int i; |
| 5205 | raid5_conf_t *conf = mddev->private; |
| 5206 | struct disk_info *tmp; |
| 5207 | |
| 5208 | for (i = 0; i < conf->raid_disks; i++) { |
| 5209 | tmp = conf->disks + i; |
| 5210 | if (tmp->rdev |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame^] | 5211 | && tmp->rdev->recovery_offset == MaxSector |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5212 | && !test_bit(Faulty, &tmp->rdev->flags) |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 5213 | && !test_and_set_bit(In_sync, &tmp->rdev->flags)) { |
| 5214 | unsigned long flags; |
| 5215 | spin_lock_irqsave(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5216 | mddev->degraded--; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 5217 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5218 | } |
| 5219 | } |
| 5220 | print_raid5_conf(conf); |
| 5221 | return 0; |
| 5222 | } |
| 5223 | |
| 5224 | static int raid5_remove_disk(mddev_t *mddev, int number) |
| 5225 | { |
| 5226 | raid5_conf_t *conf = mddev->private; |
| 5227 | int err = 0; |
| 5228 | mdk_rdev_t *rdev; |
| 5229 | struct disk_info *p = conf->disks + number; |
| 5230 | |
| 5231 | print_raid5_conf(conf); |
| 5232 | rdev = p->rdev; |
| 5233 | if (rdev) { |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5234 | if (number >= conf->raid_disks && |
| 5235 | conf->reshape_progress == MaxSector) |
| 5236 | clear_bit(In_sync, &rdev->flags); |
| 5237 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5238 | if (test_bit(In_sync, &rdev->flags) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5239 | atomic_read(&rdev->nr_pending)) { |
| 5240 | err = -EBUSY; |
| 5241 | goto abort; |
| 5242 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5243 | /* Only remove non-faulty devices if recovery |
| 5244 | * isn't possible. |
| 5245 | */ |
| 5246 | if (!test_bit(Faulty, &rdev->flags) && |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5247 | mddev->degraded <= conf->max_degraded && |
| 5248 | number < conf->raid_disks) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5249 | err = -EBUSY; |
| 5250 | goto abort; |
| 5251 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5252 | p->rdev = NULL; |
Paul E. McKenney | fbd568a3e | 2005-05-01 08:59:04 -0700 | [diff] [blame] | 5253 | synchronize_rcu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5254 | if (atomic_read(&rdev->nr_pending)) { |
| 5255 | /* lost the race, try later */ |
| 5256 | err = -EBUSY; |
| 5257 | p->rdev = rdev; |
| 5258 | } |
| 5259 | } |
| 5260 | abort: |
| 5261 | |
| 5262 | print_raid5_conf(conf); |
| 5263 | return err; |
| 5264 | } |
| 5265 | |
| 5266 | static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev) |
| 5267 | { |
| 5268 | raid5_conf_t *conf = mddev->private; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5269 | int err = -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5270 | int disk; |
| 5271 | struct disk_info *p; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5272 | int first = 0; |
| 5273 | int last = conf->raid_disks - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5274 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5275 | if (mddev->degraded > conf->max_degraded) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5276 | /* no point adding a device */ |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5277 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5278 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5279 | if (rdev->raid_disk >= 0) |
| 5280 | first = last = rdev->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5281 | |
| 5282 | /* |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5283 | * find the disk ... but prefer rdev->saved_raid_disk |
| 5284 | * if possible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5285 | */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5286 | if (rdev->saved_raid_disk >= 0 && |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5287 | rdev->saved_raid_disk >= first && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5288 | conf->disks[rdev->saved_raid_disk].rdev == NULL) |
| 5289 | disk = rdev->saved_raid_disk; |
| 5290 | else |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5291 | disk = first; |
| 5292 | for ( ; disk <= last ; disk++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5293 | if ((p=conf->disks + disk)->rdev == NULL) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5294 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5295 | rdev->raid_disk = disk; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5296 | err = 0; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5297 | if (rdev->saved_raid_disk != disk) |
| 5298 | conf->fullsync = 1; |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 5299 | rcu_assign_pointer(p->rdev, rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5300 | break; |
| 5301 | } |
| 5302 | print_raid5_conf(conf); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5303 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5304 | } |
| 5305 | |
| 5306 | static int raid5_resize(mddev_t *mddev, sector_t sectors) |
| 5307 | { |
| 5308 | /* no resync is happening, and there is enough space |
| 5309 | * on all devices, so we can resize. |
| 5310 | * We need to make sure resync covers any new space. |
| 5311 | * If the array is shrinking we should possibly wait until |
| 5312 | * any io in the removed space completes, but it hardly seems |
| 5313 | * worth it. |
| 5314 | */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5315 | sectors &= ~((sector_t)mddev->chunk_sectors - 1); |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5316 | md_set_array_sectors(mddev, raid5_size(mddev, sectors, |
| 5317 | mddev->raid_disks)); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5318 | if (mddev->array_sectors > |
| 5319 | raid5_size(mddev, sectors, mddev->raid_disks)) |
| 5320 | return -EINVAL; |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 5321 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5322 | revalidate_disk(mddev->gendisk); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5323 | if (sectors > mddev->dev_sectors && mddev->recovery_cp == MaxSector) { |
| 5324 | mddev->recovery_cp = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5325 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5326 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5327 | mddev->dev_sectors = sectors; |
NeilBrown | 4b5c7ae | 2005-07-27 11:43:28 -0700 | [diff] [blame] | 5328 | mddev->resync_max_sectors = sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5329 | return 0; |
| 5330 | } |
| 5331 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5332 | static int check_stripe_cache(mddev_t *mddev) |
| 5333 | { |
| 5334 | /* Can only proceed if there are plenty of stripe_heads. |
| 5335 | * We need a minimum of one full stripe,, and for sensible progress |
| 5336 | * it is best to have about 4 times that. |
| 5337 | * If we require 4 times, then the default 256 4K stripe_heads will |
| 5338 | * allow for chunk sizes up to 256K, which is probably OK. |
| 5339 | * If the chunk size is greater, user-space should request more |
| 5340 | * stripe_heads first. |
| 5341 | */ |
| 5342 | raid5_conf_t *conf = mddev->private; |
| 5343 | if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4 |
| 5344 | > conf->max_nr_stripes || |
| 5345 | ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4 |
| 5346 | > conf->max_nr_stripes) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5347 | printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n", |
| 5348 | mdname(mddev), |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5349 | ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9) |
| 5350 | / STRIPE_SIZE)*4); |
| 5351 | return 0; |
| 5352 | } |
| 5353 | return 1; |
| 5354 | } |
| 5355 | |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5356 | static int check_reshape(mddev_t *mddev) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5357 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 5358 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5359 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5360 | if (mddev->delta_disks == 0 && |
| 5361 | mddev->new_layout == mddev->layout && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5362 | mddev->new_chunk_sectors == mddev->chunk_sectors) |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5363 | return 0; /* nothing to do */ |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5364 | if (mddev->bitmap) |
| 5365 | /* Cannot grow a bitmap yet */ |
| 5366 | return -EBUSY; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5367 | if (mddev->degraded > conf->max_degraded) |
| 5368 | return -EINVAL; |
| 5369 | if (mddev->delta_disks < 0) { |
| 5370 | /* We might be able to shrink, but the devices must |
| 5371 | * be made bigger first. |
| 5372 | * For raid6, 4 is the minimum size. |
| 5373 | * Otherwise 2 is the minimum |
| 5374 | */ |
| 5375 | int min = 2; |
| 5376 | if (mddev->level == 6) |
| 5377 | min = 4; |
| 5378 | if (mddev->raid_disks + mddev->delta_disks < min) |
| 5379 | return -EINVAL; |
| 5380 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5381 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5382 | if (!check_stripe_cache(mddev)) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5383 | return -ENOSPC; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5384 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5385 | return resize_stripes(conf, conf->raid_disks + mddev->delta_disks); |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5386 | } |
| 5387 | |
| 5388 | static int raid5_start_reshape(mddev_t *mddev) |
| 5389 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 5390 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5391 | mdk_rdev_t *rdev; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5392 | int spares = 0; |
| 5393 | int added_devices = 0; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 5394 | unsigned long flags; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5395 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5396 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5397 | return -EBUSY; |
| 5398 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5399 | if (!check_stripe_cache(mddev)) |
| 5400 | return -ENOSPC; |
| 5401 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5402 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5403 | if (rdev->raid_disk < 0 && |
| 5404 | !test_bit(Faulty, &rdev->flags)) |
| 5405 | spares++; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5406 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5407 | if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5408 | /* Not enough devices even to make a degraded array |
| 5409 | * of that size |
| 5410 | */ |
| 5411 | return -EINVAL; |
| 5412 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5413 | /* Refuse to reduce size of the array. Any reductions in |
| 5414 | * array size must be through explicit setting of array_size |
| 5415 | * attribute. |
| 5416 | */ |
| 5417 | if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks) |
| 5418 | < mddev->array_sectors) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5419 | printk(KERN_ERR "md/raid:%s: array size must be reduced " |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5420 | "before number of disks\n", mdname(mddev)); |
| 5421 | return -EINVAL; |
| 5422 | } |
| 5423 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5424 | atomic_set(&conf->reshape_stripes, 0); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5425 | spin_lock_irq(&conf->device_lock); |
| 5426 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5427 | conf->raid_disks += mddev->delta_disks; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5428 | conf->prev_chunk_sectors = conf->chunk_sectors; |
| 5429 | conf->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5430 | conf->prev_algo = conf->algorithm; |
| 5431 | conf->algorithm = mddev->new_layout; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5432 | if (mddev->delta_disks < 0) |
| 5433 | conf->reshape_progress = raid5_size(mddev, 0, 0); |
| 5434 | else |
| 5435 | conf->reshape_progress = 0; |
| 5436 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 5437 | conf->generation++; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5438 | spin_unlock_irq(&conf->device_lock); |
| 5439 | |
| 5440 | /* Add some new drives, as many as will fit. |
| 5441 | * We know there are enough to make the newly sized array work. |
| 5442 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5443 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5444 | if (rdev->raid_disk < 0 && |
| 5445 | !test_bit(Faulty, &rdev->flags)) { |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5446 | if (raid5_add_disk(mddev, rdev) == 0) { |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5447 | char nm[20]; |
NeilBrown | 9eb07c2 | 2010-02-09 12:31:47 +1100 | [diff] [blame] | 5448 | if (rdev->raid_disk >= conf->previous_raid_disks) { |
NeilBrown | 7ef9014 | 2009-11-13 17:40:51 +1100 | [diff] [blame] | 5449 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 9eb07c2 | 2010-02-09 12:31:47 +1100 | [diff] [blame] | 5450 | added_devices++; |
| 5451 | } else |
NeilBrown | 7ef9014 | 2009-11-13 17:40:51 +1100 | [diff] [blame] | 5452 | rdev->recovery_offset = 0; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5453 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5454 | if (sysfs_create_link(&mddev->kobj, |
| 5455 | &rdev->kobj, nm)) |
| 5456 | printk(KERN_WARNING |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5457 | "md/raid:%s: failed to create " |
| 5458 | " link %s\n", |
| 5459 | mdname(mddev), nm); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5460 | } else |
| 5461 | break; |
| 5462 | } |
| 5463 | |
NeilBrown | 9eb07c2 | 2010-02-09 12:31:47 +1100 | [diff] [blame] | 5464 | /* When a reshape changes the number of devices, ->degraded |
| 5465 | * is measured against the large of the pre and post number of |
| 5466 | * devices.*/ |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5467 | if (mddev->delta_disks > 0) { |
| 5468 | spin_lock_irqsave(&conf->device_lock, flags); |
NeilBrown | 9eb07c2 | 2010-02-09 12:31:47 +1100 | [diff] [blame] | 5469 | mddev->degraded += (conf->raid_disks - conf->previous_raid_disks) |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5470 | - added_devices; |
| 5471 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 5472 | } |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5473 | mddev->raid_disks = conf->raid_disks; |
NeilBrown | e516402 | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 5474 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5475 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5476 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5477 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 5478 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 5479 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 5480 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 5481 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 5482 | "reshape"); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5483 | if (!mddev->sync_thread) { |
| 5484 | mddev->recovery = 0; |
| 5485 | spin_lock_irq(&conf->device_lock); |
| 5486 | mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5487 | conf->reshape_progress = MaxSector; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5488 | spin_unlock_irq(&conf->device_lock); |
| 5489 | return -EAGAIN; |
| 5490 | } |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5491 | conf->reshape_checkpoint = jiffies; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5492 | md_wakeup_thread(mddev->sync_thread); |
| 5493 | md_new_event(mddev); |
| 5494 | return 0; |
| 5495 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5496 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5497 | /* This is called from the reshape thread and should make any |
| 5498 | * changes needed in 'conf' |
| 5499 | */ |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5500 | static void end_reshape(raid5_conf_t *conf) |
| 5501 | { |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5502 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5503 | if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) { |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 5504 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5505 | spin_lock_irq(&conf->device_lock); |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5506 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5507 | conf->reshape_progress = MaxSector; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5508 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 5509 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5510 | |
| 5511 | /* read-ahead size must cover two whole stripes, which is |
| 5512 | * 2 * (datadisks) * chunksize where 'n' is the number of raid devices |
| 5513 | */ |
| 5514 | { |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5515 | int data_disks = conf->raid_disks - conf->max_degraded; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5516 | int stripe = data_disks * ((conf->chunk_sectors << 9) |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5517 | / PAGE_SIZE); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5518 | if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 5519 | conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
| 5520 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5521 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5522 | } |
| 5523 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5524 | /* This is called from the raid5d thread with mddev_lock held. |
| 5525 | * It makes config changes to the device. |
| 5526 | */ |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5527 | static void raid5_finish_reshape(mddev_t *mddev) |
| 5528 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 5529 | raid5_conf_t *conf = mddev->private; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5530 | |
| 5531 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 5532 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5533 | if (mddev->delta_disks > 0) { |
| 5534 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
| 5535 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5536 | revalidate_disk(mddev->gendisk); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5537 | } else { |
| 5538 | int d; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5539 | mddev->degraded = conf->raid_disks; |
| 5540 | for (d = 0; d < conf->raid_disks ; d++) |
| 5541 | if (conf->disks[d].rdev && |
| 5542 | test_bit(In_sync, |
| 5543 | &conf->disks[d].rdev->flags)) |
| 5544 | mddev->degraded--; |
| 5545 | for (d = conf->raid_disks ; |
| 5546 | d < conf->raid_disks - mddev->delta_disks; |
NeilBrown | 1a67dde | 2009-08-13 10:41:49 +1000 | [diff] [blame] | 5547 | d++) { |
| 5548 | mdk_rdev_t *rdev = conf->disks[d].rdev; |
| 5549 | if (rdev && raid5_remove_disk(mddev, d) == 0) { |
| 5550 | char nm[20]; |
| 5551 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 5552 | sysfs_remove_link(&mddev->kobj, nm); |
| 5553 | rdev->raid_disk = -1; |
| 5554 | } |
| 5555 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5556 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5557 | mddev->layout = conf->algorithm; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5558 | mddev->chunk_sectors = conf->chunk_sectors; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5559 | mddev->reshape_position = MaxSector; |
| 5560 | mddev->delta_disks = 0; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5561 | } |
| 5562 | } |
| 5563 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5564 | static void raid5_quiesce(mddev_t *mddev, int state) |
| 5565 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 5566 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5567 | |
| 5568 | switch(state) { |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5569 | case 2: /* resume for a suspend */ |
| 5570 | wake_up(&conf->wait_for_overlap); |
| 5571 | break; |
| 5572 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5573 | case 1: /* stop all writes */ |
| 5574 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5575 | /* '2' tells resync/reshape to pause so that all |
| 5576 | * active stripes can drain |
| 5577 | */ |
| 5578 | conf->quiesce = 2; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5579 | wait_event_lock_irq(conf->wait_for_stripe, |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5580 | atomic_read(&conf->active_stripes) == 0 && |
| 5581 | atomic_read(&conf->active_aligned_reads) == 0, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5582 | conf->device_lock, /* nothing */); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5583 | conf->quiesce = 1; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5584 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5585 | /* allow reshape to continue */ |
| 5586 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5587 | break; |
| 5588 | |
| 5589 | case 0: /* re-enable writes */ |
| 5590 | spin_lock_irq(&conf->device_lock); |
| 5591 | conf->quiesce = 0; |
| 5592 | wake_up(&conf->wait_for_stripe); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5593 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5594 | spin_unlock_irq(&conf->device_lock); |
| 5595 | break; |
| 5596 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5597 | } |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5598 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5599 | |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5600 | static void *raid45_takeover_raid0(mddev_t *mddev, int level) |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 5601 | { |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5602 | struct raid0_private_data *raid0_priv = mddev->private; |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 5603 | |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5604 | /* for raid0 takeover only one zone is supported */ |
| 5605 | if (raid0_priv->nr_strip_zones > 1) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5606 | printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n", |
| 5607 | mdname(mddev)); |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5608 | return ERR_PTR(-EINVAL); |
| 5609 | } |
| 5610 | |
| 5611 | mddev->new_level = level; |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 5612 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 5613 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
| 5614 | mddev->raid_disks += 1; |
| 5615 | mddev->delta_disks = 1; |
| 5616 | /* make sure it will be not marked as dirty */ |
| 5617 | mddev->recovery_cp = MaxSector; |
| 5618 | |
| 5619 | return setup_conf(mddev); |
| 5620 | } |
| 5621 | |
| 5622 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5623 | static void *raid5_takeover_raid1(mddev_t *mddev) |
| 5624 | { |
| 5625 | int chunksect; |
| 5626 | |
| 5627 | if (mddev->raid_disks != 2 || |
| 5628 | mddev->degraded > 1) |
| 5629 | return ERR_PTR(-EINVAL); |
| 5630 | |
| 5631 | /* Should check if there are write-behind devices? */ |
| 5632 | |
| 5633 | chunksect = 64*2; /* 64K by default */ |
| 5634 | |
| 5635 | /* The array must be an exact multiple of chunksize */ |
| 5636 | while (chunksect && (mddev->array_sectors & (chunksect-1))) |
| 5637 | chunksect >>= 1; |
| 5638 | |
| 5639 | if ((chunksect<<9) < STRIPE_SIZE) |
| 5640 | /* array size does not allow a suitable chunk size */ |
| 5641 | return ERR_PTR(-EINVAL); |
| 5642 | |
| 5643 | mddev->new_level = 5; |
| 5644 | mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5645 | mddev->new_chunk_sectors = chunksect; |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5646 | |
| 5647 | return setup_conf(mddev); |
| 5648 | } |
| 5649 | |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 5650 | static void *raid5_takeover_raid6(mddev_t *mddev) |
| 5651 | { |
| 5652 | int new_layout; |
| 5653 | |
| 5654 | switch (mddev->layout) { |
| 5655 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 5656 | new_layout = ALGORITHM_LEFT_ASYMMETRIC; |
| 5657 | break; |
| 5658 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 5659 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC; |
| 5660 | break; |
| 5661 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 5662 | new_layout = ALGORITHM_LEFT_SYMMETRIC; |
| 5663 | break; |
| 5664 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 5665 | new_layout = ALGORITHM_RIGHT_SYMMETRIC; |
| 5666 | break; |
| 5667 | case ALGORITHM_PARITY_0_6: |
| 5668 | new_layout = ALGORITHM_PARITY_0; |
| 5669 | break; |
| 5670 | case ALGORITHM_PARITY_N: |
| 5671 | new_layout = ALGORITHM_PARITY_N; |
| 5672 | break; |
| 5673 | default: |
| 5674 | return ERR_PTR(-EINVAL); |
| 5675 | } |
| 5676 | mddev->new_level = 5; |
| 5677 | mddev->new_layout = new_layout; |
| 5678 | mddev->delta_disks = -1; |
| 5679 | mddev->raid_disks -= 1; |
| 5680 | return setup_conf(mddev); |
| 5681 | } |
| 5682 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5683 | |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5684 | static int raid5_check_reshape(mddev_t *mddev) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5685 | { |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5686 | /* For a 2-drive array, the layout and chunk size can be changed |
| 5687 | * immediately as not restriping is needed. |
| 5688 | * For larger arrays we record the new value - after validation |
| 5689 | * to be used by a reshape pass. |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5690 | */ |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 5691 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5692 | int new_chunk = mddev->new_chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5693 | |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5694 | if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5695 | return -EINVAL; |
| 5696 | if (new_chunk > 0) { |
Andre Noll | 0ba459d | 2009-06-18 08:46:10 +1000 | [diff] [blame] | 5697 | if (!is_power_of_2(new_chunk)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5698 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5699 | if (new_chunk < (PAGE_SIZE>>9)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5700 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5701 | if (mddev->array_sectors & (new_chunk-1)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5702 | /* not factor of array size */ |
| 5703 | return -EINVAL; |
| 5704 | } |
| 5705 | |
| 5706 | /* They look valid */ |
| 5707 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5708 | if (mddev->raid_disks == 2) { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5709 | /* can make the change immediately */ |
| 5710 | if (mddev->new_layout >= 0) { |
| 5711 | conf->algorithm = mddev->new_layout; |
| 5712 | mddev->layout = mddev->new_layout; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5713 | } |
| 5714 | if (new_chunk > 0) { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5715 | conf->chunk_sectors = new_chunk ; |
| 5716 | mddev->chunk_sectors = new_chunk; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5717 | } |
| 5718 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 5719 | md_wakeup_thread(mddev->thread); |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5720 | } |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5721 | return check_reshape(mddev); |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5722 | } |
| 5723 | |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5724 | static int raid6_check_reshape(mddev_t *mddev) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5725 | { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5726 | int new_chunk = mddev->new_chunk_sectors; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5727 | |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5728 | if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5729 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5730 | if (new_chunk > 0) { |
Andre Noll | 0ba459d | 2009-06-18 08:46:10 +1000 | [diff] [blame] | 5731 | if (!is_power_of_2(new_chunk)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5732 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5733 | if (new_chunk < (PAGE_SIZE >> 9)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5734 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5735 | if (mddev->array_sectors & (new_chunk-1)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5736 | /* not factor of array size */ |
| 5737 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5738 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5739 | |
| 5740 | /* They look valid */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5741 | return check_reshape(mddev); |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5742 | } |
| 5743 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5744 | static void *raid5_takeover(mddev_t *mddev) |
| 5745 | { |
| 5746 | /* raid5 can take over: |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5747 | * raid0 - if there is only one strip zone - make it a raid4 layout |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5748 | * raid1 - if there are two drives. We need to know the chunk size |
| 5749 | * raid4 - trivial - just use a raid4 layout. |
| 5750 | * raid6 - Providing it is a *_6 layout |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5751 | */ |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5752 | if (mddev->level == 0) |
| 5753 | return raid45_takeover_raid0(mddev, 5); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5754 | if (mddev->level == 1) |
| 5755 | return raid5_takeover_raid1(mddev); |
NeilBrown | e9d4758 | 2009-03-31 14:57:09 +1100 | [diff] [blame] | 5756 | if (mddev->level == 4) { |
| 5757 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 5758 | mddev->new_level = 5; |
| 5759 | return setup_conf(mddev); |
| 5760 | } |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 5761 | if (mddev->level == 6) |
| 5762 | return raid5_takeover_raid6(mddev); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5763 | |
| 5764 | return ERR_PTR(-EINVAL); |
| 5765 | } |
| 5766 | |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 5767 | static void *raid4_takeover(mddev_t *mddev) |
| 5768 | { |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5769 | /* raid4 can take over: |
| 5770 | * raid0 - if there is only one strip zone |
| 5771 | * raid5 - if layout is right |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 5772 | */ |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5773 | if (mddev->level == 0) |
| 5774 | return raid45_takeover_raid0(mddev, 4); |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 5775 | if (mddev->level == 5 && |
| 5776 | mddev->layout == ALGORITHM_PARITY_N) { |
| 5777 | mddev->new_layout = 0; |
| 5778 | mddev->new_level = 4; |
| 5779 | return setup_conf(mddev); |
| 5780 | } |
| 5781 | return ERR_PTR(-EINVAL); |
| 5782 | } |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5783 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5784 | static struct mdk_personality raid5_personality; |
| 5785 | |
| 5786 | static void *raid6_takeover(mddev_t *mddev) |
| 5787 | { |
| 5788 | /* Currently can only take over a raid5. We map the |
| 5789 | * personality to an equivalent raid6 personality |
| 5790 | * with the Q block at the end. |
| 5791 | */ |
| 5792 | int new_layout; |
| 5793 | |
| 5794 | if (mddev->pers != &raid5_personality) |
| 5795 | return ERR_PTR(-EINVAL); |
| 5796 | if (mddev->degraded > 1) |
| 5797 | return ERR_PTR(-EINVAL); |
| 5798 | if (mddev->raid_disks > 253) |
| 5799 | return ERR_PTR(-EINVAL); |
| 5800 | if (mddev->raid_disks < 3) |
| 5801 | return ERR_PTR(-EINVAL); |
| 5802 | |
| 5803 | switch (mddev->layout) { |
| 5804 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 5805 | new_layout = ALGORITHM_LEFT_ASYMMETRIC_6; |
| 5806 | break; |
| 5807 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 5808 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6; |
| 5809 | break; |
| 5810 | case ALGORITHM_LEFT_SYMMETRIC: |
| 5811 | new_layout = ALGORITHM_LEFT_SYMMETRIC_6; |
| 5812 | break; |
| 5813 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 5814 | new_layout = ALGORITHM_RIGHT_SYMMETRIC_6; |
| 5815 | break; |
| 5816 | case ALGORITHM_PARITY_0: |
| 5817 | new_layout = ALGORITHM_PARITY_0_6; |
| 5818 | break; |
| 5819 | case ALGORITHM_PARITY_N: |
| 5820 | new_layout = ALGORITHM_PARITY_N; |
| 5821 | break; |
| 5822 | default: |
| 5823 | return ERR_PTR(-EINVAL); |
| 5824 | } |
| 5825 | mddev->new_level = 6; |
| 5826 | mddev->new_layout = new_layout; |
| 5827 | mddev->delta_disks = 1; |
| 5828 | mddev->raid_disks += 1; |
| 5829 | return setup_conf(mddev); |
| 5830 | } |
| 5831 | |
| 5832 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5833 | static struct mdk_personality raid6_personality = |
| 5834 | { |
| 5835 | .name = "raid6", |
| 5836 | .level = 6, |
| 5837 | .owner = THIS_MODULE, |
| 5838 | .make_request = make_request, |
| 5839 | .run = run, |
| 5840 | .stop = stop, |
| 5841 | .status = status, |
| 5842 | .error_handler = error, |
| 5843 | .hot_add_disk = raid5_add_disk, |
| 5844 | .hot_remove_disk= raid5_remove_disk, |
| 5845 | .spare_active = raid5_spare_active, |
| 5846 | .sync_request = sync_request, |
| 5847 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 5848 | .size = raid5_size, |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5849 | .check_reshape = raid6_check_reshape, |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5850 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5851 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5852 | .quiesce = raid5_quiesce, |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5853 | .takeover = raid6_takeover, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5854 | }; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5855 | static struct mdk_personality raid5_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5856 | { |
| 5857 | .name = "raid5", |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5858 | .level = 5, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5859 | .owner = THIS_MODULE, |
| 5860 | .make_request = make_request, |
| 5861 | .run = run, |
| 5862 | .stop = stop, |
| 5863 | .status = status, |
| 5864 | .error_handler = error, |
| 5865 | .hot_add_disk = raid5_add_disk, |
| 5866 | .hot_remove_disk= raid5_remove_disk, |
| 5867 | .spare_active = raid5_spare_active, |
| 5868 | .sync_request = sync_request, |
| 5869 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 5870 | .size = raid5_size, |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5871 | .check_reshape = raid5_check_reshape, |
| 5872 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5873 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5874 | .quiesce = raid5_quiesce, |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5875 | .takeover = raid5_takeover, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5876 | }; |
| 5877 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5878 | static struct mdk_personality raid4_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5879 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5880 | .name = "raid4", |
| 5881 | .level = 4, |
| 5882 | .owner = THIS_MODULE, |
| 5883 | .make_request = make_request, |
| 5884 | .run = run, |
| 5885 | .stop = stop, |
| 5886 | .status = status, |
| 5887 | .error_handler = error, |
| 5888 | .hot_add_disk = raid5_add_disk, |
| 5889 | .hot_remove_disk= raid5_remove_disk, |
| 5890 | .spare_active = raid5_spare_active, |
| 5891 | .sync_request = sync_request, |
| 5892 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 5893 | .size = raid5_size, |
NeilBrown | 3d37890 | 2007-03-26 21:32:13 -0800 | [diff] [blame] | 5894 | .check_reshape = raid5_check_reshape, |
| 5895 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5896 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5897 | .quiesce = raid5_quiesce, |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 5898 | .takeover = raid4_takeover, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5899 | }; |
| 5900 | |
| 5901 | static int __init raid5_init(void) |
| 5902 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5903 | register_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5904 | register_md_personality(&raid5_personality); |
| 5905 | register_md_personality(&raid4_personality); |
| 5906 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5907 | } |
| 5908 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5909 | static void raid5_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5910 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5911 | unregister_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5912 | unregister_md_personality(&raid5_personality); |
| 5913 | unregister_md_personality(&raid4_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5914 | } |
| 5915 | |
| 5916 | module_init(raid5_init); |
| 5917 | module_exit(raid5_exit); |
| 5918 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 5919 | MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5920 | MODULE_ALIAS("md-personality-4"); /* RAID5 */ |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5921 | MODULE_ALIAS("md-raid5"); |
| 5922 | MODULE_ALIAS("md-raid4"); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5923 | MODULE_ALIAS("md-level-5"); |
| 5924 | MODULE_ALIAS("md-level-4"); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5925 | MODULE_ALIAS("md-personality-8"); /* RAID6 */ |
| 5926 | MODULE_ALIAS("md-raid6"); |
| 5927 | MODULE_ALIAS("md-level-6"); |
| 5928 | |
| 5929 | /* This used to be two separate modules, they were: */ |
| 5930 | MODULE_ALIAS("raid5"); |
| 5931 | MODULE_ALIAS("raid6"); |