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 | |
| 280 | static void shrink_buffers(struct stripe_head *sh, int num) |
| 281 | { |
| 282 | struct page *p; |
| 283 | int i; |
| 284 | |
| 285 | for (i=0; i<num ; i++) { |
| 286 | p = sh->dev[i].page; |
| 287 | if (!p) |
| 288 | continue; |
| 289 | sh->dev[i].page = NULL; |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 290 | put_page(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
| 294 | static int grow_buffers(struct stripe_head *sh, int num) |
| 295 | { |
| 296 | int i; |
| 297 | |
| 298 | for (i=0; i<num; i++) { |
| 299 | struct page *page; |
| 300 | |
| 301 | if (!(page = alloc_page(GFP_KERNEL))) { |
| 302 | return 1; |
| 303 | } |
| 304 | sh->dev[i].page = page; |
| 305 | } |
| 306 | return 0; |
| 307 | } |
| 308 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 309 | static void raid5_build_block(struct stripe_head *sh, int i, int previous); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 310 | static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous, |
| 311 | struct stripe_head *sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 313 | 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] | 314 | { |
| 315 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 316 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 318 | BUG_ON(atomic_read(&sh->count) != 0); |
| 319 | BUG_ON(test_bit(STRIPE_HANDLE, &sh->state)); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 320 | BUG_ON(stripe_operations_active(sh)); |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 321 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | CHECK_DEVLOCK(); |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 323 | pr_debug("init_stripe called, stripe %llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | (unsigned long long)sh->sector); |
| 325 | |
| 326 | remove_hash(sh); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 327 | |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 328 | sh->generation = conf->generation - previous; |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 329 | sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | sh->sector = sector; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 331 | stripe_set_idx(sector, conf, previous, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | sh->state = 0; |
| 333 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 334 | |
| 335 | for (i = sh->disks; i--; ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | struct r5dev *dev = &sh->dev[i]; |
| 337 | |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 338 | if (dev->toread || dev->read || dev->towrite || dev->written || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | test_bit(R5_LOCKED, &dev->flags)) { |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 340 | 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] | 341 | (unsigned long long)sh->sector, i, dev->toread, |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 342 | dev->read, dev->towrite, dev->written, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | test_bit(R5_LOCKED, &dev->flags)); |
| 344 | BUG(); |
| 345 | } |
| 346 | dev->flags = 0; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 347 | raid5_build_block(sh, i, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | } |
| 349 | insert_hash(conf, sh); |
| 350 | } |
| 351 | |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 352 | static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector, |
| 353 | short generation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | { |
| 355 | struct stripe_head *sh; |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 356 | struct hlist_node *hn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
| 358 | CHECK_DEVLOCK(); |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 359 | pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector); |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 360 | hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash) |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 361 | if (sh->sector == sector && sh->generation == generation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | return sh; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 363 | pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | return NULL; |
| 365 | } |
| 366 | |
| 367 | static void unplug_slaves(mddev_t *mddev); |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 368 | static void raid5_unplug_device(struct request_queue *q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 370 | static struct stripe_head * |
| 371 | get_active_stripe(raid5_conf_t *conf, sector_t sector, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 372 | int previous, int noblock, int noquiesce) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | { |
| 374 | struct stripe_head *sh; |
| 375 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 376 | pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | |
| 378 | spin_lock_irq(&conf->device_lock); |
| 379 | |
| 380 | do { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 381 | wait_event_lock_irq(conf->wait_for_stripe, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 382 | conf->quiesce == 0 || noquiesce, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 383 | conf->device_lock, /* nothing */); |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 384 | sh = __find_stripe(conf, sector, conf->generation - previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | if (!sh) { |
| 386 | if (!conf->inactive_blocked) |
| 387 | sh = get_free_stripe(conf); |
| 388 | if (noblock && sh == NULL) |
| 389 | break; |
| 390 | if (!sh) { |
| 391 | conf->inactive_blocked = 1; |
| 392 | wait_event_lock_irq(conf->wait_for_stripe, |
| 393 | !list_empty(&conf->inactive_list) && |
NeilBrown | 5036805 | 2005-12-12 02:39:17 -0800 | [diff] [blame] | 394 | (atomic_read(&conf->active_stripes) |
| 395 | < (conf->max_nr_stripes *3/4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | || !conf->inactive_blocked), |
| 397 | conf->device_lock, |
NeilBrown | f437078 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 398 | raid5_unplug_device(conf->mddev->queue) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | ); |
| 400 | conf->inactive_blocked = 0; |
| 401 | } else |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 402 | init_stripe(sh, sector, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | } else { |
| 404 | if (atomic_read(&sh->count)) { |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 405 | BUG_ON(!list_empty(&sh->lru) |
| 406 | && !test_bit(STRIPE_EXPANDING, &sh->state)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | } else { |
| 408 | if (!test_bit(STRIPE_HANDLE, &sh->state)) |
| 409 | atomic_inc(&conf->active_stripes); |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 410 | if (list_empty(&sh->lru) && |
| 411 | !test_bit(STRIPE_EXPANDING, &sh->state)) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 412 | BUG(); |
| 413 | list_del_init(&sh->lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | } while (sh == NULL); |
| 417 | |
| 418 | if (sh) |
| 419 | atomic_inc(&sh->count); |
| 420 | |
| 421 | spin_unlock_irq(&conf->device_lock); |
| 422 | return sh; |
| 423 | } |
| 424 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 425 | static void |
| 426 | raid5_end_read_request(struct bio *bi, int error); |
| 427 | static void |
| 428 | raid5_end_write_request(struct bio *bi, int error); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 429 | |
Dan Williams | c4e5ac0 | 2008-06-28 08:31:53 +1000 | [diff] [blame] | 430 | 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] | 431 | { |
| 432 | raid5_conf_t *conf = sh->raid_conf; |
| 433 | int i, disks = sh->disks; |
| 434 | |
| 435 | might_sleep(); |
| 436 | |
| 437 | for (i = disks; i--; ) { |
| 438 | int rw; |
| 439 | struct bio *bi; |
| 440 | mdk_rdev_t *rdev; |
| 441 | if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) |
| 442 | rw = WRITE; |
| 443 | else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags)) |
| 444 | rw = READ; |
| 445 | else |
| 446 | continue; |
| 447 | |
| 448 | bi = &sh->dev[i].req; |
| 449 | |
| 450 | bi->bi_rw = rw; |
| 451 | if (rw == WRITE) |
| 452 | bi->bi_end_io = raid5_end_write_request; |
| 453 | else |
| 454 | bi->bi_end_io = raid5_end_read_request; |
| 455 | |
| 456 | rcu_read_lock(); |
| 457 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 458 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 459 | rdev = NULL; |
| 460 | if (rdev) |
| 461 | atomic_inc(&rdev->nr_pending); |
| 462 | rcu_read_unlock(); |
| 463 | |
| 464 | if (rdev) { |
Dan Williams | c4e5ac0 | 2008-06-28 08:31:53 +1000 | [diff] [blame] | 465 | if (s->syncing || s->expanding || s->expanded) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 466 | md_sync_acct(rdev->bdev, STRIPE_SECTORS); |
| 467 | |
Dan Williams | 2b7497f | 2008-06-28 08:31:52 +1000 | [diff] [blame] | 468 | set_bit(STRIPE_IO_STARTED, &sh->state); |
| 469 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 470 | bi->bi_bdev = rdev->bdev; |
| 471 | pr_debug("%s: for %llu schedule op %ld on disc %d\n", |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 472 | __func__, (unsigned long long)sh->sector, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 473 | bi->bi_rw, i); |
| 474 | atomic_inc(&sh->count); |
| 475 | bi->bi_sector = sh->sector + rdev->data_offset; |
| 476 | bi->bi_flags = 1 << BIO_UPTODATE; |
| 477 | bi->bi_vcnt = 1; |
| 478 | bi->bi_max_vecs = 1; |
| 479 | bi->bi_idx = 0; |
| 480 | bi->bi_io_vec = &sh->dev[i].vec; |
| 481 | bi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
| 482 | bi->bi_io_vec[0].bv_offset = 0; |
| 483 | bi->bi_size = STRIPE_SIZE; |
| 484 | bi->bi_next = NULL; |
| 485 | if (rw == WRITE && |
| 486 | test_bit(R5_ReWrite, &sh->dev[i].flags)) |
| 487 | atomic_add(STRIPE_SECTORS, |
| 488 | &rdev->corrected_errors); |
| 489 | generic_make_request(bi); |
| 490 | } else { |
| 491 | if (rw == WRITE) |
| 492 | set_bit(STRIPE_DEGRADED, &sh->state); |
| 493 | pr_debug("skip op %ld on disc %d for sector %llu\n", |
| 494 | bi->bi_rw, i, (unsigned long long)sh->sector); |
| 495 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 496 | set_bit(STRIPE_HANDLE, &sh->state); |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | static struct dma_async_tx_descriptor * |
| 502 | async_copy_data(int frombio, struct bio *bio, struct page *page, |
| 503 | sector_t sector, struct dma_async_tx_descriptor *tx) |
| 504 | { |
| 505 | struct bio_vec *bvl; |
| 506 | struct page *bio_page; |
| 507 | int i; |
| 508 | int page_offset; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 509 | struct async_submit_ctl submit; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 510 | enum async_tx_flags flags = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 511 | |
| 512 | if (bio->bi_sector >= sector) |
| 513 | page_offset = (signed)(bio->bi_sector - sector) * 512; |
| 514 | else |
| 515 | page_offset = (signed)(sector - bio->bi_sector) * -512; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 516 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 517 | if (frombio) |
| 518 | flags |= ASYNC_TX_FENCE; |
| 519 | init_async_submit(&submit, flags, tx, NULL, NULL, NULL); |
| 520 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 521 | bio_for_each_segment(bvl, bio, i) { |
| 522 | int len = bio_iovec_idx(bio, i)->bv_len; |
| 523 | int clen; |
| 524 | int b_offset = 0; |
| 525 | |
| 526 | if (page_offset < 0) { |
| 527 | b_offset = -page_offset; |
| 528 | page_offset += b_offset; |
| 529 | len -= b_offset; |
| 530 | } |
| 531 | |
| 532 | if (len > 0 && page_offset + len > STRIPE_SIZE) |
| 533 | clen = STRIPE_SIZE - page_offset; |
| 534 | else |
| 535 | clen = len; |
| 536 | |
| 537 | if (clen > 0) { |
| 538 | b_offset += bio_iovec_idx(bio, i)->bv_offset; |
| 539 | bio_page = bio_iovec_idx(bio, i)->bv_page; |
| 540 | if (frombio) |
| 541 | tx = async_memcpy(page, bio_page, page_offset, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 542 | b_offset, clen, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 543 | else |
| 544 | tx = async_memcpy(bio_page, page, b_offset, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 545 | page_offset, clen, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 546 | } |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 547 | /* chain the operations */ |
| 548 | submit.depend_tx = tx; |
| 549 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 550 | if (clen < len) /* hit end of page */ |
| 551 | break; |
| 552 | page_offset += len; |
| 553 | } |
| 554 | |
| 555 | return tx; |
| 556 | } |
| 557 | |
| 558 | static void ops_complete_biofill(void *stripe_head_ref) |
| 559 | { |
| 560 | struct stripe_head *sh = stripe_head_ref; |
| 561 | struct bio *return_bi = NULL; |
| 562 | raid5_conf_t *conf = sh->raid_conf; |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 563 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 564 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 565 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 566 | (unsigned long long)sh->sector); |
| 567 | |
| 568 | /* clear completed biofills */ |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 569 | spin_lock_irq(&conf->device_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 570 | for (i = sh->disks; i--; ) { |
| 571 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 572 | |
| 573 | /* acknowledge completion of a biofill operation */ |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 574 | /* and check if we need to reply to a read request, |
| 575 | * new R5_Wantfill requests are held off until |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 576 | * !STRIPE_BIOFILL_RUN |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 577 | */ |
| 578 | if (test_and_clear_bit(R5_Wantfill, &dev->flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 579 | struct bio *rbi, *rbi2; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 580 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 581 | BUG_ON(!dev->read); |
| 582 | rbi = dev->read; |
| 583 | dev->read = NULL; |
| 584 | while (rbi && rbi->bi_sector < |
| 585 | dev->sector + STRIPE_SECTORS) { |
| 586 | rbi2 = r5_next_bio(rbi, dev->sector); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 587 | if (!raid5_dec_bi_phys_segments(rbi)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 588 | rbi->bi_next = return_bi; |
| 589 | return_bi = rbi; |
| 590 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 591 | rbi = rbi2; |
| 592 | } |
| 593 | } |
| 594 | } |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 595 | spin_unlock_irq(&conf->device_lock); |
| 596 | clear_bit(STRIPE_BIOFILL_RUN, &sh->state); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 597 | |
| 598 | return_io(return_bi); |
| 599 | |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 600 | set_bit(STRIPE_HANDLE, &sh->state); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 601 | release_stripe(sh); |
| 602 | } |
| 603 | |
| 604 | static void ops_run_biofill(struct stripe_head *sh) |
| 605 | { |
| 606 | struct dma_async_tx_descriptor *tx = NULL; |
| 607 | raid5_conf_t *conf = sh->raid_conf; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 608 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 609 | int i; |
| 610 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 611 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 612 | (unsigned long long)sh->sector); |
| 613 | |
| 614 | for (i = sh->disks; i--; ) { |
| 615 | struct r5dev *dev = &sh->dev[i]; |
| 616 | if (test_bit(R5_Wantfill, &dev->flags)) { |
| 617 | struct bio *rbi; |
| 618 | spin_lock_irq(&conf->device_lock); |
| 619 | dev->read = rbi = dev->toread; |
| 620 | dev->toread = NULL; |
| 621 | spin_unlock_irq(&conf->device_lock); |
| 622 | while (rbi && rbi->bi_sector < |
| 623 | dev->sector + STRIPE_SECTORS) { |
| 624 | tx = async_copy_data(0, rbi, dev->page, |
| 625 | dev->sector, tx); |
| 626 | rbi = r5_next_bio(rbi, dev->sector); |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 632 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL); |
| 633 | async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 634 | } |
| 635 | |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 636 | static void mark_target_uptodate(struct stripe_head *sh, int target) |
| 637 | { |
| 638 | struct r5dev *tgt; |
| 639 | |
| 640 | if (target < 0) |
| 641 | return; |
| 642 | |
| 643 | tgt = &sh->dev[target]; |
| 644 | set_bit(R5_UPTODATE, &tgt->flags); |
| 645 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 646 | clear_bit(R5_Wantcompute, &tgt->flags); |
| 647 | } |
| 648 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 649 | static void ops_complete_compute(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 650 | { |
| 651 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 652 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 653 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 654 | (unsigned long long)sh->sector); |
| 655 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 656 | /* mark the computed target(s) as uptodate */ |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 657 | mark_target_uptodate(sh, sh->ops.target); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 658 | mark_target_uptodate(sh, sh->ops.target2); |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 659 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 660 | clear_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 661 | if (sh->check_state == check_state_compute_run) |
| 662 | sh->check_state = check_state_compute_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 663 | set_bit(STRIPE_HANDLE, &sh->state); |
| 664 | release_stripe(sh); |
| 665 | } |
| 666 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 667 | /* return a pointer to the address conversion region of the scribble buffer */ |
| 668 | static addr_conv_t *to_addr_conv(struct stripe_head *sh, |
| 669 | struct raid5_percpu *percpu) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 670 | { |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 671 | return percpu->scribble + sizeof(struct page *) * (sh->disks + 2); |
| 672 | } |
| 673 | |
| 674 | static struct dma_async_tx_descriptor * |
| 675 | ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 676 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 677 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 678 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 679 | int target = sh->ops.target; |
| 680 | struct r5dev *tgt = &sh->dev[target]; |
| 681 | struct page *xor_dest = tgt->page; |
| 682 | int count = 0; |
| 683 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 684 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 685 | int i; |
| 686 | |
| 687 | pr_debug("%s: stripe %llu block: %d\n", |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 688 | __func__, (unsigned long long)sh->sector, target); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 689 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 690 | |
| 691 | for (i = disks; i--; ) |
| 692 | if (i != target) |
| 693 | xor_srcs[count++] = sh->dev[i].page; |
| 694 | |
| 695 | atomic_inc(&sh->count); |
| 696 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 697 | 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] | 698 | ops_complete_compute, sh, to_addr_conv(sh, percpu)); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 699 | if (unlikely(count == 1)) |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 700 | 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] | 701 | else |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 702 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 703 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 704 | return tx; |
| 705 | } |
| 706 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 707 | /* set_syndrome_sources - populate source buffers for gen_syndrome |
| 708 | * @srcs - (struct page *) array of size sh->disks |
| 709 | * @sh - stripe_head to parse |
| 710 | * |
| 711 | * Populates srcs in proper layout order for the stripe and returns the |
| 712 | * 'count' of sources to be used in a call to async_gen_syndrome. The P |
| 713 | * destination buffer is recorded in srcs[count] and the Q destination |
| 714 | * is recorded in srcs[count+1]]. |
| 715 | */ |
| 716 | static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh) |
| 717 | { |
| 718 | int disks = sh->disks; |
| 719 | int syndrome_disks = sh->ddf_layout ? disks : (disks - 2); |
| 720 | int d0_idx = raid6_d0(sh); |
| 721 | int count; |
| 722 | int i; |
| 723 | |
| 724 | for (i = 0; i < disks; i++) |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 725 | srcs[i] = NULL; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 726 | |
| 727 | count = 0; |
| 728 | i = d0_idx; |
| 729 | do { |
| 730 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 731 | |
| 732 | srcs[slot] = sh->dev[i].page; |
| 733 | i = raid6_next_disk(i, disks); |
| 734 | } while (i != d0_idx); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 735 | |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 736 | return syndrome_disks; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | static struct dma_async_tx_descriptor * |
| 740 | ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 741 | { |
| 742 | int disks = sh->disks; |
| 743 | struct page **blocks = percpu->scribble; |
| 744 | int target; |
| 745 | int qd_idx = sh->qd_idx; |
| 746 | struct dma_async_tx_descriptor *tx; |
| 747 | struct async_submit_ctl submit; |
| 748 | struct r5dev *tgt; |
| 749 | struct page *dest; |
| 750 | int i; |
| 751 | int count; |
| 752 | |
| 753 | if (sh->ops.target < 0) |
| 754 | target = sh->ops.target2; |
| 755 | else if (sh->ops.target2 < 0) |
| 756 | target = sh->ops.target; |
| 757 | else |
| 758 | /* we should only have one valid target */ |
| 759 | BUG(); |
| 760 | BUG_ON(target < 0); |
| 761 | pr_debug("%s: stripe %llu block: %d\n", |
| 762 | __func__, (unsigned long long)sh->sector, target); |
| 763 | |
| 764 | tgt = &sh->dev[target]; |
| 765 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 766 | dest = tgt->page; |
| 767 | |
| 768 | atomic_inc(&sh->count); |
| 769 | |
| 770 | if (target == qd_idx) { |
| 771 | count = set_syndrome_sources(blocks, sh); |
| 772 | blocks[count] = NULL; /* regenerating p is not necessary */ |
| 773 | BUG_ON(blocks[count+1] != dest); /* q should already be set */ |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 774 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 775 | ops_complete_compute, sh, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 776 | to_addr_conv(sh, percpu)); |
| 777 | tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
| 778 | } else { |
| 779 | /* Compute any data- or p-drive using XOR */ |
| 780 | count = 0; |
| 781 | for (i = disks; i-- ; ) { |
| 782 | if (i == target || i == qd_idx) |
| 783 | continue; |
| 784 | blocks[count++] = sh->dev[i].page; |
| 785 | } |
| 786 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 787 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
| 788 | NULL, ops_complete_compute, sh, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 789 | to_addr_conv(sh, percpu)); |
| 790 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit); |
| 791 | } |
| 792 | |
| 793 | return tx; |
| 794 | } |
| 795 | |
| 796 | static struct dma_async_tx_descriptor * |
| 797 | ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 798 | { |
| 799 | int i, count, disks = sh->disks; |
| 800 | int syndrome_disks = sh->ddf_layout ? disks : disks-2; |
| 801 | int d0_idx = raid6_d0(sh); |
| 802 | int faila = -1, failb = -1; |
| 803 | int target = sh->ops.target; |
| 804 | int target2 = sh->ops.target2; |
| 805 | struct r5dev *tgt = &sh->dev[target]; |
| 806 | struct r5dev *tgt2 = &sh->dev[target2]; |
| 807 | struct dma_async_tx_descriptor *tx; |
| 808 | struct page **blocks = percpu->scribble; |
| 809 | struct async_submit_ctl submit; |
| 810 | |
| 811 | pr_debug("%s: stripe %llu block1: %d block2: %d\n", |
| 812 | __func__, (unsigned long long)sh->sector, target, target2); |
| 813 | BUG_ON(target < 0 || target2 < 0); |
| 814 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 815 | BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags)); |
| 816 | |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 817 | /* we need to open-code set_syndrome_sources to handle the |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 818 | * slot number conversion for 'faila' and 'failb' |
| 819 | */ |
| 820 | for (i = 0; i < disks ; i++) |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 821 | blocks[i] = NULL; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 822 | count = 0; |
| 823 | i = d0_idx; |
| 824 | do { |
| 825 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 826 | |
| 827 | blocks[slot] = sh->dev[i].page; |
| 828 | |
| 829 | if (i == target) |
| 830 | faila = slot; |
| 831 | if (i == target2) |
| 832 | failb = slot; |
| 833 | i = raid6_next_disk(i, disks); |
| 834 | } while (i != d0_idx); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 835 | |
| 836 | BUG_ON(faila == failb); |
| 837 | if (failb < faila) |
| 838 | swap(faila, failb); |
| 839 | pr_debug("%s: stripe: %llu faila: %d failb: %d\n", |
| 840 | __func__, (unsigned long long)sh->sector, faila, failb); |
| 841 | |
| 842 | atomic_inc(&sh->count); |
| 843 | |
| 844 | if (failb == syndrome_disks+1) { |
| 845 | /* Q disk is one of the missing disks */ |
| 846 | if (faila == syndrome_disks) { |
| 847 | /* Missing P+Q, just recompute */ |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 848 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 849 | ops_complete_compute, sh, |
| 850 | to_addr_conv(sh, percpu)); |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 851 | return async_gen_syndrome(blocks, 0, syndrome_disks+2, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 852 | STRIPE_SIZE, &submit); |
| 853 | } else { |
| 854 | struct page *dest; |
| 855 | int data_target; |
| 856 | int qd_idx = sh->qd_idx; |
| 857 | |
| 858 | /* Missing D+Q: recompute D from P, then recompute Q */ |
| 859 | if (target == qd_idx) |
| 860 | data_target = target2; |
| 861 | else |
| 862 | data_target = target; |
| 863 | |
| 864 | count = 0; |
| 865 | for (i = disks; i-- ; ) { |
| 866 | if (i == data_target || i == qd_idx) |
| 867 | continue; |
| 868 | blocks[count++] = sh->dev[i].page; |
| 869 | } |
| 870 | dest = sh->dev[data_target].page; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 871 | init_async_submit(&submit, |
| 872 | ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
| 873 | NULL, NULL, NULL, |
| 874 | to_addr_conv(sh, percpu)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 875 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, |
| 876 | &submit); |
| 877 | |
| 878 | count = set_syndrome_sources(blocks, sh); |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 879 | init_async_submit(&submit, ASYNC_TX_FENCE, tx, |
| 880 | ops_complete_compute, sh, |
| 881 | to_addr_conv(sh, percpu)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 882 | return async_gen_syndrome(blocks, 0, count+2, |
| 883 | STRIPE_SIZE, &submit); |
| 884 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 885 | } else { |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 886 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 887 | ops_complete_compute, sh, |
| 888 | to_addr_conv(sh, percpu)); |
| 889 | if (failb == syndrome_disks) { |
| 890 | /* We're missing D+P. */ |
| 891 | return async_raid6_datap_recov(syndrome_disks+2, |
| 892 | STRIPE_SIZE, faila, |
| 893 | blocks, &submit); |
| 894 | } else { |
| 895 | /* We're missing D+D. */ |
| 896 | return async_raid6_2data_recov(syndrome_disks+2, |
| 897 | STRIPE_SIZE, faila, failb, |
| 898 | blocks, &submit); |
| 899 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 900 | } |
| 901 | } |
| 902 | |
| 903 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 904 | static void ops_complete_prexor(void *stripe_head_ref) |
| 905 | { |
| 906 | struct stripe_head *sh = stripe_head_ref; |
| 907 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 908 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 909 | (unsigned long long)sh->sector); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | static struct dma_async_tx_descriptor * |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 913 | ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 914 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 915 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 916 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 917 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 918 | int count = 0, pd_idx = sh->pd_idx, i; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 919 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 920 | |
| 921 | /* existing parity data subtracted */ |
| 922 | struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 923 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 924 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 925 | (unsigned long long)sh->sector); |
| 926 | |
| 927 | for (i = disks; i--; ) { |
| 928 | struct r5dev *dev = &sh->dev[i]; |
| 929 | /* Only process blocks that are known to be uptodate */ |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 930 | if (test_bit(R5_Wantdrain, &dev->flags)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 931 | xor_srcs[count++] = dev->page; |
| 932 | } |
| 933 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 934 | 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] | 935 | ops_complete_prexor, sh, to_addr_conv(sh, percpu)); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 936 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 937 | |
| 938 | return tx; |
| 939 | } |
| 940 | |
| 941 | static struct dma_async_tx_descriptor * |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 942 | 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] | 943 | { |
| 944 | int disks = sh->disks; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 945 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 946 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 947 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 948 | (unsigned long long)sh->sector); |
| 949 | |
| 950 | for (i = disks; i--; ) { |
| 951 | struct r5dev *dev = &sh->dev[i]; |
| 952 | struct bio *chosen; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 953 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 954 | if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 955 | struct bio *wbi; |
| 956 | |
| 957 | spin_lock(&sh->lock); |
| 958 | chosen = dev->towrite; |
| 959 | dev->towrite = NULL; |
| 960 | BUG_ON(dev->written); |
| 961 | wbi = dev->written = chosen; |
| 962 | spin_unlock(&sh->lock); |
| 963 | |
| 964 | while (wbi && wbi->bi_sector < |
| 965 | dev->sector + STRIPE_SECTORS) { |
| 966 | tx = async_copy_data(1, wbi, dev->page, |
| 967 | dev->sector, tx); |
| 968 | wbi = r5_next_bio(wbi, dev->sector); |
| 969 | } |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | return tx; |
| 974 | } |
| 975 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 976 | static void ops_complete_reconstruct(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 977 | { |
| 978 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 979 | int disks = sh->disks; |
| 980 | int pd_idx = sh->pd_idx; |
| 981 | int qd_idx = sh->qd_idx; |
| 982 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 983 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 984 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 985 | (unsigned long long)sh->sector); |
| 986 | |
| 987 | for (i = disks; i--; ) { |
| 988 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 989 | |
| 990 | if (dev->written || i == pd_idx || i == qd_idx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 991 | set_bit(R5_UPTODATE, &dev->flags); |
| 992 | } |
| 993 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 994 | if (sh->reconstruct_state == reconstruct_state_drain_run) |
| 995 | sh->reconstruct_state = reconstruct_state_drain_result; |
| 996 | else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) |
| 997 | sh->reconstruct_state = reconstruct_state_prexor_drain_result; |
| 998 | else { |
| 999 | BUG_ON(sh->reconstruct_state != reconstruct_state_run); |
| 1000 | sh->reconstruct_state = reconstruct_state_result; |
| 1001 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1002 | |
| 1003 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1004 | release_stripe(sh); |
| 1005 | } |
| 1006 | |
| 1007 | static void |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1008 | ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1009 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1010 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1011 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1012 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1013 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1014 | int count = 0, pd_idx = sh->pd_idx, i; |
| 1015 | struct page *xor_dest; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1016 | int prexor = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1017 | unsigned long flags; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1018 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1019 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1020 | (unsigned long long)sh->sector); |
| 1021 | |
| 1022 | /* check if prexor is active which means only process blocks |
| 1023 | * that are part of a read-modify-write (written) |
| 1024 | */ |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1025 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) { |
| 1026 | prexor = 1; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1027 | xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 1028 | for (i = disks; i--; ) { |
| 1029 | struct r5dev *dev = &sh->dev[i]; |
| 1030 | if (dev->written) |
| 1031 | xor_srcs[count++] = dev->page; |
| 1032 | } |
| 1033 | } else { |
| 1034 | xor_dest = sh->dev[pd_idx].page; |
| 1035 | for (i = disks; i--; ) { |
| 1036 | struct r5dev *dev = &sh->dev[i]; |
| 1037 | if (i != pd_idx) |
| 1038 | xor_srcs[count++] = dev->page; |
| 1039 | } |
| 1040 | } |
| 1041 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1042 | /* 1/ if we prexor'd then the dest is reused as a source |
| 1043 | * 2/ if we did not prexor then we are redoing the parity |
| 1044 | * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST |
| 1045 | * for the synchronous xor case |
| 1046 | */ |
Dan Williams | 88ba2aa | 2009-04-09 16:16:18 -0700 | [diff] [blame] | 1047 | flags = ASYNC_TX_ACK | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1048 | (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST); |
| 1049 | |
| 1050 | atomic_inc(&sh->count); |
| 1051 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1052 | init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh, |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1053 | to_addr_conv(sh, percpu)); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1054 | if (unlikely(count == 1)) |
| 1055 | tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit); |
| 1056 | else |
| 1057 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1058 | } |
| 1059 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1060 | static void |
| 1061 | ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1062 | struct dma_async_tx_descriptor *tx) |
| 1063 | { |
| 1064 | struct async_submit_ctl submit; |
| 1065 | struct page **blocks = percpu->scribble; |
| 1066 | int count; |
| 1067 | |
| 1068 | pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector); |
| 1069 | |
| 1070 | count = set_syndrome_sources(blocks, sh); |
| 1071 | |
| 1072 | atomic_inc(&sh->count); |
| 1073 | |
| 1074 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct, |
| 1075 | sh, to_addr_conv(sh, percpu)); |
| 1076 | async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | static void ops_complete_check(void *stripe_head_ref) |
| 1080 | { |
| 1081 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1082 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1083 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1084 | (unsigned long long)sh->sector); |
| 1085 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 1086 | sh->check_state = check_state_check_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1087 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1088 | release_stripe(sh); |
| 1089 | } |
| 1090 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1091 | 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] | 1092 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1093 | int disks = sh->disks; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1094 | int pd_idx = sh->pd_idx; |
| 1095 | int qd_idx = sh->qd_idx; |
| 1096 | struct page *xor_dest; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1097 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1098 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1099 | struct async_submit_ctl submit; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1100 | int count; |
| 1101 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1102 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1103 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1104 | (unsigned long long)sh->sector); |
| 1105 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1106 | count = 0; |
| 1107 | xor_dest = sh->dev[pd_idx].page; |
| 1108 | xor_srcs[count++] = xor_dest; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1109 | for (i = disks; i--; ) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1110 | if (i == pd_idx || i == qd_idx) |
| 1111 | continue; |
| 1112 | xor_srcs[count++] = sh->dev[i].page; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1113 | } |
| 1114 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1115 | init_async_submit(&submit, 0, NULL, NULL, NULL, |
| 1116 | to_addr_conv(sh, percpu)); |
Dan Williams | 099f53c | 2009-04-08 14:28:37 -0700 | [diff] [blame] | 1117 | tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1118 | &sh->ops.zero_sum_result, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1119 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1120 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1121 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL); |
| 1122 | tx = async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1123 | } |
| 1124 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1125 | static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp) |
| 1126 | { |
| 1127 | struct page **srcs = percpu->scribble; |
| 1128 | struct async_submit_ctl submit; |
| 1129 | int count; |
| 1130 | |
| 1131 | pr_debug("%s: stripe %llu checkp: %d\n", __func__, |
| 1132 | (unsigned long long)sh->sector, checkp); |
| 1133 | |
| 1134 | count = set_syndrome_sources(srcs, sh); |
| 1135 | if (!checkp) |
| 1136 | srcs[count] = NULL; |
| 1137 | |
| 1138 | atomic_inc(&sh->count); |
| 1139 | init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check, |
| 1140 | sh, to_addr_conv(sh, percpu)); |
| 1141 | async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE, |
| 1142 | &sh->ops.zero_sum_result, percpu->spare_page, &submit); |
| 1143 | } |
| 1144 | |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 1145 | 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] | 1146 | { |
| 1147 | int overlap_clear = 0, i, disks = sh->disks; |
| 1148 | struct dma_async_tx_descriptor *tx = NULL; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1149 | raid5_conf_t *conf = sh->raid_conf; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1150 | int level = conf->level; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1151 | struct raid5_percpu *percpu; |
| 1152 | unsigned long cpu; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1153 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1154 | cpu = get_cpu(); |
| 1155 | percpu = per_cpu_ptr(conf->percpu, cpu); |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 1156 | if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1157 | ops_run_biofill(sh); |
| 1158 | overlap_clear++; |
| 1159 | } |
| 1160 | |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 1161 | if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1162 | if (level < 6) |
| 1163 | tx = ops_run_compute5(sh, percpu); |
| 1164 | else { |
| 1165 | if (sh->ops.target2 < 0 || sh->ops.target < 0) |
| 1166 | tx = ops_run_compute6_1(sh, percpu); |
| 1167 | else |
| 1168 | tx = ops_run_compute6_2(sh, percpu); |
| 1169 | } |
| 1170 | /* terminate the chain if reconstruct is not set to be run */ |
| 1171 | if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 1172 | async_tx_ack(tx); |
| 1173 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1174 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1175 | if (test_bit(STRIPE_OP_PREXOR, &ops_request)) |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1176 | tx = ops_run_prexor(sh, percpu, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1177 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1178 | if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) { |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1179 | tx = ops_run_biodrain(sh, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1180 | overlap_clear++; |
| 1181 | } |
| 1182 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1183 | if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) { |
| 1184 | if (level < 6) |
| 1185 | ops_run_reconstruct5(sh, percpu, tx); |
| 1186 | else |
| 1187 | ops_run_reconstruct6(sh, percpu, tx); |
| 1188 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1189 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1190 | if (test_bit(STRIPE_OP_CHECK, &ops_request)) { |
| 1191 | if (sh->check_state == check_state_run) |
| 1192 | ops_run_check_p(sh, percpu); |
| 1193 | else if (sh->check_state == check_state_run_q) |
| 1194 | ops_run_check_pq(sh, percpu, 0); |
| 1195 | else if (sh->check_state == check_state_run_pq) |
| 1196 | ops_run_check_pq(sh, percpu, 1); |
| 1197 | else |
| 1198 | BUG(); |
| 1199 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1200 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1201 | if (overlap_clear) |
| 1202 | for (i = disks; i--; ) { |
| 1203 | struct r5dev *dev = &sh->dev[i]; |
| 1204 | if (test_and_clear_bit(R5_Overlap, &dev->flags)) |
| 1205 | wake_up(&sh->raid_conf->wait_for_overlap); |
| 1206 | } |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1207 | put_cpu(); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1208 | } |
| 1209 | |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 1210 | #ifdef CONFIG_MULTICORE_RAID456 |
| 1211 | static void async_run_ops(void *param, async_cookie_t cookie) |
| 1212 | { |
| 1213 | struct stripe_head *sh = param; |
| 1214 | unsigned long ops_request = sh->ops.request; |
| 1215 | |
| 1216 | clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state); |
| 1217 | wake_up(&sh->ops.wait_for_ops); |
| 1218 | |
| 1219 | __raid_run_ops(sh, ops_request); |
| 1220 | release_stripe(sh); |
| 1221 | } |
| 1222 | |
| 1223 | static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request) |
| 1224 | { |
| 1225 | /* since handle_stripe can be called outside of raid5d context |
| 1226 | * we need to ensure sh->ops.request is de-staged before another |
| 1227 | * request arrives |
| 1228 | */ |
| 1229 | wait_event(sh->ops.wait_for_ops, |
| 1230 | !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state)); |
| 1231 | sh->ops.request = ops_request; |
| 1232 | |
| 1233 | atomic_inc(&sh->count); |
| 1234 | async_schedule(async_run_ops, sh); |
| 1235 | } |
| 1236 | #else |
| 1237 | #define raid_run_ops __raid_run_ops |
| 1238 | #endif |
| 1239 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1240 | static int grow_one_stripe(raid5_conf_t *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | { |
| 1242 | struct stripe_head *sh; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 1243 | int disks = max(conf->raid_disks, conf->previous_raid_disks); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1244 | sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL); |
| 1245 | if (!sh) |
| 1246 | return 0; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 1247 | memset(sh, 0, sizeof(*sh) + (disks-1)*sizeof(struct r5dev)); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1248 | sh->raid_conf = conf; |
| 1249 | spin_lock_init(&sh->lock); |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 1250 | #ifdef CONFIG_MULTICORE_RAID456 |
| 1251 | init_waitqueue_head(&sh->ops.wait_for_ops); |
| 1252 | #endif |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1253 | |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 1254 | if (grow_buffers(sh, disks)) { |
| 1255 | shrink_buffers(sh, disks); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1256 | kmem_cache_free(conf->slab_cache, sh); |
| 1257 | return 0; |
| 1258 | } |
| 1259 | /* we just created an active stripe so... */ |
| 1260 | atomic_set(&sh->count, 1); |
| 1261 | atomic_inc(&conf->active_stripes); |
| 1262 | INIT_LIST_HEAD(&sh->lru); |
| 1263 | release_stripe(sh); |
| 1264 | return 1; |
| 1265 | } |
| 1266 | |
| 1267 | static int grow_stripes(raid5_conf_t *conf, int num) |
| 1268 | { |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 1269 | struct kmem_cache *sc; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 1270 | int devs = max(conf->raid_disks, conf->previous_raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 1272 | sprintf(conf->cache_name[0], |
| 1273 | "raid%d-%s", conf->level, mdname(conf->mddev)); |
| 1274 | sprintf(conf->cache_name[1], |
| 1275 | "raid%d-%s-alt", conf->level, mdname(conf->mddev)); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1276 | conf->active_name = 0; |
| 1277 | sc = kmem_cache_create(conf->cache_name[conf->active_name], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1279 | 0, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | if (!sc) |
| 1281 | return 1; |
| 1282 | conf->slab_cache = sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1283 | conf->pool_size = devs; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1284 | while (num--) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1285 | if (!grow_one_stripe(conf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | return 0; |
| 1288 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 1289 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1290 | /** |
| 1291 | * scribble_len - return the required size of the scribble region |
| 1292 | * @num - total number of disks in the array |
| 1293 | * |
| 1294 | * The size must be enough to contain: |
| 1295 | * 1/ a struct page pointer for each device in the array +2 |
| 1296 | * 2/ room to convert each entry in (1) to its corresponding dma |
| 1297 | * (dma_map_page()) or page (page_address()) address. |
| 1298 | * |
| 1299 | * Note: the +2 is for the destination buffers of the ddf/raid6 case where we |
| 1300 | * calculate over all devices (not just the data blocks), using zeros in place |
| 1301 | * of the P and Q blocks. |
| 1302 | */ |
| 1303 | static size_t scribble_len(int num) |
| 1304 | { |
| 1305 | size_t len; |
| 1306 | |
| 1307 | len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2); |
| 1308 | |
| 1309 | return len; |
| 1310 | } |
| 1311 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1312 | static int resize_stripes(raid5_conf_t *conf, int newsize) |
| 1313 | { |
| 1314 | /* Make all the stripes able to hold 'newsize' devices. |
| 1315 | * New slots in each stripe get 'page' set to a new page. |
| 1316 | * |
| 1317 | * This happens in stages: |
| 1318 | * 1/ create a new kmem_cache and allocate the required number of |
| 1319 | * stripe_heads. |
| 1320 | * 2/ gather all the old stripe_heads and tranfer the pages across |
| 1321 | * to the new stripe_heads. This will have the side effect of |
| 1322 | * freezing the array as once all stripe_heads have been collected, |
| 1323 | * no IO will be possible. Old stripe heads are freed once their |
| 1324 | * pages have been transferred over, and the old kmem_cache is |
| 1325 | * freed when all stripes are done. |
| 1326 | * 3/ reallocate conf->disks to be suitable bigger. If this fails, |
| 1327 | * we simple return a failre status - no need to clean anything up. |
| 1328 | * 4/ allocate new pages for the new slots in the new stripe_heads. |
| 1329 | * If this fails, we don't bother trying the shrink the |
| 1330 | * stripe_heads down again, we just leave them as they are. |
| 1331 | * As each stripe_head is processed the new one is released into |
| 1332 | * active service. |
| 1333 | * |
| 1334 | * Once step2 is started, we cannot afford to wait for a write, |
| 1335 | * so we use GFP_NOIO allocations. |
| 1336 | */ |
| 1337 | struct stripe_head *osh, *nsh; |
| 1338 | LIST_HEAD(newstripes); |
| 1339 | struct disk_info *ndisks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1340 | unsigned long cpu; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 1341 | int err; |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 1342 | struct kmem_cache *sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1343 | int i; |
| 1344 | |
| 1345 | if (newsize <= conf->pool_size) |
| 1346 | return 0; /* never bother to shrink */ |
| 1347 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 1348 | err = md_allow_write(conf->mddev); |
| 1349 | if (err) |
| 1350 | return err; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 1351 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1352 | /* Step 1 */ |
| 1353 | sc = kmem_cache_create(conf->cache_name[1-conf->active_name], |
| 1354 | sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1355 | 0, 0, NULL); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1356 | if (!sc) |
| 1357 | return -ENOMEM; |
| 1358 | |
| 1359 | for (i = conf->max_nr_stripes; i; i--) { |
| 1360 | nsh = kmem_cache_alloc(sc, GFP_KERNEL); |
| 1361 | if (!nsh) |
| 1362 | break; |
| 1363 | |
| 1364 | memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev)); |
| 1365 | |
| 1366 | nsh->raid_conf = conf; |
| 1367 | spin_lock_init(&nsh->lock); |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 1368 | #ifdef CONFIG_MULTICORE_RAID456 |
| 1369 | init_waitqueue_head(&nsh->ops.wait_for_ops); |
| 1370 | #endif |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1371 | |
| 1372 | list_add(&nsh->lru, &newstripes); |
| 1373 | } |
| 1374 | if (i) { |
| 1375 | /* didn't get enough, give up */ |
| 1376 | while (!list_empty(&newstripes)) { |
| 1377 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 1378 | list_del(&nsh->lru); |
| 1379 | kmem_cache_free(sc, nsh); |
| 1380 | } |
| 1381 | kmem_cache_destroy(sc); |
| 1382 | return -ENOMEM; |
| 1383 | } |
| 1384 | /* Step 2 - Must use GFP_NOIO now. |
| 1385 | * OK, we have enough stripes, start collecting inactive |
| 1386 | * stripes and copying them over |
| 1387 | */ |
| 1388 | list_for_each_entry(nsh, &newstripes, lru) { |
| 1389 | spin_lock_irq(&conf->device_lock); |
| 1390 | wait_event_lock_irq(conf->wait_for_stripe, |
| 1391 | !list_empty(&conf->inactive_list), |
| 1392 | conf->device_lock, |
NeilBrown | b3b46be | 2006-03-27 01:18:16 -0800 | [diff] [blame] | 1393 | unplug_slaves(conf->mddev) |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1394 | ); |
| 1395 | osh = get_free_stripe(conf); |
| 1396 | spin_unlock_irq(&conf->device_lock); |
| 1397 | atomic_set(&nsh->count, 1); |
| 1398 | for(i=0; i<conf->pool_size; i++) |
| 1399 | nsh->dev[i].page = osh->dev[i].page; |
| 1400 | for( ; i<newsize; i++) |
| 1401 | nsh->dev[i].page = NULL; |
| 1402 | kmem_cache_free(conf->slab_cache, osh); |
| 1403 | } |
| 1404 | kmem_cache_destroy(conf->slab_cache); |
| 1405 | |
| 1406 | /* Step 3. |
| 1407 | * At this point, we are holding all the stripes so the array |
| 1408 | * is completely stalled, so now is a good time to resize |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1409 | * conf->disks and the scribble region |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1410 | */ |
| 1411 | ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO); |
| 1412 | if (ndisks) { |
| 1413 | for (i=0; i<conf->raid_disks; i++) |
| 1414 | ndisks[i] = conf->disks[i]; |
| 1415 | kfree(conf->disks); |
| 1416 | conf->disks = ndisks; |
| 1417 | } else |
| 1418 | err = -ENOMEM; |
| 1419 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1420 | get_online_cpus(); |
| 1421 | conf->scribble_len = scribble_len(newsize); |
| 1422 | for_each_present_cpu(cpu) { |
| 1423 | struct raid5_percpu *percpu; |
| 1424 | void *scribble; |
| 1425 | |
| 1426 | percpu = per_cpu_ptr(conf->percpu, cpu); |
| 1427 | scribble = kmalloc(conf->scribble_len, GFP_NOIO); |
| 1428 | |
| 1429 | if (scribble) { |
| 1430 | kfree(percpu->scribble); |
| 1431 | percpu->scribble = scribble; |
| 1432 | } else { |
| 1433 | err = -ENOMEM; |
| 1434 | break; |
| 1435 | } |
| 1436 | } |
| 1437 | put_online_cpus(); |
| 1438 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1439 | /* Step 4, return new stripes to service */ |
| 1440 | while(!list_empty(&newstripes)) { |
| 1441 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 1442 | list_del_init(&nsh->lru); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1443 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1444 | for (i=conf->raid_disks; i < newsize; i++) |
| 1445 | if (nsh->dev[i].page == NULL) { |
| 1446 | struct page *p = alloc_page(GFP_NOIO); |
| 1447 | nsh->dev[i].page = p; |
| 1448 | if (!p) |
| 1449 | err = -ENOMEM; |
| 1450 | } |
| 1451 | release_stripe(nsh); |
| 1452 | } |
| 1453 | /* critical section pass, GFP_NOIO no longer needed */ |
| 1454 | |
| 1455 | conf->slab_cache = sc; |
| 1456 | conf->active_name = 1-conf->active_name; |
| 1457 | conf->pool_size = newsize; |
| 1458 | return err; |
| 1459 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1461 | static int drop_one_stripe(raid5_conf_t *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | { |
| 1463 | struct stripe_head *sh; |
| 1464 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1465 | spin_lock_irq(&conf->device_lock); |
| 1466 | sh = get_free_stripe(conf); |
| 1467 | spin_unlock_irq(&conf->device_lock); |
| 1468 | if (!sh) |
| 1469 | return 0; |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 1470 | BUG_ON(atomic_read(&sh->count)); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1471 | shrink_buffers(sh, conf->pool_size); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1472 | kmem_cache_free(conf->slab_cache, sh); |
| 1473 | atomic_dec(&conf->active_stripes); |
| 1474 | return 1; |
| 1475 | } |
| 1476 | |
| 1477 | static void shrink_stripes(raid5_conf_t *conf) |
| 1478 | { |
| 1479 | while (drop_one_stripe(conf)) |
| 1480 | ; |
| 1481 | |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1482 | if (conf->slab_cache) |
| 1483 | kmem_cache_destroy(conf->slab_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | conf->slab_cache = NULL; |
| 1485 | } |
| 1486 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1487 | static void raid5_end_read_request(struct bio * bi, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1489 | struct stripe_head *sh = bi->bi_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 1491 | int disks = sh->disks, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1493 | char b[BDEVNAME_SIZE]; |
| 1494 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | |
| 1497 | for (i=0 ; i<disks; i++) |
| 1498 | if (bi == &sh->dev[i].req) |
| 1499 | break; |
| 1500 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1501 | pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n", |
| 1502 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | uptodate); |
| 1504 | if (i == disks) { |
| 1505 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1506 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | if (uptodate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | set_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1511 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1512 | rdev = conf->disks[i].rdev; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 1513 | printk_rl(KERN_INFO "md/raid:%s: read error corrected" |
Bernd Schubert | 6be9d49 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 1514 | " (%lu sectors at %llu on %s)\n", |
| 1515 | mdname(conf->mddev), STRIPE_SECTORS, |
| 1516 | (unsigned long long)(sh->sector |
| 1517 | + rdev->data_offset), |
| 1518 | bdevname(rdev->bdev, b)); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1519 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 1520 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
| 1521 | } |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1522 | if (atomic_read(&conf->disks[i].rdev->read_errors)) |
| 1523 | atomic_set(&conf->disks[i].rdev->read_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | } else { |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1525 | const char *bdn = bdevname(conf->disks[i].rdev->bdev, b); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1526 | int retry = 0; |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1527 | rdev = conf->disks[i].rdev; |
| 1528 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | clear_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1530 | atomic_inc(&rdev->read_errors); |
Gabriele A. Trombetti | 7b0bb53 | 2010-04-28 11:51:17 +1000 | [diff] [blame] | 1531 | if (conf->mddev->degraded >= conf->max_degraded) |
Bernd Schubert | 6be9d49 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 1532 | printk_rl(KERN_WARNING |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 1533 | "md/raid:%s: read error not correctable " |
Bernd Schubert | 6be9d49 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 1534 | "(sector %llu on %s).\n", |
| 1535 | mdname(conf->mddev), |
| 1536 | (unsigned long long)(sh->sector |
| 1537 | + rdev->data_offset), |
| 1538 | bdn); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1539 | else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1540 | /* Oh, no!!! */ |
Bernd Schubert | 6be9d49 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 1541 | printk_rl(KERN_WARNING |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 1542 | "md/raid:%s: read error NOT corrected!! " |
Bernd Schubert | 6be9d49 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 1543 | "(sector %llu on %s).\n", |
| 1544 | mdname(conf->mddev), |
| 1545 | (unsigned long long)(sh->sector |
| 1546 | + rdev->data_offset), |
| 1547 | bdn); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1548 | else if (atomic_read(&rdev->read_errors) |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1549 | > conf->max_nr_stripes) |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 1550 | printk(KERN_WARNING |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 1551 | "md/raid:%s: Too many read errors, failing device %s.\n", |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1552 | mdname(conf->mddev), bdn); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1553 | else |
| 1554 | retry = 1; |
| 1555 | if (retry) |
| 1556 | set_bit(R5_ReadError, &sh->dev[i].flags); |
| 1557 | else { |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1558 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 1559 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1560 | md_error(conf->mddev, rdev); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1561 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | } |
| 1563 | rdev_dec_pending(conf->disks[i].rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 1565 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1566 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | } |
| 1568 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 1569 | static void raid5_end_write_request(struct bio *bi, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1571 | struct stripe_head *sh = bi->bi_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 1573 | int disks = sh->disks, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
| 1575 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | for (i=0 ; i<disks; i++) |
| 1577 | if (bi == &sh->dev[i].req) |
| 1578 | break; |
| 1579 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1580 | pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
| 1582 | uptodate); |
| 1583 | if (i == disks) { |
| 1584 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1585 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | } |
| 1587 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | if (!uptodate) |
| 1589 | md_error(conf->mddev, conf->disks[i].rdev); |
| 1590 | |
| 1591 | rdev_dec_pending(conf->disks[i].rdev, conf->mddev); |
| 1592 | |
| 1593 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 1594 | set_bit(STRIPE_HANDLE, &sh->state); |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1595 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | } |
| 1597 | |
| 1598 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1599 | 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] | 1600 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1601 | 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] | 1602 | { |
| 1603 | struct r5dev *dev = &sh->dev[i]; |
| 1604 | |
| 1605 | bio_init(&dev->req); |
| 1606 | dev->req.bi_io_vec = &dev->vec; |
| 1607 | dev->req.bi_vcnt++; |
| 1608 | dev->req.bi_max_vecs++; |
| 1609 | dev->vec.bv_page = dev->page; |
| 1610 | dev->vec.bv_len = STRIPE_SIZE; |
| 1611 | dev->vec.bv_offset = 0; |
| 1612 | |
| 1613 | dev->req.bi_sector = sh->sector; |
| 1614 | dev->req.bi_private = sh; |
| 1615 | |
| 1616 | dev->flags = 0; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1617 | dev->sector = compute_blocknr(sh, i, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | } |
| 1619 | |
| 1620 | static void error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1621 | { |
| 1622 | char b[BDEVNAME_SIZE]; |
H Hartley Sweeten | 7b92813 | 2010-03-08 16:02:40 +1100 | [diff] [blame] | 1623 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 1624 | pr_debug("raid456: error called\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1625 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1626 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1627 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1628 | if (test_and_clear_bit(In_sync, &rdev->flags)) { |
| 1629 | unsigned long flags; |
| 1630 | spin_lock_irqsave(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | mddev->degraded++; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1632 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | /* |
| 1634 | * if recovery was running, make sure it aborts. |
| 1635 | */ |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 1636 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1638 | set_bit(Faulty, &rdev->flags); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 1639 | printk(KERN_ALERT |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 1640 | "md/raid:%s: Disk failure on %s, disabling device.\n" |
| 1641 | KERN_ALERT |
| 1642 | "md/raid:%s: Operation continuing on %d devices.\n", |
| 1643 | mdname(mddev), |
| 1644 | bdevname(rdev->bdev, b), |
| 1645 | mdname(mddev), |
| 1646 | conf->raid_disks - mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1648 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | |
| 1650 | /* |
| 1651 | * Input: a 'big' sector number, |
| 1652 | * Output: index of the data and parity disk, and the sector # in them. |
| 1653 | */ |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1654 | 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] | 1655 | int previous, int *dd_idx, |
| 1656 | struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | { |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1658 | sector_t stripe, stripe2; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 1659 | sector_t chunk_number; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | unsigned int chunk_offset; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1661 | int pd_idx, qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1662 | int ddf_layout = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | sector_t new_sector; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1664 | int algorithm = previous ? conf->prev_algo |
| 1665 | : conf->algorithm; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 1666 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
| 1667 | : conf->chunk_sectors; |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1668 | int raid_disks = previous ? conf->previous_raid_disks |
| 1669 | : conf->raid_disks; |
| 1670 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | |
| 1672 | /* First compute the information on this sector */ |
| 1673 | |
| 1674 | /* |
| 1675 | * Compute the chunk number and the sector offset inside the chunk |
| 1676 | */ |
| 1677 | chunk_offset = sector_div(r_sector, sectors_per_chunk); |
| 1678 | chunk_number = r_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | |
| 1680 | /* |
| 1681 | * Compute the stripe number |
| 1682 | */ |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 1683 | stripe = chunk_number; |
| 1684 | *dd_idx = sector_div(stripe, data_disks); |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1685 | stripe2 = stripe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | /* |
| 1687 | * Select the parity disk based on the user selected algorithm. |
| 1688 | */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1689 | pd_idx = qd_idx = ~0; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1690 | switch(conf->level) { |
| 1691 | case 4: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1692 | pd_idx = data_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1693 | break; |
| 1694 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1695 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1696 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1697 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1698 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1699 | (*dd_idx)++; |
| 1700 | break; |
| 1701 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1702 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1703 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | (*dd_idx)++; |
| 1705 | break; |
| 1706 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1707 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1708 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | break; |
| 1710 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1711 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1712 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1714 | case ALGORITHM_PARITY_0: |
| 1715 | pd_idx = 0; |
| 1716 | (*dd_idx)++; |
| 1717 | break; |
| 1718 | case ALGORITHM_PARITY_N: |
| 1719 | pd_idx = data_disks; |
| 1720 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1722 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1723 | } |
| 1724 | break; |
| 1725 | case 6: |
| 1726 | |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1727 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1728 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1729 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1730 | qd_idx = pd_idx + 1; |
| 1731 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1732 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1733 | qd_idx = 0; |
| 1734 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1735 | (*dd_idx) += 2; /* D D P Q D */ |
| 1736 | break; |
| 1737 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1738 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1739 | qd_idx = pd_idx + 1; |
| 1740 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1741 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1742 | qd_idx = 0; |
| 1743 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1744 | (*dd_idx) += 2; /* D D P Q D */ |
| 1745 | break; |
| 1746 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1747 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1748 | qd_idx = (pd_idx + 1) % raid_disks; |
| 1749 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1750 | break; |
| 1751 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1752 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1753 | qd_idx = (pd_idx + 1) % raid_disks; |
| 1754 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1755 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1756 | |
| 1757 | case ALGORITHM_PARITY_0: |
| 1758 | pd_idx = 0; |
| 1759 | qd_idx = 1; |
| 1760 | (*dd_idx) += 2; |
| 1761 | break; |
| 1762 | case ALGORITHM_PARITY_N: |
| 1763 | pd_idx = data_disks; |
| 1764 | qd_idx = data_disks + 1; |
| 1765 | break; |
| 1766 | |
| 1767 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 1768 | /* Exactly the same as RIGHT_ASYMMETRIC, but or |
| 1769 | * of blocks for computing Q is different. |
| 1770 | */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1771 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1772 | qd_idx = pd_idx + 1; |
| 1773 | if (pd_idx == raid_disks-1) { |
| 1774 | (*dd_idx)++; /* Q D D D P */ |
| 1775 | qd_idx = 0; |
| 1776 | } else if (*dd_idx >= pd_idx) |
| 1777 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1778 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1779 | break; |
| 1780 | |
| 1781 | case ALGORITHM_ROTATING_N_RESTART: |
| 1782 | /* Same a left_asymmetric, by first stripe is |
| 1783 | * D D D P Q rather than |
| 1784 | * Q D D D P |
| 1785 | */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1786 | stripe2 += 1; |
| 1787 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1788 | qd_idx = pd_idx + 1; |
| 1789 | if (pd_idx == raid_disks-1) { |
| 1790 | (*dd_idx)++; /* Q D D D P */ |
| 1791 | qd_idx = 0; |
| 1792 | } else if (*dd_idx >= pd_idx) |
| 1793 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1794 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1795 | break; |
| 1796 | |
| 1797 | case ALGORITHM_ROTATING_N_CONTINUE: |
| 1798 | /* Same as left_symmetric but Q is before P */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1799 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1800 | qd_idx = (pd_idx + raid_disks - 1) % raid_disks; |
| 1801 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1802 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1803 | break; |
| 1804 | |
| 1805 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 1806 | /* RAID5 left_asymmetric, with Q on last device */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1807 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1808 | if (*dd_idx >= pd_idx) |
| 1809 | (*dd_idx)++; |
| 1810 | qd_idx = raid_disks - 1; |
| 1811 | break; |
| 1812 | |
| 1813 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1814 | pd_idx = sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1815 | if (*dd_idx >= pd_idx) |
| 1816 | (*dd_idx)++; |
| 1817 | qd_idx = raid_disks - 1; |
| 1818 | break; |
| 1819 | |
| 1820 | case ALGORITHM_LEFT_SYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1821 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1822 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 1823 | qd_idx = raid_disks - 1; |
| 1824 | break; |
| 1825 | |
| 1826 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1827 | pd_idx = sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1828 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 1829 | qd_idx = raid_disks - 1; |
| 1830 | break; |
| 1831 | |
| 1832 | case ALGORITHM_PARITY_0_6: |
| 1833 | pd_idx = 0; |
| 1834 | (*dd_idx)++; |
| 1835 | qd_idx = raid_disks - 1; |
| 1836 | break; |
| 1837 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1838 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1839 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1840 | } |
| 1841 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1842 | } |
| 1843 | |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1844 | if (sh) { |
| 1845 | sh->pd_idx = pd_idx; |
| 1846 | sh->qd_idx = qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1847 | sh->ddf_layout = ddf_layout; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1848 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1849 | /* |
| 1850 | * Finally, compute the new sector number |
| 1851 | */ |
| 1852 | new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset; |
| 1853 | return new_sector; |
| 1854 | } |
| 1855 | |
| 1856 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1857 | 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] | 1858 | { |
| 1859 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 1860 | int raid_disks = sh->disks; |
| 1861 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | sector_t new_sector = sh->sector, check; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 1863 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
| 1864 | : conf->chunk_sectors; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1865 | int algorithm = previous ? conf->prev_algo |
| 1866 | : conf->algorithm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1867 | sector_t stripe; |
| 1868 | int chunk_offset; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 1869 | sector_t chunk_number; |
| 1870 | int dummy1, dd_idx = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | sector_t r_sector; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1872 | struct stripe_head sh2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1874 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | chunk_offset = sector_div(new_sector, sectors_per_chunk); |
| 1876 | stripe = new_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1877 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1878 | if (i == sh->pd_idx) |
| 1879 | return 0; |
| 1880 | switch(conf->level) { |
| 1881 | case 4: break; |
| 1882 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1883 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 1885 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 1886 | if (i > sh->pd_idx) |
| 1887 | i--; |
| 1888 | break; |
| 1889 | case ALGORITHM_LEFT_SYMMETRIC: |
| 1890 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 1891 | if (i < sh->pd_idx) |
| 1892 | i += raid_disks; |
| 1893 | i -= (sh->pd_idx + 1); |
| 1894 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1895 | case ALGORITHM_PARITY_0: |
| 1896 | i -= 1; |
| 1897 | break; |
| 1898 | case ALGORITHM_PARITY_N: |
| 1899 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1900 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1901 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1902 | } |
| 1903 | break; |
| 1904 | case 6: |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1905 | if (i == sh->qd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1906 | return 0; /* It is the Q disk */ |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1907 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1908 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 1909 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1910 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 1911 | case ALGORITHM_ROTATING_N_RESTART: |
| 1912 | if (sh->pd_idx == raid_disks-1) |
| 1913 | i--; /* Q D D D P */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1914 | else if (i > sh->pd_idx) |
| 1915 | i -= 2; /* D D P Q D */ |
| 1916 | break; |
| 1917 | case ALGORITHM_LEFT_SYMMETRIC: |
| 1918 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 1919 | if (sh->pd_idx == raid_disks-1) |
| 1920 | i--; /* Q D D D P */ |
| 1921 | else { |
| 1922 | /* D D P Q D */ |
| 1923 | if (i < sh->pd_idx) |
| 1924 | i += raid_disks; |
| 1925 | i -= (sh->pd_idx + 2); |
| 1926 | } |
| 1927 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1928 | case ALGORITHM_PARITY_0: |
| 1929 | i -= 2; |
| 1930 | break; |
| 1931 | case ALGORITHM_PARITY_N: |
| 1932 | break; |
| 1933 | case ALGORITHM_ROTATING_N_CONTINUE: |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 1934 | /* Like left_symmetric, but P is before Q */ |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1935 | if (sh->pd_idx == 0) |
| 1936 | i--; /* P D D D Q */ |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 1937 | else { |
| 1938 | /* D D Q P D */ |
| 1939 | if (i < sh->pd_idx) |
| 1940 | i += raid_disks; |
| 1941 | i -= (sh->pd_idx + 1); |
| 1942 | } |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1943 | break; |
| 1944 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 1945 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 1946 | if (i > sh->pd_idx) |
| 1947 | i--; |
| 1948 | break; |
| 1949 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 1950 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 1951 | if (i < sh->pd_idx) |
| 1952 | i += data_disks + 1; |
| 1953 | i -= (sh->pd_idx + 1); |
| 1954 | break; |
| 1955 | case ALGORITHM_PARITY_0_6: |
| 1956 | i -= 1; |
| 1957 | break; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1958 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1959 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1960 | } |
| 1961 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1962 | } |
| 1963 | |
| 1964 | chunk_number = stripe * data_disks + i; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 1965 | r_sector = chunk_number * sectors_per_chunk + chunk_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1966 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1967 | check = raid5_compute_sector(conf, r_sector, |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1968 | previous, &dummy1, &sh2); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1969 | if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx |
| 1970 | || sh2.qd_idx != sh->qd_idx) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 1971 | printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n", |
| 1972 | mdname(conf->mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | return 0; |
| 1974 | } |
| 1975 | return r_sector; |
| 1976 | } |
| 1977 | |
| 1978 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1979 | static void |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 1980 | schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1981 | int rcw, int expand) |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1982 | { |
| 1983 | int i, pd_idx = sh->pd_idx, disks = sh->disks; |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 1984 | raid5_conf_t *conf = sh->raid_conf; |
| 1985 | int level = conf->level; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1986 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1987 | if (rcw) { |
| 1988 | /* if we are not expanding this is a proper write request, and |
| 1989 | * there will be bios with new data to be drained into the |
| 1990 | * stripe cache |
| 1991 | */ |
| 1992 | if (!expand) { |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1993 | sh->reconstruct_state = reconstruct_state_drain_run; |
| 1994 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
| 1995 | } else |
| 1996 | sh->reconstruct_state = reconstruct_state_run; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1997 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1998 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1999 | |
| 2000 | for (i = disks; i--; ) { |
| 2001 | struct r5dev *dev = &sh->dev[i]; |
| 2002 | |
| 2003 | if (dev->towrite) { |
| 2004 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2005 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2006 | if (!expand) |
| 2007 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2008 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2009 | } |
| 2010 | } |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2011 | if (s->locked + conf->max_degraded == disks) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2012 | if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state)) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2013 | atomic_inc(&conf->pending_full_writes); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2014 | } else { |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2015 | BUG_ON(level == 6); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2016 | BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) || |
| 2017 | test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags))); |
| 2018 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2019 | sh->reconstruct_state = reconstruct_state_prexor_drain_run; |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2020 | set_bit(STRIPE_OP_PREXOR, &s->ops_request); |
| 2021 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2022 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2023 | |
| 2024 | for (i = disks; i--; ) { |
| 2025 | struct r5dev *dev = &sh->dev[i]; |
| 2026 | if (i == pd_idx) |
| 2027 | continue; |
| 2028 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2029 | if (dev->towrite && |
| 2030 | (test_bit(R5_UPTODATE, &dev->flags) || |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2031 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2032 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2033 | set_bit(R5_LOCKED, &dev->flags); |
| 2034 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2035 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2036 | } |
| 2037 | } |
| 2038 | } |
| 2039 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2040 | /* keep the parity disk(s) locked while asynchronous operations |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2041 | * are in flight |
| 2042 | */ |
| 2043 | set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); |
| 2044 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2045 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2046 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2047 | if (level == 6) { |
| 2048 | int qd_idx = sh->qd_idx; |
| 2049 | struct r5dev *dev = &sh->dev[qd_idx]; |
| 2050 | |
| 2051 | set_bit(R5_LOCKED, &dev->flags); |
| 2052 | clear_bit(R5_UPTODATE, &dev->flags); |
| 2053 | s->locked++; |
| 2054 | } |
| 2055 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2056 | pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n", |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 2057 | __func__, (unsigned long long)sh->sector, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2058 | s->locked, s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2059 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2060 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | /* |
| 2062 | * Each stripe/dev can have one or more bion attached. |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2063 | * toread/towrite point to the first in a chain. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | * The bi_next chain must be in order. |
| 2065 | */ |
| 2066 | static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite) |
| 2067 | { |
| 2068 | struct bio **bip; |
| 2069 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2070 | int firstwrite=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2072 | pr_debug("adding bh b#%llu to stripe s#%llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2073 | (unsigned long long)bi->bi_sector, |
| 2074 | (unsigned long long)sh->sector); |
| 2075 | |
| 2076 | |
| 2077 | spin_lock(&sh->lock); |
| 2078 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2079 | if (forwrite) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2080 | bip = &sh->dev[dd_idx].towrite; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2081 | if (*bip == NULL && sh->dev[dd_idx].written == NULL) |
| 2082 | firstwrite = 1; |
| 2083 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2084 | bip = &sh->dev[dd_idx].toread; |
| 2085 | while (*bip && (*bip)->bi_sector < bi->bi_sector) { |
| 2086 | if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector) |
| 2087 | goto overlap; |
| 2088 | bip = & (*bip)->bi_next; |
| 2089 | } |
| 2090 | if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9)) |
| 2091 | goto overlap; |
| 2092 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 2093 | BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | if (*bip) |
| 2095 | bi->bi_next = *bip; |
| 2096 | *bip = bi; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2097 | bi->bi_phys_segments++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2098 | spin_unlock_irq(&conf->device_lock); |
| 2099 | spin_unlock(&sh->lock); |
| 2100 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2101 | 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] | 2102 | (unsigned long long)bi->bi_sector, |
| 2103 | (unsigned long long)sh->sector, dd_idx); |
| 2104 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2105 | if (conf->mddev->bitmap && firstwrite) { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2106 | bitmap_startwrite(conf->mddev->bitmap, sh->sector, |
| 2107 | STRIPE_SECTORS, 0); |
NeilBrown | ae3c20c | 2006-07-10 04:44:17 -0700 | [diff] [blame] | 2108 | sh->bm_seq = conf->seq_flush+1; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2109 | set_bit(STRIPE_BIT_DELAY, &sh->state); |
| 2110 | } |
| 2111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | if (forwrite) { |
| 2113 | /* check if page is covered */ |
| 2114 | sector_t sector = sh->dev[dd_idx].sector; |
| 2115 | for (bi=sh->dev[dd_idx].towrite; |
| 2116 | sector < sh->dev[dd_idx].sector + STRIPE_SECTORS && |
| 2117 | bi && bi->bi_sector <= sector; |
| 2118 | bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) { |
| 2119 | if (bi->bi_sector + (bi->bi_size>>9) >= sector) |
| 2120 | sector = bi->bi_sector + (bi->bi_size>>9); |
| 2121 | } |
| 2122 | if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS) |
| 2123 | set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags); |
| 2124 | } |
| 2125 | return 1; |
| 2126 | |
| 2127 | overlap: |
| 2128 | set_bit(R5_Overlap, &sh->dev[dd_idx].flags); |
| 2129 | spin_unlock_irq(&conf->device_lock); |
| 2130 | spin_unlock(&sh->lock); |
| 2131 | return 0; |
| 2132 | } |
| 2133 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 2134 | static void end_reshape(raid5_conf_t *conf); |
| 2135 | |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2136 | static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous, |
| 2137 | struct stripe_head *sh) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2138 | { |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2139 | int sectors_per_chunk = |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 2140 | previous ? conf->prev_chunk_sectors : conf->chunk_sectors; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2141 | int dd_idx; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 2142 | int chunk_offset = sector_div(stripe, sectors_per_chunk); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2143 | int disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 2144 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2145 | raid5_compute_sector(conf, |
| 2146 | stripe * (disks - conf->max_degraded) |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 2147 | *sectors_per_chunk + chunk_offset, |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2148 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2149 | &dd_idx, sh); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2150 | } |
| 2151 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2152 | static void |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2153 | handle_failed_stripe(raid5_conf_t *conf, struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2154 | struct stripe_head_state *s, int disks, |
| 2155 | struct bio **return_bi) |
| 2156 | { |
| 2157 | int i; |
| 2158 | for (i = disks; i--; ) { |
| 2159 | struct bio *bi; |
| 2160 | int bitmap_end = 0; |
| 2161 | |
| 2162 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
| 2163 | mdk_rdev_t *rdev; |
| 2164 | rcu_read_lock(); |
| 2165 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 2166 | if (rdev && test_bit(In_sync, &rdev->flags)) |
| 2167 | /* multiple read failures in one stripe */ |
| 2168 | md_error(conf->mddev, rdev); |
| 2169 | rcu_read_unlock(); |
| 2170 | } |
| 2171 | spin_lock_irq(&conf->device_lock); |
| 2172 | /* fail all writes first */ |
| 2173 | bi = sh->dev[i].towrite; |
| 2174 | sh->dev[i].towrite = NULL; |
| 2175 | if (bi) { |
| 2176 | s->to_write--; |
| 2177 | bitmap_end = 1; |
| 2178 | } |
| 2179 | |
| 2180 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 2181 | wake_up(&conf->wait_for_overlap); |
| 2182 | |
| 2183 | while (bi && bi->bi_sector < |
| 2184 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2185 | struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); |
| 2186 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2187 | if (!raid5_dec_bi_phys_segments(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2188 | md_write_end(conf->mddev); |
| 2189 | bi->bi_next = *return_bi; |
| 2190 | *return_bi = bi; |
| 2191 | } |
| 2192 | bi = nextbi; |
| 2193 | } |
| 2194 | /* and fail all 'written' */ |
| 2195 | bi = sh->dev[i].written; |
| 2196 | sh->dev[i].written = NULL; |
| 2197 | if (bi) bitmap_end = 1; |
| 2198 | while (bi && bi->bi_sector < |
| 2199 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2200 | struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector); |
| 2201 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2202 | if (!raid5_dec_bi_phys_segments(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2203 | md_write_end(conf->mddev); |
| 2204 | bi->bi_next = *return_bi; |
| 2205 | *return_bi = bi; |
| 2206 | } |
| 2207 | bi = bi2; |
| 2208 | } |
| 2209 | |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2210 | /* fail any reads if this device is non-operational and |
| 2211 | * the data has not reached the cache yet. |
| 2212 | */ |
| 2213 | if (!test_bit(R5_Wantfill, &sh->dev[i].flags) && |
| 2214 | (!test_bit(R5_Insync, &sh->dev[i].flags) || |
| 2215 | test_bit(R5_ReadError, &sh->dev[i].flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2216 | bi = sh->dev[i].toread; |
| 2217 | sh->dev[i].toread = NULL; |
| 2218 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 2219 | wake_up(&conf->wait_for_overlap); |
| 2220 | if (bi) s->to_read--; |
| 2221 | while (bi && bi->bi_sector < |
| 2222 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2223 | struct bio *nextbi = |
| 2224 | r5_next_bio(bi, sh->dev[i].sector); |
| 2225 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2226 | if (!raid5_dec_bi_phys_segments(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2227 | bi->bi_next = *return_bi; |
| 2228 | *return_bi = bi; |
| 2229 | } |
| 2230 | bi = nextbi; |
| 2231 | } |
| 2232 | } |
| 2233 | spin_unlock_irq(&conf->device_lock); |
| 2234 | if (bitmap_end) |
| 2235 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 2236 | STRIPE_SECTORS, 0, 0); |
| 2237 | } |
| 2238 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2239 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 2240 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 2241 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2242 | } |
| 2243 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2244 | /* fetch_block5 - checks the given member device to see if its data needs |
| 2245 | * to be read or computed to satisfy a request. |
| 2246 | * |
| 2247 | * Returns 1 when no more member devices need to be checked, otherwise returns |
| 2248 | * 0 to tell the loop in handle_stripe_fill5 to continue |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2249 | */ |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2250 | static int fetch_block5(struct stripe_head *sh, struct stripe_head_state *s, |
| 2251 | int disk_idx, int disks) |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2252 | { |
| 2253 | struct r5dev *dev = &sh->dev[disk_idx]; |
| 2254 | struct r5dev *failed_dev = &sh->dev[s->failed_num]; |
| 2255 | |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2256 | /* is the data in this block needed, and can we get it? */ |
| 2257 | if (!test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2258 | !test_bit(R5_UPTODATE, &dev->flags) && |
| 2259 | (dev->toread || |
| 2260 | (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) || |
| 2261 | s->syncing || s->expanding || |
| 2262 | (s->failed && |
| 2263 | (failed_dev->toread || |
| 2264 | (failed_dev->towrite && |
| 2265 | !test_bit(R5_OVERWRITE, &failed_dev->flags)))))) { |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2266 | /* We would like to get this block, possibly by computing it, |
| 2267 | * otherwise read it if the backing disk is insync |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2268 | */ |
| 2269 | if ((s->uptodate == disks - 1) && |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2270 | (s->failed && disk_idx == s->failed_num)) { |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2271 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2272 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2273 | set_bit(R5_Wantcompute, &dev->flags); |
| 2274 | sh->ops.target = disk_idx; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2275 | sh->ops.target2 = -1; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2276 | s->req_compute = 1; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2277 | /* Careful: from this point on 'uptodate' is in the eye |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2278 | * of raid_run_ops which services 'compute' operations |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2279 | * before writes. R5_Wantcompute flags a block that will |
| 2280 | * be R5_UPTODATE by the time it is needed for a |
| 2281 | * subsequent operation. |
| 2282 | */ |
| 2283 | s->uptodate++; |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2284 | return 1; /* uptodate + compute == disks */ |
Dan Williams | 7a1fc53 | 2008-07-10 04:54:57 -0700 | [diff] [blame] | 2285 | } else if (test_bit(R5_Insync, &dev->flags)) { |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2286 | set_bit(R5_LOCKED, &dev->flags); |
| 2287 | set_bit(R5_Wantread, &dev->flags); |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2288 | s->locked++; |
| 2289 | pr_debug("Reading block %d (sync=%d)\n", disk_idx, |
| 2290 | s->syncing); |
| 2291 | } |
| 2292 | } |
| 2293 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2294 | return 0; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2295 | } |
| 2296 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2297 | /** |
| 2298 | * handle_stripe_fill5 - read or compute data to satisfy pending requests. |
| 2299 | */ |
| 2300 | static void handle_stripe_fill5(struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2301 | struct stripe_head_state *s, int disks) |
| 2302 | { |
| 2303 | int i; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2304 | |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2305 | /* look for blocks to read/compute, skip this if a compute |
| 2306 | * is already in flight, or if the stripe contents are in the |
| 2307 | * midst of changing due to a write |
| 2308 | */ |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2309 | if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state && |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2310 | !sh->reconstruct_state) |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2311 | for (i = disks; i--; ) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2312 | if (fetch_block5(sh, s, i, disks)) |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2313 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2314 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2315 | } |
| 2316 | |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2317 | /* fetch_block6 - checks the given member device to see if its data needs |
| 2318 | * to be read or computed to satisfy a request. |
| 2319 | * |
| 2320 | * Returns 1 when no more member devices need to be checked, otherwise returns |
| 2321 | * 0 to tell the loop in handle_stripe_fill6 to continue |
| 2322 | */ |
| 2323 | static int fetch_block6(struct stripe_head *sh, struct stripe_head_state *s, |
| 2324 | struct r6_state *r6s, int disk_idx, int disks) |
| 2325 | { |
| 2326 | struct r5dev *dev = &sh->dev[disk_idx]; |
| 2327 | struct r5dev *fdev[2] = { &sh->dev[r6s->failed_num[0]], |
| 2328 | &sh->dev[r6s->failed_num[1]] }; |
| 2329 | |
| 2330 | if (!test_bit(R5_LOCKED, &dev->flags) && |
| 2331 | !test_bit(R5_UPTODATE, &dev->flags) && |
| 2332 | (dev->toread || |
| 2333 | (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) || |
| 2334 | s->syncing || s->expanding || |
| 2335 | (s->failed >= 1 && |
| 2336 | (fdev[0]->toread || s->to_write)) || |
| 2337 | (s->failed >= 2 && |
| 2338 | (fdev[1]->toread || s->to_write)))) { |
| 2339 | /* we would like to get this block, possibly by computing it, |
| 2340 | * otherwise read it if the backing disk is insync |
| 2341 | */ |
| 2342 | BUG_ON(test_bit(R5_Wantcompute, &dev->flags)); |
| 2343 | BUG_ON(test_bit(R5_Wantread, &dev->flags)); |
| 2344 | if ((s->uptodate == disks - 1) && |
| 2345 | (s->failed && (disk_idx == r6s->failed_num[0] || |
| 2346 | disk_idx == r6s->failed_num[1]))) { |
| 2347 | /* have disk failed, and we're requested to fetch it; |
| 2348 | * do compute it |
| 2349 | */ |
| 2350 | pr_debug("Computing stripe %llu block %d\n", |
| 2351 | (unsigned long long)sh->sector, disk_idx); |
| 2352 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2353 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2354 | set_bit(R5_Wantcompute, &dev->flags); |
| 2355 | sh->ops.target = disk_idx; |
| 2356 | sh->ops.target2 = -1; /* no 2nd target */ |
| 2357 | s->req_compute = 1; |
| 2358 | s->uptodate++; |
| 2359 | return 1; |
| 2360 | } else if (s->uptodate == disks-2 && s->failed >= 2) { |
| 2361 | /* Computing 2-failure is *very* expensive; only |
| 2362 | * do it if failed >= 2 |
| 2363 | */ |
| 2364 | int other; |
| 2365 | for (other = disks; other--; ) { |
| 2366 | if (other == disk_idx) |
| 2367 | continue; |
| 2368 | if (!test_bit(R5_UPTODATE, |
| 2369 | &sh->dev[other].flags)) |
| 2370 | break; |
| 2371 | } |
| 2372 | BUG_ON(other < 0); |
| 2373 | pr_debug("Computing stripe %llu blocks %d,%d\n", |
| 2374 | (unsigned long long)sh->sector, |
| 2375 | disk_idx, other); |
| 2376 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2377 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2378 | set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags); |
| 2379 | set_bit(R5_Wantcompute, &sh->dev[other].flags); |
| 2380 | sh->ops.target = disk_idx; |
| 2381 | sh->ops.target2 = other; |
| 2382 | s->uptodate += 2; |
| 2383 | s->req_compute = 1; |
| 2384 | return 1; |
| 2385 | } else if (test_bit(R5_Insync, &dev->flags)) { |
| 2386 | set_bit(R5_LOCKED, &dev->flags); |
| 2387 | set_bit(R5_Wantread, &dev->flags); |
| 2388 | s->locked++; |
| 2389 | pr_debug("Reading block %d (sync=%d)\n", |
| 2390 | disk_idx, s->syncing); |
| 2391 | } |
| 2392 | } |
| 2393 | |
| 2394 | return 0; |
| 2395 | } |
| 2396 | |
| 2397 | /** |
| 2398 | * handle_stripe_fill6 - read or compute data to satisfy pending requests. |
| 2399 | */ |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2400 | static void handle_stripe_fill6(struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2401 | struct stripe_head_state *s, struct r6_state *r6s, |
| 2402 | int disks) |
| 2403 | { |
| 2404 | int i; |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2405 | |
| 2406 | /* look for blocks to read/compute, skip this if a compute |
| 2407 | * is already in flight, or if the stripe contents are in the |
| 2408 | * midst of changing due to a write |
| 2409 | */ |
| 2410 | if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state && |
| 2411 | !sh->reconstruct_state) |
| 2412 | for (i = disks; i--; ) |
| 2413 | if (fetch_block6(sh, s, r6s, i, disks)) |
| 2414 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2415 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2416 | } |
| 2417 | |
| 2418 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2419 | /* handle_stripe_clean_event |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2420 | * any written block on an uptodate or failed drive can be returned. |
| 2421 | * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but |
| 2422 | * never LOCKED, so we don't need to test 'failed' directly. |
| 2423 | */ |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2424 | static void handle_stripe_clean_event(raid5_conf_t *conf, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2425 | struct stripe_head *sh, int disks, struct bio **return_bi) |
| 2426 | { |
| 2427 | int i; |
| 2428 | struct r5dev *dev; |
| 2429 | |
| 2430 | for (i = disks; i--; ) |
| 2431 | if (sh->dev[i].written) { |
| 2432 | dev = &sh->dev[i]; |
| 2433 | if (!test_bit(R5_LOCKED, &dev->flags) && |
| 2434 | test_bit(R5_UPTODATE, &dev->flags)) { |
| 2435 | /* We can return any write requests */ |
| 2436 | struct bio *wbi, *wbi2; |
| 2437 | int bitmap_end = 0; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2438 | pr_debug("Return write for disc %d\n", i); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2439 | spin_lock_irq(&conf->device_lock); |
| 2440 | wbi = dev->written; |
| 2441 | dev->written = NULL; |
| 2442 | while (wbi && wbi->bi_sector < |
| 2443 | dev->sector + STRIPE_SECTORS) { |
| 2444 | wbi2 = r5_next_bio(wbi, dev->sector); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2445 | if (!raid5_dec_bi_phys_segments(wbi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2446 | md_write_end(conf->mddev); |
| 2447 | wbi->bi_next = *return_bi; |
| 2448 | *return_bi = wbi; |
| 2449 | } |
| 2450 | wbi = wbi2; |
| 2451 | } |
| 2452 | if (dev->towrite == NULL) |
| 2453 | bitmap_end = 1; |
| 2454 | spin_unlock_irq(&conf->device_lock); |
| 2455 | if (bitmap_end) |
| 2456 | bitmap_endwrite(conf->mddev->bitmap, |
| 2457 | sh->sector, |
| 2458 | STRIPE_SECTORS, |
| 2459 | !test_bit(STRIPE_DEGRADED, &sh->state), |
| 2460 | 0); |
| 2461 | } |
| 2462 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2463 | |
| 2464 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 2465 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 2466 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2467 | } |
| 2468 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2469 | static void handle_stripe_dirtying5(raid5_conf_t *conf, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2470 | struct stripe_head *sh, struct stripe_head_state *s, int disks) |
| 2471 | { |
| 2472 | int rmw = 0, rcw = 0, i; |
| 2473 | for (i = disks; i--; ) { |
| 2474 | /* would I have to read this buffer for read_modify_write */ |
| 2475 | struct r5dev *dev = &sh->dev[i]; |
| 2476 | if ((dev->towrite || i == sh->pd_idx) && |
| 2477 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2478 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2479 | test_bit(R5_Wantcompute, &dev->flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2480 | if (test_bit(R5_Insync, &dev->flags)) |
| 2481 | rmw++; |
| 2482 | else |
| 2483 | rmw += 2*disks; /* cannot read it */ |
| 2484 | } |
| 2485 | /* Would I have to read this buffer for reconstruct_write */ |
| 2486 | if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx && |
| 2487 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2488 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2489 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2490 | if (test_bit(R5_Insync, &dev->flags)) rcw++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2491 | else |
| 2492 | rcw += 2*disks; |
| 2493 | } |
| 2494 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2495 | pr_debug("for sector %llu, rmw=%d rcw=%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2496 | (unsigned long long)sh->sector, rmw, rcw); |
| 2497 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2498 | if (rmw < rcw && rmw > 0) |
| 2499 | /* prefer read-modify-write, but need to get some data */ |
| 2500 | for (i = disks; i--; ) { |
| 2501 | struct r5dev *dev = &sh->dev[i]; |
| 2502 | if ((dev->towrite || i == sh->pd_idx) && |
| 2503 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2504 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2505 | test_bit(R5_Wantcompute, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2506 | test_bit(R5_Insync, &dev->flags)) { |
| 2507 | if ( |
| 2508 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2509 | pr_debug("Read_old block " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2510 | "%d for r-m-w\n", i); |
| 2511 | set_bit(R5_LOCKED, &dev->flags); |
| 2512 | set_bit(R5_Wantread, &dev->flags); |
| 2513 | s->locked++; |
| 2514 | } else { |
| 2515 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2516 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2517 | } |
| 2518 | } |
| 2519 | } |
| 2520 | if (rcw <= rmw && rcw > 0) |
| 2521 | /* want reconstruct write, but need to get some data */ |
| 2522 | for (i = disks; i--; ) { |
| 2523 | struct r5dev *dev = &sh->dev[i]; |
| 2524 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
| 2525 | i != sh->pd_idx && |
| 2526 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2527 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2528 | test_bit(R5_Wantcompute, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2529 | test_bit(R5_Insync, &dev->flags)) { |
| 2530 | if ( |
| 2531 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2532 | pr_debug("Read_old block " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2533 | "%d for Reconstruct\n", i); |
| 2534 | set_bit(R5_LOCKED, &dev->flags); |
| 2535 | set_bit(R5_Wantread, &dev->flags); |
| 2536 | s->locked++; |
| 2537 | } else { |
| 2538 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2539 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2540 | } |
| 2541 | } |
| 2542 | } |
| 2543 | /* now if nothing is locked, and if we have enough data, |
| 2544 | * we can start a write request |
| 2545 | */ |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2546 | /* since handle_stripe can be called at any time we need to handle the |
| 2547 | * case where a compute block operation has been submitted and then a |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2548 | * subsequent call wants to start a write request. raid_run_ops only |
| 2549 | * handles the case where compute block and reconstruct are requested |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2550 | * simultaneously. If this is not the case then new writes need to be |
| 2551 | * held off until the compute completes. |
| 2552 | */ |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2553 | if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) && |
| 2554 | (s->locked == 0 && (rcw == 0 || rmw == 0) && |
| 2555 | !test_bit(STRIPE_BIT_DELAY, &sh->state))) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2556 | schedule_reconstruction(sh, s, rcw == 0, 0); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2557 | } |
| 2558 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2559 | static void handle_stripe_dirtying6(raid5_conf_t *conf, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2560 | struct stripe_head *sh, struct stripe_head_state *s, |
| 2561 | struct r6_state *r6s, int disks) |
| 2562 | { |
Yuri Tikhonov | a9b39a7 | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2563 | int rcw = 0, pd_idx = sh->pd_idx, i; |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 2564 | int qd_idx = sh->qd_idx; |
Yuri Tikhonov | a9b39a7 | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2565 | |
| 2566 | set_bit(STRIPE_HANDLE, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2567 | for (i = disks; i--; ) { |
| 2568 | struct r5dev *dev = &sh->dev[i]; |
Yuri Tikhonov | a9b39a7 | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2569 | /* check if we haven't enough data */ |
| 2570 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
| 2571 | i != pd_idx && i != qd_idx && |
| 2572 | !test_bit(R5_LOCKED, &dev->flags) && |
| 2573 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2574 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2575 | rcw++; |
| 2576 | if (!test_bit(R5_Insync, &dev->flags)) |
| 2577 | continue; /* it's a failed drive */ |
| 2578 | |
| 2579 | if ( |
| 2580 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
| 2581 | pr_debug("Read_old stripe %llu " |
| 2582 | "block %d for Reconstruct\n", |
| 2583 | (unsigned long long)sh->sector, i); |
| 2584 | set_bit(R5_LOCKED, &dev->flags); |
| 2585 | set_bit(R5_Wantread, &dev->flags); |
| 2586 | s->locked++; |
| 2587 | } else { |
| 2588 | pr_debug("Request delayed stripe %llu " |
| 2589 | "block %d for Reconstruct\n", |
| 2590 | (unsigned long long)sh->sector, i); |
| 2591 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2592 | set_bit(STRIPE_HANDLE, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2593 | } |
| 2594 | } |
| 2595 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2596 | /* now if nothing is locked, and if we have enough data, we can start a |
| 2597 | * write request |
| 2598 | */ |
Yuri Tikhonov | a9b39a7 | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2599 | if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) && |
| 2600 | s->locked == 0 && rcw == 0 && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2601 | !test_bit(STRIPE_BIT_DELAY, &sh->state)) { |
Yuri Tikhonov | a9b39a7 | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2602 | schedule_reconstruction(sh, s, 1, 0); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2603 | } |
| 2604 | } |
| 2605 | |
| 2606 | static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh, |
| 2607 | struct stripe_head_state *s, int disks) |
| 2608 | { |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2609 | struct r5dev *dev = NULL; |
Dan Williams | e89f896 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2610 | |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2611 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2612 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2613 | switch (sh->check_state) { |
| 2614 | case check_state_idle: |
| 2615 | /* start a new check operation if there are no failures */ |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2616 | if (s->failed == 0) { |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2617 | BUG_ON(s->uptodate != disks); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2618 | sh->check_state = check_state_run; |
| 2619 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2620 | clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2621 | s->uptodate--; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2622 | break; |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2623 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2624 | dev = &sh->dev[s->failed_num]; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2625 | /* fall through */ |
| 2626 | case check_state_compute_result: |
| 2627 | sh->check_state = check_state_idle; |
| 2628 | if (!dev) |
| 2629 | dev = &sh->dev[sh->pd_idx]; |
| 2630 | |
| 2631 | /* check that a write has not made the stripe insync */ |
| 2632 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 2633 | break; |
| 2634 | |
| 2635 | /* either failed parity check, or recovery is happening */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2636 | BUG_ON(!test_bit(R5_UPTODATE, &dev->flags)); |
| 2637 | BUG_ON(s->uptodate != disks); |
| 2638 | |
| 2639 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2640 | s->locked++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2641 | set_bit(R5_Wantwrite, &dev->flags); |
Dan Williams | 830ea01 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2642 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2643 | clear_bit(STRIPE_DEGRADED, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2644 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2645 | break; |
| 2646 | case check_state_run: |
| 2647 | break; /* we will be called again upon completion */ |
| 2648 | case check_state_check_result: |
| 2649 | sh->check_state = check_state_idle; |
| 2650 | |
| 2651 | /* if a failure occurred during the check operation, leave |
| 2652 | * STRIPE_INSYNC not set and let the stripe be handled again |
| 2653 | */ |
| 2654 | if (s->failed) |
| 2655 | break; |
| 2656 | |
| 2657 | /* handle a successful check operation, if parity is correct |
| 2658 | * we are done. Otherwise update the mismatch count and repair |
| 2659 | * parity if !MD_RECOVERY_CHECK |
| 2660 | */ |
Dan Williams | ad283ea | 2009-08-29 19:09:26 -0700 | [diff] [blame] | 2661 | if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0) |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2662 | /* parity is correct (on disc, |
| 2663 | * not in buffer any more) |
| 2664 | */ |
| 2665 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2666 | else { |
| 2667 | conf->mddev->resync_mismatches += STRIPE_SECTORS; |
| 2668 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 2669 | /* don't try to repair!! */ |
| 2670 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2671 | else { |
| 2672 | sh->check_state = check_state_compute_run; |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2673 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2674 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2675 | set_bit(R5_Wantcompute, |
| 2676 | &sh->dev[sh->pd_idx].flags); |
| 2677 | sh->ops.target = sh->pd_idx; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2678 | sh->ops.target2 = -1; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2679 | s->uptodate++; |
| 2680 | } |
| 2681 | } |
| 2682 | break; |
| 2683 | case check_state_compute_run: |
| 2684 | break; |
| 2685 | default: |
| 2686 | printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n", |
| 2687 | __func__, sh->check_state, |
| 2688 | (unsigned long long) sh->sector); |
| 2689 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2690 | } |
| 2691 | } |
| 2692 | |
| 2693 | |
| 2694 | 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] | 2695 | struct stripe_head_state *s, |
| 2696 | struct r6_state *r6s, int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2697 | { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2698 | int pd_idx = sh->pd_idx; |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 2699 | int qd_idx = sh->qd_idx; |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2700 | struct r5dev *dev; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2701 | |
| 2702 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2703 | |
| 2704 | BUG_ON(s->failed > 2); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2705 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2706 | /* Want to check and possibly repair P and Q. |
| 2707 | * However there could be one 'failed' device, in which |
| 2708 | * case we can only check one of them, possibly using the |
| 2709 | * other to generate missing data |
| 2710 | */ |
| 2711 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2712 | switch (sh->check_state) { |
| 2713 | case check_state_idle: |
| 2714 | /* start a new check operation if there are < 2 failures */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2715 | if (s->failed == r6s->q_failed) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2716 | /* The only possible failed device holds Q, so it |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2717 | * makes sense to check P (If anything else were failed, |
| 2718 | * we would have used P to recreate it). |
| 2719 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2720 | sh->check_state = check_state_run; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2721 | } |
| 2722 | if (!r6s->q_failed && s->failed < 2) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2723 | /* Q is not failed, and we didn't use it to generate |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2724 | * anything, so it makes sense to check it |
| 2725 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2726 | if (sh->check_state == check_state_run) |
| 2727 | sh->check_state = check_state_run_pq; |
| 2728 | else |
| 2729 | sh->check_state = check_state_run_q; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2730 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2731 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2732 | /* discard potentially stale zero_sum_result */ |
| 2733 | sh->ops.zero_sum_result = 0; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2734 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2735 | if (sh->check_state == check_state_run) { |
| 2736 | /* async_xor_zero_sum destroys the contents of P */ |
| 2737 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
| 2738 | s->uptodate--; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2739 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2740 | if (sh->check_state >= check_state_run && |
| 2741 | sh->check_state <= check_state_run_pq) { |
| 2742 | /* async_syndrome_zero_sum preserves P and Q, so |
| 2743 | * no need to mark them !uptodate here |
| 2744 | */ |
| 2745 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
| 2746 | break; |
| 2747 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2748 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2749 | /* we have 2-disk failure */ |
| 2750 | BUG_ON(s->failed != 2); |
| 2751 | /* fall through */ |
| 2752 | case check_state_compute_result: |
| 2753 | sh->check_state = check_state_idle; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2754 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2755 | /* check that a write has not made the stripe insync */ |
| 2756 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 2757 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2758 | |
| 2759 | /* now write out any block on a failed drive, |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2760 | * or P or Q if they were recomputed |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2761 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2762 | 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] | 2763 | if (s->failed == 2) { |
| 2764 | dev = &sh->dev[r6s->failed_num[1]]; |
| 2765 | s->locked++; |
| 2766 | set_bit(R5_LOCKED, &dev->flags); |
| 2767 | set_bit(R5_Wantwrite, &dev->flags); |
| 2768 | } |
| 2769 | if (s->failed >= 1) { |
| 2770 | dev = &sh->dev[r6s->failed_num[0]]; |
| 2771 | s->locked++; |
| 2772 | set_bit(R5_LOCKED, &dev->flags); |
| 2773 | set_bit(R5_Wantwrite, &dev->flags); |
| 2774 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2775 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2776 | dev = &sh->dev[pd_idx]; |
| 2777 | s->locked++; |
| 2778 | set_bit(R5_LOCKED, &dev->flags); |
| 2779 | set_bit(R5_Wantwrite, &dev->flags); |
| 2780 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2781 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2782 | dev = &sh->dev[qd_idx]; |
| 2783 | s->locked++; |
| 2784 | set_bit(R5_LOCKED, &dev->flags); |
| 2785 | set_bit(R5_Wantwrite, &dev->flags); |
| 2786 | } |
| 2787 | clear_bit(STRIPE_DEGRADED, &sh->state); |
| 2788 | |
| 2789 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2790 | break; |
| 2791 | case check_state_run: |
| 2792 | case check_state_run_q: |
| 2793 | case check_state_run_pq: |
| 2794 | break; /* we will be called again upon completion */ |
| 2795 | case check_state_check_result: |
| 2796 | sh->check_state = check_state_idle; |
| 2797 | |
| 2798 | /* handle a successful check operation, if parity is correct |
| 2799 | * we are done. Otherwise update the mismatch count and repair |
| 2800 | * parity if !MD_RECOVERY_CHECK |
| 2801 | */ |
| 2802 | if (sh->ops.zero_sum_result == 0) { |
| 2803 | /* both parities are correct */ |
| 2804 | if (!s->failed) |
| 2805 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2806 | else { |
| 2807 | /* in contrast to the raid5 case we can validate |
| 2808 | * parity, but still have a failure to write |
| 2809 | * back |
| 2810 | */ |
| 2811 | sh->check_state = check_state_compute_result; |
| 2812 | /* Returning at this point means that we may go |
| 2813 | * off and bring p and/or q uptodate again so |
| 2814 | * we make sure to check zero_sum_result again |
| 2815 | * to verify if p or q need writeback |
| 2816 | */ |
| 2817 | } |
| 2818 | } else { |
| 2819 | conf->mddev->resync_mismatches += STRIPE_SECTORS; |
| 2820 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 2821 | /* don't try to repair!! */ |
| 2822 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2823 | else { |
| 2824 | int *target = &sh->ops.target; |
| 2825 | |
| 2826 | sh->ops.target = -1; |
| 2827 | sh->ops.target2 = -1; |
| 2828 | sh->check_state = check_state_compute_run; |
| 2829 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2830 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2831 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
| 2832 | set_bit(R5_Wantcompute, |
| 2833 | &sh->dev[pd_idx].flags); |
| 2834 | *target = pd_idx; |
| 2835 | target = &sh->ops.target2; |
| 2836 | s->uptodate++; |
| 2837 | } |
| 2838 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
| 2839 | set_bit(R5_Wantcompute, |
| 2840 | &sh->dev[qd_idx].flags); |
| 2841 | *target = qd_idx; |
| 2842 | s->uptodate++; |
| 2843 | } |
| 2844 | } |
| 2845 | } |
| 2846 | break; |
| 2847 | case check_state_compute_run: |
| 2848 | break; |
| 2849 | default: |
| 2850 | printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n", |
| 2851 | __func__, sh->check_state, |
| 2852 | (unsigned long long) sh->sector); |
| 2853 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2854 | } |
| 2855 | } |
| 2856 | |
| 2857 | static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh, |
| 2858 | struct r6_state *r6s) |
| 2859 | { |
| 2860 | int i; |
| 2861 | |
| 2862 | /* We have read all the blocks in this stripe and now we need to |
| 2863 | * copy some of them into a target stripe for expand. |
| 2864 | */ |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2865 | struct dma_async_tx_descriptor *tx = NULL; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2866 | clear_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 2867 | for (i = 0; i < sh->disks; i++) |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 2868 | if (i != sh->pd_idx && i != sh->qd_idx) { |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2869 | int dd_idx, j; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2870 | struct stripe_head *sh2; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 2871 | struct async_submit_ctl submit; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2872 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2873 | sector_t bn = compute_blocknr(sh, i, 1); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2874 | sector_t s = raid5_compute_sector(conf, bn, 0, |
| 2875 | &dd_idx, NULL); |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 2876 | sh2 = get_active_stripe(conf, s, 0, 1, 1); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2877 | if (sh2 == NULL) |
| 2878 | /* so far only the early blocks of this stripe |
| 2879 | * have been requested. When later blocks |
| 2880 | * get requested, we will try again |
| 2881 | */ |
| 2882 | continue; |
| 2883 | if (!test_bit(STRIPE_EXPANDING, &sh2->state) || |
| 2884 | test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) { |
| 2885 | /* must have already done this block */ |
| 2886 | release_stripe(sh2); |
| 2887 | continue; |
| 2888 | } |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2889 | |
| 2890 | /* place all the copies on one channel */ |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 2891 | init_async_submit(&submit, 0, tx, NULL, NULL, NULL); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2892 | tx = async_memcpy(sh2->dev[dd_idx].page, |
Dan Williams | 88ba2aa | 2009-04-09 16:16:18 -0700 | [diff] [blame] | 2893 | sh->dev[i].page, 0, 0, STRIPE_SIZE, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 2894 | &submit); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2895 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2896 | set_bit(R5_Expanded, &sh2->dev[dd_idx].flags); |
| 2897 | set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags); |
| 2898 | for (j = 0; j < conf->raid_disks; j++) |
| 2899 | if (j != sh2->pd_idx && |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2900 | (!r6s || j != sh2->qd_idx) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2901 | !test_bit(R5_Expanded, &sh2->dev[j].flags)) |
| 2902 | break; |
| 2903 | if (j == conf->raid_disks) { |
| 2904 | set_bit(STRIPE_EXPAND_READY, &sh2->state); |
| 2905 | set_bit(STRIPE_HANDLE, &sh2->state); |
| 2906 | } |
| 2907 | release_stripe(sh2); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2908 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2909 | } |
NeilBrown | a2e0855 | 2007-09-11 15:23:36 -0700 | [diff] [blame] | 2910 | /* done submitting copies, wait for them to complete */ |
| 2911 | if (tx) { |
| 2912 | async_tx_ack(tx); |
| 2913 | dma_wait_for_async_tx(tx); |
| 2914 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2915 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2916 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2917 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2918 | /* |
| 2919 | * handle_stripe - do things to a stripe. |
| 2920 | * |
| 2921 | * We lock the stripe and then examine the state of various bits |
| 2922 | * to see what needs to be done. |
| 2923 | * Possible results: |
| 2924 | * return some read request which now have data |
| 2925 | * return some write requests which are safely on disc |
| 2926 | * schedule a read on some buffers |
| 2927 | * schedule a write of some buffers |
| 2928 | * return confirmation of parity correctness |
| 2929 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2930 | * buffers are taken off read_list or write_list, and bh_cache buffers |
| 2931 | * get BH_Lock set before the stripe lock is released. |
| 2932 | * |
| 2933 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2934 | |
NeilBrown | 1442577 | 2009-10-16 15:55:25 +1100 | [diff] [blame] | 2935 | static void handle_stripe5(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2936 | { |
| 2937 | raid5_conf_t *conf = sh->raid_conf; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2938 | int disks = sh->disks, i; |
| 2939 | struct bio *return_bi = NULL; |
| 2940 | struct stripe_head_state s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2941 | struct r5dev *dev; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2942 | mdk_rdev_t *blocked_rdev = NULL; |
Dan Williams | e0a115e | 2008-06-05 22:45:52 -0700 | [diff] [blame] | 2943 | int prexor; |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 2944 | int dec_preread_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2945 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2946 | memset(&s, 0, sizeof(s)); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2947 | pr_debug("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d check:%d " |
| 2948 | "reconstruct:%d\n", (unsigned long long)sh->sector, sh->state, |
| 2949 | atomic_read(&sh->count), sh->pd_idx, sh->check_state, |
| 2950 | sh->reconstruct_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2951 | |
| 2952 | spin_lock(&sh->lock); |
| 2953 | clear_bit(STRIPE_HANDLE, &sh->state); |
| 2954 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 2955 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2956 | s.syncing = test_bit(STRIPE_SYNCING, &sh->state); |
| 2957 | s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 2958 | s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state); |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 2959 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2960 | /* Now to look around and see what can be done */ |
NeilBrown | 9910f16 | 2006-01-06 00:20:24 -0800 | [diff] [blame] | 2961 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2962 | for (i=disks; i--; ) { |
| 2963 | mdk_rdev_t *rdev; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 2964 | |
| 2965 | dev = &sh->dev[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2966 | clear_bit(R5_Insync, &dev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2967 | |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2968 | pr_debug("check %d: state 0x%lx toread %p read %p write %p " |
| 2969 | "written %p\n", i, dev->flags, dev->toread, dev->read, |
| 2970 | dev->towrite, dev->written); |
| 2971 | |
| 2972 | /* maybe we can request a biofill operation |
| 2973 | * |
| 2974 | * new wantfill requests are only permitted while |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 2975 | * ops_complete_biofill is guaranteed to be inactive |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2976 | */ |
| 2977 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread && |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 2978 | !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2979 | set_bit(R5_Wantfill, &dev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2980 | |
| 2981 | /* now count some things */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2982 | if (test_bit(R5_LOCKED, &dev->flags)) s.locked++; |
| 2983 | if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2984 | if (test_bit(R5_Wantcompute, &dev->flags)) s.compute++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2985 | |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2986 | if (test_bit(R5_Wantfill, &dev->flags)) |
| 2987 | s.to_fill++; |
| 2988 | else if (dev->toread) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2989 | s.to_read++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2990 | if (dev->towrite) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2991 | s.to_write++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2992 | if (!test_bit(R5_OVERWRITE, &dev->flags)) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2993 | s.non_overwrite++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2994 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2995 | if (dev->written) |
| 2996 | s.written++; |
NeilBrown | 9910f16 | 2006-01-06 00:20:24 -0800 | [diff] [blame] | 2997 | rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | ac4090d | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2998 | if (blocked_rdev == NULL && |
| 2999 | rdev && unlikely(test_bit(Blocked, &rdev->flags))) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3000 | blocked_rdev = rdev; |
| 3001 | atomic_inc(&rdev->nr_pending); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3002 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3003 | if (!rdev || !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 3004 | /* The ReadError flag will just be confusing now */ |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 3005 | clear_bit(R5_ReadError, &dev->flags); |
| 3006 | clear_bit(R5_ReWrite, &dev->flags); |
| 3007 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3008 | if (!rdev || !test_bit(In_sync, &rdev->flags) |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 3009 | || test_bit(R5_ReadError, &dev->flags)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3010 | s.failed++; |
| 3011 | s.failed_num = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3012 | } else |
| 3013 | set_bit(R5_Insync, &dev->flags); |
| 3014 | } |
NeilBrown | 9910f16 | 2006-01-06 00:20:24 -0800 | [diff] [blame] | 3015 | rcu_read_unlock(); |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3016 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3017 | if (unlikely(blocked_rdev)) { |
NeilBrown | ac4090d | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3018 | if (s.syncing || s.expanding || s.expanded || |
| 3019 | s.to_write || s.written) { |
| 3020 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3021 | goto unlock; |
| 3022 | } |
| 3023 | /* There is nothing for the blocked_rdev to block */ |
| 3024 | rdev_dec_pending(blocked_rdev, conf->mddev); |
| 3025 | blocked_rdev = NULL; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3026 | } |
| 3027 | |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 3028 | if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) { |
| 3029 | set_bit(STRIPE_OP_BIOFILL, &s.ops_request); |
| 3030 | set_bit(STRIPE_BIOFILL_RUN, &sh->state); |
| 3031 | } |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3032 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3033 | pr_debug("locked=%d uptodate=%d to_read=%d" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3034 | " to_write=%d failed=%d failed_num=%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3035 | s.locked, s.uptodate, s.to_read, s.to_write, |
| 3036 | s.failed, s.failed_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3037 | /* check if the array has lost two devices and, if so, some requests might |
| 3038 | * need to be failed |
| 3039 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3040 | if (s.failed > 1 && s.to_read+s.to_write+s.written) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3041 | handle_failed_stripe(conf, sh, &s, disks, &return_bi); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3042 | if (s.failed > 1 && s.syncing) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3043 | md_done_sync(conf->mddev, STRIPE_SECTORS,0); |
| 3044 | clear_bit(STRIPE_SYNCING, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3045 | s.syncing = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3046 | } |
| 3047 | |
| 3048 | /* might be able to return some write requests if the parity block |
| 3049 | * is safe, or on a failed drive |
| 3050 | */ |
| 3051 | dev = &sh->dev[sh->pd_idx]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3052 | if ( s.written && |
| 3053 | ((test_bit(R5_Insync, &dev->flags) && |
| 3054 | !test_bit(R5_LOCKED, &dev->flags) && |
| 3055 | test_bit(R5_UPTODATE, &dev->flags)) || |
| 3056 | (s.failed == 1 && s.failed_num == sh->pd_idx))) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3057 | handle_stripe_clean_event(conf, sh, disks, &return_bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3058 | |
| 3059 | /* Now we might consider reading some blocks, either to check/generate |
| 3060 | * parity, or to satisfy requests |
| 3061 | * or to load a block that is being partially written. |
| 3062 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3063 | if (s.to_read || s.non_overwrite || |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 3064 | (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3065 | handle_stripe_fill5(sh, &s, disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3066 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3067 | /* Now we check to see if any write operations have recently |
| 3068 | * completed |
| 3069 | */ |
Dan Williams | e0a115e | 2008-06-05 22:45:52 -0700 | [diff] [blame] | 3070 | prexor = 0; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 3071 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_result) |
Dan Williams | e0a115e | 2008-06-05 22:45:52 -0700 | [diff] [blame] | 3072 | prexor = 1; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 3073 | if (sh->reconstruct_state == reconstruct_state_drain_result || |
| 3074 | sh->reconstruct_state == reconstruct_state_prexor_drain_result) { |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3075 | sh->reconstruct_state = reconstruct_state_idle; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3076 | |
| 3077 | /* All the 'written' buffers and the parity block are ready to |
| 3078 | * be written back to disk |
| 3079 | */ |
| 3080 | BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags)); |
| 3081 | for (i = disks; i--; ) { |
| 3082 | dev = &sh->dev[i]; |
| 3083 | if (test_bit(R5_LOCKED, &dev->flags) && |
| 3084 | (i == sh->pd_idx || dev->written)) { |
| 3085 | pr_debug("Writing block %d\n", i); |
| 3086 | set_bit(R5_Wantwrite, &dev->flags); |
Dan Williams | e0a115e | 2008-06-05 22:45:52 -0700 | [diff] [blame] | 3087 | if (prexor) |
| 3088 | continue; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3089 | if (!test_bit(R5_Insync, &dev->flags) || |
| 3090 | (i == sh->pd_idx && s.failed == 0)) |
| 3091 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3092 | } |
| 3093 | } |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 3094 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 3095 | dec_preread_active = 1; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3096 | } |
| 3097 | |
| 3098 | /* Now to consider new write requests and what else, if anything |
| 3099 | * should be read. We do not handle new writes when: |
| 3100 | * 1/ A 'write' operation (copy+xor) is already in flight. |
| 3101 | * 2/ A 'check' operation is in flight, as it may clobber the parity |
| 3102 | * block. |
| 3103 | */ |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3104 | if (s.to_write && !sh->reconstruct_state && !sh->check_state) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3105 | handle_stripe_dirtying5(conf, sh, &s, disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3106 | |
| 3107 | /* 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] | 3108 | * Any reads will already have been scheduled, so we just see if enough |
| 3109 | * data is available. The parity check is held off while parity |
| 3110 | * dependent operations are in flight. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3111 | */ |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3112 | if (sh->check_state || |
| 3113 | (s.syncing && s.locked == 0 && |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 3114 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3115 | !test_bit(STRIPE_INSYNC, &sh->state))) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3116 | handle_parity_checks5(conf, sh, &s, disks); |
Dan Williams | e89f896 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3117 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3118 | if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3119 | md_done_sync(conf->mddev, STRIPE_SECTORS,1); |
| 3120 | clear_bit(STRIPE_SYNCING, &sh->state); |
| 3121 | } |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 3122 | |
| 3123 | /* If the failed drive is just a ReadError, then we might need to progress |
| 3124 | * the repair/check process |
| 3125 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3126 | if (s.failed == 1 && !conf->mddev->ro && |
| 3127 | test_bit(R5_ReadError, &sh->dev[s.failed_num].flags) |
| 3128 | && !test_bit(R5_LOCKED, &sh->dev[s.failed_num].flags) |
| 3129 | && test_bit(R5_UPTODATE, &sh->dev[s.failed_num].flags) |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 3130 | ) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3131 | dev = &sh->dev[s.failed_num]; |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 3132 | if (!test_bit(R5_ReWrite, &dev->flags)) { |
| 3133 | set_bit(R5_Wantwrite, &dev->flags); |
| 3134 | set_bit(R5_ReWrite, &dev->flags); |
| 3135 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3136 | s.locked++; |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 3137 | } else { |
| 3138 | /* let's read it back */ |
| 3139 | set_bit(R5_Wantread, &dev->flags); |
| 3140 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3141 | s.locked++; |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 3142 | } |
| 3143 | } |
| 3144 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3145 | /* Finish reconstruct operations initiated by the expansion process */ |
| 3146 | if (sh->reconstruct_state == reconstruct_state_result) { |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 3147 | struct stripe_head *sh2 |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 3148 | = get_active_stripe(conf, sh->sector, 1, 1, 1); |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 3149 | if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) { |
| 3150 | /* sh cannot be written until sh2 has been read. |
| 3151 | * so arrange for sh to be delayed a little |
| 3152 | */ |
| 3153 | set_bit(STRIPE_DELAYED, &sh->state); |
| 3154 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3155 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, |
| 3156 | &sh2->state)) |
| 3157 | atomic_inc(&conf->preread_active_stripes); |
| 3158 | release_stripe(sh2); |
| 3159 | goto unlock; |
| 3160 | } |
| 3161 | if (sh2) |
| 3162 | release_stripe(sh2); |
| 3163 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3164 | sh->reconstruct_state = reconstruct_state_idle; |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3165 | clear_bit(STRIPE_EXPANDING, &sh->state); |
Dan Williams | 2339788 | 2008-07-23 20:05:34 -0700 | [diff] [blame] | 3166 | for (i = conf->raid_disks; i--; ) { |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3167 | set_bit(R5_Wantwrite, &sh->dev[i].flags); |
Dan Williams | 2339788 | 2008-07-23 20:05:34 -0700 | [diff] [blame] | 3168 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
Neil Brown | efe3114 | 2008-06-28 08:31:14 +1000 | [diff] [blame] | 3169 | s.locked++; |
Dan Williams | 2339788 | 2008-07-23 20:05:34 -0700 | [diff] [blame] | 3170 | } |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3171 | } |
| 3172 | |
| 3173 | if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) && |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3174 | !sh->reconstruct_state) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3175 | /* Need to write out all blocks after computing parity */ |
| 3176 | sh->disks = conf->raid_disks; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3177 | stripe_set_idx(sh->sector, conf, 0, sh); |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3178 | schedule_reconstruction(sh, &s, 1, 1); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3179 | } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3180 | clear_bit(STRIPE_EXPAND_READY, &sh->state); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3181 | atomic_dec(&conf->reshape_stripes); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3182 | wake_up(&conf->wait_for_overlap); |
| 3183 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 3184 | } |
| 3185 | |
Dan Williams | 0f94e87 | 2008-01-08 15:32:53 -0800 | [diff] [blame] | 3186 | if (s.expanding && s.locked == 0 && |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 3187 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3188 | handle_stripe_expansion(conf, sh, NULL); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3189 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3190 | unlock: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3191 | spin_unlock(&sh->lock); |
| 3192 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3193 | /* wait for this device to become unblocked */ |
| 3194 | if (unlikely(blocked_rdev)) |
| 3195 | md_wait_for_blocked_rdev(blocked_rdev, conf->mddev); |
| 3196 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3197 | if (s.ops_request) |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 3198 | raid_run_ops(sh, s.ops_request); |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3199 | |
Dan Williams | c4e5ac0 | 2008-06-28 08:31:53 +1000 | [diff] [blame] | 3200 | ops_run_io(sh, &s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3201 | |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 3202 | if (dec_preread_active) { |
| 3203 | /* We delay this until after ops_run_io so that if make_request |
| 3204 | * is waiting on a barrier, it won't continue until the writes |
| 3205 | * have actually been submitted. |
| 3206 | */ |
| 3207 | atomic_dec(&conf->preread_active_stripes); |
| 3208 | if (atomic_read(&conf->preread_active_stripes) < |
| 3209 | IO_THRESHOLD) |
| 3210 | md_wakeup_thread(conf->mddev->thread); |
| 3211 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3212 | return_io(return_bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3213 | } |
| 3214 | |
NeilBrown | 1442577 | 2009-10-16 15:55:25 +1100 | [diff] [blame] | 3215 | static void handle_stripe6(struct stripe_head *sh) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3216 | { |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3217 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3218 | int disks = sh->disks; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3219 | struct bio *return_bi = NULL; |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 3220 | int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3221 | struct stripe_head_state s; |
| 3222 | struct r6_state r6s; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3223 | struct r5dev *dev, *pdev, *qdev; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3224 | mdk_rdev_t *blocked_rdev = NULL; |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 3225 | int dec_preread_active = 0; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3226 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3227 | pr_debug("handling stripe %llu, state=%#lx cnt=%d, " |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3228 | "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3229 | (unsigned long long)sh->sector, sh->state, |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3230 | atomic_read(&sh->count), pd_idx, qd_idx, |
| 3231 | sh->check_state, sh->reconstruct_state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3232 | memset(&s, 0, sizeof(s)); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3233 | |
| 3234 | spin_lock(&sh->lock); |
| 3235 | clear_bit(STRIPE_HANDLE, &sh->state); |
| 3236 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 3237 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3238 | s.syncing = test_bit(STRIPE_SYNCING, &sh->state); |
| 3239 | s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 3240 | s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3241 | /* Now to look around and see what can be done */ |
| 3242 | |
| 3243 | rcu_read_lock(); |
| 3244 | for (i=disks; i--; ) { |
| 3245 | mdk_rdev_t *rdev; |
| 3246 | dev = &sh->dev[i]; |
| 3247 | clear_bit(R5_Insync, &dev->flags); |
| 3248 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3249 | 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] | 3250 | i, dev->flags, dev->toread, dev->towrite, dev->written); |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3251 | /* maybe we can reply to a read |
| 3252 | * |
| 3253 | * new wantfill requests are only permitted while |
| 3254 | * ops_complete_biofill is guaranteed to be inactive |
| 3255 | */ |
| 3256 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread && |
| 3257 | !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) |
| 3258 | set_bit(R5_Wantfill, &dev->flags); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3259 | |
| 3260 | /* now count some things */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3261 | if (test_bit(R5_LOCKED, &dev->flags)) s.locked++; |
| 3262 | if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++; |
Dan Williams | 2d6e4ec | 2009-09-16 12:11:54 -0700 | [diff] [blame] | 3263 | if (test_bit(R5_Wantcompute, &dev->flags)) { |
| 3264 | s.compute++; |
| 3265 | BUG_ON(s.compute > 2); |
| 3266 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3267 | |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3268 | if (test_bit(R5_Wantfill, &dev->flags)) { |
| 3269 | s.to_fill++; |
| 3270 | } else if (dev->toread) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3271 | s.to_read++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3272 | if (dev->towrite) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3273 | s.to_write++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3274 | if (!test_bit(R5_OVERWRITE, &dev->flags)) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3275 | s.non_overwrite++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3276 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3277 | if (dev->written) |
| 3278 | s.written++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3279 | rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | ac4090d | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3280 | if (blocked_rdev == NULL && |
| 3281 | rdev && unlikely(test_bit(Blocked, &rdev->flags))) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3282 | blocked_rdev = rdev; |
| 3283 | atomic_inc(&rdev->nr_pending); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3284 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3285 | if (!rdev || !test_bit(In_sync, &rdev->flags)) { |
| 3286 | /* The ReadError flag will just be confusing now */ |
| 3287 | clear_bit(R5_ReadError, &dev->flags); |
| 3288 | clear_bit(R5_ReWrite, &dev->flags); |
| 3289 | } |
| 3290 | if (!rdev || !test_bit(In_sync, &rdev->flags) |
| 3291 | || test_bit(R5_ReadError, &dev->flags)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3292 | if (s.failed < 2) |
| 3293 | r6s.failed_num[s.failed] = i; |
| 3294 | s.failed++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3295 | } else |
| 3296 | set_bit(R5_Insync, &dev->flags); |
| 3297 | } |
| 3298 | rcu_read_unlock(); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3299 | |
| 3300 | if (unlikely(blocked_rdev)) { |
NeilBrown | ac4090d | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3301 | if (s.syncing || s.expanding || s.expanded || |
| 3302 | s.to_write || s.written) { |
| 3303 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3304 | goto unlock; |
| 3305 | } |
| 3306 | /* There is nothing for the blocked_rdev to block */ |
| 3307 | rdev_dec_pending(blocked_rdev, conf->mddev); |
| 3308 | blocked_rdev = NULL; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3309 | } |
NeilBrown | ac4090d | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3310 | |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3311 | if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) { |
| 3312 | set_bit(STRIPE_OP_BIOFILL, &s.ops_request); |
| 3313 | set_bit(STRIPE_BIOFILL_RUN, &sh->state); |
| 3314 | } |
| 3315 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3316 | pr_debug("locked=%d uptodate=%d to_read=%d" |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3317 | " to_write=%d failed=%d failed_num=%d,%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3318 | s.locked, s.uptodate, s.to_read, s.to_write, s.failed, |
| 3319 | r6s.failed_num[0], r6s.failed_num[1]); |
| 3320 | /* check if the array has lost >2 devices and, if so, some requests |
| 3321 | * might need to be failed |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3322 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3323 | if (s.failed > 2 && s.to_read+s.to_write+s.written) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3324 | handle_failed_stripe(conf, sh, &s, disks, &return_bi); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3325 | if (s.failed > 2 && s.syncing) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3326 | md_done_sync(conf->mddev, STRIPE_SECTORS,0); |
| 3327 | clear_bit(STRIPE_SYNCING, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3328 | s.syncing = 0; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3329 | } |
| 3330 | |
| 3331 | /* |
| 3332 | * might be able to return some write requests if the parity blocks |
| 3333 | * are safe, or on a failed drive |
| 3334 | */ |
| 3335 | pdev = &sh->dev[pd_idx]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3336 | r6s.p_failed = (s.failed >= 1 && r6s.failed_num[0] == pd_idx) |
| 3337 | || (s.failed >= 2 && r6s.failed_num[1] == pd_idx); |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 3338 | qdev = &sh->dev[qd_idx]; |
| 3339 | r6s.q_failed = (s.failed >= 1 && r6s.failed_num[0] == qd_idx) |
| 3340 | || (s.failed >= 2 && r6s.failed_num[1] == qd_idx); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3341 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3342 | if ( s.written && |
| 3343 | ( r6s.p_failed || ((test_bit(R5_Insync, &pdev->flags) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3344 | && !test_bit(R5_LOCKED, &pdev->flags) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3345 | && test_bit(R5_UPTODATE, &pdev->flags)))) && |
| 3346 | ( r6s.q_failed || ((test_bit(R5_Insync, &qdev->flags) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3347 | && !test_bit(R5_LOCKED, &qdev->flags) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3348 | && test_bit(R5_UPTODATE, &qdev->flags))))) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3349 | handle_stripe_clean_event(conf, sh, disks, &return_bi); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3350 | |
| 3351 | /* Now we might consider reading some blocks, either to check/generate |
| 3352 | * parity, or to satisfy requests |
| 3353 | * or to load a block that is being partially written. |
| 3354 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3355 | if (s.to_read || s.non_overwrite || (s.to_write && s.failed) || |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3356 | (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3357 | handle_stripe_fill6(sh, &s, &r6s, disks); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3358 | |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3359 | /* Now we check to see if any write operations have recently |
| 3360 | * completed |
| 3361 | */ |
| 3362 | if (sh->reconstruct_state == reconstruct_state_drain_result) { |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3363 | |
| 3364 | sh->reconstruct_state = reconstruct_state_idle; |
| 3365 | /* All the 'written' buffers and the parity blocks are ready to |
| 3366 | * be written back to disk |
| 3367 | */ |
| 3368 | BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags)); |
| 3369 | BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags)); |
| 3370 | for (i = disks; i--; ) { |
| 3371 | dev = &sh->dev[i]; |
| 3372 | if (test_bit(R5_LOCKED, &dev->flags) && |
| 3373 | (i == sh->pd_idx || i == qd_idx || |
| 3374 | dev->written)) { |
| 3375 | pr_debug("Writing block %d\n", i); |
| 3376 | BUG_ON(!test_bit(R5_UPTODATE, &dev->flags)); |
| 3377 | set_bit(R5_Wantwrite, &dev->flags); |
| 3378 | if (!test_bit(R5_Insync, &dev->flags) || |
| 3379 | ((i == sh->pd_idx || i == qd_idx) && |
| 3380 | s.failed == 0)) |
| 3381 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3382 | } |
| 3383 | } |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 3384 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 3385 | dec_preread_active = 1; |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3386 | } |
| 3387 | |
Yuri Tikhonov | a9b39a7 | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3388 | /* Now to consider new write requests and what else, if anything |
| 3389 | * should be read. We do not handle new writes when: |
| 3390 | * 1/ A 'write' operation (copy+gen_syndrome) is already in flight. |
| 3391 | * 2/ A 'check' operation is in flight, as it may clobber the parity |
| 3392 | * block. |
| 3393 | */ |
| 3394 | if (s.to_write && !sh->reconstruct_state && !sh->check_state) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3395 | handle_stripe_dirtying6(conf, sh, &s, &r6s, disks); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3396 | |
| 3397 | /* 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] | 3398 | * 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] | 3399 | * data is available. The parity check is held off while parity |
| 3400 | * dependent operations are in flight. |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3401 | */ |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3402 | if (sh->check_state || |
| 3403 | (s.syncing && s.locked == 0 && |
| 3404 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && |
| 3405 | !test_bit(STRIPE_INSYNC, &sh->state))) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3406 | handle_parity_checks6(conf, sh, &s, &r6s, disks); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3407 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3408 | if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3409 | md_done_sync(conf->mddev, STRIPE_SECTORS,1); |
| 3410 | clear_bit(STRIPE_SYNCING, &sh->state); |
| 3411 | } |
| 3412 | |
| 3413 | /* If the failed drives are just a ReadError, then we might need |
| 3414 | * to progress the repair/check process |
| 3415 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3416 | if (s.failed <= 2 && !conf->mddev->ro) |
| 3417 | for (i = 0; i < s.failed; i++) { |
| 3418 | dev = &sh->dev[r6s.failed_num[i]]; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3419 | if (test_bit(R5_ReadError, &dev->flags) |
| 3420 | && !test_bit(R5_LOCKED, &dev->flags) |
| 3421 | && test_bit(R5_UPTODATE, &dev->flags) |
| 3422 | ) { |
| 3423 | if (!test_bit(R5_ReWrite, &dev->flags)) { |
| 3424 | set_bit(R5_Wantwrite, &dev->flags); |
| 3425 | set_bit(R5_ReWrite, &dev->flags); |
| 3426 | set_bit(R5_LOCKED, &dev->flags); |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3427 | s.locked++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3428 | } else { |
| 3429 | /* let's read it back */ |
| 3430 | set_bit(R5_Wantread, &dev->flags); |
| 3431 | set_bit(R5_LOCKED, &dev->flags); |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3432 | s.locked++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3433 | } |
| 3434 | } |
| 3435 | } |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3436 | |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3437 | /* Finish reconstruct operations initiated by the expansion process */ |
| 3438 | if (sh->reconstruct_state == reconstruct_state_result) { |
| 3439 | sh->reconstruct_state = reconstruct_state_idle; |
| 3440 | clear_bit(STRIPE_EXPANDING, &sh->state); |
| 3441 | for (i = conf->raid_disks; i--; ) { |
| 3442 | set_bit(R5_Wantwrite, &sh->dev[i].flags); |
| 3443 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 3444 | s.locked++; |
| 3445 | } |
| 3446 | } |
| 3447 | |
| 3448 | if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) && |
| 3449 | !sh->reconstruct_state) { |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 3450 | struct stripe_head *sh2 |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 3451 | = get_active_stripe(conf, sh->sector, 1, 1, 1); |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 3452 | if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) { |
| 3453 | /* sh cannot be written until sh2 has been read. |
| 3454 | * so arrange for sh to be delayed a little |
| 3455 | */ |
| 3456 | set_bit(STRIPE_DELAYED, &sh->state); |
| 3457 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3458 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, |
| 3459 | &sh2->state)) |
| 3460 | atomic_inc(&conf->preread_active_stripes); |
| 3461 | release_stripe(sh2); |
| 3462 | goto unlock; |
| 3463 | } |
| 3464 | if (sh2) |
| 3465 | release_stripe(sh2); |
| 3466 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3467 | /* Need to write out all blocks after computing P&Q */ |
| 3468 | sh->disks = conf->raid_disks; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3469 | stripe_set_idx(sh->sector, conf, 0, sh); |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3470 | schedule_reconstruction(sh, &s, 1, 1); |
| 3471 | } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) { |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3472 | clear_bit(STRIPE_EXPAND_READY, &sh->state); |
| 3473 | atomic_dec(&conf->reshape_stripes); |
| 3474 | wake_up(&conf->wait_for_overlap); |
| 3475 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 3476 | } |
| 3477 | |
Dan Williams | 0f94e87 | 2008-01-08 15:32:53 -0800 | [diff] [blame] | 3478 | if (s.expanding && s.locked == 0 && |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 3479 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3480 | handle_stripe_expansion(conf, sh, &r6s); |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3481 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3482 | unlock: |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3483 | spin_unlock(&sh->lock); |
| 3484 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3485 | /* wait for this device to become unblocked */ |
| 3486 | if (unlikely(blocked_rdev)) |
| 3487 | md_wait_for_blocked_rdev(blocked_rdev, conf->mddev); |
| 3488 | |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3489 | if (s.ops_request) |
| 3490 | raid_run_ops(sh, s.ops_request); |
| 3491 | |
Dan Williams | f0e43bc | 2008-06-28 08:31:55 +1000 | [diff] [blame] | 3492 | ops_run_io(sh, &s); |
| 3493 | |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 3494 | |
| 3495 | if (dec_preread_active) { |
| 3496 | /* We delay this until after ops_run_io so that if make_request |
| 3497 | * is waiting on a barrier, it won't continue until the writes |
| 3498 | * have actually been submitted. |
| 3499 | */ |
| 3500 | atomic_dec(&conf->preread_active_stripes); |
| 3501 | if (atomic_read(&conf->preread_active_stripes) < |
| 3502 | IO_THRESHOLD) |
| 3503 | md_wakeup_thread(conf->mddev->thread); |
| 3504 | } |
| 3505 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3506 | return_io(return_bi); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3507 | } |
| 3508 | |
NeilBrown | 1442577 | 2009-10-16 15:55:25 +1100 | [diff] [blame] | 3509 | static void handle_stripe(struct stripe_head *sh) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3510 | { |
| 3511 | if (sh->raid_conf->level == 6) |
NeilBrown | 1442577 | 2009-10-16 15:55:25 +1100 | [diff] [blame] | 3512 | handle_stripe6(sh); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3513 | else |
NeilBrown | 1442577 | 2009-10-16 15:55:25 +1100 | [diff] [blame] | 3514 | handle_stripe5(sh); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3515 | } |
| 3516 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 3517 | static void raid5_activate_delayed(raid5_conf_t *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3518 | { |
| 3519 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) { |
| 3520 | while (!list_empty(&conf->delayed_list)) { |
| 3521 | struct list_head *l = conf->delayed_list.next; |
| 3522 | struct stripe_head *sh; |
| 3523 | sh = list_entry(l, struct stripe_head, lru); |
| 3524 | list_del_init(l); |
| 3525 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 3526 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 3527 | atomic_inc(&conf->preread_active_stripes); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3528 | list_add_tail(&sh->lru, &conf->hold_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3529 | } |
NeilBrown | 6ed3003 | 2008-02-06 01:40:00 -0800 | [diff] [blame] | 3530 | } else |
| 3531 | blk_plug_device(conf->mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3532 | } |
| 3533 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 3534 | static void activate_bit_delay(raid5_conf_t *conf) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3535 | { |
| 3536 | /* device_lock is held */ |
| 3537 | struct list_head head; |
| 3538 | list_add(&head, &conf->bitmap_list); |
| 3539 | list_del_init(&conf->bitmap_list); |
| 3540 | while (!list_empty(&head)) { |
| 3541 | struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru); |
| 3542 | list_del_init(&sh->lru); |
| 3543 | atomic_inc(&sh->count); |
| 3544 | __release_stripe(conf, sh); |
| 3545 | } |
| 3546 | } |
| 3547 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3548 | static void unplug_slaves(mddev_t *mddev) |
| 3549 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 3550 | raid5_conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3551 | int i; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 3552 | int devs = max(conf->raid_disks, conf->previous_raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3553 | |
| 3554 | rcu_read_lock(); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 3555 | for (i = 0; i < devs; i++) { |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 3556 | mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3557 | if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3558 | struct request_queue *r_queue = bdev_get_queue(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3559 | |
| 3560 | atomic_inc(&rdev->nr_pending); |
| 3561 | rcu_read_unlock(); |
| 3562 | |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 3563 | blk_unplug(r_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3564 | |
| 3565 | rdev_dec_pending(rdev, mddev); |
| 3566 | rcu_read_lock(); |
| 3567 | } |
| 3568 | } |
| 3569 | rcu_read_unlock(); |
| 3570 | } |
| 3571 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3572 | static void raid5_unplug_device(struct request_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3573 | { |
| 3574 | mddev_t *mddev = q->queuedata; |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 3575 | raid5_conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3576 | unsigned long flags; |
| 3577 | |
| 3578 | spin_lock_irqsave(&conf->device_lock, flags); |
| 3579 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3580 | if (blk_remove_plug(q)) { |
| 3581 | conf->seq_flush++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3582 | raid5_activate_delayed(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3583 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3584 | md_wakeup_thread(mddev->thread); |
| 3585 | |
| 3586 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 3587 | |
| 3588 | unplug_slaves(mddev); |
| 3589 | } |
| 3590 | |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3591 | static int raid5_congested(void *data, int bits) |
| 3592 | { |
| 3593 | mddev_t *mddev = data; |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 3594 | raid5_conf_t *conf = mddev->private; |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3595 | |
| 3596 | /* No difference between reads and writes. Just check |
| 3597 | * how busy the stripe_cache is |
| 3598 | */ |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 3599 | |
| 3600 | if (mddev_congested(mddev, bits)) |
| 3601 | return 1; |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3602 | if (conf->inactive_blocked) |
| 3603 | return 1; |
| 3604 | if (conf->quiesce) |
| 3605 | return 1; |
| 3606 | if (list_empty_careful(&conf->inactive_list)) |
| 3607 | return 1; |
| 3608 | |
| 3609 | return 0; |
| 3610 | } |
| 3611 | |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3612 | /* We want read requests to align with chunks where possible, |
| 3613 | * but write requests don't need to. |
| 3614 | */ |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3615 | static int raid5_mergeable_bvec(struct request_queue *q, |
| 3616 | struct bvec_merge_data *bvm, |
| 3617 | struct bio_vec *biovec) |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3618 | { |
| 3619 | mddev_t *mddev = q->queuedata; |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3620 | 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] | 3621 | int max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3622 | unsigned int chunk_sectors = mddev->chunk_sectors; |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3623 | unsigned int bio_sectors = bvm->bi_size >> 9; |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3624 | |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3625 | if ((bvm->bi_rw & 1) == WRITE) |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3626 | return biovec->bv_len; /* always allow writes to be mergeable */ |
| 3627 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3628 | if (mddev->new_chunk_sectors < mddev->chunk_sectors) |
| 3629 | chunk_sectors = mddev->new_chunk_sectors; |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3630 | max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9; |
| 3631 | if (max < 0) max = 0; |
| 3632 | if (max <= biovec->bv_len && bio_sectors == 0) |
| 3633 | return biovec->bv_len; |
| 3634 | else |
| 3635 | return max; |
| 3636 | } |
| 3637 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3638 | |
| 3639 | static int in_chunk_boundary(mddev_t *mddev, struct bio *bio) |
| 3640 | { |
| 3641 | sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3642 | unsigned int chunk_sectors = mddev->chunk_sectors; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3643 | unsigned int bio_sectors = bio->bi_size >> 9; |
| 3644 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3645 | if (mddev->new_chunk_sectors < mddev->chunk_sectors) |
| 3646 | chunk_sectors = mddev->new_chunk_sectors; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3647 | return chunk_sectors >= |
| 3648 | ((sector & (chunk_sectors - 1)) + bio_sectors); |
| 3649 | } |
| 3650 | |
| 3651 | /* |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3652 | * add bio to the retry LIFO ( in O(1) ... we are in interrupt ) |
| 3653 | * later sampled by raid5d. |
| 3654 | */ |
| 3655 | static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf) |
| 3656 | { |
| 3657 | unsigned long flags; |
| 3658 | |
| 3659 | spin_lock_irqsave(&conf->device_lock, flags); |
| 3660 | |
| 3661 | bi->bi_next = conf->retry_read_aligned_list; |
| 3662 | conf->retry_read_aligned_list = bi; |
| 3663 | |
| 3664 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 3665 | md_wakeup_thread(conf->mddev->thread); |
| 3666 | } |
| 3667 | |
| 3668 | |
| 3669 | static struct bio *remove_bio_from_retry(raid5_conf_t *conf) |
| 3670 | { |
| 3671 | struct bio *bi; |
| 3672 | |
| 3673 | bi = conf->retry_read_aligned; |
| 3674 | if (bi) { |
| 3675 | conf->retry_read_aligned = NULL; |
| 3676 | return bi; |
| 3677 | } |
| 3678 | bi = conf->retry_read_aligned_list; |
| 3679 | if(bi) { |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3680 | conf->retry_read_aligned_list = bi->bi_next; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3681 | bi->bi_next = NULL; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 3682 | /* |
| 3683 | * this sets the active strip count to 1 and the processed |
| 3684 | * strip count to zero (upper 8 bits) |
| 3685 | */ |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3686 | bi->bi_phys_segments = 1; /* biased count of active stripes */ |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3687 | } |
| 3688 | |
| 3689 | return bi; |
| 3690 | } |
| 3691 | |
| 3692 | |
| 3693 | /* |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3694 | * The "raid5_align_endio" should check if the read succeeded and if it |
| 3695 | * did, call bio_endio on the original bio (having bio_put the new bio |
| 3696 | * first). |
| 3697 | * If the read failed.. |
| 3698 | */ |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3699 | static void raid5_align_endio(struct bio *bi, int error) |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3700 | { |
| 3701 | struct bio* raid_bi = bi->bi_private; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3702 | mddev_t *mddev; |
| 3703 | raid5_conf_t *conf; |
| 3704 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
| 3705 | mdk_rdev_t *rdev; |
| 3706 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3707 | bio_put(bi); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3708 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3709 | rdev = (void*)raid_bi->bi_next; |
| 3710 | raid_bi->bi_next = NULL; |
NeilBrown | 2b7f222 | 2010-03-25 16:06:03 +1100 | [diff] [blame] | 3711 | mddev = rdev->mddev; |
| 3712 | conf = mddev->private; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3713 | |
| 3714 | rdev_dec_pending(rdev, conf->mddev); |
| 3715 | |
| 3716 | if (!error && uptodate) { |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3717 | bio_endio(raid_bi, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3718 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
| 3719 | wake_up(&conf->wait_for_stripe); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3720 | return; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3721 | } |
| 3722 | |
| 3723 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3724 | 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] | 3725 | |
| 3726 | add_bio_to_retry(raid_bi, conf); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3727 | } |
| 3728 | |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3729 | static int bio_fits_rdev(struct bio *bi) |
| 3730 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3731 | struct request_queue *q = bdev_get_queue(bi->bi_bdev); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3732 | |
Martin K. Petersen | ae03bf6 | 2009-05-22 17:17:50 -0400 | [diff] [blame] | 3733 | if ((bi->bi_size>>9) > queue_max_sectors(q)) |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3734 | return 0; |
| 3735 | blk_recount_segments(q, bi); |
Martin K. Petersen | 8a78362 | 2010-02-26 00:20:39 -0500 | [diff] [blame] | 3736 | if (bi->bi_phys_segments > queue_max_segments(q)) |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3737 | return 0; |
| 3738 | |
| 3739 | if (q->merge_bvec_fn) |
| 3740 | /* it's too hard to apply the merge_bvec_fn at this stage, |
| 3741 | * just just give up |
| 3742 | */ |
| 3743 | return 0; |
| 3744 | |
| 3745 | return 1; |
| 3746 | } |
| 3747 | |
| 3748 | |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 3749 | 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] | 3750 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 3751 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 8553fe7ec | 2009-12-14 12:49:47 +1100 | [diff] [blame] | 3752 | int dd_idx; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3753 | struct bio* align_bi; |
| 3754 | mdk_rdev_t *rdev; |
| 3755 | |
| 3756 | if (!in_chunk_boundary(mddev, raid_bio)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3757 | pr_debug("chunk_aligned_read : non aligned\n"); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3758 | return 0; |
| 3759 | } |
| 3760 | /* |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3761 | * use bio_clone to make a copy of the bio |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3762 | */ |
| 3763 | align_bi = bio_clone(raid_bio, GFP_NOIO); |
| 3764 | if (!align_bi) |
| 3765 | return 0; |
| 3766 | /* |
| 3767 | * set bi_end_io to a new function, and set bi_private to the |
| 3768 | * original bio. |
| 3769 | */ |
| 3770 | align_bi->bi_end_io = raid5_align_endio; |
| 3771 | align_bi->bi_private = raid_bio; |
| 3772 | /* |
| 3773 | * compute position |
| 3774 | */ |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3775 | align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector, |
| 3776 | 0, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3777 | &dd_idx, NULL); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3778 | |
| 3779 | rcu_read_lock(); |
| 3780 | rdev = rcu_dereference(conf->disks[dd_idx].rdev); |
| 3781 | if (rdev && test_bit(In_sync, &rdev->flags)) { |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3782 | atomic_inc(&rdev->nr_pending); |
| 3783 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3784 | raid_bio->bi_next = (void*)rdev; |
| 3785 | align_bi->bi_bdev = rdev->bdev; |
| 3786 | align_bi->bi_flags &= ~(1 << BIO_SEG_VALID); |
| 3787 | align_bi->bi_sector += rdev->data_offset; |
| 3788 | |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3789 | if (!bio_fits_rdev(align_bi)) { |
| 3790 | /* too big in some way */ |
| 3791 | bio_put(align_bi); |
| 3792 | rdev_dec_pending(rdev, mddev); |
| 3793 | return 0; |
| 3794 | } |
| 3795 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3796 | spin_lock_irq(&conf->device_lock); |
| 3797 | wait_event_lock_irq(conf->wait_for_stripe, |
| 3798 | conf->quiesce == 0, |
| 3799 | conf->device_lock, /* nothing */); |
| 3800 | atomic_inc(&conf->active_aligned_reads); |
| 3801 | spin_unlock_irq(&conf->device_lock); |
| 3802 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3803 | generic_make_request(align_bi); |
| 3804 | return 1; |
| 3805 | } else { |
| 3806 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3807 | bio_put(align_bi); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3808 | return 0; |
| 3809 | } |
| 3810 | } |
| 3811 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3812 | /* __get_priority_stripe - get the next stripe to process |
| 3813 | * |
| 3814 | * Full stripe writes are allowed to pass preread active stripes up until |
| 3815 | * the bypass_threshold is exceeded. In general the bypass_count |
| 3816 | * increments when the handle_list is handled before the hold_list; however, it |
| 3817 | * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a |
| 3818 | * stripe with in flight i/o. The bypass_count will be reset when the |
| 3819 | * head of the hold_list has changed, i.e. the head was promoted to the |
| 3820 | * handle_list. |
| 3821 | */ |
| 3822 | static struct stripe_head *__get_priority_stripe(raid5_conf_t *conf) |
| 3823 | { |
| 3824 | struct stripe_head *sh; |
| 3825 | |
| 3826 | pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n", |
| 3827 | __func__, |
| 3828 | list_empty(&conf->handle_list) ? "empty" : "busy", |
| 3829 | list_empty(&conf->hold_list) ? "empty" : "busy", |
| 3830 | atomic_read(&conf->pending_full_writes), conf->bypass_count); |
| 3831 | |
| 3832 | if (!list_empty(&conf->handle_list)) { |
| 3833 | sh = list_entry(conf->handle_list.next, typeof(*sh), lru); |
| 3834 | |
| 3835 | if (list_empty(&conf->hold_list)) |
| 3836 | conf->bypass_count = 0; |
| 3837 | else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) { |
| 3838 | if (conf->hold_list.next == conf->last_hold) |
| 3839 | conf->bypass_count++; |
| 3840 | else { |
| 3841 | conf->last_hold = conf->hold_list.next; |
| 3842 | conf->bypass_count -= conf->bypass_threshold; |
| 3843 | if (conf->bypass_count < 0) |
| 3844 | conf->bypass_count = 0; |
| 3845 | } |
| 3846 | } |
| 3847 | } else if (!list_empty(&conf->hold_list) && |
| 3848 | ((conf->bypass_threshold && |
| 3849 | conf->bypass_count > conf->bypass_threshold) || |
| 3850 | atomic_read(&conf->pending_full_writes) == 0)) { |
| 3851 | sh = list_entry(conf->hold_list.next, |
| 3852 | typeof(*sh), lru); |
| 3853 | conf->bypass_count -= conf->bypass_threshold; |
| 3854 | if (conf->bypass_count < 0) |
| 3855 | conf->bypass_count = 0; |
| 3856 | } else |
| 3857 | return NULL; |
| 3858 | |
| 3859 | list_del_init(&sh->lru); |
| 3860 | atomic_inc(&sh->count); |
| 3861 | BUG_ON(atomic_read(&sh->count) != 1); |
| 3862 | return sh; |
| 3863 | } |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3864 | |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 3865 | static int make_request(mddev_t *mddev, struct bio * bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3866 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 3867 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3868 | int dd_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3869 | sector_t new_sector; |
| 3870 | sector_t logical_sector, last_sector; |
| 3871 | struct stripe_head *sh; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 3872 | const int rw = bio_data_dir(bi); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 3873 | int remaining; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3874 | |
Jens Axboe | 1f98a13 | 2009-09-11 14:32:04 +0200 | [diff] [blame] | 3875 | if (unlikely(bio_rw_flagged(bi, BIO_RW_BARRIER))) { |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 3876 | /* Drain all pending writes. We only really need |
| 3877 | * to ensure they have been submitted, but this is |
| 3878 | * easier. |
| 3879 | */ |
| 3880 | mddev->pers->quiesce(mddev, 1); |
| 3881 | mddev->pers->quiesce(mddev, 0); |
| 3882 | md_barrier_request(mddev, bi); |
NeilBrown | e5dcdd8 | 2005-09-09 16:23:41 -0700 | [diff] [blame] | 3883 | return 0; |
| 3884 | } |
| 3885 | |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 3886 | md_write_start(mddev, bi); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 3887 | |
NeilBrown | 802ba06 | 2006-12-13 00:34:13 -0800 | [diff] [blame] | 3888 | if (rw == READ && |
Raz Ben-Jehuda(caro) | 5248861 | 2006-12-10 02:20:48 -0800 | [diff] [blame] | 3889 | mddev->reshape_position == MaxSector && |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 3890 | chunk_aligned_read(mddev,bi)) |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3891 | return 0; |
Raz Ben-Jehuda(caro) | 5248861 | 2006-12-10 02:20:48 -0800 | [diff] [blame] | 3892 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3893 | logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
| 3894 | last_sector = bi->bi_sector + (bi->bi_size>>9); |
| 3895 | bi->bi_next = NULL; |
| 3896 | bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 3897 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3898 | for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) { |
| 3899 | DEFINE_WAIT(w); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3900 | int disks, data_disks; |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3901 | int previous; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 3902 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3903 | retry: |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3904 | previous = 0; |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 3905 | disks = conf->raid_disks; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 3906 | prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 3907 | if (unlikely(conf->reshape_progress != MaxSector)) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3908 | /* spinlock is needed as reshape_progress may be |
NeilBrown | df8e7f7 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 3909 | * 64bit on a 32bit platform, and so it might be |
| 3910 | * possible to see a half-updated value |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3911 | * Ofcourse reshape_progress could change after |
NeilBrown | df8e7f7 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 3912 | * the lock is dropped, so once we get a reference |
| 3913 | * to the stripe that we think it is, we will have |
| 3914 | * to check again. |
| 3915 | */ |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3916 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3917 | if (mddev->delta_disks < 0 |
| 3918 | ? logical_sector < conf->reshape_progress |
| 3919 | : logical_sector >= conf->reshape_progress) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3920 | disks = conf->previous_raid_disks; |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3921 | previous = 1; |
| 3922 | } else { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3923 | if (mddev->delta_disks < 0 |
| 3924 | ? logical_sector < conf->reshape_safe |
| 3925 | : logical_sector >= conf->reshape_safe) { |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 3926 | spin_unlock_irq(&conf->device_lock); |
| 3927 | schedule(); |
| 3928 | goto retry; |
| 3929 | } |
| 3930 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3931 | spin_unlock_irq(&conf->device_lock); |
| 3932 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3933 | data_disks = disks - conf->max_degraded; |
| 3934 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3935 | new_sector = raid5_compute_sector(conf, logical_sector, |
| 3936 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3937 | &dd_idx, NULL); |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 3938 | pr_debug("raid456: make_request, sector %llu logical %llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3939 | (unsigned long long)new_sector, |
| 3940 | (unsigned long long)logical_sector); |
| 3941 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3942 | sh = get_active_stripe(conf, new_sector, previous, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 3943 | (bi->bi_rw&RWA_MASK), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3944 | if (sh) { |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 3945 | if (unlikely(previous)) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3946 | /* expansion might have moved on while waiting for a |
NeilBrown | df8e7f7 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 3947 | * stripe, so we must do the range check again. |
| 3948 | * Expansion could still move past after this |
| 3949 | * test, but as we are holding a reference to |
| 3950 | * 'sh', we know that if that happens, |
| 3951 | * STRIPE_EXPANDING will get set and the expansion |
| 3952 | * won't proceed until we finish with the stripe. |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3953 | */ |
| 3954 | int must_retry = 0; |
| 3955 | spin_lock_irq(&conf->device_lock); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 3956 | if (mddev->delta_disks < 0 |
| 3957 | ? logical_sector >= conf->reshape_progress |
| 3958 | : logical_sector < conf->reshape_progress) |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3959 | /* mismatch, need to try again */ |
| 3960 | must_retry = 1; |
| 3961 | spin_unlock_irq(&conf->device_lock); |
| 3962 | if (must_retry) { |
| 3963 | release_stripe(sh); |
Dan Williams | 7a3ab90 | 2009-06-16 16:00:33 -0700 | [diff] [blame] | 3964 | schedule(); |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3965 | goto retry; |
| 3966 | } |
| 3967 | } |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 3968 | |
NeilBrown | a5c308d | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 3969 | if (bio_data_dir(bi) == WRITE && |
| 3970 | logical_sector >= mddev->suspend_lo && |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3971 | logical_sector < mddev->suspend_hi) { |
| 3972 | release_stripe(sh); |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 3973 | /* As the suspend_* range is controlled by |
| 3974 | * userspace, we want an interruptible |
| 3975 | * wait. |
| 3976 | */ |
| 3977 | flush_signals(current); |
| 3978 | prepare_to_wait(&conf->wait_for_overlap, |
| 3979 | &w, TASK_INTERRUPTIBLE); |
| 3980 | if (logical_sector >= mddev->suspend_lo && |
| 3981 | logical_sector < mddev->suspend_hi) |
| 3982 | schedule(); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3983 | goto retry; |
| 3984 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3985 | |
| 3986 | if (test_bit(STRIPE_EXPANDING, &sh->state) || |
| 3987 | !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) { |
| 3988 | /* Stripe is busy expanding or |
| 3989 | * add failed due to overlap. Flush everything |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3990 | * and wait a while |
| 3991 | */ |
| 3992 | raid5_unplug_device(mddev->queue); |
| 3993 | release_stripe(sh); |
| 3994 | schedule(); |
| 3995 | goto retry; |
| 3996 | } |
| 3997 | finish_wait(&conf->wait_for_overlap, &w); |
NeilBrown | 6ed3003 | 2008-02-06 01:40:00 -0800 | [diff] [blame] | 3998 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3999 | clear_bit(STRIPE_DELAYED, &sh->state); |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 4000 | if (mddev->barrier && |
| 4001 | !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 4002 | atomic_inc(&conf->preread_active_stripes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4003 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4004 | } else { |
| 4005 | /* cannot get stripe for read-ahead, just give-up */ |
| 4006 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
| 4007 | finish_wait(&conf->wait_for_overlap, &w); |
| 4008 | break; |
| 4009 | } |
| 4010 | |
| 4011 | } |
| 4012 | spin_lock_irq(&conf->device_lock); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4013 | remaining = raid5_dec_bi_phys_segments(bi); |
NeilBrown | f634475 | 2006-03-27 01:18:17 -0800 | [diff] [blame] | 4014 | spin_unlock_irq(&conf->device_lock); |
| 4015 | if (remaining == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4016 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4017 | if ( rw == WRITE ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4018 | md_write_end(mddev); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 4019 | |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 4020 | bio_endio(bi, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4021 | } |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 4022 | |
| 4023 | if (mddev->barrier) { |
| 4024 | /* We need to wait for the stripes to all be handled. |
| 4025 | * So: wait for preread_active_stripes to drop to 0. |
| 4026 | */ |
| 4027 | wait_event(mddev->thread->wqueue, |
| 4028 | atomic_read(&conf->preread_active_stripes) == 0); |
| 4029 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4030 | return 0; |
| 4031 | } |
| 4032 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4033 | static sector_t raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks); |
| 4034 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4035 | 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] | 4036 | { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4037 | /* reshaping is quite different to recovery/resync so it is |
| 4038 | * handled quite separately ... here. |
| 4039 | * |
| 4040 | * On each call to sync_request, we gather one chunk worth of |
| 4041 | * destination stripes and flag them as expanding. |
| 4042 | * Then we find all the source stripes and request reads. |
| 4043 | * As the reads complete, handle_stripe will copy the data |
| 4044 | * into the destination stripe and release that stripe. |
| 4045 | */ |
H Hartley Sweeten | 7b92813 | 2010-03-08 16:02:40 +1100 | [diff] [blame] | 4046 | raid5_conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4047 | struct stripe_head *sh; |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4048 | sector_t first_sector, last_sector; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4049 | int raid_disks = conf->previous_raid_disks; |
| 4050 | int data_disks = raid_disks - conf->max_degraded; |
| 4051 | int new_data_disks = conf->raid_disks - conf->max_degraded; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4052 | int i; |
| 4053 | int dd_idx; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4054 | sector_t writepos, readpos, safepos; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4055 | sector_t stripe_addr; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4056 | int reshape_sectors; |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4057 | struct list_head stripes; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4058 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4059 | if (sector_nr == 0) { |
| 4060 | /* If restarting in the middle, skip the initial sectors */ |
| 4061 | if (mddev->delta_disks < 0 && |
| 4062 | conf->reshape_progress < raid5_size(mddev, 0, 0)) { |
| 4063 | sector_nr = raid5_size(mddev, 0, 0) |
| 4064 | - conf->reshape_progress; |
NeilBrown | a639755 | 2009-08-13 10:13:00 +1000 | [diff] [blame] | 4065 | } else if (mddev->delta_disks >= 0 && |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4066 | conf->reshape_progress > 0) |
| 4067 | sector_nr = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4068 | sector_div(sector_nr, new_data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4069 | if (sector_nr) { |
NeilBrown | 8dee721 | 2009-11-06 14:59:29 +1100 | [diff] [blame] | 4070 | mddev->curr_resync_completed = sector_nr; |
| 4071 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4072 | *skipped = 1; |
| 4073 | return sector_nr; |
| 4074 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4075 | } |
| 4076 | |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4077 | /* We need to process a full chunk at a time. |
| 4078 | * If old and new chunk sizes differ, we need to process the |
| 4079 | * largest of these |
| 4080 | */ |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4081 | if (mddev->new_chunk_sectors > mddev->chunk_sectors) |
| 4082 | reshape_sectors = mddev->new_chunk_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4083 | else |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4084 | reshape_sectors = mddev->chunk_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4085 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4086 | /* we update the metadata when there is more than 3Meg |
| 4087 | * in the block range (that is rather arbitrary, should |
| 4088 | * probably be time based) or when the data about to be |
| 4089 | * copied would over-write the source of the data at |
| 4090 | * the front of the range. |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4091 | * i.e. one new_stripe along from reshape_progress new_maps |
| 4092 | * to after where reshape_safe old_maps to |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4093 | */ |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4094 | writepos = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4095 | sector_div(writepos, new_data_disks); |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4096 | readpos = conf->reshape_progress; |
| 4097 | sector_div(readpos, data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4098 | safepos = conf->reshape_safe; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4099 | sector_div(safepos, data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4100 | if (mddev->delta_disks < 0) { |
NeilBrown | ed37d83 | 2009-05-27 21:39:05 +1000 | [diff] [blame] | 4101 | writepos -= min_t(sector_t, reshape_sectors, writepos); |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4102 | readpos += reshape_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4103 | safepos += reshape_sectors; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4104 | } else { |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4105 | writepos += reshape_sectors; |
NeilBrown | ed37d83 | 2009-05-27 21:39:05 +1000 | [diff] [blame] | 4106 | readpos -= min_t(sector_t, reshape_sectors, readpos); |
| 4107 | safepos -= min_t(sector_t, reshape_sectors, safepos); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4108 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4109 | |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4110 | /* 'writepos' is the most advanced device address we might write. |
| 4111 | * 'readpos' is the least advanced device address we might read. |
| 4112 | * 'safepos' is the least address recorded in the metadata as having |
| 4113 | * been reshaped. |
| 4114 | * If 'readpos' is behind 'writepos', then there is no way that we can |
| 4115 | * ensure safety in the face of a crash - that must be done by userspace |
| 4116 | * making a backup of the data. So in that case there is no particular |
| 4117 | * rush to update metadata. |
| 4118 | * Otherwise if 'safepos' is behind 'writepos', then we really need to |
| 4119 | * update the metadata to advance 'safepos' to match 'readpos' so that |
| 4120 | * we can be safe in the event of a crash. |
| 4121 | * So we insist on updating metadata if safepos is behind writepos and |
| 4122 | * readpos is beyond writepos. |
| 4123 | * In any case, update the metadata every 10 seconds. |
| 4124 | * Maybe that number should be configurable, but I'm not sure it is |
| 4125 | * worth it.... maybe it could be a multiple of safemode_delay??? |
| 4126 | */ |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4127 | if ((mddev->delta_disks < 0 |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4128 | ? (safepos > writepos && readpos < writepos) |
| 4129 | : (safepos < writepos && readpos > writepos)) || |
| 4130 | time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4131 | /* Cannot proceed until we've updated the superblock... */ |
| 4132 | wait_event(conf->wait_for_overlap, |
| 4133 | atomic_read(&conf->reshape_stripes)==0); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4134 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4135 | mddev->curr_resync_completed = mddev->curr_resync; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4136 | conf->reshape_checkpoint = jiffies; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4137 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4138 | md_wakeup_thread(mddev->thread); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4139 | wait_event(mddev->sb_wait, mddev->flags == 0 || |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4140 | kthread_should_stop()); |
| 4141 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4142 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4143 | spin_unlock_irq(&conf->device_lock); |
| 4144 | wake_up(&conf->wait_for_overlap); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4145 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4146 | } |
| 4147 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4148 | if (mddev->delta_disks < 0) { |
| 4149 | BUG_ON(conf->reshape_progress == 0); |
| 4150 | stripe_addr = writepos; |
| 4151 | BUG_ON((mddev->dev_sectors & |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4152 | ~((sector_t)reshape_sectors - 1)) |
| 4153 | - reshape_sectors - stripe_addr |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4154 | != sector_nr); |
| 4155 | } else { |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4156 | BUG_ON(writepos != sector_nr + reshape_sectors); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4157 | stripe_addr = sector_nr; |
| 4158 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4159 | INIT_LIST_HEAD(&stripes); |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4160 | for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4161 | int j; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4162 | int skipped_disk = 0; |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4163 | sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4164 | set_bit(STRIPE_EXPANDING, &sh->state); |
| 4165 | atomic_inc(&conf->reshape_stripes); |
| 4166 | /* If any of this stripe is beyond the end of the old |
| 4167 | * array, then we need to zero those blocks |
| 4168 | */ |
| 4169 | for (j=sh->disks; j--;) { |
| 4170 | sector_t s; |
| 4171 | if (j == sh->pd_idx) |
| 4172 | continue; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4173 | if (conf->level == 6 && |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4174 | j == sh->qd_idx) |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4175 | continue; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 4176 | s = compute_blocknr(sh, j, 0); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4177 | if (s < raid5_size(mddev, 0, 0)) { |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4178 | skipped_disk = 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4179 | continue; |
| 4180 | } |
| 4181 | memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE); |
| 4182 | set_bit(R5_Expanded, &sh->dev[j].flags); |
| 4183 | set_bit(R5_UPTODATE, &sh->dev[j].flags); |
| 4184 | } |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4185 | if (!skipped_disk) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4186 | set_bit(STRIPE_EXPAND_READY, &sh->state); |
| 4187 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4188 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4189 | list_add(&sh->lru, &stripes); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4190 | } |
| 4191 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4192 | if (mddev->delta_disks < 0) |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4193 | conf->reshape_progress -= reshape_sectors * new_data_disks; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4194 | else |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4195 | conf->reshape_progress += reshape_sectors * new_data_disks; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4196 | spin_unlock_irq(&conf->device_lock); |
| 4197 | /* Ok, those stripe are ready. We can start scheduling |
| 4198 | * reads on the source stripes. |
| 4199 | * The source stripes are determined by mapping the first and last |
| 4200 | * block on the destination stripes. |
| 4201 | */ |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4202 | first_sector = |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4203 | raid5_compute_sector(conf, stripe_addr*(new_data_disks), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4204 | 1, &dd_idx, NULL); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4205 | last_sector = |
NeilBrown | 0e6e027 | 2009-06-09 16:32:22 +1000 | [diff] [blame] | 4206 | raid5_compute_sector(conf, ((stripe_addr+reshape_sectors) |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 4207 | * new_data_disks - 1), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4208 | 1, &dd_idx, NULL); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4209 | if (last_sector >= mddev->dev_sectors) |
| 4210 | last_sector = mddev->dev_sectors - 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4211 | while (first_sector <= last_sector) { |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4212 | sh = get_active_stripe(conf, first_sector, 1, 0, 1); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4213 | set_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 4214 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4215 | release_stripe(sh); |
| 4216 | first_sector += STRIPE_SECTORS; |
| 4217 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4218 | /* Now that the sources are clearly marked, we can release |
| 4219 | * the destination stripes |
| 4220 | */ |
| 4221 | while (!list_empty(&stripes)) { |
| 4222 | sh = list_entry(stripes.next, struct stripe_head, lru); |
| 4223 | list_del_init(&sh->lru); |
| 4224 | release_stripe(sh); |
| 4225 | } |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4226 | /* If this takes us to the resync_max point where we have to pause, |
| 4227 | * then we need to write out the superblock. |
| 4228 | */ |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4229 | sector_nr += reshape_sectors; |
NeilBrown | c03f6a1 | 2009-04-17 11:06:30 +1000 | [diff] [blame] | 4230 | if ((sector_nr - mddev->curr_resync_completed) * 2 |
| 4231 | >= mddev->resync_max - mddev->curr_resync_completed) { |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4232 | /* Cannot proceed until we've updated the superblock... */ |
| 4233 | wait_event(conf->wait_for_overlap, |
| 4234 | atomic_read(&conf->reshape_stripes) == 0); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4235 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 48606a9 | 2009-06-18 09:14:12 +1000 | [diff] [blame] | 4236 | mddev->curr_resync_completed = mddev->curr_resync + reshape_sectors; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4237 | conf->reshape_checkpoint = jiffies; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4238 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 4239 | md_wakeup_thread(mddev->thread); |
| 4240 | wait_event(mddev->sb_wait, |
| 4241 | !test_bit(MD_CHANGE_DEVS, &mddev->flags) |
| 4242 | || kthread_should_stop()); |
| 4243 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4244 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4245 | spin_unlock_irq(&conf->device_lock); |
| 4246 | wake_up(&conf->wait_for_overlap); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4247 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4248 | } |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4249 | return reshape_sectors; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4250 | } |
| 4251 | |
| 4252 | /* FIXME go_faster isn't used */ |
| 4253 | static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster) |
| 4254 | { |
H Hartley Sweeten | 7b92813 | 2010-03-08 16:02:40 +1100 | [diff] [blame] | 4255 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4256 | struct stripe_head *sh; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4257 | sector_t max_sector = mddev->dev_sectors; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4258 | int sync_blocks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4259 | int still_degraded = 0; |
| 4260 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4261 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4262 | if (sector_nr >= max_sector) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4263 | /* just being told to finish up .. nothing much to do */ |
| 4264 | unplug_slaves(mddev); |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 4265 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 4266 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { |
| 4267 | end_reshape(conf); |
| 4268 | return 0; |
| 4269 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4270 | |
| 4271 | if (mddev->curr_resync < max_sector) /* aborted */ |
| 4272 | bitmap_end_sync(mddev->bitmap, mddev->curr_resync, |
| 4273 | &sync_blocks, 1); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4274 | else /* completed sync */ |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4275 | conf->fullsync = 0; |
| 4276 | bitmap_close_sync(mddev->bitmap); |
| 4277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4278 | return 0; |
| 4279 | } |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4280 | |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4281 | /* Allow raid5_quiesce to complete */ |
| 4282 | wait_event(conf->wait_for_overlap, conf->quiesce != 2); |
| 4283 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4284 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 4285 | return reshape_request(mddev, sector_nr, skipped); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4286 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4287 | /* No need to check resync_max as we never do more than one |
| 4288 | * stripe, and as resync_max will always be on a chunk boundary, |
| 4289 | * if the check in md_do_sync didn't fire, there is no chance |
| 4290 | * of overstepping resync_max here |
| 4291 | */ |
| 4292 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4293 | /* if there is too many failed drives and we are trying |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4294 | * to resync, then assert that we are finished, because there is |
| 4295 | * nothing we can do. |
| 4296 | */ |
NeilBrown | 3285edf | 2006-06-26 00:27:55 -0700 | [diff] [blame] | 4297 | if (mddev->degraded >= conf->max_degraded && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4298 | test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4299 | sector_t rv = mddev->dev_sectors - sector_nr; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 4300 | *skipped = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4301 | return rv; |
| 4302 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4303 | if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) && |
NeilBrown | 3855ad9 | 2005-11-08 21:39:38 -0800 | [diff] [blame] | 4304 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4305 | !conf->fullsync && sync_blocks >= STRIPE_SECTORS) { |
| 4306 | /* we can skip this block, and probably more */ |
| 4307 | sync_blocks /= STRIPE_SECTORS; |
| 4308 | *skipped = 1; |
| 4309 | return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */ |
| 4310 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4311 | |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 4312 | |
| 4313 | bitmap_cond_end_sync(mddev->bitmap, sector_nr); |
| 4314 | |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4315 | sh = get_active_stripe(conf, sector_nr, 0, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4316 | if (sh == NULL) { |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4317 | sh = get_active_stripe(conf, sector_nr, 0, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4318 | /* make sure we don't swamp the stripe cache if someone else |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4319 | * is trying to get access |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4320 | */ |
Nishanth Aravamudan | 66c006a | 2005-11-07 01:01:17 -0800 | [diff] [blame] | 4321 | schedule_timeout_uninterruptible(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4322 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4323 | /* Need to check if array will still be degraded after recovery/resync |
| 4324 | * We don't need to check the 'failed' flag as when that gets set, |
| 4325 | * recovery aborts. |
| 4326 | */ |
NeilBrown | f001a70 | 2009-06-09 14:30:31 +1000 | [diff] [blame] | 4327 | for (i = 0; i < conf->raid_disks; i++) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4328 | if (conf->disks[i].rdev == NULL) |
| 4329 | still_degraded = 1; |
| 4330 | |
| 4331 | bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded); |
| 4332 | |
| 4333 | spin_lock(&sh->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4334 | set_bit(STRIPE_SYNCING, &sh->state); |
| 4335 | clear_bit(STRIPE_INSYNC, &sh->state); |
| 4336 | spin_unlock(&sh->lock); |
| 4337 | |
NeilBrown | 1442577 | 2009-10-16 15:55:25 +1100 | [diff] [blame] | 4338 | handle_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4339 | release_stripe(sh); |
| 4340 | |
| 4341 | return STRIPE_SECTORS; |
| 4342 | } |
| 4343 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4344 | static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio) |
| 4345 | { |
| 4346 | /* We may not be able to submit a whole bio at once as there |
| 4347 | * may not be enough stripe_heads available. |
| 4348 | * We cannot pre-allocate enough stripe_heads as we may need |
| 4349 | * more than exist in the cache (if we allow ever large chunks). |
| 4350 | * So we do one stripe head at a time and record in |
| 4351 | * ->bi_hw_segments how many have been done. |
| 4352 | * |
| 4353 | * We *know* that this entire raid_bio is in one chunk, so |
| 4354 | * it will be only one 'dd_idx' and only need one call to raid5_compute_sector. |
| 4355 | */ |
| 4356 | struct stripe_head *sh; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4357 | int dd_idx; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4358 | sector_t sector, logical_sector, last_sector; |
| 4359 | int scnt = 0; |
| 4360 | int remaining; |
| 4361 | int handled = 0; |
| 4362 | |
| 4363 | logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4364 | sector = raid5_compute_sector(conf, logical_sector, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4365 | 0, &dd_idx, NULL); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4366 | last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9); |
| 4367 | |
| 4368 | for (; logical_sector < last_sector; |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4369 | logical_sector += STRIPE_SECTORS, |
| 4370 | sector += STRIPE_SECTORS, |
| 4371 | scnt++) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4372 | |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4373 | if (scnt < raid5_bi_hw_segments(raid_bio)) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4374 | /* already done this stripe */ |
| 4375 | continue; |
| 4376 | |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4377 | sh = get_active_stripe(conf, sector, 0, 1, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4378 | |
| 4379 | if (!sh) { |
| 4380 | /* failed to get a stripe - must wait */ |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4381 | raid5_set_bi_hw_segments(raid_bio, scnt); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4382 | conf->retry_read_aligned = raid_bio; |
| 4383 | return handled; |
| 4384 | } |
| 4385 | |
| 4386 | set_bit(R5_ReadError, &sh->dev[dd_idx].flags); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4387 | if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) { |
| 4388 | release_stripe(sh); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4389 | raid5_set_bi_hw_segments(raid_bio, scnt); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4390 | conf->retry_read_aligned = raid_bio; |
| 4391 | return handled; |
| 4392 | } |
| 4393 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4394 | handle_stripe(sh); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4395 | release_stripe(sh); |
| 4396 | handled++; |
| 4397 | } |
| 4398 | spin_lock_irq(&conf->device_lock); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4399 | remaining = raid5_dec_bi_phys_segments(raid_bio); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4400 | spin_unlock_irq(&conf->device_lock); |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 4401 | if (remaining == 0) |
| 4402 | bio_endio(raid_bio, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4403 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
| 4404 | wake_up(&conf->wait_for_stripe); |
| 4405 | return handled; |
| 4406 | } |
| 4407 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4409 | /* |
| 4410 | * This is our raid5 kernel thread. |
| 4411 | * |
| 4412 | * We scan the hash table for stripes which can be handled now. |
| 4413 | * During the scan, completed stripes are saved for us by the interrupt |
| 4414 | * handler, so that they will not have to wait for our next wakeup. |
| 4415 | */ |
NeilBrown | 6ed3003 | 2008-02-06 01:40:00 -0800 | [diff] [blame] | 4416 | static void raid5d(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4417 | { |
| 4418 | struct stripe_head *sh; |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 4419 | raid5_conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4420 | int handled; |
| 4421 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4422 | pr_debug("+++ raid5d active\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4423 | |
| 4424 | md_check_recovery(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4425 | |
| 4426 | handled = 0; |
| 4427 | spin_lock_irq(&conf->device_lock); |
| 4428 | while (1) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4429 | struct bio *bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4430 | |
NeilBrown | ae3c20c | 2006-07-10 04:44:17 -0700 | [diff] [blame] | 4431 | if (conf->seq_flush != conf->seq_write) { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4432 | int seq = conf->seq_flush; |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 4433 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4434 | bitmap_unplug(mddev->bitmap); |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 4435 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4436 | conf->seq_write = seq; |
| 4437 | activate_bit_delay(conf); |
| 4438 | } |
| 4439 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4440 | while ((bio = remove_bio_from_retry(conf))) { |
| 4441 | int ok; |
| 4442 | spin_unlock_irq(&conf->device_lock); |
| 4443 | ok = retry_aligned_read(conf, bio); |
| 4444 | spin_lock_irq(&conf->device_lock); |
| 4445 | if (!ok) |
| 4446 | break; |
| 4447 | handled++; |
| 4448 | } |
| 4449 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4450 | sh = __get_priority_stripe(conf); |
| 4451 | |
Dan Williams | c9f21aa | 2008-07-23 12:05:51 -0700 | [diff] [blame] | 4452 | if (!sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4453 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4454 | spin_unlock_irq(&conf->device_lock); |
| 4455 | |
| 4456 | handled++; |
Dan Williams | 417b8d4 | 2009-10-16 16:25:22 +1100 | [diff] [blame] | 4457 | handle_stripe(sh); |
| 4458 | release_stripe(sh); |
| 4459 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4460 | |
| 4461 | spin_lock_irq(&conf->device_lock); |
| 4462 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4463 | pr_debug("%d stripes handled\n", handled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4464 | |
| 4465 | spin_unlock_irq(&conf->device_lock); |
| 4466 | |
Dan Williams | c9f21aa | 2008-07-23 12:05:51 -0700 | [diff] [blame] | 4467 | async_tx_issue_pending_all(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4468 | unplug_slaves(mddev); |
| 4469 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4470 | pr_debug("--- raid5d inactive\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4471 | } |
| 4472 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4473 | static ssize_t |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4474 | raid5_show_stripe_cache_size(mddev_t *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4475 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 4476 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4477 | if (conf) |
| 4478 | return sprintf(page, "%d\n", conf->max_nr_stripes); |
| 4479 | else |
| 4480 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4481 | } |
| 4482 | |
| 4483 | static ssize_t |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4484 | 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] | 4485 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 4486 | raid5_conf_t *conf = mddev->private; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4487 | unsigned long new; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4488 | int err; |
| 4489 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4490 | if (len >= PAGE_SIZE) |
| 4491 | return -EINVAL; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4492 | if (!conf) |
| 4493 | return -ENODEV; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4494 | |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4495 | if (strict_strtoul(page, 10, &new)) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4496 | return -EINVAL; |
| 4497 | if (new <= 16 || new > 32768) |
| 4498 | return -EINVAL; |
| 4499 | while (new < conf->max_nr_stripes) { |
| 4500 | if (drop_one_stripe(conf)) |
| 4501 | conf->max_nr_stripes--; |
| 4502 | else |
| 4503 | break; |
| 4504 | } |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4505 | err = md_allow_write(mddev); |
| 4506 | if (err) |
| 4507 | return err; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4508 | while (new > conf->max_nr_stripes) { |
| 4509 | if (grow_one_stripe(conf)) |
| 4510 | conf->max_nr_stripes++; |
| 4511 | else break; |
| 4512 | } |
| 4513 | return len; |
| 4514 | } |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4515 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4516 | static struct md_sysfs_entry |
| 4517 | raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR, |
| 4518 | raid5_show_stripe_cache_size, |
| 4519 | raid5_store_stripe_cache_size); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4520 | |
| 4521 | static ssize_t |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4522 | raid5_show_preread_threshold(mddev_t *mddev, char *page) |
| 4523 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 4524 | raid5_conf_t *conf = mddev->private; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4525 | if (conf) |
| 4526 | return sprintf(page, "%d\n", conf->bypass_threshold); |
| 4527 | else |
| 4528 | return 0; |
| 4529 | } |
| 4530 | |
| 4531 | static ssize_t |
| 4532 | raid5_store_preread_threshold(mddev_t *mddev, const char *page, size_t len) |
| 4533 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 4534 | raid5_conf_t *conf = mddev->private; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4535 | unsigned long new; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4536 | if (len >= PAGE_SIZE) |
| 4537 | return -EINVAL; |
| 4538 | if (!conf) |
| 4539 | return -ENODEV; |
| 4540 | |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4541 | if (strict_strtoul(page, 10, &new)) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4542 | return -EINVAL; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4543 | if (new > conf->max_nr_stripes) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4544 | return -EINVAL; |
| 4545 | conf->bypass_threshold = new; |
| 4546 | return len; |
| 4547 | } |
| 4548 | |
| 4549 | static struct md_sysfs_entry |
| 4550 | raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold, |
| 4551 | S_IRUGO | S_IWUSR, |
| 4552 | raid5_show_preread_threshold, |
| 4553 | raid5_store_preread_threshold); |
| 4554 | |
| 4555 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4556 | stripe_cache_active_show(mddev_t *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4557 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 4558 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4559 | if (conf) |
| 4560 | return sprintf(page, "%d\n", atomic_read(&conf->active_stripes)); |
| 4561 | else |
| 4562 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4563 | } |
| 4564 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4565 | static struct md_sysfs_entry |
| 4566 | raid5_stripecache_active = __ATTR_RO(stripe_cache_active); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4567 | |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4568 | static struct attribute *raid5_attrs[] = { |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4569 | &raid5_stripecache_size.attr, |
| 4570 | &raid5_stripecache_active.attr, |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4571 | &raid5_preread_bypass_threshold.attr, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4572 | NULL, |
| 4573 | }; |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4574 | static struct attribute_group raid5_attrs_group = { |
| 4575 | .name = NULL, |
| 4576 | .attrs = raid5_attrs, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4577 | }; |
| 4578 | |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4579 | static sector_t |
| 4580 | raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks) |
| 4581 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 4582 | raid5_conf_t *conf = mddev->private; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4583 | |
| 4584 | if (!sectors) |
| 4585 | sectors = mddev->dev_sectors; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4586 | if (!raid_disks) |
NeilBrown | 7ec0547 | 2009-03-31 15:10:36 +1100 | [diff] [blame] | 4587 | /* size is defined by the smallest of previous and new size */ |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4588 | raid_disks = min(conf->raid_disks, conf->previous_raid_disks); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4589 | |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4590 | sectors &= ~((sector_t)mddev->chunk_sectors - 1); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4591 | sectors &= ~((sector_t)mddev->new_chunk_sectors - 1); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4592 | return sectors * (raid_disks - conf->max_degraded); |
| 4593 | } |
| 4594 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4595 | static void raid5_free_percpu(raid5_conf_t *conf) |
| 4596 | { |
| 4597 | struct raid5_percpu *percpu; |
| 4598 | unsigned long cpu; |
| 4599 | |
| 4600 | if (!conf->percpu) |
| 4601 | return; |
| 4602 | |
| 4603 | get_online_cpus(); |
| 4604 | for_each_possible_cpu(cpu) { |
| 4605 | percpu = per_cpu_ptr(conf->percpu, cpu); |
| 4606 | safe_put_page(percpu->spare_page); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4607 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4608 | } |
| 4609 | #ifdef CONFIG_HOTPLUG_CPU |
| 4610 | unregister_cpu_notifier(&conf->cpu_notify); |
| 4611 | #endif |
| 4612 | put_online_cpus(); |
| 4613 | |
| 4614 | free_percpu(conf->percpu); |
| 4615 | } |
| 4616 | |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 4617 | static void free_conf(raid5_conf_t *conf) |
| 4618 | { |
| 4619 | shrink_stripes(conf); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4620 | raid5_free_percpu(conf); |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 4621 | kfree(conf->disks); |
| 4622 | kfree(conf->stripe_hashtbl); |
| 4623 | kfree(conf); |
| 4624 | } |
| 4625 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4626 | #ifdef CONFIG_HOTPLUG_CPU |
| 4627 | static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action, |
| 4628 | void *hcpu) |
| 4629 | { |
| 4630 | raid5_conf_t *conf = container_of(nfb, raid5_conf_t, cpu_notify); |
| 4631 | long cpu = (long)hcpu; |
| 4632 | struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu); |
| 4633 | |
| 4634 | switch (action) { |
| 4635 | case CPU_UP_PREPARE: |
| 4636 | case CPU_UP_PREPARE_FROZEN: |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4637 | if (conf->level == 6 && !percpu->spare_page) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4638 | percpu->spare_page = alloc_page(GFP_KERNEL); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4639 | if (!percpu->scribble) |
| 4640 | percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL); |
| 4641 | |
| 4642 | if (!percpu->scribble || |
| 4643 | (conf->level == 6 && !percpu->spare_page)) { |
| 4644 | safe_put_page(percpu->spare_page); |
| 4645 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4646 | pr_err("%s: failed memory allocation for cpu%ld\n", |
| 4647 | __func__, cpu); |
Akinobu Mita | 55af6bb | 2010-05-26 14:43:35 -0700 | [diff] [blame] | 4648 | return notifier_from_errno(-ENOMEM); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4649 | } |
| 4650 | break; |
| 4651 | case CPU_DEAD: |
| 4652 | case CPU_DEAD_FROZEN: |
| 4653 | safe_put_page(percpu->spare_page); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4654 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4655 | percpu->spare_page = NULL; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4656 | percpu->scribble = NULL; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4657 | break; |
| 4658 | default: |
| 4659 | break; |
| 4660 | } |
| 4661 | return NOTIFY_OK; |
| 4662 | } |
| 4663 | #endif |
| 4664 | |
| 4665 | static int raid5_alloc_percpu(raid5_conf_t *conf) |
| 4666 | { |
| 4667 | unsigned long cpu; |
| 4668 | struct page *spare_page; |
Tejun Heo | a29d8b8 | 2010-02-02 14:39:15 +0900 | [diff] [blame] | 4669 | struct raid5_percpu __percpu *allcpus; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4670 | void *scribble; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4671 | int err; |
| 4672 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4673 | allcpus = alloc_percpu(struct raid5_percpu); |
| 4674 | if (!allcpus) |
| 4675 | return -ENOMEM; |
| 4676 | conf->percpu = allcpus; |
| 4677 | |
| 4678 | get_online_cpus(); |
| 4679 | err = 0; |
| 4680 | for_each_present_cpu(cpu) { |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4681 | if (conf->level == 6) { |
| 4682 | spare_page = alloc_page(GFP_KERNEL); |
| 4683 | if (!spare_page) { |
| 4684 | err = -ENOMEM; |
| 4685 | break; |
| 4686 | } |
| 4687 | per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page; |
| 4688 | } |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4689 | scribble = kmalloc(conf->scribble_len, GFP_KERNEL); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4690 | if (!scribble) { |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4691 | err = -ENOMEM; |
| 4692 | break; |
| 4693 | } |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4694 | per_cpu_ptr(conf->percpu, cpu)->scribble = scribble; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4695 | } |
| 4696 | #ifdef CONFIG_HOTPLUG_CPU |
| 4697 | conf->cpu_notify.notifier_call = raid456_cpu_notify; |
| 4698 | conf->cpu_notify.priority = 0; |
| 4699 | if (err == 0) |
| 4700 | err = register_cpu_notifier(&conf->cpu_notify); |
| 4701 | #endif |
| 4702 | put_online_cpus(); |
| 4703 | |
| 4704 | return err; |
| 4705 | } |
| 4706 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4707 | static raid5_conf_t *setup_conf(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4708 | { |
| 4709 | raid5_conf_t *conf; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4710 | int raid_disk, memory, max_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4711 | mdk_rdev_t *rdev; |
| 4712 | struct disk_info *disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4713 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4714 | if (mddev->new_level != 5 |
| 4715 | && mddev->new_level != 4 |
| 4716 | && mddev->new_level != 6) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4717 | 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] | 4718 | mdname(mddev), mddev->new_level); |
| 4719 | return ERR_PTR(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4720 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4721 | if ((mddev->new_level == 5 |
| 4722 | && !algorithm_valid_raid5(mddev->new_layout)) || |
| 4723 | (mddev->new_level == 6 |
| 4724 | && !algorithm_valid_raid6(mddev->new_layout))) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4725 | printk(KERN_ERR "md/raid:%s: layout %d not supported\n", |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4726 | mdname(mddev), mddev->new_layout); |
| 4727 | return ERR_PTR(-EIO); |
| 4728 | } |
| 4729 | if (mddev->new_level == 6 && mddev->raid_disks < 4) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4730 | 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] | 4731 | mdname(mddev), mddev->raid_disks); |
| 4732 | return ERR_PTR(-EINVAL); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4733 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4734 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4735 | if (!mddev->new_chunk_sectors || |
| 4736 | (mddev->new_chunk_sectors << 9) % PAGE_SIZE || |
| 4737 | !is_power_of_2(mddev->new_chunk_sectors)) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4738 | printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n", |
| 4739 | mdname(mddev), mddev->new_chunk_sectors << 9); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4740 | return ERR_PTR(-EINVAL); |
NeilBrown | 4bbf377 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4741 | } |
| 4742 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4743 | conf = kzalloc(sizeof(raid5_conf_t), GFP_KERNEL); |
| 4744 | if (conf == NULL) |
| 4745 | goto abort; |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 4746 | spin_lock_init(&conf->device_lock); |
| 4747 | init_waitqueue_head(&conf->wait_for_stripe); |
| 4748 | init_waitqueue_head(&conf->wait_for_overlap); |
| 4749 | INIT_LIST_HEAD(&conf->handle_list); |
| 4750 | INIT_LIST_HEAD(&conf->hold_list); |
| 4751 | INIT_LIST_HEAD(&conf->delayed_list); |
| 4752 | INIT_LIST_HEAD(&conf->bitmap_list); |
| 4753 | INIT_LIST_HEAD(&conf->inactive_list); |
| 4754 | atomic_set(&conf->active_stripes, 0); |
| 4755 | atomic_set(&conf->preread_active_stripes, 0); |
| 4756 | atomic_set(&conf->active_aligned_reads, 0); |
| 4757 | conf->bypass_threshold = BYPASS_THRESHOLD; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4758 | |
| 4759 | conf->raid_disks = mddev->raid_disks; |
| 4760 | if (mddev->reshape_position == MaxSector) |
| 4761 | conf->previous_raid_disks = mddev->raid_disks; |
| 4762 | else |
| 4763 | conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4764 | max_disks = max(conf->raid_disks, conf->previous_raid_disks); |
| 4765 | conf->scribble_len = scribble_len(max_disks); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4766 | |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4767 | conf->disks = kzalloc(max_disks * sizeof(struct disk_info), |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4768 | GFP_KERNEL); |
| 4769 | if (!conf->disks) |
| 4770 | goto abort; |
| 4771 | |
| 4772 | conf->mddev = mddev; |
| 4773 | |
| 4774 | if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL) |
| 4775 | goto abort; |
| 4776 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4777 | conf->level = mddev->new_level; |
| 4778 | if (raid5_alloc_percpu(conf) != 0) |
| 4779 | goto abort; |
| 4780 | |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4781 | pr_debug("raid456: run(%s) called.\n", mdname(mddev)); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4782 | |
| 4783 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 4784 | raid_disk = rdev->raid_disk; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4785 | if (raid_disk >= max_disks |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4786 | || raid_disk < 0) |
| 4787 | continue; |
| 4788 | disk = conf->disks + raid_disk; |
| 4789 | |
| 4790 | disk->rdev = rdev; |
| 4791 | |
| 4792 | if (test_bit(In_sync, &rdev->flags)) { |
| 4793 | char b[BDEVNAME_SIZE]; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4794 | printk(KERN_INFO "md/raid:%s: device %s operational as raid" |
| 4795 | " disk %d\n", |
| 4796 | mdname(mddev), bdevname(rdev->bdev, b), raid_disk); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4797 | } else |
| 4798 | /* Cannot rely on bitmap to complete recovery */ |
| 4799 | conf->fullsync = 1; |
| 4800 | } |
| 4801 | |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 4802 | conf->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4803 | conf->level = mddev->new_level; |
| 4804 | if (conf->level == 6) |
| 4805 | conf->max_degraded = 2; |
| 4806 | else |
| 4807 | conf->max_degraded = 1; |
| 4808 | conf->algorithm = mddev->new_layout; |
| 4809 | conf->max_nr_stripes = NR_STRIPES; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4810 | conf->reshape_progress = mddev->reshape_position; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 4811 | if (conf->reshape_progress != MaxSector) { |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 4812 | conf->prev_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 4813 | conf->prev_algo = mddev->layout; |
| 4814 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4815 | |
| 4816 | memory = conf->max_nr_stripes * (sizeof(struct stripe_head) + |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4817 | max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4818 | if (grow_stripes(conf, conf->max_nr_stripes)) { |
| 4819 | printk(KERN_ERR |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4820 | "md/raid:%s: couldn't allocate %dkB for buffers\n", |
| 4821 | mdname(mddev), memory); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4822 | goto abort; |
| 4823 | } else |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4824 | printk(KERN_INFO "md/raid:%s: allocated %dkB\n", |
| 4825 | mdname(mddev), memory); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4826 | |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 4827 | conf->thread = md_register_thread(raid5d, mddev, NULL); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4828 | if (!conf->thread) { |
| 4829 | printk(KERN_ERR |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4830 | "md/raid:%s: couldn't allocate thread.\n", |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4831 | mdname(mddev)); |
| 4832 | goto abort; |
| 4833 | } |
| 4834 | |
| 4835 | return conf; |
| 4836 | |
| 4837 | abort: |
| 4838 | if (conf) { |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 4839 | free_conf(conf); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4840 | return ERR_PTR(-EIO); |
| 4841 | } else |
| 4842 | return ERR_PTR(-ENOMEM); |
| 4843 | } |
| 4844 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 4845 | |
| 4846 | static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded) |
| 4847 | { |
| 4848 | switch (algo) { |
| 4849 | case ALGORITHM_PARITY_0: |
| 4850 | if (raid_disk < max_degraded) |
| 4851 | return 1; |
| 4852 | break; |
| 4853 | case ALGORITHM_PARITY_N: |
| 4854 | if (raid_disk >= raid_disks - max_degraded) |
| 4855 | return 1; |
| 4856 | break; |
| 4857 | case ALGORITHM_PARITY_0_6: |
| 4858 | if (raid_disk == 0 || |
| 4859 | raid_disk == raid_disks - 1) |
| 4860 | return 1; |
| 4861 | break; |
| 4862 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 4863 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 4864 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 4865 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 4866 | if (raid_disk == raid_disks - 1) |
| 4867 | return 1; |
| 4868 | } |
| 4869 | return 0; |
| 4870 | } |
| 4871 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4872 | static int run(mddev_t *mddev) |
| 4873 | { |
| 4874 | raid5_conf_t *conf; |
Martin K. Petersen | 8f6c2e4 | 2009-07-01 11:13:45 +1000 | [diff] [blame] | 4875 | int working_disks = 0, chunk_size; |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 4876 | int dirty_parity_disks = 0; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4877 | mdk_rdev_t *rdev; |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 4878 | sector_t reshape_offset = 0; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4879 | |
Andre Noll | 8c6ac86 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 4880 | if (mddev->recovery_cp != MaxSector) |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4881 | printk(KERN_NOTICE "md/raid:%s: not clean" |
Andre Noll | 8c6ac86 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 4882 | " -- starting background reconstruction\n", |
| 4883 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4884 | if (mddev->reshape_position != MaxSector) { |
| 4885 | /* Check that we can continue the reshape. |
| 4886 | * Currently only disks can change, it must |
| 4887 | * increase, and we must be past the point where |
| 4888 | * a stripe over-writes itself |
| 4889 | */ |
| 4890 | sector_t here_new, here_old; |
| 4891 | int old_disks; |
Andre Noll | 18b0033 | 2009-03-31 15:00:56 +1100 | [diff] [blame] | 4892 | int max_degraded = (mddev->level == 6 ? 2 : 1); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4893 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 4894 | if (mddev->new_level != mddev->level) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4895 | printk(KERN_ERR "md/raid:%s: unsupported reshape " |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4896 | "required - aborting.\n", |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4897 | mdname(mddev)); |
| 4898 | return -EINVAL; |
| 4899 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4900 | old_disks = mddev->raid_disks - mddev->delta_disks; |
| 4901 | /* reshape_position must be on a new-stripe boundary, and one |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4902 | * further up in new geometry must map after here in old |
| 4903 | * geometry. |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4904 | */ |
| 4905 | here_new = mddev->reshape_position; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4906 | if (sector_div(here_new, mddev->new_chunk_sectors * |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4907 | (mddev->raid_disks - max_degraded))) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4908 | printk(KERN_ERR "md/raid:%s: reshape_position not " |
| 4909 | "on a stripe boundary\n", mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4910 | return -EINVAL; |
| 4911 | } |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 4912 | reshape_offset = here_new * mddev->new_chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4913 | /* here_new is the stripe we will write to */ |
| 4914 | here_old = mddev->reshape_position; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4915 | sector_div(here_old, mddev->chunk_sectors * |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4916 | (old_disks-max_degraded)); |
| 4917 | /* here_old is the first stripe that we might need to read |
| 4918 | * from */ |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 4919 | if (mddev->delta_disks == 0) { |
| 4920 | /* We cannot be sure it is safe to start an in-place |
| 4921 | * reshape. It is only safe if user-space if monitoring |
| 4922 | * and taking constant backups. |
| 4923 | * mdadm always starts a situation like this in |
| 4924 | * readonly mode so it can take control before |
| 4925 | * allowing any writes. So just check for that. |
| 4926 | */ |
| 4927 | if ((here_new * mddev->new_chunk_sectors != |
| 4928 | here_old * mddev->chunk_sectors) || |
| 4929 | mddev->ro == 0) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4930 | printk(KERN_ERR "md/raid:%s: in-place reshape must be started" |
| 4931 | " in read-only mode - aborting\n", |
| 4932 | mdname(mddev)); |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 4933 | return -EINVAL; |
| 4934 | } |
| 4935 | } else if (mddev->delta_disks < 0 |
| 4936 | ? (here_new * mddev->new_chunk_sectors <= |
| 4937 | here_old * mddev->chunk_sectors) |
| 4938 | : (here_new * mddev->new_chunk_sectors >= |
| 4939 | here_old * mddev->chunk_sectors)) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4940 | /* Reading from the same stripe as writing to - bad */ |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4941 | printk(KERN_ERR "md/raid:%s: reshape_position too early for " |
| 4942 | "auto-recovery - aborting.\n", |
| 4943 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4944 | return -EINVAL; |
| 4945 | } |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4946 | printk(KERN_INFO "md/raid:%s: reshape will continue\n", |
| 4947 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4948 | /* OK, we should be able to continue; */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4949 | } else { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4950 | BUG_ON(mddev->level != mddev->new_level); |
| 4951 | BUG_ON(mddev->layout != mddev->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4952 | BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4953 | BUG_ON(mddev->delta_disks != 0); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4954 | } |
| 4955 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4956 | if (mddev->private == NULL) |
| 4957 | conf = setup_conf(mddev); |
| 4958 | else |
| 4959 | conf = mddev->private; |
| 4960 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4961 | if (IS_ERR(conf)) |
| 4962 | return PTR_ERR(conf); |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 4963 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4964 | mddev->thread = conf->thread; |
| 4965 | conf->thread = NULL; |
| 4966 | mddev->private = conf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4967 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4968 | /* |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4969 | * 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] | 4970 | */ |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 4971 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 4972 | if (rdev->raid_disk < 0) |
| 4973 | continue; |
| 4974 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4975 | working_disks++; |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 4976 | /* This disc is not fully in-sync. However if it |
| 4977 | * just stored parity (beyond the recovery_offset), |
| 4978 | * when we don't need to be concerned about the |
| 4979 | * array being dirty. |
| 4980 | * When reshape goes 'backwards', we never have |
| 4981 | * partially completed devices, so we only need |
| 4982 | * to worry about reshape going forwards. |
| 4983 | */ |
| 4984 | /* Hack because v0.91 doesn't store recovery_offset properly. */ |
| 4985 | if (mddev->major_version == 0 && |
| 4986 | mddev->minor_version > 90) |
| 4987 | rdev->recovery_offset = reshape_offset; |
| 4988 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 4989 | if (rdev->recovery_offset < reshape_offset) { |
| 4990 | /* We need to check old and new layout */ |
| 4991 | if (!only_parity(rdev->raid_disk, |
| 4992 | conf->algorithm, |
| 4993 | conf->raid_disks, |
| 4994 | conf->max_degraded)) |
| 4995 | continue; |
| 4996 | } |
| 4997 | if (!only_parity(rdev->raid_disk, |
| 4998 | conf->prev_algo, |
| 4999 | conf->previous_raid_disks, |
| 5000 | conf->max_degraded)) |
| 5001 | continue; |
| 5002 | dirty_parity_disks++; |
| 5003 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5004 | |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 5005 | mddev->degraded = (max(conf->raid_disks, conf->previous_raid_disks) |
| 5006 | - working_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5007 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5008 | if (mddev->degraded > conf->max_degraded) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5009 | printk(KERN_ERR "md/raid:%s: not enough operational devices" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5010 | " (%d/%d failed)\n", |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 5011 | mdname(mddev), mddev->degraded, conf->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5012 | goto abort; |
| 5013 | } |
| 5014 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5015 | /* device size must be a multiple of chunk size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5016 | mddev->dev_sectors &= ~(mddev->chunk_sectors - 1); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5017 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 5018 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5019 | if (mddev->degraded > dirty_parity_disks && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5020 | mddev->recovery_cp != MaxSector) { |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5021 | if (mddev->ok_start_degraded) |
| 5022 | printk(KERN_WARNING |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5023 | "md/raid:%s: starting dirty degraded array" |
| 5024 | " - data corruption possible.\n", |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5025 | mdname(mddev)); |
| 5026 | else { |
| 5027 | printk(KERN_ERR |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5028 | "md/raid:%s: cannot start dirty degraded array.\n", |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5029 | mdname(mddev)); |
| 5030 | goto abort; |
| 5031 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5032 | } |
| 5033 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5034 | if (mddev->degraded == 0) |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5035 | printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d" |
| 5036 | " devices, algorithm %d\n", mdname(mddev), conf->level, |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 5037 | mddev->raid_disks-mddev->degraded, mddev->raid_disks, |
| 5038 | mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5039 | else |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5040 | printk(KERN_ALERT "md/raid:%s: raid level %d active with %d" |
| 5041 | " out of %d devices, algorithm %d\n", |
| 5042 | mdname(mddev), conf->level, |
| 5043 | mddev->raid_disks - mddev->degraded, |
| 5044 | mddev->raid_disks, mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5045 | |
| 5046 | print_raid5_conf(conf); |
| 5047 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5048 | if (conf->reshape_progress != MaxSector) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5049 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5050 | atomic_set(&conf->reshape_stripes, 0); |
| 5051 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 5052 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 5053 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 5054 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 5055 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 5056 | "reshape"); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5057 | } |
| 5058 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5059 | /* read-ahead size must cover two whole stripes, which is |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5060 | * 2 * (datadisks) * chunksize where 'n' is the number of raid devices |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5061 | */ |
| 5062 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5063 | int data_disks = conf->previous_raid_disks - conf->max_degraded; |
| 5064 | int stripe = data_disks * |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5065 | ((mddev->chunk_sectors << 9) / PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5066 | if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 5067 | mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
| 5068 | } |
| 5069 | |
| 5070 | /* Ok, everything is just fine now */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 5071 | if (mddev->to_remove == &raid5_attrs_group) |
| 5072 | mddev->to_remove = NULL; |
| 5073 | else if (sysfs_create_group(&mddev->kobj, &raid5_attrs_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5074 | printk(KERN_WARNING |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5075 | "md/raid:%s: failed to create sysfs attributes.\n", |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5076 | mdname(mddev)); |
NeilBrown | 7a5febe | 2005-05-16 21:53:16 -0700 | [diff] [blame] | 5077 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5078 | mddev->queue->queue_lock = &conf->device_lock; |
| 5079 | |
NeilBrown | 7a5febe | 2005-05-16 21:53:16 -0700 | [diff] [blame] | 5080 | mddev->queue->unplug_fn = raid5_unplug_device; |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 5081 | mddev->queue->backing_dev_info.congested_data = mddev; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5082 | mddev->queue->backing_dev_info.congested_fn = raid5_congested; |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 5083 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5084 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
NeilBrown | 7a5febe | 2005-05-16 21:53:16 -0700 | [diff] [blame] | 5085 | |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 5086 | blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec); |
Martin K. Petersen | 8f6c2e4 | 2009-07-01 11:13:45 +1000 | [diff] [blame] | 5087 | chunk_size = mddev->chunk_sectors << 9; |
| 5088 | blk_queue_io_min(mddev->queue, chunk_size); |
| 5089 | blk_queue_io_opt(mddev->queue, chunk_size * |
| 5090 | (conf->raid_disks - conf->max_degraded)); |
| 5091 | |
| 5092 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 5093 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 5094 | rdev->data_offset << 9); |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 5095 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5096 | return 0; |
| 5097 | abort: |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5098 | md_unregister_thread(mddev->thread); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5099 | mddev->thread = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5100 | if (conf) { |
| 5101 | print_raid5_conf(conf); |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 5102 | free_conf(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5103 | } |
| 5104 | mddev->private = NULL; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5105 | 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] | 5106 | return -EIO; |
| 5107 | } |
| 5108 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 5109 | static int stop(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5110 | { |
H Hartley Sweeten | 7b92813 | 2010-03-08 16:02:40 +1100 | [diff] [blame] | 5111 | raid5_conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5112 | |
| 5113 | md_unregister_thread(mddev->thread); |
| 5114 | mddev->thread = NULL; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5115 | mddev->queue->backing_dev_info.congested_fn = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5116 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 5117 | free_conf(conf); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 5118 | mddev->private = NULL; |
| 5119 | mddev->to_remove = &raid5_attrs_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5120 | return 0; |
| 5121 | } |
| 5122 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 5123 | #ifdef DEBUG |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5124 | static void print_sh(struct seq_file *seq, struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5125 | { |
| 5126 | int i; |
| 5127 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5128 | seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n", |
| 5129 | (unsigned long long)sh->sector, sh->pd_idx, sh->state); |
| 5130 | seq_printf(seq, "sh %llu, count %d.\n", |
| 5131 | (unsigned long long)sh->sector, atomic_read(&sh->count)); |
| 5132 | seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector); |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5133 | for (i = 0; i < sh->disks; i++) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5134 | seq_printf(seq, "(cache%d: %p %ld) ", |
| 5135 | i, sh->dev[i].page, sh->dev[i].flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5136 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5137 | seq_printf(seq, "\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5138 | } |
| 5139 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5140 | static void printall(struct seq_file *seq, raid5_conf_t *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5141 | { |
| 5142 | struct stripe_head *sh; |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 5143 | struct hlist_node *hn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5144 | int i; |
| 5145 | |
| 5146 | spin_lock_irq(&conf->device_lock); |
| 5147 | for (i = 0; i < NR_HASH; i++) { |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 5148 | hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5149 | if (sh->raid_conf != conf) |
| 5150 | continue; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5151 | print_sh(seq, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5152 | } |
| 5153 | } |
| 5154 | spin_unlock_irq(&conf->device_lock); |
| 5155 | } |
| 5156 | #endif |
| 5157 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5158 | static void status(struct seq_file *seq, mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5159 | { |
H Hartley Sweeten | 7b92813 | 2010-03-08 16:02:40 +1100 | [diff] [blame] | 5160 | raid5_conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5161 | int i; |
| 5162 | |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5163 | seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level, |
| 5164 | mddev->chunk_sectors / 2, mddev->layout); |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 5165 | 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] | 5166 | for (i = 0; i < conf->raid_disks; i++) |
| 5167 | seq_printf (seq, "%s", |
| 5168 | conf->disks[i].rdev && |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5169 | test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5170 | seq_printf (seq, "]"); |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 5171 | #ifdef DEBUG |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5172 | seq_printf (seq, "\n"); |
| 5173 | printall(seq, conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5174 | #endif |
| 5175 | } |
| 5176 | |
| 5177 | static void print_raid5_conf (raid5_conf_t *conf) |
| 5178 | { |
| 5179 | int i; |
| 5180 | struct disk_info *tmp; |
| 5181 | |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5182 | printk(KERN_DEBUG "RAID conf printout:\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5183 | if (!conf) { |
| 5184 | printk("(conf==NULL)\n"); |
| 5185 | return; |
| 5186 | } |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5187 | printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level, |
| 5188 | conf->raid_disks, |
| 5189 | conf->raid_disks - conf->mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5190 | |
| 5191 | for (i = 0; i < conf->raid_disks; i++) { |
| 5192 | char b[BDEVNAME_SIZE]; |
| 5193 | tmp = conf->disks + i; |
| 5194 | if (tmp->rdev) |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5195 | printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n", |
| 5196 | i, !test_bit(Faulty, &tmp->rdev->flags), |
| 5197 | bdevname(tmp->rdev->bdev, b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5198 | } |
| 5199 | } |
| 5200 | |
| 5201 | static int raid5_spare_active(mddev_t *mddev) |
| 5202 | { |
| 5203 | int i; |
| 5204 | raid5_conf_t *conf = mddev->private; |
| 5205 | struct disk_info *tmp; |
| 5206 | |
| 5207 | for (i = 0; i < conf->raid_disks; i++) { |
| 5208 | tmp = conf->disks + i; |
| 5209 | if (tmp->rdev |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5210 | && !test_bit(Faulty, &tmp->rdev->flags) |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 5211 | && !test_and_set_bit(In_sync, &tmp->rdev->flags)) { |
| 5212 | unsigned long flags; |
| 5213 | spin_lock_irqsave(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5214 | mddev->degraded--; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 5215 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5216 | } |
| 5217 | } |
| 5218 | print_raid5_conf(conf); |
| 5219 | return 0; |
| 5220 | } |
| 5221 | |
| 5222 | static int raid5_remove_disk(mddev_t *mddev, int number) |
| 5223 | { |
| 5224 | raid5_conf_t *conf = mddev->private; |
| 5225 | int err = 0; |
| 5226 | mdk_rdev_t *rdev; |
| 5227 | struct disk_info *p = conf->disks + number; |
| 5228 | |
| 5229 | print_raid5_conf(conf); |
| 5230 | rdev = p->rdev; |
| 5231 | if (rdev) { |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5232 | if (number >= conf->raid_disks && |
| 5233 | conf->reshape_progress == MaxSector) |
| 5234 | clear_bit(In_sync, &rdev->flags); |
| 5235 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5236 | if (test_bit(In_sync, &rdev->flags) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5237 | atomic_read(&rdev->nr_pending)) { |
| 5238 | err = -EBUSY; |
| 5239 | goto abort; |
| 5240 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5241 | /* Only remove non-faulty devices if recovery |
| 5242 | * isn't possible. |
| 5243 | */ |
| 5244 | if (!test_bit(Faulty, &rdev->flags) && |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5245 | mddev->degraded <= conf->max_degraded && |
| 5246 | number < conf->raid_disks) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5247 | err = -EBUSY; |
| 5248 | goto abort; |
| 5249 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5250 | p->rdev = NULL; |
Paul E. McKenney | fbd568a3e | 2005-05-01 08:59:04 -0700 | [diff] [blame] | 5251 | synchronize_rcu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5252 | if (atomic_read(&rdev->nr_pending)) { |
| 5253 | /* lost the race, try later */ |
| 5254 | err = -EBUSY; |
| 5255 | p->rdev = rdev; |
| 5256 | } |
| 5257 | } |
| 5258 | abort: |
| 5259 | |
| 5260 | print_raid5_conf(conf); |
| 5261 | return err; |
| 5262 | } |
| 5263 | |
| 5264 | static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev) |
| 5265 | { |
| 5266 | raid5_conf_t *conf = mddev->private; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5267 | int err = -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5268 | int disk; |
| 5269 | struct disk_info *p; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5270 | int first = 0; |
| 5271 | int last = conf->raid_disks - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5272 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5273 | if (mddev->degraded > conf->max_degraded) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5274 | /* no point adding a device */ |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5275 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5276 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5277 | if (rdev->raid_disk >= 0) |
| 5278 | first = last = rdev->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5279 | |
| 5280 | /* |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5281 | * find the disk ... but prefer rdev->saved_raid_disk |
| 5282 | * if possible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5283 | */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5284 | if (rdev->saved_raid_disk >= 0 && |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5285 | rdev->saved_raid_disk >= first && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5286 | conf->disks[rdev->saved_raid_disk].rdev == NULL) |
| 5287 | disk = rdev->saved_raid_disk; |
| 5288 | else |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5289 | disk = first; |
| 5290 | for ( ; disk <= last ; disk++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5291 | if ((p=conf->disks + disk)->rdev == NULL) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5292 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5293 | rdev->raid_disk = disk; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5294 | err = 0; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5295 | if (rdev->saved_raid_disk != disk) |
| 5296 | conf->fullsync = 1; |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 5297 | rcu_assign_pointer(p->rdev, rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5298 | break; |
| 5299 | } |
| 5300 | print_raid5_conf(conf); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5301 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5302 | } |
| 5303 | |
| 5304 | static int raid5_resize(mddev_t *mddev, sector_t sectors) |
| 5305 | { |
| 5306 | /* no resync is happening, and there is enough space |
| 5307 | * on all devices, so we can resize. |
| 5308 | * We need to make sure resync covers any new space. |
| 5309 | * If the array is shrinking we should possibly wait until |
| 5310 | * any io in the removed space completes, but it hardly seems |
| 5311 | * worth it. |
| 5312 | */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5313 | sectors &= ~((sector_t)mddev->chunk_sectors - 1); |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5314 | md_set_array_sectors(mddev, raid5_size(mddev, sectors, |
| 5315 | mddev->raid_disks)); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5316 | if (mddev->array_sectors > |
| 5317 | raid5_size(mddev, sectors, mddev->raid_disks)) |
| 5318 | return -EINVAL; |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 5319 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5320 | revalidate_disk(mddev->gendisk); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5321 | if (sectors > mddev->dev_sectors && mddev->recovery_cp == MaxSector) { |
| 5322 | mddev->recovery_cp = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5323 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5324 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5325 | mddev->dev_sectors = sectors; |
NeilBrown | 4b5c7ae | 2005-07-27 11:43:28 -0700 | [diff] [blame] | 5326 | mddev->resync_max_sectors = sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5327 | return 0; |
| 5328 | } |
| 5329 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5330 | static int check_stripe_cache(mddev_t *mddev) |
| 5331 | { |
| 5332 | /* Can only proceed if there are plenty of stripe_heads. |
| 5333 | * We need a minimum of one full stripe,, and for sensible progress |
| 5334 | * it is best to have about 4 times that. |
| 5335 | * If we require 4 times, then the default 256 4K stripe_heads will |
| 5336 | * allow for chunk sizes up to 256K, which is probably OK. |
| 5337 | * If the chunk size is greater, user-space should request more |
| 5338 | * stripe_heads first. |
| 5339 | */ |
| 5340 | raid5_conf_t *conf = mddev->private; |
| 5341 | if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4 |
| 5342 | > conf->max_nr_stripes || |
| 5343 | ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4 |
| 5344 | > conf->max_nr_stripes) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5345 | printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n", |
| 5346 | mdname(mddev), |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5347 | ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9) |
| 5348 | / STRIPE_SIZE)*4); |
| 5349 | return 0; |
| 5350 | } |
| 5351 | return 1; |
| 5352 | } |
| 5353 | |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5354 | static int check_reshape(mddev_t *mddev) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5355 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 5356 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5357 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5358 | if (mddev->delta_disks == 0 && |
| 5359 | mddev->new_layout == mddev->layout && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5360 | mddev->new_chunk_sectors == mddev->chunk_sectors) |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5361 | return 0; /* nothing to do */ |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5362 | if (mddev->bitmap) |
| 5363 | /* Cannot grow a bitmap yet */ |
| 5364 | return -EBUSY; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5365 | if (mddev->degraded > conf->max_degraded) |
| 5366 | return -EINVAL; |
| 5367 | if (mddev->delta_disks < 0) { |
| 5368 | /* We might be able to shrink, but the devices must |
| 5369 | * be made bigger first. |
| 5370 | * For raid6, 4 is the minimum size. |
| 5371 | * Otherwise 2 is the minimum |
| 5372 | */ |
| 5373 | int min = 2; |
| 5374 | if (mddev->level == 6) |
| 5375 | min = 4; |
| 5376 | if (mddev->raid_disks + mddev->delta_disks < min) |
| 5377 | return -EINVAL; |
| 5378 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5379 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5380 | if (!check_stripe_cache(mddev)) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5381 | return -ENOSPC; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5382 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5383 | return resize_stripes(conf, conf->raid_disks + mddev->delta_disks); |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5384 | } |
| 5385 | |
| 5386 | static int raid5_start_reshape(mddev_t *mddev) |
| 5387 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 5388 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5389 | mdk_rdev_t *rdev; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5390 | int spares = 0; |
| 5391 | int added_devices = 0; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 5392 | unsigned long flags; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5393 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5394 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5395 | return -EBUSY; |
| 5396 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5397 | if (!check_stripe_cache(mddev)) |
| 5398 | return -ENOSPC; |
| 5399 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5400 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5401 | if (rdev->raid_disk < 0 && |
| 5402 | !test_bit(Faulty, &rdev->flags)) |
| 5403 | spares++; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5404 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5405 | if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5406 | /* Not enough devices even to make a degraded array |
| 5407 | * of that size |
| 5408 | */ |
| 5409 | return -EINVAL; |
| 5410 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5411 | /* Refuse to reduce size of the array. Any reductions in |
| 5412 | * array size must be through explicit setting of array_size |
| 5413 | * attribute. |
| 5414 | */ |
| 5415 | if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks) |
| 5416 | < mddev->array_sectors) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5417 | printk(KERN_ERR "md/raid:%s: array size must be reduced " |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5418 | "before number of disks\n", mdname(mddev)); |
| 5419 | return -EINVAL; |
| 5420 | } |
| 5421 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5422 | atomic_set(&conf->reshape_stripes, 0); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5423 | spin_lock_irq(&conf->device_lock); |
| 5424 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5425 | conf->raid_disks += mddev->delta_disks; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5426 | conf->prev_chunk_sectors = conf->chunk_sectors; |
| 5427 | conf->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5428 | conf->prev_algo = conf->algorithm; |
| 5429 | conf->algorithm = mddev->new_layout; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5430 | if (mddev->delta_disks < 0) |
| 5431 | conf->reshape_progress = raid5_size(mddev, 0, 0); |
| 5432 | else |
| 5433 | conf->reshape_progress = 0; |
| 5434 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 5435 | conf->generation++; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5436 | spin_unlock_irq(&conf->device_lock); |
| 5437 | |
| 5438 | /* Add some new drives, as many as will fit. |
| 5439 | * We know there are enough to make the newly sized array work. |
| 5440 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5441 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5442 | if (rdev->raid_disk < 0 && |
| 5443 | !test_bit(Faulty, &rdev->flags)) { |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5444 | if (raid5_add_disk(mddev, rdev) == 0) { |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5445 | char nm[20]; |
NeilBrown | 9eb07c2 | 2010-02-09 12:31:47 +1100 | [diff] [blame] | 5446 | if (rdev->raid_disk >= conf->previous_raid_disks) { |
NeilBrown | 7ef9014 | 2009-11-13 17:40:51 +1100 | [diff] [blame] | 5447 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 9eb07c2 | 2010-02-09 12:31:47 +1100 | [diff] [blame] | 5448 | added_devices++; |
| 5449 | } else |
NeilBrown | 7ef9014 | 2009-11-13 17:40:51 +1100 | [diff] [blame] | 5450 | rdev->recovery_offset = 0; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5451 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5452 | if (sysfs_create_link(&mddev->kobj, |
| 5453 | &rdev->kobj, nm)) |
| 5454 | printk(KERN_WARNING |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5455 | "md/raid:%s: failed to create " |
| 5456 | " link %s\n", |
| 5457 | mdname(mddev), nm); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5458 | } else |
| 5459 | break; |
| 5460 | } |
| 5461 | |
NeilBrown | 9eb07c2 | 2010-02-09 12:31:47 +1100 | [diff] [blame] | 5462 | /* When a reshape changes the number of devices, ->degraded |
| 5463 | * is measured against the large of the pre and post number of |
| 5464 | * devices.*/ |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5465 | if (mddev->delta_disks > 0) { |
| 5466 | spin_lock_irqsave(&conf->device_lock, flags); |
NeilBrown | 9eb07c2 | 2010-02-09 12:31:47 +1100 | [diff] [blame] | 5467 | mddev->degraded += (conf->raid_disks - conf->previous_raid_disks) |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5468 | - added_devices; |
| 5469 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 5470 | } |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5471 | mddev->raid_disks = conf->raid_disks; |
NeilBrown | e516402 | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 5472 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5473 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5474 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5475 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 5476 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 5477 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 5478 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 5479 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 5480 | "reshape"); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5481 | if (!mddev->sync_thread) { |
| 5482 | mddev->recovery = 0; |
| 5483 | spin_lock_irq(&conf->device_lock); |
| 5484 | mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5485 | conf->reshape_progress = MaxSector; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5486 | spin_unlock_irq(&conf->device_lock); |
| 5487 | return -EAGAIN; |
| 5488 | } |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5489 | conf->reshape_checkpoint = jiffies; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5490 | md_wakeup_thread(mddev->sync_thread); |
| 5491 | md_new_event(mddev); |
| 5492 | return 0; |
| 5493 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5494 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5495 | /* This is called from the reshape thread and should make any |
| 5496 | * changes needed in 'conf' |
| 5497 | */ |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5498 | static void end_reshape(raid5_conf_t *conf) |
| 5499 | { |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5500 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5501 | if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) { |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 5502 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5503 | spin_lock_irq(&conf->device_lock); |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5504 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5505 | conf->reshape_progress = MaxSector; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5506 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 5507 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5508 | |
| 5509 | /* read-ahead size must cover two whole stripes, which is |
| 5510 | * 2 * (datadisks) * chunksize where 'n' is the number of raid devices |
| 5511 | */ |
| 5512 | { |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5513 | int data_disks = conf->raid_disks - conf->max_degraded; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5514 | int stripe = data_disks * ((conf->chunk_sectors << 9) |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5515 | / PAGE_SIZE); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5516 | if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 5517 | conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
| 5518 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5519 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5520 | } |
| 5521 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5522 | /* This is called from the raid5d thread with mddev_lock held. |
| 5523 | * It makes config changes to the device. |
| 5524 | */ |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5525 | static void raid5_finish_reshape(mddev_t *mddev) |
| 5526 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 5527 | raid5_conf_t *conf = mddev->private; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5528 | |
| 5529 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 5530 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5531 | if (mddev->delta_disks > 0) { |
| 5532 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
| 5533 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5534 | revalidate_disk(mddev->gendisk); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5535 | } else { |
| 5536 | int d; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5537 | mddev->degraded = conf->raid_disks; |
| 5538 | for (d = 0; d < conf->raid_disks ; d++) |
| 5539 | if (conf->disks[d].rdev && |
| 5540 | test_bit(In_sync, |
| 5541 | &conf->disks[d].rdev->flags)) |
| 5542 | mddev->degraded--; |
| 5543 | for (d = conf->raid_disks ; |
| 5544 | d < conf->raid_disks - mddev->delta_disks; |
NeilBrown | 1a67dde | 2009-08-13 10:41:49 +1000 | [diff] [blame] | 5545 | d++) { |
| 5546 | mdk_rdev_t *rdev = conf->disks[d].rdev; |
| 5547 | if (rdev && raid5_remove_disk(mddev, d) == 0) { |
| 5548 | char nm[20]; |
| 5549 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 5550 | sysfs_remove_link(&mddev->kobj, nm); |
| 5551 | rdev->raid_disk = -1; |
| 5552 | } |
| 5553 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5554 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5555 | mddev->layout = conf->algorithm; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5556 | mddev->chunk_sectors = conf->chunk_sectors; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5557 | mddev->reshape_position = MaxSector; |
| 5558 | mddev->delta_disks = 0; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5559 | } |
| 5560 | } |
| 5561 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5562 | static void raid5_quiesce(mddev_t *mddev, int state) |
| 5563 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 5564 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5565 | |
| 5566 | switch(state) { |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5567 | case 2: /* resume for a suspend */ |
| 5568 | wake_up(&conf->wait_for_overlap); |
| 5569 | break; |
| 5570 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5571 | case 1: /* stop all writes */ |
| 5572 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5573 | /* '2' tells resync/reshape to pause so that all |
| 5574 | * active stripes can drain |
| 5575 | */ |
| 5576 | conf->quiesce = 2; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5577 | wait_event_lock_irq(conf->wait_for_stripe, |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5578 | atomic_read(&conf->active_stripes) == 0 && |
| 5579 | atomic_read(&conf->active_aligned_reads) == 0, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5580 | conf->device_lock, /* nothing */); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5581 | conf->quiesce = 1; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5582 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5583 | /* allow reshape to continue */ |
| 5584 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5585 | break; |
| 5586 | |
| 5587 | case 0: /* re-enable writes */ |
| 5588 | spin_lock_irq(&conf->device_lock); |
| 5589 | conf->quiesce = 0; |
| 5590 | wake_up(&conf->wait_for_stripe); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5591 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5592 | spin_unlock_irq(&conf->device_lock); |
| 5593 | break; |
| 5594 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5595 | } |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5596 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5597 | |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5598 | static void *raid45_takeover_raid0(mddev_t *mddev, int level) |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 5599 | { |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5600 | struct raid0_private_data *raid0_priv = mddev->private; |
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 | /* for raid0 takeover only one zone is supported */ |
| 5603 | if (raid0_priv->nr_strip_zones > 1) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5604 | printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n", |
| 5605 | mdname(mddev)); |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5606 | return ERR_PTR(-EINVAL); |
| 5607 | } |
| 5608 | |
| 5609 | mddev->new_level = level; |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 5610 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 5611 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
| 5612 | mddev->raid_disks += 1; |
| 5613 | mddev->delta_disks = 1; |
| 5614 | /* make sure it will be not marked as dirty */ |
| 5615 | mddev->recovery_cp = MaxSector; |
| 5616 | |
| 5617 | return setup_conf(mddev); |
| 5618 | } |
| 5619 | |
| 5620 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5621 | static void *raid5_takeover_raid1(mddev_t *mddev) |
| 5622 | { |
| 5623 | int chunksect; |
| 5624 | |
| 5625 | if (mddev->raid_disks != 2 || |
| 5626 | mddev->degraded > 1) |
| 5627 | return ERR_PTR(-EINVAL); |
| 5628 | |
| 5629 | /* Should check if there are write-behind devices? */ |
| 5630 | |
| 5631 | chunksect = 64*2; /* 64K by default */ |
| 5632 | |
| 5633 | /* The array must be an exact multiple of chunksize */ |
| 5634 | while (chunksect && (mddev->array_sectors & (chunksect-1))) |
| 5635 | chunksect >>= 1; |
| 5636 | |
| 5637 | if ((chunksect<<9) < STRIPE_SIZE) |
| 5638 | /* array size does not allow a suitable chunk size */ |
| 5639 | return ERR_PTR(-EINVAL); |
| 5640 | |
| 5641 | mddev->new_level = 5; |
| 5642 | mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5643 | mddev->new_chunk_sectors = chunksect; |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5644 | |
| 5645 | return setup_conf(mddev); |
| 5646 | } |
| 5647 | |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 5648 | static void *raid5_takeover_raid6(mddev_t *mddev) |
| 5649 | { |
| 5650 | int new_layout; |
| 5651 | |
| 5652 | switch (mddev->layout) { |
| 5653 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 5654 | new_layout = ALGORITHM_LEFT_ASYMMETRIC; |
| 5655 | break; |
| 5656 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 5657 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC; |
| 5658 | break; |
| 5659 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 5660 | new_layout = ALGORITHM_LEFT_SYMMETRIC; |
| 5661 | break; |
| 5662 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 5663 | new_layout = ALGORITHM_RIGHT_SYMMETRIC; |
| 5664 | break; |
| 5665 | case ALGORITHM_PARITY_0_6: |
| 5666 | new_layout = ALGORITHM_PARITY_0; |
| 5667 | break; |
| 5668 | case ALGORITHM_PARITY_N: |
| 5669 | new_layout = ALGORITHM_PARITY_N; |
| 5670 | break; |
| 5671 | default: |
| 5672 | return ERR_PTR(-EINVAL); |
| 5673 | } |
| 5674 | mddev->new_level = 5; |
| 5675 | mddev->new_layout = new_layout; |
| 5676 | mddev->delta_disks = -1; |
| 5677 | mddev->raid_disks -= 1; |
| 5678 | return setup_conf(mddev); |
| 5679 | } |
| 5680 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5681 | |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5682 | static int raid5_check_reshape(mddev_t *mddev) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5683 | { |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5684 | /* For a 2-drive array, the layout and chunk size can be changed |
| 5685 | * immediately as not restriping is needed. |
| 5686 | * For larger arrays we record the new value - after validation |
| 5687 | * to be used by a reshape pass. |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5688 | */ |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 5689 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5690 | int new_chunk = mddev->new_chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5691 | |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5692 | if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5693 | return -EINVAL; |
| 5694 | if (new_chunk > 0) { |
Andre Noll | 0ba459d | 2009-06-18 08:46:10 +1000 | [diff] [blame] | 5695 | if (!is_power_of_2(new_chunk)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5696 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5697 | if (new_chunk < (PAGE_SIZE>>9)) |
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 (mddev->array_sectors & (new_chunk-1)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5700 | /* not factor of array size */ |
| 5701 | return -EINVAL; |
| 5702 | } |
| 5703 | |
| 5704 | /* They look valid */ |
| 5705 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5706 | if (mddev->raid_disks == 2) { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5707 | /* can make the change immediately */ |
| 5708 | if (mddev->new_layout >= 0) { |
| 5709 | conf->algorithm = mddev->new_layout; |
| 5710 | mddev->layout = mddev->new_layout; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5711 | } |
| 5712 | if (new_chunk > 0) { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5713 | conf->chunk_sectors = new_chunk ; |
| 5714 | mddev->chunk_sectors = new_chunk; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5715 | } |
| 5716 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 5717 | md_wakeup_thread(mddev->thread); |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5718 | } |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5719 | return check_reshape(mddev); |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5720 | } |
| 5721 | |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5722 | static int raid6_check_reshape(mddev_t *mddev) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5723 | { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5724 | int new_chunk = mddev->new_chunk_sectors; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5725 | |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5726 | if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5727 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5728 | if (new_chunk > 0) { |
Andre Noll | 0ba459d | 2009-06-18 08:46:10 +1000 | [diff] [blame] | 5729 | if (!is_power_of_2(new_chunk)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5730 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5731 | if (new_chunk < (PAGE_SIZE >> 9)) |
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 (mddev->array_sectors & (new_chunk-1)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5734 | /* not factor of array size */ |
| 5735 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5736 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5737 | |
| 5738 | /* They look valid */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5739 | return check_reshape(mddev); |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5740 | } |
| 5741 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5742 | static void *raid5_takeover(mddev_t *mddev) |
| 5743 | { |
| 5744 | /* raid5 can take over: |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5745 | * raid0 - if there is only one strip zone - make it a raid4 layout |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5746 | * raid1 - if there are two drives. We need to know the chunk size |
| 5747 | * raid4 - trivial - just use a raid4 layout. |
| 5748 | * raid6 - Providing it is a *_6 layout |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5749 | */ |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5750 | if (mddev->level == 0) |
| 5751 | return raid45_takeover_raid0(mddev, 5); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5752 | if (mddev->level == 1) |
| 5753 | return raid5_takeover_raid1(mddev); |
NeilBrown | e9d4758 | 2009-03-31 14:57:09 +1100 | [diff] [blame] | 5754 | if (mddev->level == 4) { |
| 5755 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 5756 | mddev->new_level = 5; |
| 5757 | return setup_conf(mddev); |
| 5758 | } |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 5759 | if (mddev->level == 6) |
| 5760 | return raid5_takeover_raid6(mddev); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5761 | |
| 5762 | return ERR_PTR(-EINVAL); |
| 5763 | } |
| 5764 | |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 5765 | static void *raid4_takeover(mddev_t *mddev) |
| 5766 | { |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5767 | /* raid4 can take over: |
| 5768 | * raid0 - if there is only one strip zone |
| 5769 | * raid5 - if layout is right |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 5770 | */ |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 5771 | if (mddev->level == 0) |
| 5772 | return raid45_takeover_raid0(mddev, 4); |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 5773 | if (mddev->level == 5 && |
| 5774 | mddev->layout == ALGORITHM_PARITY_N) { |
| 5775 | mddev->new_layout = 0; |
| 5776 | mddev->new_level = 4; |
| 5777 | return setup_conf(mddev); |
| 5778 | } |
| 5779 | return ERR_PTR(-EINVAL); |
| 5780 | } |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5781 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5782 | static struct mdk_personality raid5_personality; |
| 5783 | |
| 5784 | static void *raid6_takeover(mddev_t *mddev) |
| 5785 | { |
| 5786 | /* Currently can only take over a raid5. We map the |
| 5787 | * personality to an equivalent raid6 personality |
| 5788 | * with the Q block at the end. |
| 5789 | */ |
| 5790 | int new_layout; |
| 5791 | |
| 5792 | if (mddev->pers != &raid5_personality) |
| 5793 | return ERR_PTR(-EINVAL); |
| 5794 | if (mddev->degraded > 1) |
| 5795 | return ERR_PTR(-EINVAL); |
| 5796 | if (mddev->raid_disks > 253) |
| 5797 | return ERR_PTR(-EINVAL); |
| 5798 | if (mddev->raid_disks < 3) |
| 5799 | return ERR_PTR(-EINVAL); |
| 5800 | |
| 5801 | switch (mddev->layout) { |
| 5802 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 5803 | new_layout = ALGORITHM_LEFT_ASYMMETRIC_6; |
| 5804 | break; |
| 5805 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 5806 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6; |
| 5807 | break; |
| 5808 | case ALGORITHM_LEFT_SYMMETRIC: |
| 5809 | new_layout = ALGORITHM_LEFT_SYMMETRIC_6; |
| 5810 | break; |
| 5811 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 5812 | new_layout = ALGORITHM_RIGHT_SYMMETRIC_6; |
| 5813 | break; |
| 5814 | case ALGORITHM_PARITY_0: |
| 5815 | new_layout = ALGORITHM_PARITY_0_6; |
| 5816 | break; |
| 5817 | case ALGORITHM_PARITY_N: |
| 5818 | new_layout = ALGORITHM_PARITY_N; |
| 5819 | break; |
| 5820 | default: |
| 5821 | return ERR_PTR(-EINVAL); |
| 5822 | } |
| 5823 | mddev->new_level = 6; |
| 5824 | mddev->new_layout = new_layout; |
| 5825 | mddev->delta_disks = 1; |
| 5826 | mddev->raid_disks += 1; |
| 5827 | return setup_conf(mddev); |
| 5828 | } |
| 5829 | |
| 5830 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5831 | static struct mdk_personality raid6_personality = |
| 5832 | { |
| 5833 | .name = "raid6", |
| 5834 | .level = 6, |
| 5835 | .owner = THIS_MODULE, |
| 5836 | .make_request = make_request, |
| 5837 | .run = run, |
| 5838 | .stop = stop, |
| 5839 | .status = status, |
| 5840 | .error_handler = error, |
| 5841 | .hot_add_disk = raid5_add_disk, |
| 5842 | .hot_remove_disk= raid5_remove_disk, |
| 5843 | .spare_active = raid5_spare_active, |
| 5844 | .sync_request = sync_request, |
| 5845 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 5846 | .size = raid5_size, |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5847 | .check_reshape = raid6_check_reshape, |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5848 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5849 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5850 | .quiesce = raid5_quiesce, |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5851 | .takeover = raid6_takeover, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5852 | }; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5853 | static struct mdk_personality raid5_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5854 | { |
| 5855 | .name = "raid5", |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5856 | .level = 5, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5857 | .owner = THIS_MODULE, |
| 5858 | .make_request = make_request, |
| 5859 | .run = run, |
| 5860 | .stop = stop, |
| 5861 | .status = status, |
| 5862 | .error_handler = error, |
| 5863 | .hot_add_disk = raid5_add_disk, |
| 5864 | .hot_remove_disk= raid5_remove_disk, |
| 5865 | .spare_active = raid5_spare_active, |
| 5866 | .sync_request = sync_request, |
| 5867 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 5868 | .size = raid5_size, |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5869 | .check_reshape = raid5_check_reshape, |
| 5870 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5871 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5872 | .quiesce = raid5_quiesce, |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5873 | .takeover = raid5_takeover, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5874 | }; |
| 5875 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5876 | static struct mdk_personality raid4_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5877 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5878 | .name = "raid4", |
| 5879 | .level = 4, |
| 5880 | .owner = THIS_MODULE, |
| 5881 | .make_request = make_request, |
| 5882 | .run = run, |
| 5883 | .stop = stop, |
| 5884 | .status = status, |
| 5885 | .error_handler = error, |
| 5886 | .hot_add_disk = raid5_add_disk, |
| 5887 | .hot_remove_disk= raid5_remove_disk, |
| 5888 | .spare_active = raid5_spare_active, |
| 5889 | .sync_request = sync_request, |
| 5890 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 5891 | .size = raid5_size, |
NeilBrown | 3d37890 | 2007-03-26 21:32:13 -0800 | [diff] [blame] | 5892 | .check_reshape = raid5_check_reshape, |
| 5893 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5894 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5895 | .quiesce = raid5_quiesce, |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 5896 | .takeover = raid4_takeover, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5897 | }; |
| 5898 | |
| 5899 | static int __init raid5_init(void) |
| 5900 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5901 | register_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5902 | register_md_personality(&raid5_personality); |
| 5903 | register_md_personality(&raid4_personality); |
| 5904 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5905 | } |
| 5906 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5907 | static void raid5_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5908 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5909 | unregister_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5910 | unregister_md_personality(&raid5_personality); |
| 5911 | unregister_md_personality(&raid4_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5912 | } |
| 5913 | |
| 5914 | module_init(raid5_init); |
| 5915 | module_exit(raid5_exit); |
| 5916 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 5917 | MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5918 | MODULE_ALIAS("md-personality-4"); /* RAID5 */ |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5919 | MODULE_ALIAS("md-raid5"); |
| 5920 | MODULE_ALIAS("md-raid4"); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5921 | MODULE_ALIAS("md-level-5"); |
| 5922 | MODULE_ALIAS("md-level-4"); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5923 | MODULE_ALIAS("md-personality-8"); /* RAID6 */ |
| 5924 | MODULE_ALIAS("md-raid6"); |
| 5925 | MODULE_ALIAS("md-level-6"); |
| 5926 | |
| 5927 | /* This used to be two separate modules, they were: */ |
| 5928 | MODULE_ALIAS("raid5"); |
| 5929 | MODULE_ALIAS("raid6"); |